Add missing processor architecture cases
[reactos.git] / reactos / ntoskrnl / kdbg / kdb.c
index 95b94f1..49e68ef 100644 (file)
@@ -3,10 +3,10 @@
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/kdbg/kdb.c
  * PURPOSE:         Kernel Debugger
- * 
+ *
  * PROGRAMMERS:     Gregor Anich
  */
-
+      
 /* INCLUDES ******************************************************************/
 
 #include <ntoskrnl.h>
@@ -104,8 +104,62 @@ STATIC CONST PCHAR ExceptionNrToString[] =
    "SIMD Fault"
 };
 
+ULONG
+NTAPI
+KiSsFromTrapFrame(IN PKTRAP_FRAME TrapFrame);
+
+ULONG
+NTAPI
+KiEspFromTrapFrame(IN PKTRAP_FRAME TrapFrame);
+
+VOID
+NTAPI
+KiSsToTrapFrame(IN PKTRAP_FRAME TrapFrame,
+                IN ULONG Ss);
+
+VOID
+NTAPI
+KiEspToTrapFrame(IN PKTRAP_FRAME TrapFrame,
+                 IN ULONG Esp);
+
 /* FUNCTIONS *****************************************************************/
 
+STATIC VOID
+KdbpTrapFrameToKdbTrapFrame(PKTRAP_FRAME TrapFrame, PKDB_KTRAP_FRAME KdbTrapFrame)
+{
+   /* Copy the TrapFrame only up to Eflags and zero the rest*/
+   RtlCopyMemory(&KdbTrapFrame->Tf, TrapFrame, FIELD_OFFSET(KTRAP_FRAME, HardwareEsp));
+   RtlZeroMemory((PVOID)((ULONG_PTR)&KdbTrapFrame->Tf + FIELD_OFFSET(KTRAP_FRAME, HardwareEsp)),
+                 sizeof (KTRAP_FRAME) - FIELD_OFFSET(KTRAP_FRAME, HardwareEsp));
+   asm volatile(
+      "movl %%cr0, %0"    "\n\t"
+      "movl %%cr2, %1"    "\n\t"
+      "movl %%cr3, %2"    "\n\t"
+      "movl %%cr4, %3"    "\n\t"
+      : "=r"(KdbTrapFrame->Cr0), "=r"(KdbTrapFrame->Cr2),
+        "=r"(KdbTrapFrame->Cr3), "=r"(KdbTrapFrame->Cr4));
+
+    KdbTrapFrame->Tf.HardwareEsp = KiEspFromTrapFrame(TrapFrame);
+    KdbTrapFrame->Tf.HardwareSegSs = (USHORT)(KiSsFromTrapFrame(TrapFrame) & 0xFFFF);
+
+
+   /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
+}
+
+STATIC VOID
+KdbpKdbTrapFrameToTrapFrame(PKDB_KTRAP_FRAME KdbTrapFrame, PKTRAP_FRAME TrapFrame)
+{
+   /* Copy the TrapFrame only up to Eflags and zero the rest*/
+   RtlCopyMemory(TrapFrame, &KdbTrapFrame->Tf, FIELD_OFFSET(KTRAP_FRAME, HardwareEsp));
+   
+   /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */
+   
+    KiSsToTrapFrame(TrapFrame, KdbTrapFrame->Tf.HardwareSegSs);
+    KiEspToTrapFrame(TrapFrame, KdbTrapFrame->Tf.HardwareEsp);
+   
+   /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */
+}
+
 /*!\brief Overwrites the instruction at \a Address with \a NewInst and stores
  *        the old instruction in *OldInst.
  *
@@ -130,17 +184,17 @@ KdbpOverwriteInstruction(
 
    /* Get the protection for the address. */
    Protect = MmGetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address));
-   
+
    /* Return if that page isn't present. */
    if (Protect & PAGE_NOACCESS)
    {
       return STATUS_MEMORY_NOT_ALLOCATED;
    }
-   
+
    /* Attach to the process */
    if (CurrentProcess != Process)
    {
-      KeStackAttachProcess(EPROCESS_TO_KPROCESS(Process), &ApcState);
+      KeStackAttachProcess(&Process->Pcb, &ApcState);
    }
 
    /* Make the page writeable if it is read only. */
