Skip to content

Instantly share code, notes, and snippets.

@ds84182
Last active July 10, 2018 03:57
Show Gist options
  • Save ds84182/35cb3643a3c7b0284c9c4f379dfccc95 to your computer and use it in GitHub Desktop.
Save ds84182/35cb3643a3c7b0284c9c4f379dfccc95 to your computer and use it in GitHub Desktop.
¿libwhp example?
extern crate libwhp;
extern crate memmap;
use libwhp::*;
use memmap::*;
fn main() {
println!("?");
let p = Partition::new().unwrap();
println!("??");
let mut property: WHV_PARTITION_PROPERTY = unsafe { std::mem::zeroed() };
property.ProcessorCount = 1;
p.set_property(
WHV_PARTITION_PROPERTY_CODE::WHvPartitionPropertyCodeProcessorCount,
&property,
).unwrap();
p.setup().unwrap();
println!("???");
// Replace with an actual mapping
const SIZE: UINT64 = 4096;
let mut map = MmapMut::map_anon(SIZE as usize).unwrap();
let source_address = map.as_mut();
source_address[0] = 0xF4; // x86 HLT Instruction
let guest_address: WHV_GUEST_PHYSICAL_ADDRESS = 0xF0000;
println!("????");
println!("{:?}", source_address.as_ptr() as *const VOID);
p.map_gpa_range(
source_address.as_ptr() as *const VOID,
guest_address,
SIZE,
WHV_MAP_GPA_RANGE_FLAGS::WHvMapGpaRangeFlagRead | WHV_MAP_GPA_RANGE_FLAGS::WHvMapGpaRangeFlagExecute,
).unwrap();
println!("?????");
let vp = p.create_virtual_processor(0).unwrap();
// Replace with actual register values
const NUM_REGS: UINT32 = 2;
let mut reg_names: [WHV_REGISTER_NAME; NUM_REGS as usize] = unsafe { std::mem::zeroed() };
let mut reg_values: [WHV_REGISTER_VALUE; NUM_REGS as usize] = unsafe { std::mem::zeroed() };
reg_names[0] = WHV_REGISTER_NAME::WHvX64RegisterRax;
reg_values[0].Reg64 = 0;
reg_names[1] = WHV_REGISTER_NAME::WHvX64RegisterRip;
reg_values[1].Reg64 = guest_address;
vp.set_registers(&reg_names, &reg_values).unwrap();
println!("??????");
loop {
let exit_context = vp.run().unwrap();
// Handle exits
if exit_context.ExitReason == WHV_RUN_VP_EXIT_REASON::WHvRunVpExitReasonX64Halt {
break;
} else {
println!("{:?}", exit_context.ExitReason);
}
}
println!("!!!");
// To translate a GVA into a GPA:
let gva: WHV_GUEST_PHYSICAL_ADDRESS = guest_address;
let (translation_result, gpa) = vp.translate_gva(
gva,
WHV_TRANSLATE_GVA_FLAGS::WHvTranslateGvaFlagValidateRead,
).unwrap();
println!("{:?} {:?}", translation_result, gpa);
println!("¿");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment