Compare commits

..

3 Commits

Author SHA1 Message Date
8fa72f3cf0
kernel+libc: Implement read()
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-11 18:02:50 +01:00
810c4bc257
kernel+libc: Start interfacing with the VFS from userspace (open & close)
This commit adds open and close syscalls to the kernel, and adds matching wrappers to libc.

No read/write support, so file descriptors are kind of useless for now.
2023-03-11 17:45:20 +01:00
831973c39a
Option: Allow direct access to the underlying value via operator->
Crashes if the Option is empty.
2023-03-11 17:41:11 +01:00
16 changed files with 185 additions and 7 deletions

View File

@ -1,6 +1,8 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void bye()
{
@ -12,13 +14,20 @@ int main()
atexit(bye);
printf("Welcome to %s from userspace!\n", "Luna");
char* address = (char*)malloc(1);
printf("address: %p\n", address);
printf("memory at address: %c\n", *address);
*address = 'e';
printf("memory at address: %c\n", *address);
int fd = open("/etc/motd", 0);
if (fd < 0)
{
perror("open");
return 1;
}
free(address);
char buffer[512];
ssize_t nread = read(fd, buffer, sizeof(buffer));
buffer[nread] = 0;
printf("/etc/motd says: %s", buffer);
close(fd);
time_t now = time(NULL);
printf("date: %s", ctime(&now));

1
initrd/etc/motd Normal file
View File

@ -0,0 +1 @@
G'day mate!

View File

@ -26,6 +26,8 @@ set(SOURCES
src/sys/clock_gettime.cpp
src/sys/allocate_memory.cpp
src/sys/usleep.cpp
src/sys/open.cpp
src/sys/file.cpp
src/fs/VFS.cpp
src/fs/tmpfs/FileSystem.cpp
src/InitRD.cpp

28
kernel/src/sys/file.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "Log.h"
#include "fs/VFS.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
Result<u64> sys_read(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
u8* buf = (u8*)args[1];
usize size = (usize)args[2];
if (!MemoryManager::validate_user_write(buf, size)) return err(EFAULT);
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
usize nread = TRY(descriptor->inode->read(buf, descriptor->offset, size));
descriptor->offset += nread;
return nread;
}

46
kernel/src/sys/open.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "Log.h"
#include "fs/VFS.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
Result<u64> sys_open(Registers*, SyscallArgs args)
{
u64 path_address = args[0];
if (!MemoryManager::validate_userspace_string(path_address)) return err(EFAULT);
const char* path = (const char*)path_address;
int flags = (int)args[1];
Thread* current = Scheduler::current();
kinfoln("open: trying to open file %s, flags %d", path, flags);
auto inode = TRY(VFS::resolve_path(path));
int fd = TRY(current->allocate_fd(0));
current->fd_table->fds[fd] = FileDescriptor { inode, 0, flags };
kinfoln("open: allocated file descriptor %d for inode %zu", fd, inode->inode_number());
return (u64)fd;
}
Result<u64> sys_close(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
kinfoln("close: closing file descriptor %d (was referencing inode %zu)", fd, descriptor->inode->inode_number());
descriptor = {};
return 0;
}

View File

@ -151,6 +151,8 @@ namespace Scheduler
guard.deactivate();
directory_guard.deactivate();
thread->fd_table = FileDescriptorTable {};
kinfoln("Created userspace thread: id %lu with ip %#.16lx and sp %#.16lx (ksp %#lx)", thread->id, thread->ip(),
thread->sp(), thread->kernel_stack.top());

View File

@ -19,3 +19,16 @@ Result<Thread*> new_thread()
return thread;
}
Result<int> Thread::allocate_fd(int min)
{
if (min < 0 || min >= FD_MAX) return err(EINVAL);
for (int i = min; i < FD_MAX; i++)
{
// FIXME: Possible race condition if multiple threads share a FileDescriptorTable? Let's not worry about it for
// now, we're still a long way away from reaching that point.
if (!fd_table->fds[i].has_value()) { return i; }
}
return err(EMFILE);
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "arch/MMU.h"
#include "fs/VFS.h"
#include "memory/UserVM.h"
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
@ -21,6 +22,20 @@ enum class ThreadState
Dying
};
struct FileDescriptor
{
SharedPtr<VFS::Inode> inode;
usize offset { 0 };
int flags { 0 };
};
static constexpr int FD_MAX = 64;
struct FileDescriptorTable
{
Option<FileDescriptor> fds[FD_MAX] = {};
};
struct Thread : public LinkedListNode<Thread>
{
Registers regs;
@ -38,6 +53,9 @@ struct Thread : public LinkedListNode<Thread>
Stack kernel_stack;
OwnedPtr<UserVM> vm_allocator;
Option<FileDescriptorTable> fd_table;
Result<int> allocate_fd(int min);
FPData fp_data;

View File

@ -7,6 +7,7 @@ set(SOURCES
src/unistd.cpp
src/errno.cpp
src/string.cpp
src/fcntl.cpp
src/assert.cpp
src/atexit.cpp
src/ctype.cpp

18
libc/include/fcntl.h Normal file
View File

@ -0,0 +1,18 @@
/* fcntl.h: File control. */
#ifndef _FCNTL_H
#define _FCNTL_H
#ifdef __cplusplus
extern "C"
{
#endif
/* Open a file path and return a file descriptor to it. */
int open(const char* path, int flags);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -9,6 +9,7 @@
#include <bits/fixed-size-types.h>
typedef int pid_t;
typedef __i64_t ssize_t;
typedef __i64_t time_t;
typedef __u16_t mode_t;
typedef __u64_t useconds_t;

View File

@ -30,6 +30,12 @@ extern "C"
/* Sleeps for X seconds. */
unsigned long sleep(unsigned long seconds);
/* Closes a file descriptor. */
int close(int fd);
/* Reads bytes from a file descriptor. */
ssize_t read(int fd, void* buf, size_t size);
#ifdef __cplusplus
}
#endif

13
libc/src/fcntl.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <bits/errno-return.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C"
{
int open(const char* path, int flags)
{
long rc = syscall(SYS_open, path, flags);
__errno_return(rc, int);
}
}

View File

@ -37,4 +37,16 @@ extern "C"
syscall(SYS_usleep, seconds * 1000000);
return 0;
}
int close(int fd)
{
long rc = syscall(SYS_close, fd);
__errno_return(rc, int);
}
ssize_t read(int fd, void* buf, size_t size)
{
long rc = syscall(SYS_read, fd, buf, size);
__errno_return(rc, ssize_t);
}
}

View File

@ -109,6 +109,13 @@ template <typename T> class Option
return true;
}
// NOTE: This should also exist in Result.
T* operator->()
{
expect(has_value(), "Option::operator-> called on an empty Option");
return &m_storage.fetch_reference();
}
~Option()
{
if (has_value()) m_storage.destroy();

View File

@ -1,7 +1,8 @@
#pragma once
#define enumerate_syscalls(_e) \
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep)
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \
_e(close) _e(read)
enum Syscalls
{