engine25519 doesn't crash on boot, at least.

ManagedMemory structure for susres has some issues with large
blocks, so use "heap-allocated" structures
This commit is contained in:
bunnie 2021-06-24 03:17:32 +08:00
parent 16ec932286
commit ae25142acb
10 changed files with 616 additions and 90 deletions

1
Cargo.lock generated
View file

@ -1553,6 +1553,7 @@ dependencies = [
"com", "com",
"content-plugin-api", "content-plugin-api",
"digest", "digest",
"engine-25519",
"engine-sha512", "engine-sha512",
"gam", "gam",
"graphics-server", "graphics-server",

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" > <device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor> <vendor>litex</vendor>
<name>SOC</name> <name>SOC</name>
<description><![CDATA[Litex SoC 2021-06-22 01:01:29]]></description> <description><![CDATA[Litex SoC 2021-06-24 00:12:05]]></description>
<addressUnitBits>8</addressUnitBits> <addressUnitBits>8</addressUnitBits>
<width>32</width> <width>32</width>
@ -3451,7 +3451,7 @@ inject too fast.]]></description>
<name>SEED1</name> <name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description> <description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset> <addressOffset>0x0000</addressOffset>
<resetValue>0x4d93c1cf</resetValue> <resetValue>0x369cae83</resetValue>
<size>32</size> <size>32</size>
<fields> <fields>
<field> <field>
@ -3466,7 +3466,7 @@ inject too fast.]]></description>
<name>SEED0</name> <name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description> <description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset> <addressOffset>0x0004</addressOffset>
<resetValue>0x1d257e0e</resetValue> <resetValue>0x61e52323</resetValue>
<size>32</size> <size>32</size>
<fields> <fields>
<field> <field>

Binary file not shown.

View file

