aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#89 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/de220866720168bf.bundle -o de220866720168bf.bundle git fetch de220866720168bf.bundle refs/pull/89/head
Provenance
github-import 1
aien-dev/aienux
89
4bacada75a4330a0d1795e8d6eb2588bb85a9357
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":89,"url":"https://github.com/aien-dev/aienux/pull/89","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:45Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/crates/aienos-boot/src/handoff.rs b/crates/aienos-boot/src/handoff.rs index f353cd95..8e1601e8 100644 --- a/crates/aienos-boot/src/handoff.rs +++ b/crates/aienos-boot/src/handoff.rs @@ -1174,6 +1174,13 @@ fn main() -> Status { } } #[cfg(feature = "usb-keyboard")] - usb_keyboard::run(xhci, acpi_facts.mcfg, &mut screen); + usb_keyboard::run( + xhci, + acpi_facts.mcfg, + &mut screen, + summary.conventional_kb(), + el, + report.as_str(), + ); finish(screen) } diff --git a/crates/aienos-boot/src/usb_keyboard.rs b/crates/aienos-boot/src/usb_keyboard.rs index 3487c5db..90dc0e3b 100644 --- a/crates/aienos-boot/src/usb_keyboard.rs +++ b/crates/aienos-boot/src/usb_keyboard.rs @@ -10,7 +10,7 @@ use aienos_kernel::acpi::{self, EcamWindow}; use aienos_kernel::arch::aarch64::{counter_frequency_hz, counter_ticks, MmioReg}; use aienos_kernel::console::EarlyConsole; use aienos_kernel::display::Screen; -use aienos_kernel::usb::hid::{BootKeyboardDecoder, KeyEvent, TextSink}; +use aienos_kernel::usb::hid::{BootKeyboardDecoder, KeyEvent}; use aienos_kernel::usb::xhci::controller::{DmaMemory, Keyboard}; use aienos_kernel::usb::xhci::{self, PCI_COMMAND_BUS_MASTER, PCI_COMMAND_MEMORY}; use core::fmt::Write; @@ -133,7 +133,14 @@ fn enable_controller(ecam: &EcamWindow, at: &XhciLocation) -> bool { } /// Post-exit: bring up the keyboard and echo keys until Enter or timeout. -pub fn run(xhci: Option<XhciLocation>, mcfg: Option<&[u8]>, screen: &mut Option<Screen>) { +pub fn run( + xhci: Option<XhciLocation>, + mcfg: Option<&[u8]>, + screen: &mut Option<Screen>, + conventional_memory_kb: u64, + exception_level: u8, + boot_report: &str, +) { let mut line = aienos_kernel::report::ReportBuf::<160>::new(); let Some(at) = xhci else { say(screen, "\nkeyboard: unavailable (no xHCI controller)\n"); @@ -171,43 +178,81 @@ pub fn run(xhci: Option<XhciLocation>, mcfg: Option<&[u8]>, screen: &mut Option< msg, "keyboard: ready (port {port}, slot {slot}, endpoint {endpoint:#04x})" ); - let _ = write!( - msg, - "keyboard: type a line, Enter ends it ({} s)\nkeyboard_echo: ", - listen_secs() - ); + let _ = writeln!(msg, "keyboard: shell ready ({} s)", listen_secs()); say(screen, msg.as_str()); let mut decoder = BootKeyboardDecoder::new(); - let mut text = TextSink::<64>::new(); - let mut entered = false; + let mut line = [0u8; aienos_kernel::shell::LINE_CAPACITY]; + let mut length = 0usize; + let mut exit = false; let deadline = listen_secs().saturating_mul(counter_frequency_hz()); let start = counter_ticks(); - while !entered && counter_ticks().wrapping_sub(start) < deadline && !keyboard.is_halted() { + say(screen, "aienos> "); + while !exit && counter_ticks().wrapping_sub(start) < deadline && !keyboard.is_halted() { let Some(report) = keyboard.poll() else { core::hint::spin_loop(); continue; }; decoder.handle_report(&report, |event| { if event == KeyEvent::Enter { - entered = true; + say(screen, "\n"); + say(screen, "keyboard_echo: "); + say(screen, core::str::from_utf8(&line[..length]).unwrap_or("")); + say(screen, "\n"); + let mut entered = aienos_kernel::report::ReportBuf::<96>::new(); + let _ = writeln!( + entered, + "keyboard_line: {}", + core::str::from_utf8(&line[..length]).unwrap_or("") + ); + say(screen, entered.as_str()); + say(screen, "keyboard: done (enter)\n"); + let elapsed = counter_ticks().wrapping_sub(start); + let hz = counter_frequency_hz(); + let uptime_ms = if hz == 0 { + 0 + } else { + elapsed.saturating_mul(1000) / hz + }; + let input = core::str::from_utf8(&line[..length]).unwrap_or(""); + let parsed = aienos_kernel::shell::parse_line(input); + let output = aienos_kernel::shell::dispatch( + input, + &aienos_kernel::shell::ShellContext { + conventional_memory_kb, + // Read live when the command runs, not cached at shell start. + exception_level: aienos_kernel::arch::aarch64::current_el(), + report: boot_report, + uptime_ms, + }, + ); + say(screen, output.as_str()); + exit = parsed.is_some_and(|p| p.command == aienos_kernel::shell::Command::Exit); + length = 0; + if !exit { + say(screen, "aienos> "); + } + } else if event == KeyEvent::Backspace { + if length > 0 { + length -= 1; + say(screen, "\x08 \x08"); + } } else if let KeyEvent::Char(c) = event { - let mut buf = [0u8; 4]; - say(screen, c.encode_utf8(&mut buf)); - text.record(event); + if c.is_ascii() && length < line.len() { + line[length] = c as u8; + length += 1; + let mut buf = [0u8; 4]; + say(screen, c.encode_utf8(&mut buf)); + } } }); } let mut end = aienos_kernel::report::ReportBuf::<200>::new(); - let reason = match (entered, keyboard.is_halted()) { - (true, _) => "enter", + let reason = match (exit, keyboard.is_halted()) { + (true, _) => "exit", (false, true) => "endpoint halted", (false, false) => "timeout", }; - let _ = writeln!( - end, - "\nkeyboard_line: {}\nkeyboard: done ({reason})", - text.as_str() - ); + let _ = writeln!(end, "\nkeyboard: done ({reason})",); say(screen, end.as_str()); } diff --git a/crates/aienos-kernel/src/lib.rs b/crates/aienos-kernel/src/lib.rs index 13213f1d..7f481c8c 100644 --- a/crates/aienos-kernel/src/lib.rs +++ b/crates/aienos-kernel/src/lib.rs @@ -27,6 +27,7 @@ pub mod pe; pub mod recovery; pub mod report; pub mod scheduler; +pub mod shell; pub mod store; pub mod sync; pub mod timer; diff --git a/crates/aienos-kernel/src/shell.rs b/crates/aienos-kernel/src/shell.rs new file mode 100644 index 00000000..35ff1b79 --- /dev/null +++ b/crates/aienos-kernel/src/shell.rs @@ -0,0 +1,149 @@ +//! Bounded command parsing and dispatch for the early line shell. + +use crate::report::ReportBuf; +use core::fmt::Write; + +pub const LINE_CAPACITY: usize = 64; +pub const TOKEN_CAPACITY: usize = 16; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Command<'a> { + Help, + Mem, + El, + Report, + Uptime, + Exit, + Unknown(&'a str), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ParsedLine<'a> { + pub command: Command<'a>, + pub arg: Option<&'a str>, +} + +/// Parse one whitespace-delimited command and at most one bounded argument. +pub fn parse_line(line: &str) -> Option<ParsedLine<'_>> { + if line.len() > LINE_CAPACITY { + return None; + } + let mut words = line.split_ascii_whitespace(); + let first = words.next()?; + if first.len() > TOKEN_CAPACITY { + return None; + } + let arg = words.next(); + if arg.is_some_and(|s| s.len() > TOKEN_CAPACITY) || words.next().is_some() { + return None; + } + let command = match first { + "help" => Command::Help, + "mem" => Command::Mem, + "el" => Command::El, + "report" => Command::Report, + "uptime" => Command::Uptime, + "exit" => Command::Exit, + _ => Command::Unknown(first), + }; + Some(ParsedLine { command, arg }) +} + +pub struct ShellContext<'a> { + pub conventional_memory_kb: u64, + pub exception_level: u8, + pub report: &'a str, + pub uptime_ms: u64, +} + +/// Format a command result in a fixed-capacity buffer. Invalid/oversized input +/// produces a bounded diagnostic and never allocates. +pub fn dispatch(line: &str, context: &ShellContext<'_>) -> ReportBuf<2048> { + let mut out = ReportBuf::new(); + match parse_line(line) { + None => { + let _ = writeln!(out, "invalid command line"); + } + Some(ParsedLine { + command: Command::Help, + .. + }) => { + let _ = writeln!(out, "commands: help mem el report uptime exit"); + } + Some(ParsedLine { + command: Command::Mem, + .. + }) => { + let _ = writeln!( + out, + "conventional_memory_kb: {}", + context.conventional_memory_kb + ); + } + Some(ParsedLine { + command: Command::El, + .. + }) => { + let _ = writeln!(out, "EL{}", context.exception_level); + } + Some(ParsedLine { + command: Command::Report, + .. + }) => { + let _ = write!(out, "{}", context.report); + } + Some(ParsedLine { + command: Command::Uptime, + .. + }) => { + let _ = writeln!(out, "uptime_ms: {}", context.uptime_ms); + } + Some(ParsedLine { + command: Command::Exit, + .. + }) => {} + Some(ParsedLine { + command: Command::Unknown(name), + .. + }) => { + let _ = writeln!(out, "unknown command: {name}"); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_commands_and_bounded_arguments() { + assert_eq!( + parse_line("mem"), + Some(ParsedLine { + command: Command::Mem, + arg: None + }) + ); + assert_eq!(parse_line("unknown abc").unwrap().arg, Some("abc")); + assert!(parse_line(&"x".repeat(LINE_CAPACITY + 1)).is_none()); + assert!(parse_line("x 12345678901234567").is_none()); + assert!(parse_line("x a b").is_none()); + } + + #[test] + fn dispatches_report_and_diagnostics() { + let ctx = ShellContext { + conventional_memory_kb: 42, + exception_level: 1, + report: "final report\n", + uptime_ms: 7, + }; + assert!(dispatch("el", &ctx).as_str().contains("EL1")); + assert!(dispatch("mem", &ctx).as_str().contains("42")); + assert!(dispatch("report", &ctx).as_str().contains("final report")); + assert!(dispatch("wat", &ctx) + .as_str() + .contains("unknown command: wat")); + } +} diff --git a/scripts/qemu_keyboard_test.sh b/scripts/qemu_keyboard_test.sh index 28801dfc..297b8918 100755 --- a/scripts/qemu_keyboard_test.sh +++ b/scripts/qemu_keyboard_test.sh @@ -95,10 +95,17 @@ boot_once() { fi if wait_for "${boot_timeout}" "keyboard: ready" "keyboard: unavailable" "report_kind: panic" "report_kind: fault" \ && serial_has "keyboard: ready"; then - for key in "${keys[@]}" ret; do - echo "sendkey ${key}" >&3 - sleep 0.3 - done + send_line() { + local key + for key in "$@"; do echo "sendkey ${key}" >&3; sleep 0.12; done + echo "sendkey ret" >&3 + sleep 0.5 + } + send_line "${keys[@]}" + send_line h e l p + send_line e l + send_line m e m + send_line e x i t fi local deadline=$(( $(date +%s) + 90 )) while qemu_running && (( $(date +%s) < deadline )); do sleep 0.5; done @@ -127,13 +134,17 @@ check() { # description, pattern failed=1 fi } -echo "attempts ${attempt}, ${elapsed} s (commit ${commit:0:12}), sent keys: ${keys[*]} ret" +echo "attempts ${attempt}, ${elapsed} s (commit ${commit:0:12}), sent lines: abc, help, el, mem, exit" check "left firmware and entered the kernel" "kernel: alive" check "xHCI controller found before exit" "keyboard: xhci " check "keyboard attached by the AIENOS driver" "keyboard: ready" check "typed text echoed on the serial console" "keyboard_echo: ${expected}" check "line ended by Enter and reported" "keyboard_line: ${expected}\$" check "keyboard phase finished on Enter" "keyboard: done (enter)" +check "help command output" "commands: help mem el report uptime exit" +check "EL command output" "EL1" +check "memory command output" "conventional_memory_kb:" +check "keyboard phase finished on exit" "keyboard: done (exit)" if grep -qE "report_kind: (panic|fault)" "${work}/serial.txt"; then echo "FAIL panic or fault reported" failed=1