x86: correct CPUID output for out of bounds input
authorJan Beulich <jbeulich@suse.com>
Tue, 6 Sep 2016 08:19:18 +0000 (10:19 +0200)
committerJan Beulich <jbeulich@suse.com>
Tue, 6 Sep 2016 08:19:18 +0000 (10:19 +0200)
Another place where we should try to behave sufficiently close to how
real hardware does; see the code comments.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
xen/arch/x86/hvm/hvm.c
xen/arch/x86/traps.c

index c0c270a3f092e06d6f9f63355301984d2852f416..58a40b64165d94e44018461664427c02f8ed1f08 100644 (file)
@@ -3368,6 +3368,27 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
     if ( cpuid_hypervisor_leaves(input, count, eax, ebx, ecx, edx) )
         return;
 
+    if ( input & 0x7fffffff )
+    {
+        /*
+         * Requests outside the supported leaf ranges return zero on AMD
+         * and the highest basic leaf output on Intel. Uniformly follow
+         * the AMD model as the more sane one.
+         */
+        unsigned int limit;
+
+        domain_cpuid(d, (input >> 16) != 0x8000 ? 0 : 0x80000000, 0,
+                     &limit, &dummy, &dummy, &dummy);
+        if ( input > limit )
+        {
+            *eax = 0;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+            return;
+        }
+    }
+
     domain_cpuid(d, input, count, eax, ebx, ecx, edx);
 
     switch ( input )
index 90b074187032861c22203d4326461088fbadf973..b24ca74dcea442e17c5999f90f193a9bfd90a574 100644 (file)
@@ -952,6 +952,29 @@ void pv_cpuid(struct cpu_user_regs *regs)
     if ( cpuid_hypervisor_leaves(leaf, subleaf, &a, &b, &c, &d) )
         goto out;
 
+    if ( leaf & 0x7fffffff )
+    {
+        /*
+         * Requests outside the supported leaf ranges return zero on AMD
+         * and the highest basic leaf output on Intel. Uniformly follow
+         * the AMD model as the more sane one.
+         */
+        unsigned int limit = (leaf >> 16) != 0x8000 ? 0 : 0x80000000, dummy;
+
+        if ( !is_control_domain(currd) && !is_hardware_domain(currd) )
+            domain_cpuid(currd, limit, 0, &limit, &dummy, &dummy, &dummy);
+        else
+            limit = cpuid_eax(limit);
+        if ( leaf > limit )
+        {
+            regs->eax = 0;
+            regs->ebx = 0;
+            regs->ecx = 0;
+            regs->edx = 0;
+            return;
+        }
+    }
+
     if ( !is_control_domain(currd) && !is_hardware_domain(currd) )
         domain_cpuid(currd, leaf, subleaf, &a, &b, &c, &d);
     else