diff --git a/kernel/src/platform/cramium/mod.rs b/kernel/src/platform/cramium/mod.rs index e3ae5bbb2..f38f9e7e1 100644 --- a/kernel/src/platform/cramium/mod.rs +++ b/kernel/src/platform/cramium/mod.rs @@ -2,7 +2,13 @@ // SPDX-FileCopyrightText: 2024 bunnie // SPDX-License-Identifier: Apache-2.0 +#[cfg(feature = "cramium-soc")] pub mod rand; +#[cfg(feature = "cramium-fpga")] +pub mod rand_fake; +#[cfg(feature = "cramium-fpga")] +pub use rand_fake as rand; + #[cfg(any(feature = "debug-print", feature = "print-panics"))] pub mod uart; diff --git a/kernel/src/platform/cramium/rand_fake.rs b/kernel/src/platform/cramium/rand_fake.rs new file mode 100644 index 000000000..217671f63 --- /dev/null +++ b/kernel/src/platform/cramium/rand_fake.rs @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: 2020 Sean Cross +// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. +// SPDX-FileCopyrightText: 2024 bunnie +// SPDX-License-Identifier: Apache-2.0 + +use core::convert::TryInto; +use core::sync::atomic::{AtomicU32, Ordering}; + +use rand_chacha::ChaCha8Rng; +use rand_chacha::rand_core::RngCore; +use rand_chacha::rand_core::SeedableRng; + +// This is the sum total of state used for simulations +static LOCAL_RNG_STATE: [AtomicU32; 8] = [ + AtomicU32::new(0), + AtomicU32::new(0), + AtomicU32::new(0), + AtomicU32::new(0), + AtomicU32::new(0), + AtomicU32::new(0), + AtomicU32::new(0), + AtomicU32::new(1), +]; + +pub fn init() {} + +/// This a fully deterministic PRNG that relies on Chacha8 for state evolution. +pub fn get_u32() -> u32 { + // Local storage for the seed. + let mut seed = [0u8; 32]; + + // Pull our true seed data from the static AtomicU32 variables. We have to do this because + // we're the kernel: any machine persistent state is by definition, global mutable state. + // + // Mix in more data from the TRNG while recovering the state from the kernel holding area. + for (s, state) in seed.chunks_mut(4).zip(LOCAL_RNG_STATE.iter()) { + let incoming = get_raw_u32() ^ state.load(Ordering::SeqCst); + for (s_byte, &incoming_byte) in s.iter_mut().zip(incoming.to_le_bytes().iter()) { + *s_byte ^= incoming_byte; + } + } + let mut cstrng = ChaCha8Rng::from_seed(seed); + // Mix up the internal state with output from the CSPRNG. We do this because the TRNG bits + // could be biased, and by running the CSPRNG forward based on the new seed, we have a chance to + // diffuse any true entropy over all bits in the seed pool. + for s in seed.chunks_mut(8) { + for (s_byte, chacha_byte) in s.iter_mut().zip(cstrng.next_u64().to_le_bytes()) { + *s_byte ^= chacha_byte; + } + } + // Now extract one value from the CSPRNG: this is the number we reveal to the outside world. + // It should not, in theory, be possible to deduce the seed from this value. + let ret_val = cstrng.next_u32(); + + // Save the mixed state into the kernel holding area + for (s, state) in seed.chunks(4).zip(LOCAL_RNG_STATE.iter()) { + state.store(u32::from_le_bytes(s.try_into().unwrap()), Ordering::SeqCst); + } + ret_val +} + +/// There is no TRNG in simulation, just return a constant and rely on the chacha8 whitener +pub fn get_raw_u32() -> u32 { 0 } diff --git a/kernel/src/platform/cramium/uart.rs b/kernel/src/platform/cramium/uart.rs index 2f1935dda..e9ad0f1cb 100644 --- a/kernel/src/platform/cramium/uart.rs +++ b/kernel/src/platform/cramium/uart.rs @@ -6,16 +6,13 @@ // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint #![allow(static_mut_refs)] +#[cfg(feature = "cramium-soc")] use utralib::generated::*; +#[cfg(feature = "cramium-soc")] use xous_kernel::{MemoryFlags, MemoryType}; #[cfg(feature = "cramium-fpga")] -use crate::{ - PID, - debug::shell::process_characters, - io::{SerialRead, SerialWrite}, - mem::MemoryManager, -}; +use crate::io::{SerialRead, SerialWrite}; #[cfg(feature = "cramium-soc")] use crate::{ PID, @@ -27,117 +24,44 @@ use crate::{ /// UART virtual address. /// /// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md +#[cfg(feature = "cramium-soc")] pub const UART_ADDR: usize = 0xffcf_0000; +#[cfg(feature = "cramium-soc")] pub const IRQ0_ADDR: usize = UART_ADDR + 0x1000; /// UART instance. /// /// Initialized by [`init`]. +#[cfg(feature = "cramium-soc")] pub static mut UART: Option = None; -/// UART peripheral driver. +/// All dummy stubs for cramium-fpga because we want the console to have the DUART #[cfg(feature = "cramium-fpga")] -pub struct Uart { - uart_csr: CSR, - irq_csr: CSR, - callback: fn(&mut Self), -} +pub fn init() {} #[cfg(feature = "cramium-fpga")] +pub struct Uart {} + +#[cfg(feature = "cramium-fpga")] +#[allow(dead_code)] impl Uart { - pub fn new(addr: usize, irq_addr: usize, callback: fn(&mut Self)) -> Uart { - Uart { uart_csr: CSR::new(addr as *mut u32), irq_csr: CSR::new(irq_addr as *mut u32), callback } - } + pub fn new(_addr: usize, _irq_addr: usize, _callback: fn(&mut Self)) -> Uart { Uart {} } - pub fn init(&mut self) { - self.uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1); - self.irq_csr.rmwf(utra::irqarray0::EV_ENABLE_SOURCE0, 1); - } - - pub fn irq(_irq_number: usize, arg: *mut usize) { - let uart = unsafe { &mut *(arg as *mut Uart) }; - (uart.callback)(uart); - // uart.acknowledge_irq(); - } + pub fn init(&mut self) {} } #[cfg(feature = "cramium-fpga")] impl SerialWrite for Uart { - fn putc(&mut self, c: u8) { - // Wait until TXFULL is `0` - while self.uart_csr.r(utra::uart::TXFULL) != 0 {} - self.uart_csr.wfo(utra::uart::RXTX_RXTX, c as u32); - } + fn putc(&mut self, _c: u8) {} } #[cfg(feature = "cramium-fpga")] impl SerialRead for Uart { - fn getc(&mut self) -> Option { - // If EV_PENDING_RX is 1, return the pending character. - // Otherwise, return None. - match self.uart_csr.rf(utra::uart::EV_PENDING_RX) { - 0 => None, - _ => { - let ret = Some(self.uart_csr.r(utra::uart::RXTX) as u8); - self.uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); - self.irq_csr.wfo(utra::irqarray0::EV_PENDING_SOURCE0, 1); - ret - } - } - } -} - -/// Initialize UART driver and debug shell. -#[cfg(feature = "cramium-fpga")] -pub fn init() { - // Map the UART peripheral. - MemoryManager::with_mut(|memory_manager| { - memory_manager - .map_range( - utra::uart::HW_UART_BASE as *mut u8, - (UART_ADDR & !4095) as *mut u8, - 4096, - PID::new(1).unwrap(), - MemoryFlags::R | MemoryFlags::W, - MemoryType::Default, - ) - .expect("unable to map serial port") - }); - // Map the IRQ0 handler - MemoryManager::with_mut(|memory_manager| { - memory_manager - .map_range( - utra::irqarray0::HW_IRQARRAY0_BASE as *mut u8, - (IRQ0_ADDR & !4095) as *mut u8, - 4096, - PID::new(1).unwrap(), - MemoryFlags::R | MemoryFlags::W, - MemoryType::Default, - ) - .expect("unable to map serial port") - }); - - let mut uart = Uart::new(UART_ADDR, IRQ0_ADDR, process_characters); - uart.init(); - - unsafe { - UART = Some(uart); - crate::debug::shell::init((&mut *(&raw mut UART)).as_mut().unwrap()); - - // Claim UART interrupt. - println!("Claiming IRQ {} via syscall...", utra::uart::UART_IRQ); - xous_kernel::claim_interrupt( - utra::uart::UART_IRQ, - Uart::irq, - ((&mut *(&raw mut UART)).as_mut().unwrap() as *mut Uart) as *mut usize, - ) - .expect("Couldn't claim debug interrupt"); - } + fn getc(&mut self) -> Option { None } } #[cfg(feature = "cramium-soc")] pub fn init() { - // there is no kernel UART yet...just a placeholder function // Map the UART peripheral. MemoryManager::with_mut(|memory_manager| { memory_manager