Skip to contentSkip to conversation

AMD IOMMU

PATCH In review David Kiarie 2 participants 7 messages Sep 20, 2016 – Oct 1, 2016

lore
Share & tools
lore
#0 lore raw

v19 cover AMD IOMMU

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Resent this with some changes suggested by Michael.

Changes since v17
-removed host dependent defines in bitfields and replaced that with 'extract64/extract32' [Peter, Michael]

Changes since v16 - this is mainly supposed to come as a ping :-)
-minor endian-ness fixes

Changes since v15
-Endian-ness issue fix
-cleaned up unused macros
-removed guest frame number(gfn) from cache entry

Changes since v14
-MMIO register reading/write bug fix [Peter]
-Endian-ness issue fix[Peter]
-Bitfields layouts in IOMMU commands fix[Peter]
-IVRS changed IVHD device entry from type 3 to 1 to save a few bytes -coding style issues, comment grammer and other miscellaneous fixes.

Changes since v13
-Added an error to make AMD IOMMU incompatible with device assignment.[Alex] -Converted AMD IOMMU into a composite PCI and System Bus device. This helps with:
-We can now inherit from X86 IOMMU base class(which is implemented as a System Bus device). -We can now reserve MMIO region for IOMMU without a BAR register and without a hack.

Changes since v12

-Coding style fixes [Jan, Michael]
-Error logging fix to avoid using a macro[Jan] -moved some PCI macros to PCI header[Jan]
-Use a lookup table for MMIO register names when tracing[Jan]

