xous-core/services/pddb/src/backend/fastspace.rs

91 lines
3.5 KiB
Rust
Raw Normal View History

2021-11-05 20:22:34 +00:00
use core::num::NonZeroU32;
use super::PAGE_SIZE;
use crate::*;
2021-11-05 20:22:34 +00:00
2021-11-07 21:41:39 +00:00
/// Each free_pool entry takes about 4 bytes, so give-or-take we have about 1000 free_pool
/// entries per page of storage for the free_pool, or 4k * 1000 ~ 4MiB per page, when PhysAddr is a u32
pub(crate) const FASTSPACE_PAGES: usize = 2;
#[derive(Debug)]
2021-11-05 20:22:34 +00:00
#[repr(u8)]
pub enum SpaceState {
2021-11-05 20:22:34 +00:00
/// pages that are completely un-spoken for
2021-11-07 21:41:39 +00:00
Free = 0,
2021-11-05 20:22:34 +00:00
/// pages that are in the process of being used, but the journal has yet to be committed
2021-11-07 21:41:39 +00:00
/// in other words, these are pages that might be in the RAM cache.
MaybeUsed = 1,
/// pages that are confirm plus chop fully used
Used = 2,
2021-11-05 20:22:34 +00:00
/// pages that are no longer used and need to be erased
2021-11-07 21:41:39 +00:00
Dirty = 3,
2021-11-05 20:22:34 +00:00
}
impl From<u8> for SpaceState {
fn from(arg: u8) -> Self {
match arg & 0x3 {
0 => SpaceState::Free,
1 => SpaceState::MaybeUsed,
2 => SpaceState::Used,
_ => SpaceState::Dirty,
}
}
}
impl From<SpaceState> for u8 {
fn from(arg: SpaceState) -> Self {
arg as u8
}
}
2021-11-05 20:22:34 +00:00
/// FastSpace tracks a limited set of physical pages
2021-11-07 21:41:39 +00:00
/// 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:
/// Make-before-break: (maybe it's COW? idk, I'm a hardware guy, so I'm switched on to make-before-break)
/// Write a new version of this with a higher journal number, verify its
/// integrity, and then erase the previous version.
/// Atomically:
/// The entire contents of this structure is written in the update operation; there are no partial updates
/// possible to the structure
/// Rarely:
/// Instead of partial updates to this structure, individual "SpaceUpdate" records are encrypted to the
/// system basis and stored in a non-cryptographically free region. These are decrypted and merged
/// into an in-RAM cache that facilitates the "rare" update of this large-ish structure. This creates
/// a side-channel, where an attacker would be able to observe the rate at which pages are modified...
/// and hopefully nothing else?
/// The "rare" property is important especially as the disk size scales up; if we wanted to keep
/// 100MiB of "fast space" on hand, this structure would span 25 pages.
2021-11-05 20:22:34 +00:00
#[repr(C, packed)]
pub (crate) struct FastSpace {
p_nonce: [u8; 12],
2021-11-07 21:41:39 +00:00
/// u32 in lsb byte order
2021-11-05 20:22:34 +00:00
journal_rev: [u8; 4],
2021-11-07 21:41:39 +00:00
/// Not sure if there is a "better" way to compute things, but we want the number of entries in the
/// free_pool array to "round out" the FastSpace record to be equal to exactly one page
free_pool: [PhysPage; ((PAGE_SIZE * FASTSPACE_PAGES) - (12 + 4 + 16)) / core::mem::size_of::<PhysPage>()],
2021-11-05 20:22:34 +00:00
p_tag: [u8; 16],
}
2021-11-07 21:41:39 +00:00
/// a 128-bit record that stores an encrypted update to the FastSpace pool, facilitating the "rarely" trait of the structure.
2021-11-05 20:22:34 +00:00
#[repr(C, packed)]
2021-11-07 21:41:39 +00:00
#[cfg(not(feature = "u64_pa"))]
pub (crate) struct SpaceUpdate {
nonce: u64,
page_number: PhysAddr,
reserved: [u8; 4],
}
#[cfg(feature = "u64_pa")]
2021-11-05 20:22:34 +00:00
pub (crate) struct SpaceUpdate {
nonce: u64,
2021-11-07 21:41:39 +00:00
page_number: PhysAddr,
}
impl SpaceUpdate {
// add accessors, decryptors, and constructors so we don't shoot ourselves in the foot so much.
}
mod tests {
use super::*;
#[test]
fn test_fast_space_size() {
assert!(core::mem::size_of::<FastSpace>() & !(PAGE_SIZE - 1) == 0, "FastSpace is not exactly a multiple of one page in size");
}
2021-11-05 20:22:34 +00:00
}