From: Oleksandr Tyshchenko <[email protected]>
Date: Sat, 28 Nov 2020 22:33:51 +0200
Subject: [PATCH V3 00/23] IOREQ feature (+ virtio-mmio) on Arm
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Hello all.
The purpose of this patch series is to add IOREQ/DM support to Xen on Arm. You can find an initial discussion at [1] and RFC/V1/V2 series at [2]/[3]/[4]. Xen on Arm requires some implementation to forward guest MMIO access to a device model in order to implement virtio-mmio backend or even mediator outside of hypervisor. As Xen on x86 already contains required support this series tries to make it common and introduce Arm specific bits plus some new functionality. Patch series is based on Julien's PoC "xen/arm: Add support for Guest IO forwarding to a device emulator". Besides splitting existing IOREQ/DM support and introducing Arm side, the series also includes virtio-mmio related changes (last 2 patches for toolstack) for the reviewers to be able to see how the whole picture could look like.
According to the initial discussion there are a few open questions/concerns regarding security, performance in VirtIO solution:
1. virtio-mmio vs virtio-pci, SPI vs MSI, different use-cases require different transport...
2. virtio backend is able to access all guest memory, some kind of protection is needed: 'virtio-iommu in Xen' vs 'pre-shared-memory & memcpys in guest'
3. interface between toolstack and 'out-of-qemu' virtio backend, avoid using Xenstore in virtio backend if possible.
4. a lot of 'foreing mapping' could lead to the memory exhaustion, Julien has some idea regarding that.
Looks like all of them are valid and worth considering, but the first thing which we need on Arm is a mechanism to forward guest IO to a device emulator, so let's focus on it in the first place.
***
There are a lot of changes since RFC series, almost all TODOs were resolved on Arm, Arm code was improved and hardened, common IOREQ/DM code became really arch-agnostic (without HVM-ism), the "legacy" mechanism of mapping magic pages for the IOREQ servers was left x86 specific, etc. But one TODO still remains which is "PIO handling" on Arm. The "PIO handling" TODO is expected to left unaddressed for the current series. It is not an big issue for now while Xen doesn't have support for vPCI on Arm. On Arm64 they are only used for PCI IO Bar and we would probably want to expose them to emulator as PIO access to make a DM completely arch-agnostic. So "PIO handling" should be implemented when we add support for vPCI.
I left interface untouched in the following patch "xen/dm: Introduce xendevicemodel_set_irq_level DM op" since there is still an open discussion what interface to use/what information to pass to the hypervisor.
There is a patch on review this series depends on:
https://patchwork.kernel.org/patch/11816689
Please note, that IOREQ feature is disabled by default on Arm within current series.
***
Patch series [5] was rebased on recent "staging branch"
(181f2c2 evtchn: double per-channel locking can't hit identical channels) and tested on Renesas Salvator-X board + H3 ES3.0 SoC (Arm64) with virtio-mmio disk backend [6] running in driver domain and unmodified Linux Guest running on existing virtio-blk driver (frontend). No issues were observed. Guest domain 'reboot/destroy' use-cases work properly. Patch series was only build-tested on x86.
Please note, build-test passed for the following modes:
1. x86: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y (default)
2. x86: #CONFIG_HVM is not set / #CONFIG_IOREQ_SERVER is not set
3. Arm64: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y
4. Arm64: CONFIG_HVM=y / #CONFIG_IOREQ_SERVER is not set (default)
5. Arm32: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y
6. Arm32: CONFIG_HVM=y / #CONFIG_IOREQ_SERVER is not set (default)
***
Any feedback/help would be highly appreciated.
[1] https://lists.xenproject.org/archives/html/xen-devel/2020-07/msg00825.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2020-08/msg00071.html
[3] https://lists.xenproject.org/archives/html/xen-devel/2020-09/msg00732.html
[4] https://lists.xenproject.org/archives/html/xen-devel/2020-10/msg01077.html
[5] https://github.com/otyshchenko1/xen/commits/ioreq_4.14_ml4
[6] https://github.com/xen-troops/virtio-disk/commits/ioreq_ml1
Julien Grall (5):
xen/dm: Make x86's DM feature common
xen/mm: Make x86's XENMEM_resource_ioreq_server handling common arm/ioreq: Introduce arch specific bits for IOREQ/DM features xen/dm: Introduce xendevicemodel_set_irq_level DM op
libxl: Introduce basic virtio-mmio support on Arm
Oleksandr Tyshchenko (18):
x86/ioreq: Prepare IOREQ feature for making it common x86/ioreq: Add IOREQ_STATUS_* #define-s and update code for moving x86/ioreq: Provide out-of-line wrapper for the handle_mmio() xen/ioreq: Make x86's IOREQ feature common
xen/ioreq: Make x86's hvm_ioreq_needs_completion() common xen/ioreq: Make x86's hvm_mmio_first(last)_byte() common xen/ioreq: Make x86's hvm_ioreq_(page/vcpu/server) structs common xen/ioreq: Move x86's ioreq_server to struct domain xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu xen/ioreq: Remove "hvm" prefixes from involved function names xen/ioreq: Use guest_cmpxchg64() instead of cmpxchg() xen/arm: Stick around in leave_hypervisor_to_guest until I/O has
completed
xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm xen/ioreq: Introduce domain_has_ioreq_server()
xen/arm: io: Abstract sign-extension
xen/ioreq: Make x86's send_invalidate_req() common xen/arm: Add mapcache invalidation handling
[RFC] libxl: Add support for virtio-disk configuration
MAINTAINERS | 8 +- tools/include/xendevicemodel.h | 4 + tools/libs/devicemodel/core.c | 18 + tools/libs/devicemodel/libxendevicemodel.map | 1 + tools/libs/light/Makefile | 1 + tools/libs/light/libxl_arm.c | 94 +- tools/libs/light/libxl_create.c | 1 + tools/libs/light/libxl_internal.h | 1 + tools/libs/light/libxl_types.idl | 16 + tools/libs/light/libxl_types_internal.idl | 1 + tools/libs/light/libxl_virtio_disk.c | 109 +++ tools/xl/Makefile | 2 +- tools/xl/xl.h | 3 + tools/xl/xl_cmdtable.c | 15 + tools/xl/xl_parse.c | 116 +++ tools/xl/xl_virtio_disk.c | 46 + xen/arch/arm/Makefile | 2 + xen/arch/arm/dm.c | 89 ++ xen/arch/arm/domain.c | 9 + xen/arch/arm/hvm.c | 4 + xen/arch/arm/io.c | 29 +- xen/arch/arm/ioreq.c | 126 +++ xen/arch/arm/p2m.c | 48 +- xen/arch/arm/traps.c | 58 +- xen/arch/x86/Kconfig | 1 + xen/arch/x86/hvm/dm.c | 295 +----- xen/arch/x86/hvm/emulate.c | 80 +- xen/arch/x86/hvm/hvm.c | 12 +- xen/arch/x86/hvm/hypercall.c | 9 +- xen/arch/x86/hvm/intercept.c | 5 +- xen/arch/x86/hvm/io.c | 26 +- xen/arch/x86/hvm/ioreq.c | 1357 ++------------------------ xen/arch/x86/hvm/stdvga.c | 10 +- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/vmx/realmode.c | 6 +- xen/arch/x86/hvm/vmx/vvmx.c | 2 +- xen/arch/x86/mm.c | 46 +- xen/arch/x86/mm/p2m.c | 13 +- xen/arch/x86/mm/shadow/common.c | 2 +- xen/common/Kconfig | 3 + xen/common/Makefile | 2 + xen/common/dm.c | 292 ++++++ xen/common/ioreq.c | 1307 +++++++++++++++++++++++++ xen/common/memory.c | 73 +- xen/include/asm-arm/domain.h | 3 + xen/include/asm-arm/hvm/ioreq.h | 139 +++ xen/include/asm-arm/mm.h | 8 - xen/include/asm-arm/mmio.h | 1 + xen/include/asm-arm/p2m.h | 19 +- xen/include/asm-arm/traps.h | 24 + xen/include/asm-x86/hvm/domain.h | 43 - xen/include/asm-x86/hvm/emulate.h | 2 +- xen/include/asm-x86/hvm/io.h | 17 - xen/include/asm-x86/hvm/ioreq.h | 58 +- xen/include/asm-x86/hvm/vcpu.h | 18 - xen/include/asm-x86/mm.h | 4 - xen/include/asm-x86/p2m.h | 24 +- xen/include/public/arch-arm.h | 5 + xen/include/public/hvm/dm_op.h | 16 + xen/include/xen/dm.h | 44 + xen/include/xen/ioreq.h | 146 +++ xen/include/xen/p2m-common.h | 4 + xen/include/xen/sched.h | 32 + xen/include/xsm/dummy.h | 4 +- xen/include/xsm/xsm.h | 6 +- xen/xsm/dummy.c | 2 +- xen/xsm/flask/hooks.c | 5 +- 67 files changed, 3084 insertions(+), 1884 deletions(-) create mode 100644 tools/libs/light/libxl_virtio_disk.c create mode 100644 tools/xl/xl_virtio_disk.c create mode 100644 xen/arch/arm/dm.c create mode 100644 xen/arch/arm/ioreq.c create mode 100644 xen/common/dm.c create mode 100644 xen/common/ioreq.c create mode 100644 xen/include/asm-arm/hvm/ioreq.h create mode 100644 xen/include/xen/dm.h create mode 100644 xen/include/xen/ioreq.h
Signature · 1 line
2.7.4
PATCH v3 1/23 x86/ioreq: Prepare IOREQ feature for making it common
From: Oleksandr Tyshchenko <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch makes some preparation to x86/hvm/ioreq.c before moving to the common code. This way we will get a verbatim copy for a code movement in subsequent patch.
This patch mostly introduces specific hooks to abstract arch specific materials taking into the account the requirment to leave the "legacy" mechanism of mapping magic pages for the IOREQ servers x86 specific and not expose it to the common code.
These hooks are named according to the more consistent new naming scheme right away (including dropping the "hvm" prefixes and infixes):
- IOREQ server functions should start with "ioreq_server_"
- IOREQ functions should start with "ioreq_"
other functions will be renamed in subsequent patches.
It worth mentioning that a code which checks the return value of p2m_set_ioreq_server() in hvm_map_mem_type_to_ioreq_server() was folded into arch_ioreq_server_map_mem_type() for the clear split. So the p2m_change_entry_type_global() is called with ioreq_server lock held.
Also re-order #include-s alphabetically.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch, was split from:
"[RFC PATCH V1 01/12] hvm/ioreq: Make x86's IOREQ feature common"
- fold the check of p->type into hvm_get_ioreq_server_range_type() and make it return success/failure
- remove relocate_portio_handler() call from arch_hvm_ioreq_destroy()
in arch/x86/hvm/ioreq.c
- introduce arch_hvm_destroy_ioreq_server()/arch_handle_hvm_io_completion()
Changes V1 -> V2:
- update patch description
- make arch functions inline and put them into arch header
to achieve a truly rename by the subsequent patch
- return void in arch_hvm_destroy_ioreq_server()
- return bool in arch_hvm_ioreq_destroy()
- bring relocate_portio_handler() back to arch_hvm_ioreq_destroy()
- rename IOREQ_IO* to IOREQ_STATUS*
- remove *handle* from arch_handle_hvm_io_completion()
- re-order #include-s alphabetically
- rename hvm_get_ioreq_server_range_type() to hvm_ioreq_server_get_type_addr() and add "const" to several arguments
Changes V2 -> V3:
- update patch description
- name new arch hooks according to the new naming scheme
- don't make arch hooks inline, move them ioreq.c
- make get_ioreq_server() local again
- rework the whole patch taking into the account that "legacy" interface should remain x86 specific (additional arch hooks, etc)
- update the code to be able to use hvm_map_mem_type_to_ioreq_server() in the common code (an extra arch hook, etc)
- don’t include <asm/hvm/emulate.h> from arch header
- add "arch" prefix to hvm_ioreq_server_get_type_addr()
- move IOREQ_STATUS_* #define-s introduction to the separate patch
- move HANDLE_BUFIOREQ to the arch header
- just return relocate_portio_handler() from arch_ioreq_server_destroy_all()
- misc adjustments proposed by Jan (adding const, unsigned int instead of uint32_t)
---
--- xen/arch/x86/hvm/ioreq.c | 174 ++++++++++++++++++++++++++-------------- xen/include/asm-x86/hvm/ioreq.h | 19 +++++ 2 files changed, 133 insertions(+), 60 deletions(-)
xen/arch/x86/hvm/ioreq.c +114 -60
@@ -17,15 +17,15 @@*/ #include <xen/ctype.h> +#include <xen/domain.h> +#include <xen/event.h> #include <xen/init.h> +#include <xen/irq.h> #include <xen/lib.h> -#include <xen/trace.h> +#include <xen/paging.h> #include <xen/sched.h> -#include <xen/irq.h> #include <xen/softirq.h> -#include <xen/domain.h> -#include <xen/event.h> -#include <xen/paging.h> +#include <xen/trace.h> #include <xen/vpci.h> #include <asm/hvm/emulate.h>
@@ -170,6 +170,29 @@ static bool hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p)return true; } +bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion) +{ +switch ( io_completion ) + { +case HVMIO_realmode_completion: + { +struct hvm_emulate_ctxt ctxt; + +hvm_emulate_init_once (&ctxt,NULL ,guest_cpu_user_regs ()); +vmx_realmode_emulate_one (&ctxt); +hvm_emulate_writeback (&ctxt); + +break ; + } + +default : +ASSERT_UNREACHABLE (); +break ; + } + +return true; +} +bool handle_hvm_io_completion (struct vcpu *v) {struct domain *d = v->domain;
@@ -209,19 +232,8 @@ bool handle_hvm_io_completion(struct vcpu *v)return handle_pio (vio->io_req.addr, vio->io_req.size, vio->io_req.dir); -case HVMIO_realmode_completion: - { -struct hvm_emulate_ctxt ctxt; - -hvm_emulate_init_once (&ctxt,NULL ,guest_cpu_user_regs ()); -vmx_realmode_emulate_one (&ctxt); -hvm_emulate_writeback (&ctxt); - -break ; - }default : -ASSERT_UNREACHABLE (); -break ; +return arch_vcpu_ioreq_completion (io_completion); }return true;
@@ -477,9 +489,6 @@ static void hvm_update_ioreq_evtchn(struct hvm_ioreq_server *s, } } -#define HANDLE_BUFIOREQ (s) \ - ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) -static int hvm_ioreq_server_add_vcpu (struct hvm_ioreq_server *s,struct vcpu *v) {
@@ -586,7 +595,7 @@ static void hvm_ioreq_server_remove_all_vcpus(struct hvm_ioreq_server *s)spin_unlock (&s->lock); } -static int hvm_ioreq_server_map_pages (struct hvm_ioreq_server *s) +int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s) {int rc;
@@ -601,7 +610,7 @@ static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s)return rc; } -static void hvm_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) {hvm_unmap_ioreq_gfn (s, true);hvm_unmap_ioreq_gfn (s, false);
@@ -674,6 +683,12 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,return rc; } +void arch_ioreq_server_enable (struct hvm_ioreq_server *s) +{ +hvm_remove_ioreq_gfn (s, false); +hvm_remove_ioreq_gfn (s, true); +} +static void hvm_ioreq_server_enable (struct hvm_ioreq_server *s) {struct hvm_ioreq_vcpu *sv;
@@ -683,8 +698,7 @@ static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s)if ( s->enabled ) goto done; -hvm_remove_ioreq_gfn (s, false); -hvm_remove_ioreq_gfn (s, true); +arch_ioreq_server_enable (s); s->enabled = true;
@@ -697,6 +711,12 @@ static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s)spin_unlock (&s->lock); } +void arch_ioreq_server_disable (struct hvm_ioreq_server *s) +{ +hvm_add_ioreq_gfn (s, true); +hvm_add_ioreq_gfn (s, false); +} +static void hvm_ioreq_server_disable (struct hvm_ioreq_server *s) {spin_lock (&s->lock);
@@ -704,8 +724,7 @@ static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s)if ( !s->enabled ) goto done; -hvm_add_ioreq_gfn (s, true); -hvm_add_ioreq_gfn (s, false); +arch_ioreq_server_disable (s); s->enabled = false;
@@ -750,7 +769,7 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, fail_add:hvm_ioreq_server_remove_all_vcpus (s); -hvm_ioreq_server_unmap_pages (s); +arch_ioreq_server_unmap_pages (s);hvm_ioreq_server_free_rangesets (s);
@@ -764,7 +783,7 @@ static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s)hvm_ioreq_server_remove_all_vcpus (s);/* -* NOTE: It is safe to call both hvm_ioreq_server_unmap_pages() and +* NOTE: It is safe to call both arch_ioreq_server_unmap_pages() and * hvm_ioreq_server_free_pages() in that order. * This is because the former will do nothing if the pages * are not mapped, leaving the page to be freed by the latter.
@@ -772,7 +791,7 @@ static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s)* the page_info pointer to NULL, meaning the latter will do * nothing. */ -hvm_ioreq_server_unmap_pages (s); +arch_ioreq_server_unmap_pages (s);hvm_ioreq_server_free_pages (s);hvm_ioreq_server_free_rangesets (s);
@@ -836,6 +855,12 @@ int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling,return rc; } +/* Called when target domain is paused */ +void arch_ioreq_server_destroy (struct hvm_ioreq_server *s) +{ +p2m_set_ioreq_server (s->target,0 , s); +} +int hvm_destroy_ioreq_server (struct domain *d,ioservid_t id) {struct hvm_ioreq_server *s;
@@ -855,7 +880,7 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)domain_pause (d); -p2m_set_ioreq_server (d,0 , s); +arch_ioreq_server_destroy (s);hvm_ioreq_server_disable (s);
@@ -900,7 +925,7 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,if ( ioreq_gfn || bufioreq_gfn ) { - rc =hvm_ioreq_server_map_pages (s); + rc =arch_ioreq_server_map_pages (s);if ( rc ) goto out; }
@@ -1080,6 +1105,24 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,return rc; } +/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be
@@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); -if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -return rc; }
@@ -1210,12 +1245,17 @@ void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v)spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); } +bool arch_ioreq_server_destroy_all (struct domain *d) +{ +return relocate_portio_handler (d,0xcf8 ,0xcf8 ,4 ); +} +void hvm_destroy_all_ioreq_servers (struct domain *d) {struct hvm_ioreq_server *s;unsigned int id; -if ( !relocate_portio_handler (d,0xcf8 ,0xcf8 ,4 ) ) +if ( !arch_ioreq_server_destroy_all (d) )return ;spin_lock_recursive (&d->arch.hvm.ioreq_server.lock);
@@ -1239,33 +1279,28 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); } -struct hvm_ioreq_server *hvm_select_ioreq_server (struct domain *d, -ioreq_t *p) +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr) { -struct hvm_ioreq_server *s; -uint32_t cf8; -uint8_t type; -uint64_t addr; -unsigned int id; +unsigned int cf8 = d->arch.hvm.pci_cf8;if ( p->type !=IOREQ_TYPE_COPY && p->type !=IOREQ_TYPE_PIO ) -return NULL ; - - cf8 = d->arch.hvm.pci_cf8; +return -EINVAL ;if ( p->type ==IOREQ_TYPE_PIO && (p->addr & ~3 ) ==0xcfc &&CF8_ENABLED (cf8) ) { -uint32_t x86_fam; +unsigned int x86_fam, reg;pci_sbdf_t sbdf; -unsigned int reg; reg =hvm_pci_decode_addr (cf8, p->addr, &sbdf);/* PCI config data cycle */ - type =XEN_DMOP_IO_RANGE_PCI ; - addr = ((uint64_t )sbdf.sbdf <<32 ) | reg; + *type =XEN_DMOP_IO_RANGE_PCI ; + *addr = ((uint64_t )sbdf.sbdf <<32 ) | reg;/* AMD extended configuration space access? */ if ( CF8_ADDR_HI(cf8) && d->arch.cpuid->x86_vendor == X86_VENDOR_AMD &&
@@ -1277,16 +1312,30 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,if ( !rdmsr_safe (MSR_AMD64_NB_CFG , msr_val) && (msr_val & (1ULL <<AMD64_NB_CFG_CF8_EXT_ENABLE_BIT )) ) - addr |=CF8_ADDR_HI (cf8); + *addr |=CF8_ADDR_HI (cf8); } }else { - type = (p->type ==IOREQ_TYPE_PIO ) ? -XEN_DMOP_IO_RANGE_PORT :XEN_DMOP_IO_RANGE_MEMORY ; - addr = p->addr; + *type = (p->type ==IOREQ_TYPE_PIO ) ? +XEN_DMOP_IO_RANGE_PORT :XEN_DMOP_IO_RANGE_MEMORY ; + *addr = p->addr; } +return 0 ; +} + +struct hvm_ioreq_server *hvm_select_ioreq_server (struct domain *d, +ioreq_t *p) +{ +struct hvm_ioreq_server *s; +uint8_t type; +uint64_t addr; +unsigned int id; + +if (arch_ioreq_server_get_type_addr (d, p, &type, &addr) ) +return NULL ; + FOR_EACH_IOREQ_SERVER(d, id, s) {struct rangeset *r;
@@ -1515,11 +1564,16 @@ static int hvm_access_cf8(return X86EMUL_UNHANDLEABLE ; } +void arch_ioreq_domain_init (struct domain *d) +{ +register_portio_handler (d,0xcf8 ,4 , hvm_access_cf8); +} +void hvm_ioreq_init (struct domain *d) {spin_lock_init (&d->arch.hvm.ioreq_server.lock); -register_portio_handler (d,0xcf8 ,4 , hvm_access_cf8); +arch_ioreq_domain_init (d); }/*
xen/include/asm-x86/hvm/ioreq.h +19 -0
@@ -19,6 +19,25 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__ +#define HANDLE_BUFIOREQ (s) \ + ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) + +bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion); +int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_enable (struct hvm_ioreq_server *s); +void arch_ioreq_server_disable (struct hvm_ioreq_server *s); +void arch_ioreq_server_destroy (struct hvm_ioreq_server *s); +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags); +bool arch_ioreq_server_destroy_all (struct domain *d); +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr); +void arch_ioreq_domain_init (struct domain *d); +bool hvm_io_pending (struct vcpu *v);bool handle_hvm_io_completion (struct vcpu *v);bool is_ioreq_server_page (struct domain *d,const struct page_info *page);
Signature · 1 line
2.7.4
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
Quoted 5 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch makes some preparation to
x86/hvm/ioreq.cbefore moving to the common code. This way we will get a verbatim copy
<snip>
Quoted 6 lines
It worth mentioning that a code which checks the return value of
p2m_set_ioreq_server()inhvm_map_mem_type_to_ioreq_server()was folded intoarch_ioreq_server_map_mem_type()for the clear split. So thep2m_change_entry_type_global()is called withioreq_serverlock held.
<snip>
Quoted 30 lines
xen/arch/x86/hvm/ioreq.c +19
-1+/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1127
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +0
-8spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); -if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -
It should be noted that p2m holds it's own lock but I'm unfamiliar with Xen's locking architecture. Is there anything that prevents another vCPU accessing a page that is also being used my ioreq on the first vCPU?
Assuming that deadlock isn't a possibility to my relatively untrained eye this looks good to me:
Signature · 1 line
Alex Bennée
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
On 01.12.20 13:03, Alex Bennée wrote:
Hi Alex
Quoted 46 lines
Quoted 5 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch makes some preparation to
x86/hvm/ioreq.cbefore moving to the common code. This way we will get a verbatim copy<snip>
Quoted 5 lines
It worth mentioning that a code which checks the return value of
p2m_set_ioreq_server()inhvm_map_mem_type_to_ioreq_server()was folded intoarch_ioreq_server_map_mem_type()for the clear split. So thep2m_change_entry_type_global()is called withioreq_serverlock held.<snip>
Quoted 32 lines
xen/arch/x86/hvm/ioreq.c +19
-1+/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);
Quoted 11 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1127
Quoted 8 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -It should be noted that p2m holds it's own lock but I'm unfamiliar with Xen's locking architecture. Is there anything that prevents another vCPU accessing a page that is also being used my ioreq on the first vCPU?
I am not sure that I would be able to provide reasonable explanations here.
All what I understand is that p2m_change_entry_type_global() x86 specific (we don't have p2m_ioreq_server concept on Arm) and should remain as such (not exposed to the common code). IIRC, I raised a question during V2 review whether we could have ioreq server lock around the call to p2m_change_entry_type_global() and didn't get objections. I may mistake, but looks like the lock being used in p2m_change_entry_type_global() is yet another lock for protecting page table operations, so unlikely we could get into the trouble calling this function with the ioreq server lock held.
Quoted 5 lines
Assuming that deadlock isn't a possibility to my relatively untrained eye this looks good to me:
Reviewed-by Alex Bennée <[email protected]>
Thank you.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
Quoted 61 lines · Oleksandr
On 01.12.20 13:03, Alex Bennée wrote:
Hi Alex
Quoted 46 lines
Quoted 5 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch makes some preparation to
x86/hvm/ioreq.cbefore moving to the common code. This way we will get a verbatim copy<snip>
Quoted 5 lines
It worth mentioning that a code which checks the return value of
p2m_set_ioreq_server()inhvm_map_mem_type_to_ioreq_server()was folded intoarch_ioreq_server_map_mem_type()for the clear split. So thep2m_change_entry_type_global()is called withioreq_serverlock held.<snip>
Quoted 32 lines
xen/arch/x86/hvm/ioreq.c +19
-1+/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc = p2m_set_ioreq_server(d, flags, s;) + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);Quoted 11 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1127
Quoted 8 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -It should be noted that p2m holds it's own lock but I'm unfamiliar with Xen's locking architecture. Is there anything that prevents another vCPU accessing a page that is also being used my ioreq on the first vCPU?
Quoted 10 lines
I am not sure that I would be able to provide reasonable explanations here. All what I understand is that
p2m_change_entry_type_global()x86 specific (we don't havep2m_ioreq_serverconcept on Arm) and should remain as such (not exposed to the common code).
IIRC, I raised a question during V2 review whether we could have ioreq server lock around the call top2m_change_entry_type_global()and didn't get objections. I may mistake, but looks like the lock being used inp2m_change_entry_type_global()is yet another lock for protecting page table operations, so unlikely we could get into the trouble calling this function with the ioreq server lock held.
The p2m lock code looks designed to be recursive so I could only envision a problem where a page somehow racing to lock under the ioreq lock which I don't think is possible. However reasoning about locking is hard if your not familiar - it's one reason we added Promela/Spin [1] models to QEMU for our various locking regimes.
[1] http://spinroot.com/spin/whatispin.html
[2] https://git.qemu.org/?p=qemu.git;a=tree;f=docs/spin;h=cc168025131676429a560ca70d7234a56f958092;hb=HEAD
Quoted 9 lines
Quoted 7 lines
Assuming that deadlock isn't a possibility to my relatively untrained eye this looks good to me:
Reviewed-by Alex Bennée <[email protected]>Thank you.
Signature · 1 line
Alex Bennée
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
In reply to OleksandrQuoted 23 lines · Oleksandr
Quoted 11 lines · Alex Bennée
Quoted 10 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/ioreq.c +1
-1@@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);Quoted 11 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1127
Quoted 8 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -It should be noted that p2m holds it's own lock but I'm unfamiliar with Xen's locking architecture. Is there anything that prevents another vCPU accessing a page that is also being used my ioreq on the first vCPU?
Quoted 7 lines
I am not sure that I would be able to provide reasonable explanations here. All what I understand is that
p2m_change_entry_type_global()x86 specific (we don't havep2m_ioreq_serverconcept on Arm) and should remain as such (not exposed to the common code).
IIRC, I raised a question during V2 review whether we could have ioreq server lock around the call top2m_change_entry_type_global()and didn't get objections.
Not getting objections doesn't mean much. Personally I don't recall such a question, but this doesn't mean much. The important thing here is that you properly justify this change in the description (I didn't look at this version of the patch as a whole yet, so quite likely you actually do). This is because you need to guarantee that you don't introduce any lock order violations by this. There also should be an attempt to avoid future introduction of issues, by adding lock nesting related comments in suitable places. Again, quite likely you actually do so, and I will notice it once looking at the patch as a whole.
All of this said, I think it should be tried hard to avoid introducing this extra lock nesting, if there aren't other places already where the same nesting of locks is in effect.
Quoted 4 lines
I may mistake, but looks like the lock being used
inp2m_change_entry_type_global()is yet another lock for protecting page table operations, so unlikely we could get into the trouble calling this function with the ioreq server lock held.
I'm afraid I don't understand the "yet another" here: The ioreq server lock clearly serves an entirely different purpose.
Jan
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
On 02.12.20 10:00, Jan Beulich wrote:
Hi Jan
Quoted 31 lines · Oleksandr
Quoted 23 lines · Alex Bennée
Quoted 11 lines
Quoted 10 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/ioreq.c +1
-1@@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);Quoted 11 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1127
Quoted 8 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -It should be noted that p2m holds it's own lock but I'm unfamiliar with Xen's locking architecture. Is there anything that prevents another vCPU accessing a page that is also being used my ioreq on the first vCPU?
Quoted 7 lines
I am not sure that I would be able to provide reasonable explanations here. All what I understand is that
p2m_change_entry_type_global()x86 specific (we don't havep2m_ioreq_serverconcept on Arm) and should remain as such (not exposed to the common code).
IIRC, I raised a question during V2 review whether we could have ioreq server lock around the call top2m_change_entry_type_global()and didn't get objections.
Quoted 2 lines
Not getting objections doesn't mean much. Personally I don't recall such a question, but this doesn't mean much.
Sorry for not being clear here. Discussion happened at [1] when I was asked to move hvm_map_mem_type_to_ioreq_server() to the common code.
Quoted 5 lines
The important thing
here is that you properly justify this change in the description (I didn't look at this version of the patch as a whole yet, so quite likely you actually do). This is because you need to guarantee that you don't introduce any lock order violations by this.
Yes, almost all changes in this patch are mostly mechanical and leave things as they are.
The change with p2m_change_entry_type_global() requires an additional attention, so decided to put emphasis on touching that in the description and add a comment in the code that it is called with ioreq_server lock held.
[1] https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/#23734839
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
In reply to Oleksandr TyshchenkoQuoted 11 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/ioreq.c +3
-0@@ -17,15 +17,15 @@*/ #include <xen/ctype.h> +#include <xen/domain.h> +#include <xen/event.h> #include <xen/init.h> +#include <xen/irq.h> #include <xen/lib.h>
xen/arch/x86/hvm/ioreq.c line 29
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +2
-5-#include <xen/trace.h> +#include <xen/paging.h> #include <xen/sched.h> -#include <xen/irq.h> #include <xen/softirq.h> -#include <xen/domain.h> -#include <xen/event.h> -#include <xen/paging.h> +#include <xen/trace.h> #include <xen/vpci.h>
Seeing this consolidation (thanks!), have you been able to figure out what xen/ctype.h is needed for here? It looks to me as if it could be dropped at the same time.
xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s) · line 616
Quoted 9 lines
xen/arch/x86/hvm/ioreq.c +1
-1@@ -601,7 +610,7 @@ static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s)return rc; } -static void hvm_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) {hvm_unmap_ioreq_gfn (s, true);hvm_unmap_ioreq_gfn (s, false);
How is this now different from ...
xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, · line 690
Quoted 9 lines
xen/arch/x86/hvm/ioreq.c +5
-0@@ -674,6 +683,12 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,return rc; } +void arch_ioreq_server_enable (struct hvm_ioreq_server *s) +{ +hvm_remove_ioreq_gfn (s, false); +hvm_remove_ioreq_gfn (s, true); +}
... this? Imo if at all possible there should be no such duplication (i.e. at least have this one simply call the earlier one).
Quoted 35 lines
xen/arch/x86/hvm/ioreq.c +19
-1@@ -1080,6 +1105,24 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,return rc; } +/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1164
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -return rc; }
While you mention this change in the description, I'm still missing justification as to why the change is safe to make. I continue to think p2m_change_entry_type_global() would better not be called inside the locked region, if at all possible.
Quoted 13 lines
xen/arch/x86/hvm/ioreq.c +4
-4@@ -1239,33 +1279,28 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); } -struct hvm_ioreq_server *hvm_select_ioreq_server (struct domain *d, -ioreq_t *p) +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr) { -struct hvm_ioreq_server *s; -uint32_t cf8;
xen/arch/x86/hvm/ioreq.c void hvm_destroy_all_ioreq_servers(struct domain *d) · line 1290
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +2
-6-uint8_t type; -uint64_t addr; -unsigned int id; +unsigned int cf8 = d->arch.hvm.pci_cf8;if ( p->type !=IOREQ_TYPE_COPY && p->type !=IOREQ_TYPE_PIO ) -return NULL ; - - cf8 = d->arch.hvm.pci_cf8; +return -EINVAL ;
The caller cares about only a boolean. Either make the function return bool, or (imo better, but others may like this less) have it return "type" instead of using indirection, using e.g. negative values to identify errors (which then could still be errno ones if you wish).
Quoted 18 lines
xen/include/asm-x86/hvm/ioreq.h +12
-0@@ -19,6 +19,25 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__ +#define HANDLE_BUFIOREQ (s) \ + ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) + +bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion); +int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_enable (struct hvm_ioreq_server *s); +void arch_ioreq_server_disable (struct hvm_ioreq_server *s); +void arch_ioreq_server_destroy (struct hvm_ioreq_server *s); +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags);
xen/include/asm-x86/hvm/ioreq.h line 43
Quoted 10 lines
xen/include/asm-x86/hvm/ioreq.h +7
-0+bool arch_ioreq_server_destroy_all (struct domain *d); +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr); +void arch_ioreq_domain_init (struct domain *d); +bool hvm_io_pending (struct vcpu *v);bool handle_hvm_io_completion (struct vcpu *v);bool is_ioreq_server_page (struct domain *d,const struct page_info *page);
What's the plan here? Introduce them into the x86 header just to later move the entire block into the common one? Wouldn't it make sense to introduce the common header here right away? Or do you expect to convert some of the simpler ones to inline functions later on?
Jan
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
On 07.12.20 13:13, Jan Beulich wrote:
Hi Jan
Quoted 17 lines · Oleksandr Tyshchenko
Quoted 16 lines
xen/arch/x86/hvm/ioreq.c +4
-2@@ -17,15 +17,15 @@*/ #include <xen/ctype.h> +#include <xen/domain.h> +#include <xen/event.h> #include <xen/init.h> +#include <xen/irq.h> #include <xen/lib.h> -#include <xen/trace.h> +#include <xen/paging.h> #include <xen/sched.h> -#include <xen/irq.h> #include <xen/softirq.h>
Quoted 8 lines
xen/arch/x86/hvm/ioreq.c line 29
Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +1
-3-#include <xen/domain.h> -#include <xen/event.h> -#include <xen/paging.h> +#include <xen/trace.h> #include <xen/vpci.h> Seeing this consolidation (thanks!), have you been able to figure out what
xen/ctype.his needed for here? It looks to me as if it could be dropped at the same time.
Not yet, but will re-check.
Quoted 16 lines
xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s) · line 616
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1
-1@@ -601,7 +610,7 @@ static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s)return rc; } -static void hvm_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) {hvm_unmap_ioreq_gfn (s, true);hvm_unmap_ioreq_gfn (s, false);How is this now different from ...
Quoted 4 lines
xen/arch/x86/hvm/ioreq.c +0
-0@@ -674,6 +683,12 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,return rc; }
Quoted 7 lines
xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, · line 690
Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +5
-0+void arch_ioreq_server_enable (struct hvm_ioreq_server *s) +{ +hvm_remove_ioreq_gfn (s, false); +hvm_remove_ioreq_gfn (s, true); +}... this? Imo if at all possible there should be no such duplication (i.e. at least have this one simply call the earlier one).
I am afraid, I don't see any duplication between mentioned functions. Would you mind explaining?
Quoted 36 lines
Quoted 36 lines
xen/arch/x86/hvm/ioreq.c +19
-1@@ -1080,6 +1105,24 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,return rc; } +/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);
Quoted 14 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1164
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -return rc; }While you mention this change in the description, I'm still missing justification as to why the change is safe to make. I continue to think
p2m_change_entry_type_global()would better not be called inside the locked region, if at all possible.
Well. I am afraid, I don't have a 100% justification why the change is safe to make as well
as I don't see an obvious reason why it is not safe to make (at least I didn't find a possible deadlock scenario while investigating the code). I raised a question earlier whether I can fold this check in, which implied calling p2m_change_entry_type_global() with ioreq_server lock held.
If there is a concern with calling this inside the locked region (unfortunately still unclear for me at the moment), I will try to find another way how to split hvm_map_mem_type_to_ioreq_server() without potentially unsafe change here *and* exposing p2m_change_entry_type_global() to the common code. Right now, I don't have any ideas how this could be split other than introducing one more hook here to deal with p2m_change_entry_type_global (probably arch_ioreq_server_map_mem_type_complete?), but I don't expect it to be accepted.
I appreciate any ideas on that.
Quoted 19 lines
Quoted 19 lines
xen/arch/x86/hvm/ioreq.c +5
-7@@ -1239,33 +1279,28 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); } -struct hvm_ioreq_server *hvm_select_ioreq_server (struct domain *d, -ioreq_t *p) +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr) { -struct hvm_ioreq_server *s; -uint32_t cf8; -uint8_t type; -uint64_t addr; -unsigned int id; +unsigned int cf8 = d->arch.hvm.pci_cf8;
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c void hvm_destroy_all_ioreq_servers(struct domain *d) · line 1290
Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +1
-3if ( p->type !=IOREQ_TYPE_COPY && p->type !=IOREQ_TYPE_PIO ) -return NULL ; - - cf8 = d->arch.hvm.pci_cf8; +return -EINVAL ;The caller cares about only a boolean. Either make the function return bool, or (imo better, but others may like this less) have it return "type" instead of using indirection, using e.g. negative values to identify errors (which then could still be errno ones if you wish).
Makes sense. I will probably make the function return bool. Even if return "type" we will still have an indirection for "addr".
Quoted 26 lines
Quoted 26 lines
xen/include/asm-x86/hvm/ioreq.h +19
-0@@ -19,6 +19,25 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__ +#define HANDLE_BUFIOREQ (s) \ + ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) + +bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion); +int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_enable (struct hvm_ioreq_server *s); +void arch_ioreq_server_disable (struct hvm_ioreq_server *s); +void arch_ioreq_server_destroy (struct hvm_ioreq_server *s); +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags); +bool arch_ioreq_server_destroy_all (struct domain *d); +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr); +void arch_ioreq_domain_init (struct domain *d); +
Quoted 8 lines
xen/include/asm-x86/hvm/ioreq.h line 43
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0bool hvm_io_pending (struct vcpu *v);bool handle_hvm_io_completion (struct vcpu *v);bool is_ioreq_server_page (struct domain *d,const struct page_info *page);What's the plan here? Introduce them into the x86 header just to later move the entire block into the common one? Wouldn't it make sense to introduce the common header here right away? Or do you expect to convert some of the simpler ones to inline functions later on?
The former. The subsequent patch is moving the the entire block(s) from here and from x86/hvm/ioreq.c to the common code in one go.
I thought it was a little bit odd to expose a header before exposing an implementation to the common code. Another reason is to minimize places that need touching by current patch.
After all, this is done within single series and without breakage in between. But, if introducing the common header right away will make patch more cleaner and correct I am absolutely OK and happy to update a patch. Shall I?
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
Quoted 25 lines · Oleksandr
Quoted 16 lines · Jan Beulich
xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s) · line 616
Quoted 9 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/ioreq.c +1
-1@@ -601,7 +610,7 @@ static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s)return rc; } -static void hvm_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) {hvm_unmap_ioreq_gfn (s, true);hvm_unmap_ioreq_gfn (s, false);How is this now different from ...
Quoted 4 lines
xen/arch/x86/hvm/ioreq.c +0
-0@@ -674,6 +683,12 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,return rc; }Quoted 7 lines
xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, · line 690
Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +5
-0+void arch_ioreq_server_enable (struct hvm_ioreq_server *s) +{ +hvm_remove_ioreq_gfn (s, false); +hvm_remove_ioreq_gfn (s, true); +}... this? Imo if at all possible there should be no such duplication (i.e. at least have this one simply call the earlier one).
Quoted 2 lines
I am afraid, I don't see any duplication between mentioned functions. Would you mind explaining?
Ouch - somehow my eyes considered "unmap" == "remove". I'm sorry for the noise.
Quoted 49 lines
Quoted 35 lines
Quoted 35 lines
xen/arch/x86/hvm/ioreq.c +19
-1@@ -1080,6 +1105,24 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,return rc; } +/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);Quoted 14 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1164
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -return rc; }While you mention this change in the description, I'm still missing justification as to why the change is safe to make. I continue to think
p2m_change_entry_type_global()would better not be called inside the locked region, if at all possible.
Quoted 6 lines
Well. I am afraid, I don't have a 100% justification why the change is safe to make as well
as I don't see an obvious reason why it is not safe to make (at least I didn't find a possible deadlock scenario while investigating the code). I raised a question earlier whether I can fold this check in, which implied callingp2m_change_entry_type_global()withioreq_serverlock held.
I'm aware of the earlier discussion. But "didn't find" isn't good enough in a case like this, and since it's likely hard to indeed prove there's no deadlock possible, I think it's best to avoid having to provide such a proof by avoiding the nesting.
Quoted 10 lines
If there is a concern with calling this inside the locked region (unfortunately still unclear for me at the moment), I will try to find another way how to split
hvm_map_mem_type_to_ioreq_server()without potentially unsafe change here *and* exposingp2m_change_entry_type_global()to the common code. Right now, I don't have any ideas how this could be split other than introducing one more hook here to deal withp2m_change_entry_type_global(probablyarch_ioreq_server_map_mem_type_complete?), but I don't expect it to be accepted.
I appreciate any ideas on that.
Is there a reason why the simplest solution (two independent arch_*() calls) won't do? If so, what are the constraints? Can the first one e.g. somehow indicate what needs to happen after the lock was dropped? But the two calls look independent right now, so I don't see any complicating factors.
Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
xen/include/asm-x86/hvm/ioreq.h +19
-0@@ -19,6 +19,25 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__ +#define HANDLE_BUFIOREQ (s) \ + ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) + +bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion); +int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s); +void arch_ioreq_server_enable (struct hvm_ioreq_server *s); +void arch_ioreq_server_disable (struct hvm_ioreq_server *s); +void arch_ioreq_server_destroy (struct hvm_ioreq_server *s); +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags); +bool arch_ioreq_server_destroy_all (struct domain *d); +int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr); +void arch_ioreq_domain_init (struct domain *d); +Quoted 8 lines
xen/include/asm-x86/hvm/ioreq.h line 43
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0bool hvm_io_pending (struct vcpu *v);bool handle_hvm_io_completion (struct vcpu *v);bool is_ioreq_server_page (struct domain *d,const struct page_info *page);What's the plan here? Introduce them into the x86 header just to later move the entire block into the common one? Wouldn't it make sense to introduce the common header here right away? Or do you expect to convert some of the simpler ones to inline functions later on?
Quoted 2 lines
The former. The subsequent patch is moving the the entire block(s) from here and from
x86/hvm/ioreq.cto the common code in one go.
I think I saw it move the _other_ pieces there, and this block left here. (FAOD my comment is about the arch_*() declarations you add, not the patch context in view.)
Quoted 3 lines
I thought it was a little bit odd to expose a header before exposing an implementation to the common code. Another reason is to minimize places that need touching by current patch.
By exposing arch_*() declarations you don't give the impression of exposing any "implementation". These are helpers the implementation is to invoke; I'm fine with you moving the declarations of the functions actually constituting this component's external interface only once you also move the implementation to common code.
Jan
Re: [PATCH V3 01/23] x86/ioreq: Prepare IOREQ feature for making it common
Hi Jan
Quoted 55 lines
Quoted 49 lines
Quoted 35 lines
Quoted 35 lines
xen/arch/x86/hvm/ioreq.c +19
-1@@ -1080,6 +1105,24 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,return rc; } +/* Called with ioreq_server lock held */ +int arch_ioreq_server_map_mem_type (struct domain *d, +struct hvm_ioreq_server *s, +uint32_t flags) +{ +int rc =p2m_set_ioreq_server (d, flags, s); + +if ( rc ==0 && flags ==0 ) + { +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + +if (read_atomic (&p2m->ioreq.entry_count) ) +p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); + } + +return rc; +} +/* * Map or unmap an ioreq server to specific memory type. For now, only * HVMMEM_ioreq_server is supported, and in the future new types can be @@ -1112,19 +1155,11 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( s->emulator != current->domain ) goto out; - rc =p2m_set_ioreq_server (d, flags, s); + rc =arch_ioreq_server_map_mem_type (d, s, flags); out:spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock);Quoted 14 lines
xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1164
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +0
-8-if ( rc ==0 && flags ==0 ) - { -struct p2m_domain *p2m =p2m_get_hostp2m (d); - -if (read_atomic (&p2m->ioreq.entry_count) ) -p2m_change_entry_type_global (d, p2m_ioreq_server, p2m_ram_rw); - } -return rc; }While you mention this change in the description, I'm still missing justification as to why the change is safe to make. I continue to think
p2m_change_entry_type_global()would better not be called inside the locked region, if at all possible.Quoted 6 lines
Well. I am afraid, I don't have a 100% justification why the change is safe to make as well
as I don't see an obvious reason why it is not safe to make (at least I didn't find a possible deadlock scenario while investigating the code). I raised a question earlier whether I can fold this check in, which implied callingp2m_change_entry_type_global()withioreq_serverlock held.
Quoted 4 lines
I'm aware of the earlier discussion. But "didn't find" isn't good enough in a case like this, and since it's likely hard to indeed prove there's no deadlock possible, I think it's best to avoid having to provide such a proof by avoiding the nesting.
Agree here.
Quoted 11 lines
Quoted 11 lines
If there is a concern with calling this inside the locked region (unfortunately still unclear for me at the moment), I will try to find another way how to split
hvm_map_mem_type_to_ioreq_server()without potentially unsafe change here *and* exposingp2m_change_entry_type_global()to the common code. Right now, I don't have any ideas how this could be split other than introducing one more hook here to deal withp2m_change_entry_type_global(probablyarch_ioreq_server_map_mem_type_complete?), but I don't expect it to be accepted.
I appreciate any ideas on that.
Quoted 2 lines
Is there a reason why the simplest solution (two independent arch_*() calls) won't do? If so, what are the constraints?
There is no reason.
Quoted 2 lines
Can the first one e.g. somehow indicate what needs to happen after the lock was dropped?
I think, yes.
Quoted 2 lines
But the two calls look independent
right now, so I don't see any complicating factors.
ok, will go "two independent arch hooks" route then.
Thank you for the idea.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 2/23 x86/ioreq: Add IOREQ_STATUS_* #define-s and update code for moving
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
This patch continues to make some preparation to x86/hvm/ioreq.c before moving to the common code.
Add IOREQ_STATUS_* #define-s and update candidates for moving since X86EMUL_* shouldn't be exposed to the common code in that form.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V2 -> V3:
- new patch, was split from
[PATCH V2 01/23] x86/ioreq: Prepare IOREQ feature for making it common
---
--- xen/arch/x86/hvm/ioreq.c | 16 ++++++++-------- xen/include/asm-x86/hvm/ioreq.h | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-)
xen/arch/x86/hvm/ioreq.c +8 -8
@@ -1400,7 +1400,7 @@ static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p) pg = iorp->va;if ( !pg ) -return X86EMUL_UNHANDLEABLE ; +return IOREQ_STATUS_UNHANDLED ;/* * Return 0 for the cases we can't deal with:
@@ -1430,7 +1430,7 @@ static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p)break ;default :gdprintk (XENLOG_WARNING ,"unexpected ioreq size: %u\n" , p->size); -return X86EMUL_UNHANDLEABLE ; +return IOREQ_STATUS_UNHANDLED ; }spin_lock (&s->bufioreq_lock);
@@ -1440,7 +1440,7 @@ static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p) {/* The queue is full: send the iopacket through the normal path. */ spin_unlock (&s->bufioreq_lock); -return X86EMUL_UNHANDLEABLE ; +return IOREQ_STATUS_UNHANDLED ; } pg->buf_ioreq[pg->ptrs.write_pointer %IOREQ_BUFFER_SLOT_NUM ] = bp;
@@ -1471,7 +1471,7 @@ static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p)notify_via_xen_event_channel (d, s->bufioreq_evtchn);spin_unlock (&s->bufioreq_lock); -return X86EMUL_OKAY ; +return IOREQ_STATUS_HANDLED ; } int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p,
@@ -1487,7 +1487,7 @@ int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p,return hvm_send_buffered_ioreq (s, proto_p);if (unlikely (!vcpu_start_shutdown_deferral (curr)) ) -return X86EMUL_RETRY ; +return IOREQ_STATUS_RETRY ; list_for_each_entry ( sv, &s->ioreq_vcpu_list,
@@ -1527,11 +1527,11 @@ int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p,notify_via_xen_event_channel (d, port); sv->pending = true; -return X86EMUL_RETRY ; +return IOREQ_STATUS_RETRY ; } } -return X86EMUL_UNHANDLEABLE ; +return IOREQ_STATUS_UNHANDLED ; }unsigned int hvm_broadcast_ioreq (ioreq_t *p,bool buffered)
@@ -1545,7 +1545,7 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered)if ( !s->enabled )continue ; -if (hvm_send_ioreq (s, p, buffered) ==X86EMUL_UNHANDLEABLE ) +if (hvm_send_ioreq (s, p, buffered) ==IOREQ_STATUS_UNHANDLED ) failed++; }
xen/include/asm-x86/hvm/ioreq.h +4 -0
@@ -74,6 +74,10 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);void hvm_ioreq_init (struct domain *d); +#define IOREQ_STATUS_HANDLED X86EMUL_OKAY +#define IOREQ_STATUS_UNHANDLED X86EMUL_UNHANDLEABLE +#define IOREQ_STATUS_RETRY X86EMUL_RETRY +#endif /* __ASM_X86_HVM_IOREQ_H__ */ /*
Signature · 1 line
2.7.4
Re: [PATCH V3 02/23] x86/ioreq: Add IOREQ_STATUS_* #define-s and update code for moving
Quoted 9 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch continues to make some preparation to
x86/hvm/ioreq.cbefore moving to the common code.Add IOREQ_STATUS_* #define-s and update candidates for moving since X86EMUL_* shouldn't be exposed to the common code in that form.
Quoted 5 lines
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>
Signature · 1 line
Alex Bennée
Re: [PATCH V3 02/23] x86/ioreq: Add IOREQ_STATUS_* #define-s and update code for moving
In reply to Oleksandr Tyshchenkoxen/include/asm-x86/hvm/ioreq.h unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); · line 79
Quoted 9 lines · Oleksandr Tyshchenko
xen/include/asm-x86/hvm/ioreq.h +3
-0@@ -74,6 +74,10 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);void hvm_ioreq_init (struct domain *d); +#define IOREQ_STATUS_HANDLED X86EMUL_OKAY +#define IOREQ_STATUS_UNHANDLED X86EMUL_UNHANDLEABLE +#define IOREQ_STATUS_RETRY X86EMUL_RETRY
This correlation may not be altered. I think a comment is needed to this effect, to avoid someone trying to subsequently fold the x86 and (to be introduced) Arm ones. With that
Jan
Re: [PATCH V3 02/23] x86/ioreq: Add IOREQ_STATUS_* #define-s and update code for moving
On 07.12.20 13:19, Jan Beulich wrote:
Hi Jan
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -74,6 +74,10 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
Quoted 8 lines
xen/include/asm-x86/hvm/ioreq.h unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); · line 79
Quoted 5 lines
xen/include/asm-x86/hvm/ioreq.h +3
-0void hvm_ioreq_init (struct domain *d); +#define IOREQ_STATUS_HANDLED X86EMUL_OKAY +#define IOREQ_STATUS_UNHANDLED X86EMUL_UNHANDLEABLE +#define IOREQ_STATUS_RETRY X86EMUL_RETRYThis correlation may not be altered. I think a comment is needed to this effect, to avoid someone trying to subsequently fold the x86 and (to be introduced) Arm ones.
ok, will add.
Quoted 2 lines
With that
Acked-by Jan Beulich <[email protected]>
Thank you.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 3/23 x86/ioreq: Provide out-of-line wrapper for the handle_mmio()
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The IOREQ is about to be common feature and Arm will have its own implementation.
But the name of the function is pretty generic and can be confusing on Arm (we already have a try_handle_mmio()).
In order not to rename the function (which is used for a varying set of purposes on x86) globally and get non-confusing variant on Arm provide a wrapper ioreq_complete_mmio() to be used on common and Arm code.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch
Changes V1 -> V2:
- remove "handle"
- add Jan's A-b
Changes V2 -> V3:
- remove Jan's A-b
- update patch subject/description
- use out-of-line function instead of #define
- put earlier in the series to avoid breakage
---
--- xen/arch/x86/hvm/ioreq.c | 7 ++++++- xen/include/asm-x86/hvm/ioreq.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/ioreq.c +6 -1
@@ -36,6 +36,11 @@#include <public/hvm/ioreq.h> #include <public/hvm/params.h> +bool ioreq_complete_mmio (void ) +{ +return handle_mmio (); +} +static void set_ioreq_server (struct domain *d,unsigned int id,struct hvm_ioreq_server *s) {
@@ -226,7 +231,7 @@ bool handle_hvm_io_completion(struct vcpu *v)break ;case HVMIO_mmio_completion: -return handle_mmio (); +return ioreq_complete_mmio ();case HVMIO_pio_completion: return handle_pio(vio->io_req.addr, vio->io_req.size,
xen/include/asm-x86/hvm/ioreq.h +2 -0
@@ -74,6 +74,8 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);void hvm_ioreq_init (struct domain *d); +bool ioreq_complete_mmio (void ); +#define IOREQ_STATUS_HANDLED X86EMUL_OKAY#define IOREQ_STATUS_UNHANDLED X86EMUL_UNHANDLEABLE#define IOREQ_STATUS_RETRY X86EMUL_RETRY
Signature · 1 line
2.7.4
Re: [PATCH V3 03/23] x86/ioreq: Provide out-of-line wrapper for the handle_mmio()
xen/arch/x86/hvm/ioreq.c line 42
Quoted 10 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/ioreq.c +4
-0@@ -36,6 +36,11 @@#include <public/hvm/ioreq.h> #include <public/hvm/params.h> +bool ioreq_complete_mmio (void ) +{ +return handle_mmio (); +}
As indicated before I don't like out-of-line functions like this one; I think a #define would be quite fine here, but Paul as the maintainer thinks differently. So be it. However, shouldn't this function be named arch_ioreq_complete_mmio() according to the new naming model, and then ...
xen/include/asm-x86/hvm/ioreq.h unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); · line 77
Quoted 7 lines
xen/include/asm-x86/hvm/ioreq.h +1
-0@@ -74,6 +74,8 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);void hvm_ioreq_init (struct domain *d); +bool ioreq_complete_mmio (void );
... get declared next to the other arch_*() hooks? With this
Jan
Re: [PATCH V3 03/23] x86/ioreq: Provide out-of-line wrapper for the handle_mmio()
On 07.12.20 13:27, Jan Beulich wrote:
Hi Jan
Quoted 17 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/ioreq.c line 42
Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +4
-0@@ -36,6 +36,11 @@#include <public/hvm/ioreq.h> #include <public/hvm/params.h> +bool ioreq_complete_mmio (void ) +{ +return handle_mmio (); +}As indicated before I don't like out-of-line functions like this one; I think a #define would be quite fine here, but Paul as the maintainer thinks differently. So be it. However, shouldn't this function be named
arch_ioreq_complete_mmio()according to the new naming model, and then ...
Quoted 8 lines
xen/include/asm-x86/hvm/ioreq.h unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); · line 77
Quoted 7 lines
xen/include/asm-x86/hvm/ioreq.h +1
-0@@ -74,6 +74,8 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);void hvm_ioreq_init (struct domain *d); +bool ioreq_complete_mmio (void );... get declared next to the other arch_*() hooks? With this
sounds reasonable, will update
Quoted 1 line
Reviewed-by Jan Beulich <[email protected]>
Thank you
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 4/23 xen/ioreq: Make x86's IOREQ feature common
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch moves previously prepared IOREQ support to the common code (the code movement is verbatim copy).
The "legacy" mechanism of mapping magic pages for the IOREQ servers remains x86 specific and not exposed to the common code.
The common IOREQ feature is supposed to be built with IOREQ_SERVER option enabled, which is selected for x86's config HVM for now.
In order to avoid having a gigantic patch here, the subsequent patches will update remaining bits in the common code step by step:
- Make IOREQ related structs/materials common
- Drop the "hvm" prefixes and infixes
- Remove layering violation by moving corresponding fields out of *arch.hvm* or abstracting away accesses to them
Also include <xen/domain_page.h> which will be needed on Arm to avoid touch the common code again when introducing Arm specific bits.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
***
Please note, this patch depends on the following which is on review:
https://patchwork.kernel.org/patch/11816689/
***
Changes RFC -> V1:
- was split into three patches:
- x86/ioreq: Prepare IOREQ feature for making it common
- xen/ioreq: Make x86's IOREQ feature common
- xen/ioreq: Make x86's hvm_ioreq_needs_completion() common
- update MAINTAINERS file
- do not use a separate subdir for the IOREQ stuff, move it to:
- xen/common/ioreq.c
- xen/include/xen/ioreq.h
- update x86's files to include xen/ioreq.h
- remove unneeded headers in arch/x86/hvm/ioreq.c
- re-order the headers alphabetically in common/ioreq.c
- update common/ioreq.c according to the newly introduced arch functions:
arch_hvm_destroy_ioreq_server()/arch_handle_hvm_io_completion()
Changes V1 -> V2:
- update patch description
- make everything needed in the previous patch to achieve a truly rename here
- don't include unnecessary headers from asm-x86/hvm/ioreq.h and xen/ioreq.h
- use __XEN_IOREQ_H__ instead of __IOREQ_H__
- move get_ioreq_server() to common/ioreq.c
Changes V2 -> V3:
- update patch description
- make everything needed in the previous patch to not expose "legacy" interface to the common code here
- update patch according the "legacy interface" is x86 specific
- include <xen/domain_page.h> in common ioreq.c
---
--- MAINTAINERS | 8 +- xen/arch/x86/Kconfig | 1 + xen/arch/x86/hvm/ioreq.c | 1356 ++------------------------------------- xen/arch/x86/mm.c | 2 +- xen/arch/x86/mm/shadow/common.c | 2 +- xen/common/Kconfig | 3 + xen/common/Makefile | 1 + xen/common/ioreq.c | 1287 +++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/ioreq.h | 39 +- xen/include/xen/ioreq.h | 73 +++ 10 files changed, 1432 insertions(+), 1340 deletions(-) create mode 100644 xen/common/ioreq.c create mode 100644 xen/include/xen/ioreq.h
MAINTAINERS +7 -1
@@ -333,6 +333,13 @@ X: xen/drivers/passthrough/vtd/ X: xen/drivers/passthrough/device_tree.c F: xen/include/xen/iommu.h +I/O EMULATION (IOREQ) +M: Paul Durrant <[email protected]> +S: Supported +F: xen/common/ioreq.c +F: xen/include/xen/ioreq.h +F: xen/include/public/hvm/ioreq.h + KCONFIG M: Doug Goldstein <[email protected]> S: Supported
@@ -549,7 +556,6 @@ F: xen/arch/x86/hvm/ioreq.c F: xen/include/asm-x86/hvm/emulate.h F: xen/include/asm-x86/hvm/io.h F: xen/include/asm-x86/hvm/ioreq.h -F: xen/include/public/hvm/ioreq.h X86 MEMORY MANAGEMENT M: Jan Beulich <[email protected]>
xen/arch/x86/Kconfig +1 -0
@@ -91,6 +91,7 @@ config PV_LINEAR_PT config HVM def_bool !PV_SHIM_EXCLUSIVE + select IOREQ_SERVER prompt "HVM support" ---help--- Interfaces to support HVM domains. HVM domains require hardware
xen/arch/x86/hvm/ioreq.c +57 -1299
1,420 lines not on this page · raw message
xen/arch/x86/mm.c +1 -1
@@ -100,6 +100,7 @@*/ #include <xen/init.h> +#include <xen/ioreq.h> #include <xen/kernel.h> #include <xen/lib.h> #include <xen/mm.h>
@@ -141,7 +142,6 @@#include <asm/io_apic.h> #include <asm/pci.h> #include <asm/guest.h> -#include <asm/hvm/ioreq.h> #include <asm/hvm/grant_table.h> #include <asm/pv/domain.h>
xen/arch/x86/mm/shadow/common.c +1 -1
@@ -20,6 +20,7 @@* along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include <xen/ioreq.h> #include <xen/types.h> #include <xen/mm.h> #include <xen/trace.h>
@@ -34,7 +35,6 @@#include <asm/current.h> #include <asm/flushtlb.h> #include <asm/shadow.h> -#include <asm/hvm/ioreq.h> #include <xen/numa.h> #include "private.h"
xen/common/Kconfig +3 -0
@@ -139,6 +139,9 @@ config HYPFS_CONFIG Disable this option in case you want to spare some memory or you want to hide the .config contents from dom0. +config IOREQ_SERVER + bool + config KEXEC bool "kexec support" default y
xen/common/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_GRANT_TABLE) += grant_table.o obj-y += guestcopy.o obj-bin-y += gunzip.init.o obj-$(CONFIG_HYPFS) += hypfs.o +obj-$(CONFIG_IOREQ_SERVER) += ioreq.o obj-y += irq.o obj-y += kernel.o obj-y += keyhandler.o
xen/common/ioreq.c new file +1287 -0
1,287 lines not on this page · raw message
xen/include/asm-x86/hvm/ioreq.h +1 -38
@@ -19,8 +19,7 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__ -#define HANDLE_BUFIOREQ (s) \ - ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) +#include <xen/ioreq.h> bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion);int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s);
@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d); -bool hvm_io_pending (struct vcpu *v); -bool handle_hvm_io_completion (struct vcpu *v); -bool is_ioreq_server_page (struct domain *d,const struct page_info *page); - -int hvm_create_ioreq_server (struct domain *d,int bufioreq_handling, -ioservid_t *id); -int hvm_destroy_ioreq_server (struct domain *d,ioservid_t id); -int hvm_get_ioreq_server_info (struct domain *d,ioservid_t id, -unsigned long *ioreq_gfn, -unsigned long *bufioreq_gfn, -evtchn_port_t *bufioreq_port); -int hvm_get_ioreq_server_frame (struct domain *d,ioservid_t id, -unsigned long idx,mfn_t *mfn); -int hvm_map_io_range_to_ioreq_server (struct domain *d,ioservid_t id, -uint32_t type,uint64_t start, -uint64_t end); -int hvm_unmap_io_range_from_ioreq_server (struct domain *d,ioservid_t id, -uint32_t type,uint64_t start, -uint64_t end); -int hvm_map_mem_type_to_ioreq_server (struct domain *d,ioservid_t id, -uint32_t type,uint32_t flags); -int hvm_set_ioreq_server_state (struct domain *d,ioservid_t id, -bool enabled); - -int hvm_all_ioreq_servers_add_vcpu (struct domain *d,struct vcpu *v); -void hvm_all_ioreq_servers_remove_vcpu (struct domain *d,struct vcpu *v); -void hvm_destroy_all_ioreq_servers (struct domain *d); - -struct hvm_ioreq_server *hvm_select_ioreq_server (struct domain *d, -ioreq_t *p); -int hvm_send_ioreq (struct hvm_ioreq_server *s,ioreq_t *proto_p, -bool buffered); -unsigned int hvm_broadcast_ioreq (ioreq_t *p,bool buffered); - -void hvm_ioreq_init (struct domain *d); -bool ioreq_complete_mmio (void );#define IOREQ_STATUS_HANDLED X86EMUL_OKAY
xen/include/xen/ioreq.h new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH V3 04/23] xen/ioreq: Make x86's IOREQ feature common
xen/include/asm-x86/hvm/ioreq.h line 22
Quoted 9 lines · Oleksandr Tyshchenko
xen/include/asm-x86/hvm/ioreq.h +1
-2@@ -19,8 +19,7 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__ -#define HANDLE_BUFIOREQ (s) \ - ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) +#include <xen/ioreq.h>
Is there a strict need to do it this way round? Usually the common header would include the arch one ...
xen/include/asm-x86/hvm/ioreq.h int arch_ioreq_server_get_type_addr(const struct domain *d, · line 38
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d);
As already mentioned in an earlier reply: What about these? They shouldn't get declared once per arch. If anything, ones that want to be inline functions can / should remain in the per-arch header.
Jan
Re: [PATCH V3 04/23] xen/ioreq: Make x86's IOREQ feature common
On 07.12.20 13:41, Jan Beulich wrote:
Hi Jan
Quoted 7 lines · Oleksandr Tyshchenko
Quoted 6 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -19,8 +19,7 @@#ifndef __ASM_X86_HVM_IOREQ_H__#define __ASM_X86_HVM_IOREQ_H__
Quoted 5 lines
xen/include/asm-x86/hvm/ioreq.h line 22
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +1
-2-#define HANDLE_BUFIOREQ (s) \ - ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) +#include <xen/ioreq.h> Is there a strict need to do it this way round? Usually the common header would include the arch one ...
The reason was to make a bunch of x86 files (which included asm/hvm/ioreq.h so far) to not suffer from moving IOREQ interface location
and as the result to limit the number of files which needed touching. If a common rule is to another way around, I will follow it.
So will change to include arch header from the common one. Or even will include arch header only where it is required (common ioreq.c right now and Arm io.c in future).
Quoted 7 lines
xen/include/asm-x86/hvm/ioreq.h int arch_ioreq_server_get_type_addr(const struct domain *d, · line 38
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d);As already mentioned in an earlier reply: What about these? They shouldn't get declared once per arch. If anything, ones that want to be inline functions can / should remain in the per-arch header.
Don't entirely get a suggestion. Is the suggestion to make "simple" ones inline? Why not, there are a few ones which probably want to be inline, such as the following for example:
- arch_ioreq_domain_init
- arch_ioreq_server_destroy
- arch_ioreq_server_destroy_all
- arch_ioreq_server_map_mem_type (probably)
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 04/23] xen/ioreq: Make x86's IOREQ feature common
Quoted 9 lines · Oleksandr
Quoted 8 lines · Jan Beulich
xen/include/asm-x86/hvm/ioreq.h int arch_ioreq_server_get_type_addr(const struct domain *d, · line 38
Quoted 3 lines · Oleksandr Tyshchenko
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d);As already mentioned in an earlier reply: What about these? They shouldn't get declared once per arch. If anything, ones that want to be inline functions can / should remain in the per-arch header.
Quoted 7 lines
Don't entirely get a suggestion. Is the suggestion to make "simple" ones inline? Why not, there are a few ones which probably want to be inline, such as the following for example:
-arch_ioreq_domain_init
-arch_ioreq_server_destroy
-arch_ioreq_server_destroy_all
-arch_ioreq_server_map_mem_type(probably)
Before being able to make a suggestion, I need to have my question
answered: Why do the arch_*() declarations live in the arch header? They represent a common interface (between common and arch code) and hence should be declared in exactly one place. It is only at the point where you/we _consider_ making some of them inline that moving those (back) to the arch header may make sense. Albeit even then I'd prefer if only the ones get moved which are expected to be inline for all arch-es. Others would better have the arch header indicate to the common one that no declaration is needed (such that the declaration still remains common for all arch-es using out-of- line functions).
Jan
Re: [PATCH V3 04/23] xen/ioreq: Make x86's IOREQ feature common
On 08.12.20 11:21, Jan Beulich wrote:
Hi Jan
Quoted 10 lines · Oleksandr
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Oleksandr Tyshchenko
xen/include/asm-x86/hvm/ioreq.h int arch_ioreq_server_get_type_addr(const struct domain *d, · line 38
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d);As already mentioned in an earlier reply: What about these? They shouldn't get declared once per arch. If anything, ones that want to be inline functions can / should remain in the per-arch header.
Quoted 7 lines
Quoted 7 lines
Don't entirely get a suggestion. Is the suggestion to make "simple" ones inline? Why not, there are a few ones which probably want to be inline, such as the following for example:
-arch_ioreq_domain_init
-arch_ioreq_server_destroy
-arch_ioreq_server_destroy_all
-arch_ioreq_server_map_mem_type(probably)
First of all, thank you for the clarification, now your point is clear to me.
Quoted 4 lines
Before being able to make a suggestion, I need to have my question
answered: Why do the arch_*() declarations live in the arch header? They represent a common interface (between common and arch code) and hence should be declared in exactly one place.
I got it, I had a wrong assumption that arch hooks declarations should live in arch code.
Quoted 8 lines
It is only at
the point where you/we _consider_ making some of them inline that moving those (back) to the arch header may make sense. Albeit even then I'd prefer if only the ones get moved which are expected to be inline for all arch-es. Others would better have the arch header indicate to the common one that no declaration is needed (such that the declaration still remains common for all arch-es using out-of- line functions).
I got it as well.
Well, I think, in order to address your comments two options are available:
1. All arch hooks are out-of-line: мove all arch hook declarations to the common header here and modify "[PATCH V3 14/23] arm/ioreq: Introduce arch specific bits for IOREQ/DM features" to make all Arm variants out-of-line (I made them inline since all of them are just stubs).
2. Some of arch hooks are inline: consider which want to be inline (for both arch-es) and place them into arch headers, other ones should remain in the common header.
My question is which option is more suitable?
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 04/23] xen/ioreq: Make x86's IOREQ feature common
Quoted 57 lines · Oleksandr
On 08.12.20 11:21, Jan Beulich wrote:
Hi Jan
Quoted 10 lines · Oleksandr
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Oleksandr Tyshchenko
xen/include/asm-x86/hvm/ioreq.h int arch_ioreq_server_get_type_addr(const struct domain *d, · line 38
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d);As already mentioned in an earlier reply: What about these? They shouldn't get declared once per arch. If anything, ones that want to be inline functions can / should remain in the per-arch header.
Quoted 7 lines
Quoted 7 lines
Don't entirely get a suggestion. Is the suggestion to make "simple" ones inline? Why not, there are a few ones which probably want to be inline, such as the following for example:
-arch_ioreq_domain_init
-arch_ioreq_server_destroy
-arch_ioreq_server_destroy_all
-arch_ioreq_server_map_mem_type(probably)First of all, thank you for the clarification, now your point is clear to me.
Quoted 4 lines
Before being able to make a suggestion, I need to have my question
answered: Why do the arch_*() declarations live in the arch header? They represent a common interface (between common and arch code) and hence should be declared in exactly one place.I got it, I had a wrong assumption that arch hooks declarations should live in arch code.
Quoted 8 lines
It is only at
the point where you/we _consider_ making some of them inline that moving those (back) to the arch header may make sense. Albeit even then I'd prefer if only the ones get moved which are expected to be inline for all arch-es. Others would better have the arch header indicate to the common one that no declaration is needed (such that the declaration still remains common for all arch-es using out-of- line functions).I got it as well.
Well, I think, in order to address your comments two options are available:
1. All arch hooks are out-of-line: мove all arch hook declarations to the common header here and modify
"[PATCH V3 14/23] arm/ioreq: Introduce arch specific bits for IOREQ/DM features" to make all Arm variants
out-of-line (I made them inline since all of them are just stubs).
2. Some of arch hooks are inline: consider which want to be inline (for both arch-es) and place them into arch headers, other ones should remain in the common header.
Quoted 1 line
My question is which option is more suitable?
And the presumably very helpful to you answer is "Depends." It's a case by case judgement call in the end.
Sorry, Jan
Re: [PATCH V3 04/23] xen/ioreq: Make x86's IOREQ feature common
On 08.12.20 17:02, Jan Beulich wrote:
Hi Jan
Quoted 55 lines · Oleksandr
Quoted 54 lines · Jan Beulich
Hi Jan
Quoted 10 lines · Oleksandr
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Oleksandr Tyshchenko
xen/include/asm-x86/hvm/ioreq.h int arch_ioreq_server_get_type_addr(const struct domain *d, · line 38
Quoted 3 lines
xen/include/asm-x86/hvm/ioreq.h +0
-0@@ -38,42 +37,6 @@ int arch_ioreq_server_get_type_addr(const struct domain *d, uint64_t *addr);void arch_ioreq_domain_init (struct domain *d);As already mentioned in an earlier reply: What about these? They shouldn't get declared once per arch. If anything, ones that want to be inline functions can / should remain in the per-arch header.
Quoted 7 lines
Quoted 7 lines
Don't entirely get a suggestion. Is the suggestion to make "simple" ones inline? Why not, there are a few ones which probably want to be inline, such as the following for example:
-arch_ioreq_domain_init
-arch_ioreq_server_destroy
-arch_ioreq_server_destroy_all
-arch_ioreq_server_map_mem_type(probably)First of all, thank you for the clarification, now your point is clear to me.
Quoted 4 lines
Before being able to make a suggestion, I need to have my question
answered: Why do the arch_*() declarations live in the arch header? They represent a common interface (between common and arch code) and hence should be declared in exactly one place.I got it, I had a wrong assumption that arch hooks declarations should live in arch code.
Quoted 8 lines
It is only at
the point where you/we _consider_ making some of them inline that moving those (back) to the arch header may make sense. Albeit even then I'd prefer if only the ones get moved which are expected to be inline for all arch-es. Others would better have the arch header indicate to the common one that no declaration is needed (such that the declaration still remains common for all arch-es using out-of- line functions).I got it as well.
Well, I think, in order to address your comments two options are available:
1. All arch hooks are out-of-line: мove all arch hook declarations to the common header here and modify
"[PATCH V3 14/23] arm/ioreq: Introduce arch specific bits for IOREQ/DM features" to make all Arm variants
out-of-line (I made them inline since all of them are just stubs).
2. Some of arch hooks are inline: consider which want to be inline (for both arch-es) and place them into arch headers, other ones should remain in the common header.
Quoted 5 lines
Quoted 1 line
My question is which option is more suitable?
And the presumably very helpful to you answer is "Depends." It's a case by case judgement call in the end.
Sorry, Jan
Thank you for the honest answer. I will use option #1 since all these arch hooks are for single-use purpose only. If indeed there is a need to make some of them inline, I think it could be done later on.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 5/23 xen/ioreq: Make x86's hvm_ioreq_needs_completion() common
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and this helper will be used on Arm as is. Move it to xen/ioreq.h and remove "hvm" prefix.
Although PIO handling on Arm is not introduced with the current series (it will be implemented when we add support for vPCI), technically the PIOs exist on Arm (however they are accessed the same way as MMIO) and it would be better not to diverge now.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch, was split from:
"[RFC PATCH V1 01/12] hvm/ioreq: Make x86's IOREQ feature common"
Changes V1 -> V2:
- remove "hvm" prefix
Changes V2 -> V3:
- add Paul's R-b
---
--- xen/arch/x86/hvm/emulate.c | 4 ++-- xen/arch/x86/hvm/io.c | 2 +- xen/common/ioreq.c | 4 ++-- xen/include/asm-x86/hvm/vcpu.h | 7 ------- xen/include/xen/ioreq.h | 7 +++++++ 5 files changed, 12 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -2
@@ -336,7 +336,7 @@ static int hvmemul_do_io( rc =hvm_send_ioreq (s, &p,0 );if ( rc !=X86EMUL_RETRY || currd->is_shutting_down ) vio->io_req.state =STATE_IOREQ_NONE ; -else if ( !hvm_ioreq_needs_completion (&vio->io_req) ) +else if ( !ioreq_needs_completion (&vio->io_req) ) rc =X86EMUL_OKAY ; }break ;
@@ -2649,7 +2649,7 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,if ( rc ==X86EMUL_OKAY && vio->mmio_retry ) rc =X86EMUL_RETRY ; -if ( !hvm_ioreq_needs_completion (&vio->io_req) ) +if ( !ioreq_needs_completion (&vio->io_req) ) completion = HVMIO_no_completion;else if ( completion == HVMIO_no_completion ) completion = (vio->io_req.type != IOREQ_TYPE_PIO ||
xen/arch/x86/hvm/io.c +1 -1
@@ -135,7 +135,7 @@ bool handle_pio(uint16_t port, unsigned int size, int dir) rc =hvmemul_do_pio_buffer (port, size, dir, &data); -if (hvm_ioreq_needs_completion (&vio->io_req) ) +if (ioreq_needs_completion (&vio->io_req) ) vio->io_completion = HVMIO_pio_completion; switch ( rc )
xen/common/ioreq.c +2 -2
@@ -160,7 +160,7 @@ static bool hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p) } p = &sv->vcpu->arch.hvm.hvm_io.io_req; -if (hvm_ioreq_needs_completion (p) ) +if (ioreq_needs_completion (p) ) p->data = data; sv->pending = false;
@@ -186,7 +186,7 @@ bool handle_hvm_io_completion(struct vcpu *v)if ( sv && !hvm_wait_for_io (sv,get_ioreq (s, v)) )return false; - vio->io_req.state =hvm_ioreq_needs_completion (&vio->io_req) ? + vio->io_req.state =ioreq_needs_completion (&vio->io_req) ?STATE_IORESP_READY :STATE_IOREQ_NONE ;msix_write_completion (v);
xen/include/asm-x86/hvm/vcpu.h +0 -7
@@ -91,13 +91,6 @@ struct hvm_vcpu_io {const struct g2m_ioport *g2m_ioport; }; -static inline bool hvm_ioreq_needs_completion (const ioreq_t *ioreq) -{ -return ioreq->state ==STATE_IOREQ_READY && - !ioreq->data_is_ptr && - (ioreq->type !=IOREQ_TYPE_PIO || ioreq->dir !=IOREQ_WRITE ); -} -struct nestedvcpu {bool_t nv_guestmode;/* vcpu in guestmode? */ void *nv_vvmcx;/* l1 guest virtual VMCB/VMCS */
xen/include/xen/ioreq.h +7 -0
@@ -21,6 +21,13 @@#include <xen/sched.h> +static inline bool ioreq_needs_completion (const ioreq_t *ioreq) +{ +return ioreq->state ==STATE_IOREQ_READY && + !ioreq->data_is_ptr && + (ioreq->type !=IOREQ_TYPE_PIO || ioreq->dir !=IOREQ_WRITE ); +} +#define HANDLE_BUFIOREQ (s) \ ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF)
Signature · 1 line
2.7.4
Re: [PATCH V3 05/23] xen/ioreq: Make x86's hvm_ioreq_needs_completion() common
Quoted 5 lines · Oleksandr Tyshchenko
xen/include/xen/ioreq.h +0
-0@@ -21,6 +21,13 @@#include <xen/sched.h>
xen/include/xen/ioreq.h line 32
Quoted 10 lines
xen/include/xen/ioreq.h +7
-0+static inline bool ioreq_needs_completion (const ioreq_t *ioreq) +{ +return ioreq->state ==STATE_IOREQ_READY && + !ioreq->data_is_ptr && + (ioreq->type !=IOREQ_TYPE_PIO || ioreq->dir !=IOREQ_WRITE ); +} +#define HANDLE_BUFIOREQ (s) \ ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF)
Personally I would have suggested to keep the #define first, but I see you've already got Paul's R-b. Applicable parts
Jan
PATCH v3 6/23 xen/ioreq: Make x86's hvm_mmio_first(last)_byte() common
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and these helpers will be used on Arm as is. Move them to xen/ioreq.h and replace "hvm" prefixes with "ioreq".
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch
Changes V1 -> V2:
- replace "hvm" prefix by "ioreq"
Changes V2 -> V3:
- add Paul's R-b
---
--- xen/arch/x86/hvm/intercept.c | 5 +++-- xen/arch/x86/hvm/stdvga.c | 4 ++-- xen/common/ioreq.c | 4 ++-- xen/include/asm-x86/hvm/io.h | 16 ---------------- xen/include/xen/ioreq.h | 16 ++++++++++++++++ 5 files changed, 23 insertions(+), 22 deletions(-)
xen/arch/x86/hvm/intercept.c +3 -2
@@ -17,6 +17,7 @@* this program; If not, see <http://www.gnu.org/licenses/>. */ +#include <xen/ioreq.h> #include <xen/types.h> #include <xen/sched.h> #include <asm/regs.h>
@@ -34,7 +35,7 @@static bool_t hvm_mmio_accept (const struct hvm_io_handler *handler,const ioreq_t *p) { -paddr_t first =hvm_mmio_first_byte (p), last; +paddr_t first =ioreq_mmio_first_byte (p), last;BUG_ON (handler->type !=IOREQ_TYPE_COPY );
@@ -42,7 +43,7 @@ static bool_t hvm_mmio_accept(const struct hvm_io_handler *handler,return 0 ;/* Make sure the handler will accept the whole access. */ - last =hvm_mmio_last_byte (p); + last =ioreq_mmio_last_byte (p);if ( last != first && !handler->mmio.ops->check (current, last) )domain_crash (current->domain);
xen/arch/x86/hvm/stdvga.c +2 -2
@@ -524,8 +524,8 @@ static bool_t stdvga_mem_accept(const struct hvm_io_handler *handler,* deadlock when hvm_mmio_internal() is called from * hvm_copy_to/from_guest_phys() in hvm_process_io_intercept(). */ -if ( (hvm_mmio_first_byte (p) <VGA_MEM_BASE ) || - (hvm_mmio_last_byte (p) >= (VGA_MEM_BASE +VGA_MEM_SIZE )) ) +if ( (ioreq_mmio_first_byte (p) <VGA_MEM_BASE ) || + (ioreq_mmio_last_byte (p) >= (VGA_MEM_BASE +VGA_MEM_SIZE )) )return 0 ;spin_lock (&s->lock);
xen/common/ioreq.c +2 -2
@@ -1075,8 +1075,8 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,break ;case XEN_DMOP_IO_RANGE_MEMORY : - start =hvm_mmio_first_byte (p); - end =hvm_mmio_last_byte (p); + start =ioreq_mmio_first_byte (p); + end =ioreq_mmio_last_byte (p);if (rangeset_contains_range (r, start, end) )return s;
xen/include/asm-x86/hvm/io.h +0 -16
@@ -40,22 +40,6 @@ struct hvm_mmio_ops {hvm_mmio_write_t write; }; -static inline paddr_t hvm_mmio_first_byte (const ioreq_t *p) -{ -return unlikely (p->df) ? - p->addr - (p->count -1ul ) * p->size : - p->addr; -} - -static inline paddr_t hvm_mmio_last_byte (const ioreq_t *p) -{ -unsigned long size = p->size; - -return unlikely (p->df) ? - p->addr + size -1 : - p->addr + (p->count * size) -1 ; -} -typedef int (*portio_action_t )(int dir,unsigned int port,unsigned int bytes,uint32_t *val);
xen/include/xen/ioreq.h +16 -0
@@ -21,6 +21,22 @@#include <xen/sched.h> +static inline paddr_t ioreq_mmio_first_byte (const ioreq_t *p) +{ +return unlikely (p->df) ? + p->addr - (p->count -1ul ) * p->size : + p->addr; +} + +static inline paddr_t ioreq_mmio_last_byte (const ioreq_t *p) +{ +unsigned long size = p->size; + +return unlikely (p->df) ? + p->addr + size -1 : + p->addr + (p->count * size) -1 ; +} +static inline bool ioreq_needs_completion (const ioreq_t *ioreq) { return ioreq->state == STATE_IOREQ_READY &&
Signature · 1 line
2.7.4
Re: [PATCH V3 06/23] xen/ioreq: Make x86's hvm_mmio_first(last)_byte() common
Quoted 8 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and these helpers will be used on Arm as is. Move them to
xen/ioreq.hand replace "hvm" prefixes with "ioreq".Signed-off-by Oleksandr Tyshchenko <[email protected]>Reviewed-by Paul Durrant <[email protected]>
Applicable parts
Jan
PATCH v3 7/23 xen/ioreq: Make x86's hvm_ioreq_(page/vcpu/server) structs common
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and these structs will be used on Arm as is. Move them to xen/ioreq.h and remove "hvm" prefixes.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch
Changes V1 -> V2:
- remove "hvm" prefix
Changes V2 -> V3:
- update patch according the "legacy interface" is x86 specific
---
--- xen/arch/x86/hvm/emulate.c | 2 +- xen/arch/x86/hvm/ioreq.c | 36 ++++++------- xen/arch/x86/hvm/stdvga.c | 2 +- xen/arch/x86/mm/p2m.c | 8 +-- xen/common/ioreq.c | 108 +++++++++++++++++++-------------------- xen/include/asm-x86/hvm/domain.h | 36 +------------ xen/include/asm-x86/hvm/ioreq.h | 12 ++--- xen/include/asm-x86/p2m.h | 8 +-- xen/include/xen/ioreq.h | 40 +++++++++++++-- 9 files changed, 126 insertions(+), 126 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -1
@@ -287,7 +287,7 @@ static int hvmemul_do_io(* However, there's no cheap approach to avoid above situations in xen, * so the device model side needs to check the incoming ioreq event. */ -struct hvm_ioreq_server *s =NULL ; +struct ioreq_server *s =NULL ;p2m_type_t p2mt = p2m_invalid;if ( is_mmio )
xen/arch/x86/hvm/ioreq.c +18 -18
@@ -64,7 +64,7 @@ bool arch_vcpu_ioreq_completion(enum hvm_io_completion io_completion)return true; } -static gfn_t hvm_alloc_legacy_ioreq_gfn (struct hvm_ioreq_server *s) +static gfn_t hvm_alloc_legacy_ioreq_gfn (struct ioreq_server *s) {struct domain *d = s->target;unsigned int i;
@@ -80,7 +80,7 @@ static gfn_t hvm_alloc_legacy_ioreq_gfn(struct hvm_ioreq_server *s)return INVALID_GFN ; } -static gfn_t hvm_alloc_ioreq_gfn (struct hvm_ioreq_server *s) +static gfn_t hvm_alloc_ioreq_gfn (struct ioreq_server *s) {struct domain *d = s->target;unsigned int i;
@@ -98,7 +98,7 @@ static gfn_t hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s)return hvm_alloc_legacy_ioreq_gfn (s); } -static bool hvm_free_legacy_ioreq_gfn (struct hvm_ioreq_server *s, +static bool hvm_free_legacy_ioreq_gfn (struct ioreq_server *s,gfn_t gfn) {struct domain *d = s->target;
@@ -116,7 +116,7 @@ static bool hvm_free_legacy_ioreq_gfn(struct hvm_ioreq_server *s,return true; } -static void hvm_free_ioreq_gfn (struct hvm_ioreq_server *s,gfn_t gfn) +static void hvm_free_ioreq_gfn (struct ioreq_server *s,gfn_t gfn) {struct domain *d = s->target;unsigned int i =gfn_x (gfn) - d->arch.hvm.ioreq_gfn.base;
@@ -130,9 +130,9 @@ static void hvm_free_ioreq_gfn(struct hvm_ioreq_server *s, gfn_t gfn) } } -static void hvm_unmap_ioreq_gfn (struct hvm_ioreq_server *s,bool buf) +static void hvm_unmap_ioreq_gfn (struct ioreq_server *s,bool buf) { -struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq; +struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;if (gfn_eq (iorp->gfn,INVALID_GFN ) )return ;
@@ -144,10 +144,10 @@ static void hvm_unmap_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) iorp->gfn =INVALID_GFN ; } -static int hvm_map_ioreq_gfn (struct hvm_ioreq_server *s,bool buf) +static int hvm_map_ioreq_gfn (struct ioreq_server *s,bool buf) {struct domain *d = s->target; -struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq; +struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;int rc;if ( iorp->page )
@@ -180,11 +180,11 @@ static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)return rc; } -static void hvm_remove_ioreq_gfn (struct hvm_ioreq_server *s,bool buf) +static void hvm_remove_ioreq_gfn (struct ioreq_server *s,bool buf) {struct domain *d = s->target; -struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq; +struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;if (gfn_eq (iorp->gfn,INVALID_GFN ) )return ;
@@ -195,10 +195,10 @@ static void hvm_remove_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)clear_page (iorp->va); } -static int hvm_add_ioreq_gfn (struct hvm_ioreq_server *s,bool buf) +static int hvm_add_ioreq_gfn (struct ioreq_server *s,bool buf) {struct domain *d = s->target; -struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq; +struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;int rc;if (gfn_eq (iorp->gfn,INVALID_GFN ) )
@@ -214,7 +214,7 @@ static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)return rc; } -int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s) +int arch_ioreq_server_map_pages (struct ioreq_server *s) {int rc;
@@ -229,33 +229,33 @@ int arch_ioreq_server_map_pages(struct hvm_ioreq_server *s)return rc; } -void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s) +void arch_ioreq_server_unmap_pages (struct ioreq_server *s) {hvm_unmap_ioreq_gfn (s, true);hvm_unmap_ioreq_gfn (s, false); } -void arch_ioreq_server_enable (struct hvm_ioreq_server *s) +void arch_ioreq_server_enable (struct ioreq_server *s) {hvm_remove_ioreq_gfn (s, false);hvm_remove_ioreq_gfn (s, true); } -void arch_ioreq_server_disable (struct hvm_ioreq_server *s) +void arch_ioreq_server_disable (struct ioreq_server *s) {hvm_add_ioreq_gfn (s, true);hvm_add_ioreq_gfn (s, false); }/* Called when target domain is paused */ -void arch_ioreq_server_destroy (struct hvm_ioreq_server *s) +void arch_ioreq_server_destroy (struct ioreq_server *s) {p2m_set_ioreq_server (s->target,0 , s); }/* Called with ioreq_server lock held */ int arch_ioreq_server_map_mem_type (struct domain *d, -struct hvm_ioreq_server *s, +struct ioreq_server *s,uint32_t flags) {int rc =p2m_set_ioreq_server (d, flags, s);
xen/arch/x86/hvm/stdvga.c +1 -1
@@ -466,7 +466,7 @@ static int stdvga_mem_write(const struct hvm_io_handler *handler, .dir = IOREQ_WRITE, .data = data, }; -struct hvm_ioreq_server *srv; +struct ioreq_server *srv;if ( !stdvga_cache_is_enabled (s) || !s->stdvga ) goto done;
xen/arch/x86/mm/p2m.c +4 -4
@@ -367,7 +367,7 @@ void p2m_memory_type_changed(struct domain *d)int p2m_set_ioreq_server (struct domain *d,unsigned int flags, -struct hvm_ioreq_server *s) +struct ioreq_server *s) {struct p2m_domain *p2m =p2m_get_hostp2m (d);int rc;
@@ -415,11 +415,11 @@ int p2m_set_ioreq_server(struct domain *d,return rc; } -struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d, -unsigned int *flags) +struct ioreq_server *p2m_get_ioreq_server (struct domain *d, +unsigned int *flags) {struct p2m_domain *p2m =p2m_get_hostp2m (d); -struct hvm_ioreq_server *s; +struct ioreq_server *s;spin_lock (&p2m->ioreq.lock);
xen/common/ioreq.c +54 -54
352 lines not on this page · raw message
xen/include/asm-x86/hvm/domain.h +1 -35
@@ -30,40 +30,6 @@#include <public/hvm/dm_op.h> -struct hvm_ioreq_page { -gfn_t gfn; -struct page_info *page; -void *va; -}; - -struct hvm_ioreq_vcpu { -struct list_head list_entry; -struct vcpu *vcpu; -evtchn_port_t ioreq_evtchn; -bool pending; -}; - -#define NR_IO_RANGE_TYPES (XEN_DMOP_IO_RANGE_PCI + 1) -#define MAX_NR_IO_RANGES 256 - -struct hvm_ioreq_server { -struct domain *target, *emulator; - -/* Lock to serialize toolstack modifications */ -spinlock_t lock; - -struct hvm_ioreq_page ioreq; -struct list_head ioreq_vcpu_list; -struct hvm_ioreq_page bufioreq; - -/* Lock to serialize access to buffered ioreq ring */ -spinlock_t bufioreq_lock; -evtchn_port_t bufioreq_evtchn; -struct rangeset *range[NR_IO_RANGE_TYPES ]; -bool enabled; -uint8_t bufioreq_handling; -}; -#ifdef CONFIG_MEM_SHARING struct mem_sharing_domain {
@@ -110,7 +76,7 @@ struct hvm_domain {/* Lock protects all other values in the sub-struct and the default */ struct {spinlock_t lock; -struct hvm_ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; +struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; } ioreq_server;/* Cached CF8 for guest PCI config cycles */
xen/include/asm-x86/hvm/ioreq.h +6 -6
@@ -22,13 +22,13 @@#include <xen/ioreq.h> bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion); -int arch_ioreq_server_map_pages (struct hvm_ioreq_server *s); -void arch_ioreq_server_unmap_pages (struct hvm_ioreq_server *s); -void arch_ioreq_server_enable (struct hvm_ioreq_server *s); -void arch_ioreq_server_disable (struct hvm_ioreq_server *s); -void arch_ioreq_server_destroy (struct hvm_ioreq_server *s); +int arch_ioreq_server_map_pages (struct ioreq_server *s); +void arch_ioreq_server_unmap_pages (struct ioreq_server *s); +void arch_ioreq_server_enable (struct ioreq_server *s); +void arch_ioreq_server_disable (struct ioreq_server *s); +void arch_ioreq_server_destroy (struct ioreq_server *s);int arch_ioreq_server_map_mem_type (struct domain *d, -struct hvm_ioreq_server *s, +struct ioreq_server *s,uint32_t flags);bool arch_ioreq_server_destroy_all (struct domain *d); int arch_ioreq_server_get_type_addr(const struct domain *d,
xen/include/asm-x86/p2m.h +4 -4
@@ -363,7 +363,7 @@ struct p2m_domain {* ioreq server who's responsible for the emulation of * gfns with specific p2m type(for now, p2m_ioreq_server). */ -struct hvm_ioreq_server *server; +struct ioreq_server *server;/* * flags specifies whether read, write or both operations * are to be emulated by an ioreq server.
@@ -941,9 +941,9 @@ static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt, mfn_t mfn) }int p2m_set_ioreq_server (struct domain *d,unsigned int flags, -struct hvm_ioreq_server *s); -struct hvm_ioreq_server *p2m_get_ioreq_server (struct domain *d, -unsigned int *flags); +struct ioreq_server *s); +struct ioreq_server *p2m_get_ioreq_server (struct domain *d, +unsigned int *flags); static inline int p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt, p2m_type_t ot, mfn_t nfn, mfn_t ofn,
xen/include/xen/ioreq.h +37 -3
@@ -21,6 +21,40 @@#include <xen/sched.h> +struct ioreq_page { +gfn_t gfn; +struct page_info *page; +void *va; +}; + +struct ioreq_vcpu { +struct list_head list_entry; +struct vcpu *vcpu; +evtchn_port_t ioreq_evtchn; +bool pending; +}; + +#define NR_IO_RANGE_TYPES (XEN_DMOP_IO_RANGE_PCI + 1) +#define MAX_NR_IO_RANGES 256 + +struct ioreq_server { +struct domain *target, *emulator; + +/* Lock to serialize toolstack modifications */ +spinlock_t lock; + +struct ioreq_page ioreq; +struct list_head ioreq_vcpu_list; +struct ioreq_page bufioreq; + +/* Lock to serialize access to buffered ioreq ring */ +spinlock_t bufioreq_lock; +evtchn_port_t bufioreq_evtchn; +struct rangeset *range[NR_IO_RANGE_TYPES ]; +bool enabled; +uint8_t bufioreq_handling; +}; +static inline paddr_t ioreq_mmio_first_byte (const ioreq_t *p) { return unlikely(p->df) ?
@@ -75,9 +109,9 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v);void hvm_all_ioreq_servers_remove_vcpu (struct domain *d,struct vcpu *v);void hvm_destroy_all_ioreq_servers (struct domain *d); -struct hvm_ioreq_server *hvm_select_ioreq_server (struct domain *d, -ioreq_t *p); -int hvm_send_ioreq (struct hvm_ioreq_server *s,ioreq_t *proto_p, +struct ioreq_server *hvm_select_ioreq_server (struct domain *d, +ioreq_t *p); +int hvm_send_ioreq (struct ioreq_server *s,ioreq_t *proto_p,bool buffered);unsigned int hvm_broadcast_ioreq (ioreq_t *p,bool buffered);
Signature · 1 line
2.7.4
Re: [PATCH V3 07/23] xen/ioreq: Make x86's hvm_ioreq_(page/vcpu/server) structs common
Quoted 6 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and these structs will be used on Arm as is. Move them to
xen/ioreq.hand remove "hvm" prefixes.Signed-off-by Oleksandr Tyshchenko <[email protected]>
Applicable parts
Jan
PATCH v3 8/23 xen/ioreq: Move x86's ioreq_server to struct domain
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and this struct will be used on Arm as is. Move it to common struct domain. This also significantly reduces the layering violation in the common code (*arch.hvm* usage).
We don't move ioreq_gfn since it is not used in the common code (the "legacy" mechanism is x86 specific).
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V1 -> V2:
- new patch
Changes V2 -> V3:
- remove the mention of "ioreq_gfn" from patch subject/description
- update patch according the "legacy interface" is x86 specific
- drop hvm_params related changes in arch/x86/hvm/hvm.c
- leave ioreq_gfn in hvm_domain
---
--- xen/common/ioreq.c | 60 ++++++++++++++++++++-------------------- xen/include/asm-x86/hvm/domain.h | 8 ------ xen/include/xen/sched.h | 10 +++++++ 3 files changed, 40 insertions(+), 38 deletions(-)
xen/common/ioreq.c +30 -30
@@ -38,13 +38,13 @@ static void set_ioreq_server(struct domain *d, unsigned int id, struct ioreq_server *s) {ASSERT (id <MAX_NR_IOREQ_SERVERS ); -ASSERT (!s || !d->arch.hvm.ioreq_server.server[id]); +ASSERT (!s || !d->ioreq_server.server[id]); - d->arch.hvm.ioreq_server.server[id] = s; + d->ioreq_server.server[id] = s; }#define GET_IOREQ_SERVER (d, id) \ - (d)->arch.hvm.ioreq_server.server[id] + (d)->ioreq_server.server[id]static struct ioreq_server *get_ioreq_server (const struct domain *d,unsigned int id)
@@ -285,7 +285,7 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)unsigned int id;bool found = false; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); FOR_EACH_IOREQ_SERVER(d, id, s) {
@@ -296,7 +296,7 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page) } } -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return found; }
@@ -606,7 +606,7 @@ int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling,return -ENOMEM ;domain_pause (d); -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock);for ( i =0 ; i <MAX_NR_IOREQ_SERVERS ; i++ ) {
@@ -634,13 +634,13 @@ int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling,if ( id ) *id = i; -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);domain_unpause (d);return 0 ; fail: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);domain_unpause (d);xfree (s);
@@ -652,7 +652,7 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)struct ioreq_server *s;int rc; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -684,7 +684,7 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) rc =0 ; out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -697,7 +697,7 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,struct ioreq_server *s;int rc; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -731,7 +731,7 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, rc =0 ; out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -744,7 +744,7 @@ int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id,ASSERT (is_hvm_domain (d)); -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -782,7 +782,7 @@ int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, } out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -798,7 +798,7 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,if ( start > end )return -EINVAL ; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -834,7 +834,7 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, rc =rangeset_add_range (r, start, end); out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -850,7 +850,7 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,if ( start > end )return -EINVAL ; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -886,7 +886,7 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, rc =rangeset_remove_range (r, start, end); out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -911,7 +911,7 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,if ( flags & ~XEN_DMOP_IOREQ_MEM_ACCESS_WRITE )return -EINVAL ; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -926,7 +926,7 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, rc =arch_ioreq_server_map_mem_type (d, s, flags); out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -937,7 +937,7 @@ int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id,struct ioreq_server *s;int rc; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); s =get_ioreq_server (d, id);
@@ -961,7 +961,7 @@ int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, rc =0 ; out: -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -971,7 +971,7 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v)unsigned int id;int rc; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); FOR_EACH_IOREQ_SERVER(d, id, s) {
@@ -980,7 +980,7 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v) goto fail; } -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return 0 ;
@@ -995,7 +995,7 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v)hvm_ioreq_server_remove_vcpu (s, v); } -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock);return rc; }
@@ -1005,12 +1005,12 @@ void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v)struct ioreq_server *s;unsigned int id; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock); FOR_EACH_IOREQ_SERVER(d, id, s)hvm_ioreq_server_remove_vcpu (s, v); -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock); }void hvm_destroy_all_ioreq_servers (struct domain *d)
@@ -1021,7 +1021,7 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)if ( !arch_ioreq_server_destroy_all (d) )return ; -spin_lock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_lock_recursive (&d->ioreq_server.lock);/* No need to domain_pause() as the domain is being torn down */
@@ -1039,7 +1039,7 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)xfree (s); } -spin_unlock_recursive (&d->arch.hvm.ioreq_server.lock); +spin_unlock_recursive (&d->ioreq_server.lock); } struct ioreq_server *hvm_select_ioreq_server(struct domain *d,
@@ -1271,7 +1271,7 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered)void hvm_ioreq_init (struct domain *d) { -spin_lock_init (&d->arch.hvm.ioreq_server.lock); +spin_lock_init (&d->ioreq_server.lock);arch_ioreq_domain_init (d); }
xen/include/asm-x86/hvm/domain.h +0 -8
@@ -63,8 +63,6 @@ struct hvm_pi_ops {void (*vcpu_block)(struct vcpu *); }; -#define MAX_NR_IOREQ_SERVERS 8 -struct hvm_domain {/* Guest page range used for non-default ioreq servers */ struct {
@@ -73,12 +71,6 @@ struct hvm_domain {unsigned long legacy_mask;/* indexed by HVM param number */ } ioreq_gfn; -/* Lock protects all other values in the sub-struct and the default */ -struct { -spinlock_t lock; -struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; - } ioreq_server; -/* Cached CF8 for guest PCI config cycles */ uint32_t pci_cf8;
xen/include/xen/sched.h +10 -0
@@ -316,6 +316,8 @@ struct sched_unit {struct evtchn_port_ops ; +#define MAX_NR_IOREQ_SERVERS 8 +struct domain {domid_t domain_id;
@@ -523,6 +525,14 @@ struct domain/* Argo interdomain communication support */ struct argo_domain *argo;#endif + +#ifdef CONFIG_IOREQ_SERVER +/* Lock protects all other values in the sub-struct and the default */ +struct { +spinlock_t lock; +struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; + } ioreq_server; +#endif }; static inline struct page_list_head *page_to_list(
Signature · 1 line
2.7.4
Re: [PATCH V3 08/23] xen/ioreq: Move x86's ioreq_server to struct domain
Quoted 11 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and this struct will be used on Arm as is. Move it to common struct domain. This also significantly reduces the layering violation in the common code (*arch.hvm* usage).
We don't move
ioreq_gfnsince it is not used in the common code (the "legacy" mechanism is x86 specific).Signed-off-by Oleksandr Tyshchenko <[email protected]>
Applicable parts
yet with a question, but maybe more to Paul than to you:
Quoted 37 lines
xen/include/asm-x86/hvm/domain.h +0
-8@@ -63,8 +63,6 @@ struct hvm_pi_ops {void (*vcpu_block)(struct vcpu *); }; -#define MAX_NR_IOREQ_SERVERS 8 -struct hvm_domain {/* Guest page range used for non-default ioreq servers */ struct {@@ -73,12 +71,6 @@ struct hvm_domain {unsigned long legacy_mask;/* indexed by HVM param number */ } ioreq_gfn; -/* Lock protects all other values in the sub-struct and the default */ -struct { -spinlock_t lock; -struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; - } ioreq_server; -/* Cached CF8 for guest PCI config cycles */ uint32_t pci_cf8;xen/include/xen/sched.h +2
-0@@ -316,6 +316,8 @@ struct sched_unit {struct evtchn_port_ops ; +#define MAX_NR_IOREQ_SERVERS 8 +struct domain {domid_t domain_id;@@ -523,6 +525,14 @@ struct domain/* Argo interdomain communication support */
xen/include/xen/sched.h struct domain · line 535
Quoted 10 lines
xen/include/xen/sched.h +8
-0struct argo_domain *argo;#endif + +#ifdef CONFIG_IOREQ_SERVER +/* Lock protects all other values in the sub-struct and the default */ +struct { +spinlock_t lock; +struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; + } ioreq_server; +#endif
The comment gets merely moved, but what "default" does it talk about? Is this a stale part which would better be dropped at this occasion?
Jan
RE: [PATCH V3 08/23] xen/ioreq: Move x86's ioreq_server to struct domain
Quoted 69 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 07 December 2020 12:05
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monné <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 08/23] xen/ioreq: Move x86'sioreq_serverto struct domainQuoted 11 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and this struct will be used on Arm as is. Move it to common struct domain. This also significantly reduces the layering violation in the common code (*arch.hvm* usage).
We don't move
ioreq_gfnsince it is not used in the common code (the "legacy" mechanism is x86 specific).Signed-off-by Oleksandr Tyshchenko <[email protected]>Applicable parts
Acked-by Jan Beulich <[email protected]>yet with a question, but maybe more to Paul than to you:
Quoted 42 lines
xen/include/asm-x86/hvm/domain.h +0
-8@@ -63,8 +63,6 @@ struct hvm_pi_ops {void (*vcpu_block)(struct vcpu *); }; -#define MAX_NR_IOREQ_SERVERS 8 -struct hvm_domain {/* Guest page range used for non-default ioreq servers */ struct {@@ -73,12 +71,6 @@ struct hvm_domain {unsigned long legacy_mask;/* indexed by HVM param number */ } ioreq_gfn; -/* Lock protects all other values in the sub-struct and the default */ -struct { -spinlock_t lock; -struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; - } ioreq_server; -/* Cached CF8 for guest PCI config cycles */ uint32_t pci_cf8;xen/include/xen/sched.h +5
-0@@ -316,6 +316,8 @@ struct sched_unit {struct evtchn_port_ops ; +#define MAX_NR_IOREQ_SERVERS 8 +struct domain {domid_t domain_id;@@ -523,6 +525,14 @@ struct domain/* Argo interdomain communication support */ struct argo_domain *argo;#endif + +#ifdef CONFIG_IOREQ_SERVER +/* Lock protects all other values in the sub-struct and the default */
Quoted 9 lines
xen/include/xen/sched.h struct domain · line 535
Quoted 5 lines
xen/include/xen/sched.h +5
-0+struct { +spinlock_t lock; +struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; + } ioreq_server; +#endif The comment gets merely moved, but what "default" does it talk about? Is this a stale part which would better be dropped at this occasion?
Yes, I think that is a stale part of the comment from the days of the default ioreq server. It can be dropped.
Paul
Quoted 1 line
Jan
Re: [PATCH V3 08/23] xen/ioreq: Move x86's ioreq_server to struct domain
In reply to Jan BeulichOn 07.12.20 14:04, Jan Beulich wrote:
Hi Jan
Quoted 8 lines · Oleksandr Tyshchenko
Quoted 7 lines
From: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and this struct will be used on Arm as is. Move it to common struct domain. This also significantly reduces the layering violation in the common code (*arch.hvm* usage).
Quoted 6 lines
Quoted 4 lines
We don't move
ioreq_gfnsince it is not used in the common code (the "legacy" mechanism is x86 specific).Signed-off-by Oleksandr Tyshchenko <[email protected]>Applicable parts
Acked-by Jan Beulich <[email protected]>
Thank you.
Quoted 43 lines
yet with a question, but maybe more to Paul than to you:
Quoted 41 lines
xen/include/asm-x86/hvm/domain.h +0
-8@@ -63,8 +63,6 @@ struct hvm_pi_ops {void (*vcpu_block)(struct vcpu *); }; -#define MAX_NR_IOREQ_SERVERS 8 -struct hvm_domain {/* Guest page range used for non-default ioreq servers */ struct {@@ -73,12 +71,6 @@ struct hvm_domain {unsigned long legacy_mask;/* indexed by HVM param number */ } ioreq_gfn; -/* Lock protects all other values in the sub-struct and the default */ -struct { -spinlock_t lock; -struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; - } ioreq_server; -/* Cached CF8 for guest PCI config cycles */ uint32_t pci_cf8;xen/include/xen/sched.h +4
-0@@ -316,6 +316,8 @@ struct sched_unit {struct evtchn_port_ops ; +#define MAX_NR_IOREQ_SERVERS 8 +struct domain {domid_t domain_id;@@ -523,6 +525,14 @@ struct domain/* Argo interdomain communication support */ struct argo_domain *argo;#endif + +#ifdef CONFIG_IOREQ_SERVER
Quoted 8 lines
xen/include/xen/sched.h struct domain · line 535
Quoted 6 lines
xen/include/xen/sched.h +6
-0+/* Lock protects all other values in the sub-struct and the default */ +struct { +spinlock_t lock; +struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; + } ioreq_server; +#endif The comment gets merely moved, but what "default" does it talk about? Is this a stale part which would better be dropped at this occasion?
I saw Paul's answer, will drop stale part.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 9/23 xen/dm: Make x86's DM feature common
In reply to Oleksandr TyshchenkoFrom: Julien Grall <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts.
The common DM feature is supposed to be built with IOREQ_SERVER option enabled (as well as the IOREQ feature), which is selected for x86's config HVM for now.
Also update XSM code a bit to let DM op be used on Arm.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- update XSM, related changes were pulled from:
[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM features
Changes V1 -> V2:
- update the author of a patch
- update patch description
- introduce xen/dm.h and move definitions here
Changes V2 -> V3:
- no changes
---
--- xen/arch/x86/hvm/dm.c | 291 ++++-------------------------------------------- xen/common/Makefile | 1 + xen/common/dm.c | 291 ++++++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/dm.h | 44 ++++++++ xen/include/xsm/dummy.h | 4 +- xen/include/xsm/xsm.h | 6 +- xen/xsm/dummy.c | 2 +- xen/xsm/flask/hooks.c | 5 +- 8 files changed, 364 insertions(+), 280 deletions(-) create mode 100644 xen/common/dm.c create mode 100644 xen/include/xen/dm.h
xen/arch/x86/hvm/dm.c +20 -271
@@ -16,6 +16,7 @@#include <xen/event.h> #include <xen/guest_access.h> +#include <xen/dm.h> #include <xen/hypercall.h> #include <xen/nospec.h> #include <xen/sched.h>
@@ -29,13 +30,6 @@#include <public/hvm/hvm_op.h> -struct dmop_args { -domid_t domid; -unsigned int nr_bufs; -/* Reserve enough buf elements for all current hypercalls. */ -struct xen_dm_op_buf buf[2 ]; -}; - static bool _raw_copy_from_guest_buf_offset(void *dst, const struct dmop_args *args, unsigned int buf_idx,
@@ -338,148 +332,20 @@ static int inject_event(struct domain *d,return 0 ; } -static int dm_op (const struct dmop_args *op_args) +int arch_dm_op (struct xen_dm_op *op,struct domain *d, +const struct dmop_args *op_args,bool *const_op) { -struct domain *d; -struct xen_dm_op op; -bool const_op = true;long rc; -size_t offset; - -static const uint8_t op_size[] = { - [XEN_DMOP_create_ioreq_server] =sizeof (struct xen_dm_op_create_ioreq_server ), - [XEN_DMOP_get_ioreq_server_info] =sizeof (struct xen_dm_op_get_ioreq_server_info ), - [XEN_DMOP_map_io_range_to_ioreq_server] =sizeof (struct xen_dm_op_ioreq_server_range ), - [XEN_DMOP_unmap_io_range_from_ioreq_server] =sizeof (struct xen_dm_op_ioreq_server_range ), - [XEN_DMOP_set_ioreq_server_state] =sizeof (struct xen_dm_op_set_ioreq_server_state ), - [XEN_DMOP_destroy_ioreq_server] =sizeof (struct xen_dm_op_destroy_ioreq_server ), - [XEN_DMOP_track_dirty_vram] =sizeof (struct xen_dm_op_track_dirty_vram ), - [XEN_DMOP_set_pci_intx_level] =sizeof (struct xen_dm_op_set_pci_intx_level ), - [XEN_DMOP_set_isa_irq_level] =sizeof (struct xen_dm_op_set_isa_irq_level ), - [XEN_DMOP_set_pci_link_route] =sizeof (struct xen_dm_op_set_pci_link_route ), - [XEN_DMOP_modified_memory] =sizeof (struct xen_dm_op_modified_memory ), - [XEN_DMOP_set_mem_type] =sizeof (struct xen_dm_op_set_mem_type ), - [XEN_DMOP_inject_event] =sizeof (struct xen_dm_op_inject_event ), - [XEN_DMOP_inject_msi] =sizeof (struct xen_dm_op_inject_msi ), - [XEN_DMOP_map_mem_type_to_ioreq_server] =sizeof (struct xen_dm_op_map_mem_type_to_ioreq_server ), - [XEN_DMOP_remote_shutdown] =sizeof (struct xen_dm_op_remote_shutdown ), - [XEN_DMOP_relocate_memory] =sizeof (struct xen_dm_op_relocate_memory ), - [XEN_DMOP_pin_memory_cacheattr] =sizeof (struct xen_dm_op_pin_memory_cacheattr ), - }; - - rc =rcu_lock_remote_domain_by_id (op_args->domid, &d); -if ( rc ) -return rc; - -if ( !is_hvm_domain (d) ) - goto out; - - rc =xsm_dm_op (XSM_DM_PRIV , d); -if ( rc ) - goto out; - - offset = offsetof(struct xen_dm_op , u); - - rc = -EFAULT ; -if ( op_args->buf[0 ].size < offset ) - goto out; - -if (copy_from_guest_offset ((void *)&op, op_args->buf[0 ].h,0 , offset) ) - goto out; - -if ( op.op >=ARRAY_SIZE (op_size) ) - { - rc = -EOPNOTSUPP ; - goto out; - } - - op.op =array_index_nospec (op.op,ARRAY_SIZE (op_size)); - -if ( op_args->buf[0 ].size < offset + op_size[op.op] ) - goto out; - -if (copy_from_guest_offset ((void *)&op.u, op_args->buf[0 ].h, offset, - op_size[op.op]) ) - goto out; - - rc = -EINVAL ; -if ( op.pad ) - goto out; - -switch ( op.op ) - { -case XEN_DMOP_create_ioreq_server: - { -struct xen_dm_op_create_ioreq_server *data = - &op.u.create_ioreq_server; - - const_op = false; - - rc = -EINVAL ; -if ( data->pad[0 ] || data->pad[1 ] || data->pad[2 ] ) -break ; - - rc =hvm_create_ioreq_server (d, data->handle_bufioreq, - &data->id); -break ; - } -case XEN_DMOP_get_ioreq_server_info: +switch ( op->op ) { -struct xen_dm_op_get_ioreq_server_info *data = - &op.u.get_ioreq_server_info; -const uint16_t valid_flags = XEN_DMOP_no_gfns; - - const_op = false; - - rc = -EINVAL ; -if ( data->flags & ~valid_flags ) -break ; - - rc =hvm_get_ioreq_server_info (d, data->id, - (data->flags & XEN_DMOP_no_gfns) ? -NULL : &data->ioreq_gfn, - (data->flags & XEN_DMOP_no_gfns) ? -NULL : &data->bufioreq_gfn, - &data->bufioreq_port); -break ; - } - -case XEN_DMOP_map_io_range_to_ioreq_server: - { -const struct xen_dm_op_ioreq_server_range *data = - &op.u.map_io_range_to_ioreq_server; - - rc = -EINVAL ; -if ( data->pad ) -break ; - - rc =hvm_map_io_range_to_ioreq_server (d, data->id, data->type, - data->start, data->end); -break ; - } - -case XEN_DMOP_unmap_io_range_from_ioreq_server: - { -const struct xen_dm_op_ioreq_server_range *data = - &op.u.unmap_io_range_from_ioreq_server; - - rc = -EINVAL ; -if ( data->pad ) -break ; - - rc =hvm_unmap_io_range_from_ioreq_server (d, data->id, data->type, - data->start, data->end); -break ; - } -case XEN_DMOP_map_mem_type_to_ioreq_server: {struct xen_dm_op_map_mem_type_to_ioreq_server *data = - &op.u.map_mem_type_to_ioreq_server; + &op->u.map_mem_type_to_ioreq_server;unsigned long first_gfn = data->opaque; - const_op = false; + *const_op = false; rc = -EOPNOTSUPP ;if ( !hap_enabled (d) )
@@ -523,36 +389,10 @@ static int dm_op(const struct dmop_args *op_args)break ; } -case XEN_DMOP_set_ioreq_server_state: - { -const struct xen_dm_op_set_ioreq_server_state *data = - &op.u.set_ioreq_server_state; - - rc = -EINVAL ; -if ( data->pad ) -break ; - - rc =hvm_set_ioreq_server_state (d, data->id, !!data->enabled); -break ; - } - -case XEN_DMOP_destroy_ioreq_server: - { -const struct xen_dm_op_destroy_ioreq_server *data = - &op.u.destroy_ioreq_server; - - rc = -EINVAL ; -if ( data->pad ) -break ; - - rc =hvm_destroy_ioreq_server (d, data->id); -break ; - } -case XEN_DMOP_track_dirty_vram: {const struct xen_dm_op_track_dirty_vram *data = - &op.u.track_dirty_vram; + &op->u.track_dirty_vram; rc = -EINVAL ;if ( data->pad )
@@ -568,7 +408,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_set_pci_intx_level: {const struct xen_dm_op_set_pci_intx_level *data = - &op.u.set_pci_intx_level; + &op->u.set_pci_intx_level; rc = set_pci_intx_level(d, data->domain, data->bus, data->device, data->intx,
@@ -579,7 +419,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_set_isa_irq_level: {const struct xen_dm_op_set_isa_irq_level *data = - &op.u.set_isa_irq_level; + &op->u.set_isa_irq_level; rc =set_isa_irq_level (d, data->isa_irq, data->level);break ;
@@ -588,7 +428,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_set_pci_link_route: {const struct xen_dm_op_set_pci_link_route *data = - &op.u.set_pci_link_route; + &op->u.set_pci_link_route; rc =hvm_set_pci_link_route (d, data->link, data->isa_irq);break ;
@@ -597,19 +437,19 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_modified_memory: {struct xen_dm_op_modified_memory *data = - &op.u.modified_memory; + &op->u.modified_memory; rc =modified_memory (d, op_args, data); - const_op = !rc; + *const_op = !rc;break ; }case XEN_DMOP_set_mem_type: {struct xen_dm_op_set_mem_type *data = - &op.u.set_mem_type; + &op->u.set_mem_type; - const_op = false; + *const_op = false; rc = -EINVAL ;if ( data->pad )
@@ -622,7 +462,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_inject_event: {const struct xen_dm_op_inject_event *data = - &op.u.inject_event; + &op->u.inject_event; rc = -EINVAL ;if ( data->pad0 || data->pad1 )
@@ -635,7 +475,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_inject_msi: {const struct xen_dm_op_inject_msi *data = - &op.u.inject_msi; + &op->u.inject_msi; rc = -EINVAL ;if ( data->pad )
@@ -648,7 +488,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_remote_shutdown: {const struct xen_dm_op_remote_shutdown *data = - &op.u.remote_shutdown; + &op->u.remote_shutdown;domain_shutdown (d, data->reason); rc =0 ;
@@ -657,7 +497,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_relocate_memory: { -struct xen_dm_op_relocate_memory *data = &op.u.relocate_memory; +struct xen_dm_op_relocate_memory *data = &op->u.relocate_memory;struct xen_add_to_physmap xatp = { .domid = op_args->domid, .size = data->size,
@@ -680,7 +520,7 @@ static int dm_op(const struct dmop_args *op_args) data->size -= rc; data->src_gfn += rc; data->dst_gfn += rc; - const_op = false; + *const_op = false; rc = -ERESTART ; }break ;
@@ -689,7 +529,7 @@ static int dm_op(const struct dmop_args *op_args)case XEN_DMOP_pin_memory_cacheattr: {const struct xen_dm_op_pin_memory_cacheattr *data = - &op.u.pin_memory_cacheattr; + &op->u.pin_memory_cacheattr;if ( data->pad ) {
@@ -707,97 +547,6 @@ static int dm_op(const struct dmop_args *op_args)break ; } -if ( (!rc || rc == -ERESTART ) && - !const_op &©_to_guest_offset (op_args->buf[0 ].h, offset, - (void *)&op.u, op_size[op.op]) ) - rc = -EFAULT ; - - out: -rcu_unlock_domain (d); - -return rc; -} - -#include <compat/hvm/dm_op.h> - -CHECK_dm_op_create_ioreq_server; -CHECK_dm_op_get_ioreq_server_info; -CHECK_dm_op_ioreq_server_range; -CHECK_dm_op_set_ioreq_server_state; -CHECK_dm_op_destroy_ioreq_server; -CHECK_dm_op_track_dirty_vram; -CHECK_dm_op_set_pci_intx_level; -CHECK_dm_op_set_isa_irq_level; -CHECK_dm_op_set_pci_link_route; -CHECK_dm_op_modified_memory; -CHECK_dm_op_set_mem_type; -CHECK_dm_op_inject_event; -CHECK_dm_op_inject_msi; -CHECK_dm_op_map_mem_type_to_ioreq_server; -CHECK_dm_op_remote_shutdown; -CHECK_dm_op_relocate_memory; -CHECK_dm_op_pin_memory_cacheattr; - -int compat_dm_op (domid_t domid, -unsigned int nr_bufs, -XEN_GUEST_HANDLE_PARAM (void ) bufs) -{ -struct dmop_args args; -unsigned int i; -int rc; - -if ( nr_bufs >ARRAY_SIZE (args.buf) ) -return -E2BIG ; - - args.domid = domid; - args.nr_bufs =array_index_nospec (nr_bufs,ARRAY_SIZE (args.buf) +1 ); - -for ( i =0 ; i < args.nr_bufs; i++ ) - { -struct compat_dm_op_buf cmp; - -if (copy_from_guest_offset (&cmp, bufs, i,1 ) ) -return -EFAULT ; - -#define XLAT_dm_op_buf_HNDL_h (_d_, _s_) \ - guest_from_compat_handle((_d_)->h, (_s_)->h) - -XLAT_dm_op_buf (&args.buf[i], &cmp); - -#undef XLAT_dm_op_buf_HNDL_h - } - - rc =dm_op (&args); - -if ( rc == -ERESTART ) - rc =hypercall_create_continuation (__HYPERVISOR_dm_op,"iih" , - domid, nr_bufs, bufs); - -return rc; -} - -long do_dm_op (domid_t domid, -unsigned int nr_bufs, -XEN_GUEST_HANDLE_PARAM (xen_dm_op_buf_t) bufs) -{ -struct dmop_args args; -int rc; - -if ( nr_bufs >ARRAY_SIZE (args.buf) ) -return -E2BIG ; - - args.domid = domid; - args.nr_bufs =array_index_nospec (nr_bufs,ARRAY_SIZE (args.buf) +1 ); - -if (copy_from_guest_offset (&args.buf[0 ], bufs,0 , args.nr_bufs) ) -return -EFAULT ; - - rc =dm_op (&args); - -if ( rc == -ERESTART ) - rc =hypercall_create_continuation (__HYPERVISOR_dm_op,"iih" , - domid, nr_bufs, bufs); -return rc; }
xen/common/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_CORE_PARKING) += core_parking.o obj-y += cpu.o obj-$(CONFIG_DEBUG_TRACE) += debugtrace.o obj-$(CONFIG_HAS_DEVICE_TREE) += device_tree.o +obj-$(CONFIG_IOREQ_SERVER) += dm.o obj-y += domain.o obj-y += event_2l.o obj-y += event_channel.o
xen/common/dm.c new file +291 -0
291 lines not on this page · raw message
xen/include/xen/dm.h new file +44 -0
44 lines not on this page · raw message
xen/include/xsm/dummy.h +2 -2
@@ -707,14 +707,14 @@ static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG struct domain *d, unsigned int } } +#endif /* CONFIG_X86 */ + static XSM_INLINE int xsm_dm_op(XSM_DEFAULT_ARG struct domain *d) {XSM_ASSERT_ACTION (XSM_DM_PRIV );return xsm_default_action (action, current->domain, d); } -#endif /* CONFIG_X86 */ -#ifdef CONFIG_ARGO static XSM_INLINE int xsm_argo_enable(const struct domain *d) {
xen/include/xsm/xsm.h +3 -3
@@ -176,8 +176,8 @@ struct xsm_operations {int (*ioport_permission) (struct domain *d,uint32_t s,uint32_t e,uint8_t allow);int (*ioport_mapping) (struct domain *d,uint32_t s,uint32_t e,uint8_t allow);int (*pmu_op) (struct domain *d,unsigned int op); -int (*dm_op) (struct domain *d);#endif +int (*dm_op) (struct domain *d);int (*xen_version) (uint32_t cmd);int (*domain_resource_map) (struct domain *d);#ifdef CONFIG_ARGO
@@ -682,13 +682,13 @@ static inline int xsm_pmu_op (xsm_default_t def, struct domain *d, unsigned int return xsm_ops->pmu_op(d, op); } +#endif /* CONFIG_X86 */ +static inline int xsm_dm_op (xsm_default_t def,struct domain *d) {return xsm_ops->dm_op (d); } -#endif /* CONFIG_X86 */ -static inline int xsm_xen_version (xsm_default_t def,uint32_t op) {return xsm_ops->xen_version (op);
xen/xsm/dummy.c +1 -1
@@ -147,8 +147,8 @@ void __init xsm_fixup_ops (struct xsm_operations *ops)set_to_dummy_if_null (ops, ioport_permission);set_to_dummy_if_null (ops, ioport_mapping);set_to_dummy_if_null (ops, pmu_op); -set_to_dummy_if_null (ops, dm_op);#endif +set_to_dummy_if_null (ops, dm_op);set_to_dummy_if_null (ops, xen_version);set_to_dummy_if_null (ops, domain_resource_map);#ifdef CONFIG_ARGO
xen/xsm/flask/hooks.c +2 -3
@@ -1656,14 +1656,13 @@ static int flask_pmu_op (struct domain *d, unsigned int op) return -EPERM; } } +#endif /* CONFIG_X86 */ static int flask_dm_op (struct domain *d) {return current_has_perm (d,SECCLASS_HVM ,HVM__DM ); } -#endif /* CONFIG_X86 */ -static int flask_xen_version (uint32_t op) {u32 dsid =domain_sid (current->domain);
@@ -1865,8 +1864,8 @@ static struct xsm_operations flask_ops = { .ioport_permission = flask_ioport_permission, .ioport_mapping = flask_ioport_mapping, .pmu_op = flask_pmu_op, - .dm_op = flask_dm_op, #endif + .dm_op = flask_dm_op, .xen_version = flask_xen_version, .domain_resource_map = flask_domain_resource_map, #ifdef CONFIG_ARGO
Signature · 1 line
2.7.4
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
Quoted 25 lines · Oleksandr Tyshchenko
From: Julien Grall <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts.
The common DM feature is supposed to be built with
IOREQ_SERVERoption enabled (as well as the IOREQ feature), which is selected for x86's config HVM for now.Also update XSM code a bit to let DM op be used on Arm.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- update XSM, related changes were pulled from:
[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM features
Quoted 7 lines
Changes V1 -> V2:
- update the author of a patch
- update patch description
- introducexen/dm.hand move definitions hereChanges V2 -> V3:
- no changes
And my concern regarding the common vs arch nesting also hasn't changed.
Jan
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
On 07.12.20 14:08, Jan Beulich wrote:
Hi Jan.
Quoted 31 lines · Oleksandr Tyshchenko
Quoted 30 lines
From: Julien Grall <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts.
The common DM feature is supposed to be built with
IOREQ_SERVERoption enabled (as well as the IOREQ feature), which is selected for x86's config HVM for now.Also update XSM code a bit to let DM op be used on Arm.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- update XSM, related changes were pulled from:
[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM featuresChanges V1 -> V2:
- update the author of a patch
- update patch description
- introducexen/dm.hand move definitions here
Quoted 4 lines
Quoted 2 lines
Changes V2 -> V3:
- no changesAnd my concern regarding the common vs arch nesting also hasn't changed.
I am sorry, I might misread your comment, but I failed to see any obvious to me request(s) for changes. I have just re-read previous discussion...
So the question about considering doing it the other way around (top level dm-op handling arch-specific and call into e.g. ioreq_server_dm_op() for otherwise unhandled ops) is exactly a concern which I should have addressed?
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
Quoted 38 lines · Oleksandr
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Oleksandr Tyshchenko
From: Julien Grall <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts.
The common DM feature is supposed to be built with
IOREQ_SERVERoption enabled (as well as the IOREQ feature), which is selected for x86's config HVM for now.Also update XSM code a bit to let DM op be used on Arm.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- update XSM, related changes were pulled from:
[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM featuresChanges V1 -> V2:
- update the author of a patch
- update patch description
- introducexen/dm.hand move definitions hereQuoted 4 lines
Quoted 2 lines
Changes V2 -> V3:
- no changesAnd my concern regarding the common vs arch nesting also hasn't changed.
Quoted 7 lines
I am sorry, I might misread your comment, but I failed to see any obvious to me request(s) for changes.
I have just re-read previous discussion...
So the question about considering doing it the other way around (top level dm-op handling arch-specific
and call into e.g.ioreq_server_dm_op()for otherwise unhandled ops) is exactly a concern which I should have addressed?
Well, on v2 you replied you didn't consider the alternative. I would have expected that you would at least go through this consideration process, and see whether there are better reasons to stick with the apparently backwards arrangement than to change to the more conventional one. If there are such reasons, I would expect them to be called out in reply and perhaps also in the commit message; the latter because down the road more people may wonder why the more narrow / special set of cases gets handled at a higher layer than the wider set of remaining ones, and they would then be able to find an explanation without having to resort to searching through list archives.
Jan
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
On 08.12.20 11:30, Jan Beulich wrote:
Hi Jan
Quoted 45 lines · Oleksandr
Quoted 37 lines · Jan Beulich
Quoted 31 lines · Oleksandr Tyshchenko
Quoted 30 lines
From: Julien Grall <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts.
The common DM feature is supposed to be built with
IOREQ_SERVERoption enabled (as well as the IOREQ feature), which is selected for x86's config HVM for now.Also update XSM code a bit to let DM op be used on Arm.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- update XSM, related changes were pulled from:
[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM featuresChanges V1 -> V2:
- update the author of a patch
- update patch description
- introducexen/dm.hand move definitions hereQuoted 4 lines
Quoted 2 lines
Changes V2 -> V3:
- no changesAnd my concern regarding the common vs arch nesting also hasn't changed.
Quoted 7 lines
I am sorry, I might misread your comment, but I failed to see any obvious to me request(s) for changes.
I have just re-read previous discussion...
So the question about considering doing it the other way around (top level dm-op handling arch-specific
and call into e.g.ioreq_server_dm_op()for otherwise unhandled ops) is exactly a concern which I should have addressed?
Quoted 11 lines
Well, on v2 you replied you didn't consider the alternative. I would have expected that you would at least go through this consideration process, and see whether there are better reasons to stick with the apparently backwards arrangement than to change to the more conventional one. If there are such reasons, I would expect them to be called out in reply and perhaps also in the commit message; the latter because down the road more people may wonder why the more narrow / special set of cases gets handled at a higher layer than the wider set of remaining ones, and they would then be able to find an explanation without having to resort to searching through list archives.
Ah, will investigate. Sorry for not paying enough attention to it.
Yes, IOREQ (I mean "common") ops are 7 out of 18 right now. The subsequent patch is adding one more DM op - XEN_DMOP_set_irq_level. There are several PCI related ops which might want to be common in the future (but I am not sure).
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
Hi Jan
Quoted 54 lines
Quoted 43 lines
Quoted 36 lines
Quoted 31 lines · Oleksandr Tyshchenko
Quoted 30 lines
From: Julien Grall <[email protected]>
As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts.
The common DM feature is supposed to be built with
IOREQ_SERVERoption enabled (as well as the IOREQ feature), which is selected for x86's config HVM for now.Also update XSM code a bit to let DM op be used on Arm.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- update XSM, related changes were pulled from:
[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM featuresChanges V1 -> V2:
- update the author of a patch
- update patch description
- introducexen/dm.hand move definitions hereQuoted 4 lines
Quoted 2 lines
Changes V2 -> V3:
- no changesAnd my concern regarding the common vs arch nesting also hasn't changed.
Quoted 7 lines
I am sorry, I might misread your comment, but I failed to see any obvious to me request(s) for changes.
I have just re-read previous discussion...
So the question about considering doing it the other way around (top level dm-op handling arch-specific
and call into e.g.ioreq_server_dm_op()for otherwise unhandled ops) is exactly a concern which I should have addressed?Quoted 11 lines
Well, on v2 you replied you didn't consider the alternative. I would have expected that you would at least go through this consideration process, and see whether there are better reasons to stick with the apparently backwards arrangement than to change to the more conventional one. If there are such reasons, I would expect them to be called out in reply and perhaps also in the commit message; the latter because down the road more people may wonder why the more narrow / special set of cases gets handled at a higher layer than the wider set of remaining ones, and they would then be able to find an explanation without having to resort to searching through list archives.
Quoted 3 lines
Ah, will investigate. Sorry for not paying enough attention to it.
Yes, IOREQ (I mean "common") ops are 7 out of 18 right now. The subsequent patch is adding one more DM op -XEN_DMOP_set_irq_level. There are several PCI related ops which might want to be common in the future (but I am not sure).
I think, I can say that I have considered the alternative (doing it the other way around), of course if I got your suggestion for V2 correctly.
Agree, the alternative is more natural, also compat_dm_op() was left in x86 code. For me the downside is in code duplication. With the alternative both arches have to duplicate do_dm_op() and "common" part of dm_op() (only "switch ( op.op )" is unique). Probably, do_dm_op() could be moved to common dm.c as well as dm_op() could become global...
Now the question is which approach to take ("current" or "alternative") for me to prepare for V4. Personally, I would be OK with the both (with a little preference for "alternative"). Also, If we decide to go with the alternative, should the common files still be named dm.*?
xen/arch/x86/hvm/dm.c +0 -0
@@ -16,6 +16,7 @@
#include <xen/event.h>
#include <xen/guest_access.h>
+#include <xen/dm.h>
#include <xen/hypercall.h>
#include <xen/ioreq.h>
#include <xen/nospec.h>
@@ -29,13 +30,6 @@
#include <public/hvm/hvm_op.h>
-struct dmop_args {
- domid_t domid;
- unsigned int nr_bufs;
- /* Reserve enough buf elements for all current hypercalls. */ - struct xen_dm_op_buf buf[2];
-};
-
static bool _raw_copy_from_guest_buf_offset(void *dst,
const struct dmop_args *args, unsigned int buf_idx, @@ -408,71 +402,6 @@ static int dm_op(const struct dmop_args *op_args)
switch ( op.op )
{
- case XEN_DMOP_create_ioreq_server:
- {
- struct xen_dm_op_create_ioreq_server *data =
- &op.u.create_ioreq_server;
-
- const_op = false;
-
- rc = -EINVAL;
- if ( data->pad[0] || data->pad[1] || data->pad[2] )
- break;
-
- rc = hvm_create_ioreq_server(d, data->handle_bufioreq, - &data->id);
- break;
- }
-
- case XEN_DMOP_get_ioreq_server_info:
- {
- struct xen_dm_op_get_ioreq_server_info *data =
- &op.u.get_ioreq_server_info;
- const uint16_t valid_flags = XEN_DMOP_no_gfns;
-
- const_op = false;
-
- rc = -EINVAL;
- if ( data->flags & ~valid_flags )
- break;
-
- rc = hvm_get_ioreq_server_info(d, data->id,
- (data->flags & XEN_DMOP_no_gfns) ? - NULL : &data->ioreq_gfn, - (data->flags & XEN_DMOP_no_gfns) ? - NULL : &data->bufioreq_gfn,
- &data->bufioreq_port);
- break;
- }
-
- case XEN_DMOP_map_io_range_to_ioreq_server:
- {
- const struct xen_dm_op_ioreq_server_range *data =
- &op.u.map_io_range_to_ioreq_server;
-
- rc = -EINVAL;
- if ( data->pad )
- break;
-
- rc = hvm_map_io_range_to_ioreq_server(d, data->id, data->type, - data->start, data->end); - break;
- }
-
- case XEN_DMOP_unmap_io_range_from_ioreq_server:
- {
- const struct xen_dm_op_ioreq_server_range *data =
- &op.u.unmap_io_range_from_ioreq_server;
-
- rc = -EINVAL;
- if ( data->pad )
- break;
-
- rc = hvm_unmap_io_range_from_ioreq_server(d, data->id, data->type, - data->start, data->end); - break;
- }
-
case XEN_DMOP_map_mem_type_to_ioreq_server:
{
struct xen_dm_op_map_mem_type_to_ioreq_server *data = @@ -523,32 +452,6 @@ static int dm_op(const struct dmop_args *op_args) break;
}
- case XEN_DMOP_set_ioreq_server_state:
- {
- const struct xen_dm_op_set_ioreq_server_state *data = - &op.u.set_ioreq_server_state;
-
- rc = -EINVAL;
- if ( data->pad )
- break;
-
- rc = hvm_set_ioreq_server_state(d, data->id, !!data->enabled); - break;
- }
-
- case XEN_DMOP_destroy_ioreq_server:
- {
- const struct xen_dm_op_destroy_ioreq_server *data = - &op.u.destroy_ioreq_server;
-
- rc = -EINVAL;
- if ( data->pad )
- break;
-
- rc = hvm_destroy_ioreq_server(d, data->id);
- break;
- }
-
case XEN_DMOP_track_dirty_vram:
{
const struct xen_dm_op_track_dirty_vram *data =
@@ -703,7 +606,7 @@ static int dm_op(const struct dmop_args *op_args) }
default:
- rc = -EOPNOTSUPP;
+ rc = ioreq_server_dm_op(&op, d, &const_op); break;
}
xen/common/Makefile +0 -0
@@ -5,6 +5,7 @@ obj-$(CONFIG_CORE_PARKING) += core_parking.o
obj-y += cpu.o
obj-$(CONFIG_DEBUG_TRACE) += debugtrace.o
obj-$(CONFIG_HAS_DEVICE_TREE) += device_tree.o
+obj-$(CONFIG_IOREQ_SERVER) += dm.o
obj-y += domain.o
obj-y += event_2l.o
obj-y += event_channel.o
xen/common/dm.c new file +135 -0
135 lines not on this page · raw message
xen/include/xen/dm.h new file +41 -0
41 lines not on this page · raw message
xen/include/xsm/dummy.h +0 -0
@@ -707,14 +707,14 @@ static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG struct domain *d, unsigned int
}
}
+#endif /* CONFIG_X86 */
+
static XSM_INLINE int xsm_dm_op(XSM_DEFAULT_ARG struct domain *d) {
XSM_ASSERT_ACTION(XSM_DM_PRIV);
return xsm_default_action(action, current->domain, d); }
-#endif /* CONFIG_X86 */
-
#ifdef CONFIG_ARGO
static XSM_INLINE int xsm_argo_enable(const struct domain *d) {
xen/include/xsm/xsm.h +0 -0
@@ -176,8 +176,8 @@ struct xsm_operations {
int (*ioport_permission) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow); int (*ioport_mapping) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow); int (*pmu_op) (struct domain *d, unsigned int op);
- int (*dm_op) (struct domain *d);
#endif
+ int (*dm_op) (struct domain *d);
int (*xen_version) (uint32_t cmd);
int (*domain_resource_map) (struct domain *d);
#ifdef CONFIG_ARGO
@@ -682,13 +682,13 @@ static inline int xsm_pmu_op (xsm_default_t def, struct domain *d, unsigned int return xsm_ops->pmu_op(d, op);
}
+#endif /* CONFIG_X86 */
+
static inline int xsm_dm_op(xsm_default_t def, struct domain *d) {
return xsm_ops->dm_op(d);
}
-#endif /* CONFIG_X86 */
-
static inline int xsm_xen_version (xsm_default_t def, uint32_t op) {
return xsm_ops->xen_version(op);
xen/xsm/dummy.c +0 -0
@@ -147,8 +147,8 @@ void __init xsm_fixup_ops (struct xsm_operations *ops)
set_to_dummy_if_null(ops, ioport_permission); set_to_dummy_if_null(ops, ioport_mapping); set_to_dummy_if_null(ops, pmu_op);
- set_to_dummy_if_null(ops, dm_op); #endif
+ set_to_dummy_if_null(ops, dm_op); set_to_dummy_if_null(ops, xen_version); set_to_dummy_if_null(ops, domain_resource_map); #ifdef CONFIG_ARGO
xen/xsm/flask/hooks.c +0 -0
@@ -1656,14 +1656,13 @@ static int flask_pmu_op (struct domain *d, unsigned int op)
return -EPERM;
}
}
+#endif /* CONFIG_X86 */
static int flask_dm_op(struct domain *d)
{
return current_has_perm(d, SECCLASS_HVM, HVM__DM); }
-#endif /* CONFIG_X86 */
-
static int flask_xen_version (uint32_t op)
{
u32 dsid = domain_sid(current->domain);
@@ -1865,8 +1864,8 @@ static struct xsm_operations flask_ops = { .ioport_permission = flask_ioport_permission, .ioport_mapping = flask_ioport_mapping,
.pmu_op = flask_pmu_op,
- .dm_op = flask_dm_op,
#endif
+ .dm_op = flask_dm_op,
.xen_version = flask_xen_version, .domain_resource_map = flask_domain_resource_map, #ifdef CONFIG_ARGO
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
Quoted 16 lines · Oleksandr
Quoted 11 lines
Quoted 11 lines
Well, on v2 you replied you didn't consider the alternative. I would have expected that you would at least go through this consideration process, and see whether there are better reasons to stick with the apparently backwards arrangement than to change to the more conventional one. If there are such reasons, I would expect them to be called out in reply and perhaps also in the commit message; the latter because down the road more people may wonder why the more narrow / special set of cases gets handled at a higher layer than the wider set of remaining ones, and they would then be able to find an explanation without having to resort to searching through list archives.
Quoted 5 lines
Ah, will investigate. Sorry for not paying enough attention to it. Yes, IOREQ (I mean "common") ops are 7 out of 18 right now. The subsequent patch is adding one more DM op -
XEN_DMOP_set_irq_level. There are several PCI related ops which might want to be common in the future (but I am not sure).
Quoted 7 lines
I think, I can say that I have considered the alternative (doing it the other way around), of course if I got your suggestion for V2 correctly. Agree, the alternative is more natural, also
compat_dm_op()was left in x86 code. For me the downside is in code duplication. With the alternative both arches have to duplicatedo_dm_op()and "common" part ofdm_op()
(only "switch ( op.op )" is unique).
Yes, this duplication is the main downside.
Quoted 3 lines
Now the question is which approach to take ("current" or "alternative") for me to prepare for V4. Personally, I would be OK with the both (with a little preference for "alternative").
Same here. I don't think I've seen anyone else voice an opinion.
Quoted 2 lines
Also, If we decide to go with the alternative, should the common files still be named dm.*?
I think this should live in ioreq.c, just like e.g. iommu_do_pci_domctl() lives in passthrough/pci.c. This would then allow quite a few things to become static in that file, I believe.
Jan
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
On 07.01.21 17:01, Jan Beulich wrote:
Hi Jan, all
Quoted 24 lines · Oleksandr
Quoted 16 lines
Quoted 11 lines
Quoted 11 lines
Well, on v2 you replied you didn't consider the alternative. I would have expected that you would at least go through this consideration process, and see whether there are better reasons to stick with the apparently backwards arrangement than to change to the more conventional one. If there are such reasons, I would expect them to be called out in reply and perhaps also in the commit message; the latter because down the road more people may wonder why the more narrow / special set of cases gets handled at a higher layer than the wider set of remaining ones, and they would then be able to find an explanation without having to resort to searching through list archives.
Quoted 5 lines
Ah, will investigate. Sorry for not paying enough attention to it. Yes, IOREQ (I mean "common") ops are 7 out of 18 right now. The subsequent patch is adding one more DM op -
XEN_DMOP_set_irq_level. There are several PCI related ops which might want to be common in the future (but I am not sure).Quoted 7 lines
I think, I can say that I have considered the alternative (doing it the other way around), of course if I got your suggestion for V2 correctly. Agree, the alternative is more natural, also
compat_dm_op()was left in x86 code. For me the downside is in code duplication. With the alternative both arches have to duplicatedo_dm_op()and "common" part ofdm_op()
(only "switch ( op.op )" is unique).
Quoted 6 lines
Yes, this duplication is the main downside.
Quoted 3 lines
Now the question is which approach to take ("current" or "alternative") for me to prepare for V4. Personally, I would be OK with the both (with a little preference for "alternative").
Same here. I don't think I've seen anyone else voice an opinion.
Well, let's wait a bit for other opinions... @Julien, @Paul what do you think on that?
Quoted 6 lines
Quoted 3 lines
Also, If we decide to go with the alternative, should the common files still be named dm.*?
I think this should live in
ioreq.c, just like e.g.iommu_do_pci_domctl()lives inpassthrough/pci.c. This would then allow quite a few things to become static in that file, I believe.
I got it. It seems yes, at least the following could became static:
ioreq_server_createioreq_server_get_infoioreq_server_map_io_rangeioreq_server_unmap_io_rangeioreq_server_set_stateioreq_server_destroy
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 09/23] xen/dm: Make x86's DM feature common
Hi Jan.
Quoted 46 lines · Oleksandr
On 07.01.21 17:01, Jan Beulich wrote:
Hi Jan, all
Quoted 24 lines · Oleksandr
Quoted 16 lines
Quoted 11 lines
Quoted 11 lines
Well, on v2 you replied you didn't consider the alternative. I would have expected that you would at least go through this consideration process, and see whether there are better reasons to stick with the apparently backwards arrangement than to change to the more conventional one. If there are such reasons, I would expect them to be called out in reply and perhaps also in the commit message; the latter because down the road more people may wonder why the more narrow / special set of cases gets handled at a higher layer than the wider set of remaining ones, and they would then be able to find an explanation without having to resort to searching through list archives.
Quoted 5 lines
Ah, will investigate. Sorry for not paying enough attention to it. Yes, IOREQ (I mean "common") ops are 7 out of 18 right now. The subsequent patch is adding one more DM op -
XEN_DMOP_set_irq_level. There are several PCI related ops which might want to be common in the future (but I am not sure).Quoted 7 lines
I think, I can say that I have considered the alternative (doing it the other way around), of course if I got your suggestion for V2 correctly. Agree, the alternative is more natural, also
compat_dm_op()was left in x86 code. For me the downside is in code duplication. With the alternative both arches have to duplicatedo_dm_op()and "common" part ofdm_op()
(only "switch ( op.op )" is unique).Quoted 6 lines
Yes, this duplication is the main downside.
Quoted 3 lines
Now the question is which approach to take ("current" or "alternative") for me to prepare for V4. Personally, I would be OK with the both (with a little preference for "alternative").
Same here. I don't think I've seen anyone else voice an opinion.
Well, let's wait a bit for other opinions... @Julien, @Paul what do you think on that?
Quoted 6 lines
Quoted 3 lines
Also, If we decide to go with the alternative, should the common files still be named dm.*?
I think this should live in
ioreq.c, just like e.g.iommu_do_pci_domctl()lives inpassthrough/pci.c. This would then allow quite a few things to become static in that file, I believe.
Quoted 8 lines
I got it. It seems yes, at least the following could became static:
ioreq_server_createioreq_server_get_infoioreq_server_map_io_rangeioreq_server_unmap_io_rangeioreq_server_set_stateioreq_server_destroy
Well, I have already pushed V4 with this "alternative" approach, let's discuss there [1].
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#mb08f75657f43df869596c5c9c30e2395a9e35c7a
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 10/23 xen/mm: Make x86's XENMEM_resource_ioreq_server handling common
In reply to Oleksandr TyshchenkoFrom: Julien Grall <[email protected]>
As x86 implementation of XENMEM_resource_ioreq_server can be re-used on Arm later on, this patch makes it common and removes arch_acquire_resource as unneeded.
Also re-order #include-s alphabetically.
This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- no changes
Changes V1 -> V2:
- update the author of a patch
Changes V2 -> V3:
- don't wrap #include <xen/ioreq.h>
- limit the number of #ifdef-s
- re-order #include-s alphabetically
---
--- xen/arch/x86/mm.c | 44 --------------------------------- xen/common/memory.c | 63 +++++++++++++++++++++++++++++++++++++++--------- xen/include/asm-arm/mm.h | 8 ------ xen/include/asm-x86/mm.h | 4 --- 4 files changed, 51 insertions(+), 68 deletions(-)
xen/arch/x86/mm.c +0 -44
@@ -4699,50 +4699,6 @@ int xenmem_add_to_physmap_one(return rc; } -int arch_acquire_resource (struct domain *d,unsigned int type, -unsigned int id,unsigned long frame, -unsigned int nr_frames,xen_pfn_t mfn_list[]) -{ -int rc; - -switch ( type ) - { -#ifdef CONFIG_HVM -case XENMEM_resource_ioreq_server: - { -ioservid_t ioservid = id; -unsigned int i; - - rc = -EINVAL ; -if ( !is_hvm_domain (d) ) -break ; - -if ( id != (unsigned int )ioservid ) -break ; - - rc =0 ; -for ( i =0 ; i < nr_frames; i++ ) - { -mfn_t mfn; - - rc =hvm_get_ioreq_server_frame (d, id, frame + i, &mfn); -if ( rc ) -break ; - - mfn_list[i] =mfn_x (mfn); - } -break ; - } -#endif - -default : - rc = -EOPNOTSUPP ; -break ; - } - -return rc; -} -long arch_memory_op (unsigned long cmd,XEN_GUEST_HANDLE_PARAM (void ) arg) {int rc;
xen/common/memory.c +51 -12
88 lines not on this page · raw message
xen/include/asm-arm/mm.h +0 -8
@@ -358,14 +358,6 @@ static inline void put_page_and_type(struct page_info *page)void clear_and_clean_page (struct page_info *page); -static inline -int arch_acquire_resource (struct domain *d,unsigned int type,unsigned int id, -unsigned long frame,unsigned int nr_frames, -xen_pfn_t mfn_list[]) -{ -return -EOPNOTSUPP ; -} -unsigned int arch_get_dma_bitsize (void );#endif /* __ARCH_ARM_MM__ */
xen/include/asm-x86/mm.h +0 -4
@@ -639,8 +639,4 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn) return mfn <= (virt_to_mfn(eva - 1) + 1); } -int arch_acquire_resource (struct domain *d,unsigned int type, -unsigned int id,unsigned long frame, -unsigned int nr_frames,xen_pfn_t mfn_list[]); -#endif /* __ASM_X86_MM_H__ */
Signature · 1 line
2.7.4
Re: [PATCH V3 10/23] xen/mm: Make x86's XENMEM_resource_ioreq_server handling common
Quoted 39 lines · Oleksandr Tyshchenko
xen/arch/x86/mm.c +0
-33@@ -4699,50 +4699,6 @@ int xenmem_add_to_physmap_one(return rc; } -int arch_acquire_resource (struct domain *d,unsigned int type, -unsigned int id,unsigned long frame, -unsigned int nr_frames,xen_pfn_t mfn_list[]) -{ -int rc; - -switch ( type ) - { -#ifdef CONFIG_HVM -case XENMEM_resource_ioreq_server: - { -ioservid_t ioservid = id; -unsigned int i; - - rc = -EINVAL ; -if ( !is_hvm_domain (d) ) -break ; - -if ( id != (unsigned int )ioservid ) -break ; - - rc =0 ; -for ( i =0 ; i < nr_frames; i++ ) - { -mfn_t mfn; - - rc =hvm_get_ioreq_server_frame (d, id, frame + i, &mfn); -if ( rc ) -break ; - - mfn_list[i] =mfn_x (mfn); - } -break ;
xen/arch/x86/mm.c int xenmem_add_to_physmap_one( · line 4744
Quoted 10 lines
xen/arch/x86/mm.c +0
-10- } -#endif - -default : - rc = -EOPNOTSUPP ; -break ; - } - -return rc; -}
Can't this be accompanied by removal of the xen/ioreq.h inclusion? (I'm only looking at patch 4 right now, but the renaming there made the soon to be unnecessary #include quite apparent.)
Jan
Jan Beulich
Reviewed-byRe: [PATCH V3 10/23] xen/mm: Make x86's XENMEM_resource_ioreq_server handling common
Quoted 46 lines · Jan Beulich
Quoted 45 lines · Oleksandr Tyshchenko
xen/arch/x86/mm.c +0
-39@@ -4699,50 +4699,6 @@ int xenmem_add_to_physmap_one(return rc; } -int arch_acquire_resource (struct domain *d,unsigned int type, -unsigned int id,unsigned long frame, -unsigned int nr_frames,xen_pfn_t mfn_list[]) -{ -int rc; - -switch ( type ) - { -#ifdef CONFIG_HVM -case XENMEM_resource_ioreq_server: - { -ioservid_t ioservid = id; -unsigned int i; - - rc = -EINVAL ; -if ( !is_hvm_domain (d) ) -break ; - -if ( id != (unsigned int )ioservid ) -break ; - - rc =0 ; -for ( i =0 ; i < nr_frames; i++ ) - { -mfn_t mfn; - - rc =hvm_get_ioreq_server_frame (d, id, frame + i, &mfn); -if ( rc ) -break ; - - mfn_list[i] =mfn_x (mfn); - } -break ; - } -#endif - -default : - rc = -EOPNOTSUPP ; -break ;
Quoted 8 lines
xen/arch/x86/mm.c int xenmem_add_to_physmap_one( · line 4744
Quoted 4 lines
xen/arch/x86/mm.c +0
-4- } - -return rc; -}Can't this be accompanied by removal of the
xen/ioreq.hinclusion? (I'm only looking at patch 4 right now, but the renaming there made the soon to be unnecessary #include quite apparent.)
And then, now that I've looked at this patch as a whole,
Jan
Re: [PATCH V3 10/23] xen/mm: Make x86's XENMEM_resource_ioreq_server handling common
On 07.12.20 14:11, Jan Beulich wrote:
Hi Jan
Quoted 54 lines · Jan Beulich
Quoted 45 lines · Oleksandr Tyshchenko
Quoted 44 lines
xen/arch/x86/mm.c +0
-38@@ -4699,50 +4699,6 @@ int xenmem_add_to_physmap_one(return rc; } -int arch_acquire_resource (struct domain *d,unsigned int type, -unsigned int id,unsigned long frame, -unsigned int nr_frames,xen_pfn_t mfn_list[]) -{ -int rc; - -switch ( type ) - { -#ifdef CONFIG_HVM -case XENMEM_resource_ioreq_server: - { -ioservid_t ioservid = id; -unsigned int i; - - rc = -EINVAL ; -if ( !is_hvm_domain (d) ) -break ; - -if ( id != (unsigned int )ioservid ) -break ; - - rc =0 ; -for ( i =0 ; i < nr_frames; i++ ) - { -mfn_t mfn; - - rc =hvm_get_ioreq_server_frame (d, id, frame + i, &mfn); -if ( rc ) -break ; - - mfn_list[i] =mfn_x (mfn); - } -break ; - } -#endif - -default : - rc = -EOPNOTSUPP ;Quoted 8 lines
xen/arch/x86/mm.c int xenmem_add_to_physmap_one( · line 4744
Quoted 5 lines
xen/arch/x86/mm.c +0
-5-break ; - } - -return rc; -}Can't this be accompanied by removal of the
xen/ioreq.hinclusion? (I'm only looking at patch 4 right now, but the renaming there made the soon to be unnecessary #include quite apparent.)
Quoted 2 lines
And then, now that I've looked at this patch as a whole,
Reviewed-by Jan Beulich <[email protected]>
Great, thank you.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 11/23 xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The IOREQ is a common feature now and these fields will be used on Arm as is. Move them to common struct vcpu as a part of new struct vcpu_io and drop duplicating "io" prefixes. Also move enum hvm_io_completion to xen/sched.h and remove "hvm" prefixes.
This patch completely removes layering violation in the common code.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V1 -> V2:
- new patch
Changes V2 -> V3:
- update patch according the "legacy interface" is x86 specific
- update patch description
- drop the "io" prefixes from the field names
- wrap IO_realmode_completion
---
--- xen/arch/x86/hvm/emulate.c | 72 +++++++++++++++++++-------------------- xen/arch/x86/hvm/hvm.c | 2 +- xen/arch/x86/hvm/io.c | 8 ++--- xen/arch/x86/hvm/ioreq.c | 4 +-- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/vmx/realmode.c | 6 ++-- xen/common/ioreq.c | 22 ++++++------ xen/include/asm-x86/hvm/emulate.h | 2 +- xen/include/asm-x86/hvm/ioreq.h | 2 +- xen/include/asm-x86/hvm/vcpu.h | 11 ------ xen/include/xen/sched.h | 19 +++++++++++ 11 files changed, 79 insertions(+), 71 deletions(-)
xen/arch/x86/hvm/emulate.c +36 -36
@@ -142,8 +142,8 @@ void hvmemul_cancel(struct vcpu *v) {struct hvm_vcpu_io *vio = &v->arch.hvm.hvm_io; - vio->io_req.state =STATE_IOREQ_NONE ; - vio->io_completion = HVMIO_no_completion; + v->io.req.state =STATE_IOREQ_NONE ; + v->io.completion = IO_no_completion; vio->mmio_cache_count =0 ; vio->mmio_insn_bytes =0 ; vio->mmio_access = (struct npfec ){};
@@ -159,7 +159,7 @@ static int hvmemul_do_io( {struct vcpu *curr = current;struct domain *currd = curr->domain; -struct hvm_vcpu_io *vio = &curr->arch.hvm.hvm_io; +struct vcpu_io *vio = &curr->io;ioreq_t p = { .type = is_mmio ?IOREQ_TYPE_COPY :IOREQ_TYPE_PIO , .addr = addr,
@@ -184,13 +184,13 @@ static int hvmemul_do_io(return X86EMUL_UNHANDLEABLE ; } -switch ( vio->io_req.state ) +switch ( vio->req.state ) {case STATE_IOREQ_NONE :break ;case STATE_IORESP_READY : - vio->io_req.state =STATE_IOREQ_NONE ; - p = vio->io_req; + vio->req.state =STATE_IOREQ_NONE ; + p = vio->req;/* Verify the emulation request has been correctly re-issued */ if ( (p.type != (is_mmio ? IOREQ_TYPE_COPY : IOREQ_TYPE_PIO)) ||
@@ -238,7 +238,7 @@ static int hvmemul_do_io( }ASSERT (p.count); - vio->io_req = p; + vio->req = p; rc =hvm_io_intercept (&p);
@@ -247,12 +247,12 @@ static int hvmemul_do_io(* our callers and mirror this into latched state. */ ASSERT (p.count <= *reps); - *reps = vio->io_req.count = p.count; + *reps = vio->req.count = p.count;switch ( rc ) {case X86EMUL_OKAY : - vio->io_req.state =STATE_IOREQ_NONE ; + vio->req.state =STATE_IOREQ_NONE ;break ;case X86EMUL_UNHANDLEABLE : {
@@ -305,7 +305,7 @@ static int hvmemul_do_io(if ( s ==NULL ) { rc =X86EMUL_RETRY ; - vio->io_req.state =STATE_IOREQ_NONE ; + vio->req.state =STATE_IOREQ_NONE ;break ; }
@@ -316,7 +316,7 @@ static int hvmemul_do_io(if ( dir ==IOREQ_READ ) { rc =hvm_process_io_intercept (&ioreq_server_handler, &p); - vio->io_req.state =STATE_IOREQ_NONE ; + vio->req.state =STATE_IOREQ_NONE ;break ; } }
@@ -329,14 +329,14 @@ static int hvmemul_do_io(if ( !s ) { rc =hvm_process_io_intercept (&null_handler, &p); - vio->io_req.state =STATE_IOREQ_NONE ; + vio->req.state =STATE_IOREQ_NONE ; }else { rc =hvm_send_ioreq (s, &p,0 );if ( rc !=X86EMUL_RETRY || currd->is_shutting_down ) - vio->io_req.state =STATE_IOREQ_NONE ; -else if ( !ioreq_needs_completion (&vio->io_req) ) + vio->req.state =STATE_IOREQ_NONE ; +else if ( !ioreq_needs_completion (&vio->req) ) rc =X86EMUL_OKAY ; }break ;
@@ -1854,7 +1854,7 @@ static int hvmemul_rep_movs(* cheaper than multiple round trips through the device model. Yet * when processing a response we can always re-use the translation. */ - (vio->io_req.state ==STATE_IORESP_READY || + (curr->io.req.state ==STATE_IORESP_READY || ((!df || *reps ==1 ) && PAGE_SIZE - (saddr & ~PAGE_MASK) >= *reps * bytes_per_rep)) ) sgpa = pfn_to_paddr(vio->mmio_gpfn) | (saddr & ~PAGE_MASK);
@@ -1870,7 +1870,7 @@ static int hvmemul_rep_movs(if ( vio->mmio_access.write_access && (vio->mmio_gla == (daddr &PAGE_MASK )) &&/* See comment above. */ - (vio->io_req.state ==STATE_IORESP_READY || + (curr->io.req.state ==STATE_IORESP_READY || ((!df || *reps ==1 ) &&PAGE_SIZE - (daddr & ~PAGE_MASK ) >= *reps * bytes_per_rep)) ) dgpa =pfn_to_paddr (vio->mmio_gpfn) | (daddr & ~PAGE_MASK );
@@ -2007,7 +2007,7 @@ static int hvmemul_rep_stos(if ( vio->mmio_access.write_access && (vio->mmio_gla == (addr &PAGE_MASK )) &&/* See respective comment in MOVS processing. */ - (vio->io_req.state ==STATE_IORESP_READY || + (curr->io.req.state ==STATE_IORESP_READY || ((!df || *reps ==1 ) &&PAGE_SIZE - (addr & ~PAGE_MASK ) >= *reps * bytes_per_rep)) ) gpa =pfn_to_paddr (vio->mmio_gpfn) | (addr & ~PAGE_MASK );
@@ -2613,13 +2613,13 @@ static const struct x86_emulate_ops hvm_emulate_ops_no_write = { };/* -* Note that passing HVMIO_no_completion into this function serves as kind +* Note that passing IO_no_completion into this function serves as kind * of (but not fully) an "auto select completion" indicator. When there's * no completion needed, the passed in value will be ignored in any case. */ static int _hvm_emulate_one (struct hvm_emulate_ctxt *hvmemul_ctxt,const struct x86_emulate_ops *ops, -enum hvm_io_completion completion) +enum io_completion completion) {const struct cpu_user_regs *regs = hvmemul_ctxt->ctxt.regs;struct vcpu *curr = current;
@@ -2634,11 +2634,11 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,*/ if ( vio->cache->num_ents > vio->cache->max_ents ) { -ASSERT (vio->io_req.state ==STATE_IOREQ_NONE ); +ASSERT (curr->io.req.state ==STATE_IOREQ_NONE ); vio->cache->num_ents =0 ; }else -ASSERT (vio->io_req.state ==STATE_IORESP_READY ); +ASSERT (curr->io.req.state ==STATE_IORESP_READY );hvm_emulate_init_per_insn (hvmemul_ctxt, vio->mmio_insn, vio->mmio_insn_bytes);
@@ -2649,25 +2649,25 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,if ( rc ==X86EMUL_OKAY && vio->mmio_retry ) rc =X86EMUL_RETRY ; -if ( !ioreq_needs_completion (&vio->io_req) ) - completion = HVMIO_no_completion; -else if ( completion == HVMIO_no_completion ) - completion = (vio->io_req.type !=IOREQ_TYPE_PIO || - hvmemul_ctxt->is_mem_access) ? HVMIO_mmio_completion - : HVMIO_pio_completion; +if ( !ioreq_needs_completion (&curr->io.req) ) + completion = IO_no_completion; +else if ( completion == IO_no_completion ) + completion = (curr->io.req.type !=IOREQ_TYPE_PIO || + hvmemul_ctxt->is_mem_access) ? IO_mmio_completion + : IO_pio_completion; -switch ( vio->io_completion = completion ) +switch ( curr->io.completion = completion ) { -case HVMIO_no_completion: -case HVMIO_pio_completion: +case IO_no_completion: +case IO_pio_completion: vio->mmio_cache_count =0 ; vio->mmio_insn_bytes =0 ; vio->mmio_access = (struct npfec ){};hvmemul_cache_disable (curr);break ; -case HVMIO_mmio_completion: -case HVMIO_realmode_completion: +case IO_mmio_completion: +case IO_realmode_completion:BUILD_BUG_ON (sizeof (vio->mmio_insn) <sizeof (hvmemul_ctxt->insn_buf)); vio->mmio_insn_bytes = hvmemul_ctxt->insn_buf_bytes;memcpy (vio->mmio_insn, hvmemul_ctxt->insn_buf, vio->mmio_insn_bytes);
@@ -2716,7 +2716,7 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,int hvm_emulate_one (struct hvm_emulate_ctxt *hvmemul_ctxt, -enum hvm_io_completion completion) +enum io_completion completion) {return _hvm_emulate_one (hvmemul_ctxt, &hvm_emulate_ops, completion); }
@@ -2754,7 +2754,7 @@ int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla) guest_cpu_user_regs()); ctxt.ctxt.data = &mmio_ro_ctxt; -switch ( rc =_hvm_emulate_one (&ctxt, ops, HVMIO_no_completion) ) +switch ( rc =_hvm_emulate_one (&ctxt, ops, IO_no_completion) ) {case X86EMUL_UNHANDLEABLE :case X86EMUL_UNIMPLEMENTED :
@@ -2782,7 +2782,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr, {case EMUL_KIND_NOWRITE : rc =_hvm_emulate_one (&ctx, &hvm_emulate_ops_no_write, - HVMIO_no_completion); + IO_no_completion);break ;case EMUL_KIND_SET_CONTEXT_INSN : {struct vcpu *curr = current;
@@ -2803,7 +2803,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,/* Fall-through */ default : ctx.set_context = (kind ==EMUL_KIND_SET_CONTEXT_DATA ); - rc =hvm_emulate_one (&ctx, HVMIO_no_completion); + rc =hvm_emulate_one (&ctx, IO_no_completion); } switch ( rc )
xen/arch/x86/hvm/hvm.c +1 -1
@@ -3800,7 +3800,7 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)return ; } -switch (hvm_emulate_one (&ctxt, HVMIO_no_completion) ) +switch (hvm_emulate_one (&ctxt, IO_no_completion) ) {case X86EMUL_UNHANDLEABLE :case X86EMUL_UNIMPLEMENTED :
xen/arch/x86/hvm/io.c +4 -4
@@ -85,7 +85,7 @@ bool hvm_emulate_one_insn(hvm_emulate_validate_t *validate, const char *descr)hvm_emulate_init_once (&ctxt, validate,guest_cpu_user_regs ()); -switch ( rc =hvm_emulate_one (&ctxt, HVMIO_no_completion) ) +switch ( rc =hvm_emulate_one (&ctxt, IO_no_completion) ) {case X86EMUL_UNHANDLEABLE :hvm_dump_emulation_state (XENLOG_G_WARNING , descr, &ctxt, rc);
@@ -122,7 +122,7 @@ bool handle_mmio_with_translation(unsigned long gla, unsigned long gpfn,bool handle_pio (uint16_t port,unsigned int size,int dir) {struct vcpu *curr = current; -struct hvm_vcpu_io *vio = &curr->arch.hvm.hvm_io; +struct vcpu_io *vio = &curr->io;unsigned int data;int rc;
@@ -135,8 +135,8 @@ bool handle_pio(uint16_t port, unsigned int size, int dir) rc =hvmemul_do_pio_buffer (port, size, dir, &data); -if (ioreq_needs_completion (&vio->io_req) ) - vio->io_completion = HVMIO_pio_completion; +if (ioreq_needs_completion (&vio->req) ) + vio->completion = IO_pio_completion;switch ( rc ) {
xen/arch/x86/hvm/ioreq.c +2 -2
@@ -41,11 +41,11 @@ bool ioreq_complete_mmio(void)return handle_mmio (); } -bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion) +bool arch_vcpu_ioreq_completion (enum io_completion io_completion) {switch ( io_completion ) { -case HVMIO_realmode_completion: +case IO_realmode_completion: {struct hvm_emulate_ctxt ctxt;
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -1266,7 +1266,7 @@ enum hvm_intblk nsvm_intr_blocked(struct vcpu *v)* Delay the injection because this would result in delivering * an interrupt *within* the execution of an instruction. */ -if ( v->arch.hvm.hvm_io.io_req.state !=STATE_IOREQ_NONE ) +if ( v->io.req.state !=STATE_IOREQ_NONE )return hvm_intblk_shadow;if ( !nv->nv_vmexit_pending && n2vmcb->exit_int_info.v )
xen/arch/x86/hvm/vmx/realmode.c +3 -3
@@ -101,7 +101,7 @@ void vmx_realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)perfc_incr (realmode_emulations); - rc =hvm_emulate_one (hvmemul_ctxt, HVMIO_realmode_completion); + rc =hvm_emulate_one (hvmemul_ctxt, IO_realmode_completion);if ( rc ==X86EMUL_UNHANDLEABLE ) {
@@ -188,7 +188,7 @@ void vmx_realmode(struct cpu_user_regs *regs)vmx_realmode_emulate_one (&hvmemul_ctxt); -if ( vio->io_req.state !=STATE_IOREQ_NONE || vio->mmio_retry ) +if ( curr->io.req.state !=STATE_IOREQ_NONE || vio->mmio_retry )break ;/* Stop emulating unless our segment state is not safe */
@@ -202,7 +202,7 @@ void vmx_realmode(struct cpu_user_regs *regs) }/* Need to emulate next time if we've started an IO operation */ -if ( vio->io_req.state !=STATE_IOREQ_NONE ) +if ( curr->io.req.state !=STATE_IOREQ_NONE ) curr->arch.hvm.vmx.vmx_emulate =1 ;if ( !curr->arch.hvm.vmx.vmx_emulate && !curr->arch.hvm.vmx.vmx_realmode )
xen/common/ioreq.c +11 -11
@@ -159,7 +159,7 @@ static bool hvm_wait_for_io(struct ioreq_vcpu *sv, ioreq_t *p)break ; } - p = &sv->vcpu->arch.hvm.hvm_io.io_req; + p = &sv->vcpu->io.req;if (ioreq_needs_completion (p) ) p->data = data;
@@ -171,10 +171,10 @@ static bool hvm_wait_for_io(struct ioreq_vcpu *sv, ioreq_t *p)bool handle_hvm_io_completion (struct vcpu *v) {struct domain *d = v->domain; -struct hvm_vcpu_io *vio = &v->arch.hvm.hvm_io; +struct vcpu_io *vio = &v->io;struct ioreq_server *s;struct ioreq_vcpu *sv; -enum hvm_io_completion io_completion; +enum io_completion io_completion;if (has_vpci (d) &&vpci_process_pending (v) ) {
@@ -186,26 +186,26 @@ bool handle_hvm_io_completion(struct vcpu *v)if ( sv && !hvm_wait_for_io (sv,get_ioreq (s, v)) )return false; - vio->io_req.state =ioreq_needs_completion (&vio->io_req) ? + vio->req.state =ioreq_needs_completion (&vio->req) ?STATE_IORESP_READY :STATE_IOREQ_NONE ;msix_write_completion (v);vcpu_end_shutdown_deferral (v); - io_completion = vio->io_completion; - vio->io_completion = HVMIO_no_completion; + io_completion = vio->completion; + vio->completion = IO_no_completion;switch ( io_completion ) { -case HVMIO_no_completion: +case IO_no_completion:break ; -case HVMIO_mmio_completion: +case IO_mmio_completion:return ioreq_complete_mmio (); -case HVMIO_pio_completion: -return handle_pio (vio->io_req.addr, vio->io_req.size, - vio->io_req.dir); +case IO_pio_completion: +return handle_pio (vio->req.addr, vio->req.size, + vio->req.dir);default :return arch_vcpu_ioreq_completion (io_completion);
xen/include/asm-x86/hvm/emulate.h +1 -1
@@ -65,7 +65,7 @@ bool __nonnull(1, 2) hvm_emulate_one_insn( const char *descr);int hvm_emulate_one (struct hvm_emulate_ctxt *hvmemul_ctxt, -enum hvm_io_completion completion); +enum io_completion completion);void hvm_emulate_one_vm_event (enum emul_kind kind,unsigned int trapnr,unsigned int errcode);
xen/include/asm-x86/hvm/ioreq.h +1 -1
@@ -21,7 +21,7 @@#include <xen/ioreq.h> -bool arch_vcpu_ioreq_completion (enum hvm_io_completion io_completion); +bool arch_vcpu_ioreq_completion (enum io_completion io_completion);int arch_ioreq_server_map_pages (struct ioreq_server *s);void arch_ioreq_server_unmap_pages (struct ioreq_server *s);void arch_ioreq_server_enable (struct ioreq_server *s);
xen/include/asm-x86/hvm/vcpu.h +0 -11
@@ -28,13 +28,6 @@#include <asm/mtrr.h> #include <public/hvm/ioreq.h> -enum hvm_io_completion { - HVMIO_no_completion, - HVMIO_mmio_completion, - HVMIO_pio_completion, - HVMIO_realmode_completion -}; -struct hvm_vcpu_asid {uint64_t generation;uint32_t asid;
@@ -52,10 +45,6 @@ struct hvm_mmio_cache { };struct hvm_vcpu_io { -/* I/O request in flight to device model. */ -enum hvm_io_completion io_completion; -ioreq_t io_req; -/* * HVM emulation: * Linear address @mmio_gla maps to MMIO physical frame @mmio_gpfn.
xen/include/xen/sched.h +19 -0
@@ -145,6 +145,21 @@ void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */struct waitqueue_vcpu ; +enum io_completion { + IO_no_completion, + IO_mmio_completion, + IO_pio_completion, +#ifdef CONFIG_X86 + IO_realmode_completion, +#endif +}; + +struct vcpu_io { +/* I/O request in flight to device model. */ +enum io_completion completion; +ioreq_t req; +}; +struct vcpu {int vcpu_id;
@@ -256,6 +271,10 @@ struct vcpustruct vpci_vcpu vpci;struct arch_vcpu arch; + +#ifdef CONFIG_IOREQ_SERVER +struct vcpu_io io; +#endif };struct sched_unit {
Signature · 1 line
2.7.4
Re: [PATCH V3 11/23] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu
Quoted 14 lines · Oleksandr Tyshchenko
xen/arch/x86/hvm/emulate.c +2
-2@@ -142,8 +142,8 @@ void hvmemul_cancel(struct vcpu *v) {struct hvm_vcpu_io *vio = &v->arch.hvm.hvm_io; - vio->io_req.state =STATE_IOREQ_NONE ; - vio->io_completion = HVMIO_no_completion; + v->io.req.state =STATE_IOREQ_NONE ; + v->io.completion = IO_no_completion; vio->mmio_cache_count =0 ; vio->mmio_insn_bytes =0 ; vio->mmio_access = (struct npfec ){};
xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 162
Quoted 5 lines
xen/arch/x86/hvm/emulate.c +1
-1@@ -159,7 +159,7 @@ static int hvmemul_do_io( {struct vcpu *curr = current;struct domain *currd = curr->domain; -struct hvm_vcpu_io *vio = &curr->arch.hvm.hvm_io; +struct vcpu_io *vio = &curr->io;
Taking just these two hunks: "vio" would now stand for two entirely different things. I realize the name is applicable to both, but I wonder if such naming isn't going to risk confusion. Despite being relatively familiar with the involved code, I've been repeatedly unsure what exactly "vio" covers, and needed to go back to the header. So together with the name possible adjustment mentioned further down, maybe "vcpu_io" also wants it name changed, such that the variable then also could sensibly be named (slightly) differently? struct vcpu_io_state maybe? Or alternatively rename variables of type struct hvm_vcpu_io * to hvio or hio? Otoh the savings aren't very big for just ->io, so maybe better to stick to the prior name with the prior type, and not introduce local variables at all for the new field, like you already have it in the former case?
Quoted 4 lines
xen/include/xen/sched.h +0
-0@@ -145,6 +145,21 @@ void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 155
Quoted 10 lines
xen/include/xen/sched.h +8
-0struct waitqueue_vcpu ; +enum io_completion { + IO_no_completion, + IO_mmio_completion, + IO_pio_completion, +#ifdef CONFIG_X86 + IO_realmode_completion, +#endif +};
I'm not entirely happy with io_ / IO_ here - they seem a little too generic. How about ioreq_ / IOREQ_ respectively?
Quoted 10 lines
xen/include/xen/sched.h +6
-0+struct vcpu_io { +/* I/O request in flight to device model. */ +enum io_completion completion; +ioreq_t req; +}; +struct vcpu {int vcpu_id;
xen/include/xen/sched.h struct vcpu · line 278
Quoted 8 lines
xen/include/xen/sched.h +4
-0@@ -256,6 +271,10 @@ struct vcpustruct vpci_vcpu vpci;struct arch_vcpu arch; + +#ifdef CONFIG_IOREQ_SERVER +struct vcpu_io io; +#endif };
I don't have a good solution in mind, and I'm also not meaning to necessarily request a change here, but I'd like to point out that this does away (for this part of it only, of course) with the overlaying of the PV and HVM sub-structs on x86. As long as the HVM part is the far bigger one, that's not a problem, but I wanted to mention the aspect nevertheless.
Jan
Re: [PATCH V3 11/23] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu
On 07.12.20 14:32, Jan Beulich wrote:
Hi Jan, Paul.
Quoted 17 lines · Oleksandr Tyshchenko
Quoted 16 lines
xen/arch/x86/hvm/emulate.c +2
-2@@ -142,8 +142,8 @@ void hvmemul_cancel(struct vcpu *v) {struct hvm_vcpu_io *vio = &v->arch.hvm.hvm_io; - vio->io_req.state =STATE_IOREQ_NONE ; - vio->io_completion = HVMIO_no_completion; + v->io.req.state =STATE_IOREQ_NONE ; + v->io.completion = IO_no_completion; vio->mmio_cache_count =0 ; vio->mmio_insn_bytes =0 ; vio->mmio_access = (struct npfec ){};@@ -159,7 +159,7 @@ static int hvmemul_do_io( {struct vcpu *curr = current;
Quoted 8 lines
xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 162
Quoted 3 lines
xen/arch/x86/hvm/emulate.c +1
-1struct domain *currd = curr->domain; -struct hvm_vcpu_io *vio = &curr->arch.hvm.hvm_io; +struct vcpu_io *vio = &curr->io;Taking just these two hunks: "vio" would now stand for two entirely different things. I realize the name is applicable to both, but I wonder if such naming isn't going to risk confusion.Despite being relatively familiar with the involved code, I've been repeatedly unsure what exactly "vio" covers, and needed to go back to the
Good comment... Agree that with the naming scheme in current patch the code became a little bit confusing to read.
Quoted 9 lines
header. So together with the name possible adjustment mentioned further down, maybe "
vcpu_io" also wants it name changed, such that the variable then also could sensibly be named (slightly) differently? structvcpu_io_statemaybe? Or alternatively rename variables of type structhvm_vcpu_io* to hvio or hio? Otoh the savings aren't very big for just ->io, so maybe better to stick to the prior name with the prior type, and not introduce local variables at all for the new field, like you already have it in the former case?
I would much prefer the last suggestion which is "not introduce local
variables at all for the new field" (I admit I was thinking almost the same, but haven't chosen this direction). But I am OK with any suggestions here. Paul what do you think?
Quoted 7 lines
Quoted 7 lines
xen/include/xen/sched.h +0
-0@@ -145,6 +145,21 @@ void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */struct waitqueue_vcpu ;
Quoted 10 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 155
Quoted 8 lines
xen/include/xen/sched.h +8
-0+enum io_completion { + IO_no_completion, + IO_mmio_completion, + IO_pio_completion, +#ifdef CONFIG_X86 + IO_realmode_completion, +#endif +};I'm not entirely happy with
io_/IO_here - they seem a little too generic. How aboutioreq_/IOREQ_respectively?
I am OK, would like to hear Paul's opinion on both questions.
Quoted 27 lines
Quoted 11 lines
xen/include/xen/sched.h +6
-0+struct vcpu_io { +/* I/O request in flight to device model. */ +enum io_completion completion; +ioreq_t req; +}; +struct vcpu {int vcpu_id;xen/include/xen/sched.h struct vcpu · line 278
Quoted 8 lines
xen/include/xen/sched.h +4
-0@@ -256,6 +271,10 @@ struct vcpustruct vpci_vcpu vpci;struct arch_vcpu arch; + +#ifdef CONFIG_IOREQ_SERVER +struct vcpu_io io; +#endif };I don't have a good solution in mind, and I'm also not meaning to necessarily request a change here, but I'd like to point out that this does away (for this part of it only, of course) with the overlaying of the PV and HVM sub-structs on x86. As long as the HVM part is the far bigger one, that's not a problem, but I wanted to mention the aspect nevertheless.
Jan
Signature · 3 lines
Regards, Oleksandr Tyshchenko
RE: [PATCH V3 11/23] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu
Quoted 55 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 07 December 2020 21:00
To: Jan Beulich <[email protected]>; Paul Durrant <[email protected]>
Cc: Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 11/23] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpuOn 07.12.20 14:32, Jan Beulich wrote:
Hi Jan, Paul.
Quoted 17 lines · Oleksandr Tyshchenko
Quoted 16 lines
xen/arch/x86/hvm/emulate.c +2
-2@@ -142,8 +142,8 @@ void hvmemul_cancel(struct vcpu *v) {struct hvm_vcpu_io *vio = &v->arch.hvm.hvm_io; - vio->io_req.state =STATE_IOREQ_NONE ; - vio->io_completion = HVMIO_no_completion; + v->io.req.state =STATE_IOREQ_NONE ; + v->io.completion = IO_no_completion; vio->mmio_cache_count =0 ; vio->mmio_insn_bytes =0 ; vio->mmio_access = (struct npfec ){};@@ -159,7 +159,7 @@ static int hvmemul_do_io( {struct vcpu *curr = current;Quoted 8 lines
xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 162
Quoted 3 lines
xen/arch/x86/hvm/emulate.c +1
-1struct domain *currd = curr->domain; -struct hvm_vcpu_io *vio = &curr->arch.hvm.hvm_io; +struct vcpu_io *vio = &curr->io;Taking just these two hunks: "vio" would now stand for two entirely different things. I realize the name is applicable to both, but I wonder if such naming isn't going to risk confusion.Despite being relatively familiar with the involved code, I've been repeatedly unsure what exactly "vio" covers, and needed to go back to the
Good comment... Agree that with the naming scheme in current patch the
code became a little bit confusing to read.Quoted 9 lines
header. So together with the name possible adjustment mentioned further down, maybe "
vcpu_io" also wants it name changed, such that the variable then also could sensibly be named (slightly) differently? structvcpu_io_statemaybe? Or alternatively rename variables of type structhvm_vcpu_io* to hvio or hio? Otoh the savings aren't very big for just ->io, so maybe better to stick to the prior name with the prior type, and not introduce local variables at all for the new field, like you already have it in the former case?
Quoted 5 lines
I would much prefer the last suggestion which is "not introduce local variables at all for the new field" (I admit I was thinking almost the same, but haven't chosen this direction).
But I am OK with any suggestions here. Paul what do you think?
I personally don't think there is that much risk of confusion. If there is a desire to disambiguate though, I would go the route of naming hvm_vcpu_io locals 'hvio'.
Quoted 20 lines
Quoted 5 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy
Quoted 5 lines
xen/include/xen/sched.h +0
-0@@ -145,6 +145,21 @@ void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy*/
Quoted 13 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 155
Quoted 11 lines
xen/include/xen/sched.h +8
-0struct waitqueue_vcpu ; +enum io_completion { + IO_no_completion, + IO_mmio_completion, + IO_pio_completion, +#ifdef CONFIG_X86 + IO_realmode_completion, +#endif +};I'm not entirely happy with
io_/IO_here - they seem a little too generic. How aboutioreq_/IOREQ_respectively?
Quoted 2 lines
I am OK, would like to hear Paul's opinion on both questions.
No, I think the 'IO_' prefix is better. They relate to a field in the vcpu_io struct. An alternative might be 'VIO_'...
Quoted 5 lines
Quoted 5 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 159
Quoted 5 lines
xen/include/xen/sched.h +3
-0+struct vcpu_io { +/* I/O request in flight to device model. */ +enum io_completion completion;
... in which case, you could also name the enum 'vio_completion'.
Paul
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/include/xen/sched.h +3
-0+ioreq_t req; +}; +
Re: [PATCH V3 11/23] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu
Quoted 30 lines · Paul Durrant
Quoted 23 lines
From: Oleksandr <[email protected]>
Sent: 07 December 2020 21:00Quoted 4 lines · Jan Beulich
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy
Quoted 3 lines · Oleksandr Tyshchenko
xen/include/xen/sched.h +0
-0@@ -145,6 +145,21 @@ void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy*/
Quoted 13 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 155
Quoted 11 lines
xen/include/xen/sched.h +8
-0struct waitqueue_vcpu ; +enum io_completion { + IO_no_completion, + IO_mmio_completion, + IO_pio_completion, +#ifdef CONFIG_X86 + IO_realmode_completion, +#endif +};I'm not entirely happy with
io_/IO_here - they seem a little too generic. How aboutioreq_/IOREQ_respectively?Quoted 2 lines
I am OK, would like to hear Paul's opinion on both questions.
No, I think the '
IO_' prefix is better. They relate to a field in thevcpu_iostruct. An alternative might be 'VIO_'...Quoted 2 lines
Quoted 2 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 159
Quoted 3 lines
xen/include/xen/sched.h +3
-0+struct vcpu_io { +/* I/O request in flight to device model. */ +enum io_completion completion;... in which case, you could also name the enum '
vio_completion'.
I'd be okay with these - still better than just "io".
Jan
Re: [PATCH V3 11/23] xen/ioreq: Move x86's io_completion/io_req fields to struct vcpu
In reply to Paul DurrantOn 08.12.20 09:52, Paul Durrant wrote:
Hi Paul, Jan
Quoted 38 lines
Quoted 38 lines
Quoted 17 lines · Oleksandr Tyshchenko
Quoted 16 lines
xen/arch/x86/hvm/emulate.c +2
-2@@ -142,8 +142,8 @@ void hvmemul_cancel(struct vcpu *v) {struct hvm_vcpu_io *vio = &v->arch.hvm.hvm_io; - vio->io_req.state =STATE_IOREQ_NONE ; - vio->io_completion = HVMIO_no_completion; + v->io.req.state =STATE_IOREQ_NONE ; + v->io.completion = IO_no_completion; vio->mmio_cache_count =0 ; vio->mmio_insn_bytes =0 ; vio->mmio_access = (struct npfec ){};@@ -159,7 +159,7 @@ static int hvmemul_do_io( {struct vcpu *curr = current;Quoted 8 lines
xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 162
Quoted 3 lines
xen/arch/x86/hvm/emulate.c +1
-1struct domain *currd = curr->domain; -struct hvm_vcpu_io *vio = &curr->arch.hvm.hvm_io; +struct vcpu_io *vio = &curr->io;Taking just these two hunks: "vio" would now stand for two entirely different things. I realize the name is applicable to both, but I wonder if such naming isn't going to risk confusion.Despite being relatively familiar with the involved code, I've been repeatedly unsure what exactly "vio" covers, and needed to go back to the
Good comment... Agree that with the naming scheme in current patch the
code became a little bit confusing to read.Quoted 9 lines
header. So together with the name possible adjustment mentioned further down, maybe "
vcpu_io" also wants it name changed, such that the variable then also could sensibly be named (slightly) differently? structvcpu_io_statemaybe? Or alternatively rename variables of type structhvm_vcpu_io* to hvio or hio? Otoh the savings aren't very big for just ->io, so maybe better to stick to the prior name with the prior type, and not introduce local variables at all for the new field, like you already have it in the former case?
Quoted 6 lines
Quoted 5 lines
I would much prefer the last suggestion which is "not introduce local variables at all for the new field" (I admit I was thinking almost the same, but haven't chosen this direction).
But I am OK with any suggestions here. Paul what do you think?I personally don't think there is that much risk of confusion. If there is a desire to disambiguate though, I would go the route of naming
hvm_vcpu_iolocals 'hvio'.
Well, I assume I should rename all hvm_vcpu_io locals in the code to "hvio" (even if this patch didn't touch these places so far since no new vcpu_io fields were involved). I am OK, although expecting a lot of places which need touching here...
Quoted 19 lines
Quoted 17 lines
Quoted 4 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy
Quoted 4 lines
xen/include/xen/sched.h +0
-0@@ -145,6 +145,21 @@ void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy*/
Quoted 12 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 155
Quoted 10 lines
xen/include/xen/sched.h +8
-0struct waitqueue_vcpu ; +enum io_completion { + IO_no_completion, + IO_mmio_completion, + IO_pio_completion, +#ifdef CONFIG_X86 + IO_realmode_completion, +#endif +};I'm not entirely happy with
io_/IO_here - they seem a little too generic. How aboutioreq_/IOREQ_respectively?Quoted 2 lines
I am OK, would like to hear Paul's opinion on both questions.
Quoted 8 lines
No, I think the '
IO_' prefix is better. They relate to a field in thevcpu_iostruct. An alternative might be 'VIO_'...Quoted 3 lines
Quoted 3 lines
xen/include/xen/sched.h void evtchn_destroy_final(struct domain *d); /* from complete_domain_destroy */ · line 159
Quoted 3 lines
xen/include/xen/sched.h +3
-0+struct vcpu_io { +/* I/O request in flight to device model. */ +enum io_completion completion;... in which case, you could also name the enum '
vio_completion'.Paul
ok, will follow new renaming scheme IO_ -> VIO_ (io_ -> vio_).
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 12/23 xen/ioreq: Remove "hvm" prefixes from involved function names
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
This patch removes "hvm" prefixes and infixes from IOREQ related function names in the common code and performs a renaming where appropriate according to the more consistent new naming scheme:
- IOREQ server functions should start with "ioreq_server_"
- IOREQ functions should start with "ioreq_"
A few function names are clarified to better fit into their purposes:handle_hvm_io_completion -> vcpu_ioreq_handle_completion
hvm_io_pending -> vcpu_ioreq_pending hvm_ioreq_init -> ioreq_domain_init hvm_alloc_ioreq_mfn -> ioreq_server_alloc_mfn hvm_free_ioreq_mfn -> ioreq_server_free_mfn
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V1 -> V2:
- new patch
Changes V2 -> V3:
- update patch according the "legacy interface" is x86 specific
- update patch description
- rename everything touched according to new naming scheme
---
--- xen/arch/x86/hvm/dm.c | 4 +- xen/arch/x86/hvm/emulate.c | 6 +- xen/arch/x86/hvm/hvm.c | 10 +-- xen/arch/x86/hvm/io.c | 6 +- xen/arch/x86/hvm/ioreq.c | 2 +- xen/arch/x86/hvm/stdvga.c | 4 +- xen/arch/x86/hvm/vmx/vvmx.c | 2 +- xen/common/dm.c | 28 +++---- xen/common/ioreq.c | 174 ++++++++++++++++++++++---------------------- xen/common/memory.c | 2 +- xen/include/xen/ioreq.h | 67 ++++++++--------- 11 files changed, 153 insertions(+), 152 deletions(-)
xen/arch/x86/hvm/dm.c +2 -2
@@ -352,8 +352,8 @@ int arch_dm_op(struct xen_dm_op *op, struct domain *d,break ;if ( first_gfn ==0 ) - rc =hvm_map_mem_type_to_ioreq_server (d, data->id, - data->type, data->flags); + rc =ioreq_server_map_mem_type (d, data->id, + data->type, data->flags);else rc =0 ;
xen/arch/x86/hvm/emulate.c +3 -3
@@ -261,7 +261,7 @@ static int hvmemul_do_io(* an ioreq server that can handle it. * * Rules: -* A> PIO or MMIO accesses run through hvm_select_ioreq_server() to +* A> PIO or MMIO accesses run through ioreq_server_select() to * choose the ioreq server by range. If no server is found, the access * is ignored. *
@@ -323,7 +323,7 @@ static int hvmemul_do_io( }if ( !s ) - s =hvm_select_ioreq_server (currd, &p); + s =ioreq_server_select (currd, &p);/* If there is no suitable backing DM, just ignore accesses */ if ( !s )
@@ -333,7 +333,7 @@ static int hvmemul_do_io( }else { - rc =hvm_send_ioreq (s, &p,0 ); + rc =ioreq_send (s, &p,0 );if ( rc !=X86EMUL_RETRY || currd->is_shutting_down ) vio->req.state =STATE_IOREQ_NONE ;else if ( !ioreq_needs_completion (&vio->req) )
xen/arch/x86/hvm/hvm.c +5 -5
@@ -546,7 +546,7 @@ void hvm_do_resume(struct vcpu *v)pt_restore_timer (v); -if ( !handle_hvm_io_completion (v) ) +if ( !vcpu_ioreq_handle_completion (v) )return ;if (unlikely (v->arch.vm_event) )
@@ -677,7 +677,7 @@ int hvm_domain_initialise(struct domain *d)register_g2m_portio_handler (d);register_vpci_portio_handler (d); -hvm_ioreq_init (d); +ioreq_domain_init (d);hvm_init_guest_time (d);
@@ -739,7 +739,7 @@ void hvm_domain_relinquish_resources(struct domain *d)viridian_domain_deinit (d); -hvm_destroy_all_ioreq_servers (d); +ioreq_server_destroy_all (d);msixtbl_pt_cleanup (d);
@@ -1582,7 +1582,7 @@ int hvm_vcpu_initialise(struct vcpu *v)if ( rc ) goto fail5; - rc =hvm_all_ioreq_servers_add_vcpu (d, v); + rc =ioreq_server_add_vcpu_all (d, v);if ( rc !=0 ) goto fail6;
@@ -1618,7 +1618,7 @@ void hvm_vcpu_destroy(struct vcpu *v) {viridian_vcpu_deinit (v); -hvm_all_ioreq_servers_remove_vcpu (v->domain, v); +ioreq_server_remove_vcpu_all (v->domain, v);if (hvm_altp2m_supported () )altp2m_vcpu_destroy (v);
xen/arch/x86/hvm/io.c +3 -3
@@ -60,7 +60,7 @@ void send_timeoffset_req(unsigned long timeoff)if ( timeoff ==0 )return ; -if (hvm_broadcast_ioreq (&p, true) !=0 ) +if (ioreq_broadcast (&p, true) !=0 )gprintk (XENLOG_ERR ,"Unsuccessful timeoffset update\n" ); }
@@ -74,7 +74,7 @@ void send_invalidate_req(void) .data = ~0UL, /* flush all */ }; -if (hvm_broadcast_ioreq (&p, false) !=0 ) +if (ioreq_broadcast (&p, false) !=0 )gprintk (XENLOG_ERR ,"Unsuccessful map-cache invalidate\n" ); }
@@ -155,7 +155,7 @@ bool handle_pio(uint16_t port, unsigned int size, int dir)* We should not advance RIP/EIP if the domain is shutting down or * if X86EMUL_RETRY has been returned by an internal handler. */ -if ( curr->domain->is_shutting_down || !hvm_io_pending (curr) ) +if ( curr->domain->is_shutting_down || !vcpu_ioreq_pending (curr) )return false;break ;
xen/arch/x86/hvm/ioreq.c +1 -1
@@ -154,7 +154,7 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf) {/* * If a page has already been allocated (which will happen on -* demand if hvm_get_ioreq_server_frame() is called), then +* demand if ioreq_server_get_frame() is called), then * mapping a guest frame is not permitted. */ if (gfn_eq (iorp->gfn,INVALID_GFN ) )
xen/arch/x86/hvm/stdvga.c +2 -2
@@ -507,11 +507,11 @@ static int stdvga_mem_write(const struct hvm_io_handler *handler, } done: - srv =hvm_select_ioreq_server (current->domain, &p); + srv =ioreq_server_select (current->domain, &p);if ( !srv )return X86EMUL_UNHANDLEABLE ; -return hvm_send_ioreq (srv, &p,1 ); +return ioreq_send (srv, &p,1 ); } static bool_t stdvga_mem_accept(const struct hvm_io_handler *handler,
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
@@ -1516,7 +1516,7 @@ void nvmx_switch_guest(void)* don't want to continue as this setup is not implemented nor supported * as of right now. */ -if (hvm_io_pending (v) ) +if (vcpu_ioreq_pending (v) )return ;/* * a softirq may interrupt us between a virtual vmentry is
xen/common/dm.c +14 -14
@@ -100,8 +100,8 @@ static int dm_op(const struct dmop_args *op_args)if ( data->pad[0 ] || data->pad[1 ] || data->pad[2 ] )break ; - rc =hvm_create_ioreq_server (d, data->handle_bufioreq, - &data->id); + rc =ioreq_server_create (d, data->handle_bufioreq, + &data->id);break ; }
@@ -117,12 +117,12 @@ static int dm_op(const struct dmop_args *op_args)if ( data->flags & ~valid_flags )break ; - rc =hvm_get_ioreq_server_info (d, data->id, - (data->flags & XEN_DMOP_no_gfns) ? -NULL : (unsigned long *)&data->ioreq_gfn, - (data->flags & XEN_DMOP_no_gfns) ? -NULL : (unsigned long *)&data->bufioreq_gfn, - &data->bufioreq_port); + rc =ioreq_server_get_info (d, data->id, + (data->flags & XEN_DMOP_no_gfns) ? +NULL : (unsigned long *)&data->ioreq_gfn, + (data->flags & XEN_DMOP_no_gfns) ? +NULL : (unsigned long *)&data->bufioreq_gfn, + &data->bufioreq_port);break ; }
@@ -135,8 +135,8 @@ static int dm_op(const struct dmop_args *op_args)if ( data->pad )break ; - rc =hvm_map_io_range_to_ioreq_server (d, data->id, data->type, - data->start, data->end); + rc =ioreq_server_map_io_range (d, data->id, data->type, + data->start, data->end);break ; }
@@ -149,8 +149,8 @@ static int dm_op(const struct dmop_args *op_args)if ( data->pad )break ; - rc =hvm_unmap_io_range_from_ioreq_server (d, data->id, data->type, - data->start, data->end); + rc =ioreq_server_unmap_io_range (d, data->id, data->type, + data->start, data->end);break ; }
@@ -163,7 +163,7 @@ static int dm_op(const struct dmop_args *op_args)if ( data->pad )break ; - rc =hvm_set_ioreq_server_state (d, data->id, !!data->enabled); + rc =ioreq_server_set_state (d, data->id, !!data->enabled);break ; }
@@ -176,7 +176,7 @@ static int dm_op(const struct dmop_args *op_args)if ( data->pad )break ; - rc =hvm_destroy_ioreq_server (d, data->id); + rc =ioreq_server_destroy (d, data->id);break ; }
xen/common/ioreq.c +87 -87
504 lines not on this page · raw message
xen/common/memory.c +1 -1
@@ -1108,7 +1108,7 @@ static int acquire_ioreq_server(struct domain *d, {mfn_t mfn; - rc =hvm_get_ioreq_server_frame (d, id, frame + i, &mfn); + rc =ioreq_server_get_frame (d, id, frame + i, &mfn);if ( rc )return rc;
xen/include/xen/ioreq.h +34 -33
75 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH V3 12/23] xen/ioreq: Remove "hvm" prefixes from involved function names
xen/common/ioreq.c bool is_ioreq_server_page(struct domain *d, const struct page_info *page) · line 307
Quoted 10 lines · Oleksandr Tyshchenko
xen/common/ioreq.c +2
-2@@ -301,8 +301,8 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)return found; } -static void hvm_update_ioreq_evtchn (struct ioreq_server *s, -struct ioreq_vcpu *sv) +static void ioreq_update_evtchn (struct ioreq_server *s, +struct ioreq_vcpu *sv) {ASSERT (spin_is_locked (&s->lock));
This looks to be an ioreq server function, which hence wants to be named ioreq_server_update_evtchn()? Then
Jan
Re: [PATCH V3 12/23] xen/ioreq: Remove "hvm" prefixes from involved function names
On 07.12.20 14:45, Jan Beulich wrote:
Hi Jan
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/common/ioreq.c +0
-0@@ -301,8 +301,8 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)return found; }
Quoted 8 lines
xen/common/ioreq.c bool is_ioreq_server_page(struct domain *d, const struct page_info *page) · line 307
Quoted 6 lines
xen/common/ioreq.c +2
-2-static void hvm_update_ioreq_evtchn (struct ioreq_server *s, -struct ioreq_vcpu *sv) +static void ioreq_update_evtchn (struct ioreq_server *s, +struct ioreq_vcpu *sv) {ASSERT (spin_is_locked (&s->lock));This looks to be an ioreq server function, which hence wants to be named
ioreq_server_update_evtchn()? Then
Will rename
Quoted 2 lines
Reviewed-by Jan Beulich <[email protected]>
Thank you
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 13/23 xen/ioreq: Use guest_cmpxchg64() instead of cmpxchg()
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
The cmpxchg() in ioreq_send_buffered() operates on memory shared with the emulator domain (and the target domain if the legacy interface is used).
In order to be on the safe side we need to switch to guest_cmpxchg64() to prevent a domain to DoS Xen on Arm.
As there is no plan to support the legacy interface on Arm, we will have a page to be mapped in a single domain at the time, so we can use s->emulator in guest_cmpxchg64() safely.
Thankfully the only user of the legacy interface is x86 so far and there is not concern regarding the atomics operations.
Please note, that the legacy interface *must* not be used on Arm without revisiting the code.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch
Changes V1 -> V2:
- move earlier to avoid breaking arm32 compilation
- add an explanation to commit description and hvm_allow_set_param()
- pass s->emulator
Changes V2 -> V3:
- update patch description
---
--- xen/arch/arm/hvm.c | 4 ++++ xen/common/ioreq.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-)
xen/arch/arm/hvm.c +4 -0
@@ -31,6 +31,10 @@#include <asm/hypercall.h> +/* +* The legacy interface (which involves magic IOREQ pages) *must* not be used +* without revisiting the code. +*/ static int hvm_allow_set_param (const struct domain *d,unsigned int param) { switch ( param )
xen/common/ioreq.c +2 -1
@@ -29,6 +29,7 @@#include <xen/trace.h> #include <xen/vpci.h> +#include <asm/guest_atomics.h> #include <asm/hvm/ioreq.h> #include <public/hvm/ioreq.h>
@@ -1182,7 +1183,7 @@ static int ioreq_send_buffered(struct ioreq_server *s, ioreq_t *p) new.read_pointer = old.read_pointer - n *IOREQ_BUFFER_SLOT_NUM ; new.write_pointer = old.write_pointer - n *IOREQ_BUFFER_SLOT_NUM ; -cmpxchg (&pg->ptrs.full, old.full, new.full); +guest_cmpxchg64 (s->emulator, &pg->ptrs.full, old.full, new.full); }notify_via_xen_event_channel (d, s->bufioreq_evtchn);
Signature · 1 line
2.7.4
Re: [PATCH V3 13/23] xen/ioreq: Use guest_cmpxchg64() instead of cmpxchg()
Quoted 47 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
The
cmpxchg()inioreq_send_buffered()operates on memory shared with the emulator domain (and the target domain if the legacy interface is used).In order to be on the safe side we need to switch to
guest_cmpxchg64()to prevent a domain to DoS Xen on Arm.As there is no plan to support the legacy interface on Arm, we will have a page to be mapped in a single domain at the time, so we can use
s->emulatoringuest_cmpxchg64()safely.Thankfully the only user of the legacy interface is x86 so far and there is not concern regarding the atomics operations.
Please note, that the legacy interface *must* not be used on Arm without revisiting the code.
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- new patchChanges V1 -> V2:
- move earlier to avoid breaking arm32 compilation
- add an explanation to commit description andhvm_allow_set_param()
- passs->emulatorChanges V2 -> V3:
- update patch description
------ xen/arch/arm/hvm.c | 4 ++++ xen/common/ioreq.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-)xen/arch/arm/hvm.c +0
-0
xen/arch/arm/hvm.c line 37
Quoted 7 lines
xen/arch/arm/hvm.c +4
-0@@ -31,6 +31,10 @@#include <asm/hypercall.h> +/* +* The legacy interface (which involves magic IOREQ pages) *must* not be used +* without revisiting the code. +*/
This is a NIT, but I'd prefer if you moved the comment a few lines below, maybe just before the existing comment starting with "The following parameters".
The reason is that as it is now it is not clear which set_params interfaces should not be used without revisiting the code.
With that:
Quoted 27 lines
xen/arch/arm/hvm.c +0
-0static int hvm_allow_set_param (const struct domain *d,unsigned int param) { switch ( param )xen/common/ioreq.c +2
-1@@ -29,6 +29,7 @@#include <xen/trace.h> #include <xen/vpci.h> +#include <asm/guest_atomics.h> #include <asm/hvm/ioreq.h> #include <public/hvm/ioreq.h> @@ -1182,7 +1183,7 @@ static int ioreq_send_buffered(struct ioreq_server *s, ioreq_t *p) new.read_pointer = old.read_pointer - n *IOREQ_BUFFER_SLOT_NUM ; new.write_pointer = old.write_pointer - n *IOREQ_BUFFER_SLOT_NUM ; -cmpxchg (&pg->ptrs.full, old.full, new.full); +guest_cmpxchg64 (s->emulator, &pg->ptrs.full, old.full, new.full); }notify_via_xen_event_channel (d, s->bufioreq_evtchn);Signature · 1 line
2.7.4
Re: [PATCH V3 13/23] xen/ioreq: Use guest_cmpxchg64() instead of cmpxchg()
On 09.12.20 23:32, Stefano Stabellini wrote:
Hi Stefano
Quoted 59 lines · Oleksandr Tyshchenko
Quoted 47 lines
From: Oleksandr Tyshchenko <[email protected]>
The
cmpxchg()inioreq_send_buffered()operates on memory shared with the emulator domain (and the target domain if the legacy interface is used).In order to be on the safe side we need to switch to
guest_cmpxchg64()to prevent a domain to DoS Xen on Arm.As there is no plan to support the legacy interface on Arm, we will have a page to be mapped in a single domain at the time, so we can use
s->emulatoringuest_cmpxchg64()safely.Thankfully the only user of the legacy interface is x86 so far and there is not concern regarding the atomics operations.
Please note, that the legacy interface *must* not be used on Arm without revisiting the code.
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- new patchChanges V1 -> V2:
- move earlier to avoid breaking arm32 compilation
- add an explanation to commit description andhvm_allow_set_param()
- passs->emulatorChanges V2 -> V3:
- update patch description
------ xen/arch/arm/hvm.c | 4 ++++ xen/common/ioreq.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-)xen/arch/arm/hvm.c +0
-0xen/arch/arm/hvm.c line 37
Quoted 7 lines
xen/arch/arm/hvm.c +4
-0@@ -31,6 +31,10 @@#include <asm/hypercall.h> +/* +* The legacy interface (which involves magic IOREQ pages) *must* not be used +* without revisiting the code. +*/ This is a NIT, but I'd prefer if you moved the comment a few lines below, maybe just before the existing comment starting with "The following parameters".
Quoted 2 lines
The reason is that as it is now it is not clear which
set_paramsinterfaces should not be used without revisiting the code.
OK, but maybe this comment wants dropping at all? It was actual when the legacy interface was the part of the common code (V2). Now the legacy interface is x86 specific so I am not sure this comment should be here.
Quoted 4 lines
With that:
Thank you
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 13/23] xen/ioreq: Use guest_cmpxchg64() instead of cmpxchg()
Quoted 58 lines · Oleksandr
Quoted 58 lines · Oleksandr Tyshchenko
Quoted 50 lines
From: Oleksandr Tyshchenko <[email protected]>
The
cmpxchg()inioreq_send_buffered()operates on memory shared with the emulator domain (and the target domain if the legacy interface is used).In order to be on the safe side we need to switch to
guest_cmpxchg64()to prevent a domain to DoS Xen on Arm.As there is no plan to support the legacy interface on Arm, we will have a page to be mapped in a single domain at the time, so we can use
s->emulatoringuest_cmpxchg64()safely.Thankfully the only user of the legacy interface is x86 so far and there is not concern regarding the atomics operations.
Please note, that the legacy interface *must* not be used on Arm without revisiting the code.
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- new patchChanges V1 -> V2:
- move earlier to avoid breaking arm32 compilation
- add an explanation to commit description andhvm_allow_set_param()
- passs->emulatorChanges V2 -> V3:
- update patch description
------ xen/arch/arm/hvm.c | 4 ++++ xen/common/ioreq.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-)xen/arch/arm/hvm.c +1
-0@@ -31,6 +31,10 @@#include <asm/hypercall.h> +/* +* The legacy interface (which involves magic IOREQ pages) *must* not be Quoted 3 lines
used
+ * without revisiting the code.
+ */This is a NIT, but I'd prefer if you moved the comment a few lines below, maybe just before the existing comment starting with "The following parameters".
Quoted 6 lines
Quoted 2 lines
The reason is that as it is now it is not clear which
set_paramsinterfaces should not be used without revisiting the code.OK, but maybe this comment wants dropping at all? It was actual when the legacy interface was the part of the common code (V2). Now the legacy interface is
x86 specific so I am not sure this comment should be here.
Yeah, fine by me.
Quoted 6 lines
Quoted 4 lines
With that:
Thank you
PATCH v3 14/23 arm/ioreq: Introduce arch specific bits for IOREQ/DM features
In reply to Oleksandr TyshchenkoFrom: Julien Grall <[email protected]>
This patch adds basic IOREQ/DM support on Arm. The subsequent patches will improve functionality and add remaining bits.
The IOREQ/DM features are supposed to be built with IOREQ_SERVER option enabled, which is disabled by default on Arm for now.
Please note, the "PIO handling" TODO is expected to left unaddressed for the current series. It is not an big issue for now while Xen doesn't have support for vPCI on Arm. On Arm64 they are only used for PCI IO Bar and we would probably want to expose them to emulator as PIO access to make a DM completely arch-agnostic. So "PIO handling" should be implemented when we add support for vPCI.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- was split into:
- arm/ioreq: Introduce arch specific bits for IOREQ/DM features
- xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm
- update patch description
- update asm-arm/hvm/ioreq.h according to the newly introduced arch functions:
- arch_hvm_destroy_ioreq_server()
- arch_handle_hvm_io_completion()
- update arch files to include xen/ioreq.h
- remove HVMOP plumbing
- rewrite a logic to handle properly case when hvm_send_ioreq() returns IO_RETRY
- add a logic to handle properly handle_hvm_io_completion() return value
- rename handle_mmio() to ioreq_handle_complete_mmio()
- move paging_mark_pfn_dirty() to asm-arm/paging.h
- remove forward declaration for hvm_ioreq_server in asm-arm/paging.h
- move try_fwd_ioserv() to ioreq.c, provide stubs if !CONFIG_IOREQ_SERVER
- do not remove #ifdef CONFIG_IOREQ_SERVER in memory.c for guarding xen/ioreq.h
- use gdprintk in try_fwd_ioserv(), remove unneeded prints
- update list of #include-s
- move has_vpci() to asm-arm/domain.h
- add a comment (TODO) to unimplemented yet handle_pio()
- remove hvm_mmio_first(last)_byte() and hvm_ioreq_(page/vcpu/server) structs from the arch files, they were already moved to the common code
- remove set_foreign_p2m_entry() changes, they will be properly implemented in the follow-up patch
- select IOREQ_SERVER for Arm instead of Arm64 in Kconfig
- remove x86's realmode and other unneeded stubs from xen/ioreq.h
- clafify ioreq_t p.df usage in try_fwd_ioserv()
- set ioreq_t p.count to 1 in try_fwd_ioserv()
Changes V1 -> V2:
- was split into:
- arm/ioreq: Introduce arch specific bits for IOREQ/DM features
- xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
- update the author of a patch
- update patch description
- move a loop in leave_hypervisor_to_guest() to a separate patch
- set IOREQ_SERVER disabled by default
- remove already clarified /* XXX */
- replace BUG() by ASSERT_UNREACHABLE() in handle_pio()
- remove default case for handling the return value of try_handle_mmio()
- remove struct hvm_domain, enum hvm_io_completion, struct hvm_vcpu_io, struct hvm_vcpu from asm-arm/domain.h, these are common materials now
- update everything according to the recent changes (IOREQ related function names don't contain "hvm" prefixes/infixes anymore, IOREQ related fields are part of common struct vcpu/domain now, etc)
Changes V2 -> V3:
- update patch according the "legacy interface" is x86 specific
- add dummy arch hooks
- remove dummy paging_mark_pfn_dirty()
- don’t include <xen/domain_page.h> in common ioreq.c
- don’t include <public/hvm/ioreq.h> in arch ioreq.h
- remove #define ioreq_params(d, i)
---
--- xen/arch/arm/Makefile | 2 + xen/arch/arm/dm.c | 34 ++++++++++ xen/arch/arm/domain.c | 9 +++ xen/arch/arm/io.c | 11 +++- xen/arch/arm/ioreq.c | 141 ++++++++++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c | 13 ++++ xen/include/asm-arm/domain.h | 3 + xen/include/asm-arm/hvm/ioreq.h | 139 +++++++++++++++++++++++++++++++++++++++ xen/include/asm-arm/mmio.h | 1 + 9 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 xen/arch/arm/dm.c create mode 100644 xen/arch/arm/ioreq.c create mode 100644 xen/include/asm-arm/hvm/ioreq.h
xen/arch/arm/Makefile +2 -0
@@ -13,6 +13,7 @@ obj-y += cpuerrata.o obj-y += cpufeature.o obj-y += decode.o obj-y += device.o +obj-$(CONFIG_IOREQ_SERVER) += dm.o obj-y += domain.o obj-y += domain_build.init.o obj-y += domctl.o
@@ -27,6 +28,7 @@ obj-y += guest_atomics.o obj-y += guest_walk.o obj-y += hvm.o obj-y += io.o +obj-$(CONFIG_IOREQ_SERVER) += ioreq.o obj-y += irq.o obj-y += kernel.init.o obj-$(CONFIG_LIVEPATCH) += livepatch.o
xen/arch/arm/dm.c new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <xen/dm.h> +#include <xen/hypercall.h> + +int arch_dm_op (struct xen_dm_op *op,struct domain *d, +const struct dmop_args *op_args,bool *const_op) +{ +return -EOPNOTSUPP ; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/arch/arm/domain.c +9 -0
@@ -15,6 +15,7 @@#include <xen/guest_access.h> #include <xen/hypercall.h> #include <xen/init.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/livepatch.h> #include <xen/sched.h>
@@ -696,6 +697,10 @@ int arch_domain_create(struct domain *d,ASSERT (config !=NULL ); +#ifdef CONFIG_IOREQ_SERVER +ioreq_domain_init (d); +#endif +/* p2m_init relies on some value initialized by the IOMMU subsystem */ if ( (rc =iommu_domain_init (d, config->iommu_opts)) !=0 ) goto fail;
@@ -1014,6 +1019,10 @@ int domain_relinquish_resources(struct domain *d)if (ret )return ret; +#ifdef CONFIG_IOREQ_SERVER +ioreq_server_destroy_all (d); +#endif + PROGRESS(xen): ret =relinquish_memory (d, &d->xenpage_list);if ( ret )
xen/arch/arm/io.c +10 -1
@@ -23,6 +23,7 @@#include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/hvm/ioreq.h> #include "decode.h"
@@ -123,7 +124,15 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, handler =find_mmio_handler (v->domain, info.gpa);if ( !handler ) -return IO_UNHANDLED ; + { +int rc; + + rc =try_fwd_ioserv (regs, v, &info); +if ( rc ==IO_HANDLED ) +return handle_ioserv (regs, v); + +return rc; + }/* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid )
xen/arch/arm/ioreq.c new file +141 -0
141 lines not on this page · raw message
xen/arch/arm/traps.c +13 -0
@@ -21,6 +21,7 @@#include <xen/hypercall.h> #include <xen/init.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/irq.h> #include <xen/lib.h> #include <xen/mem_access.h>
@@ -1385,6 +1386,9 @@ static arm_hypercall_t arm_hypercall_table[] = {#ifdef CONFIG_HYPFS HYPERCALL(hypfs_op, 5),#endif +#ifdef CONFIG_IOREQ_SERVER + HYPERCALL(dm_op, 3), +#endif };#ifndef NDEBUG
@@ -1956,6 +1960,9 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,case IO_HANDLED :advance_pc (regs, hsr);return ; +case IO_RETRY : +/* finish later */ +return ;case IO_UNHANDLED :/* IO unhandled, try another way to handle it. */ break ;
@@ -2254,6 +2261,12 @@ static void check_for_vcpu_work(void) {struct vcpu *v = current; +#ifdef CONFIG_IOREQ_SERVER +local_irq_enable (); +vcpu_ioreq_handle_completion (v); +local_irq_disable (); +#endif +if (likely (!v->arch.need_flush_to_ram) )return ;
xen/include/asm-arm/domain.h +3 -0
@@ -10,6 +10,7 @@#include <asm/gic.h> #include <asm/vgic.h> #include <asm/vpl011.h> +#include <public/hvm/dm_op.h> #include <public/hvm/params.h> struct hvm_domain
@@ -262,6 +263,8 @@ static inline void arch_vcpu_block(struct vcpu *v) {}#define arch_vm_assist_valid_mask (d) (1UL << VMASST_TYPE_runstate_update_flag) +#define has_vpci (d) ({ (void)(d); false; }) +#endif /* __ASM_DOMAIN_H__ */ /*
xen/include/asm-arm/hvm/ioreq.h new file +139 -0
139 lines not on this page · raw message
xen/include/asm-arm/mmio.h +1 -0
@@ -37,6 +37,7 @@ enum io_stateIO_ABORT ,/* The IO was handled by the helper and led to an abort. */ IO_HANDLED ,/* The IO was successfully handled by the helper. */ IO_UNHANDLED ,/* The IO was not handled by the helper. */ + IO_RETRY, /* Retry the emulation for some reason */ }; typedef int (*mmio_read_t)(struct vcpu *v, mmio_info_t *info,
Signature · 1 line
2.7.4
Re: [PATCH V3 14/23] arm/ioreq: Introduce arch specific bits for IOREQ/DM features
Quoted 267 lines · Oleksandr Tyshchenko
From: Julien Grall <[email protected]>
This patch adds basic IOREQ/DM support on Arm. The subsequent patches will improve functionality and add remaining bits.
The IOREQ/DM features are supposed to be built with
IOREQ_SERVERoption enabled, which is disabled by default on Arm for now.Please note, the "PIO handling" TODO is expected to left unaddressed for the current series. It is not an big issue for now while Xen doesn't have support for vPCI on Arm. On Arm64 they are only used for PCI IO Bar and we would probably want to expose them to emulator as PIO access to make a DM completely arch-agnostic. So "PIO handling" should be implemented when we add support for vPCI.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- was split into:
- arm/ioreq: Introduce arch specific bits for IOREQ/DM features
- xen/mm: Handle properly reference inset_foreign_p2m_entry()on Arm
- update patch description
- updateasm-arm/hvm/ioreq.haccording to the newly introduced arch functions:
-arch_hvm_destroy_ioreq_server()
-arch_handle_hvm_io_completion()
- update arch files to includexen/ioreq.h
- remove HVMOP plumbing
- rewrite a logic to handle properly case whenhvm_send_ioreq()returnsIO_RETRY
- add a logic to handle properlyhandle_hvm_io_completion()return value
- renamehandle_mmio()toioreq_handle_complete_mmio()
- movepaging_mark_pfn_dirty()toasm-arm/paging.h
- remove forward declaration forhvm_ioreq_serverinasm-arm/paging.h
- movetry_fwd_ioserv()toioreq.c, provide stubs if !CONFIG_IOREQ_SERVER
- do not remove #ifdefCONFIG_IOREQ_SERVERinmemory.cfor guardingxen/ioreq.h
- use gdprintk intry_fwd_ioserv(), remove unneeded prints
- update list of #include-s
- movehas_vpci()toasm-arm/domain.h
- add a comment (TODO) to unimplemented yethandle_pio()
- remove hvm_mmio_first(last)_byte() andhvm_ioreq_(page/vcpu/server)structs from the arch files, they were already moved to the common code
- removeset_foreign_p2m_entry()changes, they will be properly implemented in the follow-up patch
- selectIOREQ_SERVERfor Arm instead of Arm64 inKconfig
- remove x86's realmode and other unneeded stubs fromxen/ioreq.h
- clafifyioreq_tp.df usage intry_fwd_ioserv()
- setioreq_tp.count to 1 intry_fwd_ioserv()Changes V1 -> V2:
- was split into:
- arm/ioreq: Introduce arch specific bits for IOREQ/DM features
- xen/arm: Stick around inleave_hypervisor_to_guestuntil I/O has completed
- update the author of a patch
- update patch description
- move a loop inleave_hypervisor_to_guest()to a separate patch
- setIOREQ_SERVERdisabled by default
- remove already clarified /* XXX */
- replaceBUG()byASSERT_UNREACHABLE()inhandle_pio()
- remove default case for handling the return value oftry_handle_mmio()
- remove structhvm_domain, enumhvm_io_completion, structhvm_vcpu_io, structhvm_vcpufromasm-arm/domain.h, these are common materials now
- update everything according to the recent changes (IOREQ related function names don't contain "hvm" prefixes/infixes anymore, IOREQ related fields are part of common struct vcpu/domain now, etc)Changes V2 -> V3:
- update patch according the "legacy interface" is x86 specific
- add dummy arch hooks
- remove dummypaging_mark_pfn_dirty()
- don’t include <xen/domain_page.h> in commonioreq.c
- don’t include <public/hvm/ioreq.h> in archioreq.h
- remove #defineioreq_params(d, i)
------ xen/arch/arm/Makefile | 2 + xen/arch/arm/dm.c | 34 ++++++++++ xen/arch/arm/domain.c | 9 +++ xen/arch/arm/io.c | 11 +++- xen/arch/arm/ioreq.c | 141 ++++++++++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c | 13 ++++ xen/include/asm-arm/domain.h | 3 + xen/include/asm-arm/hvm/ioreq.h | 139 +++++++++++++++++++++++++++++++++++++++ xen/include/asm-arm/mmio.h | 1 + 9 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 xen/arch/arm/dm.c create mode 100644 xen/arch/arm/ioreq.c create mode 100644 xen/include/asm-arm/hvm/ioreq.hxen/arch/arm/Makefile +2
-0@@ -13,6 +13,7 @@ obj-y += cpuerrata.o obj-y += cpufeature.o obj-y += decode.o obj-y += device.o +obj-$(CONFIG_IOREQ_SERVER) += dm.o obj-y += domain.o obj-y += domain_build.init.o obj-y += domctl.o@@ -27,6 +28,7 @@ obj-y += guest_atomics.o obj-y += guest_walk.o obj-y += hvm.o obj-y += io.o +obj-$(CONFIG_IOREQ_SERVER) += ioreq.o obj-y += irq.o obj-y += kernel.init.o obj-$(CONFIG_LIVEPATCH) += livepatch.oxen/arch/arm/dm.c new file +34
-0@@ -0,0 +1,34 @@ +/* +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <xen/dm.h> +#include <xen/hypercall.h> + +int arch_dm_op (struct xen_dm_op *op,struct domain *d, +const struct dmop_args *op_args,bool *const_op) +{ +return -EOPNOTSUPP ; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ xen/arch/arm/domain.c +9
-0@@ -15,6 +15,7 @@#include <xen/guest_access.h> #include <xen/hypercall.h> #include <xen/init.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/livepatch.h> #include <xen/sched.h> @@ -696,6 +697,10 @@ int arch_domain_create(struct domain *d,ASSERT (config !=NULL ); +#ifdef CONFIG_IOREQ_SERVER +ioreq_domain_init (d); +#endif +/* p2m_init relies on some value initialized by the IOMMU subsystem */ if ( (rc =iommu_domain_init (d, config->iommu_opts)) !=0 ) goto fail;@@ -1014,6 +1019,10 @@ int domain_relinquish_resources(struct domain *d)if (ret )return ret; +#ifdef CONFIG_IOREQ_SERVER +ioreq_server_destroy_all (d); +#endif + PROGRESS(xen): ret =relinquish_memory (d, &d->xenpage_list);if ( ret )xen/arch/arm/io.c +10
-1@@ -23,6 +23,7 @@#include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/hvm/ioreq.h> #include "decode.h" @@ -123,7 +124,15 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, handler =find_mmio_handler (v->domain, info.gpa);if ( !handler ) -return IO_UNHANDLED ; + { +int rc; + + rc =try_fwd_ioserv (regs, v, &info); +if ( rc ==IO_HANDLED ) +return handle_ioserv (regs, v); + +return rc; + }/* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid )xen/arch/arm/ioreq.c new file +46
-0@@ -0,0 +1,141 @@ +/* +* arm/ioreq.c: hardware virtual machine I/O emulation +* +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <xen/domain.h> +#include <xen/ioreq.h> + +#include <asm/traps.h> + +#include <public/hvm/ioreq.h> + +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v) +{ +const union hsr hsr = { .bits = regs->hsr }; +const struct hsr_dabt dabt = hsr.dabt; +/* Code is similar to handle_read */ +uint8_t size = (1 << dabt.size) *8 ; +register_t r = v->io.req.data; + +/* We are done with the IO */ + v->io.req.state =STATE_IOREQ_NONE ; + +if ( dabt.write ) +return IO_HANDLED ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. +*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + {
xen/arch/arm/ioreq.c line 56
Quoted 10 lines
xen/arch/arm/ioreq.c new file +10
-0+/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size; + } + +set_user_reg (regs, dabt.reg, r);
Could you introduce a set_user_reg_signextend static inline function that can be used both here and in handle_read?
Quoted 187 lines
xen/arch/arm/ioreq.c new file +84
-0+return IO_HANDLED ; +} + +enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info) +{ +struct vcpu_io *vio = &v->io; +ioreq_t p = { + .type =IOREQ_TYPE_COPY , + .addr = info->gpa, + .size =1 << info->dabt.size, + .count =1 , + .dir = !info->dabt.write, +/* +* On x86, df is used by 'rep' instruction to tell the direction +* to iterate (forward or backward). +* On Arm, all the accesses to MMIO region will do a single +* memory access. So for now, we can safely always set to 0. +*/ + .df =0 , + .data =get_user_reg (regs, info->dabt.reg), + .state =STATE_IOREQ_READY , + }; +struct ioreq_server *s =NULL ; +enum io_state rc; + +switch ( vio->req.state ) + { +case STATE_IOREQ_NONE : +break ; + +case STATE_IORESP_READY : +return IO_HANDLED ; + +default : +gdprintk (XENLOG_ERR ,"wrong state %u\n" , vio->req.state); +return IO_ABORT ; + } + + s =ioreq_server_select (v->domain, &p); +if ( !s ) +return IO_UNHANDLED ; + +if ( !info->dabt.valid ) +return IO_ABORT ; + + vio->req = p; + + rc =ioreq_send (s, &p,0 ); +if ( rc !=IO_RETRY || v->domain->is_shutting_down ) + vio->req.state =STATE_IOREQ_NONE ; +else if ( !ioreq_needs_completion (&vio->req) ) + rc =IO_HANDLED ; +else + vio->completion = IO_mmio_completion; + +return rc; +} + +bool ioreq_complete_mmio (void ) +{ +struct vcpu *v = current; +struct cpu_user_regs *regs =guest_cpu_user_regs (); +const union hsr hsr = { .bits = regs->hsr }; +paddr_t addr = v->io.req.addr; + +if (try_handle_mmio (regs, hsr, addr) ==IO_HANDLED ) + { +advance_pc (regs, hsr); +return true; + } + +return false; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ xen/arch/arm/traps.c +13
-0@@ -21,6 +21,7 @@#include <xen/hypercall.h> #include <xen/init.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/irq.h> #include <xen/lib.h> #include <xen/mem_access.h> @@ -1385,6 +1386,9 @@ static arm_hypercall_t arm_hypercall_table[] = {#ifdef CONFIG_HYPFS HYPERCALL(hypfs_op, 5),#endif +#ifdef CONFIG_IOREQ_SERVER + HYPERCALL(dm_op, 3), +#endif };#ifndef NDEBUG @@ -1956,6 +1960,9 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,case IO_HANDLED :advance_pc (regs, hsr);return ; +case IO_RETRY : +/* finish later */ +return ;case IO_UNHANDLED :/* IO unhandled, try another way to handle it. */ break ;@@ -2254,6 +2261,12 @@ static void check_for_vcpu_work(void) {struct vcpu *v = current; +#ifdef CONFIG_IOREQ_SERVER +local_irq_enable (); +vcpu_ioreq_handle_completion (v); +local_irq_disable (); +#endif +if (likely (!v->arch.need_flush_to_ram) )return ;xen/include/asm-arm/domain.h +3
-0@@ -10,6 +10,7 @@#include <asm/gic.h> #include <asm/vgic.h> #include <asm/vpl011.h> +#include <public/hvm/dm_op.h> #include <public/hvm/params.h> struct hvm_domain @@ -262,6 +263,8 @@ static inline void arch_vcpu_block(struct vcpu *v) {}#define arch_vm_assist_valid_mask (d) (1UL << VMASST_TYPE_runstate_update_flag) +#define has_vpci (d) ({ (void)(d); false; }) +#endif /* __ASM_DOMAIN_H__ */ /* xen/include/asm-arm/hvm/ioreq.h new file +31
-0@@ -0,0 +1,139 @@ +/* +* hvm.h: Hardware virtual machine assist interface definitions. +* +* Copyright (c) 2016 Citrix Systems Inc. +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __ASM_ARM_HVM_IOREQ_H__ +#define __ASM_ARM_HVM_IOREQ_H__ + +#include <xen/ioreq.h> + +#ifdef CONFIG_IOREQ_SERVER +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v); +enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info); +#else +static inline enum io_state handle_ioserv (struct cpu_user_regs *regs, +struct vcpu *v)
xen/include/asm-arm/hvm/ioreq.h line 41
Quoted 10 lines
xen/include/asm-arm/hvm/ioreq.h new file +10
-0+{ +return IO_UNHANDLED ; +} + +static inline enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info) +{ +return IO_UNHANDLED ; +} +#endif
If we are providing stub functions, then we can also provide stub functions for:
ioreq_domain_initioreq_server_destroy_all
and avoid the ifdefs.
Quoted 64 lines
xen/include/asm-arm/hvm/ioreq.h new file +64
-0+bool ioreq_complete_mmio (void ); + +static inline bool handle_pio (uint16_t port,unsigned int size,int dir) +{ +/* +* TODO: For Arm64, the main user will be PCI. So this should be +* implemented when we add support for vPCI. +*/ +ASSERT_UNREACHABLE (); +return true; +} + +static inline void msix_write_completion (struct vcpu *v) +{ +} + +static inline bool arch_vcpu_ioreq_completion (enum io_completion io_completion) +{ +ASSERT_UNREACHABLE (); +return true; +} + +/* +* The "legacy" mechanism of mapping magic pages for the IOREQ servers +* is x86 specific, so the following hooks don't need to be implemented on Arm: +* - arch_ioreq_server_map_pages +* - arch_ioreq_server_unmap_pages +* - arch_ioreq_server_enable +* - arch_ioreq_server_disable +*/ +static inline int arch_ioreq_server_map_pages (struct ioreq_server *s) +{ +return -EOPNOTSUPP ; +} + +static inline void arch_ioreq_server_unmap_pages (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_enable (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_disable (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_destroy (struct ioreq_server *s) +{ +} + +static inline int arch_ioreq_server_map_mem_type (struct domain *d, +struct ioreq_server *s, +uint32_t flags) +{ +return -EOPNOTSUPP ; +} + +static inline bool arch_ioreq_server_destroy_all (struct domain *d) +{ +return true; +} + +static inline int arch_ioreq_server_get_type_addr(const struct domain *d,
xen/include/asm-arm/hvm/ioreq.h line 116
Quoted 10 lines
xen/include/asm-arm/hvm/ioreq.h new file +10
-0+const ioreq_t *p, + uint8_t *type, + uint64_t *addr) +{ + if ( p->type != IOREQ_TYPE_COPY && p->type != IOREQ_TYPE_PIO ) + return -EINVAL; + + *type = (p->type ==IOREQ_TYPE_PIO ) ? +XEN_DMOP_IO_RANGE_PORT :XEN_DMOP_IO_RANGE_MEMORY ; + *addr = p->addr;
This function is not used in this patch and PIOs are left unimplemented according to a few comments, so I am puzzled by this code here. Do we need it?
Quoted 37 lines
xen/include/asm-arm/hvm/ioreq.h new file +22
-0+ return 0; +} + +static inline void arch_ioreq_domain_init (struct domain *d) +{ +} + +#define IOREQ_STATUS_HANDLED IO_HANDLED +#define IOREQ_STATUS_UNHANDLED IO_UNHANDLED +#define IOREQ_STATUS_RETRY IO_RETRY + +#endif /* __ASM_ARM_HVM_IOREQ_H__ */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ xen/include/asm-arm/mmio.h +1
-0@@ -37,6 +37,7 @@ enum io_stateIO_ABORT ,/* The IO was handled by the helper and led to an abort. */ IO_HANDLED ,/* The IO was successfully handled by the helper. */ IO_UNHANDLED ,/* The IO was not handled by the helper. */ + IO_RETRY, /* Retry the emulation for some reason */ }; typedef int (*mmio_read_t)(struct vcpu *v, mmio_info_t *info,Signature · 1 line
2.7.4
Re: [PATCH V3 14/23] arm/ioreq: Introduce arch specific bits for IOREQ/DM features
On 10.12.20 00:04, Stefano Stabellini wrote:
Hi Stefano
Quoted 272 lines · Oleksandr Tyshchenko
Quoted 271 lines
From: Julien Grall <[email protected]>
This patch adds basic IOREQ/DM support on Arm. The subsequent patches will improve functionality and add remaining bits.
The IOREQ/DM features are supposed to be built with
IOREQ_SERVERoption enabled, which is disabled by default on Arm for now.Please note, the "PIO handling" TODO is expected to left unaddressed for the current series. It is not an big issue for now while Xen doesn't have support for vPCI on Arm. On Arm64 they are only used for PCI IO Bar and we would probably want to expose them to emulator as PIO access to make a DM completely arch-agnostic. So "PIO handling" should be implemented when we add support for vPCI.
Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- was split into:
- arm/ioreq: Introduce arch specific bits for IOREQ/DM features
- xen/mm: Handle properly reference inset_foreign_p2m_entry()on Arm
- update patch description
- updateasm-arm/hvm/ioreq.haccording to the newly introduced arch functions:
-arch_hvm_destroy_ioreq_server()
-arch_handle_hvm_io_completion()
- update arch files to includexen/ioreq.h
- remove HVMOP plumbing
- rewrite a logic to handle properly case whenhvm_send_ioreq()returnsIO_RETRY
- add a logic to handle properlyhandle_hvm_io_completion()return value
- renamehandle_mmio()toioreq_handle_complete_mmio()
- movepaging_mark_pfn_dirty()toasm-arm/paging.h
- remove forward declaration forhvm_ioreq_serverinasm-arm/paging.h
- movetry_fwd_ioserv()toioreq.c, provide stubs if !CONFIG_IOREQ_SERVER
- do not remove #ifdefCONFIG_IOREQ_SERVERinmemory.cfor guardingxen/ioreq.h
- use gdprintk intry_fwd_ioserv(), remove unneeded prints
- update list of #include-s
- movehas_vpci()toasm-arm/domain.h
- add a comment (TODO) to unimplemented yethandle_pio()
- remove hvm_mmio_first(last)_byte() andhvm_ioreq_(page/vcpu/server)structs from the arch files, they were already moved to the common code
- removeset_foreign_p2m_entry()changes, they will be properly implemented in the follow-up patch
- selectIOREQ_SERVERfor Arm instead of Arm64 inKconfig
- remove x86's realmode and other unneeded stubs fromxen/ioreq.h
- clafifyioreq_tp.df usage intry_fwd_ioserv()
- setioreq_tp.count to 1 intry_fwd_ioserv()Changes V1 -> V2:
- was split into:
- arm/ioreq: Introduce arch specific bits for IOREQ/DM features
- xen/arm: Stick around inleave_hypervisor_to_guestuntil I/O has completed
- update the author of a patch
- update patch description
- move a loop inleave_hypervisor_to_guest()to a separate patch
- setIOREQ_SERVERdisabled by default
- remove already clarified /* XXX */
- replaceBUG()byASSERT_UNREACHABLE()inhandle_pio()
- remove default case for handling the return value oftry_handle_mmio()
- remove structhvm_domain, enumhvm_io_completion, structhvm_vcpu_io, structhvm_vcpufromasm-arm/domain.h, these are common materials now
- update everything according to the recent changes (IOREQ related function names don't contain "hvm" prefixes/infixes anymore, IOREQ related fields are part of common struct vcpu/domain now, etc)Changes V2 -> V3:
- update patch according the "legacy interface" is x86 specific
- add dummy arch hooks
- remove dummypaging_mark_pfn_dirty()
- don’t include <xen/domain_page.h> in commonioreq.c
- don’t include <public/hvm/ioreq.h> in archioreq.h
- remove #defineioreq_params(d, i)
------ xen/arch/arm/Makefile | 2 + xen/arch/arm/dm.c | 34 ++++++++++ xen/arch/arm/domain.c | 9 +++ xen/arch/arm/io.c | 11 +++- xen/arch/arm/ioreq.c | 141 ++++++++++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c | 13 ++++ xen/include/asm-arm/domain.h | 3 + xen/include/asm-arm/hvm/ioreq.h | 139 +++++++++++++++++++++++++++++++++++++++ xen/include/asm-arm/mmio.h | 1 + 9 files changed, 352 insertions(+), 1 deletion(-)create mode 100644
xen/arch/arm/dm.ccreate mode 100644xen/arch/arm/ioreq.ccreate mode 100644xen/include/asm-arm/hvm/ioreq.hxen/arch/arm/Makefile +2
-0@@ -13,6 +13,7 @@ obj-y += cpuerrata.o obj-y += cpufeature.o obj-y += decode.o obj-y += device.o +obj-$(CONFIG_IOREQ_SERVER) += dm.o obj-y += domain.o obj-y += domain_build.init.o obj-y += domctl.o@@ -27,6 +28,7 @@ obj-y += guest_atomics.o obj-y += guest_walk.o obj-y += hvm.o obj-y += io.o +obj-$(CONFIG_IOREQ_SERVER) += ioreq.o obj-y += irq.o obj-y += kernel.init.o obj-$(CONFIG_LIVEPATCH) += livepatch.oxen/arch/arm/dm.c new file +34
-0@@ -0,0 +1,34 @@ +/* +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <xen/dm.h> +#include <xen/hypercall.h> + +int arch_dm_op (struct xen_dm_op *op,struct domain *d, +const struct dmop_args *op_args,bool *const_op) +{ +return -EOPNOTSUPP ; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ xen/arch/arm/domain.c +9
-0@@ -15,6 +15,7 @@#include <xen/guest_access.h> #include <xen/hypercall.h> #include <xen/init.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/livepatch.h> #include <xen/sched.h> @@ -696,6 +697,10 @@ int arch_domain_create(struct domain *d,ASSERT (config !=NULL ); +#ifdef CONFIG_IOREQ_SERVER +ioreq_domain_init (d); +#endif +/* p2m_init relies on some value initialized by the IOMMU subsystem */ if ( (rc =iommu_domain_init (d, config->iommu_opts)) !=0 ) goto fail;@@ -1014,6 +1019,10 @@ int domain_relinquish_resources(struct domain *d)if (ret )return ret; +#ifdef CONFIG_IOREQ_SERVER +ioreq_server_destroy_all (d); +#endif + PROGRESS(xen): ret =relinquish_memory (d, &d->xenpage_list);if ( ret )xen/arch/arm/io.c +10
-1@@ -23,6 +23,7 @@#include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/hvm/ioreq.h> #include "decode.h" @@ -123,7 +124,15 @@ enum io_state try_handle_mmio(struct cpu_user_regs *regs, handler =find_mmio_handler (v->domain, info.gpa);if ( !handler ) -return IO_UNHANDLED ; + { +int rc; + + rc =try_fwd_ioserv (regs, v, &info); +if ( rc ==IO_HANDLED ) +return handle_ioserv (regs, v); + +return rc; + }/* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid )xen/arch/arm/ioreq.c new file +50
-0@@ -0,0 +1,141 @@ +/* +* arm/ioreq.c: hardware virtual machine I/O emulation +* +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <xen/domain.h> +#include <xen/ioreq.h> + +#include <asm/traps.h> + +#include <public/hvm/ioreq.h> + +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v) +{ +const union hsr hsr = { .bits = regs->hsr }; +const struct hsr_dabt dabt = hsr.dabt; +/* Code is similar to handle_read */ +uint8_t size = (1 << dabt.size) *8 ; +register_t r = v->io.req.data; + +/* We are done with the IO */ + v->io.req.state =STATE_IOREQ_NONE ; + +if ( dabt.write ) +return IO_HANDLED ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. +*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + { +/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller.
Quoted 8 lines
xen/arch/arm/ioreq.c line 56
Quoted 6 lines
xen/arch/arm/ioreq.c new file +6
-0+*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size; + } + +set_user_reg (regs, dabt.reg, r);Could you introduce a
set_user_reg_signextendstatic inline function that can be used both here and inhandle_read?
Yes, already done (this was requested by Julien). Please see
https://www.mail-archive.com/[email protected]/msg86986.html
Quoted 202 lines
Quoted 189 lines
xen/arch/arm/ioreq.c new file +84
-0+return IO_HANDLED ; +} + +enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info) +{ +struct vcpu_io *vio = &v->io; +ioreq_t p = { + .type =IOREQ_TYPE_COPY , + .addr = info->gpa, + .size =1 << info->dabt.size, + .count =1 , + .dir = !info->dabt.write, +/* +* On x86, df is used by 'rep' instruction to tell the direction +* to iterate (forward or backward). +* On Arm, all the accesses to MMIO region will do a single +* memory access. So for now, we can safely always set to 0. +*/ + .df =0 , + .data =get_user_reg (regs, info->dabt.reg), + .state =STATE_IOREQ_READY , + }; +struct ioreq_server *s =NULL ; +enum io_state rc; + +switch ( vio->req.state ) + { +case STATE_IOREQ_NONE : +break ; + +case STATE_IORESP_READY : +return IO_HANDLED ; + +default : +gdprintk (XENLOG_ERR ,"wrong state %u\n" , vio->req.state); +return IO_ABORT ; + } + + s =ioreq_server_select (v->domain, &p); +if ( !s ) +return IO_UNHANDLED ; + +if ( !info->dabt.valid ) +return IO_ABORT ; + + vio->req = p; + + rc =ioreq_send (s, &p,0 ); +if ( rc !=IO_RETRY || v->domain->is_shutting_down ) + vio->req.state =STATE_IOREQ_NONE ; +else if ( !ioreq_needs_completion (&vio->req) ) + rc =IO_HANDLED ; +else + vio->completion = IO_mmio_completion; + +return rc; +} + +bool ioreq_complete_mmio (void ) +{ +struct vcpu *v = current; +struct cpu_user_regs *regs =guest_cpu_user_regs (); +const union hsr hsr = { .bits = regs->hsr }; +paddr_t addr = v->io.req.addr; + +if (try_handle_mmio (regs, hsr, addr) ==IO_HANDLED ) + { +advance_pc (regs, hsr); +return true; + } + +return false; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ xen/arch/arm/traps.c +13
-0@@ -21,6 +21,7 @@#include <xen/hypercall.h> #include <xen/init.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/irq.h> #include <xen/lib.h> #include <xen/mem_access.h> @@ -1385,6 +1386,9 @@ static arm_hypercall_t arm_hypercall_table[] = {#ifdef CONFIG_HYPFS HYPERCALL(hypfs_op, 5),#endif +#ifdef CONFIG_IOREQ_SERVER + HYPERCALL(dm_op, 3), +#endif };#ifndef NDEBUG @@ -1956,6 +1960,9 @@ static void do_trap_stage2_abort_guest(struct cpu_user_regs *regs,case IO_HANDLED :advance_pc (regs, hsr);return ; +case IO_RETRY : +/* finish later */ +return ;case IO_UNHANDLED :/* IO unhandled, try another way to handle it. */ break ;@@ -2254,6 +2261,12 @@ static void check_for_vcpu_work(void) {struct vcpu *v = current; +#ifdef CONFIG_IOREQ_SERVER +local_irq_enable (); +vcpu_ioreq_handle_completion (v); +local_irq_disable (); +#endif +if (likely (!v->arch.need_flush_to_ram) )return ;xen/include/asm-arm/domain.h +3
-0@@ -10,6 +10,7 @@#include <asm/gic.h> #include <asm/vgic.h> #include <asm/vpl011.h> +#include <public/hvm/dm_op.h> #include <public/hvm/params.h> struct hvm_domain @@ -262,6 +263,8 @@ static inline void arch_vcpu_block(struct vcpu *v) {}#define arch_vm_assist_valid_mask (d) (1UL << VMASST_TYPE_runstate_update_flag) +#define has_vpci (d) ({ (void)(d); false; }) +#endif /* __ASM_DOMAIN_H__ */ /* xen/include/asm-arm/hvm/ioreq.h new file +31
-0@@ -0,0 +1,139 @@ +/* +* hvm.h: Hardware virtual machine assist interface definitions. +* +* Copyright (c) 2016 Citrix Systems Inc. +* Copyright (c) 2019 Arm ltd. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __ASM_ARM_HVM_IOREQ_H__ +#define __ASM_ARM_HVM_IOREQ_H__ + +#include <xen/ioreq.h> + +#ifdef CONFIG_IOREQ_SERVER +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v); +enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info); +#else +static inline enum io_state handle_ioserv (struct cpu_user_regs *regs, +struct vcpu *v)xen/include/asm-arm/hvm/ioreq.h line 41
Quoted 10 lines
xen/include/asm-arm/hvm/ioreq.h new file +10
-0+{ +return IO_UNHANDLED ; +} + +static inline enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info) +{ +return IO_UNHANDLED ; +} +#endif If we are providing stub functions, then we can also provide stub functions for:
Quoted 4 lines
ioreq_domain_initioreq_server_destroy_alland avoid the ifdefs.
I got your point. These are common IOREQ interface functions, which declarations live in the common header, should I provide stubs in the common ioreq.h?
Quoted 71 lines
Quoted 71 lines
xen/include/asm-arm/hvm/ioreq.h new file +69
-0+bool ioreq_complete_mmio (void ); + +static inline bool handle_pio (uint16_t port,unsigned int size,int dir) +{ +/* +* TODO: For Arm64, the main user will be PCI. So this should be +* implemented when we add support for vPCI. +*/ +ASSERT_UNREACHABLE (); +return true; +} + +static inline void msix_write_completion (struct vcpu *v) +{ +} + +static inline bool arch_vcpu_ioreq_completion (enum io_completion io_completion) +{ +ASSERT_UNREACHABLE (); +return true; +} + +/* +* The "legacy" mechanism of mapping magic pages for the IOREQ servers +* is x86 specific, so the following hooks don't need to be implemented on Arm: +* - arch_ioreq_server_map_pages +* - arch_ioreq_server_unmap_pages +* - arch_ioreq_server_enable +* - arch_ioreq_server_disable +*/ +static inline int arch_ioreq_server_map_pages (struct ioreq_server *s) +{ +return -EOPNOTSUPP ; +} + +static inline void arch_ioreq_server_unmap_pages (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_enable (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_disable (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_destroy (struct ioreq_server *s) +{ +} + +static inline int arch_ioreq_server_map_mem_type (struct domain *d, +struct ioreq_server *s, +uint32_t flags) +{ +return -EOPNOTSUPP ; +} + +static inline bool arch_ioreq_server_destroy_all (struct domain *d) +{ +return true; +} + +static inline int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr) +{ +if ( p->type !=IOREQ_TYPE_COPY && p->type !=IOREQ_TYPE_PIO )
Quoted 8 lines
xen/include/asm-arm/hvm/ioreq.h line 116
Quoted 5 lines
xen/include/asm-arm/hvm/ioreq.h new file +5
-0+return -EINVAL ; + + *type = (p->type ==IOREQ_TYPE_PIO ) ? +XEN_DMOP_IO_RANGE_PORT :XEN_DMOP_IO_RANGE_MEMORY ; + *addr = p->addr;This function is not used in this patch and PIOs are left unimplemented according to a few comments, so I am puzzled by this code here. Do we need it?
Yes. It is called from ioreq_server_select (common/ioreq.c). I could just skip PIO case and use *type = XEN_DMOP_IO_RANGE_MEMORY, but I didn't want to diverge.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 14/23] arm/ioreq: Introduce arch specific bits for IOREQ/DM features
Quoted 21 lines · Oleksandr
Quoted 21 lines
Quoted 14 lines
xen/include/asm-arm/hvm/ioreq.h new file +13
-0+#ifdef CONFIG_IOREQ_SERVER +enum io_state handle_ioserv (struct cpu_user_regs *regs,struct vcpu *v); +enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, +struct vcpu *v,mmio_info_t *info); +#else +static inline enum io_state handle_ioserv (struct cpu_user_regs *regs, +struct vcpu *v) +{ +return IO_UNHANDLED ; +} + +static inline enum io_state try_fwd_ioserv (struct cpu_user_regs *regs, + struct vcpu *v, mmio_info_t*info)
xen/include/asm-arm/hvm/ioreq.h line 41
Quoted 4 lines
xen/include/asm-arm/hvm/ioreq.h new file +4
-0+{ +return IO_UNHANDLED ; +} +#endif If we are providing stub functions, then we can also provide stub functions for:
Quoted 7 lines
Quoted 4 lines
ioreq_domain_initioreq_server_destroy_alland avoid the ifdefs.
I got your point. These are common IOREQ interface functions, which declarations live in the common header, should I provide stubs in the common
ioreq.h?
I'd prefer that, but if Jan and Paul don't want to have them I won't insist.
Quoted 79 lines
Quoted 71 lines
Quoted 71 lines
xen/include/asm-arm/hvm/ioreq.h new file +17
-0+bool ioreq_complete_mmio (void ); + +static inline bool handle_pio (uint16_t port,unsigned int size,int dir) +{ +/* +* TODO: For Arm64, the main user will be PCI. So this should be +* implemented when we add support for vPCI. +*/ +ASSERT_UNREACHABLE (); +return true; +} + +static inline void msix_write_completion (struct vcpu *v) +{ +} + +static inline bool arch_vcpu_ioreq_completion(enum io_completion
io_completion)xen/include/asm-arm/hvm/ioreq.h new file +8
-0+{ +ASSERT_UNREACHABLE (); +return true; +} + +/* +* The "legacy" mechanism of mapping magic pages for the IOREQ servers +* is x86 specific, so the following hooks don't need to be implemented on Arm:
+ * -arch_ioreq_server_map_pages
+ * -arch_ioreq_server_unmap_pages
+ * -arch_ioreq_server_enable
+ * -arch_ioreq_server_disable
+ */xen/include/asm-arm/hvm/ioreq.h new file +39
-0+static inline int arch_ioreq_server_map_pages (struct ioreq_server *s) +{ +return -EOPNOTSUPP ; +} + +static inline void arch_ioreq_server_unmap_pages (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_enable (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_disable (struct ioreq_server *s) +{ +} + +static inline void arch_ioreq_server_destroy (struct ioreq_server *s) +{ +} + +static inline int arch_ioreq_server_map_mem_type (struct domain *d, +struct ioreq_server *s, +uint32_t flags) +{ +return -EOPNOTSUPP ; +} + +static inline bool arch_ioreq_server_destroy_all (struct domain *d) +{ +return true; +} + +static inline int arch_ioreq_server_get_type_addr (const struct domain *d, +const ioreq_t *p, +uint8_t *type, +uint64_t *addr) +{ +if ( p->type !=IOREQ_TYPE_COPY && p->type !=IOREQ_TYPE_PIO )Quoted 8 lines
xen/include/asm-arm/hvm/ioreq.h line 116
Quoted 5 lines
xen/include/asm-arm/hvm/ioreq.h new file +5
-0+return -EINVAL ; + + *type = (p->type ==IOREQ_TYPE_PIO ) ? +XEN_DMOP_IO_RANGE_PORT :XEN_DMOP_IO_RANGE_MEMORY ; + *addr = p->addr;This function is not used in this patch and PIOs are left unimplemented according to a few comments, so I am puzzled by this code here. Do we need it?
Quoted 3 lines
Yes. It is called from
ioreq_server_select(common/ioreq.c). I could just skip PIO case and use
*type =XEN_DMOP_IO_RANGE_MEMORY, but I didn't want to diverge.
I see. OK then.
PATCH v3 15/23 xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of vcpu_ioreq_handle_completion() which involves using a loop in leave_hypervisor_to_guest().
The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
This wouldn't be an issue for Xen as do_softirq() would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V1 -> V2:
- new patch, changes were derived from (+ new explanation):
arm/ioreq: Introduce arch specific bits for IOREQ/DM features
Changes V2 -> V3:
- update patch description
---
--- xen/arch/arm/traps.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
xen/arch/arm/traps.c +26 -5
@@ -2257,18 +2257,23 @@ static void check_for_pcpu_work(void)* Process pending work for the vCPU. Any call should be fast or * implement preemption. */ -static void check_for_vcpu_work (void ) +static bool check_for_vcpu_work (void ) {struct vcpu *v = current;#ifdef CONFIG_IOREQ_SERVER +bool handled; +local_irq_enable (); -vcpu_ioreq_handle_completion (v); + handled =vcpu_ioreq_handle_completion (v);local_irq_disable (); + +if ( !handled ) +return true;#endif if (likely (!v->arch.need_flush_to_ram) ) -return ; +return false;/* * Give a chance for the pCPU to process work before handling the vCPU
@@ -2279,6 +2284,8 @@ static void check_for_vcpu_work(void)local_irq_enable ();p2m_flush_vm (v);local_irq_disable (); + +return false; }/*
@@ -2291,8 +2298,22 @@ void leave_hypervisor_to_guest(void) {local_irq_disable (); -check_for_vcpu_work (); -check_for_pcpu_work (); +/* +* The reason to use an unbounded loop here is the fact that vCPU +* shouldn't continue until an I/O has completed. In Xen case, if an I/O +* never completes then it most likely means that something went horribly +* wrong with the Device Emulator. And it is most likely not safe to +* continue. So letting the vCPU to spin forever if I/O never completes +* is a safer action than letting it continue and leaving the guest in +* unclear state and is the best what we can do for now. +* +* This wouldn't be an issue for Xen as do_softirq() would be called at +* every loop. In case of failure, the guest will crash and the vCPU +* will be unscheduled. +*/ +do { +check_for_pcpu_work (); + }while (check_for_vcpu_work () );vgic_sync_to_lrs ();
Signature · 1 line
2.7.4
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
Hello Oleksandr,
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 4 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.
Why you don't block vcpu there and unblock it when response is ready? If I got it right, "client" vcpu will spin in the loop, eating own scheduling budget with no useful work done. In the worst case, it will prevent "server" vcpu from running.
Quoted 82 lines
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patch, changes were derived from (+ new explanation):
arm/ioreq: Introduce arch specific bits for IOREQ/DM featuresChanges V2 -> V3:
- update patch description
------ xen/arch/arm/traps.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)xen/arch/arm/traps.c +26
-5@@ -2257,18 +2257,23 @@ static void check_for_pcpu_work(void)* Process pending work for the vCPU. Any call should be fast or * implement preemption. */ -static void check_for_vcpu_work (void ) +static bool check_for_vcpu_work (void ) {struct vcpu *v = current;#ifdef CONFIG_IOREQ_SERVER +bool handled; +local_irq_enable (); -vcpu_ioreq_handle_completion (v); + handled =vcpu_ioreq_handle_completion (v);local_irq_disable (); + +if ( !handled ) +return true;#endif if (likely (!v->arch.need_flush_to_ram) ) -return ; +return false;/* * Give a chance for the pCPU to process work before handling the vCPU @@ -2279,6 +2284,8 @@ static void check_for_vcpu_work(void)local_irq_enable ();p2m_flush_vm (v);local_irq_disable (); + +return false; }/* @@ -2291,8 +2298,22 @@ void leave_hypervisor_to_guest(void) {local_irq_disable (); -check_for_vcpu_work (); -check_for_pcpu_work (); +/* +* The reason to use an unbounded loop here is the fact that vCPU +* shouldn't continue until an I/O has completed. In Xen case, if an I/O +* never completes then it most likely means that something went horribly +* wrong with the Device Emulator. And it is most likely not safe to +* continue. So letting the vCPU to spin forever if I/O never completes +* is a safer action than letting it continue and leaving the guest in +* unclear state and is the best what we can do for now. +* +* This wouldn't be an issue for Xen as do_softirq() would be called at +* every loop. In case of failure, the guest will crash and the vCPU +* will be unscheduled. +*/ +do { +check_for_pcpu_work (); + }while (check_for_vcpu_work () );vgic_sync_to_lrs ();
Signature · 1 line
Volodymyr Babchuk at EPAM
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
Hi Volodymyr,
Quoted 16 lines · Volodymyr Babchuk
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 6 lines
Quoted 4 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.Why you don't block vcpu there and unblock it when response is ready?
The vCPU will already block while waiting for the event channel. See the call wait_for_event_channel() in the ioreq code. However, you can still receive spurious unblock (e.g. the event channel notificaiton is received before the I/O is unhandled). So you have to loop around and check if there are more work to do.
Quoted 3 lines
If
I got it right, "client" vcpu will spin in the loop, eating own scheduling budget with no useful work done.
You can't really do much about that if you have a rogue/buggy device model.
Quoted 2 lines
In the worst case, it will
prevent "server" vcpu from running.
How so? Xen will raise the schedule softirq if the I/O is handled. You would have a pretty buggy system if your "client" vCPU is considered to be a much higher priority than your "server" vCPU.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
In reply to Oleksandr TyshchenkoQuoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 3 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.
Imagine that we have two guests: one that requires an ioreq server and one that doesn't. If I am not mistaken this loop could potentially spin forever on a pcpu, thus preventing any other guest being scheduled, even if the other guest doesn't need any ioreq servers.
My other concern is that we are busy-looping. Could we call something like wfi() or do_idle() instead? The ioreq server event notification of completion should wake us up?
Following this line of thinking, I am wondering if instead of the busy-loop we should call vcpu_block_unless_event_pending(current) in try_handle_mmio if IO_RETRY. Then when the emulation is done, QEMU (or equivalent) calls xenevtchn_notify which ends up waking up the domU vcpu. Would that work?
Quoted 86 lines
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patch, changes were derived from (+ new explanation):
arm/ioreq: Introduce arch specific bits for IOREQ/DM featuresChanges V2 -> V3:
- update patch description
------ xen/arch/arm/traps.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)xen/arch/arm/traps.c +26
-5@@ -2257,18 +2257,23 @@ static void check_for_pcpu_work(void)* Process pending work for the vCPU. Any call should be fast or * implement preemption. */ -static void check_for_vcpu_work (void ) +static bool check_for_vcpu_work (void ) {struct vcpu *v = current;#ifdef CONFIG_IOREQ_SERVER +bool handled; +local_irq_enable (); -vcpu_ioreq_handle_completion (v); + handled =vcpu_ioreq_handle_completion (v);local_irq_disable (); + +if ( !handled ) +return true;#endif if (likely (!v->arch.need_flush_to_ram) ) -return ; +return false;/* * Give a chance for the pCPU to process work before handling the vCPU @@ -2279,6 +2284,8 @@ static void check_for_vcpu_work(void)local_irq_enable ();p2m_flush_vm (v);local_irq_disable (); + +return false; }/* @@ -2291,8 +2298,22 @@ void leave_hypervisor_to_guest(void) {local_irq_disable (); -check_for_vcpu_work (); -check_for_pcpu_work (); +/* +* The reason to use an unbounded loop here is the fact that vCPU +* shouldn't continue until an I/O has completed. In Xen case, if an I/O +* never completes then it most likely means that something went horribly +* wrong with the Device Emulator. And it is most likely not safe to +* continue. So letting the vCPU to spin forever if I/O never completes +* is a safer action than letting it continue and leaving the guest in +* unclear state and is the best what we can do for now. +* +* This wouldn't be an issue for Xen as do_softirq() would be called at +* every loop. In case of failure, the guest will crash and the vCPU +* will be unscheduled. +*/ +do { +check_for_pcpu_work (); + }while (check_for_vcpu_work () );vgic_sync_to_lrs ();Signature · 1 line
2.7.4
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
Quoted 29 lines · Stefano Stabellini
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 3 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.Imagine that we have two guests: one that requires an ioreq server and one that doesn't. If I am not mistaken this loop could potentially spin forever on a pcpu, thus preventing any other guest being scheduled, even if the other guest doesn't need any ioreq servers.
My other concern is that we are busy-looping. Could we call something like
wfi()ordo_idle()instead? The ioreq server event notification of completion should wake us up?
Quoted 5 lines
Following this line of thinking, I am wondering if instead of the busy-loop we should call
vcpu_block_unless_event_pending(current)intry_handle_mmioifIO_RETRY. Then when the emulation is done, QEMU (or equivalent) callsxenevtchn_notifywhich ends up waking up the domU vcpu. Would that work?
I read now Julien's reply: we are already doing something similar to what I suggested with the following call chain:
check_for_vcpu_work -> vcpu_ioreq_handle_completion -> wait_for_io -> wait_on_xen_event_channel
So the busy-loop here is only a safety-belt in cause of a spurious wake-up, in which case we are going to call again check_for_vcpu_work, potentially causing a guest reschedule.
Then, this is fine and addresses both my concerns. Maybe let's add a note in the commit message about it.
I am also wondering if there is any benefit in calling wait_for_io() earlier, maybe from try_handle_mmio if IO_RETRY? leave_hypervisor_to_guest is very late for that. In any case, it is not an important optimization (if it is even an optimization at all) so it is fine to leave it as is.
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
Quoted 39 lines · Stefano Stabellini
Quoted 29 lines · Stefano Stabellini
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 3 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.Imagine that we have two guests: one that requires an ioreq server and one that doesn't. If I am not mistaken this loop could potentially spin forever on a pcpu, thus preventing any other guest being scheduled, even if the other guest doesn't need any ioreq servers.
My other concern is that we are busy-looping. Could we call something like
wfi()ordo_idle()instead? The ioreq server event notification of completion should wake us up?Quoted 5 lines
Following this line of thinking, I am wondering if instead of the busy-loop we should call
vcpu_block_unless_event_pending(current)intry_handle_mmioifIO_RETRY. Then when the emulation is done, QEMU (or equivalent) callsxenevtchn_notifywhich ends up waking up the domU vcpu. Would that work?I read now Julien's reply: we are already doing something similar to what I suggested with the following call chain:
Quoted 8 lines
check_for_vcpu_work->vcpu_ioreq_handle_completion->wait_for_io->wait_on_xen_event_channelSo the busy-loop here is only a safety-belt in cause of a spurious wake-up, in which case we are going to call again
check_for_vcpu_work, potentially causing a guest reschedule.Then, this is fine and addresses both my concerns. Maybe let's add a note in the commit message about it.
Damm, I hit the "sent" button just a second before seen your reply. :/ Oh well. I suggested the same because I have seen the same question multiple time.
Quoted 4 lines
I am also wondering if there is any benefit in calling
wait_for_io()earlier, maybe fromtry_handle_mmioifIO_RETRY?
wait_for_io() may end up to deschedule the vCPU. I would like to avoid this to happen in the middle of the I/O emulation because we need to happen it without lock held at all.
I don't think there are locks involved today, but the deeper in the call stack the scheduling happens, the more chance we may screw up in the future.
However...
Quoted 1 line
leave_hypervisor_to_guestis very late for that.
... I am not sure what's the problem with that. The IOREQ will be notified of the pending I/O as soon as try_handle_mmio() put the I/O in the shared page.
If the IOREQ server is running on a different pCPU, then it might be possible that the I/O has completed before reached leave_hypervisor_to_guest(). In this case, we would not have to wait for the I/O.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
Quoted 47 lines · Julien Grall
Quoted 46 lines · Stefano Stabellini
Quoted 29 lines · Stefano Stabellini
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 3 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.Imagine that we have two guests: one that requires an ioreq server and one that doesn't. If I am not mistaken this loop could potentially spin forever on a pcpu, thus preventing any other guest being scheduled, even if the other guest doesn't need any ioreq servers.
My other concern is that we are busy-looping. Could we call something like
wfi()ordo_idle()instead? The ioreq server event notification of completion should wake us up?Quoted 5 lines
Following this line of thinking, I am wondering if instead of the busy-loop we should call
vcpu_block_unless_event_pending(current)intry_handle_mmioifIO_RETRY. Then when the emulation is done, QEMU (or equivalent) callsxenevtchn_notifywhich ends up waking up the domU vcpu. Would that work?I read now Julien's reply: we are already doing something similar to what I suggested with the following call chain:
check_for_vcpu_work->vcpu_ioreq_handle_completion->wait_for_io->wait_on_xen_event_channelSo the busy-loop here is only a safety-belt in cause of a spurious wake-up, in which case we are going to call again
check_for_vcpu_work, potentially causing a guest reschedule.
Quoted 6 lines
Quoted 2 lines
Then, this is fine and addresses both my concerns. Maybe let's add a note in the commit message about it.
Damm, I hit the "sent" button just a second before seen your reply. :/ Oh well. I suggested the same because I have seen the same question multiple time.
:-)
Quoted 12 lines
Quoted 2 lines
I am also wondering if there is any benefit in calling
wait_for_io()earlier, maybe fromtry_handle_mmioifIO_RETRY?
wait_for_io()may end up to deschedule the vCPU. I would like to avoid this to happen in the middle of the I/O emulation because we need to happen it without lock held at all.I don't think there are locks involved today, but the deeper in the call stack the scheduling happens, the more chance we may screw up in the future.
However...
Quoted 8 lines
Quoted 1 line
leave_hypervisor_to_guestis very late for that.... I am not sure what's the problem with that. The IOREQ will be notified of the pending I/O as soon as
try_handle_mmio()put the I/O in the shared page.If the IOREQ server is running on a different pCPU, then it might be possible that the I/O has completed before reached
leave_hypervisor_to_guest(). In this case, we would not have to wait for the I/O.
Yeah, I was thinking about that too. Actually it could be faster this way we end up being lucky.
The reason for moving it earlier would be that by the time leave_hypervisor_to_guest is called "Xen" has already decided to continue running this particular vcpu. If we called wait_for_io() earlier, we would give important information to the scheduler before any decision is made. This is more "philosophical" than practical though. Let's leave it as is.
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
Hi Stefano,
Quoted 21 lines · Stefano Stabellini
Quoted 12 lines
Quoted 2 lines
I am also wondering if there is any benefit in calling
wait_for_io()earlier, maybe fromtry_handle_mmioifIO_RETRY?
wait_for_io()may end up to deschedule the vCPU. I would like to avoid this to happen in the middle of the I/O emulation because we need to happen it without lock held at all.I don't think there are locks involved today, but the deeper in the call stack the scheduling happens, the more chance we may screw up in the future.
However...
Quoted 8 lines
Quoted 1 line
leave_hypervisor_to_guestis very late for that.... I am not sure what's the problem with that. The IOREQ will be notified of the pending I/O as soon as
try_handle_mmio()put the I/O in the shared page.If the IOREQ server is running on a different pCPU, then it might be possible that the I/O has completed before reached
leave_hypervisor_to_guest(). In this case, we would not have to wait for the I/O.
Quoted 8 lines
Yeah, I was thinking about that too. Actually it could be faster this way we end up being lucky.
The reason for moving it earlier would be that by the time
leave_hypervisor_to_guestis called "Xen" has already decided to continue running this particular vcpu. If we calledwait_for_io()earlier, we would give important information to the scheduler before any decision is made.
I don't understand this. Xen preemption is voluntary, so the scheduler is not going to run unless requested.
wait_for_io() is a preemption point. So if you call it, then vCPU may get descheduled at that point.
Why would we want to do this? What's our benefits here?
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
In reply to Stefano StabelliniOn 10.12.20 04:30, Stefano Stabellini wrote:
Hi Julien, Stefano
Quoted 46 lines · Julien Grall
Quoted 45 lines · Stefano Stabellini
Quoted 44 lines · Stefano Stabellini
Quoted 28 lines · Oleksandr Tyshchenko
Quoted 14 lines
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 3 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.Imagine that we have two guests: one that requires an ioreq server and one that doesn't. If I am not mistaken this loop could potentially spin forever on a pcpu, thus preventing any other guest being scheduled, even if the other guest doesn't need any ioreq servers.
My other concern is that we are busy-looping. Could we call something like
wfi()ordo_idle()instead? The ioreq server event notification of completion should wake us up?Quoted 5 lines
Following this line of thinking, I am wondering if instead of the busy-loop we should call
vcpu_block_unless_event_pending(current)intry_handle_mmioifIO_RETRY. Then when the emulation is done, QEMU (or equivalent) callsxenevtchn_notifywhich ends up waking up the domU vcpu. Would that work?I read now Julien's reply: we are already doing something similar to what I suggested with the following call chain:
check_for_vcpu_work->vcpu_ioreq_handle_completion->wait_for_io->wait_on_xen_event_channelSo the busy-loop here is only a safety-belt in cause of a spurious wake-up, in which case we are going to call again
check_for_vcpu_work, potentially causing a guest reschedule.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Then, this is fine and addresses both my concerns. Maybe let's add a note in the commit message about it.
Damm, I hit the "sent" button just a second before seen your reply. :/ Oh well. I suggested the same because I have seen the same question multiple time.
I will update commit description and probably the comment in code.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 15/23] xen/arm: Stick around in leave_hypervisor_to_guest until I/O has completed
In reply to Stefano StabelliniHi Stefano,
Quoted 15 lines · Stefano Stabellini
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch adds proper handling of return value of
vcpu_ioreq_handle_completion()which involves using a loop inleave_hypervisor_to_guest().The reason to use an unbounded loop here is the fact that vCPU shouldn't continue until an I/O has completed. In Xen case, if an I/O never completes then it most likely means that something went horribly wrong with the Device Emulator. And it is most likely not safe to continue. So letting the vCPU to spin forever if I/O never completes is a safer action than letting it continue and leaving the guest in unclear state and is the best what we can do for now.
Quoted 8 lines
Quoted 3 lines
This wouldn't be an issue for Xen as
do_softirq()would be called at every loop. In case of failure, the guest will crash and the vCPU will be unscheduled.Imagine that we have two guests: one that requires an ioreq server and one that doesn't. If I am not mistaken this loop could potentially spin forever on a pcpu, thus preventing any other guest being scheduled, even if the other guest doesn't need any ioreq servers.
That's not correct. At every loop we will call check_for_pcpu_work() that will process pending softirqs. If the rescheduling is necessary (might be set by a timer or by a caller in check_for_vcpu_work()), then the vCPU will be descheduled to give place to someone else.
Quoted 5 lines
My other concern is that we are busy-looping. Could we call something like
wfi()ordo_idle()instead? The ioreq server event notification of completion should wake us up?
There are no busy loop here. If the IOREQ has not yet handled the I/O we will block the vCPU until an event notification is received (see the call to wait_on_xen_event_channel()).
This loop make sure that all the vPCU works are done before we return to the guest.
The worse that can happen here if the vCPU will never run again if the IOREQ server is been naughty.
Quoted 6 lines
Following this line of thinking, I am wondering if instead of the busy-loop we should call
vcpu_block_unless_event_pending(current)intry_handle_mmioifIO_RETRY. Then when the emulation is done, QEMU (or equivalent) callsxenevtchn_notifywhich ends up waking up the domU vcpu. Would that work?
vcpu_block_unless_event_pending() will not block if there are interrupts pending. However, here we really want to block until the I/O has been completed. So vcpu_block_unless_event_pending() is not the right approach.
The IOREQ code is using wait_on_xen_event_channel(). Yet, this can still "exit" early if an event has been received. But this doesn't mean the I/O has completed. So we need to check if the I/O has completed and wait again if it hasn't.
I seem to keep having to explain how the code works. So maybe we want to update the commit message with more details?
Cheers,
Signature · 1 line
Julien Grall
PATCH v3 16/23 xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
This patch implements reference counting of foreign entries in in set_foreign_p2m_entry() on Arm. This is a mandatory action if we want to run emulator (IOREQ server) in other than dom0 domain, as we can't trust it to do the right thing if it is not running in dom0. So we need to grab a reference on the page to avoid it disappearing.
It is valid to always pass "p2m_map_foreign_rw" type to guest_physmap_add_entry() since the current and foreign domains would be always different. A case when they are equal would be rejected by rcu_lock_remote_domain_by_id(). Besides the similar comment in the code put a respective ASSERT() to catch incorrect usage in future.
It was tested with IOREQ feature to confirm that all the pages given to this function belong to a domain, so we can use the same approach as for XENMAPSPACE_gmfn_foreign handling in xenmem_add_to_physmap_one().
This involves adding an extra parameter for the foreign domain to set_foreign_p2m_entry() and a helper to indicate whether the arch supports the reference counting of foreign entries and the restriction for the hardware domain in the common code can be skipped for it.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch, was split from:
"[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM features"
- rewrite a logic to handle properly reference in set_foreign_p2m_entry() instead of treating foreign entries as p2m_ram_rw
Changes V1 -> V2:
- rebase according to the recent changes to acquire_resource()
- update patch description
- introduce arch_refcounts_p2m()
- add an explanation why p2m_map_foreign_rw is valid
- move set_foreign_p2m_entry() to p2m-common.h
- add const to new parameter
Changes V2 -> V3:
- update patch description
- rename arch_refcounts_p2m() to arch_acquire_resource_check()
- move comment to x86’s arch_acquire_resource_check()
- return rc in Arm's set_foreign_p2m_entry()
- put a respective ASSERT() into Arm's set_foreign_p2m_entry()
---
--- xen/arch/arm/p2m.c | 24 ++++++++++++++++++++++++ xen/arch/x86/mm/p2m.c | 5 +++-- xen/common/memory.c | 10 +++------- xen/include/asm-arm/p2m.h | 19 +++++++++---------- xen/include/asm-x86/p2m.h | 16 +++++++++++++--- xen/include/xen/p2m-common.h | 4 ++++ 6 files changed, 56 insertions(+), 22 deletions(-)
xen/arch/arm/p2m.c +24 -0
@@ -1380,6 +1380,30 @@ int guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,return p2m_remove_mapping (d, gfn, (1 << page_order), mfn); } +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn) +{ +struct page_info *page =mfn_to_page (mfn); +int rc; + +if ( !get_page (page, fd) ) +return -EINVAL ; + +/* +* It is valid to always use p2m_map_foreign_rw here as if this gets +* called then d != fd. A case when d == fd would be rejected by +* rcu_lock_remote_domain_by_id() earlier. Put a respective ASSERT() +* to catch incorrect usage in future. +*/ +ASSERT (d != fd); + + rc =guest_physmap_add_entry (d,_gfn (gfn), mfn,0 , p2m_map_foreign_rw); +if ( rc ) +put_page (page); + +return rc; +} +static struct page_info *p2m_allocate_root (void ) {struct page_info *page;
xen/arch/x86/mm/p2m.c +3 -2
@@ -1321,7 +1321,8 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn_l, }/* Set foreign mfn in the given guest's p2m table. */ -int set_foreign_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn) +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn) {return set_typed_p2m_entry (d, gfn, mfn,PAGE_ORDER_4K , p2m_map_foreign,p2m_get_hostp2m (d)->default_access);
@@ -2621,7 +2622,7 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,* will update the m2p table which will result in mfn -> gpfn of dom0 * and not fgfn of domU. */ - rc =set_foreign_p2m_entry (tdom, gpfn, mfn); + rc =set_foreign_p2m_entry (tdom, fdom, gpfn, mfn);if ( rc ) gdprintk(XENLOG_WARNING, "set_foreign_p2m_entry failed. " "gpfn:%lx mfn:%lx fgfn:%lx td:%d fd:%d\n",
xen/common/memory.c +3 -7
@@ -1134,12 +1134,8 @@ static int acquire_resource(xen_pfn_t mfn_list[32 ];int rc; -/* -* FIXME: Until foreign pages inserted into the P2M are properly -* reference counted, it is unsafe to allow mapping of -* resource pages unless the caller is the hardware domain. -*/ -if (paging_mode_translate (currd) && !is_hardware_domain (currd) ) +if (paging_mode_translate (currd) && !is_hardware_domain (currd) && + !arch_acquire_resource_check () )return -EACCES ;if (copy_from_guest (&xmar, arg,1 ) )
@@ -1207,7 +1203,7 @@ static int acquire_resource(for ( i =0 ; !rc && i < xmar.nr_frames; i++ ) { - rc =set_foreign_p2m_entry (currd, gfn_list[i], + rc =set_foreign_p2m_entry (currd, d, gfn_list[i],_mfn (mfn_list[i]));/* rc should be -EIO for any iteration other than the first */ if ( rc && i )
xen/include/asm-arm/p2m.h +9 -10
@@ -161,6 +161,15 @@ typedef enum {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{ +/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is supported on Arm. +*/ +return true; +} +static inline void p2m_altp2m_check (struct vcpu *v,uint16_t idx) {
@@ -392,16 +401,6 @@ static inline gfn_t gfn_next_boundary(gfn_t gfn, unsigned int order)return gfn_add (gfn,1UL << order); } -static inline int set_foreign_p2m_entry (struct domain *d,unsigned long gfn, -mfn_t mfn) -{ -/* -* NOTE: If this is implemented then proper reference counting of -* foreign entries will need to be implemented. -*/ -return -EOPNOTSUPP ; -} -/* * A vCPU has cache enabled only when the MMU is enabled and data cache * is enabled.
xen/include/asm-x86/p2m.h +13 -3
@@ -382,6 +382,19 @@ struct p2m_domain {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{ +/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is not supported on x86. +* +* FIXME: Until foreign pages inserted into the P2M are properly +* reference counted, it is unsafe to allow mapping of +* resource pages unless the caller is the hardware domain. +*/ +return false; +} +/* * Updates vCPU's n2pm to match its np2m_base in VMCx12 and returns that np2m. */
@@ -647,9 +660,6 @@ int p2m_finish_type_change(struct domain *d,int p2m_is_logdirty_range (struct p2m_domain *,unsigned long start,unsigned long end); -/* Set foreign entry in the p2m table (for priv-mapping) */ -int set_foreign_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn); -/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,gfn_t gfn,mfn_t mfn,unsigned int order);
xen/include/xen/p2m-common.h +4 -0
@@ -3,6 +3,10 @@#include <xen/mm.h> +/* Set foreign entry in the p2m table */ +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn); +/* Remove a page from a domain's p2m table */ int __must_check guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,
Signature · 1 line
2.7.4
Re: [PATCH V3 16/23] xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm
Quoted 5 lines · Oleksandr Tyshchenko
xen/common/memory.c +0
-0@@ -1134,12 +1134,8 @@ static int acquire_resource(xen_pfn_t mfn_list[32 ];int rc;
xen/common/memory.c static int acquire_resource( · line 1139
Quoted 10 lines
xen/common/memory.c +2
-6-/* -* FIXME: Until foreign pages inserted into the P2M are properly -* reference counted, it is unsafe to allow mapping of -* resource pages unless the caller is the hardware domain. -*/ -if (paging_mode_translate (currd) && !is_hardware_domain (currd) ) +if (paging_mode_translate (currd) && !is_hardware_domain (currd) && + !arch_acquire_resource_check () )return -EACCES ;
Looks like I didn't express myself clearly enough when replying to v2, by saying "as both prior parts of the condition should be needed only on the x86 side, and there (for PV) there's no p2m involved in the refcounting". While one may debate whether the hwdom check may remain here, the "translated" one definitely should move into the x86 hook. This (I think) will the also make apparent that ...
Quoted 8 lines
xen/include/asm-x86/p2m.h +2
-0@@ -382,6 +382,19 @@ struct p2m_domain {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{
xen/include/asm-x86/p2m.h struct p2m_domain { · line 396
Quoted 10 lines
xen/include/asm-x86/p2m.h +10
-0+/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is not supported on x86. +* +* FIXME: Until foreign pages inserted into the P2M are properly +* reference counted, it is unsafe to allow mapping of +* resource pages unless the caller is the hardware domain. +*/ +return false; +}
... the initial part of the comment is true only for translated domains. The reference to hwdom in the latter part of the comment (which merely gets moved here) is a good indication that the hwdom check also wants moving here. In turn the check at the top of p2m_add_foreign() should imo then also use this new function, instead of effectively open-coding it (with a similar comment). And x86's set_foreign_p2m_entry() may want to gain
ASSERT(arch_acquire_resource_check(d));
perhaps alongside the same ASSERT() you add to the Arm variant.
Jan
Re: [PATCH V3 16/23] xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm
On 08.12.20 16:24, Jan Beulich wrote:
Hi Jan
Quoted 50 lines · Oleksandr Tyshchenko
Quoted 5 lines
xen/common/memory.c +0
-0@@ -1134,12 +1134,8 @@ static int acquire_resource(xen_pfn_t mfn_list[32 ];int rc;xen/common/memory.c static int acquire_resource( · line 1139
Quoted 10 lines
xen/common/memory.c +2
-6-/* -* FIXME: Until foreign pages inserted into the P2M are properly -* reference counted, it is unsafe to allow mapping of -* resource pages unless the caller is the hardware domain. -*/ -if (paging_mode_translate (currd) && !is_hardware_domain (currd) ) +if (paging_mode_translate (currd) && !is_hardware_domain (currd) && + !arch_acquire_resource_check () )return -EACCES ;Looks like I didn't express myself clearly enough when replying to v2, by saying "as both prior parts of the condition should be needed only on the x86 side, and there (for PV) there's no p2m involved in the refcounting". While one may debate whether the hwdom check may remain here, the "translated" one definitely should move into the x86 hook. This (I think) will the also make apparent that ...
Quoted 8 lines
xen/include/asm-x86/p2m.h +2
-0@@ -382,6 +382,19 @@ struct p2m_domain {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{xen/include/asm-x86/p2m.h struct p2m_domain { · line 396
Quoted 10 lines
xen/include/asm-x86/p2m.h +10
-0+/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is not supported on x86. +* +* FIXME: Until foreign pages inserted into the P2M are properly +* reference counted, it is unsafe to allow mapping of +* resource pages unless the caller is the hardware domain. +*/ +return false; +}... the initial part of the comment is true only for translated domains. The reference to hwdom in the latter part of the comment (which merely gets moved here) is a good indication that the hwdom check also wants moving here. In turn the check at the top of
p2m_add_foreign()should imo then also use this new function, instead of effectively open-coding it (with a similar comment). And x86'sset_foreign_p2m_entry()may want to gain
Quoted 3 lines
ASSERT(arch_acquire_resource_check(d));perhaps alongside the same
ASSERT()you add to the Arm variant.
Well, will do. I was about to mention, that new function wanted to gain domain as parameter, but noticed you had given a hint in the ASSERT example.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 16/23] xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm
In reply to Oleksandr TyshchenkoQuoted 20 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch implements reference counting of foreign entries in in
set_foreign_p2m_entry()on Arm. This is a mandatory action if we want to run emulator (IOREQ server) in other than dom0 domain, as we can't trust it to do the right thing if it is not running in dom0. So we need to grab a reference on the page to avoid it disappearing.It is valid to always pass "
p2m_map_foreign_rw" type toguest_physmap_add_entry()since the current and foreign domains would be always different. A case when they are equal would be rejected byrcu_lock_remote_domain_by_id(). Besides the similar comment in the code put a respectiveASSERT()to catch incorrect usage in future.It was tested with IOREQ feature to confirm that all the pages given to this function belong to a domain, so we can use the same approach as for
XENMAPSPACE_gmfn_foreignhandling inxenmem_add_to_physmap_one().
Quoted 7 lines
This involves adding an extra parameter for the foreign domain to
set_foreign_p2m_entry()and a helper to indicate whether the arch supports the reference counting of foreign entries and the restriction for the hardware domain in the common code can be skipped for it.Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>
The arm side looks OK to me
Quoted 209 lines
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- new patch, was split from:
"[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM features"
- rewrite a logic to handle properly reference inset_foreign_p2m_entry()instead of treating foreign entries asp2m_ram_rwChanges V1 -> V2:
- rebase according to the recent changes toacquire_resource()
- update patch description
- introducearch_refcounts_p2m()
- add an explanation whyp2m_map_foreign_rwis valid
- moveset_foreign_p2m_entry()top2m-common.h
- add const to new parameterChanges V2 -> V3:
- update patch description
- renamearch_refcounts_p2m()toarch_acquire_resource_check()
- move comment to x86’sarch_acquire_resource_check()
- return rc in Arm'sset_foreign_p2m_entry()
- put a respectiveASSERT()into Arm'sset_foreign_p2m_entry()
------ xen/arch/arm/p2m.c | 24 ++++++++++++++++++++++++ xen/arch/x86/mm/p2m.c | 5 +++-- xen/common/memory.c | 10 +++------- xen/include/asm-arm/p2m.h | 19 +++++++++---------- xen/include/asm-x86/p2m.h | 16 +++++++++++++--- xen/include/xen/p2m-common.h | 4 ++++ 6 files changed, 56 insertions(+), 22 deletions(-)xen/arch/arm/p2m.c +24
-0@@ -1380,6 +1380,30 @@ int guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,return p2m_remove_mapping (d, gfn, (1 << page_order), mfn); } +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn) +{ +struct page_info *page =mfn_to_page (mfn); +int rc; + +if ( !get_page (page, fd) ) +return -EINVAL ; + +/* +* It is valid to always use p2m_map_foreign_rw here as if this gets +* called then d != fd. A case when d == fd would be rejected by +* rcu_lock_remote_domain_by_id() earlier. Put a respective ASSERT() +* to catch incorrect usage in future. +*/ +ASSERT (d != fd); + + rc =guest_physmap_add_entry (d,_gfn (gfn), mfn,0 , p2m_map_foreign_rw); +if ( rc ) +put_page (page); + +return rc; +} +static struct page_info *p2m_allocate_root (void ) {struct page_info *page;xen/arch/x86/mm/p2m.c +3
-2@@ -1321,7 +1321,8 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn_l, }/* Set foreign mfn in the given guest's p2m table. */ -int set_foreign_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn) +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn) {return set_typed_p2m_entry (d, gfn, mfn,PAGE_ORDER_4K , p2m_map_foreign,p2m_get_hostp2m (d)->default_access);@@ -2621,7 +2622,7 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,* will update the m2p table which will result in mfn -> gpfn of dom0 * and not fgfn of domU. */ - rc =set_foreign_p2m_entry (tdom, gpfn, mfn); + rc =set_foreign_p2m_entry (tdom, fdom, gpfn, mfn);if ( rc ) gdprintk(XENLOG_WARNING, "set_foreign_p2m_entry failed. " "gpfn:%lx mfn:%lx fgfn:%lx td:%d fd:%d\n",xen/common/memory.c +3
-7@@ -1134,12 +1134,8 @@ static int acquire_resource(xen_pfn_t mfn_list[32 ];int rc; -/* -* FIXME: Until foreign pages inserted into the P2M are properly -* reference counted, it is unsafe to allow mapping of -* resource pages unless the caller is the hardware domain. -*/ -if (paging_mode_translate (currd) && !is_hardware_domain (currd) ) +if (paging_mode_translate (currd) && !is_hardware_domain (currd) && + !arch_acquire_resource_check () )return -EACCES ;if (copy_from_guest (&xmar, arg,1 ) )@@ -1207,7 +1203,7 @@ static int acquire_resource(for ( i =0 ; !rc && i < xmar.nr_frames; i++ ) { - rc =set_foreign_p2m_entry (currd, gfn_list[i], + rc =set_foreign_p2m_entry (currd, d, gfn_list[i],_mfn (mfn_list[i]));/* rc should be -EIO for any iteration other than the first */ if ( rc && i )xen/include/asm-arm/p2m.h +9
-10@@ -161,6 +161,15 @@ typedef enum {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{ +/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is supported on Arm. +*/ +return true; +} +static inline void p2m_altp2m_check (struct vcpu *v,uint16_t idx) {@@ -392,16 +401,6 @@ static inline gfn_t gfn_next_boundary(gfn_t gfn, unsigned int order)return gfn_add (gfn,1UL << order); } -static inline int set_foreign_p2m_entry (struct domain *d,unsigned long gfn, -mfn_t mfn) -{ -/* -* NOTE: If this is implemented then proper reference counting of -* foreign entries will need to be implemented. -*/ -return -EOPNOTSUPP ; -} -/* * A vCPU has cache enabled only when the MMU is enabled and data cache * is enabled. xen/include/asm-x86/p2m.h +13
-3@@ -382,6 +382,19 @@ struct p2m_domain {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{ +/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is not supported on x86. +* +* FIXME: Until foreign pages inserted into the P2M are properly +* reference counted, it is unsafe to allow mapping of +* resource pages unless the caller is the hardware domain. +*/ +return false; +} +/* * Updates vCPU's n2pm to match its np2m_base in VMCx12 and returns that np2m. */ @@ -647,9 +660,6 @@ int p2m_finish_type_change(struct domain *d,int p2m_is_logdirty_range (struct p2m_domain *,unsigned long start,unsigned long end); -/* Set foreign entry in the p2m table (for priv-mapping) */ -int set_foreign_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn); -/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,gfn_t gfn,mfn_t mfn,unsigned int order);xen/include/xen/p2m-common.h +4
-0@@ -3,6 +3,10 @@#include <xen/mm.h> +/* Set foreign entry in the p2m table */ +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn); +/* Remove a page from a domain's p2m table */ int __must_check guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,Signature · 1 line
2.7.4
Re: [PATCH V3 16/23] xen/mm: Handle properly reference in set_foreign_p2m_entry() on Arm
In reply to Oleksandr TyshchenkoQuoted 20 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
This patch implements reference counting of foreign entries in in
set_foreign_p2m_entry()on Arm. This is a mandatory action if we want to run emulator (IOREQ server) in other than dom0 domain, as we can't trust it to do the right thing if it is not running in dom0. So we need to grab a reference on the page to avoid it disappearing.It is valid to always pass "
p2m_map_foreign_rw" type toguest_physmap_add_entry()since the current and foreign domains would be always different. A case when they are equal would be rejected byrcu_lock_remote_domain_by_id(). Besides the similar comment in the code put a respectiveASSERT()to catch incorrect usage in future.It was tested with IOREQ feature to confirm that all the pages given to this function belong to a domain, so we can use the same approach as for
XENMAPSPACE_gmfn_foreignhandling inxenmem_add_to_physmap_one().
Quoted 7 lines
This involves adding an extra parameter for the foreign domain to
set_foreign_p2m_entry()and a helper to indicate whether the arch supports the reference counting of foreign entries and the restriction for the hardware domain in the common code can be skipped for it.Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>
Quoted 209 lines
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes RFC -> V1:
- new patch, was split from:
"[RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM features"
- rewrite a logic to handle properly reference inset_foreign_p2m_entry()instead of treating foreign entries asp2m_ram_rwChanges V1 -> V2:
- rebase according to the recent changes toacquire_resource()
- update patch description
- introducearch_refcounts_p2m()
- add an explanation whyp2m_map_foreign_rwis valid
- moveset_foreign_p2m_entry()top2m-common.h
- add const to new parameterChanges V2 -> V3:
- update patch description
- renamearch_refcounts_p2m()toarch_acquire_resource_check()
- move comment to x86’sarch_acquire_resource_check()
- return rc in Arm'sset_foreign_p2m_entry()
- put a respectiveASSERT()into Arm'sset_foreign_p2m_entry()
------ xen/arch/arm/p2m.c | 24 ++++++++++++++++++++++++ xen/arch/x86/mm/p2m.c | 5 +++-- xen/common/memory.c | 10 +++------- xen/include/asm-arm/p2m.h | 19 +++++++++---------- xen/include/asm-x86/p2m.h | 16 +++++++++++++--- xen/include/xen/p2m-common.h | 4 ++++ 6 files changed, 56 insertions(+), 22 deletions(-)xen/arch/arm/p2m.c +24
-0@@ -1380,6 +1380,30 @@ int guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,return p2m_remove_mapping (d, gfn, (1 << page_order), mfn); } +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn) +{ +struct page_info *page =mfn_to_page (mfn); +int rc; + +if ( !get_page (page, fd) ) +return -EINVAL ; + +/* +* It is valid to always use p2m_map_foreign_rw here as if this gets +* called then d != fd. A case when d == fd would be rejected by +* rcu_lock_remote_domain_by_id() earlier. Put a respective ASSERT() +* to catch incorrect usage in future. +*/ +ASSERT (d != fd); + + rc =guest_physmap_add_entry (d,_gfn (gfn), mfn,0 , p2m_map_foreign_rw); +if ( rc ) +put_page (page); + +return rc; +} +static struct page_info *p2m_allocate_root (void ) {struct page_info *page;xen/arch/x86/mm/p2m.c +3
-2@@ -1321,7 +1321,8 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn_l, }/* Set foreign mfn in the given guest's p2m table. */ -int set_foreign_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn) +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn) {return set_typed_p2m_entry (d, gfn, mfn,PAGE_ORDER_4K , p2m_map_foreign,p2m_get_hostp2m (d)->default_access);@@ -2621,7 +2622,7 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,* will update the m2p table which will result in mfn -> gpfn of dom0 * and not fgfn of domU. */ - rc =set_foreign_p2m_entry (tdom, gpfn, mfn); + rc =set_foreign_p2m_entry (tdom, fdom, gpfn, mfn);if ( rc ) gdprintk(XENLOG_WARNING, "set_foreign_p2m_entry failed. " "gpfn:%lx mfn:%lx fgfn:%lx td:%d fd:%d\n",xen/common/memory.c +3
-7@@ -1134,12 +1134,8 @@ static int acquire_resource(xen_pfn_t mfn_list[32 ];int rc; -/* -* FIXME: Until foreign pages inserted into the P2M are properly -* reference counted, it is unsafe to allow mapping of -* resource pages unless the caller is the hardware domain. -*/ -if (paging_mode_translate (currd) && !is_hardware_domain (currd) ) +if (paging_mode_translate (currd) && !is_hardware_domain (currd) && + !arch_acquire_resource_check () )return -EACCES ;if (copy_from_guest (&xmar, arg,1 ) )@@ -1207,7 +1203,7 @@ static int acquire_resource(for ( i =0 ; !rc && i < xmar.nr_frames; i++ ) { - rc =set_foreign_p2m_entry (currd, gfn_list[i], + rc =set_foreign_p2m_entry (currd, d, gfn_list[i],_mfn (mfn_list[i]));/* rc should be -EIO for any iteration other than the first */ if ( rc && i )xen/include/asm-arm/p2m.h +9
-10@@ -161,6 +161,15 @@ typedef enum {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{ +/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is supported on Arm. +*/ +return true; +} +static inline void p2m_altp2m_check (struct vcpu *v,uint16_t idx) {@@ -392,16 +401,6 @@ static inline gfn_t gfn_next_boundary(gfn_t gfn, unsigned int order)return gfn_add (gfn,1UL << order); } -static inline int set_foreign_p2m_entry (struct domain *d,unsigned long gfn, -mfn_t mfn) -{ -/* -* NOTE: If this is implemented then proper reference counting of -* foreign entries will need to be implemented. -*/ -return -EOPNOTSUPP ; -} -/* * A vCPU has cache enabled only when the MMU is enabled and data cache * is enabled. xen/include/asm-x86/p2m.h +13
-3@@ -382,6 +382,19 @@ struct p2m_domain {#endif #include <xen/p2m-common.h> +static inline bool arch_acquire_resource_check (void ) +{ +/* +* The reference counting of foreign entries in set_foreign_p2m_entry() +* is not supported on x86. +* +* FIXME: Until foreign pages inserted into the P2M are properly +* reference counted, it is unsafe to allow mapping of +* resource pages unless the caller is the hardware domain. +*/ +return false; +} +/* * Updates vCPU's n2pm to match its np2m_base in VMCx12 and returns that np2m. */ @@ -647,9 +660,6 @@ int p2m_finish_type_change(struct domain *d,int p2m_is_logdirty_range (struct p2m_domain *,unsigned long start,unsigned long end); -/* Set foreign entry in the p2m table (for priv-mapping) */ -int set_foreign_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn); -/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,gfn_t gfn,mfn_t mfn,unsigned int order);xen/include/xen/p2m-common.h +4
-0@@ -3,6 +3,10 @@#include <xen/mm.h> +/* Set foreign entry in the p2m table */ +int set_foreign_p2m_entry (struct domain *d,const struct domain *fd, +unsigned long gfn,mfn_t mfn); +/* Remove a page from a domain's p2m table */ int __must_check guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,Signature · 1 line
2.7.4
PATCH v3 17/23 xen/ioreq: Introduce domain_has_ioreq_server()
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
This patch introduces a helper the main purpose of which is to check if a domain is using IOREQ server(s).
On Arm the current benefit is to avoid calling vcpu_ioreq_handle_completion() (which implies iterating over all possible IOREQ servers anyway) on every return in leave_hypervisor_to_guest() if there is no active servers for the particular domain.
Also this helper will be used by one of the subsequent patches on Arm.
This involves adding an extra per-domain variable to store the count of servers in use.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- new patch
Changes V1 -> V2:
- update patch description
- guard helper with CONFIG_IOREQ_SERVER
- remove "hvm" prefix
- modify helper to just return d->arch.hvm.ioreq_server.nr_servers
- put suitable ASSERT()s
- use ASSERT(d->ioreq_server.server[id] ? !s : !!s) in set_ioreq_server()
- remove d->ioreq_server.nr_servers = 0 from hvm_ioreq_init()
Changes V2 -> V3:
- update patch description
- remove ASSERT()s from the helper, add a comment
- use #ifdef CONFIG_IOREQ_SERVER inside function body
- use new ASSERT() construction in set_ioreq_server()
---
--- xen/arch/arm/traps.c | 15 +++++++++------ xen/common/ioreq.c | 7 ++++++- xen/include/xen/ioreq.h | 14 ++++++++++++++ xen/include/xen/sched.h | 1 + 4 files changed, 30 insertions(+), 7 deletions(-)
xen/arch/arm/traps.c +9 -6
@@ -2262,14 +2262,17 @@ static bool check_for_vcpu_work(void)struct vcpu *v = current;#ifdef CONFIG_IOREQ_SERVER -bool handled; +if (domain_has_ioreq_server (v->domain) ) + { +bool handled; -local_irq_enable (); - handled =vcpu_ioreq_handle_completion (v); -local_irq_disable (); +local_irq_enable (); + handled =vcpu_ioreq_handle_completion (v); +local_irq_disable (); -if ( !handled ) -return true; +if ( !handled ) +return true; + }#endif if (likely (!v->arch.need_flush_to_ram) )
xen/common/ioreq.c +6 -1
@@ -39,9 +39,14 @@ static void set_ioreq_server(struct domain *d, unsigned int id, struct ioreq_server *s) {ASSERT (id <MAX_NR_IOREQ_SERVERS ); -ASSERT (!s || !d->ioreq_server.server[id]); +ASSERT (!s ^ !d->ioreq_server.server[id]); d->ioreq_server.server[id] = s; + +if ( s ) + d->ioreq_server.nr_servers++; +else + d->ioreq_server.nr_servers--; }#define GET_IOREQ_SERVER (d, id) \
xen/include/xen/ioreq.h +14 -0
@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling; }; +/* +* This should only be used when d == current->domain and it's not paused, +* or when they're distinct and d is paused. Otherwise the result is +* stale before the caller can inspect it. +*/ +static inline bool domain_has_ioreq_server (const struct domain *d) +{ +#ifdef CONFIG_IOREQ_SERVER +return d->ioreq_server.nr_servers; +#else +return false; +#endif +} +static inline paddr_t ioreq_mmio_first_byte (const ioreq_t *p) { return unlikely(p->df) ?
xen/include/xen/sched.h +1 -0
@@ -550,6 +550,7 @@ struct domainstruct {spinlock_t lock;struct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; +unsigned int nr_servers; } ioreq_server;#endif };
Signature · 1 line
2.7.4
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 8 lines · Oleksandr Tyshchenko
xen/include/xen/ioreq.h +2
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling; }; +/* +* This should only be used when d == current->domain and it's not paused,
Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Jan
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 7 lines · Oleksandr Tyshchenko
Quoted 6 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling; };
Quoted 5 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 2 lines
xen/include/xen/ioreq.h +2
-0+/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare
that the current domain would be paused (if so, it's in the process
of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 5 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
We would like to avoid to do any extra actions in leave_hypervisor_to_guest() if possible.
But not only there, the logic whether we check/set mapcache_invalidation variable could be avoided if a domain doesn't use IOREQ server...
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
Hi Paul.
Quoted 22 lines · Oleksandr
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 12 lines · Oleksandr Tyshchenko
Quoted 8 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server { uint8_t bufioreq_handling; };+/*
+ * This should only be used when d ==current->domainand it's not paused,Is the "not paused" part really relevant here? Besides it being rare
that the current domain would be paused (if so, it's in the process
of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actionsNo, it isn't relevant, I will drop it.
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...
Are you OK with this patch (common part of it)?
Signature · 3 lines
Regards, Oleksandr Tyshchenko
RE: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
Quoted 43 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 16:57
To: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>; Oleksandr Tyshchenko <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 27 lines · Oleksandr
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 5 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 8 lines
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
How much of a performance benefit is this? The array is small to simply counting the non-NULL entries should be pretty quick.
Paul
Quoted 6 lines
--
Regards,Oleksandr Tyshchenko
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
On 08.12.20 21:43, Paul Durrant wrote:
Hi Paul
Quoted 40 lines
Quoted 40 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 16:57
To: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>; Oleksandr Tyshchenko <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 24 lines · Oleksandr
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
How much of a performance benefit is this? The array is small to simply counting the non-NULL entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes.
In our system we run three domains. The emulator is in DomD only, so I would like to avoid to call vcpu_ioreq_handle_completion() for every Dom0/DomU's vCPUs if there is no real need to do it. On Arm vcpu_ioreq_handle_completion() is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable. These unneeded actions could be avoided by using this simple one-line helper...
Signature · 3 lines
Regards, Oleksandr Tyshchenko
RE: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
Quoted 62 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 20:17
To: [email protected]
Cc: 'Jan Beulich' <[email protected]>; 'Oleksandr Tyshchenko' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Julien Grall' <[email protected]>; 'Volodymyr Babchuk' <[email protected]>; 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Wei Liu' <[email protected]>; 'Julien Grall' <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()On 08.12.20 21:43, Paul Durrant wrote:
Hi Paul
Quoted 46 lines
Quoted 46 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 16:57
To: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>; Oleksandr Tyshchenko <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 29 lines · Oleksandr
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...
Quoted 8 lines
Quoted 2 lines
Quoted 1 line
Are you OK with this patch (common part of it)?
How much of a performance benefit is this? The array is small to simply counting the non-NULL
entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes. In our system we run three domains. The emulator is in DomD only, so I would like to avoid to callvcpu_ioreq_handle_completion()for every Dom0/DomU's vCPUs
if there is no real need to do it.
This is not relevant to the domain that the emulator is running in; it's concerning the domains which the emulator is servicing. How many of those are there?
Quoted 6 lines
On Arm
vcpu_ioreq_handle_completion()
is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable.
These unneeded actions could be avoided by using this simple one-line helper...
The helper may be one line but there is more to the patch than that. I still think you could just walk the array in the helper rather than keeping a running occupancy count.
Paul
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
Hi Oleksandr and Paul,
Sorry for jumping late in the conversation.
Quoted 64 lines · Paul Durrant
Quoted 64 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 20:17
To: [email protected]
Cc: 'Jan Beulich' <[email protected]>; 'Oleksandr Tyshchenko' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Julien Grall' <[email protected]>; 'Volodymyr Babchuk' <[email protected]>; 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Wei Liu' <[email protected]>; 'Julien Grall' <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()On 08.12.20 21:43, Paul Durrant wrote:
Hi Paul
Quoted 48 lines
Quoted 40 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 16:57
To: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>; Oleksandr Tyshchenko <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 24 lines · Oleksandr
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 7 lines
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
How much of a performance benefit is this? The array is small to simply counting the non-NULL
Quoted 8 lines
Quoted 6 lines
entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes. In our system we run three domains. The emulator is in DomD only, so I would like to avoid to callvcpu_ioreq_handle_completion()for every Dom0/DomU's vCPUs
if there is no real need to do it.This is not relevant to the domain that the emulator is running in; it's concerning the domains which the emulator is servicing. How many of those are there?
AFAICT, the maximum number of IOREQ servers is 8 today.
Quoted 9 lines
Quoted 7 lines
On Arm
vcpu_ioreq_handle_completion()
is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable.
These unneeded actions could be avoided by using this simple one-line helper...The helper may be one line but there is more to the patch than that. I still think you could just walk the array in the helper rather than keeping a running occupancy count.
Right, the concern here is this function will be called in an hotpath (everytime we are re-entering to the guest). At the difference of x86, the entry/exit code is really small, so any additional code will have an impact on the overall performance.
That said, the IOREQ code is a tech preview for Arm. So I would be fine going with Paul's approach until we have a better understanding on the performance of virtio/IOREQ.
I am going to throw some more thoughts about the optimization here. The patch is focusing on performance impact when IOREQ is built-in and not used. I think we can do further optimization (which may superseed this one).
get_pending_vcpu() (called from handle_hvm_io_completion()) is overly expensive in particular if you have no I/O forwarded to an IOREQ server. Entry to the hypervisor can happen for many reasons (interrupts, system registers emulation, I/O emulation...) and the I/O forwarded should be a small subset.
Ideally, handle_hvm_io_completion() should be a NOP (at max a few instructions) if there are nothing to do. Maybe we want to introduce a per-vCPU flag indicating if an I/O has been forwarded to an IOREQ server.
This would also us to bypass most of the function if there is nothing to do.
Any thoughts?
In any case this is more a forward looking rather than a request for the current series. What matters to me is we have a functional (not necessarily optimized) version of IOREQ in Xen 4.15. This would be a great step towards using Virto on Xen.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
Quoted 1 line · Julien Grall
Hi Oleksandr and Paul,
Hi Julien, Paul.
Quoted 88 lines
Sorry for jumping late in the conversation.
Quoted 65 lines · Paul Durrant
Quoted 65 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 20:17
To: [email protected]
Cc: 'Jan Beulich' <[email protected]>; 'Oleksandr Tyshchenko' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Julien Grall' <[email protected]>; 'Volodymyr Babchuk' <[email protected]>; 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Wei Liu' <[email protected]>; 'Julien Grall' <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()On 08.12.20 21:43, Paul Durrant wrote:
Hi Paul
Quoted 49 lines
Quoted 41 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 08 December 2020 16:57
To: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>; Oleksandr Tyshchenko <[email protected]>; Stefano Stabellini <[email protected]>; Julien Grall <[email protected]>; Volodymyr Babchuk <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Julien Grall <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 25 lines · Oleksandr
On 08.12.20 17:11, Jan Beulich wrote:
Hi Jan
Quoted 6 lines · Oleksandr Tyshchenko
Quoted 5 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server { uint8_t bufioreq_handling; };Quoted 8 lines
Quoted 4 lines
+/*
+ * This should only be used when d ==current->domainand it's not
paused,Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 7 lines
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
How much of a performance benefit is this? The array is small to simply counting the non-NULL
Quoted 8 lines
Quoted 6 lines
entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes. In our system we run three domains. The emulator is in DomD only, so I would like to avoid to callvcpu_ioreq_handle_completion()for every Dom0/DomU's vCPUs
if there is no real need to do it.This is not relevant to the domain that the emulator is running in; it's concerning the domains which the emulator is servicing. How many of those are there?
AFAICT, the maximum number of IOREQ servers is 8 today.
Quoted 8 lines
Quoted 7 lines
On Arm
vcpu_ioreq_handle_completion()
is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable.
These unneeded actions could be avoided by using this simple one-line helper...
Quoted 3 lines
Quoted 1 line
The helper may be one line but there is more to the patch than that. I still think you could just walk the array in the helper rather than keeping a running occupancy count.
Right, the concern here is this function will be called in an hotpath (everytime we are re-entering to the guest). At the difference of x86, the entry/exit code is really small, so any additional code will have an impact on the overall performance.
+1
Quoted 3 lines
That said, the IOREQ code is a tech preview for Arm. So I would be fine going with Paul's approach until we have a better understanding on the performance of virtio/IOREQ.
I am fine with Paul's approach for now (I only need a confirmation that I got it correctly).
Quoted 3 lines
I am going to throw some more thoughts about the optimization here. The patch is focusing on performance impact when IOREQ is built-in and not used.
It is true, what I would to add here is the helper also avoids unnecessary vcpu_ioreq_handle_completion() calls as well another unnecessary action (mapcache handling logic, although it is not a hotpath) in subsequent patch when IOREQ is used.
Quoted 11 lines
I think we can do further optimization (which may superseed this one).
get_pending_vcpu()(called fromhandle_hvm_io_completion()) is overly expensive in particular if you have no I/O forwarded to an IOREQ server. Entry to the hypervisor can happen for many reasons (interrupts, system registers emulation, I/O emulation...) and the I/O forwarded should be a small subset.Ideally,
handle_hvm_io_completion()should be a NOP (at max a few instructions) if there are nothing to do. Maybe we want to introduce a per-vCPU flag indicating if an I/O has been forwarded to an IOREQ server.This would also us to bypass most of the function if there is nothing to do.
Any thoughts?
In any case this is more a forward looking rather than a request for the current series. What matters to me is we have a functional (not necessarily optimized) version of IOREQ in Xen 4.15. This would be a great step towards using Virto on Xen.
Completely agree, current series is quite big) and if we will try to make it perfect I am afraid, we won't have it even in Xen 4.16). As for proposed optimization - I think it worth considering, I will mention about it in the cover letter for the series among other possible things such as buffered request, etc.
Quoted 4 lines
Cheers,
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
In reply to Paul DurrantHi Paul.
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 20 lines
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
Quoted 8 lines
Quoted 7 lines
Quoted 1 line
How much of a performance benefit is this? The array is small to simply counting the non-NULL
entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes. In our system we run three domains. The emulator is in DomD only, so I would like to avoid to callvcpu_ioreq_handle_completion()for every Dom0/DomU's vCPUs
if there is no real need to do it.This is not relevant to the domain that the emulator is running in; it's concerning the domains which the emulator is servicing. How many of those are there?
Err, yes, I wasn't precise when providing an example.
Single emulator is running in DomD and servicing DomU. So with the helper in place the vcpu_ioreq_handle_completion() gets only called for DomU vCPUs (as expected). Without an optimization the vcpu_ioreq_handle_completion() gets called for _all_ vCPUs, and I see it as an extra action for Dom0, DomD vCPUs.
Quoted 8 lines
Quoted 7 lines
On Arm
vcpu_ioreq_handle_completion()
is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable.
These unneeded actions could be avoided by using this simple one-line helper...The helper may be one line but there is more to the patch than that. I still think you could just walk the array in the helper rather than keeping a running occupancy count.
OK, is the implementation below close to what you propose? If yes, I will update a helper and drop nr_servers variable.
bool domain_has_ioreq_server(const struct domain *d) {
const struct ioreq_server *s;
unsigned int id;
FOR_EACH_IOREQ_SERVER(d, id, s)
return true;
return false;
}
Signature · 3 lines
Regards, Oleksandr Tyshchenko
RE: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
Quoted 72 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 09 December 2020 20:36
To: [email protected]
Cc: 'Jan Beulich' <[email protected]>; 'Oleksandr Tyshchenko' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Julien Grall' <[email protected]>; 'Volodymyr Babchuk' <[email protected]>; 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Wei Liu' <[email protected]>; 'Julien Grall' <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 20 lines
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
Quoted 8 lines
Quoted 7 lines
Quoted 1 line
How much of a performance benefit is this? The array is small to simply counting the non-NULL
entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes. In our system we run three domains. The emulator is in DomD only, so I would like to avoid to callvcpu_ioreq_handle_completion()for every Dom0/DomU's vCPUs
if there is no real need to do it.This is not relevant to the domain that the emulator is running in; it's concerning the domains
which the emulator is servicing. How many of those are there? Err, yes, I wasn't precise when providing an example.
Single emulator is running in DomD and servicing DomU. So with the helper in place thevcpu_ioreq_handle_completion()gets only called for DomU vCPUs (as expected).
Without an optimization thevcpu_ioreq_handle_completion()gets called for _all_ vCPUs, and I see it as an extra action for Dom0, DomD vCPUs.Quoted 8 lines
Quoted 7 lines
On Arm
vcpu_ioreq_handle_completion()
is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable.
These unneeded actions could be avoided by using this simple one-line helper...The helper may be one line but there is more to the patch than that. I still think you could just
walk the array in the helper rather than keeping a running occupancy count.
OK, is the implementation below close to what you propose? If yes, I will update a helper and drop
nr_serversvariable.bool
domain_has_ioreq_server(const struct domain *d)
Quoted 9 lines
{
const struct ioreq_server *s;
unsigned int id;
FOR_EACH_IOREQ_SERVER(d, id, s)
return true;
return false;
}Yes, that's what I had in mind.
Paul
Quoted 5 lines
--
Regards,Oleksandr Tyshchenko
Re: [PATCH V3 17/23] xen/ioreq: Introduce domain_has_ioreq_server()
On 10.12.20 10:38, Paul Durrant wrote:
Hi Paul.
Quoted 75 lines
Quoted 75 lines
-----Original Message-----
From: Oleksandr <[email protected]>
Sent: 09 December 2020 20:36
To: [email protected]
Cc: 'Jan Beulich' <[email protected]>; 'Oleksandr Tyshchenko' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Julien Grall' <[email protected]>; 'Volodymyr Babchuk' <[email protected]>; 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Wei Liu' <[email protected]>; 'Julien Grall' <[email protected]>; [email protected]
Subject: Re: [PATCH V3 17/23] xen/ioreq: Introducedomain_has_ioreq_server()Hi Paul.
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 20 lines
Quoted 5 lines · Oleksandr Tyshchenko
Quoted 4 lines
xen/include/xen/ioreq.h +0
-0@@ -55,6 +55,20 @@ struct ioreq_server {uint8_t bufioreq_handling;Quoted 8 lines
xen/include/xen/ioreq.h struct ioreq_server { · line 59
Quoted 4 lines
xen/include/xen/ioreq.h +1
-0}; +/* +* This should only be used when d == current->domain and it's not paused, Is the "not paused" part really relevant here? Besides it being rare that the current domain would be paused (if so, it's in the process of having all its vCPU-s scheduled out), does this matter at all?do any extra actionsdo any extra actions
No, it isn't relevant, I will drop it.
Quoted 4 lines
Apart from this the patch looks okay to me, but I'm not sure it addresses Paul's concerns. Iirc he had suggested to switch back to a list if doing a swipe over the entire array is too expensive in this specific case.
Quoted 5 lines
We would like to avoid to do any extra actions in
leave_hypervisor_to_guest()if possible.
But not only there, the logic whether we check/setmapcache_invalidationvariable could be avoided if a domain doesn't use IOREQ server...Are you OK with this patch (common part of it)?
Quoted 8 lines
Quoted 7 lines
Quoted 1 line
How much of a performance benefit is this? The array is small to simply counting the non-NULL
entries should be pretty quick.
I didn't perform performance measurements on how much this call consumes. In our system we run three domains. The emulator is in DomD only, so I would like to avoid to callvcpu_ioreq_handle_completion()for every Dom0/DomU's vCPUs
if there is no real need to do it.This is not relevant to the domain that the emulator is running in; it's concerning the domains
which the emulator is servicing. How many of those are there? Err, yes, I wasn't precise when providing an example.
Single emulator is running in DomD and servicing DomU. So with the helper in place thevcpu_ioreq_handle_completion()gets only called for DomU vCPUs (as expected).
Without an optimization thevcpu_ioreq_handle_completion()gets called for _all_ vCPUs, and I see it as an extra action for Dom0, DomD vCPUs.Quoted 7 lines
Quoted 6 lines
On Arm
vcpu_ioreq_handle_completion()
is called with IRQ enabled, so the call is accompanied with corresponding irq_enable/irq_disable.
These unneeded actions could be avoided by using this simple one-line helper...The helper may be one line but there is more to the patch than that. I still think you could just
walk the array in the helper rather than keeping a running occupancy count.
OK, is the implementation below close to what you propose? If yes, I will update a helper and drop
nr_serversvariable.bool
domain_has_ioreq_server(const struct domain *d){ const struct ioreq_server *s; unsigned int id;
Quoted 8 lines
Quoted 5 lines
FOR_EACH_IOREQ_SERVER(d, id, s) return true; return false; }Yes, that's what I had in mind.
Paul
Thank you for the clarification.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
PATCH v3 18/23 xen/dm: Introduce xendevicemodel_set_irq_level DM op
In reply to Oleksandr TyshchenkoFrom: Julien Grall <[email protected]>
This patch adds ability to the device emulator to notify otherend (some entity running in the guest) using a SPI and implements Arm specific bits for it. Proposed interface allows emulator to set the logical level of a one of a domain's IRQ lines.
We can't reuse the existing DM op (xen_dm_op_set_isa_irq_level) to inject an interrupt as the "isa_irq" field is only 8-bit and able to cover IRQ 0 - 255, whereas we need a wider range (0 - 1020).
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
***
Please note, I left interface untouched since there is still an open discussion what interface to use/what information to pass to the hypervisor. The question whether we should abstract away the state of the line or not.
***
Changes RFC -> V1:
- check incoming parameters in arch_dm_op()
- add explicit padding to struct xen_dm_op_set_irq_level
Changes V1 -> V2:
- update the author of a patch
- update patch description
- check that padding is always 0
- mention that interface is Arm only and only SPIs are supported for now
- allow to set the logical level of a line for non-allocated interrupts only
- add xen_dm_op_set_irq_level_t
Changes V2 -> V3:
- no changes
---
--- tools/include/xendevicemodel.h | 4 ++ tools/libs/devicemodel/core.c | 18 +++++++++ tools/libs/devicemodel/libxendevicemodel.map | 1 + xen/arch/arm/dm.c | 57 +++++++++++++++++++++++++++- xen/common/dm.c | 1 + xen/include/public/hvm/dm_op.h | 16 ++++++++ 6 files changed, 96 insertions(+), 1 deletion(-)
tools/include/xendevicemodel.h +4 -0
@@ -209,6 +209,10 @@ int xendevicemodel_set_isa_irq_level( xendevicemodel_handle *dmod, domid_t domid, uint8_t irq, unsigned int level); +int xendevicemodel_set_irq_level ( +xendevicemodel_handle *dmod,domid_t domid,unsigned int irq, +unsigned int level); +/** * This function maps a PCI INTx line to a an IRQ line. *
tools/libs/devicemodel/core.c +18 -0
@@ -430,6 +430,24 @@ int xendevicemodel_set_isa_irq_level(return xendevicemodel_op (dmod, domid,1 , &op,sizeof (op)); } +int xendevicemodel_set_irq_level ( +xendevicemodel_handle *dmod,domid_t domid,uint32_t irq, +unsigned int level) +{ +struct xen_dm_op op; +struct xen_dm_op_set_irq_level *data; + +memset (&op,0 ,sizeof (op)); + + op.op = XEN_DMOP_set_irq_level; + data = &op.u.set_irq_level; + + data->irq = irq; + data->level = level; + +return xendevicemodel_op (dmod, domid,1 , &op,sizeof (op)); +} +int xendevicemodel_set_pci_link_route (xendevicemodel_handle *dmod,domid_t domid,uint8_t link,uint8_t irq) {
tools/libs/devicemodel/libxendevicemodel.map +1 -0
@@ -32,6 +32,7 @@ VERS_1.2 { global: xendevicemodel_relocate_memory; xendevicemodel_pin_memory_cacheattr; + xendevicemodel_set_irq_level; } VERS_1.1; VERS_1.3 {
xen/arch/arm/dm.c +56 -1
66 lines not on this page · raw message
xen/common/dm.c +1 -0
@@ -48,6 +48,7 @@ static int dm_op(const struct dmop_args *op_args) [XEN_DMOP_remote_shutdown] = sizeof(struct xen_dm_op_remote_shutdown), [XEN_DMOP_relocate_memory] = sizeof(struct xen_dm_op_relocate_memory), [XEN_DMOP_pin_memory_cacheattr] = sizeof(struct xen_dm_op_pin_memory_cacheattr), + [XEN_DMOP_set_irq_level] =sizeof (struct xen_dm_op_set_irq_level ), }; rc =rcu_lock_remote_domain_by_id (op_args->domid, &d);
xen/include/public/hvm/dm_op.h +16 -0
@@ -434,6 +434,21 @@ struct xen_dm_op_pin_memory_cacheattr { };typedef struct xen_dm_op_pin_memory_cacheattr xen_dm_op_pin_memory_cacheattr_t ; +/* +* XEN_DMOP_set_irq_level: Set the logical level of a one of a domain's +* IRQ lines (currently Arm only). +* Only SPIs are supported. +*/ +#define XEN_DMOP_set_irq_level 19 + +struct xen_dm_op_set_irq_level { +uint32_t irq; +/* IN - Level: 0 -> deasserted, 1 -> asserted */ +uint8_t level; +uint8_t pad[3 ]; +}; +typedef struct xen_dm_op_set_irq_level xen_dm_op_set_irq_level_t ; +struct xen_dm_op {uint32_t op;uint32_t pad;
@@ -447,6 +462,7 @@ struct xen_dm_op {xen_dm_op_track_dirty_vram_t track_dirty_vram;xen_dm_op_set_pci_intx_level_t set_pci_intx_level;xen_dm_op_set_isa_irq_level_t set_isa_irq_level; +xen_dm_op_set_irq_level_t set_irq_level;xen_dm_op_set_pci_link_route_t set_pci_link_route;xen_dm_op_modified_memory_t modified_memory;xen_dm_op_set_mem_type_t set_mem_type;
Signature · 1 line
2.7.4
Re: [PATCH V3 18/23] xen/dm: Introduce xendevicemodel_set_irq_level DM op
Quoted 18 lines · Oleksandr Tyshchenko
From: Julien Grall <[email protected]>
This patch adds ability to the device emulator to notify otherend (some entity running in the guest) using a SPI and implements Arm specific bits for it. Proposed interface allows emulator to set the logical level of a one of a domain's IRQ lines.
We can't reuse the existing DM op (
xen_dm_op_set_isa_irq_level) to inject an interrupt as the "isa_irq" field is only 8-bit and able to cover IRQ 0 - 255, whereas we need a wider range (0 - 1020).Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Quoted 6 lines
***
Please note, I left interface untouched since there is still an open discussion what interface to use/what information to pass to the hypervisor. The question whether we should abstract away the state of the line or not.
***
Let's start with a simple question: is this going to work with virtio-mmio emulation in QEMU that doesn't lower the state of the line to end the notification (only calls qemu_set_irq(irq, high))?
See: hw/virtio/virtio-mmio.c:virtio_mmio_update_irq
Alex (CC'ed) might be able to confirm whether I am reading the QEMU code correctly. Assuming that it is true that QEMU is only raising the level, never lowering it, although the emulation is obviously not correct, I would rather keep QEMU as is for efficiency reasons, and because we don't want to deviate from the common implementation in QEMU.
Looking at this patch and at vgic_inject_irq, yes, I think it would work as is.
So it looks like we are going to end up with an interface that:
- in theory it is modelling the line closely
- in practice it is only called to "trigger the IRQ"
Hence my preference for being explicit about it and just call it trigger_irq.
If we keep the patch as is, should we at least add a comment to document the "QEMU style" use model?
Quoted 202 lines
Changes RFC -> V1:
- check incoming parameters inarch_dm_op()
- add explicit padding to structxen_dm_op_set_irq_levelChanges V1 -> V2:
- update the author of a patch
- update patch description
- check that padding is always 0
- mention that interface is Arm only and only SPIs are supported for now
- allow to set the logical level of a line for non-allocated interrupts only
- addxen_dm_op_set_irq_level_tChanges V2 -> V3:
- no changes
------ tools/include/xendevicemodel.h | 4 ++ tools/libs/devicemodel/core.c | 18 +++++++++ tools/libs/devicemodel/libxendevicemodel.map | 1 + xen/arch/arm/dm.c | 57 +++++++++++++++++++++++++++- xen/common/dm.c | 1 + xen/include/public/hvm/dm_op.h | 16 ++++++++ 6 files changed, 96 insertions(+), 1 deletion(-)tools/include/xendevicemodel.h +4
-0@@ -209,6 +209,10 @@ int xendevicemodel_set_isa_irq_level( xendevicemodel_handle *dmod, domid_t domid, uint8_t irq, unsigned int level); +int xendevicemodel_set_irq_level ( +xendevicemodel_handle *dmod,domid_t domid,unsigned int irq, +unsigned int level); +/** * This function maps a PCI INTx line to a an IRQ line. * tools/libs/devicemodel/core.c +18
-0@@ -430,6 +430,24 @@ int xendevicemodel_set_isa_irq_level(return xendevicemodel_op (dmod, domid,1 , &op,sizeof (op)); } +int xendevicemodel_set_irq_level ( +xendevicemodel_handle *dmod,domid_t domid,uint32_t irq, +unsigned int level) +{ +struct xen_dm_op op; +struct xen_dm_op_set_irq_level *data; + +memset (&op,0 ,sizeof (op)); + + op.op = XEN_DMOP_set_irq_level; + data = &op.u.set_irq_level; + + data->irq = irq; + data->level = level; + +return xendevicemodel_op (dmod, domid,1 , &op,sizeof (op)); +} +int xendevicemodel_set_pci_link_route (xendevicemodel_handle *dmod,domid_t domid,uint8_t link,uint8_t irq) {tools/libs/devicemodel/libxendevicemodel.map +1
-0@@ -32,6 +32,7 @@ VERS_1.2 { global: xendevicemodel_relocate_memory; xendevicemodel_pin_memory_cacheattr; + xendevicemodel_set_irq_level; } VERS_1.1; VERS_1.3 {xen/arch/arm/dm.c +56
-1@@ -17,10 +17,65 @@#include <xen/dm.h> #include <xen/hypercall.h> +#include <asm/vgic.h> +int arch_dm_op (struct xen_dm_op *op,struct domain *d,const struct dmop_args *op_args,bool *const_op) { -return -EOPNOTSUPP ; +int rc; + +switch ( op->op ) + { +case XEN_DMOP_set_irq_level: + { +const struct xen_dm_op_set_irq_level *data = + &op->u.set_irq_level; +unsigned int i; + +/* Only SPIs are supported */ +if ( (data->irq <NR_LOCAL_IRQS ) || (data->irq >=vgic_num_irqs (d)) ) + { + rc = -EINVAL ; +break ; + } + +if ( data->level !=0 && data->level !=1 ) + { + rc = -EINVAL ; +break ; + } + +/* Check that padding is always 0 */ +for ( i =0 ; i <sizeof (data->pad); i++ ) + { +if ( data->pad[i] ) + { + rc = -EINVAL ; +break ; + } + } + +/* +* Allow to set the logical level of a line for non-allocated +* interrupts only. +*/ +if (test_bit (data->irq, d->arch.vgic.allocated_irqs) ) + { + rc = -EINVAL ; +break ; + } + +vgic_inject_irq (d,NULL , data->irq, data->level); + rc =0 ; +break ; + } + +default : + rc = -EOPNOTSUPP ; +break ; + } + +return rc; }/* xen/common/dm.c +1
-0@@ -48,6 +48,7 @@ static int dm_op(const struct dmop_args *op_args) [XEN_DMOP_remote_shutdown] = sizeof(struct xen_dm_op_remote_shutdown), [XEN_DMOP_relocate_memory] = sizeof(struct xen_dm_op_relocate_memory), [XEN_DMOP_pin_memory_cacheattr] = sizeof(struct xen_dm_op_pin_memory_cacheattr), + [XEN_DMOP_set_irq_level] =sizeof (struct xen_dm_op_set_irq_level ), }; rc =rcu_lock_remote_domain_by_id (op_args->domid, &d);xen/include/public/hvm/dm_op.h +16
-0@@ -434,6 +434,21 @@ struct xen_dm_op_pin_memory_cacheattr { };typedef struct xen_dm_op_pin_memory_cacheattr xen_dm_op_pin_memory_cacheattr_t ; +/* +* XEN_DMOP_set_irq_level: Set the logical level of a one of a domain's +* IRQ lines (currently Arm only). +* Only SPIs are supported. +*/ +#define XEN_DMOP_set_irq_level 19 + +struct xen_dm_op_set_irq_level { +uint32_t irq; +/* IN - Level: 0 -> deasserted, 1 -> asserted */ +uint8_t level; +uint8_t pad[3 ]; +}; +typedef struct xen_dm_op_set_irq_level xen_dm_op_set_irq_level_t ; +struct xen_dm_op {uint32_t op;uint32_t pad;@@ -447,6 +462,7 @@ struct xen_dm_op {xen_dm_op_track_dirty_vram_t track_dirty_vram;xen_dm_op_set_pci_intx_level_t set_pci_intx_level;xen_dm_op_set_isa_irq_level_t set_isa_irq_level; +xen_dm_op_set_irq_level_t set_irq_level;xen_dm_op_set_pci_link_route_t set_pci_link_route;xen_dm_op_modified_memory_t modified_memory;xen_dm_op_set_mem_type_t set_mem_type;Signature · 1 line
2.7.4
Re: [PATCH V3 18/23] xen/dm: Introduce xendevicemodel_set_irq_level DM op
On 10.12.20 04:21, Stefano Stabellini wrote:
Hi Stefano
Quoted 39 lines · Oleksandr Tyshchenko
Quoted 18 lines
From: Julien Grall <[email protected]>
This patch adds ability to the device emulator to notify otherend (some entity running in the guest) using a SPI and implements Arm specific bits for it. Proposed interface allows emulator to set the logical level of a one of a domain's IRQ lines.
We can't reuse the existing DM op (
xen_dm_op_set_isa_irq_level) to inject an interrupt as the "isa_irq" field is only 8-bit and able to cover IRQ 0 - 255, whereas we need a wider range (0 - 1020).Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Quoted 6 lines
***
Please note, I left interface untouched since there is still an open discussion what interface to use/what information to pass to the hypervisor. The question whether we should abstract away the state of the line or not.
***Let's start with a simple question: is this going to work with virtio-mmio emulation in QEMU that doesn't lower the state of the line to end the notification (only calls
qemu_set_irq(irq, high))?See: hw/virtio/virtio-mmio.c:virtio_mmio_update_irq
Alex (CC'ed) might be able to confirm whether I am reading the QEMU code correctly. Assuming that it is true that QEMU is only raising the level, never lowering it, although the emulation is obviously not correct, I would rather keep QEMU as is for efficiency reasons, and because we don't want to deviate from the common implementation in QEMU.
Quoted 2 lines
Looking at this patch and at
vgic_inject_irq, yes, I think it would work as is.
Not sure whether QEMU lowers the level or not, but in virtio-disk backend example we don't set level to 0. IIRC there was a discussion about that from which I took that "setting level to 0 still does nothing on Arm if IRQ edge triggered". So, looks like, yes, it would work as is.
Quoted 9 lines
So it looks like we are going to end up with an interface that:
- in theory it is modelling the line closely
- in practice it is only called to "trigger the IRQ"Hence my preference for being explicit about it and just call it
trigger_irq.
I got it, just rename with retaining the level parameter?
Quoted 3 lines
If we keep the patch as is, should we at least add a comment to document the "QEMU style" use model?
Sure, I will describe that QEMU is only raising the level and never lowering it, if I have a confirmation this is true.
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 18/23] xen/dm: Introduce xendevicemodel_set_irq_level DM op
In reply to Stefano StabelliniHi Stefano,
Quoted 26 lines · Stefano Stabellini
Quoted 18 lines · Oleksandr Tyshchenko
From: Julien Grall <[email protected]>
This patch adds ability to the device emulator to notify otherend (some entity running in the guest) using a SPI and implements Arm specific bits for it. Proposed interface allows emulator to set the logical level of a one of a domain's IRQ lines.
We can't reuse the existing DM op (
xen_dm_op_set_isa_irq_level) to inject an interrupt as the "isa_irq" field is only 8-bit and able to cover IRQ 0 - 255, whereas we need a wider range (0 - 1020).Signed-off-by Julien Grall <[email protected]>Signed-off-by Oleksandr Tyshchenko <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Quoted 6 lines
***
Please note, I left interface untouched since there is still an open discussion what interface to use/what information to pass to the hypervisor. The question whether we should abstract away the state of the line or not.
***
Quoted 5 lines
Let's start with a simple question: is this going to work with virtio-mmio emulation in QEMU that doesn't lower the state of the line to end the notification (only calls
qemu_set_irq(irq, high))?See: hw/virtio/virtio-mmio.c:virtio_mmio_update_irq
Hmmm my version of QEMU is using:
level = (qatomic_read(&vdev->isr) != 0);trace_virtio_mmio_setting_irq(level);qemu_set_irq(proxy->irq, level);
So QEMU will raise/lower the interrupt based on whether there are pending still interrupts.
Quoted 11 lines
Alex (CC'ed) might be able to confirm whether I am reading the QEMU code correctly. Assuming that it is true that QEMU is only raising the level, never lowering it, although the emulation is obviously not correct, I would rather keep QEMU as is for efficiency reasons, and because we don't want to deviate from the common implementation in QEMU.
Looking at this patch and at
vgic_inject_irq, yes, I think it would work as is.
Our implementation of vgic_inject_irq() is completely bogus as soon as you deal with level interrupt. We are getting away so far, because there are not many fully emulated level interrupt (AFAIK this would only be the pl011). In fact, we carry a gross hack in the emulation to handle them.
In case of the level interrupt, we should keep injecting the interrupt to the guest until the line was lowered down (e.g qemu_set_irq(irq, 0) assuming active-high).
Quoted 5 lines
So it looks like we are going to end up with an interface that:
- in theory it is modelling the line closely
For level interrupt we need to know whether the line is low or high. I am struggling to see how this would work if we consider the variable as "trigger".
Cheers,
Signature · 1 line
Julien Grall
PATCH v3 19/23 xen/arm: io: Abstract sign-extension
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
In order to avoid code duplication (both handle_read() and handle_ioserv() contain the same code for the sign-extension) put this code to a common helper to be used for both.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V1 -> V2:
- new patch
Changes V2 -> V3:
- no changes
---
--- xen/arch/arm/io.c | 18 ++---------------- xen/arch/arm/ioreq.c | 17 +---------------- xen/include/asm-arm/traps.h | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 32 deletions(-)
xen/arch/arm/io.c +2 -16
@@ -23,6 +23,7 @@#include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/traps.h> #include <asm/hvm/ioreq.h> #include "decode.h"
@@ -39,26 +40,11 @@ static enum io_state handle_read(const struct mmio_handler *handler,* setting r). */ register_t r =0 ; -uint8_t size = (1 << dabt.size) *8 ;if ( !handler->ops->read (v, info, &r, handler->priv) )return IO_ABORT ; -/* -* Sign extend if required. -* Note that we expect the read handler to have zeroed the bits -* outside the requested access size. -*/ -if ( dabt.sign && (r & (1UL << (size -1 ))) ) - { -/* -* We are relying on register_t using the same as -* an unsigned long in order to keep the 32-bit assembly -* code smaller. -*/ -BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); - r |= (~0UL ) << size; - } + r =sign_extend (dabt, r);set_user_reg (regs, dabt.reg, r);
xen/arch/arm/ioreq.c +1 -16
@@ -28,7 +28,6 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v)const union hsr hsr = { .bits = regs->hsr };const struct hsr_dabt dabt = hsr.dabt;/* Code is similar to handle_read */ -uint8_t size = (1 << dabt.size) *8 ;register_t r = v->io.req.data;/* We are done with the IO */
@@ -37,21 +36,7 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v)if ( dabt.write )return IO_HANDLED ; -/* -* Sign extend if required. -* Note that we expect the read handler to have zeroed the bits -* outside the requested access size. -*/ -if ( dabt.sign && (r & (1UL << (size -1 ))) ) - { -/* -* We are relying on register_t using the same as -* an unsigned long in order to keep the 32-bit assembly -* code smaller. -*/ -BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); - r |= (~0UL ) << size; - } + r =sign_extend (dabt, r);set_user_reg (regs, dabt.reg, r);
xen/include/asm-arm/traps.h +24 -0
@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long)abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ +uint8_t size = (1 << dabt.size) *8 ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. +*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + { +/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size; + } + +return r; +} +#endif /* __ASM_ARM_TRAPS__ */ /* * Local variables:
Signature · 1 line
2.7.4
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Hi,
Quoted 117 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
In order to avoid code duplication (both
handle_read()andhandle_ioserv()contain the same code for the sign-extension) put this code to a common helper to be used for both.Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patchChanges V2 -> V3:
- no changes
------ xen/arch/arm/io.c | 18 ++---------------- xen/arch/arm/ioreq.c | 17 +---------------- xen/include/asm-arm/traps.h | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 32 deletions(-)xen/arch/arm/io.c +2
-16@@ -23,6 +23,7 @@#include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/traps.h> #include <asm/hvm/ioreq.h> #include "decode.h" @@ -39,26 +40,11 @@ static enum io_state handle_read(const struct mmio_handler *handler,* setting r). */ register_t r =0 ; -uint8_t size = (1 << dabt.size) *8 ;if ( !handler->ops->read (v, info, &r, handler->priv) )return IO_ABORT ; -/* -* Sign extend if required. -* Note that we expect the read handler to have zeroed the bits -* outside the requested access size. -*/ -if ( dabt.sign && (r & (1UL << (size -1 ))) ) - { -/* -* We are relying on register_t using the same as -* an unsigned long in order to keep the 32-bit assembly -* code smaller. -*/ -BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); - r |= (~0UL ) << size; - } + r =sign_extend (dabt, r);set_user_reg (regs, dabt.reg, r);xen/arch/arm/ioreq.c +1
-16@@ -28,7 +28,6 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v)const union hsr hsr = { .bits = regs->hsr };const struct hsr_dabt dabt = hsr.dabt;/* Code is similar to handle_read */ -uint8_t size = (1 << dabt.size) *8 ;register_t r = v->io.req.data;/* We are done with the IO */ @@ -37,21 +36,7 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v)if ( dabt.write )return IO_HANDLED ; -/* -* Sign extend if required. -* Note that we expect the read handler to have zeroed the bits -* outside the requested access size. -*/ -if ( dabt.sign && (r & (1UL << (size -1 ))) ) - { -/* -* We are relying on register_t using the same as -* an unsigned long in order to keep the 32-bit assembly -* code smaller. -*/ -BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); - r |= (~0UL ) << size; - } + r =sign_extend (dabt, r);set_user_reg (regs, dabt.reg, r);xen/include/asm-arm/traps.h +9
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long )abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ +uint8_t size = (1 << dabt.size) *8 ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size.
xen/include/asm-arm/traps.h static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) · line 104
Quoted 10 lines
xen/include/asm-arm/traps.h +10
-0+*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + { +/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size;
If size is 64, you will get undefined behavior there.
Quoted 8 lines
xen/include/asm-arm/traps.h +5
-0+ } + +return r; +} +#endif /* __ASM_ARM_TRAPS__ */ /* * Local variables:
Signature · 1 line
Volodymyr Babchuk at EPAM
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Quoted 1 line · Volodymyr Babchuk
Hi,
Hi Volodymyr
Quoted 123 lines
Quoted 120 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
In order to avoid code duplication (both
handle_read()andhandle_ioserv()contain the same code for the sign-extension) put this code to a common helper to be used for both.Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patchChanges V2 -> V3:
- no changes
------ xen/arch/arm/io.c | 18 ++---------------- xen/arch/arm/ioreq.c | 17 +---------------- xen/include/asm-arm/traps.h | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 32 deletions(-)xen/arch/arm/io.c +2
-16@@ -23,6 +23,7 @@#include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/traps.h> #include <asm/hvm/ioreq.h> #include "decode.h" @@ -39,26 +40,11 @@ static enum io_state handle_read(const struct mmio_handler *handler,* setting r). */ register_t r =0 ; -uint8_t size = (1 << dabt.size) *8 ;if ( !handler->ops->read (v, info, &r, handler->priv) )return IO_ABORT ; -/* -* Sign extend if required. -* Note that we expect the read handler to have zeroed the bits -* outside the requested access size. -*/ -if ( dabt.sign && (r & (1UL << (size -1 ))) ) - { -/* -* We are relying on register_t using the same as -* an unsigned long in order to keep the 32-bit assembly -* code smaller. -*/ -BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); - r |= (~0UL ) << size; - } + r =sign_extend (dabt, r);set_user_reg (regs, dabt.reg, r);xen/arch/arm/ioreq.c +1
-16@@ -28,7 +28,6 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v)const union hsr hsr = { .bits = regs->hsr };const struct hsr_dabt dabt = hsr.dabt;/* Code is similar to handle_read */ -uint8_t size = (1 << dabt.size) *8 ;register_t r = v->io.req.data;/* We are done with the IO */ @@ -37,21 +36,7 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v)if ( dabt.write )return IO_HANDLED ; -/* -* Sign extend if required. -* Note that we expect the read handler to have zeroed the bits -* outside the requested access size. -*/ -if ( dabt.sign && (r & (1UL << (size -1 ))) ) - { -/* -* We are relying on register_t using the same as -* an unsigned long in order to keep the 32-bit assembly -* code smaller. -*/ -BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); - r |= (~0UL ) << size; - } + r =sign_extend (dabt, r);set_user_reg (regs, dabt.reg, r);xen/include/asm-arm/traps.h +12
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long )abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ +uint8_t size = (1 << dabt.size) *8 ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. +*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + {
Quoted 8 lines
xen/include/asm-arm/traps.h static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) · line 104
Quoted 7 lines
xen/include/asm-arm/traps.h +7
-0+/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size;If
sizeis 64, you will get undefined behavior there.
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Quoted 28 lines · Oleksandr
Quoted 19 lines · Volodymyr Babchuk
Quoted 18 lines · Oleksandr Tyshchenko
xen/include/asm-arm/traps.h +12
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long )abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ +uint8_t size = (1 << dabt.size) *8 ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. +*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + {Quoted 8 lines
xen/include/asm-arm/traps.h static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) · line 104
Quoted 7 lines
xen/include/asm-arm/traps.h +7
-0+/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size;If
sizeis 64, you will get undefined behavior there.
Quoted 5 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
At which point please put in a respective ASSERT(), possibly amended by a brief comment.
Jan
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Hi Jan,
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Oleksandr
Quoted 27 lines · Volodymyr Babchuk
Quoted 15 lines · Oleksandr Tyshchenko
xen/include/asm-arm/traps.h +9
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long )abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ +uint8_t size = (1 << dabt.size) *8 ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. xen/include/asm-arm/traps.h static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) · line 104
Quoted 10 lines
xen/include/asm-arm/traps.h +10
-0+*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + { +/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size;If
sizeis 64, you will get undefined behavior there.
Quoted 8 lines
Quoted 5 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
At which point please put in a respective
ASSERT(), possibly amended by a brief comment.
ASSERT()s are only meant to catch programatic error. However, in this case, the bigger risk is an hardware bug such as advertising a sign extension for either 64-bit (or 32-bit) on Arm64 (resp. Arm32).
Actually the Armv8 spec is a bit more blurry when running in AArch32 state because they suggest that the sign extension can be set even for 32-bit access. I think this is a spelling mistake, but it is probably better to be cautious here.
Therefore, I would recommend to rework the code so it is only called when len < sizeof(register_t).
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
On 01.12.20 12:30, Julien Grall wrote:
Hi Julien
Quoted 39 lines
Hi Jan,
Quoted 36 lines · Jan Beulich
Quoted 34 lines · Oleksandr
Quoted 20 lines · Volodymyr Babchuk
Quoted 19 lines · Oleksandr Tyshchenko
xen/include/asm-arm/traps.h +0
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long)abort_guest_exit_end == regs->pc; }+/* Check whether the sign extension is required and perform it */ +static inline
register_tsign_extend(const struct hsr_dabt dabt, register_t r)xen/include/asm-arm/traps.h +10
-0+{ + uint8_t size = (1 << dabt.size) * 8; + + /* + * Sign extend if required. + * Note that we expect the read handler to have zeroed the bits + * outside the requested access size. + */ + if ( dabt.sign && (r & (1UL << (size - 1))) ) + {Quoted 8 lines
Quoted 7 lines
+ /*
+ * We are relying onregister_tusing the same as + * an unsigned long in order to keep the 32-bit assembly + * code smaller.
+ */
+BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); + r |= (~0UL) << size;If
sizeis 64, you will get undefined behavior there.I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
Quoted 8 lines
Quoted 2 lines
At which point please put in a respective
ASSERT(), possibly amended by a brief comment.ASSERT()s are only meant to catch programatic error. However, in this case, the bigger risk is an hardware bug such as advertising a sign extension for either 64-bit (or 32-bit) on Arm64 (resp. Arm32).
Actually the Armv8 spec is a bit more blurry when running in AArch32 state because they suggest that the sign extension can be set even for 32-bit access. I think this is a spelling mistake, but it is probably better to be cautious here.
Therefore, I would recommend to rework the code so it is only called when len <
sizeof(register_t).
I am not sure I understand the recommendation, could you please clarify (also I don't see 'len' being used here).
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Hi Oleksandr,
Quoted 47 lines · Oleksandr
On 01.12.20 12:30, Julien Grall wrote:
Hi Julien
Quoted 42 lines
Hi Jan,
Quoted 38 lines · Jan Beulich
Quoted 29 lines · Oleksandr
Quoted 20 lines · Volodymyr Babchuk
Quoted 19 lines · Oleksandr Tyshchenko
xen/include/asm-arm/traps.h +0
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long)abort_guest_exit_end == regs->pc; }+/* Check whether the sign extension is required and perform it */ +static inline
register_tsign_extend(const struct hsr_dabt dabt, register_t r)xen/include/asm-arm/traps.h +10
-0+{ + uint8_t size = (1 << dabt.size) * 8; + + /* + * Sign extend if required. + * Note that we expect the read handler to have zeroed the bits + * outside the requested access size. + */ + if ( dabt.sign && (r & (1UL << (size - 1))) ) + {Quoted 8 lines
Quoted 7 lines
+ /*
+ * We are relying onregister_tusing the same as + * an unsigned long in order to keep the 32-bit assembly + * code smaller.
+ */
+BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); + r |= (~0UL) << size;If
sizeis 64, you will get undefined behavior there.Quoted 5 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
At which point please put in a respective
ASSERT(), possibly amended by a brief comment.
Quoted 7 lines
Quoted 5 lines
ASSERT()s are only meant to catch programatic error. However, in this case, the bigger risk is an hardware bug such as advertising a sign extension for either 64-bit (or 32-bit) on Arm64 (resp. Arm32).
Actually the Armv8 spec is a bit more blurry when running in AArch32 state because they suggest that the sign extension can be set even for 32-bit access. I think this is a spelling mistake, but it is probably better to be cautious here.
Therefore, I would recommend to rework the code so it is only called when len <
sizeof(register_t).I am not sure I understand the recommendation, could you please clarify (also I don't see 'len' being used here).
Sorry I meant 'size'. I think something like:
if ( dabt.sign && (size < sizeof(register_t)) &&
(r & (1UL << (size - 1)) )
{
}
Another posibility would be:
if ( dabt.sign && (size < sizeof(register_t)) )
{
/* find whether the sign bit is set and propagate it */
}
I have a slight preference for the latter as the "if" is easier to read.
In any case, I think this change should be done in a separate patch (I don't mint whether this is done after or before this one).
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Quoted 1 line · Julien Grall
Hi Oleksandr,
Hi Julien.
Quoted 55 lines
Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 29 lines
Quoted 21 lines
Quoted 21 lines
xen/include/asm-arm/traps.h +0
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long)abort_guest_exit_end == regs->pc; }+/* Check whether the sign extension is required and perform it */ +static inline
register_tsign_extend(const struct hsr_dabt dabt, register_t r)xen/include/asm-arm/traps.h +6
-0+{ + uint8_t size = (1 << dabt.size) * 8; + + /* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits
+ * outside the requested access size. + */
+ if ( dabt.sign && (r & (1UL << (size - 1))) ) + {Quoted 8 lines
Quoted 7 lines
+ /*
+ * We are relying onregister_tusing the same as + * an unsigned long in order to keep the 32-bit assembly + * code smaller.
+ */
+BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); + r |= (~0UL) << size;If
sizeis 64, you will get undefined behavior there.Quoted 5 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
At which point please put in a respective
ASSERT(), possibly amended by a brief comment.Quoted 7 lines
Quoted 5 lines
ASSERT()s are only meant to catch programatic error. However, in this case, the bigger risk is an hardware bug such as advertising a sign extension for either 64-bit (or 32-bit) on Arm64 (resp. Arm32).
Actually the Armv8 spec is a bit more blurry when running in AArch32 state because they suggest that the sign extension can be set even for 32-bit access. I think this is a spelling mistake, but it is probably better to be cautious here.
Therefore, I would recommend to rework the code so it is only called when len <
sizeof(register_t).I am not sure I understand the recommendation, could you please clarify (also I don't see 'len' being used here).
Sorry I meant 'size'. I think something like:
if ( dabt.sign && (size <
sizeof(register_t)) &&
(r & (1UL << (size - 1)) )
{
}Another posibility would be:
Quoted 8 lines
if ( dabt.sign && (size <
sizeof(register_t)) )
{
/* find whether the sign bit is set and propagate it */ }I have a slight preference for the latter as the "if" is easier to read.
In any case, I think this change should be done in a separate patch (I don't mint whether this is done after or before this one).
ok, I got it, thank you for the clarification. Of course, I will do that in a separate patch, since the current one is to avoid code duplication only. BTW, do you have comments on this patch itself?
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
Quoted 67 lines · Oleksandr
Quoted 1 line · Julien Grall
Hi Oleksandr,
Hi Julien.
Quoted 60 lines
Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 29 lines
Quoted 21 lines
Quoted 21 lines
xen/include/asm-arm/traps.h +0
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long)abort_guest_exit_end == regs->pc; }+/* Check whether the sign extension is required and perform it */ +static inline
register_tsign_extend(const struct hsr_dabt dabt, register_t r)xen/include/asm-arm/traps.h +6
-0+{ + uint8_t size = (1 << dabt.size) * 8; + + /* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits
+ * outside the requested access size. + */
+ if ( dabt.sign && (r & (1UL << (size - 1))) ) + {Quoted 8 lines
Quoted 7 lines
+ /*
+ * We are relying onregister_tusing the same as + * an unsigned long in order to keep the 32-bit assembly + * code smaller.
+ */
+BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); + r |= (~0UL) << size;If
sizeis 64, you will get undefined behavior there.Quoted 5 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
At which point please put in a respective
ASSERT(), possibly amended by a brief comment.Quoted 7 lines
Quoted 5 lines
ASSERT()s are only meant to catch programatic error. However, in this case, the bigger risk is an hardware bug such as advertising a sign extension for either 64-bit (or 32-bit) on Arm64 (resp. Arm32).
Actually the Armv8 spec is a bit more blurry when running in AArch32 state because they suggest that the sign extension can be set even for 32-bit access. I think this is a spelling mistake, but it is probably better to be cautious here.
Therefore, I would recommend to rework the code so it is only called when len <
sizeof(register_t).I am not sure I understand the recommendation, could you please clarify (also I don't see 'len' being used here).
Sorry I meant 'size'. I think something like:
if ( dabt.sign && (size <
sizeof(register_t)) &&
(r & (1UL << (size - 1)) )
{
}Another posibility would be:
if ( dabt.sign && (size <
sizeof(register_t)) )
{
/* find whether the sign bit is set and propagate it */ }
Quoted 5 lines
Quoted 3 lines
I have a slight preference for the latter as the "if" is easier to read.
In any case, I think this change should be done in a separate patch (I don't mint whether this is done after or before this one).
ok, I got it, thank you for the clarification. Of course, I will do that in a separate patch, since the current one is to avoid code duplication only. BTW, do you have comments on this patch itself?
The series is in my TODO list. I will have a look once in a bit :).
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
In reply to Julien GrallQuoted 45 lines · Julien Grall
Hi Jan,
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Oleksandr
Quoted 27 lines · Volodymyr Babchuk
Quoted 15 lines · Oleksandr Tyshchenko
xen/include/asm-arm/traps.h +9
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long )abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ +uint8_t size = (1 << dabt.size) *8 ; + +/* +* Sign extend if required. +* Note that we expect the read handler to have zeroed the bits +* outside the requested access size. xen/include/asm-arm/traps.h static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) · line 104
Quoted 10 lines
xen/include/asm-arm/traps.h +10
-0+*/ +if ( dabt.sign && (r & (1UL << (size -1 ))) ) + { +/* +* We are relying on register_t using the same as +* an unsigned long in order to keep the 32-bit assembly +* code smaller. +*/ +BUILD_BUG_ON (sizeof (register_t) !=sizeof (unsigned long )); + r |= (~0UL ) << size;If
sizeis 64, you will get undefined behavior there.Quoted 8 lines
Quoted 5 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
At which point please put in a respective
ASSERT(), possibly amended by a brief comment.ASSERT()s are only meant to catch programatic error. However, in this case, the bigger risk is an hardware bug such as advertising a sign extension for either 64-bit (or 32-bit) on Arm64 (resp. Arm32).
Quoted 7 lines
Actually the Armv8 spec is a bit more blurry when running in AArch32 state because they suggest that the sign extension can be set even for 32-bit access. I think this is a spelling mistake, but it is probably better to be cautious here.
Therefore, I would recommend to rework the code so it is only called when len <
sizeof(register_t).
This would be even better in this case, I agree.
Jan
Re: [PATCH V3 19/23] xen/arm: io: Abstract sign-extension
In reply to OleksandrQuoted 133 lines · Oleksandr
Quoted 1 line · Volodymyr Babchuk
Hi,
Hi Volodymyr
Quoted 118 lines
Quoted 115 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
In order to avoid code duplication (both
handle_read()andhandle_ioserv()contain the same code for the sign-extension) put this code to a common helper to be used for both.Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patchChanges V2 -> V3:
- no changes
---
---
xen/arch/arm/io.c | 18 ++---------------- xen/arch/arm/ioreq.c | 17 +----------------xen/include/asm-arm/traps.h| 24 ++++++++++++++++++++++++3 files changed, 27 insertions(+), 32 deletions(-)xen/arch/arm/io.c +2
-16@@ -23,6 +23,7 @@ #include <asm/cpuerrata.h> #include <asm/current.h> #include <asm/mmio.h> +#include <asm/traps.h> #include <asm/hvm/ioreq.h> #include "decode.h"@@ -39,26 +40,11 @@ static enum io_state handle_read(const structmmio_handler *handler, * setting r). */ register_t r = 0; - uint8_t size = (1 << dabt.size) * 8; if ( !handler->ops->read(v, info, &r, handler->priv) ) return IO_ABORT; - /* - * Sign extend if required. - * Note that we expect the read handler to have zeroed the bits - * outside the requested access size. - */ - if ( dabt.sign && (r & (1UL << (size - 1))) ) - { - /* - * We are relying on register_t using the same as - * an unsigned long in order to keep the 32-bit assembly - * code smaller. - */ - BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); - r |= (~0UL) << size; - } + r = sign_extend(dabt, r); set_user_reg(regs, dabt.reg, r);xen/arch/arm/ioreq.c +1
-16@@ -28,7 +28,6 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v) const union hsr hsr = { .bits = regs->hsr }; const struct hsr_dabt dabt = hsr.dabt; /* Code is similar to handle_read */ - uint8_t size = (1 << dabt.size) * 8; register_t r = v->io.req.data; /* We are done with the IO */@@ -37,21 +36,7 @@ enum io_state handle_ioserv(struct cpu_user_regs *regs, struct vcpu *v) if ( dabt.write ) return IO_HANDLED; - /* - * Sign extend if required. - * Note that we expect the read handler to have zeroed the bits - * outside the requested access size. - */ - if ( dabt.sign && (r & (1UL << (size - 1))) ) - { - /* - * We are relying on register_t using the same as - * an unsigned long in order to keep the 32-bit assembly - * code smaller. - */ - BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); - r |= (~0UL) << size; - } + r = sign_extend(dabt, r); set_user_reg(regs, dabt.reg, r);xen/include/asm-arm/traps.h +12
-0@@ -83,6 +83,30 @@ static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) (unsigned long)abort_guest_exit_end == regs->pc; } +/* Check whether the sign extension is required and perform it */ +static inline register_t sign_extend (const struct hsr_dabt dabt,register_t r) +{ + uint8_t size = (1 << dabt.size) * 8; + + /* + * Sign extend if required. + * Note that we expect the read handler to have zeroed the bits + * outside the requested access size. + */ + if ( dabt.sign && (r & (1UL << (size - 1))) ) + {Quoted 8 lines
xen/include/asm-arm/traps.h static inline bool VABORT_GEN_BY_GUEST(const struct cpu_user_regs *regs) · line 104
Quoted 7 lines
xen/include/asm-arm/traps.h +7
-0+ /* + * We are relying on register_t using the same as + * an unsigned long in order to keep the 32-bit assembly + * code smaller. + */ + BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long)); + r |= (~0UL) << size;If
sizeis 64, you will get undefined behavior there.
Quoted 2 lines
I think, we don't need to worry about undefined behavior here. Having size=64 would be possible with doubleword (dabt.size=3). But if "r" adjustment gets called (I mean Syndrome Sign Extend bit is set) then we deal with byte, halfword or word operations (dabt.size<3). Or I missed something?
This is known and was pointed out in the commit message introducing the sign-extension:
"Note that the bit can only be set for access size smaller than the register size (i.e byte/half-word for aarch32, byte/half-word/word for aarch32). So we don't have to worry about undefined C behavior."
Cheers,
Signature · 1 line
Julien Grall
PATCH v3 20/23 xen/ioreq: Make x86's send_invalidate_req() common
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
As the IOREQ is a common feature now and we also need to invalidate qemu/demu mapcache on Arm when the required condition occurs this patch moves this function to the common code (and remames it to ioreq_signal_mapcache_invalidate). This patch also moves per-domain qemu_mapcache_invalidate variable out of the arch sub-struct (and drops "qemu" prefix).
The subsequent patch will add mapcache invalidation handling on Arm.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes RFC -> V1:
- move send_invalidate_req() to the common code
- update patch subject/description
- move qemu_mapcache_invalidate out of the arch sub-struct, update checks
- remove #if defined(CONFIG_ARM64) from the common code
Changes V1 -> V2:
- was split into:
- xen/ioreq: Make x86's send_invalidate_req() common
- xen/arm: Add mapcache invalidation handling
- update patch description/subject
- move Arm bits to a separate patch
- don't alter the common code, the flag is set by arch code
- rename send_invalidate_req() to send_invalidate_ioreq()
- guard qemu_mapcache_invalidate with CONFIG_IOREQ_SERVER
- use bool instead of bool_t
- remove blank line blank line between head comment and #include-s
Changes V2 -> V3:
- update patch description
- drop "qemu" prefix from the variable name
- rename send_invalidate_req() to ioreq_signal_mapcache_invalidate()
---
--- xen/arch/x86/hvm/hypercall.c | 9 +++++---- xen/arch/x86/hvm/io.c | 14 -------------- xen/common/ioreq.c | 14 ++++++++++++++ xen/include/asm-x86/hvm/domain.h | 1 - xen/include/asm-x86/hvm/io.h | 1 - xen/include/xen/ioreq.h | 1 + xen/include/xen/sched.h | 2 ++ 7 files changed, 22 insertions(+), 20 deletions(-)
xen/arch/x86/hvm/hypercall.c +5 -4
@@ -20,6 +20,7 @@*/ #include <xen/lib.h> #include <xen/hypercall.h> +#include <xen/ioreq.h> #include <xen/nospec.h> #include <asm/hvm/emulate.h>
@@ -47,7 +48,7 @@ static long hvm_memory_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) rc =compat_memory_op (cmd, arg);if ( (cmd &MEMOP_CMD_MASK ) == XENMEM_decrease_reservation ) - curr->domain->arch.hvm.qemu_mapcache_invalidate = true; + curr->domain->mapcache_invalidate = true;return rc; }
@@ -326,9 +327,9 @@ int hvm_hypercall(struct cpu_user_regs *regs)HVM_DBG_LOG (DBG_LEVEL_HCALL ,"hcall%lu -> %lx" , eax, regs->rax); -if (unlikely (currd->arch.hvm.qemu_mapcache_invalidate) && -test_and_clear_bool (currd->arch.hvm.qemu_mapcache_invalidate) ) -send_invalidate_req (); +if (unlikely (currd->mapcache_invalidate) && +test_and_clear_bool (currd->mapcache_invalidate) ) +ioreq_signal_mapcache_invalidate ();return curr->hcall_preempted ? HVM_HCALL_preempted : HVM_HCALL_completed; }
xen/arch/x86/hvm/io.c +0 -14
@@ -64,20 +64,6 @@ void send_timeoffset_req(unsigned long timeoff)gprintk (XENLOG_ERR ,"Unsuccessful timeoffset update\n" ); } -/* Ask ioemu mapcache to invalidate mappings. */ -void send_invalidate_req (void ) -{ -ioreq_t p = { - .type =IOREQ_TYPE_INVALIDATE , - .size =4 , - .dir =IOREQ_WRITE , - .data = ~0UL ,/* flush all */ - }; - -if (ioreq_broadcast (&p, false) !=0 ) -gprintk (XENLOG_ERR ,"Unsuccessful map-cache invalidate\n" ); -} -bool hvm_emulate_one_insn (hvm_emulate_validate_t *validate,const char *descr) {struct hvm_emulate_ctxt ctxt;
xen/common/ioreq.c +14 -0
@@ -35,6 +35,20 @@#include <public/hvm/ioreq.h> #include <public/hvm/params.h> +/* Ask ioemu mapcache to invalidate mappings. */ +void ioreq_signal_mapcache_invalidate (void ) +{ +ioreq_t p = { + .type =IOREQ_TYPE_INVALIDATE , + .size =4 , + .dir =IOREQ_WRITE , + .data = ~0UL ,/* flush all */ + }; + +if (ioreq_broadcast (&p, false) !=0 ) +gprintk (XENLOG_ERR ,"Unsuccessful map-cache invalidate\n" ); +} +static void set_ioreq_server (struct domain *d,unsigned int id,struct ioreq_server *s) {
xen/include/asm-x86/hvm/domain.h +0 -1
@@ -122,7 +122,6 @@ struct hvm_domain {struct viridian_domain *viridian; -bool_t qemu_mapcache_invalidate;bool_t is_s3_suspended;/*
xen/include/asm-x86/hvm/io.h +0 -1
@@ -97,7 +97,6 @@ bool relocate_portio_handler( unsigned int size);void send_timeoffset_req (unsigned long timeoff); -void send_invalidate_req (void );bool handle_mmio_with_translation (unsigned long gla,unsigned long gpfn,struct npfec );bool handle_pio (uint16_t port,unsigned int size,int dir);
xen/include/xen/ioreq.h +1 -0
@@ -129,6 +129,7 @@ struct ioreq_server *ioreq_server_select(struct domain *d,int ioreq_send (struct ioreq_server *s,ioreq_t *proto_p,bool buffered);unsigned int ioreq_broadcast (ioreq_t *p,bool buffered); +void ioreq_signal_mapcache_invalidate (void );void ioreq_domain_init (struct domain *d);
xen/include/xen/sched.h +2 -0
@@ -552,6 +552,8 @@ struct domainstruct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; unsigned int nr_servers; } ioreq_server; + +bool mapcache_invalidate;#endif };
Signature · 1 line
2.7.4
Re: [PATCH V3 20/23] xen/ioreq: Make x86's send_invalidate_req() common
xen/include/xen/sched.h struct domain · line 558
Quoted 10 lines · Oleksandr Tyshchenko
xen/include/xen/sched.h +2
-0@@ -552,6 +552,8 @@ struct domainstruct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; unsigned int nr_servers; } ioreq_server; + +bool mapcache_invalidate;#endif };
While I can see reasons to put this inside the #ifdef here, I would suspect putting it in the hole next to the group of 5 bools further up would be more efficient.
Jan
Re: [PATCH V3 20/23] xen/ioreq: Make x86's send_invalidate_req() common
On 08.12.20 17:24, Jan Beulich wrote:
Hi Jan
Quoted 6 lines · Oleksandr Tyshchenko
Quoted 5 lines
xen/include/xen/sched.h +0
-0@@ -552,6 +552,8 @@ struct domainstruct ioreq_server *server[MAX_NR_IOREQ_SERVERS ];unsigned int nr_servers;
Quoted 8 lines
xen/include/xen/sched.h struct domain · line 558
Quoted 5 lines
xen/include/xen/sched.h +2
-0} ioreq_server; + +bool mapcache_invalidate;#endif };While I can see reasons to put this inside the #ifdef here, I would suspect putting it in the hole next to the group of 5 bools further up would be more efficient.
ok, will put (although it will increase the number of #ifdef)
Signature · 3 lines
Regards, Oleksandr Tyshchenko
Re: [PATCH V3 20/23] xen/ioreq: Make x86's send_invalidate_req() common
Quoted 16 lines · Oleksandr
Quoted 14 lines · Jan Beulich
xen/include/xen/sched.h struct domain · line 558
Quoted 10 lines · Oleksandr Tyshchenko
xen/include/xen/sched.h +2
-0@@ -552,6 +552,8 @@ struct domainstruct ioreq_server *server[MAX_NR_IOREQ_SERVERS ]; unsigned int nr_servers; } ioreq_server; + +bool mapcache_invalidate;#endif };While I can see reasons to put this inside the #ifdef here, I would suspect putting it in the hole next to the group of 5 bools further up would be more efficient.
Quoted 1 line
ok, will put (although it will increase the number of #ifdef)
I was implying no #ifdef in this case, suitably justified by half a sentence in the patch description.
Jan
PATCH v3 21/23 xen/arm: Add mapcache invalidation handling
In reply to Oleksandr TyshchenkoFrom: Oleksandr Tyshchenko <[email protected]>
We need to send mapcache invalidation request to qemu/demu everytime the page gets removed from a guest.
At the moment, the Arm code doesn't explicitely remove the existing mapping before inserting the new mapping. Instead, this is done implicitely by __p2m_set_entry().
So we need to recognize a case when old entry is a RAM page *and* the new MFN is different in order to set the corresponding flag. The most suitable place to do this is p2m_free_entry(), there we can find the correct leaf type. The invalidation request will be sent in do_trap_hypercall() later on.
---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"
Changes V1 -> V2:
- new patch, some changes were derived from (+ new explanation):
xen/ioreq: Make x86's invalidate qemu mapcache handling common
- put setting of the flag into __p2m_set_entry()
- clarify the conditions when the flag should be set
- use domain_has_ioreq_server()
- update do_trap_hypercall() by adding local variable
Changes V2 -> V3:
- update patch description
- move check to p2m_free_entry()
- add a comment
- use "curr" instead of "v" in do_trap_hypercall()
---
--- xen/arch/arm/p2m.c | 24 ++++++++++++++++-------- xen/arch/arm/traps.c | 13 ++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-)
xen/arch/arm/p2m.c +16 -8
@@ -1,6 +1,7 @@#include <xen/cpu.h> #include <xen/domain_page.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/sched.h> #include <xen/softirq.h>
@@ -749,17 +750,24 @@ static void p2m_free_entry(struct p2m_domain *p2m,if ( !p2m_is_valid (entry) )return ; -/* Nothing to do but updating the stats if the entry is a super-page. */ -if (p2m_is_superpage (entry, level) ) +if (p2m_is_superpage (entry, level) || (level ==3 ) ) { - p2m->stats.mappings[level]--; -return ; - } +#ifdef CONFIG_IOREQ_SERVER +/* +* If this gets called (non-recursively) then either the entry +* was replaced by an entry with a different base (valid case) or +* the shattering of a superpage was failed (error case). +* So, at worst, the spurious mapcache invalidation might be sent. +*/ +if (domain_has_ioreq_server (p2m->domain) && + (p2m->domain == current->domain) &&p2m_is_ram (entry.p2m.type) ) + p2m->domain->mapcache_invalidate = true; +#endif -if ( level ==3 ) - { p2m->stats.mappings[level]--; -p2m_put_l3_page (entry); +/* Nothing to do if the entry is a super-page. */ +if ( level ==3 ) +p2m_put_l3_page (entry);return ; }
xen/arch/arm/traps.c +10 -3
@@ -1443,6 +1443,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, const union hsr hsr) {arm_hypercall_fn_t call =NULL ; +struct vcpu *curr = current;BUILD_BUG_ON (NR_hypercalls <ARRAY_SIZE (arm_hypercall_table) );
@@ -1459,7 +1460,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,return ; } - current->hcall_preempted = false; + curr->hcall_preempted = false;perfc_incra (hypercalls, *nr); call = arm_hypercall_table[*nr].fn;
@@ -1472,7 +1473,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,HYPERCALL_RESULT_REG (regs) =call (HYPERCALL_ARGS (regs));#ifndef NDEBUG -if ( !current->hcall_preempted ) +if ( !curr->hcall_preempted ) {/* Deliberately corrupt parameter regs used by this hypercall. */ switch ( arm_hypercall_table[*nr].nr_args ) {
@@ -1489,8 +1490,14 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,#endif /* Ensure the hypercall trap instruction is re-executed. */ -if ( current->hcall_preempted ) +if ( curr->hcall_preempted ) regs->pc -=4 ;/* re-execute 'hvc #XEN_HYPERCALL_TAG' */ + +#ifdef CONFIG_IOREQ_SERVER +if (unlikely (curr->domain->mapcache_invalidate) && +test_and_clear_bool (curr->domain->mapcache_invalidate) ) +ioreq_signal_mapcache_invalidate (); +#endif }void arch_hypercall_tasklet_result (struct vcpu *v,long res)
Signature · 1 line
2.7.4
Re: [PATCH V3 21/23] xen/arm: Add mapcache invalidation handling
Quoted 9 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
We need to send mapcache invalidation request to qemu/demu everytime the page gets removed from a guest.
At the moment, the Arm code doesn't explicitely remove the existing mapping before inserting the new mapping. Instead, this is done implicitely by
__p2m_set_entry().
Quoted 5 lines
So we need to recognize a case when old entry is a RAM page *and* the new MFN is different in order to set the corresponding flag. The most suitable place to do this is
p2m_free_entry(), there we can find the correct leaf type. The invalidation request will be sent indo_trap_hypercall()later on.
Why is it sent in do_trap_hypercall() ?
Quoted 49 lines
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patch, some changes were derived from (+ new explanation):
xen/ioreq: Make x86's invalidate qemu mapcache handling common
- put setting of the flag into__p2m_set_entry()
- clarify the conditions when the flag should be set
- usedomain_has_ioreq_server()
- updatedo_trap_hypercall()by adding local variableChanges V2 -> V3:
- update patch description
- move check top2m_free_entry()
- add a comment
- use "curr" instead of "v" indo_trap_hypercall()
------ xen/arch/arm/p2m.c | 24 ++++++++++++++++-------- xen/arch/arm/traps.c | 13 ++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-)xen/arch/arm/p2m.c +2
-5@@ -1,6 +1,7 @@#include <xen/cpu.h> #include <xen/domain_page.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/sched.h> #include <xen/softirq.h> @@ -749,17 +750,24 @@ static void p2m_free_entry(struct p2m_domain *p2m,if ( !p2m_is_valid (entry) )return ; -/* Nothing to do but updating the stats if the entry is a super-page. */ -if (p2m_is_superpage (entry, level) ) +if (p2m_is_superpage (entry, level) || (level ==3 ) ) { - p2m->stats.mappings[level]--; -return ; - }
xen/arch/arm/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 764
Quoted 10 lines
xen/arch/arm/p2m.c +10
-0+#ifdef CONFIG_IOREQ_SERVER +/* +* If this gets called (non-recursively) then either the entry +* was replaced by an entry with a different base (valid case) or +* the shattering of a superpage was failed (error case). +* So, at worst, the spurious mapcache invalidation might be sent. +*/ +if (domain_has_ioreq_server (p2m->domain) && + (p2m->domain == current->domain) &&p2m_is_ram (entry.p2m.type) ) + p2m->domain->mapcache_invalidate = true;
Why the (p2m->domain == current->domain) check? Shouldn't we set mapcache_invalidate to true anyway? What happens if p2m->domain != current->domain? We wouldn't want the domain to lose the mapcache_invalidate notification.
Quoted 17 lines
xen/arch/arm/p2m.c +4
-3+#endif -if ( level ==3 ) - { p2m->stats.mappings[level]--; -p2m_put_l3_page (entry); +/* Nothing to do if the entry is a super-page. */ +if ( level ==3 ) +p2m_put_l3_page (entry);return ; }xen/arch/arm/traps.c +0
-0
xen/arch/arm/traps.c static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, · line 1446
Quoted 4 lines
xen/arch/arm/traps.c +1
-0@@ -1443,6 +1443,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, const union hsr hsr) {arm_hypercall_fn_t call =NULL ; +struct vcpu *curr = current;
Is this just to save 3 characters?
Quoted 22 lines
xen/arch/arm/traps.c +2
-2BUILD_BUG_ON (NR_hypercalls <ARRAY_SIZE (arm_hypercall_table) );@@ -1459,7 +1460,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,return ; } - current->hcall_preempted = false; + curr->hcall_preempted = false;perfc_incra (hypercalls, *nr); call = arm_hypercall_table[*nr].fn;@@ -1472,7 +1473,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,HYPERCALL_RESULT_REG (regs) =call (HYPERCALL_ARGS (regs));#ifndef NDEBUG -if ( !current->hcall_preempted ) +if ( !curr->hcall_preempted ) {/* Deliberately corrupt parameter regs used by this hypercall. */ switch ( arm_hypercall_table[*nr].nr_args ) {@@ -1489,8 +1490,14 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,#endif
xen/arch/arm/traps.c static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, · line 1499
Quoted 10 lines
xen/arch/arm/traps.c +6
-1/* Ensure the hypercall trap instruction is re-executed. */ -if ( current->hcall_preempted ) +if ( curr->hcall_preempted ) regs->pc -=4 ;/* re-execute 'hvc #XEN_HYPERCALL_TAG' */ + +#ifdef CONFIG_IOREQ_SERVER +if (unlikely (curr->domain->mapcache_invalidate) && +test_and_clear_bool (curr->domain->mapcache_invalidate) ) +ioreq_signal_mapcache_invalidate ();
Why not just:
if ( unlikely(test_and_clear_bool(curr->domain->mapcache_invalidate)) )
ioreq_signal_mapcache_invalidate();
Re: [PATCH V3 21/23] xen/arm: Add mapcache invalidation handling
Hi Stefano,
Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
We need to send mapcache invalidation request to qemu/demu everytime the page gets removed from a guest.
At the moment, the Arm code doesn't explicitely remove the existing mapping before inserting the new mapping. Instead, this is done implicitely by
__p2m_set_entry().
Quoted 7 lines
Quoted 5 lines
So we need to recognize a case when old entry is a RAM page *and* the new MFN is different in order to set the corresponding flag. The most suitable place to do this is
p2m_free_entry(), there we can find the correct leaf type. The invalidation request will be sent indo_trap_hypercall()later on.Why is it sent in
do_trap_hypercall()?
I believe this is following the approach used by x86. There are actually some discussion about it (see [1]).
Leaving aside the toolstack case for now, AFAIK, the only way a guest can modify its p2m is via an hypercall. Do you have an example otherwise?
When sending the invalidation request, the vCPU will be blocked until all the IOREQ server have acknowledged the invalidation. So the hypercall seems to be the best position to do it.
Alternatively, we could use check_for_vcpu_work() to check if the mapcache needs to be invalidated. The inconvenience is we would execute a few more instructions in each entry/exit path.
Quoted 58 lines
Quoted 58 lines
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patch, some changes were derived from (+ new explanation):
xen/ioreq: Make x86's invalidate qemu mapcache handling common
- put setting of the flag into__p2m_set_entry()
- clarify the conditions when the flag should be set
- usedomain_has_ioreq_server()
- updatedo_trap_hypercall()by adding local variableChanges V2 -> V3:
- update patch description
- move check top2m_free_entry()
- add a comment
- use "curr" instead of "v" indo_trap_hypercall()
------ xen/arch/arm/p2m.c | 24 ++++++++++++++++-------- xen/arch/arm/traps.c | 13 ++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-)xen/arch/arm/p2m.c +9
-5@@ -1,6 +1,7 @@#include <xen/cpu.h> #include <xen/domain_page.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/sched.h> #include <xen/softirq.h> @@ -749,17 +750,24 @@ static void p2m_free_entry(struct p2m_domain *p2m,if ( !p2m_is_valid (entry) )return ; -/* Nothing to do but updating the stats if the entry is a super-page. */ -if (p2m_is_superpage (entry, level) ) +if (p2m_is_superpage (entry, level) || (level ==3 ) ) { - p2m->stats.mappings[level]--; -return ; - } +#ifdef CONFIG_IOREQ_SERVER +/* +* If this gets called (non-recursively) then either the entry +* was replaced by an entry with a different base (valid case) or +* the shattering of a superpage was failed (error case). +* So, at worst, the spurious mapcache invalidation might be sent. +*/
Quoted 8 lines
xen/arch/arm/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 764
Quoted 3 lines
xen/arch/arm/p2m.c +3
-0+if (domain_has_ioreq_server (p2m->domain) && + (p2m->domain == current->domain) &&p2m_is_ram (entry.p2m.type) ) + p2m->domain->mapcache_invalidate = true;Why the (
p2m->domain==current->domain) check? Shouldn't we setmapcache_invalidateto true anyway? What happens ifp2m->domain!=current->domain? We wouldn't want the domain to lose themapcache_invalidatenotification.
This is also discussed in [1]. :) The main question is why would a toolstack/device model modify the guest memory after boot?
If we assume it does, then the device model would need to pause the domain before modifying the RAM.
We also need to make sure that all the IOREQ servers have invalidated the mapcache before the domain run again.
This would require quite a bit of work. I am not sure the effort is worth if there are no active users today.
Quoted 14 lines
Quoted 14 lines
xen/arch/arm/p2m.c +4
-3+#endif -if ( level ==3 ) - { p2m->stats.mappings[level]--; -p2m_put_l3_page (entry); +/* Nothing to do if the entry is a super-page. */ +if ( level ==3 ) +p2m_put_l3_page (entry);return ; }
Quoted 11 lines
xen/arch/arm/traps.c static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, · line 1446
Quoted 9 lines
xen/arch/arm/traps.c +1
-0@@ -1443,6 +1443,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, const union hsr hsr) {arm_hypercall_fn_t call =NULL ; +struct vcpu *curr = current;Is this just to save 3 characters?
Because current is not cheap to read and the compiler cannot optimize it (we obfuscate it as this is a per-cpu variable). So we commonly store 'current' in a local variable if there are multiple use.
Quoted 37 lines
Quoted 24 lines
xen/arch/arm/traps.c +2
-2BUILD_BUG_ON (NR_hypercalls <ARRAY_SIZE (arm_hypercall_table) );@@ -1459,7 +1460,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,return ; } - current->hcall_preempted = false; + curr->hcall_preempted = false;perfc_incra (hypercalls, *nr); call = arm_hypercall_table[*nr].fn;@@ -1472,7 +1473,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,HYPERCALL_RESULT_REG (regs) =call (HYPERCALL_ARGS (regs));#ifndef NDEBUG -if ( !current->hcall_preempted ) +if ( !curr->hcall_preempted ) {/* Deliberately corrupt parameter regs used by this hypercall. */ switch ( arm_hypercall_table[*nr].nr_args ) {@@ -1489,8 +1490,14 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,#endif xen/arch/arm/traps.c static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, · line 1499
Quoted 10 lines
xen/arch/arm/traps.c +6
-1/* Ensure the hypercall trap instruction is re-executed. */ -if ( current->hcall_preempted ) +if ( curr->hcall_preempted ) regs->pc -=4 ;/* re-execute 'hvc #XEN_HYPERCALL_TAG' */ + +#ifdef CONFIG_IOREQ_SERVER +if (unlikely (curr->domain->mapcache_invalidate) && +test_and_clear_bool (curr->domain->mapcache_invalidate) ) +ioreq_signal_mapcache_invalidate ();Why not just:
Quoted 3 lines
if ( unlikely(test_and_clear_bool(curr->domain->mapcache_invalidate)) )
ioreq_signal_mapcache_invalidate();
This seems to match the x86 code. My guess is they tried to prevent the cost of the atomic operation if there is no chance mapcache_invalidate is true.
I am split whether the first check is worth it. The atomic operation should be uncontended most of the time, so it should be quick. But it will always be slower than just a read because there is always a store involved.
On a related topic, Jan pointed out that the invalidation would not work properly if you have multiple vCPU modifying the P2M at the same time.
Cheers,
[1] https://lore.kernel.org/xen-devel/[email protected]/
Signature · 1 line
Julien Grall
Re: [PATCH V3 21/23] xen/arm: Add mapcache invalidation handling
Quoted 17 lines · Julien Grall
Quoted 16 lines · Stefano Stabellini
Quoted 14 lines · Oleksandr Tyshchenko
From: Oleksandr Tyshchenko <[email protected]>
We need to send mapcache invalidation request to qemu/demu everytime the page gets removed from a guest.
At the moment, the Arm code doesn't explicitely remove the existing mapping before inserting the new mapping. Instead, this is done implicitely by
__p2m_set_entry().So we need to recognize a case when old entry is a RAM page *and* the new MFN is different in order to set the corresponding flag. The most suitable place to do this is
p2m_free_entry(), there we can find the correct leaf type. The invalidation request will be sent indo_trap_hypercall()later on.
Quoted 7 lines
Quoted 1 line
Why is it sent in
do_trap_hypercall()?I believe this is following the approach used by x86. There are actually some discussion about it (see [1]).
Leaving aside the toolstack case for now, AFAIK, the only way a guest can modify its p2m is via an hypercall. Do you have an example otherwise?
OK this is a very important assumption. We should write it down for sure. I think it is true today on ARM.
Quoted 7 lines
When sending the invalidation request, the vCPU will be blocked until all the IOREQ server have acknowledged the invalidation. So the hypercall seems to be the best position to do it.
Alternatively, we could use
check_for_vcpu_work()to check if the mapcache needs to be invalidated. The inconvenience is we would execute a few more instructions in each entry/exit path.
Yeah it would be more natural to call it from check_for_vcpu_work(). If we put it between #ifdef CONFIG_IOREQ_SERVER it wouldn't be bad. But I am not a fan of increasing the instructions on the exit path either. From this point of view, putting it at the end of do_trap_hypercall is a nice trick actually. Let's just make sure it has a good comment on top.
Quoted 70 lines
Quoted 58 lines
Quoted 58 lines
Signed-off-by Oleksandr Tyshchenko <[email protected]>CC Julien Grall <[email protected]>---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"Changes V1 -> V2:
- new patch, some changes were derived from (+ new explanation):
xen/ioreq: Make x86's invalidate qemu mapcache handling common
- put setting of the flag into__p2m_set_entry()
- clarify the conditions when the flag should be set
- usedomain_has_ioreq_server()
- updatedo_trap_hypercall()by adding local variableChanges V2 -> V3:
- update patch description
- move check top2m_free_entry()
- add a comment
- use "curr" instead of "v" indo_trap_hypercall()
------ xen/arch/arm/p2m.c | 24 ++++++++++++++++-------- xen/arch/arm/traps.c | 13 ++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-)xen/arch/arm/p2m.c +1
-0@@ -1,6 +1,7 @@#include <xen/cpu.h> #include <xen/domain_page.h> #include <xen/iocap.h> +#include <xen/ioreq.h> #include <xen/lib.h> #include <xen/sched.h> #include <xen/softirq.h> @@ -749,17 +750,24 @@ static void p2m_free_entry(struct p2m_domain *p2m,if ( !p2m_is_valid (entry) )return ; - /* Nothing to do but updating the stats if the entry is asuper-page. */
xen/arch/arm/p2m.c +7
-4-if (p2m_is_superpage (entry, level) ) +if (p2m_is_superpage (entry, level) || (level ==3 ) ) { - p2m->stats.mappings[level]--; -return ; - } +#ifdef CONFIG_IOREQ_SERVER +/* +* If this gets called (non-recursively) then either the entry +* was replaced by an entry with a different base (valid case) or +* the shattering of a superpage was failed (error case). +* So, at worst, the spurious mapcache invalidation might be sent.
xen/arch/arm/p2m.c +2
-0+*/ + if ( domain_has_ioreq_server(p2m->domain) &&Quoted 8 lines
Quoted 3 lines
+ (
p2m->domain==current->domain) &&p2m_is_ram(entry.p2m.type))
+p2m->domain->mapcache_invalidate= true;Why the (
p2m->domain==current->domain) check? Shouldn't we setmapcache_invalidateto true anyway? What happens ifp2m->domain!=current->domain? We wouldn't want the domain to lose themapcache_invalidatenotification.This is also discussed in [1]. :) The main question is why would a toolstack/device model modify the guest memory after boot?
Quoted 8 lines
If we assume it does, then the device model would need to pause the domain before modifying the RAM.
We also need to make sure that all the IOREQ servers have invalidated the mapcache before the domain run again.
This would require quite a bit of work. I am not sure the effort is worth if there are no active users today.
OK, that explains why we think p2m->domain == current->domain, but why do we need to have a check for it right here?
In other words, we don't think it is realistc to get here with p2m->domain != current->domain, but let's say that we do somehow. What's the best course of action? Probably, set mapcache_invalidate to true and possibly print a warning?
Leaving mapcache_invalidate to false doesn't seem to be what we want to do?
Quoted 38 lines
Quoted 34 lines
Quoted 21 lines
BUILD_BUG_ON(NR_hypercalls < ARRAY_SIZE(arm_hypercall_table) ); @@ -1459,7 +1460,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr, return; }-
current->hcall_preempted= false;xen/arch/arm/p2m.c +2
-1+ curr->hcall_preempted = false;perfc_incra (hypercalls, *nr); call = arm_hypercall_table[*nr].fn;@@ -1472,7 +1473,7 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,HYPERCALL_RESULT_REG (regs) =call (HYPERCALL_ARGS (regs));#ifndef NDEBUG -if ( !current->hcall_preempted ) +if ( !curr->hcall_preempted ) {/* Deliberately corrupt parameter regs used by this hypercall. */ switch ( arm_hypercall_table[*nr].nr_args ) {xen/arch/arm/p2m.c +0
-0@@ -1489,8 +1490,14 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,xen/arch/arm/p2m.c static void do_trap_hypercall(struct cpu_user_regs · line 1499
Quoted 10 lines
xen/arch/arm/p2m.c +6
-1#endif /* Ensure the hypercall trap instruction is re-executed. */ -if ( current->hcall_preempted ) +if ( curr->hcall_preempted ) regs->pc -=4 ;/* re-execute 'hvc #XEN_HYPERCALL_TAG' */ + +#ifdef CONFIG_IOREQ_SERVER +if (unlikely (curr->domain->mapcache_invalidate) && +test_and_clear_bool (curr->domain->mapcache_invalidate) ) +ioreq_signal_mapcache_invalidate ();Why not just:
Quoted 3 lines
if ( unlikely(test_and_clear_bool(curr->domain->mapcache_invalidate)) )
ioreq_signal_mapcache_invalidate();
Quoted 6 lines
This seems to match the x86 code. My guess is they tried to prevent the cost of the atomic operation if there is no chance
mapcache_invalidateis true.I am split whether the first check is worth it. The atomic operation should be uncontended most of the time, so it should be quick. But it will always be slower than just a read because there is always a store involved.
I am not a fun of optimizations with unclear benefits :-)
Quoted 2 lines
On a related topic, Jan pointed out that the invalidation would not work properly if you have multiple vCPU modifying the P2M at the same time.
Uhm, yes.