Implement syscalls, hello world from userspace
This commit is contained in:
43
kernel/hal/x86_64/syscall.c
Normal file
43
kernel/hal/x86_64/syscall.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <stdint.h>
|
||||
#include "syscall.h"
|
||||
#include "cpu.h"
|
||||
#include "intr.h"
|
||||
#include "syscall/syscall.h"
|
||||
#include "proc/proc.h"
|
||||
#include "kprintf.h"
|
||||
#include "errors.h"
|
||||
|
||||
extern void hal_enable_syscallentry(void);
|
||||
extern void hal_syscallentry(void);
|
||||
|
||||
void hal_syscalldispatch(IntrStackFrame *frame) {
|
||||
uint64_t sysnum = frame->regs.rax;
|
||||
if (sysnum < SYSCALLS_MAX) {
|
||||
SyscallFn fn = SYSCALL_TABLE[sysnum];
|
||||
if (fn == NULL) {
|
||||
frame->regs.rax = E_BADSYSCALL;
|
||||
return;
|
||||
}
|
||||
int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx,
|
||||
frame->regs.r10, frame->regs.r8, frame->regs.r9);
|
||||
|
||||
if (sysnum == SYS_QUITPROC) {
|
||||
proc_sched((void *)frame);
|
||||
}
|
||||
frame->regs.rax = *(uint64_t *)&ret;
|
||||
}
|
||||
}
|
||||
|
||||
void hal_syscall_init(void) {
|
||||
hal_cpu_init(0);
|
||||
LocalCpuData *lcd = &HAL_CPUS[0];
|
||||
|
||||
hal_cpu_wrmsr(HAL_CPU_EFER, hal_cpu_rdmsr(HAL_CPU_EFER) | 0x1);
|
||||
hal_enable_syscallentry();
|
||||
|
||||
hal_cpu_wrmsr(HAL_CPU_GSBASE, (uint64_t)lcd);
|
||||
hal_cpu_wrmsr(HAL_CPU_KGSBASE, (uint64_t)lcd);
|
||||
hal_cpu_wrmsr(HAL_CPU_SFMASK, (uint64_t)0);
|
||||
|
||||
hal_cpu_wrmsr(HAL_CPU_LSTAR, (uint64_t)&hal_syscallentry);
|
||||
}
|
Reference in New Issue
Block a user