kh-runtime 0.1.14

Kakehashi native ARM64 execution context, memory layout, traps, and bottle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Guest address-space tracking for trap-path validation and dynamic maps.
//!
//! [`AddressSpace`] owns the region list for one guest process. Image and stack
//! regions are registered as **borrowed** (owned by `GuestMemory` / the stack
//! `MappedRegion`). Guest `mmap` registers **owned** regions that the space
//! unmaps on `munmap` / `clear`.
//!
//! Trap handlers and BSD syscalls use a process-wide **active** address space
//! installed before guest entry (see [`install_active`] / free-function
//! wrappers). Unit tests can exercise a local [`AddressSpace`] without touching
//! the active slot, or use the wrappers under [`test_lock`].

use std::cell::Cell;
use std::sync::{Arc, OnceLock, RwLock};

use crate::host;

use super::map::{MappedRegion, VM_PROT_READ, VM_PROT_WRITE};

/// Per-thread last hit for [`check_range`] (archive I/O is highly sequential).
#[derive(Clone, Copy, Debug)]
struct RangeCache {
    guest_addr: u64,
    len: usize,
    prot: u32,
}

impl RangeCache {
    fn contains(self, addr: u64, len: usize) -> bool {
        if len == 0 {
            return addr >= self.guest_addr
                && addr
                    <= self
                        .guest_addr
                        .saturating_add(u64::try_from(self.len).unwrap_or(u64::MAX));
        }
        let Some(end) = addr.checked_add(u64::try_from(len).unwrap_or(u64::MAX)) else {
            return false;
        };
        let reg_end = self
            .guest_addr
            .saturating_add(u64::try_from(self.len).unwrap_or(u64::MAX));
        addr >= self.guest_addr && end <= reg_end
    }

    fn allows(self, need_write: bool) -> bool {
        if need_write {
            self.prot & VM_PROT_WRITE != 0
        } else {
            self.prot & (VM_PROT_READ | VM_PROT_WRITE) != 0
        }
    }

    fn from_region(region: &RegRegion) -> Self {
        Self {
            guest_addr: region.guest_addr,
            len: region.len,
            prot: region.prot,
        }
    }
}

thread_local! {
    static LAST_RANGE: Cell<Option<RangeCache>> = const { Cell::new(None) };
}

/// One live guest mapping visible to the syscall layer.
#[derive(Debug)]
struct RegRegion {
    /// Guest VA of the mapping base (identity with host in the current model).
    guest_addr: u64,
    /// Host mapping length in bytes.
    len: usize,
    /// Host base pointer.
    ptr: *mut u8,
    /// Darwin `VM_PROT_*` bits.
    prot: u32,
    /// When true, [`AddressSpace::clear`] / `munmap` will `munmap` the host pages.
    owned: bool,
}

// SAFETY: regions are only mutated while the owning `AddressSpace` is locked
// (active slot mutex or exclusive local borrow); pointers uniquely refer to
// live maps for the single guest process model.
unsafe impl Send for RegRegion {}

impl RegRegion {
    /// Exclusive end guest address (`guest_addr + len`), saturating.
    #[must_use]
    fn guest_end(&self) -> u64 {
        self.guest_addr
            .saturating_add(u64::try_from(self.len).unwrap_or(u64::MAX))
    }

    /// Whether `[addr, addr+len)` is fully contained in this region.
    #[must_use]
    fn contains_range(&self, addr: u64, len: usize) -> bool {
        if len == 0 {
            return addr >= self.guest_addr && addr <= self.guest_end();
        }
        let Some(end) = addr.checked_add(u64::try_from(len).unwrap_or(u64::MAX)) else {
            return false;
        };
        addr >= self.guest_addr && end <= self.guest_end()
    }
}

/// Tracked guest VA ranges for one process (borrowed images + owned mmaps).
///
/// Independent of the active trap slot: build and test locally, then
/// [`install_active`] before jumping to guest code.
///
/// [`check_snap`] is an immutable Arc rebuilt on mutations so concurrent
/// [`check_range`] can walk regions without holding the active `RwLock`.
#[derive(Debug)]
pub struct AddressSpace {
    regions: Vec<RegRegion>,
    /// Copy-only meta for the I/O hot path (shared across threads).
    check_snap: Arc<Vec<RangeCache>>,
}

