Skip to content

Commit

Permalink
Merge pull request #1172 from hermit-os/core_local-repr
Browse files Browse the repository at this point in the history
fix(x86_64): make `CoreLocal` `#[repr(Rust)]` again
  • Loading branch information
mkroening committed May 7, 2024
2 parents 6e9de65 + 3098c51 commit 95694b4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
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

0 comments on commit 95694b4

Please sign in to comment.