Support device passthrough when dom0 is PVH on Xen
v11 cover Support device passthrough when dom0 is PVH on Xen
Hi All,
This is v11 series to support passthrough when dom0 is PVHv10->v11 changes:
* patch#1: Move the curly braces of "case PHYSDEVOP_pci_device_state_reset" to the next line. Delete unnecessary local variables "struct physdev_pci_device *dev".
Downgrade printk to dprintk.
Moved struct pci_device_state_reset to the public header file.
Delete enum pci_device_state_reset_type, and use macro definitions to represent different reset types.
Delete pci_device_state_reset_method, and add switch cases in PHYSDEVOP_pci_device_state_reset to handle different reset functions.
Add reset type as a function parameter for vpci_reset_device_state for possible future use
* patch#2: Delete the judgment of "d==currd", so that we can prevent physdev_(un)map_pirq from being executed when domU has no pirq, instead of just preventing self-mapping; and modify the description of the commit message accordingly.
* patch#3: Modify the commit message to explain why the gsi of normal devices can work in PVH dom0 and why the passthrough device does not work in PVH dom0.
* patch#4: New patch, modification of allocate_pirq function, return the allocated pirq when there is already an allocated pirq and the caller has no specific requirements for pirq, and make it successful.
* patch#5: Modification on the hypervisor side proposed from patch#5 of v10.
Add non-zero judgment for other bits of allow_access.
Delete unnecessary judgment "if ( is_pv_domain(currd) || has_pirq(currd) )". Change the error exit path identifier "out" to "gsi_permission_out".
Use ARRAY_SIZE() instead of open coed.
* patch#6: New patch, modification of xc_physdev_map_pirq to support mapping gsi to an idle pirq.
* patch#7: Patch#4 of v10, directly open "/dev/xen/privcmd" in the function xc_physdev_gsi_from_dev instead of adding unnecessary functions to libxencall.
Change the type of gsi in the structure privcmd_gsi_from_dev from int to u32.
* patch#8: Modification of the tools part of patches#4 and #5 of v10, use privcmd_gsi_from_dev to get gsi, and use XEN_DOMCTL_gsi_permission to grant gsi.
Change the hard-coded 0 to use LIBXL_TOOLSTACK_DOMID.
Add libxl__arch_hvm_map_gsi to distinguish x86 related implementations.
Add a list pcidev_pirq_list to record the relationship between sbdf and pirq, which can be used to obtain the corresponding pirq when unmap PIRQ.
Best regards,
Jiqian Chen
v9->v10 changes:
* patch#2: Indent the comments above PHYSDEVOP_map_pirq according to the code style.
* patch#3: Modified the description in the commit message, changing "it calls" to "it will need to call", indicating that there will be new codes on the kernel side that will call PHYSDEVOP_setup_gsi. Also added an explanation of why the interrupt of passthrough device does not work if gsi is not registered.
* patch#4: Added define for CONFIG_X86 in tools/libs/light/Makefile to isolate x86 code in libxl_pci.c.
* patch#5: Modified the commit message to further describe the purpose of adding XEN_DOMCTL_gsi_permission. Deleted pci_device_set_gsi and called XEN_DOMCTL_gsi_permission directly in pci_add_dm_done. Added a check for all zeros in the padding field in XEN_DOMCTL_gsi_permission, and used currd instead of current->domain.
In the function gsi_2_irq, apic_pin_2_gsi_irq was used instead of the original new code, and error handling for irq0 was added.
Deleted the extra spaces in the upper and lower lines of the struct xen_domctl_gsi_permission definition.
All patches have modified signatures as follows:
v8->v9 changes:
* patch#1: Move pcidevs_unlock below write_lock, and remove "ASSERT(pcidevs_locked());" from vpci_reset_device_state;
Add pci_device_state_reset_type to distinguish the reset types.
* patch#2: Add a comment above PHYSDEVOP_map_pirq to describe why need this hypercall.
Change "!is_pv_domain(d)" to "is_hvm_domain(d)", and "map.domid == DOMID_SELF" to
"d == current->domian".* patch#3: Remove the check of PHYSDEVOP_setup_gsi, since there is same checke in below.Although their return values are different, this difference is acceptable for the sake of code consistency if ( !is_hardware_domain(currd) )
return -ENOSYS;
break;
* patch#5: Change the commit message to describe more why we need this new hypercall.
Add comment above "if ( is_pv_domain(current->domain) || has_pirq(current->domain) )" to explain why we need this check.
Add gsi_2_irq to transform gsi to irq, instead of considering gsi == irq.
Add explicit padding to struct xen_domctl_gsi_permission.
v7->v8 changes:
* patch#2: Add the domid check(domid == DOMID_SELF) to prevent self map when guest doesn't use pirq. That check was missed in the previous version.
* patch#4: Due to changes in the implementation of obtaining gsi in the kernel. Change to add a new function to get gsi by passing in the sbdf of pci device.
* patch#5: Remove the parameter "is_gsi", when there exist gsi, in pci_add_dm_done use a new function pci_device_set_gsi to do map_pirq and grant permission. That gets more intuitive code logic.
v6->v7 changes:
* patch#4: Due to changes in the implementation of obtaining gsi in the kernel. Change to add a new function to get gsi from irq, instead of gsi sysfs.
* patch#5: Fix the issue with variable usage, rc->r.
v5->v6 changes:
* patch#1: Add Reviewed-by Stefano and Stewart. Rebase code and change old function vpci_remove_device, vpci_add_handlers to vpci_deassign_device, vpci_assign_device
* patch#2: Add Reviewed-by Stefano
* patch#3: Remove unnecessary "ASSERT(!has_pirq(currd));"
* patch#4: Fix some coding style issues below directory tools
* patch#5: Modified some variable names and code logic to make code easier to be understood, which to use gsi by default and be compatible with older kernel versions to continue to use irq
v4->v5 changes:
* patch#1: add pci_lock wrap function vpci_reset_device_state
* patch#2: move the check of self map_pirq to physdev.c, and change to check if the caller has PIRQ flag, and just break for PHYSDEVOP_(un)map_pirq in hvm_physdev_op
* patch#3: return -EOPNOTSUPP instead, and use ASSERT(!has_pirq(currd));
* patch#4: is the patch#5 in v4 because patch#5 in v5 has some dependency on it. And add the handling of errno and add the Reviewed-by Stefano
* patch#5: is the patch#4 in v4. New implementation to add new hypercall XEN_DOMCTL_gsi_permission to grant gsi
v3->v4 changes:
* patch#1: change the comment of PHYSDEVOP_pci_device_state_reset; move printings behind pcidevs_unlock
* patch#2: add check to prevent PVH self map
* patch#3: new patch, The implementation of adding PHYSDEVOP_setup_gsi for PVH is treated as a separate patch
* patch#4: new patch to solve the map_pirq problem of PVH dom0. use gsi to grant irq permission in XEN_DOMCTL_irq_permission.
* patch#5: to be compatible with previous kernel versions, when there is no gsi sysfs, still use irq v4 link:
https://lore.kernel.org/xen-devel/[email protected]/T/#t
v2->v3 changes:
* patch#1: move the content out of pci_reset_device_state and delete pci_reset_device_state; add xsm_resource_setup_pci check for PHYSDEVOP_pci_device_state_reset; add description for PHYSDEVOP_pci_device_state_reset;
* patch#2: du to changes in the implementation of the second patch on kernel side(that it will do setup_gsi and map_pirq when assigning a device to passthrough), add PHYSDEVOP_setup_gsi for PVH dom0, and we need to support self mapping.
* patch#3: du to changes in the implementation of the second patch on kernel side(that adds a new sysfs for gsi instead of a new syscall), so read gsi number from the sysfs of gsi.
v3 link:
https://lore.kernel.org/xen-devel/[email protected]/T/#t
v2 link:
https://lore.kernel.org/xen-devel/[email protected]/T/#t
Below is the description of v2 cover letter:
This series of patches are the v2 of the implementation of passthrough when dom0 is PVH on Xen. We sent the v1 to upstream before, but the v1 had so many problems and we got lots of suggestions. I will introduce all issues that these patches try to fix and the differences between v1 and v2.
Issues we encountered:
1. pci_stub failed to write bar for a passthrough device.
Problem: when we run \u201csudo xl pci-assignable-add <sbdf>\u201d to assign a device, pci_stub will call pcistub_init_device() -> pci_restore_state() -> pci_restore_config_space() -> pci_restore_config_space_range() -> pci_restore_config_dword() -> pci_write_config_dword()\u201d, the pci config write will trigger an io interrupt to bar_write() in the xen, but thebar->enabled was set before, the write is not allowed now, and then whenbar->Qemu config the
passthrough device in xen_pt_realize(), it gets invalid bar values.
Reason: the reason is that we don't tell vPCI that the device has been reset, so the current cached state in pdev->vpci is all out of date and is different from the real device state.
Solution: to solve this problem, the first patch of kernel(xen/pci: Add xen_reset_device_state function) and the fist patch of xen(xen/vpci: Clear all vpci status of device) add a new hypercall to reset the state stored in vPCI when the state of real device has changed.
Thank Roger for the suggestion of this v2, and it is different from
v1 (https://lore.kernel.org/xen-devel/[email protected]/), v1 simply allow domU to write pci bar, it does not comply with the design principles of vPCI.
2. failed to do PHYSDEVOP_map_pirq when dom0 is PVH
Problem: HVM domU will do PHYSDEVOP_map_pirq for a passthrough device by using gsi. See xen_pt_realize->xc_physdev_map_pirq and pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op(), PHYSDEVOP_map_pirq is not allowed.
Reason: In hvm_physdev_op(), the variable "currd" is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.
Solution: I think we may need to allow PHYSDEVOP_map_pirq when "currd" is dom0 (at present dom0 is PVH). The second patch of xen(x86/pvh: Open PHYSDEVOP_map_pirq for PVH dom0) allow PVH dom0 do PHYSDEVOP_map_pirq. This v2 patch is better than v1, v1 simply remove the has_pirq check
(xen https://lore.kernel.org/xen-devel/[email protected]/).
3. the gsi of a passthrough device doesn't be unmasked 3.1 failed to check the permission of pirq
3.2 the gsi of passthrough device was not registered in PVH dom0
Problem:
3.1 callback function pci_add_dm_done() will be called when qemu config a passthrough device for domU. This function will call xc_domain_irq_permission()-> pirq_access_permitted() to check if the gsi has corresponding mappings in dom0. But it didn\u2019t, so failed. See XEN_DOMCTL_irq_permission->pirq_access_permitted, "current" is PVH dom0 and it return irq is 0.
3.2 it's possible for a gsi (iow: vIO-APIC pin) to never get registered on PVH dom0, because the devices of PVH are using MSI(-X) interrupts. However, the IO-APIC pin must be configured for it to be able to be mapped into a domU.
Reason: After searching codes, I find "map_pirq" and "register_gsi" will be done in function vioapic_write_redirent->vioapic_hwdom_map_gsi when the gsi(aka ioapic's pin) is unmasked in PVH dom0. So the two problems can be concluded to that the gsi of a passthrough device doesn't be unmasked.
Solution: to solve these problems, the second patch of kernel(xen/pvh: Unmask irq for passthrough device in PVH dom0) call the unmask_irq() when we assign a device to be passthrough. So that passthrough devices can have the mapping of gsi on PVH dom0 and gsi can be registered. This v2 patch is different from the
v1( kernel https://lore.kernel.org/xen-devel/[email protected]/,
kernel https://lore.kernel.org/xen-devel/[email protected]/ and
xen https://lore.kernel.org/xen-devel/[email protected]/),
v1 performed "map_pirq" and "register_gsi" on all pci devices on PVH dom0, which is unnecessary and may cause multiple registration.
4. failed to map pirq for gsi
Problem: qemu will call xc_physdev_map_pirq() to map a passthrough device\u2019s gsi to pirq in function xen_pt_realize(). But failed.
Reason: According to the implement of xc_physdev_map_pirq(), it needs gsi instead of irq, but qemu pass irq to it and treat irq as gsi, it is got from file /sys/bus/pci/devices/xxxx:xx:xx.x/irq in function xen_host_pci_device_get(). But actually the gsi number is not equal with irq. On PVH dom0, when it allocates irq for a gsi in function acpi_register_gsi_ioapic(), allocation is dynamic, and follow the principle of applying first, distributing first. And if you debug the kernel codes(see function __irq_alloc_descs), you will find the irq number is allocated from small to large by order, but the applying gsi number is not, gsi 38 may come before gsi 28, that causes gsi 38 get a smaller irq number than gsi 28, and then gsi != irq.
Solution: we can record the relation between gsi and irq, then when userspace(qemu) want to use gsi, we can do a translation. The third patch of kernel(xen/privcmd: Add new syscall to get gsi from irq) records all the relations in acpi_register_gsi_xen_pvh() when dom0 initialize pci devices, and provide a syscall for userspace to get the gsi from irq. The third patch of xen(tools: Add new function to get gsi from irq) add a new function xc_physdev_gsi_from_irq() to call the new syscall added on kernel side.
And then userspace can use that function to get gsi. Then xc_physdev_map_pirq() will success. This v2 patch is the same as v1( kernel https://lore.kernel.org/xen-devel/[email protected]/ and xen https://lore.kernel.org/xen-devel/[email protected]/)
About the v2 patch of qemu, just change an included head file, other are similar to the v1 ( qemu https://lore.kernel.org/xen-devel/[email protected]/), just call xc_physdev_gsi_from_irq() to get gsi from irq.
Jiqian Chen (8):
xen/vpci: Clear all vpci status of device x86/pvh: Allow (un)map_pirq when dom0 is PVH x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0 x86/physdev: Return pirq that irq was already mapped to x86/domctl: Add XEN_DOMCTL_gsi_permission to grant gsi tools/libxc: Allow gsi be mapped into a free pirq
tools: Add new function to get gsi from dev
tools: Add new function to do PIRQ (un)map on PVH dom0
tools/include/xen-sys/Linux/privcmd.h | 7 ++ tools/include/xenctrl.h | 7 ++ tools/libs/ctrl/xc_domain.c | 15 ++++ tools/libs/ctrl/xc_physdev.c | 37 ++++++++- tools/libs/light/libxl_arch.h | 4 + tools/libs/light/libxl_arm.c | 10 +++ tools/libs/light/libxl_pci.c | 17 ++++ tools/libs/light/libxl_x86.c | 111 ++++++++++++++++++++++++++ xen/arch/x86/domctl.c | 33 ++++++++ xen/arch/x86/hvm/hypercall.c | 8 ++ xen/arch/x86/include/asm/io_apic.h | 2 + xen/arch/x86/io_apic.c | 17 ++++ xen/arch/x86/irq.c | 2 + xen/arch/x86/mpparse.c | 3 +- xen/arch/x86/physdev.c | 14 ++++ xen/drivers/pci/physdev.c | 58 ++++++++++++++ xen/drivers/vpci/vpci.c | 10 +++ xen/include/public/domctl.h | 8 ++ xen/include/public/physdev.h | 20 +++++ xen/include/xen/vpci.h | 8 ++ xen/xsm/flask/hooks.c | 1 + 21 files changed, 389 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
PATCH v11 1/8 xen/vpci: Clear all vpci status of device
When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.
--- xen/arch/x86/hvm/hypercall.c | 1 + xen/drivers/pci/physdev.c | 58 ++++++++++++++++++++++++++++++++++++ xen/drivers/vpci/vpci.c | 10 +++++++ xen/include/public/physdev.h | 20 +++++++++++++ xen/include/xen/vpci.h | 8 +++++ 5 files changed, 97 insertions(+)
xen/arch/x86/hvm/hypercall.c +1 -0
@@ -83,6 +83,7 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)case PHYSDEVOP_pci_mmcfg_reserved:case PHYSDEVOP_pci_device_add:case PHYSDEVOP_pci_device_remove: +case PHYSDEVOP_pci_device_state_reset:case PHYSDEVOP_dbgp_op:if ( !is_hardware_domain (currd) )return -ENOSYS ;
xen/drivers/pci/physdev.c +58 -0
70 lines not on this page · raw message
xen/drivers/vpci/vpci.c +10 -0
@@ -172,6 +172,16 @@ int vpci_assign_device(struct pci_dev *pdev)return rc; } + +int vpci_reset_device_state (struct pci_dev *pdev, +uint32_t reset_type) +{ +ASSERT (rw_is_write_locked (&pdev->domain->pci_lock)); + +vpci_deassign_device (pdev); +return vpci_assign_device (pdev); +} +#endif /* __XEN__ */ static int vpci_register_cmp(const struct vpci_register *r1,
xen/include/public/physdev.h +20 -0
@@ -296,6 +296,13 @@ DEFINE_XEN_GUEST_HANDLE(physdev_pci_device_add_t);*/ #define PHYSDEVOP_prepare_msix 30#define PHYSDEVOP_release_msix 31 +/* +* Notify the hypervisor that a PCI device has been reset, so that any +* internally cached state is regenerated. Should be called after any +* device reset performed by the hardware domain. +*/ +#define PHYSDEVOP_pci_device_state_reset 32 +struct physdev_pci_device {/* IN */ uint16_t seg;
@@ -305,6 +312,19 @@ struct physdev_pci_device {typedef struct physdev_pci_device physdev_pci_device_t ;DEFINE_XEN_GUEST_HANDLE (physdev_pci_device_t); +struct pci_device_state_reset { +physdev_pci_device_t dev; +#define _PCI_DEVICE_STATE_RESET_COLD 0 +#define PCI_DEVICE_STATE_RESET_COLD (1U<<_PCI_DEVICE_STATE_RESET_COLD) +#define _PCI_DEVICE_STATE_RESET_WARM 1 +#define PCI_DEVICE_STATE_RESET_WARM (1U<<_PCI_DEVICE_STATE_RESET_WARM) +#define _PCI_DEVICE_STATE_RESET_HOT 2 +#define PCI_DEVICE_STATE_RESET_HOT (1U<<_PCI_DEVICE_STATE_RESET_HOT) +#define _PCI_DEVICE_STATE_RESET_FLR 3 +#define PCI_DEVICE_STATE_RESET_FLR (1U<<_PCI_DEVICE_STATE_RESET_FLR) +uint32_t reset_type; +}; +#define PHYSDEVOP_DBGP_RESET_PREPARE 1#define PHYSDEVOP_DBGP_RESET_DONE 2
xen/include/xen/vpci.h +8 -0
@@ -38,6 +38,8 @@ int __must_check vpci_assign_device(struct pci_dev *pdev);/* Remove all handlers and free vpci related structures. */ void vpci_deassign_device (struct pci_dev *pdev); +int __must_check vpci_reset_device_state (struct pci_dev *pdev, +uint32_t reset_type);/* Add/remove a register handler. */ int __must_check vpci_add_register_mask(struct vpci *vpci,
@@ -282,6 +284,12 @@ static inline int vpci_assign_device(struct pci_dev *pdev)static inline void vpci_deassign_device (struct pci_dev *pdev) { } +static inline int __must_check vpci_reset_device_state (struct pci_dev *pdev, +uint32_t reset_type) +{ +return 0 ; +} +static inline void vpci_dump_msi (void ) { } static inline uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg,
Signature · 1 line
2.34.1
Re: [XEN PATCH v11 1/8] xen/vpci: Clear all vpci status of device
Quoted 6 lines · Jiqian Chen
When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.
While the description properly talks about all of this being about device reset, the title suggests otherwise (leaving open what the context is, thus
- to me at least - suggesting it's during vPCI init for a particular device).
Quoted 28 lines
xen/drivers/pci/physdev.c +24
-0@@ -67,6 +68,63 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)break ; } +case PHYSDEVOP_pci_device_state_reset: + { +struct pci_device_state_reset dev_reset; +struct pci_dev *pdev; +pci_sbdf_t sbdf; + + ret = -EOPNOTSUPP ; +if ( !is_pci_passthrough_enabled () ) +break ; + + ret = -EFAULT ; +if (copy_from_guest (&dev_reset, arg,1 ) !=0 ) +break ; + + sbdf =PCI_SBDF (dev_reset.dev.seg, + dev_reset.dev.bus, + dev_reset.dev.devfn); + + ret =xsm_resource_setup_pci (XSM_PRIV , sbdf.sbdf); +if ( ret ) +break ; + +pcidevs_lock (); + pdev =pci_get_pdev (NULL , sbdf);
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 104
Quoted 10 lines
xen/drivers/pci/physdev.c +10
-0+if ( !pdev ) + { +pcidevs_unlock (); + ret = -ENODEV ; +break ; + } + +write_lock (&pdev->domain->pci_lock); +pcidevs_unlock (); +/* Implement FLR, other reset types may be implemented in future */
The comment isn't in sync with the code anymore.
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 111
Quoted 7 lines
xen/drivers/pci/physdev.c +7
-0+switch ( dev_reset.reset_type ) + { +case PCI_DEVICE_STATE_RESET_COLD : +case PCI_DEVICE_STATE_RESET_WARM : +case PCI_DEVICE_STATE_RESET_HOT : +case PCI_DEVICE_STATE_RESET_FLR : + {
This brace isn't needed while at the same time it is confusing.
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 115
Quoted 4 lines
xen/drivers/pci/physdev.c +4
-0+ ret =vpci_reset_device_state (pdev, dev_reset.reset_type); +if ( ret ) +dprintk (XENLOG_ERR , +"%pp: failed to reset vPCI device state\n" , &sbdf);
I question the need for a log message here.
Quoted 22 lines
xen/include/public/physdev.h +9
-0@@ -296,6 +296,13 @@ DEFINE_XEN_GUEST_HANDLE(physdev_pci_device_add_t);*/ #define PHYSDEVOP_prepare_msix 30#define PHYSDEVOP_release_msix 31 +/* +* Notify the hypervisor that a PCI device has been reset, so that any +* internally cached state is regenerated. Should be called after any +* device reset performed by the hardware domain. +*/ +#define PHYSDEVOP_pci_device_state_reset 32 +struct physdev_pci_device {/* IN */ uint16_t seg;@@ -305,6 +312,19 @@ struct physdev_pci_device {typedef struct physdev_pci_device physdev_pci_device_t ;DEFINE_XEN_GUEST_HANDLE (physdev_pci_device_t); +struct pci_device_state_reset { +physdev_pci_device_t dev;
xen/include/public/physdev.h struct physdev_pci_device { · line 326
Quoted 10 lines
xen/include/public/physdev.h +10
-0+#define _PCI_DEVICE_STATE_RESET_COLD 0 +#define PCI_DEVICE_STATE_RESET_COLD (1U<<_PCI_DEVICE_STATE_RESET_COLD) +#define _PCI_DEVICE_STATE_RESET_WARM 1 +#define PCI_DEVICE_STATE_RESET_WARM (1U<<_PCI_DEVICE_STATE_RESET_WARM) +#define _PCI_DEVICE_STATE_RESET_HOT 2 +#define PCI_DEVICE_STATE_RESET_HOT (1U<<_PCI_DEVICE_STATE_RESET_HOT) +#define _PCI_DEVICE_STATE_RESET_FLR 3 +#define PCI_DEVICE_STATE_RESET_FLR (1U<<_PCI_DEVICE_STATE_RESET_FLR) +uint32_t reset_type; +};
Do we really need the _PCI_DEVICE_STATE_RESET_* bit positions as separate #define-s? I can't spot any use anywhere.
Jan
Re: [XEN PATCH v11 1/8] xen/vpci: Clear all vpci status of device
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Jiqian Chen
When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.
Quoted 4 lines
While the description properly talks about all of this being about device reset, the title suggests otherwise (leaving open what the context is, thus
- to me at least - suggesting it's during vPCI init for a particular device).
Change title to "xen/pci: Add hypercall to support reset of pcidev" ?
Quoted 33 lines
Quoted 33 lines
xen/drivers/pci/physdev.c +28
-0@@ -67,6 +68,63 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)break ; } +case PHYSDEVOP_pci_device_state_reset: + { +struct pci_device_state_reset dev_reset; +struct pci_dev *pdev; +pci_sbdf_t sbdf; + + ret = -EOPNOTSUPP ; +if ( !is_pci_passthrough_enabled () ) +break ; + + ret = -EFAULT ; +if (copy_from_guest (&dev_reset, arg,1 ) !=0 ) +break ; + + sbdf =PCI_SBDF (dev_reset.dev.seg, + dev_reset.dev.bus, + dev_reset.dev.devfn); + + ret =xsm_resource_setup_pci (XSM_PRIV , sbdf.sbdf); +if ( ret ) +break ; + +pcidevs_lock (); + pdev =pci_get_pdev (NULL , sbdf); +if ( !pdev ) + { +pcidevs_unlock (); + ret = -ENODEV ;
Quoted 8 lines
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 104
Quoted 6 lines
xen/drivers/pci/physdev.c +6
-0+break ; + } + +write_lock (&pdev->domain->pci_lock); +pcidevs_unlock (); +/* Implement FLR, other reset types may be implemented in future */ The comment isn't in sync with the code anymore.
Change to "/* vpci_reset_device_state is called by default for all reset types, other specific operations can be added later as needed */" ?
Quoted 9 lines
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 111
Quoted 8 lines
xen/drivers/pci/physdev.c +7
-0+switch ( dev_reset.reset_type ) + { +case PCI_DEVICE_STATE_RESET_COLD : +case PCI_DEVICE_STATE_RESET_WARM : +case PCI_DEVICE_STATE_RESET_HOT : +case PCI_DEVICE_STATE_RESET_FLR : + {
Quoted 8 lines
This brace isn't needed while at the same time it is confusing.
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 115
Quoted 4 lines
xen/drivers/pci/physdev.c +4
-0+ ret =vpci_reset_device_state (pdev, dev_reset.reset_type); +if ( ret ) +dprintk (XENLOG_ERR , +"%pp: failed to reset vPCI device state\n" , &sbdf);I question the need for a log message here.
OK, will delete it in next version.
Quoted 21 lines
Quoted 21 lines
xen/include/public/physdev.h +7
-0@@ -296,6 +296,13 @@ DEFINE_XEN_GUEST_HANDLE(physdev_pci_device_add_t);*/ #define PHYSDEVOP_prepare_msix 30#define PHYSDEVOP_release_msix 31 +/* +* Notify the hypervisor that a PCI device has been reset, so that any +* internally cached state is regenerated. Should be called after any +* device reset performed by the hardware domain. +*/ +#define PHYSDEVOP_pci_device_state_reset 32 +struct physdev_pci_device {/* IN */ uint16_t seg;@@ -305,6 +312,19 @@ struct physdev_pci_device {typedef struct physdev_pci_device physdev_pci_device_t ;DEFINE_XEN_GUEST_HANDLE (physdev_pci_device_t);
Quoted 15 lines
xen/include/public/physdev.h struct physdev_pci_device { · line 326
Quoted 12 lines
xen/include/public/physdev.h +12
-0+struct pci_device_state_reset { +physdev_pci_device_t dev; +#define _PCI_DEVICE_STATE_RESET_COLD 0 +#define PCI_DEVICE_STATE_RESET_COLD (1U<<_PCI_DEVICE_STATE_RESET_COLD) +#define _PCI_DEVICE_STATE_RESET_WARM 1 +#define PCI_DEVICE_STATE_RESET_WARM (1U<<_PCI_DEVICE_STATE_RESET_WARM) +#define _PCI_DEVICE_STATE_RESET_HOT 2 +#define PCI_DEVICE_STATE_RESET_HOT (1U<<_PCI_DEVICE_STATE_RESET_HOT) +#define _PCI_DEVICE_STATE_RESET_FLR 3 +#define PCI_DEVICE_STATE_RESET_FLR (1U<<_PCI_DEVICE_STATE_RESET_FLR) +uint32_t reset_type; +};Do we really need the _PCI_DEVICE_STATE_RESET_* bit positions as separate #define-s? I can't spot any use anywhere.
I thought it was a coding style.
I will delete them in next version.
Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
Re: [XEN PATCH v11 1/8] xen/vpci: Clear all vpci status of device
Quoted 9 lines · Jiqian
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Jiqian Chen
When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.
Quoted 5 lines
Quoted 4 lines
While the description properly talks about all of this being about device reset, the title suggests otherwise (leaving open what the context is, thus
- to me at least - suggesting it's during vPCI init for a particular device).Change title to "xen/pci: Add hypercall to support reset of pcidev" ?
Perhaps.
Quoted 40 lines
Quoted 32 lines
Quoted 32 lines
xen/drivers/pci/physdev.c +28
-0@@ -67,6 +68,63 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)break ; } +case PHYSDEVOP_pci_device_state_reset: + { +struct pci_device_state_reset dev_reset; +struct pci_dev *pdev; +pci_sbdf_t sbdf; + + ret = -EOPNOTSUPP ; +if ( !is_pci_passthrough_enabled () ) +break ; + + ret = -EFAULT ; +if (copy_from_guest (&dev_reset, arg,1 ) !=0 ) +break ; + + sbdf =PCI_SBDF (dev_reset.dev.seg, + dev_reset.dev.bus, + dev_reset.dev.devfn); + + ret =xsm_resource_setup_pci (XSM_PRIV , sbdf.sbdf); +if ( ret ) +break ; + +pcidevs_lock (); + pdev =pci_get_pdev (NULL , sbdf); +if ( !pdev ) + { +pcidevs_unlock (); + ret = -ENODEV ;Quoted 8 lines
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 104
Quoted 6 lines
xen/drivers/pci/physdev.c +6
-0+break ; + } + +write_lock (&pdev->domain->pci_lock); +pcidevs_unlock (); +/* Implement FLR, other reset types may be implemented in future */ The comment isn't in sync with the code anymore.
Quoted 1 line
Change to "/*
vpci_reset_device_stateis called by default for all reset types, other specific operations can be added later as needed */" ?
Counter question: Is such a comment really adding any value?
Jan
PATCH v11 2/8 x86/pvh: Allow (un)map_pirq when dom0 is PVH
In reply to Jiqian ChenIf run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see qemu code xen_pt_realize->xc_physdev_map_pirq and libxl code pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op, PHYSDEVOP_map_pirq is not allowed because currd is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.
So, allow PHYSDEVOP_map_pirq when dom0 is PVH and also allow PHYSDEVOP_unmap_pirq for the removal device path to unmap pirq. And add a new check to prevent (un)map when the subject domain has no X86_EMU_USE_PIRQ flag.
So that the interrupt of a passthrough device can be successfully mapped to pirq for domU with X86_EMU_USE_PIRQ flag when dom0 is PVH
--- xen/arch/x86/hvm/hypercall.c | 6 ++++++ xen/arch/x86/physdev.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+)
xen/arch/x86/hvm/hypercall.c +6 -0
@@ -71,8 +71,14 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)switch ( cmd ) { +/* +* Only being permitted for management of other domains. +* Further restrictions are enforced in do_physdev_op. +*/ case PHYSDEVOP_map_pirq:case PHYSDEVOP_unmap_pirq: +break ; +case PHYSDEVOP_eoi:case PHYSDEVOP_irq_status_query:case PHYSDEVOP_get_free_pirq:
xen/arch/x86/physdev.c +14 -0
@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);
@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);
Signature · 1 line
2.34.1
Re: [XEN PATCH v11 2/8] x86/pvh: Allow (un)map_pirq when dom0 is PVH
Quoted 13 lines · Jiqian Chen
If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see qemu code
xen_pt_realize->xc_physdev_map_pirqand libxl codepci_add_dm_done->xc_physdev_map_pirq. Thenxc_physdev_map_pirqwill call into Xen, but inhvm_physdev_op,PHYSDEVOP_map_pirqis not allowed because currd is PVH dom0 and PVH has noX86_EMU_USE_PIRQflag, it will fail athas_pirqcheck.So, allow
PHYSDEVOP_map_pirqwhen dom0 is PVH and also allowPHYSDEVOP_unmap_pirqfor the removal device path to unmap pirq. And add a new check to prevent (un)map when the subject domain has noX86_EMU_USE_PIRQflag.
Quoted 8 lines
So that the interrupt of a passthrough device can be successfully mapped to pirq for domU with
X86_EMU_USE_PIRQflag when dom0 is PVHSigned-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>
You keep carrying this R-b, despite making functional changes. This can't be quite right.
While functionally I'm now okay with the change, I still have a code structure concern:
Quoted 20 lines
xen/arch/x86/physdev.c +7
-0@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ;
xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 365
Quoted 10 lines
xen/arch/x86/physdev.c +7
-0+/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);
If you did go look, you will have noticed that we use "return" in the middle of this function only very sparingly (when alternatives would result in more complicated code elsewhere). I think you want to avoid "return" here, too, and probably go even further and avoid the extra rcu_unlock_domain() as well. That's easily possible to arrange for (taking the latter case as example):
/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */
if ( !is_hvm_domain(d) || has_pirq(d) )
ret = physdev_unmap_pirq(d, unmap.pirq);
else
ret = -EOPNOTSUPP;
rcu_unlock_domain(d);Personally I would even use a conditional operator here, but I believe others might dislike its use in situations like this one.
The re-arrangement make a little more noticeable though that the comment isn't quite right either: PV domains necessarily have no X86_EMU_USE_PIRQ. Maybe "... has no notion of pIRQ"?
Jan
Re: [XEN PATCH v11 2/8] x86/pvh: Allow (un)map_pirq when dom0 is PVH
Quoted 18 lines · Jan Beulich
Quoted 17 lines · Jiqian Chen
If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see qemu code
xen_pt_realize->xc_physdev_map_pirqand libxl codepci_add_dm_done->xc_physdev_map_pirq. Thenxc_physdev_map_pirqwill call into Xen, but inhvm_physdev_op,PHYSDEVOP_map_pirqis not allowed because currd is PVH dom0 and PVH has noX86_EMU_USE_PIRQflag, it will fail athas_pirqcheck.So, allow
PHYSDEVOP_map_pirqwhen dom0 is PVH and also allowPHYSDEVOP_unmap_pirqfor the removal device path to unmap pirq. And add a new check to prevent (un)map when the subject domain has noX86_EMU_USE_PIRQflag.So that the interrupt of a passthrough device can be successfully mapped to pirq for domU with
X86_EMU_USE_PIRQflag when dom0 is PVH
Quoted 7 lines
Quoted 4 lines
Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>You keep carrying this R-b, despite making functional changes. This can't be quite right.
Will remove in next version.
Quoted 49 lines
While functionally I'm now okay with the change, I still have a code structure concern:
Quoted 20 lines
xen/arch/x86/physdev.c +7
-0@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ;xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 365
Quoted 10 lines
xen/arch/x86/physdev.c +7
-0+/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);If you did go look, you will have noticed that we use "return" in the middle of this function only very sparingly (when alternatives would result in more complicated code elsewhere). I think you want to avoid "return" here, too, and probably go even further and avoid the extra
rcu_unlock_domain()as well. That's easily possible to arrange for (taking the latter case as example):/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ if ( !is_hvm_domain(d) || has_pirq(d) ) ret = physdev_unmap_pirq(d, unmap.pirq); else ret = -EOPNOTSUPP; rcu_unlock_domain(d);
Quoted 6 lines
Personally I would even use a conditional operator here, but I believe others might dislike its use in situations like this one.
The re-arrangement make a little more noticeable though that the comment isn't quite right either: PV domains necessarily have no
X86_EMU_USE_PIRQ. Maybe "... has no notion of pIRQ"?
Or just like below?
/*
* Prevent unmapping when the subject hvm domain has no
* X86_EMU_USE_PIRQ
*/
if ( is_hvm_domain(d) && !has_pirq(d) )
ret = -EOPNOTSUPP;
else
ret = physdev_unmap_pirq(d, unmap.pirq);Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
Re: [XEN PATCH v11 2/8] x86/pvh: Allow (un)map_pirq when dom0 is PVH
Quoted 86 lines · Jiqian
Quoted 18 lines · Jan Beulich
Quoted 17 lines · Jiqian Chen
If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see qemu code
xen_pt_realize->xc_physdev_map_pirqand libxl codepci_add_dm_done->xc_physdev_map_pirq. Thenxc_physdev_map_pirqwill call into Xen, but inhvm_physdev_op,PHYSDEVOP_map_pirqis not allowed because currd is PVH dom0 and PVH has noX86_EMU_USE_PIRQflag, it will fail athas_pirqcheck.So, allow
PHYSDEVOP_map_pirqwhen dom0 is PVH and also allowPHYSDEVOP_unmap_pirqfor the removal device path to unmap pirq. And add a new check to prevent (un)map when the subject domain has noX86_EMU_USE_PIRQflag.So that the interrupt of a passthrough device can be successfully mapped to pirq for domU with
X86_EMU_USE_PIRQflag when dom0 is PVHQuoted 7 lines
Quoted 4 lines
Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>You keep carrying this R-b, despite making functional changes. This can't be quite right.
Will remove in next version.
Quoted 49 lines
While functionally I'm now okay with the change, I still have a code structure concern:
Quoted 20 lines
xen/arch/x86/physdev.c +7
-0@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ;xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 365
Quoted 10 lines
xen/arch/x86/physdev.c +7
-0+/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);If you did go look, you will have noticed that we use "return" in the middle of this function only very sparingly (when alternatives would result in more complicated code elsewhere). I think you want to avoid "return" here, too, and probably go even further and avoid the extra
rcu_unlock_domain()as well. That's easily possible to arrange for (taking the latter case as example):/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ if ( !is_hvm_domain(d) || has_pirq(d) ) ret = physdev_unmap_pirq(d, unmap.pirq); else ret = -EOPNOTSUPP; rcu_unlock_domain(d);Quoted 6 lines
Personally I would even use a conditional operator here, but I believe others might dislike its use in situations like this one.
The re-arrangement make a little more noticeable though that the comment isn't quite right either: PV domains necessarily have no
X86_EMU_USE_PIRQ. Maybe "... has no notion of pIRQ"?Or just like below?
Quoted 8 lines
/*
* Prevent unmapping when the subject hvm domain has no
* X86_EMU_USE_PIRQ
*/
if ( is_hvm_domain(d) && !has_pirq(d) )
ret = -EOPNOTSUPP;
else
ret = physdev_unmap_pirq(d, unmap.pirq);No objection to the slightly changed comment. The code alternative you present is of course functionally identical, yet personally I prefer to have the "good" case on the "if" branch and the "bad" one following "else". I wouldn't insist, though.
Jan
Re: [XEN PATCH v11 2/8] x86/pvh: Allow (un)map_pirq when dom0 is PVH
Quoted 96 lines · Jan Beulich
Quoted 86 lines · Jiqian
Quoted 18 lines · Jan Beulich
Quoted 17 lines · Jiqian Chen
If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see qemu code
xen_pt_realize->xc_physdev_map_pirqand libxl codepci_add_dm_done->xc_physdev_map_pirq. Thenxc_physdev_map_pirqwill call into Xen, but inhvm_physdev_op,PHYSDEVOP_map_pirqis not allowed because currd is PVH dom0 and PVH has noX86_EMU_USE_PIRQflag, it will fail athas_pirqcheck.So, allow
PHYSDEVOP_map_pirqwhen dom0 is PVH and also allowPHYSDEVOP_unmap_pirqfor the removal device path to unmap pirq. And add a new check to prevent (un)map when the subject domain has noX86_EMU_USE_PIRQflag.So that the interrupt of a passthrough device can be successfully mapped to pirq for domU with
X86_EMU_USE_PIRQflag when dom0 is PVHQuoted 7 lines
Quoted 4 lines
Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>Reviewed-by Stefano Stabellini <[email protected]>You keep carrying this R-b, despite making functional changes. This can't be quite right.
Will remove in next version.
Quoted 49 lines
While functionally I'm now okay with the change, I still have a code structure concern:
Quoted 20 lines
xen/arch/x86/physdev.c +7
-0@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ;xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 365
Quoted 10 lines
xen/arch/x86/physdev.c +7
-0+/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);If you did go look, you will have noticed that we use "return" in the middle of this function only very sparingly (when alternatives would result in more complicated code elsewhere). I think you want to avoid "return" here, too, and probably go even further and avoid the extra
rcu_unlock_domain()as well. That's easily possible to arrange for (taking the latter case as example):/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ if ( !is_hvm_domain(d) || has_pirq(d) ) ret = physdev_unmap_pirq(d, unmap.pirq); else ret = -EOPNOTSUPP; rcu_unlock_domain(d);Quoted 6 lines
Personally I would even use a conditional operator here, but I believe others might dislike its use in situations like this one.
The re-arrangement make a little more noticeable though that the comment isn't quite right either: PV domains necessarily have no
X86_EMU_USE_PIRQ. Maybe "... has no notion of pIRQ"?Or just like below?
Quoted 8 lines
/* * Prevent unmapping when the subject hvm domain has no * X86_EMU_USE_PIRQ */ if ( is_hvm_domain(d) && !has_pirq(d) ) ret = -EOPNOTSUPP; else ret = physdev_unmap_pirq(d, unmap.pirq);
Quoted 4 lines
No objection to the slightly changed comment. The code alternative you present is of course functionally identical, yet personally I prefer to have the "good" case on the "if" branch and the "bad" one following "else". I wouldn't insist, though.
OK, will change "good" case on the "if" branch.
Do I need to change "!is_hvm_domain(d)" to "is_pv_domain(d)" ? And then have:
/* Only unmapping when the subject domain has a notion of PIRQ */
if ( is_pv_domain(d) || has_pirq(d) )
ret = physdev_unmap_pirq(d, unmap.pirq);
else
ret = -EOPNOTSUPP;Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
Re: [XEN PATCH v11 2/8] x86/pvh: Allow (un)map_pirq when dom0 is PVH
Quoted 75 lines · Jiqian
Quoted 66 lines · Jan Beulich
Quoted 56 lines · Jiqian
Quoted 46 lines · Jan Beulich
Quoted 20 lines · Jiqian Chen
xen/arch/x86/physdev.c +7
-0@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ;xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 365
Quoted 10 lines
xen/arch/x86/physdev.c +7
-0+/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);If you did go look, you will have noticed that we use "return" in the middle of this function only very sparingly (when alternatives would result in more complicated code elsewhere). I think you want to avoid "return" here, too, and probably go even further and avoid the extra
rcu_unlock_domain()as well. That's easily possible to arrange for (taking the latter case as example):/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ if ( !is_hvm_domain(d) || has_pirq(d) ) ret = physdev_unmap_pirq(d, unmap.pirq); else ret = -EOPNOTSUPP; rcu_unlock_domain(d);Quoted 6 lines
Personally I would even use a conditional operator here, but I believe others might dislike its use in situations like this one.
The re-arrangement make a little more noticeable though that the comment isn't quite right either: PV domains necessarily have no
X86_EMU_USE_PIRQ. Maybe "... has no notion of pIRQ"?Or just like below?
Quoted 8 lines
/* * Prevent unmapping when the subject hvm domain has no * X86_EMU_USE_PIRQ */ if ( is_hvm_domain(d) && !has_pirq(d) ) ret = -EOPNOTSUPP; else ret = physdev_unmap_pirq(d, unmap.pirq);Quoted 4 lines
No objection to the slightly changed comment. The code alternative you present is of course functionally identical, yet personally I prefer to have the "good" case on the "if" branch and the "bad" one following "else". I wouldn't insist, though.
OK, will change "good" case on the "if" branch.
Do I need to change "!is_hvm_domain(d)" to "is_pv_domain(d)" ? And then have:
Quoted 5 lines
/* Only unmapping when the subject domain has a notion of PIRQ */
if ( is_pv_domain(d) || has_pirq(d) )
ret = physdev_unmap_pirq(d, unmap.pirq);
else
ret = -EOPNOTSUPP;I for one would prefer if you kept using is_hvm_domain(), for being more precise in this situation.
Jan
Re: [XEN PATCH v11 2/8] x86/pvh: Allow (un)map_pirq when dom0 is PVH
Quoted 76 lines · Jan Beulich
Quoted 75 lines · Jiqian
Quoted 66 lines · Jan Beulich
Quoted 56 lines · Jiqian
Quoted 46 lines · Jan Beulich
Quoted 20 lines · Jiqian Chen
xen/arch/x86/physdev.c +7
-0@@ -323,6 +323,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ; +/* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_map_pirq (d, map.type, &map.index, &map.pirq, &msi);rcu_unlock_domain (d);@@ -346,6 +353,13 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if ( !d )break ;xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 365
Quoted 10 lines
xen/arch/x86/physdev.c +7
-0+/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ +if (is_hvm_domain (d) && !has_pirq (d) ) + { +rcu_unlock_domain (d); +return -EOPNOTSUPP ; + } + ret =physdev_unmap_pirq (d, unmap.pirq);rcu_unlock_domain (d);If you did go look, you will have noticed that we use "return" in the middle of this function only very sparingly (when alternatives would result in more complicated code elsewhere). I think you want to avoid "return" here, too, and probably go even further and avoid the extra
rcu_unlock_domain()as well. That's easily possible to arrange for (taking the latter case as example):/* Prevent unmapping when the subject domain has no X86_EMU_USE_PIRQ */ if ( !is_hvm_domain(d) || has_pirq(d) ) ret = physdev_unmap_pirq(d, unmap.pirq); else ret = -EOPNOTSUPP; rcu_unlock_domain(d);Quoted 6 lines
Personally I would even use a conditional operator here, but I believe others might dislike its use in situations like this one.
The re-arrangement make a little more noticeable though that the comment isn't quite right either: PV domains necessarily have no
X86_EMU_USE_PIRQ. Maybe "... has no notion of pIRQ"?Or just like below?
Quoted 8 lines
/* * Prevent unmapping when the subject hvm domain has no * X86_EMU_USE_PIRQ */ if ( is_hvm_domain(d) && !has_pirq(d) ) ret = -EOPNOTSUPP; else ret = physdev_unmap_pirq(d, unmap.pirq);Quoted 4 lines
No objection to the slightly changed comment. The code alternative you present is of course functionally identical, yet personally I prefer to have the "good" case on the "if" branch and the "bad" one following "else". I wouldn't insist, though.
OK, will change "good" case on the "if" branch.
Do I need to change "!is_hvm_domain(d)" to "is_pv_domain(d)" ? And then have:
Quoted 8 lines
Quoted 5 lines
/* Only unmapping when the subject domain has a notion of PIRQ */ if ( is_pv_domain(d) || has_pirq(d) ) ret = physdev_unmap_pirq(d, unmap.pirq); else ret = -EOPNOTSUPP;I for one would prefer if you kept using
is_hvm_domain(), for being more precise in this situation.
OK, thanks. Will change in next version.
Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
PATCH v11 3/8 x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0
In reply to Jiqian ChenThe gsi of a passthrough device must be configured for it to be able to be mapped into a hvm domU.
But When dom0 is PVH, the gsis don't get registered, it causes the info of apic, pin and irq not be added into irq_2_pin list, and the handler of irq_desc is not set, then when passthrough a device, setting ioapic affinity and vector will fail.
To fix above problem, on Linux kernel side, a new code will need to call PHYSDEVOP_setup_gsi for passthrough devices to register gsi when dom0 is PVH.
So, add PHYSDEVOP_setup_gsi into hvm_physdev_op for above purpose.
Clarify two questions:
First, why the gsi of devices belong to PVH dom0 can work? Because when probe a driver to a normal device, it calls(on linux kernel side) pci_device_probe-> request_threaded_irq-> irq_startup-> __unmask_ioapic-> io_apic_write, then trap into xen side hvmemul_do_io-> hvm_io_intercept-> hvm_process_io_intercept-> vioapic_write_indirect-> vioapic_hwdom_map_gsi-> mp_register_gsi. So that the gsi can be registered.
Second, why the gsi of passthrough device can't work when dom0 is PVH?
Because when assign a device to passthrough, it uses pciback to probe the device, and it calls pcistub_probe->pcistub_seize-> pcistub_init_device-> xen_pcibk_reset_device-> xen_pcibk_control_isr->isr_on, but isr_on is not set, so that the fake IRQ handler is not installed, then the gsi isn't unmasked. What's more, we can see on Xen side, the function vioapic_hwdom_map_gsi-> mp_register_gsi will be called only when the gsi is unmasked, so that the gsi can't work for passthrough device.
--- xen/arch/x86/hvm/hypercall.c | 1 + 1 file changed, 1 insertion(+)
xen/arch/x86/hvm/hypercall.c +1 -0
@@ -86,6 +86,7 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)return -ENOSYS ;break ; +case PHYSDEVOP_setup_gsi:case PHYSDEVOP_pci_mmcfg_reserved:case PHYSDEVOP_pci_device_add:case PHYSDEVOP_pci_device_remove:
Signature · 1 line
2.34.1
Re: [XEN PATCH v11 3/8] x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0
Quoted 3 lines · Jiqian Chen
The gsi of a passthrough device must be configured for it to be able to be mapped into a hvm domU.
But When dom0 is PVH, the gsis don't get registered, it causes
As per below, it's not "don't" but "may not". As the details don't follow right away, you may also want to add something like "(see below)".
Quoted 20 lines
the info of apic, pin and irq not be added into
irq_2_pinlist, and the handler ofirq_descis not set, then when passthrough a device, setting ioapic affinity and vector will fail.To fix above problem, on Linux kernel side, a new code will need to call
PHYSDEVOP_setup_gsifor passthrough devices to register gsi when dom0 is PVH.So, add
PHYSDEVOP_setup_gsiintohvm_physdev_opfor above purpose.Clarify two questions:
First, why the gsi of devices belong to PVH dom0 can work? Because when probe a driver to a normal device, it calls(on linux kernel side) pci_device_probe-> request_threaded_irq-> irq_startup-> __unmask_ioapic->io_apic_write, then trap into xen side hvmemul_do_io-> hvm_io_intercept-> hvm_process_io_intercept-> vioapic_write_indirect-> vioapic_hwdom_map_gsi->mp_register_gsi. So that the gsi can be registered.
Quoted 11 lines
Second, why the gsi of passthrough device can't work when dom0 is PVH?
Because when assign a device to passthrough, it uses pciback to probe the device, and it calls pcistub_probe->pcistub_seize-> pcistub_init_device-> xen_pcibk_reset_device->xen_pcibk_control_isr->isr_on, butisr_onis not set, so that the fake IRQ handler is not installed, then the gsi isn't unmasked. What's more, we can see on Xen side, the function vioapic_hwdom_map_gsi->mp_register_gsiwill be called only when the gsi is unmasked, so that the gsi can't work for passthrough device.
While this provides the requested detail (thanks), personally I find this pretty hard to follow. It would likely be easier if it was written to a larger part in English, rather than in call chain terminology. But I'm not going to insist, unless others would agree with that view of mine.
Jan
Re: [XEN PATCH v11 3/8] x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0
Quoted 8 lines · Jan Beulich
Quoted 3 lines · Jiqian Chen
The gsi of a passthrough device must be configured for it to be able to be mapped into a hvm domU.
But When dom0 is PVH, the gsis don't get registered, it causesAs per below, it's not "don't" but "may not". As the details don't follow right away, you may also want to add something like "(see below)".
OK, will change in next version.
Quoted 33 lines
Quoted 21 lines
the info of apic, pin and irq not be added into
irq_2_pinlist, and the handler ofirq_descis not set, then when passthrough a device, setting ioapic affinity and vector will fail.To fix above problem, on Linux kernel side, a new code will need to call
PHYSDEVOP_setup_gsifor passthrough devices to register gsi when dom0 is PVH.So, add
PHYSDEVOP_setup_gsiintohvm_physdev_opfor above purpose.Clarify two questions:
First, why the gsi of devices belong to PVH dom0 can work? Because when probe a driver to a normal device, it calls(on linux kernel side) pci_device_probe-> request_threaded_irq-> irq_startup-> __unmask_ioapic->io_apic_write, then trap into xen side hvmemul_do_io-> hvm_io_intercept-> hvm_process_io_intercept-> vioapic_write_indirect-> vioapic_hwdom_map_gsi->mp_register_gsi. So that the gsi can be registered.Quoted 11 lines
Second, why the gsi of passthrough device can't work when dom0 is PVH?
Because when assign a device to passthrough, it uses pciback to probe the device, and it calls pcistub_probe->pcistub_seize-> pcistub_init_device-> xen_pcibk_reset_device->xen_pcibk_control_isr->isr_on, butisr_onis not set, so that the fake IRQ handler is not installed, then the gsi isn't unmasked. What's more, we can see on Xen side, the function vioapic_hwdom_map_gsi->mp_register_gsiwill be called only when the gsi is unmasked, so that the gsi can't work for passthrough device.
Quoted 5 lines
While this provides the requested detail (thanks), personally I find this pretty hard to follow. It would likely be easier if it was written to a larger part in English, rather than in call chain terminology. But I'm not going to insist, unless others would agree with that view of mine.
I will add the language description in next version, and also keep the call stack if not necessary to remove.
Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
PATCH v11 4/8 x86/physdev: Return pirq that irq was already mapped to
In reply to Jiqian Chenallocate_pirq is to allocate a pirq for a irq, and it supports to allocate a free pirq(pirq parameter is <0) or a specific pirq (pirq parameter is > 0).
For current code, it has four usecases.
First, pirq>0 and current_pirq>0, (current_pirq means if irq already has a mapped pirq), if pirq==current_pirq means the irq already has mapped to the pirq expected by the caller, it successes, if pirq!=current_pirq means the pirq expected by the caller has been mapped into other irq, it fails.
Second, pirq>0 and current_pirq<0, it means pirq expected by the caller has not been allocated to any irqs, so it can be allocated to caller, it successes.
Third, pirq<0 and current_pirq<0, it means caller want to allocate a free pirq for irq and irq has no mapped pirq, it successes.
Fourth, pirq<0 and current_pirq>0, it means caller want to allocate a free pirq for irq but irq has a mapped pirq, then it returns the negative pirq, so it fails.
The problem is in Fourth, since the irq has a mapped pirq(current_pirq), and the caller doesn't want to allocate a specified pirq to the irq, so the current_pirq should be returned directly in this case, indicating that the allocation is successful. That can help caller to success when caller just want to allocate a free pirq but doesn't know if the irq already has a mapped pirq or not.
--- xen/arch/x86/irq.c | 2 ++ 1 file changed, 2 insertions(+)
xen/arch/x86/irq.c +2 -0
@@ -2897,6 +2897,8 @@ static int allocate_pirq(struct domain *d, int index, int pirq, int irq, d->domain_id, index, pirq, current_pirq);if ( current_pirq <0 )return -EBUSY ; +else +return current_pirq; }else if ( type ==MAP_PIRQ_TYPE_MULTI_MSI ) {
Signature · 1 line
2.34.1
Re: [XEN PATCH v11 4/8] x86/physdev: Return pirq that irq was already mapped to
Quoted 30 lines · Jiqian Chen
allocate_pirqis to allocate a pirq for a irq, and it supports to allocate a free pirq(pirq parameter is <0) or a specific pirq (pirq parameter is > 0).For current code, it has four usecases.
First, pirq>0 and current_pirq>0, (
current_pirqmeans if irq already has a mapped pirq), if pirq==current_pirq means the irq already has mapped to the pirq expected by the caller, it successes, if pirq!=current_pirq means the pirq expected by the caller has been mapped into other irq, it fails.Second, pirq>0 and current_pirq<0, it means pirq expected by the caller has not been allocated to any irqs, so it can be allocated to caller, it successes.
Third, pirq<0 and current_pirq<0, it means caller want to allocate a free pirq for irq and irq has no mapped pirq, it successes.
Fourth, pirq<0 and current_pirq>0, it means caller want to allocate a free pirq for irq but irq has a mapped pirq, then it returns the negative pirq, so it fails.
The problem is in Fourth, since the irq has a mapped
pirq(current_pirq), and the caller doesn't want to allocate a specified pirq to the irq, so thecurrent_pirqshould be returned directly in this case, indicating that the allocation is successful. That can help caller to success when caller just want to allocate a free pirq but doesn't know if the irq already has a mapped pirq or not.
Quoted 3 lines
Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>
I think the change is correct, and actually fixes a regression. You want
which would also have helped reviewing quite a bit. And it likely would also have helped you write a description which is easier to follow. Enumerating all the cases isn't really needed here; what is needed is an explanation of what went wrong in that re-factoring.
xen/arch/x86/irq.c static int allocate_pirq(struct domain *d, int index, int pirq, int irq, · line 2901
Quoted 8 lines
xen/arch/x86/irq.c +2
-0@@ -2897,6 +2897,8 @@ static int allocate_pirq(struct domain *d, int index, int pirq, int irq, d->domain_id, index, pirq, current_pirq);if ( current_pirq <0 )return -EBUSY ; +else +return current_pirq;
Please can this be simply
pirq = current_pirq;
without any "else", and then taking the normal return path. That again is (imo) closer to what was there before.
I would further suggest that you split this fix out of this series and re-submit soon with a for-4.19 tag and with Oleksii Cc-ed, so that this can be considered for inclusion in 4.19.
Jan
Re: [XEN PATCH v11 4/8] x86/physdev: Return pirq that irq was already mapped to
Quoted 55 lines · Jan Beulich
Quoted 30 lines · Jiqian Chen
allocate_pirqis to allocate a pirq for a irq, and it supports to allocate a free pirq(pirq parameter is <0) or a specific pirq (pirq parameter is > 0).For current code, it has four usecases.
First, pirq>0 and current_pirq>0, (
current_pirqmeans if irq already has a mapped pirq), if pirq==current_pirq means the irq already has mapped to the pirq expected by the caller, it successes, if pirq!=current_pirq means the pirq expected by the caller has been mapped into other irq, it fails.Second, pirq>0 and current_pirq<0, it means pirq expected by the caller has not been allocated to any irqs, so it can be allocated to caller, it successes.
Third, pirq<0 and current_pirq<0, it means caller want to allocate a free pirq for irq and irq has no mapped pirq, it successes.
Fourth, pirq<0 and current_pirq>0, it means caller want to allocate a free pirq for irq but irq has a mapped pirq, then it returns the negative pirq, so it fails.
The problem is in Fourth, since the irq has a mapped
pirq(current_pirq), and the caller doesn't want to allocate a specified pirq to the irq, so thecurrent_pirqshould be returned directly in this case, indicating that the allocation is successful. That can help caller to success when caller just want to allocate a free pirq but doesn't know if the irq already has a mapped pirq or not.Quoted 3 lines
Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>I think the change is correct, and actually fixes a regression. You want
Fixes 0762e2502f1f ("x86/physdev: factor out the code to allocate and map a pirq")which would also have helped reviewing quite a bit. And it likely would also have helped you write a description which is easier to follow. Enumerating all the cases isn't really needed here; what is needed is an explanation of what went wrong in that re-factoring.
xen/arch/x86/irq.c static int allocate_pirq(struct domain *d, int index, int pirq, int irq, · line 2901
Quoted 8 lines
xen/arch/x86/irq.c +2
-0@@ -2897,6 +2897,8 @@ static int allocate_pirq(struct domain *d, int index, int pirq, int irq, d->domain_id, index, pirq, current_pirq);if ( current_pirq <0 )return -EBUSY ; +else +return current_pirq;Please can this be simply
Quoted 8 lines
pirq = current_pirq;without any "else", and then taking the normal return path. That again is (imo) closer to what was there before.
I would further suggest that you split this fix out of this series and re-submit soon with a for-4.19 tag and with Oleksii Cc-ed, so that this can be considered for inclusion in 4.19.
Thanks, will split and send today.
Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
PATCH v11 5/8 x86/domctl: Add XEN_DOMCTL_gsi_permission to grant gsi
In reply to Jiqian ChenSome type of domain don't have PIRQs, like PVH, it doesn't do PHYSDEVOP_map_pirq for each gsi. When passthrough a device to guest base on PVH dom0, callstack pci_add_dm_done->XEN_DOMCTL_irq_permission will fail at function domain_pirq_to_irq, because PVH has no mapping of gsi, pirq and irq on Xen side.
What's more, current hypercall XEN_DOMCTL_irq_permission requires passing in pirq, it is not suitable for dom0 that doesn't have PIRQs.
So, add a new hypercall XEN_DOMCTL_gsi_permission to grant the permission of irq(translate from gsi) to dumU when dom0 has no PIRQs.
--- xen/arch/x86/domctl.c | 33 ++++++++++++++++++++++++++++++ xen/arch/x86/include/asm/io_apic.h | 2 ++ xen/arch/x86/io_apic.c | 17 +++++++++++++++ xen/arch/x86/mpparse.c | 3 +-- xen/include/public/domctl.h | 8 ++++++++ xen/xsm/flask/hooks.c | 1 + 6 files changed, 62 insertions(+), 2 deletions(-)
xen/arch/x86/domctl.c +33 -0
@@ -36,6 +36,7 @@#include <asm/xstate.h> #include <asm/psr.h> #include <asm/cpu-policy.h> +#include <asm/io_apic.h> static int update_domain_cpu_policy (struct domain *d,xen_domctl_cpu_policy_t *xdpc)
@@ -237,6 +238,38 @@ long arch_do_domctl(break ; } +case XEN_DOMCTL_gsi_permission: + { +int irq; +uint8_t mask =1 ; +unsigned int gsi = domctl->u.gsi_permission.gsi; +bool allow = domctl->u.gsi_permission.allow_access; + +/* Check all bits and pads are zero except lowest bit */ + ret = -EINVAL ; +if ( domctl->u.gsi_permission.allow_access & ( !mask ) ) + goto gsi_permission_out; +for ( i =0 ; i <ARRAY_SIZE (domctl->u.gsi_permission.pad); ++i ) +if ( domctl->u.gsi_permission.pad[i] ) + goto gsi_permission_out; + +if ( gsi >= nr_irqs_gsi || ( irq =gsi_2_irq (gsi) ) <0 ) + goto gsi_permission_out; + + ret = -EPERM ; +if ( !irq_access_permitted (currd, irq) || +xsm_irq_permission (XSM_HOOK , d, irq, allow) ) + goto gsi_permission_out; + +if ( allow ) + ret =irq_permit_access (d, irq); +else + ret =irq_deny_access (d, irq); + + gsi_permission_out: +break ; + } +case XEN_DOMCTL_getpageframeinfo3: {unsigned int num = domctl->u.getpageframeinfo3.num;
xen/arch/x86/include/asm/io_apic.h +2 -0
@@ -213,5 +213,7 @@ unsigned highest_gsi(void);int ioapic_guest_read (unsigned long physbase,unsigned int reg,u32 *pval);int ioapic_guest_write (unsigned long physbase,unsigned int reg,u32 val); +int mp_find_ioapic (int gsi); +int gsi_2_irq (int gsi);#endif
xen/arch/x86/io_apic.c +17 -0
@@ -955,6 +955,23 @@ static int pin_2_irq(int idx, int apic, int pin)return irq; } +int gsi_2_irq (int gsi) +{ +int ioapic, pin, irq; + + ioapic =mp_find_ioapic (gsi); +if ( ioapic <0 ) +return -EINVAL ; + + pin = gsi -io_apic_gsi_base (ioapic); + + irq =apic_pin_2_gsi_irq (ioapic, pin); +if ( irq <=0 ) +return -EINVAL ; + +return irq; +} +static inline int IO_APIC_irq_trigger (int irq) {int apic, idx, pin;
xen/arch/x86/mpparse.c +1 -2
@@ -841,8 +841,7 @@ static struct mp_ioapic_routing { } mp_ioapic_routing[MAX_IO_APICS ]; -static int mp_find_ioapic ( -int gsi) +int mp_find_ioapic (int gsi) {unsigned int i;
xen/include/public/domctl.h +8 -0
@@ -464,6 +464,12 @@ struct xen_domctl_irq_permission {uint8_t pad[3 ]; }; +/* XEN_DOMCTL_gsi_permission */ +struct xen_domctl_gsi_permission { +uint32_t gsi; +uint8_t allow_access;/* flag to specify enable/disable of x86 gsi access */ +uint8_t pad[3 ]; +};/* XEN_DOMCTL_iomem_permission */ struct xen_domctl_iomem_permission {
@@ -1306,6 +1312,7 @@ struct xen_domctl {#define XEN_DOMCTL_get_paging_mempool_size 85#define XEN_DOMCTL_set_paging_mempool_size 86#define XEN_DOMCTL_dt_overlay 87 +#define XEN_DOMCTL_gsi_permission 88#define XEN_DOMCTL_gdbsx_guestmemio 1000#define XEN_DOMCTL_gdbsx_pausevcpu 1001#define XEN_DOMCTL_gdbsx_unpausevcpu 1002
@@ -1328,6 +1335,7 @@ struct xen_domctl {struct xen_domctl_setdomainhandle setdomainhandle;struct xen_domctl_setdebugging setdebugging;struct xen_domctl_irq_permission irq_permission; +struct xen_domctl_gsi_permission gsi_permission;struct xen_domctl_iomem_permission iomem_permission;struct xen_domctl_ioport_permission ioport_permission;struct xen_domctl_hypercall_init hypercall_init;
xen/xsm/flask/hooks.c +1 -0
@@ -685,6 +685,7 @@ static int cf_check flask_domctl(struct domain *d, int cmd)case XEN_DOMCTL_shadow_op:case XEN_DOMCTL_ioport_permission:case XEN_DOMCTL_ioport_mapping: +case XEN_DOMCTL_gsi_permission:#endif #ifdef CONFIG_HAS_PASSTHROUGH /*
Signature · 1 line
2.34.1
Re: [XEN PATCH v11 5/8] x86/domctl: Add XEN_DOMCTL_gsi_permission to grant gsi
Quoted 5 lines · Jiqian Chen
xen/arch/x86/domctl.c +1
-0@@ -237,6 +238,38 @@ long arch_do_domctl(break ; } +case XEN_DOMCTL_gsi_permission:
xen/arch/x86/domctl.c long arch_do_domctl( · line 251
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+ { +int irq; +uint8_t mask =1 ; +unsigned int gsi = domctl->u.gsi_permission.gsi; +bool allow = domctl->u.gsi_permission.allow_access; + +/* Check all bits and pads are zero except lowest bit */ + ret = -EINVAL ; +if ( domctl->u.gsi_permission.allow_access & ( !mask ) ) + goto gsi_permission_out;
I'm pretty sure that if you had, as would have been expected, added a #define to the public header for just the low bit you assign meaning to, you wouldn't have caused yourself problems here. For one, the initializer for "allow" will be easy to miss if meaning is assigned to another of the bits. It sadly _still_ takes the full 8 bits and converts those to a boolean. And then the check here won't work either. I don't see what use the local variable "mask" is, but at the very least I expect in place of ! you mean ~ really.
xen/arch/x86/domctl.c long arch_do_domctl( · line 256
Quoted 5 lines
xen/arch/x86/domctl.c +5
-0+for ( i =0 ; i <ARRAY_SIZE (domctl->u.gsi_permission.pad); ++i ) +if ( domctl->u.gsi_permission.pad[i] ) + goto gsi_permission_out; + +if ( gsi >= nr_irqs_gsi || ( irq =gsi_2_irq (gsi) ) <0 )
nr_irqs_gsi is the upper bound on IRQs representing a GSI; as said before, GSIs and IRQs are different number spaces, and hence you can't compare gsi against nr_irqs_gsi. The (inclusive) upper bound on (valid) GSIs is mp_ioapic_routing[nr_ioapics - 1].gsi_end, or the return value of highest_gsi().
Also, style nit: Blanks belong immediately inside parentheses only for the outer pair of control statements; no inner expressions should have them this way.
Finally I'd like to ask that you use "<= 0", as we do in various places elsewhere. IRQ0 is the timer interrupt; we never want to have that used by any entity outside of Xen itself.
xen/include/public/domctl.h struct xen_domctl_irq_permission { · line 470
Quoted 10 lines
xen/include/public/domctl.h +4
-0@@ -464,6 +464,12 @@ struct xen_domctl_irq_permission {uint8_t pad[3 ]; }; +/* XEN_DOMCTL_gsi_permission */ +struct xen_domctl_gsi_permission { +uint32_t gsi; +uint8_t allow_access;/* flag to specify enable/disable of x86 gsi access */
See above. It's not the field that serves as a flag for the purpose you state, but just the low bit. You want to rename the field (flags?) and #define a suitable constant.
Jan
Re: [XEN PATCH v11 5/8] x86/domctl: Add XEN_DOMCTL_gsi_permission to grant gsi
Quoted 32 lines · Jan Beulich
Quoted 5 lines · Jiqian Chen
xen/arch/x86/domctl.c +1
-0@@ -237,6 +238,38 @@ long arch_do_domctl(break ; } +case XEN_DOMCTL_gsi_permission:xen/arch/x86/domctl.c long arch_do_domctl( · line 251
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+ { +int irq; +uint8_t mask =1 ; +unsigned int gsi = domctl->u.gsi_permission.gsi; +bool allow = domctl->u.gsi_permission.allow_access; + +/* Check all bits and pads are zero except lowest bit */ + ret = -EINVAL ; +if ( domctl->u.gsi_permission.allow_access & ( !mask ) ) + goto gsi_permission_out;I'm pretty sure that if you had, as would have been expected, added a #define to the public header for just the low bit you assign meaning to, you wouldn't have caused yourself problems here. For one, the initializer for "allow" will be easy to miss if meaning is assigned to another of the bits. It sadly _still_ takes the full 8 bits and converts those to a boolean. And then the check here won't work either. I don't see what use the local variable "mask" is, but at the very least I expect in place of ! you mean ~ really.
xen/arch/x86/domctl.c long arch_do_domctl( · line 256
Quoted 5 lines
xen/arch/x86/domctl.c +5
-0+for ( i =0 ; i <ARRAY_SIZE (domctl->u.gsi_permission.pad); ++i ) +if ( domctl->u.gsi_permission.pad[i] ) + goto gsi_permission_out; + +if ( gsi >= nr_irqs_gsi || ( irq =gsi_2_irq (gsi) ) <0 )
Quoted 5 lines
nr_irqs_gsiis the upper bound on IRQs representing a GSI; as said before, GSIs and IRQs are different number spaces, and hence you can't compare gsi againstnr_irqs_gsi. The (inclusive) upper bound on (valid) GSIs is mp_ioapic_routing[nr_ioapics - 1].gsi_end, or the return value ofhighest_gsi().
Will change to highest_gsi in next version.
Quoted 15 lines
Also, style nit: Blanks belong immediately inside parentheses only for the outer pair of control statements; no inner expressions should have them this way.
Finally I'd like to ask that you use "<= 0", as we do in various places elsewhere. IRQ0 is the timer interrupt; we never want to have that used by any entity outside of Xen itself.
Quoted 6 lines
xen/include/public/domctl.h +0
-0@@ -464,6 +464,12 @@ struct xen_domctl_irq_permission {uint8_t pad[3 ]; };
Quoted 8 lines
xen/include/public/domctl.h struct xen_domctl_irq_permission { · line 470
Quoted 4 lines
xen/include/public/domctl.h +4
-0+/* XEN_DOMCTL_gsi_permission */ +struct xen_domctl_gsi_permission { +uint32_t gsi; +uint8_t allow_access;/* flag to specify enable/disable of x86 gsi access */ See above. It's not the field that serves as a flag for the purpose you state, but just the low bit. You want to rename the field (flags?) and #define a suitable constant.
You mean?
struct xen_domctl_gsi_permission {
uint32_t gsi;#define GSI_PERMISSION_MASK 1
#define GSI_PERMISSION_DISABLE 0
#define GSI_PERMISSION_ENABLE 1
uint8_t access_flag; /* flag to specify enable/disable of x86 gsi access */
uint8_t pad[3];
};
Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
Re: [XEN PATCH v11 5/8] x86/domctl: Add XEN_DOMCTL_gsi_permission to grant gsi
Quoted 19 lines · Jiqian
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Jiqian Chen
xen/include/public/domctl.h +0
-0@@ -464,6 +464,12 @@ struct xen_domctl_irq_permission {uint8_t pad[3 ]; };Quoted 8 lines
xen/include/public/domctl.h struct xen_domctl_irq_permission { · line 470
Quoted 4 lines
xen/include/public/domctl.h +4
-0+/* XEN_DOMCTL_gsi_permission */ +struct xen_domctl_gsi_permission { +uint32_t gsi; +uint8_t allow_access;/* flag to specify enable/disable of x86 gsi access */ See above. It's not the field that serves as a flag for the purpose you state, but just the low bit. You want to rename the field (flags?) and #define a suitable constant.
You mean?
Quoted 8 lines
struct xen_domctl_gsi_permission { uint32_t gsi;#define
GSI_PERMISSION_MASK1
#defineGSI_PERMISSION_DISABLE0
#defineGSI_PERMISSION_ENABLE1
uint8_taccess_flag; /* flag to specify enable/disable of x86 gsi access */
uint8_tpad[3];
};
Something along these lines, yes. How far to go is a matter of taste; personally I'd add just a single #define for now. One aspect is important though: The names you chose are inappropriate. At the very least a XEN_ prefix is wanted, to reduce the risk of possible name space collisions. Whether to actually use XEN_DOMCTL_ is perhaps again a matter of taste - the header isn't consistent at all in this regard.
Jan
PATCH v11 6/8 tools/libxc: Allow gsi be mapped into a free pirq
In reply to Jiqian ChenHypercall PHYSDEVOP_map_pirq support to map a gsi into a specific pirq or a free pirq, it depends on the parameter pirq(>0 or <0). But in current xc_physdev_map_pirq, it set *pirq=index when parameter pirq is <0, it causes to force all cases to be mapped to a specific pirq. That has some problems, one is caller can't get a free pirq value, another is that once the pecific pirq was already mapped to other gsi, then it will fail.
So, change xc_physdev_map_pirq to allow to pass negative parameter in and then get a free pirq.
--- tools/libs/ctrl/xc_physdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libs/ctrl/xc_physdev.c +1 -1
@@ -50,7 +50,7 @@ int xc_physdev_map_pirq(xc_interface *xch, map.domid = domid; map.type =MAP_PIRQ_TYPE_GSI ; map.index = index; - map.pirq = *pirq <0 ? index : *pirq; + map.pirq = *pirq; rc =do_physdev_op (xch, PHYSDEVOP_map_pirq, &map,sizeof (map));
Signature · 1 line
2.34.1
Re: [XEN PATCH v11 6/8] tools/libxc: Allow gsi be mapped into a free pirq
Quoted 23 lines · Jiqian Chen
Hypercall
PHYSDEVOP_map_pirqsupport to map a gsi into a specific pirq or a free pirq, it depends on the parameter pirq(>0 or <0). But in currentxc_physdev_map_pirq, it set *pirq=index when parameter pirq is <0, it causes to force all cases to be mapped to a specific pirq. That has some problems, one is caller can't get a free pirq value, another is that once the pecific pirq was already mapped to other gsi, then it will fail.So, change
xc_physdev_map_pirqto allow to pass negative parameter in and then get a free pirq.Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>--- tools/libs/ctrl/xc_physdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/libs/ctrl/xc_physdev.c +0
-0
tools/libs/ctrl/xc_physdev.c int xc_physdev_map_pirq(xc_interface *xch, · line 56
Quoted 8 lines
tools/libs/ctrl/xc_physdev.c +1
-1@@ -50,7 +50,7 @@ int xc_physdev_map_pirq(xc_interface *xch, map.domid = domid; map.type =MAP_PIRQ_TYPE_GSI ; map.index = index; - map.pirq = *pirq <0 ? index : *pirq; + map.pirq = *pirq; rc =do_physdev_op (xch, PHYSDEVOP_map_pirq, &map,sizeof (map));
This is a functional change to existing callers, without any kind of clarification whether this changed behavior is actually okay for them.
Jan
Re: [XEN PATCH v11 6/8] tools/libxc: Allow gsi be mapped into a free pirq
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Jiqian Chen
Hypercall
PHYSDEVOP_map_pirqsupport to map a gsi into a specific pirq or a free pirq, it depends on the parameter pirq(>0 or <0). But in currentxc_physdev_map_pirq, it set *pirq=index when parameter pirq is <0, it causes to force all cases to be mapped to a specific pirq. That has some problems, one is caller can't get a free pirq value, another is that once the pecific pirq was already mapped to other gsi, then it will fail.So, change
xc_physdev_map_pirqto allow to pass negative parameter in and then get a free pirq.Signed-off-by Jiqian Chen <[email protected]>Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>--- tools/libs/ctrl/xc_physdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/libs/ctrl/xc_physdev.c +1
-1@@ -50,7 +50,7 @@ int xc_physdev_map_pirq(xc_interface *xch, map.domid = domid; map.type =MAP_PIRQ_TYPE_GSI ; map.index = index; - map.pirq = *pirq <0 ? index : *pirq; + map.pirq = *pirq;
Quoted 5 lines
tools/libs/ctrl/xc_physdev.c int xc_physdev_map_pirq(xc_interface *xch, · line 56
Quoted 2 lines
tools/libs/ctrl/xc_physdev.c +0
-0rc =do_physdev_op (xch, PHYSDEVOP_map_pirq, &map,sizeof (map));This is a functional change to existing callers, without any kind of clarification whether this changed behavior is actually okay for them.
Make sense.
There are three callers pci_add_dm_done, libxl__arch_domain_map_irq and pyxc_physdev_map_pirq, I know how to clarify the first two, but the last one, I have no idea.
Hi Marek,
Will this patch break the existing behavior of pyxc_physdev_map_pirq, and why?
Quoted 2 lines
Jan
Signature · 2 lines
Best regards, Jiqian Chen.
RFC PATCH v11 7/8 tools: Add new function to get gsi from dev
In reply to Jiqian ChenWhen passthrough a device to domU, QEMU and xl tools use its gsi number to do pirq mapping, see QEMU code xen_pt_realize->xc_physdev_map_pirq, and xl code pci_add_dm_done->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq, that is wrong, because irq is not equal with gsi, they are in different spaces, so pirq mapping fails.
And in current codes, there is no method to get gsi for userspace. For above purpose, add new function to get gsi, and the corresponding ioctl is implemented on linux kernel side.
---
RFC: it needs to wait for the corresponding third patch on linux kernel side to be merged.
https://lore.kernel.org/xen-devel/[email protected]/
This patch must be merged after the patch on linux kernel side
--- tools/include/xen-sys/Linux/privcmd.h | 7 ++++++ tools/include/xenctrl.h | 2 ++ tools/libs/ctrl/xc_physdev.c | 35 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
tools/include/xen-sys/Linux/privcmd.h +7 -0
@@ -95,6 +95,11 @@ typedef struct privcmd_mmap_resource {__u64 addr; } privcmd_mmap_resource_t; +typedef struct privcmd_gsi_from_pcidev { +__u32 sbdf; +__u32 gsi; +}privcmd_gsi_from_pcidev_t ; +/* * @cmd: IOCTL_PRIVCMD_HYPERCALL * @arg: &privcmd_hypercall_t
@@ -114,6 +119,8 @@ typedef struct privcmd_mmap_resource { _IOC(_IOC_NONE, 'P', 6, sizeof(domid_t))#define IOCTL_PRIVCMD_MMAP_RESOURCE \ _IOC(_IOC_NONE, 'P', 7, sizeof(privcmd_mmap_resource_t)) +#define IOCTL_PRIVCMD_GSI_FROM_PCIDEV \ + _IOC(_IOC_NONE, 'P', 10, sizeof(privcmd_gsi_from_pcidev_t))#define IOCTL_PRIVCMD_UNIMPLEMENTED \ _IOC(_IOC_NONE, 'P', 0xFF, 0)
tools/include/xenctrl.h +2 -0
@@ -1641,6 +1641,8 @@ int xc_physdev_unmap_pirq(xc_interface *xch,uint32_t domid, int pirq); +int xc_physdev_gsi_from_pcidev (xc_interface *xch,uint32_t sbdf); +/* * LOGGING AND ERROR REPORTING */
tools/libs/ctrl/xc_physdev.c +35 -0
@@ -111,3 +111,38 @@ int xc_physdev_unmap_pirq(xc_interface *xch,return rc; } +int xc_physdev_gsi_from_pcidev (xc_interface *xch,uint32_t sbdf) +{ +int rc =-1 ; + +#if defined(__linux__) +int fd; +privcmd_gsi_from_pcidev_t dev_gsi = { + .sbdf = sbdf, + .gsi =0 , + }; + + fd =open ("/dev/xen/privcmd" ,O_RDWR ); + +if (fd <0 && (errno ==ENOENT || errno ==ENXIO || errno ==ENODEV )) { +/* Fallback to /proc/xen/privcmd */ + fd =open ("/proc/xen/privcmd" ,O_RDWR ); + } + +if (fd <0 ) { +PERROR ("Could not obtain handle on privileged command interface" ); +return rc; + } + + rc =ioctl (fd,IOCTL_PRIVCMD_GSI_FROM_PCIDEV , &dev_gsi); +close (fd); + +if (rc) { +PERROR ("Failed to get gsi from dev" ); + }else { + rc = dev_gsi.gsi; + } +#endif + +return rc; +}
Signature · 1 line
2.34.1
Re: [RFC XEN PATCH v11 7/8] tools: Add new function to get gsi from dev
Quoted 31 lines · Jiqian Chen
tools/libs/ctrl/xc_physdev.c +25
-0@@ -111,3 +111,38 @@ int xc_physdev_unmap_pirq(xc_interface *xch,return rc; } +int xc_physdev_gsi_from_pcidev (xc_interface *xch,uint32_t sbdf) +{ +int rc =-1 ; + +#if defined(__linux__) +int fd; +privcmd_gsi_from_pcidev_t dev_gsi = { + .sbdf = sbdf, + .gsi =0 , + }; + + fd =open ("/dev/xen/privcmd" ,O_RDWR ); + +if (fd <0 && (errno ==ENOENT || errno ==ENXIO || errno ==ENODEV )) { +/* Fallback to /proc/xen/privcmd */ + fd =open ("/proc/xen/privcmd" ,O_RDWR ); + } + +if (fd <0 ) { +PERROR ("Could not obtain handle on privileged command interface" ); +return rc; + } + + rc =ioctl (fd,IOCTL_PRIVCMD_GSI_FROM_PCIDEV , &dev_gsi); +close (fd);
tools/libs/ctrl/xc_physdev.c int xc_physdev_unmap_pirq(xc_interface *xch, · line 148
Quoted 10 lines
tools/libs/ctrl/xc_physdev.c +10
-0+ +if (rc) { +PERROR ("Failed to get gsi from dev" ); + }else { + rc = dev_gsi.gsi; + } +#endif + +return rc; +}
I realize Anthony had asked to move this out of libxencall, yet doing it like this (without really abstracting away the OS specifics) doesn't look quite right either. In particular the opening of /dev/xen/privcmd looks questionable to now have yet another instance in yet another library. Couldn't we split osdep_xencall_open(), making available its former half for use here and in the other two libraries? Of course that'll still leave the ioctl() invocation, which necessarily is OS-specific, too.
Jan
Re: [RFC XEN PATCH v11 7/8] tools: Add new function to get gsi from dev
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Jiqian Chen
tools/libs/ctrl/xc_physdev.c +34
-0@@ -111,3 +111,38 @@ int xc_physdev_unmap_pirq(xc_interface *xch,return rc; } +int xc_physdev_gsi_from_pcidev (xc_interface *xch,uint32_t sbdf) +{ +int rc =-1 ; + +#if defined(__linux__) +int fd; +privcmd_gsi_from_pcidev_t dev_gsi = { + .sbdf = sbdf, + .gsi =0 , + }; + + fd =open ("/dev/xen/privcmd" ,O_RDWR ); + +if (fd <0 && (errno ==ENOENT || errno ==ENXIO || errno ==ENODEV )) { +/* Fallback to /proc/xen/privcmd */ + fd =open ("/proc/xen/privcmd" ,O_RDWR ); + } + +if (fd <0 ) { +PERROR ("Could not obtain handle on privileged command interface" ); +return rc; + } + + rc =ioctl (fd,IOCTL_PRIVCMD_GSI_FROM_PCIDEV , &dev_gsi); +close (fd); + +if (rc) { +PERROR ("Failed to get gsi from dev" ); + }else { + rc = dev_gsi.gsi; + } +#endif + +return rc;
Quoted 8 lines
tools/libs/ctrl/xc_physdev.c int xc_physdev_unmap_pirq(xc_interface *xch, · line 148
Quoted 1 line
tools/libs/ctrl/xc_physdev.c +1
-0+}I realize Anthony had asked to move this out of libxencall, yet doing it like this (without really abstracting away the OS specifics) doesn't look quite right either. In particular the opening of
/dev/xen/privcmdlooks questionable to now have yet another instance in yet another library. Couldn't we splitosdep_xencall_open(), making available its former half for use here and in the other two libraries?
Hi Anthony, what about your opinion?
Quoted 3 lines
Of course that'll still leave the
ioctl()invocation, which necessarily is OS-specific, too.Jan
Signature · 2 lines
Best regards, Jiqian Chen.
Re: [RFC XEN PATCH v11 7/8] tools: Add new function to get gsi from dev
Hi Anthony,
Quoted 50 lines · Jiqian
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Jiqian Chen
tools/libs/ctrl/xc_physdev.c +34
-0@@ -111,3 +111,38 @@ int xc_physdev_unmap_pirq(xc_interface *xch,return rc; } +int xc_physdev_gsi_from_pcidev (xc_interface *xch,uint32_t sbdf) +{ +int rc =-1 ; + +#if defined(__linux__) +int fd; +privcmd_gsi_from_pcidev_t dev_gsi = { + .sbdf = sbdf, + .gsi =0 , + }; + + fd =open ("/dev/xen/privcmd" ,O_RDWR ); + +if (fd <0 && (errno ==ENOENT || errno ==ENXIO || errno ==ENODEV )) { +/* Fallback to /proc/xen/privcmd */ + fd =open ("/proc/xen/privcmd" ,O_RDWR ); + } + +if (fd <0 ) { +PERROR ("Could not obtain handle on privileged command interface" ); +return rc; + } + + rc =ioctl (fd,IOCTL_PRIVCMD_GSI_FROM_PCIDEV , &dev_gsi); +close (fd); + +if (rc) { +PERROR ("Failed to get gsi from dev" ); + }else { + rc = dev_gsi.gsi; + } +#endif + +return rc;Quoted 8 lines
tools/libs/ctrl/xc_physdev.c int xc_physdev_unmap_pirq(xc_interface *xch, · line 148
Quoted 1 line
tools/libs/ctrl/xc_physdev.c +1
-0+}I realize Anthony had asked to move this out of libxencall, yet doing it like this (without really abstracting away the OS specifics) doesn't look quite right either. In particular the opening of
/dev/xen/privcmdlooks questionable to now have yet another instance in yet another library. Couldn't we splitosdep_xencall_open(), making available its former half for use here and in the other two libraries?
Quoted 1 line
Hi Anthony, what about your opinion?
What's your opinion about taking " opening of /dev/xen/privcmd " as a single function, then use it in this patch and other libraries?
Quoted 5 lines
Quoted 4 lines
Of course that'll still leave the
ioctl()invocation, which necessarily is OS-specific, too.Jan
Signature · 2 lines
Best regards, Jiqian Chen.
RFC PATCH v11 8/8 tools: Add new function to do PIRQ (un)map on PVH dom0
In reply to Jiqian ChenWhen dom0 is PVH, and passthrough a device to dumU, xl will use the gsi number of device to do a pirq mapping, see pci_add_dm_done->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq, that confuses irq and gsi, they are in different space and are not equal, so it will fail when mapping.
To solve this issue, use xc_physdev_gsi_from_dev to get the real gsi and then to map pirq.
Besides, PVH dom doesn't have PIRQ flag, it doesn't do PHYSDEVOP_map_pirq for each gsi. So grant function callstack pci_add_dm_done->XEN_DOMCTL_irq_permission will fail at function domain_pirq_to_irq. And old hypercall XEN_DOMCTL_irq_permission requires passing in pirq, it is not suitable for dom0 that doesn't have PIRQs to grant irq permission.
To solve this issue, use the new hypercall XEN_DOMCTL_gsi_permission to grant the permission of irq( translate from gsi) to dumU when dom0 has no PIRQs.
---
RFC: it needs to wait for the corresponding third patch on linux kernel side to be merged.
https://lore.kernel.org/xen-devel/[email protected]/
This patch must be merged after the patch on linux kernel side
--- tools/include/xenctrl.h | 5 ++ tools/libs/ctrl/xc_domain.c | 15 +++++ tools/libs/light/libxl_arch.h | 4 ++ tools/libs/light/libxl_arm.c | 10 +++ tools/libs/light/libxl_pci.c | 17 ++++++ tools/libs/light/libxl_x86.c | 111 ++++++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+)
tools/include/xenctrl.h +5 -0
@@ -1382,6 +1382,11 @@ int xc_domain_irq_permission(xc_interface *xch,uint32_t pirq, bool allow_access); +int xc_domain_gsi_permission (xc_interface *xch, +uint32_t domid, +uint32_t gsi, +bool allow_access); + int xc_domain_iomem_permission(xc_interface *xch, uint32_t domid, unsigned long first_mfn,
tools/libs/ctrl/xc_domain.c +15 -0
@@ -1394,6 +1394,21 @@ int xc_domain_irq_permission(xc_interface *xch,return do_domctl (xch, &domctl); } +int xc_domain_gsi_permission (xc_interface *xch, +uint32_t domid, +uint32_t gsi, +bool allow_access) +{ +struct xen_domctl domctl = { + .cmd = XEN_DOMCTL_gsi_permission, + .domain = domid, + .u.gsi_permission.gsi = gsi, + .u.gsi_permission.allow_access = allow_access, + }; + +return do_domctl (xch, &domctl); +} + int xc_domain_iomem_permission(xc_interface *xch, uint32_t domid, unsigned long first_mfn,
tools/libs/light/libxl_arch.h +4 -0
@@ -91,6 +91,10 @@ void libxl__arch_update_domain_config(libxl__gc *gc,libxl_domain_config *dst, const libxl_domain_config *src); +_hidden +int libxl__arch_hvm_map_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid); +_hidden +int libxl__arch_hvm_unmap_gsi(libxl__gc *gc, uint32_t sbdf, uint32_t domid);#if defined(__i386__) || defined(__x86_64__)#define LAPIC_BASE_ADDRESS 0xfee00000
tools/libs/light/libxl_arm.c +10 -0
@@ -1774,6 +1774,16 @@ void libxl__arch_update_domain_config(libxl__gc *gc, { } +int libxl__arch_hvm_map_gsi (libxl__gc *gc,uint32_t sbdf,uint32_t domid) +{ +return -1 ; +} + +int libxl__arch_hvm_unmap_gsi (libxl__gc *gc,uint32_t sbdf,uint32_t domid) +{ +return -1 ; +} +/* * Local variables: * mode: C
tools/libs/light/libxl_pci.c +17 -0
@@ -17,6 +17,7 @@#include "libxl_osdeps.h" /* must come before any other headers */ #include "libxl_internal.h" +#include "libxl_arch.h" #define PCI_BDF "%04x:%02x:%02x.%01x"#define PCI_BDF_SHORT "%02x:%02x.%01x"
@@ -1478,6 +1479,16 @@ static void pci_add_dm_done(libxl__egc *egc,fclose (f);if (!pci_supp_legacy_irq ()) goto out_no_irq; + +/* +* When dom0 is PVH and mapping a x86 gsi to pirq for domU, +* should use gsi to grant irq permission. +*/ +if (!libxl__arch_hvm_map_gsi (gc,pci_encode_bdf (pci), domid)) + goto pci_permissive; +else +LOGED (WARN , domid,"libxl__arch_hvm_map_gsi failed (err=%d)" , errno); + sysfs_path =GCSPRINTF (SYSFS_PCI_DEV "/" PCI_BDF "/irq" , pci->domain, pci->bus, pci->dev, pci->func); f =fopen (sysfs_path,"r" );
@@ -1505,6 +1516,7 @@ static void pci_add_dm_done(libxl__egc *egc, }fclose (f); +pci_permissive:/* Don't restrict writes to the PCI config space from this VM */ if (pci->permissive) { if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/permissive",
@@ -2229,6 +2241,11 @@ skip_bar:if (!pci_supp_legacy_irq ()) goto skip_legacy_irq; +if (!libxl__arch_hvm_unmap_gsi (gc,pci_encode_bdf (pci), domid)) + goto skip_legacy_irq; +else +LOGED (WARN , domid,"libxl__arch_hvm_unmap_gsi failed (err=%d)" , errno); + sysfs_path =GCSPRINTF (SYSFS_PCI_DEV "/" PCI_BDF "/irq" , pci->domain, pci->bus, pci->dev, pci->func);
tools/libs/light/libxl_x86.c +111 -0
117 lines not on this page · raw message
Signature · 1 line
2.34.1