Skip to contentSkip to conversation

hw/i386/intel_iommu: Block CFI when necessary

PATCH Superseded Yuke Peng 3 participants 5 messages Jun 25, 2024 – Jul 1, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yuke Peng

author
#0 lore raw

PATCH 1/1 hw/i386/intel_iommu: Block CFI when necessary

According to Intel VT-d specification 5.1.4, CFI must be blocked when Extended Interrupt Mode is enabled or Compatibility format interrupts are disabled.

---
 hw/i386/intel_iommu.c         | 28 ++++++++++++++++++++++++++++
 hw/i386/trace-events          |  1 +
 include/hw/i386/intel_iommu.h |  1 +
 3 files changed, 30 insertions(+)
hw/i386/intel_iommu.c +28 -0
index 5085a6fee3..dfa2f979e7 100644
@@ -2410,6 +2410,22 @@ static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
     }
 }
 
+/* Handle Compatibility Format Interrupts Enable/Disable */
+static void vtd_handle_gcmd_cfi(IntelIOMMUState *s, bool en)
+{
+    trace_vtd_cfi_enable(en);
+
+    if (en) {
+        s->cfi_enabled = true;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_CFIS);
+    } else {
+        s->cfi_enabled = false;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_CFIS, 0);
+    }
+}
+
 /* Handle write to Global Command Register */
 static void vtd_handle_gcmd_write(IntelIOMMUState *s)
 {
@@ -2440,6 +2456,10 @@ static void vtd_handle_gcmd_write(IntelIOMMUState *s)
         /* Interrupt remap enable/disable */
         vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
     }
+    if (changed & VTD_GCMD_CFI) {
+        /* Compatibility format interrupts enable/disable */
+        vtd_handle_gcmd_cfi(s, val & VTD_GCMD_CFI);
+    }
 }
 
 /* Handle write to Context Command Register */
@@ -3304,6 +3324,7 @@ static const VMStateDescription vtd_vmstate = {
         VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
         VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
         VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
+        VMSTATE_BOOL(cfi_enabled, IntelIOMMUState),
         VMSTATE_BOOL(intr_eime, IntelIOMMUState),
         VMSTATE_END_OF_LIST()
     }
@@ -3525,6 +3546,12 @@ static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
 
     /* This is compatible mode. */
     if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
+        if (iommu->intr_eime || !iommu->cfi_enabled) {
+            if (do_fault) {
+                vtd_report_ir_fault(iommu, sid, VTD_FR_IR_REQ_COMPAT, 0);
+            }
+            return -EINVAL;
+        }
         memcpy(translated, origin, sizeof(*origin));
         goto out;
     }
@@ -3950,6 +3977,7 @@ static void vtd_init(IntelIOMMUState *s)
     s->root_scalable = false;
     s->dmar_enabled = false;
     s->intr_enabled = false;
+    s->cfi_enabled = false;
     s->iq_head = 0;
     s->iq_tail = 0;
     s->iq = 0;
hw/i386/trace-events +1 -0
index 53c02d7ac8..ffd87db65f 100644
@@ -57,6 +57,7 @@ vtd_dmar_translate(uint8_t bus, uint8_t slot, uint8_t func, uint64_t iova, uint6
 vtd_dmar_enable(bool en) "enable %d"
 vtd_dmar_fault(uint16_t sid, int fault, uint64_t addr, bool is_write) "sid 0x%"PRIx16" fault %d addr 0x%"PRIx64" write %d"
 vtd_ir_enable(bool en) "enable %d"
+vtd_cfi_enable(bool en) "enable %d"
 vtd_ir_irte_get(int index, uint64_t lo, uint64_t hi) "index %d low 0x%"PRIx64" high 0x%"PRIx64
 vtd_ir_remap(int index, int tri, int vec, int deliver, uint32_t dest, int dest_mode) "index %d trigger %d vector %d deliver %d dest 0x%"PRIx32" mode %d"
 vtd_ir_remap_type(const char *type) "%s"
include/hw/i386/intel_iommu.h +1 -0
index 7fa0a695c8..38e20d0f2c 100644
@@ -294,6 +294,7 @@ struct IntelIOMMUState {
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
+    bool cfi_enabled;               /* Whether CFI is enabled */
     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 */
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Quoted 23 lines · Yuke Peng

According to Intel VT-d specification 5.1.4, CFI must be blocked when Extended Interrupt Mode is enabled or Compatibility format interrupts are disabled.

---
  hw/i386/intel_iommu.c         | 28 ++++++++++++++++++++++++++++
  hw/i386/trace-events          |  1 +
  include/hw/i386/intel_iommu.h |  1 +
  3 files changed, 30 insertions(+)
hw/i386/intel_iommu.c +4 -0
index 5085a6fee3..dfa2f979e7 100644
@@ -2410,6 +2410,22 @@ static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
      }
  }
 