@@ -149,7 +203,7 @@ KdbpOverwriteInstruction(
       MmSetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address),
                       (Protect & ~(PAGE_READONLY|PAGE_EXECUTE|PAGE_EXECUTE_READ)) | PAGE_READWRITE);
    }
-   
+
    /* Copy the old instruction back to the caller. */
    if (OldInst != NULL)
    {
@@ -168,16 +222,16 @@ KdbpOverwriteInstruction(
         return Status;
       }
    }
-   
+
    /* Copy the new instruction in its place. */
    Status = KdbpSafeWriteMemory((PUCHAR)Address, &NewInst, 1);
-   
+
    /* Restore the page protection. */
    if (Protect & (PAGE_READONLY|PAGE_EXECUTE|PAGE_EXECUTE_READ))
    {
       MmSetPageProtect(Process, (PVOID)PAGE_ROUND_DOWN(Address), Protect);
    }
-   
+
    /* Detach from process */
    if (CurrentProcess != Process)
    {
@@ -197,11 +251,11 @@ BOOLEAN
 KdbpShouldStepOverInstruction(ULONG_PTR Eip)
 {
    UCHAR Mem[3];
-   INT i = 0;
+   UINT i = 0;
 
    if (!NT_SUCCESS(KdbpSafeReadMemory(Mem, (PVOID)Eip, sizeof (Mem))))
    {
-      KdbpPrint("Couldn't access memory at 0x%x\n", (UINT)Eip);
+      KdbpPrint("Couldn't access memory at 0x%p\n", Eip);
       return FALSE;
    }
 
@@ -257,10 +311,7 @@ KdbpStepOverInstruction(ULONG_PTR Eip)
 BOOLEAN
 KdbpStepIntoInstruction(ULONG_PTR Eip)
 {
-   struct __attribute__((packed)) {
-      USHORT Limit;
-      ULONG Base;
-   } Idtr;
+   KDESCRIPTOR Idtr;
    UCHAR Mem[2];
    INT IntVect;
    ULONG IntDesc[2];
@@ -269,7 +320,7 @@ KdbpStepIntoInstruction(ULONG_PTR Eip)
    /* Read memory */
    if (!NT_SUCCESS(KdbpSafeReadMemory(Mem, (PVOID)Eip, sizeof (Mem))))
    {
-      /*KdbpPrint("Couldn't access memory at 0x%x\n", (UINT)Eip);*/
+      /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
       return FALSE;
    }
 
@@ -279,7 +330,7 @@ KdbpStepIntoInstruction(ULONG_PTR Eip)
       IntVect = 3;
    else if (Mem[0] == 0xcd)
       IntVect = Mem[1];
-   else if (Mem[0] == 0xce && KdbCurrentTrapFrame->Tf.Eflags & (1<<11)) /* 1 << 11 is the overflow flag */
+   else if (Mem[0] == 0xce && KdbCurrentTrapFrame->Tf.EFlags & (1<<11)) /* 1 << 11 is the overflow flag */
       IntVect = 4;
    else
       return FALSE;
@@ -290,7 +341,7 @@ KdbpStepIntoInstruction(ULONG_PTR Eip)
    }
 
    /* Read the interrupt descriptor table register  */
-   asm volatile("sidt %0" : : "m"(Idtr));
+   asm volatile("sidt %0" : : "m"(Idtr.Limit));
    if (IntVect >= (Idtr.Limit + 1) / 8)
    {
       /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
@@ -300,10 +351,10 @@ KdbpStepIntoInstruction(ULONG_PTR Eip)
    /* Get the interrupt descriptor */
    if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc, (PVOID)(Idtr.Base + (IntVect * 8)), sizeof (IntDesc))))
    {
-      /*KdbpPrint("Couldn't access memory at 0x%x\n", (UINT)Idtr.Base + (IntVect * 8));*/
+      /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
       return FALSE;
    }
