From: Andrew Cooper <andrew.cooper3@citrix.com>
Subject: x86/pv: Improve pv_cpuid()'s API

pv_cpuid()'s API is awkward to use.  There are already two callers jumping
through hoops to use it, and a third is on its way.

Change the API to take each parameter individually (like its counterpart,
hvm_cpuid(), already does), and introduce a new pv_cpuid_regs() wrapper
implementing the old API.

No functional change.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>

--- a/xen/arch/x86/hvm/vmx/vmx.c
+++ b/xen/arch/x86/hvm/vmx/vmx.c
@@ -2995,7 +2995,7 @@ void vmx_vmexit_handler(struct cpu_user_
         break;
     }
     case EXIT_REASON_CPUID:
-        is_pvh_vcpu(v) ? pv_cpuid(regs) : vmx_do_cpuid(regs);
+        is_pvh_vcpu(v) ? pv_cpuid_regs(regs) : vmx_do_cpuid(regs);
         update_guest_eip(); /* Safe: CPUID */
         break;
     case EXIT_REASON_HLT:
--- a/xen/arch/x86/traps.c
+++ b/xen/arch/x86/traps.c
@@ -741,23 +741,17 @@ static void _domain_cpuid(struct domain
         cpuid_count(leaf, subleaf, eax, ebx, ecx, edx);
 }
 
-void pv_cpuid(struct cpu_user_regs *regs)
+void pv_cpuid(uint32_t leaf, uint32_t sub_leaf,
+              uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
 {
     uint32_t a, b, c, d;
 
-    a = regs->eax;
-    b = regs->ebx;
-    c = regs->ecx;
-    d = regs->edx;
-
     if ( current->domain->domain_id != 0 )
     {
-        unsigned int cpuid_leaf = a, sub_leaf = c;
-
-        if ( !cpuid_hypervisor_leaves(a, c, &a, &b, &c, &d) )
-            domain_cpuid(current->domain, a, c, &a, &b, &c, &d);
+        if ( !cpuid_hypervisor_leaves(leaf, sub_leaf, &a, &b, &c, &d) )
+            domain_cpuid(current->domain, leaf, sub_leaf, &a, &b, &c, &d);
 
-        switch ( cpuid_leaf )
+        switch ( leaf )
         {
             unsigned int _eax, _ebx, _ecx, _edx;
 
@@ -772,17 +766,18 @@ void pv_cpuid(struct cpu_user_regs *regs
                 {
                     if ( !(current->arch.xcr0 & (1ULL << sub_leaf)) )
                         continue;
-                    domain_cpuid(current->domain, cpuid_leaf, sub_leaf,
+                    domain_cpuid(current->domain, leaf, sub_leaf,
                                  &_eax, &_ebx, &_ecx, &_edx);
                     if ( (_eax + _ebx) > b )
                         b = _eax + _ebx;
                 }
+                sub_leaf = 0;
             }
             goto xstate;
         }
 
         case 0x00000007:
-            if ( regs->_ecx == 0 )
+            if ( sub_leaf == 0 )
             {
                 if ( !opt_msr_sc_pv )
                     d &= ~(cpufeat_mask(X86_FEATURE_IBRSB) |
@@ -815,9 +810,9 @@ void pv_cpuid(struct cpu_user_regs *regs
     asm ( 
         "cpuid"
         : "=a" (a), "=b" (b), "=c" (c), "=d" (d)
-        : "0" (a), "1" (b), "2" (c), "3" (d) );
+        : "0" (leaf), "2" (sub_leaf) );
 
-    if ( (regs->eax & 0x7fffffff) == 0x00000001 )
+    if ( (leaf & 0x7fffffff) == 0x00000001 )
     {
         /* Modify Feature Information. */
         __clear_bit(X86_FEATURE_VME, &d);
@@ -828,7 +823,7 @@ void pv_cpuid(struct cpu_user_regs *regs
         __clear_bit(X86_FEATURE_PSE36, &d);
     }
 
-    switch ( regs->_eax )
+    switch ( leaf )
     {
     case 0x00000001:
         /* Modify Feature Information. */
@@ -863,7 +858,7 @@ void pv_cpuid(struct cpu_user_regs *regs
         break;
 
     case 0x00000007:
-        if ( regs->_ecx == 0 )
+        if ( sub_leaf == 0 )
         {
             b &= (cpufeat_mask(X86_FEATURE_BMI1) |
                   cpufeat_mask(X86_FEATURE_HLE)  |
@@ -894,7 +889,7 @@ void pv_cpuid(struct cpu_user_regs *regs
     xstate:
         if ( !cpu_has_xsave )
             goto unsupported;
-        if ( regs->_ecx == 1 )
+        if ( sub_leaf == 1 )
         {
             a &= XSTATE_FEATURE_XSAVEOPT |
                  XSTATE_FEATURE_XSAVEC |
@@ -949,15 +944,19 @@ void pv_cpuid(struct cpu_user_regs *regs
         break;
 
     default:
-        (void)cpuid_hypervisor_leaves(regs->eax, 0, &a, &b, &c, &d);
+        (void)cpuid_hypervisor_leaves(leaf, sub_leaf, &a, &b, &c, &d);
         break;
     }
 
  out:
-    regs->eax = a;
-    regs->ebx = b;
-    regs->ecx = c;
-    regs->edx = d;
+    if ( eax )
+        *eax = a;
+    if ( ebx )
+        *ebx = b;
+    if ( ecx )
+        *ecx = c;
+    if ( edx )
+        *edx = d;
 }
 
 static int emulate_invalid_rdtscp(struct cpu_user_regs *regs)
@@ -1007,7 +1006,7 @@ static int emulate_forced_invalid_op(str
         return 0;
     eip += sizeof(instr);
 
-    pv_cpuid(regs);
+    pv_cpuid_regs(regs);
 
     instruction_done(regs, eip, 0);
 
@@ -2763,7 +2762,7 @@ static int emulate_privileged_op(struct
         break;
 
     case 0xa2: /* CPUID */
-        pv_cpuid(regs);
+        pv_cpuid_regs(regs);
         break;
 
     default:
--- a/xen/include/asm-x86/processor.h
+++ b/xen/include/asm-x86/processor.h
@@ -600,7 +600,14 @@ int early_microcode_init(void);
 int microcode_init_intel(void);
 int microcode_init_amd(void);
 
-void pv_cpuid(struct cpu_user_regs *regs);
+void pv_cpuid(uint32_t leaf, uint32_t subleaf,
+              uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
+
+static inline void pv_cpuid_regs(struct cpu_user_regs *regs)
+{
+    pv_cpuid(regs->_eax, regs->_ecx,
+             &regs->_eax, &regs->_ebx, &regs->_ecx, &regs->_edx);
+}
 
 #endif /* !__ASSEMBLY__ */
 