+/* Handle Compatibility Format Interrupts Enable/Disable */
+static void vtd_handle_gcmd_cfi(IntelIOMMUState *s, bool en)
+{
+    trace_vtd_cfi_enable(en);

hw/i386/intel_iommu.c static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en) · line 2426

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+
+    if (en) {
+        s->cfi_enabled = true;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_CFIS);
+    } else {
+        s->cfi_enabled = false;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_CFIS, 0);
+    }

You have the same comment in both branches of the if statement, couldn't we put it between 'trace' and 'if'?

Quoted 62 lines
hw/i386/intel_iommu.c +14 -0
+}
+
  /* Handle write to Global Command Register */
  static void vtd_handle_gcmd_write(IntelIOMMUState *s)
  {
@@ -2440,6 +2456,10 @@ static void vtd_handle_gcmd_write(IntelIOMMUState *s)
          /* Interrupt remap enable/disable */
          vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
      }
+    if (changed & VTD_GCMD_CFI) {
+        /* Compatibility format interrupts enable/disable */
+        vtd_handle_gcmd_cfi(s, val & VTD_GCMD_CFI);
+    }
  }
 
  /* Handle write to Context Command Register */
@@ -3304,6 +3324,7 @@ static const VMStateDescription vtd_vmstate = {
          VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
          VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
          VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
+        VMSTATE_BOOL(cfi_enabled, IntelIOMMUState),
          VMSTATE_BOOL(intr_eime, IntelIOMMUState),
          VMSTATE_END_OF_LIST()
      }
@@ -3525,6 +3546,12 @@ static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
 
      /* This is compatible mode. */
      if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
+        if (iommu->intr_eime || !iommu->cfi_enabled) {
+            if (do_fault) {
+                vtd_report_ir_fault(iommu, sid, VTD_FR_IR_REQ_COMPAT, 0);
+            }
+            return -EINVAL;
+        }
          memcpy(translated, origin, sizeof(*origin));
          goto out;
      }
@@ -3950,6 +3977,7 @@ static void vtd_init(IntelIOMMUState *s)
      s->root_scalable = false;
      s->dmar_enabled = false;
      s->intr_enabled = false;
+    s->cfi_enabled = false;
      s->iq_head = 0;
      s->iq_tail = 0;
      s->iq = 0;
hw/i386/trace-events +1 -0
index 53c02d7ac8..ffd87db65f 100644
@@ -57,6 +57,7 @@ vtd_dmar_translate(uint8_t bus, uint8_t slot, uint8_t func, uint64_t iova, uint6
  vtd_dmar_enable(bool en) "enable %d"
  vtd_dmar_fault(uint16_t sid, int fault, uint64_t addr, bool is_write) "sid 0x%"PRIx16" fault %d addr 0x%"PRIx64" write %d"
  vtd_ir_enable(bool en) "enable %d"
+vtd_cfi_enable(bool en) "enable %d"
  vtd_ir_irte_get(int index, uint64_t lo, uint64_t hi) "index %d low 0x%"PRIx64" high 0x%"PRIx64
  vtd_ir_remap(int index, int tri, int vec, int deliver, uint32_t dest, int dest_mode) "index %d trigger %d vector %d deliver %d dest 0x%"PRIx32" mode %d"
  vtd_ir_remap_type(const char *type) "%s"
include/hw/i386/intel_iommu.h +0 -0
index 7fa0a695c8..38e20d0f2c 100644

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 300

Quoted 7 lines
include/hw/i386/intel_iommu.h +1 -0
@@ -294,6 +294,7 @@ struct IntelIOMMUState {
 
      /* interrupt remapping */
      bool intr_enabled;              /* Whether guest enabled IR */
+    bool cfi_enabled;               /* Whether CFI is enabled */
      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 */

LGTM

author
#2 lore raw
Quoted 23 lines · cmd

Hi,

hw/i386/intel_iommu.c static void vtd_handle_gcmd_ire(IntelIOMMUState

Quoted 16 lines · Yuke Peng

According to Intel VT-d specification 5.1.4, CFI must be blocked when Extended Interrupt Mode is enabled or Compatibility format interrupts are disabled.

---
  hw/i386/intel_iommu.c         | 28 ++++++++++++++++++++++++++++
  hw/i386/trace-events          |  1 +
  include/hw/i386/intel_iommu.h |  1 +
  3 files changed, 30 insertions(+)
hw/i386/intel_iommu.c +0 -0
index 5085a6fee3..dfa2f979e7 100644
@@ -2410,6 +2410,22 @@ static void vtd_handle_gcmd_ire(IntelIOMMUState

*s, bool en)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
      }
  }
 
Quoted 17 lines
Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+/* Handle Compatibility Format Interrupts Enable/Disable */
+static void vtd_handle_gcmd_cfi(IntelIOMMUState *s, bool en)
+{
+    trace_vtd_cfi_enable(en);

hw/i386/intel_iommu.c static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en) · line 2426

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+
+    if (en) {
+        s->cfi_enabled = true;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_CFIS);
+    } else {
+        s->cfi_enabled = false;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_CFIS, 0);
+    }

