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
@@ -3105,7 +3105,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
@@ -769,24 +769,18 @@ 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;
     struct vcpu *curr = current;
 
-    a = regs->eax;
-    b = regs->ebx;
-    c = regs->ecx;
-    d = regs->edx;
-
     if ( !is_control_domain(curr->domain) && !is_hardware_domain(curr->domain) )
     {
-        unsigned int cpuid_leaf = a, sub_leaf = c;
-
-        if ( !cpuid_hypervisor_leaves(a, c, &a, &b, &c, &d) )
-            domain_cpuid(curr->domain, a, c, &a, &b, &c, &d);
+        if ( !cpuid_hypervisor_leaves(leaf, sub_leaf, &a, &b, &c, &d) )
+            domain_cpuid(curr->domain, leaf, sub_leaf, &a, &b, &c, &d);
 
-        switch ( cpuid_leaf )
+        switch ( leaf )
         {
             unsigned int _eax, _ebx, _ecx, _edx;
 
@@ -801,17 +795,18 @@ void pv_cpuid(struct cpu_user_regs *regs
                 {
                     if ( !(curr->arch.xcr0 & (1ULL << sub_leaf)) )
                         continue;
-                    domain_cpuid(curr->domain, cpuid_leaf, sub_leaf,
+                    domain_cpuid(curr->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 ( !boot_cpu_has(X86_FEATURE_SC_MSR_PV) )
                     d &= ~(cpufeat_mask(X86_FEATURE_IBRSB) |
@@ -844,9 +839,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. */
         if ( !cpu_has_apic )
@@ -861,7 +856,7 @@ void pv_cpuid(struct cpu_user_regs *regs
         }
     }
 
-    switch ( regs->_eax )
+    switch ( leaf )
     {
     case 0x00000001:
         /* Modify Feature Information. */
@@ -896,7 +891,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)  |
@@ -929,7 +924,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 |
@@ -984,15 +979,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)
@@ -1042,7 +1041,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);
 
@@ -2856,7 +2855,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
@@ -550,7 +550,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__ */
 
