Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ volt-ir = { path = "src/ir" }
volt-target = { path = "src/target" }
volt-emulator = { path = "src/emulator" }

object = { version = "0.40.0", default-features = false, features = ["read_core", "write_core", "elf"] }
object = { version = "0.40.0", default-features = false, features = ["read_core", "write_core", "elf", "macho"] }
libc = "0.2"
parking_lot = "0.12"
winnow-args = "0.1"
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ The disk form supplies `root=/dev/vda rw` by default. `--cpus`, `--idle`,
`--fast`, and `--spin` control the virtual CPU count and WFI policy. Use
`volt-boot --help` for the complete generated interface.

## XNU boot

`volt-boot` also boots an XNU kernel. A Mach-O image is detected by its magic, so no
flag is needed:

```sh
cargo run --release --bin volt-boot -- \
path/to/kernel.development.qemu 20 --fast
```

The loader follows the `bootxnu` U-Boot command: it places the Mach-O segments at a
`physBase` that is congruent to the link-time `virtBase` modulo a 32 MiB L2 block,
builds an Apple flattened device tree (the equivalent of `mkafdt.py`) and a `boot_args`
block, and enters at the kernel entry with the MMU off and `x0` pointing at
`boot_args`. The machine is the QEMU `virt` layout XNU's QEMU platform expects:
PL011 `uart0` at `0x09000000`, and a GICv3 (distributor `0x08000000`, redistributor
`0x080a0000`, `ICC_*` system registers) that forwards the virtual timer as a FIQ. The
last 512 KiB of RAM is the panic log (`chosen/pram`), and `memSize` excludes it.

Defaults for a Mach-O kernel are 1 GiB of RAM and the boot-args
`-v serial=3 debug=0x14e keepsyms=1 serial-device-name=uart0`. To pass your own, the
first word must not start with a dash (`serial=3 -v ...`), or it is read as a flag.
`--initrd`, `--disk` and `--cpus` above 1 are rejected for XNU.

The bare kernel boots through the pmap and VM bootstrap, zone and IOKit start-up, the
scheduler and IONVRAM, and then panics in `read_random`: the corecrypto kext that
registers the kernel PRNG is not part of the kernel image. That is the kernel's own
behavior on any machine, and the panic message and backtrace appear on the console.
Going further needs a kernelcache with corecrypto prelinked.

Not modelled: privileged-access-never is stored and saved in `SPSR_EL1` but not
enforced by translation, the physical timer is stored but never fires, floating point
rounds to nearest even and sets no `FPSR` flags, and the GICv3 carries only the
virtual timer (no SPIs, SGIs or group 1).

## Performance

Translating a block costs much more than running it once, and a kernel boot runs most
Expand Down
24 changes: 16 additions & 8 deletions src/emulator/cli/src/boot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Raw Linux arm64 Image boot through Volt's full-system JIT with VirtIO-Blk.
//! Raw Linux arm64 Image, or XNU Mach-O kernel, boot through Volt's full-system JIT.

use std::{
io::Write,
Expand All @@ -7,7 +7,10 @@ use std::{
time::Duration,
};
use volt_emulator::devices::BlockBackend;
use volt_emulator::system::{DEFAULT_CMDLINE, IdleMode, RAM_SIZE, SystemConfig, smp::SmpConfig};
use volt_emulator::system::{
DEFAULT_CMDLINE, DEFAULT_XNU_CMDLINE, IdleMode, RAM_SIZE, SystemConfig, XNU_RAM_SIZE,
smp::SmpConfig,
};
use winnow_args::{Args, ValueEnum};

#[derive(ValueEnum, Clone, Copy, Debug, Default, PartialEq, Eq)]
Expand All @@ -33,18 +36,18 @@ impl From<IdleModeArg> for IdleMode {
#[derive(Args, Debug)]
#[arg(
name = "volt-boot",
about = "AArch64 Linux boot with PL011, GICv2, and VirtIO-Blk"
about = "AArch64 Linux or XNU boot with PL011, GICv2/GICv3, and VirtIO-Blk (Linux)"
)]
struct Cli {
/// Path to the raw Linux AArch64 Image.
/// Path to a raw Linux AArch64 Image, or an XNU kernel (Mach-O).
#[arg(positional)]
image: PathBuf,

/// Timeout in seconds (default: 20).
#[arg(positional)]
seconds: Option<u64>,

/// Kernel command line.
/// Kernel command line (Linux) or boot-args (XNU; start with a non-dash word).
#[arg(positional)]
cmdline: Option<String>,

Expand Down Expand Up @@ -189,22 +192,27 @@ fn run(cli: Cli) -> Result<(), String> {
};

let seconds = cli.seconds.unwrap_or(20);
let default_cmdline = if cli.disk.is_some() {
let image = std::fs::read(&cli.image).map_err(|e| format!("{}: {e}", cli.image.display()))?;
// 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 cli.disk.is_some() {
"root=/dev/vda rw console=ttyAMA0 earlycon=pl011,0x9000000 nokaslr loglevel=8"
} else {
DEFAULT_CMDLINE
};
let cmdline = cli.cmdline.unwrap_or_else(|| default_cmdline.to_string());

let image = std::fs::read(&cli.image).map_err(|e| format!("{}: {e}", cli.image.display()))?;

let initrd_data = match &cli.initrd {
Some(path) => Some(load_initrd(path)?),
None => None,
};

let default_ram = if let Some(initrd) = &initrd_data {
RAM_SIZE.max((initrd.len() * 3 + (128 << 20)).next_power_of_two())
} else if xnu {
XNU_RAM_SIZE
} else {
RAM_SIZE
};
Expand Down
11 changes: 10 additions & 1 deletion src/emulator/src/aarch64/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,17 @@ impl Cache {
let word = u32::from_le_bytes(room.try_into().unwrap());
let instruction = match decode(word) {
Ok(insn) => insn,
// Run the decoded prefix first: the undecodable word then heads the
// next block, where it is reported at its own address.
Err(_) if count > 4 => {
count -= 4;
break;
}
Err(_) => {
eprintln!("cache decode failure at pc {at:x}: word {word:08x}");
// Architecturally undefined words are the guest's to handle.
if !super::decode::is_undefined(word) {
eprintln!("cache decode failure at pc {at:x}: word {word:08x}");
}
return Err(Error::Decode { word, pc: at });
}
};
Expand Down
Loading
Loading