All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: kvm@vger.kernel.org
Cc: avi@redhat.com, gleb@redhat.com, Marcelo Tosatti <mtosatti@redhat.com>
Subject: [patch 2/5] qemu-kvm: use upstream fpu/xsave/xcrs save/restore code
Date: Sat, 23 Oct 2010 20:05:26 -0200	[thread overview]
Message-ID: <20101023220718.605518479@amt.cnet> (raw)
In-Reply-To: 20101023220524.049191736@amt.cnet

[-- Attachment #1: qemu-kvm-x86-3 --]
[-- Type: text/plain, Size: 8851 bytes --]

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Index: qemu-kvm/qemu-kvm.c
===================================================================
--- qemu-kvm.orig/qemu-kvm.c
+++ qemu-kvm/qemu-kvm.c
@@ -472,16 +472,6 @@ int kvm_set_regs(CPUState *env, struct k
     return kvm_vcpu_ioctl(env, KVM_SET_REGS, regs);
 }
 
-int kvm_get_fpu(CPUState *env, struct kvm_fpu *fpu)
-{
-    return kvm_vcpu_ioctl(env, KVM_GET_FPU, fpu);
-}
-
-int kvm_set_fpu(CPUState *env, struct kvm_fpu *fpu)
-{
-    return kvm_vcpu_ioctl(env, KVM_SET_FPU, fpu);
-}
-
 int kvm_get_sregs(CPUState *env, struct kvm_sregs *sregs)
 {
     return kvm_vcpu_ioctl(env, KVM_GET_SREGS, sregs);
@@ -1696,6 +1686,16 @@ static int kvm_create_context(void)
     kvm_state->debugregs = kvm_check_extension(kvm_state, KVM_CAP_DEBUGREGS);
 #endif
 
+    kvm_state->xsave = 0;
+#ifdef KVM_CAP_XSAVE
+    kvm_state->xsave = kvm_check_extension(kvm_state, KVM_CAP_XSAVE);
+#endif
+
+    kvm_state->xcrs = 0;
+#ifdef KVM_CAP_XCRS
+    kvm_state->xcrs = kvm_check_extension(kvm_state, KVM_CAP_XCRS);
+#endif
+
     kvm_init_ap();
     if (kvm_irqchip) {
         if (!qemu_kvm_has_gsi_routing()) {
Index: qemu-kvm/qemu-kvm-x86.c
===================================================================
--- qemu-kvm.orig/qemu-kvm-x86.c
+++ qemu-kvm/qemu-kvm-x86.c
@@ -769,7 +769,6 @@ static void get_seg(SegmentCache *lhs, c
 void kvm_arch_load_regs(CPUState *env, int level)
 {
     struct kvm_regs regs;
-    struct kvm_fpu fpu;
     struct kvm_sregs sregs;
     struct kvm_msr_entry msrs[100];
     int rc, n, i;
@@ -800,58 +799,8 @@ void kvm_arch_load_regs(CPUState *env, i
 
     kvm_set_regs(env, &regs);
 
-#ifdef KVM_CAP_XSAVE
-    if (kvm_check_extension(kvm_state, KVM_CAP_XSAVE)) {
-        struct kvm_xsave* xsave;
-
-        uint16_t cwd, swd, twd, fop;
-
-        xsave = qemu_memalign(4096, sizeof(struct kvm_xsave));
-        memset(xsave, 0, sizeof(struct kvm_xsave));
-        cwd = swd = twd = fop = 0;
-        swd = env->fpus & ~(7 << 11);
-        swd |= (env->fpstt & 7) << 11;
-        cwd = env->fpuc;
-        for (i = 0; i < 8; ++i) {
-            twd |= (!env->fptags[i]) << i;
-        }
-        xsave->region[0] = (uint32_t)(swd << 16) + cwd;
-        xsave->region[1] = (uint32_t)(fop << 16) + twd;
-        memcpy(&xsave->region[XSAVE_ST_SPACE], env->fpregs,
-               sizeof env->fpregs);
-        memcpy(&xsave->region[XSAVE_XMM_SPACE], env->xmm_regs,
-               sizeof env->xmm_regs);
-        xsave->region[XSAVE_MXCSR] = env->mxcsr;
-        *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv;
-        memcpy(&xsave->region[XSAVE_YMMH_SPACE], env->ymmh_regs,
-               sizeof env->ymmh_regs);
-        kvm_vcpu_ioctl(env, KVM_SET_XSAVE, xsave);
-        if (kvm_check_extension(kvm_state, KVM_CAP_XCRS)) {
-            struct kvm_xcrs xcrs;
-
-            xcrs.nr_xcrs = 1;
-            xcrs.flags = 0;
-            xcrs.xcrs[0].xcr = 0;
-            xcrs.xcrs[0].value = env->xcr0;
-            kvm_vcpu_ioctl(env, KVM_SET_XCRS, &xcrs);
-        }
-        qemu_free(xsave);
-    } else {
-#endif
-        memset(&fpu, 0, sizeof fpu);
-        fpu.fsw = env->fpus & ~(7 << 11);
-        fpu.fsw |= (env->fpstt & 7) << 11;
-        fpu.fcw = env->fpuc;
-        for (i = 0; i < 8; ++i) {
-            fpu.ftwx |= (!env->fptags[i]) << i;
-        }
-        memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
-        memcpy(fpu.xmm, env->xmm_regs, sizeof env->xmm_regs);
-        fpu.mxcsr = env->mxcsr;
-        kvm_set_fpu(env, &fpu);
-#ifdef KVM_CAP_XSAVE
-    }
-#endif
+    kvm_put_xsave(env);
+    kvm_put_xcrs(env);
 
     memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));
     if (env->interrupt_injected >= 0) {
@@ -974,7 +923,6 @@ void kvm_arch_load_regs(CPUState *env, i
 void kvm_arch_save_regs(CPUState *env)
 {
     struct kvm_regs regs;
-    struct kvm_fpu fpu;
     struct kvm_sregs sregs;
     struct kvm_msr_entry msrs[100];
     uint32_t hflags;
@@ -1006,54 +954,8 @@ void kvm_arch_save_regs(CPUState *env)
     env->eflags = regs.rflags;
     env->eip = regs.rip;
 
-#ifdef KVM_CAP_XSAVE
-    if (kvm_check_extension(kvm_state, KVM_CAP_XSAVE)) {
-        struct kvm_xsave* xsave;
-        uint16_t cwd, swd, twd, fop;
-        xsave = qemu_memalign(4096, sizeof(struct kvm_xsave));
-        kvm_vcpu_ioctl(env, KVM_GET_XSAVE, xsave);
-        cwd = (uint16_t)xsave->region[0];
-        swd = (uint16_t)(xsave->region[0] >> 16);
-        twd = (uint16_t)xsave->region[1];
-        fop = (uint16_t)(xsave->region[1] >> 16);
-        env->fpstt = (swd >> 11) & 7;
-        env->fpus = swd;
-        env->fpuc = cwd;
-        for (i = 0; i < 8; ++i) {
-            env->fptags[i] = !((twd >> i) & 1);
-        }
-        env->mxcsr = xsave->region[XSAVE_MXCSR];
-        memcpy(env->fpregs, &xsave->region[XSAVE_ST_SPACE],
-                sizeof env->fpregs);
-        memcpy(env->xmm_regs, &xsave->region[XSAVE_XMM_SPACE],
-                sizeof env->xmm_regs);
-        env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV];
-        memcpy(env->ymmh_regs, &xsave->region[XSAVE_YMMH_SPACE],
-                sizeof env->ymmh_regs);
-        if (kvm_check_extension(kvm_state, KVM_CAP_XCRS)) {
-            struct kvm_xcrs xcrs;
-
-            kvm_vcpu_ioctl(env, KVM_GET_XCRS, &xcrs);
-            if (xcrs.xcrs[0].xcr == 0) {
-                env->xcr0 = xcrs.xcrs[0].value;
-            }
-        }
-        qemu_free(xsave);
-    } else {
-#endif
-        kvm_get_fpu(env, &fpu);
-        env->fpstt = (fpu.fsw >> 11) & 7;
-        env->fpus = fpu.fsw;
-        env->fpuc = fpu.fcw;
-        for (i = 0; i < 8; ++i) {
-            env->fptags[i] = !((fpu.ftwx >> i) & 1);
-        }
-        memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
-        memcpy(env->xmm_regs, fpu.xmm, sizeof env->xmm_regs);
-        env->mxcsr = fpu.mxcsr;
-#ifdef KVM_CAP_XSAVE
-    }
-#endif
+    kvm_get_xsave(env);
+    kvm_get_xcrs(env);
 
     kvm_get_sregs(env, &sregs);
 
Index: qemu-kvm/qemu-kvm.h
===================================================================
--- qemu-kvm.orig/qemu-kvm.h
+++ qemu-kvm/qemu-kvm.h
@@ -206,36 +206,6 @@ int kvm_get_regs(CPUState *env, struct k
  * \return 0 on success
  */
 int kvm_set_regs(CPUState *env, struct kvm_regs *regs);
-/*!
- * \brief Read VCPU fpu registers
- *
- * This gets the FPU registers from the VCPU and outputs them
- * into a kvm_fpu structure
- *
- * \note This function returns a \b copy of the VCPUs registers.\n
- * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
- *
- * \param kvm Pointer to the current kvm_context
- * \param vcpu Which virtual CPU should get dumped
- * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
- * fpu registers values
- * \return 0 on success
- */
-int kvm_get_fpu(CPUState *env, struct kvm_fpu *fpu);
-
-/*!
- * \brief Write VCPU fpu registers
- *
- * This sets the FPU registers on the VCPU from a kvm_fpu structure
- *
- * \note When this function returns, the fpu pointer and the data it points to
- * can be discarded
- * \param kvm Pointer to the current kvm_context
- * \param vcpu Which virtual CPU should get dumped
- * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
- * \return 0 on success
- */
-int kvm_set_fpu(CPUState *env, struct kvm_fpu *fpu);
 
 /*!
  * \brief Read VCPU system registers
@@ -847,6 +817,7 @@ struct KVMState {
 #endif
     int irqchip_in_kernel;
     int pit_in_kernel;
+    int xsave, xcrs;
 
     struct kvm_context kvm_context;
 };
Index: qemu-kvm/target-i386/kvm.c
===================================================================
--- qemu-kvm.orig/target-i386/kvm.c
+++ qemu-kvm/target-i386/kvm.c
@@ -675,6 +675,8 @@ static int kvm_getput_regs(CPUState *env
     return ret;
 }
 
+#endif
+
 static int kvm_put_fpu(CPUState *env)
 {
     struct kvm_fpu fpu;
@@ -757,6 +759,8 @@ static int kvm_put_xcrs(CPUState *env)
 #endif
 }
 
+#ifdef OBSOLETE_KVM_IMPL
+
 static int kvm_put_sregs(CPUState *env)
 {
     struct kvm_sregs sregs;
@@ -879,6 +883,7 @@ static int kvm_put_msrs(CPUState *env, i
 
 }
 
+#endif
 
 static int kvm_get_fpu(CPUState *env)
 {
@@ -967,6 +972,8 @@ static int kvm_get_xcrs(CPUState *env)
 #endif
 }
 
+#ifdef OBSOLETE_KVM_IMPL
+
 static int kvm_get_sregs(CPUState *env)
 {
     struct kvm_sregs sregs;
Index: qemu-kvm/kvm-all.c
===================================================================
--- qemu-kvm.orig/kvm-all.c
+++ qemu-kvm/kvm-all.c
@@ -1056,7 +1056,6 @@ int kvm_has_debugregs(void)
     return kvm_state->debugregs;
 }
 
-#ifdef OBSOLETE_KVM_IMPL
 int kvm_has_xsave(void)
 {
     return kvm_state->xsave;
@@ -1066,7 +1065,6 @@ int kvm_has_xcrs(void)
 {
     return kvm_state->xcrs;
 }
-#endif
 
 void kvm_setup_guest_memory(void *start, size_t size)
 {



  parent reply	other threads:[~2010-10-23 22:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-23 22:05 [patch 0/5] use upstream x86 state save/restore code Marcelo Tosatti
2010-10-23 22:05 ` [patch 1/5] qemu-kvm: kill xsave/xcrs helpers Marcelo Tosatti
2010-10-23 22:05 ` Marcelo Tosatti [this message]
2010-10-23 22:05 ` [patch 3/5] qemu-kvm: use upstream sregs save/restore code Marcelo Tosatti
2010-10-23 22:05 ` [patch 4/5] qemu-kvm: use upstream regs " Marcelo Tosatti
2010-10-23 22:05 ` [patch 5/5] qemu-kvm: use upstream msr " Marcelo Tosatti
2010-10-24 13:46 ` [patch 0/5] use upstream x86 state " Avi Kivity
2010-10-24 21:05   ` Marcelo Tosatti
2010-10-25  9:56     ` Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101023220718.605518479@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=avi@redhat.com \
    --cc=gleb@redhat.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.