Skip to contentSkip to conversation

intel_iommu: Fix invalidation descriptor type field

PATCH Superseded Zhenzhong Duan 3 participants 4 messages Aug 13, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 intel_iommu: Fix invalidation descriptor type field

According to spec, invalidation descriptor type is 7bits which is concatenation of bits[11:9] and bits[3:0] of invalidation descriptor.

Currently we only pick bits[3:0] as the invalidation type and treat bits[11:9] as reserved zero. This is not a problem for now as bits[11:9] is zero for all current invalidation types. But it will break if newer type occupies bits[11:9].

Fix it by take bits[11:9] into type and make reserved bits check accurate.

---
Tested intel-iommu.flat in kvm-unit-test: PASS
Tested vfio device hotplug: PASS

---
 hw/i386/intel_iommu_internal.h | 11 ++++++-----
 hw/i386/intel_iommu.c          |  2 +-
 2 files changed, 7 insertions(+), 6 deletions(-)
hw/i386/intel_iommu_internal.h +6 -5
index 5f32c36943..13d5d129ae 100644
@@ -356,7 +356,8 @@ union VTDInvDesc {
 typedef union VTDInvDesc VTDInvDesc;
 
 /* Masks for struct VTDInvDesc */
-#define VTD_INV_DESC_TYPE               0xf
+#define VTD_INV_DESC_TYPE(val)          ((((val) >> 5) & 0x70ULL) | \
+                                         ((val) & 0xfULL))
 #define VTD_INV_DESC_CC                 0x1 /* Context-cache Invalidate Desc */
 #define VTD_INV_DESC_IOTLB              0x2
 #define VTD_INV_DESC_DEVICE             0x3
@@ -372,7 +373,7 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_WAIT_IF            (1ULL << 4)
 #define VTD_INV_DESC_WAIT_FN            (1ULL << 6)
 #define VTD_INV_DESC_WAIT_DATA_SHIFT    32
-#define VTD_INV_DESC_WAIT_RSVD_LO       0Xffffff80ULL
+#define VTD_INV_DESC_WAIT_RSVD_LO       0Xfffff180ULL
 #define VTD_INV_DESC_WAIT_RSVD_HI       3ULL
 
 /* Masks for Context-cache Invalidation Descriptor */
@@ -383,7 +384,7 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_CC_DID(val)        (((val) >> 16) & VTD_DOMAIN_ID_MASK)
 #define VTD_INV_DESC_CC_SID(val)        (((val) >> 32) & 0xffffUL)
 #define VTD_INV_DESC_CC_FM(val)         (((val) >> 48) & 3UL)
-#define VTD_INV_DESC_CC_RSVD            0xfffc00000000ffc0ULL
+#define VTD_INV_DESC_CC_RSVD            0xfffc00000000f1c0ULL
 
 /* Masks for IOTLB Invalidate Descriptor */
 #define VTD_INV_DESC_IOTLB_G            (3ULL << 4)
@@ -393,7 +394,7 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_IOTLB_DID(val)     (((val) >> 16) & VTD_DOMAIN_ID_MASK)
 #define VTD_INV_DESC_IOTLB_ADDR(val)    ((val) & ~0xfffULL)
 #define VTD_INV_DESC_IOTLB_AM(val)      ((val) & 0x3fULL)
-#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000ff00ULL
+#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000f100ULL
 #define VTD_INV_DESC_IOTLB_RSVD_HI      0xf80ULL
 #define VTD_INV_DESC_IOTLB_PASID_PASID  (2ULL << 4)
 #define VTD_INV_DESC_IOTLB_PASID_PAGE   (3ULL << 4)
@@ -406,7 +407,7 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_DEVICE_IOTLB_SIZE(val) ((val) & 0x1)
 #define VTD_INV_DESC_DEVICE_IOTLB_SID(val) (((val) >> 32) & 0xFFFFULL)
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
-#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
+#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0f1f0
 
 /* Rsvd field masks for spte */
 #define VTD_SPTE_SNP 0x800ULL
