Add mutex syscalls

This commit is contained in:
2026-01-20 22:18:43 +01:00
parent fff51321bc
commit 7eceecf6e3
9 changed files with 122 additions and 22 deletions

View File

@@ -9,5 +9,9 @@
#define SYS_SCHED 6 #define SYS_SCHED 6
#define SYS_CREATE_MEM 7 #define SYS_CREATE_MEM 7
#define SYS_UNLINK_MEM 8 #define SYS_UNLINK_MEM 8
#define SYS_CREATE_MUTEX 9
#define SYS_UNLINK_MUTEX 10
#define SYS_LOCK_MUTEX 11
#define SYS_UNLOCK_MUTEX 12
#endif // _M_SYSCALL_DEFS_H #endif // _M_SYSCALL_DEFS_H

View File

@@ -9,17 +9,21 @@
#define EXAMPLE 1 #define EXAMPLE 1
#if EXAMPLE == 1 #if EXAMPLE == 1
void app_main (void) {
test ('a');
test ('a');
test ('a');
int* xs = malloc (1024 * sizeof (*xs)); void app_thread1 (void) {
memset (xs, 123, 1024 * sizeof (*xs)); test ('b');
free (xs); quit ();
test ('a');
test ('a');
test ('a');
} }
int spawn (void (*fn) (void)) {
size_t stack_size = 256 * PAGE_SIZE;
void* stack = malloc (stack_size);
if (stack == NULL)
return -ST_OOM_ERROR;
uintptr_t stack_top = (uintptr_t)stack + stack_size;
return clone (stack_top, stack_size, fn);
}
void app_main (void) { spawn (&app_thread1); }
#endif #endif

View File

