diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 579e5b359..5b7431bd2 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -87,7 +87,7 @@ mod tests { assert!(core::mem::size_of::() == 4096, "PddBuf record has the wrong size"); } 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 { diff --git a/services/pddb/src/backend.rs b/services/pddb/src/backend.rs index 4a754b899..89c80dae3 100644 --- a/services/pddb/src/backend.rs +++ b/services/pddb/src/backend.rs @@ -14,8 +14,9 @@ pub(crate) use murmur3::*; mod trngpool; pub(crate) use trngpool::*; -#[cfg(any(target_os = "none", target_os = "xous"))] +//#[cfg(any(target_os = "none", target_os = "xous"))] mod hw; pub(crate) use hw::*; + // TODO: the alternative back-end PddbOs structures for hosted mode. \ No newline at end of file diff --git a/services/pddb/src/backend/fastspace.rs b/services/pddb/src/backend/fastspace.rs index c488f8598..16cc0af1d 100644 --- a/services/pddb/src/backend/fastspace.rs +++ b/services/pddb/src/backend/fastspace.rs @@ -40,8 +40,9 @@ impl From for u8 { } } -pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = ((PAGE_SIZE * FASTSPACE_PAGES) - - (size_of::() + size_of::())) / core::mem::size_of::(); +pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = + ((PAGE_SIZE * FASTSPACE_PAGES) - (size_of::() + size_of::())) + / core::mem::size_of::(); /// FastSpace tracks a limited set of physical pages /// 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: @@ -157,6 +158,6 @@ mod tests { use super::*; #[test] fn test_fast_space_size() { - assert!(core::mem::size_of::() & !(PAGE_SIZE - 1) == 0, "FastSpace is not exactly a multiple of one page in size"); + assert!(core::mem::size_of::() & (PAGE_SIZE - 1) == 0, "FastSpaceInFlash is not exactly a multiple of one page in size"); } } diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 13bff49f7..90eb6cb92 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -803,26 +803,17 @@ impl PddbOs { } // step 9. generate & write initial page table entries - // page table 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 - + if let Some(system_key) = self.system_basis_key { + let cipher = Aes256::new(GenericArray::from_slice(&system_key)); + if let Some(basis_v2p_map) = self.v2p_map.get(&basis_root.name) { + for (&virt, &phys) in basis_v2p_map.into_iter() { + let mut pte = Pte::new(virt, PtFlags::CLEAN, Rc::clone(&self.entropy)); + let mut block = Block::from_mut_slice(pte.deref_mut()); + cipher.encrypt_block(block); + self.patch_pagetable(&block, phys.page_number() * aes::BLOCK_SIZE as u32); + } + } + } Ok(()) } diff --git a/services/pddb/src/backend/murmur3.rs b/services/pddb/src/backend/murmur3.rs index 91c69560b..49e55a7d0 100644 --- a/services/pddb/src/backend/murmur3.rs +++ b/services/pddb/src/backend/murmur3.rs @@ -60,7 +60,7 @@ fn calc_k(k: u32) -> u32 { mod tests { use super::*; struct Result { - slice: &'static [u32], + slice: &'static [u8], hash_32: u32, } diff --git a/services/pddb/src/backend/pagetable.rs b/services/pddb/src/backend/pagetable.rs index 0f495ba26..f7dfee58d 100644 --- a/services/pddb/src/backend/pagetable.rs +++ b/services/pddb/src/backend/pagetable.rs @@ -1,6 +1,10 @@ -use super::PAGE_SIZE; +use super::{PAGE_SIZE, TrngPool, VirtAddr, murmur3_32}; use core::mem::size_of; 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; @@ -47,6 +51,41 @@ pub(crate) struct Pte { /// checksum is computed on all of the bits prior, so checksum(pddb_addr, flags, nonce) checksum: [u8; 4], } +impl Pte { + pub fn new(va: VirtAddr, flags: PtFlags, entropy: Rc>) -> 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::()) + 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::()) + as &mut [u8] + } + } +} 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 diff --git a/services/pddb/src/backend/types.rs b/services/pddb/src/backend/types.rs index 21bc2acbc..87d442b2f 100644 --- a/services/pddb/src/backend/types.rs +++ b/services/pddb/src/backend/types.rs @@ -167,7 +167,7 @@ mod tests { #[test] /// PAGE_SIZE is required to be a power of two. 0x1000 -> 0x1000 - 1 = 0xFFF, which forms the bitmasks. 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. /// 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 diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 38f477a1a..cd2b6d377 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -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. /// 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. +/// +/// 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] fn xmain() -> ! {