Skip to content

Commit

Permalink
ioctls: Rework map/unmap_user_memory
Browse files Browse the repository at this point in the history
Update for reworked MSHV_MAP/UNMAP_GUEST_MEMORY.
Create util.rs for generic utils, add set_bits() variadic macro for
shifting and setting by bit indices.

Signed-off-by: Nuno Das Neves <[email protected]>
  • Loading branch information
NunoDasNeves committed May 7, 2024
1 parent da4e654 commit 9739642
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions mshv-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ pub use hvdef::*;

pub mod hvcall;
pub use hvcall::*;

pub mod util;
11 changes: 11 additions & 0 deletions mshv-bindings/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Set bits by index and OR them together
#[macro_export]
macro_rules! set_bits {
($int_type:ty, $bit:expr) => {{
let bit: $int_type = (1 << $bit).into();
bit
}};
($int_type:ty, $bit:expr, $($bits:expr),+) => {{
set_bits!($int_type, $bit) | set_bits!($int_type, $($bits),+)
}};
}
3 changes: 2 additions & 1 deletion mshv-ioctls/src/ioctls/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1429,10 +1429,11 @@ mod tests {
)
} as *mut u8;
let mem_region = mshv_user_mem_region {
flags: HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE | HV_MAP_GPA_EXECUTABLE,
flags: set_bits!(u8, MSHV_MAP_GPA_BIT_WRITABLE, MSHV_MAP_GPA_BIT_EXECUTABLE),
guest_pfn: 0x1,
size: 0x1000,
userspace_addr: load_addr as u64,
..Default::default()
};

vm.map_user_memory(mem_region).unwrap();
Expand Down
6 changes: 4 additions & 2 deletions mshv-ioctls/src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,11 @@ mod tests {
)
};
let mem = mshv_user_mem_region {
flags: HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE | HV_MAP_GPA_EXECUTABLE,
flags: set_bits!(u8, MSHV_MAP_GPA_BIT_WRITABLE, MSHV_MAP_GPA_BIT_EXECUTABLE),
guest_pfn: 0x1,
size: 0x1000,
userspace_addr: addr as u64,
..Default::default()
};

vm.map_user_memory(mem).unwrap();
Expand Down Expand Up @@ -969,10 +970,11 @@ mod tests {
)
} as *mut u8;
let mem_region = mshv_user_mem_region {
flags: HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE | HV_MAP_GPA_EXECUTABLE,
flags: set_bits!(u8, MSHV_MAP_GPA_BIT_WRITABLE, MSHV_MAP_GPA_BIT_EXECUTABLE),
guest_pfn: 0x0_u64,
size: mem_size as u64,
userspace_addr: load_addr as u64,
..Default::default()
};
// TODO need more real time testing: validating data,
// number of bits returned etc.
Expand Down
3 changes: 2 additions & 1 deletion mshv-ioctls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@
//! )
//! } as *mut u8;
//! let mem_region = mshv_user_mem_region {
//! flags: HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE | HV_MAP_GPA_EXECUTABLE,
//! flags: set_bits!(u8, MSHV_MAP_GPA_BIT_WRITABLE, MSHV_MAP_GPA_BIT_EXECUTABLE),
//! guest_pfn: 0x1,
//! size: 0x1000,
//! userspace_addr: load_addr as u64,
//! ..Default::default()
//! };
//!
//! vm.map_user_memory(mem_region).unwrap();
Expand Down

0 comments on commit 9739642

Please sign in to comment.