Skip to contentSkip to conversation

FPU LWP: patch description

PATCH In review Huang2, Wei 2 participants 11 messages May 7, 2011 – May 9, 2011 9 messages not in the archive

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 FPU LWP: patch description

In reply to a message not in the archive

This patch set supports AMD lightweight profiling for SVM guests. Because LWP isn't tracked by CR0.TS bit, we clean up the FPU code to handle lazy and unlazy FPU states differently. Lazy FPU state (such as SSE, YMM) is handled when #NM is triggered. Unlazy state, such as LWP, is saved and restored on each vcpu context switch.

Per Keir's comments, I moved extended state related code into xstate.c and xstate.h. The FPU related code in i387.c was also cleaned up and has consistent names now. The comments from Jan Beulich were also taken. I added a new variable, nonlazy_xstate_used, to control whether save/restore nonlazy state.

====== i387.c ======
* void vcpu_restore_fpu_eager(struct vcpu *v);
* void vcpu_restore_fpu_lazy(struct vcpu *v);
* void vcpu_save_fpu(struct vcpu *v);
* int vcpu_init_fpu(struct vcpu *v);
* void vcpu_destroy_fpu(struct vcpu *v);

====== xstate.c ======
* void set_xcr0(u64 xfeatures);
* uint64_t get_xcr0(void);
* void xsave(struct vcpu *v, uint64_t mask);
* void xrstor(struct vcpu *v, uint64_t mask);
* bool_t xsave_enabled(const struct vcpu *v);
* void xstate_free_save_area(struct vcpu *v);
* int xstate_alloc_save_area(struct vcpu *v);
* void xstate_init(void);

This code has been tested on real hardware. Please comment.

-Wei

 b/xen/arch/x86/xstate.c            |  188 +++++++++++++++
 b/xen/include/asm-x86/xstate.h     |   72 +++++
 tools/libxc/xc_cpuid_x86.c         |    6
 xen/arch/x86/Makefile              |    1
 xen/arch/x86/acpi/suspend.c        |    2
 xen/arch/x86/cpu/common.c          |    4
 xen/arch/x86/domain.c              |   29 --
 xen/arch/x86/domctl.c              |    2
 xen/arch/x86/hvm/hvm.c             |    3
 xen/arch/x86/hvm/svm/svm.c         |   75 ++++++
 xen/arch/x86/hvm/svm/vmcb.c        |    5
 xen/arch/x86/hvm/vmx/vmcs.c        |    1
 xen/arch/x86/hvm/vmx/vmx.c         |    2
 xen/arch/x86/i387.c                |  450 ++++++++++++++++---------------------
 xen/arch/x86/traps.c               |    3
 xen/include/asm-x86/cpufeature.h   |    2
 xen/include/asm-x86/domain.h       |    5
 xen/include/asm-x86/hvm/svm/vmcb.h |    3
 xen/include/asm-x86/i387.h         |   71 -----
 xen/include/asm-x86/msr-index.h    |    4
 20 files changed, 573 insertions(+), 355 deletions(-)
#4 lore raw

PATCH 1/1 FPU: extract extended related code into xstate.h and xstate.c

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304447651 18000
# Node ID 439311a108d71aba1580177543c7d4320b012721 # Parent fac1411505d6e56cd6745d29682bcc02fd043649
FPU: extract extended related code into xstate.h and xstate.c

Current extended code is mixing with FPU code in i387.c. As part of FPU code cleanup, this patch moves all extended state code into independent files. Not much semantic are changed and most function names are kept untouched, except for xsave() and xsaveopt(). These two functions are combined into a single function.

xen/arch/x86/Makefile +1 -0
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/Makefile
@@ -56,6 +56,7 @@
 obj-y += crash.o
 obj-y += tboot.o
 obj-y += hpet.o
+obj-y += xstate.o
 
 obj-$(crash_debug) += gdbstub.o
 
xen/arch/x86/cpu/common.c +2 -2
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/cpu/common.c
@@ -5,7 +5,7 @@
 #include <xen/smp.h>
 #include <asm/current.h>
 #include <asm/processor.h>
-#include <asm/i387.h>
+#include <asm/xstate.h>
 #include <asm/msr.h>
 #include <asm/io.h>
 #include <asm/mpspec.h>
@@ -354,7 +354,7 @@
 		clear_bit(X86_FEATURE_XSAVE, boot_cpu_data.x86_capability);
 
 	if ( cpu_has_xsave )
