Commit graph

388 commits

Author SHA1 Message Date
Daniel Verkamp
fbb8137c0a Windows clippy fixes for Rust 1.81
Add the minimal allow(dead_code) and feature declarations to allow the
clippy checks to pass for Windows with Rust 1.81.

BUG=b:365852007
TEST=tools/presubmit clippy_mingw64

Change-Id: I9ab776b07d8e9e14fe380d9b5ef1fa332788ff63
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/6013080
Reviewed-by: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-11-13 00:07:11 +00:00
Daniel Verkamp
f2056c6684 hypervisor: aarch64: rename VcpuFeature::SVE -> Sve
This matches the Rust convention where only the first letter of an
acronym is capitalized in CamelCase.

<https://github.com/rust-lang/rfcs/blob/master/text/0430-finalizing-naming-conventions.md>

Change-Id: I22e4ff3e2a3e0f1e728dd9dba219a42f06b80fa9
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5980713
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Elie Kheirallah <khei@google.com>
Commit-Queue: Elie Kheirallah <khei@google.com>
2024-11-01 17:52:39 +00:00
Elie Kheirallah
97dbce5226 hypervisor: aarch64: add SVE support
Refactor `init` and add function `get_features` to get the Vcpu features.
Previously some features were only retrieved as part of `init`.
Add function `finalize` which is required for SVE support. SVE requires
`KVM_ARM_VCPU_INIT` to be called first, which gives access to the SVE
config, specifically `KVM_REG_ARM64_SVE_VLS`, which is a
pseudo-register that allows discovering and configuring the vector
lengths by userspace. After `finalize` is called,
`KVM_REG_ARM64_SVE_VLS` is no longer accessible, and the config is
final. In this patch, we are not modifying the vector lengths, reason
behind this is `KVM_ARM_VCPU_INIT` initializes `KVM_REG_ARM64_SVE_VLS`
to the best set of vector lengths that the host supports.

BUG=b:375372656
TEST=presubmit

Change-Id: Ibd0902ad68f28b9d4e9a595070fe972e620e5572
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5973935
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Elie Kheirallah <khei@google.com>
2024-11-01 17:08:42 +00:00
Daniel Verkamp
40920e7278 hypervisor: pass IoOperation data as slices
Previously, the Vcpu handle_io() and handle_mmio() functions used an
IoOperation containing a fixed-length data array to represent a write
and returning a fixed-length data array to represent a read, along with
a separate size field to indicate how much of the fixed-length array
should be read/written.

This change uses Rust slices to represent the I/O data instead:
- Write contains a &[u8] of data to be written.
- Read contains a &mut [u8] to be filled with the read data.

The new IoOperation matches the Bus read()/write() APIs more closely,
and avoids the need for hypervisors and callers to convert between
fixed-size arrays and slices.

The Bus::read() function now always initializes the data slice before
(potentially) calling a device's read() function. This ensures
consistent results even if a device does not always fill out every data
byte (for example, the default BusDevice read() handler that is a no-op)
or if no device is found. This replaces the previous zeroing that would
happen when initializing the read data array to return from handle_fn.
Without this, the data slice may have had stale data from the previous
MMIO/IO exit, depending on the hypervisor implementation.

No functional change intended.

BUG=b:359382839
TEST=tools/dev_container tools/presubmit

Change-Id: Id88ebfa7ece5cc7466c010db2cbde303aeb97bf8
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5913962
Reviewed-by: Vaibhav Nagarnaik <vnagarnaik@google.com>
Reviewed-by: Noah Gold <nkgold@google.com>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-10-15 20:21:19 +00:00
Daniel Verkamp
8d06f7a776 hypervisor: tests: add REP INSB/OUTSB test
This tests repeated I/O string instructions call the handle_io callback
the correct number of times with the expected size.

BUG=b:283990685

Change-Id: Ia8b55c7059082376d6cdd825224f362f46bd0ea2
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5798664
Reviewed-by: Oystein Eftevaag <oysteine@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Judson Powers <judsonp@google.com>
2024-10-07 17:38:25 +00:00
Daniel Verkamp
c9fb635561 hypervisor: haxm: fix repeated port IO (REP OUTSB etc.)
For repeated string port I/O instructions (REP OUTSB, REP INSB, and
similar for other data sizes), the size field specifies the size of one
operation, and count specifies the number of times to repeatedly
read/write the *same* port with *different* data. This was fixed for KVM
in commit 063708084c ("hypervisor: kvm: handle INS/OUTS VM exits");
apply the same fix to HAXM.

