Leaked source code of windows server 2003
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.

403 lines
13 KiB

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