You have the same comment in both branches of the if statement, couldn't we put it between 'trace' and 'if'?

The reason I put the comment within the branches is that the function 'vtd_handle_gcmd_cfi' is similar to the function 'vtd_handle_gcmd_ire' and 'vtd_handle_gcmd_te'. So for consistency, I also put comments within the branches instead of in front of 'if'. Besides, I think the comments
only explain the statement 'vtd_set_clear_mask_long'.

The 'vtd_handle_gcmd_ire' function:

Quoted 15 lines

/* Handle Interrupt Remap Enable/Disable */
static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)

{
    trace_vtd_ir_enable(en);

    if (en) {
        s->intr_enabled = true;
        /* Ok - report back to driver */
        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
    } else {
        s->intr_enabled = false;
        /* Ok - report back to driver */
        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
    }
}
#3 lore raw
Quoted 48 lines · Yuke Peng

On Wed, Jun 26, 2024 at 2:15 PM cmd <[email protected]> wrote:

    Hi,

    On 25/06/2024 13:28, Yuke Peng wrote:
    > According to Intel VT-d specification 5.1.4, CFI must be blocked
    when
    > Extended Interrupt Mode is enabled or Compatibility format
    interrupts
    > are disabled.
    >
    > Signed-off-by: Yuke Peng <[email protected]>
    > ---
    >   hw/i386/intel_iommu.c         | 28 ++++++++++++++++++++++++++++
    >   hw/i386/trace-events          |  1 +
    >   include/hw/i386/intel_iommu.h |  1 +
    >   3 files changed, 30 insertions(+)
    >
    > diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
    > index 5085a6fee3..dfa2f979e7 100644
    > --- a/hw/i386/intel_iommu.c
    > +++ b/hw/i386/intel_iommu.c
    > @@ -2410,6 +2410,22 @@ static void
    vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
    >       }
    >   }
    >
    > +/* Handle Compatibility Format Interrupts Enable/Disable */
    > +static void vtd_handle_gcmd_cfi(IntelIOMMUState *s, bool en)
    > +{
    > +    trace_vtd_cfi_enable(en);
    > +
    > +    if (en) {
    > +        s->cfi_enabled = true;
    > +        /* Ok - report back to driver */
    > +        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0,
    VTD_GSTS_CFIS);
    > +    } else {
    > +        s->cfi_enabled = false;
    > +        /* Ok - report back to driver */
    > +        vtd_set_clear_mask_long(s, DMAR_GSTS_REG,
    VTD_GSTS_CFIS, 0);
    > +    }
    You have the same comment in both branches of the if statement,
    couldn't
    we put it between 'trace' and 'if'?
Quoted 7 lines

The reason I put the comment within the branches is that the function 'vtd_handle_gcmd_cfi'  is similar to the function 'vtd_handle_gcmd_ire' and  'vtd_handle_gcmd_te'. So for consistency, I also put comments within the branches instead of in front of 'if'. Besides, I think the comments only explain the statement 'vtd_set_clear_mask_long'.

The 'vtd_handle_gcmd_ire' function:

ok fine

Quoted 16 lines
Quoted 16 lines

/* Handle Interrupt Remap Enable/Disable */
static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en) {
     trace_vtd_ir_enable(en);

     if (en) {
         s->intr_enabled = true;
         /* Ok - report back to driver */ vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES); } else {
         s->intr_enabled = false;
         /* Ok - report back to driver */ vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0); }
}

#4 lore raw
In reply to Yuke Peng
Quoted 50 lines · Yuke Peng

