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

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

View file

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

View file

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

View file

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

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);
}