From 97da54437e1fda6c46a35d5bae3bca0c40cfd001 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 20 Apr 2023 13:06:02 +0800 Subject: [PATCH 1/5] kernel: allow debug of crashed processes When a process crashes, report this in the kernel console and offer to attach a debugger. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/irq.rs | 2 +- kernel/src/debug/gdb.rs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index 94b64987b..be8d107a5 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -411,7 +411,7 @@ pub extern "C" fn trap_handler( ex ); #[cfg(any(feature = "precursor", feature = "renode"))] - panic!( + println!( "{}: CPU Exception on PID {}: {}", if is_kernel_failure { "!!! KERNEL FAILURE !!!" diff --git a/kernel/src/debug/gdb.rs b/kernel/src/debug/gdb.rs index 55fd0e2ca..1c0bf89c7 100644 --- a/kernel/src/debug/gdb.rs +++ b/kernel/src/debug/gdb.rs @@ -295,7 +295,7 @@ pub fn report_stop(_pid: xous_kernel::PID, tid: xous_kernel::TID, _pc: usize) { }; } -pub fn report_terminated(_pid: xous_kernel::PID) { +pub fn report_terminated(pid: xous_kernel::PID) { let Some(XousDebugState { mut target, server: gdb, @@ -306,7 +306,6 @@ pub fn report_terminated(_pid: xous_kernel::PID) { let new_gdb = match gdb { GdbStubStateMachine::Running(inner) => { - println!("Reporting a STOP"); match inner.report_stop( &mut target, MultiThreadStopReason::Signal(Signal::EXC_BAD_ACCESS), @@ -326,9 +325,9 @@ pub fn report_terminated(_pid: xous_kernel::PID) { println!("GDB state was in Disconnect, which shouldn't be possible!"); return; } - GdbStubStateMachine::Idle(_inner) => { - println!("GDB state was in Idle, which shouldn't be possible!"); - return; + GdbStubStateMachine::Idle(inner) => { + println!("Please connect a debugger to debug process {}", pid); + GdbStubStateMachine::Idle(inner) } }; From ef077ed7dec5ecf1eb15907367e7036e7810f834 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 20 Apr 2023 13:06:33 +0800 Subject: [PATCH 2/5] codec: place codec object into heap The codec object contains a lot of data including a `FrameRing` buffer as well as some other services. With the latest version of Rust, this has grown large and no longer fits onto the stack. As a result, it crashes with a stack overflow immediately upon boot. Move the Codec onto the heap. This frees up stack and allows it to run under Rust 1.69.0. Signed-off-by: Sean Cross --- services/codec/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/codec/src/main.rs b/services/codec/src/main.rs index 545e845ac..818ab3be8 100644 --- a/services/codec/src/main.rs +++ b/services/codec/src/main.rs @@ -31,7 +31,7 @@ fn main() -> ! { log::trace!("registered with NS -- {:?}", codec_sid); let codec_conn = xous::connect(codec_sid).expect("couldn't make connection for the codec implementation"); - let mut codec = Codec::new(codec_conn, &xns); + let mut codec = Box::new(Codec::new(codec_conn, &xns)); let ticktimer = ticktimer_server::Ticktimer::new().unwrap(); log::trace!("ready to accept requests"); From 5e5cc9352b2b5984f5b36aa651258cd37deec0df Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 20 Apr 2023 13:14:36 +0800 Subject: [PATCH 3/5] services: aes: fix () warnings on vex Rust 1.69 adds more warnings on excess punctuation. Remove this to clear up the warning. Signed-off-by: Sean Cross --- services/aes/src/vex/aes128.rs | 10 +++++----- services/aes/src/vex/aes256.rs | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/services/aes/src/vex/aes128.rs b/services/aes/src/vex/aes128.rs index aa9c88b81..1ef8f7c2d 100644 --- a/services/aes/src/vex/aes128.rs +++ b/services/aes/src/vex/aes128.rs @@ -21,7 +21,7 @@ fn set_encrypt_key_inner_128(user_key: &[u8], swap_final: bool) -> VexKeys128 { ^ (TE2[(temp >> 16) & 0xff] & 0xff000000) ^ (TE3[(temp >> 8) & 0xff] & 0x00ff0000) ^ (TE0[(temp) & 0xff] & 0x0000ff00) - ^ (TE1[(temp >> 24)] & 0x000000ff) + ^ (TE1[temp >> 24] & 0x000000ff) ^ rcon; rk[5 + rk_offset] = rk[1 + rk_offset] ^ rk[4 + rk_offset]; rk[6 + rk_offset] = rk[2 + rk_offset] ^ rk[5 + rk_offset]; @@ -68,19 +68,19 @@ pub fn aes128_dec_key_schedule(user_key: &[u8]) -> VexKeys128 { /* apply the inverse MixColumn transform to all round keys but the first and the last: */ let mut rk_offset = 4; for _ in 1..rounds { - rk[0 + rk_offset] = TD0[TE1[(rk[0 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[0 + rk_offset] = TD0[TE1[rk[0 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[0 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[0 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[0 + rk_offset] as usize) & 0xff] as usize & 0xff]; - rk[1 + rk_offset] = TD0[TE1[(rk[1 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[1 + rk_offset] = TD0[TE1[rk[1 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[1 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[1 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[1 + rk_offset] as usize) & 0xff] as usize & 0xff]; - rk[2 + rk_offset] = TD0[TE1[(rk[2 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[2 + rk_offset] = TD0[TE1[rk[2 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[2 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[2 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[2 + rk_offset] as usize) & 0xff] as usize & 0xff]; - rk[3 + rk_offset] = TD0[TE1[(rk[3 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[3 + rk_offset] = TD0[TE1[rk[3 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[3 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[3 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[3 + rk_offset] as usize) & 0xff] as usize & 0xff]; diff --git a/services/aes/src/vex/aes256.rs b/services/aes/src/vex/aes256.rs index 7738edf32..30ad61d0b 100644 --- a/services/aes/src/vex/aes256.rs +++ b/services/aes/src/vex/aes256.rs @@ -26,7 +26,7 @@ fn set_encrypt_key_inner_256(user_key: &[u8], swap_final: bool) -> VexKeys256 { ^ (TE2[(temp >> 16) & 0xff] & 0xff000000) ^ (TE3[(temp >> 8) & 0xff] & 0x00ff0000) ^ (TE0[(temp) & 0xff] & 0x0000ff00) - ^ (TE1[(temp >> 24)] & 0x000000ff) + ^ (TE1[temp >> 24] & 0x000000ff) ^ rcon; rk[9 + rk_offset] = rk[1 + rk_offset] ^ rk[8 + rk_offset]; rk[10 + rk_offset] = rk[2 + rk_offset] ^ rk[9 + rk_offset]; @@ -39,7 +39,7 @@ fn set_encrypt_key_inner_256(user_key: &[u8], swap_final: bool) -> VexKeys256 { let temp = rk[11 + rk_offset] as usize; rk[12 + rk_offset] = rk[4 + rk_offset] - ^ (TE2[(temp >> 24)] & 0xff000000) + ^ (TE2[temp >> 24] & 0xff000000) ^ (TE3[(temp >> 16) & 0xff] & 0x00ff0000) ^ (TE0[(temp >> 8) & 0xff] & 0x0000ff00) ^ (TE1[(temp) & 0xff] & 0x000000ff); @@ -90,19 +90,19 @@ pub fn aes256_dec_key_schedule(user_key: &[u8]) -> VexKeys256 { /* apply the inverse MixColumn transform to all round keys but the first and the last: */ let mut rk_offset = 4; for _ in 1..rounds { - rk[0 + rk_offset] = TD0[TE1[(rk[0 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[0 + rk_offset] = TD0[TE1[rk[0 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[0 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[0 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[0 + rk_offset] as usize) & 0xff] as usize & 0xff]; - rk[1 + rk_offset] = TD0[TE1[(rk[1 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[1 + rk_offset] = TD0[TE1[rk[1 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[1 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[1 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[1 + rk_offset] as usize) & 0xff] as usize & 0xff]; - rk[2 + rk_offset] = TD0[TE1[(rk[2 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[2 + rk_offset] = TD0[TE1[rk[2 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[2 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[2 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[2 + rk_offset] as usize) & 0xff] as usize & 0xff]; - rk[3 + rk_offset] = TD0[TE1[(rk[3 + rk_offset] as usize >> 24)] as usize & 0xff] + rk[3 + rk_offset] = TD0[TE1[rk[3 + rk_offset] as usize >> 24] as usize & 0xff] ^ TD1[TE1[(rk[3 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] ^ TD2[TE1[(rk[3 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] ^ TD3[TE1[(rk[3 + rk_offset] as usize) & 0xff] as usize & 0xff]; From 9b7dbcee2abb0e70aaa03ab204501d1396ece5ce Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 21 Apr 2023 13:43:27 +0800 Subject: [PATCH 4/5] fix manifest feature issue --- services/keyboard/Cargo.toml | 2 +- services/usb-device-xous/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/keyboard/Cargo.toml b/services/keyboard/Cargo.toml index f0d6e879f..44498fdc8 100644 --- a/services/keyboard/Cargo.toml +++ b/services/keyboard/Cargo.toml @@ -33,4 +33,4 @@ debuginject = [] # used only if you want direct key injection via serial or USB. rawserial = [] braille = [] inject-api = [] # used by the serial feature in usb -default = ["debuginject", "inject-api"] # "debugprint" +default = ["debuginject"] # "debugprint" diff --git a/services/usb-device-xous/Cargo.toml b/services/usb-device-xous/Cargo.toml index d7314fccc..daea13cea 100644 --- a/services/usb-device-xous/Cargo.toml +++ b/services/usb-device-xous/Cargo.toml @@ -19,7 +19,7 @@ num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} susres = {package = "xous-api-susres", version = "0.9.36"} modals = {path = "../modals"} -keyboard = {path = "../keyboard", feature="inject-api"} +keyboard = {path = "../keyboard", features=["inject-api"]} bitfield = "0.13.2" vcell = "0.1.3" xous-semver = "0.1.2" From 0b9199bc253215359bcaf253ba26f4683687b578 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 21 Apr 2023 13:43:50 +0800 Subject: [PATCH 5/5] fix regression due to #366 Boxing the `Codec` object and sticking it on the heap causes audio playback to hard-lock (no kp, no other explanation given). Not sure what the issue is, but a cleaner fix is arguably to simply increase the stack size so the objects can be allocated there. I suspect the problem may have to do with how the FIFO structures are memory-mapped and somehow this does not play nice with Box (there are `unsafe` routines within it). --- services/codec/src/main.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/services/codec/src/main.rs b/services/codec/src/main.rs index 818ab3be8..50f8e3e27 100644 --- a/services/codec/src/main.rs +++ b/services/codec/src/main.rs @@ -20,7 +20,17 @@ struct ScalarCallback { cb_to_client_id: u32, } -fn main() -> ! { +fn main () -> ! { + let stack_size = 256 * 1024; + std::thread::Builder::new() + .stack_size(stack_size) + .spawn(wrapped_main) + .unwrap() + .join() + .unwrap() +} + +fn wrapped_main() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); info!("my PID is {}", xous::process::id()); @@ -31,7 +41,7 @@ fn main() -> ! { log::trace!("registered with NS -- {:?}", codec_sid); let codec_conn = xous::connect(codec_sid).expect("couldn't make connection for the codec implementation"); - let mut codec = Box::new(Codec::new(codec_conn, &xns)); + let mut codec = Codec::new(codec_conn, &xns); let ticktimer = ticktimer_server::Ticktimer::new().unwrap(); log::trace!("ready to accept requests");