hw/i386/intel_iommu.c +1 -1
index 16d2885fcc..68cb72a481 100644
@@ -2744,7 +2744,7 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s)
         return false;
     }
 
-    desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
+    desc_type = VTD_INV_DESC_TYPE(inv_desc.lo);
     /* FIXME: should update at first or at last? */
     s->iq_last_desc_type = desc_type;
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Zhenzhong Duan

According to spec, invalidation descriptor type is 7bits which is concatenation of bits[11:9] and bits[3:0] of invalidation descriptor.

Currently we only pick bits[3:0] as the invalidation type and treat bits[11:9] as reserved zero. This is not a problem for now as bits[11:9] is zero for all current invalidation types. But it will break if newer type occupies bits[11:9].

Fix it by take bits[11:9] into type and make reserved bits check accurate.

s/take/taking/

There is another fix you may add. In vtd_process_inv_desc(), it should treat the type VTD_INV_DESC_PC and VTD_INV_DESC_PIOTLB as invalid type if vIOMMU is running in legacy mode.

Quoted 73 lines

---
Tested intel-iommu.flat in kvm-unit-test: PASS
Tested vfio device hotplug: PASS

---
  hw/i386/intel_iommu_internal.h | 11 ++++++-----
  hw/i386/intel_iommu.c          |  2 +-
  2 files changed, 7 insertions(+), 6 deletions(-)
hw/i386/intel_iommu_internal.h +6 -5
index 5f32c36943..13d5d129ae 100644
@@ -356,7 +356,8 @@ union VTDInvDesc {
  typedef union VTDInvDesc VTDInvDesc;
 
  /* Masks for struct VTDInvDesc */
-#define VTD_INV_DESC_TYPE               0xf
+#define VTD_INV_DESC_TYPE(val)          ((((val) >> 5) & 0x70ULL) | \
+                                         ((val) & 0xfULL))
  #define VTD_INV_DESC_CC                 0x1 /* Context-cache Invalidate Desc */
  #define VTD_INV_DESC_IOTLB              0x2
  #define VTD_INV_DESC_DEVICE             0x3
@@ -372,7 +373,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_WAIT_IF            (1ULL << 4)
  #define VTD_INV_DESC_WAIT_FN            (1ULL << 6)
  #define VTD_INV_DESC_WAIT_DATA_SHIFT    32
-#define VTD_INV_DESC_WAIT_RSVD_LO       0Xffffff80ULL
+#define VTD_INV_DESC_WAIT_RSVD_LO       0Xfffff180ULL
  #define VTD_INV_DESC_WAIT_RSVD_HI       3ULL
 
  /* Masks for Context-cache Invalidation Descriptor */
@@ -383,7 +384,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_CC_DID(val)        (((val) >> 16) & VTD_DOMAIN_ID_MASK)
  #define VTD_INV_DESC_CC_SID(val)        (((val) >> 32) & 0xffffUL)
  #define VTD_INV_DESC_CC_FM(val)         (((val) >> 48) & 3UL)
-#define VTD_INV_DESC_CC_RSVD            0xfffc00000000ffc0ULL
+#define VTD_INV_DESC_CC_RSVD            0xfffc00000000f1c0ULL
 
  /* Masks for IOTLB Invalidate Descriptor */
  #define VTD_INV_DESC_IOTLB_G            (3ULL << 4)
@@ -393,7 +394,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_IOTLB_DID(val)     (((val) >> 16) & VTD_DOMAIN_ID_MASK)
  #define VTD_INV_DESC_IOTLB_ADDR(val)    ((val) & ~0xfffULL)
  #define VTD_INV_DESC_IOTLB_AM(val)      ((val) & 0x3fULL)
-#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000ff00ULL
+#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000f100ULL
  #define VTD_INV_DESC_IOTLB_RSVD_HI      0xf80ULL
  #define VTD_INV_DESC_IOTLB_PASID_PASID  (2ULL << 4)
  #define VTD_INV_DESC_IOTLB_PASID_PAGE   (3ULL << 4)
