From 6bef111bf239e72676bd7f57751396123b35bbbf Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 12 May 2024 21:50:53 +0800 Subject: [PATCH] fix RPT/XPT alloc in swap; make swap resume-compatible --- loader/src/consts.rs | 7 ------- loader/src/lib.rs | 14 ++++++++++++++ loader/src/main.rs | 7 +------ loader/src/phase1.rs | 40 ++++++++++++++++++++++++---------------- loader/src/phase2.rs | 4 +++- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/loader/src/consts.rs b/loader/src/consts.rs index 1a87b91a9..b4531bcd3 100644 --- a/loader/src/consts.rs +++ b/loader/src/consts.rs @@ -14,10 +14,3 @@ pub const KERNEL_STACK_TOP: usize = 0xfff8_0000; pub const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; pub const KERNEL_STACK_PAGE_COUNT: usize = 1; pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; -#[cfg(not(feature = "swap"))] -pub const GUARD_MEMORY_BYTES: usize = 3 * crate::PAGE_SIZE; -#[cfg(feature = "swap")] -pub const GUARD_MEMORY_BYTES: usize = 7 * crate::PAGE_SIZE; // this is bare minimum. Increase more if we want to do resume! - -#[cfg(feature = "swap")] -pub const SWAPPER_PID: u8 = 2; diff --git a/loader/src/lib.rs b/loader/src/lib.rs index 05ad4bd7c..420d0b792 100644 --- a/loader/src/lib.rs +++ b/loader/src/lib.rs @@ -30,3 +30,17 @@ pub const UART_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_M #[allow(dead_code)] #[cfg(feature = "cramium-soc")] pub const APP_UART_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 3 * 4096; + +/// This is the amount of space that the loader stack will occupy as it runs, assuming no swap and giving one +/// page for the clean suspend marker +#[cfg(not(feature = "swap"))] +pub const GUARD_MEMORY_BYTES: usize = 3 * crate::PAGE_SIZE; +/// Amount of space for loader stack only, with swap +#[cfg(all(feature = "swap", not(feature = "resume")))] +pub const GUARD_MEMORY_BYTES: usize = 7 * crate::PAGE_SIZE; +/// Amount of space for loader stack plus clean suspend, with swap +#[cfg(all(feature = "swap", feature = "resume"))] +pub const GUARD_MEMORY_BYTES: usize = 8 * crate::PAGE_SIZE; // 1 extra page for clean suspend + +#[cfg(feature = "swap")] +pub const SWAPPER_PID: u8 = 2; diff --git a/loader/src/main.rs b/loader/src/main.rs index 920414746..9d279d463 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -132,11 +132,6 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - { cfg.swap_hal = SwapHal::new(&cfg); read_swap_config(&mut cfg); - #[cfg(feature = "resume")] - compile_error!( - "WARNING WARNING WARNING: swap and resume selected - this is not a valid configuration because\ - the stack overlaps into the 'clean suspend' marker with swap. This needs to be fixed if resume is desired with swap." - ); } // check to see if we are recovering from a clean suspend or not @@ -454,7 +449,7 @@ fn check_resume(cfg: &mut BootConfig) -> (bool, bool, u32) { const NUM_SECTORS: usize = 8; const WORDS_PER_PAGE: usize = PAGE_SIZE / 4; - let suspend_marker = cfg.sram_start as usize + cfg.sram_size - PAGE_SIZE * 3; + let suspend_marker = cfg.sram_start as usize + cfg.sram_size - GUARD_MEMORY_BYTES; let marker: *mut [u32; WORDS_PER_PAGE] = suspend_marker as *mut [u32; WORDS_PER_PAGE]; let boot_seed = CSR::new(utra::seed::HW_SEED_BASE as *mut u32); diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 0c40e301e..32f4f3c22 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -33,8 +33,9 @@ pub struct InitialProcess { pub fn phase_1(cfg: &mut BootConfig) { // Allocate space for the stack pointer. // The bootloader should have placed the stack pointer at the end of RAM - // prior to jumping to our program, so allocate one page of data for - // stack. + // prior to jumping to our program. Reserve space for the stack, so that it does not smash + // run time allocations. + // // All other allocations will be placed below the stack pointer. // // As of Xous 0.8, the top page is bootloader stack, and the page below that is the 'clean suspend' page. @@ -110,27 +111,34 @@ pub fn phase_1(cfg: &mut BootConfig) { pub fn allocate_regions(cfg: &mut BootConfig) { // Number of individual pages in the system let mut rpt_pages = cfg.sram_size / PAGE_SIZE; + // Round the tracker to a multiple of the pointer size, so as to keep memory + // operations fast. + rpt_pages = (rpt_pages + mem::size_of::() - 1) & !(mem::size_of::() - 1); // allocate the RPT #[cfg(not(feature = "swap"))] { - // Round the tracker to a multiple of the pointer size, so as to keep memory - // operations fast. - rpt_pages = (rpt_pages + mem::size_of::() - 1) & !(mem::size_of::() - 1); + cfg.init_size += rpt_pages * mem::size_of::(); } #[cfg(feature = "swap")] { - // Round the tracker to a multiple of the page size, so that it can be mapped into userspace - rpt_pages = (rpt_pages + PAGE_SIZE - 1) & !(PAGE_SIZE - 1); - } - println!( - "RPT ALLOC init_size before: {:x}, ext pages: {:x}, rpt alloc: {:x}", - cfg.init_size, - cfg.extra_pages, - rpt_pages * mem::size_of::() - ); + println!("RPT raw pages: {:x}", rpt_pages); - cfg.init_size += rpt_pages * mem::size_of::(); + // Round the allocation to a multiple of the page size, so that it can be mapped into userspace + let proposed_alloc = rpt_pages * mem::size_of::(); + let page_aligned_alloc = (proposed_alloc + PAGE_SIZE - 1) & !(PAGE_SIZE - 1); + assert!(cfg.init_size & 0xFFF == 0, "init_size should be page aligned going into the RPT alloc"); + cfg.init_size += page_aligned_alloc; + assert!(cfg.init_size & 0xFFF == 0, "init_size should be page aligned leaving the RPT alloc"); + println!( + "RPT ALLOC init_size before: {:x}, ext pages: {:x}, minimal alloc: {:x}, page-aligned alloc: {:x}, sizeof entry: {:x}", + cfg.init_size, + cfg.extra_pages, + proposed_alloc, + page_aligned_alloc, + mem::size_of::(), + ); + } // Clear all memory pages such that they're not owned by anyone let runtime_page_tracker = cfg.get_top(); @@ -182,7 +190,7 @@ pub fn allocate_regions(cfg: &mut BootConfig) { // Clear all memory pages such that they're not owned by anyone let extra_page_tracker = cfg.get_top(); - println!("rpt value: {:x}", extra_page_tracker as usize); + println!("xpt value: {:x}", extra_page_tracker as usize); assert!((extra_page_tracker as usize) < (cfg.sram_start as usize) + cfg.sram_size); unsafe { bzero(extra_page_tracker, extra_page_tracker.add(xpt_pages / mem::size_of::())); diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 82f258c79..799968de2 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -147,7 +147,9 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { // Map boot-generated kernel structures into the kernel let satp = unsafe { &mut *(krn_l1_pt_addr as *mut PageTable) }; let kernel_arg_extents = cfg.init_size - (GUARD_MEMORY_BYTES + cfg.swap_offset); - assert!(kernel_arg_extents <= 0xA000, "Kernel init structures exceeded allocated region"); + println!("Kernel argument extents: {:x}", kernel_arg_extents); + // this is just a manual sanity check, the actual limit varies depending on system config parameters + assert!(kernel_arg_extents <= 0xD000, "Kernel init structures exceeded allocated region"); for addr in (0..kernel_arg_extents).step_by(PAGE_SIZE as usize) { cfg.map_page( satp,