Move GuestAddress and GuestMemory to a new crate for VM memory. This
will make separating sys_util and crosvm independent making it easier
to use sys_util functions outside of crosvm.
Change-Id: I12e14948ea85754dfa6267b3a3fb32b77ef6796e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2311251
Auto-Submit: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Tested-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
When a memory region is added to the guest, ownership is passed to the
Vm object. However, it was not possible to get the memory region back
when removing it from the Vm. Update the return type of
remove_memory_region so that the original boxed memory region can be
returned to the caller.
BUG=None
TEST=cargo test -p kvm
Change-Id: Ie39a57e0c037ed25a603865cb3dce1af1478e143
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2300840
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Tomasz Jeznach <tjeznach@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Implementation of a KVM split irqchip. KVM's "split irqchip"
functionality only works for x86/x86_64 so this is implemented in the
kvm/x86_64.rs sub-module. The chip has a userspace Pic, Ioapic, and Pit.
This change necessitated a couple modifications to the irqchip traits:
- The create_pit function has been removed, and it's now implied that
the creation of the irqchip handles the creation of the pit.
- A finalize_devices function has been added. This function will need
to be called on the irqchip after all devices have been setup. The
purpose of finalize_devices is to allow the irqchip to register any
userspace devices with the io_bus or mmio_bus, and for the irqchip to
supply any necessary EventFds to these devices.
- A service_irq_event function has been added. This function works a
lot like the service_irq function, except it's specifically designed
to work the same way an IRQFD works: it first asserts then immediately
deasserts the line. If a resamplefd is associated with the irq line,
the deassert doesn't happen immediately, but happens when an EOI
occurs for a vector associated with the line. The service_irq function
will still exist for unittests.
- A process_delayed_irq_events function has been added. There
is a case where a deadlock can occur if the main thread blocks on
locking the ioapic in order to service an irq event, while a vcpu
thread holding the ioapic lock waits on the main thread to process the
addition of a MSI route. So the irqchip delays the servicing of irq
events if it finds a locked ioapic, and the
process_delayed_irq_events function should be called regularly
by the main thread in order to re-try servicing any delayed irq events.
Bug: chromium:1077058
Test: split irqchip runs all available x86-specific irqchip tests.
Also added some tests specific for the split irqchip. Ran these tests
and cargo test -p hypervisor -p devices
Change-Id: I14866645b86b3bf318440051069ff165e2cf9d88
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2290192
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
In particular, Android targets use c_int whereas normal Linux targets
use c_ulong. An ioctl number should always fit in 16 bits, so casting
from c_uint (as bindgen uses for the constants) to either other type
should be safe.
BUG=b:158290206
TEST=cargo test
Change-Id: I3d808ba9a5588a75c029a299e3609d97e328e3cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2288229
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Andrew Walbran <qwandor@google.com>
Commit-Queue: Andrew Walbran <qwandor@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Add VcpuAArch64::set_one_reg, init, and init_pmu. init combines
arm_preferred_target and arm_vcpu_init from the kvm crate.
BUG=chromium:1077058
TEST=cargo test -p hypervisor, ARM build
Change-Id: I5005d568c2337da3b9a96ec05ca9865c6ddbab74
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247369
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Move try_clone from Kvm to Hypervisor, and add try_clone to Vcpu.
Include a flags field in CpuIdEntry, since it's used by KVM_SET_CPUID2.
Tighten the bounds on Vcpu::Runnable so it derefs to the same type as
Vcpu.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I451f9156210eae342122f97fedfad630b89a7645
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2274837
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Udam Saini <udam@google.com>
These functions are mostly unchanged from the kvm crate. Error code
checks were tweaked for consistency.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: Ib4b30281eed877efcb4b55332f1f588213b98918
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247370
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
set_msrs, which had used std::alloc in x86_64/src/regs.rs, now uses
vec_with_array_field to allocate the msr struct like get_msrs.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I288bb18cc9e9ebf3e52b4ac46e36d03013b620ea
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2246654
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Added get/set_lapic_state functions to KvmKernelIrqChip and KvmVcpu.
Added tests for the KvmKernelIrqChip.
BUG=chromium:1077058
TEST=added associated tests for get/set_lapic_state
Change-Id: I0f1cebd9db370b5453a951f7827de511399cddf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2260929
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
Added an emum for MPState, and functions to translate between MPState
and kvm_mp_state. Added get_mp_state and set_mp_state functions to KvmVm
and the IrqChip interface.
BUG=chromium:1077058
TEST=added associated tests for get/set_mp_state
Change-Id: I0825f81b1b4d85884690606d691e8b88e8306ae1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2261293
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
Add methods for making RunnableVcpus, requesting vmexits on a vcpu,
injecting interrupts, and setting I/O data. This code is unchanged
from the kvm crate.
A new Vcpu::handle_io_events method delivers pending IO events. This is
for hypervisors that handle IO events in userspace. On KVM it's a
no-op.
Delete integration tests, which were for showing how the hypervisor
traits fit together.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: Ie9eb6dd38f168031bc0ae1b3c44638e8930284a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247367
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
These functions are mostly unchanged from the kvm crate.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I819e34b2d7895b10658ca7d8870b64435220ea9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202847
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Use a MemSlot type alias for memory region slots instead of u32, for
better type checking and self-documentation.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I0ee790f2eba432a6569c720a216d5bd5a66b07ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2253336
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Modified ioapic implementation for the hypervisor abstraction layer.
This implementation has the same functionality and tests as the existing
one, but accepts a vec of resample_events instead of a GsiRelay. It also
takes a list of irqfds instead of creating them itself.
This will be used by the KvmSplitIrqChip, and will eventualy be modified
again to support an ApicBus when we want to support a UserspaceIrqChip.
Bug: chromium:1077058
Test: cargo test -p hypervisor -p devices
Change-Id: I306724266511a7975a25a34955651ea6f53e1c4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2255303
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
Copied and slightly modified pic implementation in the irqchip
submodule. It uses the PicState defined in the hypervisor crate and has
a register_resample_events function instead of a register_relay
function.
Some function signatures have been slightly changed to take &self and
&mut self where before they would take a PicState. This was done so
that the Pic struct itself would be the only object that needs access
to the resample_events rather than each PicState needing it's own copy
or clone.
This will later be used by the the KvmSplitIrqChip.
Test: cargo test -p devices -p hypervisor
Bug: chromium:1077058
Change-Id: Ia15ea1800b4339d3ad38d88d8ec6ace8ca5ea67a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2255302
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
Vcpus are created on the Vm object, instead of with Vcpu::new. Vm keeps
a copy of the Hypervisor to support create_vcpu. Otherwise the methods
are the same as the kvm crate.
BUG=chromium:1077058
TEST=cargo test
Change-Id: I6fbd0e5fb5d81d4362a259e85eb392d8edbfff1f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247366
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Vm capability checking has been split into two functions: one for a
small set of hypervisor-agnostic caps, and one for hypervisor-specific
u32 constants to support the plugin.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I17fb6804f0fa7114f39c5016da430afa75179919
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202848
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Implemented the remaining IrqChip trait functions for the
KvmKernelIrqChip, including register/unregister_irq_event and irq
routing functions.
Added some irqchip tests for setting irq routes and servicing irq lines.
Also added tests for registering irq events.
BUG=chromium:1077058
TEST=cargo test -p devices -p hypervisor
Change-Id: Ia04c927b663ebdcacc88bc61d746077aa5b02514
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2246648
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
Add Vm::create_device to create kernel virtual devices, currently for
VFIO, ARM VGIC v2, and ARM VGIC v3.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I83c9c367c2b2674c3faaae9e4ae09919adfe34b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202846
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
- Reduces code duplication between MMIO and mmap arenas
- Makes adding future types easier
- Makes upcoming deprecation of kvm crate easier
- Use BTreeMap instead of HashMap since it's more efficient
BUG=chromium:924405
TEST=compile and test
Change-Id: I520abed0926489e64aac046e0dc0cfeb72fae7b2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2216446
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Gurchetan Singh <gurchetansingh@chromium.org>
The separate Vm functions for MemoryMappings and MemoryMappingArenas
have been combined and now use a MappedRegion trait that the mappings
implement. msync_memory_region replaces the get_mmap_arena function,
which is used by VmMsyncRequest. Since Vm uses mutexes for cloning, it
can't return mem region references.
BUG=chromium:1077058
TEST=cargo test, cargo test -p sys_util, cargo test -p hypervisor
Change-Id: If257b16ee34d07820ae7ebdb9a3a598a41df013c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202845
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
Implemented get/set_pic/ioapic/pit functions for the KvmKernelIrqchip.
Added respective functions on KvmVm for interacting with the underlying
KVM API.
Added associated tests for get/set functions.
BUG=chromium:1077058
TEST=ran devices tests and added get/set function tests
Change-Id: I66a29828fe2f1fbdf54d7325656a003ac09e36d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2219422
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
The clock functions on the Vm trait are for any arch, to support
hypervisors that might have ARM pv clocks. The KVM implementation (x86
only) is mostly the same as before, but uses a hypervisor-agnostic
ClockState struct instead of the KVM struct.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I0e77ae997d6a30851d28aeb5f73c9ef8ebc464a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202742
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Hypervisor-agnostic structures for the pic, ioapic, lapic, and pit.
These are derived from existing structures in the pic, ioapic, and pit
implementations, as well as from the kvm_sys bindings.
Includes From implementations converting these structures to their
associated KVM structures. Also includes tests for these conversion
implementations.
BUG=chromium:1077058
TEST=added tests to convert kvm structures to hypervisor-agnostic
structures
Change-Id: Ie2f254bf2dba3aed755008296c00cb6a49f845fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2197716
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Vms and Vcpus will be try_cloneable and Send, so we can configure on
vcpu threads and so IrqChips can reference all Vcpus. To support
cloning, collection fields in Vm have been moved into arc mutexes, and
Vm and Vcpu are now Sized. Because this breaks object safety, all usage
of the traits will be via static binding.
Add impl AsRawFd for SafeDescriptor, to get the sys_util ioctl functions
working with SafeDescriptor; eventually the functions will change to
take SafeDescriptors. Copy set_user_memory_region helper from kvm
crate.
BUG=chromium:1077058
TEST=cargo test -p hypervisor
Change-Id: I23de47c4472a77632006d0d45de9754394b400c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2197337
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
This new module contains the irqchip trait and it's implementations. The
irqchips will work with the new hypervisor crate to abstract the
interaction between crosvm and kvm.
This just defines the irqchip trait and an empty implementation of the
KvmKernelIrqChip.
BUG=chromium:1077058
TEST=added test for creating a KvmKernelIrqChip and adding a Vcpu to it
Change-Id: Ic1609c965e0a057f5a9d4d74f1cae46edb46dcb4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2197398
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
This adds the ability for getting both supported/emulated cpuids from
the kvm hypervisor. In addition, checking the available capabilities
for kvm is now implemented.
BUG=chromium:1077058
TEST=Added unit tests for each implemented function.
Change-Id: Ide4c2840b7bfa022deae835eb734ea97c1859169
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2177641
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Udam Saini <udam@google.com>
Add arch-agnostic traits Vm and Vcpu. Add arch-specific traits
HypervisorXXX, VmXXX, VcpuXXX, with impls for KVM.
BUG=chromium:1077058
TEST=added test for functions and structs interacting with the traits
Change-Id: I809f42f32a558c7835831c90e24fca82ce7744ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2176562
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
This is a separate hypervisor crate for interacting with Kvm in an
abstract manner. The intention is to not leak the internals of kvm
with its specific calls in various places in the codebase. Currently,
this just creates an initial structure for adding various
implementations over time.
In addition, a SafeDescriptor class is added, that wraps a raw file
descriptor safely, without needing to wrap it in the rust file class.
The intention is to use this for non file raw descriptors.
BUG=chromium:1077058
TEST=added a basic kvm test that creates a Kvm struct
Change-Id: I4229203902e480b52435cde12bf0d25a322c71be
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2174756
Commit-Queue: Udam Saini <udam@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>