Skip to contentSkip to conversation

hw/xen/xen_pt: Call default handler only if no custom one is set

PATCH In review Marek Marczykowski-Górecki 5 participants 15 messages Nov 14, 2022 – Sep 26, 2023 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 hw/xen/xen_pt: Call default handler only if no custom one is set

Call pci_default_write_config() in xen_pt_pci_write_config() only for registers that do not have custom handler, and do that only after resolving them. This is important for two reasons:
1. XenPTRegInfo has ro_mask which needs to be enforced - Xen-specific hooks do that on their own (especially xen_pt_*_reg_write()).
2. Not setting value early allows the hooks to see the old value too.

If it would be only about the first point, setting PCIDevice.wmask would probably be sufficient, but given the second point, restructure those writes.

---
 hw/xen/xen_pt.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
hw/xen/xen_pt.c +21 -1
index 0ec7e52183..269bd26109 100644
@@ -255,6 +255,7 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     uint32_t find_addr = addr;
     XenPTRegInfo *reg = NULL;
     bool wp_flag = false;
+    uint32_t emul_mask = 0, write_val;
 
     if (xen_pt_pci_config_access_check(d, addr, len)) {
         return;
@@ -310,7 +311,6 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     }
 
     memory_region_transaction_begin();
-    pci_default_write_config(d, addr, val, len);
 
     /* adjust the read and write value to appropriate CFC-CFF window */
     read_val <<= (addr & 3) << 3;
@@ -370,6 +370,8 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
                 return;
             }
 
