Skip to contentSkip to conversation

hw/intc: handle GICD_TYPER2 for KVM GICv3

PATCH Superseded Peter Maydell 2 participants 7 messages Jul 7, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/intc: handle GICD_TYPER2 for KVM GICv3

The GICD_TYPER2 GICv3 distributor register is one that is added for GICv4.1; previously this was architected as a RES0 location. Our TCG GIC doesn't implement GICv4.1, but for KVM the kernel might support it.

This patchset:
* makes GICD_TYPER0 reads not trigger a bad-read trace event on the TCG GICv3, for the benefit of GICv4.1-aware guest code
* migrates the GICD_TYPER2 register value on a KVM GIC, so that a mismatch between source and destination can be caught by the destination kernel

Note that I have only very lightly tested this, on a host which (I believe) doesn't have a GICv4.1.

thanks
-- PMM

Peter Maydell (2):
hw/intc/arm_gicv3_dist: Implement GICD_TYPER2 as 0 hw/intc/arm_gicv3_kvm: Migrate GICD_TYPER2

 hw/intc/gicv3_internal.h           |  1 +
 include/hw/intc/arm_gicv3_common.h |  6 ++++++
 hw/intc/arm_gicv3_common.c         | 24 ++++++++++++++++++++++++
 hw/intc/arm_gicv3_dist.c           |  9 +++++++++
 hw/intc/arm_gicv3_kvm.c            |  6 ++++++
 5 files changed, 46 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 hw/intc/arm_gicv3_dist: Implement GICD_TYPER2 as 0

The GIC distributor registers GICD_TYPER2 is present when the GICv4.1 is implemented, and RES0 otherwise. QEMU's TCG implementation is only GICv4.0, so this register is RES0. However, since it's reasonable for GICv4.1-aware software to read the register, expecting the zero for GICv3 and GICv4.0, implement the case to avoid it being logged as an invalid guest read.

---
 hw/intc/gicv3_internal.h | 1 +
 hw/intc/arm_gicv3_dist.c | 9 +++++++++
 2 files changed, 10 insertions(+)
hw/intc/gicv3_internal.h +1 -0
index bc9f518fe86..fc586524f56 100644
@@ -31,6 +31,7 @@
 #define GICD_CTLR            0x0000
 #define GICD_TYPER           0x0004
 #define GICD_IIDR            0x0008
+#define GICD_TYPER2          0x000C
 #define GICD_STATUSR         0x0010
 #define GICD_SETSPI_NSR      0x0040
 #define GICD_CLRSPI_NSR      0x0048
hw/intc/arm_gicv3_dist.c +9 -0
index d8207acb22c..a7d10ed9493 100644
@@ -431,6 +431,15 @@ static bool gicd_readl(GICv3State *s, hwaddr offset,
             (0xf << 19) | itlinesnumber;
         return true;
     }
+    case GICD_TYPER2:
+        /*
+         * This register only exists for GICv4.1, which QEMU doesn't
+         * currently emulate. On GICv3 and GICv4 it's defined to be RES0.
+         * We implement as read-zero here to avoid tracing a bad-register-read
+         * if GICv4.1-aware software reads this ID register.
+         */
+        *data = 0;
+        return true;
     case GICD_IIDR:
         /* We claim to be an ARM r0p0 with a zero ProductID.
          * This is the same as an r0p0 GIC-500.
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/intc/arm_gicv3_dist: Implement GICD_TYPER2 as 0

Quoted 8 lines · Peter Maydell

The GIC distributor registers GICD_TYPER2 is present when the GICv4.1 is implemented, and RES0 otherwise. QEMU's TCG implementation is only GICv4.0, so this register is RES0. However, since it's reasonable for GICv4.1-aware software to read the register, expecting the zero for GICv3 and GICv4.0, implement the case to avoid it being logged as an invalid guest read.

Eric

Quoted 37 lines
---
 hw/intc/gicv3_internal.h | 1 +
 hw/intc/arm_gicv3_dist.c | 9 +++++++++
 2 files changed, 10 insertions(+)
hw/intc/gicv3_internal.h +1 -0
index bc9f518fe86..fc586524f56 100644
@@ -31,6 +31,7 @@
 #define GICD_CTLR            0x0000
 #define GICD_TYPER           0x0004
 #define GICD_IIDR            0x0008
+#define GICD_TYPER2          0x000C
 #define GICD_STATUSR         0x0010
 #define GICD_SETSPI_NSR      0x0040
 #define GICD_CLRSPI_NSR      0x0048
hw/intc/arm_gicv3_dist.c +9 -0
index d8207acb22c..a7d10ed9493 100644
@@ -431,6 +431,15 @@ static bool gicd_readl(GICv3State *s, hwaddr offset,
             (0xf << 19) | itlinesnumber;
         return true;
     }
+    case GICD_TYPER2:
+        /*
+         * This register only exists for GICv4.1, which QEMU doesn't
+         * currently emulate. On GICv3 and GICv4 it's defined to be RES0.
+         * We implement as read-zero here to avoid tracing a bad-register-read
+         * if GICv4.1-aware software reads this ID register.
+         */
+        *data = 0;
+        return true;
     case GICD_IIDR:
         /* We claim to be an ARM r0p0 with a zero ProductID.
          * This is the same as an r0p0 GIC-500.
#3 lore raw

PATCH 2/2 hw/intc/arm_gicv3_kvm: Migrate GICD_TYPER2

In reply to Peter Maydell

The GICD_TYPER2 register is new for GICv4.1. As an ID register, we migrate it so that on the destination the kernel can check that the destination supports the same configuration that the source system had. This avoids confusing behaviour if the user tries to migrate a VM from a GICv3 system to a GICv4.1 system or vice-versa. (In practice the inability to migrate between different CPU types probably already prevented this.)

Note that older kernels pre-dating GICv4.1 support in the KVM GIC will just ignore whatever userspace writes to GICD_TYPER2, so migration where the destination is one of those kernels won't have this safety-check.

(The reporting of a mismatch will be a bit clunky, because this file currently uses error_abort for all failures to write the data to the kernel. Ideally we would fix this by making them propagate the failure information back up to the post_load hook return value, to cause a migration failure.)

---
 include/hw/intc/arm_gicv3_common.h |  6 ++++++
 hw/intc/arm_gicv3_common.c         | 24 ++++++++++++++++++++++++
 hw/intc/arm_gicv3_kvm.c            |  6 ++++++
 3 files changed, 36 insertions(+)
include/hw/intc/arm_gicv3_common.h +6 -0
index a3d6a0e5077..bcbcae1fbe7 100644
@@ -267,6 +267,12 @@ struct GICv3State {
     GICv3CPUState *gicd_irouter_target[GICV3_MAXIRQ];
     uint32_t gicd_nsacr[DIV_ROUND_UP(GICV3_MAXIRQ, 16)];
 
+    /*
+     * GICv4.1 extended ID information. This is currently only needed
+     * for migration of a KVM GIC.
+     */
+    uint32_t gicd_typer2;
+
     GICv3CPUState *cpu;
     /* List of all ITSes connected to this GIC */
     GPtrArray *itslist;
hw/intc/arm_gicv3_common.c +24 -0
index 1cee68193ca..7b09771310e 100644
@@ -275,6 +275,29 @@ const VMStateDescription vmstate_gicv3_gicd_nmi = {
     }
 };
 
+static bool gicv3_typer2_needed(void *opaque)
+{
+    /*
+     * GICD_TYPER2 ID register for GICv4.1. Was RES0 for
+     * GICv3 and GICv4.0; don't migrate if zero for backwards
+     * compatibility.
+     */
+    GICv3State *cs = opaque;
+
+    return cs->gicd_typer2 != 0;
+}
+
+const VMStateDescription vmstate_gicv3_gicd_typer2 = {
+    .name = "arm_gicv3/gicd_typer2",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = gicv3_typer2_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT32(gicd_typer2, GICv3State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static const VMStateDescription vmstate_gicv3 = {
     .name = "arm_gicv3",
     .version_id = 1,
@@ -304,6 +327,7 @@ static const VMStateDescription vmstate_gicv3 = {
     .subsections = (const VMStateDescription * const []) {
         &vmstate_gicv3_gicd_no_migration_shift_bug,
         &vmstate_gicv3_gicd_nmi,
+        &vmstate_gicv3_gicd_typer2,
         NULL
     }
 };
hw/intc/arm_gicv3_kvm.c +6 -0
index 3be3bf6c28d..0302beb0c07 100644
@@ -332,6 +332,9 @@ static void kvm_arm_gicv3_put(GICv3State *s)
     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
     redist_typer = ((uint64_t)regh << 32) | regl;
 
+    reg = s->gicd_typer2;
+    kvm_gicd_access(s, GICD_TYPER2, &reg, true);
+
     reg = s->gicd_ctlr;
     kvm_gicd_access(s, GICD_CTLR, &reg, true);
 
@@ -519,6 +522,9 @@ static void kvm_arm_gicv3_get(GICv3State *s)
     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
     redist_typer = ((uint64_t)regh << 32) | regl;
 
+    kvm_gicd_access(s, GICD_TYPER2, &reg, false);
+    s->gicd_typer2 = reg;
+
     kvm_gicd_access(s, GICD_CTLR, &reg, false);
     s->gicd_ctlr = reg;
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/2] hw/intc/arm_gicv3_kvm: Migrate GICD_TYPER2

Hi Peter,

Quoted 90 lines · Peter Maydell

The GICD_TYPER2 register is new for GICv4.1. As an ID register, we migrate it so that on the destination the kernel can check that the destination supports the same configuration that the source system had. This avoids confusing behaviour if the user tries to migrate a VM from a GICv3 system to a GICv4.1 system or vice-versa. (In practice the inability to migrate between different CPU types probably already prevented this.)

Note that older kernels pre-dating GICv4.1 support in the KVM GIC will just ignore whatever userspace writes to GICD_TYPER2, so migration where the destination is one of those kernels won't have this safety-check.

(The reporting of a mismatch will be a bit clunky, because this file currently uses error_abort for all failures to write the data to the kernel. Ideally we would fix this by making them propagate the failure information back up to the post_load hook return value, to cause a migration failure.)

---
 include/hw/intc/arm_gicv3_common.h |  6 ++++++
 hw/intc/arm_gicv3_common.c         | 24 ++++++++++++++++++++++++
 hw/intc/arm_gicv3_kvm.c            |  6 ++++++
 3 files changed, 36 insertions(+)
include/hw/intc/arm_gicv3_common.h +6 -0
index a3d6a0e5077..bcbcae1fbe7 100644
@@ -267,6 +267,12 @@ struct GICv3State {
     GICv3CPUState *gicd_irouter_target[GICV3_MAXIRQ];
     uint32_t gicd_nsacr[DIV_ROUND_UP(GICV3_MAXIRQ, 16)];
 
+    /*
+     * GICv4.1 extended ID information. This is currently only needed
+     * for migration of a KVM GIC.
+     */
+    uint32_t gicd_typer2;
+
     GICv3CPUState *cpu;
     /* List of all ITSes connected to this GIC */
     GPtrArray *itslist;
hw/intc/arm_gicv3_common.c +24 -0
index 1cee68193ca..7b09771310e 100644
@@ -275,6 +275,29 @@ const VMStateDescription vmstate_gicv3_gicd_nmi = {
     }
 };
 
+static bool gicv3_typer2_needed(void *opaque)
+{
+    /*
+     * GICD_TYPER2 ID register for GICv4.1. Was RES0 for
+     * GICv3 and GICv4.0; don't migrate if zero for backwards
+     * compatibility.
+     */
+    GICv3State *cs = opaque;
+
+    return cs->gicd_typer2 != 0;
+}
+
+const VMStateDescription vmstate_gicv3_gicd_typer2 = {
+    .name = "arm_gicv3/gicd_typer2",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = gicv3_typer2_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT32(gicd_typer2, GICv3State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static const VMStateDescription vmstate_gicv3 = {
     .name = "arm_gicv3",
     .version_id = 1,
@@ -304,6 +327,7 @@ static const VMStateDescription vmstate_gicv3 = {
     .subsections = (const VMStateDescription * const []) {
         &vmstate_gicv3_gicd_no_migration_shift_bug,
         &vmstate_gicv3_gicd_nmi,
+        &vmstate_gicv3_gicd_typer2,
         NULL
     }
 };
hw/intc/arm_gicv3_kvm.c +0 -0
index 3be3bf6c28d..0302beb0c07 100644

hw/intc/arm_gicv3_kvm.c static void kvm_arm_gicv3_put(GICv3State *s) · line 337

Quoted 6 lines
hw/intc/arm_gicv3_kvm.c +3 -0
@@ -332,6 +332,9 @@ static void kvm_arm_gicv3_put(GICv3State *s)
     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
     redist_typer = ((uint64_t)regh << 32) | regl;
 
+    reg = s->gicd_typer2;
+    kvm_gicd_access(s, GICD_TYPER2, &reg, true);
+

I don't know if there is any rationale about the access order. I see most of the dist reg accesses are done below in the function after rdist ones, although the GICD_CTLR is also here. If there is a rationale a comment may be useful to avoid another dev to do something wrong. I remember that for the ITS for instance some speicifc ordering needed to be enforced.

While at it you can also fix the missing bracket at /* Distributor state (shared between all CPUs */

same remark on the guest side and same missing bracket ;-)

hw/intc/arm_gicv3_kvm.c static void kvm_arm_gicv3_get(GICv3State *s) · line 530

Quoted 13 lines
hw/intc/arm_gicv3_kvm.c +3 -0
     reg = s->gicd_ctlr;
     kvm_gicd_access(s, GICD_CTLR, &reg, true);
 
@@ -519,6 +522,9 @@ static void kvm_arm_gicv3_get(GICv3State *s)
     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
     redist_typer = ((uint64_t)regh << 32) | regl;
 
+    kvm_gicd_access(s, GICD_TYPER2, &reg, false);
+    s->gicd_typer2 = reg;
+
     kvm_gicd_access(s, GICD_CTLR, &reg, false);
     s->gicd_ctlr = reg;
 

Others looks good to me

Eric

#5 lore raw

Re: [PATCH 2/2] hw/intc/arm_gicv3_kvm: Migrate GICD_TYPER2

Quoted 32 lines · Eric Auger

Hi Peter,

Quoted 28 lines · Peter Maydell

The GICD_TYPER2 register is new for GICv4.1. As an ID register, we migrate it so that on the destination the kernel can check that the destination supports the same configuration that the source system had. This avoids confusing behaviour if the user tries to migrate a VM from a GICv3 system to a GICv4.1 system or vice-versa. (In practice the inability to migrate between different CPU types probably already prevented this.)

Note that older kernels pre-dating GICv4.1 support in the KVM GIC will just ignore whatever userspace writes to GICD_TYPER2, so migration where the destination is one of those kernels won't have this safety-check.

(The reporting of a mismatch will be a bit clunky, because this file currently uses error_abort for all failures to write the data to the kernel. Ideally we would fix this by making them propagate the failure information back up to the post_load hook return value, to cause a migration failure.)

hw/intc/arm_gicv3_kvm.c +0 -0
index 3be3bf6c28d..0302beb0c07 100644
@@ -332,6 +332,9 @@ static void kvm_arm_gicv3_put(GICv3State *s)
     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
     redist_typer = ((uint64_t)regh << 32) | regl;
 
Quoted 9 lines

hw/intc/arm_gicv3_kvm.c static void kvm_arm_gicv3_put(GICv3State *s) · line 337

Quoted 3 lines
hw/intc/arm_gicv3_kvm.c +3 -0
+    reg = s->gicd_typer2;
+    kvm_gicd_access(s, GICD_TYPER2, &reg, true);
+

I don't know if there is any rationale about the access order. I see most of the dist reg accesses are done below in the function after rdist ones, although the GICD_CTLR is also here. If there is a rationale a comment may be useful to avoid another dev to do something wrong. I remember that for the ITS for instance some speicifc ordering needed to be enforced.

I think in general we have tried to put in comments to note where the ordering matters. (Though I do suspect the GICD_CTLR write may be early because we need to write the enable bit before doing anything else -- if that's true, we failed to document it.)

In this case I just put this one at the top because "bail out early if the config is wrong" seemed better than doing it late. But it doesn't matter and it's probably more sensible to do it with the other GICD registers. A comment that we only write this ID register to validate the config would also help.

Quoted 2 lines

While at it you can also fix the missing bracket at
/* Distributor state (shared between all CPUs */

Sure.

thanks
-- PMM

#6 lore raw

Re: [PATCH 2/2] hw/intc/arm_gicv3_kvm: Migrate GICD_TYPER2

Quoted 50 lines · Peter Maydell
Quoted 32 lines · Eric Auger

Hi Peter,

Quoted 28 lines · Peter Maydell

The GICD_TYPER2 register is new for GICv4.1. As an ID register, we migrate it so that on the destination the kernel can check that the destination supports the same configuration that the source system had. This avoids confusing behaviour if the user tries to migrate a VM from a GICv3 system to a GICv4.1 system or vice-versa. (In practice the inability to migrate between different CPU types probably already prevented this.)

Note that older kernels pre-dating GICv4.1 support in the KVM GIC will just ignore whatever userspace writes to GICD_TYPER2, so migration where the destination is one of those kernels won't have this safety-check.

(The reporting of a mismatch will be a bit clunky, because this file currently uses error_abort for all failures to write the data to the kernel. Ideally we would fix this by making them propagate the failure information back up to the post_load hook return value, to cause a migration failure.)

hw/intc/arm_gicv3_kvm.c +0 -0
index 3be3bf6c28d..0302beb0c07 100644
@@ -332,6 +332,9 @@ static void kvm_arm_gicv3_put(GICv3State *s)
     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
     redist_typer = ((uint64_t)regh << 32) | regl;
 
Quoted 9 lines

hw/intc/arm_gicv3_kvm.c static void kvm_arm_gicv3_put(GICv3State *s) · line 337

Quoted 3 lines
hw/intc/arm_gicv3_kvm.c +3 -0
+    reg = s->gicd_typer2;
+    kvm_gicd_access(s, GICD_TYPER2, &reg, true);
+

I don't know if there is any rationale about the access order. I see most of the dist reg accesses are done below in the function after rdist ones, although the GICD_CTLR is also here. If there is a rationale a comment may be useful to avoid another dev to do something wrong. I remember that for the ITS for instance some speicifc ordering needed to be enforced.

I think in general we have tried to put in comments to note where the ordering matters. (Though I do suspect the GICD_CTLR write may be early because we need to write the enable bit before doing anything else -- if that's true, we failed to document it.)

Quoted 5 lines

In this case I just put this one at the top because "bail out early if the config is wrong" seemed better than doing it late. But it doesn't matter and it's probably more sensible to do it with the other GICD registers. A comment that we only write this ID register to validate the config would also help.

Also I realised that this probably fails on a GICv4.1 host, because I think I have the reset handling wrong. On reset the gicd_typer2 field in the struct will be zero, and then the reset_hold function calls kvm_arm_gicv3_put(), which will try to write the 0 into the kernel, which will fail.

-- PMM