syscall: enable fast syscall instruction

This commit is contained in:
Michael Shaw 2024-06-27 13:11:50 -04:00
commit d67c828c6f
6 changed files with 39 additions and 7 deletions

View file

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

View file

@ -1,11 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "lib/errno.h"
#include <kernel/lock.h>
#include <kernel/common.h>
#include <kernel/proc.h>
#include <kernel/slab.h>
#include <kernel/rbtree.h>
#include <kernel/syscall.h>
#include <kernel/msr.h>
#include <kernel/gdt.h>
#include <fs/vfs.h>
@ -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);
}