impl Default for AddressSpace {
    fn default() -> Self {
        Self::new()
    }
}

// SAFETY: regions are only mutated under the active write lock; concurrent
// readers only call pure range checks. Raw host pointers are not shared for
// mutation across threads without that lock.
unsafe impl Sync for AddressSpace {}

impl AddressSpace {
    /// Empty address space (no tracked regions).
    #[must_use]
    pub fn new() -> Self {
        Self {
            regions: Vec::new(),
            check_snap: Arc::new(Vec::new()),
        }
    }

    fn rebuild_snap(&mut self) {
        let snap: Vec<RangeCache> = self.regions.iter().map(RangeCache::from_region).collect();
        self.check_snap = Arc::new(snap);
    }

    /// True when at least one region is registered.
    #[must_use]
    pub fn is_active(&self) -> bool {
        !self.regions.is_empty()
    }

    /// Clears all regions. Owned mappings are `munmap`'d; borrowed ones are not.
    pub fn clear(&mut self) {
        for region in self.regions.drain(..) {
            if region.owned {
                let _ = host::munmap(region.ptr, region.len);
            }
        }
        self.rebuild_snap();
    }

    /// Registers a borrowed region (image / stack). Does not take ownership.
    pub fn register_borrowed(&mut self, region: &MappedRegion) {
        self.regions.push(RegRegion {
            guest_addr: region.guest_addr,
            len: region.host_len(),
            ptr: host_ptr_from_addr(region.host_addr()),
            prot: region.prot,
            owned: false,
        });
        self.rebuild_snap();
    }

    /// Registers an owned mapping created by a guest `mmap`.
    pub fn register_owned(&mut self, guest_addr: u64, ptr: *mut u8, len: usize, prot: u32) {
        self.regions.push(RegRegion {
            guest_addr,
            len,
            ptr,
            prot,
            owned: true,
        });
        self.rebuild_snap();
    }

    /// Validates that `[addr, addr+len)` lies in a registered region with the
    /// requested access. When empty (unit tests without maps), returns `true`
    /// so pure dispatch tests do not need a full map setup.
    #[must_use]
    pub fn check_range(&self, addr: u64, len: usize, need_write: bool) -> bool {
        if len == 0 {
            return true;
        }
        if addr == 0 {
            return false;
        }
        if self.regions.is_empty() {
            return true;
        }
        for region in &self.regions {
            if !region.contains_range(addr, len) {
                continue;
            }
            if need_write {
                return region.prot & VM_PROT_WRITE != 0;
            }
            // Readable if any of R/W is set (writable maps are readable for load).
            return region.prot & (VM_PROT_READ | VM_PROT_WRITE) != 0;
        }
        false
    }

    /// Finds the region that fully contains `[addr, addr+len)`, if any.
    #[must_use]
    pub fn find_covering(&self, addr: u64, len: usize) -> Option<RegRegionSnapshot> {
        for region in &self.regions {
            if region.contains_range(addr, len) {
                return Some(RegRegionSnapshot {
                    guest_addr: region.guest_addr,
                    len: region.len,
                    ptr: region.ptr,
                    prot: region.prot,
                    owned: region.owned,
                });
            }
        }
        None
    }

    /// Updates protection bits on the region covering `addr`.
    pub fn update_prot(&mut self, addr: u64, len: usize, prot: u32) -> bool {
        for region in &mut self.regions {
            if region.contains_range(addr, len) {
                region.prot = prot;
                self.rebuild_snap();
                return true;
            }
        }
        false
    }

    /// Removes and optionally unmaps an owned region that fully covers the range.
    ///
    /// Borrowed regions are removed from tracking **without** `munmap`.
    pub fn remove_range(&mut self, addr: u64, len: usize) -> RemoveOutcome {
        let idx = self
            .regions
            .iter()
            .position(|r| r.contains_range(addr, len));
        let Some(i) = idx else {
            return RemoveOutcome::NotFound;
        };
        let region = self.regions.remove(i);
        self.rebuild_snap();
        if region.owned {
            if host::munmap(region.ptr, region.len) {
                RemoveOutcome::Unmapped
            } else {
                RemoveOutcome::UnmapFailed
            }
        } else {
            RemoveOutcome::Untracked
        }
    }