Changes since V11
-AMD IOMMU is not started with -device amd-iommu (with a dependency on Marcel's patches). -IOMMU commands are represented using bitfields which is less error prone and more readable[Peter] -Changed from debug fprintfs to tracing[Jan]

Changes since V10

-Support for huge pages including some obscure AMD IOMMU feature that allows default page size override[Jan].
-Fixed an issue with generation of interrupts. We noted that AMD IOMMU has BusMaster- and is therefore not able to generate interrupts like any other PCI device. We have resulted in writing directly to system address but this could be fixed by some patches which have not been merged yet.

Changes since v9

-amd_iommu prefixes have been renamed to a shorter 'amdvi' both in the macros
and in the functions/code. The register macros have not been moved to the implementation file since almost the macros there are basically macros and I reckoned renaming them should suffice.
-taken care of byte order in the use of 'dma_memory_read'[Michael]
-Taken care of invalid DTE entries to ensure no DMA unless a device is configured to allow it. -An issue with the emulate IOMMU defaulting to AMD_IOMMU has been fixed[Marcel]

You can test[1] this patches by starting with parameters
qemu-system-x86_64 -M -device amd-iommu -m 2G -enable-kvm -smp 4 -cpu host -hda file.img -soundhw ac97
emulating whatever devices you want.

Not passing any command line parameters to linux should be enough to test this patches since the devices are basically passes-through but to the 'host' (l1 guest). You can still go ahead pass command line parameter 'iommu=pt iommu=1' and try to pass a device to L2 guest. This can also done without passing any iommu related parameters to the kernel.

David Kiarie (4):
hw/pci: Prepare for AMD IOMMU hw/i386/trace-events: Add AMD IOMMU trace events hw/i386: Introduce AMD IOMMU
hw/i386: AMD IOMMU IVRS table

 hw/acpi/aml-build.c         |    2 +-
 hw/i386/Makefile.objs       |    1 +
 hw/i386/acpi-build.c        |   76 ++-
 hw/i386/amd_iommu.c         | 1201 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h         |  289 +++++++++++
 hw/i386/intel_iommu.c       |    1 +
 hw/i386/trace-events        |   29 ++
 hw/i386/x86-iommu.c         |    6 +
 include/hw/acpi/aml-build.h |    1 +
 include/hw/i386/x86-iommu.h |   12 +
 include/hw/pci/pci.h        |    3 +
 11 files changed, 1611 insertions(+), 10 deletions(-)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
Signature · 1 line
2.1.4
#1 lore raw

PATCH v19 1/4 hw/pci: Prepare for AMD IOMMU

Introduce PCI macros from for use by AMD IOMMU

---
 include/hw/pci/pci.h | 3 +++
 1 file changed, 3 insertions(+)
include/hw/pci/pci.h +3 -0
index e8b83bb..772692f 100644
@@ -13,9 +13,12 @@
 /* PCI bus */
 
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
+#define PCI_BUS_NUM(x)          (((x) >> 8) & 0xff)
 #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
 #define PCI_FUNC(devfn)         ((devfn) & 0x07)
 #define PCI_BUILD_BDF(bus, devfn)     ((bus << 8) | (devfn))
+#define PCI_BUS_MAX             256
+#define PCI_DEVFN_MAX           256
 #define PCI_SLOT_MAX            32
 #define PCI_FUNC_MAX            8
 
Signature · 1 line
2.1.4
#2 lore raw

PATCH v19 2/4 hw/i386/trace-events: Add AMD IOMMU trace events

In reply to David Kiarie
---
 hw/i386/trace-events | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
hw/i386/trace-events +29 -0
index 5b99eba..1938b98 100644
@@ -13,3 +13,32 @@ mhp_pc_dimm_assigned_address(uint64_t addr) "0x%"PRIx64
 
 # hw/i386/x86-iommu.c
 x86_iommu_iec_notify(bool global, uint32_t index, uint32_t mask) "Notify IEC invalidation: global=%d index=%" PRIu32 " mask=%" PRIu32
+
+# hw/i386/amd_iommu.c
+amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" +  offset 0x%"PRIx32
+amdvi_cache_update(uint16_t domid, uint8_t bus, uint8_t slot, uint8_t func, uint64_t gpa, uint64_t txaddr) " update iotlb domid 0x%"PRIx16" devid: %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64
+amdvi_completion_wait_fail(uint64_t addr) "error: fail to write at address 0x%"PRIx64
+amdvi_mmio_write(const char *reg, uint64_t addr, unsigned size, uint64_t val, uint64_t offset) "%s write addr 0x%"PRIx64", size %u, val 0x%"PRIx64", offset 0x%"PRIx64
+amdvi_mmio_read(const char *reg, uint64_t addr, unsigned size, uint64_t offset) "%s read addr 0x%"PRIx64", size %u offset 0x%"PRIx64
+amdvi_command_error(uint64_t status) "error: Executing commands with command buffer disabled 0x%"PRIx64
+amdvi_command_read_fail(uint64_t addr, uint32_t head) "error: fail to access memory at 0x%"PRIx64" + 0x%"PRIx32
+amdvi_command_exec(uint32_t head, uint32_t tail, uint64_t buf) "command buffer head at 0x%"PRIx32" command buffer tail at 0x%"PRIx32" command buffer base at 0x%"PRIx64
+amdvi_unhandled_command(uint8_t type) "unhandled command 0x%"PRIx8
+amdvi_intr_inval(void) "Interrupt table invalidated"
+amdvi_iotlb_inval(void) "IOTLB pages invalidated"
+amdvi_prefetch_pages(void) "Pre-fetch of AMD-Vi pages requested"
+amdvi_pages_inval(uint16_t domid) "AMD-Vi pages for domain 0x%"PRIx16 " invalidated"
+amdvi_all_inval(void) "Invalidation of all AMD-Vi cache requested "
+amdvi_ppr_exec(void) "Execution of PPR queue requested "
+amdvi_devtab_inval(uint8_t bus, uint8_t slot, uint8_t func) "device table entry for devid: %02x:%02x.%x invalidated"
+amdvi_completion_wait(uint64_t addr, uint64_t data) "completion wait requested with store address 0x%"PRIx64" and store data 0x%"PRIx64
+amdvi_control_status(uint64_t val) "MMIO_STATUS state 0x%"PRIx64
+amdvi_iotlb_reset(void) "IOTLB exceed size limit - reset "
+amdvi_completion_wait_exec(uint64_t addr, uint64_t data) "completion wait requested with store address 0x%"PRIx64" and store data 0x%"PRIx64
+amdvi_dte_get_fail(uint64_t addr, uint32_t offset) "error: failed to access Device Entry devtab 0x%"PRIx64" offset 0x%"PRIx32
+amdvi_invalid_dte(uint64_t addr) "PTE entry at 0x%"PRIx64" is invalid "
+amdvi_get_pte_hwerror(uint64_t addr) "hardware error eccessing PTE at addr 0x%"PRIx64
+amdvi_mode_invalid(uint8_t level, uint64_t addr)"error: translation level 0x%"PRIx8" translating addr 0x%"PRIx64
+amdvi_page_fault(uint64_t addr) "error: page fault accessing guest physical address 0x%"PRIx64
+amdvi_iotlb_hit(uint8_t bus, uint8_t slot, uint8_t func, uint64_t addr, uint64_t txaddr) "hit iotlb devid %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64
+amdvi_translation_result(uint8_t bus, uint8_t slot, uint8_t func, uint64_t addr, uint64_t txaddr) "devid: %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64
Signature · 1 line
2.1.4
#3 lore raw

PATCH v19 3/4 hw/i386: Introduce AMD IOMMU

In reply to David Kiarie

Add AMD IOMMU emulaton to Qemu in addition to Intel IOMMU. The IOMMU does basic translation, error checking and has a minimal IOTLB implementation. This IOMMU bypassed the need for target aborts by responding with IOMMU_NONE access rights and exempts the region 0xfee00000-0xfeefffff from translation as it is the q35 interrupt region.

We advertise features that are not yet implemented to please the Linux IOMMU driver.

IOTLB aims at implementing commands on real IOMMUs which is essential for debugging and may not offer any performance benefits

---
 hw/i386/Makefile.objs |    1 +
 hw/i386/amd_iommu.c   | 1199 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h   |  289 ++++++++++++
 3 files changed, 1489 insertions(+)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
hw/i386/Makefile.objs +1 -0
index 90e94ff..909ead6 100644
@@ -3,6 +3,7 @@ obj-y += multiboot.o
 obj-y += pc.o pc_piix.o pc_q35.o
 obj-y += pc_sysfw.o
 obj-y += x86-iommu.o intel_iommu.o
+obj-y += amd_iommu.o
 obj-$(CONFIG_XEN) += ../xenpv/ xen/
 
 obj-y += kvmvapic.o
hw/i386/amd_iommu.c new file +1199 -0

1,199 lines not on this page · raw message

hw/i386/amd_iommu.h new file +289 -0

289 lines not on this page · raw message

Signature · 1 line
2.1.4
#4 lore raw
In reply to David Kiarie
Quoted 5 lines · David Kiarie

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Resent this with some changes suggested by Michael.

Hi David,

please fix the problems below, that were reported by Coverity.

Paolo

** CID 1363372: Uninitialized variables (UNINIT) /hw/i386/amd_iommu.c: 147 in amdvi_generate_msi_interrupt()
________________________________________________________________________________________________________
141 amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) & val);
142 }
143
144 static void amdvi_generate_msi_interrupt(AMDVIState *s)
145 {
146 MSIMessage msg;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363372: Uninitialized variables (UNINIT) Declaring variable "attrs" without initializer.

147 MemTxAttrs attrs;
148
149 attrs.requester_id = pci_requester_id(&s->pci.dev);
150
151 if (msi_enabled(&s->pci.dev)) {
152 msg = msi_get_message(&s->pci.dev, 0);

** CID 1363371: Code maintainability issues (SIZEOF_MISMATCH) /hw/i386/amd_iommu.c: 337 in amdvi_update_iotlb()
________________________________________________________________________________________________________
331
332 static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
333 uint64_t gpa, IOMMUTLBEntry to_cache,
334 uint16_t domid)
335 {
336 AMDVIIOTLBEntry *entry = g_malloc(sizeof(*entry));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

CID 1363371: Code maintainability issues (SIZEOF_MISMATCH) Passing argument "8UL /* sizeof (key) */" to function "g_malloc" and then casting the return value to "uint64_t *" is suspicious. In this particular case "sizeof (uint64_t *)" happens to be equal to "sizeof (uint64_t)", but this is not a portable assumption.

337 uint64_t *key = g_malloc(sizeof(key));
338 uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
339
340 /* don't cache erroneous translations */
341 if (to_cache.perm != IOMMU_NONE) {
342 trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid),

** CID 1363370: Resource leaks (RESOURCE_LEAK)
/hw/i386/amd_iommu.c: 356 in amdvi_update_iotlb()
________________________________________________________________________________________________________

350             entry->perms = to_cache.perm;
351             entry->translated_addr = to_cache.translated_addr;
352             entry->page_mask = to_cache.addr_mask;
353             *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
354             g_hash_table_replace(s->iotlb, key, entry);

355 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363370: Resource leaks (RESOURCE_LEAK)
Variable "entry" going out of scope leaks the storage it points to.

356 }
357
358 static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
359 {
360 /* pad the last 3 bits */
361 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;

** CID 1363369: Resource leaks (RESOURCE_LEAK)
/hw/i386/amd_iommu.c: 356 in amdvi_update_iotlb()
________________________________________________________________________________________________________

350             entry->perms = to_cache.perm;
351             entry->translated_addr = to_cache.translated_addr;
352             entry->page_mask = to_cache.addr_mask;
353             *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
354             g_hash_table_replace(s->iotlb, key, entry);

355 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363369: Resource leaks (RESOURCE_LEAK)
Variable "key" going out of scope leaks the storage it points to.

356 }
357
358 static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
359 {
360 /* pad the last 3 bits */
361 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;

** CID 1363364: (CHECKED_RETURN)
/hw/i386/amd_iommu.c: 1150 in amdvi_realize()
/hw/i386/amd_iommu.c: 1151 in amdvi_realize()
________________________________________________________________________________________________________

1144         /* This device should take care of IOMMU PCI properties */
1145         x86_iommu->type = TYPE_AMD;
1146         qdev_set_parent_bus(DEVICE(&s->pci), &bus->qbus);
1147         object_property_set_bool(OBJECT(&s->pci), true, "realized", err);
1148         s->capab_offset = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,

1149 AMDVI_CAPAB_SIZE);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363364: (CHECKED_RETURN)
Calling "pci_add_capability" without checking return value.

1150 pci_add_capability(&s->pci.dev, PCI_CAP_ID_MSI, 0, AMDVI_CAPAB_REG_SIZE);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363364: (CHECKED_RETURN)
Calling "pci_add_capability" without checking return value.

1151 pci_add_capability(&s->pci.dev, PCI_CAP_ID_HT, 0, AMDVI_CAPAB_REG_SIZE);
1152
1153 /* set up MMIO */
1154 memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
1155 AMDVI_MMIO_SIZE);
1156

