From 4869ba0006638d36ead5dbe96386597c69933823 Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Tue, 27 Oct 2009 23:38:56 +0000 Subject: [PATCH] Sync kdcom from amd64 branch svn path=/trunk/; revision=43801 --- reactos/drivers/base/kddll/kdcom.c | 7 ++ reactos/drivers/base/kddll/kddll.c | 127 +++++++++++++----------- reactos/drivers/base/kddll/kddll.rbuild | 1 + reactos/drivers/base/kddll/kdserial.c | 4 +- 4 files changed, 77 insertions(+), 62 deletions(-) diff --git a/reactos/drivers/base/kddll/kdcom.c b/reactos/drivers/base/kddll/kdcom.c index 409b88e4d91..22d806c0720 100644 --- a/reactos/drivers/base/kddll/kdcom.c +++ b/reactos/drivers/base/kddll/kdcom.c @@ -216,6 +216,11 @@ KdpSendByte(IN BYTE Byte) /* Wait for the port to be ready */ while ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_TBE) == 0); + /* This is needed due to subtle timing issues */ + READ_PORT_UCHAR(ComPortBase + COM_MSR); + while ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_TBE) == 0); + READ_PORT_UCHAR(ComPortBase + COM_MSR); + /* Send the byte */ WRITE_PORT_UCHAR(ComPortBase + COM_DAT, Byte); } @@ -224,6 +229,8 @@ KDP_STATUS NTAPI KdpPollByte(OUT PBYTE OutByte) { + READ_PORT_UCHAR(ComPortBase + COM_MSR); // Timing + /* Check if data is available */ if ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_DR)) { diff --git a/reactos/drivers/base/kddll/kddll.c b/reactos/drivers/base/kddll/kddll.c index b86c9055e09..43aca1d534a 100644 --- a/reactos/drivers/base/kddll/kddll.c +++ b/reactos/drivers/base/kddll/kddll.c @@ -13,6 +13,7 @@ PFNDBGPRNT KdpDbgPrint = NULL; ULONG CurrentPacketId = INITIAL_PACKET_ID | SYNC_PACKET_ID; +ULONG RemotePacketId = 0; /* PRIVATE FUNCTIONS **********************************************************/ @@ -148,24 +149,31 @@ KdReceivePacket( KdStatus = KdpReceiveBuffer(&Packet.PacketType, sizeof(USHORT)); if (KdStatus != KDP_PACKET_RECEIVED) { - /* Didn't receive a PacketType or PacketType is bad. Start over. */ - continue; + /* Didn't receive a PacketType. */ + return KdStatus; + } + + /* Check if we got a resend packet */ + if (Packet.PacketLeader == CONTROL_PACKET_LEADER && + Packet.PacketType == PACKET_TYPE_KD_RESEND) + { + return KDP_PACKET_RESEND; } /* Step 3 - Read ByteCount */ KdStatus = KdpReceiveBuffer(&Packet.ByteCount, sizeof(USHORT)); - if (KdStatus != KDP_PACKET_RECEIVED || Packet.ByteCount > PACKET_MAX_SIZE) + if (KdStatus != KDP_PACKET_RECEIVED) { - /* Didn't receive ByteCount or it's too big. Start over. */ - continue; + /* Didn't receive ByteCount. */ + return KdStatus; } /* Step 4 - Read PacketId */ KdStatus = KdpReceiveBuffer(&Packet.PacketId, sizeof(ULONG)); if (KdStatus != KDP_PACKET_RECEIVED) { - /* Didn't receive PacketId. Start over. */ - continue; + /* Didn't receive PacketId. */ + return KdStatus; } /* @@ -180,8 +188,8 @@ KdReceivePacket( KdStatus = KdpReceiveBuffer(&Packet.Checksum, sizeof(ULONG)); if (KdStatus != KDP_PACKET_RECEIVED) { - /* Didn't receive Checksum. Start over. */ - continue; + /* Didn't receive Checksum. */ + return KdStatus; } /* Step 6 - Handle control packets */ @@ -205,6 +213,7 @@ KdReceivePacket( KDDBGPRINT("KdReceivePacket - got a reset packet\n"); KdpSendControlPacket(PACKET_TYPE_KD_RESET, 0); CurrentPacketId = INITIAL_PACKET_ID; + RemotePacketId = INITIAL_PACKET_ID; /* Fall through */ case PACKET_TYPE_KD_RESEND: @@ -227,43 +236,18 @@ KdReceivePacket( return KDP_PACKET_RECEIVED; } - /* Did we get the right packet type? */ - if (PacketType != Packet.PacketType) - { - /* We received something different, start over */ - KDDBGPRINT("KdReceivePacket - wrong PacketType\n"); - KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0); - continue; - } - /* Get size of the message header */ - switch (Packet.PacketType) - { - case PACKET_TYPE_KD_STATE_CHANGE64: - MessageHeader->Length = sizeof(DBGKD_WAIT_STATE_CHANGE64); - break; + MessageHeader->Length = MessageHeader->MaximumLength; - case PACKET_TYPE_KD_STATE_MANIPULATE: - MessageHeader->Length = sizeof(DBGKD_MANIPULATE_STATE64); - break; - - case PACKET_TYPE_KD_DEBUG_IO: - MessageHeader->Length = sizeof(DBGKD_DEBUG_IO); - break; - - default: - KDDBGPRINT("KdReceivePacket - unknown PacketType\n"); - return KDP_PACKET_RESEND; - } - - //KDDBGPRINT("KdReceivePacket - got normal PacketType\n"); - - /* Packet smaller than expected? */ - if (MessageHeader->Length > Packet.ByteCount) + /* Packet smaller than expected or too big? */ + if (Packet.ByteCount < MessageHeader->Length || + Packet.ByteCount > PACKET_MAX_SIZE) { KDDBGPRINT("KdReceivePacket - too few data (%d) for type %d\n", Packet.ByteCount, MessageHeader->Length); MessageHeader->Length = Packet.ByteCount; + KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0); + continue; } //KDDBGPRINT("KdReceivePacket - got normal PacketType, Buffer = %p\n", MessageHeader->Buffer); @@ -316,15 +300,6 @@ KdReceivePacket( } } - /* Compare checksum */ - if (Packet.Checksum != Checksum) - { - KDDBGPRINT("KdReceivePacket - wrong cheksum, got %x, calculated %x\n", - Packet.Checksum, Checksum); - KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0); - continue; - } - /* We must receive a PACKET_TRAILING_BYTE now */ KdStatus = KdpReceiveBuffer(&Byte, sizeof(UCHAR)); if (KdStatus != KDP_PACKET_RECEIVED || Byte != PACKET_TRAILING_BYTE) @@ -334,12 +309,36 @@ KdReceivePacket( continue; } + /* Compare checksum */ + if (Packet.Checksum != Checksum) + { + KDDBGPRINT("KdReceivePacket - wrong cheksum, got %x, calculated %x\n", + Packet.Checksum, Checksum); + KdpSendControlPacket(PACKET_TYPE_KD_RESEND, 0); + continue; + } + /* Acknowledge the received packet */ KdpSendControlPacket(PACKET_TYPE_KD_ACKNOWLEDGE, Packet.PacketId); - //KDDBGPRINT("KdReceivePacket - all ok\n"); + /* Check if the received PacketId is ok */ + if (Packet.PacketId != RemotePacketId) + { + /* Continue with next packet */ + continue; + } - return KDP_PACKET_RECEIVED; + /* Did we get the right packet type? */ + if (PacketType == Packet.PacketType) + { + /* Yes, return success */ + //KDDBGPRINT("KdReceivePacket - all ok\n"); + RemotePacketId ^= 1; + return KDP_PACKET_RECEIVED; + } + + /* We received something different, ignore it. */ + KDDBGPRINT("KdReceivePacket - wrong PacketType\n"); } return KDP_PACKET_RECEIVED; @@ -352,10 +351,11 @@ KdSendPacket( IN ULONG PacketType, IN PSTRING MessageHeader, IN PSTRING MessageData, - IN OUT PKD_CONTEXT Context) + IN OUT PKD_CONTEXT KdContext) { KD_PACKET Packet; KDP_STATUS KdStatus; + ULONG Retries; /* Initialize a KD_PACKET */ Packet.PacketLeader = PACKET_LEADER; @@ -372,7 +372,9 @@ KdSendPacket( MessageData->Length); } - for (;;) + Retries = KdContext->KdpDefaultRetries; + + do { /* Set the packet id */ Packet.PacketId = CurrentPacketId; @@ -394,10 +396,10 @@ KdSendPacket( /* Wait for acknowledge */ KdStatus = KdReceivePacket(PACKET_TYPE_KD_ACKNOWLEDGE, - NULL, - NULL, - 0, - NULL); + NULL, + NULL, + 0, + KdContext); /* Did we succeed? */ if (KdStatus == KDP_PACKET_RECEIVED) @@ -413,9 +415,14 @@ KdSendPacket( return; } - /* Packet timed out, send it again */ - } + if (KdStatus == KDP_PACKET_TIMEOUT) + { + Retries--; + } - return; + /* Packet timed out, send it again */ + KDDBGPRINT("KdSendPacket got KdStatus 0x%x\n", KdStatus); + } + while (Retries > 0); } diff --git a/reactos/drivers/base/kddll/kddll.rbuild b/reactos/drivers/base/kddll/kddll.rbuild index 4e243beca1c..3fca485e8e7 100644 --- a/reactos/drivers/base/kddll/kddll.rbuild +++ b/reactos/drivers/base/kddll/kddll.rbuild @@ -10,6 +10,7 @@ . + ntoskrnl kdserial.c diff --git a/reactos/drivers/base/kddll/kdserial.c b/reactos/drivers/base/kddll/kdserial.c index 6964ab95ae6..14a3c4ecb2c 100644 --- a/reactos/drivers/base/kddll/kdserial.c +++ b/reactos/drivers/base/kddll/kdserial.c @@ -124,7 +124,7 @@ KdpReceivePacketLeader( /* Check for breakin byte */ if (Byte == BREAKIN_PACKET_BYTE) { - KdpDbgPrint("BREAKIN_PACKET_BYTE\n"); + KDDBGPRINT("BREAKIN_PACKET_BYTE\n"); Index = 0; Buffer[0] = Byte; continue; @@ -138,7 +138,7 @@ KdpReceivePacketLeader( while (Index < 4); /* Enable the debugger */ -// KdDebuggerNotPresent = FALSE; + KdDebuggerNotPresent = FALSE; SharedUserData->KdDebuggerEnabled |= 0x00000002; /* Return the received packet leader */