From e6f624f0e7025409b7557c689a7241ad467e6d65 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Thu, 21 Oct 1999 13:00:09 +0000 Subject: [PATCH] Improved debug output and added hal READ_XXX and WRITE_XXX functions. svn path=/trunk/; revision=717 --- reactos/ntoskrnl/hal/x86/kdbg.c | 297 +++++++++++++++++++++++++++++- reactos/ntoskrnl/hal/x86/portio.c | 116 ++++++++++++ reactos/ntoskrnl/hal/x86/sources | 2 +- 3 files changed, 404 insertions(+), 11 deletions(-) create mode 100644 reactos/ntoskrnl/hal/x86/portio.c diff --git a/reactos/ntoskrnl/hal/x86/kdbg.c b/reactos/ntoskrnl/hal/x86/kdbg.c index 0f71406efd5..4027401c16e 100644 --- a/reactos/ntoskrnl/hal/x86/kdbg.c +++ b/reactos/ntoskrnl/hal/x86/kdbg.c @@ -1,26 +1,291 @@ -/* $Id: kdbg.c,v 1.1 1999/09/05 20:54:57 ea Exp $ - * - * reactos/ntoskrnl/hal/x86/kdbg.c +/* $Id: kdbg.c,v 1.2 1999/10/21 13:00:09 ekohl Exp $ * + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * FILE: ntoskrnl/hal/x86/kdbg.c + * PURPOSE: Serial i/o functions for the kernel debugger. + * PROGRAMMER: Emanuele Aliberti + * UPDATE HISTORY: + * Created 05/09/99 */ /* INCLUDES *****************************************************************/ -#include +#include + #include +#define DEFAULT_BAUD_RATE 19200 +//#define DEFAULT_COM_PORT 2 +//#define DEFAULT_BASE_ADDRESS 0x2F8 + +#define DEFAULT_COM_PORT 1 +#define DEFAULT_BASE_ADDRESS 0x3F8 + + +/* MACROS *******************************************************************/ + +#define SER_RBR(x) ((x)+0) +#define SER_THR(x) ((x)+0) +#define SER_DLL(x) ((x)+0) +#define SER_IER(x) ((x)+1) +#define SER_DLM(x) ((x)+1) +#define SER_IIR(x) ((x)+2) +#define SER_LCR(x) ((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) ((x)+4) +#define SR_MCR_DTR 0x01 +#define SR_MCR_RTS 0x02 +#define SER_LSR(x) ((x)+5) +#define SR_LSR_TBE 0x20 +#define SER_MSR(x) ((x)+6) +#define SR_MSR_CTS 0x10 +#define SR_MSR_DSR 0x20 +#define SER_SCR(x) ((x)+7) + + + + +/* GLOBAL VARIABLES *********************************************************/ + +ULONG KdComPortInUse = 0; + + +/* STATIC VARIABLES *********************************************************/ + +static ULONG ComPort = 0; +static ULONG BaudRate = 0; +static PUCHAR PortBase = (PUCHAR)0; + +/* The com port must only be initialized once! */ +static BOOLEAN PortInitialized = FALSE; + + +/* STATIC FUNCTIONS *********************************************************/ + +static BOOLEAN +KdDoesComPortExist (PUCHAR BaseAddress) +{ + BOOLEAN found; + BYTE mcr; + BYTE msr; + + found = FALSE; + + /* save Modem Control Register (MCR) */ + mcr = READ_PORT_UCHAR (SER_MCR(BaseAddress)); + + /* enable loop mode (set Bit 4 of the MCR) */ + WRITE_PORT_UCHAR (SER_MCR(BaseAddress), 0x10); + + /* clear all modem output bits */ + WRITE_PORT_UCHAR (SER_MCR(BaseAddress), 0x10); + + /* read the Modem Status Register */ + msr = READ_PORT_UCHAR (SER_MSR(BaseAddress)); + + /* + * the upper nibble of the MSR (modem output bits) must be + * equal to the lower nibble of the MCR (modem input bits) + */ + if ((msr & 0xF0) == 0x00) + { + /* set all modem output bits */ + WRITE_PORT_UCHAR (SER_MCR(BaseAddress), 0x1F); + + /* read the Modem Status Register */ + msr = READ_PORT_UCHAR (SER_MSR(BaseAddress)); + + /* + * the upper nibble of the MSR (modem output bits) must be + * equal to the lower nibble of the MCR (modem input bits) + */ + if ((msr & 0xF0) == 0xF0) + found = TRUE; + } + + /* restore MCR */ + WRITE_PORT_UCHAR (SER_MCR(BaseAddress), mcr); + + return (found); +} + + +/* FUNCTIONS ****************************************************************/ + /* HAL.KdPortInitialize */ BOOLEAN STDCALL KdPortInitialize ( - DWORD Unknown0, - DWORD Unknown1, - DWORD Unknown2 - ) + PKD_PORT_INFORMATION PortInformation, + DWORD Unknown1, + DWORD Unknown2 + ) { - return FALSE; + ULONG BaseArray[5] = {0, 0x3F8, 0x2F8, 3E8, 2E8}; + char buffer[80]; + ULONG divisor; + BYTE lcr; + + + if (PortInitialized == FALSE) + { + if (PortInformation->BaudRate != 0) + { + BaudRate = PortInformation->BaudRate; + } + else + { + BaudRate = DEFAULT_BAUD_RATE; + } + + if (PortInformation->ComPort == 0) + { + if (KdDoesComPortExist ((PUCHAR)BaseArray[2])) + { + PortBase = (PUCHAR)BaseArray[2]; + ComPort = 2; +#ifndef NDEBUG + sprintf (buffer, + "\nSerial port COM%ld found at 0x%lx\n", + ComPort, + (ULONG)PortBase); + HalDisplayString (buffer); +#endif + } + else if (KdDoesComPortExist ((PUCHAR)BaseArray[1])) + { + PortBase = (PUCHAR)BaseArray[1]; + ComPort = 1; +#ifndef NDEBUG + sprintf (buffer, + "\nSerial port COM%ld found at 0x%lx\n", + ComPort, + (ULONG)PortBase); + HalDisplayString (buffer); +#endif + } + else + { + sprintf (buffer, + "\nKernel Debugger: No COM port found!!!\n\n"); + + HalDisplayString (buffer); + return FALSE; + } + } + else + { + if (KdDoesComPortExist ((PUCHAR)BaseArray[PortInformation->ComPort])) + { + PortBase = (PUCHAR)BaseArray[PortInformation->ComPort]; + ComPort = PortInformation->ComPort; +#ifndef NDEBUG + sprintf (buffer, + "\nSerial port COM%ld found at 0x%lx\n", + ComPort, + (ULONG)PortBase); + HalDisplayString (buffer); +#endif + } + else + { + sprintf (buffer, + "\nKernel Debugger: No serial port found!!!\n\n"); + + HalDisplayString (buffer); + return FALSE; + } + } + +/* + if (KdDoesComPortExist ((PUCHAR)BaseArray[2])) + { + PortBase = (PUCHAR)BaseArray[2]; + ComPort = 2; + sprintf (buffer, + "\nCOM port found at 0x%lx\n", + (ULONG)PortBase); + + HalDisplayString (buffer); + } + else if (KdDoesComPortExist ((PUCHAR)BaseArray[1])) + { + PortBase = (PUCHAR)BaseArray[1]; + ComPort = 1; + sprintf (buffer, + "\nCOM port found at 0x%lx\n", + (ULONG)PortBase); + + HalDisplayString (buffer); + } + else + { + sprintf (buffer, + "\nKernel Debugger: No COM port found!!!\n"); + + HalDisplayString (buffer); + return FALSE; + } +*/ + PortInitialized = TRUE; + } + + + /* + * set baud rate and data format (8N1) + */ + + /* turn on DTR and RTS */ + WRITE_PORT_UCHAR (SER_MCR(PortBase), SR_MCR_DTR | SR_MCR_RTS); + + /* set DLAB */ + lcr = READ_PORT_UCHAR (SER_LCR(PortBase)) | SR_LCR_DLAB; + WRITE_PORT_UCHAR (SER_LCR(PortBase), lcr); + + /* set baud rate */ + divisor = 115200 / BaudRate; + WRITE_PORT_UCHAR (SER_DLL(PortBase), divisor & 0xff); + WRITE_PORT_UCHAR (SER_DLM(PortBase), (divisor >> 8) & 0xff); + + /* reset DLAB and set 8N1 format */ + WRITE_PORT_UCHAR (SER_LCR(PortBase), + SR_LCR_CS8 | SR_LCR_ST1 | SR_LCR_PNO); + + /* read junk out of the RBR */ + lcr = READ_PORT_UCHAR (SER_RBR(PortBase)); + + /* + * set global info + */ + KdComPortInUse = (ULONG)PortBase; + + /* + * print message to blue screen + */ + sprintf (buffer, + "\nKernel Debugger using: COM%ld (Port 0x%lx) BaudRate %ld\n\n", + ComPort, + (ULONG)PortBase, + BaudRate); + + HalDisplayString (buffer); + + return TRUE; } @@ -31,6 +296,10 @@ KdPortGetByte ( VOID ) { + if (PortInitialized == FALSE) + return 0; + + return (BYTE) 0; } @@ -45,13 +314,21 @@ KdPortPollByte ( return (BYTE) 0; } + /* HAL.KdPortPutByte */ VOID STDCALL KdPortPutByte ( - BYTE ByteToSend + UCHAR ByteToSend ) { + if (PortInitialized == FALSE) + return; + + while ((READ_PORT_UCHAR (SER_LSR(PortBase)) & SR_LSR_TBE) == 0) + ; + + WRITE_PORT_UCHAR (SER_THR(PortBase), ByteToSend); } diff --git a/reactos/ntoskrnl/hal/x86/portio.c b/reactos/ntoskrnl/hal/x86/portio.c new file mode 100644 index 00000000000..4f20bcae669 --- /dev/null +++ b/reactos/ntoskrnl/hal/x86/portio.c @@ -0,0 +1,116 @@ +/* $Id: portio.c,v 1.1 1999/10/21 13:00:09 ekohl Exp $ + * + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * FILE: ntoskrnl/hal/x86/portio.c + * PURPOSE: Port I/O functions + * PROGRAMMER: Eric Kohl (ekohl@abo.rhein-zeitung.de) + * UPDATE HISTORY: + * Created 18/10/99 + */ + +#include +#include + + +/* FUNCTIONS ****************************************************************/ + +VOID +READ_PORT_BUFFER_UCHAR (PUCHAR Port, + PUCHAR Buffer, + ULONG Count) +{ + insb ((ULONG)Port, Buffer, Count); +} + + +VOID +READ_PORT_BUFFER_ULONG (PULONG Port, + PULONG Buffer, + ULONG Count) +{ + insl ((ULONG)Port, Buffer, Count); +} + + +VOID +READ_PORT_BUFFER_USHORT (PUSHORT Port, + PUSHORT Buffer, + ULONG Count) +{ + insw ((ULONG)Port, Buffer, Count); +} + + +UCHAR +READ_PORT_UCHAR (PUCHAR Port) +{ + return inb_p ((ULONG)Port); +} + + +ULONG +READ_PORT_ULONG (PULONG Port) +{ + return inl_p ((ULONG)Port); +} + + +USHORT +READ_PORT_USHORT (PUSHORT Port) +{ + return inw_p ((ULONG)Port); +} + + +VOID +WRITE_PORT_BUFFER_UCHAR (PUCHAR Port, + PUCHAR Buffer, + ULONG Count) +{ + outsb ((ULONG)Port, Buffer, Count); +} + + +VOID +WRITE_PORT_BUFFER_ULONG (PULONG Port, + PULONG Buffer, + ULONG Count) +{ + outsl ((ULONG)Port, Buffer, Count); +} + + +VOID +WRITE_PORT_BUFFER_USHORT (PUSHORT Port, + PUSHORT Buffer, + ULONG Count) +{ + outsw ((ULONG)Port, Buffer, Count); +} + + +VOID +WRITE_PORT_UCHAR (PUCHAR Port, + UCHAR Value) +{ + outb_p ((ULONG)Port, Value); +} + + +VOID +WRITE_PORT_ULONG (PULONG Port, + ULONG Value) +{ + outl_p ((ULONG)Port, Value); +} + + +VOID +WRITE_PORT_USHORT (PUSHORT Port, + USHORT Value) +{ + outw_p ((ULONG)Port, Value); +} + +/* EOF */ diff --git a/reactos/ntoskrnl/hal/x86/sources b/reactos/ntoskrnl/hal/x86/sources index eec4dd1ac58..dbac9e1ca3c 100644 --- a/reactos/ntoskrnl/hal/x86/sources +++ b/reactos/ntoskrnl/hal/x86/sources @@ -3,5 +3,5 @@ HAL_OBJECTS = hal/x86/head.o hal/x86/irq.o hal/x86/exp.o hal/x86/isa.o \ hal/x86/irql.o hal/x86/bios32.o hal/x86/thread.o hal/x86/spinlock.o \ hal/x86/mp.o hal/x86/dma.o hal/x86/bus.o hal/x86/mbr.o \ hal/x86/sysinfo.o hal/x86/time.o hal/x86/usercall.o hal/x86/beep.o \ - hal/x86/display.o hal/x86/reboot.o + hal/x86/display.o hal/x86/reboot.o hal/x86/kdbg.o hal/x86/portio.o