From 4bc47669aba2254d4f82c63987699f87ef8395bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Mon, 6 May 2024 12:27:24 +0200 Subject: [PATCH] fix(x86_64/CoreLocal): remove hardcoded field offsets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/x86_64/kernel/core_local.rs | 4 ++-- src/arch/x86_64/kernel/syscall.rs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_64/kernel/core_local.rs b/src/arch/x86_64/kernel/core_local.rs index 1ea9278ee7..d35316dfbc 100644 --- a/src/arch/x86_64/kernel/core_local.rs +++ b/src/arch/x86_64/kernel/core_local.rs @@ -2,8 +2,8 @@ use alloc::boxed::Box; use alloc::vec::Vec; use core::arch::asm; use core::cell::{Cell, RefCell, RefMut}; -use core::ptr; use core::sync::atomic::Ordering; +use core::{mem, ptr}; #[cfg(feature = "smp")] use hermit_sync::InterruptTicketMutex; @@ -81,7 +81,7 @@ impl CoreLocal { debug_assert_ne!(VirtAddr::zero(), GsBase::read()); unsafe { let raw: *const Self; - asm!("mov {}, gs:0", out(reg) raw, options(nomem, nostack, preserves_flags)); + asm!("mov {}, gs:{}", out(reg) raw, const mem::offset_of!(Self, this), options(nomem, nostack, preserves_flags)); &*raw } } diff --git a/src/arch/x86_64/kernel/syscall.rs b/src/arch/x86_64/kernel/syscall.rs index 2c99d0c27c..5283f0929a 100644 --- a/src/arch/x86_64/kernel/syscall.rs +++ b/src/arch/x86_64/kernel/syscall.rs @@ -1,5 +1,7 @@ use core::arch::asm; +use core::mem; +use super::core_local::CoreLocal; use crate::syscalls::table::SYSHANDLER_TABLE; #[no_mangle] @@ -19,7 +21,7 @@ pub(crate) unsafe extern "C" fn syscall_handler() -> ! { // switch to kernel stack "swapgs", "mov rcx, rsp", - "mov rsp, gs:32", + "mov rsp, gs:{core_local_kernel_stack}", // save user stack pointer "push rcx", // copy 4th argument to rcx to adhere x86_64 ABI @@ -42,6 +44,7 @@ pub(crate) unsafe extern "C" fn syscall_handler() -> ! { "pop rdx", "pop rcx", "sysretq", + core_local_kernel_stack = const mem::offset_of!(CoreLocal, kernel_stack), table = sym SYSHANDLER_TABLE, options(noreturn) );