From abd5dee7079d68525db9f817d2e11a7bbcf210f8 Mon Sep 17 00:00:00 2001 From: Gautam Ahuja <47530525+ahuja-gautam@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:46:44 -0800 Subject: [PATCH 1/3] arm64: add native cold-boot foundation Add the ARM64 kernel, platform and counter bindings to the existing shared guest binaries and cold-boot CLI. Reconcile the validated foundation with the selected NVX baseline while retaining its upstream fixes. Keep x86 defaults and behavior, architecture-qualified producer inputs, native artifact admission, and the cold-boot-only ARM contract. Managed artifact admission and layered sandbox enablement follow separately. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- doc/build.md | 163 ++ doc/usage.md | 57 +- docker/Dockerfile | 53 +- guest/alpine/nvx-container-enter | 13 +- guest/common/nvx-init-agent | 41 +- kernel/config-microvm-arm64 | 2086 +++++++++++++++++ kernel/config-microvm-arm64.derived | 496 ++++ .../arm64/0001-nvx-platform-mmio.patch | 291 +++ .../arm64/0002-nvx-shared-status-of.patch | 92 + nvx-rs/README.md | 91 +- nvx-rs/build.rs | 3 + nvx-rs/public-api/linux.txt | 7 + nvx-rs/public-api/windows.txt | 7 + nvx-rs/src/bin/nvx-exit.rs | 13 +- nvx-rs/src/bin/nvx-init.rs | 13 +- nvx-rs/src/bin/nvx-time.rs | 13 +- nvx-rs/src/guest/entry/tests.rs | 28 +- nvx-rs/src/guest/exit.rs | 30 +- nvx-rs/src/guest/exit/tests.rs | 6 + nvx-rs/src/guest/init.rs | 67 +- nvx-rs/src/guest/init/tests.rs | 102 + nvx-rs/src/guest/kernel_text.rs | 5 + nvx-rs/src/guest/managed_agent/seam.rs | 6 +- nvx-rs/src/guest/managed_agent/tests.rs | 26 +- nvx-rs/src/guest/mappings/mapper/tests.rs | 1 + nvx-rs/src/guest/mod.rs | 17 +- nvx-rs/src/guest/parse.rs | 4 + nvx-rs/src/guest/reseed/tests.rs | 1 + nvx-rs/src/guest/root.rs | 26 +- nvx-rs/src/guest/root/block.rs | 55 +- nvx-rs/src/guest/root/block/platform.rs | 188 ++ nvx-rs/src/guest/root/block/platform/tests.rs | 217 ++ nvx-rs/src/guest/root/kernel.rs | 2 + nvx-rs/src/guest/root/tests.rs | 41 +- nvx-rs/src/guest/status.rs | 9 + nvx-rs/src/guest/sys/aarch64.rs | 40 + nvx-rs/src/guest/sys/aarch64/tests.rs | 13 + nvx-rs/src/guest/sys/clock.rs | 12 +- nvx-rs/src/guest/sys/discipline.rs | 7 +- nvx-rs/src/guest/sys/fd.rs | 16 +- nvx-rs/src/guest/sys/fd/tests.rs | 17 + nvx-rs/src/guest/sys/filesystem.rs | 9 +- nvx-rs/src/guest/sys/ioctl.rs | 1 + nvx-rs/src/guest/sys/kernel.rs | 17 +- nvx-rs/src/guest/sys/kernel/tests.rs | 1 - nvx-rs/src/guest/sys/mapping.rs | 77 + nvx-rs/src/guest/sys/mod.rs | 77 +- nvx-rs/src/guest/sys/platform.rs | 345 +++ nvx-rs/src/guest/sys/platform/binding.rs | 104 + .../src/guest/sys/platform/binding/tests.rs | 44 + nvx-rs/src/guest/sys/platform/tests.rs | 134 ++ nvx-rs/src/guest/sys/poll.rs | 6 +- nvx-rs/src/guest/sys/process.rs | 37 +- nvx-rs/src/guest/sys/process/tests.rs | 9 + nvx-rs/src/guest/sys/scheduler.rs | 4 + nvx-rs/src/guest/sys/signal.rs | 11 +- nvx-rs/src/guest/sys/signal/tests.rs | 2 + nvx-rs/src/guest/sys/socket.rs | 1 + nvx-rs/src/guest/sys/spawn.rs | 2 +- nvx-rs/src/guest/sys/spawn/tests.rs | 6 +- nvx-rs/src/guest/sys/tests.rs | 6 +- nvx-rs/src/guest/time/api.rs | 202 +- nvx-rs/src/guest/time/api/tests.rs | 2 + nvx-rs/src/guest/time/checks.rs | 90 +- nvx-rs/src/guest/time/checks/tests.rs | 1 + nvx-rs/src/guest/time/checks_arm64.rs | 239 ++ nvx-rs/src/guest/time/checks_arm64/tests.rs | 40 + nvx-rs/src/guest/time/commands.rs | 165 +- nvx-rs/src/guest/time/daemon.rs | 444 ++-- nvx-rs/src/guest/time/daemon/tests.rs | 336 +-- nvx-rs/src/guest/time/daemon/tests/x86.rs | 283 +++ nvx-rs/src/guest/time/decoders.rs | 377 +-- nvx-rs/src/guest/time/decoders/tests.rs | 2 + nvx-rs/src/guest/time/deferred.rs | 70 + nvx-rs/src/guest/time/events.rs | 127 +- nvx-rs/src/guest/time/events/tests.rs | 35 +- nvx-rs/src/guest/time/mod.rs | 173 +- nvx-rs/src/guest/time/sample.rs | 516 ++++ nvx-rs/src/guest/time/sample/arm64.rs | 241 ++ nvx-rs/src/guest/time/sample/arm64/tests.rs | 61 + nvx-rs/src/guest/time/sample/tests.rs | 215 ++ nvx-rs/src/guest/time/state.rs | 82 +- nvx-rs/src/guest/time/state/tests.rs | 13 + nvx-rs/src/guest/time/system.rs | 416 +++- nvx-rs/src/guest/time/system/tests.rs | 2 + nvx-rs/src/guest/time/test_mode.rs | 2 +- nvx-rs/src/guest/time/tests.rs | 35 + nvx-rs/src/host/artifacts/architecture.rs | 50 + .../src/host/artifacts/architecture/tests.rs | 162 ++ nvx-rs/src/host/artifacts/dir.rs | 107 +- nvx-rs/src/host/artifacts/identity.rs | 77 + nvx-rs/src/host/artifacts/mod.rs | 4 + nvx-rs/src/host/cli/download.rs | 15 +- nvx-rs/src/host/cli/download/tests.rs | 25 +- nvx-rs/src/host/cli/run.rs | 40 +- nvx-rs/src/host/cli/run/tests.rs | 65 + nvx-rs/tests/cli.rs | 82 +- nvx-rs/tests/cli/download.rs | 26 + nvx-rs/tests/cli/harness.rs | 15 +- nvx-rs/tests/cli/one_shot.rs | 5 +- nvx-rs/tests/guest_tools.rs | 36 + scripts/nvx.py | 123 +- scripts/nvx_tools/benchmark.py | 6 +- scripts/nvx_tools/build.py | 394 +++- scripts/nvx_tools/build_config.py | 92 +- scripts/nvx_tools/build_constants.py | 29 + scripts/nvx_tools/collect_ubuntu_sources.py | 23 +- scripts/nvx_tools/elf.py | 45 +- scripts/nvx_tools/guest_binaries.py | 139 +- scripts/nvx_tools/kernel_config.py | 143 +- scripts/nvx_tools/kernel_features.py | 165 +- scripts/nvx_tools/microvm_tests.py | 2 + scripts/nvx_tools/time_abi.py | 58 +- scripts/nvx_tools/ubuntu.py | 124 +- scripts/test-layout-allowlist.json | 19 +- scripts/test_guest_binaries.py | 377 ++- scripts/test_kernel_config.py | 229 +- scripts/test_microvm_tests.py | 15 + scripts/test_nvx_tools.py | 172 +- scripts/test_time_abi.py | 83 +- ubuntu/packages-arm64.lock.json | 113 + 121 files changed, 11021 insertions(+), 1476 deletions(-) create mode 100644 kernel/config-microvm-arm64 create mode 100644 kernel/config-microvm-arm64.derived create mode 100644 kernel/patches/arm64/0001-nvx-platform-mmio.patch create mode 100644 kernel/patches/arm64/0002-nvx-shared-status-of.patch create mode 100644 nvx-rs/src/guest/root/block/platform.rs create mode 100644 nvx-rs/src/guest/root/block/platform/tests.rs create mode 100644 nvx-rs/src/guest/sys/aarch64.rs create mode 100644 nvx-rs/src/guest/sys/aarch64/tests.rs create mode 100644 nvx-rs/src/guest/sys/platform.rs create mode 100644 nvx-rs/src/guest/sys/platform/binding.rs create mode 100644 nvx-rs/src/guest/sys/platform/binding/tests.rs create mode 100644 nvx-rs/src/guest/sys/platform/tests.rs create mode 100644 nvx-rs/src/guest/time/checks_arm64.rs create mode 100644 nvx-rs/src/guest/time/checks_arm64/tests.rs create mode 100644 nvx-rs/src/guest/time/daemon/tests/x86.rs create mode 100644 nvx-rs/src/guest/time/deferred.rs create mode 100644 nvx-rs/src/guest/time/sample.rs create mode 100644 nvx-rs/src/guest/time/sample/arm64.rs create mode 100644 nvx-rs/src/guest/time/sample/arm64/tests.rs create mode 100644 nvx-rs/src/guest/time/sample/tests.rs create mode 100644 nvx-rs/src/host/artifacts/architecture.rs create mode 100644 nvx-rs/src/host/artifacts/architecture/tests.rs create mode 100644 ubuntu/packages-arm64.lock.json diff --git a/doc/build.md b/doc/build.md index 3e6832a59..2c95156c7 100644 --- a/doc/build.md +++ b/doc/build.md @@ -47,6 +47,169 @@ Cargo invocation builds all ten programs, and every result must pass one digest over the `nvx` crate, the shared Python build recipe, and `rust-toolchain.toml`, and each binary SHA-256. +The guest-binary commands also accept `--architecture aarch64` to select +`aarch64-unknown-linux-musl` explicitly; omitting it preserves the x86 target +and manifest. Use a separate output directory, such as +`build/aarch64/guest-binaries`: a rebuild refuses an output owned by another +architecture. The ARM manifest records `architecture` and its target, and +staging and inspection verify each executable's ELF machine identity as well +as its static linkage and stack permissions. `check-guest-binaries` and +`test-guest-binaries` take the same architecture option; their tests require +a host capable of running the selected Linux target. Target selection alone +does not establish ARM guest-platform support or native microVM qualification. + +The existing Docker `guest-binaries` stage accepts +`--build-arg NVX_ARCHITECTURE=aarch64`. Its `guest-binary-artifacts` +target uses BuildKit's existing local output. Select `--platform linux/arm64` +and a separate output directory for ARM builds. The seven-helper cold-boot +inventory includes the real ARM `nvx-time` entry point; missing required +helpers and stale diagnostic exports fail source/manifest inspection. +Compilation and ELF identity do not establish native product qualification. +The architecture argument is not a CPU-profile +selector. An ARM platform flag on an x86 engine may use emulation and is not +native build evidence. + +The ARM cold-boot inventory uses that same producer without the +snapshot-only MMIO kick helper, device-I/O benchmark helper, or x86 time +probe. It still requires real `nvx-time` before normal `/init` can announce +readiness. Omitting unrelated diagnostics neither omits boot-time enforcement +nor qualifies ARM counter/timer probes. The default x86 inventory remains all +ten executables. + +Existing x86-only production helpers remain available to shared unit tests +on ARM through their architecture-or-test compilation guards. The exact +guards are recorded in the test-layout allowlist as non-movable seams; test +implementations still live in separate test modules, and neither lint +warnings nor tests are disabled. + +The ARM time helper uses only the ISA, Linux interfaces and existing machine +binding: a6/a7/a8, the shared wire-v1 decoder, full-Hz counter pairing, and the +same clock-discipline policy, state format and watcher rules. It performs the +initial bounded UTC step before boot returns, then runs operational per-CPU +counter/timer checks at deferred idle priority with the existing promotion +deadline. It has no hypervisor-vendor, host-provider or feature-bank branches. +The VMM still owns pre-entry CPU-profile admission; these guest checks neither +admit an unapproved profile nor prove privileged raw cache registers. +Snapshot/restore commands are not exposed by the ARM cold-boot helper. + +The ARM kernel enables `ARM64_PTR_AUTH` for ordinary userspace pointer +authentication when the selected CPU profile exposes it. Linux initializes +process keys at exec and handles context switches through its existing PAC +support; no NVX key broker or new guest ABI is needed. Kernel return signing +(`ARM64_PTR_AUTH_KERNEL`) remains disabled, stack protection stays enabled, +and guest Rust helper branch-protection/instrumentation policy is unchanged. +Native PAC capability and a successful build alone do not prove guest PAC +execution or future snapshot key preservation. +`nvx-rs run --architecture aarch64 --cpu-profile auto|host` forwards the +selection to OpenVMM's common CPU/time admission path. `auto` selects the +approved built-in profile for a supported CPU; `host` derives a separate +canonical policy-filtered native profile, not a renamed built-in profile. +The guest consumes the selected ID/digest and actual counter frequency +through the same machine binding; it embeds neither profile's constants +and does not select a hypervisor-specific path. +The unchanged persisted time-state/boot-marker field `tsc_hz` holds the +actual architected-counter Hz on ARM; `lapic_hz=0` means no LAPIC. The ARM +sample command names `counter_hz` explicitly and reports the ID/digest read +from a8. The shared harness selects ISA-specific counter validation from +the normal CLI architecture option, never from a hypervisor-specific rate. +Every time-helper invocation resolves the kernel's +`nvx_time_abi=report-only` token before command dispatch, so a later plain +`status` cannot relabel a report-only boot as enforcing. The shared +qualification monitor rejects report-only mode even when its checks pass. + +ARM64 development kernel builds use the same stages and source pin: +`kernel-config --architecture aarch64` generates +`kernel/config-microvm-arm64` and its source-backed derived record; +`arm64-kernel-artifacts`, with `NVX_ARCHITECTURE=aarch64` and +`--platform linux/arm64`, exports `Image`, its configuration and provenance +under a separate output directory. The ARM inventory retains common product +capabilities and checks the actual resolved configuration for x86 leakage, +unnecessary explicit additions and unowned derived symbols. The NVX HVC +frontend selects the generic serial-core/early-console infrastructure, not +a PL011 or 8250 device. + +The actual ARM configuration retains `EFI_PARTITION` as the common GPT disk +parser, not firmware boot. Pinned Linux's ARM64 Kconfig unconditionally selects +the hidden `HAS_IOPORT` capability; with PCI disabled, `NO_IOPORT_MAP=y` and +`HAS_IOPORT_MAP` remains disabled. These names do not enable x86 boot or active +PIO devices. The source-backed audit rejects unknown enabled symbols rather +than treating them as hidden defaults. + +The shared initramfs producer also selects the pinned AArch64 Alpine archive, +native musl loader and package identities with +`build-initramfs --architecture aarch64`. Its work, output and provenance stay +under `build/aarch64`; the `arm64-artifacts` BuildKit export pairs it with +`Image`. `--guest ubuntu` uses the pinned ARM64 Ubuntu Base archive and the +separate `ubuntu/packages-arm64.lock.json`, preserving supplemental source +versions while checking ARM-specific binary hashes and dependencies. +`arm64-ubuntu-artifacts` exports that pair. Required guest-helper inventory, +ISA, digest and source checks still reject incomplete or stale inputs; these producers do +not add qualified ARM releases or relax runtime admission. +The normal `build-guest --architecture aarch64 --guest alpine|ubuntu` +command selects these same producers and namespaces, adding `--native` on +Linux to avoid Docker. ARM rejects the x86 debug-kernel and all-guests +distro-layer modes instead of mixing their outputs. +The existing Alpine one-shot container entry selects its control guest's +fixed x86-64 or AArch64 musl loader; it keeps the same namespace and +privilege-drop policy and refuses other control ISAs. + +For a normal local ARM cold boot, `nvx-rs run` defaults to the native host +ISA; `--architecture aarch64` remains an explicit override. It resolves +`build/aarch64/Image` and the matching initramfs/package manifest, without +falling back to x86 files or downloading an unqualified ARM release. It +rejects snapshot, memory-activation and Azure Linux modes. The approved ARM +microVM profile has a 36-bit GPA address space, not 64 GiB of RAM: the fixed +3-4 GiB MMIO aperture leaves at most 63 GiB of logical RAM, before other +reservations. This does not select or approve the remaining CPU-profile +values. On Windows, ARM runs also require an ARM64 PE OpenVMM executable +before any process is launched. Existing x86 paths and default selection +remain unchanged. +This native runtime default does not change the build commands' x86-64 +default or the existing artifact API's explicit architecture selection. + +**Deferred ARM cacheinfo metadata.** Phase 1 defers complete Linux sysfs +cache geometry and sharing metadata, an existing upstream Standard ARM +direct-boot gap. This supersedes only the complete-cacheinfo acceptance +requirement, not functional CPU/ISA/address checks, clock/counter/timer +checks, cache-operation/coherency checks, readiness or teardown. + +The pinned Linux ARM cacheinfo code discovers levels and types through +CLIDR; geometry and explicit sharing/LLC identity depend on standard OF +cache descriptions that the current product DT does not supply. Retries +can expose partial `indexN` nodes with missing numeric attributes. Linux's +generic higher-level all-shared fallback is not a measured sharing graph +or proof of valid LLC identity. The optional OpenVMM DT cache helper stays +unused: no informational geometry or virtual-sharing policy is selected. +Do not fabricate descriptors or introduce a raw-register ABI to fill this +metadata gap. + +Existing upstream startup logs in +[microsoft/openvmm#1399](https://github.com/microsoft/openvmm/issues/1399) +contain `cacheinfo: Unable to detect cache hierarchy for CPU 0`, then +`Run /init as init process`: the +[Linux 6.1 log](https://github.com/microsoft/openvmm/issues/1399#issuecomment-2903459559) +warns at 0.004064 and 1.098314 seconds and reaches `/init` at 1.284990; +the [Linux 6.15 log](https://github.com/microsoft/openvmm/issues/1399#issuecomment-2938965914) +warns at 0.006713 seconds and reaches `/init` at 2.103507. These are upstream +observations, not NVX product qualification or evidence that functional +cache/coherency checks passed. + +When native NVX qualification is admitted, preserve the unfiltered boot +warnings and each online CPU's actual cache `indexN` fields: level, type, +line size, size, sets, ways, ID and shared CPU list, including missing or +zero fields and read errors. Do not blanket-suppress errors or report +fallback metadata as complete. Missing cacheinfo metadata alone no longer +blocks acceptance; all retained functional checks and CPU/time admission +still apply. No native NVX cacheinfo observations are claimed here. + +ARM CPU and time configuration share OpenVMM's **same `ProtoPartitionConfig` +and existing optional `time_abi` contract/backend-interface path**, with an +ARM-specific payload consumed through `Partition::time_abi`. There is no +parallel CPU-only configuration or time service. The development machine +binding does not freeze a CPU profile, waive admission/readback requirements +or qualify native execution; incomplete ARM time helpers are not product +artifacts. + The native build keeps the Linux archive, the Ubuntu Base archive, and the supplemental Ubuntu packages under `downloads/` in the cache directory, `.cache/` or `$NVX_CACHE_DIR`. It reuses a file there whose SHA-256 matches its diff --git a/doc/usage.md b/doc/usage.md index 2dc1c7886..bbe0c1569 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -122,6 +122,7 @@ each code for callers of the `nvx` library. ```text nvx-rs/target/release/nvx-rs run + [--architecture {x86_64,aarch64}] [--guest {alpine,ubuntu,azurelinux}] [--hypervisor {auto,whp,kvm,mshv}] [--machine {microvm}] @@ -156,10 +157,11 @@ nvx-rs/target/release/nvx-rs run | Option | Default | Description | | --- | --- | --- | +| `--architecture {x86_64,aarch64}` | native host ISA | Select the guest artifact ISA, using the native host ISA when omitted. ARM cold boot uses `build/aarch64/Image` and matching Alpine/Ubuntu manifests; it never falls back to x86 files or an unqualified release. ARM snapshot/activation modes remain unsupported. | | `--guest {alpine,ubuntu,azurelinux}` | `alpine` | Select Alpine, Ubuntu, or Azure Linux userland with the same NVX kernel. This option is not used for snapshot restore. | | `--hypervisor {auto,whp,kvm,mshv}` | `auto` | Select the OpenVMM hypervisor. `auto` chooses WHP on Windows and KVM elsewhere. | | `--machine {microvm}` | `microvm` | Select the fixed-topology microVM with shared-status edge interrupts. | -| `--memory-mib MIB` | guest-specific | Set guest memory in MiB, an ASCII decimal integer from 1 to 17592186044415, without signs, whitespace, or underscores. Defaults to 128 for Alpine, 512 for Ubuntu, and 512 for Azure Linux. | +| `--memory-mib MIB` | guest-specific | Set guest memory in MiB, an ASCII decimal integer from 1 to 17592186044415, without signs, whitespace, or underscores. ARM cold boot is further capped at 64512 MiB (63 GiB): the approved 36-bit GPA space excludes the fixed 3-4 GiB aperture; other reservations are separate. Defaults to 128 for Alpine, 512 for Ubuntu, and 512 for Azure Linux. | | `--memory-capacity-mib MIB` | none | Reserve an immutable, 128 MiB-aligned RAM capacity for a fresh microVM snapshot, in the range of `--memory-mib`. | | `--processors {1,2,4,8}` | `1` | Select the microVM processor count, written as one of the choices. | | `--cpu-profile ID` | `auto` | Select the guest's [CPU profile](#cpu-profiles): `auto` for the built-in profile of the host's CPU, which falls back with a warning to a development profile derived from this host on an Intel or AMD CPU that no built-in profile serves, a built-in profile ID, or `host` for that development profile. A restore uses the snapshot's profile, which `auto` and, for a host profile, `host` also name. | @@ -359,6 +361,11 @@ ancestor that has one. Those fallback artifacts match that ancestor, not `HEAD`. It checks at most 20 commits, at one GitHub API request each, and reads the history with `git`. Update the checkout to install a newer release. +There is no qualified ARM64 release catalog yet. On an ARM64 host, `download` +rejects release selection before querying GitHub instead of installing an +x86 package. Explicit ARM cold boots use matching local artifacts built with +`--architecture aarch64`; already-present artifacts need no download. + `download` checks the size of the package, the names of its members, and every file against the package's `SHA256SUMS`, and then installs it: it replaces the packaged guest artifacts under `build/`, among them the sandbox scratch @@ -805,6 +812,7 @@ also fails for an executable stack: a `PT_GNU_STACK` program header with ```text python3 scripts/nvx.py check-guest-binaries [--toolchain TOOLCHAIN] + [--architecture {x86_64,aarch64}] ``` The Linux-only check command adds the `x86_64-unknown-linux-musl` target to the @@ -815,11 +823,14 @@ executables run on the host. It ignores an inherited `NVX_GUEST_BINARIES`, so that the integration tests run the binaries that Cargo builds from the checkout. The `nvx-rs` CI job runs it with the toolchain that its action selects, before it builds the binaries. +`--architecture` explicitly selects the musl target; x86-64 remains the default. +Tests require a Linux host capable of executing the selected target. ### `build-guest-binaries` ```text python3 scripts/nvx.py build-guest-binaries --output DIRECTORY + [--architecture {x86_64,aarch64}] ``` The Linux-only build command checks the exact `rust-toolchain.toml` pin and @@ -840,16 +851,24 @@ It updates the output of an earlier build in place, adding `nvx-managed-agent`, manifest does not record them and removing the retired `nvx-port-io` executable when that build's manifest records it, and refuses any other existing `DIRECTORY`. +`--architecture aarch64` uses the same producer with the ARM cold-boot +inventory, a matching ELF machine and a separate output directory. Missing +required helpers and stale sources fail inspection; compilation alone is not +qualification. Omitting the option preserves the ten-helper x86 build and +legacy manifest. ### `test-guest-binaries` ```text python3 scripts/nvx.py test-guest-binaries --binaries DIRECTORY + [--architecture {x86_64,aarch64}] ``` The Linux-only test command validates the ten binaries and build manifest, sets `NVX_GUEST_BINARIES`, and runs the musl `guest_tools` integration test against that exact directory. +`--architecture` selects its musl target and expected inventory; it defaults +to x86-64 and requires a host that can execute that target. See [Setup](setup.md) for host prerequisites. @@ -869,6 +888,7 @@ revision and is used before packaging kernel provenance inputs. ```text python3 scripts/nvx.py build-guest + [--architecture {x86_64,aarch64}] [--guest {alpine,ubuntu,azurelinux,all}] [--native] [--debug-kernel] @@ -881,11 +901,15 @@ Docker. `--guest all` also builds the Ubuntu EROFS distro layer and the sandbox scratch template; see [`build-scratch-template`](#build-scratch-template). `--debug-kernel` also builds the CI debug kernel, `build/vmlinux-debug`; see [Build](build.md#ci-debug-kernel). +`--architecture` defaults to x86-64. ARM uses the same producers with +separate inputs and outputs, supports Alpine or Ubuntu cold boot, and rejects +the all-guests distro-layer and x86 debug-kernel modes. It does not select or +approve a CPU profile. ### `build-kernel` ```console -python3 scripts/nvx.py build-kernel [--debug] +python3 scripts/nvx.py build-kernel [--debug] [--architecture {x86_64,aarch64}] ``` Fetches, verifies, patches, and builds the pinned kernel directly on Linux. @@ -893,11 +917,14 @@ Fetches, verifies, patches, and builds the pinned kernel directly on Linux. production build also requires its configuration to be the one that the kernel feature inventory generates; see [Kernel configuration](build.md#kernel-configuration). +`--architecture aarch64` selects the ARM inventory, patches and namespaced +`Image` output; the default remains x86-64 `vmlinux`. ### `kernel-config` ```console python3 scripts/nvx.py kernel-config [--native] [--check] + [--architecture {x86_64,aarch64}] ``` Generates `kernel/config-microvm` and `kernel/config-microvm.derived` from the @@ -907,11 +934,13 @@ every other symbol. By default it generates them in the Docker build's image, whose toolchain builds CI's kernels; `--native` generates them on Linux with the host's toolchain. `--check` fails if the checked-in files differ instead of writing them. See [Kernel configuration](build.md#kernel-configuration). +`--architecture` defaults to x86-64; ARM generates its separate configuration +and derived record from the pinned ARM Kconfig source. ### `check-kernel-config` ```console -python3 scripts/nvx.py check-kernel-config +python3 scripts/nvx.py check-kernel-config [--architecture {x86_64,aarch64}] ``` Checks, without a kernel source, that every setting of the kernel feature @@ -919,15 +948,22 @@ inventory holds in `kernel/config-microvm` and that every other symbol that it sets is recorded, with the same value, in `kernel/config-microvm.derived`. A newly enabled symbol that neither accounts for fails the check, which CI's quality job runs on every change and which `verify` runs too. +`--architecture aarch64` checks the ARM inventory and actual-value exclusions +instead; the default x86 check and attribution remain unchanged. ### `build-initramfs` ```console python3 scripts/nvx.py build-initramfs [--guest {alpine,ubuntu,azurelinux}] + [--architecture {x86_64,aarch64}] ``` Builds the selected initramfs. Alpine and Ubuntu build directly on Linux; Azure Linux uses Docker. Alpine is the default. +`--architecture` defaults to x86-64. ARM selects separate Alpine or Ubuntu +archives, package identities, helper inventory and output paths, and rejects +Azure Linux rather than using x86 inputs. Required guest-time implementation +and runtime qualification gates are not waived. ### `build-distro-layer` @@ -1040,6 +1076,7 @@ itself. ```text python3 scripts/nvx.py build + [--architecture {x86_64,aarch64}] [--guest {alpine,ubuntu,azurelinux,all}] [--native] [--debug-kernel] @@ -1049,6 +1086,8 @@ python3 scripts/nvx.py build Runs `build-guest` followed by `build-openvmm`. The options have the same meaning as on those individual commands. +`--architecture` selects the guest producers, with x86-64 the default; it +does not change the host's native OpenVMM build target or select a CPU profile. See [Build](build.md) for dependencies, outputs, and native build details. @@ -1401,6 +1440,18 @@ for the trust boundary, wrapper contract, artifacts, and CI policy. ## CPU profiles +ARM64 development cold boots use the same `--cpu-profile` option and normal +launch/admission path. `auto` selects the supported built-in ARM profile; +`host` derives a separate canonical, policy-filtered native profile with its +own ID/digest. Both retain native PAC capability through ordinary Linux +userspace key handling, not a new guest key service. The actual counter Hz +remains separate from CPU identity and is paired with it through the same +machine time binding. Kernel and Rust-helper return-signing policy is +unchanged. ARM snapshots are not supported; see +[ARM build and qualification scope](build.md#build). + +The existing x86-64 profiles and their selection behavior are unchanged: + Every microVM presents a CPU profile to its guest: the complete CPUID surface of one CPU generation, which OpenVMM pins and every backend shares (see the [CPU profile design](design/2.2-cpu-and-topology.md#221-cpu-profiles)). By default, OpenVMM selects the diff --git a/docker/Dockerfile b/docker/Dockerfile index 9f7ff51db..566a3460b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -5,8 +5,10 @@ ARG RUST_BUILDER_IMAGE=rust:1.97.1-slim-bookworm@sha256:2775a09d208ff0d7c1f50490 ARG EROFS_UTILS_VERSION=1.5-1 ARG AZURELINUX_VERSION=3.0 ARG AZURELINUX_IMAGE=mcr.microsoft.com/azurelinux/base/core@sha256:c877612270d1ee2d6ab2bc1f64bfe38ab697ac50be325154ee5129fce89c17e4 +ARG NVX_ARCHITECTURE=x86_64 FROM ${RUST_BUILDER_IMAGE} AS guest-binaries +ARG NVX_ARCHITECTURE RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ python3 \ @@ -18,10 +20,20 @@ COPY scripts/ /repo/scripts/ RUN set -eux; \ toolchain="$(sed -n 's/^channel = "\([^"]*\)".*/\1/p' rust-toolchain.toml)"; \ test -n "$toolchain"; \ - rustup target add --toolchain "$toolchain" x86_64-unknown-linux-musl; \ - python3 scripts/nvx.py build-guest-binaries --output /out/guest-binaries + set --; \ + if [ "$NVX_ARCHITECTURE" = x86_64 ]; then \ + rustup target add --toolchain "$toolchain" x86_64-unknown-linux-musl; \ + else \ + target="$(PYTHONPATH=/repo/scripts python3 -c \ + 'import sys; from nvx_tools.guest_binaries import guest_rust_target; print(guest_rust_target(sys.argv[1]))' \ + "$NVX_ARCHITECTURE")"; \ + rustup target add --toolchain "$toolchain" "$target"; \ + set -- --architecture "$NVX_ARCHITECTURE"; \ + fi; \ + python3 scripts/nvx.py build-guest-binaries --output /out/guest-binaries "$@" FROM ${DEBIAN_IMAGE} AS base +ARG NVX_ARCHITECTURE ARG EROFS_UTILS_VERSION RUN apt-get update && apt-get install -y --no-install-recommends \ bc \ @@ -53,7 +65,8 @@ COPY scripts/ /repo/scripts/ COPY LICENSE README.md SOURCE-MANIFEST.json THIRD_PARTY_NOTICES.md /repo/ FROM base AS kernel -RUN python3 scripts/nvx.py build-kernel +RUN --mount=type=cache,target=/repo/.cache/downloads \ + python3 scripts/nvx.py build-kernel --architecture "$NVX_ARCHITECTURE" FROM base AS kernel-debug RUN python3 scripts/nvx.py build-kernel --debug @@ -61,7 +74,8 @@ RUN python3 scripts/nvx.py build-kernel --debug # The kernel configuration and its derived record, generated from the feature # inventory with the toolchain that builds the kernel (doc/build.md). FROM base AS kernel-config -RUN python3 scripts/nvx.py kernel-config --native +RUN --mount=type=cache,target=/repo/.cache/downloads \ + python3 scripts/nvx.py kernel-config --native --architecture "$NVX_ARCHITECTURE" # Only the guest images install the Rust guest binaries and record their sources, # so the kernel and Linux source targets never build them. @@ -72,10 +86,10 @@ COPY --from=guest-binaries /out/guest-binaries/ /repo/build/guest-binaries/ ENV NVX_GUEST_BINARIES=/repo/build/guest-binaries FROM guest-base AS initramfs -RUN python3 scripts/nvx.py build-initramfs --guest alpine +RUN python3 scripts/nvx.py build-initramfs --guest alpine --architecture "$NVX_ARCHITECTURE" FROM guest-base AS ubuntu-initramfs -RUN python3 scripts/nvx.py build-initramfs --guest ubuntu +RUN python3 scripts/nvx.py build-initramfs --guest ubuntu --architecture "$NVX_ARCHITECTURE" FROM ubuntu-initramfs AS ubuntu-distro-layer RUN python3 scripts/nvx.py build-distro-layer \ @@ -255,6 +269,33 @@ COPY --from=kernel /repo/build/vmlinux /vmlinux COPY --from=kernel /repo/build/vmlinux.config /vmlinux.config COPY --from=kernel /repo/build/vmlinux.provenance.json /vmlinux.provenance.json +FROM scratch AS guest-binary-artifacts +COPY --from=guest-binaries /out/guest-binaries/ / + +FROM scratch AS arm64-kernel-artifacts +COPY --from=kernel /repo/build/aarch64/Image /Image +COPY --from=kernel /repo/build/aarch64/Image.config /Image.config +COPY --from=kernel /repo/build/aarch64/Image.provenance.json /Image.provenance.json + +FROM scratch AS arm64-kernel-config-artifacts +COPY --from=kernel-config /repo/kernel/config-microvm-arm64 /config-microvm-arm64 +COPY --from=kernel-config /repo/kernel/config-microvm-arm64.derived /config-microvm-arm64.derived + +FROM scratch AS arm64-artifacts +COPY --from=kernel /repo/build/aarch64/Image /Image +COPY --from=kernel /repo/build/aarch64/Image.config /Image.config +COPY --from=kernel /repo/build/aarch64/Image.provenance.json /Image.provenance.json +COPY --from=initramfs /repo/build/aarch64/initramfs.cpio.gz /initramfs.cpio.gz +COPY --from=initramfs /repo/build/aarch64/initramfs.cpio.gz.packages.json /initramfs.cpio.gz.packages.json +COPY --from=initramfs /repo/build/aarch64/initramfs.provenance.json /initramfs.provenance.json + +FROM scratch AS arm64-ubuntu-artifacts +COPY --from=kernel /repo/build/aarch64/Image /Image +COPY --from=kernel /repo/build/aarch64/Image.config /Image.config +COPY --from=kernel /repo/build/aarch64/Image.provenance.json /Image.provenance.json +COPY --from=ubuntu-initramfs /repo/build/aarch64/initramfs-ubuntu.cpio.gz /initramfs-ubuntu.cpio.gz +COPY --from=ubuntu-initramfs /repo/build/aarch64/initramfs-ubuntu.cpio.gz.packages.json /initramfs-ubuntu.cpio.gz.packages.json + FROM scratch AS kernel-debug-artifacts COPY --from=kernel-debug /repo/build/vmlinux-debug /vmlinux-debug COPY --from=kernel-debug /repo/build/vmlinux-debug.config /vmlinux-debug.config diff --git a/guest/alpine/nvx-container-enter b/guest/alpine/nvx-container-enter index 34aca9ae0..f9734608b 100644 --- a/guest/alpine/nvx-container-enter +++ b/guest/alpine/nvx-container-enter @@ -10,6 +10,15 @@ workload_home=$6 shift 6 tools=$rootfs/.nvx-agent +case $(uname -m) in + x86_64) loader=ld-musl-x86_64.so.1 ;; + aarch64) loader=ld-musl-aarch64.so.1 ;; + *) + echo "NVX-SANDBOX-ERROR: unsupported control guest architecture" >&2 + exit 125 + ;; +esac + mount --make-rprivate / rm -rf "$tools" mkdir -p \ @@ -19,7 +28,7 @@ mkdir -p \ "$rootfs/proc" \ "$rootfs/sys" mount -t tmpfs -o mode=755,nosuid,nodev tmpfs "$tools" -cp /lib/ld-musl-x86_64.so.1 "$tools/ld-musl-x86_64.so.1" +cp "/lib/$loader" "$tools/$loader" cp /bin/setpriv "$tools/setpriv" cp -L /usr/lib/libcap-ng.so.0 "$tools/libcap-ng.so.0" cp -L /usr/lib/libeconf.so.0 "$tools/libeconf.so.0" @@ -70,7 +79,7 @@ ln -s /proc/self/fd/2 "$rootfs/dev/stderr" hostname "$hostname_value" export HOME="$workload_home" USER="$workload_name" LOGNAME="$workload_name" -exec chroot "$rootfs" /.nvx-agent/ld-musl-x86_64.so.1 \ +exec chroot "$rootfs" "/.nvx-agent/$loader" \ --library-path /.nvx-agent \ /.nvx-agent/setpriv \ --reuid "$workload_uid" \ diff --git a/guest/common/nvx-init-agent b/guest/common/nvx-init-agent index dcad77a21..427f0789f 100644 --- a/guest/common/nvx-init-agent +++ b/guest/common/nvx-init-agent @@ -61,8 +61,38 @@ cmdline_value() { return 1 } +# This existing Rust helper shares the maintained fixed-role discovery with root assembly. +root_device_query() { + /sbin/nvx-init --root-device "$1" +} + resolve_block_device() { address=$(printf '%d' "$1") || return 1 + case "$(uname -m)" in + aarch64 | arm64) + if [ "$address" -eq "$(printf '%d' 0xd0003000)" ]; then + role=distro + elif [ "$address" -eq "$(printf '%d' 0xd0006000)" ]; then + role=scratch + else + echo "NVX-SANDBOX-ERROR: no fixed block role at $1" >&2 + return 2 + fi + attempts=0 + while [ "$attempts" -lt 600 ]; do + if device=$(root_device_query "$role"); then + printf '%s\n' "$device" + return 0 + else + query_status=$? + [ "$query_status" -eq 1 ] || return "$query_status" + fi + attempts=$((attempts + 1)) + sleep 0.05 + done + return 1 + ;; + esac attempts=0 while [ "$attempts" -lt 600 ]; do while IFS=' ' read -r range separator platform _; do @@ -272,9 +302,16 @@ read_root_tokens() { # MMIO base, 0xd0006000, which a RAM upper must not have. nvx::protocol::root # reads the devices from the virtio_mmio.device= parameters of the command # line, with the kernel's rules; this init asks the kernel instead, which lists -# the window of each device that it added in /proc/iomem, where -# resolve_block_device finds them too. +# the window of each device that it added in /proc/iomem on x86. ARM uses the +# same pure Rust OF lookup as root assembly, including transports still probing. refuse_scratch_device() { + case "$(uname -m)" in + aarch64 | arm64) + root_device_query no-scratch || + fatal "a RAM upper requires an absent, unambiguous fixed scratch transport" + return 0 + ;; + esac scratch_address=$(printf '%d' 0xd0006000) while IFS=' ' read -r range separator platform _; do [ "$separator" = : ] || continue diff --git a/kernel/config-microvm-arm64 b/kernel/config-microvm-arm64 new file mode 100644 index 000000000..31c7d5fcf --- /dev/null +++ b/kernel/config-microvm-arm64 @@ -0,0 +1,2086 @@ +# +# Automatically generated file; DO NOT EDIT. +# Linux/arm64 6.18.38 Kernel Configuration +# +CONFIG_CC_VERSION_TEXT="gcc (Debian 12.2.0-14+deb12u1) 12.2.0" +CONFIG_CC_IS_GCC=y +CONFIG_GCC_VERSION=120200 +CONFIG_CLANG_VERSION=0 +CONFIG_AS_IS_GNU=y +CONFIG_AS_VERSION=24000 +CONFIG_LD_IS_BFD=y +CONFIG_LD_VERSION=24000 +CONFIG_LLD_VERSION=0 +CONFIG_RUSTC_VERSION=0 +CONFIG_RUSTC_LLVM_VERSION=0 +CONFIG_CC_CAN_LINK=y +CONFIG_GCC_ASM_GOTO_OUTPUT_BROKEN=y +CONFIG_CC_HAS_ASM_INLINE=y +CONFIG_CC_HAS_NO_PROFILE_FN_ATTR=y +CONFIG_LD_CAN_USE_KEEP_IN_OVERLAY=y +CONFIG_PAHOLE_VERSION=0 +CONFIG_IRQ_WORK=y +CONFIG_BUILDTIME_TABLE_SORT=y +CONFIG_THREAD_INFO_IN_TASK=y + +# +# General setup +# +CONFIG_INIT_ENV_ARG_LIMIT=32 +# CONFIG_COMPILE_TEST is not set +CONFIG_WERROR=y +CONFIG_LOCALVERSION="" +# CONFIG_LOCALVERSION_AUTO is not set +CONFIG_BUILD_SALT="" +CONFIG_DEFAULT_INIT="" +CONFIG_DEFAULT_HOSTNAME="(none)" +CONFIG_SYSVIPC=y +CONFIG_SYSVIPC_SYSCTL=y +# CONFIG_POSIX_MQUEUE is not set +# CONFIG_WATCH_QUEUE is not set +CONFIG_CROSS_MEMORY_ATTACH=y +# CONFIG_AUDIT is not set +CONFIG_HAVE_ARCH_AUDITSYSCALL=y + +# +# IRQ subsystem +# +CONFIG_GENERIC_IRQ_PROBE=y +CONFIG_GENERIC_IRQ_SHOW=y +CONFIG_GENERIC_IRQ_SHOW_LEVEL=y +CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK=y +CONFIG_HARDIRQS_SW_RESEND=y +CONFIG_IRQ_DOMAIN=y +CONFIG_IRQ_DOMAIN_HIERARCHY=y +CONFIG_GENERIC_IRQ_IPI=y +CONFIG_GENERIC_MSI_IRQ=y +CONFIG_IRQ_MSI_IOMMU=y +CONFIG_IRQ_FORCED_THREADING=y +CONFIG_SPARSE_IRQ=y +# CONFIG_GENERIC_IRQ_DEBUGFS is not set +CONFIG_GENERIC_IRQ_KEXEC_CLEAR_VM_FORWARD=y +# end of IRQ subsystem + +CONFIG_GENERIC_TIME_VSYSCALL=y +CONFIG_GENERIC_CLOCKEVENTS=y +CONFIG_ARCH_HAS_TICK_BROADCAST=y +CONFIG_GENERIC_CLOCKEVENTS_BROADCAST=y +CONFIG_HAVE_POSIX_CPU_TIMERS_TASK_WORK=y +CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y +CONFIG_CONTEXT_TRACKING=y +CONFIG_CONTEXT_TRACKING_IDLE=y + +# +# Timers subsystem +# +CONFIG_TICK_ONESHOT=y +CONFIG_NO_HZ_COMMON=y +# CONFIG_HZ_PERIODIC is not set +CONFIG_NO_HZ_IDLE=y +# CONFIG_NO_HZ_FULL is not set +# CONFIG_NO_HZ is not set +CONFIG_HIGH_RES_TIMERS=y +# CONFIG_POSIX_AUX_CLOCKS is not set +# end of Timers subsystem + +CONFIG_BPF=y +CONFIG_HAVE_EBPF_JIT=y +CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y + +# +# BPF subsystem +# +CONFIG_BPF_SYSCALL=y +# CONFIG_BPF_JIT is not set +CONFIG_BPF_UNPRIV_DEFAULT_OFF=y +# CONFIG_BPF_PRELOAD is not set +# end of BPF subsystem + +CONFIG_PREEMPT_BUILD=y +CONFIG_ARCH_HAS_PREEMPT_LAZY=y +# CONFIG_PREEMPT_NONE is not set +CONFIG_PREEMPT_VOLUNTARY=y +# CONFIG_PREEMPT is not set +# CONFIG_PREEMPT_LAZY is not set +# CONFIG_PREEMPT_RT is not set +CONFIG_PREEMPT_COUNT=y +CONFIG_PREEMPTION=y +CONFIG_PREEMPT_DYNAMIC=y + +# +# CPU/Task time and stats accounting +# +CONFIG_TICK_CPU_ACCOUNTING=y +# CONFIG_VIRT_CPU_ACCOUNTING_GEN is not set +# CONFIG_IRQ_TIME_ACCOUNTING is not set +# CONFIG_BSD_PROCESS_ACCT is not set +# CONFIG_TASKSTATS is not set +# CONFIG_PSI is not set +# end of CPU/Task time and stats accounting + +# CONFIG_CPU_ISOLATION is not set + +# +# RCU Subsystem +# +CONFIG_TREE_RCU=y +CONFIG_PREEMPT_RCU=y +# CONFIG_RCU_EXPERT is not set +CONFIG_TREE_SRCU=y +CONFIG_TASKS_RCU_GENERIC=y +CONFIG_NEED_TASKS_RCU=y +CONFIG_TASKS_RCU=y +CONFIG_TASKS_TRACE_RCU=y +CONFIG_RCU_STALL_COMMON=y +CONFIG_RCU_NEED_SEGCBLIST=y +# end of RCU Subsystem + +# CONFIG_IKCONFIG is not set +# CONFIG_IKHEADERS is not set +CONFIG_LOG_BUF_SHIFT=18 +CONFIG_LOG_CPU_MAX_BUF_SHIFT=12 +# CONFIG_PRINTK_INDEX is not set +CONFIG_GENERIC_SCHED_CLOCK=y + +# +# Scheduler features +# +# CONFIG_SCHED_PROXY_EXEC is not set +# end of Scheduler features + +CONFIG_ARCH_SUPPORTS_NUMA_BALANCING=y +CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH=y +CONFIG_CC_HAS_INT128=y +CONFIG_CC_IMPLICIT_FALLTHROUGH="-Wimplicit-fallthrough=5" +CONFIG_GCC10_NO_ARRAY_BOUNDS=y +CONFIG_CC_NO_ARRAY_BOUNDS=y +CONFIG_GCC_NO_STRINGOP_OVERFLOW=y +CONFIG_CC_NO_STRINGOP_OVERFLOW=y +CONFIG_ARCH_SUPPORTS_INT128=y +CONFIG_SLAB_OBJ_EXT=y +CONFIG_CGROUPS=y +CONFIG_PAGE_COUNTER=y +# CONFIG_CGROUP_FAVOR_DYNMODS is not set +CONFIG_MEMCG=y +# CONFIG_MEMCG_V1 is not set +CONFIG_BLK_CGROUP=y +CONFIG_CGROUP_WRITEBACK=y +# CONFIG_CGROUP_SCHED is not set +CONFIG_SCHED_MM_CID=y +CONFIG_CGROUP_PIDS=y +# CONFIG_CGROUP_RDMA is not set +# CONFIG_CGROUP_DMEM is not set +# CONFIG_CGROUP_FREEZER is not set +# CONFIG_CPUSETS is not set +# CONFIG_CGROUP_DEVICE is not set +# CONFIG_CGROUP_CPUACCT is not set +CONFIG_CGROUP_BPF=y +# CONFIG_CGROUP_MISC is not set +# CONFIG_CGROUP_DEBUG is not set +CONFIG_SOCK_CGROUP_DATA=y +CONFIG_NAMESPACES=y +CONFIG_UTS_NS=y +# CONFIG_TIME_NS is not set +# CONFIG_IPC_NS is not set +# CONFIG_USER_NS is not set +CONFIG_PID_NS=y +CONFIG_NET_NS=y +# CONFIG_CHECKPOINT_RESTORE is not set +# CONFIG_SCHED_AUTOGROUP is not set +# CONFIG_RELAY is not set +CONFIG_BLK_DEV_INITRD=y +CONFIG_INITRAMFS_SOURCE="" +CONFIG_RD_GZIP=y +# CONFIG_RD_BZIP2 is not set +# CONFIG_RD_LZMA is not set +# CONFIG_RD_XZ is not set +# CONFIG_RD_LZO is not set +# CONFIG_RD_LZ4 is not set +# CONFIG_RD_ZSTD is not set +# CONFIG_BOOT_CONFIG is not set +CONFIG_INITRAMFS_PRESERVE_MTIME=y +CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y +# CONFIG_CC_OPTIMIZE_FOR_SIZE is not set +CONFIG_LD_ORPHAN_WARN=y +CONFIG_LD_ORPHAN_WARN_LEVEL="error" +CONFIG_SYSCTL=y +CONFIG_SYSCTL_EXCEPTION_TRACE=y +# CONFIG_SYSFS_SYSCALL is not set +CONFIG_EXPERT=y +CONFIG_MULTIUSER=y +# CONFIG_SGETMASK_SYSCALL is not set +CONFIG_FHANDLE=y +CONFIG_POSIX_TIMERS=y +CONFIG_PRINTK=y +CONFIG_BUG=y +CONFIG_ELF_CORE=y +# CONFIG_BASE_SMALL is not set +CONFIG_FUTEX=y +CONFIG_FUTEX_PI=y +CONFIG_FUTEX_PRIVATE_HASH=y +CONFIG_EPOLL=y +CONFIG_SIGNALFD=y +CONFIG_TIMERFD=y +CONFIG_EVENTFD=y +CONFIG_SHMEM=y +CONFIG_AIO=y +CONFIG_IO_URING=y +# CONFIG_IO_URING_MOCK_FILE is not set +CONFIG_ADVISE_SYSCALLS=y +CONFIG_MEMBARRIER=y +# CONFIG_KCMP is not set +CONFIG_RSEQ=y +# CONFIG_DEBUG_RSEQ is not set +CONFIG_CACHESTAT_SYSCALL=y +CONFIG_KALLSYMS=y +# CONFIG_KALLSYMS_SELFTEST is not set +# CONFIG_KALLSYMS_ALL is not set +CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE=y +CONFIG_ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS=y +CONFIG_HAVE_PERF_EVENTS=y + +# +# Kernel Performance Events And Counters +# +# CONFIG_PERF_EVENTS is not set +# end of Kernel Performance Events And Counters + +# CONFIG_PROFILING is not set + +# +# Kexec and crash features +# +# CONFIG_KEXEC_FILE is not set +# CONFIG_KEXEC_HANDOVER is not set +# end of Kexec and crash features +# end of General setup + +CONFIG_ARM64=y +CONFIG_RUSTC_SUPPORTS_ARM64=y +CONFIG_GCC_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS=y +CONFIG_64BIT=y +CONFIG_MMU=y +CONFIG_ARM64_CONT_PTE_SHIFT=4 +CONFIG_ARM64_CONT_PMD_SHIFT=4 +CONFIG_ARCH_MMAP_RND_BITS_MIN=18 +CONFIG_ARCH_MMAP_RND_BITS_MAX=33 +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN=11 +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX=16 +CONFIG_NO_IOPORT_MAP=y +CONFIG_STACKTRACE_SUPPORT=y +CONFIG_ILLEGAL_POINTER_VALUE=0xdead000000000000 +CONFIG_LOCKDEP_SUPPORT=y +CONFIG_GENERIC_BUG=y +CONFIG_GENERIC_BUG_RELATIVE_POINTERS=y +CONFIG_GENERIC_HWEIGHT=y +CONFIG_GENERIC_CSUM=y +CONFIG_GENERIC_CALIBRATE_DELAY=y +CONFIG_SMP=y +CONFIG_KERNEL_MODE_NEON=y +CONFIG_FIX_EARLYCON_MEM=y +CONFIG_PGTABLE_LEVELS=4 +CONFIG_ARCH_SUPPORTS_UPROBES=y +CONFIG_ARCH_PROC_KCORE_TEXT=y +CONFIG_BUILTIN_RETURN_ADDRESS_STRIPS_PAC=y + +# +# Platform selection +# +# CONFIG_ARCH_ACTIONS is not set +# CONFIG_ARCH_AIROHA is not set +# CONFIG_ARCH_SUNXI is not set +# CONFIG_ARCH_ALPINE is not set +# CONFIG_ARCH_APPLE is not set +# CONFIG_ARCH_AXIADO is not set +# CONFIG_ARCH_BCM is not set +# CONFIG_ARCH_BERLIN is not set +# CONFIG_ARCH_BITMAIN is not set +# CONFIG_ARCH_BLAIZE is not set +# CONFIG_ARCH_CIX is not set +# CONFIG_ARCH_EXYNOS is not set +# CONFIG_ARCH_K3 is not set +# CONFIG_ARCH_LG1K is not set +# CONFIG_ARCH_HISI is not set +# CONFIG_ARCH_KEEMBAY is not set +# CONFIG_ARCH_MEDIATEK is not set +# CONFIG_ARCH_MESON is not set + +# +# Microchip SoC support +# +# CONFIG_ARCH_LAN969X is not set +# CONFIG_ARCH_SPARX5 is not set +# end of Microchip SoC support + +# CONFIG_ARCH_MMP is not set +# CONFIG_ARCH_MVEBU is not set +# CONFIG_ARCH_NXP is not set +# CONFIG_ARCH_MA35 is not set +# CONFIG_ARCH_NPCM is not set +# CONFIG_ARCH_PENSANDO is not set +# CONFIG_ARCH_QCOM is not set +# CONFIG_ARCH_REALTEK is not set +# CONFIG_ARCH_RENESAS is not set +# CONFIG_ARCH_ROCKCHIP is not set +# CONFIG_ARCH_SEATTLE is not set +# CONFIG_ARCH_INTEL_SOCFPGA is not set +# CONFIG_ARCH_SOPHGO is not set +# CONFIG_ARCH_STM32 is not set +# CONFIG_ARCH_SYNQUACER is not set +# CONFIG_ARCH_TEGRA is not set +# CONFIG_ARCH_SPRD is not set +# CONFIG_ARCH_THUNDER is not set +# CONFIG_ARCH_THUNDER2 is not set +# CONFIG_ARCH_UNIPHIER is not set +# CONFIG_ARCH_VEXPRESS is not set +# CONFIG_ARCH_VISCONTI is not set +# CONFIG_ARCH_XGENE is not set +# CONFIG_ARCH_ZYNQMP is not set +# end of Platform selection + +# +# Kernel Features +# + +# +# ARM errata workarounds via the alternatives framework +# +# CONFIG_AMPERE_ERRATUM_AC03_CPU_38 is not set +# CONFIG_AMPERE_ERRATUM_AC04_CPU_23 is not set +# CONFIG_ARM64_ERRATUM_826319 is not set +# CONFIG_ARM64_ERRATUM_827319 is not set +# CONFIG_ARM64_ERRATUM_824069 is not set +# CONFIG_ARM64_ERRATUM_819472 is not set +# CONFIG_ARM64_ERRATUM_832075 is not set +# CONFIG_ARM64_ERRATUM_843419 is not set +# CONFIG_ARM64_ERRATUM_1024718 is not set +# CONFIG_ARM64_ERRATUM_1165522 is not set +# CONFIG_ARM64_ERRATUM_1319367 is not set +# CONFIG_ARM64_ERRATUM_1530923 is not set +# CONFIG_ARM64_ERRATUM_2441007 is not set +# CONFIG_ARM64_ERRATUM_1286807 is not set +# CONFIG_ARM64_ERRATUM_1463225 is not set +# CONFIG_ARM64_ERRATUM_1542419 is not set +# CONFIG_ARM64_ERRATUM_1508412 is not set +# CONFIG_ARM64_ERRATUM_2051678 is not set +# CONFIG_ARM64_ERRATUM_2077057 is not set +# CONFIG_ARM64_ERRATUM_2658417 is not set +# CONFIG_ARM64_ERRATUM_2054223 is not set +# CONFIG_ARM64_ERRATUM_2067961 is not set +# CONFIG_ARM64_ERRATUM_2441009 is not set +# CONFIG_ARM64_ERRATUM_2645198 is not set +# CONFIG_ARM64_ERRATUM_2966298 is not set +# CONFIG_ARM64_ERRATUM_3117295 is not set +# CONFIG_ARM64_ERRATUM_3194386 is not set +# CONFIG_ARM64_ERRATUM_4118414 is not set +# CONFIG_CAVIUM_ERRATUM_22375 is not set +# CONFIG_CAVIUM_ERRATUM_23154 is not set +# CONFIG_CAVIUM_ERRATUM_27456 is not set +# CONFIG_CAVIUM_ERRATUM_30115 is not set +# CONFIG_CAVIUM_TX2_ERRATUM_219 is not set +# CONFIG_FUJITSU_ERRATUM_010001 is not set +# CONFIG_HISILICON_ERRATUM_161600802 is not set +# CONFIG_HISILICON_ERRATUM_162100801 is not set +# CONFIG_QCOM_FALKOR_ERRATUM_1003 is not set +# CONFIG_QCOM_FALKOR_ERRATUM_1009 is not set +# CONFIG_QCOM_QDF2400_ERRATUM_0065 is not set +# CONFIG_QCOM_FALKOR_ERRATUM_E1041 is not set +# CONFIG_NVIDIA_CARMEL_CNP_ERRATUM is not set +# CONFIG_ROCKCHIP_ERRATUM_3568002 is not set +# CONFIG_ROCKCHIP_ERRATUM_3588001 is not set +# CONFIG_SOCIONEXT_SYNQUACER_PREITS is not set +# end of ARM errata workarounds via the alternatives framework + +CONFIG_ARM64_4K_PAGES=y +# CONFIG_ARM64_16K_PAGES is not set +# CONFIG_ARM64_64K_PAGES is not set +# CONFIG_ARM64_VA_BITS_39 is not set +CONFIG_ARM64_VA_BITS_48=y +# CONFIG_ARM64_VA_BITS_52 is not set +CONFIG_ARM64_VA_BITS=48 +CONFIG_ARM64_PA_BITS_48=y +CONFIG_ARM64_PA_BITS=48 +CONFIG_CPU_LITTLE_ENDIAN=y +CONFIG_NR_CPUS=8 +# CONFIG_HOTPLUG_CPU is not set +# CONFIG_NUMA is not set +CONFIG_HZ_100=y +# CONFIG_HZ_250 is not set +# CONFIG_HZ_300 is not set +# CONFIG_HZ_1000 is not set +CONFIG_HZ=100 +CONFIG_SCHED_HRTICK=y +CONFIG_ARCH_SPARSEMEM_ENABLE=y +CONFIG_CC_HAVE_SHADOW_CALL_STACK=y +# CONFIG_PARAVIRT is not set +# CONFIG_PARAVIRT_TIME_ACCOUNTING is not set +CONFIG_ARCH_SUPPORTS_KEXEC_FILE=y +CONFIG_ARCH_SUPPORTS_KEXEC_SIG=y +CONFIG_ARCH_SUPPORTS_KEXEC_IMAGE_VERIFY_SIG=y +CONFIG_ARCH_DEFAULT_KEXEC_IMAGE_VERIFY_SIG=y +CONFIG_ARCH_SUPPORTS_KEXEC_HANDOVER=y +CONFIG_ARCH_SUPPORTS_CRASH_DUMP=y +CONFIG_ARCH_DEFAULT_CRASH_DUMP=y +# CONFIG_XEN is not set +CONFIG_ARCH_FORCE_MAX_ORDER=10 +CONFIG_UNMAP_KERNEL_AT_EL0=y +CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY=y +# CONFIG_ARM64_SW_TTBR0_PAN is not set +# CONFIG_ARM64_TAGGED_ADDR_ABI is not set +# CONFIG_COMPAT is not set + +# +# ARMv8.1 architectural features +# +# CONFIG_ARM64_HW_AFDBM is not set +# CONFIG_ARM64_PAN is not set +# CONFIG_ARM64_USE_LSE_ATOMICS is not set +# end of ARMv8.1 architectural features + +# +# ARMv8.2 architectural features +# +# CONFIG_ARM64_PMEM is not set +# CONFIG_ARM64_RAS_EXTN is not set +# CONFIG_ARM64_CNP is not set +# end of ARMv8.2 architectural features + +# +# ARMv8.3 architectural features +# +CONFIG_ARM64_PTR_AUTH=y +# CONFIG_ARM64_PTR_AUTH_KERNEL is not set +CONFIG_CC_HAS_BRANCH_PROT_PAC_RET=y +CONFIG_AS_HAS_CFI_NEGATE_RA_STATE=y +# end of ARMv8.3 architectural features + +# +# ARMv8.4 architectural features +# +# CONFIG_ARM64_AMU_EXTN is not set +# CONFIG_ARM64_TLB_RANGE is not set +# end of ARMv8.4 architectural features + +# +# ARMv8.5 architectural features +# +CONFIG_AS_HAS_ARMV8_5=y +# CONFIG_ARM64_BTI is not set +CONFIG_CC_HAS_BRANCH_PROT_PAC_RET_BTI=y +# CONFIG_ARM64_E0PD is not set +CONFIG_ARM64_AS_HAS_MTE=y +# end of ARMv8.5 architectural features + +# +# ARMv8.7 architectural features +# +# end of ARMv8.7 architectural features + +CONFIG_AS_HAS_MOPS=y + +# +# ARMv8.9 architectural features +# +# CONFIG_ARM64_POE is not set +CONFIG_ARCH_PKEY_BITS=3 +# end of ARMv8.9 architectural features + +# +# ARMv9.4 architectural features +# +# CONFIG_ARM64_GCS is not set +# end of ARMv9.4 architectural features + +# CONFIG_ARM64_SVE is not set +# CONFIG_ARM64_PSEUDO_NMI is not set +# CONFIG_RELOCATABLE is not set +# CONFIG_RANDOMIZE_BASE is not set +CONFIG_CC_HAVE_STACKPROTECTOR_SYSREG=y +CONFIG_STACKPROTECTOR_PER_TASK=y +# end of Kernel Features + +# +# Boot options +# +CONFIG_CMDLINE="" +# CONFIG_EFI is not set +# CONFIG_COMPRESSED_INSTALL is not set +# end of Boot options + +# +# Power management options +# +# CONFIG_SUSPEND is not set +# CONFIG_PM is not set +CONFIG_ARCH_SUSPEND_POSSIBLE=y +# end of Power management options + +# +# CPU Power Management +# + +# +# CPU Idle +# +# CONFIG_CPU_IDLE is not set +# end of CPU Idle + +# +# CPU Frequency scaling +# +# CONFIG_CPU_FREQ is not set +# end of CPU Frequency scaling +# end of CPU Power Management + +# CONFIG_VIRTUALIZATION is not set +CONFIG_HAVE_LIVEPATCH=y +CONFIG_CPU_MITIGATIONS=y + +# +# General architecture-dependent options +# +CONFIG_ARCH_SUPPORTS_SCHED_SMT=y +CONFIG_ARCH_SUPPORTS_SCHED_CLUSTER=y +CONFIG_ARCH_SUPPORTS_SCHED_MC=y +# CONFIG_SCHED_SMT is not set +CONFIG_SCHED_CLUSTER=y +CONFIG_SCHED_MC=y +CONFIG_GENERIC_IRQ_ENTRY=y +# CONFIG_KPROBES is not set +CONFIG_JUMP_LABEL=y +# CONFIG_STATIC_KEYS_SELFTEST is not set +CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS=y +CONFIG_HAVE_IOREMAP_PROT=y +CONFIG_HAVE_KPROBES=y +CONFIG_HAVE_KRETPROBES=y +CONFIG_HAVE_FUNCTION_ERROR_INJECTION=y +CONFIG_HAVE_NMI=y +CONFIG_TRACE_IRQFLAGS_SUPPORT=y +CONFIG_TRACE_IRQFLAGS_NMI_SUPPORT=y +CONFIG_HAVE_ARCH_TRACEHOOK=y +CONFIG_HAVE_DMA_CONTIGUOUS=y +CONFIG_GENERIC_SMP_IDLE_THREAD=y +CONFIG_GENERIC_IDLE_POLL_SETUP=y +CONFIG_ARCH_HAS_FORTIFY_SOURCE=y +CONFIG_ARCH_HAS_KEEPINITRD=y +CONFIG_ARCH_HAS_SET_MEMORY=y +CONFIG_ARCH_HAS_SET_DIRECT_MAP=y +CONFIG_HAVE_ARCH_THREAD_STRUCT_WHITELIST=y +CONFIG_ARCH_WANTS_NO_INSTR=y +CONFIG_HAVE_ASM_MODVERSIONS=y +CONFIG_HAVE_REGS_AND_STACK_ACCESS_API=y +CONFIG_HAVE_RSEQ=y +CONFIG_HAVE_RUST=y +CONFIG_HAVE_FUNCTION_ARG_ACCESS_API=y +CONFIG_HAVE_PERF_REGS=y +CONFIG_HAVE_PERF_USER_STACK_DUMP=y +CONFIG_HAVE_ARCH_JUMP_LABEL=y +CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y +CONFIG_MMU_GATHER_TABLE_FREE=y +CONFIG_MMU_GATHER_RCU_TABLE_FREE=y +CONFIG_MMU_LAZY_TLB_REFCOUNT=y +CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG=y +CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS=y +CONFIG_HAVE_ALIGNED_STRUCT_PAGE=y +CONFIG_HAVE_CMPXCHG_LOCAL=y +CONFIG_HAVE_CMPXCHG_DOUBLE=y +CONFIG_HAVE_ARCH_SECCOMP=y +CONFIG_HAVE_ARCH_SECCOMP_FILTER=y +CONFIG_SECCOMP=y +CONFIG_SECCOMP_FILTER=y +# CONFIG_SECCOMP_CACHE_DEBUG is not set +CONFIG_HAVE_ARCH_KSTACK_ERASE=y +CONFIG_HAVE_STACKPROTECTOR=y +CONFIG_STACKPROTECTOR=y +CONFIG_STACKPROTECTOR_STRONG=y +CONFIG_ARCH_SUPPORTS_SHADOW_CALL_STACK=y +# CONFIG_SHADOW_CALL_STACK is not set +CONFIG_ARCH_SUPPORTS_LTO_CLANG=y +CONFIG_ARCH_SUPPORTS_LTO_CLANG_THIN=y +CONFIG_LTO_NONE=y +CONFIG_ARCH_SUPPORTS_CFI=y +CONFIG_HAVE_CONTEXT_TRACKING_USER=y +CONFIG_HAVE_VIRT_CPU_ACCOUNTING_GEN=y +CONFIG_HAVE_IRQ_TIME_ACCOUNTING=y +CONFIG_HAVE_MOVE_PUD=y +CONFIG_HAVE_MOVE_PMD=y +CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE=y +CONFIG_HAVE_ARCH_HUGE_VMAP=y +CONFIG_HAVE_ARCH_HUGE_VMALLOC=y +CONFIG_ARCH_WANT_HUGE_PMD_SHARE=y +CONFIG_HAVE_MOD_ARCH_SPECIFIC=y +CONFIG_MODULES_USE_ELF_RELA=y +CONFIG_ARCH_WANTS_EXECMEM_LATE=y +CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK=y +CONFIG_SOFTIRQ_ON_OWN_STACK=y +CONFIG_ARCH_HAS_ELF_RANDOMIZE=y +CONFIG_HAVE_ARCH_MMAP_RND_BITS=y +CONFIG_ARCH_MMAP_RND_BITS=18 +CONFIG_HAVE_PAGE_SIZE_4KB=y +CONFIG_PAGE_SIZE_4KB=y +CONFIG_PAGE_SIZE_LESS_THAN_64KB=y +CONFIG_PAGE_SIZE_LESS_THAN_256KB=y +CONFIG_PAGE_SHIFT=12 +CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT=y +CONFIG_HAVE_RELIABLE_STACKTRACE=y +CONFIG_CLONE_BACKWARDS=y +# CONFIG_COMPAT_32BIT_TIME is not set +CONFIG_ARCH_SUPPORTS_RT=y +CONFIG_HAVE_ARCH_VMAP_STACK=y +CONFIG_VMAP_STACK=y +CONFIG_HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET=y +CONFIG_RANDOMIZE_KSTACK_OFFSET=y +# CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT is not set +CONFIG_ARCH_HAS_STRICT_KERNEL_RWX=y +CONFIG_STRICT_KERNEL_RWX=y +CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y +CONFIG_STRICT_MODULE_RWX=y +CONFIG_HAVE_ARCH_COMPILER_H=y +CONFIG_HAVE_ARCH_PREL32_RELOCATIONS=y +# CONFIG_LOCK_EVENT_COUNTS is not set +CONFIG_ARCH_HAS_MEM_ENCRYPT=y +CONFIG_ARCH_HAS_CC_PLATFORM=y +CONFIG_HAVE_PREEMPT_DYNAMIC=y +CONFIG_HAVE_PREEMPT_DYNAMIC_KEY=y +CONFIG_ARCH_WANT_LD_ORPHAN_WARN=y +CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC=y +CONFIG_ARCH_SUPPORTS_PAGE_TABLE_CHECK=y +CONFIG_ARCH_HAVE_TRACE_MMIO_ACCESS=y +CONFIG_ARCH_HAS_HW_PTE_YOUNG=y +CONFIG_ARCH_HAS_KERNEL_FPU_SUPPORT=y + +# +# GCOV-based kernel profiling +# +# CONFIG_GCOV_KERNEL is not set +CONFIG_ARCH_HAS_GCOV_PROFILE_ALL=y +# end of GCOV-based kernel profiling + +CONFIG_HAVE_GCC_PLUGINS=y +CONFIG_FUNCTION_ALIGNMENT_4B=y +CONFIG_FUNCTION_ALIGNMENT=4 +# end of General architecture-dependent options + +CONFIG_RT_MUTEXES=y +CONFIG_MODULES=y +# CONFIG_MODULE_DEBUG is not set +# CONFIG_MODULE_FORCE_LOAD is not set +# CONFIG_MODULE_UNLOAD is not set +# CONFIG_MODVERSIONS is not set +# CONFIG_MODULE_SRCVERSION_ALL is not set +# CONFIG_MODULE_SIG is not set +# CONFIG_MODULE_COMPRESS is not set +# CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS is not set +CONFIG_MODPROBE_PATH="" +CONFIG_TRIM_UNUSED_KSYMS=y +CONFIG_UNUSED_KSYMS_WHITELIST="" +CONFIG_BLOCK=y +# CONFIG_BLOCK_LEGACY_AUTOLOAD is not set +# CONFIG_BLK_DEV_BSGLIB is not set +# CONFIG_BLK_DEV_INTEGRITY is not set +# CONFIG_BLK_DEV_WRITE_MOUNTED is not set +# CONFIG_BLK_DEV_ZONED is not set +# CONFIG_BLK_DEV_THROTTLING is not set +# CONFIG_BLK_WBT is not set +# CONFIG_BLK_CGROUP_IOLATENCY is not set +# CONFIG_BLK_CGROUP_IOCOST is not set +# CONFIG_BLK_CGROUP_IOPRIO is not set +# CONFIG_BLK_DEBUG_FS is not set +# CONFIG_BLK_INLINE_ENCRYPTION is not set + +# +# Partition Types +# +CONFIG_PARTITION_ADVANCED=y +# CONFIG_ACORN_PARTITION is not set +# CONFIG_AIX_PARTITION is not set +# CONFIG_OSF_PARTITION is not set +# CONFIG_AMIGA_PARTITION is not set +# CONFIG_ATARI_PARTITION is not set +# CONFIG_MAC_PARTITION is not set +# CONFIG_MSDOS_PARTITION is not set +# CONFIG_LDM_PARTITION is not set +# CONFIG_SGI_PARTITION is not set +# CONFIG_ULTRIX_PARTITION is not set +# CONFIG_SUN_PARTITION is not set +# CONFIG_KARMA_PARTITION is not set +CONFIG_EFI_PARTITION=y +# CONFIG_SYSV68_PARTITION is not set +# CONFIG_CMDLINE_PARTITION is not set +# CONFIG_OF_PARTITION is not set +# end of Partition Types + +# +# IO Schedulers +# +# CONFIG_MQ_IOSCHED_DEADLINE is not set +# CONFIG_MQ_IOSCHED_KYBER is not set +# CONFIG_IOSCHED_BFQ is not set +# end of IO Schedulers + +CONFIG_UNINLINE_SPIN_UNLOCK=y +CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y +CONFIG_MUTEX_SPIN_ON_OWNER=y +CONFIG_RWSEM_SPIN_ON_OWNER=y +CONFIG_LOCK_SPIN_ON_OWNER=y +CONFIG_ARCH_USE_QUEUED_SPINLOCKS=y +CONFIG_QUEUED_SPINLOCKS=y +CONFIG_ARCH_USE_QUEUED_RWLOCKS=y +CONFIG_QUEUED_RWLOCKS=y +CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE=y +CONFIG_ARCH_HAS_SYSCALL_WRAPPER=y + +# +# Executable file formats +# +CONFIG_BINFMT_ELF=y +CONFIG_ARCH_BINFMT_ELF_STATE=y +CONFIG_ARCH_BINFMT_ELF_EXTRA_PHDRS=y +CONFIG_ARCH_HAVE_ELF_PROT=y +CONFIG_ARCH_USE_GNU_PROPERTY=y +CONFIG_ELFCORE=y +CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS=y +CONFIG_BINFMT_SCRIPT=y +# CONFIG_BINFMT_MISC is not set +CONFIG_COREDUMP=y +# end of Executable file formats + +# +# Memory Management options +# +# CONFIG_SWAP is not set + +# +# Slab allocator options +# +CONFIG_SLUB=y +CONFIG_KVFREE_RCU_BATCHED=y +# CONFIG_SLUB_TINY is not set +CONFIG_SLAB_MERGE_DEFAULT=y +# CONFIG_SLAB_FREELIST_RANDOM is not set +# CONFIG_SLAB_FREELIST_HARDENED is not set +# CONFIG_SLAB_BUCKETS is not set +# CONFIG_SLUB_STATS is not set +CONFIG_SLUB_CPU_PARTIAL=y +# CONFIG_RANDOM_KMALLOC_CACHES is not set +# end of Slab allocator options + +# CONFIG_SHUFFLE_PAGE_ALLOCATOR is not set +# CONFIG_COMPAT_BRK is not set +CONFIG_SPARSEMEM=y +CONFIG_SPARSEMEM_EXTREME=y +CONFIG_SPARSEMEM_VMEMMAP_ENABLE=y +CONFIG_SPARSEMEM_VMEMMAP=y +CONFIG_HAVE_GUP_FAST=y +CONFIG_ARCH_KEEP_MEMBLOCK=y +CONFIG_EXCLUSIVE_SYSTEM_RAM=y +CONFIG_ARCH_ENABLE_MEMORY_HOTPLUG=y +CONFIG_ARCH_ENABLE_MEMORY_HOTREMOVE=y +# CONFIG_MEMORY_HOTPLUG is not set +CONFIG_ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE=y +CONFIG_SPLIT_PTE_PTLOCKS=y +CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK=y +CONFIG_SPLIT_PMD_PTLOCKS=y +CONFIG_COMPACTION=y +CONFIG_COMPACT_UNEVICTABLE_DEFAULT=1 +# CONFIG_PAGE_REPORTING is not set +CONFIG_MIGRATION=y +CONFIG_PCP_BATCH_SCALE_MAX=5 +CONFIG_PHYS_ADDR_T_64BIT=y +# CONFIG_KSM is not set +CONFIG_DEFAULT_MMAP_MIN_ADDR=4096 +CONFIG_ARCH_SUPPORTS_MEMORY_FAILURE=y +# CONFIG_MEMORY_FAILURE is not set +CONFIG_ARCH_WANTS_THP_SWAP=y +# CONFIG_TRANSPARENT_HUGEPAGE is not set +CONFIG_PAGE_MAPCOUNT=y +# CONFIG_CMA is not set +CONFIG_PAGE_BLOCK_MAX_ORDER=10 +CONFIG_GENERIC_EARLY_IOREMAP=y +# CONFIG_DEFERRED_STRUCT_PAGE_INIT is not set +# CONFIG_IDLE_PAGE_TRACKING is not set +CONFIG_ARCH_HAS_CACHE_LINE_SIZE=y +CONFIG_ARCH_HAS_CURRENT_STACK_POINTER=y +CONFIG_ARCH_HAS_ZONE_DMA_SET=y +# CONFIG_ZONE_DMA is not set +# CONFIG_ZONE_DMA32 is not set +CONFIG_VM_EVENT_COUNTERS=y +# CONFIG_PERCPU_STATS is not set +# CONFIG_GUP_TEST is not set +# CONFIG_DMAPOOL_TEST is not set +CONFIG_ARCH_HAS_PTE_SPECIAL=y +CONFIG_MEMFD_CREATE=y +CONFIG_SECRETMEM=y +# CONFIG_ANON_VMA_NAME is not set +# CONFIG_USERFAULTFD is not set +# CONFIG_LRU_GEN is not set +CONFIG_ARCH_SUPPORTS_PER_VMA_LOCK=y +CONFIG_PER_VMA_LOCK=y +CONFIG_LOCK_MM_AND_FIND_VMA=y +CONFIG_EXECMEM=y + +# +# Data Access Monitoring +# +# CONFIG_DAMON is not set +# end of Data Access Monitoring +# end of Memory Management options + +CONFIG_NET=y +CONFIG_NET_INGRESS=y +CONFIG_NET_EGRESS=y +CONFIG_NET_XGRESS=y + +# +# Networking options +# +# CONFIG_PACKET is not set +# CONFIG_INET_PSP is not set +CONFIG_UNIX=y +CONFIG_AF_UNIX_OOB=y +# CONFIG_UNIX_DIAG is not set +# CONFIG_TLS is not set +# CONFIG_XFRM_USER is not set +# CONFIG_NET_KEY is not set +# CONFIG_DIBS is not set +# CONFIG_XDP_SOCKETS is not set +CONFIG_INET=y +# CONFIG_IP_MULTICAST is not set +# CONFIG_IP_ADVANCED_ROUTER is not set +# CONFIG_IP_PNP is not set +# CONFIG_NET_IPIP is not set +# CONFIG_NET_IPGRE_DEMUX is not set +# CONFIG_SYN_COOKIES is not set +# CONFIG_NET_IPVTI is not set +# CONFIG_NET_FOU is not set +# CONFIG_INET_AH is not set +# CONFIG_INET_ESP is not set +# CONFIG_INET_IPCOMP is not set +CONFIG_INET_TABLE_PERTURB_ORDER=16 +CONFIG_INET_DIAG=y +CONFIG_INET_TCP_DIAG=y +# CONFIG_INET_UDP_DIAG is not set +# CONFIG_INET_RAW_DIAG is not set +# CONFIG_INET_DIAG_DESTROY is not set +# CONFIG_TCP_CONG_ADVANCED is not set +CONFIG_TCP_CONG_CUBIC=y +CONFIG_DEFAULT_TCP_CONG="cubic" +# CONFIG_TCP_AO is not set +# CONFIG_TCP_MD5SIG is not set +CONFIG_IPV6=y +# CONFIG_IPV6_ROUTER_PREF is not set +# CONFIG_IPV6_OPTIMISTIC_DAD is not set +# CONFIG_INET6_AH is not set +# CONFIG_INET6_ESP is not set +# CONFIG_INET6_IPCOMP is not set +# CONFIG_IPV6_MIP6 is not set +# CONFIG_IPV6_VTI is not set +# CONFIG_IPV6_SIT is not set +# CONFIG_IPV6_TUNNEL is not set +# CONFIG_IPV6_MULTIPLE_TABLES is not set +# CONFIG_IPV6_MROUTE is not set +# CONFIG_IPV6_SEG6_LWTUNNEL is not set +# CONFIG_IPV6_SEG6_HMAC is not set +# CONFIG_IPV6_RPL_LWTUNNEL is not set +# CONFIG_IPV6_IOAM6_LWTUNNEL is not set +# CONFIG_MPTCP is not set +# CONFIG_NETWORK_SECMARK is not set +# CONFIG_NETWORK_PHY_TIMESTAMPING is not set +# CONFIG_NETFILTER is not set +# CONFIG_IP_SCTP is not set +# CONFIG_RDS is not set +# CONFIG_TIPC is not set +# CONFIG_ATM is not set +# CONFIG_L2TP is not set +# CONFIG_BRIDGE is not set +# CONFIG_NET_DSA is not set +# CONFIG_VLAN_8021Q is not set +# CONFIG_LLC2 is not set +# CONFIG_ATALK is not set +# CONFIG_X25 is not set +# CONFIG_LAPB is not set +# CONFIG_PHONET is not set +# CONFIG_6LOWPAN is not set +# CONFIG_IEEE802154 is not set +# CONFIG_NET_SCHED is not set +# CONFIG_DCB is not set +# CONFIG_BATMAN_ADV is not set +# CONFIG_OPENVSWITCH is not set +# CONFIG_VSOCKETS is not set +# CONFIG_NETLINK_DIAG is not set +# CONFIG_MPLS is not set +# CONFIG_NET_NSH is not set +# CONFIG_HSR is not set +# CONFIG_NET_SWITCHDEV is not set +# CONFIG_NET_L3_MASTER_DEV is not set +# CONFIG_QRTR is not set +# CONFIG_NET_NCSI is not set +CONFIG_PCPU_DEV_REFCNT=y +CONFIG_MAX_SKB_FRAGS=17 +CONFIG_RPS=y +# CONFIG_RFS_ACCEL is not set +CONFIG_SOCK_RX_QUEUE_MAPPING=y +CONFIG_XPS=y +# CONFIG_CGROUP_NET_PRIO is not set +# CONFIG_CGROUP_NET_CLASSID is not set +CONFIG_NET_RX_BUSY_POLL=y +CONFIG_BQL=y +# CONFIG_BPF_STREAM_PARSER is not set +CONFIG_NET_FLOW_LIMIT=y + +# +# Network testing +# +# CONFIG_NET_PKTGEN is not set +# end of Network testing +# end of Networking options + +# CONFIG_HAMRADIO is not set +# CONFIG_CAN is not set +# CONFIG_BT is not set +# CONFIG_AF_RXRPC is not set +# CONFIG_AF_KCM is not set +# CONFIG_MCTP is not set +# CONFIG_WIRELESS is not set +# CONFIG_RFKILL is not set +# CONFIG_NET_9P is not set +# CONFIG_CAIF is not set +# CONFIG_CEPH_LIB is not set +# CONFIG_NFC is not set +# CONFIG_PSAMPLE is not set +# CONFIG_NET_IFE is not set +# CONFIG_LWTUNNEL is not set +CONFIG_NET_SOCK_MSG=y +CONFIG_PAGE_POOL=y +# CONFIG_PAGE_POOL_STATS is not set +CONFIG_FAILOVER=y +# CONFIG_ETHTOOL_NETLINK is not set + +# +# Device Drivers +# +CONFIG_ARM_AMBA=y +CONFIG_HAVE_PCI=y +CONFIG_GENERIC_PCI_IOMAP=y +# CONFIG_PCI is not set +# CONFIG_PCCARD is not set +# CONFIG_PC104 is not set + +# +# Generic Driver Options +# +# CONFIG_UEVENT_HELPER is not set +CONFIG_DEVTMPFS=y +# CONFIG_DEVTMPFS_MOUNT is not set +# CONFIG_DEVTMPFS_SAFE is not set +# CONFIG_STANDALONE is not set +# CONFIG_PREVENT_FIRMWARE_BUILD is not set + +# +# Firmware loader +# +# CONFIG_FW_LOADER is not set +# end of Firmware loader + +# CONFIG_ALLOW_DEV_COREDUMP is not set +# CONFIG_DEBUG_DRIVER is not set +# CONFIG_DEBUG_DEVRES is not set +# CONFIG_DEBUG_TEST_DRIVER_REMOVE is not set +# CONFIG_TEST_ASYNC_DRIVER_PROBE is not set +CONFIG_GENERIC_CPU_DEVICES=y +CONFIG_GENERIC_CPU_AUTOPROBE=y +CONFIG_GENERIC_CPU_VULNERABILITIES=y +CONFIG_GENERIC_ARCH_TOPOLOGY=y +# CONFIG_FW_DEVLINK_SYNC_STATE_TIMEOUT is not set +# end of Generic Driver Options + +# +# Bus devices +# +# CONFIG_VEXPRESS_CONFIG is not set +# CONFIG_MHI_BUS is not set +# CONFIG_MHI_BUS_EP is not set +# end of Bus devices + +# +# Cache Drivers +# +# end of Cache Drivers + +# CONFIG_CONNECTOR is not set + +# +# Firmware Drivers +# + +# +# ARM System Control and Management Interface Protocol +# +# CONFIG_ARM_SCMI_PROTOCOL is not set +# end of ARM System Control and Management Interface Protocol + +# CONFIG_ARM_SDE_INTERFACE is not set +# CONFIG_FIRMWARE_MEMMAP is not set +# CONFIG_ARM_FFA_TRANSPORT is not set +# CONFIG_GOOGLE_FIRMWARE is not set +CONFIG_ARM_PSCI_FW=y + +# +# Qualcomm firmware drivers +# +# end of Qualcomm firmware drivers + +CONFIG_HAVE_ARM_SMCCC=y +CONFIG_HAVE_ARM_SMCCC_DISCOVERY=y +# CONFIG_ARM_SMCCC_SOC_ID is not set + +# +# Tegra firmware driver +# +# end of Tegra firmware driver +# end of Firmware Drivers + +# CONFIG_FWCTL is not set +# CONFIG_GNSS is not set +# CONFIG_MTD is not set +CONFIG_DTC=y +CONFIG_OF=y +# CONFIG_OF_UNITTEST is not set +CONFIG_OF_FLATTREE=y +CONFIG_OF_EARLY_FLATTREE=y +CONFIG_OF_KOBJ=y +CONFIG_OF_ADDRESS=y +CONFIG_OF_IRQ=y +CONFIG_OF_RESERVED_MEM=y +# CONFIG_OF_OVERLAY is not set +# CONFIG_PARPORT is not set +CONFIG_BLK_DEV=y +# CONFIG_BLK_DEV_NULL_BLK is not set +# CONFIG_ZRAM is not set +# CONFIG_BLK_DEV_LOOP is not set +# CONFIG_BLK_DEV_DRBD is not set +# CONFIG_BLK_DEV_NBD is not set +# CONFIG_BLK_DEV_RAM is not set +# CONFIG_ATA_OVER_ETH is not set +CONFIG_VIRTIO_BLK=y +# CONFIG_BLK_DEV_RBD is not set +# CONFIG_BLK_DEV_UBLK is not set + +# +# NVME Support +# +# CONFIG_NVME_FC is not set +# CONFIG_NVME_TCP is not set +# CONFIG_NVME_TARGET is not set +# end of NVME Support + +# +# Misc devices +# +# CONFIG_DUMMY_IRQ is not set +# CONFIG_ENCLOSURE_SERVICES is not set +# CONFIG_SRAM is not set +# CONFIG_XILINX_SDFEC is not set +# CONFIG_OPEN_DICE is not set +# CONFIG_NTSYNC is not set +# CONFIG_VCPU_STALL_DETECTOR is not set +# CONFIG_NSM is not set +# CONFIG_C2PORT is not set + +# +# EEPROM support +# +# CONFIG_EEPROM_93CX6 is not set +# end of EEPROM support + +# +# Altera FPGA firmware download module (requires I2C) +# +# CONFIG_PVPANIC is not set +# end of Misc devices + +# +# SCSI device support +# +CONFIG_SCSI_MOD=y +# CONFIG_RAID_ATTRS is not set +# CONFIG_SCSI is not set +# end of SCSI device support + +# CONFIG_ATA is not set +# CONFIG_MD is not set +# CONFIG_TARGET_CORE is not set +CONFIG_NETDEVICES=y +CONFIG_NET_CORE=y +# CONFIG_BONDING is not set +# CONFIG_DUMMY is not set +# CONFIG_WIREGUARD is not set +# CONFIG_OVPN is not set +# CONFIG_EQUALIZER is not set +# CONFIG_NET_TEAM is not set +# CONFIG_MACVLAN is not set +# CONFIG_IPVLAN is not set +# CONFIG_VXLAN is not set +# CONFIG_GENEVE is not set +# CONFIG_BAREUDP is not set +# CONFIG_GTP is not set +# CONFIG_PFCP is not set +# CONFIG_MACSEC is not set +# CONFIG_NETCONSOLE is not set +# CONFIG_TUN is not set +# CONFIG_TUN_VNET_CROSS_LE is not set +CONFIG_VETH=y +CONFIG_VIRTIO_NET=y +# CONFIG_NLMON is not set +# CONFIG_NETKIT is not set +# CONFIG_ETHERNET is not set +# CONFIG_MDIO_BUS is not set +# CONFIG_PHYLIB is not set + +# +# PCS device drivers +# +# CONFIG_PCS_XPCS is not set +# end of PCS device drivers + +# CONFIG_PPP is not set +# CONFIG_SLIP is not set + +# +# Host-side USB support is needed for USB Network Adapter support +# +# CONFIG_WLAN is not set +# CONFIG_WAN is not set + +# +# Wireless WAN +# +# CONFIG_WWAN is not set +# end of Wireless WAN + +# CONFIG_NETDEVSIM is not set +CONFIG_NET_FAILOVER=y +# CONFIG_ISDN is not set + +# +# Input device support +# +# CONFIG_INPUT is not set + +# +# Hardware I/O ports +# +# CONFIG_SERIO is not set +# CONFIG_GAMEPORT is not set +# end of Hardware I/O ports +# end of Input device support + +# +# Character devices +# +CONFIG_TTY=y +# CONFIG_VT is not set +CONFIG_UNIX98_PTYS=y +# CONFIG_LEGACY_PTYS is not set +# CONFIG_LEGACY_TIOCSTI is not set +# CONFIG_LDISC_AUTOLOAD is not set + +# +# Serial drivers +# +CONFIG_SERIAL_EARLYCON=y +# CONFIG_SERIAL_8250 is not set + +# +# Non-8250 serial port support +# +# CONFIG_SERIAL_AMBA_PL010 is not set +# CONFIG_SERIAL_AMBA_PL011 is not set +# CONFIG_SERIAL_EARLYCON_SEMIHOST is not set +# CONFIG_SERIAL_UARTLITE is not set +CONFIG_SERIAL_CORE=y +CONFIG_SERIAL_CORE_CONSOLE=y +# CONFIG_SERIAL_SIFIVE is not set +# CONFIG_SERIAL_SCCNXP is not set +# CONFIG_SERIAL_ALTERA_JTAGUART is not set +# CONFIG_SERIAL_ALTERA_UART is not set +# CONFIG_SERIAL_XILINX_PS_UART is not set +# CONFIG_SERIAL_ARC is not set +# CONFIG_SERIAL_FSL_LPUART is not set +# CONFIG_SERIAL_FSL_LINFLEXUART is not set +# CONFIG_SERIAL_CONEXANT_DIGICOLOR is not set +# CONFIG_SERIAL_SPRD is not set +# end of Serial drivers + +# CONFIG_SERIAL_NONSTANDARD is not set +# CONFIG_N_GSM is not set +# CONFIG_NULL_TTY is not set +CONFIG_HVC_DRIVER=y +CONFIG_HVC_NVX_MMIO=y +# CONFIG_HVC_DCC is not set +# CONFIG_SERIAL_DEV_BUS is not set +# CONFIG_TTY_PRINTK is not set +CONFIG_VIRTIO_CONSOLE=y +# CONFIG_IPMI_HANDLER is not set +# CONFIG_HW_RANDOM is not set +# CONFIG_DEVMEM is not set +# CONFIG_DEVPORT is not set +# CONFIG_TCG_TPM is not set +# CONFIG_XILLYBUS is not set +# end of Character devices + +# +# I2C support +# +# CONFIG_I2C is not set +# end of I2C support + +# CONFIG_I3C is not set +# CONFIG_SPI is not set +# CONFIG_SPMI is not set +# CONFIG_HSI is not set +# CONFIG_PPS is not set + +# +# PTP clock support +# +# CONFIG_PTP_1588_CLOCK is not set +CONFIG_PTP_1588_CLOCK_OPTIONAL=y + +# +# Enable PHYLIB and NETWORK_PHY_TIMESTAMPING to see the additional clocks. +# +# end of PTP clock support + +# +# DPLL device support +# +# end of DPLL device support + +# CONFIG_PINCTRL is not set +CONFIG_GPIOLIB_LEGACY=y +# CONFIG_GPIOLIB is not set +# CONFIG_W1 is not set +CONFIG_POWER_RESET=y +# CONFIG_POWER_RESET_RESTART is not set +# CONFIG_POWER_RESET_XGENE is not set +# CONFIG_POWER_RESET_SYSCON is not set +# CONFIG_POWER_RESET_SYSCON_POWEROFF is not set +# CONFIG_NVMEM_REBOOT_MODE is not set +# CONFIG_POWER_SEQUENCING is not set +CONFIG_POWER_SUPPLY=y +# CONFIG_POWER_SUPPLY_DEBUG is not set +# CONFIG_TEST_POWER is not set +# CONFIG_BATTERY_DS2780 is not set +# CONFIG_BATTERY_DS2781 is not set +# CONFIG_BATTERY_SAMSUNG_SDI is not set +# CONFIG_BATTERY_BQ27XXX is not set +# CONFIG_CHARGER_MAX8903 is not set +# CONFIG_BATTERY_GOLDFISH is not set +# CONFIG_HWMON is not set +# CONFIG_THERMAL is not set +# CONFIG_WATCHDOG is not set +CONFIG_SSB_POSSIBLE=y +# CONFIG_SSB is not set +CONFIG_BCMA_POSSIBLE=y +# CONFIG_BCMA is not set + +# +# Multifunction device drivers +# +# CONFIG_MFD_ATMEL_FLEXCOM is not set +# CONFIG_MFD_ATMEL_HLCDC is not set +# CONFIG_MFD_MADERA is not set +# CONFIG_MFD_HI6421_PMIC is not set +# CONFIG_MFD_KEMPLD is not set +# CONFIG_MFD_MT6397 is not set +# CONFIG_MFD_SM501 is not set +# CONFIG_MFD_SYSCON is not set +# CONFIG_MFD_TQMX86 is not set +# end of Multifunction device drivers + +# CONFIG_REGULATOR is not set + +# +# CEC support +# +# CONFIG_MEDIA_CEC_SUPPORT is not set +# end of CEC support + +# CONFIG_MEDIA_SUPPORT is not set + +# +# Graphics support +# +# CONFIG_AUXDISPLAY is not set +# CONFIG_DRM is not set + +# +# Frame buffer Devices +# +# CONFIG_FB is not set +# end of Frame buffer Devices + +# +# Backlight & LCD device support +# +# CONFIG_LCD_CLASS_DEVICE is not set +# CONFIG_BACKLIGHT_CLASS_DEVICE is not set +# end of Backlight & LCD device support + +# CONFIG_TRACE_GPU_MEM is not set +# end of Graphics support + +# CONFIG_SOUND is not set +CONFIG_USB_OHCI_LITTLE_ENDIAN=y +# CONFIG_USB_SUPPORT is not set +# CONFIG_MMC is not set +# CONFIG_MEMSTICK is not set +# CONFIG_NEW_LEDS is not set +# CONFIG_ACCESSIBILITY is not set +# CONFIG_INFINIBAND is not set +CONFIG_EDAC_SUPPORT=y +# CONFIG_RTC_CLASS is not set +# CONFIG_DMADEVICES is not set + +# +# DMABUF options +# +# CONFIG_SYNC_FILE is not set +# CONFIG_DMABUF_HEAPS is not set +# end of DMABUF options + +# CONFIG_UIO is not set +# CONFIG_VFIO is not set +# CONFIG_VIRT_DRIVERS is not set +CONFIG_VIRTIO_ANCHOR=y +CONFIG_VIRTIO=y +CONFIG_VIRTIO_MENU=y +# CONFIG_VIRTIO_BALLOON is not set +CONFIG_VIRTIO_MMIO=y +# CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES is not set +CONFIG_VIRTIO_MMIO_SHARED_STATUS=y +# CONFIG_VIRTIO_DEBUG is not set +# CONFIG_VIRTIO_RTC is not set +# CONFIG_VDPA is not set +# CONFIG_VHOST_MENU is not set + +# +# Microsoft Hyper-V guest support +# +# CONFIG_HYPERV is not set +# end of Microsoft Hyper-V guest support + +# CONFIG_GREYBUS is not set +# CONFIG_COMEDI is not set +# CONFIG_STAGING is not set +# CONFIG_GOLDFISH is not set +# CONFIG_CHROME_PLATFORMS is not set +# CONFIG_MELLANOX_PLATFORM is not set +# CONFIG_SURFACE_PLATFORMS is not set +# CONFIG_ARM64_PLATFORM_DEVICES is not set +CONFIG_HAVE_CLK=y +CONFIG_HAVE_CLK_PREPARE=y +CONFIG_COMMON_CLK=y + +# +# Clock driver for ARM Reference designs +# +# CONFIG_CLK_ICST is not set +# CONFIG_CLK_SP810 is not set +# end of Clock driver for ARM Reference designs + +# CONFIG_COMMON_CLK_AXI_CLKGEN is not set +# CONFIG_COMMON_CLK_XGENE is not set +# CONFIG_COMMON_CLK_FIXED_MMIO is not set +# CONFIG_XILINX_VCU is not set +# CONFIG_COMMON_CLK_XLNX_CLKWZRD is not set +# CONFIG_HWSPINLOCK is not set + +# +# Clock Source drivers +# +CONFIG_TIMER_OF=y +CONFIG_TIMER_PROBE=y +CONFIG_ARM_ARCH_TIMER=y +# CONFIG_ARM_ARCH_TIMER_EVTSTREAM is not set +# CONFIG_FSL_ERRATUM_A008585 is not set +# CONFIG_HISILICON_ERRATUM_161010101 is not set +# CONFIG_ARM64_ERRATUM_858921 is not set +# CONFIG_ARM_TIMER_SP804 is not set +# end of Clock Source drivers + +# CONFIG_MAILBOX is not set +# CONFIG_IOMMU_SUPPORT is not set + +# +# Remoteproc drivers +# +# CONFIG_REMOTEPROC is not set +# end of Remoteproc drivers + +# +# Rpmsg drivers +# +# CONFIG_RPMSG_VIRTIO is not set +# end of Rpmsg drivers + +# +# SOC (System On Chip) specific Drivers +# + +# +# Amlogic SoC drivers +# +# end of Amlogic SoC drivers + +# +# Broadcom SoC drivers +# +# end of Broadcom SoC drivers + +# +# NXP/Freescale QorIQ SoC drivers +# +# CONFIG_QUICC_ENGINE is not set +# end of NXP/Freescale QorIQ SoC drivers + +# +# fujitsu SoC drivers +# +# end of fujitsu SoC drivers + +# +# i.MX SoC drivers +# +# end of i.MX SoC drivers + +# +# Enable LiteX SoC Builder specific drivers +# +# CONFIG_LITEX_SOC_CONTROLLER is not set +# end of Enable LiteX SoC Builder specific drivers + +# CONFIG_WPCM450_SOC is not set + +# +# Qualcomm SoC drivers +# +# end of Qualcomm SoC drivers + +# CONFIG_SOC_TI is not set + +# +# Xilinx SoC drivers +# +# end of Xilinx SoC drivers +# end of SOC (System On Chip) specific Drivers + +# +# PM Domains +# + +# +# Amlogic PM Domains +# +# end of Amlogic PM Domains + +# +# Broadcom PM Domains +# +# end of Broadcom PM Domains + +# +# i.MX PM Domains +# +# end of i.MX PM Domains + +# +# Qualcomm PM Domains +# +# end of Qualcomm PM Domains +# end of PM Domains + +# CONFIG_PM_DEVFREQ is not set +# CONFIG_EXTCON is not set +# CONFIG_MEMORY is not set +# CONFIG_IIO is not set +# CONFIG_PWM is not set + +# +# IRQ chip support +# +CONFIG_IRQCHIP=y +CONFIG_ARM_GIC=y +CONFIG_ARM_GIC_MAX_NR=1 +CONFIG_ARM_GIC_V3=y +CONFIG_ARM_GIC_ITS_PARENT=y +CONFIG_ARM_GIC_V3_ITS=y +CONFIG_ARM_GIC_V5=y +CONFIG_IRQ_MSI_LIB=y +# CONFIG_AL_FIC is not set +# CONFIG_XILINX_INTC is not set +CONFIG_PARTITION_PERCPU=y +# end of IRQ chip support + +# CONFIG_IPACK_BUS is not set +# CONFIG_RESET_CONTROLLER is not set + +# +# PHY Subsystem +# +# CONFIG_GENERIC_PHY is not set +# CONFIG_PHY_CAN_TRANSCEIVER is not set + +# +# PHY drivers for Broadcom platforms +# +# CONFIG_BCM_KONA_USB2_PHY is not set +# end of PHY drivers for Broadcom platforms + +# CONFIG_PHY_CADENCE_TORRENT is not set +# CONFIG_PHY_CADENCE_DPHY is not set +# CONFIG_PHY_CADENCE_DPHY_RX is not set +# CONFIG_PHY_CADENCE_SALVO is not set +# CONFIG_PHY_PXA_28NM_HSIC is not set +# CONFIG_PHY_PXA_28NM_USB2 is not set +# end of PHY Subsystem + +# CONFIG_POWERCAP is not set +# CONFIG_MCB is not set +# CONFIG_RAS is not set + +# +# Android +# +# CONFIG_ANDROID_BINDER_IPC is not set +# end of Android + +# CONFIG_LIBNVDIMM is not set +# CONFIG_DAX is not set +# CONFIG_NVMEM is not set + +# +# HW tracing support +# +# CONFIG_STM is not set +# CONFIG_INTEL_TH is not set +# end of HW tracing support + +# CONFIG_FPGA is not set +# CONFIG_FSI is not set +# CONFIG_TEE is not set +# CONFIG_SIOX is not set +# CONFIG_SLIMBUS is not set +# CONFIG_INTERCONNECT is not set +# CONFIG_COUNTER is not set +# CONFIG_PECI is not set +# CONFIG_HTE is not set +# CONFIG_CDX_BUS is not set +# end of Device Drivers + +# +# File systems +# +CONFIG_DCACHE_WORD_ACCESS=y +# CONFIG_VALIDATE_FS_PARSER is not set +CONFIG_FS_IOMAP=y +CONFIG_FS_STACK=y +CONFIG_BUFFER_HEAD=y +# CONFIG_EXT2_FS is not set +CONFIG_EXT4_FS=y +# CONFIG_EXT4_USE_FOR_EXT2 is not set +CONFIG_EXT4_FS_POSIX_ACL=y +CONFIG_EXT4_FS_SECURITY=y +# CONFIG_EXT4_DEBUG is not set +CONFIG_JBD2=y +# CONFIG_JBD2_DEBUG is not set +CONFIG_FS_MBCACHE=y +# CONFIG_JFS_FS is not set +# CONFIG_XFS_FS is not set +# CONFIG_GFS2_FS is not set +# CONFIG_BTRFS_FS is not set +# CONFIG_NILFS2_FS is not set +# CONFIG_F2FS_FS is not set +CONFIG_FS_POSIX_ACL=y +CONFIG_EXPORTFS=y +# CONFIG_EXPORTFS_BLOCK_OPS is not set +CONFIG_FILE_LOCKING=y +# CONFIG_FS_ENCRYPTION is not set +# CONFIG_FS_VERITY is not set +CONFIG_FSNOTIFY=y +CONFIG_DNOTIFY=y +CONFIG_INOTIFY_USER=y +# CONFIG_FANOTIFY is not set +# CONFIG_QUOTA is not set +# CONFIG_AUTOFS_FS is not set +CONFIG_FUSE_FS=y +# CONFIG_CUSE is not set +CONFIG_VIRTIO_FS=y +# CONFIG_FUSE_PASSTHROUGH is not set +# CONFIG_FUSE_IO_URING is not set +CONFIG_OVERLAY_FS=y +# CONFIG_OVERLAY_FS_REDIRECT_DIR is not set +# CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW is not set +# CONFIG_OVERLAY_FS_INDEX is not set +# CONFIG_OVERLAY_FS_XINO_AUTO is not set +# CONFIG_OVERLAY_FS_METACOPY is not set +# CONFIG_OVERLAY_FS_DEBUG is not set + +# +# Caches +# +# end of Caches + +# +# CD-ROM/DVD Filesystems +# +# CONFIG_ISO9660_FS is not set +# CONFIG_UDF_FS is not set +# end of CD-ROM/DVD Filesystems + +# +# DOS/FAT/EXFAT/NT Filesystems +# +# CONFIG_MSDOS_FS is not set +# CONFIG_VFAT_FS is not set +# CONFIG_EXFAT_FS is not set +# CONFIG_NTFS3_FS is not set +# CONFIG_NTFS_FS is not set +# end of DOS/FAT/EXFAT/NT Filesystems + +# +# Pseudo filesystems +# +CONFIG_PROC_FS=y +# CONFIG_PROC_KCORE is not set +CONFIG_PROC_SYSCTL=y +CONFIG_PROC_PAGE_MONITOR=y +# CONFIG_PROC_CHILDREN is not set +CONFIG_KERNFS=y +CONFIG_SYSFS=y +CONFIG_TMPFS=y +CONFIG_TMPFS_POSIX_ACL=y +CONFIG_TMPFS_XATTR=y +# CONFIG_TMPFS_INODE64 is not set +# CONFIG_TMPFS_QUOTA is not set +CONFIG_ARCH_SUPPORTS_HUGETLBFS=y +# CONFIG_HUGETLBFS is not set +CONFIG_ARCH_HAS_GIGANTIC_PAGE=y +# CONFIG_CONFIGFS_FS is not set +# end of Pseudo filesystems + +CONFIG_MISC_FILESYSTEMS=y +# CONFIG_ORANGEFS_FS is not set +# CONFIG_ADFS_FS is not set +# CONFIG_AFFS_FS is not set +# CONFIG_HFS_FS is not set +# CONFIG_HFSPLUS_FS is not set +# CONFIG_BEFS_FS is not set +# CONFIG_BFS_FS is not set +# CONFIG_EFS_FS is not set +# CONFIG_CRAMFS is not set +# CONFIG_SQUASHFS is not set +# CONFIG_VXFS_FS is not set +# CONFIG_MINIX_FS is not set +# CONFIG_OMFS_FS is not set +# CONFIG_HPFS_FS is not set +# CONFIG_QNX4FS_FS is not set +# CONFIG_QNX6FS_FS is not set +# CONFIG_ROMFS_FS is not set +# CONFIG_PSTORE is not set +# CONFIG_UFS_FS is not set +CONFIG_EROFS_FS=y +# CONFIG_EROFS_FS_DEBUG is not set +CONFIG_EROFS_FS_XATTR=y +CONFIG_EROFS_FS_POSIX_ACL=y +CONFIG_EROFS_FS_SECURITY=y +# CONFIG_EROFS_FS_BACKED_BY_FILE is not set +CONFIG_EROFS_FS_ZIP=y +# CONFIG_EROFS_FS_ZIP_LZMA is not set +# CONFIG_EROFS_FS_ZIP_DEFLATE is not set +CONFIG_EROFS_FS_ZIP_ZSTD=y +# CONFIG_EROFS_FS_ZIP_ACCEL is not set +# CONFIG_EROFS_FS_ONDEMAND is not set +# CONFIG_EROFS_FS_PCPU_KTHREAD is not set +# CONFIG_NETWORK_FILESYSTEMS is not set +# CONFIG_NLS is not set +# CONFIG_UNICODE is not set +CONFIG_IO_WQ=y +# end of File systems + +# +# Security options +# +# CONFIG_KEYS is not set +# CONFIG_SECURITY_DMESG_RESTRICT is not set +CONFIG_PROC_MEM_ALWAYS_FORCE=y +# CONFIG_PROC_MEM_FORCE_PTRACE is not set +# CONFIG_PROC_MEM_NO_FORCE is not set +# CONFIG_MSEAL_SYSTEM_MAPPINGS is not set +# CONFIG_SECURITY is not set +# CONFIG_SECURITYFS is not set +# CONFIG_STATIC_USERMODEHELPER is not set +CONFIG_DEFAULT_SECURITY_DAC=y + +# +# Kernel hardening options +# + +# +# Memory initialization +# +CONFIG_CC_HAS_AUTO_VAR_INIT_PATTERN=y +CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO_BARE=y +CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO=y +# CONFIG_INIT_STACK_NONE is not set +# CONFIG_INIT_STACK_ALL_PATTERN is not set +CONFIG_INIT_STACK_ALL_ZERO=y +# CONFIG_INIT_ON_ALLOC_DEFAULT_ON is not set +# CONFIG_INIT_ON_FREE_DEFAULT_ON is not set +CONFIG_CC_HAS_ZERO_CALL_USED_REGS=y +# CONFIG_ZERO_CALL_USED_REGS is not set +# end of Memory initialization + +# +# Bounds checking +# +# CONFIG_FORTIFY_SOURCE is not set +# CONFIG_HARDENED_USERCOPY is not set +# end of Bounds checking + +# +# Hardening of kernel data structures +# +# CONFIG_LIST_HARDENED is not set +# CONFIG_BUG_ON_DATA_CORRUPTION is not set +# end of Hardening of kernel data structures + +CONFIG_RANDSTRUCT_NONE=y +# end of Kernel hardening options +# end of Security options + +# CONFIG_CRYPTO is not set +CONFIG_BINARY_PRINTF=y + +# +# Library routines +# +# CONFIG_PACKING is not set +CONFIG_BITREVERSE=y +CONFIG_HAVE_ARCH_BITREVERSE=y +CONFIG_GENERIC_STRNCPY_FROM_USER=y +CONFIG_GENERIC_STRNLEN_USER=y +CONFIG_GENERIC_NET_UTILS=y +# CONFIG_CORDIC is not set +# CONFIG_PRIME_NUMBERS is not set +CONFIG_RATIONAL=y +CONFIG_ARCH_USE_CMPXCHG_LOCKREF=y +CONFIG_ARCH_HAS_FAST_MULTIPLIER=y +CONFIG_ARCH_USE_SYM_ANNOTATIONS=y +# CONFIG_INDIRECT_PIO is not set +CONFIG_CRC16=y +CONFIG_CRC32=y +CONFIG_CRC32_ARCH=y +CONFIG_CRC_OPTIMIZATIONS=y + +# +# Crypto library routines +# +CONFIG_CRYPTO_LIB_POLY1305_RSIZE=9 +CONFIG_CRYPTO_LIB_SHA1=y +CONFIG_CRYPTO_LIB_SHA1_ARCH=y +CONFIG_CRYPTO_LIB_SHA256=y +CONFIG_CRYPTO_LIB_SHA256_ARCH=y +# end of Crypto library routines + +CONFIG_XXHASH=y +CONFIG_AUDIT_ARCH_COMPAT_GENERIC=y +# CONFIG_RANDOM32_SELFTEST is not set +CONFIG_ZLIB_INFLATE=y +CONFIG_LZ4_DECOMPRESS=y +CONFIG_ZSTD_COMMON=y +CONFIG_ZSTD_DECOMPRESS=y +# CONFIG_XZ_DEC is not set +CONFIG_DECOMPRESS_GZIP=y +CONFIG_GENERIC_ALLOCATOR=y +CONFIG_HAS_IOMEM=y +CONFIG_HAS_IOPORT=y +CONFIG_HAS_DMA=y +CONFIG_NEED_SG_DMA_LENGTH=y +CONFIG_NEED_DMA_MAP_STATE=y +CONFIG_ARCH_DMA_ADDR_T_64BIT=y +CONFIG_DMA_DECLARE_COHERENT=y +CONFIG_ARCH_HAS_SETUP_DMA_OPS=y +CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE=y +CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU=y +CONFIG_ARCH_HAS_DMA_PREP_COHERENT=y +CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED=y +CONFIG_SWIOTLB=y +# CONFIG_SWIOTLB_DYNAMIC is not set +CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC=y +CONFIG_DMA_NEED_SYNC=y +# CONFIG_DMA_RESTRICTED_POOL is not set +CONFIG_DMA_NONCOHERENT_MMAP=y +CONFIG_DMA_COHERENT_POOL=y +CONFIG_DMA_DIRECT_REMAP=y +# CONFIG_DMA_API_DEBUG is not set +# CONFIG_DMA_MAP_BENCHMARK is not set +CONFIG_DQL=y +CONFIG_NLATTR=y +# CONFIG_IRQ_POLL is not set +CONFIG_DIMLIB=y +CONFIG_LIBFDT=y +CONFIG_HAVE_GENERIC_VDSO=y +CONFIG_GENERIC_GETTIMEOFDAY=y +CONFIG_VDSO_GETRANDOM=y +CONFIG_SG_POOL=y +CONFIG_ARCH_STACKWALK=y +CONFIG_SBITMAP=y +# CONFIG_LWQ_TEST is not set +# end of Library routines + +CONFIG_GENERIC_IOREMAP=y +CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED=y + +# +# Kernel hacking +# + +# +# printk and dmesg options +# +CONFIG_PRINTK_TIME=y +# CONFIG_PRINTK_CALLER is not set +# CONFIG_STACKTRACE_BUILD_ID is not set +CONFIG_CONSOLE_LOGLEVEL_DEFAULT=7 +CONFIG_CONSOLE_LOGLEVEL_QUIET=4 +CONFIG_MESSAGE_LOGLEVEL_DEFAULT=4 +# CONFIG_BOOT_PRINTK_DELAY is not set +# CONFIG_DYNAMIC_DEBUG is not set +# CONFIG_DYNAMIC_DEBUG_CORE is not set +CONFIG_SYMBOLIC_ERRNAME=y +CONFIG_DEBUG_BUGVERBOSE=y +# end of printk and dmesg options + +CONFIG_DEBUG_KERNEL=y +# CONFIG_DEBUG_MISC is not set + +# +# Compile-time checks and compiler options +# +CONFIG_AS_HAS_NON_CONST_ULEB128=y +CONFIG_DEBUG_INFO_NONE=y +# CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT is not set +# CONFIG_DEBUG_INFO_DWARF4 is not set +# CONFIG_DEBUG_INFO_DWARF5 is not set +CONFIG_FRAME_WARN=2048 +# CONFIG_STRIP_ASM_SYMS is not set +# CONFIG_READABLE_ASM is not set +# CONFIG_HEADERS_INSTALL is not set +# CONFIG_DEBUG_SECTION_MISMATCH is not set +CONFIG_SECTION_MISMATCH_WARN_ONLY=y +# CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B is not set +CONFIG_ARCH_WANT_FRAME_POINTERS=y +CONFIG_FRAME_POINTER=y +# CONFIG_VMLINUX_MAP is not set +# CONFIG_DEBUG_FORCE_WEAK_PER_CPU is not set +# end of Compile-time checks and compiler options + +# +# Generic Kernel Debugging Instruments +# +CONFIG_MAGIC_SYSRQ=y +CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1 +# CONFIG_MAGIC_SYSRQ_SERIAL is not set +CONFIG_DEBUG_FS=y +CONFIG_DEBUG_FS_ALLOW_ALL=y +# CONFIG_DEBUG_FS_DISALLOW_MOUNT is not set +# CONFIG_DEBUG_FS_ALLOW_NONE is not set +CONFIG_HAVE_ARCH_KGDB=y +# CONFIG_KGDB is not set +CONFIG_ARCH_HAS_UBSAN=y +# CONFIG_UBSAN is not set +CONFIG_HAVE_ARCH_KCSAN=y +CONFIG_HAVE_KCSAN_COMPILER=y +# CONFIG_KCSAN is not set +# end of Generic Kernel Debugging Instruments + +# +# Networking Debugging +# +# CONFIG_NET_DEV_REFCNT_TRACKER is not set +# CONFIG_NET_NS_REFCNT_TRACKER is not set +# CONFIG_DEBUG_NET is not set +# CONFIG_DEBUG_NET_SMALL_RTNL is not set +# end of Networking Debugging + +# +# Memory Debugging +# +# CONFIG_PAGE_EXTENSION is not set +# CONFIG_DEBUG_PAGEALLOC is not set +# CONFIG_SLUB_DEBUG is not set +# CONFIG_PAGE_OWNER is not set +# CONFIG_PAGE_TABLE_CHECK is not set +# CONFIG_PAGE_POISONING is not set +# CONFIG_DEBUG_RODATA_TEST is not set +CONFIG_ARCH_HAS_DEBUG_WX=y +CONFIG_DEBUG_WX=y +CONFIG_ARCH_HAS_PTDUMP=y +CONFIG_PTDUMP=y +# CONFIG_PTDUMP_DEBUGFS is not set +CONFIG_HAVE_DEBUG_KMEMLEAK=y +# CONFIG_DEBUG_KMEMLEAK is not set +# CONFIG_PER_VMA_LOCK_STATS is not set +# CONFIG_DEBUG_OBJECTS is not set +# CONFIG_SHRINKER_DEBUG is not set +# CONFIG_DEBUG_STACK_USAGE is not set +# CONFIG_SCHED_STACK_END_CHECK is not set +CONFIG_ARCH_HAS_DEBUG_VM_PGTABLE=y +# CONFIG_DEBUG_VFS is not set +# CONFIG_DEBUG_VM is not set +# CONFIG_DEBUG_VM_PGTABLE is not set +CONFIG_ARCH_HAS_DEBUG_VIRTUAL=y +# CONFIG_DEBUG_VIRTUAL is not set +# CONFIG_DEBUG_MEMORY_INIT is not set +# CONFIG_DEBUG_PER_CPU_MAPS is not set +# CONFIG_MEM_ALLOC_PROFILING is not set +CONFIG_HAVE_ARCH_KASAN=y +CONFIG_HAVE_ARCH_KASAN_SW_TAGS=y +CONFIG_HAVE_ARCH_KASAN_VMALLOC=y +CONFIG_CC_HAS_KASAN_GENERIC=y +CONFIG_CC_HAS_KASAN_SW_TAGS=y +CONFIG_CC_HAS_WORKING_NOSANITIZE_ADDRESS=y +# CONFIG_KASAN is not set +CONFIG_HAVE_ARCH_KFENCE=y +# CONFIG_KFENCE is not set +# end of Memory Debugging + +# CONFIG_DEBUG_SHIRQ is not set + +# +# Debug Oops, Lockups and Hangs +# +# CONFIG_PANIC_ON_OOPS is not set +CONFIG_PANIC_TIMEOUT=0 +# CONFIG_SOFTLOCKUP_DETECTOR is not set +CONFIG_HAVE_HARDLOCKUP_DETECTOR_BUDDY=y +# CONFIG_HARDLOCKUP_DETECTOR is not set +# CONFIG_DETECT_HUNG_TASK is not set +# CONFIG_WQ_WATCHDOG is not set +# CONFIG_WQ_CPU_INTENSIVE_REPORT is not set +# CONFIG_TEST_LOCKUP is not set +# end of Debug Oops, Lockups and Hangs + +# +# Scheduler Debugging +# +# CONFIG_SCHEDSTATS is not set +# end of Scheduler Debugging + +# CONFIG_DEBUG_PREEMPT is not set + +# +# Lock Debugging (spinlocks, mutexes, etc...) +# +CONFIG_LOCK_DEBUGGING_SUPPORT=y +# CONFIG_PROVE_LOCKING is not set +# CONFIG_LOCK_STAT is not set +# CONFIG_DEBUG_RT_MUTEXES is not set +# CONFIG_DEBUG_SPINLOCK is not set +# CONFIG_DEBUG_MUTEXES is not set +# CONFIG_DEBUG_WW_MUTEX_SLOWPATH is not set +# CONFIG_DEBUG_RWSEMS is not set +# CONFIG_DEBUG_LOCK_ALLOC is not set +# CONFIG_DEBUG_ATOMIC_SLEEP is not set +# CONFIG_DEBUG_LOCKING_API_SELFTESTS is not set +# CONFIG_LOCK_TORTURE_TEST is not set +# CONFIG_WW_MUTEX_SELFTEST is not set +# CONFIG_SCF_TORTURE_TEST is not set +# CONFIG_CSD_LOCK_WAIT_DEBUG is not set +# end of Lock Debugging (spinlocks, mutexes, etc...) + +# CONFIG_DEBUG_IRQFLAGS is not set +# CONFIG_STACKTRACE is not set +# CONFIG_DEBUG_KOBJECT is not set + +# +# Debug kernel data structures +# +# CONFIG_DEBUG_LIST is not set +# CONFIG_DEBUG_PLIST is not set +# CONFIG_DEBUG_SG is not set +# CONFIG_DEBUG_NOTIFIERS is not set +# CONFIG_DEBUG_MAPLE_TREE is not set +# end of Debug kernel data structures + +# +# RCU Debugging +# +# CONFIG_RCU_SCALE_TEST is not set +# CONFIG_RCU_TORTURE_TEST is not set +# CONFIG_RCU_REF_SCALE_TEST is not set +CONFIG_RCU_CPU_STALL_TIMEOUT=21 +CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 +# CONFIG_RCU_CPU_STALL_CPUTIME is not set +# CONFIG_RCU_TRACE is not set +# CONFIG_RCU_EQS_DEBUG is not set +# end of RCU Debugging + +# CONFIG_DEBUG_WQ_FORCE_RR_CPU is not set +# CONFIG_LATENCYTOP is not set +CONFIG_USER_STACKTRACE_SUPPORT=y +CONFIG_HAVE_FUNCTION_TRACER=y +CONFIG_HAVE_FUNCTION_GRAPH_TRACER=y +CONFIG_HAVE_FUNCTION_GRAPH_FREGS=y +CONFIG_HAVE_FTRACE_GRAPH_FUNC=y +CONFIG_HAVE_DYNAMIC_FTRACE=y +CONFIG_HAVE_EXTRA_IPI_TRACEPOINTS=y +CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS=y +CONFIG_HAVE_SYSCALL_TRACEPOINTS=y +CONFIG_HAVE_C_RECORDMCOUNT=y +CONFIG_HAVE_BUILDTIME_MCOUNT_SORT=y +CONFIG_TRACING_SUPPORT=y +# CONFIG_FTRACE is not set +# CONFIG_SAMPLES is not set +CONFIG_HAVE_SAMPLE_FTRACE_DIRECT=y +CONFIG_HAVE_SAMPLE_FTRACE_DIRECT_MULTI=y + +# +# arm64 Debugging +# +# CONFIG_PID_IN_CONTEXTIDR is not set +# CONFIG_ARM64_RELOC_TEST is not set +# CONFIG_CORESIGHT is not set +# end of arm64 Debugging + +# +# Kernel Testing and Coverage +# +# CONFIG_KUNIT is not set +# CONFIG_NOTIFIER_ERROR_INJECTION is not set +# CONFIG_FAULT_INJECTION is not set +CONFIG_ARCH_HAS_KCOV=y +# CONFIG_KCOV is not set +# CONFIG_RUNTIME_TESTING_MENU is not set +CONFIG_ARCH_USE_MEMTEST=y +# CONFIG_MEMTEST is not set +# end of Kernel Testing and Coverage + +# +# Rust hacking +# +# end of Rust hacking +# end of Kernel hacking + +CONFIG_IO_URING_ZCRX=y + +# +# Inventory settings whose symbols this configuration's toolchain hides +# +# CONFIG_ACPI is not set +# CONFIG_CPU_BIG_ENDIAN is not set +# CONFIG_GCC_PLUGINS is not set +# CONFIG_PT_RECLAIM is not set +# CONFIG_SERIAL_AMBA_PL011_CONSOLE is not set diff --git a/kernel/config-microvm-arm64.derived b/kernel/config-microvm-arm64.derived new file mode 100644 index 000000000..bcbd5b69c --- /dev/null +++ b/kernel/config-microvm-arm64.derived @@ -0,0 +1,496 @@ +# Generated by `python3 scripts/nvx.py kernel-config` from the kernel feature +# inventory, scripts/nvx_tools/kernel_features.py. Do not edit. +# +# Each symbol that kernel/config-microvm sets and that no inventory setting +# sets, with the Kconfig rule that sets it: `selected by` the symbols whose +# select applies; `implied by`; `choice default`, the default member of its +# choice; `default`, for a value that keeps its default, or for a symbol whose +# prompt is hidden, with the condition that hides it; or `no prompt`, for a +# symbol that no user can set, which its architecture, its toolchain, or other +# symbols decide. +# +# Toolchain: gcc (Debian 12.2.0-14+deb12u1) 12.2.0; AS_VERSION=24000; LD_VERSION=24000 +CONFIG_64BIT=y # no prompt +CONFIG_ARCH_BINFMT_ELF_EXTRA_PHDRS=y # no prompt +CONFIG_ARCH_BINFMT_ELF_STATE=y # no prompt +CONFIG_ARCH_DEFAULT_CRASH_DUMP=y # no prompt +CONFIG_ARCH_DEFAULT_KEXEC_IMAGE_VERIFY_SIG=y # no prompt +CONFIG_ARCH_DMA_ADDR_T_64BIT=y # no prompt +CONFIG_ARCH_ENABLE_MEMORY_HOTPLUG=y # no prompt +CONFIG_ARCH_ENABLE_MEMORY_HOTREMOVE=y # no prompt +CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK=y # no prompt +CONFIG_ARCH_FORCE_MAX_ORDER=10 # no prompt +CONFIG_ARCH_HAS_CACHE_LINE_SIZE=y # no prompt +CONFIG_ARCH_HAS_CC_PLATFORM=y # no prompt +CONFIG_ARCH_HAS_CURRENT_STACK_POINTER=y # no prompt +CONFIG_ARCH_HAS_DEBUG_VIRTUAL=y # no prompt +CONFIG_ARCH_HAS_DEBUG_VM_PGTABLE=y # no prompt +CONFIG_ARCH_HAS_DEBUG_WX=y # no prompt +CONFIG_ARCH_HAS_DMA_PREP_COHERENT=y # no prompt +CONFIG_ARCH_HAS_ELF_RANDOMIZE=y # no prompt +CONFIG_ARCH_HAS_FAST_MULTIPLIER=y # no prompt +CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED=y # no prompt +CONFIG_ARCH_HAS_FORTIFY_SOURCE=y # no prompt +CONFIG_ARCH_HAS_GCOV_PROFILE_ALL=y # no prompt +CONFIG_ARCH_HAS_GIGANTIC_PAGE=y # no prompt +CONFIG_ARCH_HAS_HW_PTE_YOUNG=y # no prompt +CONFIG_ARCH_HAS_KCOV=y # no prompt +CONFIG_ARCH_HAS_KEEPINITRD=y # no prompt +CONFIG_ARCH_HAS_KERNEL_FPU_SUPPORT=y # no prompt +CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE=y # no prompt +CONFIG_ARCH_HAS_MEM_ENCRYPT=y # no prompt +CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS=y # no prompt +CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE=y # no prompt +CONFIG_ARCH_HAS_PREEMPT_LAZY=y # no prompt +CONFIG_ARCH_HAS_PTDUMP=y # no prompt +CONFIG_ARCH_HAS_PTE_SPECIAL=y # no prompt +CONFIG_ARCH_HAS_SETUP_DMA_OPS=y # no prompt +CONFIG_ARCH_HAS_SET_DIRECT_MAP=y # no prompt +CONFIG_ARCH_HAS_SET_MEMORY=y # no prompt +CONFIG_ARCH_HAS_STRICT_KERNEL_RWX=y # no prompt +CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y # no prompt +CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU=y # no prompt +CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE=y # no prompt +CONFIG_ARCH_HAS_SYSCALL_WRAPPER=y # no prompt +CONFIG_ARCH_HAS_TICK_BROADCAST=y # no prompt +CONFIG_ARCH_HAS_UBSAN=y # no prompt +CONFIG_ARCH_HAS_ZONE_DMA_SET=y # no prompt +CONFIG_ARCH_HAVE_ELF_PROT=y # no prompt +CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG=y # no prompt +CONFIG_ARCH_HAVE_TRACE_MMIO_ACCESS=y # no prompt +CONFIG_ARCH_KEEP_MEMBLOCK=y # no prompt +CONFIG_ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE=y # no prompt +CONFIG_ARCH_MMAP_RND_BITS=18 # default +CONFIG_ARCH_MMAP_RND_BITS_MAX=33 # no prompt +CONFIG_ARCH_MMAP_RND_BITS_MIN=18 # no prompt +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX=16 # no prompt +CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN=11 # no prompt +CONFIG_ARCH_PKEY_BITS=3 # no prompt +CONFIG_ARCH_PROC_KCORE_TEXT=y # no prompt +CONFIG_ARCH_SPARSEMEM_ENABLE=y # no prompt +CONFIG_ARCH_STACKWALK=y # no prompt +CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y # no prompt +CONFIG_ARCH_SUPPORTS_CFI=y # no prompt +CONFIG_ARCH_SUPPORTS_CRASH_DUMP=y # no prompt +CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC=y # no prompt +CONFIG_ARCH_SUPPORTS_HUGETLBFS=y # no prompt +CONFIG_ARCH_SUPPORTS_INT128=y # no prompt +CONFIG_ARCH_SUPPORTS_KEXEC_FILE=y # no prompt +CONFIG_ARCH_SUPPORTS_KEXEC_HANDOVER=y # no prompt +CONFIG_ARCH_SUPPORTS_KEXEC_IMAGE_VERIFY_SIG=y # no prompt +CONFIG_ARCH_SUPPORTS_KEXEC_SIG=y # no prompt +CONFIG_ARCH_SUPPORTS_LTO_CLANG=y # no prompt +CONFIG_ARCH_SUPPORTS_LTO_CLANG_THIN=y # no prompt +CONFIG_ARCH_SUPPORTS_MEMORY_FAILURE=y # no prompt +CONFIG_ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS=y # no prompt +CONFIG_ARCH_SUPPORTS_NUMA_BALANCING=y # no prompt +CONFIG_ARCH_SUPPORTS_PAGE_TABLE_CHECK=y # no prompt +CONFIG_ARCH_SUPPORTS_PER_VMA_LOCK=y # no prompt +CONFIG_ARCH_SUPPORTS_RT=y # no prompt +CONFIG_ARCH_SUPPORTS_SCHED_CLUSTER=y # no prompt +CONFIG_ARCH_SUPPORTS_SCHED_MC=y # no prompt +CONFIG_ARCH_SUPPORTS_SCHED_SMT=y # no prompt +CONFIG_ARCH_SUPPORTS_SHADOW_CALL_STACK=y # no prompt +CONFIG_ARCH_SUPPORTS_UPROBES=y # no prompt +CONFIG_ARCH_SUSPEND_POSSIBLE=y # no prompt +CONFIG_ARCH_USE_CMPXCHG_LOCKREF=y # no prompt +CONFIG_ARCH_USE_GNU_PROPERTY=y # no prompt +CONFIG_ARCH_USE_MEMTEST=y # no prompt +CONFIG_ARCH_USE_QUEUED_RWLOCKS=y # no prompt +CONFIG_ARCH_USE_QUEUED_SPINLOCKS=y # no prompt +CONFIG_ARCH_USE_SYM_ANNOTATIONS=y # no prompt +CONFIG_ARCH_WANTS_EXECMEM_LATE=y # no prompt +CONFIG_ARCH_WANTS_NO_INSTR=y # no prompt +CONFIG_ARCH_WANTS_THP_SWAP=y # no prompt +CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH=y # no prompt +CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y # no prompt +CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT=y # no prompt +CONFIG_ARCH_WANT_FRAME_POINTERS=y # no prompt +CONFIG_ARCH_WANT_HUGE_PMD_SHARE=y # no prompt +CONFIG_ARCH_WANT_LD_ORPHAN_WARN=y # no prompt +CONFIG_ARM64=y # no prompt +CONFIG_ARM64_AS_HAS_MTE=y # no prompt +CONFIG_ARM64_CONT_PMD_SHIFT=4 # no prompt +CONFIG_ARM64_CONT_PTE_SHIFT=4 # no prompt +CONFIG_ARM64_PA_BITS=48 # no prompt +CONFIG_ARM64_PA_BITS_48=y # choice default +CONFIG_ARM64_VA_BITS=48 # no prompt +CONFIG_ARM_AMBA=y # no prompt +CONFIG_ARM_ARCH_TIMER=y # no prompt +CONFIG_ARM_GIC=y # no prompt +CONFIG_ARM_GIC_ITS_PARENT=y # no prompt +CONFIG_ARM_GIC_MAX_NR=1 # no prompt +CONFIG_ARM_GIC_V3_ITS=y # no prompt +CONFIG_ARM_GIC_V5=y # no prompt +CONFIG_ARM_PSCI_FW=y # no prompt +CONFIG_AS_HAS_ARMV8_5=y # no prompt +CONFIG_AS_HAS_CFI_NEGATE_RA_STATE=y # no prompt +CONFIG_AS_HAS_MOPS=y # no prompt +CONFIG_AS_HAS_NON_CONST_ULEB128=y # no prompt +CONFIG_AS_IS_GNU=y # no prompt +CONFIG_AS_VERSION=24000 # no prompt +CONFIG_AUDIT_ARCH_COMPAT_GENERIC=y # no prompt +CONFIG_BCMA_POSSIBLE=y # no prompt +CONFIG_BINARY_PRINTF=y # no prompt +CONFIG_BITREVERSE=y # no prompt +CONFIG_BPF=y # no prompt +CONFIG_BUFFER_HEAD=y # no prompt +CONFIG_BUILDTIME_TABLE_SORT=y # no prompt +CONFIG_BUILD_SALT="" # default +CONFIG_BUILTIN_RETURN_ADDRESS_STRIPS_PAC=y # no prompt +CONFIG_CC_CAN_LINK=y # no prompt +CONFIG_CC_HAS_ASM_INLINE=y # no prompt +CONFIG_CC_HAS_AUTO_VAR_INIT_PATTERN=y # no prompt +CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO=y # no prompt +CONFIG_CC_HAS_AUTO_VAR_INIT_ZERO_BARE=y # no prompt +CONFIG_CC_HAS_BRANCH_PROT_PAC_RET=y # no prompt +CONFIG_CC_HAS_BRANCH_PROT_PAC_RET_BTI=y # no prompt +CONFIG_CC_HAS_INT128=y # no prompt +CONFIG_CC_HAS_KASAN_GENERIC=y # no prompt +CONFIG_CC_HAS_KASAN_SW_TAGS=y # no prompt +CONFIG_CC_HAS_NO_PROFILE_FN_ATTR=y # no prompt +CONFIG_CC_HAS_WORKING_NOSANITIZE_ADDRESS=y # no prompt +CONFIG_CC_HAS_ZERO_CALL_USED_REGS=y # no prompt +CONFIG_CC_HAVE_SHADOW_CALL_STACK=y # no prompt +CONFIG_CC_HAVE_STACKPROTECTOR_SYSREG=y # no prompt +CONFIG_CC_IMPLICIT_FALLTHROUGH="-Wimplicit-fallthrough=5" # no prompt +CONFIG_CC_IS_GCC=y # no prompt +CONFIG_CC_NO_ARRAY_BOUNDS=y # no prompt +CONFIG_CC_NO_STRINGOP_OVERFLOW=y # no prompt +CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y # choice default +CONFIG_CC_VERSION_TEXT="gcc (Debian 12.2.0-14+deb12u1) 12.2.0" # no prompt +CONFIG_CGROUP_WRITEBACK=y # no prompt +CONFIG_CLANG_VERSION=0 # no prompt +CONFIG_CLONE_BACKWARDS=y # no prompt +CONFIG_CMDLINE="" # default +CONFIG_COMMON_CLK=y # selected by ARM64 +CONFIG_COMPACT_UNEVICTABLE_DEFAULT=1 # no prompt +CONFIG_CONSOLE_LOGLEVEL_DEFAULT=7 # default +CONFIG_CONSOLE_LOGLEVEL_QUIET=4 # default +CONFIG_CONTEXT_TRACKING=y # no prompt +CONFIG_CONTEXT_TRACKING_IDLE=y # no prompt +CONFIG_CPU_LITTLE_ENDIAN=y # choice default +CONFIG_CPU_MITIGATIONS=y # no prompt +CONFIG_CRC16=y # no prompt +CONFIG_CRC32=y # no prompt +CONFIG_CRC32_ARCH=y # no prompt +CONFIG_CRYPTO_LIB_POLY1305_RSIZE=9 # no prompt +CONFIG_CRYPTO_LIB_SHA1=y # no prompt +CONFIG_CRYPTO_LIB_SHA1_ARCH=y # no prompt +CONFIG_CRYPTO_LIB_SHA256=y # no prompt +CONFIG_CRYPTO_LIB_SHA256_ARCH=y # no prompt +CONFIG_DCACHE_WORD_ACCESS=y # no prompt +CONFIG_DEBUG_FS_ALLOW_ALL=y # choice default +CONFIG_DEBUG_INFO_NONE=y # choice default +CONFIG_DEBUG_KERNEL=y # selected by EXPERT +CONFIG_DECOMPRESS_GZIP=y # no prompt +CONFIG_DEFAULT_HOSTNAME="(none)" # default +CONFIG_DEFAULT_INIT="" # default +CONFIG_DEFAULT_MMAP_MIN_ADDR=4096 # default +CONFIG_DEFAULT_SECURITY_DAC=y # choice default +CONFIG_DEFAULT_TCP_CONG="cubic" # no prompt +CONFIG_DIMLIB=y # no prompt +CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC=y # no prompt +CONFIG_DMA_COHERENT_POOL=y # no prompt +CONFIG_DMA_DECLARE_COHERENT=y # no prompt +CONFIG_DMA_DIRECT_REMAP=y # no prompt +CONFIG_DMA_NEED_SYNC=y # no prompt +CONFIG_DMA_NONCOHERENT_MMAP=y # no prompt +CONFIG_DQL=y # no prompt +CONFIG_DTC=y # no prompt +CONFIG_EDAC_SUPPORT=y # no prompt +CONFIG_ELFCORE=y # no prompt +CONFIG_EXCLUSIVE_SYSTEM_RAM=y # no prompt +CONFIG_EXECMEM=y # no prompt +CONFIG_EXPORTFS=y # no prompt +CONFIG_FAILOVER=y # selected by NET_FAILOVER +CONFIG_FIX_EARLYCON_MEM=y # no prompt +CONFIG_FRAME_POINTER=y # selected by ARM64 +CONFIG_FRAME_WARN=2048 # default +CONFIG_FSNOTIFY=y # no prompt +CONFIG_FS_IOMAP=y # no prompt +CONFIG_FS_MBCACHE=y # no prompt +CONFIG_FS_POSIX_ACL=y # no prompt +CONFIG_FS_STACK=y # no prompt +CONFIG_FUNCTION_ALIGNMENT=4 # no prompt +CONFIG_FUNCTION_ALIGNMENT_4B=y # no prompt +CONFIG_FUTEX_PI=y # no prompt +CONFIG_FUTEX_PRIVATE_HASH=y # no prompt +CONFIG_GCC10_NO_ARRAY_BOUNDS=y # no prompt +CONFIG_GCC_ASM_GOTO_OUTPUT_BROKEN=y # no prompt +CONFIG_GCC_NO_STRINGOP_OVERFLOW=y # no prompt +CONFIG_GCC_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS=y # no prompt +CONFIG_GCC_VERSION=120200 # no prompt +CONFIG_GENERIC_ALLOCATOR=y # no prompt +CONFIG_GENERIC_ARCH_TOPOLOGY=y # no prompt +CONFIG_GENERIC_BUG=y # no prompt +CONFIG_GENERIC_BUG_RELATIVE_POINTERS=y # no prompt +CONFIG_GENERIC_CALIBRATE_DELAY=y # no prompt +CONFIG_GENERIC_CLOCKEVENTS=y # no prompt +CONFIG_GENERIC_CLOCKEVENTS_BROADCAST=y # no prompt +CONFIG_GENERIC_CPU_AUTOPROBE=y # no prompt +CONFIG_GENERIC_CPU_DEVICES=y # no prompt +CONFIG_GENERIC_CPU_VULNERABILITIES=y # no prompt +CONFIG_GENERIC_CSUM=y # no prompt +CONFIG_GENERIC_EARLY_IOREMAP=y # no prompt +CONFIG_GENERIC_GETTIMEOFDAY=y # no prompt +CONFIG_GENERIC_HWEIGHT=y # no prompt +CONFIG_GENERIC_IDLE_POLL_SETUP=y # no prompt +CONFIG_GENERIC_IOREMAP=y # no prompt +CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK=y # no prompt +CONFIG_GENERIC_IRQ_ENTRY=y # no prompt +CONFIG_GENERIC_IRQ_IPI=y # no prompt +CONFIG_GENERIC_IRQ_KEXEC_CLEAR_VM_FORWARD=y # no prompt +CONFIG_GENERIC_IRQ_PROBE=y # no prompt +CONFIG_GENERIC_IRQ_SHOW=y # no prompt +CONFIG_GENERIC_IRQ_SHOW_LEVEL=y # no prompt +CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED=y # no prompt +CONFIG_GENERIC_MSI_IRQ=y # no prompt +CONFIG_GENERIC_NET_UTILS=y # no prompt +CONFIG_GENERIC_PCI_IOMAP=y # no prompt +CONFIG_GENERIC_SCHED_CLOCK=y # no prompt +CONFIG_GENERIC_SMP_IDLE_THREAD=y # no prompt +CONFIG_GENERIC_STRNCPY_FROM_USER=y # no prompt +CONFIG_GENERIC_STRNLEN_USER=y # no prompt +CONFIG_GENERIC_TIME_VSYSCALL=y # no prompt +CONFIG_GPIOLIB_LEGACY=y # no prompt +CONFIG_HARDIRQS_SW_RESEND=y # no prompt +CONFIG_HAS_DMA=y # no prompt +CONFIG_HAS_IOMEM=y # no prompt +CONFIG_HAS_IOPORT=y # no prompt +CONFIG_HAVE_ALIGNED_STRUCT_PAGE=y # no prompt +CONFIG_HAVE_ARCH_AUDITSYSCALL=y # no prompt +CONFIG_HAVE_ARCH_BITREVERSE=y # no prompt +CONFIG_HAVE_ARCH_COMPILER_H=y # no prompt +CONFIG_HAVE_ARCH_HUGE_VMALLOC=y # no prompt +CONFIG_HAVE_ARCH_HUGE_VMAP=y # no prompt +CONFIG_HAVE_ARCH_JUMP_LABEL=y # no prompt +CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y # no prompt +CONFIG_HAVE_ARCH_KASAN=y # no prompt +CONFIG_HAVE_ARCH_KASAN_SW_TAGS=y # no prompt +CONFIG_HAVE_ARCH_KASAN_VMALLOC=y # no prompt +CONFIG_HAVE_ARCH_KCSAN=y # no prompt +CONFIG_HAVE_ARCH_KFENCE=y # no prompt +CONFIG_HAVE_ARCH_KGDB=y # no prompt +CONFIG_HAVE_ARCH_KSTACK_ERASE=y # no prompt +CONFIG_HAVE_ARCH_MMAP_RND_BITS=y # no prompt +CONFIG_HAVE_ARCH_PREL32_RELOCATIONS=y # no prompt +CONFIG_HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET=y # no prompt +CONFIG_HAVE_ARCH_SECCOMP=y # no prompt +CONFIG_HAVE_ARCH_SECCOMP_FILTER=y # no prompt +CONFIG_HAVE_ARCH_THREAD_STRUCT_WHITELIST=y # no prompt +CONFIG_HAVE_ARCH_TRACEHOOK=y # no prompt +CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE=y # no prompt +CONFIG_HAVE_ARCH_VMAP_STACK=y # no prompt +CONFIG_HAVE_ARM_SMCCC=y # no prompt +CONFIG_HAVE_ARM_SMCCC_DISCOVERY=y # no prompt +CONFIG_HAVE_ASM_MODVERSIONS=y # no prompt +CONFIG_HAVE_BUILDTIME_MCOUNT_SORT=y # no prompt +CONFIG_HAVE_CLK=y # no prompt +CONFIG_HAVE_CLK_PREPARE=y # no prompt +CONFIG_HAVE_CMPXCHG_DOUBLE=y # no prompt +CONFIG_HAVE_CMPXCHG_LOCAL=y # no prompt +CONFIG_HAVE_CONTEXT_TRACKING_USER=y # no prompt +CONFIG_HAVE_C_RECORDMCOUNT=y # no prompt +CONFIG_HAVE_DEBUG_KMEMLEAK=y # no prompt +CONFIG_HAVE_DMA_CONTIGUOUS=y # no prompt +CONFIG_HAVE_DYNAMIC_FTRACE=y # no prompt +CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS=y # no prompt +CONFIG_HAVE_EBPF_JIT=y # no prompt +CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS=y # no prompt +CONFIG_HAVE_EXTRA_IPI_TRACEPOINTS=y # no prompt +CONFIG_HAVE_FTRACE_GRAPH_FUNC=y # no prompt +CONFIG_HAVE_FUNCTION_ARG_ACCESS_API=y # no prompt +CONFIG_HAVE_FUNCTION_ERROR_INJECTION=y # no prompt +CONFIG_HAVE_FUNCTION_GRAPH_FREGS=y # no prompt +CONFIG_HAVE_FUNCTION_GRAPH_TRACER=y # no prompt +CONFIG_HAVE_FUNCTION_TRACER=y # no prompt +CONFIG_HAVE_GCC_PLUGINS=y # no prompt +CONFIG_HAVE_GENERIC_VDSO=y # no prompt +CONFIG_HAVE_GUP_FAST=y # no prompt +CONFIG_HAVE_HARDLOCKUP_DETECTOR_BUDDY=y # no prompt +CONFIG_HAVE_IOREMAP_PROT=y # no prompt +CONFIG_HAVE_IRQ_TIME_ACCOUNTING=y # no prompt +CONFIG_HAVE_KCSAN_COMPILER=y # no prompt +CONFIG_HAVE_KPROBES=y # no prompt +CONFIG_HAVE_KRETPROBES=y # no prompt +CONFIG_HAVE_LIVEPATCH=y # no prompt +CONFIG_HAVE_MOD_ARCH_SPECIFIC=y # no prompt +CONFIG_HAVE_MOVE_PMD=y # no prompt +CONFIG_HAVE_MOVE_PUD=y # no prompt +CONFIG_HAVE_NMI=y # no prompt +CONFIG_HAVE_PAGE_SIZE_4KB=y # no prompt +CONFIG_HAVE_PCI=y # no prompt +CONFIG_HAVE_PERF_EVENTS=y # no prompt +CONFIG_HAVE_PERF_REGS=y # no prompt +CONFIG_HAVE_PERF_USER_STACK_DUMP=y # no prompt +CONFIG_HAVE_POSIX_CPU_TIMERS_TASK_WORK=y # no prompt +CONFIG_HAVE_PREEMPT_DYNAMIC=y # no prompt +CONFIG_HAVE_PREEMPT_DYNAMIC_KEY=y # no prompt +CONFIG_HAVE_REGS_AND_STACK_ACCESS_API=y # no prompt +CONFIG_HAVE_RELIABLE_STACKTRACE=y # no prompt +CONFIG_HAVE_RSEQ=y # no prompt +CONFIG_HAVE_RUST=y # no prompt +CONFIG_HAVE_SAMPLE_FTRACE_DIRECT=y # no prompt +CONFIG_HAVE_SAMPLE_FTRACE_DIRECT_MULTI=y # no prompt +CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK=y # no prompt +CONFIG_HAVE_STACKPROTECTOR=y # no prompt +CONFIG_HAVE_SYSCALL_TRACEPOINTS=y # no prompt +CONFIG_HAVE_VIRT_CPU_ACCOUNTING_GEN=y # no prompt +CONFIG_HVC_DRIVER=y # no prompt +CONFIG_HZ=100 # no prompt +CONFIG_ILLEGAL_POINTER_VALUE=0xdead000000000000 # no prompt +CONFIG_INET_TABLE_PERTURB_ORDER=16 # default +CONFIG_INET_TCP_DIAG=y # no prompt +CONFIG_INITRAMFS_SOURCE="" # default +CONFIG_INIT_ENV_ARG_LIMIT=32 # no prompt +CONFIG_IO_URING_ZCRX=y # no prompt +CONFIG_IO_WQ=y # no prompt +CONFIG_IRQCHIP=y # no prompt +CONFIG_IRQ_DOMAIN=y # no prompt +CONFIG_IRQ_DOMAIN_HIERARCHY=y # no prompt +CONFIG_IRQ_FORCED_THREADING=y # no prompt +CONFIG_IRQ_MSI_IOMMU=y # no prompt +CONFIG_IRQ_MSI_LIB=y # no prompt +CONFIG_IRQ_WORK=y # no prompt +CONFIG_JBD2=y # no prompt +CONFIG_KERNEL_MODE_NEON=y # no prompt +CONFIG_KERNFS=y # no prompt +CONFIG_KVFREE_RCU_BATCHED=y # no prompt +CONFIG_LD_CAN_USE_KEEP_IN_OVERLAY=y # no prompt +CONFIG_LD_IS_BFD=y # no prompt +CONFIG_LD_ORPHAN_WARN=y # no prompt +CONFIG_LD_ORPHAN_WARN_LEVEL="error" # no prompt +CONFIG_LD_VERSION=24000 # no prompt +CONFIG_LIBFDT=y # no prompt +CONFIG_LLD_VERSION=0 # no prompt +CONFIG_LOCALVERSION="" # default +CONFIG_LOCKDEP_SUPPORT=y # no prompt +CONFIG_LOCK_DEBUGGING_SUPPORT=y # no prompt +CONFIG_LOCK_MM_AND_FIND_VMA=y # no prompt +CONFIG_LOCK_SPIN_ON_OWNER=y # no prompt +CONFIG_LOG_CPU_MAX_BUF_SHIFT=12 # default +CONFIG_LTO_NONE=y # choice default +CONFIG_LZ4_DECOMPRESS=y # no prompt +CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1 # default +CONFIG_MAX_SKB_FRAGS=17 # default +CONFIG_MEMFD_CREATE=y # selected by TMPFS +CONFIG_MESSAGE_LOGLEVEL_DEFAULT=4 # default +CONFIG_MIGRATION=y # selected by COMPACTION +CONFIG_MMU=y # no prompt +CONFIG_MMU_GATHER_RCU_TABLE_FREE=y # no prompt +CONFIG_MMU_GATHER_TABLE_FREE=y # no prompt +CONFIG_MMU_LAZY_TLB_REFCOUNT=y # no prompt +CONFIG_MODULES_USE_ELF_RELA=y # no prompt +CONFIG_MUTEX_SPIN_ON_OWNER=y # no prompt +CONFIG_NEED_DMA_MAP_STATE=y # no prompt +CONFIG_NEED_SG_DMA_LENGTH=y # no prompt +CONFIG_NEED_TASKS_RCU=y # no prompt +CONFIG_NET_EGRESS=y # no prompt +CONFIG_NET_FAILOVER=y # selected by VIRTIO_NET +CONFIG_NET_INGRESS=y # no prompt +CONFIG_NET_RX_BUSY_POLL=y # no prompt +CONFIG_NET_SOCK_MSG=y # no prompt +CONFIG_NET_XGRESS=y # no prompt +CONFIG_NLATTR=y # no prompt +CONFIG_NO_HZ_COMMON=y # no prompt +CONFIG_NO_IOPORT_MAP=y # no prompt +CONFIG_OF=y # selected by ARM64 +CONFIG_OF_ADDRESS=y # no prompt +CONFIG_OF_EARLY_FLATTREE=y # no prompt +CONFIG_OF_FLATTREE=y # no prompt +CONFIG_OF_IRQ=y # no prompt +CONFIG_OF_KOBJ=y # no prompt +CONFIG_PAGE_BLOCK_MAX_ORDER=10 # default +CONFIG_PAGE_COUNTER=y # no prompt +CONFIG_PAGE_MAPCOUNT=y # no prompt +CONFIG_PAGE_POOL=y # no prompt +CONFIG_PAGE_SHIFT=12 # no prompt +CONFIG_PAGE_SIZE_4KB=y # choice default +CONFIG_PAGE_SIZE_LESS_THAN_256KB=y # no prompt +CONFIG_PAGE_SIZE_LESS_THAN_64KB=y # no prompt +CONFIG_PAHOLE_VERSION=0 # no prompt +CONFIG_PANIC_TIMEOUT=0 # default +CONFIG_PARTITION_PERCPU=y # no prompt +CONFIG_PCP_BATCH_SCALE_MAX=5 # default +CONFIG_PER_VMA_LOCK=y # no prompt +CONFIG_PGTABLE_LEVELS=4 # no prompt +CONFIG_PHYS_ADDR_T_64BIT=y # no prompt +CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y # no prompt +CONFIG_POWER_RESET=y # selected by ARM64 +CONFIG_POWER_SUPPLY=y # selected by ARM64 +CONFIG_PREEMPTION=y # no prompt +CONFIG_PREEMPT_BUILD=y # no prompt +CONFIG_PREEMPT_COUNT=y # no prompt +CONFIG_PREEMPT_RCU=y # no prompt +CONFIG_PROC_MEM_ALWAYS_FORCE=y # choice default +CONFIG_PTDUMP=y # no prompt +CONFIG_PTP_1588_CLOCK_OPTIONAL=y # no prompt +CONFIG_QUEUED_RWLOCKS=y # no prompt +CONFIG_QUEUED_SPINLOCKS=y # no prompt +CONFIG_RANDSTRUCT_NONE=y # choice default +CONFIG_RATIONAL=y # no prompt +CONFIG_RCU_NEED_SEGCBLIST=y # no prompt +CONFIG_RCU_STALL_COMMON=y # no prompt +CONFIG_RT_MUTEXES=y # no prompt +CONFIG_RUSTC_LLVM_VERSION=0 # no prompt +CONFIG_RUSTC_SUPPORTS_ARM64=y # no prompt +CONFIG_RUSTC_VERSION=0 # no prompt +CONFIG_RWSEM_SPIN_ON_OWNER=y # no prompt +CONFIG_SBITMAP=y # no prompt +CONFIG_SCHED_HRTICK=y # no prompt +CONFIG_SCHED_MM_CID=y # no prompt +CONFIG_SCSI_MOD=y # no prompt +CONFIG_SECCOMP_FILTER=y # no prompt +CONFIG_SERIAL_CORE=y # no prompt +CONFIG_SERIAL_CORE_CONSOLE=y # no prompt +CONFIG_SERIAL_EARLYCON=y # no prompt +CONFIG_SG_POOL=y # no prompt +CONFIG_SLAB_OBJ_EXT=y # no prompt +CONFIG_SLUB=y # no prompt +CONFIG_SOCK_CGROUP_DATA=y # no prompt +CONFIG_SOCK_RX_QUEUE_MAPPING=y # no prompt +CONFIG_SOFTIRQ_ON_OWN_STACK=y # no prompt +CONFIG_SPARSEMEM=y # no prompt +CONFIG_SPARSEMEM_EXTREME=y # no prompt +CONFIG_SPARSEMEM_VMEMMAP=y # no prompt +CONFIG_SPARSEMEM_VMEMMAP_ENABLE=y # no prompt +CONFIG_SPARSE_IRQ=y # selected by ARM64 +CONFIG_SPLIT_PMD_PTLOCKS=y # no prompt +CONFIG_SPLIT_PTE_PTLOCKS=y # no prompt +CONFIG_SSB_POSSIBLE=y # no prompt +CONFIG_STACKPROTECTOR_PER_TASK=y # no prompt +CONFIG_STACKTRACE_SUPPORT=y # no prompt +CONFIG_STRICT_KERNEL_RWX=y # default, prompt hidden by ARCH_OPTIONAL_KERNEL_RWX +CONFIG_STRICT_MODULE_RWX=y # default, prompt hidden by ARCH_OPTIONAL_KERNEL_RWX +CONFIG_SWIOTLB=y # no prompt +CONFIG_SYSCTL=y # no prompt +CONFIG_SYSCTL_EXCEPTION_TRACE=y # no prompt +CONFIG_SYSVIPC_SYSCTL=y # no prompt +CONFIG_TASKS_RCU=y # no prompt +CONFIG_TASKS_RCU_GENERIC=y # no prompt +CONFIG_TASKS_TRACE_RCU=y # no prompt +CONFIG_TCP_CONG_CUBIC=y # default, prompt hidden by NET && INET && TCP_CONG_ADVANCED +CONFIG_THREAD_INFO_IN_TASK=y # no prompt +CONFIG_TICK_CPU_ACCOUNTING=y # choice default +CONFIG_TICK_ONESHOT=y # no prompt +CONFIG_TIMER_OF=y # no prompt +CONFIG_TIMER_PROBE=y # no prompt +CONFIG_TRACE_IRQFLAGS_NMI_SUPPORT=y # no prompt +CONFIG_TRACE_IRQFLAGS_SUPPORT=y # no prompt +CONFIG_TRACING_SUPPORT=y # no prompt +CONFIG_TREE_RCU=y # no prompt +CONFIG_TREE_SRCU=y # no prompt +CONFIG_UNINLINE_SPIN_UNLOCK=y # no prompt +CONFIG_UNUSED_KSYMS_WHITELIST="" # default +CONFIG_USB_OHCI_LITTLE_ENDIAN=y # no prompt +CONFIG_USER_STACKTRACE_SUPPORT=y # no prompt +CONFIG_VDSO_GETRANDOM=y # no prompt +CONFIG_VIRTIO=y # no prompt +CONFIG_VIRTIO_ANCHOR=y # no prompt +CONFIG_VM_EVENT_COUNTERS=y # selected by MEMCG +CONFIG_XPS=y # no prompt +CONFIG_XXHASH=y # no prompt +CONFIG_ZLIB_INFLATE=y # no prompt +CONFIG_ZSTD_COMMON=y # no prompt +CONFIG_ZSTD_DECOMPRESS=y # no prompt diff --git a/kernel/patches/arm64/0001-nvx-platform-mmio.patch b/kernel/patches/arm64/0001-nvx-platform-mmio.patch new file mode 100644 index 000000000..eb4f7f2ad --- /dev/null +++ b/kernel/patches/arm64/0001-nvx-platform-mmio.patch @@ -0,0 +1,291 @@ +From: NVX maintainers +Subject: [PATCH] hvc: add the restricted NVX ARM64 development platform binding + +--- a/drivers/tty/hvc/Kconfig ++++ b/drivers/tty/hvc/Kconfig +@@ -67,6 +67,19 @@ + default y + help + Xen driver for secondary virtual consoles ++ ++config HVC_NVX_MMIO ++ bool "NVX ARM64 MMIO platform console and restricted control device" ++ depends on ARM64 && OF ++ select HVC_DRIVER ++ select SERIAL_CORE ++ select SERIAL_CORE_CONSOLE ++ select SERIAL_EARLYCON ++ help ++ Use the fixed NVX development platform page for early output and ++ hvc0, and expose its exact registers through /dev/nvx-platform. ++ The page is root-only and mappings require CAP_SYS_RAWIO. ++ This is not a generic UART, physical-memory interface or snapshot API. + + config HVC_XE9 + bool "microVM portb (0xE9/0xEA) hypervisor console" +--- a/drivers/tty/hvc/Makefile ++++ b/drivers/tty/hvc/Makefile +@@ -8,6 +8,7 @@ + obj-$(CONFIG_HVC_IRQ) += hvc_irq.o + obj-$(CONFIG_HVC_XEN) += hvc_xen.o + obj-$(CONFIG_HVC_XE9) += hvc_xe9.o ++obj-$(CONFIG_HVC_NVX_MMIO) += hvc_nvx_mmio.o + obj-$(CONFIG_HVC_IUCV) += hvc_iucv.o + obj-$(CONFIG_HVC_UDBG) += hvc_udbg.o + obj-$(CONFIG_HVC_RISCV_SBI) += hvc_riscv_sbi.o +--- a/drivers/tty/hvc/hvc_nvx_mmio.c ++++ b/drivers/tty/hvc/hvc_nvx_mmio.c +@@ -0,0 +1,253 @@ ++// SPDX-License-Identifier: GPL-2.0 ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "hvc_console.h" ++ ++#define NVX_BASE 0xd1000000 ++#define NVX_SIZE 0x1000 ++#define NVX_DATA 0x00 ++#define NVX_STATUS 0x04 ++#define NVX_TIME 0x08 ++#define NVX_SHUTDOWN 0x0c ++#define NVX_SNAPSHOT 0x10 ++ ++/* This built-in fixed device cannot unbind or outlive the guest machine. */ ++static void __iomem *nvx_base; ++ ++static ssize_t nvx_put(u32 vtermno, const u8 *buffer, size_t count) ++{ ++ size_t i; ++ ++ if (!nvx_base) ++ return -ENODEV; ++ for (i = 0; i < count; i++) ++ writeb(buffer[i], nvx_base + NVX_DATA); ++ return count; ++} ++ ++static ssize_t nvx_get(u32 vtermno, u8 *buffer, size_t count) ++{ ++ size_t i; ++ ++ if (!nvx_base) ++ return -ENODEV; ++ for (i = 0; i < count; i++) { ++ if (!(readb(nvx_base + NVX_STATUS) & 1)) ++ break; ++ buffer[i] = readb(nvx_base + NVX_DATA); ++ } ++ return i; ++} ++ ++static const struct hv_ops nvx_ops = { ++ .get_chars = nvx_get, ++ .put_chars = nvx_put, ++}; ++ ++static void nvx_early_write(struct console *console, const char *text, ++ unsigned int count) ++{ ++ struct earlycon_device *device = console->data; ++ unsigned int i; ++ ++ for (i = 0; i < count; i++) ++ writeb(text[i], device->port.membase + NVX_DATA); ++} ++ ++static int __init nvx_early_setup(struct earlycon_device *device, ++ const char *options) ++{ ++ if (device->port.mapbase != NVX_BASE || !device->port.membase) ++ return -EINVAL; ++ device->con->write = nvx_early_write; ++ return 0; ++} ++ ++/* EARLYCON_DECLARE stringifies a C identifier, which cannot contain this hyphen. */ ++static const struct earlycon_id nvx_earlycon ++ EARLYCON_USED_OR_UNUSED __section("__earlycon_table") ++ __aligned(__alignof__(struct earlycon_id)) = { ++ .name = "nvx-mmio", ++ .compatible = "microsoft,nvx-platform-control-v1", ++ .setup = nvx_early_setup, ++}; ++ ++static int __init nvx_console_init(void) ++{ ++ struct device_node *node; ++ struct resource resource; ++ int error; ++ ++ node = of_find_compatible_node(NULL, NULL, ++ "microsoft,nvx-platform-control-v1"); ++ if (!node) ++ return -ENODEV; ++ if (!of_device_is_available(node)) { ++ of_node_put(node); ++ return -ENODEV; ++ } ++ error = of_address_to_resource(node, 0, &resource); ++ of_node_put(node); ++ if (error) ++ return error; ++ if (resource.start != NVX_BASE || resource_size(&resource) != NVX_SIZE) ++ return -EINVAL; ++ nvx_base = ioremap(NVX_BASE, NVX_SIZE); ++ if (!nvx_base) ++ return -ENOMEM; ++ error = hvc_instantiate(0, 0, &nvx_ops); ++ if (error) { ++ iounmap(nvx_base); ++ nvx_base = NULL; ++ return error; ++ } ++ if (!console_set_on_cmdline) ++ add_preferred_console("hvc", 0, NULL); ++ return 0; ++} ++console_initcall(nvx_console_init); ++ ++static int nvx_open(struct inode *inode, struct file *file) ++{ ++ return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; ++} ++ ++static int nvx_access(loff_t offset, size_t count, bool writing) ++{ ++ if (count != 1 && count != 2 && count != 4) ++ return -EINVAL; ++ switch (offset) { ++ case NVX_DATA: ++ case NVX_SHUTDOWN: ++ return !writing || count == 1 ? 0 : -EINVAL; ++ case NVX_STATUS: ++ return count == 1 ? 0 : -EINVAL; ++ case NVX_TIME: ++ return writing ? -EINVAL : 0; ++ case NVX_SNAPSHOT: ++ return writing ? -EOPNOTSUPP : 0; ++ default: ++ return -EINVAL; ++ } ++} ++ ++static ssize_t nvx_read(struct file *file, char __user *buffer, size_t count, ++ loff_t *position) ++{ ++ u32 value; ++ int error = nvx_access(*position, count, false); ++ ++ if (error) ++ return error; ++ switch (count) { ++ case 1: value = readb(nvx_base + *position); break; ++ case 2: value = readw(nvx_base + *position); break; ++ default: value = readl(nvx_base + *position); break; ++ } ++ value = cpu_to_le32(value); ++ if (copy_to_user(buffer, &value, count)) ++ return -EFAULT; ++ *position += count; ++ return count; ++} ++ ++static ssize_t nvx_write(struct file *file, const char __user *buffer, ++ size_t count, loff_t *position) ++{ ++ u8 value; ++ int error = nvx_access(*position, count, true); ++ ++ if (error) ++ return error; ++ if (copy_from_user(&value, buffer, 1)) ++ return -EFAULT; ++ writeb(value, nvx_base + *position); ++ mb(); ++ *position += count; ++ return count; ++} ++ ++static int nvx_mmap(struct file *file, struct vm_area_struct *vma) ++{ ++ unsigned long flags = VM_SHARED | VM_READ | VM_WRITE; ++ ++ if (!capable(CAP_SYS_RAWIO)) ++ return -EPERM; ++ if (PAGE_SIZE != NVX_SIZE || vma->vm_pgoff || ++ vma->vm_end - vma->vm_start != NVX_SIZE || ++ (vma->vm_flags & flags) != flags || (vma->vm_flags & VM_EXEC)) ++ return -EINVAL; ++ vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP | VM_DONTCOPY); ++ vm_flags_clear(vma, VM_MAYEXEC); ++ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); ++ return remap_pfn_range(vma, vma->vm_start, NVX_BASE >> PAGE_SHIFT, ++ NVX_SIZE, vma->vm_page_prot); ++} ++ ++static const struct file_operations nvx_fops = { ++ .open = nvx_open, ++ .read = nvx_read, ++ .write = nvx_write, ++ .mmap = nvx_mmap, ++ .llseek = default_llseek, ++}; ++ ++static struct miscdevice nvx_device = { ++ .minor = MISC_DYNAMIC_MINOR, ++ .name = "nvx-platform", ++ .fops = &nvx_fops, ++ .mode = 0600, ++}; ++ ++static int __init nvx_probe(struct platform_device *pdev) ++{ ++ struct resource *resource; ++ struct hvc_struct *console; ++ int error; ++ ++ resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); ++ if (!nvx_base || !resource || resource->start != NVX_BASE || ++ resource_size(resource) != NVX_SIZE) ++ return -EINVAL; ++ if (!devm_request_mem_region(&pdev->dev, NVX_BASE, NVX_SIZE, ++ "nvx-platform")) ++ return -EBUSY; ++ console = hvc_alloc(0, 0, &nvx_ops, 128); ++ if (IS_ERR(console)) ++ return PTR_ERR(console); ++ nvx_device.parent = &pdev->dev; ++ error = misc_register(&nvx_device); ++ if (error) ++ hvc_remove(console); ++ return error; ++} ++ ++static const struct of_device_id nvx_match[] = { ++ { .compatible = "microsoft,nvx-platform-control-v1" }, ++ { } ++}; ++ ++static struct platform_driver nvx_driver = { ++ .driver = { ++ .name = "nvx-platform", ++ .of_match_table = nvx_match, ++ .suppress_bind_attrs = true, ++ }, ++}; ++ ++static int __init nvx_init(void) ++{ ++ return platform_driver_probe(&nvx_driver, nvx_probe); ++} ++device_initcall(nvx_init); diff --git a/kernel/patches/arm64/0002-nvx-shared-status-of.patch b/kernel/patches/arm64/0002-nvx-shared-status-of.patch new file mode 100644 index 000000000..c5eada7b4 --- /dev/null +++ b/kernel/patches/arm64/0002-nvx-shared-status-of.patch @@ -0,0 +1,92 @@ +From: NVX maintainers +Subject: [PATCH] virtio: retain shared status atomics with ARM OF reserved-memory discovery + +--- a/drivers/virtio/Kconfig ++++ b/drivers/virtio/Kconfig +@@ -173,10 +173,12 @@ + + config VIRTIO_MMIO_SHARED_STATUS + bool "OpenVMM microVM shared virtio-mmio interrupt status" +- depends on VIRTIO_MMIO && VIRTIO_MMIO_CMDLINE_DEVICES && X86 ++ depends on VIRTIO_MMIO ++ depends on (X86 && VIRTIO_MMIO_CMDLINE_DEVICES) || (ARM64 && OF_RESERVED_MEM) + help + Use the interrupt-status page defined by the OpenVMM microVM ABI. +- The page is discovered through the Xen PVH E820 map and has one atomic ++ The page is discovered through E820 on x86 or the exact normal-memory ++ DT reservation on ARM64 and has one atomic + status word for every fixed virtio-mmio transport slot. Interrupt handlers + exchange their word to zero instead of reading and acknowledging the + virtio-mmio interrupt registers. +--- a/drivers/virtio/virtio_mmio.c ++++ b/drivers/virtio/virtio_mmio.c +@@ -71,7 +71,12 @@ + #include + + #ifdef CONFIG_VIRTIO_MMIO_SHARED_STATUS ++#ifdef CONFIG_X86 + #include ++#else ++#include ++#include ++#endif + #endif + + +@@ -115,8 +120,14 @@ + phys_addr_t status_gpa; + u64 offset; + +- if (!microvm_shared_status_available) ++ if (!microvm_shared_status_available) { ++#ifdef CONFIG_ARM64 ++ dev_err(&pdev->dev, "NVX requires the exact WB shared-status reservation\n"); ++ return -EINVAL; ++#else + return 0; ++#endif ++ } + + resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!resource) +@@ -899,13 +910,40 @@ + }, + }; + ++#if defined(CONFIG_VIRTIO_MMIO_SHARED_STATUS) && defined(CONFIG_ARM64) ++static bool __init microvm_shared_status_from_of(void) ++{ ++ struct device_node *root, *child; ++ unsigned int matches = 0; ++ ++ root = of_find_node_by_path("/reserved-memory"); ++ if (!root) ++ return false; ++ for_each_available_child_of_node(root, child) { ++ struct reserved_mem *reserved = of_reserved_mem_lookup(child); ++ ++ if (reserved && ++ reserved->base == MICROVM_SHARED_STATUS_PAGE_GPA && ++ reserved->size == MICROVM_SHARED_STATUS_PAGE_SIZE && ++ !of_property_present(child, "no-map")) ++ matches++; ++ } ++ of_node_put(root); ++ return matches == 1; ++} ++#endif ++ + static int __init virtio_mmio_init(void) + { + #ifdef CONFIG_VIRTIO_MMIO_SHARED_STATUS ++#ifdef CONFIG_X86 + microvm_shared_status_available = e820__mapped_all( + MICROVM_SHARED_STATUS_PAGE_GPA, + MICROVM_SHARED_STATUS_PAGE_GPA + MICROVM_SHARED_STATUS_PAGE_SIZE, + E820_TYPE_RESERVED); ++#else ++ microvm_shared_status_available = microvm_shared_status_from_of(); ++#endif + #endif + + return platform_driver_register(&virtio_mmio_driver); diff --git a/nvx-rs/README.md b/nvx-rs/README.md index e7cb2e8b7..5218bb770 100644 --- a/nvx-rs/README.md +++ b/nvx-rs/README.md @@ -144,7 +144,7 @@ changes only when the module's purpose does. | [`nvx::guest::cgroup`](#nvxguestcgroup) | Cgroup v2 handles through which a guest init or agent places, accounts, kills, and settles its executions | | [`nvx::guest::exec`](#nvxguestexec) | `Execution`: drives one program as an execution, for any guest init or agent | | [`nvx::guest::exec::binding`](#nvxguestexecbinding) | `Binding`: an `nvx::exec::Runner` bound to an `Execution`, the glue that a guest adapter would otherwise write | -| [`nvx::guest::time`](#nvxguesttime) | x86_64: the steps of NVX time ABI v1 as typed calls in the caller's process, for any guest init or agent | +| [`nvx::guest::time`](#nvxguesttime) | shared cold-boot time synchronization and daemon calls for x86_64 and ARM64, with capture and restore on x86_64 | | [`nvx::guest::clone`](#nvxguestclone) | x86_64: the preparation of a template's guest for a capture, and the repair and activation of each clone | | [`nvx::guest::reseed`](#nvxguestreseed) | A restore's fresh entropy, and the reseed of the kernel's random number generator with it | | [`nvx::guest::root`](#nvxguestroot) | The operations of a layered root: its bootstrap, assembly, shares, export, mappings, and teardown | @@ -402,6 +402,18 @@ manifest, in a checkout's `build/` and `openvmm/target/release/` or a release's `guest/` and `bin/`, checked in the order and with the messages of `nvx.py`. `ArtifactPaths` lists them all and serializes as JSON. +`Architecture` makes the guest ISA explicit: +`ArtifactDir::with_architecture(Architecture::Aarch64)` selects a checkout's +`build/aarch64/Image` and matching Alpine or Ubuntu initramfs and package +manifest, without changing its OpenVMM location. The default remains x86-64. +ARM kernel headers and each distribution's package architecture are checked; +ARM release layouts, bundles, Azure Linux and managed sandbox artifacts are +not yet qualified and are rejected rather than resolved as x86 artifacts. +Standalone `download` still selects the legacy x86-64 packages for the host OS +and requested backend, including when the downloading CLI runs on ARM. +Downloading those files does not admit their execution on a different ISA; +an ARM runtime's request for ARM release artifacts is still rejected. + `ArtifactDir::runtime_identity` reads the `RuntimeIdentity` of the OpenVMM executable, kernel, and Alpine initramfs from the install metadata, a release's `SHA256SUMS` or a checkout's build provenance, without hashing an artifact. @@ -1374,11 +1386,35 @@ binding could not read the launch report. ### `nvx::guest::time` -x86_64: `TimeAbi`, the steps of NVX time ABI v1 as typed calls in the caller's -process, for any guest init or agent: the cold-boot time step and, separately, -the start of the time daemon in a child process that the caller supervises, the -VM generation ID, a `CaptureRequest` that repairs a `Restore`, and a -`RestoreFinish`. +`TimeAbi` exposes the same cold-boot time step, supervised daemon startup and VM +generation-ID calls on x86_64 and ARM64 Linux. `/sbin/nvx-time` uses one command +dispatcher and these same calls on both architectures. The initial UTC step +and frequency hold finish before `boot` returns; architectural conformance +checks run later in the child, before it publishes its daemon PID and completed +boot status. + +The daemon shares kernel-log watching, violation delivery and accounting, +bounded sample selection, step/slew/frequency-hold policy, state publication, +and polling cadence. A failed runtime clock adjustment rejects the sample on +either architecture; a failed check remains a failed check in report-only +mode, with its actual failure count. The initial synchronization is not counted +as a runtime discontinuity. +Unreadable or malformed state is an explicit failure, not a synthetic pending +record. Loss of the watcher, state publisher or discipline timer is a daemon +failure, rather than a silently inactive service. A private daemon-file lock +keeps ownership independent of the root assembler's runtime-directory lock. + +Only the hardware adapter and conformance checks depend on the ISA. x86 uses +PIO, the TSC and Linux's kHz-rounded conversion. ARM uses the restricted +platform device, CNTVCT and actual CNTFRQ, with the selected a8 profile identity +and full-Hz frequency. No vendor name, MIDR value or observed host frequency +selects the guest's time policy. The a6 identity, a7 sample and a8 binding +formats are unchanged. + +Capture and restore remain x86_64-only: a `CaptureRequest` repairs a `Restore`, +and `RestoreFinish` completes it. ARM cold boot does not accept or acknowledge +restore requests; sharing the invocation and daemon does not add ARM snapshot +support or qualify a new CPU or host. The request's mode selects how a restore hands over its fresh entropy: the file mode, which `nvx-time` keeps, writes it to a file, and the in-memory mode @@ -1390,11 +1426,10 @@ that owns the VM's exit status and shutdown selects, the call fails with the `Violation`, whose `status` is the one with which enforcing mode stops the VM, and the time daemon and a capture's preparation enforce the ABI all the same. -A call takes the permission for OpenVMM's ports on the calling thread for each -port transaction alone, and fails with `TimeError::PortRelease` if the thread -may still hold some of it, which a process that it started would inherit, and -from which `nvx::guest::spawn` starts none. No call waits for a child that it -did not start. +A call takes hardware authority only for its transaction: x86's port permission +or ARM's restricted device mapping or descriptor. A release failure is explicit, +and `nvx::guest::spawn` refuses to inherit retained authority. No call waits for +a child that it did not start. `nvx-time` runs its steps through them. @@ -2757,18 +2792,37 @@ disk's block device. | `nvx-console-pending DEVICE` | Prints the number of bytes waiting to be read from a device, such as a console | | `nvx-mmio-write ADDRESS VALUE` | Stores a 32-bit value at a physical address through `/dev/mem` (x86_64) | | `nvx-reseed [--generation-only] SEED_FILE [PREVIOUS]`, `nvx-reseed --sample` | Reseeds the kernel's random number generator from a seed file and checks its VM generation ID, or prints a random sample | -| `nvx-exit [CODE]` | Stops the VM with an 8-bit exit code through OpenVMM's control port 0x604 (x86_64) | +| `nvx-exit [CODE]` | Stops the VM with an 8-bit exit code through OpenVMM's control port 0x604 on x86_64 or the restricted platform device's shutdown register on aarch64 | | `nvx-device-io file ...`, `nvx-device-io network ...`, `nvx-device-io map PATH TEXT` | The benchmark helper that measures file and UDP round-trip I/O and prints one JSON line, and reports a file benchmark's progress on stderr; `map` writes TEXT at the start of a file through a shared mapping and holds the dirty mapping until its standard input ends, for the microVM scenarios (x86_64) | | `nvx-managed-agent CONTROL_TTY ROOTFS HOSTNAME UID GID USER HOME` | Serves the managed control session and supervises sequential workloads; `--exec-config-fd FD -- WORKLOAD ...` is the direct agent's internal launch helper | | `nvx-ingress [inherit\|start] GATEWAY_IPV4` | Announces reachable TCP listeners and settled UDP sockets to the portable-network gateway, from a privileged source port that it binds, or, with `inherit`, from its standard input, a socket that a clone's application bound to one; `start` starts `inherit` as `Announcer::start` does and exits once it runs, or with status 1 if it does not start, for a guest agent that cannot hold the announcer; `test DIRECTORY` runs synthetic procfs snapshots | -| `nvx-time [--report-only] COMMAND` | Enforces NVX time ABI v1: runs its conformance checks, repairs the wall clock after a restore, publishes the time state, and runs the discipline and violation-watcher daemon; `test NAME ...` runs its decoder and process test hooks (x86_64) | +| `nvx-time [--report-only] COMMAND` | Enforces NVX time ABI v1 through shared cold-boot checks, time-state publication, and the discipline and violation-watcher daemon on x86_64 and aarch64; capture/restore and the `test NAME ...` decoder and process hooks remain x86_64-only | | `nvx-time-probe latency\|warp\|clock ...` | Diagnoses clock-read latency, cross-CPU TSC warp and offset, the clocks, CPUID, clock events, kernel messages, and optionally MSRs (x86_64) | -| `nvx-init TIME_DAEMON [ANNOUNCER]` | Takes PID 1 over from a layered root's early boot, with its time daemon and optional announcer: gives itself and them an out-of-memory score adjustment of -1000 and a nice value of -10, collects every child's status, and, after `sync`, ends the VM with status 125 when a helper ends or 0 on `SIGTERM`; elsewhere it exits with 2 (x86_64) | +| `nvx-init TIME_DAEMON [ANNOUNCER]` | Takes PID 1 over from a layered root's early boot, with its time daemon and optional announcer: gives itself and them an out-of-memory score adjustment of -1000 and a nice value of -10, collects every child's status, and, after `sync`, ends the VM with status 125 when a helper ends or 0 on `SIGTERM`; elsewhere it exits with 2 (x86_64, aarch64) | + +The init agent also uses the private, read-only +`nvx-init --root-device distro|scratch|no-scratch` query on ARM. It resolves +only the fixed distro and scratch protocol roles through root-child OF +`reg` properties, their declared big-endian address/size cells, and the +virtio-MMIO driver and block-child identities; it does not depend on a +generic platform `resource` file or `/dev/vdX` probe order. Unsupported +translated buses, malformed or ambiguous resources, and wrong drivers are +errors. The query never prepares a root, mounts a filesystem, changes a +partition, or enters PID 1 supervision. + +Status 0 prints a ready device, or confirms transport absence for +`no-scratch`; status 1 means not yet ready, or a present scratch transport +for `no-scratch`; status 2 reports invalid arguments or a fatal lookup error. +An unbound transport or one without a block child is still present. The ARM +shell launcher retries only status 1, for its existing bounded probe +interval; its x86 command-line discovery is unchanged. Each binary's command line, exit statuses, and byte output are compatibility contracts; the [`nvx::guest`](src/guest/mod.rs) documentation describes each -program. Guests run them as static `x86_64-unknown-linux-musl` executables. On -Linux, with the musl target and a C compiler driver, `cc`, `nvx.py` builds them: +program. Guest builds default to static `x86_64-unknown-linux-musl` +executables; `--architecture aarch64` selects `aarch64-unknown-linux-musl` +for the supported ARM programs. On Linux, with the musl target and a C compiler +driver, `cc`, `nvx.py` builds them: ```bash rustup target add x86_64-unknown-linux-musl @@ -2777,8 +2831,9 @@ python3 scripts/nvx.py build-guest-binaries \ ``` Native guest builds, the digest-pinned Docker Rust stage, and CI all call this -routine. Docker builds the set once and installs identical bytes in all three -guest images; Ubuntu determinism checks rebuild from fresh targets. +routine. The default Docker build compiles the set once and installs identical +bytes in all three guest images; Ubuntu determinism checks rebuild from fresh +targets. Of this crate, the guest images depend only on the files that this build reads: `Cargo.toml`, `Cargo.lock`, `build.rs`, any Cargo configuration in `.cargo/`, diff --git a/nvx-rs/build.rs b/nvx-rs/build.rs index 1d81bb83a..b9f854dae 100644 --- a/nvx-rs/build.rs +++ b/nvx-rs/build.rs @@ -68,6 +68,9 @@ mod bundle { // The OpenVMM backend supports Linux and Windows hosts only. _ => return, }; + if env::var("CARGO_CFG_TARGET_ARCH").as_deref() != Ok("x86_64") { + fail("no qualified ARM64 bundle/release catalog exists; build explicit local ARM64 artifacts without bundled instead of staging an x86 package".to_owned()); + } let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR is set")); let source = match env::var_os(SOURCE_DIR) { Some(directory) => { diff --git a/nvx-rs/public-api/linux.txt b/nvx-rs/public-api/linux.txt index 1d704930d..9c61fd1d4 100644 --- a/nvx-rs/public-api/linux.txt +++ b/nvx-rs/public-api/linux.txt @@ -1649,7 +1649,13 @@ fn nvx::guest::time_probe::main (hidden) fn nvx::guest_path mod nvx::host mod nvx::host::artifacts +enum nvx::host::artifacts::Architecture +variant nvx::host::artifacts::Architecture::Aarch64 +variant nvx::host::artifacts::Architecture::X86_64 +method nvx::host::artifacts::Architecture::kernel_name +method nvx::host::artifacts::Architecture::name struct nvx::host::artifacts::ArtifactDir +method nvx::host::artifacts::ArtifactDir::architecture method nvx::host::artifacts::ArtifactDir::artifact method nvx::host::artifacts::ArtifactDir::distro_layer method nvx::host::artifacts::ArtifactDir::distro_manifest @@ -1668,6 +1674,7 @@ method nvx::host::artifacts::ArtifactDir::require_openvmm method nvx::host::artifacts::ArtifactDir::root method nvx::host::artifacts::ArtifactDir::runtime_identity method nvx::host::artifacts::ArtifactDir::sandbox_artifacts +method nvx::host::artifacts::ArtifactDir::with_architecture struct nvx::host::artifacts::ArtifactPaths field nvx::host::artifacts::ArtifactPaths::guests field nvx::host::artifacts::ArtifactPaths::kernel diff --git a/nvx-rs/public-api/windows.txt b/nvx-rs/public-api/windows.txt index b52056edd..baa71e6c3 100644 --- a/nvx-rs/public-api/windows.txt +++ b/nvx-rs/public-api/windows.txt @@ -593,7 +593,13 @@ method nvx::gpt::ReadAt::read_exact_at fn nvx::guest_path mod nvx::host mod nvx::host::artifacts +enum nvx::host::artifacts::Architecture +variant nvx::host::artifacts::Architecture::Aarch64 +variant nvx::host::artifacts::Architecture::X86_64 +method nvx::host::artifacts::Architecture::kernel_name +method nvx::host::artifacts::Architecture::name struct nvx::host::artifacts::ArtifactDir +method nvx::host::artifacts::ArtifactDir::architecture method nvx::host::artifacts::ArtifactDir::artifact method nvx::host::artifacts::ArtifactDir::distro_layer method nvx::host::artifacts::ArtifactDir::distro_manifest @@ -612,6 +618,7 @@ method nvx::host::artifacts::ArtifactDir::require_openvmm method nvx::host::artifacts::ArtifactDir::root method nvx::host::artifacts::ArtifactDir::runtime_identity method nvx::host::artifacts::ArtifactDir::sandbox_artifacts +method nvx::host::artifacts::ArtifactDir::with_architecture struct nvx::host::artifacts::ArtifactPaths field nvx::host::artifacts::ArtifactPaths::guests field nvx::host::artifacts::ArtifactPaths::kernel diff --git a/nvx-rs/src/bin/nvx-exit.rs b/nvx-rs/src/bin/nvx-exit.rs index 671f0ea73..e766d441e 100644 --- a/nvx-rs/src/bin/nvx-exit.rs +++ b/nvx-rs/src/bin/nvx-exit.rs @@ -1,13 +1,18 @@ //! `nvx-exit [CODE]`; see `nvx::guest::exit`. -#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +#[cfg(all( + target_os = "linux", + any(target_arch = "x86_64", target_arch = "aarch64") +))] fn main() -> std::process::ExitCode { nvx::guest::exit::main() } -// The program writes to an I/O port of NVX's x86_64 guests. Elsewhere this stub fails, so that -// every target that CI checks with `--all-features` still builds. -#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))] +// Unsupported targets retain the CI stub; Linux x86-64 and ARM64 use the guest implementation. +#[cfg(not(all( + target_os = "linux", + any(target_arch = "x86_64", target_arch = "aarch64") +)))] fn main() -> std::process::ExitCode { std::process::ExitCode::FAILURE } diff --git a/nvx-rs/src/bin/nvx-init.rs b/nvx-rs/src/bin/nvx-init.rs index 69875136a..394c6c96a 100644 --- a/nvx-rs/src/bin/nvx-init.rs +++ b/nvx-rs/src/bin/nvx-init.rs @@ -1,13 +1,18 @@ //! `nvx-init TIME_DAEMON [ANNOUNCER]`; see `nvx::guest::init`. -#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +#[cfg(all( + target_os = "linux", + any(target_arch = "x86_64", target_arch = "aarch64") +))] fn main() -> std::process::ExitCode { nvx::guest::init::main() } -// PID 1 stops NVX's x86_64 guests through an I/O port. Elsewhere this stub fails, so that every -// target that CI checks with `--all-features` still builds. -#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))] +// Unsupported targets retain the CI stub; Linux x86-64 and ARM64 use the Rust PID 1. +#[cfg(not(all( + target_os = "linux", + any(target_arch = "x86_64", target_arch = "aarch64") +)))] fn main() -> std::process::ExitCode { std::process::ExitCode::FAILURE } diff --git a/nvx-rs/src/bin/nvx-time.rs b/nvx-rs/src/bin/nvx-time.rs index eb666d60d..a2d04f9a9 100644 --- a/nvx-rs/src/bin/nvx-time.rs +++ b/nvx-rs/src/bin/nvx-time.rs @@ -1,14 +1,17 @@ //! `nvx-time [--report-only] COMMAND`, `nvx-time test NAME ARGUMENTS...`; see `nvx::guest::time`. -#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +#[cfg(all( + target_os = "linux", + any(target_arch = "x86_64", target_arch = "aarch64") +))] fn main() -> std::process::ExitCode { nvx::guest::time::main() } -// The program reads the time ABI's I/O ports and x86 time-stamp counter of NVX's x86_64 guests. -// Elsewhere this stub fails, so that every target that CI checks with `--all-features` still -// builds. -#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))] +#[cfg(not(all( + target_os = "linux", + any(target_arch = "x86_64", target_arch = "aarch64") +)))] fn main() -> std::process::ExitCode { std::process::ExitCode::FAILURE } diff --git a/nvx-rs/src/guest/entry/tests.rs b/nvx-rs/src/guest/entry/tests.rs index b85764bbe..e97780987 100644 --- a/nvx-rs/src/guest/entry/tests.rs +++ b/nvx-rs/src/guest/entry/tests.rs @@ -1486,6 +1486,14 @@ fn check_search(entry: &Entry, root: &Path) { std::fs::remove_dir_all(root.join("tmp/only-inside")).unwrap(); } +fn kernel_interface_exists(path: &str) -> bool { + match std::fs::symlink_metadata(path) { + Ok(_) => true, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => false, + Err(error) => panic!("read kernel interface {path}: {error}"), + } +} + #[test] fn privileged_entries_see_their_own_view_and_identity() { if crate::guest::managed_agent::tests::isolate_process_test( @@ -1578,7 +1586,14 @@ fn privileged_entries_see_their_own_view_and_identity() { for set in ["CapPrm", "CapEff", "CapBnd"] { assert_eq!(value(&output, set), mask, "{set}"); } - assert_eq!(value(&output, "kcore"), "0"); + assert_eq!( + value(&output, "kcore"), + if kernel_interface_exists("/proc/kcore") { + "0" + } else { + "unreadable" + } + ); assert_eq!(value(&output, "firmware"), ""); for point in [ "/proc/sys", @@ -1586,7 +1601,16 @@ fn privileged_entries_see_their_own_view_and_identity() { "/proc/kcore", "/sys/firmware", ] { - assert!(mount_options(&output, point).contains(&"ro"), "{point}"); + if kernel_interface_exists(point) { + assert!(mount_options(&output, point).contains(&"ro"), "{point}"); + } else { + assert!( + !values(&output, "mount") + .iter() + .any(|mount| mount.starts_with(&format!("{point} "))), + "an absent kernel interface must not acquire a manufactured mount: {point}" + ); + } } // A nested `chroot` and `..` from it land at the top of the root, never above. for escape in values(&output, "escape") { diff --git a/nvx-rs/src/guest/exit.rs b/nvx-rs/src/guest/exit.rs index 0e99924da..ff054fa53 100644 --- a/nvx-rs/src/guest/exit.rs +++ b/nvx-rs/src/guest/exit.rs @@ -1,19 +1,31 @@ -//! `nvx-exit [CODE]`: stops the VM with an 8-bit exit code, by writing one byte to OpenVMM's -//! control port 0x604 through `/dev/port`. +//! `nvx-exit [CODE]`: stops the VM with an 8-bit exit code through OpenVMM's platform control +//! interface: control port 0x604 through `/dev/port` on x86-64, or the shutdown register of the +//! restricted platform device on AArch64. //! //! CODE defaults to 1, and so does anything other than a decimal number from 0 through 255. //! Further arguments are ignored, and the program prints nothing. It exits with 1 when it cannot //! write the byte. -use std::ffi::{CStr, CString}; use std::process::ExitCode; use super::status::{CommandError, run_main}; -use super::sys::{self, Mode, OpenFlags, PORT_DEVICE, Port}; +use super::sys; +#[cfg(target_arch = "x86_64")] +use super::sys::{Mode, OpenFlags, PORT_DEVICE, Port}; /// Runs `nvx-exit` with the process's arguments. It exits with 0 after it writes the byte, /// unless the VM stops first, or with 1 when it cannot write the byte. pub fn main() -> ExitCode { + #[cfg(target_arch = "aarch64")] + return run_main(|args| { + let code = exit_code(args.get(1).map(|argument| argument.as_bytes())); + let access = sys::platform::Access::request().map_err(platform_error)?; + access + .write_u8(sys::platform::Register::Shutdown, code) + .map_err(platform_error)?; + access.release().map_err(platform_error) + }); + #[cfg(target_arch = "x86_64")] run_main(|args| run(args, PORT_DEVICE)) } @@ -22,7 +34,8 @@ pub fn main() -> ExitCode { /// Any failure to open the device or write the byte is an operation failure, which the program /// does not report. The descriptor closes when the write returns, whereas the C program left it /// open until the process exited; neither the output nor the status differs. -fn run(args: &[CString], port: &CStr) -> Result<(), CommandError> { +#[cfg(target_arch = "x86_64")] +fn run(args: &[std::ffi::CString], port: &std::ffi::CStr) -> Result<(), CommandError> { let code = exit_code(args.get(1).map(|argument| argument.as_bytes())); let written = sys::open(port, OpenFlags::O_WRONLY, Mode::empty()) .and_then(|descriptor| descriptor.pwrite(&[code], Port::Control.offset())); @@ -33,6 +46,13 @@ fn run(args: &[CString], port: &CStr) -> Result<(), CommandError> { } } +/// Reports an ARM platform operation failure without treating an absent device as shutdown. +#[cfg(target_arch = "aarch64")] +fn platform_error(error: sys::Errno) -> CommandError { + sys::write_standard_error(format!("nvx-exit: platform access: {error}\n").as_bytes()); + CommandError::Operation +} + /// Returns the VM exit code that `argument` selects. /// /// A nonempty decimal number from 0 through 255 is accepted, including leading zeros. A missing diff --git a/nvx-rs/src/guest/exit/tests.rs b/nvx-rs/src/guest/exit/tests.rs index a5bee0d02..8a25ea7ad 100644 --- a/nvx-rs/src/guest/exit/tests.rs +++ b/nvx-rs/src/guest/exit/tests.rs @@ -1,7 +1,11 @@ +use std::ffi::CString; +#[cfg(target_arch = "x86_64")] use std::fs; +#[cfg(target_arch = "x86_64")] use std::os::unix::ffi::OsStrExt; use super::*; +#[cfg(target_arch = "x86_64")] use crate::guest::status::ExitStatus; use crate::guest::status::tests::command_line; @@ -40,6 +44,7 @@ fn exit_code_takes_a_decimal_number_up_to_255() { } #[test] +#[cfg(target_arch = "x86_64")] fn writes_one_byte_at_the_control_port() { let file = tempfile::NamedTempFile::new().unwrap(); let port = CString::new(file.path().as_os_str().as_bytes()).unwrap(); @@ -58,6 +63,7 @@ fn writes_one_byte_at_the_control_port() { } #[test] +#[cfg(target_arch = "x86_64")] fn fails_when_it_cannot_open_or_write_the_device() { let directory = tempfile::tempdir().unwrap(); let missing = directory.path().join("port"); diff --git a/nvx-rs/src/guest/init.rs b/nvx-rs/src/guest/init.rs index 722f755c2..1096456fe 100644 --- a/nvx-rs/src/guest/init.rs +++ b/nvx-rs/src/guest/init.rs @@ -26,13 +26,17 @@ //! controlling terminal, or to its standard error when the console does not open, and reads //! nothing from it; //! - ends the VM itself, without another guest program: it writes every file system's modified -//! data to its storage, with `sync`, and then stops the VM through OpenVMM's control port with a -//! status: 0 after `SIGTERM`, or 125, as `nvx-init-agent`'s failures do, after a failure, which +//! data to its storage, with `sync`, and then stops the VM through OpenVMM's control interface +//! with a status: 0 after `SIGTERM`, or 125, as `nvx-init-agent`'s failures do, after a failure, which //! an `NVX-INIT-ERROR:` line on the console names first. //! -//! Outside PID 1, `nvx-init` changes nothing: it fails with status 2 and a message. +//! Outside PID 1, the default invocation fails with status 2. Its private read-only +//! `--root-device distro|scratch|no-scratch` mode only discovers the fixed block roles: +//! 0 means found/satisfied, 1 means not yet present (or scratch present for `no-scratch`), +//! and 2 means invalid input or a fatal discovery error. It changes no devices or process state. use std::ffi::{CStr, CString}; +use std::io::{self, Write}; use std::process::ExitCode; use std::time::Duration; @@ -188,12 +192,25 @@ impl System for RealSystem { /// then ends it, so it does not return; elsewhere it fails with status 2. #[doc(hidden)] pub fn main() -> ExitCode { + let arguments = sys::args(); + if let Some(result) = root_device_command( + arguments.get(1..).unwrap_or_default(), + &mut io::stdout().lock(), + super::root::lookup_block_role, + ) { + return match result { + Ok(satisfied) => ExitCode::from(u8::from(!satisfied)), + Err(error) => { + sys::write_standard_error(format!("nvx-init: root device: {error}\n").as_bytes()); + ExitCode::from(2) + } + }; + } sys::restore_sigpipe(); if sys::current_process().raw() != INIT_PROCESS { sys::write_standard_error(b"nvx-init: runs only as PID 1\n"); return ExitStatus::Usage.into(); } - let arguments = sys::args(); init( &mut RealSystem { signals: None }, arguments.get(1..).unwrap_or_default(), @@ -201,6 +218,48 @@ pub fn main() -> ExitCode { ExitStatus::Failure.into() } +/// Dispatches the private pure lookup before PID-1 behavior, with no arbitrary address input. +fn root_device_command( + arguments: &[CString], + out: &mut dyn Write, + lookup: impl FnOnce(super::root::Role) -> io::Result, +) -> Option> { + use super::root::{BlockRoleState, Role}; + if arguments + .first() + .is_none_or(|argument| argument.as_bytes() != b"--root-device") + { + return None; + } + let (role, absence) = match arguments { + [_, role] if role.as_bytes() == b"distro" => (Role::Distro, false), + [_, role] if role.as_bytes() == b"scratch" => (Role::Scratch, false), + [_, role] if role.as_bytes() == b"no-scratch" => (Role::Scratch, true), + _ => { + return Some(Err( + "usage: nvx-init --root-device distro|scratch|no-scratch".into(), + )); + } + }; + Some( + lookup(role) + .map_err(|error| error.to_string()) + .and_then(|state| { + if absence { + return Ok(matches!(state, BlockRoleState::Absent)); + } + match state { + BlockRoleState::Absent | BlockRoleState::Probing => Ok(false), + BlockRoleState::Ready(path) => { + writeln!(out, "{}", path.display()).map_err(|error| error.to_string())?; + out.flush().map_err(|error| error.to_string())?; + Ok(true) + } + } + }), + ) +} + /// Serves as PID 1 with the helpers that `arguments` name until the VM must end, and then ends /// it: with 0 after `SIGTERM`, or with [`FAILURE`] after the diagnostic of a failure. fn init(system: &mut impl System, arguments: &[CString]) { diff --git a/nvx-rs/src/guest/init/tests.rs b/nvx-rs/src/guest/init/tests.rs index 06aaf285c..760f0ac28 100644 --- a/nvx-rs/src/guest/init/tests.rs +++ b/nvx-rs/src/guest/init/tests.rs @@ -18,6 +18,108 @@ use crate::guest::sys::tests::{ }; use crate::guest::sys::{ForkResult, wait}; +#[test] +fn read_only_root_queries_use_only_named_roles_and_distinguish_absence_and_errors() { + use crate::guest::root::{BlockRoleState, Role}; + let arguments = |role: &str| { + [ + CString::new("--root-device").unwrap(), + CString::new(role).unwrap(), + ] + }; + let mut out = Vec::new(); + assert_eq!( + root_device_command(&arguments("distro"), &mut out, |role| { + assert_eq!(role, Role::Distro); + Ok(BlockRoleState::Ready("/dev/vdz".into())) + }), + Some(Ok(true)), + ); + assert_eq!(out, b"/dev/vdz\n"); + out.clear(); + assert_eq!( + root_device_command(&arguments("scratch"), &mut out, |role| { + assert_eq!(role, Role::Scratch); + Ok(BlockRoleState::Ready("/dev/vdc".into())) + }), + Some(Ok(true)), + ); + assert_eq!(out, b"/dev/vdc\n"); + for state in [BlockRoleState::Absent, BlockRoleState::Probing] { + out.clear(); + assert_eq!( + root_device_command(&arguments("distro"), &mut out, |_| Ok(state)), + Some(Ok(false)) + ); + assert!(out.is_empty()); + } + for (state, expected) in [ + (BlockRoleState::Absent, true), + (BlockRoleState::Probing, false), + (BlockRoleState::Ready("/dev/vdz".into()), false), + ] { + out.clear(); + assert_eq!( + root_device_command(&arguments("no-scratch"), &mut out, |role| { + assert_eq!(role, Role::Scratch); + Ok(state) + }), + Some(Ok(expected)), + ); + assert!(out.is_empty()); + } + assert!( + root_device_command(&arguments("distro"), &mut out, |_| { + Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "duplicate or wrong driver", + )) + }) + .unwrap() + .unwrap_err() + .contains("duplicate or wrong driver") + ); + assert!( + root_device_command(&arguments("0xd0003000"), &mut out, |_| panic!( + "no arbitrary addresses" + )) + .unwrap() + .is_err() + ); + for invalid in [ + vec![CString::new("--root-device").unwrap()], + vec![ + CString::new("--root-device").unwrap(), + CString::new("distro").unwrap(), + CString::new("extra").unwrap(), + ], + ] { + assert!( + root_device_command(&invalid, &mut out, |_| panic!( + "no lookup for invalid arguments" + )) + .unwrap() + .is_err() + ); + } + assert!( + root_device_command(&[], &mut out, |_| panic!("default init is not discovery")).is_none() + ); +} + +#[test] +fn read_only_root_queries_surface_device_output_failures() { + let arguments = ["--root-device", "distro"].map(|value| CString::new(value).unwrap()); + let mut buffer: [u8; 0] = []; + assert!( + root_device_command(&arguments, &mut buffer.as_mut_slice(), |_| { + Ok(crate::guest::root::BlockRoleState::Ready("/dev/vdz".into())) + }) + .unwrap() + .is_err() + ); +} + /// One operation that PID 1 asked its system for. #[derive(Clone, Debug, PartialEq, Eq)] enum Call { diff --git a/nvx-rs/src/guest/kernel_text.rs b/nvx-rs/src/guest/kernel_text.rs index a6d6c3460..4405e0b5c 100644 --- a/nvx-rs/src/guest/kernel_text.rs +++ b/nvx-rs/src/guest/kernel_text.rs @@ -57,6 +57,7 @@ impl<'a> KmsgRecord<'a> { /// One line of the kernel's log as `syslog(2)` returns it: ``, then `[seconds]` when the /// kernel stamps its messages, then the message. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg(any(target_arch = "x86_64", test))] pub(crate) struct SyslogLine<'a> { /// Microseconds since boot of the line's timestamp, when it has a well-formed one. pub(crate) timestamp_us: Option, @@ -64,6 +65,7 @@ pub(crate) struct SyslogLine<'a> { pub(crate) message: &'a [u8], } +#[cfg(any(target_arch = "x86_64", test))] impl<'a> SyslogLine<'a> { /// Parses one line. A timestamp is spaces, then decimal seconds with a decimal fraction, /// between brackets. A line whose brackets hold anything else keeps them in its message. @@ -92,6 +94,7 @@ impl<'a> SyslogLine<'a> { } /// Parses a log timestamp, spaces then `seconds.fraction` in decimal, into microseconds. +#[cfg(any(target_arch = "x86_64", test))] fn microseconds(stamp: &[u8]) -> Option { /// Microseconds in a second. const PER_SECOND: u64 = 1_000_000; @@ -157,6 +160,7 @@ impl TickMode { } /// Returns the mode's name, as the time probe prints it. + #[cfg(target_arch = "x86_64")] pub(crate) const fn name(self) -> &'static str { match self { Self::Periodic => "periodic", @@ -194,6 +198,7 @@ impl ClockEventState { } /// Returns the state's name, as the time probe prints it. + #[cfg(any(target_arch = "x86_64", test))] pub(crate) const fn name(self) -> &'static str { match self { Self::Detached => "detached", diff --git a/nvx-rs/src/guest/managed_agent/seam.rs b/nvx-rs/src/guest/managed_agent/seam.rs index 16fa5b610..23eabb771 100644 --- a/nvx-rs/src/guest/managed_agent/seam.rs +++ b/nvx-rs/src/guest/managed_agent/seam.rs @@ -38,7 +38,11 @@ impl Default for Paths { cgroup_root: "/sys/fs/cgroup".into(), hostfs_dir: "/run/nvx/hostfs".into(), proc_cmdline: "/proc/cmdline".into(), - diagnostic_port: "/dev/port".into(), + diagnostic_port: if cfg!(target_arch = "x86_64") { + "/dev/port".into() + } else { + "/dev/console".into() + }, null_device: "/dev/null".into(), machine_id: "/run/nvx/workload-machine-id".into(), time_status: "/run/nvx/time".into(), diff --git a/nvx-rs/src/guest/managed_agent/tests.rs b/nvx-rs/src/guest/managed_agent/tests.rs index 2c993e854..f63f10aba 100644 --- a/nvx-rs/src/guest/managed_agent/tests.rs +++ b/nvx-rs/src/guest/managed_agent/tests.rs @@ -15,20 +15,42 @@ pub(crate) static PROCESS_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::ne /// should execute the test body. Isolation prevents PID-1-style `waitpid(-1)` behavior from /// reaping children that unrelated parallel host tests own. pub(crate) fn isolate_process_test(marker: &str, test: &str) -> bool { - if std::env::var_os(marker).is_some() { + if let Some(receipt) = std::env::var_os(marker) { + std::fs::write(receipt, test).expect("record entry into the selected isolated test"); return false; } + let directory = tempfile::tempdir().expect("create isolated-test receipt directory"); + let receipt = directory.path().join("entered"); let status = std::process::Command::new( std::env::current_exe().expect("the test executable has a path"), ) .args(["--exact", test, "--nocapture"]) - .env(marker, "1") + .env(marker, &receipt) .status() .expect("the isolated test process starts"); assert!(status.success(), "the isolated test process failed"); + assert_eq!( + std::fs::read_to_string(receipt).ok().as_deref(), + Some(test), + "the isolated test did not enter its body; an exact selector matched zero tests or an ignored test", + ); true } +#[test] +fn process_isolation_rejects_successful_zero_test_subprocesses() { + let result = std::panic::catch_unwind(|| { + isolate_process_test( + "NVX_MISSING_ISOLATED_TEST_RECEIPT", + "guest::this_exact_isolated_test_does_not_exist", + ) + }); + assert!( + result.is_err(), + "zero matching tests must not be a successful isolated run" + ); +} + impl AgentConfig { /// Returns the configuration of a test agent of the workload `1000:1000`, named `workload` /// and homed in `/home/workload`: a direct agent, or a sandbox agent whose assembled root is diff --git a/nvx-rs/src/guest/mappings/mapper/tests.rs b/nvx-rs/src/guest/mappings/mapper/tests.rs index b4d5263d0..ba648865b 100644 --- a/nvx-rs/src/guest/mappings/mapper/tests.rs +++ b/nvx-rs/src/guest/mappings/mapper/tests.rs @@ -23,6 +23,7 @@ impl Mapped { } } +#[cfg(target_arch = "x86_64")] impl TeardownError { /// Returns the error of a teardown that failed to unmount the mapping at `target`, an /// absolute path in the root, with `error`, for the tests of other modules. diff --git a/nvx-rs/src/guest/mod.rs b/nvx-rs/src/guest/mod.rs index 0640ca4ae..cba9eba5c 100644 --- a/nvx-rs/src/guest/mod.rs +++ b/nvx-rs/src/guest/mod.rs @@ -30,16 +30,16 @@ //! //! Each other module holds one program, and the crate's binary of the same name runs its `main`. //! Two of them also offer their steps as typed calls, which any guest init or agent runs in its -//! own process and their programs run on: on x86-64, `time`'s `TimeAbi` runs the time ABI's -//! cold-boot step, capture, and restore finish, and [`reseed`] reseeds the kernel's random number +//! own process and their programs run on: `time`'s `TimeAbi` runs the shared cold-boot step +//! and daemon, and, on x86-64, capture and restore finish; [`reseed`] reseeds the kernel's random number //! generator with a restore's fresh entropy, which starts with the new [`GenerationId`]. And //! [`ingress`] starts its program, the announcer of the guest's listeners, from a socket that it //! binds for it, as a clone's application does. The //! libraries need none of NVX's control protocol, managed agent, or workload identity, and NVX's //! managed agent opens its control terminal, applies its host mapping table, and starts, drives, -//! collects, and settles its workloads through them. On x86-64, the hidden `init` module holds -//! `nvx-init`, a PID 1 for layered roots, which takes PID 1 over from their early boot with the -//! guest's helpers, the time daemon and the announcer: it runs with them ahead of the workloads, +//! collects, and settles its workloads through them. On x86-64 and AArch64, the hidden `init` +//! module holds `nvx-init`, a PID 1 for layered roots, which takes PID 1 over from their early +//! boot with the guest's helpers, the time daemon and the announcer: it runs ahead of the workloads, //! collects every child's status, and ends the VM after `SIGTERM` or once either helper ends. //! //! The programs keep the command lines, exit statuses, and output of the C programs that they @@ -96,7 +96,6 @@ mod status; #[allow(unsafe_code)] mod sys; -#[cfg(target_arch = "x86_64")] mod kernel_text; pub use generation::GenerationId; @@ -111,13 +110,13 @@ pub mod console_pending; pub mod device_io; pub mod entry; pub mod exec; -#[cfg(target_arch = "x86_64")] +#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] #[doc(hidden)] pub mod exit; #[doc(hidden)] pub mod gpt; pub mod ingress; -#[cfg(target_arch = "x86_64")] +#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] #[doc(hidden)] pub mod init; #[doc(hidden)] @@ -133,7 +132,7 @@ pub mod reseed; pub mod root; pub mod spawn; pub mod terminal; -#[cfg(target_arch = "x86_64")] +#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] pub mod time; #[cfg(target_arch = "x86_64")] #[doc(hidden)] diff --git a/nvx-rs/src/guest/parse.rs b/nvx-rs/src/guest/parse.rs index 8787789cc..0bc07d3ea 100644 --- a/nvx-rs/src/guest/parse.rs +++ b/nvx-rs/src/guest/parse.rs @@ -12,12 +12,14 @@ //! Those also accept the forms above, and saturate an overflowing value, but the kernel never //! writes them, so the programs parse the kernel's numbers as strictly as their arguments. +#[cfg(any(target_arch = "x86_64", test))] use std::str::FromStr; /// Parses a whole, nonempty decimal number, or a hexadecimal one after a `0x` or `0X` prefix. /// /// Returns `None` for an empty argument, any other character, a decimal number with a leading /// zero, which C read as octal, or a value above `u64::MAX`. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn parse_u64(text: &[u8]) -> Option { let text = std::str::from_utf8(text).ok()?; let (digits, radix) = match text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) { @@ -50,6 +52,7 @@ pub(crate) fn parse_decimal(text: &[u8]) -> Option { /// /// Returns `None` for any other form, including the leading whitespace and `+` sign that C's /// `strtoll` also skipped, or for a value outside `T`, which it saturated. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn parse_integer(text: &[u8]) -> Option { let digits = text.strip_prefix(b"-").unwrap_or(text); if digits.is_empty() || !digits.iter().all(u8::is_ascii_digit) { @@ -78,6 +81,7 @@ pub(crate) fn parse_hexadecimal(text: &[u8]) -> Option { /// Returns `None` for any other form, including the leading whitespace, sign, hexadecimal /// significand, infinity, and NaN that C's `strtod` also read, or for a value too large for a /// finite `f64`, which `strtod` reported as a range error. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn parse_f64(text: &[u8]) -> Option { let text = std::str::from_utf8(text).ok()?; let digits = |part: &str| part.bytes().all(|byte| byte.is_ascii_digit()); diff --git a/nvx-rs/src/guest/reseed/tests.rs b/nvx-rs/src/guest/reseed/tests.rs index bd13cc19f..f680be4a8 100644 --- a/nvx-rs/src/guest/reseed/tests.rs +++ b/nvx-rs/src/guest/reseed/tests.rs @@ -23,6 +23,7 @@ pub(super) fn observe_drop(seed: &Seed) { /// Returns the bytes of each seed that the test's thread dropped since the last call, in /// order, as their drops left them. +#[cfg(target_arch = "x86_64")] pub(crate) fn take_dropped() -> Vec<[u8; Seed::LENGTH]> { DROPPED.take() } diff --git a/nvx-rs/src/guest/root.rs b/nvx-rs/src/guest/root.rs index f65f02ad3..3dbe954eb 100644 --- a/nvx-rs/src/guest/root.rs +++ b/nvx-rs/src/guest/root.rs @@ -184,6 +184,7 @@ use super::sys::{self, Errno, MountFlags}; use crate::gpt::{Expected, GptError, Partition}; use crate::uuid::Uuid; +#[cfg(target_arch = "x86_64")] pub(crate) use self::block::window_directories; pub use self::bootstrap::{Bootstrap, BootstrapStep, Resources, bootstrap}; pub(crate) use self::kernel::Paths; @@ -252,6 +253,25 @@ pub enum Role { Scratch, } +/// Read-only discovery of a fixed role, without preparing or mutating its block device. +pub(crate) enum BlockRoleState { + /// No transport has been registered for the role. + Absent, + /// Its transport exists, but its block child or devtmpfs node has not appeared. + Probing, + /// The unique block child and its devtmpfs node exist. + Ready(PathBuf), +} + +/// Looks up only the two contracted block roles, without mounts, flushes or partition changes. +pub(crate) fn lookup_block_role(role: Role) -> io::Result { + let address = match role { + Role::Distro => crate::protocol::root::DISTRO_MMIO_BASE, + Role::Scratch => crate::protocol::root::SCRATCH_MMIO_BASE, + }; + block::lookup(&Paths::default(), address) +} + impl Role { /// Returns whether the role's device is read-only: the distro's is, and the scratch's is /// writable. @@ -521,6 +541,7 @@ pub(crate) fn assemble_until( /// /// The check is bounded, and reads no file's content: of each directory, it reads the status, /// and at most one entry besides `.` and `..`, and it opens nothing below it. +#[cfg(target_arch = "x86_64")] pub(crate) fn assemble_fresh( assembly: &Assembly, deadline: Option, @@ -545,6 +566,7 @@ pub(crate) fn assemble_fresh( /// /// It needs `CAP_SYS_ADMIN`, and the kernel refuses it while one of the disk's partitions is /// open, as a mounted one is. +#[cfg(target_arch = "x86_64")] pub(crate) fn reread_partitions( distro: Device, timeout: Duration, @@ -567,11 +589,11 @@ enum Contents { /// Whatever they hold, which the root keeps, as [`assemble`] accepts: a cold sandbox's /// scratch keeps its upper across boots. Kept, - /// Nothing: each must be missing or empty, as [`assemble_fresh`] requires. + /// Nothing: each must be missing or empty, as a clone's `assemble_fresh` requires. Fresh, } -/// Implements [`assemble_until`] and [`assemble_fresh`] with `kernel`, and with the kernel's +/// Implements [`assemble_until`] and a clone's `assemble_fresh` with `kernel`, and with the kernel's /// interfaces at `paths`, accepting the upper's `contents`. fn assemble_with( kernel: Box, diff --git a/nvx-rs/src/guest/root/block.rs b/nvx-rs/src/guest/root/block.rs index b78758bb3..1170fd915 100644 --- a/nvx-rs/src/guest/root/block.rs +++ b/nvx-rs/src/guest/root/block.rs @@ -2,9 +2,9 @@ //! MMIO windows name, and the block devices of a GPT disk's partitions, whose table a clone asks //! the kernel to reread. //! -//! The VMM declares each device on the kernel command line, so the kernel names its MMIO window -//! `virtio-mmio.N` in `/proc/iomem`, and creates its block device below the window's device in -//! `/sys/devices/virtio-mmio-cmdline`. +//! x86 discovers command-line devices through `/proc/iomem` and +//! `/sys/devices/virtio-mmio-cmdline`. ARM discovers the fixed root-bus OF `reg` windows through +//! platform devices' `of_node` links. Neither identifies a device role by its `vdX` name. use std::fs::{self, File}; use std::io; @@ -14,17 +14,23 @@ use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; use super::kernel::{Kernel, Paths}; -use super::{Device, DeviceStep, Error, Role}; -use crate::guest::parse::{parse_decimal, parse_hexadecimal}; +use super::{BlockRoleState, Device, DeviceStep, Error, Role}; +use crate::guest::parse::parse_decimal; +#[cfg(not(target_arch = "aarch64"))] +use crate::guest::parse::parse_hexadecimal; use crate::guest::sys; use crate::uuid::Uuid; /// The interval between two polls for a block device that has not appeared yet. pub(super) const POLL_INTERVAL: Duration = Duration::from_millis(50); /// The prefix of the name of a virtio MMIO window in `/proc/iomem`. +#[cfg(not(target_arch = "aarch64"))] const WINDOW_PREFIX: &[u8] = b"virtio-mmio."; /// The directory, below sysfs, of the virtio MMIO devices that the kernel command line declares. +#[cfg(not(target_arch = "aarch64"))] const WINDOW_DEVICES: &str = "devices/virtio-mmio-cmdline"; +#[cfg(target_arch = "aarch64")] +mod platform; /// The byte offset of the EROFS superblock on its device. const EROFS_SUPERBLOCK: u64 = 1024; /// The EROFS superblock's magic number, which it starts with, little-endian. @@ -126,6 +132,7 @@ pub(super) fn prepare( /// block devices of the partitions that the kernel created from the table that the disk held /// before, and creates those of the table that it holds now, and their nodes. It checks nothing /// else of the device, whose checks [`prepare`] makes. Returns the path of its node. +#[cfg(any(target_arch = "x86_64", test))] pub(super) fn reread( kernel: &mut dyn Kernel, paths: &Paths, @@ -236,10 +243,35 @@ fn find(paths: &Paths, mmio: u64) -> io::Result> { Ok(None) } +/// Performs one pure role lookup; the caller owns its retry deadline and error policy. +pub(super) fn lookup(paths: &Paths, mmio: u64) -> io::Result { + let windows = window_directories(paths, mmio)?; + let window = match windows.as_slice() { + [] => return Ok(BlockRoleState::Absent), + [window] => window, + _ => { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "multiple transports claim one block role", + )); + } + }; + let Some(name) = block_device(window)? else { + return Ok(BlockRoleState::Probing); + }; + let node = paths.dev.join(name); + match fs::symlink_metadata(&node) { + Ok(_) => Ok(BlockRoleState::Ready(node)), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(BlockRoleState::Probing), + Err(error) => Err(error), + } +} + /// Returns the sysfs directories of the virtio MMIO windows that the kernel lists at `mmio` in /// `/proc/iomem`, in its order, below which it creates their virtio devices, and the block devices /// and partitions of those. A directory may not exist yet: the kernel lists a window before it adds /// the window's device. +#[cfg(not(target_arch = "aarch64"))] pub(crate) fn window_directories(paths: &Paths, mmio: u64) -> io::Result> { let windows = fs::read(paths.proc.join("iomem"))?; let mut directories = Vec::new(); @@ -264,8 +296,15 @@ pub(crate) fn window_directories(paths: &Paths, mmio: u64) -> io::Result io::Result> { + platform::window_directories(paths, mmio) +} + /// Returns the name of the first block device, in byte order, of the first virtio device of the /// window device `window`, or `None` while there is none. +#[cfg(not(target_arch = "aarch64"))] fn block_device(window: &Path) -> io::Result> { for device in sorted_entries(window)? { if !device.starts_with("virtio") { @@ -281,6 +320,12 @@ fn block_device(window: &Path) -> io::Result> { Ok(None) } +/// Returns the sole block child of the fixed OF-created transport, once it has probed. +#[cfg(target_arch = "aarch64")] +fn block_device(window: &Path) -> io::Result> { + platform::block_device(window) +} + /// Returns the names of the entries of `directory` that are UTF-8, in byte order, or none if it /// does not exist yet. fn sorted_entries(directory: &Path) -> io::Result> { diff --git a/nvx-rs/src/guest/root/block/platform.rs b/nvx-rs/src/guest/root/block/platform.rs new file mode 100644 index 000000000..deed753e5 --- /dev/null +++ b/nvx-rs/src/guest/root/block/platform.rs @@ -0,0 +1,188 @@ +//! ARM fixed virtio-MMIO discovery through real OF properties and platform-driver identity. + +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; + +use super::Paths; + +/// First fixed virtio-MMIO window in the development machine binding. +const BASE: u64 = 0xd000_0000; +/// Size and stride of each fixed transport. +const WINDOW_SIZE: u64 = 0x1000; +/// Slots that have an assigned device role; 4, 5, and 8 remain reserved. +const SLOTS: [u64; 6] = [0, 1, 2, 3, 6, 7]; + +/// Reports a malformed or ambiguous kernel-published fixed-device identity. +fn invalid(message: impl Into) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, message.into()) +} + +/// Reads the explicitly declared cell width of the fixed machine's root bus. +fn cell_count(root: &Path, property: &str) -> io::Result { + let bytes: [u8; 4] = fs::read(root.join(property))? + .try_into() + .map_err(|_| invalid("OF cell count must be exactly one big-endian word"))?; + match u32::from_be_bytes(bytes) { + count @ (1 | 2) => Ok(count as usize), + _ => Err(invalid( + "fixed OF resources need one or two address/size cells", + )), + } +} + +/// Decodes the one root-relative register window, using the parent's big-endian cell widths. +fn resource(bytes: &[u8], address_cells: usize, size_cells: usize) -> io::Result<(u64, u64)> { + if !matches!(address_cells, 1 | 2) + || !matches!(size_cells, 1 | 2) + || bytes.len() != (address_cells + size_cells) * 4 + { + return Err(invalid( + "fixed OF reg must contain exactly one complete address/size tuple", + )); + } + let number = |cells: &[u8]| { + cells + .iter() + .fold(0_u64, |value, &byte| (value << 8) | u64::from(byte)) + }; + let split = address_cells * 4; + let (start, size) = (number(&bytes[..split]), number(&bytes[split..])); + let end = start + .checked_add(size) + .filter(|_| size != 0) + .ok_or_else(|| invalid("OF resource has zero size or overflowing bounds"))?; + Ok((start, end)) +} + +/// Identifies the OF transport even before its platform driver has bound. +fn compatible(device: &Path) -> io::Result { + let bytes = match fs::read(device.join("of_node/compatible")) { + Ok(bytes) => bytes, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false), + Err(error) => return Err(error), + }; + let found = bytes + .split(|&byte| byte == 0) + .any(|item| item == b"virtio,mmio"); + if found && !bytes.ends_with(&[0]) { + return Err(invalid("virtio,mmio OF compatible is not NUL terminated")); + } + Ok(found) +} + +/// Verifies the driver and distinguishes an unbound transport that is still probing. +fn virtio_mmio(device: &Path) -> io::Result { + match fs::read_link(device.join("driver")) { + Ok(driver) if driver.file_name().is_some_and(|name| name == "virtio-mmio") => Ok(true), + Ok(_) => Err(invalid( + "fixed virtio-MMIO resource is bound to another driver", + )), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false), + Err(error) => Err(error), + } +} + +/// Finds exactly one fixed transport, without node-name or enumeration-order assumptions. +pub(super) fn window_directories(paths: &Paths, mmio: u64) -> io::Result> { + if !SLOTS.iter().any(|&slot| BASE + slot * WINDOW_SIZE == mmio) { + return Err(invalid("MMIO address has no assigned fixed-device slot")); + } + let end = mmio + .checked_add(WINDOW_SIZE) + .ok_or_else(|| invalid("fixed MMIO window overflows"))?; + let mut found = Vec::new(); + let root = fs::canonicalize(paths.sys.join("firmware/devicetree/base"))?; + let address_cells = cell_count(&root, "#address-cells")?; + let size_cells = cell_count(&root, "#size-cells")?; + for entry in fs::read_dir(paths.sys.join("bus/platform/devices"))? { + let device = entry?.path(); + let node = match fs::canonicalize(device.join("of_node")) { + Ok(node) => node, + Err(error) if error.kind() == io::ErrorKind::NotFound => { + match fs::read(device.join("modalias")) { + Ok(alias) + if alias.strip_suffix(b"\n").unwrap_or(&alias) + == b"platform:virtio-mmio" => + { + return Err(invalid("ARM virtio-MMIO transport has no OF identity")); + } + Ok(_) => {} + Err(error) if error.kind() == io::ErrorKind::NotFound => {} + Err(error) => return Err(error), + } + continue; + } + Err(error) => return Err(error), + }; + let identity = compatible(&device)?; + let registers = match fs::read(node.join("reg")) { + Ok(registers) => registers, + Err(error) if error.kind() == io::ErrorKind::NotFound && !identity => continue, + Err(error) => return Err(error), + }; + // The fixed binding emits root children, so no bus-range translation is implicit. + if node.parent() != Some(root.as_path()) { + return Err(invalid( + "fixed MMIO discovery does not support non-root OF register translation", + )); + } + let stride = (address_cells + size_cells) * 4; + if registers.is_empty() || !registers.len().is_multiple_of(stride) { + return Err(invalid("OF reg has incomplete address/size tuples")); + } + let mut overlaps = Vec::new(); + for tuple in registers.chunks_exact(stride) { + let (start, last) = resource(tuple, address_cells, size_cells)?; + if start < end && last > mmio { + overlaps.push((start, last)); + } + } + if overlaps.is_empty() { + continue; + } + if registers.len() != stride || overlaps != [(mmio, end)] { + return Err(invalid( + "fixed virtio-MMIO OF resource has incorrect bounds", + )); + } + if !identity { + return Err(invalid( + "fixed MMIO role is occupied by a non-virtio OF device", + )); + } + virtio_mmio(&device)?; + found.push(device); + } + if found.len() > 1 { + return Err(invalid( + "multiple platform devices claim the fixed MMIO resource", + )); + } + Ok(found) +} + +/// Resolves a unique virtio/block child, or waits for its ordinary driver probe. +pub(super) fn block_device(window: &Path) -> io::Result> { + if !virtio_mmio(window)? { + return Ok(None); + } + let virtio: Vec<_> = super::sorted_entries(window)? + .into_iter() + .filter(|name| name.starts_with("virtio")) + .collect(); + if virtio.len() > 1 { + return Err(invalid("fixed transport has multiple virtio children")); + } + let Some(child) = virtio.first() else { + return Ok(None); + }; + let blocks = super::sorted_entries(&window.join(child).join("block"))?; + if blocks.len() > 1 { + return Err(invalid("fixed transport has multiple block children")); + } + Ok(blocks.into_iter().next()) +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/guest/root/block/platform/tests.rs b/nvx-rs/src/guest/root/block/platform/tests.rs new file mode 100644 index 000000000..293ffc6a5 --- /dev/null +++ b/nvx-rs/src/guest/root/block/platform/tests.rs @@ -0,0 +1,217 @@ +use super::*; +use std::os::unix::fs::symlink; + +#[test] +fn of_reg_uses_the_declared_big_endian_cells_and_exact_checked_bounds() { + let wide = reg(0xd0003000, 0x1000); + assert_eq!(resource(&wide, 2, 2).unwrap(), (0xd0003000, 0xd0004000)); + let narrow: Vec<_> = [0xd0003000_u32, 0x1000] + .into_iter() + .flat_map(u32::to_be_bytes) + .collect(); + assert_eq!(resource(&narrow, 1, 1).unwrap(), (0xd0003000, 0xd0004000)); + for (bytes, address, size) in [ + (vec![], 2, 2), + (wide[..15].to_vec(), 2, 2), + (wide.repeat(2), 2, 2), + (wide.clone(), 0, 2), + (wide, 3, 1), + (reg(0xd0003000, 0), 2, 2), + (reg(u64::MAX, 1), 2, 2), + ] { + assert!(resource(&bytes, address, size).is_err()); + } +} + +fn reg(start: u64, size: u64) -> Vec { + [start, size] + .into_iter() + .flat_map(u64::to_be_bytes) + .collect() +} + +#[test] +fn reserved_and_unknown_slots_fail_before_sysfs_access() { + let paths = Paths::default(); + for address in [0xd0004000, 0xd0005000, 0xd0008000, 0xd0003001, u64::MAX] { + assert_eq!( + window_directories(&paths, address).unwrap_err().kind(), + io::ErrorKind::InvalidData + ); + } +} + +#[test] +fn multiple_virtio_or_block_children_are_not_selected_by_order() { + let root = tempfile::tempdir().unwrap(); + let window = root.path(); + assert!(block_device(window).unwrap().is_none()); + bind_transport(window); + fs::create_dir_all(window.join("virtio9/block/vdz")).unwrap(); + assert_eq!(block_device(window).unwrap().as_deref(), Some("vdz")); + fs::create_dir_all(window.join("virtio9/block/vda")).unwrap(); + assert!(block_device(window).is_err()); + fs::remove_dir(window.join("virtio9/block/vda")).unwrap(); + fs::create_dir_all(window.join("virtio1")).unwrap(); + assert!(block_device(window).is_err()); +} + +fn paths(root: &Path) -> Paths { + Paths { + proc: root.join("proc"), + sys: root.join("sys"), + dev: root.join("dev"), + } +} + +fn bind_transport(device: &Path) { + symlink( + "/sys/bus/platform/drivers/virtio-mmio", + device.join("driver"), + ) + .unwrap(); +} + +fn transport(paths: &Paths, name: &str, start: u64, size: u64) -> PathBuf { + let root = paths.sys.join("firmware/devicetree/base"); + let node = root.join(format!("unrelated-node-name-{name}")); + fs::create_dir_all(&node).unwrap(); + fs::write(root.join("#address-cells"), 2_u32.to_be_bytes()).unwrap(); + fs::write(root.join("#size-cells"), 2_u32.to_be_bytes()).unwrap(); + fs::write(node.join("compatible"), b"virtio,mmio\0").unwrap(); + fs::write(node.join("reg"), reg(start, size)).unwrap(); + let directory = paths.sys.join("bus/platform/devices").join(name); + fs::create_dir_all(&directory).unwrap(); + symlink(node, directory.join("of_node")).unwrap(); + directory +} + +#[test] +fn actual_of_links_find_roles_without_a_platform_resource_file_or_node_name() { + let root = tempfile::tempdir().unwrap(); + let paths = paths(root.path()); + let directory = transport(&paths, "arbitrary-name", 0xd0003000, 0x1000); + assert!(!directory.join("resource").exists()); + bind_transport(&directory); + fs::create_dir_all(directory.join("virtio9/block/vdz")).unwrap(); + assert_eq!( + window_directories(&paths, 0xd0003000).unwrap(), + vec![directory.clone()] + ); + assert_eq!(block_device(&directory).unwrap().as_deref(), Some("vdz")); +} + +#[test] +fn pure_role_lookup_distinguishes_absent_unbound_pending_and_ready_without_mutation() { + use crate::guest::root::{BlockRoleState, block::lookup}; + let root = tempfile::tempdir().unwrap(); + let paths = paths(root.path()); + let distro = transport(&paths, "shuffled", 0xd0003000, 0x1000); + assert!(matches!( + lookup(&paths, 0xd0006000).unwrap(), + BlockRoleState::Absent + )); + let scratch = transport(&paths, "not-vdb", 0xd0006000, 0x1000); + assert!(matches!( + lookup(&paths, 0xd0006000).unwrap(), + BlockRoleState::Probing + )); + assert!(!scratch.join("driver").exists()); + bind_transport(&distro); + bind_transport(&scratch); + assert!(matches!( + lookup(&paths, 0xd0006000).unwrap(), + BlockRoleState::Probing + )); + fs::create_dir_all(distro.join("virtio9/block/vdz")).unwrap(); + fs::create_dir_all(&paths.dev).unwrap(); + fs::write(paths.dev.join("vdz"), b"fixture devtmpfs node").unwrap(); + assert!( + matches!(lookup(&paths, 0xd0003000).unwrap(), BlockRoleState::Ready(path) if path == paths.dev.join("vdz")) + ); + fs::create_dir_all(scratch.join("virtio2/block/vdc")).unwrap(); + assert!(matches!( + lookup(&paths, 0xd0006000).unwrap(), + BlockRoleState::Probing + )); + fs::write(paths.dev.join("vdc"), b"fixture devtmpfs node").unwrap(); + assert!( + matches!(lookup(&paths, 0xd0006000).unwrap(), BlockRoleState::Ready(path) if path == paths.dev.join("vdc")) + ); + fs::write(scratch.join("of_node/compatible"), b"not,a-virtio-device\0").unwrap(); + assert!(lookup(&paths, 0xd0006000).is_err()); + fs::write(scratch.join("of_node/compatible"), b"virtio,mmio\0").unwrap(); + fs::remove_file(scratch.join("driver")).unwrap(); + symlink("/sys/bus/platform/drivers/wrong", scratch.join("driver")).unwrap(); + assert!(lookup(&paths, 0xd0006000).is_err()); +} + +#[test] +fn repeated_or_overlapping_resources_and_wrong_driver_fail_closed() { + let root = tempfile::tempdir().unwrap(); + let paths = paths(root.path()); + let first = transport(&paths, "first", 0xd0003000, 0x1000); + let second = transport(&paths, "second", 0xd0003000, 0x1000); + assert!(window_directories(&paths, 0xd0003000).is_err()); + fs::remove_dir_all(second).unwrap(); + for bad in [ + reg(0xd0003000, 0xfff), + reg(0xd0003000, 0x1001), + reg(0xd0002fff, 2), + ] { + fs::write(first.join("of_node/reg"), bad).unwrap(); + assert!(window_directories(&paths, 0xd0003000).is_err()); + } + fs::write(first.join("of_node/reg"), reg(0xd0003000, 0x1000).repeat(2)).unwrap(); + assert!(window_directories(&paths, 0xd0003000).is_err()); + fs::write(first.join("of_node/reg"), reg(0xd0003000, 0x1000)).unwrap(); + symlink("/sys/bus/platform/drivers/not-virtio", first.join("driver")).unwrap(); + assert!(window_directories(&paths, 0xd0003000).is_err()); + fs::remove_file(first.join("driver")).unwrap(); + symlink( + "/sys/bus/platform/drivers/virtio-mmio", + first.join("driver"), + ) + .unwrap(); + assert_eq!(window_directories(&paths, 0xd0003000).unwrap(), vec![first]); +} + +#[test] +fn malformed_cells_compatible_and_translated_bus_nodes_are_explicit_errors() { + let root = tempfile::tempdir().unwrap(); + let paths = paths(root.path()); + let device = transport(&paths, "transport", 0xd0003000, 0x1000); + let base = paths.sys.join("firmware/devicetree/base"); + for cells in [ + vec![], + vec![2], + 0_u32.to_be_bytes().to_vec(), + 3_u32.to_be_bytes().to_vec(), + ] { + fs::write(base.join("#address-cells"), cells).unwrap(); + assert!(window_directories(&paths, 0xd0003000).is_err()); + } + + fs::write(base.join("#address-cells"), 2_u32.to_be_bytes()).unwrap(); + fs::write(device.join("of_node/compatible"), b"virtio,mmio").unwrap(); + assert!(window_directories(&paths, 0xd0003000).is_err()); + fs::write(device.join("of_node/compatible"), b"virtio,mmio\0").unwrap(); + let nested = base.join("translated-bus/node"); + fs::create_dir_all(&nested).unwrap(); + fs::write(nested.join("compatible"), b"virtio,mmio\0").unwrap(); + fs::write(nested.join("reg"), reg(0xd0003000, 0x1000)).unwrap(); + fs::remove_file(device.join("of_node")).unwrap(); + symlink(nested, device.join("of_node")).unwrap(); + assert!(window_directories(&paths, 0xd0003000).is_err()); +} + +#[test] +fn a_non_of_virtio_transport_is_not_reported_as_an_absent_role() { + let root = tempfile::tempdir().unwrap(); + let paths = paths(root.path()); + transport(&paths, "distro", 0xd0003000, 0x1000); + let cmdline = paths.sys.join("bus/platform/devices/command-line-device"); + fs::create_dir_all(&cmdline).unwrap(); + fs::write(cmdline.join("modalias"), b"platform:virtio-mmio\n").unwrap(); + assert!(window_directories(&paths, 0xd0006000).is_err()); +} diff --git a/nvx-rs/src/guest/root/kernel.rs b/nvx-rs/src/guest/root/kernel.rs index 32325890d..5ba3b113f 100644 --- a/nvx-rs/src/guest/root/kernel.rs +++ b/nvx-rs/src/guest/root/kernel.rs @@ -60,6 +60,7 @@ pub(super) trait Kernel: MappingKernel + Send + Sync { /// Asks the kernel to reread the partition table of the disk whose whole-disk block device is /// `device`, as [`sys::reread_partition_table`] does. + #[cfg(any(target_arch = "x86_64", test))] fn reread_partitions(&mut self, device: &CStr) -> Result<(), Errno>; /// Reads and checks the partition table of the GPT disk that `file` opens, the whole-disk @@ -126,6 +127,7 @@ impl Kernel for SystemKernel { descriptor.close() } + #[cfg(any(target_arch = "x86_64", test))] fn reread_partitions(&mut self, device: &CStr) -> Result<(), Errno> { let descriptor = sys::open( device, diff --git a/nvx-rs/src/guest/root/tests.rs b/nvx-rs/src/guest/root/tests.rs index 0d89efd1a..b1cabba83 100644 --- a/nvx-rs/src/guest/root/tests.rs +++ b/nvx-rs/src/guest/root/tests.rs @@ -18,9 +18,15 @@ use crate::guest::mappings::{Access, Mapping, SourceRoot}; use crate::guest::sys::MountAttributes; /// The base address of the distro device's MMIO window. +#[cfg(not(target_arch = "aarch64"))] const DISTRO: u64 = 0xd000_0000; +#[cfg(target_arch = "aarch64")] +const DISTRO: u64 = crate::protocol::root::DISTRO_MMIO_BASE; /// The base address of the scratch device's MMIO window. +#[cfg(not(target_arch = "aarch64"))] const SCRATCH: u64 = 0x1_d000_1000; +#[cfg(target_arch = "aarch64")] +const SCRATCH: u64 = crate::protocol::root::SCRATCH_MMIO_BASE; /// The UUID of the test's EROFS image. const UUID: Uuid = Uuid::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); /// The GUID of the tests' GPT disks. @@ -82,6 +88,28 @@ impl Guest { SCRATCH + 0x1ff, ), ); + #[cfg(target_arch = "aarch64")] + for (index, address) in [(0, DISTRO), (1, SCRATCH)] { + let tree = paths.sys.join("firmware/devicetree/base"); + let node = tree.join(format!("fixed-role-{index}")); + fs::create_dir_all(&node).unwrap(); + fs::write(tree.join("#address-cells"), 2_u32.to_be_bytes()).unwrap(); + fs::write(tree.join("#size-cells"), 2_u32.to_be_bytes()).unwrap(); + fs::write(node.join("compatible"), b"virtio,mmio\0").unwrap(); + fs::write( + node.join("reg"), + [address, 0x1000] + .into_iter() + .flat_map(u64::to_be_bytes) + .collect::>(), + ) + .unwrap(); + let window = paths + .sys + .join(format!("bus/platform/devices/fixed-role-{index}")); + fs::create_dir_all(&window).unwrap(); + std::os::unix::fs::symlink(node, window.join("of_node")).unwrap(); + } // The defaults of the overlay's options, as the guest's kernel sets them: all off. for parameter in [ "metacopy", @@ -257,7 +285,18 @@ fn attach(root: &Path, index: usize, name: &str, sectors: u64) { /// window `index` of the guest in `root`, without its node. The device is read-only at window 0, /// where the tests attach the distro device, and writable elsewhere, as at the scratch's window 1. fn probe(root: &Path, index: usize, name: &str, sectors: u64) { + #[cfg(not(target_arch = "aarch64"))] let window = format!("sys/devices/virtio-mmio-cmdline/virtio-mmio.{index}"); + #[cfg(target_arch = "aarch64")] + let window = format!("sys/bus/platform/devices/fixed-role-{index}"); + #[cfg(target_arch = "aarch64")] + if !root.join(&window).join("driver").is_symlink() { + std::os::unix::fs::symlink( + "/sys/bus/platform/drivers/virtio-mmio", + root.join(&window).join("driver"), + ) + .unwrap(); + } fs::create_dir_all( root.join(window) .join(format!("virtio{index}/block/{name}")), @@ -549,7 +588,7 @@ fn devices_are_found_when_they_appear_and_missing_ones_time_out() { assert_eq!(guest.kernel.sleeps(), 2); assert_eq!( error.to_string(), - "the scratch device at MMIO 0x1d0001000 did not appear" + format!("the scratch device at MMIO {SCRATCH:#x} did not appear") ); // The assembly checks the scratch device before it mounts anything. assert!(guest.kernel.mounts().is_empty()); diff --git a/nvx-rs/src/guest/status.rs b/nvx-rs/src/guest/status.rs index db28aa3bc..00db0e9a7 100644 --- a/nvx-rs/src/guest/status.rs +++ b/nvx-rs/src/guest/status.rs @@ -21,12 +21,15 @@ pub(crate) enum ExitStatus { Usage = 2, /// A benchmark could not open, connect, size, or time its device, or a diagnostic could not /// prepare or run its measurement. + #[cfg(any(target_arch = "x86_64", test))] SetupFailure = 3, /// A benchmark transfer or send was incomplete. + #[cfg(any(target_arch = "x86_64", test))] TransferFailure = 4, /// The managed agent, or its execution helper before it executed the workload, failed. AgentFailure = 125, /// A benchmark could not write its result. + #[cfg(any(target_arch = "x86_64", test))] OutputFailure = 255, } @@ -39,10 +42,13 @@ pub(crate) enum CommandError { Operation, /// A benchmark could not open, connect, size, or time its device, or a diagnostic could not /// prepare or run its measurement. + #[cfg(any(target_arch = "x86_64", test))] Setup, /// A benchmark transfer or send was incomplete. + #[cfg(any(target_arch = "x86_64", test))] Transfer, /// A benchmark could not write its result. + #[cfg(any(target_arch = "x86_64", test))] Output, /// The managed agent or its execution helper failed for any reason, including an invalid /// command line. It has reported why where its C program did. @@ -55,8 +61,11 @@ impl From> for ExitStatus { Ok(()) => Self::Success, Err(CommandError::Usage) => Self::Usage, Err(CommandError::Operation) => Self::Failure, + #[cfg(any(target_arch = "x86_64", test))] Err(CommandError::Setup) => Self::SetupFailure, + #[cfg(any(target_arch = "x86_64", test))] Err(CommandError::Transfer) => Self::TransferFailure, + #[cfg(any(target_arch = "x86_64", test))] Err(CommandError::Output) => Self::OutputFailure, Err(CommandError::Agent) => Self::AgentFailure, } diff --git a/nvx-rs/src/guest/sys/aarch64.rs b/nvx-rs/src/guest/sys/aarch64.rs new file mode 100644 index 000000000..55b741cb5 --- /dev/null +++ b/nvx-rs/src/guest/sys/aarch64.rs @@ -0,0 +1,40 @@ +//! Architected EL0 counter reads, not CPU-profile or backend qualification. + +use super::Errno; + +/// Reads the architectural 32-bit counter frequency without substituting a host observation. +pub(crate) fn counter_frequency_hz() -> Result { + let frequency: u64; + // SAFETY: Linux permits this architected EL0 read or emulates it through the timer driver. + unsafe { + core::arch::asm!( + "isb", + "mrs {frequency}, cntfrq_el0", + frequency = out(reg) frequency, + options(nostack, preserves_flags), + ); + } + if frequency == 0 || frequency > u64::from(u32::MAX) { + return Err(Errno::EPROTO); + } + Ok(frequency) +} + +/// Reads the virtual counter between instruction barriers that also order compiler accesses. +pub(crate) fn virtual_counter() -> u64 { + let counter: u64; + // SAFETY: Linux permits this architected EL0 read or emulates it through the timer driver. + unsafe { + core::arch::asm!( + "isb", + "mrs {counter}, cntvct_el0", + "isb", + counter = out(reg) counter, + options(nostack, preserves_flags), + ); + } + counter +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/guest/sys/aarch64/tests.rs b/nvx-rs/src/guest/sys/aarch64/tests.rs new file mode 100644 index 000000000..f56a064ed --- /dev/null +++ b/nvx-rs/src/guest/sys/aarch64/tests.rs @@ -0,0 +1,13 @@ +use super::*; + +#[test] +fn linux_el0_counter_reads_do_not_invent_a_frequency() { + let frequency = counter_frequency_hz().unwrap(); + assert!(frequency > 0 && frequency <= u64::from(u32::MAX)); + let mut previous = virtual_counter(); + for _ in 0..32 { + let next = virtual_counter(); + assert!(next >= previous); + previous = next; + } +} diff --git a/nvx-rs/src/guest/sys/clock.rs b/nvx-rs/src/guest/sys/clock.rs index a8a24bb47..81ed997bf 100644 --- a/nvx-rs/src/guest/sys/clock.rs +++ b/nvx-rs/src/guest/sys/clock.rs @@ -1,9 +1,12 @@ //! The kernel's clocks: reading them and sleeping on them. +#[cfg(any(target_arch = "x86_64", test))] use std::time::Duration; -use nix::sys::time::{TimeSpec, TimeValLike}; -use nix::time::{ClockId, ClockNanosleepFlags, clock_gettime, clock_nanosleep}; +use nix::sys::time::TimeSpec; +use nix::sys::time::TimeValLike; +use nix::time::{ClockId, clock_gettime}; +use nix::time::{ClockNanosleepFlags, clock_nanosleep}; use super::Errno; @@ -21,9 +24,11 @@ pub(crate) enum Clock { /// The time since boot, without suspend, which the wall clock's discipline slews. Monotonic, /// The time since boot, without suspend or discipline. + #[cfg(any(target_arch = "x86_64", test))] MonotonicRaw, /// The monotonic clock as the kernel saved it at the last scheduler tick, which a read /// returns without reading the clocksource, so that it advances once per tick. + #[cfg(any(target_arch = "x86_64", test))] MonotonicCoarse, /// The time since boot, with suspend. Boottime, @@ -37,7 +42,9 @@ impl Clock { match self { Self::Realtime => ClockId::CLOCK_REALTIME, Self::Monotonic => ClockId::CLOCK_MONOTONIC, + #[cfg(any(target_arch = "x86_64", test))] Self::MonotonicRaw => ClockId::CLOCK_MONOTONIC_RAW, + #[cfg(any(target_arch = "x86_64", test))] Self::MonotonicCoarse => ClockId::CLOCK_MONOTONIC_COARSE, Self::Boottime => ClockId::CLOCK_BOOTTIME, Self::ProcessCpuTime => ClockId::CLOCK_PROCESS_CPUTIME_ID, @@ -85,6 +92,7 @@ pub(crate) fn sleep_until_monotonic(deadline_ns: i64) { } /// Sleeps for `duration` on the monotonic clock with one call, which a signal ends early. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn sleep_once(duration: Duration) { let _ = clock_nanosleep( ClockId::CLOCK_MONOTONIC, diff --git a/nvx-rs/src/guest/sys/discipline.rs b/nvx-rs/src/guest/sys/discipline.rs index b743cdf8d..38a7b540a 100644 --- a/nvx-rs/src/guest/sys/discipline.rs +++ b/nvx-rs/src/guest/sys/discipline.rs @@ -30,6 +30,7 @@ pub(crate) enum ClockAdjustment { Frequency(FrequencyHold), /// Clears the phase offset through the phase-locked loop in nanosecond mode, without changing /// its status or held frequency correction. + #[cfg(any(target_arch = "x86_64", test))] ClearPhase, /// Slews the wall clock by `offset_ns` through the phase-locked loop in nanosecond mode, with /// its frequency held, and sets its time constant, the frequency correction, and the error @@ -66,6 +67,7 @@ impl ClockAdjustment { Self::Query => 0, Self::Step { .. } => libc::ADJ_SETOFFSET | libc::ADJ_NANO, Self::Frequency(_) => HOLD, + #[cfg(any(target_arch = "x86_64", test))] Self::ClearPhase => libc::ADJ_OFFSET | libc::ADJ_NANO, Self::Slew { .. } => HOLD | libc::ADJ_OFFSET | libc::ADJ_TIMECONST | libc::ADJ_NANO, } @@ -74,7 +76,9 @@ impl ClockAdjustment { /// Returns the discipline status that the adjustment sets, or zero for one that sets none. pub(crate) fn status(self) -> i32 { match self { - Self::Query | Self::Step { .. } | Self::ClearPhase => 0, + Self::Query | Self::Step { .. } => 0, + #[cfg(any(target_arch = "x86_64", test))] + Self::ClearPhase => 0, Self::Frequency(_) | Self::Slew { .. } => PLL_NANOSECONDS, } } @@ -116,6 +120,7 @@ pub(crate) fn adjust_clock(adjustment: ClockAdjustment) -> Result hold.apply(&mut value), + #[cfg(any(target_arch = "x86_64", test))] ClockAdjustment::ClearPhase => {} ClockAdjustment::Slew { offset_ns, diff --git a/nvx-rs/src/guest/sys/fd.rs b/nvx-rs/src/guest/sys/fd.rs index 37bfafe47..eb9fc6999 100644 --- a/nvx-rs/src/guest/sys/fd.rs +++ b/nvx-rs/src/guest/sys/fd.rs @@ -6,6 +6,7 @@ use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd}; pub(crate) use nix::fcntl::OFlag as OpenFlags; use nix::fcntl::{FcntlArg, FdFlag, Flock, FlockArg, SealFlag, fcntl}; +#[cfg(any(target_arch = "x86_64", test))] use nix::poll::{PollFd, PollFlags}; use nix::sys::memfd::{MFdFlags, memfd_create}; use nix::sys::socket::{AddressFamily, Shutdown, SockFlag, SockType, shutdown, socketpair}; @@ -96,6 +97,7 @@ impl FileDescriptor { } /// Sets the file's size to `length` bytes, preserving the C cast from `u64` to `off_t`. + #[cfg(any(target_arch = "x86_64", test))] pub(crate) fn truncate(&self, length: u64) -> Result<(), Errno> { nix::unistd::ftruncate(&self.0, length as _) } @@ -126,6 +128,7 @@ impl FileDescriptor { /// Waits at most `timeout_ms` milliseconds for data to read, with one `poll` call. /// /// Returns whether `poll` reported `POLLIN`. An interrupted call is an error, not a retry. + #[cfg(any(target_arch = "x86_64", test))] pub(crate) fn wait_readable(&self, timeout_ms: u16) -> Result { let mut descriptors = [PollFd::new(self.0.as_fd(), PollFlags::POLLIN)]; let ready = nix::poll::poll(&mut descriptors, timeout_ms)?; @@ -333,6 +336,16 @@ pub(crate) fn lock_file(path: &CStr, mode: Mode) -> Result { } } +/// Takes a file's exclusive lock without waiting, creating it privately when missing. +pub(crate) fn try_lock_file(path: &CStr, mode: Mode) -> Result { + let flags = + OpenFlags::O_RDWR | OpenFlags::O_CREAT | OpenFlags::O_CLOEXEC | OpenFlags::O_NOFOLLOW; + let descriptor = nix::fcntl::open(path, flags, mode)?; + Flock::lock(descriptor, FlockArg::LockExclusiveNonblock) + .map(|lock| FileLock { _lock: lock }) + .map_err(|(_, error)| error) +} + /// Opens the directory `path` and takes its exclusive lock without waiting, or fails with /// `EWOULDBLOCK` while another open file description holds a lock on it, in this process or /// another. @@ -345,7 +358,7 @@ pub(crate) fn try_lock_directory(path: &CStr) -> Result { } /// A file that this process creates, which dropping the owner closes and removes unless the -/// process [keeps](Self::keep) or [renames](Self::rename) it first. +/// process keeps it (the x86 restore recorder) or [renames](Self::rename) it first. #[derive(Debug)] pub(crate) struct CreatedFile<'a> { /// The file's path, until the process keeps or renames the file. @@ -386,6 +399,7 @@ impl<'a> CreatedFile<'a> { } /// Closes the file and keeps it, even when closing fails, which it reports. + #[cfg(target_arch = "x86_64")] pub(crate) fn keep(mut self) -> Result<(), Errno> { self.path = None; self.descriptor.take().map_or(Ok(()), FileDescriptor::close) diff --git a/nvx-rs/src/guest/sys/fd/tests.rs b/nvx-rs/src/guest/sys/fd/tests.rs index 2ab8c56c6..42d36b176 100644 --- a/nvx-rs/src/guest/sys/fd/tests.rs +++ b/nvx-rs/src/guest/sys/fd/tests.rs @@ -1,5 +1,22 @@ use super::*; +#[test] +fn daemon_file_ownership_does_not_reserve_the_root_runtime_directory() { + let directory = tempfile::tempdir().unwrap(); + let path = crate::guest::sys::path_c_string(directory.path()).unwrap(); + let _root = try_lock_directory(&path).unwrap(); + let path = + crate::guest::sys::path_c_string(&directory.path().join("time-daemon.lock")).unwrap(); + let mode = Mode::S_IRUSR | Mode::S_IWUSR; + let daemon = try_lock_file(&path, mode).unwrap(); + assert!(matches!( + try_lock_file(&path, mode), + Err(Errno::EWOULDBLOCK) + )); + drop(daemon); + assert!(try_lock_file(&path, mode).is_ok()); +} + /// Sets the soft limit of the process's open descriptors to `limit`, and returns the limit that /// it replaced, for a test that runs out of descriptors in a process of its own. /// diff --git a/nvx-rs/src/guest/sys/filesystem.rs b/nvx-rs/src/guest/sys/filesystem.rs index b1d1f4715..eac60c245 100644 --- a/nvx-rs/src/guest/sys/filesystem.rs +++ b/nvx-rs/src/guest/sys/filesystem.rs @@ -11,7 +11,8 @@ use std::str::FromStr; pub(crate) use nix::mount::MsFlags as MountFlags; use nix::sched::CloneFlags; use nix::sys::stat::{FchmodatFlags, Mode}; -use nix::unistd::{AccessFlags, Gid, Uid}; +use nix::unistd::AccessFlags; +use nix::unistd::{Gid, Uid}; use super::{Errno, OpenFlags, errno_of}; use crate::guest::parse::parse_decimal; @@ -154,8 +155,10 @@ pub(crate) fn enter_private_mount_namespace() -> Result<(), Errno> { /// It also removes a directory that existed already, such as one that an interrupted earlier /// owner left behind, which the owner then reuses. #[derive(Debug)] +#[cfg(target_arch = "x86_64")] pub(crate) struct ScratchDirectory<'a>(&'a CStr); +#[cfg(target_arch = "x86_64")] impl<'a> ScratchDirectory<'a> { /// Creates `path` as a directory with `mode` unless it exists already, ignoring other /// failures as the C program did, because a later use of the directory reports them. @@ -170,6 +173,7 @@ impl<'a> ScratchDirectory<'a> { } } +#[cfg(target_arch = "x86_64")] impl Drop for ScratchDirectory<'_> { /// Removes the directory, which must be empty, best effort. fn drop(&mut self) { @@ -184,8 +188,10 @@ impl Drop for ScratchDirectory<'_> { /// A tmpfs mount, which dropping the owner unmounts, best effort, unless /// [`unmount`](Self::unmount) did. #[derive(Debug)] +#[cfg(any(target_arch = "x86_64", test))] pub(crate) struct TmpfsMount<'a>(Option<&'a CStr>); +#[cfg(any(target_arch = "x86_64", test))] impl<'a> TmpfsMount<'a> { /// Mounts a tmpfs named `source` on `target`, without set-user-ID files, device files, or /// executables, and with the filesystem's mount options `options`. @@ -206,6 +212,7 @@ impl<'a> TmpfsMount<'a> { } } +#[cfg(any(target_arch = "x86_64", test))] impl Drop for TmpfsMount<'_> { /// Unmounts the filesystem, best effort, unless [`unmount`](Self::unmount) did. fn drop(&mut self) { diff --git a/nvx-rs/src/guest/sys/ioctl.rs b/nvx-rs/src/guest/sys/ioctl.rs index fa4c76dd9..0f419805a 100644 --- a/nvx-rs/src/guest/sys/ioctl.rs +++ b/nvx-rs/src/guest/sys/ioctl.rs @@ -130,6 +130,7 @@ pub(crate) fn flush_block_buffers(descriptor: &FileDescriptor) -> Result<(), Err /// returns. It needs `CAP_SYS_ADMIN`, and fails with `EBUSY` while one of the disk's partitions is /// open, as a mounted one is, and with `EINVAL` on a partition, or on a disk that the kernel does /// not partition. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn reread_partition_table(descriptor: &FileDescriptor) -> Result<(), Errno> { // SAFETY: `BLKRRPART` reads no argument. unsafe { blkrrpart(descriptor.raw()) }?; diff --git a/nvx-rs/src/guest/sys/kernel.rs b/nvx-rs/src/guest/sys/kernel.rs index 8ad3b9efa..1298fb3df 100644 --- a/nvx-rs/src/guest/sys/kernel.rs +++ b/nvx-rs/src/guest/sys/kernel.rs @@ -14,24 +14,30 @@ pub(crate) const KERNEL_LOG_DEVICE: &CStr = c"/dev/kmsg"; /// The kernel's command line. pub(crate) const COMMAND_LINE: &CStr = c"/proc/cmdline"; /// The kernel's description of each processor. +#[cfg(target_arch = "x86_64")] pub(crate) const CPU_INFORMATION: &CStr = c"/proc/cpuinfo"; /// The kernel's timers, clock-event devices, and tick devices. pub(crate) const TIMER_LIST: &CStr = c"/proc/timer_list"; /// The CPUs that are online, as a CPU list. pub(crate) const ONLINE_CPUS: &CStr = c"/sys/devices/system/cpu/online"; /// The CPUs that could ever be online, as a CPU list. +#[cfg(target_arch = "x86_64")] pub(crate) const POSSIBLE_CPUS: &CStr = c"/sys/devices/system/cpu/possible"; /// The TSC frequency in kHz that the kernel determined, when it exposes it. +#[cfg(target_arch = "x86_64")] pub(crate) const TSC_FREQUENCY_KHZ: &CStr = c"/sys/devices/system/cpu/cpu0/tsc_freq_khz"; /// The first frequency-scaling policy, present when the kernel scales CPU frequency. +#[cfg(target_arch = "x86_64")] pub(crate) const CPU_FREQUENCY_POLICY: &CStr = c"/sys/devices/system/cpu/cpufreq/policy0"; /// The clocksource that timekeeping uses. pub(crate) const CURRENT_CLOCKSOURCE: &CStr = c"/sys/devices/system/clocksource/clocksource0/current_clocksource"; /// The clocksources that timekeeping could use. +#[cfg(target_arch = "x86_64")] pub(crate) const AVAILABLE_CLOCKSOURCES: &CStr = c"/sys/devices/system/clocksource/clocksource0/available_clocksource"; /// The VMBus bus, present when the kernel found Hyper-V's VMBus. +#[cfg(target_arch = "x86_64")] pub(crate) const VMBUS: &CStr = c"/sys/bus/vmbus"; /// Whether RCU suppresses its CPU stall warnings. pub(crate) const RCU_STALL_SUPPRESS: &CStr = @@ -40,16 +46,20 @@ pub(crate) const RCU_STALL_SUPPRESS: &CStr = pub(crate) const RCU_STALL_TIMEOUT: &CStr = c"/sys/module/rcupdate/parameters/rcu_cpu_stall_timeout"; /// Whether RCU uses normal rather than expedited grace periods. +#[cfg(target_arch = "x86_64")] pub(crate) const RCU_NORMAL: &CStr = c"/sys/kernel/rcu_normal"; /// How many RCU CPU stalls the kernel has reported. pub(crate) const RCU_STALL_COUNT: &CStr = c"/sys/kernel/rcu_stall_count"; /// Whether the soft-lockup and hard-lockup detectors run. +#[cfg(target_arch = "x86_64")] pub(crate) const WATCHDOG: &CStr = c"/proc/sys/kernel/watchdog"; /// Whether the soft-lockup detector runs. pub(crate) const SOFT_WATCHDOG: &CStr = c"/proc/sys/kernel/soft_watchdog"; /// Whether the hard-lockup detector runs. +#[cfg(target_arch = "x86_64")] pub(crate) const NMI_WATCHDOG: &CStr = c"/proc/sys/kernel/nmi_watchdog"; /// Seconds after which the lockup detectors report a lockup. +#[cfg(target_arch = "x86_64")] pub(crate) const WATCHDOG_THRESHOLD: &CStr = c"/proc/sys/kernel/watchdog_thresh"; /// Seconds after which the kernel reports a task that has not run as hung. pub(crate) const HUNG_TASK_TIMEOUT: &CStr = c"/proc/sys/kernel/hung_task_timeout_secs"; @@ -61,11 +71,14 @@ pub(crate) const NULL_DEVICE: &CStr = c"/dev/null"; pub(crate) const CONSOLE_DEVICE: &CStr = c"/dev/console"; /// `syslog(2)`'s `SYSLOG_ACTION_READ_ALL`, which copies the whole log without consuming it. +#[cfg(any(target_arch = "x86_64", test))] const SYSLOG_ACTION_READ_ALL: c_int = 3; /// `syslog(2)`'s `SYSLOG_ACTION_SIZE_BUFFER`, which returns the log buffer's size. +#[cfg(any(target_arch = "x86_64", test))] const SYSLOG_ACTION_SIZE_BUFFER: c_int = 10; /// Returns the size in bytes of the kernel's log buffer. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn kernel_log_size() -> Result { // SAFETY: `SYSLOG_ACTION_SIZE_BUFFER` reads neither the null buffer nor its length. let size = Errno::result(unsafe { @@ -76,6 +89,7 @@ pub(crate) fn kernel_log_size() -> Result { /// Copies as much of the kernel's log as fits into `buffer`, and returns the number of bytes /// copied, without consuming the log. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn read_kernel_log(buffer: &mut [u8]) -> Result { let length = c_int::try_from(buffer.len()).unwrap_or(c_int::MAX); // SAFETY: the kernel writes at most `length` bytes, which `buffer` holds, during the call. @@ -87,6 +101,7 @@ pub(crate) fn read_kernel_log(buffer: &mut [u8]) -> Result { /// Returns whether the kernel mapped a vDSO into this process, which its auxiliary vector /// reports. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn has_vdso() -> bool { // SAFETY: `getauxval` reads the process's immutable auxiliary vector. unsafe { nix::libc::getauxval(nix::libc::AT_SYSINFO_EHDR) != 0 } @@ -98,7 +113,6 @@ pub(crate) fn has_vdso() -> bool { /// Linux applies `setpriority`'s `PRIO_PROCESS` to the one thread whose ID it names: for a /// process ID, the process's first thread, whose later threads and children start with its nice /// value. Lowering a nice value needs `CAP_SYS_NICE`. -#[cfg(target_arch = "x86_64")] pub(crate) fn set_nice(process: Option, nice: c_int) -> Result<(), Errno> { let process = process.ok_or(Errno::EINVAL)?.raw().unsigned_abs(); // SAFETY: `setpriority` reads only its integer arguments. @@ -107,7 +121,6 @@ pub(crate) fn set_nice(process: Option, nice: c_int) -> Result } /// Writes the modified data and metadata of every file system to its storage, as `sync(2)` does. -#[cfg(target_arch = "x86_64")] pub(crate) fn sync() { nix::unistd::sync(); } diff --git a/nvx-rs/src/guest/sys/kernel/tests.rs b/nvx-rs/src/guest/sys/kernel/tests.rs index 975461ec0..047fd29d4 100644 --- a/nvx-rs/src/guest/sys/kernel/tests.rs +++ b/nvx-rs/src/guest/sys/kernel/tests.rs @@ -3,7 +3,6 @@ use super::*; /// Returns the nice value of the process `process`, the 19th field of its procfs `stat` file. -#[cfg(target_arch = "x86_64")] pub(crate) fn nice_of(process: crate::guest::sys::ProcessId) -> i32 { let stat = std::fs::read_to_string(format!("/proc/{}/stat", process.raw())).unwrap(); // The command name, the second field, is in parentheses, and may hold spaces. diff --git a/nvx-rs/src/guest/sys/mapping.rs b/nvx-rs/src/guest/sys/mapping.rs index 7d35258d6..f498830f3 100644 --- a/nvx-rs/src/guest/sys/mapping.rs +++ b/nvx-rs/src/guest/sys/mapping.rs @@ -45,6 +45,83 @@ impl MemoryMapping { }) } + /// Performs one ordered byte read from a checked device-memory mapping. + #[cfg(target_arch = "aarch64")] + pub(super) fn device_read_u8(&self, offset: usize) -> Result { + if offset >= self.length.get() { + return Err(Errno::EINVAL); + } + let address = self.address.expect("mapping is owned"); + // SAFETY: the byte lies within the owned mapping; the ARM device transaction is ordered. + let value = unsafe { + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + let value = std::ptr::read_volatile(address.as_ptr().cast::().add(offset)); + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + value + }; + Ok(value) + } + + /// Performs one naturally aligned, ordered 16-bit little-endian device read. + #[cfg(target_arch = "aarch64")] + pub(super) fn device_read_u16(&self, offset: usize) -> Result { + if !offset.is_multiple_of(2) + || offset + .checked_add(2) + .is_none_or(|end| end > self.length.get()) + { + return Err(Errno::EINVAL); + } + let address = self.address.expect("mapping is owned"); + // SAFETY: the aligned two-byte range lies within the owned device mapping. + let value = unsafe { + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + let value = + std::ptr::read_volatile(address.as_ptr().cast::().add(offset).cast::()); + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + value + }; + Ok(u16::from_le(value)) + } + + /// Performs one naturally aligned, ordered 32-bit little-endian device read. + #[cfg(target_arch = "aarch64")] + pub(super) fn device_read_u32(&self, offset: usize) -> Result { + if !offset.is_multiple_of(4) + || offset + .checked_add(4) + .is_none_or(|end| end > self.length.get()) + { + return Err(Errno::EINVAL); + } + let address = self.address.expect("mapping is owned"); + // SAFETY: the aligned four-byte range lies within the owned device mapping. + let value = unsafe { + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + let value = + std::ptr::read_volatile(address.as_ptr().cast::().add(offset).cast::()); + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + value + }; + Ok(u32::from_le(value)) + } + + /// Performs one ordered byte store to a checked device-memory mapping. + #[cfg(target_arch = "aarch64")] + pub(super) fn device_write_u8(&self, offset: usize, value: u8) -> Result<(), Errno> { + if offset >= self.length.get() { + return Err(Errno::EINVAL); + } + let address = self.address.expect("mapping is owned"); + // SAFETY: the byte lies within the writable mapping; barriers keep the store synchronous. + unsafe { + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + std::ptr::write_volatile(address.as_ptr().cast::().add(offset), value); + core::arch::asm!("dmb osh", options(nostack, preserves_flags)); + } + Ok(()) + } + /// Stores a native-endian 32-bit value at `offset`, even when it is unaligned. /// /// Returns `false` without writing unless all four bytes lie inside the mapping. The mapping diff --git a/nvx-rs/src/guest/sys/mod.rs b/nvx-rs/src/guest/sys/mod.rs index 013db3ab0..fef6e22a1 100644 --- a/nvx-rs/src/guest/sys/mod.rs +++ b/nvx-rs/src/guest/sys/mod.rs @@ -51,7 +51,12 @@ //! instructions, which only `port` executes, as inline assembly. The constants of the kernel's //! and the processor's interfaces, such as procfs and sysfs paths, OpenVMM's ports, CPUID //! leaves, and MSRs, are defined here once, so that the programs name them. +//! +//! Private helpers used only by the x86 diagnostic or restore clients build with those clients. +//! Architecture-neutral helpers with unit tests also build on every ISA under `cfg(test)`. +#[cfg(target_arch = "aarch64")] +pub(crate) mod aarch64; mod clock; mod discipline; mod entry; @@ -62,7 +67,10 @@ pub(crate) mod kernel; mod mapping; #[cfg(target_arch = "x86_64")] mod msr; +#[cfg(target_arch = "aarch64")] +pub(crate) mod platform; mod poll; +#[cfg(target_arch = "x86_64")] mod port; mod process; mod rooted; @@ -83,68 +91,87 @@ use std::os::unix::ffi::OsStringExt; pub(crate) use nix::errno::Errno; pub(crate) use nix::sys::signal::Signal; -pub(crate) use nix::sys::time::{TimeSpec, TimeValLike}; +#[cfg(target_arch = "x86_64")] +pub(crate) use nix::sys::time::TimeSpec; +pub(crate) use nix::sys::time::TimeValLike; -pub(crate) use clock::{Clock, clock_time, monotonic_ms, sleep_once, sleep_until_monotonic}; +pub(crate) use clock::{Clock, clock_time, monotonic_ms, sleep_until_monotonic}; #[cfg(target_arch = "x86_64")] -pub(crate) use clock::{monotonic_ns, syscall_monotonic_nanoseconds}; -pub(crate) use discipline::{ClockAdjustment, ClockDiscipline, FrequencyHold, adjust_clock}; +pub(crate) use clock::{monotonic_ns, sleep_once, syscall_monotonic_nanoseconds}; +pub(crate) use discipline::ClockDiscipline; +pub(crate) use discipline::{ClockAdjustment, FrequencyHold, adjust_clock}; pub(crate) use entry::{EntryChild, file_size}; #[cfg(target_arch = "x86_64")] pub(crate) use fd::close_inherited_descriptors; +#[cfg(target_arch = "x86_64")] +pub(crate) use fd::unlink; pub(crate) use fd::{ CreatedFile, FileDescriptor, FileLock, Mode, OpenFlags, create_directory, create_memfd, create_pipe, create_socket_pair, lock_file, open, open_at, page_size, try_lock_directory, - unlink, + try_lock_file, }; +#[cfg(any(target_arch = "x86_64", test))] +pub(crate) use filesystem::TmpfsMount; pub(crate) use filesystem::{ - MountFlags, PATH_MAX, ScratchDirectory, TmpfsMount, check_exists, chmod, chown, - enter_private_mount_namespace, mount, mount_id, path_c_string, path_exists, read_file, - read_line, read_number, unmount, write_file, write_number, + MountFlags, PATH_MAX, chmod, chown, enter_private_mount_namespace, mount, mount_id, + path_c_string, path_exists, read_file, read_number, unmount, write_file, write_number, }; +#[cfg(target_arch = "x86_64")] +pub(crate) use filesystem::{ScratchDirectory, check_exists, read_line}; +#[cfg(any(target_arch = "x86_64", test))] +pub(crate) use ioctl::reread_partition_table; pub(crate) use ioctl::{ - block_device_size, console_pending, flush_block_buffers, logical_block_size, - reread_partition_table, reseed_random, + block_device_size, console_pending, flush_block_buffers, logical_block_size, reseed_random, }; pub(crate) use mapping::MemoryMapping; #[cfg(target_arch = "x86_64")] pub(crate) use msr::{MsrAccess, MsrFile}; +#[cfg(target_arch = "aarch64")] +pub(crate) use platform::{check_released, power_off}; pub(crate) use poll::{Pollable, hung_up, poll, readable_or_hung_up}; +#[cfg(target_arch = "x86_64")] pub(crate) use port::{PORT_DEVICE, Port}; #[cfg(target_arch = "x86_64")] pub(crate) use port::{PortAccess, PortError, check_released, power_off, probe_port}; pub(crate) use process::set_command_name; pub(crate) use process::{ - ChildStatus, DirectStdout, ForkResult, ProcessId, Stdout, WaitTarget, current_process, detach, - exec, exit_immediately, fork, ignore_interrupt_and_hangup, is_unreaped, kill_process, - kill_process_group, kill_through_pidfd, open_pidfd, process_exists, process_group_of, - process_name, reap_any_nonblocking, restore_sigpipe, signal_process_group, - signal_through_pidfd, take_inherited_descriptor, try_wait, wait, wait_named, - write_port_diagnostic, write_standard_error, + ChildStatus, ForkResult, ProcessId, Stdout, WaitTarget, current_process, detach, exec, + exit_immediately, fork, ignore_interrupt_and_hangup, is_unreaped, kill_process, + kill_process_group, kill_through_pidfd, open_pidfd, process_group_of, reap_any_nonblocking, + restore_sigpipe, signal_process_group, signal_through_pidfd, take_inherited_descriptor, + try_wait, wait_named, write_port_diagnostic, write_standard_error, }; #[cfg(target_arch = "x86_64")] -pub(crate) use process::{discard_standard_input, syscall_process_id}; +pub(crate) use process::{DirectStdout, discard_standard_input, process_name, syscall_process_id}; +#[cfg(any(target_arch = "x86_64", test))] +pub(crate) use process::{process_exists, wait}; pub(crate) use rooted::{ FileKind, FileStatus, MountAttributes, change_mode, create_directory_at, create_file_at, detach_mount, file_status, is_whole_file_system, move_mount, open_beneath, open_tree, set_mount_attributes, unmount_beneath, }; pub(crate) use scheduler::{ - AffinityGuard, IdlePriority, SchedulingPolicy, current_affinity, current_cpu, current_thread, - pin_current_thread, promote_all_threads, scheduling_policy, set_scheduling_policy, - thread_count, + AffinityGuard, IdlePriority, pin_current_thread, promote_all_threads, thread_count, }; -pub(crate) use secret::zeroize; #[cfg(target_arch = "x86_64")] -pub(crate) use signal::write_standard_error_without_sigpipe; +pub(crate) use scheduler::{ + SchedulingPolicy, current_affinity, current_cpu, current_thread, scheduling_policy, + set_scheduling_policy, +}; +pub(crate) use secret::zeroize; +#[cfg(any(target_arch = "x86_64", test))] +pub(crate) use signal::ChildSignalWait; pub(crate) use signal::{ - ChildSignalWait, SignalDescriptor, ignore_signals, queue_user_signal, reset_child_signal, - write_without_sigpipe, + SignalDescriptor, ignore_signals, reset_child_signal, write_without_sigpipe, }; +#[cfg(target_arch = "x86_64")] +pub(crate) use signal::{queue_user_signal, write_standard_error_without_sigpipe}; pub(crate) use sock_diag::{ INET_DIAG_BUFFER_LENGTH, InetDiagMessage, decode_inet_diag_dump, dump_ipv6_tcp_sockets, }; -pub(crate) use socket::{UdpSocket, connect_datagram}; +pub(crate) use socket::UdpSocket; +#[cfg(any(target_arch = "x86_64", test))] +pub(crate) use socket::connect_datagram; pub(crate) use spawn::{ChildSpec, PreparedChild, REPORT_LENGTH}; pub(crate) use terminal::{configure_raw, drain}; pub(crate) use timer::Timer; diff --git a/nvx-rs/src/guest/sys/platform.rs b/nvx-rs/src/guest/sys/platform.rs new file mode 100644 index 000000000..cf50b1b19 --- /dev/null +++ b/nvx-rs/src/guest/sys/platform.rs @@ -0,0 +1,345 @@ +//! Typed access to the ARM development binding's restricted platform-control device. + +use std::cell::Cell; +use std::ffi::CStr; +use std::marker::PhantomData; +use std::num::NonZeroUsize; + +use super::{Errno, FileDescriptor, MemoryMapping, Mode, OpenFlags, TimeValLike}; + +pub(crate) mod binding; + +/// The only device authority exposed by this backend, never arbitrary physical memory. +const DEVICE: &CStr = c"/dev/nvx-platform"; +/// The fixed device aperture and the only accepted mapping length. +const PAGE_SIZE: usize = 4096; +/// The existing process-shared record-window lock, also used by the x86 time implementation. +const RECORD_LOCK: &CStr = c"/run/nvx/portb.lock"; + +/// Registers of the approved development platform binding. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Register { + /// Console data or selected record bytes. + Data, + /// Status and record selector. + Status, + /// Latched version-1 time-sample bytes. + Time, + /// Exact shutdown status. + Shutdown, + /// Snapshot/acknowledgement, unavailable during Phase 1. + Snapshot, +} + +impl Register { + /// The fixed offset in the one-page restricted device. + pub(crate) const fn offset(self) -> u64 { + match self { + Self::Data => 0x00, + Self::Status => 0x04, + Self::Time => 0x08, + Self::Shutdown => 0x0c, + Self::Snapshot => 0x10, + } + } + + /// Validates the documented exact-width transaction before any device access. + fn check(self, width: usize, writing: bool) -> Result<(), Errno> { + if writing { + if width != 1 || self == Self::Time { + return Err(Errno::EINVAL); + } + if self == Self::Snapshot { + return Err(Errno::EOPNOTSUPP); + } + } else if !matches!(width, 1 | 2 | 4) || (self == Self::Status && width != 1) { + return Err(Errno::EINVAL); + } + Ok(()) + } +} + +thread_local! { + /// A transaction must end before this thread forks, spawns, or executes a workload. + static HELD: Cell = const { Cell::new(false) }; + /// Failed unmapping leaves authority that this process must not hand to workload code. + static RETAINED: Cell = const { Cell::new(false) }; +} + +/// Whether the thread has safely relinquished all platform authority. +pub(crate) fn check_released() -> Result<(), Errno> { + if HELD.get() || RETAINED.get() { + Err(Errno::EPERM) + } else { + Ok(()) + } +} + +/// The two restricted access paths, each held by exactly one owner. +enum Backend { + /// Preferred, non-copyable device-memory mapping. + Mapped(MemoryMapping), + /// Exact-width positional I/O, explicitly selected rather than masking a mapping error. + File(FileDescriptor), +} + +/// A thread-local trusted transaction; it exposes no pointer or raw mapping. +pub(crate) struct Access { + /// Device authority until explicit release or RAII cleanup. + backend: Option, + /// File-backed authority also must never leave the transaction's thread. + _thread: PhantomData<*const ()>, +} + +impl Access { + /// Opens and maps exactly one page, then immediately closes its CLOEXEC descriptor. + pub(crate) fn request() -> Result { + check_released()?; + if super::page_size() != Some(PAGE_SIZE) { + return Err(Errno::EINVAL); + } + let descriptor = super::open( + DEVICE, + OpenFlags::O_RDWR | OpenFlags::O_CLOEXEC, + Mode::empty(), + )?; + let mapping = MemoryMapping::shared_read_write( + &descriptor, + NonZeroUsize::new(PAGE_SIZE).expect("one page is nonzero"), + 0, + )?; + HELD.set(true); + let access = Self { + backend: Some(Backend::Mapped(mapping)), + _thread: PhantomData, + }; + descriptor.close()?; + Ok(access) + } + + /// Opens the validated positional-I/O path without falling back after a denied mapping. + pub(crate) fn positional() -> Result { + check_released()?; + let descriptor = super::open( + DEVICE, + OpenFlags::O_RDWR | OpenFlags::O_CLOEXEC, + Mode::empty(), + )?; + HELD.set(true); + Ok(Self { + backend: Some(Backend::File(descriptor)), + _thread: PhantomData, + }) + } + + /// Reads one byte with one naturally aligned device transaction. + pub(crate) fn read_u8(&self, register: Register) -> Result { + register.check(1, false)?; + match self.backend.as_ref().expect("access retains its backend") { + Backend::Mapped(mapping) => mapping.device_read_u8(register.offset() as usize), + Backend::File(file) => { + let mut bytes = [0]; + if file.pread(&mut bytes, register.offset())? != 1 { + return Err(Errno::EIO); + } + Ok(bytes[0]) + } + } + } + + /// Reads the next two record bytes with one ordered device transaction. + pub(crate) fn read_u16(&self, register: Register) -> Result { + register.check(2, false)?; + match self.backend.as_ref().expect("access retains its backend") { + Backend::Mapped(mapping) => mapping.device_read_u16(register.offset() as usize), + Backend::File(file) => { + let mut bytes = [0; 2]; + if file.pread(&mut bytes, register.offset())? != 2 { + return Err(Errno::EIO); + } + Ok(u16::from_le_bytes(bytes)) + } + } + } + + /// Reads the next four record bytes with one ordered device transaction. + pub(crate) fn read_u32(&self, register: Register) -> Result { + register.check(4, false)?; + match self.backend.as_ref().expect("access retains its backend") { + Backend::Mapped(mapping) => mapping.device_read_u32(register.offset() as usize), + Backend::File(file) => { + let mut bytes = [0; 4]; + if file.pread(&mut bytes, register.offset())? != 4 { + return Err(Errno::EIO); + } + Ok(u32::from_le_bytes(bytes)) + } + } + } + + /// Writes one byte; Phase 1 never requests a snapshot or acknowledges a restore. + pub(crate) fn write_u8(&self, register: Register, value: u8) -> Result<(), Errno> { + register.check(1, true)?; + match self.backend.as_ref().expect("access retains its backend") { + Backend::Mapped(mapping) => mapping.device_write_u8(register.offset() as usize, value), + Backend::File(file) => { + if file.pwrite(&[value], register.offset())? != 1 { + return Err(Errno::EIO); + } + Ok(()) + } + } + } + + /// Relinquishes device authority, surfacing an explicit unmap or close failure. + pub(crate) fn release(mut self) -> Result<(), Errno> { + self.end() + } + + /// Shared cleanup for explicit release and earlier-return RAII. + fn end(&mut self) -> Result<(), Errno> { + let Some(backend) = self.backend.take() else { + return Ok(()); + }; + let result = match backend { + Backend::Mapped(mapping) => mapping.unmap(), + Backend::File(file) => file.close(), + }; + HELD.set(false); + if result.is_err() { + RETAINED.set(true); + } + result + } +} + +impl Drop for Access { + fn drop(&mut self) { + let _ = self.end(); + } +} + +/// Runs a record transaction under the existing cross-process lock, relinquishing device +/// authority before unlocking or returning any record to its caller. +fn record_transaction( + open: fn() -> Result, + read: impl FnOnce(&Access) -> Result, +) -> Result { + for directory in [c"/run/nvx", c"/run/nvx/time"] { + match super::create_directory(directory, Mode::from_bits_truncate(0o755)) { + Ok(()) | Err(Errno::EEXIST) => {} + Err(error) => return Err(error), + } + } + let _lock = super::lock_file(RECORD_LOCK, Mode::S_IRUSR | Mode::S_IWUSR)?; + let access = open()?; + let result = read(&access); + access.release()?; + result +} + +/// Consumes an exact, word-aligned record from one approved register window. +fn read_window(access: &Access, register: Register) -> Result<[u8; N], Errno> { + const { assert!(N.is_multiple_of(4)) }; + let mut bytes = [0; N]; + for word in bytes.chunks_exact_mut(4) { + word.copy_from_slice(&access.read_u32(register)?.to_le_bytes()); + } + Ok(bytes) +} + +/// Reads the approved identity transport without admitting its CPU profile. +pub(crate) fn time_binding_record() -> Result<[u8; 96], Errno> { + record_transaction(Access::request, |access| { + access.write_u8(Register::Status, 0xa8)?; + let mut bytes = [0; 96]; + bytes[..8].copy_from_slice(&read_window::<8>(access, Register::Data)?); + bytes[8..10].copy_from_slice(&access.read_u16(Register::Data)?.to_le_bytes()); + bytes[10..12].copy_from_slice(&access.read_u16(Register::Data)?.to_le_bytes()); + bytes[12..].copy_from_slice(&read_window::<84>(access, Register::Data)?); + Ok(bytes) + }) +} + +/// Reads the existing process-generation identity through the normal record selector. +pub(crate) fn generation_id() -> Result { + record_transaction(Access::positional, |access| { + if access.read_u8(Register::Status)? & 0x20 == 0 { + return Err(Errno::EOPNOTSUPP); + } + access.write_u8(Register::Status, 0xa6)?; + read_window(access, Register::Data).map(crate::guest::GenerationId::from_bytes) + }) +} + +/// Writes an event to the same platform debug channel as x86 without retaining authority. +pub(crate) fn write_console(separator: &[u8], line: &[u8]) -> Result<(), Errno> { + let access = Access::request()?; + let result = separator + .iter() + .chain(line) + .try_for_each(|&byte| access.write_u8(Register::Data, byte)); + access.release()?; + result +} + +/// Guest clock reads around one synchronous latch, before any sample bytes are consumed. +pub(crate) struct LatchedTime { + /// The unchanged 48-byte version-1 sample, including unknown-bound sentinels. + pub(crate) bytes: [u8; 48], + /// Counter immediately before the selector write. + pub(crate) before: u64, + /// Counter immediately after the selector write. + pub(crate) after: u64, + /// Linux realtime read immediately after the latch. + pub(crate) realtime_ns: i64, + /// Counter midpoint around that realtime read. + pub(crate) realtime_counter: u64, + /// Half the counter interval around the realtime read. + pub(crate) realtime_half_cycles: u64, +} + +/// Latches a real host sample; no frequency, UTC, correction bound or pairing is fabricated. +pub(crate) fn latch_time() -> Result { + record_transaction(Access::request, |access| { + if access.read_u8(Register::Status)? & 0x40 == 0 { + return Err(Errno::EOPNOTSUPP); + } + let before = super::aarch64::virtual_counter(); + access.write_u8(Register::Status, 0xa7)?; + let after = super::aarch64::virtual_counter(); + let realtime_before = super::aarch64::virtual_counter(); + let realtime_ns = super::clock_time(super::Clock::Realtime)?.num_nanoseconds(); + let realtime_after = super::aarch64::virtual_counter(); + if after < before || realtime_after < realtime_before { + return Err(Errno::EPROTO); + } + let half = (realtime_after - realtime_before).div_ceil(2); + Ok(LatchedTime { + bytes: read_window(access, Register::Time)?, + before, + after, + realtime_ns, + realtime_counter: realtime_before + half, + realtime_half_cycles: half, + }) + }) +} + +/// Requests exact-status shutdown and waits only after a successful synchronous device write. +pub(crate) fn power_off(status: u8) -> ! { + let result = Access::request().and_then(|access| { + access.write_u8(Register::Shutdown, status)?; + access.release() + }); + if let Err(error) = result { + super::write_standard_error(format!("NVX-PLATFORM-ERROR: shutdown: {error}\n").as_bytes()); + super::exit_immediately(1); + } + loop { + nix::unistd::pause(); + } +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/guest/sys/platform/binding.rs b/nvx-rs/src/guest/sys/platform/binding.rs new file mode 100644 index 000000000..2ddaedcc9 --- /dev/null +++ b/nvx-rs/src/guest/sys/platform/binding.rs @@ -0,0 +1,104 @@ +//! The approved development identity record, distinct from approval of any CPU profile. + +use std::fmt; + +/// A structurally valid identity, not proof of profile admission. The VMM admits the effective +/// CPU/time contract before product entry; ordinary guest conformance follows readiness. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct TimeBinding { + /// The cold-boot processor capacity. + pub(crate) capacity: u32, + /// Declared architected-counter frequency, not an inferred CPU core frequency. + pub(crate) counter_hz: u64, + /// The selected profile's lowercase ASCII identifier. + pub(crate) profile_id: String, + /// Digest of that same canonical CPU-profile document. + pub(crate) profile_sha256: [u8; 32], +} + +/// The malformed field, with the observed value, not a success-shaped default. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct BindingError(String); + +impl fmt::Display for BindingError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.0) + } +} + +impl std::error::Error for BindingError {} + +impl TimeBinding { + /// Decodes exactly the approved 96-byte a8 record; this does not approve its profile. + pub(crate) fn parse(bytes: &[u8]) -> Result { + let bytes: &[u8; 96] = bytes.try_into().map_err(|_| { + BindingError(format!( + "ARM time identity length {}, expected 96", + bytes.len() + )) + })?; + if &bytes[..4] != b"NVT\0" || bytes[4] != 1 || bytes[5..8] != [0; 3] { + return Err(BindingError(format!( + "ARM time identity header {:02x?}, expected NVT/1 with zero flags/reserved", + &bytes[..8] + ))); + } + let major = u16::from_le_bytes(bytes[8..10].try_into().expect("checked record")); + let minor = u16::from_le_bytes(bytes[10..12].try_into().expect("checked record")); + if (major, minor) != (1, 0) { + return Err(BindingError(format!( + "ARM time binding {major}.{minor}, expected 1.0" + ))); + } + let word = |offset| { + u32::from_le_bytes( + bytes[offset..offset + 4] + .try_into() + .expect("checked record"), + ) + }; + let capacity = word(12); + if !matches!(capacity, 1 | 2 | 4 | 8) { + return Err(BindingError(format!( + "ARM time capacity {capacity}, expected 1/2/4/8" + ))); + } + let counter_hz = u64::from_le_bytes(bytes[16..24].try_into().expect("checked record")); + if counter_hz == 0 || counter_hz > u64::from(u32::MAX) { + return Err(BindingError(format!( + "ARM counter frequency {counter_hz}, expected nonzero CNTFRQ value" + ))); + } + if word(24) != 20 { + return Err(BindingError(format!( + "ARM timer PPI {}, expected INTID 20", + word(24) + ))); + } + let length = word(28) as usize; + if !(1..=32).contains(&length) { + return Err(BindingError(format!( + "ARM CPU profile ID length {length}, expected 1..32" + ))); + } + let id = &bytes[32..32 + length]; + if !id + .iter() + .all(|byte| byte.is_ascii_graphic() && !byte.is_ascii_uppercase()) + || bytes[32 + length..64].iter().any(|&byte| byte != 0) + { + return Err(BindingError( + "ARM CPU profile ID must be lowercase ASCII with zero padding".into(), + )); + } + Ok(Self { + capacity, + counter_hz, + profile_id: String::from_utf8(id.to_vec()).expect("validated ASCII"), + profile_sha256: bytes[64..96].try_into().expect("checked record"), + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/guest/sys/platform/binding/tests.rs b/nvx-rs/src/guest/sys/platform/binding/tests.rs new file mode 100644 index 000000000..207ba7c5f --- /dev/null +++ b/nvx-rs/src/guest/sys/platform/binding/tests.rs @@ -0,0 +1,44 @@ +use super::*; + +/// A test-only identity, never an approved or published CPU profile. +fn identity() -> [u8; 96] { + let mut bytes = [0; 96]; + bytes[..4].copy_from_slice(b"NVT\0"); + bytes[4] = 1; + bytes[8..10].copy_from_slice(&1_u16.to_le_bytes()); + bytes[12..16].copy_from_slice(&8_u32.to_le_bytes()); + bytes[16..24].copy_from_slice(&24_000_000_u64.to_le_bytes()); + bytes[24..28].copy_from_slice(&20_u32.to_le_bytes()); + bytes[28..32].copy_from_slice(&15_u32.to_le_bytes()); + bytes[32..47].copy_from_slice(b"test.profile.v1"); + bytes[64..].fill(0x55); + bytes +} + +#[test] +fn exact_development_record_decodes_without_approving_a_cpu_profile() { + let binding = TimeBinding::parse(&identity()).unwrap(); + assert_eq!(binding.capacity, 8); + assert_eq!(binding.counter_hz, 24_000_000); + assert_eq!(binding.profile_id, "test.profile.v1"); + assert_eq!(binding.profile_sha256, [0x55; 32]); +} + +#[test] +fn truncated_extended_or_mismatched_identity_is_rejected() { + let valid = identity(); + for size in [0, 4, 8, 32, 64, 95] { + assert!(TimeBinding::parse(&valid[..size]).is_err()); + } + assert!(TimeBinding::parse(&[0; 97]).is_err()); + for offset in [0, 4, 5, 6, 8, 10, 12, 24, 28, 32, 47] { + let mut bytes = valid; + bytes[offset] = 0xff; + assert!(TimeBinding::parse(&bytes).is_err(), "offset {offset}"); + } + for frequency in [0, u64::from(u32::MAX) + 1] { + let mut bytes = valid; + bytes[16..24].copy_from_slice(&frequency.to_le_bytes()); + assert!(TimeBinding::parse(&bytes).is_err()); + } +} diff --git a/nvx-rs/src/guest/sys/platform/tests.rs b/nvx-rs/src/guest/sys/platform/tests.rs new file mode 100644 index 000000000..b8551a233 --- /dev/null +++ b/nvx-rs/src/guest/sys/platform/tests.rs @@ -0,0 +1,134 @@ +use super::*; + +#[test] +fn documented_offsets_and_access_matrix_are_exact() { + for (register, offset) in [ + (Register::Data, 0), + (Register::Status, 4), + (Register::Time, 8), + (Register::Shutdown, 12), + (Register::Snapshot, 16), + ] { + assert_eq!(register.offset(), offset); + for width in [0, 1, 2, 3, 4, 8] { + assert_eq!( + register.check(width, false).is_ok(), + matches!(width, 1 | 2 | 4) && (register != Register::Status || width == 1), + "{register:?} read {width}" + ); + assert_eq!( + register.check(width, true).is_ok(), + width == 1 && !matches!(register, Register::Time | Register::Snapshot), + "{register:?} write {width}" + ); + } + } +} + +#[test] +fn snapshot_unavailability_is_explicit() { + assert_eq!(Register::Snapshot.check(1, true), Err(Errno::EOPNOTSUPP)); +} + +#[test] +fn retained_or_active_authority_blocks_workload_spawn() { + assert_eq!(check_released(), Ok(())); + HELD.set(true); + assert_eq!(check_released(), Err(Errno::EPERM)); + HELD.set(false); + RETAINED.set(true); + assert_eq!(check_released(), Err(Errno::EPERM)); + RETAINED.set(false); + assert_eq!(check_released(), Ok(())); +} + +#[test] +fn mapped_and_positional_transactions_have_identical_width_and_byte_order() { + let file = tempfile::tempfile().unwrap(); + file.set_len(PAGE_SIZE as u64).unwrap(); + let descriptor = FileDescriptor::from_owned(file.into()); + assert_eq!( + descriptor.pwrite(&[0x78, 0x56, 0x34, 0x12], Register::Data.offset()), + Ok(4) + ); + let mapping = + MemoryMapping::shared_read_write(&descriptor, NonZeroUsize::new(PAGE_SIZE).unwrap(), 0) + .unwrap(); + let mapped = Access { + backend: Some(Backend::Mapped(mapping)), + _thread: PhantomData, + }; + let positional = Access { + backend: Some(Backend::File(descriptor)), + _thread: PhantomData, + }; + for access in [&mapped, &positional] { + assert_eq!(access.read_u8(Register::Data), Ok(0x78)); + assert_eq!(access.read_u16(Register::Data), Ok(0x5678)); + assert_eq!(access.read_u32(Register::Data), Ok(0x1234_5678)); + assert_eq!(access.read_u16(Register::Status), Err(Errno::EINVAL)); + assert_eq!(access.read_u32(Register::Status), Err(Errno::EINVAL)); + assert_eq!(access.write_u8(Register::Time, 1), Err(Errno::EINVAL)); + assert_eq!( + access.write_u8(Register::Snapshot, 1), + Err(Errno::EOPNOTSUPP) + ); + } + assert_eq!(mapped.write_u8(Register::Status, 0xa8), Ok(())); + assert_eq!(positional.read_u8(Register::Status), Ok(0xa8)); + assert_eq!(positional.write_u8(Register::Data, 0xbc), Ok(())); + assert_eq!(mapped.read_u32(Register::Data), Ok(0x1234_56bc)); + mapped.release().unwrap(); + positional.release().unwrap(); +} + +#[test] +fn mapping_bounds_and_natural_alignment_are_checked_before_access() { + let file = tempfile::tempfile().unwrap(); + file.set_len(PAGE_SIZE as u64).unwrap(); + let descriptor = FileDescriptor::from_owned(file.into()); + let mapping = + MemoryMapping::shared_read_write(&descriptor, NonZeroUsize::new(PAGE_SIZE).unwrap(), 0) + .unwrap(); + for offset in [PAGE_SIZE, usize::MAX] { + assert_eq!(mapping.device_read_u8(offset), Err(Errno::EINVAL)); + assert_eq!(mapping.device_write_u8(offset, 1), Err(Errno::EINVAL)); + } + for offset in [1, PAGE_SIZE - 1, PAGE_SIZE, usize::MAX - 1, usize::MAX] { + assert_eq!(mapping.device_read_u16(offset), Err(Errno::EINVAL)); + } + for offset in [1, 2, PAGE_SIZE - 2, PAGE_SIZE, usize::MAX - 3, usize::MAX] { + assert_eq!(mapping.device_read_u32(offset), Err(Errno::EINVAL)); + } + assert_eq!(mapping.device_read_u16(PAGE_SIZE - 2), Ok(0)); + assert_eq!(mapping.device_read_u32(PAGE_SIZE - 4), Ok(0)); + mapping.unmap().unwrap(); +} + +#[test] +fn short_positional_reads_fail_instead_of_supplying_record_bytes() { + let descriptor = FileDescriptor::from_owned(tempfile::tempfile().unwrap().into()); + let access = Access { + backend: Some(Backend::File(descriptor)), + _thread: PhantomData, + }; + assert_eq!(access.read_u8(Register::Data), Err(Errno::EIO)); + assert_eq!(access.read_u16(Register::Data), Err(Errno::EIO)); + assert_eq!(access.read_u32(Register::Data), Err(Errno::EIO)); + access.release().unwrap(); +} + +#[test] +fn drop_releases_authority_after_a_rejected_transaction() { + let descriptor = FileDescriptor::from_owned(tempfile::tempfile().unwrap().into()); + HELD.set(true); + { + let access = Access { + backend: Some(Backend::File(descriptor)), + _thread: PhantomData, + }; + assert_eq!(check_released(), Err(Errno::EPERM)); + assert_eq!(access.write_u8(Register::Time, 1), Err(Errno::EINVAL)); + } + assert_eq!(check_released(), Ok(())); +} diff --git a/nvx-rs/src/guest/sys/poll.rs b/nvx-rs/src/guest/sys/poll.rs index 65078733d..0cc7765c1 100644 --- a/nvx-rs/src/guest/sys/poll.rs +++ b/nvx-rs/src/guest/sys/poll.rs @@ -5,7 +5,9 @@ use std::time::Duration; use nix::poll::{PollFd, PollFlags, PollTimeout}; -use super::{ChildSignalWait, Errno, FileDescriptor, SignalDescriptor, Timer}; +#[cfg(any(target_arch = "x86_64", test))] +use super::ChildSignalWait; +use super::{Errno, FileDescriptor, SignalDescriptor, Timer}; /// Polls `descriptors` for readable data or hangup, and returns whether each one woke, in the /// caller's order. @@ -47,6 +49,7 @@ pub(crate) enum Pollable<'a> { /// A signal descriptor, which polls readable while one of its signals is pending. Signals(&'a SignalDescriptor), /// A child-signal wait, which polls readable while `SIGCHLD` is pending. + #[cfg(any(target_arch = "x86_64", test))] ChildSignal(&'a ChildSignalWait), } @@ -57,6 +60,7 @@ impl Pollable<'_> { Self::File(descriptor) => descriptor.borrowed(), Self::Timer(timer) => timer.borrowed(), Self::Signals(signals) => signals.borrowed(), + #[cfg(any(target_arch = "x86_64", test))] Self::ChildSignal(wait) => wait.borrowed(), } } diff --git a/nvx-rs/src/guest/sys/process.rs b/nvx-rs/src/guest/sys/process.rs index 9b82123ce..1540fb594 100644 --- a/nvx-rs/src/guest/sys/process.rs +++ b/nvx-rs/src/guest/sys/process.rs @@ -1,14 +1,18 @@ //! Safe process, signal, inherited-descriptor, and standard-stream operations. use std::ffi::{CStr, CString}; -use std::io::{self, Read, Write}; +#[cfg(target_arch = "x86_64")] +use std::io::Read; +use std::io::{self, Write}; use std::os::fd::{AsRawFd, BorrowedFd, RawFd}; use std::sync::atomic::{AtomicBool, Ordering}; use nix::sys::signal::{SigHandler, Signal, kill}; use nix::unistd::Pid; -use super::{Errno, FileDescriptor, Port}; +#[cfg(target_arch = "x86_64")] +use super::Port; +use super::{Errno, FileDescriptor}; use crate::guest::parse::parse_decimal; /// Whether this process inherited an ignored SIGPIPE across `exec`. @@ -129,6 +133,8 @@ pub(crate) enum ForkResult { /// children do. Only the calling thread could start another thread between the check and the /// fork, so the check holds at the fork. pub(crate) fn fork() -> Result { + #[cfg(target_arch = "aarch64")] + super::check_released()?; if super::thread_count()? != 1 { return Err(Errno::EBUSY); } @@ -166,11 +172,13 @@ pub(crate) fn syscall_process_id() -> u64 { } /// Returns whether `process` exists, which includes a zombie, by sending it no signal. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn process_exists(process: ProcessId) -> bool { kill(process.0, None).is_ok() } /// Returns the command name of `process`, which its procfs `comm` file holds with a newline. +#[cfg(target_arch = "x86_64")] pub(crate) fn process_name(process: ProcessId) -> Result, Errno> { let path = super::c_string(format!("/proc/{}/comm", process.0.as_raw()).as_bytes())?; let mut descriptor = super::open( @@ -275,6 +283,7 @@ pub(crate) fn reap_any_nonblocking() -> Option { /// Waits for `process`, retrying interruptions. /// /// libc is used for the same real-time signals as [`reap_any_nonblocking`]. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn wait(process: ProcessId) -> Option { loop { let mut status = 0; @@ -506,6 +515,10 @@ pub(crate) fn signal_through_pidfd( /// Replaces the process image with `path` and `arguments`, and with `environment`, or without /// one, the current environment. It returns only the error of a failed `exec`. pub(crate) fn exec(path: &CStr, arguments: &[CString], environment: Option<&[CString]>) -> Errno { + #[cfg(target_arch = "aarch64")] + if let Err(error) = super::check_released() { + return error; + } let result = match environment { Some(environment) => nix::unistd::execve(path, arguments, environment), None => nix::unistd::execv(path, arguments), @@ -529,19 +542,27 @@ pub(crate) fn write_standard_error(bytes: &[u8]) { } } -/// Writes `message` to a diagnostic I/O port through a descriptor. +/// Writes through the machine's diagnostic descriptor: positional x86 port I/O, or the Linux +/// console stream on other ISAs. /// /// A write that stores no byte is `EIO`, the status that the agent reports for it. pub(crate) fn write_port_diagnostic( descriptor: &FileDescriptor, message: &[u8], ) -> Result<(), Errno> { - for byte in message { - if descriptor.pwrite(std::slice::from_ref(byte), Port::PortbData.offset())? != 1 { - return Err(Errno::EIO); + #[cfg(target_arch = "x86_64")] + { + for byte in message { + if descriptor.pwrite(std::slice::from_ref(byte), Port::PortbData.offset())? != 1 { + return Err(Errno::EIO); + } } + Ok(()) + } + #[cfg(not(target_arch = "x86_64"))] + { + descriptor.write_all(message) } - Ok(()) } /// Standard output with the error behavior of C stdio. @@ -586,8 +607,10 @@ impl Write for Stdout { /// `nvx-device-io`'s C program wrote its result with its own `write` loop rather than stdio. The /// loop retried only `EINTR`, and failed on any other error or on a write that wrote nothing. /// [`Write::write_all`] over this adapter does the same. +#[cfg(target_arch = "x86_64")] pub(crate) struct DirectStdout; +#[cfg(target_arch = "x86_64")] impl Write for DirectStdout { fn write(&mut self, bytes: &[u8]) -> io::Result { nix::unistd::write(io::stdout(), bytes).map_err(io::Error::from) diff --git a/nvx-rs/src/guest/sys/process/tests.rs b/nvx-rs/src/guest/sys/process/tests.rs index 7202a8e10..98d726df4 100644 --- a/nvx-rs/src/guest/sys/process/tests.rs +++ b/nvx-rs/src/guest/sys/process/tests.rs @@ -39,6 +39,15 @@ fn process_group_exists(process: ProcessId) -> bool { signal_process_group(process, 0) != Err(Errno::ESRCH) } +#[test] +#[cfg(target_arch = "aarch64")] +fn arm_diagnostics_write_console_bytes_without_x86_port_offsets() { + let file = tempfile::NamedTempFile::new().unwrap(); + let descriptor = FileDescriptor::from_owned(file.reopen().unwrap().into()); + write_port_diagnostic(&descriptor, b"ARM diagnostic\n").unwrap(); + assert_eq!(std::fs::read(file.path()).unwrap(), b"ARM diagnostic\n"); +} + /// Forks a test's process, which the harness's threads keep [`fork`] from forking, for a test /// that the process runs alone, as `isolate_process_test` starts one. The child has only the /// calling thread, so [`fork`] works in it. diff --git a/nvx-rs/src/guest/sys/scheduler.rs b/nvx-rs/src/guest/sys/scheduler.rs index 9524ef9c2..ba48f9e4c 100644 --- a/nvx-rs/src/guest/sys/scheduler.rs +++ b/nvx-rs/src/guest/sys/scheduler.rs @@ -21,8 +21,10 @@ const PROMOTION_PASSES: usize = 2; /// A CPU affinity mask. #[derive(Clone, Debug)] +#[cfg(any(target_arch = "x86_64", test))] pub(crate) struct CpuAffinity(CpuSet); +#[cfg(any(target_arch = "x86_64", test))] impl CpuAffinity { /// Returns whether the mask contains `cpu`. pub(crate) fn contains(&self, cpu: usize) -> bool { @@ -38,6 +40,7 @@ impl CpuAffinity { } /// Returns the calling thread's CPU affinity mask. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn current_affinity() -> Result { sched_getaffinity(CALLING_THREAD).map(CpuAffinity) } @@ -126,6 +129,7 @@ pub(crate) fn set_scheduling_policy(thread: Option, policy: Schedulin /// /// It makes the system call through libc's `syscall` for the reason that /// [`set_scheduling_policy`] gives. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn scheduling_policy(thread: Option) -> Result { let thread = thread.map_or(CALLING_THREAD.as_raw(), ProcessId::raw); // SAFETY: the system call reads only its integer argument. diff --git a/nvx-rs/src/guest/sys/signal.rs b/nvx-rs/src/guest/sys/signal.rs index 594d152e1..a90feffd8 100644 --- a/nvx-rs/src/guest/sys/signal.rs +++ b/nvx-rs/src/guest/sys/signal.rs @@ -1,12 +1,17 @@ //! Signal dispositions, masks, descriptors, and queued signals. use std::os::fd::{AsFd, BorrowedFd}; +#[cfg(any(target_arch = "x86_64", test))] use std::time::Duration; use nix::sys::signal::{SigHandler, SigSet, SigmaskHow, Signal}; use nix::sys::signalfd::{SfdFlags, SignalFd}; -use super::{Errno, FileDescriptor, Mode, OpenFlags, Pollable, ProcessId, poll}; +#[cfg(target_arch = "x86_64")] +use super::ProcessId; +use super::{Errno, FileDescriptor, Mode, OpenFlags}; +#[cfg(any(target_arch = "x86_64", test))] +use super::{Pollable, poll}; /// One signal read from a [`SignalDescriptor`]. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -191,6 +196,7 @@ pub(crate) fn reset_child_signal() { /// Sends `SIGUSR1` to `process` with `value`, which a [`SignalEvent`] returns. /// /// `nix` does not wrap `sigqueue`, so this calls it through libc. +#[cfg(target_arch = "x86_64")] pub(crate) fn queue_user_signal(process: ProcessId, value: u32) -> Result<(), Errno> { let value = nix::libc::sigval { sival_ptr: value as usize as *mut nix::libc::c_void, @@ -204,6 +210,7 @@ pub(crate) fn queue_user_signal(process: ProcessId, value: u32) -> Result<(), Er /// it from a signal descriptor. Dropping it closes the descriptor and restores the thread's /// previous signal mask. #[derive(Debug)] +#[cfg(any(target_arch = "x86_64", test))] pub(crate) struct ChildSignalWait { /// The thread's signal mask before the wait blocked `SIGCHLD`. previous: SigSet, @@ -211,6 +218,7 @@ pub(crate) struct ChildSignalWait { descriptor: SignalFd, } +#[cfg(any(target_arch = "x86_64", test))] impl ChildSignalWait { /// Blocks `SIGCHLD` in the calling thread before the caller forks the child that it waits /// for, so that the signal of a child that ends at once stays pending. @@ -250,6 +258,7 @@ impl ChildSignalWait { } } +#[cfg(any(target_arch = "x86_64", test))] impl Drop for ChildSignalWait { /// Restores the previous signal mask, best effort, if `restore` has not. fn drop(&mut self) { diff --git a/nvx-rs/src/guest/sys/signal/tests.rs b/nvx-rs/src/guest/sys/signal/tests.rs index 33e94461f..8f3711ebb 100644 --- a/nvx-rs/src/guest/sys/signal/tests.rs +++ b/nvx-rs/src/guest/sys/signal/tests.rs @@ -7,6 +7,7 @@ use super::*; /// Makes the kernel reap the calling process's children on its own, which the `SIG_IGN` /// disposition of `SIGCHLD` does when `ignored`, and `SA_NOCLDWAIT` with the default action /// does otherwise. [`reset_child_signal`] undoes either. +#[cfg(target_arch = "x86_64")] pub(crate) fn reap_children_automatically(ignored: bool) -> Result<(), Errno> { use nix::sys::signal::{SaFlags, SigAction}; @@ -21,6 +22,7 @@ pub(crate) fn reap_children_automatically(ignored: bool) -> Result<(), Errno> { /// Returns whether the calling process has a child, which it has not reaped, without reaping /// it, whatever its disposition of `SIGCHLD`. +#[cfg(target_arch = "x86_64")] pub(crate) fn has_child() -> bool { use nix::sys::wait::{Id, WaitPidFlag}; diff --git a/nvx-rs/src/guest/sys/socket.rs b/nvx-rs/src/guest/sys/socket.rs index dcff34fe1..802b08f84 100644 --- a/nvx-rs/src/guest/sys/socket.rs +++ b/nvx-rs/src/guest/sys/socket.rs @@ -13,6 +13,7 @@ use super::{Errno, FileDescriptor}; /// /// Connecting fixes the destination of every `write` and the only source from which `read` /// receives, as the `socket` and `connect` calls of `nvx-device-io`'s C program did. +#[cfg(any(target_arch = "x86_64", test))] pub(crate) fn connect_datagram(peer: SocketAddrV4) -> Result { let UdpSocket(socket) = UdpSocket::ipv4()?; nix::sys::socket::connect(socket.as_raw_fd(), &SockaddrIn::from(peer))?; diff --git a/nvx-rs/src/guest/sys/spawn.rs b/nvx-rs/src/guest/sys/spawn.rs index 1f76e05a1..8c9e27b3f 100644 --- a/nvx-rs/src/guest/sys/spawn.rs +++ b/nvx-rs/src/guest/sys/spawn.rs @@ -458,7 +458,7 @@ impl PreparedChild { /// returns into Rust. `fork` may occur in a multithreaded process: the child makes only /// direct async-signal-safe system calls over storage allocated before the fork. pub(crate) fn spawn(mut self) -> Result { - #[cfg(target_arch = "x86_64")] + #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] if super::check_released().is_err() { return Err(Errno::EPERM); } diff --git a/nvx-rs/src/guest/sys/spawn/tests.rs b/nvx-rs/src/guest/sys/spawn/tests.rs index 7f455346d..83957e2a5 100644 --- a/nvx-rs/src/guest/sys/spawn/tests.rs +++ b/nvx-rs/src/guest/sys/spawn/tests.rs @@ -23,11 +23,9 @@ pub(crate) const CAP_SYS_CHROOT: u32 = 18; pub(crate) const CAP_SYS_ADMIN: u32 = 21; /// Capability to lower nice values. -#[cfg(target_arch = "x86_64")] pub(crate) const CAP_SYS_NICE: u32 = 23; /// Capability to override resource limits, and to lower an out-of-memory score adjustment. -#[cfg(target_arch = "x86_64")] pub(crate) const CAP_SYS_RESOURCE: u32 = 24; /// Returns whether the calling thread's effective set holds `capability`, because tests whose @@ -265,8 +263,10 @@ fn the_report_writer_is_raised_above_the_standard_streams() { } // With standard input and output closed, the report's pipe takes their numbers, and // placing the child's standard output would replace a writer that kept number 1. + let temporary = tempfile::tempdir().unwrap(); + let missing = crate::guest::sys::path_c_string(&temporary.path().join("nonexistent")).unwrap(); let streams = ClosedStreams::new(&[0, 1]); - let mut spec = ChildSpec::new(c"/nonexistent".to_owned(), vec![c"nonexistent".to_owned()]); + let mut spec = ChildSpec::new(missing, vec![c"nonexistent".to_owned()]); spec.streams[1] = Some(descriptor()); let outcome = run(spec); drop(streams); diff --git a/nvx-rs/src/guest/sys/tests.rs b/nvx-rs/src/guest/sys/tests.rs index eb9bf2711..42e5df6a8 100644 --- a/nvx-rs/src/guest/sys/tests.rs +++ b/nvx-rs/src/guest/sys/tests.rs @@ -6,7 +6,6 @@ use super::*; pub(crate) use super::entry::tests::Simulation; pub(crate) use super::fd::tests::{create_fifo, set_descriptor_limit}; -#[cfg(target_arch = "x86_64")] pub(crate) use super::kernel::tests::nice_of; #[cfg(target_arch = "x86_64")] pub(crate) use super::port::tests::PortBackend; @@ -20,12 +19,13 @@ pub(crate) use super::rooted::tests::{ exchange_paths, restrict_new_entries, thread_umask, unshare_standard_descriptors, with_thread_umask, }; -pub(crate) use super::signal::tests::{default_sigpipe, has_child, reap_children_automatically}; +pub(crate) use super::signal::tests::default_sigpipe; +#[cfg(target_arch = "x86_64")] +pub(crate) use super::signal::tests::{has_child, reap_children_automatically}; pub(crate) use super::sock_diag::tests::{encode_inet_diag_done, encode_inet_diag_socket}; pub(crate) use super::spawn::tests::{ CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_SETGID, CAP_SETUID, CAP_SYS_ADMIN, holds_capability, }; -#[cfg(target_arch = "x86_64")] pub(crate) use super::spawn::tests::{CAP_SYS_NICE, CAP_SYS_RESOURCE}; /// A stream whose writes fail with a chosen `errno`. diff --git a/nvx-rs/src/guest/time/api.rs b/nvx-rs/src/guest/time/api.rs index dabc230cf..5a67d7460 100644 --- a/nvx-rs/src/guest/time/api.rs +++ b/nvx-rs/src/guest/time/api.rs @@ -1,49 +1,57 @@ //! The time ABI's typed calls, through which a guest init or agent runs its steps in process. +#[cfg(target_arch = "x86_64")] use std::ffi::CString; use std::fmt::{self, Display}; use std::io; +#[cfg(target_arch = "x86_64")] use std::net::IpAddr; +#[cfg(target_arch = "x86_64")] use std::os::unix::ffi::OsStrExt; +#[cfg(target_arch = "x86_64")] use std::path::PathBuf; -use super::checks::{self, Checks}; +#[cfg(target_arch = "x86_64")] +use super::NSEC_PER_USEC; +use super::Phase; +#[cfg(target_arch = "x86_64")] +use super::checks; +use super::checks::Checks; +#[cfg(target_arch = "x86_64")] use super::cpu_mask::CpuMask; use super::daemon; +#[cfg(target_arch = "x86_64")] use super::decoders::{self, PACKET_HEADER_SIZE, PacketError, PacketHeader, RestorePacket}; use super::events::{CheckId, ViolationCode}; +#[cfg(target_arch = "aarch64")] +use super::state::TimeState; +#[cfg(target_arch = "x86_64")] use super::state::{CheckState, CheckStatus, Discontinuity, DowntimeSource, TimeState}; use super::system::{ - self, BestSample, PORTB_GENERATION_ID_AVAILABLE, PORTB_PACKET_AVAILABLE, PRIVATE_FILE_MODE, - PowerOffStatus, RESTORE_PATH, RESTORE_TEMP_PATH, RealOps, Runtime, SAMPLE_ATTEMPTS, - SAMPLE_BOUND_NS, SELECT_GENERATION_ID, SELECT_PACKET, SampleError, SampleResult, SaveError, - SpawnError, clock_ns, cpu_time_ns, current_frequency, fork_process, initial_sample, portb_read, - state_apply, state_load, state_store, + self, PowerOffStatus, RealOps, Runtime, SpawnError, clock_ns, cpu_time_ns, current_frequency, + fork_process, initial_sample, state_store, +}; +#[cfg(target_arch = "x86_64")] +use super::system::{ + BestSample, PORTB_GENERATION_ID_AVAILABLE, PORTB_PACKET_AVAILABLE, PRIVATE_FILE_MODE, + RESTORE_PATH, RESTORE_TEMP_PATH, SAMPLE_ATTEMPTS, SAMPLE_BOUND_NS, SELECT_GENERATION_ID, + SELECT_PACKET, SampleError, SampleResult, SaveError, portb_read, state_apply, state_load, }; -use super::{NSEC_PER_USEC, Phase}; use crate::guest::GenerationId; +#[cfg(target_arch = "x86_64")] use crate::guest::reseed::Seed; -use crate::guest::sys::{ - self, Errno, FileDescriptor, ForkResult, OpenFlags, Port, PortAccess, PortError, kernel, -}; -use crate::protocol::cmdline::{self, Token}; +use crate::guest::sys::{self, Errno, ForkResult, kernel}; +#[cfg(target_arch = "x86_64")] +use crate::guest::sys::{FileDescriptor, OpenFlags, Port, PortAccess, PortError}; -/// The kernel command-line token that makes the time ABI report-only, `nvx_time_abi=report-only`. -const REPORT_ONLY_TOKEN: Token<'static> = Token { - name: b"nvx_time_abi", - value: Some(b"report-only"), -}; - -/// The guest side of NVX time ABI version one: its cold-boot time step and the start of its time -/// daemon, its VM generation ID, the capture request with the repair of a restore, and the -/// restore's finish, which run in the calling process. +/// The shared cold-boot step, supervised time daemon and VM generation identity. +/// On x86, the same API also prepares capture and repairs and finishes restores. /// -/// A call needs the privileges of `nvx-time`: `CAP_SYS_RAWIO` for OpenVMM's I/O ports, and -/// write access to `/run/nvx`. It takes the permission for the ports only for each of its port -/// transactions, on the calling thread, and returns only once the thread holds none of it, or -/// fails with [`TimeError::PortRelease`], after which the thread must start neither a thread -/// nor a process, which would inherit the permission; [`Command`](crate::guest::spawn::Command) -/// refuses to start one from it. No call waits for a child that it did not start. +/// Calls need write access to `/run/nvx` and the ISA's hardware interface: x86's port permission +/// and MSR devices, or ARM's restricted platform device and architected EL0 counter. +/// Every transaction releases its authority before another process can be started. +/// [`Command`](crate::guest::spawn::Command) refuses to inherit retained authority. +/// No call waits for a child that it did not start. /// /// A violation of the time ABI is reported by a violation event on the console, in the kernel /// log, and on OpenVMM's debug console. In enforcing mode, the event stops the VM, so the call @@ -66,16 +74,18 @@ impl TimeAbi { /// Returns the time ABI in the mode that the kernel command line selects: report-only when /// it holds `nvx_time_abi=report-only`, and enforcing otherwise. pub fn new() -> Self { - Self { - report_only: cmdline_requests_report_only(), - strict: false, - } + Self::with_report_only(cmdline_requests_report_only()) } /// Returns the time ABI in report-only mode, whatever the kernel command line selects. pub fn report_only() -> Self { + Self::with_report_only(true) + } + + /// Constructs the mode already resolved by the command dispatcher. + pub(crate) const fn with_report_only(report_only: bool) -> Self { Self { - report_only: true, + report_only, strict: false, } } @@ -126,8 +136,8 @@ impl TimeAbi { /// the wall clock to it, holds the frequency correction that the host's clock correction /// asks for, and publishes the time state, which starts at generation zero. /// - /// The sample pairs the frequency MSR's declared TSC rate, which it reads through - /// `/dev/cpu/0/msr`, so devtmpfs must be mounted first. A sample that cannot be taken within + /// The sample uses the declared counter rate: x86's frequency MSR or ARM's a8/CNTFRQ binding. + /// The hardware device must be available first. A sample that cannot be taken within /// its bound is a violation of check C12, which does not fail the call in report-only mode: /// the step then leaves the wall clock unsynchronized, which [`ColdBoot::synchronized`] /// tells. The step starts no process: [`start_daemon`](Self::start_daemon) starts the time @@ -142,8 +152,8 @@ impl TimeAbi { /// /// The child runs the deferred boot checks in the background, at idle priority, and then /// becomes the time daemon: it disciplines the wall clock, watches the kernel log for - /// violations, and runs repair step 7 of the restores that - /// [`restore_finish`](Self::restore_finish) hands it. It runs in a session of its own, with + /// violations, and on x86-64 runs repair step 7 of the restores that + /// `restore_finish` hands it. It runs in a session of its own, with /// its standard streams on the null device, until the VM stops or `SIGTERM` stops it, and /// waits only for the children that it starts itself. It takes the command name `nvx-time`, /// by which the calls of the time ABI recognize it. The caller supervises it and collects @@ -166,12 +176,15 @@ impl TimeAbi { daemon::boot_checker(&self.enforcing_runtime(), &boot) } Ok(ForkResult::Parent(child)) => Ok(child.raw().unsigned_abs()), + #[cfg(target_arch = "x86_64")] Err(SpawnError::Ports(error)) => Err(error.into()), + #[cfg(target_arch = "aarch64")] + Err(SpawnError::Authority(error)) => Err(TimeError::PlatformRelease(error.into())), Err(SpawnError::Fork(error)) => Err(TimeError::Spawn(error.into())), } } - /// Reads the VM generation ID, which a [capture](Self::capture) needs. + /// Reads the VM generation ID, also used by x86-64's `capture`. pub fn generation_id(&self) -> Result { let runtime = self.runtime(); settled(&runtime, read_generation_id(&runtime)) @@ -191,6 +204,7 @@ impl TimeAbi { /// requested it, in which the call does not return. With [`CaptureRequest::finish`], /// [`Restore::finished`] tells whether the call finished the restore, and how it reports a /// failed handoff of repair step 7. + #[cfg(target_arch = "x86_64")] pub fn capture(&self, request: CaptureRequest) -> Result { let runtime = self.runtime(); settled(&runtime, capture(&runtime, request)) @@ -209,6 +223,7 @@ impl TimeAbi { /// passes to init, or to the nearest subreaper, without the process's descriptors. The call /// returns when step 7 ends, which reports its failures by their events only, as /// `nvx-time restore-finish` does. + #[cfg(target_arch = "x86_64")] pub fn restore_finish(&self, request: RestoreFinish) -> Result<(), TimeError> { let runtime = self.runtime(); settled(&runtime, restore_finish(&runtime, request)) @@ -229,6 +244,7 @@ impl TimeAbi { /// Like the other calls, it fails with [`TimeError::PortRelease`], as /// [`PreCaptureError::PortRelease`], if the thread may still hold some of the permission for /// the ports. + #[cfg(target_arch = "x86_64")] pub(crate) fn pre_capture(&self) -> Result<(), PreCaptureError> { released_preparation( pre_capture(&self.enforcing_runtime()), @@ -240,6 +256,7 @@ impl TimeAbi { /// removes their record, as `nvx-time cancel-capture` does, or fails with its diagnostic, /// such as `read /run/nvx/time-suppression: No such file or directory`. It takes no /// permission for the ports. + #[cfg(target_arch = "x86_64")] pub(crate) fn cancel_capture(&self) -> Result<(), Vec> { system::suppression_restore(false) } @@ -253,19 +270,30 @@ impl Default for TimeAbi { } /// Returns whether the kernel command line selects report-only mode: whether it holds the -/// `nvx_time_abi=report-only` token, among the tokens into which [`cmdline::init_tokens`] splits +/// `nvx_time_abi=report-only` token, among the tokens into which +/// [`crate::protocol::cmdline::init_tokens`] splits /// it as the guest's init does. fn cmdline_requests_report_only() -> bool { - let Ok(text) = sys::read_file(kernel::COMMAND_LINE) else { - return false; + let text = match sys::read_file(kernel::COMMAND_LINE) { + Ok(text) => text, + Err(error) => { + sys::write_standard_error( + format!("nvx-time: read kernel command line: {error}; keeping enforcing mode\n") + .as_bytes(), + ); + return false; + } }; - cmdline::init_tokens(&text).any(|token| token == REPORT_ONLY_TOKEN) + super::sample::report_only(&text, false) } /// Ends a call with `result`, unless the calling thread may still hold port permission that a /// failed revocation left, which the call reports instead. fn released(result: Result) -> Result { + #[cfg(target_arch = "x86_64")] sys::check_released().map_err(TimeError::from)?; + #[cfg(target_arch = "aarch64")] + sys::check_released().map_err(|error| TimeError::PlatformRelease(error.into()))?; result } @@ -282,6 +310,7 @@ fn settled(runtime: &Runtime<'_>, result: Result) -> Result, released: Result<(), PortError>, @@ -339,9 +368,10 @@ fn cold_boot(runtime: &Runtime<'_>) -> Result { }; let mut state = TimeState::default(); state.synchronized = initial.is_some(); + state.violations = checks.failures as u64; match &initial { Some((sample, _)) => state.record_sample(sample), - None => state.frequency_ppb = decoders::frequency_to_ppb(current_frequency(runtime)), + None => state.frequency_ppb = super::sample::frequency_to_ppb(current_frequency(runtime)), } if let Err(error) = state_store(&state) { return Err(check_failed( @@ -360,6 +390,7 @@ fn cold_boot(runtime: &Runtime<'_>) -> Result { } /// The read of [`TimeAbi::generation_id`]. +#[cfg(target_arch = "x86_64")] fn read_generation_id(runtime: &Runtime<'_>) -> Result { let ports = runtime.open_ports()?; if ports.read_u8(Port::PortbStatus) & PORTB_GENERATION_ID_AVAILABLE == 0 { @@ -374,8 +405,18 @@ fn read_generation_id(runtime: &Runtime<'_>) -> Result Ok(GenerationId::from_bytes(id)) } +/// Reads the same a6 generation record through the restricted ARM transport. +#[cfg(target_arch = "aarch64")] +fn read_generation_id(_runtime: &Runtime<'_>) -> Result { + sys::platform::generation_id().map_err(|error| match error { + Errno::EOPNOTSUPP => TimeError::GenerationIdUnavailable, + error => TimeError::PlatformAccess(error.into()), + }) +} + /// Why [`TimeAbi::pre_capture`] failed. #[derive(Debug)] +#[cfg(target_arch = "x86_64")] pub(crate) enum PreCaptureError { /// The boot checks were still pending when the wait ended: a violation of check C10, which /// a report-only call reports with its event. @@ -403,6 +444,7 @@ pub(crate) enum PreCaptureError { }, } +#[cfg(target_arch = "x86_64")] impl PreCaptureError { /// Returns whether the call that failed holds the suppression of the stall detectors, as a /// call that succeeded does: their record stays, and the caller restores their settings with @@ -420,8 +462,16 @@ impl PreCaptureError { } /// The preparation of [`TimeAbi::pre_capture`]. +#[cfg(target_arch = "x86_64")] fn pre_capture(runtime: &Runtime<'_>) -> Result<(), PreCaptureError> { - let (state, completed) = checks::wait_for_checks(true); + let (state, completed) = checks::wait_for_checks(true).map_err(|error| { + PreCaptureError::BootChecks(check_failed( + runtime, + Phase::Capture, + CheckId::C10, + format!("cannot read the time state: {error}"), + )) + })?; if !completed { return Err(PreCaptureError::BootChecks(check_failed( runtime, @@ -469,6 +519,7 @@ fn pre_capture(runtime: &Runtime<'_>) -> Result<(), PreCaptureError> { /// How a snapshot treats the guest's scratch disk, which the capture request selects. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[cfg(target_arch = "x86_64")] pub enum ScratchPolicy { /// The snapshot keeps no scratch disk: the capture happens before the scratch disk is /// mounted, and a restore attaches a new one. @@ -477,6 +528,7 @@ pub enum ScratchPolicy { Paired, } +#[cfg(target_arch = "x86_64")] impl ScratchPolicy { /// Returns the byte that requests the capture through the snapshot port. pub(crate) fn request(self) -> u8 { @@ -493,6 +545,7 @@ impl ScratchPolicy { /// [`in_memory`](Self::in_memory), which writes no file, and returns it as the [`Restore`]'s /// owned [`Seed`]. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg(target_arch = "x86_64")] pub struct CaptureRequest { /// The snapshot's scratch policy. scratch: ScratchPolicy, @@ -507,6 +560,7 @@ pub struct CaptureRequest { /// How a restore hands over the fresh entropy of its restore packet: the mode of a /// [`CaptureRequest`]. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg(target_arch = "x86_64")] enum EntropyMode { /// The file mode: the restore writes the entropy to the file at this path, which only its /// owner may read and write. @@ -515,6 +569,7 @@ enum EntropyMode { InMemory, } +#[cfg(target_arch = "x86_64")] impl CaptureRequest { /// Describes a capture in the file mode, with the `scratch` policy, by the VM process whose /// generation ID, from [`TimeAbi::generation_id`], is `generation_id`. A restore writes the @@ -557,6 +612,7 @@ impl CaptureRequest { /// How a capture request ended, in the VM process in which it returned. #[derive(Debug)] +#[cfg(target_arch = "x86_64")] pub enum CaptureOutcome { /// The VM continued without a restore: no snapshot was taken, or the restore packet was of /// the recorded generation. The entropy file of the file mode is removed. @@ -572,6 +628,7 @@ pub enum CaptureOutcome { /// [`take_seed`](Self::take_seed) hands it over, so it is neither `Clone` nor comparable: the /// seed has one owner. #[derive(Debug)] +#[cfg(target_arch = "x86_64")] pub struct Restore { /// The packet's flags. flags: u8, @@ -591,6 +648,7 @@ pub struct Restore { seed: Option, } +#[cfg(target_arch = "x86_64")] impl Restore { /// Returns whether the call finished the restore, as [`CaptureRequest::finish`] asked. /// Otherwise, the caller activates the restore's processors and memory, applies its network @@ -676,6 +734,7 @@ impl Restore { /// announces its listeners under it, as a cold boot under that policy learns them from the /// `virtnet_dns=` and `virtnet_ingress=` tokens of its command line. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[cfg(target_arch = "x86_64")] pub struct RestoreNetwork { /// The DNS server, one of the gateway's addresses, or `None` when the policy lets the guest /// reach no DNS service of the gateway. @@ -684,6 +743,7 @@ pub struct RestoreNetwork { announce_listeners: bool, } +#[cfg(target_arch = "x86_64")] impl RestoreNetwork { /// Returns the network settings with `dns_server` and `announce_listeners`. pub(crate) const fn new(dns_server: Option, announce_listeners: bool) -> Self { @@ -707,6 +767,7 @@ impl RestoreNetwork { /// A range of guest physical memory that a restore adds. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[cfg(target_arch = "x86_64")] pub struct MemoryRange { /// The range's first guest physical address. start: u64, @@ -714,6 +775,7 @@ pub struct MemoryRange { length: u64, } +#[cfg(target_arch = "x86_64")] impl MemoryRange { /// Returns the range's first guest physical address. pub fn start(&self) -> u64 { @@ -728,6 +790,7 @@ impl MemoryRange { /// The flags with which a capture in the file mode creates the entropy file: write-only, created /// or truncated, and never through a symbolic link. +#[cfg(target_arch = "x86_64")] const ENTROPY_FLAGS: OpenFlags = OpenFlags::O_WRONLY .union(OpenFlags::O_CREAT) .union(OpenFlags::O_TRUNC) @@ -735,6 +798,7 @@ const ENTROPY_FLAGS: OpenFlags = OpenFlags::O_WRONLY .union(OpenFlags::O_CLOEXEC); /// The capture request and repair of [`TimeAbi::capture`]. +#[cfg(target_arch = "x86_64")] fn capture(runtime: &Runtime<'_>, request: CaptureRequest) -> Result { let CaptureRequest { scratch, @@ -801,6 +865,7 @@ fn capture(runtime: &Runtime<'_>, request: CaptureRequest) -> Result) { if let Ok(state) = state_load() { runtime.set_generation(state.generation as u32); @@ -809,6 +874,7 @@ pub(crate) fn load_generation(runtime: &Runtime<'_>) { } /// Where a capture hands over a restore's fresh entropy, in its request's mode. +#[cfg(target_arch = "x86_64")] enum EntropySink { /// The file mode's entropy file, which the capture created. File { @@ -821,6 +887,7 @@ enum EntropySink { Seed, } +#[cfg(target_arch = "x86_64")] impl EntropySink { /// Closes and removes the file mode's entropy file, of a capture that restored no packet. fn discard(self) { @@ -845,6 +912,7 @@ impl EntropySink { /// What repair step 2's port reads returned, which repair validates once their transaction /// ended. +#[cfg(target_arch = "x86_64")] enum PacketRead { /// The packet's header is invalid. InvalidHeader(PacketError), @@ -859,12 +927,14 @@ enum PacketRead { } /// The time sample that repair selected, or why it could select none. +#[cfg(target_arch = "x86_64")] type SampleRead = Result<(SampleResult, BestSample), SampleError>; /// Repair step 2's port reads, after the capture read the status, step 1: selects the restore /// packet, and reads its header and, when the header is valid and follows the `recorded` /// generation, its body, with four-byte reads, and then, for a valid packet, takes the time /// sample of the packet's generation, to which repair steps the wall clock. +#[cfg(target_arch = "x86_64")] fn read_packet(runtime: &Runtime<'_>, ports: &PortAccess<'_>, recorded: u32) -> PacketRead { ports.write_u8(Port::PortbStatus, SELECT_PACKET); let mut header = [0_u8; PACKET_HEADER_SIZE]; @@ -906,6 +976,7 @@ fn read_packet(runtime: &Runtime<'_>, ports: &PortAccess<'_>, recorded: u32) -> } /// What a restore transaction writes, and where. +#[cfg(target_arch = "x86_64")] struct RepairTarget { /// The VM generation ID before the snapshot, which the packet must replace. previous_id: GenerationId, @@ -920,6 +991,7 @@ struct RepairTarget { /// Repair steps 2 and 3, once the port transaction that read the packet and its time sample /// ended: validates them, steps the clock to the sample and holds the frequency correction that /// its host clock correction asks for, hands over the packet's entropy, and records the restore. +#[cfg(target_arch = "x86_64")] fn repair_restore( runtime: &Runtime<'_>, read: PacketRead, @@ -1049,6 +1121,7 @@ fn repair_restore( /// Applies one successful restore packet, and the time sample to which repair stepped the wall /// clock, to persistent state. +#[cfg(target_arch = "x86_64")] fn apply_repair( state: &mut TimeState, packet: &PacketHeader, @@ -1088,8 +1161,10 @@ fn apply_repair( /// Its `Display` form is a CPU list in the kernel's syntax, such as `1-3,6`, or `none` for the /// empty set. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[cfg(target_arch = "x86_64")] pub struct CpuSet(CpuMask); +#[cfg(target_arch = "x86_64")] impl CpuSet { /// Returns the empty set. pub const fn new() -> Self { @@ -1123,6 +1198,7 @@ impl CpuSet { } } +#[cfg(target_arch = "x86_64")] impl From for CpuSet { /// Returns the set of the mask's CPUs. fn from(mask: CpuMask) -> Self { @@ -1130,6 +1206,7 @@ impl From for CpuSet { } } +#[cfg(target_arch = "x86_64")] impl Display for CpuSet { /// Writes the set as a CPU list in the kernel's syntax, or `none` for the empty set. fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -1139,6 +1216,7 @@ impl Display for CpuSet { /// The finish of a restore, [`TimeAbi::restore_finish`]. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[cfg(target_arch = "x86_64")] pub struct RestoreFinish { /// Whether to acknowledge the restore through the snapshot port. acknowledge: bool, @@ -1146,6 +1224,7 @@ pub struct RestoreFinish { new_cpus: CpuSet, } +#[cfg(target_arch = "x86_64")] impl RestoreFinish { /// Describes a finish that records no new CPUs and sends no acknowledgement. pub fn new() -> Self { @@ -1168,6 +1247,7 @@ impl RestoreFinish { } /// The finish of [`TimeAbi::restore_finish`]. +#[cfg(target_arch = "x86_64")] fn restore_finish(runtime: &Runtime<'_>, request: RestoreFinish) -> Result<(), TimeError> { let RestoreFinish { acknowledge, @@ -1233,6 +1313,7 @@ fn restore_finish(runtime: &Runtime<'_>, request: RestoreFinish) -> Result<(), T /// Reports that restore-time repair failed, which stops the VM unless report-only or strict, /// and returns the violation. +#[cfg(target_arch = "x86_64")] fn repair_failed( runtime: &Runtime<'_>, code: ViolationCode, @@ -1320,16 +1401,26 @@ impl Display for Violation { pub enum TimeError { /// The calling thread could not take the permission for OpenVMM's ports, which needs /// `CAP_SYS_RAWIO`. + #[cfg(target_arch = "x86_64")] PortAccess(io::Error), /// The kernel did not revoke the calling thread's permission for OpenVMM's ports. The /// thread may still hold some of it, and must start neither a thread nor a process, which /// [`Command`](crate::guest::spawn::Command) refuses to start from it. + #[cfg(target_arch = "x86_64")] PortRelease(io::Error), /// The lock that serializes the transactions of port B's window failed. + #[cfg(target_arch = "x86_64")] PortLock(io::Error), + /// The restricted ARM platform transaction could not start or complete. + #[cfg(target_arch = "aarch64")] + PlatformAccess(io::Error), + /// The thread may retain platform authority and must not start another process. + #[cfg(target_arch = "aarch64")] + PlatformRelease(io::Error), /// The time state, which the cold-boot time step publishes, is unavailable. StateUnavailable, /// The entropy file could not be created. + #[cfg(target_arch = "x86_64")] EntropyFile { /// The file's path. path: PathBuf, @@ -1346,6 +1437,7 @@ pub enum TimeError { Violation(Violation), } +#[cfg(target_arch = "x86_64")] impl From for TimeError { /// Returns the error of a port transaction that could not start or end. fn from(error: PortError) -> Self { @@ -1360,12 +1452,22 @@ impl From for TimeError { impl Display for TimeError { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + #[cfg(target_arch = "x86_64")] Self::PortAccess(error) => write!(formatter, "ioperm: {}", sys::describe(error)), + #[cfg(target_arch = "x86_64")] Self::PortRelease(error) => { write!(formatter, "ioperm revoke: {}", sys::describe(error)) } + #[cfg(target_arch = "x86_64")] Self::PortLock(error) => write!(formatter, "portb lock: {}", sys::describe(error)), + #[cfg(target_arch = "aarch64")] + Self::PlatformAccess(error) => write!(formatter, "platform: {}", sys::describe(error)), + #[cfg(target_arch = "aarch64")] + Self::PlatformRelease(error) => { + write!(formatter, "platform release: {}", sys::describe(error)) + } Self::StateUnavailable => formatter.write_str("the time state is unavailable"), + #[cfg(target_arch = "x86_64")] Self::EntropyFile { path, source } => write!( formatter, "open {}: {}", @@ -1384,15 +1486,19 @@ impl Display for TimeError { impl std::error::Error for TimeError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { - Self::PortAccess(error) - | Self::PortRelease(error) - | Self::PortLock(error) - | Self::Spawn(error) - | Self::EntropyFile { source: error, .. } => Some(error), + #[cfg(target_arch = "x86_64")] + Self::PortAccess(error) | Self::PortRelease(error) | Self::PortLock(error) => { + Some(error) + } + Self::Spawn(error) => Some(error), + #[cfg(target_arch = "x86_64")] + Self::EntropyFile { source: error, .. } => Some(error), + #[cfg(target_arch = "aarch64")] + Self::PlatformAccess(error) | Self::PlatformRelease(error) => Some(error), Self::StateUnavailable | Self::GenerationIdUnavailable | Self::Violation(_) => None, } } } -#[cfg(test)] +#[cfg(all(test, target_arch = "x86_64"))] mod tests; diff --git a/nvx-rs/src/guest/time/api/tests.rs b/nvx-rs/src/guest/time/api/tests.rs index 772f2981f..9393ffdbb 100644 --- a/nvx-rs/src/guest/time/api/tests.rs +++ b/nvx-rs/src/guest/time/api/tests.rs @@ -699,6 +699,7 @@ fn privileged_captures_hand_over_the_entropy_in_their_mode() { fn cold_boot_in_strict_and_report_only_mode() { sys::enter_private_mount_namespace().unwrap(); let _run = sys::TmpfsMount::mount(c"nvx-test", c"/run", c"mode=0755").unwrap(); + assert!(matches!(checks::wait_for_checks(true), Err(Errno::ENOENT))); // Port B offers no time-sample window, so the initial sample fails check C12. let ops = FakeOps::new([0]); let runtime = Runtime::new(&ops, false).strict(); @@ -728,6 +729,7 @@ fn cold_boot_in_strict_and_report_only_mode() { assert!(!boot.synchronized()); assert_eq!(boot.failures, 1); assert!(!state_load().unwrap().synchronized); + assert_eq!(state_load().unwrap().violations, 1); // A sample beyond the bound fails check C12 too. let host = HostClock { status: HostClockStatus::FreeRunning, diff --git a/nvx-rs/src/guest/time/checks.rs b/nvx-rs/src/guest/time/checks.rs index 78263aaa4..74d75b5d2 100644 --- a/nvx-rs/src/guest/time/checks.rs +++ b/nvx-rs/src/guest/time/checks.rs @@ -9,24 +9,18 @@ use super::Phase; use super::cpu_mask::CpuMask; use super::decoders::{self, CpuInfo, ZERO_HYPERV_LEAVES}; use super::events::{CheckId, DETAIL_MAX, ViolationCode, escape_text}; -use super::state::{CheckState, CheckStatus, TimeState}; +use super::state::CheckState; use super::system::{ DAEMON_NAME, DAEMON_PID_PATH, DEFAULT_STALL_TIMEOUT_S, MsrDescriptor, Runtime, msr_error, - rates_load, state_apply, state_load, + rates_load, state_load, }; use crate::guest::status::CommandError; use crate::guest::sys::x86::cpuid::{self, Register}; use crate::guest::sys::x86::{CpuidResult, msr}; use crate::guest::sys::{self, Errno, MsrAccess, MsrFile, ProcessId, kernel}; -/// Maximum timer-list convergence wait during boot and restore. -pub(crate) const TIMER_LIST_WAIT: Duration = Duration::from_millis(200); -/// Poll interval while the timer list converges. -const TIMER_LIST_POLL: Duration = Duration::from_millis(10); -/// Maximum wait for persisted phase checks to complete. -const STATUS_WAIT: Duration = Duration::from_secs(30); -/// Poll interval while waiting for persisted checks. -const STATUS_POLL: Duration = Duration::from_millis(10); +use super::deferred::TIMER_LIST_POLL; +pub(crate) use super::deferred::TIMER_LIST_WAIT; /// Hz in a kHz, which `/proc/cpuinfo`'s rates use. const HZ_PER_KHZ: u64 = 1000; /// The process ID of init, which never runs the daemon. @@ -306,39 +300,13 @@ pub(crate) fn record_check( tsc_hz: u64, lapic_hz: u64, ) -> Result<(), Errno> { - let phase = checks.phase; - if phase.check_index().is_none() { - return Err(Errno::EINVAL); - } let failures = runtime.report_only().then_some(checks.failures as u64); - let status = if failures.is_some_and(|failures| failures > 0) { - CheckStatus::Fail - } else { - CheckStatus::Ok - }; - state_apply(|state| { - let generation = if phase == Phase::Boot { - 0 - } else { - state.generation - }; - if let Some(check) = state.check_mut(phase) { - *check = CheckState { - status: Some(status), - cpus: checks.online.len() as u64, - elapsed_us, - cpu_us, - generation, - failures, - }; - } - if tsc_hz != 0 { - state.tsc_hz = tsc_hz; - } - if lapic_hz != 0 { - state.lapic_hz = lapic_hz; - } - }) + super::system::record_check( + checks.phase, + CheckState::completed(checks.online.len() as u64, elapsed_us, cpu_us, failures), + tsc_hz, + lapic_hz, + ) } /// Validates the Hyper-V CPUID identity leaves on one CPU, of `possible` possible CPUs. @@ -678,29 +646,7 @@ fn check_daemon() -> Verdict { /// Validates the soft-lockup and hung-task detectors when the kernel has either. fn check_debug_watchdogs() -> Verdict { - // A missing or unreadable setting prints as -1, as in the C program. - let setting = - |path| sys::path_exists(path).then(|| sys::read_number::(path).unwrap_or(-1)); - let (soft, hung) = ( - setting(kernel::SOFT_WATCHDOG), - setting(kernel::HUNG_TASK_TIMEOUT), - ); - if soft.is_none() && hung.is_none() { - return Ok(()); - } - if soft != Some(1) { - return Err(Failure::new( - CheckId::C11, - format!("soft_watchdog is {}", soft.unwrap_or(-1)), - )); - } - match hung { - Some(timeout) if timeout != 0 => Ok(()), - _ => Err(Failure::new( - CheckId::C11, - format!("hung_task_timeout_secs is {}", hung.unwrap_or(-1)), - )), - } + super::system::check_debug_watchdogs().map_err(|detail| Failure::new(CheckId::C11, detail)) } /// Runs selected identity and MSR checks on one pinned CPU. @@ -959,19 +905,7 @@ pub(crate) fn run_restore_checks(runtime: &Runtime<'_>, checks: &mut Checks, new } /// Waits for boot-only or all persisted checks and returns completion state. -pub(crate) fn wait_for_checks(boot_only: bool) -> (TimeState, bool) { - let deadline = Instant::now() + STATUS_WAIT; - loop { - let state = state_load().unwrap_or_default(); - if !state.pending(boot_only) { - return (state, true); - } - if Instant::now() >= deadline { - return (state, false); - } - thread::sleep(STATUS_POLL); - } -} +pub(crate) use super::state::wait_for_checks; /// Aggregate counters and rates for the exhaustive CI-only report. struct Exhaustive { diff --git a/nvx-rs/src/guest/time/checks/tests.rs b/nvx-rs/src/guest/time/checks/tests.rs index 94db9580d..1009d139d 100644 --- a/nvx-rs/src/guest/time/checks/tests.rs +++ b/nvx-rs/src/guest/time/checks/tests.rs @@ -1,4 +1,5 @@ use super::*; +use crate::guest::time::deferred::{STATUS_POLL, STATUS_WAIT}; #[test] fn the_daemon_is_recognized_by_the_name_that_it_takes() { diff --git a/nvx-rs/src/guest/time/checks_arm64.rs b/nvx-rs/src/guest/time/checks_arm64.rs new file mode 100644 index 000000000..61f17e64d --- /dev/null +++ b/nvx-rs/src/guest/time/checks_arm64.rs @@ -0,0 +1,239 @@ +//! Architectural-counter and Linux timer checks for the shared time lifecycle. + +use std::time::Instant; + +use super::Phase; +use super::deferred::{TIMER_LIST_POLL, TIMER_LIST_WAIT}; +use super::events::{CheckId, ViolationCode}; +use super::sample::arm64::{Clock, Error}; +use super::state::CheckState; +use super::system::{self, Runtime, SampleResult}; +use crate::guest::kernel_text::{self, ClockEventState, TickMode, TimerList}; +use crate::guest::sys::{self, Errno, aarch64, kernel}; + +pub(crate) use super::state::wait_for_checks; + +/// Evidence passed from architectural checks to the common phase publisher. +pub(crate) struct Checks { + /// Phase whose failures are being reported. + phase: Phase, + /// Actual failed checks, including report-only failures. + pub(crate) failures: usize, + /// Actual number of online processors checked. + capacity: u64, + /// Architected-counter Hz, kept in the compatible state format's frequency slot. + pub(crate) tsc_hz: u64, + /// No local APIC exists on ARM; the compatible state slot remains zero. + pub(crate) lapic_hz: u64, +} + +impl Checks { + /// Starts one phase without claiming that any architectural check passed. + pub(crate) fn empty(phase: Phase) -> Self { + Self { + phase, + failures: 0, + capacity: 0, + tsc_hz: 0, + lapic_hz: 0, + } + } + + /// Reads the actual online processor inventory before deferred checks. + pub(crate) fn load(phase: Phase) -> Result { + let text = sys::read_file(kernel::ONLINE_CPUS)?; + let cpus = kernel_text::parse_cpu_list(text.strip_suffix(b"\n").unwrap_or(&text)) + .ok_or(Errno::EPROTO)?; + let capacity = cpus.into_iter().flatten().count(); + if capacity == 0 || capacity > 8 { + return Err(Errno::EPROTO); + } + Ok(Self { + capacity: capacity as u64, + ..Self::empty(phase) + }) + } + + /// Counts and reports the same conformance failure as the x86 lifecycle. + pub(crate) fn fail( + &mut self, + runtime: &Runtime<'_>, + id: CheckId, + detail: impl AsRef<[u8]>, + ) -> bool { + self.failures += 1; + runtime.conformance_failure(ViolationCode::Conformance(id), self.phase, detail.as_ref()); + false + } +} + +/// Publishes actual failures through the shared phase/state implementation. +pub(crate) fn record_check( + runtime: &Runtime<'_>, + checks: &Checks, + elapsed_us: i64, + cpu_us: i64, + counter_hz: u64, + timer_hz: u64, +) -> Result<(), Errno> { + system::record_check( + checks.phase, + CheckState::completed( + checks.capacity, + elapsed_us, + cpu_us, + runtime.report_only().then_some(checks.failures as u64), + ), + counter_hz, + timer_hz, + ) +} + +/// Checks architectural mechanisms, not a vendor name or a raw CPU-profile allowlist. +pub(crate) fn run_boot_checks(runtime: &Runtime<'_>, checks: &mut Checks) { + let clock = match Clock::open() { + Ok(clock) => clock, + Err(error) => { + let id = if matches!(error, Error::Clocksource(_)) { + CheckId::C6 + } else { + CheckId::C3 + }; + checks.fail(runtime, id, error.to_string()); + return; + } + }; + checks.tsc_hz = clock.binding.counter_hz; + let online = sys::read_file(kernel::ONLINE_CPUS) + .map_err(|error| error.to_string()) + .and_then(|text| online_cpus(&text, clock.binding.capacity)); + match online { + Ok(cpus) => { + checks.capacity = cpus.len() as u64; + if let Err(error) = check_counters(runtime, &clock, &cpus) { + checks.fail(runtime, CheckId::C3, error); + } + } + Err(error) => { + checks.fail(runtime, CheckId::C6, error); + } + } + if let Err(error) = check_timers(clock.binding.capacity) { + checks.fail(runtime, CheckId::C7, error); + } + let stall = || -> Result<(), String> { + let suppressed = sys::read_number::(kernel::RCU_STALL_SUPPRESS) + .map_err(|error| format!("RCU stall suppression: {error}"))?; + let timeout = sys::read_number::(kernel::RCU_STALL_TIMEOUT) + .map_err(|error| format!("RCU stall timeout: {error}"))?; + if suppressed != 0 || timeout != system::DEFAULT_STALL_TIMEOUT_S as u32 { + return Err(format!( + "RCU stall policy suppression={suppressed} timeout={timeout}, expected 0/21" + )); + } + Ok(()) + }; + if let Err(error) = stall() { + checks.fail(runtime, CheckId::C8, error); + } + if let Err(error) = system::check_debug_watchdogs() { + checks.fail(runtime, CheckId::C11, error); + } +} + +/// Checks every retained CPU's effective counter and shared-counter pairing. +fn check_counters(runtime: &Runtime<'_>, clock: &Clock, cpus: &[usize]) -> Result<(), String> { + let _affinity = + sys::AffinityGuard::save().map_err(|error| format!("save affinity: {error}"))?; + for &cpu in cpus { + sys::pin_current_thread(cpu).map_err(|error| format!("pin CPU {cpu}: {error}"))?; + let frequency = aarch64::counter_frequency_hz() + .map_err(|error| format!("CPU {cpu} CNTFRQ: {error}"))?; + if frequency != clock.binding.counter_hz { + return Err(format!( + "CPU {cpu} CNTFRQ {frequency}, expected {}", + clock.binding.counter_hz + )); + } + let before = aarch64::virtual_counter(); + let after = aarch64::virtual_counter(); + if after < before { + return Err(format!( + "CPU {cpu} counter moved backward: {before}..{after}" + )); + } + let (result, sample) = system::best_sample( + runtime, + system::SAMPLE_ATTEMPTS, + system::SAMPLE_BOUND_NS, + runtime.generation(), + ) + .map_err(|error| format!("CPU {cpu} shared-counter pairing: {error}"))?; + if result == SampleResult::Wide { + return Err(String::from_utf8_lossy(&system::wide_sample(sample.epsilon)).into_owned()); + } + } + let current = Clock::open().map_err(|error| error.to_string())?; + if current.binding != clock.binding { + return Err("time binding changed after initial synchronization".into()); + } + Ok(()) +} + +/// Validates Linux numbering against the capacity declared by the admitted machine binding. +fn online_cpus(text: &[u8], capacity: u32) -> Result, String> { + let text = text.strip_suffix(b"\n").unwrap_or(text); + let ranges = kernel_text::parse_cpu_list(text).ok_or("malformed online CPU list")?; + let cpus = ranges.into_iter().flatten().collect::>(); + if cpus != (0..capacity as usize).collect::>() { + return Err(format!( + "online CPUs {cpus:?}, declared capacity {capacity}" + )); + } + Ok(cpus) +} + +/// Uses the standard Linux timer-list interface for every online architectural timer. +fn check_timers(capacity: u32) -> Result<(), String> { + let deadline = Instant::now() + TIMER_LIST_WAIT; + loop { + let text = + sys::read_file(kernel::TIMER_LIST).map_err(|error| format!("timer list: {error}"))?; + let timers = TimerList::parse(&text); + match check_timer_list(&timers, capacity) { + Ok(()) => return Ok(()), + Err(error) if Instant::now() >= deadline => return Err(error), + Err(_) => std::thread::sleep(TIMER_LIST_POLL), + } + } +} + +/// Requires the same architectural high-resolution clock-event state on every CPU. +fn check_timer_list(timers: &TimerList, capacity: u32) -> Result<(), String> { + for cpu in 0..capacity as usize { + let tick = timers + .tick_devices + .get(&cpu) + .ok_or_else(|| format!("CPU {cpu} has no tick device"))?; + if tick.device.as_deref() != Some("arch_sys_timer") + || tick.tick_mode() != Some(TickMode::Oneshot) + || !matches!( + tick.clock_event_state(), + Some(ClockEventState::Oneshot | ClockEventState::OneshotStopped) + ) + || timers + .timer_bases + .get(&cpu) + .and_then(|base| base.hres_active) + != Some(1) + { + return Err(format!( + "CPU {cpu} lacks the active high-resolution architectural timer: {tick:?}" + )); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/guest/time/checks_arm64/tests.rs b/nvx-rs/src/guest/time/checks_arm64/tests.rs new file mode 100644 index 000000000..2208365be --- /dev/null +++ b/nvx-rs/src/guest/time/checks_arm64/tests.rs @@ -0,0 +1,40 @@ +use super::*; +use crate::guest::kernel_text::{TickDevice, TimerBase}; + +#[test] +fn online_capacity_checks_neither_guess_cpu_numbers_nor_ignore_duplicates() { + assert_eq!(online_cpus(b"0-3\n", 4).unwrap(), [0, 1, 2, 3]); + for text in [b"0-2".as_slice(), b"0,0,1,2", b"1-4", b"0-3,4", b"bad"] { + assert!(online_cpus(text, 4).is_err(), "{text:?}"); + } +} + +#[test] +fn every_cpu_needs_the_standard_architectural_high_resolution_timer() { + let mut list = TimerList::default(); + for cpu in 0..2 { + list.tick_devices.insert( + cpu, + TickDevice { + mode: Some(1), + device: Some("arch_sys_timer".into()), + state: Some(3), + handler: Some("hrtimer_interrupt".into()), + }, + ); + list.timer_bases.insert( + cpu, + TimerBase { + hres_active: Some(1), + nohz: Some(1), + }, + ); + } + assert!(check_timer_list(&list, 2).is_ok()); + list.tick_devices.get_mut(&1).unwrap().state = Some(4); + assert!(check_timer_list(&list, 2).is_ok()); + list.tick_devices.get_mut(&1).unwrap().device = Some("unrelated_timer".into()); + assert!(check_timer_list(&list, 2).is_err()); + list.tick_devices.remove(&1); + assert!(check_timer_list(&list, 2).is_err()); +} diff --git a/nvx-rs/src/guest/time/commands.rs b/nvx-rs/src/guest/time/commands.rs index 79754c3bb..a09a5463d 100644 --- a/nvx-rs/src/guest/time/commands.rs +++ b/nvx-rs/src/guest/time/commands.rs @@ -1,27 +1,44 @@ //! `nvx-time`'s commands, which run the time ABI's typed calls or the program's own steps. +#[cfg(target_arch = "x86_64")] use std::ffi::OsStr; -use std::fmt::{Display, Write as _}; +use std::fmt::Display; +#[cfg(target_arch = "x86_64")] +use std::fmt::Write as _; use std::io::Write; +#[cfg(target_arch = "x86_64")] use std::os::unix::ffi::OsStrExt; +#[cfg(target_arch = "x86_64")] use std::path::PathBuf; +#[cfg(target_arch = "x86_64")] +use super::NSEC_PER_USEC; +#[cfg(target_arch = "x86_64")] use super::api::{ self, CaptureOutcome, CaptureRequest, CpuSet, PreCaptureError, RestoreFinish, ScratchPolicy, - TimeAbi, TimeError, }; +use super::api::{TimeAbi, TimeError}; use super::checks::{self, Checks}; +#[cfg(target_arch = "x86_64")] use super::cpu_mask::CpuMask; +#[cfg(target_arch = "x86_64")] use super::decoders::{self, GENERATION_ID_SIZE}; use super::events::CheckId; -use super::system::{self, PORTB_TIME_WINDOW, Runtime, clock_ns, cpu_time_ns}; -use super::{NSEC_PER_USEC, Phase, USAGE}; +use super::system::{self, Runtime}; +#[cfg(target_arch = "x86_64")] +use super::system::{clock_ns, cpu_time_ns}; +use super::{Phase, USAGE}; +#[cfg(target_arch = "x86_64")] use crate::guest::GenerationId; +#[cfg(target_arch = "x86_64")] use crate::guest::parse::parse_decimal; use crate::guest::status::CommandError; -use crate::guest::sys::{self, Port, PortError}; +use crate::guest::sys; +#[cfg(target_arch = "x86_64")] +use crate::guest::sys::PortError; /// The capture requests that `capture` accepts, and writes to the snapshot port: 0 or 1. +#[cfg(target_arch = "x86_64")] const CAPTURE_REQUESTS: std::ops::RangeInclusive = 0..=1; /// Writes one command diagnostic assembled from raw byte slices. @@ -37,6 +54,7 @@ fn usage(err: &mut dyn Write, line: &[u8]) -> Result<(), CommandError> { /// Reports why the thread could not take, or give up, its permission for the time ABI's ports, /// such as `nvx-time: ioperm: Operation not permitted`, and fails. +#[cfg(target_arch = "x86_64")] fn port_failure(err: &mut dyn Write, error: PortError) -> CommandError { report(err, &[format!("nvx-time: {error}\n").as_bytes()]); CommandError::Operation @@ -47,7 +65,12 @@ fn port_failure(err: &mut dyn Write, error: PortError) -> CommandError { /// that the thread may still hold once, when the command ends. fn time_failure(err: &mut dyn Write, error: TimeError) -> CommandError { match error { - TimeError::Violation(_) | TimeError::PortRelease(_) => {} + TimeError::Violation(_) => {} + #[cfg(target_arch = "x86_64")] + TimeError::PortRelease(_) => {} + #[cfg(target_arch = "aarch64")] + TimeError::PlatformRelease(_) => {} + #[cfg(target_arch = "x86_64")] TimeError::EntropyFile { path, source } => report( err, &[ @@ -71,7 +94,10 @@ fn cmd_boot(abi: TimeAbi, runtime: &Runtime<'_>, err: &mut dyn Write) -> Result< Err(TimeError::Spawn(error)) => Err(boot_checks_unstarted(runtime, sys::describe(&error))), // The step ran, but the thread may still hold port permission, which the daemon would // inherit. + #[cfg(target_arch = "x86_64")] Err(error @ TimeError::PortRelease(_)) => Err(boot_checks_unstarted(runtime, error)), + #[cfg(target_arch = "aarch64")] + Err(error @ TimeError::PlatformRelease(_)) => Err(boot_checks_unstarted(runtime, error)), Err(error) => Err(time_failure(err, error)), } } @@ -88,6 +114,7 @@ fn boot_checks_unstarted(runtime: &Runtime<'_>, reason: impl Display) -> Command } /// Runs explicit capture or restore conformance checks. +#[cfg(target_arch = "x86_64")] fn cmd_check( runtime: &Runtime<'_>, arguments: &[&[u8]], @@ -146,6 +173,7 @@ fn cmd_check( /// requires, runs the capture checks, and suppresses the stall detectors before the snapshot. /// A preparation that fails leaves the detectors as they were, as far as it can: the snapshot /// script cancels only a capture whose preparation succeeded. +#[cfg(target_arch = "x86_64")] fn cmd_pre_capture(abi: TimeAbi, err: &mut dyn Write) -> Result<(), CommandError> { abi.pre_capture().map_err(|error| { if error.holds_suppression() { @@ -158,6 +186,7 @@ fn cmd_pre_capture(abi: TimeAbi, err: &mut dyn Write) -> Result<(), CommandError /// Reports why the preparation of a capture failed, such as `nvx-time: cannot suppress the stall /// detectors: a capture is already pending`, and fails. A violation's event is its only report, /// and `run` reports a permission that the thread may still hold once, when the command ends. +#[cfg(target_arch = "x86_64")] fn pre_capture_failure(err: &mut dyn Write, error: PreCaptureError) -> CommandError { let reason = match error { PreCaptureError::BootChecks(_) @@ -180,6 +209,7 @@ fn pre_capture_failure(err: &mut dyn Write, error: PreCaptureError) -> CommandEr } /// Cancels capture by restoring saved stall-detector settings. +#[cfg(target_arch = "x86_64")] fn cmd_cancel_capture(abi: TimeAbi, err: &mut dyn Write) -> Result<(), CommandError> { abi.cancel_capture().map_err(|detail| { report(err, &[b"nvx-time: ", &detail, b"\n"]); @@ -188,6 +218,7 @@ fn cmd_cancel_capture(abi: TimeAbi, err: &mut dyn Write) -> Result<(), CommandEr } /// Parses a capture request, 0 or 1, as plain decimal. +#[cfg(target_arch = "x86_64")] fn parse_capture_request(text: &[u8]) -> Option { parse_decimal(text) .filter(|request| CAPTURE_REQUESTS.contains(request)) @@ -197,6 +228,7 @@ fn parse_capture_request(text: &[u8]) -> Option { /// Reads the request of `capture 0|1 GENERATION_ID ENTROPY_FILE [--finish]`, in the file mode, /// in which the restore's entropy goes to `ENTROPY_FILE`, or returns `None` for an invalid /// command line. +#[cfg(target_arch = "x86_64")] fn capture_request(arguments: &[&[u8]]) -> Option { let (request, previous_id, entropy_path, finish) = match arguments { [request, id, path] => (request, id, path, false), @@ -224,6 +256,7 @@ fn capture_request(arguments: &[&[u8]]) -> Option { /// flags, online VP count, range count, and new generation ID, the DNS server of its network /// settings, or `-` for none, `1` if they ask the guest to announce its listeners, or else `0`, /// and each range's start and length. +#[cfg(target_arch = "x86_64")] fn cmd_capture( abi: TimeAbi, arguments: &[&[u8]], @@ -266,6 +299,7 @@ fn cmd_capture( /// Records new CPUs, acknowledges the restore when asked, and hands repair step 7 to the daemon /// or runs it. +#[cfg(target_arch = "x86_64")] fn cmd_restore_finish( abi: TimeAbi, arguments: &[&[u8]], @@ -289,8 +323,18 @@ fn cmd_restore_finish( } /// Prints persisted status after waiting for pending checks. -fn cmd_status(runtime: &Runtime<'_>, out: &mut dyn Write) -> Result<(), CommandError> { - let (state, completed) = checks::wait_for_checks(false); +fn cmd_status( + runtime: &Runtime<'_>, + out: &mut dyn Write, + err: &mut dyn Write, +) -> Result<(), CommandError> { + let (state, completed) = checks::wait_for_checks(false).map_err(|error| { + report( + err, + &[format!("nvx-time: read time state: {error}\n").as_bytes()], + ); + CommandError::Operation + })?; let _ = out.write_all(state.status_markers(runtime.report_only()).as_bytes()); if completed && state.checks_ok() { Ok(()) @@ -306,70 +350,21 @@ fn cmd_sample( out: &mut dyn Write, err: &mut dyn Write, ) -> Result<(), CommandError> { - let rates = system::clock_rates(runtime).map_err(|error| { - report(err, &[format!("nvx-time: {error}\n").as_bytes()]); - CommandError::Operation - })?; - let taken = { - let ports = runtime - .open_ports() - .map_err(|error| port_failure(err, error))?; - let status = ports.read_u8(Port::PortbStatus); - if status & PORTB_TIME_WINDOW == 0 { - report( - err, - &[ - format!("nvx-time: portb status 0x{status:02x} has no time window\n") - .as_bytes(), - ], - ); - return Err(CommandError::Operation); - } - let _lock = system::lock_portb().map_err(|error| { - report( - err, - &[b"nvx-time: ", &system::portb_lock_failure(error), b"\n"], - ); - CommandError::Operation - })?; - system::take_sample(&ports, runtime.ops) - }; - let (sample, reads) = taken.map_err(|error| { - report(err, &[format!("nvx-time: {error}\n").as_bytes()]); + let sample = system::diagnostic_sample(runtime).map_err(|detail| { + report(err, &[b"nvx-time: ", &detail, b"\n"]); CommandError::Operation })?; - let host = &sample.host; - let bound = |bound: Option| bound.map_or_else(|| "unknown".to_owned(), |b| b.to_string()); - let mut line = format!( - "flags=0x{:02x} generation={} status={} utc_ns={} tsc={} pairing_ns={} disruption={} \ - correction={} correction_bound={} max_error_us={} before={} after={} realtime_ns={}", - sample.flags, - sample.generation, - host.status, - sample.utc_ns, - sample.tsc, - sample.pairing_ns, - host.disruption, - host.correction, - bound(host.correction_bound), - bound(host.max_error_us), - reads.before, - reads.after, - reads.realtime_ns - ); - let pairing = decoders::pair_sample(&sample, &reads, rates); - if let Ok((theta, epsilon)) = pairing { - let (frequency, limited) = decoders::guest_frequency(host.correction, rates.tsc_hz); - let _ = write!( - line, - " theta_ns={theta} epsilon_ns={epsilon} frequency={frequency} frequency_limited={}", - u8::from(limited) - ); - } - line.push('\n'); + let (line, pairing) = sample.format(); let _ = out.write_all(line.as_bytes()); - pairing.map(drop).map_err(|error| { - report(err, &[format!("nvx-time: {error}\n").as_bytes()]); + pairing.map_err(|error| { + report( + err, + &[format!( + "nvx-time: sample counter {} outside latch reads {}..{}\n", + error.counter, error.before, error.after + ) + .as_bytes()], + ); CommandError::Operation }) } @@ -398,11 +393,14 @@ pub(super) fn run( out: &mut dyn Write, err: &mut dyn Write, ) -> Result<(), CommandError> { - let abi = if report_only { - TimeAbi::report_only() - } else { - TimeAbi::new() - }; + let command_line = sys::read_file(sys::kernel::COMMAND_LINE).map_err(|error| { + report( + err, + &[format!("nvx-time: read kernel command line: {error}\n").as_bytes()], + ); + CommandError::Operation + })?; + let abi = TimeAbi::with_report_only(super::sample::report_only(&command_line, report_only)); let runtime = abi.runtime(); sys::reset_child_signal(); let Some((&command, rest)) = arguments.split_first() else { @@ -410,22 +408,37 @@ pub(super) fn run( }; let result = match (command, rest) { (b"boot", []) => cmd_boot(abi, &runtime, err), + #[cfg(target_arch = "x86_64")] (b"check", arguments) => cmd_check(&runtime, arguments, err), + #[cfg(target_arch = "x86_64")] (b"pre-capture", []) => cmd_pre_capture(abi, err), + #[cfg(target_arch = "x86_64")] (b"capture", arguments) => cmd_capture(abi, arguments, out, err), + #[cfg(target_arch = "x86_64")] (b"cancel-capture", []) => cmd_cancel_capture(abi, err), + #[cfg(target_arch = "x86_64")] (b"restore-finish", arguments) => cmd_restore_finish(abi, arguments, err), - (b"status", []) => cmd_status(&runtime, out), + (b"status", []) => cmd_status(&runtime, out, err), (b"sample", []) => cmd_sample(&runtime, out, err), (b"generation-id", []) => cmd_generation_id(abi, out, err), + #[cfg(target_arch = "x86_64")] (b"exhaustive", []) => checks::exhaustive(&runtime, out), _ => usage(err, USAGE), }; match sys::check_released() { Ok(()) => result, + #[cfg(target_arch = "x86_64")] Err(error) => Err(port_failure(err, error)), + #[cfg(target_arch = "aarch64")] + Err(error) => { + report( + err, + &[format!("nvx-time: platform release: {error}\n").as_bytes()], + ); + Err(CommandError::Operation) + } } } -#[cfg(test)] +#[cfg(all(test, target_arch = "x86_64"))] mod tests; diff --git a/nvx-rs/src/guest/time/daemon.rs b/nvx-rs/src/guest/time/daemon.rs index b1ea01f6b..a731fe5f9 100644 --- a/nvx-rs/src/guest/time/daemon.rs +++ b/nvx-rs/src/guest/time/daemon.rs @@ -1,38 +1,43 @@ //! The time daemon: deferred work, violation watching, the wall clock's discipline, and repair //! step 7. +#[cfg(target_arch = "x86_64")] use std::ffi::CStr; -use std::sync::mpsc::{self, RecvTimeoutError}; use std::thread; use std::time::Duration; use super::api::ColdBoot; -use super::checks::{self, BootLog, Checks}; +#[cfg(target_arch = "x86_64")] +use super::checks::BootLog; +use super::checks::{self, Checks}; +#[cfg(target_arch = "x86_64")] use super::cpu_mask::CpuMask; +#[cfg(target_arch = "x86_64")] use super::decoders; +pub(crate) use super::deferred::Promoter; use super::events::{CheckId, EventSource, ViolationCode}; +use super::sample; +use super::sample::{FAST_PERIOD, FAST_POLLS, SLOW_PERIOD, SYNCHRONIZED_AGE_NS}; use super::state::{Discontinuity, SampleRejection, TimeState}; use super::system::{ - self, BestSample, DAEMON_NAME, DAEMON_PID_PATH, DAEMON_PID_TEMP_PATH, DEFAULT_STALL_TIMEOUT_S, - PowerOffStatus, RCU_SYNC_PATH, RESTORE_PATH, Runtime, SAMPLE_ATTEMPTS, SAMPLE_BOUND_NS, - SNAPSHOT_ACKNOWLEDGE, SampleError, SampleResult, SpawnError, clock_ns, cpu_time_ns, - fork_process, state_apply, state_load, suppression_restore, + self, BestSample, DAEMON_NAME, DAEMON_PID_PATH, DAEMON_PID_TEMP_PATH, PowerOffStatus, Runtime, + SampleError, SampleResult, clock_ns, cpu_time_ns, state_apply, }; -use super::{DEFERRED_START_NS, IDLE_BOUND_NS, NSEC_PER_MSEC, NSEC_PER_SEC, NSEC_PER_USEC, Phase}; +#[cfg(target_arch = "x86_64")] +use super::system::{ + DEFAULT_STALL_TIMEOUT_S, RCU_SYNC_PATH, RESTORE_PATH, SNAPSHOT_ACKNOWLEDGE, SpawnError, + fork_process, state_load, suppression_restore, +}; +use super::{DEFERRED_START_NS, NSEC_PER_USEC, Phase}; +#[cfg(target_arch = "x86_64")] +use super::{IDLE_BOUND_NS, NSEC_PER_MSEC, NSEC_PER_SEC}; use crate::guest::kernel_text::KmsgRecord; use crate::guest::sys::{ - self, ChildStatus, Errno, FileDescriptor, ForkResult, Mode, OpenFlags, Pollable, Port, - PortAccess, PortError, ProcessId, Signal, SignalDescriptor, Timer, kernel, + self, Errno, FileDescriptor, Mode, OpenFlags, Pollable, Signal, SignalDescriptor, Timer, kernel, }; +#[cfg(target_arch = "x86_64")] +use crate::guest::sys::{ChildStatus, ForkResult, Port, PortAccess, PortError, ProcessId}; -/// Maximum age of the latest accepted sample while synchronized. -const SYNCHRONIZED_AGE_NS: i64 = 128 * NSEC_PER_SEC; -/// Poll interval during initial convergence. -const FAST_PERIOD: Duration = Duration::from_secs(16); -/// Poll interval after initial convergence. -const SLOW_PERIOD: Duration = Duration::from_secs(64); -/// Number of initial fast polls. -const FAST_POLLS: i32 = 4; /// Pause before the daemon polls again after `poll` failed for a reason other than a signal. const POLL_RETRY: Duration = Duration::from_secs(1); /// The largest structured kernel log record, the kernel's `CONSOLE_EXT_LOG_MAX`. @@ -44,71 +49,23 @@ const WATCHED_MESSAGE_LIMIT: usize = 400; const OOM_SCORE_ADJUSTMENT: &[u8] = b"-1000"; /// The signals that the daemon reads from its signal descriptor: a queued restore, a stop, and /// the end of a restore worker. +#[cfg(target_arch = "x86_64")] const DAEMON_SIGNALS: [Signal; 3] = [Signal::SIGUSR1, Signal::SIGTERM, Signal::SIGCHLD]; +/// Cold-boot ARM never acknowledges or schedules a restore. +#[cfg(target_arch = "aarch64")] +const DAEMON_SIGNALS: [Signal; 2] = [Signal::SIGUSR1, Signal::SIGTERM]; /// The signals that the daemon ignores: the hangup of its session, and writes to a closed pipe. const IGNORED_SIGNALS: [Signal; 2] = [Signal::SIGHUP, Signal::SIGPIPE]; /// The source name of the tmpfs that forces an RCU grace period. +#[cfg(target_arch = "x86_64")] const RCU_SYNC_SOURCE: &CStr = c"nvx-rcu-sync"; /// The mount options of that tmpfs: the smallest size, one page. +#[cfg(target_arch = "x86_64")] const RCU_SYNC_OPTIONS: &CStr = c"size=4k"; /// The value of `rcu_normal` that forces normal, rather than expedited, grace periods. +#[cfg(target_arch = "x86_64")] const RCU_NORMAL_FORCED: i64 = 1; -/// Join-on-drop owner of the thread that moves the process's threads from idle to normal -/// scheduling once deferred work has run for `IDLE_BOUND_NS`. -/// -/// Dropping it before then cancels the promotion, and the caller keeps its policy. -pub(crate) struct Promoter { - /// The channel whose disconnection cancels the promotion; it carries nothing. - cancel: Option>, - /// Running promoter thread, until it is joined. - thread: Option>, -} - -impl Promoter { - /// Starts a promoter whose deadline is `IDLE_BOUND_NS` after the monotonic `start_ns`, - /// unless the calling thread may hold port permission, which the promoter would inherit. - pub(crate) fn start(start_ns: i64) -> Option { - sys::check_released().ok()?; - let (cancel, cancelled) = mpsc::channel(); - let deadline = start_ns.wrapping_add(IDLE_BOUND_NS); - let thread = thread::Builder::new() - .name("nvx-time-promote".to_owned()) - .spawn(move || { - let remaining = deadline.wrapping_sub(clock_ns(sys::Clock::Monotonic)); - let timeout = Duration::from_nanos(remaining.max(0) as u64); - if cancelled.recv_timeout(timeout) == Err(RecvTimeoutError::Timeout) { - sys::promote_all_threads(); - } - }) - .ok()?; - Some(Self { - cancel: Some(cancel), - thread: Some(thread), - }) - } - - /// Waits for the promoter to promote the threads at its deadline. - pub(crate) fn finish(mut self) { - if let Some(thread) = self.thread.take() { - let _ = thread.join(); - } - } - - /// Cancels a pending promotion and joins the thread, as dropping the promoter does. - pub(crate) fn stop(self) {} -} - -impl Drop for Promoter { - /// Cancels a promotion that is still pending and joins the thread. - fn drop(&mut self) { - drop(self.cancel.take()); - if let Some(thread) = self.thread.take() { - let _ = thread.join(); - } - } -} - /// Drains structured kernel log records and supplies each one's message. /// /// It returns when no record is left, or with the error of a failed read, such as `EPIPE` for @@ -122,9 +79,8 @@ fn kmsg_drain(log: &FileDescriptor, mut consume: impl FnMut(&[u8])) -> Result<() Err(Errno::EINTR) => continue, Err(error) => return Err(error), }; - if let Some(record) = KmsgRecord::parse(&buffer[..count]) { - consume(record.message); - } + let record = KmsgRecord::parse(&buffer[..count]).ok_or(Errno::EPROTO)?; + consume(record.message); } } @@ -173,6 +129,7 @@ fn apply_poll(state: &mut TimeState, outcome: &PollOutcome, synchronized: bool) /// Repair step 7 work that a queued signal scheduled, which starts once it is due. #[derive(Clone, Copy, Debug)] +#[cfg(target_arch = "x86_64")] struct PendingRestore { /// Monotonic time at which the work may start. due_ns: i64, @@ -182,6 +139,7 @@ struct PendingRestore { /// The child that runs repair step 7, and its outstanding deadlines on the monotonic clock. #[derive(Clone, Copy, Debug)] +#[cfg(target_arch = "x86_64")] struct RestoreWorker { /// The worker process. process: ProcessId, @@ -204,8 +162,10 @@ pub(crate) struct Daemon { /// Monotonic time of the latest accepted sample, if any. last_accepted_ns: Option, /// Restore work that awaits its start. + #[cfg(target_arch = "x86_64")] restore: Option, /// The running restore worker. + #[cfg(target_arch = "x86_64")] worker: Option, } @@ -221,7 +181,7 @@ fn arm_timer(daemon: &Daemon) -> Result<(), Errno> { /// Drains new kernel messages and raises matching runtime violations. fn watch_kernel_log(runtime: &Runtime<'_>, daemon: &Daemon) { let result = kmsg_drain(&daemon.kmsg, |message| { - if let Some(code) = decoders::match_watch_rule(message) { + if let Some(code) = super::events::match_watch_rule(message) { runtime.fail_fatal( PowerOffStatus::Violation, code, @@ -231,20 +191,78 @@ fn watch_kernel_log(runtime: &Runtime<'_>, daemon: &Daemon) { ); } }); - if result == Err(Errno::EPIPE) { + if let Err(error) = result { + let (code, detail) = if error == Errno::EPIPE { + ( + ViolationCode::KmsgOverrun, + "kernel log records were lost (EPIPE)".into(), + ) + } else { + ( + ViolationCode::Conformance(CheckId::C4), + format!("read kernel watcher: {error}"), + ) + }; runtime.fail_fatal( PowerOffStatus::Violation, - ViolationCode::KmsgOverrun, + code, EventSource::Watcher, Phase::Runtime, - b"kernel log records were lost (EPIPE)", + detail.as_bytes(), + ); + } +} + +/// Reads ARM's historical structured boot log through the same drain and event policy. +#[cfg(target_arch = "aarch64")] +fn check_boot_log(runtime: &Runtime<'_>, checks: &mut Checks) { + let mut read = || -> Result<(), Errno> { + let log = sys::open( + kernel::KERNEL_LOG_DEVICE, + OpenFlags::O_RDONLY | OpenFlags::O_NONBLOCK | OpenFlags::O_CLOEXEC, + Mode::empty(), + )?; + kmsg_drain(&log, |message| { + if message.starts_with(b"NVX-TIME-") { + return; + } + let code = + if crate::guest::kernel_text::contains(message, b"Found insecure W+X mapping") { + Some(ViolationCode::KernelWx) + } else { + super::events::match_watch_rule(message) + .filter(|code| *code != ViolationCode::ClocksourceSwitch) + }; + if let Some(code) = code { + checks.failures += 1; + runtime.conformance_failure(code, Phase::Boot, message); + } + }) + }; + if let Err(error) = read() { + checks.fail( + runtime, + CheckId::C4, + format!("read historical boot log: {error}"), ); } } /// Takes one host sample and updates Linux clock discipline and state. fn discipline_poll(runtime: &Runtime<'_>, daemon: &mut Daemon) { - let stalls = sys::read_number::(kernel::RCU_STALL_COUNT).unwrap_or(0); + let stalls = match sys::read_number::(kernel::RCU_STALL_COUNT) { + Ok(stalls) => stalls, + Err(error) => { + runtime.fail_fatal( + PowerOffStatus::Violation, + ViolationCode::Conformance(CheckId::C8), + EventSource::Watcher, + Phase::Runtime, + format!("RCU stall count: {error}").as_bytes(), + ); + return; + } + }; if stalls != 0 { runtime.fail_fatal( PowerOffStatus::Violation, @@ -258,28 +276,45 @@ fn discipline_poll(runtime: &Runtime<'_>, daemon: &mut Daemon) { daemon.polls += 1; // The port-B lock serializes the sample with the other port transactions, and the state // update with the sample. - let lock = system::lock_portb().ok(); - let mut outcome = PollOutcome::Rejected(SampleRejection::Invalid); - if lock.is_some() { - let sample = system::best_sample( - runtime, - SAMPLE_ATTEMPTS, - SAMPLE_BOUND_NS, - runtime.generation(), - ); - outcome = match sample { - Ok((SampleResult::Accepted, sample)) => correct_clock(runtime, sample, fast), - Ok((SampleResult::Wide, _)) => PollOutcome::Rejected(SampleRejection::Uncertain), - Err(SampleError::Generation { .. }) => return, - Err(error) => { - // Nothing else would report it, and the daemon goes on without the sample. - if let SampleError::Ports(error @ PortError::Revoke(_)) = error { - runtime.log_release_failure(error); - } - PollOutcome::Rejected(error.rejection()) + let (selected, lock) = system::discipline_sample(runtime); + let outcome = match selected { + Ok((SampleResult::Accepted, sample)) => correct_clock(runtime, sample, fast), + Ok((SampleResult::Wide, _)) => PollOutcome::Rejected(SampleRejection::Uncertain), + #[cfg(target_arch = "x86_64")] + Err(SampleError::Generation { .. }) => return, + #[cfg(target_arch = "aarch64")] + Err( + error @ (SampleError::Generation { .. } + | SampleError::Arm( + sample::arm64::Error::Frequency { .. } + | sample::arm64::Error::Clocksource(_) + | sample::arm64::Error::Binding(_), + )), + ) => { + let id = match &error { + SampleError::Arm(sample::arm64::Error::Clocksource(_)) => CheckId::C6, + SampleError::Generation { .. } => CheckId::C10, + _ => CheckId::C3, + }; + runtime.fail_fatal( + PowerOffStatus::Violation, + ViolationCode::Conformance(id), + EventSource::Watcher, + Phase::Runtime, + error.to_string().as_bytes(), + ); + PollOutcome::Rejected(error.rejection()) + } + Err(error) => { + // Nothing else would report it, and the daemon goes on without the sample. + #[cfg(target_arch = "x86_64")] + if let SampleError::Ports(error @ PortError::Revoke(_)) = error { + runtime.log_release_failure(error); } - }; - } + runtime.log_sample_failure(&error); + PollOutcome::Rejected(error.rejection()) + } + }; if matches!(outcome, PollOutcome::Accepted { .. }) { daemon.last_accepted_ns = Some(clock_ns(sys::Clock::Monotonic)); } @@ -287,7 +322,15 @@ fn discipline_poll(runtime: &Runtime<'_>, daemon: &mut Daemon) { clock_ns(sys::Clock::Monotonic).wrapping_sub(accepted_ns) <= SYNCHRONIZED_AGE_NS }); let mut disrupted = false; - let _ = state_apply(|state| disrupted = apply_poll(state, &outcome, synchronized)); + if let Err(error) = state_apply(|state| disrupted = apply_poll(state, &outcome, synchronized)) { + runtime.fail_fatal( + PowerOffStatus::Violation, + ViolationCode::Conformance(CheckId::C10), + EventSource::Watcher, + Phase::Runtime, + format!("publish discipline state: {error}").as_bytes(), + ); + } // A disruption restarts the discipline at the fast cadence, as a restore does. if disrupted { daemon.polls = 0; @@ -300,7 +343,7 @@ fn discipline_poll(runtime: &Runtime<'_>, daemon: &mut Daemon) { /// clock correction asks for, or slews it while holding that frequency. fn correct_clock(runtime: &Runtime<'_>, sample: BestSample, fast: bool) -> PollOutcome { let host_error_us = sample.host.error_us(); - let correction = decoders::plan_correction( + let correction = sample::plan_correction( sample.theta, sample.epsilon, fast, @@ -308,10 +351,16 @@ fn correct_clock(runtime: &Runtime<'_>, sample: BestSample, fast: bool) -> PollO host_error_us, ); let stepped = matches!(correction, sys::ClockAdjustment::Step { .. }); - let hold = decoders::frequency_hold(0, sample.epsilon, sample.frequency, host_error_us); - let applied = runtime.ops.adjust_clock(correction).is_ok() - && (!stepped || system::hold_frequency(runtime, hold).is_ok()); - if !applied { + let hold = sample::frequency_hold(0, sample.epsilon, sample.frequency, host_error_us); + let applied = runtime.ops.adjust_clock(correction).and_then(|_| { + if stepped { + system::hold_frequency(runtime, hold) + } else { + Ok(()) + } + }); + if let Err(error) = applied { + runtime.log_sample_failure(&format!("adjtimex: {error}")); return PollOutcome::Rejected(SampleRejection::Invalid); } PollOutcome::Accepted { @@ -322,6 +371,7 @@ fn correct_clock(runtime: &Runtime<'_>, sample: BestSample, fast: bool) -> PollO /// A kernel setting forced to a value while the owner lives. Dropping the owner restores the /// setting's saved value, best effort, unless [`restore`](Self::restore) did. +#[cfg(target_arch = "x86_64")] struct ForcedSetting { /// The setting's file. path: &'static CStr, @@ -329,6 +379,7 @@ struct ForcedSetting { saved: Option, } +#[cfg(target_arch = "x86_64")] impl ForcedSetting { /// Saves the setting at `path` and forces it to `value`, or returns `None` when either /// fails, which leaves the setting unchanged. @@ -349,6 +400,7 @@ impl ForcedSetting { } } +#[cfg(target_arch = "x86_64")] impl Drop for ForcedSetting { /// Restores the saved value, best effort, unless `restore` did. fn drop(&mut self) { @@ -360,6 +412,7 @@ impl Drop for ForcedSetting { /// Forces an RCU grace period with a tmpfs mount and unmount in a private mount namespace, /// under normal grace periods, and returns whether every step succeeded. +#[cfg(target_arch = "x86_64")] fn grace_period_release() -> bool { let Some(normal) = ForcedSetting::force(kernel::RCU_NORMAL, RCU_NORMAL_FORCED) else { return false; @@ -375,6 +428,7 @@ fn grace_period_release() -> bool { /// Runs the grace-period release with a deadline of `timeout_s` seconds, after which the /// release counts as done, so that a stalled release cannot hold the suppression back. +#[cfg(target_arch = "x86_64")] fn release_with_deadline(timeout_s: i64) -> bool { run_with_deadline(timeout_s, grace_period_release) } @@ -389,6 +443,7 @@ fn release_with_deadline(timeout_s: i64) -> bool { /// passes to init, or to the nearest subreaper, as when the caller is `nvx-time restore-finish` /// and exits. The work holds none of the caller's descriptors, which it would keep open after /// the call, and has the null device as its standard streams. +#[cfg(target_arch = "x86_64")] fn run_with_deadline(timeout_s: i64, work: fn() -> bool) -> bool { let Ok((reader, writer)) = sys::create_pipe() else { return false; @@ -412,6 +467,7 @@ fn run_with_deadline(timeout_s: i64, work: fn() -> bool) -> bool { /// The waiter of [`run_with_deadline`]: restores the default disposition of `SIGCHLD`, so that /// it reaps the work itself, runs `work` in a child, waits for it for at most `timeout_s` /// seconds, after which it counts as done, and writes whether it succeeded to `verdict`. +#[cfg(target_arch = "x86_64")] fn run_waiter(timeout_s: i64, work: fn() -> bool, verdict: FileDescriptor) -> ! { sys::reset_child_signal(); let deadline = clock_ns(sys::Clock::Monotonic).wrapping_add(timeout_s * NSEC_PER_SEC); @@ -436,6 +492,7 @@ fn run_waiter(timeout_s: i64, work: fn() -> bool, verdict: FileDescriptor) -> ! /// Waits for the work `process` until the monotonic `deadline`, after which it counts as done, /// and returns whether it succeeded. +#[cfg(target_arch = "x86_64")] fn await_work(process: ProcessId, deadline: i64, wait: &sys::ChildSignalWait) -> bool { loop { let remaining = deadline.wrapping_sub(clock_ns(sys::Clock::Monotonic)); @@ -457,6 +514,7 @@ fn await_work(process: ProcessId, deadline: i64, wait: &sys::ChildSignalWait) -> /// alone: this process's since `cpu_base_ns`, plus the daemon's preparation, `prior_cpu_ns` up /// to the fork, and the fork's own share, which the daemon writes to `fork_reader` once the /// fork returns. +#[cfg(target_arch = "x86_64")] pub(crate) struct RestoreTiming { /// Monotonic start used for elapsed accounting. pub(crate) start_ns: i64, @@ -472,6 +530,7 @@ pub(crate) struct RestoreTiming { /// Repair step 7's CPU time so far, in a process that `timing` describes. The read waits for /// the daemon's fork share if the daemon has not yet written it. +#[cfg(target_arch = "x86_64")] pub(crate) fn deferred_restore_cpu_ns(timing: &RestoreTiming) -> i64 { let mut cpu = timing .prior_cpu_ns @@ -494,6 +553,7 @@ pub(crate) fn deferred_restore_cpu_ns(timing: &RestoreTiming) -> i64 { /// returns to normal priority as soon as the fork does, after `at_fork`, which stands for a /// `pthread_atfork` parent handler. The fork costs the parent CPU time after the child's copy /// of `timing` was taken, so the parent writes that share to a pipe that the child reads. +#[cfg(target_arch = "x86_64")] pub(crate) fn fork_deferred_restore( cpu_start_ns: i64, timing: &mut RestoreTiming, @@ -535,6 +595,7 @@ pub(crate) fn fork_deferred_restore( /// timeout. `restore-finish` runs it inline, with its own deadline (`with_deadline`), in /// report-only mode when no daemon runs. Neither recorded time counts the grace period wait or /// C7. Returns whether the grace period was released. +#[cfg(target_arch = "x86_64")] pub(crate) fn restore_work( runtime: &Runtime<'_>, new_cpus: CpuMask, @@ -589,6 +650,7 @@ pub(crate) fn restore_work( /// Reads the private restore record, and returns the CPUs that the restore brought online, none /// for a missing record or one of another generation, and the recorded generation. +#[cfg(target_arch = "x86_64")] fn parse_restore_state() -> Result<(CpuMask, u64), Vec> { let state = state_load().map_err(|_| b"the time state is unreadable".to_vec())?; let Ok(text) = sys::read_file(RESTORE_PATH) else { @@ -608,6 +670,7 @@ fn parse_restore_state() -> Result<(CpuMask, u64), Vec> { /// acknowledgement, in a child at `SCHED_IDLE`, so the watcher and the discipline keep normal /// priority; the fork itself also runs at `SCHED_IDLE`. The readiness path's wall time came /// with the signal. +#[cfg(target_arch = "x86_64")] fn start_restore(runtime: &Runtime<'_>, daemon: &mut Daemon, restore: PendingRestore) { let cpu_start = cpu_time_ns(); let (new_cpus, generation) = match parse_restore_state() { @@ -650,6 +713,7 @@ fn start_restore(runtime: &Runtime<'_>, daemon: &mut Daemon, restore: PendingRes /// Restores the saved stall-detector settings when repair step 7 ends, or when its worker passes /// its deadline or fails, and reports a repair failure if it cannot. +#[cfg(target_arch = "x86_64")] fn release_suppression(runtime: &Runtime<'_>) { if let Err(detail) = suppression_restore(true) { runtime.repair_failure(ViolationCode::RepairSuppression, &detail); @@ -657,6 +721,7 @@ fn release_suppression(runtime: &Runtime<'_>) { } /// Reaps every available child and handles the active restore worker. +#[cfg(target_arch = "x86_64")] fn reap_children(runtime: &Runtime<'_>, daemon: &mut Daemon) { while let Some(child) = sys::reap_any_nonblocking() { if daemon.worker.map(|worker| worker.process) != Some(child.process) { @@ -688,20 +753,35 @@ fn handle_signals(runtime: &Runtime<'_>, daemon: &mut Daemon) { // `sigqueue` carries the readiness path's wall time. Step 7 is due // `DEFERRED_START_NS` later; the repair stepped the clock to a time sample now. Some(Signal::SIGUSR1) => { - let now = clock_ns(sys::Clock::Monotonic); - daemon.last_accepted_ns = Some(now); - daemon.restore = Some(PendingRestore { - due_ns: now.wrapping_add(DEFERRED_START_NS), - readiness_us: if event.queued { - i64::from(event.value) - } else { - 0 - }, - }); + #[cfg(target_arch = "x86_64")] + { + let now = clock_ns(sys::Clock::Monotonic); + daemon.last_accepted_ns = Some(now); + daemon.restore = Some(PendingRestore { + due_ns: now.wrapping_add(DEFERRED_START_NS), + readiness_us: if event.queued { + i64::from(event.value) + } else { + 0 + }, + }); + } + #[cfg(target_arch = "aarch64")] + { + runtime.fail_fatal( + PowerOffStatus::Violation, + ViolationCode::Conformance(CheckId::C10), + EventSource::Watcher, + Phase::Runtime, + b"ARM cold boot does not support restore repair", + ); + sys::exit_immediately(1); + } } _ => {} } } + #[cfg(target_arch = "x86_64")] reap_children(runtime, daemon); } @@ -716,7 +796,9 @@ fn setup_daemon(kmsg: FileDescriptor, last_accepted_ns: Option) -> Result (Checks, Option) { - let _promoter = Promoter::start(start_ns); + let _promoter = match Promoter::start(start_ns) { + Some(promoter) => promoter, + None => { + runtime.conformance_failure( + ViolationCode::Conformance(CheckId::C10), + Phase::Boot, + b"cannot start deferred priority promoter", + ); + sys::exit_immediately(1); + } + }; let _idle = sys::IdlePriority::enter(); sys::sleep_until_monotonic(start_ns); let mut checks = match Checks::load(Phase::Boot) { Ok(checks) => checks, Err(_) => { let mut checks = Checks::empty(Phase::Boot); - checks.fail(runtime, CheckId::C1, "cannot read the CPU sets"); + #[cfg(target_arch = "x86_64")] + let id = CheckId::C1; + #[cfg(target_arch = "aarch64")] + let id = CheckId::C6; + checks.fail(runtime, id, "cannot read the CPU sets"); sys::exit_immediately(1); } }; checks.failures = c12_failures; // The buffer is allocated before the checks start their threads, as the C program did. + #[cfg(target_arch = "x86_64")] let log_size = sys::kernel::kernel_log_size(); + #[cfg(target_arch = "x86_64")] let mut log_buffer = Vec::new(); + #[cfg(target_arch = "x86_64")] let buffer = match log_size { Ok(size) if size > 0 && log_buffer.try_reserve_exact(size).is_ok() => { log_buffer.resize(size, 0); @@ -778,12 +877,19 @@ fn deferred_boot_checks( _ => None, }; let kmsg = open_kernel_log(runtime, &mut checks); + #[cfg(target_arch = "x86_64")] let log = kmsg.is_some().then_some(BootLog { // A size that `klogctl` could not report is its failure result, -1. size: log_size.map_or(-1, |size| size as i64), buffer, }); + #[cfg(target_arch = "x86_64")] checks::run_boot_checks(runtime, &mut checks, log); + #[cfg(target_arch = "aarch64")] + { + checks::run_boot_checks(runtime, &mut checks); + check_boot_log(runtime, &mut checks); + } (checks, kmsg) } @@ -797,12 +903,28 @@ pub(crate) fn boot_checker(runtime: &Runtime<'_>, boot: &ColdBoot) -> ! { runtime.set_async_output(true); let detached = { let flags = OpenFlags::O_RDWR | OpenFlags::O_CLOEXEC; - let null = sys::open(kernel::NULL_DEVICE, flags, Mode::empty()).ok(); - sys::detach(null.as_ref()) + sys::open(kernel::NULL_DEVICE, flags, Mode::empty()) + .and_then(|null| sys::detach(Some(&null))) }; - if detached.is_err() { + if let Err(error) = detached { + runtime.conformance_failure( + ViolationCode::Conformance(CheckId::C10), + Phase::Boot, + format!("detach time daemon: {error}").as_bytes(), + ); sys::exit_immediately(1); } + let _exclusive = match sys::try_lock_file(system::DAEMON_LOCK_PATH, system::PRIVATE_FILE_MODE) { + Ok(owner) => owner, + Err(error) => { + runtime.conformance_failure( + ViolationCode::Conformance(CheckId::C10), + Phase::Boot, + format!("time daemon ownership: {error}").as_bytes(), + ); + sys::exit_immediately(1); + } + }; let (mut checks, kmsg) = deferred_boot_checks(runtime, start, boot.failures); let daemon = kmsg.and_then(|kmsg| match become_daemon(kmsg, boot.accepted_ns) { Ok(daemon) => Some(daemon), @@ -857,6 +979,7 @@ pub(crate) fn become_daemon( /// Converts an absolute monotonic nanosecond deadline to a poll timeout, rounded up to whole /// milliseconds, the unit of `poll`, so that the daemon does not wake before it. +#[cfg(target_arch = "x86_64")] fn timeout_until(deadline_ns: i64) -> Duration { /// The longest timeout that `poll` takes, in milliseconds. const POLL_TIMEOUT_MAX_MS: u64 = i32::MAX as u64; @@ -872,8 +995,11 @@ fn timeout_until(deadline_ns: i64) -> Duration { /// Runs the kernel watcher, discipline poller, and restore-worker coordinator. pub(crate) fn daemon_loop(runtime: &Runtime<'_>, mut daemon: Daemon) -> ! { loop { + #[cfg(target_arch = "x86_64")] let worker = daemon.worker; + #[cfg(target_arch = "x86_64")] let due = daemon.restore.filter(|_| worker.is_none()); + #[cfg(target_arch = "x86_64")] let timeout = [ worker.and_then(|worker| worker.promote_ns), worker.and_then(|worker| worker.release_ns), @@ -883,6 +1009,8 @@ pub(crate) fn daemon_loop(runtime: &Runtime<'_>, mut daemon: Daemon) -> ! { .flatten() .min() .map(timeout_until); + #[cfg(target_arch = "aarch64")] + let timeout = None; let events = match sys::poll( [ Pollable::File(&daemon.kmsg), @@ -899,6 +1027,16 @@ pub(crate) fn daemon_loop(runtime: &Runtime<'_>, mut daemon: Daemon) -> ! { } }; let [log, signals, timer] = events; + if signals.error || timer.error { + runtime.fail_fatal( + PowerOffStatus::Violation, + ViolationCode::Conformance(CheckId::C10), + EventSource::Watcher, + Phase::Runtime, + b"time-daemon signal or timer descriptor failed", + ); + sys::exit_immediately(1); + } if log.readable || log.error { watch_kernel_log(runtime, &daemon); } @@ -908,44 +1046,58 @@ pub(crate) fn daemon_loop(runtime: &Runtime<'_>, mut daemon: Daemon) -> ! { if timer.readable { daemon.timer.consume(); discipline_poll(runtime, &mut daemon); - let _ = arm_timer(&daemon); + if let Err(error) = arm_timer(&daemon) { + runtime.fail_fatal( + PowerOffStatus::Violation, + ViolationCode::Conformance(CheckId::C10), + EventSource::Watcher, + Phase::Runtime, + format!("arm discipline timer: {error}").as_bytes(), + ); + sys::exit_immediately(1); + } } - let mut now = clock_ns(sys::Clock::Monotonic); - // Step 7 starts `DEFERRED_START_NS` after the acknowledgement, and continues at normal - // priority `IDLE_BOUND_NS` after it starts. - if daemon.worker.is_none() - && let Some(restore) = daemon.restore.filter(|restore| now >= restore.due_ns) + #[cfg(target_arch = "x86_64")] { - daemon.restore = None; - start_restore(runtime, &mut daemon, restore); - now = clock_ns(sys::Clock::Monotonic); - } - if let Some(worker) = &mut daemon.worker { - if worker - .promote_ns - .is_some_and(|promote_ns| now >= promote_ns) + let mut now = clock_ns(sys::Clock::Monotonic); + // Step 7 starts `DEFERRED_START_NS` after the acknowledgement, and continues at normal + // priority `IDLE_BOUND_NS` after it starts. + if daemon.worker.is_none() + && let Some(restore) = daemon.restore.filter(|restore| now >= restore.due_ns) { - worker.promote_ns = None; - sys::set_scheduling_policy(Some(worker.process), sys::SchedulingPolicy::Normal); + daemon.restore = None; + start_restore(runtime, &mut daemon, restore); + now = clock_ns(sys::Clock::Monotonic); } - if worker - .release_ns - .is_some_and(|release_ns| now >= release_ns) - { - worker.release_ns = None; - release_suppression(runtime); + if let Some(worker) = &mut daemon.worker { + if worker + .promote_ns + .is_some_and(|promote_ns| now >= promote_ns) + { + worker.promote_ns = None; + sys::set_scheduling_policy(Some(worker.process), sys::SchedulingPolicy::Normal); + } + if worker + .release_ns + .is_some_and(|release_ns| now >= release_ns) + { + worker.release_ns = None; + release_suppression(runtime); + } } } } } /// Returns the recorded `daemon` while it runs. +#[cfg(target_arch = "x86_64")] pub(crate) fn running_daemon(daemon: Option) -> Option { daemon.filter(|&process| sys::process_exists(process)) } /// Repair step 6: acknowledges the restore through the snapshot port, when the host gated its /// input, and ends the port transaction. +#[cfg(target_arch = "x86_64")] pub(crate) fn acknowledge_restore(ports: PortAccess<'_>) { ports.write_u8(Port::Snapshot, SNAPSHOT_ACKNOWLEDGE); } @@ -954,6 +1106,7 @@ pub(crate) fn acknowledge_restore(ports: PortAccess<'_>) { /// The `sigqueue` value is the readiness path's wall time since `start_ns`, on the monotonic /// clock, in microseconds clamped to 32 bits, read after the acknowledgement so that the read /// does not delay it. +#[cfg(target_arch = "x86_64")] pub(crate) fn queue_restore(daemon: ProcessId, start_ns: i64) -> Result<(), Errno> { let elapsed = clock_ns(sys::Clock::Monotonic).wrapping_sub(start_ns) / NSEC_PER_USEC; let value = elapsed.clamp(0, i64::from(u32::MAX)) as u32; @@ -969,6 +1122,7 @@ pub(crate) fn queue_restore(daemon: ProcessId, start_ns: i64) -> Result<(), Errn /// takes the permission again, and reports a failure. A handoff that fails after the /// acknowledgement fails check C10, which stops the VM unless report-only, and the restore /// still counts as finished, so that the caller does not acknowledge it again. +#[cfg(target_arch = "x86_64")] pub(crate) fn finish_restore( runtime: &Runtime<'_>, daemon: ProcessId, @@ -989,6 +1143,7 @@ pub(crate) fn finish_restore( } /// Constructs accounting for an inline report-only restore. +#[cfg(target_arch = "x86_64")] pub(crate) fn inline_restore_timing(start_ns: i64) -> RestoreTiming { let now = clock_ns(sys::Clock::Monotonic); RestoreTiming { @@ -1002,6 +1157,7 @@ pub(crate) fn inline_restore_timing(start_ns: i64) -> RestoreTiming { /// Returns the accounting that a test of `fork_deferred_restore` starts from, which the fork /// completes. +#[cfg(target_arch = "x86_64")] pub(crate) fn empty_restore_timing() -> RestoreTiming { RestoreTiming { start_ns: 0, diff --git a/nvx-rs/src/guest/time/daemon/tests.rs b/nvx-rs/src/guest/time/daemon/tests.rs index 60abda1ee..91f3747a6 100644 --- a/nvx-rs/src/guest/time/daemon/tests.rs +++ b/nvx-rs/src/guest/time/daemon/tests.rs @@ -1,282 +1,106 @@ use super::*; -use crate::guest::sys::{ClockAdjustment, FrequencyHold}; -use crate::guest::time::system::tests::FakeOps; + +#[cfg(target_arch = "x86_64")] +mod x86; #[test] -fn daemon_timing_constants_match_the_abi() { - assert_eq!((SAMPLE_BOUND_NS, SAMPLE_ATTEMPTS), (100_000, 3)); - assert_eq!(SYNCHRONIZED_AGE_NS, 128_000_000_000); - assert_eq!( - (FAST_PERIOD.as_secs(), SLOW_PERIOD.as_secs(), FAST_POLLS), - (16, 64, 4) - ); +fn malformed_watcher_records_are_not_silently_discarded() { + let (reader, writer) = sys::create_pipe().unwrap(); + writer + .write_all(b"malformed structured kernel record\n") + .unwrap(); + drop(writer); + let mut consumed = false; + assert_eq!(kmsg_drain(&reader, |_| consumed = true), Err(Errno::EPROTO)); + assert!(!consumed); } -/// A sample of the host clock whose disruption counter is `disruption`, 1.5 µs ahead of the -/// wall clock, and whose host correction asks for 100 ppm. -fn polled(disruption: u32) -> BestSample { +fn measurement() -> BestSample { BestSample { - theta: 1_500, + theta: 1500, epsilon: 900, - host: decoders::HostClock { - disruption, - correction: 6_553_600, - ..decoders::HostClock::default() - }, - frequency: 6_553_600, + host: sample::HostClock::default(), + frequency: 0, limited: false, test_hooks: false, } } #[test] -fn a_poll_records_its_sample_and_reports_a_disruption() { +fn common_state_keeps_initial_synchronization_separate_from_runtime_discontinuities() { + let sample = measurement(); let mut state = TimeState::default(); - let accepted = |disruption, step_realtime_ns| PollOutcome::Accepted { - sample: polled(disruption), - step_realtime_ns, - }; - // The first accepted sample sets the disruption counter, and is no disruption. - assert!(!apply_poll(&mut state, &accepted(5, None), true)); - assert_eq!((state.samples, state.host_disruption), (1, 5)); - assert_eq!((state.offset_ns, state.frequency_ppb), (1_500, 100_000)); - assert!(!apply_poll(&mut state, &accepted(5, None), true)); + state.record_sample(&sample); + state.synchronized = true; assert_eq!(state.discontinuities, 0); - // A changed counter is a disruption, and a step a discontinuity. - assert!(apply_poll(&mut state, &accepted(6, Some(42)), true)); - assert_eq!((state.host_disruption, state.discontinuities), (6, 1)); - assert_eq!(state.last_discontinuity, Discontinuity::Step); - assert_eq!( - (state.last_step_ns, state.last_step_realtime_ns), - (1_500, 42) - ); - // A rejection keeps the counter, and records its reason. - for reason in [ - SampleRejection::Uncertain, - SampleRejection::Tsc, - SampleRejection::Invalid, - ] { - assert!(!apply_poll( - &mut state, - &PollOutcome::Rejected(reason), - false - )); - assert_eq!(state.last_sample_error, reason); - } - assert_eq!( - (state.samples, state.rejected_samples, state.synchronized), - (3, 3, false) - ); - assert!(!apply_poll(&mut state, &accepted(6, None), true)); - assert_eq!(state.last_sample_error, SampleRejection::None); -} - -#[test] -fn a_correction_slews_or_steps_while_holding_the_host_frequency() { - let ops = FakeOps::new([]); - let runtime = Runtime::new(&ops, false); - let PollOutcome::Accepted { - step_realtime_ns: None, - .. - } = correct_clock(&runtime, polled(0), true) - else { - panic!("the slew was not accepted"); - }; - let far = BestSample { - theta: -1_128_000_000, - ..polled(0) - }; - let PollOutcome::Accepted { - step_realtime_ns: Some(_), - .. - } = correct_clock(&runtime, far, false) - else { - panic!("the step was not accepted"); - }; - let hold = FrequencyHold { - scaled_ppm: 6_553_600, - maximum_error_us: 1, - estimated_error_us: 1, - }; - assert_eq!( - ops.adjustments.lock().unwrap().as_slice(), - [ - ClockAdjustment::Slew { - offset_ns: 1_500, - time_constant: 4, - hold: FrequencyHold { - maximum_error_us: 3, - ..hold - }, - }, - ClockAdjustment::Step { - seconds: -2, - nanoseconds: 872_000_000, - }, - ClockAdjustment::Frequency(hold), - ] - ); -} - -#[test] -fn poll_deadlines_round_up_to_milliseconds() { - let now = clock_ns(sys::Clock::Monotonic); - assert_eq!(timeout_until(now - 1), Duration::ZERO); - let soon = timeout_until(now + NSEC_PER_MSEC / 2); - assert!(soon <= Duration::from_millis(1), "{soon:?}"); - assert_eq!( - timeout_until(i64::MAX), - Duration::from_millis(i32::MAX as u64) - ); -} - -#[test] -fn a_dropped_promoter_cancels_its_promotion() { - let far = clock_ns(sys::Clock::Monotonic) + 10 * NSEC_PER_SEC; - let started = std::time::Instant::now(); - drop(Promoter::start(far).unwrap()); - assert!(started.elapsed() < Duration::from_secs(5)); -} - -#[test] -fn a_failed_revocation_stops_the_daemon_before_it_starts_anything() { - let mut ops = FakeOps::new([]); - ops.revoke_error = Some(Errno::ENOMEM); - Runtime::new(&ops, true).repair_failure(ViolationCode::RepairSuppression, b"revoke"); - let refused = Err(SpawnError::Ports(PortError::Revoke(Errno::ENOMEM))); - // Without the check, `fork` itself would refuse with `EBUSY`, in a process with more - // than one thread, such as the test harness. - let mut timing = empty_restore_timing(); - let mut forked = false; - assert_eq!( - fork_deferred_restore(0, &mut timing, || forked = true), - refused - ); - assert!(!forked); - assert!(Promoter::start(clock_ns(sys::Clock::Monotonic)).is_none()); -} - -/// Returns a process ID above Linux's largest, which no process has, so that a signal to it -/// fails. -fn absent_process() -> ProcessId { - ProcessId::from_positive(i64::from(i32::MAX)).unwrap() -} - -#[test] -fn a_restore_whose_acknowledgement_cannot_take_the_ports_stays_unfinished() { - let mut ops = FakeOps::new([]); - ops.grant_error = Some(Errno::ENOMEM); - let runtime = Runtime::new(&ops, false); - assert!(!finish_restore(&runtime, absent_process(), true, 0)); - // Neither an acknowledgement nor the event of a failed handoff. - assert!(ops.output.lock().unwrap().is_empty()); - assert!(ops.console.lock().unwrap().is_empty()); - assert_eq!(ops.power.load(std::sync::atomic::Ordering::Relaxed), 0); - assert_eq!(sys::check_released(), Ok(())); -} - -#[test] -fn a_finished_restore_is_acknowledged_before_its_handoff() { - let ops = FakeOps::new([]); - let runtime = Runtime::new(&ops, true); - // The handoff to a process that does not exist fails check C10 after the - // acknowledgement, and the restore still counts as finished. - assert!(finish_restore(&runtime, absent_process(), true, 0)); - assert_eq!( - ops.output.lock().unwrap().first(), - Some(&(Port::Snapshot, SNAPSHOT_ACKNOWLEDGE)) - ); - let console = String::from_utf8(ops.console.lock().unwrap().clone()).unwrap(); - assert!(console.contains("G_CONFORMANCE_C10"), "{console}"); - assert_eq!(sys::check_released(), Ok(())); - - // Without an acknowledgement, the call takes no port transaction of its own. - let ops = FakeOps::new([]); - assert!(finish_restore( - &Runtime::new(&ops, true), - absent_process(), - false, - 0 + assert_eq!(state.last_discontinuity, Discontinuity::None); + assert!(!apply_poll( + &mut state, + &PollOutcome::Rejected(SampleRejection::Invalid), + true )); - assert!( - !ops.output - .lock() - .unwrap() - .contains(&(Port::Snapshot, SNAPSHOT_ACKNOWLEDGE)) - ); -} - -/// Work for [`run_with_deadline`] that succeeds. -fn succeeding_work() -> bool { - true + assert_eq!(state.rejected_samples, 1); + assert_eq!(state.samples, 1); + assert!(state.synchronized); } -/// Work for [`run_with_deadline`] that fails. -fn failing_work() -> bool { - false +#[cfg(target_arch = "aarch64")] +struct AdjustmentOps { + requests: std::sync::Mutex>, + fail_at: usize, } -/// Work for [`run_with_deadline`] that would fail, after a deadline of one second. -fn stalled_work() -> bool { - thread::sleep(Duration::from_secs(3)); - false -} - -/// Runs [`run_with_deadline`] with work that succeeds, work that fails, and work that -/// outlives its deadline, in a process that has only the calling thread, and returns whether -/// a call went wrong: returned the wrong verdict, waited for the stalled work, left the -/// process a child, or left the stalled work a descriptor of the process's. -fn deadline_calls_fail() -> bool { - // The reader must see its end of file once the process closes the writer, a - // close-on-exec descriptor, while the stalled work still runs. - let Ok((reader, writer)) = sys::create_pipe() else { - return true; - }; - let start = std::time::Instant::now(); - let wrong = !run_with_deadline(1, succeeding_work) - || run_with_deadline(1, failing_work) - // Work that outlives the deadline counts as done. - || !run_with_deadline(1, stalled_work); - drop(writer); - let held = reader.set_nonblocking().is_err() || reader.read(&mut [0]) != Ok(0); - wrong || held || start.elapsed() >= Duration::from_secs(3) || sys::tests::has_child() +#[cfg(target_arch = "aarch64")] +impl system::PrivilegedOps for AdjustmentOps { + fn adjust_clock( + &self, + adjustment: sys::ClockAdjustment, + ) -> Result { + let mut requests = self.requests.lock().unwrap(); + requests.push(adjustment); + if requests.len() == self.fail_at { + Err(Errno::EIO) + } else { + Ok(sys::ClockDiscipline::default()) + } + } + fn power_off(&self, _status: PowerOffStatus) { + panic!("a failed adjustment must reject the sample, not stop the VM"); + } + fn write_kernel_log(&self, _record: &[u8]) {} + fn write_console(&self, _separator: &[u8], _line: &[u8]) {} } #[test] -fn work_with_a_deadline_leaves_no_child_whatever_the_child_signal_disposition() { - if crate::guest::managed_agent::tests::isolate_process_test( - "NVX_TIME_DEADLINE_TEST_CHILD", - "guest::time::daemon::tests::work_with_a_deadline_leaves_no_child_whatever_the_child_signal_disposition", - ) { - return; - } - // `fork_process` refuses the harness's process, which has more than one thread, so a - // child that has only this thread makes the calls. Its standard streams are on the null - // device, which the stalled work, left to init, keeps open after the test. - match sys::tests::fork_isolated_test().expect("fork the caller") { - ForkResult::Child => { - let flags = OpenFlags::O_RDWR | OpenFlags::O_CLOEXEC; - let null = sys::open(kernel::NULL_DEVICE, flags, Mode::empty()).ok(); - let detached = null.is_some() && sys::detach(null.as_ref()).is_ok(); - let failures = [ - !detached, - deadline_calls_fail(), - sys::tests::reap_children_automatically(true).is_err() || deadline_calls_fail(), - sys::tests::reap_children_automatically(false).is_err() || deadline_calls_fail(), - ]; - let status = failures - .iter() - .enumerate() - .fold(0, |status, (bit, &failed)| status | u8::from(failed) << bit); - sys::exit_immediately(status); - } - ForkResult::Parent(caller) => { - assert_eq!( - sys::wait(caller), - Some(ChildStatus::Exited(0)), - "failure bits, from the lowest: detach, then the calls under SIGCHLD's \ - default disposition, SIG_IGN, and SA_NOCLDWAIT" - ); +#[cfg(target_arch = "aarch64")] +fn arm_uses_the_common_slew_step_hold_and_adjustment_failure_policy() { + for (theta, fail_at, expected_requests) in [ + (1500, 0, 1), + (200_000_000, 0, 2), + (1500, 1, 1), + (200_000_000, 2, 2), + ] { + let ops = AdjustmentOps { + requests: std::sync::Mutex::new(Vec::new()), + fail_at, + }; + let runtime = Runtime::new(&ops, false); + let sample = BestSample { + theta, + ..measurement() + }; + let outcome = correct_clock(&runtime, sample, false); + assert_eq!( + matches!(outcome, PollOutcome::Rejected(SampleRejection::Invalid)), + fail_at != 0 + ); + let requests = ops.requests.lock().unwrap(); + assert_eq!(requests.len(), expected_requests); + if theta == 1500 { + assert!(matches!(requests[0], sys::ClockAdjustment::Slew { .. })); + } else { + assert!(matches!(requests[0], sys::ClockAdjustment::Step { .. })); + assert!(matches!(requests[1], sys::ClockAdjustment::Frequency(_))); } } } diff --git a/nvx-rs/src/guest/time/daemon/tests/x86.rs b/nvx-rs/src/guest/time/daemon/tests/x86.rs new file mode 100644 index 000000000..0378ecede --- /dev/null +++ b/nvx-rs/src/guest/time/daemon/tests/x86.rs @@ -0,0 +1,283 @@ +use super::*; +use crate::guest::sys::{ClockAdjustment, FrequencyHold}; +use crate::guest::time::system::tests::FakeOps; +use crate::guest::time::system::{SAMPLE_ATTEMPTS, SAMPLE_BOUND_NS}; + +#[test] +fn daemon_timing_constants_match_the_abi() { + assert_eq!((SAMPLE_BOUND_NS, SAMPLE_ATTEMPTS), (100_000, 3)); + assert_eq!(SYNCHRONIZED_AGE_NS, 128_000_000_000); + assert_eq!( + (FAST_PERIOD.as_secs(), SLOW_PERIOD.as_secs(), FAST_POLLS), + (16, 64, 4) + ); +} + +/// A sample of the host clock whose disruption counter is `disruption`, 1.5 µs ahead of the +/// wall clock, and whose host correction asks for 100 ppm. +fn polled(disruption: u32) -> BestSample { + BestSample { + theta: 1_500, + epsilon: 900, + host: decoders::HostClock { + disruption, + correction: 6_553_600, + ..decoders::HostClock::default() + }, + frequency: 6_553_600, + limited: false, + test_hooks: false, + } +} + +#[test] +fn a_poll_records_its_sample_and_reports_a_disruption() { + let mut state = TimeState::default(); + let accepted = |disruption, step_realtime_ns| PollOutcome::Accepted { + sample: polled(disruption), + step_realtime_ns, + }; + // The first accepted sample sets the disruption counter, and is no disruption. + assert!(!apply_poll(&mut state, &accepted(5, None), true)); + assert_eq!((state.samples, state.host_disruption), (1, 5)); + assert_eq!((state.offset_ns, state.frequency_ppb), (1_500, 100_000)); + assert!(!apply_poll(&mut state, &accepted(5, None), true)); + assert_eq!(state.discontinuities, 0); + // A changed counter is a disruption, and a step a discontinuity. + assert!(apply_poll(&mut state, &accepted(6, Some(42)), true)); + assert_eq!((state.host_disruption, state.discontinuities), (6, 1)); + assert_eq!(state.last_discontinuity, Discontinuity::Step); + assert_eq!( + (state.last_step_ns, state.last_step_realtime_ns), + (1_500, 42) + ); + // A rejection keeps the counter, and records its reason. + for reason in [ + SampleRejection::Uncertain, + SampleRejection::Tsc, + SampleRejection::Invalid, + ] { + assert!(!apply_poll( + &mut state, + &PollOutcome::Rejected(reason), + false + )); + assert_eq!(state.last_sample_error, reason); + } + assert_eq!( + (state.samples, state.rejected_samples, state.synchronized), + (3, 3, false) + ); + assert!(!apply_poll(&mut state, &accepted(6, None), true)); + assert_eq!(state.last_sample_error, SampleRejection::None); +} + +#[test] +fn a_correction_slews_or_steps_while_holding_the_host_frequency() { + let ops = FakeOps::new([]); + let runtime = Runtime::new(&ops, false); + let PollOutcome::Accepted { + step_realtime_ns: None, + .. + } = correct_clock(&runtime, polled(0), true) + else { + panic!("the slew was not accepted"); + }; + let far = BestSample { + theta: -1_128_000_000, + ..polled(0) + }; + let PollOutcome::Accepted { + step_realtime_ns: Some(_), + .. + } = correct_clock(&runtime, far, false) + else { + panic!("the step was not accepted"); + }; + let hold = FrequencyHold { + scaled_ppm: 6_553_600, + maximum_error_us: 1, + estimated_error_us: 1, + }; + assert_eq!( + ops.adjustments.lock().unwrap().as_slice(), + [ + ClockAdjustment::Slew { + offset_ns: 1_500, + time_constant: 4, + hold: FrequencyHold { + maximum_error_us: 3, + ..hold + }, + }, + ClockAdjustment::Step { + seconds: -2, + nanoseconds: 872_000_000, + }, + ClockAdjustment::Frequency(hold), + ] + ); +} + +#[test] +fn poll_deadlines_round_up_to_milliseconds() { + let now = clock_ns(sys::Clock::Monotonic); + assert_eq!(timeout_until(now - 1), Duration::ZERO); + let soon = timeout_until(now + NSEC_PER_MSEC / 2); + assert!(soon <= Duration::from_millis(1), "{soon:?}"); + assert_eq!( + timeout_until(i64::MAX), + Duration::from_millis(i32::MAX as u64) + ); +} + +#[test] +fn a_dropped_promoter_cancels_its_promotion() { + let far = clock_ns(sys::Clock::Monotonic) + 10 * NSEC_PER_SEC; + let started = std::time::Instant::now(); + drop(Promoter::start(far).unwrap()); + assert!(started.elapsed() < Duration::from_secs(5)); +} + +#[test] +fn a_failed_revocation_stops_the_daemon_before_it_starts_anything() { + let mut ops = FakeOps::new([]); + ops.revoke_error = Some(Errno::ENOMEM); + Runtime::new(&ops, true).repair_failure(ViolationCode::RepairSuppression, b"revoke"); + let refused = Err(SpawnError::Ports(PortError::Revoke(Errno::ENOMEM))); + // Without the check, `fork` itself would refuse with `EBUSY`, in a process with more + // than one thread, such as the test harness. + let mut timing = empty_restore_timing(); + let mut forked = false; + assert_eq!( + fork_deferred_restore(0, &mut timing, || forked = true), + refused + ); + assert!(!forked); + assert!(Promoter::start(clock_ns(sys::Clock::Monotonic)).is_none()); +} + +/// Returns a process ID above Linux's largest, which no process has, so that a signal to it +/// fails. +fn absent_process() -> ProcessId { + ProcessId::from_positive(i64::from(i32::MAX)).unwrap() +} + +#[test] +fn a_restore_whose_acknowledgement_cannot_take_the_ports_stays_unfinished() { + let mut ops = FakeOps::new([]); + ops.grant_error = Some(Errno::ENOMEM); + let runtime = Runtime::new(&ops, false); + assert!(!finish_restore(&runtime, absent_process(), true, 0)); + // Neither an acknowledgement nor the event of a failed handoff. + assert!(ops.output.lock().unwrap().is_empty()); + assert!(ops.console.lock().unwrap().is_empty()); + assert_eq!(ops.power.load(std::sync::atomic::Ordering::Relaxed), 0); + assert_eq!(sys::check_released(), Ok(())); +} + +#[test] +fn a_finished_restore_is_acknowledged_before_its_handoff() { + let ops = FakeOps::new([]); + let runtime = Runtime::new(&ops, true); + // The handoff to a process that does not exist fails check C10 after the + // acknowledgement, and the restore still counts as finished. + assert!(finish_restore(&runtime, absent_process(), true, 0)); + assert_eq!( + ops.output.lock().unwrap().first(), + Some(&(Port::Snapshot, SNAPSHOT_ACKNOWLEDGE)) + ); + let console = String::from_utf8(ops.console.lock().unwrap().clone()).unwrap(); + assert!(console.contains("G_CONFORMANCE_C10"), "{console}"); + assert_eq!(sys::check_released(), Ok(())); + + // Without an acknowledgement, the call takes no port transaction of its own. + let ops = FakeOps::new([]); + assert!(finish_restore( + &Runtime::new(&ops, true), + absent_process(), + false, + 0 + )); + assert!( + !ops.output + .lock() + .unwrap() + .contains(&(Port::Snapshot, SNAPSHOT_ACKNOWLEDGE)) + ); +} + +/// Work for [`run_with_deadline`] that succeeds. +fn succeeding_work() -> bool { + true +} + +/// Work for [`run_with_deadline`] that fails. +fn failing_work() -> bool { + false +} + +/// Work for [`run_with_deadline`] that would fail, after a deadline of one second. +fn stalled_work() -> bool { + thread::sleep(Duration::from_secs(3)); + false +} + +/// Runs [`run_with_deadline`] with work that succeeds, work that fails, and work that +/// outlives its deadline, in a process that has only the calling thread, and returns whether +/// a call went wrong: returned the wrong verdict, waited for the stalled work, left the +/// process a child, or left the stalled work a descriptor of the process's. +fn deadline_calls_fail() -> bool { + // The reader must see its end of file once the process closes the writer, a + // close-on-exec descriptor, while the stalled work still runs. + let Ok((reader, writer)) = sys::create_pipe() else { + return true; + }; + let start = std::time::Instant::now(); + let wrong = !run_with_deadline(1, succeeding_work) + || run_with_deadline(1, failing_work) + // Work that outlives the deadline counts as done. + || !run_with_deadline(1, stalled_work); + drop(writer); + let held = reader.set_nonblocking().is_err() || reader.read(&mut [0]) != Ok(0); + wrong || held || start.elapsed() >= Duration::from_secs(3) || sys::tests::has_child() +} + +#[test] +fn work_with_a_deadline_leaves_no_child_whatever_the_child_signal_disposition() { + if crate::guest::managed_agent::tests::isolate_process_test( + "NVX_TIME_DEADLINE_TEST_CHILD", + "guest::time::daemon::tests::x86::work_with_a_deadline_leaves_no_child_whatever_the_child_signal_disposition", + ) { + return; + } + // `fork_process` refuses the harness's process, which has more than one thread, so a + // child that has only this thread makes the calls. Its standard streams are on the null + // device, which the stalled work, left to init, keeps open after the test. + match sys::tests::fork_isolated_test().expect("fork the caller") { + ForkResult::Child => { + let flags = OpenFlags::O_RDWR | OpenFlags::O_CLOEXEC; + let null = sys::open(kernel::NULL_DEVICE, flags, Mode::empty()).ok(); + let detached = null.is_some() && sys::detach(null.as_ref()).is_ok(); + let failures = [ + !detached, + deadline_calls_fail(), + sys::tests::reap_children_automatically(true).is_err() || deadline_calls_fail(), + sys::tests::reap_children_automatically(false).is_err() || deadline_calls_fail(), + ]; + let status = failures + .iter() + .enumerate() + .fold(0, |status, (bit, &failed)| status | u8::from(failed) << bit); + sys::exit_immediately(status); + } + ForkResult::Parent(caller) => { + assert_eq!( + sys::wait(caller), + Some(ChildStatus::Exited(0)), + "failure bits, from the lowest: detach, then the calls under SIGCHLD's \ + default disposition, SIG_IGN, and SA_NOCLDWAIT" + ); + } + } +} diff --git a/nvx-rs/src/guest/time/decoders.rs b/nvx-rs/src/guest/time/decoders.rs index c7cb14eae..29a0e1ba2 100644 --- a/nvx-rs/src/guest/time/decoders.rs +++ b/nvx-rs/src/guest/time/decoders.rs @@ -7,15 +7,16 @@ use std::ops::RangeInclusive; use super::api::RestoreNetwork; use super::cpu_mask::CpuMask; use super::events::ViolationCode; +pub(crate) use super::events::match_watch_rule; +pub(crate) use super::sample::{HostClock, frequency_to_ppb, plan_correction}; use super::state::HostClockStatus; -use super::{MAX_CPUS, NSEC_PER_SEC, NSEC_PER_USEC}; +use super::{MAX_CPUS, NSEC_PER_SEC}; use crate::guest::kernel_text::{ self, ClockEventState, SyslogLine, TickDevice, TickMode, TimerList, }; use crate::guest::parse::parse_decimal; use crate::guest::reseed::Seed; use crate::guest::sys::x86::{CpuidResult, cpuid}; -use crate::guest::sys::{ClockAdjustment, FrequencyHold}; use crate::protocol::cmdline::{self, Parameter}; /// Fixed restore-packet header length. @@ -27,7 +28,7 @@ pub(crate) const PACKET_ENTROPY_SIZE: usize = 64; // The packet's entropy is decoded straight into a seed. const _: () = assert!(PACKET_ENTROPY_SIZE == Seed::LENGTH); /// Fixed host time-sample record length. -pub(crate) const SAMPLE_SIZE: usize = 48; +pub(crate) const SAMPLE_SIZE: usize = super::sample::SIZE; /// VM generation identifier length. pub(crate) const GENERATION_ID_SIZE: usize = 16; @@ -78,31 +79,12 @@ pub(crate) const PACKET_ACTIVATION_LENGTH_SIZE: usize = 4; pub(crate) const PACKET_ACTIVATION_MAX: usize = crate::activation::MAX_RECORD_BYTES; /// The online VP counts that a restore packet may give. const PACKET_ONLINE_VP_COUNTS: [u8; 5] = [0, 1, 2, 4, 8]; -/// The time-sample version. -const SAMPLE_VERSION: u8 = 1; /// Time-sample flag reserved for host test hooks. const SAMPLE_TEST_HOOKS: u8 = 0x08; -/// The encoding of an unknown bound in a time sample. -const SAMPLE_UNKNOWN: u32 = u32::MAX; /// Maximum signed restore rate deviation, equivalent to 250 ppm. const RATE_DEVIATION_LIMIT: i32 = 16_384_000; -/// One part per million in Linux's frequency correction, which `adjtimex` scales by 2^16. -const SCALED_PPM: i64 = 1 << 16; -/// One, in Linux's frequency unit: a million parts per million, scaled by 2^16. -const SCALED_ONE: i128 = SCALED_PPM as i128 * 1_000_000; -/// Parts per billion in a part per million. -const PPB_PER_PPM: i64 = 1000; -/// Linux's limit of the frequency correction, 500 ppm. -const FREQUENCY_LIMIT: i64 = 500 * SCALED_PPM; /// Hz in a kHz, the unit to which Linux reads the TSC rate from the frequency MSR. const HZ_PER_KHZ: u64 = 1000; -/// The time ABI's bound on the skew between any two processors' TSCs, in nanoseconds, by which a -/// sample's guest TSC, of the first processor, may lie outside the reading processor's reads. -const SKEW_BOUND_NS: u64 = 1000; -/// The most, in parts per million, by which the realtime clock's rate may differ from its -/// frequency correction's while the kernel slews an offset below the step threshold at the fast -/// time constant: 128 ms per 2^6 seconds. -const SLEW_RATE_PPM: i128 = 2000; /// Seconds in a day. const SECONDS_PER_DAY: u64 = 24 * 60 * 60; /// Maximum accepted downtime, inclusive: 365 days, the fixed duration of 31,536,000 seconds @@ -110,12 +92,6 @@ const SECONDS_PER_DAY: u64 = 24 * 60 * 60; const DOWNTIME_LIMIT_NS: u64 = 365 * SECONDS_PER_DAY * NSEC_PER_SEC as u64; /// kHz in a MHz, which scale the whole MHz of `/proc/cpuinfo`. const KHZ_PER_MHZ: u64 = 1000; -/// Offset magnitude at or above which correction steps the clock. -const STEP_THRESHOLD_NS: u64 = 128_000_000; -/// Time constant of the phase-locked loop while the discipline converges. -const FAST_TIME_CONSTANT: i64 = 4; -/// Time constant of the phase-locked loop once the discipline has converged. -const SLOW_TIME_CONSTANT: i64 = 6; /// Bytes of a kernel message that a boot-log diagnostic quotes. const QUOTED_MESSAGE_MAX: usize = 300; /// Bytes of a kernel command-line token that a diagnostic quotes. @@ -167,11 +143,6 @@ impl<'a, E: Copy> Fields<'a, E> { fn u64(&mut self) -> Result { self.bytes().map(u64::from_le_bytes) } - - /// Reads the next signed 64-bit field. - fn i64(&mut self) -> Result { - self.bytes().map(i64::from_le_bytes) - } } /// Why a restore packet is invalid. Its `Display` is the diagnostic that repair reports. @@ -482,7 +453,7 @@ fn parse_dns_server( pub(crate) enum TimeSampleError { /// The sample is shorter than its record. Incomplete, - /// The version is not [`SAMPLE_VERSION`]. + /// The version is not one. Version(u8), /// The flags set bits other than the test-hook flag, or the reserved byte is not zero. Flags { @@ -529,37 +500,6 @@ impl Display for TimeSampleError { impl std::error::Error for TimeSampleError {} -/// The discipline of host UTC that a time sample publishes. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub(crate) struct HostClock { - /// The status of host UTC. - pub(crate) status: HostClockStatus, - /// A counter that changes when host UTC steps, the host resumes from a suspension, or the - /// guest TSC's offset from the host's changes. - pub(crate) disruption: u32, - /// How fast host UTC runs against the guest TSC counted at the declared rate, minus one, in - /// parts per million scaled by 2^16. - pub(crate) correction: i64, - /// A bound on the error of `correction`, in parts per million scaled by 2^16, if the host - /// knows one. - pub(crate) correction_bound: Option, - /// The host time service's bound on the error of host UTC, in microseconds, if it reports - /// one. - pub(crate) max_error_us: Option, -} - -impl HostClock { - /// Returns the host's bound on the error of host UTC that the guest adds to its own maximum - /// error, in microseconds: the time service's while it synchronizes host UTC, and otherwise - /// none. - pub(crate) fn error_us(&self) -> i64 { - match (self.status, self.max_error_us) { - (HostClockStatus::Synchronized, Some(error)) => i64::from(error), - _ => 0, - } - } -} - #[derive(Clone, Copy, Debug, PartialEq, Eq)] /// One host time sample: host UTC paired with the guest TSC, and the discipline of host UTC. pub(crate) struct TimeSample { @@ -580,39 +520,27 @@ pub(crate) struct TimeSample { impl TimeSample { /// Decodes and validates the sample record at the start of `bytes`. pub(crate) fn parse(bytes: &[u8]) -> Result { - let mut fields = Fields::new(bytes, TimeSampleError::Incomplete); - let [version, flags, status, reserved] = fields.bytes()?; - let generation = fields.u32()?; - let utc_ns = fields.u64()?; - let tsc = fields.u64()?; - let pairing_ns = fields.u32()?; - let disruption = fields.u32()?; - let correction = fields.i64()?; - let bound = |value: u32| (value != SAMPLE_UNKNOWN).then_some(value); - let correction_bound = bound(fields.u32()?); - let max_error_us = bound(fields.u32()?); - if version != SAMPLE_VERSION { - return Err(TimeSampleError::Version(version)); - } - if flags & !SAMPLE_TEST_HOOKS != 0 || reserved != 0 { - return Err(TimeSampleError::Flags { flags, reserved }); - } - let status = HostClockStatus::from_byte(status).ok_or(TimeSampleError::Status(status))?; - if utc_ns == 0 || i64::try_from(utc_ns).is_err() { - return Err(TimeSampleError::Utc(utc_ns)); - } + use super::sample::{Error, Sample}; + let sample = Sample::parse(bytes).map_err(|error| match error { + Error::Incomplete => TimeSampleError::Incomplete, + Error::Version(version) => TimeSampleError::Version(version), + Error::Flags { flags, reserved } => TimeSampleError::Flags { flags, reserved }, + Error::Status(status) => TimeSampleError::Status(status), + Error::Utc(utc) => TimeSampleError::Utc(utc), + })?; Ok(Self { - flags, - generation, - utc_ns, - tsc, - pairing_ns, + flags: sample.flags, + generation: sample.generation, + utc_ns: sample.utc_ns, + tsc: sample.counter, + pairing_ns: sample.pairing_ns, host: HostClock { - status, - disruption, - correction, - correction_bound, - max_error_us, + status: HostClockStatus::from_byte(sample.host_status) + .expect("validated common wire status"), + disruption: sample.disruption, + correction: sample.correction, + correction_bound: sample.correction_bound, + max_error_us: sample.max_error_us, }, }) } @@ -649,27 +577,6 @@ pub(crate) struct ClockRates { pub(crate) frequency: i64, } -/// Divides `numerator` by the positive `denominator`, rounding to the nearest, half away from -/// zero. -fn divide_rounded(numerator: i128, denominator: i128) -> i128 { - let half = denominator / 2; - if numerator >= 0 { - (numerator + half) / denominator - } else { - (numerator - half) / denominator - } -} - -/// Divides the nonnegative `numerator` by the positive `denominator`, rounding up. -fn divide_up(numerator: i128, denominator: i128) -> i128 { - (numerator + denominator - 1) / denominator -} - -/// Saturates `value` to an `i64`. -fn saturate(value: i128) -> i64 { - value.clamp(i64::MIN.into(), i64::MAX.into()) as i64 -} - /// Returns the rate at which the kernel counts the TSC for the declared rate `tsc_hz`: Linux /// reads the frequency MSR to the kHz. pub(crate) const fn kernel_tsc_hz(tsc_hz: u64) -> u64 { @@ -691,31 +598,28 @@ pub(crate) fn pair_sample( reads: &GuestReads, rates: ClockRates, ) -> Result<(i64, i64), TimeSampleError> { - const NSEC: i128 = NSEC_PER_SEC as i128; - let tsc_hz = i128::from(rates.tsc_hz.max(1)); - let kernel_hz = i128::from(kernel_tsc_hz(rates.tsc_hz).max(HZ_PER_KHZ)); - let pairing = i128::from(sample.pairing_ns); - let tolerance = divide_up((pairing + i128::from(SKEW_BOUND_NS)) * tsc_hz, NSEC); - let tsc = i128::from(sample.tsc); - if tsc < i128::from(reads.before) - tolerance || tsc > i128::from(reads.after) + tolerance { - return Err(TimeSampleError::Tsc { - tsc: sample.tsc, + super::sample::pair( + sample.utc_ns, + sample.tsc, + sample.pairing_ns, + &super::sample::Reads { before: reads.before, after: reads.after, - }); - } - // The kernel's correction never exceeds its limit, which bounds the arithmetic. - let frequency = i128::from(rates.frequency.clamp(-FREQUENCY_LIMIT, FREQUENCY_LIMIT)); - let elapsed = divide_rounded((i128::from(reads.realtime_tsc) - tsc) * NSEC, kernel_hz); - let extrapolated = elapsed + divide_rounded(elapsed * frequency, SCALED_ONE); - let realtime = i128::from(reads.realtime_ns) - extrapolated; - let theta = i128::from(sample.utc_ns) - realtime; - let local = divide_up(i128::from(reads.realtime_half_cycles) * NSEC, kernel_hz); - let slew = divide_up(extrapolated.abs() * SLEW_RATE_PPM, 1_000_000); - Ok(( - saturate(theta), - saturate(pairing + i128::from(SKEW_BOUND_NS) + local + slew), - )) + realtime_ns: reads.realtime_ns, + realtime_counter: reads.realtime_tsc, + realtime_half_cycles: reads.realtime_half_cycles, + }, + super::sample::Rates { + counter_hz: rates.tsc_hz, + kernel_hz: kernel_tsc_hz(rates.tsc_hz).max(HZ_PER_KHZ), + frequency: rates.frequency, + }, + ) + .map_err(|error| TimeSampleError::Tsc { + tsc: error.counter, + before: error.before, + after: error.after, + }) } /// Returns the frequency correction, in parts per million scaled by 2^16, that runs the wall @@ -725,15 +629,7 @@ pub(crate) fn pair_sample( /// The kernel counts the TSC at `F_k`, the declared rate `F` to the kHz, so the correction is /// `(1 + correction) * F_k / F - 1`, rounded to the nearest. pub(crate) fn guest_frequency(correction: i64, tsc_hz: u64) -> (i64, bool) { - let tsc_hz_wide = i128::from(tsc_hz.max(1)); - // A correction beyond 100% is beyond the limit too, which bounds the arithmetic. - let correction = i128::from(correction).clamp(-SCALED_ONE, SCALED_ONE); - let numerator = - (SCALED_ONE + correction) * i128::from(kernel_tsc_hz(tsc_hz)) - SCALED_ONE * tsc_hz_wide; - let frequency = divide_rounded(numerator, tsc_hz_wide); - let limit = i128::from(FREQUENCY_LIMIT); - let held = frequency.clamp(-limit, limit); - (held as i64, held != frequency) + super::sample::guest_frequency(correction, tsc_hz, kernel_tsc_hz(tsc_hz)) } /// Encodes bytes as lowercase hexadecimal without separators. @@ -758,189 +654,6 @@ pub(crate) fn parse_hex(text: &[u8]) -> Option<[u8; N]> { Some(bytes) } -/// Converts Linux scaled-ppm frequency into rounded parts per billion. -pub(crate) fn frequency_to_ppb(frequency: i64) -> i64 { - saturate(divide_rounded( - i128::from(frequency) * i128::from(PPB_PER_PPM), - i128::from(SCALED_PPM), - )) -} - -/// Converts a bound in Linux's scaled-ppm unit into parts per billion, rounded up. -pub(crate) fn bound_to_ppb(bound: u32) -> u64 { - (u64::from(bound) * PPB_PER_PPM as u64).div_ceil(SCALED_PPM as u64) -} - -/// Splits a signed nanosecond offset into whole seconds, rounded toward negative infinity, and -/// the nonnegative nanoseconds that complete it, as a step of the wall clock takes it. -pub(crate) fn split_offset(offset_ns: i64) -> (i64, i64) { - ( - offset_ns.div_euclid(NSEC_PER_SEC), - offset_ns.rem_euclid(NSEC_PER_SEC), - ) -} - -/// Rounds nonnegative nanoseconds up to microseconds, saturating. -fn ceil_us(nanoseconds: i64) -> i64 { - nanoseconds.saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC -} - -/// Returns the frequency hold of a wall clock that is `offset` nanoseconds off host UTC, give or -/// take the uncertainty `epsilon`, with the frequency correction `frequency`, in scaled ppm, and -/// a host whose UTC is within `host_error_us` microseconds of the reference: the maximum error -/// adds the host's to the offset and its uncertainty, and the estimated error is the uncertainty. -pub(crate) fn frequency_hold( - offset: u64, - epsilon: i64, - frequency: i64, - host_error_us: i64, -) -> FrequencyHold { - let offset = i64::try_from(offset).unwrap_or(i64::MAX); - FrequencyHold { - scaled_ppm: frequency, - maximum_error_us: ceil_us(offset.saturating_add(epsilon)).saturating_add(host_error_us), - estimated_error_us: ceil_us(epsilon), - } -} - -/// Selects and parameterizes a step or slew of the wall clock for the offset `theta` and the -/// uncertainty `epsilon`, both in nanoseconds: a step at or above the step threshold, and -/// otherwise a slew with the fast or slow time constant, which also holds the frequency -/// correction `frequency` with error estimates that add `host_error_us`, as [`frequency_hold`] -/// derives them. A step leaves its caller to hold the frequency after it. -pub(crate) fn plan_correction( - theta: i64, - epsilon: i64, - fast: bool, - frequency: i64, - host_error_us: i64, -) -> ClockAdjustment { - let magnitude = theta.unsigned_abs(); - if magnitude >= STEP_THRESHOLD_NS { - let (seconds, nanoseconds) = split_offset(theta); - return ClockAdjustment::Step { - seconds, - nanoseconds, - }; - } - ClockAdjustment::Slew { - offset_ns: theta, - time_constant: if fast { - FAST_TIME_CONSTANT - } else { - SLOW_TIME_CONSTANT - }, - hold: frequency_hold(magnitude, epsilon, frequency, host_error_us), - } -} - -/// Kernel-message signature associated with one violation code. -struct WatchRule { - /// Stable ABI violation code. - code: ViolationCode, - /// Substrings that must all occur. - all: &'static [&'static str], - /// Alternative substrings of which at least one must occur. - any: &'static [&'static str], -} - -/// Ordered kernel-message rules used by the runtime watcher. -const WATCH_RULES: &[WatchRule] = &[ - WatchRule { - code: ViolationCode::TscUnstable, - all: &["Marking TSC unstable due to "], - any: &[], - }, - WatchRule { - code: ViolationCode::ClocksourceUnstable, - all: &[ - "timekeeping watchdog on CPU", - "as unstable because the skew is too large", - ], - any: &[], - }, - WatchRule { - code: ViolationCode::ClocksourceSwitch, - all: &["clocksource: Switched to clocksource "], - any: &[], - }, - WatchRule { - code: ViolationCode::ClocksourceSkew, - all: &["clocksource: "], - any: &[" ahead of CPU ", " behind CPU "], - }, - WatchRule { - code: ViolationCode::TscWarp, - all: &[], - any: &[ - "TSC synchronization [CPU#", - "cycles TSC warp between CPUs", - "TSC warped randomly between CPUs", - ], - }, - WatchRule { - code: ViolationCode::TscAdjust, - all: &["TSC ADJUST"], - any: &[], - }, - WatchRule { - code: ViolationCode::RcuStall, - all: &["rcu: INFO: "], - any: &[ - " detected stalls on CPUs/tasks", - " self-detected stall on CPU", - " detected expedited stalls", - ], - }, - WatchRule { - code: ViolationCode::RcuStarved, - all: &["rcu: "], - any: &[ - " kthread starved for ", - " kthread timer wakeup didn't happen for ", - ], - }, - WatchRule { - code: ViolationCode::SoftLockup, - all: &["watchdog: BUG: soft lockup - CPU#"], - any: &[], - }, - WatchRule { - code: ViolationCode::HardLockup, - all: &["Watchdog detected hard LOCKUP"], - any: &[], - }, - WatchRule { - code: ViolationCode::HungTask, - all: &["INFO: task ", " blocked for more than "], - any: &[], - }, - WatchRule { - code: ViolationCode::UncheckedMsr, - all: &["unchecked MSR access error"], - any: &[], - }, - WatchRule { - code: ViolationCode::ApicIdMismatch, - all: &["APIC ID mismatch"], - any: &[], - }, -]; - -/// Returns the first violation code matched by a kernel message. -pub(crate) fn match_watch_rule(message: &[u8]) -> Option { - if message.starts_with(OWN_RECORD_PREFIX) { - return None; - } - let contains = |part: &&str| kernel_text::contains(message, part.as_bytes()); - WATCH_RULES - .iter() - .find(|rule| { - rule.all.iter().all(contains) && (rule.any.is_empty() || rule.any.iter().any(contains)) - }) - .map(|rule| rule.code) -} - /// Accumulated boot-log evidence for check C4, and the boot log's first W+X record. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct BootLog { diff --git a/nvx-rs/src/guest/time/decoders/tests.rs b/nvx-rs/src/guest/time/decoders/tests.rs index b700ddeba..1d73e1db2 100644 --- a/nvx-rs/src/guest/time/decoders/tests.rs +++ b/nvx-rs/src/guest/time/decoders/tests.rs @@ -1,6 +1,8 @@ use proptest::prelude::*; use super::*; +use crate::guest::sys::{ClockAdjustment, FrequencyHold}; +use crate::guest::time::sample::{bound_to_ppb, frequency_hold, split_offset}; use crate::protocol::cmdline::tests::model; /// A valid packet header without memory ranges, of generation 1. diff --git a/nvx-rs/src/guest/time/deferred.rs b/nvx-rs/src/guest/time/deferred.rs new file mode 100644 index 000000000..035014adf --- /dev/null +++ b/nvx-rs/src/guest/time/deferred.rs @@ -0,0 +1,70 @@ +//! The existing bounded idle-priority policy for deferred time conformance work. + +use std::sync::mpsc::{self, RecvTimeoutError}; +use std::thread; +use std::time::Duration; + +use crate::guest::sys::{self, TimeValLike}; + +/// Existing delay before asynchronous conformance work starts. +pub(crate) const DEFERRED_START_NS: i64 = 150_000_000; +/// Existing maximum idle-priority interval before promotion to normal priority. +pub(crate) const IDLE_BOUND_NS: i64 = 100_000_000; +/// Existing maximum wait for timer-list convergence. +pub(crate) const TIMER_LIST_WAIT: Duration = Duration::from_millis(200); +/// Existing timer-list convergence polling interval. +pub(crate) const TIMER_LIST_POLL: Duration = Duration::from_millis(10); +/// Existing maximum wait for published phase checks. +pub(crate) const STATUS_WAIT: Duration = Duration::from_secs(30); +/// Existing interval while waiting for published phase checks. +pub(crate) const STATUS_POLL: Duration = Duration::from_millis(10); + +/// Join-on-drop owner of the existing idle-to-normal promotion timer. +pub(crate) struct Promoter { + /// Disconnecting this sender cancels a pending promotion. + cancel: Option>, + /// The owned promoter thread. + thread: Option>, +} + +impl Promoter { + /// Starts the existing promotion deadline only after all platform authority is released. + pub(crate) fn start(start_ns: i64) -> Option { + sys::check_released().ok()?; + let (cancel, cancelled) = mpsc::channel(); + let deadline = start_ns.wrapping_add(IDLE_BOUND_NS); + let thread = thread::Builder::new() + .name("nvx-time-promote".to_owned()) + .spawn(move || { + let now = + sys::clock_time(sys::Clock::Monotonic).map_or(0, |time| time.num_nanoseconds()); + let remaining = deadline.wrapping_sub(now); + let timeout = Duration::from_nanos(remaining.max(0) as u64); + if cancelled.recv_timeout(timeout) == Err(RecvTimeoutError::Timeout) { + sys::promote_all_threads(); + } + }) + .ok()?; + Some(Self { + cancel: Some(cancel), + thread: Some(thread), + }) + } + + /// Waits for the existing promotion deadline. + #[cfg(target_arch = "x86_64")] + pub(crate) fn finish(mut self) { + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} + +impl Drop for Promoter { + fn drop(&mut self) { + drop(self.cancel.take()); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} diff --git a/nvx-rs/src/guest/time/events.rs b/nvx-rs/src/guest/time/events.rs index 00d6457c1..c2deaea55 100644 --- a/nvx-rs/src/guest/time/events.rs +++ b/nvx-rs/src/guest/time/events.rs @@ -1,16 +1,22 @@ -//! Time-ABI violation events, their codes, and their byte-exact escaping. +//! Shared time-ABI violation events, their codes, watcher rules and byte-exact escaping. use std::fmt::{self, Display, Write as _}; -use super::Phase; +use super::sample::Phase; +use crate::guest::kernel_text; /// Maximum event record length before its terminating newline. pub(crate) const EVENT_MAX: usize = 512; -/// Maximum unescaped detail bytes considered for one event. +/// Maximum detail buffer for x86 exhaustive-check diagnostics. +#[cfg(target_arch = "x86_64")] pub(crate) const DETAIL_MAX: usize = 384; /// The conformance checks, whose failures the time ABI reports as `G_CONFORMANCE_Cn`. +/// +/// Keep the complete wire-v1 namespace on every ISA, even when its cold-boot checks emit only +/// a subset of the IDs. The shared formatter and its tests retain the other IDs too. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] pub(crate) enum CheckId { /// The Hyper-V identity leaves, on every CPU that the checks run on. C1, @@ -81,14 +87,20 @@ pub(crate) enum ViolationCode { /// A CPU's APIC ID differs from the firmware's. ApicIdMismatch, /// Restore repair could not correct the wall clock. + /// Kept as wire-v1 event vocabulary on cold-boot-only guests, not as restore support. + #[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] RepairClock, /// Restore repair found the wrong VM generation. + #[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] RepairGeneration, /// Restore repair read a malformed restore packet. + #[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] RepairPacket, /// Restore repair could not take a narrow enough time sample. + #[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] RepairSample, /// Restore repair could not restore the stall detectors. + #[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] RepairSuppression, } @@ -130,6 +142,8 @@ pub(crate) enum EventSource { /// The kernel-log and discipline watcher. Watcher, /// Restore repair. + /// Kept as wire-v1 event vocabulary on cold-boot-only guests, not as restore support. + #[cfg_attr(all(not(target_arch = "x86_64"), not(test)), allow(dead_code))] Repair, } @@ -196,5 +210,112 @@ pub(crate) fn format_event( line } +/// Kernel-message signature associated with one existing violation code. +struct WatchRule { + /// Stable ABI violation code. + code: ViolationCode, + /// Substrings that must all occur. + all: &'static [&'static str], + /// Alternatives of which at least one must occur. + any: &'static [&'static str], +} + +/// Existing ordered watcher rules, shared rather than duplicated for the ARM daemon. +const WATCH_RULES: &[WatchRule] = &[ + WatchRule { + code: ViolationCode::TscUnstable, + all: &["Marking TSC unstable due to "], + any: &[], + }, + WatchRule { + code: ViolationCode::ClocksourceUnstable, + all: &[ + "timekeeping watchdog on CPU", + "as unstable because the skew is too large", + ], + any: &[], + }, + WatchRule { + code: ViolationCode::ClocksourceSwitch, + all: &["clocksource: Switched to clocksource "], + any: &[], + }, + WatchRule { + code: ViolationCode::ClocksourceSkew, + all: &["clocksource: "], + any: &[" ahead of CPU ", " behind CPU "], + }, + WatchRule { + code: ViolationCode::TscWarp, + all: &[], + any: &[ + "TSC synchronization [CPU#", + "cycles TSC warp between CPUs", + "TSC warped randomly between CPUs", + ], + }, + WatchRule { + code: ViolationCode::TscAdjust, + all: &["TSC ADJUST"], + any: &[], + }, + WatchRule { + code: ViolationCode::RcuStall, + all: &["rcu: INFO: "], + any: &[ + " detected stalls on CPUs/tasks", + " self-detected stall on CPU", + " detected expedited stalls", + ], + }, + WatchRule { + code: ViolationCode::RcuStarved, + all: &["rcu: "], + any: &[ + " kthread starved for ", + " kthread timer wakeup didn't happen for ", + ], + }, + WatchRule { + code: ViolationCode::SoftLockup, + all: &["watchdog: BUG: soft lockup - CPU#"], + any: &[], + }, + WatchRule { + code: ViolationCode::HardLockup, + all: &["Watchdog detected hard LOCKUP"], + any: &[], + }, + WatchRule { + code: ViolationCode::HungTask, + all: &["INFO: task ", " blocked for more than "], + any: &[], + }, + WatchRule { + code: ViolationCode::UncheckedMsr, + all: &["unchecked MSR access error"], + any: &[], + }, + WatchRule { + code: ViolationCode::ApicIdMismatch, + all: &["APIC ID mismatch"], + any: &[], + }, +]; + +/// Returns the existing first matching violation, excluding the time tools' own records. +pub(crate) fn match_watch_rule(message: &[u8]) -> Option { + if message.starts_with(b"NVX-TIME-") { + return None; + } + let contains = |part: &&str| kernel_text::contains(message, part.as_bytes()); + WATCH_RULES + .iter() + .find(|rule| { + rule.all.iter().all(contains) && (rule.any.is_empty() || rule.any.iter().any(contains)) + }) + .map(|rule| rule.code) +} + #[cfg(test)] mod tests; diff --git a/nvx-rs/src/guest/time/events/tests.rs b/nvx-rs/src/guest/time/events/tests.rs index c2d89800d..bd0b3b00c 100644 --- a/nvx-rs/src/guest/time/events/tests.rs +++ b/nvx-rs/src/guest/time/events/tests.rs @@ -24,10 +24,37 @@ fn event_has_the_abi_limit() { #[test] fn codes_and_sources_have_the_abi_names() { - assert_eq!( - ViolationCode::Conformance(CheckId::C12).to_string(), - "G_CONFORMANCE_C12" - ); + for (index, check) in [ + CheckId::C1, + CheckId::C2, + CheckId::C3, + CheckId::C4, + CheckId::C5, + CheckId::C6, + CheckId::C7, + CheckId::C8, + CheckId::C9, + CheckId::C10, + CheckId::C11, + CheckId::C12, + ] + .into_iter() + .enumerate() + { + assert_eq!( + ViolationCode::Conformance(check).to_string(), + format!("G_CONFORMANCE_C{}", index + 1) + ); + } + for (code, name) in [ + (ViolationCode::RepairClock, "G_REPAIR_CLOCK"), + (ViolationCode::RepairGeneration, "G_REPAIR_GENERATION"), + (ViolationCode::RepairPacket, "G_REPAIR_PACKET"), + (ViolationCode::RepairSample, "G_REPAIR_SAMPLE"), + (ViolationCode::RepairSuppression, "G_REPAIR_SUPPRESSION"), + ] { + assert_eq!(code.to_string(), name); + } assert_eq!(ViolationCode::KmsgOverrun.to_string(), "G_KMSG_OVERRUN"); assert_eq!(EventSource::Repair.to_string(), "repair"); } diff --git a/nvx-rs/src/guest/time/mod.rs b/nvx-rs/src/guest/time/mod.rs index 6929810f6..8a8c13ec4 100644 --- a/nvx-rs/src/guest/time/mod.rs +++ b/nvx-rs/src/guest/time/mod.rs @@ -1,138 +1,109 @@ -//! `nvx-time`, the guest side of NVX time ABI v2, and the typed calls through which any guest -//! init or agent runs the ABI's steps in its own process. +//! The shared `nvx-time` program and typed time-ABI calls for x86-64 and ARM64 Linux guests. //! -//! [`TimeAbi`] runs the cold-boot time step and, separately, starts the time daemon, a process -//! that the caller supervises; it reads the VM generation ID, requests a capture and repairs a -//! restore, and finishes a restore, with typed inputs, results, and errors. Each call holds the -//! permission for OpenVMM's ports only for its port transactions, on the calling thread, and no -//! call waits for a child that it did not start. `nvx-time` runs those steps through the same -//! calls, and the rest of the ABI itself. +//! [`TimeAbi::cold_boot`] synchronizes UTC before guest work. [`TimeAbi::start_daemon`] starts +//! one supervised child for deferred architectural checks, kernel-log watching and continuous +//! clock discipline. The command-line program uses these same calls on either ISA. +//! [`TimeAbi::generation_id`] reads the unchanged a6 process identity. //! -//! A restore hands over its fresh entropy in the [`CaptureRequest`]'s mode: the file mode, which -//! `nvx-time capture` keeps, writes it to a file, and the in-memory mode returns it as the -//! [`Restore`]'s owned [`Seed`](crate::guest::reseed::Seed), and writes no file: +//! Transport and counter conversion remain architectural: x86 uses PIO, the TSC and Linux's +//! kHz conversion; ARM uses the restricted platform device, CNTVCT and the a8 binding's full-Hz +//! frequency, checked against CNTFRQ. CPU-profile admission belongs to the VMM, not this program. +//! ARM currently supports cold boot only; capture, restore and x86 diagnostic test hooks remain +//! x86-only. The sample wire format, event codes and persisted state format are unchanged. //! -//! ```no_run -//! use nvx::guest::reseed; -//! use nvx::guest::time::{CaptureOutcome, CaptureRequest, RestoreFinish, ScratchPolicy, TimeAbi}; -//! -//! # fn main() -> Result<(), Box> { -//! let abi = TimeAbi::new(); -//! // At boot, before the process starts any thread. -//! let daemon = abi.start_daemon(abi.cold_boot()?)?; -//! // The caller supervises the process `daemon`. -//! -//! // A snapshot's capture, after `nvx-time pre-capture`. -//! let request = CaptureRequest::in_memory(ScratchPolicy::Fresh, abi.generation_id()?); -//! if let CaptureOutcome::Restored(mut restore) = abi.capture(request)? { -//! // A clone of the snapshot continues here, with its wall clock repaired. The seed zeroes -//! // its bytes when it is dropped, after the reseed. -//! if let Some(seed) = restore.take_seed() { -//! reseed::reseed(&seed)?; -//! } -//! let mut finish = RestoreFinish::new(); -//! if restore.acknowledgement_required() { -//! finish = finish.acknowledge(); -//! } -//! abi.restore_finish(finish)?; -//! } -//! # let _ = daemon; -//! # Ok(()) -//! # } -//! ``` -//! -//! A violation of the ABI stops the VM in enforcing mode, the mode of the command tools, with a -//! status that `nvx::protocol::status` publishes: 193 for a conformance check, 194 for the time -//! daemon's watcher, and 195 for the repair of a restore. A caller that owns the VM's exit -//! status and its shutdown takes the ABI in strict mode, with [`TimeAbi::strict`], in which a -//! call fails with the violation instead, whose [`status`](Violation::status) the caller ends the -//! VM with. -//! -//! `nvx-time` runs the conformance checks, the violation watcher and the wall-clock discipline -//! in one daemon, a separate process, and the snapshot agent's time steps: the guest preparation -//! steps of the architecture guide's section 5.3.2 and the guest repair steps of its section -//! 5.5.7, which the comments cite by number. Section 3.2.10 is the contract that it enforces. It -//! prints nothing on success: checks record their results in the state file, and only violation -//! events reach the console. It keeps the check IDs, the guest codes, and the formats of the -//! output lines and the state file of the C program that it replaced. -//! -//! `nvx-time test NAME ARGUMENTS...` runs one of the C program's test hooks, which exercise its -//! decoders, formats, and process structure without the hypervisor. +//! Enforcing violations stop the VM with the status in [`crate::protocol::status`]. A caller +//! that owns shutdown uses [`TimeAbi::strict`] and receives a [`Violation`] instead. The child +//! daemon enforces independently. Each hardware transaction relinquishes its authority before +//! another process can be started, and no call waits for a child it did not start. +#![cfg_attr( + target_arch = "x86_64", + doc = r#" +On x86, capture and restore use the same API. An in-memory capture hands fresh entropy over +as an owned `Seed`, rather than writing an entropy file: + +```no_run +use nvx::guest::reseed; +use nvx::guest::time::{CaptureOutcome, CaptureRequest, RestoreFinish, ScratchPolicy, TimeAbi}; + +# fn main() -> Result<(), Box> { +let abi = TimeAbi::new(); +let daemon = abi.start_daemon(abi.cold_boot()?)?; +let request = CaptureRequest::in_memory(ScratchPolicy::Fresh, abi.generation_id()?); +if let CaptureOutcome::Restored(mut restore) = abi.capture(request)? { + if let Some(seed) = restore.take_seed() { + reseed::reseed(&seed)?; + } + let mut finish = RestoreFinish::new(); + if restore.acknowledgement_required() { + finish = finish.acknowledge(); + } + abi.restore_finish(finish)?; +} +# let _ = daemon; +# Ok(()) +# } +``` +"# +)] mod api; +#[cfg(target_arch = "x86_64")] mod checks; +#[cfg(target_arch = "aarch64")] +mod checks_arm64; +#[cfg(target_arch = "aarch64")] +use checks_arm64 as checks; mod commands; +#[cfg(target_arch = "x86_64")] mod cpu_mask; mod daemon; +#[cfg(target_arch = "x86_64")] mod decoders; +mod deferred; mod events; +mod sample; mod state; mod system; +#[cfg(target_arch = "x86_64")] mod test_mode; use std::ffi::CString; use std::io::{self, Write}; use std::process::ExitCode; +#[cfg(target_arch = "x86_64")] pub(crate) use api::PreCaptureError; +#[cfg(target_arch = "x86_64")] pub use api::{ - CaptureOutcome, CaptureRequest, ColdBoot, CpuSet, MemoryRange, Restore, RestoreFinish, - RestoreNetwork, ScratchPolicy, TimeAbi, TimeError, Violation, + CaptureOutcome, CaptureRequest, CpuSet, MemoryRange, Restore, RestoreFinish, RestoreNetwork, + ScratchPolicy, }; +pub use api::{ColdBoot, TimeAbi, TimeError, Violation}; use super::status::{CommandError, run_main}; use super::sys; +pub(crate) use deferred::DEFERRED_START_NS; +#[cfg(target_arch = "x86_64")] +pub(crate) use deferred::IDLE_BOUND_NS; +pub(crate) use sample::Phase; /// Nanoseconds in one second. +#[cfg(target_arch = "x86_64")] pub(crate) const NSEC_PER_SEC: i64 = 1_000_000_000; /// Nanoseconds in one millisecond. +#[cfg(target_arch = "x86_64")] pub(crate) const NSEC_PER_MSEC: i64 = 1_000_000; /// Nanoseconds in one microsecond. pub(crate) const NSEC_PER_USEC: i64 = 1_000; /// Maximum processor count represented by the time ABI's fixed CPU masks. +#[cfg(target_arch = "x86_64")] pub(crate) const MAX_CPUS: usize = 64; -/// Delay between restore readiness and deferred check work. -pub(crate) const DEFERRED_START_NS: i64 = 150 * NSEC_PER_MSEC; -/// Deadline after which idle-priority work is promoted. -pub(crate) const IDLE_BOUND_NS: i64 = 100 * NSEC_PER_MSEC; /// Most arguments that a command line may hold besides `--report-only`, as many as the C /// program's fixed argument array. const MAX_ARGUMENTS: usize = 32; -/// The phase in which a check or event occurs. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum Phase { - /// Initial guest boot. - Boot, - /// Snapshot capture preparation. - Capture, - /// Post-snapshot restore repair. - Restore, - /// Continuous daemon operation. - Runtime, -} - -impl Phase { - /// Persisted check phases in state-file order. - pub(crate) const CHECKS: [Self; 3] = [Self::Boot, Self::Capture, Self::Restore]; - - /// Returns the stable lowercase phase token used in output records. - pub(crate) fn name(self) -> &'static str { - match self { - Self::Boot => "boot", - Self::Capture => "capture", - Self::Restore => "restore", - Self::Runtime => "runtime", - } - } - - /// Returns this phase's position in the three persisted check records. - pub(crate) fn check_index(self) -> Option { - Self::CHECKS.iter().position(|&phase| phase == self) - } -} - /// Byte-exact command help printed for malformed invocations. +#[cfg(target_arch = "x86_64")] const USAGE: &[u8] = br#"usage: nvx-time [--report-only] COMMAND boot check --phase capture|restore [--new-cpus LIST] @@ -146,6 +117,9 @@ const USAGE: &[u8] = br#"usage: nvx-time [--report-only] COMMAND exhaustive test NAME ARGUMENTS... "#; +/// Cold-boot commands supported by the ARM machine contract. +#[cfg(target_arch = "aarch64")] +const USAGE: &[u8] = b"usage: nvx-time [--report-only] boot|status|sample|generation-id\n"; /// Runs `nvx-time` with the process's arguments. It exits with 0 on success, 1 when a command /// or a check fails, or 2 for an invalid command line. @@ -168,13 +142,16 @@ fn run(args: &[CString], out: &mut dyn Write, err: &mut dyn Write) -> Result<(), return Err(CommandError::Usage); } } - let Some((&command, rest)) = arguments.split_first() else { + let Some((command, rest)) = arguments.split_first() else { sys::report(err, &[USAGE]); return Err(CommandError::Usage); }; - if command == b"test" { + #[cfg(target_arch = "x86_64")] + if *command == b"test" { return test_mode::run(rest, out); } + #[cfg(target_arch = "aarch64")] + let _ = (command, rest); commands::run(&arguments, report_only, out, err) } diff --git a/nvx-rs/src/guest/time/sample.rs b/nvx-rs/src/guest/time/sample.rs new file mode 100644 index 000000000..aabe949f5 --- /dev/null +++ b/nvx-rs/src/guest/time/sample.rs @@ -0,0 +1,516 @@ +//! The one version-1 host-UTC wire decoder, independent of the guest counter architecture. + +use std::fmt::Write as _; + +use super::state::HostClockStatus; +use crate::guest::sys::{ClockAdjustment, FrequencyHold}; + +#[cfg(target_arch = "aarch64")] +pub(crate) mod arm64; + +/// Lifecycle phase of the existing published time state and violation records. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Phase { + /// Initial guest boot. + Boot, + /// Snapshot capture preparation, unsupported by Phase 1 ARM. + Capture, + /// Post-snapshot repair, unsupported by Phase 1 ARM. + Restore, + /// Continuous daemon operation. + Runtime, +} + +impl Phase { + /// Existing persisted phase order, unchanged for x86 state-file compatibility. + pub(crate) const CHECKS: [Self; 3] = [Self::Boot, Self::Capture, Self::Restore]; + + /// Returns the existing lowercase token. + pub(crate) const fn name(self) -> &'static str { + match self { + Self::Boot => "boot", + Self::Capture => "capture", + Self::Restore => "restore", + Self::Runtime => "runtime", + } + } + + /// Returns the phase's position in the persisted check records. + pub(crate) fn check_index(self) -> Option { + Self::CHECKS.iter().position(|&phase| phase == self) + } +} + +/// The discipline of host UTC published by the unchanged time sample. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) struct HostClock { + /// Published host-clock status. + pub(crate) status: HostClockStatus, + /// Host UTC or guest-counter disruption generation. + pub(crate) disruption: u32, + /// Host UTC's signed scaled-ppm correction relative to the declared counter rate. + pub(crate) correction: i64, + /// Published correction-error bound, or honestly unknown. + pub(crate) correction_bound: Option, + /// Published host UTC error bound in microseconds, or honestly unknown. + pub(crate) max_error_us: Option, +} + +impl HostClock { + /// Returns the existing adjtimex error-estimate contribution: a synchronized host's known + /// bound, otherwise none. This does not turn an unknown published bound into a known one. + pub(crate) fn error_us(&self) -> i64 { + match (self.status, self.max_error_us) { + (HostClockStatus::Synchronized, Some(error)) => i64::from(error), + _ => 0, + } + } +} + +/// A paired sample in the existing published state's architecture-independent representation. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct BestSample { + /// Guest-to-host UTC offset in nanoseconds. + pub(crate) theta: i64, + /// Pairing uncertainty in nanoseconds. + pub(crate) epsilon: i64, + /// Host clock correction and honestly unknown bounds. + pub(crate) host: HostClock, + /// Linux's resulting frequency correction, in scaled ppm. + pub(crate) frequency: i64, + /// Whether Linux's existing 500-ppm correction limit clamped the result. + pub(crate) limited: bool, + /// Existing host test-hook indicator, never a qualification result. + pub(crate) test_hooks: bool, +} + +/// Whether the selected sample satisfied the requested uncertainty bound. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum SampleResult { + /// The sample was within the bound. + Accepted, + /// A valid sample was selected but remained wider than the bound. + Wide, +} + +/// Whether another attempt can repair a failed measurement. +pub(crate) enum AttemptError { + /// A malformed record or pairing permits another bounded attempt. + Retry(E), + /// An authority, binding or generation failure ends the transaction. + Fatal(E), +} + +/// Selects the first narrow sample, or the least uncertain valid sample after all attempts. +pub(crate) fn select_sample( + attempts: usize, + bound_ns: i64, + mut invalid: E, + mut take: impl FnMut() -> Result>, +) -> Result<(SampleResult, BestSample), E> { + let mut best: Option = None; + for _ in 0..attempts { + let candidate = match take() { + Ok(candidate) => candidate, + Err(AttemptError::Retry(error)) => { + invalid = error; + continue; + } + Err(AttemptError::Fatal(error)) => return Err(error), + }; + if best.is_none_or(|best| candidate.epsilon < best.epsilon) { + best = Some(candidate); + } + if candidate.epsilon <= bound_ns { + return Ok((SampleResult::Accepted, candidate)); + } + } + best.map(|best| (SampleResult::Wide, best)).ok_or(invalid) +} + +/// Exact encoded record length, unchanged across the x86 and ARM time bindings. +pub(crate) const SIZE: usize = 48; +/// Existing maximum attempt count for an initial or periodic sample group. +pub(crate) const SAMPLE_ATTEMPTS: usize = 3; +/// Existing maximum accepted pairing uncertainty, in nanoseconds. +pub(crate) const SAMPLE_BOUND_NS: i64 = 100_000; +/// Existing initial discipline interval. +pub(crate) const FAST_PERIOD: std::time::Duration = std::time::Duration::from_secs(16); +/// Existing steady-state discipline interval. +pub(crate) const SLOW_PERIOD: std::time::Duration = std::time::Duration::from_secs(64); +/// Existing number of initial fast polls. +pub(crate) const FAST_POLLS: i32 = 4; +/// Existing maximum synchronized sample age, in nanoseconds. +pub(crate) const SYNCHRONIZED_AGE_NS: i64 = 128_000_000_000; +/// Nanoseconds in a second. +const NSEC: i128 = 1_000_000_000; +/// Existing cross-processor skew allowance, not a measured qualification result. +const SKEW_NS: i128 = 1000; +/// Existing maximum slew contribution to local extrapolation. +const SLEW_PPM: i128 = 2000; +/// One full rate in Linux's signed scaled-ppm unit. +const SCALED_ONE: i128 = 1_000_000 * (1 << 16); +/// Linux's existing 500-ppm frequency-correction limit. +const FREQUENCY_LIMIT: i128 = 500 * (1 << 16); +/// Offset magnitude at or above which the existing discipline steps the clock. +const STEP_THRESHOLD_NS: u64 = 128_000_000; +/// Linux's scaled-ppm representation of one ppm. +const SCALED_PPM: i64 = 1 << 16; +/// Parts per billion in one ppm. +const PPB_PER_PPM: i64 = 1000; + +/// A malformed version-1 field, before architecture-specific pairing or admission. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Error { + /// The record ends before byte 48. + Incomplete, + /// A version other than one. + Version(u8), + /// Undefined flags or a nonzero reserved byte. + Flags { + /// Observed flags. + flags: u8, + /// Observed reserved byte. + reserved: u8, + }, + /// An undefined host-clock status. + Status(u8), + /// Zero UTC or UTC outside signed nanoseconds. + Utc(u64), +} + +/// Validated common wire fields; structural validity is not CPU/time qualification. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Sample { + /// Existing sample/test flags. + pub(crate) flags: u8, + /// Existing host-clock status byte, zero through three. + pub(crate) host_status: u8, + /// VM process generation counter. + pub(crate) generation: u32, + /// Host UTC nanoseconds. + pub(crate) utc_ns: u64, + /// The guest architecture's counter paired with UTC, without conversion. + pub(crate) counter: u64, + /// Published pairing uncertainty, not a correction-error bound. + pub(crate) pairing_ns: u32, + /// Host-clock disruption generation. + pub(crate) disruption: u32, + /// Host-clock correction in signed scaled ppm. + pub(crate) correction: i64, + /// Correction-error bound, or honestly unknown. + pub(crate) correction_bound: Option, + /// Host UTC maximum error in microseconds, or unknown. + pub(crate) max_error_us: Option, +} + +impl Sample { + /// Decodes the record at the start of the buffer, preserving the existing x86 behavior + /// for trailing bytes and the existing unknown-bound sentinel. + pub(crate) fn parse(bytes: &[u8]) -> Result { + let bytes = bytes.get(..SIZE).ok_or(Error::Incomplete)?; + let [version, flags, status, reserved] = bytes[..4].try_into().expect("checked record"); + let word = |offset| { + u32::from_le_bytes( + bytes[offset..offset + 4] + .try_into() + .expect("checked record"), + ) + }; + let double = |offset| { + u64::from_le_bytes( + bytes[offset..offset + 8] + .try_into() + .expect("checked record"), + ) + }; + if version != 1 { + return Err(Error::Version(version)); + } + if flags & !0x08 != 0 || reserved != 0 { + return Err(Error::Flags { flags, reserved }); + } + if status > 3 { + return Err(Error::Status(status)); + } + let utc_ns = double(8); + if utc_ns == 0 || i64::try_from(utc_ns).is_err() { + return Err(Error::Utc(utc_ns)); + } + let bound = |value| (value != u32::MAX).then_some(value); + Ok(Self { + flags, + host_status: status, + generation: word(4), + utc_ns, + counter: double(16), + pairing_ns: word(24), + disruption: word(28), + correction: i64::from_le_bytes(bytes[32..40].try_into().expect("checked record")), + correction_bound: bound(word(40)), + max_error_us: bound(word(44)), + }) + } +} + +/// Guest clock reads surrounding a synchronous selector write and the following realtime read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Reads { + /// Counter immediately before the latch. + pub(crate) before: u64, + /// Counter immediately after the latch. + pub(crate) after: u64, + /// Linux realtime in nanoseconds. + pub(crate) realtime_ns: i64, + /// Midpoint of the counter reads around realtime. + pub(crate) realtime_counter: u64, + /// Upper bound on half the counter interval around realtime. + pub(crate) realtime_half_cycles: u64, +} + +/// Declared and kernel conversion rates, with the current Linux correction. +#[derive(Clone, Copy)] +pub(crate) struct Rates { + /// The actual declared counter frequency. + pub(crate) counter_hz: u64, + /// The rate Linux uses to convert its clocksource, not necessarily x86's truncated kHz. + pub(crate) kernel_hz: u64, + /// Current signed scaled-ppm frequency correction. + pub(crate) frequency: i64, +} + +/// One diagnostic record before pairing, so a failed pairing does not hide the raw sample. +pub(crate) struct DiagnosticSample { + /// Validated wire fields, with honestly unknown host-error bounds. + pub(crate) sample: Sample, + /// Actual local reads surrounding the latch. + pub(crate) reads: Reads, + /// Actual counter and Linux conversion rates. + pub(crate) rates: Rates, + /// Actual ARM binding identity, not a profile allowlist. + #[cfg(target_arch = "aarch64")] + pub(crate) profile: (String, [u8; 32]), +} + +impl DiagnosticSample { + /// Formats the same raw-and-paired diagnostic on either ISA, retaining counter-specific names. + pub(crate) fn format(&self) -> (String, Result<(), CounterOutsideReads>) { + let sample = &self.sample; + let host = HostClockStatus::from_byte(sample.host_status).expect("validated wire status"); + let bound = + |value: Option| value.map_or_else(|| "unknown".into(), |value| value.to_string()); + #[cfg(target_arch = "x86_64")] + let counter_name = "tsc"; + #[cfg(target_arch = "aarch64")] + let counter_name = "counter"; + let mut line = format!( + "flags=0x{:02x} generation={} status={} utc_ns={} {counter_name}={} pairing_ns={} disruption={} correction={} correction_bound={} max_error_us={} before={} after={} realtime_ns={}", + sample.flags, + sample.generation, + host, + sample.utc_ns, + sample.counter, + sample.pairing_ns, + sample.disruption, + sample.correction, + bound(sample.correction_bound), + bound(sample.max_error_us), + self.reads.before, + self.reads.after, + self.reads.realtime_ns, + ); + let paired = pair( + sample.utc_ns, + sample.counter, + sample.pairing_ns, + &self.reads, + self.rates, + ); + if let Ok((theta, epsilon)) = &paired { + let (frequency, limited) = guest_frequency( + sample.correction, + self.rates.counter_hz, + self.rates.kernel_hz, + ); + let _ = write!( + line, + " theta_ns={theta} epsilon_ns={epsilon} frequency={frequency} frequency_limited={}", + u8::from(limited) + ); + } + #[cfg(target_arch = "aarch64")] + { + let mut digest = String::new(); + for byte in self.profile.1 { + let _ = write!(digest, "{byte:02x}"); + } + let _ = write!( + line, + " counter_hz={} profile_id={} profile_sha256={digest}", + self.rates.counter_hz, self.profile.0 + ); + } + line.push('\n'); + (line, paired.map(drop)) + } +} + +/// An impossible host latch after the published pairing uncertainty and skew allowance. +#[derive(Debug)] +pub(crate) struct CounterOutsideReads { + /// The sample's counter. + pub(crate) counter: u64, + /// Local lower read. + pub(crate) before: u64, + /// Local upper read. + pub(crate) after: u64, +} + +/// Divides by a positive denominator, rounding to nearest and ties away from zero. +pub(crate) fn divide_rounded(numerator: i128, denominator: i128) -> i128 { + let half = denominator / 2; + if numerator >= 0 { + (numerator + half) / denominator + } else { + (numerator - half) / denominator + } +} + +/// Divides a nonnegative numerator by a positive denominator, rounding upward. +fn divide_up(numerator: i128, denominator: i128) -> i128 { + (numerator + denominator - 1) / denominator +} + +/// Saturates a clock result to signed nanoseconds. +pub(crate) fn saturate(value: i128) -> i64 { + value.clamp(i64::MIN.into(), i64::MAX.into()) as i64 +} + +/// The existing offset/uncertainty calculation, parameterized only by how Linux converts the +/// architecture's clocksource. Unknown correction bounds remain unknown and do not replace +/// measured pairing uncertainty. +pub(crate) fn pair( + utc_ns: u64, + counter: u64, + pairing_ns: u32, + reads: &Reads, + rates: Rates, +) -> Result<(i64, i64), CounterOutsideReads> { + let counter_hz = i128::from(rates.counter_hz.max(1)); + let kernel_hz = i128::from(rates.kernel_hz.max(1)); + let pairing = i128::from(pairing_ns); + let tolerance = divide_up((pairing + SKEW_NS) * counter_hz, NSEC); + let paired = i128::from(counter); + if paired < i128::from(reads.before) - tolerance || paired > i128::from(reads.after) + tolerance + { + return Err(CounterOutsideReads { + counter, + before: reads.before, + after: reads.after, + }); + } + let frequency = i128::from(rates.frequency).clamp(-FREQUENCY_LIMIT, FREQUENCY_LIMIT); + let elapsed = divide_rounded( + (i128::from(reads.realtime_counter) - paired) * NSEC, + kernel_hz, + ); + let extrapolated = elapsed + divide_rounded(elapsed * frequency, SCALED_ONE); + let realtime = i128::from(reads.realtime_ns) - extrapolated; + let theta = i128::from(utc_ns) - realtime; + let local = divide_up(i128::from(reads.realtime_half_cycles) * NSEC, kernel_hz); + let slew = divide_up(extrapolated.abs() * SLEW_PPM, 1_000_000); + Ok((saturate(theta), saturate(pairing + SKEW_NS + local + slew))) +} + +/// Derives the correction from the published host rate and the kernel clocksource conversion, +/// preserving Linux's existing clamp and without rounding ARM frequency to x86 kHz. +pub(crate) fn guest_frequency(correction: i64, counter_hz: u64, kernel_hz: u64) -> (i64, bool) { + let rate = i128::from(counter_hz.max(1)); + let correction = i128::from(correction).clamp(-SCALED_ONE, SCALED_ONE); + let numerator = (SCALED_ONE + correction) * i128::from(kernel_hz) - SCALED_ONE * rate; + let frequency = divide_rounded(numerator, rate); + let held = frequency.clamp(-FREQUENCY_LIMIT, FREQUENCY_LIMIT); + (held as i64, held != frequency) +} + +/// Converts Linux scaled ppm to rounded parts per billion. +pub(crate) fn frequency_to_ppb(frequency: i64) -> i64 { + saturate(divide_rounded( + i128::from(frequency) * i128::from(PPB_PER_PPM), + i128::from(SCALED_PPM), + )) +} + +/// Converts a known scaled-ppm error bound to ppb, rounding upward. +pub(crate) fn bound_to_ppb(bound: u32) -> u64 { + (u64::from(bound) * PPB_PER_PPM as u64).div_ceil(SCALED_PPM as u64) +} + +/// Splits a signed step into floor seconds and nonnegative nanoseconds. +pub(crate) fn split_offset(offset_ns: i64) -> (i64, i64) { + ( + offset_ns.div_euclid(NSEC as i64), + offset_ns.rem_euclid(NSEC as i64), + ) +} + +/// Rounds nonnegative nanoseconds to whole microseconds, upward and saturating. +fn ceil_us(nanoseconds: i64) -> i64 { + nanoseconds.saturating_add(999) / 1000 +} + +/// Derives the existing Linux frequency hold and error estimates without replacing unknown +/// host bounds: callers supply their existing interpretation of the host error. +pub(crate) fn frequency_hold( + offset: u64, + epsilon: i64, + frequency: i64, + host_error_us: i64, +) -> FrequencyHold { + let offset = i64::try_from(offset).unwrap_or(i64::MAX); + FrequencyHold { + scaled_ppm: frequency, + maximum_error_us: ceil_us(offset.saturating_add(epsilon)).saturating_add(host_error_us), + estimated_error_us: ceil_us(epsilon), + } +} + +/// Applies the existing step threshold and fast/slow PLL constants, independently of the +/// architecture of the counter used to derive the offset. +pub(crate) fn plan_correction( + theta: i64, + epsilon: i64, + fast: bool, + frequency: i64, + host_error_us: i64, +) -> ClockAdjustment { + let magnitude = theta.unsigned_abs(); + if magnitude >= STEP_THRESHOLD_NS { + let (seconds, nanoseconds) = split_offset(theta); + return ClockAdjustment::Step { + seconds, + nanoseconds, + }; + } + ClockAdjustment::Slew { + offset_ns: theta, + time_constant: if fast { 4 } else { 6 }, + hold: frequency_hold(magnitude, epsilon, frequency, host_error_us), + } +} + +#[cfg(test)] +mod tests; +/// Resolves the shared effective mode for every command, using init's exact token splitting. +pub(crate) fn report_only(command_line: &[u8], explicit: bool) -> bool { + use crate::protocol::cmdline::{self, Token}; + + explicit + || cmdline::init_tokens(command_line).any(|token| { + token + == Token { + name: b"nvx_time_abi", + value: Some(b"report-only"), + } + }) +} diff --git a/nvx-rs/src/guest/time/sample/arm64.rs b/nvx-rs/src/guest/time/sample/arm64.rs new file mode 100644 index 000000000..198644a17 --- /dev/null +++ b/nvx-rs/src/guest/time/sample/arm64.rs @@ -0,0 +1,241 @@ +//! ARM hardware adapter for the shared codec, pairing and Linux clock discipline. + +use std::fmt; + +use super::{Rates, Reads, Sample}; +use crate::guest::sys::platform::binding::{BindingError, TimeBinding}; +use crate::guest::sys::{self, ClockAdjustment, Errno, aarch64, kernel, platform}; + +/// A failed time transaction or an inconsistent effective Linux counter interface. +#[derive(Debug)] +pub(crate) enum Error { + /// An operating-system or restricted-device operation failed. + Operation(&'static str, Errno), + /// The identity record was structurally malformed. + Binding(BindingError), + /// Linux is not using the architected system counter as its clocksource. + Clocksource(Vec), + /// The effective architected frequency differs from the declared one. + Frequency { + /// Frequency declared by the admitted host binding. + declared: u64, + /// Effective CNTFRQ value visible through Linux's normal EL0 interface. + effective: u64, + }, + /// A time sample failed the unchanged wire-v1 decoder. + Wire(super::Error), + /// A different generation must not discipline the current guest's clock. + Generation { + /// Generation carried by the sample. + observed: u32, + /// Generation of the caller's current cold boot or repair. + expected: u32, + }, + /// The paired counter lies outside the synchronous local latch interval. + Counter(super::CounterOutsideReads), +} + +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Operation(operation, error) => write!(formatter, "{operation}: {error}"), + Self::Binding(error) => error.fmt(formatter), + Self::Clocksource(value) => write!( + formatter, + "clocksource {:?}, expected arch_sys_counter", + String::from_utf8_lossy(value) + ), + Self::Frequency { + declared, + effective, + } => write!( + formatter, + "CNTFRQ is {effective} Hz, time binding declares {declared} Hz" + ), + Self::Wire(error) => write!(formatter, "time sample: {error:?}"), + Self::Generation { observed, expected } => { + write!( + formatter, + "time sample generation {observed}, expected {expected}" + ) + } + Self::Counter(reads) => write!( + formatter, + "sample counter {} outside latch reads {}..{}", + reads.counter, reads.before, reads.after + ), + } + } +} + +impl std::error::Error for Error {} + +/// A real paired sample and the existing discipline's resulting correction. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Paired { + /// Original wire fields, retaining honestly unknown host-error bounds. + pub(crate) sample: Sample, + /// Actual local reads surrounding the synchronous host latch. + pub(crate) reads: Reads, + /// Guest-to-host UTC offset in nanoseconds. + pub(crate) theta: i64, + /// Pairing uncertainty in nanoseconds. + pub(crate) epsilon: i64, + /// Linux frequency correction in scaled ppm. + pub(crate) frequency: i64, + /// Whether Linux's existing 500-ppm correction limit clamped the result. + pub(crate) limited: bool, +} + +impl Paired { + /// Converts the measurement into the existing published-state representation. + pub(crate) fn measurement(self) -> super::BestSample { + super::BestSample { + theta: self.theta, + epsilon: self.epsilon, + host: super::HostClock { + status: super::HostClockStatus::from_byte(self.sample.host_status) + .expect("wire decoder validated host status"), + disruption: self.sample.disruption, + correction: self.sample.correction, + correction_bound: self.sample.correction_bound, + max_error_us: self.sample.max_error_us, + }, + frequency: self.frequency, + limited: self.limited, + test_hooks: self.sample.flags & 0x08 != 0, + } + } +} + +/// Clock metadata only: no descriptor, mapping, permission or record lock is retained. +pub(crate) struct Clock { + /// Structural binding metadata, not independent proof of CPU-profile admission. + pub(crate) binding: TimeBinding, +} + +impl Clock { + /// Opens the existing identity window, verifies Linux's effective counter conversion, and + /// relinquishes all device authority before returning. + pub(crate) fn open() -> Result { + let record = platform::time_binding_record() + .map_err(|error| Error::Operation("read time binding", error))?; + let binding = TimeBinding::parse(&record).map_err(Error::Binding)?; + let clocksource = sys::read_file(kernel::CURRENT_CLOCKSOURCE) + .map_err(|error| Error::Operation("read current clocksource", error))?; + let frequency = aarch64::counter_frequency_hz() + .map_err(|error| Error::Operation("read CNTFRQ", error))?; + validate_conversion(&binding, &clocksource, frequency)?; + Ok(Self { binding }) + } + + /// Takes one synchronous sample through the existing a7 window and shared v1 math. + pub(crate) fn sample(&self, generation: u32) -> Result { + let (latched, correction) = self.read()?; + pair_latched(&latched, self.binding.counter_hz, correction, generation) + } + + /// Retains raw diagnostic fields even when their pairing will fail. + pub(crate) fn diagnostic(&self) -> Result { + let (latched, correction) = self.read()?; + Ok(super::DiagnosticSample { + sample: Sample::parse(&latched.bytes).map_err(Error::Wire)?, + reads: reads(&latched), + rates: Rates { + counter_hz: self.binding.counter_hz, + kernel_hz: self.binding.counter_hz, + frequency: correction, + }, + profile: (self.binding.profile_id.clone(), self.binding.profile_sha256), + }) + } + + /// Reads the real effective conversion and latch without retaining device authority. + fn read(&self) -> Result<(platform::LatchedTime, i64), Error> { + let frequency = aarch64::counter_frequency_hz() + .map_err(|error| Error::Operation("read CNTFRQ", error))?; + if frequency != self.binding.counter_hz { + return Err(Error::Frequency { + declared: self.binding.counter_hz, + effective: frequency, + }); + } + let discipline = sys::adjust_clock(ClockAdjustment::Query) + .map_err(|error| Error::Operation("query adjtimex", error))?; + let latched = + platform::latch_time().map_err(|error| Error::Operation("latch time sample", error))?; + Ok((latched, discipline.scaled_ppm)) + } +} + +/// Verifies the standard kernel conversion interface; this is not raw CPU-profile readback. +fn validate_conversion( + binding: &TimeBinding, + clocksource: &[u8], + effective_hz: u64, +) -> Result<(), Error> { + if clocksource != b"arch_sys_counter\n" && clocksource != b"arch_sys_counter" { + return Err(Error::Clocksource(clocksource.to_vec())); + } + if effective_hz != binding.counter_hz { + return Err(Error::Frequency { + declared: binding.counter_hz, + effective: effective_hz, + }); + } + Ok(()) +} + +/// Adapts the real latch to the same codec and math as x86, preserving full ARM Hz rather than +/// applying the x86 kernel's kHz truncation. +fn pair_latched( + latched: &platform::LatchedTime, + frequency_hz: u64, + current_correction: i64, + generation: u32, +) -> Result { + let sample = Sample::parse(&latched.bytes).map_err(Error::Wire)?; + if sample.generation != generation { + return Err(Error::Generation { + observed: sample.generation, + expected: generation, + }); + } + let reads = reads(latched); + let (theta, epsilon) = super::pair( + sample.utc_ns, + sample.counter, + sample.pairing_ns, + &reads, + Rates { + counter_hz: frequency_hz, + kernel_hz: frequency_hz, + frequency: current_correction, + }, + ) + .map_err(Error::Counter)?; + let (frequency, limited) = + super::guest_frequency(sample.correction, frequency_hz, frequency_hz); + Ok(Paired { + sample, + reads, + theta, + epsilon, + frequency, + limited, + }) +} + +/// Adapts the hardware latch's reads without changing counter units. +fn reads(latched: &platform::LatchedTime) -> Reads { + Reads { + before: latched.before, + after: latched.after, + realtime_ns: latched.realtime_ns, + realtime_counter: latched.realtime_counter, + realtime_half_cycles: latched.realtime_half_cycles, + } +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/guest/time/sample/arm64/tests.rs b/nvx-rs/src/guest/time/sample/arm64/tests.rs new file mode 100644 index 000000000..7cfa2c1ea --- /dev/null +++ b/nvx-rs/src/guest/time/sample/arm64/tests.rs @@ -0,0 +1,61 @@ +use super::*; + +/// Synthetic binding for conversion arithmetic, never an approved product profile. +fn binding() -> TimeBinding { + TimeBinding { + capacity: 1, + counter_hz: 24_000_001, + profile_id: "fixture".into(), + profile_sha256: [0; 32], + } +} + +#[test] +fn effective_conversion_rejects_wrong_clocksource_or_frequency() { + let binding = binding(); + assert!(validate_conversion(&binding, b"arch_sys_counter\n", binding.counter_hz).is_ok()); + assert!(matches!( + validate_conversion(&binding, b"jiffies\n", binding.counter_hz), + Err(Error::Clocksource(_)) + )); + assert!(matches!( + validate_conversion(&binding, b"arch_sys_counter\n", 24_000_000), + Err(Error::Frequency { + declared: 24_000_001, + effective: 24_000_000, + }) + )); +} + +#[test] +fn arm_latch_uses_shared_v1_and_rejects_other_generations() { + let mut bytes = super::super::tests::sample(); + bytes[16..24].copy_from_slice(&1000_u64.to_le_bytes()); + let mut latched = platform::LatchedTime { + bytes, + before: 999, + after: 1001, + realtime_ns: 1_000_020_000, + realtime_counter: 1480, + realtime_half_cycles: 12, + }; + let paired = pair_latched(&latched, 24_000_000, 0, 0).unwrap(); + assert_eq!(paired.theta, 0); + assert_eq!(paired.epsilon, 10540); + assert_eq!(paired.frequency, -42); + assert_eq!(paired.sample.correction_bound, None); + assert_eq!(paired.sample.max_error_us, None); + latched.bytes[4..8].copy_from_slice(&1_u32.to_le_bytes()); + assert!(matches!( + pair_latched(&latched, 24_000_000, 0, 0), + Err(Error::Generation { + observed: 1, + expected: 0, + }) + )); + latched.bytes[0] = 2; + assert!(matches!( + pair_latched(&latched, 24_000_000, 0, 0), + Err(Error::Wire(super::super::Error::Version(2))) + )); +} diff --git a/nvx-rs/src/guest/time/sample/tests.rs b/nvx-rs/src/guest/time/sample/tests.rs new file mode 100644 index 000000000..ac7b48928 --- /dev/null +++ b/nvx-rs/src/guest/time/sample/tests.rs @@ -0,0 +1,215 @@ +use super::*; + +fn measurement(epsilon: i64) -> BestSample { + BestSample { + theta: 1500, + epsilon, + host: HostClock::default(), + frequency: 0, + limited: false, + test_hooks: false, + } +} + +#[test] +fn shared_selection_retries_invalid_records_and_retains_the_best_wide_sample() { + let mut replies = [ + Err(AttemptError::Retry("malformed")), + Ok(measurement(SAMPLE_BOUND_NS + 20)), + Ok(measurement(SAMPLE_BOUND_NS + 10)), + ] + .into_iter(); + let (result, sample) = + select_sample(3, SAMPLE_BOUND_NS, "empty", || replies.next().unwrap()).unwrap(); + assert_eq!(result, SampleResult::Wide); + assert_eq!(sample.epsilon, SAMPLE_BOUND_NS + 10); + assert!(replies.next().is_none()); +} + +#[test] +fn shared_selection_accepts_the_bound_and_never_hides_a_fatal_contract_error() { + let mut attempts = 0; + let selected = select_sample(3, SAMPLE_BOUND_NS, "empty", || { + attempts += 1; + Ok(measurement(SAMPLE_BOUND_NS)) + }) + .unwrap(); + assert_eq!(selected.0, SampleResult::Accepted); + assert_eq!(attempts, 1); + let mut attempts = 0; + let failed = select_sample(3, SAMPLE_BOUND_NS, "empty", || { + attempts += 1; + if attempts == 1 { + Ok(measurement(SAMPLE_BOUND_NS + 1)) + } else { + Err(AttemptError::Fatal("generation or binding changed")) + } + }); + assert_eq!(failed.unwrap_err(), "generation or binding changed"); + assert_eq!(attempts, 2); +} + +#[test] +fn shared_diagnostic_keeps_raw_fields_when_pairing_is_invalid() { + let mut raw = Sample::parse(&sample()).unwrap(); + raw.counter = 1_000_000; + let diagnostic = DiagnosticSample { + sample: raw, + reads: Reads { + before: 0, + after: 1, + realtime_ns: 1_000_000_000, + realtime_counter: 1, + realtime_half_cycles: 1, + }, + rates: Rates { + counter_hz: 24_000_001, + kernel_hz: 24_000_001, + frequency: 0, + }, + #[cfg(target_arch = "aarch64")] + profile: ("fixture.cpu.v1".into(), [0x12; 32]), + }; + let (line, result) = diagnostic.format(); + assert!(result.is_err()); + assert!(line.starts_with("flags=0x00 generation=0 status=synchronized utc_ns=1000000000 ")); + assert!(line.contains("correction_bound=unknown max_error_us=unknown")); + assert!(!line.contains("theta_ns=")); + assert!(line.ends_with('\n')); +} + +#[test] +fn kernel_report_only_survives_boot_then_plain_status_even_when_checks_pass() { + let command_line = b"console=hvc1 nvx_time_abi=report-only"; + let mut state = crate::guest::time::state::TimeState::default(); + state.check_mut(Phase::Boot).unwrap().status = Some(crate::guest::time::state::CheckStatus::Ok); + for command in ["boot", "status"] { + let mode = report_only(command_line, false); + assert!(mode, "{command}"); + let markers = state.status_markers(mode); + assert!(markers.starts_with("NVX-TIME-REPORT: v=1 phase=boot status=ok")); + assert!(!markers.contains("NVX-TIME-ABI:")); + } + assert!(!report_only(b"other=nvx_time_abi=report-only", false)); + assert!(!report_only(b"nvx_time_abi=enforce", false)); + assert!(report_only(b"nvx_time_abi=enforce", true)); +} + +/// A wire fixture, not a host clock, counter rate or CPU-profile identity. +pub(super) fn sample() -> [u8; SIZE] { + let mut bytes = [0; SIZE]; + bytes[0] = 1; + bytes[2] = 1; + bytes[8..16].copy_from_slice(&1_000_000_000_u64.to_le_bytes()); + bytes[16..24].copy_from_slice(&1234_u64.to_le_bytes()); + bytes[24..28].copy_from_slice(&9000_u32.to_le_bytes()); + bytes[32..40].copy_from_slice(&(-42_i64).to_le_bytes()); + bytes[40..44].copy_from_slice(&u32::MAX.to_le_bytes()); + bytes[44..48].copy_from_slice(&u32::MAX.to_le_bytes()); + bytes +} + +#[test] +fn common_codec_keeps_real_counter_units_and_unknown_bounds() { + let decoded = Sample::parse(&sample()).unwrap(); + assert_eq!(decoded.counter, 1234); + assert_eq!(decoded.pairing_ns, 9000); + assert_eq!(decoded.correction, -42); + assert_eq!(decoded.correction_bound, None); + assert_eq!(decoded.max_error_us, None); + let mut bytes = sample(); + bytes[40..44].copy_from_slice(&17_u32.to_le_bytes()); + assert_eq!(Sample::parse(&bytes).unwrap().correction_bound, Some(17)); +} + +#[test] +fn common_codec_preserves_v1_validation_and_trailing_byte_behavior() { + let bytes = sample(); + assert_eq!(Sample::parse(&bytes[..47]), Err(Error::Incomplete)); + let mut extended = bytes.to_vec(); + extended.push(0xff); + assert_eq!(Sample::parse(&extended), Sample::parse(&bytes)); + for (offset, value, expected) in [ + (0, 2, Error::Version(2)), + ( + 1, + 1, + Error::Flags { + flags: 1, + reserved: 0, + }, + ), + (2, 4, Error::Status(4)), + ( + 3, + 1, + Error::Flags { + flags: 0, + reserved: 1, + }, + ), + ] { + let mut invalid = bytes; + invalid[offset] = value; + assert_eq!(Sample::parse(&invalid), Err(expected)); + } + + for utc in [0, u64::MAX] { + let mut invalid = bytes; + invalid[8..16].copy_from_slice(&utc.to_le_bytes()); + assert_eq!(Sample::parse(&invalid), Err(Error::Utc(utc))); + } +} + +#[test] +fn arm_pairing_uses_full_frequency_and_keeps_the_existing_uncertainty_terms() { + let reads = Reads { + before: 999, + after: 1001, + realtime_ns: 1_000_020_000, + realtime_counter: 1480, + realtime_half_cycles: 12, + }; + let rates = Rates { + counter_hz: 24_000_000, + kernel_hz: 24_000_000, + frequency: 0, + }; + assert_eq!( + pair(1_000_000_000, 1000, 9000, &reads, rates).ok(), + Some((0, 10540)) + ); + assert_eq!(guest_frequency(1234, 24_000_001, 24_000_001), (1234, false)); + assert_eq!( + guest_frequency(500 * 65536 + 1, 24_000_001, 24_000_001), + (500 * 65536, true) + ); +} + +#[test] +fn shared_discipline_preserves_negative_steps_threshold_and_pll_constants() { + assert_eq!(split_offset(-1), (-1, 999_999_999)); + assert_eq!( + plan_correction(-128_000_000, 9000, true, 42, 17), + ClockAdjustment::Step { + seconds: -1, + nanoseconds: 872_000_000, + } + ); + for (fast, time_constant) in [(true, 4), (false, 6)] { + assert_eq!( + plan_correction(127_999_999, 9001, fast, 42, 17), + ClockAdjustment::Slew { + offset_ns: 127_999_999, + time_constant, + hold: FrequencyHold { + scaled_ppm: 42, + maximum_error_us: 128_026, + estimated_error_us: 10, + }, + } + ); + } + assert_eq!(frequency_to_ppb(-65536), -1000); + assert_eq!(bound_to_ppb(1), 1); +} diff --git a/nvx-rs/src/guest/time/state.rs b/nvx-rs/src/guest/time/state.rs index ed6582216..bc02a52fa 100644 --- a/nvx-rs/src/guest/time/state.rs +++ b/nvx-rs/src/guest/time/state.rs @@ -1,11 +1,15 @@ -//! The published `/run/nvx/time/state` representation. +//! The shared, unchanged `/run/nvx/time/state` representation and atomic publication. use std::fmt::{self, Write as _}; use std::str::FromStr; -use super::Phase; -use super::decoders; -use super::system::BestSample; +use super::sample::{self as decoders, BestSample, Phase}; +use crate::guest::sys::{self, CreatedFile, Errno, Mode}; + +/// Existing published time-state path, shared by both counter architectures. +pub(crate) const STATE_PATH: &std::ffi::CStr = c"/run/nvx/time/state"; +/// Existing temporary file for an atomic state replacement. +pub(crate) const STATE_TEMP_PATH: &std::ffi::CStr = c"/run/nvx/time/.state"; /// Defines an enumeration of the tokens of one state-file field, with each token's spelling. macro_rules! tokens { @@ -158,6 +162,45 @@ pub(crate) struct CheckState { pub(crate) failures: Option, } +impl CheckState { + /// Records a completed phase, including actual report-only failures on either ISA. + pub(crate) fn completed( + cpus: u64, + elapsed_us: i64, + cpu_us: i64, + failures: Option, + ) -> Self { + Self { + status: Some(if failures.is_some_and(|failures| failures != 0) { + CheckStatus::Fail + } else { + CheckStatus::Ok + }), + cpus, + elapsed_us, + cpu_us, + failures, + ..Self::default() + } + } +} + +/// Waits for boot-only or all persisted checks using the existing status deadline. +pub(crate) fn wait_for_checks(boot_only: bool) -> Result<(TimeState, bool), Errno> { + use super::deferred::{STATUS_POLL, STATUS_WAIT}; + let deadline = std::time::Instant::now() + STATUS_WAIT; + loop { + let state = TimeState::load()?; + if !state.pending(boot_only) { + return Ok((state, true)); + } + if std::time::Instant::now() >= deadline { + return Ok((state, false)); + } + std::thread::sleep(STATUS_POLL); + } +} + /// Complete state published under `/run/nvx/time/state`, of version one of its format. #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct TimeState { @@ -165,9 +208,9 @@ pub(crate) struct TimeState { pub(crate) generation: u64, /// Boot, capture, and restore records, in the order of `Phase::CHECKS`. checks: [CheckState; 3], - /// Validated TSC frequency in hertz. + /// Validated counter Hz: TSC on x86, CNTVCT on ARM, in the compatible `tsc_hz` slot. pub(crate) tsc_hz: u64, - /// Validated local-APIC timer frequency in hertz. + /// Validated local-APIC timer Hz on x86; zero on ARM, which has no local APIC. pub(crate) lapic_hz: u64, /// Number of observed clock discontinuities. pub(crate) discontinuities: u64, @@ -195,7 +238,7 @@ pub(crate) struct TimeState { /// The status of host UTC that the latest accepted sample reported. pub(crate) host_status: HostClockStatus, /// The host's clock correction that the latest accepted sample published: how fast host UTC - /// runs against the TSC counted at the declared rate, minus one, in parts per billion. + /// runs against the counter counted at the declared rate, minus one, in parts per billion. pub(crate) host_correction_ppb: i64, /// The host's bound on the error of its correction, in parts per billion, if it knows one. pub(crate) host_correction_bound_ppb: Option, @@ -203,7 +246,7 @@ pub(crate) struct TimeState { /// one. pub(crate) host_max_error_us: Option, /// The disruption counter of the latest accepted sample, which changes when host UTC steps, - /// the host resumes from a suspension, or the guest TSC's offset from the host's changes. + /// the host resumes from a suspension, or the guest counter's offset from the host's changes. pub(crate) host_disruption: u64, /// Whether the latest accepted sample carried the test-hook flag: host overrides, which only /// tests make, of the time that it published were active. @@ -256,6 +299,21 @@ impl Default for TimeState { } impl TimeState { + /// Loads the existing state format without replacing a malformed record with a default. + pub(crate) fn load() -> Result { + let text = sys::read_file(STATE_PATH)?; + Self::parse(&text).ok_or(Errno::EINVAL) + } + + /// Durably replaces the published record using the existing ownership and mode. + pub(crate) fn store(&self) -> Result<(), Errno> { + let mode = Mode::S_IRUSR | Mode::S_IWUSR | Mode::S_IRGRP | Mode::S_IROTH; + let file = CreatedFile::create(STATE_TEMP_PATH, false, mode)?; + file.write_all(self.to_string().as_bytes())?; + file.sync()?; + file.rename(STATE_PATH) + } + /// Records the accepted time sample `sample`: the wall clock's offset from host UTC and its /// uncertainty, the frequency correction that the discipline holds, whether Linux's limit /// clamped it, the discipline of host UTC that the sample published, and whether host test @@ -364,10 +422,10 @@ impl TimeState { /// Returns the phases whose checks have run, with their records and outcomes, in state-file /// order. fn recorded(&self) -> impl Iterator { - Phase::CHECKS - .into_iter() - .zip(&self.checks) - .filter_map(|(phase, check)| Some((phase, check, check.status?))) + Phase::CHECKS.into_iter().filter_map(|phase| { + let check = self.check(phase)?; + Some((phase, check, check.status?)) + }) } /// Returns whether the boot check, or unless `boot_only` any recorded check, is pending. diff --git a/nvx-rs/src/guest/time/state/tests.rs b/nvx-rs/src/guest/time/state/tests.rs index c096d3385..3753ad796 100644 --- a/nvx-rs/src/guest/time/state/tests.rs +++ b/nvx-rs/src/guest/time/state/tests.rs @@ -1,5 +1,18 @@ use super::*; +#[test] +fn report_only_phase_publication_keeps_the_actual_failure_count() { + let mut state = TimeState::default(); + *state.check_mut(Phase::Boot).unwrap() = CheckState::completed(4, 17, 9, Some(2)); + let output = state.status_markers(true); + assert!(output.starts_with("NVX-TIME-REPORT: v=1 phase=boot status=fail cpus=4 ")); + assert!(output.contains("elapsed_us=17 cpu_us=9 failures=2\n")); + assert!(!state.checks_ok()); + assert!(!state.pending(false)); + *state.check_mut(Phase::Boot).unwrap() = CheckState::completed(4, 17, 9, Some(0)); + assert!(state.checks_ok()); +} + const ROUND_TRIP_STATE: &str = concat!( "version=1\n", "generation=2\n", diff --git a/nvx-rs/src/guest/time/system.rs b/nvx-rs/src/guest/time/system.rs index b2e109462..982d88390 100644 --- a/nvx-rs/src/guest/time/system.rs +++ b/nvx-rs/src/guest/time/system.rs @@ -1,24 +1,31 @@ //! The time tools' runtime: persistent state, violation events, port-B transactions, and the //! wall clock's discipline, over the system layer's owners. -use std::ffi::{CStr, CString}; +use std::ffi::CStr; +#[cfg(target_arch = "x86_64")] +use std::ffi::CString; use std::fmt::{self, Display}; -use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; +#[cfg(target_arch = "x86_64")] +use std::sync::atomic::AtomicU64; +use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::{Mutex, PoisonError}; use super::Phase; use super::api::Violation; -use super::decoders::{ - self, ClockRates, GuestReads, HostClock, SAMPLE_SIZE, TimeSample, TimeSampleError, -}; +#[cfg(target_arch = "x86_64")] +use super::decoders::{self, ClockRates, GuestReads, SAMPLE_SIZE, TimeSample, TimeSampleError}; use super::events::{EventSource, ViolationCode, format_event}; -use super::state::{SampleRejection, TimeState}; +use super::sample; +pub(crate) use super::sample::{BestSample, SAMPLE_ATTEMPTS, SAMPLE_BOUND_NS, SampleResult}; +use super::state::{CheckState, SampleRejection, TimeState}; +#[cfg(target_arch = "x86_64")] use crate::guest::sys::x86::{self, msr}; use crate::guest::sys::{ - self, ClockAdjustment, ClockDiscipline, CreatedFile, Errno, FileDescriptor, FileLock, - ForkResult, FrequencyHold, Mode, MsrAccess, MsrFile, OpenFlags, Port, PortAccess, PortError, - TimeValLike, kernel, + self, ClockAdjustment, ClockDiscipline, CreatedFile, Errno, FileLock, ForkResult, + FrequencyHold, Mode, OpenFlags, TimeValLike, kernel, }; +#[cfg(target_arch = "x86_64")] +use crate::guest::sys::{FileDescriptor, MsrAccess, MsrFile, Port, PortAccess, PortError}; use crate::protocol::status; /// Statuses with which a fatal time-ABI failure stops the VM through the control port, which @@ -31,6 +38,7 @@ pub(crate) enum PowerOffStatus { /// The runtime watcher observed a time-ABI violation. Violation = status::TIME_VIOLATION, /// Restore-time repair could not complete safely. + #[cfg(target_arch = "x86_64")] Repair = status::TIME_REPAIR, } @@ -42,18 +50,25 @@ impl PowerOffStatus { } /// Selector requesting the restore packet. +#[cfg(target_arch = "x86_64")] pub(crate) const SELECT_PACKET: u8 = 0xa5; /// Selector requesting the VM generation identifier. +#[cfg(target_arch = "x86_64")] pub(crate) const SELECT_GENERATION_ID: u8 = 0xa6; /// Selector requesting a host time sample. +#[cfg(target_arch = "x86_64")] pub(crate) const SELECT_TIME_SAMPLE: u8 = 0xa7; /// Status bit indicating that a restore packet can be read. +#[cfg(target_arch = "x86_64")] pub(crate) const PORTB_PACKET_AVAILABLE: u8 = 0x02; /// Status bit indicating that a generation identifier can be read. +#[cfg(target_arch = "x86_64")] pub(crate) const PORTB_GENERATION_ID_AVAILABLE: u8 = 0x20; /// Status bit indicating that the time-sample window is supported. +#[cfg(target_arch = "x86_64")] pub(crate) const PORTB_TIME_WINDOW: u8 = 0x40; /// Snapshot-port value acknowledging completed restore repair. +#[cfg(target_arch = "x86_64")] pub(crate) const SNAPSHOT_ACKNOWLEDGE: u8 = 2; /// Top-level NVX runtime directory, which also holds the time tools' locks and private records. @@ -63,10 +78,7 @@ pub(crate) const RUN_DIR: &CStr = c"/run/nvx"; /// workload's root, where even a workload of user ID 0 must reach no lock that it could hold, /// so the locks and private records stay in [`RUN_DIR`]. pub(crate) const TIME_DIR: &CStr = c"/run/nvx/time"; -/// Published time state file. -pub(crate) const STATE_PATH: &CStr = c"/run/nvx/time/state"; -/// Temporary state file atomically renamed into place. -const STATE_TEMP_PATH: &CStr = c"/run/nvx/time/.state"; +pub(crate) use super::state::STATE_PATH; /// Advisory lock protecting state updates. const STATE_LOCK_PATH: &CStr = c"/run/nvx/time-state.lock"; /// Published time-daemon process identifier. @@ -76,17 +88,25 @@ pub(crate) const DAEMON_PID_TEMP_PATH: &CStr = c"/run/nvx/time/daemon.pid.tmp"; /// The time daemon's command name, which it takes whichever program started it, and by which /// the published process identifier is recognized. pub(crate) const DAEMON_NAME: &CStr = c"nvx-time"; +/// Private daemon ownership, separate from the root assembler's runtime-directory reservation. +pub(crate) const DAEMON_LOCK_PATH: &CStr = c"/run/nvx/time-daemon.lock"; /// Saved stall-detector settings during capture/restore. +#[cfg(target_arch = "x86_64")] pub(crate) const SUPPRESSION_PATH: &CStr = c"/run/nvx/time-suppression"; /// Advisory lock protecting suppression state. +#[cfg(target_arch = "x86_64")] const SUPPRESSION_LOCK_PATH: &CStr = c"/run/nvx/time-suppression.lock"; /// Private restore record consumed by the daemon. +#[cfg(target_arch = "x86_64")] pub(crate) const RESTORE_PATH: &CStr = c"/run/nvx/time-restore"; /// Temporary restore-record file. +#[cfg(target_arch = "x86_64")] pub(crate) const RESTORE_TEMP_PATH: &CStr = c"/run/nvx/time-restore.tmp"; /// Advisory lock serializing port-window transactions. +#[cfg(target_arch = "x86_64")] pub(crate) const PORTB_LOCK_PATH: &CStr = c"/run/nvx/portb.lock"; /// Temporary mount point used to force an RCU grace period. +#[cfg(target_arch = "x86_64")] pub(crate) const RCU_SYNC_PATH: &CStr = c"/run/nvx/rcu-sync"; /// Mode of the runtime directories: `rwxr-xr-x`. @@ -104,10 +124,12 @@ const PUBLIC_FILE_MODE: Mode = Mode::S_IRUSR pub(crate) const PRIVATE_FILE_MODE: Mode = Mode::S_IRUSR.union(Mode::S_IWUSR); /// Longest prefix of a path that a restore diagnostic quotes, as C's `%.200s` did. +#[cfg(target_arch = "x86_64")] const QUOTED_PATH_LIMIT: usize = 200; /// The stall detectors that capture suppresses, and the value that suppresses each: RCU's stall /// warnings, the soft-lockup detector, and the hung-task detector. Only the first must exist. +#[cfg(target_arch = "x86_64")] const SUPPRESSED_DETECTORS: [(&CStr, i64); 3] = [ (kernel::RCU_STALL_SUPPRESS, 1), (kernel::SOFT_WATCHDOG, 0), @@ -119,13 +141,9 @@ const KERNEL_LOG_PRIORITY: &[u8] = b"<2>"; /// The priority prefix of a diagnostic that the program writes to the kernel log: `KERN_ERR`. const DIAGNOSTIC_PRIORITY: &[u8] = b"<3>"; -/// Number of host samples that boot, restore repair, and each discipline poll take at most. -pub(crate) const SAMPLE_ATTEMPTS: usize = 3; -/// The largest uncertainty of a time sample that the guest accepts, in nanoseconds: the host -/// pairs its UTC with the guest TSC within 100 µs. -pub(crate) const SAMPLE_BOUND_NS: i64 = 100_000; /// The smallest declared TSC rate, in Hz, that pairs a time sample: any lower rate is a misread /// of the frequency MSR. +#[cfg(target_arch = "x86_64")] const MIN_TSC_HZ: u64 = 1_000_000; /// Seconds after which RCU reports a CPU stall in NVX's guest kernels, which the checks require @@ -137,17 +155,23 @@ pub(crate) const DEFAULT_STALL_TIMEOUT_S: i64 = 21; /// testable. pub(crate) trait PrivilegedOps: Sync { /// Reads the time-stamp counter after the instructions before it. + #[cfg(target_arch = "x86_64")] fn read_tsc(&self) -> u64; /// Reads the realtime clock, in nanoseconds since the Unix epoch. + #[cfg(target_arch = "x86_64")] fn realtime_ns(&self) -> i64; /// Takes the calling thread's permission for the time ABI's ports, for one transaction, /// which [`PortAccess::release`] ends. + #[cfg(target_arch = "x86_64")] fn open_ports(&self) -> Result, PortError>; /// Opens the MSR device of `cpu`. + #[cfg(target_arch = "x86_64")] fn open_msr(&self, cpu: usize, access: MsrAccess) -> Result; /// Reads the register `msr`. + #[cfg(target_arch = "x86_64")] fn read_msr(&self, descriptor: &MsrDescriptor, msr: u32) -> Result; /// Writes `value` to the register `msr`. + #[cfg(target_arch = "x86_64")] fn write_msr(&self, descriptor: &MsrDescriptor, msr: u32, value: u64) -> Result<(), Errno>; /// Applies `adjustment` to the wall clock's discipline. fn adjust_clock(&self, adjustment: ClockAdjustment) -> Result; @@ -161,6 +185,7 @@ pub(crate) trait PrivilegedOps: Sync { } /// An MSR device, or the token of a test's fake. +#[cfg(target_arch = "x86_64")] pub(crate) enum MsrDescriptor { /// A device that the kernel opened. Real(MsrFile), @@ -177,22 +202,27 @@ pub(crate) enum MsrDescriptor { pub(crate) struct RealOps; impl PrivilegedOps for RealOps { + #[cfg(target_arch = "x86_64")] fn read_tsc(&self) -> u64 { x86::rdtsc_lfence() } + #[cfg(target_arch = "x86_64")] fn realtime_ns(&self) -> i64 { clock_ns(sys::Clock::Realtime) } + #[cfg(target_arch = "x86_64")] fn open_ports(&self) -> Result, PortError> { PortAccess::request() } + #[cfg(target_arch = "x86_64")] fn open_msr(&self, cpu: usize, access: MsrAccess) -> Result { MsrFile::open(cpu, access).map(MsrDescriptor::Real) } + #[cfg(target_arch = "x86_64")] fn read_msr(&self, descriptor: &MsrDescriptor, msr: u32) -> Result { match descriptor { MsrDescriptor::Real(file) => file.read(msr), @@ -201,6 +231,7 @@ impl PrivilegedOps for RealOps { } } + #[cfg(target_arch = "x86_64")] fn write_msr(&self, descriptor: &MsrDescriptor, msr: u32, value: u64) -> Result<(), Errno> { match descriptor { MsrDescriptor::Real(file) => file.write(msr, value), @@ -241,6 +272,7 @@ impl PrivilegedOps for RealOps { /// Returns the negative `errno` of an MSR access, or zero for one that succeeded, as the time /// ABI's diagnostics print it. +#[cfg(target_arch = "x86_64")] pub(crate) fn msr_error(result: &Result) -> i32 { result.as_ref().err().map_or(0, |&error| -(error as i32)) } @@ -259,6 +291,7 @@ pub(crate) struct Runtime<'a> { /// Current VM generation. generation: AtomicU32, /// The declared TSC rate, `F`, in Hz, once known, or zero. + #[cfg(target_arch = "x86_64")] tsc_hz: AtomicU64, /// Whether an asynchronous child may have interrupted console output. async_output: AtomicBool, @@ -275,6 +308,7 @@ impl<'a> Runtime<'a> { strict: false, violation: Mutex::new(None), generation: AtomicU32::new(0), + #[cfg(target_arch = "x86_64")] tsc_hz: AtomicU64::new(0), async_output: AtomicBool::new(false), report_lock: Mutex::new(()), @@ -295,6 +329,13 @@ impl<'a> Runtime<'a> { self.report_only } + /// Reports an operation that rejected a sample without turning rejection into a violation. + pub(crate) fn log_sample_failure(&self, detail: &dyn Display) { + let line = format!("NVX-TIME-DIAGNOSTIC: rejected sample: {detail}\n"); + self.ops + .write_kernel_log(&[DIAGNOSTIC_PRIORITY, line.as_bytes()].concat()); + } + /// Returns whether a violation fails the call instead of powering off the VM. pub(crate) fn is_strict(&self) -> bool { self.strict @@ -321,6 +362,7 @@ impl<'a> Runtime<'a> { /// Records the declared TSC rate, `F`, in Hz, that the boot checks validated, unless it is /// zero, for none. + #[cfg(target_arch = "x86_64")] pub(crate) fn set_tsc_hz(&self, value: u64) { if value != 0 { self.tsc_hz.store(value, Ordering::Relaxed); @@ -329,6 +371,7 @@ impl<'a> Runtime<'a> { /// Returns the declared TSC rate, `F`, in Hz: the one that the runtime recorded, or else the /// frequency MSR's, of CPU 0, which it then records. + #[cfg(target_arch = "x86_64")] pub(crate) fn tsc_hz(&self) -> Result { let recorded = self.tsc_hz.load(Ordering::Relaxed); if recorded != 0 { @@ -354,6 +397,7 @@ impl<'a> Runtime<'a> { /// Takes the calling thread's permission for the time ABI's ports, for one transaction, /// which [`PortAccess::release`] ends. + #[cfg(target_arch = "x86_64")] pub(crate) fn open_ports(&self) -> Result, PortError> { self.ops.open_ports() } @@ -363,6 +407,7 @@ impl<'a> Runtime<'a> { /// event's write to OpenVMM's debug console, and in the time daemon, whose standard streams /// are on the null device. Until a later transaction revokes the permission, the thread /// starts nothing, as [`fork_process`] checks. + #[cfg(target_arch = "x86_64")] pub(crate) fn log_release_failure(&self, error: PortError) { let record = format!("nvx-time: {error}\n"); self.ops @@ -400,6 +445,7 @@ impl<'a> Runtime<'a> { ); self.ops .write_kernel_log(&[KERNEL_LOG_PRIORITY, line.as_bytes()].concat()); + #[cfg(target_arch = "x86_64")] if let Ok(ports) = self.open_ports() { if self.async_output.load(Ordering::Relaxed) { ports.write_u8(Port::PortbData, b'\n'); @@ -413,6 +459,19 @@ impl<'a> Runtime<'a> { self.log_release_failure(error); } } + #[cfg(target_arch = "aarch64")] + { + let separator = if self.async_output.load(Ordering::Relaxed) { + b"\n".as_slice() + } else { + b"".as_slice() + }; + if let Err(error) = sys::platform::write_console(separator, line.as_bytes()) { + let diagnostic = format!("nvx-time: platform event output: {error}\n"); + self.ops + .write_kernel_log(&[DIAGNOSTIC_PRIORITY, diagnostic.as_bytes()].concat()); + } + } let separator: &[u8] = if self.async_output.load(Ordering::Relaxed) { b"\n" } else { @@ -445,6 +504,7 @@ impl<'a> Runtime<'a> { /// Reports that restore-time repair failed, a fatal event of the restore phase, and powers off /// with the repair status unless report-only or strict. + #[cfg(target_arch = "x86_64")] pub(crate) fn repair_failure(&self, code: ViolationCode, detail: &[u8]) { self.fail_fatal( PowerOffStatus::Repair, @@ -480,11 +540,42 @@ pub(crate) fn cpu_time_ns() -> i64 { clock_ns(sys::Clock::ProcessCpuTime) } +/// Checks the same portable detector policy on both guest architectures. +pub(crate) fn check_debug_watchdogs() -> Result<(), String> { + let setting = |path| match sys::read_number::(path) { + Ok(value) => Ok(Some(value)), + Err(Errno::ENOENT) => Ok(None), + Err(error) => Err(format!("read {path:?}: {error}")), + }; + debug_watchdog_policy( + setting(kernel::SOFT_WATCHDOG)?, + setting(kernel::HUNG_TASK_TIMEOUT)?, + ) +} + +/// Allows a kernel with neither detector, but requires enabled detectors when exposed. +pub(super) fn debug_watchdog_policy(soft: Option, hung: Option) -> Result<(), String> { + if soft.is_none() && hung.is_none() { + return Ok(()); + } + if soft != Some(1) { + return Err(format!("soft_watchdog is {}", soft.unwrap_or(-1))); + } + match hung { + Some(timeout) if timeout != 0 => Ok(()), + _ => Err(format!("hung_task_timeout_secs is {}", hung.unwrap_or(-1))), + } +} + /// Why the time tools could not start a process. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum SpawnError { /// The calling thread may hold port permission, which the process would inherit. + #[cfg(target_arch = "x86_64")] Ports(PortError), + /// The thread may retain restricted platform authority. + #[cfg(target_arch = "aarch64")] + Authority(Errno), /// `fork` failed. Fork(Errno), } @@ -494,7 +585,10 @@ impl Display for SpawnError { /// port permission, such as `ioperm revoke: Cannot allocate memory`. fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { + #[cfg(target_arch = "x86_64")] Self::Ports(error) => error.fmt(formatter), + #[cfg(target_arch = "aarch64")] + Self::Authority(error) => write!(formatter, "platform release: {error}"), Self::Fork(error) => formatter.write_str(error.desc()), } } @@ -505,7 +599,10 @@ impl std::error::Error for SpawnError {} /// Forks the process, as [`sys::fork`] does, once the calling thread holds no port permission, /// which the child would inherit. pub(crate) fn fork_process() -> Result { + #[cfg(target_arch = "x86_64")] sys::check_released().map_err(SpawnError::Ports)?; + #[cfg(target_arch = "aarch64")] + sys::check_released().map_err(SpawnError::Authority)?; sys::fork().map_err(SpawnError::Fork) } @@ -532,23 +629,50 @@ pub(crate) fn lock_file(path: &CStr) -> Result { /// Loads and parses the published time state. pub(crate) fn state_load() -> Result { - let text = sys::read_file(STATE_PATH)?; - TimeState::parse(&text).ok_or(Errno::EINVAL) + TimeState::load() } /// Atomically persists one complete time state. pub(crate) fn state_store(state: &TimeState) -> Result<(), Errno> { - replace_file(STATE_PATH, STATE_TEMP_PATH, state.to_string().as_bytes()) + state.store() } /// Applies one read-modify-write update to the time state under its lock. pub(crate) fn state_apply(update: impl FnOnce(&mut TimeState)) -> Result<(), Errno> { let _lock = lock_file(STATE_LOCK_PATH)?; - let mut state = state_load().unwrap_or_default(); + let mut state = state_load()?; update(&mut state); state_store(&state) } +/// Publishes one phase through the same locked state update on either architecture. +pub(crate) fn record_check( + phase: Phase, + mut check: CheckState, + counter_hz: u64, + timer_hz: u64, +) -> Result<(), Errno> { + if phase.check_index().is_none() { + return Err(Errno::EINVAL); + } + state_apply(|state| { + check.generation = if phase == Phase::Boot { + 0 + } else { + state.generation + }; + if let Some(record) = state.check_mut(phase) { + *record = check; + } + if counter_hz != 0 { + state.tsc_hz = counter_hz; + } + if timer_hz != 0 { + state.lapic_hz = timer_hz; + } + }) +} + /// Increments the persisted violation counter when state exists. fn count_violation() -> Result<(), Errno> { if !sys::path_exists(STATE_PATH) { @@ -558,6 +682,7 @@ fn count_violation() -> Result<(), Errno> { } /// Loads validated TSC and LAPIC rates from state. +#[cfg(target_arch = "x86_64")] pub(crate) fn rates_load() -> Result<(u64, u64), Errno> { let state = state_load()?; if state.tsc_hz == 0 || state.lapic_hz == 0 { @@ -570,6 +695,7 @@ pub(crate) fn rates_load() -> Result<(u64, u64), Errno> { /// Saves the stall detectors' settings in the suppression record, and suppresses them for a /// snapshot capture; see [`Suppression::save`]. A record that exists already, of a pending /// capture, is `EEXIST`. +#[cfg(target_arch = "x86_64")] pub(crate) fn suppression_save() -> Result<(), SaveError> { make_runtime_directories(); SUPPRESSION.save() @@ -577,6 +703,7 @@ pub(crate) fn suppression_save() -> Result<(), SaveError> { /// Why a save of the stall detectors' settings failed. #[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg(target_arch = "x86_64")] pub(crate) enum SaveError { /// The save failed with this error, and left the settings as they were: it changed none, or /// undid every change and removed its record. A record that exists already, of a pending @@ -594,6 +721,7 @@ fn describe(prefix: &[u8], error: Errno) -> Vec { /// Restores the stall detectors' settings from the suppression record, and removes the record; /// see [`Suppression::restore`]. A missing record succeeds when `missing_ok`. +#[cfg(target_arch = "x86_64")] pub(crate) fn suppression_restore(missing_ok: bool) -> Result<(), Vec> { make_runtime_directories(); SUPPRESSION.restore(missing_ok) @@ -602,6 +730,7 @@ pub(crate) fn suppression_restore(missing_ok: bool) -> Result<(), Vec> { /// The suppression of the stall detectors for a capture: the detectors, the record of their /// saved settings, which exists while a capture is pending, and the lock that serializes the /// record's saves and restores, so that each restores only the settings that a save recorded. +#[cfg(target_arch = "x86_64")] struct Suppression<'a> { /// The detectors, of which only the first must exist, and the value that suppresses each. detectors: &'a [(&'a CStr, i64)], @@ -612,12 +741,14 @@ struct Suppression<'a> { } /// The guest's suppression of its stall detectors. +#[cfg(target_arch = "x86_64")] const SUPPRESSION: Suppression<'static> = Suppression { detectors: &SUPPRESSED_DETECTORS, record: SUPPRESSION_PATH, lock: SUPPRESSION_LOCK_PATH, }; +#[cfg(target_arch = "x86_64")] impl Suppression<'_> { /// Saves the detectors' settings in the record, and suppresses the detectors, under the /// lock, so that no restore runs between its reads and its writes. A record that exists @@ -706,6 +837,7 @@ impl Suppression<'_> { } /// Reads a byte buffer using one scalar four-byte port access per chunk. +#[cfg(target_arch = "x86_64")] pub(crate) fn portb_read(ports: &PortAccess<'_>, port: Port, buffer: &mut [u8]) { for chunk in buffer.chunks_mut(size_of::()) { let bytes = ports.read_u32(port).to_ne_bytes(); @@ -714,11 +846,13 @@ pub(crate) fn portb_read(ports: &PortAccess<'_>, port: Port, buffer: &mut [u8]) } /// Locks the shared port-window transaction. +#[cfg(target_arch = "x86_64")] pub(crate) fn lock_portb() -> Result { lock_file(PORTB_LOCK_PATH) } /// Formats a failure of [`lock_portb`]: `portb lock: `, then the description of `error`. +#[cfg(target_arch = "x86_64")] pub(crate) fn portb_lock_failure(error: Errno) -> Vec { describe(b"portb lock: ", error) } @@ -726,6 +860,7 @@ pub(crate) fn portb_lock_failure(error: Errno) -> Vec { /// Latches and decodes one host time sample: brackets the selector write with TSC reads, then /// reads the realtime clock between two more, with which the guest pairs the sample's guest TSC, /// before it reads the time window. +#[cfg(target_arch = "x86_64")] pub(crate) fn take_sample( ports: &PortAccess<'_>, ops: &dyn PrivilegedOps, @@ -748,43 +883,81 @@ pub(crate) fn take_sample( Ok((TimeSample::parse(&bytes)?, reads)) } -/// Narrowest valid sample observed during one attempt group, and the frequency correction that -/// its host clock correction asks for. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) struct BestSample { - /// Guest-to-host offset in nanoseconds. - pub(crate) theta: i64, - /// Pairing uncertainty in nanoseconds. - pub(crate) epsilon: i64, - /// The discipline of host UTC that the sample published. - pub(crate) host: HostClock, - /// The frequency correction, in scaled ppm, that runs the wall clock at host UTC's rate. - pub(crate) frequency: i64, - /// Whether Linux's ±500 ppm limit clamped `frequency`. - pub(crate) limited: bool, - /// Whether the sample carried the test-hook flag: host overrides, which only tests make, of - /// the time that it published were active. - pub(crate) test_hooks: bool, -} - -/// Whether the selected sample satisfied the requested uncertainty bound. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum SampleResult { - /// The sample was within the bound. - Accepted, - /// A valid sample was selected but remained wider than the bound. - Wide, +/// Reads x86 hardware only; command dispatch, formatting and pairing are shared. +#[cfg(target_arch = "x86_64")] +pub(crate) fn diagnostic_sample( + runtime: &Runtime<'_>, +) -> Result> { + let rates = clock_rates(runtime).map_err(|error| error.to_string().into_bytes())?; + let (taken, reads) = { + let ports = runtime + .open_ports() + .map_err(|error| error.to_string().into_bytes())?; + let status = ports.read_u8(Port::PortbStatus); + if status & PORTB_TIME_WINDOW == 0 { + return Err(format!("portb status 0x{status:02x} has no time window").into_bytes()); + } + let _lock = lock_portb().map_err(portb_lock_failure)?; + take_sample(&ports, runtime.ops).map_err(|error| error.to_string().into_bytes())? + }; + Ok(sample::DiagnosticSample { + sample: sample::Sample { + flags: taken.flags, + generation: taken.generation, + utc_ns: taken.utc_ns, + counter: taken.tsc, + pairing_ns: taken.pairing_ns, + host_status: match taken.host.status { + super::state::HostClockStatus::Unknown => 0, + super::state::HostClockStatus::Synchronized => 1, + super::state::HostClockStatus::FreeRunning => 2, + super::state::HostClockStatus::Unreliable => 3, + }, + disruption: taken.host.disruption, + correction: taken.host.correction, + correction_bound: taken.host.correction_bound, + max_error_us: taken.host.max_error_us, + }, + reads: sample::Reads { + before: reads.before, + after: reads.after, + realtime_ns: reads.realtime_ns, + realtime_counter: reads.realtime_tsc, + realtime_half_cycles: reads.realtime_half_cycles, + }, + rates: sample::Rates { + counter_hz: rates.tsc_hz, + kernel_hz: decoders::kernel_tsc_hz(rates.tsc_hz).max(1000), + frequency: rates.frequency, + }, + }) +} + +/// Reads the same diagnostic record over the admitted ARM binding. +#[cfg(target_arch = "aarch64")] +pub(crate) fn diagnostic_sample( + _runtime: &Runtime<'_>, +) -> Result> { + sample::arm64::Clock::open() + .and_then(|clock| clock.diagnostic()) + .map_err(|error| error.to_string().into_bytes()) } /// Why no host time sample could be selected. Its `Display` is the diagnostic. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Debug)] +#[cfg_attr(target_arch = "x86_64", derive(Clone, Copy, PartialEq, Eq))] pub(crate) enum SampleError { + /// A Linux clock operation needed to interpret the sample failed. + Operation(&'static str, Errno), /// The thread could not take, or give up, the permission for the ports. + #[cfg(target_arch = "x86_64")] Ports(PortError), /// The guest could not read the declared TSC rate, with which it pairs a sample, from the /// frequency MSR: the error, or the implausible rate that it read. + #[cfg(target_arch = "x86_64")] TscRate(Result), /// The port window, of this status, offers no time sample. + #[cfg(target_arch = "x86_64")] NoTimeWindow(u8), /// A sample came from another VM generation, which a restore must handle first. Generation { @@ -794,14 +967,21 @@ pub(crate) enum SampleError { expected: u32, }, /// No attempt took a valid sample: the last attempt's error, if any. + #[cfg(target_arch = "x86_64")] Invalid(Option), + /// The restricted ARM transaction or effective architectural counter was invalid. + #[cfg(target_arch = "aarch64")] + Arm(sample::arm64::Error), } impl SampleError { /// Returns how the discipline records the rejection of a poll's sample that failed so. pub(crate) fn rejection(&self) -> SampleRejection { match self { + #[cfg(target_arch = "x86_64")] Self::Invalid(Some(TimeSampleError::Tsc { .. })) => SampleRejection::Tsc, + #[cfg(target_arch = "aarch64")] + Self::Arm(sample::arm64::Error::Counter(_)) => SampleRejection::Tsc, _ => SampleRejection::Invalid, } } @@ -810,19 +990,24 @@ impl SampleError { impl Display for SampleError { /// Writes the diagnostic, such as `time sample generation 7, expected 6`. fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { + match self { + Self::Operation(operation, error) => write!(formatter, "{operation}: {error}"), + #[cfg(target_arch = "x86_64")] Self::Ports(error) => error.fmt(formatter), + #[cfg(target_arch = "x86_64")] Self::TscRate(Err(error)) => write!( formatter, "MSR {:#010x} read: {}", msr::HV_TSC_FREQUENCY, error.desc() ), + #[cfg(target_arch = "x86_64")] Self::TscRate(Ok(rate)) => write!( formatter, "MSR {:#010x} (TSC rate) is {rate} Hz", msr::HV_TSC_FREQUENCY ), + #[cfg(target_arch = "x86_64")] Self::NoTimeWindow(status) => write!( formatter, "portb status 0x{status:02x} has no time-sample window" @@ -831,25 +1016,49 @@ impl Display for SampleError { formatter, "time sample generation {sample}, expected {expected}" ), + #[cfg(target_arch = "x86_64")] Self::Invalid(Some(error)) => error.fmt(formatter), + #[cfg(target_arch = "x86_64")] Self::Invalid(None) => formatter.write_str("no valid time sample"), + #[cfg(target_arch = "aarch64")] + Self::Arm(error) => error.fmt(formatter), } } } impl std::error::Error for SampleError {} +#[cfg(target_arch = "aarch64")] +impl From for SampleError { + fn from(error: sample::arm64::Error) -> Self { + match error { + sample::arm64::Error::Operation(operation, error) => Self::Operation(operation, error), + sample::arm64::Error::Generation { observed, expected } => Self::Generation { + sample: observed, + expected, + }, + error => Self::Arm(error), + } + } +} + /// Returns the rates with which the guest's realtime clock counts its TSC: the declared TSC rate, /// and the kernel's frequency correction. +#[cfg(target_arch = "x86_64")] pub(crate) fn clock_rates(runtime: &Runtime<'_>) -> Result { Ok(ClockRates { tsc_hz: runtime.tsc_hz()?, - frequency: current_frequency(runtime), + frequency: runtime + .ops + .adjust_clock(ClockAdjustment::Query) + .map_err(|error| SampleError::Operation("query adjtimex", error))? + .scaled_ppm, }) } /// Selects the lowest-uncertainty valid sample from a bounded attempt count, in one port /// transaction, which ends before this returns. +#[cfg(target_arch = "x86_64")] pub(crate) fn best_sample( runtime: &Runtime<'_>, attempts: usize, @@ -865,6 +1074,7 @@ pub(crate) fn best_sample( /// Selects the lowest-uncertainty valid sample through `ports`, pairing each with the guest's /// clocks at `rates`, as [`best_sample`] does. +#[cfg(target_arch = "x86_64")] pub(crate) fn select_sample( ports: &PortAccess<'_>, ops: &dyn PrivilegedOps, @@ -877,52 +1087,88 @@ pub(crate) fn select_sample( if status & PORTB_TIME_WINDOW == 0 { return Err(SampleError::NoTimeWindow(status)); } - let mut best: Option = None; - let mut invalid = None; - for _ in 0..attempts { - let (sample, reads) = match take_sample(ports, ops) { - Ok(taken) => taken, - Err(error) => { - invalid = Some(error); - continue; - } - }; + sample::select_sample(attempts, bound_ns, SampleError::Invalid(None), || { + let (sample, reads) = take_sample(ports, ops) + .map_err(|error| sample::AttemptError::Retry(SampleError::Invalid(Some(error))))?; if sample.generation != generation { - return Err(SampleError::Generation { + return Err(sample::AttemptError::Fatal(SampleError::Generation { sample: sample.generation, expected: generation, - }); + })); } - let (theta, epsilon) = match decoders::pair_sample(&sample, &reads, rates) { - Ok(pairing) => pairing, - Err(error) => { - invalid = Some(error); - continue; - } - }; + let (theta, epsilon) = decoders::pair_sample(&sample, &reads, rates) + .map_err(|error| sample::AttemptError::Retry(SampleError::Invalid(Some(error))))?; let (frequency, limited) = decoders::guest_frequency(sample.host.correction, rates.tsc_hz); - let candidate = BestSample { + Ok(BestSample { theta, epsilon, host: sample.host, frequency, limited, test_hooks: sample.test_hooks(), - }; - if best.is_none_or(|best| epsilon < best.epsilon) { - best = Some(candidate); - } - if epsilon <= bound_ns { - return Ok((SampleResult::Accepted, candidate)); - } - } - best.map(|best| (SampleResult::Wide, best)) - .ok_or(SampleError::Invalid(invalid)) + }) + }) +} + +/// Takes a sample using the admitted binding and the same bounded selection as x86. +#[cfg(target_arch = "aarch64")] +pub(crate) fn best_sample( + _runtime: &Runtime<'_>, + attempts: usize, + bound_ns: i64, + generation: u32, +) -> Result<(SampleResult, BestSample), SampleError> { + let clock = sample::arm64::Clock::open().map_err(SampleError::from)?; + sample::select_sample( + attempts, + bound_ns, + SampleError::Operation("no valid time sample", Errno::EPROTO), + || match clock.sample(generation) { + Ok(paired) => Ok(paired.measurement()), + Err(error @ (sample::arm64::Error::Wire(_) | sample::arm64::Error::Counter(_))) => { + Err(sample::AttemptError::Retry(SampleError::from(error))) + } + Err(error) => Err(sample::AttemptError::Fatal(SampleError::from(error))), + }, + ) +} + +/// Serializes x86 sample/correction/state as before; ARM serializes each device transaction. +pub(crate) fn discipline_sample( + runtime: &Runtime<'_>, +) -> ( + Result<(SampleResult, BestSample), SampleError>, + Option, +) { + #[cfg(target_arch = "x86_64")] + let lock = lock_portb().ok(); + #[cfg(target_arch = "x86_64")] + let result = if lock.is_some() { + best_sample( + runtime, + SAMPLE_ATTEMPTS, + SAMPLE_BOUND_NS, + runtime.generation(), + ) + } else { + Err(SampleError::Invalid(None)) + }; + #[cfg(target_arch = "aarch64")] + let (lock, result) = ( + None, + best_sample( + runtime, + SAMPLE_ATTEMPTS, + SAMPLE_BOUND_NS, + runtime.generation(), + ), + ); + (result, lock) } /// Steps realtime by a signed nanosecond offset. pub(crate) fn step_clock(runtime: &Runtime<'_>, offset_ns: i64) -> Result<(), Errno> { - let (seconds, nanoseconds) = decoders::split_offset(offset_ns); + let (seconds, nanoseconds) = sample::split_offset(offset_ns); runtime .ops .adjust_clock(ClockAdjustment::Step { @@ -955,7 +1201,7 @@ pub(crate) fn synchronize(runtime: &Runtime<'_>, sample: &BestSample) -> Result< step_clock(runtime, sample.theta)?; hold_frequency( runtime, - decoders::frequency_hold(0, sample.epsilon, sample.frequency, sample.host.error_us()), + sample::frequency_hold(0, sample.epsilon, sample.frequency, sample.host.error_us()), ) } @@ -963,6 +1209,7 @@ pub(crate) fn synchronize(runtime: &Runtime<'_>, sample: &BestSample) -> Result< /// correction that the host's clock correction asks for. pub(crate) fn initial_sample(runtime: &Runtime<'_>) -> Result> { let (result, sample) = { + #[cfg(target_arch = "x86_64")] let _lock = lock_portb().map_err(portb_lock_failure)?; best_sample( runtime, @@ -987,6 +1234,7 @@ pub(crate) fn wide_sample(epsilon: i64) -> Vec { /// Writes `entropy` at the start of the restored entropy file, and reports a failure to write /// or close it with the file's path. +#[cfg(target_arch = "x86_64")] pub(crate) fn write_entropy( file: FileDescriptor, path: &CStr, @@ -1011,5 +1259,5 @@ pub(crate) fn write_entropy( )) } -#[cfg(test)] +#[cfg(all(test, target_arch = "x86_64"))] pub(crate) mod tests; diff --git a/nvx-rs/src/guest/time/system/tests.rs b/nvx-rs/src/guest/time/system/tests.rs index 7aca1d5c7..8baace925 100644 --- a/nvx-rs/src/guest/time/system/tests.rs +++ b/nvx-rs/src/guest/time/system/tests.rs @@ -909,3 +909,5 @@ fn a_save_waits_for_the_suppression_lock() { }); assert_eq!(test.settings(), settings([Some("1"), Some("0"), Some("0")])); } +use crate::guest::time::sample::HostClock; +use crate::guest::time::state::STATE_TEMP_PATH; diff --git a/nvx-rs/src/guest/time/test_mode.rs b/nvx-rs/src/guest/time/test_mode.rs index bfc324a52..7f0f7bba2 100644 --- a/nvx-rs/src/guest/time/test_mode.rs +++ b/nvx-rs/src/guest/time/test_mode.rs @@ -718,7 +718,7 @@ fn test_promote(out: &mut dyn Write) -> Result<(), CommandError> { let start = clock_ns(sys::Clock::Monotonic); let promoter = start_promoter(start, CANCELLED_PROMOTION_DELAY_NS)?; let _idle = sys::IdlePriority::enter(); - promoter.stop(); + drop(promoter); let _ = writeln!( out, "stopped caller={} stop_ms={}", diff --git a/nvx-rs/src/guest/time/tests.rs b/nvx-rs/src/guest/time/tests.rs index 8d6184f60..064bf30f2 100644 --- a/nvx-rs/src/guest/time/tests.rs +++ b/nvx-rs/src/guest/time/tests.rs @@ -1,6 +1,20 @@ use super::*; use crate::guest::status::tests::command_line; +#[test] +fn both_architectures_require_enabled_watchdogs_when_the_interfaces_exist() { + assert!(system::debug_watchdog_policy(None, None).is_ok()); + assert!(system::debug_watchdog_policy(Some(1), Some(120)).is_ok()); + for (soft, hung) in [ + (Some(0), Some(120)), + (Some(1), Some(0)), + (None, Some(120)), + (Some(1), None), + ] { + assert!(system::debug_watchdog_policy(soft, hung).is_err()); + } +} + /// Returns the command line of `nvx-time` with `values`. fn args(values: &[&[u8]]) -> Vec { command_line("nvx-time", values) @@ -31,3 +45,24 @@ fn checked_phases_have_state_file_positions() { assert_eq!(positions, [Some(0), Some(1), Some(2)]); assert_eq!(Phase::Runtime.check_index(), None); } + +#[test] +#[cfg(target_arch = "aarch64")] +fn coldboot_cli_rejects_snapshot_commands_without_accessing_the_platform() { + for command in [ + "capture", + "restore-finish", + "pre-capture", + "cancel-capture", + "check", + ] { + let mut out = Vec::new(); + let mut err = Vec::new(); + assert_eq!( + run(&args(&[command.as_bytes()]), &mut out, &mut err), + Err(CommandError::Usage) + ); + assert_eq!(err, USAGE); + assert!(out.is_empty()); + } +} diff --git a/nvx-rs/src/host/artifacts/architecture.rs b/nvx-rs/src/host/artifacts/architecture.rs new file mode 100644 index 000000000..fe903071d --- /dev/null +++ b/nvx-rs/src/host/artifacts/architecture.rs @@ -0,0 +1,50 @@ +//! Explicit guest artifact ISA, independent of the host's hypervisor backend. + +/// The ISA of a guest artifact set. Existing constructors retain the x86-64 default. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum Architecture { + /// Existing x86-64 artifact paths and formats. + #[default] + X86_64, + /// ARM64 cold-boot `Image` and architecture-qualified guest artifacts. + Aarch64, +} + +impl Architecture { + /// Selects the native ISA for runtime commands, not the legacy producer/API default. + #[cfg(feature = "cli")] + pub(crate) const fn native() -> Self { + if cfg!(target_arch = "aarch64") { + Self::Aarch64 + } else { + Self::X86_64 + } + } + + /// Returns the canonical build/manifest architecture token. + pub const fn name(self) -> &'static str { + match self { + Self::X86_64 => "x86_64", + Self::Aarch64 => "aarch64", + } + } + + /// Returns the kernel filename in this architecture's existing producer output. + pub const fn kernel_name(self) -> &'static str { + match self { + Self::X86_64 => super::KERNEL_NAME, + Self::Aarch64 => "Image", + } + } + + /// Returns the corresponding kernel provenance filename. + pub(super) const fn kernel_provenance_name(self) -> &'static str { + match self { + Self::X86_64 => super::KERNEL_PROVENANCE_NAME, + Self::Aarch64 => "Image.provenance.json", + } + } +} + +#[cfg(test)] +mod tests; diff --git a/nvx-rs/src/host/artifacts/architecture/tests.rs b/nvx-rs/src/host/artifacts/architecture/tests.rs new file mode 100644 index 000000000..c8227d23f --- /dev/null +++ b/nvx-rs/src/host/artifacts/architecture/tests.rs @@ -0,0 +1,162 @@ +use std::fs; + +use super::*; +use crate::host::artifacts::{ArtifactDir, Guest, openvmm_executable}; + +#[test] +#[cfg(all(windows, feature = "cli"))] +fn arm_native_vmm_admission_rejects_foreign_or_truncated_pe_images() { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join("openvmm.exe"); + let mut bytes = [0; 70]; + bytes[..2].copy_from_slice(b"MZ"); + bytes[60..64].copy_from_slice(&64_u32.to_le_bytes()); + bytes[64..68].copy_from_slice(b"PE\0\0"); + bytes[68..70].copy_from_slice(&0xaa64_u16.to_le_bytes()); + fs::write(&path, bytes).unwrap(); + assert!(crate::host::artifacts::check_arm64_pe(&path).is_ok()); + for machine in [0x8664_u16, 0x14c] { + bytes[68..70].copy_from_slice(&machine.to_le_bytes()); + fs::write(&path, bytes).unwrap(); + assert!(crate::host::artifacts::check_arm64_pe(&path).is_err()); + } + bytes[60..64].copy_from_slice(&u32::MAX.to_le_bytes()); + fs::write(&path, bytes).unwrap(); + assert!(crate::host::artifacts::check_arm64_pe(&path).is_err()); + fs::write(&path, b"MZ").unwrap(); + assert!(crate::host::artifacts::check_arm64_pe(&path).is_err()); +} + +/// A bounded header fixture, not a bootable or qualified kernel. +fn image() -> [u8; 64] { + let mut image = [0; 64]; + image[16..24].copy_from_slice(&4096_u64.to_le_bytes()); + image[24..32].copy_from_slice(&10_u64.to_le_bytes()); + image[56..60].copy_from_slice(b"ARM\x64"); + image +} + +#[test] +fn explicit_arm_paths_never_change_legacy_default_or_openvmm_location() { + let root = tempfile::tempdir().unwrap(); + let legacy = ArtifactDir::repository(root.path()).unwrap(); + let arm = legacy + .clone() + .with_architecture(Architecture::Aarch64) + .unwrap(); + assert_eq!(legacy.architecture(), Architecture::X86_64); + assert_eq!(legacy.kernel(), root.path().join("build").join("vmlinux")); + assert_eq!( + arm.kernel(), + root.path().join("build").join("aarch64").join("Image") + ); + assert_eq!(arm.openvmm(), legacy.openvmm()); + assert_eq!(arm.openvmm().file_name().unwrap(), openvmm_executable()); + assert_eq!(arm.paths().guests.len(), 2); + assert!(arm.distro_layer(Guest::Ubuntu).is_none()); + assert!(arm.sandbox_artifacts().is_err()); +} + +#[test] +fn missing_arm_image_does_not_fall_back_to_existing_x86_artifacts() { + let root = tempfile::tempdir().unwrap(); + fs::create_dir(root.path().join("build")).unwrap(); + fs::write(root.path().join("build").join("vmlinux"), b"x86").unwrap(); + let arm = ArtifactDir::repository(root.path()) + .unwrap() + .with_architecture(Architecture::Aarch64) + .unwrap(); + assert!( + arm.require_kernel() + .unwrap_err() + .to_string() + .contains("Image") + ); +} + +#[test] +fn arm_image_admission_matches_producer_flags_footprint_and_offset_rules() { + let root = tempfile::tempdir().unwrap(); + let arm = ArtifactDir::repository(root.path()) + .unwrap() + .with_architecture(Architecture::Aarch64) + .unwrap(); + fs::create_dir_all(arm.kernel().parent().unwrap()).unwrap(); + fs::write(arm.kernel(), image()).unwrap(); + assert_eq!(arm.require_kernel().unwrap(), arm.kernel()); + for (offset, value) in [ + (24, 11_u64), + (24, 8), + (24, 2), + (24, 26), + (8, 1), + (8, u64::MAX - 4095), + (16, 63), + ] { + let mut invalid = image(); + invalid[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); + fs::write(arm.kernel(), invalid).unwrap(); + assert!(arm.require_kernel().is_err(), "{offset}: {value}"); + } + fs::write(arm.kernel(), b"\x7fELF").unwrap(); + assert!(arm.require_kernel().is_err()); + let mut wrong_magic = image(); + wrong_magic[56] = 0; + fs::write(arm.kernel(), wrong_magic).unwrap(); + assert!(arm.require_kernel().is_err()); +} + +#[test] +fn arm_guest_admission_requires_matching_package_manifest_architecture() { + let root = tempfile::tempdir().unwrap(); + let arm = ArtifactDir::repository(root.path()) + .unwrap() + .with_architecture(Architecture::Aarch64) + .unwrap(); + fs::create_dir_all(arm.initramfs(Guest::Alpine).parent().unwrap()).unwrap(); + fs::write(arm.initramfs(Guest::Alpine), b"initramfs fixture").unwrap(); + assert!(arm.require_initramfs(Guest::Alpine).is_err()); + fs::write( + arm.package_manifest(Guest::Alpine), + r#"{"architecture":"x86_64"}"#, + ) + .unwrap(); + assert!(arm.require_initramfs(Guest::Alpine).is_err()); + fs::write( + arm.package_manifest(Guest::Alpine), + r#"{"architecture":"aarch64"}"#, + ) + .unwrap(); + assert_eq!( + arm.require_initramfs(Guest::Alpine).unwrap(), + arm.initramfs(Guest::Alpine) + ); + assert!(arm.require_initramfs(Guest::AzureLinux).is_err()); + fs::write(arm.initramfs(Guest::Ubuntu), b"Ubuntu initramfs fixture").unwrap(); + fs::write( + arm.package_manifest(Guest::Ubuntu), + r#"{"architecture":"amd64"}"#, + ) + .unwrap(); + assert!(arm.require_initramfs(Guest::Ubuntu).is_err()); + fs::write( + arm.package_manifest(Guest::Ubuntu), + r#"{"architecture":"arm64"}"#, + ) + .unwrap(); + assert_eq!( + arm.require_initramfs(Guest::Ubuntu).unwrap(), + arm.initramfs(Guest::Ubuntu) + ); +} + +#[test] +fn arm_release_selection_fails_closed_instead_of_inventing_a_catalog_entry() { + let root = tempfile::tempdir().unwrap(); + assert!( + ArtifactDir::release(root.path()) + .unwrap() + .with_architecture(Architecture::Aarch64) + .is_err() + ); +} diff --git a/nvx-rs/src/host/artifacts/dir.rs b/nvx-rs/src/host/artifacts/dir.rs index dddab5c11..ddef271fe 100644 --- a/nvx-rs/src/host/artifacts/dir.rs +++ b/nvx-rs/src/host/artifacts/dir.rs @@ -6,12 +6,12 @@ use std::path::{Path, PathBuf}; use serde::Serialize; -use super::identity::{provenance_identity, read_metadata, sha256_identity}; +use super::identity::{check_arm64_image, provenance_identity, read_metadata, sha256_identity}; use super::require::required; use super::{ - Artifacts, CHECKSUMS_NAME, Guest, INITRAMFS_PROVENANCE_NAME, KERNEL_NAME, - KERNEL_PROVENANCE_NAME, OPENVMM_PROVENANCE_NAME, RELEASE_BIN_DIR, RELEASE_GUEST_DIR, - REPO_BUILD_DIR, REPO_OPENVMM_DIR, absolute, is_release_dir, openvmm_executable, + Architecture, Artifacts, CHECKSUMS_NAME, Guest, INITRAMFS_PROVENANCE_NAME, + OPENVMM_PROVENANCE_NAME, RELEASE_BIN_DIR, RELEASE_GUEST_DIR, REPO_BUILD_DIR, REPO_OPENVMM_DIR, + absolute, is_release_dir, openvmm_executable, }; use crate::error::{Error, Result}; use crate::host::store::{ArtifactIdentity, RuntimeIdentity}; @@ -41,6 +41,8 @@ pub struct ArtifactDir { root: PathBuf, /// Its layout. layout: Layout, + /// Explicit guest ISA; never inferred from an arbitrary file or a failed lookup. + architecture: Architecture, } impl ArtifactDir { @@ -80,9 +82,29 @@ impl ArtifactDir { Ok(Self { root: absolute(root)?, layout, + architecture: Architecture::X86_64, }) } + /// Selects an architecture-qualified local build without changing the legacy constructors. + /// + /// ARM releases remain unavailable until a release/catalog is qualified. A missing ARM + /// build never falls back to the x86 directory. + pub fn with_architecture(mut self, architecture: Architecture) -> Result { + if architecture == Architecture::Aarch64 && self.layout == Layout::Release { + return Err(Error::unsupported( + "ARM64 release artifacts are not qualified; use an explicit local ARM64 build", + )); + } + self.architecture = architecture; + Ok(self) + } + + /// Returns the explicitly selected guest artifact ISA. + pub fn architecture(&self) -> Architecture { + self.architecture + } + /// Returns the absolute directory. pub fn root(&self) -> &Path { &self.root @@ -100,7 +122,11 @@ impl ArtifactDir { Layout::Repository => REPO_BUILD_DIR, Layout::Release => RELEASE_GUEST_DIR, }; - self.root.join(directory).join(name) + let directory = self.root.join(directory); + match self.architecture { + Architecture::X86_64 => directory.join(name), + Architecture::Aarch64 => directory.join("aarch64").join(name), + } } /// Returns the path of the OpenVMM executable, `openvmm_binary_path()` of `nvx.py`: @@ -116,7 +142,7 @@ impl ArtifactDir { /// Returns the path of the guest kernel, which every guest boots. pub fn kernel(&self) -> PathBuf { - self.artifact(KERNEL_NAME) + self.artifact(self.architecture.kernel_name()) } /// Returns the path of a guest's initramfs. @@ -131,11 +157,17 @@ impl ArtifactDir { /// Returns the path of a guest's distro layer, if the guest provides one. pub fn distro_layer(&self, guest: Guest) -> Option { + if self.architecture == Architecture::Aarch64 { + return None; + } guest.distro_layer_name().map(|name| self.artifact(name)) } /// Returns the path of the manifest of a guest's distro layer, if the guest provides one. pub fn distro_manifest(&self, guest: Guest) -> Option { + if self.architecture == Architecture::Aarch64 { + return None; + } guest .distro_manifest_name() .map(|name| self.artifact(&name)) @@ -158,7 +190,11 @@ impl ArtifactDir { /// /// [`ErrorCode::ArtifactMissing`]: crate::ErrorCode::ArtifactMissing pub fn require_kernel(&self) -> Result { - required(self.kernel(), "Linux direct kernel") + let path = required(self.kernel(), "Linux direct kernel")?; + if self.architecture == Architecture::Aarch64 { + check_arm64_image(&path)?; + } + Ok(path) } /// Checks that a guest's initramfs is a regular file and returns its path, as `nvx-rs run` @@ -169,10 +205,46 @@ impl ArtifactDir { /// /// [`ErrorCode::ArtifactMissing`]: crate::ErrorCode::ArtifactMissing pub fn require_initramfs(&self, guest: Guest) -> Result { - required( + if self.architecture == Architecture::Aarch64 && guest == Guest::AzureLinux { + return Err(Error::unsupported( + "ARM64 cold boot supports Alpine or Ubuntu, not Azure Linux", + )); + } + let path = required( self.initramfs(guest), &format!("{} initramfs", guest.distribution()), - ) + )?; + if self.architecture == Architecture::Aarch64 { + let manifest_path = self.package_manifest(guest); + let text = read_metadata( + &manifest_path, + "ARM64 guest package manifest", + "build the matching ARM64 initramfs with nvx.py", + )?; + let manifest: serde_json::Value = serde_json::from_str(&text).map_err(|error| { + Error::unavailable(format!( + "invalid ARM64 package manifest {}", + manifest_path.display() + )) + .with_source(error) + })?; + let package_architecture = match guest { + Guest::Ubuntu => "arm64", + _ => "aarch64", + }; + if manifest + .get("architecture") + .and_then(serde_json::Value::as_str) + != Some(package_architecture) + { + return Err(Error::unavailable(format!( + "guest package manifest {} does not declare {} architecture {package_architecture}", + manifest_path.display(), + guest.distribution() + ))); + } + } + Ok(path) } /// Returns the artifacts of a sandbox, as `nvx-rs sandbox run` and `nvx-rs sandbox start` @@ -185,6 +257,11 @@ impl ArtifactDir { /// /// [`ErrorCode::ArtifactMissing`]: crate::ErrorCode::ArtifactMissing pub fn sandbox_artifacts(&self) -> Result { + if self.architecture == Architecture::Aarch64 { + return Err(Error::unsupported( + "ARM64 managed sandboxes are not qualified by the Phase 1 cold-boot contract", + )); + } self.require_openvmm()?; self.require_kernel()?; required(self.initramfs(Guest::Alpine), "initramfs")?; @@ -200,6 +277,9 @@ impl ArtifactDir { kernel: self.kernel(), guests: Guest::ALL .iter() + .filter(|&&guest| { + self.architecture == Architecture::X86_64 || guest != Guest::AzureLinux + }) .map(|&guest| GuestPaths { name: guest.name(), distribution: guest.distribution(), @@ -239,12 +319,12 @@ impl ArtifactDir { Layout::Release => self.listed_identities()?, Layout::Repository => [ provenance_identity( - &self.artifact(OPENVMM_PROVENANCE_NAME), + &self.root.join(REPO_BUILD_DIR).join(OPENVMM_PROVENANCE_NAME), "OpenVMM", "executable_sha256", )?, provenance_identity( - &self.artifact(KERNEL_PROVENANCE_NAME), + &self.artifact(self.architecture.kernel_provenance_name()), "kernel", "kernel_sha256", )?, @@ -294,7 +374,10 @@ impl ArtifactDir { }; Ok([ listed(format!("{RELEASE_BIN_DIR}/{}", openvmm_executable()))?, - listed(format!("{RELEASE_GUEST_DIR}/{KERNEL_NAME}"))?, + listed(format!( + "{RELEASE_GUEST_DIR}/{}", + self.architecture.kernel_name() + ))?, listed(format!( "{RELEASE_GUEST_DIR}/{}", Guest::Alpine.initramfs_name() diff --git a/nvx-rs/src/host/artifacts/identity.rs b/nvx-rs/src/host/artifacts/identity.rs index 393c16d06..43a1d1e9a 100644 --- a/nvx-rs/src/host/artifacts/identity.rs +++ b/nvx-rs/src/host/artifacts/identity.rs @@ -108,9 +108,86 @@ pub(super) fn provenance_identity( "does not have format {PROVENANCE_FORMAT}" ))); } + provenance .get(field) .and_then(Value::as_str) .and_then(sha256_identity) .ok_or_else(|| invalid(&format!("has no SHA-256 digest in {field}"))) } + +/// Rejects a Windows OpenVMM executable from another ISA before any process or WHP call. +#[cfg(all(windows, feature = "cli"))] +pub(crate) fn check_arm64_pe(path: &Path) -> Result<()> { + use std::io::{Seek, SeekFrom}; + + let invalid = + |problem: &str| Error::unavailable(format!("ARM64 OpenVMM {}: {problem}", path.display())); + let mut file = fs::File::open(path) + .map_err(|error| invalid("cannot open executable").with_source(error))?; + let length = file + .metadata() + .map_err(|error| invalid("cannot read executable length").with_source(error))? + .len(); + let mut dos = [0; 64]; + file.read_exact(&mut dos) + .map_err(|error| invalid("truncated PE DOS header").with_source(error))?; + if &dos[..2] != b"MZ" { + return Err(invalid("not a Windows PE executable")); + } + let offset = u64::from(u32::from_le_bytes( + dos[60..64].try_into().expect("fixed DOS header"), + )); + if offset < 64 || offset.checked_add(6).is_none_or(|end| end > length) { + return Err(invalid("PE header offset is outside the executable")); + } + file.seek(SeekFrom::Start(offset)) + .map_err(|error| invalid("cannot seek PE header").with_source(error))?; + let mut pe = [0; 6]; + file.read_exact(&mut pe) + .map_err(|error| invalid("truncated PE header").with_source(error))?; + if &pe[..4] != b"PE\0\0" || u16::from_le_bytes([pe[4], pe[5]]) != 0xaa64 { + return Err(invalid( + "executable is not native ARM64 (PE machine 0xaa64)", + )); + } + Ok(()) +} + +/// Applies the same bounded ARM64 direct-Image header rules as the existing Python producer. +/// A filename or an ELF from another ISA is not sufficient admission. +pub(super) fn check_arm64_image(path: &Path) -> Result<()> { + let invalid = + |problem: &str| Error::unavailable(format!("ARM64 kernel {}: {problem}", path.display())); + let mut file = + fs::File::open(path).map_err(|error| invalid("cannot open Image").with_source(error))?; + let length = file + .metadata() + .map_err(|error| invalid("cannot read Image length").with_source(error))? + .len(); + let mut header = [0; 64]; + file.read_exact(&mut header) + .map_err(|error| invalid("truncated Image header").with_source(error))?; + if header[56..60] != *b"ARM\x64" { + return Err(invalid("not an uncompressed ARM64 Linux Image")); + } + let double = |offset| { + u64::from_le_bytes( + header[offset..offset + 8] + .try_into() + .expect("fixed Image header"), + ) + }; + let offset = double(8); + let size = double(16); + let flags = double(24); + if flags & 1 != 0 || (flags >> 1) & 3 != 1 || flags & !0xf != 0 || flags & 8 == 0 { + return Err(invalid( + "Image requires little-endian 4-KiB pages and reservation-safe placement", + )); + } + if size < length || offset % 4096 != 0 || offset.checked_add(size).is_none() { + return Err(invalid("invalid Image footprint or text offset")); + } + Ok(()) +} diff --git a/nvx-rs/src/host/artifacts/mod.rs b/nvx-rs/src/host/artifacts/mod.rs index 3deabc5d0..bd8f85286 100644 --- a/nvx-rs/src/host/artifacts/mod.rs +++ b/nvx-rs/src/host/artifacts/mod.rs @@ -25,6 +25,7 @@ //! `scripts/nvx_tools/common.py`, so that `nvx-rs` and the `nvx.py` commands that build, //! install, benchmark, and test the guests agree on where each artifact is. +mod architecture; mod dir; mod guest; mod identity; @@ -42,8 +43,11 @@ use crate::error::{Error, ErrorCode, Result}; use crate::host::platform; use crate::host::store::RuntimeIdentity; +pub use self::architecture::Architecture; pub use self::dir::{ArtifactDir, ArtifactPaths, GuestPaths, Layout}; pub use self::guest::Guest; +#[cfg(all(windows, feature = "cli"))] +pub(crate) use self::identity::check_arm64_pe; #[cfg(feature = "cli")] pub(crate) use self::require::require_artifact; pub(crate) use self::require::require_existing; diff --git a/nvx-rs/src/host/cli/download.rs b/nvx-rs/src/host/cli/download.rs index 07eb1d47d..ea08e536a 100644 --- a/nvx-rs/src/host/cli/download.rs +++ b/nvx-rs/src/host/cli/download.rs @@ -52,7 +52,9 @@ use super::artifacts::{Located, locate}; use super::sandbox::HYPERVISORS; use super::{Exit, Result, check_interrupt, io_error, output}; use crate::ErrorCode; -use crate::host::artifacts::{ArtifactDir, Guest, Layout, MANIFEST_NAME, REPO_BUILD_DIR}; +use crate::host::artifacts::{ + Architecture, ArtifactDir, Guest, Layout, MANIFEST_NAME, REPO_BUILD_DIR, +}; /// The usage and help of the parser. pub(super) const SYNTAX: Syntax = Syntax { @@ -188,8 +190,9 @@ impl Report { } } -/// Returns the release platform of `hypervisor` on this host, as `_release_platform` of `nvx.py` -/// did: Windows takes `whp`, and Linux `kvm` and `mshv`, and `auto` selects the first of them. +/// Selects the legacy x86 release's OS/backend, independently of the downloader's CPU ISA. +/// ARM artifact targets are rejected by `action` before release selection; downloading x86 +/// artifacts as data does not require the host to be able to execute them. fn platform(hypervisor: &str) -> Result<&'static str> { let (host, platforms): (&str, &[(&str, &'static str)]) = if cfg!(windows) { ("windows", &[("whp", "windows-whp")]) @@ -710,6 +713,12 @@ fn action( if !is_checkout(dir) || !enabled()? { return Ok(Action::Proceed); } + if dir.architecture() == Architecture::Aarch64 { + return Err(Exit::error( + ErrorCode::Unsupported, + "ARM64 release downloads are not qualified; build the matching local ARM64 Image and initramfs instead of downloading x86 artifacts", + )); + } for path in install::destinations(dir) { if install::exists(&path).map_err(|error| io_error(ErrorCode::Io, &error, &path))? { return Ok(Action::Warn(missing.clone(), path)); diff --git a/nvx-rs/src/host/cli/download/tests.rs b/nvx-rs/src/host/cli/download/tests.rs index fd64e22c3..656d2f993 100644 --- a/nvx-rs/src/host/cli/download/tests.rs +++ b/nvx-rs/src/host/cli/download/tests.rs @@ -184,7 +184,7 @@ fn repositories_are_owner_and_name() { } #[test] -fn platforms_are_those_of_the_host() { +fn platforms_are_selected_by_os_and_backend_not_downloader_isa() { if cfg!(windows) { assert_eq!(platform("auto").unwrap(), "windows-whp"); assert_eq!(platform("whp").unwrap(), "windows-whp"); @@ -543,6 +543,29 @@ fn present_artifacts_need_no_download_and_no_variable() { assert_eq!(action(&dir, &[], unasked).unwrap(), Action::Proceed); } +#[test] +fn local_arm_artifacts_proceed_but_missing_ones_never_download_x86() { + let (_directory, dir) = checkout(true); + let dir = dir.with_architecture(Architecture::Aarch64).unwrap(); + let exit = action(&dir, &needed(&dir), || Ok(true)).unwrap_err(); + assert_eq!(exit.code(), Some(ErrorCode::Unsupported)); + assert!(exit.message().unwrap().contains("ARM64 release downloads")); + assert_eq!( + action(&dir, &needed(&dir), || Ok(false)).unwrap(), + Action::Proceed + ); + for path in needed(&dir) { + write(&path); + } + assert_eq!( + action(&dir, &needed(&dir), || panic!( + "local artifacts need no download setting" + )) + .unwrap(), + Action::Proceed + ); +} + #[test] fn only_git_checkouts_of_nvx_download_and_only_when_allowed() { let (_directory, dir) = checkout(false); diff --git a/nvx-rs/src/host/cli/run.rs b/nvx-rs/src/host/cli/run.rs index a7e8bc29c..0c01e6bdb 100644 --- a/nvx-rs/src/host/cli/run.rs +++ b/nvx-rs/src/host/cli/run.rs @@ -56,7 +56,7 @@ use super::sandbox::{ use super::shell::format_command; use super::{Exit, Result, check_interrupt, output}; use crate::ErrorCode; -use crate::host::artifacts::{Guest, require_artifact}; +use crate::host::artifacts::{Architecture, Guest, require_artifact}; use crate::host::sandbox::mount::{ AGGREGATE_MOUNT_TARGET, MountKind, MountOwner, POLICY_KINDS, PolicyKind, aggregate_child_argument, aggregate_child_name, aggregate_command_line_fragment, @@ -74,6 +74,9 @@ pub(super) const SYNTAX: Syntax = Syntax { help: "\ options: -h, --help show this help message and exit + --architecture {x86_64,aarch64} + guest artifact ISA (default: native host ISA); + aarch64 uses build/aarch64 and requires local artifacts --guest {alpine,ubuntu,azurelinux} --hypervisor {auto,whp,kvm,mshv} --machine {microvm} @@ -149,6 +152,10 @@ const MACHINES: [&str; 1] = ["microvm"]; /// The choices of `--processors` and `--restore-processors`, the processor counts that a microVM /// takes. const PROCESSORS: [&str; 4] = ["1", "2", "4", "8"]; +/// Approved ARM microVM GPA width; this is address space, not a 64-GiB RAM allowance. +const ARM64_MICROVM_GPA_BITS: u32 = 36; +/// Logical RAM ceiling after the fixed 3-4 GiB MMIO aperture, before other reservations. +const ARM64_MICROVM_MAX_RAM_MIB: u64 = (1_u64 << (ARM64_MICROVM_GPA_BITS - 20)) - 1024; /// The stable ID of the attachment of a microVM's boot console, `hvc1`, in a snapshot's machine /// contract. @@ -159,7 +166,8 @@ const BOOT_CONSOLE: &str = "console:microvm-virtio0"; const INHERITED_ATTACHMENT: &str = "require-inherited-attachment"; /// The options of `nvx-rs run`, which parse with the defaults of the former `nvx.py run`. -pub(super) const OPTIONS: [Spec; 30] = [ +pub(super) const OPTIONS: [Spec; 31] = [ + Spec::one("--architecture"), Spec::one("--guest"), Spec::one("--hypervisor"), Spec::one("--machine"), @@ -195,6 +203,8 @@ pub(super) const OPTIONS: [Spec; 30] = [ /// The parsed options of `nvx-rs run`: the namespace of the former `nvx.py run`. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct Options { + /// Explicit guest artifact ISA, retaining the existing x86-64 default. + pub(crate) architecture: Architecture, /// `--guest`, Alpine by default. pub(crate) guest: Guest, /// `--hypervisor`, `auto` by default. @@ -238,6 +248,7 @@ pub(crate) struct Options { impl Default for Options { fn default() -> Self { Self { + architecture: Architecture::native(), guest: Guest::Alpine, hypervisor: "auto", machine: "microvm", @@ -276,6 +287,12 @@ impl Receiver for Options { fn take(&mut self, name: &'static str, value: Option<&str>) -> std::result::Result<(), String> { let text = || value.unwrap_or_default().to_owned(); match name { + "--architecture" => { + self.architecture = match choice(&text(), &["x86_64", "aarch64"])? { + "aarch64" => Architecture::Aarch64, + _ => Architecture::X86_64, + }; + } "--guest" => { let guest = choice(&text(), &GUESTS)?; self.guest = Guest::ALL @@ -335,6 +352,18 @@ fn check(options: &Options) -> Result { let refused = |message: String| Err(Exit::error(ErrorCode::Unsupported, message)); let network = &options.network; let restore = options.restore_snapshot.is_some(); + if options.architecture == Architecture::Aarch64 { + if restore || options.memory_capacity_mib.is_some() || options.guest == Guest::AzureLinux { + return refused( + "ARM64 Phase 1 supports Alpine/Ubuntu cold boot, not snapshot restore, memory activation, or Azure Linux".into(), + ); + } + if memory_mib(options) > ARM64_MICROVM_MAX_RAM_MIB { + return refused(format!( + "ARM64 microVM RAM exceeds {ARM64_MICROVM_MAX_RAM_MIB} MiB (63 GiB): the approved 36-bit GPA space reserves the fixed 3-4 GiB aperture; other reservations are separate" + )); + } + } if restore && network.net.is_some() { return refused( "--net is not accepted for restore; the snapshot already fixes the network addressing" @@ -415,7 +444,8 @@ pub(super) fn run(root: Option<&str>, options: Options) -> Result { // its host path holds, which a download may install. let refused = terminal_console.as_ref().is_some_and(Result::is_err) || extend_shares_and_network(&mut Vec::new(), &options, &rules).is_err(); - let located = locate(root)?; + let mut located = locate(root)?; + located.dir = located.dir.with_architecture(options.architecture)?; // A restore boots no kernel or initramfs: the snapshot holds the guest. let guest = options.restore_snapshot.is_none().then_some(options.guest); let hold = if refused { @@ -425,6 +455,10 @@ pub(super) fn run(root: Option<&str>, options: Options) -> Result { }; let Located { dir, openvmm } = located; require_artifact(&openvmm, "OpenVMM release binary")?; + #[cfg(windows)] + if options.architecture == Architecture::Aarch64 { + crate::host::artifacts::check_arm64_pe(&openvmm)?; + } let mut command = vec![ path_text(&path_string(&openvmm)?), "--single-process".to_owned(), diff --git a/nvx-rs/src/host/cli/run/tests.rs b/nvx-rs/src/host/cli/run/tests.rs index 2aa8f99b5..2a5e258c8 100644 --- a/nvx-rs/src/host/cli/run/tests.rs +++ b/nvx-rs/src/host/cli/run/tests.rs @@ -7,6 +7,71 @@ use super::*; use crate::host::sandbox::mount::MountAccess; use crate::protocol::cmdline::{self, parameters, virtio_mmio_devices}; +#[test] +fn runtime_architecture_defaults_to_native_and_preserves_explicit_selection() { + let (native, _) = parse(&[]).unwrap(); + assert_eq!(native.architecture, Architecture::native()); + let (legacy, _) = parse(&["--architecture".into(), "x86_64".into()]).unwrap(); + assert_eq!(legacy.architecture, Architecture::X86_64); + let (arm, remaining) = parse(&["--architecture".into(), "aarch64".into()]).unwrap(); + assert!(remaining.is_empty()); + assert_eq!(arm.architecture, Architecture::Aarch64); + assert!(parse(&["--architecture".into(), "unknown".into()]).is_err()); +} + +#[test] +fn arm_auto_and_host_profile_selection_is_forwarded_without_guest_constants() { + for profile in ["auto", "host"] { + let (options, remaining) = + parse(&["--architecture", "aarch64", "--cpu-profile", profile].map(str::to_owned)) + .unwrap(); + assert!(remaining.is_empty()); + assert_eq!(options.architecture, Architecture::Aarch64); + assert_eq!(options.cpu_profile.as_deref(), Some(profile)); + assert!(check(&options).is_ok()); + } +} + +#[test] +fn approved_arm_36_bit_gpa_space_is_not_a_64_gib_ram_allowance() { + let mut options = Options { + architecture: Architecture::Aarch64, + memory_mib: Some(63 * 1024), + ..Options::default() + }; + assert!(check(&options).is_ok()); + for memory in [63 * 1024 + 1, 64 * 1024] { + options.memory_mib = Some(memory); + assert!(check(&options).is_err()); + } + options.architecture = Architecture::X86_64; + assert!(check(&options).is_ok()); +} + +#[test] +fn arm_out_of_scope_modes_are_refused_before_artifact_discovery() { + for options in [ + Options { + architecture: Architecture::Aarch64, + restore_snapshot: Some("not-opened".into()), + ..Options::default() + }, + Options { + architecture: Architecture::Aarch64, + memory_capacity_mib: Some(256), + ..Options::default() + }, + Options { + architecture: Architecture::Aarch64, + guest: Guest::AzureLinux, + ..Options::default() + }, + ] { + let failure = run(None, options).unwrap_err(); + assert!(format!("{failure:?}").contains("ARM64 Phase 1")); + } +} + /// Returns the kernel command line that OpenVMM composes from `cmdline`, the `--cmdline` that /// `run` gives it for the cold boot of an aggregate of read-write shares, as its /// `build_microvm_command_line` and `append_microvm_virtio_discovery` do: its console parameters, diff --git a/nvx-rs/tests/cli.rs b/nvx-rs/tests/cli.rs index f3b64de5b..e6d67002d 100644 --- a/nvx-rs/tests/cli.rs +++ b/nvx-rs/tests/cli.rs @@ -100,6 +100,60 @@ fn run(root: &Root, arguments: &[String]) -> Output { nvx_rs(root, arguments, &BTreeMap::new()) } +#[cfg(target_arch = "aarch64")] +#[test] +fn native_arm_auto_and_host_runs_find_arm_artifacts_without_an_isa_option() { + let root = Root::new(); + let build = root.path.join("build").join("aarch64"); + fs::create_dir_all(&build).unwrap(); + let mut image = [0_u8; 64]; + image[16..24].copy_from_slice(&4096_u64.to_le_bytes()); + image[24..32].copy_from_slice(&10_u64.to_le_bytes()); + image[56..60].copy_from_slice(b"ARM\x64"); + fs::write(build.join("Image"), image).unwrap(); + for (guest, architecture) in [("alpine", "aarch64"), ("ubuntu", "arm64")] { + let name = if guest == "alpine" { + "initramfs" + } else { + "initramfs-ubuntu" + }; + fs::write(build.join(format!("{name}.cpio.gz")), b"dry-run fixture").unwrap(); + fs::write( + build.join(format!("{name}.cpio.gz.packages.json")), + format!(r#"{{"architecture":"{architecture}"}}"#), + ) + .unwrap(); + for profile in ["auto", "host"] { + // Deliberately bypass the x86-fixture helper: this checks the real native default. + let output = Command::new(NVX_RS) + .arg("--root") + .arg(&root.path) + .args([ + "run", + "--dry-run", + "--guest", + guest, + "--cpu-profile", + profile, + ]) + .env_remove("NVX_OPENVMM") + .env("NVX_AUTO_DOWNLOAD", "0") + .output() + .unwrap(); + assert!(output.status.success(), "{}", text(&output.stderr)); + let printed = text(&output.stdout); + assert!( + printed.contains(build.join("Image").to_str().unwrap()), + "{printed}" + ); + assert!( + printed.contains(&format!("--cpu-profile {profile}")), + "{printed}" + ); + } + } +} + fn lifecycle(operation: &str, root: &Root, state: &str) -> Vec { vec![ "sandbox".into(), @@ -2458,8 +2512,11 @@ fn usage_errors_print_the_one_line_usage_of_their_parser() { /// The host CPU as the host OS reports it, independently of the CPUID that `nvx-rs` reads: its /// vendor and display family, model, and stepping, from the first processor of `/proc/cpuinfo` on /// Linux, and from `PROCESSOR_IDENTIFIER`, such as `Intel64 Family 6 Model 85 Stepping 4, -/// GenuineIntel`, on Windows. -fn reported_cpu() -> (String, u32, u32, u32) { +/// GenuineIntel`, on Windows. Non-x86 hosts have no CPUID tuple. +fn reported_cpu() -> Option<(String, u32, u32, u32)> { + if !cfg!(target_arch = "x86_64") { + return None; + } if cfg!(windows) { let identifier = std::env::var("PROCESSOR_IDENTIFIER").unwrap(); let (description, vendor) = identifier.split_once(", ").unwrap(); @@ -2468,12 +2525,12 @@ fn reported_cpu() -> (String, u32, u32, u32) { let position = words.iter().position(|word| *word == name).unwrap(); words[position + 1].parse().unwrap() }; - return ( + return Some(( vendor.trim().to_owned(), number("Family"), number("Model"), number("Stepping"), - ); + )); } let cpuinfo = fs::read_to_string("/proc/cpuinfo").unwrap(); let field = |name: &str| -> String { @@ -2485,12 +2542,12 @@ fn reported_cpu() -> (String, u32, u32, u32) { }) .unwrap() }; - ( + Some(( field("vendor_id"), field("cpu family").parse().unwrap(), field("model").parse().unwrap(), field("stepping").parse().unwrap(), - ) + )) } /// Returns whether a built-in CPU profile serves the CPU of `vendor` with the display `family`, @@ -2519,9 +2576,15 @@ fn run_guides_failed_cold_boots_on_cpus_that_no_built_in_profile_serves() { // nvx-rs run identifies the host's CPU with CPUID, as OpenVMM does and as the host OS reports // it, and guides a failed cold boot only where no built-in profile serves the CPU: what // follows OpenVMM's output depends on the host that runs the test. - let (vendor, family, model, stepping) = reported_cpu(); - let unserved = !served(&vendor, family, model, stepping); - let host_profiles = ["GenuineIntel", "AuthenticAMD"].contains(&vendor.as_str()); + let cpu = reported_cpu(); + let unserved = cpu + .as_ref() + .is_some_and(|(vendor, family, model, stepping)| { + !served(vendor, *family, *model, *stepping) + }); + let host_profiles = cpu + .as_ref() + .is_some_and(|(vendor, ..)| ["GenuineIntel", "AuthenticAMD"].contains(&vendor.as_str())); let root = Root::new(); let arguments = |arguments: &[&str]| -> Vec { std::iter::once("run") @@ -2606,6 +2669,7 @@ fn run_guides_failed_cold_boots_on_cpus_that_no_built_in_profile_serves() { assert_eq!(guidance, "", "{options:?}"); continue; } + let (vendor, family, model, stepping) = cpu.as_ref().unwrap(); assert!( guidance.starts_with(&format!( "nvx: no built-in CPU profile serves this host's CPU, {vendor} \ diff --git a/nvx-rs/tests/cli/download.rs b/nvx-rs/tests/cli/download.rs index be959f532..ebdeab97e 100644 --- a/nvx-rs/tests/cli/download.rs +++ b/nvx-rs/tests/cli/download.rs @@ -77,6 +77,32 @@ fn run_downloads_the_release_of_a_checkout_without_artifacts() { } } +#[test] +fn an_explicit_x86_dry_run_reaches_download_history_on_any_host_isa() { + let root = checkout(&INSTALLED); + let outcome = root.nvx_rs(&["run", "--architecture", "x86_64", "--dry-run"]); + assert_eq!(outcome.status, Some(1), "{}", outcome.describe()); + assert!(outcome.stdout.is_empty(), "{}", outcome.describe()); + let stderr = outcome.stderr(); + let lines: Vec<_> = stderr.lines().collect(); + assert_eq!(lines.len(), 2, "{}", outcome.describe()); + assert_eq!( + lines[0], + format!( + ">> {} holds no NVX artifacts, so nvx-rs downloads its release; set \ + NVX_AUTO_DOWNLOAD=0 to skip that", + root.text() + ), + ); + assert!( + lines[1] + .starts_with("error[unavailable]: download needs a Git checkout to find its release: "), + "{}", + outcome.describe() + ); + assert_no_staging(&root); +} + #[test] fn run_downloads_nothing_over_some_artifacts() { let root = checkout(&["build/vmlinux"]); diff --git a/nvx-rs/tests/cli/harness.rs b/nvx-rs/tests/cli/harness.rs index 1470a1cf5..063b6503e 100644 --- a/nvx-rs/tests/cli/harness.rs +++ b/nvx-rs/tests/cli/harness.rs @@ -337,10 +337,19 @@ impl Root { /// no standard input. pub(crate) fn command>(&self, arguments: &[S]) -> Command { let mut command = Command::new(NVX_RS); + command.arg("--root").arg(&self.path); + let arguments: Vec<&OsStr> = arguments.iter().map(AsRef::as_ref).collect(); + if arguments.first().is_some_and(|argument| *argument == "run") + && !arguments.contains(&OsStr::new("--architecture")) + { + // These fixtures contain x86 artifacts even on a native ARM test host. + command + .args(["run", "--architecture", "x86_64"]) + .args(&arguments[1..]); + } else { + command.args(arguments); + } command - .arg("--root") - .arg(&self.path) - .args(arguments) .current_dir(&self.path) .env_remove("NVX_OPENVMM") .env_remove("NVX_AUTO_DOWNLOAD") diff --git a/nvx-rs/tests/cli/one_shot.rs b/nvx-rs/tests/cli/one_shot.rs index cef3c22a5..c209e45c7 100644 --- a/nvx-rs/tests/cli/one_shot.rs +++ b/nvx-rs/tests/cli/one_shot.rs @@ -662,8 +662,9 @@ fn one_shot_runs_exit_with_the_workload_or_openvmm_status() { .into_iter() .chain(run_launches()) .collect(); - let (vendor, family, model, stepping) = crate::reported_cpu(); - let unserved = !crate::served(&vendor, family, model, stepping); + let unserved = crate::reported_cpu().is_some_and(|(vendor, family, model, stepping)| { + !crate::served(&vendor, family, model, stepping) + }); in_parallel(&cases, |case| { let root = Root::new(); for (relative, content) in &case.files { diff --git a/nvx-rs/tests/guest_tools.rs b/nvx-rs/tests/guest_tools.rs index 28fefafcb..0e3d277b4 100644 --- a/nvx-rs/tests/guest_tools.rs +++ b/nvx-rs/tests/guest_tools.rs @@ -141,6 +141,7 @@ fn is_root() -> bool { /// Returns the `errno` with which this process fails to open the device at `path`, as an /// unprivileged guest binary does, or `None` when the tests run as root or the device opens: the /// binaries would then reach the host's real devices. +#[cfg(target_arch = "x86_64")] fn device_error(path: &str, read: bool, write: bool) -> Option { if is_root() { return None; @@ -167,6 +168,41 @@ fn console_pending_usage() { } } +#[test] +#[cfg(target_arch = "aarch64")] +fn arm_coldboot_time_has_a_real_cli_entrypoint() { + let usage = b"usage: nvx-time [--report-only] boot|status|sample|generation-id\n"; + for arguments in [ + &[][..], + &["unknown"], + &["sample", "extra"], + &["boot", "extra"], + ] { + assert_eq!( + run("nvx-time", arguments), + (Some(2), Vec::new(), usage.to_vec()), + "{arguments:?}" + ); + } +} + +#[test] +#[cfg(target_arch = "aarch64")] +fn arm_coldboot_time_does_not_expose_snapshot_or_restore_commands() { + for command in [ + "capture", + "restore-finish", + "pre-capture", + "cancel-capture", + "check", + ] { + let (status, stdout, stderr) = run("nvx-time", [command]); + assert_eq!(status, Some(2), "{command}"); + assert!(stdout.is_empty()); + assert!(stderr.starts_with(b"usage: nvx-time ")); + } +} + /// Runs the guest binary `name` with `arguments` and its stdout on `stdout`, and returns how it /// ended and its stderr. fn run_to( diff --git a/scripts/nvx.py b/scripts/nvx.py index 5095a008f..1cd8334b2 100644 --- a/scripts/nvx.py +++ b/scripts/nvx.py @@ -44,6 +44,7 @@ ) from nvx_tools.build_constants import ( BuildConstants, + GuestRustBuildConstants, InitramfsBuildConstants, KernelBuildConstants, UbuntuBuildConstants, @@ -91,8 +92,9 @@ test_guest_binaries, ) from nvx_tools.guests import GUEST_NAMES, guest_descriptor -from nvx_tools.kernel_config import configuration_problems +from nvx_tools.kernel_config import arm_configuration_problems, configuration_problems from nvx_tools.kernel_config import summary as kernel_config_summary +from nvx_tools.kernel_features import features_for_architecture from nvx_tools.merge_queue import configure_parser as configure_merge_queue_parser from nvx_tools.microvm_tests import configure_parser as configure_microvm_test_parser from nvx_tools.microvm_tests import ( @@ -139,10 +141,13 @@ def _openvmm_build_config(args: argparse.Namespace) -> OpenVmmBuildConfig: def _build_config(args: argparse.Namespace) -> BuildConfig: + architecture = getattr(args, "architecture", "x86_64") return BuildConfig( guest=getattr(args, "guest", InitramfsBuildConstants.DEFAULT_GUEST), native_guest=getattr(args, "native", False), debug_kernel=getattr(args, "debug_kernel", False), + docker=DockerBuildConfig.for_architecture(architecture), + kernel=KernelBuildConfig.for_architecture(architecture), openvmm=_openvmm_build_config(args), ) @@ -152,17 +157,28 @@ def command_build_guest(args: argparse.Namespace) -> None: def command_check_guest_binaries(args: argparse.Namespace) -> None: - check_guest_binaries(args.toolchain) + if args.architecture == "x86_64": + check_guest_binaries(args.toolchain) + else: + check_guest_binaries(args.toolchain, architecture=args.architecture) def command_build_guest_binaries(args: argparse.Namespace) -> None: - result = build_guest_binaries(args.output) + result = ( + build_guest_binaries(args.output) + if args.architecture == "x86_64" + else build_guest_binaries(args.output, architecture=args.architecture) + ) + report: dict[str, object] = { + "source_sha256": result.source_sha256, + "binaries": result.binaries, + } + if result.architecture != "x86_64": + report["architecture"] = result.architecture + report["target"] = GuestRustBuildConstants.TARGETS[result.architecture] print( json.dumps( - { - "source_sha256": result.source_sha256, - "binaries": result.binaries, - }, + report, indent=2, sort_keys=True, ) @@ -170,10 +186,21 @@ def command_build_guest_binaries(args: argparse.Namespace) -> None: def command_test_guest_binaries(args: argparse.Namespace) -> None: - test_guest_binaries(args.binaries) + if args.architecture == "x86_64": + test_guest_binaries(args.binaries) + else: + test_guest_binaries(args.binaries, architecture=args.architecture) def command_build_kernel(args: argparse.Namespace) -> None: + architecture = getattr(args, "architecture", "x86_64") + if architecture != "x86_64": + build_kernel( + KernelBuildConfig.for_architecture( + architecture, debug=getattr(args, "debug", False) + ) + ) + return build_kernel( KernelBuildConfig.debug_variant() if getattr(args, "debug", False) @@ -182,30 +209,55 @@ def command_build_kernel(args: argparse.Namespace) -> None: def command_kernel_config(args: argparse.Namespace) -> None: + if args.architecture == "aarch64": + if args.native: + generate_kernel_config( + KernelBuildConfig.for_architecture("aarch64"), check=args.check + ) + else: + generate_docker_kernel_config( + DockerBuildConfig.for_architecture("aarch64"), check=args.check + ) + return if args.native: generate_kernel_config(KernelBuildConfig(), check=args.check) else: generate_docker_kernel_config(DockerBuildConfig(), check=args.check) -def command_check_kernel_config(_: argparse.Namespace) -> None: +def command_check_kernel_config(args: argparse.Namespace | None) -> None: root = BuildConstants.REPO_ROOT - config_text = (root / KernelBuildConstants.INPUT_CONFIG).read_text(encoding="utf-8") - derived_text = (root / KernelBuildConstants.DERIVED_CONFIG).read_text( - encoding="utf-8" + architecture = getattr(args, "architecture", "x86_64") + kernel = KernelBuildConfig.for_architecture(architecture) + config_text = (root / kernel.input_config).read_text(encoding="utf-8") + derived_text = (root / kernel.derived_config).read_text(encoding="utf-8") + problems = ( + configuration_problems(config_text, derived_text) + if architecture == "x86_64" + else arm_configuration_problems( + config_text, derived_text, features_for_architecture(architecture) + ) ) - problems = configuration_problems(config_text, derived_text) if problems: raise ScriptError( "the kernel configuration does not match its feature inventory:\n " + "\n ".join(problems) ) - print( - f">> kernel configuration: {kernel_config_summary(config_text, derived_text)}" + summary = ( + kernel_config_summary(config_text, derived_text) + if architecture == "x86_64" + else kernel_config_summary( + config_text, derived_text, features_for_architecture(architecture) + ) ) + print(f">> kernel configuration: {summary}") def command_build_initramfs(args: argparse.Namespace) -> None: + architecture = getattr(args, "architecture", "x86_64") + if architecture != "x86_64": + build_initramfs(BuildConfig.initramfs_config(args.guest, architecture)) + return if not guest_descriptor(args.guest).native_build_supported: build_docker_initramfs(DockerBuildConfig(), args.guest) return @@ -392,6 +444,12 @@ def _add_guest_options( allow_all: bool, ) -> None: choices = (*GUEST_NAMES, "all") if allow_all else GUEST_NAMES + parser.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + help="guest artifact ISA (default: x86_64); does not select a CPU profile", + ) parser.add_argument( "--guest", choices=choices, @@ -448,12 +506,22 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace: help="Rust toolchain to use (default: the rust-toolchain.toml pin)", ) guest_binary_checks.set_defaults(handler=command_check_guest_binaries) + guest_binary_checks.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + ) guest_binaries = subparsers.add_parser( "build-guest-binaries", help="build the pinned Rust executables for Linux guests", ) guest_binaries.add_argument("--output", type=Path, required=True) + guest_binaries.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + ) guest_binaries.set_defaults(handler=command_build_guest_binaries) guest_binary_tests = subparsers.add_parser( @@ -461,6 +529,11 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace: help="test prebuilt Rust guest executables", ) guest_binary_tests.add_argument("--binaries", type=Path, required=True) + guest_binary_tests.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + ) guest_binary_tests.set_defaults(handler=command_test_guest_binaries) kernel = subparsers.add_parser( @@ -476,6 +549,11 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace: ), ) kernel.set_defaults(handler=command_build_kernel) + kernel.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default=os.environ.get("NVX_ARCHITECTURE", "x86_64"), + ) kernel_config = subparsers.add_parser( "kernel-config", @@ -498,6 +576,11 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace: help="fail if the checked-in files differ instead of writing them", ) kernel_config.set_defaults(handler=command_kernel_config) + kernel_config.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + ) check_kernel_config = subparsers.add_parser( "check-kernel-config", @@ -507,6 +590,11 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace: ), ) check_kernel_config.set_defaults(handler=command_check_kernel_config) + check_kernel_config.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + ) initramfs = subparsers.add_parser( "build-initramfs", @@ -525,6 +613,11 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace: ), ) initramfs.set_defaults(handler=command_build_initramfs) + initramfs.add_argument( + "--architecture", + choices=GuestRustBuildConstants.ARCHITECTURES, + default="x86_64", + ) distro_layer = subparsers.add_parser( "build-distro-layer", diff --git a/scripts/nvx_tools/benchmark.py b/scripts/nvx_tools/benchmark.py index 635506940..0e19d8cd2 100644 --- a/scripts/nvx_tools/benchmark.py +++ b/scripts/nvx_tools/benchmark.py @@ -2834,9 +2834,13 @@ def run_guest_script( time_abi_status: bool = False, host_state_on_timeout: Callable[[subprocess.Popen[bytes]], str] | None = None, on_stderr: Callable[[bytes], None] | None = None, + expected_exit_status: int = 0, ) -> GuestCommandResult: """Run ``script`` once the guest boots and wait for ``completion_marker``. + Guest teardown must return ``expected_exit_status`` (zero by default); + reserved time-violation statuses still fail even when requested. + With ``time_abi_status``, a cold boot first asks the guest for its time ABI status and sends ``script`` only after the query exits, so the guest's tty cannot echo script bytes into the status lines. The run @@ -2925,7 +2929,7 @@ def run_guest_script( monitor.finish() returncode = process.wait() monitor.check_exit(returncode) - if teardown_mode == "guest-exit" and returncode != 0: + if teardown_mode == "guest-exit" and returncode != expected_exit_status: raise monitor.exit_error(returncode) if not input_sent: if status_sent: diff --git a/scripts/nvx_tools/build.py b/scripts/nvx_tools/build.py index b7b97734f..ecb1c054a 100644 --- a/scripts/nvx_tools/build.py +++ b/scripts/nvx_tools/build.py @@ -50,13 +50,16 @@ run_checked, sha256_file, ) +from .elf import read_arm64_image from .guest_binaries import ( build_guest_binaries, + guest_binary_inventory, guest_binary_source_files, inspect_guest_binaries, ) from .guests import GuestDescriptor, guest_descriptor -from .kernel_config import check_build_configuration, generate +from .kernel_config import Assignment, check_build_configuration, generate, parse_config +from .kernel_features import features_for_architecture class ApkPackage(TypedDict): @@ -183,8 +186,28 @@ def _assert_watchdog_kernel_config(path: Path, *, debug: bool) -> None: ) -def assert_required_kernel_config(path: Path, *, debug: bool = False) -> None: +def assert_required_kernel_config( + path: Path, *, debug: bool = False, architecture: str = "x86_64" +) -> None: """Validate the generated configuration required by the NVX platform.""" + if architecture == "aarch64": + values = parse_config(path.read_text(encoding="utf-8"), str(path)) + missing: list[str] = [] + for feature in features_for_architecture(architecture): + if debug and feature.name == "production-watchdogs": + continue + for line in (*feature.settings, *feature.expects): + assignment = Assignment.parse(line) + if assignment is None or not assignment.holds(values): + missing.append(f"{feature.name}: {line}") + if missing: + raise ScriptError( + "ARM64 kernel configuration is incompatible: " + ", ".join(missing) + ) + _assert_watchdog_kernel_config(path, debug=debug) + return + if architecture != "x86_64": + raise ScriptError(f"unsupported kernel architecture: {architecture}") _assert_direct_boot_kernel_config(path) _assert_virtio_console_kernel_config(path) _assert_sandbox_kernel_config(path) @@ -203,10 +226,14 @@ def _require_linux(workflow: str) -> None: def _alpine_tarball(config: InitramfsBuildConfig) -> Path: - return config.work / AlpineBuildConstants.MINIROOTFS_NAME + return config.work / ( + AlpineBuildConstants.ARM_MINIROOTFS_NAME + if config.architecture == "aarch64" + else AlpineBuildConstants.MINIROOTFS_NAME + ) -def _kernel_patch_files() -> tuple[Path, ...]: +def _kernel_patch_files(architecture: str = "x86_64") -> tuple[Path, ...]: patches = tuple( sorted( (BuildConstants.REPO_ROOT / KernelBuildConstants.PATCH_DIRECTORY).glob( @@ -216,6 +243,19 @@ def _kernel_patch_files() -> tuple[Path, ...]: ) if not patches: raise ScriptError("no kernel patches were found") + if architecture == "aarch64": + arm = tuple( + sorted( + ( + BuildConstants.REPO_ROOT / KernelBuildConstants.ARM_PATCH_DIRECTORY + ).glob("*.patch") + ) + ) + if not arm: + raise ScriptError("no ARM64 kernel patches were found") + patches += arm + elif architecture != "x86_64": + raise ScriptError(f"unsupported kernel architecture: {architecture}") return patches @@ -243,7 +283,7 @@ def materialize_kernel_provenance_inputs() -> None: patch_paths = tuple( path for path in tree_paths - if path.startswith(f"{KernelBuildConstants.PATCH_DIRECTORY.as_posix()}/") + if Path(path).parent == KernelBuildConstants.PATCH_DIRECTORY and path.endswith(".patch") ) if config_path not in tree_paths: @@ -272,7 +312,7 @@ def materialize_kernel_provenance_inputs() -> None: print(f">> materialized {relative} from the run head") -def _kernel_source_fingerprint() -> str: +def _kernel_source_fingerprint(architecture: str = "x86_64") -> str: return json.dumps( { "version": KernelBuildConstants.VERSION, @@ -283,7 +323,11 @@ def _kernel_source_fingerprint() -> str: "path": patch.relative_to(BuildConstants.REPO_ROOT).as_posix(), "sha256": sha256_file(patch), } - for patch in _kernel_patch_files() + for patch in ( + _kernel_patch_files() + if architecture == "x86_64" + else _kernel_patch_files(architecture) + ) ], }, sort_keys=True, @@ -294,14 +338,26 @@ def _kernel_provenance_inputs( source_fingerprint: str, input_config_sha256: str, debug_config_fragment_sha256: str | None = None, + *, + architecture: str = "x86_64", ) -> dict[str, object]: inputs: dict[str, object] = { "source": json.loads(source_fingerprint), "input_config": { - "path": KernelBuildConstants.INPUT_CONFIG.as_posix(), + "path": ( + KernelBuildConstants.ARM_INPUT_CONFIG + if architecture == "aarch64" + else KernelBuildConstants.INPUT_CONFIG + ).as_posix(), "sha256": input_config_sha256, }, } + if architecture == "aarch64": + inputs.update( + architecture=architecture, + machine_binding="nvx-microvm-arm64-v1", + image_format="arm64-image", + ) if debug_config_fragment_sha256 is not None: inputs["debug_config_fragment"] = { "path": KernelBuildConstants.DEBUG_CONFIG_FRAGMENT.as_posix(), @@ -310,8 +366,15 @@ def _kernel_provenance_inputs( return inputs -def kernel_provenance_inputs() -> dict[str, object]: +def kernel_provenance_inputs(architecture: str = "x86_64") -> dict[str, object]: """Return the current source and input-config identity for a kernel build.""" + if architecture != "x86_64": + config = KernelBuildConfig.for_architecture(architecture) + return _kernel_provenance_inputs( + _kernel_source_fingerprint(architecture), + sha256_file(BuildConstants.REPO_ROOT / config.input_config), + architecture=architecture, + ) return _kernel_provenance_inputs( _kernel_source_fingerprint(), sha256_file(BuildConstants.REPO_ROOT / KernelBuildConstants.INPUT_CONFIG), @@ -381,13 +444,18 @@ def _initramfs_source_files() -> tuple[Path, ...]: ) -def initramfs_provenance_inputs() -> dict[str, object]: +def initramfs_provenance_inputs(architecture: str = "x86_64") -> dict[str, object]: """Return the current source identity for an initramfs build.""" - return { + guest_binary_inventory(architecture) + inputs: dict[str, object] = { "alpine": { "version": AlpineBuildConstants.VERSION, "branch": AlpineBuildConstants.BRANCH, - "minirootfs_sha256": AlpineBuildConstants.MINIROOTFS_SHA256, + "minirootfs_sha256": ( + AlpineBuildConstants.ARM_MINIROOTFS_SHA256 + if architecture == "aarch64" + else AlpineBuildConstants.MINIROOTFS_SHA256 + ), }, "source_files": [ { @@ -397,6 +465,10 @@ def initramfs_provenance_inputs() -> dict[str, object]: for path in _initramfs_source_files() ], } + if architecture == "aarch64": + inputs["architecture"] = architecture + inputs["machine_binding"] = "nvx-microvm-arm64-v1" + return inputs def record_openvmm_provenance(config: OpenVmmBuildConfig) -> None: @@ -516,6 +588,21 @@ def build_openvmm( def build_guest(config: BuildConfig) -> None: + architecture = ( + config.kernel.architecture + if config.native_guest + else config.docker.architecture + ) + if architecture == "aarch64": + if config.guest not in ("alpine", "ubuntu"): + raise ScriptError( + "ARM64 cold-boot builds require --guest alpine or ubuntu; " + "Azure Linux and the all-guests distro-layer build are not supported" + ) + if config.debug_kernel: + raise ScriptError( + "ARM64 cold-boot builds do not provide the x86 CI debug kernel" + ) if config.native_guest: unsupported = [ guest @@ -531,7 +618,11 @@ def build_guest(config: BuildConfig) -> None: if config.debug_kernel: build_kernel(KernelBuildConfig.debug_variant()) for guest in config.selected_guests(): - build_initramfs(config.initramfs_config(guest)) + build_initramfs( + config.initramfs_config(guest) + if architecture == "x86_64" + else config.initramfs_config(guest, architecture) + ) if config.guest == "all": build_distro_layer(config.distro_layer_config()) return @@ -554,10 +645,16 @@ def prepare_kernel_source(config: KernelBuildConfig) -> tuple[Path, str]: cache = config.cache_directory downloads = cache / BuildConstants.DOWNLOAD_DIRECTORY_NAME source_parent = cache / KernelBuildConstants.SOURCE_DIRECTORY_NAME + if config.architecture != "x86_64": + source_parent /= config.architecture tarball = downloads / KernelBuildConstants.UPSTREAM_ARCHIVE_NAME source = source_parent / KernelBuildConstants.SOURCE_NAME stamp = source_parent / KernelBuildConstants.SOURCE_STAMP_NAME - fingerprint = _kernel_source_fingerprint() + fingerprint = ( + _kernel_source_fingerprint() + if config.architecture == "x86_64" + else _kernel_source_fingerprint(config.architecture) + ) downloads.mkdir(parents=True, exist_ok=True) source_parent.mkdir(parents=True, exist_ok=True) @@ -572,7 +669,11 @@ def prepare_kernel_source(config: KernelBuildConfig) -> tuple[Path, str]: run_checked(["tar", "-xf", tarball, "-C", source_parent]) if not (source / "Makefile").is_file(): raise ScriptError(f"Linux archive did not produce {source}") - for patch in _kernel_patch_files(): + for patch in ( + _kernel_patch_files() + if config.architecture == "x86_64" + else _kernel_patch_files(config.architecture) + ): print(f">> applying {patch.name}") run_checked( ["patch", "--batch", "--forward", "-p1", "-i", patch], @@ -586,9 +687,17 @@ def _prepare_alpine_root(config: InitramfsBuildConfig) -> Path: config.work.mkdir(parents=True, exist_ok=True) tarball = _alpine_tarball(config) download_verified( - AlpineBuildConstants.MINIROOTFS_URL, + ( + AlpineBuildConstants.ARM_MINIROOTFS_URL + if config.architecture == "aarch64" + else AlpineBuildConstants.MINIROOTFS_URL + ), tarball, - AlpineBuildConstants.MINIROOTFS_SHA256, + ( + AlpineBuildConstants.ARM_MINIROOTFS_SHA256 + if config.architecture == "aarch64" + else AlpineBuildConstants.MINIROOTFS_SHA256 + ), ) root = config.work / InitramfsBuildConstants.ROOT_DIRECTORY_NAME shutil.rmtree(root, ignore_errors=True) @@ -596,7 +705,10 @@ def _prepare_alpine_root(config: InitramfsBuildConfig) -> Path: require_tool("tar") run_checked(["tar", "-xzf", tarball, "-C", root]) print(">> installing sandbox utilities into the Alpine rootfs") - _apk_add(root, *AlpineBuildConstants.PACKAGES) + if config.architecture == "x86_64": + _apk_add(root, *AlpineBuildConstants.PACKAGES) + else: + _apk_add(root, *AlpineBuildConstants.PACKAGES, architecture=config.architecture) resolver = root / "etc" / "resolv.conf" resolver.unlink(missing_ok=True) resolver.touch() @@ -612,8 +724,9 @@ def _install(source: Path, destination: Path) -> dict[str, str]: } -def _apk_add(root: Path, *packages: str) -> None: - loader = root / "lib" / "ld-musl-x86_64.so.1" +def _apk_add(root: Path, *packages: str, architecture: str = "x86_64") -> None: + guest_binary_inventory(architecture) + loader = root / "lib" / f"ld-musl-{architecture}.so.1" environment = os.environ.copy() environment["LD_LIBRARY_PATH"] = f"{root / 'lib'}:{root / 'usr' / 'lib'}" host_ca_file = ssl.get_default_verify_paths().cafile @@ -713,6 +826,8 @@ def _write_apk_manifest( root: Path, output: Path, helpers: dict[str, dict[str, str]], + *, + architecture: str = "x86_64", ) -> None: installed = root / "lib" / "apk" / "db" / "installed" packages: list[ApkPackage] = [] @@ -723,6 +838,11 @@ def _write_apk_manifest( fields[line[0]] = line[2:] if "P" not in fields: continue + if architecture == "aarch64" and fields.get("A") not in ("aarch64", "noarch"): + raise ScriptError( + f"ARM64 initramfs holds an incompatible package: " + f"{fields['P']} ({fields.get('A')})" + ) packages.append( { "name": fields["P"], @@ -746,7 +866,7 @@ def _write_apk_manifest( "format": AlpineBuildConstants.PACKAGE_MANIFEST_VERSION, "alpine_version": AlpineBuildConstants.VERSION, "alpine_branch": AlpineBuildConstants.BRANCH, - "architecture": AlpineBuildConstants.ARCHITECTURE, + "architecture": architecture, "packages": packages, "helpers": helpers, }, @@ -831,11 +951,23 @@ def _install_guest_files( prebuilt = os.environ.get(GuestRustBuildConstants.BINARIES_VARIABLE) if prebuilt: binary_directory = Path(prebuilt) - result = inspect_guest_binaries(binary_directory) + result = ( + inspect_guest_binaries(binary_directory) + if config.architecture == "x86_64" + else inspect_guest_binaries( + binary_directory, architecture=config.architecture + ) + ) else: binary_directory = config.work / GuestRustBuildConstants.OUTPUT_DIRECTORY_NAME - result = build_guest_binaries(binary_directory) - for name in GuestRustBuildConstants.BINARIES: + result = ( + build_guest_binaries(binary_directory) + if config.architecture == "x86_64" + else build_guest_binaries( + binary_directory, architecture=config.architecture + ) + ) + for name in guest_binary_inventory(config.architecture): source = binary_directory / name destination = root / "sbin" / name shutil.copyfile(source, destination) @@ -857,7 +989,11 @@ def _prepare_guest_root( if descriptor.name == "alpine": return _prepare_alpine_root(config) if descriptor.name == "ubuntu": - return ubuntu.prepare_root(config.work) + return ( + ubuntu.prepare_root(config.work) + if config.architecture == "x86_64" + else ubuntu.prepare_root(config.work, architecture=config.architecture) + ) raise AssertionError(f"missing rootfs preparer for {descriptor.name}") @@ -866,11 +1002,17 @@ def _write_ubuntu_manifest( output: Path, helpers: dict[str, dict[str, str]], input_sha256: str, + *, + architecture: str = "x86_64", ) -> None: manifest = output.with_name( f"{output.name}{BuildConstants.PACKAGE_MANIFEST_SUFFIX}" ) - document = ubuntu.package_manifest(root, helpers) + document = ( + ubuntu.package_manifest(root, helpers) + if architecture == "x86_64" + else ubuntu.package_manifest(root, helpers, architecture=architecture) + ) document.update( { "artifact": output.name, @@ -912,7 +1054,13 @@ def build_initramfs(config: InitramfsBuildConfig) -> None: f"{output.name}{BuildConstants.PACKAGE_MANIFEST_SUFFIX}" ) provenance_inputs = ( - initramfs_provenance_inputs() if descriptor.name == "alpine" else None + ( + initramfs_provenance_inputs() + if config.architecture == "x86_64" + else initramfs_provenance_inputs(config.architecture) + ) + if descriptor.name == "alpine" + else None ) provenance_path = output.with_name(InitramfsBuildConstants.PROVENANCE_NAME) if provenance_inputs is not None: @@ -920,7 +1068,14 @@ def build_initramfs(config: InitramfsBuildConfig) -> None: # Snapshot before the root is prepared so the manifest describes the sources # this build consumed; the post-pack comparison catches any edit in between. ubuntu_input_sha256 = ( - ubuntu.converter_input_sha256(ubuntu.customization_files()) + ( + ubuntu.converter_input_sha256(ubuntu.customization_files()) + if config.architecture == "x86_64" + else ubuntu.converter_input_sha256( + ubuntu.customization_files(config.architecture), + architecture=config.architecture, + ) + ) if descriptor.name == "ubuntu" else None ) @@ -929,20 +1084,41 @@ def build_initramfs(config: InitramfsBuildConfig) -> None: if descriptor.name == "ubuntu": ubuntu.apply_metadata_policy(root) else: - _write_apk_manifest(root, output, helpers) + if config.architecture == "x86_64": + _write_apk_manifest(root, output, helpers) + else: + _write_apk_manifest(root, output, helpers, architecture=config.architecture) _pack_initramfs(root, output) if ubuntu_input_sha256 is not None: - _write_ubuntu_manifest(root, output, helpers, ubuntu_input_sha256) + if config.architecture == "x86_64": + _write_ubuntu_manifest(root, output, helpers, ubuntu_input_sha256) + else: + _write_ubuntu_manifest( + root, + output, + helpers, + ubuntu_input_sha256, + architecture=config.architecture, + ) if ( ubuntu.converter_input_sha256(ubuntu.customization_files()) - != ubuntu_input_sha256 - ): + if config.architecture == "x86_64" + else ubuntu.converter_input_sha256( + ubuntu.customization_files(config.architecture), + architecture=config.architecture, + ) + ) != ubuntu_input_sha256: output.unlink(missing_ok=True) package_manifest.unlink(missing_ok=True) raise ScriptError("initramfs source inputs changed during the build") if ( provenance_inputs is not None - and initramfs_provenance_inputs() != provenance_inputs + and ( + initramfs_provenance_inputs() + if config.architecture == "x86_64" + else initramfs_provenance_inputs(config.architecture) + ) + != provenance_inputs ): output.unlink(missing_ok=True) package_manifest.unlink(missing_ok=True) @@ -1239,7 +1415,7 @@ def build_kernel(config: KernelBuildConfig) -> None: for tool in ("make",): require_tool(tool) source, source_fingerprint = prepare_kernel_source(config) - input_config = BuildConstants.REPO_ROOT / KernelBuildConstants.INPUT_CONFIG + input_config = BuildConstants.REPO_ROOT / config.input_config input_config_bytes = input_config.read_bytes() input_config_sha256 = hashlib.sha256(input_config_bytes).hexdigest() fragment = ( @@ -1253,10 +1429,17 @@ def build_kernel(config: KernelBuildConfig) -> None: if fragment_bytes is not None else None ) - provenance_inputs = _kernel_provenance_inputs( - source_fingerprint, - input_config_sha256, - fragment_sha256, + provenance_inputs = ( + _kernel_provenance_inputs( + source_fingerprint, input_config_sha256, fragment_sha256 + ) + if config.architecture == "x86_64" + else _kernel_provenance_inputs( + source_fingerprint, + input_config_sha256, + fragment_sha256, + architecture=config.architecture, + ) ) build_inputs: dict[str, str] = { "source": source_fingerprint, @@ -1287,29 +1470,60 @@ def build_kernel(config: KernelBuildConfig) -> None: ), encoding="utf-8", ) - make = ["make", "-C", source, f"O={config.work}"] + make = ["make", "-C", source, f"O={config.work}", *config.make_architecture] run_checked([*make, "olddefconfig"]) - assert_required_kernel_config(kernel_config, debug=config.debug) + if config.architecture == "x86_64": + assert_required_kernel_config(kernel_config, debug=config.debug) + else: + assert_required_kernel_config( + kernel_config, debug=config.debug, architecture=config.architecture + ) if not config.debug: # The debug variant adds its fragment, which the inventory never sets, # so only the production configuration must be the inventory's. - check_build_configuration( + arguments = ( source, config.work.with_name(KernelBuildConstants.INVENTORY_WORK_DIRECTORY_NAME), kernel_config, - BuildConstants.REPO_ROOT / KernelBuildConstants.DERIVED_CONFIG, + BuildConstants.REPO_ROOT / config.derived_config, ) + if config.architecture == "x86_64": + check_build_configuration(*arguments) + else: + check_build_configuration( + *arguments, + features_for_architecture(config.architecture), + architecture=config.architecture, + ) jobs = os.cpu_count() or 1 print(f">> building {config.output.name} with {jobs} jobs") - run_checked([*make, f"-j{jobs}", KernelBuildConstants.BINARY_NAME]) + binary = ( + KernelBuildConstants.ARM_BINARY_NAME + if config.architecture == "aarch64" + else KernelBuildConstants.BINARY_NAME + ) + run_checked([*make, f"-j{jobs}", binary]) config.output.parent.mkdir(parents=True, exist_ok=True) - shutil.copy2(config.work / KernelBuildConstants.BINARY_NAME, config.output) + binary_path = ( + KernelBuildConstants.ARM_BINARY_PATH + if config.architecture == "aarch64" + else Path(binary) + ) + shutil.copy2(config.work / binary_path, config.output) + image_header = ( + read_arm64_image(config.output) if config.architecture == "aarch64" else None + ) generated_config = config.output.with_name(f"{config.output.name}.config") shutil.copy2(kernel_config, generated_config) print(f">> built {config.output}") if ( - _kernel_source_fingerprint() != source_fingerprint + ( + _kernel_source_fingerprint() + if config.architecture == "x86_64" + else _kernel_source_fingerprint(config.architecture) + ) + != source_fingerprint or sha256_file(input_config) != input_config_sha256 or (fragment is not None and sha256_file(fragment) != fragment_sha256) ): @@ -1322,23 +1536,36 @@ def build_kernel(config: KernelBuildConfig) -> None: "kernel_sha256": sha256_file(config.output), "config_sha256": sha256_file(generated_config), } + if image_header is not None: + provenance["image_header"] = image_header provenance_path.write_text( json.dumps(provenance, indent=2) + "\n", encoding="utf-8", ) -def _kernel_config_paths() -> tuple[Path, Path]: +def _kernel_config_paths(architecture: str = "x86_64") -> tuple[Path, Path]: + if architecture == "aarch64": + return ( + BuildConstants.REPO_ROOT / KernelBuildConstants.ARM_INPUT_CONFIG, + BuildConstants.REPO_ROOT / KernelBuildConstants.ARM_DERIVED_CONFIG, + ) return ( BuildConstants.REPO_ROOT / KernelBuildConstants.INPUT_CONFIG, BuildConstants.REPO_ROOT / KernelBuildConstants.DERIVED_CONFIG, ) -def _install_kernel_config(config_text: str, derived_text: str, *, check: bool) -> None: +def _install_kernel_config( + config_text: str, derived_text: str, *, check: bool, architecture: str = "x86_64" +) -> None: """Write the generated configuration and derived record into kernel/, or, with `check`, require the checked-in files to equal them.""" - paths = _kernel_config_paths() + paths = ( + _kernel_config_paths() + if architecture == "x86_64" + else _kernel_config_paths(architecture) + ) texts = (config_text, derived_text) if check: stale = [ @@ -1366,11 +1593,23 @@ def generate_kernel_config(config: KernelBuildConfig, *, check: bool = False) -> _require_linux("kernel-config --native") require_tool("make") source, _ = prepare_kernel_source(config) - generated = generate( - source, - config.work.with_name(KernelBuildConstants.INVENTORY_WORK_DIRECTORY_NAME), - ) - _install_kernel_config(generated.text, generated.record.render(), check=check) + work = config.work.with_name(KernelBuildConstants.INVENTORY_WORK_DIRECTORY_NAME) + if config.architecture == "x86_64": + generated = generate(source, work) + _install_kernel_config(generated.text, generated.record.render(), check=check) + else: + generated = generate( + source, + work, + features_for_architecture(config.architecture), + architecture=config.architecture, + ) + _install_kernel_config( + generated.text, + generated.record.render(), + check=check, + architecture=config.architecture, + ) def generate_docker_kernel_config( @@ -1390,8 +1629,12 @@ def generate_docker_kernel_config( print(f">> generating the kernel configuration into '{destination}'") run_checked( docker_build_command( - DockerBuildConfig(artifact_destination=destination), - DockerBuildConstants.KERNEL_CONFIG_TARGET, + DockerBuildConfig( + artifact_destination=destination, architecture=config.architecture + ), + DockerBuildConstants.KERNEL_CONFIG_TARGET + if config.architecture == "x86_64" + else "arm64-kernel-config-artifacts", ), cwd=BuildConstants.REPO_ROOT, ) @@ -1399,9 +1642,18 @@ def generate_docker_kernel_config( require_file( destination / path.name, "generated kernel configuration" ).read_text(encoding="utf-8") - for path in _kernel_config_paths() + for path in ( + _kernel_config_paths() + if config.architecture == "x86_64" + else _kernel_config_paths(config.architecture) + ) ] - _install_kernel_config(texts[0], texts[1], check=check) + if config.architecture == "x86_64": + _install_kernel_config(texts[0], texts[1], check=check) + else: + _install_kernel_config( + texts[0], texts[1], check=check, architecture=config.architecture + ) def docker_build_command(config: DockerBuildConfig, target: str) -> list[str | Path]: @@ -1419,6 +1671,14 @@ def docker_build_command(config: DockerBuildConfig, target: str) -> list[str | P "--target", target, ] + if config.architecture == "aarch64": + command.extend( + ["--platform", "linux/arm64", "--build-arg", "NVX_ARCHITECTURE=aarch64"] + ) + elif config.architecture != "x86_64": + raise ScriptError( + f"unsupported Docker artifact architecture: {config.architecture}" + ) command.extend( [ "--build-arg", @@ -1473,7 +1733,25 @@ def build_docker_artifacts( "docker was not found on PATH; install Docker with the Linux engine first", ) destination = _docker_destination(config.artifact_destination) - if guest == "all": + if config.architecture == "aarch64": + if guest not in ("alpine", "ubuntu"): + raise ScriptError( + "ARM64 Docker guest production supports Alpine or Ubuntu; " + "refusing to use x86 artifacts" + ) + descriptor = guest_descriptor(guest) + target = "arm64-artifacts" if guest == "alpine" else "arm64-ubuntu-artifacts" + expected = ( + "Image", + "Image.config", + "Image.provenance.json", + descriptor.initramfs_name, + descriptor.package_manifest_name, + ) + if guest == "alpine": + expected = (*expected, InitramfsBuildConstants.PROVENANCE_NAME) + guest_label = f"ARM64 {descriptor.distribution}" + elif guest == "all": target = DockerBuildConstants.ALL_GUESTS_TARGET expected = DockerBuildConstants.ALL_GUEST_ARTIFACT_NAMES guest_label = "Alpine, Ubuntu, and Azure Linux" diff --git a/scripts/nvx_tools/build_config.py b/scripts/nvx_tools/build_config.py index 6eb35d636..a0ab7066d 100644 --- a/scripts/nvx_tools/build_config.py +++ b/scripts/nvx_tools/build_config.py @@ -13,7 +13,7 @@ OpenVMMBuildConstants, UbuntuBuildConstants, ) -from .common import artifact_path, cache_root, openvmm_binary_path +from .common import ScriptError, artifact_path, cache_root, openvmm_binary_path from .guests import GUEST_NAMES, guest_descriptor OpenVmmBackend = Literal["kvm", "mshv", "whp"] @@ -31,6 +31,28 @@ class DockerBuildConfig: linux_source_destination: Path = ( BuildConstants.SOURCE_DIR / KernelBuildConstants.SOURCE_DIRECTORY_NAME ) + architecture: str = "x86_64" + + def __post_init__(self) -> None: + if self.architecture not in ("x86_64", "aarch64"): + raise ScriptError( + f"unsupported Docker artifact architecture: {self.architecture}" + ) + + @staticmethod + def for_architecture(architecture: str) -> DockerBuildConfig: + if architecture == "x86_64": + return DockerBuildConfig() + if architecture != "aarch64": + raise ScriptError( + f"unsupported Docker artifact architecture: {architecture}" + ) + root = BuildConstants.BUILD_DIR / architecture + return DockerBuildConfig( + architecture=architecture, + artifact_destination=root, + linux_source_destination=root / "sources" / "linux", + ) @dataclass(frozen=True, slots=True) @@ -43,6 +65,18 @@ class InitramfsBuildConfig: ) ) output: Path | None = None + architecture: str = "x86_64" + + def __post_init__(self) -> None: + if self.architecture not in ("x86_64", "aarch64"): + raise ScriptError( + f"unsupported initramfs architecture: {self.architecture}" + ) + if self.architecture == "aarch64" and self.guest not in ("alpine", "ubuntu"): + raise ScriptError( + f"ARM64 {self.guest} initramfs inputs are not integrated; " + "refusing to use x86 userland or package locks" + ) @dataclass(frozen=True, slots=True) @@ -70,6 +104,48 @@ class KernelBuildConfig: output: Path = artifact_path(KernelBuildConstants.BINARY_NAME) cache_directory: Path = field(default_factory=_default_cache_directory) debug: bool = False + architecture: str = "x86_64" + + def __post_init__(self) -> None: + if self.architecture not in ("x86_64", "aarch64"): + raise ScriptError(f"unsupported kernel architecture: {self.architecture}") + + @staticmethod + def for_architecture( + architecture: str, *, debug: bool = False + ) -> KernelBuildConfig: + """Select a namespaced kernel producer without changing x86 paths or defaults.""" + if architecture == "x86_64": + return KernelBuildConfig.debug_variant() if debug else KernelBuildConfig() + if architecture != "aarch64": + raise ScriptError(f"unsupported kernel architecture: {architecture}") + suffix = "-debug" if debug else "" + return KernelBuildConfig( + architecture=architecture, + work=BuildConstants.BUILD_DIR / architecture / f"linux{suffix}", + output=BuildConstants.BUILD_DIR / architecture / f"Image{suffix}", + debug=debug, + ) + + @property + def input_config(self) -> Path: + return ( + KernelBuildConstants.ARM_INPUT_CONFIG + if self.architecture == "aarch64" + else KernelBuildConstants.INPUT_CONFIG + ) + + @property + def derived_config(self) -> Path: + return ( + KernelBuildConstants.ARM_DERIVED_CONFIG + if self.architecture == "aarch64" + else KernelBuildConstants.DERIVED_CONFIG + ) + + @property + def make_architecture(self) -> tuple[str, ...]: + return ("ARCH=arm64",) if self.architecture == "aarch64" else () @staticmethod def debug_variant() -> KernelBuildConfig: @@ -83,6 +159,8 @@ def debug_variant() -> KernelBuildConfig: @property def provenance_name(self) -> str: + if self.architecture == "aarch64": + return f"{self.output.name}.provenance.json" return ( KernelBuildConstants.DEBUG_PROVENANCE_NAME if self.debug @@ -133,17 +211,23 @@ def selected_guests(self) -> tuple[str, ...]: return (guest_descriptor(self.guest).name,) @staticmethod - def initramfs_config(guest: str) -> InitramfsBuildConfig: + def initramfs_config( + guest: str, architecture: str = "x86_64" + ) -> InitramfsBuildConfig: descriptor = guest_descriptor(guest) + directory = BuildConstants.BUILD_DIR + if architecture != "x86_64": + directory /= architecture return InitramfsBuildConfig( guest=descriptor.name, work=( - BuildConstants.BUILD_DIR + directory / InitramfsBuildConstants.WORK_DIRECTORY_TEMPLATE.format( guest=descriptor.name ) ), - output=artifact_path(descriptor.initramfs_name), + output=directory / descriptor.initramfs_name, + architecture=architecture, ) @staticmethod diff --git a/scripts/nvx_tools/build_constants.py b/scripts/nvx_tools/build_constants.py index f08e93277..26f32ac88 100644 --- a/scripts/nvx_tools/build_constants.py +++ b/scripts/nvx_tools/build_constants.py @@ -51,6 +51,11 @@ class KernelBuildConstants: BUILD_STAMP_NAME: Final = ".nvx-build.json" BUILD_CONFIG_NAME: Final = ".config" INPUT_CONFIG: Final = Path("kernel") / "config-microvm" + ARM_INPUT_CONFIG: Final = Path("kernel") / "config-microvm-arm64" + ARM_DERIVED_CONFIG: Final = Path("kernel") / "config-microvm-arm64.derived" + ARM_PATCH_DIRECTORY: Final = Path("kernel") / "patches" / "arm64" + ARM_BINARY_NAME: Final = "Image" + ARM_BINARY_PATH: Final = Path("arch") / "arm64" / "boot" / ARM_BINARY_NAME # The record of every symbol that INPUT_CONFIG sets beyond the feature # inventory, with the Kconfig rule that sets it. DERIVED_CONFIG: Final = Path("kernel") / "config-microvm.derived" @@ -148,6 +153,11 @@ class GuestRustBuildConstants: TOOLCHAIN_FILE: Final = BuildConstants.REPO_ROOT / "rust-toolchain.toml" TOOLCHAIN: Final = OpenVMMBuildConstants.RUST_TOOLCHAIN TARGET: Final = OpenVMMBuildConstants.MUSL_RUST_TARGET + ARCHITECTURES: Final = ("x86_64", "aarch64") + TARGETS: Final[dict[str, str]] = { + "x86_64": TARGET, + "aarch64": "aarch64-unknown-linux-musl", + } PROFILE: Final = "guest" FEATURE: Final = "guest" BINARIES_VARIABLE: Final = "NVX_GUEST_BINARIES" @@ -218,6 +228,14 @@ class AlpineBuildConstants: MINIROOTFS_SHA256: Final = ( "41f73e3cf5fa919b8aa5ca6b30dc48f0da2720776d7423e2a7748211456fe081" ) + ARM_MINIROOTFS_NAME: Final = f"alpine-minirootfs-{VERSION}-aarch64.tar.gz" + ARM_MINIROOTFS_URL: Final = ( + f"https://dl-cdn.alpinelinux.org/alpine/{BRANCH}/releases/" + f"aarch64/{ARM_MINIROOTFS_NAME}" + ) + ARM_MINIROOTFS_SHA256: Final = ( + "f55a90f69052c5bd6f92cb09a8f47065970830b194c917a006fb94028e721259" + ) INITRAMFS_NAME: Final = "initramfs.cpio.gz" PACKAGE_MANIFEST_NAME: Final = ( f"{INITRAMFS_NAME}{BuildConstants.PACKAGE_MANIFEST_SUFFIX}" @@ -262,6 +280,17 @@ class UbuntuBuildConstants: BASE_ARCHIVE_NAME: Final = f"ubuntu-base-{VERSION}-base-{ARCHITECTURE}.tar.gz" PACKAGE_LOCK_RELATIVE_PATH: Final = Path("ubuntu") / "packages.lock.json" PACKAGE_LOCK: Final = BuildConstants.REPO_ROOT / PACKAGE_LOCK_RELATIVE_PATH + ARM_BASE_ARCHIVE_NAME: Final = f"ubuntu-base-{VERSION}-base-arm64.tar.gz" + ARM_BASE_URL: Final = ( + "https://cdimage.ubuntu.com/ubuntu-base/releases/26.04/release/" + f"{ARM_BASE_ARCHIVE_NAME}" + ) + ARM_BASE_SHA256: Final = ( + "5a1906794ced63a71a8119c3f211ef5f0bbe0a243001b4bbd41fdf80c5b219fd" + ) + ARM_PACKAGE_LOCK: Final = ( + BuildConstants.REPO_ROOT / "ubuntu" / "packages-arm64.lock.json" + ) PACKAGE_LOCK_FORMAT: Final = 1 INITRAMFS_NAME: Final = "initramfs-ubuntu.cpio.gz" PACKAGE_MANIFEST_NAME: Final = ( diff --git a/scripts/nvx_tools/collect_ubuntu_sources.py b/scripts/nvx_tools/collect_ubuntu_sources.py index 4ca825640..01e3c228e 100644 --- a/scripts/nvx_tools/collect_ubuntu_sources.py +++ b/scripts/nvx_tools/collect_ubuntu_sources.py @@ -68,8 +68,11 @@ class SourceMetadata(TypedDict): authenticated_by: str | None -def _source_requirements(manifests: list[Path]) -> tuple[SourceRequirement, ...]: +def _source_requirements( + manifests: list[Path], +) -> tuple[str, tuple[SourceRequirement, ...]]: requirements: dict[tuple[str, str], SourceRequirement] = {} + architecture: str | None = None for path in manifests: try: raw_document = json.loads(path.read_text(encoding="utf-8")) @@ -81,9 +84,13 @@ def _source_requirements(manifests: list[Path]) -> tuple[SourceRequirement, ...] if ( document.get("guest") != "ubuntu" or document.get("release") != UbuntuBuildConstants.VERSION - or document.get("architecture") != UbuntuBuildConstants.ARCHITECTURE + or document.get("architecture") not in ("amd64", "arm64") ): raise ScriptError(f"{path} is not a pinned Ubuntu package manifest") + selected_architecture = str(document["architecture"]) + if architecture is not None and architecture != selected_architecture: + raise ScriptError("Ubuntu package manifests use different architectures") + architecture = selected_architecture raw_packages = document.get("packages") if not isinstance(raw_packages, list): raise ScriptError(f"{path} has no packages array") @@ -91,6 +98,11 @@ def _source_requirements(manifests: list[Path]) -> tuple[SourceRequirement, ...] if not isinstance(raw_package, dict): raise ScriptError(f"{path} contains a non-object package record") package = cast(dict[str, object], raw_package) + if architecture == "arm64" and package.get("architecture") not in ( + "arm64", + "all", + ): + raise ScriptError(f"{path} contains a non-ARM Ubuntu package") source_name = package.get("source_name") source_version = package.get("source_version") if ( @@ -108,7 +120,8 @@ def _source_requirements(manifests: list[Path]) -> tuple[SourceRequirement, ...] } if not requirements: raise ScriptError("Ubuntu package manifests contain no source requirements") - return tuple(requirements[key] for key in sorted(requirements)) + assert architecture is not None + return architecture, tuple(requirements[key] for key in sorted(requirements)) def _checksum_records(value: str, label: str) -> list[SourceChecksum]: @@ -743,7 +756,7 @@ def collect_ubuntu_sources( _validate_source_output(output) output = output.resolve() _validate_source_output(output) - requirements = _source_requirements(manifests) + architecture, requirements = _source_requirements(manifests) records, indexes = _load_source_records(cache, requirements) missing = [ f"{requirement['source_name']}={requirement['source_version']}" @@ -820,7 +833,7 @@ def collect_ubuntu_sources( "format": UbuntuBuildConstants.SOURCE_MANIFEST_FORMAT, "release": UbuntuBuildConstants.VERSION, "codename": UbuntuBuildConstants.CODENAME, - "architecture": UbuntuBuildConstants.ARCHITECTURE, + "architecture": architecture, "source_indexes": source_indexes, "packages": collected, }, diff --git a/scripts/nvx_tools/elf.py b/scripts/nvx_tools/elf.py index ac7e5a52b..5fc79248e 100644 --- a/scripts/nvx_tools/elf.py +++ b/scripts/nvx_tools/elf.py @@ -31,6 +31,33 @@ _HEADER = struct.Struct("<16sHHIQQQIHHHHHH") _PROGRAM_HEADER = struct.Struct(" dict[str, int]: + """Validate the uncompressed little-endian, 4-KiB-page direct-boot Image.""" + with path.open("rb") as image: + header = image.read(_ARM_IMAGE_HEADER.size) + if len(header) != _ARM_IMAGE_HEADER.size: + raise ScriptError(f"{path}: truncated ARM64 Image header") + _, _, offset, size, flags, _, _, _, magic, _ = _ARM_IMAGE_HEADER.unpack(header) + file_size = path.stat().st_size + if magic != _ARM_IMAGE_MAGIC: + raise ScriptError(f"{path}: not an ARM64 Linux Image") + if flags & 1 or (flags >> 1) & 3 != 1 or flags & ~0xF: + raise ScriptError(f"{path}: Image is not little-endian with 4-KiB pages") + if not flags & 8: + raise ScriptError(f"{path}: Image does not support reservation-safe placement") + if size < file_size or offset % 4096 or offset + size > 0xFFFF_FFFF_FFFF_FFFF: + raise ScriptError(f"{path}: invalid ARM64 Image footprint or text offset") + return { + "text_offset": offset, + "image_size": size, + "flags": flags, + "file_size": file_size, + } @dataclass(frozen=True) @@ -44,6 +71,7 @@ class ElfExecutable: needed: int dynamic: bool executable_stack: bool + machine: int @property def static(self) -> bool: @@ -123,13 +151,28 @@ def _parse(path: Path, data: bytes) -> ElfExecutable: needed=needed, dynamic=dynamic, executable_stack=executable_stack, + machine=header[2], ) -def check_static_executables(paths: Sequence[Path]) -> list[ElfExecutable]: +def check_static_executables( + paths: Sequence[Path], *, architecture: str | None = None +) -> list[ElfExecutable]: """Describe each executable, and fail unless every one is static with a non-executable stack.""" + if architecture is not None and architecture not in _MACHINES: + raise ScriptError(f"unsupported ELF architecture: {architecture}") executables = [read_elf_executable(path) for path in paths] + if architecture is not None: + wrong_machine = [ + f"{executable.path} (e_machine={executable.machine})" + for executable in executables + if executable.machine != _MACHINES[architecture] + ] + if wrong_machine: + raise ScriptError( + f"executables are not {architecture}: " + ", ".join(wrong_machine) + ) for executable in executables: print(executable.describe()) dynamic = [ diff --git a/scripts/nvx_tools/guest_binaries.py b/scripts/nvx_tools/guest_binaries.py index 4d35b1715..70aeceb2d 100644 --- a/scripts/nvx_tools/guest_binaries.py +++ b/scripts/nvx_tools/guest_binaries.py @@ -31,6 +31,29 @@ class GuestBinaryBuildResult: source_sha256: str binaries: dict[str, str] + architecture: str = "x86_64" + + +def guest_rust_target(architecture: str) -> str: + """Resolve an explicit guest ISA without inferring it from the build host.""" + try: + return GuestRustBuildConstants.TARGETS[architecture] + except KeyError as error: + raise ScriptError( + f"unsupported Rust guest architecture: {architecture}" + ) from error + + +def guest_binary_inventory(architecture: str = "x86_64") -> tuple[str, ...]: + """Keep x86's inventory and omit non-cold-boot ARM diagnostic executables.""" + guest_rust_target(architecture) + if architecture == "x86_64": + return GuestRustBuildConstants.BINARIES + return tuple( + binary + for binary in GuestRustBuildConstants.BINARIES + if binary not in ("nvx-mmio-write", "nvx-device-io", "nvx-time-probe") + ) def guest_binary_source_files(root: Path | None = None) -> tuple[Path, ...]: @@ -145,7 +168,9 @@ def _dep_info_paths(dep_info: Path) -> tuple[str, ...]: return tuple(paths) -def _check_guest_build_inputs(target_directory: Path) -> None: +def _check_guest_build_inputs( + target_directory: Path, architecture: str = "x86_64" +) -> None: """Fail unless the source identity covers every file that the build read. Cargo's dep-info for each executable lists every source file of the crate @@ -161,11 +186,11 @@ def _check_guest_build_inputs(target_directory: Path) -> None: identity = {path.resolve() for path in guest_binary_source_files()} outputs = ( target_directory - / GuestRustBuildConstants.TARGET + / guest_rust_target(architecture) / GuestRustBuildConstants.PROFILE ) omitted: set[str] = set() - for binary in GuestRustBuildConstants.BINARIES: + for binary in guest_binary_inventory(architecture): dep_info = require_file( outputs / f"{binary}.d", f"Cargo dep-info of Rust guest binary {binary}" ) @@ -202,14 +227,14 @@ def _check_guest_build_inputs(target_directory: Path) -> None: ) -def _guest_cargo_arguments() -> list[str]: +def _guest_cargo_arguments(architecture: str = "x86_64") -> list[str]: """Return the Cargo arguments that select the canonical guest build.""" return [ "--locked", "--profile", GuestRustBuildConstants.PROFILE, "--target", - GuestRustBuildConstants.TARGET, + guest_rust_target(architecture), "--no-default-features", "--features", GuestRustBuildConstants.FEATURE, @@ -218,18 +243,21 @@ def _guest_cargo_arguments() -> list[str]: def _manifest(result: GuestBinaryBuildResult) -> dict[str, object]: """Return the stable on-disk manifest for a completed build.""" - return { + manifest: dict[str, object] = { "domain": GuestRustBuildConstants.BUILD_MANIFEST_DOMAIN, "format": GuestRustBuildConstants.BUILD_MANIFEST_FORMAT, "toolchain": GuestRustBuildConstants.TOOLCHAIN, - "target": GuestRustBuildConstants.TARGET, + "target": guest_rust_target(result.architecture), "profile": GuestRustBuildConstants.PROFILE, "source_sha256": result.source_sha256, "binaries": result.binaries, } + if result.architecture != "x86_64": + manifest["architecture"] = result.architecture + return manifest -def _validate_output_path(output: Path) -> None: +def _validate_output_path(output: Path, architecture: str = "x86_64") -> None: """Reject output paths that overlap source or hold unrelated files.""" crate = GuestRustBuildConstants.DIRECTORY.resolve() toolchain = GuestRustBuildConstants.TOOLCHAIN_FILE.resolve() @@ -242,10 +270,12 @@ def _validate_output_path(output: Path) -> None: ): raise ScriptError(f"guest binary output overlaps source inputs: {output}") if output.exists(): - _validate_owned_output(output) + _validate_owned_output(output, architecture) -def _validate_owned_output(output: Path) -> tuple[str, ...]: +def _validate_owned_output( + output: Path, architecture: str = "x86_64" +) -> tuple[str, ...]: """Require an existing output to be a complete build owned by this tool. Return the retired executables that the output holds, which its build manifest @@ -257,6 +287,7 @@ def _validate_owned_output(output: Path) -> tuple[str, ...]: f"guest binary output exists and is not a directory: {output}" ) actual = {entry.name for entry in output.iterdir()} + inventory = guest_binary_inventory(architecture) retired = tuple( binary for binary in GuestRustBuildConstants.RETIRED_BINARIES @@ -265,11 +296,9 @@ def _validate_owned_output(output: Path) -> tuple[str, ...]: missing = tuple( binary for binary in GuestRustBuildConstants.ADDED_BINARIES - if binary not in actual - ) - present = tuple( - binary for binary in GuestRustBuildConstants.BINARIES if binary not in missing + if binary in inventory and binary not in actual ) + present = tuple(binary for binary in inventory if binary not in missing) expected = { GuestRustBuildConstants.BUILD_MANIFEST_NAME, *present, @@ -315,13 +344,13 @@ def _validate_owned_output(output: Path) -> tuple[str, ...]: return retired -def _install_output(staging: Path, output: Path) -> None: +def _install_output(staging: Path, output: Path, architecture: str = "x86_64") -> None: """Install staging without recursively deleting an existing directory.""" if not output.exists(): staging.replace(output) return - retired = _validate_owned_output(output) - for binary in GuestRustBuildConstants.BINARIES: + retired = _validate_owned_output(output, architecture) + for binary in guest_binary_inventory(architecture): (staging / binary).replace(output / binary) # Remove retired executables while the previous manifest still records them, so # that an interrupted installation leaves an output that the next build accepts. @@ -333,7 +362,9 @@ def _install_output(staging: Path, output: Path) -> None: staging.rmdir() -def check_guest_binaries(toolchain: str | None = None) -> None: +def check_guest_binaries( + toolchain: str | None = None, *, architecture: str = "x86_64" +) -> None: """Lint and test the guest binaries for musl. ``toolchain`` names the Rust toolchain, by default the exact repository pin. @@ -344,7 +375,7 @@ def check_guest_binaries(toolchain: str | None = None) -> None: selected = toolchain or pinned_guest_rust_toolchain() rustup = require_tool("rustup") cargo = require_tool("cargo") - target = GuestRustBuildConstants.TARGET + target = guest_rust_target(architecture) guest = ( "--locked", "--target", @@ -374,30 +405,29 @@ def check_guest_binaries(toolchain: str | None = None) -> None: ) -def inspect_guest_binaries(directory: Path) -> GuestBinaryBuildResult: +def inspect_guest_binaries( + directory: Path, *, architecture: str = "x86_64" +) -> GuestBinaryBuildResult: """Validate and identify a current, previously staged guest build.""" + guest_rust_target(architecture) directory = directory.resolve() - retired = _validate_owned_output(directory) - missing = [ - binary - for binary in GuestRustBuildConstants.BINARIES - if not (directory / binary).is_file() - ] + inventory = guest_binary_inventory(architecture) + retired = _validate_owned_output(directory, architecture) + missing = [binary for binary in inventory if not (directory / binary).is_file()] if retired or missing: raise ScriptError( f"guest binary output is not a current build of every executable: " f"{directory}" ) - paths = [directory / binary for binary in GuestRustBuildConstants.BINARIES] - check_static_executables(paths) + paths = [directory / binary for binary in inventory] + check_static_executables(paths, architecture=architecture) result = GuestBinaryBuildResult( source_sha256=guest_binary_source_sha256(), binaries={ binary: sha256_file(path) - for binary, path in zip( - GuestRustBuildConstants.BINARIES, paths, strict=True - ) + for binary, path in zip(inventory, paths, strict=True) }, + architecture=architecture, ) manifest_path = directory / GuestRustBuildConstants.BUILD_MANIFEST_NAME manifest = cast( @@ -412,14 +442,28 @@ def inspect_guest_binaries(directory: Path) -> GuestBinaryBuildResult: return result -def build_guest_binaries(output: Path) -> GuestBinaryBuildResult: +def build_guest_binaries( + output: Path, *, architecture: str = "x86_64" +) -> GuestBinaryBuildResult: """Build, statically check, digest, and stage a fresh set of executables.""" + rust_target = guest_rust_target(architecture) + inventory = guest_binary_inventory(architecture) if sys.platform != "linux": raise ScriptError("Rust guest binaries can be built only on Linux") if not output.name: raise ScriptError("guest binary output must name a directory") output = output.resolve() - _validate_output_path(output) + _validate_output_path(output, architecture) + if output.exists(): + manifest = json.loads( + (output / GuestRustBuildConstants.BUILD_MANIFEST_NAME).read_text( + encoding="utf-8" + ) + ) + if manifest.get("target", GuestRustBuildConstants.TARGET) != rust_target: + raise ScriptError( + f"guest binary output belongs to another architecture: {output}" + ) output.parent.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory( prefix=f".{output.name}.work-", @@ -440,22 +484,20 @@ def build_guest_binaries(output: Path) -> GuestBinaryBuildResult: cargo, f"+{toolchain}", "build", - *_guest_cargo_arguments(), + *_guest_cargo_arguments(architecture), ] - for binary in GuestRustBuildConstants.BINARIES: + for binary in inventory: command.extend(("--bin", binary)) run_checked( command, cwd=GuestRustBuildConstants.DIRECTORY, env=environment, ) - _check_guest_build_inputs(target) + _check_guest_build_inputs(target, architecture) - built_directory = ( - target / GuestRustBuildConstants.TARGET / GuestRustBuildConstants.PROFILE - ) + built_directory = target / rust_target / GuestRustBuildConstants.PROFILE staged_paths: list[Path] = [] - for binary in GuestRustBuildConstants.BINARIES: + for binary in inventory: source = require_file( built_directory / binary, f"built Rust guest binary {binary}" ) @@ -463,35 +505,34 @@ def build_guest_binaries(output: Path) -> GuestBinaryBuildResult: shutil.copyfile(source, destination) destination.chmod(0o755) staged_paths.append(destination) - check_static_executables(staged_paths) + check_static_executables(staged_paths, architecture=architecture) if guest_binary_source_sha256() != source_sha256: raise ScriptError( "Rust guest binary source inputs changed during the build" ) result = GuestBinaryBuildResult( source_sha256=source_sha256, - binaries={ - binary: sha256_file(staging / binary) - for binary in GuestRustBuildConstants.BINARIES - }, + binaries={binary: sha256_file(staging / binary) for binary in inventory}, + architecture=architecture, ) (staging / GuestRustBuildConstants.BUILD_MANIFEST_NAME).write_text( json.dumps(_manifest(result), indent=2, sort_keys=True) + "\n", encoding="utf-8", newline="\n", ) - _install_output(staging, output) + _install_output(staging, output, architecture) return result -def test_guest_binaries(binaries: Path) -> None: +def test_guest_binaries(binaries: Path, *, architecture: str = "x86_64") -> None: """Run the musl integration tests against a staged guest-profile build.""" + target = guest_rust_target(architecture) if sys.platform != "linux": raise ScriptError("Rust guest binaries can be tested only on Linux") binaries = binaries.resolve() if not binaries.is_dir(): raise ScriptError(f"guest binary directory not found: {binaries}") - for binary in GuestRustBuildConstants.BINARIES: + for binary in guest_binary_inventory(architecture): require_file(binaries / binary, f"Rust guest binary {binary}") require_file( binaries / GuestRustBuildConstants.BUILD_MANIFEST_NAME, @@ -506,7 +547,7 @@ def test_guest_binaries(binaries: Path) -> None: "test", "--locked", "--target", - GuestRustBuildConstants.TARGET, + target, "--no-default-features", "--features", GuestRustBuildConstants.FEATURE, diff --git a/scripts/nvx_tools/kernel_config.py b/scripts/nvx_tools/kernel_config.py index e96ed04af..cba6cd86c 100644 --- a/scripts/nvx_tools/kernel_config.py +++ b/scripts/nvx_tools/kernel_config.py @@ -48,6 +48,17 @@ HIDDEN_SETTINGS_COMMENT = ( "#\n# Inventory settings whose symbols this configuration's toolchain hides\n#\n" ) +ARM_EXPLICIT_ADDITIONS = { + "ARM64_4K_PAGES": "y", + "ARM64_VA_BITS_48": "y", + "ARM_GIC_V3": "y", + "OF_RESERVED_MEM": "y", + "NR_CPUS": "8", + "HVC_NVX_MMIO": "y", + "UNMAP_KERNEL_AT_EL0": "y", + "MITIGATE_SPECTRE_BRANCH_HISTORY": "y", + "ARM64_PTR_AUTH": "y", +} DERIVED_HEADER = """\ # Generated by `python3 scripts/nvx.py kernel-config` from the kernel feature # inventory, scripts/nvx_tools/kernel_features.py. Do not edit. @@ -310,6 +321,80 @@ def configuration_problems( return problems +def arm_configuration_problems( + config_text: str, derived_text: str, features: tuple[KernelFeature, ...] +) -> list[str]: + """Audit actual ARM values and reject x86 leakage and unowned explicit additions.""" + problems = configuration_problems( + config_text, + derived_text, + features, + config_name="kernel/config-microvm-arm64", + derived_name="kernel/config-microvm-arm64.derived", + ) + values = parse_config(config_text, "ARM64 configuration") + legacy_prefixes = ( + "X86", + "IA32", + "PARAVIRT", + "PCI", + "ACPI", + "HYPERV_", + "SERIAL_8250", + "SERIAL_AMBA_PL011", + ) + legacy = { + "HVC_XE9", + "EARLY_PRINTK", + "DEVPORT", + "DEVMEM", + "HAS_IOPORT_MAP", + "PVH", + "XEN", + "KVM_GUEST", + "HYPERV", + "HYPERVISOR_GUEST", + "COMPAT", + "HOTPLUG_CPU", + "MEMORY_HOTPLUG", + "EFI", + "EFI_STUB", + "EFI_RUNTIME_MAP", + "EFIVAR_FS", + "MODIFY_LDT_SYSCALL", + "PCSPKR_PLATFORM", + "PCMCIA", + } + for symbol, value in values.items(): + if value != NOT_SET and ( + symbol in legacy or symbol.startswith(legacy_prefixes) + ): + problems.append( + f"ARM64 configuration enables excluded option CONFIG_{symbol}={value}" + ) + if value == "m": + problems.append( + f"ARM64 configuration builds an unshipped module CONFIG_{symbol}=m" + ) + if values.get("ARM64") != "y" or values.get("ARM64_4K_PAGES") != "y": + problems.append( + "ARM64 configuration does not select the required 64-bit/4-KiB binding" + ) + if values.get("CMDLINE", '""') != '""': + problems.append( + "ARM64 configuration embeds a command line outside the machine profile" + ) + common = inventory_settings() + for symbol, (_, assignment) in inventory_settings(features).items(): + if assignment.value == NOT_SET or symbol in common: + continue + if ARM_EXPLICIT_ADDITIONS.get(symbol) != assignment.value: + problems.append( + f"ARM64 inventory adds an unnecessary or unreviewed {assignment.line}" + ) + return problems + + def toolchain_identity(values: Mapping[str, str]) -> str | None: """The toolchain that resolved a configuration, as its derived record names it: the compiler's version text, then the version of each other @@ -340,6 +425,8 @@ def derive( tree: kconfig.KconfigTree, values: Mapping[str, str], settings: Mapping[str, tuple[KernelFeature, Assignment]], + *, + require_known_symbols: bool = False, ) -> dict[str, tuple[Assignment, str]]: """Attribute each symbol that a generated configuration sets beyond the inventory's settings to the Kconfig rule that sets it.""" @@ -349,6 +436,10 @@ def derive( for symbol, value in values.items(): if value == NOT_SET or symbol in settings: continue + if require_known_symbols and symbol not in tree.symbols: + raise ScriptError( + f"configuration sets unknown Kconfig symbol {Assignment(symbol, value).line}" + ) reason = _reason(tree, evaluated, symbol) if reason is None: unexplained.append(Assignment(symbol, value).line) @@ -400,6 +491,8 @@ def generate( source: Path, work: Path, features: tuple[KernelFeature, ...] = KERNEL_FEATURES, + *, + architecture: str = "x86_64", ) -> GeneratedConfig: """Generate the configuration from a patched kernel source: `make allnoconfig` with the inventory's settings as KCONFIG_ALLCONFIG, in the @@ -422,6 +515,7 @@ def generate( source, f"O={work}", f"KCONFIG_ALLCONFIG={fragment}", + *(("ARCH=arm64",) if architecture == "aarch64" else ()), "allnoconfig", ] ) @@ -447,8 +541,26 @@ def generate( if hidden: text = text.rstrip("\n") + "\n\n" + HIDDEN_SETTINGS_COMMENT text += "\n".join(sorted(hidden)) + "\n" - tree = kconfig.read_kconfig(source) - record = DerivedRecord(toolchain_identity(values), derive(tree, values, settings)) + tree = ( + kconfig.read_kconfig(source) + if architecture == "x86_64" + else kconfig.read_kconfig(source, srcarch="arm64", arch="arm64") + ) + record = DerivedRecord( + toolchain_identity(values), + derive( + tree, + values, + settings, + require_known_symbols=architecture == "aarch64", + ), + ) + if architecture == "aarch64": + problems = arm_configuration_problems(text, record.render(), features) + if problems: + raise ScriptError( + "ARM64 configuration audit failed: " + "; ".join(problems) + ) return GeneratedConfig(text, values, record) @@ -458,6 +570,8 @@ def check_build_configuration( effective: Path, derived: Path, features: tuple[KernelFeature, ...] = KERNEL_FEATURES, + *, + architecture: str = "x86_64", ) -> None: """Require the configuration that a kernel build resolved, `effective`, to be the one that the inventory generates with the same kernel source and @@ -469,7 +583,11 @@ def check_build_configuration( detect other capabilities, so then only its entries for symbols with a prompt, which the toolchain does not decide, must match, and every other entry must name a symbol without a prompt.""" - generated = generate(source, work, features) + generated = ( + generate(source, work, features) + if architecture == "x86_64" + else generate(source, work, features, architecture=architecture) + ) resolved = parse_config(effective.read_text(encoding="utf-8"), str(effective)) problems: list[str] = [] for symbol in sorted(resolved.keys() | generated.values.keys()): @@ -481,10 +599,17 @@ def check_build_configuration( f"inventory generates {Assignment(symbol, expected).line}" ) recorded = DerivedRecord.parse(derived.read_text(encoding="utf-8"), str(derived)) - tree = kconfig.read_kconfig(source) + tree = ( + kconfig.read_kconfig(source) + if architecture == "x86_64" + else kconfig.read_kconfig(source, srcarch="arm64", arch="arm64") + ) exact = recorded.toolchain == generated.record.toolchain for symbol in sorted(recorded.entries.keys() | generated.record.entries.keys()): definition = tree.symbols.get(symbol) + if architecture == "aarch64" and definition is None: + problems.append(f"{derived} records unknown Kconfig symbol {symbol}") + continue prompted = definition is not None and definition.prompted mine = recorded.entries.get(symbol) theirs = generated.record.entries.get(symbol) @@ -519,10 +644,14 @@ def _describe(entry: tuple[Assignment, str] | None) -> str: return f"{entry[0].line} ({entry[1]})" -def summary(config_text: str, derived_text: str) -> str: +def summary( + config_text: str, + derived_text: str, + features: tuple[KernelFeature, ...] = KERNEL_FEATURES, +) -> str: values = parse_config(config_text) record = DerivedRecord.parse(derived_text) - settings = inventory_settings() + settings = inventory_settings(features) enabled = [symbol for symbol, value in values.items() if value != NOT_SET] by_inventory = [symbol for symbol in enabled if symbol in settings] prompted = [ @@ -530,6 +659,6 @@ def summary(config_text: str, derived_text: str) -> str: ] return ( f"{len(enabled)} symbols set: {len(by_inventory)} by the inventory's " - f"{len(KERNEL_FEATURES)} features, {len(record.entries)} derived, " + f"{len(features)} features, {len(record.entries)} derived, " f"{len(prompted)} of them with a prompt" ) diff --git a/scripts/nvx_tools/kernel_features.py b/scripts/nvx_tools/kernel_features.py index dc055fb72..39ceb8eb5 100644 --- a/scripts/nvx_tools/kernel_features.py +++ b/scripts/nvx_tools/kernel_features.py @@ -18,7 +18,7 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, replace @dataclass(frozen=True, slots=True) @@ -713,3 +713,166 @@ class KernelFeature: "time-abi": ("time-abi",), "hardening": ("hardening",), } + + +def features_for_architecture(architecture: str) -> tuple[KernelFeature, ...]: + """Keep the x86 inventory unchanged and bind its common capabilities to ARM.""" + if architecture == "x86_64": + return KERNEL_FEATURES + if architecture != "aarch64": + raise ValueError(f"unsupported kernel architecture: {architecture}") + replacements: dict[str, tuple[tuple[str, ...], tuple[str, ...]]] = { + "kernel-build": ( + ( + "CONFIG_WERROR=y", + "CONFIG_EXPERT=y", + "# CONFIG_GCC_PLUGINS is not set", + "CONFIG_SECTION_MISMATCH_WARN_ONLY=y", + "CONFIG_ARM64_4K_PAGES=y", + "CONFIG_ARM64_VA_BITS_48=y", + "# CONFIG_CPU_BIG_ENDIAN is not set", + ), + ("CONFIG_ARM64=y", "CONFIG_64BIT=y"), + ), + "direct-boot": ( + ( + "# CONFIG_ACPI is not set", + "# CONFIG_EFI is not set", + "# CONFIG_PCI is not set", + "CONFIG_VIRTIO_MENU=y", + "CONFIG_VIRTIO_MMIO=y", + "# CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES is not set", + "CONFIG_ARM_GIC_V3=y", + "CONFIG_OF_RESERVED_MEM=y", + ), + ("CONFIG_OF=y", "CONFIG_ARM_PSCI_FW=y", "CONFIG_VIRTIO=y"), + ), + "processors": ( + ("CONFIG_SMP=y", "CONFIG_NR_CPUS=8", "# CONFIG_HOTPLUG_CPU is not set"), + (), + ), + "scheduling": ( + ( + "CONFIG_PREEMPT_VOLUNTARY=y", + "CONFIG_PREEMPT_DYNAMIC=y", + "CONFIG_SCHED_MC=y", + "CONFIG_SCHED_CLUSTER=y", + "CONFIG_JUMP_LABEL=y", + ), + ("CONFIG_PREEMPT_RCU=y",), + ), + "time-abi": ( + ( + "# CONFIG_CPU_FREQ is not set", + "CONFIG_HZ_100=y", + "CONFIG_NO_HZ_IDLE=y", + "CONFIG_HIGH_RES_TIMERS=y", + "CONFIG_RCU_CPU_STALL_TIMEOUT=21", + "CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0", + "CONFIG_LOG_BUF_SHIFT=18", + "CONFIG_PRINTK=y", + "CONFIG_PRINTK_TIME=y", + ), + ("CONFIG_ARM_ARCH_TIMER=y",), + ), + "byte-channel-console": ( + ( + "CONFIG_TTY=y", + "CONFIG_HVC_NVX_MMIO=y", + "# CONFIG_SERIAL_AMBA_PL011 is not set", + "# CONFIG_SERIAL_AMBA_PL011_CONSOLE is not set", + ), + ("CONFIG_HVC_DRIVER=y", "CONFIG_SERIAL_EARLYCON=y"), + ), + "platform-control": ( + ("# CONFIG_DEVMEM is not set", "# CONFIG_DEVPORT is not set"), + ("CONFIG_HVC_NVX_MMIO=y",), + ), + "virtio-console": ( + ("CONFIG_VIRTIO_CONSOLE=y",), + ("CONFIG_HVC_DRIVER=y", "CONFIG_VIRTIO=y", "CONFIG_VIRTIO_MMIO=y"), + ), + "memory-hotplug": ( + ("# CONFIG_MEMORY_HOTPLUG is not set",), + (), + ), + "memory-management": ( + ( + "CONFIG_COMPACTION=y", + "# CONFIG_PT_RECLAIM is not set", + "CONFIG_SLAB_MERGE_DEFAULT=y", + "CONFIG_SLUB_CPU_PARTIAL=y", + ), + (), + ), + "cpu-mitigations": ( + ( + "CONFIG_UNMAP_KERNEL_AT_EL0=y", + "CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY=y", + ), + (), + ), + "hardening": ( + ( + "CONFIG_MODULES=y", + 'CONFIG_MODPROBE_PATH=""', + "CONFIG_TRIM_UNUSED_KSYMS=y", + "CONFIG_DEBUG_WX=y", + "CONFIG_STACKPROTECTOR=y", + "CONFIG_STACKPROTECTOR_STRONG=y", + "CONFIG_ARM64_PTR_AUTH=y", + "# CONFIG_ARM64_PTR_AUTH_KERNEL is not set", + "CONFIG_VMAP_STACK=y", + "CONFIG_RANDOMIZE_KSTACK_OFFSET=y", + "# CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT is not set", + "CONFIG_INIT_STACK_ALL_ZERO=y", + ), + ("CONFIG_STRICT_KERNEL_RWX=y", "CONFIG_STRICT_MODULE_RWX=y"), + ), + "linux-abi": ( + tuple( + setting + for feature in KERNEL_FEATURES + if feature.name == "linux-abi" + for setting in feature.settings + if not any( + name in setting + for name in ( + "IA32_EMULATION", + "COMPAT_32BIT_TIME", + "X86_", + "MODIFY_LDT_SYSCALL", + ) + ) + ) + + ("# CONFIG_COMPAT is not set",), + (), + ), + } + return tuple( + replace( + feature, + summary=f"ARM64 binding of {feature.name}", + rationale=( + "Linux 6.18.38 ARM64 does not select ARCH_SUPPORTS_PT_RECLAIM, " + "so empty user PTE reclaim cannot be enabled. Preserve compaction " + "and the slab defaults without adding a new memory-management " + "implementation to the fixed cold-boot scope." + if feature.name == "memory-management" + else "Use Linux's ordinary userspace PAC capability, exec key " + "initialization and task-switch handling when the CPU profile " + "exposes PAC. Keep stack protection and leave kernel return " + "signing and guest-helper instrumentation unchanged." + if feature.name == "hardening" + else "Preserve the shared NVX kernel capabilities with the approved " + "ARM64 direct-boot, OF, GICv3, counter and restricted platform " + "development binding; no x86 ports, firmware, PL011, 32-bit " + "workloads or memory activation are part of this cold-boot kernel." + ), + settings=replacements[feature.name][0], + expects=replacements[feature.name][1], + ) + if feature.name in replacements + else feature + for feature in KERNEL_FEATURES + ) diff --git a/scripts/nvx_tools/microvm_tests.py b/scripts/nvx_tools/microvm_tests.py index 034d58941..9e63a2931 100644 --- a/scripts/nvx_tools/microvm_tests.py +++ b/scripts/nvx_tools/microvm_tests.py @@ -872,6 +872,7 @@ def run_guest_script( contain_process_tree: bool = False, boot_marker: bytes = BOOT_MARKER, on_stderr: Callable[[bytes], None] | None = None, + expected_exit_status: int = 0, ) -> GuestScriptResult: """Run ``script`` in the shell of the guest that ``command`` boots, and require it to print the line ``completion_marker``. @@ -908,6 +909,7 @@ def run_guest_script( contain_process_tree=contain_process_tree, time_abi_status=True, on_stderr=on_stderr, + expected_exit_status=expected_exit_status, ) output = _guest_script_output(result["text"]) if not contains_output_line(output.encode("utf-8"), completion_marker): diff --git a/scripts/nvx_tools/time_abi.py b/scripts/nvx_tools/time_abi.py index 048f9ed00..57543c04b 100644 --- a/scripts/nvx_tools/time_abi.py +++ b/scripts/nvx_tools/time_abi.py @@ -11,6 +11,7 @@ from __future__ import annotations import json +import platform import re import urllib.parse from collections.abc import Mapping, Sequence @@ -573,8 +574,28 @@ def _command_option(command: Sequence[str], option: str) -> str | None: def is_cold_boot_command(command: Sequence[str]) -> bool: - """Whether an OpenVMM command boots a kernel rather than restoring.""" - return "--kernel" in command and "--restore-snapshot" not in command + """Whether a normal OpenVMM or nvx-rs run boots rather than restores.""" + cli_run = ( + bool(command) and Path(command[0]).stem == "nvx-rs" and "run" in command[1:] + ) + return ("--kernel" in command or cli_run) and "--restore-snapshot" not in command + + +def _counter_architecture(command: Sequence[str]) -> str: + """Use explicit ISA/product metadata, or the native default of the normal CLI.""" + explicit = _command_option(command, "--architecture") + if explicit is not None: + return explicit + if command and Path(command[0]).stem == "nvx-rs": + return ( + "aarch64" + if platform.machine().lower() in ("arm64", "aarch64") + else "x86_64" + ) + kernel = _command_option(command, "--kernel") + return ( + "aarch64" if kernel is not None and Path(kernel).name == "Image" else "x86_64" + ) def _validate_marker( @@ -584,6 +605,7 @@ def _validate_marker( backend: str | None, online_cpus: int | None, generation: int | None = None, + architecture: str = "x86_64", ) -> None: """Check a boot or restore line's fields against the backend's rates.""" problems: list[str] = [] @@ -593,12 +615,25 @@ def _validate_marker( problems.append(f"it is not a passing {phase} check") try: tsc_hz = int(fields.get("tsc_hz", "")) - if not MIN_TSC_HZ <= tsc_hz <= MAX_TSC_HZ: + if architecture == "aarch64": + if not 0 < tsc_hz <= 0xFFFFFFFF: + problems.append( + f"counter frequency={tsc_hz} is not a nonzero CNTFRQ value" + ) + elif architecture != "x86_64": + problems.append(f"unknown counter architecture {architecture!r}") + elif not MIN_TSC_HZ <= tsc_hz <= MAX_TSC_HZ: problems.append(f"tsc_hz={tsc_hz} is outside 500 MHz to 10 GHz") except ValueError: problems.append(f"tsc_hz={fields.get('tsc_hz')!r} is not an integer") lapic = fields.get("lapic_hz") - expected_lapic = LAPIC_HZ.get(backend) if backend is not None else None + expected_lapic = ( + 0 + if architecture == "aarch64" + else LAPIC_HZ.get(backend) + if backend is not None + else None + ) if expected_lapic is not None and lapic != str(expected_lapic): problems.append(f"lapic_hz={lapic} is not the {backend} rate {expected_lapic}") actual = fields.get("generation", "") @@ -621,9 +656,16 @@ def validate_boot_marker( *, backend: str | None, online_cpus: int | None = None, + architecture: str = "x86_64", ) -> None: """Check the boot marker's fields against the backend's declared rates.""" - _validate_marker(fields, phase="boot", backend=backend, online_cpus=online_cpus) + _validate_marker( + fields, + phase="boot", + backend=backend, + online_cpus=online_cpus, + architecture=architecture, + ) def validate_restore_marker( @@ -660,6 +702,7 @@ class TimeAbiMonitor: def __init__(self, command: Sequence[str] = ()) -> None: self.backend = _command_option(command, "--hypervisor") + self.architecture = _counter_architecture(command) self.cold_boot = is_cold_boot_command(command) self.restored = "--restore-snapshot" in command self.boot: dict[str, str] | None = None @@ -807,7 +850,7 @@ def require_boot( online_cpus: int | None = None, ) -> dict[str, str]: """Return the validated boot marker of a cold boot, or fail clearly.""" - if self.boot is None: + if self.boot is None or self.report_only: raise TimeAbiFailure( f"guest did not report the NVX-TIME-ABI boot marker before {context}; " f"{self._missing_reason()}" @@ -816,6 +859,7 @@ def require_boot( self.boot, backend=self.backend, online_cpus=online_cpus, + architecture=self.architecture, ) return self.boot @@ -832,7 +876,7 @@ def require_restore( first, and each line keeps the values from when its check ran: only the restore line describes the CPUs that are online now. """ - if not self.restores: + if not self.restores or self.report_only: raise TimeAbiFailure( f"guest did not report an NVX-TIME-ABI restore marker before " f"{context}; {self._missing_reason()}" diff --git a/scripts/nvx_tools/ubuntu.py b/scripts/nvx_tools/ubuntu.py index 28c461fd6..b70f3cb0d 100644 --- a/scripts/nvx_tools/ubuntu.py +++ b/scripts/nvx_tools/ubuntu.py @@ -113,9 +113,30 @@ def _required_string( return value +def package_architecture(architecture: str = "x86_64") -> str: + """Select an explicit Ubuntu package ISA without host inference.""" + if architecture == "x86_64": + return UbuntuBuildConstants.ARCHITECTURE + if architecture == "aarch64": + return "arm64" + raise ScriptError(f"unsupported Ubuntu architecture: {architecture}") + + +def package_lock_path(architecture: str = "x86_64") -> Path: + """Keep the original lock and the ARM lock distinct.""" + package_architecture(architecture) + return ( + UbuntuBuildConstants.ARM_PACKAGE_LOCK + if architecture == "aarch64" + else UbuntuBuildConstants.PACKAGE_LOCK + ) + + def load_package_lock( - path: Path = UbuntuBuildConstants.PACKAGE_LOCK, + path: Path | None = None, *, architecture: str = "x86_64" ) -> tuple[UbuntuLockedPackage, ...]: + path = path if path is not None else package_lock_path(architecture) + selected_architecture = package_architecture(architecture) try: raw_document = json.loads(path.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError, json.JSONDecodeError) as error: @@ -127,7 +148,7 @@ def load_package_lock( "format": UbuntuBuildConstants.PACKAGE_LOCK_FORMAT, "release": UbuntuBuildConstants.VERSION, "codename": UbuntuBuildConstants.CODENAME, - "architecture": UbuntuBuildConstants.ARCHITECTURE, + "architecture": selected_architecture, } for field, expected in expected_header.items(): if document.get(field) != expected: @@ -186,7 +207,7 @@ def load_package_lock( "pre_depends": str(package_document.get("pre_depends", "")), }, ) - if package["architecture"] not in (UbuntuBuildConstants.ARCHITECTURE, "all"): + if package["architecture"] not in (selected_architecture, "all"): raise ScriptError( f"{path} package {name} has unsupported architecture " f"{package['architecture']}" @@ -691,13 +712,15 @@ def _validate_accounts(root: Path) -> None: raise ScriptError("Ubuntu Base must contain the expected nogroup") -def _validate_usr_merge(root: Path) -> None: +def _validate_usr_merge(root: Path, *, architecture: str = "x86_64") -> None: + package_architecture(architecture) expected = { "bin": "usr/bin", "sbin": "usr/sbin", "lib": "usr/lib", - "lib64": "usr/lib64", } + if architecture == "x86_64": + expected["lib64"] = "usr/lib64" for name, target in expected.items(): path = root / name if not path.is_symlink() or os.readlink(path) != target: @@ -710,7 +733,7 @@ def _validate_usr_merge(root: Path) -> None: raise ScriptError(f"Ubuntu Base usr-merge target is invalid: /{name}") -def _customize_root(root: Path) -> None: +def _customize_root(root: Path, *, architecture: str = "x86_64") -> None: for relative, mode in ( ("root", 0o700), ("tmp", 0o1777), @@ -752,7 +775,10 @@ def _customize_root(root: Path) -> None: if path_exists(root / "usr" / "bin" / "python3"): _ensure_symlink(root, "usr/bin/python", "python3") _validate_accounts(root) - _validate_usr_merge(root) + if architecture == "x86_64": + _validate_usr_merge(root) + else: + _validate_usr_merge(root, architecture=architecture) def _validate_ubuntu_identity(root: Path) -> None: @@ -873,16 +899,29 @@ def require_posix_permissions(work: Path) -> None: ) -def prepare_root(work: Path) -> Path: +def prepare_root(work: Path, *, architecture: str = "x86_64") -> Path: + package_architecture(architecture) work.mkdir(parents=True, exist_ok=True) require_posix_permissions(work) downloads = cache_root() / BuildConstants.DOWNLOAD_DIRECTORY_NAME downloads.mkdir(parents=True, exist_ok=True) - archive = downloads / UbuntuBuildConstants.BASE_ARCHIVE_NAME + archive = downloads / ( + UbuntuBuildConstants.ARM_BASE_ARCHIVE_NAME + if architecture == "aarch64" + else UbuntuBuildConstants.BASE_ARCHIVE_NAME + ) download_verified( - UbuntuBuildConstants.BASE_URL, + ( + UbuntuBuildConstants.ARM_BASE_URL + if architecture == "aarch64" + else UbuntuBuildConstants.BASE_URL + ), archive, - UbuntuBuildConstants.BASE_SHA256, + ( + UbuntuBuildConstants.ARM_BASE_SHA256 + if architecture == "aarch64" + else UbuntuBuildConstants.BASE_SHA256 + ), ) root = work / InitramfsBuildConstants.ROOT_DIRECTORY_NAME if root.exists(): @@ -892,7 +931,11 @@ def prepare_root(work: Path) -> Path: safe_extract_tar(archive, root, label="Ubuntu Base archive") _validate_ubuntu_identity(root) - packages = load_package_lock() + packages = ( + load_package_lock() + if architecture == "x86_64" + else load_package_lock(architecture=architecture) + ) base_records = parse_deb822( (root / "var" / "lib" / "dpkg" / "status").read_text(encoding="utf-8") ) @@ -917,7 +960,10 @@ def prepare_root(work: Path) -> Path: download_verified(package["url"], package_path, package["sha256"]) _install_deb(root, package, package_path) precompile_python(root) - _customize_root(root) + if architecture == "x86_64": + _customize_root(root) + else: + _customize_root(root, architecture=architecture) return root @@ -1018,8 +1064,16 @@ def _license_path(root: Path, package_name: str, declared: str | None) -> str: return f"/{relative}" -def package_records(root: Path) -> list[UbuntuPackageRecord]: - locked = {package["name"]: package for package in load_package_lock()} +def package_records( + root: Path, *, architecture: str = "x86_64" +) -> list[UbuntuPackageRecord]: + selected_architecture = package_architecture(architecture) + lock = ( + load_package_lock() + if architecture == "x86_64" + else load_package_lock(architecture=architecture) + ) + locked = {package["name"]: package for package in lock} records = parse_deb822( (root / "var" / "lib" / "dpkg" / "status").read_text(encoding="utf-8") ) @@ -1030,13 +1084,13 @@ def package_records(root: Path) -> list[UbuntuPackageRecord]: continue name = record.get("Package") version = record.get("Version") - architecture = record.get("Architecture") + installed_architecture = record.get("Architecture") if ( not name or not version - or architecture + or installed_architecture not in ( - UbuntuBuildConstants.ARCHITECTURE, + selected_architecture, "all", ) ): @@ -1057,7 +1111,7 @@ def package_records(root: Path) -> list[UbuntuPackageRecord]: { "name": name, "version": version, - "architecture": architecture, + "architecture": installed_architecture, "source_name": source_name, "source_version": source_version, "installed": True, @@ -1083,19 +1137,25 @@ def package_records(root: Path) -> list[UbuntuPackageRecord]: def package_manifest( root: Path, helpers: HelperProvenance, + *, + architecture: str = "x86_64", ) -> dict[str, object]: return { "format": UbuntuBuildConstants.PACKAGE_MANIFEST_VERSION, "guest": UbuntuBuildConstants.GUEST_NAME, "release": UbuntuBuildConstants.VERSION, - "architecture": UbuntuBuildConstants.ARCHITECTURE, + "architecture": package_architecture(architecture), "rootfs_sha256": rootfs_sha256(root), - "packages": package_records(root), + "packages": ( + package_records(root) + if architecture == "x86_64" + else package_records(root, architecture=architecture) + ), "helpers": helpers, } -def customization_files() -> tuple[Path, ...]: +def customization_files(architecture: str = "x86_64") -> tuple[Path, ...]: return ( *( path @@ -1115,19 +1175,29 @@ def customization_files() -> tuple[Path, ...]: ) if path.is_file() ), - UbuntuBuildConstants.PACKAGE_LOCK, + package_lock_path(architecture), *guest_binary_source_files(), ) -def converter_input_sha256(customization_files: Sequence[Path]) -> str: - lock = load_package_lock() +def converter_input_sha256( + customization_files: Sequence[Path], *, architecture: str = "x86_64" +) -> str: + lock = ( + load_package_lock() + if architecture == "x86_64" + else load_package_lock(architecture=architecture) + ) document = { "domain": UbuntuBuildConstants.EROFS_DIGEST_DOMAIN, "format": UbuntuBuildConstants.EROFS_FORMAT, "release": UbuntuBuildConstants.VERSION, - "architecture": UbuntuBuildConstants.ARCHITECTURE, - "base_sha256": UbuntuBuildConstants.BASE_SHA256, + "architecture": package_architecture(architecture), + "base_sha256": ( + UbuntuBuildConstants.ARM_BASE_SHA256 + if architecture == "aarch64" + else UbuntuBuildConstants.BASE_SHA256 + ), "supplemental_packages": [ {"name": package["name"], "sha256": package["sha256"]} for package in lock ], diff --git a/scripts/test-layout-allowlist.json b/scripts/test-layout-allowlist.json index 98ea94f86..f1cd37dc8 100644 --- a/scripts/test-layout-allowlist.json +++ b/scripts/test-layout-allowlist.json @@ -6,10 +6,27 @@ "nvx-rs/src/guest/clone/activation/apply.rs": 1, "nvx-rs/src/guest/entry.rs": 1, "nvx-rs/src/guest/exec.rs": 4, + "nvx-rs/src/guest/kernel_text.rs": 4, + "nvx-rs/src/guest/parse.rs": 4, "nvx-rs/src/guest/reseed.rs": 1, + "nvx-rs/src/guest/root/block.rs": 1, + "nvx-rs/src/guest/root/kernel.rs": 2, + "nvx-rs/src/guest/status.rs": 9, + "nvx-rs/src/guest/sys/clock.rs": 6, + "nvx-rs/src/guest/sys/discipline.rs": 4, "nvx-rs/src/guest/sys/entry.rs": 3, - "nvx-rs/src/guest/sys/process.rs": 1, + "nvx-rs/src/guest/sys/fd.rs": 3, + "nvx-rs/src/guest/sys/filesystem.rs": 3, + "nvx-rs/src/guest/sys/ioctl.rs": 1, + "nvx-rs/src/guest/sys/kernel.rs": 5, + "nvx-rs/src/guest/sys/mod.rs": 5, + "nvx-rs/src/guest/sys/poll.rs": 3, + "nvx-rs/src/guest/sys/process.rs": 3, + "nvx-rs/src/guest/sys/scheduler.rs": 4, + "nvx-rs/src/guest/sys/signal.rs": 5, + "nvx-rs/src/guest/sys/socket.rs": 1, "nvx-rs/src/guest/sys/spawn.rs": 6, + "nvx-rs/src/guest/time/events.rs": 7, "nvx-rs/src/guest/time/system.rs": 3, "nvx-rs/src/host/cli/store/mod.rs": 1, "nvx-rs/src/host/network/address.rs": 2, diff --git a/scripts/test_guest_binaries.py b/scripts/test_guest_binaries.py index 46cf573fe..361af656a 100644 --- a/scripts/test_guest_binaries.py +++ b/scripts/test_guest_binaries.py @@ -6,6 +6,7 @@ import json import os import re +import struct import sys import tempfile import unittest @@ -16,7 +17,14 @@ sys.path.insert(0, str(Path(__file__).parent)) import nvx # noqa: E402 -from nvx_tools import guest_binaries # noqa: E402 +from nvx_tools import ( # noqa: E402 + build, + build_config, + collect_ubuntu_sources, + elf, + guest_binaries, + ubuntu, +) from nvx_tools.build_constants import ( # noqa: E402 BuildConstants, GuestRustBuildConstants, @@ -1041,5 +1049,372 @@ def test_source_identity_command_prints_the_recorded_source_digest(self): self.assertEqual(stdout.getvalue(), "c" * 64 + "\n") +class GuestBinaryArchitectureTests(unittest.TestCase): + def test_common_guest_build_cli_selects_one_architecture_for_every_producer(self): + args = nvx.parse_args( + [ + "build-guest", + "--guest", + "ubuntu", + "--architecture", + "aarch64", + "--native", + ] + ) + config = nvx._build_config(args) + self.assertEqual(config.kernel.architecture, "aarch64") + self.assertEqual(config.docker.architecture, "aarch64") + with ( + patch.object(build, "build_kernel") as kernel, + patch.object(build, "build_initramfs") as initramfs, + ): + build.build_guest(config) + kernel.assert_called_once_with(config.kernel) + self.assertEqual(initramfs.call_args.args[0].architecture, "aarch64") + self.assertIn("aarch64", initramfs.call_args.args[0].work.parts) + self.assertEqual( + nvx._build_config(nvx.parse_args(["build-guest"])), + build_config.BuildConfig(), + ) + for extra in (["--guest", "all"], ["--debug-kernel"]): + rejected = nvx._build_config( + nvx.parse_args(["build-guest", "--architecture", "aarch64", *extra]) + ) + with self.assertRaises(build.ScriptError): + build.build_guest(rejected) + + def test_arm_ubuntu_source_inputs_keep_architecture_and_reject_mixing(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + packages = [ + { + "architecture": "arm64", + "source_name": "busybox", + "source_version": "1:1.37.0-7ubuntu1", + } + ] + manifest: dict[str, object] = { + "guest": "ubuntu", + "release": "26.04.1", + "architecture": "arm64", + "packages": packages, + } + arm = root / "arm.json" + arm.write_text(json.dumps(manifest), encoding="utf-8") + architecture, requirements = collect_ubuntu_sources._source_requirements( + [arm] + ) + self.assertEqual(architecture, "arm64") + self.assertEqual(requirements[0]["source_name"], "busybox") + x86 = root / "x86.json" + x86.write_text( + json.dumps({**manifest, "architecture": "amd64"}), encoding="utf-8" + ) + with self.assertRaisesRegex(build.ScriptError, "different architectures"): + collect_ubuntu_sources._source_requirements([arm, x86]) + packages[0]["architecture"] = "amd64" + arm.write_text(json.dumps(manifest), encoding="utf-8") + with self.assertRaisesRegex(build.ScriptError, "non-ARM Ubuntu package"): + collect_ubuntu_sources._source_requirements([arm]) + + def test_arm_initramfs_inputs_and_work_are_distinct_from_x86(self): + x86 = build_config.BuildConfig.initramfs_config("alpine") + arm = build_config.BuildConfig.initramfs_config("alpine", "aarch64") + self.assertEqual(x86.architecture, "x86_64") + self.assertNotEqual(x86.work, arm.work) + self.assertNotEqual(x86.output, arm.output) + self.assertIn("aarch64", arm.work.parts) + self.assertIn("aarch64", build._alpine_tarball(arm).name) + self.assertIn("x86_64", build._alpine_tarball(x86).name) + legacy = build.initramfs_provenance_inputs() + selected = build.initramfs_provenance_inputs("aarch64") + self.assertNotIn("architecture", legacy) + self.assertEqual(selected["architecture"], "aarch64") + self.assertNotEqual(selected["alpine"], legacy["alpine"]) + with self.assertRaisesRegex( + build.ScriptError, "refusing to use x86 userland or package locks" + ): + build_config.BuildConfig.initramfs_config("azurelinux", "aarch64") + + def test_arm_ubuntu_lock_is_separate_and_keeps_source_versions(self): + x86 = ubuntu.load_package_lock() + arm = ubuntu.load_package_lock(architecture="aarch64") + self.assertEqual(len(x86), len(arm)) + for before, after in zip(x86, arm, strict=True): + self.assertEqual(before["name"], after["name"]) + self.assertEqual(before["source_name"], after["source_name"]) + self.assertEqual(before["source_version"], after["source_version"]) + self.assertEqual(before["version"], after["version"]) + self.assertEqual(after["architecture"], "arm64") + self.assertNotEqual(before["sha256"], after["sha256"]) + self.assertIn("ports.ubuntu.com/ubuntu-ports/", after["url"]) + with self.assertRaisesRegex(build.ScriptError, "architecture must be 'arm64'"): + ubuntu.load_package_lock(ubuntu.package_lock_path(), architecture="aarch64") + self.assertNotEqual( + ubuntu.converter_input_sha256(ubuntu.customization_files()), + ubuntu.converter_input_sha256( + ubuntu.customization_files("aarch64"), architecture="aarch64" + ), + ) + + def test_arm_package_manifest_rejects_an_actual_x86_package(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + installed = root / "lib" / "apk" / "db" / "installed" + installed.parent.mkdir(parents=True) + installed.write_text("P:busybox\nV:1\nA:x86_64\n\n", encoding="utf-8") + output = root / "initramfs.cpio.gz" + with self.assertRaisesRegex( + build.ScriptError, "incompatible package: busybox" + ): + build._write_apk_manifest(root, output, {}, architecture="aarch64") + installed.write_text("P:busybox\nV:1\nA:aarch64\n\n", encoding="utf-8") + build._write_apk_manifest(root, output, {}, architecture="aarch64") + manifest = json.loads( + output.with_name(output.name + ".packages.json").read_text( + encoding="utf-8" + ) + ) + self.assertEqual(manifest["architecture"], "aarch64") + self.assertEqual(manifest["packages"][0]["architecture"], "aarch64") + + def test_arm_initramfs_cli_preserves_legacy_default_routing(self): + for selected in ([], ["--architecture", "aarch64"]): + args = nvx.parse_args(["build-initramfs", *selected]) + with patch.object(nvx, "build_initramfs") as called: + args.handler(args) + self.assertEqual( + called.call_args.args[0], + build_config.BuildConfig.initramfs_config( + "alpine", "aarch64" if selected else "x86_64" + ), + ) + + def test_arm_cold_boot_inventory_keeps_required_time_and_omits_diagnostics(self): + self.assertIs( + guest_binaries.guest_binary_inventory(), + GuestRustBuildConstants.BINARIES, + ) + inventory = guest_binaries.guest_binary_inventory("aarch64") + self.assertEqual(len(inventory), 7) + self.assertIn("nvx-time", inventory) + self.assertIn("nvx-init", inventory) + self.assertIn("nvx-exit", inventory) + for binary in ("nvx-mmio-write", "nvx-device-io", "nvx-time-probe"): + self.assertNotIn(binary, inventory) + + def test_docker_architecture_input_reuses_the_shared_producer_and_export(self): + dockerfile = (BuildConstants.REPO_ROOT / "docker" / "Dockerfile").read_text( + encoding="utf-8" + ) + builder = dockerfile.split("FROM ${RUST_BUILDER_IMAGE} AS guest-binaries", 1)[ + 1 + ].split("\nFROM ", 1)[0] + self.assertIn("ARG NVX_ARCHITECTURE", builder) + self.assertIn("ARG NVX_ARCHITECTURE=x86_64", dockerfile) + self.assertIn("guest_rust_target(sys.argv[1])", builder) + self.assertIn('set -- --architecture "$NVX_ARCHITECTURE"', builder) + self.assertEqual(dockerfile.count("build-guest-binaries"), 1) + self.assertIn( + "FROM scratch AS guest-binary-artifacts\n" + "COPY --from=guest-binaries /out/guest-binaries/ /", + dockerfile, + ) + + def test_explicit_target_resolution_and_legacy_manifest(self): + for architecture, target in GuestRustBuildConstants.TARGETS.items(): + with self.subTest(architecture=architecture): + self.assertEqual(guest_binaries.guest_rust_target(architecture), target) + self.assertIn( + target, guest_binaries._guest_cargo_arguments(architecture) + ) + with self.assertRaisesRegex(guest_binaries.ScriptError, "unsupported"): + guest_binaries.guest_rust_target("arm64") + result = guest_binaries.GuestBinaryBuildResult("a" * 64, {}) + self.assertEqual( + guest_binaries._manifest(result), + { + "domain": GuestRustBuildConstants.BUILD_MANIFEST_DOMAIN, + "format": GuestRustBuildConstants.BUILD_MANIFEST_FORMAT, + "toolchain": GuestRustBuildConstants.TOOLCHAIN, + "target": GuestRustBuildConstants.TARGET, + "profile": GuestRustBuildConstants.PROFILE, + "source_sha256": "a" * 64, + "binaries": {}, + }, + ) + + @staticmethod + def _elf(machine: int) -> bytes: + return struct.pack( + "<16sHHIQQQIHHHHHH", + b"\x7fELF\x02\x01\x01" + bytes(9), + 2, + machine, + 1, + 0, + 64, + 0, + 0, + 64, + 56, + 0, + 64, + 0, + 0, + ) + + def test_elf_machine_admission_rejects_mixed_and_unknown_isa(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + x86, arm = root / "x86", root / "arm" + x86.write_bytes(self._elf(62)) + arm.write_bytes(self._elf(183)) + with contextlib.redirect_stdout(io.StringIO()): + self.assertEqual( + elf.check_static_executables([arm], architecture="aarch64")[ + 0 + ].machine, + 183, + ) + self.assertEqual( + elf.check_static_executables([x86], architecture="x86_64")[ + 0 + ].machine, + 62, + ) + self.assertEqual(len(elf.check_static_executables([x86, arm])), 2) + for paths, architecture in ( + ([x86], "aarch64"), + ([arm], "x86_64"), + ([arm, x86], "aarch64"), + ([arm], "arm64"), + ): + with self.subTest(architecture=architecture, paths=paths): + with self.assertRaises(guest_binaries.ScriptError): + elf.check_static_executables(paths, architecture=architecture) + + def test_arm_build_preserves_fresh_targets_and_checks_staged_isa(self): + targets: list[Path] = [] + + def cargo( + command: list[str | os.PathLike[str]], + *, + cwd: Path, + env: dict[str, str], + ) -> None: + target = Path(env["CARGO_TARGET_DIR"]) + targets.append(target) + self.assertEqual(cwd, GuestRustBuildConstants.DIRECTORY) + self.assertIn("--locked", command) + self.assertEqual(command[1], "+1.97.1") + self.assertEqual( + command[command.index("--target") + 1], "aarch64-unknown-linux-musl" + ) + inventory = guest_binaries.guest_binary_inventory("aarch64") + self.assertEqual( + [ + command[index + 1] + for index, value in enumerate(command) + if value == "--bin" + ], + list(inventory), + ) + outputs = target / "aarch64-unknown-linux-musl" / "guest" + outputs.mkdir(parents=True) + for binary in inventory: + (outputs / binary).write_bytes(self._elf(183)) + _write_dep_info( + outputs, + list(inventory), + (GuestRustBuildConstants.DIRECTORY / "src" / "lib.rs",), + ) + + with tempfile.TemporaryDirectory() as temporary: + output = Path(temporary) / "arm" + with ( + patch.object(guest_binaries.sys, "platform", "linux"), + patch.object(guest_binaries, "require_tool", return_value="cargo"), + patch.object(guest_binaries, "run_checked", side_effect=cargo), + contextlib.redirect_stdout(io.StringIO()), + ): + for _ in range(2): + result = guest_binaries.build_guest_binaries( + output, architecture="aarch64" + ) + self.assertEqual( + guest_binaries.inspect_guest_binaries( + output, architecture="aarch64" + ), + result, + ) + with self.assertRaisesRegex(guest_binaries.ScriptError, "not owned"): + guest_binaries.inspect_guest_binaries(output) + before = _contents(output) + with self.assertRaisesRegex(guest_binaries.ScriptError, "not owned"): + guest_binaries.build_guest_binaries(output) + self.assertEqual(_contents(output), before) + required = output / "nvx-time" + required_bytes = required.read_bytes() + required.unlink() + with self.assertRaisesRegex(guest_binaries.ScriptError, "not owned"): + guest_binaries.inspect_guest_binaries( + output, architecture="aarch64" + ) + required.write_bytes(required_bytes) + required.chmod(0o755) + guest_binaries.build_guest_binaries(output, architecture="aarch64") + manifest_path = output / "manifest.json" + original = manifest_path.read_bytes() + stale = json.loads(original) + stale["source_sha256"] = "0" * 64 + manifest_path.write_text(json.dumps(stale), encoding="utf-8") + with self.assertRaisesRegex( + guest_binaries.ScriptError, "does not match the current" + ): + guest_binaries.inspect_guest_binaries( + output, architecture="aarch64" + ) + manifest_path.write_bytes(original) + self.assertEqual(_contents(output), before) + self.assertEqual(len(targets), 3) + self.assertEqual(len(set(targets)), 3) + self.assertTrue(all(not target.exists() for target in targets)) + manifest = json.loads( + (output / "manifest.json").read_text(encoding="utf-8") + ) + self.assertEqual(manifest["architecture"], "aarch64") + self.assertEqual(manifest["target"], "aarch64-unknown-linux-musl") + self.assertEqual(manifest["toolchain"], "1.97.1") + + def test_arm_cli_routes_only_explicit_architecture(self): + for command, path_option, function in ( + ("build-guest-binaries", "--output", "build_guest_binaries"), + ("test-guest-binaries", "--binaries", "test_guest_binaries"), + ): + args = nvx.parse_args( + [ + command, + path_option, + "build/arm/guest-binaries", + "--architecture", + "aarch64", + ] + ) + result = guest_binaries.GuestBinaryBuildResult("a" * 64, {}, "aarch64") + with ( + patch.object(nvx, function, return_value=result) as called, + contextlib.redirect_stdout(io.StringIO()), + ): + args.handler(args) + called.assert_called_once_with( + Path("build/arm/guest-binaries"), architecture="aarch64" + ) + args = nvx.parse_args(["check-guest-binaries", "--architecture", "aarch64"]) + with patch.object(nvx, "check_guest_binaries") as called: + args.handler(args) + called.assert_called_once_with(None, architecture="aarch64") + + if __name__ == "__main__": unittest.main() diff --git a/scripts/test_kernel_config.py b/scripts/test_kernel_config.py index 57fdcf3d8..ea1d755cc 100644 --- a/scripts/test_kernel_config.py +++ b/scripts/test_kernel_config.py @@ -15,15 +15,17 @@ from __future__ import annotations import argparse +import struct import sys import tempfile import unittest from dataclasses import replace from pathlib import Path +from typing import cast from unittest.mock import patch sys.path.insert(0, str(Path(__file__).parent)) -from nvx_tools import build, build_config, common, kconfig # noqa: E402 +from nvx_tools import build, build_config, common, elf, kconfig # noqa: E402 from nvx_tools import kernel_config as kc # noqa: E402 from nvx_tools.build_constants import ( # noqa: E402 BuildConstants, @@ -33,6 +35,7 @@ KERNEL_CONFIG_CHECKS, KERNEL_FEATURES, KernelFeature, + features_for_architecture, ) from test_kconfig import read_tree, write_tree # noqa: E402 @@ -103,6 +106,25 @@ def test_refuses_a_symbol_that_a_user_set_beyond_the_inventory(self): with self.assertRaisesRegex(common.ScriptError, "CONFIG_OPTION=y"): kc.derive(tree, values, kc.inventory_settings(features("CONFIG_BASE=y"))) + def test_strict_attribution_cannot_treat_an_unknown_symbol_as_hidden(self): + tree = read_tree() + settings = kc.inventory_settings(features("CONFIG_BASE=y")) + with self.assertRaisesRegex( + common.ScriptError, "unknown Kconfig symbol CONFIG_UNKNOWN=y" + ): + kc.derive( + tree, + {"BASE": "y", "UNKNOWN": "y"}, + settings, + require_known_symbols=True, + ) + self.assertEqual( + kc.derive( + tree, {"BASE": "y", "HELPER": "y"}, settings, require_known_symbols=True + )["HELPER"][1], + kc.NO_PROMPT, + ) + class InventoryTests(unittest.TestCase): def test_the_inventory_is_valid(self): @@ -439,5 +461,210 @@ def stop_after_olddefconfig(command: list[object], **_: object) -> None: ) +class ArmKernelBindingTests(unittest.TestCase): + def test_actual_arm_config_rejects_x86_leakage_even_with_forged_attribution(self): + config = (REPO_ROOT / KernelBuildConstants.ARM_INPUT_CONFIG).read_text( + encoding="utf-8" + ) + record = kc.DerivedRecord.parse( + (REPO_ROOT / KernelBuildConstants.ARM_DERIVED_CONFIG).read_text( + encoding="utf-8" + ) + ) + arm = features_for_architecture("aarch64") + for symbol in ( + "X86_64", + "HVC_XE9", + "SERIAL_8250", + "ACPI", + "COMPAT", + "HOTPLUG_CPU", + "HAS_IOPORT_MAP", + ): + lines = [ + line + for line in config.splitlines() + if (assignment := kc.Assignment.parse(line)) is None + or assignment.symbol != symbol + ] + lines.append(f"CONFIG_{symbol}=y") + entries = dict(record.entries) + entries[symbol] = (kc.Assignment(symbol, "y"), kc.NO_PROMPT) + forged = replace(record, entries=entries).render() + problems = kc.arm_configuration_problems( + "\n".join(lines) + "\n", forged, arm + ) + self.assertTrue( + any( + f"excluded option CONFIG_{symbol}=y" in problem + for problem in problems + ), + problems, + ) + + def test_actual_arm_config_rejects_unnecessary_explicit_extension(self): + config = (REPO_ROOT / KernelBuildConstants.ARM_INPUT_CONFIG).read_text( + encoding="utf-8" + ) + record = (REPO_ROOT / KernelBuildConstants.ARM_DERIVED_CONFIG).read_text( + encoding="utf-8" + ) + arm = features_for_architecture("aarch64") + extension = KernelFeature( + name="unnecessary-arm-extension", + summary="Test extension", + rationale="Negative fixture", + consumers=("test",), + validation=("test",), + settings=("CONFIG_ARM64_SVE=y",), + ) + lines = [line for line in config.splitlines() if "CONFIG_ARM64_SVE" not in line] + problems = kc.arm_configuration_problems( + "\n".join(lines) + "\nCONFIG_ARM64_SVE=y\n", + record, + (*arm, extension), + ) + self.assertTrue( + any( + "unnecessary or unreviewed CONFIG_ARM64_SVE=y" in problem + for problem in problems + ), + problems, + ) + + def test_arm_audit_still_requires_common_product_capabilities(self): + config = (REPO_ROOT / KernelBuildConstants.ARM_INPUT_CONFIG).read_text( + encoding="utf-8" + ) + record = (REPO_ROOT / KernelBuildConstants.ARM_DERIVED_CONFIG).read_text( + encoding="utf-8" + ) + self.assertIn("CONFIG_CGROUPS=y", config) + problems = kc.arm_configuration_problems( + config.replace("CONFIG_CGROUPS=y", "# CONFIG_CGROUPS is not set"), + record, + features_for_architecture("aarch64"), + ) + self.assertTrue( + any("CONFIG_CGROUPS=y" in problem for problem in problems), problems + ) + + def test_arm_audit_keeps_gpt_and_unavoidable_arm_capabilities(self): + config = (REPO_ROOT / KernelBuildConstants.ARM_INPUT_CONFIG).read_text( + encoding="utf-8" + ) + record = (REPO_ROOT / KernelBuildConstants.ARM_DERIVED_CONFIG).read_text( + encoding="utf-8" + ) + values = kc.parse_config(config) + self.assertEqual(values["EFI_PARTITION"], "y") + self.assertEqual(values["HAS_IOPORT"], "y") + self.assertEqual(values["NO_IOPORT_MAP"], "y") + self.assertEqual(values.get("HAS_IOPORT_MAP", "n"), "n") + self.assertEqual( + kc.arm_configuration_problems( + config, record, features_for_architecture("aarch64") + ), + [], + ) + + def test_arm_inventory_is_coherent_and_keeps_x86_inventory_identity(self): + self.assertIs(features_for_architecture("x86_64"), KERNEL_FEATURES) + arm = features_for_architecture("aarch64") + self.assertEqual(kc.inventory_problems(arm), []) + settings = kc.inventory_settings(arm) + for symbol, value in { + "ARM64_4K_PAGES": "y", + "ARM_GIC_V3": "y", + "OF_RESERVED_MEM": "y", + "HVC_NVX_MMIO": "y", + "VIRTIO_MMIO_CMDLINE_DEVICES": "n", + "DEVMEM": "n", + "DEVPORT": "n", + "COMPAT": "n", + "MEMORY_HOTPLUG": "n", + "PT_RECLAIM": "n", + "ARM64_PTR_AUTH": "y", + "ARM64_PTR_AUTH_KERNEL": "n", + }.items(): + self.assertEqual(settings[symbol][1].value, value) + with self.assertRaises(ValueError): + features_for_architecture("arm64") + + def test_arm_pac_uses_existing_user_keys_without_enabling_kernel_signing(self): + config = (REPO_ROOT / KernelBuildConstants.ARM_INPUT_CONFIG).read_text( + encoding="utf-8" + ) + values = kc.parse_config(config) + for symbol, value in { + "ARM64_PTR_AUTH": "y", + "ARM64_PTR_AUTH_KERNEL": "n", + "STACKPROTECTOR": "y", + "STACKPROTECTOR_STRONG": "y", + }.items(): + self.assertEqual(values.get(symbol, "n"), value) + x86 = kc.inventory_settings(features_for_architecture("x86_64")) + self.assertNotIn("ARM64_PTR_AUTH", x86) + self.assertNotIn("ARM64_PTR_AUTH_KERNEL", x86) + + def test_kernel_paths_and_patch_sets_keep_architectures_separate(self): + x86 = build_config.KernelBuildConfig.for_architecture("x86_64") + self.assertEqual(x86, build_config.KernelBuildConfig()) + arm = build_config.KernelBuildConfig.for_architecture("aarch64") + self.assertEqual(arm.output.name, "Image") + self.assertEqual(arm.input_config, KernelBuildConstants.ARM_INPUT_CONFIG) + self.assertEqual(arm.make_architecture, ("ARCH=arm64",)) + self.assertNotEqual(arm.work.parent, x86.work.parent) + + def patches(architecture: str) -> list[str]: + source = cast( + dict[str, object], + build.kernel_provenance_inputs(architecture)["source"], + ) + records = cast(list[dict[str, str]], source["patches"]) + return [record["path"] for record in records] + + x86_patches = patches("x86_64") + arm_patches = patches("aarch64") + self.assertEqual(arm_patches[: len(x86_patches)], x86_patches) + self.assertTrue( + all( + path.startswith("kernel/patches/arm64/") + for path in arm_patches[len(x86_patches) :] + ) + ) + with self.assertRaises(common.ScriptError): + build_config.KernelBuildConfig(architecture="arm64") + + def test_image_header_checks_exact_flags_bounds_and_overflow(self): + def header( + offset: int = 0, size: int = 64, flags: int = 10, magic: int = 0x644D5241 + ) -> bytes: + return struct.pack( + " None: def read_output(self, chunks: queue.Queue[bytes | None]) -> None: chunks.put(b"NVX-GUEST-BOOT-OK: alpine\r\n") typed = self._input.get(timeout=5).decode() + if typed == time_abi.status_script(): + arm = time_abi.TimeAbiMonitor(["nvx-rs", "run"]).architecture == "aarch64" + chunks.put( + b"NVX-TIME-ABI: v=1 phase=boot status=ok cpus=1 " + + ( + b"tsc_hz=19200000 lapic_hz=0" + if arm + else b"tsc_hz=2194804000 lapic_hz=1000000000" + ) + + b" generation=0 elapsed_us=1\n" + b"NVX-TIME-STATUS-EXIT status=0\n" + ) + typed = self._input.get(timeout=5).decode() files: dict[str, list[str]] = {} arrived = 0 consumed = 0 @@ -2523,6 +2536,7 @@ def test_scripts_run_once_the_guest_has_consumed_them(self): timeout=30, log_path=Path("guest.log"), contain_process_tree=True, + expected_exit_status=42, ) run.assert_called_once_with( ["openvmm"], @@ -2539,6 +2553,7 @@ def test_scripts_run_once_the_guest_has_consumed_them(self): contain_process_tree=True, time_abi_status=True, on_stderr=None, + expected_exit_status=42, ) self.assertEqual(result["output"], "NVX-OUT none\r\nNVX-DONE\r\n") self.assertEqual(result["text"], transcript) diff --git a/scripts/test_nvx_tools.py b/scripts/test_nvx_tools.py index de16efcaa..7e172198f 100644 --- a/scripts/test_nvx_tools.py +++ b/scripts/test_nvx_tools.py @@ -15686,10 +15686,13 @@ def test_materialize_kernel_provenance_inputs_bypasses_mutable_index(self): config = root / "kernel" / "config-microvm" patch_path = root / "kernel" / "patches" / "example.patch" stale_patch = root / "kernel" / "patches" / "stale.patch" + arm_patch = root / "kernel" / "patches" / "arm64" / "arm.patch" config.parent.mkdir(parents=True) patch_path.parent.mkdir() config.write_bytes(b"CONFIG_EXAMPLE=y\n") patch_path.write_bytes(b"patch\n") + arm_patch.parent.mkdir() + arm_patch.write_bytes(b"ARM patch\n") (root / ".gitattributes").write_text( "\n".join( ( @@ -15725,6 +15728,7 @@ def test_materialize_kernel_provenance_inputs_bypasses_mutable_index(self): ".gitattributes", "kernel/config-microvm", "kernel/patches/example.patch", + "kernel/patches/arm64/arm.patch", ], cwd=root, env=git_environment, @@ -15759,6 +15763,7 @@ def test_materialize_kernel_provenance_inputs_bypasses_mutable_index(self): config.write_bytes(b"CONFIG_EXAMPLE=y\r\n") patch_path.write_bytes(b"patch\r\n") stale_patch.write_bytes(b"stale\r\n") + arm_patch.write_bytes(b"preserve ARM worktree patch\n") subprocess.run( [ "git", @@ -15798,6 +15803,7 @@ def test_materialize_kernel_provenance_inputs_bypasses_mutable_index(self): self.assertEqual(config.read_bytes(), b"CONFIG_EXAMPLE=y\n") self.assertEqual(patch_path.read_bytes(), b"patch\n") self.assertFalse(stale_patch.exists()) + self.assertEqual(arm_patch.read_bytes(), b"preserve ARM worktree patch\n") def test_manifest_tracks_every_kernel_patch(self): manifest = json.loads( @@ -17024,7 +17030,10 @@ def test_ubuntu_source_requirements_deduplicate_binary_manifests(self): ) manifests.append(path) - requirements = collect_ubuntu_sources._source_requirements(manifests) + architecture, requirements = collect_ubuntu_sources._source_requirements( + manifests + ) + self.assertEqual(architecture, "amd64") self.assertEqual( requirements, @@ -18002,6 +18011,55 @@ def test_docker_distro_layer_targets_export_the_scratch_template(self): )[0] self.assertIn(copy, section) + @unittest.skipUnless(shutil.which("sh"), "POSIX shell is unavailable") + def test_alpine_container_entry_uses_its_fixed_control_isa_loader(self): + source = ( + BuildConstants.REPO_ROOT / "guest" / "alpine" / "nvx-container-enter" + ).read_text(encoding="utf-8") + selection = ( + "case $(uname -m) in\n" + + source.split("case $(uname -m) in\n", 1)[1].split("\nesac\n", 1)[0] + + "\nesac\n" + ) + for architecture, expected in ( + ("x86_64", "ld-musl-x86_64.so.1"), + ("aarch64", "ld-musl-aarch64.so.1"), + ("riscv64", None), + ): + with self.subTest(architecture=architecture): + result = subprocess.run( + [ + "sh", + "-c", + f"uname() {{ printf '%s\\n' {architecture}; }}\n" + + selection + + 'printf "%s\\n" "$loader"', + ], + capture_output=True, + text=True, + check=False, + ) + if expected is None: + self.assertEqual(result.returncode, 125) + self.assertEqual(result.stdout, "") + self.assertIn( + "unsupported control guest architecture", result.stderr + ) + else: + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, expected + "\n") + self.assertEqual(result.stderr, "") + self.assertIn('cp "/lib/$loader" "$tools/$loader"', source) + self.assertIn('exec chroot "$rootfs" "/.nvx-agent/$loader"', source) + for option in ( + "--clear-groups", + "--no-new-privs", + "--bounding-set=-all", + "--inh-caps=-all", + "--ambient-caps=-all", + ): + self.assertIn(option, source) + def test_azurelinux_initramfs_uses_locked_busybox_applets(self): dockerfile = (BuildConstants.REPO_ROOT / "docker" / "Dockerfile").read_text( encoding="utf-8" @@ -18978,6 +19036,74 @@ def setUp(self): "mount_host_export", ) + def _run_arm_role_query( + self, scenario: str, action: str = "resolve", address: str = "0xd0003000" + ) -> tuple[subprocess.CompletedProcess[str], list[str]]: + log = self.root / "role-query.log" + log.unlink(missing_ok=True) + functions = "".join( + _shell_function(self.source, name) + for name in ("resolve_block_device", "refuse_scratch_device") + ) + script = ( + "set -eu\nscenario=$1\nprobe_log=$2\naction=$3\naddress=$4\n" + "uname() { printf 'aarch64\\n'; }\n" + "sleep() { :; }\n" + 'fatal() { printf "%s\\n" "$*" >&2; exit 125; }\n' + "root_device_query() {\n" + ' printf "%s\\n" "$1" >>"$probe_log"\n' + ' if [ "$scenario" = fatal ]; then echo "malformed fixed OF role" >&2; return 2; fi\n' + ' if [ "$1" = no-scratch ]; then [ "$scenario" = absent ]; return; fi\n' + ' [ "$scenario" != absent ] || return 1\n' + ' if [ "$scenario" = pending ] && [ "$(wc -l <"$probe_log")" -eq 1 ]; then return 1; fi\n' + " printf '/dev/vdz\\n'\n" + "}\n" + f"{functions}" + 'if [ "$action" = resolve ]; then resolve_block_device "$address"; ' + "else refuse_scratch_device; fi\n" + ) + result = subprocess.run( + [self.shell, "-s", "--", scenario, log.as_posix(), action, address], + input=script, + text=True, + capture_output=True, + check=False, + ) + return result, log.read_text().splitlines() if log.exists() else [] + + def test_arm_launcher_uses_the_packaged_rust_fixed_role_query(self): + wrapper = _shell_function(self.source, "root_device_query") + self.assertIn('/sbin/nvx-init --root-device "$1"', wrapper) + for address, role in (("0xd0003000", "distro"), ("0xd0006000", "scratch")): + result, calls = self._run_arm_role_query("ready", address=address) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, "/dev/vdz\n") + self.assertEqual(calls, [role]) + result, calls = self._run_arm_role_query("pending") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(calls, ["distro", "distro"]) + + def test_arm_role_lookup_retries_absence_but_never_fatal_discovery(self): + result, calls = self._run_arm_role_query("fatal") + self.assertEqual(result.returncode, 2) + self.assertIn("malformed fixed OF role", result.stderr) + self.assertEqual(calls, ["distro"]) + result, calls = self._run_arm_role_query("ready", address="0xd0004000") + self.assertEqual(result.returncode, 2) + self.assertEqual(calls, []) + result, calls = self._run_arm_role_query("absent") + self.assertEqual(result.returncode, 1) + self.assertEqual(len(calls), 600) + + def test_arm_ram_upper_rejects_an_unbound_or_invalid_scratch_transport(self): + result, calls = self._run_arm_role_query("absent", action="no-scratch") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(calls, ["no-scratch"]) + for scenario in ("ready", "pending", "fatal"): + result, calls = self._run_arm_role_query(scenario, action="no-scratch") + self.assertEqual(result.returncode, 125) + self.assertEqual(calls, ["no-scratch"]) + def _aggregate(self, *children: str, mode: str = "rw") -> str: """Returns the bootstrap of an aggregate whose children the CHILD,TARGET,MODE specifications `children` place.""" @@ -23362,6 +23488,50 @@ def test_guest_runner_keeps_stderr_out_of_console_lines_on_request(self): + b"NVX-GUEST-SCRIPT-START\r\n\x00\r\nNVX-LIFECYCLE-OK\r\n\x7f\xff\n", ) + def test_guest_runner_exact_status_keeps_default_and_time_failure_enforcement(self): + steps: list[tuple[benchmark.OutputStream, bytes | None]] = [ + ("console", b"DONE\n"), + ("console", None), + ("stderr", b"tail-before-eof\n"), + ("stderr", None), + ] + for status, expected, passes in [ + (42, 42, True), + (42, 0, False), + (194, 194, False), + ]: + with self.subTest(status=status, expected=expected): + interaction = _TwoStreamInteraction(steps, status, separate_stderr=True) + stderr = bytearray() + with ( + patch.object( + benchmark, "InteractiveProcess", return_value=interaction + ), + patch.object(benchmark, "_try_peak_rss", return_value=1024), + patch.object(benchmark, "terminate"), + ): + if passes: + result = benchmark.run_guest_script( + ["openvmm"], + "true\n", + b"DONE", + timeout=5, + expected_exit_status=expected, + on_stderr=stderr.extend, + ) + self.assertIn("DONE\n", result["text"]) + else: + with self.assertRaises(RuntimeError): + benchmark.run_guest_script( + ["openvmm"], + "true\n", + b"DONE", + timeout=5, + expected_exit_status=expected, + on_stderr=stderr.extend, + ) + self.assertEqual(stderr, b"tail-before-eof\n") + def test_guest_runner_reads_a_separate_stderr_to_its_end(self): # OpenVMM writes a fatal error to stderr after its console has ended. fatal = b"fatal error: failed to launch vm worker\n" diff --git a/scripts/test_time_abi.py b/scripts/test_time_abi.py index 2eb439048..22b139b06 100644 --- a/scripts/test_time_abi.py +++ b/scripts/test_time_abi.py @@ -979,6 +979,77 @@ def test_validates_boot_marker_fields_against_the_backend(self): assert mshv is not None time_abi.validate_boot_marker(mshv, backend="mshv") + def test_normal_arm_cli_uses_architected_counter_not_x86_frequency_or_lapic(self): + arm_line = BOOT_LINE.replace("tsc_hz=2194804000", "tsc_hz=19200000").replace( + "lapic_hz=1000000000", "lapic_hz=0" + ) + for profile in ("auto", "host"): + command = [ + "nvx-rs.exe", + "--root", + "repository", + "run", + "--architecture", + "aarch64", + "--hypervisor", + "whp", + "--cpu-profile", + profile, + ] + monitor = TimeAbiMonitor(command) + self.assertTrue(monitor.cold_boot) + monitor.feed(arm_line.encode() + STATUS_OK.encode()) + self.assertEqual( + monitor.require_boot("qualification", online_cpus=4)["tsc_hz"], + "19200000", + ) + fields = time_abi.parse_marker(arm_line) + assert fields is not None + with patch.object(time_abi.platform, "machine", return_value="ARM64"): + for profile in ("auto", "host"): + native = TimeAbiMonitor(["nvx-rs.exe", "run", "--cpu-profile", profile]) + native.feed(arm_line.encode() + STATUS_OK.encode()) + self.assertEqual( + native.require_boot("native omitted ISA")["tsc_hz"], "19200000" + ) + report_only = TimeAbiMonitor(["nvx-rs.exe", "run"]) + with self.assertRaisesRegex(TimeAbiFailure, "report-only"): + report_only.feed( + arm_line.replace("NVX-TIME-ABI:", "NVX-TIME-REPORT:").encode() + + STATUS_OK.encode() + ) + mixed = TimeAbiMonitor(["nvx-rs.exe", "run"]) + mixed.feed(arm_line.encode()) + with self.assertRaisesRegex(TimeAbiFailure, "report-only"): + mixed.feed( + arm_line.replace("NVX-TIME-ABI:", "NVX-TIME-REPORT:").encode() + + STATUS_OK.encode() + ) + for frequency in ("1", "24000000", "4294967295"): + time_abi.validate_boot_marker( + {**fields, "tsc_hz": frequency}, backend="kvm", architecture="aarch64" + ) + for frequency in ("0", "-1", "4294967296", "fast"): + with self.assertRaises(TimeAbiFailure): + time_abi.validate_boot_marker( + {**fields, "tsc_hz": frequency}, + backend="whp", + architecture="aarch64", + ) + with self.assertRaises(TimeAbiFailure): + time_abi.validate_boot_marker( + {**fields, "lapic_hz": "200000000"}, + backend="whp", + architecture="aarch64", + ) + with self.assertRaises(TimeAbiFailure): + time_abi.validate_boot_marker(fields, backend="whp") + self.assertFalse( + time_abi.is_cold_boot_command( + ["nvx-rs", "run", "--restore-snapshot", "snapshot"] + ) + ) + def test_validates_restore_marker_fields_against_the_backend(self): restore = time_abi.parse_marker(RESTORE_LINE) assert restore is not None @@ -2554,18 +2625,22 @@ def test_the_rate_limit_is_the_guests_frequency_budget(self): encoding="utf-8" ) ) - scaled_ppm = decoders["SCALED_PPM"] + shared = rust_integer_constants( + (root / "nvx-rs" / "src" / "guest" / "time" / "sample.rs").read_text( + encoding="utf-8" + ) + ) + scaled_ppm = shared["SCALED_PPM"] self.assertEqual(scaled_ppm, 1 << 16) self.assertEqual( - decoders["FREQUENCY_LIMIT"], doctor.GUEST_FREQUENCY_LIMIT_PPM * scaled_ppm + shared["FREQUENCY_LIMIT"], doctor.GUEST_FREQUENCY_LIMIT_PPM * scaled_ppm ) self.assertEqual( decoders["RATE_DEVIATION_LIMIT"], doctor.RESTORE_TOLERANCE_PPM * scaled_ppm ) self.assertEqual( doctor.RATE_TOLERANCE_PPM, - (decoders["FREQUENCY_LIMIT"] - decoders["RATE_DEVIATION_LIMIT"]) - / scaled_ppm, + (shared["FREQUENCY_LIMIT"] - decoders["RATE_DEVIATION_LIMIT"]) / scaled_ppm, ) rate = rust_integer_constants( pinned_openvmm_file(self, "vmm_core/virt/src/time_abi/rate.rs") diff --git a/ubuntu/packages-arm64.lock.json b/ubuntu/packages-arm64.lock.json new file mode 100644 index 000000000..310dd75ce --- /dev/null +++ b/ubuntu/packages-arm64.lock.json @@ -0,0 +1,113 @@ +{ + "format": 1, + "release": "26.04.1", + "codename": "resolute", + "architecture": "arm64", + "packages": [ + { + "name": "busybox-static", + "source_name": "busybox", + "source_version": "1:1.37.0-7ubuntu1", + "version": "1:1.37.0-7ubuntu1", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/b/busybox/busybox-static_1.37.0-7ubuntu1_arm64.deb", + "sha256": "fcf50794faf920455895d7ef103d1c54b9ebf7f18d2e52982dbf3903b7769592", + "license_path": "/usr/share/doc/busybox-static/copyright", + "required_by": ["nvx-initramfs"], + "depends": "", + "pre_depends": "", + "component": "main", + "suite": "resolute" + }, + { + "name": "iputils-ping", + "source_name": "iputils", + "source_version": "3:20250605-1ubuntu1", + "version": "3:20250605-1ubuntu1", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/i/iputils/iputils-ping_20250605-1ubuntu1_arm64.deb", + "sha256": "c3d5de927ab87519f79d36a85fe4d6fd35fb198fa283586da899617b50ea3374", + "license_path": "/usr/share/doc/iputils-ping/copyright", + "required_by": ["nvx-initramfs"], + "depends": "libc6 (>= 2.38), libcap2 (>= 1:2.10), libidn2-0 (>= 0.6)", + "pre_depends": "", + "component": "main", + "suite": "resolute" + }, + { + "name": "libcap2", + "source_name": "libcap2", + "source_version": "1:2.75-10ubuntu2", + "version": "1:2.75-10ubuntu2", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/libc/libcap2/libcap2_2.75-10ubuntu2_arm64.deb", + "sha256": "52c249af074c215f6372f9cd911ae70c63a8bddefdc1aca80350b47d39888829", + "license_path": "/usr/share/doc/libcap2/copyright", + "required_by": ["iputils-ping"], + "depends": "libc6 (>= 2.38)", + "pre_depends": "", + "component": "main", + "suite": "resolute" + }, + { + "name": "libidn2-0", + "source_name": "libidn2", + "source_version": "2.3.8-4build1", + "version": "2.3.8-4build1", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/libi/libidn2/libidn2-0_2.3.8-4build1_arm64.deb", + "sha256": "8c0daa227474b787790f62fd15c9ad05890e22a66c2eaeabee12b4ccdb5102c6", + "license_path": "/usr/share/doc/libidn2-0/copyright", + "required_by": ["iputils-ping"], + "depends": "libc6 (>= 2.17), libunistring5 (>= 1.1)", + "pre_depends": "", + "component": "main", + "suite": "resolute" + }, + { + "name": "libunistring5", + "source_name": "libunistring", + "source_version": "1.3-2build1", + "version": "1.3-2build1", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/libu/libunistring/libunistring5_1.3-2build1_arm64.deb", + "sha256": "1a73d3d7690e26f926d7af025b44d6d57c2dd1e0abb35c0d5b1b11f4b9b7f4da", + "license_path": "/usr/share/doc/libunistring5/copyright", + "required_by": ["libidn2-0"], + "depends": "libc6 (>= 2.34)", + "pre_depends": "", + "component": "main", + "suite": "resolute" + }, + { + "name": "net-tools", + "source_name": "net-tools", + "source_version": "2.10-2ubuntu1", + "version": "2.10-2ubuntu1", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/n/net-tools/net-tools_2.10-2ubuntu1_arm64.deb", + "sha256": "5fcc9f1738cbecaa0b461bbf36f9c586373d7196d618ae4835faef8f40ea2464", + "license_path": "/usr/share/doc/net-tools/copyright", + "required_by": ["nvx-initramfs"], + "depends": "libc6 (>= 2.38), libselinux1 (>= 3.1~)", + "pre_depends": "", + "component": "main", + "suite": "resolute" + }, + { + "name": "netcat-openbsd", + "source_name": "netcat-openbsd", + "source_version": "1.234-1", + "version": "1.234-1", + "architecture": "arm64", + "url": "https://ports.ubuntu.com/ubuntu-ports/pool/main/n/netcat-openbsd/netcat-openbsd_1.234-1_arm64.deb", + "sha256": "1a5ff7885e6aef6d183eadd64a50f0dcac6315007d21b50552888dff29c75f0e", + "license_path": "/usr/share/doc/netcat-openbsd/copyright", + "required_by": ["nvx-initramfs"], + "depends": "libbsd0 (>= 0.2.0), libc6 (>= 2.38)", + "pre_depends": "", + "component": "main", + "suite": "resolute" + } + ] +} From 6bf9e1e2356b4a0272e5a36cc809bfc0e53ae356 Mon Sep 17 00:00:00 2001 From: Gautam Ahuja Date: Thu, 12 Nov 2026 12:11:21 -0800 Subject: [PATCH 2/3] arm64: preserve architecture in source export and time checks Route Linux corresponding-source packaging through the selected kernel architecture for both its patched sources and generated configuration. Keep the x86 default and explicit configuration override. Recognize attached option values in the shared time-ABI command reader so an explicit guest architecture never falls back to the Python host ISA. Cover both spellings and retain counter validation. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- doc/usage.md | 11 +-- docker/Dockerfile | 2 +- .../nvx_tools/create_linux_source_archive.py | 12 +++- scripts/nvx_tools/time_abi.py | 6 +- scripts/test_guest_binaries.py | 69 +++++++++++++++++++ scripts/test_time_abi.py | 45 ++++++++++++ 6 files changed, 135 insertions(+), 10 deletions(-) diff --git a/doc/usage.md b/doc/usage.md index bbe0c1569..aa837d9ed 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -1892,14 +1892,17 @@ authenticates live or historical Ubuntu source indexes through signed ```text python3 scripts/nvx.py create-linux-source-archive - --config PATH + [--architecture {x86_64,aarch64}] + [--config PATH] --output PATH ``` Creates the deterministic Linux corresponding-source archive from the pinned -kernel inputs and the generated kernel configuration. `--config` must name an -existing generated kernel configuration, and `--output` names the archive to -create. +kernel inputs and the generated kernel configuration. `--architecture` defaults +to `x86_64` and selects the matching kernel patch set and build directory. +`--config` overrides the generated configuration path, which defaults to +`build/linux/.config` for x86 and `build/aarch64/linux/.config` for ARM64; the +selected file must already exist. `--output` names the archive to create. ### `package` diff --git a/docker/Dockerfile b/docker/Dockerfile index 566a3460b..8dab0ec59 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -333,7 +333,7 @@ COPY --from=azurelinux-initramfs /tmp/initramfs-azurelinux.cpio.gz.packages.json FROM kernel AS linux-source-archive RUN mkdir -p /out \ && python3 scripts/nvx.py create-linux-source-archive \ - --config /repo/build/linux/.config \ + --architecture "$NVX_ARCHITECTURE" \ --output /out/nvx-linux-source-6.18.38.tar.gz FROM scratch AS artifacts diff --git a/scripts/nvx_tools/create_linux_source_archive.py b/scripts/nvx_tools/create_linux_source_archive.py index 188806416..ba5de8f2e 100644 --- a/scripts/nvx_tools/create_linux_source_archive.py +++ b/scripts/nvx_tools/create_linux_source_archive.py @@ -15,18 +15,24 @@ def configure_parser(parser: argparse.ArgumentParser) -> None: - parser.add_argument("--config", type=Path, required=True) + parser.add_argument( + "--architecture", choices=("x86_64", "aarch64"), default="x86_64" + ) + parser.add_argument("--config", type=Path) parser.add_argument("--output", type=Path, required=True) parser.set_defaults(handler=command_create_linux_source_archive) def command_create_linux_source_archive(args: argparse.Namespace) -> None: - source, _ = prepare_kernel_source(KernelBuildConfig()) - generated_config = args.config.resolve() + kernel = KernelBuildConfig.for_architecture(args.architecture) + generated_config = ( + args.config or kernel.work / KernelBuildConstants.BUILD_CONFIG_NAME + ).resolve() if not generated_config.is_file(): raise FileNotFoundError( f"generated kernel config not found: {generated_config}" ) + source, _ = prepare_kernel_source(kernel) output = args.output.resolve() output.parent.mkdir(parents=True, exist_ok=True) root = KernelBuildConstants.SOURCE_ARCHIVE_ROOT diff --git a/scripts/nvx_tools/time_abi.py b/scripts/nvx_tools/time_abi.py index 57543c04b..0d3e97b1d 100644 --- a/scripts/nvx_tools/time_abi.py +++ b/scripts/nvx_tools/time_abi.py @@ -567,9 +567,11 @@ def describe_exit_status(returncode: int | None) -> str | None: def _command_option(command: Sequence[str], option: str) -> str | None: - for index, argument in enumerate(command[:-1]): - if argument == option: + for index, argument in enumerate(command): + if argument == option and index + 1 < len(command): return command[index + 1] + if argument.startswith(f"{option}="): + return argument[len(option) + 1 :] return None diff --git a/scripts/test_guest_binaries.py b/scripts/test_guest_binaries.py index 361af656a..28826d44b 100644 --- a/scripts/test_guest_binaries.py +++ b/scripts/test_guest_binaries.py @@ -21,6 +21,7 @@ build, build_config, collect_ubuntu_sources, + create_linux_source_archive, elf, guest_binaries, ubuntu, @@ -28,6 +29,7 @@ from nvx_tools.build_constants import ( # noqa: E402 BuildConstants, GuestRustBuildConstants, + KernelBuildConstants, ) @@ -1050,6 +1052,68 @@ def test_source_identity_command_prints_the_recorded_source_digest(self): class GuestBinaryArchitectureTests(unittest.TestCase): + def test_linux_source_archive_uses_the_selected_architecture_and_config(self): + for architecture in ("x86_64", "aarch64"): + for explicit_config in (False, True): + with ( + self.subTest( + architecture=architecture, explicit_config=explicit_config + ), + tempfile.TemporaryDirectory() as temporary, + ): + root = Path(temporary).resolve() + kernel = build_config.KernelBuildConfig( + architecture=architecture, work=root / architecture / "linux" + ) + config = ( + root / "custom.config" + if explicit_config + else kernel.work / KernelBuildConstants.BUILD_CONFIG_NAME + ) + config.parent.mkdir(parents=True, exist_ok=True) + config.write_text("CONFIG_64BIT=y\n", encoding="utf-8") + output = root / "out" / "linux.tar.gz" + argv = ["create-linux-source-archive", "--output", str(output)] + if architecture == "aarch64": + argv.extend(["--architecture", architecture]) + if explicit_config: + argv.extend(["--config", str(config)]) + args = nvx.parse_args(argv) + with ( + patch.object( + create_linux_source_archive.KernelBuildConfig, + "for_architecture", + return_value=kernel, + ) as select, + patch.object( + create_linux_source_archive, + "prepare_kernel_source", + return_value=(root / "source", "fingerprint"), + ) as prepare, + patch.object( + create_linux_source_archive, "create_reproducible_tar_gz" + ) as archive, + contextlib.redirect_stdout(io.StringIO()), + ): + args.handler(args) + select.assert_called_once_with(architecture) + prepare.assert_called_once_with(kernel) + self.assertEqual(archive.call_args.args[0], output) + archive_root = KernelBuildConstants.SOURCE_ARCHIVE_ROOT + self.assertEqual( + archive.call_args.args[1][:2], + ( + ( + root / "source", + f"{archive_root}/{KernelBuildConstants.SOURCE_NAME}", + ), + ( + config, + f"{archive_root}/{KernelBuildConstants.CONFIG_NAME}", + ), + ), + ) + def test_common_guest_build_cli_selects_one_architecture_for_every_producer(self): args = nvx.parse_args( [ @@ -1220,6 +1284,11 @@ def test_docker_architecture_input_reuses_the_shared_producer_and_export(self): "COPY --from=guest-binaries /out/guest-binaries/ /", dockerfile, ) + source = dockerfile.split("FROM kernel AS linux-source-archive", 1)[1].split( + "\nFROM ", 1 + )[0] + self.assertIn('--architecture "$NVX_ARCHITECTURE"', source) + self.assertNotIn("--config", source) def test_explicit_target_resolution_and_legacy_manifest(self): for architecture, target in GuestRustBuildConstants.TARGETS.items(): diff --git a/scripts/test_time_abi.py b/scripts/test_time_abi.py index 22b139b06..1d90ed938 100644 --- a/scripts/test_time_abi.py +++ b/scripts/test_time_abi.py @@ -1050,6 +1050,51 @@ def test_normal_arm_cli_uses_architected_counter_not_x86_frequency_or_lapic(self ) ) + def test_explicit_counter_architecture_accepts_both_option_spellings(self): + arm_line = BOOT_LINE.replace("tsc_hz=2194804000", "tsc_hz=19200000").replace( + "lapic_hz=1000000000", "lapic_hz=0" + ) + for native, architecture, line in ( + ("x86_64", "aarch64", arm_line), + ("ARM64", "x86_64", BOOT_LINE), + ): + for options in ( + ["--hypervisor", "kvm", "--architecture", architecture], + ["--hypervisor=kvm", f"--architecture={architecture}"], + ): + with ( + self.subTest(native=native, options=options), + patch.object(time_abi.platform, "machine", return_value=native), + ): + monitor = TimeAbiMonitor(["nvx-rs", "run", *options]) + self.assertEqual(monitor.architecture, architecture) + self.assertEqual(monitor.backend, "kvm") + monitor.feed(line.encode() + STATUS_OK.encode()) + self.assertEqual( + monitor.require_boot("explicit ISA", online_cpus=4)["tsc_hz"], + "19200000" if architecture == "aarch64" else "2194804000", + ) + rejected = TimeAbiMonitor(["nvx-rs", "run", *options]) + with self.assertRaises(TimeAbiFailure): + rejected.feed( + line.replace("lapic_hz=", "lapic_hz=1").encode() + + STATUS_OK.encode() + ) + + def test_option_reader_matches_exact_names_and_keeps_native_defaults(self): + for options in ([], ["--architecture-extra=aarch64"], ["--architecture"]): + with ( + self.subTest(options=options), + patch.object(time_abi.platform, "machine", return_value="x86_64"), + ): + self.assertEqual( + TimeAbiMonitor(["nvx-rs", "run", *options]).architecture, "x86_64" + ) + self.assertEqual( + TimeAbiMonitor(["openvmm", "--kernel=build/aarch64/Image"]).architecture, + "aarch64", + ) + def test_validates_restore_marker_fields_against_the_backend(self): restore = time_abi.parse_marker(RESTORE_LINE) assert restore is not None From 3103f24c292f82907c77dda5cd9d8c105b608971 Mon Sep 17 00:00:00 2001 From: Gautam Ahuja Date: Thu, 12 Nov 2026 12:43:43 -0800 Subject: [PATCH 3/3] arm64: lock kernel downloads and validate restore options Serialize the two Docker kernel producers that share the fixed download staging path. Retain the shared cache and architecture defaults. Use the shared option reader to classify split and attached restore options consistently. Validate the latest restore status rather than accepting a saved boot record, with regressions for missing records, generation, and counter rates. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- docker/Dockerfile | 4 +-- scripts/nvx_tools/time_abi.py | 6 +++-- scripts/test_guest_binaries.py | 18 +++++++++++++ scripts/test_time_abi.py | 46 ++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 8dab0ec59..54cff2ebf 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -65,7 +65,7 @@ COPY scripts/ /repo/scripts/ COPY LICENSE README.md SOURCE-MANIFEST.json THIRD_PARTY_NOTICES.md /repo/ FROM base AS kernel -RUN --mount=type=cache,target=/repo/.cache/downloads \ +RUN --mount=type=cache,target=/repo/.cache/downloads,sharing=locked \ python3 scripts/nvx.py build-kernel --architecture "$NVX_ARCHITECTURE" FROM base AS kernel-debug @@ -74,7 +74,7 @@ RUN python3 scripts/nvx.py build-kernel --debug # The kernel configuration and its derived record, generated from the feature # inventory with the toolchain that builds the kernel (doc/build.md). FROM base AS kernel-config -RUN --mount=type=cache,target=/repo/.cache/downloads \ +RUN --mount=type=cache,target=/repo/.cache/downloads,sharing=locked \ python3 scripts/nvx.py kernel-config --native --architecture "$NVX_ARCHITECTURE" # Only the guest images install the Rust guest binaries and record their sources, diff --git a/scripts/nvx_tools/time_abi.py b/scripts/nvx_tools/time_abi.py index 0d3e97b1d..b9a966b5e 100644 --- a/scripts/nvx_tools/time_abi.py +++ b/scripts/nvx_tools/time_abi.py @@ -580,7 +580,9 @@ def is_cold_boot_command(command: Sequence[str]) -> bool: cli_run = ( bool(command) and Path(command[0]).stem == "nvx-rs" and "run" in command[1:] ) - return ("--kernel" in command or cli_run) and "--restore-snapshot" not in command + return ("--kernel" in command or cli_run) and _command_option( + command, "--restore-snapshot" + ) is None def _counter_architecture(command: Sequence[str]) -> str: @@ -706,7 +708,7 @@ def __init__(self, command: Sequence[str] = ()) -> None: self.backend = _command_option(command, "--hypervisor") self.architecture = _counter_architecture(command) self.cold_boot = is_cold_boot_command(command) - self.restored = "--restore-snapshot" in command + self.restored = _command_option(command, "--restore-snapshot") is not None self.boot: dict[str, str] | None = None self.restores: list[dict[str, str]] = [] self.runtime: dict[str, str] | None = None diff --git a/scripts/test_guest_binaries.py b/scripts/test_guest_binaries.py index 28826d44b..714377e22 100644 --- a/scripts/test_guest_binaries.py +++ b/scripts/test_guest_binaries.py @@ -1290,6 +1290,24 @@ def test_docker_architecture_input_reuses_the_shared_producer_and_export(self): self.assertIn('--architecture "$NVX_ARCHITECTURE"', source) self.assertNotIn("--config", source) + def test_kernel_producers_lock_the_shared_docker_download_cache(self): + dockerfile = (BuildConstants.REPO_ROOT / "docker" / "Dockerfile").read_text( + encoding="utf-8" + ) + for stage in ("kernel", "kernel-config"): + with self.subTest(stage=stage): + body = dockerfile.split(f"FROM base AS {stage}\n", 1)[1].split( + "\nFROM ", 1 + )[0] + mount = re.search(r"^RUN --mount=(\S+)", body, flags=re.MULTILINE) + assert mount is not None + options = dict( + option.split("=", 1) for option in mount.group(1).split(",") + ) + self.assertEqual(options.get("type"), "cache") + self.assertEqual(options.get("target"), "/repo/.cache/downloads") + self.assertEqual(options.get("sharing"), "locked") + def test_explicit_target_resolution_and_legacy_manifest(self): for architecture, target in GuestRustBuildConstants.TARGETS.items(): with self.subTest(architecture=architecture): diff --git a/scripts/test_time_abi.py b/scripts/test_time_abi.py index 1d90ed938..1d0a31a0e 100644 --- a/scripts/test_time_abi.py +++ b/scripts/test_time_abi.py @@ -780,6 +780,52 @@ def test_classifies_commands(self): self.assertEqual(restore.backend, "mshv") self.assertFalse(restore.cold_boot) + def test_restore_option_spellings_require_valid_latest_restore_status(self): + boot = BOOT_LINE.replace("lapic_hz=1000000000", "lapic_hz=200000000") + restored = RESTORE_LINE.replace("lapic_hz=1000000000", "lapic_hz=200000000") + for executable in (["openvmm"], ["nvx-rs.exe", "run"]): + for option in ( + ["--restore-snapshot", "snapshot"], + ["--restore-snapshot=snapshot"], + ): + command = [ + *executable, + "--architecture=x86_64", + "--hypervisor=mshv", + *option, + ] + with self.subTest(command=command, status="valid"): + monitor = TimeAbiMonitor(command) + monitor.feed((boot + restored + STATUS_OK).encode()) + self.assertFalse(monitor.cold_boot) + self.assertTrue(monitor.restored) + self.assertEqual( + monitor.require_restore( + "restored guest", online_cpus=4, generation=2 + )["generation"], + "2", + ) + for invalid, message in ( + ("", "did not report an NVX-TIME-ABI restore marker"), + ( + restored.replace("generation=2", "generation=0"), + "not 1 or later after a restore", + ), + ( + restored.replace("lapic_hz=200000000", "lapic_hz=1000000000"), + "is not the mshv rate", + ), + ( + restored.replace("tsc_hz=2194804000", "tsc_hz=400000000"), + "outside 500 MHz", + ), + ): + with self.subTest(command=command, status=message): + monitor = TimeAbiMonitor(command) + monitor.feed((boot + invalid).encode()) + with self.assertRaisesRegex(TimeAbiFailure, message): + monitor.feed(STATUS_OK.encode()) + def test_records_markers_across_chunk_boundaries(self): monitor = TimeAbiMonitor(KVM_BOOT) data = (