unicorn/qemu/include
Alex Williamson fe66c2e088
memory: Don't use memcpy for ram_device regions
With a vfio assigned device we lay down a base MemoryRegion registered
as an IO region, giving us read & write accessors. If the region
supports mmap, we lay down a higher priority sub-region MemoryRegion
on top of the base layer initialized as a RAM device pointer to the
mmap. Finally, if we have any quirks for the device (ie. address
ranges that need additional virtualization support), we put another IO
sub-region on top of the mmap MemoryRegion. When this is flattened,
we now potentially have sub-page mmap MemoryRegions exposed which
cannot be directly mapped through KVM.

This is as expected, but a subtle detail of this is that we end up
with two different access mechanisms through QEMU. If we disable the
mmap MemoryRegion, we make use of the IO MemoryRegion and service
accesses using pread and pwrite to the vfio device file descriptor.
If the mmap MemoryRegion is enabled and results in one of these
sub-page gaps, QEMU handles the access as RAM, using memcpy to the
mmap. Using either pread/pwrite or the mmap directly should be
correct, but using memcpy causes us problems. I expect that not only
does memcpy not necessarily honor the original width and alignment in
performing a copy, but it potentially also uses processor instructions
not intended for MMIO spaces. It turns out that this has been a
problem for Realtek NIC assignment, which has such a quirk that
creates a sub-page mmap MemoryRegion access.

To resolve this, we disable memory_access_is_direct() for ram_device
regions since QEMU assumes that it can use memcpy for those regions.
Instead we access through MemoryRegionOps, which replaces the memcpy
with simple de-references of standard sizes to the host memory.

With this patch we attempt to provide unrestricted access to the RAM
device, allowing byte through qword access as well as unaligned
access. The assumption here is that accesses initiated by the VM are
driven by a device specific driver, which knows the device
capabilities. If unaligned accesses are not supported by the device,
we don't want them to work in a VM by performing multiple aligned
accesses to compose the unaligned access. A down-side of this
philosophy is that the xp command from the monitor attempts to use
the largest available access weidth, unaware of the underlying
device. Using memcpy had this same restriction, but at least now an
operator can dump individual registers, even if blocks of device
memory may result in access widths beyond the capabilities of a
given device (RTL NICs only support up to dword).

Backports commit 1b16ded6a512809f99c133a97f19026fe612b2de from qemu
2018-02-25 23:06:36 -05:00
..
crypto Drop unused crypto source files 2018-02-17 15:23:57 -05:00
exec memory: Don't use memcpy for ram_device regions 2018-02-25 23:06:36 -05:00
fpu softfloat: Fix warn about implicit conversion from int to int8_t 2018-02-25 22:54:39 -05:00
hw target-i386: Add x86_cpu_unrealizefn() 2018-02-25 20:54:13 -05:00
qapi Clean up header guards that don't match their file name 2018-02-25 04:18:42 -05:00
qemu tcg: Lower indirect registers in a separate pass 2018-02-25 22:32:28 -05:00
qom Fix confusing argument names in some common functions 2018-02-25 03:58:27 -05:00
sysemu accel: make configure_accelerator return void 2018-02-24 00:31:28 -05:00
config.h import 2015-08-21 15:04:50 +08:00
elf.h fix merge conflicts 2017-03-10 21:04:33 +08:00
glib_compat.h qapi: Fix memleak in string visitors on int lists 2018-02-25 00:20:34 -05:00
qemu-common.h qemu-common.h: Drop WORDS_ALIGNED define 2018-02-24 17:01:55 -05:00