-		xsave_init();
+		xstate_init();
 
 	/*
 	 * The vendor-specific functions might have changed features.  Now
xen/arch/x86/domain.c +4 -3
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/domain.c
@@ -42,6 +42,7 @@
 #include <asm/processor.h>
 #include <asm/desc.h>
 #include <asm/i387.h>
+#include <asm/xstate.h>
 #include <asm/mpspec.h>
 #include <asm/ldt.h>
 #include <asm/hypercall.h>
@@ -419,7 +420,7 @@
 
     v->arch.perdomain_ptes = perdomain_ptes(d, v);
 
-    if ( (rc = xsave_alloc_save_area(v)) != 0 )
+    if ( (rc = xstate_alloc_save_area(v)) != 0 )
         return rc;
     if ( v->arch.xsave_area )
         v->arch.fpu_ctxt = &v->arch.xsave_area->fpu_sse;
@@ -485,7 +486,7 @@
     if ( rc )
     {
         if ( v->arch.xsave_area )
-            xsave_free_save_area(v);
+            xstate_free_save_area(v);
         else
             xfree(v->arch.fpu_ctxt);
         if ( !is_hvm_domain(d) && standalone_trap_ctxt(v) )
@@ -501,7 +502,7 @@
         release_compat_l4(v);
 
     if ( v->arch.xsave_area )
-        xsave_free_save_area(v);
+        xstate_free_save_area(v);
     else
         xfree(v->arch.fpu_ctxt);
 
xen/arch/x86/domctl.c +1 -1
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/domctl.c
@@ -33,7 +33,7 @@
 #include <asm/mem_event.h>
 #include <public/mem_event.h>
 #include <asm/mem_sharing.h>
-#include <asm/i387.h>
+#include <asm/xstate.h>
 
 #ifdef XEN_KDB_CONFIG
 #include "../kdb/include/kdbdefs.h"
xen/arch/x86/hvm/hvm.c +2 -1
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/hvm/hvm.c
@@ -46,6 +46,7 @@
 #include <asm/types.h>
 #include <asm/msr.h>
 #include <asm/i387.h>
+#include <asm/xstate.h>
 #include <asm/traps.h>
 #include <asm/mc146818rtc.h>
 #include <asm/spinlock.h>
@@ -2427,7 +2428,7 @@
         if ( count == 0 && v->arch.xcr0 ) 
         {
             /* reset EBX to default value first */
