diff --git a/Cargo.lock b/Cargo.lock index cd8065999..23cfd12ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -527,6 +527,23 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "engine-25519" +version = "0.1.0" +dependencies = [ + "log", + "log-server", + "num-derive", + "num-traits", + "rkyv 0.4.3", + "susres", + "ticktimer-server", + "utralib", + "xous", + "xous-ipc", + "xous-names", +] + [[package]] name = "engine-sha512" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1c058bb91..2b649faf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ default-members = [ "services/susres", "services/codec", "services/engine-sha512", + "services/engine-25519", "services/aes", ] members = [ @@ -64,6 +65,7 @@ members = [ "services/susres", "services/codec", "services/engine-sha512", + "services/engine-25519", "services/aes", ] resolver = "2" diff --git a/precursors/soc.svd b/precursors/soc.svd index f70d56c14..c7e186a6a 100755 --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -3451,7 +3451,7 @@ inject too fast.]]> SEED1 0x0000 - 0x33e603a3 + 0x4d93c1cf 32 @@ -3466,7 +3466,7 @@ inject too fast.]]> SEED0 0x0004 - 0xf1d9b09 + 0x1d257e0e 32 @@ -6406,10 +6406,25 @@ microcode instructions starting at mpstart]]> - POWER + MPRESUME 0x0010 0x00 32 + + + mpresume + 9 + [9:0] + 0 + + + + + + POWER + 0x0014 + 0x00 + 32 on @@ -6433,7 +6448,7 @@ pause has happened. Used to interrupt flow for suspend/resume.]]> STATUS - 0x0014 + 0x0018 0x00 32 @@ -6465,7 +6480,7 @@ be read out for suspend/resume]]> EV_STATUS - 0x0018 + 0x001c 0x00 32 @@ -6488,7 +6503,7 @@ be read out for suspend/resume]]> EV_PENDING - 0x001c + 0x0020 0x00 32 @@ -6511,7 +6526,7 @@ be read out for suspend/resume]]> EV_ENABLE - 0x0020 + 0x0024 0x00 32 @@ -6535,7 +6550,7 @@ be read out for suspend/resume]]> INSTRUCTION - 0x0024 + 0x0028 0x00 32 @@ -6593,7 +6608,7 @@ exactly reflects the binary layout of an Engine instruction.]]> 0 - 0x28 + 0x2c registers diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin index a59ebd4da..eb3a0a6ea 100755 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/engine-25519/Cargo.toml b/services/engine-25519/Cargo.toml index 795bb6781..52a9591b2 100644 --- a/services/engine-25519/Cargo.toml +++ b/services/engine-25519/Cargo.toml @@ -11,9 +11,16 @@ log-server = { path = "../log-server" } ticktimer-server = { path = "../ticktimer-server" } xous-names = { path = "../xous-names" } log = "0.4" +susres = {path = "../susres"} + +xous-ipc = {path="../../xous-ipc"} 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"]} + +#curve25519-dalek = {git = "https://github.com/betrusted-io/curve25519-dalek.git", rev = "2ea17d554a54066efb3bc4895bee92e5b2e1bd50"} +#curve25519-dalek-hw = {path = "../../../curve25519-dalek"} +#curve25519-dalek = "3.1.0" [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/engine-25519/src/api.rs b/services/engine-25519/src/api.rs index 98a74e272..fffa0c286 100644 --- a/services/engine-25519/src/api.rs +++ b/services/engine-25519/src/api.rs @@ -1,10 +1,61 @@ -pub(crate) const SERVER_NAME_CODEC: &str = "_Low-level Audio Codec Server_"; +pub(crate) const SERVER_NAME_ENGINE25519: &str = "_Curve-25519 Accelerator Engine_"; + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub (crate) struct Job { + /// unique identifier so the server knows who the request is coming from + id: [u32; 3], + /// start location for microcode load + uc_start: u32, + /// length of the microcode to run + uc_len: u32, + /// microcode program + ucode: [u32; 1024], + /// initial register file contents (contains any arguments to the program) + rf: [u32; 64], + /// which register window, if any, to use for the job + window: Option, +} -#[allow(dead_code)] #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { - + /// Acquire an exclusive lock on the hardware + /// sends a 96-bit random key + config word, returns true or false if acquisition was successful + /// note: 96-bit space has a pcollision=10^-18 for 400,000 concurrent hash requests, + /// pcollision=10^-15 for 13,000,000 concurrent hash requests. + /// for context, a typical consumer SSD has an uncorrectable bit error rate of 10^-15, + /// and we probably expect about 3-4 concurrent hash requests in the worst case. + /// Acquisition will always fail if a Suspend request is pending. + AcquireExclusive, - /// Suspend/resume callback - SuspendResume, + /// Used by higher level coordination processes to acquire a lock on the hardware unit + /// to prevent any new transactions from occuring. The lock is automatically cleared on + /// a resume, or by an explicit release + AcquireSuspendLock, + /// this is to be used if we decided in the end we aren't going to suspend. + AbortSuspendLock, + + /// Runs a job + RunJob, + + /// a function that can be polled to determine if the block has been currently acquired + IsIdle, + + /// IRQ handler feedback + EngineDone, + IllegalOpcode, + + /// exit the server + Quit, + + // note that suspend/resume is handled by a secondary thread that can concurrently + // interrupt the main thread and store the state } + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub(crate) enum JobResult { + /// returns a copy of the entire register file as a result + Result([u32; 64]), + SuspendError, + Uninitialized, + IdMismatch, +} \ No newline at end of file diff --git a/services/engine-25519/src/lib.rs b/services/engine-25519/src/lib.rs index e6e31810c..4d0cde2d7 100644 --- a/services/engine-25519/src/lib.rs +++ b/services/engine-25519/src/lib.rs @@ -1,5 +1,13 @@ #![cfg_attr(target_os = "none", no_std)] +/* +#[cfg(target_os = "none")] +pub use curve25519_dalek_hw::*; + +#[cfg(not(target_os = "none"))] +pub use curve25519_dalek::*; + + pub mod api; use xous::{CID, send_message}; use num_traits::ToPrimitive; @@ -26,4 +34,5 @@ impl Drop for Codec { unsafe{xous::disconnect(self.conn).unwrap();} } } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/services/engine-25519/src/main.rs b/services/engine-25519/src/main.rs index 00507117c..99e676c01 100644 --- a/services/engine-25519/src/main.rs +++ b/services/engine-25519/src/main.rs @@ -2,56 +2,163 @@ #![cfg_attr(target_os = "none", no_main)] mod api; +use api::*; -use num_traits::FromPrimitive; - -use log::info; - +use num_traits::*; +use core::sync::atomic::{AtomicBool, Ordering}; #[cfg(target_os = "none")] mod implementation { use utralib::generated::*; - // use crate::api::*; - use log::info; - use susres::{RegManager, RegOrField, SuspendResume}; + use crate::api::*; + use susres::{ManagedMem, RegManager, RegOrField, SuspendResume}; + use num_traits::*; - pub struct Codec { + pub struct Engine25519 { csr: utralib::CSR, - fifo: xous::MemoryRange, - susres_manager: RegManager::<{utra::audio::AUDIO_NUMREGS}>, + mem: xous::MemoryRange, + susres: RegManager::<{utra::engine::ENGINE_NUMREGS}>, + handler_conn: Option, + sr_backing: ManagedMem::<{utralib::generated::HW_ENGINE_MEM_LEN}>, + mpc_resume: Option, + clean_resume: Option, } + fn handle_engine_irq(_irq_no: usize, arg: *mut usize) { + let engine = unsafe { &mut *(arg as *mut Engine25519) }; - impl Codec { - pub fn new() -> Codec { + let reason = engine.csr.r(utra::engine::EV_PENDING); + + if let Some(conn) = engine.handler_conn { + if reason & engine.csr.ms(utra::engine::EV_PENDING_FINISHED, 1) != 0 { + xous::try_send_message(conn, + xous::Message::new_scalar(Opcode::EngineDone.to_usize().unwrap(), + 0, 0, 0, 0)).map(|_|()).unwrap(); + } + if reason & engine.csr.ms(utra::engine::EV_PENDING_ILLEGAL_OPCODE, 1) != 0 { + xous::try_send_message(conn, + xous::Message::new_scalar(Opcode::IllegalOpcode.to_usize().unwrap(), + 0, 0, 0, 0)).map(|_|()).unwrap(); + } + } else { + panic!("engine interrupt happened with a handler"); + } + // clear the interrupt + engine.csr + .wo(utra::engine::EV_PENDING, reason); + } + impl Engine25519 { + pub fn new(handler_conn: xous::CID) -> Engine25519 { let csr = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::audio::HW_AUDIO_BASE), + xous::MemoryAddress::new(utra::engine::HW_ENGINE_BASE), None, 4096, xous::MemoryFlags::R | xous::MemoryFlags::W, ) - .expect("couldn't map Audio CSR range"); - let fifo = xous::syscall::map_memory( - xous::MemoryAddress::new(utralib::HW_AUDIO_MEM), + .expect("couldn't map engine CSR range"); + let mem = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::HW_ENGINE_MEM), None, - utralib::HW_AUDIO_MEM_LEN, + utralib::HW_ENGINE_MEM_LEN, xous::MemoryFlags::R | xous::MemoryFlags::W, ) - .expect("couldn't map Audio CSR range"); + .expect("couldn't map engine memory window range"); - let mut codec = Codec { + let mut engine = Engine25519 { csr: CSR::new(csr.as_mut_ptr() as *mut u32), - susres_manager: RegManager::new(csr.as_mut_ptr() as *mut u32), - fifo, + susres: RegManager::new(csr.as_mut_ptr() as *mut u32), + mem, + handler_conn: Some(handler_conn), + sr_backing: ManagedMem::new(mem), + mpc_resume: None, + clean_resume: None, }; - codec + xous::claim_interrupt( + utra::engine::ENGINE_IRQ, + handle_engine_irq, + (&mut engine) as *mut Engine25519 as *mut usize, + ) + .expect("couldn't claim Power irq"); + + engine.csr.wo(utra::engine::EV_PENDING, 0xFFFF_FFFF); // clear any droppings. + engine.csr.wo(utra::engine::EV_ENABLE, + engine.csr.ms(utra::engine::EV_ENABLE_FINISHED, 1) | + engine.csr.ms(utra::engine::EV_ENABLE_ILLEGAL_OPCODE, 1) + ); + + // setup the susres context. Most of the defaults are fine, so they aren't explicitly initialized in the code above, + // but they still have to show up down here in case we're suspended mid-op + engine.susres.push(RegOrField::Reg(utra::engine::POWER), None); // on resume, this needs to be setup first, so that the "pause" state is captured correctly + engine.susres.push(RegOrField::Reg(utra::engine::WINDOW), None); + engine.susres.push(RegOrField::Reg(utra::engine::MPSTART), None); + engine.susres.push(RegOrField::Reg(utra::engine::MPLEN), None); + engine.susres.push_fixed_value(RegOrField::Reg(utra::engine::EV_PENDING), 0xFFFF_FFFF); + engine.susres.push(RegOrField::Reg(utra::engine::EV_ENABLE), None); + + // engine.susres.push(RegOrField::Reg(utra::engine::CONTROL), None); // don't push this, we need to manually coordinate `mpcresume` before resuming + + engine } pub fn suspend(&mut self) { - self.susres_manager.suspend(); + if self.csr.rf(utra::engine::STATUS_RUNNING) == 1 { + // request a pause from the engine. it will stop executing at the next microcode op + // and assert STATUS_PAUSE_GNT + self.csr.rmwf(utra::engine::POWER_PAUSE_REQ, 1); + while (self.csr.rf(utra::engine::STATUS_PAUSE_GNT) == 0) && (self.csr.rf(utra::engine::STATUS_RUNNING) == 1) { + // busy wait for this to clear, or the engine to stop running; should happen in << 1us + if self.csr.rf(utra::engine::STATUS_PAUSE_GNT) == 1 { + // store the current PC value as a resume note + self.mpc_resume = Some(self.csr.rf(utra::engine::STATUS_MPC)); + } else { + // the implication here is the engine actually finished its last opcode, so it would not enter the paused state; + // rather, it is now stopped. + self.mpc_resume = None; + } + } + + } else { + self.mpc_resume = None; + } + // copy the ucode & rf out to the backing memory + self.sr_backing.suspend(); + // now backup all the machine registers + self.susres.suspend(); + self.clean_resume = None; } pub fn resume(&mut self) { - self.susres_manager.resume(); + // "power" should be resumed first by the restore, which would set the pause bit, if it was previously set + self.susres.resume(); + self.sr_backing.resume(); + + // in the case of a resume from pause, we need to specify the PC to resume from + // clear the pause + if let Some(mpc) = self.mpc_resume { + if self.csr.rf(utra::engine::POWER_PAUSE_REQ) != 1 { + log::error!("resuming from an unexpected state: we had mpc of {} set, but pause was not requested!", mpc); + self.clean_resume = Some(false); + // we don't resume execution. Presumably this will cause terrible things to happen such as + // the interrupt waiting for execution to be done to never trigger. + // perhaps we could try to trigger that somehow...? + } else { + // the pause was requested, but crucially, the engine was not in the "go" state. This means that + // the engine will get its starting PC from the resume PC when we hit go again, instead of the mpstart register. + self.csr.wfo(utra::engine::MPRESUME_MPRESUME, mpc); + // start the engine + self.csr.wfo(utra::engine::CONTROL_GO, 1); + // it should grab the PC from `mpresume` and then go to the paused state. Wait until + // we have achieved the identical paused state that happened before resume, before unpausing! + while self.csr.rf(utra::engine::STATUS_PAUSE_GNT) == 0 { + // this should be very fast, within a couple CPU cycles + } + self.clean_resume = Some(true); // note that we had a clean resume before resuming the execution + // this resumes execution of the CPU + self.csr.rmwf(utra::engine::POWER_PAUSE_REQ, 0); + } + } else { + // if we didn't have a resume PC set, we weren't paused, so we just continue on our merry way. + self.clean_resume = Some(true); + } } } } @@ -61,12 +168,12 @@ mod implementation { mod implementation { use log::info; - pub struct Codec { + pub struct Engine25519 { } - impl Codec { - pub fn new() -> Codec { - Codec { + impl Engine25519 { + pub fn new() -> Engine25519 { + Engine25519 { } } pub fn suspend(&self) { @@ -76,34 +183,78 @@ mod implementation { } } +static RUN_IN_PROGRESS: AtomicBool = AtomicBool::new(false); +static SUSPEND_FAILURE: AtomicBool = AtomicBool::new(false); +static SUSPEND_PENDING: AtomicBool = AtomicBool::new(false); + +fn susres_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { + let susres_sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); + let xns = xous_names::XousNames::new().unwrap(); + + // register a suspend/resume listener + let sr_cid = xous::connect(susres_sid).expect("couldn't create suspend callback connection"); + let mut susres = susres::Susres::new(&xns, api::SusResOps::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); + + log::trace!("starting engine25519 suspend/resume manager loop"); + loop { + let msg = xous::receive_message(susres_sid).unwrap(); + log::trace!("Message: {:?}", msg); + match FromPrimitive::from_usize(msg.body.id()) { + Some(SusResOps::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { + SUSPEND_PENDING.store(true, Ordering::Relaxed); + while RUN_IN_PROGRESS.load(Ordering::Relaxed) { + xous::yield_slice(); + } + if susres.suspend_until_resume(token).expect("couldn't execute suspend/resume") == false { + SUSPEND_FAILURE.store(true, Ordering::Relaxed); + } else { + SUSPEND_FAILURE.store(false, Ordering::Relaxed); + } + SUSPEND_PENDING.store(false, Ordering::Relaxed); + }), + Some(SusResOps::Quit) => { + log::info!("Received quit opcode, exiting!"); + break; + } + None => { + log::error!("Received unknown opcode: {:?}", msg); + } + } + } + xns.unregister_server(susres_sid).unwrap(); + xous::destroy_server(susres_sid).unwrap(); +} #[xous::xous_main] fn xmain() -> ! { - use crate::implementation::Codec; + use crate::implementation::Engine25519; log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); - info!("my PID is {}", xous::process::id()); + log::set_max_level(log::LevelFilter::Trace); + log::info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); - let codec_sid = xns.register_name(api::SERVER_NAME_CODEC).expect("can't register server"); - log::trace!("registered with NS -- {:?}", codec_sid); + let engine25519_sid = xns.register_name(api::SERVER_NAME_ENGINE25519, None).expect("can't register server"); + log::trace!("registered with NS -- {:?}", engine25519_sid); - let mut codec = Codec::new(); + let handler_conn = xous::connect(engine25519_sid).expect("couldn't create IRQ handler connection"); + let mut engine25519 = Engine25519::new(handler_conn); log::trace!("ready to accept requests"); - // register a suspend/resume listener - let sr_cid = xous::connect(codec_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"); + // handle suspend/resume with a separate thread, which monitors our in-progress state + // we can't save hardware state of a hash, so the hash MUST finish before we can suspend. + let susres_mgr_sid = xous::create_server().unwrap(); + let (sid0, sid1, sid2, sid3) = susres_mgr_sid.to_u32(); + xous::create_thread_4(susres_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).expect("couldn't start susres handler thread"); loop { - let msg = xous::receive_message(codec_sid).unwrap(); + let msg = xous::receive_message(engine25519_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { - codec.suspend(); + engine25519.suspend(); susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); - codec.resume(); + engine25519.resume(); }), None => { log::error!("couldn't convert opcode"); @@ -113,8 +264,8 @@ fn xmain() -> ! { } // clean up our program log::trace!("main loop exit, destroying servers"); - xns.unregister_server(codec_sid).unwrap(); - xous::destroy_server(codec_sid).unwrap(); + xns.unregister_server(engine25519_sid).unwrap(); + xous::destroy_server(engine25519_sid).unwrap(); log::trace!("quitting"); xous::terminate_process(0) } diff --git a/services/susres/src/api.rs b/services/susres/src/api.rs index d30b0e5c8..2d911f4a0 100644 --- a/services/susres/src/api.rs +++ b/services/susres/src/api.rs @@ -114,7 +114,7 @@ impl RegManager:: where ManagedReg: core::marker::Copy { } panic!("Ran out of space pushing to suspend/resume manager structure. Please increase the allocated size!"); } - // push registers into the manager in the order you want them suspended + // push registers into the manager in the order that you would normally initialized them pub fn push(&mut self, mr: RegOrField, mask_override: Option) { let mrt = ManagedReg { item: mr, diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 7cedbe87e..be1029dc4 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -53,6 +53,7 @@ fn try_main() -> Result<(), DynError> { "susres", "codec", "engine-sha512", + "engine-25519", ]; let benchmark_pkgs = [ "benchmark",