** CID 1363363: Integer handling issues (BAD_SHIFT) /hw/i386/amd_iommu.c: 188 in amdvi_setevent_bits()
________________________________________________________________________________________________________
182 }
183
184 static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int start,
185 int length)
186 {
187 int index = start / 64, bitpos = start % 64;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

CID 1363363: Integer handling issues (BAD_SHIFT)
In expression "(1 << length) - 1 << bitpos", left shifting by more than 31 bits has undefined behavior. The shift amount, "bitpos", is as much as 63.

188         uint64_t mask = ((1 << length) - 1) << bitpos;
189         buffer[index] &= ~mask;
190         buffer[index] |= (value << bitpos) & mask;

191 }
192 /*
193 * AMDVi event structure

#5 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · David Kiarie

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Resent this with some changes suggested by Michael.

Hi David,

Hi Paolo,

Quoted 2 lines

please fix the problems below, that were reported by Coverity.

Thanks, will do ASAP.

Quoted 148 lines

Paolo

** CID 1363372: Uninitialized variables (UNINIT) /hw/i386/amd_iommu.c: 147 in amdvi_generate_msi_interrupt()
________________________________________________________________________________________________________
141 amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) & val);
142 }
143
144 static void amdvi_generate_msi_interrupt(AMDVIState *s)
145 {
146 MSIMessage msg;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363372: Uninitialized variables (UNINIT) Declaring variable "attrs" without initializer.

147 MemTxAttrs attrs;
148
149 attrs.requester_id = pci_requester_id(&s->pci.dev);
150
151 if (msi_enabled(&s->pci.dev)) {
152 msg = msi_get_message(&s->pci.dev, 0);

** CID 1363371: Code maintainability issues (SIZEOF_MISMATCH) /hw/i386/amd_iommu.c: 337 in amdvi_update_iotlb()
________________________________________________________________________________________________________
331
332 static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
333 uint64_t gpa, IOMMUTLBEntry to_cache,
334 uint16_t domid)
335 {
336 AMDVIIOTLBEntry *entry = g_malloc(sizeof(*entry));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

CID 1363371: Code maintainability issues (SIZEOF_MISMATCH) Passing argument "8UL /* sizeof (key) */" to function "g_malloc" and then casting the return value to "uint64_t *" is suspicious. In this particular case "sizeof (uint64_t *)" happens to be equal to "sizeof (uint64_t)", but this is not a portable assumption.

