Commit graph

59 commits

Author SHA1 Message Date
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
Tomasz Jeznach
f622e504d3 crosvm/kvm: enable runtime detection of IOAPIC pins
Enable support for runtime verification of number
of irqchip kernel emulated inputs, up to 120 pins.

KVM implementation supporting extended input pins shall
report KVM_CHECK_EXTENSION/KVM_CAP_IOAPIC_NUM_PINS value.

BUG=b:179648314
TEST=On systems with 24/120 pin IOAPIC kvm emulation.

Change-Id: I80063216310e427d664e3eaca3aba27e8a972cde
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2893366
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Jeznach <tjeznach@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-05-20 00:37:59 +00:00
Noah Gold
9af97d72fa [base] update/clean mmap interfaces.
This CL addresses some minor issues with the existing interface:
1. from_descriptor is too generic for some platforms that require
   special handling for file/File backed mappings.
2. Nearly all call sites pass either File or SharedMemory. Now
   we just have from_ methods for those types to preserve type
   information.
3. Other platforms require additional fields in MemoryMapping, so a
   tuple struct no longer makes sense.
4. The mmap syscall error message was misleading as we use it for more
   than just the mmap syscall.

BUG=None
TEST=builds

Change-Id: I74c41bad52bb81880a11231cd18f47e233548a24
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2815614
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
2021-04-15 02:10:35 +00:00
Dylan Reid
1cb1d01f79 vm_memory: Allow GuestMemory to be backed by multiple FDs
Allowing each region to have a separate backing FD will make it possible
to build GuestMemory from the vhost `SET_MEM_TABLE` message that
transmits the memory regions for virtio queues in vhost-user devices.

Change-Id: I6f9bc6136915da9d873ea896823e3b8f426ca69d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2795282
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
2021-04-06 04:02:26 +00:00
Zach Reizner
d49bcdb081 replace all usage of MsgOnSocket derives
Replaced all usage of MsgSocket with Tube.

BUG=b:176847362
TEST=run_tests

Change-Id: Ie647f79926bc0414c125a776eafe7f60c071bab2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2710324
Auto-Submit: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2021-04-02 15:40:41 +00:00
Zach Reizner
d09392e37e uprev rust-toolchain and fix clippy warnings
Some judgement calls were made about unnecessary wrapping. Usually they
would get resolved by removing the wrapping or returning a convenient
error, but the ones that returned results for consistency with other
functions were added to the allow list.

The error handling in the usb code had a lot of unit error types which
is now a clippy lint. This was resolved by either removing the result
entirely or returning a convenient error.

The field_reassign_with_default lint is faulty and was added to the list
of supressions. This affected virtio-wayland code.

BUG=b:179277332
TEST=cargo clippy with rustc 1.50+

Change-Id: Ie812cdeaf7c42f4f2b47b1dc87f05a7c87a60f8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2757510
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Auto-Submit: Zach Reizner <zachr@chromium.org>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2021-03-22 21:41:07 +00:00
Chuanxiao Dong
e2d00176e5 VFIO: set user memory region with page size aligned size
For some PCI device, its MMIO bar size may not be page size aligned.
When setting user memory region for such bar with not aligned size, KVM
will report failure back and failed to map that bar. As current crosvm
can continue run with this failure, the performance will be hurt as each
time when guest is accessing this bar, it will trap to hypervisor.

To resolve this, extend the size to be page size aligned when setting
user memory region in KVM and do DMA map. This should be safe to extend
because the mmap actually rounds up the mmap size to be page aligned.

BUG=None
TEST=boot vm with a passthrough device whose bar has unaligned size

Change-Id: Ic816984ec503edf7f12da4893b78d996ebf93976
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2717448
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
2021-03-19 18:51:32 +00:00
Tomasz Jeznach
eb1114ced7 crosvm-direct: support for variable IOAPIC irqs.
More recent Intel IO-APICs can support more than 24 interrupt
lines. This change enables variable size of IO-APIC lines for
user level IO-APIC emulation code (split-irqchip).

Reported version and supported IO-APIC registes matching ICH10
implementation of IO-APIC device.

BUG=b:181795297
TEST=boot and allocate irq from upper range.

Change-Id: I56480befb39c4c268266f04e4a93105402248772
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2733579
Tested-by: Tomasz Jeznach <tjeznach@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Jeznach <tjeznach@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-03-18 22:02:54 +00:00
Christian Blichmann
33d5677804 Make KVM path configurable
Most users will want to keep the default `/dev/kvm` path. However, in
certain environments, namely Borg, the KVM device node may be located
elsewhere.

This is the first of a set of related changes that will make hard-coded
device paths configurable.

BUG=None
TEST=./ci/builder --vm ./run_tests

Change-Id: I6087879c535be3779e20eff1f8fb5080f80cf020
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2736520
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
2021-03-09 07:08:23 +00:00
Andrew Walbran
ce10855e91 enable_raw_capability and kvm_enable_cap are unsafe
The args may be interpreted as pointers for some capabilities, so the
caller must ensure that any such pointers are allocated appropriately.

BUG=b:181564686
TEST=cargo test

Change-Id: I244f4d9417e588a6be5681f4718bb9ad7b262c3e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2726709
Auto-Submit: Andrew Walbran <qwandor@google.com>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2021-03-03 02:20:49 +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
Andrew Walbran
9cfdbd9cc0 Fix clippy errors and some warnings.
TEST=cargo test

Change-Id: I91afe78141c717a592eec5ec77acac2a500ba163
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2623941
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Andrew Walbran <qwandor@google.com>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Andrew Walbran <qwandor@google.com>
2021-01-18 15:50:25 +00:00
Keiichi Watanabe
eefe7fb19e devices: virtio: fs: DAX based shared memory support
Support virtio-fs's DAX (direct memory access) operation which allows the guest
to directly access file pages.
Specifically, FUSE_SETUP_MAPPING and FUSE_REMOVE_MAPPING operations are
supported.

This option can be used by specifing `dax` option when mount a file system in
the guest.

The DAX optoin improved file I/O performance in most cases.
In Fio tests, both of read and write score were improved by 1.3-14x depending on
test cases.
In Blogbench tests, which create many small files, DAX improved the write score
by 1.5x while the read score was reduced to ~25% (20391 -> 4593).

Here is an excerpt of results:
Fio
* seq_read:     10.2x (143528 -> 1464911)
* seq_write:     3.3x (61253 -> 896791)
* rand_read:    11.6x (138753 -> 1612739)
* rand_write:   14.6x (61253 -> 896791)
* surfing_read:  1.3x (98473 -> 127907)
* surfing_write: 1.3x (83309 -> 108089)

Blogbench
* read:  0.23x (20391 -> 4593)
* write: 1.50x (248 -> 373)

BUG=b:147341783
TEST=Run vm.{Blogbench, Fio} with CL:2291856

Change-Id: I4a47c601412ed32d926de6304337e1594252d258
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2108315
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2020-12-16 06:03:54 +00:00
Camilo Jacomet
a9bfcb485c Fixed Clippy or_fun_call warnings in devices/
BUG=chromium:1111728
TEST=cargo clippy -- -A clippy::all -W clippy::or_fun_call passes under
devices directory

Change-Id: I4c9e079df98b50e55a5489cd925fb7b621caf4dd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2587191
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: Camilo Jacomet <cjacomet@google.com>
Commit-Queue: Camilo Jacomet <cjacomet@google.com>
2020-12-15 05:15:00 +00:00
Keiichi Watanabe
f12c5ea7b4 aarch64: Check the current PSCI version when creating device tree on ARM
Call KVM_REG_ARM_PSCI_VERSION to see the PSCI version and use the value
when creating a device-tree node.
Also stop setting PSCI constants which are ignored by kernel.

BUG=chromium:1141902
TEST=run crosvm on krane and check /proc/device-tree/psci/compatible

Change-Id: I4b8ed7620d7f0e2205b84b5f08cfaa5ae8d94339
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2529289
Tested-by: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-11-17 08:34:55 +00:00
Keiichi Watanabe
bb318f8bf8 kvm_sys: Update bindings.rs
Regenerate bindings.rs for kvm and add comments about how to generate it.
As a result, manually-added hack related to zero-sized arrays' alignment
was removed, as the bug had been fixed:
https://github.com/rust-lang/rust-bindgen/issues/684

BUG=none
TEST=build_test

Change-Id: I257975ce3cd4667b39381ddafd8b08d9e91de655
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2532546
Tested-by: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
2020-11-17 08:34:52 +00:00
Keiichi Watanabe
23f9471bc3 gdb: Support single-step execution and HW breakpoints for x86_64
Support single-step execution command and hardware-associated breakpoints
command in GDB.

BUG=chromium:1141812
TEST=Set breakpoint with `hbreak start_kernel` and show backtrace

Change-Id: I762565ffc9fb055dd3891e172799c3fc87f7604c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2494327
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: Keiichi Watanabe <keiichiw@chromium.org>
Auto-Submit: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-11-12 08:48:01 +00:00
Steven Richman
ce33acefff hypervisor: add vcpu methods: id, nmi, window off
New Vcpu methods get the vcpu ID for a vcpu and the current thread.
A new VcpuX86_64 method injects NMIs. request_interrupt_window now
takes a bool parameter, so the request window flag can be turned off.

These methods are to support the userspace irqchip. The thread-local
vcpu ID will help route MMIO requests to the appropriate APIC. Clearing
the interrupt window request is needed because KVM won't clear the
flag if the kernel APIC is disabled.

BUG=chromium:1077058
TEST=boots with KVM kernel irqchip, KVM split irqchip

Change-Id: I2a08988e71663f0d2a63985f3239568c7a9befea
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2419673
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-11-05 03:24:36 +00:00
Colin Downs-Razouk
e5774c3a8b hypervisor: userspace ioevent changes
For hypervisors that do not handle ioevents in-kernel, we need to make
some minor adjustments to the hypervisor abstraction api:
  - register/unregister_ioevents needs &mut self instead of &self
  - move handle_io_events from Vcpu to Vm, because the ioevents are
  registered on the Vm not the Vcpu.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: Iddd61502a716143e117c454e64a9c9bd2578148d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2405299
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-11-03 21:15:11 +00:00
Michael Hoyle
6a0960a4a7 base: First steps towards universal RawDescriptor
This involves removing RawFd trait implementations from
Event and Timer, and resolving the echoing dependencies from
there.

Ultimately, this results mostly in changes across ioctl
(new thin layer in base), kvm, msg_on_socket, and a few other
areas. As usual, most changes are negligible.

BUG=b:162363783
TEST=./build_test

Change-Id: I47dec94666bc3430fed627b4767919c9cb4fce6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2462330
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Michael Hoyle <mikehoyle@google.com>
2020-10-31 07:12:34 +00:00
Colin Downs-Razouk
b428fa1c15 hypervisor: allow shared MemSlotOrd
Move MemSlotOrd out of the kvm hypervisor implementation so other
implementations can share the same struct.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I9d992699defab53e32517bb40e29192e31c45983
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2405301
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-10-28 05:37:33 +00:00
Michael Hoyle
3038e40a6b base: Refactor mmap to use builder pattern.
BUG=b:165423256
TEST=./build_test

Change-Id: Ia67c1a7fe29b66c9cab38476eecde8c25a55617b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2442569
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Michael Hoyle <mikehoyle@google.com>
2020-10-16 07:15:24 +00:00
Zach Reizner
304e731cdb make hypervisor traits object safe
This allows code to create references to traits such as `&dyn Vcpu` for
`&dyn IrqChip`. This also allows keeping such traits inside of opaque
`Box` like wrappers.

To achieve this, trait methods referencing `Self` have an additonal
`where` clause that restricts them to sized types. Associated types are
removed and replaced with their trait equivalents (as parameters) or an
opaque Box (as return values).

To work around certain cases where a concrete type is needed, such as
for KVM based `IrqChip` impls, the `downcast_rs` trait is used to allow
`dynamic_cast` style casting.

The binary size impact of this change is small. For comparison, here is
the size change with this commit:

armv7a  -0.49% (-9 kiB)
aarch64 -0.17% (-3 kiB)
x86_64  +1.77% (+40 KiB)

BUG=None
TEST=build_test

Cq-Depend: chromium:2466660
Change-Id: I1a408734832d2971ea2979c1bc64b2ffe051b02d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2439298
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
2020-10-15 14:31:20 +00:00
Michael Hoyle
685316f0bd base: EventFd -> Event renaming
Note the CL size is large entirely due to the rename,
the changes are mostly negligible.

Also making a few small additional changes in sys_util
areas that don't need much attention in base. This includes
typedefing and adding specific imports for areas that don't
require significant interface changes.

BUG=b:162363783
TEST=./build_test

Change-Id: I4a2c9c4cdce7565806ed338e241c6b8c82c855c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2415180
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Michael Hoyle <mikehoyle@google.com>
2020-10-06 13:50:09 +00:00
Zach Reizner
2c770e6f79 hypervisor: replace RunnableVcpu with VcpuRunHandle
This removes RunnableVcpu as an associated type on Vcpu traits, instead
using the new VcpuRunHandle type to enforce the same guarantees. This
simplifies implementations of Vcpu becuase only one impl is needed with
all the methods provided.

The other advantage to removing RunnableVcpu was that a Vcpu could be
try_cloned, defeating the purpose of consuming the Vcpu in the first
place with to_runnable. The type system and ownership model gave a false
sense of compile time safety. The usage of VcpuRunHandle is more
consistent with the fact that it is a runtime check that the programmer
is doing the safe thing. It's also consistent with all hypervisor impls.

BUG=None
TEST=./build_test

Change-Id: I14bbc5d939744cb92fb9cb3152edb82603cadd11
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2442096
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
2020-10-03 02:59:54 +00:00
Michael Hoyle
a7e38ab421 base: Add shared memory layer to base.
This is very largely boilerplate, but changes some interfaces
to accept different parameters, esp using Descriptor.

BUG=b:162363783
TEST=./build_test

Change-Id: I81b513c0de9448c2de505fe5d3a61a29f4eccb78
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2342591
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Michael Hoyle <mikehoyle@google.com>
2020-09-30 19:44:40 +00:00
Steven Richman
f32d0b48fd Use hypervisor abstraction to run VMs
Calls to the kvm crate have been replaced by calls to hypervisor and
devices::irqchip.  Plugin is unchanged and still KVM-specific.

x86 creates and configures vcpus on the vcpu thread instead of the main
thread.

Code that still depends on kvm or kvm_sys:

  - crosvm_plugin, plugin, and protos use kvm and kvm_sys if built with
    the plugin feature

  - devices::VfioGroup does a kvm_sys ioctl

  - devices::irqchip and hypervisor use kvm_sys.  hypervisor uses
    kvm::Cap and kvm::CpuId internally for now.

BUG=chromium:1077058
TEST=runs es2gears_wayland on kukui
TEST=runs evince on amd64-generic with kernel and split irqchip
TEST=build_test excluding tpm2
TEST=cargo check --features plugin

Change-Id: I0068e34da6a5ff0079b1237be1958933bf12ffe4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2307711
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2020-08-11 04:51:34 +00:00
Michael Hoyle
6b19695c81 Add "base" crate and transition crosvm usages to it from sys_util
For now, this crate simply re-exports all of sys_util, but it will
be updated to provide new interfaces when needed. This is the
first step to making crosvm not directly depend on sys_util, so
that we can make the interface changes we need without fear of
negatively affecting (i.e. completely breaking) other usages
within chromeos.

BUG=b:162363783
TEST=./build_test

Change-Id: I7d0aa3d8a1f66af1c7fee8fd649723ef17027150
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2325168
Tested-by: Michael Hoyle <mikehoyle@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Michael Hoyle <mikehoyle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2020-08-06 18:19:44 +00:00
Steven Richman
24a89faeb3 Fix hypervisor and devices test compilation on ARM
Run unit tests for the old ioapic only on x86.  Fixes use statements in
hypervisor tests.  Run the hypervisor vcpu enable_feature test only on
x86, like in the kvm crate.

TEST=cargo test --no-run --target=aarch64-cros-linux-gnu
TEST=cargo test --no-run --target=aarch64-cros-linux-gnu -p devices
TEST=cargo test --no-run --target=aarch64-cros-linux-gnu -p hypervisor
BUG=1112839

Change-Id: Ie8dc152de6acd37b93e394642208a86a746c9308
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2337151
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: Steven Richman <srichman@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2020-08-06 04:28:37 +00:00
Dylan Reid
ec058d6c46 vm_memory: A crate to hold vm-specific memory objects
Move GuestAddress and GuestMemory to a new crate for VM memory. This
will make separating sys_util and crosvm independent making it easier
to use sys_util functions outside of crosvm.

Change-Id: I12e14948ea85754dfa6267b3a3fb32b77ef6796e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2311251
Auto-Submit: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Tested-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2020-07-24 21:42:34 +00:00
Daniel Verkamp
05b13d0eff kvm: return removed memory region
When a memory region is added to the guest, ownership is passed to the
Vm object.  However, it was not possible to get the memory region back
when removing it from the Vm.  Update the return type of
remove_memory_region so that the original boxed memory region can be
returned to the caller.

BUG=None
TEST=cargo test -p kvm

Change-Id: Ie39a57e0c037ed25a603865cb3dce1af1478e143
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2300840
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Tomasz Jeznach <tjeznach@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
2020-07-17 11:16:42 +00:00
Colin Downs-Razouk
fe368791f8 devices: irqchip: KvmSplitIrqChip impl
Implementation of a KVM split irqchip. KVM's "split irqchip"
functionality only works for x86/x86_64 so this is implemented in the
kvm/x86_64.rs sub-module. The chip has a userspace Pic, Ioapic, and Pit.

This change necessitated a couple modifications to the irqchip traits:
  - The create_pit function has been removed, and it's now implied that
  the creation of the irqchip handles the creation of the pit.
  - A finalize_devices function has been added. This function will need
  to be called on the irqchip after all devices have been setup. The
  purpose of finalize_devices is to allow the irqchip to register any
  userspace devices with the io_bus or mmio_bus, and for the irqchip to
  supply any necessary EventFds to these devices.
  - A service_irq_event function has been added. This function works a
  lot like the service_irq function, except it's specifically designed
  to work the same way an IRQFD works: it first asserts then immediately
  deasserts the line. If a resamplefd is associated with the irq line,
  the deassert doesn't happen immediately, but happens when an EOI
  occurs for a vector associated with the line. The service_irq function
  will still exist for unittests.
  - A process_delayed_irq_events function has been added. There
  is a case where a deadlock can occur if the main thread blocks on
  locking the ioapic in order to service an irq event, while a vcpu
  thread holding the ioapic lock waits on the main thread to process the
  addition of a MSI route. So the irqchip delays the servicing of irq
  events if it finds a locked ioapic, and the
  process_delayed_irq_events function should be called regularly
  by the main thread in order to re-try servicing any delayed irq events.

Bug: chromium:1077058
Test: split irqchip runs all available x86-specific irqchip tests.
  Also added some tests specific for the split irqchip. Ran these tests
  and cargo test -p hypervisor -p devices

Change-Id: I14866645b86b3bf318440051069ff165e2cf9d88
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2290192
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-07-16 00:45:31 +00:00
Andrew Walbran
e43468f066 sys_util: Add IoctlNr type, as different platforms use different types.
In particular, Android targets use c_int whereas normal Linux targets
use c_ulong. An ioctl number should always fit in 16 bits, so casting
from c_uint (as bindgen uses for the constants) to either other type
should be safe.

BUG=b:158290206
TEST=cargo test

Change-Id: I3d808ba9a5588a75c029a299e3609d97e328e3cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2288229
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Andrew Walbran <qwandor@google.com>
Commit-Queue: Andrew Walbran <qwandor@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2020-07-15 19:10:23 +00:00
Steven Richman
2be73da5d4 hypervisor: add ARM vcpu functions
Add VcpuAArch64::set_one_reg, init, and init_pmu.  init combines
arm_preferred_target and arm_vcpu_init from the kvm crate.

BUG=chromium:1077058
TEST=cargo test -p hypervisor, ARM build

Change-Id: I5005d568c2337da3b9a96ec05ca9865c6ddbab74
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247369
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-07-09 22:31:45 +00:00
Steven Richman
c3073ee6b1 hypervisor: add clone methods and cpuid flags
Move try_clone from Kvm to Hypervisor, and add try_clone to Vcpu.
Include a flags field in CpuIdEntry, since it's used by KVM_SET_CPUID2.
Tighten the bounds on Vcpu::Runnable so it derefs to the same type as
Vcpu.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I451f9156210eae342122f97fedfad630b89a7645
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2274837
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Udam Saini <udam@google.com>
2020-07-09 22:31:44 +00:00
Steven Richman
30010eb874 hypervisor: add pvclock_ctrl, sigmask, enable cap
These functions are mostly unchanged from the kvm crate.  Error code
checks were tweaked for consistency.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: Ib4b30281eed877efcb4b55332f1f588213b98918
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247370
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-07-09 22:31:43 +00:00
Steven Richman
44b82cbc03 hypervisor: add msr, xcr, and vcpu cpuid funcs
set_msrs, which had used std::alloc in x86_64/src/regs.rs, now uses
vec_with_array_field to allocate the msr struct like get_msrs.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I288bb18cc9e9ebf3e52b4ac46e36d03013b620ea
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2246654
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-07-09 22:31:42 +00:00
Steven Richman
2d3476630f hypervisor: add x86 register functions
BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I5ae215377a322b4520aca4207f4313ea9b7e4217
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247368
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-07-09 22:31:40 +00:00
Colin Downs-Razouk
963e2ddcf2 devices: irqchip: lapic state
Added get/set_lapic_state functions to KvmKernelIrqChip and KvmVcpu.
Added tests for the KvmKernelIrqChip.

BUG=chromium:1077058
TEST=added associated tests for get/set_lapic_state

Change-Id: I0f1cebd9db370b5453a951f7827de511399cddf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2260929
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-07-08 01:36:24 +00:00
Colin Downs-Razouk
0ae7c7c765 devices: irqchip: mp state
Added an emum for MPState, and functions to translate between MPState
and kvm_mp_state. Added get_mp_state and set_mp_state functions to KvmVm
and the IrqChip interface.

BUG=chromium:1077058
TEST=added associated tests for get/set_mp_state

Change-Id: I0825f81b1b4d85884690606d691e8b88e8306ae1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2261293
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-07-07 19:49:41 +00:00
Steven Richman
64a70c8003 hypervisor: add vcpu to_runnable, interrupts, I/O
Add methods for making RunnableVcpus, requesting vmexits on a vcpu,
injecting interrupts, and setting I/O data.  This code is unchanged
from the kvm crate.

A new Vcpu::handle_io_events method delivers pending IO events.  This is
for hypervisors that handle IO events in userspace.  On KVM it's a
no-op.

Delete integration tests, which were for showing how the hypervisor
traits fit together.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: Ie9eb6dd38f168031bc0ae1b3c44638e8930284a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247367
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2020-07-02 10:04:08 +00:00
Steven Richman
583651a52a hypervisor: add ioevent, dirty log, TSS, ident map
These functions are mostly unchanged from the kvm crate.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I819e34b2d7895b10658ca7d8870b64435220ea9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202847
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2020-06-30 23:03:08 +00:00
Steven Richman
41e4d7153a hypervisor: add MemSlot type alias
Use a MemSlot type alias for memory region slots instead of u32, for
better type checking and self-documentation.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I0ee790f2eba432a6569c720a216d5bd5a66b07ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2253336
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-06-30 23:03:07 +00:00
Colin Downs-Razouk
b865569893 devices: irqchip: updated ioapic implementation
Modified ioapic implementation for the hypervisor abstraction layer.
This implementation has the same functionality and tests as the existing
one, but accepts a vec of resample_events instead of a GsiRelay. It also
takes a list of irqfds instead of creating them itself.

This will be used by the KvmSplitIrqChip, and will eventualy be modified
again to support an ApicBus when we want to support a UserspaceIrqChip.

Bug: chromium:1077058
Test: cargo test -p hypervisor -p devices
Change-Id: I306724266511a7975a25a34955651ea6f53e1c4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2255303
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-06-25 18:15:30 +00:00
Colin Downs-Razouk
6583fa8d6a devices: irqchip: updated pic implementation
Copied and slightly modified pic implementation in the irqchip
submodule. It uses the PicState defined in the hypervisor crate and has
a register_resample_events function instead of a register_relay
function.

Some function signatures have been slightly changed to take &self and
&mut self where before they would take a PicState. This was done so
that the Pic struct itself would be the only object that needs access
to the resample_events rather than each PicState needing it's own copy
or clone.

This will later be used by the the KvmSplitIrqChip.

Test: cargo test -p devices -p hypervisor
Bug: chromium:1077058
Change-Id: Ia15ea1800b4339d3ad38d88d8ec6ace8ca5ea67a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2255302
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-06-25 18:15:24 +00:00
Steven Richman
07a0706829 hypervisor: add vcpu new and run
Vcpus are created on the Vm object, instead of with Vcpu::new.  Vm keeps
a copy of the Hypervisor to support create_vcpu.  Otherwise the methods
are the same as the kvm crate.

BUG=chromium:1077058
TEST=cargo test

Change-Id: I6fbd0e5fb5d81d4362a259e85eb392d8edbfff1f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2247366
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2020-06-25 14:38:18 +00:00
Steven Richman
c328f65054 hypervisor: add Vm capabilities
Vm capability checking has been split into two functions: one for a
small set of hypervisor-agnostic caps, and one for hypervisor-specific
u32 constants to support the plugin.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I17fb6804f0fa7114f39c5016da430afa75179919
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202848
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2020-06-24 19:59:00 +00:00
Colin Downs-Razouk
6364823093 devices: irqchip: finish KvmKernelIrqchip impl
Implemented the remaining IrqChip trait functions for the
KvmKernelIrqChip, including register/unregister_irq_event and irq
routing functions.

Added some irqchip tests for setting irq routes and servicing irq lines.
Also added tests for registering irq events.

BUG=chromium:1077058
TEST=cargo test -p devices -p hypervisor

Change-Id: Ia04c927b663ebdcacc88bc61d746077aa5b02514
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2246648
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2020-06-17 23:49:31 +00:00
Steven Richman
2bb21491f9 hypervisor: add create_device
Add Vm::create_device to create kernel virtual devices, currently for
VFIO, ARM VGIC v2, and ARM VGIC v3.

BUG=chromium:1077058
TEST=cargo test -p hypervisor

Change-Id: I83c9c367c2b2674c3faaae9e4ae09919adfe34b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2202846
Reviewed-by: Udam Saini <udam@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Steven Richman <srichman@google.com>
2020-06-15 04:44:36 +00:00
Gurchetan Singh
173fe62df2 kvm: use MappedRegion trait
- Reduces code duplication between MMIO and mmap arenas
- Makes adding future types easier
- Makes upcoming deprecation of kvm crate easier
- Use BTreeMap instead of HashMap since it's more efficient

BUG=chromium:924405
TEST=compile and test

Change-Id: I520abed0926489e64aac046e0dc0cfeb72fae7b2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2216446
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Steven Richman <srichman@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Gurchetan Singh <gurchetansingh@chromium.org>
2020-06-12 05:00:37 +00:00