memory: Fix comment typo

s/ajacent/adjacent

Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
memory: as_update_topology_pass: Improve comments

These comments we're a little difficult to read. First one had
incorrect parenthesis. The part about attributes changing is
really applicable to the region being 'in both' rather than 'in
new'

Second comment has an obscure parenthetic about 'Logging may have
changed'. Made clearer, as this if is supposed to handle the case where
the memory region is unchanged (with the notable exception re logging).

Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
memory: render_memory_region: factor out fr constant setters

These 4 replicated lines set properties of fr that are constant over
the course of the function. Factor out their repeated setting (and also
guards against them being set multiple times in the loop below).

Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
memory: add getter/setter for owner

Whenever memory regions are accessed outside the BQL, they need to be
preserved against hot-unplug.  MemoryRegions actually do not have their
own reference count; they piggyback on a QOM object, their "owner".
Add two functions to retrieve and specify the owner.

The setter function will affect the owner recursively on a whole tree
of contained regions, but without crossing (a) aliases (b) regions that
are already owned by another device.  This is so that a device can create
a complex tree of regions and a single call to memory_region_set_owner
will affect the entire tree.

In turn, this lets buses (usually through a bus-specific function, e.g.
pci_register_bar) set the owner for regions that are managed by the bus.
The device must set the owner itself only if the device plays directly
with address_space_memory/io (which shouldn't happen except in special
cases) or if regions are added/deleted after passing the container to
the bus (for example dynamically while the device runs).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
memory: add ref/unref

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
memory: add ref/unref calls

Add ref/unref calls at the following places:

- places where memory regions are stashed by a listener and
  used outside the BQL (including in Xen or KVM).

- memory_region_find callsites

- creation of aliases and containers (only the aliased/contained
  region gets a reference to avoid loops)

- around calls to del_subregion/add_subregion, where the region
  could disappear after the first call

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
exec: add a reference to the region returned by address_space_translate

Once address_space_translate will only be protected by RCU, the returned
MemoryRegion can disappear as soon as the RCU read-side critical
section ends.  Avoid this by adding a reference to the region, and
dropping it in the caller of address_space_translate.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
pci: set owner for BARs

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
sysbus: add sysbus_pass_mmio

This matches sysbus_pass_irq in cases where a device is a thin wrapper
of another.  MMIO regions will keep the subdevice as the owner.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
sysbus: set owner for MMIO regions

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
acpi: add memory_region_set_owner calls

ACPI regions are added directly to the I/O address space, without
going through BARs.  Thus they need the owner to be set directly.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
misc: add memory_region_set_owner calls

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
isa/portio: allow setting an owner

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
vga: add memory_region_set_owner calls

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
pci-assign: add memory_region_set_owner calls

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
vfio: add memory_region_set_owner calls

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
exec: check MRU in qemu_ram_addr_from_host

This function is not used outside the iothread mutex, so it
can use ram_list.mru_block.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
exec: move qemu_ram_addr_from_host_nofail to cputlb.c

It is only used there, and the _nofail and the standard
versions of this function return different things.  Limit
confusion by removing the function from the public headers.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
memory: return MemoryRegion from qemu_ram_addr_from_host

It will be needed in the next patch.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
memory: ref/unref memory across address_space_map/unmap

The iothread mutex might be released between map and unmap, so the
mapped region might disappear.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>