diff --git a/apps/system/components/DebugConsole/kata-allocator/src/lib.rs b/apps/system/components/DebugConsole/kata-allocator/src/lib.rs index 9fefeef..24094a8 100644 --- a/apps/system/components/DebugConsole/kata-allocator/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-allocator/src/lib.rs @@ -5,6 +5,7 @@ use core::alloc::{GlobalAlloc, Layout}; use core::cell::RefCell; +use core::panic; use core::ptr::{self, NonNull}; use log::info; @@ -19,9 +20,8 @@ pub struct KataHeap { pub static ALLOCATOR: KataHeap = KataHeap::empty(); #[alloc_error_handler] -fn alloc_error_handler(_: Layout) -> ! { - // TODO(sleffler): at least print a msg on the console - loop {} +fn alloc_error_handler(layout: Layout) -> ! { + panic!("Global allocation failure: {:?}", layout); } impl KataHeap { diff --git a/apps/system/components/DebugConsole/kata-debug-console/Cargo.toml b/apps/system/components/DebugConsole/kata-debug-console/Cargo.toml index 167fc4b..55fbe48 100644 --- a/apps/system/components/DebugConsole/kata-debug-console/Cargo.toml +++ b/apps/system/components/DebugConsole/kata-debug-console/Cargo.toml @@ -9,6 +9,7 @@ description = "Kata OS DebugConsole" panic-halt = "0.2.0" kata-allocator = { path = "../kata-allocator" } kata-logger = { path = "../kata-logger" } +kata-panic = { path = "../kata-panic" } kata-shell = { path = "../kata-shell" } kata-uart-client = { path = "../kata-uart-client" } log = "0.4" diff --git a/apps/system/components/DebugConsole/kata-debug-console/src/run.rs b/apps/system/components/DebugConsole/kata-debug-console/src/run.rs index fb7740b..8e3dfc5 100644 --- a/apps/system/components/DebugConsole/kata-debug-console/src/run.rs +++ b/apps/system/components/DebugConsole/kata-debug-console/src/run.rs @@ -9,11 +9,9 @@ // * kata_shell // * kata_debug_console main entry point fn run() -// std:: requires at least an allocator, which Kata does not have yet. For now -// the CLI will be implemented with only core::. #![no_std] -extern crate panic_halt; +extern crate kata_panic; use kata_allocator; use kata_logger::KataLogger; diff --git a/apps/system/components/DebugConsole/kata-panic/Cargo.toml b/apps/system/components/DebugConsole/kata-panic/Cargo.toml new file mode 100644 index 0000000..98fc63b --- /dev/null +++ b/apps/system/components/DebugConsole/kata-panic/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "kata-panic" +version = "0.1.0" +edition = "2018" + +[dependencies] +log = "0.4" diff --git a/apps/system/components/DebugConsole/kata-panic/src/lib.rs b/apps/system/components/DebugConsole/kata-panic/src/lib.rs new file mode 100644 index 0000000..9b2b574 --- /dev/null +++ b/apps/system/components/DebugConsole/kata-panic/src/lib.rs @@ -0,0 +1,18 @@ +#![no_std] + +use core::panic::PanicInfo; +use core::sync::atomic::{self, Ordering}; +use log::error; + +#[inline(never)] +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + // Could use panic::set_hook but we're already here... + error!("{}", info); + + // Halt the thread. + loop { + // TODO(sleffler): seL4_Yield? + atomic::compiler_fence(Ordering::SeqCst); + } +} diff --git a/apps/system/components/DebugConsole/kata-shell/src/lib.rs b/apps/system/components/DebugConsole/kata-shell/src/lib.rs index 2ef39bb..cc3935a 100644 --- a/apps/system/components/DebugConsole/kata-shell/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-shell/src/lib.rs @@ -73,11 +73,15 @@ fn dispatch_command(cmdline: &str, output: &mut dyn io::Write) { // Since even the binding is static, it is fine for each command // implementation to use its own preferred signature. let result = match command { - "echo" => echo_command(cmdline, output), "add" => add_command(&mut args, output), + "echo" => echo_command(cmdline, output), "clear" => clear_command(output), "ps" => ps_command(), - "alloc_test" => alloc_test_command(output), + + "test_alloc" => test_alloc_command(output), + "test_alloc_error" => test_alloc_error_command(output), + "test_panic" => test_panic_command(), + _ => Err(CommandError::UnknownCommand), }; if let Err(e) = result { @@ -143,7 +147,7 @@ fn clear_command(output: &mut dyn io::Write) -> Result<(), CommandError> { /// Implements a command that tests facilities that use the global allocator. /// Shamelessly cribbed from https://os.phil-opp.com/heap-allocation/ -fn alloc_test_command(output: &mut dyn io::Write) -> Result<(), CommandError> { +fn test_alloc_command(output: &mut dyn io::Write) -> Result<(), CommandError> { extern crate alloc; use alloc::{boxed::Box, rc::Rc, vec, vec::Vec}; @@ -177,3 +181,21 @@ fn alloc_test_command(output: &mut dyn io::Write) -> Result<(), CommandError> { Ok(writeln!(output, "All tests passed!")?) } + +/// Implements a command that tests the global allocator error handling. +fn test_alloc_error_command(output: &mut dyn io::Write) -> Result<(), CommandError> { + extern crate alloc; + use alloc::vec::Vec; + + // Default heap holds 16KB. + let mut vec = Vec::with_capacity(16384); + for i in 0..16348 { + vec.push(i); + } + Ok(writeln!(output, "vec at {:p}", vec.as_slice())?) +} + +/// Implements a command that tests panic handling. +fn test_panic_command() -> Result<(), CommandError> { + panic!("testing"); +} diff --git a/apps/system/components/ProcessManager/kata-proc-manager/Cargo.toml b/apps/system/components/ProcessManager/kata-proc-manager/Cargo.toml index dbb7737..3372eed 100644 --- a/apps/system/components/ProcessManager/kata-proc-manager/Cargo.toml +++ b/apps/system/components/ProcessManager/kata-proc-manager/Cargo.toml @@ -8,9 +8,9 @@ edition = "2018" arrayvec = { version = "0.7", default-features = false } kata-allocator = { path = "../../DebugConsole/kata-allocator" } kata-logger = { path = "../../DebugConsole/kata-logger" } +kata-panic = { path = "../../DebugConsole/kata-panic" } kata-proc-common = { path = "../kata-proc-common" } log = "0.4" -panic-halt = "0.2.0" [lib] name = "kata_process_manager" diff --git a/apps/system/components/ProcessManager/kata-proc-manager/src/run.rs b/apps/system/components/ProcessManager/kata-proc-manager/src/run.rs index a7241eb..ce22bec 100644 --- a/apps/system/components/ProcessManager/kata-proc-manager/src/run.rs +++ b/apps/system/components/ProcessManager/kata-proc-manager/src/run.rs @@ -6,7 +6,7 @@ #![feature(const_fn_trait_bound)] // NB: for ProcessManager::empty using manager: None #[cfg(not(test))] -extern crate panic_halt; +extern crate kata_panic; use arrayvec::ArrayVec; use core::marker::Sync;