[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v4 1/4] xen/libxc: Allow changes to hypervisor CPUID leaf from config file



Currently only "real" cpuid leaves can be overwritten by users via
'cpuid' option in the configuration file. This patch provides ability to
do the same for hypervisor leaves (but for now only 0x40000000 is allowed).

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
---
 tools/libxc/xc_cpuid_x86.c   |   71 ++++++++++++++++++++++++++++++++++++++++--
 xen/arch/x86/domain.c        |   19 +++++++++--
 xen/arch/x86/traps.c         |    3 ++
 xen/include/asm-x86/domain.h |    7 +++++
 4 files changed, 95 insertions(+), 5 deletions(-)

diff --git a/tools/libxc/xc_cpuid_x86.c b/tools/libxc/xc_cpuid_x86.c
index bbbf9b8..2eb901e 100644
--- a/tools/libxc/xc_cpuid_x86.c
+++ b/tools/libxc/xc_cpuid_x86.c
@@ -33,6 +33,8 @@
 #define DEF_MAX_INTELEXT  0x80000008u
 #define DEF_MAX_AMDEXT    0x8000001cu
 
+#define IS_HYPERVISOR_LEAF(idx) (((idx) & 0xffff0000) == 0x40000000)
+
 static int hypervisor_is_64bit(xc_interface *xch)
 {
     xen_capabilities_info_t xen_caps = "";
@@ -43,22 +45,31 @@ static int hypervisor_is_64bit(xc_interface *xch)
 static void cpuid(const unsigned int *input, unsigned int *regs)
 {
     unsigned int count = (input[1] == XEN_CPUID_INPUT_UNUSED) ? 0 : input[1];
+    uint8_t is_hyp = IS_HYPERVISOR_LEAF(input[0]);
 #ifdef __i386__
     /* Use the stack to avoid reg constraint failures with some gcc flags */
     asm (
         "push %%ebx; push %%edx\n\t"
+        "testb $0xff,%5\n\t"
+        "jz .Lcpuid%=\n\t"
+        XEN_EMULATE_PREFIX
+        ".Lcpuid%=:\n\t"
         "cpuid\n\t"
         "mov %%ebx,4(%4)\n\t"
         "mov %%edx,12(%4)\n\t"
         "pop %%edx; pop %%ebx\n\t"
         : "=a" (regs[0]), "=c" (regs[2])
-        : "0" (input[0]), "1" (count), "S" (regs)
+        : "0" (input[0]), "1" (count), "S" (regs), "q" (is_hyp)
         : "memory" );
 #else
     asm (
+        "testb $0xff,%6\n\t"
+        "jz .Lcpuid%=\n\t"
+        XEN_EMULATE_PREFIX
+        ".Lcpuid%=:\n\t"
         "cpuid"
         : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
-        : "0" (input[0]), "2" (count) );
+        : "0" (input[0]), "2" (count), "q" (is_hyp) );
 #endif
 }
 