@@ -406,7 +407,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_DEVICE_IOTLB_SIZE(val) ((val) & 0x1)
  #define VTD_INV_DESC_DEVICE_IOTLB_SID(val) (((val) >> 32) & 0xFFFFULL)
  #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
-#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
+#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0f1f0
 
  /* Rsvd field masks for spte */
  #define VTD_SPTE_SNP 0x800ULL
hw/i386/intel_iommu.c +1 -1
index 16d2885fcc..68cb72a481 100644
@@ -2744,7 +2744,7 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s)
          return false;
      }
 
-    desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
+    desc_type = VTD_INV_DESC_TYPE(inv_desc.lo);
      /* FIXME: should update at first or at last? */
      s->iq_last_desc_type = desc_type;
 
Signature · 2 lines
Regards,
Yi Liu
#2 lore raw
Quoted 10 lines

-----Original Message-----
From: Liu, Yi L <[email protected]>
Subject: Re: [PATCH] intel_iommu: Fix invalidation descriptor type field

Quoted 3 lines · Zhenzhong Duan

According to spec, invalidation descriptor type is 7bits which is concatenation of bits[11:9] and bits[3:0] of invalidation descriptor.

Quoted 8 lines
Quoted 6 lines

Currently we only pick bits[3:0] as the invalidation type and treat bits[11:9] as reserved zero. This is not a problem for now as bits[11:9] is zero for all current invalidation types. But it will break if newer type occupies bits[11:9].

Fix it by take bits[11:9] into type and make reserved bits check accurate.

s/take/taking/

Will fix.

Quoted 6 lines

There is another fix you may add. In vtd_process_inv_desc(), it should treat the type VTD_INV_DESC_PC and VTD_INV_DESC_PIOTLB as invalid type if vIOMMU is running in legacy mode.

Ah, indeed, will fix with a new adding patch. Thanks for suggesting.

Quoted 83 lines
Quoted 6 lines

---
Tested intel-iommu.flat in kvm-unit-test: PASS
Tested vfio device hotplug: PASS

Review

Quoted 6 lines
---
  hw/i386/intel_iommu_internal.h | 11 ++++++-----
  hw/i386/intel_iommu.c          |  2 +-
  2 files changed, 7 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/hw/i386/intel_iommu_internal.h

