2021-11-05 20:22:34 +00:00
|
|
|
use core::num::NonZeroU32;
|
2021-11-08 20:57:12 +00:00
|
|
|
use core::ops::Deref;
|
2021-11-05 20:22:34 +00:00
|
|
|
|
|
|
|
|
use super::PAGE_SIZE;
|
2021-11-06 19:16:32 +00:00
|
|
|
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;
|
|
|
|
|
|
2021-11-08 10:41:31 +00:00
|
|
|
#[derive(Debug)]
|
2021-11-05 20:22:34 +00:00
|
|
|
#[repr(u8)]
|
2021-11-08 10:41:31 +00:00
|
|
|
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
|
|
|
}
|
2021-11-08 10:41:31 +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
|
|
|
|
2021-11-08 20:57:12 +00:00
|
|
|
pub(crate) const FASTSPACE_FREE_POOL_LEN: usize = ((PAGE_SIZE * FASTSPACE_PAGES) - (12 + 16)) / core::mem::size_of::<PhysPage>();
|
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 {
|
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
|
2021-11-08 20:57:12 +00:00
|
|
|
pub(crate) free_pool: [PhysPage; FASTSPACE_FREE_POOL_LEN],
|
|
|
|
|
}
|
|
|
|
|
impl Deref for FastSpace {
|
|
|
|
|
type Target = [u8];
|
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
|
|
|
unsafe {
|
|
|
|
|
core::slice::from_raw_parts(self as *const FastSpace as *const u8, core::mem::size_of::<FastSpace>())
|
|
|
|
|
as &[u8]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// this structure is created just to confirm that the overall size is exactly one page, and should only
|
|
|
|
|
/// be used by the test included in this file.
|
|
|
|
|
#[repr(C, packed)]
|
|
|
|
|
struct FastSpaceInFlash {
|
|
|
|
|
p_nonce: [u8; 12],
|
|
|
|
|
ram_rep: FastSpace,
|
2021-11-05 20:22:34 +00:00
|
|
|
p_tag: [u8; 16],
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 20:57:12 +00:00
|
|
|
/// a 128-bit record that stores an encrypted update to the FastSpace pool, facilitating the "rarely" update property 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,
|
2021-11-08 20:57:12 +00:00
|
|
|
// this checksum is "weak" but we are protecting against two scenarios:
|
|
|
|
|
// 1. partially written SpaceUpdate record (so the last bytes or so are FF)
|
|
|
|
|
// 2. a malicious attacker
|
|
|
|
|
// In the case of (1), the occurence should be diminishingly small (expected to never happen, maybe
|
|
|
|
|
// a very unstable system that's "blinking" power constantly would have it occure a few times)
|
|
|
|
|
// In the case of (2), an attacker has a chance of generating a collision, but the result is
|
|
|
|
|
// also unlikely to generate a valid PhysAddr, and if it does, the consequence is some valid data
|
|
|
|
|
// being treated as free space and getting erased (data loss, not disclosure).
|
|
|
|
|
checksum: [u8; 4],
|
2021-11-07 21:41:39 +00:00
|
|
|
}
|
|
|
|
|
#[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,
|
2021-11-08 20:57:12 +00:00
|
|
|
// consider: using the top 12 bits of the PhysAddr as a checksum
|
2021-11-07 21:41:39 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-11-08 20:57:12 +00:00
|
|
|
assert!(core::mem::size_of::<FastSpaceInFlash>() & !(PAGE_SIZE - 1) == 0, "FastSpace is not exactly a multiple of one page in size");
|
2021-11-07 21:41:39 +00:00
|
|
|
}
|
2021-11-05 20:22:34 +00:00
|
|
|
}
|