crosvm/kvm/tests/dirty_log.rs
A. Cody Schuffelen 97dff044f8 Replace #[cfg(unix)] with #[cfg(any(target_os = "android", target_os = "linux"))]
Updates are made to source and documentation.

This more accurately represents the currently supported platforms of
Android/Linux and Windows, without unexpectedly including other
unix-like operating systems.

Command to reproduce:
$ find . -type f -not -path '*/\.git/*' | xargs -I {} sed -i 's/cfg(unix)/cfg(any(target_os = "android", target_os = "linux"))/g' {}
$ cargo fmt

md files manually updated to fix line lengths.

Renaming `unix` modules to `linux` will be done in a later CL.

Test: ./tools/dev_container ./tools/presubmit
Bug: b/298269162
Change-Id: I42c1bf0abf80b9a0df25551613910293217c7295
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4909059
Commit-Queue: Cody Schuffelen <schuffelen@google.com>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
2023-10-11 00:43:29 +00:00

80 lines
2.6 KiB
Rust

// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![cfg(any(target_os = "android", target_os = "linux"))]
#![cfg(target_arch = "x86_64")]
use base::MemoryMappingBuilder;
use base::SharedMemory;
use kvm::*;
use kvm_sys::kvm_regs;
use vm_memory::GuestAddress;
use vm_memory::GuestMemory;
#[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();
let mem = SharedMemory::new("test", mem_size).expect("failed to create shared memory");
let mmap = MemoryMappingBuilder::new(mem_size as usize)
.from_shared_memory(&mem)
.build()
.expect("failed to create memory mapping");
mmap.write_slice(&code[..], load_addr.offset() as usize)
.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();
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");
let slot = vm
.add_memory_region(
GuestAddress(0),
Box::new(
MemoryMappingBuilder::new(mem_size as usize)
.from_shared_memory(&mem)
.build()
.expect("failed to create memory mapping"),
),
false,
true,
)
.expect("failed to register memory");
let runnable_vcpu = vcpu.to_runnable(None).unwrap();
loop {
match runnable_vcpu.run().expect("run failed") {
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);
assert_eq!(
mmap.read_obj::<u64>(vcpu_regs.rsi as usize).unwrap(),
vcpu_regs.rbx
);
}