crosvm/aarch64
Pierre-Clément Tosi 869e16cdeb vfio_platform: Map device MMIO before booting pVMs
When memory is protected, the guest address space mapped to physical
memory shouldn't be modified dynamically after the guest has started
running so force CrosVM to map the device MMIO regions before that.
Otherwise, the protected VM firmware will fail to validate the assigned
regions (as they haven't actually been assigned yet), aborting the boot
of the pVM.

Note that we can't simply call device.regions_mmap() at this point
because the crosvm internal IPC (in particular vm_memory_handler_thread)
isn't set-up yet so this commit had to duplicate the mmap() logic to be
available in the main thread.

This change was verified using strace, going from

    ioctl(7, KVM_CREATE_VM, 0x80000028)     = 9
    [...]
    ioctl(36, VFIO_SET_IOMMU, 0xb)          = 0
    ioctl(9, KVM_CREATE_DEVICE, {type=KVM_DEV_TYPE_VFIO, flags=0}) = 0
    ioctl(43, KVM_SET_DEVICE_ATTR, {flags=0, group=1, attr=1, addr=0x7fdd3a4a60}) = 0
    ioctl(42, VFIO_GROUP_GET_DEVICE_FD, 0xb400007cc3d0eb90) = 60
    ioctl(60, VFIO_DEVICE_GET_INFO, 0x7fdd3a3cd0) = 0
    ioctl(60, VFIO_DEVICE_GET_REGION_INFO, 0x7fdd3a3570) = 0
    [...]
    [pid  4279] ioctl(5, KVM_RUN <unfinished ...>
    [...]
    [pid  4279] ioctl(5, KVM_RUN, 0)        = 0
    [pid  4281] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 369, 0) = 0x7d5b3fb000
    [pid  4281] ioctl(239, KVM_SET_USER_MEMORY_REGION, {slot=2, flags=0, guest_phys_addr=0x90000000, memory_size=4096, userspace_addr=0x7d5b3fb000}) = 0

to

    ioctl(7, KVM_CREATE_VM, 0x80000028)     = 9
    [...]
    ioctl(36, VFIO_SET_IOMMU, 0xb)          = 0
    ioctl(9, KVM_CREATE_DEVICE, {type=KVM_DEV_TYPE_VFIO, flags=0}) = 0
    ioctl(43, KVM_SET_DEVICE_ATTR, {flags=0, group=1, attr=1, addr=0x7fd1c1e030}) = 0
    ioctl(42, VFIO_GROUP_GET_DEVICE_FD, 0xb4000077a0628b70) = 60
    ioctl(60, VFIO_DEVICE_GET_INFO, 0x7fd1c1d2a0) = 0
    ioctl(60, VFIO_DEVICE_GET_REGION_INFO, 0x7fd1c1cb40) = 0
    [...]
    mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 189, 0) = 0x7917dbc000
    ioctl(9, KVM_SET_USER_MEMORY_REGION, {slot=2, flags=0, guest_phys_addr=0x90000000, memory_size=4096, userspace_addr=0x7917dbc000}) = 0
    [...]
    [pid  4233] ioctl(5, KVM_RUN <unfinished ...>

BUG=b:308642771
TEST=Manual run with a pKVM-VFIO device assigned

Change-Id: Iad3494c4e9c309f99fc5cab37ed913d484fc5150
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5029655
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Pierre-Clément Tosi <ptosi@google.com>
2023-11-17 12:04:03 +00:00
..
src vfio_platform: Map device MMIO before booting pVMs 2023-11-17 12:04:03 +00:00
.build_test_skip
Cargo.toml