[ZIPFLDR] Make function prototype consistent
[reactos.git] / ntoskrnl / kdbg / kdb.c
index b13384c..15175d5 100644 (file)
 /* INCLUDES ******************************************************************/
 
 #include <ntoskrnl.h>
-#define NDEBUG
-#include <debug.h>
+#include "kdb.h"
 
 /* TYPES *********************************************************************/
 
 /* DEFINES *******************************************************************/
 
 #define KDB_STACK_SIZE                   (4096*3)
+#ifdef _M_AMD64
+#define KDB_STACK_ALIGN                 16
+#define KDB_STACK_RESERVE               (5 * sizeof(PVOID)) /* Home space + return address */
+#else
+#define KDB_STACK_ALIGN                 4
+#define KDB_STACK_RESERVE               sizeof(ULONG) /* Return address */
+#endif
 #define KDB_MAXIMUM_BREAKPOINT_COUNT     256
 #define KDB_MAXIMUM_HW_BREAKPOINT_COUNT  4
 #define KDB_MAXIMUM_SW_BREAKPOINT_COUNT  256
@@ -28,7 +34,7 @@
 /* GLOBALS *******************************************************************/
 
 static LONG KdbEntryCount = 0;
-static CHAR KdbStack[KDB_STACK_SIZE];
+static DECLSPEC_ALIGN(KDB_STACK_ALIGN) CHAR KdbStack[KDB_STACK_SIZE];
 
 static ULONG KdbBreakPointCount = 0;  /* Number of used breakpoints in the array */
 static KDB_BREAKPOINT KdbBreakPoints[KDB_MAXIMUM_BREAKPOINT_COUNT] = {{0}};  /* Breakpoint array */
@@ -38,18 +44,18 @@ static PKDB_BREAKPOINT KdbSwBreakPoints[KDB_MAXIMUM_SW_BREAKPOINT_COUNT]; /* Ena
 static PKDB_BREAKPOINT KdbHwBreakPoints[KDB_MAXIMUM_HW_BREAKPOINT_COUNT]; /* Enabled hardware breakpoints, orderless */
 static PKDB_BREAKPOINT KdbBreakPointToReenable = NULL; /* Set to a breakpoint struct when single stepping after
                                                           a software breakpoint was hit, to reenable it */
+static BOOLEAN KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep;
 LONG KdbLastBreakPointNr = -1;  /* Index of the breakpoint which cause KDB to be entered */
 ULONG KdbNumSingleSteps = 0; /* How many single steps to do */
 BOOLEAN KdbSingleStepOver = FALSE; /* Whether to step over calls/reps. */
-ULONG KdbDebugState = 0; /* KDBG Settings (NOECHO, KDSERIAL) */
 static BOOLEAN KdbEnteredOnSingleStep = FALSE; /* Set to true when KDB was entered because of single step */
 PEPROCESS KdbCurrentProcess = NULL;  /* The current process context in which KDB runs */
 PEPROCESS KdbOriginalProcess = NULL; /* The process in whichs context KDB was intered */
 PETHREAD KdbCurrentThread = NULL;  /* The current thread context in which KDB runs */
 PETHREAD KdbOriginalThread = NULL; /* The thread in whichs context KDB was entered */
 PKDB_KTRAP_FRAME KdbCurrentTrapFrame = NULL; /* Pointer to the current trapframe */
-static KDB_KTRAP_FRAME KdbTrapFrame = { { 0 } };  /* The trapframe which was passed to KdbEnterDebuggerException */
-static KDB_KTRAP_FRAME KdbThreadTrapFrame = { { 0 } }; /* The trapframe of the current thread (KdbCurrentThread) */
+static KDB_KTRAP_FRAME KdbTrapFrame = { 0 };  /* The trapframe which was passed to KdbEnterDebuggerException */
+static KDB_KTRAP_FRAME KdbThreadTrapFrame = { 0 }; /* The trapframe of the current thread (KdbCurrentThread) */
 static KAPC_STATE KdbApcState;
 extern BOOLEAN KdbpBugCheckRequested;
 
@@ -57,26 +63,27 @@ extern BOOLEAN KdbpBugCheckRequested;
 static KDB_ENTER_CONDITION KdbEnterConditions[][2] =
 {
     /* First chance       Last chance */
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* Zero devide */
-    { KdbEnterFromKmode,  KdbDoNotEnter },       /* Debug trap */
-    { KdbDoNotEnter,      KdbEnterAlways },      /* NMI */
-    { KdbEnterFromKmode,  KdbDoNotEnter },       /* INT3 */
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* Overflow */
-    { KdbDoNotEnter,      KdbEnterFromKmode },
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* Invalid opcode */
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* No math coprocessor fault */
-    { KdbEnterAlways,     KdbEnterAlways },
-    { KdbEnterAlways,     KdbEnterAlways },
-    { KdbDoNotEnter,      KdbEnterFromKmode },
-    { KdbDoNotEnter,      KdbEnterFromKmode },
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* Stack fault */
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* General protection fault */
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* Page fault */
-    { KdbEnterAlways,     KdbEnterAlways },      /* Reserved (15) */
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* FPU fault */
-    { KdbDoNotEnter,      KdbEnterFromKmode },
-    { KdbDoNotEnter,      KdbEnterFromKmode },
-    { KdbDoNotEnter,      KdbEnterFromKmode },   /* SIMD fault */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 0: Zero divide */
+    { KdbEnterFromKmode,  KdbDoNotEnter },       /* 1: Debug trap */
+    { KdbDoNotEnter,      KdbEnterAlways },      /* 2: NMI */
+    { KdbEnterFromKmode,  KdbDoNotEnter },       /* 3: INT3 */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 4: Overflow */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 5: BOUND range exceeded */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 6: Invalid opcode */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 7: No math coprocessor fault */
+    { KdbEnterAlways,     KdbEnterAlways },      /* 8: Double Fault */
+    { KdbEnterAlways,     KdbEnterAlways },      /* 9: Unknown(9) */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 10: Invalid TSS */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 11: Segment Not Present */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 12: Stack fault */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 13: General protection fault */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 14: Page fault */
+    { KdbEnterAlways,     KdbEnterAlways },      /* 15: Reserved (15) */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 16: FPU fault */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 17: Alignment Check */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 18: Machine Check */
+    { KdbDoNotEnter,      KdbEnterFromKmode },   /* 19: SIMD fault */
+    { KdbEnterFromKmode,  KdbDoNotEnter },       /* 20: Assertion failure */
     { KdbDoNotEnter,      KdbEnterFromKmode }    /* Last entry: used for unknown exceptions */
 };
 
@@ -102,98 +109,12 @@ static const CHAR *ExceptionNrToString[] =
     "Math Fault",
     "Alignment Check",
     "Machine Check",
-    "SIMD Fault"
+    "SIMD Fault",
+    "Assertion Failure"
 };
 
-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)
-{
-    ULONG TrapCr0, TrapCr2, TrapCr3, TrapCr4;
-
-    /* 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));
-
-#ifndef _MSC_VER
-   asm volatile(
-      "movl %%cr0, %0"    "\n\t"
-      "movl %%cr2, %1"    "\n\t"
-      "movl %%cr3, %2"    "\n\t"
-      "movl %%cr4, %3"    "\n\t"
-      : "=r"(TrapCr0), "=r"(TrapCr2),
-        "=r"(TrapCr3), "=r"(TrapCr4));
-#else
-   __asm
-   {
-       mov eax, cr0;
-       mov TrapCr0, eax;
-
-       mov eax, cr2;
-       mov TrapCr2, eax;
-
-       mov eax, cr3;
-       mov TrapCr3, eax;
-/* FIXME: What's the problem with cr4? */
-       //mov eax, cr4;
-       //mov TrapCr4, eax;
-   }
-#endif
-
-    KdbTrapFrame->Cr0 = TrapCr0;
-    KdbTrapFrame->Cr2 = TrapCr2;
-    KdbTrapFrame->Cr3 = TrapCr3;
-    KdbTrapFrame->Cr4 = TrapCr4;
-
-    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 */
-}
-
 static VOID
 KdbpKdbTrapFrameFromKernelStack(
     PVOID KernelStack,
@@ -204,17 +125,17 @@ KdbpKdbTrapFrameFromKernelStack(
     RtlZeroMemory(KdbTrapFrame, sizeof(KDB_KTRAP_FRAME));
     StackPtr = (ULONG_PTR *) KernelStack;
 #ifdef _M_IX86
-    KdbTrapFrame->Tf.Ebp = StackPtr[3];
-    KdbTrapFrame->Tf.Edi = StackPtr[4];
-    KdbTrapFrame->Tf.Esi = StackPtr[5];
-    KdbTrapFrame->Tf.Ebx = StackPtr[6];
-    KdbTrapFrame->Tf.Eip = StackPtr[7];
-    KdbTrapFrame->Tf.HardwareEsp = (ULONG) (StackPtr + 8);
-    KdbTrapFrame->Tf.HardwareSegSs = KGDT_R0_DATA;
-    KdbTrapFrame->Tf.SegCs = KGDT_R0_CODE;
-    KdbTrapFrame->Tf.SegDs = KGDT_R0_DATA;
-    KdbTrapFrame->Tf.SegEs = KGDT_R0_DATA;
-    KdbTrapFrame->Tf.SegGs = KGDT_R0_DATA;
+    KdbTrapFrame->Ebp = StackPtr[3];
+    KdbTrapFrame->Edi = StackPtr[4];
+    KdbTrapFrame->Esi = StackPtr[5];
+    KdbTrapFrame->Ebx = StackPtr[6];
+    KdbTrapFrame->Eip = StackPtr[7];
+    KdbTrapFrame->Esp = (ULONG) (StackPtr + 8);
+    KdbTrapFrame->SegSs = KGDT_R0_DATA;
+    KdbTrapFrame->SegCs = KGDT_R0_CODE;
+    KdbTrapFrame->SegDs = KGDT_R0_DATA;
+    KdbTrapFrame->SegEs = KGDT_R0_DATA;
+    KdbTrapFrame->SegGs = KGDT_R0_DATA;
 #endif
 
     /* FIXME: what about the other registers??? */
@@ -278,7 +199,7 @@ KdbpOverwriteInstruction(
             /* Detach from process */
             if (CurrentProcess != Process)
             {
-                KeDetachProcess();
+                KeUnstackDetachProcess(&ApcState);
             }
 
             return Status;
@@ -318,7 +239,7 @@ KdbpShouldStepOverInstruction(
 
     if (!NT_SUCCESS(KdbpSafeReadMemory(Mem, (PVOID)Eip, sizeof (Mem))))
     {
-        KdbpPrint("Couldn't access memory at 0x%p\n", Eip);
+        KdbPrintf("Couldn't access memory at 0x%p\n", Eip);
         return FALSE;
     }
 
@@ -388,7 +309,7 @@ KdbpStepIntoInstruction(
     /* Read memory */
     if (!NT_SUCCESS(KdbpSafeReadMemory(Mem, (PVOID)Eip, sizeof (Mem))))
     {
-        /*KdbpPrint("Couldn't access memory at 0x%p\n", Eip);*/
+        // KdbPrintf("Couldn't access memory at 0x%p\n", Eip);
         return FALSE;
     }
 
@@ -398,7 +319,7 @@ KdbpStepIntoInstruction(
         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->EFlags & (1<<11)) /* 1 << 11 is the overflow flag */
         IntVect = 4;
     else
         return FALSE;
@@ -412,14 +333,14 @@ KdbpStepIntoInstruction(
     __sidt(&Idtr.Limit);
     if (IntVect >= (Idtr.Limit + 1) / 8)
     {
-        /*KdbpPrint("IDT does not contain interrupt vector %d\n.", IntVect);*/
+        // KdbPrintf("IDT does not contain interrupt vector %d.\n", IntVect);
         return TRUE;
     }
 
     /* Get the interrupt descriptor */
-    if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc, (PVOID)(ULONG_PTR)(Idtr.Base + (IntVect * 8)), sizeof (IntDesc))))
+    if (!NT_SUCCESS(KdbpSafeReadMemory(IntDesc, (PVOID)((ULONG_PTR)Idtr.Base + (IntVect * 8)), sizeof(IntDesc))))
     {
-        /*KdbpPrint("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));*/
+        // KdbPrintf("Couldn't access memory at 0x%p\n", (ULONG_PTR)Idtr.Base + (IntVect * 8));
         return FALSE;
     }
 
@@ -563,7 +484,7 @@ KdbpInsertBreakPoint(
     IN  BOOLEAN Global,
     OUT PLONG BreakPointNr  OPTIONAL)
 {
-    LONG i;
+    LONG_PTR i;
     PVOID Condition;
     PCHAR ConditionExpressionDup;
     LONG ErrOffset;
@@ -575,13 +496,13 @@ KdbpInsertBreakPoint(
     {
         if ((Address % Size) != 0)
         {
-            KdbpPrint("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address, Size);
+            KdbPrintf("Address (0x%p) must be aligned to a multiple of the size (%d)\n", Address, Size);
             return STATUS_UNSUCCESSFUL;
         }
 
         if (AccessType == KdbAccessExec && Size != 1)
         {
-            KdbpPrint("Size must be 1 for execution breakpoints.\n");
+            KdbPuts("Size must be 1 for execution breakpoints.\n");
             return STATUS_UNSUCCESSFUL;
         }
     }
@@ -591,16 +512,16 @@ KdbpInsertBreakPoint(
         return STATUS_UNSUCCESSFUL;
     }
 
-    /* Parse conditon expression string and duplicate it */
+    /* Parse condition expression string and duplicate it */
     if (ConditionExpression)
     {
         Condition = KdbpRpnParseExpression(ConditionExpression, &ErrOffset, ErrMsg);
         if (!Condition)
         {
             if (ErrOffset >= 0)
-                KdbpPrint("Couldn't parse expression: %s at character %d\n", ErrMsg, ErrOffset);
+                KdbPrintf("Couldn't parse expression: %s at character %d\n", ErrMsg, ErrOffset);
             else
-                KdbpPrint("Couldn't parse expression: %s", ErrMsg);
+                KdbPrintf("Couldn't parse expression: %s", ErrMsg);
 
             return STATUS_UNSUCCESSFUL;
         }
@@ -654,7 +575,7 @@ KdbpInsertBreakPoint(
     KdbBreakPointCount++;
 
     if (Type != KdbBreakPointTemporary)
-        KdbpPrint("Breakpoint %d inserted.\n", i);
+        KdbPrintf("Breakpoint %d inserted.\n", i);
 
     /* Try to enable the breakpoint */
     KdbpEnableBreakPoint(i, NULL);
@@ -687,7 +608,7 @@ KdbpDeleteBreakPoint(
 
     if (BreakPointNr < 0 || BreakPointNr >= KDB_MAXIMUM_BREAKPOINT_COUNT)
     {
-        KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
+        KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
         return FALSE;
     }
 
@@ -698,7 +619,7 @@ KdbpDeleteBreakPoint(
 
     if (BreakPoint->Type == KdbBreakPointNone)
     {
-        KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
+        KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
         return FALSE;
     }
 
@@ -706,7 +627,7 @@ KdbpDeleteBreakPoint(
         return FALSE;
 
     if (BreakPoint->Type != KdbBreakPointTemporary)
-        KdbpPrint("Breakpoint %d deleted.\n", BreakPointNr);
+        KdbPrintf("Breakpoint %d deleted.\n", BreakPointNr);
 
     BreakPoint->Type = KdbBreakPointNone;
     KdbBreakPointCount--;
@@ -727,21 +648,21 @@ KdbpDeleteBreakPoint(
 static LONG
 KdbpIsBreakPointOurs(
     IN NTSTATUS ExceptionCode,
-    IN PKTRAP_FRAME TrapFrame)
+    IN PCONTEXT Context)
 {
     ULONG i;
     ASSERT(ExceptionCode == STATUS_SINGLE_STEP || ExceptionCode == STATUS_BREAKPOINT);
 
     if (ExceptionCode == STATUS_BREAKPOINT) /* Software interrupt */
     {
-        ULONG_PTR BpEip = (ULONG_PTR)TrapFrame->Eip - 1; /* Get EIP of INT3 instruction */
+        ULONG_PTR BpPc = KeGetContextPc(Context) - 1; /* Get EIP of INT3 instruction */
         for (i = 0; i < KdbSwBreakPointCount; i++)
         {
             ASSERT((KdbSwBreakPoints[i]->Type == KdbBreakPointSoftware ||
                    KdbSwBreakPoints[i]->Type == KdbBreakPointTemporary));
             ASSERT(KdbSwBreakPoints[i]->Enabled);
 
-            if (KdbSwBreakPoints[i]->Address == BpEip)
+            if (KdbSwBreakPoints[i]->Address == BpPc)
             {
                 return KdbSwBreakPoints[i] - KdbBreakPoints;
             }
@@ -757,7 +678,7 @@ KdbpIsBreakPointOurs(
                    KdbHwBreakPoints[i]->Enabled);
             DebugReg = KdbHwBreakPoints[i]->Data.Hw.DebugReg;
 
-            if ((TrapFrame->Dr6 & (1 << DebugReg)) != 0)
+            if ((Context->Dr6 & ((ULONG_PTR)1 << DebugReg)) != 0)
             {
                 return KdbHwBreakPoints[i] - KdbBreakPoints;
             }
@@ -794,7 +715,7 @@ KdbpEnableBreakPoint(
 
     if (BreakPointNr < 0 || BreakPointNr >= KDB_MAXIMUM_BREAKPOINT_COUNT)
     {
-        KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
+        KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
         return FALSE;
     }
 
@@ -805,13 +726,13 @@ KdbpEnableBreakPoint(
 
     if (BreakPoint->Type == KdbBreakPointNone)
     {
-        KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
+        KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
         return FALSE;
     }
 
     if (BreakPoint->Enabled)
     {
-        KdbpPrint("Breakpoint %d is already enabled.\n", BreakPointNr);
+        KdbPrintf("Breakpoint %d is already enabled.\n", BreakPointNr);
         return TRUE;
     }
 
@@ -820,7 +741,7 @@ KdbpEnableBreakPoint(
     {
         if (KdbSwBreakPointCount >= KDB_MAXIMUM_SW_BREAKPOINT_COUNT)
         {
-            KdbpPrint("Maximum number of SW breakpoints (%d) used. "
+            KdbPrintf("Maximum number of SW breakpoints (%d) used. "
                       "Disable another breakpoint in order to enable this one.\n",
                       KDB_MAXIMUM_SW_BREAKPOINT_COUNT);
             return FALSE;
@@ -830,7 +751,7 @@ KdbpEnableBreakPoint(
                                           0xCC, &BreakPoint->Data.SavedInstruction);
         if (!NT_SUCCESS(Status))
         {
-            KdbpPrint("Couldn't access memory at 0x%p\n", BreakPoint->Address);
+            KdbPrintf("Couldn't access memory at 0x%p\n", BreakPoint->Address);
             return FALSE;
         }
 
@@ -845,10 +766,9 @@ KdbpEnableBreakPoint(
 
         if (KdbHwBreakPointCount >= KDB_MAXIMUM_HW_BREAKPOINT_COUNT)
         {
-            KdbpPrint("Maximum number of HW breakpoints (%d) already used. "
+            KdbPrintf("Maximum number of HW breakpoints (%d) already used. "
                       "Disable another breakpoint in order to enable this one.\n",
                       KDB_MAXIMUM_HW_BREAKPOINT_COUNT);
-
             return FALSE;
         }
 
@@ -856,7 +776,7 @@ KdbpEnableBreakPoint(
         ASSERT(KDB_MAXIMUM_HW_BREAKPOINT_COUNT == 4);
         for (i = 0; i < KDB_MAXIMUM_HW_BREAKPOINT_COUNT; i++)
         {
-            if ((KdbTrapFrame.Tf.Dr7 & (0x3 << (i * 2))) == 0)
+            if ((KdbTrapFrame.Dr7 & (0x3 << (i * 2))) == 0)
                 break;
         }
 
@@ -866,27 +786,27 @@ KdbpEnableBreakPoint(
         switch (i)
         {
             case 0:
-                KdbTrapFrame.Tf.Dr0 = BreakPoint->Address;
+                KdbTrapFrame.Dr0 = BreakPoint->Address;
                 break;
             case 1:
-                KdbTrapFrame.Tf.Dr1 = BreakPoint->Address;
+                KdbTrapFrame.Dr1 = BreakPoint->Address;
                 break;
             case 2:
-                KdbTrapFrame.Tf.Dr2 = BreakPoint->Address;
+                KdbTrapFrame.Dr2 = BreakPoint->Address;
                 break;
             case 3:
-                KdbTrapFrame.Tf.Dr3 = BreakPoint->Address;
+                KdbTrapFrame.Dr3 = BreakPoint->Address;
                 break;
         }
 
         /* Enable the global breakpoint */
-        KdbTrapFrame.Tf.Dr7 |= (0x2 << (i * 2));
+        KdbTrapFrame.Dr7 |= (0x2 << (i * 2));
 
         /* Enable the exact match bits. */
-        KdbTrapFrame.Tf.Dr7 |= 0x00000300;
+        KdbTrapFrame.Dr7 |= 0x00000300;
 
         /* Clear existing state. */
-        KdbTrapFrame.Tf.Dr7 &= ~(0xF << (16 + (i * 4)));
+        KdbTrapFrame.Dr7 &= ~(0xF << (16 + (i * 4)));
 
         /* Set the breakpoint type. */
         switch (BreakPoint->Data.Hw.AccessType)
@@ -907,20 +827,20 @@ KdbpEnableBreakPoint(
                 break;
         }
 
-        KdbTrapFrame.Tf.Dr7 |= (ul << (16 + (i * 4)));
+        KdbTrapFrame.Dr7 |= (ul << (16 + (i * 4)));
 
         /* Set the breakpoint length. */
-        KdbTrapFrame.Tf.Dr7 |= ((BreakPoint->Data.Hw.Size - 1) << (18 + (i * 4)));
+        KdbTrapFrame.Dr7 |= ((BreakPoint->Data.Hw.Size - 1) << (18 + (i * 4)));
 
         /* Update KdbCurrentTrapFrame - values are taken from there by the CLI */
         if (&KdbTrapFrame != KdbCurrentTrapFrame)
         {
-            KdbCurrentTrapFrame->Tf.Dr0 = KdbTrapFrame.Tf.Dr0;
-            KdbCurrentTrapFrame->Tf.Dr1 = KdbTrapFrame.Tf.Dr1;
-            KdbCurrentTrapFrame->Tf.Dr2 = KdbTrapFrame.Tf.Dr2;
-            KdbCurrentTrapFrame->Tf.Dr3 = KdbTrapFrame.Tf.Dr3;
-            KdbCurrentTrapFrame->Tf.Dr6 = KdbTrapFrame.Tf.Dr6;
-            KdbCurrentTrapFrame->Tf.Dr7 = KdbTrapFrame.Tf.Dr7;
+            KdbCurrentTrapFrame->Dr0 = KdbTrapFrame.Dr0;
+            KdbCurrentTrapFrame->Dr1 = KdbTrapFrame.Dr1;
+            KdbCurrentTrapFrame->Dr2 = KdbTrapFrame.Dr2;
+            KdbCurrentTrapFrame->Dr3 = KdbTrapFrame.Dr3;
+            KdbCurrentTrapFrame->Dr6 = KdbTrapFrame.Dr6;
+            KdbCurrentTrapFrame->Dr7 = KdbTrapFrame.Dr7;
         }
 
         BreakPoint->Data.Hw.DebugReg = i;
@@ -929,7 +849,7 @@ KdbpEnableBreakPoint(
 
     BreakPoint->Enabled = TRUE;
     if (BreakPoint->Type != KdbBreakPointTemporary)
-        KdbpPrint("Breakpoint %d enabled.\n", BreakPointNr);
+        KdbPrintf("Breakpoint %d enabled.\n", BreakPointNr);
 
     return TRUE;
 }
@@ -960,7 +880,7 @@ KdbpDisableBreakPoint(
 
     if (BreakPointNr < 0 || BreakPointNr >= KDB_MAXIMUM_BREAKPOINT_COUNT)
     {
-        KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
+        KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
         return FALSE;
     }
 
@@ -971,13 +891,13 @@ KdbpDisableBreakPoint(
 
     if (BreakPoint->Type == KdbBreakPointNone)
     {
-        KdbpPrint("Invalid breakpoint: %d\n", BreakPointNr);
+        KdbPrintf("Invalid breakpoint: %d\n", BreakPointNr);
         return FALSE;
     }
 
     if (BreakPoint->Enabled == FALSE)
     {
-        KdbpPrint("Breakpoint %d is not enabled.\n", BreakPointNr);
+        KdbPrintf("Breakpoint %d is not enabled.\n", BreakPointNr);
         return TRUE;
     }
 
@@ -990,7 +910,7 @@ KdbpDisableBreakPoint(
 
         if (!NT_SUCCESS(Status))
         {
-            KdbpPrint("Couldn't restore original instruction.\n");
+            KdbPuts("Couldn't restore original instruction.\n");
             return FALSE;
         }
 
@@ -1012,11 +932,11 @@ KdbpDisableBreakPoint(
         ASSERT(BreakPoint->Type == KdbBreakPointHardware);
 
         /* Clear the breakpoint. */
-        KdbTrapFrame.Tf.Dr7 &= ~(0x3 << (BreakPoint->Data.Hw.DebugReg * 2));
-        if ((KdbTrapFrame.Tf.Dr7 & 0xFF) == 0)
+        KdbTrapFrame.Dr7 &= ~(0x3 << (BreakPoint->Data.Hw.DebugReg * 2));
+        if ((KdbTrapFrame.Dr7 & 0xFF) == 0)
         {
             /* If no breakpoints are enabled then clear the exact match flags. */
-            KdbTrapFrame.Tf.Dr7 &= 0xFFFFFCFF;
+            KdbTrapFrame.Dr7 &= 0xFFFFFCFF;
         }
 
         for (i = 0; i < KdbHwBreakPointCount; i++)
@@ -1035,7 +955,7 @@ KdbpDisableBreakPoint(
 
     BreakPoint->Enabled = FALSE;
     if (BreakPoint->Type != KdbBreakPointTemporary)
-        KdbpPrint("Breakpoint %d disabled.\n", BreakPointNr);
+        KdbPrintf("Breakpoint %d disabled.\n", BreakPointNr);
 
     return TRUE;
 }
@@ -1234,7 +1154,7 @@ KdbpCallMainLoop(VOID)
  * Disables interrupts, releases display ownership, ...
  */
 static VOID
-KdbpInternalEnter()
+KdbpInternalEnter(VOID)
 {
     PETHREAD Thread;
     PVOID SavedInitialStack, SavedStackBase, SavedKernelStack;
@@ -1242,21 +1162,9 @@ KdbpInternalEnter()
 
     KbdDisableMouse();
 
-    if (KdpDebugMode.Screen &&
-        InbvIsBootDriverInstalled() &&
-        !InbvCheckDisplayOwnership())
-    {
-        /* Acquire ownership and reset the display */
-        InbvAcquireDisplayOwnership();
-        InbvResetDisplay();
-
-        /* Display debugger prompt */
-        InbvSolidColorFill(0, 0, 639, 479, 0);
-        InbvSetTextColor(15);
-        InbvInstallDisplayStringFilter(NULL);
-        InbvEnableDisplayString(TRUE);
-        InbvSetScrollRegion(0, 0, 639, 479);
-    }
+    /* Take control of the display */
+    if (KdpDebugMode.Screen)
+        KdpScreenAcquire();
 
     /* Call the interface's main loop on a different stack */
     Thread = PsGetCurrentThread();
@@ -1268,14 +1176,19 @@ KdbpInternalEnter()
     Thread->Tcb.StackLimit = (ULONG_PTR)KdbStack;
     Thread->Tcb.KernelStack = (char*)KdbStack + KDB_STACK_SIZE;
 
-    /*KdbpPrint("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);*/
+    // KdbPrintf("Switching to KDB stack 0x%08x-0x%08x (Current Stack is 0x%08x)\n", Thread->Tcb.StackLimit, Thread->Tcb.StackBase, Esp);
 
-    KdbpStackSwitchAndCall(KdbStack + KDB_STACK_SIZE - sizeof(ULONG), KdbpCallMainLoop);
+    KdbpStackSwitchAndCall(KdbStack + KDB_STACK_SIZE - KDB_STACK_RESERVE, KdbpCallMainLoop);
 
     Thread->Tcb.InitialStack = SavedInitialStack;
     Thread->Tcb.StackBase = SavedStackBase;
     Thread->Tcb.StackLimit = SavedStackLimit;
     Thread->Tcb.KernelStack = SavedKernelStack;
+
+    /* Release the display */
+    if (KdpDebugMode.Screen)
+        KdpScreenRelease();
+
     KbdEnableMouse();
 }
 
@@ -1320,6 +1233,9 @@ KdbpGetExceptionNumberFromStatus(
         case STATUS_FLOAT_MULTIPLE_TRAPS:
             Ret = 18;
             break;
+        case STATUS_ASSERTION_FAILURE:
+            Ret = 20;
+            break;
 
         default:
             Ret = RTL_NUMBER_OF(KdbEnterConditions) - 1;
@@ -1344,10 +1260,9 @@ KdbpGetExceptionNumberFromStatus(
  */
 KD_CONTINUE_TYPE
 KdbEnterDebuggerException(
-    IN PEXCEPTION_RECORD ExceptionRecord  OPTIONAL,
+    IN PEXCEPTION_RECORD64 ExceptionRecord,
     IN KPROCESSOR_MODE PreviousMode,
     IN PCONTEXT Context,
-    IN OUT PKTRAP_FRAME TrapFrame,
     IN BOOLEAN FirstChance)
 {
     KDB_ENTER_CONDITION EnterCondition;
@@ -1366,8 +1281,12 @@ KdbEnterDebuggerException(
     KdbCurrentProcess = PsGetCurrentProcess();
 
     /* Set continue type to kdContinue for single steps and breakpoints */
-    if (ExceptionCode == STATUS_SINGLE_STEP || ExceptionCode == STATUS_BREAKPOINT)
+    if (ExceptionCode == STATUS_SINGLE_STEP ||
+        ExceptionCode == STATUS_BREAKPOINT ||
+        ExceptionCode == STATUS_ASSERTION_FAILURE)
+    {
         ContinueType = kdContinue;
+    }
 
     /* Check if we should handle the exception. */
     /* FIXME - won't get all exceptions here :( */
@@ -1380,24 +1299,33 @@ KdbEnterDebuggerException(
         EnterConditionMet = FALSE;
     }
 
-    /* If we stopped on one of our breakpoints then let the user know. */
+    /* If we stopped on one of our breakpoints then let the user know */
     KdbLastBreakPointNr = -1;
     KdbEnteredOnSingleStep = FALSE;
 
     if (FirstChance && (ExceptionCode == STATUS_SINGLE_STEP || ExceptionCode == STATUS_BREAKPOINT) &&
-        (KdbLastBreakPointNr = KdbpIsBreakPointOurs(ExceptionCode, TrapFrame)) >= 0)
+        (KdbLastBreakPointNr = KdbpIsBreakPointOurs(ExceptionCode, Context)) >= 0)
     {
         BreakPoint = KdbBreakPoints + KdbLastBreakPointNr;
 
         if (ExceptionCode == STATUS_BREAKPOINT)
         {
-            /* ... and restore the original instruction. */
+            /* ... and restore the original instruction */
             if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess, BreakPoint->Address,
                                                      BreakPoint->Data.SavedInstruction, NULL)))
             {
-                KdbpPrint("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
+                KdbPuts("Couldn't restore original instruction after INT3! Cannot continue execution.\n");
                 KeBugCheck(0); // FIXME: Proper bugcode!
             }
+
+            /* Also since we are past the int3 now, decrement EIP in the
+               TrapFrame. This is only needed because KDBG insists on working
+               with the TrapFrame instead of with the Context, as it is supposed
+               to do. The context has already EIP point to the int3, since
+               KiDispatchException accounts for that. Whatever we do here with
+               the TrapFrame does not matter anyway, since KiDispatchException
+               will overwrite it with the values from the Context! */
+            KeSetContextPc(Context, KeGetContextPc(Context) - 1);
         }
 
         if ((BreakPoint->Type == KdbBreakPointHardware) &&
@@ -1414,17 +1342,15 @@ KdbEnterDebuggerException(
         else if (BreakPoint->Type == KdbBreakPointTemporary &&
                  BreakPoint->Process == KdbCurrentProcess)
         {
-            ASSERT((TrapFrame->EFlags & EFLAGS_TF) == 0);
+            ASSERT((Context->EFlags & EFLAGS_TF) == 0);
 
-            /* Delete the temporary breakpoint which was used to step over or into the instruction. */
+            /* Delete the temporary breakpoint which was used to step over or into the instruction */
             KdbpDeleteBreakPoint(-1, BreakPoint);
 
-            TrapFrame->Eip--;
-
             if (--KdbNumSingleSteps > 0)
             {
-                if ((KdbSingleStepOver && !KdbpStepOverInstruction(TrapFrame->Eip)) ||
-                    (!KdbSingleStepOver && !KdbpStepIntoInstruction(TrapFrame->Eip)))
+                if ((KdbSingleStepOver && !KdbpStepOverInstruction(KeGetContextPc(Context))) ||
+                    (!KdbSingleStepOver && !KdbpStepIntoInstruction(KeGetContextPc(Context))))
                 {
                     Context->EFlags |= EFLAGS_TF;
                 }
@@ -1457,7 +1383,7 @@ KdbEnterDebuggerException(
         if (BreakPoint->Condition)
         {
             /* Setup the KDB trap frame */
-            KdbpTrapFrameToKdbTrapFrame(TrapFrame, &KdbTrapFrame);
+            KdbTrapFrame = *Context;
 
             ull = 0;
             if (!KdbpRpnEvaluateParsedExpression(BreakPoint->Condition, &KdbTrapFrame, &ull, NULL, NULL))
@@ -1472,23 +1398,23 @@ KdbEnterDebuggerException(
 
         if (BreakPoint->Type == KdbBreakPointSoftware)
         {
-            KdbpPrint("\nEntered debugger on breakpoint #%d: EXEC 0x%04x:0x%08x\n",
-                      KdbLastBreakPointNr, TrapFrame->SegCs & 0xffff, TrapFrame->Eip);
+            KdbPrintf("\nEntered debugger on breakpoint #%d: EXEC 0x%04x:0x%p\n",
+                      KdbLastBreakPointNr, Context->SegCs & 0xffff, KeGetContextPc(Context));
         }
         else if (BreakPoint->Type == KdbBreakPointHardware)
         {
-            KdbpPrint("\nEntered debugger on breakpoint #%d: %s 0x%08x\n",
+            KdbPrintf("\nEntered debugger on breakpoint #%d: %s 0x%08x\n",
                       KdbLastBreakPointNr,
-                     (BreakPoint->Data.Hw.AccessType == KdbAccessRead) ? "READ" :
-                     ((BreakPoint->Data.Hw.AccessType == KdbAccessWrite) ? "WRITE" :
-                     ((BreakPoint->Data.Hw.AccessType == KdbAccessReadWrite) ? "RDWR" : "EXEC")),
-                     BreakPoint->Address);
+                      (BreakPoint->Data.Hw.AccessType == KdbAccessRead) ? "READ" :
+                      ((BreakPoint->Data.Hw.AccessType == KdbAccessWrite) ? "WRITE" :
+                      ((BreakPoint->Data.Hw.AccessType == KdbAccessReadWrite) ? "RDWR" : "EXEC")),
+                      BreakPoint->Address);
         }
     }
     else if (ExceptionCode == STATUS_SINGLE_STEP)
     {
         /* Silently ignore a debugger initiated single step. */
-        if ((TrapFrame->Dr6 & 0xf) == 0 && KdbBreakPointToReenable)
+        if ((Context->Dr6 & 0xf) == 0 && KdbBreakPointToReenable)
         {
             /* FIXME: Make sure that the breakpoint was really hit (check bp->Address vs. tf->Eip) */
             BreakPoint = KdbBreakPointToReenable;
@@ -1503,7 +1429,7 @@ KdbEnterDebuggerException(
             if (!NT_SUCCESS(KdbpOverwriteInstruction(KdbCurrentProcess, BreakPoint->Address, 0xCC,
                                                      &BreakPoint->Data.SavedInstruction)))
             {
-                KdbpPrint("Warning: Couldn't reenable breakpoint %d\n",
+                KdbPrintf("Warning: Couldn't reenable breakpoint %d\n",
                           BreakPoint - KdbBreakPoints);
             }
 
@@ -1511,17 +1437,23 @@ KdbEnterDebuggerException(
             if (KdbNumSingleSteps == 0)
                 Context->EFlags &= ~EFLAGS_TF;
 
-            goto continue_execution; /* return */
+            if (!KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep)
+            {
+                goto continue_execution; /* return */
+            }
         }
 
+        /* Quoth the raven, 'Nevermore!' */
+        KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep = FALSE;
+
         /* Check if we expect a single step */
-        if ((TrapFrame->Dr6 & 0xf) == 0 && KdbNumSingleSteps > 0)
+        if ((Context->Dr6 & 0xf) == 0 && KdbNumSingleSteps > 0)
         {
             /*ASSERT((Context->Eflags & EFLAGS_TF) != 0);*/
             if (--KdbNumSingleSteps > 0)
             {
-                if ((KdbSingleStepOver && KdbpStepOverInstruction(TrapFrame->Eip)) ||
-                    (!KdbSingleStepOver && KdbpStepIntoInstruction(TrapFrame->Eip)))
+                if ((KdbSingleStepOver && KdbpStepOverInstruction(KeGetContextPc(Context))) ||
+                    (!KdbSingleStepOver && KdbpStepIntoInstruction(KeGetContextPc(Context))))
                 {
                     Context->EFlags &= ~EFLAGS_TF;
                 }
@@ -1545,7 +1477,7 @@ KdbEnterDebuggerException(
                 return kdHandleException;
             }
 
-            KdbpPrint("\nEntered debugger on unexpected debug trap!\n");
+            KdbPuts("\nEntered debugger on unexpected debug trap!\n");
         }
     }
     else if (ExceptionCode == STATUS_BREAKPOINT)
@@ -1560,46 +1492,28 @@ KdbEnterDebuggerException(
             return kdHandleException;
         }
 
-        KdbpPrint("\nEntered debugger on embedded INT3 at 0x%04x:0x%08x.\n",
-                  TrapFrame->SegCs & 0xffff, TrapFrame->Eip - 1);
+        KdbPrintf("\nEntered debugger on embedded INT3 at 0x%04x:0x%p.\n",
+                  Context->SegCs & 0xffff, KeGetContextPc(Context));
     }
     else
     {
         const CHAR *ExceptionString = (ExpNr < RTL_NUMBER_OF(ExceptionNrToString)) ?
-                                      (ExceptionNrToString[ExpNr]) :
-                                      ("Unknown/User defined exception");
+                                      ExceptionNrToString[ExpNr] :
+                                      "Unknown/User defined exception";
 
         if (!EnterConditionMet)
         {
             return ContinueType;
         }
 
-        KdbpPrint("\nEntered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
+        KdbPrintf("\nEntered debugger on %s-chance exception (Exception Code: 0x%x) (%s)\n",
                   FirstChance ? "first" : "last", ExceptionCode, ExceptionString);
 
         if (ExceptionCode == STATUS_ACCESS_VIOLATION &&
             ExceptionRecord && ExceptionRecord->NumberParameters != 0)
         {
-            /* FIXME: Add noexec memory stuff */
-            ULONG_PTR TrapCr2;
-            ULONG Err;
-
-            TrapCr2 = __readcr2();
-
-            Err = TrapFrame->ErrCode;
-            KdbpPrint("Memory at 0x%p could not be %s: ", TrapCr2, (Err & (1 << 1)) ? "written" : "read");
-
-            if ((Err & (1 << 0)) == 0)
-            {
-                KdbpPrint("Page not present.\n");
-            }
-            else
-            {
-                if ((Err & (1 << 3)) != 0)
-                    KdbpPrint("Reserved bits in page directory set.\n");
-                else
-                    KdbpPrint("Page protection violation.\n");
-            }
+            ULONG_PTR TrapCr2 = __readcr2();
+            KdbPrintf("Memory at 0x%p could not be accessed\n", TrapCr2);
         }
     }
 
@@ -1612,16 +1526,16 @@ KdbEnterDebuggerException(
     KdbCurrentTrapFrame = &KdbTrapFrame;
 
     /* Setup the KDB trap frame */
-    KdbpTrapFrameToKdbTrapFrame(TrapFrame, &KdbTrapFrame);
+    KdbTrapFrame = *Context;
 
     /* Enter critical section */
     OldEflags = __readeflags();
     _disable();
 
-    /* HACK: Save the current IRQL and pretend we are at passive level,
-     * although interrupts are off. Needed because KDBG calls pageable code. */
+    /* HACK: Save the current IRQL and pretend we are at dispatch level */
     OldIrql = KeGetCurrentIrql();
-    KeLowerIrql(PASSIVE_LEVEL);
+    if (OldIrql > DISPATCH_LEVEL)
+        KeLowerIrql(DISPATCH_LEVEL);
 
     /* Exception inside the debugger? Game over. */
     if (InterlockedIncrement(&KdbEntryCount) > 1)
@@ -1630,21 +1544,24 @@ KdbEnterDebuggerException(
         return kdHandleException;
     }
 
-    /* Call the main loop. */
+    /* Call the main loop */
     KdbpInternalEnter();
 
     /* Check if we should single step */
     if (KdbNumSingleSteps > 0)
     {
-        if ((KdbSingleStepOver && KdbpStepOverInstruction(KdbCurrentTrapFrame->Tf.Eip)) ||
-            (!KdbSingleStepOver && KdbpStepIntoInstruction(KdbCurrentTrapFrame->Tf.Eip)))
+        /* Variable explains itself! */
+        KdbpEvenThoughWeHaveABreakPointToReenableWeAlsoHaveARealSingleStep = TRUE;
+
+        if ((KdbSingleStepOver && KdbpStepOverInstruction(KeGetContextPc(KdbCurrentTrapFrame))) ||
+            (!KdbSingleStepOver && KdbpStepIntoInstruction(KeGetContextPc(KdbCurrentTrapFrame))))
         {
-            ASSERT((KdbCurrentTrapFrame->Tf.EFlags & EFLAGS_TF) == 0);
-            /*KdbCurrentTrapFrame->Tf.EFlags &= ~EFLAGS_TF;*/
+            ASSERT((KdbCurrentTrapFrame->EFlags & EFLAGS_TF) == 0);
+            /*KdbCurrentTrapFrame->EFlags &= ~EFLAGS_TF;*/
         }
         else
         {
-            Context->EFlags |= EFLAGS_TF;
+            KdbTrapFrame.EFlags |= EFLAGS_TF;
         }
     }
 
@@ -1656,14 +1573,15 @@ KdbEnterDebuggerException(
         KeUnstackDetachProcess(&KdbApcState);
     }
 
-    /* Update the exception TrapFrame */
-    KdbpKdbTrapFrameToTrapFrame(&KdbTrapFrame, TrapFrame);
+    /* Update the exception Context */
+    *Context = KdbTrapFrame;
 
     /* Decrement the entry count */
     InterlockedDecrement(&KdbEntryCount);
 
-    /* HACK: Raise back to old IRWL */
-    KeRaiseIrql(OldIrql, &OldIrql);
+    /* HACK: Raise back to old IRQL */
+    if (OldIrql > DISPATCH_LEVEL)
+        KeRaiseIrql(OldIrql, &OldIrql);
 
     /* Leave critical section */
     __writeeflags(OldEflags);
@@ -1683,16 +1601,16 @@ continue_execution:
         /* Set the RF flag so we don't trigger the same breakpoint again. */
         if (Resume)
         {
-            TrapFrame->EFlags |= EFLAGS_RF;
+            Context->EFlags |= EFLAGS_RF;
         }
 
         /* Clear dr6 status flags. */
-        TrapFrame->Dr6 &= ~0x0000e00f;
+        Context->Dr6 &= ~0x0000e00f;
 
         if (!(KdbEnteredOnSingleStep && KdbSingleStepOver))
         {
             /* Skip the current instruction */
-            Context->Eip++;
+            KeSetContextPc(Context, KeGetContextPc(Context) + KD_BREAKPOINT_SIZE);
         }
     }
 
@@ -1700,35 +1618,24 @@ continue_execution:
 }
 
 VOID
-NTAPI
-INIT_FUNCTION
 KdbpGetCommandLineSettings(
-    PCHAR p1)
+    _In_ PCSTR p1)
 {
-    PCHAR p2;
+#define CONST_STR_LEN(x) (sizeof(x)/sizeof(x[0]) - 1)
 
-    while (p1 && (p2 = strchr(p1, ' ')))
+    while (p1 && *p1)
     {
-        p2 += 2;
+        /* Skip leading whitespace */
+        while (*p1 == ' ') ++p1;
 
-        if (!_strnicmp(p2, "KDSERIAL", 8))
-        {
-            p2 += 8;
-            KdbDebugState |= KD_DEBUG_KDSERIAL;
-            KdpDebugMode.Serial = TRUE;
-        }
-        else if (!_strnicmp(p2, "KDNOECHO", 8))
-        {
-            p2 += 8;
-            KdbDebugState |= KD_DEBUG_KDNOECHO;
-        }
-        else if (!_strnicmp(p2, "FIRSTCHANCE", 11))
+        if (!_strnicmp(p1, "FIRSTCHANCE", CONST_STR_LEN("FIRSTCHANCE")))
         {
-            p2 += 11;
+            p1 += CONST_STR_LEN("FIRSTCHANCE");
             KdbpSetEnterCondition(-1, TRUE, KdbEnterAlways);
         }
 
-        p1 = p2;
+        /* Move on to the next option */
+        p1 = strchr(p1, ' ');
     }
 }
 
@@ -1738,34 +1645,12 @@ KdbpSafeReadMemory(
     IN PVOID Src,
     IN ULONG Bytes)
 {
-    BOOLEAN Result = TRUE;
-
-    switch (Bytes)
-    {
-        case 1:
-        case 2:
-        case 4:
-        case 8:
-            Result = KdpSafeReadMemory((ULONG_PTR)Src, Bytes, Dest);
-            break;
-
-        default:
-        {
-            ULONG_PTR Start, End, Write;
-
-            for (Start = (ULONG_PTR)Src,
-                    End = Start + Bytes,
-                    Write = (ULONG_PTR)Dest;
-                 Result && (Start < End);
-                 Start++, Write++)
-                if (!KdpSafeReadMemory(Start, 1, (PVOID)Write))
-                    Result = FALSE;
-
-            break;
-        }
-    }
-
-    return Result ? STATUS_SUCCESS : STATUS_ACCESS_VIOLATION;
+    return KdpCopyMemoryChunks((ULONG64)(ULONG_PTR)Src,
+                               Dest,
+                               Bytes,
+                               0,
+                               MMDBG_COPY_UNSAFE,
+                               NULL);
 }
 
 NTSTATUS
@@ -1774,16 +1659,10 @@ KdbpSafeWriteMemory(
     IN PVOID Src,
     IN ULONG Bytes)
 {
-    BOOLEAN Result = TRUE;
-    ULONG_PTR Start, End, Write;
-
-    for (Start = (ULONG_PTR)Src,
-            End = Start + Bytes,
-            Write = (ULONG_PTR)Dest;
-         Result && (Start < End);
-         Start++, Write++)
-        if (!KdpSafeWriteMemory(Write, 1, *((PCHAR)Start)))
-            Result = FALSE;
-
-    return Result ? STATUS_SUCCESS : STATUS_ACCESS_VIOLATION;
+    return KdpCopyMemoryChunks((ULONG64)(ULONG_PTR)Dest,
+                               Src,
+                               Bytes,
+                               0,
+                               MMDBG_COPY_UNSAFE | MMDBG_COPY_WRITE,
+                               NULL);
 }