syscall: add basic syscall entry

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-15 16:36:38 -04:00
commit c01b1d7eee
12 changed files with 154 additions and 15 deletions

View file

@ -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

View file

@ -1,14 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <drivers/acpi.h>
#include <kernel/tty.h>
#include <kernel/atomic.h>
#include <arch/proc.h>
#include <drivers/acpi.h>
#include <drivers/serial.h>
#include <lib/stdio.h>
#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();
}

View file

@ -4,3 +4,4 @@ obj-y += page.o
obj-y += reg.o
obj-y += stack.o
obj-y += cache.o
obj-y += entry.o

48
amd64/arch/entry.S Normal file
View file

@ -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

View file

@ -4,6 +4,8 @@
#include <kernel/types.h>
#define IA32_GS_BASE 0xC0000101
u64 rdmsr(u32 msr);
void wrmsr(u32 msr, u64 val);

31
amd64/include/arch/proc.h Normal file
View file

@ -0,0 +1,31 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ARCH_PROC_H_
#define _ARCH_PROC_H_
#include <kernel/proc.h>
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_ */

12
amd64/syscall.c Normal file
View file

@ -0,0 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/syscall.h>
#include <arch/proc.h>
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);
}

View file

@ -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