Skip to contentSkip to conversation

x86: Allow PV guest set X86_CR4_PCE flag

PATCH In review Boris Ostrovsky 2 participants 5 messages Aug 10, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Allow PV guest set X86_CR4_PCE flag

With added PV support for VPMU, guests may legitimately decide to set CR4's PCE flag. We should allow this when VPMU is enabled.

---
 xen/arch/x86/cpu/vpmu.c      | 19 +++++++++++++++++++
 xen/arch/x86/domain.c        | 13 ++++++++++++-
 xen/include/asm-x86/domain.h |  2 ++
 3 files changed, 33 insertions(+), 1 deletion(-)
xen/arch/x86/cpu/vpmu.c +19 -0
index 8af3df1..8cac04e 100644
@@ -81,6 +81,12 @@ static void __init parse_vpmu_param(char *s)
     }
 }
 
+static void update_cr4_mask_pce(bool_t allow)
+{
+    pv_guest_update_cr4_mask(X86_CR4_PCE, 0, allow);
+    pv_guest_update_cr4_mask(X86_CR4_PCE, 1, allow);
+}
+
 void vpmu_lvtpc_update(uint32_t val)
 {
     struct vpmu_struct *vpmu;
@@ -475,6 +481,7 @@ void vpmu_initialise(struct vcpu *v)
             printk(XENLOG_G_WARNING "VPMU: Unknown CPU vendor %d. "
                    "Disabling VPMU\n", vendor);
             opt_vpmu_enabled = 0;
+            update_cr4_mask_pce(0);
             vpmu_mode = XENPMU_MODE_OFF;
         }
         return; /* Don't bother restoring vpmu_count, VPMU is off forever */
@@ -679,7 +686,16 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
         if ( (vpmu_count == 0) ||
              ((vpmu_mode ^ pmu_params.val) ==
               (XENPMU_MODE_SELF | XENPMU_MODE_HV)) )
+        {
+            if ( (vpmu_mode != XENPMU_MODE_OFF) &&
+                 (pmu_params.val == XENPMU_MODE_OFF) )
+                update_cr4_mask_pce(0);
+            else if ( (vpmu_mode == XENPMU_MODE_OFF) &&
+                      (pmu_params.val != XENPMU_MODE_OFF) )
+                update_cr4_mask_pce(1);
+
             vpmu_mode = pmu_params.val;
+        }
         else if ( vpmu_mode != pmu_params.val )
         {
             printk(XENLOG_WARNING
@@ -807,8 +823,11 @@ static int __init vpmu_init(void)
     }
 
     if ( vpmu_mode != XENPMU_MODE_OFF )
+    {
+        update_cr4_mask_pce(1);
         printk(XENLOG_INFO "VPMU: version " __stringify(XENPMU_VER_MAJ) "."
                __stringify(XENPMU_VER_MIN) "\n");
+    }
     else
         opt_vpmu_enabled = 0;
 
xen/arch/x86/domain.c +12 -1
index 045f6ff..71a2bb3 100644
@@ -738,7 +738,18 @@ static int __init init_pv_cr4_masks(void)
 
     return 0;
 }
-__initcall(init_pv_cr4_masks);
+presmp_initcall(init_pv_cr4_masks);
+
+void pv_guest_update_cr4_mask(unsigned long mask, bool_t is_compat,
+                              bool_t allow)
+{
+    unsigned long *curr_mask = is_compat ? &compat_pv_cr4_mask : &pv_cr4_mask;
+
+    if ( !allow )
+        *curr_mask |= mask;
+    else
+        *curr_mask &= ~mask;
+}
 
 unsigned long pv_guest_cr4_fixup(const struct vcpu *v, unsigned long guest_cr4)
 {
xen/include/asm-x86/domain.h +2 -0
index 0fce09e..9758b0a 100644
@@ -561,6 +561,8 @@ void vcpu_show_registers(const struct vcpu *);
 
 /* Clean up CR4 bits that are not under guest control. */
 unsigned long pv_guest_cr4_fixup(const struct vcpu *, unsigned long guest_cr4);
+void pv_guest_update_cr4_mask(unsigned long mask, bool_t is_compat,
+                              bool_t allow);
 
 /* Convert between guest-visible and real CR4 values. */
 #define pv_guest_cr4_to_real_cr4(v)                         \
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 4 lines · Boris Ostrovsky

With added PV support for VPMU, guests may legitimately decide to set CR4's PCE flag. We should allow this when VPMU is enabled.

Why? Even a PV guest using VPMU should know that it doesn't actually control CR4.PCE

All this (appears to) end up doing is putting PCE into the "allow but ignore" mask.

How about this (not even compile tested) which is a rather shorter way of doing the same thing:

xen/arch/x86/domain.c +3 -1
index 045f6ff..834ce0f 100644
@@ -721,10 +721,12 @@ static int __init init_pv_cr4_masks(void)
     unsigned long common_mask = ~X86_CR4_TSD;
 
     /*
-     * All PV guests may attempt to modify TSD, DE and OSXSAVE.
+     * All PV guests may attempt to modify TSD, DE, PCE and OSXSAVE.
      */
     if ( cpu_has_de )
         common_mask &= ~X86_CR4_DE;
+    if ( cpu_has_pce )
+        common_mask &= ~X86_CR4_PCE;
     if ( cpu_has_xsave )
         common_mask &= ~X86_CR4_OSXSAVE;
 

~Andrew

#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Boris Ostrovsky

With added PV support for VPMU, guests may legitimately decide to set CR4's PCE flag. We should allow this when VPMU is enabled.

Why? Even a PV guest using VPMU should know that it doesn't actually control CR4.PCE

All this (appears to) end up doing is putting PCE into the "allow but ignore" mask.

Yes, that's what I wanted to do.

Quoted 3 lines

How about this (not even compile tested) which is a rather shorter way of doing the same thing:

We could do this too but I thought that if we have VPMU off there is no reason to allow this bit to be set (quietly).

(There is no cpu_has_pce, we'd use cpu_has_arch_perfmon on Intel and do this unconditionally on AMD)

-boris

Quoted 20 lines
xen/arch/x86/domain.c +3 -1
index 045f6ff..834ce0f 100644
@@ -721,10 +721,12 @@ static int __init init_pv_cr4_masks(void)
      unsigned long common_mask = ~X86_CR4_TSD;
 
      /*
-     * All PV guests may attempt to modify TSD, DE and OSXSAVE.
+     * All PV guests may attempt to modify TSD, DE, PCE and OSXSAVE.
       */
      if ( cpu_has_de )
          common_mask &= ~X86_CR4_DE;
+    if ( cpu_has_pce )
+        common_mask &= ~X86_CR4_PCE;
      if ( cpu_has_xsave )
          common_mask &= ~X86_CR4_OSXSAVE;
 

~Andrew

#3 lore raw
Quoted 14 lines · Boris Ostrovsky
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Boris Ostrovsky

With added PV support for VPMU, guests may legitimately decide to set CR4's PCE flag. We should allow this when VPMU is enabled.

Why? Even a PV guest using VPMU should know that it doesn't actually control CR4.PCE

All this (appears to) end up doing is putting PCE into the "allow but ignore" mask.

Quoted 8 lines

Yes, that's what I wanted to do.

Quoted 3 lines

How about this (not even compile tested) which is a rather shorter way of doing the same thing:

We could do this too but I thought that if we have VPMU off there is no reason to allow this bit to be set (quietly).

Adding the bit to this mask doesn't allow the guest to play with it. Xen never sets CR4.PCE.

The question is whether warning about the guest attempting to set it is worthwhile or not. We have no rdpmc support in emulate_privileged_op() so any attempt to use it will result in a #UD being injected.

Quoted 3 lines

(There is no cpu_has_pce, we'd use cpu_has_arch_perfmon on Intel and do this unconditionally on AMD)

It could trivially be added. The salient piece of information is whether the hardware would support setting CR4.PCE, not whether any of the interesting features which come with it are present.

~Andrew

#4 lore raw
Quoted 17 lines · Andrew Cooper
Quoted 16 lines · Boris Ostrovsky
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Boris Ostrovsky

With added PV support for VPMU, guests may legitimately decide to set CR4's PCE flag. We should allow this when VPMU is enabled.

Why? Even a PV guest using VPMU should know that it doesn't actually control CR4.PCE

All this (appears to) end up doing is putting PCE into the "allow but ignore" mask.

Yes, that's what I wanted to do.

Quoted 2 lines

How about this (not even compile tested) which is a rather shorter way of doing the same thing:

Quoted 8 lines
Quoted 2 lines

We could do this too but I thought that if we have VPMU off there is no reason to allow this bit to be set (quietly).

Adding the bit to this mask doesn't allow the guest to play with it. Xen never sets CR4.PCE.

The question is whether warning about the guest attempting to set it is worthwhile or not. We have no rdpmc support in emulate_privileged_op() so any attempt to use it will result in a #UD being injected.

That's because (at least on Linux) we turn it into rdmsr.

Actually, let's forget this patch. Given what I just said above, I think it's better to fix this on Linux side and just clear PCE bit in xen_write_cr4().

-boris

Quoted 8 lines
Quoted 3 lines

(There is no cpu_has_pce, we'd use cpu_has_arch_perfmon on Intel and do this unconditionally on AMD)

It could trivially be added. The salient piece of information is whether the hardware would support setting CR4.PCE, not whether any of the interesting features which come with it are present.

~Andrew