1#![allow(unsafe_code)]
11
12use std::collections::HashMap;
13use std::sync::{Mutex, OnceLock};
14
15#[derive(Clone, Copy, Debug, Default)]
17pub struct TlsSlot {
18 pub host_tpidr: u64,
20 pub guest_tpidr: u64,
22 pub active: bool,
24}
25
26#[derive(Clone, Copy, Debug)]
28pub struct AltStackSlot {
29 pub base: *mut u8,
30 pub len: usize,
31 pub top: u64,
32}
33
34unsafe impl Send for AltStackSlot {}
37
38#[derive(Clone, Copy, Debug, Default)]
40pub struct WorkerSlot {
41 pub exit_pc: u64,
42 pub exit_sp: u64,
43 pub has_exit: bool,
44 pub guest_pthread: u64,
45 pub guest_tid: u64,
46}
47
48#[derive(Default)]
49struct ThreadSlot {
50 tls: TlsSlot,
51 alt: Option<AltStackSlot>,
52 worker: WorkerSlot,
53}
54
55fn slots() -> &'static Mutex<HashMap<u64, ThreadSlot>> {
56 static SLOTS: OnceLock<Mutex<HashMap<u64, ThreadSlot>>> = OnceLock::new();
57 SLOTS.get_or_init(|| Mutex::new(HashMap::new()))
58}
59
60#[must_use]
62pub fn os_tid() -> u64 {
63 #[cfg(target_os = "linux")]
64 {
65 let raw = unsafe { libc::syscall(libc::SYS_gettid) };
67 u64::try_from(raw).unwrap_or(0)
68 }
69 #[cfg(not(target_os = "linux"))]
70 {
71 let p = unsafe { libc::pthread_self() };
73 #[allow(clippy::as_conversions)]
74 {
75 p as u64
76 }
77 }
78}
79
80fn with_slot_init<R>(f: impl FnOnce(&mut ThreadSlot) -> R) -> R {
82 let tid = os_tid();
83 let mut guard = slots()
84 .lock()
85 .unwrap_or_else(std::sync::PoisonError::into_inner);
86 let slot = guard.entry(tid).or_default();
87 f(slot)
88}
89
90fn with_slot_existing_mut<R>(f: impl FnOnce(&mut ThreadSlot) -> R) -> Option<R> {
92 let tid = os_tid();
93 let mut guard = slots()
94 .lock()
95 .unwrap_or_else(std::sync::PoisonError::into_inner);
96 let slot = guard.get_mut(&tid)?;
97 Some(f(slot))
98}
99
100fn with_slot_existing<R>(f: impl FnOnce(&ThreadSlot) -> R) -> Option<R> {
102 let tid = os_tid();
103 let guard = slots()
104 .lock()
105 .unwrap_or_else(std::sync::PoisonError::into_inner);
106 guard.get(&tid).map(f)
107}
108
109pub fn with_tls_init<R>(f: impl FnOnce(&mut TlsSlot) -> R) -> R {
111 with_slot_init(|s| f(&mut s.tls))
112}
113
114pub fn with_tls_existing_mut<R>(f: impl FnOnce(&mut TlsSlot) -> R) -> Option<R> {
116 with_slot_existing_mut(|s| f(&mut s.tls))
117}
118
119#[must_use]
120pub fn tls_get() -> TlsSlot {
121 with_slot_existing(|s| s.tls).unwrap_or_default()
122}
123
124pub fn with_alt_mut<R>(f: impl FnOnce(&mut Option<AltStackSlot>) -> R) -> R {
126 let tid = os_tid();
127 let mut guard = slots()
128 .lock()
129 .unwrap_or_else(std::sync::PoisonError::into_inner);
130 if let Some(slot) = guard.get_mut(&tid) {
131 return f(&mut slot.alt);
132 }
133 let slot = guard.entry(tid).or_default();
136 f(&mut slot.alt)
137}
138
139pub fn with_worker_mut<R>(f: impl FnOnce(&mut WorkerSlot) -> R) -> R {
141 let tid = os_tid();
142 let mut guard = slots()
143 .lock()
144 .unwrap_or_else(std::sync::PoisonError::into_inner);
145 if let Some(slot) = guard.get_mut(&tid) {
146 return f(&mut slot.worker);
147 }
148 let slot = guard.entry(tid).or_default();
149 f(&mut slot.worker)
150}
151
152#[must_use]
153pub fn worker_get() -> WorkerSlot {
154 with_slot_existing(|s| s.worker).unwrap_or_default()
155}
156
157pub fn clear_current() {
159 let tid = os_tid();
160 let mut guard = slots()
161 .lock()
162 .unwrap_or_else(std::sync::PoisonError::into_inner);
163 let _removed = guard.remove(&tid);
164}