diff --git a/loader/src/lib.rs b/loader/src/lib.rs index 98172fda7..dc92a60f1 100644 --- a/loader/src/lib.rs +++ b/loader/src/lib.rs @@ -4,6 +4,7 @@ pub mod swap; pub const PAGE_SIZE: usize = 4096; +pub const FLG_SWAP_USED: u32 = 0x8000_0000; // Locate the hard-wired IFRAM allocations for UDMA #[allow(dead_code)] diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 5c0403d84..cf6d6b486 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -252,7 +252,8 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { if let Some(desc) = cfg.swap { swap_spec.key.copy_from_slice(&cfg.swap_hal.as_ref().unwrap().get_swap_key()); swap_spec.pid_count = cfg.init_process_count as u32 + 1; - swap_spec.rpt_len_bytes = cfg.runtime_page_tracker.len() as u32; + // we only track main ram area (which is also the only swappable memory) to save space + swap_spec.rpt_len_bytes = (cfg.sram_size / 4096) as u32; swap_spec.swap_base = desc.ram_offset; swap_spec.swap_len = desc.ram_size; (swap_spec.mac_base, swap_spec.mac_len) = cfg.swap_hal.as_ref().unwrap().mac_base_bounds(); @@ -260,6 +261,35 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { swap_spec.sram_size = cfg.sram_size as u32; } + // allocate swap offset tracker + { + // note that this computation is mirrored in xous-swapper + let swap_size_usable = crate::swap::derive_usable_swap(swap_spec.swap_len as usize); + let mut offset_start = 0; + for offset in (0..(swap_size_usable / PAGE_SIZE) * core::mem::size_of::()).step_by(4096) { + let offset_page = cfg.alloc() as usize; // this also zeroes the page + // we want the start page to keep refreshing, because allocations grow *down*, so the + // last iteration actually contains the base pointer of the offset tracker + offset_start = offset_page; + cfg.map_page( + root, + offset_page, + SWAP_OFFSET_VADDR + offset, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); + } + // initialize which pages are used inside the offset tracker by marking the MSB + // this is safe because the offset range is contiguous (just freshly allocated), initialized + // to 0's, and all values are representable inside u32 + let offsets: &mut [u32] = unsafe { + core::slice::from_raw_parts_mut(offset_start as *mut u32, swap_size_usable / PAGE_SIZE) + }; + for i in 0..cfg.last_swap_page { + offsets[i] = loader::FLG_SWAP_USED; + } + } + // copy the RPT into PID 2 { // safety: This is safe because we know that: @@ -272,39 +302,25 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { // only ever affects the `swap` configuration (i.e., if we did this as a RefCell we'd have // to pay the performance penalty for all configurations, and/or we'd have to add `cfg` // directives in lots of places). Mea culpa, this is not how you're supposed to write Rust. - let terrible_rpt_alias = unsafe { - core::slice::from_raw_parts(cfg.runtime_page_tracker.as_ptr(), cfg.runtime_page_tracker.len()) - }; - for (i, rpt_page) in terrible_rpt_alias.chunks(4096).enumerate() { + let mut rpt_start = 0; + for i in (0..swap_spec.rpt_len_bytes as usize).step_by(4096) { let swap_rpt = cfg.alloc() as usize; + // track rpt_start as the allocator allocates *down* + rpt_start = swap_rpt; cfg.map_page( root, swap_rpt, - SWAP_RPT_VADDR + i * 4096, - FLG_R | FLG_W | FLG_U | FLG_VALID, - SWAPPER_PID, - ); - // safety: this is safe because the allocator aligns it and initializes it, and all underlying - // data has defined behavior as initialized - let dest = unsafe { core::slice::from_raw_parts_mut(swap_rpt as *mut u8, 4096) }; - dest[..rpt_page.len()].copy_from_slice(rpt_page); - } - } - - // allocate swap offset tracker - { - // note that this computation is mirrored in xous-swapper - let swap_size_usable = crate::swap::derive_usable_swap(swap_spec.swap_len as usize); - for offset in (0..(swap_size_usable / PAGE_SIZE) * core::mem::size_of::()).step_by(4096) { - let offset_page = cfg.alloc() as usize; // this also zeroes the page - cfg.map_page( - root, - offset_page, - SWAP_OFFSET_VADDR + offset, + SWAP_RPT_VADDR + i, FLG_R | FLG_W | FLG_U | FLG_VALID, SWAPPER_PID, ); } + // safety: this is safe because the allocator aligns it and initializes it, and all underlying + // data has defined behavior as initialized, and all the pages are contiguous + let rpt_copy: &mut [u8] = unsafe { + core::slice::from_raw_parts_mut(rpt_start as *mut u8, swap_spec.rpt_len_bytes as usize) + }; + rpt_copy.copy_from_slice(&cfg.runtime_page_tracker[..swap_spec.rpt_len_bytes as usize]); } // map any hardware-specific pages into the userspace swapper