Skip to content

Commit

Permalink
test_utils: Rename granule_addr to alloc_granule
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 0733283 commit 43d1a29
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
6 changes: 4 additions & 2 deletions rmm/src/granule/array/entry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::granule::array::{align_up, GRANULE_STATUS_TABLE};
use crate::granule::array::GRANULE_STATUS_TABLE;
use crate::rmi::error::Error;

use super::{GranuleState, GRANULE_SIZE};
Expand Down Expand Up @@ -165,7 +165,9 @@ impl Granule {
assert!(idx >= 0 && idx < 8);

#[cfg(any(miri, test))]
return align_up(unsafe { GRANULE_REGION.as_ptr() as usize + (idx * GRANULE_SIZE) });
return crate::test_utils::align_up(unsafe {
GRANULE_REGION.as_ptr() as usize + (idx * GRANULE_SIZE)
});

#[cfg(kani)]
return unsafe { GRANULE_REGION.as_ptr() as usize + (idx * GRANULE_SIZE) };
Expand Down
17 changes: 0 additions & 17 deletions rmm/src/granule/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,6 @@ pub const GRANULE_MEM_SIZE: usize = GRANULE_SIZE * GRANULE_STATUS_TABLE_SIZE;
// so the last region cannot be utilized.
pub static mut GRANULE_REGION: [u8; GRANULE_MEM_SIZE] = [0; GRANULE_MEM_SIZE];

#[cfg(any(miri, test))]
pub fn granule_addr(idx: usize) -> usize {
let start = unsafe { GRANULE_REGION.as_ptr() as usize };
let first = align_up(start);
first + idx * GRANULE_SIZE
}

#[cfg(any(miri, test))]
fn align_up(addr: usize) -> usize {
let align_mask = GRANULE_SIZE - 1;
if addr & align_mask == 0 {
addr
} else {
(addr | align_mask) + 1
}
}

#[cfg(not(any(kani, miri, test)))]
pub fn validate_addr(addr: usize) -> bool {
if addr % GRANULE_SIZE != 0 {
Expand Down
7 changes: 3 additions & 4 deletions rmm/src/rmi/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn set_event_handler(mainloop: &mut Mainloop) {
}?;

#[cfg(not(feature = "gst_page_table"))]
let mut granule = get_granule_if!(addr, GranuleState::Undelegated)?;
let _granule = get_granule_if!(addr, GranuleState::Undelegated)?;
}

if smc(MARK_REALM, &[addr])[0] != SMC_SUCCESS {
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn set_event_handler(mainloop: &mut Mainloop) {
let addr = arg[0];
{
// Avoid deadlock in get_granule() in smc
let granule = get_granule_if!(addr, GranuleState::Delegated)?;
let _granule = get_granule_if!(addr, GranuleState::Delegated)?;
}

if smc(MARK_NONSECURE, &[addr])[0] != SMC_SUCCESS {
Expand Down Expand Up @@ -93,7 +93,6 @@ pub fn set_event_handler(mainloop: &mut Mainloop) {

#[cfg(test)]
mod test {
use crate::granule::array::granule_addr;
use crate::rmi::gpt::GranuleState;
use crate::rmi::{ERROR_INPUT, GRANULE_DELEGATE, GRANULE_UNDELEGATE, SUCCESS};
use crate::test_utils::*;
Expand All @@ -103,7 +102,7 @@ mod test {

#[test]
fn rmi_granule_delegate_positive() {
let mocking_addr = granule_addr(0);
let mocking_addr = alloc_granule(0);
let ret = rmi::<GRANULE_DELEGATE>(&[mocking_addr]);
assert_eq!(ret[0], SUCCESS);
assert!(get_granule_if!(mocking_addr, GranuleState::Delegated).is_ok());
Expand Down
1 change: 0 additions & 1 deletion rmm/src/rmi/realm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ fn create_realm(vmid: usize) -> Result<(), Error> {

#[cfg(test)]
mod test {
use crate::granule::array::granule_addr;
use crate::realm::rd::{Rd, State};
use crate::rmi::{
ERROR_INPUT, GRANULE_UNDELEGATE, REALM_ACTIVATE, REALM_CREATE, REALM_DESTROY, SUCCESS,
Expand Down
13 changes: 6 additions & 7 deletions rmm/src/rmi/rtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,12 @@ pub fn validate_ipa(rd: &Rd, ipa: usize) -> Result<(), Error> {

#[cfg(test)]
mod test {
use crate::granule::array::granule_addr;
use crate::realm::rd::{Rd, State};
use crate::rmi::{
GRANULE_DELEGATE, GRANULE_UNDELEGATE, RTT_CREATE, RTT_DESTROY, RTT_INIT_RIPAS,
RTT_READ_ENTRY, SUCCESS,
};
use crate::test_utils::*; // alloc_granule
use crate::test_utils::*;

use alloc::vec;

Expand All @@ -374,10 +373,10 @@ mod test {
let rd = realm_create();

let (rtt1, rtt2, rtt3, rtt4) = (
granule_addr(3),
granule_addr(4),
granule_addr(5),
granule_addr(6),
alloc_granule(3),
alloc_granule(4),
alloc_granule(5),
alloc_granule(6),
);

for rtt in &[rtt1, rtt2, rtt3, rtt4] {
Expand Down Expand Up @@ -438,7 +437,7 @@ mod test {
fn rmi_rtt_init_ripas_positive() {
let rd = realm_create();

let (rtt1, rtt2, rtt3) = (granule_addr(3), granule_addr(4), granule_addr(5));
let (rtt1, rtt2, rtt3) = (alloc_granule(3), alloc_granule(4), alloc_granule(5));

for rtt in &[rtt1, rtt2, rtt3] {
let ret = rmi::<GRANULE_DELEGATE>(&[*rtt]);
Expand Down
26 changes: 20 additions & 6 deletions rmm/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::event::Context;
use crate::event::Mainloop;
use crate::granule::array::granule_addr; // alloc_granule
use crate::granule::{GRANULE_REGION, GRANULE_SIZE};
use crate::monitor::Monitor;
use crate::rmi::realm::Params;
use crate::rmi::{GRANULE_DELEGATE, GRANULE_UNDELEGATE, REALM_CREATE, REALM_DESTROY, SUCCESS};
use crate::{get_granule, get_granule_if};

use alloc::vec::Vec;

Expand Down Expand Up @@ -33,15 +34,13 @@ pub fn extract_bits(value: usize, start: u32, end: u32) -> usize {
(value >> start) & mask
}

use crate::{get_granule, get_granule_if};

pub fn realm_create() -> usize {
for mocking_addr in &[granule_addr(0), granule_addr(1)] {
for mocking_addr in &[alloc_granule(0), alloc_granule(1)] {
let ret = rmi::<GRANULE_DELEGATE>(&[*mocking_addr]);
assert_eq!(ret[0], SUCCESS);
}

let (rd, rtt, params_ptr) = (granule_addr(0), granule_addr(1), granule_addr(2));
let (rd, rtt, params_ptr) = (alloc_granule(0), alloc_granule(1), alloc_granule(2));

unsafe {
let params = &mut *(params_ptr as *mut Params);
Expand All @@ -61,8 +60,23 @@ pub fn realm_destroy(rd: usize) {
let ret = rmi::<REALM_DESTROY>(&[rd]);
assert_eq!(ret[0], SUCCESS);

for mocking_addr in &[granule_addr(0), granule_addr(1)] {
for mocking_addr in &[alloc_granule(0), alloc_granule(1)] {
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 {
addr
} else {
(addr | align_mask) + 1
}
}

pub fn alloc_granule(idx: usize) -> usize {
let start = unsafe { GRANULE_REGION.as_ptr() as usize };
let first = crate::test_utils::align_up(start);
first + idx * GRANULE_SIZE
}

0 comments on commit 43d1a29

Please sign in to comment.