Skip to contentSkip to conversation

KVM: arm64: Ensure LRs are clear when they should be

PATCH In review Christoffer Dall 2 participants 4 messages Mar 18, 2017 – Mar 20, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: Ensure LRs are clear when they should be

From: Christoffer Dall <[email protected]>

We currently have some code to clear the list registers on GICv3, but we never call this code, because the caller got nuked when removing the old vgic. We also used to have a similar GICv2 part, but that got lost in the process too.

Let's reintroduce the logic for GICv2 and call the logic when we initialize the use of hypervisors on the CPU, for example when first loading KVM or when exiting a low power state.

---
Marc, Eric, Andre,

I'm unable to convince myself that the LRs should already be clear via the reset of the hardware, and for any power management scenario I suppose it's possible for secure-side firmware to have messed with the LRs behind our backs; plus we used to have this functionality and it got dropped for the new vgic. Am I forgetting some discussion where we decided it wasn't needed anymore?

Thanks,
-Christoffer

 arch/arm/kvm/arm.c            |  3 +++
 include/kvm/arm_vgic.h        |  1 +
 virt/kvm/arm/vgic/vgic-init.c | 19 +++++++++++++++++++
 virt/kvm/arm/vgic/vgic-v2.c   | 15 +++++++++++++++
 virt/kvm/arm/vgic/vgic.h      |  2 ++
 5 files changed, 40 insertions(+)
arch/arm/kvm/arm.c +3 -0
index c9a2103..d775aac 100644
@@ -1121,6 +1121,9 @@ static void cpu_hyp_reinit(void)
 		if (__hyp_get_vectors() == hyp_default_vectors)
 			cpu_init_hyp_mode(NULL);
 	}
+
+	if (vgic_present)
+		kvm_vgic_init_cpu_hardware();
 }
 
 static void cpu_hyp_reset(void)
include/kvm/arm_vgic.h +1 -0
index b72dd2a..c0b3d99 100644
@@ -295,6 +295,7 @@ void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 int kvm_vgic_map_resources(struct kvm *kvm);
 int kvm_vgic_hyp_init(void);
+void kvm_vgic_init_cpu_hardware(void);
 
 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
 			bool level);
