Skip to contentSkip to conversation

More fp instructions for realmode emulation (Enables booting OS/2 as a HVM guest on Intel/VT hardware)

RFC PATCH In review Trolle Selander 3 participants 11 messages Feb 29, 2008 – Mar 4, 2008

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 More fp instructions for realmode emulation (Enables booting OS/2 as a HVM guest on Intel/VT hardware)

This patch adds a number of fp instructions needed for OS/2 to boot as a HVM guest on Intel/VT hardware. It appears to work fine, and OS/2 is now finally working on Intel/VT as well as AMD/SVM.

I'm a little concerned about the "correctness" of the FSTSW emulation and the use of inline assembly directly using the corresponding ops for emulation. Wrt FSTSW, it is really two ops FNSTSW immediately preceeded by an FWAIT. I've left FWAIT defined, but a no-op because after looking at realmode.c it seems in the emulation, exceptions are always processed if a call to x86_emulate returns an exception, so there should be no way for there to be "pending" exceptions for FWAIT to wait for. I may have missed something, though, hence the RFC.

Secondly, about the inline assembly - i saw that this was how many of the other FP instructions were done, so I did it that way as well. However, none of the previous instructions done that way referenced the fp registers. Does this make a difference, or is it ok the way i've done it here?

xen/arch/x86/x86_emulate.c +56 -8
diff -uNr xen-unstable.hg/xen/arch/x86/x86_emulate.c xen-unstable.hg-patched/xen/arch/x86/x86_emulate.c
@@ -127,7 +127,7 @@
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     /* 0x98 - 0x9F */
-    ImplicitOps, ImplicitOps, ImplicitOps, 0,
+    ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     /* 0xA0 - 0xA7 */
     ByteOp|ImplicitOps|Mov, ImplicitOps|Mov,
@@ -160,7 +160,7 @@
     ByteOp|DstMem|SrcImplicit|ModRM, DstMem|SrcImplicit|ModRM, 
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     /* 0xD8 - 0xDF */
-    0, ImplicitOps|ModRM, 0, ImplicitOps|ModRM, 0, ImplicitOps|ModRM, 0, 0,
+    0, ImplicitOps|ModRM, 0, ImplicitOps|ModRM, 0, ImplicitOps|ModRM, ImplicitOps|ModRM, ImplicitOps|ModRM,
     /* 0xE0 - 0xE7 */
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
@@ -2355,6 +2355,10 @@
         break;
     }
 
+    case 0x9b:  /* wait/fwait */
+        /* No-op */
+        break;
+
     case 0x9c: /* pushf */
         src.val = _regs.eflags;
         goto push;
@@ -2670,12 +2674,28 @@
     case 0xd9: /* FPU 0xd9 */
         fail_if(ops->load_fpu_ctxt == NULL);
         ops->load_fpu_ctxt(ctxt);
-        fail_if((modrm_reg & 7) != 7);
-        fail_if(modrm >= 0xc0);
-        /* fnstcw m2byte */
-        ea.bytes = 2;
-        dst = ea;
-        asm volatile ( "fnstcw %0" : "=m" (dst.val) );
+        switch ( modrm )
+        {
+        case 0xc0:
+             asm volatile ( "fld %st(0)" );
+             break;
+        case 0xe0:
+             asm volatile ( "fchs" );
+             break;
+        case 0xe8:
+             asm volatile ( "fld1" );
+             break;
+        case 0xee:
+             asm volatile ( "fldz" );
+             break;
+        default:
+            fail_if((modrm_reg & 7) != 7);
+            fail_if(modrm >= 0xc0);
+            /* fnstcw m2byte */
+            ea.bytes = 2;
+            dst = ea;
+            asm volatile ( "fnstcw %0" : "=m" (dst.val) );
+        }
         break;
 
     case 0xdb: /* FPU 0xdb */
@@ -2697,6 +2717,34 @@
         asm volatile ( "fnstsw %0" : "=m" (dst.val) );
         break;
 
