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 base::{MemoryMappingBuilder, SharedMemory};
8 use kvm::*;
9 use kvm_sys::kvm_regs;
10 use vm_memory::{GuestAddress, GuestMemory};
11
12 #[test]
test_run()13 fn test_run() {
14 /*
15 0000 881C mov [si],bl
16 0002 F4 hlt
17 */
18 let code = [0x88, 0x1c, 0xf4];
19 let mem_size = 0x10000;
20 let load_addr = GuestAddress(0x1000);
21 let guest_mem = GuestMemory::new(&[]).unwrap();
22 let mem = SharedMemory::anon(mem_size).expect("failed to create shared memory");
23 let mmap = MemoryMappingBuilder::new(mem_size as usize)
24 .from_shared_memory(&mem)
25 .build()
26 .expect("failed to create memory mapping");
27
28 mmap.write_slice(&code[..], load_addr.offset() as usize)
29 .expect("Writing code to memory failed.");
30
31 let kvm = Kvm::new().expect("new kvm failed");
32 let mut vm = Vm::new(&kvm, guest_mem).expect("new vm failed");
33 let vcpu = Vcpu::new(0, &kvm, &vm).expect("new vcpu failed");
34 let mut vcpu_sregs = vcpu.get_sregs().expect("get sregs failed");
35 vcpu_sregs.cs.base = 0;
36 vcpu_sregs.cs.selector = 0;
37 vcpu.set_sregs(&vcpu_sregs).expect("set sregs failed");
38
39 let mut vcpu_regs: kvm_regs = unsafe { std::mem::zeroed() };
40 vcpu_regs.rip = load_addr.offset() as u64;
41 vcpu_regs.rflags = 2;
42 // Write 0x12 to the beginning of the 9th page.
43 vcpu_regs.rsi = 0x8000;
44 vcpu_regs.rbx = 0x12;
45 vcpu.set_regs(&vcpu_regs).expect("set regs failed");
46 let slot = vm
47 .add_memory_region(
48 GuestAddress(0),
49 Box::new(
50 MemoryMappingBuilder::new(mem_size as usize)
51 .from_shared_memory(&mem)
52 .build()
53 .expect("failed to create memory mapping"),
54 ),
55 false,
56 true,
57 )
58 .expect("failed to register memory");
59
60 let runnable_vcpu = vcpu.to_runnable(None).unwrap();
61 loop {
62 match runnable_vcpu.run().expect("run failed") {
63 VcpuExit::Hlt => break,
64 r => panic!("unexpected exit reason: {:?}", r),
65 }
66 }
67
68 let mut dirty_log = [0x0, 0x0];
69 vm.get_dirty_log(slot, &mut dirty_log[..])
70 .expect("failed to get dirty log");
71 // Tests the 9th page was written to.
72 assert_eq!(dirty_log[1], 0x1);
73 assert_eq!(
74 mmap.read_obj::<u64>(vcpu_regs.rsi as usize).unwrap(),
75 vcpu_regs.rbx
76 );
77 }
78