• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
6 
7 use kvm::*;
8 use kvm_sys::kvm_regs;
9 use sys_util::{GuestAddress, GuestMemory};
10 
11 #[test]
test_run()12 fn test_run() {
13     // This example based on https://lwn.net/Articles/658511/
14     let code = [
15         0xba, 0xf8, 0x03, /* mov $0x3f8, %dx */
16         0x00, 0xd8, /* add %bl, %al */
17         0x04, '0' as u8, /* add $'0', %al */
18         0xee,      /* out %al, (%dx) */
19         0xb0, '\n' as u8, /* mov $'\n', %al */
20         0xee,       /* out %al, (%dx) */
21         0x2e, 0xc6, 0x06, 0xf1, 0x10, 0x13, /* movb $0x13, %cs:0xf1 */
22         0xf4, /* hlt */
23     ];
24 
25     let mem_size = 0x1000;
26     let load_addr = GuestAddress(0x1000);
27     let mem = GuestMemory::new(&vec![(load_addr, mem_size)]).unwrap();
28 
29     let kvm = Kvm::new().expect("new kvm failed");
30     let vm = Vm::new(&kvm, mem).expect("new vm failed");
31     let vcpu = Vcpu::new(0, &kvm, &vm).expect("new vcpu failed");
32 
33     vm.get_memory()
34         .write_at_addr(&code, load_addr)
35         .expect("Writing code to memory failed.");
36 
37     let mut vcpu_sregs = vcpu.get_sregs().expect("get sregs failed");
38     assert_ne!(vcpu_sregs.cs.base, 0);
39     assert_ne!(vcpu_sregs.cs.selector, 0);
40     vcpu_sregs.cs.base = 0;
41     vcpu_sregs.cs.selector = 0;
42     vcpu.set_sregs(&vcpu_sregs).expect("set sregs failed");
43 
44     let mut vcpu_regs: kvm_regs = unsafe { std::mem::zeroed() };
45     vcpu_regs.rip = 0x1000;
46     vcpu_regs.rax = 2;
47     vcpu_regs.rbx = 7;
48     vcpu_regs.rflags = 2;
49     vcpu.set_regs(&vcpu_regs).expect("set regs failed");
50 
51     let mut out = String::new();
52     loop {
53         match vcpu.run().expect("run failed") {
54             VcpuExit::IoOut {
55                 port: 0x3f8,
56                 size,
57                 data,
58             } => {
59                 assert_eq!(size, 1);
60                 out.push(data[0] as char);
61             }
62             VcpuExit::Hlt => break,
63             r => panic!("unexpected exit reason: {:?}", r),
64         }
65     }
66 
67     assert_eq!(out, "9\n");
68     let result: u8 = vm
69         .get_memory()
70         .read_obj_from_addr(load_addr.checked_add(0xf1).unwrap())
71         .expect("Error reading the result.");
72     assert_eq!(result, 0x13);
73 }
74