attempt to re-type the runtime page tracker to something we can use for swap

This is too expensive to use generically - we're going to have
to split the RPT, but checking in this change as a checkpoint
This commit is contained in:
bunnie 2024-05-12 16:27:30 +08:00
parent f6bfddcf20
commit 6080bc45d8
9 changed files with 109 additions and 83 deletions

View file

@ -45,7 +45,7 @@ pub struct BootConfig {
/// This structure keeps track of which pages are owned
/// and which are free. A PID of `0` indicates it's free.
pub runtime_page_tracker: &'static mut [XousPid],
pub runtime_page_tracker: &'static mut [XousAlloc],
/// A list of processes that were set up. The first element
/// is the kernel, and any subsequent elements are init processes.
@ -147,7 +147,8 @@ impl BootConfig {
}
// Mark this page as in-use by the kernel
let extra_bytes = self.extra_pages * PAGE_SIZE;
self.runtime_page_tracker[(self.sram_size - (extra_bytes + self.init_size)) / PAGE_SIZE] = 1;
self.runtime_page_tracker[(self.sram_size - (extra_bytes + self.init_size)) / PAGE_SIZE] =
XousAlloc::from(1);
// Return the address
pg as *mut usize
@ -157,7 +158,7 @@ impl BootConfig {
// First, check to see if the region is in RAM,
if addr >= self.sram_start as usize && addr < self.sram_start as usize + self.sram_size {
// Mark this page as in-use by the kernel
self.runtime_page_tracker[(addr - self.sram_start as usize) / PAGE_SIZE] = pid;
self.runtime_page_tracker[(addr - self.sram_start as usize) / PAGE_SIZE] = XousAlloc::from(pid);
return;
}
// The region isn't in RAM, so check the other memory regions.
@ -167,7 +168,7 @@ impl BootConfig {
let rstart = region.start as usize;
let rlen = region.length as usize;
if addr >= rstart && addr < rstart + rlen {
self.runtime_page_tracker[rpt_offset + (addr - rstart) / PAGE_SIZE] = pid;
self.runtime_page_tracker[rpt_offset + (addr - rstart) / PAGE_SIZE] = XousAlloc::from(pid);
return;
}
rpt_offset += rlen / PAGE_SIZE;
@ -242,7 +243,7 @@ impl BootConfig {
virt
);
}
let previous_flags = l0_pt[vpn0] & 0xf;
let previous_flags = l0_pt[vpn0] & 0x3f;
l0_pt[vpn0] = (ppn1 << 20) | (ppn0 << 10) | previous_flags | FLG_VALID;
}
@ -301,7 +302,7 @@ impl BootConfig {
virt
);
}
let previous_flags = l0_pt[vpn0] & 0xf;
let previous_flags = l0_pt[vpn0] & 0x3f;
l0_pt[vpn0] = (ppn1 << 20) | (ppn0 << 10) | flags | previous_flags | FLG_D | FLG_A;
// If we had to allocate a level 1 pagetable entry, ensure that it's

View file

@ -4,6 +4,23 @@
pub mod swap;
pub const PAGE_SIZE: usize = 4096;
pub type XousPid = u8;
#[cfg(not(feature = "swap"))]
pub type XousAlloc = XousPid;
#[cfg(feature = "swap")]
pub type XousAlloc = swap::SwapAlloc;
pub const FLG_VALID: usize = 0x1;
pub const FLG_X: usize = 0x8;
pub const FLG_W: usize = 0x4;
pub const FLG_R: usize = 0x2;
pub const FLG_U: usize = 0x10;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_A: usize = 0x40;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_D: usize = 0x80;
pub const FLG_P: usize = 0x200;
pub const FLG_SWAP_USED: u32 = 0x8000_0000;
// Locate the hard-wired IFRAM allocations for UDMA

View file

@ -23,20 +23,20 @@ mod phase1;
mod phase2;
mod platform;
#[cfg(feature = "swap")]
mod swap;
pub mod swap;
use core::{mem, ptr, slice};
use asm::*;
use bootconfig::BootConfig;
use consts::*;
pub use loader::PAGE_SIZE;
pub use loader::*;
use minielf::*;
use phase1::{phase_1, InitialProcess};
use phase2::{phase_2, ProgramDescription};
#[cfg(feature = "swap")]
use platform::SwapHal;
pub type XousPid = u8;
const WORD_SIZE: usize = mem::size_of::<usize>();
pub const SIGBLOCK_SIZE: usize = 0x1000;
const STACK_PAGE_COUNT: usize = 8;
@ -231,7 +231,9 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
// this heuristic within that range (any stack-stored pointer, for example, will break this).
for &check in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 64..].iter() {
assert!(
check <= cfg.processes.len() as u8,
// use .to_le() to access the structure because SwapAlloc can either be a u8 or a composite
// type, and .to_le() can do the right thing for both cases.
check.to_le() <= cfg.processes.len() as u8,
"RPT looks corrupted, suspect stack overflow in loader. Increase GUARD_MEMORY_BYTES!"
);
}

