diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 8165d99f0..142cd7d43 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -729,7 +729,13 @@ impl SystemServices { let sp = if pid.get() == 1 { EXCEPTION_STACK_TOP } else { - arch_process.current_thread().stack_pointer() + match cb_type { + // for swap, we have to provide our own stack, because we can have a fault + // *inside* an IRQ handler, which leaves us with no stack (as the IRQ handler + // is already planning on vampiring off an existing thread's stack). + CallbackType::Swap(_) | CallbackType::SwapInIrq(_) => SWAP_STACK_TOP_VADDR, + _ => arch_process.current_thread().stack_pointer(), + } }; // Activate the current context diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 1c8b0490a..07e39d41d 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -177,6 +177,8 @@ pub fn phase_2(cfg: &mut BootConfig, env_variables: EnvVariables) { } #[cfg(feature = "swap")] { + use xous::arch::SWAP_STACK_TOP_VADDR; + // map the swap page table into PID space 2 let tt_address = cfg.processes[SWAPPER_PID as usize - 1].satp << 12; let root = unsafe { &mut *(tt_address as *mut PageTable) }; @@ -277,6 +279,18 @@ pub fn phase_2(cfg: &mut BootConfig, env_variables: EnvVariables) { } } + // allocate swap private stack + for i in 0..2 { + let stack_page = cfg.alloc() as usize; + cfg.map_page( + root, + stack_page, + SWAP_STACK_TOP_VADDR - PAGE_SIZE * (i + 1), + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); + } + // map any hardware-specific pages into the userspace swapper crate::platform::userspace_maps(cfg); } diff --git a/services/xous-swapper/Cargo.toml b/services/xous-swapper/Cargo.toml index 8ea2401e8..e87c1c082 100644 --- a/services/xous-swapper/Cargo.toml +++ b/services/xous-swapper/Cargo.toml @@ -47,6 +47,6 @@ oom-doom = [] debug-verbose = [] # mutually exclusive feature with other crates that use the app UART -# *** requires "userspace-debug" to be passed to the loader for this to work. *** +# *** requires "userspace-swap-debug" to be passed to the loader for this to work. *** debug-print-swapper = [] default = [] diff --git a/services/xous-swapper/src/lib.rs b/services/xous-swapper/src/lib.rs index 298acecd5..9a24c2ecc 100644 --- a/services/xous-swapper/src/lib.rs +++ b/services/xous-swapper/src/lib.rs @@ -67,10 +67,7 @@ pub enum Opcode { Test0 = 3, // kernel has to be compiled with "debug-proc" for these to work // the Debug* series of opcodes are hard-coded in other crates, so don't change their discriminant. - DebugProcesses = 9, - DebugServers = 10, - DebugFree = 11, - DebugInterrupts = 12, + DebugServers = 9, None = 255, } diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index 5be061593..9eb4d4899 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -582,7 +582,16 @@ fn swap_handler( reserved_mem, ) .ok(); - xous::unmap_memory(reserved_mem).expect("couldn't free memory for hard OOM handler"); + match xous::unmap_memory(reserved_mem) { + Ok(_) => {} + Err(e) => { + // unmap_memory doesn't work in an IRQ context - so this errors out. Let's + // just pray we have enough free memory for the handler to do its thing? + writeln!(DebugUart {}, "Likely in IRQ context: {:?} ***************", e).ok(); + // replace the pages because they weren't unmapped + ss.hard_oom_reserved_page = Some(reserved_mem); + } + }; } else { panic!("No space was reserved for the hard OOM manager to run!"); } @@ -645,17 +654,19 @@ fn swap_handler( ) .ok(); // Restore some reserved memory for the next hard OOM invocation. - let mut reserved = xous::map_memory( - None, - None, - PAGE_SIZE * HARD_OOM_RESERVED_PAGES, - MemoryFlags::R | MemoryFlags::W | MemoryFlags::RESERVE, - ) - .expect("could't reserve space for hard OOM handler"); - // *touch* the memory -- otherwise it might not actually be demand-paged - let reserved_slice: &mut [u32] = unsafe { reserved.as_slice_mut() }; // this is safe because `u32` is fully representable - reserved_slice.fill(0); - ss.hard_oom_reserved_page = Some(reserved); + if ss.hard_oom_reserved_page.is_none() { + let mut reserved = xous::map_memory( + None, + None, + PAGE_SIZE * HARD_OOM_RESERVED_PAGES, + MemoryFlags::R | MemoryFlags::W | MemoryFlags::RESERVE, + ) + .expect("could't reserve space for hard OOM handler"); + // *touch* the memory -- otherwise it might not actually be demand-paged + let reserved_slice: &mut [u32] = unsafe { reserved.as_slice_mut() }; // this is safe because `u32` is fully representable + reserved_slice.fill(0); + ss.hard_oom_reserved_page = Some(reserved); + } if ss.report_full_rpt { ss.report_full_rpt = false; } @@ -822,21 +833,10 @@ fn main() { .expect("couldn't Block Erase region"); } } - Some(Opcode::DebugProcesses) => { - xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugProcesses as usize, 0, 0, 0, 0, 0, 0)) - .unwrap(); - } Some(Opcode::DebugServers) => { xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugServers as usize, 0, 0, 0, 0, 0, 0)) .unwrap(); } - Some(Opcode::DebugFree) => { - xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugFree as usize, 0, 0, 0, 0, 0, 0)).unwrap(); - } - Some(Opcode::DebugInterrupts) => { - xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugInterrupts as usize, 0, 0, 0, 0, 0, 0)) - .unwrap(); - } #[cfg(feature = "swap-userspace-testing")] Some(Opcode::Test0) => { log::info!("Free mem: {}kiB", get_free_pages() * PAGE_SIZE / 1024); diff --git a/xous-rs/src/arch/riscv/mem.rs b/xous-rs/src/arch/riscv/mem.rs index f7c330a87..c4445aadd 100644 --- a/xous-rs/src/arch/riscv/mem.rs +++ b/xous-rs/src/arch/riscv/mem.rs @@ -17,6 +17,7 @@ pub const PAGE_SIZE: usize = 4096; pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000; pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000; pub const THREAD_CONTEXT_AREA: usize = 0xff80_1000; +pub const USERSPACE_BUFFER: usize = 0xff90_0000; pub const FLG_VALID: usize = 0x1; pub const FLG_R: usize = 0x2; @@ -37,6 +38,7 @@ pub const SWAP_RPT_VADDR: usize = 0xE100_1000; pub const SWAP_COUNT_VADDR: usize = 0xE110_0000; pub const SWAP_APP_UART_VADDR: usize = 0xE180_0000; pub const SWAP_APP_UART_IFRAM_VADDR: usize = 0xE180_1000; +pub const SWAP_STACK_TOP_VADDR: usize = 0xE800_0000; pub fn map_memory_pre( _phys: &Option,