From 992ea27bd66baefb76e83d6076089daaedbedbe2 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Tue, 10 Nov 2026 09:57:22 +0000 Subject: [PATCH] perf(compiler): read and write NZCV with MRS and MSR inline --- crates/stela-compiler/src/aarch64/flags.rs | 72 ++++++++++++++++++- crates/stela-compiler/src/block/system.rs | 4 +- crates/stela-compiler/src/x86_64/flags.rs | 65 +++++++++++++++++ .../stela-compiler/src/x86_64/guest_range.rs | 3 + .../src/machine/engine/system_access.rs | 13 ++-- 5 files changed, 151 insertions(+), 6 deletions(-) diff --git a/crates/stela-compiler/src/aarch64/flags.rs b/crates/stela-compiler/src/aarch64/flags.rs index 230f5e63e6..2b583ecc8d 100644 --- a/crates/stela-compiler/src/aarch64/flags.rs +++ b/crates/stela-compiler/src/aarch64/flags.rs @@ -71,11 +71,37 @@ pub enum Operand { }, ArmToExternal, ExternalToArm, + /// MRS Xt, NZCV: the flags in bits 31:28, the rest zero. + Read { + destination: Register, + }, + /// MSR NZCV, Xt: bits 31:28 of Xt; the rest are RES0 and ignored. + Write { + source: Register, + }, +} + +/// MRS Xt, NZCV and MSR NZCV, Xt, with Rt 0. +const MRS_NZCV: u32 = 0xd53b_4200; +const MSR_NZCV: u32 = 0xd51b_4200; + +/// Whether `word` is MRS or MSR of NZCV. NZCV is plain PSTATE at every EL: +/// no control traps or redirects it (Arm `NZCV` accessibility), so these +/// complete inline rather than as System register accesses. +pub fn accesses_nzcv(word: u32) -> bool { + matches!(word & !REGISTER_MASK, MRS_NZCV | MSR_NZCV) } /// CRm's parenthesized zero bits in CFINV/AXFLAG/XAFLAG are ignored. -/// RMIF reads Xn and SETF reads Wn; register 31 is ZR, never SP. +/// RMIF reads Xn and SETF reads Wn; register 31 is ZR, never SP. MRS and +/// MSR of NZCV take Xt, register 31 XZR. pub fn decode(word: u32) -> Option { + let rt = register(word & REGISTER_MASK, false); + match word & !REGISTER_MASK { + MRS_NZCV => return Some(Operand::Read { destination: rt }), + MSR_NZCV => return Some(Operand::Write { source: rt }), + _ => {} + } match word & PSTATE_MASK { CFINV => return Some(Operand::InvertCarry), AXFLAG => return Some(Operand::ArmToExternal), @@ -107,6 +133,50 @@ pub fn decode(word: u32) -> Option { mod tests { use super::*; + #[test] + fn nzcv_register_accesses_decode_for_every_register_and_nothing_near() { + for rt in 0..=REGISTER_MASK { + let expected = register(rt, false); + assert_eq!( + decode(MRS_NZCV | rt), + Some(Operand::Read { + destination: expected + }) + ); + assert_eq!( + decode(MSR_NZCV | rt), + Some(Operand::Write { source: expected }) + ); + assert!(accesses_nzcv(MRS_NZCV | rt) && accesses_nzcv(MSR_NZCV | rt)); + } + assert_eq!( + decode(MRS_NZCV | 31), + Some(Operand::Read { + destination: Register::Zero + }) + ); + // DAIF, FPCR, FPSR, SYS and MSR (immediate) neighbors stay out. + for word in [ + 0xd53b_4220, + 0xd53b_4400, + 0xd51b_4420, + 0xd50b_4200, + 0xd503_421f, + ] { + assert_eq!(decode(word), None, "{word:#010x}"); + assert!(!accesses_nzcv(word), "{word:#010x}"); + } + let system = crate::block::decode_system_mode_instruction; + for word in [MRS_NZCV | 7, MSR_NZCV | 7] { + assert_eq!(system(crate::GuestAddress(0x1000), word).unwrap(), None); + } + assert!( + system(crate::GuestAddress(0x1000), 0xd53b_4227) + .unwrap() + .is_some() + ); + } + #[test] fn every_condition_matches_the_architectural_truth_table() { // Bit k describes the condition at NZCV nibble k, independently of the diff --git a/crates/stela-compiler/src/block/system.rs b/crates/stela-compiler/src/block/system.rs index e685a1f62d..56e9b81a2f 100644 --- a/crates/stela-compiler/src/block/system.rs +++ b/crates/stela-compiler/src/block/system.rs @@ -363,9 +363,11 @@ fn decode_system_class( }), _ => None, }; + // MRS and MSR of NZCV complete inline (`aarch64::flags`). + let register = registers::decode(word).filter(|_| !crate::aarch64::flags::accesses_nzcv(word)); let mut operations = [ terminal_sync, - registers::decode(word).map(SystemOperation::Register), + register.map(SystemOperation::Register), registers::decode_pstate_immediate(word).map(SystemOperation::PstateImmediate), ] .into_iter() diff --git a/crates/stela-compiler/src/x86_64/flags.rs b/crates/stela-compiler/src/x86_64/flags.rs index 17128abe54..eeb8b0bf1d 100644 --- a/crates/stela-compiler/src/x86_64/flags.rs +++ b/crates/stela-compiler/src/x86_64/flags.rs @@ -23,6 +23,8 @@ const ZERO: u32 = 1 << ZERO_BIT; const CARRY: u32 = 1 << CARRY_BIT; const OVERFLOW: u32 = 1 << OVERFLOW_BIT; const CARRY_MASK: u32 = CARRY << NZCV_SHIFT; +/// NZCV's bits of the register MRS and MSR NZCV access. +const FLAGS_MASK: u32 = (NEGATIVE | ZERO | CARRY | OVERFLOW) << NZCV_SHIFT; const NZCV_NIBBLE: u8 = (NEGATIVE | ZERO | CARRY | OVERFLOW) as u8; const TABLE_ENTRY_BITS: u32 = 4; const TABLE_STRIDE_SHIFT: u8 = 2; @@ -46,6 +48,20 @@ const EXTERNAL_TO_ARM_TABLE: u32 = NEGATIVE pub(super) fn emit(code: &mut Code, operand: &Operand) { match *operand { + Operand::Read { destination } => { + { + let mut asm = code.asm(); + asm.mov_load(Dword, Rax, nzcv()); + asm.alu_accumulator_imm32(Alu::And, Dword, FLAGS_MASK as i32); + } + super::store(code, destination); + return; + } + Operand::Write { source } => { + load(code, source); + code.asm() + .alu_accumulator_imm32(Alu::And, Dword, FLAGS_MASK as i32); + } Operand::InvertCarry => { code.asm() .alu_imm32(Alu::Xor, Dword, nzcv(), CARRY_MASK as i32); @@ -176,6 +192,55 @@ mod tests { } } + /// MRS and MSR of NZCV complete inside the block in system mode and + /// ordinary code alike, in pinned and frame code. + #[cfg(all(target_arch = "x86_64", any(target_os = "macos", target_os = "linux")))] + #[test] + fn nzcv_reads_and_writes_complete_inline_in_every_profile() { + use crate::{ + GuestAddress, Options, TargetCpu, + block::{BlockProfile, Features}, + }; + // mrs x3, nzcv; msr nzcv, x4; mrs x5, nzcv; mrs xzr, nzcv; msr nzcv, x6 + const WORDS: [u32; 5] = [ + 0xd53b_4203, + 0xd51b_4204, + 0xd53b_4205, + 0xd53b_421f, + 0xd51b_4206, + ]; + let source: alloc::vec::Vec = + WORDS.iter().flat_map(|word| word.to_le_bytes()).collect(); + let base = GuestAddress(0x4000_0000); + for (profile, pinned) in [ + (BlockProfile::new(Features::SYSTEM_MODE), true), + (BlockProfile::ORDINARY, false), + ] { + let options = Options::new(profile, TargetCpu::Baseline); + let block = crate::compile(&source, base, &options).unwrap(); + // No System exit ends the block early. + assert_eq!(block.source_bytes, source.len()); + let mut state = GuestState { + nzcv: 0x6000_0000, + ..GuestState::default() + }; + state.x[3] = u64::MAX; + state.x[4] = 0xffff_ffff_9fff_ffff; + state.x[5] = u64::MAX; + // Bits 31:28 clear, the rest set: RES0 bits are ignored. + state.x[6] = 0x0fff_ffff; + let code = &block.native.x86_64_code; + if pinned { + crate::x86_64::native::run(&crate::x86_64::pinned::entered(code), &mut state); + } else { + crate::x86_64::native::run(code, &mut state); + } + assert_eq!(state.x[3], 0x6000_0000, "pinned {pinned}"); + assert_eq!(state.x[5], 0x9000_0000, "pinned {pinned}"); + assert_eq!(state.nzcv, 0, "pinned {pinned}"); + } + } + #[test] fn zero_rotate_mask_keeps_a_nonempty_mapping_interval() { let mut code = Code::new(); diff --git a/crates/stela-compiler/src/x86_64/guest_range.rs b/crates/stela-compiler/src/x86_64/guest_range.rs index 855ba7c38e..9abe189ac4 100644 --- a/crates/stela-compiler/src/x86_64/guest_range.rs +++ b/crates/stela-compiler/src/x86_64/guest_range.rs @@ -173,6 +173,9 @@ fn after(operation: &Operation, bases: u32) -> u32 { Operation::IntegerUnary(operand) => bases & !bit(operand.destination), Operation::IntegerMinMax(operand) => bases & !bit(operand.destination), Operation::Crc(operand) => bases & !bit(operand.destination), + Operation::Flags(crate::aarch64::flags::Operand::Read { destination }) => { + bases & !bit(*destination) + } // NZCV, barriers and hints only; the block a branch leaves is over. Operation::Flags(_) | Operation::BranchCondition { .. } diff --git a/crates/stela-engine/src/machine/engine/system_access.rs b/crates/stela-engine/src/machine/engine/system_access.rs index fc280a3861..e5a16e5d45 100644 --- a/crates/stela-engine/src/machine/engine/system_access.rs +++ b/crates/stela-engine/src/machine/engine/system_access.rs @@ -31,10 +31,15 @@ impl DecodeCache { { return Some(operation); } - let decoded = block::decode_system_mode_instruction(GuestAddress(pc), word) - .ok() - .flatten()? - .operation; + let decoded = match block::decode_system_mode_instruction(GuestAddress(pc), word) { + Ok(Some(instruction)) => instruction.operation, + // Compiled code completes MRS and MSR of NZCV inline + // (`aarch64::flags`); the engine still completes one it is given. + Ok(None) if stela_compiler::aarch64::flags::accesses_nzcv(word) => { + SystemOperation::Register(decode_register(word)?) + } + _ => return None, + }; *entry = Some((word, decoded)); Some(decoded) }