Commit graph

1081 commits

Author SHA1 Message Date
Daniel Verkamp
d78d1e48e9 devices: refer to vmm_vhost via path dependency
Remove the crates.io patch from the top-level Cargo.toml and make
devices point to the third_party/vmm_vhost location directly, since
vmm_vhost has been forked in crosvm and cannot be replaced by the
crates.io version anymore.

Incidentally fixes this warning from cargo:

  warning: patch for `vmm_vhost` uses the features mechanism.
  default-features and features will not take effect because the
  patch dependency does not support this mechanism

BUG=b:205511695
TEST=cargo build
TEST=tools/presubmit --quick

Change-Id: Id29578b57f7540805cf85a3db9a09542b7b84825
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3299125
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-11-24 19:12:47 +00:00
Xiong Zhang
46471a03e1 Devices: Use mmap for direct mmio r/w
write_all_at()/read_exact_at() fail to access pmc_mux through /dev/mem.
while devmem2 tool success to access them through /dev/mem, so this
commit reference devmem2 implementation and use mmap to access direct
mmio.

BUG=b:199354528
TEST=Apply https://chromium-review.googlesource.com/c/chromiumos/platform/initramfs/+/3194150
, then verify platform device (like typec) function in ManaTEE CrOS

Change-Id: Id69c44444e2dc1ef6d40cb7b36febda38848d4f0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3259931
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-23 20:16:54 +00:00
Xiong Zhang
db749f37d6 vfio-pci: Emulate msix mask all bit
msix vector table is stored in mmio, its contents is saved and restored by pci bus
dirver during device PM. With vfio-pci device, msix is virtualized and guest pci
bus driver could maintain virtualized msix vector table. But vfio-pci device maybe
poweroff and the physical msix vector table will be lost, crosvm couldn't maintain
physical msix vector table during vfio-pci device suspend and resume.

kernel vfio-pci set msix message for each enabled vector which is specified in
VFIO_DEVICE_SET_IRQS() ioctl, this commit use this to restore physical msix vector
table during vfio-pci device's resume.

__pci_restore_msix_state() is called during device resume, this function set enable
and maskall bit first, then restore msix vetor table, finally set enable bit and
clear maskall bit.

so when enable and maskall bit is set, virtualized msix could be disabled. When enable
is set and maskall is cleared, vituallized msix will be enabled through
VFIO_DEVICE_SET_IRQS() ioctl, and kernel restores vfio-pci device's physical msix table.

BUG=b:185084350
TEST=verify vfio-pci device's suspend and resume function in vm, this device should have
msix capability like TBT controller.

Change-Id: I0a9e91874a9d1ec4b0bb9ff655b83a293daa6865
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3291758
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-23 19:10:13 +00:00
Xiong Zhang
48937f791d devices:pcie: Remove downstream device when guest poweroff root port
When guest poweroff root port, its downstream device could be destroyed
safely, so put the downstream device into
PciDevice->get_removed_children_devices(), then the downstream device
will be removed by pci root.

BUG=b:185084350
TEST=Boot a guest and hotplug vfio pci device repeatedly

Change-Id: Icb8104a2417832864d8dbe4947895028dabb46d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955577
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:15 +00:00
Xiong Zhang
55d5cfb659 devices:pci: Destroy the children devices at specific config write
When PCIE root port receives poweroff config register write, its
downstream devices could be removed. So get_removed_children_devices()
in PciDevice trait is used to get removed downstream children devices
pci address, then these children devices will be destroyed by pci root.

BUG=b:185084350
TEST=Boot a guest and hotplug vfio device repeatedly

Change-Id: Iea861459b3cbabd7a9daf639ed4b7ad181a81e01
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955576
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:14 +00:00
Xiong Zhang
4d3dfc9e44 devices:pci: Add destroy_device() into BusDevice and PciDevice trait
When vfio-pci device is hotplug out from guest machine, guest will
write some registers to notify the device could be removed from
crosvm, then destroy_device() is used to destroy vfio pci device
from pci_root.

