From 0b92da5babaca6d136fad2f97d4180036e0b8c53 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 3 Nov 2025 08:00:31 +0800 Subject: [PATCH 1/2] add better locking on core USB object there is a lock that happens in serial console interactions with local echo where the instant feedback of a character entered causes the USB object to be locked when the interrupt handler enters to receive a character that was just sent. The fix to this is to add the ability to disable interrupts before entering a critical section (such as enqueuing data for a write) and then re-enabling after tha section is done. --- libs/bao1x-hal/Cargo.toml | 7 ++++--- libs/bao1x-hal/src/lib.rs | 7 +------ libs/bao1x-hal/src/usb/driver.rs | 13 +++++++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libs/bao1x-hal/Cargo.toml b/libs/bao1x-hal/Cargo.toml index db72e87ca..47fa95167 100644 --- a/libs/bao1x-hal/Cargo.toml +++ b/libs/bao1x-hal/Cargo.toml @@ -77,9 +77,10 @@ loader-baosec = [ "ux-api/loader-baosec", "sigcheck", ] -kernel-baosec = ["utralib/bao1x"] -kernel-baosor = ["utralib/bao1x"] -kernel-dabao = ["utralib/bao1x"] +kernel = [] +kernel-baosec = ["utralib/bao1x", "kernel"] +kernel-baosor = ["utralib/bao1x", "kernel"] +kernel-dabao = ["utralib/bao1x", "kernel"] test-baosec = ["ux-api/loader-baosec", "camera-gc2145"] hosted-baosec = ["std"] # emulated hardware on x86 platform board-baosor = [ diff --git a/libs/bao1x-hal/src/lib.rs b/libs/bao1x-hal/src/lib.rs index 569b539d6..e8f1ee506 100644 --- a/libs/bao1x-hal/src/lib.rs +++ b/libs/bao1x-hal/src/lib.rs @@ -37,12 +37,7 @@ pub mod mbox; #[cfg(not(feature = "hosted-baosec"))] pub mod rram; // no signature checking in the kernel -#[cfg(all( - not(feature = "hosted-baosec"), - not(feature = "kernel-baosec"), - not(feature = "kernel-dabao"), - not(feature = "kernel-baosor") -))] +#[cfg(all(not(feature = "hosted-baosec"), not(feature = "kernel"),))] pub mod sigcheck; #[cfg(not(feature = "hosted-baosec"))] pub mod sram_trim; diff --git a/libs/bao1x-hal/src/usb/driver.rs b/libs/bao1x-hal/src/usb/driver.rs index b51567d43..d1d374334 100644 --- a/libs/bao1x-hal/src/usb/driver.rs +++ b/libs/bao1x-hal/src/usb/driver.rs @@ -2251,10 +2251,12 @@ pub struct CorigineWrapper { pub ep_out_ready: Box<[Arc]>, pub address_is_set: Arc, pub event: Option, + pub irq_csr: AtomicCsr, } #[cfg(feature = "std")] impl CorigineWrapper { pub fn new(obj: CorigineUsb) -> Self { + let irq_csr = obj.irq_csr.clone(); let c = Self { hw: Arc::new(Mutex::new(obj)), ep_meta: [None; CRG_EP_NUM], @@ -2264,6 +2266,7 @@ impl CorigineWrapper { .into_boxed_slice(), event: None, address_is_set: Arc::new(AtomicBool::new(false)), + irq_csr, }; c } @@ -2275,6 +2278,7 @@ impl CorigineWrapper { ep_out_ready: self.ep_out_ready.clone(), event: None, address_is_set: self.address_is_set.clone(), + irq_csr: self.irq_csr.clone(), }; c.ep_meta.copy_from_slice(&self.ep_meta); for (dst, src) in c.ep_out_ready.iter().zip(self.ep_out_ready.iter()) { @@ -2290,6 +2294,13 @@ impl CorigineWrapper { */ self.hw.lock().unwrap() } + + pub fn disable_interrupts(&self) { self.irq_csr.wo(utralib::utra::irqarray1::EV_ENABLE, 0); } + + pub fn enable_interrupts(&self) { + self.irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, 0xFFFF_FFFF); + self.irq_csr.wo(utralib::utra::irqarray1::EV_ENABLE, 3); + } } #[cfg(feature = "std")] @@ -2535,6 +2546,7 @@ impl UsbBus for CorigineWrapper { buf.len(), &buf[..8.min(buf.len())] ); + self.disable_interrupts(); let addr = if let Some(addr) = self.core().get_app_buf_ptr(ep_addr.index() as u8, CRG_IN) { // crate::println!("addr {:x}", addr); addr @@ -2574,6 +2586,7 @@ impl UsbBus for CorigineWrapper { } #[cfg(feature = "verbose-debug")] crate::println!("ep{} initiated {}", ep_addr.index(), buf.len()); + self.enable_interrupts(); Ok(buf.len()) } } From 58632966320959c538df0ceb53a64b87f50d2771 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 3 Nov 2025 08:02:21 +0800 Subject: [PATCH 2/2] cleanup the feature tree; add double-lock detection printing this cleans up the feature dependency tree a bit and also adds some ability to print an error when the double lock condition is discovered. it should be solved but it can only be detected at runtime. Insntead of it being a silent failure (because the server hung in an interrupt context and couldn't emit any characters), it sets an atomic bool that can be checked and reported upon by the main thread. also cleans up debug uart acquisition. --- services/usb-bao1x/Cargo.toml | 2 +- services/usb-bao1x/src/hw.rs | 41 +++++++++++++++++++++++++--------- services/usb-bao1x/src/main.rs | 17 +++++--------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/services/usb-bao1x/Cargo.toml b/services/usb-bao1x/Cargo.toml index 57dd790f5..207b3e03d 100644 --- a/services/usb-bao1x/Cargo.toml +++ b/services/usb-bao1x/Cargo.toml @@ -74,7 +74,7 @@ verbose-debug = ["bao1x-hal/verbose-debug"] # mutually exclusive with kernel "print-panics"; will get a DUART allocation error on boot if kernel panics are on # sticks the DUART into the deep stack; necessary because we can't have external dependencies in usbd-device # and the DUART can be banged with simple hard-coded memory offsets. -usbd-debug = ["usb-device/baochip-debug"] +usbd-debug = ["usb-device/baochip-debug", "debug-print-usb"] prototyping = [ ] # temporary feature-out for figuring out the code; remove this if you don't recognize it anymore diff --git a/services/usb-bao1x/src/hw.rs b/services/usb-bao1x/src/hw.rs index 7ffd3dfd2..decf13257 100644 --- a/services/usb-bao1x/src/hw.rs +++ b/services/usb-bao1x/src/hw.rs @@ -1,7 +1,7 @@ use std::borrow::BorrowMut; use std::cell::RefCell; use std::collections::VecDeque; -use std::sync::atomic::compiler_fence; +use std::sync::atomic::{AtomicBool, compiler_fence}; use std::sync::atomic::{AtomicPtr, Ordering}; use bao1x_hal::usb::driver::*; @@ -51,6 +51,10 @@ pub struct Bao1xUsb<'a> { // holds one HS packet - must be statically allocated in IRQ handler. Valid length is // passed as part of the interrupt recovery message. pub serial_rx: [u8; SERIAL_MAX_PACKET_SIZE], + // an error reporter for the double lock condition, which we need to figure out how to handle still. + // used for debugging, the idea is to query this in userspace to try and pick up the double-lock problem + // from the interrupt handler. + pub double_lock: AtomicBool, } impl<'a> Bao1xUsb<'a> { @@ -104,9 +108,14 @@ impl<'a> Bao1xUsb<'a> { hid_packet: None, serial_port, serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE], + double_lock: AtomicBool::new(false), } } + #[allow(dead_code)] + /// Used only when debugging the double lock problem + pub fn double_lock_detected(&self) -> bool { self.double_lock.swap(false, Ordering::SeqCst) } + pub fn init(&mut self) { // this has to be done in `main` because we're passing a pointer to the Box'd structure, which // the IRQ handler can freely and safely manipulate @@ -124,9 +133,9 @@ impl<'a> Bao1xUsb<'a> { self.irq_csr.wo(utra::irqarray1::EV_EDGE_TRIGGERED, 0); self.irq_csr.wo(utra::irqarray1::EV_POLARITY, 0); - self.wrapper.hw.lock().unwrap().init(); - self.wrapper.hw.lock().unwrap().start(); - self.wrapper.hw.lock().unwrap().update_current_speed(); + self.wrapper.core().init(); + self.wrapper.core().start(); + self.wrapper.core().update_current_speed(); // irq must me enabled without dependency on the hw lock self.irq_csr.wo(utra::irqarray1::EV_PENDING, 0xFFFF_FFFF); @@ -145,10 +154,10 @@ impl<'a> Bao1xUsb<'a> { // disable all interrupts so we can safely go through initialization routines self.irq_csr.wo(utra::irqarray1::EV_ENABLE, 0); - self.wrapper.hw.lock().unwrap().reset(); - self.wrapper.hw.lock().unwrap().init(); - self.wrapper.hw.lock().unwrap().start(); - self.wrapper.hw.lock().unwrap().update_current_speed(); + self.wrapper.core().reset(); + self.wrapper.core().init(); + self.wrapper.core().start(); + self.wrapper.core().update_current_speed(); // reset all shared data structures self.device.force_reset().ok(); @@ -211,7 +220,14 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) { // #[cfg(feature = "verbose-debug")] // crate::println!("getting event"); // scoping on the hardware lock to manipulate pointer states - let mut corigine_usb = usb.wrapper.core(); + let mut corigine_usb = match usb.wrapper.hw.try_lock() { + Ok(lock) => lock, + _ => { + crate::println!("double lock - this case is actually broken, stack will crash"); + usb.double_lock.store(true, Ordering::SeqCst); + return; + } + }; let mut event = { if corigine_usb.udc_event.evt_dq_pt.load(Ordering::SeqCst).is_null() { // break; @@ -219,7 +235,12 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) { break; } let event_ptr = corigine_usb.udc_event.evt_dq_pt.load(Ordering::SeqCst) as usize; - unsafe { (event_ptr as *mut EventTrbS).as_mut().expect("couldn't deref pointer") } + match unsafe { (event_ptr as *mut EventTrbS).as_mut() } { + Some(ptr) => ptr, + None => { + break; + } + } }; if event.dw3.cycle_bit() != corigine_usb.udc_event.ccs { break; diff --git a/services/usb-bao1x/src/main.rs b/services/usb-bao1x/src/main.rs index 070614aa4..85827be99 100644 --- a/services/usb-bao1x/src/main.rs +++ b/services/usb-bao1x/src/main.rs @@ -62,16 +62,7 @@ fn main() -> ! { pub(crate) fn main_hw() -> ! { #[cfg(feature = "usbd-debug")] - // bind the duart - let duart_mapping = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::duart::HW_DUART_BASE), - xous::MemoryAddress::new(0x3000_0000), - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map DUART"); - #[cfg(feature = "usbd-debug")] - crate::println!("duart mapping: {:x?}", duart_mapping); + bao1x_hal::claim_duart(); #[cfg(feature = "debug-print-usb")] panic::set_hook(Box::new(|info| { @@ -288,6 +279,11 @@ pub(crate) fn main_hw() -> ! { let msg = msg_opt.as_mut().unwrap(); let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::InvalidCall); log::debug!("{:?}", opcode); + if cu.double_lock_detected() { + log::warn!( + "Double lock error detected in USB stack. Meditations: services/usb-bao1x/src/hw.rs@226 (composite_handler inner loop) and consider adding more IRQ enable/disable similar to libs/bao1x-hal/src/usb/driver.rs@2549 (write impl)" + ); + } match opcode { #[cfg(feature = "board-baosec")] Opcode::PmicIrq => match pmic.get_vbus_irq_status(&mut i2c).unwrap() { @@ -750,7 +746,6 @@ pub(crate) fn main_hw() -> ! { usb_send.s.as_bytes().chunks(bao1x_hal::usb::driver::CRG_UDC_APP_BUFSIZE) { cu.serial_port.write(&chunk).ok(); - cu.serial_port.flush().ok(); // just drop characters on error } } _ => {} // silent errors