Files
mop3/kernel/amd64/syscall.c
kamkow1 e5ebd7f3ba
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 2m21s
Build documentation / build-and-deploy (push) Successful in 54s
Use a big-lock for kernel sychronization instead of fine-grained locking
2026-04-27 18:06:02 +02:00

94 lines
2.0 KiB
C

#include <amd64/apic.h>
#include <amd64/fx.h>
#include <amd64/gdt.h>
#include <amd64/intr.h>
#include <amd64/mm.h>
#include <amd64/msr-index.h>
#include <amd64/msr.h>
#include <libk/lengthof.h>
#include <libk/list.h>
#include <libk/string.h>
#include <mm/malloc.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <status.h>
#include <sync/biglock.h>
#include <sys/debug.h>
#include <sys/intr.h>
#include <sys/smp.h>
#include <syscall/syscall.h>
#include <syscall_defs.h>
extern void syscall_entry(void);
static uintptr_t syscall_dispatch1(void* stack_ptr) {
struct saved_regs* regs = stack_ptr;
struct proc* caller = thiscpu->proc_current;
int caller_pid = caller->pid;
memcpy(&caller->pdata.regs, regs, sizeof(struct saved_regs));
fx_save(caller->pdata.fx_env);
int syscall_num = regs->rax;
syscall_handler_func_t func = syscall_find_handler(syscall_num);
if (func == NULL) {
return -ST_SYSCALL_NOT_FOUND;
}
struct reschedule_ctx rctx;
memset(&rctx, 0, sizeof(rctx));
lapic_timer_mask();
intr_enable();
uintptr_t r =
func(caller, regs, &rctx, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
intr_disable();
lapic_timer_unmask();
caller = proc_find_pid(caller_pid);
if (caller != NULL) {
caller->pdata.regs.rax = r;
}
bool do_thiscpu = false;
for (size_t i = 0; i < lengthof(rctx.cpus); i++) {
if (rctx.cpus[i] != NULL && rctx.cpus[i] != thiscpu)
cpu_request_sched(rctx.cpus[i], true);
else
do_thiscpu = true;
}
if (do_thiscpu)
cpu_request_sched(thiscpu, true);
return r;
}
uintptr_t syscall_dispatch(void* stack_ptr) {
load_kernel_cr3();
biglock_lock();
uintptr_t r = syscall_dispatch1(stack_ptr);
biglock_unlock();
return r;
}
void syscall_init(void) {
wrmsr(MSR_STAR, ((uint64_t)GDT_KCODE << 32) | ((uint64_t)(GDT_KDATA | 0x03) << 48));
wrmsr(MSR_LSTAR, (uint64_t)&syscall_entry);
wrmsr(MSR_SYSCALL_MASK, (1ULL << 9));
wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_SCE);
}