Skip to contentSkip to conversation

x86/pvh/vpmu: Disable VPMU for PVH guests

PATCH In review Boris Ostrovsky 3 participants 7 messages Nov 24, 2014 – Nov 25, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/pvh/vpmu: Disable VPMU for PVH guests

Currently when VPMU is enabled on a system both HVM and PVH VPCUs will initialize their VPMUs, including setting up vpmu_ops. As result even though VPMU will not work for PVH guests (APIC is not supported there), the guest may decide to perform a write to a PMU MSR. This will cause a call to is_vlapic_lvtpc_enabled() which will crash the hypervisor, e.g.:

(XEN) Xen call trace:

 (XEN)    [<ffff82d0801ca06f>] is_vlapic_lvtpc_enabled+0x13/0x22
 (XEN)    [<ffff82d0801e2a15>] core2_vpmu_do_wrmsr+0x415/0x589
 (XEN)    [<ffff82d0801cedaa>] vpmu_do_wrmsr+0x2a/0x33
 (XEN)    [<ffff82d0801dd648>] vmx_msr_write_intercept+0x268/0x557
 (XEN)    [<ffff82d0801bcd2e>] hvm_msr_write_intercept+0x36c/0x39b
 (XEN)    [<ffff82d0801e0a0e>] vmx_vmexit_handler+0x1082/0x185b
 (XEN)    [<ffff82d0801e74c1>] vmx_asm_vmexit_handler+0x41/0xc0

If we prevent VPMU from being initialized on PVH guests we will avoid those accesses.

---
 xen/arch/x86/hvm/vpmu.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/x86/hvm/vpmu.c +3 -0
index aec7b5f..4daa993 100644
@@ -218,6 +218,9 @@ void vpmu_initialise(struct vcpu *v)
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     uint8_t vendor = current_cpu_data.x86_vendor;
 
+    if ( is_pvh_vcpu(v) )
+        return;
+
     if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
         vpmu_destroy(v);
     vpmu_clear(vpmu);
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 24.11.14 at 20:49, <[email protected]> wrote:

Currently when VPMU is enabled on a system both HVM and PVH VPCUs will initialize their VPMUs, including setting up vpmu_ops. As result even though VPMU will not work for PVH guests (APIC is not supported there), the guest may decide to perform a write to a PMU MSR. This will cause a call to is_vlapic_lvtpc_enabled() which will crash the hypervisor, e.g.:

(XEN) Xen call trace:

 (XEN)    [<ffff82d0801ca06f>] is_vlapic_lvtpc_enabled+0x13/0x22
 (XEN)    [<ffff82d0801e2a15>] core2_vpmu_do_wrmsr+0x415/0x589
 (XEN)    [<ffff82d0801cedaa>] vpmu_do_wrmsr+0x2a/0x33
 (XEN)    [<ffff82d0801dd648>] vmx_msr_write_intercept+0x268/0x557
 (XEN)    [<ffff82d0801bcd2e>] hvm_msr_write_intercept+0x36c/0x39b
 (XEN)    [<ffff82d0801e0a0e>] vmx_vmexit_handler+0x1082/0x185b
 (XEN)    [<ffff82d0801e74c1>] vmx_asm_vmexit_handler+0x41/0xc0
Quoted 2 lines

If we prevent VPMU from being initialized on PVH guests we will avoid those accesses.

I think this fix is too specific; instead we should mark the LAPIC disabled, which will implicitly prevent the issue afaict - see below.

Jan

x86/PVH: properly disable vLAPIC

Rather than guarding higher level operations (like vPMU initialization as suggested by Boris in
http://lists.xenproject.org/archives/html/xen-devel/2014-11/msg02278.html)
mark the vLAPIC hardware disabled for PVH guests and prevent it from getting moved out of this state.

xen/arch/x86/hvm/hvm.c +3 -3
@@ -2217,8 +2217,7 @@ int hvm_vcpu_initialise(struct vcpu *v)
         goto fail1;
 
     /* NB: vlapic_init must be called before hvm_funcs.vcpu_initialise */
-    if ( is_hvm_vcpu(v) )
-        rc = vlapic_init(v);
+    rc = vlapic_init(v);
     if ( rc != 0 ) /* teardown: vlapic_destroy */
         goto fail2;
 
@@ -4483,7 +4482,8 @@ int hvm_msr_write_intercept(unsigned int
         break;
 
     case MSR_IA32_APICBASE:
-        if ( !vlapic_msr_set(vcpu_vlapic(v), msr_content) )
+        if ( unlikely(is_pvh_vcpu(v)) ||
+             !vlapic_msr_set(vcpu_vlapic(v), msr_content) )
             goto gp_fault;
         break;
 
xen/arch/x86/hvm/vlapic.c +6 -0
@@ -1429,6 +1429,12 @@ int vlapic_init(struct vcpu *v)
 
     HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "%d", v->vcpu_id);
 
+    if ( is_pvh_vcpu(v) )
+    {
+        vlapic->hw.disabled = VLAPIC_HW_DISABLED;
+        return 0;
+    }
+
     vlapic->pt.source = PTSRC_lapic;
 
     if (vlapic->regs_page == NULL)
#2 lore raw

xen/arch/x86/hvm/vlapic.c int vlapic_init(struct vcpu *v) · line 1434

Quoted 7 lines · Jan Beulich
xen/arch/x86/hvm/vlapic.c +3 -0
@@ -1429,6 +1429,12 @@ int vlapic_init(struct vcpu *v)
 
      HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "%d", v->vcpu_id);
 
