From c01b1d7eeee00124e067131a62cecb8948474585 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 15 Jul 2026 16:36:38 -0400 Subject: [PATCH] syscall: add basic syscall entry Signed-off-by: Michael Shaw --- amd64/Makefile | 3 ++- amd64/amd64_init.c | 18 ++++++++++++++- amd64/arch/Makefile | 1 + amd64/arch/entry.S | 48 +++++++++++++++++++++++++++++++++++++++ amd64/include/arch/msr.h | 2 ++ amd64/include/arch/proc.h | 31 +++++++++++++++++++++++++ amd64/syscall.c | 12 ++++++++++ amd64/trap.S | 23 +++++++++---------- include/kernel/syscall.h | 11 +++++++++ init/main.c | 5 +++- kernel/Makefile | 1 + kernel/syscall.c | 14 ++++++++++++ 12 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 amd64/arch/entry.S create mode 100644 amd64/include/arch/proc.h create mode 100644 amd64/syscall.c create mode 100644 include/kernel/syscall.h create mode 100644 kernel/syscall.c diff --git a/amd64/Makefile b/amd64/Makefile index 78b5ab6..4226440 100644 --- a/amd64/Makefile +++ b/amd64/Makefile @@ -4,7 +4,8 @@ obj-y += drivers/ obj-y += kernel/ obj-y += amd64_init.o +obj-y += exception.o obj-y += gdt.o obj-y += idt.o +obj-y += syscall.o obj-y += trap.o -obj-y += exception.o diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index c709233..d0bed2a 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -1,14 +1,20 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include #include +#include +#include +#include #include #include +#include "arch/msr.h" #include "arch/reg.h" #include "descriptor.h" static struct tty tty_early = {0}; static struct tty_serial_ops tty_early_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write}; +static struct cpu_local cpu_local_arr[128]; +static u64 cpu_ticker = 0; + void yield() { for (;;) { @@ -39,3 +45,13 @@ void arch_init() gdt_init(); idt_init(); } + +void arch_ap_startup() +{ + u64 id = atomic_xadd(&cpu_ticker, 1); + + struct cpu_local *this_local = &cpu_local_arr[id]; + wrmsr(IA32_GS_BASE, (u64)this_local); + + yield(); +} diff --git a/amd64/arch/Makefile b/amd64/arch/Makefile index 5e3e09b..4ff8b80 100644 --- a/amd64/arch/Makefile +++ b/amd64/arch/Makefile @@ -4,3 +4,4 @@ obj-y += page.o obj-y += reg.o obj-y += stack.o obj-y += cache.o +obj-y += entry.o diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S new file mode 100644 index 0000000..22bf409 --- /dev/null +++ b/amd64/arch/entry.S @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +.intel_syntax noprefix + +.global _syscall_enter +_syscall_enter: + swapgs + /* save user stack */ + mov gs:[16], rsp + /* load kernel stack */ + mov rsp, gs:[8] + + /* save registers */ + push r15 + push r14 + push r13 + push r12 + push r11 + push r10 + push r9 + push r8 + push rsi + push rdi + push rdx + push rbx + push rax + + call syscall + + /* restore registers */ + pop rax + pop rbx + pop rdx + pop rdi + pop rsi + pop r8 + pop r9 + pop r10 + pop r11 + pop r12 + pop r13 + pop r14 + pop r15 + + /* restore user stack */ + mov rsp, gs:[16] + + swapgs + sysretq diff --git a/amd64/include/arch/msr.h b/amd64/include/arch/msr.h index dfead50..e5844bb 100644 --- a/amd64/include/arch/msr.h +++ b/amd64/include/arch/msr.h @@ -4,6 +4,8 @@ #include +#define IA32_GS_BASE 0xC0000101 + u64 rdmsr(u32 msr); void wrmsr(u32 msr, u64 val); diff --git a/amd64/include/arch/proc.h b/amd64/include/arch/proc.h new file mode 100644 index 0000000..bd1ef86 --- /dev/null +++ b/amd64/include/arch/proc.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ARCH_PROC_H_ +#define _ARCH_PROC_H_ + +#include + +struct proc; +struct cpu_local { + struct proc *current_proc; + vaddr_t kernel_rsp; + vaddr_t user_rsp; + u32 cpu_id; +} PACKED; + +struct pt_regs { + u64 rax; + u64 rbx; + u64 rdx; + u64 rdi; + u64 rsi; + u64 r8; + u64 r9; + u64 r10; + u64 r11; + u64 r12; + u64 r13; + u64 r14; + u64 r15; +} PACKED; + +#endif /* _ARCH_PROC_H_ */ diff --git a/amd64/syscall.c b/amd64/syscall.c new file mode 100644 index 0000000..9926fdd --- /dev/null +++ b/amd64/syscall.c @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +i64 syscall(struct pt_regs *regs) +{ + syscall_t c = get_syscall(regs->rax); + if (!c) + return -ENOSYS; + + return c(regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9); +} diff --git a/amd64/trap.S b/amd64/trap.S index 72914b0..81f2e29 100644 --- a/amd64/trap.S +++ b/amd64/trap.S @@ -13,8 +13,7 @@ .global _exception_\nr _exception_\nr: mov rdi, \nr - popq rsi - call exception_handler + pop rsi iretq .endm @@ -34,20 +33,20 @@ exception 0x04 exception 0x05 exception 0x06 exception 0x07 -exception 0x08 +exception_err 0x08 exception 0x09 -exception 0x0A -exception 0x0B -exception 0x0C -exception 0x0D -exception 0x0E +exception_err 0x0A +exception_err 0x0B +exception_err 0x0C +exception_err 0x0D +exception_err 0x0E exception 0x0F exception 0x10 -exception 0x11 +exception_err 0x11 exception 0x12 exception 0x13 exception 0x14 -exception 0x15 +exception_err 0x15 exception 0x16 exception 0x17 exception 0x18 @@ -55,8 +54,8 @@ exception 0x19 exception 0x1A exception 0x1B exception 0x1C -exception 0x1D -exception 0x1E +exception_err 0x1D +exception_err 0x1E exception 0x1F irq 0x00 diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h new file mode 100644 index 0000000..0d03975 --- /dev/null +++ b/include/kernel/syscall.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _SYSCALL_H_ +#define _SYSCALL_H_ + +#include + +typedef ssize_t (*syscall_t)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6); + +syscall_t get_syscall(u64 no); + +#endif /* _SYSCALL_H_ */ diff --git a/init/main.c b/init/main.c index a9c39b1..bbd7c65 100644 --- a/init/main.c +++ b/init/main.c @@ -80,8 +80,11 @@ static void kmain() do_initcalls(); + void arch_ap_startup(); + arch_ap_startup(); + /* do nothing, for now :) */ - kernel_init_ready = true; + kernel_init_ready = true; yield(); } diff --git a/kernel/Makefile b/kernel/Makefile index 478d6f9..ee8b067 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += device.o obj-y += sg.o +obj-y += syscall.o obj-y += block/ obj-y += proc/ obj-y += tty/ diff --git a/kernel/syscall.c b/kernel/syscall.c new file mode 100644 index 0000000..da2df1d --- /dev/null +++ b/kernel/syscall.c @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +syscall_t syscalls[512] = {0}; + +syscall_t get_syscall(u64 no) +{ + if (no >= ARRAY_SIZE(syscalls)) + return NULL; + + return syscalls[no]; +}