337 uint64_t *key = g_malloc(sizeof(key));
338 uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
339
340 /* don't cache erroneous translations */
341 if (to_cache.perm != IOMMU_NONE) {
342 trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid),

** CID 1363370: Resource leaks (RESOURCE_LEAK)
/hw/i386/amd_iommu.c: 356 in amdvi_update_iotlb()
________________________________________________________________________________________________________

350             entry->perms = to_cache.perm;
351             entry->translated_addr = to_cache.translated_addr;
352             entry->page_mask = to_cache.addr_mask;
353             *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
354             g_hash_table_replace(s->iotlb, key, entry);

355 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363370: Resource leaks (RESOURCE_LEAK)
Variable "entry" going out of scope leaks the storage it points to.

356 }
357
358 static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
359 {
360 /* pad the last 3 bits */
361 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;

** CID 1363369: Resource leaks (RESOURCE_LEAK)
/hw/i386/amd_iommu.c: 356 in amdvi_update_iotlb()
________________________________________________________________________________________________________

350             entry->perms = to_cache.perm;
351             entry->translated_addr = to_cache.translated_addr;
352             entry->page_mask = to_cache.addr_mask;
353             *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
354             g_hash_table_replace(s->iotlb, key, entry);

355 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363369: Resource leaks (RESOURCE_LEAK)
Variable "key" going out of scope leaks the storage it points to.

356 }
357
358 static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
359 {
360 /* pad the last 3 bits */
361 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;

** CID 1363364: (CHECKED_RETURN)
/hw/i386/amd_iommu.c: 1150 in amdvi_realize()
/hw/i386/amd_iommu.c: 1151 in amdvi_realize()
________________________________________________________________________________________________________

1144         /* This device should take care of IOMMU PCI properties */
1145         x86_iommu->type = TYPE_AMD;
1146         qdev_set_parent_bus(DEVICE(&s->pci), &bus->qbus);
1147         object_property_set_bool(OBJECT(&s->pci), true, "realized", err);
1148         s->capab_offset = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,

1149 AMDVI_CAPAB_SIZE);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363364: (CHECKED_RETURN)
Calling "pci_add_capability" without checking return value.