virt/kvm/arm/vgic/vgic-init.c +19 -0
index 276139a..702f810 100644
@@ -392,6 +392,25 @@ static irqreturn_t vgic_maintenance_handler(int irq, void *data)
 }
 
 /**
+ * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
+ *
+ * For a specific CPU, initialize the GIC VE hardware.
+ */
+void kvm_vgic_init_cpu_hardware(void)
+{
+	BUG_ON(preemptible());
+
+	/*
+	 * We want to make sure the list registers start out clear so that we
+	 * only have the program the used registers.
+	 */
+	if (kvm_vgic_global_state.type == VGIC_V2)
+		vgic_v2_init_lrs();
+	else
+		kvm_call_hyp(__vgic_v3_init_lrs);
+}
+
+/**
  * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  * according to the host GIC model. Accordingly calls either
  * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
virt/kvm/arm/vgic/vgic-v2.c +15 -0
index b834ecd..94cf4b9 100644
@@ -36,6 +36,21 @@ static unsigned long *u64_to_bitmask(u64 *val)
 	return (unsigned long *)val;
 }
 
+static inline void vgic_v2_write_lr(int lr, u32 val)
+{
+	void __iomem *base = kvm_vgic_global_state.vctrl_base;
+
+	writel_relaxed(val, base + GICH_LR0 + (lr * 4));
+}
+
+void vgic_v2_init_lrs(void)
+{
+	int i;
+
+	for (i = 0; i < kvm_vgic_global_state.nr_lr; i++)
+		vgic_v2_write_lr(i, 0);
+}
+
 void vgic_v2_process_maintenance(struct kvm_vcpu *vcpu)
 {
 	struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
virt/kvm/arm/vgic/vgic.h +2 -0
index db28f7c..91566f5 100644
@@ -130,6 +130,8 @@ int vgic_v2_map_resources(struct kvm *kvm);
 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
 			     enum vgic_type);
 
+void vgic_v2_init_lrs(void);
+
 static inline void vgic_get_irq_kref(struct vgic_irq *irq)
 {
 	if (irq->intid < VGIC_MIN_LPI)
Signature · 1 line
2.9.0
Reviewed-by on v1 1/1
#1 lore raw

On Sat, Mar 18 2017 at 12:56:56 pm GMT, Christoffer Dall <[email protected]> wrote:

Hi Christoffer,

Quoted 15 lines

From: Christoffer Dall <[email protected]>

We currently have some code to clear the list registers on GICv3, but we never call this code, because the caller got nuked when removing the old vgic. We also used to have a similar GICv2 part, but that got lost in the process too.

Let's reintroduce the logic for GICv2 and call the logic when we initialize the use of hypervisors on the CPU, for example when first loading KVM or when exiting a low power state.

---
Marc, Eric, Andre,

Quoted 6 lines

I'm unable to convince myself that the LRs should already be clear via the reset of the hardware, and for any power management scenario I suppose it's possible for secure-side firmware to have messed with the LRs behind our backs; plus we used to have this functionality and it got dropped for the new vgic. Am I forgetting some discussion where we decided it wasn't needed anymore?

No, that's definitely something we overlooked while transitioning from one implementation to another. Thanks for noticing it!

Quoted 16 lines

Thanks,

(quoted diff) +0 -1
-Christoffer
 
 arch/arm/kvm/arm.c            |  3 +++
 include/kvm/arm_vgic.h        |  1 +
 virt/kvm/arm/vgic/vgic-init.c | 19 +++++++++++++++++++
 virt/kvm/arm/vgic/vgic-v2.c   | 15 +++++++++++++++
 virt/kvm/arm/vgic/vgic.h      |  2 ++
 5 files changed, 40 insertions(+)
arch/arm/kvm/arm.c +0 -0
index c9a2103..d775aac 100644

arch/arm/kvm/arm.c static void cpu_hyp_reinit(void) · line 1126

Quoted 6 lines
arch/arm/kvm/arm.c +3 -0
@@ -1121,6 +1121,9 @@ static void cpu_hyp_reinit(void)
 		if (__hyp_get_vectors() == hyp_default_vectors)
 			cpu_init_hyp_mode(NULL);
 	}
+
+	if (vgic_present)
+		kvm_vgic_init_cpu_hardware();

We didn't have this before, but that's certainly an improvement. It is not so much that secure could have messed with the LRs themselves, but that the LRs reset value is UNDEF out of reset.

Quoted 57 lines
arch/arm/kvm/arm.c +0 -0
 }
 
 static void cpu_hyp_reset(void)
include/kvm/arm_vgic.h +1 -0
index b72dd2a..c0b3d99 100644
@@ -295,6 +295,7 @@ void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 int kvm_vgic_map_resources(struct kvm *kvm);
 int kvm_vgic_hyp_init(void);
+void kvm_vgic_init_cpu_hardware(void);
 
 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
 			bool level);
virt/kvm/arm/vgic/vgic-init.c +19 -0
index 276139a..702f810 100644
@@ -392,6 +392,25 @@ static irqreturn_t vgic_maintenance_handler(int irq, void *data)
 }
 
 /**
+ * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
+ *
+ * For a specific CPU, initialize the GIC VE hardware.
+ */
+void kvm_vgic_init_cpu_hardware(void)
+{
+	BUG_ON(preemptible());
+
+	/*
+	 * We want to make sure the list registers start out clear so that we
+	 * only have the program the used registers.
+	 */
+	if (kvm_vgic_global_state.type == VGIC_V2)
+		vgic_v2_init_lrs();
+	else
+		kvm_call_hyp(__vgic_v3_init_lrs);
+}
+
+/**
  * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  * according to the host GIC model. Accordingly calls either
  * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
virt/kvm/arm/vgic/vgic-v2.c +4 -0
index b834ecd..94cf4b9 100644
@@ -36,6 +36,21 @@ static unsigned long *u64_to_bitmask(u64 *val)
 	return (unsigned long *)val;
 }
 
+static inline void vgic_v2_write_lr(int lr, u32 val)
+{
+	void __iomem *base = kvm_vgic_global_state.vctrl_base;
+

virt/kvm/arm/vgic/vgic-v2.c static unsigned long *u64_to_bitmask(u64 *val) · line 52

Quoted 10 lines
virt/kvm/arm/vgic/vgic-v2.c +10 -0
+	writel_relaxed(val, base + GICH_LR0 + (lr * 4));
+}
+
+void vgic_v2_init_lrs(void)
+{
+	int i;
+
+	for (i = 0; i < kvm_vgic_global_state.nr_lr; i++)
+		vgic_v2_write_lr(i, 0);
+}

Nit: do you need to have two functions here? Or is that something that you're going to reuse in the near future?

Quoted 9 lines
virt/kvm/arm/vgic/vgic-v2.c +1 -0
+
 void vgic_v2_process_maintenance(struct kvm_vcpu *vcpu)
 {
 	struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
virt/kvm/arm/vgic/vgic.h +0 -0
index db28f7c..91566f5 100644

virt/kvm/arm/vgic/vgic.h int vgic_v2_map_resources(struct kvm *kvm); · line 137

Quoted 8 lines
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -130,6 +130,8 @@ int vgic_v2_map_resources(struct kvm *kvm);
 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
 			     enum vgic_type);
 
+void vgic_v2_init_lrs(void);
+
 static inline void vgic_get_irq_kref(struct vgic_irq *irq)
 {
 	if (irq->intid < VGIC_MIN_LPI)

Looks good to me!

Thanks,

	M.
Signature · 1 line
Jazz is not dead, it just smell funny.
#2 lore raw
Quoted 40 lines · Marc Zyngier

On Sat, Mar 18 2017 at 12:56:56 pm GMT, Christoffer Dall <[email protected]> wrote:

Hi Christoffer,

Quoted 15 lines

From: Christoffer Dall <[email protected]>

We currently have some code to clear the list registers on GICv3, but we never call this code, because the caller got nuked when removing the old vgic. We also used to have a similar GICv2 part, but that got lost in the process too.

Let's reintroduce the logic for GICv2 and call the logic when we initialize the use of hypervisors on the CPU, for example when first loading KVM or when exiting a low power state.

---
Marc, Eric, Andre,

Quoted 6 lines

I'm unable to convince myself that the LRs should already be clear via the reset of the hardware, and for any power management scenario I suppose it's possible for secure-side firmware to have messed with the LRs behind our backs; plus we used to have this functionality and it got dropped for the new vgic. Am I forgetting some discussion where we decided it wasn't needed anymore?

No, that's definitely something we overlooked while transitioning from one implementation to another. Thanks for noticing it!

Quoted 11 lines

Thanks,
-Christoffer

 arch/arm/kvm/arm.c            |  3 +++
 include/kvm/arm_vgic.h        |  1 +
 virt/kvm/arm/vgic/vgic-init.c | 19 +++++++++++++++++++
 virt/kvm/arm/vgic/vgic-v2.c   | 15 +++++++++++++++
 virt/kvm/arm/vgic/vgic.h      |  2 ++
 5 files changed, 40 insertions(+)
Quoted 16 lines

arch/arm/kvm/arm.c static void cpu_hyp_reinit(void) · line 1126

Quoted 11 lines
arch/arm/kvm/arm.c +3 -0
index c9a2103..d775aac 100644
@@ -1121,6 +1121,9 @@ static void cpu_hyp_reinit(void)
 		if (__hyp_get_vectors() == hyp_default_vectors)
 			cpu_init_hyp_mode(NULL);
 	}
+
+	if (vgic_present)
+		kvm_vgic_init_cpu_hardware();

We didn't have this before, but that's certainly an improvement. It is not so much that secure could have messed with the LRs themselves, but that the LRs reset value is UNDEF out of reset.

ok, is the other scenario with secure side messing with the LRs not still potentially possible though? (someone impementing a misguided power management solution for example)

Quoted 62 lines
Quoted 62 lines
arch/arm/kvm/arm.c +0 -0
 }
 
 static void cpu_hyp_reset(void)
include/kvm/arm_vgic.h +1 -0
index b72dd2a..c0b3d99 100644
@@ -295,6 +295,7 @@ void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 int kvm_vgic_map_resources(struct kvm *kvm);
 int kvm_vgic_hyp_init(void);
+void kvm_vgic_init_cpu_hardware(void);
 
 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
 			bool level);
virt/kvm/arm/vgic/vgic-init.c +19 -0
index 276139a..702f810 100644
@@ -392,6 +392,25 @@ static irqreturn_t vgic_maintenance_handler(int irq, void *data)
 }
 
 /**
+ * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
+ *
+ * For a specific CPU, initialize the GIC VE hardware.
+ */
+void kvm_vgic_init_cpu_hardware(void)
+{
+	BUG_ON(preemptible());
+
+	/*
+	 * We want to make sure the list registers start out clear so that we
+	 * only have the program the used registers.
+	 */
+	if (kvm_vgic_global_state.type == VGIC_V2)
+		vgic_v2_init_lrs();
+	else
+		kvm_call_hyp(__vgic_v3_init_lrs);
+}
+
+/**
  * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  * according to the host GIC model. Accordingly calls either
  * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
virt/kvm/arm/vgic/vgic-v2.c +9 -0
index b834ecd..94cf4b9 100644
@@ -36,6 +36,21 @@ static unsigned long *u64_to_bitmask(u64 *val)
 	return (unsigned long *)val;
 }
 
+static inline void vgic_v2_write_lr(int lr, u32 val)
+{
+	void __iomem *base = kvm_vgic_global_state.vctrl_base;
+
+	writel_relaxed(val, base + GICH_LR0 + (lr * 4));
+}
+
+void vgic_v2_init_lrs(void)
+{
Quoted 9 lines

virt/kvm/arm/vgic/vgic-v2.c static unsigned long *u64_to_bitmask(u64 *val) · line 52

Quoted 5 lines
virt/kvm/arm/vgic/vgic-v2.c +5 -0
+	int i;
+
+	for (i = 0; i < kvm_vgic_global_state.nr_lr; i++)
+		vgic_v2_write_lr(i, 0);
+}

Nit: do you need to have two functions here? Or is that something that you're going to reuse in the near future?

I have some optimization patches to GICv2 lying around that will eventually use vgic_v2_write_lr directly, but I don't mind combining it now and splitting it later when introducting those patches if you prefer?

Quoted 20 lines
Quoted 9 lines
virt/kvm/arm/vgic/vgic-v2.c +1 -0
+
 void vgic_v2_process_maintenance(struct kvm_vcpu *vcpu)
 {
 	struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
virt/kvm/arm/vgic/vgic.h +0 -0
index db28f7c..91566f5 100644

virt/kvm/arm/vgic/vgic.h int vgic_v2_map_resources(struct kvm *kvm); · line 137

Quoted 8 lines
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -130,6 +130,8 @@ int vgic_v2_map_resources(struct kvm *kvm);
 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
 			     enum vgic_type);
 
+void vgic_v2_init_lrs(void);
+
 static inline void vgic_get_irq_kref(struct vgic_irq *irq)
 {
 	if (irq->intid < VGIC_MIN_LPI)

Looks good to me!

Quoted 2 lines

Thanks,
-Christoffer

#3 lore raw
Quoted 58 lines · Christoffer Dall
Quoted 40 lines · Marc Zyngier

On Sat, Mar 18 2017 at 12:56:56 pm GMT, Christoffer Dall <[email protected]> wrote:

Hi Christoffer,

Quoted 15 lines

From: Christoffer Dall <[email protected]>

We currently have some code to clear the list registers on GICv3, but we never call this code, because the caller got nuked when removing the old vgic. We also used to have a similar GICv2 part, but that got lost in the process too.

Let's reintroduce the logic for GICv2 and call the logic when we initialize the use of hypervisors on the CPU, for example when first loading KVM or when exiting a low power state.

---
Marc, Eric, Andre,

Quoted 6 lines

I'm unable to convince myself that the LRs should already be clear via the reset of the hardware, and for any power management scenario I suppose it's possible for secure-side firmware to have messed with the LRs behind our backs; plus we used to have this functionality and it got dropped for the new vgic. Am I forgetting some discussion where we decided it wasn't needed anymore?

No, that's definitely something we overlooked while transitioning from one implementation to another. Thanks for noticing it!

Quoted 11 lines

Thanks,
-Christoffer

 arch/arm/kvm/arm.c            |  3 +++
 include/kvm/arm_vgic.h        |  1 +
 virt/kvm/arm/vgic/vgic-init.c | 19 +++++++++++++++++++
 virt/kvm/arm/vgic/vgic-v2.c   | 15 +++++++++++++++
 virt/kvm/arm/vgic/vgic.h      |  2 ++
 5 files changed, 40 insertions(+)
Quoted 16 lines

arch/arm/kvm/arm.c static void cpu_hyp_reinit(void) · line 1126

Quoted 11 lines
arch/arm/kvm/arm.c +3 -0
index c9a2103..d775aac 100644
@@ -1121,6 +1121,9 @@ static void cpu_hyp_reinit(void)
 		if (__hyp_get_vectors() == hyp_default_vectors)
 			cpu_init_hyp_mode(NULL);
 	}
+
+	if (vgic_present)
+		kvm_vgic_init_cpu_hardware();

We didn't have this before, but that's certainly an improvement. It is not so much that secure could have messed with the LRs themselves, but that the LRs reset value is UNDEF out of reset.

Quoted 3 lines

ok, is the other scenario with secure side messing with the LRs not still potentially possible though? (someone impementing a misguided power management solution for example)

Once you end-up on the secure side, everything is possible, including wiping memory. We have to trust that it is not going to do anything silly... My approach to the secure side is that it is part of the HW. If it screws you over, you're done.

Quoted 73 lines
Quoted 63 lines
Quoted 63 lines
arch/arm/kvm/arm.c +0 -0
 }
 
 static void cpu_hyp_reset(void)
include/kvm/arm_vgic.h +1 -0
index b72dd2a..c0b3d99 100644
@@ -295,6 +295,7 @@ void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 int kvm_vgic_map_resources(struct kvm *kvm);
 int kvm_vgic_hyp_init(void);
+void kvm_vgic_init_cpu_hardware(void);
 
 int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
 			bool level);
virt/kvm/arm/vgic/vgic-init.c +19 -0
index 276139a..702f810 100644
@@ -392,6 +392,25 @@ static irqreturn_t vgic_maintenance_handler(int irq, void *data)
 }
 
 /**
+ * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
+ *
+ * For a specific CPU, initialize the GIC VE hardware.
+ */
+void kvm_vgic_init_cpu_hardware(void)
+{
+	BUG_ON(preemptible());
+
+	/*
+	 * We want to make sure the list registers start out clear so that we
+	 * only have the program the used registers.
+	 */
+	if (kvm_vgic_global_state.type == VGIC_V2)
+		vgic_v2_init_lrs();
+	else
+		kvm_call_hyp(__vgic_v3_init_lrs);
+}
+
+/**
  * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  * according to the host GIC model. Accordingly calls either
  * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
virt/kvm/arm/vgic/vgic-v2.c +9 -0
index b834ecd..94cf4b9 100644
@@ -36,6 +36,21 @@ static unsigned long *u64_to_bitmask(u64 *val)
 	return (unsigned long *)val;
 }
 
+static inline void vgic_v2_write_lr(int lr, u32 val)
+{
+	void __iomem *base = kvm_vgic_global_state.vctrl_base;
+
+	writel_relaxed(val, base + GICH_LR0 + (lr * 4));
+}
+
+void vgic_v2_init_lrs(void)
+{
Quoted 9 lines

virt/kvm/arm/vgic/vgic-v2.c static unsigned long *u64_to_bitmask(u64 *val) · line 52

Quoted 5 lines
virt/kvm/arm/vgic/vgic-v2.c +5 -0
+	int i;
+
+	for (i = 0; i < kvm_vgic_global_state.nr_lr; i++)
+		vgic_v2_write_lr(i, 0);
+}

Nit: do you need to have two functions here? Or is that something that you're going to reuse in the near future?

Quoted 4 lines

I have some optimization patches to GICv2 lying around that will eventually use vgic_v2_write_lr directly, but I don't mind combining it now and splitting it later when introducting those patches if you prefer?

No, that's fine. I'll queue it as is.

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...