Skip to content

Commit

Permalink
miri: Add RMI_REC_CREATE, RMI_REC_DESTROY TC
Browse files Browse the repository at this point in the history
Signed-off-by: Sangwan Kwon <[email protected]>
  • Loading branch information
bitboom committed Oct 14, 2024
1 parent 43d1a29 commit 18a1469
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rmm/src/granule/array/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ impl Granule {
#[cfg(any(kani, miri, test))]
// DIFF: calculate addr using GRANULE_REGION
pub fn index_to_addr(&self) -> usize {
use crate::granule::GRANULE_REGION;
use crate::granule::{GRANULE_REGION, GRANULE_STATUS_TABLE_SIZE};
let idx = self.index();
assert!(idx >= 0 && idx < 8);
assert!(idx >= 0 && idx < GRANULE_STATUS_TABLE_SIZE);

#[cfg(any(miri, test))]
return crate::test_utils::align_up(unsafe {
Expand Down
4 changes: 1 addition & 3 deletions rmm/src/rmi/realm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ fn create_realm(vmid: usize) -> Result<(), Error> {
#[cfg(test)]
mod test {
use crate::realm::rd::{Rd, State};
use crate::rmi::{
ERROR_INPUT, GRANULE_UNDELEGATE, REALM_ACTIVATE, REALM_CREATE, REALM_DESTROY, SUCCESS,
};
use crate::rmi::{ERROR_INPUT, REALM_ACTIVATE, REALM_CREATE, SUCCESS};
use crate::test_utils::*;

use alloc::vec;
Expand Down
21 changes: 21 additions & 0 deletions rmm/src/rmi/rec/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,24 @@ pub fn set_event_handler(mainloop: &mut Mainloop) {
complete_psci(&mut caller, &mut target, status)
});
}

#[cfg(test)]
mod test {
use crate::realm::rd::{Rd, State};
use crate::rmi::{ERROR_INPUT, MAX_REC_AUX_GRANULES, REC_AUX_COUNT, SUCCESS};
use crate::test_utils::*;

use alloc::vec;

// Source: https://github.com/ARM-software/cca-rmm-acs
// Test Case: cmd_rec_create
// Covered RMIs: REC_CREATE, REC_DESTROY
// Related Spec: D1.2.4 REC creation flow
#[test]
fn rmi_rec_create_positive() {
let rd = realm_create();
let rec = rec_create(rd);
rec_destroy(rec);
realm_destroy(rd);
}
}
8 changes: 7 additions & 1 deletion rmm/src/rmi/rec/vtcr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use crate::rmi::error::Error;
use aarch64_cpu::registers::*;

fn is_feat_vmid16_present() -> bool {
ID_AA64MMFR1_EL1.read(ID_AA64MMFR1_EL1::VMIDBits) == ID_AA64MMFR1_EL1::VMIDBits::Bits16.into()
#[cfg(not(any(miri, test)))]
let ret = ID_AA64MMFR1_EL1.read(ID_AA64MMFR1_EL1::VMIDBits)
== ID_AA64MMFR1_EL1::VMIDBits::Bits16.into();

#[cfg(any(miri, test))]
let ret = true;
ret
}

pub fn prepare_vtcr(rd: &Rd) -> Result<u64, Error> {
Expand Down
67 changes: 65 additions & 2 deletions rmm/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::event::Context;
use crate::event::Mainloop;
use crate::granule::{GRANULE_REGION, GRANULE_SIZE};
use crate::monitor::Monitor;
use crate::rmi::realm::Params;
use crate::rmi::realm::Params as RealmParams;
use crate::rmi::rec::params::Params as RecParams;
use crate::rmi::{GRANULE_DELEGATE, GRANULE_UNDELEGATE, REALM_CREATE, REALM_DESTROY, SUCCESS};
use crate::rmi::{MAX_REC_AUX_GRANULES, REC_AUX_COUNT, REC_CREATE, REC_DESTROY};
use crate::{get_granule, get_granule_if};

use alloc::vec::Vec;
Expand Down Expand Up @@ -43,7 +45,7 @@ pub fn realm_create() -> usize {
let (rd, rtt, params_ptr) = (alloc_granule(0), alloc_granule(1), alloc_granule(2));

unsafe {
let params = &mut *(params_ptr as *mut Params);
let params = &mut *(params_ptr as *mut RealmParams);
params.s2sz = 40;
params.rtt_num_start = 1;
params.rtt_level_start = 0;
Expand All @@ -66,6 +68,52 @@ pub fn realm_destroy(rd: usize) {
}
}

pub fn rec_create(rd: usize) -> usize {
let (rec, params_ptr) = (alloc_granule(IDX_REC), alloc_granule(IDX_REC_PARAMS));
let ret = rmi::<GRANULE_DELEGATE>(&[rec]);
assert_eq!(ret[0], SUCCESS);

let ret = rmi::<REC_AUX_COUNT>(&[rd]);
assert_eq!(ret[0], SUCCESS);
assert_eq!(ret[1], MAX_REC_AUX_GRANULES);

let aux_count = ret[1];
unsafe {
let params = &mut *(params_ptr as *mut RecParams);
params.pc = 0;
params.flags = 1; // RMI_RUNNABLE
params.mpidr = 0; // MPIDR_VALID
params.num_aux = aux_count as u64;

for idx in 0..aux_count {
let mocking_addr = alloc_granule(idx + IDX_REC_AUX);
let ret = rmi::<GRANULE_DELEGATE>(&[mocking_addr]);
assert_eq!(ret[0], SUCCESS);

params.aux[idx] = mocking_addr as u64;
}
}

let ret = rmi::<REC_CREATE>(&[rd, rec, params_ptr]);
assert_eq!(ret[0], SUCCESS);

rec
}

pub fn rec_destroy(rec: usize) {
let ret = rmi::<REC_DESTROY>(&[rec]);
assert_eq!(ret[0], SUCCESS);

let ret = rmi::<GRANULE_UNDELEGATE>(&[rec]);
assert_eq!(ret[0], SUCCESS);

for idx in 0..MAX_REC_AUX_GRANULES {
let mocking_addr = alloc_granule(idx + IDX_REC_AUX);
let ret = rmi::<GRANULE_UNDELEGATE>(&[mocking_addr]);
assert_eq!(ret[0], SUCCESS);
}
}

pub fn align_up(addr: usize) -> usize {
let align_mask = GRANULE_SIZE - 1;
if addr & align_mask == 0 {
Expand All @@ -75,6 +123,21 @@ pub fn align_up(addr: usize) -> usize {
}
}

/*
* 0: RD
* 1: RTT0
* 2: Realm Params
* 3: RTT1
* 4: RTT2
* 5: RTT3
* 6: RTT4
* 7: REC
* 8: REC Params
* 9..25: REC AUX
*/
const IDX_REC: usize = 7;
const IDX_REC_PARAMS: usize = 8;
const IDX_REC_AUX: usize = 9;
pub fn alloc_granule(idx: usize) -> usize {
let start = unsafe { GRANULE_REGION.as_ptr() as usize };
let first = crate::test_utils::align_up(start);
Expand Down

0 comments on commit 18a1469

Please sign in to comment.