hw/i386/intel_iommu_internal.h union VTDInvDesc { · line 361

Quoted 11 lines

index 5f32c36943..13d5d129ae 100644

hw/i386/intel_iommu_internal.h +2 -1
@@ -356,7 +356,8 @@ union VTDInvDesc {
  typedef union VTDInvDesc VTDInvDesc;
 
  /* Masks for struct VTDInvDesc */
-#define VTD_INV_DESC_TYPE               0xf
+#define VTD_INV_DESC_TYPE(val)          ((((val) >> 5) & 0x70ULL) | \
+                                         ((val) & 0xfULL))
  #define VTD_INV_DESC_CC                 0x1 /* Context-cache Invalidate Desc

*/

hw/i386/intel_iommu_internal.h typedef union VTDInvDesc VTDInvDesc; · line 384

Quoted 13 lines
hw/i386/intel_iommu_internal.h +1 -1
  #define VTD_INV_DESC_IOTLB              0x2
  #define VTD_INV_DESC_DEVICE             0x3
@@ -372,7 +373,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_WAIT_IF            (1ULL << 4)
  #define VTD_INV_DESC_WAIT_FN            (1ULL << 6)
  #define VTD_INV_DESC_WAIT_DATA_SHIFT    32
-#define VTD_INV_DESC_WAIT_RSVD_LO       0Xffffff80ULL
+#define VTD_INV_DESC_WAIT_RSVD_LO       0Xfffff180ULL
  #define VTD_INV_DESC_WAIT_RSVD_HI       3ULL
 
  /* Masks for Context-cache Invalidation Descriptor */
@@ -383,7 +384,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_CC_DID(val)        (((val) >> 16) &

VTD_DOMAIN_ID_MASK)

hw/i386/intel_iommu_internal.h typedef union VTDInvDesc VTDInvDesc; · line 394

Quoted 9 lines
hw/i386/intel_iommu_internal.h +1 -1
  #define VTD_INV_DESC_CC_SID(val)        (((val) >> 32) & 0xffffUL)
  #define VTD_INV_DESC_CC_FM(val)         (((val) >> 48) & 3UL)
-#define VTD_INV_DESC_CC_RSVD            0xfffc00000000ffc0ULL
+#define VTD_INV_DESC_CC_RSVD            0xfffc00000000f1c0ULL
 
  /* Masks for IOTLB Invalidate Descriptor */
  #define VTD_INV_DESC_IOTLB_G            (3ULL << 4)
@@ -393,7 +394,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_IOTLB_DID(val)     (((val) >> 16) &

VTD_DOMAIN_ID_MASK)

hw/i386/intel_iommu.c static bool

Quoted 21 lines
hw/i386/intel_iommu_internal.h +2 -2
  #define VTD_INV_DESC_IOTLB_ADDR(val)    ((val) & ~0xfffULL)
  #define VTD_INV_DESC_IOTLB_AM(val)      ((val) & 0x3fULL)
-#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000ff00ULL
+#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000f100ULL
  #define VTD_INV_DESC_IOTLB_RSVD_HI      0xf80ULL
  #define VTD_INV_DESC_IOTLB_PASID_PASID  (2ULL << 4)
  #define VTD_INV_DESC_IOTLB_PASID_PAGE   (3ULL << 4)
@@ -406,7 +407,7 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_DEVICE_IOTLB_SIZE(val) ((val) & 0x1)
  #define VTD_INV_DESC_DEVICE_IOTLB_SID(val) (((val) >> 32) & 0xFFFFULL)
  #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
-#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
+#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0f1f0
 
  /* Rsvd field masks for spte */
  #define VTD_SPTE_SNP 0x800ULL
hw/i386/intel_iommu.c +0 -0
index 16d2885fcc..68cb72a481 100644
@@ -2744,7 +2744,7 @@ static bool

vtd_process_inv_desc(IntelIOMMUState *s)

hw/i386/intel_iommu.c static bool vtd_process_inv_desc(IntelIOMMUState *s) · line 2750

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
          return false;
      }
 
-    desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
+    desc_type = VTD_INV_DESC_TYPE(inv_desc.lo);
      /* FIXME: should update at first or at last? */
      s->iq_last_desc_type = desc_type;
 

--
Regards,
Yi Liu

#3 lore raw
In reply to Yi Liu
Quoted 14 lines · Yi Liu

Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.

Quoted 8 lines · Zhenzhong Duan

According to spec, invalidation descriptor type is 7bits which is concatenation of bits[11:9] and bits[3:0] of invalidation descriptor.

Currently we only pick bits[3:0] as the invalidation type and treat bits[11:9] as reserved zero. This is not a problem for now as bits[11:9] is zero for all current invalidation types. But it will break if newer type occupies bits[11:9].

Quoted 6 lines
Quoted 2 lines

Fix it by take bits[11:9] into type and make reserved bits check accurate.

s/take/taking/

Quoted 4 lines

There is another fix you may add. In vtd_process_inv_desc(), it should treat the type VTD_INV_DESC_PC and VTD_INV_DESC_PIOTLB as invalid type if vIOMMU is running in legacy mode.

PASID based device IOTLB invalidation as well

Quoted 83 lines
Quoted 79 lines

---
Tested intel-iommu.flat in kvm-unit-test: PASS
Tested vfio device hotplug: PASS
---
  hw/i386/intel_iommu_internal.h | 11 ++++++-----
  hw/i386/intel_iommu.c          |  2 +-

  2 files changed, 7 insertions(+), 6 deletions(-)