BUG=b:283990685

Change-Id: Iaf4cfe12fba816730030368651db0e6fb116e50a
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5798663
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Judson Powers <judsonp@google.com>
2024-10-07 17:38:19 +00:00
Frederick Mayle
2a96ff0fb1 hypervisor: kvm: riscv64: don't mutate through a shared ref
When shared refs are used, the compiler may generate code that doesn't
witness the new value written via the pointer.

Change-Id: I6354fc3af749eaf7aaf1369c30b273371047d351
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5893919
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Frederick Mayle <fmayle@google.com>
2024-10-01 17:37:01 +00:00
Daniel Verkamp
aef7f5f5a6 hypervisor: add gvm feature to Cargo.toml
This doesn't build upstream, but cfg(feature = "gvm") is checked in
various places in upstream code, which becomes a warning in Rust 1.80.

BUG=b:365852007

Change-Id: Ib13f8b6ce965f790b292db1cf827472a292473da
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5894665
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-10-01 02:17:20 +00:00
Daniel Verkamp
72550d97b1 hypervisor: x86_64: remove unused get_emulated_cpuid()
This was added in the original hypervisor abstraction layer since it was
in the kvm crate, but it isn't actually used anywhere in crosvm.

BUG=None
TEST=tools/dev_container tools/presubmit

Change-Id: Ie1eacbe90771b050e3a60a0c868362d2d454d99e
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5660939
Reviewed-by: Richard Zhang <rizhang@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-09-27 19:10:04 +00:00
Daniel Verkamp
52f0f63d87 hypervisor: geniezone: GZVM_CHECK_EXTENSION mutates its parameter
For GZVM_CAP_PROTECTED_VM at least, the GZVM_CHECK_EXTENSION ioctl
ovewrites its parameter with the result rather than using the return
value, so its parameter needs to be a mutable reference.

Change-Id: Ief9f153804ebb57e2b247f2b6a5f04b429c77a7d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5847469
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Frederick Mayle <fmayle@google.com>
2024-09-26 23:04:42 +00:00
Daniel Verkamp
f2d11d94d7 hypervisor: disable getsec and invvpid tests on Linux
These tests currently don't pass on KVM (at least on AMD), and I don't
think the tests are actually testing for the expected result (there is
no configured IDT, so the system triple faults and ends up at the reset
vector), which seems nontrivial to fix, so skip them on Linux for now.

This makes `tools/dev_container tools/presubmit` pass for me again.

Change-Id: Ifc31fdd88b270c1c37f0f105dc655be4048ed279
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5888352
Reviewed-by: Oystein Eftevaag <oysteine@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
2024-09-25 00:40:40 +00:00
Dmytro Maluka
6aed5cc351 x86_64: support --unprotected-vm-with-firmware
As pKVM and pvmfw development for x86 is ongoing, add initial support
for running VMs with pvmfw as the VM bootloader. For now only support
the --unprotected-vm-with-firmware development flag, for running regular
non-protected VMs with pvmfw, which doesn't require pKVM support.

Similarly to ARM, add a dedicated memory region for pvmfw to the x86-64
memory map. This region is located right below the PCI MMIO memory hole.
Its size is 4MB, like on ARM.

Similarly to ARM, the pvmfw entry point is at the beginning of the pvmfw
image, the normal x86-64 boot protocol is kept as is, and additionally
the VM payload (i.e. kernel) entry point is passed to pvmfw in the %rdi
register. This ABI is a subject to change, the final ABI for x86 pvmfw
is not defined yet.

BUG=b:354676864
TEST=1. Run a VM with "--unprotected-vm-with-firmware pvmfw_test.bin"
where pvmfw_test.bin begins with the "jmp *%rdi" instruction (2 bytes:
0xff 0xe7), i.e. pvmfw immediately jumping to the VM entry point.
Result: VM runs as usual.
2. Run a VM with "--unprotected-vm-with-firmware pvmfw_random.bin" where
pvmfw_random.bin contains just random data. Result: VM crashes.

