memory: add owner argument to initialization functions

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
memory: add getter 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".
The owner is set at creation time, and there is a function to retrieve
the owner.

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>
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>
vga: pass owner to vga_init

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
piolist: add owner argument to initialization functions and pass devices

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/a*: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/block: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/c*: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/d*: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/gpio: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/i*: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/m*: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/n*: pass owner to memory_region_init_io

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/p*: pass owner to memory_region_init_io

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