diff --git a/src/arch/aarch64/kernel/mod.rs b/src/arch/aarch64/kernel/mod.rs index 2de414e95b..9c1cbd8405 100644 --- a/src/arch/aarch64/kernel/mod.rs +++ b/src/arch/aarch64/kernel/mod.rs @@ -75,44 +75,6 @@ pub fn get_limit() -> usize { boot_info().hardware_info.phys_addr_range.end as usize } -pub fn get_tls_start() -> VirtAddr { - VirtAddr( - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.start) - .unwrap_or_default(), - ) -} - -pub fn get_tls_filesz() -> usize { - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.filesz) - .unwrap_or_default() as usize -} - -pub fn get_tls_memsz() -> usize { - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.memsz) - .unwrap_or_default() as usize -} - -pub fn get_tls_align() -> usize { - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.align) - .unwrap_or_default() as usize -} - #[cfg(feature = "smp")] pub fn get_possible_cpus() -> u32 { CPU_ONLINE.load(Ordering::Acquire) diff --git a/src/arch/aarch64/kernel/scheduler.rs b/src/arch/aarch64/kernel/scheduler.rs index 1517e23a5e..96e4d1f8aa 100644 --- a/src/arch/aarch64/kernel/scheduler.rs +++ b/src/arch/aarch64/kernel/scheduler.rs @@ -13,7 +13,7 @@ use crate::arch::aarch64::kernel::CURRENT_STACK_ADDRESS; use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags}; use crate::arch::aarch64::mm::{PhysAddr, VirtAddr}; use crate::scheduler::task::{Task, TaskFrame}; -use crate::{env, DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE}; +use crate::{kernel, DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE}; #[derive(Debug)] #[repr(C, packed)] @@ -264,21 +264,20 @@ pub struct TaskTLS { impl TaskTLS { fn from_environment() -> Option> { - if env::get_tls_memsz() == 0 { - return None; - } + let tls_info = kernel::boot_info().load_info.tls_info?; + assert_ne!(tls_info.memsz, 0); // Get TLS initialization image let tls_init_image = { - let tls_init_data = env::get_tls_start().as_ptr::(); - let tls_init_len = env::get_tls_filesz(); + let tls_init_data = ptr::from_exposed_addr(tls_info.start.try_into().unwrap()); + let tls_init_len = tls_info.filesz.try_into().unwrap(); // SAFETY: We will have to trust the environment here. unsafe { slice::from_raw_parts(tls_init_data, tls_init_len) } }; - let off = core::cmp::max(16, env::get_tls_align()) - 16; - let block_len = env::get_tls_memsz() + off; + let off = core::cmp::max(16, usize::try_from(tls_info.align).unwrap()) - 16; + let block_len = usize::try_from(tls_info.memsz).unwrap() + off; let len = block_len + mem::size_of::>(); let layout = Layout::from_size_align(len, 16).unwrap(); diff --git a/src/arch/x86_64/kernel/mod.rs b/src/arch/x86_64/kernel/mod.rs index a8afad3ec3..c231ed91c5 100644 --- a/src/arch/x86_64/kernel/mod.rs +++ b/src/arch/x86_64/kernel/mod.rs @@ -67,44 +67,6 @@ pub fn get_limit() -> usize { boot_info().hardware_info.phys_addr_range.end as usize } -pub fn get_tls_start() -> VirtAddr { - VirtAddr( - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.start) - .unwrap_or_default(), - ) -} - -pub fn get_tls_filesz() -> usize { - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.filesz) - .unwrap_or_default() as usize -} - -pub fn get_tls_memsz() -> usize { - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.memsz) - .unwrap_or_default() as usize -} - -pub fn get_tls_align() -> usize { - boot_info() - .load_info - .tls_info - .as_ref() - .map(|tls_info| tls_info.align) - .unwrap_or_default() as usize -} - pub fn get_mbinfo() -> VirtAddr { match boot_info().platform_info { PlatformInfo::Multiboot { diff --git a/src/arch/x86_64/kernel/scheduler.rs b/src/arch/x86_64/kernel/scheduler.rs index 75b6835b5e..41d11e3954 100644 --- a/src/arch/x86_64/kernel/scheduler.rs +++ b/src/arch/x86_64/kernel/scheduler.rs @@ -14,7 +14,7 @@ use crate::arch::x86_64::mm::paging::{ }; use crate::arch::x86_64::mm::{PhysAddr, VirtAddr}; use crate::config::*; -use crate::env; +use crate::kernel; use crate::scheduler::task::{Task, TaskFrame}; #[repr(C, packed)] @@ -235,22 +235,18 @@ pub struct TaskTLS { } impl TaskTLS { + // For details on thread-local storage data structures see + // + // “ELF Handling For Thread-Local Storage” Section 3.4.6: x86-64 Specific Definitions for Run-Time Handling of TLS + // https://akkadia.org/drepper/tls.pdf fn from_environment() -> Option> { - // For details on thread-local storage data structures see - // - // “ELF Handling For Thread-Local Storage” Section 3.4.6: x86-64 Specific Definitions for Run-Time Handling of TLS - // https://akkadia.org/drepper/tls.pdf - - let tls_len = env::get_tls_memsz(); - - if env::get_tls_memsz() == 0 { - return None; - } + let tls_info = kernel::boot_info().load_info.tls_info?; + assert_ne!(tls_info.memsz, 0); // Get TLS initialization image let tls_init_image = { - let tls_init_data = env::get_tls_start().as_ptr::(); - let tls_init_len = env::get_tls_filesz(); + let tls_init_data = ptr::from_exposed_addr(tls_info.start.try_into().unwrap()); + let tls_init_len = tls_info.filesz.try_into().unwrap(); // SAFETY: We will have to trust the environment here. unsafe { slice::from_raw_parts(tls_init_data, tls_init_len) } @@ -258,10 +254,10 @@ impl TaskTLS { // Allocate TLS block let mut block = { - let tls_align = env::get_tls_align(); - // As described in “ELF Handling For Thread-Local Storage” - let tls_offset = tls_len.align_up(tls_align); + let tls_offset = usize::try_from(tls_info.memsz) + .unwrap() + .align_up(usize::try_from(tls_info.align).unwrap()); // 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`. diff --git a/src/env.rs b/src/env.rs index 5a1e4efd23..0582b90d11 100644 --- a/src/env.rs +++ b/src/env.rs @@ -11,8 +11,7 @@ use hermit_entry::boot_info::PlatformInfo; use hermit_sync::OnceCell; pub use crate::arch::kernel::{ - get_base_address, get_cmdline, get_cmdsize, get_image_size, get_ram_address, get_tls_align, - get_tls_filesz, get_tls_memsz, get_tls_start, + get_base_address, get_cmdline, get_cmdsize, get_image_size, get_ram_address, }; use crate::kernel::boot_info; diff --git a/src/lib.rs b/src/lib.rs index 23fd6ecb4b..29cb0edf2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -314,11 +314,7 @@ fn boot_processor_main() -> ! { info!("BSS starts at {:p}", unsafe { core::ptr::addr_of_mut!(__bss_start) }); - info!( - "TLS starts at {:p} (size {} Bytes)", - env::get_tls_start(), - env::get_tls_memsz() - ); + info!("tls_info = {:#x?}", kernel::boot_info().load_info.tls_info); arch::boot_processor_init(); scheduler::add_current_core();