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.
This commit is contained in:
bunnie 2025-11-03 08:00:31 +08:00
parent eaef679faa
commit 0b92da5bab
3 changed files with 18 additions and 9 deletions

View file

@ -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 = [

View file

@ -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;

View file

@ -2251,10 +2251,12 @@ pub struct CorigineWrapper {
pub ep_out_ready: Box<[Arc<AtomicBool>]>,
pub address_is_set: Arc<AtomicBool>,
pub event: Option<CrgEvent>,
pub irq_csr: AtomicCsr<u32>,
}
#[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())
}
}