X-Git-Url: https://git.reactos.org/?p=reactos.git;a=blobdiff_plain;f=reactos%2Fntoskrnl%2Fkdbg%2Fkdb.c;h=49e68ef1012c561c19d88e323ffa29b0a27fbb2c;hp=972d4b4f52e11852633b7d88e27069cbd21671fb;hb=db41ecbbff4962a627e56241263d048b23dea403;hpb=fbf17221970853700b114ea3c08a0f965b2839a1 diff --git a/reactos/ntoskrnl/kdbg/kdb.c b/reactos/ntoskrnl/kdbg/kdb.c index 972d4b4f52e..49e68ef1012 100644 --- a/reactos/ntoskrnl/kdbg/kdb.c +++ b/reactos/ntoskrnl/kdbg/kdb.c @@ -15,10 +15,6 @@ /* TYPES *********************************************************************/ -/* FIXME: NDK headers */ -#define TempEsp TempEip -#define TempSegSs TempCs - /* DEFINES *******************************************************************/ #define KDB_STACK_SIZE (4096*3) @@ -108,15 +104,33 @@ 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, Esp)); - RtlZeroMemory((PVOID)((ULONG_PTR)&KdbTrapFrame->Tf + FIELD_OFFSET(KTRAP_FRAME, Esp)), - sizeof (KTRAP_FRAME) - FIELD_OFFSET(KTRAP_FRAME, Esp)); + 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" @@ -124,22 +138,11 @@ KdbpTrapFrameToKdbTrapFrame(PKTRAP_FRAME TrapFrame, PKDB_KTRAP_FRAME KdbTrapFram "movl %%cr4, %3" "\n\t" : "=r"(KdbTrapFrame->Cr0), "=r"(KdbTrapFrame->Cr2), "=r"(KdbTrapFrame->Cr3), "=r"(KdbTrapFrame->Cr4)); - - if (TrapFrame->PreviousMode == KernelMode) - { - /* If the trapframe is a kmode one use the temp ss:esp */ - KdbTrapFrame->Tf.Esp = (ULONG)TrapFrame->TempEsp; - KdbTrapFrame->Tf.Ss = (USHORT)((ULONG)TrapFrame->TempSegSs & 0xFFFF); - } - else - { - /* Otherwise use ss:esp pushed by the CPU */ - /* FIXME: maybe change all trapframes to always put ss:esp into tempss:tempesp so we - * can handle umode and kmode the same way */ - KdbTrapFrame->Tf.Esp = TrapFrame->Esp; - KdbTrapFrame->Tf.Ss = TrapFrame->Ss; - } - + + KdbTrapFrame->Tf.HardwareEsp = KiEspFromTrapFrame(TrapFrame); + KdbTrapFrame->Tf.HardwareSegSs = (USHORT)(KiSsFromTrapFrame(TrapFrame) & 0xFFFF); + + /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */ } @@ -147,24 +150,12 @@ 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, Esp)); + RtlCopyMemory(TrapFrame, &KdbTrapFrame->Tf, FIELD_OFFSET(KTRAP_FRAME, HardwareEsp)); /* FIXME: write cr0, cr2, cr3 and cr4 (not needed atm) */ - if (TrapFrame->PreviousMode == KernelMode) - { - /* If the trapframe is a kmode one write to the temp ss:esp */ - TrapFrame->TempEsp = (PVOID)KdbTrapFrame->Tf.Esp; - TrapFrame->TempSegSs = (PVOID)(((ULONG)TrapFrame->TempSegSs & ~0xffff) | KdbTrapFrame->Tf.Ss); - } - else - { - /* Otherwise write to ss:esp pushed by the CPU */ - /* FIXME: maybe change all trap-epilogs to always put temp ss:esp into ss:esp so we - * can handle umode and kmode the same way */ - TrapFrame->Esp = KdbTrapFrame->Tf.Esp; - TrapFrame->Ss = KdbTrapFrame->Tf.Ss; - } + KiSsToTrapFrame(TrapFrame, KdbTrapFrame->Tf.HardwareSegSs); + KiEspToTrapFrame(TrapFrame, KdbTrapFrame->Tf.HardwareEsp); /* FIXME: copy v86 registers if TrapFrame is a V86 trapframe */ } @@ -203,7 +194,7 @@ KdbpOverwriteInstruction( /* 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. */ @@ -260,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; } @@ -320,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]; @@ -332,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; } @@ -342,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; @@ -353,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);*/ @@ -363,7 +351,7 @@ 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; } @@ -510,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) @@ -560,13 +548,13 @@ 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); @@ -658,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 */ @@ -752,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; @@ -864,7 +852,7 @@ KdbpDisableBreakPoint( IN LONG BreakPointNr OPTIONAL, IN OUT PKDB_BREAKPOINT BreakPoint OPTIONAL) { - INT i; + UINT i; NTSTATUS Status; if (BreakPointNr < 0) @@ -914,7 +902,7 @@ KdbpDisableBreakPoint( break; } } - if (i != -1) /* not found */ + if (i != (UINT)-1) /* not found */ ASSERT(0); } else @@ -940,7 +928,7 @@ KdbpDisableBreakPoint( break; } } - if (i != -1) /* not found */ + if (i != (UINT)-1) /* not found */ ASSERT(0); } @@ -965,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]; @@ -989,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 */ @@ -1001,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 */ { @@ -1078,7 +1066,7 @@ KdbpAttachToThread( } if (KdbOriginalProcess != Process) { - KeStackAttachProcess(EPROCESS_TO_KPROCESS(Process), &KdbApcState); + KeStackAttachProcess(&Process->Pcb, &KdbApcState); } KdbCurrentProcess = Process; } @@ -1189,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; @@ -1209,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; @@ -1257,7 +1245,7 @@ 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. @@ -1269,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 */ } @@ -1285,7 +1273,7 @@ KdbEnterDebuggerException( BreakPoint->Type == KdbBreakPointTemporary) { ASSERT(ExpNr == 3); - TrapFrame->Eflags |= X86_EFLAGS_TF; + TrapFrame->EFlags |= X86_EFLAGS_TF; KdbBreakPointToReenable = BreakPoint; } @@ -1319,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) { @@ -1358,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 */ } @@ -1371,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 @@ -1405,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 { @@ -1423,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 @@ -1470,12 +1459,12 @@ 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; } } @@ -1507,7 +1496,7 @@ 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. */ @@ -1551,3 +1540,47 @@ KdbpGetCommandLineSettings(PCHAR p1) 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; +}