From ae9c1d1b16061242068353ff9064c8c9ef63b577 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 24 Jul 2021 18:53:34 +0200 Subject: sysexit() now overrides all registers when doing anything more complex than starting a new program, you pretty much need to pass a full register dump. otherwise stuff will break --- src/kernel/arch/generic.h | 2 +- src/kernel/arch/i386/registers.h | 6 ++++++ src/kernel/arch/i386/sysenter.c | 21 +++++++++++++++++++++ src/kernel/arch/i386/sysenter.s | 12 ++++++------ 4 files changed, 34 insertions(+), 7 deletions(-) (limited to 'src/kernel/arch') diff --git a/src/kernel/arch/generic.h b/src/kernel/arch/generic.h index f208aa3..d5f8b3c 100644 --- a/src/kernel/arch/generic.h +++ b/src/kernel/arch/generic.h @@ -16,7 +16,7 @@ __attribute__((noreturn)) void halt_cpu(); // src/arch/i386/sysenter.s -void sysexit(void (*fun)(), void *stack_top); +void sysexit(struct registers); int syscall_handler(int, int, int, int); // all of those can allocate memory diff --git a/src/kernel/arch/i386/registers.h b/src/kernel/arch/i386/registers.h index feba157..1264139 100644 --- a/src/kernel/arch/i386/registers.h +++ b/src/kernel/arch/i386/registers.h @@ -1,11 +1,17 @@ #pragma once #include +// TODO merge registers and registers_pushad + struct registers { uint32_t eax, ebx, ecx, edx, esi, edi; void *ebp, *esp, *eip; }; +struct registers_pushad { + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; +} __attribute__((__packed__)); + // saves a return value according to the SysV ABI inline void regs_savereturn(struct registers *regs, uint64_t value) { regs->eax = value; diff --git a/src/kernel/arch/i386/sysenter.c b/src/kernel/arch/i386/sysenter.c index 91dac2e..f3f5987 100644 --- a/src/kernel/arch/i386/sysenter.c +++ b/src/kernel/arch/i386/sysenter.c @@ -1,6 +1,27 @@ #include #include +struct registers_pushad _sysexit_regs; // a hack + +extern void _sysexit_real(); + +void sysexit(struct registers regs) { + _sysexit_regs = (struct registers_pushad) { + .eax = regs.eax, + .ebx = regs.ebx, + .ebp = regs.ebp, + .esi = regs.esi, + .edi = regs.edi, + + // sysexit args + .ecx = regs.esp, + .edx = regs.eip, + + // ESP doesn't matter + }; + _sysexit_real(); +} + void sysenter_stage2(int edi, int esi, void *ebp, void *esp, int ebx, int edx, int ecx, int eax) { diff --git a/src/kernel/arch/i386/sysenter.s b/src/kernel/arch/i386/sysenter.s index a982dd8..24cbc89 100644 --- a/src/kernel/arch/i386/sysenter.s +++ b/src/kernel/arch/i386/sysenter.s @@ -8,12 +8,9 @@ .set IA32_SYSENTER_EIP, 0x176 .section .text -.global sysexit -.type sysexit, @function -sysexit: - mov 4(%esp), %edx - mov 8(%esp), %ecx - +.global _sysexit_real +.type _sysexit_real, @function +_sysexit_real: mov $(SEG_r3data << 3 | 3), %ax mov %ax, %ds mov %ax, %es @@ -25,6 +22,9 @@ sysexit: or $0x80000000, %eax mov %eax, %cr0 + // restore the registers + mov $_sysexit_regs, %esp + popal // probably a bad idea sysexit -- cgit v1.2.3