added SIOCATMARK and times() syscall
[qemu] / op-i386.c
index f8188e3..4330378 100644 (file)
--- a/op-i386.c
+++ b/op-i386.c
@@ -3,19 +3,19 @@
  * 
  *  Copyright (c) 2003 Fabrice Bellard
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #include "exec-i386.h"
 
@@ -119,14 +119,6 @@ static inline int lshift(int x, int n)
         return x >> (-n);
 }
 
-/* exception support */
-/* NOTE: not static to force relocation generation by GCC */
-void raise_exception(int exception_index)
-{
-    env->exception_index = exception_index;
-    longjmp(env->jmp_env, 1);
-}
-
 /* we define the various pieces of code used by the JIT */
 
 #define REG EAX
@@ -177,7 +169,7 @@ void raise_exception(int exception_index)
 #undef REG
 #undef REGNAME
 
-/* operations */
+/* operations with flags */
 
 void OPPROTO op_addl_T0_T1_cc(void)
 {
@@ -217,11 +209,6 @@ void OPPROTO op_cmpl_T0_T1_cc(void)
     CC_DST = T0 - T1;
 }
 
-void OPPROTO op_notl_T0(void)
-{
-    T0 = ~T0;
-}
-
 void OPPROTO op_negl_T0_cc(void)
 {
     CC_SRC = 0;
@@ -248,6 +235,53 @@ void OPPROTO op_testl_T0_T1_cc(void)
     CC_DST = T0 & T1;
 }
 
+/* operations without flags */
+
+void OPPROTO op_addl_T0_T1(void)
+{
+    T0 += T1;
+}
+
+void OPPROTO op_orl_T0_T1(void)
+{
+    T0 |= T1;
+}
+
+void OPPROTO op_andl_T0_T1(void)
+{
+    T0 &= T1;
+}
+
+void OPPROTO op_subl_T0_T1(void)
+{
+    T0 -= T1;
+}
+
+void OPPROTO op_xorl_T0_T1(void)
+{
+    T0 ^= T1;
+}
+
+void OPPROTO op_negl_T0(void)
+{
+    T0 = -T0;
+}
+
+void OPPROTO op_incl_T0(void)
+{
+    T0++;
+}
+
+void OPPROTO op_decl_T0(void)
+{
+    T0--;
+}
+
+void OPPROTO op_notl_T0(void)
+{
+    T0 = ~T0;
+}
+
 void OPPROTO op_bswapl_T0(void)
 {
     T0 = bswap32(T0);
@@ -323,13 +357,15 @@ void OPPROTO op_imull_T0_T1(void)
 }
 
 /* division, flags are undefined */
-/* XXX: add exceptions for overflow & div by zero */
+/* XXX: add exceptions for overflow */
 void OPPROTO op_divb_AL_T0(void)
 {
     unsigned int num, den, q, r;
 
     num = (EAX & 0xffff);
     den = (T0 & 0xff);
+    if (den == 0)
+        raise_exception(EXCP00_DIVZ);
     q = (num / den) & 0xff;
     r = (num % den) & 0xff;
     EAX = (EAX & 0xffff0000) | (r << 8) | q;
@@ -341,6 +377,8 @@ void OPPROTO op_idivb_AL_T0(void)
 
     num = (int16_t)EAX;
     den = (int8_t)T0;
+    if (den == 0)
+        raise_exception(EXCP00_DIVZ);
     q = (num / den) & 0xff;
     r = (num % den) & 0xff;
     EAX = (EAX & 0xffff0000) | (r << 8) | q;
@@ -352,6 +390,8 @@ void OPPROTO op_divw_AX_T0(void)
 
     num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
     den = (T0 & 0xffff);
+    if (den == 0)
+        raise_exception(EXCP00_DIVZ);
     q = (num / den) & 0xffff;
     r = (num % den) & 0xffff;
     EAX = (EAX & 0xffff0000) | q;
@@ -364,6 +404,8 @@ void OPPROTO op_idivw_AX_T0(void)
 
     num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
     den = (int16_t)T0;
+    if (den == 0)
+        raise_exception(EXCP00_DIVZ);
     q = (num / den) & 0xffff;
     r = (num % den) & 0xffff;
     EAX = (EAX & 0xffff0000) | q;
@@ -377,6 +419,8 @@ void OPPROTO op_divl_EAX_T0(void)
     
     num = EAX | ((uint64_t)EDX << 32);
     den = T0;
+    if (den == 0)
+        raise_exception(EXCP00_DIVZ);
     q = (num / den);
     r = (num % den);
     EAX = q;
@@ -390,24 +434,51 @@ void OPPROTO op_idivl_EAX_T0(void)
     
     num = EAX | ((uint64_t)EDX << 32);
     den = T0;
+    if (den == 0)
+        raise_exception(EXCP00_DIVZ);
     q = (num / den);
     r = (num % den);
     EAX = q;
     EDX = r;
 }
 
-/* constant load */
+/* constant load & misc op */
 
 void OPPROTO op_movl_T0_im(void)
 {
     T0 = PARAM1;
 }
 
+void OPPROTO op_addl_T0_im(void)
+{
+    T0 += PARAM1;
+}
+
+void OPPROTO op_andl_T0_ffff(void)
+{
+    T0 = T0 & 0xffff;
+}
+
+void OPPROTO op_movl_T0_T1(void)
+{
+    T0 = T1;
+}
+
 void OPPROTO op_movl_T1_im(void)
 {
     T1 = PARAM1;
 }
 
+void OPPROTO op_addl_T1_im(void)
+{
+    T1 += PARAM1;
+}
+
+void OPPROTO op_movl_T1_A0(void)
+{
+    T1 = A0;
+}
+
 void OPPROTO op_movl_A0_im(void)
 {
     A0 = PARAM1;
@@ -418,6 +489,11 @@ void OPPROTO op_addl_A0_im(void)
     A0 += PARAM1;
 }
 
+void OPPROTO op_addl_A0_AL(void)
+{
+    A0 += (EAX & 0xff);
+}
+
 void OPPROTO op_andl_A0_ffff(void)
 {
     A0 = A0 & 0xffff;
@@ -506,23 +582,23 @@ void OPPROTO op_add_bitl_A0_T1(void)
 
 void OPPROTO op_jmp_T0(void)
 {
-    PC = T0;
+    EIP = T0;
 }
 
 void OPPROTO op_jmp_im(void)
 {
-    PC = PARAM1;
+    EIP = PARAM1;
 }
 
 void OPPROTO op_int_im(void)
 {
-    PC = PARAM1;
+    EIP = PARAM1;
     raise_exception(EXCP0D_GPF);
 }
 
 void OPPROTO op_int3(void)
 {
-    PC = PARAM1;
+    EIP = PARAM1;
     raise_exception(EXCP03_INT3);
 }
 
@@ -531,11 +607,78 @@ void OPPROTO op_into(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (eflags & CC_O) {
-        PC = PARAM1;
         raise_exception(EXCP04_INTO);
+    }
+}
+
+/* XXX: add IOPL/CPL tests */
+void OPPROTO op_cli(void)
+{
+    raise_exception(EXCP0D_GPF);
+}
+
+/* XXX: add IOPL/CPL tests */
+void OPPROTO op_sti(void)
+{
+    raise_exception(EXCP0D_GPF);
+}
+
+/* vm86plus instructions */
+
+void OPPROTO op_cli_vm(void)
+{
+    env->eflags &= ~VIF_MASK;
+}
+
+void OPPROTO op_sti_vm(void)
+{
+    env->eflags |= VIF_MASK;
+    if (env->eflags & VIP_MASK) {
+        EIP = PARAM1;
+        raise_exception(EXCP0D_GPF);
+    }
+    FORCE_RET();
+}
+
+void OPPROTO op_boundw(void)
+{
+    int low, high, v;
+    low = ldsw((uint8_t *)A0);
+    high = ldsw((uint8_t *)A0 + 2);
+    v = (int16_t)T0;
+    if (v < low || v > high)
+        raise_exception(EXCP05_BOUND);
+    FORCE_RET();
+}
+
+void OPPROTO op_boundl(void)
+{
+    int low, high, v;
+    low = ldl((uint8_t *)A0);
+    high = ldl((uint8_t *)A0 + 4);
+    v = T0;
+    if (v < low || v > high)
+        raise_exception(EXCP05_BOUND);
+    FORCE_RET();
+}
+
+void OPPROTO op_cmpxchg8b(void)
+{
+    uint64_t d;
+    int eflags;
+
+    eflags = cc_table[CC_OP].compute_all();
+    d = ldq((uint8_t *)A0);
+    if (d == (((uint64_t)EDX << 32) | EAX)) {
+        stq((uint8_t *)A0, ((uint64_t)ECX << 32) | EBX);
+        eflags |= CC_Z;
     } else {
-        PC = PARAM2;
+        EDX = d >> 32;
+        EAX = d;
+        eflags &= ~CC_Z;
     }
+    CC_SRC = eflags;
+    FORCE_RET();
 }
 
 /* string ops */
@@ -597,7 +740,6 @@ void OPPROTO op_movswl_DX_AX(void)
 }
 
 /* push/pop */
-/* XXX: add 16 bit operand/16 bit seg variants */
 
 void op_pushl_T0(void)
 {
@@ -608,114 +750,118 @@ void op_pushl_T0(void)
     ESP = offset;
 }
 
-void op_pushl_T1(void)
+void op_pushw_T0(void)
+{
+    uint32_t offset;
+    offset = ESP - 2;
+    stw((void *)offset, T0);
+    /* modify ESP after to handle exceptions correctly */
+    ESP = offset;
+}
+
+void op_pushl_ss32_T0(void)
 {
     uint32_t offset;
     offset = ESP - 4;
-    stl((void *)offset, T1);
+    stl(env->seg_cache[R_SS].base + offset, T0);
     /* modify ESP after to handle exceptions correctly */
     ESP = offset;
 }
 
+void op_pushw_ss32_T0(void)
+{
+    uint32_t offset;
+    offset = ESP - 2;
+    stw(env->seg_cache[R_SS].base + offset, T0);
+    /* modify ESP after to handle exceptions correctly */
+    ESP = offset;
+}
+
+void op_pushl_ss16_T0(void)
+{
+    uint32_t offset;
+    offset = (ESP - 4) & 0xffff;
+    stl(env->seg_cache[R_SS].base + offset, T0);
+    /* modify ESP after to handle exceptions correctly */
+    ESP = (ESP & ~0xffff) | offset;
+}
+
+void op_pushw_ss16_T0(void)
+{
+    uint32_t offset;
+    offset = (ESP - 2) & 0xffff;
+    stw(env->seg_cache[R_SS].base + offset, T0);
+    /* modify ESP after to handle exceptions correctly */
+    ESP = (ESP & ~0xffff) | offset;
+}
+
+/* NOTE: ESP update is done after */
 void op_popl_T0(void)
 {
     T0 = ldl((void *)ESP);
+}
+
+void op_popw_T0(void)
+{
+    T0 = lduw((void *)ESP);
+}
+
+void op_popl_ss32_T0(void)
+{
+    T0 = ldl(env->seg_cache[R_SS].base + ESP);
+}
+
+void op_popw_ss32_T0(void)
+{
+    T0 = lduw(env->seg_cache[R_SS].base + ESP);
+}
+
+void op_popl_ss16_T0(void)
+{
+    T0 = ldl(env->seg_cache[R_SS].base + (ESP & 0xffff));
+}
+
+void op_popw_ss16_T0(void)
+{
+    T0 = lduw(env->seg_cache[R_SS].base + (ESP & 0xffff));
+}
+
+void op_addl_ESP_4(void)
+{
     ESP += 4;
 }
 
+void op_addl_ESP_2(void)
+{
+    ESP += 2;
+}
+
+void op_addw_ESP_4(void)
+{
+    ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
+}
+
+void op_addw_ESP_2(void)
+{
+    ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
+}
+
 void op_addl_ESP_im(void)
 {
     ESP += PARAM1;
 }
 
-void op_pushal(void)
-{
-    uint8_t *sp;
-    sp = (void *)(ESP - 32);
-    stl(sp, EDI);
-    stl(sp + 4, ESI);
-    stl(sp + 8, EBP);
-    stl(sp + 12, ESP);
-    stl(sp + 16, EBX);
-    stl(sp + 20, EDX);
-    stl(sp + 24, ECX);
-    stl(sp + 28, EAX);
-    ESP = (unsigned long)sp;
-}
-
-void op_pushaw(void)
-{
-    uint8_t *sp;
-    sp = (void *)(ESP - 16);
-    stw(sp, EDI);
-    stw(sp + 2, ESI);
-    stw(sp + 4, EBP);
-    stw(sp + 6, ESP);
-    stw(sp + 8, EBX);
-    stw(sp + 10, EDX);
-    stw(sp + 12, ECX);
-    stw(sp + 14, EAX);
-    ESP = (unsigned long)sp;
-}
-
-void op_popal(void)
-{
-    uint8_t *sp;
-    sp = (void *)ESP;
-    EDI = ldl(sp);
-    ESI = ldl(sp + 4);
-    EBP = ldl(sp + 8);
-    EBX = ldl(sp + 16);
-    EDX = ldl(sp + 20);
-    ECX = ldl(sp + 24);
-    EAX = ldl(sp + 28);
-    ESP = (unsigned long)sp + 32;
-}
-
-void op_popaw(void)
-{
-    uint8_t *sp;
-    sp = (void *)ESP;
-    EDI = ldl(sp);
-    ESI = ldl(sp + 2);
-    EBP = ldl(sp + 4);
-    EBX = ldl(sp + 8);
-    EDX = ldl(sp + 10);
-    ECX = ldl(sp + 12);
-    EAX = ldl(sp + 14);
-    ESP = (unsigned long)sp + 16;
-}
-
-void op_enterl(void)
-{
-    unsigned int bp, frame_temp, level;
-    uint8_t *sp;
-
-    sp = (void *)ESP;
-    bp = EBP;
-    sp -= 4;
-    stl(sp, bp);
-    frame_temp = (unsigned int)sp;
-    level = PARAM2;
-    if (level) {
-        while (level--) {
-            bp -= 4; 
-            sp -= 4;
-            stl(sp, bp);
-        }
-        sp -= 4;
-        stl(sp, frame_temp);
-    }
-    EBP = frame_temp;
-    sp -= PARAM1;
-    ESP = (int)sp;
+void op_addw_ESP_im(void)
+{
+    ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
 }
 
 /* rdtsc */
 #ifndef __i386__
 uint64_t emu_time;
 #endif
-void op_rdtsc(void)
+
+void OPPROTO op_rdtsc(void)
 {
     uint64_t val;
 #ifdef __i386__
@@ -728,6 +874,51 @@ void op_rdtsc(void)
     EDX = val >> 32;
 }
 
+/* We simulate a pre-MMX pentium as in valgrind */
+#define CPUID_FP87 (1 << 0)
+#define CPUID_VME  (1 << 1)
+#define CPUID_DE   (1 << 2)
+#define CPUID_PSE  (1 << 3)
+#define CPUID_TSC  (1 << 4)
+#define CPUID_MSR  (1 << 5)
+#define CPUID_PAE  (1 << 6)
+#define CPUID_MCE  (1 << 7)
+#define CPUID_CX8  (1 << 8)
+#define CPUID_APIC (1 << 9)
+#define CPUID_SEP  (1 << 11) /* sysenter/sysexit */
+#define CPUID_MTRR (1 << 12)
+#define CPUID_PGE  (1 << 13)
+#define CPUID_MCA  (1 << 14)
+#define CPUID_CMOV (1 << 15)
+/* ... */
+#define CPUID_MMX  (1 << 23)
+#define CPUID_FXSR (1 << 24)
+#define CPUID_SSE  (1 << 25)
+#define CPUID_SSE2 (1 << 26)
+
+void helper_cpuid(void)
+{
+    if (EAX == 0) {
+        EAX = 1; /* max EAX index supported */
+        EBX = 0x756e6547;
+        ECX = 0x6c65746e;
+        EDX = 0x49656e69;
+    } else {
+        /* EAX = 1 info */
+        EAX = 0x52b;
+        EBX = 0;
+        ECX = 0;
+        EDX = CPUID_FP87 | CPUID_VME | CPUID_DE | CPUID_PSE |
+            CPUID_TSC | CPUID_MSR | CPUID_MCE |
+            CPUID_CX8;
+    }
+}
+
+void OPPROTO op_cpuid(void)
+{
+    helper_cpuid();
+}
+
 /* bcd */
 
 /* XXX: exception */
@@ -860,6 +1051,7 @@ void OPPROTO op_das(void)
 
 /* segment handling */
 
+/* XXX: use static VM86 information */
 void load_seg(int seg_reg, int selector)
 {
     SegmentCache *sc;
@@ -870,7 +1062,7 @@ void load_seg(int seg_reg, int selector)
 
     env->segs[seg_reg] = selector;
     sc = &env->seg_cache[seg_reg];
-    if (env->vm86) {
+    if (env->eflags & VM_MASK) {
         sc->base = (void *)(selector << 4);
         sc->limit = 0xffff;
         sc->seg_32bit = 0;
@@ -907,6 +1099,11 @@ void OPPROTO op_movl_T0_seg(void)
     T0 = env->segs[PARAM1];
 }
 
+void OPPROTO op_movl_A0_seg(void)
+{
+    A0 = *(unsigned long *)((char *)env + PARAM1);
+}
+
 void OPPROTO op_addl_A0_seg(void)
 {
     A0 += *(unsigned long *)((char *)env + PARAM1);
@@ -920,18 +1117,18 @@ void OPPROTO op_jo_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (eflags & CC_O)
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
 void OPPROTO op_jb_cc(void)
 {
     if (cc_table[CC_OP].compute_c())
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -940,9 +1137,9 @@ void OPPROTO op_jz_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (eflags & CC_Z)
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -951,9 +1148,9 @@ void OPPROTO op_jbe_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (eflags & (CC_Z | CC_C))
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -962,9 +1159,9 @@ void OPPROTO op_js_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (eflags & CC_S)
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -973,9 +1170,9 @@ void OPPROTO op_jp_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (eflags & CC_P)
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -984,9 +1181,9 @@ void OPPROTO op_jl_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if ((eflags ^ (eflags >> 4)) & 0x80)
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -995,9 +1192,9 @@ void OPPROTO op_jle_cc(void)
     int eflags;
     eflags = cc_table[CC_OP].compute_all();
     if (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z))
-        PC = PARAM1;
+        EIP = PARAM1;
     else
-        PC = PARAM2;
+        EIP = PARAM2;
     FORCE_RET();
 }
 
@@ -1066,10 +1263,66 @@ void OPPROTO op_set_cc_op(void)
     CC_OP = PARAM1;
 }
 
+#define FL_UPDATE_MASK32 (TF_MASK | AC_MASK | ID_MASK)
+#define FL_UPDATE_MASK16 (TF_MASK)
+
 void OPPROTO op_movl_eflags_T0(void)
 {
-    CC_SRC = T0;
-    DF = 1 - (2 * ((T0 >> 10) & 1));
+    int eflags;
+    eflags = T0;
+    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
+    DF = 1 - (2 * ((eflags >> 10) & 1));
+    /* we also update some system flags as in user mode */
+    env->eflags = (env->eflags & ~FL_UPDATE_MASK32) | (eflags & FL_UPDATE_MASK32);
+}
+
+void OPPROTO op_movw_eflags_T0(void)
+{
+    int eflags;
+    eflags = T0;
+    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
+    DF = 1 - (2 * ((eflags >> 10) & 1));
+    /* we also update some system flags as in user mode */
+    env->eflags = (env->eflags & ~FL_UPDATE_MASK16) | (eflags & FL_UPDATE_MASK16);
+}
+
+/* vm86 version */
+void OPPROTO op_movw_eflags_T0_vm(void)
+{
+    int eflags;
+    eflags = T0;
+    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
+    DF = 1 - (2 * ((eflags >> 10) & 1));
+    /* we also update some system flags as in user mode */
+    env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
+        (eflags & FL_UPDATE_MASK16);
+    if (eflags & IF_MASK) {
+        env->eflags |= VIF_MASK;
+        if (env->eflags & VIP_MASK) {
+            EIP = PARAM1;
+            raise_exception(EXCP0D_GPF);
+        }
+    }
+    FORCE_RET();
+}
+
+void OPPROTO op_movl_eflags_T0_vm(void)
+{
+    int eflags;
+    eflags = T0;
+    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
+    DF = 1 - (2 * ((eflags >> 10) & 1));
+    /* we also update some system flags as in user mode */
+    env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
+        (eflags & FL_UPDATE_MASK32);
+    if (eflags & IF_MASK) {
+        env->eflags |= VIF_MASK;
+        if (env->eflags & VIP_MASK) {
+            EIP = PARAM1;
+            raise_exception(EXCP0D_GPF);
+        }
+    }
+    FORCE_RET();
 }
 
 /* XXX: compute only O flag */
@@ -1077,13 +1330,28 @@ void OPPROTO op_movb_eflags_T0(void)
 {
     int of;
     of = cc_table[CC_OP].compute_all() & CC_O;
-    CC_SRC = T0 | of;
+    CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
 }
 
 void OPPROTO op_movl_T0_eflags(void)
 {
-    T0 = cc_table[CC_OP].compute_all();
-    T0 |= (DF & DIRECTION_FLAG);
+    int eflags;
+    eflags = cc_table[CC_OP].compute_all();
+    eflags |= (DF & DF_MASK);
+    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
+    T0 = eflags;
+}
+
+/* vm86 version */
+void OPPROTO op_movl_T0_eflags_vm(void)
+{
+    int eflags;
+    eflags = cc_table[CC_OP].compute_all();
+    eflags |= (DF & DF_MASK);
+    eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
+    if (env->eflags & VIF_MASK)
+        eflags |= IF_MASK;
+    T0 = eflags;
 }
 
 void OPPROTO op_cld(void)
@@ -1200,7 +1468,9 @@ CCTable cc_table[CC_OP_NB] = {
     [CC_OP_SARL] = { compute_all_sarl, compute_c_shll },
 };
 
-/* floating point support */
+/* floating point support. Some of the code for complicated x87
+   functions comes from the LGPL'ed x86 emulator found in the Willows
+   TWIN windows emulator. */
 
 #ifdef USE_X86LDOUBLE
 /* use long double functions */
@@ -1299,6 +1569,41 @@ void OPPROTO op_fldl_FT0_A0(void)
     FT0 = ldfq((void *)A0);
 }
 
+/* helpers are needed to avoid static constant reference. XXX: find a better way */
+#ifdef USE_INT_TO_FLOAT_HELPERS
+
+void helper_fild_FT0_A0(void)
+{
+    FT0 = (CPU86_LDouble)ldsw((void *)A0);
+}
+
+void helper_fildl_FT0_A0(void)
+{
+    FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
+}
+
+void helper_fildll_FT0_A0(void)
+{
+    FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
+}
+
+void OPPROTO op_fild_FT0_A0(void)
+{
+    helper_fild_FT0_A0();
+}
+
+void OPPROTO op_fildl_FT0_A0(void)
+{
+    helper_fildl_FT0_A0();
+}
+
+void OPPROTO op_fildll_FT0_A0(void)
+{
+    helper_fildll_FT0_A0();
+}
+
+#else
+
 void OPPROTO op_fild_FT0_A0(void)
 {
     FT0 = (CPU86_LDouble)ldsw((void *)A0);
@@ -1313,6 +1618,7 @@ void OPPROTO op_fildll_FT0_A0(void)
 {
     FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
 }
+#endif
 
 /* fp load ST0 */
 
@@ -1351,6 +1657,41 @@ void OPPROTO op_fldt_ST0_A0(void)
 }
 #endif
 
+/* helpers are needed to avoid static constant reference. XXX: find a better way */
+#ifdef USE_INT_TO_FLOAT_HELPERS
+
+void helper_fild_ST0_A0(void)
+{
+    ST0 = (CPU86_LDouble)ldsw((void *)A0);
+}
+
+void helper_fildl_ST0_A0(void)
+{
+    ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
+}
+
+void helper_fildll_ST0_A0(void)
+{
+    ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
+}
+
+void OPPROTO op_fild_ST0_A0(void)
+{
+    helper_fild_ST0_A0();
+}
+
+void OPPROTO op_fildl_ST0_A0(void)
+{
+    helper_fildl_ST0_A0();
+}
+
+void OPPROTO op_fildll_ST0_A0(void)
+{
+    helper_fildll_ST0_A0();
+}
+
+#else
+
 void OPPROTO op_fild_ST0_A0(void)
 {
     ST0 = (CPU86_LDouble)ldsw((void *)A0);
@@ -1366,6 +1707,8 @@ void OPPROTO op_fildll_ST0_A0(void)
     ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
 }
 
+#endif
+
 /* fp store */
 
 void OPPROTO op_fsts_ST0_A0(void)
@@ -2104,3 +2447,14 @@ void OPPROTO op_fninit(void)
     env->fptags[6] = 1;
     env->fptags[7] = 1;
 }
+
+/* threading support */
+void OPPROTO op_lock(void)
+{
+    cpu_lock();
+}
+
+void OPPROTO op_unlock(void)
+{
+    cpu_unlock();
+}