|
| 1 | +// Apple Silicon example. |
| 2 | +// Adapted from https://github.com/zhuowei/FakeHVF/blob/main/simplevm.c |
| 3 | + |
| 4 | +use std::ptr; |
| 5 | + |
| 6 | +use hv::arm64::{Reg, VcpuExt}; |
| 7 | + |
| 8 | +static CODE: &[u8] = &[ |
| 9 | + // Compute ((2 + 2) - 1) |
| 10 | + 0x40, 0x00, 0x80, 0xD2, // mov x0, #2 |
| 11 | + 0x00, 0x08, 0x00, 0x91, // add x0, x0, #2 |
| 12 | + 0x00, 0x04, 0x00, 0xD1, // sub x0, x0, #1 |
| 13 | + // Write it to memory pointed by x1 |
| 14 | + 0x20, 0x00, 0x00, 0xF9, // str x0, [x1] |
| 15 | + // Reboot the computer with PSCI/SMCCC |
| 16 | + // 0x84000009 is PSCI SYSTEM_RESET using SMC32 calling convention |
| 17 | + 0x20, 0x01, 0x80, 0xd2, // mov x0, 0x0009 |
| 18 | + 0x00, 0x80, 0xb0, 0xf2, // movk x0, 0x8400, lsl #16 |
| 19 | + 0x02, 0x00, 0x00, 0xD4, // hvc #0 |
| 20 | + // Infinite loop |
| 21 | + 0x00, 0x00, 0x00, 0x14, |
| 22 | +]; |
| 23 | + |
| 24 | +const MEM_SIZE: usize = 0x100000; |
| 25 | +const GUEST_ADDR: usize = 0x69420000; |
| 26 | + |
| 27 | +const RESULT_OFFSET: usize = 0x100; |
| 28 | +const GUEST_RESULT_ADDR: usize = GUEST_ADDR + RESULT_OFFSET; |
| 29 | + |
| 30 | +fn main() -> Result<(), hv::Error> { |
| 31 | + let load_addr = unsafe { |
| 32 | + libc::mmap( |
| 33 | + ptr::null_mut(), |
| 34 | + MEM_SIZE, |
| 35 | + libc::PROT_READ | libc::PROT_WRITE, |
| 36 | + libc::MAP_ANONYMOUS | libc::MAP_PRIVATE | libc::MAP_NORESERVE, |
| 37 | + -1, |
| 38 | + 0, |
| 39 | + ) as *mut u8 |
| 40 | + }; |
| 41 | + |
| 42 | + if load_addr == libc::MAP_FAILED as _ { |
| 43 | + panic!("libc::mmap returned MAP_FAILED"); |
| 44 | + } |
| 45 | + |
| 46 | + unsafe { |
| 47 | + ptr::copy_nonoverlapping(CODE.as_ptr(), load_addr, CODE.len()); |
| 48 | + } |
| 49 | + |
| 50 | + // Init VM |
| 51 | + hv::Vm::create_vm(ptr::null_mut()).expect("Failed to create VM"); |
| 52 | + |
| 53 | + // Initialize guest memory |
| 54 | + hv::Vm::map( |
| 55 | + load_addr, |
| 56 | + GUEST_ADDR as _, |
| 57 | + MEM_SIZE as _, |
| 58 | + hv::Memory::READ | hv::Memory::WRITE | hv::Memory::EXEC, |
| 59 | + ) |
| 60 | + .expect("Failed to map guest memory"); |
| 61 | + |
| 62 | + // Create VCPU |
| 63 | + let cpu = hv::Vm::create_cpu().expect("Failed to create CPU"); |
| 64 | + |
| 65 | + // Register regs |
| 66 | + cpu.set_reg(Reg::PC, GUEST_ADDR as _) |
| 67 | + .expect("Failed to set PC reg"); |
| 68 | + |
| 69 | + cpu.set_reg(Reg::X1, GUEST_RESULT_ADDR as _) |
| 70 | + .expect("Failed to set X1"); |
| 71 | + |
| 72 | + loop { |
| 73 | + cpu.run().expect("Failed to run CPU"); |
| 74 | + |
| 75 | + let info = cpu.exit_info(); |
| 76 | + println!("{:?}", info); |
| 77 | + |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + let result_addr = unsafe { load_addr.add(RESULT_OFFSET) } as *const u64; |
| 82 | + let result = unsafe { *result_addr }; |
| 83 | + |
| 84 | + println!("Result: {}", result); |
| 85 | + assert_eq!(result, 3); |
| 86 | + |
| 87 | + drop(cpu); |
| 88 | + |
| 89 | + hv::Vm::destroy()?; |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
0 commit comments