Author: hbelusca Date: Tue May 7 00:14:36 2013 New Revision: 58964 URL: http://svn.reactos.org/svn/reactos?rev=58964&view=rev Log: [KDROSDBG] In fact, the kdcom used for KDBG (in GCC builds) is really a thin layer atop of CPORTLIB. Remove extra code (remove also the KD_PORT_INFORMATION type) and replace it with calls to CPORTLIB functions. Next step is to put all that thing into KDBG directly, as helper functions. Then, if one wants to make a really kdcom (as we have for MSVC builds), one has to refactor all KDBG.
Modified: branches/kd++/drivers/base/kdcom/kdcom.c branches/kd++/drivers/base/kdrosdbg/arm/kdbg.c branches/kd++/drivers/base/kdrosdbg/i386/kdbg.c branches/kd++/drivers/base/kdrosdbg/kdrosdbg.spec branches/kd++/ntoskrnl/include/internal/kd.h branches/kd++/ntoskrnl/kd/kdinit.c branches/kd++/ntoskrnl/kd/kdio.c branches/kd++/ntoskrnl/kd/wrappers/gdbstub.c branches/kd++/ntoskrnl/kd/wrappers/gdbstub_powerpc.c Modified: branches/kd++/drivers/base/kdcom/kdcom.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/drivers/base/kdcom/kdcom.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/drivers/base/kdcom/kdcom.c [iso-8859-1] (original) +++ branches/kd++/drivers/base/kdcom/kdcom.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -17,8 +17,7 @@ #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */ #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */ -#define DEFAULT_BAUD_RATE 19200 - +#define DEFAULT_BAUD_RATE 19200 #if defined(_M_IX86) || defined(_M_AMD64) const ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8}; Modified: branches/kd++/drivers/base/kdrosdbg/arm/kdbg.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/drivers/base/kdrosdbg/arm/kdbg.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/drivers/base/kdrosdbg/arm/kdbg.c [iso-8859-1] (original) +++ branches/kd++/drivers/base/kdrosdbg/arm/kdbg.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -10,27 +10,20 @@ #define NOEXTAPI #include <ntifs.h> +#include <arc/arc.h> #include <halfuncs.h> +#include <windbgkd.h> +#include <ioaccess.h> /* port intrinsics */ +#include <cportlib/cportlib.h> +#include <arm/peripherals/pl011.h> #include <stdio.h> -#include <arc/arc.h> -#include <windbgkd.h> -#include <kddll.h> -#include <ioaccess.h> -#include <arm/peripherals/pl011.h> #define NDEBUG #include <debug.h> /* GLOBALS ********************************************************************/ -typedef struct _KD_PORT_INFORMATION -{ - ULONG ComPort; - ULONG BaudRate; - ULONG BaseAddress; -} KD_PORT_INFORMATION, *PKD_PORT_INFORMATION; - -KD_PORT_INFORMATION DefaultPort = {0, 0, 0}; +CPPORT DefaultPort = {0, 0, 0}; // // We need to build this in the configuration root and use KeFindConfigurationEntry @@ -42,42 +35,41 @@ BOOLEAN NTAPI -KdPortInitializeEx(IN PKD_PORT_INFORMATION PortInformation, - IN ULONG Unknown1, - IN ULONG Unknown2) +KdPortInitializeEx(IN PCPPORT PortInformation, + IN ULONG ComPortNumber) { ULONG Divider, Remainder, Fraction; ULONG Baudrate = PortInformation->BaudRate; - + // // Calculate baudrate clock divider and remainder // Divider = HACK / (16 * Baudrate); Remainder = HACK % (16 * Baudrate); - + // // Calculate the fractional part // Fraction = (8 * Remainder / Baudrate) >> 1; Fraction += (8 * Remainder / Baudrate) & 1; - + // // Disable interrupts // WRITE_REGISTER_ULONG(UART_PL011_CR, 0); - + // // Set the baud rate // WRITE_REGISTER_ULONG(UART_PL011_IBRD, Divider); WRITE_REGISTER_ULONG(UART_PL011_FBRD, Fraction); - + // // Set 8 bits for data, 1 stop bit, no parity, FIFO enabled // WRITE_REGISTER_ULONG(UART_PL011_LCRH, UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN); - + // // Clear and enable FIFO // @@ -85,7 +77,7 @@ UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE); - + // // Done // @@ -94,7 +86,7 @@ BOOLEAN NTAPI -KdPortGetByteEx(IN PKD_PORT_INFORMATION PortInformation, +KdPortGetByteEx(IN PCPPORT PortInformation, OUT PUCHAR ByteReceived) { UNIMPLEMENTED; @@ -104,7 +96,7 @@ VOID NTAPI -KdPortPutByteEx(IN PKD_PORT_INFORMATION PortInformation, +KdPortPutByteEx(IN PCPPORT PortInformation, IN UCHAR ByteToSend) { // Modified: branches/kd++/drivers/base/kdrosdbg/i386/kdbg.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/drivers/base/kdrosdbg/i386/kdbg.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/drivers/base/kdrosdbg/i386/kdbg.c [iso-8859-1] (original) +++ branches/kd++/drivers/base/kdrosdbg/i386/kdbg.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -11,26 +11,18 @@ #define NOEXTAPI #include <ntifs.h> +#include <arc/arc.h> #include <halfuncs.h> -#include <stdio.h> -#include <arc/arc.h> #include <windbgkd.h> -#include <kddll.h> #include <ioaccess.h> /* port intrinsics */ #include <cportlib/cportlib.h> +#include <stdio.h> #define NDEBUG #include <debug.h> -typedef struct _KD_PORT_INFORMATION -{ - ULONG ComPort; - ULONG BaudRate; - ULONG BaseAddress; -} KD_PORT_INFORMATION, *PKD_PORT_INFORMATION; - -#define DEFAULT_BAUD_RATE 19200 +#define DEFAULT_BAUD_RATE 19200 #if defined(_M_IX86) || defined(_M_AMD64) const ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8}; @@ -44,57 +36,11 @@ #error Unknown architecture #endif -/* MACROS *******************************************************************/ +/* STATIC VARIABLES ***********************************************************/ -#define SER_RBR(x) ((PUCHAR)(x)+0) -#define SER_THR(x) ((PUCHAR)(x)+0) -#define SER_DLL(x) ((PUCHAR)(x)+0) -#define SER_IER(x) ((PUCHAR)(x)+1) -#define SR_IER_ERDA 0x01 -#define SR_IER_ETHRE 0x02 -#define SR_IER_ERLSI 0x04 -#define SR_IER_EMS 0x08 -#define SR_IER_ALL 0x0F -#define SER_DLM(x) ((PUCHAR)(x)+1) -#define SER_IIR(x) ((PUCHAR)(x)+2) -#define SER_FCR(x) ((PUCHAR)(x)+2) -#define SR_FCR_ENABLE_FIFO 0x01 -#define SR_FCR_CLEAR_RCVR 0x02 -#define SR_FCR_CLEAR_XMIT 0x04 -#define SER_LCR(x) ((PUCHAR)(x)+3) -#define SR_LCR_CS5 0x00 -#define SR_LCR_CS6 0x01 -#define SR_LCR_CS7 0x02 -#define SR_LCR_CS8 0x03 -#define SR_LCR_ST1 0x00 -#define SR_LCR_ST2 0x04 -#define SR_LCR_PNO 0x00 -#define SR_LCR_POD 0x08 -#define SR_LCR_PEV 0x18 -#define SR_LCR_PMK 0x28 -#define SR_LCR_PSP 0x38 -#define SR_LCR_BRK 0x40 -#define SR_LCR_DLAB 0x80 -#define SER_MCR(x) ((PUCHAR)(x)+4) -#define SR_MCR_DTR 0x01 -#define SR_MCR_RTS 0x02 -#define SR_MCR_OUT1 0x04 -#define SR_MCR_OUT2 0x08 -#define SR_MCR_LOOP 0x10 -#define SER_LSR(x) ((PUCHAR)(x)+5) -#define SR_LSR_DR 0x01 -#define SR_LSR_TBE 0x20 -#define SER_MSR(x) ((PUCHAR)(x)+6) -#define SR_MSR_CTS 0x10 -#define SR_MSR_DSR 0x20 -#define SER_SCR(x) ((PUCHAR)(x)+7) +// static CPPORT DefaultPort = {0, 0, 0}; - -/* STATIC VARIABLES *********************************************************/ - -// static KD_PORT_INFORMATION DefaultPort = { 0, 0, 0 }; - -/* The com port must only be initialized once! */ +/* The COM port must only be initialized once! */ // static BOOLEAN PortInitialized = FALSE; @@ -103,14 +49,11 @@ BOOLEAN NTAPI KdPortInitializeEx( - IN PKD_PORT_INFORMATION PortInformation, - IN ULONG Unknown1, - IN ULONG Unknown2) + IN PCPPORT PortInformation, + IN ULONG ComPortNumber) { - ULONG ComPortBase; + NTSTATUS Status; CHAR buffer[80]; - ULONG divisor; - UCHAR lcr; #if 0 // Deactivated because never used in fact (was in KdPortInitialize but we use KdPortInitializeEx) /* @@ -122,7 +65,7 @@ { DefaultPort.BaudRate = PortInformation->BaudRate; - if (PortInformation->ComPort == 0) + if (ComPortNumber == 0) { /* * Start enumerating COM ports from the last one to the first one, @@ -134,12 +77,12 @@ { if (CpDoesPortExist(UlongToPtr(BaseArray[i]))) { - PortInformation->BaseAddress = DefaultPort.BaseAddress = BaseArray[i]; - PortInformation->ComPort = DefaultPort.ComPort = i; + PortInformation->Address = DefaultPort.Address = BaseArray[i]; + ComPortNumber = (ULONG)i; break; } } - if (i == 0) + if (ComPortNumber == 0) { sprintf(buffer, "\nKernel Debugger: No COM port found!\n\n"); @@ -155,108 +98,54 @@ /* * Initialize the port */ - - if (PortInformation->BaudRate == 0) - PortInformation->BaudRate = DEFAULT_BAUD_RATE; - - if (PortInformation->ComPort != 0) + Status = CpInitialize(PortInformation, + (ComPortNumber == 0 ? PortInformation->Address + : UlongToPtr(BaseArray[ComPortNumber])), + (PortInformation->BaudRate == 0 ? DEFAULT_BAUD_RATE + : PortInformation->BaudRate)); + if (!NT_SUCCESS(Status)) { - if (!CpDoesPortExist(UlongToPtr(BaseArray[PortInformation->ComPort]))) - { - sprintf(buffer, - "\nKernel Debugger: Serial port not found!\n\n"); - HalDisplayString(buffer); - return FALSE; - } - - ComPortBase = BaseArray[PortInformation->ComPort]; - PortInformation->BaseAddress = ComPortBase; + sprintf(buffer, + "\nKernel Debugger: Serial port not found!\n\n"); + HalDisplayString(buffer); + return FALSE; } else { - ComPortBase = PortInformation->BaseAddress; - } - - if (ComPortBase == 0) - return FALSE; - #ifndef NDEBUG - sprintf(buffer, - "\nSerial port COM%ld found at 0x%lx\n", - PortInformation->ComPort, - ComPortBase); - HalDisplayString(buffer); -#endif /* NDEBUG */ - - /* set baud rate and data format (8N1) */ - - /* turn on DTR and RTS */ - WRITE_PORT_UCHAR(SER_MCR(ComPortBase), SR_MCR_DTR | SR_MCR_RTS); - - /* set DLAB */ - lcr = READ_PORT_UCHAR(SER_LCR(ComPortBase)) | SR_LCR_DLAB; - WRITE_PORT_UCHAR(SER_LCR(ComPortBase), lcr); - - /* set baud rate */ - divisor = 115200 / PortInformation->BaudRate; - WRITE_PORT_UCHAR(SER_DLL(ComPortBase), (UCHAR)(divisor & 0xff)); - WRITE_PORT_UCHAR(SER_DLM(ComPortBase), (UCHAR)((divisor >> 8) & 0xff)); - - /* reset DLAB and set 8N1 format */ - WRITE_PORT_UCHAR(SER_LCR(ComPortBase), - SR_LCR_CS8 | SR_LCR_ST1 | SR_LCR_PNO); - - /* read junk out of the RBR */ - lcr = READ_PORT_UCHAR(SER_RBR(ComPortBase)); - -#ifndef NDEBUG - /* print message to blue screen */ - sprintf(buffer, - "\nKernel Debugger: COM%ld (Port 0x%lx) BaudRate %ld\n\n", - PortInformation->ComPort, - ComPortBase, - PortInformation->BaudRate); - - HalDisplayString(buffer); + /* Print message to blue screen */ + sprintf(buffer, + "\nKernel Debugger: Serial port found: COM%ld (Port 0x%lx) BaudRate %ld\n\n", + ComPortNumber, + PortInformation->Address, + PortInformation->BaudRate); + HalDisplayString(buffer); #endif /* NDEBUG */ #if 0 - /* set global info */ - KdComPortInUse = (PUCHAR)DefaultPort.BaseAddress; + /* Set global info */ + KdComPortInUse = DefaultPort.Address; #endif - - return TRUE; + return TRUE; + } } BOOLEAN NTAPI KdPortGetByteEx( - IN PKD_PORT_INFORMATION PortInformation, + IN PCPPORT PortInformation, OUT PUCHAR ByteReceived) { - PUCHAR ComPortBase = (PUCHAR)PortInformation->BaseAddress; - - if ((READ_PORT_UCHAR(SER_LSR(ComPortBase)) & SR_LSR_DR)) - { - *ByteReceived = READ_PORT_UCHAR(SER_RBR(ComPortBase)); - return TRUE; - } - - return FALSE; + return (CpGetByte(PortInformation, ByteReceived, FALSE) == CP_GET_SUCCESS); } VOID NTAPI KdPortPutByteEx( - IN PKD_PORT_INFORMATION PortInformation, + IN PCPPORT PortInformation, IN UCHAR ByteToSend) { - PUCHAR ComPortBase = (PUCHAR)PortInformation->BaseAddress; - - while ((READ_PORT_UCHAR(SER_LSR(ComPortBase)) & SR_LSR_TBE) == 0) - ; - - WRITE_PORT_UCHAR(SER_THR(ComPortBase), ByteToSend); + CpPutByte(PortInformation, ByteToSend); } /* EOF */ Modified: branches/kd++/drivers/base/kdrosdbg/kdrosdbg.spec URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/drivers/base/kdrosdbg/kdrosdbg.spec?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/drivers/base/kdrosdbg/kdrosdbg.spec [iso-8859-1] (original) +++ branches/kd++/drivers/base/kdrosdbg/kdrosdbg.spec [iso-8859-1] Tue May 7 00:14:36 2013 @@ -8,6 +8,6 @@ 8 stdcall KdSendPacket(long ptr ptr ptr) ; Legacy KD -@ stdcall KdPortInitializeEx(ptr long long) +@ stdcall KdPortInitializeEx(ptr long) @ stdcall KdPortGetByteEx(ptr ptr) @ stdcall KdPortPutByteEx(ptr long) Modified: branches/kd++/ntoskrnl/include/internal/kd.h URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/ntoskrnl/include/internal/kd.h?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/ntoskrnl/include/internal/kd.h [iso-8859-1] (original) +++ branches/kd++/ntoskrnl/include/internal/kd.h [iso-8859-1] Tue May 7 00:14:36 2013 @@ -1,4 +1,6 @@ #pragma once + +#include <cportlib/cportlib.h> #ifdef _M_PPC #define KdDebuggerEnabled _KdDebuggerEnabled @@ -8,15 +10,8 @@ // // Kernel Debugger Port Definition // -typedef struct _KD_PORT_INFORMATION -{ - ULONG ComPort; - ULONG BaudRate; - ULONG BaseAddress; -} KD_PORT_INFORMATION, *PKD_PORT_INFORMATION; - struct _KD_DISPATCH_TABLE; -extern KD_PORT_INFORMATION GdbPortInfo; +extern CPPORT GdbPortInfo; extern BOOLEAN _KdDebuggerEnabled; extern BOOLEAN _KdDebuggerNotPresent; extern BOOLEAN KdBreakAfterSymbolLoad; @@ -26,21 +21,20 @@ BOOLEAN NTAPI KdPortInitializeEx( - PKD_PORT_INFORMATION PortInformation, - ULONG Unknown1, - ULONG Unknown2 + PCPPORT PortInformation, + ULONG ComPortNumber ); BOOLEAN NTAPI KdPortGetByteEx( - PKD_PORT_INFORMATION PortInformation, + PCPPORT PortInformation, PUCHAR ByteReceived); VOID NTAPI KdPortPutByteEx( - PKD_PORT_INFORMATION PortInformation, + PCPPORT PortInformation, UCHAR ByteToSend ); @@ -340,7 +334,8 @@ extern ULONG KdpPort; /* Port Information for the Serial Native Mode */ -extern KD_PORT_INFORMATION SerialPortInfo; +extern ULONG SerialPortNumber; +extern CPPORT SerialPortInfo; /* Init Functions for Native Providers */ extern PKDP_INIT_ROUTINE InitRoutines[KdMax]; Modified: branches/kd++/ntoskrnl/kd/kdinit.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/ntoskrnl/kd/kdinit.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/ntoskrnl/kd/kdinit.c [iso-8859-1] (original) +++ branches/kd++/ntoskrnl/kd/kdinit.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -16,7 +16,8 @@ /* VARIABLES ***************************************************************/ -KD_PORT_INFORMATION PortInfo = {DEFAULT_DEBUG_PORT, DEFAULT_DEBUG_BAUD_RATE, 0}; +ULONG PortNumber = DEFAULT_DEBUG_PORT; +CPPORT PortInfo = {0, DEFAULT_DEBUG_BAUD_RATE, 0}; ULONG KdpPortIrq; #ifdef AUTO_ENABLE_BOCHS KDP_DEBUG_MODE KdpDebugMode = {{{.Bochs=TRUE}}}; @@ -68,7 +69,7 @@ KdpDebugMode.Serial = TRUE; /* Set the port to use */ - SerialPortInfo.ComPort = Value; + SerialPortNumber = Value; KdpPort = Value; } } @@ -78,8 +79,8 @@ if (Value) { KdpDebugMode.Serial = TRUE; - SerialPortInfo.BaseAddress = Value; - SerialPortInfo.ComPort = 0; + SerialPortInfo.Address = UlongToPtr(Value); + SerialPortNumber = 0; KdpPort = 0; } } Modified: branches/kd++/ntoskrnl/kd/kdio.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/ntoskrnl/kd/kdio.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/ntoskrnl/kd/kdio.c [iso-8859-1] (original) +++ branches/kd++/ntoskrnl/kd/kdio.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -26,7 +26,8 @@ ANSI_STRING KdpLogFileName = RTL_CONSTANT_STRING("\\SystemRoot\\debug.log"); KSPIN_LOCK KdpSerialSpinLock; -KD_PORT_INFORMATION SerialPortInfo = { DEFAULT_DEBUG_PORT, DEFAULT_DEBUG_BAUD_RATE, 0 }; +ULONG SerialPortNumber = DEFAULT_DEBUG_PORT; +CPPORT SerialPortInfo = {0, DEFAULT_DEBUG_BAUD_RATE, 0}; /* Current Port in use. FIXME: Do we support more then one? */ ULONG KdpPort; @@ -358,12 +359,12 @@ DispatchTable->KdpPrintRoutine = KdpSerialDebugPrint; /* Initialize the Port */ - if (!KdPortInitializeEx(&SerialPortInfo, 0, 0)) + if (!KdPortInitializeEx(&SerialPortInfo, SerialPortNumber)) { KdpDebugMode.Serial = FALSE; return; } - KdComPortInUse = (PUCHAR)(ULONG_PTR)SerialPortInfo.BaseAddress; + KdComPortInUse = SerialPortInfo.Address; /* Initialize spinlock */ KeInitializeSpinLock(&KdpSerialSpinLock); Modified: branches/kd++/ntoskrnl/kd/wrappers/gdbstub.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/ntoskrnl/kd/wrappers/gdbstub.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/ntoskrnl/kd/wrappers/gdbstub.c [iso-8859-1] (original) +++ branches/kd++/ntoskrnl/kd/wrappers/gdbstub.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -44,7 +44,8 @@ extern LIST_ENTRY PsActiveProcessHead; /* FIXME hardcoded for COM2, 115200 baud */ -KD_PORT_INFORMATION GdbPortInfo = { 2, 115200, 0 }; +ULONG GdbPortNumber = DEFAULT_DEBUG_PORT; +CPPORT GdbPortInfo = {0, DEFAULT_DEBUG_BAUD_RATE, 0}; static CHAR GspInBuffer[1000]; static CHAR GspOutBuffer[1000]; @@ -126,9 +127,7 @@ { UCHAR Value; - while (!KdPortGetByteEx(&GdbPortInfo, &Value)) - ; - + while (!KdPortGetByteEx(&GdbPortInfo, &Value)) ; return Value; } @@ -145,8 +144,7 @@ while (TRUE) { /* wait around for the start character, ignore all other characters */ - while ((ch = GdbGetChar()) != '$') - ; + while ((ch = GdbGetChar()) != '$') ; retry: Checksum = 0; @@ -784,7 +782,6 @@ } else if (strncmp(Request, "Rcmd,", 5) == 0) { - ; } } @@ -943,12 +940,14 @@ ULONG Index; for (Index = 0; Index < GspSwBreakpointCount; Index++) + { if (GspSwBreakpoints[Index].Address == Address) { if (PIndex) *PIndex = Index; return TRUE; } + } return FALSE; } @@ -1569,7 +1568,7 @@ WrapperTable->KdpExceptionRoutine = KdpGdbEnterDebuggerException; /* Initialize the Port */ - KdPortInitializeEx(&GdbPortInfo, 0, 0); + KdPortInitializeEx(&GdbPortInfo, GdbPortNumber); } else if (BootPhase == 1) { Modified: branches/kd++/ntoskrnl/kd/wrappers/gdbstub_powerpc.c URL: http://svn.reactos.org/svn/reactos/branches/kd%2B%2B/ntoskrnl/kd/wrappers/gdbstub_powerpc.c?rev=58964&r1=58963&r2=58964&view=diff ============================================================================== --- branches/kd++/ntoskrnl/kd/wrappers/gdbstub_powerpc.c [iso-8859-1] (original) +++ branches/kd++/ntoskrnl/kd/wrappers/gdbstub_powerpc.c [iso-8859-1] Tue May 7 00:14:36 2013 @@ -88,44 +88,47 @@ #include <debug.h> /************************************************************************/ + +static BOOLEAN GspInitialized; +static BOOLEAN GspRemoteDebug; + +static CONST CHAR HexChars[]="0123456789abcdef"; + +static PETHREAD GspRunThread; /* NULL means run all threads */ +static PETHREAD GspDbgThread; +static PETHREAD GspEnumThread; + +static FAST_MUTEX GspLock; + +extern LIST_ENTRY PsActiveProcessHead; + +/* FIXME hardcoded for COM2, 115200 baud */ +ULONG GdbPortNumber = DEFAULT_DEBUG_PORT; +CPPORT GdbPortInfo = {0, DEFAULT_DEBUG_BAUD_RATE, 0}; + /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/ /* at least NUMREGBYTES*2 are needed for register packets */ #define BUFMAX 1000 - -static BOOLEAN GspInitialized; - -static BOOLEAN GspRemoteDebug; - -static CONST CHAR HexChars[]="0123456789abcdef"; - -static PETHREAD GspRunThread; /* NULL means run all threads */ -static PETHREAD GspDbgThread; -static PETHREAD GspEnumThread; - -static FAST_MUTEX GspLock; - -extern LIST_ENTRY PsActiveProcessHead; - -/* FIXME hardcoded for COM2, 115200 baud */ -KD_PORT_INFORMATION GdbPortInfo = { 2, 115200, 0 }; +static CHAR GspInBuffer[BUFMAX]; +static CHAR GspOutBuffer[BUFMAX]; /* Number of Registers. */ #define NUMREGS 16 enum REGISTER_NAMES { - EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI, - PC /* also known as eip */, - PS /* also known as eflags */, - CS, SS, DS, ES, FS, GS + EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI, + PC /* also known as eip */, + PS /* also known as eflags */, + CS, SS, DS, ES, FS, GS }; typedef struct _CPU_REGISTER { - ULONG Size; - ULONG OffsetInTF; - ULONG OffsetInContext; - BOOLEAN SetInContext; + ULONG Size; + ULONG OffsetInTF; + ULONG OffsetInContext; + BOOLEAN SetInContext; } CPU_REGISTER, *PCPU_REGISTER; static CPU_REGISTER GspRegisters[NUMREGS] = @@ -134,54 +137,45 @@ static PCHAR GspThreadStates[DeferredReady+1] = { - "Initialized", - "Ready", - "Running", - "Standby", - "Terminated", - "Waiting", - "Transition", - "DeferredReady" + "Initialized", + "Ready", + "Running", + "Standby", + "Terminated", + "Waiting", + "Transition", + "DeferredReady" }; LONG HexValue(CHAR ch) { - if ((ch >= '0') && (ch <= '9')) - { - return (ch - '0'); - } - if ((ch >= 'a') && (ch <= 'f')) - { - return (ch - 'a' + 10); - } - if ((ch >= 'A') && (ch <= 'F')) - { - return (ch - 'A' + 10); - } - - return -1; -} - -static CHAR GspInBuffer[BUFMAX]; -static CHAR GspOutBuffer[BUFMAX]; + if ((ch >= '0') && (ch <= '9')) + return (ch - '0'); + + if ((ch >= 'a') && (ch <= 'f')) + return (ch - 'a' + 10); + + if ((ch >= 'A') && (ch <= 'F')) + return (ch - 'A' + 10); + + return -1; +} VOID GdbPutChar(UCHAR Value) { - KdPortPutByteEx(&GdbPortInfo, Value); + KdPortPutByteEx(&GdbPortInfo, Value); } UCHAR GdbGetChar(VOID) { - UCHAR Value; - - while (!KdPortGetByteEx(&GdbPortInfo, &Value)) - ; - - return Value; + UCHAR Value; + + while (!KdPortGetByteEx(&GdbPortInfo, &Value)) ; + return Value; } /* scan for the sequence $<data>#<Checksum> */ @@ -189,57 +183,53 @@ PCHAR GspGetPacket() { - PCHAR Buffer = &GspInBuffer[0]; - CHAR Checksum; - CHAR XmitChecksum; - ULONG Count; - CHAR ch; - - while (TRUE) - { - /* wait around for the start character, ignore all other characters */ - while ((ch = GdbGetChar ()) != '$') - ; - - retry: - Checksum = 0; - XmitChecksum = -1; - Count = 0; - - /* now, read until a # or end of Buffer is found */ - while (Count < BUFMAX) - { - ch = GdbGetChar(); - if (ch == '$') - { - goto retry; - } - if (ch == '#') - { - break; - } - Checksum = Checksum + ch; - Buffer[Count] = ch; - Count = Count + 1; - } - Buffer[Count] = 0; - - if (ch == '#') - { - ch = GdbGetChar(); - XmitChecksum = (CHAR)(HexValue(ch) << 4); - ch = GdbGetChar(); - XmitChecksum += (CHAR)(HexValue(ch)); - - if (Checksum != XmitChecksum) - { - GdbPutChar('-'); /* failed checksum */ - } - else - { - GdbPutChar('+'); /* successful transfer */ - - return &Buffer[0]; + PCHAR Buffer = &GspInBuffer[0]; + CHAR Checksum; + CHAR XmitChecksum; + ULONG Count; + CHAR ch; + + while (TRUE) + { + /* wait around for the start character, ignore all other characters */ + while ((ch = GdbGetChar ()) != '$') ; + +retry: + Checksum = 0; + XmitChecksum = -1; + Count = 0; + + /* now, read until a # or end of Buffer is found */ + while (Count < BUFMAX) + { + ch = GdbGetChar(); + if (ch == '$') + goto retry; + + if (ch == '#') + break; + + Checksum = Checksum + ch; + Buffer[Count] = ch; + Count = Count + 1; + } + Buffer[Count] = 0; + + if (ch == '#') + { + ch = GdbGetChar(); + XmitChecksum = (CHAR)(HexValue(ch) << 4); + ch = GdbGetChar(); + XmitChecksum += (CHAR)(HexValue(ch)); + + if (Checksum != XmitChecksum) + { + GdbPutChar('-'); /* failed checksum */ + } + else + { + GdbPutChar('+'); /* successful transfer */ + return &Buffer[0]; } } } @@ -250,54 +240,54 @@ VOID GspPutPacket(PCHAR Buffer) { - CHAR Checksum; - LONG Count; - CHAR ch; - - /* $<packet info>#<Checksum>. */ - do - { - GdbPutChar('$'); - Checksum = 0; - Count = 0; - - while ((ch = Buffer[Count])) - { - GdbPutChar(ch); - Checksum += ch; - Count += 1; - } - - GdbPutChar('#'); - GdbPutChar(HexChars[(Checksum >> 4) & 0xf]); - GdbPutChar(HexChars[Checksum & 0xf]); - } - while (GdbGetChar() != '+'); + CHAR Checksum; + LONG Count; + CHAR ch; + + /* $<packet info>#<Checksum>. */ + do + { + GdbPutChar('$'); + Checksum = 0; + Count = 0; + + while ((ch = Buffer[Count])) + { + GdbPutChar(ch); + Checksum += ch; + Count += 1; + } + + GdbPutChar('#'); + GdbPutChar(HexChars[(Checksum >> 4) & 0xf]); + GdbPutChar(HexChars[Checksum & 0xf]); + } + while (GdbGetChar() != '+'); } VOID GspPutPacketNoWait(PCHAR Buffer) { - CHAR Checksum; - LONG Count; - CHAR ch; - - /* $<packet info>#<Checksum>. */ - GdbPutChar('$'); - Checksum = 0; - Count = 0; - - while ((ch = Buffer[Count])) - { - GdbPutChar(ch); - Checksum += ch; - Count += 1; - } - - GdbPutChar('#'); - GdbPutChar(HexChars[(Checksum >> 4) & 0xf]); - GdbPutChar(HexChars[Checksum & 0xf]); + CHAR Checksum; + LONG Count; + CHAR ch; + + /* $<packet info>#<Checksum>. */ + GdbPutChar('$'); + Checksum = 0; + Count = 0; + + while ((ch = Buffer[Count])) + { + GdbPutChar(ch); + Checksum += ch; + Count += 1; + } + + GdbPutChar('#'); + GdbPutChar(HexChars[(Checksum >> 4) & 0xf]); + GdbPutChar(HexChars[Checksum & 0xf]); } /* Indicate to caller of GspMem2Hex or GspHex2Mem that there has been an @@ -308,19 +298,33 @@ static CHAR GspReadMemSafe(PCHAR Address) { - CHAR ch; - - if (NULL == Address) - { - GspMemoryError = TRUE; - return '\0'; - } - - GspAccessLocation = Address; - ch = *Address; - GspAccessLocation = NULL; - - return ch; + CHAR ch; + + if (NULL == Address) + { + GspMemoryError = TRUE; + return '\0'; + } + + GspAccessLocation = Address; + ch = *Address; + GspAccessLocation = NULL; + + return ch; +} + +static CHAR +GspWriteMemSafeGetContent(PVOID Context, ULONG Offset) +{ + ASSERT(0 == Offset); + return *((PCHAR) Context); +} + +static void +GspWriteMemSafe(PCHAR Address, + CHAR Ch) +{ + GspWriteMem(Address, 1, TRUE, GspWriteMemSafeGetContent, &Ch); } /* Convert the memory pointed to by Address into hex, placing result in Buffer */ @@ -329,131 +333,110 @@ a fault; if FALSE treat a fault like any other fault in the stub. */ static PCHAR GspMem2Hex(PCHAR Address, - PCHAR Buffer, - LONG Count, - BOOLEAN MayFault) -{ - ULONG i; - CHAR ch; - - for (i = 0; i < (ULONG) Count; i++) - { - if (MayFault) - { - ch = GspReadMemSafe(Address); - if (GspMemoryError) - { - return Buffer; - } - } - else - { - ch = *Address; - } - *Buffer++ = HexChars[(ch >> 4) & 0xf]; - *Buffer++ = HexChars[ch & 0xf]; - Address++; - } - - *Buffer = 0; - return Buffer; + PCHAR Buffer, + LONG Count, + BOOLEAN MayFault) +{ + ULONG i; + CHAR ch; + + for (i = 0; i < (ULONG) Count; i++) + { + if (MayFault) + { + ch = GspReadMemSafe(Address); + if (GspMemoryError) + return Buffer; + } + else + { + ch = *Address; + } + *Buffer++ = HexChars[(ch >> 4) & 0xf]; + *Buffer++ = HexChars[ch & 0xf]; + Address++; + } + + *Buffer = 0; + return Buffer; } static ULONG GspWriteMem(PCHAR Address, - ULONG Count, - BOOLEAN MayFault, - CHAR (*GetContent)(PVOID Context, ULONG Offset), - PVOID Context) -{ - PCHAR Current; - PCHAR Page; - ULONG CountInPage; - ULONG i; - CHAR ch; - ULONG OldProt = 0; - - Current = Address; - while (Current < Address + Count) - { - Page = (PCHAR)PAGE_ROUND_DOWN(Current); - if (Address + Count <= Page + PAGE_SIZE) - { - /* Fits in this page */ - CountInPage = Count; - } - else - { - /* Flows into next page, handle only current page in this iteration */ - CountInPage = PAGE_SIZE - (Address - Page); - } - if (MayFault) - { - OldProt = MmGetPageProtect(NULL, Address); - MmSetPageProtect(NULL, Address, PAGE_EXECUTE_READWRITE); - } - - for (i = 0; i < CountInPage && ! GspMemoryError; i++) - { - ch = (*GetContent)(Context, Current - Address); - - if (MayFault) - { - GspAccessLocation = Current; - } - *Current = ch; - if (MayFault) - { - GspAccessLocation = NULL; - } - Current++; - } - if (MayFault) - { - MmSetPageProtect(NULL, Page, OldProt); - if (GspMemoryError) - { - return Current - Address; - } - } - } - - return Current - Address; + ULONG Count, + BOOLEAN MayFault, + CHAR (*GetContent)(PVOID Context, ULONG Offset), + PVOID Context) +{ + PCHAR Current; + PCHAR Page; + ULONG CountInPage; + ULONG i; + CHAR ch; + ULONG OldProt = 0; + + Current = Address; + while (Current < Address + Count) + { + Page = (PCHAR)PAGE_ROUND_DOWN(Current); + if (Address + Count <= Page + PAGE_SIZE) + { + /* Fits in this page */ + CountInPage = Count; + } + else + { + /* Flows into next page, handle only current page in this iteration */ + CountInPage = PAGE_SIZE - (Address - Page); + } + if (MayFault) + { + OldProt = MmGetPageProtect(NULL, Address); + MmSetPageProtect(NULL, Address, PAGE_EXECUTE_READWRITE); + } + + for (i = 0; i < CountInPage && ! GspMemoryError; i++) + { + ch = (*GetContent)(Context, Current - Address); + + if (MayFault) + GspAccessLocation = Current; + + *Current = ch; + + if (MayFault) + GspAccessLocation = NULL; + + Current++; + } + if (MayFault) + { + MmSetPageProtect(NULL, Page, OldProt); + if (GspMemoryError) + return Current - Address; + } + } + + return Current - Address; } static CHAR GspHex2MemGetContent(PVOID Context, ULONG Offset) { - return (CHAR)((HexValue(*((PCHAR) Context + 2 * Offset)) << 4) + - HexValue(*((PCHAR) Context + 2 * Offset + 1))); + return (CHAR)((HexValue(*((PCHAR) Context + 2 * Offset)) << 4) + + HexValue(*((PCHAR) Context + 2 * Offset + 1))); } /* Convert the hex array pointed to by Buffer into binary to be placed at Address */ /* Return a pointer to the character AFTER the last byte read from Buffer */ static PCHAR GspHex2Mem(PCHAR Buffer, - PCHAR Address, - ULONG Count, - BOOLEAN MayFault) -{ - Count = GspWriteMem(Address, Count, MayFault, GspHex2MemGetContent, Buffer); - - return Buffer + 2 * Count; -} - -static CHAR -GspWriteMemSafeGetContent(PVOID Context, ULONG Offset) -{ - ASSERT(0 == Offset); - - return *((PCHAR) Context); -} - -static void -GspWriteMemSafe(PCHAR Address, - CHAR Ch) -{ - GspWriteMem(Address, 1, TRUE, GspWriteMemSafeGetContent, &Ch); + PCHAR Address, + ULONG Count, + BOOLEAN MayFault) +{ + Count = GspWriteMem(Address, Count, MayFault, GspHex2MemGetContent, Buffer); + return Buffer + 2 * Count; } @@ -462,33 +445,38 @@ ULONG GspComputeSignal(NTSTATUS ExceptionCode) { - ULONG SigVal; - - switch (ExceptionCode) - { - case STATUS_INTEGER_DIVIDE_BY_ZERO: - SigVal = 8; /* divide by zero */ - break; - case STATUS_SINGLE_STEP: - case STATUS_BREAKPOINT: - SigVal = 5; /* breakpoint */ - break; - case STATUS_INTEGER_OVERFLOW: - case STATUS_ARRAY_BOUNDS_EXCEEDED: - SigVal = 16; /* bound instruction */ - break; - case STATUS_ILLEGAL_INSTRUCTION: - SigVal = 4; /* Invalid opcode */ - break; - case STATUS_STACK_OVERFLOW: - case STATUS_DATATYPE_MISALIGNMENT: - case STATUS_ACCESS_VIOLATION: - SigVal = 11; /* access violation */ - break; - default: - SigVal = 7; /* "software generated" */ - } - return SigVal; + ULONG SigVal; + + switch (ExceptionCode) + { + case STATUS_INTEGER_DIVIDE_BY_ZERO: + SigVal = 8; /* divide by zero */ + break; + + case STATUS_SINGLE_STEP: + case STATUS_BREAKPOINT: + SigVal = 5; /* breakpoint */ + break; + + case STATUS_INTEGER_OVERFLOW: + case STATUS_ARRAY_BOUNDS_EXCEEDED: + SigVal = 16; /* bound instruction */ + break; + + case STATUS_ILLEGAL_INSTRUCTION: + SigVal = 4; /* Invalid opcode */ + break; + + case STATUS_STACK_OVERFLOW: + case STATUS_DATATYPE_MISALIGNMENT: + case STATUS_ACCESS_VIOLATION: + SigVal = 11; /* access violation */ + break; + + default: + SigVal = 7; /* "software generated" */ + } + return SigVal; } @@ -1626,43 +1614,39 @@ BOOLEAN NTAPI GspBreakIn(PKINTERRUPT Interrupt, - PVOID ServiceContext) -{ - PKTRAP_FRAME TrapFrame; - BOOLEAN DoBreakIn; - CONTEXT Context; - KIRQL OldIrql; - UCHAR Value; - - DPRINT("Break In\n"); - - DoBreakIn = FALSE; - while (KdPortGetByteEx(&GdbPortInfo, &Value)) - { - if (Value == 0x03) - { - DoBreakIn = TRUE; - } - } - - if (!DoBreakIn) - { - return TRUE; - } - - KeRaiseIrql(HIGH_LEVEL, &OldIrql); - - TrapFrame = PsGetCurrentThread()->Tcb.TrapFrame; - - KeTrapFrameToContext(TrapFrame, NULL, &Context); - - KdpGdbEnterDebuggerException(NULL, &Context, TrapFrame); - - KeContextToTrapFrame(&Context, NULL, TrapFrame, Context.ContextFlags, KernelMode); - - KeLowerIrql(OldIrql); - - return TRUE; + PVOID ServiceContext) +{ + PKTRAP_FRAME TrapFrame; + BOOLEAN DoBreakIn; + CONTEXT Context; + KIRQL OldIrql; + UCHAR Value; + + DPRINT("Break In\n"); + + DoBreakIn = FALSE; + while (KdPortGetByteEx(&GdbPortInfo, &Value)) + { + if (Value == 0x03) + DoBreakIn = TRUE; + } + + if (!DoBreakIn) + return TRUE; + + KeRaiseIrql(HIGH_LEVEL, &OldIrql); + + TrapFrame = PsGetCurrentThread()->Tcb.TrapFrame; + + KeTrapFrameToContext(TrapFrame, NULL, &Context); + + KdpGdbEnterDebuggerException(NULL, &Context, TrapFrame); + + KeContextToTrapFrame(&Context, NULL, TrapFrame, Context.ContextFlags, KernelMode); + + KeLowerIrql(OldIrql); + + return TRUE; } VOID @@ -1677,39 +1661,36 @@ KdpGdbStubInit(PKD_DISPATCH_TABLE WrapperTable, ULONG BootPhase) { - if (!KdDebuggerEnabled || !KdpDebugMode.Gdb) - { - return; - } - - if (BootPhase == 0) - { - ExInitializeFastMutex(&GspLock); - - /* Write out the functions that we support for now */ - WrapperTable->KdpInitRoutine = KdpGdbStubInit; - WrapperTable->KdpPrintRoutine = KdpGdbDebugPrint; - WrapperTable->KdpExceptionRoutine = KdpGdbEnterDebuggerException; - - /* Initialize the Port */ - KdPortInitializeEx(&GdbPortInfo, 0, 0); - - KdpPort = GdbPortInfo.ComPort; - } - else if (BootPhase == 1) - { - GspInitialized = TRUE; - - GspRunThread = NULL; - GspDbgThread = NULL; - GspEnumThread = NULL; - - HalDisplayString("Waiting for GDB to attach\n"); - DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C); - } - else if (BootPhase == 2) - { - HalDisplayString("\n GDB debugging enabled\n\n"); + if (!KdDebuggerEnabled || !KdpDebugMode.Gdb) + return; + + if (BootPhase == 0) + { + ExInitializeFastMutex(&GspLock); + + /* Write out the functions that we support for now */ + WrapperTable->KdpInitRoutine = KdpGdbStubInit; + WrapperTable->KdpPrintRoutine = KdpGdbDebugPrint; + WrapperTable->KdpExceptionRoutine = KdpGdbEnterDebuggerException; + + /* Initialize the Port */ + KdPortInitializeEx(&GdbPortInfo, GdbPortNumber); + // KdpPort = GdbPortInfo.ComPort; + } + else if (BootPhase == 1) + { + GspInitialized = TRUE; + + GspRunThread = NULL; + GspDbgThread = NULL; + GspEnumThread = NULL; + + HalDisplayString("Waiting for GDB to attach\n"); + DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C); + } + else if (BootPhase == 2) + { + HalDisplayString("\n GDB debugging enabled\n\n"); } }