fix RPT/XPT alloc in swap; make swap resume-compatible

This commit is contained in:
bunnie 2024-05-12 21:50:53 +08:00
parent 2ff46c4b8f
commit 6bef111bf2
5 changed files with 42 additions and 30 deletions

View file

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

View file

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

View file

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

View file

@ -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::<usize>() - 1) & !(mem::size_of::<usize>() - 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::<usize>() - 1) & !(mem::size_of::<usize>() - 1);
cfg.init_size += rpt_pages * mem::size_of::<XousAlloc>();
}
#[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::<XousAlloc>()
);
println!("RPT raw pages: {:x}", rpt_pages);
cfg.init_size += rpt_pages * mem::size_of::<XousAlloc>();
// 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::<XousAlloc>();
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::<XousAlloc>(),
);
}
// 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::<usize>()));

View file

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