beginnings of the curve25519 accelerator engine core driver

the engine-25519 server just takes microcode jobs and runs them
on the curve25519 accelerator engine.

all the fancy crypto stuff comes from other crates, which will
be filled in later.
This commit is contained in:
bunnie 2021-06-22 01:48:51 +08:00
parent e8dc10977a
commit 5d21ef0c76
10 changed files with 313 additions and 60 deletions

17
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-06-21 19:10:58]]></description>
<description><![CDATA[Litex SoC 2021-06-22 01:01:29]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -3451,7 +3451,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x33e603a3</resetValue>
<resetValue>0x4d93c1cf</resetValue>
<size>32</size>
<fields>
<field>
@ -3466,7 +3466,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0xf1d9b09</resetValue>
<resetValue>0x1d257e0e</resetValue>
<size>32</size>
<fields>
<field>
@ -6406,10 +6406,25 @@ microcode instructions starting at mpstart]]></description>
</fields>
</register>
<register>
<name>POWER</name>
<name>MPRESUME</name>
<addressOffset>0x0010</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>mpresume</name>
<msb>9</msb>
<bitRange>[9:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Where to resume execution after a pause]]></description>
</field>
</fields>
</register>
<register>
<name>POWER</name>
<addressOffset>0x0014</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>on</name>
@ -6433,7 +6448,7 @@ pause has happened. Used to interrupt flow for suspend/resume.]]></description>
</register>
<register>
<name>STATUS</name>
<addressOffset>0x0014</addressOffset>
<addressOffset>0x0018</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -6465,7 +6480,7 @@ be read out for suspend/resume]]></description>
<register>
<name>EV_STATUS</name>
<description><![CDATA[Illegal opcode encountered]]></description>
<addressOffset>0x0018</addressOffset>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -6488,7 +6503,7 @@ be read out for suspend/resume]]></description>
<register>
<name>EV_PENDING</name>
<description><![CDATA[Illegal opcode encountered]]></description>
<addressOffset>0x001c</addressOffset>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -6511,7 +6526,7 @@ be read out for suspend/resume]]></description>
<register>
<name>EV_ENABLE</name>
<description><![CDATA[Illegal opcode encountered]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -6535,7 +6550,7 @@ be read out for suspend/resume]]></description>
<name>INSTRUCTION</name>
<description><![CDATA[Current instruction being executed by the engine. The format of this register
exactly reflects the binary layout of an Engine instruction.]]></description>
<addressOffset>0x0024</addressOffset>
<addressOffset>0x0028</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -6593,7 +6608,7 @@ exactly reflects the binary layout of an Engine instruction.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x28</size>
<size>0x2c</size>
<usage>registers</usage>
</addressBlock>
<interrupt>

Binary file not shown.

View file

@ -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"}

View file

@ -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<u8>,
}
#[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,
}

View file

@ -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();}
}
}
}
}
*/

View file

@ -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<u32>,
fifo: xous::MemoryRange,
susres_manager: RegManager::<{utra::audio::AUDIO_NUMREGS}>,
mem: xous::MemoryRange,
susres: RegManager::<{utra::engine::ENGINE_NUMREGS}>,
handler_conn: Option<xous::CID>,
sr_backing: ManagedMem::<{utralib::generated::HW_ENGINE_MEM_LEN}>,
mpc_resume: Option<u32>,
clean_resume: Option<bool>,
}
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)
}

View file

@ -114,7 +114,7 @@ impl<const N: usize> RegManager::<N> 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<usize>) {
let mrt = ManagedReg {
item: mr,

View file

@ -53,6 +53,7 @@ fn try_main() -> Result<(), DynError> {
"susres",
"codec",
"engine-sha512",
"engine-25519",
];
let benchmark_pkgs = [
"benchmark",