execute code from mmio area
v2 cover execute code from mmio area
From: KONRAD Frederic <[email protected]>
This series allows to execute code from mmio areas.
The main goal of this is to be able to run code for example from an SPI device.
The three first patch fixes the way get_page_addr_code fills the TLB.
The fourth patch implements the mmio execution helpers: the device must implement the request_ptr callback of the MemoryRegion and will be notified when the guest wants to execute code from it.
The fifth patch introduces mmio_interface device which allows to dynamically map a host pointer somewhere into the memory.
The sixth patch implements the execution from the SPI memories in the xilinx_spips model.
Thanks,
Fred
V1 -> V2:
* Fix the DPRINTF error.
RFC -> V1:
* Use an interface (mmio-interface) to fix any reference leak issue.
KONRAD Frederic (7):
cputlb: cleanup get_page_addr_code to use VICTIM_TLB_HIT
cputlb: move get_page_addr_code
cputlb: fix the way get_page_addr_code fills the tlb
exec: allow to get a pointer for some mmio memory region
qdev: add MemoryRegion property
introduce mmio_interface
xilinx_spips: allow mmio execution
cputlb.c | 81 ++++++++++++++----------- hw/misc/Makefile.objs | 1 + hw/misc/mmio_interface.c | 128 +++++++++++++++++++++++++++++++++++++++ hw/ssi/xilinx_spips.c | 74 ++++++++++++++++------ include/exec/memory.h | 35 +++++++++++ include/hw/misc/mmio_interface.h | 49 +++++++++++++++ include/hw/qdev-properties.h | 2 + memory.c | 57 +++++++++++++++++ 8 files changed, 372 insertions(+), 55 deletions(-) create mode 100644 hw/misc/mmio_interface.c create mode 100644 include/hw/misc/mmio_interface.h
Signature · 1 line
1.8.3.1
PATCH v2 1/7 cputlb: cleanup get_page_addr_code to use VICTIM_TLB_HIT
From: KONRAD Frederic <[email protected]>
This replaces env1 and page_index variables by env and index so we can use VICTIM_TLB_HIT macro later.
--- cputlb.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
cputlb.c +9 -9
@@ -457,21 +457,21 @@ static void report_bad_exec(CPUState *cpu, target_ulong addr)* is actually a ram_addr_t (in system mode; the user mode emulation * version of this function returns a guest virtual address). */ -tb_page_addr_t get_page_addr_code (CPUArchState *env1,target_ulong addr) +tb_page_addr_t get_page_addr_code (CPUArchState *env,target_ulong addr) { -int mmu_idx, page_index, pd; +int mmu_idx, index, pd;void *p;MemoryRegion *mr; -CPUState *cpu =ENV_GET_CPU (env1); +CPUState *cpu =ENV_GET_CPU (env);CPUIOTLBEntry *iotlbentry; - page_index = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); - mmu_idx =cpu_mmu_index (env1, true); -if (unlikely (env1->tlb_table[mmu_idx][page_index].addr_code != + index = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); + mmu_idx =cpu_mmu_index (env, true); +if (unlikely (env->tlb_table[mmu_idx][index].addr_code != (addr &TARGET_PAGE_MASK ))) { -cpu_ldub_code (env1, addr); +cpu_ldub_code (env, addr); } - iotlbentry = &env1->iotlb[mmu_idx][page_index]; + iotlbentry = &env->iotlb[mmu_idx][index]; pd = iotlbentry->addr & ~TARGET_PAGE_MASK ; mr =iotlb_to_region (cpu, pd, iotlbentry->attrs);if (memory_region_is_unassigned (mr)) {
@@ -484,7 +484,7 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)exit (1 ); } } - p = (void *)((uintptr_t )addr + env1->tlb_table[mmu_idx][page_index].addend); + p = (void *)((uintptr_t )addr + env->tlb_table[mmu_idx][index].addend);return qemu_ram_addr_from_host_nofail (p); }
Signature · 1 line
1.8.3.1
PATCH v2 2/7 cputlb: move get_page_addr_code
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This just moves the code before VICTIM_TLB_HIT macro definition so we can use it.
--- cputlb.c | 72 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 36 deletions(-)
cputlb.c +36 -36
@@ -452,42 +452,6 @@ static void report_bad_exec(CPUState *cpu, target_ulong addr)log_cpu_state_mask (LOG_GUEST_ERROR , cpu,CPU_DUMP_FPU |CPU_DUMP_CCOP ); } -/* NOTE: this function can trigger an exception */ -/* NOTE2: the returned address is not exactly the physical address: it -* is actually a ram_addr_t (in system mode; the user mode emulation -* version of this function returns a guest virtual address). -*/ -tb_page_addr_t get_page_addr_code (CPUArchState *env,target_ulong addr) -{ -int mmu_idx, index, pd; -void *p; -MemoryRegion *mr; -CPUState *cpu =ENV_GET_CPU (env); -CPUIOTLBEntry *iotlbentry; - - index = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); - mmu_idx =cpu_mmu_index (env, true); -if (unlikely (env->tlb_table[mmu_idx][index].addr_code != - (addr &TARGET_PAGE_MASK ))) { -cpu_ldub_code (env, addr); - } - iotlbentry = &env->iotlb[mmu_idx][index]; - pd = iotlbentry->addr & ~TARGET_PAGE_MASK ; - mr =iotlb_to_region (cpu, pd, iotlbentry->attrs); -if (memory_region_is_unassigned (mr)) { -CPUClass *cc =CPU_GET_CLASS (cpu); - -if (cc->do_unassigned_access) { - cc->do_unassigned_access (cpu, addr, false, true,0 ,4 ); - }else { -report_bad_exec (cpu, addr); -exit (1 ); - } - } - p = (void *)((uintptr_t )addr + env->tlb_table[mmu_idx][index].addend); -return qemu_ram_addr_from_host_nofail (p); -} -static uint64_t io_readx (CPUArchState *env,CPUIOTLBEntry *iotlbentry,target_ulong addr,uintptr_t retaddr,int size) {
@@ -554,6 +518,42 @@ static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index,victim_tlb_hit (env, mmu_idx, index, offsetof(CPUTLBEntry , TY), \ (ADDR ) &TARGET_PAGE_MASK ) +/* NOTE: this function can trigger an exception */ +/* NOTE2: the returned address is not exactly the physical address: it +* is actually a ram_addr_t (in system mode; the user mode emulation +* version of this function returns a guest virtual address). + */ +tb_page_addr_t get_page_addr_code (CPUArchState *env,target_ulong addr) +{ +int mmu_idx, index, pd; +void *p; +MemoryRegion *mr; +CPUState *cpu =ENV_GET_CPU (env); +CPUIOTLBEntry *iotlbentry; + + index = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); + mmu_idx =cpu_mmu_index (env, true); +if (unlikely (env->tlb_table[mmu_idx][index].addr_code != + (addr &TARGET_PAGE_MASK ))) { +cpu_ldub_code (env, addr); + } + iotlbentry = &env->iotlb[mmu_idx][index]; + pd = iotlbentry->addr & ~TARGET_PAGE_MASK ; + mr =iotlb_to_region (cpu, pd, iotlbentry->attrs); +if (memory_region_is_unassigned (mr)) { +CPUClass *cc =CPU_GET_CLASS (cpu); + +if (cc->do_unassigned_access) { + cc->do_unassigned_access (cpu, addr, false, true,0 ,4 ); + }else { +report_bad_exec (cpu, addr); +exit (1 ); + } + } + p = (void *)((uintptr_t )addr + env->tlb_table[mmu_idx][index].addend); +return qemu_ram_addr_from_host_nofail (p); +} +/* Probe for whether the specified guest write access is permitted. * If it is not permitted then an exception will be taken in the same * way as if this were a real write access (and we will not return).
Signature · 1 line
1.8.3.1
PATCH v2 3/7 cputlb: fix the way get_page_addr_code fills the tlb
In reply to [email protected]From: KONRAD Frederic <[email protected]>
get_page_addr_code(..) does a cpu_ldub_code to fill the tlb:
This can lead to some side effects if a device is mapped at this address.
So this patch replaces the cpu_memory_ld by a tlb_fill.
--- cputlb.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
cputlb.c +4 -2
@@ -534,8 +534,10 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr) index = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); mmu_idx =cpu_mmu_index (env, true);if (unlikely (env->tlb_table[mmu_idx][index].addr_code != - (addr &TARGET_PAGE_MASK ))) { -cpu_ldub_code (env, addr); + (addr & (TARGET_PAGE_MASK |TLB_INVALID_MASK )))) { +if (!VICTIM_TLB_HIT (addr_read, addr)) { +tlb_fill (ENV_GET_CPU (env), addr,MMU_INST_FETCH , mmu_idx,0 ); + } } iotlbentry = &env->iotlb[mmu_idx][index]; pd = iotlbentry->addr & ~TARGET_PAGE_MASK ;
Signature · 1 line
1.8.3.1
PATCH v2 4/7 exec: allow to get a pointer for some mmio memory region
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This introduces a special callback which allows to run code from some MMIO devices.
SysBusDevice with a MemoryRegion which implements the request_ptr callback will be notified when the guest try to execute code from their offset. Then it will be able to eg: pre-load some code from an SPI device or ask a pointer from an external simulator, etc..
When the pointer or the data in it are no longer valid the device has to invalidate it.
RFC -> V1:
* Use mmio-interface instead of directly creating the subregion.
--- cputlb.c | 7 +++++++ include/exec/memory.h | 35 +++++++++++++++++++++++++++++++ memory.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+)
cputlb.c +7 -0
@@ -545,6 +545,13 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)if (memory_region_is_unassigned (mr)) {CPUClass *cc =CPU_GET_CLASS (cpu); +if (memory_region_request_mmio_ptr (mr, addr)) { +/* A MemoryRegion is potentially added so re-run the +* get_page_addr_code. +*/ +return get_page_addr_code (env, addr); + } +if (cc->do_unassigned_access) { cc->do_unassigned_access (cpu, addr, false, true,0 ,4 ); }else {
include/exec/memory.h +35 -0
@@ -120,6 +120,15 @@ struct MemoryRegionOps {uint64_t data, unsigned size, MemTxAttrs attrs); +/* Instruction execution pre-callback: +* @addr is the address of the access relative to the @mr. +* @size is the size of the area returned by the callback. +* @offset is the location of the pointer inside @mr. +* +* Returns a pointer to a location which contains guest code. +*/ +void *(*request_ptr)(void *opaque,hwaddr addr,unsigned *size, +unsigned *offset);enum device_endian endianness;/* Guest-visible constraints: */
@@ -1253,6 +1262,32 @@ void memory_global_dirty_log_stop(void);void mtree_info (fprintf_function mon_printf,void *f,bool flatview);/** +* memory_region_request_mmio_ptr: request a pointer to an mmio +* MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer. +* When the device wants to invalidate the pointer it will call +* memory_region_invalidate_mmio_ptr. +* +* @mr: #MemoryRegion to check +* @addr: address within that region +* +* Returns true on success, false otherwise. +*/ +bool memory_region_request_mmio_ptr (MemoryRegion *mr,hwaddr addr); + +/** +* memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio +* previously requested. +* In the end that means that if something wants to execute from this area it +* will need to request the pointer again. +* +* @mr: #MemoryRegion associated to the pointer. +* @addr: address within that region +* @size: size of that area. +*/ +void memory_region_invalidate_mmio_ptr (MemoryRegion *mr,hwaddr offset, +unsigned size); + +/** * memory_region_dispatch_read: perform a read directly to the specified * MemoryRegion. *
memory.c +57 -0
69 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH V2 4/7] exec: allow to get a pointer for some mmio memory region
Quoted 10 lines · On Fri, Feb 17, 2017 at 09:17:10PM +0100, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This introduces a special callback which allows to run code from some MMIO devices.
SysBusDevice with a MemoryRegion which implements the
request_ptrcallback will be notified when the guest try to execute code from their offset. Then it will be able to eg: pre-load some code from an SPI device or ask a pointer from an external simulator, etc..
Quoted 7 lines
When the pointer or the data in it are no longer valid the device has to invalidate it.
Signed-off-by KONRAD Frederic <[email protected]>RFC -> V1:
* Use mmio-interface instead of directly creating the subregion.
Hi Fred,
Quoted 130 lines
--- cputlb.c | 7 +++++++ include/exec/memory.h | 35 +++++++++++++++++++++++++++++++ memory.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+)cputlb.c +7
-0@@ -545,6 +545,13 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)if (memory_region_is_unassigned (mr)) {CPUClass *cc =CPU_GET_CLASS (cpu); +if (memory_region_request_mmio_ptr (mr, addr)) { +/* A MemoryRegion is potentially added so re-run the +* get_page_addr_code. +*/ +return get_page_addr_code (env, addr); + } +if (cc->do_unassigned_access) { cc->do_unassigned_access (cpu, addr, false, true,0 ,4 ); }else {include/exec/memory.h +35
-0@@ -120,6 +120,15 @@ struct MemoryRegionOps {uint64_t data, unsigned size, MemTxAttrs attrs); +/* Instruction execution pre-callback: +* @addr is the address of the access relative to the @mr. +* @size is the size of the area returned by the callback. +* @offset is the location of the pointer inside @mr. +* +* Returns a pointer to a location which contains guest code. +*/ +void *(*request_ptr)(void *opaque,hwaddr addr,unsigned *size, +unsigned *offset);enum device_endian endianness;/* Guest-visible constraints: */ @@ -1253,6 +1262,32 @@ void memory_global_dirty_log_stop(void);void mtree_info (fprintf_function mon_printf,void *f,bool flatview);/** +* memory_region_request_mmio_ptr: request a pointer to an mmio +* MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer. +* When the device wants to invalidate the pointer it will call +* memory_region_invalidate_mmio_ptr. +* +* @mr: #MemoryRegion to check +* @addr: address within that region +* +* Returns true on success, false otherwise. +*/ +bool memory_region_request_mmio_ptr (MemoryRegion *mr,hwaddr addr); + +/** +* memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio +* previously requested. +* In the end that means that if something wants to execute from this area it +* will need to request the pointer again. +* +* @mr: #MemoryRegion associated to the pointer. +* @addr: address within that region +* @size: size of that area. +*/ +void memory_region_invalidate_mmio_ptr (MemoryRegion *mr,hwaddr offset, +unsigned size); + +/** * memory_region_dispatch_read: perform a read directly to the specified * MemoryRegion. * memory.c +38
-0@@ -30,6 +30,8 @@#include "exec/ram_addr.h" #include "sysemu/kvm.h" #include "sysemu/sysemu.h" +#include "hw/misc/mmio_interface.h" +#include "hw/qdev-properties.h" //#define DEBUG_UNASSIGNED @@ -2375,6 +2377,61 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&listener->address_space->listeners, listener, link_as); } +bool memory_region_request_mmio_ptr (MemoryRegion *mr,hwaddr addr) +{ +void *host; +unsigned size =0 ; +unsigned offset =0 ; +Object *new_interface; + +if (!mr || !mr->ops->request_ptr) { +return false; + } + +/* +* Avoid an update if the request_ptr call +* memory_region_invalidate_mmio_ptr which seems to be likely when we use +* a cache. +*/ +memory_region_transaction_begin (); + + host = mr->ops->request_ptr (mr->opaque, addr - mr->addr, &size, &offset); + +if (!host || !size) { +memory_region_transaction_commit (); +return false; + } + + new_interface =object_new ("mmio_interface" ); +qdev_prop_set_uint64 (DEVICE (new_interface),"start" , offset); +qdev_prop_set_uint64 (DEVICE (new_interface),"end" , offset + size -1 ); +qdev_prop_set_bit (DEVICE (new_interface),"ro" , true); +qdev_prop_set_ptr (DEVICE (new_interface),"host_ptr" , host); +qdev_prop_set_ptr (DEVICE (new_interface),"subregion" , mr); +object_property_set_bool (OBJECT (new_interface), true,"realized" ,NULL ); + +memory_region_transaction_commit (); +return true; +}
memory.c void memory_listener_unregister(MemoryListener *listener) · line 2425
Quoted 10 lines
memory.c +10
-0+ +void memory_region_invalidate_mmio_ptr (MemoryRegion *mr,hwaddr offset, +unsigned size) +{ +MemoryRegionSection section =memory_region_find (mr, offset, size); + +if (section.mr != mr) { +/* memory_region_find add a ref on section.mr */ +memory_region_unref (section.mr); +if (!MMIO_INTERFACE (section.mr->owner)) {
Is MMIO_INTERFACE defined yet?
This may break bisection...
Cheers,
Edgar
Quoted 16 lines
memory.c +9
-0+return ; + } +/* We found the interface just drop it. */ +object_property_set_bool (section.mr->owner, false,"realized" ,NULL ); +object_unref (section.mr->owner); +object_unparent (section.mr->owner); + } +} +void address_space_init (AddressSpace *as,MemoryRegion *root,const char *name) {memory_region_ref (root);Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH V2 4/7] exec: allow to get a pointer for some mmio memory region
Quoted 159 lines · Edgar E. Iglesias
Quoted 10 lines · On Fri, Feb 17, 2017 at 09:17:10PM +0100, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This introduces a special callback which allows to run code from some MMIO devices.
SysBusDevice with a MemoryRegion which implements the
request_ptrcallback will be notified when the guest try to execute code from their offset. Then it will be able to eg: pre-load some code from an SPI device or ask a pointer from an external simulator, etc..Quoted 7 lines
When the pointer or the data in it are no longer valid the device has to invalidate it.
Signed-off-by KONRAD Frederic <[email protected]>RFC -> V1:
* Use mmio-interface instead of directly creating the subregion.Hi Fred,
Quoted 135 lines
--- cputlb.c | 7 +++++++ include/exec/memory.h | 35 +++++++++++++++++++++++++++++++ memory.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+)cputlb.c +7
-0@@ -545,6 +545,13 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr)if (memory_region_is_unassigned (mr)) {CPUClass *cc =CPU_GET_CLASS (cpu); +if (memory_region_request_mmio_ptr (mr, addr)) { +/* A MemoryRegion is potentially added so re-run the +* get_page_addr_code. +*/ +return get_page_addr_code (env, addr); + } +if (cc->do_unassigned_access) { cc->do_unassigned_access (cpu, addr, false, true,0 ,4 ); }else {include/exec/memory.h +35
-0@@ -120,6 +120,15 @@ struct MemoryRegionOps {uint64_t data, unsigned size, MemTxAttrs attrs); +/* Instruction execution pre-callback: +* @addr is the address of the access relative to the @mr. +* @size is the size of the area returned by the callback. +* @offset is the location of the pointer inside @mr. +* +* Returns a pointer to a location which contains guest code. +*/ +void *(*request_ptr)(void *opaque,hwaddr addr,unsigned *size, +unsigned *offset);enum device_endian endianness;/* Guest-visible constraints: */ @@ -1253,6 +1262,32 @@ void memory_global_dirty_log_stop(void);void mtree_info (fprintf_function mon_printf,void *f,bool flatview);/** +* memory_region_request_mmio_ptr: request a pointer to an mmio +* MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer. +* When the device wants to invalidate the pointer it will call +* memory_region_invalidate_mmio_ptr. +* +* @mr: #MemoryRegion to check +* @addr: address within that region +* +* Returns true on success, false otherwise. +*/ +bool memory_region_request_mmio_ptr (MemoryRegion *mr,hwaddr addr); + +/** +* memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio +* previously requested. +* In the end that means that if something wants to execute from this area it +* will need to request the pointer again. +* +* @mr: #MemoryRegion associated to the pointer. +* @addr: address within that region +* @size: size of that area. +*/ +void memory_region_invalidate_mmio_ptr (MemoryRegion *mr,hwaddr offset, +unsigned size); + +/** * memory_region_dispatch_read: perform a read directly to the specified * MemoryRegion. * memory.c +43
-0@@ -30,6 +30,8 @@#include "exec/ram_addr.h" #include "sysemu/kvm.h" #include "sysemu/sysemu.h" +#include "hw/misc/mmio_interface.h" +#include "hw/qdev-properties.h" //#define DEBUG_UNASSIGNED @@ -2375,6 +2377,61 @@ void memory_listener_unregister(MemoryListener *listener)QTAILQ_REMOVE (&listener->address_space->listeners, listener, link_as); } +bool memory_region_request_mmio_ptr (MemoryRegion *mr,hwaddr addr) +{ +void *host; +unsigned size =0 ; +unsigned offset =0 ; +Object *new_interface; + +if (!mr || !mr->ops->request_ptr) { +return false; + } + +/* +* Avoid an update if the request_ptr call +* memory_region_invalidate_mmio_ptr which seems to be likely when we use +* a cache. +*/ +memory_region_transaction_begin (); + + host = mr->ops->request_ptr (mr->opaque, addr - mr->addr, &size, &offset); + +if (!host || !size) { +memory_region_transaction_commit (); +return false; + } + + new_interface =object_new ("mmio_interface" ); +qdev_prop_set_uint64 (DEVICE (new_interface),"start" , offset); +qdev_prop_set_uint64 (DEVICE (new_interface),"end" , offset + size -1 ); +qdev_prop_set_bit (DEVICE (new_interface),"ro" , true); +qdev_prop_set_ptr (DEVICE (new_interface),"host_ptr" , host); +qdev_prop_set_ptr (DEVICE (new_interface),"subregion" , mr); +object_property_set_bool (OBJECT (new_interface), true,"realized" ,NULL ); + +memory_region_transaction_commit (); +return true; +} + +void memory_region_invalidate_mmio_ptr (MemoryRegion *mr,hwaddr offset, +unsigned size) +{ +MemoryRegionSection section =memory_region_find (mr, offset, size);
Quoted 8 lines
memory.c void memory_listener_unregister(MemoryListener *listener) · line 2425
Quoted 5 lines
memory.c +5
-0+ +if (section.mr != mr) { +/* memory_region_find add a ref on section.mr */ +memory_region_unref (section.mr); +if (!MMIO_INTERFACE (section.mr->owner)) {Is
MMIO_INTERFACEdefined yet?
This may break bisection...
OOPS, your right :(..
I'll resend with the right order!
Thanks!
Fred
Quoted 21 lines
Cheers,
EdgarQuoted 16 lines
memory.c +9
-0+return ; + } +/* We found the interface just drop it. */ +object_property_set_bool (section.mr->owner, false,"realized" ,NULL ); +object_unref (section.mr->owner); +object_unparent (section.mr->owner); + } +} +void address_space_init (AddressSpace *as,MemoryRegion *root,const char *name) {memory_region_ref (root);Signature · 1 line
1.8.3.1
PATCH v2 5/7 qdev: add MemoryRegion property
In reply to [email protected]From: KONRAD Frederic <[email protected]>
We need to pass a pointer to a MemoryRegion for mmio_interface. So this just adds that.
--- include/hw/qdev-properties.h | 2 ++ 1 file changed, 2 insertions(+)
include/hw/qdev-properties.h +2 -0
@@ -171,6 +171,8 @@ extern PropertyInfo qdev_prop_arraylen; DEFINE_PROP_DEFAULT(_n, _s, _f, 0, qdev_prop_blocksize, uint16_t)#define DEFINE_PROP_PCI_HOST_DEVADDR (_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress) +#define DEFINE_PROP_MEMORY_REGION (_n, _s, _f) \ + DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, MemoryRegion *)#define DEFINE_PROP_END_OF_LIST () \ {}
Signature · 1 line
1.8.3.1
PATCH v2 6/7 introduce mmio_interface
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This introduces mmio_interface object which contains a MemoryRegion and can be hotplugged/hotunplugged.
V1 -> V2:
* Fix the qemu_log format.
--- hw/misc/Makefile.objs | 1 + hw/misc/mmio_interface.c | 128 +++++++++++++++++++++++++++++++++++++++ include/hw/misc/mmio_interface.h | 49 +++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 hw/misc/mmio_interface.c create mode 100644 include/hw/misc/mmio_interface.h
hw/misc/Makefile.objs +1 -0
@@ -55,3 +55,4 @@ obj-$(CONFIG_EDU) += edu.o obj-$(CONFIG_HYPERV_TESTDEV) += hyperv_testdev.o obj-$(CONFIG_AUX) += auxbus.o obj-$(CONFIG_ASPEED_SOC) += aspeed_scu.o aspeed_sdmc.o +obj-y += mmio_interface.o
hw/misc/mmio_interface.c new file +128 -0
128 lines not on this page · raw message
include/hw/misc/mmio_interface.h new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 7/7 xilinx_spips: allow mmio execution
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This allows to execute from the lqspi area.
When the request_ptr is called the device loads 1024bytes from the SPI device. Then this code can be executed by the guest.
--- hw/ssi/xilinx_spips.c | 74 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 19 deletions(-)
hw/ssi/xilinx_spips.c +55 -19
@@ -496,6 +496,18 @@ static const MemoryRegionOps spips_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; +static void xilinx_qspips_invalidate_mmio_ptr (XilinxQSPIPS *q) +{ +XilinxSPIPS *s = &q->parent_obj; + +if (q->lqspi_cached_addr != ~0ULL ) { +/* Invalidate the current mapped mmio */ +memory_region_invalidate_mmio_ptr (&s->mmlqspi, q->lqspi_cached_addr, +LQSPI_CACHE_SIZE ); + q->lqspi_cached_addr = ~0ULL ; + } +} +static void xilinx_qspips_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {
@@ -505,7 +517,7 @@ static void xilinx_qspips_write(void *opaque, hwaddr addr, addr >>=2 ;if (addr ==R_LQSPI_CFG ) { - q->lqspi_cached_addr = ~0ULL ; +xilinx_qspips_invalidate_mmio_ptr (q); } }
@@ -517,27 +529,20 @@ static const MemoryRegionOps qspips_ops = {#define LQSPI_CACHE_SIZE 1024 -static uint64_t -lqspi_read (void *opaque,hwaddr addr,unsigned int size) +static void lqspi_load_cache (void *opaque,hwaddr addr) { -int i;XilinxQSPIPS *q = opaque;XilinxSPIPS *s = opaque; -uint32_t ret; - -if (addr >= q->lqspi_cached_addr && - addr <= q->lqspi_cached_addr +LQSPI_CACHE_SIZE -4 ) { -uint8_t *retp = &q->lqspi_buf[addr - q->lqspi_cached_addr]; - ret =cpu_to_le32 (*(uint32_t *)retp); -DB_PRINT_L (1 ,"addr: %08x, data: %08x\n" , (unsigned )addr, - (unsigned )ret); -return ret; - }else { -int flash_addr = (addr /num_effective_busses (s)); -int slave = flash_addr >>LQSPI_ADDRESS_BITS ; -int cache_entry =0 ; -uint32_t u_page_save = s->regs[R_LQSPI_STS ] & ~LQSPI_CFG_U_PAGE ; - +int i; +int flash_addr = ((addr & ~(LQSPI_CACHE_SIZE -1 )) + /num_effective_busses (s)); +int slave = flash_addr >>LQSPI_ADDRESS_BITS ; +int cache_entry =0 ; +uint32_t u_page_save = s->regs[R_LQSPI_STS ] & ~LQSPI_CFG_U_PAGE ; + +if (addr < q->lqspi_cached_addr || + addr > q->lqspi_cached_addr +LQSPI_CACHE_SIZE -4 ) { +xilinx_qspips_invalidate_mmio_ptr (q); s->regs[R_LQSPI_STS ] &= ~LQSPI_CFG_U_PAGE ; s->regs[R_LQSPI_STS ] |= slave ?LQSPI_CFG_U_PAGE :0 ;
@@ -589,12 +594,43 @@ lqspi_read(void *opaque, hwaddr addr, unsigned int size)xilinx_spips_update_cs_lines (s); q->lqspi_cached_addr = flash_addr *num_effective_busses (s); + } +} + +static void *lqspi_request_mmio_ptr (void *opaque,hwaddr addr,unsigned *size, +unsigned *offset) +{ +XilinxQSPIPS *q = opaque; +hwaddr offset_within_the_region = addr & ~(LQSPI_CACHE_SIZE -1 ); + +lqspi_load_cache (opaque, offset_within_the_region); + *size =LQSPI_CACHE_SIZE ; + *offset = offset_within_the_region; +return q->lqspi_buf; +} + +static uint64_t +lqspi_read (void *opaque,hwaddr addr,unsigned int size) +{ +XilinxQSPIPS *q = opaque; +uint32_t ret; + +if (addr >= q->lqspi_cached_addr && + addr <= q->lqspi_cached_addr +LQSPI_CACHE_SIZE -4 ) { +uint8_t *retp = &q->lqspi_buf[addr - q->lqspi_cached_addr]; + ret =cpu_to_le32 (*(uint32_t *)retp); +DB_PRINT_L (1 ,"addr: %08x, data: %08x\n" , (unsigned )addr, + (unsigned )ret); +return ret; + }else { +lqspi_load_cache (opaque, addr);return lqspi_read (opaque, addr, size); } }static const MemoryRegionOps lqspi_ops = { .read = lqspi_read, + .request_ptr = lqspi_request_mmio_ptr, .endianness =DEVICE_NATIVE_ENDIAN , .valid = { .min_access_size =1 ,
Signature · 1 line
1.8.3.1
Ping!
Would be nice for us if we can get this into 2.9.
Thanks,
Fred
Quoted 46 lines · Le 17/02/2017 à 21:17, [email protected] a écrit :
From: KONRAD Frederic <[email protected]>
This series allows to execute code from mmio areas.
The main goal of this is to be able to run code for example from an SPI device.The three first patch fixes the way
get_page_addr_codefills the TLB.The fourth patch implements the mmio execution helpers: the device must implement the
request_ptrcallback of the MemoryRegion and will be notified when the guest wants to execute code from it.The fifth patch introduces
mmio_interfacedevice which allows to dynamically map a host pointer somewhere into the memory.The sixth patch implements the execution from the SPI memories in the
xilinx_spipsmodel.Thanks,
FredV1 -> V2:
* Fix the DPRINTF error.
RFC -> V1:
* Use an interface (mmio-interface) to fix any reference leak issue.KONRAD Frederic (7):
cputlb: cleanupget_page_addr_codeto useVICTIM_TLB_HIT
cputlb: moveget_page_addr_code
cputlb: fix the wayget_page_addr_codefills the tlb
exec: allow to get a pointer for some mmio memory region
qdev: add MemoryRegion property
introducemmio_interface
xilinx_spips: allow mmio executioncputlb.c | 81 ++++++++++++++----------- hw/misc/Makefile.objs | 1 + hw/misc/mmio_interface.c | 128 +++++++++++++++++++++++++++++++++++++++ hw/ssi/xilinx_spips.c | 74 ++++++++++++++++------ include/exec/memory.h | 35 +++++++++++ include/hw/misc/mmio_interface.h | 49 +++++++++++++++ include/hw/qdev-properties.h | 2 + memory.c | 57 +++++++++++++++++ 8 files changed, 372 insertions(+), 55 deletions(-) create mode 100644 hw/misc/mmio_interface.c create mode 100644 include/hw/misc/mmio_interface.h
Quoted 3 lines · KONRAD Frederic
Ping!
Would be nice for us if we can get this into 2.9.
Sorry, I've been much busier with icount than I would have liked. :(
Paolo
Quoted 54 lines
Thanks,
FredQuoted 50 lines · Le 17/02/2017 à 21:17, [email protected] a écrit :
From: KONRAD Frederic <[email protected]>
This series allows to execute code from mmio areas.
The main goal of this is to be able to run code for example from an SPI device.The three first patch fixes the way
get_page_addr_codefills the TLB.The fourth patch implements the mmio execution helpers: the device must implement the
request_ptrcallback of the MemoryRegion and will be notified when
the guest wants to execute code from it.The fifth patch introduces
mmio_interfacedevice which allows to dynamically
map a host pointer somewhere into the memory.The sixth patch implements the execution from the SPI memories in the
xilinx_spipsmodel.Thanks,
FredV1 -> V2:
* Fix the DPRINTF error.
RFC -> V1:
* Use an interface (mmio-interface) to fix any reference leak issue.KONRAD Frederic (7):
cputlb: cleanupget_page_addr_codeto useVICTIM_TLB_HIT
cputlb: moveget_page_addr_code
cputlb: fix the wayget_page_addr_codefills the tlb
exec: allow to get a pointer for some mmio memory region
qdev: add MemoryRegion property
introducemmio_interface
xilinx_spips: allow mmio executioncputlb.c | 81 ++++++++++++++----------- hw/misc/Makefile.objs | 1 + hw/misc/mmio_interface.c | 128 +++++++++++++++++++++++++++++++++++++++ hw/ssi/xilinx_spips.c | 74 ++++++++++++++++------ include/exec/memory.h | 35 +++++++++++ include/hw/misc/mmio_interface.h | 49 +++++++++++++++ include/hw/qdev-properties.h | 2 + memory.c | 57 +++++++++++++++++ 8 files changed, 372 insertions(+), 55 deletions(-) create mode 100644 hw/misc/mmio_interface.c create mode 100644 include/hw/misc/mmio_interface.h
Hi All,
Any feedback for the 4 last patches?
Thanks,
Fred
Quoted 46 lines · On 02/17/2017 09:17 PM, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This series allows to execute code from mmio areas.
The main goal of this is to be able to run code for example from an SPI device.The three first patch fixes the way
get_page_addr_codefills the TLB.The fourth patch implements the mmio execution helpers: the device must implement the
request_ptrcallback of the MemoryRegion and will be notified when the guest wants to execute code from it.The fifth patch introduces
mmio_interfacedevice which allows to dynamically map a host pointer somewhere into the memory.The sixth patch implements the execution from the SPI memories in the
xilinx_spipsmodel.Thanks,
FredV1 -> V2:
* Fix the DPRINTF error.
RFC -> V1:
* Use an interface (mmio-interface) to fix any reference leak issue.KONRAD Frederic (7):
cputlb: cleanupget_page_addr_codeto useVICTIM_TLB_HIT
cputlb: moveget_page_addr_code
cputlb: fix the wayget_page_addr_codefills the tlb
exec: allow to get a pointer for some mmio memory region
qdev: add MemoryRegion property
introducemmio_interface
xilinx_spips: allow mmio executioncputlb.c | 81 ++++++++++++++----------- hw/misc/Makefile.objs | 1 + hw/misc/mmio_interface.c | 128 +++++++++++++++++++++++++++++++++++++++ hw/ssi/xilinx_spips.c | 74 ++++++++++++++++------ include/exec/memory.h | 35 +++++++++++ include/hw/misc/mmio_interface.h | 49 +++++++++++++++ include/hw/qdev-properties.h | 2 + memory.c | 57 +++++++++++++++++ 8 files changed, 372 insertions(+), 55 deletions(-) create mode 100644 hw/misc/mmio_interface.c create mode 100644 include/hw/misc/mmio_interface.h
Quoted 11 lines · On Fri, Feb 17, 2017 at 09:17:06PM +0100, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This series allows to execute code from mmio areas.
The main goal of this is to be able to run code for example from an SPI device.The three first patch fixes the way
get_page_addr_codefills the TLB.The fourth patch implements the mmio execution helpers: the device must implement the
request_ptrcallback of the MemoryRegion and will be notified when the guest wants to execute code from it.
Quoted 5 lines
The fifth patch introduces
mmio_interfacedevice which allows to dynamically map a host pointer somewhere into the memory.The sixth patch implements the execution from the SPI memories in the
xilinx_spipsmodel.
I had a comment on the possible break of bisection, but the series looks good to me. If you fix up the ordering:
Cheers,
Edgar
Quoted 34 lines
Thanks,
FredV1 -> V2:
* Fix the DPRINTF error.
RFC -> V1:
* Use an interface (mmio-interface) to fix any reference leak issue.KONRAD Frederic (7):
cputlb: cleanupget_page_addr_codeto useVICTIM_TLB_HIT
cputlb: moveget_page_addr_code
cputlb: fix the wayget_page_addr_codefills the tlb
exec: allow to get a pointer for some mmio memory region
qdev: add MemoryRegion property
introducemmio_interface
xilinx_spips: allow mmio executioncputlb.c | 81 ++++++++++++++----------- hw/misc/Makefile.objs | 1 + hw/misc/mmio_interface.c | 128 +++++++++++++++++++++++++++++++++++++++ hw/ssi/xilinx_spips.c | 74 ++++++++++++++++------ include/exec/memory.h | 35 +++++++++++ include/hw/misc/mmio_interface.h | 49 +++++++++++++++ include/hw/qdev-properties.h | 2 + memory.c | 57 +++++++++++++++++ 8 files changed, 372 insertions(+), 55 deletions(-) create mode 100644 hw/misc/mmio_interface.c create mode 100644 include/hw/misc/mmio_interface.hSignature · 1 line
1.8.3.1