-   
+
    /* Check descriptor and get target eip (16 bit interrupt/trap gates not supported) */
    if ((IntDesc[1] & (1 << 15)) == 0) /* not present */
    {
@@ -384,7 +435,7 @@ KdbpGetBreakPointInfo(
    {
       return FALSE;
    }
-   
+
    bp = KdbBreakPoints + BreakPointNr;
    if (Address != NULL)
       *Address = bp->Address;
@@ -447,7 +498,7 @@ KdbpInsertBreakPoint(
    {
       if ((Address % Size) != 0)
       {
-         KdbpPrint("Address (0x%x) must be aligned to a multiple of the size (%d)\n", Address, Size);
+         KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address, Size);
          return STATUS_UNSUCCESSFUL;
       }
       if (AccessType == KdbAccessExec && Size != 1)
@@ -461,7 +512,7 @@ KdbpInsertBreakPoint(
    {
       return STATUS_UNSUCCESSFUL;
    }
-   
+
    /* Parse conditon expression string and duplicate it */
    if (ConditionExpression != NULL)
    {
@@ -497,14 +548,14 @@ KdbpInsertBreakPoint(
    }
    else
    {
-      for (i = 0; i < RTL_NUMBER_OF(KdbBreakPoints); i++)
+      for (i = 0; i < (LONG)RTL_NUMBER_OF(KdbBreakPoints); i++)
       {
          if (KdbBreakPoints[i].Type == KdbBreakPointNone)
             break;
       }
    }
-   ASSERT(i < RTL_NUMBER_OF(KdbBreakPoints));
-   
+   ASSERT(i < (LONG)RTL_NUMBER_OF(KdbBreakPoints));
+
    /* Set the breakpoint */
    ASSERT(KdbCurrentProcess != NULL);
    KdbBreakPoints[i].Type = Type;
@@ -521,7 +572,7 @@ KdbpInsertBreakPoint(
       KdbBreakPoints[i].Data.Hw.AccessType = AccessType;
    }
    KdbBreakPointCount++;
-   
+
    if (Type != KdbBreakPointTemporary)
       KdbpPrint("Breakpoint %d inserted.\n", i);
 
@@ -576,7 +627,7 @@ KdbpDeleteBreakPoint(
       KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr);
    BreakPoint->Type = KdbBreakPointNone;
    KdbBreakPointCount--;
-   
+
    return TRUE;
 }
 
@@ -595,7 +646,7 @@ KdbpIsBreakPointOurs(
    IN ULONG ExpNr,
    IN PKTRAP_FRAME TrapFrame)
 {
-   INT i;
+   UINT i;
    ASSERT(ExpNr == 1 || ExpNr == 3);
 
    if (ExpNr == 3) /* Software interrupt */
@@ -626,7 +677,7 @@ KdbpIsBreakPointOurs(
          }
       }
    }
-   
+
    return -1;
 }
 
@@ -668,7 +719,7 @@ KdbpEnableBreakPoint(
       KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
       return FALSE;
    }
-   
+
    if (BreakPoint->Enabled == TRUE)
    {
       KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr);
@@ -689,7 +740,7 @@ KdbpEnableBreakPoint(
                                         0xCC, &BreakPoint->Data.SavedInstruction);
       if (!NT_SUCCESS(Status))
       {
-         KdbpPrint("Couldn't access memory at 0x%x\n", BreakPoint->Address);
+         KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint->Address);
          return FALSE;
       }
       KdbSwBreakPoints[KdbSwBreakPointCount++] = BreakPoint;
@@ -801,9 +852,9 @@ KdbpDisableBreakPoint(
    IN LONG BreakPointNr  OPTIONAL,
    IN OUT PKDB_BREAKPOINT BreakPoint  OPTIONAL)
 {
-   INT i;
+   UINT i;
    NTSTATUS Status;
-   
+
    if (BreakPointNr < 0)
    {
       ASSERT(BreakPoint != NULL);
@@ -841,7 +892,7 @@ KdbpDisableBreakPoint(
          KdbpPrint("Couldn't restore original instruction.\n");
          return FALSE;
       }
-         
+
       for (i = 0; i < KdbSwBreakPointCount; i++)
       {
          if (KdbSwBreakPoints[i] == BreakPoint)
@@ -851,13 +902,13 @@ KdbpDisableBreakPoint(
             break;
          }
       }
-      if (i != -1) /* not found */
+      if (i != (UINT)-1) /* not found */
          ASSERT(0);
    }
    else
    {
       ASSERT(BreakPoint->Type == KdbBreakPointHardware);
-      
+
       /* Clear the breakpoint. */
       KdbTrapFrame.Tf.Dr7 &= ~(0x3 << (BreakPoint->Data.Hw.DebugReg * 2));
       if ((KdbTrapFrame.Tf.Dr7 & 0xFF) == 0)
@@ -877,7 +928,7 @@ KdbpDisableBreakPoint(
             break;
          }
       }
-      if (i != -1) /* not found */
+      if (i != (UINT)-1) /* not found */
          ASSERT(0);
    }
 
@@ -902,7 +953,7 @@ KdbpGetEnterCondition(
    IN BOOLEAN FirstChance,
    OUT KDB_ENTER_CONDITION *Condition)
 {
-   if (ExceptionNr >= RTL_NUMBER_OF(KdbEnterConditions))
+   if (ExceptionNr >= (LONG)RTL_NUMBER_OF(KdbEnterConditions))
       return FALSE;
 
    *Condition = KdbEnterConditions[ExceptionNr][FirstChance ? 0 : 1];
@@ -926,7 +977,7 @@ KdbpSetEnterCondition(
 {
    if (ExceptionNr < 0)
    {
-      for (ExceptionNr = 0; ExceptionNr < RTL_NUMBER_OF(KdbEnterConditions); ExceptionNr++)
+      for (ExceptionNr = 0; ExceptionNr < (LONG)RTL_NUMBER_OF(KdbEnterConditions); ExceptionNr++)
       {
          if (ExceptionNr == 1 || ExceptionNr == 8 ||
              ExceptionNr == 9 || ExceptionNr == 15) /* Reserved exceptions */
@@ -938,7 +989,7 @@ KdbpSetEnterCondition(
    }
    else
    {
-      if (ExceptionNr >= RTL_NUMBER_OF(KdbEnterConditions) ||
+      if (ExceptionNr >= (LONG)RTL_NUMBER_OF(KdbEnterConditions) ||
           ExceptionNr == 1 || ExceptionNr == 8 || /* Do not allow changing of the debug */
           ExceptionNr == 9 || ExceptionNr == 15)  /* trap or reserved exceptions */
       {
@@ -976,12 +1027,12 @@ KdbpAttachToThread(
       KdbpPrint("Cannot attach to thread within another process while executing a DPC.\n");
       return FALSE;
    }
-   
+
    /* Save the current thread's context (if we previously attached to a thread) */
    if (KdbCurrentThread != KdbOriginalThread)
    {
       ASSERT(KdbCurrentTrapFrame == &KdbThreadTrapFrame);
-      RtlCopyMemory(KdbCurrentThread->Tcb.TrapFrame, &KdbCurrentTrapFrame->Tf, sizeof (KTRAP_FRAME));
+      KdbpKdbTrapFrameToTrapFrame(KdbCurrentTrapFrame, KdbCurrentThread->Tcb.TrapFrame);
    }
    else
    {
@@ -991,15 +1042,12 @@ KdbpAttachToThread(
    /* Switch to the thread's context */
    if (Thread != KdbOriginalThread)
    {
-      ASSERT(Thread->Tcb.TrapFrame != NULL);
-      RtlCopyMemory(&KdbThreadTrapFrame.Tf, Thread->Tcb.TrapFrame, sizeof (KTRAP_FRAME));
-      asm volatile(
-         "movl %%cr0, %0"    "\n\t"
-         "movl %%cr2, %1"    "\n\t"
-         "movl %%cr3, %2"    "\n\t"
-         "movl %%cr4, %3"    "\n\t"
-         : "=r"(KdbTrapFrame.Cr0), "=r"(KdbTrapFrame.Cr2),
-           "=r"(KdbTrapFrame.Cr3), "=r"(KdbTrapFrame.Cr4));
+      if (Thread->Tcb.TrapFrame == NULL)
+      {
+         KdbpPrint("Threads TrapFrame is NULL! Cannot attach.\n");
+         return FALSE;
+      }
+      KdbpTrapFrameToKdbTrapFrame(Thread->Tcb.TrapFrame, &KdbThreadTrapFrame);
       KdbCurrentTrapFrame = &KdbThreadTrapFrame;
    }
    else /* Switching back to original thread */
@@ -1018,7 +1066,7 @@ KdbpAttachToThread(
       }
       if (KdbOriginalProcess != Process)
       {
-         KeStackAttachProcess(EPROCESS_TO_KPROCESS(Process), &KdbApcState);
+         KeStackAttachProcess(&Process->Pcb, &KdbApcState);
       }
       KdbCurrentProcess = Process;
    }
@@ -1076,11 +1124,11 @@ KdbpCallMainLoop()
  */
 STATIC VOID
 KdbpInternalEnter()
-{  
+{
    PETHREAD Thread;
    PVOID SavedInitialStack, SavedStackBase, SavedKernelStack;
    ULONG SavedStackLimit;
-   
+
    KbdDisableMouse();
    if (KdpDebugMode.Screen)
    {
@@ -1129,7 +1177,7 @@ KdbEnterDebuggerException(
    IN OUT PKTRAP_FRAME TrapFrame,
    IN BOOLEAN FirstChance)
 {
-   ULONG ExpNr = (ULONG)TrapFrame->DebugArgMark;
+   ULONG ExpNr = (ULONG)TrapFrame->DbgArgMark;
    KDB_ENTER_CONDITION EnterCondition;
    KD_CONTINUE_TYPE ContinueType = kdHandleException;
    PKDB_BREAKPOINT BreakPoint;
@@ -1149,7 +1197,7 @@ KdbEnterDebuggerException(
    ul = min(ExpNr, RTL_NUMBER_OF(KdbEnterConditions) - 1);
    EnterCondition = KdbEnterConditions[ul][FirstChance ? 0 : 1];
    if (EnterCondition == KdbDoNotEnter ||
-       (EnterCondition == KdbEnterFromUmode && PreviousMode != UserMode) ||
+       (EnterCondition == KdbEnterFromUmode && PreviousMode == KernelMode) ||
        (EnterCondition == KdbEnterFromKmode && PreviousMode != KernelMode))
    {
       EnterConditionMet = FALSE;
@@ -1188,7 +1236,7 @@ KdbEnterDebuggerException(
       {
          Resume = TRUE; /* Set the resume flag when continuing execution */
       }
-      
+
       /*
        * When a temporary breakpoint is hit we have to make sure that we are
        * in the same context in which it was set, otherwise it could happen
@@ -1197,8 +1245,8 @@ KdbEnterDebuggerException(
       else if (BreakPoint->Type == KdbBreakPointTemporary &&
                BreakPoint->Process == KdbCurrentProcess)
       {
-         ASSERT((TrapFrame->Eflags & X86_EFLAGS_TF) == 0);
-         
+         ASSERT((TrapFrame->EFlags & X86_EFLAGS_TF) == 0);
+
          /*
           * Delete the temporary breakpoint which was used to step over or into the instruction.
           */
@@ -1209,7 +1257,7 @@ KdbEnterDebuggerException(
             if ((KdbSingleStepOver && !KdbpStepOverInstruction(TrapFrame->Eip)) ||
                 (!KdbSingleStepOver && !KdbpStepIntoInstruction(TrapFrame->Eip)))
             {
-               TrapFrame->Eflags |= X86_EFLAGS_TF;
+               TrapFrame->EFlags |= X86_EFLAGS_TF;
             }
             goto continue_execution; /* return */
          }
@@ -1225,10 +1273,10 @@ KdbEnterDebuggerException(
                BreakPoint->Type == KdbBreakPointTemporary)
       {
          ASSERT(ExpNr == 3);
-         TrapFrame->Eflags |= X86_EFLAGS_TF;
+         TrapFrame->EFlags |= X86_EFLAGS_TF;
          KdbBreakPointToReenable = BreakPoint;
       }
-      
+
       /*
        * Make sure that the breakpoint should be triggered in this context
        */
@@ -1236,21 +1284,14 @@ KdbEnterDebuggerException(
       {
             goto continue_execution; /* return */
       }
-      
+
       /*
        * Check if the condition for the breakpoint is met.
        */
       if (BreakPoint->Condition != NULL)
       {
          /* Setup the KDB trap frame */
-         RtlCopyMemory(&KdbTrapFrame.Tf, TrapFrame, sizeof (KTRAP_FRAME));
-         asm volatile(
-            "movl %%cr0, %0"    "\n\t"
-            "movl %%cr2, %1"    "\n\t"
-            "movl %%cr3, %2"    "\n\t"
-            "movl %%cr4, %3"    "\n\t"
-            : "=r"(KdbTrapFrame.Cr0), "=r"(KdbTrapFrame.Cr2),
-              "=r"(KdbTrapFrame.Cr3), "=r"(KdbTrapFrame.Cr4));
+         KdbpTrapFrameToKdbTrapFrame(TrapFrame, &KdbTrapFrame);
 
          ull = 0;
          if (!KdbpRpnEvaluateParsedExpression(BreakPoint->Condition, &KdbTrapFrame, &ull, NULL, NULL))
@@ -1266,7 +1307,7 @@ KdbEnterDebuggerException(
       if (BreakPoint->Type == KdbBreakPointSoftware)
       {
          DbgPrint("Entered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
-                  KdbLastBreakPointNr, TrapFrame->Cs & 0xffff, TrapFrame->Eip);
+                  KdbLastBreakPointNr, TrapFrame->SegCs & 0xffff, TrapFrame->Eip);
       }
       else if (BreakPoint->Type == KdbBreakPointHardware)
       {
@@ -1305,7 +1346,7 @@ KdbEnterDebuggerException(
 
          /* Unset TF if we are no longer single stepping. */
          if (KdbNumSingleSteps == 0)
-            TrapFrame->Eflags &= ~X86_EFLAGS_TF;
+            TrapFrame->EFlags &= ~X86_EFLAGS_TF;
          goto continue_execution; /* return */
       }
 
@@ -1318,16 +1359,16 @@ KdbEnterDebuggerException(
             if ((KdbSingleStepOver && KdbpStepOverInstruction(TrapFrame->Eip)) ||
                 (!KdbSingleStepOver && KdbpStepIntoInstruction(TrapFrame->Eip)))
             {
-               TrapFrame->Eflags &= ~X86_EFLAGS_TF;
+               TrapFrame->EFlags &= ~X86_EFLAGS_TF;
             }
             else
             {
-               TrapFrame->Eflags |= X86_EFLAGS_TF;
+               TrapFrame->EFlags |= X86_EFLAGS_TF;
             }
             goto continue_execution; /* return */
          }
 
-         TrapFrame->Eflags &= ~X86_EFLAGS_TF;
+         TrapFrame->EFlags &= ~X86_EFLAGS_TF;
          KdbEnteredOnSingleStep = TRUE;
       }
       else
@@ -1352,7 +1393,7 @@ KdbEnterDebuggerException(
       }
 
       DbgPrint("Entered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
-               TrapFrame->Cs & 0xffff, TrapFrame->Eip - 1);
+               TrapFrame->SegCs & 0xffff, TrapFrame->Eip - 1);
    }
    else
    {
@@ -1370,10 +1411,11 @@ KdbEnterDebuggerException(
       if (ExpNr == 14)
       {
          /* FIXME: Add noexec memory stuff */
-         ULONG Cr2, Err;
+         ULONG_PTR Cr2;
+         ULONG Err;
          asm volatile("movl %%cr2, %0" : "=r"(Cr2));
-         Err = TrapFrame->ErrorCode;
-         DbgPrint("Memory at 0x%x could not be %s: ", Cr2, (Err & (1 << 1)) ? "written" : "read");
+         Err = TrapFrame->ErrCode;
+         DbgPrint("Memory at 0x%p could not be %s: ", Cr2, (Err & (1 << 1)) ? "written" : "read");
          if ((Err & (1 << 0)) == 0)
             DbgPrint("Page not present.\n");
          else
@@ -1385,24 +1427,17 @@ KdbEnterDebuggerException(
          }
       }
    }
-   
+
    /* Once we enter the debugger we do not expect any more single steps to happen */
    KdbNumSingleSteps = 0;
-   
+
    /* Update the current process pointer */
    KdbCurrentProcess = KdbOriginalProcess = PsGetCurrentProcess();
    KdbCurrentThread = KdbOriginalThread = PsGetCurrentThread();
    KdbCurrentTrapFrame = &KdbTrapFrame;
 
    /* Setup the KDB trap frame */
-   RtlCopyMemory(&KdbTrapFrame.Tf, TrapFrame, sizeof(KTRAP_FRAME));
-   asm volatile(
-      "movl %%cr0, %0"    "\n\t"
-      "movl %%cr2, %1"    "\n\t"
-      "movl %%cr3, %2"    "\n\t"
-      "movl %%cr4, %3"    "\n\t"
-      : "=r"(KdbTrapFrame.Cr0), "=r"(KdbTrapFrame.Cr2),
-        "=r"(KdbTrapFrame.Cr3), "=r"(KdbTrapFrame.Cr4));
+   KdbpTrapFrameToKdbTrapFrame(TrapFrame, &KdbTrapFrame);
 
    /* Enter critical section */
    Ke386SaveFlags(OldEflags);
@@ -1424,19 +1459,19 @@ KdbEnterDebuggerException(
       if ((KdbSingleStepOver && KdbpStepOverInstruction(KdbCurrentTrapFrame->Tf.Eip)) ||
           (!KdbSingleStepOver && KdbpStepIntoInstruction(KdbCurrentTrapFrame->Tf.Eip)))
       {
-         ASSERT((KdbCurrentTrapFrame->Tf.Eflags & X86_EFLAGS_TF) == 0);
-         /*KdbCurrentTrapFrame->Tf.Eflags &= ~X86_EFLAGS_TF;*/
+         ASSERT((KdbCurrentTrapFrame->Tf.EFlags & X86_EFLAGS_TF) == 0);
+         /*KdbCurrentTrapFrame->Tf.EFlags &= ~X86_EFLAGS_TF;*/
       }
       else
       {
-         KdbCurrentTrapFrame->Tf.Eflags |= X86_EFLAGS_TF;
+         KdbCurrentTrapFrame->Tf.EFlags |= X86_EFLAGS_TF;
       }
    }
 
    /* Save the current thread's trapframe */
    if (KdbCurrentTrapFrame == &KdbThreadTrapFrame)
    {
-      RtlCopyMemory(KdbCurrentThread->Tcb.TrapFrame, KdbCurrentTrapFrame, sizeof (KTRAP_FRAME));
+      KdbpKdbTrapFrameToTrapFrame(KdbCurrentTrapFrame, KdbCurrentThread->Tcb.TrapFrame);
    }
 
    /* Detach from attached process */
@@ -1446,16 +1481,7 @@ KdbEnterDebuggerException(
    }
 
    /* Update the exception TrapFrame */
-   RtlCopyMemory(TrapFrame, &KdbTrapFrame.Tf, sizeof(KTRAP_FRAME));
-#if 0
-   asm volatile(
-      "movl %0, %%cr0"    "\n\t"
-      "movl %1, %%cr2"    "\n\t"
-      "movl %2, %%cr3"    "\n\t"
-      "movl %3, %%cr4"    "\n\t"
-      : : "r"(KdbTrapFrame.Cr0), "r"(KdbTrapFrame.Cr2),
-          "r"(KdbTrapFrame.Cr3), "r"(KdbTrapFrame.Cr4));
-#endif
+   KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame, TrapFrame);
 
    /* Decrement the entry count */
    InterlockedDecrement(&KdbEntryCount);
@@ -1470,9 +1496,9 @@ continue_execution:
       /* Set the RF flag so we don't trigger the same breakpoint again. */
       if (Resume)
       {
-         TrapFrame->Eflags |= X86_EFLAGS_RF;
+         TrapFrame->EFlags |= X86_EFLAGS_RF;
       }
-         
+
       /* Clear dr6 status flags. */
       TrapFrame->Dr6 &= ~0x0000e00f;
 
@@ -1485,7 +1511,7 @@ VOID
 KdbDeleteProcessHook(IN PEPROCESS Process)
 {
    KdbSymFreeProcessSymbols(Process);
-   
+
    /* FIXME: Delete breakpoints for process */
 }
 
@@ -1510,7 +1536,51 @@ KdbpGetCommandLineSettings(PCHAR p1)
             p2 += 8;
             KdbDebugState |= KD_DEBUG_KDNOECHO;
         }
-        
+
         p1 = p2;
     }
 }
+
+NTSTATUS
+KdbpSafeReadMemory(OUT PVOID Dest,
+                   IN PVOID Src,
+                   IN ULONG Bytes)
+{
+   NTSTATUS Status = STATUS_SUCCESS;
+
+   _SEH_TRY
+   {
+      RtlCopyMemory(Dest,
+                    Src,
+                    Bytes);
+   }
+   _SEH_HANDLE
+   {
+      Status = _SEH_GetExceptionCode();
+   }
+   _SEH_END;
+   
+   return Status;
+}
+
+NTSTATUS
+KdbpSafeWriteMemory(OUT PVOID Dest,
+                    IN PVOID Src,
+                    IN ULONG Bytes)
+{
+   NTSTATUS Status = STATUS_SUCCESS;
+
+   _SEH_TRY
+   {
+      RtlCopyMemory(Dest,
+                    Src,
+                    Bytes);
+   }
+   _SEH_HANDLE
+   {
+      Status = _SEH_GetExceptionCode();
+   }
+   _SEH_END;
+
+   return Status;
+}