1150 pci_add_capability(&s->pci.dev, PCI_CAP_ID_MSI, 0, AMDVI_CAPAB_REG_SIZE);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363364: (CHECKED_RETURN)
Calling "pci_add_capability" without checking return value.

1151 pci_add_capability(&s->pci.dev, PCI_CAP_ID_HT, 0, AMDVI_CAPAB_REG_SIZE);
1152
1153 /* set up MMIO */
1154 memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
1155 AMDVI_MMIO_SIZE);
1156

** CID 1363363: Integer handling issues (BAD_SHIFT) /hw/i386/amd_iommu.c: 188 in amdvi_setevent_bits()
________________________________________________________________________________________________________
182 }
183
184 static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int start,
185 int length)
186 {
187 int index = start / 64, bitpos = start % 64;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

CID 1363363: Integer handling issues (BAD_SHIFT)
In expression "(1 << length) - 1 << bitpos", left shifting by more than 31 bits has undefined behavior. The shift amount, "bitpos", is as much as 63.

188         uint64_t mask = ((1 << length) - 1) << bitpos;
189         buffer[index] &= ~mask;
190         buffer[index] |= (value << bitpos) & mask;

191 }
192 /*
193 * AMDVi event structure

#6 lore raw
Quoted 11 lines · David Kiarie
Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · David Kiarie

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Resent this with some changes suggested by Michael.

Quoted 100 lines
Quoted 1 line

Hi David,

Hi Paolo,

Quoted 2 lines

please fix the problems below, that were reported by Coverity.

Thanks, will do ASAP.

Quoted 91 lines

Paolo

** CID 1363372: Uninitialized variables (UNINIT) /hw/i386/amd_iommu.c: 147 in amdvi_generate_msi_interrupt()
________________________________________________________________________________________________________
141 amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) & val);
142 }
143
144 static void amdvi_generate_msi_interrupt(AMDVIState *s)
145 {
146 MSIMessage msg;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363372: Uninitialized variables (UNINIT) Declaring variable "attrs" without initializer.

147 MemTxAttrs attrs;
148
149 attrs.requester_id = pci_requester_id(&s->pci.dev);
150
151 if (msi_enabled(&s->pci.dev)) {
152 msg = msi_get_message(&s->pci.dev, 0);

** CID 1363371: Code maintainability issues (SIZEOF_MISMATCH) /hw/i386/amd_iommu.c: 337 in amdvi_update_iotlb()
________________________________________________________________________________________________________
331
332 static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
333 uint64_t gpa, IOMMUTLBEntry to_cache,
334 uint16_t domid)
335 {
336 AMDVIIOTLBEntry *entry = g_malloc(sizeof(*entry));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

CID 1363371: Code maintainability issues (SIZEOF_MISMATCH) Passing argument "8UL /* sizeof (key) */" to function "g_malloc" and then casting the return value to "uint64_t *" is suspicious. In this particular case "sizeof (uint64_t *)" happens to be equal to "sizeof (uint64_t)", but this is not a portable assumption.

