From 78c24992d06f27955a723215bc1c90d9aff27d45 Mon Sep 17 00:00:00 2001 From: Theo Paris Date: Wed, 7 Oct 2026 20:16:48 -0700 Subject: [PATCH 1/2] feat(emulator): add apple m1 --- README.md | 32 + src/emulator/cli/src/boot.rs | 25 +- src/emulator/src/aarch64/compile.rs | 11 +- src/emulator/src/aarch64/cpu.rs | 11 + src/emulator/src/aarch64/decode.rs | 67 +- src/emulator/src/aarch64/exception.rs | 8 +- src/emulator/src/aarch64/identification.rs | 12 +- src/emulator/src/aarch64/translate.rs | 47 +- src/emulator/src/devices/aic.rs | 470 ++++++++++++ src/emulator/src/devices/mod.rs | 3 + src/emulator/src/devices/pmgr.rs | 93 +++ src/emulator/src/devices/s5l_uart.rs | 358 ++++++++++ src/emulator/src/system/boot.rs | 32 + src/emulator/src/system/dtb.rs | 794 +++++++++++++++++++++ src/emulator/src/system/m1.rs | 216 ++++++ src/emulator/src/system/machine.rs | 119 ++- src/emulator/src/system/mod.rs | 247 +++++-- 17 files changed, 2402 insertions(+), 143 deletions(-) create mode 100644 src/emulator/src/devices/aic.rs create mode 100644 src/emulator/src/devices/pmgr.rs create mode 100644 src/emulator/src/devices/s5l_uart.rs create mode 100644 src/emulator/src/system/dtb.rs create mode 100644 src/emulator/src/system/m1.rs diff --git a/README.md b/README.md index 7bab141..3399e49 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,38 @@ enforced by translation, the physical timer is stored but never fires, floating rounds to nearest even and sets no `FPSR` flags, and the GICv3 carries only the virtual timer (no SPIs, SGIs or group 1). +## Apple M1 (t8103) SoC + +`--dtb` selects the M1 machine in place of the QEMU `virt` board. The device tree is +the Asahi kernel's board tree, which the kernel tree builds as +`arch/arm64/boot/dts/apple/t8103-j274.dtb`: + +```sh +cargo run --release --bin volt-boot -- \ + ~/src/linux/arch/arm64/boot/Image 60 \ + --dtb ~/src/linux/arch/arm64/boot/dts/apple/t8103-j274.dtb +``` + +The SoC addresses come from the M1 Mac mini's own device tree (`DeviceTree.j274ap` +in a macOS restore image; `ipsw dtree` reads it). DRAM is at `0x8_0000_0000`, the +interrupt controller (AIC) at `0x2_3b10_0000`, the power-state blocks at +`0x2_3b70_0000` and `0x2_3d28_0000`, and the console (`apple,s5l-uart`, `stdout-path`) +at `0x2_3520_0000`. The machine is one vCPU with the M1 Icestorm identity +(`MIDR_EL1` 0x610f0220) and a 16 KiB granule, which the Asahi kernel requires. + +The Linux kernel boots through the AIC and the S5L console, probes its power domains, +and panics on the missing root filesystem (`VFS: Unable to mount root fs`), which is +the expected result without one. `--initrd`, `--disk` and `--smp` above 1 are rejected +on this board, and a Mach-O kernel is refused: XNU needs the SoC's own ADT and +firmware interfaces, which are not modelled. + +Not modelled: any arm-io device other than the AIC, the UART and the power-state +blocks. Those addresses read as zero and writes are dropped, and the boot report counts +them as `unmodeled device accesses`, so driver probes of missing hardware (DART, GPU, +SMC, PCIe) fail rather than fault. Secondary cores stay powered off (the device tree +gives them no release address), the physical timer never fires, and fast IPIs are +storage only. + ## Performance Translating a block costs much more than running it once, and a kernel boot runs most diff --git a/src/emulator/cli/src/boot.rs b/src/emulator/cli/src/boot.rs index 8832cc9..9ccd3cd 100644 --- a/src/emulator/cli/src/boot.rs +++ b/src/emulator/cli/src/boot.rs @@ -8,8 +8,8 @@ use std::{ }; use volt_emulator::devices::BlockBackend; use volt_emulator::system::{ - DEFAULT_CMDLINE, DEFAULT_XNU_CMDLINE, IdleMode, RAM_SIZE, SystemConfig, XNU_RAM_SIZE, - smp::SmpConfig, + Board, DEFAULT_CMDLINE, DEFAULT_XNU_CMDLINE, IdleMode, RAM_SIZE, SystemConfig, XNU_RAM_SIZE, + m1, smp::SmpConfig, }; use winnow_args::{Args, ValueEnum}; @@ -36,7 +36,7 @@ impl From for IdleMode { #[derive(Args, Debug)] #[arg( name = "volt-boot", - about = "AArch64 Linux or XNU boot with PL011, GICv2/GICv3, and VirtIO-Blk (Linux)" + about = "AArch64 Linux or XNU boot with PL011, GICv2/GICv3, VirtIO-Blk, or the Apple M1 SoC" )] struct Cli { /// Path to a raw Linux AArch64 Image, or an XNU kernel (Mach-O). @@ -59,6 +59,11 @@ struct Cli { #[arg(short, long, alias = "initramfs")] initrd: Option, + /// Device tree for the Apple M1 (t8103) SoC, such as the Asahi kernel's + /// `t8103-j274.dtb`. Selects that machine in place of the QEMU `virt` board. + #[arg(long)] + dtb: Option, + /// Guest RAM size (e.g. 128M, 256M, 512M, 1G). #[arg(short, long, alias = "ram")] memory: Option, @@ -193,10 +198,18 @@ fn run(cli: Cli) -> Result<(), String> { let seconds = cli.seconds.unwrap_or(20); let image = std::fs::read(&cli.image).map_err(|e| format!("{}: {e}", cli.image.display()))?; + let board = match &cli.dtb { + Some(path) => Board::AppleM1 { + device_tree: std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?, + }, + None => Board::Virt, + }; // A Mach-O image is an XNU kernel; anything else is a raw Linux Image. let xnu = volt_emulator::system::xnu::is_macho(&image); let default_cmdline = if xnu { DEFAULT_XNU_CMDLINE + } else if matches!(board, Board::AppleM1 { .. }) { + m1::DEFAULT_CMDLINE } else if cli.disk.is_some() { "root=/dev/vda rw console=ttyAMA0 earlycon=pl011,0x9000000 nokaslr loglevel=8" } else { @@ -213,6 +226,8 @@ fn run(cli: Cli) -> Result<(), String> { RAM_SIZE.max((initrd.len() * 3 + (128 << 20)).next_power_of_two()) } else if xnu { XNU_RAM_SIZE + } else if matches!(board, Board::AppleM1 { .. }) { + m1::DEFAULT_RAM_SIZE } else { RAM_SIZE }; @@ -227,6 +242,9 @@ fn run(cli: Cli) -> Result<(), String> { None => None, }; + if cli.cpus > 1 && !matches!(board, Board::Virt) { + return Err("the Apple M1 SoC boots one vCPU; pass --smp 1".into()); + } if cli.cpus > 1 { let config = SmpConfig { cpus: cli.cpus, @@ -256,6 +274,7 @@ fn run(cli: Cli) -> Result<(), String> { ram_size, initrd: initrd_data, disk: disk_backend, + board, }; let mut output = std::io::BufWriter::new(std::io::stdout()); let report = volt_emulator::system::boot_system(&image, config, move |byte| { diff --git a/src/emulator/src/aarch64/compile.rs b/src/emulator/src/aarch64/compile.rs index b1365ad..21cab61 100644 --- a/src/emulator/src/aarch64/compile.rs +++ b/src/emulator/src/aarch64/compile.rs @@ -2147,7 +2147,10 @@ pub fn compile_with(guest_pc: u64, bytes: &[u8], inline_memory: bool) -> Result< | Instruction::Dmb | Instruction::Yield => {} Instruction::Wfe => l.trap(pc.wrapping_add(4), Trap::Wfe), - Instruction::Trap => l.trap(pc, Trap::Brk), + Instruction::Brk(imm) => { + l.store(offset_of!(Cpu, address), l.k(l.u64, u64::from(imm))); + l.trap(pc, Trap::Brk) + } Instruction::Isb => l.trap(pc.wrapping_add(4), Trap::Isb), Instruction::Tlbi => l.trap(pc.wrapping_add(4), Trap::Tlbi), Instruction::DcZva(rt) => { @@ -2464,6 +2467,10 @@ fn system_offset(r: SystemRegister) -> Option { Fpcr => offset_of!(CpuSystem, fpcr), Fpsr => offset_of!(CpuSystem, fpsr), Daif => offset_of!(CpuSystem, daif), + Apple(slot) => offset_of!(CpuSystem, apple) + 8 * usize::from(slot), + MidrEl1 => offset_of!(CpuSystem, midr_el1), + IdAa64pfr0El1 => offset_of!(CpuSystem, id_aa64pfr0_el1), + IdAa64mmfr0El1 => offset_of!(CpuSystem, id_aa64mmfr0_el1), _ => return None, }) } @@ -2531,7 +2538,7 @@ fn lower_system(l: &Lower, a: System) { l.store(pan, l.imm(l.u64, B::BitAnd, bit, 1)); } } - RazWi => { + RazWi | AppleZero => { if a.read { l.put(a.rt, l.k(l.u64, 0)); } diff --git a/src/emulator/src/aarch64/cpu.rs b/src/emulator/src/aarch64/cpu.rs index 8e37c64..a2d1b7b 100644 --- a/src/emulator/src/aarch64/cpu.rs +++ b/src/emulator/src/aarch64/cpu.rs @@ -199,6 +199,13 @@ pub struct System { pub icc_igrpen0_el1: u64, pub cntp_ctl_el0: u64, pub actlr_el1: u64, + /// Apple IMP-DEF configuration registers, indexed by `decode::SystemRegister::Apple`. + pub apple: [u64; crate::aarch64::decode::APPLE_SLOTS], + /// `MIDR_EL1`, `ID_AA64PFR0_EL1` and `ID_AA64MMFR0_EL1`: the identity this CPU + /// advertises, set by the board. + pub midr_el1: u64, + pub id_aa64pfr0_el1: u64, + pub id_aa64mmfr0_el1: u64, } impl Default for System { fn default() -> Self { @@ -243,6 +250,10 @@ impl Default for System { icc_igrpen0_el1: 0, cntp_ctl_el0: 0, actlr_el1: 0, + apple: [0; crate::aarch64::decode::APPLE_SLOTS], + midr_el1: crate::aarch64::identification::DEFAULT_MIDR_EL1, + id_aa64pfr0_el1: crate::aarch64::identification::DEFAULT_ID_AA64PFR0_EL1, + id_aa64mmfr0_el1: crate::aarch64::identification::DEFAULT_ID_AA64MMFR0_EL1, } } } diff --git a/src/emulator/src/aarch64/decode.rs b/src/emulator/src/aarch64/decode.rs index 8d10f55..1f445dc 100644 --- a/src/emulator/src/aarch64/decode.rs +++ b/src/emulator/src/aarch64/decode.rs @@ -601,6 +601,11 @@ pub enum SystemRegister { ActlrEl1, Nzcv, RazWi, + /// Read-as-zero, write-ignored: Apple IMP-DEF status registers (`op0` 3). + AppleZero, + /// Apple IMP-DEF configuration register the guest reads back, stored in + /// `System::apple[slot]`. + Apple(u8), } impl SystemRegister { pub fn read_only(self) -> bool { @@ -814,7 +819,7 @@ pub enum Instruction { Sevl, Wfe, Yield, - Trap, + Brk(u16), B(i64), BCond(BranchCondition), Csel(Select), @@ -894,7 +899,7 @@ impl Instruction { | Psci | Wfi | Wfe - | Trap + | Brk(_) | Isb | DcZva(_) | AddressTranslate(_) @@ -1081,6 +1086,48 @@ fn simd_struct(word: u32) -> Result, DecodeErro } Ok(Some((desc, post))) } +/// Number of Apple IMP-DEF configuration registers `System::apple` holds. +pub const APPLE_SLOTS: usize = 21; + +/// The `S3_*` implementation-defined registers the M1 kernel and PMU driver touch +/// at EL1 (`arch/arm64/include/asm/apple_m1_pmu.h`, `irq-apple-aic.c`). Configuration +/// is stored so the guest reads back what it wrote. Status registers (IPI pending, +/// PMU interrupt active, PMU state) read as zero: this model raises no fast IPIs and +/// no PMU interrupts. Anything else falls through to the unsupported path. +fn apple_register(op0: u32, op1: u32, crn: u32, crm: u32, op2: u32) -> Option { + use SystemRegister::*; + if op0 != 3 || crn != 15 { + return None; + } + Some(match (op1, crm, op2) { + (1, 0, 0) => Apple(0), + (1, 1, 0) => Apple(1), + (1, 2, 0) => Apple(2), + (1, 3, 0) => Apple(3), + (1, 4, 0) => Apple(4), + (1, 5, 0) => Apple(5), + (1, 6, 0) => Apple(6), + (1, 13, 0) => AppleZero, + (2, 0, 0) => Apple(7), + (2, 1, 0) => Apple(8), + (2, 2, 0) => Apple(9), + (2, 3, 0) => Apple(10), + (2, 4, 0) => Apple(11), + (2, 5, 0) => Apple(12), + (2, 6, 0) => Apple(13), + (2, 7, 0) => Apple(14), + (2, 9, 0) => Apple(15), + (2, 10, 0) => Apple(16), + (5, 0, 0) => Apple(17), + (5, 0, 1) => Apple(18), + (5, 1, 1) => AppleZero, + (5, 3, 1) => Apple(19), + (7, 0, 4) => Apple(20), + (7, 6, 4) => AppleZero, + _ => return None, + }) +} + fn system_register( op0: u32, op1: u32, @@ -1092,6 +1139,9 @@ fn system_register( if op0 == 2 && op1 == 0 && crn == 0 && (4..=7).contains(&op2) { return Ok(RazWi); } + if let Some(register) = apple_register(op0, op1, crn, crm, op2) { + return Ok(register); + } if op1 == 0 && crn == 4 && op2 == 5 { return Ok(SpSelSet); } @@ -1753,12 +1803,10 @@ pub fn decode(word: u32) -> Result { immediate: ((word >> 8) & 15) as u8, })); } - if word & 0xffe0_f000 == 0xd420_0000 { - return if word & 0x0000_ffe0 == 0x20 { - Ok(Trap) - } else { - Err(UnsupportedInstruction) - }; + // BRK #imm16: opc 001 with LL 00. Its immediate is the whole field, so any value + // decodes; the trap carries it to the exception syndrome. + if word & 0xffe0_001f == 0xd420_0000 { + return Ok(Brk((word >> 5) as u16)); } if word & 0x1f00_0000 == 0x1000_0000 { let page = word & 0x8000_0000 != 0; @@ -2493,7 +2541,8 @@ mod tests { (0xd69f03e0, Eret), (0xd4000001, Svc), (0xd4000002, Psci), - (0xd4200020, Trap), + (0xd4200020, Brk(1)), + (0xd4210000, Brk(0x800)), ] { assert_eq!(decode(word), Ok(expected)); } diff --git a/src/emulator/src/aarch64/exception.rs b/src/emulator/src/aarch64/exception.rs index b0c5260..bc9ad9c 100644 --- a/src/emulator/src/aarch64/exception.rs +++ b/src/emulator/src/aarch64/exception.rs @@ -15,6 +15,8 @@ pub struct Reason { pub ec: u8, pub instruction: bool, pub status: u8, + /// Further ISS bits: a BRK's immediate. Syndromes with a DFSC leave this 0. + pub iss: u32, } impl Default for Reason { fn default() -> Self { @@ -23,6 +25,7 @@ impl Default for Reason { ec: 0, instruction: false, status: status::TRANSLATION, + iss: 0, } } } @@ -30,6 +33,8 @@ pub mod status { pub const TRANSLATION: u8 = 0b000100; pub const PERMISSION: u8 = 0b001100; pub const PERMISSION_FETCH: u8 = 0b001101; + /// Synchronous external abort: the access reached no memory or device. + pub const SYNC_EXTERNAL: u8 = 0b010000; } pub fn take(cpu: &mut Cpu, reason: Reason, fault_address: u64) -> u64 { let from = cpu.system.el; @@ -41,7 +46,8 @@ pub fn take(cpu: &mut Cpu, reason: Reason, fault_address: u64) -> u64 { cpu.system.elr_el1 = cpu.pc; cpu.system.esr_el1 = (u64::from(reason.ec & 63) << 26) | (u64::from(reason.instruction) << 25) - | u64::from(reason.status & 63); + | u64::from(reason.status & 63) + | u64::from(reason.iss & 0x1ff_ffff); cpu.system.far_el1 = fault_address; let group = if from != 1 { 2 diff --git a/src/emulator/src/aarch64/identification.rs b/src/emulator/src/aarch64/identification.rs index 46a7ba2..595e76b 100644 --- a/src/emulator/src/aarch64/identification.rs +++ b/src/emulator/src/aarch64/identification.rs @@ -16,19 +16,21 @@ pub const CCSIDR_L1D: u64 = ccsidr(4, 128, 4); pub const CCSIDR_L1I: u64 = ccsidr(2, 128, 4); /// L2 unified: 1 MiB, 8-way. pub const CCSIDR_L2: u64 = ccsidr(4, 2048, 8); +/// The virt board's `MIDR_EL1`, `ID_AA64PFR0_EL1` and `ID_AA64MMFR0_EL1`. A board +/// may advertise different identity, so `value` leaves these three to the CPU state. +pub const DEFAULT_MIDR_EL1: u64 = 0x410f_d0f0; +pub const DEFAULT_ID_AA64PFR0_EL1: u64 = (0xf << 20) | (0xf << 16) | (1 << 4) | 1; +pub const DEFAULT_ID_AA64MMFR0_EL1: u64 = (0xf << 24) | 2; pub fn value(register: SystemRegister) -> Option { use SystemRegister::*; Some(match register { - MidrEl1 => 0x410f_d0f0, MpidrEl1 => 0x8000_0000, CtrEl0 => { (1 << 31) | (1 << 29) | (1 << 28) | (4 << 24) | (4 << 20) | (4 << 16) | (3 << 14) | 4 } DczidEl0 => 4, CntfrqEl0 => counter_hz, - IdAa64pfr0El1 => (0xf << 20) | (0xf << 16) | (1 << 4) | 1, IdAa64dfr0El1 => 6, - IdAa64mmfr0El1 => (0xf << 24) | 2, IdAa64isar0El1 => 2 << 20, IdAa64isar1El1 => 1 << 20, ClidrEl1 => CLIDR, @@ -47,11 +49,11 @@ mod tests { use SystemRegister::*; assert_eq!(value(CntfrqEl0), Some(62_500_000)); assert_eq!(value(DczidEl0), Some(4)); - let pfr0 = value(IdAa64pfr0El1).unwrap(); + let pfr0 = DEFAULT_ID_AA64PFR0_EL1; assert_eq!(pfr0 & 0xffff, 0x11); assert_eq!((pfr0 >> 16) & 0xff, 0xff); assert_eq!((pfr0 >> 24) & 15, 0); - let mmfr0 = value(IdAa64mmfr0El1).unwrap(); + let mmfr0 = DEFAULT_ID_AA64MMFR0_EL1; assert_eq!(mmfr0 & 15, 2); assert_eq!((mmfr0 >> 24) & 15, 15); assert_eq!((mmfr0 >> 20) & 15, 0); diff --git a/src/emulator/src/aarch64/translate.rs b/src/emulator/src/aarch64/translate.rs index 9c4cedc..abba353 100644 --- a/src/emulator/src/aarch64/translate.rs +++ b/src/emulator/src/aarch64/translate.rs @@ -26,7 +26,6 @@ pub struct Entry { pub generation: u64, pub ap: u8, pub executable: bool, - pub faulted: bool, } #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] struct Context { @@ -52,8 +51,6 @@ pub struct Tlb { pub entries: [Entry; 256], pub generation: u64, context: Option, - /// Some entry caches a translation fault. - has_faulted: bool, } impl Default for Tlb { fn default() -> Self { @@ -61,7 +58,6 @@ impl Default for Tlb { entries: [Entry::default(); 256], generation: 1, context: None, - has_faulted: false, } } } @@ -71,27 +67,10 @@ impl Tlb { } pub fn flush(&mut self) { self.generation = self.generation.wrapping_add(1); - self.has_faulted = false; if self.generation <= 1 { self.entries = [Entry::default(); 256]; } } - /// Drop cached translation faults, keeping every valid translation (and so - /// the generation that code and data fast paths are validated against). - /// A context-synchronization event (ISB) makes earlier page-table writes - /// visible to the walker, which can turn a fault into a mapping. It does - /// not invalidate valid translations; only TLBI does. - pub fn purge_faults(&mut self) { - if !self.has_faulted { - return; - } - self.has_faulted = false; - for entry in &mut self.entries { - if entry.faulted { - *entry = Entry::default(); - } - } - } fn slot(virtual_address: u64) -> usize { ((virtual_address >> 12) & 255) as usize } @@ -166,9 +145,6 @@ pub fn translate( let slot = Tlb::slot(virtual_address); let entry = &tlb.entries[slot]; if entry.generation == tlb.generation && entry.virtual_address == page { - if entry.faulted { - return Err(TranslateError::TranslationFault); - } // A cached entry that denies the access is re-walked below: the // descriptor may have been upgraded since it was cached, which // hardware also resolves by retrying the walk. @@ -176,22 +152,10 @@ pub fn translate( return Ok(entry.physical | (virtual_address & PAGE_MASK)); } } - let found = match walk(cpu, memory, virtual_address, access, upper) { - Ok(found) => found, - Err(fault) => { - // Only access-independent failures may be cached as unmapped. - if fault != TranslateError::PermissionFault { - tlb.entries[slot] = Entry { - virtual_address: page, - generation: tlb.generation, - faulted: true, - ..Entry::default() - }; - tlb.has_faulted = true; - } - return Err(fault); - } - }; + // Faults are not cached. ARMv8 never caches an invalid descriptor, so a mapping + // installed later, without a TLBI or an ISB (as Linux's `ioremap` does), must be + // seen by the next access. + let found = walk(cpu, memory, virtual_address, access, upper)?; tlb.entries[slot] = Entry { virtual_address: page, physical: found.physical & !PAGE_MASK, @@ -509,7 +473,8 @@ mod tests { translate(&mut tlb, &cpu, &mut memory, 0, Access::Read), Err(TranslateError::TranslationFault) ); - assert_eq!(memory.reads.get(), count); + // A fault is not cached: the repeated access walks the tables again. + assert!(memory.reads.get() > count); tlb.flush(); memory.put(0, 0x1013); assert_eq!( diff --git a/src/emulator/src/devices/aic.rs b/src/emulator/src/devices/aic.rs new file mode 100644 index 0000000..2a3e26e --- /dev/null +++ b/src/emulator/src/devices/aic.rs @@ -0,0 +1,470 @@ +//! Apple Interrupt Controller, AIC v1: the interrupt controller of the t8103 +//! (M1) SoC, as the Asahi Linux driver drives it on the `apple,t8103-aic` path. +//! +//! The guest sees 896 level-triggered hardware IRQ lines. Each IRQ has a mask +//! bit, a software trigger ORed with its line, and a target CPU. Each CPU has +//! one event register. Reading it returns the lowest-numbered IRQ that is +//! pending, unmasked and targeted at that CPU, and the read itself masks that +//! IRQ (auto-ack). The driver unmasks it again in its EOI handler. Because the +//! IRQ stays masked until then, a level line that is still high does not +//! re-enter the handler. +//! +//! IPIs on this SoC go through the fast-IPI system registers, which are not +//! part of this device. The driver still writes the MMIO IPI registers during +//! CPU bring-up (ACK, and MASK_SET for the SELF and OTHER bits). Those writes +//! are accepted: the mask registers store state and read it back. SEND, ACK, +//! and the per-CPU SET/CLR triggers are write-only no-ops, since no MMIO IPI is +//! ever sent on this path. +//! +//! Reset state: every IRQ is masked and targeted at CPU 0. The driver unmasks +//! an IRQ when it is requested. A guest never sees an IRQ that has not been +//! explicitly enabled. + +/// Mapping length of the AIC register window (the DT `reg` length). +pub const LEN: u64 = 0x8000; + +/// Hardware IRQ lines the guest sees. `AIC_INFO` reports this value, and the +/// driver's header comment documents 896 level-triggered hardware IRQs. Any IRQ +/// number at or above this is not a real pin: it has no effect on delivery, and +/// its register slots read as zero. +const NR_IRQ: usize = 896; +/// 32-bit words that cover `NR_IRQ` in each per-IRQ bitmap. +const IRQ_WORDS: usize = NR_IRQ / 32; + +const INFO: u64 = 0x0004; +const WHOAMI: u64 = 0x2000; +const EVENT: u64 = 0x2004; +const IPI_SEND: u64 = 0x2008; +const IPI_ACK: u64 = 0x200c; +const IPI_MASK_SET: u64 = 0x2024; +const IPI_MASK_CLR: u64 = 0x2028; + +/// `TARGET_CPU`: one word per IRQ at `0x3000 + 4 * irq`. The word holds the CPU +/// bit(s) that receive the IRQ. The driver writes `BIT(cpu)`. +const TARGET_CPU: u64 = 0x3000; + +/// Per-IRQ bitmap register blocks. The driver lays them out in this order, and +/// each block is 32 words (128 bytes) long, one bit per IRQ. +const SW_SET: u64 = 0x4000; +#[cfg(test)] +const SW_CLR: u64 = 0x4080; +#[cfg(test)] +const MASK_SET: u64 = 0x4100; +#[cfg(test)] +const MASK_CLR: u64 = 0x4180; +const HW_STATE: u64 = 0x4200; +const BITMAP_STRIDE: u64 = 0x80; + +/// Explicit per-CPU IPI views: `0x5000 + cpu * 0x80`, with the registers at +/// fixed offsets within each 128-byte block. +const CPU_IPI_BASE: u64 = 0x5000; +const CPU_IPI_STRIDE: u64 = 0x80; +const CPU_IPI_SET: u64 = 0x08; +const CPU_IPI_CLR: u64 = 0x0c; +const CPU_IPI_MASK_SET: u64 = 0x24; +const CPU_IPI_MASK_CLR: u64 = 0x28; + +/// Event word for an IRQ: type `IRQ` (1) in bits 23:16, IRQ number in 15:0. +const EVENT_TYPE_IRQ: u32 = 1 << 16; + +/// Which per-IRQ bitmap a register offset selects. +#[derive(Clone, Copy)] +enum Bitmap { + SwSet, + SwClr, + MaskSet, + MaskClr, + HwState, +} + +/// MMIO IPI registers, both the default view and the explicit per-CPU view. +#[derive(Clone, Copy)] +enum IpiReg { + Send, + Ack, + Set, + Clr, + MaskSet, + MaskClr, +} + +/// A decoded, word-aligned register offset. +#[derive(Clone, Copy)] +enum Reg { + Info, + WhoAmI, + Event, + Target(usize), + Bitmap(Bitmap, usize), + Ipi { cpu: u32, reg: IpiReg }, + Reserved, +} + +/// Apple Interrupt Controller (AIC v1) state for `cpus` CPUs. +pub struct Aic { + cpus: u32, + /// Hardware line state, one bit per IRQ. + hw: [u32; IRQ_WORDS], + /// Software trigger state (`SW_SET`/`SW_CLR`), one bit per IRQ. + sw: [u32; IRQ_WORDS], + /// Mask state (`MASK_SET`/`MASK_CLR`), one bit per IRQ. A set bit masks. + mask: [u32; IRQ_WORDS], + /// CPU routing per IRQ (`TARGET_CPU`). Only the first `NR_IRQ` entries are + /// meaningful. + target: [u32; NR_IRQ], + /// Per-CPU IPI mask state, indexed by CPU. Only the first `cpus` entries + /// are reachable. + ipi_mask: [u32; 32], +} + +/// Mask of the low `n` bytes (`n` in 1..=4) of a 32-bit word. +fn byte_mask(n: u32) -> u32 { + if n >= 4 { + u32::MAX + } else { + (1u32 << (n * 8)) - 1 + } +} + +impl Aic { + /// `cpus` = number of CPUs wired to the IRQ pins (affects per-CPU IPI registers and target masks). + /// + /// Panics if `cpus` is outside `1..=32`, since target registers are 32-bit CPU masks. + pub fn new(cpus: u32) -> Self { + assert!((1..=32).contains(&cpus), "AIC routes to 1..=32 CPUs"); + Self { + cpus, + hw: [0; IRQ_WORDS], + sw: [0; IRQ_WORDS], + mask: [u32::MAX; IRQ_WORDS], + target: [1; NR_IRQ], + ipi_mask: [u32::MAX; 32], + } + } + + /// Drive hardware IRQ `irq` (level-triggered source line). + /// + /// IRQ numbers at or above `NR_IRQ` have no pin and are ignored. + pub fn set_irq(&mut self, irq: u32, asserted: bool) { + let irq = irq as usize; + if irq >= NR_IRQ { + return; + } + let (w, bit) = (irq / 32, 1u32 << (irq % 32)); + if asserted { + self.hw[w] |= bit; + } else { + self.hw[w] &= !bit; + } + } + + /// Whether the IRQ pin of CPU `cpu` is asserted right now (an enabled, unmasked, targeted source is pending). + pub fn irq_pending(&self, cpu: u32) -> bool { + self.first_pending(cpu).is_some() + } + + /// MMIO read at `offset` from the AIC base, performed by CPU `cpu` (AIC_WHOAMI and AIC_EVENT are per reading CPU). Takes &mut self because reading AIC_EVENT has side effects. + /// + /// `size` is 1, 2, 4 or 8 bytes. Accesses that cross a 32-bit register are + /// split per register. Unmapped offsets read as zero. + pub fn read(&mut self, cpu: u32, offset: u64, size: u8) -> u64 { + let size = u64::from(size.min(8)); + let mut value = 0u64; + let mut done = 0u64; + while done < size { + let addr = offset.wrapping_add(done); + let lane = (addr & 3) as u32; + let n = (4 - u64::from(lane)).min(size - done) as u32; + let word = self.read_word(cpu, addr & !3); + let bytes = u64::from((word >> (lane * 8)) & byte_mask(n)); + value |= bytes << (done * 8); + done += u64::from(n); + } + value + } + + /// MMIO write. Only the low `size` bytes of `value` are written. Writes to + /// set/clear registers only affect the bits they name. Writes to read-only + /// or unmapped offsets are ignored. + pub fn write(&mut self, cpu: u32, offset: u64, size: u8, value: u64) { + let size = u64::from(size.min(8)); + let mut done = 0u64; + while done < size { + let addr = offset.wrapping_add(done); + let lane = (addr & 3) as u32; + let n = (4 - u64::from(lane)).min(size - done) as u32; + let lanes = byte_mask(n) << (lane * 8); + let bits = (((value >> (done * 8)) as u32) << (lane * 8)) & lanes; + self.write_word(cpu, addr & !3, bits, lanes); + done += u64::from(n); + } + } + + /// Lowest IRQ that is pending, unmasked, and targeted at `cpu`. A CPU index + /// of 32 or more has no routing bit, so it never matches. + fn first_pending(&self, cpu: u32) -> Option { + let bit = 1u32.checked_shl(cpu).unwrap_or(0); + for w in 0..IRQ_WORDS { + let mut candidates = (self.hw[w] | self.sw[w]) & !self.mask[w]; + while candidates != 0 { + let irq = w * 32 + candidates.trailing_zeros() as usize; + if self.target[irq] & bit != 0 { + return Some(irq); + } + candidates &= candidates - 1; + } + } + None + } + + /// Reading AIC_EVENT: deliver the lowest pending IRQ for `cpu` and mask it + /// (auto-ack). Returns 0 when nothing is pending. + fn claim(&mut self, cpu: u32) -> u32 { + let Some(irq) = self.first_pending(cpu) else { + return 0; + }; + self.mask[irq / 32] |= 1 << (irq % 32); + EVENT_TYPE_IRQ | irq as u32 + } + + fn own_ipi(&self, cpu: u32, reg: IpiReg) -> Reg { + if cpu < self.cpus { + Reg::Ipi { cpu, reg } + } else { + Reg::Reserved + } + } + + fn decode(&self, offset: u64, cpu: u32) -> Reg { + match offset { + INFO => Reg::Info, + WHOAMI => Reg::WhoAmI, + EVENT => Reg::Event, + IPI_SEND => self.own_ipi(cpu, IpiReg::Send), + IPI_ACK => self.own_ipi(cpu, IpiReg::Ack), + IPI_MASK_SET => self.own_ipi(cpu, IpiReg::MaskSet), + IPI_MASK_CLR => self.own_ipi(cpu, IpiReg::MaskClr), + o if (TARGET_CPU..SW_SET).contains(&o) => { + let irq = ((o - TARGET_CPU) / 4) as usize; + if irq < NR_IRQ { + Reg::Target(irq) + } else { + Reg::Reserved + } + } + o if (SW_SET..HW_STATE + BITMAP_STRIDE).contains(&o) => { + let block = (o - SW_SET) / BITMAP_STRIDE; + let word = (((o - SW_SET) % BITMAP_STRIDE) / 4) as usize; + let which = match block { + 0 => Bitmap::SwSet, + 1 => Bitmap::SwClr, + 2 => Bitmap::MaskSet, + 3 => Bitmap::MaskClr, + _ => Bitmap::HwState, + }; + if word < IRQ_WORDS { + Reg::Bitmap(which, word) + } else { + Reg::Reserved + } + } + o if (CPU_IPI_BASE..CPU_IPI_BASE + CPU_IPI_STRIDE * u64::from(self.cpus)) + .contains(&o) => + { + let rel = o - CPU_IPI_BASE; + let reg = match rel % CPU_IPI_STRIDE { + CPU_IPI_SET => IpiReg::Set, + CPU_IPI_CLR => IpiReg::Clr, + CPU_IPI_MASK_SET => IpiReg::MaskSet, + CPU_IPI_MASK_CLR => IpiReg::MaskClr, + _ => return Reg::Reserved, + }; + Reg::Ipi { + cpu: (rel / CPU_IPI_STRIDE) as u32, + reg, + } + } + _ => Reg::Reserved, + } + } + + /// Read the 32-bit register at word-aligned `offset`. + fn read_word(&mut self, cpu: u32, offset: u64) -> u32 { + match self.decode(offset, cpu) { + Reg::Info => NR_IRQ as u32, + Reg::WhoAmI => cpu, + Reg::Event => self.claim(cpu), + Reg::Target(irq) => self.target[irq], + Reg::Bitmap(Bitmap::SwSet | Bitmap::SwClr, w) => self.sw[w], + Reg::Bitmap(Bitmap::MaskSet | Bitmap::MaskClr, w) => self.mask[w], + Reg::Bitmap(Bitmap::HwState, w) => self.hw[w], + Reg::Ipi { + cpu: c, + reg: IpiReg::MaskSet | IpiReg::MaskClr, + } => self.ipi_mask[c as usize], + Reg::Ipi { .. } | Reg::Reserved => 0, + } + } + + /// Write the lanes in `lanes` of the 32-bit register at word-aligned + /// `offset`. `bits` holds the written value already shifted into those + /// lanes and zero elsewhere. + fn write_word(&mut self, cpu: u32, offset: u64, bits: u32, lanes: u32) { + match self.decode(offset, cpu) { + Reg::Target(irq) => self.target[irq] = (self.target[irq] & !lanes) | bits, + Reg::Bitmap(Bitmap::SwSet, w) => self.sw[w] |= bits, + Reg::Bitmap(Bitmap::SwClr, w) => self.sw[w] &= !bits, + Reg::Bitmap(Bitmap::MaskSet, w) => self.mask[w] |= bits, + Reg::Bitmap(Bitmap::MaskClr, w) => self.mask[w] &= !bits, + Reg::Ipi { + cpu: c, + reg: IpiReg::MaskSet, + } => self.ipi_mask[c as usize] |= bits, + Reg::Ipi { + cpu: c, + reg: IpiReg::MaskClr, + } => self.ipi_mask[c as usize] &= !bits, + // Read-only registers, write-only IPI triggers, and reserved + // offsets: accepted and ignored. + Reg::Info + | Reg::WhoAmI + | Reg::Event + | Reg::Bitmap(Bitmap::HwState, _) + | Reg::Ipi { .. } + | Reg::Reserved => {} + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Address of the MASK_CLR bit for `irq`. + fn mask_clr(irq: u32) -> u64 { + MASK_CLR + 4 * u64::from(irq / 32) + } + + /// Clear the mask of `irq` on CPU 0, as the driver does when it enables an IRQ. + fn unmask(aic: &mut Aic, irq: u32) { + aic.write(0, mask_clr(irq), 4, 1u64 << (irq % 32)); + } + + fn event(aic: &mut Aic, cpu: u32) -> u64 { + aic.read(cpu, EVENT, 4) + } + + #[test] + fn event_returns_lowest_pending_irq_and_auto_masks() { + let mut aic = Aic::new(1); + unmask(&mut aic, 40); + unmask(&mut aic, 5); + aic.set_irq(40, true); + aic.set_irq(5, true); + + assert_eq!(event(&mut aic, 0), u64::from(EVENT_TYPE_IRQ | 5)); + // IRQ 5 was auto-masked by the read, so IRQ 40 is next. + assert!(aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), u64::from(EVENT_TYPE_IRQ | 40)); + assert!(!aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), 0); + } + + #[test] + fn masked_irq_does_not_assert_pin_until_unmasked() { + let mut aic = Aic::new(1); + aic.set_irq(3, true); + assert!(!aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), 0); + + unmask(&mut aic, 3); + assert!(aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), u64::from(EVENT_TYPE_IRQ | 3)); + } + + #[test] + fn reasserting_while_auto_masked_does_not_raise_pin() { + let mut aic = Aic::new(1); + unmask(&mut aic, 3); + aic.set_irq(3, true); + assert_eq!(event(&mut aic, 0), u64::from(EVENT_TYPE_IRQ | 3)); + + // The line drops and rises again while the IRQ is auto-masked. + aic.set_irq(3, false); + aic.set_irq(3, true); + assert!(!aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), 0); + } + + #[test] + fn mask_clr_reenables_and_pin_rises_again() { + let mut aic = Aic::new(1); + unmask(&mut aic, 3); + aic.set_irq(3, true); + event(&mut aic, 0); + assert!(!aic.irq_pending(0)); + + // The driver's EOI unmasks the level-triggered source, which is still high. + unmask(&mut aic, 3); + assert!(aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), u64::from(EVENT_TYPE_IRQ | 3)); + } + + #[test] + fn target_register_routes_irq_away_from_reading_cpu() { + let mut aic = Aic::new(2); + unmask(&mut aic, 7); + aic.set_irq(7, true); + // Reset routes every IRQ to CPU 0. + assert!(aic.irq_pending(0)); + + // The driver writes BIT(1) to route IRQ 7 to CPU 1. + aic.write(0, TARGET_CPU + 4 * 7, 4, 1 << 1); + assert!(!aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), 0); + assert!(aic.irq_pending(1)); + assert_eq!(event(&mut aic, 1), u64::from(EVENT_TYPE_IRQ | 7)); + } + + #[test] + fn software_trigger_asserts_without_hardware_line() { + let mut aic = Aic::new(1); + unmask(&mut aic, 40); + // IRQ 40 is bit 8 of the second word of each bitmap block. + aic.write(0, SW_SET + 4, 4, 1 << 8); + assert!(aic.irq_pending(0)); + assert_eq!(event(&mut aic, 0), u64::from(EVENT_TYPE_IRQ | 40)); + + aic.write(0, SW_SET + 4, 4, 1 << 8); + aic.write(0, SW_CLR + 4, 4, 1 << 8); + assert!(!aic.irq_pending(0)); + } + + #[test] + fn whoami_reports_the_reading_cpu() { + let mut aic = Aic::new(2); + assert_eq!(aic.read(0, WHOAMI, 4), 0); + assert_eq!(aic.read(1, WHOAMI, 4), 1); + } + + #[test] + fn unknown_offsets_read_zero_and_ignore_writes() { + let mut aic = Aic::new(1); + // Reserved gap, AIC_CONFIG (not used by the driver on this path), a + // reserved word in the mask block, a target slot past the last IRQ, a + // bitmap block past HW_STATE, and a per-CPU view for a CPU that is absent. + let offsets = [ + 0x0000, + 0x0010, + 0x2010, + MASK_SET + 4 * 28, + TARGET_CPU + 4 * NR_IRQ as u64, + HW_STATE + BITMAP_STRIDE, + CPU_IPI_BASE + CPU_IPI_STRIDE, + ]; + for offset in offsets { + aic.write(0, offset, 4, u64::from(u32::MAX)); + assert_eq!(aic.read(0, offset, 4), 0, "offset {offset:#x}"); + } + } +} diff --git a/src/emulator/src/devices/mod.rs b/src/emulator/src/devices/mod.rs index 72ce939..fa62444 100644 --- a/src/emulator/src/devices/mod.rs +++ b/src/emulator/src/devices/mod.rs @@ -1,7 +1,10 @@ +pub mod aic; pub mod bus; pub mod gicv2; pub mod gicv3; pub mod pl011; +pub mod pmgr; +pub mod s5l_uart; pub mod virtio_blk; pub use bus::{Bus, BusDevice, Device}; diff --git a/src/emulator/src/devices/pmgr.rs b/src/emulator/src/devices/pmgr.rs new file mode 100644 index 0000000..a2cba60 --- /dev/null +++ b/src/emulator/src/devices/pmgr.rs @@ -0,0 +1,93 @@ +//! Apple's power manager (PMGR), as the M1 Linux power-domain driver drives it. +//! +//! Each power domain has one 32-bit power-state register (`apple-pmgr-pwrstate`): +//! `PS_TARGET` in bits 3:0 is the state the driver asks for, and `PS_ACTUAL` in bits +//! 7:4 is the state the hardware is in. The driver writes the target and polls until +//! the actual state matches. This model acknowledges a write at once, so the actual +//! state is the target, and every register starts in the active state iBoot leaves +//! the always-on domains in. Other bits are stored as written. +use alloc::vec; +use alloc::vec::Vec; + +/// `PS_ACTIVE`: the target and the actual state both name the active state. +const ACTIVE: u32 = 0xff; +const TARGET: u32 = 0x0f; +const ACTUAL: u32 = 0xf0; + +/// The power-state registers of one PMGR block of `len` bytes. +pub struct Pmgr { + words: Vec, +} + +impl Pmgr { + /// A block of `len` bytes (a multiple of four) with every power domain active. + pub fn new(len: u64) -> Self { + Self { + words: vec![ACTIVE; (len / 4) as usize], + } + } + + /// Read `size` bytes at `offset` (relative to the block base). + pub fn read(&self, offset: u64, size: u8) -> u64 { + (0..u64::from(size)).fold(0, |value, i| { + let at = offset + i; + let word = self.words[(at / 4) as usize]; + value | u64::from((word >> (8 * (at % 4))) as u8) << (8 * i) + }) + } + + /// Write the low `size` bytes of `value` at `offset`. Each 32-bit register the write + /// touches takes its target power state at once. + pub fn write(&mut self, offset: u64, size: u8, value: u64) { + for i in 0..u64::from(size) { + let at = offset + i; + let shift = 8 * (at % 4); + let byte = ((value >> (8 * i)) & 0xff) as u32; + let word = &mut self.words[(at / 4) as usize]; + *word = (*word & !(0xff << shift)) | byte << shift; + } + let first = (offset / 4) as usize; + let last = ((offset + u64::from(size) - 1) / 4) as usize; + for word in &mut self.words[first..=last] { + *word = (*word & !ACTUAL) | (*word & TARGET) << 4; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const PS: u64 = 0x100; + const LEN: u64 = 0x4000; + + #[test] + fn every_power_domain_starts_active() { + let pmgr = Pmgr::new(LEN); + assert_eq!(pmgr.read(PS, 4) & 0xff, 0xff); + } + + #[test] + fn a_target_is_acknowledged_as_the_actual_state_at_once() { + let mut pmgr = Pmgr::new(LEN); + pmgr.write(PS, 4, 0x0); + let state = pmgr.read(PS, 4); + assert_eq!(state & 0x0f, 0, "target is what was written"); + assert_eq!((state >> 4) & 0x0f, 0, "actual follows the target"); + pmgr.write(PS, 4, 0x0f | 1 << 31); + assert_eq!( + pmgr.read(PS, 4) & 0xff, + 0xff, + "active again, reset bit stored" + ); + } + + #[test] + fn narrow_writes_change_only_their_bytes_and_flags_are_stored() { + let mut pmgr = Pmgr::new(LEN); + // WAS_PWRGATED (bit 8) lives in the second byte: a byte write leaves the rest. + pmgr.write(PS + 1, 1, 0x1); + assert_eq!(pmgr.read(PS + 1, 1), 0x1); + assert_eq!(pmgr.read(PS, 1), 0xff); + } +} diff --git a/src/emulator/src/devices/s5l_uart.rs b/src/emulator/src/devices/s5l_uart.rs new file mode 100644 index 0000000..6566e3b --- /dev/null +++ b/src/emulator/src/devices/s5l_uart.rs @@ -0,0 +1,358 @@ +//! Apple S5L UART (`apple,s5l-uart`), the console port of M1-class SoCs. +//! +//! The Apple variant differs from the classic Samsung register map in one way +//! that matters here: it has no `UINTP`/`UINTM` interrupt registers. Its +//! interrupt causes are the flag bits of `UTRSTAT` itself. A cause is cleared +//! by writing a one to its bit, and a `UCON` enable bit decides whether a +//! pending cause reaches the line. `apple_serial_handle_irq` in the driver +//! works this way and never touches `UINTP`. +//! +//! Transmit completes at once. Every byte written to `UTXH` reaches the sink, +//! and the transmit FIFO never reports full. The one transmit cause is +//! `TXTHRESH` (`UTRSTAT` bit 5). Real hardware latches it when the transmit +//! FIFO drops below its threshold, and in this model every transmit does that, +//! so the cause is raised after each byte. The line rises only while +//! `UCON.TXTHRESH_ENA` is set, which is when the driver wants a TX interrupt. +//! +//! The driver's TX path never waits on that interrupt. `enable_tx_pio` sets the +//! enable bit and calls the TX routine itself, because the Apple UART only +//! raises edge-triggered TX interrupts (see the comment there). That routine +//! drains the queue synchronously, since the FIFO never fills, and then clears +//! the enable bit through `stop_tx`. If an interrupt does arrive while work is +//! left, the handler clears `TXTHRESH` first and then writes more bytes, which +//! raises the cause again, so each entry drains some of the queue. +//! +//! Receive has no input in this model: the receive FIFO is empty, `RXDR` stays +//! clear, and no receive cause is ever raised. + +use alloc::boxed::Box; +use alloc::collections::VecDeque; + +/// Mapping length of the UART register window. +pub const LEN: u64 = 0x1000; + +const ULCON: u64 = 0x00; +const UCON: u64 = 0x04; +const UFCON: u64 = 0x08; +const UMCON: u64 = 0x0c; +const UTRSTAT: u64 = 0x10; +const UFSTAT: u64 = 0x18; +const UTXH: u64 = 0x20; +const URXH: u64 = 0x24; +const UBRDIV: u64 = 0x28; +const UFRACVAL: u64 = 0x2c; + +/// `UCON` bit that enables the transmit interrupt line, `APPLE_S5L_UCON_TXTHRESH_ENA`. +const UCON_TXTHRESH_ENA: u32 = 1 << 13; + +/// `UTRSTAT` bit for the transmit-threshold cause, `APPLE_S5L_UTRSTAT_TXTHRESH`. +const UTRSTAT_TXTHRESH: u32 = 1 << 5; +/// Transmitter empty and transmit FIFO empty. Always set: transmit is instant. +const UTRSTAT_TXE: u32 = 1 << 2; +const UTRSTAT_TXFE: u32 = 1 << 1; +/// Receive data ready, set only while the receive FIFO holds data. +const UTRSTAT_RXDR: u32 = 1 << 0; + +/// `UFCON` bits that reset a FIFO. The hardware clears them itself, so they +/// never read back as set. +const UFCON_RESETRX: u32 = 1 << 1; +const UFCON_RESETTX: u32 = 1 << 2; + +/// `UFSTAT` receive count field and its full flag. +const UFSTAT_RXMASK: u32 = 0xf; +const UFSTAT_RXFULL: u32 = 1 << 8; +/// Receive FIFO depth reported in `UFSTAT`, `fifosize` for this port. +const FIFO_DEPTH: usize = 16; + +/// An Apple S5L UART. +/// +/// The sink takes each transmitted byte in order. The interrupt line is read +/// through [`S5lUart::irq_asserted`]. +pub struct S5lUart { + sink: Box, + ulcon: u32, + ucon: u32, + ufcon: u32, + umcon: u32, + ubrdiv: u32, + ufracval: u32, + /// Pending interrupt causes, in `UTRSTAT` bit positions. + pending: u32, + rx: VecDeque, +} + +impl S5lUart { + /// A UART writing transmitted bytes to `sink`. + pub fn new(sink: impl FnMut(u8) + Send + 'static) -> Self { + Self { + sink: Box::new(sink), + ulcon: 0, + ucon: 0, + ufcon: 0, + umcon: 0, + ubrdiv: 0, + ufracval: 0, + pending: 0, + rx: VecDeque::new(), + } + } + + /// Read a register. `offset` is relative to the device base; `size` is in + /// bytes. Unknown offsets read as zero. + pub fn read(&mut self, offset: u64, size: u8) -> u64 { + let value = match offset { + ULCON => self.ulcon, + UCON => self.ucon, + UFCON => self.ufcon, + UMCON => self.umcon, + UTRSTAT => self.utrstat(), + UFSTAT => self.ufstat(), + URXH => self.rx.pop_front().map_or(0, u32::from), + UBRDIV => self.ubrdiv, + UFRACVAL => self.ufracval, + _ => 0, + }; + u64::from(value) & lane_mask(size) + } + + /// Write a register. Only the low `size` bytes of `value` are used. + /// Unknown offsets and read-only registers ignore the write. + pub fn write(&mut self, offset: u64, size: u8, value: u64) { + let value = (value & lane_mask(size)) as u32; + match offset { + ULCON => self.ulcon = value, + UCON => self.ucon = value, + UFCON => { + if value & UFCON_RESETRX != 0 { + self.rx.clear(); + } + self.ufcon = value & !(UFCON_RESETRX | UFCON_RESETTX); + } + UMCON => self.umcon = value, + // Write-one-to-clear. Bits that are not causes have nothing pending + // to clear, so they fall through harmlessly. + UTRSTAT => self.pending &= !value, + UTXH => self.transmit(value as u8), + UBRDIV => self.ubrdiv = value, + UFRACVAL => self.ufracval = value, + _ => {} + } + } + + /// Level of the interrupt line to the AIC: asserted while the transmit + /// cause is pending and its `UCON` enable bit is set. + pub fn irq_asserted(&self) -> bool { + self.pending & UTRSTAT_TXTHRESH != 0 && self.ucon & UCON_TXTHRESH_ENA != 0 + } + + fn transmit(&mut self, byte: u8) { + (self.sink)(byte); + self.pending |= UTRSTAT_TXTHRESH; + } + + fn utrstat(&self) -> u32 { + let rxdr = if self.rx.is_empty() { 0 } else { UTRSTAT_RXDR }; + self.pending | UTRSTAT_TXE | UTRSTAT_TXFE | rxdr + } + + /// The transmit FIFO never holds anything, so only the receive count shows. + fn ufstat(&self) -> u32 { + let len = self.rx.len(); + let full = if len >= FIFO_DEPTH { UFSTAT_RXFULL } else { 0 }; + full | (len as u32 & UFSTAT_RXMASK) + } + + /// Feed one byte into the receive FIFO, as input from a host would. + #[cfg(test)] + fn feed_rx(&mut self, byte: u8) { + self.rx.push_back(byte); + } +} + +/// Mask for the low `size` bytes of a register access. +fn lane_mask(size: u8) -> u64 { + match size { + 1 => 0xff, + 2 => 0xffff, + 4 => 0xffff_ffff, + _ => u64::MAX, + } +} + +#[cfg(test)] +mod tests { + use super::{ + S5lUart, UBRDIV, UCON, UCON_TXTHRESH_ENA, UFCON, UFRACVAL, UFSTAT, ULCON, UMCON, URXH, + UTRSTAT, UTRSTAT_TXTHRESH, UTXH, + }; + use alloc::sync::Arc; + use core::sync::atomic::{AtomicU8, AtomicUsize, Ordering}; + + const CAPACITY: usize = 512; + const ZERO: AtomicU8 = AtomicU8::new(0); + + /// Bytes the sink received, shared with the test through atomics so the + /// sink stays `Send` without `std`. + struct Capture { + len: AtomicUsize, + bytes: [AtomicU8; CAPACITY], + } + + impl Capture { + fn new() -> Arc { + Arc::new(Self { + len: AtomicUsize::new(0), + bytes: [ZERO; CAPACITY], + }) + } + + fn push(&self, byte: u8) { + let index = self.len.fetch_add(1, Ordering::SeqCst); + self.bytes[index].store(byte, Ordering::SeqCst); + } + + fn collected(&self) -> alloc::vec::Vec { + (0..self.len.load(Ordering::SeqCst)) + .map(|i| self.bytes[i].load(Ordering::SeqCst)) + .collect() + } + } + + fn uart_with_capture() -> (S5lUart, Arc) { + let capture = Capture::new(); + let sink = Arc::clone(&capture); + (S5lUart::new(move |byte| sink.push(byte)), capture) + } + + /// The `UFCON` value the driver's startup writes: FIFO mode, RX trigger 8, + /// and the RX reset bit it ORs in (plus TX reset when not a console). + const UFCON_STARTUP: u32 = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 4); + + #[test] + fn transmitted_bytes_reach_sink_in_order() { + let (mut uart, capture) = uart_with_capture(); + for &byte in b"volt\n" { + uart.write(UTXH, 4, u64::from(byte)); + } + // Only the low byte of the register is the character. + uart.write(UTXH, 4, 0x1_0000_0041); + uart.write(UTXH, 1, 0x142); + assert_eq!(capture.collected(), b"volt\nAB"); + } + + #[test] + fn transmit_fifo_never_reports_full_or_nonempty() { + let (mut uart, _capture) = uart_with_capture(); + for i in 0..200u64 { + uart.write(UTXH, 4, i); + let ufstat = uart.read(UFSTAT, 4); + assert_eq!(ufstat & (1 << 9), 0, "TXFULL set after byte {i}"); + assert_eq!(ufstat & (0xf << 4), 0, "TX count nonzero after byte {i}"); + } + } + + #[test] + fn early_console_sees_room_and_empty_transmitter() { + // `samsung_early_putc` polls TXFULL when UFCON has FIFO mode set, and + // TXFE in UTRSTAT otherwise. The driver's startup sets FIFO mode. + let (mut uart, _capture) = uart_with_capture(); + uart.write(UFCON, 4, u64::from(UFCON_STARTUP)); + uart.write(UTXH, 4, u64::from(b'x')); + assert_eq!(uart.read(UFSTAT, 4) & (1 << 9), 0); + assert_ne!(uart.read(UTRSTAT, 4) & (1 << 1), 0); + // Without FIFO mode the early console polls TXFE only; it must also pass. + uart.write(UFCON, 4, 0); + assert_ne!(uart.read(UTRSTAT, 4) & (1 << 1), 0); + } + + #[test] + fn write_one_to_clear_drops_the_line() { + let (mut uart, _capture) = uart_with_capture(); + uart.write(UCON, 4, u64::from(UCON_TXTHRESH_ENA)); + uart.write(UTXH, 4, u64::from(b'a')); + assert!(uart.irq_asserted()); + assert_ne!(uart.read(UTRSTAT, 4) & u64::from(UTRSTAT_TXTHRESH), 0); + + uart.write(UTRSTAT, 4, u64::from(UTRSTAT_TXTHRESH)); + assert!(!uart.irq_asserted()); + assert_eq!(uart.read(UTRSTAT, 4) & u64::from(UTRSTAT_TXTHRESH), 0); + } + + #[test] + fn enable_bit_masks_the_line_but_not_the_cause() { + let (mut uart, _capture) = uart_with_capture(); + uart.write(UTXH, 4, u64::from(b'a')); + assert!(!uart.irq_asserted(), "no enable bit, no line"); + assert_ne!(uart.read(UTRSTAT, 4) & u64::from(UTRSTAT_TXTHRESH), 0); + + uart.write(UCON, 4, u64::from(UCON_TXTHRESH_ENA)); + assert!(uart.irq_asserted(), "enabling reveals the pending cause"); + } + + #[test] + fn driver_tx_kick_completes_without_waiting_for_an_interrupt() { + // `enable_tx_pio`: set the enable bit, write the queued bytes inline, + // then `stop_tx` clears the enable bit. The line must end released. + let (mut uart, capture) = uart_with_capture(); + uart.write(UCON, 4, u64::from(UCON_TXTHRESH_ENA)); + for &byte in b"boot" { + uart.write(UTXH, 4, u64::from(byte)); + } + uart.write(UCON, 4, 0); + assert!(!uart.irq_asserted()); + assert_eq!(capture.collected(), b"boot"); + + // A later kick with a cause left over from before still makes progress: + // the handler clears the cause, then drains and disables the line. + uart.write(UCON, 4, u64::from(UCON_TXTHRESH_ENA)); + assert!(uart.irq_asserted()); + uart.write(UTRSTAT, 4, u64::from(UTRSTAT_TXTHRESH)); + uart.write(UTXH, 4, u64::from(b'!')); + uart.write(UCON, 4, 0); + assert!(!uart.irq_asserted()); + assert_eq!(capture.collected(), b"boot!"); + } + + #[test] + fn startup_registers_read_back() { + let (mut uart, _capture) = uart_with_capture(); + let ucon = 0x3a85_u32; // APPLE_S5L_UCON_DEFAULT with all four enable bits set + uart.write(ULCON, 4, 0x3); + uart.write(UCON, 4, u64::from(ucon)); + uart.write(UFCON, 4, u64::from(UFCON_STARTUP)); + uart.write(UMCON, 4, 0x10); + uart.write(UBRDIV, 4, 0x1a0); + uart.write(UFRACVAL, 4, 0x7); + + assert_eq!(uart.read(ULCON, 4), 0x3); + assert_eq!(uart.read(UCON, 4), u64::from(ucon)); + // The FIFO reset bits are self-clearing and do not read back. + assert_eq!(uart.read(UFCON, 4), u64::from(UFCON_STARTUP & !0b110)); + assert_eq!(uart.read(UMCON, 4), 0x10); + assert_eq!(uart.read(UBRDIV, 4), 0x1a0); + assert_eq!(uart.read(UFRACVAL, 4), 0x7); + } + + #[test] + fn unknown_offsets_read_zero_and_ignore_writes() { + let (mut uart, _capture) = uart_with_capture(); + uart.write(0x30, 4, 0xffff_ffff); + uart.write(0x38, 4, 0xffff_ffff); + assert_eq!(uart.read(0x30, 4), 0); + assert_eq!(uart.read(0x38, 4), 0); + assert!(!uart.irq_asserted()); + } + + #[test] + fn receive_fifo_drives_data_ready_and_urxh() { + let (mut uart, _capture) = uart_with_capture(); + assert_eq!(uart.read(URXH, 4), 0); + assert_eq!(uart.read(UTRSTAT, 4) & 1, 0); + + uart.feed_rx(b'k'); + assert_ne!(uart.read(UTRSTAT, 4) & 1, 0); + assert_eq!(uart.read(UFSTAT, 4) & 0xf, 1); + assert_eq!(uart.read(URXH, 4), u64::from(b'k')); + assert_eq!(uart.read(UTRSTAT, 4) & 1, 0); + } +} diff --git a/src/emulator/src/system/boot.rs b/src/emulator/src/system/boot.rs index 1dd9a04..bb6d6b6 100644 --- a/src/emulator/src/system/boot.rs +++ b/src/emulator/src/system/boot.rs @@ -43,6 +43,8 @@ pub enum Error { /// A boot option the selected kernel format cannot honour. Unsupported(&'static str), Memory(MemoryError), + /// The device tree the caller supplied cannot be patched. + DeviceTree(super::dtb::Error), } impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -163,6 +165,36 @@ pub fn prepare_boot( }) } +/// Place a Linux `Image` at its text offset above `ram_base`, with `tree` (a device +/// tree the caller already built or patched) 2 MiB-aligned after it. The returned +/// layout's `boot_info` is the tree's address, which the kernel receives in `x0`. +pub fn place_with_tree( + memory: &mut impl GuestMemory, + image: &[u8], + tree: &[u8], + ram_base: u64, + ram_size: u64, +) -> Result { + let header = parse(image)?; + let entry = load_address(ram_base, header)?; + let occupied = header.image_size.max(image.len() as u64); + let tree_at = align(entry.checked_add(occupied).ok_or(Error::NoRoom)?)?; + let end = ram_base.checked_add(ram_size).ok_or(Error::NoRoom)?; + if tree_at + .checked_add(tree.len() as u64) + .is_none_or(|tree_end| tree_end > end) + { + return Err(Error::NoRoom); + } + memory.write(entry, image).map_err(Error::Memory)?; + memory.write(tree_at, tree).map_err(Error::Memory)?; + Ok(Layout { + entry, + boot_info: tree_at, + initrd: None, + }) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/emulator/src/system/dtb.rs b/src/emulator/src/system/dtb.rs new file mode 100644 index 0000000..90804fe --- /dev/null +++ b/src/emulator/src/system/dtb.rs @@ -0,0 +1,794 @@ +//! Flattened device tree patching for a device tree supplied by the user. +//! +//! The kernel reads `/chosen/bootargs` for its command line and `/memory` `reg` +//! for the RAM it may use, so both must describe what the emulator provides. The +//! blob is parsed into an owned tree of nodes and properties, keeping their order +//! and the memory reservation map, the two properties are edited there, and the +//! tree is serialized into a fresh blob. Nothing else in the tree is touched. + +use alloc::vec::Vec; +use core::fmt; + +const MAGIC: u32 = 0xd00d_feed; +const HEADER_LEN: usize = 40; +/// Newest format version this reader and writer speak. +const VERSION: u32 = 17; +const LAST_COMP_VERSION: u32 = 16; +const RESERVE_ENTRY_LEN: usize = 16; + +const FDT_BEGIN_NODE: u32 = 1; +const FDT_END_NODE: u32 = 2; +const FDT_PROP: u32 = 3; +const FDT_NOP: u32 = 4; +const FDT_END: u32 = 9; + +/// Cell counts the spec assumes when the root node omits them. +const DEFAULT_ADDRESS_CELLS: u32 = 2; +const DEFAULT_SIZE_CELLS: u32 = 1; + +const TRUNCATED_STRUCT: Error = Error::Malformed("structure block ends inside a token"); +const PROP_PAST_STRUCT: Error = Error::Malformed("property runs past structure block"); + +#[derive(Debug, PartialEq, Eq)] +pub enum Error { + /// The blob is shorter than its header, or than the `totalsize` it declares. + Truncated { + len: usize, + needed: usize, + }, + BadMagic(u32), + UnsupportedVersion(u32), + /// The blob violates the flattened format; the message names the first problem found. + Malformed(&'static str), + /// No node named `memory*` with `device_type = "memory"` exists to receive RAM. + NoMemoryNode, + /// `#address-cells` or `#size-cells` is not 1 or 2, so `reg` cannot be encoded. + CellCount(u32), + /// The RAM base or size does not fit in the single cell the root node declares. + ValueTooLarge { + value: u64, + cells: u32, + }, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::Truncated { len, needed } => { + write!(f, "device tree is {len} bytes, needs {needed}") + } + Error::BadMagic(magic) => write!(f, "bad device tree magic {magic:#010x}"), + Error::UnsupportedVersion(version) => { + write!( + f, + "unsupported device tree version {version}, need 17 or newer" + ) + } + Error::Malformed(what) => write!(f, "malformed device tree: {what}"), + Error::NoMemoryNode => { + write!( + f, + "device tree has no memory node with device_type \"memory\"" + ) + } + Error::CellCount(cells) => { + write!(f, "root cell count {cells} is not 1 or 2") + } + Error::ValueTooLarge { value, cells } => { + write!(f, "{value:#x} does not fit in {cells} cell(s)") + } + } + } +} + +impl core::error::Error for Error {} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Node { + name: Vec, + props: Vec, + children: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Prop { + name: Vec, + value: Vec, +} + +impl Node { + fn named(name: &[u8]) -> Self { + Node { + name: name.to_vec(), + props: Vec::new(), + children: Vec::new(), + } + } +} + +struct Parsed { + reservations: Vec<(u64, u64)>, + boot_cpuid_phys: u32, + root: Node, +} + +/// Return a new blob equal to `blob` except: if `bootargs` is `Some`, `/chosen` +/// gets the `bootargs` string property (replacing any existing one, creating +/// `/chosen` if absent); if `memory` is `Some((base, size))`, the first node +/// (depth-first) whose name starts with `memory` and whose `device_type` is +/// `memory` gets `reg` encoded with the root's `#address-cells` and `#size-cells`. +/// Every other node and property is preserved in order. +pub fn patch( + blob: &[u8], + bootargs: Option<&str>, + memory: Option<(u64, u64)>, +) -> Result, Error> { + let Parsed { + reservations, + boot_cpuid_phys, + mut root, + } = parse(blob)?; + if let Some(args) = bootargs { + let mut value = args.as_bytes().to_vec(); + value.push(0); + set_prop(chosen_node(&mut root), b"bootargs", value); + } + if let Some((base, size)) = memory { + let address_cells = cell_count(&root, b"#address-cells", DEFAULT_ADDRESS_CELLS)?; + let size_cells = cell_count(&root, b"#size-cells", DEFAULT_SIZE_CELLS)?; + let mut reg = Vec::new(); + push_cells(&mut reg, base, address_cells)?; + push_cells(&mut reg, size, size_cells)?; + let node = memory_node_mut(&mut root).ok_or(Error::NoMemoryNode)?; + set_prop(node, b"reg", reg); + } + serialize(&root, &reservations, boot_cpuid_phys) +} + +fn chosen_node(root: &mut Node) -> &mut Node { + let index = match root.children.iter().position(|c| c.name == b"chosen") { + Some(index) => index, + None => { + root.children.push(Node::named(b"chosen")); + root.children.len() - 1 + } + }; + &mut root.children[index] +} + +fn memory_node_mut(node: &mut Node) -> Option<&mut Node> { + if is_memory_device(node) { + return Some(node); + } + node.children.iter_mut().find_map(memory_node_mut) +} + +fn is_memory_device(node: &Node) -> bool { + node.name.starts_with(b"memory") + && prop_value(node, b"device_type") + .is_some_and(|value| value.strip_suffix(b"\0").unwrap_or(value) == b"memory") +} + +fn prop_value<'a>(node: &'a Node, name: &[u8]) -> Option<&'a [u8]> { + node.props + .iter() + .find(|p| p.name == name) + .map(|p| p.value.as_slice()) +} + +fn set_prop(node: &mut Node, name: &[u8], value: Vec) { + match node.props.iter_mut().find(|p| p.name == name) { + Some(prop) => prop.value = value, + None => node.props.push(Prop { + name: name.to_vec(), + value, + }), + } +} + +fn cell_count(root: &Node, name: &[u8], default: u32) -> Result { + match prop_value(root, name) { + None => Ok(default), + Some(value) => <[u8; 4]>::try_from(value) + .map(u32::from_be_bytes) + .map_err(|_| Error::Malformed("cell count property is not one u32")), + } +} + +/// Appends `value` as `cells` big-endian u32s; only 1 and 2 cells are valid. +fn push_cells(out: &mut Vec, value: u64, cells: u32) -> Result<(), Error> { + match cells { + 1 => { + let word = u32::try_from(value).map_err(|_| Error::ValueTooLarge { value, cells })?; + out.extend_from_slice(&word.to_be_bytes()); + Ok(()) + } + 2 => { + out.extend_from_slice(&value.to_be_bytes()); + Ok(()) + } + _ => Err(Error::CellCount(cells)), + } +} + +fn parse(blob: &[u8]) -> Result { + let header = parse_header(blob)?; + let data = blob + .get(..header.totalsize) + .ok_or(Error::Malformed("totalsize exceeds blob"))?; + let reservations = parse_reservations(data, header.off_mem_rsvmap)?; + let structs = block( + data, + header.off_dt_struct, + header.size_dt_struct, + "structure block lies outside totalsize", + )?; + let strings = block( + data, + header.off_dt_strings, + header.size_dt_strings, + "strings block lies outside totalsize", + )?; + if header.off_dt_struct % 4 != 0 { + return Err(Error::Malformed("structure block is not 4-byte aligned")); + } + let root = parse_struct(structs, strings)?; + Ok(Parsed { + reservations, + boot_cpuid_phys: header.boot_cpuid_phys, + root, + }) +} + +struct Header { + totalsize: usize, + off_dt_struct: u32, + off_dt_strings: u32, + off_mem_rsvmap: u32, + size_dt_strings: u32, + size_dt_struct: u32, + boot_cpuid_phys: u32, +} + +fn parse_header(blob: &[u8]) -> Result { + if blob.len() < HEADER_LEN { + return Err(Error::Truncated { + len: blob.len(), + needed: HEADER_LEN, + }); + } + let word = |at: usize| read_u32(blob, at).ok_or(Error::Malformed("header is truncated")); + let magic = word(0)?; + if magic != MAGIC { + return Err(Error::BadMagic(magic)); + } + let totalsize = word(4)? as usize; + let version = word(20)?; + if version < VERSION { + return Err(Error::UnsupportedVersion(version)); + } + if totalsize < HEADER_LEN { + return Err(Error::Malformed("totalsize is smaller than the header")); + } + if totalsize > blob.len() { + return Err(Error::Truncated { + len: blob.len(), + needed: totalsize, + }); + } + Ok(Header { + totalsize, + off_dt_struct: word(8)?, + off_dt_strings: word(12)?, + off_mem_rsvmap: word(16)?, + boot_cpuid_phys: word(28)?, + size_dt_strings: word(32)?, + size_dt_struct: word(36)?, + }) +} + +/// Reads (address, size) pairs up to the all-zero terminator. +fn parse_reservations(data: &[u8], off: u32) -> Result, Error> { + const PAST_END: Error = Error::Malformed("memory reservation block runs past totalsize"); + let mut entries = Vec::new(); + let mut pos = off as usize; + loop { + let address = read_u64(data, pos).ok_or(PAST_END)?; + let size = read_u64(data, pos + 8).ok_or(PAST_END)?; + pos += RESERVE_ENTRY_LEN; + if address == 0 && size == 0 { + return Ok(entries); + } + entries.push((address, size)); + } +} + +fn parse_struct(structs: &[u8], strings: &[u8]) -> Result { + let mut stack: Vec = Vec::new(); + let mut root: Option = None; + let mut pos = 0usize; + loop { + let token = read_u32(structs, pos).ok_or(TRUNCATED_STRUCT)?; + pos += 4; + match token { + FDT_BEGIN_NODE => { + if stack.is_empty() && root.is_some() { + return Err(Error::Malformed("more than one root node")); + } + let (name, next) = read_padded_name(structs, pos)?; + pos = next; + stack.push(Node { + name, + props: Vec::new(), + children: Vec::new(), + }); + } + FDT_END_NODE => { + let node = stack + .pop() + .ok_or(Error::Malformed("FDT_END_NODE with no open node"))?; + match stack.last_mut() { + Some(parent) => parent.children.push(node), + None => root = Some(node), + } + } + FDT_PROP => { + let len = read_u32(structs, pos).ok_or(TRUNCATED_STRUCT)? as usize; + let name_off = read_u32(structs, pos + 4).ok_or(TRUNCATED_STRUCT)?; + pos += 8; + let end = pos.checked_add(len).ok_or(PROP_PAST_STRUCT)?; + let value = structs.get(pos..end).ok_or(PROP_PAST_STRUCT)?.to_vec(); + pos = align4(end); + if pos > structs.len() { + return Err(PROP_PAST_STRUCT); + } + let name = string_at(strings, name_off)?; + let parent = stack + .last_mut() + .ok_or(Error::Malformed("property outside any node"))?; + parent.props.push(Prop { name, value }); + } + FDT_NOP => {} + FDT_END => { + if !stack.is_empty() { + return Err(Error::Malformed("FDT_END inside an open node")); + } + return root.ok_or(Error::Malformed("no root node before FDT_END")); + } + _ => return Err(Error::Malformed("unknown structure token")), + } + } +} + +/// Reads a NUL-terminated node name at `at`; returns it with the next token offset. +fn read_padded_name(structs: &[u8], at: usize) -> Result<(Vec, usize), Error> { + let rest = structs + .get(at..) + .ok_or(Error::Malformed("node name starts outside structure block"))?; + let nul = rest + .iter() + .position(|&b| b == 0) + .ok_or(Error::Malformed("unterminated node name"))?; + let next = align4(at + nul + 1); + if next > structs.len() { + return Err(Error::Malformed( + "node name padding runs past structure block", + )); + } + Ok((rest[..nul].to_vec(), next)) +} + +fn string_at(strings: &[u8], at: u32) -> Result, Error> { + let rest = strings.get(at as usize..).ok_or(Error::Malformed( + "property name offset outside strings block", + ))?; + let nul = rest + .iter() + .position(|&b| b == 0) + .ok_or(Error::Malformed("unterminated property name"))?; + Ok(rest[..nul].to_vec()) +} + +/// Serializes `root` into a version 17 blob. Property names are interned into the +/// strings block, so each distinct name is stored once. +fn serialize( + root: &Node, + reservations: &[(u64, u64)], + boot_cpuid_phys: u32, +) -> Result, Error> { + let mut writer = Writer { + structs: Vec::new(), + strings: Vec::new(), + names: Vec::new(), + }; + writer.node(root)?; + push_u32(&mut writer.structs, FDT_END); + + let off_mem_rsvmap = HEADER_LEN; + let rsvmap_len = (reservations.len() + 1) * RESERVE_ENTRY_LEN; + let off_dt_struct = off_mem_rsvmap + rsvmap_len; + let off_dt_strings = off_dt_struct + writer.structs.len(); + let totalsize = off_dt_strings + writer.strings.len(); + + let to_u32 = + |value: usize| u32::try_from(value).map_err(|_| Error::Malformed("blob exceeds 4 GiB")); + let mut out = Vec::with_capacity(totalsize); + push_u32(&mut out, MAGIC); + push_u32(&mut out, to_u32(totalsize)?); + push_u32(&mut out, to_u32(off_dt_struct)?); + push_u32(&mut out, to_u32(off_dt_strings)?); + push_u32(&mut out, to_u32(off_mem_rsvmap)?); + push_u32(&mut out, VERSION); + push_u32(&mut out, LAST_COMP_VERSION); + push_u32(&mut out, boot_cpuid_phys); + push_u32(&mut out, to_u32(writer.strings.len())?); + push_u32(&mut out, to_u32(writer.structs.len())?); + for &(address, size) in reservations { + out.extend_from_slice(&address.to_be_bytes()); + out.extend_from_slice(&size.to_be_bytes()); + } + out.extend_from_slice(&[0; RESERVE_ENTRY_LEN]); + out.extend_from_slice(&writer.structs); + out.extend_from_slice(&writer.strings); + Ok(out) +} + +struct Writer { + structs: Vec, + strings: Vec, + names: Vec<(Vec, u32)>, +} + +impl Writer { + fn name_offset(&mut self, name: &[u8]) -> Result { + if let Some((_, offset)) = self.names.iter().find(|(n, _)| n.as_slice() == name) { + return Ok(*offset); + } + let offset = u32::try_from(self.strings.len()) + .map_err(|_| Error::Malformed("strings block exceeds 4 GiB"))?; + self.strings.extend_from_slice(name); + self.strings.push(0); + self.names.push((name.to_vec(), offset)); + Ok(offset) + } + + fn node(&mut self, node: &Node) -> Result<(), Error> { + push_u32(&mut self.structs, FDT_BEGIN_NODE); + self.structs.extend_from_slice(&node.name); + self.structs.push(0); + pad4(&mut self.structs); + for prop in &node.props { + let name_off = self.name_offset(&prop.name)?; + let len = u32::try_from(prop.value.len()) + .map_err(|_| Error::Malformed("property value exceeds 4 GiB"))?; + push_u32(&mut self.structs, FDT_PROP); + push_u32(&mut self.structs, len); + push_u32(&mut self.structs, name_off); + self.structs.extend_from_slice(&prop.value); + pad4(&mut self.structs); + } + for child in &node.children { + self.node(child)?; + } + push_u32(&mut self.structs, FDT_END_NODE); + Ok(()) + } +} + +fn block<'a>(data: &'a [u8], off: u32, size: u32, what: &'static str) -> Result<&'a [u8], Error> { + let start = off as usize; + let end = start + .checked_add(size as usize) + .ok_or(Error::Malformed(what))?; + data.get(start..end).ok_or(Error::Malformed(what)) +} + +fn read_u32(bytes: &[u8], at: usize) -> Option { + let end = at.checked_add(4)?; + let word: [u8; 4] = bytes.get(at..end)?.try_into().ok()?; + Some(u32::from_be_bytes(word)) +} + +fn read_u64(bytes: &[u8], at: usize) -> Option { + let end = at.checked_add(8)?; + let word: [u8; 8] = bytes.get(at..end)?.try_into().ok()?; + Some(u64::from_be_bytes(word)) +} + +fn align4(value: usize) -> usize { + (value + 3) & !3 +} + +fn push_u32(out: &mut Vec, value: u32) { + out.extend_from_slice(&value.to_be_bytes()); +} + +fn pad4(out: &mut Vec) { + while !out.len().is_multiple_of(4) { + out.push(0); + } +} + +#[cfg(test)] +mod tests { + use alloc::vec; + + use super::*; + + fn prop(name: &str, value: &[u8]) -> Prop { + Prop { + name: name.as_bytes().to_vec(), + value: value.to_vec(), + } + } + + fn cstr(s: &str) -> Vec { + let mut v = s.as_bytes().to_vec(); + v.push(0); + v + } + + fn be32(words: &[u32]) -> Vec { + words.iter().flat_map(|w| w.to_be_bytes()).collect() + } + + fn node(name: &str, props: Vec, children: Vec) -> Node { + Node { + name: name.as_bytes().to_vec(), + props, + children, + } + } + + /// Root with a memory-controller (not a memory device), memory, chosen and a serial. + fn fixture_with_cells(address_cells: u32, size_cells: u32) -> Node { + let mut root = Node::named(b""); + root.props = vec![ + prop("#address-cells", &be32(&[address_cells])), + prop("#size-cells", &be32(&[size_cells])), + prop("compatible", &cstr("apple,j274")), + ]; + root.children = vec![ + node( + "memory-controller@20e000000", + vec![prop("compatible", &cstr("apple,memory-controller"))], + vec![], + ), + node( + "memory@800000000", + vec![ + prop("device_type", &cstr("memory")), + prop("reg", &be32(&[8, 0, 2, 0])), + ], + vec![], + ), + node( + "chosen", + vec![prop("stdout-path", &cstr("/serial@235200000"))], + vec![], + ), + node( + "serial@235200000", + vec![ + prop("compatible", &cstr("ns16550")), + prop("reg", &be32(&[2, 0x3520_0000, 0, 0x4000])), + ], + vec![], + ), + ]; + root + } + + fn fixture() -> Node { + fixture_with_cells(2, 2) + } + + fn blob_of(root: &Node) -> Vec { + serialize(root, &[], 0).unwrap() + } + + fn tree_of(blob: &[u8]) -> Node { + parse(blob).unwrap().root + } + + fn child<'a>(node: &'a Node, name: &str) -> &'a Node { + node.children + .iter() + .find(|c| c.name == name.as_bytes()) + .unwrap() + } + + fn prop_of<'a>(node: &'a Node, name: &str) -> &'a [u8] { + prop_value(node, name.as_bytes()).unwrap() + } + + fn set_u32(blob: &mut [u8], at: usize, value: u32) { + blob[at..at + 4].copy_from_slice(&value.to_be_bytes()); + } + + #[test] + fn round_trip_keeps_tree_order_and_reservations() { + let original = fixture(); + let reservations = [(0x4000_0000u64, 0x0010_0000u64)]; + let blob = serialize(&original, &reservations, 0).unwrap(); + let out = patch(&blob, None, None).unwrap(); + let parsed = parse(&out).unwrap(); + assert_eq!(parsed.root, original); + assert_eq!(parsed.reservations, reservations); + } + + #[test] + fn bootargs_is_appended_to_chosen_when_absent() { + let out = patch( + &blob_of(&fixture()), + Some("console=ttyS0 root=/dev/vda"), + None, + ) + .unwrap(); + let out = tree_of(&out); + let chosen = child(&out, "chosen"); + let names: Vec<&[u8]> = chosen.props.iter().map(|p| p.name.as_slice()).collect(); + assert_eq!(names, [&b"stdout-path"[..], &b"bootargs"[..]]); + assert_eq!( + prop_of(chosen, "bootargs"), + b"console=ttyS0 root=/dev/vda\0" + ); + } + + #[test] + fn bootargs_replaces_existing_property_in_place() { + let mut root = fixture(); + let chosen = root + .children + .iter_mut() + .find(|c| c.name == b"chosen") + .unwrap(); + chosen.props.insert(0, prop("bootargs", &cstr("old"))); + let out = tree_of(&patch(&blob_of(&root), Some("new args"), None).unwrap()); + let chosen = child(&out, "chosen"); + let names: Vec<&[u8]> = chosen.props.iter().map(|p| p.name.as_slice()).collect(); + assert_eq!(names, [&b"bootargs"[..], &b"stdout-path"[..]]); + assert_eq!(prop_of(chosen, "bootargs"), b"new args\0"); + } + + #[test] + fn chosen_is_created_at_root_when_absent() { + let mut root = fixture(); + root.children.retain(|c| c.name != b"chosen"); + let out = tree_of(&patch(&blob_of(&root), Some("quiet"), None).unwrap()); + assert_eq!(out.children.len(), root.children.len() + 1); + assert_eq!(&out.children[..root.children.len()], &root.children[..]); + let chosen = out.children.last().unwrap(); + assert_eq!(chosen.name, b"chosen"); + assert_eq!(chosen.props, vec![prop("bootargs", b"quiet\0")]); + } + + #[test] + fn memory_reg_uses_two_two_cells() { + let out = patch( + &blob_of(&fixture()), + None, + Some((0x1_4000_0000, 0x2_0000_0000)), + ) + .unwrap(); + let out = tree_of(&out); + let mem = child(&out, "memory@800000000"); + assert_eq!(prop_of(mem, "reg"), be32(&[1, 0x4000_0000, 2, 0])); + } + + #[test] + fn memory_reg_uses_one_one_cells() { + let out = patch( + &blob_of(&fixture_with_cells(1, 1)), + None, + Some((0x4000_0000, 0x0800_0000)), + ) + .unwrap(); + let out = tree_of(&out); + let mem = child(&out, "memory@800000000"); + assert_eq!(prop_of(mem, "reg"), be32(&[0x4000_0000, 0x0800_0000])); + } + + #[test] + fn patch_touches_only_chosen_and_memory_reg() { + let original = fixture(); + let out = tree_of( + &patch( + &blob_of(&original), + Some("quiet"), + Some((0x1_4000_0000, 0x2_0000_0000)), + ) + .unwrap(), + ); + assert_eq!(out.props, original.props); + assert_eq!(out.name, original.name); + assert_eq!( + child(&out, "memory-controller@20e000000"), + child(&original, "memory-controller@20e000000") + ); + assert_eq!( + child(&out, "serial@235200000"), + child(&original, "serial@235200000") + ); + assert_eq!(out.children.len(), original.children.len()); + assert_eq!( + prop_of(child(&out, "memory@800000000"), "device_type"), + b"memory\0" + ); + assert_eq!( + prop_of(child(&out, "chosen"), "stdout-path"), + b"/serial@235200000\0" + ); + } + + #[test] + fn memory_value_too_large_for_one_cell_is_rejected() { + let blob = blob_of(&fixture_with_cells(1, 1)); + assert_eq!( + patch(&blob, None, Some((1 << 32, 0x1000))), + Err(Error::ValueTooLarge { + value: 1 << 32, + cells: 1 + }) + ); + } + + #[test] + fn unsupported_cell_count_is_rejected() { + let blob = blob_of(&fixture_with_cells(3, 2)); + assert_eq!( + patch(&blob, None, Some((0x4000_0000, 0x1000))), + Err(Error::CellCount(3)) + ); + } + + #[test] + fn memory_patch_needs_a_memory_device_node() { + let mut root = fixture(); + root.children.retain(|c| !c.name.starts_with(b"memory@")); + assert_eq!( + patch(&blob_of(&root), None, Some((0x4000_0000, 0x1000))), + Err(Error::NoMemoryNode) + ); + } + + #[test] + fn bad_magic_is_rejected() { + let mut blob = blob_of(&fixture()); + blob[0] ^= 0xff; + assert!(matches!(patch(&blob, None, None), Err(Error::BadMagic(_)))); + } + + #[test] + fn truncated_blob_is_rejected() { + let blob = blob_of(&fixture()); + assert!(matches!( + patch(&blob[..blob.len() - 1], None, None), + Err(Error::Truncated { .. }) + )); + assert!(matches!( + patch(&blob[..HEADER_LEN - 1], None, None), + Err(Error::Truncated { .. }) + )); + } + + #[test] + fn offsets_past_end_are_rejected() { + let blob = blob_of(&fixture()); + let total = blob.len() as u32; + + let mut bad_struct = blob.clone(); + set_u32(&mut bad_struct, 8, total + 4); + assert!(matches!( + patch(&bad_struct, None, None), + Err(Error::Malformed(_)) + )); + + let mut bad_strings = blob; + set_u32(&mut bad_strings, 12, u32::MAX); + assert!(matches!( + patch(&bad_strings, None, None), + Err(Error::Malformed(_)) + )); + } +} diff --git a/src/emulator/src/system/m1.rs b/src/emulator/src/system/m1.rs new file mode 100644 index 0000000..b49cdec --- /dev/null +++ b/src/emulator/src/system/m1.rs @@ -0,0 +1,216 @@ +//! Apple M1 (t8103) SoC: the devices the Asahi Linux kernel's +//! `arch/arm64/boot/dts/apple/t8103.dtsi` describes, far enough to boot it. +//! +//! The addresses are the ones the M1 Mac mini's own device tree (`DeviceTree.j274ap` +//! in the macOS restore image) gives: arm-io's `ranges` places the SoC at +//! `0x2_0000_0000`, and the Linux nodes use the same absolute addresses. DRAM starts +//! at `0x8_0000_0000`. The kernel is booted with the device tree the caller supplies, +//! patched to describe the RAM this run provides. +use super::boot::{self, Error, Layout}; +use super::dtb; +use crate::devices::aic::{self, Aic}; +use crate::devices::pmgr::Pmgr; +use crate::devices::s5l_uart::{self, S5lUart}; +use crate::memory::GuestMemory; + +/// DRAM base (`memory@800000000` in the Asahi device tree). +pub const RAM_BASE: u64 = 0x8_0000_0000; +/// Default guest RAM: enough for the Linux kernel's own memory, well below the host's. +pub const DEFAULT_RAM_SIZE: usize = 1 << 30; +/// Interrupt controller (`aic: interrupt-controller@23b100000`). +pub const AIC_BASE: u64 = 0x2_3b10_0000; +/// Power-state register blocks (`power-management@…` with the `apple,pmgr` syscon): +/// the always-on block and the NUB block, as the device tree gives them. +pub const PMGR_BLOCKS: [(u64, u64); 2] = [(0x2_3b70_0000, 0x1_4000), (0x2_3d28_0000, 0x4000)]; +/// Console (`serial0: serial@235200000`), the `stdout-path` of the M1 device trees. +pub const UART_BASE: u64 = 0x2_3520_0000; +/// Hardware IRQ of the console (`AIC_IRQ 605` on `serial0`). +pub const UART_IRQ: u32 = 605; +/// Physical space below DRAM, where the device tree (an identity-mapped `simple-bus`) +/// places the SoC's devices. Devices this model does not implement live here. +pub const UNMODELED_BASE: u64 = 0; +pub const UNMODELED_SIZE: u64 = RAM_BASE; +/// `earlycon` without an address takes the device from `stdout-path`. +pub const DEFAULT_CMDLINE: &str = "earlycon nokaslr loglevel=8"; + +/// `MIDR_EL1` of an Icestorm core: implementer Apple (0x61), architecture from ID +/// registers (0xf), part 0x022. +pub const MIDR_EL1: u64 = 0x610f_0220; +/// `ID_AA64PFR0_EL1`: EL0 and EL1 in AArch64, no EL2 or EL3, and floating point and +/// Advanced SIMD implemented, as on the M1. +pub const ID_AA64PFR0_EL1: u64 = 0x11; +/// `ID_AA64MMFR0_EL1`: 4 KiB and 16 KiB translation granules (the Asahi kernel is built +/// for 16 KiB pages and refuses a CPU without one), no 64 KiB granule, 40-bit PA. +pub const ID_AA64MMFR0_EL1: u64 = (0xf << 24) | (1 << 20) | 2; + +/// The machine runs one vCPU. The device tree lists eight cores, but their +/// `cpu-release-addr` is zero, so Linux's spin-table code does not start them. +const CPUS: u32 = 1; + +/// Give `system` the identity of an M1 core. +pub fn identify(system: &mut crate::aarch64::cpu::System) { + system.midr_el1 = MIDR_EL1; + system.id_aa64pfr0_el1 = ID_AA64PFR0_EL1; + system.id_aa64mmfr0_el1 = ID_AA64MMFR0_EL1; +} + +/// The SoC devices on the bus: the AIC, the power-state blocks, the console, and the +/// line between the console and the AIC. +pub struct Soc { + aic: Aic, + pmgr: [Pmgr; PMGR_BLOCKS.len()], + uart: S5lUart, + unmodeled: u64, +} + +impl Soc { + /// `sink` receives every byte the kernel writes to the console. + pub fn new(sink: impl FnMut(u8) + Send + 'static) -> Self { + Self { + aic: Aic::new(CPUS), + pmgr: PMGR_BLOCKS.map(|(_, len)| Pmgr::new(len)), + uart: S5lUart::new(sink), + unmodeled: 0, + } + } + + /// Present the current device lines to the interrupt controller. Call before each + /// run of the CPU. + pub fn sync_lines(&mut self) { + self.aic.set_irq(UART_IRQ, self.uart.irq_asserted()); + } + + /// Whether the IRQ pin of CPU `cpu` is asserted. + pub fn irq_pending(&self, cpu: u32) -> bool { + self.aic.irq_pending(cpu) + } + + /// Accesses to arm-io devices this model does not implement: reads return zero and + /// writes are dropped, so a probe of such a device cannot fault the kernel. + pub fn unmodeled_accesses(&self) -> u64 { + self.unmodeled + } + + /// The power-state block and offset an access of `size` bytes at `address` falls in. + fn pmgr_window(address: u64, size: u8) -> Option<(usize, u64)> { + PMGR_BLOCKS + .iter() + .enumerate() + .find_map(|(block, &(base, len))| { + window(address, size, base, len).map(|offset| (block, offset)) + }) + } + + /// Read `size` bytes at guest-physical `address`, or `None` when no SoC device maps it. + pub fn read(&mut self, cpu: u32, address: u64, size: u8) -> Option { + if let Some(offset) = window(address, size, AIC_BASE, aic::LEN) { + return Some(self.aic.read(cpu, offset, size)); + } + if let Some((block, offset)) = Self::pmgr_window(address, size) { + return Some(self.pmgr[block].read(offset, size)); + } + if let Some(offset) = window(address, size, UART_BASE, s5l_uart::LEN) { + return Some(self.uart.read(offset, size)); + } + if window(address, size, UNMODELED_BASE, UNMODELED_SIZE).is_some() { + self.unmodeled += 1; + return Some(0); + } + None + } + + /// Write `size` bytes to guest-physical `address`; `false` when no SoC device maps it. + pub fn write(&mut self, cpu: u32, address: u64, size: u8, value: u64) -> bool { + if let Some(offset) = window(address, size, AIC_BASE, aic::LEN) { + self.aic.write(cpu, offset, size, value); + return true; + } + if let Some((block, offset)) = Self::pmgr_window(address, size) { + self.pmgr[block].write(offset, size, value); + return true; + } + if let Some(offset) = window(address, size, UART_BASE, s5l_uart::LEN) { + self.uart.write(offset, size, value); + return true; + } + if window(address, size, UNMODELED_BASE, UNMODELED_SIZE).is_some() { + self.unmodeled += 1; + return true; + } + false + } +} + +/// Offset of an access of `size` bytes at `address` within `[base, base + len)`. +/// An access that straddles the end of the window matches nothing. +fn window(address: u64, size: u8, base: u64, len: u64) -> Option { + let end = address.checked_add(u64::from(size))?; + (address >= base && end <= base + len).then(|| address - base) +} + +/// Place a Linux `Image` in RAM and the device tree after it, with the tree's +/// `/chosen/bootargs` set to `cmdline` (when non-empty) and its memory node describing +/// `ram_size` bytes at [`RAM_BASE`]. The kernel is entered with `x0` = the tree. +pub fn prepare( + memory: &mut impl GuestMemory, + image: &[u8], + device_tree: &[u8], + cmdline: &str, + ram_size: u64, +) -> Result { + let bootargs = (!cmdline.is_empty()).then_some(cmdline); + let tree = + dtb::patch(device_tree, bootargs, Some((RAM_BASE, ram_size))).map_err(Error::DeviceTree)?; + boot::place_with_tree(memory, image, &tree, RAM_BASE, ram_size) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn window_matches_only_accesses_entirely_inside_the_device() { + assert_eq!(window(AIC_BASE, 4, AIC_BASE, aic::LEN), Some(0)); + assert_eq!( + window(AIC_BASE + aic::LEN - 4, 4, AIC_BASE, aic::LEN), + Some(aic::LEN - 4) + ); + // Straddling the end, or wrapping, matches nothing. + assert_eq!(window(AIC_BASE + aic::LEN - 2, 4, AIC_BASE, aic::LEN), None); + assert_eq!(window(u64::MAX - 1, 4, AIC_BASE, aic::LEN), None); + } + + #[test] + fn soc_routes_by_address_and_declines_everything_else() { + let mut soc = Soc::new(|_| {}); + // The AIC identifies the reading CPU at WHOAMI. + assert_eq!(soc.read(0, AIC_BASE + 0x2000, 4), Some(0)); + // Above DRAM, where the SoC maps nothing, is not an M1 address. + assert_eq!(soc.read(0, RAM_BASE + (1 << 40), 4), None); + assert!(!soc.write(0, RAM_BASE + (1 << 40), 4, 0)); + assert_eq!(soc.unmodeled_accesses(), 0); + } + + #[test] + fn power_state_registers_of_each_pmgr_block_acknowledge_their_target() { + let mut soc = Soc::new(|_| {}); + for (base, _) in PMGR_BLOCKS { + // A domain's state register, as its driver polls it. + let state = base + 0x100; + assert_eq!(soc.read(0, state, 4), Some(0xff)); + assert!(soc.write(0, state, 4, 0x0)); + assert_eq!(soc.read(0, state, 4), Some(0x00)); + } + assert_eq!(soc.unmodeled_accesses(), 0); + } + + #[test] + fn unmodeled_arm_io_device_reads_zero_and_drops_writes_and_is_counted() { + let mut soc = Soc::new(|_| {}); + // The pin controller (`pinctrl@23e820000`) is probed at boot. + assert_eq!(soc.read(0, 0x2_3e82_0000, 4), Some(0)); + assert!(soc.write(0, 0x2_3e82_0000, 4, 0xffff_ffff)); + assert_eq!(soc.read(0, 0x2_3e82_0000, 4), Some(0)); + assert_eq!(soc.unmodeled_accesses(), 3); + } +} diff --git a/src/emulator/src/system/machine.rs b/src/emulator/src/system/machine.rs index 5abff51..bfebffa 100644 --- a/src/emulator/src/system/machine.rs +++ b/src/emulator/src/system/machine.rs @@ -44,6 +44,23 @@ pub trait DeviceIo { fn write_virtio(&mut self, offset: u64, size: u8, value: u64) { let _ = (offset, size, value); } + /// A device the board places anywhere in the address space (the M1 SoC's + /// interrupt controller and UART). `None` declines the access, which then goes + /// to the fixed windows below. + fn read_soc(&mut self, cpu_id: u32, address: u64, size: u8) -> Option { + let _ = (cpu_id, address, size); + None + } + /// Returns `false` when the board's devices do not claim the address. + fn write_soc(&mut self, cpu_id: u32, address: u64, size: u8, value: u64) -> bool { + let _ = (cpu_id, address, size, value); + false + } + /// Whether the fixed windows below (the QEMU `virt` UART, GIC and virtio) exist on + /// this board. A board that maps none of them makes accesses there fault. + fn has_fixed_windows(&self) -> bool { + true + } } impl DeviceIo for (&mut Pl011, &mut Gicv2) { @@ -167,6 +184,10 @@ pub struct Machine { /// Set when a GICv3 forwards PPI 27 (the virtual timer) as a group 0 FIQ: the /// priority it was given. `None` leaves the timer to the GICv2 path. pub vtimer_fiq: Option, + /// A FIQ source the board drives directly. The M1 interrupt controller presents + /// the virtual timer this way: it is level-high while the timer's own condition + /// holds, with no GIC-style priority gating. + pub fiq_line: bool, timer_fired: bool, pub stalled_at: u64, pub stalled: Option, @@ -201,6 +222,7 @@ impl Machine { watch_epoch: 0, irq_line: false, vtimer_fiq: None, + fiq_line: false, timer_fired: false, stalled_at: 0, stalled: None, @@ -269,7 +291,7 @@ impl Machine { if self.timer_edge() { return Ok(Exit::Timer); } - let fiq = self.fiq_asserted(); + let fiq = self.fiq_line || self.fiq_asserted(); self.cpu.system.isr_el1 = u64::from(self.irq_line) << 7 | u64::from(fiq) << 6; let kind = if fiq && self.cpu.system.daif & 1 == 0 { Some(exception::Kind::Fiq) @@ -384,14 +406,32 @@ impl Machine { self.cpu.trap = Trap::None; exception::eret(&mut self.cpu); } - Trap::Svc | Trap::Brk => { - let ec = if self.cpu.trap == Trap::Svc { 0x15 } else { 0 }; + Trap::Svc => { + self.cpu.trap = Trap::None; + let pc = self.cpu.pc; + exception::take( + &mut self.cpu, + exception::Reason { + ec: 0x15, + status: 0, + instruction: true, + ..Default::default() + }, + pc, + ); + } + Trap::Brk => { self.cpu.trap = Trap::None; let pc = self.cpu.pc; + let immediate = self.cpu.address as u32; + // A breakpoint's syndrome is EC 0x3c with its immediate in ISS. exception::take( &mut self.cpu, exception::Reason { - ec, + ec: 0x3c, + status: 0, + instruction: true, + iss: immediate, ..Default::default() }, pc, @@ -407,12 +447,12 @@ impl Machine { ec: 0, instruction: true, status: 0, + iss: 0, }, pc, ); } Trap::Isb => { - self.tlb.purge_faults(); self.cpu.trap = Trap::None; } Trap::Tlbi => { @@ -613,6 +653,15 @@ impl Machine { } } fn abort(&mut self, address: u64, permission: bool, instruction: bool, write: bool) { + let status = match (permission, instruction) { + (false, _) => exception::status::TRANSLATION, + (true, false) => exception::status::PERMISSION, + (true, true) => exception::status::PERMISSION_FETCH, + }; + self.abort_with_status(address, status, instruction, write); + } + /// A synchronous abort with fault status `status`. + fn abort_with_status(&mut self, address: u64, status: u8, instruction: bool, write: bool) { let ec = if instruction { if self.cpu.system.el == 1 { 0x21 } else { 0x20 } } else if self.cpu.system.el == 1 { @@ -620,11 +669,6 @@ impl Machine { } else { 0x24 }; - let status = if permission { - if instruction { 13 } else { 12 } - } else { - 4 - }; self.cpu.trap = Trap::None; if !instruction { self.cpu.pc = self.cpu.pc.wrapping_sub(4); @@ -636,6 +680,7 @@ impl Machine { ec, instruction, status, + iss: 0, }, address, ); @@ -963,7 +1008,7 @@ impl Machine { }; if matches!(result, Err(MemoryError::AccessFault { .. })) { self.unmapped += 1; - self.abort(address, false, false, !load); + self.abort_with_status(address, exception::status::SYNC_EXTERNAL, false, !load); return Ok(()); } result @@ -973,9 +1018,20 @@ impl Machine { Err(MemoryError::AccessFault { .. }) => { self.device_access = true; let end = physical.checked_add(width as u64); - let window = - |base, length| physical >= base && end.is_some_and(|end| end <= base + length); - let read = if window(fdt::UART_BASE, crate::devices::pl011::LEN) { + let fixed = devices.has_fixed_windows(); + let window = |base, length| { + fixed && physical >= base && end.is_some_and(|end| end <= base + length) + }; + let soc = if load { + devices.read_soc(self.cpu_id, physical, width).map(Some) + } else { + devices + .write_soc(self.cpu_id, physical, width, value) + .then_some(None) + }; + let read = if let Some(read) = soc { + read + } else if window(fdt::UART_BASE, crate::devices::pl011::LEN) { if load { Some(devices.read_uart(physical - fdt::UART_BASE, width)) } else { @@ -1026,7 +1082,7 @@ impl Machine { } } else { self.unmapped += 1; - self.abort(address, false, false, !load); + self.abort_with_status(address, exception::status::SYNC_EXTERNAL, false, !load); return Ok(()); }; if let Some(value) = read { @@ -1273,6 +1329,39 @@ mod tests { Exit::Psci { .. } )); } + + #[test] + fn brk_takes_an_el1_breakpoint_exception_with_its_immediate_in_the_syndrome() { + let mut machine = machine(); + machine.cpu.system.el = 1; + machine.cpu.system.spsel = true; + machine.cpu.system.vbar_el1 = 0x40000400; + // `brk #0x800`, the immediate the kernel's `BUG()` uses. + machine + .memory + .write(0x40000000, &0xd4210000u32.to_le_bytes()) + .unwrap(); + // The vector for a synchronous exception from EL1 on SP_EL1: `VBAR + 0x200`. + machine + .memory + .write(0x40000600, &HVC.to_le_bytes()) + .unwrap(); + machine.cpu.pc = 0x40000000; + let mut serial = Pl011::new(|_| {}); + let mut gic = Gicv2::default(); + assert!(matches!( + machine.run(&mut serial, &mut gic).unwrap(), + Exit::Psci { .. } + )); + let esr = machine.cpu.system.esr_el1; + assert_eq!(esr >> 26, 0x3c, "EC is a breakpoint"); + assert_eq!((esr >> 25) & 1, 1, "IL: a 32-bit instruction"); + assert_eq!(esr & 0xffff, 0x800, "ISS is the immediate"); + assert_eq!( + machine.cpu.system.elr_el1, 0x40000000, + "ELR is the BRK itself" + ); + } const LDXR_X1_X0: u32 = 0xc85f7c01; const STXR_W2_X3_X0: u32 = 0xc8027c03; const HVC: u32 = 0xd4000002; diff --git a/src/emulator/src/system/mod.rs b/src/emulator/src/system/mod.rs index 2b4740a..c3da1b0 100644 --- a/src/emulator/src/system/mod.rs +++ b/src/emulator/src/system/mod.rs @@ -4,8 +4,10 @@ pub mod afdt; pub mod boot; +pub mod dtb; pub mod esr; pub mod fdt; +pub mod m1; pub mod machine; pub mod psci; pub mod smp; @@ -92,6 +94,8 @@ pub struct BootReport { pub executed_blocks: u64, pub timer_interrupts: u64, pub unmapped_accesses: u64, + /// Accesses to arm-io devices the M1 model does not implement (read as zero). + pub unmodeled_device_accesses: u64, pub stalled_at: Option, pub instruction: Option, pub last_fault_walk: Vec, @@ -169,6 +173,11 @@ impl core::fmt::Display for BootReport { for entry in &self.trace { writeln!(f, " pc {:x} sp {:x}", entry.pc, entry.sp)?; } + writeln!( + f, + "unmodeled device accesses {}", + self.unmodeled_device_accesses + )?; writeln!( f, "exits {}, blocks compiled {}, blocks executed {}, timer interrupts {}, unmapped accesses {}, elapsed {:.3}s", @@ -189,6 +198,18 @@ pub struct SystemConfig> { pub ram_size: usize, pub initrd: Option>, pub disk: Option, + pub board: Board, +} + +/// The machine a boot builds around the kernel. +#[derive(Debug, Clone, Default)] +pub enum Board { + /// QEMU `virt`-style board: a GICv2 for Linux, a GICv3 for XNU, a PL011 console. + #[default] + Virt, + /// Apple M1 (t8103) SoC ([`m1`]): an AIC and an S5L console. Linux only. The + /// caller supplies the device tree, such as the Asahi kernel's `t8103-j274.dtb`. + AppleM1 { device_tree: Vec }, } impl Default for SystemConfig { @@ -200,6 +221,7 @@ impl Default for SystemConfig { ram_size: RAM_SIZE, initrd: None, disk: None, + board: Board::Virt, } } } @@ -207,10 +229,12 @@ impl Default for SystemConfig { /// The interrupt controller a boot configures: the GICv2 a Linux device tree /// describes, or the GICv3 XNU's platform expects. Shared peripherals (UART, virtio) /// are wired to the GICv2 only; the GICv3 carries the virtual timer, which the -/// machine evaluates live from the timer registers. +/// machine evaluates live from the timer registers. The M1 board has no GIC: its +/// AIC sits in [`m1::Soc`] and presents the virtual timer as a FIQ. enum Intc { V2(Gicv2), V3(Gicv3), + Apple, } impl Intc { fn raise(&mut self, intid: u32) { @@ -231,35 +255,51 @@ impl Intc { fn signalled(&self, cpu: u32) -> bool { match self { Self::V2(gic) => gic.signalled(cpu), - Self::V3(_) => false, + Self::V3(_) | Self::Apple => false, } } /// See [`machine::Machine::vtimer_fiq`]. fn vtimer_fiq(&self) -> Option { match self { - Self::V2(_) => None, + Self::V2(_) | Self::Apple => None, Self::V3(gic) => gic.vtimer_fiq_priority(), } } + /// Whether the CPU sees a FIQ from the virtual timer, which is due at `due`. The + /// M1 AIC presents the timer's own condition with no further gating. + fn timer_fiq(&self, due: bool) -> bool { + matches!(self, Self::Apple) && due + } } +/// The bus of one board. The fixed windows (QEMU `virt`'s UART, GIC and virtio) exist +/// only on the virt board; on the M1 board `soc` answers instead, and the GIC accessors +/// below are never reached with [`Intc::Apple`]. struct SystemDevices<'a, V> { - serial: &'a mut Pl011, + serial: Option<&'a mut Pl011>, gic: &'a mut Intc, virtio: Option<&'a mut V>, + soc: Option<&'a mut m1::Soc>, } impl machine::DeviceIo for SystemDevices<'_, V> { fn read_uart(&mut self, offset: u64, size: u8) -> u64 { - self.serial.read(offset, size) + self.serial + .as_mut() + .expect("the fixed UART window exists only on the virt board, which has a PL011") + .read(offset, size) } fn write_uart(&mut self, offset: u64, size: u8, value: u64) { - self.serial.write(offset, size, value); + self.serial + .as_mut() + .expect("the fixed UART window exists only on the virt board, which has a PL011") + .write(offset, size, value); } fn read_gic_distributor(&mut self, cpu_id: u32, offset: u64, size: u8) -> u64 { match self.gic { Intc::V2(gic) => gic.read_distributor_for(cpu_id, offset, size), Intc::V3(gic) => gic.read_distributor(offset, size), + Intc::Apple => 0, } } fn write_gic_distributor(&mut self, cpu_id: u32, offset: u64, size: u8, value: u64) { @@ -268,13 +308,14 @@ impl machine::DeviceIo for SystemDevices<'_, V> { gic.write_distributor_for(cpu_id, offset, size, value); } Intc::V3(gic) => gic.write_distributor(offset, size, value), + Intc::Apple => {} } } fn read_gic_cpu(&mut self, cpu_id: u32, offset: u64, size: u8) -> u64 { match self.gic { Intc::V2(gic) => gic.read_cpu_for(cpu_id, offset, size), // The GICv3 CPU interface is the `ICC_*` system registers. - Intc::V3(_) => 0, + Intc::V3(_) | Intc::Apple => 0, } } fn write_gic_cpu(&mut self, cpu_id: u32, offset: u64, size: u8, value: u64) { @@ -285,7 +326,7 @@ impl machine::DeviceIo for SystemDevices<'_, V> { fn read_gic_redistributor(&mut self, offset: u64, size: u8) -> u64 { match self.gic { Intc::V3(gic) => gic.read_redistributor(offset, size), - Intc::V2(_) => 0, + Intc::V2(_) | Intc::Apple => 0, } } fn write_gic_redistributor(&mut self, offset: u64, size: u8, value: u64) { @@ -305,8 +346,18 @@ impl machine::DeviceIo for SystemDevices<'_, V> { v.write(offset, size, value); } } + fn read_soc(&mut self, cpu_id: u32, address: u64, size: u8) -> Option { + self.soc.as_mut()?.read(cpu_id, address, size) + } + fn write_soc(&mut self, cpu_id: u32, address: u64, size: u8, value: u64) -> bool { + self.soc + .as_mut() + .is_some_and(|soc| soc.write(cpu_id, address, size, value)) + } + fn has_fixed_windows(&self) -> bool { + self.soc.is_none() + } } - /// Boot a raw Linux Image with live serial output on stdout and a bounded wall-clock run. /// Execution faults are returned in the report with the full guest state, not discarded. pub fn boot(image: &[u8], timeout: Duration, cmdline: &str) -> Result { @@ -357,6 +408,7 @@ pub fn boot_with_options( ram_size: RAM_SIZE, initrd: None, disk: None, + board: Board::Virt, }, sink, ) @@ -375,26 +427,48 @@ pub fn boot_system( }; let timeout = config.timeout; let idle_mode = config.idle_mode; - let region = Region::ram(RAM_BASE, vec![0; ram_size]).map_err(Error::Memory)?; + let apple = matches!(config.board, Board::AppleM1 { .. }); + let ram_base = if apple { m1::RAM_BASE } else { RAM_BASE }; + let region = Region::ram(ram_base, vec![0; ram_size]).map_err(Error::Memory)?; let memory = PhysicalMemory::new(vec![region]).map_err(Error::Memory)?; let mut shared_mem = SharedMemory::new(memory); let has_virtio = config.disk.is_some(); let is_xnu = xnu::is_macho(image); - let layout = if is_xnu { - if config.initrd.is_some() || has_virtio { - return Err(Error::Boot(boot::Error::Unsupported( - "XNU boot has no initrd or virtio disk support", - ))); + let layout = match config.board { + Board::AppleM1 { device_tree } => { + if is_xnu { + return Err(Error::Boot(boot::Error::Unsupported( + "XNU boot on the Apple M1 SoC is not supported yet", + ))); + } + if has_virtio || config.initrd.is_some() { + return Err(Error::Boot(boot::Error::Unsupported( + "the Apple M1 SoC has no initrd or virtio disk support yet", + ))); + } + m1::prepare( + &mut shared_mem, + image, + &device_tree, + &config.cmdline, + ram_size as u64, + ) } - xnu::prepare_boot( - &mut shared_mem, - image, - &config.cmdline, - RAM_BASE, - ram_size as u64, - ) - } else { - boot::prepare_boot( + Board::Virt if is_xnu => { + if config.initrd.is_some() || has_virtio { + return Err(Error::Boot(boot::Error::Unsupported( + "XNU boot has no initrd or virtio disk support", + ))); + } + xnu::prepare_boot( + &mut shared_mem, + image, + &config.cmdline, + RAM_BASE, + ram_size as u64, + ) + } + Board::Virt => boot::prepare_boot( &mut shared_mem, image, config.initrd.as_deref(), @@ -403,22 +477,28 @@ pub fn boot_system( ram_size as u64, 1, has_virtio, - ) + ), } .map_err(Error::Boot)?; let console = Arc::new(Mutex::new(Vec::new())); let serial_console = console.clone(); - let uart_line = Arc::new(AtomicBool::new(false)); - let serial_line = uart_line.clone(); - let mut serial = Pl011::new(move |byte| { + let emit = move |byte: u8| { serial_console.lock().push(byte); sink(byte); - }) - .with_line(move |level| serial_line.store(level, Ordering::Release)); - let mut gic = if is_xnu { - Intc::V3(Gicv3::default()) + }; + let uart_line = Arc::new(AtomicBool::new(false)); + let serial_line = uart_line.clone(); + let (mut serial, mut soc, mut gic) = if apple { + (None, Some(m1::Soc::new(emit)), Intc::Apple) } else { - Intc::V2(Gicv2::default()) + let serial = + Pl011::new(emit).with_line(move |level| serial_line.store(level, Ordering::Release)); + let gic = if is_xnu { + Intc::V3(Gicv3::default()) + } else { + Intc::V2(Gicv2::default()) + }; + (Some(serial), None, gic) }; let virtio_line = Arc::new(AtomicBool::new(false)); let virtio_notify = virtio_line.clone(); @@ -427,6 +507,9 @@ pub fn boot_system( .with_line(move |level| virtio_notify.store(level, Ordering::Release)) }); let mut machine = machine::Machine::new(shared_mem); + if apple { + m1::identify(&mut machine.cpu.system); + } machine.cpu.pc = layout.entry; machine.cpu.x[0] = layout.boot_info; machine.cpu.x[1..4].fill(0); @@ -437,22 +520,32 @@ pub fn boot_system( if started.elapsed() >= timeout { break StopReason::Timeout; } - if uart_line.load(Ordering::Acquire) { - gic.raise(fdt::UART_INTID); - } else { - gic.lower(fdt::UART_INTID); - } - if virtio_line.load(Ordering::Acquire) { - gic.raise(fdt::VIRTIO_INTID); - } else { - gic.lower(fdt::VIRTIO_INTID); + match soc.as_mut() { + Some(soc) => soc.sync_lines(), + None => { + if uart_line.load(Ordering::Acquire) { + gic.raise(fdt::UART_INTID); + } else { + gic.lower(fdt::UART_INTID); + } + if virtio_line.load(Ordering::Acquire) { + gic.raise(fdt::VIRTIO_INTID); + } else { + gic.lower(fdt::VIRTIO_INTID); + } + } } - machine.irq_line = gic.signalled(0); + machine.irq_line = match &soc { + Some(soc) => soc.irq_pending(0), + None => gic.signalled(0), + }; + machine.fiq_line = gic.timer_fiq(machine.timer_due()); machine.vtimer_fiq = gic.vtimer_fiq(); let mut dev = SystemDevices { - serial: &mut serial, + serial: serial.as_mut(), gic: &mut gic, virtio: virtio.as_mut(), + soc: soc.as_mut(), }; match machine.run_with_devices(&mut dev) { Ok(machine::Exit::Timer) => { @@ -522,7 +615,7 @@ pub fn boot_system( if !is_xnu && machine.cpu.system.sctlr_el1 & 1 == 0 && sp != 0 - && !(RAM_BASE..=RAM_BASE + ram_size as u64).contains(&sp) + && !(ram_base..=ram_base + ram_size as u64).contains(&sp) { break StopReason::LostStack; } @@ -544,6 +637,7 @@ pub fn boot_system( executed_blocks: machine.blocks_run, timer_interrupts, unmapped_accesses: machine.unmapped, + unmodeled_device_accesses: soc.as_ref().map_or(0, |soc| soc.unmodeled_accesses()), stalled_at, instruction, last_fault_walk, @@ -585,41 +679,58 @@ fn instruction_diagnostic( } last } +/// The descriptors a translation of `address` reads, top level first, for the granule +/// and VA size the TCR selects. It follows `translate::walk` step for step, so the +/// report shows what the translation saw, for any granule. fn walk_diagnostic(cpu: &Cpu, memory: &impl GuestMemory, address: u64) -> Vec { let mut entries = Vec::new(); if address == 0 || cpu.system.sctlr_el1 & 1 == 0 { return entries; } - let upper = address >> 63 != 0; - let size = if upper { - (cpu.system.tcr_el1 >> 16) & 63 - } else { - cpu.system.tcr_el1 & 63 - }; - let granule = if upper { - (cpu.system.tcr_el1 >> 30) & 3 + let tcr = cpu.system.tcr_el1; + let (granule, va_bits, root) = if address >> 63 != 0 { + let granule = [0u32, 14, 12, 16][((tcr >> 30) & 3) as usize]; + let size = ((tcr >> 16) & 63) as u32; + (granule, 64 - size, cpu.system.ttbr1_el1) } else { - (cpu.system.tcr_el1 >> 14) & 3 + let granule = [12u32, 16, 14, 0][((tcr >> 14) & 3) as usize]; + let size = (tcr & 63) as u32; + (granule, 64 - size, cpu.system.ttbr0_el1) }; - if (!upper && granule != 0) || (upper && granule != 2) { + if granule == 0 || va_bits > 48 || va_bits <= granule { return entries; } - let bits = 64u64.saturating_sub(size); - let levels = bits.saturating_sub(12).div_ceil(9).clamp(1, 4) as u8; - let mut table = if upper { - cpu.system.ttbr1_el1 + let stride = granule - 3; + let levels = (va_bits - granule).div_ceil(stride); + if levels > 4 { + return entries; + } + let top_bits = va_bits - (granule + stride * (levels - 1)); + let root_alignment = if granule == 12 { + granule } else { - cpu.system.ttbr0_el1 - } & 0x0000_ffff_ffff_f000; - for level in 4 - levels..4 { - let index = (address >> (39 - 9 * level)) & 0x1ff; + (top_bits + 3).max(6) + }; + let address_mask = 0x0000_ffff_ffff_ffff & !((1u64 << granule) - 1); + let mut table = root & 0x0000_ffff_ffff_ffff & !((1u64 << root_alignment) - 1); + for depth in 0..levels { + // `translate::walk` counts levels from the leaf; the report counts from the top. + let from_leaf = levels - 1 - depth; + let shift = granule + stride * from_leaf; + let index_bits = if granule != 12 && depth == 0 { + top_bits + } else { + stride + }; + let index = (address >> shift) & ((1u64 << index_bits) - 1); + let at = table.wrapping_add(index * 8); let mut bytes = [0; 8]; let descriptor = memory - .read(table + index * 8, &mut bytes) + .read(at, &mut bytes) .ok() .map(|_| u64::from_le_bytes(bytes)); entries.push(WalkEntry { - level, + level: (4 - levels + depth) as u8, table, index, descriptor, @@ -627,10 +738,10 @@ fn walk_diagnostic(cpu: &Cpu, memory: &impl GuestMemory, address: u64) -> Vec Date: Wed, 7 Oct 2026 20:21:05 -0700 Subject: [PATCH 2/2] feat(emulator): add mh_filset support to xnu loader --- README.md | 5 +++-- src/emulator/src/aarch64/decode.rs | 3 +++ src/emulator/src/system/mod.rs | 13 ++++++----- src/emulator/src/system/xnu.rs | 35 ++++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3399e49..68bbd32 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,9 @@ at `0x2_3520_0000`. The machine is one vCPU with the M1 Icestorm identity The Linux kernel boots through the AIC and the S5L console, probes its power domains, and panics on the missing root filesystem (`VFS: Unable to mount root fs`), which is the expected result without one. `--initrd`, `--disk` and `--smp` above 1 are rejected -on this board, and a Mach-O kernel is refused: XNU needs the SoC's own ADT and -firmware interfaces, which are not modelled. +on this board. A kernelcache from the restore image (`MH_FILESET`) loads, with its ADT +as `--dtb`, and runs its first blocks; it does not get past the early boot CPU +table yet, and it needs PAuth and chained fixups before it can go further. Not modelled: any arm-io device other than the AIC, the UART and the power-state blocks. Those addresses read as zero and writes are dropped, and the boot report counts diff --git a/src/emulator/src/aarch64/decode.rs b/src/emulator/src/aarch64/decode.rs index 1f445dc..71dcd15 100644 --- a/src/emulator/src/aarch64/decode.rs +++ b/src/emulator/src/aarch64/decode.rs @@ -1124,6 +1124,9 @@ fn apple_register(op0: u32, op1: u32, crn: u32, crm: u32, op2: u32) -> Option Apple(19), (7, 0, 4) => Apple(20), (7, 6, 4) => AppleZero, + // The S3_5_c15_cN_0 state registers early init reads, none named in the XNU + // source tree. Read as zero until their roles are identified. + (5, 2..=7, 0) => AppleZero, _ => return None, }) } diff --git a/src/emulator/src/system/mod.rs b/src/emulator/src/system/mod.rs index c3da1b0..e13c5d1 100644 --- a/src/emulator/src/system/mod.rs +++ b/src/emulator/src/system/mod.rs @@ -435,12 +435,15 @@ pub fn boot_system( let has_virtio = config.disk.is_some(); let is_xnu = xnu::is_macho(image); let layout = match config.board { + Board::AppleM1 { device_tree } if is_xnu => xnu::prepare_boot_with_tree( + &mut shared_mem, + image, + &config.cmdline, + &device_tree, + ram_base, + ram_size as u64, + ), Board::AppleM1 { device_tree } => { - if is_xnu { - return Err(Error::Boot(boot::Error::Unsupported( - "XNU boot on the Apple M1 SoC is not supported yet", - ))); - } if has_virtio || config.initrd.is_some() { return Err(Error::Boot(boot::Error::Unsupported( "the Apple M1 SoC has no initrd or virtio disk support yet", diff --git a/src/emulator/src/system/xnu.rs b/src/emulator/src/system/xnu.rs index f1a7115..4ee834a 100644 --- a/src/emulator/src/system/xnu.rs +++ b/src/emulator/src/system/xnu.rs @@ -15,6 +15,9 @@ use crate::memory::GuestMemory; const MH_MAGIC_64: u32 = 0xfeed_facf; const CPU_TYPE_ARM64: u32 = 0x0100_000c; const MH_EXECUTE: u32 = 2; +/// A kernel collection: one top-level header whose load commands list every segment of +/// every component, with `LC_FILESET_ENTRY` naming each component's own Mach-O header. +const MH_FILESET: u32 = 12; const LC_UNIXTHREAD: u32 = 0x5; const LC_SEGMENT_64: u32 = 0x19; const LC_MAIN: u32 = 0x8000_0028; @@ -85,8 +88,8 @@ pub fn parse(image: &[u8]) -> Result { if word(4) != CPU_TYPE_ARM64 { return Err(bad("not an arm64 Mach-O")); } - if word(12) != MH_EXECUTE { - return Err(bad("not an executable Mach-O")); + if word(12) != MH_EXECUTE && word(12) != MH_FILESET { + return Err(bad("not an executable or fileset Mach-O")); } let commands_end = HEADER_SIZE .checked_add(word(20) as usize) @@ -205,6 +208,20 @@ pub fn prepare_boot( cmdline: &str, ram_base: u64, ram_size: u64, +) -> Result { + let tree = super::afdt::build(ram_base, ram_size, cmdline); + prepare_boot_with_tree(memory, image, cmdline, &tree, ram_base, ram_size) +} + +/// [`prepare_boot`] with the device tree the caller supplies, such as the platform's +/// own ADT for the board. +pub fn prepare_boot_with_tree( + memory: &mut impl GuestMemory, + image: &[u8], + cmdline: &str, + tree: &[u8], + ram_base: u64, + ram_size: u64, ) -> Result { let kernel = parse(image)?; let ram_end = ram_base.checked_add(ram_size).ok_or(Error::NoRoom)?; @@ -235,7 +252,6 @@ pub fn prepare_boot( } let device_tree = align(kernel_end, PAGE)?; - let tree = super::afdt::build(ram_base, ram_size, cmdline); let args_at = align( device_tree .checked_add(tree.len() as u64) @@ -272,7 +288,7 @@ pub fn prepare_boot( .write(physical(segment.vmaddr)?, data) .map_err(Error::Memory)?; } - memory.write(device_tree, &tree).map_err(Error::Memory)?; + memory.write(device_tree, tree).map_err(Error::Memory)?; memory.write(args_at, &args).map_err(Error::Memory)?; Ok(Layout { entry, @@ -443,4 +459,15 @@ mod tests { Err(Error::Unsupported(_)) )); } + + #[test] + fn a_fileset_kernel_parses_like_an_executable_with_the_same_segments() { + let executable = image(Entry::Main(0x800)); + let mut fileset = executable.clone(); + fileset[12..16].copy_from_slice(&MH_FILESET.to_le_bytes()); + assert_eq!(parse(&fileset).unwrap(), parse(&executable).unwrap()); + let mut other = executable; + other[12..16].copy_from_slice(&1u32.to_le_bytes()); + assert!(matches!(parse(&other), Err(Error::BadMachO(_)))); + } }