Skip to contentSkip to conversation

Introduce a fpu_initilised filed to HVM CPU context

RFC PATCH Superseded Roger Pau Monne 5 participants 14 messages Oct 14, 2015 – Nov 17, 2015

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover Introduce a fpu_initilised filed to HVM CPU context

Hello,

This patch series tries to properly solve the problem seen with the HVMlite series, that Xen always assumes the FPU is initialised on CPU context restore.

Since I don't have a pre-3.4 Xen box, I would like to request if this patch series can be tested by the XenServer CI loop, which I've heard tests migrations between very old Xen versions.

Thanks, Roger.

#1 lore raw

RFC PATCH 1/3 xen/save: pass a size paramter to the HVM compat functions

In order to cope with types having multiple compat versions pass a parameter to the fixup function so we can identify which compat version Xen is dealing with.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Tim Deegan <[email protected]>
---
 xen/include/public/arch-x86/hvm/save.h |  2 +-
 xen/include/public/hvm/save.h          | 10 ++++++----
 xen/include/xen/hvm/save.h             |  2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)
xen/include/public/arch-x86/hvm/save.h +1 -1
index efb0b62..c7560f2 100644
@@ -268,7 +268,7 @@ struct hvm_hw_cpu_compat {
     uint32_t error_code;
 };
 
