usr: implement user process switching
This is a rather large commit, so the gist of it is this: User context switching is now functional, after a change to the TSS, and a few minor details in the scheduler and loader. ELF parsing is rather primitive, but basically functional, as long as it only loads code. The interrupt 0x80 is for syscalls, and will switch from user mode to kernel mode. It is currently the only interrupt that can be called from user mode This adds the syscall functionality, which currently uses a red-black tree to store the function pointers, although when the table is more flushed out, it will probably be replaced with an array The pager now correctly marks the parent nodes with attributes as it allocates them. Without doing this, the page table entries will never be reached in lower privilege modes, causing page faults Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
d60da85470
commit
37518aea81
19 changed files with 262 additions and 69 deletions
50
sched/syscall.c
Normal file
50
sched/syscall.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/rbtree.h>
|
||||
|
||||
#define SYS_EXIT 60
|
||||
|
||||
static struct rbtree *syscall_tree;
|
||||
|
||||
typedef void (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
|
||||
|
||||
void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
|
||||
{
|
||||
struct rbnode *node = rbt_search(syscall_tree, syscall_no);
|
||||
if (node == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
syscall_t syscall = (void *)node->value;
|
||||
|
||||
syscall(arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
|
||||
void sys_exit(int status)
|
||||
{
|
||||
struct proc *proc = proc_find(getpid());
|
||||
if (proc == NULL)
|
||||
return;
|
||||
|
||||
proc->state = PROC_STOPPED;
|
||||
|
||||
proc_term(getpid());
|
||||
|
||||
kprintf("(syscall)Process %d exited with status 0x%x\n", getpid(), status);
|
||||
|
||||
proc_set_current(0);
|
||||
}
|
||||
|
||||
static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
|
||||
{
|
||||
struct rbnode *node = rbt_insert(syscall_tree, syscall_no);
|
||||
node->value = (uintptr_t)syscall;
|
||||
}
|
||||
|
||||
void syscall_init()
|
||||
{
|
||||
syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN);
|
||||
syscall_insert(SYS_EXIT, (syscall_t)sys_exit);
|
||||
}
|
||||
Loading…
Reference in a new issue