From 642e24552246c9a0e64053dcf7d2bdef94a6e6c6 Mon Sep 17 00:00:00 2001 From: Matheus Date: Fri, 24 May 2024 22:01:11 -0300 Subject: [PATCH] syscalls: sync --- kernel-src/arch/x86-64/isr.asm | 1 + kernel-src/arch/x86-64/syscall.asm | 4 ++- kernel-src/arch/x86-64/syscalllog.c | 8 +++-- kernel-src/mm/vmmcache.c | 43 ++++++++++++++++++++++---- kernel-src/sys/syscalls/sync.c | 8 +++++ patches/mlibc/jinx-working-patch.patch | 14 ++++++--- 6 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 kernel-src/sys/syscalls/sync.c diff --git a/kernel-src/arch/x86-64/isr.asm b/kernel-src/arch/x86-64/isr.asm index bac8efaa..8077e442 100644 --- a/kernel-src/arch/x86-64/isr.asm +++ b/kernel-src/arch/x86-64/isr.asm @@ -48,6 +48,7 @@ isr_%+ i: cld extern interrupt_isr call interrupt_isr + cli ; pop context add rsp, 24 ; cr2 gs and fs are not popped. diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index 1d460897..241d0928 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -64,6 +64,7 @@ extern syscall_kill extern syscall_sigreturn extern syscall_uname extern syscall_hostname +extern syscall_sync syscalltab: dq syscall_print dq syscall_mmap @@ -130,7 +131,8 @@ dq syscall_kill dq syscall_sigreturn dq syscall_uname dq syscall_hostname -syscallcount equ 65 +dq syscall_sync +syscallcount equ 66 section .text global arch_syscall_entry ; on entry: diff --git a/kernel-src/arch/x86-64/syscalllog.c b/kernel-src/arch/x86-64/syscalllog.c index 9f7c7ae0..e38aaa4a 100644 --- a/kernel-src/arch/x86-64/syscalllog.c +++ b/kernel-src/arch/x86-64/syscalllog.c @@ -4,7 +4,7 @@ #ifdef SYSCALL_LOGGING -#define SYSCALL_COUNT 65 +#define SYSCALL_COUNT 66 #define LOGSTR(x) arch_e9_puts(x) static char *name[] = { @@ -72,7 +72,8 @@ static char *name[] = { "kill", "sigreturn", "uname", - "hostname" + "hostname", + "sync" }; static char *args[] = { @@ -140,7 +141,8 @@ static char *args[] = { "pid %d signal %d", // kill "N/A", // sigreturn "utsname %p", // uname - "new %p newsize %lu old %p oldsize %lu" // hostname + "new %p newsize %lu old %p oldsize %lu", // hostname + "N/A" // sync }; #endif diff --git a/kernel-src/mm/vmmcache.c b/kernel-src/mm/vmmcache.c index a0b0766b..6656806c 100644 --- a/kernel-src/mm/vmmcache.c +++ b/kernel-src/mm/vmmcache.c @@ -2,6 +2,7 @@ #include #include #include +#include #define TABLE_SIZE 4096 #define WRITER_TICK_SECONDS 1 @@ -11,6 +12,7 @@ static page_t **table; static thread_t *writerthread; static semaphore_t sync; +static pollheader_t syncpollheader; #define HOLD_LOCK() \ MUTEX_ACQUIRE(&mutex, false); @@ -234,13 +236,40 @@ int vmmcache_syncvnode(vnode_t *vnode) { __assert(!"unimplemented"); } -int vmmcache_sync() { - semaphore_signal(&sync); - return 0; -} - static page_t *dirtylist; +int vmmcache_sync() { + polldesc_t polldesc = {0}; + int e = poll_initdesc(&polldesc, 1); + HOLD_LOCK(); + if (dirtylist == NULL) { + // no dirty pages + RELEASE_LOCK(); + if (e == 0) + goto leave; + + return 0; + } else if (e) { + // there are dirty pages but poll alloc failed, + // tell the thread to do its thing but don't wait + RELEASE_LOCK(); + printf("vmmcache: sync not waiting because of a poll allocation failure\n"); + semaphore_signal(&sync); + return e; + } + + poll_add(&syncpollheader, &polldesc.data[0], POLLPRI); + semaphore_signal(&sync); + RELEASE_LOCK(); + + e = poll_dowait(&polldesc, 0); + + leave: + poll_leave(&polldesc); + poll_destroydesc(&polldesc); + return e; +} + int vmmcache_makedirty(page_t *page) { HOLD_LOCK(); @@ -264,7 +293,7 @@ int vmmcache_makedirty(page_t *page) { } static void tick(context_t *, dpcarg_t arg) { - vmmcache_sync(); + semaphore_signal(&sync); } static void writer() { @@ -278,6 +307,7 @@ static void writer() { // TODO make FIFO rather than LIFO volatile page_t *page = dirtylist; if (page == NULL) { + poll_event(&syncpollheader, POLLPRI); RELEASE_LOCK(); semaphore_wait(&sync, false); continue; @@ -313,4 +343,5 @@ void vmmcache_init() { __assert(writerthread); sched_queue(writerthread); vmmcache_sync(); + POLL_INITHEADER(&syncpollheader); } diff --git a/kernel-src/sys/syscalls/sync.c b/kernel-src/sys/syscalls/sync.c new file mode 100644 index 00000000..6ea965c4 --- /dev/null +++ b/kernel-src/sys/syscalls/sync.c @@ -0,0 +1,8 @@ +#include +#include + +syscallret_t syscall_sync(context_t *) { + syscallret_t ret = {0}; + vmmcache_sync(); + return ret; +} diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index 10e99309..ff12cb54 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -192,10 +192,10 @@ index 0000000..e5b087a + diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp new file mode 100644 -index 0000000..c409d89 +index 0000000..12da52c --- /dev/null +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp -@@ -0,0 +1,690 @@ +@@ -0,0 +1,695 @@ +#include +#include +#include @@ -315,6 +315,11 @@ index 0000000..c409d89 + return syscall(SYSCALL_UNAME, &ret, (uint64_t)buf); + } + ++ void sys_sync() { ++ long ret; ++ syscall(SYSCALL_SYNC, &ret); ++ } ++ + int sys_fsync(int fd) { + // TODO as the VFS does not do caching as of yet, this system call is stubbed and should be implemented once caching is done + return 0; @@ -905,10 +910,10 @@ index 0000000..990eca0 +#endif diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h new file mode 100644 -index 0000000..3a130be +index 0000000..7daaf11 --- /dev/null +++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h -@@ -0,0 +1,88 @@ +@@ -0,0 +1,89 @@ +#ifndef _SYSCALL_H_INCLUDE +#define _SYSCALL_H_INCLUDE + @@ -977,6 +982,7 @@ index 0000000..3a130be +#define SYSCALL_SIGRETURN 62 +#define SYSCALL_UNAME 63 +#define SYSCALL_HOSTNAME 64 ++#define SYSCALL_SYNC 65 + +#include +#include