View file

@ -13,17 +13,6 @@ use armv7::{
use crate::*;
pub const FLG_VALID: usize = 0x1;
pub const FLG_X: usize = 0x8;
pub const FLG_W: usize = 0x4;
pub const FLG_R: usize = 0x2;
pub const FLG_U: usize = 0x10;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_A: usize = 0x40;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_D: usize = 0x80;
pub const FLG_P: usize = 0x200;
pub const MINIELF_FLG_W: u8 = 1;
#[allow(dead_code)]
pub const MINIELF_FLG_NC: u8 = 2;

View file

@ -101,7 +101,7 @@ pub fn phase_1(cfg: &mut BootConfig) {
// Lower numbered indices corresponding to higher address pages.
println!("Marking pages as in-use");
for i in 4..(cfg.init_size / PAGE_SIZE) {
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - i] = 1;
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - i] = XousAlloc::from(1);
}
}
@ -123,27 +123,46 @@ pub fn allocate_regions(cfg: &mut BootConfig) {
rpt_pages += region_length_rounded / 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);
#[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(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!(
"init_size bef: {:x}, ext pages: {:x}, rpt alloc: {:x}",
cfg.init_size,
cfg.extra_pages,
rpt_pages * mem::size_of::<XousAlloc>()
);
cfg.init_size += rpt_pages * mem::size_of::<XousPid>();
cfg.init_size += rpt_pages * mem::size_of::<XousAlloc>();
// Clear all memory pages such that they're not owned by anyone
let runtime_page_tracker = cfg.get_top();
println!("rpt value: {:x}", runtime_page_tracker as usize);
#[cfg(feature = "swap")]
{
assert!(runtime_page_tracker as usize & 0xFFF == 0); // this needs to be page-aligned for swap to work
}
assert!((runtime_page_tracker as usize) < (cfg.sram_start as usize) + cfg.sram_size);
unsafe {
bzero(runtime_page_tracker, runtime_page_tracker.add(rpt_pages / mem::size_of::<usize>()));
}
cfg.runtime_page_tracker =
unsafe { slice::from_raw_parts_mut(runtime_page_tracker as *mut XousPid, rpt_pages) };
unsafe { slice::from_raw_parts_mut(runtime_page_tracker as *mut XousAlloc, rpt_pages) };
#[cfg(feature = "swap")]
if SDBG {
println!(
" -> RPT range: {:x} - {:x}",
runtime_page_tracker as usize,
runtime_page_tracker as usize + rpt_pages * core::mem::size_of::<XousPid>()
runtime_page_tracker as usize + rpt_pages * core::mem::size_of::<XousAlloc>()
);
}
}

View file

@ -261,68 +261,34 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
swap_spec.sram_size = cfg.sram_size as u32;
}
// allocate swap offset tracker
// allocate swap count 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;
let mut swap_count_start = 0;
for offset in (0..(swap_size_usable / PAGE_SIZE) * core::mem::size_of::<u32>()).step_by(4096) {
let offset_page = cfg.alloc() as usize; // this also zeroes the page
let count_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;
// last iteration actually contains the base pointer of the count tracker
swap_count_start = count_page;
cfg.map_page(
root,
offset_page,
SWAP_OFFSET_VADDR + offset,
count_page,
SWAP_COUNT_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
// initialize which pages are used inside the count tracker by marking the MSB
// this is safe because the count 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)
let counts: &mut [u32] = unsafe {
core::slice::from_raw_parts_mut(swap_count_start as *mut u32, swap_size_usable / PAGE_SIZE)
};
for i in 0..cfg.last_swap_page {
offsets[i] = loader::FLG_SWAP_USED;
counts[i] = loader::FLG_SWAP_USED;
}
}
// copy the RPT into PID 2
{
// safety: This is safe because we know that:
// - RPT is fully initialized at this point
// - rpt_alias is scoped in a block so it can't be abused later by accident
// - We are only going to read data from the alias pointer
// - There is no borrow conflict in the upcoming for loop
// This is done to avoid adding a RefCell around the runtime page tracker to work around
// interior mutability issues in a very performance-sensitive region, and in a manner that
// 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 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,
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
crate::platform::userspace_maps(cfg);
}
@ -347,9 +313,9 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
}
println!("Runtime Page Tracker: {} bytes", cfg.runtime_page_tracker.len());
// mark pages used by suspend/resume according to their needs
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 1] = 1; // claim the loader stack -- do not allow tampering, as it contains backup kernel args
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 2] = 1; // 8k in total (to allow for digital signatures to be computed)
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 3] = 0; // allow clean suspend page to be mapped in Xous
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 1] = XousAlloc::from(1); // claim the loader stack -- do not allow tampering, as it contains backup kernel args
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 2] = XousAlloc::from(1); // 8k in total (to allow for digital signatures to be computed)
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 3] = XousAlloc::from(0); // allow clean suspend page to be mapped in Xous
}
/// This describes the kernel as well as initially-loaded processes

