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.
This commit is contained in:
apio 2023-03-11 17:45:20 +01:00
parent 831973c39a
commit 810c4bc257
Signed by: apio
GPG Key ID: B8A7D06E42258954
13 changed files with 131 additions and 7 deletions

View File

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

1
initrd/etc/motd Normal file
View File

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

View File

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

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

@ -0,0 +1,45 @@
#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();
if (!current->fd_table->fds[fd].has_value()) return err(EBADF);
kinfoln("close: closing file descriptor %d (was referencing inode %zu)", fd,
current->fd_table->fds[fd]->inode->inode_number());
current->fd_table->fds[fd] = {};
return 0;
}

View File

@ -151,6 +151,8 @@ namespace Scheduler
guard.deactivate(); guard.deactivate();
directory_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(), kinfoln("Created userspace thread: id %lu with ip %#.16lx and sp %#.16lx (ksp %#lx)", thread->id, thread->ip(),
thread->sp(), thread->kernel_stack.top()); thread->sp(), thread->kernel_stack.top());

View File

@ -19,3 +19,16 @@ Result<Thread*> new_thread()
return 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 #pragma once
#include "arch/MMU.h" #include "arch/MMU.h"
#include "fs/VFS.h"
#include "memory/UserVM.h" #include "memory/UserVM.h"
#include <luna/LinkedList.h> #include <luna/LinkedList.h>
#include <luna/OwnedPtr.h> #include <luna/OwnedPtr.h>
@ -21,6 +22,20 @@ enum class ThreadState
Dying 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> struct Thread : public LinkedListNode<Thread>
{ {
Registers regs; Registers regs;
@ -38,6 +53,9 @@ struct Thread : public LinkedListNode<Thread>
Stack kernel_stack; Stack kernel_stack;
OwnedPtr<UserVM> vm_allocator; OwnedPtr<UserVM> vm_allocator;
Option<FileDescriptorTable> fd_table;
Result<int> allocate_fd(int min);
FPData fp_data; FPData fp_data;

View File

@ -7,6 +7,7 @@ set(SOURCES
src/unistd.cpp src/unistd.cpp
src/errno.cpp src/errno.cpp
src/string.cpp src/string.cpp
src/fcntl.cpp
src/assert.cpp src/assert.cpp
src/atexit.cpp src/atexit.cpp
src/ctype.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

@ -30,6 +30,9 @@ extern "C"
/* Sleeps for X seconds. */ /* Sleeps for X seconds. */
unsigned long sleep(unsigned long seconds); unsigned long sleep(unsigned long seconds);
/* Closes a file descriptor. */
int close(int fd);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #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,10 @@ extern "C"
syscall(SYS_usleep, seconds * 1000000); syscall(SYS_usleep, seconds * 1000000);
return 0; return 0;
} }
int close(int fd)
{
long rc = syscall(SYS_close, fd);
__errno_return(rc, int);
}
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#define enumerate_syscalls(_e) \ #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)
enum Syscalls enum Syscalls
{ {