From 0b92da5babaca6d136fad2f97d4180036e0b8c53 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 3 Nov 2025 08:00:31 +0800 Subject: [PATCH] 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()) } }