@@ -43,11 +43,11 @@ void proc_cleanup_resources (struct proc* proc) {
void proc_drop_resource (struct proc* proc, struct proc_resource* resource, bool lock) { void proc_drop_resource (struct proc* proc, struct proc_resource* resource, bool lock) {
spin_lock_ctx_t ctxrs; spin_lock_ctx_t ctxrs;
if (atomic_fetch_sub (&resource->refs, 1) == 1) {
DEBUG ("resource=%p created_by=%d vis=%d type=%d rid=%d refs=%d\n", resource, DEBUG ("resource=%p created_by=%d vis=%d type=%d rid=%d refs=%d\n", resource,
resource->created_by_pid, resource->visibility, resource->type, resource->rid, resource->created_by_pid, resource->visibility, resource->type, resource->rid,
atomic_load (&resource->refs)); atomic_load (&resource->refs));
if (atomic_fetch_sub (&resource->refs, 1) == 1) {
switch (resource->visibility) { switch (resource->visibility) {
case RV_PRIVATE: { case RV_PRIVATE: {
if (lock) if (lock)

View File

@@ -177,12 +177,84 @@ DEFINE_SYSCALL (sys_clone) {
return pid; return pid;
} }
/* int proc_sched (void) */ /* int sched (void) */
DEFINE_SYSCALL (sys_sched) { DEFINE_SYSCALL (sys_sched) {
proc_sched (regs); proc_sched (regs);
return ST_OK; return ST_OK;
} }
/* int create_mutex (int mutex_rid, int vis) */
DEFINE_SYSCALL (sys_create_mutex) {
int mutex_rid = (int)a1;
int vis = (int)a2;
if (mutex_rid < 0)
return -ST_BAD_RESOURCE;
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
struct proc_resource* mutex_resource =
proc_create_resource (proc, mutex_rid, PR_MUTEX, vis, NULL);
if (mutex_resource == NULL)
return -ST_OOM_ERROR;
return mutex_resource->rid;
}
/* int unlink_mutex (int mutex_rid, int vis) */
DEFINE_SYSCALL (sys_unlink_mutex) {
int mutex_rid = (int)a1;
int vis = (int)a2;
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
struct proc_resource* mutex_resource = proc_find_resource (proc, mutex_rid, vis);
if (mutex_resource == NULL)
return -ST_NOT_FOUND;
proc_drop_resource (proc, mutex_resource, true);
return ST_OK;
}
/* int lock_mutex (int mutex_rid, int vis) */
DEFINE_SYSCALL (sys_lock_mutex) {
int mutex_rid = (int)a1;
int vis = (int)a2;
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
struct proc_resource* mutex_resource = proc_find_resource (proc, mutex_rid, vis);
if (mutex_resource == NULL)
return -ST_NOT_FOUND;
proc_mutex_lock (proc, &mutex_resource->u.mutex);
return ST_OK;
}
/* int unlock_mutex (int mutex_rid, int vis) */
DEFINE_SYSCALL (sys_unlock_mutex) {
int mutex_rid = (int)a1;
int vis = (int)a2;
if (!(vis == RV_PUBLIC || vis == RV_PRIVATE))
return -ST_BAD_RESOURCE;
struct proc_resource* mutex_resource = proc_find_resource (proc, mutex_rid, vis);
if (mutex_resource == NULL)
return -ST_NOT_FOUND;
return proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK : -ST_PERMISSION_ERROR;
}
static syscall_handler_func_t handler_table[] = { static syscall_handler_func_t handler_table[] = {
[SYS_QUIT] = &sys_quit, [SYS_QUIT] = &sys_quit,
[SYS_TEST] = &sys_test, [SYS_TEST] = &sys_test,
@@ -192,6 +264,10 @@ static syscall_handler_func_t handler_table[] = {
[SYS_SCHED] = &sys_sched, [SYS_SCHED] = &sys_sched,
[SYS_CREATE_MEM] = &sys_create_mem, [SYS_CREATE_MEM] = &sys_create_mem,
[SYS_UNLINK_MEM] = &sys_unlink_mem, [SYS_UNLINK_MEM] = &sys_unlink_mem,
[SYS_CREATE_MUTEX] = &sys_create_mutex,
[SYS_UNLINK_MUTEX] = &sys_unlink_mutex,
[SYS_LOCK_MUTEX] = &sys_lock_mutex,
[SYS_UNLOCK_MUTEX] = &sys_unlock_mutex,
}; };
syscall_handler_func_t syscall_find_handler (int syscall_num) { syscall_handler_func_t syscall_find_handler (int syscall_num) {

View File

@@ -4,14 +4,18 @@
#include <alloc/liballoc.h> #include <alloc/liballoc.h>
#include <m/system.h> #include <m/system.h>
#define LIBALLOC_MUTEX 500
static uintptr_t liballoc_map_base = PROC_MAP_BASE; static uintptr_t liballoc_map_base = PROC_MAP_BASE;
static int mem_rid_base = 1000000; static int mem_rid_base = 1000000;
void liballoc_init (void) {} void liballoc_init (void) { create_mutex (LIBALLOC_MUTEX, RV_PRIVATE); }
int liballoc_lock (void) { return 0; } void liballoc_deinit (void) { unlink_mutex (LIBALLOC_MUTEX, RV_PRIVATE); }
int liballoc_unlock (void) { return 0; } int liballoc_lock (void) { return lock_mutex (LIBALLOC_MUTEX, RV_PRIVATE); }
int liballoc_unlock (void) { return unlock_mutex (LIBALLOC_MUTEX, RV_PRIVATE); }
void* liballoc_alloc (int pages, int* mem_rid) { void* liballoc_alloc (int pages, int* mem_rid) {
uintptr_t current_base = liballoc_map_base; uintptr_t current_base = liballoc_map_base;

View File

@@ -87,6 +87,7 @@ void* calloc (size_t, size_t); //< The standard function.
void free (void*); //< The standard function. void free (void*); //< The standard function.
void liballoc_init (void); void liballoc_init (void);
void liballoc_deinit (void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -17,8 +17,7 @@ static void clear_bss (void) {
void __premain (void) { void __premain (void) {
clear_bss (); clear_bss ();
liballoc_init (); liballoc_init ();
app_main (); app_main ();
liballoc_deinit ();
quit (); quit ();
} }

View File

@@ -9,7 +9,7 @@
#define do_syscall(...) do_syscall1 (__VA_ARGS__, 0, 0, 0, 0, 0, 0) #define do_syscall(...) do_syscall1 (__VA_ARGS__, 0, 0, 0, 0, 0, 0)
int quit (void) { return do_syscall (SYS_QUIT); } int quit (void) { return do_syscall (SYS_QUIT, 0); }
int test (char c) { return do_syscall (SYS_TEST, c); } int test (char c) { return do_syscall (SYS_TEST, c); }
@@ -30,3 +30,11 @@ int unlink_mem (int mem_rid, int vis, size_t pages) {
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void)) { int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void)) {
return do_syscall (SYS_CLONE, vstack_top, stack_size, entry); return do_syscall (SYS_CLONE, vstack_top, stack_size, entry);
} }
int create_mutex (int mutex_rid, int vis) { return do_syscall (SYS_CREATE_MUTEX, mutex_rid, vis); }
int unlink_mutex (int mutex_rid, int vis) { return do_syscall (SYS_UNLINK_MUTEX, mutex_rid, vis); }
int lock_mutex (int mutex_rid, int vis) { return do_syscall (SYS_LOCK_MUTEX, mutex_rid, vis); }
int unlock_mutex (int mutex_rid, int vis) { return do_syscall (SYS_UNLOCK_MUTEX, mutex_rid, vis); }

View File

@@ -24,5 +24,9 @@ int unmap (uintptr_t vaddr, size_t pages);
int create_mem (int mem_rid, int vis, size_t pages); int create_mem (int mem_rid, int vis, size_t pages);
int unlink_mem (int mem_rid, int vis, size_t pages); int unlink_mem (int mem_rid, int vis, size_t pages);
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void)); int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void));
int create_mutex (int mutex_rid, int vis);
int unlink_mutex (int mutex_rid, int vis);
int lock_mutex (int mutex_rid, int vis);
int unlock_mutex (int mutex_rid, int vis);
#endif // _LIBMSL_M_SYSTEM_H #endif // _LIBMSL_M_SYSTEM_H