    /// Test/helper: push a raw region (used by unit tests for RX-only maps).
    #[cfg(test)]
    fn push_raw(&mut self, region: RegRegion) {
        self.regions.push(region);
        self.rebuild_snap();
    }
}

impl Drop for AddressSpace {
    fn drop(&mut self) {
        self.clear();
    }
}

/// Active address space for the trap / syscall path (single guest process).
///
/// `RwLock`: concurrent `check_range` on the I/O hot path must not serialize
/// all guest threads behind a single exclusive mutex.
fn active_space() -> &'static RwLock<AddressSpace> {
    static ACTIVE: OnceLock<RwLock<AddressSpace>> = OnceLock::new();
    ACTIVE.get_or_init(|| RwLock::new(AddressSpace::new()))
}

/// Installs `space` as the active trap-path address space.
///
/// Returns the previous active space (caller may drop it to unmap owned regions).
pub fn install_active(space: AddressSpace) -> AddressSpace {
    match active_space().write() {
        Ok(mut guard) => std::mem::replace(&mut *guard, space),
        Err(poisoned) => {
            let mut guard = poisoned.into_inner();
            std::mem::replace(&mut *guard, space)
        }
    }
}

/// Takes the active address space, leaving an empty one installed.
#[must_use]
pub fn take_active() -> AddressSpace {
    install_active(AddressSpace::new())
}

/// Runs `f` with exclusive access to the active address space.
pub fn with_active_mut<R>(f: impl FnOnce(&mut AddressSpace) -> R) -> R {
    match active_space().write() {
        Ok(mut guard) => f(&mut guard),
        Err(poisoned) => f(&mut poisoned.into_inner()),
    }
}

/// Runs `f` with shared access to the active address space.
pub fn with_active<R>(f: impl FnOnce(&AddressSpace) -> R) -> R {
    match active_space().read() {
        Ok(guard) => f(&guard),
        Err(poisoned) => f(&poisoned.into_inner()),
    }
}

/// Clears the active address space. Owned mappings are `munmap`'d.
pub fn clear() {
    LAST_RANGE.with(|c| c.set(None));
    with_active_mut(AddressSpace::clear);
}

/// Registers a borrowed region on the active address space.
pub fn register_borrowed(region: &MappedRegion) {
    with_active_mut(|space| space.register_borrowed(region));
}

/// Registers an owned anonymous mapping on the active address space.
pub fn register_owned(guest_addr: u64, ptr: *mut u8, len: usize, prot: u32) {
    with_active_mut(|space| space.register_owned(guest_addr, ptr, len, prot));
}

/// True when the active space has at least one region.
#[must_use]
pub fn is_active() -> bool {
    with_active(AddressSpace::is_active)
}

/// Validates a range against the active address space.
///
/// Hot path: thread-local last-hit cache (no `RwLock`) for sequential
/// read/write into the same mapping. On miss, clones an [`Arc`] region
/// snapshot under a short read lock, then walks **without** holding the lock.
#[must_use]
pub fn check_range(addr: u64, len: usize, need_write: bool) -> bool {
    if len == 0 {
        return true;
    }
    if addr == 0 {
        return false;
    }
    if LAST_RANGE.with(|c| {
        c.get()
            .is_some_and(|r| r.contains(addr, len) && r.allows(need_write))
    }) {
        return true;
    }
    // Short lock: Arc clone only.
    let snap = with_active(|space| Arc::clone(&space.check_snap));
    if snap.is_empty() {
        // Empty space → allow (unit tests without maps).
        return true;
    }
    for region in snap.iter() {
        if !region.contains(addr, len) {
            continue;
        }
        let ok = region.allows(need_write);
        if ok {
            LAST_RANGE.with(|c| c.set(Some(*region)));
        }
        return ok;
    }
    false
}

/// Finds a covering region in the active address space.
#[must_use]
pub fn find_covering(addr: u64, len: usize) -> Option<RegRegionSnapshot> {
    with_active(|space| space.find_covering(addr, len))
}

/// Updates protection on a region in the active address space.
pub fn update_prot(addr: u64, len: usize, prot: u32) -> bool {
    with_active_mut(|space| space.update_prot(addr, len, prot))
}

/// Removes a range from the active address space.
pub fn remove_range(addr: u64, len: usize) -> RemoveOutcome {
    with_active_mut(|space| space.remove_range(addr, len))
}