-            *ebx = XSAVE_AREA_MIN_SIZE; 
+            *ebx = XSTATE_AREA_MIN_SIZE; 
             for ( sub_leaf = 2; sub_leaf < 64; sub_leaf++ )
             {
                 if ( !(v->arch.xcr0 & (1ULL << sub_leaf)) )
xen/arch/x86/hvm/vmx/vmcs.c +1 -0
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/hvm/vmx/vmcs.c
@@ -26,6 +26,7 @@
 #include <asm/cpufeature.h>
 #include <asm/processor.h>
 #include <asm/msr.h>
+#include <asm/xstate.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/io.h>
 #include <asm/hvm/support.h>
xen/arch/x86/i387.c +2 -145

166 lines not on this page · raw message

xen/arch/x86/traps.c +1 -0
diff -r fac1411505d6 -r 439311a108d7 xen/arch/x86/traps.c
@@ -58,6 +58,7 @@
 #include <asm/flushtlb.h>
 #include <asm/uaccess.h>
 #include <asm/i387.h>
+#include <asm/xstate.h>
 #include <asm/debugger.h>
 #include <asm/msr.h>
 #include <asm/shared.h>
xen/arch/x86/xstate.c new file +183 -0

183 lines not on this page · raw message

xen/include/asm-x86/i387.h +2 -66

73 lines not on this page · raw message

xen/include/asm-x86/xstate.h new file +68 -0

68 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#6 lore raw

PATCH 1/1 FPU: create FPU init and destroy functions

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304447821 18000
# Node ID eb6f1c5d0c7e688df083708348892ec5ba45c41a # Parent 439311a108d71aba1580177543c7d4320b012721
FPU: create FPU init and destroy functions

Extract FPU initialization and destroy code into two functions. These functions handle memory allocation/deallocation for FPU context.

xen/arch/x86/domain.c +4 -21
diff -r 439311a108d7 -r eb6f1c5d0c7e xen/arch/x86/domain.c
@@ -420,20 +420,8 @@
 
     v->arch.perdomain_ptes = perdomain_ptes(d, v);
 
-    if ( (rc = xstate_alloc_save_area(v)) != 0 )
+    if ( (rc = vcpu_init_fpu(v)) != 0 )
         return rc;
-    if ( v->arch.xsave_area )
-        v->arch.fpu_ctxt = &v->arch.xsave_area->fpu_sse;
-    else if ( !is_idle_domain(d) )
-    {
-        v->arch.fpu_ctxt = _xmalloc(sizeof(v->arch.xsave_area->fpu_sse), 16);
-        if ( !v->arch.fpu_ctxt )
-        {
-            rc = -ENOMEM;
-            goto done;
-        }
-        memset(v->arch.fpu_ctxt, 0, sizeof(v->arch.xsave_area->fpu_sse));
-    }
 
     if ( is_hvm_domain(d) )
     {
@@ -485,10 +473,8 @@
  done:
     if ( rc )
     {
-        if ( v->arch.xsave_area )
-            xstate_free_save_area(v);
-        else
-            xfree(v->arch.fpu_ctxt);
+        vcpu_destroy_fpu(v);
+
         if ( !is_hvm_domain(d) && standalone_trap_ctxt(v) )
             free_xenheap_page(v->arch.pv_vcpu.trap_ctxt);
     }
@@ -501,10 +487,7 @@
     if ( is_pv_32on64_vcpu(v) )
         release_compat_l4(v);
 
-    if ( v->arch.xsave_area )
-        xstate_free_save_area(v);
-    else
-        xfree(v->arch.fpu_ctxt);
+    vcpu_destroy_fpu(v);
 
     if ( is_hvm_vcpu(v) )
         hvm_vcpu_destroy(v);
xen/arch/x86/i387.c +41 -0

47 lines not on this page · raw message

xen/include/asm-x86/i387.h +2 -0
diff -r 439311a108d7 -r eb6f1c5d0c7e xen/include/asm-x86/i387.h
@@ -17,4 +17,6 @@
 void setup_fpu(struct vcpu *v);
 void save_init_fpu(struct vcpu *v);
 
+int vcpu_init_fpu(struct vcpu *v);
+void vcpu_destroy_fpu(struct vcpu *v);
 #endif /* __ASM_I386_I387_H */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#8 lore raw

PATCH 1/1 FPU: clean up FPU context save function

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304448193 18000
# Node ID 5560826d130f143c8aecbf4abbcc59cef587f29c # Parent eb6f1c5d0c7e688df083708348892ec5ba45c41a
FPU: clean up FPU context save function

This patch cleans up context save function. It renames the save function to vcpu_save_fpu() because existing function name is confusion. It also extracts FPU context save code (fsave, fxsave, xsave) into seperate functions. vcpu_save_fpu() will call corresponding sub-function depending on CPU's capability.

xen/arch/x86/acpi/suspend.c +1 -1
diff -r eb6f1c5d0c7e -r 5560826d130f xen/arch/x86/acpi/suspend.c
@@ -24,7 +24,7 @@
 
 void save_rest_processor_state(void)
 {
-    save_init_fpu(current);
+    vcpu_save_fpu(current);
 
 #if defined(CONFIG_X86_64)
     asm volatile (
xen/arch/x86/domain.c +1 -1
diff -r eb6f1c5d0c7e -r 5560826d130f xen/arch/x86/domain.c
@@ -1560,7 +1560,7 @@
     if ( !is_idle_vcpu(p) )
     {
         memcpy(&p->arch.user_regs, stack_regs, CTXT_SWITCH_STACK_BYTES);
-        save_init_fpu(p);
+        vcpu_save_fpu(p);
         p->arch.ctxt_switch_from(p);
     }
 
xen/arch/x86/i387.c +88 -72

174 lines not on this page · raw message

xen/include/asm-x86/i387.h +1 -1
diff -r eb6f1c5d0c7e -r 5560826d130f xen/include/asm-x86/i387.h
@@ -15,7 +15,7 @@
 #include <xen/percpu.h>
 
 void setup_fpu(struct vcpu *v);
-void save_init_fpu(struct vcpu *v);
+void vcpu_save_fpu(struct vcpu *v);
 
 int vcpu_init_fpu(struct vcpu *v);
 void vcpu_destroy_fpu(struct vcpu *v);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#10 lore raw

PATCH 1/1 FPU: clean up FPU context restore function

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304448326 18000
# Node ID 5badff7cda6b4af71991d8150803ac2f7c4a27f5 # Parent 5560826d130f143c8aecbf4abbcc59cef587f29c
FPU: clean up FPU context restore function

This patch cleans up context restore function. It renames the function name to vcpu_restore_fpu(). It also extracts FPU restore code (frstor, fxrstor, xrstor) out into seperate functions. vcpu_restor_fpu() will dispatch to these functions depending on CPU's capability.

xen/arch/x86/hvm/svm/svm.c +1 -1
diff -r 5560826d130f -r 5badff7cda6b xen/arch/x86/hvm/svm/svm.c
@@ -348,7 +348,7 @@
 {
     struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
 
-    setup_fpu(v);
+    vcpu_restore_fpu(v);
     vmcb_set_exception_intercepts(
         vmcb, vmcb_get_exception_intercepts(vmcb) & ~(1U << TRAP_no_device));
 }
xen/arch/x86/hvm/vmx/vmx.c +1 -1
diff -r 5560826d130f -r 5badff7cda6b xen/arch/x86/hvm/vmx/vmx.c
@@ -612,7 +612,7 @@
 
 static void vmx_fpu_enter(struct vcpu *v)
 {
-    setup_fpu(v);
+    vcpu_restore_fpu(v);
     v->arch.hvm_vmx.exception_bitmap &= ~(1u << TRAP_no_device);
     vmx_update_exception_bitmap(v);
     v->arch.hvm_vmx.host_cr0 &= ~X86_CR0_TS;
xen/arch/x86/i387.c +85 -74

188 lines not on this page · raw message

xen/arch/x86/traps.c +1 -1
diff -r 5560826d130f -r 5badff7cda6b xen/arch/x86/traps.c
@@ -3198,7 +3198,7 @@
 
     BUG_ON(!guest_mode(regs));
 
-    setup_fpu(curr);
+    vcpu_restore_fpu(curr);
 
     if ( curr->arch.pv_vcpu.ctrlreg[0] & X86_CR0_TS )
     {
xen/include/asm-x86/i387.h +1 -1
diff -r 5560826d130f -r 5badff7cda6b xen/include/asm-x86/i387.h
@@ -14,7 +14,7 @@
 #include <xen/types.h>
 #include <xen/percpu.h>
 
-void setup_fpu(struct vcpu *v);
+void vcpu_restore_fpu(struct vcpu *v);
 void vcpu_save_fpu(struct vcpu *v);
 
 int vcpu_init_fpu(struct vcpu *v);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#12 lore raw

PATCH 1/1 FPU: add mask parameter to xsave and xrstor

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304696422 18000
# Node ID ab7d2191421fbf95313e1109de3ced5e2a094424 # Parent 5badff7cda6b4af71991d8150803ac2f7c4a27f5
FPU: add mask parameter to xsave and xrstor

Xen currently sets mask bits of xsave() and xrstor() to all 1's. This patch adds a mask option to xsave() and xrstor().

xen/arch/x86/i387.c +6 -6
diff -r 5badff7cda6b -r ab7d2191421f xen/arch/x86/i387.c
@@ -35,14 +35,14 @@
 /*     FPU Restore Functions   */
 /*******************************/
 /* Restore x87 extended state */
-static inline void fpu_xrstor(struct vcpu *v)
+static inline void fpu_xrstor(struct vcpu *v, uint64_t mask)
 {
     /*
      * XCR0 normally represents what guest OS set. In case of Xen itself, 
      * we set all supported feature mask before doing save/restore.
      */
     set_xcr0(v->arch.xcr0_accum);
-    xrstor(v);
+    xrstor(v, mask);
     set_xcr0(v->arch.xcr0);
 }
 
@@ -98,13 +98,13 @@
 /*      FPU Save Functions     */
 /*******************************/
 /* Save x87 extended state */
-static inline void fpu_xsave(struct vcpu *v)
+static inline void fpu_xsave(struct vcpu *v, uint64_t mask)
 {
     /* XCR0 normally represents what guest OS set. In case of Xen itself,
      * we set all accumulated feature mask before doing save/restore.
      */
     set_xcr0(v->arch.xcr0_accum);
-    xsave(v);
+    xsave(v, mask);
     set_xcr0(v->arch.xcr0);    
 }
 
@@ -174,7 +174,7 @@
         return;
 
     if ( xsave_enabled(v) )
-        fpu_xrstor(v);
+        fpu_xrstor(v, XSTATE_ALL);
     else if ( v->fpu_initialised )
     {
         if ( cpu_has_fxsr )
@@ -204,7 +204,7 @@
     clts();
 
     if ( xsave_enabled(v) )
-        fpu_xsave(v);
+        fpu_xsave(v, XSTATE_ALL);
     else if ( cpu_has_fxsr )
         fpu_fxsave(v);
     else
xen/arch/x86/xstate.c +10 -5
diff -r 5badff7cda6b -r ab7d2191421f xen/arch/x86/xstate.c
@@ -51,32 +51,37 @@
     return this_cpu(xcr0);
 }
 
-void xsave(struct vcpu *v)
+void xsave(struct vcpu *v, uint64_t mask)
 {
     struct xsave_struct *ptr = v->arch.xsave_area;
+    uint32_t hmask = mask >> 32;
+    uint32_t lmask = mask;
 
     if ( cpu_has_xsaveopt )
         asm volatile (
             ".byte " REX_PREFIX "0x0f,0xae,0x37"
             :
-            : "a" (-1), "d" (-1), "D"(ptr)
+            : "a" (lmask), "d" (hmask), "D"(ptr)
             : "memory" );
     else
         asm volatile (
             ".byte " REX_PREFIX "0x0f,0xae,0x27"
             :
-            : "a" (-1), "d" (-1), "D"(ptr)
+            : "a" (lmask), "d" (hmask), "D"(ptr)
             : "memory" );
 }
 
-void xrstor(struct vcpu *v)
+void xrstor(struct vcpu *v, uint64_t mask)
 {
+    uint32_t hmask = mask >> 32;
+    uint32_t lmask = mask;
+
     struct xsave_struct *ptr = v->arch.xsave_area;
 
     asm volatile (
         ".byte " REX_PREFIX "0x0f,0xae,0x2f"
         :
-        : "m" (*ptr), "a" (-1), "d" (-1), "D"(ptr) );
+        : "m" (*ptr), "a" (lmask), "d" (hmask), "D"(ptr) );
 }
 
 bool_t xsave_enabled(const struct vcpu *v)
xen/include/asm-x86/xstate.h +6 -2
diff -r 5badff7cda6b -r ab7d2191421f xen/include/asm-x86/xstate.h
@@ -26,6 +26,10 @@
 #define XSTATE_LWP     (1ULL << 62) /* AMD lightweight profiling */
 #define XSTATE_FP_SSE  (XSTATE_FP | XSTATE_SSE)
 #define XCNTXT_MASK    (XSTATE_FP | XSTATE_SSE | XSTATE_YMM | XSTATE_LWP)
+
+#define XSTATE_ALL     (~0)
+#define XSTATE_NONLAZY (XSTATE_LWP)
+#define XSTATE_LAZY    (XSTATE_ALL & ~XSTATE_NONLAZY)
 
 #ifdef CONFIG_X86_64
 #define REX_PREFIX     "0x48, "
@@ -56,8 +60,8 @@
 /* extended state operations */
 void set_xcr0(u64 xfeatures);
 uint64_t get_xcr0(void);
-void xsave(struct vcpu *v);
-void xrstor(struct vcpu *v);
+void xsave(struct vcpu *v, uint64_t mask);
+void xrstor(struct vcpu *v, uint64_t mask);
 bool_t xsave_enabled(const struct vcpu *v);
 
 /* extended state init and cleanup functions */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#14 lore raw

PATCH 1/1 FPU: create lazy and non-lazy FPU restore functions

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304697215 18000
# Node ID e58b1d06aabe5c0aa26f2bd29b607a1c337d6c80 # Parent ab7d2191421fbf95313e1109de3ced5e2a094424
FPU: create lazy and non-lazy FPU restore functions

Currently Xen relies on #NM (via CR0.TS) to trigger FPU context restore. But not all FPU state is tracked by TS bit. This function creates two FPU restore functions: vcpu_restore_fpu_lazy() and vcpu_restore_fpu_eager(). vcpu_restore_fpu_lazy() is still used when #NM is triggered. vcpu_restore_fpu_eager(), as a comparision, is called for vcpu which is being scheduled in on every context switch. To minimize restore overhead, it creates a flag, nonlazy_xstate_used, to control non-lazy restore.

xen/arch/x86/domain.c +1 -0
diff -r ab7d2191421f -r e58b1d06aabe xen/arch/x86/domain.c
@@ -1578,6 +1578,7 @@
         memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES);
         if ( xsave_enabled(n) && n->arch.xcr0 != get_xcr0() )
             set_xcr0(n->arch.xcr0);
+        vcpu_restore_fpu_eager(n);
         n->arch.ctxt_switch_to(n);
     }
 
xen/arch/x86/hvm/svm/svm.c +1 -1
diff -r ab7d2191421f -r e58b1d06aabe xen/arch/x86/hvm/svm/svm.c
@@ -348,7 +348,7 @@
 {
     struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;
 
-    vcpu_restore_fpu(v);
+    vcpu_restore_fpu_lazy(v);
     vmcb_set_exception_intercepts(
         vmcb, vmcb_get_exception_intercepts(vmcb) & ~(1U << TRAP_no_device));
 }
xen/arch/x86/hvm/vmx/vmx.c +1 -1
diff -r ab7d2191421f -r e58b1d06aabe xen/arch/x86/hvm/vmx/vmx.c
@@ -612,7 +612,7 @@
 
 static void vmx_fpu_enter(struct vcpu *v)
 {
-    vcpu_restore_fpu(v);
+    vcpu_restore_fpu_lazy(v);
     v->arch.hvm_vmx.exception_bitmap &= ~(1u << TRAP_no_device);
     vmx_update_exception_bitmap(v);
     v->arch.hvm_vmx.host_cr0 &= ~X86_CR0_TS;
xen/arch/x86/i387.c +20 -5
diff -r ab7d2191421f -r e58b1d06aabe xen/arch/x86/i387.c
@@ -98,13 +98,13 @@
 /*      FPU Save Functions     */
 /*******************************/
 /* Save x87 extended state */
-static inline void fpu_xsave(struct vcpu *v, uint64_t mask)
+static inline void fpu_xsave(struct vcpu *v)
 {
     /* XCR0 normally represents what guest OS set. In case of Xen itself,
      * we set all accumulated feature mask before doing save/restore.
      */
     set_xcr0(v->arch.xcr0_accum);
-    xsave(v, mask);
+    xsave(v, v->arch.nonlazy_xstate_used ? XSTATE_ALL : XSTATE_LAZY);
     set_xcr0(v->arch.xcr0);    
 }
 
@@ -160,10 +160,25 @@
 /*******************************/
 /*       VCPU FPU Functions    */
 /*******************************/
+/* Restore FPU state whenever VCPU is schduled in. */
+void vcpu_restore_fpu_eager(struct vcpu *v)
+{
+    ASSERT(!is_idle_vcpu(v));
+    
+    /* save the nonlazy extended state which is not tracked by CR0.TS bit */
+    if ( v->arch.nonlazy_xstate_used )
+    {
+        /* Avoid recursion */
+        clts();        
+        fpu_xrstor(v, XSTATE_NONLAZY);
+        stts();
+    }
+}
+
 /* 
  * Restore FPU state when #NM is triggered.
  */
-void vcpu_restore_fpu(struct vcpu *v)
+void vcpu_restore_fpu_lazy(struct vcpu *v)
 {
     ASSERT(!is_idle_vcpu(v));
 
@@ -174,7 +189,7 @@
         return;
 
     if ( xsave_enabled(v) )
-        fpu_xrstor(v, XSTATE_ALL);
+        fpu_xrstor(v, XSTATE_LAZY);
     else if ( v->fpu_initialised )
     {
         if ( cpu_has_fxsr )
@@ -204,7 +219,7 @@
     clts();
 
     if ( xsave_enabled(v) )
-        fpu_xsave(v, XSTATE_ALL);
+        fpu_xsave(v);
     else if ( cpu_has_fxsr )
         fpu_fxsave(v);
     else
xen/arch/x86/traps.c +1 -1
diff -r ab7d2191421f -r e58b1d06aabe xen/arch/x86/traps.c
@@ -3198,7 +3198,7 @@
 
     BUG_ON(!guest_mode(regs));
 
-    vcpu_restore_fpu(curr);
+    vcpu_restore_fpu_lazy(curr);
 
     if ( curr->arch.pv_vcpu.ctrlreg[0] & X86_CR0_TS )
     {
xen/include/asm-x86/domain.h +4 -1
diff -r ab7d2191421f -r e58b1d06aabe xen/include/asm-x86/domain.h
@@ -492,7 +492,10 @@
      * it explicitly enables it via xcr0.
      */
     uint64_t xcr0_accum;
-
+    /* This variable determines whether nonlazy extended state has been used,
+     * and thus should be saved/restored. */
+    bool_t nonlazy_xstate_used;
+    
     struct paging_vcpu paging;
 
 #ifdef CONFIG_X86_32
xen/include/asm-x86/i387.h +2 -1
diff -r ab7d2191421f -r e58b1d06aabe xen/include/asm-x86/i387.h
@@ -14,7 +14,8 @@
 #include <xen/types.h>
 #include <xen/percpu.h>
 
-void vcpu_restore_fpu(struct vcpu *v);
+void vcpu_restore_fpu_eager(struct vcpu *v);
+void vcpu_restore_fpu_lazy(struct vcpu *v);
 void vcpu_save_fpu(struct vcpu *v);
 
 int vcpu_init_fpu(struct vcpu *v);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#17 lore raw

PATCH 1/1 LWP: export LWP related CPUID to AMD SVM guest

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304449290 18000
# Node ID 92b0ed16c09da1fb24f5c21ed06ed1c99709b6d7 # Parent e58b1d06aabe5c0aa26f2bd29b607a1c337d6c80
LWP: export LWP related CPUID to AMD SVM guest

This patch exposes LWP CPUID 0x8000001C to SVM guests.

tools/libxc/xc_cpuid_x86.c +4 -2
diff -r e58b1d06aabe -r 92b0ed16c09d tools/libxc/xc_cpuid_x86.c
@@ -31,7 +31,7 @@
 
 #define DEF_MAX_BASE 0x0000000du
 #define DEF_MAX_INTELEXT  0x80000008u
-#define DEF_MAX_AMDEXT    0x8000000au
+#define DEF_MAX_AMDEXT    0x8000001cu
 
 static int hypervisor_is_64bit(xc_interface *xch)
 {
@@ -111,7 +111,8 @@
                     bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
                     bitmaskof(X86_FEATURE_XOP) |
                     bitmaskof(X86_FEATURE_FMA4) |
-                    bitmaskof(X86_FEATURE_TBM));
+                    bitmaskof(X86_FEATURE_TBM) |
+                    bitmaskof(X86_FEATURE_LWP));
         regs[3] &= (0x0183f3ff | /* features shared with 0x00000001:EDX */
                     (is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
@@ -385,6 +386,7 @@
     case 0x80000005: /* AMD L1 cache/TLB info (dumped by Intel policy) */
     case 0x80000006: /* AMD L2/3 cache/TLB info ; Intel L2 cache features */
     case 0x8000000a: /* AMD SVM feature bits */
+    case 0x8000001c: /* AMD lightweight profiling */
         break;
 
     default:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#19 lore raw

PATCH 1/1 LWP: Add LWP support for SVM guests

In reply to a message not in the archive

# HG changeset patch
# User Wei Huang <[email protected]>
# Date 1304746997 18000
# Node ID ea027ce41fb45a619d39f7bdd2bd94e233fe2030 # Parent 92b0ed16c09da1fb24f5c21ed06ed1c99709b6d7
LWP: Add LWP support for SVM guests

This patch enables SVM to handle LWP related MSRs and CPUID. It intercepts guests read/write to LWP_CFG. It also save/restore LWP_CFG when guests touch this MSR. The LWP_CBADDR MSR is not intercepted because this MSR is handled by xsave/xrstor.

xen/arch/x86/hvm/svm/svm.c +75 -1
diff -r 92b0ed16c09d -r ea027ce41fb4 xen/arch/x86/hvm/svm/svm.c
@@ -58,7 +58,8 @@
 #include <asm/hvm/trace.h>
 #include <asm/hap.h>
 #include <asm/apic.h>
-#include <asm/debugger.h>       
+#include <asm/debugger.h>
+#include <asm/xstate.h>
 
 u32 svm_feature_flags;
 
@@ -695,6 +696,50 @@
     *(u16 *)(hypercall_page + (__HYPERVISOR_iret * 32)) = 0x0b0f; /* ud2 */
 }
 
+static inline void svm_lwp_save(struct vcpu *v)
+{
+    /* Don't mess up with other guests. Disable LWP for next VCPU. */
+    if ( v->arch.hvm_svm.guest_lwp_cfg )
+    {
+        wrmsrl(MSR_AMD64_LWP_CFG, 0x0);
+        wrmsrl(MSR_AMD64_LWP_CBADDR, 0x0);
+    }
+}
+
+static inline void svm_lwp_load(struct vcpu *v)
+{
+    /* Only LWP_CFG is reloaded. LWP_CBADDR will be reloaded via xrstor. */
+   if ( v->arch.hvm_svm.guest_lwp_cfg ) 
+       wrmsrl(MSR_AMD64_LWP_CFG, v->arch.hvm_svm.guest_lwp_cfg);
+}
+
+/* Update LWP_CFG MSR (0xc0000105). Return -1 if error; otherwise returns 0. */
+static int svm_update_lwp_cfg(struct vcpu *v, uint64_t msr_content)
+{
+    unsigned int eax, ebx, ecx, edx;
+    uint32_t msr_low;
+    
+    if ( xsave_enabled(v) && cpu_has_lwp )
+    {
+        hvm_cpuid(0x8000001c, &eax, &ebx, &ecx, &edx);
+        msr_low = (uint32_t)msr_content;
+        
+        /* generate #GP if guest tries to turn on unsupported features. */
+        if ( msr_low & ~edx)
+            return -1;
+        
+        wrmsrl(MSR_AMD64_LWP_CFG, msr_content);
+        /* CPU might automatically correct reserved bits. So read it back. */
+        rdmsrl(MSR_AMD64_LWP_CFG, msr_content);
+        v->arch.hvm_svm.guest_lwp_cfg = msr_content;
+
+        /* track nonalzy state if LWP_CFG is non-zero. */
+        v->arch.nonlazy_xstate_used = !!(msr_content);
+    }
+
+    return 0;
+}
+
 static void svm_ctxt_switch_from(struct vcpu *v)
 {
     int cpu = smp_processor_id();
@@ -703,6 +748,7 @@
 
     svm_save_dr(v);
     vpmu_save(v);
+    svm_lwp_save(v);
 
     svm_sync_vmcb(v);
     svm_vmload(per_cpu(root_vmcb, cpu));
@@ -746,6 +792,7 @@
     svm_vmload(vmcb);
     vmcb->cleanbits.bytes = 0;
     vpmu_load(v);
+    svm_lwp_load(v);
 
     if ( cpu_has_rdtscp )
         wrmsrl(MSR_TSC_AUX, hvm_msr_tsc_aux(v));
@@ -1120,6 +1167,24 @@
         if ( vlapic_hw_disabled(vcpu_vlapic(v)) )
             __clear_bit(X86_FEATURE_APIC & 31, edx);
         break;
+    case 0x8000001c: 
+    {
+        /* LWP capability CPUID */
+        uint64_t lwp_cfg = v->arch.hvm_svm.guest_lwp_cfg;
+
+        if ( cpu_has_lwp )
+        {
+            if ( !(v->arch.xcr0 & XSTATE_LWP) )
+           {
+                *eax = 0x0;
+                break;
+            }
+
+            /* turn on available bit and other features specified in lwp_cfg */
+            *eax = (*edx & lwp_cfg) | 0x00000001;
+        }
+        break;
+    }
     default:
         break;
     }
@@ -1227,6 +1292,10 @@
         *msr_content = vmcb_get_lastinttoip(vmcb);
         break;
 
+    case MSR_AMD64_LWP_CFG:
+        *msr_content = v->arch.hvm_svm.guest_lwp_cfg;
+        break;
+
     case MSR_K7_PERFCTR0:
     case MSR_K7_PERFCTR1:
     case MSR_K7_PERFCTR2:
@@ -1323,6 +1392,11 @@
 
     case MSR_IA32_LASTINTTOIP:
         vmcb_set_lastinttoip(vmcb, msr_content);
+        break;
+
+    case MSR_AMD64_LWP_CFG:
+        if ( svm_update_lwp_cfg(v, msr_content) < 0 )
+            goto gpf;
         break;
 
     case MSR_K7_PERFCTR0:
xen/arch/x86/hvm/svm/vmcb.c +5 -0
diff -r 92b0ed16c09d -r ea027ce41fb4 xen/arch/x86/hvm/svm/vmcb.c
@@ -120,6 +120,11 @@
     svm_disable_intercept_for_msr(v, MSR_LSTAR);
     svm_disable_intercept_for_msr(v, MSR_STAR);
     svm_disable_intercept_for_msr(v, MSR_SYSCALL_MASK);
+
+    /* LWP_CBADDR MSR is saved and restored by FPU code. So SVM doesn't need to
+     * intercept it. */
+    if ( cpu_has_lwp )
+        svm_disable_intercept_for_msr(v, MSR_AMD64_LWP_CBADDR);
 
     vmcb->_msrpm_base_pa = (u64)virt_to_maddr(arch_svm->msrpm);
     vmcb->_iopm_base_pa  = (u64)virt_to_maddr(hvm_io_bitmap);
xen/include/asm-x86/cpufeature.h +2 -0
diff -r 92b0ed16c09d -r ea027ce41fb4 xen/include/asm-x86/cpufeature.h
@@ -208,6 +208,8 @@
 
 #define cpu_has_xsave           boot_cpu_has(X86_FEATURE_XSAVE)
 
+#define cpu_has_lwp             boot_cpu_has(X86_FEATURE_LWP)
+
 #define cpu_has_arch_perfmon    boot_cpu_has(X86_FEATURE_ARCH_PERFMON)
 
 #define cpu_has_rdtscp          boot_cpu_has(X86_FEATURE_RDTSCP)
xen/include/asm-x86/hvm/svm/vmcb.h +3 -0
diff -r 92b0ed16c09d -r ea027ce41fb4 xen/include/asm-x86/hvm/svm/vmcb.h
@@ -512,6 +512,9 @@
     uint64_t guest_sysenter_cs;
     uint64_t guest_sysenter_esp;
     uint64_t guest_sysenter_eip;
+    
+    /* AMD lightweight profiling MSR */
+    uint64_t guest_lwp_cfg;
 };
 
 struct vmcb_struct *alloc_vmcb(void);
xen/include/asm-x86/msr-index.h +4 -0
diff -r 92b0ed16c09d -r ea027ce41fb4 xen/include/asm-x86/msr-index.h
@@ -253,6 +253,10 @@
 #define MSR_AMD_PATCHLEVEL		0x0000008b
 #define MSR_AMD_PATCHLOADER		0xc0010020
 
+/* AMD Lightweight Profiling MSRs */
+#define MSR_AMD64_LWP_CFG		0xc0000105
+#define MSR_AMD64_LWP_CBADDR		0xc0000106
+
 /* AMD OS Visible Workaround MSRs */
 #define MSR_AMD_OSVW_ID_LENGTH          0xc0010140
 #define MSR_AMD_OSVW_STATUS             0xc0010141

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#15 lore raw

Re: [PATCH 6 of 8] FPU: create lazy and non-lazy FPU restore functions

In reply to Huang2, Wei

xen/arch/x86/i387.c line 101

Quoted 9 lines
Quoted 1 line
Quoted 1 line · Wei
xen/arch/x86/i387.c +1 -1
@@ -98,13 +98,13 @@
 /*      FPU Save Functions     */
 /*******************************/
 /* Save x87 extended state */
-static inline void fpu_xsave(struct vcpu *v, uint64_t mask)
+static inline void fpu_xsave(struct vcpu *v)

This looks okay now to me, only a cosmetic comment: You add the "mask" parameter in patch 5, just to remove it here again.

Jan

#2 lore raw
In reply to Huang2, Wei
Quoted 31 lines
Quoted 1 line
Quoted 1 line · Wei

This patch set supports AMD lightweight profiling for SVM guests. Because LWP isn't tracked by CR0.TS bit, we clean up the FPU code to handle lazy and unlazy FPU states differently. Lazy FPU state (such as SSE, YMM) is handled when #NM is triggered. Unlazy state, such as LWP, is saved and restored on each vcpu context switch.

Per Keir's comments, I moved extended state related code into xstate.c and xstate.h. The FPU related code in i387.c was also cleaned up and has consistent names now. The comments from Jan Beulich were also taken. I added a new variable, nonlazy_xstate_used, to control whether save/restore nonlazy state.

====== i387.c ======
* void vcpu_restore_fpu_eager(struct vcpu *v);
* void vcpu_restore_fpu_lazy(struct vcpu *v);
* void vcpu_save_fpu(struct vcpu *v);
* int vcpu_init_fpu(struct vcpu *v);
* void vcpu_destroy_fpu(struct vcpu *v);

====== xstate.c ======
* void set_xcr0(u64 xfeatures);
* uint64_t get_xcr0(void);
* void xsave(struct vcpu *v, uint64_t mask);
* void xrstor(struct vcpu *v, uint64_t mask);
* bool_t xsave_enabled(const struct vcpu *v);
* void xstate_free_save_area(struct vcpu *v);
* int xstate_alloc_save_area(struct vcpu *v);
* void xstate_init(void);

Quoted 1 line

This code has been tested on real hardware. Please comment.

Thanks, Wei - this looks good now to me.

Jan

Quoted 31 lines

-Wei

 b/xen/arch/x86/xstate.c            |  188 +++++++++++++++
 b/xen/include/asm-x86/xstate.h     |   72 +++++
 tools/libxc/xc_cpuid_x86.c         |    6
 xen/arch/x86/Makefile              |    1
 xen/arch/x86/acpi/suspend.c        |    2
 xen/arch/x86/cpu/common.c          |    4
 xen/arch/x86/domain.c              |   29 --
 xen/arch/x86/domctl.c              |    2
 xen/arch/x86/hvm/hvm.c             |    3
 xen/arch/x86/hvm/svm/svm.c         |   75 ++++++
 xen/arch/x86/hvm/svm/vmcb.c        |    5
 xen/arch/x86/hvm/vmx/vmcs.c        |    1
 xen/arch/x86/hvm/vmx/vmx.c         |    2
 xen/arch/x86/i387.c                |  450 ++++++++++++++++---------------------
 xen/arch/x86/traps.c               |    3
 xen/include/asm-x86/cpufeature.h   |    2
 xen/include/asm-x86/domain.h       |    5
 xen/include/asm-x86/hvm/svm/vmcb.h |    3
 xen/include/asm-x86/i387.h         |   71 -----
 xen/include/asm-x86/msr-index.h    |    4
 20 files changed, 573 insertions(+), 355 deletions(-)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel