Skip to contentSkip to conversation

arm/arm64: KVM: VGIC: limit ITARGETSR bits to number of VCPUs

PATCH Superseded Andre Przywara 2 participants 3 messages Nov 15, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm/arm64: KVM: VGIC: limit ITARGETSR bits to number of VCPUs

The GICv2 spec says in section 4.3.12 that a "CPU targets field bit that corresponds to an unimplemented CPU interface is RAZ/WI." Currently we allow the guest to write any value in there and it can read that back.
Mask the written value with the proper CPU mask to be spec compliant.

---
 virt/kvm/arm/vgic/vgic-mmio-v2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -1
index b44b359..e59d4c7 100644
@@ -129,6 +129,7 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
 				   unsigned long val)
 {
 	u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
+	u8 cpu_mask = (1 << atomic_read(&vcpu->kvm->online_vcpus)) - 1;
 	int i;
 
 	/* GICD_ITARGETSR[0-7] are read-only */
@@ -141,7 +142,7 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
 
 		spin_lock(&irq->irq_lock);
 
-		irq->targets = (val >> (i * 8)) & 0xff;
+		irq->targets = ((val >> (i * 8)) & 0xff) & cpu_mask;
 		target = irq->targets ? __ffs(irq->targets) : 0;
 		irq->target_vcpu = kvm_get_vcpu(vcpu->kvm, target);
 
Signature · 1 line
2.9.0
#1 lore raw

Hi Andre,

Quoted 16 lines · Andre Przywara

The GICv2 spec says in section 4.3.12 that a "CPU targets field bit that corresponds to an unimplemented CPU interface is RAZ/WI." Currently we allow the guest to write any value in there and it can read that back.
Mask the written value with the proper CPU mask to be spec compliant.

---
 virt/kvm/arm/vgic/vgic-mmio-v2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +0 -0
index b44b359..e59d4c7 100644

virt/kvm/arm/vgic/vgic-mmio-v2.c static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, · line 132

Quoted 4 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -0
@@ -129,6 +129,7 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
 				   unsigned long val)
 {
 	u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
+	u8 cpu_mask = (1 << atomic_read(&vcpu->kvm->online_vcpus)) - 1;

For the sake of avoiding open-coding things, how about using GENMASK?

virt/kvm/arm/vgic/vgic-mmio-v2.c static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, · line 145

Quoted 9 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -1
 	int i;
 
 	/* GICD_ITARGETSR[0-7] are read-only */
@@ -141,7 +142,7 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
 
 		spin_lock(&irq->irq_lock);
 
-		irq->targets = (val >> (i * 8)) & 0xff;
+		irq->targets = ((val >> (i * 8)) & 0xff) & cpu_mask;

Can't you just drop the '& 0xff' part, since cpu_mask is guaranteed to be more restrictive?

virt/kvm/arm/vgic/vgic-mmio-v2.c static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, · line 148

Quoted 4 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +0 -0
 		target = irq->targets ? __ffs(irq->targets) : 0;
 		irq->target_vcpu = kvm_get_vcpu(vcpu->kvm, target);
 

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#2 lore raw

Hi Marc,

Quoted 14 lines · Marc Zyngier

Hi Andre,

Quoted 11 lines · Andre Przywara

The GICv2 spec says in section 4.3.12 that a "CPU targets field bit that corresponds to an unimplemented CPU interface is RAZ/WI." Currently we allow the guest to write any value in there and it can read that back.
Mask the written value with the proper CPU mask to be spec compliant.

---
 virt/kvm/arm/vgic/vgic-mmio-v2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 11 lines

virt/kvm/arm/vgic/vgic-mmio-v2.c static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, · line 132

Quoted 9 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -0
index b44b359..e59d4c7 100644
@@ -129,6 +129,7 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
 				   unsigned long val)
 {
 	u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
+	u8 cpu_mask = (1 << atomic_read(&vcpu->kvm->online_vcpus)) - 1;

For the sake of avoiding open-coding things, how about using GENMASK?

Yes.

Quoted 6 lines
Quoted 6 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +0 -0
 	int i;
 
 	/* GICD_ITARGETSR[0-7] are read-only */
@@ -141,7 +142,7 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu,
 
Quoted 7 lines

virt/kvm/arm/vgic/vgic-mmio-v2.c static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, · line 145

Quoted 4 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -1
 		spin_lock(&irq->irq_lock);
 
-		irq->targets = (val >> (i * 8)) & 0xff;
+		irq->targets = ((val >> (i * 8)) & 0xff) & cpu_mask;

Can't you just drop the '& 0xff' part, since cpu_mask is guaranteed to be more restrictive?

Well, and also irq->targets is an u8 ...

Fixed both.

Thanks!
Andre.

Quoted 9 lines

virt/kvm/arm/vgic/vgic-mmio-v2.c static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, · line 148

Quoted 4 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +0 -0
 		target = irq->targets ? __ffs(irq->targets) : 0;
 		irq->target_vcpu = kvm_get_vcpu(vcpu->kvm, target);
 

Thanks,

	M.