diff --git a/CHANGELOG.md b/CHANGELOG.md index f25d83bfe..0eb0c371d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## [Prerelease] - Unreleased ### Added +* ARM64 guests support CPUs without LSE atomics when built with `-C target-feature=-lse`. * Add `HYPERLIGHT_SURROGATE_EXTRACT_DIR` to configure the surrogate extraction directory on Windows. * Add `Sandbox::recreate()`, which rebuilds a sandbox's VM and memory from a snapshot. It recovers unrecoverable sandboxes and keeps configuration, host functions, and interrupt handles by @ludfjig in https://github.com/hyperlight-dev/hyperlight/pull/1751 * Add ARM64 Linux MSHV support by @cshung in https://github.com/hyperlight-dev/hyperlight/pull/1626 diff --git a/Justfile b/Justfile index efbe8460a..51633401f 100644 --- a/Justfile +++ b/Justfile @@ -79,7 +79,7 @@ build-and-move-c-guests: (build-c-guests "debug") (move-c-guests "debug") (build # Uses plain cargo with non-PIE link flags and the sysroot of the PIE build. # cargo-hyperlight would leak these RUSTFLAGS into its sysroot wrapper build. build-rust-guests-non-pie target=default-target: (build-rust-guests target) - {{ if os() == "windows" { "$env:RUSTC_BOOTSTRAP=1; $env:RUSTFLAGS='--sysroot=' + (Resolve-Path src/tests/rust_guests/target/sysroot).Path + ' -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint';" } else { "" } }} cd src/tests/rust_guests/simpleguest && {{ if os() == "windows" { "" } else { "RUSTC_BOOTSTRAP=1 RUSTFLAGS=\"--sysroot=$(cd .. && pwd)/target/sysroot -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint\"" } }} cargo build --bin simpleguest --target {{ hyperlight-target }} --target-dir ../target-non-pie/build --profile={{ if target == "debug" { "dev" } else { target } }} + {{ if os() == "windows" { "$env:RUSTC_BOOTSTRAP=1; $env:RUSTFLAGS=$env:RUSTFLAGS + ' --sysroot=' + (Resolve-Path src/tests/rust_guests/target/sysroot).Path + ' -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint';" } else { "" } }} cd src/tests/rust_guests/simpleguest && {{ if os() == "windows" { "" } else { "RUSTC_BOOTSTRAP=1 RUSTFLAGS=\"${RUSTFLAGS:-} --sysroot=$(cd .. && pwd)/target/sysroot -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint\"" } }} cargo build --bin simpleguest --target {{ hyperlight-target }} --target-dir ../target-non-pie/build --profile={{ if target == "debug" { "dev" } else { target } }} non_pie_guests_target := "src/tests/rust_guests/target-non-pie/build/" + hyperlight-target diff --git a/docs/how-to-build-a-hyperlight-guest-binary.md b/docs/how-to-build-a-hyperlight-guest-binary.md index 66b7b2da0..f8fe7a4d0 100644 --- a/docs/how-to-build-a-hyperlight-guest-binary.md +++ b/docs/how-to-build-a-hyperlight-guest-binary.md @@ -26,6 +26,29 @@ only a host crate. The rest of this document explains what the generated guest contains and how to build one by hand. +## Raspberry Pi 4 and ARM64 CPUs without LSE + +The default ARM64 guest target requires ARMv8.1 LSE atomics. The Pi 4's +Cortex-A72 does not support them. Disable LSE for the guest and its dependencies: + +```sh +RUSTFLAGS="-C target-feature=-lse" cargo hyperlight build +``` + +To build this repository's Rust and C test guests on ARM64: + +```sh +RUSTFLAGS="-C target-feature=-lse" just guests +just test +``` + +The physical-page allocator uses an exclusive load/store retry loop when LSE +is disabled. Default builds keep the LSE instruction. The loop can cost more +under contention. The host does not need this flag. + +The Pi needs a 64-bit Linux kernel with readable and writable `/dev/kvm`. +Use a 64-bit userspace or a 64-bit build environment. + ## Rust guest binary ### Minimal guest diff --git a/src/hyperlight_guest/src/arch/aarch64/prim_alloc.rs b/src/hyperlight_guest/src/arch/aarch64/prim_alloc.rs index e69f6e1e8..a865029f8 100644 --- a/src/hyperlight_guest/src/arch/aarch64/prim_alloc.rs +++ b/src/hyperlight_guest/src/arch/aarch64/prim_alloc.rs @@ -11,9 +11,10 @@ use hyperlight_common::vmem; pub unsafe fn alloc_phys_pages(n: u64) -> u64 { let addr = crate::layout::allocator_gva(); let nbytes = n * vmem::PAGE_SIZE as u64; - let mut prev_base: u64 = 0; + let prev_base: u64; + // SAFETY: The caller guarantees initialized allocator state with a writable, aligned counter. unsafe { - // todo: actually check for FEAT_LSE presence. + #[cfg(target_feature = "lse")] core::arch::asm!(" ldadd {nbytes}, {prev_base}, [{addr}] ", @@ -21,6 +22,20 @@ pub unsafe fn alloc_phys_pages(n: u64) -> u64 { nbytes = in(reg) nbytes, prev_base = out(reg) prev_base, ); + #[cfg(not(target_feature = "lse"))] + core::arch::asm!(" + 2: + ldxr {prev_base}, [{addr}] + add {next}, {prev_base}, {nbytes} + stxr {status:w}, {next}, [{addr}] + cbnz {status:w}, 2b + ", + addr = in(reg) addr, + nbytes = in(reg) nbytes, + prev_base = out(reg) prev_base, + next = out(reg) _, + status = out(reg) _, + ); } let limit = crate::layout::allocator_limit_gpa(); if prev_base.checked_add(nbytes).is_none_or(|end| end > limit) {