@@ -555,6 +566,9 @@ static int xc_cpuid_policy(
 {
     xc_dominfo_t        info;
 
+    if ( IS_HYPERVISOR_LEAF(input[0]) )
+        return 0;
+
     if ( xc_domain_getinfo(xch, domid, 1, &info) == 0 )
         return -EINVAL;
 
@@ -726,6 +740,57 @@ int xc_cpuid_check(
     return rc;
 }
 
+static void xc_cpuid_revert(unsigned int *reg, unsigned int polreg,
+    unsigned int start, unsigned int end, char *config)
+{
+    unsigned int i;
+
+    for (i = start; i <= end; i++)
+    {
+        if ( polreg & (1U << i) )
+        {
+            set_bit(i, *reg);
+            config[32 - i] = '1';
+        }
+        else
+        {
+            clear_bit(i, *reg);
+            config[32 - i] = '0';
+        }
+    }
+}
+
+static void xc_cpuid_constrain(const unsigned int *input, unsigned int *regs,
+    unsigned int *polregs, char **config_transformed)
+{
+    unsigned int i;
+
+    if ( IS_HYPERVISOR_LEAF(input[0]) )
+    {
+        switch ( input[0] )
+        {
+            case 0x40000000:
+                if ( (regs[0] & 0xffffff00) != (polregs[0] & 0xffffff00) )
+                        xc_cpuid_revert(&regs[0], polregs[0], 8, 31,
+                                        config_transformed[0]);
+                if ( (regs[0] & 0xff) < 2 )
+                        xc_cpuid_revert(&regs[0], polregs[0], 0, 7,
+                                        config_transformed[0]);
+                for (i = 1; i < 4; i++)
+                    if ( regs[i] != polregs[i] )
+                        xc_cpuid_revert(&regs[i], polregs[i], 0, 31,
+                                        config_transformed[i]);
+                break;
+
+            default:
+                for (i = 0; i < 4; i++)
+                    xc_cpuid_revert(&regs[i], polregs[i], 0, 31,
+                                    config_transformed[i]);
+                break;
+        }
+    }
+}
+
 /*
  * Configure a single input with the informatiom from config.
  *
@@ -801,6 +866,8 @@ int xc_cpuid_set(
         }
     }
 
+    xc_cpuid_constrain(input, regs, polregs, config_transformed);
+
     rc = xc_cpuid_do_domctl(xch, domid, input, regs);
     if ( rc == 0 )
         return 0;
diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index c42a079..98e2b5f 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -1997,7 +1997,7 @@ void arch_dump_vcpu_info(struct vcpu *v)
         vpmu_dump(v);
 }
 
-void domain_cpuid(
+bool_t domain_cpuid_exists(
     struct domain *d,
     unsigned int  input,
     unsigned int  sub_input,
@@ -2030,11 +2030,24 @@ void domain_cpuid(
                  !d->disable_migrate && !d->arch.vtsc )
                 *edx &= ~(1u<<8); /* TSC Invariant */
 
-            return;
+            return 1;
         }
     }
 
-    *eax = *ebx = *ecx = *edx = 0;
+    return 0;
+}
+
+void domain_cpuid(
+    struct domain *d,
+    unsigned int  input,
+    unsigned int  sub_input,
+    unsigned int  *eax,
+    unsigned int  *ebx,
+    unsigned int  *ecx,
+    unsigned int  *edx)
+{
+    if ( !domain_cpuid_exists(d, input, sub_input, eax, ebx, ecx, edx) )
+        *eax = *ebx = *ecx = *edx = 0;
 }
 
 void vcpu_kick(struct vcpu *v)
diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c
index c462317..e1f39d9 100644
--- a/xen/arch/x86/traps.c
+++ b/xen/arch/x86/traps.c
@@ -690,6 +690,9 @@ int cpuid_hypervisor_leaves( uint32_t idx, uint32_t sub_idx,
     if ( idx > limit ) 
         return 0;
 
+    if ( domain_cpuid_exists(d, base + idx, sub_idx, eax, ebx, ecx, edx) )
+        return 1;
+
     switch ( idx )
     {
     case 0:
diff --git a/xen/include/asm-x86/domain.h b/xen/include/asm-x86/domain.h
index 49f7c0c..5fd47de 100644
--- a/xen/include/asm-x86/domain.h
+++ b/xen/include/asm-x86/domain.h
@@ -471,6 +471,13 @@ unsigned long pv_guest_cr4_fixup(const struct vcpu *, 
unsigned long guest_cr4);
     ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD |      \
              X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE))
 
+bool_t domain_cpuid_exists(struct domain *d,
+                           unsigned int  input,
+                           unsigned int  sub_input,
+                           unsigned int  *eax,
+                           unsigned int  *ebx,
+                           unsigned int  *ecx,
+                           unsigned int  *edx);
 void domain_cpuid(struct domain *d,
                   unsigned int  input,
                   unsigned int  sub_input,
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.