-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thread-local storage is commonly needed in all sorts of places, from storing a harts `TrapFrame`, ID or other state that should be statically available but still per-hart. Instead of coming up with something homegrown here we use Rusts/LLVMs [`thread_local` attribute](rust-lang/rust#29594). This requires us to do a couple things: 1. allocate one or more frames for TLS 2. (identity) map the frames 3. *on each hart* set the `tp` register to point to the TLS base
- Loading branch information
1 parent
b6c3a8d
commit 77539dc
Showing
9 changed files
with
160 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::arch; | ||
use crate::arch::paging::{entry::PageFlags, MAPPER}; | ||
use crate::board_info::BoardInfo; | ||
use crate::paging::PhysicalAddress; | ||
use core::arch::asm; | ||
use core::ptr::addr_of; | ||
|
||
// Example of TLS usage: | ||
// #[thread_local] | ||
// static mut HART_ID: usize = 1; | ||
|
||
extern "C" { | ||
static __tdata_start: u8; | ||
static __tbss_end: u8; | ||
} | ||
|
||
static mut TLS_BASE: Option<PhysicalAddress> = None; | ||
|
||
pub fn setup(board_info: &BoardInfo) -> crate::Result<()> { | ||
let tls_start = unsafe { addr_of!(__tdata_start) as usize }; | ||
let tls_end = unsafe { addr_of!(__tbss_end) as usize }; | ||
|
||
let tls_region = unsafe { PhysicalAddress::new(tls_start)..PhysicalAddress::new(tls_end) }; | ||
|
||
if !tls_region.is_empty() { | ||
let mut mapper = unsafe { MAPPER.lock() }; | ||
let mapper = mapper.as_mut().unwrap(); | ||
|
||
let num_pages = (tls_region.end.as_raw() - tls_region.start.as_raw()) | ||
.div_ceil(arch::PAGE_SIZE) | ||
* board_info.cpus; | ||
let start = mapper.allocator_mut().allocate_frames(num_pages)?; | ||
|
||
log::debug!( | ||
"mapping TLS region: {:?}", | ||
start..start.add(num_pages * arch::PAGE_SIZE) | ||
); | ||
|
||
for i in 0..num_pages { | ||
let phys = start.add(i * arch::PAGE_SIZE); | ||
let flush = mapper.map_identity(phys, PageFlags::READ | PageFlags::WRITE)?; | ||
unsafe { | ||
flush.ignore(); | ||
} | ||
} | ||
|
||
unsafe { | ||
TLS_BASE = Some(start); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn activate() { | ||
unsafe { | ||
let base = TLS_BASE.unwrap(); | ||
asm!("mv tp, {0}", in(reg) base.as_raw()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters