From c8a38b4bf57c79088f1d3af7c70c9f8b3b091055 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Apr 2022 00:44:45 +0800 Subject: [PATCH] fix representation of va in pagetable so we're not burning 12 bits on 0's this is a v2 fix, incompatible with v1 --- services/pddb/src/backend/pagetable.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/services/pddb/src/backend/pagetable.rs b/services/pddb/src/backend/pagetable.rs index 8897dfd83..9b6428b6a 100644 --- a/services/pddb/src/backend/pagetable.rs +++ b/services/pddb/src/backend/pagetable.rs @@ -1,4 +1,4 @@ -use super::{PAGE_SIZE, TrngPool, VirtAddr, murmur3_32}; +use super::{PAGE_SIZE, TrngPool, VirtAddr, murmur3_32, VPAGE_SIZE}; use core::mem::size_of; use aes_gcm_siv::{Nonce, Tag}; use std::rc::Rc; @@ -44,6 +44,8 @@ impl Default for PtFlags { #[derive(Default)] pub(crate) struct Pte { /// the virtual page number is 52 bits long (52 + 12 = 64). 4 bits are wasted in this representation. + /// The storage format is in *page numbers* but the API accepts *addresses*. Therefore a division and + /// multiplication by VPAGE_SIZE wraps the getters and setters for this field. pddb_addr: [u8; 7], /// this maps to a u8 flags: PtFlags, @@ -57,7 +59,7 @@ 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()[..7].try_into().unwrap(), + pddb_addr: (va.get() / VPAGE_SIZE as u64).to_le_bytes()[..7].try_into().unwrap(), flags, nonce: nonce_u32.to_le_bytes(), checksum: [0; 4], @@ -69,6 +71,19 @@ impl Pte { pte } pub fn vaddr(&self) -> VirtAddr { + let mut full_addr = [0u8; 8]; + // LSB encoded, so this loop deposits the partial pddb_addr in the LSBs, and the MSBs are correctly 0 from above initializer + for (&src, dst) in self.pddb_addr.iter().zip(full_addr.iter_mut()) { + *dst = src; + } + VirtAddr::new(u64::from_le_bytes(full_addr) * VPAGE_SIZE as u64).unwrap() + } + /// V1 databases stored the virtual address as a full address, instead of as a page number, which means + /// the overall size of our database was about 4000x smaller than we had thought. This was fixed in v2, + /// but this getter is required to migrate from v1. + /// This allows us to retrieve the old address format for the first phase of migration. + #[cfg(feature="migration1")] + pub fn vaddr_v1(&self) -> VirtAddr { let mut full_addr = [0u8; 8]; // LSB encoded, so this loop deposits the partial pddb_addr in the LSBs, and the MSBs are correctly 0 from above initializer for (&src, dst) in self.pddb_addr.iter().zip(full_addr.iter_mut()) { @@ -76,6 +91,7 @@ impl Pte { } VirtAddr::new(u64::from_le_bytes(full_addr)).unwrap() } + #[allow(dead_code)] pub fn flags(&self) -> PtFlags { self.flags