+            emul_mask |= ( (1 << (reg->size * 8) ) - 1 ) << ((find_addr & 3) * 8);
+
             /* calculate next address to find */
             emul_len -= reg->size;
             if (emul_len > 0) {
@@ -396,6 +398,24 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     /* need to shift back before passing them to xen_host_pci_set_block. */
     val >>= (addr & 3) << 3;
 
+    /* store emulated registers that didn't have specific hooks */
+    write_val = val;
+    for (index = 0; emul_mask; index += emul_len) {
+        emul_len = 0;
+        while (emul_mask & 0xff) {
+            emul_len++;
+            emul_mask >>= 8;
+        }
+        if (emul_len) {
+            uint32_t mask = ((1 << (emul_len * 8)) - 1);
+            pci_default_write_config(d, addr, write_val & mask, emul_len);
+            write_val >>= emul_len * 8;
+        } else {
+            emul_mask >>= 8;
+            write_val >>= 8;
+        }
+    }
+
     memory_region_transaction_commit();
 
 out:
Signature · 1 line
2.37.3
#1 lore raw

PATCH 2/2 Do not access /dev/mem in MSI-X PCI passthrough on Xen

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

---
 hw/xen/xen_pt.h     |  1 -
 hw/xen/xen_pt_msi.c | 51 ++++-----------------------------------------
 2 files changed, 4 insertions(+), 48 deletions(-)
hw/xen/xen_pt.h +0 -1
index e7c4316a7d..de4094e7ec 100644
@@ -206,7 +206,6 @@ typedef struct XenPTMSIX {
     uint32_t table_offset_adjust; /* page align mmap */
     uint64_t mmio_base_addr;
     MemoryRegion mmio;
-    void *phys_iomem_base;
     XenPTMSIXEntry msix_entry[];
 } XenPTMSIX;
 
hw/xen/xen_pt_msi.c +4 -47

81 lines not on this page · raw message

Signature · 1 line
2.37.3
#2 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 14 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Quoted 4 lines

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

The commit message ought to go further.  Using /dev/mem like this is buggy anyway, because it is trapped and emulated by Xen in whatever context Qemu is running.  Qemu cannot get the actual hardware value, and even if it could, it would be racy with transient operations needing to mask the vector.

i.e. it's not just nice-to-remote - it's fixing real corner cases.

~Andrew

#3 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

I should have noted that "has been changed in Xen" isn't fully accurate (yet). It refers to this patch:
https://lore.kernel.org/xen-devel/[email protected]/T/#u

Quoted 10 lines
Quoted 9 lines

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

Quoted 7 lines

The commit message ought to go further.  Using /dev/mem like this is buggy anyway, because it is trapped and emulated by Xen in whatever context Qemu is running.  Qemu cannot get the actual hardware value, and even if it could, it would be racy with transient operations needing to mask the vector.

i.e. it's not just nice-to-remote - it's fixing real corner cases.

Good point, I'll extend it.
But for this to work, the Xen patch needs to go in first (which won't happen for 4.17). And also, upstream QEMU probably needs some way to detect whether Xen has the change or not - to work with older versions too.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#4 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

In reply to Marek Marczykowski-Górecki
Quoted 9 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

Quoted 4 lines

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Don't you need to check for new enough Xen for both aspects?

Jan

#5 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 10 lines · Jan Beulich
Quoted 9 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

Quoted 6 lines
Quoted 4 lines

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Don't you need to check for new enough Xen for both aspects?

Yes, see my response to Andrew in the thread. I'm open for suggestions what to check specifically (Xen version directly?).

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#6 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 16 lines · Marek Marczykowski-Górecki
Quoted 15 lines · Jan Beulich
Quoted 13 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Quoted 4 lines
Quoted 1 line

Don't you need to check for new enough Xen for both aspects?

Yes, see my response to Andrew in the thread. I'm open for suggestions what to check specifically (Xen version directly?).

I guess we should first see what changes we (you) end up doing in Xen itself, and then decide whether to surface the new functionality in some kind of feature indicator. Generally I'd prefer to avoid version checks, because they don't fit very well with backports nor the (rare) need to revert something. But sometimes a feature can be probed easily without requiring an explicit feature indicator.

Jan

#7 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

In reply to Marek Marczykowski-Górecki
Quoted 15 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Quoted 4 lines

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

I put the Xen, QEMU, and xen-pciback patches into OpenXT and gave a little test. When pci_permissive=0, iwlwifi fails to load its firmware. With pci_permissive=1, it looks like MSI-X is enabled. (I previously included your libxl allow_interrupt_control patch - that seemed to get regular MSIs working prior to the MSI-X patches.) I also removed the OpenXT equivalent of 0005-Disable-MSI-X-caps.patch. I am testing with Linux 5.4.y, so that could be another factor.

One strange thing is the lspci output. Dom0 shows MSI-X enabled. Meanwhile NDVM (sys-net) does not show the MSI-X capability. If you hexdump -C /sys/bus/pci/devices/$dev/config you can see MSI-X enabled, but you also see that the MSI capability has 00 as the next pointer, so lspci stops parsing.

MSI cap stubdom:
00000040 10 00 92 00 c0 0e 00 00 10 0c 10 00 00 00 00 00 |................| 0x41 -> next 0x00
MSI cap dom0:
00000040 10 80 92 00 c0 0e 00 10 10 0c 10 00 00 00 00 00 |................| 0x41 -> next 0x80

MSI-X:
00000080 11 00 0f 80 00 20 00 00 00 30 00 00 00 00 00 00

AFAIU, the value 0x80 at offset 0x83 is MSI-X Enabled.

I had a boot where assignment failed with the hypervisor printing:
d12: assign (0000:00:14.3) failed (-16)
Rebooting the laptop seemed to clear that.

Regards,
Jason

#8 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 22 lines · Jason Andryuk
Quoted 15 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Quoted 4 lines

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

Quoted 7 lines

I put the Xen, QEMU, and xen-pciback patches into OpenXT and gave a little test. When pci_permissive=0, iwlwifi fails to load its firmware. With pci_permissive=1, it looks like MSI-X is enabled. (I previously included your libxl allow_interrupt_control patch - that seemed to get regular MSIs working prior to the MSI-X patches.) I also removed the OpenXT equivalent of 0005-Disable-MSI-X-caps.patch. I am testing with Linux 5.4.y, so that could be another factor.

Can you confirm the allow_interrupt_control is set by libxl? Also, vanilla 5.4 doesn't have the allow_interrupt_control patch at all, and you may have an earlier version that had "allow_msi_enable" as the sysfs file name.

Quoted 5 lines

One strange thing is the lspci output. Dom0 shows MSI-X enabled. Meanwhile NDVM (sys-net) does not show the MSI-X capability. If you hexdump -C /sys/bus/pci/devices/$dev/config you can see MSI-X enabled, but you also see that the MSI capability has 00 as the next pointer, so lspci stops parsing.

This 00 value is written by Linux[1] (sic!) and then qemu incorrectly allowing the write and happily emulating that zero. The other qemu patch in this series ought to fix that (as in: properly refuse the write), do you have it included?

[1] https://github.com/torvalds/linux/blob/master/drivers/net/wireless/intel/iwlwifi/pcie/drv.c#L1721

Quoted 7 lines

MSI cap stubdom:
00000040 10 00 92 00 c0 0e 00 00 10 0c 10 00 00 00 00 00 |................| 0x41 -> next 0x00
MSI cap dom0:
00000040 10 80 92 00 c0 0e 00 10 10 0c 10 00 00 00 00 00 |................| 0x41 -> next 0x80

Quoted 8 lines

MSI-X:
00000080 11 00 0f 80 00 20 00 00 00 30 00 00 00 00 00 00

AFAIU, the value 0x80 at offset 0x83 is MSI-X Enabled.

I had a boot where assignment failed with the hypervisor printing:
d12: assign (0000:00:14.3) failed (-16)
Rebooting the laptop seemed to clear that.

Zombie of previous domain? Not set as "assignable" first?

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#9 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 31 lines · Marek Marczykowski-Górecki
Quoted 22 lines · Jason Andryuk
Quoted 15 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Quoted 4 lines

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

Quoted 7 lines

I put the Xen, QEMU, and xen-pciback patches into OpenXT and gave a little test. When pci_permissive=0, iwlwifi fails to load its firmware. With pci_permissive=1, it looks like MSI-X is enabled. (I previously included your libxl allow_interrupt_control patch - that seemed to get regular MSIs working prior to the MSI-X patches.) I also removed the OpenXT equivalent of 0005-Disable-MSI-X-caps.patch. I am testing with Linux 5.4.y, so that could be another factor.

Quoted 4 lines

Can you confirm the allow_interrupt_control is set by libxl? Also, vanilla 5.4 doesn't have the allow_interrupt_control patch at all, and you may have an earlier version that had "allow_msi_enable" as the sysfs file name.

Ok, I found what is wrong. Enabling MSI-X is refused, because INTx isn't disabled at this point yet. And apparently I was testing this with permissive=1...

Linux does this:
https://github.com/torvalds/linux/blob/master/drivers/pci/msi/msi.c#L611
In short:
1. Enable MSI-X with MASKALL=1
2. Setup MSI-X table
3. Disable INTx
4. Set MASKALL=0

This patch on top should fix this:
----8<----

drivers/xen/xen-pciback/conf_space_capability.c +1 -1
index 097316a74126..f4c4381de76e 100644
@@ -235,7 +235,7 @@ static int msi_msix_flags_write(struct pci_dev *dev, int offset, u16 new_value,
 	    (new_value ^ old_value) & ~field_config->allowed_bits)
 		return PCIBIOS_SET_FAILED;
 
-	if (new_value & field_config->enable_bit) {
+	if ((new_value & field_config->allowed_bits) == field_config->enable_bit) {
 		/* don't allow enabling together with other interrupt types */
 		int int_type = xen_pcibk_get_interrupt_type(dev);
 

----8<----

Jan, is the above safe? It should prevent clearing MASKALL if INTx isn't disabled, unless I missed something? But also, it will allow enabling MSI-X with MASKALL=1 together with MSI, which I'm not sure if should be allowed.
Alternatively to the above patch, I could allow specifically setting MSIX_FLAGS_ENABLE + MSIX_FLAGS_MASKALL while INTx isn't disabled as a single exception, but at this point I'm not sure if some other driver or OS wouldn't approach this in yet another way.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#10 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 29 lines · Marek Marczykowski-Górecki

Ok, I found what is wrong. Enabling MSI-X is refused, because INTx isn't disabled at this point yet. And apparently I was testing this with permissive=1...

Linux does this:
https://github.com/torvalds/linux/blob/master/drivers/pci/msi/msi.c#L611
In short:
1. Enable MSI-X with MASKALL=1
2. Setup MSI-X table
3. Disable INTx
4. Set MASKALL=0

This patch on top should fix this:
----8<----

drivers/xen/xen-pciback/conf_space_capability.c +1 -2
index 097316a74126..f4c4381de76e 100644
@@ -235,7 +235,7 @@ static int msi_msix_flags_write(struct pci_dev *dev, int offset, u16 new_value,
 	    (new_value ^ old_value) & ~field_config->allowed_bits)
 		return PCIBIOS_SET_FAILED;
 
-	if (new_value & field_config->enable_bit) {
+	if ((new_value & field_config->allowed_bits) == field_config->enable_bit) {
 		/* don't allow enabling together with other interrupt types */
 		int int_type = xen_pcibk_get_interrupt_type(dev);
 
----8<----
Quoted 4 lines

Jan, is the above safe? It should prevent clearing MASKALL if INTx isn't disabled, unless I missed something? But also, it will allow enabling MSI-X with MASKALL=1 together with MSI, which I'm not sure if should be allowed.

While it would probably be okay with what we have now (after your earlier patch introducing allowed_bits), it's likely not going to be correct once further bits would be added to allowed_bits (which clearly is going to be wanted sooner or later, e.g. for multi-vector MSI). Hence I think ...

Quoted 3 lines

Alternatively to the above patch, I could allow specifically setting MSIX_FLAGS_ENABLE + MSIX_FLAGS_MASKALL while INTx isn't disabled as a single exception,

... this is the way to go, and ...

Quoted 2 lines

but at this point I'm not sure if some other driver or OS wouldn't approach this in yet another way.

... I guess we need to further add exceptions as needed - the one further approach I could see is to keep all entry's mask bits set until setting "INTx disable", without using MASKALL.

I'd like to note though that the PCI spec has no such exception. It, however, also doesn't mandate setting "INTx disable" - that's merely a workaround for flawed hardware afaik. (In Xen we also only cross check MSI and MSI-X. IOW we rely on Dom0 anyway when it comes to driving "INTx disable".) Therefore in the end pciback looks to be going too far in enforcing such dependencies.

Thinking of this - what about making the change in xen_pcibk_get_interrupt_type() instead, setting INTERRUPT_TYPE_MSIX only when MASKALL is clear (alongside ENABLE being set)? This would then also cover command_write().

Jan

#11 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

Quoted 40 lines · Jan Beulich
Quoted 29 lines · Marek Marczykowski-Górecki

Ok, I found what is wrong. Enabling MSI-X is refused, because INTx isn't disabled at this point yet. And apparently I was testing this with permissive=1...

Linux does this:
https://github.com/torvalds/linux/blob/master/drivers/pci/msi/msi.c#L611
In short:
1. Enable MSI-X with MASKALL=1
2. Setup MSI-X table
3. Disable INTx
4. Set MASKALL=0

This patch on top should fix this:
----8<----

drivers/xen/xen-pciback/conf_space_capability.c +1 -2
index 097316a74126..f4c4381de76e 100644
@@ -235,7 +235,7 @@ static int msi_msix_flags_write(struct pci_dev *dev, int offset, u16 new_value,
 	    (new_value ^ old_value) & ~field_config->allowed_bits)
 		return PCIBIOS_SET_FAILED;
 
-	if (new_value & field_config->enable_bit) {
+	if ((new_value & field_config->allowed_bits) == field_config->enable_bit) {
 		/* don't allow enabling together with other interrupt types */
 		int int_type = xen_pcibk_get_interrupt_type(dev);
 
----8<----
Quoted 4 lines

Jan, is the above safe? It should prevent clearing MASKALL if INTx isn't disabled, unless I missed something? But also, it will allow enabling MSI-X with MASKALL=1 together with MSI, which I'm not sure if should be allowed.

While it would probably be okay with what we have now (after your earlier patch introducing allowed_bits), it's likely not going to be correct once further bits would be added to allowed_bits (which clearly is going to be wanted sooner or later, e.g. for multi-vector MSI). Hence I think ...

Quoted 5 lines
Quoted 3 lines

Alternatively to the above patch, I could allow specifically setting MSIX_FLAGS_ENABLE + MSIX_FLAGS_MASKALL while INTx isn't disabled as a single exception,

... this is the way to go, and ...

Ok, I'll go this way then.

Quoted 14 lines
Quoted 2 lines

but at this point I'm not sure if some other driver or OS wouldn't approach this in yet another way.

... I guess we need to further add exceptions as needed - the one further approach I could see is to keep all entry's mask bits set until setting "INTx disable", without using MASKALL.

I'd like to note though that the PCI spec has no such exception. It, however, also doesn't mandate setting "INTx disable" - that's merely a workaround for flawed hardware afaik. (In Xen we also only cross check MSI and MSI-X. IOW we rely on Dom0 anyway when it comes to driving "INTx disable".) Therefore in the end pciback looks to be going too far in enforcing such dependencies.

Quoted 4 lines

Thinking of this - what about making the change in xen_pcibk_get_interrupt_type() instead, setting INTERRUPT_TYPE_MSIX only when MASKALL is clear (alongside ENABLE being set)? This would then also cover command_write().

That may be a good idea, but wouldn't cover the case here:
xen_pcibk_get_interrupt_type() at this time returns INTERRUPT_TYPE_INTX only, and setting MSIX_FLAGS_ENABLE is prevented.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#12 lore raw

Re: [PATCH 2/2] Do not access /dev/mem in MSI-X PCI passthrough on Xen

In reply to Marek Marczykowski-Górecki
Quoted 33 lines · Marek Marczykowski-Górecki
Quoted 31 lines · Marek Marczykowski-Górecki
Quoted 29 lines · Jason Andryuk
Quoted 15 lines · Marek Marczykowski-Górecki

The /dev/mem is used for two purposes:
- reading PCI_MSIX_ENTRY_CTRL_MASKBIT
- reading Pending Bit Array (PBA)

The first one was originally done because when Xen did not send all vector ctrl writes to the device model, so QEMU might have outdated old register value. This has been changed in Xen, so QEMU can now use its cached value of the register instead.

The Pending Bit Array (PBA) handling is for the case where it lives on the same page as the MSI-X table itself. Xen has been extended to handle this case too (as well as other registers that may live on those pages), so QEMU handling is not necessary anymore.

Quoted 4 lines

Removing /dev/mem access is useful to work within stubdomain, and necessary when dom0 kernel runs in lockdown mode.

I put the Xen, QEMU, and xen-pciback patches into OpenXT and gave a little test. When pci_permissive=0, iwlwifi fails to load its firmware. With pci_permissive=1, it looks like MSI-X is enabled. (I previously included your libxl allow_interrupt_control patch - that seemed to get regular MSIs working prior to the MSI-X patches.) I also removed the OpenXT equivalent of 0005-Disable-MSI-X-caps.patch. I am testing with Linux 5.4.y, so that could be another factor.

Quoted 4 lines
Quoted 4 lines

Can you confirm the allow_interrupt_control is set by libxl? Also, vanilla 5.4 doesn't have the allow_interrupt_control patch at all, and you may have an earlier version that had "allow_msi_enable" as the sysfs file name.

I backported allow_interrupt_control to 5.4 and that is set properly.

Quoted 19 lines

Ok, I found what is wrong. Enabling MSI-X is refused, because INTx isn't disabled at this point yet. And apparently I was testing this with permissive=1...

Linux does this:
https://github.com/torvalds/linux/blob/master/drivers/pci/msi/msi.c#L611
In short:
1. Enable MSI-X with MASKALL=1
2. Setup MSI-X table
3. Disable INTx
4. Set MASKALL=0

This patch on top should fix this:
----8<----

drivers/xen/xen-pciback/conf_space_capability.c +0 -0
index 097316a74126..f4c4381de76e 100644

drivers/xen/xen-pciback/conf_space_capability.c static int msi_msix_flags_write(struct pci_dev *dev, int offset, u16 new_value,

Quoted 9 lines
drivers/xen/xen-pciback/conf_space_capability.c +1 -2
@@ -235,7 +235,7 @@ static int msi_msix_flags_write(struct pci_dev *dev, int offset, u16 new_value,
            (new_value ^ old_value) & ~field_config->allowed_bits)
                return PCIBIOS_SET_FAILED;
 
-       if (new_value & field_config->enable_bit) {
+       if ((new_value & field_config->allowed_bits) == field_config->enable_bit) {
                /* don't allow enabling together with other interrupt types */
                int int_type = xen_pcibk_get_interrupt_type(dev);
 
----8<----

FWIW, I can confirm this allows enabling MSI-X with permissive=0 for me.

Thanks,
Jason

#13 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 30 lines · Marek Marczykowski-Górecki
hw/xen/xen_pt.c +3 -1
index 0ec7e52183..269bd26109 100644
@@ -255,6 +255,7 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     uint32_t find_addr = addr;
     XenPTRegInfo *reg = NULL;
     bool wp_flag = false;
+    uint32_t emul_mask = 0, write_val;
 
     if (xen_pt_pci_config_access_check(d, addr, len)) {
         return;
@@ -310,7 +311,6 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     }
 
     memory_region_transaction_begin();
-    pci_default_write_config(d, addr, val, len);
 
     /* adjust the read and write value to appropriate CFC-CFF window */
     read_val <<= (addr & 3) << 3;
@@ -370,6 +370,8 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
                 return;
             }
 
+            emul_mask |= ( (1 << (reg->size * 8) ) - 1 ) << ((find_addr & 3) * 8);
+
             /* calculate next address to find */
             emul_len -= reg->size;
             if (emul_len > 0) {

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 403

Quoted 6 lines
hw/xen/xen_pt.c +3 -0
@@ -396,6 +398,24 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     /* need to shift back before passing them to xen_host_pci_set_block. */
     val >>= (addr & 3) << 3;
 
+    /* store emulated registers that didn't have specific hooks */
+    write_val = val;
+    for (index = 0; emul_mask; index += emul_len) {

index isn't used, was it meant to be use for something?

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 406

Quoted 3 lines
hw/xen/xen_pt.c +3 -0
+        emul_len = 0;
+        while (emul_mask & 0xff) {
+            emul_len++;

This seems to count the number of byte that have a hook (xen_pt_find_reg() found a reg_entry).
This loop should count instead the number of bytes for which no reg_entry have been found, right? Shouldn't the loop count when a byte in emul_mask is unset?

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 411

Quoted 5 lines
hw/xen/xen_pt.c +5 -0
+            emul_mask >>= 8;
+        }
+        if (emul_len) {
+            uint32_t mask = ((1 << (emul_len * 8)) - 1);
+            pci_default_write_config(d, addr, write_val & mask, emul_len);

addr isn't updated in the loop, aren't we going to write bytes to the wrong place? If for example "emul_mask == 0x00ff00ff" ?

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 417

Quoted 6 lines
hw/xen/xen_pt.c +6 -0
+            write_val >>= emul_len * 8;
+        } else {
+            emul_mask >>= 8;
+            write_val >>= 8;
+        }
+    }

Thanks,

Signature · 1 line
Anthony PERARD
#14 lore raw
Quoted 34 lines · Anthony PERARD
Quoted 33 lines · Marek Marczykowski-Górecki
hw/xen/xen_pt.c +3 -1
index 0ec7e52183..269bd26109 100644
@@ -255,6 +255,7 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     uint32_t find_addr = addr;
     XenPTRegInfo *reg = NULL;
     bool wp_flag = false;
+    uint32_t emul_mask = 0, write_val;
 
     if (xen_pt_pci_config_access_check(d, addr, len)) {
         return;
@@ -310,7 +311,6 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     }
 
     memory_region_transaction_begin();
-    pci_default_write_config(d, addr, val, len);
 
     /* adjust the read and write value to appropriate CFC-CFF window */
     read_val <<= (addr & 3) << 3;
@@ -370,6 +370,8 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
                 return;
             }
 
+            emul_mask |= ( (1 << (reg->size * 8) ) - 1 ) << ((find_addr & 3) * 8);
+
             /* calculate next address to find */
             emul_len -= reg->size;
             if (emul_len > 0) {
@@ -396,6 +398,24 @@ static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
     /* need to shift back before passing them to xen_host_pci_set_block. */
     val >>= (addr & 3) << 3;
 
Quoted 5 lines

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 403

Quoted 3 lines
hw/xen/xen_pt.c +3 -0
+    /* store emulated registers that didn't have specific hooks */
+    write_val = val;
+    for (index = 0; emul_mask; index += emul_len) {

index isn't used, was it meant to be use for something?

Yes, it should be used as addr + index below.

Quoted 9 lines

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 406

Quoted 3 lines
hw/xen/xen_pt.c +3 -0
+        emul_len = 0;
+        while (emul_mask & 0xff) {
+            emul_len++;

This seems to count the number of byte that have a hook (xen_pt_find_reg() found a reg_entry).
This loop should count instead the number of bytes for which no reg_entry have been found, right? Shouldn't the loop count when a byte in emul_mask is unset?

No, see the patch description - only declared registers should be saved. The patch title is misleading, I'll clarify it.

Quoted 8 lines

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 411

Quoted 5 lines
hw/xen/xen_pt.c +5 -0
+            emul_mask >>= 8;
+        }
+        if (emul_len) {
+            uint32_t mask = ((1 << (emul_len * 8)) - 1);
+            pci_default_write_config(d, addr, write_val & mask, emul_len);

addr isn't updated in the loop, aren't we going to write bytes to the wrong place? If for example "emul_mask == 0x00ff00ff" ?

Indeed, it should be addr + index.

Quoted 11 lines

hw/xen/xen_pt.c static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr, · line 417

Quoted 6 lines
hw/xen/xen_pt.c +6 -0
+            write_val >>= emul_len * 8;
+        } else {
+            emul_mask >>= 8;
+            write_val >>= 8;
+        }
+    }

Thanks,

Signature · 1 line
Anthony PERARD
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab