From 27d24f3e20aaa9956aa11f5605551252682f8c32 Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 29 Apr 2024 21:26:47 -0300 Subject: [PATCH] cpu: dont access cpu local storage using rdmsr --- kernel-src/arch/x86-64/syscall.asm | 3 ++- kernel-src/include/x86-64/arch/cpu.h | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index 9600115f..43d38dbe 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -130,7 +130,8 @@ arch_syscall_entry: ; because it's still using the user stack mov cr2, rax ; rax can be used just fine now - mov rax, [gs:0x0] ; thread pointer + mov rax, [gs:0x0] ; cpu pointer + mov rax, [rax] ; thread pointer mov rax, [rax] ; kernel stack top xchg rsp, rax ; switch stack pointers diff --git a/kernel-src/include/x86-64/arch/cpu.h b/kernel-src/include/x86-64/arch/cpu.h index 67bd22ff..8bd751ea 100644 --- a/kernel-src/include/x86-64/arch/cpu.h +++ b/kernel-src/include/x86-64/arch/cpu.h @@ -31,6 +31,7 @@ typedef struct cpu_t { void *schedulerstack; isr_t *isrqueue; dpc_t *dpcqueue; + struct cpu_t *self; } cpu_t; #define CPU_HALT() asm volatile("hlt") @@ -53,11 +54,14 @@ static inline uint16_t be_to_cpu_w(uint16_t w) { } static inline cpu_t *_cpu() { - return (cpu_t *)rdmsr(MSR_GSBASE); + volatile cpu_t *cpu; + asm volatile ("mov %%gs:0, %%rax" : "=a"(cpu)); + return cpu->self; } static inline void cpu_set(cpu_t *ptr) { - wrmsr(MSR_GSBASE, (uint64_t)ptr); + ptr->self = ptr; + wrmsr(MSR_GSBASE, (uint64_t)&ptr->self); } void cpu_initstate();