BUG=b:185084350
TEST=Boot a guest and hotplug vfio device repeatedly

Change-Id: I40f72aeb2c12d885f5cf5c8189238bd1cea0c1e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955575
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:13 +00:00
Xiong Zhang
22dc9f80c4 devices:vfio-pci: Add close() for vfio pci device
When vfio pci device is hotplug out from the guest machine, vfio device's
resource should be returned to system, so vfio pci device's close() is
needed.

BUG=b:185084350
TEST=Boot a guest and hotplug vfio pci device repeatedly

Change-Id: I4686d737e93ae6db6078851e2add3275b9faaa84
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955574
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:12 +00:00
Xiong Zhang
436fa1b807 devices:vfio: Delete vfio group from kvm vfio device at group destroy
When vfio group is opened at the first time, group is added into
kvm vfio device. In order to support vfio device hotplug repeatedly,
vfio group should be deleted from kvm vfio device when group is
closed.

BUG=b:185084350
TEST=Boot a guest and hotplug vfio device repeatedly

Change-Id: I4a8a18a6a890846f514df12b48a7440786e4e74c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955573
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:11 +00:00
Xiong Zhang
5865a65180 devices:vfio: Remove vfio group when vfio device is closed
One container could contains multi groups, one group could contains
multi devices, when all the devices in the group are closed, the
group itself should be removed also.

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

Change-Id: Ia43da3e2e9ad58d0a7596141645926931c0bcc5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955572
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:10 +00:00
Xiong Zhang
3897cd0a2c devices:vfio-pci: Handle vfio pci device bar reallocation
Vfio pci device's bar are mapped into ept and iommu page table for
hardware acceleration, when vfio pci device is hotplug in the guest,
guest kernel will reallocate the bar, so the old bar address should
be deleted from ept and iommu, and the new bar address should be
added.

BUG=b:185084350
TEST=Boot a vm and hotplug add a device

Change-Id: Icc489917f4c241180bbef412a17917b34fafe7e5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955571
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:09 +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
Xiong Zhang
f67785e9e4 devices:pcie: Specify Bus number at pci bridge creation
When pci bridige is created, caller should specify its primary bus
number and secondary bus number. And supposing kernel won't modify
them as the current pci device topology is simple enough, once
guest modify them, warn message will be printed.

The secondary bus number shouldn't be assigned to any other device,
so this patch loop the bus number to find a free bus number.

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

Change-Id: Iae72a0e0401a6e75c62582456b92792a1a36211a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2955569
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:07 +00:00
Christian Blichmann
50f9591b6a devices: vhost: Allow vhost-vsock fd from command-line
Passing an open FD is necessary to avoid racing for vsock context IDs:
On Borg, we first want to generate a random, unused CID and pass this to
crosvm. This requires us to open `/dev/vhost-vsock` and call the
`VHOST_VSOCK_SET_GUEST_CID` ioctl on it. Closing the device and simply
assing its path to crovm opens up a time window where an overlapping VM
creation request may set a different CID on the device.

BUG=none
TEST=tools/presubmit --quick

Change-Id: If053e26ba93ab357b06e8cf07798679c8ad06c9e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3263933
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Christian Blichmann <cblichmann@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-11-18 15:19:29 +00:00
Keiichi Watanabe
6f5193e2cb devices: vfio: Use DataInit for {read, write}_config()
BUG=none
TEST=build

Change-Id: I23d00352dbeb82f6a4356f038942b66dce9b22f4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3245355
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-18 15:11:11 +00:00
Keiichi Watanabe
5c62c01a3f vm_memory: Allow File-backing MemoryRegion
In addition to SharedMemory, we allow File (e.g. mmap'd file) to be an
backing object of MemoryRegion.

BUG=b:194137301
TEST=kokoro

Change-Id: I9dafd028eaf74cf34820ba3f16e458c08ec67cb8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3159883
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-11-18 08:45:19 +00:00
Keiichi Watanabe
961743f797 devices: ac97: Create SharedMemory from GuestMemory's descriptor
This is a preparation for CL:3159883, where GuestMemory will be backed
by `dyn AsRawDescriptor` instead of `SharedMemory`.
So this CL adds a logic to create a SharedMemory from `&dyn
AsRawDescriptor`.

BUG=b:194137301
TEST=cargo test --all-features in audio_streams

Change-Id: I615be5976184696bcb9ae90928e366276467013a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3159882
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
2021-11-18 08:45:17 +00:00
Keiichi Watanabe
3893a1dcbe audio_streams: Introduce SharedMemory trait for base::SharedMemory
Define `SharedMemory` trait, implement it for `base::SharedMemory`, and
use it for `ShmStreamSource::new_stream()` so that we can pass on
`base::SharedMemory` without exposing `base` to libcras.

Note that we implement the trait for `base::SharedMemory` in `base`
crate instead of in `audio_streams`, which means `base` depends on
`audio_streams`, because `audio_streams` is available for non-crosvm
projects while `base` isn't.

The corresponding CRAS change is crrev.com/c/3251123.

BUG=b:194137301, b:204444445
TEST=cargo test --all-features

Change-Id: I2430d9881d98032592c9f9202d70deb22e006f63
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3251820
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-18 08:45:14 +00:00
Tinghao Zhang
f5a3037eec devices: vfio: Add device label to error messages
Add device label to vfio related error messages to get device name when
debugging

BUG=None
TEST=./tools/run_tests
TEST=./tools/presubmit

Change-Id: I910cbe35dde2d88155f693f90225a2708663d75d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3288284
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-11-17 23:16:26 +00:00
Keiichi Watanabe
9e37f05611 devices: vfio: Abort if reading/writing VFIO region fails
Cause panic when it failed to read or write VFIO regions because there
are no ways of recovery.

BUG=b:194137301
TEST=build

Change-Id: I0f6296d1835443641a12d5608b774ec35fdea7e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3149872
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-17 22:56:15 +00:00
Haiwei Li
e892738ed6 devices: vfio: Fix overlapping only if pba offset is greater than table offset
This fixes a bug introduced by [1] when msix pba table offset is smaller than
msix table offset.

An error may occure when booting guest with some vfio devices:

[ERROR:src/panic_hook.rs:90] thread 'main' panicked at 'attempt to subtract with overflow', devices/src/pci/vfio_pci.rs:274:26

[1] 7458a3a19b

So limit the adjustment of table size only if pba table offset is greater than
table offset.

BUG=b:1971693450
TEST=passthrough a nvme which the msix table offset is greater than pba
table offset, boot and verify MSIX-capable passed-through devices work
properly.

Change-Id: I1432c2290feaa5f6db088ebd416dc346547e7952
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3279957
Reviewed-by: Tina Zhang <tina.zhang@intel.corp-partner.google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-11-15 21:29:24 +00:00
Daniel Verkamp
c13c0c210b enumn: switch to upstream crates.io version
Developers may need to manually clean up the common/enumn directory - it
will be left behind if there are build artifacts (Cargo.lock, target
directory, etc.):

  rm -rf common/enumn

BUG=b:205344148
TEST=cargo build
TEST=tools/presubmit
TEST=emerge-hatch crosvm # with https://crrev.com/c/3265967

Change-Id: I1af3bdd22f40e87895a78a5cbc8033476058c927
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3278774
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-11-15 21:03:25 +00:00
Dennis Kempin
c3dedf3cc1 cleanup: Fix previously disabled clippy checks
There were not too many cases here. This fixes:

- comparison_chain
- wrong_self_convention
- upper_case_acronyms
- from_over_into
- let-and-return

The collapsible_if check is moved to the permanently
allowed checks. The cases we do have improve
readability or semantics.

BUG=chromium:908640
TEST=Kokoro

Change-Id: I6e905d08e2a87aa0862d4d1cf5ff57b60e95fa7d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3278776
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-12 23:23:48 +00:00
Chirantan Ekbote
13f5e9b983 fs: Use openat64 instead of openat
This ensures that we can open files whose sizes may be larger than a
32-bit off_t.

BUG=b:181113648
TEST=emerge-kukui crosvm

Change-Id: I2322480dcf507f83609117b4def40846d619b69b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3270100
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-12 01:11:19 +00:00
Tina Zhang
7458a3a19b devices: vfio: Fix a msix table and msix pba table overlapping issue
An error occured when passthrough some nvme to a guest based on vfio:

[ERROR:devices/src/pci/vfio_pci.rs:773] add_bar_mmap_msix failed:
		Out-of-space detected in MSIX Allocator

Althrough the issue may only happen to some devices whose msix table
overlaps with its msix pba table, as it was mentioned before[1], this
situation must be covered by some validation test. Otherwise, it will
block crosvm booting.

[1] https://patchwork.kernel.org/project/qemu-devel/patch/099db937-3fa3-465e-9a23-a900df9adb7c@default/

BUG=b:1971693450
TEST=passthrough a nvme with a msix table overlapping the msix pba table,
then boot Linux kernel and verify MSIX-capable passed-through devices
work properly.

Change-Id: I602dda95d4671682dc03478415f6a96d7c40ec6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3152434
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-11-10 23:21:11 +00:00
Christian Blichmann
e32b55670b virtio-fs: Unshare fs attributes to avoid locking
Calling `unshare(CLONE_FS)` from worker threads means we can freely
modify the current working directory and umask without having to guard
theses attributes with locks.

BUG=none
TEST=./tools/presubmit --quick

Change-Id: I29144b3d233b84e761c11a5e46efe541117e7f2a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3263932
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Christian Blichmann <cblichmann@google.com>
Auto-Submit: Christian Blichmann <cblichmann@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-11-10 10:45:39 +00:00
Keiichi Watanabe
63564d55cc vmm_vhost: vhost_user: Introduce Listener trait
Rename Listener struct to SocketListener and introduce Listener trait.
This is a preparation for adding VFIO backend for virtio-vhost-user.

BUG=b:194137301
TEST=cargo test --all-features --all-targets

Change-Id: Iea1e5b4378606b696c4022e3e2e6f4bbf80be55f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3268267
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-11-10 08:42:17 +00:00
David Staessens
85fa9ce5f1 Revert "virtio: video: reject resources with more than one entry"
This reverts commit 4ceec18783.

Reason for revert: This breaks all ARCVM HW encoding

BUG=b:205488531
TEST=arc.VideoEncodeAccel.h264_192p_i420_vm

Original change's description:
> virtio: video: reject resources with more than one entry
>
> The crosvm video device cannot currently work with resources that have
> more than one memory/object entry. Enforce this rule at the command
> level.
>
> BUG=b:161774071
> BUG=b:169295147
> TEST=Android Youtube plays properly on Hatch.
>
> Cq-Depend: chromium:3247491
> Cq-Depend: chromium:3247492
> Change-Id: Ibfe2e420b4a77062cca940c5e97e7053aa4b76a7
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3026345
> Tested-by: kokoro <noreply+kokoro@google.com>
> Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
> Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>

Bug: b:161774071
Bug: b:169295147
Change-Id: Ib24bd8098b37d696b74bc942cdb7c81719d1b8b6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3267727
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: David Staessens <dstaessens@chromium.org>
2021-11-08 03:57:27 +00:00
Xiong Zhang
2d8e1b09a5 Devices: Return bar reallocation info at bar written
Previous commit "devices: Add/Remove device mmio at config memory enable
/disable", it has an assumption that when driver update bar's location,
driver should disable bar's memory/io at first.

But some driver reallocate bar's address even with memory/io enabled,
this commit monitor bar's config write, if address is modified, it
return this  modification.

BUG=b:174705596
TEST=boot a vm, monitor resource reallocation and check function.

Change-Id: I9aa883192f5e2fec344d3e86c818c7a29287f49a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3184726
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-05 19:35:10 +00:00
Xiong Zhang
479efab2f5 devices: Don't return bar info if memory/io is disabled
get_bar_configuration() returnis bar's info, this ignores memory/io enable
bit. Usually driver disable memory/io first, then wrtie 0xFFFFFFFF to
probe bar's existence and size, finally write real address and enable
memory/io.

if get_bar_configuration is called after mmio/io disabled, current
it may return invalid info. In order to filter these invalid info,
this commit checks memory/io enable bit, only return info at memory/io
enabled.

BUG=b:174705996
TEST=carto test -p devices

Change-Id: Ib8bf1a7f7f959025117f319f8f6c5a9e5efea2e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3252779
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-05 19:35:09 +00:00
Christian Blichmann
7a976d3a8a virtio-fs: Apply syscall!() macro to syscall invocations
TESTED=./tools/presubmit --quick

Change-Id: I5238731b9785f315d0869ed85984cf87ed36ad73
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3259940
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Christian Blichmann <cblichmann@google.com>
2021-11-05 09:22:30 +00:00
David Staessens
528fcabc5d virtio: video: Allow config changes if encoding hasn't started yet.
When initializing the device for encoding the underlying encoder
session is immediately brought up, as this is required to determine
parameters such as the desired output buffer size. This causes further
configuration changes to be refused, even when the device isn't
encoding anything yet. This CL makes changes to allow configuration
changes by recreating the encoder sessions as long as no buffers have
been queued.

BUG=b:203608233
TEST=tast run DUT arc.VideoEncodeAccel.h264_192p_i420_vm

Change-Id: I7e9d8b41ea2497b5d86aaca7db7ed13af056f11d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3252185
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: David Staessens <dstaessens@chromium.org>
2021-11-05 00:32:33 +00:00
Colin Downs-Razouk
11bed5eef7 devices: re-add set_access_id to bus
We need the access id for some future changes that will be posted soon
for a userspace irqchip.

Each vcpu will have a clone of the Bus itself, not the Arc<Bus>, so that
they can each have their own access id.

This also adds a call to set_access_id to run_vcpu.

BUG=b:160610704
TEST=ran local presubmit, and ran test image

Change-Id: I4afb0ef9c234f25150ce2f89d6fb228ad5b88745
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3261627
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Colin Downs-Razouk <colindr@google.com>
2021-11-04 17:55:18 +00:00
Alexandre Courbot
7f02059afc virtio: video: build backends separately from the codec device
The current way for building a codec device is to call the corresponding
constructor method of the backend we want to use. However doing this
forces us to pass the resource bridge to these constructors, introducing
dependencies from the backend to parts of the code that are not used
otherwise.

Switch the construction method to one constructor per backend, which
only takes the arguments relevant to the backend, and one device
constructor that takes the backend to use as parameter.

BUG=b:161774071
BUG=b:169295147
TEST=Android Youtube plays properly on Hatch.

Change-Id: I493f421e982a1d2ba8292a69bae575afeee17e4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3026349
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-04 14:01:22 +00:00
Alexandre Courbot
8230abf39a virtio: video: compile out decoder or encoder if corresponding feature not selected
We have two separate features for video decoder and encoder, but
selecting any of them results in all the video code being included. Add
some #[cfg] statements to compile out the unneeded code if only one of
the features is selected.

BUG=b:161774071
TEST=`cargo build` passes without warning
TEST=`cargo build --features "video-decoder"` passes without warning
TEST=`cargo build --features "video-encoder"` passes without warning
TEST=`cargo build --features "video-decoder,video-encoder"` passes without warning

Change-Id: I3e5f2173695ee9dd5e8d26ace14374d426e570a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3026347
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-04 14:01:20 +00:00
Alexandre Courbot
66ed8cfb41 virtio: video: make backends accept GuestResources as inputs
Make the backends accept our generic resource types as inputs instead of
expecting virtio objects. This makes the backends able to deal with any
kind of resource, the counterpart being that they must now check what
kind of resource they received. The VDA can only deal with virtio
objects and will reject other kinds of resources.

On top of making backends not limited to virtio objects, this is also
safer than passing FDs because it becomes clear when we are transferring
ownership of a resource, and when we need to clone them. Also since
resources carry information about their layout, the number of arguments
we need to pass to the backends is reduced.

BUG=b:161774071
BUG=b:169295147
TEST=arc.VideoDecodeAccel.h264_vm passes on hatch
TEST=arc.VideoEncodeAccel.h264_360p_i420_vm passes on hatch
TEST=GtsExoPlayerTestCases com.google.android.exoplayer.gts.DashTest
passes on hatch

Change-Id: I09dd81bac13a46fa908a4107a376fa7d59fb9759
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2983096
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-04 14:01:17 +00:00
Alexandre Courbot
cf16363c4c virtio: video: introduce basis for proper resource management
Currently the video device only accepts virtio objects (as file
descriptors) for resources, and file descriptors are hard-coded a bit
all over the place to represent buffers and frames. This causes two issues:

1) Multiple buffers cannot currently be used to represent frames, i.e. 1
   frame is exactly one virtio object,
2) Resources that are not virtio objects, like guest memory, cannot
   be used at the moment.

This patch introduces more flexibility in the way resources are handled:
first, all the resource-related code, including their resolution using
the resource bridge, is consolidated into a new resource module. Then,
the command module does not arbitrarily decide that all resources are
virtio objects, and leaves that decision to the queues. Finally,
resources now have their enum type that can unfold into any of the
resource types we will support (currently only virtio objects). This
forces consumers of resources to check their type before using them.

Semantically, very little should change. The only noticeable side-effect
should be that we now call the resource bridge only once per input
buffer instead of once per decoder operation.

BUG=b:161774071
BUG=b:169295147
TEST=arc.VideoDecodeAccel.h264_vm passes on hatch
TEST=arc.VideoEncodeAccel.h264_360p_i420_vm passes on hatch

Change-Id: I93b203117b5c96e91d4f1b8007e95ec5c501ea5f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2983095
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-04 14:01:16 +00:00
Alexandre Courbot
4ceec18783 virtio: video: reject resources with more than one entry
The crosvm video device cannot currently work with resources that have
more than one memory/object entry. Enforce this rule at the command
level.

BUG=b:161774071
BUG=b:169295147
TEST=Android Youtube plays properly on Hatch.

Cq-Depend: chromium:3247491
Cq-Depend: chromium:3247492
Change-Id: Ibfe2e420b4a77062cca940c5e97e7053aa4b76a7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3026345
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
2021-11-04 14:01:16 +00:00
David Stevens
5ebe30d0e5 wl: add support for host fences
Add support for guest fences backed by host sync_files. Rather than
adding a new fence type, fences are implemented as vfds that only
support hup. The host simply waits on the sync_file and sends the guest
a hup when the fence is signaled.