According to Intel VT-d specification 5.1.4, CFI must be blocked when Extended Interrupt Mode is enabled or Compatibility format interrupts are disabled.

---
 hw/i386/intel_iommu.c         | 28 ++++++++++++++++++++++++++++
 hw/i386/trace-events          |  1 +
 include/hw/i386/intel_iommu.h |  1 +
 3 files changed, 30 insertions(+)
hw/i386/intel_iommu.c +20 -0
index 5085a6fee3..dfa2f979e7 100644
@@ -2410,6 +2410,22 @@ static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
     }
 }
 
+/* Handle Compatibility Format Interrupts Enable/Disable */
+static void vtd_handle_gcmd_cfi(IntelIOMMUState *s, bool en)
+{
+    trace_vtd_cfi_enable(en);
+
+    if (en) {
+        s->cfi_enabled = true;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_CFIS);
+    } else {
+        s->cfi_enabled = false;
+        /* Ok - report back to driver */
+        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_CFIS, 0);
+    }
+}
+
 /* Handle write to Global Command Register */
 static void vtd_handle_gcmd_write(IntelIOMMUState *s)
 {
@@ -2440,6 +2456,10 @@ static void vtd_handle_gcmd_write(IntelIOMMUState *s)
         /* Interrupt remap enable/disable */
         vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
     }
+    if (changed & VTD_GCMD_CFI) {
+        /* Compatibility format interrupts enable/disable */
+        vtd_handle_gcmd_cfi(s, val & VTD_GCMD_CFI);
+    }
 }
 
 /* Handle write to Context Command Register */

hw/i386/intel_iommu.c static const VMStateDescription vtd_vmstate = { · line 3330

Quoted 7 lines
hw/i386/intel_iommu.c +1 -0
@@ -3304,6 +3324,7 @@ static const VMStateDescription vtd_vmstate = {
         VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
         VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
         VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
+        VMSTATE_BOOL(cfi_enabled, IntelIOMMUState),
         VMSTATE_BOOL(intr_eime, IntelIOMMUState),
         VMSTATE_END_OF_LIST()
     }

Looks like this will break migration compat.

The normal waywhould be that we use something like VMSTATE_UINT32_TEST() or subsections for the new vmstate field. sometimes it's possible to re-generate the field from the registers, then there's no need to add a new field to migration at all, instead calculate it in vtd_post_load.

Quoted 47 lines
hw/i386/intel_iommu.c +7 -0
@@ -3525,6 +3546,12 @@ static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
 
     /* This is compatible mode. */
     if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
+        if (iommu->intr_eime || !iommu->cfi_enabled) {
+            if (do_fault) {
+                vtd_report_ir_fault(iommu, sid, VTD_FR_IR_REQ_COMPAT, 0);
+            }
+            return -EINVAL;
+        }
         memcpy(translated, origin, sizeof(*origin));
         goto out;
     }
@@ -3950,6 +3977,7 @@ static void vtd_init(IntelIOMMUState *s)
     s->root_scalable = false;
     s->dmar_enabled = false;
     s->intr_enabled = false;
+    s->cfi_enabled = false;
     s->iq_head = 0;
     s->iq_tail = 0;
     s->iq = 0;
hw/i386/trace-events +1 -0
index 53c02d7ac8..ffd87db65f 100644
@@ -57,6 +57,7 @@ vtd_dmar_translate(uint8_t bus, uint8_t slot, uint8_t func, uint64_t iova, uint6
 vtd_dmar_enable(bool en) "enable %d"
 vtd_dmar_fault(uint16_t sid, int fault, uint64_t addr, bool is_write) "sid 0x%"PRIx16" fault %d addr 0x%"PRIx64" write %d"
 vtd_ir_enable(bool en) "enable %d"
+vtd_cfi_enable(bool en) "enable %d"
 vtd_ir_irte_get(int index, uint64_t lo, uint64_t hi) "index %d low 0x%"PRIx64" high 0x%"PRIx64
 vtd_ir_remap(int index, int tri, int vec, int deliver, uint32_t dest, int dest_mode) "index %d trigger %d vector %d deliver %d dest 0x%"PRIx32" mode %d"
 vtd_ir_remap_type(const char *type) "%s"
include/hw/i386/intel_iommu.h +1 -0
index 7fa0a695c8..38e20d0f2c 100644
@@ -294,6 +294,7 @@ struct IntelIOMMUState {
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
+    bool cfi_enabled;               /* Whether CFI is enabled */
     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 */
Signature · 1 line
2.34.1