Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ 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. 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
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
Expand Down
25 changes: 22 additions & 3 deletions src/emulator/cli/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -36,7 +36,7 @@ impl From<IdleModeArg> 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).
Expand All @@ -59,6 +59,11 @@ struct Cli {
#[arg(short, long, alias = "initramfs")]
initrd: Option<PathBuf>,

/// 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<PathBuf>,

/// Guest RAM size (e.g. 128M, 256M, 512M, 1G).
#[arg(short, long, alias = "ram")]
memory: Option<String>,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
};
Expand All @@ -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,
Expand Down Expand Up @@ -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| {
Expand Down
11 changes: 9 additions & 2 deletions src/emulator/src/aarch64/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -2464,6 +2467,10 @@ fn system_offset(r: SystemRegister) -> Option<usize> {
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,
})
}
Expand Down Expand Up @@ -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));
}
Expand Down
11 changes: 11 additions & 0 deletions src/emulator/src/aarch64/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}
}
Expand Down
70 changes: 61 additions & 9 deletions src/emulator/src/aarch64/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -814,7 +819,7 @@ pub enum Instruction {
Sevl,
Wfe,
Yield,
Trap,
Brk(u16),
B(i64),
BCond(BranchCondition),
Csel(Select),
Expand Down Expand Up @@ -894,7 +899,7 @@ impl Instruction {
| Psci
| Wfi
| Wfe
| Trap
| Brk(_)
| Isb
| DcZva(_)
| AddressTranslate(_)
Expand Down Expand Up @@ -1081,6 +1086,51 @@ fn simd_struct(word: u32) -> Result<Option<(StructDesc, StructPost)>, 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<SystemRegister> {
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,
// 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,
})
}

fn system_register(
op0: u32,
op1: u32,
Expand All @@ -1092,6 +1142,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);
}
Expand Down Expand Up @@ -1753,12 +1806,10 @@ pub fn decode(word: u32) -> Result<Instruction, DecodeError> {
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;
Expand Down Expand Up @@ -2493,7 +2544,8 @@ mod tests {
(0xd69f03e0, Eret),
(0xd4000001, Svc),
(0xd4000002, Psci),
(0xd4200020, Trap),
(0xd4200020, Brk(1)),
(0xd4210000, Brk(0x800)),
] {
assert_eq!(decode(word), Ok(expected));
}
Expand Down
8 changes: 7 additions & 1 deletion src/emulator/src/aarch64/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,13 +25,16 @@ impl Default for Reason {
ec: 0,
instruction: false,
status: status::TRANSLATION,
iss: 0,
}
}
}
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;
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions src/emulator/src/aarch64/identification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
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,
Expand All @@ -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);
Expand Down
Loading
Loading