View file

@ -65,7 +65,8 @@ impl BootConfig {
}
// Mark this page as in-use by the kernel
let extra_bytes = self.extra_pages * PAGE_SIZE;
self.runtime_page_tracker[(self.sram_size - (extra_bytes + self.init_size)) / PAGE_SIZE] = 1;
self.runtime_page_tracker[(self.sram_size - (extra_bytes + self.init_size)) / PAGE_SIZE] =
XousPid::from(1);
dprintln!("Allocated a physical page: {:08x}", pg as usize);
@ -126,7 +127,7 @@ impl BootConfig {
// First, check to see if the region is in RAM,
if addr >= self.sram_start as usize && addr < self.sram_start as usize + self.sram_size {
// Mark this page as in-use by the PID
self.runtime_page_tracker[(addr - self.sram_start as usize) / PAGE_SIZE] = pid;
self.runtime_page_tracker[(addr - self.sram_start as usize) / PAGE_SIZE] = XousPid::from(pid);
return;
}
// The region isn't in RAM, so check the other memory regions.
@ -136,7 +137,7 @@ impl BootConfig {
let rstart = region.start as usize;
let rlen = region.length as usize;
if addr >= rstart && addr < rstart + rlen {
self.runtime_page_tracker[rpt_offset + (addr - rstart) / PAGE_SIZE] = pid;
self.runtime_page_tracker[rpt_offset + (addr - rstart) / PAGE_SIZE] = XousPid::from(pid);
return;
}
rpt_offset += rlen / PAGE_SIZE;

View file

@ -169,8 +169,8 @@ impl SwapHal {
/// Used to examine contents of swap RAM. Swap count is fixed at 0 by this routine. Decrypted data is
/// returned as a slice.
pub fn decrypt_swap_from(&mut self, src_offset: usize, dst_vaddr: usize, dst_pid: u8) -> &[u8] {
// println!("Decrypt swap:");
// println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid);
println!("Decrypt swap:");
println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid);
assert!(src_offset & (PAGE_SIZE - 1) == 0);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field

View file

@ -1,7 +1,9 @@
use core::fmt;
use core::mem::size_of;
use aes_gcm_siv::Tag;
use crate::XousPid;
use crate::PAGE_SIZE;
/// Virtual address fields:
@ -48,7 +50,7 @@ pub const SWAP_PT_VADDR: usize = 0xE000_0000;
// E000_0000 - E100_0000 => 16 MiB of vaddr space for page tables; should be more than enough
pub const SWAP_CFG_VADDR: usize = 0xE100_0000;
pub const SWAP_RPT_VADDR: usize = 0xE100_1000;
pub const SWAP_OFFSET_VADDR: usize = 0xE110_0000;
pub const SWAP_COUNT_VADDR: usize = 0xE110_0000;
pub const SWAP_APP_UART_VADDR: usize = 0xE180_0000;
#[cfg(feature = "cramium-soc")]
pub const SWAP_APP_UART_IFRAM_VADDR: usize = 0xE180_1000;
@ -104,3 +106,32 @@ pub fn derive_usable_swap(swap_len: usize) -> usize {
}
pub fn derive_mac_size(swap_len: usize) -> usize { (swap_len / 4096) * size_of::<Tag>() }
/// This needs to be synchronized with what's in kernel/src/mem.rs
pub const SWAP_FLG_WIRED: u32 = 0x1_00;
#[derive(Copy, Clone)]
pub struct SwapAlloc {
timestamp: u32,
/// virtual_page_number[19:0] | flags[3:0] | pid[7:0]
vpn: u32,
}
impl SwapAlloc {
pub fn from(pid: u8) -> SwapAlloc { SwapAlloc { timestamp: 0, vpn: pid as u32 | SWAP_FLG_WIRED } }
/// This is a slight abuse of the naming system to provide us cross-compatibility with the case where the
/// structure is defined as an overload of the `u8` type
pub fn to_le(&self) -> u8 { self.vpn as u8 }
}
impl fmt::Debug for SwapAlloc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SwapAlloc")
.field("pid", &(self.vpn as u8))
.field("vaddr", &(self.vpn & !0xFFF))
.field("flags", &(if self.vpn & SWAP_FLG_WIRED != 0 { "WIRED" } else { "NONE" }))
.finish()
}
}