-static inline int _hvm_hw_fix_cpu(void *h) {
+static inline int _hvm_hw_fix_cpu(void *h, int size) {
 
     union hvm_hw_cpu_union {
         struct hvm_hw_cpu nat;
xen/include/public/hvm/save.h +6 -4
index cc8b5fd..411871e 100644
@@ -63,13 +63,15 @@ struct hvm_save_descriptor {
 
 #ifdef __XEN__
 # define DECLARE_HVM_SAVE_TYPE_COMPAT(_x, _code, _type, _ctype, _fix)     \
-    static inline int __HVM_SAVE_FIX_COMPAT_##_x(void *h) { return _fix(h); } \
-    struct __HVM_SAVE_TYPE_##_x { _type t; char c[_code]; char cpt[2];}; \
+    static inline int __HVM_SAVE_FIX_COMPAT_##_x(void *h, int size)       \
+        { return _fix(h, size); }                                         \
+    struct __HVM_SAVE_TYPE_##_x { _type t; char c[_code]; char cpt[2];};  \
     struct __HVM_SAVE_TYPE_COMPAT_##_x { _ctype t; }                   
 
 # include <xen/lib.h> /* BUG() */
 # define DECLARE_HVM_SAVE_TYPE(_x, _code, _type)                         \
-    static inline int __HVM_SAVE_FIX_COMPAT_##_x(void *h) { BUG(); return -1; } \
+    static inline int __HVM_SAVE_FIX_COMPAT_##_x(void *h, int size)      \
+        { BUG(); return -1; }                                            \
     struct __HVM_SAVE_TYPE_##_x { _type t; char c[_code]; char cpt[1];}; \
     struct __HVM_SAVE_TYPE_COMPAT_##_x { _type t; }                   
 #else
@@ -89,7 +91,7 @@ struct hvm_save_descriptor {
 # define HVM_SAVE_LENGTH_COMPAT(_x) (sizeof (HVM_SAVE_TYPE_COMPAT(_x)))
 
 # define HVM_SAVE_HAS_COMPAT(_x) (sizeof (((struct __HVM_SAVE_TYPE_##_x *)(0))->cpt)-1)
-# define HVM_SAVE_FIX_COMPAT(_x, _dst) __HVM_SAVE_FIX_COMPAT_##_x(_dst)
+# define HVM_SAVE_FIX_COMPAT(_x, _dst, _code) __HVM_SAVE_FIX_COMPAT_##_x(_dst, _code)
 #endif
 
 /* 
xen/include/xen/hvm/save.h +1 -1
index aa27a50..14e6754 100644
@@ -67,7 +67,7 @@ void _hvm_read_entry(struct hvm_domain_context *h,
              && (r = _hvm_check_entry((_h), HVM_SAVE_CODE(_x),          \
                        HVM_SAVE_LENGTH_COMPAT(_x), (_strict))) == 0 ) { \
         _hvm_read_entry((_h), (_dst), HVM_SAVE_LENGTH_COMPAT(_x));      \
-        r=HVM_SAVE_FIX_COMPAT(_x, (_dst));                              \
+        r=HVM_SAVE_FIX_COMPAT(_x, (_dst), (_h)->size);                  \
     }                                                                   \
     r; })
 
Signature · 7 lines
1.9.5 (Apple Git-50.3)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH RFC 1/3] xen/save: pass a size paramter to the HVM compat functions

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 14.10.15 at 18:24, <[email protected]> wrote:

In order to cope with types having multiple compat versions pass a parameter to the fixup function so we can identify which compat version Xen is dealing with.

Having peeked at patch 2, this won't help once another bit gets added to the tail of that structure. Also it doesn't seem logical that the previous compat handling got around without being passed the size explicitly. I.e. while perhaps more involved, I think the compat handling needs to be extended to allow for multiple versions.

Or, since we have this under control going forward, don't even declare all the various compat structures in the public header (and only ever add to the tail). Then staying with the passing of size probably makes sense, but the fixup function then should use offsetof() instead of sizeof() (and validate unused tail bits are zero, so they can be used for something later on).

Jan

#3 lore raw

Re: [PATCH RFC 1/3] xen/save: pass a size paramter to the HVM compat functions

El 15/10/15 a les 10.12, Jan Beulich ha escrit:

Quoted 10 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 14.10.15 at 18:24, <[email protected]> wrote:

In order to cope with types having multiple compat versions pass a parameter to the fixup function so we can identify which compat version Xen is dealing with.

Having peeked at patch 2, this won't help once another bit gets added to the tail of that structure. Also it doesn't seem logical that the previous compat handling got around without being passed the size explicitly. I.e. while perhaps more involved, I think the compat handling needs to be extended to allow for multiple versions.

Yes, patch #2 needs to be fixed by making fpu_initialised an uint32_t. Adding versions would be the best option IMHO, but I don't think I have enough time to do that myself right now.

Quoted 6 lines

Or, since we have this under control going forward, don't even declare all the various compat structures in the public header (and only ever add to the tail). Then staying with the passing of size probably makes sense, but the fixup function then should use offsetof() instead of sizeof() (and validate unused tail bits are zero, so they can be used for something later on).

Since we use hvm_load_entry_zeroextend I think we can assert that all new tail bits are going to be 0, but I'm not sure I follow you regarding the usage of offsetof instead of sizeof. What are we going to compare with offsetof?

Also, we already have a compat version of hvm_hw_cpu that didn't add the new field to the end.

Roger.

#4 lore raw

Re: [PATCH RFC 1/3] xen/save: pass a size paramter to the HVM compat functions

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 15.10.15 at 11:49, <[email protected]> wrote:

El 15/10/15 a les 10.12, Jan Beulich ha escrit:

Quoted 10 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 14.10.15 at 18:24, <[email protected]> wrote:

In order to cope with types having multiple compat versions pass a parameter to the fixup function so we can identify which compat version Xen is dealing with.

Having peeked at patch 2, this won't help once another bit gets added to the tail of that structure. Also it doesn't seem logical that the previous compat handling got around without being passed the size explicitly. I.e. while perhaps more involved, I think the compat handling needs to be extended to allow for multiple versions.

Quoted 3 lines

Yes, patch #2 needs to be fixed by making fpu_initialised an uint32_t. Adding versions would be the best option IMHO, but I don't think I have enough time to do that myself right now.

Understood. An I don't think we really need versioning here.

Quoted 11 lines
Quoted 6 lines

Or, since we have this under control going forward, don't even declare all the various compat structures in the public header (and only ever add to the tail). Then staying with the passing of size probably makes sense, but the fixup function then should use offsetof() instead of sizeof() (and validate unused tail bits are zero, so they can be used for something later on).

Since we use hvm_load_entry_zeroextend I think we can assert that all new tail bits are going to be 0, but I'm not sure I follow you regarding the usage of offsetof instead of sizeof. What are we going to compare with offsetof?

Instead of introducing compat1 and compat2 structures, just add the new field to the existing structure, and use offsetof() to compare the passed in size with the that of the structure in its original state.

Quoted 2 lines

Also, we already have a compat version of hvm_hw_cpu that didn't add the new field to the end.

Right, but we can avoid making the same mistake again.

Jan

#5 lore raw

Re: [PATCH RFC 1/3] xen/save: pass a size paramter to the HVM compat functions

Quoted 26 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 15.10.15 at 11:49, <[email protected]> wrote:

El 15/10/15 a les 10.12, Jan Beulich ha escrit:

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 14.10.15 at 18:24, <[email protected]> wrote:

In order to cope with types having multiple compat versions pass a parameter to the fixup function so we can identify which compat version Xen is dealing with.

Quoted 8 lines
Quoted 5 lines

Having peeked at patch 2, this won't help once another bit gets added to the tail of that structure. Also it doesn't seem logical that the previous compat handling got around without being passed the size explicitly. I.e. while perhaps more involved, I think the compat handling needs to be extended to allow for multiple versions.

Yes, patch #2 needs to be fixed by making fpu_initialised an uint32_t. Adding versions would be the best option IMHO, but I don't think I have enough time to do that myself right now.

Understood. An I don't think we really need versioning here.

Quoted 10 lines
Quoted 6 lines

Or, since we have this under control going forward, don't even declare all the various compat structures in the public header (and only ever add to the tail). Then staying with the passing of size probably makes sense, but the fixup function then should use offsetof() instead of sizeof() (and validate unused tail bits are zero, so they can be used for something later on).

Since we use hvm_load_entry_zeroextend I think we can assert that all new tail bits are going to be 0, but I'm not sure I follow you regarding the usage of offsetof instead of sizeof. What are we going to compare with offsetof?

Quoted 7 lines

Instead of introducing compat1 and compat2 structures, just add the new field to the existing structure, and use offsetof() to compare the passed in size with the that of the structure in its original state.

Quoted 2 lines

Also, we already have a compat version of hvm_hw_cpu that didn't add the new field to the end.

Right, but we can avoid making the same mistake again.

Please be aware that, in due course, I will be replacing all of this, migration v2 style, as a prerequisite of getting cpuid policies functioning correctly during migrate.

While we shouldn't paint ourselves into a corner, I wouldn't worry too much about their longevity.

~Andrew

#6 lore raw

RFC PATCH 2/3 xen/hvm: introduce a fpu_initialised filed to the CPU save record

In reply to Roger Pau Monne

Introduce a new filed to signal if the FPU has been initialised or not. Xen needs this new filed in order to know whether to set the FPU as initialised or not during restore of CPU context. Previously Xen always wrongly assumed the FPU was initialised on restore.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/hvm.c                 |   5 +-
 xen/include/public/arch-x86/hvm/save.h | 146 ++++++++++++++++++++++++++++++---
 2 files changed, 139 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -2
index 3fa2280..e71570c 100644
@@ -1796,6 +1796,7 @@ static int hvm_save_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
             memcpy(ctxt.fpu_regs, v->arch.fpu_ctxt, sizeof(ctxt.fpu_regs));
         else 
             memset(ctxt.fpu_regs, 0, sizeof(ctxt.fpu_regs));
+        ctxt.fpu_initialised = v->fpu_initialised;
 
         ctxt.rax = v->arch.user_regs.eax;
         ctxt.rbx = v->arch.user_regs.ebx;
@@ -1975,7 +1976,7 @@ static int hvm_load_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
         return -EINVAL;
     }
 
-    if ( hvm_load_entry(CPU, h, &ctxt) != 0 ) 
+    if ( hvm_load_entry_zeroextend(CPU, h, &ctxt) != 0 )
         return -EINVAL;
 
     /* Sanity check some control registers. */
@@ -2118,7 +2119,7 @@ static int hvm_load_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     v->arch.debugreg[7] = ctxt.dr7;
 
     v->arch.vgc_flags = VGCF_online;
-    v->fpu_initialised = 1;
+    v->fpu_initialised = ctxt.fpu_initialised;
 
     /* Auxiliary processors should be woken immediately. */
     v->is_initialised = 1;
xen/include/public/arch-x86/hvm/save.h +136 -10

179 lines not on this page · raw message

Signature · 7 lines
1.9.5 (Apple Git-50.3)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#7 lore raw

Re: [PATCH RFC 2/3] xen/hvm: introduce a fpu_initialised filed to the CPU save record

Quoted 1 line · Roger Pau Monne

Introduce a new filed to signal if the FPU has been initialised or not. Xen

"field", I am guessing?

xen/include/public/arch-x86/hvm/save.h DECLARE_HVM_SAVE_TYPE(HEADER, 1, struct hvm_save_header); · line 51

Quoted 11 lines
xen/include/public/arch-x86/hvm/save.h +2 -1
index c7560f2..c4863be 100644
@@ -47,7 +47,8 @@ DECLARE_HVM_SAVE_TYPE(HEADER, 1, struct hvm_save_header);
 /*
  * Processor
  *
- * Compat: Pre-3.4 didn't have msr_tsc_aux
+ * Compat2: Pre-4.7 didn't have fpu_initialised
+ * Compat1: Pre-3.4 didn't have msr_tsc_aux

I would suggest reversing the Compat1 and 2 lines, to match chronology.

xen/include/public/arch-x86/hvm/save.h struct hvm_hw_cpu { · line 162

Quoted 9 lines
xen/include/public/arch-x86/hvm/save.h +2 -0
  */
 
 struct hvm_hw_cpu {
@@ -157,9 +158,121 @@ struct hvm_hw_cpu {
     };
     /* error code for pending event */
     uint32_t error_code;
+    /* is fpu initialised? */
+    uint8_t fpu_initialised:1;

Bitfields can't be used in the public ABI, and please don't leave trailing implicit padding.

I would recommend uint32_t flags and specify that bit 0 indicates that fpu context is initialised.

~Andrew

#8 lore raw

Re: [PATCH RFC 2/3] xen/hvm: introduce a fpu_initialised filed to the CPU save record

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 14.10.15 at 18:51, <[email protected]> wrote:

xen/include/public/arch-x86/hvm/save.h struct hvm_hw_cpu { · line 162

Quoted 6 lines · Roger Pau Monne
xen/include/public/arch-x86/hvm/save.h +2 -0
@@ -157,9 +158,121 @@ struct hvm_hw_cpu {
     };
     /* error code for pending event */
     uint32_t error_code;
+    /* is fpu initialised? */
+    uint8_t fpu_initialised:1;

Bitfields can't be used in the public ABI, and please don't leave trailing implicit padding.

Quoted 2 lines

I would recommend uint32_t flags and specify that bit 0 indicates that fpu context is initialised.

Seconded, despite the bad precedent in the pending event sub-union.

Jan

#9 lore raw

Re: [PATCH RFC 2/3] xen/hvm: introduce a fpu_initialised filed to the CPU save record

In reply to Andrew Cooper

El 14/10/15 a les 18.51, Andrew Cooper ha escrit:

Quoted 4 lines · Roger Pau Monne
Quoted 1 line

Introduce a new filed to signal if the FPU has been initialised or not. Xen

"field", I am guessing?

Yes.

Quoted 5 lines
Quoted 5 lines
xen/include/public/arch-x86/hvm/save.h +0 -0
index c7560f2..c4863be 100644
@@ -47,7 +47,8 @@ DECLARE_HVM_SAVE_TYPE(HEADER, 1, struct hvm_save_header);
Quoted 8 lines

xen/include/public/arch-x86/hvm/save.h DECLARE_HVM_SAVE_TYPE(HEADER, 1, struct hvm_save_header); · line 51

Quoted 6 lines
xen/include/public/arch-x86/hvm/save.h +2 -1
 /*
  * Processor
  *
- * Compat: Pre-3.4 didn't have msr_tsc_aux
+ * Compat2: Pre-4.7 didn't have fpu_initialised
+ * Compat1: Pre-3.4 didn't have msr_tsc_aux

I would suggest reversing the Compat1 and 2 lines, to match chronology.

OK, that's fine.

Quoted 12 lines

xen/include/public/arch-x86/hvm/save.h struct hvm_hw_cpu { · line 162

Quoted 9 lines
xen/include/public/arch-x86/hvm/save.h +2 -0
  */
 
 struct hvm_hw_cpu {
@@ -157,9 +158,121 @@ struct hvm_hw_cpu {
     };
     /* error code for pending event */
     uint32_t error_code;
+    /* is fpu initialised? */
+    uint8_t fpu_initialised:1;

Bitfields can't be used in the public ABI, and please don't leave trailing implicit padding.

Sadly we already have quite a lot of bitfields in the same file (see hvm_hw_vpic) but I understand why they shouldn't be used, specially if we do the versioning based on the size of the structure.

Quoted 2 lines

I would recommend uint32_t flags and specify that bit 0 indicates that fpu context is initialised.

Right.

Roger.

#10 lore raw

RFC PATCH 3/3 Revert "libxc: create an initial FPU state for HVM guests"

In reply to Roger Pau Monne

This reverts commit d64dbbcc7c9934a46126c59d78536235908377ad.

Xen always set the FPU as initialized when loading a HVM context, so libxc has to provide a valid FPU context when setting the CPU registers.

This is a stop-gap measure in order to unblock OSSTest Windows 7 failures while a proper fix for the HVM CPU save/restore is being worked on.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Ian Campbell <[email protected]>
Cc Wei Liu <[email protected]>
---
 tools/libxc/xc_dom_x86.c | 38 --------------------------------------
 1 file changed, 38 deletions(-)
tools/libxc/xc_dom_x86.c +0 -38
index 034abe0..dd331bf 100644
@@ -841,27 +841,6 @@ static int vcpu_hvm(struct xc_dom_image *dom)
         struct hvm_save_descriptor end_d;
         HVM_SAVE_TYPE(END) end;
     } bsp_ctx;
-    /*
-     * The layout of the fpu context structure is the same for
-     * both 32 and 64 bits.
-     */
-    struct {
-        uint16_t fcw;
-        uint16_t fsw;
-        uint8_t ftw;
-        uint8_t rsvd1;
-        uint16_t fop;
-        union {
-            uint64_t addr;
-            struct {
-                uint32_t offs;
-                uint16_t sel;
-                uint16_t rsvd;
-            };
-        } fip, fdp;
-        uint32_t mxcsr;
-        uint32_t mxcsr_mask;
-    } *fpu_ctxt;
     uint8_t *full_ctx = NULL;
     int rc;
 
@@ -929,23 +908,6 @@ static int vcpu_hvm(struct xc_dom_image *dom)
     /* Set the control registers. */
     bsp_ctx.cpu.cr0 = X86_CR0_PE | X86_CR0_ET;
 
-    /*
-     * XXX: Set initial FPU state.
-     *
-     * This should be removed once Xen is able to know if the
-     * FPU state saved is valid or not, now Xen always sets
-     * fpu_initialised to true regardless of the FPU state.
-     *
-     * The code below mimics the FPU sate after executing
-     * fninit
-     * ldmxcsr 0x1f80
-     */
-    fpu_ctxt = (typeof(fpu_ctxt))bsp_ctx.cpu.fpu_regs;
-
-    fpu_ctxt->fcw = 0x37f;
-    fpu_ctxt->ftw = 0xff;
-    fpu_ctxt->mxcsr = 0x1f80;
-
     /* Set the IP. */
     bsp_ctx.cpu.rip = dom->parms.phys_entry;
 
Signature · 7 lines
1.9.5 (Apple Git-50.3)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Acked-by on v1 3/3
#11 lore raw

Re: [PATCH RFC 3/3] Revert "libxc: create an initial FPU state for HVM guests"

Quoted 7 lines · Roger Pau Monne

This reverts commit d64dbbcc7c9934a46126c59d78536235908377ad.

Xen always set the FPU as initialized when loading a HVM context, so libxc has to provide a valid FPU context when setting the CPU registers.

This is a stop-gap measure in order to unblock OSSTest Windows 7 failures while a proper fix for the HVM CPU save/restore is being worked on.

I think it is better to say in the commit message that a proper fix is in place so we can revert the stop-gap patch instead of copying the commit message from the patch this is being reverted.

Assuming I'm right about a proper fix will be committed before this patch and the commit message fixed:

#12 lore raw

Re: [PATCH RFC 3/3] Revert "libxc: create an initial FPU state for HVM guests"

Quoted 11 lines · Wei Liu
Quoted 9 lines · Roger Pau Monne

This reverts commit d64dbbcc7c9934a46126c59d78536235908377ad.

Xen always set the FPU as initialized when loading a HVM context, so libxc
has to provide a valid FPU context when setting the CPU registers.

This is a stop-gap measure in order to unblock OSSTest Windows 7 failures
while a proper fix for the HVM CPU save/restore is being worked on.

Quoted 6 lines

I think it is better to say in the commit message that a proper fix is in place so we can revert the stop-gap patch instead of copying the commit message from the patch this is being reverted.

Assuming I'm right about a proper fix will be committed before this patch and the commit message fixed:

Roger, Is the "proper fix" in tree now? If so then please can we get an updated commit message which references that as Wei suggests.

I can update the message upon commit if you just want to provide the words, or feel free to resend of course.

Ian.

Quoted 2 lines
#13 lore raw

Re: [PATCH RFC 3/3] Revert "libxc: create an initial FPU state for HVM guests"

El 16/11/15 a les 12.51, Ian Campbell ha escrit:

Quoted 16 lines · Wei Liu
Quoted 15 lines · Roger Pau Monne
Quoted 9 lines

This reverts commit d64dbbcc7c9934a46126c59d78536235908377ad.

Xen always set the FPU as initialized when loading a HVM context, so libxc
has to provide a valid FPU context when setting the CPU registers.

This is a stop-gap measure in order to unblock OSSTest Windows 7 failures
while a proper fix for the HVM CPU save/restore is being worked on.

I think it is better to say in the commit message that a proper fix is in place so we can revert the stop-gap patch instead of copying the commit message from the patch this is being reverted.

Quoted 8 lines
Quoted 2 lines

Assuming I'm right about a proper fix will be committed before this patch and the commit message fixed:

Roger, Is the "proper fix" in tree now? If so then please can we get an updated commit message which references that as Wei suggests.

I can update the message upon commit if you just want to provide the words, or feel free to resend of course.

No, a proper fix is still not in place, sorry. Let me see if I can get a new version out today.

Roger.