diff --git a/services/trng/Cargo.toml b/services/trng/Cargo.toml index ae13cbac2..92f791816 100644 --- a/services/trng/Cargo.toml +++ b/services/trng/Cargo.toml @@ -22,6 +22,8 @@ rkyv = { version = "0.8.8", default-features = false, features = [ xous-ipc = "0.10.1" rand_core = "0.6.4" # the 0.6.4 API is necessary for compatibility with curve25519-dalek crates utralib = { version = "0.1.25", optional = true, default-features = false } +flatipc = { path = "../../libs/flatipc" } +flatipc-derive = { path = "../../libs/flatipc-derive" } [target.'cfg(any(windows,unix))'.dependencies] rand = "0.8.5" diff --git a/services/trng/src/api.rs b/services/trng/src/api.rs index f4a63bc4c..4b493fcb0 100644 --- a/services/trng/src/api.rs +++ b/services/trng/src/api.rs @@ -37,15 +37,11 @@ pub struct TrngErrors { pub pending_mask: u32, } -/// Performance issue just noticed: the data field is exactly 4096 bytes long, which means -/// the "len" field overflows the structure to be 2 pages. This will cause a lot of extra -/// zero-ing of pages, thrashing the cache and also pegging the CPU for useless work. -/// Consider revising the data field down to 1023 words in length, but need to revisit the -/// library implemnetations to make sure this doesn't break any existing code. -/// Note that this structure had to be mirrored into the local "getrandom" implementation -#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +#[derive(Debug, flatipc::Ipc)] +/// Note that this structure is mirrored in imports/getrandom/src/xous.rs +#[repr(C)] pub struct TrngBuf { - pub data: [u32; 1024], + pub data: [u32; 1020], pub len: u16, } diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 5d1c1756e..82d1e924c 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -69,7 +69,6 @@ impl Trng { let mut buf = tb.into_ipc(); buf.lend_mut(self.conn, api::Opcode::FillTrng.to_usize().unwrap()) .or(Err(xous::Error::InternalError))?; - println!("{}", tb.len); // this shouldn't be possible if buf.len as usize != data.len() { return Err(xous::Error::InternalError); } diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index b6788e2dd..99e11952c 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -3,6 +3,7 @@ mod api; use api::*; +use flatipc::Ipc; use log::info; use num_traits::*; use xous::CID; @@ -24,11 +25,11 @@ mod implementation { use susres::{RegManager, RegOrField, SuspendResume}; use utralib::generated::*; - use crate::api::{ - ExcursionTest, HealthTests, MiniRunsTest, NistTests, TrngBuf, TrngErrors, TrngTestBuf, TrngTestMode, - TRNG_TEST_BUF_LEN, - }; use crate::TEST_CACHE_LEN; + use crate::api::{ + ExcursionTest, HealthTests, MiniRunsTest, NistTests, TRNG_TEST_BUF_LEN, TrngErrors, TrngTestBuf, + TrngTestMode, + }; pub struct Trng { csr: utralib::CSR, @@ -370,12 +371,11 @@ mod implementation { } } - pub fn get_buf(&mut self, len: u16) -> TrngBuf { - let mut tb = TrngBuf { data: [0; 1024], len }; - for i in 0..len as usize { - tb.data[i] = self.get_data_eager(); + pub fn get_buf(&mut self, buf: &mut trng::api::IpcTrngBuf) { + let len = buf.len as usize; + for d in buf.data[..len].iter_mut() { + *d = self.get_data_eager(); } - tb } pub fn get_trng(&mut self, count: usize) -> [u32; 2] { @@ -520,11 +520,11 @@ mod implementation { // a stub to try to avoid breaking hosted mode for as long as possible. #[cfg(not(target_os = "xous"))] mod implementation { + use rand_chacha::ChaCha8Rng; use rand_chacha::rand_core::RngCore; use rand_chacha::rand_core::SeedableRng; - use rand_chacha::ChaCha8Rng; - use crate::api::{HealthTests, TrngBuf, TrngErrors, TrngTestBuf, TrngTestMode, TRNG_TEST_BUF_LEN}; + use crate::api::{HealthTests, TRNG_TEST_BUF_LEN, TrngErrors, TrngTestBuf, TrngTestMode}; pub struct Trng { rng: ChaCha8Rng, @@ -555,16 +555,15 @@ mod implementation { #[allow(dead_code)] pub fn wait_full(&self) {} - pub fn get_buf(&mut self, len: u16) -> TrngBuf { + pub fn get_buf(&mut self, buf: &mut trng::api::IpcTrngBuf) { if self.msgcount < 3 { log::info!("hosted mode TRNG is *not* random, it is a deterministic LFSR"); } self.msgcount += 1; - let mut data = [0; 1024]; - for d in data.iter_mut() { + let len = buf.len as usize; + for d in buf.data[..len].iter_mut() { *d = self.rng.next_u32(); } - TrngBuf { data, len } } pub fn get_trng(&mut self, _count: usize) -> [u32; 2] { @@ -698,10 +697,14 @@ fn main() -> ! { buffer.replace(trng.get_errors()).unwrap(); } Some(api::Opcode::FillTrng) => { - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let len = buffer.as_flat::().unwrap().len; - buffer.replace(trng.get_buf(len.into())).unwrap(); + let mm = msg.body.memory_message_mut().unwrap(); + // safety: the slice is `u8` and all values are representable + let buffer = trng::api::IpcTrngBuf::from_slice_mut( + unsafe { mm.buf.as_slice_mut() }, + mm.offset.unwrap().get(), + ) + .expect("couldn't unpack FillTrng buffer"); + trng.get_buf(buffer); } Some(api::Opcode::TestSetMode) => xous::msg_blocking_scalar_unpack!(msg, mode_code, _, _, _, { if let Some(mode) = FromPrimitive::from_usize(mode_code) {