From d67c828c6fab29b3c2318d0a7deae30ff72ee795 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 27 Jun 2024 13:11:50 -0400 Subject: [PATCH] syscall: enable fast syscall instruction --- include/kernel/gdt.h | 4 ++-- include/kernel/msr.h | 4 ++++ init/gdt.c | 4 ++-- init/irq.c | 4 ++-- sched/entry.S | 14 ++++++++++++++ sched/syscall.c | 16 +++++++++++++++- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/include/kernel/gdt.h b/include/kernel/gdt.h index 9591fc0..9bdbace 100644 --- a/include/kernel/gdt.h +++ b/include/kernel/gdt.h @@ -14,8 +14,8 @@ #define GDT_SEGMENT_CODE_RING0 0x28 #define GDT_SEGMENT_DATA_RING0 0x30 -#define GDT_SEGMENT_CODE_RING3 0x38 -#define GDT_SEGMENT_DATA_RING3 0x40 +#define GDT_SEGMENT_DATA_RING3 0x38 +#define GDT_SEGMENT_CODE_RING3 0x40 #ifndef __ASM__ diff --git a/include/kernel/msr.h b/include/kernel/msr.h index e13d76e..5086e83 100644 --- a/include/kernel/msr.h +++ b/include/kernel/msr.h @@ -6,6 +6,10 @@ #define MSR_APIC_BASE 0x1B +#define MSR_IA32_EFER 0xC0000080 +#define MSR_IA32_STAR 0xC0000081 +#define MSR_IA32_LSTAR 0xC0000082 + #ifndef __ASM__ uint64_t rdmsr(uint32_t msr); diff --git a/init/gdt.c b/init/gdt.c index 23af7bc..c914768 100644 --- a/init/gdt.c +++ b/init/gdt.c @@ -60,8 +60,8 @@ void gdt_init() gdt_insert(4, GDT_ACCESS_DATA_RING0, 0xC0); gdt_insert(5, GDT_ACCESS_CODE_RING0, 0xA0); gdt_insert(6, GDT_ACCESS_DATA_RING0, 0xC0); - gdt_insert(7, GDT_ACCESS_CODE_RING3, 0xA0); - gdt_insert(8, GDT_ACCESS_DATA_RING3, 0xC0); + gdt_insert(7, GDT_ACCESS_DATA_RING3, 0xC0); + gdt_insert(8, GDT_ACCESS_CODE_RING3, 0xA0); __gdtr.size = (sizeof(__gdt) - 1); __gdtr.offset = (uint64_t)__gdt; diff --git a/init/irq.c b/init/irq.c index 5989c0c..70a6327 100644 --- a/init/irq.c +++ b/init/irq.c @@ -15,7 +15,7 @@ static bool irq_mapped[16] = { false }; static void *irq_handlers[16] = { NULL }; -void gate_syscall(); +void gate_syscall_int80(); exception_decl(0x00); exception_decl(0x01); @@ -170,7 +170,7 @@ void idt_init() idt_insert(0x2E, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0E)); idt_insert(0x2F, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0F)); - idt_insert(0x80, GATE_INTR_DPL3, 0, GDT_SEGMENT_CODE_RING0, gate_syscall); + idt_insert(0x80, GATE_INTR_DPL3, 0, GDT_SEGMENT_CODE_RING0, gate_syscall_int80); irq_map(4, serial_trap); diff --git a/sched/entry.S b/sched/entry.S index 7b17ff8..9442476 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -199,6 +199,20 @@ irq 0x0F .global gate_syscall gate_syscall: + cli + pushq %r11 + pushq %rcx + movq %r10, %rcx + movq %rax, %r9 + + call syscall + + popq %rcx + popq %r11 + sysretq + +.global gate_syscall_int80 +gate_syscall_int80: cli movq %r10, %rcx movq %rax, %r9 diff --git a/sched/syscall.c b/sched/syscall.c index eac7bf6..685bf2b 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -1,11 +1,12 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include "lib/errno.h" #include #include #include #include #include #include +#include +#include #include @@ -16,6 +17,8 @@ typedef void *(*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t syscall_t syscall_table[SYSCALL_MAX]; slab_t *fd_slab; +void gate_syscall(); + uint64_t syscall(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t syscall_no) { syscall_t syscall = (void *)syscall_table[syscall_no]; @@ -178,4 +181,15 @@ void syscall_init() syscall_insert(SYS_OPEN, (syscall_t)sys_open); syscall_insert(SYS_FORK, (syscall_t)sys_fork); syscall_insert(SYS_EXIT, (syscall_t)sys_exit); + + /* init syscall instruction */ + uint64_t star = rdmsr(MSR_IA32_STAR); + star |= ((uint64_t)GDT_SEGMENT_CODE_RING0 << 32); + star |= ((uint64_t)(GDT_SEGMENT_CODE_RING3 - 16) << 48); + wrmsr(MSR_IA32_STAR, star); + + wrmsr(MSR_IA32_LSTAR, (uint64_t)gate_syscall); + + uint64_t efer = rdmsr(MSR_IA32_EFER); + wrmsr(MSR_IA32_EFER, efer | 0x1); }