+    case 0xde: /* FPU 0xde */
+        fail_if(ops->load_fpu_ctxt == NULL);
+        ops->load_fpu_ctxt(ctxt);
+        switch ( modrm )
+        {
+        case 0xd9:
+             asm volatile ( "fcompp" );
+             break;
+        case 0xf9:
+             asm volatile ( "fdivp %st(1),%st" );
+             break;
+        default:
+            goto cannot_emulate;
+        }
+        break;
+
+
+    case 0xdf: /* FPU 0xdf */
+        fail_if(ops->load_fpu_ctxt == NULL);
+        ops->load_fpu_ctxt(ctxt);
+        fail_if(modrm != 0xe0);
+        /* fnstsw %ax */
+        dst.bytes = 2;
+        dst.type = OP_REG;
+        dst.reg = (unsigned long *)&_regs.eax;
+        asm volatile ( "fnstsw %0" : "=m" (dst.val) );
+        break;
+
     case 0xe0 ... 0xe2: /* loop{,z,nz} */ {
         int rel = insn_fetch_type(int8_t);
         int do_jmp = !(_regs.eflags & EFLG_ZF); /* loopnz */

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

#1 lore raw

Regarding point 1: Don¹t FPU instructions that raise an exception only log that in the status word, to be picked up by the next FPU instruction which checks for exceptions? It strikes me that we can have previously loaded up a status word that requires an exception to occur on FWAIT, in which case semantics are broken. More worryingly, if any of the instructions you have added can cause an exception, we will crash Xen because you have not added fixup handling. Unfortunately I¹m not really up on exactly how FPU exception handling works: are you?

Regarding point 2: Yes, direct use of the FPU instructions is fine as we already loaded guest FPU context.

-- Keir

Quoted 9 lines · Trolle Selander

I'm a little concerned about the "correctness" of the FSTSW emulation and the use of inline assembly directly using the corresponding ops for emulation. Wrt FSTSW, it is really two ops FNSTSW immediately preceeded by an FWAIT. I've left FWAIT defined, but a no-op because after looking at realmode.c it seems in the emulation, exceptions are always processed if a call to x86_emulate returns an exception, so there should be no way for there to be "pending" exceptions for FWAIT to wait for. I may have missed something, though, hence the RFC.

Quoted 4 lines

Secondly, about the inline assembly - i saw that this was how many of the other FP instructions were done, so I did it that way as well. However, none of the previous instructions done that way referenced the fp registers. Does this make a difference, or is it ok the way i've done it here?

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

#2 lore raw

Intel SDM Volume 1 Section 8.6 (³X87 FPU Exception Synchronization²) is very helpful in this regard. It confirms my suspicions, that we will need to wrap the FPU inline asms in some fixup glue. The only exceptions to this rule are the FN* (no-wait) instructions. Properly speaking we should also check for unmasked exceptions becoming pending after any FPU instruction and set the faulting CS:IP in the FPU context block appropriately. But life is perhaps too short. :-)

-- Keir

Quoted 8 lines · Keir Fraser

Regarding point 1: Don¹t FPU instructions that raise an exception only log that in the status word, to be picked up by the next FPU instruction which checks for exceptions? It strikes me that we can have previously loaded up a status word that requires an exception to occur on FWAIT, in which case semantics are broken. More worryingly, if any of the instructions you have added can cause an exception, we will crash Xen because you have not added fixup handling. Unfortunately I¹m not really up on exactly how FPU exception handling works: are you?

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

#3 lore raw

That's what I was worried about. I honestly had zilch experience working with assembler-level x87 stuff before Friday afternoon when I whipped up this patch, mainly to see if the fp instructions were all that was missing to get OS/2 on VT to work. I'll see if I can find time to make an exception-safe version of this sometime this week. I'm not worrying about life being too short, but rather that March 2008 is too dense... ;)

Quoted 12 lines · Keir Fraser

Intel SDM Volume 1 Section 8.6 ("X87 FPU Exception Synchronization") is
very helpful in this regard. It confirms my suspicions, that we will need to wrap the FPU inline asms in some fixup glue. The only exceptions to this rule are the FN* (no-wait) instructions. Properly speaking we should also check for unmasked exceptions becoming pending after any FPU instruction and set the faulting CS:IP in the FPU context block appropriately. But life is perhaps too short. :-)

-- Keir

On 1/3/08 08:46, "Keir Fraser" <[email protected]> wrote:

Quoted 11 lines

Regarding point 1: Don't FPU instructions that raise an exception only log that in the status word, to be picked up by the next FPU instruction which checks for exceptions? It strikes me that we can have previously loaded up a status word that requires an exception to occur on FWAIT, in which case semantics are broken. More worryingly, if *any* of the instructions you have added can cause an exception, we will crash Xen because you have not added fixup handling. Unfortunately I'm not really up on exactly how FPU exception handling works: are you?

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

#4 lore raw

I¹ll have some time to do it this afternoon.

-- Keir

Quoted 7 lines · Trolle Selander

That's what I was worried about. I honestly had zilch experience working with assembler-level x87 stuff before Friday afternoon when I whipped up this patch, mainly to see if the fp instructions were all that was missing to get OS/2 on VT to work. I'll see if I can find time to make an exception-safe version of this sometime this week. I'm not worrying about life being too short, but rather that March 2008 is too dense... ;)

Quoted 26 lines
Quoted 22 lines · Keir Fraser

Intel SDM Volume 1 Section 8.6 ("X87 FPU Exception Synchronization") is very helpful in this regard. It confirms my suspicions, that we will need to wrap the FPU inline asms in some fixup glue. The only exceptions to this rule are the FN* (no-wait) instructions. Properly speaking we should also check for unmasked exceptions becoming pending after any FPU instruction and set the faulting CS:IP in the FPU context block appropriately. But life is perhaps too short. :-)

-- Keir

Quoted 8 lines · Keir Fraser

