first pass at a format + passing trivial built-in tests

This commit is contained in:
bunnie 2021-11-11 23:33:01 +08:00
parent d45369a19d
commit 55f2314e7c
8 changed files with 81 additions and 28 deletions

View file

@ -87,7 +87,7 @@ mod tests {
assert!(core::mem::size_of::<PddbBuf>() == 4096, "PddBuf record has the wrong size"); assert!(core::mem::size_of::<PddbBuf>() == 4096, "PddBuf record has the wrong size");
} }
fn test_pddb_len() { fn test_pddb_len() {
assert!(PDDB_A_LEN <= xous::PDDB_LEN, "PDDB_A_LEN is larger than the maximum extents available in the hardware"); assert!(PDDB_A_LEN <= xous::PDDB_LEN as usize, "PDDB_A_LEN is larger than the maximum extents available in the hardware");
} }
} }
impl PddbBuf { impl PddbBuf {

View file

@ -14,8 +14,9 @@ pub(crate) use murmur3::*;
mod trngpool; mod trngpool;
pub(crate) use trngpool::*; pub(crate) use trngpool::*;
#[cfg(any(target_os = "none", target_os = "xous"))] //#[cfg(any(target_os = "none", target_os = "xous"))]
mod hw; mod hw;
pub(crate) use hw::*; pub(crate) use hw::*;
// TODO: the alternative back-end PddbOs structures for hosted mode. // TODO: the alternative back-end PddbOs structures for hosted mode.

View file

@ -40,8 +40,9 @@ impl From<SpaceState> for u8 {
} }
} }
pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = ((PAGE_SIZE * FASTSPACE_PAGES) pub(crate) const FASTSPACE_FREE_POOL_LEN: usize =
- (size_of::<Nonce>() + size_of::<Tag>())) / core::mem::size_of::<PhysPage>(); ((PAGE_SIZE * FASTSPACE_PAGES) - (size_of::<Nonce>() + size_of::<Tag>()))
/ core::mem::size_of::<PhysPage>();
/// FastSpace tracks a limited set of physical pages /// FastSpace tracks a limited set of physical pages
/// An optimal implementation of this would fill whole pages with the free_pool array. /// An optimal implementation of this would fill whole pages with the free_pool array.
/// This record is meant to be updated rarely, and atomically, in a make-before-break fashion: /// This record is meant to be updated rarely, and atomically, in a make-before-break fashion:
@ -157,6 +158,6 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_fast_space_size() { fn test_fast_space_size() {
assert!(core::mem::size_of::<FastSpaceInFlash>() & !(PAGE_SIZE - 1) == 0, "FastSpace is not exactly a multiple of one page in size"); assert!(core::mem::size_of::<FastSpaceInFlash>() & (PAGE_SIZE - 1) == 0, "FastSpaceInFlash is not exactly a multiple of one page in size");
} }
} }

View file

@ -803,26 +803,17 @@ impl PddbOs {
} }
// step 9. generate & write initial page table entries // step 9. generate & write initial page table entries
// page table organization: if let Some(system_key) = self.system_basis_key {
// let cipher = Aes256::new(GenericArray::from_slice(&system_key));
// offset from | if let Some(basis_v2p_map) = self.v2p_map.get(&basis_root.name) {
// pt_phys_base| contents (example for total PDDB len of 0x6f8_0000 or 111 MiB) for (&virt, &phys) in basis_v2p_map.into_iter() {
// ------------|--------------------------------------------------- let mut pte = Pte::new(virt, PtFlags::CLEAN, Rc::clone(&self.entropy));
// 0x0000_0000 | virtual map for page at (0x0000 + data_phys_base) let mut block = Block::from_mut_slice(pte.deref_mut());
// 0x0000_0010 | virtual map for page at (0x1000 + data_phys_base) cipher.encrypt_block(block);
// 0x0000_0020 | virtual map for page at (0x2000 + data_phys_base) self.patch_pagetable(&block, phys.page_number() * aes::BLOCK_SIZE as u32);
// ... }
// 0x0006_F7F0 | virtual map for page at (0x06F7_F000 + data_phys_base) }
// 0x0006_F800 | unused }
// ...
// 0x0007_0000 | key page
// ...
// 0x0007_1000 | mbbb start (example of 10 pages)
// ...
// 0x0007_B000 | fscb start (example of 10 pages)
// ...
// 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region
Ok(()) Ok(())
} }

View file

