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

refactor: remove get_tls_* functions #874

Merged
merged 2 commits into from
Aug 31, 2023
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
38 changes: 0 additions & 38 deletions src/arch/aarch64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions src/arch/aarch64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -264,21 +264,20 @@ pub struct TaskTLS {

impl TaskTLS {
fn from_environment() -> Option<Box<Self>> {
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::<u8>();
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::<Box<[Dtv; 2]>>();

let layout = Layout::from_size_align(len, 16).unwrap();
Expand Down
38 changes: 0 additions & 38 deletions src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 12 additions & 16 deletions src/arch/x86_64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -235,33 +235,29 @@ 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<Box<Self>> {
// 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::<u8>();
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) }
};

// 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`.
Expand Down
3 changes: 1 addition & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down