KVM: arm64: vgic-v3: Advertise GICR_CTLR.{IR, CES} as a new GICD_IIDR revision
authorMarc Zyngier <maz@kernel.org>
Tue, 5 Apr 2022 18:23:27 +0000 (19:23 +0100)
committerMarc Zyngier <maz@kernel.org>
Wed, 4 May 2022 13:09:53 +0000 (14:09 +0100)
Since adversising GICR_CTLR.{IC,CES} is directly observable from
a guest, we need to make it selectable from userspace.

For that, bump the default GICD_IIDR revision and let userspace
downgrade it to the previous default. For GICv2, the two distributor
revisions are strictly equivalent.

Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220405182327.205520-5-maz@kernel.org
arch/arm64/kvm/vgic/vgic-init.c
arch/arm64/kvm/vgic/vgic-mmio-v2.c
arch/arm64/kvm/vgic/vgic-mmio-v3.c
arch/arm64/kvm/vgic/vgic.h
include/kvm/arm_vgic.h

index fc00304fe7d8a7f0863a389bab32f9a8ea73cad3..f84e04f334c68c0e6c37f4e1e2b1007aada3cdc3 100644 (file)
@@ -319,7 +319,12 @@ int vgic_init(struct kvm *kvm)
 
        vgic_debug_init(kvm);
 
-       dist->implementation_rev = 2;
+       /*
+        * If userspace didn't set the GIC implementation revision,
+        * default to the latest and greatest. You know want it.
+        */
+       if (!dist->implementation_rev)
+               dist->implementation_rev = KVM_VGIC_IMP_REV_LATEST;
        dist->initialized = true;
 
 out:
index 12e4c223e6b8cdfa86d395d19b4b64a4f7ea36b6..77a67e9d3d14b947fa8e8369da4a184024bf03df 100644 (file)
@@ -73,9 +73,13 @@ static int vgic_mmio_uaccess_write_v2_misc(struct kvm_vcpu *vcpu,
                                           gpa_t addr, unsigned int len,
                                           unsigned long val)
 {
+       struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
+       u32 reg;
+
        switch (addr & 0x0c) {
        case GIC_DIST_IIDR:
-               if (val != vgic_mmio_read_v2_misc(vcpu, addr, len))
+               reg = vgic_mmio_read_v2_misc(vcpu, addr, len);
+               if ((reg ^ val) & ~GICD_IIDR_REVISION_MASK)
                        return -EINVAL;
 
                /*
@@ -87,8 +91,16 @@ static int vgic_mmio_uaccess_write_v2_misc(struct kvm_vcpu *vcpu,
                 * migration from old kernels to new kernels with legacy
                 * userspace.
                 */
-               vcpu->kvm->arch.vgic.v2_groups_user_writable = true;
-               return 0;
+               reg = FIELD_GET(GICD_IIDR_REVISION_MASK, reg);
+               switch (reg) {
+               case KVM_VGIC_IMP_REV_2:
+               case KVM_VGIC_IMP_REV_3:
+                       vcpu->kvm->arch.vgic.v2_groups_user_writable = true;
+                       dist->implementation_rev = reg;
+                       return 0;
+               default:
+                       return -EINVAL;
+               }
        }
 
        vgic_mmio_write_v2_misc(vcpu, addr, len, val);
index 9824c773427df71b20f297783f0ccffbf9f76e74..f7aa7bcd6fb8cdaed3272b577fa5fa15de290203 100644 (file)
@@ -155,13 +155,27 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
                                           unsigned long val)
 {
        struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
+       u32 reg;
 
        switch (addr & 0x0c) {
        case GICD_TYPER2:
-       case GICD_IIDR:
                if (val != vgic_mmio_read_v3_misc(vcpu, addr, len))
                        return -EINVAL;
                return 0;
+       case GICD_IIDR:
+               reg = vgic_mmio_read_v3_misc(vcpu, addr, len);
+               if ((reg ^ val) & ~GICD_IIDR_REVISION_MASK)
+                       return -EINVAL;
+
+               reg = FIELD_GET(GICD_IIDR_REVISION_MASK, reg);
+               switch (reg) {
+               case KVM_VGIC_IMP_REV_2:
+               case KVM_VGIC_IMP_REV_3:
+                       dist->implementation_rev = reg;
+                       return 0;
+               default:
+                       return -EINVAL;
+               }
        case GICD_CTLR:
                /* Not a GICv4.1? No HW SGIs */
                if (!kvm_vgic_global_state.has_gicv4_1)
@@ -232,8 +246,13 @@ static unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu,
                                             gpa_t addr, unsigned int len)
 {
        struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+       unsigned long val;
+
+       val = atomic_read(&vgic_cpu->ctlr);
+       if (vgic_get_implementation_rev(vcpu) >= KVM_VGIC_IMP_REV_3)
+               val |= GICR_CTLR_IR | GICR_CTLR_CES;
 
-       return vgic_cpu->lpis_enabled ? GICR_CTLR_ENABLE_LPIS : 0;
+       return val;
 }
 
 static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,
index 1d04a900f3e3630ada6e3a91b914e45f54702f43..4c6bdd321faaa85c1228d661e3ca4ca4b21f4a97 100644 (file)
 #define DEBUG_SPINLOCK_BUG_ON(p)
 #endif
 
+static inline u32 vgic_get_implementation_rev(struct kvm_vcpu *vcpu)
+{
+       return vcpu->kvm->arch.vgic.implementation_rev;
+}
+
 /* Requires the irq_lock to be held by the caller. */
 static inline bool irq_is_pending(struct vgic_irq *irq)
 {
index 401236f97cf28456d17f2eb6f7022f8bbc5cd479..2d8f2e90edc2d88975b63d9d7c08eb47c31611b6 100644 (file)
@@ -231,6 +231,9 @@ struct vgic_dist {
 
        /* Implementation revision as reported in the GICD_IIDR */
        u32                     implementation_rev;
+#define KVM_VGIC_IMP_REV_2     2 /* GICv2 restorable groups */
+#define KVM_VGIC_IMP_REV_3     3 /* GICv3 GICR_CTLR.{IW,CES,RWP} */
+#define KVM_VGIC_IMP_REV_LATEST        KVM_VGIC_IMP_REV_3
 
        /* Userspace can write to GICv2 IGROUPR */
        bool                    v2_groups_user_writable;