suspend/resume now works to hardware

This commit is contained in:
bunnie 2021-04-30 19:29:36 +08:00
parent 7053c2b7c6
commit 640ddb48e8
17 changed files with 69 additions and 14 deletions

0
kernel/assemble.sh Normal file → Executable file
View file

View file

@ -3,7 +3,7 @@
use core::fmt; use core::fmt;
#[derive(PartialEq)] #[derive(PartialEq, Debug)]
pub enum RiscvException { pub enum RiscvException {
/// When things are all 0 /// When things are all 0
NoException, NoException,

View file

@ -27,7 +27,8 @@ pub fn disable_all_irqs() {
} }
/// Enable external interrupts /// Enable external interrupts
pub fn enable_all_irqs() { #[export_name = "_enable_all_irqs"]
pub extern "C" fn enable_all_irqs() {
sim::write(unsafe{SIM_BACKING}); sim::write(unsafe{SIM_BACKING});
} }
@ -122,6 +123,7 @@ pub extern "C" fn trap_handler(
use crate::arch::exception::RiscvException; use crate::arch::exception::RiscvException;
let ex = RiscvException::from_regs(sc.bits(), sepc::read(), stval::read()); let ex = RiscvException::from_regs(sc.bits(), sepc::read(), stval::read());
println!("ex: {:?}", ex);
if sc.is_exception() { if sc.is_exception() {
// If the CPU tries to store, look for a "reserved page" and provide // If the CPU tries to store, look for a "reserved page" and provide
// it with one if necessary. // it with one if necessary.
@ -201,10 +203,10 @@ pub extern "C" fn trap_handler(
.take() .take()
.expect("got an instruction page fault with no previous PID") .expect("got an instruction page fault with no previous PID")
}; };
// println!( println!(
// "ISR: Resuming previous pair of ({}, {})", "ISR: Resuming previous pair of ({}, {})",
// previous_pid, previous_context previous_pid, previous_context
// ); );
// Switch to the previous process' address space. // Switch to the previous process' address space.
SystemServices::with_mut(|ss| { SystemServices::with_mut(|ss| {
ss.finish_callback_and_resume(previous_pid, previous_context) ss.finish_callback_and_resume(previous_pid, previous_context)
@ -229,6 +231,8 @@ pub extern "C" fn trap_handler(
loop {} loop {}
} else { } else {
let irqs_pending = sip::read(); let irqs_pending = sip::read();
println!("irqs: {:x}", irqs_pending);
// Safe to access globals since interrupts are disabled // Safe to access globals since interrupts are disabled
// when this function runs. // when this function runs.
unsafe { unsafe {

View file

@ -28,6 +28,28 @@ _start:
la t0, _start_trap la t0, _start_trap
csrw stvec, t0 csrw stvec, t0
/////// resume prep code
// if resume flag is false (0), skip this section
beq a3, zero, coldboot
// Restore a default stack pointer
li sp, 0xfffefffc
// setup sie ssoft, sext: bits 1 and 9 0b10_0000_0010
li t0, 0x202
csrw sie, t0
// restore the SIM with the previously socked away value
jal ra, _enable_all_irqs
// set scause to simulate an interrupt's occurence
li t0, 0x80000000
csrw scause, t0
// Note that registers $a0-$a7 still contain the arguments
j _start_trap_rust
coldboot:
// Jump to a program to initialize global variables. // Jump to a program to initialize global variables.
// Note that at this point, the .data section should // Note that at this point, the .data section should
// have already been set up by the loader. // have already been set up by the loader.

View file

@ -22,7 +22,7 @@ pub fn handle(irqs_pending: usize) -> Result<xous_kernel::Result, xous_kernel::E
return SystemServices::with_mut(|ss| { return SystemServices::with_mut(|ss| {
// Disable all other IRQs and redirect into userspace // Disable all other IRQs and redirect into userspace
arch::irq::disable_all_irqs(); arch::irq::disable_all_irqs();
// println!("Making a callback to PID{}: {:08x} ({:08x}, {:08x})", pid, f as usize, irq_no as usize, arg as usize); // println!("Making a callback to PID{}: {:x?} ({:08x}, {:x?})", pid, f, irq_no as usize, arg);
ss.make_callback_to( ss.make_callback_to(
pid, pid,
f.get() as *mut usize, f.get() as *mut usize,

View file

@ -131,6 +131,7 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs
arch::rand::get_u32(); arch::rand::get_u32();
if resume { if resume {
println!("Resume: enabling IRQs and kicking into the resume interrupt context");
// this will reload the SIM from its backing store in RAM // this will reload the SIM from its backing store in RAM
crate::arch::irq::enable_all_irqs(); crate::arch::irq::enable_all_irqs();
// we should already have our page mapped from a prior boot // we should already have our page mapped from a prior boot
@ -208,6 +209,7 @@ pub extern "C" fn kmain() {
Some(pid) => { Some(pid) => {
// #[cfg(feature = "debug-print")] // #[cfg(feature = "debug-print")]
// klog!("switching to pid {}", pid); // klog!("switching to pid {}", pid);
println!("switching to pid {}", pid);
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0)) xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0))
.expect("couldn't switch to pid"); .expect("couldn't switch to pid");
} }

View file

@ -532,6 +532,7 @@ impl SystemServices {
// Activate the current context // Activate the current context
arch_process.set_thread(arch::process::IRQ_TID).unwrap(); arch_process.set_thread(arch::process::IRQ_TID).unwrap();
// println!("Invoking...");
// Construct the new frame // Construct the new frame
arch::syscall::invoke( arch::syscall::invoke(
arch_process.current_thread_mut(), arch_process.current_thread_mut(),

View file

@ -1100,6 +1100,9 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
resume_csr.wfo(utra::susres::CONTROL_PAUSE, 1); // ensure that the ticktimer is paused before resuming resume_csr.wfo(utra::susres::CONTROL_PAUSE, 1); // ensure that the ticktimer is paused before resuming
resume_csr.wfo(utra::susres::EV_ENABLE_SOFT_INT, 1); // ensure that the soft interrupt is enabled for the kernel to kick resume_csr.wfo(utra::susres::EV_ENABLE_SOFT_INT, 1); // ensure that the soft interrupt is enabled for the kernel to kick
println!("clean suspend marker found, doing a resume!"); println!("clean suspend marker found, doing a resume!");
// trigger the interrupt; it's not immediately handled, but rather checked later on by the kernel on clean resume
resume_csr.wfo(utra::susres::INTERRUPT_INTERRUPT, 1);
// SATP restore is handled by the `start_kernel` assembly pre-amble // SATP restore is handled by the `start_kernel` assembly pre-amble
} }
@ -1160,11 +1163,21 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
for i in 0..7 { for i in 0..7 {
println!("0x{:08x}", (*backup_args)[i]); println!("0x{:08x}", (*backup_args)[i]);
} }
let satp = ((*backup_args)[3] as usize) & 0x803F_FFFF | (10 << 22);
//let satp = (*backup_args)[3];
println!("Adjusting SATP was: 0x{:08x} now: 0x{:08x}", (*backup_args)[3], satp);
if true {
use utralib::generated::*;
let mut gpio_csr = CSR::new(utra::gpio::HW_GPIO_BASE as *mut u32);
gpio_csr.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // patch us over to a different UART for debug (1=LOG 2=APP, 0=KERENL(default))
}
start_kernel( start_kernel(
(*backup_args)[0] as usize, (*backup_args)[0] as usize,
(*backup_args)[1] as usize, (*backup_args)[1] as usize,
(*backup_args)[2] as usize, (*backup_args)[2] as usize,
(*backup_args)[3] as usize, satp as usize,
(*backup_args)[4] as usize, (*backup_args)[4] as usize,
(*backup_args)[5] as usize, (*backup_args)[5] as usize,
if (*backup_args)[6] == 0 {false} else {true}, if (*backup_args)[6] == 0 {false} else {true},

View file

@ -76,7 +76,7 @@ fn rkyv_test_client() -> ! {
com.req_batt_stats().unwrap(); com.req_batt_stats().unwrap();
// let the loop run a bit, then try a suspend // let the loop run a bit, then try a suspend
if idx == 2 { if idx == 0 {
// TODO: add a self-wakeup RTC alarm once we're beyond the touch-and-go phase // TODO: add a self-wakeup RTC alarm once we're beyond the touch-and-go phase
susres.initiate_suspend().unwrap(); susres.initiate_suspend().unwrap();
} }

View file

@ -22,4 +22,4 @@ utralib = { path = "../../utralib"}
[features] [features]
debugprint = [] debugprint = []
default = [] default = ["debugprint"]

View file

@ -59,6 +59,7 @@ mod implementation {
// on a clean suspend then resume, the loader will queue up this interrupt to be the first thing to // on a clean suspend then resume, the loader will queue up this interrupt to be the first thing to
// run on Xous resume, with a bit in the RESUME register set. // run on Xous resume, with a bit in the RESUME register set.
fn susres_handler(_irq_no: usize, arg: *mut usize) { fn susres_handler(_irq_no: usize, arg: *mut usize) {
println!("susres handler");
let sr = unsafe{ &mut *(arg as *mut SusResHw) }; let sr = unsafe{ &mut *(arg as *mut SusResHw) };
// clear the pending interrupt // clear the pending interrupt
sr.csr.wfo(utra::susres::EV_PENDING_SOFT_INT, 1); sr.csr.wfo(utra::susres::EV_PENDING_SOFT_INT, 1);
@ -70,7 +71,8 @@ mod implementation {
} }
if sr.csr.rf(utra::susres::STATE_RESUME) == 0 { if sr.csr.rf(utra::susres::STATE_RESUME) == 0 {
{ // this is just for testing println!("going into suspend");
if false { // this is just for testing
let mut reboot_csr = CSR::new(REBOOT_CSR.load(Ordering::Relaxed) as *mut u32); let mut reboot_csr = CSR::new(REBOOT_CSR.load(Ordering::Relaxed) as *mut u32);
reboot_csr.wfo(utra::reboot::SOC_RESET_SOC_RESET, 0xAC); reboot_csr.wfo(utra::reboot::SOC_RESET_SOC_RESET, 0xAC);
} }
@ -80,6 +82,7 @@ mod implementation {
loop {} // block forever here loop {} // block forever here
} else { } else {
println!("going into resume");
// this unblocks the threads waiting on the resume // this unblocks the threads waiting on the resume
SHOULD_RESUME.store(true, Ordering::Relaxed); SHOULD_RESUME.store(true, Ordering::Relaxed);
} }
@ -498,7 +501,7 @@ pub fn execution_gate() {
match FromPrimitive::from_usize(msg.body.id()) { match FromPrimitive::from_usize(msg.body.id()) {
// the entire purpose of SupendingNow is to block the thread that sent the message, until we're ready to resume. // the entire purpose of SupendingNow is to block the thread that sent the message, until we're ready to resume.
Some(ExecGateOpcode::SuspendingNow) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { Some(ExecGateOpcode::SuspendingNow) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
println!("execution gated!"); println!("checking exec gate:");
while !RESUME_EXEC.load(Ordering::Relaxed) { while !RESUME_EXEC.load(Ordering::Relaxed) {
println!("execution gate active"); println!("execution gate active");
xous::yield_slice(); xous::yield_slice();

View file

@ -223,8 +223,7 @@ mod implementation {
pub fn schedule_response(&mut self, request: SleepRequest) { pub fn schedule_response(&mut self, request: SleepRequest) {
let irq_target = request.msec; let irq_target = request.msec;
#[cfg(feature = "debug-print")] log::trace!(
log::info!(
"setting a response at {} ms (current time: {} ms)", "setting a response at {} ms (current time: {} ms)",
irq_target, irq_target,
self.elapsed_ms() self.elapsed_ms()
@ -287,6 +286,16 @@ mod implementation {
self.wdt_sr_manager.resume(); self.wdt_sr_manager.resume();
self.ticktimer_sr_manager.resume(); self.ticktimer_sr_manager.resume();
#[cfg(feature = "watchdog")]
{ // do a watchdog reset right away
self.wdt.wfo(utra::wdt::EV_PENDING_SOFT_INT, 1);
self.wdt.wfo(utra::wdt::EV_ENABLE_SOFT_INT, 1);
self.wdt.wfo(utra::wdt::INTERRUPT_INTERRUPT, 1);
}
log::trace!("ticktimer enable: {}", self.csr.r(utra::ticktimer::EV_ENABLE));
log::trace!("ticktimer target: {}", self.csr.r(utra::ticktimer::MSLEEP_TARGET0));
} }
} }
} }
@ -511,6 +520,7 @@ fn xmain() -> ! {
//ticktimer.check_wdt(); //ticktimer.check_wdt();
let msg = xous::receive_message(ticktimer_server).unwrap(); let msg = xous::receive_message(ticktimer_server).unwrap();
log::trace!("msg: {:?}", msg);
match num_traits::FromPrimitive::from_usize(msg.body.id()) { match num_traits::FromPrimitive::from_usize(msg.body.id()) {
Some(api::Opcode::ElapsedMs) => { Some(api::Opcode::ElapsedMs) => {
let time = ticktimer.elapsed_ms() as i64; let time = ticktimer.elapsed_ms() as i64;