Redesign userspace memory management
All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
#include <m/status.h>
|
||||
#include <m/syscall_defs.h>
|
||||
#include <mm/pmm.h>
|
||||
#include <proc/mem.h>
|
||||
#include <proc/mutex.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/procgroup.h>
|
||||
@@ -17,8 +16,11 @@
|
||||
#include <syscall/syscall.h>
|
||||
|
||||
#define DEFINE_SYSCALL(name) \
|
||||
int 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)
|
||||
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;
|
||||
@@ -43,46 +45,26 @@ static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t si
|
||||
/* int quit (void) */
|
||||
DEFINE_SYSCALL (sys_quit) {
|
||||
proc_kill (proc);
|
||||
return ST_OK;
|
||||
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 ST_OK;
|
||||
return SYSRESULT (ST_OK);
|
||||
}
|
||||
|
||||
/* int map (int mem_rid, uintptr_t vaddr, uint32_t flags) */
|
||||
/* int map (uintptr_t vaddr, size_t pages, uint32_t flags) */
|
||||
DEFINE_SYSCALL (sys_map) {
|
||||
spin_lock_ctx_t ctxrs;
|
||||
|
||||
int mem_rid = (int)a1;
|
||||
uintptr_t vaddr = a2;
|
||||
uintptr_t vaddr = a1;
|
||||
size_t pages = (size_t)a2;
|
||||
uint32_t flags = (uint32_t)a3;
|
||||
|
||||
if (vaddr % PAGE_SIZE != 0)
|
||||
return -ST_UNALIGNED;
|
||||
return SYSRESULT (-ST_UNALIGNED);
|
||||
|
||||
struct proc_resource* mem_resource = proc_find_resource (proc->procgroup, mem_rid);
|
||||
|
||||
if (mem_resource == NULL) {
|
||||
return -ST_NOT_FOUND;
|
||||
}
|
||||
|
||||
spin_lock (&mem_resource->lock, &ctxrs);
|
||||
|
||||
if (mem_resource->type != PR_MEM) {
|
||||
spin_unlock (&mem_resource->lock, &ctxrs);
|
||||
return -ST_BAD_RESOURCE;
|
||||
}
|
||||
|
||||
uintptr_t paddr = mem_resource->u.mem.paddr;
|
||||
size_t pages = mem_resource->u.mem.pages;
|
||||
|
||||
spin_unlock (&mem_resource->lock, &ctxrs);
|
||||
|
||||
return proc_map (proc, paddr, vaddr, pages, flags) ? ST_OK : -ST_OOM_ERROR;
|
||||
return SYSRESULT (procgroup_map (proc->procgroup, vaddr, pages, flags, NULL));
|
||||
}
|
||||
|
||||
/* int unmap (uintptr_t vaddr, size_t pages) */
|
||||
@@ -91,59 +73,9 @@ DEFINE_SYSCALL (sys_unmap) {
|
||||
size_t pages = (size_t)a2;
|
||||
|
||||
if (vaddr % PAGE_SIZE != 0)
|
||||
return -ST_UNALIGNED;
|
||||
return SYSRESULT (-ST_UNALIGNED);
|
||||
|
||||
return proc_unmap (proc, vaddr, pages) ? ST_OK : -ST_OOM_ERROR;
|
||||
}
|
||||
|
||||
/* int create_mem (int rid, size_t pages) */
|
||||
DEFINE_SYSCALL (sys_create_mem) {
|
||||
int rid = (int)a1;
|
||||
size_t pages = (size_t)a2;
|
||||
|
||||
if (pages == 0)
|
||||
return ST_OK;
|
||||
|
||||
/* int rid1 = rid < 0 ? atomic_fetch_add (&proc->resources->sys_rids, 1) : rid; */
|
||||
int rid1 = rid < 0 ? procgroup_get_sys_rid (proc->procgroup) : rid;
|
||||
struct proc_resource* mem_resource =
|
||||
proc_create_resource_mem (proc->procgroup, rid1, pages, 0, false);
|
||||
|
||||
if (mem_resource == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
return mem_resource->rid;
|
||||
}
|
||||
|
||||
/* int unlink_mem (int rid, size_t pages) */
|
||||
DEFINE_SYSCALL (sys_unlink_mem) {
|
||||
/* spin_lock_ctx_t ctxrs; */
|
||||
|
||||
/* int rid = (int)a1; */
|
||||
/* size_t pages = (size_t)a2; */
|
||||
|
||||
/* if (!(vis == RV_PUBLIC || vis == RV_PRIVATE)) */
|
||||
/* return -ST_BAD_RESOURCE; */
|
||||
|
||||
/* struct proc_resource* mem_resource = proc_find_resource (proc, rid, vis); */
|
||||
|
||||
/* if (mem_resource == NULL) */
|
||||
/* return -ST_NOT_FOUND; */
|
||||
|
||||
/* spin_lock (&mem_resource->lock, &ctxrs); */
|
||||
|
||||
/* if (mem_resource->type != PR_MEM) { */
|
||||
/* spin_unlock (&mem_resource->lock, &ctxrs); */
|
||||
/* return -ST_BAD_RESOURCE; */
|
||||
/* } */
|
||||
|
||||
/* mem_resource->u.mem.alive_pages -= pages; */
|
||||
/* if (mem_resource->u.mem.alive_pages < 0) { */
|
||||
/* spin_unlock (&mem_resource->lock, &ctxrs); */
|
||||
/* proc_drop_resource (proc, mem_resource, true); */
|
||||
/* } */
|
||||
|
||||
return ST_OK;
|
||||
return SYSRESULT (procgroup_unmap (proc->procgroup, vaddr, pages));
|
||||
}
|
||||
|
||||
/* int clone (uintptr_t vstack_top, size_t stack_size, void* entry) */
|
||||
@@ -157,20 +89,20 @@ DEFINE_SYSCALL (sys_clone) {
|
||||
DEBUG ("new=%p\n", new);
|
||||
|
||||
if (new == NULL) {
|
||||
return -ST_OOM_ERROR;
|
||||
return SYSRESULT (-ST_OOM_ERROR);
|
||||
}
|
||||
|
||||
int pid = new->pid;
|
||||
|
||||
proc_register (new, NULL);
|
||||
|
||||
return pid;
|
||||
return SYSRESULT (pid);
|
||||
}
|
||||
|
||||
/* int sched (void) */
|
||||
DEFINE_SYSCALL (sys_sched) {
|
||||
proc_sched ();
|
||||
return ST_OK;
|
||||
return SYSRESULT (ST_OK);
|
||||
}
|
||||
|
||||
/* int create_mutex (int mutex_rid) */
|
||||
@@ -180,9 +112,9 @@ DEFINE_SYSCALL (sys_create_mutex) {
|
||||
struct proc_resource* mutex_resource = proc_create_resource_mutex (proc->procgroup, mutex_rid);
|
||||
|
||||
if (mutex_resource == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
return SYSRESULT (-ST_OOM_ERROR);
|
||||
|
||||
return mutex_resource->rid;
|
||||
return SYSRESULT (mutex_resource->rid);
|
||||
}
|
||||
|
||||
/* int unlink_mutex (int mutex_rid) */
|
||||
@@ -192,11 +124,9 @@ DEFINE_SYSCALL (sys_unlink_mutex) {
|
||||
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||
|
||||
if (mutex_resource == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
return SYSRESULT (-ST_NOT_FOUND);
|
||||
|
||||
/* proc_drop_resource (proc, mutex_resource, true); */
|
||||
|
||||
return ST_OK;
|
||||
return SYSRESULT (ST_OK);
|
||||
}
|
||||
|
||||
/* int lock_mutex (int mutex_rid) */
|
||||
@@ -206,11 +136,11 @@ DEFINE_SYSCALL (sys_lock_mutex) {
|
||||
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||
|
||||
if (mutex_resource == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
return SYSRESULT (-ST_NOT_FOUND);
|
||||
|
||||
proc_mutex_lock (proc, &mutex_resource->u.mutex);
|
||||
|
||||
return ST_OK;
|
||||
return SYSRESULT (ST_OK);
|
||||
}
|
||||
|
||||
/* int unlock_mutex (int mutex_rid) */
|
||||
@@ -220,9 +150,10 @@ DEFINE_SYSCALL (sys_unlock_mutex) {
|
||||
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||
|
||||
if (mutex_resource == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
return SYSRESULT (-ST_NOT_FOUND);
|
||||
|
||||
return proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK : -ST_PERMISSION_ERROR;
|
||||
return SYSRESULT (proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK
|
||||
: -ST_PERMISSION_ERROR);
|
||||
}
|
||||
|
||||
static syscall_handler_func_t handler_table[] = {
|
||||
@@ -232,8 +163,6 @@ static syscall_handler_func_t handler_table[] = {
|
||||
[SYS_UNMAP] = &sys_unmap,
|
||||
[SYS_CLONE] = &sys_clone,
|
||||
[SYS_SCHED] = &sys_sched,
|
||||
[SYS_CREATE_MEM] = &sys_create_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,
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
|
||||
typedef int (*syscall_handler_func_t) (struct proc* proc, void* regs, uintptr_t a1, uintptr_t a2,
|
||||
uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6);
|
||||
typedef uintptr_t (*syscall_handler_func_t) (struct proc* proc, void* regs, uintptr_t a1,
|
||||
uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5,
|
||||
uintptr_t a6);
|
||||
|
||||
syscall_handler_func_t syscall_find_handler (int syscall_num);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user