337 uint64_t *key = g_malloc(sizeof(key));
338 uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
339
340 /* don't cache erroneous translations */
341 if (to_cache.perm != IOMMU_NONE) {
342 trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid),

** CID 1363370: Resource leaks (RESOURCE_LEAK)
/hw/i386/amd_iommu.c: 356 in amdvi_update_iotlb()
________________________________________________________________________________________________________

350             entry->perms = to_cache.perm;
351             entry->translated_addr = to_cache.translated_addr;
352             entry->page_mask = to_cache.addr_mask;
353             *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
354             g_hash_table_replace(s->iotlb, key, entry);

355 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363370: Resource leaks (RESOURCE_LEAK)
Variable "entry" going out of scope leaks the storage it points to.

356 }
357
358 static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
359 {
360 /* pad the last 3 bits */
361 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;

** CID 1363369: Resource leaks (RESOURCE_LEAK)
/hw/i386/amd_iommu.c: 356 in amdvi_update_iotlb()
________________________________________________________________________________________________________

350             entry->perms = to_cache.perm;
351             entry->translated_addr = to_cache.translated_addr;
352             entry->page_mask = to_cache.addr_mask;
353             *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
354             g_hash_table_replace(s->iotlb, key, entry);

355 }

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363369: Resource leaks (RESOURCE_LEAK)
Variable "key" going out of scope leaks the storage it points to.

356 }

This looks like a false positive.

Quoted 57 lines
Quoted 57 lines

357
358 static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
359 {
360 /* pad the last 3 bits */
361 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;

** CID 1363364: (CHECKED_RETURN)
/hw/i386/amd_iommu.c: 1150 in amdvi_realize()
/hw/i386/amd_iommu.c: 1151 in amdvi_realize()
________________________________________________________________________________________________________

1144         /* This device should take care of IOMMU PCI properties */
1145         x86_iommu->type = TYPE_AMD;
1146         qdev_set_parent_bus(DEVICE(&s->pci), &bus->qbus);
1147         object_property_set_bool(OBJECT(&s->pci), true, "realized", err);
1148         s->capab_offset = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,

1149 AMDVI_CAPAB_SIZE);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363364: (CHECKED_RETURN)
Calling "pci_add_capability" without checking return value.

1150 pci_add_capability(&s->pci.dev, PCI_CAP_ID_MSI, 0, AMDVI_CAPAB_REG_SIZE);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1363364: (CHECKED_RETURN)
Calling "pci_add_capability" without checking return value.

1151 pci_add_capability(&s->pci.dev, PCI_CAP_ID_HT, 0, AMDVI_CAPAB_REG_SIZE);
1152
1153 /* set up MMIO */
1154 memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
1155 AMDVI_MMIO_SIZE);
1156

** CID 1363363: Integer handling issues (BAD_SHIFT) /hw/i386/amd_iommu.c: 188 in amdvi_setevent_bits()
________________________________________________________________________________________________________
182 }
183
184 static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int start,
185 int length)
186 {
187 int index = start / 64, bitpos = start % 64;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

CID 1363363: Integer handling issues (BAD_SHIFT)
In expression "(1 << length) - 1 << bitpos", left shifting by more than 31 bits has undefined behavior. The shift amount, "bitpos", is as much as 63.

188         uint64_t mask = ((1 << length) - 1) << bitpos;
189         buffer[index] &= ~mask;
190         buffer[index] |= (value << bitpos) & mask;

191 }
192 /*
193 * AMDVi event structure