intel_iommu: fix EIM
cover intel_iommu: fix EIM
intel_iommu exposed EIM (extended interrupt mode) feature, which in turn made the guest think that using x2APIC is a good idea. It was not:
QEMU clamped all addresses to 8 bits (effectively allowing only APIC IDs below 8 in cluster mode) and 0xff was also interpreted as x2APIC broadcast even in physical mode.
This series forbids EIM unless KVM is configured to use full 32 bit addresses and doesn't have the broadcast quirk.
On top of this, it would be great if we had a mechanism that enabled EIM whenever it can be used -- it is disabled by default now.
Peter Xu (1):
intel_iommu: add "eim" property
Radim Krčmář (4):
apic: add global apic_get_class()
apic: add send_msi() to APICCommonClass
intel_iommu: pass whole remapped addresses to apic
intel_iommu: do not allow EIM without KVM support
hw/i386/intel_iommu.c | 41 +++++++++++++++++++++++++++++------------ hw/i386/kvm/apic.c | 19 +++++++++++++------ hw/i386/xen/xen_apic.c | 6 ++++++ hw/intc/apic.c | 6 ++++++ hw/intc/apic_common.c | 14 ++++++++++++++ include/hw/i386/apic_internal.h | 7 +++++++ include/hw/i386/intel_iommu.h | 1 + target-i386/kvm-stub.c | 5 +++++ target-i386/kvm.c | 13 +++++++++++++ target-i386/kvm_i386.h | 1 + 10 files changed, 95 insertions(+), 18 deletions(-)
Signature · 1 line
2.10.0
PATCH 1/5 apic: add global apic_get_class()
Every configuration has only up to one APIC class and we'll be extending the class with a function that can be called without an instanced object, so a direct access to the class is convenient.
--- hw/intc/apic_common.c | 14 ++++++++++++++ include/hw/i386/apic_internal.h | 3 +++ 2 files changed, 17 insertions(+)
hw/intc/apic_common.c +14 -0
@@ -18,6 +18,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/> */ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "qemu-common.h" #include "cpu.h"
@@ -296,6 +297,13 @@ static int apic_load_old(QEMUFile *f, void *opaque, int version_id)static const VMStateDescription vmstate_apic_common; +APICCommonClass *apic_class; + +APICCommonClass *apic_get_class (void ) +{ +return apic_class; +} +static void apic_common_realize (DeviceState *dev,Error **errp) {APICCommonState *s =APIC_COMMON (dev);
@@ -306,6 +314,12 @@ static void apic_common_realize(DeviceState *dev, Error **errp) info =APIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (apic_class && apic_class != info) { +error_report ("All APICs must be of the same class." ); +exit (1 ); + } + apic_class = info; +/* Note: We need at least 1M to map the VAPIC option ROM */ if (!vapic && s->vapic_control &VAPIC_ENABLE_MASK && ram_size >=1024 *1024 ) {
include/hw/i386/apic_internal.h +3 -0
@@ -222,4 +222,7 @@ static inline int apic_get_bit(uint32_t *tab, int index) return !!(tab[i] & mask); } + +APICCommonClass *apic_get_class (void ); +#endif /* QEMU_APIC_INTERNAL_H */
Signature · 1 line
2.10.0
Re: [Qemu-devel] [PATCH 1/5] apic: add global apic_get_class()
Quoted 37 lines · Radim Krčmář
Every configuration has only up to one APIC class and we'll be extending the class with a function that can be called without an instanced object, so a direct access to the class is convenient.
Signed-off-by Radim Krčmář <[email protected]>--- hw/intc/apic_common.c | 14 ++++++++++++++ include/hw/i386/apic_internal.h | 3 +++ 2 files changed, 17 insertions(+)hw/intc/apic_common.c +8
-0@@ -18,6 +18,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/> */ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "qemu-common.h" #include "cpu.h" @@ -296,6 +297,13 @@ static int apic_load_old(QEMUFile *f, void *opaque, int version_id)static const VMStateDescription vmstate_apic_common; +APICCommonClass *apic_class; + +APICCommonClass *apic_get_class (void ) +{ +return apic_class; +} +static void apic_common_realize (DeviceState *dev,Error **errp) {APICCommonState *s =APIC_COMMON (dev);
hw/intc/apic_common.c static void apic_common_realize(DeviceState *dev, Error **errp) · line 320
Quoted 7 lines
hw/intc/apic_common.c +4
-0@@ -306,6 +314,12 @@ static void apic_common_realize(DeviceState *dev, Error **errp) info =APIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (apic_class && apic_class != info) { +error_report ("All APICs must be of the same class." ); +exit (1 ); + }
Can user trigger this error? If not, I'd prefer:
assert(!apic_class || apic_class == info);
Thanks,
-- peterx
Re: [Qemu-devel] [PATCH 1/5] apic: add global apic_get_class()
2016-09-23 17:17+0800, Peter Xu:
Quoted 12 lines · Radim Krčmář
Quoted 11 lines
Every configuration has only up to one APIC class and we'll be extending the class with a function that can be called without an instanced object, so a direct access to the class is convenient.
Signed-off-by Radim Krčmář <[email protected]>---
hw/intc/apic_common.c +0
-0@@ -306,6 +314,12 @@ static void apic_common_realize(DeviceState *dev, Error **errp) info =APIC_COMMON_GET_CLASS (s); info->realize (dev, errp);
Quoted 6 lines
hw/intc/apic_common.c static void apic_common_realize(DeviceState *dev, Error **errp) · line 320
Quoted 4 lines
hw/intc/apic_common.c +4
-0+if (apic_class && apic_class != info) { +error_report ("All APICs must be of the same class." ); +exit (1 ); + }Can user trigger this error? If not, I'd prefer:
Shouldn't be able to.
Quoted 1 line
assert(!apic_class || apic_class == info);
I'll use that, thanks.
PATCH 2/5 apic: add send_msi() to APICCommonClass
In reply to Radim KrčmářThe MMIO based interface to APIC doesn't work well with MSIs that have upper address bits set (remapped x2APIC MSIs). A specialized interface is a quick and dirty way to avoid the shortcoming.
--- hw/i386/kvm/apic.c | 19 +++++++++++++------ hw/i386/xen/xen_apic.c | 6 ++++++ hw/intc/apic.c | 6 ++++++ include/hw/i386/apic_internal.h | 4 ++++ 4 files changed, 29 insertions(+), 6 deletions(-)
hw/i386/kvm/apic.c +13 -6
@@ -168,6 +168,17 @@ static void kvm_apic_external_nmi(APICCommonState *s)run_on_cpu (CPU (s->cpu), do_inject_external_nmi, s); } +static void kvm_send_msi (MSIMessage *msg) +{ +int ret; + + ret =kvm_irqchip_send_msi (kvm_state, *msg); +if (ret <0 ) { +fprintf (stderr,"KVM: injection failed, MSI lost (%s)\n" , +strerror (-ret)); + } +} +static uint64_t kvm_apic_mem_read (void *opaque,hwaddr addr,unsigned size) {
@@ -178,13 +189,8 @@ static void kvm_apic_mem_write(void *opaque, hwaddr addr, uint64_t data, unsigned size) {MSIMessage msg = { .address = addr, .data = data }; -int ret; - ret =kvm_irqchip_send_msi (kvm_state, msg); -if (ret <0 ) { -fprintf (stderr,"KVM: injection failed, MSI lost (%s)\n" , -strerror (-ret)); - } +kvm_send_msi (&msg); }static const MemoryRegionOps kvm_apic_io_ops = {
@@ -231,6 +237,7 @@ static void kvm_apic_class_init(ObjectClass *klass, void *data) k->enable_tpr_reporting = kvm_apic_enable_tpr_reporting; k->vapic_base_update = kvm_apic_vapic_base_update; k->external_nmi = kvm_apic_external_nmi; + k->send_msi = kvm_send_msi; }static const TypeInfo kvm_apic_info = {
hw/i386/xen/xen_apic.c +6 -0
@@ -68,6 +68,11 @@ static void xen_apic_external_nmi(APICCommonState *s) { } +static void xen_send_msi (MSIMessage *msi) +{ +xen_hvm_inject_msi (msi->address, msi->data); +} +static void xen_apic_class_init (ObjectClass *klass,void *data) {APICCommonClass *k =APIC_COMMON_CLASS (klass);
@@ -78,6 +83,7 @@ static void xen_apic_class_init(ObjectClass *klass, void *data) k->get_tpr = xen_apic_get_tpr; k->vapic_base_update = xen_apic_vapic_base_update; k->external_nmi = xen_apic_external_nmi; + k->send_msi = xen_send_msi; }static const TypeInfo xen_apic_info = {
hw/intc/apic.c +6 -0
@@ -900,6 +900,11 @@ static void apic_unrealize(DeviceState *dev, Error **errp) local_apics[s->id] =NULL ; } +static void apic_send_msi_struct (MSIMessage *msi) +{ +apic_send_msi (msi->address, msi->data); +} +static void apic_class_init (ObjectClass *klass,void *data) {APICCommonClass *k =APIC_COMMON_CLASS (klass);
@@ -913,6 +918,7 @@ static void apic_class_init(ObjectClass *klass, void *data) k->external_nmi = apic_external_nmi; k->pre_save = apic_pre_save; k->post_load = apic_post_load; + k->send_msi = apic_send_msi_struct; }static const TypeInfo apic_info = {
include/hw/i386/apic_internal.h +4 -0
@@ -146,6 +146,10 @@ typedef struct APICCommonClassvoid (*pre_save)(APICCommonState *s);void (*post_load)(APICCommonState *s);void (*reset)(APICCommonState *s); +/* send_msi emulates an APIC bus and its proper place would be in a new +* device, but it's convenient to have it here for now. +*/ +void (*send_msi)(MSIMessage *msi); } APICCommonClass;struct APICCommonState {
Signature · 1 line
2.10.0
Re: [Qemu-devel] [PATCH 2/5] apic: add send_msi() to APICCommonClass
Quoted 5 lines · Radim Krčmář
The MMIO based interface to APIC doesn't work well with MSIs that have upper address bits set (remapped x2APIC MSIs). A specialized interface is a quick and dirty way to avoid the shortcoming.
Signed-off-by Radim Krčmář <[email protected]>
-- peterx
Re: [Qemu-devel] [PATCH 2/5] apic: add send_msi() to APICCommonClass
In reply to Radim KrčmářQuoted 86 lines · On Thu, 22 Sep 2016 23:04:29 +0200 Radim Krčmář <[email protected]> wrote:
The MMIO based interface to APIC doesn't work well with MSIs that have upper address bits set (remapped x2APIC MSIs). A specialized interface is a quick and dirty way to avoid the shortcoming.
Signed-off-by Radim Krčmář <[email protected]>--- hw/i386/kvm/apic.c | 19 +++++++++++++------ hw/i386/xen/xen_apic.c | 6 ++++++ hw/intc/apic.c | 6 ++++++ include/hw/i386/apic_internal.h | 4 ++++ 4 files changed, 29 insertions(+), 6 deletions(-)hw/i386/kvm/apic.c +13
-6@@ -168,6 +168,17 @@ static void kvm_apic_external_nmi(APICCommonState *s)run_on_cpu (CPU (s->cpu), do_inject_external_nmi, s); } +static void kvm_send_msi (MSIMessage *msg) +{ +int ret; + + ret =kvm_irqchip_send_msi (kvm_state, *msg); +if (ret <0 ) { +fprintf (stderr,"KVM: injection failed, MSI lost (%s)\n" , +strerror (-ret)); + } +} +static uint64_t kvm_apic_mem_read (void *opaque,hwaddr addr,unsigned size) {@@ -178,13 +189,8 @@ static void kvm_apic_mem_write(void *opaque, hwaddr addr, uint64_t data, unsigned size) {MSIMessage msg = { .address = addr, .data = data }; -int ret; - ret =kvm_irqchip_send_msi (kvm_state, msg); -if (ret <0 ) { -fprintf (stderr,"KVM: injection failed, MSI lost (%s)\n" , -strerror (-ret)); - } +kvm_send_msi (&msg); }static const MemoryRegionOps kvm_apic_io_ops = {@@ -231,6 +237,7 @@ static void kvm_apic_class_init(ObjectClass *klass, void *data) k->enable_tpr_reporting = kvm_apic_enable_tpr_reporting; k->vapic_base_update = kvm_apic_vapic_base_update; k->external_nmi = kvm_apic_external_nmi; + k->send_msi = kvm_send_msi; }static const TypeInfo kvm_apic_info = {hw/i386/xen/xen_apic.c +6
-0@@ -68,6 +68,11 @@ static void xen_apic_external_nmi(APICCommonState *s) { } +static void xen_send_msi (MSIMessage *msi) +{ +xen_hvm_inject_msi (msi->address, msi->data); +} +static void xen_apic_class_init (ObjectClass *klass,void *data) {APICCommonClass *k =APIC_COMMON_CLASS (klass);@@ -78,6 +83,7 @@ static void xen_apic_class_init(ObjectClass *klass, void *data) k->get_tpr = xen_apic_get_tpr; k->vapic_base_update = xen_apic_vapic_base_update; k->external_nmi = xen_apic_external_nmi; + k->send_msi = xen_send_msi; }static const TypeInfo xen_apic_info = {hw/intc/apic.c +0
-0
hw/intc/apic.c static void apic_unrealize(DeviceState *dev, Error **errp) · line 906
Quoted 7 lines
hw/intc/apic.c +4
-0@@ -900,6 +900,11 @@ static void apic_unrealize(DeviceState *dev, Error **errp) local_apics[s->id] =NULL ; } +static void apic_send_msi_struct (MSIMessage *msi) +{ +apic_send_msi (msi->address, msi->data); +}
why not to make apic_send_msi(MSIMessage *msi) instead of adding a wrapper?
Also when interface is switched to send_msi() in 3/5, aren't you loosing following checks in apic_mem_writel():
if (addr > 0xfff || !index) { Quoted 27 lines
hw/intc/apic.c +2
-0+static void apic_class_init (ObjectClass *klass,void *data) {APICCommonClass *k =APIC_COMMON_CLASS (klass);@@ -913,6 +918,7 @@ static void apic_class_init(ObjectClass *klass, void *data) k->external_nmi = apic_external_nmi; k->pre_save = apic_pre_save; k->post_load = apic_post_load; + k->send_msi = apic_send_msi_struct; }static const TypeInfo apic_info = {include/hw/i386/apic_internal.h +4
-0@@ -146,6 +146,10 @@ typedef struct APICCommonClassvoid (*pre_save)(APICCommonState *s);void (*post_load)(APICCommonState *s);void (*reset)(APICCommonState *s); +/* send_msi emulates an APIC bus and its proper place would be in a new +* device, but it's convenient to have it here for now. +*/ +void (*send_msi)(MSIMessage *msi); } APICCommonClass;struct APICCommonState {
Re: [Qemu-devel] [PATCH 2/5] apic: add send_msi() to APICCommonClass
2016-09-26 14:38+0200, Igor Mammedov:
Quoted 92 lines
Quoted 89 lines · On Thu, 22 Sep 2016 23:04:29 +0200 Radim Krčmář <[email protected]> wrote:
The MMIO based interface to APIC doesn't work well with MSIs that have upper address bits set (remapped x2APIC MSIs). A specialized interface is a quick and dirty way to avoid the shortcoming.
Signed-off-by Radim Krčmář <[email protected]>--- hw/i386/kvm/apic.c | 19 +++++++++++++------ hw/i386/xen/xen_apic.c | 6 ++++++ hw/intc/apic.c | 6 ++++++ include/hw/i386/apic_internal.h | 4 ++++ 4 files changed, 29 insertions(+), 6 deletions(-)hw/i386/kvm/apic.c +13
-6@@ -168,6 +168,17 @@ static void kvm_apic_external_nmi(APICCommonState *s)run_on_cpu (CPU (s->cpu), do_inject_external_nmi, s); } +static void kvm_send_msi (MSIMessage *msg) +{ +int ret; + + ret =kvm_irqchip_send_msi (kvm_state, *msg); +if (ret <0 ) { +fprintf (stderr,"KVM: injection failed, MSI lost (%s)\n" , +strerror (-ret)); + } +} +static uint64_t kvm_apic_mem_read (void *opaque,hwaddr addr,unsigned size) {@@ -178,13 +189,8 @@ static void kvm_apic_mem_write(void *opaque, hwaddr addr, uint64_t data, unsigned size) {MSIMessage msg = { .address = addr, .data = data }; -int ret; - ret =kvm_irqchip_send_msi (kvm_state, msg); -if (ret <0 ) { -fprintf (stderr,"KVM: injection failed, MSI lost (%s)\n" , -strerror (-ret)); - } +kvm_send_msi (&msg); }static const MemoryRegionOps kvm_apic_io_ops = {@@ -231,6 +237,7 @@ static void kvm_apic_class_init(ObjectClass *klass, void *data) k->enable_tpr_reporting = kvm_apic_enable_tpr_reporting; k->vapic_base_update = kvm_apic_vapic_base_update; k->external_nmi = kvm_apic_external_nmi; + k->send_msi = kvm_send_msi; }static const TypeInfo kvm_apic_info = {hw/i386/xen/xen_apic.c +6
-0@@ -68,6 +68,11 @@ static void xen_apic_external_nmi(APICCommonState *s) { } +static void xen_send_msi (MSIMessage *msi) +{ +xen_hvm_inject_msi (msi->address, msi->data); +} +static void xen_apic_class_init (ObjectClass *klass,void *data) {APICCommonClass *k =APIC_COMMON_CLASS (klass);@@ -78,6 +83,7 @@ static void xen_apic_class_init(ObjectClass *klass, void *data) k->get_tpr = xen_apic_get_tpr; k->vapic_base_update = xen_apic_vapic_base_update; k->external_nmi = xen_apic_external_nmi; + k->send_msi = xen_send_msi; }static const TypeInfo xen_apic_info = {hw/intc/apic.c +0
-0@@ -900,6 +900,11 @@ static void apic_unrealize(DeviceState *dev, Error **errp) local_apics[s->id] =NULL ; }
Quoted 5 lines
hw/intc/apic.c static void apic_unrealize(DeviceState *dev, Error **errp) · line 906
Quoted 4 lines
hw/intc/apic.c +4
-0+static void apic_send_msi_struct (MSIMessage *msi) +{ +apic_send_msi (msi->address, msi->data); +}why not to make
apic_send_msi(MSIMessage *msi)instead of adding a wrapper?
Good point, I'll change it.
Quoted 4 lines
Also when interface is switched to
send_msi()in 3/5, aren't you loosing following checks inapic_mem_writel():if (addr > 0xfff || !index) {
We don't need them. addr <= 0xfff (the first page) was checked because of the the APIC register page that overlaid the MSI space. I think that the comment in code explains it:
/* MSI and MMIO APIC are at the same memory location,
* but actually not on the global bus: MSI is on PCI bus
* APIC is connected directly to the CPU.
* Mapping them on the global bus happens to work because
* MSI registers are reserved in APIC MMIO and vice versa. */
PATCH 3/5 intel_iommu: pass whole remapped addresses to apic
In reply to Radim KrčmářThe MMIO interface to APIC only allowed 8 bit addresses, which is not enough for 32 bit addresses from EIM remapping.
Intel stored upper 24 bits in the high MSI address, so use the same technique. The technique is also used in KVM MSI interface. Other APICs are unlikely to handle those upper bits.
--- hw/i386/intel_iommu.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
hw/i386/intel_iommu.c +7 -11
@@ -31,6 +31,7 @@#include "hw/i386/x86-iommu.h" #include "hw/pci-host/q35.h" #include "sysemu/kvm.h" +#include "hw/i386/apic_internal.h" /*#define DEBUG_INTEL_IOMMU*/ #ifdef DEBUG_INTEL_IOMMU
@@ -279,18 +280,16 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,static void vtd_generate_interrupt (IntelIOMMUState *s,hwaddr mesg_addr_reg,hwaddr mesg_data_reg) { -hwaddr addr; -uint32_t data; +MSIMessage msi;assert (mesg_data_reg <DMAR_REG_SIZE );assert (mesg_addr_reg <DMAR_REG_SIZE ); - addr =vtd_get_long_raw (s, mesg_addr_reg); - data =vtd_get_long_raw (s, mesg_data_reg); + msi.address =vtd_get_long_raw (s, mesg_addr_reg); + msi.data =vtd_get_long_raw (s, mesg_data_reg);VTD_DPRINTF (FLOG ,"msi: addr 0x%" PRIx64" data 0x%" PRIx32, addr, data); -address_space_stl_le (&address_space_memory, addr, data, -MEMTXATTRS_UNSPECIFIED ,NULL ); +apic_get_class ()->send_msi (&msi); }/* Generate a fault event to software via MSI if conditions are met.
@@ -2127,6 +2126,7 @@ static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out) msg.dest_mode = irq->dest_mode; msg.redir_hint = irq->redir_hint; msg.dest = irq->dest; + msg.__addr_hi = irq->dest &0xffffff00 ; msg.__addr_head =cpu_to_le32 (0xfee );/* Keep this from original MSI address bits */ msg.__not_used = irq->msi_addr_last_bits;
@@ -2275,11 +2275,7 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr," for device sid 0x%04x" , to.address, to.data, sid); -if (dma_memory_write (&address_space_memory, to.address, - &to.data, size)) { -VTD_DPRINTF (GENERAL ,"error: fail to write 0x%" PRIx64 -" value 0x%" PRIx32, to.address, to.data); - } +apic_get_class ()->send_msi (&to);return MEMTX_OK ; }
Signature · 1 line
2.10.0
Re: [Qemu-devel] [PATCH 3/5] intel_iommu: pass whole remapped addresses to apic
On Thu, Sep 22, 2016 at 11:04:30PM +0200, Radim Krčmář wrote:
[...]
Quoted 7 lines
hw/i386/intel_iommu.c +1
-2@@ -279,18 +280,16 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,static void vtd_generate_interrupt (IntelIOMMUState *s,hwaddr mesg_addr_reg,hwaddr mesg_data_reg) { -hwaddr addr; -uint32_t data; +MSIMessage msi;
hw/i386/intel_iommu.c static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, · line 291
Quoted 10 lines
hw/i386/intel_iommu.c +2
-2assert (mesg_data_reg <DMAR_REG_SIZE );assert (mesg_addr_reg <DMAR_REG_SIZE ); - addr =vtd_get_long_raw (s, mesg_addr_reg); - data =vtd_get_long_raw (s, mesg_data_reg); + msi.address =vtd_get_long_raw (s, mesg_addr_reg); + msi.data =vtd_get_long_raw (s, mesg_data_reg);VTD_DPRINTF (FLOG ,"msi: addr 0x%" PRIx64" data 0x%" PRIx32, addr, data);
Need to fix addr/data as well?
-- peterx
Re: [Qemu-devel] [PATCH 3/5] intel_iommu: pass whole remapped addresses to apic
2016-09-23 17:41+0800, Peter Xu:
Quoted 15 lines · Radim Krčmář
[...]
Quoted 11 lines
hw/i386/intel_iommu.c +1
-2@@ -279,18 +280,16 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,static void vtd_generate_interrupt (IntelIOMMUState *s,hwaddr mesg_addr_reg,hwaddr mesg_data_reg) { -hwaddr addr; -uint32_t data; +MSIMessage msi;assert (mesg_data_reg <DMAR_REG_SIZE );assert (mesg_addr_reg <DMAR_REG_SIZE );
Quoted 8 lines
hw/i386/intel_iommu.c static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, · line 291
Quoted 6 lines
hw/i386/intel_iommu.c +2
-2- addr =vtd_get_long_raw (s, mesg_addr_reg); - data =vtd_get_long_raw (s, mesg_data_reg); + msi.address =vtd_get_long_raw (s, mesg_addr_reg); + msi.data =vtd_get_long_raw (s, mesg_data_reg);VTD_DPRINTF (FLOG ,"msi: addr 0x%" PRIx64" data 0x%" PRIx32, addr, data);Need to fix addr/data as well?
Oops, thanks.
Re: [Qemu-devel] [PATCH 3/5] intel_iommu: pass whole remapped addresses to apic
In reply to Radim KrčmářQuoted 5 lines · On Thu, 22 Sep 2016 23:04:30 +0200 Radim Krčmář <[email protected]> wrote:
The MMIO interface to APIC only allowed 8 bit addresses, which is not enough for 32 bit addresses from EIM remapping.
Intel stored upper 24 bits in the high MSI address, so use the same technique. The technique is also used in KVM MSI interface. Other APICs are unlikely to handle those upper bits.
this series applied upto here fixes:
KVM: injection failed, MSI lost (Operation not permitted)
KVM: injection failed, MSI lost (Operation not permitted)
KVM: injection failed, MSI lost (Operation not permitted)
with EMI enabled by default that I've reported
https://lists.gnu.org/archive/html/qemu-devel/2016-09/msg05459.html
Quoted 62 lines
Signed-off-by Radim Krčmář <[email protected]>--- hw/i386/intel_iommu.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)hw/i386/intel_iommu.c +7
-11@@ -31,6 +31,7 @@#include "hw/i386/x86-iommu.h" #include "hw/pci-host/q35.h" #include "sysemu/kvm.h" +#include "hw/i386/apic_internal.h" /*#define DEBUG_INTEL_IOMMU*/ #ifdef DEBUG_INTEL_IOMMU @@ -279,18 +280,16 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,static void vtd_generate_interrupt (IntelIOMMUState *s,hwaddr mesg_addr_reg,hwaddr mesg_data_reg) { -hwaddr addr; -uint32_t data; +MSIMessage msi;assert (mesg_data_reg <DMAR_REG_SIZE );assert (mesg_addr_reg <DMAR_REG_SIZE ); - addr =vtd_get_long_raw (s, mesg_addr_reg); - data =vtd_get_long_raw (s, mesg_data_reg); + msi.address =vtd_get_long_raw (s, mesg_addr_reg); + msi.data =vtd_get_long_raw (s, mesg_data_reg);VTD_DPRINTF (FLOG ,"msi: addr 0x%" PRIx64" data 0x%" PRIx32, addr, data); -address_space_stl_le (&address_space_memory, addr, data, -MEMTXATTRS_UNSPECIFIED ,NULL ); +apic_get_class ()->send_msi (&msi); }/* Generate a fault event to software via MSI if conditions are met. @@ -2127,6 +2126,7 @@ static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out) msg.dest_mode = irq->dest_mode; msg.redir_hint = irq->redir_hint; msg.dest = irq->dest; + msg.__addr_hi = irq->dest &0xffffff00 ; msg.__addr_head =cpu_to_le32 (0xfee );/* Keep this from original MSI address bits */ msg.__not_used = irq->msi_addr_last_bits;@@ -2275,11 +2275,7 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr," for device sid 0x%04x" , to.address, to.data, sid); -if (dma_memory_write (&address_space_memory, to.address, - &to.data, size)) { -VTD_DPRINTF (GENERAL ,"error: fail to write 0x%" PRIx64 -" value 0x%" PRIx32, to.address, to.data); - } +apic_get_class ()->send_msi (&to);return MEMTX_OK ; }
PATCH 4/5 intel_iommu: add "eim" property
In reply to Radim KrčmářFrom: Peter Xu <[email protected]>
Adding one extra property for intel-iommu device to decide whether we should support EIM bit for IR.
Now we are throwing high 24 bits of dest_id away directly. This will cause interrupt issues with guests that:
- enabled x2apic with cluster mode
- have more than 8 vcpus (so dest_id[31:8] might be nonzero)
Let's make xapic the default one, and for the brave people who would like to try EIM and know the side effects, we can do it by explicitly enabling EIM using:
-device intel-iommu,intremap=on,eim=on
Even after we have x2apic support, it'll still be good if we can provide a way to switch xapic/x2apic from QEMU side for e.g. debugging purpose, which is an alternative for tuning guest kernel boot parameters.
We can switch the default to "on" after x2apic fully supported.
--- hw/i386/intel_iommu.c | 16 +++++++++++++++- include/hw/i386/intel_iommu.h | 1 + 2 files changed, 16 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +15 -1
@@ -2004,6 +2004,11 @@ static const MemoryRegionOps vtd_mem_ops = {static Property vtd_properties[] = {DEFINE_PROP_UINT32 ("version" , IntelIOMMUState, version,0 ), +/* +* TODO: currently EIM is disabled by default. We can enable this +* after fully support x2apic. +*/ +DEFINE_PROP_BOOL ("eim" , IntelIOMMUState, eim_supported, false),DEFINE_PROP_END_OF_LIST (), };
@@ -2360,7 +2365,10 @@ static void vtd_init(IntelIOMMUState *s) s->ecap =VTD_ECAP_QI |VTD_ECAP_IRO ;if (x86_iommu->intr_supported) { - s->ecap |=VTD_ECAP_IR |VTD_ECAP_EIM |VTD_ECAP_MHMV ; + s->ecap |=VTD_ECAP_IR |VTD_ECAP_MHMV ; +if (s->eim_supported) { + s->ecap |=VTD_ECAP_EIM ; + } }vtd_reset_context_cache (s);
@@ -2464,6 +2472,12 @@ static void vtd_realize(DeviceState *dev, Error **errp)/* Pseudo address space under root PCI bus. */ pcms->ioapic_as =vtd_host_dma_iommu (bus, s,Q35_PSEUDO_DEVFN_IOAPIC ); +/* EIM bit requires IR */ +if (s->eim_supported && !x86_iommu->intr_supported) { +error_report ("EIM (Extended Interrupt Mode) bit requires intremap=on" ); +exit (1 ); + } +/* Currently Intel IOMMU IR only support "kernel-irqchip={off|split}" */ if (x86_iommu->intr_supported &&kvm_irqchip_in_kernel () && !kvm_irqchip_is_split ()) {
include/hw/i386/intel_iommu.h +1 -0
@@ -289,6 +289,7 @@ struct IntelIOMMUState {dma_addr_t intr_root;/* Interrupt remapping table pointer */ uint32_t intr_size;/* Number of IR table entries */ bool intr_eime;/* Extended interrupt mode enabled */ +bool eim_supported;/* Whether to allow EIM bit */ };/* Find the VTD Address space associated with the given bus pointer,
Signature · 1 line
2.10.0
PATCH 5/5 intel_iommu: do not allow EIM without KVM support
In reply to Radim KrčmářCluster x2APIC cannot work without KVM's x2apic API when the maximal APIC ID is > 8. Make the code simpler by completely forbidding EIM without KVM's x2apic API.
---
I think it the dependency would be nicer in the eim setter, but the other dependency, for interrupt remapping, isn't there and I didn't venture for reasons.
--- hw/i386/intel_iommu.c | 7 +++++++ target-i386/kvm-stub.c | 5 +++++ target-i386/kvm.c | 13 +++++++++++++ target-i386/kvm_i386.h | 1 + 4 files changed, 26 insertions(+)
hw/i386/intel_iommu.c +7 -0
@@ -32,6 +32,7 @@#include "hw/pci-host/q35.h" #include "sysemu/kvm.h" #include "hw/i386/apic_internal.h" +#include "kvm_i386.h" /*#define DEBUG_INTEL_IOMMU*/ #ifdef DEBUG_INTEL_IOMMU
@@ -2485,6 +2486,12 @@ static void vtd_realize(DeviceState *dev, Error **errp) "kernel-irqchip=on, please use 'split|off'.");exit (1 ); } + +if (s->eim_supported &&kvm_irqchip_in_kernel () && + !kvm_enable_x2apic ()) { +error_report ("EIM requires support from the KVM side (X2APIC_API)." ); +exit (1 ); + } }static void vtd_class_init (ObjectClass *klass,void *data)
target-i386/kvm-stub.c +5 -0
@@ -25,6 +25,11 @@ bool kvm_has_smm(void)return 1 ; } +bool kvm_enable_x2apic (void ) +{ +return false; +} +/* This function is only called inside conditionals which we * rely on the compiler to optimize out when CONFIG_KVM is not * defined.
target-i386/kvm.c +13 -0
@@ -128,6 +128,19 @@ bool kvm_allows_irq0_override(void)return !kvm_irqchip_in_kernel () ||kvm_has_gsi_routing (); } +static bool kvm_x2apic_api_set_flags (uint64_t flags) +{ +KVMState *s =KVM_STATE (current_machine->accelerator); + +return !kvm_vm_enable_cap (s,KVM_CAP_X2APIC_API ,0 , flags); +} + +bool kvm_enable_x2apic (void ) +{ +return kvm_x2apic_api_set_flags (KVM_X2APIC_API_USE_32BIT_IDS | +KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK ); +} +static int kvm_get_tsc (CPUState *cs) {X86CPU *cpu =X86_CPU (cs);
target-i386/kvm_i386.h +1 -0
@@ -41,4 +41,5 @@ int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector,int kvm_device_msix_assign (KVMState *s,uint32_t dev_id);int kvm_device_msix_deassign (KVMState *s,uint32_t dev_id); +bool kvm_enable_x2apic (void );#endif
Signature · 1 line
2.10.0
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
Quoted 87 lines · Radim Krčmář
Cluster x2APIC cannot work without KVM's x2apic API when the maximal APIC ID is > 8. Make the code simpler by completely forbidding EIM without KVM's x2apic API.
Signed-off-by Radim Krčmář <[email protected]>---
I think it the dependency would be nicer in the eim setter, but the other dependency, for interrupt remapping, isn't there and I didn't venture for reasons.--- hw/i386/intel_iommu.c | 7 +++++++ target-i386/kvm-stub.c | 5 +++++ target-i386/kvm.c | 13 +++++++++++++ target-i386/kvm_i386.h | 1 + 4 files changed, 26 insertions(+)hw/i386/intel_iommu.c +7
-0@@ -32,6 +32,7 @@#include "hw/pci-host/q35.h" #include "sysemu/kvm.h" #include "hw/i386/apic_internal.h" +#include "kvm_i386.h" /*#define DEBUG_INTEL_IOMMU*/ #ifdef DEBUG_INTEL_IOMMU @@ -2485,6 +2486,12 @@ static void vtd_realize(DeviceState *dev, Error **errp) "kernel-irqchip=on, please use 'split|off'.");exit (1 ); } + +if (s->eim_supported &&kvm_irqchip_in_kernel () && + !kvm_enable_x2apic ()) { +error_report ("EIM requires support from the KVM side (X2APIC_API)." ); +exit (1 ); + } }static void vtd_class_init (ObjectClass *klass,void *data)target-i386/kvm-stub.c +5
-0@@ -25,6 +25,11 @@ bool kvm_has_smm(void)return 1 ; } +bool kvm_enable_x2apic (void ) +{ +return false; +} +/* This function is only called inside conditionals which we * rely on the compiler to optimize out when CONFIG_KVM is not * defined. target-i386/kvm.c +13
-0@@ -128,6 +128,19 @@ bool kvm_allows_irq0_override(void)return !kvm_irqchip_in_kernel () ||kvm_has_gsi_routing (); } +static bool kvm_x2apic_api_set_flags (uint64_t flags) +{ +KVMState *s =KVM_STATE (current_machine->accelerator); + +return !kvm_vm_enable_cap (s,KVM_CAP_X2APIC_API ,0 , flags); +} + +bool kvm_enable_x2apic (void ) +{ +return kvm_x2apic_api_set_flags (KVM_X2APIC_API_USE_32BIT_IDS | +KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK ); +} +static int kvm_get_tsc (CPUState *cs) {X86CPU *cpu =X86_CPU (cs);target-i386/kvm_i386.h +0
-0
target-i386/kvm_i386.h int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector, · line 45
Quoted 5 lines
target-i386/kvm_i386.h +1
-0@@ -41,4 +41,5 @@ int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector,int kvm_device_msix_assign (KVMState *s,uint32_t dev_id);int kvm_device_msix_deassign (KVMState *s,uint32_t dev_id); +bool kvm_enable_x2apic (void );#endif
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and false otherwise, and pc-2.7 would set eim=off). It means requiring kernel 4.8 by default, but I don't think it's a big deal.
Paolo
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
Quoted 5 lines · Paolo Bonzini
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and false otherwise, and pc-2.7 would set eim=off). It means requiring kernel 4.8 by default, but I don't think it's a big deal.
I think the problem is, even we have KVM support for x2apic, we are still losing QEMU part. And guests with cluster x2apic and >8 vcpus will not working properly on device interrupts, which can be very confusing to people (it can boot, but some devices just don't work properly, and they won't see useful information in guest dmesg).
Thanks,
-- peterx
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
Quoted 11 lines · Peter Xu
Quoted 5 lines · Paolo Bonzini
Quoted 5 lines
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and false otherwise, and pc-2.7 would set eim=off). It means requiring kernel 4.8 by default, but I don't think it's a big deal.I think the problem is, even we have KVM support for x2apic, we are still losing QEMU part. And guests with cluster x2apic and >8 vcpus will not working properly on device interrupts, which can be very confusing to people (it can boot, but some devices just don't work properly, and they won't see useful information in guest dmesg).
Yes, that's why I suggested EIM=on by default.
Paolo
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Peter Xu
Quoted 5 lines · Paolo Bonzini
Quoted 5 lines
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and false otherwise, and pc-2.7 would set eim=off). It means requiring kernel 4.8 by default, but I don't think it's a big deal.
Quoted 7 lines
Quoted 5 lines
I think the problem is, even we have KVM support for x2apic, we are still losing QEMU part. And guests with cluster x2apic and >8 vcpus will not working properly on device interrupts, which can be very confusing to people (it can boot, but some devices just don't work properly, and they won't see useful information in guest dmesg).
Yes, that's why I suggested EIM=on by default.
I am confused. :(
Why not we just keep people from that wrong configuration by default, until we have x2apic in QEMU?
-- peterx
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
Quoted 16 lines · Peter Xu
Quoted 15 lines · Paolo Bonzini
Quoted 11 lines · Peter Xu
Quoted 5 lines · Paolo Bonzini
Quoted 5 lines
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and false otherwise, and pc-2.7 would set eim=off). It means requiring kernel 4.8 by default, but I don't think it's a big deal.I think the problem is, even we have KVM support for x2apic, we are still losing QEMU part. And guests with cluster x2apic and >8 vcpus will not working properly on device interrupts, which can be very confusing to people (it can boot, but some devices just don't work properly, and they won't see useful information in guest dmesg).
Quoted 6 lines
Quoted 1 line
Yes, that's why I suggested EIM=on by default.
I am confused. :(
Why not we just keep people from that wrong configuration by default, until we have x2apic in QEMU?
Do you mean Igor's patches? I expect that they will go in pretty much at the same time as Radim's.
Paolo
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
Quoted 21 lines · Paolo Bonzini
Quoted 18 lines · Peter Xu
Quoted 16 lines · Paolo Bonzini
Quoted 11 lines · Peter Xu
Quoted 5 lines · Paolo Bonzini
Quoted 5 lines
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and false otherwise, and pc-2.7 would set eim=off). It means requiring kernel 4.8 by default, but I don't think it's a big deal.I think the problem is, even we have KVM support for x2apic, we are still losing QEMU part. And guests with cluster x2apic and >8 vcpus will not working properly on device interrupts, which can be very confusing to people (it can boot, but some devices just don't work properly, and they won't see useful information in guest dmesg).
Yes, that's why I suggested EIM=on by default.
Quoted 7 lines
Quoted 4 lines
I am confused. :(
Why not we just keep people from that wrong configuration by default, until we have x2apic in QEMU?
Do you mean Igor's patches? I expect that they will go in pretty much at the same time as Radim's.
Ah! Yes we have x2apic all here... So I totally agree we should set it on as default.
(My mistake of not noticing the truth)
Thanks,
-- peterx
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
In reply to Paolo Bonzini2016-09-23 11:27+0200, Paolo Bonzini:
Quoted 3 lines
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on and
Sounds good.
Quoted 1 line
false otherwise, and pc-2.7 would set eim=off).
What about eim=on in pc-2.7, to avoid breaking migration?
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
----- Original Message -----
Quoted 7 lines
From: "Radim Krčmář" <[email protected]>
To: "Paolo Bonzini" <[email protected]>
Cc: [email protected], "Peter Xu" <[email protected]>, "Igor Mammedov" <[email protected]>, "Richard Henderson" <[email protected]>, "Eduardo Habkost" <[email protected]>, "Michael S. Tsirkin" <[email protected]>
Sent: Tuesday, September 27, 2016 4:01:39 PM
Subject: Re: [PATCH 5/5]intel_iommu: do not allow EIM without KVM support
Quoted 6 lines
2016-09-23 11:27+0200, Paolo Bonzini:
Quoted 3 lines
Since the whole IOMMU feature is new and somewhat experimental, I think it's okay to just make EIM the default for >=2.8 machine types if KVM is on (using
DEFINE_PROP_ON_OFF_AUTO; auto means true if KVM is on andSounds good.
BTW this also means KVM+vIOMMU requires 4.8. Let's remember to document it in the release notes.
Quoted 3 lines
Quoted 1 line
false otherwise, and pc-2.7 would set eim=off).
What about eim=on in pc-2.7, to avoid breaking migration?
Yup, just a thinko.
Paolo
Re: [Qemu-devel] [PATCH 5/5] intel_iommu: do not allow EIM without KVM support
In reply to Radim KrčmářQuoted 64 lines · On Thu, 22 Sep 2016 23:04:32 +0200 Radim Krčmář <[email protected]> wrote:
Cluster x2APIC cannot work without KVM's x2apic API when the maximal APIC ID is > 8. Make the code simpler by completely forbidding EIM without KVM's x2apic API.
Signed-off-by Radim Krčmář <[email protected]>---
I think it the dependency would be nicer in the eim setter, but the other dependency, for interrupt remapping, isn't there and I didn't venture for reasons.--- hw/i386/intel_iommu.c | 7 +++++++ target-i386/kvm-stub.c | 5 +++++ target-i386/kvm.c | 13 +++++++++++++ target-i386/kvm_i386.h | 1 + 4 files changed, 26 insertions(+)hw/i386/intel_iommu.c +7
-0@@ -32,6 +32,7 @@#include "hw/pci-host/q35.h" #include "sysemu/kvm.h" #include "hw/i386/apic_internal.h" +#include "kvm_i386.h" /*#define DEBUG_INTEL_IOMMU*/ #ifdef DEBUG_INTEL_IOMMU @@ -2485,6 +2486,12 @@ static void vtd_realize(DeviceState *dev, Error **errp) "kernel-irqchip=on, please use 'split|off'.");exit (1 ); } + +if (s->eim_supported &&kvm_irqchip_in_kernel () && + !kvm_enable_x2apic ()) { +error_report ("EIM requires support from the KVM side (X2APIC_API)." ); +exit (1 ); + } }static void vtd_class_init (ObjectClass *klass,void *data)target-i386/kvm-stub.c +5
-0@@ -25,6 +25,11 @@ bool kvm_has_smm(void)return 1 ; } +bool kvm_enable_x2apic (void ) +{ +return false; +} +/* This function is only called inside conditionals which we * rely on the compiler to optimize out when CONFIG_KVM is not * defined. target-i386/kvm.c +0
-0@@ -128,6 +128,19 @@ bool kvm_allows_irq0_override(void)return !kvm_irqchip_in_kernel () ||kvm_has_gsi_routing (); }
target-i386/kvm.c bool kvm_allows_irq0_override(void) · line 139
Quoted 10 lines
target-i386/kvm.c +9
-0+static bool kvm_x2apic_api_set_flags (uint64_t flags) +{ +KVMState *s =KVM_STATE (current_machine->accelerator); + +return !kvm_vm_enable_cap (s,KVM_CAP_X2APIC_API ,0 , flags); +} + +bool kvm_enable_x2apic (void ) +{
maybe it should be:
static bool enabled_x2apic;
if (!enabled_x2apic && kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS |
KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)) {
enabled_x2apic = true;
}return enabled_x2apic;
so that it could be called from multiple sites.
Quoted 19 lines
target-i386/kvm.c +4
-0+return kvm_x2apic_api_set_flags (KVM_X2APIC_API_USE_32BIT_IDS | +KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK ); +} +static int kvm_get_tsc (CPUState *cs){ X86CPU *cpu = X86_CPU(cs);target-i386/kvm_i386.h +1
-0@@ -41,4 +41,5 @@ int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector,int kvm_device_msix_assign (KVMState *s,uint32_t dev_id);int kvm_device_msix_deassign (KVMState *s,uint32_t dev_id); +bool kvm_enable_x2apic (void );#endif