+    if ( is_pvh_vcpu(v) )
+    {
+        vlapic->hw.disabled = VLAPIC_HW_DISABLED;

I did consider doing that but I thought that this flag is meant to be set when the guest clears MSR_IA32_APICBASE_ENABLE to disable APIC and therefore I'd be overloading it (the flag) in a way.

Regardless, do you think that disabling VPMU for PVH is worth anyway?

-boris

Quoted 9 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+        return 0;
+    }
+
      vlapic->pt.source = PTSRC_lapic;
 
      if (vlapic->regs_page == NULL)
#3 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 15:38, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
xen/arch/x86/hvm/vlapic.c +0 -0
@@ -1429,6 +1429,12 @@ int vlapic_init(struct vcpu *v)
 
      HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "%d", v->vcpu_id);
 
Quoted 8 lines

xen/arch/x86/hvm/vlapic.c int vlapic_init(struct vcpu *v) · line 1434

Quoted 3 lines
xen/arch/x86/hvm/vlapic.c +3 -0
+    if ( is_pvh_vcpu(v) )
+    {
+        vlapic->hw.disabled = VLAPIC_HW_DISABLED;

I did consider doing that but I thought that this flag is meant to be set when the guest clears MSR_IA32_APICBASE_ENABLE to disable APIC and therefore I'd be overloading it (the flag) in a way.

There's no overloading here - we're then simply treating all PVH vCPU-s as having permanently hardware-disabled LAPICs (reflecting current reality).

Quoted 1 line

Regardless, do you think that disabling VPMU for PVH is worth anyway?

That depends on what (bad) consequences not doing so has.

Jan

#4 lore raw
Quoted 3 lines · Jan Beulich
Quoted 2 lines

Regardless, do you think that disabling VPMU for PVH is worth anyway?

That depends on what (bad) consequences not doing so has.

I haven't seen anything (besides VAPIC accesses) but I think it would be prudent to prevent any VPMU activity from happening. I can see, for example MSRs and APIC vector being written. All of which look benign on the first sight but who knows...

-boris

#5 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 17:19, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 2 lines

Regardless, do you think that disabling VPMU for PVH is worth anyway?

That depends on what (bad) consequences not doing so has.

I haven't seen anything (besides VAPIC accesses) but I think it would be prudent to prevent any VPMU activity from happening. I can see, for example MSRs and APIC vector being written. All of which look benign on the first sight but who knows...

Yeah, it's not really a problem to put it in (if Konrad agrees; remember that PVH is still experimental, and hence fixing bugs caused only by it may be out of scope at this point - in any event I think that if your patch is to go in, mine should too).

Jan

#6 lore raw
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 25.11.14 at 17:19, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 2 lines

Regardless, do you think that disabling VPMU for PVH is worth anyway?

That depends on what (bad) consequences not doing so has.

I haven't seen anything (besides VAPIC accesses) but I think it would be prudent to prevent any VPMU activity from happening. I can see, for example MSRs and APIC vector being written. All of which look benign on the first sight but who knows...

Quoted 4 lines

Yeah, it's not really a problem to put it in (if Konrad agrees; remember that PVH is still experimental, and hence fixing bugs caused only by it may be out of scope at this point - in any event I think that if your patch is to go in, mine should too).

The beaty of experimental is that we can add it later in the cycle as at worst they will regress something that is unbaked already.

Quoted 1 line

From that perspective the bar to put fixes for 'experimental' is lower

than normal code. The part that I am worried about is the common paths and this potentially causing regressions on the those.

But the potential for that is low that I am OK with these patches going in.

Quoted 3 lines

Jan