Skip to content

Commit

Permalink
Implement set_apb_pauser method for ModelEmulated.
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty1968 committed Oct 21, 2024
1 parent a9ad8a6 commit 51171bd
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 23 deletions.
4 changes: 4 additions & 0 deletions hw-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use caliptra_hw_model_types::{
};
use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unalign};

use caliptra_emu_periph::MailboxRequester;
use caliptra_registers::mbox;
use caliptra_registers::mbox::enums::{MboxFsmE, MboxStatusE};
use caliptra_registers::soc_ifc::regs::{
Expand Down Expand Up @@ -176,6 +177,8 @@ pub struct InitParams<'a> {
// A trace path to use. If None, the CPTRA_TRACE_PATH environment variable
// will be used
pub trace_path: Option<PathBuf>,

pub soc_user: MailboxRequester,
}
impl<'a> Default for InitParams<'a> {
fn default() -> Self {
Expand Down Expand Up @@ -210,6 +213,7 @@ impl<'a> Default for InitParams<'a> {
}),
random_sram_puf: true,
trace_path: None,
soc_user: MailboxRequester::SocUser1,
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions hw-model/src/model_emulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use caliptra_emu_bus::Clock;
use caliptra_emu_cpu::CoverageBitmaps;
use caliptra_emu_cpu::{Cpu, InstrTracer};
use caliptra_emu_periph::ActionCb;
use caliptra_emu_periph::MailboxExternal;
use caliptra_emu_periph::ReadyForFwCb;
use caliptra_emu_periph::{CaliptraRootBus, CaliptraRootBusArgs, SocToCaliptraBus, TbServicesCb};
use caliptra_emu_periph::{
CaliptraRootBus, CaliptraRootBusArgs, MailboxRequester, SocToCaliptraBus, TbServicesCb,
};
use caliptra_emu_types::{RvAddr, RvData, RvSize};
use caliptra_hw_model_types::ErrorInjectionMode;
use caliptra_image_types::IMAGE_MANIFEST_BYTE_SIZE;
Expand Down Expand Up @@ -186,7 +189,7 @@ impl HwModel for ModelEmulated {
};
dccm_dest.copy_from_slice(params.dccm);
}
let soc_to_caliptra_bus = root_bus.soc_to_caliptra_bus();
let soc_to_caliptra_bus = root_bus.soc_to_caliptra_bus(params.soc_user);
let cpu = Cpu::new(BusLogger::new(root_bus), clock);

let mut hasher = DefaultHasher::new();
Expand Down Expand Up @@ -282,8 +285,11 @@ impl HwModel for ModelEmulated {
}
}

