diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index f4192b319..764dd4d93 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -118,4 +118,5 @@ dbg-ecupdate = [] # for issuing manual commands to initiate an EC update (vs UX) test-rekey = [] nettest = [] # batch network tests tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"] -default = [] # "debugprint" +perfcounter = [] +default = ["perfcounter"] # "debugprint" diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 0bbdebae8..27c9820c0 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -1,6 +1,8 @@ use xous::{MessageEnvelope}; use xous_ipc::String; use core::fmt::Write; +#[cfg(feature="perfcounter")] +use utralib::generated::*; use std::collections::HashMap; /////////////////////////// Common items to all commands @@ -48,6 +50,11 @@ pub struct CommonEnv { netmgr: net::NetManager, xns: xous_names::XousNames, boot_instant: std::time::Instant, + /// make this communal so any number of commands can trigger or reset the performance counter, and/or perform logging + #[cfg(feature="perfcounter")] + perf_csr: CSR, + #[cfg(feature="perfcounter")] + event_csr: CSR::, } impl CommonEnv { pub fn register_handler(&mut self, verb: String::<256>) -> u32 { @@ -161,6 +168,23 @@ pub struct CmdEnv { impl CmdEnv { pub fn new(xns: &xous_names::XousNames) -> CmdEnv { let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); + #[cfg(feature="perfcounter")] + let perf_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::perfcounter::HW_PERFCOUNTER_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map perfcounter CSR range"); + #[cfg(feature="perfcounter")] + let event1_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::event_source1::HW_EVENT_SOURCE1_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map event1 CSR range"); + let common = CommonEnv { llio: llio::Llio::new(&xns), com: com::Com::new(&xns).expect("could't connect to COM"), @@ -171,6 +195,10 @@ impl CmdEnv { xns: xous_names::XousNames::new().unwrap(), netmgr: net::NetManager::new(), boot_instant: std::time::Instant::now(), + #[cfg(feature="perfcounter")] + perf_csr: CSR::new(perf_csr.as_mut_ptr() as *mut u32), + #[cfg(feature="perfcounter")] + event_csr: CSR::new(event1_csr.as_mut_ptr() as *mut u32), }; //let fcc = Fcc::new(&mut common); #[cfg(feature="benchmarks")] diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 4909cd8a9..1e3f58d11 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -22,6 +22,8 @@ use gam::DecodePng; use std::convert::TryInto; #[cfg(feature="tls")] use tungstenite::{WebSocket, stream::MaybeTlsStream}; +#[cfg(feature="perfcounter")] +use utralib::generated::*; pub struct NetCmd { callback_id: Option, @@ -498,6 +500,77 @@ impl<'a> ShellCmdApi<'a> for NetCmd { log::info!("{}", std::str::from_utf8(&plaintext).unwrap_or("utf-error")); log::set_max_level(log::LevelFilter::Info); } + #[cfg(feature="perfcounter")] + "ctime" => { + log::info!("constant time tests"); + env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1); + + // stop the counter if it would rollover + env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT0, 0xFFFF_FFFF); + env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT1, 0); + + // configure the system + env.perf_csr.wo(utra::perfcounter::CONFIG, + env.perf_csr.ms(utra::perfcounter::CONFIG_PRESCALER, 0) + | env.perf_csr.ms(utra::perfcounter::CONFIG_SATURATE, 1) + | env.perf_csr.ms(utra::perfcounter::CONFIG_EVENT_WIDTH_MINUS_ONE, 31) + ); + + // this starts the performance counter + log::info!("== restart =="); + env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1); + + use aes::Aes256; + use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit}; + use aes::cipher::generic_array::GenericArray; + + let mut key_array: [u8; 32]; + let mut data_array: [u8; 16]; + const MAX_ENTRIES: u32 = 4096; + let mut entries: u32 = 0; // track the number of entries we've used up + for databit in 0..128 { + data_array = [0; 16]; + data_array[databit / 8] = 1 << databit % 8; + for keybit in 0..256 { + key_array = [0; 32]; + key_array[keybit / 8] = 1 << keybit % 8; + let cipher_hw = Aes256::new(&key_array.into()); + let mut block = GenericArray::clone_from_slice(&mut data_array); + // demarcate the encryption operation performance counter events + entries += 1; + env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, + ((databit as u32) << 8) | keybit as u32); + cipher_hw.encrypt_block(&mut block); + env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, + 0x100_0000 | ((databit as u32) << 8) | keybit as u32); + entries += 1; + if entries >= MAX_ENTRIES { + // stop the counter + env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1); + // read out the events and print them + while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 { + let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0); + let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1); + let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX); + log::info!("{}:{}:{}", index, time, code) + } + // restart the counter + log::info!("== restart =="); + entries = 0; + env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1); + } + } + } + // stop the counter + env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1); + // read out the events and print them + while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 { + let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0); + let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1); + let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX); + log::info!("{}:{}:{}", index, time, code) + } + } #[cfg(any(target_os = "none", target_os = "xous"))] "ping" => { if let Some(name) = tokens.next() {