BUG=b:169908659
TEST=Revert ag/15543199, resize ArcCameraFpsTest

Change-Id: I8c79ec9f418b1d71150b5a19f500c5ac7a6c9b02
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3199298
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: David Stevens <stevensd@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-04 01:24:37 +00:00
Xiong Zhang
e9020b1c83 devices: Add/Remove deviece mmio at config memory enable/disable
When device memory/io is disabled, its mmio/io range should be
removed from mmio_bus/io_bus.

When device memory/io is enabled, its mmio/io range should be
added into mmio_bus/io_bus.

BUG=b:174705596
TEST=Boot a vm, monitor device mmio/io enable/disable, then check
vm function.

Change-Id: Ic77565b9828599d574329bea76146cff597a94fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3252778
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-03 20:14:02 +00:00
Daniel Verkamp
ff31bcfb48 devices: bus: add BusDevice function to get ranges
When device's bar are reallocated, all the old resource ranges should be
deleted, the new resouce ranges shoud be added, this commit add the
interface to get device mmmio/io old/new ranges.

BUG=b:174705596
TEST=Boot a vm, monitor device bar reallocation and check function

Change-Id: I1000607d9f766ff8b445bf92f17969b094f3b2ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2718280
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2021-11-03 20:14:01 +00:00
Xiong Zhang
d982b8d9a3 devices: Introduce remove() for bus
When device's bar is reallocated, the old ranges will be deleted,
this commit add remove() function to delete one old range from
bus->devices.

