map offset tracker into swapper state

this is allocated by the loader for the swapper. This should
be the last big piece of state we need to get the swapper to work...
honestly, it's already a pretty heavy bit of code :( we should
probably optimize out the RPT hand-off to not include all of
the non-swappable memory pages, which is quite substantial in
the case of some SoCs.

also refactor to use the loader's computation function for
the MAC offset in swap, instead of copying this hairy bit of
logic like....8 times over throughout the code base, yeaaach!
This commit is contained in:
bunnie 2024-05-09 02:09:43 +08:00
parent 95c27c765e
commit 87b53e77a4
3 changed files with 24 additions and 15 deletions

View file

@ -9,7 +9,7 @@ use std::{fmt::Debug, num::NonZeroU8};
use debug::*;
use heapless::LinearMap;
use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR};
use loader::swap::{SwapSpec, SWAP_CFG_VADDR, SWAP_OFFSET_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR};
use lru::LruCache;
use num_traits::*;
use platform::{SwapHal, PAGE_SIZE};
@ -58,6 +58,10 @@ pub struct RuntimePageTracker {
pub allocs: &'static mut [Option<PID>],
}
pub struct OffsetTracker {
pub offsets: &'static mut [u32],
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
#[repr(usize)]
pub enum Opcode {
@ -79,6 +83,7 @@ pub struct SwapperSharedState {
pub pts: SwapPageTables,
pub hal: SwapHal,
pub rpt: RuntimePageTracker,
pub ot: OffsetTracker,
pub sram_start: usize,
pub sram_size: usize,
pub swap_size: usize,
@ -196,6 +201,18 @@ fn swap_handler(
)
},
},
// safety: this is safe because the loader has allocated this region and zeroed the contents,
// and the length is correctly set up by the loader. Note that the length is slightly
// longer than it needs to be -- the region that has to be tracked does not include the
// area of swap dedicated to the MAC table, which swap_len includes.
ot: OffsetTracker {
offsets: unsafe {
core::slice::from_raw_parts_mut(
SWAP_OFFSET_VADDR as *mut u32,
loader::swap::derive_usable_swap(swap_spec.swap_len as usize) / PAGE_SIZE,
)
},
},
sram_start: swap_spec.sram_start as usize,
sram_size: swap_spec.sram_size as usize,
swap_size: swap_spec.swap_len as usize,
@ -359,14 +376,10 @@ fn main() {
// The cache is keyed by physical page number; the values are an Option<(PID, vaddr)> tuple that
// corresponds to the current owner of that page.
let total_ram = sss.inner.as_ref().unwrap().sram_size;
let total_swap = sss.inner.as_ref().unwrap().swap_size;
let mut lru = LruCache::new(NonZeroUsize::new(total_ram / PAGE_SIZE).unwrap());
// Offset tracker
// Track an offset count for every page that gets written to swap.
// TODO: find a way to bind this into the swapper's exclusive state in the interrupt context...maybe
// even just have the loader allocate this block directly?
let mut offset = vec![0u32; total_swap / PAGE_SIZE];
/*
for (index, pa) in (start..(start + total)).step_by(PAGE_SIZE).enumerate() {
if let Some(pid) = sss.inner.as_ref().unwrap().rpt.allocs[index] {

View file

@ -24,9 +24,7 @@ impl SwapHal {
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
// because once we remove the MAC area, we need even less storage, but it's a small error.
let mac_size = (spec.swap_len as usize / 4096) * size_of::<Tag>();
let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1);
let ram_size_actual = (spec.swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page;
let ram_size_actual = loader::swap::derive_usable_swap(spec.swap_len as usize);
#[cfg(feature = "spi-alt-channel")]
let channel = SpimChannel::Channel0;

View file

@ -14,12 +14,7 @@ pub struct SwapHal {
}
impl SwapHal {
pub fn new(spec: &SwapSpec) -> Self {
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
// because once we remove the MAC area, we need even less storage, but it's a small error.
let mac_size = (spec.swap_len as usize / 4096) * size_of::<Tag>();
let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1);
let ram_size_actual = (spec.swap_len as usize & !(PAGE_SIZE - 1)) - mac_size_to_page;
let ram_size_actual = loader::swap::derive_usable_swap(spec.swap_len as usize);
Self {
// safety: the ram swap area is pre-mapped into our virtual address by the loader, and our
// calculations on lengths ensure area alignment
@ -29,7 +24,10 @@ impl SwapHal {
// safety: the ram swap area is pre-mapped into our virtual address by the loader, and our
// calculations on lengths ensure area alignment
dst_mac_area: unsafe {
core::slice::from_raw_parts_mut((SWAP_HAL_VADDR as *mut u8).add(ram_size_actual), mac_size)
core::slice::from_raw_parts_mut(
(SWAP_HAL_VADDR as *mut u8).add(ram_size_actual),
loader::swap::derive_mac_size(spec.swap_len as usize),
)
},
cipher: Aes256GcmSiv::new((&spec.key).into()),
}