Regarding point 1: Don't FPU instructions that raise an exception only log that in the status word, to be picked up by the next FPU instruction which checks for exceptions? It strikes me that we can have previously loaded up a status word that requires an exception to occur on FWAIT, in which case semantics are broken. More worryingly, if any of the instructions you have added can cause an exception, we will crash Xen because you have not added fixup handling. Unfortunately I'm not really up on exactly how FPU exception handling works: are you?

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

#5 lore raw
In reply to Keir Fraser
Quoted 3 lines · Keir Fraser

Intel SDM Volume 1 Section 8.6 (?X87 FPU Exception Synchronization?) is very helpful in this regard. It confirms my suspicions, that we will need to wrap the FPU inline asms in some fixup glue. The only exceptions to this rule are

Yes you definitely need to. That was a kernel crash bug on native Linux some time ago...

Also you need to do the same for any FXSAVEs/FXRSTORs if you haven't already.

-Andi

#6 lore raw
Quoted 8 lines · Andi Kleen
Quoted 3 lines

Intel SDM Volume 1 Section 8.6 (?X87 FPU Exception Synchronization?) is very helpful in this regard. It confirms my suspicions, that we will need to wrap the FPU inline asms in some fixup glue. The only exceptions to this rule are

Yes you definitely need to. That was a kernel crash bug on native Linux some time ago...

Also you need to do the same for any FXSAVEs/FXRSTORs if you haven't already.

FXSAVE/FXRSTOR do not flush pending FPU exceptions, right?

-- Keir

#7 lore raw
Quoted 6 lines · Keir Fraser
Quoted 4 lines · Andi Kleen
Quoted 3 lines

Intel SDM Volume 1 Section 8.6 (?X87 FPU Exception Synchronization?) is very helpful in this regard. It confirms my suspicions, that we will need to wrap the FPU inline asms in some fixup glue. The only exceptions to this rule are

Quoted 6 lines
Quoted 4 lines

Yes you definitely need to. That was a kernel crash bug on native Linux some time ago...

Also you need to do the same for any FXSAVEs/FXRSTORs if you haven't already.

FXSAVE/FXRSTOR do not flush pending FPU exceptions, right?

Yes they do not, but there are other ways to make them #GP

-Andi

#8 lore raw
Quoted 9 lines · Andi Kleen
Quoted 7 lines
Quoted 5 lines

Yes you definitely need to. That was a kernel crash bug on native Linux some time ago...

Also you need to do the same for any FXSAVEs/FXRSTORs if you haven't already.

FXSAVE/FXRSTOR do not flush pending FPU exceptions, right?

Yes they do not, but there are other ways to make them #GP

We don't trust FXRSTOR but we do trust FXSAVE. The AMD manual says that FXRSTOR will #GP if 1s are written to MXCSR reserved bits, but I take this to be a cut-and-paste typo since FXSAVE does not write to MXCSR.

If there's real evidence of FXSAVE causing #GP dependent on current FPU state, I'd be very happy to find out about it!

-- Keir

#9 lore raw
Quoted 6 lines

We don't trust FXRSTOR but we do trust FXSAVE. The AMD manual says that FXRSTOR will #GP if 1s are written to MXCSR reserved bits, but I take this to be a cut-and-paste typo since FXSAVE does not write to MXCSR.

If there's real evidence of FXSAVE causing #GP dependent on current FPU state, I'd be very happy to find out about it!

Sorry, double checked now the Intel docs. First I admit I don't quite remember why it was originally done in Linux, but they both use exception handlers.

The Intel docs say that #MF can happen for a pending FPU exception in 64bit mode.

-Andi

#10 lore raw
In reply to Trolle Selander

Annoying fact #2 about the FPU instructions is that the GNU assembler reverses the operand order of many two-operand FPU instructions. So, for example, FDIVP becomes DIVRP, and vice versa. Super annoying. So I¹m going to change our emulated opcodes for most FPU instructions to be ³.byte xx,yy². Personally I think this is as clear as using the mnemonic opcode, or perhaps clearer since it is now obvious that we are executing the exact instruction that we have just decoded.

-- Keir

Quoted 18 lines · Trolle Selander

This patch adds a number of fp instructions needed for OS/2 to boot as a HVM guest on Intel/VT hardware. It appears to work fine, and OS/2 is now finally working on Intel/VT as well as AMD/SVM.

I'm a little concerned about the "correctness" of the FSTSW emulation and the use of inline assembly directly using the corresponding ops for emulation. Wrt FSTSW, it is really two ops FNSTSW immediately preceeded by an FWAIT. I've left FWAIT defined, but a no-op because after looking at realmode.c it seems in the emulation, exceptions are always processed if a call to x86_emulate returns an exception, so there should be no way for there to be "pending" exceptions for FWAIT to wait for. I may have missed something, though, hence the RFC.

Secondly, about the inline assembly - i saw that this was how many of the other FP instructions were done, so I did it that way as well. However, none of the previous instructions done that way referenced the fp registers. Does this make a difference, or is it ok the way i've done it here?

Quoted 7 lines

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

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