- Finish the work around for the Pentium cmpxchg8b lock errata: We detected the errat...
[reactos.git] / reactos / ntoskrnl / ke / i386 / trap.s
index 9ec54ba..c8458c3 100644 (file)
@@ -80,7 +80,8 @@ GENERATE_IDT_STUBS                  /* INT 30-FF: UNEXPECTED INTERRUPTS     */
 
 .globl _KiIdtDescriptor
 _KiIdtDescriptor:
-    .short 0x800
+    .short 0
+    .short 0x7FF
     .long _KiIdt
 
 .globl _KiUnexpectedEntrySize
@@ -93,32 +94,64 @@ _UnexpectedMsg:
 _UnhandledMsg:
     .asciz "\n\x7\x7!!! Unhandled or Unexpected Code at line: %lx!!!\n"
 
+_IsrTimeoutMsg:
+    .asciz "\n*** ISR at %lx took over .5 second\n"
+
+_IsrOverflowMsg:
+    .asciz "\n*** ISR at %lx appears to have an interrupt storm\n"
+
+_KiTrapPrefixTable:
+    .byte 0xF2                      /* REP                                  */
+    .byte 0xF3                      /* REP INS/OUTS                         */
+    .byte 0x67                      /* ADDR                                 */
+    .byte 0xF0                      /* LOCK                                 */
+    .byte 0x66                      /* OP                                   */
+    .byte 0x2E                      /* SEG                                  */
+    .byte 0x3E                      /* DS                                   */
+    .byte 0x26                      /* ES                                   */
+    .byte 0x64                      /* FS                                   */
+    .byte 0x65                      /* GS                                   */
+    .byte 0x36                      /* SS                                   */
+
+_KiTrapIoTable:
+    .byte 0xE4                      /* IN                                   */
+    .byte 0xE5                      /* IN                                   */
+    .byte 0xEC                      /* IN                                   */
+    .byte 0xED                      /* IN                                   */
+    .byte 0x6C                      /* INS                                  */
+    .byte 0x6D                      /* INS                                  */
+    .byte 0xE6                      /* OUT                                  */
+    .byte 0xE7                      /* OUT                                  */
+    .byte 0xEE                      /* OUT                                  */
+    .byte 0xEF                      /* OUT                                  */
+    .byte 0x6E                      /* OUTS                                 */
+    .byte 0x6F                      /* OUTS                                 */
+
 /* SOFTWARE INTERRUPT SERVICES ***********************************************/
 .text
 
 _KiGetTickCount:
 _KiCallbackReturn:
-_KiRaiseAssertion:
     /* FIXME: TODO */
     UNHANDLED_PATH
 
 .func KiSystemService
-Dr_kss: DR_TRAP_FIXUP
+TRAP_FIXUPS kss_a, kss_t, DoNotFixupV86, DoNotFixupAbios
 _KiSystemService:
 
     /* Enter the shared system call prolog */
-    SYSCALL_PROLOG kss
+    SYSCALL_PROLOG kss_a, kss_t
 
     /* Jump to the actual handler */
     jmp SharedCode
 .endfunc
 
 .func KiFastCallEntry
-Dr_FastCallDrSave: DR_TRAP_FIXUP
+TRAP_FIXUPS FastCallDrSave, FastCallDrReturn, DoNotFixupV86, DoNotFixupAbios
 _KiFastCallEntry:
 
     /* Enter the fast system call prolog */
-    FASTCALL_PROLOG FastCallDrSave
+    FASTCALL_PROLOG FastCallDrSave, FastCallDrReturn
 
 SharedCode:
 
@@ -147,7 +180,7 @@ SharedCode:
     jnz NotWin32K
 
     /* Get the TEB */
-    mov ecx, [fs:KPCR_TEB]
+    mov ecx, PCR[KPCR_TEB]
 
     /* Check if we should flush the User Batch */
     xor ebx, ebx
@@ -158,15 +191,15 @@ ReadBatch:
     /* Flush it */
     push edx
     push eax
-    //call [_KeGdiFlushUserBatch]
+    call [_KeGdiFlushUserBatch]
     pop eax
     pop edx
 
 NotWin32K:
     /* Increase total syscall count */
-    inc dword ptr fs:[KPCR_SYSTEM_CALLS]
+    inc dword ptr PCR[KPCR_SYSTEM_CALLS]
 
-#ifdef DBG
+#if DBG
     /* Increase per-syscall count */
     mov ecx, [edi+SERVICE_DESCRIPTOR_COUNT]
     jecxz NoCountTable
@@ -201,27 +234,11 @@ CopyParams:
     /* Copy the parameters */
     rep movsd
 
-#ifdef DBG
-    /*
-     * The following lines are for the benefit of GDB. It will see the return
-     * address of the "call ebx" below, find the last label before it and
-     * thinks that that's the start of the function. It will then check to see
-     * if it starts with a standard function prolog (push ebp, mov ebp,esp1).
-     * When that standard function prolog is not found, it will stop the
-     * stack backtrace. Since we do want to backtrace into usermode, let's
-     * make GDB happy and create a standard prolog.
-     */
-KiSystemService:
-    push ebp
-    mov ebp,esp
-    pop ebp
-#endif
-
     /* Do the System Call */
     call ebx
 
 AfterSysCall:
-#ifdef DBG
+#if DBG
     /* Make sure the user-mode call didn't return at elevated IRQL */
     test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
     jz SkipCheck
@@ -232,7 +249,7 @@ AfterSysCall:
     mov eax, esi                /* Restore it */
 
     /* Get our temporary current thread pointer for sanity check */
-    mov ecx, fs:[KPCR_CURRENT_THREAD]
+    mov ecx, PCR[KPCR_CURRENT_THREAD]
 
     /* Make sure that we are not attached and that APCs are not disabled */
     mov dl, [ecx+KTHREAD_APC_STATE_INDEX]
@@ -251,7 +268,7 @@ SkipCheck:
 KeReturnFromSystemCall:
 
     /* Get the Current Thread */
-    mov ecx, [fs:KPCR_CURRENT_THREAD]
+    mov ecx, PCR[KPCR_CURRENT_THREAD]
 
     /* Restore the old trap frame pointer */
     mov edx, [ebp+KTRAP_FRAME_EDX]
@@ -342,7 +359,7 @@ AccessViolation:
 BadStack:
 
     /* Restore ESP0 stack */
-    mov ecx, [fs:KPCR_TSS]
+    mov ecx, PCR[KPCR_TSS]
     mov esp, ss:[ecx+KTSS_ESP0]
 
     /* Generate V86M Stack for Trap 6 */
@@ -359,13 +376,13 @@ BadStack:
     push 0
     jmp _KiTrap6
 
-#ifdef DBG
+#if DBG
 InvalidIrql:
     /* Save current IRQL */
-    push fs:[KPCR_IRQL]
+    push PCR[KPCR_IRQL]
 
     /* Set us at passive */
-    mov dword ptr fs:[KPCR_IRQL], 0
+    mov dword ptr PCR[KPCR_IRQL], 0
     cli
 
     /* Bugcheck */
@@ -443,62 +460,49 @@ AbiosExit:
     /* FIXME: TODO */
     UNHANDLED_PATH
 
+.func KiRaiseAssertion
+TRAP_FIXUPS kira_a, kira_t, DoFixupV86, DoFixupAbios
+_KiRaiseAssertion:
+
+    /* Push error code */
+    push 0
+
+    /* Enter trap */
+    TRAP_PROLOG kira_a, kira_t
+
+    /*
+     * Modify EIP so it points to the faulting instruction and set it as the
+     * exception address. Note that the 'int 2C' instruction used for this call
+     * is 2 bytes long as opposed to 1 byte 'int 3'.
+     */
+    sub dword ptr [ebp+KTRAP_FRAME_EIP], 2
+    mov ebx, [ebp+KTRAP_FRAME_EIP]
+
+    /* Raise an assertion failure */
+    mov eax, STATUS_ASSERTION_FAILURE
+    jmp _DispatchNoParam
+.endfunc
+
 .func KiDebugService
-Dr_kids:    DR_TRAP_FIXUP
-V86_kids:   V86_TRAP_FIXUP
+TRAP_FIXUPS kids_a, kids_t, DoFixupV86, DoFixupAbios
 _KiDebugService:
 
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kids
+    TRAP_PROLOG kids_a, kids_t
 
     /* Increase EIP so we skip the INT3 */
-    //inc dword ptr [ebp+KTRAP_FRAME_EIP]
+    inc dword ptr [ebp+KTRAP_FRAME_EIP]
 
     /* Call debug service dispatcher */
     mov eax, [ebp+KTRAP_FRAME_EAX]
     mov ecx, [ebp+KTRAP_FRAME_ECX]
-    mov edx, [ebp+KTRAP_FRAME_EAX]
-
-    /* Check for V86 mode */
-    test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
-    jnz NotUserMode
-
-    /* Check if this is kernel or user-mode */
-    test byte ptr [ebp+KTRAP_FRAME_CS], 1
-    jz CallDispatch
-    cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
-    jnz NotUserMode
-
-    /* Re-enable interrupts */
-VdmProc:
-    sti
-
-    /* Call the debug routine */
-CallDispatch:
-    mov esi, ecx
-    mov edi, edx
-    mov edx, eax
-    mov ecx, 3
-    push edi
-    push esi
-    push edx
-    call _KdpServiceDispatcher@12
-
-NotUserMode:
-
-    /* Get the current process */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
-    mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
-
-    /* Check if this is a VDM Process */
-    //cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
-    //jz VdmProc
+    mov edx, [ebp+KTRAP_FRAME_EDX]
 
-    /* Exit through common routine */
-    jmp _Kei386EoiHelper@0
+    /* Jump to INT3 handler */
+    jmp PrepareInt3
 .endfunc
 
 .func NtRaiseException@12
@@ -509,7 +513,7 @@ _NtRaiseException@12:
     push ebp
 
     /* Get the current thread and restore its trap frame */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov edx, [ebp+KTRAP_FRAME_EDX]
     mov [ebx+KTHREAD_TRAP_FRAME], edx
 
@@ -521,7 +525,7 @@ _NtRaiseException@12:
 
     /* Get the exception list and restore */
     mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
-    mov [fs:KPCR_EXCEPTION_LIST], eax
+    mov PCR[KPCR_EXCEPTION_LIST], eax
 
     /* Get the parameters */
     mov edx, [ebp+16] /* Search frames */
@@ -556,7 +560,7 @@ _NtContinue@8:
     push ebp
 
     /* Get the current thread and restore its trap frame */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov edx, [ebp+KTRAP_FRAME_EDX]
     mov [ebx+KTHREAD_TRAP_FRAME], edx
 
@@ -615,7 +619,7 @@ _CommonDispatchException:
     mov [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], ecx
 
     /* Check parameter count */
-    cmp eax, 0
+    cmp ecx, 0
     jz NoParams
 
     /* Get information */
@@ -628,7 +632,7 @@ NoParams:
 
     /* Set the record in ECX and check if this was V86 */
     mov ecx, esp
-    test dword ptr [esp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
+    test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
     jz SetPreviousMode
 
     /* Set V86 mode */
@@ -637,9 +641,11 @@ NoParams:
 
 SetPreviousMode:
 
-    /* Calculate the previous mode */
+    /* Get the caller's CS */
     mov eax, [ebp+KTRAP_FRAME_CS]
+
 MaskMode:
+    /* Check if it was user-mode or kernel-mode */
     and eax, MODE_MASK
 
     /* Dispatch the exception */
@@ -662,18 +668,25 @@ _DispatchNoParam:
     call _CommonDispatchException
 .endfunc
 
-.func DispatchOneParam
-_DispatchOneParam:
+.func DispatchOneParamZero
+_DispatchOneParamZero:
     /* Call the common dispatcher */
     xor edx, edx
     mov ecx, 1
     call _CommonDispatchException
 .endfunc
 
+.func DispatchTwoParamZero
+_DispatchTwoParamZero:
+    /* Call the common dispatcher */
+    xor edx, edx
+    mov ecx, 2
+    call _CommonDispatchException
+.endfunc
+
 .func DispatchTwoParam
 _DispatchTwoParam:
     /* Call the common dispatcher */
-    xor edx, edx
     mov ecx, 2
     call _CommonDispatchException
 .endfunc
@@ -688,14 +701,13 @@ _KiFixupFrame:
 .endfunc
 
 .func KiTrap0
-Dr_kit0:    DR_TRAP_FIXUP
-V86_kit0:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit0_a, kit0_t, DoFixupV86, DoNotFixupAbios
 _KiTrap0:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit0
+    TRAP_PROLOG kit0_a, kit0_t
 
     /* Check for V86 */
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
@@ -718,7 +730,7 @@ SendException:
 
 VdmCheck:
     /* Check if this is a VDM process */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz SendException
@@ -730,14 +742,13 @@ V86Int0:
 .endfunc
 
 .func KiTrap1
-Dr_kit1:    DR_TRAP_FIXUP
-V86_kit1:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit1_a, kit1_t, DoFixupV86, DoNotFixupAbios
 _KiTrap1:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit1
+    TRAP_PROLOG kit1_a, kit1_t
 
     /* Check for V86 */
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
@@ -764,7 +775,7 @@ PrepInt1:
 
 V86Int1:
     /* Check if this is a VDM process */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz EnableInterrupts
@@ -783,16 +794,22 @@ _KiTrap2:
 .endfunc
 
 .func KiTrap3
-Dr_kit3:    DR_TRAP_FIXUP
-V86_kit3:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit3_a, kit3_t, DoFixupV86, DoNotFixupAbios
 _KiTrap3:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit3
+    TRAP_PROLOG kit3_a, kit3_t
+
+    /*
+     * Set the special code to indicate that this is a software breakpoint
+     * and not a debug service call
+     */
+    mov eax, BREAKPOINT_BREAK
 
     /* Check for V86 */
+PrepareInt3:
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
     jnz V86Int3
 
@@ -809,6 +826,7 @@ EnableInterrupts3:
     sti
 
 PrepInt3:
+
     /* Prepare the exception */
     mov esi, ecx
     mov edi, edx
@@ -817,13 +835,13 @@ PrepInt3:
     /* Setup EIP, NTSTATUS and parameter count, then dispatch */
     mov ebx, [ebp+KTRAP_FRAME_EIP]
     dec ebx
-    mov eax, STATUS_BREAKPOINT
     mov ecx, 3
+    mov eax, STATUS_BREAKPOINT
     call _CommonDispatchException
 
 V86Int3:
     /* Check if this is a VDM process */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz EnableInterrupts3
@@ -833,14 +851,13 @@ V86Int3:
 .endfunc
 
 .func KiTrap4
-Dr_kit4:    DR_TRAP_FIXUP
-V86_kit4:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit4_a, kit4_t, DoFixupV86, DoNotFixupAbios
 _KiTrap4:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit4
+    TRAP_PROLOG kit4_a, kit4_t
 
     /* Check for V86 */
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
@@ -864,7 +881,7 @@ SendException4:
 
 VdmCheck4:
     /* Check if this is a VDM process */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz SendException4
@@ -875,14 +892,13 @@ V86Int4:
 .endfunc
 
 .func KiTrap5
-Dr_kit5:    DR_TRAP_FIXUP
-V86_kit5:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit5_a, kit5_t, DoFixupV86, DoNotFixupAbios
 _KiTrap5:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit5
+    TRAP_PROLOG kit5_a, kit5_t
 
     /* Check for V86 */
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
@@ -910,7 +926,7 @@ SendException5:
 
 VdmCheck5:
     /* Check if this is a VDM process */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz SendException5
@@ -921,8 +937,7 @@ V86Int5:
 .endfunc
 
 .func KiTrap6
-Dr_kit6:    DR_TRAP_FIXUP
-V86_kit6:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit6_a, kit6_t, DoFixupV86, DoNotFixupAbios
 _KiTrap6:
 
     /* It this a V86 GPF? */
@@ -930,8 +945,9 @@ _KiTrap6:
     jz NotV86UD
 
     /* Enter V86 Trap */
-    V86_TRAP_PROLOG kit6
+    V86_TRAP_PROLOG kit6_a, kit6_v
 
+VdmOpCodeFault:
     /* Not yet supported (Invalid OPCODE from V86) */
     UNHANDLED_PATH
 
@@ -940,8 +956,9 @@ NotV86UD:
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit6
+    TRAP_PROLOG kit6_a, kit6_t
 
+DispatchLockErrata:
     /* Check if this happened in kernel mode */
     test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
     jz KmodeOpcode
@@ -951,7 +968,7 @@ NotV86UD:
     jz UmodeOpcode
 
     /* Check if the process is vDM */
-    mov ebx, fs:[KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jnz IsVdmOpcode
@@ -967,8 +984,8 @@ UmodeOpcode:
     /* Setup a SEH frame */
     push ebp
     push OpcodeSEH
-    push fs:[KPCR_EXCEPTION_LIST]
-    mov fs:[KPCR_EXCEPTION_LIST], esp
+    push PCR[KPCR_EXCEPTION_LIST]
+    mov PCR[KPCR_EXCEPTION_LIST], esp
 
 OpcodeLoop:
     /* Get the instruction and check if it's LOCK */
@@ -981,7 +998,7 @@ OpcodeLoop:
     loop OpcodeLoop
 
     /* Undo SEH frame */
-    pop fs:[KPCR_EXCEPTION_LIST]
+    pop PCR[KPCR_EXCEPTION_LIST]
     add esp, 8
 
 KmodeOpcode:
@@ -997,7 +1014,7 @@ KmodeOpcode:
 LockCrash:
 
     /* Undo SEH Frame */
-    pop fs:[KPCR_EXCEPTION_LIST]
+    pop PCR[KPCR_EXCEPTION_LIST]
     add esp, 8
 
     /* Setup invalid lock exception and dispatch it */
@@ -1017,7 +1034,7 @@ OpcodeSEH:
 
     /* Get SEH frame */
     mov esp, [esp+8]
-    pop fs:[KPCR_EXCEPTION_LIST]
+    pop PCR[KPCR_EXCEPTION_LIST]
     add esp, 4
     pop ebp
 
@@ -1036,23 +1053,22 @@ OpcodeSEH:
 .endfunc
 
 .func KiTrap7
-Dr_kit7:    DR_TRAP_FIXUP
-V86_kit7:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit7_a, kit7_t, DoFixupV86, DoNotFixupAbios
 _KiTrap7:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit7
+    TRAP_PROLOG kit7_a, kit7_t
 
     /* Get the current thread and stack */
 StartTrapHandle:
-    mov eax, [fs:KPCR_CURRENT_THREAD]
+    mov eax, PCR[KPCR_CURRENT_THREAD]
     mov ecx, [eax+KTHREAD_INITIAL_STACK]
     sub ecx, NPX_FRAME_LENGTH
 
     /* Check if emulation is enabled */
-    test dword ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
+    test byte ptr [ecx+FN_CR0_NPX_STATE], CR0_EM
     jnz EmulationEnabled
 
 CheckState:
@@ -1066,7 +1082,7 @@ CheckState:
     mov cr0, ebx
 
     /* Check the NPX thread */
-    mov edx, [fs:KPCR_NPX_THREAD]
+    mov edx, PCR[KPCR_NPX_THREAD]
     or edx, edx
     jz NoNpxThread
 
@@ -1095,12 +1111,12 @@ NoNpxThread:
     jmp AfterRestore
 
 FrRestore:
-    frstor [esi]
+    frstor [ecx]
 
 AfterRestore:
     /* Set state loaded */
     mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_LOADED
-    mov [fs:KPCR_NPX_THREAD], eax
+    mov PCR[KPCR_NPX_THREAD], eax
 
     /* Enable interrupts to happen now */
     sti
@@ -1161,7 +1177,7 @@ HandleNpxFault:
 
 UserNpx:
     /* Get the current thread */
-    mov eax, fs:[KPCR_CURRENT_THREAD]
+    mov eax, PCR[KPCR_CURRENT_THREAD]
 
     /* Check NPX state */
     cmp byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
@@ -1197,7 +1213,7 @@ MakeCr0Dirty:
 
     /* Update NPX state */
     mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
-    mov dword ptr fs:[KPCR_NPX_THREAD], 0
+    mov dword ptr PCR[KPCR_NPX_THREAD], 0
 
 NoSaveRestore:
     /* Clear the TS bit and re-enable interrupts */
@@ -1242,13 +1258,13 @@ CheckError:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_INVALID_OPERATION
-    jmp _DispatchOneParam
+    jmp _DispatchOneParamZero
 
 InvalidStack:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_STACK_CHECK
-    jmp _DispatchTwoParam
+    jmp _DispatchTwoParamZero
 
 ValidNpxOpcode:
 
@@ -1258,7 +1274,7 @@ ValidNpxOpcode:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_DIVIDE_BY_ZERO
-    jmp _DispatchOneParam
+    jmp _DispatchOneParamZero
 
 1:
     /* Check for denormal */
@@ -1267,7 +1283,7 @@ ValidNpxOpcode:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_INVALID_OPERATION
-    jmp _DispatchOneParam
+    jmp _DispatchOneParamZero
 
 1:
     /* Check for overflow */
@@ -1276,7 +1292,7 @@ ValidNpxOpcode:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_OVERFLOW
-    jmp _DispatchOneParam
+    jmp _DispatchOneParamZero
 
 1:
     /* Check for underflow */
@@ -1285,7 +1301,7 @@ ValidNpxOpcode:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_UNDERFLOW
-    jmp _DispatchOneParam
+    jmp _DispatchOneParamZero
 
 1:
     /* Check for precision fault */
@@ -1294,7 +1310,7 @@ ValidNpxOpcode:
 
     /* Raise exception */
     mov eax, STATUS_FLOAT_INEXACT_RESULT
-    jmp _DispatchOneParam
+    jmp _DispatchOneParamZero
 
 UnexpectedNpx:
 
@@ -1310,7 +1326,7 @@ UnexpectedNpx:
 
 V86Npx:
     /* Check if this is a VDM */
-    mov eax, fs:[KPCR_CURRENT_THREAD]
+    mov eax, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz HandleUserNpx
@@ -1349,21 +1365,19 @@ BogusTrap:
 .globl _KiTrap8
 .func KiTrap8
 _KiTrap8:
-
     /* Can't really do too much */
     mov eax, 8
     jmp _KiSystemFatalException
 .endfunc
 
 .func KiTrap9
-Dr_kit9:    DR_TRAP_FIXUP
-V86_kit9:   V86_TRAP_FIXUP
+TRAP_FIXUPS kit9_a, kit9_t, DoFixupV86, DoNotFixupAbios
 _KiTrap9:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit9
+    TRAP_PROLOG kit9_a, kit9_t
 
     /* Enable interrupts and bugcheck */
     sti
@@ -1372,11 +1386,10 @@ _KiTrap9:
 .endfunc
 
 .func KiTrap10
-Dr_kit10:   DR_TRAP_FIXUP
-V86_kit10:  V86_TRAP_FIXUP
+TRAP_FIXUPS kita_a, kita_t, DoFixupV86, DoNotFixupAbios
 _KiTrap10:
     /* Enter trap */
-    TRAP_PROLOG kit10
+    TRAP_PROLOG kita_a, kita_t
 
     /* Check for V86 */
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
@@ -1403,11 +1416,10 @@ Fatal:
 .endfunc
 
 .func KiTrap11
-Dr_kit11:   DR_TRAP_FIXUP
-V86_kit11:  V86_TRAP_FIXUP
+TRAP_FIXUPS kitb_a, kitb_t, DoFixupV86, DoNotFixupAbios
 _KiTrap11:
     /* Enter trap */
-    TRAP_PROLOG kit11
+    TRAP_PROLOG kitb_a, kitb_t
 
     /* FIXME: ROS Doesn't handle segment faults yet */
     mov eax, 11
@@ -1415,20 +1427,41 @@ _KiTrap11:
 .endfunc
 
 .func KiTrap12
-Dr_kit12:   DR_TRAP_FIXUP
-V86_kit12:  V86_TRAP_FIXUP
+TRAP_FIXUPS kitc_a, kitc_t, DoFixupV86, DoNotFixupAbios
 _KiTrap12:
     /* Enter trap */
-    TRAP_PROLOG kit12
+    TRAP_PROLOG kitc_a, kitc_t
 
     /* FIXME: ROS Doesn't handle stack faults yet */
     mov eax, 12
     jmp _KiSystemFatalException
 .endfunc
 
+.func KiTrapExceptHandler
+_KiTrapExceptHandler:
+
+    /* Setup SEH handler frame */
+    mov esp, [esp+8]
+    pop PCR[KPCR_EXCEPTION_LIST]
+    add esp, 4
+    pop ebp
+
+    /* Check if the fault came from user mode */
+    test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
+    jnz SetException
+
+    /* Kernel fault, bugcheck */
+    push ebp
+    push 0
+    push 0
+    push 0
+    push 0
+    push KMODE_EXCEPTION_NOT_HANDLED
+    call _KeBugCheckWithTf@24
+.endfunc
+
 .func KiTrap13
-Dr_kitd:    DR_TRAP_FIXUP
-V86_kitd:   V86_TRAP_FIXUP
+TRAP_FIXUPS kitd_a, kitd_t, DoFixupV86, DoNotFixupAbios
 _KiTrap13:
 
     /* It this a V86 GPF? */
@@ -1436,20 +1469,17 @@ _KiTrap13:
     jz NotV86
 
     /* Enter V86 Trap */
-    V86_TRAP_PROLOG kitd
+    V86_TRAP_PROLOG kitd_a, kitd_v
 
     /* Make sure that this is a V86 process */
-    mov ecx, [fs:KPCR_CURRENT_THREAD]
+    mov ecx, PCR[KPCR_CURRENT_THREAD]
     mov ecx, [ecx+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [ecx+EPROCESS_VDM_OBJECTS], 0
     jnz RaiseIrql
 
     /* Otherwise, something is very wrong, raise an exception */
     sti
-    mov ebx, [ebp+KTRAP_FRAME_EIP]
-    mov esi, -1
-    mov eax, STATUS_ACCESS_VIOLATION
-    jmp _DispatchTwoParam
+    jmp SetException
 
 RaiseIrql:
 
@@ -1492,14 +1522,14 @@ NotV86Trap:
 
 NotV86:
     /* Enter trap */
-    TRAP_PROLOG kitd
-
+    TRAP_PROLOG kitd_a, kitd_t
+    
     /* Check if this was from kernel-mode */
     test dword ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
     jnz UserModeGpf
 
     /* Check if we have a VDM alert */
-    cmp dword ptr fs:[KPCR_VDM_ALERT], 0
+    cmp dword ptr PCR[KPCR_VDM_ALERT], 0
     jnz VdmAlertGpf
 
     /* Check for GPF during GPF */
@@ -1507,6 +1537,7 @@ NotV86:
     cmp eax, offset CheckPrivilegedInstruction
     jbe KmodeGpf
     cmp eax, offset CheckPrivilegedInstruction2
+    jae KmodeGpf
 
     /* FIXME: TODO */
     UNHANDLED_PATH
@@ -1559,7 +1590,7 @@ KmodeGpf:
 
 NotBiosGpf:
     /* Check if the thread was in kernel mode */
-    mov ebx, [fs:KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     test byte ptr [ebx+KTHREAD_PREVIOUS_MODE], 0xFF
     jz UserModeGpf
 
@@ -1593,7 +1624,7 @@ TrapCopy:
     mov esi, [ebp+KTRAP_FRAME_ERROR_CODE]
     and esi, 0xFFFF
     mov eax, STATUS_ACCESS_VIOLATION
-    jmp _DispatchTwoParam
+    jmp _DispatchTwoParamZero
 
 MsrCheck:
 
@@ -1630,6 +1661,7 @@ SegPopGpf:
     lea eax, [ebp+KTRAP_FRAME_ESP]
     cmp edx, eax
     jz HandleSegPop
+    int 3
 
     /* Handle segment POP fault by setting it to 0 */
 HandleSegPop:
@@ -1649,7 +1681,7 @@ UserModeGpf:
     jz _KiSystemFatalException
 
     /* Get the process and check which CS this came from */
-    mov ebx, fs:[KPCR_CURRENT_THREAD]
+    mov ebx, PCR[KPCR_CURRENT_THREAD]
     mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]
     cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
     jz CheckVdmGpf
@@ -1668,7 +1700,7 @@ HandleSegPop2:
     /* Update EIP (will be updated below again) */
     add dword ptr [ebp+KTRAP_FRAME_EIP], 1
 
-HandleBop4:
+HandleEsPop:
     /* Clear the segment, update EIP and ESP */
     mov dword ptr [edx], 0
     add dword ptr [ebp+KTRAP_FRAME_EIP], 1
@@ -1680,15 +1712,23 @@ CheckVdmGpf:
     cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
     jz CheckPrivilegedInstruction
 
+    /* Bring interrupts back */
+    sti
+
     /* Check what kind of instruction this is */
     mov eax, [ebp+KTRAP_FRAME_EIP]
     mov eax, [eax]
 
     /* FIXME: Check for BOP4 */
 
-    /* Check if this is POP FS */
+    /* Check if this is POP ES */
     mov edx, ebp
-    add edx, KTRAP_FRAME_FS
+    add edx, KTRAP_FRAME_ES
+    cmp al, 0x07
+    jz HandleEsPop
+
+    /* Check if this is POP FS */
+    add edx, KTRAP_FRAME_FS - KTRAP_FRAME_ES
     cmp ax, 0xA10F
     jz HandleSegPop2
 
@@ -1698,16 +1738,178 @@ CheckVdmGpf:
     jz HandleSegPop2
 
 CheckPrivilegedInstruction:
-    /* FIXME */
-    UNHANDLED_PATH
+    /* Bring interrupts back */
+    sti
+
+    /* Setup a SEH handler */
+    push ebp
+    push offset _KiTrapExceptHandler
+    push PCR[KPCR_EXCEPTION_LIST]
+    mov PCR[KPCR_EXCEPTION_LIST], esp
+
+    /* Get EIP */
+    mov esi, [ebp+KTRAP_FRAME_EIP]
+
+    /* Setup loop count */
+    mov ecx, 15
+
+InstLoop:
+    /* Save loop count */
+    push ecx
+
+    /* Get the instruction */
+    lods byte ptr [esi]
+
+    /* Now lookup in the prefix table */
+    mov ecx, 11
+    mov edi, offset _KiTrapPrefixTable
+    repnz scasb
+
+    /* Restore loop count */
+    pop ecx
+
+    /* If it's not a prefix byte, check other instructions */
+    jnz NotPrefixByte
+    
+    /* Keep looping */
+    loop InstLoop
+    
+    /* Fixup the stack */
+    pop PCR[KPCR_EXCEPTION_LIST]
+    add esp, 8
+
+    /* Illegal instruction */
+    jmp KmodeOpcode
+
+NotPrefixByte:
+    /* Check if it's a HLT */
+    cmp al, 0x0F4
+    je IsPrivInstruction
+
+    /* Check if the instruction has two bytes */
+    cmp al, 0xF
+    jne CheckRing3Io
+    
+    /* Check if this is a LLDT or LTR */
+    lods byte ptr [esi]
+    cmp al, 0
+    jne NotLldt
+    
+    /* Check if this is an LLDT */
+    lods byte ptr [esi]
+    and al, 0x38
+    cmp al, 0x10
+    je IsPrivInstruction
+    
+    /* Check if this is an LTR */
+    cmp al, 0x18
+    je IsPrivInstruction
+    
+    /* Otherwise, access violation */
+    jmp NotIoViolation
+    
+NotLldt:
+    /* Check if this is LGDT or LIDT or LMSW */
+    cmp al, 0x01
+    jne NotGdt
+    
+    /* Check if this is an LGDT */
+    lods byte ptr [esi]
+    and al, 0x38
+    cmp al, 0x10
+    je IsPrivInstruction
+    
+    /* Check if this is an LIDT */
+    cmp al, 0x18
+    je IsPrivInstruction
+    
+    /* Check if this is an LMSW */
+    cmp al, 0x30
+    je IsPrivInstruction
+    
+    /* Otherwise, access violation */
+    jmp NotIoViolation
+    
+NotGdt:
+    /* Check if it's INVD or WBINVD */
+    cmp al, 0x8
+    je IsPrivInstruction
+    cmp al, 0x9
+    je IsPrivInstruction
+    
+    /* Check if it's sysexit */
+    cmp al, 0x35
+    je IsPrivInstruction
+    
+    /* Check if it's a DR move */
+    cmp al, 0x26
+    je IsPrivInstruction
+    
+    /* Check if it's a CLTS */
+    cmp al, 0x6
+    je IsPrivInstruction
+    
+    /* Check if it's a CR move */
+    cmp al, 0x20
+    jb NotIoViolation
+    
+    /* Check if it's a DR move */
+    cmp al, 0x24
+    jbe IsPrivInstruction
+    
+    /* Everything else is an access violation */
+    jmp NotIoViolation
+
+CheckRing3Io:
+    /* Get EFLAGS and IOPL */
+    mov ebx, [ebp+KTRAP_FRAME_EFLAGS]
+    and ebx, EFLAGS_IOPL
+    shr ebx, 12
+
+    /* Check the CS's RPL mask */
+    mov ecx, [ebp+KTRAP_FRAME_CS]
+    and ecx, RPL_MASK
+    cmp ebx, ecx
+    jge NotIoViolation
 
 CheckPrivilegedInstruction2:
-    /* FIXME */
-    UNHANDLED_PATH
+    /* Check if this is a CLI or STI */
+    cmp al, 0xFA
+    je IsPrivInstruction
+    cmp al, 0xFB
+    je IsPrivInstruction
+
+    /* Setup I/O table lookup */
+    mov ecx, 13
+    mov edi, offset _KiTrapIoTable
+
+    /* Loopup in the table */
+    repnz scasb
+    jnz NotIoViolation
+
+    /* FIXME: Check IOPM!!! */
+
+IsPrivInstruction:
+    /* Cleanup the SEH frame */
+    pop PCR[KPCR_EXCEPTION_LIST]
+    add esp, 8
+
+    /* Setup the exception */
+    mov ebx, [ebp+KTRAP_FRAME_EIP]
+    mov eax, STATUS_PRIVILEGED_INSTRUCTION
+    jmp _DispatchNoParam
+
+NotIoViolation:
+    /* Cleanup the SEH frame */
+    pop PCR[KPCR_EXCEPTION_LIST]
+    add esp, 8
 
 SetException:
-    /* FIXME */
-    UNHANDLED_PATH
+    /* Setup the exception */
+    mov ebx, [ebp+KTRAP_FRAME_EIP]
+    mov esi, -1
+    mov eax, STATUS_ACCESS_VIOLATION
+    jmp _DispatchTwoParamZero
 
 DispatchV86Gpf:
     /* FIXME */
@@ -1715,19 +1917,18 @@ DispatchV86Gpf:
 .endfunc
 
 .func KiTrap14
-Dr_kit14:   DR_TRAP_FIXUP
-V86_kit14:  V86_TRAP_FIXUP
+TRAP_FIXUPS kite_a, kite_t, DoFixupV86, DoNotFixupAbios
 _KiTrap14:
 
     /* Enter trap */
-    TRAP_PROLOG kit14
+    TRAP_PROLOG kite_a, kite_t
 
     /* Check if we have a VDM alert */
-    cmp dword ptr fs:[KPCR_VDM_ALERT], 0
+    cmp dword ptr PCR[KPCR_VDM_ALERT], 0
     jnz VdmAlertGpf
 
     /* Get the current thread */
-    mov edi, fs:[KPCR_CURRENT_THREAD]
+    mov edi, PCR[KPCR_CURRENT_THREAD]
 
     /* Get the stack address of the frame */
     lea eax, [esp+KTRAP_FRAME_LENGTH+NPX_FRAME_LENGTH]
@@ -1739,7 +1940,7 @@ _KiTrap14:
     jb NoFixUp
 
     /* Check if we have a TEB */
-    mov eax, fs:[KPCR_TEB]
+    mov eax, PCR[KPCR_TEB]
     or eax, eax
     jle NoFixUp
 
@@ -1750,14 +1951,21 @@ _KiTrap14:
 NoFixUp:
     mov edi, cr2
 
-    /* ROS HACK: Sometimes we get called with INTS DISABLED! WTF? */
+    /* Check if this processor has the cmpxchg8b lock errata */
+    cmp byte ptr _KiI386PentiumLockErrataPresent, 0
+    jnz HandleLockErrata
+
+NotLockErrata:
+    /* HACK: Handle page faults with interrupts disabled */
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
     je HandlePf
 
     /* Enable interrupts and check if we got here with interrupts disabled */
     sti
+#ifdef HACK_ABOVE_FIXED
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_INTERRUPT_MASK
     jz IllegalState
+#endif
 
 HandlePf:
     /* Send trap frame and check if this is kernel-mode or usermode */
@@ -1806,13 +2014,13 @@ SysCallCopyFault:
     /* Check if the fault occured in a V86 mode */
 CheckVdmPf:
     mov ecx, [ebp+KTRAP_FRAME_ERROR_CODE]
-    and ecx, 1
     shr ecx, 1
+    and ecx, 1
     test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
     jnz VdmPF
 
     /* Check if the fault occured in a VDM */
-    mov esi, fs:[KPCR_CURRENT_THREAD]
+    mov esi, PCR[KPCR_CURRENT_THREAD]
     mov esi, [esi+KTHREAD_APCSTATE_PROCESS]
     cmp dword ptr [esi+EPROCESS_VDM_OBJECTS], 0
     jz CheckStatus
@@ -1876,17 +2084,43 @@ VdmAlertGpf:
 
     /* FIXME: NOT SUPPORTED */
     UNHANDLED_PATH
+
+HandleLockErrata:
+
+    /* Fail if this isn't a write fault */
+    test word ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0x4
+    jnz NotLockErrata
+
+    /* Also make sure the page fault is for IDT entry 6 */
+    mov eax, PCR[KPCR_IDT]
+    add eax, 0x30
+    cmp eax, edi
+    jne NotLockErrata
+
+    /*
+     * This is a write fault to the Invalid Opcode handler entry.
+     * We assume this is the lock errata and not a real write fault.
+     */
+
+    /* Clear the error code */
+    and dword ptr [ebp+KTRAP_FRAME_ERROR_CODE], 0
+
+    /* Check if this happened in V86 mode */
+    test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
+    jnz VdmOpCodeFault
+
+    /* Dispatch this to the invalid opcode handler */
+    jmp DispatchLockErrata
 .endfunc
 
 .func KiTrap0F
-Dr_kit15:   DR_TRAP_FIXUP
-V86_kit15:  V86_TRAP_FIXUP
+TRAP_FIXUPS kitf_a, kitf_t, DoFixupV86, DoNotFixupAbios
 _KiTrap0F:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit15
+    TRAP_PROLOG kitf_a, kitf_t
     sti
 
     /* Raise a fatal exception */
@@ -1895,18 +2129,17 @@ _KiTrap0F:
 .endfunc
 
 .func KiTrap16
-Dr_kit16:   DR_TRAP_FIXUP
-V86_kit16:  V86_TRAP_FIXUP
+TRAP_FIXUPS kit10_a, kit10_t, DoFixupV86, DoNotFixupAbios
 _KiTrap16:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit16
+    TRAP_PROLOG kit10_a, kit10_t
 
     /* Check if this is the NPX Thread */
-    mov eax, fs:[KPCR_CURRENT_THREAD]
-    cmp eax, fs:[KPCR_NPX_THREAD]
+    mov eax, PCR[KPCR_CURRENT_THREAD]
+    cmp eax, PCR[KPCR_NPX_THREAD]
 
     /* Get the initial stack and NPX frame */
     mov ecx, [eax+KTHREAD_INITIAL_STACK]
@@ -1922,20 +2155,193 @@ _KiTrap16:
 .endfunc
 
 .func KiTrap17
-Dr_kit17:   DR_TRAP_FIXUP
-V86_kit17:  V86_TRAP_FIXUP
+TRAP_FIXUPS kit11_a, kit11_t, DoFixupV86, DoNotFixupAbios
 _KiTrap17:
     /* Push error code */
     push 0
 
     /* Enter trap */
-    TRAP_PROLOG kit17
+    TRAP_PROLOG kit11_a, kit11_t
 
     /* FIXME: ROS Doesn't handle alignment faults yet */
     mov eax, 17
     jmp _KiSystemFatalException
 .endfunc
 
+.globl _KiTrap19
+.func KiTrap19
+TRAP_FIXUPS kit19_a, kit19_t, DoFixupV86, DoNotFixupAbios
+_KiTrap19:
+    /* Push error code */
+    push 0
+
+    /* Enter trap */
+    TRAP_PROLOG kit19_a, kit19_t
+
+    /* Check if this is the NPX Thread */
+    mov eax, PCR[KPCR_CURRENT_THREAD]
+    cmp eax, PCR[KPCR_NPX_THREAD]
+
+    /* If this is a valid fault, handle it */
+    jz HandleXmmiFault
+
+    /* Otherwise, bugcheck */
+    mov eax, 19
+    jmp _KiSystemFatalException
+
+HandleXmmiFault:
+    /* Get the initial stack and NPX frame */
+    mov ecx, [eax+KTHREAD_INITIAL_STACK]
+    lea ecx, [ecx-NPX_FRAME_LENGTH]
+
+    /* Check if the trap came from V86 mode */
+    test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
+    jnz V86Xmmi
+
+    /* Check if it came from kernel mode */
+    test byte ptr [ebp+KTRAP_FRAME_CS], MODE_MASK
+    jz KernelXmmi
+
+    /* Check if it came from a VDM */
+    cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
+    jne VdmXmmi
+
+HandleUserXmmi:
+    /* Set new CR0 */
+    mov ebx, cr0
+    and ebx, ~(CR0_MP + CR0_EM + CR0_TS)
+    mov cr0, ebx
+
+    /* Check if we have FX support */
+    test byte ptr _KeI386FxsrPresent, 1
+    jz XmmiFnSave2
+
+    /* Save the state */
+    fxsave [ecx]
+    jmp XmmiMakeCr0Dirty
+XmmiFnSave2:
+    fnsave [ecx]
+    wait
+
+XmmiMakeCr0Dirty:
+    /* Make CR0 state not loaded */
+    or ebx, NPX_STATE_NOT_LOADED
+    or ebx, [ecx+FN_CR0_NPX_STATE]
+    mov cr0, ebx
+
+    /* Update NPX state */
+    mov byte ptr [eax+KTHREAD_NPX_STATE], NPX_STATE_NOT_LOADED
+    mov dword ptr PCR[KPCR_NPX_THREAD], 0
+
+    /* Clear the TS bit and re-enable interrupts */
+    and dword ptr [ecx+FN_CR0_NPX_STATE], ~CR0_TS
+
+    /* Re-enable interrupts for user-mode and send the exception */
+    sti
+    mov ebx, [ebp+KTRAP_FRAME_EIP]
+
+    /* Get MxCSR and get current mask (bits 7-12) */
+    movzx eax, word ptr [ecx+FX_MXCSR]
+    mov edx, eax
+    shr edx, 7
+    not edx
+
+    /* Set faulting opcode address to 0 */
+    mov esi, 0
+
+    /* Apply legal exceptions mask */
+    and eax, 0x3f
+
+    /* Apply the mask we got in MXCSR itself */
+    and eax, edx
+
+    /* Check for invalid operation */
+    test al, 1
+    jz 1f
+
+    /* Raise exception */
+    mov eax, STATUS_FLOAT_MULTIPLE_TRAPS
+    jmp _DispatchOneParamZero
+
+1:
+    /* Check for zero divide */
+    test al, 2
+    jz 1f
+
+    /* Raise exception */
+    mov eax, STATUS_FLOAT_MULTIPLE_TRAPS
+    jmp _DispatchOneParamZero
+
+1:
+    /* Check for denormal */
+    test al, 4
+    jz 1f
+
+    /* Raise exception */
+    mov eax, STATUS_FLOAT_MULTIPLE_TRAPS
+    jmp _DispatchOneParamZero
+
+1:
+    /* Check for overflow*/
+    test al, 8
+    jz 1f
+
+    /* Raise exception */
+    mov eax, STATUS_FLOAT_MULTIPLE_FAULTS
+    jmp _DispatchOneParamZero
+
+1:
+    /* Check for denormal */
+    test al, 16
+    jz 1f
+
+    /* Raise exception */
+    mov eax, STATUS_FLOAT_MULTIPLE_FAULTS
+    jmp _DispatchOneParamZero
+
+1:
+    /* Check for Precision */
+    test al, 32
+    jz UnexpectedXmmi
+
+    /* Raise exception */
+    mov eax, STATUS_FLOAT_MULTIPLE_FAULTS
+    jmp _DispatchOneParamZero
+
+UnexpectedXmmi:
+    /* Strange result, bugcheck the OS */
+    sti
+    push ebp
+    push 1
+    push 0
+    push eax
+    push 13
+    push TRAP_CAUSE_UNKNOWN
+    call _KeBugCheckWithTf@24
+
+VdmXmmi:
+    /* Check if this is a VDM */
+    mov eax, PCR[KPCR_CURRENT_THREAD]
+    mov ebx, [eax+KTHREAD_APCSTATE_PROCESS]
+    cmp dword ptr [ebx+EPROCESS_VDM_OBJECTS], 0
+    jz HandleUserXmmi
+
+V86Xmmi:
+    /* V86 XMMI not handled */
+    UNHANDLED_PATH
+
+KernelXmmi:
+    /* Another weird situation */
+    push ebp
+    push 2
+    push 0
+    push eax
+    push 13
+    push TRAP_CAUSE_UNKNOWN
+    call _KeBugCheckWithTf@24
+.endfunc
+
+
 .func KiSystemFatalException
 _KiSystemFatalException:
 
@@ -1958,7 +2364,7 @@ _KiSystemFatalException:
 _KiCoprocessorError@0:
 
     /* Get the NPX Thread's Initial stack */
-    mov eax, [fs:KPCR_NPX_THREAD]
+    mov eax, PCR[KPCR_NPX_THREAD]
     mov eax, [eax+KTHREAD_INITIAL_STACK]
 
     /* Make space for the FPU Save area */
@@ -1984,7 +2390,7 @@ _Ki16BitStackException:
     push esp
 
     /* Go to kernel mode thread stack */
-    mov eax, fs:[KPCR_CURRENT_THREAD]
+    mov eax, PCR[KPCR_CURRENT_THREAD]
     add esp, [eax+KTHREAD_INITIAL_STACK]
 
     /* Switch to good stack segment */
@@ -2003,15 +2409,14 @@ _KiEndUnexpectedRange@0:
     jmp _KiUnexpectedInterruptTail
 
 .func KiUnexpectedInterruptTail
-V86_kui: V86_TRAP_FIXUP
-Dr_kui:  DR_TRAP_FIXUP
+TRAP_FIXUPS kui_a, kui_t, DoFixupV86, DoFixupAbios
 _KiUnexpectedInterruptTail:
 
     /* Enter interrupt trap */
-    INT_PROLOG kui, DoNotPushFakeErrorCode
+    INT_PROLOG kui_a, kui_t, DoNotPushFakeErrorCode
 
     /* Increase interrupt count */
-    inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
+    inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
 
     /* Put vector in EBX and make space for KIRQL */
     mov ebx, [esp]
@@ -2060,7 +2465,7 @@ _KiUnexpectedInterrupt:
 _KiDispatchInterrupt@0:
 
     /* Get the PCR  and disable interrupts */
-    mov ebx, [fs:KPCR_SELF]
+    mov ebx, PCR[KPCR_SELF]
     cli
 
     /* Check if we have to deliver DPCs, timers, or deferred threads */
@@ -2076,7 +2481,7 @@ _KiDispatchInterrupt@0:
 
     /* Save the stack and switch to the DPC Stack */
     mov edx, esp
-    //mov esp, [ebx+KPCR_PRCB_DPC_STACK]
+    mov esp, [ebx+KPCR_PRCB_DPC_STACK]
     push edx
 
     /* Deliver DPCs */
@@ -2111,16 +2516,27 @@ CheckQuantum:
     mov edi, [ebx+KPCR_CURRENT_THREAD]
 
 #ifdef CONFIG_SMP
-    #error SMP Interrupt not handled!
+    /* Raise to synch level */
+    call _KeRaiseIrqlToSynchLevel@0
+
+    /* Set context swap busy */
+    mov byte ptr [edi+KTHREAD_SWAP_BUSY], 1
+
+    /* Acquire the PRCB Lock */
+    lock bts dword ptr [ebx+KPCR_PRCB_PRCB_LOCK], 0
+    jnb GetNext
+    lea ecx, [ebx+KPCR_PRCB_PRCB_LOCK]
+    call @KefAcquireSpinLockAtDpcLevel@4
 #endif
 
+GetNext:
     /* Get the next thread and clear it */
     mov esi, [ebx+KPCR_PRCB_NEXT_THREAD]
     and dword ptr [ebx+KPCR_PRCB_NEXT_THREAD], 0
 
     /* Set us as the current running thread */
     mov [ebx+KPCR_CURRENT_THREAD], esi
-    mov byte ptr [esi+KTHREAD_STATE], Running
+    mov byte ptr [esi+KTHREAD_STATE_], Running
     mov byte ptr [edi+KTHREAD_WAIT_REASON], WrDispatchInt
 
     /* Put thread in ECX and get the PRCB in EDX */
@@ -2132,6 +2548,12 @@ CheckQuantum:
     mov cl, APC_LEVEL
     call @KiSwapContextInternal@0
 
+#ifdef CONFIG_SMP
+    /* Lower IRQL back to dispatch */
+    mov cl, DISPATCH_LEVEL
+    call @KfLowerIrql@4
+#endif
+
     /* Restore registers */
     mov ebp, [esp+0]
     mov edi, [esp+4]
@@ -2150,13 +2572,10 @@ QuantumEnd:
 .endfunc
 
 .func KiInterruptTemplate
-V86_kit: V86_TRAP_FIXUP
-Dr_kit:  DR_TRAP_FIXUP
 _KiInterruptTemplate:
 
     /* Enter interrupt trap */
-    INT_PROLOG kit, DoPushFakeErrorCode
-.endfunc
+    INT_PROLOG kit_a, kit_t, DoPushFakeErrorCode
 
 _KiInterruptTemplate2ndDispatch:
     /* Dummy code, will be replaced by the address of the KINTERRUPT */
@@ -2169,18 +2588,94 @@ _KiInterruptTemplateObject:
 _KiInterruptTemplateDispatch:
     /* Marks the end of the template so that the jump above can be edited */
 
+TRAP_FIXUPS kit_a, kit_t, DoFixupV86, DoFixupAbios
+.endfunc
+
 .func KiChainedDispatch2ndLvl@0
 _KiChainedDispatch2ndLvl@0:
 
-    /* Not yet supported */
-    UNHANDLED_PATH
+NextSharedInt:
+    /* Raise IRQL if necessary */
+    mov cl, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
+    cmp cl, [edi+KINTERRUPT_IRQL]
+    je 1f
+    call @KfRaiseIrql@4
+
+1:
+    /* Acquire the lock */
+    mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
+GetIntLock2:
+    ACQUIRE_SPINLOCK(esi, IntSpin2)
+
+    /* Make sure that this interrupt isn't storming */
+    VERIFY_INT kid2
+
+    /* Save the tick count */
+    mov esi, _KeTickCount
+
+    /* Call the ISR */
+    mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
+    push eax
+    push edi
+    call [edi+KINTERRUPT_SERVICE_ROUTINE]
+
+    /* Save the ISR result */
+    mov bl, al
+
+    /* Check if the ISR timed out */
+    add esi, _KiISRTimeout
+    cmp _KeTickCount, esi
+    jnc ChainedIsrTimeout
+
+ReleaseLock2:
+    /* Release the lock */
+    mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
+    RELEASE_SPINLOCK(esi)
+
+    /* Lower IRQL if necessary */
+    mov cl, [edi+KINTERRUPT_IRQL]
+    cmp cl, [edi+KINTERRUPT_SYNCHRONIZE_IRQL]
+    je 1f
+    call @KfLowerIrql@4
+
+1:
+    /* Check if the interrupt is handled */
+    or bl, bl
+    jnz 1f
+
+    /* Try the next shared interrupt handler */
+    mov eax, [edi+KINTERRUPT_INTERRUPT_LIST_HEAD]
+    lea edi, [eax-KINTERRUPT_INTERRUPT_LIST_HEAD]
+    jmp NextSharedInt
+
+1:
+    ret
+
+#ifdef CONFIG_SMP
+IntSpin2:
+    SPIN_ON_LOCK(esi, GetIntLock2)
+#endif
+
+ChainedIsrTimeout:
+    /* Print warning message */
+    push [edi+KINTERRUPT_SERVICE_ROUTINE]
+    push offset _IsrTimeoutMsg
+    call _DbgPrint
+    add esp,8
+
+    /* Break into debugger, then continue */
+    int 3
+    jmp ReleaseLock2
+
+    /* Cleanup verification */
+    VERIFY_INT_END kid2, 0
 .endfunc
 
 .func KiChainedDispatch@0
 _KiChainedDispatch@0:
 
     /* Increase interrupt count */
-    inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
+    inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
 
     /* Save trap frame */
     mov ebp, esp
@@ -2207,17 +2702,14 @@ _KiChainedDispatch@0:
     call _KiChainedDispatch2ndLvl@0
 
     /* Exit the interrupt */
-    mov esi, $
-    cli
-    call _HalEndSystemInterrupt@8
-    jmp _Kei386EoiHelper@0
+    INT_EPILOG 0
 .endfunc
 
 .func KiInterruptDispatch@0
 _KiInterruptDispatch@0:
 
     /* Increase interrupt count */
-    inc dword ptr [fs:KPCR_PRCB_INTERRUPT_COUNT]
+    inc dword ptr PCR[KPCR_PRCB_INTERRUPT_COUNT]
 
     /* Save trap frame */
     mov ebp, esp
@@ -2241,31 +2733,126 @@ _KiInterruptDispatch@0:
     jz SpuriousInt
 
     /* Acquire the lock */
-GetIntLock:
     mov esi, [edi+KINTERRUPT_ACTUAL_LOCK]
+GetIntLock:
     ACQUIRE_SPINLOCK(esi, IntSpin)
 
+    /* Make sure that this interrupt isn't storming */
+    VERIFY_INT kid
+
+    /* Save the tick count */
+    mov ebx, _KeTickCount
+
     /* Call the ISR */
     mov eax, [edi+KINTERRUPT_SERVICE_CONTEXT]
     push eax
     push edi
     call [edi+KINTERRUPT_SERVICE_ROUTINE]
 
+    /* Check if the ISR timed out */
+    add ebx, _KiISRTimeout
+    cmp _KeTickCount, ebx
+    jnc IsrTimeout
+
+ReleaseLock:
     /* Release the lock */
     RELEASE_SPINLOCK(esi)
 
     /* Exit the interrupt */
-    cli
-    call _HalEndSystemInterrupt@8
-    jmp _Kei386EoiHelper@0
+    INT_EPILOG 0
 
 SpuriousInt:
     /* Exit the interrupt */
     add esp, 8
-    jmp _Kei386EoiHelper@0
+    INT_EPILOG 1
 
 #ifdef CONFIG_SMP
 IntSpin:
-    SPIN_ON_LOCK esi, GetIntLock
+    SPIN_ON_LOCK(esi, GetIntLock)
 #endif
+
+IsrTimeout:
+    /* Print warning message */
+    push [edi+KINTERRUPT_SERVICE_ROUTINE]
+    push offset _IsrTimeoutMsg
+    call _DbgPrint
+    add esp,8
+
+    /* Break into debugger, then continue */
+    int 3
+    jmp ReleaseLock
+
+    /* Cleanup verification */
+    VERIFY_INT_END kid, 0
+.endfunc
+
+.globl _KeSynchronizeExecution@12
+.func KeSynchronizeExecution@12
+_KeSynchronizeExecution@12:
+
+    /* Save EBX and put the interrupt object in it */
+    push ebx
+    mov ebx, [esp+8]
+
+    /* Go to DIRQL */
+    mov cl, [ebx+KINTERRUPT_SYNCHRONIZE_IRQL]
+    call @KfRaiseIrql@4
+    push eax
+
+#ifdef CONFIG_SMP
+    /* Acquire the interrupt spinlock FIXME: Write this in assembly */
+    mov ecx, [ebx+KINTERRUPT_ACTUAL_LOCK]
+    call @KefAcquireSpinLockAtDpcLevel@4
+#endif
+
+    /* Call the routine */
+    push [esp+20]
+    call [esp+20]
+
+#ifdef CONFIG_SMP
+    /* Release the interrupt spinlock FIXME: Write this in assembly */
+    push eax
+    mov ecx, [ebx+KINTERRUPT_ACTUAL_LOCK]
+    call @KefReleaseSpinLockFromDpcLevel@4
+    pop eax
+#endif
+
+    /* Lower IRQL */
+    mov ebx, eax
+    pop ecx
+    call @KfLowerIrql@4
+
+    /* Return status */
+    mov eax, ebx
+    pop ebx
+    ret 12
+.endfunc
+
+/*++
+ * Kii386SpinOnSpinLock 
+ *
+ *     FILLMEIN
+ *
+ * Params:
+ *     SpinLock - FILLMEIN
+ *
+ *     Flags - FILLMEIN
+ *
+ * Returns:
+ *     None.
+ *
+ * Remarks:
+ *     FILLMEIN
+ *
+ *--*/
+.globl _Kii386SpinOnSpinLock@8
+.func Kii386SpinOnSpinLock@8
+_Kii386SpinOnSpinLock@8:
+
+#ifdef CONFIG_SMP
+    /* FIXME: TODO */
+    int 3
+#endif
+
+    ret 8
 .endfunc