/// Snapshot of a registry entry (no ownership).
#[derive(Debug, Clone, Copy)]
pub struct RegRegionSnapshot {
    /// Guest base.
    pub guest_addr: u64,
    /// Host length.
    pub len: usize,
    /// Host pointer.
    pub ptr: *mut u8,
    /// Darwin prot.
    pub prot: u32,
    /// Owned by the address space.
    pub owned: bool,
}

// SAFETY: snapshot is a POD copy of pointers managed under the active mutex
// during the syscall that uses it.
unsafe impl Send for RegRegionSnapshot {}

/// Result of [`AddressSpace::remove_range`] / [`remove_range`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RemoveOutcome {
    /// No covering region.
    NotFound,
    /// Owned region unmapped successfully.
    Unmapped,
    /// Borrowed region dropped from tracking only.
    Untracked,
    /// `munmap` failed.
    UnmapFailed,
}

fn host_ptr_from_addr(addr: u64) -> *mut u8 {
    let u = usize::try_from(addr).unwrap_or(0);
    std::ptr::with_exposed_provenance_mut(u)
}

/// Serializes tests that mutate process-wide active space / process state.
#[cfg(test)]
pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
    crate::process::test_lock()
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use crate::mem::HostPageSize;
    use crate::mem::map::{VM_PROT_EXECUTE, map_stack};

    #[test]
    fn empty_local_space_allows_checks() {
        let space = AddressSpace::new();
        assert!(!space.is_active());
        assert!(space.check_range(0x1000, 16, false));
    }

    #[test]
    fn empty_active_registry_allows_checks() {
        let _g = test_lock();
        clear();
        assert!(!is_active());
        assert!(check_range(0x1000, 16, false));
    }

    #[test]
    fn local_stack_region_validates() {
        let host = HostPageSize::detect().expect("host page");
        let stack = map_stack(host, 64 * 1024).expect("stack");
        let mut space = AddressSpace::new();
        space.register_borrowed(&stack);
        assert!(space.is_active());
        let base = stack.guest_addr;
        assert!(space.check_range(base, 64, true));
        assert!(space.check_range(base, 64, false));
        assert!(!space.check_range(0, 8, false));
        let outside = base.wrapping_add(u64::try_from(stack.host_len()).unwrap_or(0));
        assert!(!space.check_range(outside, 8, false));
        space.clear();
        drop(stack);
    }

    #[test]
    fn stack_region_validates_via_active() {
        let _g = test_lock();
        clear();
        let host = HostPageSize::detect().expect("host page");
        let stack = map_stack(host, 64 * 1024).expect("stack");
        register_borrowed(&stack);
        assert!(is_active());
        let base = stack.guest_addr;
        assert!(check_range(base, 64, true));
        assert!(check_range(base, 64, false));
        assert!(!check_range(0, 8, false));
        let outside = base.wrapping_add(u64::try_from(stack.host_len()).unwrap_or(0));
        assert!(!check_range(outside, 8, false));
        clear();
        drop(stack);
    }

    #[test]
    fn execute_only_not_writable() {
        let host = HostPageSize::detect().expect("host page");
        let stack = map_stack(host, u64::from(host.bytes())).expect("stack");
        let mut space = AddressSpace::new();
        space.push_raw(RegRegion {
            guest_addr: stack.guest_addr,
            len: stack.host_len(),
            ptr: host_ptr_from_addr(stack.host_addr()),
            prot: VM_PROT_READ | VM_PROT_EXECUTE,
            owned: false,
        });
        assert!(space.check_range(stack.guest_addr, 8, false));
        assert!(!space.check_range(stack.guest_addr, 8, true));
        space.clear();
        drop(stack);
    }

    #[test]
    fn install_active_swaps_spaces() {
        let _g = test_lock();
        clear();
        let host = HostPageSize::detect().expect("host page");
        let stack = map_stack(host, 64 * 1024).expect("stack");
        let mut space = AddressSpace::new();
        space.register_borrowed(&stack);
        let prev = install_active(space);
        assert!(!prev.is_active());
        assert!(is_active());
        assert!(check_range(stack.guest_addr, 8, false));
        let taken = take_active();
        assert!(taken.is_active());
        assert!(!is_active());
        drop(taken);
        drop(stack);
    }
}