From 2be2236e99e5aed779b820b540d72dffb47da406 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 22 Jul 2024 20:49:58 +0200 Subject: kernel: explicitly store process RIP and RFLAGS Once again, needed for the interrupt return syscall. Seems to have made the kernel slower in timebench? --- src/kernel/arch/amd64/registers.h | 2 +- src/kernel/arch/amd64/sysenter.s | 46 ++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 21 deletions(-) (limited to 'src/kernel/arch') diff --git a/src/kernel/arch/amd64/registers.h b/src/kernel/arch/amd64/registers.h index 5fd09c9..cfbfa54 100644 --- a/src/kernel/arch/amd64/registers.h +++ b/src/kernel/arch/amd64/registers.h @@ -7,7 +7,7 @@ struct CpuRegs { uint64_t r15, r14, r13, r12, r11, r10, r9, r8; uint64_t rdi, rsi; userptr_t rbp, rsp; - uint64_t rbx, rdx, rcx, rax; + uint64_t rbx, rdx, rcx, rax, rip, flags; uint8_t _sse[512]; } __attribute__((__packed__)); diff --git a/src/kernel/arch/amd64/sysenter.s b/src/kernel/arch/amd64/sysenter.s index 8d91c4a..8e07dbb 100644 --- a/src/kernel/arch/amd64/sysenter.s +++ b/src/kernel/arch/amd64/sysenter.s @@ -41,13 +41,19 @@ stored_rsp: pagedir_current: .skip 8 -// temporarily used for IRET +/* fake stack used for IRET */ +.align 8 +iret_stack: +iret_ip: .skip 8 +iret_cs: + .quad SEG_r3code << 3 | 3 +iret_flags: .skip 8 +iret_sp: .skip 8 - .skip 8 - .skip 8 -dumb_stack: +iret_ss: + .quad SEG_r3data << 3 | 3 .global _sysexit_real .type _sysexit_real, @function @@ -58,12 +64,6 @@ _sysexit_real: mov %ax, %fs mov %ax, %gs - /* The state image referenced with an FXRSTOR instruction must have - * been saved using an FXSAVE instruction or be in the same format - * as required [...] will result in an incorrect state restoration. */ - // TODO will probably end up fucking something up in a hard to debug way - // sorry, future me. hopefully you have learned something from this - fxrstor (_sysexit_regs + 128) mov $_sysexit_regs, %rsp pop %r15 pop %r14 @@ -78,23 +78,25 @@ _sysexit_real: pop %rdi pop %rsi pop %rbp - pop (stored_rsp) + pop (iret_sp) pop %rbx pop %rdx pop %rcx pop %rax - // enable paging + pop (iret_ip) + pop (iret_flags) + + /* The state image referenced with an FXRSTOR instruction must have + * been saved using an FXSAVE instruction or be in the same format + * as required [...] will result in an incorrect state restoration. */ + fxrstor (%rsp) + mov (pagedir_current), %rsp mov %rsp, %cr3 - mov $dumb_stack, %rsp - push $(SEG_r3data << 3 | 3) /* SS */ - push (stored_rsp) /* SP */ - push %r11 /* FLAGS */ - push $(SEG_r3code << 3 | 3) /* CS */ - push %rcx /* IP */ + mov $iret_stack, %rsp iretq sysenter_stage1: @@ -105,8 +107,12 @@ sysenter_stage1: mov $pml4_identity, %rsp mov %rsp, %cr3 - fxsave (_sysexit_regs + 128) - mov $(_sysexit_regs + 128), %rsp + mov $(_sysexit_regs + 144), %rsp + fxsave (%rsp) + + push %r11 /* flags */ + push %rcx /* ip */ + push %rax push %rcx push %rdx -- cgit v1.2.3