Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(x86_64): make CoreLocal #[repr(Rust)] again #1172

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/arch/x86_64/kernel/core_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +18,6 @@ use crate::executor::task::AsyncTask;
use crate::scheduler::SchedulerInput;
use crate::scheduler::{CoreId, PerCoreScheduler};

#[repr(C)]
pub(crate) struct CoreLocal {
this: *const Self,
/// Sequential ID of this CPU Core.
Expand Down Expand Up @@ -81,7 +80,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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ where
if tls_size > 0 {
// To access TLS blocks on x86-64, TLS offsets are *subtracted* from the thread register value.
// So the thread pointer needs to be `block_ptr + tls_offset`.
// GNU style TLS requires `gs:0` to represent the same address as the thread pointer.
// GNU style TLS requires `fs:0` to represent the same address as the thread pointer.
// Since the thread pointer points to the end of the TLS blocks, we need to store it there.
let tcb_size = core::mem::size_of::<*mut ()>();
let tls_offset = tls_size as usize;
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl TaskTLS {
let mut block = {
// To access TLS blocks on x86-64, TLS offsets are *subtracted* from the thread register value.
// So the thread pointer needs to be `block_ptr + tls_offset`.
// GNU style TLS requires `gs:0` to represent the same address as the thread pointer.
// GNU style TLS requires `fs:0` to represent the same address as the thread pointer.
// Since the thread pointer points to the end of the TLS blocks, we need to store it there.
let tcb_size = mem::size_of::<*mut ()>();

Expand Down
5 changes: 4 additions & 1 deletion src/arch/x86_64/kernel/syscall.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::arch::asm;
use core::mem;

use super::core_local::CoreLocal;
use crate::syscalls::table::SYSHANDLER_TABLE;

#[no_mangle]
Expand All @@ -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
Expand All @@ -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)
);
Expand Down