Skip to main content

kh_runtime/
process.rs

1//! Process-wide translator state for the single guest-process model.
2//!
3//! Hot paths avoid the process `RwLock`:
4//! - FD table is lock-free atomics ([`fd_get`] / [`fd_alloc`] / [`fd_take`])
5//! - Bottle root is a separate `RwLock` (path translate never takes process write)
6//! - Directory streams use a dedicated mutex (readdir never blocks open/stat)
7
8use std::collections::HashMap;
9use std::os::fd::RawFd;
10use std::path::{Path, PathBuf};
11use std::sync::atomic::{AtomicI32, AtomicU64, Ordering};
12use std::sync::{Mutex, OnceLock, RwLock};
13
14use crate::host;
15
16/// Process-wide syscall counter (hot path: no process-state lock).
17static SYSCALL_COUNT: AtomicU64 = AtomicU64::new(0);
18/// Cap for [`SYSCALL_COUNT`]; updated by [`reset_run`].
19static MAX_SYSCALLS: AtomicU64 = AtomicU64::new(256);
20
21/// Max guest FD slots (stdio 0–2 + table). Keeps FDs under typical OPEN_MAX.
22const FD_SLOTS: usize = 1024;
23/// Empty slot in the lock-free FD map.
24const FD_EMPTY: i32 = -1;
25
26/// Guest → host FD map for **lookup** without the process `RwLock`.
27///
28/// Slots 0–2 are unused (stdio identity). Other slots: host fd ≥ 0, or [`FD_EMPTY`].
29static FD_HOST: [AtomicI32; FD_SLOTS] = [const { AtomicI32::new(FD_EMPTY) }; FD_SLOTS];
30/// Hint for next free FD scan.
31static FD_NEXT: AtomicI32 = AtomicI32::new(3);
32
33/// Soft Darwin `sigaction` slot (handler + mask + flags).
34#[derive(Clone, Copy, Debug)]
35pub(crate) struct SoftSigAct {
36    pub handler: u64,
37    pub mask: u32,
38    pub flags: i32,
39}
40
41/// Values from Darwin `bsdthread_register` (libpthread start trampoline, etc.).
42///
43/// Extra fields are retained for future workq/TLS wiring even when unused today.
44#[derive(Clone, Copy, Debug, Default)]
45#[allow(dead_code)]
46pub(crate) struct BsdThreadReg {
47    /// Userland start trampoline (`_pthread_start`).
48    pub threadstart: u64,
49    /// Workqueue thread entry (unused in micro; stored for completeness).
50    pub wqthread: u64,
51    /// Registration flags from libpthread.
52    pub flags: u32,
53    /// Stack address hint (unused in micro).
54    pub stack_addr_hint: u64,
55    /// TSD offset within the pthread structure.
56    pub tsd_offset: u32,
57    /// Dispatch queue offset within the pthread structure.
58    pub dispatchqueue_offset: u32,
59}
60
61impl SoftSigAct {
62    pub(crate) const fn zero() -> Self {
63        Self {
64            handler: 0,
65            mask: 0,
66            flags: 0,
67        }
68    }
69}
70
71/// Resolve guest FD → host FD without taking the process lock (I/O hot path).
72#[must_use]
73#[inline]
74pub fn fd_get(gfd: i32) -> Option<RawFd> {
75    if gfd == 0 || gfd == 1 || gfd == 2 {
76        return Some(gfd);
77    }
78    if gfd < 0 {
79        return None;
80    }
81    let Ok(idx) = usize::try_from(gfd) else {
82        return None;
83    };
84    let slot = FD_HOST.get(idx)?;
85    let v = slot.load(Ordering::Acquire);
86    if v < 0 { None } else { Some(v) }
87}
88
89/// Allocates a guest FD for `host_fd` without the process lock.
90#[must_use]
91#[inline]
92pub fn fd_alloc(host_fd: RawFd) -> Option<i32> {
93    if host_fd < 0 {
94        return None;
95    }
96    let start = usize::try_from(FD_NEXT.load(Ordering::Relaxed).max(3)).unwrap_or(3);
97    for idx in start..FD_SLOTS {
98        if try_claim_slot(idx, host_fd) {
99            let gfd = i32::try_from(idx).ok()?;
100            FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
101            return Some(gfd);
102        }
103    }
104    for idx in 3..start.min(FD_SLOTS) {
105        if try_claim_slot(idx, host_fd) {
106            let gfd = i32::try_from(idx).ok()?;
107            FD_NEXT.store(gfd.saturating_add(1), Ordering::Relaxed);
108            return Some(gfd);
109        }
110    }
111    None
112}
113
114/// Removes a guest FD mapping and returns the host FD (lock-free).
115#[must_use]
116#[inline]
117pub fn fd_take(gfd: i32) -> Option<RawFd> {
118    if gfd <= 2 {
119        return None;
120    }
121    let Ok(idx) = usize::try_from(gfd) else {
122        return None;
123    };
124    let slot = FD_HOST.get(idx)?;
125    let v = slot.swap(FD_EMPTY, Ordering::AcqRel);
126    if v < 0 { None } else { Some(v) }
127}
128
129fn try_claim_slot(idx: usize, host_fd: RawFd) -> bool {
130    let Some(slot) = FD_HOST.get(idx) else {
131        return false;
132    };
133    slot.compare_exchange(FD_EMPTY, host_fd, Ordering::AcqRel, Ordering::Relaxed)
134        .is_ok()
135}
136
137fn reset_fd_map() {
138    for (i, slot) in FD_HOST.iter().enumerate() {
139        let prev = slot.swap(FD_EMPTY, Ordering::AcqRel);
140        if i > 2 && prev >= 0 {
141            host::close_fd(prev);
142        }
143    }
144    FD_NEXT.store(3, Ordering::Relaxed);
145}
146
147// ── bottle root (separate from process lock) ────────────────────────────────
148
149fn bottle_root_lock() -> &'static RwLock<Option<PathBuf>> {
150    static ROOT: OnceLock<RwLock<Option<PathBuf>>> = OnceLock::new();
151    ROOT.get_or_init(|| RwLock::new(None))
152}
153
154// ── directory streams (separate mutex; readdir ≠ open contention) ───────────
155
156fn dir_streams_lock() -> &'static Mutex<HashMap<i32, host::HostDir>> {
157    static DIRS: OnceLock<Mutex<HashMap<i32, host::HostDir>>> = OnceLock::new();
158    DIRS.get_or_init(|| Mutex::new(HashMap::new()))
159}
160
161/// Closes any directory stream associated with `gfd` (call before FD free).
162pub fn close_dir_stream(gfd: i32) {
163    if let Ok(mut map) = dir_streams_lock().lock() {
164        drop(map.remove(&gfd));
165    } else if let Err(poisoned) = dir_streams_lock().lock() {
166        drop(poisoned.into_inner().remove(&gfd));
167    }
168}
169
170/// Next directory entry for `gfd`, writing the name into `name_out` (NUL-terminated).
171///
172/// Returns `Ok(Some(d_type))` when an entry was written, `Ok(None)` at EOF.
173pub fn readdir_next_into(gfd: i32, name_out: &mut [u8]) -> Result<Option<u8>, i64> {
174    let Some(host_fd) = fd_get(gfd) else {
175        return Err(9); // EBADF
176    };
177
178    let mut map = match dir_streams_lock().lock() {
179        Ok(g) => g,
180        Err(p) => p.into_inner(),
181    };
182
183    if let std::collections::hash_map::Entry::Vacant(e) = map.entry(gfd) {
184        match host::HostDir::open_dup(host_fd) {
185            Ok(dir) => {
186                e.insert(dir);
187            }
188            Err(err) => return Err(i64::from(err)),
189        }
190    }
191
192    let Some(stream) = map.get_mut(&gfd) else {
193        return Err(9);
194    };
195    match stream.read_next_into(name_out) {
196        Some(d_type) => Ok(Some(d_type)),
197        None => Ok(None),
198    }
199}
200
201/// Owned process state for one guest (or unit-test isolation via reset).
202#[derive(Debug)]
203pub struct ProcessState {
204    sig_mask: u32,
205    sigactions: [SoftSigAct; 32],
206    bsdthread: Option<BsdThreadReg>,
207}
208
209impl ProcessState {
210    /// Fresh process state with default syscall limit.
211    #[must_use]
212    pub fn new() -> Self {
213        Self {
214            sig_mask: 0,
215            sigactions: [SoftSigAct::zero(); 32],
216            bsdthread: None,
217        }
218    }
219
220    /// Resets FD table, soft signals, thread reg, and counters. Preserves bottle root.
221    pub fn reset_run(&mut self, max_syscalls: usize) {
222        // Drop all directory streams before closing FDs.
223        if let Ok(mut map) = dir_streams_lock().lock() {
224            map.clear();
225        } else if let Err(poisoned) = dir_streams_lock().lock() {
226            poisoned.into_inner().clear();
227        }
228        reset_fd_map();
229        self.sig_mask = 0;
230        self.sigactions = [SoftSigAct::zero(); 32];
231        self.bsdthread = None;
232        let max = u64::try_from(max_syscalls).unwrap_or(256);
233        MAX_SYSCALLS.store(max, Ordering::Relaxed);
234        SYSCALL_COUNT.store(0, Ordering::Relaxed);
235        crate::thread::reset_thread_runtime();
236    }
237
238    #[must_use]
239    pub(crate) fn bsdthread(&self) -> Option<BsdThreadReg> {
240        self.bsdthread
241    }
242
243    pub(crate) fn set_bsdthread(&mut self, reg: BsdThreadReg) {
244        self.bsdthread = Some(reg);
245    }
246
247    #[must_use]
248    pub(crate) fn sig_mask(&self) -> u32 {
249        self.sig_mask
250    }
251
252    pub(crate) fn set_sig_mask(&mut self, mask: u32) {
253        self.sig_mask = mask;
254    }
255
256    #[must_use]
257    pub(crate) fn sigaction(&self, sig: usize) -> Option<SoftSigAct> {
258        self.sigactions.get(sig).copied()
259    }
260
261    pub(crate) fn set_sigaction(&mut self, sig: usize, act: SoftSigAct) -> bool {
262        if let Some(slot) = self.sigactions.get_mut(sig) {
263            *slot = act;
264            true
265        } else {
266            false
267        }
268    }
269}
270
271impl Default for ProcessState {
272    fn default() -> Self {
273        Self::new()
274    }
275}
276
277/// Bumps the process-wide syscall counter without taking the process lock.
278///
279/// Returns `true` when the configured max has been exceeded.
280#[must_use]
281pub fn tick_syscall() -> bool {
282    let n = SYSCALL_COUNT.fetch_add(1, Ordering::Relaxed);
283    n >= MAX_SYSCALLS.load(Ordering::Relaxed)
284}
285
286fn active_lock() -> &'static RwLock<ProcessState> {
287    static ACTIVE: OnceLock<RwLock<ProcessState>> = OnceLock::new();
288    ACTIVE.get_or_init(|| RwLock::new(ProcessState::new()))
289}
290
291/// Exclusive access to the active process state.
292pub fn with_mut<R>(f: impl FnOnce(&mut ProcessState) -> R) -> R {
293    match active_lock().write() {
294        Ok(mut guard) => f(&mut guard),
295        Err(poisoned) => f(&mut poisoned.into_inner()),
296    }
297}
298
299/// Shared access to the active process state.
300pub fn with_ref<R>(f: impl FnOnce(&ProcessState) -> R) -> R {
301    match active_lock().read() {
302        Ok(guard) => f(&guard),
303        Err(poisoned) => f(&poisoned.into_inner()),
304    }
305}
306
307/// Resets FD/signals/counters for a new guest run (keeps bottle root).
308pub fn reset_run(max_syscalls: usize) {
309    with_mut(|p| p.reset_run(max_syscalls));
310}
311
312/// Configures the bottle root used by path-taking syscalls.
313pub fn set_bottle_root(root: Option<PathBuf>) {
314    match bottle_root_lock().write() {
315        Ok(mut g) => *g = root,
316        Err(p) => *p.into_inner() = root,
317    }
318}
319
320/// Clone of the configured bottle root, if any.
321#[must_use]
322pub fn bottle_root() -> Option<PathBuf> {
323    match bottle_root_lock().read() {
324        Ok(g) => g.clone(),
325        Err(p) => p.into_inner().clone(),
326    }
327}
328
329/// Borrow the bottle root without cloning (hot path for path translation).
330pub fn with_bottle_root<R>(f: impl FnOnce(Option<&Path>) -> R) -> R {
331    match bottle_root_lock().read() {
332        Ok(g) => f(g.as_deref()),
333        Err(p) => f(p.into_inner().as_deref()),
334    }
335}
336
337/// Serializes tests that mutate process-wide state (address space + process).
338#[cfg(test)]
339pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
340    use std::sync::Mutex as StdMutex;
341    static LOCK: StdMutex<()> = StdMutex::new(());
342    LOCK.lock()
343        .unwrap_or_else(std::sync::PoisonError::into_inner)
344}