Skip to content

Commit

Permalink
mshv-bindings: add a macro for setting filed in GHCB page
Browse files Browse the repository at this point in the history
Signed-off-by: Muminul Islam <muislam@microsoft.com>
  • Loading branch information
russell-islam committed Aug 16, 2024
1 parent 51ed153 commit 4221eea
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions mshv-bindings/src/x86_64/snp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,40 @@ impl Default for svm_ghcb_base {
}
}
}

#[allow(unused_macros)]
macro_rules! set_svm_field_u64_ptr {
($this: ident, $name: ident, $value: expr) => {
unsafe {
(*$this).$name = $value;
}
let qword_offset = std::mem::offset_of!(svm_ghcb_base, $name) / 8;
let arr_idx = qword_offset / 64;
let bit_index = qword_offset % 64;
let mut bitmap = unsafe { (*$this).valid_bitmap };
bitmap[arr_idx] |= (1 << bit_index);
unsafe { (*$this).valid_bitmap = bitmap };
};
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_svm_macro() {
let mut st: svm_ghcb_base = svm_ghcb_base::default();
println!("{}", std::mem::size_of::<svm_ghcb_base>());
let pptr: *mut svm_ghcb_base = &mut st;
set_svm_field_u64_ptr!(pptr, cpl, 100);
let mut val = st.cpl as u64;
let mut bitmap = st.valid_bitmap;
assert!(100 == val);
assert!((33554432 == bitmap[0]));
set_svm_field_u64_ptr!(pptr, xfem, 0x100);
val = st.xfem;
assert!(0x100 == val);
bitmap = st.valid_bitmap;
assert!(0x2000000000000000 == bitmap[1]);
}
}

0 comments on commit 4221eea

Please sign in to comment.