Change-Id: Ib0ee998a99a0cf352a97222769b87be615419187
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5797353
Commit-Queue: Dmytro Maluka <dmaluka@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2024-09-20 19:49:24 +00:00
Ryan Neph
d93be1b11d virtio: optional prepare of pci bar mapping with explicit MemCacheType
Lazy-init of VirtioPciDevice shared memory region (to setup hypervisor
mapping) would previously use the MemCacheType seen on first mapping,
which could be anything -- this behavior is never desired, but grew from
a later addition of MemCacheType workaround to the existing Virtio "Fixed
Mapping" optimization.

On devices where the single hypervisor mapping's MemCacheType matters
(e.g. for devices that must configure it with
MemCacheType::CacheNonCoherent), if the first mapping attempted is for a
shmem with WB caching, all later mappings with WC or UC memtype would be
configured incorrectly.

Instead, query the VirtioDevice Trait implementator whether lazy-init of
a single hypervisor mapping should be used (the default), and with which
MemCacheType.

Attempting to later add a `CacheNonCoherent` mapping for a device that
explicitly declared `SingleMappingOnFirst(CacheCoherent)` is invalid,
and would lead to bugs, so we now treat this as an error and fail the
mapping altogether.

VirtioGpu device implementations will use this for devices with either
mandatory or optional-but-enabled non-coherent DMA (e.g. Intel devices
without coherent LLC shared between CPU/GPU, or that may opt to bypass
LLC coherency for optimal perf).

BUG=b:360295883, b:246334944
TEST=Builds

Change-Id: If41d238fd3c220e45c61d78da4a2505572709053
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5871721
Commit-Queue: Ryan Neph <ryanneph@google.com>
Reviewed-by: David Stevens <stevensd@chromium.org>
2024-09-20 00:33:26 +00:00
Daniel Verkamp
c2ebaa19e4 Fix bad indentation in Markdown around lists
Some doc comments were being incorrectly interpreted due to missing
indentation (for intentional line contintuations) or line breaks (for
new lines that were intended to be separate paragraphs).

Clippy warns about these as of Rust 1.80:
<https://rust-lang.github.io/rust-clippy/master/index.html#/doc_lazy_continuation>

Fix them along with some other nearby minor formatting cleanups.

TEST=tools/cargo-doc
TEST=tools/clippy # with rust-toolchain 1.80

Change-Id: Ice0b7cc3bd75d9ab08c10107a13f95ca9f87a0a3
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5758934
Auto-Submit: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-09-10 02:05:59 +00:00
Kaiyi Li
a1033831c4 hypervisor: allow test to config the TR register
This allows the long mode tests to config task state segment and IST,
which further allows tests to switch the interrupt stack without having
to change the privilege level.

TEST=./tools/dev_container cargo nextest run --workspace --profile=default -E 'package(hypervisor) & binary(hypervisor_virtualization)'  --retries 0 --no-capture
BUG=b:354901961

Change-Id: I9a118fb6eee1e7186b40f9e877583381acaa2aef
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5838082
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Kaiyi Li <kaiyili@google.com>
2024-09-10 00:01:48 +00:00
Kaiyi Li
77f8aaaa08 hypervisor: allow test to config paging
This change also changes the default long mode paging to make the first
2MiB use 4K pages, because 4K pages brings less memory pressures if the
test wants to configure different pages.

TEST=./tools/dev_container cargo nextest run --workspace --profile=default -E 'package(hypervisor) & binary(hypervisor_virtualization)'  --retries 0 --no-capture
BUG=b:354901961

Change-Id: Ib03eb49e465018a154447534f07bdac0d8b74d2d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5835891
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Kaiyi Li <kaiyili@google.com>
2024-09-09 23:16:05 +00:00
Daniel Verkamp
364b340472 hypervisor: kvm: KVM_GET_MSRS writes to its parameter
The KVM code that retrieves MSRs was using ioctl_with_ref(), which is
supposed to have a read-only reference, but KVM_GET_MSRS needs to write
via the provided reference.

Change-Id: I2f0972463c99d7d97f19834d23040ab49bbee20d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5847468
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Elie Kheirallah <khei@google.com>
2024-09-09 23:04:00 +00:00
Kaiyi Li
bfabdc1a80 hypervisor: allow test to config GDT
TEST=./tools/dev_container cargo nextest run --workspace --profile=default -E 'package(hypervisor) & binary(hypervisor_virtualization)'  --retries 0 --no-capture
BUG=b:354901961

Change-Id: Ibc63d4a9d3c4c85263a1fdc9325d42ef9e7d8507
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5838478
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Judson Powers <judsonp@google.com>
2024-09-09 17:54:29 +00:00
Kaiyi Li
574b3cc572 hypervisor: allow test to config IDT
TEST=./tools/dev_container cargo nextest run --workspace --profile=default -E 'package(hypervisor) & binary(hypervisor_virtualization)'  --retries 0 --no-capture
BUG=b:354901961

Change-Id: I823587f519107539c32268ef437949c00c9db0ea
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5835889
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Judson Powers <judsonp@google.com>
Reviewed-by: Oystein Eftevaag <oysteine@chromium.org>
2024-09-06 15:23:45 +00:00
Kaiyi Li
40ca3e029c hypervisor: add ModeConfig
... which will allow to config GDT, IDT, and page tables in hypervisor
tests in following CLs.

TEST=./tools/dev_container cargo nextest run --workspace --profile=default -E 'package(hypervisor) & binary(hypervisor_virtualization)'  --retries 0 --no-capture
BUG=b:354901961

Change-Id: Id0c2399fb28b915a244ddd9413b4086b98936280
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5836773
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Judson Powers <judsonp@google.com>
2024-09-06 00:53:35 +00:00
Kaiyi Li
39ed318f8f hypervisor: fix compile error with whpx feature enabled
BUG=b:354901961
TEST=cargo nextest run -E 'binary(hypervisor_virtualization) & package(hypervisor)' --features=all-msvc64,whpx --workspace --no-capture --retries=0

Change-Id: I054183f0e938dc90026c1d2dd364d4ddb7c1437d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5835881
Reviewed-by: Judson Powers <judsonp@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-09-05 22:41:27 +00:00
George Burgess IV
cdaf2bd57f kvm: add mut to mutable binding
`KVM_CREATE_DEVICE` turns into an `ioctl` that mutates `device`, so
`device` must be declared as `mut`. Rust 1.78 forwards `device.fd == 0`
to the `Ok(...)` return, which made this incorrect code more visible.

BUG=b:361302729
TEST=Built & ran with Rust 1.78.

Change-Id: Idf99228b67c657351681adcaec563dd021fbe451
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5838113
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2024-09-05 21:11:59 +00:00
Oystein Eftevaag
bde0c39860 Update a few older VM tests to work with more functional HAXM
* test_msr_access_invalid: Setting an invalid EFER bit gives a shutdown rather than a VcpuRun failure now
* test_getsec_instruction: Enter long mode first
* test_xsetbv_instruction: Long mode
* test_invept_instruction: Long mode
* test_invvpid_instruction: Long mode

Bug: 359608908, 342183625
Change-Id: Ife4e5dafc9894ce9f7ded84eae740183599d0ba7
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5787016
Reviewed-by: Judson Powers <judsonp@google.com>
Commit-Queue: Oystein Eftevaag <oysteine@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
2024-08-22 22:26:16 +00:00
Oystein Eftevaag
78e2d51d4f Added test to verify nonsupport of MONITOR and related instructions
Bug: 347004003
Change-Id: Idd62a6a9e16f6b5f19c1521fe9b35795c1f60e56
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5639273
Reviewed-by: Judson Powers <judsonp@google.com>
Commit-Queue: Oystein Eftevaag <oysteine@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
2024-08-22 20:08:38 +00:00
Pierre-Clément Tosi
2233006cd7 hyp: Rename ProtectionType::loads_firmware()
Gives the method a better name as not only was it not clear but it was
confusing, potentially being interpreted as the hypervisor (implementing
the protection type) loading the firmware, similarly to how it performs
what .isolates_memory() refers to.

Note: No functional change intended.

TEST=-

Change-Id: Ie7d7fb9dc3732658837ce035298c374e6e52bf30
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5800574
Auto-Submit: Pierre-Clément Tosi <ptosi@google.com>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Dmytro Maluka <dmaluka@chromium.org>
2024-08-21 10:06:42 +00:00
Judson Powers
b75642742d Check wstatus in check_raw_capability.
The winfo field is only populated with capability bits if wstatus is
nonzero.  If wstatus is zero, those bits are re-used and indicate why
HAXM is not usable.

Bug: 357064526
Test: tested locally
Change-Id: I9cb0c82267095e4f89c879d48d8a3b7c00dfe36c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5792562
Commit-Queue: Idan Raiter <idanr@google.com>
Commit-Queue: Judson Powers <judsonp@google.com>
Auto-Submit: Judson Powers <judsonp@google.com>
Reviewed-by: Idan Raiter <idanr@google.com>
2024-08-16 17:46:27 +00:00
Daniel Verkamp
ad7bf30508 hypervisor: haxm: fix segment limits
Similar to WHPX, it looks like HAXM's segment_desc_t structure exactly
mirrors the Intel VMCS, where segment limits are always represented in
bytes.

From the Intel SDM Vol. 3, 25.4.1 ("Guest Register State"):
> Segment limit (32 bits). The limit field is always a measure in bytes.

Fixes: 2fc3047b9b ("hypervisor: x86_64: clarify Segment limit units")
Change-Id: I533216ea2359d1702d72cae0192e11e0478cf2c3
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5751215
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Oystein Eftevaag <oysteine@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
2024-08-15 17:22:16 +00:00
Oystein Eftevaag
51cd1f219e Debug ISR handler test
Bug: 354046783
Change-Id: Icd70b160c2ab09fee4fd8d7019465b2640b315b0
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5689092
Reviewed-by: Judson Powers <judsonp@google.com>
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Oystein Eftevaag <oysteine@chromium.org>
2024-08-14 21:15:49 +00:00
Noah Gold
6dc36202de hypervisor: whpx: failing MMIO injects GP fault.
If our internal handling for MMIO fails catastrophically, we should
inject a GP fault (we can't satisfy the MMIO, and the guest needs to
know that).

BUG=b:352191269
TEST=builds (tested downstream)

Change-Id: I28919879e8451a3802c0ca353bc5abe2f2ed4199
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5777253
Reviewed-by: Idan Raiter <idanr@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
Reviewed-by: Vaibhav Nagarnaik <vnagarnaik@google.com>
2024-08-14 05:53:34 +00:00
Noah Gold
e9818ae0ca hypervisor: allow errors in handle_mmio handle_fn.
This fixes interface drift from a downstream hypervisor interface
change.

BUG=b:352191269

Change-Id: Iaa5bcbaee43b9db87df0a096ada3eaf12df9fefc
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5777252
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Vaibhav Nagarnaik <vnagarnaik@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
2024-08-14 05:52:23 +00:00
Oystein Eftevaag
9ef848ce72 Test for debug register persistence across vmexits
Change-Id: Ia0deeb6e29c62567dc2a70fc9283584069a03f7e
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5630056
Commit-Queue: Oystein Eftevaag <oysteine@chromium.org>
Reviewed-by: Judson Powers <judsonp@google.com>
Reviewed-by: Noah Gold <nkgold@google.com>
2024-07-30 20:09:06 +00:00
Daniel Verkamp
034f48e1af hypervisor: tests: add 32-bit pmode segment limit test
BUG=b:354053941

Change-Id: I16008b408379afd59730863587e2ca138c577bdf
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5730916
Reviewed-by: Oystein Eftevaag <oysteine@chromium.org>
Reviewed-by: Idan Raiter <idanr@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-07-29 21:03:30 +00:00
Daniel Verkamp
89d641d2a5 hypervisor: whpx: fix segment limits
It seems that WHPX also wants its segment Limit value to be in bytes
(unaffected by the Granularity flag), similar to KVM.

This also fixes a compilation failure (using "limit" rather than
"limit_bytes").

Tested with a 32-bit bzImage kernel on Windows 10 host.

Fixes: 2fc3047b9b ("hypervisor: x86_64: clarify Segment limit units")
Change-Id: Ifa013b3687a5febd4d36b3a830c9c6995cc2bdb9
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5743006
Reviewed-by: Noah Gold <nkgold@google.com>
Auto-Submit: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Noah Gold <nkgold@google.com>
2024-07-29 19:55:00 +00:00
Daniel Verkamp
2fc3047b9b hypervisor: x86_64: clarify Segment limit units
The limit value in a kvm_segment is intended to be in bytes, meaning it
should already include the scale factor from the G (granularity) field.
This is basically undocumented, but this is based on observation of the
values assigned to kvm_segment limit fields inside the kernel being
larger than 20 bits.

Rename the field in the generic Segment struct to "limit_bytes" to make
its units clear and match the KVM behavior.

I could not find documentation for either WHPX or HAXM to determine
whether their limit fields should be in units of bytes or not, so those
hypervisors currently perform the conversion to maintain the previous
behavior of using the raw limit field as encoded in the GDT entry.

This mostly does not matter for 64-bit guests, since segment limit
fields are ignored in long mode, but it does matter in 32-bit protected
mode.

BUG=b:354053941

Change-Id: I1e464d3e1b8556184c732adea034b326e5ed6a72
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5727075
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-07-26 21:20:39 +00:00
Idan Raiter
5c0079697e hypervisor: Minimal exception injection test
Write an invalid MSR, check GP handler runs. Initially written in real
mode but we saw Intel HAXM doesn't work, so swapped to long mode. Better
to test what we really will be doing anyway.

BUG=b:334055761
TEST=run test

Change-Id: If97b720d13eb796fd8d3b45dd4fcef25d0abf59c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5714993
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Idan Raiter <idanr@google.com>
Reviewed-by: Oystein Eftevaag <oysteine@chromium.org>
2024-07-19 16:59:31 +00:00
Elie Kheirallah
65a2a07aea hypervisor: aarch64: snapshot cache architecture info
Cache architecture information is written per cache level. Cache levels
are selected by writing to the register CSSELR_EL1. To perform a
complete snapshot, save all the cache information and restore it.
The code at the end writes back the previously existing CSSELR_EL1
value, to avoid changing behavior in the system.

BUG=b:274145255
TEST=presubmit, AOSP

Change-Id: Ie9fdef1f3aa6f5633f832ea136d389c129c1868d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5689416
Reviewed-by: Pierre-Clément Tosi <ptosi@google.com>
Commit-Queue: Elie Kheirallah <khei@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2024-07-18 23:38:34 +00:00
Noah Gold
1d6be0f345 hypervisor: TLB flush test
We need to verify that a hypervisor is flushing a TLB on region
removal. This test removes a region in the middle of the VM running,
which allows us to verify that MMIO occurs on the next instruction,
rather than a read from an unmapped region.

BUG=b:333728516
TEST=presubmit

Change-Id: I215520ecfd4181a1e1ebd3cb5d784b3db94e799a
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5690379
Reviewed-by: Idan Raiter <idanr@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Noah Gold <nkgold@google.com>
2024-07-10 20:42:34 +00:00
Noah Gold
97930cec38 hypervisor: tests: make Vm available to setup func.
BUG=b:333728516
TEST=presubmit

Change-Id: I9c8fdc48b919628ff2bb1f0bd762fd29496eae60
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5689769
Commit-Queue: Noah Gold <nkgold@google.com>
Reviewed-by: Idan Raiter <idanr@google.com>
2024-07-10 17:56:23 +00:00
Daniel Verkamp
f8011f9c15 hypervisor: aarch64: vcpu snapshot restore support
Add get_system_regs function to AArch64.
This function should return all advertised system registers by the
hypervisor.

BUG=b:274145255
TEST=AOSP on ARM64

Change-Id: Ia472863d70e1cf29fb745fa4afd6a11dc0d44b78
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5445727
Commit-Queue: Elie Kheirallah <khei@google.com>
Reviewed-by: Pierre-Clément Tosi <ptosi@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2024-07-09 20:12:28 +00:00
Daniel Verkamp
32ed310b9e hypervisor: kvm: check CAP_KVMCLOCK_CTRL once per VM
Retrieve the kvmclock capability once in KvmVm::new() instead of
checking it in each call to create_kvm_vcpu().

BUG=None
TEST=tools/dev_container tools/presubmit

Change-Id: I4acf3fbd68999a7b4aa517553b6684f27629e0e9
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5671241
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-07-08 20:13:24 +00:00
Daniel Verkamp
e2d619d6f0 hypervisor: kvm: avoid unnecessary PathBuf allocation
Create a Path directly in Kvm::new() rather than constructing a PathBuf
and borrowing it.

BUG=None
TEST=tools/dev_container tools/presubmit

Change-Id: Icf46c48dc2dde3548cc796fc0162d5b57d4ad9bd
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5671064
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
2024-07-08 20:10:41 +00:00
Daniel Verkamp
8fef190b71 hypervisor: kvm: get vcpu mmap size at startup
We don't need to call ioctl(KVM_GET_VCPU_MMAP_SIZE) repeatedly for each
vcpu; the result is constant. Retrieve it once during Kvm::new().

BUG=None
TEST=tools/dev_container tools/presubmit

Change-Id: Ibe9607524198cc65c00f1ac0be1311934d7a9671
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5671063
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-07-08 20:10:34 +00:00
Daniel Verkamp
7c126bad72 hypervisor: tests: initialize CPUID for KVM
KVM was excluded from several tests; these mostly did not work because
the test setup code failed to configure CPUID, so the guest vCPUs did
not have any CPUID bits enabled.

BUG=None
TEST=cargo test -p hypervisor

Change-Id: I8c42c90a3547c011ab4f62fa94b31d05b0af6d15
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5677667
Reviewed-by: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-07-08 17:33:09 +00:00
Daniel Verkamp
2cd2cfbd02 hypervisor: tests: fix test_xsaves compilation
There was a missing comma, so the global_asm macro failed to parse the
assembly. This test is conditionally enabled only for whpx, so it did
not cause the CI to fail since whpx is not enabled there currently.

BUG=None
TEST=cargo test -p hypervisor --features=whpx

Change-Id: I0f6788eacc46c12baeee9827202b68635cbe2e2a
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5677434
Reviewed-by: Noah Gold <nkgold@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
Auto-Submit: Daniel Verkamp <dverkamp@chromium.org>
2024-07-03 21:56:51 +00:00
Daniel Verkamp
da981f99a6 hypervisor: kvm, gunyah: manage vcpu fd as File
This will let us use the MemoryMappingBuilder::from_file() function that
accepts &File directly rather than the generic from_descriptor() that
takes a &dyn AsRawDescriptor.

BUG=b:242953353
TEST=tools/dev_container tools/presubmit

Change-Id: I6866243d8959e5b5d48d05f6a04fac5c8ffe02cd
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5671359
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-07-03 17:40:07 +00:00
Noah Gold
a9cfe0c903 hypervisor: test that xsaves is off on gHAXM.
BUG=b:333888472
TEST=presubmit

Change-Id: I1a8ef9bef1c82cfb378398ff3faab3ef8b46a1f4
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5669343
Reviewed-by: Judson Powers <judsonp@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
Reviewed-by: Idan Raiter <idanr@google.com>
2024-07-03 00:40:38 +00:00
Kaiyi Li
e44ac626c8 hypervisor: change the contract of VcpuX86_64::interrupt
... to always not to inject an interrupt in a not interruptible window.

Test: presubmit
Change-Id: I303de46b023df4ba0b33624f392d31103da7beec
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5664544
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Kaiyi Li <kaiyili@google.com>
2024-06-30 11:35:18 +00:00
Noah Gold
7c24ffe4f9 hypervisor: add xsaves test
BUG=b:333888472
TEST=presubmit

Change-Id: I5bb81c9c5504e6ed90c050616bac5ecca3fb1a2a
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5665734
Commit-Queue: Noah Gold <nkgold@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2024-06-28 23:11:30 +00:00
Daniel Verkamp
318319ce54 hypervisor: kvm: check interrupt readiness before injecting
The caller of VcpuX86_64 should normally check ready_for_interrupt()
before calling interrupt(), but it is relatively cheap to add the check
to interrupt() itself and return an error in the case of incorrect use.

For KVM on x86, this is only called in the split irqchip mode, so it
should not affect performance in the normal in-kernel irqchip crosvm
configuration. In any case, the data accessed by ready_for_interrupt()
should be cheap to access since it will already be in the cache on the
second call.

Fixes hypervisor::test_interrupt_injection_when_not_ready on Linux/AMD.

BUG=None
TEST=tools/dev_container tools/presubmit

Change-Id: Id830771b3fc920d1e9705275f1da3efdd3de3367
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5651809
Reviewed-by: Kaiyi Li <kaiyili@google.com>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-06-28 22:24:04 +00:00
Daniel Verkamp
383ae1a6bf Replace memoffset with std::core::offset_of
offset_of!() is available in the standard library as of Rust 1.77.

Also update intrusive-collections 0.9.4 -> 0.9.6, which drops an
indirect dependency on memoffset 0.5.6:

  cargo update -p intrusive-collections

BUG=b:344974550
TEST=tools/dev_container tools/presubmit

Change-Id: I37a9bf0aee5f5f55bdea1e17d71889e8a3badb36
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5667936
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2024-06-28 22:07:50 +00:00