-
Notifications
You must be signed in to change notification settings - Fork 4
/
real_mode.rs
59 lines (53 loc) · 1.73 KB
/
real_mode.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use kvm_rs::kvm::Kvm;
use kvm_rs::vcpu::KvmExit;
use kvm_rs::{PhysAddr, UserMem};
fn main() -> std::io::Result<()> {
// Create VM & VCPU.
let vm = Kvm::new()?.create_vm()?;
let mut vcpu = vm.create_vpcu(0)?;
// Map memory for guest VM and initialize with guest image.
let mem = UserMem::with_init(0x1000, include_bytes!("../guest/guest16"))?;
unsafe {
vm.set_user_memory_region(PhysAddr(0x0), &mem)?;
}
// Initialize VPCU registers.
let mut regs = vcpu.get_regs()?;
regs.rip = 0;
regs.rflags = 0x2;
vcpu.set_regs(regs)?;
// Initialize VPCU special registers.
let mut sregs = vcpu.get_sregs()?;
sregs.cs.base = 0;
sregs.cs.selector = 0;
vcpu.set_sregs(sregs)?;
// Run VCPU until `hlt` instruction.
while let Ok(exit) = vcpu.run() {
match exit {
KvmExit::Halt => break,
KvmExit::IoIn(port, data) => {
println!("IO_IN: port={} len={}", port, data.len());
// Provide some input data.
data.fill(0xaa);
}
KvmExit::IoOut(_port, data) => {
let s = std::str::from_utf8(data).unwrap();
print!("{}", s);
}
KvmExit::MmioRead(addr, data) => {
println!("MMIO_READ: addr={:#x} len={}", addr, data.len());
// Provide some read data.
data.fill(0xbb);
}
KvmExit::MmioWrite(addr, data) => {
println!(
"MMIO_WRITE: addr={:#x} len={} data={:#x?}",
addr,
data.len(),
data
);
}
KvmExit::Debug(_pc) => {}
};
}
Ok(())
}