mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
kernel in -fpga variant uses duart
This commit is contained in:
parent
201decd4ca
commit
03d67de1f6
3 changed files with 85 additions and 92 deletions
|
|
@ -2,7 +2,13 @@
|
||||||
// SPDX-FileCopyrightText: 2024 bunnie <bunnie@kosagi.com>
|
// SPDX-FileCopyrightText: 2024 bunnie <bunnie@kosagi.com>
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
#[cfg(feature = "cramium-soc")]
|
||||||
pub mod rand;
|
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"))]
|
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||||
pub mod uart;
|
pub mod uart;
|
||||||
|
|
||||||
|
|
|
||||||
63
kernel/src/platform/cramium/rand_fake.rs
Normal file
63
kernel/src/platform/cramium/rand_fake.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
|
||||||
|
// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. <hello@foundationdevices.com>
|
||||||
|
// SPDX-FileCopyrightText: 2024 bunnie <bunnie@kosagi.com>
|
||||||
|
// 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 }
|
||||||
|
|
@ -6,16 +6,13 @@
|
||||||
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
|
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
|
||||||
#![allow(static_mut_refs)]
|
#![allow(static_mut_refs)]
|
||||||
|
|
||||||
|
#[cfg(feature = "cramium-soc")]
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
|
#[cfg(feature = "cramium-soc")]
|
||||||
use xous_kernel::{MemoryFlags, MemoryType};
|
use xous_kernel::{MemoryFlags, MemoryType};
|
||||||
|
|
||||||
#[cfg(feature = "cramium-fpga")]
|
#[cfg(feature = "cramium-fpga")]
|
||||||
use crate::{
|
use crate::io::{SerialRead, SerialWrite};
|
||||||
PID,
|
|
||||||
debug::shell::process_characters,
|
|
||||||
io::{SerialRead, SerialWrite},
|
|
||||||
mem::MemoryManager,
|
|
||||||
};
|
|
||||||
#[cfg(feature = "cramium-soc")]
|
#[cfg(feature = "cramium-soc")]
|
||||||
use crate::{
|
use crate::{
|
||||||
PID,
|
PID,
|
||||||
|
|
@ -27,117 +24,44 @@ use crate::{
|
||||||
/// UART virtual address.
|
/// UART virtual address.
|
||||||
///
|
///
|
||||||
/// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
/// See https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||||
|
#[cfg(feature = "cramium-soc")]
|
||||||
pub const UART_ADDR: usize = 0xffcf_0000;
|
pub const UART_ADDR: usize = 0xffcf_0000;
|
||||||
|
#[cfg(feature = "cramium-soc")]
|
||||||
pub const IRQ0_ADDR: usize = UART_ADDR + 0x1000;
|
pub const IRQ0_ADDR: usize = UART_ADDR + 0x1000;
|
||||||
|
|
||||||
/// UART instance.
|
/// UART instance.
|
||||||
///
|
///
|
||||||
/// Initialized by [`init`].
|
/// Initialized by [`init`].
|
||||||
|
#[cfg(feature = "cramium-soc")]
|
||||||
pub static mut UART: Option<Uart> = None;
|
pub static mut UART: Option<Uart> = None;
|
||||||
|
|
||||||
/// UART peripheral driver.
|
/// All dummy stubs for cramium-fpga because we want the console to have the DUART
|
||||||
#[cfg(feature = "cramium-fpga")]
|
#[cfg(feature = "cramium-fpga")]
|
||||||
pub struct Uart {
|
pub fn init() {}
|
||||||
uart_csr: CSR<u32>,
|
|
||||||
irq_csr: CSR<u32>,
|
|
||||||
callback: fn(&mut Self),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "cramium-fpga")]
|
#[cfg(feature = "cramium-fpga")]
|
||||||
|
pub struct Uart {}
|
||||||
|
|
||||||
|
#[cfg(feature = "cramium-fpga")]
|
||||||
|
#[allow(dead_code)]
|
||||||
impl Uart {
|
impl Uart {
|
||||||
pub fn new(addr: usize, irq_addr: usize, callback: fn(&mut Self)) -> Uart {
|
pub fn new(_addr: usize, _irq_addr: usize, _callback: fn(&mut Self)) -> Uart { Uart {} }
|
||||||
Uart { uart_csr: CSR::new(addr as *mut u32), irq_csr: CSR::new(irq_addr as *mut u32), callback }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(&mut self) {
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cramium-fpga")]
|
#[cfg(feature = "cramium-fpga")]
|
||||||
impl SerialWrite for Uart {
|
impl SerialWrite for Uart {
|
||||||
fn putc(&mut self, c: u8) {
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cramium-fpga")]
|
#[cfg(feature = "cramium-fpga")]
|
||||||
impl SerialRead for Uart {
|
impl SerialRead for Uart {
|
||||||
fn getc(&mut self) -> Option<u8> {
|
fn getc(&mut self) -> Option<u8> { None }
|
||||||
// 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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cramium-soc")]
|
#[cfg(feature = "cramium-soc")]
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
// there is no kernel UART yet...just a placeholder function
|
|
||||||
// Map the UART peripheral.
|
// Map the UART peripheral.
|
||||||
MemoryManager::with_mut(|memory_manager| {
|
MemoryManager::with_mut(|memory_manager| {
|
||||||
memory_manager
|
memory_manager
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue