- Update KTHREAD and KUSER_SHARED_DATA to latest versions. This should make 2K3 drive...
[reactos.git] / reactos / ntoskrnl / ke / i386 / exp.c
index ac21c66..0d7d18d 100644 (file)
@@ -1,39 +1,34 @@
 /*
- *  ReactOS kernel
- *  Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS kernel
+ * FILE:            ntoskrnl/ke/i386/exp.c
+ * PURPOSE:         Handling exceptions
  *
- *  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 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.
- *
- *  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.
- */
-/*
- * PROJECT:              ReactOS kernel
- * FILE:                 ntoskrnl/ke/i386/exp.c
- * PURPOSE:              Handling exceptions
- * PROGRAMMERS:          David Welch (welch@cwcom.net)
- *                       Skywing (skywing@valhallalegends.com)
- * REVISION HISTORY:
- *              ??/??/??: Created
- *              09/12/03: KeRaiseUserException added (Skywing).
+ * PROGRAMMERS:     David Welch (welch@cwcom.net)
+ *                  Skywing (skywing@valhallalegends.com)
  */
 
 /* INCLUDES *****************************************************************/
 
 #include <ntoskrnl.h>
-#include <pseh.h>
+
 #define NDEBUG
 #include <internal/debug.h>
 
+/*
+ * FIXMES:
+ *  - Put back VEH.
+ *  - Clean up file.
+ *  - Sanitize some context fields.
+ *  - Add PSEH handler when an exception occurs in an exception (KiCopyExceptionRecord).
+ *  - Implement official stack trace functions (exported) and remove stuff here.
+ *  - Forward exceptions to user-mode debugger.
+ */
+
+VOID
+NTAPI
+Ki386AdjustEsp0(IN PKTRAP_FRAME TrapFrame);
+
 /* GLOBALS *****************************************************************/
 
 #define FLAG_IF (1<<9)
@@ -45,8 +40,8 @@
 # define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
 #endif
 
-extern void interrupt_handler2e(void);
-extern void interrupt_handler2d(void);
+extern void KiSystemService(void);
+extern void KiDebugService(void);
 
 extern VOID KiTrap0(VOID);
 extern VOID KiTrap1(VOID);
@@ -99,7 +94,7 @@ static char *ExceptionTypeStrings[] =
     "SIMD Fault"
   };
 
-static NTSTATUS ExceptionToNtStatus[] =
+NTSTATUS ExceptionToNtStatus[] =
   {
     STATUS_INTEGER_DIVIDE_BY_ZERO,
     STATUS_SINGLE_STEP,
@@ -108,7 +103,7 @@ static NTSTATUS ExceptionToNtStatus[] =
     STATUS_INTEGER_OVERFLOW,
     STATUS_ARRAY_BOUNDS_EXCEEDED,
     STATUS_ILLEGAL_INSTRUCTION,
-    STATUS_ACCESS_VIOLATION, /* STATUS_FLT_INVALID_OPERATION */
+    STATUS_FLOAT_INVALID_OPERATION,
     STATUS_ACCESS_VIOLATION,
     STATUS_ACCESS_VIOLATION,
     STATUS_ACCESS_VIOLATION,
@@ -117,49 +112,47 @@ static NTSTATUS ExceptionToNtStatus[] =
     STATUS_ACCESS_VIOLATION,
     STATUS_ACCESS_VIOLATION,
     STATUS_ACCESS_VIOLATION, /* RESERVED */
-    STATUS_ACCESS_VIOLATION, /* STATUS_FLT_INVALID_OPERATION */
+    STATUS_FLOAT_INVALID_OPERATION, /* Should not be used, the FPU can give more specific info */
     STATUS_DATATYPE_MISALIGNMENT,
     STATUS_ACCESS_VIOLATION,
-    STATUS_ACCESS_VIOLATION  /* STATUS_FLT_MULTIPLE_TRAPS? */
+    STATUS_FLOAT_MULTIPLE_TRAPS,
   };
 
 /* FUNCTIONS ****************************************************************/
 
-#if defined(DBG) || defined(KDBG)
 BOOLEAN STDCALL
-KeRosPrintAddress(PVOID address)
-{
-  return KdbSymPrintAddress(address);
-}
-#else /* KDBG */
-BOOLEAN STDCALL
-KeRosPrintAddress(PVOID address)
+KiRosPrintAddress(PVOID address)
 {
    PLIST_ENTRY current_entry;
-   MODULE_TEXT_SECTION* current;
-   extern LIST_ENTRY ModuleTextListHead;
+   PLDR_DATA_TABLE_ENTRY current;
+   extern LIST_ENTRY ModuleListHead;
    ULONG_PTR RelativeAddress;
+   ULONG i = 0;
 
-   current_entry = ModuleTextListHead.Flink;
-
-   while (current_entry != &ModuleTextListHead &&
-         current_entry != NULL)
-     {
-       current =
-         CONTAINING_RECORD(current_entry, MODULE_TEXT_SECTION, ListEntry);
+   do
+   {
+     current_entry = ModuleListHead.Flink;
+
+     while (current_entry != &ModuleListHead)
+       {
+          current =
+            CONTAINING_RECORD(current_entry, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList);
+
+          if (address >= (PVOID)current->DllBase &&
+              address < (PVOID)((ULONG_PTR)current->DllBase + current->SizeOfImage))
+            {
+              RelativeAddress = (ULONG_PTR) address - (ULONG_PTR) current->DllBase;
+              DbgPrint("<%wZ: %x>", &current->FullDllName, RelativeAddress);
+              return(TRUE);
+            }
+          current_entry = current_entry->Flink;
+       }
+
+     address = (PVOID)((ULONG_PTR)address & ~(ULONG_PTR)MmSystemRangeStart);
+   } while(++i <= 1);
 
-       if (address >= (PVOID)current->Base &&
-           address < (PVOID)(current->Base + current->Length))
-         {
-            RelativeAddress = (ULONG_PTR) address - current->Base;
-           DbgPrint("<%ws: %x>", current->Name, RelativeAddress);
-           return(TRUE);
-         }
-       current_entry = current_entry->Flink;
-     }
    return(FALSE);
 }
-#endif /* KDBG */
 
 ULONG
 KiKernelTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr, PVOID Cr2)
@@ -190,16 +183,15 @@ KiKernelTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr, PVOID Cr2)
       Er.NumberParameters = 0;
     }
 
-  Er.ExceptionFlags = ((NTSTATUS) STATUS_SINGLE_STEP == (NTSTATUS) Er.ExceptionCode
-    || (NTSTATUS) STATUS_BREAKPOINT == (NTSTATUS) Er.ExceptionCode) ?
-    EXCEPTION_NONCONTINUABLE : 0;
+  /* FIXME: Which exceptions are noncontinuable? */
+  Er.ExceptionFlags = 0;
 
-  KiDispatchException(&Er, 0, Tf, KernelMode, TRUE);
+  KiDispatchException(&Er, NULL, Tf, KernelMode, TRUE);
 
   return(0);
 }
 
-ULONG
+VOID
 KiDoubleFaultHandler(VOID)
 {
   unsigned int cr2;
@@ -266,7 +258,7 @@ KiDoubleFaultHandler(VOID)
    if (PsGetCurrentProcess() != NULL)
      {
        DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId);
-       DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName);
+       DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName);
      }
    if (PsGetCurrentThread() != NULL)
      {
@@ -279,7 +271,7 @@ KiDoubleFaultHandler(VOID)
            OldTss->Fs, OldTss->Gs);
    DbgPrint("EAX: %.8x   EBX: %.8x   ECX: %.8x\n", OldTss->Eax, OldTss->Ebx,
            OldTss->Ecx);
-   DbgPrint("EDX: %.8x   EBP: %.8x   ESI: %.8x\n   ESP: %.8x", OldTss->Edx,
+   DbgPrint("EDX: %.8x   EBP: %.8x   ESI: %.8x\nESP: %.8x ", OldTss->Edx,
            OldTss->Ebp, OldTss->Esi, Esp0);
    DbgPrint("EDI: %.8x   EFLAGS: %.8x ", OldTss->Edi, OldTss->Eflags);
    if (OldTss->Cs == KERNEL_CS)
@@ -305,8 +297,8 @@ KiDoubleFaultHandler(VOID)
        }
       else
        {
-         StackLimit = (ULONG)&init_stack_top;
-         StackBase = (ULONG)&init_stack;
+         StackLimit = (ULONG)init_stack_top;
+         StackBase = (ULONG)init_stack;
        }
 
       /*
@@ -320,7 +312,7 @@ KiDoubleFaultHandler(VOID)
        {
          KeRosPrintAddress((PVOID)Frame[1]);
          Frame = (PULONG)Frame[0];
-          DbgPrint(" ");
+          DbgPrint("\n");
        }
 #else
       DbgPrint("Frames: ");
@@ -405,16 +397,14 @@ KiDoubleFaultHandler(VOID)
 
    DbgPrint("\n");
    for(;;);
-   return 0;
 }
 
 VOID
+NTAPI
 KiDumpTrapFrame(PKTRAP_FRAME Tf, ULONG Parameter1, ULONG Parameter2)
 {
   ULONG cr3_;
-  ULONG i;
   ULONG StackLimit;
-  PULONG Frame;
   ULONG Esp0;
   ULONG ExceptionNr = (ULONG)Tf->DebugArgMark;
   ULONG cr2 = (ULONG)Tf->DebugPointer;
@@ -443,7 +433,7 @@ KiDumpTrapFrame(PKTRAP_FRAME Tf, ULONG Parameter1, ULONG Parameter2)
    if (PsGetCurrentProcess() != NULL)
      {
        DbgPrint("Pid: %x <", PsGetCurrentProcess()->UniqueProcessId);
-       DbgPrint("%.8s> ", PsGetCurrentProcess()->ImageFileName);
+       DbgPrint("%.16s> ", PsGetCurrentProcess()->ImageFileName);
      }
    if (PsGetCurrentThread() != NULL)
      {
@@ -475,52 +465,13 @@ KiDumpTrapFrame(PKTRAP_FRAME Tf, ULONG Parameter1, ULONG Parameter2)
      }
    else
      {
-       StackLimit = (ULONG)&init_stack_top;
+       StackLimit = (ULONG)init_stack_top;
      }
 
    /*
     * Dump the stack frames
     */
-   DbgPrint("Frames: ");
-   /* Change to an #if 0 if no frames are printed because of fpo. */
-#if 1
-   i = 1;
-   Frame = (PULONG)Tf->Ebp;
-   while (Frame != NULL)
-     {
-       NTSTATUS Status;
-       PVOID Eip;
-       Status = MmSafeCopyFromUser(&Eip, Frame + 1, sizeof(Eip));
-       if (!NT_SUCCESS(Status))
-        {
-          DbgPrint("<INVALID>");
-          break;
-        }
-       if (!KeRosPrintAddress(Eip))
-        {
-          DbgPrint("<%X>", Eip);
-        }
-       Status = MmSafeCopyFromUser(&Frame, Frame, sizeof(Frame));
-       if (!NT_SUCCESS(Status))
-        {
-          break;
-        }
-       i++;
-       DbgPrint(" ");
-     }
-#else
-   i = 1;
-   Frame = (PULONG)((ULONG_PTR)Esp0 + KTRAP_FRAME_EFLAGS);
-   while (Frame < (PULONG)PsGetCurrentThread()->Tcb.StackBase && i < 50)
-     {
-        ULONG Address = *Frame;
-        if (KeRosPrintAddress((PVOID)Address))
-          {
-            i++;
-          }
-        Frame++;
-     }
-#endif
+   KeDumpStackFrames((PULONG)Tf->Ebp);
 }
 
 ULONG
@@ -532,30 +483,28 @@ KiTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr)
  *        Complete CPU context
  */
 {
-   unsigned int cr2;
+   ULONG_PTR cr2;
    NTSTATUS Status;
    ULONG Esp0;
 
+   ASSERT(ExceptionNr != 14);
+
    /* Store the exception number in an unused field in the trap frame. */
-   Tf->DebugArgMark = (PVOID)ExceptionNr;
+   Tf->DebugArgMark = ExceptionNr;
 
    /* Use the address of the trap frame as approximation to the ring0 esp */
    Esp0 = (ULONG)&Tf->Eip;
 
    /* Get CR2 */
    cr2 = Ke386GetCr2();
-   Tf->DebugPointer = (PVOID)cr2;
-
-   if (ExceptionNr == 14 && Tf->Eflags & FLAG_IF)
-   {
-     Ke386EnableInterrupts();
-   }
+   Tf->DebugPointer = cr2;
 
    /*
     * If this was a V86 mode exception then handle it specially
     */
    if (Tf->Eflags & (1 << 17))
      {
+       DPRINT("Tf->Eflags, %x, Tf->Eip %x, ExceptionNr: %d\n", Tf->Eflags, Tf->Eip, ExceptionNr);
        return(KeV86Exception(ExceptionNr, Tf, cr2));
      }
 
@@ -565,29 +514,24 @@ KiTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr)
    if (PsGetCurrentThread() != NULL &&
        Esp0 < (ULONG)PsGetCurrentThread()->Tcb.StackLimit)
      {
-       DbgPrint("Stack underflow (tf->esp %x Limit %x)\n",
-                Esp0, (ULONG)PsGetCurrentThread()->Tcb.StackLimit);
+       DPRINT1("Stack underflow (tf->esp %x Limit %x Eip %x)\n",
+               Esp0, (ULONG)PsGetCurrentThread()->Tcb.StackLimit, Tf->Eip);
        ExceptionNr = 12;
      }
 
-   /*
-    * Maybe handle the page fault and return
-    */
-   if (ExceptionNr == 14)
+   if (ExceptionNr == 15)
      {
-        if (Ke386NoExecute && Tf->ErrorCode & 0x10 && cr2 >= KERNEL_BASE)
-       {
-           KEBUGCHECKWITHTF(ATTEMPTED_EXECUTE_OF_NOEXECUTE_MEMORY, 0, 0, 0, 0, Tf);
-       }
-       Status = MmPageFault(Tf->Cs&0xffff,
-                            &Tf->Eip,
-                            &Tf->Eax,
-                            cr2,
-                            Tf->ErrorCode);
-       if (NT_SUCCESS(Status))
-         {
-            return(0);
-         }
+       /*
+        * FIXME:
+        *   This exception should never occur. The P6 has a bug, which does sometimes deliver
+        *   the apic spurious interrupt as exception 15. On an athlon64, I get one exception
+        *   in the early boot phase in apic mode (using the smp build). I've looked to the linux
+        *   sources. Linux does ignore this exception.
+        *
+        *   Hartmut Birr
+        */
+       DPRINT1("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
+       return(0);
      }
 
    /*
@@ -627,7 +571,364 @@ KiTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr)
     }
 }
 
+ULONG
+NTAPI
+KiEspFromTrapFrame(IN PKTRAP_FRAME TrapFrame)
+{
+    /* Check if this is user-mode or V86 */
+    if ((TrapFrame->Cs & 1) || (TrapFrame->Eflags & X86_EFLAGS_VM))
+    {
+        /* Return it directly */
+        return TrapFrame->Esp;
+    }
+    else
+    {
+        /* Edited frame */
+        if (!(TrapFrame->Cs & FRAME_EDITED))
+        {
+            /* Return edited value */
+            return TrapFrame->TempEsp;
+        }
+        else
+        {
+            /* Virgin frame, calculate */
+            return (ULONG)&TrapFrame->Esp;
+        }
+    }
+}
+
 VOID
+NTAPI
+KiEspToTrapFrame(IN PKTRAP_FRAME TrapFrame,
+                 IN ULONG Esp)
+{
+    ULONG Previous = KiEspFromTrapFrame(TrapFrame);
+
+    /* Check if this is user-mode or V86 */
+    if ((TrapFrame->Cs & 1) || (TrapFrame->Eflags & X86_EFLAGS_VM))
+    {
+        /* Write it directly */
+        TrapFrame->Esp = Esp;
+    }
+    else
+    {
+        /* Don't allow ESP to be lowered, this is illegal */
+        if (Esp < Previous)
+        {
+            KeBugCheck(SET_OF_INVALID_CONTEXT);
+        }
+
+        /* Create an edit frame, check if it was alrady */
+        if (!(TrapFrame->Cs & FRAME_EDITED))
+        {
+            /* Update the value */
+            TrapFrame->TempEsp = Esp;
+        }
+        else
+        {
+            /* Check if ESP changed */
+            if (Previous != Esp)
+            {
+                /* Save CS */
+                TrapFrame->TempCs = TrapFrame->Cs;
+                TrapFrame->Cs &= ~FRAME_EDITED;
+
+                /* Save ESP */
+                TrapFrame->TempEsp = Esp;
+            }
+        }
+    }
+}
+
+ULONG
+NTAPI
+KiSsFromTrapFrame(IN PKTRAP_FRAME TrapFrame)
+{
+    /* If this was V86 Mode */
+    if (TrapFrame->Eflags & X86_EFLAGS_VM)
+    {
+        /* Just return it */
+        return TrapFrame->Ss;
+    }
+    else if (TrapFrame->Cs & 1)
+    {
+        /* Usermode, return the User SS */
+        return TrapFrame->Ss | 3;
+    }
+    else
+    {
+        /* Kernel mode */
+        return KERNEL_DS;
+    }
+}
+
+VOID
+NTAPI
+KiSsToTrapFrame(IN PKTRAP_FRAME TrapFrame,
+                IN ULONG Ss)
+{
+    /* Remove the high-bits */
+    Ss &= 0xFFFF;
+
+    /* If this was V86 Mode */
+    if (TrapFrame->Eflags & X86_EFLAGS_VM)
+    {
+        /* Just write it */
+        TrapFrame->Ss = Ss;
+    }
+    else if (TrapFrame->Cs & 1)
+    {
+        /* Usermode, save the User SS */
+        TrapFrame->Ss = Ss | 3;
+    }
+}
+
+BOOLEAN
+NTAPI
+KeContextToTrapFrame(IN PCONTEXT Context,
+                     IN OUT PKEXCEPTION_FRAME ExceptionFrame,
+                     IN OUT PKTRAP_FRAME TrapFrame,
+                     IN KPROCESSOR_MODE PreviousMode)
+{
+    BOOLEAN V86Switch = FALSE;
+
+    /* Start with the basic Registers */
+    if ((Context->ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL)
+    {
+        /* Check if we went through a V86 switch */
+        if ((Context->EFlags & X86_EFLAGS_VM) !=
+            (TrapFrame->Eflags & X86_EFLAGS_VM))
+        {
+            /* We did, remember this for later */
+            V86Switch = TRUE;
+        }
+
+        /* Copy EFLAGS. FIXME: Needs to be sanitized */
+        TrapFrame->Eflags = Context->EFlags;
+
+        /* Copy EBP and EIP */
+        TrapFrame->Ebp = Context->Ebp;
+        TrapFrame->Eip = Context->Eip;
+
+        /* Check if we were in V86 Mode */
+        if (TrapFrame->Eflags & X86_EFLAGS_VM)
+        {
+            /* Simply copy the CS value */
+            TrapFrame->Cs = Context->SegCs;
+        }
+        else
+        {
+            /* We weren't in V86, so sanitize the CS (FIXME!) */
+            TrapFrame->Cs = Context->SegCs;
+
+            /* Don't let it under 8, that's invalid */
+            if ((PreviousMode !=KernelMode) && (TrapFrame->Cs < 8))
+            {
+                /* Force it to User CS */
+                TrapFrame->Cs = USER_CS;
+            }
+        }
+
+        /* Handle SS Specially for validation */
+        KiSsToTrapFrame(TrapFrame, Context->SegSs);
+
+        /* Write ESP back; take into account Edited Trap Frames */
+        KiEspToTrapFrame(TrapFrame, Context->Esp);
+
+        /* Handle our V86 Bias if we went through a switch */
+        if (V86Switch) Ki386AdjustEsp0(TrapFrame);
+    }
+
+    /* Process the Integer Registers */
+    if ((Context->ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER)
+    {
+        TrapFrame->Eax = Context->Eax;
+        TrapFrame->Ebx = Context->Ebx;
+        TrapFrame->Ecx = Context->Ecx;
+        TrapFrame->Edx = Context->Edx;
+        TrapFrame->Esi = Context->Esi;
+        TrapFrame->Edi = Context->Edi;
+    }
+
+    /* Process the Context Segments */
+    if ((Context->ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS)
+    {
+        /* Check if we were in V86 Mode */
+        if (TrapFrame->Eflags & X86_EFLAGS_VM)
+        {
+            /* Copy the V86 Segments directlry */
+            TrapFrame->V86_Ds = Context->SegDs;
+            TrapFrame->V86_Es = Context->SegEs;
+            TrapFrame->V86_Fs = Context->SegFs;
+            TrapFrame->V86_Gs = Context->SegGs;
+        }
+        else if (!(TrapFrame->Cs & 1))
+        {
+            /* For user mode, write the values directly */
+            TrapFrame->Ds = USER_DS;
+            TrapFrame->Es = USER_DS;
+            TrapFrame->Fs = Context->SegFs;
+            TrapFrame->Gs = 0;
+        }
+        else
+        {
+            /* For kernel-mode, return the values */
+            TrapFrame->Ds = Context->SegDs;
+            TrapFrame->Es = Context->SegEs;
+            TrapFrame->Fs = Context->SegFs;
+
+            /* Handle GS specially */
+            if (TrapFrame->Cs == USER_CS)
+            {
+                /* Don't use it, if user */
+                TrapFrame->Gs = 0;
+            }
+            else
+            {
+                /* Copy it if kernel */
+                TrapFrame->Gs = Context->SegGs;
+            }
+        }
+    }
+
+    /* Handle the Debug Registers */
+    if ((Context->ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS)
+    {
+        /* FIXME: All these should be sanitized */
+        TrapFrame->Dr0 = Context->Dr0;
+        TrapFrame->Dr1 = Context->Dr1;
+        TrapFrame->Dr2 = Context->Dr2;
+        TrapFrame->Dr3 = Context->Dr3;
+        TrapFrame->Dr6 = Context->Dr6;
+        TrapFrame->Dr7 = Context->Dr7;
+
+        /* Check if usermode */
+        if (PreviousMode != KernelMode)
+        {
+            /* Set the Debug Flag */
+            KeGetCurrentThread()->DispatcherHeader.DebugActive = (Context->Dr7 & DR7_ACTIVE);
+        }
+    }
+
+    /* Handle FPU and Extended Registers */
+    return KiContextToFxSaveArea((PFX_SAVE_AREA)(TrapFrame + 1), Context);
+}
+
+VOID
+NTAPI
+KeTrapFrameToContext(IN PKTRAP_FRAME TrapFrame,
+                     IN PKEXCEPTION_FRAME ExceptionFrame,
+                     IN OUT PCONTEXT Context)
+{
+    PFX_SAVE_AREA FxSaveArea = NULL;
+
+    /* Start with the Control flags */
+    if ((Context->ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL)
+    {
+        /* EBP, EIP and EFLAGS */
+        Context->Ebp = TrapFrame->Ebp;
+        Context->Eip = TrapFrame->Eip;
+        Context->EFlags = TrapFrame->Eflags;
+
+        /* Return the correct CS */
+        if (!(TrapFrame->Cs & FRAME_EDITED) &&
+            !(TrapFrame->Eflags & X86_EFLAGS_VM))
+        {
+            /* Get it from the Temp location */
+            Context->SegCs = TrapFrame->TempCs & 0xFFFF;
+        }
+        else
+        {
+            /* Return it directly */
+            Context->SegCs = TrapFrame->Cs & 0xFFFF;
+        }
+
+        /* Get the Ss and ESP */
+        Context->SegSs = KiSsFromTrapFrame(TrapFrame);
+        Context->Esp = KiEspFromTrapFrame(TrapFrame);
+    }
+
+    /* Handle the Segments */
+    if ((Context->ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS)
+    {
+        /* Do V86 Mode first */
+        if (TrapFrame->Eflags & X86_EFLAGS_VM)
+        {
+            /* Return from the V86 location */
+            Context->SegGs = TrapFrame->V86_Gs & 0xFFFF;
+            Context->SegFs = TrapFrame->V86_Fs & 0xFFFF;
+            Context->SegEs = TrapFrame->V86_Es & 0xFFFF;
+            Context->SegDs = TrapFrame->V86_Ds & 0xFFFF;
+        }
+        else
+        {
+            /* Check if this was a Kernel Trap */
+            if (TrapFrame->Cs == KERNEL_CS)
+            {
+                /* Set valid selectors */
+                TrapFrame->Gs = 0;
+                TrapFrame->Fs = PCR_SELECTOR;
+                TrapFrame->Es = USER_DS;
+                TrapFrame->Ds = USER_DS;
+            }
+
+            /* Return the segments */
+            Context->SegGs = TrapFrame->Gs & 0xFFFF;
+            Context->SegFs = TrapFrame->Fs & 0xFFFF;
+            Context->SegEs = TrapFrame->Es & 0xFFFF;
+            Context->SegDs = TrapFrame->Ds & 0xFFFF;
+        }
+    }
+
+    /* Handle the simple registers */
+    if ((Context->ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER)
+    {
+        /* Return them directly */
+        Context->Eax = TrapFrame->Eax;
+        Context->Ebx = TrapFrame->Ebx;
+        Context->Ecx = TrapFrame->Ecx;
+        Context->Edx = TrapFrame->Edx;
+        Context->Esi = TrapFrame->Esi;
+        Context->Edi = TrapFrame->Edi;
+    }
+
+    if ((Context->ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS)
+    {
+        /*
+         * FIXME: Implement this case
+         */
+        Context->ContextFlags &= (~CONTEXT_DEBUG_REGISTERS) | CONTEXT_i386;
+    }
+    if ((Context->ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT)
+    {
+        FxSaveArea = KiGetFpuState(KeGetCurrentThread());
+        if (FxSaveArea != NULL)
+        {
+            KiFxSaveAreaToFloatingSaveArea(&Context->FloatSave, FxSaveArea);
+        }
+        else
+        {
+            Context->ContextFlags &= (~CONTEXT_FLOATING_POINT) | CONTEXT_i386;
+        }
+    }
+    if ((Context->ContextFlags & CONTEXT_EXTENDED_REGISTERS) == CONTEXT_EXTENDED_REGISTERS)
+    {
+        if (FxSaveArea == NULL)
+            FxSaveArea = KiGetFpuState(KeGetCurrentThread());
+        if (FxSaveArea != NULL)
+        {
+            memcpy(Context->ExtendedRegisters, &FxSaveArea->U.FxArea,
+                   min(sizeof (Context->ExtendedRegisters), sizeof (FxSaveArea->U.FxArea)) );
+        }
+        else
+        {
+            Context->ContextFlags &= (~CONTEXT_EXTENDED_REGISTERS) | CONTEXT_i386;
+        }
+    }
+}
+
+VOID
+NTAPI
 KeDumpStackFrames(PULONG Frame)
 {
        PULONG StackBase, StackEnd;
@@ -635,7 +936,7 @@ KeDumpStackFrames(PULONG Frame)
        ULONG ResultLength = sizeof(mbi);
        NTSTATUS Status;
 
-       DbgPrint("Frames: ");
+       DbgPrint("Frames:\n");
        _SEH_TRY
        {
                Status = MiQueryVirtualMemory (
@@ -647,12 +948,12 @@ KeDumpStackFrames(PULONG Frame)
                        &ResultLength );
                if ( !NT_SUCCESS(Status) )
                {
-                       DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status );
+                       DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status );
                        return;
                }
 
                StackBase = Frame;
-               StackEnd = mbi.BaseAddress + mbi.RegionSize;
+               StackEnd = (PULONG)((ULONG_PTR)mbi.BaseAddress + mbi.RegionSize);
 
                while ( Frame >= StackBase && Frame < StackEnd )
                {
@@ -663,7 +964,7 @@ KeDumpStackFrames(PULONG Frame)
                                break;
                        StackBase = Frame;
                        Frame = (PULONG)Frame[0];
-                       DbgPrint(" ");
+                       DbgPrint("\n");
                }
        }
        _SEH_HANDLE
@@ -688,7 +989,7 @@ KeRosDumpStackFrames ( PULONG Frame, ULONG FrameCount )
                if ( !Frame )
                {
 #if defined __GNUC__
-                       __asm__("mov %%ebp, %%ebx" : "=b" (Frame) : );
+                       __asm__("mov %%ebp, %0" : "=r" (Frame) : );
 #elif defined(_MSC_VER)
                        __asm mov [Frame], ebp
 #endif
@@ -704,12 +1005,12 @@ KeRosDumpStackFrames ( PULONG Frame, ULONG FrameCount )
                        &ResultLength );
                if ( !NT_SUCCESS(Status) )
                {
-                       DPRINT1("Can't dump stack frames: NtQueryVirtualMemory() failed: %x\n", Status );
+                       DPRINT1("Can't dump stack frames: MiQueryVirtualMemory() failed: %x\n", Status );
                        return;
                }
 
                StackBase = Frame;
-               StackEnd = mbi.BaseAddress + mbi.RegionSize;
+               StackEnd = (PULONG)((ULONG_PTR)mbi.BaseAddress + mbi.RegionSize);
 
                while ( Frame >= StackBase && Frame < StackEnd && i++ < FrameCount )
                {
@@ -742,7 +1043,7 @@ KeRosGetStackFrames ( PULONG Frames, ULONG FrameCount )
        _SEH_TRY
        {
 #if defined __GNUC__
-               __asm__("mov %%ebp, %%ebx" : "=b" (Frame) : );
+               __asm__("mov %%ebp, %0" : "=r" (Frame) : );
 #elif defined(_MSC_VER)
                __asm mov [Frame], ebp
 #endif
@@ -756,12 +1057,12 @@ KeRosGetStackFrames ( PULONG Frames, ULONG FrameCount )
                        &ResultLength );
                if ( !NT_SUCCESS(Status) )
                {
-                       DPRINT1("Can't get stack frames: NtQueryVirtualMemory() failed: %x\n", Status );
+                       DPRINT1("Can't get stack frames: MiQueryVirtualMemory() failed: %x\n", Status );
                        return 0;
                }
 
                StackBase = Frame;
-               StackEnd = mbi.BaseAddress + mbi.RegionSize;
+               StackEnd = (PULONG)((ULONG_PTR)mbi.BaseAddress + mbi.RegionSize);
 
                while ( Count < FrameCount && Frame >= StackBase && Frame < StackEnd )
                {
@@ -811,7 +1112,9 @@ set_task_gate(unsigned int sel, unsigned task_sel)
   KiIdt[sel].b = 0x8500;
 }
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
+NTAPI
 KeInitExceptions(VOID)
 /*
  * FUNCTION: Initalize CPU exception handling
@@ -850,52 +1153,189 @@ KeInitExceptions(VOID)
         set_trap_gate(i,(int)KiTrapUnknown, 0);
      }
 
-   set_system_call_gate(0x2d,(int)interrupt_handler2d);
-   set_system_call_gate(0x2e,(int)interrupt_handler2e);
+   set_system_call_gate(0x2d,(int)KiDebugService);
+   set_system_call_gate(0x2e,(int)KiSystemService);
+}
+
+VOID
+NTAPI
+KiDispatchException(PEXCEPTION_RECORD ExceptionRecord,
+                    PKEXCEPTION_FRAME ExceptionFrame,
+                    PKTRAP_FRAME TrapFrame,
+                    KPROCESSOR_MODE PreviousMode,
+                    BOOLEAN FirstChance)
+{
+    CONTEXT Context;
+    KD_CONTINUE_TYPE Action;
+    ULONG_PTR Stack, NewStack;
+    ULONG Size;
+    BOOLEAN UserDispatch = FALSE;
+    DPRINT("KiDispatchException() called\n");
+
+    /* Increase number of Exception Dispatches */
+    KeGetCurrentPrcb()->KeExceptionDispatchCount++;
+
+    /* Set the context flags */
+    Context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
+
+    /* Check if User Mode */
+    if (PreviousMode == UserMode)
+    {
+        extern ULONG FxsrSupport;
+        /* Add the FPU Flag */
+        Context.ContextFlags |= CONTEXT_FLOATING_POINT;
+        if (FxsrSupport)
+            Context.ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
+    }
+
+    /* Get a Context */
+    KeTrapFrameToContext(TrapFrame, ExceptionFrame, &Context);
+
+    /* Handle kernel-mode first, it's simpler */
+    if (PreviousMode == KernelMode)
+    {
+        /* Check if this is a first-chance exception */
+        if (FirstChance == TRUE)
+        {
+            /* Break into the debugger for the first time */
+            Action = KdpEnterDebuggerException(ExceptionRecord,
+                                               PreviousMode,
+                                               &Context,
+                                               TrapFrame,
+                                               TRUE,
+                                               TRUE);
+
+            /* If the debugger said continue, then continue */
+            if (Action == kdContinue) goto Handled;
+
+            /* If the Debugger couldn't handle it, dispatch the exception */
+            if (RtlDispatchException(ExceptionRecord, &Context))
+            {
+                /* It was handled by an exception handler, continue */
+                goto Handled;
+            }
+        }
+
+        /* This is a second-chance exception, only for the debugger */
+        Action = KdpEnterDebuggerException(ExceptionRecord,
+                                           PreviousMode,
+                                           &Context,
+                                           TrapFrame,
+                                           FALSE,
+                                           FALSE);
+
+        /* If the debugger said continue, then continue */
+        if (Action == kdContinue) goto Handled;
+
+        /* Third strike; you're out */
+        KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED,
+                         ExceptionRecord->ExceptionCode,
+                         (ULONG_PTR)ExceptionRecord->ExceptionAddress,
+                         ExceptionRecord->ExceptionInformation[0],
+                         ExceptionRecord->ExceptionInformation[1],
+                         TrapFrame);
+    }
+    else
+    {
+        /* User mode exception, was it first-chance? */
+        if (FirstChance)
+        {
+            /* Enter Debugger if available */
+            Action = KdpEnterDebuggerException(ExceptionRecord,
+                                               PreviousMode,
+                                               &Context,
+                                               TrapFrame,
+                                               TRUE,
+                                               TRUE);
+
+            /* Exit if we're continuing */
+            if (Action == kdContinue) goto Handled;
+
+            /* FIXME: Forward exception to user mode debugger */
+
+            /* Set up the user-stack */
+            _SEH_TRY
+            {
+                /* Align context size and get stack pointer */
+                Size = (sizeof(CONTEXT) + 3) & ~3;
+                Stack = (Context.Esp & ~3) - Size;
+                DPRINT("Stack: %lx\n", Stack);
+
+                /* Probe stack and copy Context */
+                ProbeForWrite((PVOID)Stack, Size, sizeof(ULONG));
+                RtlCopyMemory((PVOID)Stack, &Context, sizeof(CONTEXT));
+
+                /* Align exception record size and get stack pointer */
+                Size = (sizeof(EXCEPTION_RECORD) - 
+                        (EXCEPTION_MAXIMUM_PARAMETERS - ExceptionRecord->NumberParameters) *
+                        sizeof(ULONG) + 3) & ~3;
+                NewStack = Stack - Size;
+                DPRINT("NewStack: %lx\n", NewStack);
+
+                /* Probe stack and copy exception record. Don't forget to add the two params */
+                ProbeForWrite((PVOID)(NewStack - 2 * sizeof(ULONG_PTR)),
+                              Size +  2 * sizeof(ULONG_PTR),
+                              sizeof(ULONG));
+                RtlCopyMemory((PVOID)NewStack, ExceptionRecord, Size);
+
+                /* Now write the two params for the user-mode dispatcher */
+                *(PULONG_PTR)(NewStack - 1 * sizeof(ULONG_PTR)) = Stack;
+                *(PULONG_PTR)(NewStack - 2 * sizeof(ULONG_PTR)) = NewStack;
+
+                /* Set new Stack Pointer */
+                KiEspToTrapFrame(TrapFrame, NewStack - 2 * sizeof(ULONG_PTR));
+
+                /* Set EIP to the User-mode Dispathcer */
+                TrapFrame->Eip = (ULONG)KeUserExceptionDispatcher;
+                UserDispatch = TRUE;
+                _SEH_LEAVE;
+            }
+            _SEH_HANDLE
+            {
+                /* Do second-chance */
+            }
+            _SEH_END;
+        }
+
+        /* If we dispatch to user, return now */
+        if (UserDispatch) return;
+
+        /* FIXME: Forward the exception to the debugger for 2nd chance */
+
+        /* 3rd strike, kill the thread */
+        DPRINT1("Unhandled UserMode exception, terminating thread\n");
+        ZwTerminateThread(NtCurrentThread(), ExceptionRecord->ExceptionCode);
+        KEBUGCHECKWITHTF(KMODE_EXCEPTION_NOT_HANDLED,
+                         ExceptionRecord->ExceptionCode,
+                         (ULONG_PTR)ExceptionRecord->ExceptionAddress,
+                         ExceptionRecord->ExceptionInformation[0],
+                         ExceptionRecord->ExceptionInformation[1],
+                         TrapFrame);
+    }
+
+Handled:
+    /* Convert the context back into Trap/Exception Frames */
+    KeContextToTrapFrame(&Context, NULL, TrapFrame, PreviousMode);
+    return;
 }
 
 /*
  * @implemented
  */
-
 NTSTATUS STDCALL
 KeRaiseUserException(IN NTSTATUS ExceptionCode)
 {
-   /* FIXME: This needs SEH */
    ULONG OldEip;
    PKTHREAD Thread = KeGetCurrentThread();
 
-   ProbeForWrite(&Thread->Teb->ExceptionCode, sizeof(NTSTATUS), sizeof(NTSTATUS)); /* NT doesn't check this -- bad? */
+    _SEH_TRY {
+        Thread->Teb->ExceptionCode = ExceptionCode;
+    } _SEH_HANDLE {
+        return(ExceptionCode);
+    } _SEH_END;
+
    OldEip = Thread->TrapFrame->Eip;
-   Thread->TrapFrame->Eip = (ULONG_PTR)LdrpGetSystemDllRaiseExceptionDispatcher();
-   Thread->Teb->ExceptionCode = ExceptionCode;
+   Thread->TrapFrame->Eip = (ULONG_PTR)KeRaiseUserExceptionDispatcher;
    return((NTSTATUS)OldEip);
 }
 
-VOID
-FASTCALL
-KeRosTrapReturn ( PKTRAP_FRAME TrapFrame, PKTRAP_FRAME PrevTrapFrame );
-
-/*
- * @implemented
- */
-NTSTATUS STDCALL
-NtRaiseException (
-       IN PEXCEPTION_RECORD ExceptionRecord,
-       IN PCONTEXT Context,
-       IN BOOLEAN SearchFrames)
-{
-       PKTRAP_FRAME TrapFrame = KeGetCurrentThread()->TrapFrame;
-       PKTRAP_FRAME PrevTrapFrame = (PKTRAP_FRAME)TrapFrame->Edx;
-
-       KeGetCurrentKPCR()->Tib.ExceptionList = TrapFrame->ExceptionList;
-
-       KiDispatchException(ExceptionRecord,
-               Context,
-               PsGetCurrentThread()->Tcb.TrapFrame,
-               (KPROCESSOR_MODE)ExGetPreviousMode(),
-               SearchFrames);
-
-       KeRosTrapReturn ( TrapFrame, PrevTrapFrame );
-       return(STATUS_SUCCESS);
-}