|
| 1 | +# hv |
| 2 | + |
| 3 | +[](https://github.com/mxpv/hv/actions/workflows/ci.yml) |
| 4 | + |
| 5 | +`hv` is a high level Rust bindings for Hypervisor Framework. |
| 6 | + |
| 7 | +Build virtualization solutions on top of a lightweight hypervisor using Rust: |
| 8 | +- Full Hypervisor Framework support. |
| 9 | +- Supports Apple Silicon. |
| 10 | +- Rusty API. |
| 11 | + |
| 12 | +Please also see the [repository](https://github.com/mxpv/hv) for latest changes and updates. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +### Hypervisor Framework |
| 17 | + |
| 18 | +At runtime, determine whether the Hypervisor APIs are available on a particular machine with the `sysctl`: |
| 19 | + |
| 20 | +```bash |
| 21 | +$ sysctl kern.hv_support |
| 22 | +kern.hv_support: 1 |
| 23 | +``` |
| 24 | + |
| 25 | +In order to use Hypervisor API your app must have `com.apple.security.hypervisor` entitlement. |
| 26 | +Refer to [example.entitlements](example.entitlements) for example of how entitlement file might look like. |
| 27 | + |
| 28 | +Use the following command to self sign your binary for local development: |
| 29 | + |
| 30 | +```bash |
| 31 | +$ codesign --sign - --force --entitlements=example.entitlements ./binary |
| 32 | +``` |
| 33 | + |
| 34 | +### Rust |
| 35 | + |
| 36 | +Developed and tested on latest stable Rust (1.53.0+). |
| 37 | + |
| 38 | +Be sure to have [Xcode](https://developer.apple.com/xcode/) installed and don't forget to `xcode-select --install`, |
| 39 | +otherwise `bindgen` may fail to find Hypervisor headers. |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +This crate uses [hv-sys]() |
| 44 | + |
| 45 | +## Example |
| 46 | + |
| 47 | +Here is basic "Hello world" example on Apple Silicon: |
| 48 | +```rust |
| 49 | +// Init VM |
| 50 | +hv::Vm::create_vm(ptr::null_mut())?; |
| 51 | + |
| 52 | +// Initialize guest memory |
| 53 | +hv::Vm::map(load_addr, GUEST_ADDR as _, MEM_SIZE as _, hv::Memory::READ)?; |
| 54 | + |
| 55 | +// Create VCPU |
| 56 | +let cpu = hv::Vm::create_cpu()?; |
| 57 | + |
| 58 | +// Set regs |
| 59 | +cpu.set_reg(Reg::PC, GUEST_ADDR)?; |
| 60 | +cpu.set_reg(Reg::X1, GUEST_RESULT_ADDR)?; |
| 61 | + |
| 62 | +// Run CPU |
| 63 | +loop { |
| 64 | + cpu.run()?; |
| 65 | + |
| 66 | + let info = cpu.exit_info(); |
| 67 | + println!("{:?}", info); |
| 68 | + |
| 69 | + break; |
| 70 | +} |
| 71 | +``` |
0 commit comments