2022-09-13 17:55:17 +00:00
|
|
|
// Copyright 2017 The ChromiumOS Authors
|
2018-01-02 21:34:03 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
|
|
|
|
2022-07-27 18:11:32 +00:00
|
|
|
use base::MemoryMappingBuilder;
|
|
|
|
use base::SharedMemory;
|
2018-01-02 21:34:03 +00:00
|
|
|
use kvm::*;
|
|
|
|
use kvm_sys::kvm_regs;
|
2022-07-27 18:11:32 +00:00
|
|
|
use vm_memory::GuestAddress;
|
|
|
|
use vm_memory::GuestMemory;
|
2018-01-02 21:34:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_run() {
|
|
|
|
/*
|
|
|
|
0000 881C mov [si],bl
|
|
|
|
0002 F4 hlt
|
|
|
|
*/
|
|
|
|
let code = [0x88, 0x1c, 0xf4];
|
|
|
|
let mem_size = 0x10000;
|
|
|
|
let load_addr = GuestAddress(0x1000);
|
|
|
|
let guest_mem = GuestMemory::new(&[]).unwrap();
|
2022-05-30 05:02:31 +00:00
|
|
|
let mem = SharedMemory::new("test", mem_size).expect("failed to create shared memory");
|
2020-10-01 06:51:39 +00:00
|
|
|
let mmap = MemoryMappingBuilder::new(mem_size as usize)
|
2021-09-24 20:07:45 +00:00
|
|
|
.from_shared_memory(&mem)
|
2020-10-01 06:51:39 +00:00
|
|
|
.build()
|
2020-08-06 01:50:47 +00:00
|
|
|
.expect("failed to create memory mapping");
|
2018-01-02 21:34:03 +00:00
|
|
|
|
2018-02-01 01:49:07 +00:00
|
|
|
mmap.write_slice(&code[..], load_addr.offset() as usize)
|
2018-01-02 21:34:03 +00:00
|
|
|
.expect("Writing code to memory failed.");
|
|
|
|
|
|
|
|
let kvm = Kvm::new().expect("new kvm failed");
|
|
|
|
let mut vm = Vm::new(&kvm, guest_mem).expect("new vm failed");
|
|
|
|
let vcpu = Vcpu::new(0, &kvm, &vm).expect("new vcpu failed");
|
|
|
|
let mut vcpu_sregs = vcpu.get_sregs().expect("get sregs failed");
|
|
|
|
vcpu_sregs.cs.base = 0;
|
|
|
|
vcpu_sregs.cs.selector = 0;
|
|
|
|
vcpu.set_sregs(&vcpu_sregs).expect("set sregs failed");
|
|
|
|
|
|
|
|
let mut vcpu_regs: kvm_regs = unsafe { std::mem::zeroed() };
|
|
|
|
vcpu_regs.rip = load_addr.offset() as u64;
|
|
|
|
vcpu_regs.rflags = 2;
|
|
|
|
// Write 0x12 to the beginning of the 9th page.
|
|
|
|
vcpu_regs.rsi = 0x8000;
|
|
|
|
vcpu_regs.rbx = 0x12;
|
|
|
|
vcpu.set_regs(&vcpu_regs).expect("set regs failed");
|
2018-10-03 17:22:32 +00:00
|
|
|
let slot = vm
|
2020-05-22 01:05:06 +00:00
|
|
|
.add_memory_region(
|
2018-10-03 17:22:32 +00:00
|
|
|
GuestAddress(0),
|
2020-05-22 01:05:06 +00:00
|
|
|
Box::new(
|
2020-10-01 06:51:39 +00:00
|
|
|
MemoryMappingBuilder::new(mem_size as usize)
|
2021-09-24 20:07:45 +00:00
|
|
|
.from_shared_memory(&mem)
|
2020-10-01 06:51:39 +00:00
|
|
|
.build()
|
2020-05-22 01:05:06 +00:00
|
|
|
.expect("failed to create memory mapping"),
|
|
|
|
),
|
2018-10-03 17:22:32 +00:00
|
|
|
false,
|
|
|
|
true,
|
2018-12-12 22:33:42 +00:00
|
|
|
)
|
|
|
|
.expect("failed to register memory");
|
2018-01-02 21:34:03 +00:00
|
|
|
|
2019-10-22 15:30:36 +00:00
|
|
|
let runnable_vcpu = vcpu.to_runnable(None).unwrap();
|
2018-01-02 21:34:03 +00:00
|
|
|
loop {
|
2019-10-22 15:30:36 +00:00
|
|
|
match runnable_vcpu.run().expect("run failed") {
|
2018-01-02 21:34:03 +00:00
|
|
|
VcpuExit::Hlt => break,
|
|
|
|
r => panic!("unexpected exit reason: {:?}", r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut dirty_log = [0x0, 0x0];
|
|
|
|
vm.get_dirty_log(slot, &mut dirty_log[..])
|
|
|
|
.expect("failed to get dirty log");
|
|
|
|
// Tests the 9th page was written to.
|
|
|
|
assert_eq!(dirty_log[1], 0x1);
|
2018-10-03 17:22:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
mmap.read_obj::<u64>(vcpu_regs.rsi as usize).unwrap(),
|
|
|
|
vcpu_regs.rbx
|
|
|
|
);
|
2018-01-02 21:34:03 +00:00
|
|
|
}
|