From f0726b51e781a4876a3562790d40fdc711e4fb69 Mon Sep 17 00:00:00 2001 From: kernalix7 Date: Tue, 18 Aug 2026 08:41:33 +0900 Subject: [PATCH 1/5] feat(syscall): add AArch64 getpid adapter --- crates/syscall/src/aarch64.rs | 99 +++++++++++++++++++++++++++++++++++ crates/syscall/src/lib.rs | 1 + 2 files changed, 100 insertions(+) create mode 100644 crates/syscall/src/aarch64.rs diff --git a/crates/syscall/src/aarch64.rs b/crates/syscall/src/aarch64.rs new file mode 100644 index 00000000..d2930aad --- /dev/null +++ b/crates/syscall/src/aarch64.rs @@ -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 { + 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)); + } +} diff --git a/crates/syscall/src/lib.rs b/crates/syscall/src/lib.rs index b8484a77..7efced4e 100644 --- a/crates/syscall/src/lib.rs +++ b/crates/syscall/src/lib.rs @@ -15,6 +15,7 @@ #![no_std] +pub mod aarch64; pub mod clock; pub mod copy_range; pub mod dispatch; From 1794afe5135d068cd96f07486fa6020f5f690718 Mon Sep 17 00:00:00 2001 From: kernalix7 Date: Tue, 18 Aug 2026 08:42:59 +0900 Subject: [PATCH 2/5] feat(kernel): dispatch AArch64 getpid --- crates/hal/src/arch/aarch64/boot.rs | 5 +++ crates/kernel/src/arch/aarch64/usermode.rs | 45 ++++++++++++++++------ tests/boot_tests/src/main.rs | 5 ++- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/crates/hal/src/arch/aarch64/boot.rs b/crates/hal/src/arch/aarch64/boot.rs index 5560344d..5a746e64 100644 --- a/crates/hal/src/arch/aarch64/boot.rs +++ b/crates/hal/src/arch/aarch64/boot.rs @@ -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]", diff --git a/crates/kernel/src/arch/aarch64/usermode.rs b/crates/kernel/src/arch/aarch64/usermode.rs index d621d548..3583774f 100644 --- a/crates/kernel/src/arch/aarch64/usermode.rs +++ b/crates/kernel/src/arch/aarch64/usermode.rs @@ -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; @@ -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 @@ -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 @@ -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. @@ -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"); @@ -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")] @@ -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, ); } diff --git a/tests/boot_tests/src/main.rs b/tests/boot_tests/src/main.rs index f1a1a9db..dfcfa3af 100644 --- a/tests/boot_tests/src/main.rs +++ b/tests/boot_tests/src/main.rs @@ -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", @@ -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], From 9b5aacf05fcbbe2a4e0df779fdfce707b85b9c92 Mon Sep 17 00:00:00 2001 From: kernalix7 Date: Tue, 18 Aug 2026 08:43:45 +0900 Subject: [PATCH 3/5] docs(syscall): generalize return registers --- crates/syscall/src/dispatch.rs | 25 ++++++++++++------------- crates/syscall/src/handler.rs | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/crates/syscall/src/dispatch.rs b/crates/syscall/src/dispatch.rs index 8a3b49ba..bb6aab42 100644 --- a/crates/syscall/src/dispatch.rs +++ b/crates/syscall/src/dispatch.rs @@ -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), diff --git a/crates/syscall/src/handler.rs b/crates/syscall/src/handler.rs index 3e9d121e..45d8601b 100644 --- a/crates/syscall/src/handler.rs +++ b/crates/syscall/src/handler.rs @@ -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; From 73c4213182a5b5d5c8963e062eccb68bde8befc6 Mon Sep 17 00:00:00 2001 From: kernalix7 Date: Tue, 18 Aug 2026 08:44:37 +0900 Subject: [PATCH 4/5] docs(kernel): describe AArch64 getpid path --- crates/kernel/src/arch/aarch64/mod.rs | 10 +++++----- crates/kernel/src/arch/aarch64/syscall_entry.rs | 6 +++--- crates/kernel/src/main.rs | 11 +++++------ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/crates/kernel/src/arch/aarch64/mod.rs b/crates/kernel/src/arch/aarch64/mod.rs index 091da4da..5e3b4c44 100644 --- a/crates/kernel/src/arch/aarch64/mod.rs +++ b/crates/kernel/src/arch/aarch64/mod.rs @@ -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 diff --git a/crates/kernel/src/arch/aarch64/syscall_entry.rs b/crates/kernel/src/arch/aarch64/syscall_entry.rs index 77d8fec6..3eb33591 100644 --- a/crates/kernel/src/arch/aarch64/syscall_entry.rs +++ b/crates/kernel/src/arch/aarch64/syscall_entry.rs @@ -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}; diff --git a/crates/kernel/src/main.rs b/crates/kernel/src/main.rs index cd2f3265..efa6bb5e 100644 --- a/crates/kernel/src/main.rs +++ b/crates/kernel/src/main.rs @@ -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; @@ -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, From 4e40cf07cef34e197466aa1b6c9f6aa34a0434cd Mon Sep 17 00:00:00 2001 From: kernalix7 Date: Tue, 18 Aug 2026 08:45:31 +0900 Subject: [PATCH 5/5] docs: update AArch64 QEMU example --- scripts/run-qemu-aarch64.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/run-qemu-aarch64.sh b/scripts/run-qemu-aarch64.sh index 5c9d39e1..529db924 100755 --- a/scripts/run-qemu-aarch64.sh +++ b/scripts/run-qemu-aarch64.sh @@ -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