From 485e581cb703c2bd8b7b05eef0dfd6e6743c9348 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:56:26 -0500 Subject: [PATCH] perf(engine): complete SPRR_PERM_EL0 and SPRR_PERM_EL1 writes of the held value natively, and forget no derived state for them in the engine --- .../src/machine/engine/compilation.rs | 6 + .../src/machine/engine/mirrors.rs | 25 ++- .../src/machine/engine/system_access.rs | 31 +++- .../stela-engine/src/machine/engine/tests.rs | 1 + .../machine/engine/tests/permission_kept.rs | 156 ++++++++++++++++++ crates/stela-engine/src/machine/registers.rs | 7 +- 6 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 crates/stela-engine/src/machine/engine/tests/permission_kept.rs diff --git a/crates/stela-engine/src/machine/engine/compilation.rs b/crates/stela-engine/src/machine/engine/compilation.rs index 1e2d435c01..b54aec0b67 100644 --- a/crates/stela-engine/src/machine/engine/compilation.rs +++ b/crates/stela-engine/src/machine/engine/compilation.rs @@ -1367,6 +1367,12 @@ impl Machine { let cell = cell_place(TABLE_BASE_KEPT); let written = self.register_fixed_bits(SystemRegister::TTBR0_EL1); register_read::unchanged_write_thunk(GuestAddress(pc), rt, cell, written) + } else if let Some((kept, rt)) = permission_write_word(word) { + let all = register_read::Written { + keep: u64::MAX, + set: 0, + }; + register_read::unchanged_write_thunk(GuestAddress(pc), rt, cell_place(kept), all) } else if let Some((index, rt)) = guarded_written_register(pc, word) { let cell = cell_place(index); let all = register_read::Written { diff --git a/crates/stela-engine/src/machine/engine/mirrors.rs b/crates/stela-engine/src/machine/engine/mirrors.rs index 451a820070..78a98ee71e 100644 --- a/crates/stela-engine/src/machine/engine/mirrors.rs +++ b/crates/stela-engine/src/machine/engine/mirrors.rs @@ -227,8 +227,31 @@ pub(super) const CYCLES_PLAIN: usize = TIMER_CONTROL + 1; /// Where native code finds [`Mirrors::cycles`]. pub(super) const CYCLES: Place = Place::new(Anchor::Mirrors, offset_of!(Mirrors, cycles)); +/// The cells of MSR SPRR_PERM_EL0 and MSR SPRR_PERM_EL1 +/// (`x86_64::register_read::unchanged_write_thunk`), open on the storage a +/// plain write reached in the state: a write of the value the register +/// holds completes natively and changes nothing (no access changes, as the +/// engine's own write of it finds), any other exits. A kernel writes the +/// user permissions at each return to a thread and the kernel ones around +/// each write of protected data, mostly with the value they hold. A write +/// whose model refused it closes them ([`Machine::register_refused`]). +pub(super) const PERMISSION_KEPT: [usize; 2] = [CYCLES_PLAIN + 1, CYCLES_PLAIN + 2]; + +/// The [`PERMISSION_KEPT`] cell of the MSR `word`, and Rt, if it writes +/// SPRR_PERM_EL0 or SPRR_PERM_EL1. +pub(super) fn permission_write_word(word: u32) -> Option<(usize, u8)> { + const MSR_SPRR_PERM_EL0: u32 = 0xd51e_f1a0; + const MSR_SPRR_PERM_EL1: u32 = 0xd51e_f1c0; + let kept = match word & !31 { + MSR_SPRR_PERM_EL0 => PERMISSION_KEPT[0], + MSR_SPRR_PERM_EL1 => PERMISSION_KEPT[1], + _ => return None, + }; + Some((kept, (word & 31) as u8)) +} + /// The cell of the first of the [`STORED_WRITES`]. -const STORED_WRITE_CELLS: usize = CYCLES_PLAIN + 1; +const STORED_WRITE_CELLS: usize = PERMISSION_KEPT[1] + 1; /// The IMPLEMENTATION DEFINED registers that a store completes writes of /// that get a cell of their own the first time code naming them is installed diff --git a/crates/stela-engine/src/machine/engine/system_access.rs b/crates/stela-engine/src/machine/engine/system_access.rs index e1a4243f5c..1e98f63ae0 100644 --- a/crates/stela-engine/src/machine/engine/system_access.rs +++ b/crates/stela-engine/src/machine/engine/system_access.rs @@ -674,7 +674,11 @@ impl Machine { .collect(); let before = self.completion_point(); let step = self.register_access(word, access); - if step.is_err() || reopen.is_empty() || self.position() != before { + let plain = step.is_ok() && self.position() == before; + if plain { + self.keep_permission(word, access); + } + if !plain || reopen.is_empty() { return step; } let read = decode_register(word | MRS_READ).expect("MSR has an MRS form"); @@ -702,10 +706,31 @@ impl Machine { for (bank, cell) in self.mirrors.banks.iter_mut().zip(cells) { bank[SPRR_PERM_MIRROR] = cell; } + self.keep_permission(word, access); } step } + /// After a plain MSR SPRR_PERM_EL0 or SPRR_PERM_EL1: open its + /// [`PERMISSION_KEPT`] cell on the storage the write reached in the + /// state, which a read there returns, so a later write of the value it + /// holds completes natively. + fn keep_permission(&mut self, word: u32, access: RegisterAccess) { + let Some((kept, _)) = permission_write_word(word) else { + return; + }; + if self + .cpu + .degraded + .contains_key(&access.encoding().selector()) + { + return; + } + if let Some(value) = self.cpu.apple_read_storage(access.encoding()) { + self.mirrors[kept] = register_read::Cell { readable: 1, value }; + } + } + /// The PC, EL, GXF state and exception count of the PE, which an /// instruction that completes without an exception leaves as /// [`Self::completion_point`] gives before it. @@ -1022,6 +1047,10 @@ impl Machine { } else { let value = if rt == 31 { 0 } else { self.cpu.state.x[rt] }; self.cpu.degraded.insert(key, value); + // A read now answers what the write held, not the storage. + for kept in PERMISSION_KEPT { + self.mirrors.close(kept); + } } } self.next() diff --git a/crates/stela-engine/src/machine/engine/tests.rs b/crates/stela-engine/src/machine/engine/tests.rs index 7f591e7278..1e55b05fba 100644 --- a/crates/stela-engine/src/machine/engine/tests.rs +++ b/crates/stela-engine/src/machine/engine/tests.rs @@ -9275,6 +9275,7 @@ mod exits; mod floating_point_traps; mod native_exceptions; mod one_sided; +mod permission_kept; mod process_switching; mod pstate_features; mod recovery; diff --git a/crates/stela-engine/src/machine/engine/tests/permission_kept.rs b/crates/stela-engine/src/machine/engine/tests/permission_kept.rs new file mode 100644 index 0000000000..42ed69a33e --- /dev/null +++ b/crates/stela-engine/src/machine/engine/tests/permission_kept.rs @@ -0,0 +1,156 @@ +//! Native MSR SPRR_PERM_EL0 and SPRR_PERM_EL1 of the value the register +//! holds (`register_read::unchanged_write_thunk` on [`PERMISSION_KEPT`]) +//! against the engine path: the engine's write of the held value changes +//! nothing derived, so the native one, which changes nothing, leaves the +//! machine as the engine's does; another value exits to the engine. + +use super::*; + +const MSR_EL0: u32 = 0xd51e_f1a8; // msr sprr_perm_el0, x8 +const MSR_EL1: u32 = 0xd51e_f1c8; // msr sprr_perm_el1, x8 + +/// A machine at `el` with SPRR enabled whose `word` cell a first engine +/// path write of `first` opened, on the storage the write reached. +fn opened(el: u8, word: u32, first: u64) -> Machine { + let mut machine = machine(&[word]); + machine.cpu.pstate = Pstate::reset(el); + machine.cpu.apple.sprr_config |= mmu::SPRR_CONFIG_EN; + machine.derived_changed(); + let base = machine.cpu.pc; + machine.cpu.state.x[8] = first; + machine.system_instruction(word).unwrap(); + assert_eq!(machine.cpu.pc, base + 4, "{word:#x} completed plainly"); + let (kept, rt) = permission_write_word(word).unwrap(); + assert_eq!(rt, 8); + let cell = machine.mirrors[kept]; + assert_eq!(cell.readable, 1, "a plain write opens the cell"); + let encoding = decode_register(word).unwrap().encoding(); + assert_eq!(Some(cell.value), machine.cpu.apple_read_storage(encoding)); + machine +} + +fn observed(machine: &Machine) -> alloc::string::String { + let open: Vec> = machine + .mirrors + .banks + .iter() + .map(|cells| cells.iter().map(|cell| cell.readable).collect()) + .collect(); + alloc::format!( + "pc={:#x} perm={:x?} x={:?} open={open:?} regimes={} generation={} changes={}", + machine.cpu.pc, + machine.cpu.apple.sprr_perm, + machine.cpu.state.x, + machine.host.regimes.len(), + machine.translation_generation, + machine.statistics.translation_changes, + ) +} + +#[cfg(all(target_arch = "x86_64", any(target_os = "macos", target_os = "linux")))] +#[test] +fn native_unchanged_permission_writes_match_the_engine_path() { + use super::super::transitions::tests::run_native; + const FIRST: u64 = 0x2010_0000_3300_0000; + const OTHER: u64 = 0x2010_0000_7700_0000; + for word in [MSR_EL0, MSR_EL1] { + for el in [1, 2] { + let (mut engine, mut native) = (opened(el, word, FIRST), opened(el, word, FIRST)); + // What native entry derives first, which closes cells. + engine.native_system(); + native.native_system(); + for machine in [&mut engine, &mut native] { + machine.cpu.state.x[8] = FIRST; + machine.cpu.pc = machine.cpu.pc.wrapping_sub(4); + machine.system_instruction(word).unwrap(); + } + let pc = engine.cpu.pc; + let (kept, _) = permission_write_word(word).unwrap(); + let all = register_read::Written { + keep: u64::MAX, + set: 0, + }; + let thunk = + register_read::unchanged_write_thunk(GuestAddress(pc), 8, cell_place(kept), all); + assert_eq!(observed(&engine), observed(&native)); + let mut exits = 0; + for value in [FIRST, FIRST, OTHER, OTHER, FIRST] { + for machine in [&mut engine, &mut native] { + machine.cpu.pc = pc; + machine.cpu.state.x[8] = value; + } + let storage = native.mirrors[kept].value as *const u64; + // SAFETY: the open cell's value is the address of the + // register's storage in the machine, which lives and is not + // written now. + let held = unsafe { storage.read() }; + engine.system_instruction(word).unwrap(); + let (next, kind) = run_native(&mut native, &thunk.code); + if value == held { + assert_eq!((next, kind), (pc + 4, ExitKind::Fallthrough as u64)); + native.cpu.pc = next; + } else { + assert_eq!((next, kind), (pc, ExitKind::System as u64), "{value:#x}"); + native.system_instruction(word).unwrap(); + exits += 1; + } + assert_eq!( + observed(&engine), + observed(&native), + "{word:#x} EL{el} {value:#x}" + ); + } + assert_eq!(exits, 2, "{word:#x} EL{el}: only the two changes exit"); + } + } +} + +/// A change still forgets what derived from the permissions; the held +/// value forgets nothing; a refused write closes the cells; the block's +/// exit gets the thunk. +#[test] +fn permission_writes_forget_derived_state_only_when_they_change() { + let mut machine = opened(2, MSR_EL1, 5); + let generation = machine.translation_generation; + let pc = machine.cpu.pc - 4; + machine.cpu.pc = pc; + machine.system_instruction(MSR_EL1).unwrap(); + assert_eq!(machine.translation_generation, generation, "held value"); + machine.cpu.pc = pc; + machine.cpu.state.x[8] = 6; + machine.system_instruction(MSR_EL1).unwrap(); + assert_ne!(machine.translation_generation, generation, "a change"); + machine.cpu.pc = pc; + machine + .register_refused(MSR_EL1, decode_register(MSR_EL1), Refusal::Unimplemented) + .unwrap(); + assert!( + PERMISSION_KEPT + .iter() + .all(|&kept| machine.mirrors[kept].readable == 0) + ); + + let mut machine = machine_with_permission_block(); + let base = machine.cpu.pc; + machine.host.exits = vec![NativeExit::Exit { + next_pc: base + 8, + kind: ExitKind::Fallthrough as u64, + }]; + machine.step().unwrap(); + let all = register_read::Written { + keep: u64::MAX, + set: 0, + }; + let thunk = register_read::unchanged_write_thunk( + GuestAddress(base + 4), + 8, + cell_place(PERMISSION_KEPT[0]), + all, + ); + assert_eq!(machine.host.installed.last(), Some(&thunk.code)); +} + +fn machine_with_permission_block() -> Machine { + const ADD: u32 = 0x9100_0400; // add x0, x0, #1 + machine(&[ADD, MSR_EL0, ADD]) +} diff --git a/crates/stela-engine/src/machine/registers.rs b/crates/stela-engine/src/machine/registers.rs index 116a75a5d3..2d9c14cb53 100644 --- a/crates/stela-engine/src/machine/registers.rs +++ b/crates/stela-engine/src/machine/registers.rs @@ -1309,7 +1309,12 @@ impl Apple { ); } apple.sprr_perm[index] = value; - changes.permissions = true; + // The value the register held changes no access. + if changed == 0 { + changes.stored = true; + } else { + changes.permissions = true; + } } // The fields gate later SPRR_PERM writes, which the engine // completes, and change no access (`mmu::sprr_unlocked`): nothing