@ -1,23 +1,42 @@
pub(crate) const SERVER_NAME_ENGINE25519: &str = "_Curve-25519 Accelerator Engine_"; pub(crate) const SERVER_NAME_ENGINE25519: &str = "_Curve-25519 Accelerator Engine_";
// I don't understand why clippy says these are unused. But clippy is wrong. Shut up, clippy.
#[allow(dead_code)]
pub(crate) const UCODE_U8_BASE: usize = 0x0;
#[allow(dead_code)]
pub(crate) const UCODE_U32_BASE: usize = 0x0;
#[allow(dead_code)]
pub(crate) const UCODE_U32_SIZE: usize = 0x1000 / 4;
#[allow(dead_code)]
pub(crate) const UCODE_U8_SIZE: usize = 0x1000;
#[allow(dead_code)]
pub(crate) const RF_U8_BASE: usize = 0x1_0000;
#[allow(dead_code)]
pub(crate) const RF_U32_BASE: usize = 0x1_0000 / 4;
#[allow(dead_code)]
pub(crate) const RF_U32_SIZE: usize = 0x4000 / 4;
#[allow(dead_code)]
pub(crate) const RF_U8_SIZE: usize = 0x4000;
pub(crate) const NUM_REGS: usize = 32; pub(crate) const NUM_REGS: usize = 32;
pub(crate) const BITWIDTH: usize = 256; pub(crate) const BITWIDTH: usize = 256;
pub(crate) const RF_SIZE_IN_U32: usize = NUM_REGS*(BITWIDTH/core::mem::size_of(u32)); // 32 registers, 256 bits/register, divided by 4 bytes per u32 pub const RF_SIZE_IN_U32: usize = NUM_REGS*(BITWIDTH/core::mem::size_of::<u32>()); // 32 registers, 256 bits/register, divided by 4 bytes per u32
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone, Copy)]
pub (crate) struct Job { pub struct Job {
/// the SID of the server to which we should return results. Ideally, this is an application-specific server, and not your main loop server. /// If present the SID of the server to which we should return results asynchronously.
id: [u32; 4], /// If None, then the job will run synchronously.
pub id: Option<[u32; 4]>,
/// start location for microcode load /// start location for microcode load
uc_start: u32, pub uc_start: u32,
/// length of the microcode to run /// length of the microcode to run
uc_len: u32, pub uc_len: u32,
/// microcode program /// microcode program
ucode: [u32; 1024], pub ucode: [u32; 1024],
/// initial register file contents (also contains any arguments to the program) /// initial register file contents (also contains any arguments to the program)
rf: [u32; RF_SIZE_IN_U32], pub rf: [u32; RF_SIZE_IN_U32],
/// which register window, if any, to use for the job /// which register window, if any, to use for the job
window: Option<u8>, pub window: Option<u8>,
} }
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
@ -32,8 +51,7 @@ pub(crate) enum Opcode {
EngineDone, EngineDone,
IllegalOpcode, IllegalOpcode,
/// Suspend/resume callback // note: suspend/resume handled by a separate thread and server
SuspendResume,
/// exit the server /// exit the server
Quit, Quit,
@ -50,11 +68,20 @@ pub(crate) enum Return {
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) enum JobResult { pub enum JobResult {
/// returns a copy of the entire register file as a result /// returns a copy of the entire register file as a result
Result([u32; RF_SIZE_IN_U32]), Result([u32; RF_SIZE_IN_U32]),
Started, Started,
EngineUnavailable, EngineUnavailable,
NotAsyncObject, // attempt to run an async job on an object that was setup for sync jobs
IllegalOpcodeException, IllegalOpcodeException,
SuspendError, SuspendError,
} }
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum SusResOps {
/// Suspend/resume callback
SuspendResume,
/// exit the thread
Quit,
}

View file

@ -9,19 +9,20 @@ pub use curve25519_dalek::*;
*/ */
pub mod api; pub mod api;
use api::*; pub use api::*;
use xous::{CID, send_message}; use xous::{CID, Message};
use num_traits::ToPrimitive; use num_traits::*;
use xous_ipc::Buffer;
static mut ENGINE_CB: Option<fn(JobResult)> = None; static mut ENGINE_CB: Option<fn(JobResult)> = None;
pub struct Engine25519 { pub struct Engine25519 {
conn: CID, conn: CID,
cb_sid: [u32; 4], cb_sid: Option<[u32; 4]>,
} }
impl Engine25519 { impl Engine25519 {
pub fn new() -> Result<Self, xous::Error> { // this is used to set up a system with async callbacks
let xns = xous_names::XousNames::new().unwrap(); pub fn new_async(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
let conn = xns.request_connection_blocking(api::SERVER_NAME_ENGINE25519).expect("Can't connect to Engine25519 server"); let conn = xns.request_connection_blocking(api::SERVER_NAME_ENGINE25519).expect("Can't connect to Engine25519 server");
let sid = xous::create_server().unwrap(); let sid = xous::create_server().unwrap();
@ -31,29 +32,81 @@ impl Engine25519 {
Ok(Engine25519 { Ok(Engine25519 {
conn, conn,
cb_sid: sid.to_array(), cb_sid: Some(sid.to_array()),
}) })
} }
// typically, we just want synchronous callbacks. This integrates more nicely into single-threaded code.
pub fn new() -> Self {
let xns = xous_names::XousNames::new().unwrap();
let conn = xns.request_connection_blocking(api::SERVER_NAME_ENGINE25519).expect("Can't connect to Engine25519 server");
pub fn spawn_job(job: Job) -> Result<bool, xous::Error> { Engine25519 {
let mut buf = Buffer::into_buf(job).or(ERr(xous::Error::InternalError))?; conn,
cb_sid: None,
}
}
/// this is unsafe because the caller must make sure that within their process space, multiple threads
/// are not spawing concurrent jobs; or, if they do, they all share a common result_callback method:
/// we always blindly replace result_callback!
pub unsafe fn spawn_async_job(&mut self, job: &mut Job, result_callback: fn(JobResult)) -> Result<bool, xous::Error> {
if let Some(cb_sid) = self.cb_sid {
ENGINE_CB = Some(result_callback); // this is the unsafe bit!
job.id = Some(cb_sid);
let mut buf = Buffer::into_buf(*job).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
JobResult::Started => Ok(true),
_ => Ok(false)
}
} else {
Err(xous::Error::InvalidSyscall)
}
}
/// this is a blocking version of spawn_async_job.
/// if the engine is free, it will block until a result is returned
/// if the engine is busy, it will return an EngineUnavailable result.
pub fn spawn_job(&mut self, job: Job) -> Result<[u32; RF_SIZE_IN_U32], xous::Error> {
if job.id.is_some() {
return Err(xous::Error::InvalidSyscall) // the job.id should be None for a sync job. Do not set it to Some, even as a joke.
}
let mut buf = Buffer::into_buf(job).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() { match buf.to_original().unwrap() {
JobResult::Started => Ok(true), JobResult::Result(rf) => {
_ => Ok(false) Ok(rf)
},
JobResult::EngineUnavailable => {
Err(xous::Error::ServerQueueFull)
},
JobResult::IllegalOpcodeException => {
Err(xous::Error::InvalidString)
},
_ => {
Err(xous::Error::InternalError)
}
} }
} }
} }
impl Drop for Engine25519 { impl Drop for Engine25519 {
fn drop(&mut self) { fn drop(&mut self) {
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. // disconnect from the main server
unsafe{xous::disconnect(self.conn).unwrap();} unsafe{xous::disconnect(self.conn).unwrap();}
// disconnect and destroy the callback server
if let Some(cb_sid) = self.cb_sid {
let cid = xous::connect(xous::SID::from_array(cb_sid)).unwrap();
xous::send_message(cid,
Message::new_scalar(Return::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
unsafe{xous::disconnect(cid).unwrap();}
}
} }
} }
/// handles callback messages from I2C server, in the library user's process space. /// handles callback messages from the engine server, in the library user's process space.
fn engine_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { fn engine_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); let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
loop { loop {

View file

@ -7,48 +7,72 @@ use api::*;
use num_traits::*; use num_traits::*;
use core::sync::atomic::{AtomicBool, Ordering}; use core::sync::atomic::{AtomicBool, Ordering};
use xous::msg_blocking_scalar_unpack; use xous::msg_blocking_scalar_unpack;
use xous_ipc::Buffer;
static RUN_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
static DISALLOW_SUSPEND: AtomicBool = AtomicBool::new(false);
static SUSPEND_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
#[cfg(target_os = "none")] #[cfg(target_os = "none")]
mod implementation { mod implementation {
use utralib::generated::*; use utralib::generated::*;
use crate::api::*; use crate::api::*;
use susres::{ManagedMem, RegManager, RegOrField, SuspendResume}; use susres::{/*ManagedMem,*/ RegManager, RegOrField, SuspendResume};
use num_traits::*; use num_traits::*;
//use core::num::NonZeroUsize;
use core::sync::atomic::Ordering;
use crate::RUN_IN_PROGRESS;
use crate::DISALLOW_SUSPEND;
pub struct Engine25519 { pub struct Engine25519Hw {
csr: utralib::CSR<u32>, csr: utralib::CSR<u32>,
mem: xous::MemoryRange, mem: xous::MemoryRange,
susres: RegManager::<{utra::engine::ENGINE_NUMREGS}>, susres: RegManager::<{utra::engine::ENGINE_NUMREGS}>,
handler_conn: Option<xous::CID>, handler_conn: Option<xous::CID>,
sr_backing: ManagedMem::<{utralib::generated::HW_ENGINE_MEM_LEN}>, //ucode_backing: ManagedMem::<UCODE_U32_SIZE>,
//rf_backing: ManagedMem::<RF_U32_SIZE>,
ucode_backing: xous::MemoryRange,
rf_backing: xous::MemoryRange,
mpc_resume: Option<u32>, mpc_resume: Option<u32>,
clean_resume: Option<bool>, clean_resume: Option<bool>,
do_notify: bool,
illegal_opcode: bool,
} }
fn handle_engine_irq(_irq_no: usize, arg: *mut usize) { fn handle_engine_irq(_irq_no: usize, arg: *mut usize) {
let engine = unsafe { &mut *(arg as *mut Engine25519) }; let engine = unsafe { &mut *(arg as *mut Engine25519Hw) };
let reason = engine.csr.r(utra::engine::EV_PENDING); let reason = engine.csr.r(utra::engine::EV_PENDING);
RUN_IN_PROGRESS.store(false, Ordering::Relaxed);
if let Some(conn) = engine.handler_conn { if reason & engine.csr.ms(utra::engine::EV_PENDING_ILLEGAL_OPCODE, 1) != 0 {
if reason & engine.csr.ms(utra::engine::EV_PENDING_FINISHED, 1) != 0 { engine.illegal_opcode = true;
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 { } else {
panic!("engine interrupt happened with a handler"); engine.illegal_opcode = false;
}
if engine.do_notify {
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 without a handler");
}
} }
// clear the interrupt // clear the interrupt
engine.csr engine.csr
.wo(utra::engine::EV_PENDING, reason); .wo(utra::engine::EV_PENDING, reason);
} }
impl Engine25519 {
pub fn new(handler_conn: xous::CID) -> Engine25519 { impl Engine25519Hw {
pub fn new(handler_conn: xous::CID) -> Engine25519Hw {
log::trace!("creating engine25519 CSR");
let csr = xous::syscall::map_memory( let csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::engine::HW_ENGINE_BASE), xous::MemoryAddress::new(utra::engine::HW_ENGINE_BASE),
None, None,
@ -56,6 +80,7 @@ mod implementation {
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
) )
.expect("couldn't map engine CSR range"); .expect("couldn't map engine CSR range");
log::trace!("creating engine25519 memrange");
let mem = xous::syscall::map_memory( let mem = xous::syscall::map_memory(
xous::MemoryAddress::new(utralib::HW_ENGINE_MEM), xous::MemoryAddress::new(utralib::HW_ENGINE_MEM),
None, None,
@ -64,23 +89,50 @@ mod implementation {
) )
.expect("couldn't map engine memory window range"); .expect("couldn't map engine memory window range");
let mut engine = Engine25519 { /*
let ucode_mem = xous::MemoryRange {
addr: NonZeroUsize::new(mem.addr.get() + UCODE_U8_BASE).unwrap(),
size: NonZeroUsize::new(UCODE_U8_SIZE).unwrap(),
};
let rf_mem = xous::MemoryRange {
addr: NonZeroUsize::new(mem.addr.get() + RF_U8_BASE).unwrap(),
size: NonZeroUsize::new(RF_U8_SIZE).unwrap(),
};
*/
let ucode_backing = xous::syscall::map_memory(
None,
None,
UCODE_U8_SIZE,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't map backing store for microcode");
let rf_backing = xous::syscall::map_memory(
None,
None,
RF_U8_SIZE,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't map RF backing store");
let mut engine = Engine25519Hw {
csr: CSR::new(csr.as_mut_ptr() as *mut u32), csr: CSR::new(csr.as_mut_ptr() as *mut u32),
susres: RegManager::new(csr.as_mut_ptr() as *mut u32), susres: RegManager::new(csr.as_mut_ptr() as *mut u32),
mem, mem,
handler_conn: Some(handler_conn), handler_conn: Some(handler_conn),
sr_backing: ManagedMem::new(mem), ucode_backing, // : [0; UCODE_U32_SIZE], // ManagedMem::new(ucode_mem),
rf_backing, //: [0; RF_U32_SIZE], // ManagedMem::new(rf_mem),
mpc_resume: None, mpc_resume: None,
clean_resume: None, clean_resume: None,
do_notify: false,
illegal_opcode: false,
}; };
log::trace!("claiming interrupt");
xous::claim_interrupt( xous::claim_interrupt(
utra::engine::ENGINE_IRQ, utra::engine::ENGINE_IRQ,
handle_engine_irq, handle_engine_irq,
(&mut engine) as *mut Engine25519 as *mut usize, (&mut engine) as *mut Engine25519Hw as *mut usize,
) )
.expect("couldn't claim Power irq"); .expect("couldn't claim Power irq");
log::trace!("enabling interrupt");
engine.csr.wo(utra::engine::EV_PENDING, 0xFFFF_FFFF); // clear any droppings. engine.csr.wo(utra::engine::EV_PENDING, 0xFFFF_FFFF); // clear any droppings.
engine.csr.wo(utra::engine::EV_ENABLE, engine.csr.wo(utra::engine::EV_ENABLE,
engine.csr.ms(utra::engine::EV_ENABLE_FINISHED, 1) | engine.csr.ms(utra::engine::EV_ENABLE_FINISHED, 1) |
@ -89,6 +141,7 @@ mod implementation {
// setup the susres context. Most of the defaults are fine, so they aren't explicitly initialized in the code above, // 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 // but they still have to show up down here in case we're suspended mid-op
log::trace!("setting up susres");
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::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::WINDOW), None);
engine.susres.push(RegOrField::Reg(utra::engine::MPSTART), None); engine.susres.push(RegOrField::Reg(utra::engine::MPSTART), None);
@ -123,15 +176,82 @@ mod implementation {
} else { } else {
self.mpc_resume = None; self.mpc_resume = None;
} }
// copy the ucode & rf out to the backing memory // accessing ucode & rf requires clocks to be on
self.sr_backing.suspend(); let orig_state = if self.csr.rf(utra::engine::POWER_ON) == 0 {
self.csr.rmwf(utra::engine::POWER_ON, 1);
false
} else {
true
};
// copy the ucode & rf into the backing memory
//self.ucode_backing.suspend();
//self.rf_backing.suspend();
// do it manually, because the automated backing memory mechanism can't allocate a big enough data segment to work for blocks this large
// create a pointer to the entire memory window range
let mem_window = unsafe{*(self.mem.as_mut_ptr() as *const [u32; (utralib::HW_ENGINE_MEM_LEN / 4)])};
// parcel out mutable subslices
// first, split into the ucode/rf areas
let (ucode_superblock, rf_superblock) = mem_window.split_at(RF_U32_BASE);
// further reduce the ucode area to just the unaliased ucode region
let (ucode, _) = ucode_superblock.split_at(UCODE_U32_SIZE);
// further reduce the rf area to just the unaliased rf region
let (rf, _) = rf_superblock.split_at(RF_U32_SIZE);
let mut rf_backing = unsafe{*(self.rf_backing.as_mut_ptr() as *mut [u32; RF_U32_SIZE])};
let mut ucode_backing = unsafe{*(self.ucode_backing.as_mut_ptr() as *mut [u32; UCODE_U32_SIZE])};
for (&src, dst) in rf.iter().zip(rf_backing.iter_mut()) {
*dst = src;
}
for (&src, dst) in ucode.iter().zip(ucode_backing.iter_mut()) {
*dst = src;
}
// restore the power state setting
if !orig_state {
self.csr.rmwf(utra::engine::POWER_ON, 0);
}
// now backup all the machine registers // now backup all the machine registers
self.susres.suspend(); self.susres.suspend();
} }
pub fn resume(&mut self) { pub fn resume(&mut self) {
// "power" should be resumed first by the restore, which would set the pause bit, if it was previously set
self.susres.resume(); self.susres.resume();
self.sr_backing.resume(); // if the power wasn't on, we have to flip it on temporarily to access the backing memories
let orig_state = if self.csr.rf(utra::engine::POWER_ON) == 0 {
self.csr.rmwf(utra::engine::POWER_ON, 1);
false
} else {
true
};
//self.rf_backing.resume();
//self.ucode_backing.resume();
// do it manually, because the automated backing memory mechanism can't allocate a big enough data segment to work for blocks this large
// create a pointer to the entire memory window range
let mut mem_window = unsafe{*(self.mem.as_mut_ptr() as *mut [u32; (utralib::HW_ENGINE_MEM_LEN / 4)])};
// parcel out mutable subslices
// first, split into the ucode/rf areas
let (ucode_superblock, rf_superblock) = mem_window.split_at_mut(RF_U32_BASE);
// further reduce the ucode area to just the unaliased ucode region
let (ucode, _) = ucode_superblock.split_at_mut(UCODE_U32_SIZE);
// further reduce the rf area to just the unaliased rf region
let (rf, _) = rf_superblock.split_at_mut(RF_U32_SIZE);
let rf_backing = unsafe{*(self.rf_backing.as_ptr() as *const [u32; RF_U32_SIZE])};
let ucode_backing = unsafe{*(self.ucode_backing.as_ptr() as *const [u32; UCODE_U32_SIZE])};
for (&src, dst) in rf_backing.iter().zip(rf.iter_mut()) {
*dst = src;
}
for (&src, dst) in ucode_backing.iter().zip(ucode.iter_mut()) {
*dst = src;
}
// restore the power state setting
if !orig_state {
self.csr.rmwf(utra::engine::POWER_ON, 0);
}
// in the case of a resume from pause, we need to specify the PC to resume from // in the case of a resume from pause, we need to specify the PC to resume from
// clear the pause // clear the pause
@ -162,19 +282,17 @@ mod implementation {
self.clean_resume = Some(true); self.clean_resume = Some(true);
} }
} }
const UCODE_U8_BASE: usize = 0x0;
const UCODE_U32_BASE: usize = 0x0;
const UCODE_U32_SIZE: usize = (0x1_0000 / 4);
const RF_U8_BASE: usize = 0x1_0000;
const RF_U32_BASE: usize = (0x1_0000 / 4);
const RF_U32_SIZE: usize = (0x4000 / 4);
pub fn run(&mut self, job: Job) { pub fn run(&mut self, job: Job) {
// block any suspends from happening while we set up the engine
DISALLOW_SUSPEND.store(true, Ordering::Relaxed);
self.csr.rmwf(utra::engine::POWER_ON, 1);
// create a pointer to the entire memory window range // create a pointer to the entire memory window range
let mem_window: &mut [u32; (utralib::HW_ENGINE_MEM_LEN / 4)] = self.mem.as_mut_ptr() as *mut [u32; (utralib::HW_ENGINE_MEM_LEN / 4)]; let mut mem_window = unsafe{*(self.mem.as_mut_ptr() as *mut [u32; (utralib::HW_ENGINE_MEM_LEN / 4)])};
// parcel out mutable subslices // parcel out mutable subslices
// first, split into the ucode/rf areas // first, split into the ucode/rf areas
let (ucode_superblock, rf_superblock) = (&mem_window).split_at_mut(RF_U32_BASE); let (ucode_superblock, rf_superblock) = mem_window.split_at_mut(RF_U32_BASE);
// further reduce the ucode area to just the unaliased ucode region // further reduce the ucode area to just the unaliased ucode region
let (ucode, _) = ucode_superblock.split_at_mut(UCODE_U32_SIZE); let (ucode, _) = ucode_superblock.split_at_mut(UCODE_U32_SIZE);
// further reduce the rf area to just the unaliased rf region // further reduce the rf area to just the unaliased rf region
@ -198,24 +316,42 @@ mod implementation {
self.csr.wfo(utra::engine::MPSTART_MPSTART, job.uc_start); self.csr.wfo(utra::engine::MPSTART_MPSTART, job.uc_start);
self.csr.wfo(utra::engine::MPLEN_MPLEN, job.uc_len); self.csr.wfo(utra::engine::MPLEN_MPLEN, job.uc_len);
// determine if this a sync or async call
if job.id.is_some() {
// async calls need a notification message
self.do_notify = true;
} else {
// sync calls poll a state variable, and thus no message is sent
self.do_notify = false;
}
// setup the sync polling variable
RUN_IN_PROGRESS.store(true, Ordering::Relaxed);
// this will start the run. interrupts should *already* be enabled for the completion notification... // this will start the run. interrupts should *already* be enabled for the completion notification...
self.csr.wfo(utra::engine::CONTROL_GO, 1); self.csr.wfo(utra::engine::CONTROL_GO, 1);
// we are now in a stable config, suspends are allowed
DISALLOW_SUSPEND.store(false, Ordering::Relaxed);
} }
pub fn get_result(&mut self) -> JobResult { pub fn get_result(&mut self) -> JobResult {
if let Some(clean_resume) = self.clean_resume { if let Some(clean_resume) = self.clean_resume {
if !clean_resume { if !clean_resume {
self.csr.rmwf(utra::engine::POWER_ON, 0);
return JobResult::SuspendError; return JobResult::SuspendError;
} }
} }
if self.illegal_opcode {
self.csr.rmwf(utra::engine::POWER_ON, 0);
return JobResult::IllegalOpcodeException;
}
// create a pointer to the entire memory window range // create a pointer to the entire memory window range
let mem_window: &mut [u32; (utralib::HW_ENGINE_MEM_LEN / 4)] = self.mem.as_mut_ptr() as *mut [u32; (utralib::HW_ENGINE_MEM_LEN / 4)]; let mem_window = unsafe{*(self.mem.as_ptr() as *const [u32; (utralib::HW_ENGINE_MEM_LEN / 4)])};
// parcel out mutable subslices // parcel out mutable subslices
// first, split into the ucode/rf areas // first, split into the ucode/rf areas
let (_ucode_superblock, rf_superblock) = (&mem_window).split_at_mut(RF_U32_BASE); let (_ucode_superblock, rf_superblock) = (&mem_window).split_at(RF_U32_BASE);
// further reduce the ucode area to just the unaliased ucode region // further reduce the ucode area to just the unaliased ucode region
let (rf, _) = rf_superblock.split_at_mut(RF_U32_SIZE); let (rf, _) = rf_superblock.split_at(RF_U32_SIZE);
let mut ret_rf: [u32; RF_SIZE_IN_U32] = [0; RF_SIZE_IN_U32]; let mut ret_rf: [u32; RF_SIZE_IN_U32] = [0; RF_SIZE_IN_U32];
let window = self.csr.rf(utra::engine::WINDOW_WINDOW) as usize; let window = self.csr.rf(utra::engine::WINDOW_WINDOW) as usize;
@ -223,6 +359,7 @@ mod implementation {
*dst = src; *dst = src;
} }
self.csr.rmwf(utra::engine::POWER_ON, 0);
JobResult::Result(ret_rf) JobResult::Result(ret_rf)
} }
} }
@ -233,12 +370,12 @@ mod implementation {
mod implementation { mod implementation {
use log::info; use log::info;
pub struct Engine25519 { pub struct Engine25519Hw {
} }
impl Engine25519 { impl Engine25519Hw {
pub fn new() -> Engine25519 { pub fn new() -> Engine25519Hw {
Engine25519 { Engine25519Hw {
} }
} }
pub fn suspend(&self) { pub fn suspend(&self) {
@ -253,51 +390,120 @@ mod implementation {
} }
} }
fn susres_thread(engine_arg: usize) {
use crate::implementation::Engine25519Hw;
let engine25519 = unsafe { &mut *(engine_arg as *mut Engine25519Hw) };
let susres_sid = xous::create_server().unwrap();
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, _, _, _, {
// prevent new jobs from starting while we're in suspend
// we do this first, because the race condition we're trying to catch is between a job
// being set up, and it running.
SUSPEND_IN_PROGRESS.store(true, Ordering::Relaxed);
// this check will catch the case that a job happened to be started before we could set
// our flag above.
while DISALLOW_SUSPEND.load(Ordering::Relaxed) {
// don't start a suspend if we're in the middle of a critical region
xous::yield_slice();
}
// at this point:
// - there should be no new jobs in progress
// - any job that was being set up, will have been set up so its safe to interrupt execution
engine25519.suspend();
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
engine25519.resume();
SUSPEND_IN_PROGRESS.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] #[xous::xous_main]
fn xmain() -> ! { fn xmain() -> ! {
use crate::implementation::Engine25519; use crate::implementation::Engine25519Hw;
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Trace); log::set_max_level(log::LevelFilter::Trace);
log::info!("my PID is {}", xous::process::id()); log::info!("my PID is {}", xous::process::id());
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
ticktimer.sleep_ms(500).unwrap();
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
let engine25519_sid = xns.register_name(api::SERVER_NAME_ENGINE25519, None).expect("can't register server"); let engine25519_sid = xns.register_name(api::SERVER_NAME_ENGINE25519, None).expect("can't register server");
log::trace!("registered with NS -- {:?}", engine25519_sid); log::trace!("registered with NS -- {:?}", engine25519_sid);
let handler_conn = xous::connect(engine25519_sid).expect("couldn't create IRQ handler connection"); let handler_conn = xous::connect(engine25519_sid).expect("couldn't create IRQ handler connection");
let mut engine25519 = Engine25519::new(handler_conn); log::trace!("creating engine25519 object");
let mut engine25519 = Engine25519Hw::new(handler_conn);
log::trace!("ready to accept requests"); log::trace!("ready to accept requests");
// register a suspend/resume listener // register a suspend/resume listener
let sr_cid = xous::connect(engine25519_sid).expect("couldn't create suspend callback connection"); xous::create_thread_1(susres_thread, (&mut engine25519) as *mut Engine25519Hw as usize).expect("couldn't start susres handler thread");
let mut susres = susres::Susres::new(&xns, Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
let mut client_cid: Option<xous::CID> = None; let mut client_cid: Option<xous::CID> = None;
loop { loop {
let msg = xous::receive_message(engine25519_sid).unwrap(); let mut msg = xous::receive_message(engine25519_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) { match FromPrimitive::from_usize(msg.body.id()) {
Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
engine25519.suspend();
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
engine25519.resume();
}),
Some(Opcode::RunJob) => { Some(Opcode::RunJob) => {
// don't start a new job if a suspend is in progress
while SUSPEND_IN_PROGRESS.load(Ordering::Relaxed) {
xous::yield_slice();
}
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let job = buffer.to_original::<Job, _>().unwrap(); let job = buffer.to_original::<Job, _>().unwrap();
let response = if client_id.is_none() { let response = if client_cid.is_none() {
client_cid = Some(xous::connect(job.id).expect("couldn't connect to the caller's server")); if let Some(job_id) = job.id {
engine25519.run(job); // async job
JobResult::Started // the presence of an ID indicates we are doing an async method
client_cid = Some(xous::connect(xous::SID::from_array(job_id)).expect("couldn't connect to the caller's server"));
engine25519.run(job);
// just let the caller know we started a job, but don't return any results
JobResult::Started
} else {
// sync job
// start the job, which should set RUN_IN_PROGRESS to true
engine25519.run(job);
while RUN_IN_PROGRESS.load(Ordering::Relaxed) {
// block until the job is done
xous::yield_slice();
}
engine25519.get_result() // return the result
}
} else { } else {
JobResult::EngineUnavailable JobResult::EngineUnavailable
}; };
buffer.replace(response).unwrap(); buffer.replace(response).unwrap();
}, },
Some(Opcode::IsFree) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { Some(Opcode::IsFree) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
if client_id.is_none() { if client_cid.is_none() {
xous::return_scalar(msg.sender, 1).expect("couldn't return IsIdle query"); xous::return_scalar(msg.sender, 1).expect("couldn't return IsIdle query");
} else { } else {
xous::return_scalar(msg.sender, 0).expect("couldn't return IsIdle query"); xous::return_scalar(msg.sender, 0).expect("couldn't return IsIdle query");
@ -306,18 +512,22 @@ fn xmain() -> ! {
Some(Opcode::EngineDone) => { Some(Opcode::EngineDone) => {
if let Some(cid) = client_cid { if let Some(cid) = client_cid {
let result = engine25519.get_result(); let result = engine25519.get_result();
let buf = Buffer::into_buf(result).or(Err(xous::Error::InternalError))?; let buf = Buffer::into_buf(result).or(Err(xous::Error::InternalError)).unwrap();
buf.send(cid, Return::Result.to_u32().unwrap()).expect("couldn't return result to caller"); buf.send(cid, Return::Result.to_u32().unwrap()).expect("couldn't return result to caller");
// this simultaneously releases the lock and disconnects from the caller // this simultaneously releases the lock and disconnects from the caller
unsafe{xous::disconnect(client_cid.take()).expect("couldn't disconnect from the caller");} unsafe{xous::disconnect(client_cid.take().unwrap()).expect("couldn't disconnect from the caller");}
} else { } else {
log::error!("illegal state: got a result, but no client was registered??"); log::error!("illegal state: got a result, but no client was registered. Did we forget to disable interrupts on a synchronous call??");
} }
}, },
Some(Opcode::IllegalOpcode) => { Some(Opcode::IllegalOpcode) => {
let buf = Buffer::into_buf(JobResult::IllegalOpcodeException).or(Err(xous::Error::InternalError))?; if let Some(cid) = client_cid {
buf.send(cid, Return::Result.to_u32().unwrap()).expect("couldn't return result to caller"); let buf = Buffer::into_buf(JobResult::IllegalOpcodeException).or(Err(xous::Error::InternalError)).unwrap();
buf.send(cid, Return::Result.to_u32().unwrap()).expect("couldn't return result to caller");
} else {
log::error!("illegal state: got a result, but no client was registered. Did we forget to disable interrupts on a synchronous call??");
}
} }
Some(Opcode::Quit) => { Some(Opcode::Quit) => {
log::info!("Received quit opcode, exiting!"); log::info!("Received quit opcode, exiting!");

View file

@ -27,6 +27,7 @@ engine-sha512 = {path="../engine-sha512"}
digest = "0.9" digest = "0.9"
aes-xous={path="../aes"} aes-xous={path="../aes"}
cipher = "0.3" cipher = "0.3"
engine-25519 = {path="../engine-25519"}
heapless = "0.7" heapless = "0.7"
trng = {path = "../trng"} trng = {path = "../trng"}

View file

@ -93,6 +93,7 @@ mod sha; use sha::*;
mod ecup; use ecup::*; mod ecup; use ecup::*;
mod aes; use aes::*; mod aes; use aes::*;
mod trng_cmd; use trng_cmd::*; mod trng_cmd; use trng_cmd::*;
mod engine; use engine::*;
mod fcc; use fcc::*; mod fcc; use fcc::*;
mod pds; // dependency of the FCC file mod pds; // dependency of the FCC file
@ -114,6 +115,7 @@ pub struct CmdEnv {
ecup_cmd: EcUpdate, ecup_cmd: EcUpdate,
aes_cmd: Aes, aes_cmd: Aes,
trng_cmd: TrngCmd, trng_cmd: TrngCmd,
engine_cmd: Engine,
fcc_cmd: Fcc, fcc_cmd: Fcc,
} }
@ -133,6 +135,7 @@ impl CmdEnv {
let sha = Sha::new(&xns, &mut common); let sha = Sha::new(&xns, &mut common);
let aes = Aes::new(&xns, &mut common); let aes = Aes::new(&xns, &mut common);
let ecup = EcUpdate::new(&mut common); let ecup = EcUpdate::new(&mut common);
let engine = Engine::new(&xns, &mut common);
// print our version info // print our version info
let (maj, min, rev, extra, gitrev) = common.llio.soc_gitrev().unwrap(); let (maj, min, rev, extra, gitrev) = common.llio.soc_gitrev().unwrap();
@ -160,6 +163,7 @@ impl CmdEnv {
ecup_cmd: ecup, ecup_cmd: ecup,
aes_cmd: aes, aes_cmd: aes,
trng_cmd: TrngCmd::new(), trng_cmd: TrngCmd::new(),
engine_cmd: engine,
fcc_cmd: fcc, fcc_cmd: fcc,
} }
@ -190,6 +194,7 @@ impl CmdEnv {
&mut self.ecup_cmd, &mut self.ecup_cmd,
&mut self.aes_cmd, &mut self.aes_cmd,
&mut self.trng_cmd, &mut self.trng_cmd,
&mut self.engine_cmd,
&mut self.fcc_cmd, &mut self.fcc_cmd,
]; ];

View file

@ -0,0 +1,229 @@
use crate::{ShellCmdApi, CommonEnv};
use xous_ipc::String;
use engine_25519::*;
use num_traits::*;
use core::sync::atomic::{AtomicU32, Ordering};
static CB_ID: AtomicU32 = AtomicU32::new(0);
#[export_name = "engine_vectors"]
pub static ENGINE_VECTORS: &[u8; 15652] = include_bytes!("engine25519_vectors.bin");
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum BenchOp {
StartEngine,
Quit,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum BenchResult {
EngineDone,
}
const TEST_ITERS: usize = 10;
fn vector_read(word_offset: usize) -> u32 {
let mut bytes: [u8; 4] = [0; 4];
for i in 0..4 {
bytes[i] = ENGINE_VECTORS[word_offset*4 + i];
}
u32::from_le_bytes(bytes)
}
fn run_vectors(engine: &mut Engine25519) -> (usize, usize) {
let mut test_offset: usize = 0x0;
let mut passes: usize = 0;
let mut fails: usize = 0;
loop {
let magic_number = vector_read(test_offset);
if magic_number != 0x5645_4354 {
break;
}
test_offset += 1;
let load_addr = (vector_read(test_offset) >> 16) & 0xFFFF;
let code_len = vector_read(test_offset) & 0xFFFF;
test_offset += 1;
let num_args = (vector_read(test_offset) >> 27) & 0x1F;
let window = (vector_read(test_offset) >> 23) & 0xF;
let num_vectors = (vector_read(test_offset) >> 0) & 0x3F_FFFF;
test_offset += 1;
let mut job = Job {
id: None,
uc_start: load_addr,
uc_len: code_len,
ucode: [0; 1024],
rf: [0; RF_SIZE_IN_U32],
window: Some(window as u8),
};
for i in load_addr as usize..(load_addr + code_len) as usize {
job.ucode[i] = vector_read(test_offset);
test_offset += 1;
}
test_offset = test_offset + (8 - (test_offset % 8)); // skip over padding
// copy in the arguments
for _vector in 0..num_vectors {
for argcnt in 0..num_args {
for word in 0..8 {
job.rf[(window * 32 * 8 + argcnt * 8 + word) as usize] = vector_read(test_offset);
test_offset += 1;
}
}
let mut passed = true;
match engine.spawn_job(job) {
Ok(rf_result) => {
for word in 0..8 {
let expect = vector_read(test_offset);
test_offset += 1;
let actual = rf_result[(window * 32 * 8 + 31 * 8 + word) as usize];
if expect != actual {
passed = false;
}
}
},
_ => {
log::error!("system error in running test vector: {}", test_offset);
}
}
if passed {
passes += 1;
} else {
fails += 1;
log::error!("arithmetic error in running test vector: {}", test_offset);
}
}
}
(passes, fails)
}
pub fn benchmark_thread(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);
let xns = xous_names::XousNames::new().unwrap();
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
let mut engine = engine_25519::Engine25519::new();
loop {
let msg = xous::receive_message(sid).unwrap();
log::debug!("benchmark got msg {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) {
Some(BenchOp::StartEngine) => {
let mut passes = 0;
let mut fails = 0;
for _ in 0..TEST_ITERS {
let (p, f) = run_vectors(&mut engine);
passes += p;
fails += f;
}
xous::send_message(callback_conn,
xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize,
passes as usize,
fails as usize,
0, 0)
).unwrap();
},
Some(BenchOp::Quit) => {
log::info!("quitting benchmark thread");
break;
},
None => {
log::error!("received unknown opcode");
}
}
}
xous::destroy_server(sid).unwrap();
}
#[derive(Debug)]
pub struct Engine {
susres: susres::Susres,
benchmark_cid: xous::CID,
start_time: Option<u64>,
}
impl Engine {
pub fn new(xns: &xous_names::XousNames, env: &mut CommonEnv) -> Self {
let sid = xous::create_server().unwrap();
let sid_tuple = sid.to_u32();
let cb_id = env.register_handler(String::<256>::from_str("engine"));
CB_ID.store(cb_id, Ordering::Relaxed);
xous::create_thread_4(benchmark_thread, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
Engine {
susres: susres::Susres::new_without_hook(&xns).unwrap(),
benchmark_cid: xous::connect(sid).unwrap(),
start_time: None,
}
}
}
impl<'a> ShellCmdApi<'a> for Engine {
cmd_api!(engine); // inserts boilerplate for command API
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
use core::fmt::Write;
let mut ret = String::<1024>::new();
let helpstring = "engine [check] [bench] [susres]";
let mut tokens = args.as_str().unwrap().split(' ');
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"check" => {
let mut engine = engine_25519::Engine25519::new();
let (passes, fails) = run_vectors(&mut engine);
write!(ret, "Engine passed {} vectors, failed {} vectors", passes, fails).unwrap();
}
"bench" => {
let start = env.ticktimer.elapsed_ms();
self.start_time = Some(start);
xous::send_message(self.benchmark_cid,
xous::Message::new_scalar(BenchOp::StartEngine.to_usize().unwrap(), 0, 0, 0, 0)
).unwrap();
write!(ret, "Starting Engine hardware benchmark with {} iters", TEST_ITERS).unwrap();
}
"susres" => {
let start = env.ticktimer.elapsed_ms();
self.start_time = Some(start);
xous::send_message(self.benchmark_cid,
xous::Message::new_scalar(BenchOp::StartEngine.to_usize().unwrap(), 0, 0, 0, 0)
).unwrap();
let wait_time = (env.trng.get_u32().unwrap() % 2000) + 500; // at least half a second wait, up to 2 seconds
env.ticktimer.sleep_ms(wait_time as _).unwrap();
self.susres.initiate_suspend().unwrap();
write!(ret, "Interrupted Engine hardware benchmark with a suspend/resume").unwrap();
}
_ => {
write!(ret, "{}", helpstring).unwrap();
}
}
} else {
write!(ret, "{}", helpstring).unwrap();
}
Ok(Some(ret))
}
fn callback(&mut self, msg: &xous::MessageEnvelope, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
use core::fmt::Write;
log::debug!("benchmark callback");
let mut ret = String::<1024>::new();
xous::msg_scalar_unpack!(msg, passes, fails, _, _, {
let end = env.ticktimer.elapsed_ms();
let elapsed: f64 = ((end - self.start_time.unwrap()) as f64) / TEST_ITERS as f64;
write!(ret, "{}ms/iter; In total, Engine passed {} vectors, failed {} vectors", elapsed, passes, fails).unwrap();
});
Ok(Some(ret))
}
}

Binary file not shown.