From 5b164793cf3f72f13bb1ef803d5b32d7c289e1e4 Mon Sep 17 00:00:00 2001 From: Matheus Date: Thu, 2 Apr 2026 01:45:17 -0300 Subject: [PATCH] syscall: do not use cr2 as a scratch register --- kernel-src/arch/x86-64/syscall.asm | 10 +++------- kernel-src/include/x86-64/arch/cpu.h | 2 ++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index dc00958d..3bdf33f2 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -225,10 +225,7 @@ global arch_syscall_entry ; rax has return value arch_syscall_entry: swapgs - ; saving the syscall number on cr2 is cursed but we need this extra register - ; and taking a page fault here would result in a triple fault anyways - ; because it's still using the user stack - mov cr2, rax + mov [gs:40], rax ; save syscall number ; rax can be used just fine now mov rax, [gs:0] ; thread pointer mov rax, [rax] ; kernel stack top @@ -237,7 +234,7 @@ arch_syscall_entry: ; push context push qword 0x1b ; user SS push qword rax ; user RSP - mov rax, cr2 ; restore syscall number + mov rax, [gs:40] ; restore syscall number push r11 ; rflags is stored in r11 push qword 0x23 ; user CS push qword rcx ; save return RIP @@ -266,8 +263,7 @@ arch_syscall_entry: push rbx mov rbx, gs push rbx - mov rbx, cr2 - push rbx + sub rsp, 8 ; cr2 mov rbx, 0x10 mov es, rbx diff --git a/kernel-src/include/x86-64/arch/cpu.h b/kernel-src/include/x86-64/arch/cpu.h index 6f70b1a9..a5c5cb2a 100644 --- a/kernel-src/include/x86-64/arch/cpu.h +++ b/kernel-src/include/x86-64/arch/cpu.h @@ -28,6 +28,8 @@ typedef struct cpu_t { long hardware_id; // expected to be here by other code long internal_id; // expected to be here by other code + uint64_t scratch; // expected to be here by other code + timekeeper_source_t *timekeeper_source; timekeeper_source_info_t *timekeeper_source_info; time_t timekeeper_source_base_ticks;