@ -60,7 +60,7 @@ fn calc_k(k: u32) -> u32 {
mod tests { mod tests {
use super::*; use super::*;
struct Result { struct Result {
slice: &'static [u32], slice: &'static [u8],
hash_32: u32, hash_32: u32,
} }

View file

@ -1,6 +1,10 @@
use super::PAGE_SIZE; use super::{PAGE_SIZE, TrngPool, VirtAddr, murmur3_32};
use core::mem::size_of; use core::mem::size_of;
use aes_gcm_siv::{Nonce, Tag}; use aes_gcm_siv::{Nonce, Tag};
use std::rc::Rc;
use core::cell::RefCell;
use core::ops::{Deref, DerefMut};
use core::convert::TryInto;
use bitflags::bitflags; use bitflags::bitflags;
@ -47,6 +51,41 @@ pub(crate) struct Pte {
/// checksum is computed on all of the bits prior, so checksum(pddb_addr, flags, nonce) /// checksum is computed on all of the bits prior, so checksum(pddb_addr, flags, nonce)
checksum: [u8; 4], checksum: [u8; 4],
} }
impl Pte {
pub fn new(va: VirtAddr, flags: PtFlags, entropy: Rc<RefCell<TrngPool>>) -> Self {
let nonce_u32 = entropy.borrow_mut().get_u32();
let mut pte = Pte {
pddb_addr: va.get().to_le_bytes()[..6].try_into().unwrap(),
flags,
reserved: 0,
nonce: nonce_u32.to_le_bytes(),
checksum: [0; 4],
};
let mut pte_data = pte.deref();
let checksum = murmur3_32(&pte_data[..12], nonce_u32);
pte.checksum = checksum.to_le_bytes();
pte
}
}
impl Deref for Pte {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(self as *const Pte as *const u8, core::mem::size_of::<Pte>())
as &[u8]
}
}
}
impl DerefMut for Pte {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
core::slice::from_raw_parts_mut(self as *mut Pte as *mut u8, core::mem::size_of::<Pte>())
as &mut [u8]
}
}
}
pub const PDDB_SIZE_PAGES: usize = crate::PDDB_A_LEN as usize / PAGE_SIZE; pub const PDDB_SIZE_PAGES: usize = crate::PDDB_A_LEN as usize / PAGE_SIZE;
/// This structure is mapped into the top of FLASH memory, starting at /// This structure is mapped into the top of FLASH memory, starting at

View file

@ -167,7 +167,7 @@ mod tests {
#[test] #[test]
/// PAGE_SIZE is required to be a power of two. 0x1000 -> 0x1000 - 1 = 0xFFF, which forms the bitmasks. /// PAGE_SIZE is required to be a power of two. 0x1000 -> 0x1000 - 1 = 0xFFF, which forms the bitmasks.
fn test_page_size() { fn test_page_size() {
assert!(PAGE_SIZE & !(PAGE_SIZE - 1) == 0, "PAGE_SIZE is not a power of two!"); assert!(PAGE_SIZE & (PAGE_SIZE - 1) == 0, "PAGE_SIZE is not a power of two!");
} }
/// This test exists because nothing in the bitfield spec explicitly requires that a true maps to a 1. /// This test exists because nothing in the bitfield spec explicitly requires that a true maps to a 1.
/// In fact a lot of code would work just fine if you mapped true to 0 and false to 1: if you're just using /// In fact a lot of code would work just fine if you mapped true to 0 and false to 1: if you're just using

View file

@ -326,6 +326,27 @@ const PDDB_BACKING_SIZE: usize = 0x4000_0000;
/// The special case of long run's of 1's (more than 32 bits in a row) is checked during the page table scan. /// The special case of long run's of 1's (more than 32 bits in a row) is checked during the page table scan.
/// If a long run of 1's is detected, then a suspected corrupted page table update is flagged, and for that page the data /// If a long run of 1's is detected, then a suspected corrupted page table update is flagged, and for that page the data
/// is pulled from the mbbb record. /// is pulled from the mbbb record.
///
/// Precursor's Implementation-Specific Flash Memory Organization:
///
/// offset from |
/// pt_phys_base| contents (example for total PDDB len of 0x6f8_0000 or 111 MiB)
/// ------------|---------------------------------------------------
/// 0x0000_0000 | virtual map for page at (0x0000 + data_phys_base)
/// 0x0000_0010 | virtual map for page at (0x1000 + data_phys_base)
/// 0x0000_0020 | virtual map for page at (0x2000 + data_phys_base)
/// ...
/// 0x0006_F7F0 | virtual map for page at (0x06F7_F000 + data_phys_base)
/// 0x0006_F800 | unused
/// ...
/// 0x0007_0000 | key page
/// ...
/// 0x0007_1000 | mbbb start (example of 10 pages)
/// ...
/// 0x0007_B000 | fscb start (example of 10 pages)
/// ...
/// 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region
#[xous::xous_main] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {