Commit graph

234 commits

Author SHA1 Message Date
Steven Richman
e3259f6319 kernel_loader: load elf32 images
Loading 32-bit images is needed for kvm-unit-tests.

BUG=b:233317135
TEST=boots, new unit tests

Change-Id: I6ba63255e0e2993f3735ef81489f64dbd0bb6374
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3764465
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Steven Richman <srichman@google.com>
Tested-by: Steven Richman <srichman@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2022-07-26 16:10:07 +00:00
Sebastian Ene
acb1f6683c vmwdt: Add memory mapped watchdog backend driver
This adds a new dt node identified by "qemu,vm-watchdog". The watchdog
is instantiated per-cpu and each instance has the following layout
described in this document:
https://docs.google.com/document/d/1DYmk2roxlwHZsOfcJi8xDMdWOHAmomvs2SDh7KPud3Y/edit?resourcekey=0-oSNabc-t040a1q0K4cyI8Q#heading=h.4hbqud2zl941

The watchdog uses an internal clock which is driven by the
timerfd at regular intervals. When the internal clock expires, a counter
value is decremented. When the counter register reaches value "0",
the watchdog expires. To prevent 'watchdog expiration', regular pet
events should be sent from the guest kernel driver.

BUG=b:213422094
Signed-off-by: Sebastian Ene <sebastianene@google.com>
Change-Id: Ic8c0488ffdfed1a43b6deb34b72d37f46e5e18a2
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3768290
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2022-07-21 14:07:11 +00:00
Noah Gold
637b1ee1d1 arch: split platform lib code into sys mod.
Highlights:
* goldfish battery never worked on Windows, but it sort of compiled
  anyways downstream. This CL makes it unix only because it never worked
  on Windows as a device anyway.

BUG=b:237597358
TEST=builds

Change-Id: Ia5fc3b5e5fcd243d8454646b00a6857085d018ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3741432
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
Reviewed-by: Vikram Auradkar <auradkar@google.com>
2022-07-14 00:08:33 +00:00
Daniel Verkamp
1b7d5b8fba arch: provide one vcpu_init per vcpu
Rather than having a single vcpu_init instance that is used for all
VCPUs, make vcpu_init into a Vec so it can store different initial state
for each VCPU. This allows us to set up e.g. bootstrap processor state
differently than other processors, and it also means that the VcpuInit
struct doesn't need to be Copy.

BUG=b:237095693
TEST=Boot Linux with >1 CPU

Change-Id: I0ebfdc2dbd84d0817e3f75c2c852e4320b9e77c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3723798
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-06-29 18:14:49 +00:00
Noah Gold
4ea25d1e33 x86_64: add TSC leaf synthesis.
There have been two evolutions of providing the TSC cpuid leaf
(aka 0x15) to the guest.

a) For CrosVM on Windows, we have been providing the leaf
   unconditionally.  Furthermore, we've not been using the
   exact host leaf; instead, we calibrate the TSC frequency
   and provide that value in the leaf. This was done because
   the actual cpuid leaf values are not as accurate as
   we needed them to be to drive a guest clocksource.

b) In CrosVM mainline, 4080aaf9b3
   introduced the flag enable_pnp / enable_pnp_data, and
   provides the exact host 0x15 leaf to the guest if the
   flag is enabled.

This CL adds a new hypervisor capability (CalibratedTscLeafRequired) to control
whether or not the calibrated TSC leaf should be used, in addition to a new CLI
option to force it on hypervisors where it isn't enabled by default. The new
option is `--force_calibrated_tsc_leaf`.

BUG=b:213152505
TEST=builds upstream, battletested downstream on WHPX.

Change-Id: I611422808a9e10578c0ddcbd211ae902f937685f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3698993
Commit-Queue: Noah Gold <nkgold@google.com>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-06-28 02:23:55 +00:00
Daniel Verkamp
b4244d3952 kernel_loader: load ELF kernels at the right paddr
Previously, we were loading ELF kernels at the provided kernel_start
address plus the p_paddr (physical address) field of each program
header. This resulted in the kernel being loaded after a big gap of
zero bytes, which accidentally worked on x86_64 because 0x00 0x00
encodes a valid instruction, and the entry point was at the beginning of
the first section, so execution would effectively "nop slide" its way
from the supposed entry point all the way to the actual beginning of the
correct code. In addition, the Linux kernel entry point is compiled as
position-independent code, so the mismatched address did not matter.

Fix this by loading ELF kernels at whatever physical address they
specify, without adding any extra offset. The load_kernel() function
still accepts a start address, but this is now used simply to verify
that the ELF file does not try to load any sections outside of the
desired kernel region.

As a demonstration, we can look at the instructions at the kernel's
declared entry point (0x1000000 for a normal x86-64 Linux kernel in ELF
format) by attaching to the gdb stub and running:

   (gdb) disas 0x1000000,+8

With the old behavior, we get purely 0x00 0x00 opcodes, decoding as:
   0x0000000001000000:  add    BYTE PTR [rax],al
   0x0000000001000002:  add    BYTE PTR [rax],al
   0x0000000001000004:  add    BYTE PTR [rax],al
   0x0000000001000006:  add    BYTE PTR [rax],al

With the new behavior, we get the correct entry point instructions:
   0x0000000001000000:  lea    rsp,[rip+0x1203f51]        # 0x2203f58
   0x0000000001000007:  lea    rdi,[rip+0xfffffffffffffff2] # 0x1000000

BUG=b:234155022
TEST=cargo test -p kernel_loader
TEST=Boot x86-64 ELF vmlinux kernel

Change-Id: Iae4c8db022674e6311e54dffe479a1ed430a1ef4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3673612
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Anton Romanov <romanton@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
2022-06-23 22:30:37 +00:00
Daniel Verkamp
8cac16bf75 kernel_loader: return a struct for better naming
The new LoadedKernel structure also splits out the size and end address,
which are both used in different places (x86_64 wants end, aarch64 wants
both).

BUG=b:234155022
TEST=tools/presubmit
TEST=cargo test -p kernel_loader

Change-Id: I86a7e412c983f29a2c614cc9e6896aa11db6ba94
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3673611
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
2022-06-21 19:39:40 +00:00
Daniel Verkamp
77abcb0d0a arch: add VcpuInit data to provide initial values
This will be used to pass the initial register values for each vcpu.
Each architecture can define its own variant of the initial vcpu state.

BUG=b:234155022
TEST=tools/presubmit --all

Change-Id: I696a5ecda202c1ff0681f0b89b89b9eec11e27e7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3673615
Reviewed-by: Anton Romanov <romanton@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-06-17 20:50:51 +00:00
Vaibhav Nagarnaik
b8bfdb496f irqchip: Add IrqEventSource for each event
This allows irq processing thread to log statistics and metrics and
track them using the source of the device. To be upstreamed later.

Introduce `PCI_VENDOR_ID_CROSVM` which identifies devices created by
crosvm. And introduce `CrosvmDeviceId` which adds a unique device ID for
each type of crosvm device.

BUG=b:213149158
BUG=b:213152505
TEST=Compiled.

Change-Id: Ica1374d8a2d8fd627c9397553bb7fb62baeaff7d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3687418
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Vaibhav Nagarnaik <vnagarnaik@google.com>
2022-06-17 18:44:20 +00:00
Daniel Verkamp
fb641f67e4 resources: introduce AddressRange type
The AddressRange type is used for representing contiguous memory or I/O
address ranges, mostly equivalent to RangeInclusive<u64> but with the
added benefit of implementing Copy and not carrying an extra field used
only for iteration, which we don't need.

Using an inclusive range means we can represent the full address space
with no weird corner cases around the upper limit of the range (for
example, an AddressRange covering the full 64-bit space can be
represented as start: 0, end: u64::MAX).

AddressRange also replaces the previous MemRegion type, which was a
start/size pair.

This normalizes the interface of the resources crates to use
AddressRange rather than a start/size pair; more cleanups throughout the
tree may be possible, but this commit attempts to be somewhat smaller to
have a hope of being reviewable.

BUG=b:222769529
TEST=tools/presubmit --all
TEST=cargo test -p resources

Change-Id: I8c90bdc28456221b5107ef7e589233315ea739c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3696671
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Reviewed-by: David Stevens <stevensd@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-06-16 20:29:26 +00:00
Masami Hiramatsu
eb83ee79b6 aarch64: Add Goldfish battery to aarch64 crosvm
Add Goldfish battery to aarch64 crosvm. The guest kernel will
see the goldfish battery node on the FDT.
This copies the seccomp policy for the battery from x86-64.

BUG=b:214124318
TEST=create VM with parameter "--battery" or "--battery=type=goldfish"
TEST=android-sh -c 'dumpsys battery'
TEST=tast run ${DUT} arc.PerfBoot.vm

Change-Id: I8212e760db5ffbc5d01442cfa7930baa5962ef6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3686713
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Masami Hiramatsu <mhiramat@google.com>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Masami Hiramatsu <mhiramat@google.com>
2022-06-15 02:46:45 +00:00
Peter Collingbourne
4f7d84e5b9 Fix some clippy warnings on aarch64.
Change-Id: Iac9a9540c585a08374c966a1014c6746fbba0554
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3687061
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Peter Collingbourne <pcc@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-06-06 16:58:32 +00:00
Dapeng Mi
4080aaf9b3 crosvm: expose cpu frequency and temperature CPUID bits to guest
Turbostat detects if CPU frequency and temperature features are
supported by reading several CPUID entries bits. Expose these
CPUID bits to guest when enable-pnp-data option is enabled.

Several MSR registers which turbostat depends on have been already
passed through when ITMT is enabled. Adding code to detect this and
avoid to doubly pass through.

BUG=b:229770963
TEST=Run "turbostat" in Chrome OS guest and show correct value.

Change-Id: Ieebaf616d544cb187a80ec6f3313a246ebf54bb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3641466
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-05-31 02:03:38 +00:00
Mike Gerow
539ba3dcc9 Reland "devices: debugcon: add bochs-style debugcon device"
This is a reland of commit 129840471d

Original change's description:
> devices: debugcon: add bochs-style debugcon device
>
> This allows for creating a simple write-only debug connection that uses
> a single IO port. This method is supported by both bochs and qemu[1].
>
> The main motivation here is to help in improving crosvm's ability to
> boot OVMF, which makes use of this simple debug device on port 0x402.
> For that one would use `--serial
> type=stdout,hardware=debugcon,debugcon_port=1026` or something similar.
>
> Note that serde_keyvalue doesn't support parsing hex values, hence the
> need to use `1026`, but I intend to address that as well to allow the
> more natural `debugcon_port=0x402` phrasing.
>
> [1]: https://github.com/qemu/qemu/blob/master/hw/char/debugcon.c
>
> BUG=b:233610263
>
> Change-Id: I73238e5c35925668d133bfaa542fcbcf1e28765f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3661255
> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
> Commit-Queue: Mike Gerow <gerow@google.com>
> Tested-by: kokoro <noreply+kokoro@google.com>

Bug: b:233610263
Change-Id: I2f2125da95e8907f4f0c16930952e9b765d33e6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3671087
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Mike Gerow <gerow@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-05-26 19:25:02 +00:00
Mike Gerow
d46572f47e Revert "devices: debugcon: add bochs-style debugcon device"
This reverts commit 129840471d.

Reason for revert: This conflicts with https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3600167 breaking the build

Original change's description:
> devices: debugcon: add bochs-style debugcon device
>
> This allows for creating a simple write-only debug connection that uses
> a single IO port. This method is supported by both bochs and qemu[1].
>
> The main motivation here is to help in improving crosvm's ability to
> boot OVMF, which makes use of this simple debug device on port 0x402.
> For that one would use `--serial
> type=stdout,hardware=debugcon,debugcon_port=1026` or something similar.
>
> Note that serde_keyvalue doesn't support parsing hex values, hence the
> need to use `1026`, but I intend to address that as well to allow the
> more natural `debugcon_port=0x402` phrasing.
>
> [1]: https://github.com/qemu/qemu/blob/master/hw/char/debugcon.c
>
> BUG=b:233610263
>
> Change-Id: I73238e5c35925668d133bfaa542fcbcf1e28765f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3661255
> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
> Commit-Queue: Mike Gerow <gerow@google.com>
> Tested-by: kokoro <noreply+kokoro@google.com>

Bug: b:233610263
Change-Id: Ia70540c3041611f59968cb7929b238eecfe2530d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3671082
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Mike Gerow <gerow@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
2022-05-26 18:23:30 +00:00
Mike Gerow
129840471d devices: debugcon: add bochs-style debugcon device
This allows for creating a simple write-only debug connection that uses
a single IO port. This method is supported by both bochs and qemu[1].

The main motivation here is to help in improving crosvm's ability to
boot OVMF, which makes use of this simple debug device on port 0x402.
For that one would use `--serial
type=stdout,hardware=debugcon,debugcon_port=1026` or something similar.

Note that serde_keyvalue doesn't support parsing hex values, hence the
need to use `1026`, but I intend to address that as well to allow the
more natural `debugcon_port=0x402` phrasing.

[1]: https://github.com/qemu/qemu/blob/master/hw/char/debugcon.c

BUG=b:233610263

Change-Id: I73238e5c35925668d133bfaa542fcbcf1e28765f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3661255
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Mike Gerow <gerow@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-05-26 17:13:59 +00:00
Vineeth Pillai
81b5e616d6 crosvm: vm_events: consolidate vm events into one framework.
crosvm waits on events like exit, reset, crash, guest panic etc and
uses eventfd to wait on these events. As of now, we have 4 eventfds
and may increase.

This is an attempt to consolidate all Vm events into one framework.
Use Tube instead of Event to get consistent behavior between OSes.
Implement a wrapper over Tube to have a consistent API for events.

BUG=None.
TEST=Built crosvm. Ran a minimal vm to panic and verified that crosvm
received the panic event. cargo test on devices.

Change-Id: I313d428de5e3ce3b879982f913918ec0a4a72c35
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3480577
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Vineeth Pillai <vineethrp@google.com>
Reviewed-by: Noah Gold <nkgold@google.com>
2022-05-20 18:04:21 +00:00
Sahitya Tummala
a6945f4a49 aarch64: Add support for loading kernel image formatted as ELF
This is done similar to x86 platform to check if a kernel image have
ELF signature first and if it doesn't, then it is passed to the Image
loader as a fallback.

BUG=b:232360323

Change-Id: I27b362952bd7f2678d1c7646b1b1c052c3518fca
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3646449
Commit-Queue: Steven Moreland <smoreland@google.com>
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-05-13 18:48:12 +00:00
Junichi Uekawa
78a12954cf crosvm: Keep one MsrFileType per vcpu.
Each vcpu has a vcpu->MsrDev file mapping. Instead of trying to create
an instance at first use, just keep an empty map in the beginning.

BUG=b:215297064
TEST=boot volteer-manatee

Change-Id: Ic5a63b0427a6441f190da3917b5f272bbb8ec778
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3611369
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-05-09 08:11:40 +00:00
Daniel Verkamp
45b0920767 doc: add a top-level comment to all crates
This provides at least a minimal one-line description to show what each
crate is about.

BUG=None
TEST=tools/cargo-doc

Change-Id: I26732e8c29062e622d5be09bdc120a49d564b9fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3630422
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-05-06 20:46:57 +00:00
Andrew Walbran
0465b61b1e Move PVTIME down below MMIO_BASE.
This leaves the region between 0x4000_0000 and the start of RAM free of
MMIO, which simplifies the stage 1 page table mappings needed by the
guest, and makes room for the pVM firmware allocation to be expanded.

TEST=tools/dev_container tools/run_tests --target=vm:aarch64

Change-Id: Ibae73a946cadd16e6f03559d23cc256f4b9d3f3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3627454
Commit-Queue: Andrew Walbran <qwandor@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Andrew Walbran <qwandor@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2022-05-06 13:01:31 +00:00
Shikha Panwar
dac771b644 Add --unprotected-vm-with-firmware flag to simulate protected mode.
This runs an unprotected VM but uses the same memory layout as for a
protected VM, loads pVM firmware, disables the same devices as for
protected mode. This is useful for debugging pVM firmware and other
protected VM issues.

TEST=crosvm --extended-status run --disable-sandbox --serial=stdout,hardware=serial,num=1 --unprotected-vm-with-firmware=pvmfw --bios=test_bootloader
TEST=tools/dev_container tools/run_tests --target=vm:aarch64
BUG=b:230475926

Change-Id: I9a8077d76874f10221071ea312291659d81da3d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3623760
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Andrew Walbran <qwandor@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2022-05-04 09:36:08 +00:00
ZhaoLiu
0c2ce08ca1 x86_64: Preparation for ITMT scheduling in Guest
With Intel Turbo Boost Max Technology 3.0, (ITMT),  the maximum turbo
frequencies of some cores in a CPU package may be higher than for the
other cores in the same package. In that case, make the scheduler prefer
to run tasks on the CPUs with higher max turbo frequencies, to achieve
better performance.

When vCPUs 1:1 map to pCPUs, ITMT scheduling feature can benefit
performance of Guest. So when user sets `--itmt`, crosvm will check if
`--host-cpu-topology` is set or if `--cpu-affinity` is set for 1:1
mapping.

In kernel, ITMT scheduling feature depends on intel_pstate driver and
ACPI CPPC support.

To setup intel_pstate driver and pass CPPC capability check, we need
emulate related MSRs in userspace:

- MSR_HWP_CAPABILITIES and MSR_TURBO_RATIO_LIMIT: intel_pstate driver
  only needs to read. So we passthrough them to Guest. But please note,
  to be compatible with the configuration in initramfs, use `FromCPU0`
  Temporarily.

- MSR_PM_ENABLE and MSR_HWP_REQUEST: they are MSRs to control HWP.
  When intel_pstate driver is initializing, the driver will close/open
  HWP by writing into MSR_PM_ENABLE, and the driver also needs to set
  performance policy by writing some hints into MSR_HWP_REQUEST.
  Note that we don't want Guest's behavior to affect the performance
  control of the host, so use 'emulate' handler for them.

- MSR_PLATFORM_INFO: this MSR is emulated in kernel with a fault value
  by default. intel_pstate doesn't want this fault value, so we ask
  this MSR to exit to userspace and give it a real value.

- MSR_IA32_PERF_CTL: intel_pstate driver use this MSR to set the min
  pstate in the initialization. So use 'emulate' handler for this MSR.

Since of `FromCPU0` setting of MSR_HWP_CAPABILITIES, ITMT scheduling
will set the same priority for each core so optimization will not work.

BUG=b:225375705
TEST=Set `--itmt` and check /proc/sys/kernel/sched_itmt_enabled

Change-Id: Icfa85ffa15c3b977c3b62a1f8a379c4c2f5fc49d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3575511
Commit-Queue: Zhao Liu <zhao1.liu@intel.corp-partner.google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
2022-04-25 08:21:16 +00:00
ZhaoLiu
b969ded8a5 crosvm: Support MSR emulation
At present, crosvm can support handle RDMSR in userspace by read MSR of
Host directly. This is the RDMSR passthrough in userpace.

This patch adds more MSR handler support, including:
- WRMSR passthrough: write the corresponding MSR of Host directly in
  userspace; The control of MSR will work on host;
- RDMSR emulation: crosvm uses a dummy MSR value to let Guest reads on.
  This value can be different with real MSR of Host;
- WRMSR emulation: crosvm uses a dummy MSR value to let Guest writes
  into. This means the control of MSR won't really work (on Host).

BUG=b:225375705
TEST=Set `--userspace-msr=0x1a2,type=r,action=pass,from=cpu0`

Change-Id: I3276dd651464ef7b695e2ddd433793d59128af9b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3575508
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
2022-04-25 07:50:55 +00:00
Elliot Berman
c903a58f86 aarch64: Abstract KVM register read/writes
Remove KVM-specific code from the generic aarch64 module by providing a
generic enum for hypervisor-specific vCpus to use instead:
VCpuRegAArch64.

Change-Id: Idd7379ad35943622c534b5e0d954ec15f9d15b41
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3586723
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Moreland <smoreland@google.com>
2022-04-18 17:58:06 +00:00
Elliot Berman
53d9530f36 linux: Drop kvm_ prefix from vcpu_ids
Simple rename to emphasize that vcpu_ids are not KVM-specific.

Change-Id: Icaf8a5f15a83021cf48467b0b8d0042ab0ab4a92
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3586010
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Moreland <smoreland@google.com>
2022-04-15 22:23:32 +00:00
Daniel Verkamp
ceb5ef155a aarch64: fdt: add PSCI compatible string tests
Move the PSCI version to compatible code to a function so it can be more
easily tested and add a few basic unit tests.

BUG=b:227142928
TEST=tools/dev_container tools/run_tests --target=vm:aarch64

Change-Id: I383b6e9df76f26995adab6fe980fd29fe1fcdf0a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3573362
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-04-07 18:08:55 +00:00
Daniel Verkamp
1d1372f742 aarch64: fdt: fix PSCI v0.1 case
This was accidentally creating a new `compatible` variable that shadowed
the other one rather than updating its value.

BUG=b:227142928
TEST=tools/presubmit

Change-Id: I11ec4f89501315d9946a5641493673ab6e938b76
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3573361
Reviewed-by: Pierre-Clément Tosi <ptosi@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-04-07 18:08:54 +00:00
Dmitry Torokhov
f021a62130 devices: irqchip: add specialized register_{edge|level}_irq_event()
The new specialized functions take IrqEdgeEvent and IrqLevelEvent
arguments, so that callers can use them directly.

BUG=None
TEST=./tools/presubmit

Change-Id: I2c1272e31f6b20eb22743b003bd23b9c1105cda6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3548066
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dmitry Torokhov <dtor@chromium.org>
2022-04-07 01:41:52 +00:00
Dmitry Torokhov
be3bbc0f37 devices: pl030: convert to using IrqEdgeEvent
This converts pl030 code to use IrqEdgeEvent.

BUG=None
TEST=./tools/presubmit

Change-Id: Ibc216d1989d90880176e70821f3d98ccc41302f0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3572999
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dmitry Torokhov <dtor@chromium.org>
2022-04-06 18:54:48 +00:00
Pierre-Clément Tosi
3a81c077b3 aarch64: fdt: Fix create_psci_node
Don't format the PSCI compatible string from the version received from
KVM as that generates invalid strings for versions other than 0.2 and
1.0. Instead, assume backward compatibility from 0.2 onwards and pass
only those that are relevant and supported by the kernel driver or pass
the PSCI v0.1 compatible string otherwise.

BUG=b:227142928
TEST=tools/dev_container tools/run_tests --target=vm:aarch64
TEST=booted a protected VM from the AOSP fork + checked DT node

Change-Id: I3b6a05b487751435b4e0e1c7a6f4643fece02ca7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3560156
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
2022-04-05 13:26:23 +00:00
Pierre-Clément Tosi
bdef378a27 arch: fdt: Pass &[&str] to property_string_list
Make the function take the more versatile slice of string slices,
instead of requiring caller code to generate the dynamic Vec and String
types, which can easily be converted back into those primitive types.

Note: no functional change intended.

BUG=b:227142928
TEST=booted a protected VM from the AOSP fork

Change-Id: I61701700efdacab4cfc5f5dd3df761e06a372065
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3560154
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
2022-04-05 13:26:21 +00:00
David Stevens
dc790f39c8 arch: refactor pci range generation
Add a method to SystemAllocator that returns a vector of all pools and
use it to generate the PCI ranges in build_vm. This also fixes a bug
where x86 used the size as the end address for the high_mmio region.

BUG=b:181736020
TEST=tast run trogdor|hatch arc.Boot.vm

Change-Id: Id4a300ec61c15bc08fb069b80428b22461ca935b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3516668
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: David Stevens <stevensd@chromium.org>
2022-03-17 01:58:14 +00:00
David Stevens
e4db417895 pstore: reserve ramoops region in system allocator
Remove the ramoops region from high_mmio when constructing the system
allocator. This means the aarch64 code no longer needs to manually
adjust high_mmio when determining the pci regions.

BUG=b:181736020
TEST=Check arcvm pstore still works

Change-Id: I81ca398a1984f0efb30c0a4d4b620bd50fe9df85
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3516667
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: David Stevens <stevensd@chromium.org>
2022-03-17 01:58:13 +00:00
David Stevens
00ab01e920 resources: use RangeInclusive to create allocator
Use RangeInclusive when constructing an AddressAllocator. A followup
change will add an iterator constructor, so using RangeInclusive makes
that API cleaner.

BUG=b:181736020
TEST=cargo test

Change-Id: I59c28b9cbd06c86482c0c57704fc2753a7886ac6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3516666
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: David Stevens <stevensd@chromium.org>
2022-03-17 01:58:12 +00:00
David Stevens
097623dce4 arch: return system allocator config
Have the arch code return the SystemAllocatorConfig instead of a
SystemAllocator. This will be used to allow the core code to apply
additional restrictions on top of the arch code's restrictions.

BUG=b:181736020
TEST=compiles

Change-Id: I4d9ca277f039586e664648492c8744967dcd2ee5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3516665
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: David Stevens <stevensd@chromium.org>
2022-03-17 01:58:11 +00:00
Dennis Kempin
ef516189db Refactoring: Move common/base into base
Executes the script in https://crrev.com/c/3530502

BUG=b:223206469
TEST=presubmit

Change-Id: Ibeffdc8de0b2270f070e60bb2de8d9fdc78a2a6d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3530503
Reviewed-by: Vikram Auradkar <auradkar@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2022-03-17 00:01:27 +00:00
Peter Fang
6ca0323c87 acpi: support fixed power button event in command line
Support injecting an ACPI fixed power button event using "powerbtn" in
the command line.

BUG=b:199383670
TEST=boot Linux kernel and trigger a power button event

Change-Id: I5ed57f533fa3d91043491fd1f0695223a139fc7a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3350492
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Nowicki <tnowicki@google.com>
2022-03-10 16:59:20 +00:00
Anton Romanov
bed40ad547 crosvm: migrate to Rust 2021 edition
BUG=none
TEST=cq

Change-Id: I0059c970879b78bfd40b6ce58b10debcf154b50f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3508322
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Anton Romanov <romanton@google.com>
2022-03-09 01:16:03 +00:00
Daniel Verkamp
0ae13d221e Require memoffset version 0.6
Chrome OS does not currently package memoffset-0.6.5 (only 0.6.4), so
building within cros_sdk will fail to find the expected crate. We don't
need any API requirements past what is provided by 0.6, so just request
that. At the same time, upgrade the cros_asyncv2 memoffset from 0.5 to
0.6 so we don't need two separate versions. (The crosvm ebuild was not
requiring a 0.5 version, so this was technically a bug.)

BUG=None
TEST=cargo build
TEST=emerge-hatch crosvm

Change-Id: I2b6c778cdbf57977e6fffd73867e0c4818c44a61
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3502625
Auto-Submit: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Vineeth Pillai <vineethrp@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-03-04 05:15:35 +00:00
Andrew Walbran
27f2133dba Use memoffset::offset_of rather than implementing it ourselves.
TEST=tools/dev_container tools/run_tests
TEST=tools/dev_container tools/run_tests --target=vm:aarch64

Change-Id: Iccdf0653001599d76c742db27349e274f7a4bb23
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3497460
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Andrew Walbran <qwandor@google.com>
2022-03-02 08:33:02 +00:00
Andrew Walbran
40b470c6b5 Log reset_type and cookie for PSCI SYSTEM_RESET2.
These may be used by the pVM firmware or bootloader for protected VMs on
Android to indicate the reason why a VM failed to boot, such as a
verification failure, and so we want to have them in the logs for
debugging.

BUG=b:220084045

Change-Id: I449873fb85d590d9e7978a93648e90586ecfff10
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3473452
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Andrew Walbran <qwandor@google.com>
2022-03-01 16:03:06 +00:00
Junichi Uekawa
629e352c67 Revert "system_allocator: allow more than one region to be in the pool"
This reverts commit d2d66bc0a4.

Reason for revert: It turns out that adding the first page to the pool
of memory managed by the MMIO allocator has undesired consequences
since crosvm will actually use it for MMIO regions. The first page
 has special semantics in other code though, and thus we get stray
accesses to this region, with hard-to-predict consequences.

BUG=b:188011323
TEST=cq

Original change's description:
> system_allocator: allow more than one region to be in the pool
>
> Allows crosvm-direct to have 0-0xfff regions to be mapped.
>
> limitations: Only the first regions gets reflected in the
> pool_base/pool_size.
>
> BUG=b:188011323
> BUG=b:184815519
> TEST=build
>
> Change-Id: I9da3cb2b8d5611068f9323d6ebf62f44162838b4
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3450017
> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
> Tested-by: kokoro <noreply+kokoro@google.com>
> Commit-Queue: Junichi Uekawa <uekawa@chromium.org>

Bug: b:188011323
Bug: b:184815519
Change-Id: Ib42b3007662a7a49ad876b83a01f1bb88d09d5f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3497136
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Mattias Nissler <mnissler@chromium.org>
Commit-Queue: Mattias Nissler <mnissler@chromium.org>
2022-03-01 11:19:44 +00:00
Vikram Auradkar
ae5118f5c7 Enable windows tests for a few crates
Run tests for sys_util_core, poll_token_derive and balloon_control on
windows.

Using dotfiles to disable/serialize test runs of a subset of crates does
not work well with third party crates as it forces us to commit the dot
file to the crate.

The patch modifies and uses the script that runs linux tests.
This patch also allows us to
- build/test child crate even if parent crate has disabled build/test.
- avoid building crosvm if it is not explicitly specified.

RIP short lived .windows_build_test_skip. You allowed us to run noop
kokoro tests.

Test: py .\tools\impl\test_runner.py --arch x86_64
Bug: b:215610772
Change-Id: Icc6d04ffd7c0c33d4f60aeac16fc7d23881c387d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3459809
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Vikram Auradkar <auradkar@google.com>
2022-02-18 03:18:58 +00:00
Daniel Verkamp
310d4b3593 aarch64: generalize FDT PCI node parameters
Rather than hard-coding knowledge of the low and high MMIO regions and
configuration space access region in the fdt creation code, we can move
this to the aarch64 VM setup code that has more visibility into memory
layout and allocators.

This will enable future refactoring to collapse the multiple MMIO
regions into a single allocator.

BUG=b:188011323
TEST=Boot Crostini on trogdor

Change-Id: I2709b831d94b467b772cd37465e3431072c7a49d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3457884
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-02-16 21:44:56 +00:00
Junichi Uekawa
d2d66bc0a4 system_allocator: allow more than one region to be in the pool
Allows crosvm-direct to have 0-0xfff regions to be mapped.

limitations: Only the first regions gets reflected in the
pool_base/pool_size.

BUG=b:188011323
BUG=b:184815519
TEST=build

Change-Id: I9da3cb2b8d5611068f9323d6ebf62f44162838b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3450017
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
2022-02-15 00:46:48 +00:00
Daniel Verkamp
f04ba1a14b aarch64: limit PCI IRQ allocator to number of SPIs
The previous limit was incorrect, as we are using SPI (Shared Peripheral
Interrupt) type interrupt sources for PCI devices, but the limit was
based on the total number of SPI + PPI interrupts.

This fixes interrupt delivery when many PCI devices are used on arm
platforms.

BUG=b:218757314
TEST=Run crosvm with 32+ block devices on trogdor

Change-Id: Ie89bc5b7115117d8acaca30ff758b9342940b450
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3453119
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-02-12 00:07:05 +00:00
Daniel Verkamp
7a06e688f7 aarch64: enlarge low MMIO allocator region
The previous size was only 1 MiB, which can be exhausted when enough
virtio-pci devices or other users of low MMIO are added. Increase it to
32 MiB to allow the addition of more devices, and while we are at it,
change the base of the low MMIO region to a more nicely aligned offset.

BUG=b:218757314
TEST=Run crosvm with 28 block devices on trogdor

Change-Id: Iea30d02f5c529be133b0fcb3666bf2ca72d5a90c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3453117
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-02-12 00:07:04 +00:00
Junichi Uekawa
67d4219489 system_allocator: use config object pattern for constructing
In preparation to allow multiple low memory regions to be passed on, I wanted to
make initialization simpler.

Introduce `MemRegion` struct instead of tuple to help me understand it is a base
and size.

BUG=b:188011323
TEST=build

Change-Id: Ie8b54354a25c478d5ad0a0185b7e07d28840dd87
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3439666
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
2022-02-10 08:46:07 +00:00
Dennis Kempin
d9af9d6a58 clippy: Fix aarch64 and crosvm-fuzz
Add proper conditional compilation flags. This will largely still skip
most of those crates, but at least the syntax will be checked.

BUG=b:192373803
TEST=./tools/clippy

Change-Id: I66d29ccdfec01f3a83b682a9cc135188fdc830cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3438705
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2022-02-04 19:16:05 +00:00
Andrew Walbran
985491ae73 Pass HypervisorCap by value rather than reference.
It's a simple enum, so it should be Copy.

TEST=tools/dev_container tools/run_tests
TEST=tools/dev_container tools/run_tests --target=vm:aarch64

Change-Id: Ieeacedb10fa6a93d6cdd9878097c66aa936f8e30
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3420329
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Andrew Walbran <qwandor@google.com>
2022-01-28 11:02:45 +00:00
Vikram Auradkar
0108238474 Upstream windows build/test script
The patch also adds files to skip building and testing crates on
windows. When we run
```
tools/windows/build_test.py --skip_file_name .windows_build_test_skip
```

the build/test succeeds without actually doing anything as build/test
for all crates is skipped by creating '.windows_build_test_skip'.

Bug: 213170957
Test: Ran script on downstream repo
Change-Id: Iebd2cea463ee722be4feaed88229e1fb5e9fd6c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3417918
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Vikram Auradkar <auradkar@google.com>
2022-01-28 00:55:08 +00:00
Daniel Verkamp
a06f22f2ac aarch64: actually load kernel at 8MB
The comment above indicates that it was intended to be loaded at an
offset of 8 megabytes, but it was missing a zero, so it was being loaded
at an offset of 512 kilobytes instead. Fix the constant to match the
comment.

Fixes a warning from newer kernels:

  Kernel image misaligned at boot, please fix your bootloader!

BUG=None
TEST=Boot Linux 5.10 on trogdor64, verify no error in dmesg

Change-Id: I2650d23458a13adff89a7f214734866c47d6f7cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3364972
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-01-11 22:41:43 +00:00
Daniel Verkamp
891ea3e88a aarch64: limit high MMIO addresses to VM phys addr range
The high MMIO region of the system allocator was previously set up with
the upper limit of 2**64 - 1, but physical addresses on actual systems
are not that large. Now that pmem allocates from the upper end of the
high MMIO region, the size of the allocator actually matters.

Limit the aarch64 high MMIO allocator to the VM's physical address size
so that addresses at the end of the allocation range are usable and pmem
devices can be created on aarch64.

BUG=b:210727578
TEST=Boot with pmem device on aarch64

Change-Id: I67962c2b03f0f265349ae889c988eb69ef526f4a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3366858
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-01-11 22:41:42 +00:00
Daniel Verkamp
3c2abaf838 aarch64: deduplicate MMIO region calculations
The get_high_mmio_base_size() function duplicates and hard-codes the
math from get_platform_mmio_base_size(). Since these are both only
called in one location now, pull them into get_resource_allocator() so
the plat_mmio variables can be used instead of duplicating the math.

Also reorder the SystemAllocator::builder() calls so that they are in
the same order as the layout of the regions in memory; this makes no
functional difference, but it is less confusing to read.

BUG=b:210727578
TEST=Boot crosvm on trogdor64

Change-Id: I1238e5b1d412e41d0b4b9be5b93b59fec6598734
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3373451
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-01-11 22:41:41 +00:00
Daniel Verkamp
a475cd4754 aarch64: avoid recalculating MMIO regions
Rather than calling get_high_mmio_base_size() twice, once in the Arch
create_system_allocator() callback and once in build_vm(), it would be
cleaner to retrieve the MMIO regions from the existing system allocator.
Add a couple of helper functions to AddressAllocator so we can get the
original region back.

This also helps in the next patch, which will pass additional
information to get_high_mmio_base_size() that is messy to calculate
twice.

BUG=b:210727578
TEST=Boot crosvm on trogdor64

Change-Id: Ia5c7d7c7ff7edb406a9b4af482b277204f865474
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3373450
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-01-11 22:41:40 +00:00
Daniel Verkamp
6f4f8223b8 hypervisor: add API to query VM physaddr size
Add a function that returns the size of guest physical addresses. This
wraps KVM_CAP_ARM_VM_IPA_SIZE on aarch64 and CPUID to query host
physical address size on x86_64. This replaces the phy_max_address_bits
function from x86_64, and all callers are migrated to use the new Vm
function instead.

BUG=b:210727578
TEST=check on trogdor64 with debug prints

Change-Id: I3107fe6357fcf166b7ad0e2a7c55919705da3b0c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3364971
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-01-11 22:41:38 +00:00
Andrew Walbran
00f1c9fd46 Update to the latest pKVM ABI.
This involves two main changes:
 * Protected VMs must be created with KVM_VM_TYPE_ARM_PROTECTED.
 * pVM firmware is now loaded by IPA rather than memslot ID.

There are also a lot of trivial changes because the ProtectionType enum
was moved from the devices crate to the hypervisor crate.

BUG=b:209794844
TEST=Will tested manually with patched kernel and dummy firmware

Change-Id: I1dd75e20063ca4736f155292ca5f70b94664fdd9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3330204
Auto-Submit: Andrew Walbran <qwandor@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-01-04 19:35:27 +00:00
Xiong Zhang
0b8318a2be Linux: Change root_config from PciConfigArch to PciRoot
Since PciRoot has been modified into Arc<Mutex<>>, RunnableLinuxVm->root_config
could be changed from PciConfigArch to PciRoot also, this could simplify code
and reduce two functions from PciConfigArch.

BUG=b:197877871
TEST=tools/presubmit

Change-Id: Ibc18587900d6f8259ac1d6f8fe7b3ea4fedad07e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3305942
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
2021-12-08 12:30:28 +00:00
Dmitry Torokhov
f75699f59c crosvm: return 32 exit code when guest issues reset request
Instead of returning 0 for both shutdown and reset/reboot, let's have
shutdown continue returning 0, but reset will now return 32 exit code,
so that callers will have an option to handle this case differently.

BUG=b:3169569
TEST=Examine crosvm exit codes when resetting and shutting down VM

Change-Id: I2c7c11b7f8c8528744f7e25e5d17c6fa9810f409
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3316835
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dmitry Torokhov <dtor@chromium.org>
2021-12-08 06:33:02 +00:00
Xiong Zhang
fd032f2a77 x86_64: Add pcie enhanced configruation into mmio_bus
Reserved 64MB mmio space for pcie enhanced configuration access,
and added it into mmio_bus with PciConfigMmio.

Now pci_root will be added into PciConfigIo and PciConfigMmio, so
Arc<Mux<pci_root>> is used.

BUG=b:197877871
TEST=tools/presubmit

Change-Id: Ic90a4cc3dfb00b09358478e129cc8a16e91012c6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3305941
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
2021-12-08 00:22:50 +00:00
Xiong Zhang
2b286f4305 pci_root: Don't assume register bit number is eight
In pcie enhanced configuration access, register bit number is 12, in order to
support such PciConfigAddress, this commit make register bit number
variable, it could be 8 or 12 decided by caller.

BUG=b:197877871
TEST=tools/presubmit

Change-Id: I96a317896b9536742534bee86f0e8f1acc323292
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3305940
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-12-07 01:11:42 +00:00
Xiong Zhang
f82f2dcfa0 devices:pcie: Add PcieRootPort into hotplug bus
PcieRootPort implement hotplug_bus trait, it should be added into
RunnableLinuxVm->hotplug_bus, so create pcie root port when
RunnableLinuxVm->hotplug_bus is ready.
Many hotplug bus may exist, when a hotplug device is added, is_match()
should be called on each hotplug bus before attach that device to
the bus.

BUG=b:185084350
TEST=Boot a vm with passthrough device and check its function

Change-Id: I89dbc091f336b3f9d0435b52963f7cf62d408a67
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955570
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-19 22:10:08 +00:00
Pierre-Clément Tosi
6e2d770d91 crosvm: arm64: Pass pVM image size through x2
Extend the ABI in protected mode to provide the protected VM firmware
with the size of the loaded image by using register x2.

BUG=b:203505371
TEST=presubmit

Change-Id: Ib01b88dcce2f3fbfba759599d7fce9de62de720c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3259937
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-18 11:34:24 +00:00
Pierre-Clément Tosi
ab8889c2f0 crosvm: arm64: Move image loading code in lib.rs
Move the block handling image loading above the code initializing vCPUs
to make an upcoming patch clearer: no functional change intended.

BUG=b:203505371
TEST=presubmit

Change-Id: Ie215a58de942dd5eceff5df46016bd17fafcf5ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3259936
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-18 11:34:23 +00:00
Chirantan Ekbote
55bf685478 aarch64: Fix UB in offset__of
This currently gives a compiler warning:

warning: dereferencing a null pointer
   --> aarch64/src/lib.rs:71:19
    |
71  |         unsafe { &(*(0 as *const $str))$(.$field)*  $([$idx])* as *const _ as usize }
    |                   ^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
...
622 |                 arm64_core_reg!(pc)
    |                 ------------------- in this macro invocation
    |
    = note: this warning originates in the macro `offset__of` (in Nightly builds, run with -Z macro-backtrace for more info)

Fix the UB with an implementation based on memoffset::offset_of and
addr_of_mut! instead.

BUG=none
TEST=`emerge-kukui crosvm` no longer complains about UB

Change-Id: I0ab55dc7527c3a4acffe02ee587742dbddd45a0f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3270099
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
2021-11-12 01:10:51 +00:00
Chirantan Ekbote
4bfa59ad61 aarch64: Fix warnings
BUG=none
TEST=emerge-kukui crosvm

Change-Id: I6d4eb3655cad7b5f2dbfa6f3315482b50fe033f5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3270098
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-11 05:29:02 +00:00
Daniel Verkamp
6b29858a34 linux.rs: replace Error enum with anyhow
Use the anyhow crate to provide ad-hoc errors with context. This removes
the large, manually-updated enum, which we never use programmatically
anyway (error messages are printed in human-readable form and otherwise
not matched against).

BUG=b:190433480
TEST=cargo build
TEST=./test_all

Change-Id: Ia7b90b33774d4031054b346d650861d3105044ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105436
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-11-05 23:06:09 +00:00
Dennis Kempin
8a1c50d5cd Refactoring: Move various general purpose crates to common/
This change contains the results of running

./tools/contib/cargo_refactor.py

This will break the next uprev, and needs to be synchronizized
with the corresponding ebuild changes in https://crrev.com/c/3248925

BUG=b:195126527
TEST=./tools/run_tests

Change-Id: Ied15a1841887bb8f59fba65b912b81acf69beb73
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3248129
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-29 22:31:43 +00:00
ZhaoLiu
2aaf7ad9fc x86: Support Host exposes CPU topology to Guest VM
At present the Guest generates its own CPU topology. To mitigate
cross-hyperthread speculative execution side channel attacks, allow
Guest to use mirror CPU topology of Host is needed for future scheduling
optimization.

Add a config option "--host-cpu-topology" to ask the vCPU number to be
identical to physical CPUs, and make the vCPU has the same APIC ID in
MADT and CPUID as the corresponding physical CPU. The same APIC ID can
ensure the same topology.

"--host-cpu-topology" requires vCPU number must equal to pCPU number,
and it has the default vCPU number setting, which equals to pCPU number.

"--host-cpu-topology" also defaultly sets CPU affinity for each vCPU to
the pCPU which has the same processor ID, like 1=1:2=2:3=3:4=4, so that
the vCPU and its corresponding pCPU will have the same processor ID and
the same APIC ID. User can't set CPU affinity if "--host-cpu-topology"
was set.

BUG=b:197875305
TEST=Set "--host-cpu-topology" option and check Guest's /proc/cpuinfo,
  lscpu, CPUID for different vCPU

Change-Id: Ibc4eb10649e89f43b81bde6d46d6e0e6c7234324
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3217035
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-10-25 04:19:45 +00:00
Xiong Zhang
3022a234b0 devices: Use Mutex to protect bus->devices
With vfio pci device hotplug, bus->devices will be inserted and removed at
runtime in different vcpu threads, so mutex should be used to protect it.

Both each vcpu thread and pci_root share mmio_bus and io_bus, so use Arc
for mmio_bus and io_bus. But pci_root is inserted into io_bus for pci cfg io,
in order to resolve reference loop and memory leak, weak is used for
pci_root.mmio_bus and pci_root.io_bus.

BUG=b:174705596
TEST=boot a vm and check its function

Change-Id: I7b3e63db76a90bc3873619b4d80a66db741ce78f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3184724
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-21 05:42:49 +00:00
Tomasz Nowicki
b8957f90a8 devices: vfio: Add VFIO platform support
Platform device is another type of device which might be assigned
directly to VM by using the same IOMMU DMA isolation technique.
In contrary to PCI, platform device has no config space which
describes device resources and makes it self-contained and
self-discoverable. Therefore, platform device resources are described in
FW and host is the one who enumerates/probes devices by parsing DT/ACPI.
We have ioctl i/f which allows to expose all that info to VMM.
Add platform bus specific calls to interact with host properly and
plug in to aarch64 bus creation code.

Note, for now all IRQs are abstracted to wired line (SPI in ARM GIC world)
which means guest wont see MSI-X for platform devices.

BUG=b:185504618
TEST=trogdor64-manatee SDHCI and GENIQUP device passthrough boots/works

Change-Id: Id0e9efde3586af77ed1d0b8bf79ca485906afb83
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2961216
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Nowicki <tnowicki@google.com>
Commit-Queue: Micah Morton <mortonm@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-04 17:06:19 +00:00
Tomasz Nowicki
d7cbad7707 resources: Introduce new allocator for VFIO platform device
DT platform bus abstraction requires its own MMIO address range.
Let it allocate from well defined address range.

BUG=b:185504618
TEST=trogdor64-manatee SDHCI and GENIQUP device passthrough boots/works

Change-Id: I357e909433a565968a7f081c727a3b1e420e7729
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2961212
Commit-Queue: Micah Morton <mortonm@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-04 16:32:47 +00:00
Tomasz Nowicki
ab86d52fba arch: Generalize PCI device box for build_vm
Before we call build_vm we are creating devices and there is no reason
to assume those have to be PCI only. In preparation for VFIO platform
device support, add super trait which allows to pass generic device
structure around and still be able get back to our original type.

BUG=b:185504618
TEST=manatee PCI device passthrough boots/works

Change-Id: I500f44af430f5f06299f20fc4ca17ca008a7e0c5
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2961210
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Nowicki <tnowicki@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-01 08:46:21 +00:00
Andrew Scull
82f723fede crosvm: arm64: Use helper function for entry address
Rather than duplicate the calculation, resuse the helper function that
calculates the entry address. This means there's just one place to make
changes and everything will be kept in sync.

BUG=none
TEST=presubmit

Change-Id: I2fd31dbaf2479ad0ce9d05c7341323b695268fa4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3111366
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Andrew Scull <ascull@google.com>
2021-09-22 12:21:14 +00:00
Daniel Verkamp
114361eb26 aarch64: convert to ThisError
BUG=b:197143586
TEST=cargo check

Change-Id: I21428922fce8f7a25d288c88813d43b10ee47fae
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105070
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-08-26 22:28:29 +00:00
Ryo Hashimoto
8f9dc1d55e aarch64: Support pstore
Add necessary kernel cmdlines for aarch64.
Adjust PCI address to make room for ramoops on aarch64.
Move the code from x86_64 to common places.

BUG=b:153934386
TEST=vm_pstore_dump

Change-Id: I8f92a7fd04a49afd7e8be9e7cf1901ef70b88d65
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3103131
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Kansho Nishida <kansho@chromium.org>
Commit-Queue: Ryo Hashimoto <hashimoto@chromium.org>
2021-08-24 14:31:44 +00:00
Xiong Zhang
9fadc3fda0 devices🚌 Add HotPlugBus Trait
Device implement HotPlugBus trait could notify hotplug event into
guest, and such device should be added into RunnableLinuxVm, so it
could be used at device plug in and plug out.

BUG=b:185084350
TEST=Boot a guest with and without passthrough device

Change-Id: I9497f61312582483090ff708d0f37b97d7303811
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2954673
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-08-24 00:56:29 +00:00
Xiong Zhang
8c9fe3ef34 Arch: Vfio device could be created and configured at runtime
When a vfio pci device is added through hotplug in, it should be configured
at runtime and added into pci_root->devices tree, so pci_root is added
into linux.

BUG=b:185084350
TEST=Boot a guest with and without passthrough device

Change-Id: Ibcb5f4a849134f64fbceeac645bebd80d6ca72d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2954672
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-08-24 00:56:28 +00:00
Keiichi Watanabe
553d219902 arch, devices: Move serial data structures to devices
Move the definitions of SerialHardware, SerialParameters and SerialType
to the devices crate so that they'll be available for code in the
devices crate as well.

BUG=b:195495971
TEST=cargo test

Change-Id: Ieb711bdb18a8afdb28cac262a3355739604d4607
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3096439
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-08-18 09:06:23 +00:00
Will Deacon
c48e78344e crosvm: arm64: Populate restricted DMA node based on 'swiotlb=' option
Protected VMs require all virtio data to be bounced through a memory
window shared between the host and the guest. Although this can be
achieved by forcing the guest to use swiotlb bouncing for all DMA
transfers, the recent introduction of "Restricted DMA" in Linux allows
these buffers to be sized and allocated on a per-device basis.

Remove the 'swiotlb=force' option when '--protected-vm' is set in favour
of describing a reserved memory region to be used as the swiotlb buffer
for all virtio-pci devices, adjustable via the new '--swiotlb' option.

BUG=b:190593703
TEST=cargo test on x86 and arm64 machines
Cc: David Brazdil <dbrazdil@google.com>
Cc: Claire Chang <tientzu@chromium.org>
Cc: Quentin Perret <qperret@google.com>
Cc: Andrew Walbran <qwandor@google.com>
Cc: Marc Zyngier <mzyngier@google.com>

Change-Id: I2d48a7c77740e7f3ad996ad33592f0acd53b7144
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3064198
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Will Deacon <willdeacon@google.com>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-08-13 16:12:35 +00:00
Suleiman Souhlal
63630e8dce Add --delay-rt flag.
This flag is intended to be used to delay making a VM's RT VCPUs
RT until "make_rt" is called, in order to avoid boot time
regressions with RT.

BUG=b:142777321
TEST="grep policy /proc/<pid of rt vcpu>/sched" to make sure RT vcpu
    isn't RT until "make_rt".

Change-Id: I1b9132e11f0acc626434c60cecc2f4afe350961a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2703224
Auto-Submit: Suleiman Souhlal <suleiman@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Suleiman Souhlal <suleiman@chromium.org>
2021-08-11 11:39:30 +00:00
Daniel Verkamp
da4e8a9fcd arch: move resume_notify_devices out of Bus
The devices to be notified on resume are unrelated to the functionality
of Bus, which is looking up devices in an address space. Additionally,
each Bus instance had its own list of devices to notify, although in
practice, only the one in the I/O bus was used.

Move the resume_notify_devices list into RunnableLinuxVm instead.

BUG=None
TEST=Boot Crostini on x86 and arm

Change-Id: I72c629c6d6589c4a9350831c8a076c5c0c9f9aeb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3043489
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Tomasz Jeznach <tjeznach@chromium.org>
2021-07-22 02:59:34 +00:00
Zide Chen
71435c1aea devices: virtio: iommu: implement VIRTIO_IOMMU_F_INPUT_RANGE feature
In VT-d, the IOMMU hardware can translate guest physical address that
is no more than MGAW (Maximum Guest Address Width) which is reported
from the VT-d Capability register.

We pass this information to the guest IOMMU front driver so that it
can allocate appropriate IOVA.

VT-d indicates that "implementations must support MGAW at least equal
to the physical addressability (host address width) of the platform".
Thus we take the Physical Address Bits that is reported by
CPUID.80000008H as the minimum MGAW.

BUG=b:181736020
TEST=--vfio=/sys/bus/pci/devices/0000:00:14.0,iommu=on

Change-Id: I26a421ea2e7dd893d413d63ab313721cfdf0b5c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2757278
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Stevens <stevensd@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: David Stevens <stevensd@chromium.org>
2021-07-19 06:29:44 +00:00
Pierre-Clément Tosi
86d5b199bc aarch64: Only enable pVM once umem allocs are done
Move the call to enable_protected_vm past the PVTIME memory region
allocation introduced by 895c90c5f2 ("aarch64: Implement
ARM_VCPU_PVTIME for "stolen time" accounting"). As such allocation is
prohibited once protected VM mode has been enabled (Kernel returns
EPERM), which currently results in crosvm failing when started with
--protected-vm.

BUG=b:193231584
TEST=crosvm --protected-vm on a kernel with KVM_CAP_ARM_PROTECTED_VM

Change-Id: I2a97d3ff64ed59dd6679e51951c79e27bf290d5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3021126
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
2021-07-13 17:57:05 +00:00
Stephen Boyd
895c90c5f2 aarch64: Implement ARM_VCPU_PVTIME for "stolen time" accounting
ARM supports paravirtual clocks or "stolen time" accounting by
allocating a page of memory in the guest memory map for a pvtime
structure and then sharing that address with the guest and updating it
in the host to tell the guest about how much time has been stolen from
the guest because the guest hasn't been running. Read all about it
here[1]. Add support to crosvm to allocate this page (for now a 64K size
chunk of memory that all VCPUs share access to) and then set the address
of the page in each VCPU when starting up.

[1] https://www.kernel.org/doc/html/latest/virt/kvm/arm/pvtime.html#stolen-time

BUG=chromium:1130828, b:169094241
TEST=emerge-trogdor crosvm && cros deploy $DUT crosvm && \
     tast run $DUT crostini.Basic.buster_stable;
     dmesg | grep pv # in the guest shows "steal time"

Change-Id: Ie3497bb22fb0e38eeff8ebac14b4213824bb6ca6
Disallow-Recycled-Builds: test-failures
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2871394
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen Boyd <swboyd@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-05-26 23:37:05 +00:00
Daniel Verkamp
8a72afc44c main: add --cpu-cluster and --cpu-capacity options
The --cpu-cluster option can be used to report a physical grouping of
CPUs in the device tree as a "cpu-map" node:

https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/topology.txt

Each instance of the --cpu-cluster option specifies a new group
consisting of a list of CPUs, and it may be repeated to add multiple
groups.

The --cpu-capacity option can be used to specify the relative
performance of CPUs so that the guest kernel's scheduler can make better
decisions on systems with heterogeneous cores (e.g. big.LITTLE).

--cpu-capacity units are left up to each architecture; for devicetree
systems, capacity is used to fill the cpu capacity-dmips-mhz (Dhrystone
benchmark MIPS per MHz) field in each cpu node:

https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/cpu-capacity.txt

For example, on a trogdor board, there are 6 little cores (0-5) with a
capacity of 452, and 2 big cores (6-7) with a capacity of 1024, which
results in a crosvm command line argument of:

  --cpu-capacity 0-452,1=452,2=452,3=452,4=452,5=452,6=1024,7=1024

Currently, these options only have an effect on devicetree platforms and
are ignored elsewhere; they may be expanded in the future to fill in the
equivalent ACPI tables if systems with heterogeneous CPUs are used
there.

BUG=b:182198842
TEST=Start crosvm on kevin with --cpu-cluster options
TEST=crosvm run --cpu-cluster 0,1,2,3 --cpu-cluster 4,5

Change-Id: I59c466549ccd908f8eea1da0651d82716bc82972
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2762298
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2021-05-13 22:49:09 +00:00
Daniel Verkamp
a8423d73c3 devices: pass MMIO and IO bus to PciRoot::new()
This will be used to dynamically add and remove bus ranges when the PCI
command register is updated to enable/disable memory and IO decode.

BUG=b:174705596
TEST=cargo test -p devices

Change-Id: I6bb175e0628bf598d049562700e2f55a2a62df59
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2689081
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Jeznach <tjeznach@chromium.org>
Reviewed-by: Tomasz Jeznach <tjeznach@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2021-05-12 00:09:37 +00:00
Zach Reizner
dc74848b7c arch: Simplify build_vm by removing closure parameters
These parameters multiplied the number of type arguments to build_vm
unnecessarily and complicated the thread of execution in the programmers
head. Closures also complicate the borrow rules, making things much
harder to change.

This change uses the results of the closures (e.g. PCI devices, IRQ
chips) as parameters instead. The rest of this change follows naturally
from pulling on that thread until tests pass.

As a result of the removal of several type arguments, the code size was
reduced by ~100KiB on a 5MiB build.

BUG=b:185170486
TEST=./test_all

Change-Id: I6bcc5eb1b1f3031d4328bb4a81ddef618d04767b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2829136
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Reviewed-by: Tomasz Jeznach <tjeznach@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-05-06 16:31:49 +00:00
Andrew Scull
74bf69a567 aarch64: pass arguments to protected VM firmware
A protected VM will have the PC set to the start of the firmware at boot
by EL2. Pass the address of the next stage to the firmware in x1.

BUG=b:175729255
TEST=cargo test

Change-Id: I8d5a2aae4e2790914e12358f52511c8f18b3d769
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2843605
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Andrew Scull <ascull@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2021-04-23 22:48:40 +00:00
Andrew Scull
0131dd3579 aarch64: refactor register initialization
Use single purpose, immutable variables with meaningful names.

BUG=b:163789172
TEST=cargo test

Change-Id: Icc90918f6154bef8ab93358ba0651fb0ed7b041a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2843604
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Auto-Submit: Andrew Scull <ascull@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
2021-04-21 23:19:49 +00:00
Zach Reizner
a90649ab7c arch: move GuestMemory creation to common of linux module
This requires exporting the memory layout from the arch crates, but it
does simplify the bloated build_vm interface a bit. It also will allow
for more fine-grained control the backing memory of GuestMemory.

TEST=test_all
BUG=b:183988204

Change-Id: Ie76755198d2fdc2a41bd538650939d6550686b88
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2809434
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Zach Reizner <zachr@chromium.org>
2021-04-09 21:06:12 +00:00
Daniel Verkamp
0a91c96437 arch: rewrite FDT writer in native rust
This removes some unsafe code, improving the interface so that it cannot
be misused (e.g. previously, different Vec<u8> instances could be passed
to fdt functions that did not validate the contents).

The new implementation also adds some extra error checking to catch
invalid string values in all API entry points that accept strings, as
well as out-of-order node nesting that would result in DTB data that did
not conform to the spec.

BUG=b:175729255
TEST=cargo test -p arch
TEST=Boot Crostini on kevin (arm)
TEST=diff old and new dts from `dtc -I fs -O dts /proc/device-tree`

Change-Id: I567d562ed1b447aa0d282b68c71758edea13ffc0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2713569
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-03-03 01:29:15 +00:00
Andrew Walbran
413f854564 Enable KVM_CAP_ARM_PROTECTED_VM when --protected-vm is passed.
- Add an address space region for the protected KVM firmware.
- Query firmware size, mmap something that size and create a memslot.

BUG=b:163789172
TEST=cargo test

Change-Id: I054cf5d763c980d073c17bce70e85a781816b64d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2623942
Auto-Submit: Andrew Walbran <qwandor@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Andrew Walbran <qwandor@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2021-03-02 19:04:43 +00:00
Ram Muthiah
15e8a97d6d Reland "Relocate FDT to beginning of RAM"
Only relocating FDT to the front of RAM if booting arm guest in BIOS mode.
ARCVM depends on the kernel and fdt placement at 0x80080000 and the end
of RAM respectively to boot correctly.

BUG=b:177926450
BUG=b:178427080
TEST=local boot of android virtual device on ARM host

Change-Id: I4c23bd2db63532ab3cc478627818322641b895bd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2686590
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Ram Muthiah <rammuthiah@google.com>
Commit-Queue: Ram Muthiah <rammuthiah@google.com>
Auto-Submit: Ram Muthiah <rammuthiah@google.com>
Reviewed-by: Alistair Delva <adelva@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-02-11 17:50:54 +00:00
Gurchetan Singh
293913c01a rutabaga_gfx: rutabaga_gralloc: a shimmering beacon of hope
rutabaga_gralloc is a cross-platform, Rust-based buffer
manager.

The rationale for this change is:

1) For the {cross-domain, wayland} context type, we need to
have a good story for the crucial "wl-dmabuf" feature.  As
minigbm has been thoroughly tested on ChromeOS and currently
powers the "wl-dmabuf" feature, it only makes sense for us to
have a path to minigbm for the cross-domain prototype.  This
will be used by Sommelier.

2) While minigbm allocation works well on Chromebooks, it is
not sufficient for cross-platform purposes.  For their Virtual
Graphics Interface (VGI) initiative, Android graphics
virtualization experts have expressed their desire for a Vulkan
based allocator.  This will to go alongside cros_gralloc in
minigbm, which is considered by many to be the ""world's
premiere gralloc implementation".

3) Android graphics virtualization experts have expressed their
desire for vkMapMemory(..) to be used when crosvm is in
multi-process mode.  Currently, only dma-buf mmap() is supported
for zero-copy blobs in multi-process mode.  dma-buf mmap() is not
guaranteed to work on Nvidia (a "must have" for Cuttlefish) or
any other driver for that matter (we *make* it work for ChromeOS).
Possibly only solution: vkMapMemory ;-)

With these goals in mind, here's a summary of the revelant changes:

* Renamed the {gpu_allocator.rs, GpuMemoryAllocator trait} to be
  {gralloc.rs, Gralloc trait}.

* Moved all GPU allocation out of the resources crate and into
  the rutabaga_gfx crate.  This will allow the resources crate to
  be focused on managing resources for virtual machines.

* Moved the gpu_buffer crate into the gralloc module in the
  rutabaga_gfx crate.  The same functionality is now under
  "minigbm.rs", "minigbm_bindings.rs" and "rendernode.rs"

* Added an optional dependency on vulkano.rs.  vulkano.rs is a safe
  Rust wrapper around the Vulkan api [a].  It's emphasis on type
  safety makes a good fit for crosvm, though there are other high
  quality crates out there (gfx-rs, ash.rs).  Though development
  has slowed down, it should satisfy goals (2) and (3) quite easily.

* Added a system_gralloc implementation based on memfd.  This can be
  used when minigbm or Vulkano features are not used, to replicate the
  highly useful "wl-shm" feature in Sommelier.  Astute observers will
  note this can also enable seamless Wayland windowing without GPU
  features for Android too.  Some minor changes to the base crate were
  needed.

* Cut down on the amount of DrmFormats to the subset needed by
  Sommelier and cros_gralloc.

* Moved checked arithmetic into it's own file.

* Internally renamed to "wl-dmabuf" feature to be the "minigbm"
  feature.  This is because "wl-dmabuf" has a dependency on minigbm.

* Small rutabaga_gfx cleanups

[a] https://github.com/vulkano-rs/vulkano/blob/master/DESIGN.md

BUG=b:146066070, b:173630595, b:150239451
TEST=launch virtual machine with 2D mode
TEST=launch virtual machine with 3D mode
TEST=run sommelier with "wl-dmabuf" and "wl-shm"

Change-Id: I693a39cef64cd98e56d843d3c60caa7983d4d6e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2626487
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
2021-02-04 02:35:52 +00:00
Ryo Hashimoto
18d5733e15 Revert "Relocate FDT to beginning of RAM"
This reverts commit 890ae4cdb8.

Reason for revert: breaks arc.Boot.vm on kukui-arc-r

Original change's description:
> Relocate FDT to beginning of RAM
>
> This change serves two purposes. The FDT needs to be in a location
> accessible by 32 bit devices. Secondly the FDT's location was dependent
> on the amount of RAM allocated to the guest prior to this change which
> made passing it's location to the BIOS challenging.
>
> By moving the FDT to a fixed location within the 32 bit accesible memory
> boundary, these problems have been fixed.
>
> BUG=b:177926450
> TEST=local boot of android virtual device on ARM host
> Change-Id: I89e481f66ddc8ffcf0e86f00fbfa92d407c48736
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2641126
> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
> Tested-by: Ram Muthiah <rammuthiah@google.com>
> Commit-Queue: Ram Muthiah <rammuthiah@google.com>

Bug: b:177926450
Bug: b:178427080
Change-Id: Iba629542d024fff84721ff434655c0c322020db3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2656215
Reviewed-by: Ryo Hashimoto <hashimoto@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Ryo Hashimoto <hashimoto@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Ryo Hashimoto <hashimoto@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-01-29 00:56:47 +00:00
Ram Muthiah
890ae4cdb8 Relocate FDT to beginning of RAM
This change serves two purposes. The FDT needs to be in a location
accessible by 32 bit devices. Secondly the FDT's location was dependent
on the amount of RAM allocated to the guest prior to this change which
made passing it's location to the BIOS challenging.

By moving the FDT to a fixed location within the 32 bit accesible memory
boundary, these problems have been fixed.

BUG=b:177926450
TEST=local boot of android virtual device on ARM host
Change-Id: I89e481f66ddc8ffcf0e86f00fbfa92d407c48736
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2641126
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Ram Muthiah <rammuthiah@google.com>
Commit-Queue: Ram Muthiah <rammuthiah@google.com>
2021-01-23 02:22:28 +00:00
Ram Muthiah
a81bedbf92 AARCH64 bios support
BUG=b:171352650
TEST=boot test on arm cuttlefish on rockpi host with bootloader from
https://r.android.com/1504313

Change-Id: I45841178c50207ad31ce597186643a0ae1ed52ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2627445
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Ram Muthiah <rammuthiah@google.com>
Auto-Submit: Ram Muthiah <rammuthiah@google.com>
2021-01-16 00:56:32 +00:00
Keiichi Watanabe
22f808ff13 aarch64: fdt: Fix format of properties containing multiple values
When a FDT's property contains multiple values, the property must be a byte
array which uses the null character ('\0') as the delimiter, but we didn't
so.
This CL fixes the format so the guest kernel can parse PSCI versions
properly.

BUG=b:174224484
TEST=arc.Reboot.vm on kukui-arc-r

Change-Id: I61a983251cdbe8c021f5999cbf5efd026bbc0c27
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2567837
Tested-by: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Marc Zyngier <mzyngier@google.com>
Reviewed-by: Lepton Wu <lepton@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
2020-12-03 07:27:25 +00:00