181 lines
4.7 KiB
C
181 lines
4.7 KiB
C
#include <aux/compiler.h>
|
|
#include <libk/assert.h>
|
|
#include <libk/std.h>
|
|
#include <limine/requests.h>
|
|
#include <m/status.h>
|
|
#include <m/syscall_defs.h>
|
|
#include <mm/pmm.h>
|
|
#include <proc/mutex.h>
|
|
#include <proc/proc.h>
|
|
#include <proc/procgroup.h>
|
|
#include <proc/resource.h>
|
|
#include <sync/spin_lock.h>
|
|
#include <sys/debug.h>
|
|
#include <sys/mm.h>
|
|
#include <sys/proc.h>
|
|
#include <syscall/syscall.h>
|
|
|
|
#define DEFINE_SYSCALL(name) \
|
|
uintptr_t name (struct proc* UNUSED proc, void* UNUSED regs, uintptr_t UNUSED a1, \
|
|
uintptr_t UNUSED a2, uintptr_t UNUSED a3, uintptr_t UNUSED a4, \
|
|
uintptr_t UNUSED a5, uintptr_t UNUSED a6)
|
|
|
|
#define SYSRESULT(x) ((uintptr_t)(x))
|
|
|
|
static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t size) {
|
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
spin_lock_ctx_t ctxpg;
|
|
|
|
spin_lock (&proc->procgroup->lock, &ctxpg);
|
|
|
|
if (!mm_validate_buffer (&proc->procgroup->pd, (uintptr_t)uvaddr, size)) {
|
|
spin_unlock (&proc->procgroup->lock, &ctxpg);
|
|
return NULL;
|
|
}
|
|
|
|
uintptr_t out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr);
|
|
|
|
spin_unlock (&proc->procgroup->lock, &ctxpg);
|
|
|
|
uintptr_t out_kvaddr = (uintptr_t)hhdm->offset + out_paddr;
|
|
|
|
return (void*)out_kvaddr;
|
|
}
|
|
|
|
/* int quit (void) */
|
|
DEFINE_SYSCALL (sys_quit) {
|
|
proc_kill (proc);
|
|
return SYSRESULT (ST_OK);
|
|
}
|
|
|
|
/* int test (void) */
|
|
DEFINE_SYSCALL (sys_test) {
|
|
char c = (char)a1;
|
|
DEBUG ("test syscall from %d! %c\n", proc->pid, c);
|
|
return SYSRESULT (ST_OK);
|
|
}
|
|
|
|
/* int map (uintptr_t vaddr, size_t pages, uint32_t flags) */
|
|
DEFINE_SYSCALL (sys_map) {
|
|
uintptr_t vaddr = a1;
|
|
size_t pages = (size_t)a2;
|
|
uint32_t flags = (uint32_t)a3;
|
|
|
|
if (vaddr % PAGE_SIZE != 0)
|
|
return SYSRESULT (-ST_UNALIGNED);
|
|
|
|
return SYSRESULT (procgroup_map (proc->procgroup, vaddr, pages, flags, NULL));
|
|
}
|
|
|
|
/* int unmap (uintptr_t vaddr, size_t pages) */
|
|
DEFINE_SYSCALL (sys_unmap) {
|
|
uintptr_t vaddr = a1;
|
|
size_t pages = (size_t)a2;
|
|
|
|
if (vaddr % PAGE_SIZE != 0)
|
|
return SYSRESULT (-ST_UNALIGNED);
|
|
|
|
return SYSRESULT (procgroup_unmap (proc->procgroup, vaddr, pages));
|
|
}
|
|
|
|
/* int clone (uintptr_t vstack_top, void* entry) */
|
|
DEFINE_SYSCALL (sys_clone) {
|
|
uintptr_t vstack_top = a1;
|
|
uintptr_t entry = a2;
|
|
|
|
struct proc* new = proc_clone (proc, vstack_top, entry);
|
|
|
|
DEBUG ("new=%p\n", new);
|
|
|
|
if (new == NULL) {
|
|
return SYSRESULT (-ST_OOM_ERROR);
|
|
}
|
|
|
|
int pid = new->pid;
|
|
|
|
proc_register (new, NULL);
|
|
|
|
return SYSRESULT (pid);
|
|
}
|
|
|
|
/* int sched (void) */
|
|
DEFINE_SYSCALL (sys_sched) {
|
|
proc_sched ();
|
|
return SYSRESULT (ST_OK);
|
|
}
|
|
|
|
/* int mutex_create (int mutex_rid) */
|
|
DEFINE_SYSCALL (sys_mutex_create) {
|
|
int mutex_rid = (int)a1;
|
|
|
|
struct proc_resource* mutex_resource = proc_create_resource_mutex (proc->procgroup, mutex_rid);
|
|
|
|
if (mutex_resource == NULL)
|
|
return SYSRESULT (-ST_OOM_ERROR);
|
|
|
|
return SYSRESULT (mutex_resource->rid);
|
|
}
|
|
|
|
/* int mutex_delete (int mutex_rid) */
|
|
DEFINE_SYSCALL (sys_mutex_delete) {
|
|
int mutex_rid = (int)a1;
|
|
|
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
|
|
|
if (mutex_resource == NULL)
|
|
return SYSRESULT (-ST_NOT_FOUND);
|
|
|
|
proc_delete_resource (mutex_resource);
|
|
|
|
return SYSRESULT (ST_OK);
|
|
}
|
|
|
|
/* int mutex_lock (int mutex_rid) */
|
|
DEFINE_SYSCALL (sys_mutex_lock) {
|
|
int mutex_rid = (int)a1;
|
|
|
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
|
|
|
if (mutex_resource == NULL)
|
|
return SYSRESULT (-ST_NOT_FOUND);
|
|
|
|
proc_mutex_lock (proc, &mutex_resource->u.mutex);
|
|
|
|
return SYSRESULT (ST_OK);
|
|
}
|
|
|
|
/* int mutex_unlock (int mutex_rid) */
|
|
DEFINE_SYSCALL (sys_mutex_unlock) {
|
|
int mutex_rid = (int)a1;
|
|
|
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
|
|
|
if (mutex_resource == NULL)
|
|
return SYSRESULT (-ST_NOT_FOUND);
|
|
|
|
return SYSRESULT (proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK
|
|
: -ST_PERMISSION_ERROR);
|
|
}
|
|
|
|
static syscall_handler_func_t handler_table[] = {
|
|
[SYS_QUIT] = &sys_quit,
|
|
[SYS_TEST] = &sys_test,
|
|
[SYS_MAP] = &sys_map,
|
|
[SYS_UNMAP] = &sys_unmap,
|
|
[SYS_CLONE] = &sys_clone,
|
|
[SYS_SCHED] = &sys_sched,
|
|
[SYS_MUTEX_CREATE] = &sys_mutex_create,
|
|
[SYS_MUTEX_DELETE] = &sys_mutex_delete,
|
|
[SYS_MUTEX_LOCK] = &sys_mutex_lock,
|
|
[SYS_MUTEX_UNLOCK] = &sys_mutex_unlock,
|
|
};
|
|
|
|
syscall_handler_func_t syscall_find_handler (int syscall_num) {
|
|
if (!(syscall_num >= 0 &&
|
|
syscall_num < (int)(sizeof (handler_table) / sizeof (handler_table[0])))) {
|
|
return NULL;
|
|
}
|
|
|
|
return handler_table[syscall_num];
|
|
}
|