fn set_apb_pauser(&mut self, _pauser: u32) {
unimplemented!();
fn set_apb_pauser(&mut self, pauser: u32) {
self.soc_to_caliptra_bus.mailbox = MailboxExternal {
soc_user: MailboxRequester::try_from(pauser).unwrap(),
regs: self.soc_to_caliptra_bus.mailbox.regs.clone(),
};
}

fn warm_reset(&mut self) {
Expand Down
1 change: 1 addition & 0 deletions runtime/src/invoke_dpe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl InvokeDpeCmd {
};

let locality = drivers.mbox.user();
caliptra_common::cprintln!("[rt] locality = {}", locality);
let command = Command::deserialize(&cmd.data[..cmd.data_size as usize])
.map_err(|_| CaliptraError::RUNTIME_DPE_COMMAND_DESERIALIZATION_FAILED)?;
let flags = pdata.manifest1.header.flags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,50 @@ fn test_pl1_init_ctx_dpe_context_thresholds() {
}
}

#[test]
fn test_change_locality() {
let mut model = run_rt_test(None, None, None);

model.step_until(|m| {
m.soc_ifc().cptra_boot_status().read() == u32::from(RtBootStatus::RtReadyForCommands)
});
model.set_apb_pauser(0x01);

let derive_context_cmd = DeriveContextCmd {
handle: ContextHandle::default(),
data: DATA,
flags: DeriveContextFlags::CHANGE_LOCALITY
| DeriveContextFlags::MAKE_DEFAULT
| DeriveContextFlags::INPUT_ALLOW_X509,
tci_type: 0,
target_locality: 2,
};

let _ = execute_dpe_cmd(
&mut model,
&mut Command::DeriveContext(derive_context_cmd),
DpeResult::Success,
)
.unwrap();

model.set_apb_pauser(0x02);

let derive_context_cmd = DeriveContextCmd {
handle: ContextHandle::default(),
data: DATA,
flags: DeriveContextFlags::MAKE_DEFAULT,
tci_type: 0,
target_locality: 2,
};

let _ = execute_dpe_cmd(
&mut model,
&mut Command::DeriveContext(derive_context_cmd),
DpeResult::Success,
)
.unwrap();
}

#[test]
fn test_populate_idev_cannot_be_called_from_pl1() {
let mut image_opts = ImageOptions::default();
Expand Down
10 changes: 5 additions & 5 deletions sw-emulator/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use caliptra_emu_bus::Clock;
use caliptra_emu_cpu::{Cpu, RvInstr, StepAction};
use caliptra_emu_periph::soc_reg::DebugManufService;
use caliptra_emu_periph::{
CaliptraRootBus, CaliptraRootBusArgs, DownloadIdevidCsrCb, MailboxInternal, ReadyForFwCb,
TbServicesCb, UploadUpdateFwCb,
CaliptraRootBus, CaliptraRootBusArgs, DownloadIdevidCsrCb, MailboxInternal, MailboxRequester,
ReadyForFwCb, TbServicesCb, UploadUpdateFwCb,
};
use caliptra_hw_model::BusMmio;
use clap::{arg, value_parser, ArgAction};
Expand Down Expand Up @@ -283,7 +283,7 @@ fn main() -> io::Result<()> {
let soc_ifc = unsafe {
caliptra_registers::soc_ifc::RegisterBlock::new_with_mmio(
0x3003_0000 as *mut u32,
BusMmio::new(root_bus.soc_to_caliptra_bus()),
BusMmio::new(root_bus.soc_to_caliptra_bus(MailboxRequester::SocUser1)),
)
};

Expand Down Expand Up @@ -405,7 +405,7 @@ fn change_dword_endianess(data: &mut Vec<u8>) {
}

fn upload_fw_to_mailbox(mailbox: &mut MailboxInternal, firmware_buffer: Rc<Vec<u8>>) {
let soc_mbox = mailbox.as_external().regs();
let soc_mbox = mailbox.as_external(MailboxRequester::SocUser1).regs();
// Write the cmd to mailbox.

assert!(!soc_mbox.lock().read().lock());
Expand Down Expand Up @@ -449,7 +449,7 @@ fn download_idev_id_csr(

let mut file = std::fs::File::create(path).unwrap();

let soc_mbox = mailbox.as_external().regs();
let soc_mbox = mailbox.as_external(MailboxRequester::SocUser1).regs();

let byte_count = soc_mbox.dlen().read() as usize;
let remainder = byte_count % core::mem::size_of::<u32>();
Expand Down
2 changes: 1 addition & 1 deletion sw-emulator/lib/periph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use hmac_sha384::HmacSha384;
pub use iccm::Iccm;
pub use key_vault::KeyUsage;
pub use key_vault::KeyVault;
pub use mailbox::{MailboxExternal, MailboxInternal, MailboxRam};
pub use mailbox::{MailboxExternal, MailboxInternal, MailboxRam, MailboxRequester};
pub use root_bus::{
ActionCb, CaliptraRootBus, CaliptraRootBusArgs, DownloadIdevidCsrCb, ReadyForFwCb,
SocToCaliptraBus, TbServicesCb, UploadUpdateFwCb,
Expand Down
41 changes: 32 additions & 9 deletions sw-emulator/lib/periph/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ impl Default for MailboxRam {

#[derive(Clone)]
pub struct MailboxExternal {
regs: Rc<RefCell<MailboxRegs>>,
pub soc_user: MailboxRequester,
pub regs: Rc<RefCell<MailboxRegs>>,
}
impl MailboxExternal {
pub fn regs(&mut self) -> caliptra_registers::mbox::RegisterBlock<BusMmio<Self>> {
Expand All @@ -107,7 +108,7 @@ impl Bus for MailboxExternal {
/// Read data of specified size from given address
fn read(&mut self, size: RvSize, addr: RvAddr) -> Result<RvData, BusError> {
let mut regs = self.regs.borrow_mut();
regs.set_request(MailboxRequester::Soc);
regs.set_request(self.soc_user);
let result = regs.read(size, addr);
regs.set_request(MailboxRequester::Caliptra);
result
Expand All @@ -116,7 +117,7 @@ impl Bus for MailboxExternal {
/// Write data of specified size to given address
fn write(&mut self, size: RvSize, addr: RvAddr, val: RvData) -> Result<(), BusError> {
let mut regs = self.regs.borrow_mut();
regs.set_request(MailboxRequester::Soc);
regs.set_request(self.soc_user);
let result = regs.write(size, addr, val);
regs.set_request(MailboxRequester::Caliptra);
result
Expand Down Expand Up @@ -146,8 +147,9 @@ impl MailboxInternal {
}
}

pub fn as_external(&self) -> MailboxExternal {
pub fn as_external(&self, soc_user: MailboxRequester) -> MailboxExternal {
MailboxExternal {
soc_user,
regs: self.regs.clone(),
}
}
Expand Down Expand Up @@ -183,14 +185,35 @@ impl Bus for MailboxInternal {

pub enum MailboxRequester {
Caliptra = 0,
Soc = 1,
SocUser1 = 1,
SocUser2 = 2,
SocUser3 = 3,
SocUser4 = 4,
}

impl TryFrom<u32> for MailboxRequester {
type Error = &'static str;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(MailboxRequester::Caliptra),
1 => Ok(MailboxRequester::SocUser1),
2 => Ok(MailboxRequester::SocUser2),
3 => Ok(MailboxRequester::SocUser3),
4 => Ok(MailboxRequester::SocUser4),
_ => Err("Invalid value for MailboxRequester"),
}
}
}

impl From<MailboxRequester> for u32 {
fn from(val: MailboxRequester) -> Self {
match val {
MailboxRequester::Caliptra => 0,
MailboxRequester::Soc => 1,
MailboxRequester::SocUser1 => 1,
MailboxRequester::SocUser2 => 2,
MailboxRequester::SocUser3 => 3,
MailboxRequester::SocUser4 => 4,
}
}
}
Expand Down Expand Up @@ -706,7 +729,7 @@ mod tests {
#[test]
fn test_soc_to_caliptra_lock() {
let mut caliptra = MailboxInternal::new(&Clock::new(), MailboxRam::new());
let mut soc = caliptra.as_external();
let mut soc = caliptra.as_external(MailboxRequester::SocUser1);
let soc_regs = soc.regs();

assert!(!soc_regs.lock().read().lock());
Expand All @@ -723,15 +746,15 @@ mod tests {
let request_to_send: [u32; 4] = [0x1111_1111, 0x2222_2222, 0x3333_3333, 0x4444_4444];

let mut caliptra = MailboxInternal::new(&Clock::new(), MailboxRam::new());
let mut soc = caliptra.as_external();
let mut soc = caliptra.as_external(MailboxRequester::SocUser1);
let soc_regs = soc.regs();
let uc_regs = caliptra.regs();

assert!(!soc_regs.lock().read().lock());
// Confirm it is locked
assert!(soc_regs.lock().read().lock());

assert_eq!(soc_regs.user().read(), MailboxRequester::Soc as u32);
assert_eq!(soc_regs.user().read(), MailboxRequester::SocUser1 as u32);

// Write command
soc_regs.cmd().write(|_| 0x55);
Expand Down
7 changes: 4 additions & 3 deletions sw-emulator/lib/periph/src/root_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use caliptra_emu_derive::Bus;
use caliptra_hw_model_types::{EtrngResponse, RandomEtrngResponses, RandomNibbles};
use std::path::PathBuf;
use tock_registers::registers::InMemoryRegister;
use crate::MailboxRequester;

/// Default Deobfuscation engine key
pub const DEFAULT_DOE_KEY: [u8; 32] = [
Expand Down Expand Up @@ -345,9 +346,9 @@ impl CaliptraRootBus {
}
}

pub fn soc_to_caliptra_bus(&self) -> SocToCaliptraBus {
pub fn soc_to_caliptra_bus(&self, soc_user: MailboxRequester) -> SocToCaliptraBus {
SocToCaliptraBus {
mailbox: self.mailbox.as_external(),
mailbox: self.mailbox.as_external(soc_user),
sha512_acc: self.sha512_acc.clone(),
soc_ifc: self.soc_reg.external_regs(),
}
Expand All @@ -357,7 +358,7 @@ impl CaliptraRootBus {
#[derive(Bus)]
pub struct SocToCaliptraBus {
#[peripheral(offset = 0x3002_0000, mask = 0x0000_0fff)]
mailbox: MailboxExternal,
pub mailbox: MailboxExternal,

#[peripheral(offset = 0x3002_1000, mask = 0x0000_0fff)]
sha512_acc: Sha512Accelerator,
Expand Down
3 changes: 2 additions & 1 deletion sw-emulator/lib/periph/src/soc_reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Abstract:
--*/

use crate::helpers::{bytes_from_words_be, words_from_bytes_be};
use crate::mailbox::MailboxRequester;
use crate::root_bus::ReadyForFwCbArgs;
use crate::{CaliptraRootBusArgs, Iccm, MailboxInternal};
use caliptra_emu_bus::BusError::{LoadAccessFault, StoreAccessFault};
Expand Down Expand Up @@ -1186,7 +1187,7 @@ impl SocRegistersImpl {
}

if self.timer.fired(&mut self.op_fw_read_complete_action) {
let soc_mbox = self.mailbox.as_external().regs();
let soc_mbox = self.mailbox.as_external(MailboxRequester::SocUser1).regs();
// uC will set status to CMD_COMPLETE after reading the
// mailbox data; we can't clear the execute bit until that is done.`
if !soc_mbox.status().read().status().cmd_busy() {
Expand Down

0 comments on commit 51171bd

Please sign in to comment.