From ccb5912c1a8374765cd0515c7f30d830e720528d Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 May 2026 03:10:33 +0800 Subject: [PATCH 1/6] fix timeout on suspend/resume the susres process timeout timer was not cancellable, which means that there was always a timeout pending regardless of the success or failure of a suspend/resume process. This fixes the implementation so that the timer is cancelled in the case of a successful suspend/resume. --- .../bao1x-hal-service/src/servers/susres.rs | 89 +++++++++++-------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/services/bao1x-hal-service/src/servers/susres.rs b/services/bao1x-hal-service/src/servers/susres.rs index f4b8dee07..c4a25b1df 100644 --- a/services/bao1x-hal-service/src/servers/susres.rs +++ b/services/bao1x-hal-service/src/servers/susres.rs @@ -1,4 +1,5 @@ use core::sync::atomic::{AtomicU32, Ordering}; +use std::sync::{Arc, atomic::AtomicBool}; use num_traits::*; use utralib::*; @@ -51,7 +52,9 @@ fn susres_service() { let timeout_sid = xous::create_server().unwrap(); let timeout_outgoing_conn = xous::connect(timeout_sid).unwrap(); + let timeout_pending = Arc::new(AtomicBool::new(false)); std::thread::spawn({ + let timeout_pending = timeout_pending.clone(); let timeout_sid = timeout_sid.clone(); // safety: this thread will read-only from the susres timer fields, and thus its operations // are thread-safe @@ -70,21 +73,30 @@ fn susres_service() { } let start = get_hw_time(&hw); let timeout = TIMEOUT_TIME.load(Ordering::Relaxed); // ignore updates to timeout once we're waiting + let mut reached_timeout = true; while ((get_hw_time(&hw) - start) as u32) < timeout { // log::info!("delta t: {}", (get_hw_time(hw) - start) as u32); xous::yield_slice(); + if !timeout_pending.load(Ordering::SeqCst) { + // remember the check here so we don't have inconsistent state with the send_message + // later + reached_timeout = false; + break; + } + } + + if reached_timeout { + log::trace!("HW timeout reached"); + send_message( + susres_conn, + Message::new_scalar(Opcode::SuspendTimeout.to_usize().unwrap(), 0, 0, 0, 0), + ) + .ok(); } - log::trace!("HW timeout reached"); - send_message( - susres_conn, - Message::new_scalar(Opcode::SuspendTimeout.to_usize().unwrap(), 0, 0, 0, 0), - ) - .ok(); } } }); let mut suspend_requested: Option = None; - let mut timeout_pending = false; let mut reboot_requested: bool = false; let mut allow_suspend = true; @@ -173,10 +185,14 @@ fn susres_service() { } // note: we must have at least one `Last` subscriber for this logic to work! if all_ready && current_op_order == xous_api_susres::SuspendOrder::Last { + // this stops the timeout timer from going + timeout_pending.store(false, Ordering::SeqCst); + // allow the state to propagate to the timer + xous::yield_slice(); + // turn off preemption hal.set_preemption(false); log::info!("all callbacks reporting in, doing suspend"); - timeout_pending = false; use bao1x_api::bio::BioApi; let mut bio = bao1x_hal::bio::Bio::new(); @@ -210,35 +226,29 @@ fn susres_service() { // - The update_bio_freq routine is being "too clever" and compensating for the fd // setting. But it seems like the routine just takes the putative fclk setting as // the argument to update_bio_freq(), I don't see any compensation happening... - bio.prep_freq_change(); - if fclk_fd_backup == 0xff { - bio.update_bio_freq(48_000_000); - } else { - bio.update_bio_freq(24_000_000); - } - // clk_mgr.set_fclk_fd(0xff); // set divider to 1:1 ratio + bio.prep_freq_change(true); + bio.update_bio_freq(48_000_000); clk_mgr.wfi(); // ~~~ time passes, but we're on carbonite so we don't notice ~~~ - // clk_mgr.set_fclk_fd(fclk_fd_backup); // restore the FD before restoring potentially + bio.prep_freq_change(false); + clk_mgr.set_fclk_fd(fclk_fd_backup); // restore the FD for fclk, as it is side-effected by WFI // fast PLL setting - bio.prep_freq_change(); clk_mgr.restore_wfi(); bio.update_bio_freq(clk_mgr.fclk); // when wfi() returns, it means we've resumed let sender = suspend_requested .take() - .expect("suspend was requested, but no requestor is on record!"); + .expect("suspend_requested was checked to be Some() at entry to the loop, but now somehow it's now None!"); log_server::resume(); // log server is a special case, in order to avoid circular dependencies // this now allows all other threads to commence log::info!("low-level resume done, restoring execution"); for pid in gated_pids.drain(..) { - xous::return_scalar(pid, 0) - .expect("couldn't return dummy message to unblock execution"); + xous::return_scalar(pid, 0).ok(); } // restore preemption hal.set_preemption(true); @@ -265,13 +275,16 @@ fn susres_service() { } }), Some(Opcode::SuspendRequest) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { - log::info!("registered suspend listeners:"); + log::debug!("registered suspend listeners:"); for sub in suspend_subscribers.iter() { - log::info!("{:?}", sub); + log::debug!("{:?}", sub); } - // if the 2-second timeout is still pending from a previous suspend, deny the suspend + // if the timeout is still pending from a previous suspend, deny the suspend // request. ...just don't suspend that quickly after resuming??? - if allow_suspend && !timeout_pending { + // + // note that short-circuit eval means timeout_pending is not side-effected if + // allow_spend is false. Do not change the order of these operations! + if allow_suspend && !timeout_pending.swap(true, Ordering::SeqCst) { suspend_requested = Some(msg.sender); // clear the ready to suspend flag and failed to suspend flag @@ -279,11 +292,10 @@ fn susres_service() { sub.ready_to_suspend = false; sub.failed_to_suspend = false; } - // do we want to start the timeout before or after sending the notifications? hmm. 🤔 - timeout_pending = true; - // any message to this server will trigger it - it only has one function - send_message(timeout_outgoing_conn, Message::new_scalar(0, 0, 0, 0, 0)) - .expect("couldn't initiate timeout before suspend!"); + + // any message to the timeout server will trigger it - it only has one function + xous::try_send_message(timeout_outgoing_conn, Message::new_scalar(0, 0, 0, 0, 0)) + .ok(); current_op_order = xous_api_susres::SuspendOrder::Early; let mut at_least_one_event_sent = false; @@ -305,12 +317,11 @@ fn susres_service() { } }), Some(Opcode::SuspendTimeout) => { - if timeout_pending { + if timeout_pending.swap(false, Ordering::SeqCst) { // record which tokens had not reported in for sub in suspend_subscribers.iter_mut() { sub.failed_to_suspend = !sub.ready_to_suspend; } - timeout_pending = false; log::warn!( "Suspend timed out, forcing an unclean suspend at stage {:?}", current_op_order @@ -330,20 +341,22 @@ fn susres_service() { } } - let sender = suspend_requested - .take() - .expect("suspend was requested, but no requestor is on record!"); for pid in gated_pids.drain(..) { xous::return_scalar(pid, 0) .expect("couldn't return dummy message to unblock execution"); } // this unblocks the requestor of the suspend - xous::return_scalar(sender, 0).ok(); + if let Some(sender) = suspend_requested.take() { + xous::return_scalar(sender, 0).ok(); + } else { + log::error!("Suspend requester disappeared - race condition somewhere!"); + } } else { - log::info!("clean suspend timeout received, ignoring"); + log::warn!("clean suspend timeout received, ignoring"); // this means we did a clean suspend, we've resumed, and the timeout came back after - // the resume just ignore the message. + // the resume just ignore the message. It's a warning because this message should, + // in practice, never be generated due to the timeout_pending semaphore. } } Some(Opcode::WasSuspendClean) => msg_blocking_scalar_unpack!(msg, token, _, _, _, { @@ -448,7 +461,7 @@ fn send_event( order: xous_api_susres::SuspendOrder, ) -> (bool, xous_api_susres::SuspendOrder) { let mut at_least_one_event_sent = false; - log::info!("Sending suspend to {:?} stage", order); + log::debug!("Sending suspend to {:?} stage", order); /* // abortive attempt to get suspend to shut down the system. Doesn't work, results in a panic because too many messages are still moving around. #[cfg(not(target_os = "xous"))] From ae0eee60c5b29d337dd7785dfb17273d4818798f Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 May 2026 03:11:30 +0800 Subject: [PATCH 2/6] make bio-op infalliable this allows suspends to proceed even if the subsystem has an error --- services/bao1x-hal-service/src/servers/bio.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/bao1x-hal-service/src/servers/bio.rs b/services/bao1x-hal-service/src/servers/bio.rs index 77f46d374..52f93d458 100644 --- a/services/bao1x-hal-service/src/servers/bio.rs +++ b/services/bao1x-hal-service/src/servers/bio.rs @@ -132,7 +132,11 @@ fn bio_service(clk_freq: u32) { if let Some(_scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { // pause rendering while the clock transitions, avoiding 'bright glitches' #[cfg(feature = "oem-baosec-lite")] - xous::send_message(led_conn, xous::Message::new_blocking_scalar(128, 0, 0, 0, 0)).ok(); + xous::send_message( + led_conn, + xous::Message::new_blocking_scalar(128, _scalar.arg1, 0, 0, 0), + ) + .ok(); } } From b48df7182b3109860cab9362c49ee4bf3c3dc5e0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 May 2026 03:11:58 +0800 Subject: [PATCH 3/6] make preemption switching infalliable this allows suspend/resume to complete even if there's a back-up in the hal processing these requests --- services/bao1x-hal-service/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/bao1x-hal-service/src/lib.rs b/services/bao1x-hal-service/src/lib.rs index 9a10ffea1..aec1de231 100644 --- a/services/bao1x-hal-service/src/lib.rs +++ b/services/bao1x-hal-service/src/lib.rs @@ -95,7 +95,7 @@ impl Hal { 0, ), ) - .expect("Couldn't setup preemption state"); + .ok(); } } From d30ef2f197ab6749f418a04322cbe7e45cf145b1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 May 2026 03:12:47 +0800 Subject: [PATCH 4/6] debug/fix frequency changes into and out of WFI I am now confident that the FCLK frequency is correct going into WFI. --- libs/bao1x-hal/src/bio.rs | 10 ++++++++-- libs/bao1x-hal/src/bio_hw.rs | 8 ++++++-- libs/bao1x-hal/src/clocks.rs | 4 +++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libs/bao1x-hal/src/bio.rs b/libs/bao1x-hal/src/bio.rs index 40dbce042..0979eeda7 100644 --- a/libs/bao1x-hal/src/bio.rs +++ b/libs/bao1x-hal/src/bio.rs @@ -134,10 +134,16 @@ impl<'a> BioApi<'a> for Bio { } } - fn prep_freq_change(&mut self) { + fn prep_freq_change(&mut self, into_wfi: bool) { send_message( self.conn, - Message::new_blocking_scalar(BioOp::PrepFreqChange.to_usize().unwrap(), 0, 0, 0, 0), + Message::new_blocking_scalar( + BioOp::PrepFreqChange.to_usize().unwrap(), + if into_wfi { 1 } else { 0 }, + 0, + 0, + 0, + ), ) .ok(); } diff --git a/libs/bao1x-hal/src/bio_hw.rs b/libs/bao1x-hal/src/bio_hw.rs index d6c4fc69d..5b4889316 100644 --- a/libs/bao1x-hal/src/bio_hw.rs +++ b/libs/bao1x-hal/src/bio_hw.rs @@ -405,10 +405,12 @@ impl BioSharedState { // mask in the core that is now listening to the clock let existing_extclk = self.bio.rf(bio_bdma::SFR_EXTCLOCK_USE_EXTCLK); self.bio.rmwf(bio_bdma::SFR_EXTCLOCK_USE_EXTCLK, (1 << core as u32) | existing_extclk); - crate::println!("core: {:?} - extclock {:x}", core, self.bio.r(bio_bdma::SFR_EXTCLOCK)); + // crate::println!("core: {:?} - extclock {:x}", core, self.bio.r(bio_bdma::SFR_EXTCLOCK)); return None; } }; + #[cfg(feature = "std")] + log::debug!("config: {:?}, int {}, fraq {}, actual {}", config, div_int, div_frac, actual_freq); let sfr_value = (div_int as u32) << 16 | (div_frac as u32) << 8; match core { BioCore::Core0 => { @@ -494,6 +496,8 @@ impl<'a> BioApi<'a> for BioSharedState { fn update_bio_freq(&mut self, freq: u32) -> u32 { let prev_freq = self.fclk_freq_hz; self.fclk_freq_hz = freq; + #[cfg(feature = "std")] + log::debug!("new freq: {}, old freq: {}", freq, prev_freq); for (i, config) in self.core_config.clone().iter().enumerate() { if let Some(config) = config { self.apply_config(config, i.into()); @@ -502,7 +506,7 @@ impl<'a> BioApi<'a> for BioSharedState { prev_freq } - fn prep_freq_change(&mut self) { + fn prep_freq_change(&mut self, _wfi: bool) { // at the hw level, there's nothing to do; a handler at the `std` level is invoked instead } diff --git a/libs/bao1x-hal/src/clocks.rs b/libs/bao1x-hal/src/clocks.rs index c4b3da1e3..686d0246e 100644 --- a/libs/bao1x-hal/src/clocks.rs +++ b/libs/bao1x-hal/src/clocks.rs @@ -637,7 +637,9 @@ impl ClockManagerImpl { self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); wfi_debug("internal osc"); // don't divide fclk -- allows BIO to keep operating at 48MHz rate - self.sysctrl.wo(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, 0x0700_01ff); + self.sysctrl.wo(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, 0x0000_ffff); + self.sysctrl.wo(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, 0x0000_ffff); + self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); // lower core voltage to 0.7v self.ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420002); From 50dcf7880fe4f23f25e1992edb3d642ebbc05f3d Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 May 2026 03:13:30 +0800 Subject: [PATCH 5/6] add a wfi specifier to the bio clock change api this indicates if we're going into or out of wfi - this matters because the clock rate asymmetry can be quite great, and the calling function can have different optimizations for each mode of operation --- libs/bao1x-api/src/bio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/bao1x-api/src/bio.rs b/libs/bao1x-api/src/bio.rs index 01234d36e..553633cba 100644 --- a/libs/bao1x-api/src/bio.rs +++ b/libs/bao1x-api/src/bio.rs @@ -52,8 +52,8 @@ pub trait BioApi<'a> { fn update_bio_freq(&mut self, freq: u32) -> u32; /// Called before frequency changes to activate any application-specific callbacks - /// to avoid glitches. - fn prep_freq_change(&mut self); + /// to avoid glitches. `into_wfi` specifies if we're about to enter a WFI state. + fn prep_freq_change(&mut self, into_wfi: bool); /// Returns the frequency, in Hz, of the incoming clock to the BIO cores. fn get_bio_freq(&self) -> u32; From ada3964dfbc4d3d034466d303297fc57dede0f39 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 May 2026 03:14:26 +0800 Subject: [PATCH 6/6] add an "os" set to the signer makes things a little more convenient for iterating on signed images --- baosign.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/baosign.ps1 b/baosign.ps1 index eeb63b315..8cb99d3d3 100644 --- a/baosign.ps1 +++ b/baosign.ps1 @@ -9,7 +9,7 @@ param( [Parameter(Mandatory = $true)] - [ValidateSet("baremetal", "dabao", "baosec", "bootloader", "kernel", "loader", "swap", "apps", "boot1", "alt-boot1", "baosec-lite")] + [ValidateSet("baremetal", "dabao", "baosec", "bootloader", "kernel", "loader", "swap", "apps", "boot1", "alt-boot1", "baosec-lite", "os")] [string]$Config, [Parameter(Mandatory = $false)] @@ -54,6 +54,10 @@ $configurations = @{ @{ Image = "bao1x-boot1.img"; FunctionCode = "boot1" ; TargetDir = "target\riscv32imac-unknown-none-elf\release" }, @{ Image = "bao1x-alt-boot1.img"; FunctionCode = "loader" ; TargetDir = "target\riscv32imac-unknown-none-elf\release" } ) + "os" = @( + @{ Image = "xous.img"; FunctionCode = "kernel" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" } + @{ Image = "swap.img"; FunctionCode = "swap" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" } + ) "kernel" = @( @{ Image = "xous.img"; FunctionCode = "kernel" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" } )