BUG=b:174705596
TEST=Boot a vm, monitor Bar Reallocation then check function

Change-Id: Id0600ae1d7a3d04c7f05a90abcc8807b7f1020c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3184725
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-11-03 20:14:00 +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
Mattias Nissler
de2c640f17 Add stub PCI device implementation.
Introduce a very simple stub PCI device that can be added to the bus
at a specified address with given PCI config parameters (vendor,
device, etc.). This is useful for cases where we just require a device
to be present during PCI enumeration.

The case that motivates this is a vfio device passthrough
configuration that passes only selected functions of a given device at
the original addresses, but function 0 is not passed through. Absence
of function 0 would be interpreted in enumeration as the entire device
being absent (in accordance with the specification). Putting a stub
device at function 0 fixes this.

BUG=b:167947780
TEST=New unit test, boot minimal Linux image and verify enumerated PCI device.

Change-Id: Iaedffe1c4ac2ad8f6ff6e00dfeebbbfeb5490551
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3245497
Auto-Submit: Mattias Nissler <mnissler@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Mattias Nissler <mnissler@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-27 14:06:35 +00:00
Mattias Nissler
fce74e6fc2 Add TryFrom implementation for PciClassCode
This is useful for example when parsing numeric PCI class code values
from the command line.

BUG=b:167947780
TEST=None

