diff --git a/redox-rt/src/thread.rs b/redox-rt/src/thread.rs index 7b0059ed08..3536257db3 100644 --- a/redox-rt/src/thread.rs +++ b/redox-rt/src/thread.rs @@ -58,11 +58,6 @@ pub unsafe fn exit_this_thread(stack_base: *mut (), stack_size: usize) -> ! { // TODO: modify interface so it writes directly to the thread fd? let status_fd = tcb.thread_fd().dup_into_upper(b"status").unwrap(); - let tcb_ptr = unsafe { crate::Tcb::current_ptr() }; - if let Some(tcb_ptr) = tcb_ptr { - let _ = unsafe { syscall::funmap(tcb_ptr as usize, syscall::PAGE_SIZE) }; - } - let mut buf = [0; size_of::() * 3]; plain::slice_from_mut_bytes(&mut buf) .unwrap() diff --git a/src/ld_so/tcb.rs b/src/ld_so/tcb.rs index 2f90ad8cb6..73c37709e0 100644 --- a/src/ld_so/tcb.rs +++ b/src/ld_so/tcb.rs @@ -107,6 +107,7 @@ impl Tcb { stack_base: core::ptr::null_mut(), stack_size: 0, os_tid: UnsafeCell::new(OsTid::default()), + tcb_selfref: UnsafeCell::new(tcb_ptr), }, dtv_ptr: ptr::null_mut(), diff --git a/src/pthread/mod.rs b/src/pthread/mod.rs index e3d559e2e8..4d488e37df 100644 --- a/src/pthread/mod.rs +++ b/src/pthread/mod.rs @@ -32,6 +32,8 @@ pub unsafe fn init() { stack_base: ptr::null_mut(), stack_size: 0, + tcb_selfref: UnsafeCell::new(core::ptr::null_mut()), + os_tid: UnsafeCell::new(Sys::current_os_tid()), }; @@ -43,9 +45,11 @@ pub unsafe fn init() { thread.stack_size = STACK_SIZE; } - unsafe { Tcb::current() } - .expect_notls("no TCB present for main thread") - .pthread = thread; + let current_tcb = unsafe { Tcb::current() }.expect_notls("no TCB present for main thread"); + current_tcb.pthread = thread; + unsafe { + current_tcb.pthread.tcb_selfref.get().write(current_tcb); + } } //static NEXT_INDEX: AtomicU32 = AtomicU32::new(FIRST_THREAD_IDX + 1); @@ -73,6 +77,8 @@ pub struct Pthread { pub(crate) stack_base: *mut c_void, pub(crate) stack_size: usize, + pub(crate) tcb_selfref: UnsafeCell<*mut Tcb>, + pub os_tid: UnsafeCell, } @@ -317,6 +323,13 @@ unsafe fn dealloc_thread(thread: &Pthread) { unsafe { OS_TID_TO_PTHREAD.lock().remove(&thread.os_tid.get().read()); } + #[cfg(target_os = "redox")] + unsafe { + let tcb = thread.tcb_selfref.get().read(); + if !tcb.is_null() { + let _ = syscall::funmap(tcb as usize, syscall::PAGE_SIZE); + } + } } pub const SIGRT_RLCT_CANCEL: usize = 33; pub const SIGRT_RLCT_TIMER: usize = 34;