qemu_ram_block_from_host

Postcopy sends RAMBlock names and offsets over the wire (since it can't
rely on the order of ramaddr being the same), and it starts out with
HVA fault addresses from the kernel.

qemu_ram_block_from_host translates a HVA into a RAMBlock, an offset
in the RAMBlock and the global ram_addr_t value.

Rewrite qemu_ram_addr_from_host to use qemu_ram_block_from_host.

Provide qemu_ram_get_idstr since its the actual name text sent on the
wire.

Backports commit 422148d3e56c3c9a07c0cf36c1e0a0b76f09c357 from qemu
This commit is contained in:
Dr. David Alan Gilbert 2018-02-17 17:52:30 -05:00 committed by Lioncash
parent 23ece1622c
commit cc088f84b5
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
18 changed files with 79 additions and 5 deletions

View file

@ -19,7 +19,6 @@ struct uc_struct;
// This two struct is originally from qemu/include/exec/cpu-all.h
// Temporarily moved here since there is circular inclusion.
typedef struct RAMBlock RAMBlock;
struct RAMBlock {
struct MemoryRegion *mr;
uint8_t *host;

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_aarch64
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_aarch64
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_aarch64
#define qemu_ram_block_from_host qemu_ram_block_from_host_aarch64
#define qemu_ram_foreach_block qemu_ram_foreach_block_aarch64
#define qemu_ram_free qemu_ram_free_aarch64
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_aarch64
#define qemu_ram_get_idstr qemu_ram_get_idstr_aarch64
#define qemu_ram_ptr_length qemu_ram_ptr_length_aarch64
#define qemu_ram_remap qemu_ram_remap_aarch64
#define qemu_ram_resize qemu_ram_resize_aarch64

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_aarch64eb
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_aarch64eb
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_aarch64eb
#define qemu_ram_block_from_host qemu_ram_block_from_host_aarch64eb
#define qemu_ram_foreach_block qemu_ram_foreach_block_aarch64eb
#define qemu_ram_free qemu_ram_free_aarch64eb
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_aarch64eb
#define qemu_ram_get_idstr qemu_ram_get_idstr_aarch64eb
#define qemu_ram_ptr_length qemu_ram_ptr_length_aarch64eb
#define qemu_ram_remap qemu_ram_remap_aarch64eb
#define qemu_ram_resize qemu_ram_resize_aarch64eb

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_arm
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_arm
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_arm
#define qemu_ram_block_from_host qemu_ram_block_from_host_arm
#define qemu_ram_foreach_block qemu_ram_foreach_block_arm
#define qemu_ram_free qemu_ram_free_arm
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_arm
#define qemu_ram_get_idstr qemu_ram_get_idstr_arm
#define qemu_ram_ptr_length qemu_ram_ptr_length_arm
#define qemu_ram_remap qemu_ram_remap_arm
#define qemu_ram_resize qemu_ram_resize_arm

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_armeb
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_armeb
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_armeb
#define qemu_ram_block_from_host qemu_ram_block_from_host_armeb
#define qemu_ram_foreach_block qemu_ram_foreach_block_armeb
#define qemu_ram_free qemu_ram_free_armeb
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_armeb
#define qemu_ram_get_idstr qemu_ram_get_idstr_armeb
#define qemu_ram_ptr_length qemu_ram_ptr_length_armeb
#define qemu_ram_remap qemu_ram_remap_armeb
#define qemu_ram_resize qemu_ram_resize_armeb

View file

@ -986,6 +986,11 @@ static RAMBlock *find_ram_block(struct uc_struct *uc, ram_addr_t addr)
return NULL;
}
const char *qemu_ram_get_idstr(RAMBlock *rb)
{
return rb->idstr;
}
void qemu_ram_unset_idstr(struct uc_struct *uc, ram_addr_t addr)
{
RAMBlock *block = find_ram_block(uc, addr);
@ -1301,9 +1306,27 @@ static void *qemu_ram_ptr_length(struct uc_struct *uc, ram_addr_t addr, hwaddr *
abort();
}
/* Some of the softmmu routines need to translate from a host pointer
(typically a TLB entry) back to a ram offset. */
MemoryRegion *qemu_ram_addr_from_host(struct uc_struct *uc, void *ptr, ram_addr_t *ram_addr)
/*
* Translates a host ptr back to a RAMBlock, a ram_addr and an offset
* in that RAMBlock.
*
* ptr: Host pointer to look up
* round_offset: If true round the result offset down to a page boundary
* *ram_addr: set to result ram_addr
* *offset: set to result offset within the RAMBlock
*
* Returns: RAMBlock (or NULL if not found)
*
*
* By the time this function returns, the returned pointer is not protected
* by RCU anymore. If the caller is not within an RCU critical section and
* does not hold the iothread lock, it must have other means of protecting the
* pointer, such as a reference to the region that includes the incoming
* ram_addr_t.
*/
RAMBlock *qemu_ram_block_from_host(struct uc_struct* uc, void *ptr, bool round_offset,
ram_addr_t *ram_addr,
ram_addr_t *offset)
{
RAMBlock *block;
uint8_t *host = ptr;
@ -1326,7 +1349,27 @@ MemoryRegion *qemu_ram_addr_from_host(struct uc_struct *uc, void *ptr, ram_addr_
return NULL;
found:
*ram_addr = block->offset + (host - block->host);
*offset = (host - block->host);
if (round_offset) {
*offset &= TARGET_PAGE_MASK;
}
*ram_addr = block->offset + *offset;
return block;
}
/* Some of the softmmu routines need to translate from a host pointer
(typically a TLB entry) back to a ram offset. */
MemoryRegion *qemu_ram_addr_from_host(struct uc_struct* uc, void *ptr, ram_addr_t *ram_addr)
{
RAMBlock *block;
ram_addr_t offset; /* Not used */
block = qemu_ram_block_from_host(uc, ptr, false, ram_addr, &offset);
if (!block) {
return NULL;
}
return block->mr;
}

View file

@ -2442,9 +2442,11 @@ symbols = (
'qemu_ram_alloc',
'qemu_ram_alloc_from_ptr',
'qemu_ram_alloc_resizeable',
'qemu_ram_block_from_host',
'qemu_ram_foreach_block',
'qemu_ram_free',
'qemu_ram_free_from_ptr',
'qemu_ram_get_idstr',
'qemu_ram_ptr_length',
'qemu_ram_remap',
'qemu_ram_resize',

View file

@ -49,8 +49,11 @@ typedef uint32_t CPUReadMemoryFunc(void *opaque, hwaddr addr);
void qemu_ram_remap(struct uc_struct *uc, ram_addr_t addr, ram_addr_t length);
/* This should not be used by devices. */
MemoryRegion *qemu_ram_addr_from_host(struct uc_struct* uc, void *ptr, ram_addr_t *ram_addr);
RAMBlock *qemu_ram_block_from_host(struct uc_struct* uc, void *ptr, bool round_offset,
ram_addr_t *ram_addr, ram_addr_t *offset);
void qemu_ram_set_idstr(struct uc_struct *uc, ram_addr_t addr, const char *name, DeviceState *dev);
void qemu_ram_unset_idstr(struct uc_struct *uc, ram_addr_t addr);
const char *qemu_ram_get_idstr(RAMBlock *rb);
bool cpu_physical_memory_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
int len, int is_write);

View file

@ -77,6 +77,7 @@ typedef struct SHPCDevice SHPCDevice;
typedef struct FWCfgState FWCfgState;
typedef struct PcGuestInfo PcGuestInfo;
typedef struct Range Range;
typedef struct RAMBlock RAMBlock;
typedef struct AdapterInfo AdapterInfo;
#endif /* QEMU_TYPEDEFS_H */

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_m68k
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_m68k
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_m68k
#define qemu_ram_block_from_host qemu_ram_block_from_host_m68k
#define qemu_ram_foreach_block qemu_ram_foreach_block_m68k
#define qemu_ram_free qemu_ram_free_m68k
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_m68k
#define qemu_ram_get_idstr qemu_ram_get_idstr_m68k
#define qemu_ram_ptr_length qemu_ram_ptr_length_m68k
#define qemu_ram_remap qemu_ram_remap_m68k
#define qemu_ram_resize qemu_ram_resize_m68k

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_mips
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_mips
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_mips
#define qemu_ram_block_from_host qemu_ram_block_from_host_mips
#define qemu_ram_foreach_block qemu_ram_foreach_block_mips
#define qemu_ram_free qemu_ram_free_mips
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_mips
#define qemu_ram_get_idstr qemu_ram_get_idstr_mips
#define qemu_ram_ptr_length qemu_ram_ptr_length_mips
#define qemu_ram_remap qemu_ram_remap_mips
#define qemu_ram_resize qemu_ram_resize_mips

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_mips64
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_mips64
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_mips64
#define qemu_ram_block_from_host qemu_ram_block_from_host_mips64
#define qemu_ram_foreach_block qemu_ram_foreach_block_mips64
#define qemu_ram_free qemu_ram_free_mips64
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_mips64
#define qemu_ram_get_idstr qemu_ram_get_idstr_mips64
#define qemu_ram_ptr_length qemu_ram_ptr_length_mips64
#define qemu_ram_remap qemu_ram_remap_mips64
#define qemu_ram_resize qemu_ram_resize_mips64

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_mips64el
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_mips64el
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_mips64el
#define qemu_ram_block_from_host qemu_ram_block_from_host_mips64el
#define qemu_ram_foreach_block qemu_ram_foreach_block_mips64el
#define qemu_ram_free qemu_ram_free_mips64el
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_mips64el
#define qemu_ram_get_idstr qemu_ram_get_idstr_mips64el
#define qemu_ram_ptr_length qemu_ram_ptr_length_mips64el
#define qemu_ram_remap qemu_ram_remap_mips64el
#define qemu_ram_resize qemu_ram_resize_mips64el

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_mipsel
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_mipsel
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_mipsel
#define qemu_ram_block_from_host qemu_ram_block_from_host_mipsel
#define qemu_ram_foreach_block qemu_ram_foreach_block_mipsel
#define qemu_ram_free qemu_ram_free_mipsel
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_mipsel
#define qemu_ram_get_idstr qemu_ram_get_idstr_mipsel
#define qemu_ram_ptr_length qemu_ram_ptr_length_mipsel
#define qemu_ram_remap qemu_ram_remap_mipsel
#define qemu_ram_resize qemu_ram_resize_mipsel

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_powerpc
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_powerpc
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_powerpc
#define qemu_ram_block_from_host qemu_ram_block_from_host_powerpc
#define qemu_ram_foreach_block qemu_ram_foreach_block_powerpc
#define qemu_ram_free qemu_ram_free_powerpc
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_powerpc
#define qemu_ram_get_idstr qemu_ram_get_idstr_powerpc
#define qemu_ram_ptr_length qemu_ram_ptr_length_powerpc
#define qemu_ram_remap qemu_ram_remap_powerpc
#define qemu_ram_resize qemu_ram_resize_powerpc

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_sparc
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_sparc
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_sparc
#define qemu_ram_block_from_host qemu_ram_block_from_host_sparc
#define qemu_ram_foreach_block qemu_ram_foreach_block_sparc
#define qemu_ram_free qemu_ram_free_sparc
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_sparc
#define qemu_ram_get_idstr qemu_ram_get_idstr_sparc
#define qemu_ram_ptr_length qemu_ram_ptr_length_sparc
#define qemu_ram_remap qemu_ram_remap_sparc
#define qemu_ram_resize qemu_ram_resize_sparc

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_sparc64
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_sparc64
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_sparc64
#define qemu_ram_block_from_host qemu_ram_block_from_host_sparc64
#define qemu_ram_foreach_block qemu_ram_foreach_block_sparc64
#define qemu_ram_free qemu_ram_free_sparc64
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_sparc64
#define qemu_ram_get_idstr qemu_ram_get_idstr_sparc64
#define qemu_ram_ptr_length qemu_ram_ptr_length_sparc64
#define qemu_ram_remap qemu_ram_remap_sparc64
#define qemu_ram_resize qemu_ram_resize_sparc64

View file

@ -2436,9 +2436,11 @@
#define qemu_ram_alloc qemu_ram_alloc_x86_64
#define qemu_ram_alloc_from_ptr qemu_ram_alloc_from_ptr_x86_64
#define qemu_ram_alloc_resizeable qemu_ram_alloc_resizeable_x86_64
#define qemu_ram_block_from_host qemu_ram_block_from_host_x86_64
#define qemu_ram_foreach_block qemu_ram_foreach_block_x86_64
#define qemu_ram_free qemu_ram_free_x86_64
#define qemu_ram_free_from_ptr qemu_ram_free_from_ptr_x86_64
#define qemu_ram_get_idstr qemu_ram_get_idstr_x86_64
#define qemu_ram_ptr_length qemu_ram_ptr_length_x86_64
#define qemu_ram_remap qemu_ram_remap_x86_64
#define qemu_ram_resize qemu_ram_resize_x86_64