hw/i386/intel_iommu_internal.h +2 -1
index 5f32c36943..13d5d129ae 100644
@@ -356,7 +356,8 @@ union VTDInvDesc {
   typedef union VTDInvDesc VTDInvDesc;
 
   /* Masks for struct VTDInvDesc */
-#define VTD_INV_DESC_TYPE               0xf
+#define VTD_INV_DESC_TYPE(val)          ((((val) >> 5) & 0x70ULL) | \
+                                         ((val) & 0xfULL))
   #define VTD_INV_DESC_CC                 0x1 /* Context-cache 

Invalidate Desc */
  #define VTD_INV_DESC_IOTLB              0x2
  #define VTD_INV_DESC_DEVICE             0x3

hw/i386/intel_iommu_internal.h +2 -2
@@ -372,7 +373,7 @@ typedef union VTDInvDesc VTDInvDesc;
   #define VTD_INV_DESC_WAIT_IF            (1ULL << 4)
   #define VTD_INV_DESC_WAIT_FN            (1ULL << 6)
   #define VTD_INV_DESC_WAIT_DATA_SHIFT    32
-#define VTD_INV_DESC_WAIT_RSVD_LO       0Xffffff80ULL
+#define VTD_INV_DESC_WAIT_RSVD_LO       0Xfffff180ULL
   #define VTD_INV_DESC_WAIT_RSVD_HI       3ULL
 
   /* Masks for Context-cache Invalidation Descriptor */
@@ -383,7 +384,7 @@ typedef union VTDInvDesc VTDInvDesc;
   #define VTD_INV_DESC_CC_DID(val)        (((val) >> 16) & 
 VTD_DOMAIN_ID_MASK)
   #define VTD_INV_DESC_CC_SID(val)        (((val) >> 32) & 0xffffUL)
   #define VTD_INV_DESC_CC_FM(val)         (((val) >> 48) & 3UL)
-#define VTD_INV_DESC_CC_RSVD            0xfffc00000000ffc0ULL
+#define VTD_INV_DESC_CC_RSVD            0xfffc00000000f1c0ULL
 
   /* Masks for IOTLB Invalidate Descriptor */

  #define VTD_INV_DESC_IOTLB_G            (3ULL << 4)

hw/i386/intel_iommu_internal.h +1 -1
@@ -393,7 +394,7 @@ typedef union VTDInvDesc VTDInvDesc;
   #define VTD_INV_DESC_IOTLB_DID(val)     (((val) >> 16) & 
 VTD_DOMAIN_ID_MASK)
   #define VTD_INV_DESC_IOTLB_ADDR(val)    ((val) & ~0xfffULL)
   #define VTD_INV_DESC_IOTLB_AM(val)      ((val) & 0x3fULL)
-#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000ff00ULL
+#define VTD_INV_DESC_IOTLB_RSVD_LO      0xffffffff0000f100ULL
   #define VTD_INV_DESC_IOTLB_RSVD_HI      0xf80ULL
   #define VTD_INV_DESC_IOTLB_PASID_PASID  (2ULL << 4)

  #define VTD_INV_DESC_IOTLB_PASID_PAGE   (3ULL << 4)

hw/i386/intel_iommu_internal.h +1 -1
@@ -406,7 +407,7 @@ typedef union VTDInvDesc VTDInvDesc;
   #define VTD_INV_DESC_DEVICE_IOTLB_SIZE(val) ((val) & 0x1)
   #define VTD_INV_DESC_DEVICE_IOTLB_SID(val) (((val) >> 32) & 0xFFFFULL)
   #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
-#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
+#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0f1f0
 
   /* Rsvd field masks for spte */
   #define VTD_SPTE_SNP 0x800ULL
hw/i386/intel_iommu.c +1 -1
index 16d2885fcc..68cb72a481 100644
@@ -2744,7 +2744,7 @@ static bool 
 vtd_process_inv_desc(IntelIOMMUState *s)
           return false;
       }
 
-    desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
+    desc_type = VTD_INV_DESC_TYPE(inv_desc.lo);
       /* FIXME: should update at first or at last? */
       s->iq_last_desc_type = desc_type;
Signature · 2 lines
Regards,
Yi Liu