Change-Id: I55515af2fa7e38bc26f4c2eebb083f1e45aebf22
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3245496
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Mattias Nissler <mnissler@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-27 13:06:55 +00:00
Mattias Nissler
441a68194a Add multifunction bit to PCIConfiguration
It's the highest bit in the header type field, this change allows to
set the bit and thus indicate that a device is a multifunction device.

BUG=b:167947780
TEST=None

Change-Id: I71372e2490897bc3d5c24717c9504e337792a48f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3245495
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Mattias Nissler <mnissler@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-27 13:06:54 +00:00
Jiyong Park
5d5a128943 devices: Serial devices can be backed by FD via /proc/self/fd/N
Like disks, serial devices can be backed by file descriptors using the
/proc/self/fd/N syntax.

BUG=b:200914564
TEST:cargo test
Change-Id: Idd6f5763db24e61a80dc34732c9e2118d613cefc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3241083
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-10-27 00:38:43 +00:00
Keiichi Watanabe
d73e41276e devices: vfio: Reorder use statements
BUG=none
TEST=build

Change-Id: I8ce2d48cce787fe6352d784fe0b583657d51ad39
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3245354
Auto-Submit: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-26 19:33:07 +00:00
Daniel Verkamp
0c2f9c749f bindgen: switch to allowlist/blocklist options
Rust bindgen now supports --allowlist and --blocklist option names as of
https://github.com/rust-lang/rust-bindgen/pull/1990 - use them in the
various bindgen invocations in scripts and comments.

BUG=b:178821708
TEST=../dev/contrib/search_blocked_words.sh unblocked_terms.txt

Change-Id: I9797f049999a01ab2c41617823463236b4d03cbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3243012
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-10-26 18:32:51 +00:00
Xiong Zhang
6d1b2df054 pci-bridge: Set limit smaller than base at initialize
According to pci bridge spec, the io/mem/prefetch mem limit register
must be programmed to a smaller value than the corresponding base register
if there is no io/mem/prefetch mem on the secondary side of the brige.

When bridge is created, it doesn't have any child device, then
no io/mem/prefetch mem on the secondary side of the bridge, so this
patch set limit to 0, but base to 0xffff.

When a device is attached behind the bridge, kernel will assign
resource and set the right value to these base and limit register.

BUG=b:199442120
BUG=b:185084350
TEST=crosvm run --bios OVMF.fd
TEST=hotplug in/out a pcie device to pcie root port repeatly

Change-Id: Id1c2ff1132d59e1aafd548fc17ab5aee2023dd8c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3166883
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-10-25 22:03:13 +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