diff --git a/Cargo.lock b/Cargo.lock index aa445f4f7..e2f278262 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1723,10 +1723,12 @@ dependencies = [ "log-server", "num-derive", "num-traits", + "rkyv 0.4.3", "susres", "ticktimer-server", "utralib", "xous", + "xous-ipc", "xous-names", ] diff --git a/precursors/soc.svd b/precursors/soc.svd old mode 100644 new mode 100755 index e0deaba90..6d66c762d --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1210,10 +1210,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xaabcc492 + 0x54f46891 32 @@ -1230,7 +1230,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x00 + 0x0f 32 @@ -1244,7 +1244,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x00 + 0x01 32 @@ -3439,7 +3439,7 @@ inject too fast.]]> SEED1 0x0000 - 0x6f5e38f3 + 0x5243fcb 32 @@ -3454,7 +3454,7 @@ inject too fast.]]> SEED0 0x0004 - 0x55bde311 + 0x34203b8b 32 @@ -4063,17 +4063,10 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> when cleared, TRNGs are always on]]> - no_check + clr_err 4 [4:4] 4 - - - - clr_err - 5 - [5:5] - 5 @@ -4179,8 +4172,8 @@ snapshot of the avalanche generator waveform.]]> 12 [12:1] 1 - @@ -4200,7 +4193,7 @@ coupling of the deterministic sysclk noise into the rings.]]> 23 [23:23] 23 - @@ -4216,8 +4209,56 @@ add another stage.]]> - ERRORS + AV_NIST 0x0014 + 0xcd29 + 32 + + + repcount_cutoff + 6 + [6:0] + 0 + + + + adaptive_cutoff + 15 + [15:7] + 7 + + + + + + RO_NIST + 0x0018 + 0x1a433 + 32 + + + repcount_cutoff + 6 + [6:0] + 0 + + + + adaptive_cutoff + 16 + [16:7] + 7 + + + + + + UNDERRUNS + 0x001c 0x00 32 @@ -4237,26 +4278,797 @@ to max field size]]> + + + + NIST_ERRORS + 0x0020 + 0x00 + 32 + - ro_health - 20 - [20:20] - 20 - + av_repcount + 1 + [1:0] + 0 + - av_health - 21 - [21:21] - 21 - + av_adaptive + 3 + [3:2] + 2 + + + + ro_repcount + 7 + [7:4] + 4 + + + + ro_adaptive + 11 + [11:8] + 8 + + + + + + NIST_RO_STAT0 + 0x0024 + 0x00 + 32 + + + adap_b + 9 + [9:0] + 0 + + + + fresh + 10 + [10:10] + 10 + + + + rep_b + 17 + [17:11] + 11 + + + + + + NIST_RO_STAT1 + 0x0028 + 0x00 + 32 + + + adap_b + 9 + [9:0] + 0 + + + + fresh + 10 + [10:10] + 10 + + + + rep_b + 17 + [17:11] + 11 + + + + + + NIST_RO_STAT2 + 0x002c + 0x00 + 32 + + + adap_b + 9 + [9:0] + 0 + + + + fresh + 10 + [10:10] + 10 + + + + rep_b + 17 + [17:11] + 11 + + + + + + NIST_RO_STAT3 + 0x0030 + 0x00 + 32 + + + adap_b + 9 + [9:0] + 0 + + + + fresh + 10 + [10:10] + 10 + + + + rep_b + 17 + [17:11] + 11 + + + + + + NIST_AV_STAT0 + 0x0034 + 0x00 + 32 + + + adap_b + 9 + [9:0] + 0 + + + + fresh + 10 + [10:10] + 10 + + + + rep_b + 17 + [17:11] + 11 + + + + + + NIST_AV_STAT1 + 0x0038 + 0x00 + 32 + + + adap_b + 9 + [9:0] + 0 + + + + fresh + 10 + [10:10] + 10 + + + + rep_b + 17 + [17:11] + 11 + + + + + + RO_RUN0_CTRL + 0x003c + 0x400 + 32 + + + window + 10 + [10:0] + 0 + + + + + + RO_RUN0_FRESH + + 0x0040 + 0x00 + 32 + + + ro_run0_fresh + 4 + [4:0] + 0 + + + + + RO_RUN0_COUNT1 + + 0x0044 + 0x00 + 32 + + + ro_run0_count1 + 10 + [10:0] + 0 + + + + + RO_RUN0_COUNT2 + + 0x0048 + 0x00 + 32 + + + ro_run0_count2 + 10 + [10:0] + 0 + + + + + RO_RUN0_COUNT3 + + 0x004c + 0x00 + 32 + + + ro_run0_count3 + 10 + [10:0] + 0 + + + + + RO_RUN0_COUNT4 + + 0x0050 + 0x00 + 32 + + + ro_run0_count4 + 10 + [10:0] + 0 + + + + + RO_RUN0_COUNT5 + + 0x0054 + 0x00 + 32 + + + ro_run0_count5 + 10 + [10:0] + 0 + + + + + RO_RUN1_CTRL + 0x0058 + 0x400 + 32 + + + window + 10 + [10:0] + 0 + + + + + + RO_RUN1_FRESH + + 0x005c + 0x00 + 32 + + + ro_run1_fresh + 4 + [4:0] + 0 + + + + + RO_RUN1_COUNT1 + + 0x0060 + 0x00 + 32 + + + ro_run1_count1 + 10 + [10:0] + 0 + + + + + RO_RUN1_COUNT2 + + 0x0064 + 0x00 + 32 + + + ro_run1_count2 + 10 + [10:0] + 0 + + + + + RO_RUN1_COUNT3 + + 0x0068 + 0x00 + 32 + + + ro_run1_count3 + 10 + [10:0] + 0 + + + + + RO_RUN1_COUNT4 + + 0x006c + 0x00 + 32 + + + ro_run1_count4 + 10 + [10:0] + 0 + + + + + RO_RUN1_COUNT5 + + 0x0070 + 0x00 + 32 + + + ro_run1_count5 + 10 + [10:0] + 0 + + + + + RO_RUN2_CTRL + 0x0074 + 0x400 + 32 + + + window + 10 + [10:0] + 0 + + + + + + RO_RUN2_FRESH + + 0x0078 + 0x00 + 32 + + + ro_run2_fresh + 4 + [4:0] + 0 + + + + + RO_RUN2_COUNT1 + + 0x007c + 0x00 + 32 + + + ro_run2_count1 + 10 + [10:0] + 0 + + + + + RO_RUN2_COUNT2 + + 0x0080 + 0x00 + 32 + + + ro_run2_count2 + 10 + [10:0] + 0 + + + + + RO_RUN2_COUNT3 + + 0x0084 + 0x00 + 32 + + + ro_run2_count3 + 10 + [10:0] + 0 + + + + + RO_RUN2_COUNT4 + + 0x0088 + 0x00 + 32 + + + ro_run2_count4 + 10 + [10:0] + 0 + + + + + RO_RUN2_COUNT5 + + 0x008c + 0x00 + 32 + + + ro_run2_count5 + 10 + [10:0] + 0 + + + + + RO_RUN3_CTRL + 0x0090 + 0x400 + 32 + + + window + 10 + [10:0] + 0 + + + + + + RO_RUN3_FRESH + + 0x0094 + 0x00 + 32 + + + ro_run3_fresh + 4 + [4:0] + 0 + + + + + RO_RUN3_COUNT1 + + 0x0098 + 0x00 + 32 + + + ro_run3_count1 + 10 + [10:0] + 0 + + + + + RO_RUN3_COUNT2 + + 0x009c + 0x00 + 32 + + + ro_run3_count2 + 10 + [10:0] + 0 + + + + + RO_RUN3_COUNT3 + + 0x00a0 + 0x00 + 32 + + + ro_run3_count3 + 10 + [10:0] + 0 + + + + + RO_RUN3_COUNT4 + + 0x00a4 + 0x00 + 32 + + + ro_run3_count4 + 10 + [10:0] + 0 + + + + + RO_RUN3_COUNT5 + + 0x00a8 + 0x00 + 32 + + + ro_run3_count5 + 10 + [10:0] + 0 + + + + + AV_EXCURSION0_CTRL + 0x00ac + 0x190140 + 32 + + + cutoff + 11 + [11:0] + 0 + + + + reset + 12 + [12:12] + 12 + + + + window + 31 + [31:13] + 13 + + + + + + AV_EXCURSION0_STAT + 0x00b0 + 0x00 + 32 + + + min + 11 + [11:0] + 0 + + + + max + 23 + [23:12] + 12 + + + + + + AV_EXCURSION0_LAST_ERR + 0x00b4 + 0x00 + 32 + + + min + 11 + [11:0] + 0 + + + + max + 23 + [23:12] + 12 + + + + + + AV_EXCURSION1_CTRL + 0x00b8 + 0x190140 + 32 + + + cutoff + 11 + [11:0] + 0 + + + + reset + 12 + [12:12] + 12 + + + + window + 31 + [31:13] + 13 + + + + + + AV_EXCURSION1_STAT + 0x00bc + 0x00 + 32 + + + min + 11 + [11:0] + 0 + + + + max + 23 + [23:12] + 12 + + + + + + AV_EXCURSION1_LAST_ERR + 0x00c0 + 0x00 + 32 + + + min + 11 + [11:0] + 0 + + + + max + 23 + [23:12] + 12 + EV_STATUS - - 0x0018 + + 0x00c4 0x00 32 @@ -4281,12 +5093,26 @@ to max field size]]> 2 + + excursion0 + 3 + [3:3] + 3 + + + + excursion1 + 4 + [4:4] + 4 + + EV_PENDING - - 0x001c + + 0x00c8 0x00 32 @@ -4311,12 +5137,26 @@ to max field size]]> 2 + + excursion0 + 3 + [3:3] + 3 + + + + excursion1 + 4 + [4:4] + 4 + + EV_ENABLE - - 0x0020 + + 0x00cc 0x00 32 @@ -4341,12 +5181,26 @@ to max field size]]> 2 + + excursion0 + 3 + [3:3] + 3 + + + + excursion1 + 4 + [4:4] + 4 + + 0 - 0x24 + 0xd0 registers @@ -4363,7 +5217,7 @@ to max field size]]> XADC_TEMPERATURE +Temperature (C) = ``Value`` x 503.975 / 4096 - 273.15.]]> 0x0000 0x00 32 diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin old mode 100644 new mode 100755 index 575058c3d..19ac32c85 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/trng/Cargo.toml b/services/trng/Cargo.toml index 6b76f6d26..61584f5c1 100644 --- a/services/trng/Cargo.toml +++ b/services/trng/Cargo.toml @@ -14,6 +14,8 @@ log = "0.4" num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} susres = {path = "../susres"} +rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} +xous-ipc = {path = "../../xous-ipc"} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/trng/src/api.rs b/services/trng/src/api.rs index 48af9d1b6..d5d7e8e26 100644 --- a/services/trng/src/api.rs +++ b/services/trng/src/api.rs @@ -1,6 +1,41 @@ pub(crate) const SERVER_NAME_TRNG: &str = "_TRNG manager_"; -#[allow(dead_code)] + +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] +pub struct ExcursionTest { + pub min: u16, + pub max: u16, +} +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] +pub struct MiniRunsTest { + pub run_count: [u16; 5], + pub fresh: bool, +} +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] +pub struct NistTests { + pub adaptive_b: u16, + pub repcount_b: u16, + pub fresh: bool, +} +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] +pub struct HealthTests { + pub av_excursion: [ExcursionTest; 2], + pub av_nist: [NistTests; 2], + pub ro_miniruns: [MiniRunsTest; 4], + pub ro_nist: [NistTests; 4], +} + +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] +pub struct TrngErrors { + pub excursion_errs: [Option; 2], + pub av_repcount_errs: Option, + pub av_adaptive_errs: Option, + pub ro_repcount_errs: Option, + pub ro_adaptive_errs: Option, + pub kernel_underruns: Option, + pub server_underruns: Option, +} + #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { /// Get one or two 32-bit words of TRNG data @@ -9,5 +44,30 @@ pub(crate) enum Opcode { /// Suspend/resume callback SuspendResume, + /// Notification of an error from the interrupt handler + ErrorNotification, + + /// Subscribe to error notifications + ErrorSubscribe, + + /// Get TRNG health stats + HealthStats, + + /// Get Error stats + ErrorStats, + Quit, } + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub(crate) enum EventCallback { + Event, + Drop, +} + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub(crate) struct ScalarHook { + pub sid: (u32, u32, u32, u32), + pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API) + pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking. +} diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 9bcc0d2bc..c7320a8cf 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -2,18 +2,21 @@ pub mod api; use xous::{CID, send_message}; -use num_traits::ToPrimitive; +use num_traits::*; +use xous_ipc::Buffer; #[derive(Debug)] pub struct Trng { conn: CID, + error_sid: Option, } impl Trng { pub fn new(xns: &xous_names::XousNames) -> Result { let conn = xns.request_connection_blocking(api::SERVER_NAME_TRNG).expect("Can't connect to TRNG server"); REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); Ok(Trng { - conn + conn, + error_sid: None, }) } pub fn get_u32(&self) -> Result { @@ -38,6 +41,35 @@ impl Trng { panic!("unexpected return value: {:#?}", response); } } + pub fn hook_error_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> { + if self.error_sid.is_none() { + let sid = xous::create_server().unwrap(); + self.error_sid = Some(sid); + let sid_tuple = sid.to_u32(); + xous::create_thread_4(error_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap(); + let hookdata = api::ScalarHook { + sid: sid_tuple, + id, + cid, + }; + let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, api::Opcode::ErrorSubscribe.to_u32().unwrap()).map(|_|()) + } else { + Err(xous::Error::MemoryInUse) // can't hook it twice + } + } + pub fn get_health_tests(&self) -> Result { + let ht = api::HealthTests::default(); + let mut buf = Buffer::into_buf(ht).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, api::Opcode::HealthStats.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + Ok(buf.to_original().unwrap()) + } + pub fn get_error_stats(&self) -> Result { + let errs = api::TrngErrors::default(); + let mut buf = Buffer::into_buf(errs).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, api::Opcode::ErrorStats.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + Ok(buf.to_original().unwrap()) + } } use core::sync::atomic::{AtomicU32, Ordering}; @@ -49,4 +81,24 @@ impl Drop for Trng { unsafe{xous::disconnect(self.conn).unwrap();} } } +} + +fn error_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { + let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); + loop { + let msg = xous::receive_message(sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(api::EventCallback::Event) => xous::msg_scalar_unpack!(msg, cid, id, _, _, { + // directly pass the scalar message onto the CID with the ID memorized in the original hook + send_message(cid as u32, + xous::Message::new_scalar(id, 0, 0, 0, 0) + ).unwrap(); + }), + Some(api::EventCallback::Drop) => { + break; // this exits the loop and kills the thread + } + None => (), + } + } + xous::destroy_server(sid).unwrap(); } \ No newline at end of file diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index 1f4bff5d2..e5a89caa4 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -2,27 +2,91 @@ #![cfg_attr(target_os = "none", no_main)] mod api; +use api::*; -use num_traits::FromPrimitive; +use num_traits::*; +use xous::CID; +use xous_ipc::Buffer; use log::info; +#[derive(Copy, Clone, Debug)] +struct ScalarCallback { + server_to_cb_cid: CID, + cb_to_client_cid: CID, + cb_to_client_id: u32, +} #[cfg(target_os = "none")] mod implementation { use utralib::generated::*; - // use crate::api::*; - use log::info; + use crate::api::{ExcursionTest, MiniRunsTest, NistTests, HealthTests, TrngErrors}; use susres::{RegManager, RegOrField, SuspendResume}; + use num_traits::*; pub struct Trng { csr: utralib::CSR, - // TODO: allocate a software buffer for whitened TRNGs susres_manager: RegManager::<{utra::trng_server::TRNG_SERVER_NUMREGS}>, // probably can be reduced to save space? + conn: xous::CID, + errors: TrngErrors, + } + + fn trng_handler(_irq_no: usize, arg: *mut usize) { + let trng = unsafe { &mut *(arg as *mut Trng) }; + + let pending = trng.csr.r(utra::trng_server::EV_PENDING); + if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION0, 1)) != 0 { + trng.errors.excursion_errs[0] = Some(ExcursionTest { + min: trng.csr.rf(utra::trng_server::AV_EXCURSION0_LAST_ERR_MIN) as u16, + max: trng.csr.rf(utra::trng_server::AV_EXCURSION0_LAST_ERR_MAX) as u16, + }); + trng.csr.rmwf(utra::trng_server::AV_EXCURSION0_CTRL_RESET, 1); + } + if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION1, 1)) != 0 { + trng.errors.excursion_errs[1] = Some(ExcursionTest { + min: trng.csr.rf(utra::trng_server::AV_EXCURSION1_LAST_ERR_MIN) as u16, + max: trng.csr.rf(utra::trng_server::AV_EXCURSION1_LAST_ERR_MAX) as u16, + }); + trng.csr.rmwf(utra::trng_server::AV_EXCURSION1_CTRL_RESET, 1); + } + if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_HEALTH, 1)) != 0 { + let av_repcount = trng.csr.rf(utra::trng_server::NIST_ERRORS_AV_REPCOUNT); + let av_adaptive = trng.csr.rf(utra::trng_server::NIST_ERRORS_AV_ADAPTIVE); + let ro_repcount = trng.csr.rf(utra::trng_server::NIST_ERRORS_RO_REPCOUNT); + let ro_adaptive = trng.csr.rf(utra::trng_server::NIST_ERRORS_RO_ADAPTIVE); + if av_repcount != 0 { + trng.errors.av_repcount_errs = Some(av_repcount as u8); + } + if av_adaptive != 0 { + trng.errors.av_adaptive_errs = Some(av_adaptive as u8); + } + if ro_repcount != 0 { + trng.errors.ro_repcount_errs = Some(ro_repcount as u8); + } + if ro_adaptive != 0 { + trng.errors.ro_adaptive_errs = Some(ro_adaptive as u8); + } + } + if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_ERROR, 1)) != 0 { + if trng.csr.rf(utra::trng_server::UNDERRUNS_SERVER_UNDERRUN) != 0 { + trng.errors.server_underruns = Some(trng.csr.rf(utra::trng_server::UNDERRUNS_SERVER_UNDERRUN) as u16); + } + if trng.csr.rf(utra::trng_server::UNDERRUNS_KERNEL_UNDERRUN) != 0 { + trng.errors.kernel_underruns = Some(trng.csr.rf(utra::trng_server::UNDERRUNS_KERNEL_UNDERRUN) as u16); + } + } + // reset any error flags + trng.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1); + // clear the pending interrupt(s) + trng.csr.wo(utra::trng_server::EV_PENDING, pending); + + // notify the main loop of the error condition + xous::try_send_message(trng.conn, + xous::Message::new_scalar(crate::api::Opcode::ErrorNotification.to_usize().unwrap(), 0, 0, 0, 0)).map(|_|()).unwrap(); } impl Trng { - pub fn new() -> Trng { + pub fn new(xns: &xous_names::XousNames) -> Trng { let csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::trng_server::HW_TRNG_SERVER_BASE), None, @@ -33,7 +97,17 @@ mod implementation { let mut trng = Trng { csr: CSR::new(csr.as_mut_ptr() as *mut u32), + conn: xns.request_connection_blocking(crate::api::SERVER_NAME_TRNG).unwrap(), susres_manager: RegManager::new(csr.as_mut_ptr() as *mut u32), + errors: TrngErrors { + excursion_errs: [None; 2], + av_repcount_errs: None, + av_adaptive_errs: None, + ro_repcount_errs: None, + ro_adaptive_errs: None, + kernel_underruns: None, + server_underruns: None, + } }; ///// configure power settings and which generator to use @@ -94,7 +168,29 @@ mod implementation { ); trng.susres_manager.push(RegOrField::Reg(utra::trng_server::RO_CONFIG), None); - info!("hardware initialized"); + // handle error interrupts + xous::claim_interrupt( + utra::trng_server::TRNG_SERVER_IRQ, + trng_handler, + (&mut trng) as *mut Trng as *mut usize, + ) + .expect("couldn't claim audio irq"); + trng.csr.wo(utra::trng_server::EV_PENDING, + trng.csr.ms(utra::trng_server::EV_PENDING_ERROR, 1) + | trng.csr.ms(utra::trng_server::EV_PENDING_HEALTH, 1) + | trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION0, 1) + | trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION1, 1) + ); + trng.csr.wo(utra::trng_server::EV_ENABLE, + trng.csr.ms(utra::trng_server::EV_ENABLE_ERROR, 1) + | trng.csr.ms(utra::trng_server::EV_ENABLE_HEALTH, 1) + | trng.csr.ms(utra::trng_server::EV_ENABLE_EXCURSION0, 1) + | trng.csr.ms(utra::trng_server::EV_ENABLE_EXCURSION1, 1) + ); + trng.susres_manager.push_fixed_value(RegOrField::Reg(utra::trng_server::EV_PENDING), 0xFFFF_FFFF); + trng.susres_manager.push(RegOrField::Reg(utra::trng_server::EV_ENABLE), None); + + log::debug!("hardware initialized"); trng } @@ -104,6 +200,101 @@ mod implementation { self.csr.base } + pub fn get_errors(&self) -> TrngErrors { + self.errors + } + + pub fn get_tests(&self) -> HealthTests { + HealthTests { + av_excursion: [ + ExcursionTest { + min: self.csr.rf(utra::trng_server::AV_EXCURSION0_STAT_MIN) as u16, + max: self.csr.rf(utra::trng_server::AV_EXCURSION0_STAT_MAX) as u16, + }, + ExcursionTest { + min: self.csr.rf(utra::trng_server::AV_EXCURSION1_STAT_MIN) as u16, + max: self.csr.rf(utra::trng_server::AV_EXCURSION1_STAT_MAX) as u16, + }, + ], + av_nist: [ + NistTests { + adaptive_b: self.csr.rf(utra::trng_server::NIST_AV_STAT0_ADAP_B) as u16, + repcount_b: self.csr.rf(utra::trng_server::NIST_AV_STAT0_REP_B) as u16, + fresh: if self.csr.rf(utra::trng_server::NIST_AV_STAT0_FRESH) == 0 {false} else {true}, + }, + NistTests { + adaptive_b: self.csr.rf(utra::trng_server::NIST_AV_STAT1_ADAP_B) as u16, + repcount_b: self.csr.rf(utra::trng_server::NIST_AV_STAT1_REP_B) as u16, + fresh: if self.csr.rf(utra::trng_server::NIST_AV_STAT1_FRESH) == 0 {false} else {true}, + }, + ], + ro_miniruns: [ + MiniRunsTest { + run_count: [ + self.csr.r(utra::trng_server::RO_RUN0_COUNT1) as u16, + self.csr.r(utra::trng_server::RO_RUN0_COUNT2) as u16, + self.csr.r(utra::trng_server::RO_RUN0_COUNT3) as u16, + self.csr.r(utra::trng_server::RO_RUN0_COUNT4) as u16, + self.csr.r(utra::trng_server::RO_RUN0_COUNT5) as u16, + ], + fresh: if self.csr.rf(utra::trng_server::RO_RUN0_FRESH_RO_RUN0_FRESH) == 0 {false} else {true}, + }, + MiniRunsTest { + run_count: [ + self.csr.r(utra::trng_server::RO_RUN1_COUNT1) as u16, + self.csr.r(utra::trng_server::RO_RUN1_COUNT2) as u16, + self.csr.r(utra::trng_server::RO_RUN1_COUNT3) as u16, + self.csr.r(utra::trng_server::RO_RUN1_COUNT4) as u16, + self.csr.r(utra::trng_server::RO_RUN1_COUNT5) as u16, + ], + fresh: if self.csr.rf(utra::trng_server::RO_RUN1_FRESH_RO_RUN1_FRESH) == 0 {false} else {true}, + }, + MiniRunsTest { + run_count: [ + self.csr.r(utra::trng_server::RO_RUN2_COUNT1) as u16, + self.csr.r(utra::trng_server::RO_RUN2_COUNT2) as u16, + self.csr.r(utra::trng_server::RO_RUN2_COUNT3) as u16, + self.csr.r(utra::trng_server::RO_RUN2_COUNT4) as u16, + self.csr.r(utra::trng_server::RO_RUN2_COUNT5) as u16, + ], + fresh: if self.csr.rf(utra::trng_server::RO_RUN2_FRESH_RO_RUN2_FRESH) == 0 {false} else {true}, + }, + MiniRunsTest { + run_count: [ + self.csr.r(utra::trng_server::RO_RUN3_COUNT1) as u16, + self.csr.r(utra::trng_server::RO_RUN3_COUNT2) as u16, + self.csr.r(utra::trng_server::RO_RUN3_COUNT3) as u16, + self.csr.r(utra::trng_server::RO_RUN3_COUNT4) as u16, + self.csr.r(utra::trng_server::RO_RUN3_COUNT5) as u16, + ], + fresh: if self.csr.rf(utra::trng_server::RO_RUN3_FRESH_RO_RUN3_FRESH) == 0 {false} else {true}, + }, + ], + ro_nist: [ + NistTests { + adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT0_ADAP_B) as u16, + repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT0_REP_B) as u16, + fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT0_FRESH) == 0 {false} else {true}, + }, + NistTests { + adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16, + repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16, + fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}, + }, + NistTests { + adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16, + repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16, + fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}, + }, + NistTests { + adaptive_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_ADAP_B) as u16, + repcount_b: self.csr.rf(utra::trng_server::NIST_RO_STAT1_REP_B) as u16, + fresh: if self.csr.rf(utra::trng_server::NIST_RO_STAT1_FRESH) == 0 {false} else {true}, + }, + ] + } + } + pub fn get_data_eager(&self) -> u32 { while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { xous::yield_slice(); @@ -193,6 +384,20 @@ mod implementation { } pub fn resume(&self) { } + pub fn get_tests(&self) -> HealthTests { + HealthTests::default() + } + pub fn get_errors(&self) -> TrngErrors { + TrngErrors { + excursion_errs: [None; 2], + av_repcount_errs: None, + av_adaptive_errs: None, + ro_repcount_errs: None, + ro_adaptive_errs: None, + kernel_underruns: None, + server_underruns: None, + } + } } } @@ -368,7 +573,7 @@ fn xmain() -> ! { let trng_sid = xns.register_name(api::SERVER_NAME_TRNG).expect("can't register server"); log::trace!("registered with NS -- {:?}", trng_sid); - let mut trng = Trng::new(); + let mut trng = Trng::new(&xns); #[cfg(feature = "avalanchetest")] log::info!("TRNG built with avalanche test enabled"); @@ -387,8 +592,9 @@ fn xmain() -> ! { let sr_cid = xous::connect(trng_sid).expect("couldn't create suspend callback connection"); let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); + let mut error_cb_conns: [Option; 32] = [None; 32]; loop { - let msg = xous::receive_message(trng_sid).unwrap(); + let mut msg = xous::receive_message(trng_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { Some(api::Opcode::GetTrng) => xous::msg_blocking_scalar_unpack!(msg, count, _, _, _, { let val: [u32; 2] = trng.get_trng(count); @@ -400,6 +606,23 @@ fn xmain() -> ! { susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); trng.resume(); }), + Some(api::Opcode::ErrorSubscribe) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let hookdata = buffer.to_original::().unwrap(); + do_hook(hookdata, &mut error_cb_conns); + }, + Some(api::Opcode::ErrorNotification) => { + log::error!("Got an error condition in the TRNG. Syndrome: {:?}", trng.get_errors()); + send_event(&error_cb_conns); + }, + Some(api::Opcode::HealthStats) => { + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + buffer.replace(trng.get_tests()).unwrap(); + }, + Some(api::Opcode::ErrorStats) => { + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + buffer.replace(trng.get_errors()).unwrap(); + }, Some(api::Opcode::Quit) => { break }, @@ -410,8 +633,54 @@ fn xmain() -> ! { } // clean up our program log::trace!("main loop exit, destroying servers"); + unhook(&mut error_cb_conns); xns.unregister_server(trng_sid).unwrap(); xous::destroy_server(trng_sid).unwrap(); log::trace!("quitting"); xous::terminate_process(0) } + + +fn do_hook(hookdata: ScalarHook, cb_conns: &mut [Option; 32]) { + let (s0, s1, s2, s3) = hookdata.sid; + let sid = xous::SID::from_u32(s0, s1, s2, s3); + let server_to_cb_cid = xous::connect(sid).unwrap(); + let cb_dat = Some(ScalarCallback { + server_to_cb_cid, + cb_to_client_cid: hookdata.cid, + cb_to_client_id: hookdata.id, + }); + let mut found = false; + for entry in cb_conns.iter_mut() { + if entry.is_none() { + *entry = cb_dat; + found = true; + break; + } + } + if !found { + log::error!("ran out of space registering callback"); + } +} +fn unhook(cb_conns: &mut [Option; 32]) { + for entry in cb_conns.iter_mut() { + if let Some(scb) = entry { + xous::send_message(scb.server_to_cb_cid, + xous::Message::new_blocking_scalar(api::EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); + unsafe{xous::disconnect(scb.server_to_cb_cid).unwrap();} + } + *entry = None; + } +} +fn send_event(cb_conns: &[Option; 32]) { + for entry in cb_conns.iter() { + if let Some(scb) = entry { + // note that the "which" argument is only used for GPIO events, to indicate which pin had the event + xous::send_message(scb.server_to_cb_cid, + xous::Message::new_scalar(api::EventCallback::Event.to_usize().unwrap(), + scb.cb_to_client_cid as usize, scb.cb_to_client_id as usize, 0, 0) + ).unwrap(); + }; + } +}