Skip to main content

kh_runtime/
host_slot.rs

1//! Per-host-thread state reachable **without** host `TPIDR_EL0` / Rust TLS.
2//!
3//! Guest code may point `TPIDR_EL0` at freestanding Darwin TSD. While that is
4//! live, `thread_local!` and glibc TLS are unusable. Everything needed on the
5//! guest→host boundary is stored here, keyed by the OS thread id (`gettid` on
6//! Linux, `pthread_self` elsewhere).
7//!
8//! **Hot path under guest TPIDR** must not allocate (glibc malloc may touch TLS).
9//! Insert/claim slots only while host TPIDR is live (prepare paths).
10#![allow(unsafe_code)]
11
12use std::collections::HashMap;
13use std::sync::{Mutex, OnceLock};
14
15/// Host glibc / guest TSD snapshots for one OS thread.
16#[derive(Clone, Copy, Debug, Default)]
17pub struct TlsSlot {
18    /// Host `TPIDR_EL0` captured before any guest `msr`.
19    pub host_tpidr: u64,
20    /// Last guest `TPIDR_EL0` (refreshed via `mrs` on host entry).
21    pub guest_tpidr: u64,
22    /// True after prepare.
23    pub active: bool,
24}
25
26/// Hypercall alt stack (host private map).
27#[derive(Clone, Copy, Debug)]
28pub struct AltStackSlot {
29    pub base: *mut u8,
30    pub len: usize,
31    pub top: u64,
32}
33
34// SAFETY: each slot is only mutated from its owning OS thread; the map lock
35// serializes insert/remove. Pointers are private anonymous maps.
36unsafe impl Send for AltStackSlot {}
37
38/// Worker exit trampoline + guest pthread identity.
39#[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/// Stable key for the current OS thread (does not touch `TPIDR_EL0`).
61#[must_use]
62pub fn os_tid() -> u64 {
63    #[cfg(target_os = "linux")]
64    {
65        // SAFETY: `gettid` returns a scalar; no TLS required.
66        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        // SAFETY: pthread_self is a thread cookie for map keying.
72        let p = unsafe { libc::pthread_self() };
73        #[allow(clippy::as_conversions)]
74        {
75            p as u64
76        }
77    }
78}
79
80/// Insert/update while **host** TPIDR is live (may allocate).
81fn 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
90/// Mutate an existing slot only (no HashMap insert — safe under guest TPIDR).
91fn 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
100/// Read snapshot without insert.
101fn 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
109/// Prepare/update TLS fields (host TPIDR live — may create slot).
110pub fn with_tls_init<R>(f: impl FnOnce(&mut TlsSlot) -> R) -> R {
111    with_slot_init(|s| f(&mut s.tls))
112}
113
114/// Mutate TLS on an existing slot only.
115pub 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
124/// Alt stack accessor. Uses existing slot when present (guest-TPIDR safe).
125pub 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    // First touch without prepare — allocate under whatever TLS is current.
134    // Prefer prepare_host_meta first in production paths.
135    let slot = guard.entry(tid).or_default();
136    f(&mut slot.alt)
137}
138
139/// Worker slot accessor.
140pub 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
157/// Drop this OS thread's slot (worker exit). Caller unmaps alt stack first.
158pub 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}