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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/hal/src/arch/aarch64/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ core::arch::global_asm!(
" stp q26, q27, [sp, #0x2c0]",
" stp q28, q29, [sp, #0x2e0]",
" stp q30, q31, [sp, #0x300]",
" ldr x0, [sp, #0x40]",
" ldp x1, x2, [sp, #0x00]",
" ldp x3, x4, [sp, #0x10]",
" ldp x5, x6, [sp, #0x20]",
" bl aarch64_handle_sync_lower",
" str x0, [sp, #0x00]",
" ldp q0, q1, [sp, #0x120]",
" ldp q2, q3, [sp, #0x140]",
" ldp q4, q5, [sp, #0x160]",
Expand Down
10 changes: 5 additions & 5 deletions crates/kernel/src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
//! primitive plus a seeded-stack spawn path). The `usermode` submodule
//! provides a smoke-only EL1->EL0 transition on a dedicated 4KiB RX code
//! page, followed by an invalid guard page and four RW/NX stack pages. Its
//! SVC #7 canary and SVC #8 sequence proves a real exception-vector `eret`
//! back to EL0 (see [`jump_to_el0`]); it performs no syscall dispatch. The
//! `clone`, `init_embed`, and `syscall_entry` submodules remain aarch64 build
//! stubs that mirror the x86_64 public API so architecture-neutral kernel
//! code type-checks.
//! SVC #7 canary, Linux AArch64 `getpid(2)` SVC #0, and SVC #8 sequence proves
//! a real exception-vector `eret` back to EL0 (see [`jump_to_el0`]). Only this
//! dedicated getpid smoke is dispatched; the `clone`, `init_embed`, and
//! `syscall_entry` submodules remain aarch64 build stubs that mirror the x86_64
//! public API so architecture-neutral kernel code type-checks.
//!
//! [`switch_context`]: context::switch_context
//! [`jump_to_el0`]: usermode::jump_to_el0
Expand Down
6 changes: 3 additions & 3 deletions crates/kernel/src/arch/aarch64/syscall_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

//! AArch64 SYSCALL-entry saved-register mirrors.
//!
//! aarch64 build stub — not yet functional.
//! aarch64 fork/exec/signal register-state stub — not yet functional.
//!
//! Provides the saved-user-register atomics and accessors that
//! architecture-neutral fork/exec/signal dispatch reads, mirroring
//! [`crate::arch::x86_64::syscall_entry`]. The atomics themselves are real
//! (they are plain shared storage); what is missing is the aarch64 `svc`
//! exception vector that would populate them on a real syscall entry.
//! plain storage, but the live lower-EL `getpid` smoke does not populate them;
//! full fork/exec/signal syscall entry remains unwired.

use core::sync::atomic::{AtomicU64, Ordering};

Expand Down
45 changes: 34 additions & 11 deletions crates/kernel/src/arch/aarch64/usermode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
//! SIMD registers plus `FPCR`/`FPSR` across its first SVC round trip.
//!
//! [`el0_test_entry`] verifies a stack canary, seeds SIMD/FP state, and issues
//! `svc #7`. That handler deliberately overwrites the seeded state. The payload
//! issues `svc #8` only after the exception vector restores the state and
//! returns to EL0, proving the complete round trip without syscall dispatch.
//! `svc #7`. After the vector restores that state, the payload invokes Linux
//! AArch64 `getpid(2)` through `svc #0`, checks its returned `x0`, and issues
//! `svc #8` to report the complete round trip.

use oncrix_hal::arch::aarch64::pl011::{PL011_BASE, Pl011};
use oncrix_hal::serial::SerialPort;
Expand All @@ -24,7 +24,7 @@ use oncrix_hal::serial::SerialPort;
/// - `M[4:0] = 0b00000` -> return to EL0 using `SP_EL0` (EL0t).
/// - `DAIF` (bits `[9:6]`) all set -> Debug, SError, IRQ, and FIQ masked.
///
/// Masking is deliberate for the smoke test so the two SVCs are the only
/// Masking is deliberate for the smoke test so its synchronous SVCs are the only
/// exceptions the round-trip depends on. `PSTATE.I` at EL0 does *not* mask a
/// physical IRQ that targets EL1 (a higher EL), so the caller additionally
/// disarms the generic timer before descending. A real userspace launch would
Expand Down Expand Up @@ -222,13 +222,20 @@ pub unsafe fn jump_to_el0(entry: u64, user_sp: u64) -> ! {

/// Synchronous-exception handler for a *lower* Exception Level (EL0).
///
/// The lower-EL synchronous vector calls this function with a saved trap
/// frame. SVC #7 reports the validated stack canary and deliberately overwrites
/// representative SIMD/FP state. SVC #8 reports that the vector restored that
/// state and returned to EL0. A verification failure, unexpected SVC, or other
/// lower-EL exception reports diagnostics and fail-stops at EL1.
/// The lower-EL synchronous vector passes the saved user `x8` syscall number
/// followed by `x0..x5`. SVC #0 dispatches the Linux AArch64 syscall ABI and
/// returns its result for writeback to saved user `x0`. SVC #7 and SVC #8
/// retain the smoke-test control path. Any other exception fail-stops at EL1.
#[unsafe(no_mangle)]
pub extern "C" fn aarch64_handle_sync_lower() {
pub extern "C" fn aarch64_handle_sync_lower(
native_number: u64,
arg0: u64,
arg1: u64,
arg2: u64,
arg3: u64,
arg4: u64,
arg5: u64,
) -> u64 {
let esr: u64;
// SAFETY: `mrs` from `ESR_EL1` reads the Exception Syndrome Register,
// which is always accessible at EL1 and has no side effects. It still
Expand Down Expand Up @@ -261,6 +268,13 @@ pub extern "C" fn aarch64_handle_sync_lower() {
// ESR_EL1.ISS[15:0] contains the immediate from the trapped SVC.
let imm = (esr & 0xFFFF) as u16;
match imm {
0 => {
let result = oncrix_syscall::aarch64::dispatch_from_trap_registers(
native_number,
[arg0, arg1, arg2, arg3, arg4, arg5],
);
u64::from_ne_bytes(result.to_ne_bytes())
}
7 => {
let _ = serial.write_str("[ONCRIX/aarch64] EL0 stack canary verified\n");
// Deliberately destroy every state item seeded by the EL0 payload.
Expand All @@ -282,12 +296,15 @@ pub extern "C" fn aarch64_handle_sync_lower() {
options(nostack),
);
}
arg0
}
8 => {
let _ = serial.write_str(
"[ONCRIX/aarch64] EL0 SIMD/FP state preserved and round-trip verified\n",
);
let _ = serial.write_str("[ONCRIX/aarch64] EL0 getpid syscall round trip verified\n");
let _ = serial.write_str("[ONCRIX/aarch64] EL0 round trip verified\n");
arg0
}
SVC_STACK_FAILURE => {
let _ = serial.write_str("[ONCRIX/aarch64] EL0 stack/SIMD/FP verification FAILED\n");
Expand Down Expand Up @@ -317,7 +334,8 @@ fn fail_stop() -> ! {
/// or literal pool. It verifies a stack canary, seeds both 64-bit lanes of
/// `q0`, `q15`, and `q31` plus nonzero valid `FPCR`/`FPSR` values, then traps
/// with SVC #7. It compares every seeded value after `eret`; only complete
/// restoration reaches SVC #8. A mismatch uses the existing failure SVC.
/// restoration reaches the `getpid` SVC. Only a zero result reaches SVC #8; a
/// mismatch uses the existing failure SVC.
#[unsafe(no_mangle)]
#[unsafe(naked)]
#[unsafe(link_section = ".text.user")]
Expand Down Expand Up @@ -393,10 +411,15 @@ pub extern "C" fn el0_test_entry() -> ! {
"movk x9, #0x0800, lsl #16",
"cmp x9, x10",
"b.ne 1f",
"mov x0, #1",
"mov x8, #{getpid}",
"svc #0",
"cbnz x0, 1f",
"svc #8",
"b .",
"1:",
"svc #0xbad",
"b .",
getpid = const oncrix_syscall::aarch64::LINUX_SYS_GETPID,
);
}
11 changes: 5 additions & 6 deletions crates/kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,9 @@ pub extern "C" fn kernel_main() -> ! {
// Prove the EL1->EL0 transition on a dedicated 4KiB RX code page,
// followed by an invalid guard page and four RW/NX stack pages.
// The EL0 payload issues an SVC #7 canary, resumes after the vector's
// `eret`, then issues SVC #8 to prove a real EL0->EL1->EL0 round-trip.
// The handler is smoke-only and performs no syscall dispatch. After
// the proof EL0 idles, so this is the last action on the aarch64 path.
// `eret`, dispatches Linux AArch64 getpid through SVC #0, validates the
// returned x0, then issues SVC #8. After the proof EL0 idles, so this is
// the last action on the aarch64 path.
{
use oncrix_hal::arch::aarch64::timer::AArch64Timer;
use oncrix_hal::timer::Timer;
Expand All @@ -979,9 +979,8 @@ pub extern "C" fn kernel_main() -> ! {
let mut timer = AArch64Timer::new();
let _ = timer.stop();

let _ = serial.write_str(
"[ONCRIX/aarch64] entering EL0 (dedicated-page smoke; no syscall dispatch)...\n",
);
let _ = serial
.write_str("[ONCRIX/aarch64] entering EL0 (dedicated-page getpid smoke)...\n");

// SAFETY: the boot tables map `EL0_ENTRY_VA` as the dedicated 4KiB
// RX code page and `EL0_STACK_TOP_VA` above four RW/NX stack pages,
Expand Down
99 changes: 99 additions & 0 deletions crates/syscall/src/aarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2026 ONCRIX Contributors
// SPDX-License-Identifier: Apache-2.0

//! Linux AArch64 syscall ABI normalization.

use crate::dispatch::SyscallArgs;
use crate::handler::SyscallResult;

/// Linux AArch64 syscall number for `getpid(2)`.
pub const LINUX_SYS_GETPID: u64 = 172;

fn normalize_trap_registers(native_number: u64, args: [u64; 6]) -> Option<SyscallArgs> {
match native_number {
LINUX_SYS_GETPID => Some(SyscallArgs {
number: crate::number::SYS_GETPID,
arg0: args[0],
arg1: args[1],
arg2: args[2],
arg3: args[3],
arg4: args[4],
arg5: args[5],
}),
_ => None,
}
}

/// Dispatch a syscall captured from Linux AArch64 trap registers.
pub fn dispatch_from_trap_registers(native_number: u64, args: [u64; 6]) -> SyscallResult {
match normalize_trap_registers(native_number, args) {
Some(normalized) => crate::dispatch::dispatch(&normalized),
None => -i64::from(crate::libc::ENOSYS),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn trap_registers_map_linux_getpid_to_shared_args() {
// Given
let native_args = [11, 22, 33, 44, 55, 66];

// When
let normalized = normalize_trap_registers(LINUX_SYS_GETPID, native_args);

// Then
assert!(normalized.is_some(), "AArch64 getpid must be normalized");
let normalized = normalized.unwrap();
assert_eq!(normalized.number, crate::number::SYS_GETPID);
assert_eq!(
[
normalized.arg0,
normalized.arg1,
normalized.arg2,
normalized.arg3,
normalized.arg4,
normalized.arg5,
],
native_args
);
}

#[test]
fn x86_getpid_number_is_not_accepted_as_aarch64() {
// Given
let x86_getpid_number = crate::number::SYS_GETPID;

// When
let normalized = normalize_trap_registers(x86_getpid_number, [0; 6]);

// Then
assert!(normalized.is_none());
}

#[test]
fn getpid_dispatch_returns_existing_stub_pid() {
// Given
let native_args = [1, 2, 3, 4, 5, 6];

// When
let result = dispatch_from_trap_registers(LINUX_SYS_GETPID, native_args);

// Then
assert_eq!(result, 0);
}

#[test]
fn unknown_aarch64_syscall_returns_enosys() {
// Given
let unknown_number = u64::MAX;

// When
let result = dispatch_from_trap_registers(unknown_number, [0; 6]);

// Then
assert_eq!(result, -i64::from(crate::libc::ENOSYS));
}
}
25 changes: 12 additions & 13 deletions crates/syscall/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@
use crate::handler::{self, SyscallResult};
use crate::number::*;

/// System call register frame (x86_64 SYSCALL convention).
/// Normalized system call arguments.
///
/// On `SYSCALL`, the kernel receives arguments in:
/// - RAX = syscall number
/// - RDI = arg0, RSI = arg1, RDX = arg2
/// - R10 = arg3, R8 = arg4, R9 = arg5
/// `number` uses the Linux x86_64 namespace defined by [`crate::number`].
/// Architecture entry code translates native syscall numbers into that
/// namespace before constructing this value.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct SyscallArgs {
/// Syscall number (RAX).
/// Normalized syscall number.
pub number: u64,
/// First argument (RDI).
/// First argument.
pub arg0: u64,
/// Second argument (RSI).
/// Second argument.
pub arg1: u64,
/// Third argument (RDX).
/// Third argument.
pub arg2: u64,
/// Fourth argument (R10).
/// Fourth argument.
pub arg3: u64,
/// Fifth argument (R8).
/// Fifth argument.
pub arg4: u64,
/// Sixth argument (R9).
/// Sixth argument.
pub arg5: u64,
}

/// Dispatch a system call by number.
///
/// Returns the result to be placed in RAX on return to user space.
/// Returns the result to be placed in the architecture's syscall return register.
pub fn dispatch(args: &SyscallArgs) -> SyscallResult {
match args.number {
SYS_READ => handler::sys_read(args.arg0, args.arg1, args.arg2),
Expand Down
2 changes: 1 addition & 1 deletion crates/syscall/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//!
//! Each handler validates user-space arguments and delegates to the
//! appropriate kernel subsystem. Returns a `SyscallResult` that is
//! passed back to user space in the RAX register.
//! passed back through the architecture's syscall return register.

use oncrix_lib::Error;

Expand Down
1 change: 1 addition & 0 deletions crates/syscall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#![no_std]

pub mod aarch64;
pub mod clock;
pub mod copy_range;
pub mod dispatch;
Expand Down
7 changes: 4 additions & 3 deletions scripts/run-qemu-aarch64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
# [ONCRIX/aarch64] preemptive: thread D scheduled
# [ONCRIX/aarch64] preemptive: C and D both ran - timer preemption verified.
# [ONCRIX/aarch64] kernel SIMD/FP state preserved across timer IRQ
# [ONCRIX/aarch64] entering EL0 (dedicated-page smoke; no syscall dispatch)...
# [ONCRIX/aarch64] entering EL0 (dedicated-page getpid smoke)...
# [ONCRIX/aarch64] Transitioning to EL0...
# [debug] entry=0xc0000000 sp=0xc0006000
# SVC proof: #7 validates the canary and clobbers SIMD/FP state; #8 runs only
# after returning to EL0 and validating full-width q0/q15/q31 plus FPCR/FPSR.
# SVC proof: #7 validates the canary and clobbers SIMD/FP state; SVC #0 then
# dispatches getpid and writes its result to x0; #8 runs only after EL0 checks it.
# [ONCRIX/aarch64] EL0 stack canary verified
# [ONCRIX/aarch64] EL0 SIMD/FP state preserved and round-trip verified
# [ONCRIX/aarch64] EL0 getpid syscall round trip verified
# [ONCRIX/aarch64] EL0 round trip verified

set -euo pipefail
Expand Down
5 changes: 3 additions & 2 deletions tests/boot_tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ struct ArchBootSpec {
}

/// Mirrors `scripts/run-qemu-aarch64.sh`. Asserts the full chain: boot, GICv3,
/// cooperative switch, timer preemption, and the three FP/SIMD preservation
/// proofs across context switch, timer IRQ, and the EL0 round trip.
/// cooperative switch, timer preemption, the three FP/SIMD preservation
/// proofs, and the live EL0 getpid syscall round trip.
const AARCH64_SPEC: ArchBootSpec = ArchBootSpec {
name: "aarch64",
target: "aarch64-unknown-none",
Expand All @@ -734,6 +734,7 @@ const AARCH64_SPEC: ArchBootSpec = ArchBootSpec {
"[ONCRIX/aarch64] kernel SIMD/FP state preserved across timer IRQ",
"[ONCRIX/aarch64] EL0 stack canary verified",
"[ONCRIX/aarch64] EL0 SIMD/FP state preserved and round-trip verified",
"[ONCRIX/aarch64] EL0 getpid syscall round trip verified",
"[ONCRIX/aarch64] EL0 round trip verified",
],
failure: &["FAILED", PANIC_MARKER],
Expand Down