Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
11 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // KD hard-line communication support.
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999-2001.
  6. //
  7. //----------------------------------------------------------------------------
  8. #ifndef __DBGKDTRANS_HPP__
  9. #define __DBGKDTRANS_HPP__
  10. #define PACKET_MAX_MANIP_SIZE \
  11. (PACKET_MAX_SIZE + sizeof(DBGKD_MANIPULATE_STATE64) - \
  12. sizeof(DBGKD_MANIPULATE_STATE32) + sizeof(KD_PACKET))
  13. enum
  14. {
  15. DBGKD_WRITE_PACKET,
  16. DBGKD_WRITE_RESEND,
  17. };
  18. enum
  19. {
  20. DBGKD_WAIT_PACKET,
  21. DBGKD_WAIT_ACK,
  22. DBGKD_WAIT_RESYNC,
  23. DBGKD_WAIT_FAILED,
  24. DBGKD_WAIT_RESEND,
  25. DBGKD_WAIT_AGAIN,
  26. };
  27. enum
  28. {
  29. DBGKD_TRANSPORT_COM,
  30. DBGKD_TRANSPORT_1394,
  31. DBGKD_TRANSPORT_COUNT
  32. };
  33. void InitKdFileAssoc(void);
  34. void ParseKdFileAssoc(void);
  35. class DbgKdTransport : public ParameterStringParser
  36. {
  37. public:
  38. //
  39. // DbgKdTransport.
  40. //
  41. void Restart(void);
  42. // Base implementation displays general information and
  43. // packets/bytes read/written. It should be invoked
  44. // before derived operations.
  45. virtual void OutputInfo(void);
  46. // Base implementation creates events for overlapped operations.
  47. virtual HRESULT Initialize(void);
  48. // Base implementation cleans up events.
  49. virtual void Uninitialize(void);
  50. virtual BOOL Read(IN PVOID Buffer,
  51. IN ULONG SizeOfBuffer,
  52. IN PULONG BytesRead) = 0;
  53. virtual BOOL Write(IN PVOID Buffer,
  54. IN ULONG SizeOfBuffer,
  55. IN PULONG BytesWritten) = 0;
  56. // Base implementation does nothing.
  57. virtual void CycleSpeed(void);
  58. virtual HRESULT ReadTargetPhysicalMemory(IN ULONG64 MemoryOffset,
  59. IN PVOID Buffer,
  60. IN ULONG SizeofBuffer,
  61. IN PULONG BytesRead);
  62. // This routine keeps on sending reset packet to target until reset packet
  63. // is acknowledged by a reset packet from target.
  64. //
  65. // N.B. This routine is intended to be used by kernel debugger at startup
  66. // time (ONLY) to get packet control variables on both target and host
  67. // back in synchronization. Also, reset request will cause kernel to
  68. // reset its control variables AND resend us its previous packet (with
  69. // the new packet id).
  70. virtual VOID Synchronize(VOID) = 0;
  71. virtual ULONG ReadPacketContents(IN USHORT PacketType) = 0;
  72. virtual ULONG WritePacketContents(IN KD_PACKET* Packet,
  73. IN PVOID PacketData,
  74. IN USHORT PacketDataLength,
  75. IN PVOID MorePacketData OPTIONAL,
  76. IN USHORT MorePacketDataLength OPTIONAL,
  77. IN BOOL NoAck) = 0;
  78. ULONG HandleDebugIo(PDBGKD_DEBUG_IO Packet);
  79. ULONG HandleTraceIo(PDBGKD_TRACE_IO Packet);
  80. ULONG HandleControlRequest(PDBGKD_CONTROL_REQUEST Packet);
  81. ULONG HandleFileIo(PDBGKD_FILE_IO Packet);
  82. ULONG WaitForPacket(IN USHORT PacketType,
  83. OUT PVOID Packet);
  84. VOID WriteBreakInPacket(VOID);
  85. VOID WriteControlPacket(IN USHORT PacketType,
  86. IN ULONG PacketId OPTIONAL);
  87. VOID WriteDataPacket(IN PVOID PacketData,
  88. IN USHORT PacketDataLength,
  89. IN USHORT PacketType,
  90. IN PVOID MorePacketData OPTIONAL,
  91. IN USHORT MorePacketDataLength OPTIONAL,
  92. IN BOOL NoAck);
  93. inline VOID WritePacket(IN PVOID PacketData,
  94. IN USHORT PacketDataLength,
  95. IN USHORT PacketType,
  96. IN PVOID MorePacketData OPTIONAL,
  97. IN USHORT MorePacketDataLength OPTIONAL)
  98. {
  99. WriteDataPacket(PacketData, PacketDataLength, PacketType,
  100. MorePacketData, MorePacketDataLength, FALSE);
  101. }
  102. inline VOID WriteAsyncPacket(IN PVOID PacketData,
  103. IN USHORT PacketDataLength,
  104. IN USHORT PacketType,
  105. IN PVOID MorePacketData OPTIONAL,
  106. IN USHORT MorePacketDataLength OPTIONAL)
  107. {
  108. WriteDataPacket(PacketData, PacketDataLength, PacketType,
  109. MorePacketData, MorePacketDataLength, TRUE);
  110. }
  111. ULONG ComputeChecksum(IN PUCHAR Buffer,
  112. IN ULONG Length);
  113. // This save/restore isn't particularly elegant
  114. // but we need something like it. We receive a state change
  115. // without knowing what kind of machine we're talking to.
  116. // We have to send/receive a GetVersion packet to get that
  117. // information, but we need to keep the original packet
  118. // information around while we do so.
  119. void SaveReadPacket(void)
  120. {
  121. memcpy(s_SavedPacket, s_Packet, sizeof(s_Packet));
  122. s_SavedPacketHeader = s_PacketHeader;
  123. }
  124. void RestoreReadPacket(void)
  125. {
  126. memcpy(s_Packet, s_SavedPacket, sizeof(s_Packet));
  127. s_PacketHeader = s_SavedPacketHeader;
  128. }
  129. ULONG m_Index;
  130. HANDLE m_Handle;
  131. BOOL m_DirectPhysicalMemory;
  132. ULONG m_InvPacketRetryLimit;
  133. BOOL m_AckWrites;
  134. //
  135. // This overlapped structure will be used for all serial read
  136. // operations. We only need one structure since the code is
  137. // designed so that no more than one serial read operation is
  138. // outstanding at any one time.
  139. //
  140. OVERLAPPED m_ReadOverlapped;
  141. //
  142. // This overlapped structure will be used for all serial write
  143. // operations. We only need one structure since the code is
  144. // designed so that no more than one serial write operation is
  145. // outstanding at any one time.
  146. //
  147. OVERLAPPED m_WriteOverlapped;
  148. ULONG m_PacketsRead;
  149. ULONG64 m_BytesRead;
  150. ULONG m_PacketsWritten;
  151. ULONG64 m_BytesWritten;
  152. // ID for expected incoming packet.
  153. ULONG m_PacketExpected;
  154. // ID for Next packet to send.
  155. ULONG m_NextPacketToSend;
  156. // Thread ID for thread in WaitStateChange. Normally
  157. // multithreaded access to the transport is prevented by
  158. // the engine lock, but the engine lock is suspended
  159. // while WaitStateChange is doing its WaitPacketForever.
  160. // During that time other threads must be forcibly
  161. // prevented from using the kernel connection.
  162. ULONG m_WaitingThread;
  163. BOOL m_AllowInitialBreak;
  164. BOOL m_Resync;
  165. BOOL m_BreakIn;
  166. BOOL m_SyncBreakIn;
  167. BOOL m_ValidUnaccessedPacket;
  168. static UCHAR s_BreakinPacket[1];
  169. static UCHAR s_PacketTrailingByte[1];
  170. static UCHAR s_PacketLeader[4];
  171. static UCHAR s_Packet[PACKET_MAX_MANIP_SIZE];
  172. static KD_PACKET s_PacketHeader;
  173. static UCHAR s_SavedPacket[PACKET_MAX_MANIP_SIZE];
  174. static KD_PACKET s_SavedPacketHeader;
  175. };
  176. class DbgKdComTransport : public DbgKdTransport
  177. {
  178. public:
  179. DbgKdComTransport(void);
  180. // ParameterStringParser.
  181. virtual ULONG GetNumberParameters(void);
  182. virtual void GetParameter(ULONG Index, PSTR Name, PSTR Value);
  183. virtual void ResetParameters(void);
  184. virtual BOOL SetParameter(PCSTR Name, PCSTR Value);
  185. // DbgKdTransport.
  186. virtual HRESULT Initialize(void);
  187. virtual void Uninitialize(void);
  188. virtual BOOL Read(IN PVOID Buffer,
  189. IN ULONG SizeOfBuffer,
  190. IN PULONG BytesRead);
  191. virtual BOOL Write(IN PVOID Buffer,
  192. IN ULONG SizeOfBuffer,
  193. IN PULONG BytesWritten);
  194. virtual void CycleSpeed(void);
  195. virtual VOID Synchronize(VOID);
  196. virtual ULONG ReadPacketContents(IN USHORT PacketType);
  197. virtual ULONG WritePacketContents(IN KD_PACKET* Packet,
  198. IN PVOID PacketData,
  199. IN USHORT PacketDataLength,
  200. IN PVOID MorePacketData OPTIONAL,
  201. IN USHORT MorePacketDataLength OPTIONAL,
  202. IN BOOL NoAck);
  203. //
  204. // DbgKdComTransport.
  205. //
  206. ULONG ReadPacketLeader(IN ULONG PacketType,
  207. OUT PULONG PacketLeader);
  208. void CheckComStatus(void);
  209. DWORD SelectNewBaudRate(DWORD NewRate);
  210. char m_PortName[MAX_PARAM_VALUE + 8];
  211. ULONG m_BaudRate;
  212. BOOL m_Modem;
  213. ULONG m_Timeout;
  214. // Used to carrier detection.
  215. DWORD m_ComEvent;
  216. //
  217. // This overlapped structure will be used for all event operations.
  218. // We only need one structure since the code is designed so that no more
  219. // than one serial event operation is outstanding at any one time.
  220. //
  221. OVERLAPPED m_EventOverlapped;
  222. };
  223. enum
  224. {
  225. DBGKD_1394_OPERATION_MODE_DEBUG,
  226. DBGKD_1394_OPERATION_RAW_MEMORY_ACCESS
  227. };
  228. class DbgKd1394Transport : public DbgKdTransport
  229. {
  230. public:
  231. DbgKd1394Transport(void);
  232. // ParameterStringParser.
  233. virtual ULONG GetNumberParameters(void);
  234. virtual void GetParameter(ULONG Index, PSTR Name, PSTR Value);
  235. virtual void ResetParameters(void);
  236. virtual BOOL SetParameter(PCSTR Name, PCSTR Value);
  237. // DbgKdTransport.
  238. virtual HRESULT Initialize(void);
  239. virtual void Uninitialize(void);
  240. virtual BOOL Read(IN PVOID Buffer,
  241. IN ULONG SizeOfBuffer,
  242. IN PULONG BytesRead);
  243. virtual BOOL Write(IN PVOID Buffer,
  244. IN ULONG SizeOfBuffer,
  245. IN PULONG BytesWritten);
  246. virtual HRESULT ReadTargetPhysicalMemory(IN ULONG64 MemoryOffset,
  247. IN PVOID Buffer,
  248. IN ULONG SizeofBuffer,
  249. IN PULONG BytesRead);
  250. virtual VOID Synchronize(VOID);
  251. virtual ULONG ReadPacketContents(IN USHORT PacketType);
  252. virtual ULONG WritePacketContents(IN KD_PACKET* Packet,
  253. IN PVOID PacketData,
  254. IN USHORT PacketDataLength,
  255. IN PVOID MorePacketData OPTIONAL,
  256. IN USHORT MorePacketDataLength OPTIONAL,
  257. IN BOOL NoAck);
  258. // DbgKd1394Transport.
  259. BOOL SwitchVirtualDebuggerDriverMode(IN ULONG NewMode);
  260. ULONG m_Channel;
  261. ULONG m_OperationMode;
  262. UCHAR m_TxPacket[PACKET_MAX_MANIP_SIZE];
  263. };
  264. extern DbgKdTransport* g_DbgKdTransport;
  265. HRESULT
  266. DbgKdConnectAndInitialize(PCSTR Options);
  267. VOID
  268. DbgKdpHandlePromptString(
  269. IN PDBGKD_DEBUG_IO IoMessage
  270. );
  271. VOID
  272. DbgKdpPrint(
  273. IN ULONG Processor,
  274. IN PCSTR String,
  275. IN USHORT StringLength,
  276. IN ULONG Mask
  277. );
  278. VOID
  279. DbgKdpPrintTrace(
  280. IN ULONG Processor,
  281. IN PUCHAR Data,
  282. IN USHORT DataLength,
  283. IN ULONG Mask
  284. );
  285. #endif // #ifndef __DBGKDTRANS_HPP__