Commit | Line | Data |
---|---|---|
18b144ea | 1 | /** @file\r |
2 | Commond Debug Agent library implementition. It mainly includes\r | |
3 | the first C function called by exception/interrupt handlers,\r | |
4 | read/write debug packet to communication with HOST based on transfer\r | |
5 | protocol.\r | |
6 | \r | |
b422b62c | 7 | Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r |
18b144ea | 8 | This program and the accompanying materials\r |
9 | are licensed and made available under the terms and conditions of the BSD License\r | |
10 | which accompanies this distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php.\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | \r | |
16 | **/\r | |
17 | \r | |
18 | #include "DebugAgent.h"\r | |
19 | #include "Ia32/DebugException.h"\r | |
20 | \r | |
b422b62c | 21 | CHAR8 mErrorMsgVersionAlert[] = "\rThe SourceLevelDebugPkg you are using requires a newer version of the Intel(R) UDK Debugger Tool.\r\n";\r |
4db921ce | 22 | CHAR8 mErrorMsgSendInitPacket[] = "\rSend INIT break packet and try to connect the HOST (Intel(R) UDK Debugger Tool v1.3.1) ...\r\n";\r |
b422b62c | 23 | CHAR8 mErrorMsgConnectOK[] = "HOST connection is successful!\r\n";\r |
24 | CHAR8 mErrorMsgConnectFail[] = "HOST connection is failed!\r\n";\r | |
25 | CHAR8 mWarningMsgIngoreBreakpoint[] = "Ignore break point in SMM for SMI issued during DXE debugging!\r\n";\r | |
93c0bdec | 26 | \r |
8cc26df4 JF |
27 | //\r |
28 | // Vector Handoff Info list used by Debug Agent for persist\r | |
29 | //\r | |
30 | EFI_VECTOR_HANDOFF_INFO mVectorHandoffInfoDebugAgent[] = {\r | |
31 | {\r | |
32 | DEBUG_EXCEPT_DIVIDE_ERROR, // Vector 0\r | |
33 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
34 | EFI_DEBUG_AGENT_GUID\r | |
35 | },\r | |
36 | {\r | |
37 | DEBUG_EXCEPT_DEBUG, // Vector 1\r | |
38 | EFI_VECTOR_HANDOFF_DO_NOT_HOOK,\r | |
39 | EFI_DEBUG_AGENT_GUID\r | |
40 | },\r | |
41 | {\r | |
42 | DEBUG_EXCEPT_NMI, // Vector 2\r | |
43 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
44 | EFI_DEBUG_AGENT_GUID\r | |
45 | },\r | |
46 | {\r | |
47 | DEBUG_EXCEPT_BREAKPOINT, // Vector 3\r | |
48 | EFI_VECTOR_HANDOFF_DO_NOT_HOOK,\r | |
49 | EFI_DEBUG_AGENT_GUID\r | |
50 | },\r | |
51 | {\r | |
52 | DEBUG_EXCEPT_OVERFLOW, // Vector 4\r | |
53 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
54 | EFI_DEBUG_AGENT_GUID\r | |
55 | },\r | |
56 | {\r | |
57 | DEBUG_EXCEPT_BOUND, // Vector 5\r | |
58 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
59 | EFI_DEBUG_AGENT_GUID\r | |
60 | },\r | |
61 | {\r | |
62 | DEBUG_EXCEPT_INVALID_OPCODE, // Vector 6\r | |
63 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
64 | EFI_DEBUG_AGENT_GUID\r | |
65 | },\r | |
66 | {\r | |
67 | DEBUG_EXCEPT_DOUBLE_FAULT, // Vector 8\r | |
68 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
69 | EFI_DEBUG_AGENT_GUID\r | |
70 | },\r | |
71 | {\r | |
72 | DEBUG_EXCEPT_INVALID_TSS, // Vector 10\r | |
73 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
74 | EFI_DEBUG_AGENT_GUID\r | |
75 | },\r | |
76 | {\r | |
77 | DEBUG_EXCEPT_SEG_NOT_PRESENT, // Vector 11\r | |
78 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
79 | EFI_DEBUG_AGENT_GUID\r | |
80 | },\r | |
81 | {\r | |
82 | DEBUG_EXCEPT_STACK_FAULT, // Vector 12\r | |
83 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
84 | EFI_DEBUG_AGENT_GUID\r | |
85 | },\r | |
86 | {\r | |
87 | DEBUG_EXCEPT_GP_FAULT, // Vector 13\r | |
88 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
89 | EFI_DEBUG_AGENT_GUID\r | |
90 | },\r | |
91 | {\r | |
92 | DEBUG_EXCEPT_PAGE_FAULT, // Vector 14\r | |
93 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
94 | EFI_DEBUG_AGENT_GUID\r | |
95 | },\r | |
96 | {\r | |
97 | DEBUG_EXCEPT_FP_ERROR, // Vector 16\r | |
98 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
99 | EFI_DEBUG_AGENT_GUID\r | |
100 | },\r | |
101 | {\r | |
102 | DEBUG_EXCEPT_ALIGNMENT_CHECK, // Vector 17\r | |
103 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
104 | EFI_DEBUG_AGENT_GUID\r | |
105 | },\r | |
106 | {\r | |
107 | DEBUG_EXCEPT_MACHINE_CHECK, // Vector 18\r | |
108 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
109 | EFI_DEBUG_AGENT_GUID\r | |
110 | },\r | |
111 | {\r | |
112 | DEBUG_EXCEPT_SIMD, // Vector 19\r | |
113 | EFI_VECTOR_HANDOFF_HOOK_BEFORE,\r | |
114 | EFI_DEBUG_AGENT_GUID\r | |
115 | },\r | |
116 | {\r | |
117 | DEBUG_TIMER_VECTOR, // Vector 32\r | |
118 | EFI_VECTOR_HANDOFF_DO_NOT_HOOK,\r | |
119 | EFI_DEBUG_AGENT_GUID\r | |
120 | },\r | |
121 | {\r | |
122 | DEBUG_MAILBOX_VECTOR, // Vector 33\r | |
123 | EFI_VECTOR_HANDOFF_DO_NOT_HOOK,\r | |
124 | EFI_DEBUG_AGENT_GUID\r | |
125 | },\r | |
126 | {\r | |
127 | 0,\r | |
128 | EFI_VECTOR_HANDOFF_LAST_ENTRY,\r | |
129 | { 0 }\r | |
130 | }\r | |
131 | };\r | |
132 | \r | |
133 | UINTN mVectorHandoffInfoCount = sizeof (mVectorHandoffInfoDebugAgent) / sizeof (EFI_VECTOR_HANDOFF_INFO);\r | |
134 | \r | |
b422b62c | 135 | /**\r |
136 | Calculate CRC16 for target data.\r | |
137 | \r | |
138 | @param[in] Data The target data.\r | |
139 | @param[in] DataSize The target data size.\r | |
140 | @param[in] Crc Initial CRC.\r | |
141 | \r | |
142 | @return UINT16 The CRC16 value.\r | |
143 | \r | |
144 | **/\r | |
145 | UINT16\r | |
146 | CalculateCrc16 (\r | |
147 | IN UINT8 *Data,\r | |
148 | IN UINTN DataSize,\r | |
149 | IN UINT16 Crc\r | |
150 | )\r | |
151 | {\r | |
152 | UINTN Index;\r | |
153 | UINTN BitIndex;\r | |
154 | \r | |
155 | for (Index = 0; Index < DataSize; Index++) {\r | |
156 | Crc ^= Data[Index];\r | |
157 | for (BitIndex = 0; BitIndex < 8; BitIndex++) {\r | |
158 | if ((Crc & 0x8000) != 0) {\r | |
159 | Crc <<= 1;\r | |
160 | Crc ^= 0x1021;\r | |
161 | } else {\r | |
162 | Crc <<= 1;\r | |
163 | }\r | |
164 | }\r | |
165 | }\r | |
166 | return Crc;\r | |
167 | }\r | |
168 | \r | |
169 | \r | |
170 | /**\r | |
171 | Read IDT entry to check if IDT entries are setup by Debug Agent.\r | |
172 | \r | |
173 | @retval TRUE IDT entries were setup by Debug Agent.\r | |
174 | @retval FALSE IDT entries were not setup by Debug Agent.\r | |
175 | \r | |
176 | **/\r | |
177 | BOOLEAN \r | |
178 | IsDebugAgentInitialzed (\r | |
179 | VOID\r | |
180 | )\r | |
181 | {\r | |
182 | UINTN InterruptHandler;\r | |
183 | \r | |
184 | InterruptHandler = (UINTN) GetExceptionHandlerInIdtEntry (0);\r | |
185 | if (InterruptHandler >= 4 && *(UINT32 *)(InterruptHandler - 4) == AGENT_HANDLER_SIGNATURE) {\r | |
186 | return TRUE;\r | |
187 | } else {\r | |
188 | return FALSE;\r | |
189 | }\r | |
190 | }\r | |
191 | \r | |
192 | /**\r | |
193 | Find and report module image info to HOST.\r | |
194 | \r | |
195 | @param[in] AlignSize Image aligned size.\r | |
196 | \r | |
197 | **/\r | |
198 | VOID \r | |
199 | FindAndReportModuleImageInfo (\r | |
200 | IN UINTN AlignSize \r | |
201 | )\r | |
202 | {\r | |
203 | UINTN Pe32Data;\r | |
204 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
205 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
206 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
207 | \r | |
208 | //\r | |
209 | // Find Image Base\r | |
210 | //\r | |
211 | Pe32Data = ((UINTN)mErrorMsgVersionAlert) & ~(AlignSize - 1);\r | |
212 | while (Pe32Data != 0) {\r | |
213 | DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;\r | |
214 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
215 | //\r | |
216 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
217 | // Check if address overflow firstly.\r | |
218 | //\r | |
219 | if ((MAX_ADDRESS - (UINTN)DosHdr->e_lfanew) > Pe32Data) {\r | |
220 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN)(DosHdr->e_lfanew));\r | |
221 | if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
222 | //\r | |
223 | // It's PE image.\r | |
224 | //\r | |
225 | break;\r | |
226 | }\r | |
227 | }\r | |
228 | } else {\r | |
229 | //\r | |
230 | // DOS image header is not present, TE header is at the image base.\r | |
231 | //\r | |
232 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
233 | if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&\r | |
234 | ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64)) {\r | |
235 | //\r | |
236 | // It's TE image, it TE header and Machine type match\r | |
237 | //\r | |
238 | break;\r | |
239 | }\r | |
240 | }\r | |
241 | \r | |
242 | //\r | |
243 | // Not found the image base, check the previous aligned address\r | |
244 | // \r | |
245 | Pe32Data -= AlignSize;\r | |
246 | }\r | |
247 | \r | |
248 | ImageContext.ImageAddress = Pe32Data;\r | |
249 | ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);\r | |
250 | PeCoffLoaderRelocateImageExtraAction (&ImageContext);\r | |
251 | }\r | |
252 | \r | |
253 | /**\r | |
254 | Trigger one software interrupt to debug agent to handle it.\r | |
255 | \r | |
256 | @param[in] Signature Software interrupt signature.\r | |
257 | \r | |
258 | **/\r | |
259 | VOID\r | |
260 | TriggerSoftInterrupt (\r | |
261 | IN UINT32 Signature\r | |
262 | )\r | |
263 | {\r | |
264 | UINTN Dr0;\r | |
265 | UINTN Dr1;\r | |
266 | \r | |
267 | //\r | |
268 | // Save Debug Register State\r | |
269 | //\r | |
270 | Dr0 = AsmReadDr0 ();\r | |
271 | Dr1 = AsmReadDr1 ();\r | |
272 | \r | |
273 | //\r | |
274 | // DR0 = Signature\r | |
275 | //\r | |
276 | AsmWriteDr0 (SOFT_INTERRUPT_SIGNATURE);\r | |
277 | AsmWriteDr1 (Signature);\r | |
278 | \r | |
279 | //\r | |
280 | // Do INT3 to communicate with HOST side\r | |
281 | //\r | |
282 | CpuBreakpoint ();\r | |
283 | \r | |
284 | //\r | |
285 | // Restore Debug Register State only when Host didn't change it inside exception handler.\r | |
286 | // Dr registers can only be changed by setting the HW breakpoint.\r | |
287 | //\r | |
288 | AsmWriteDr0 (Dr0);\r | |
289 | AsmWriteDr1 (Dr1);\r | |
290 | \r | |
291 | }\r | |
292 | \r | |
293 | /**\r | |
294 | Caculate Mailbox checksum and update the checksum field.\r | |
295 | \r | |
296 | @param[in] Mailbox Debug Agent Mailbox pointer.\r | |
297 | \r | |
298 | **/\r | |
299 | VOID\r | |
300 | UpdateMailboxChecksum (\r | |
301 | IN DEBUG_AGENT_MAILBOX *Mailbox\r | |
302 | )\r | |
303 | {\r | |
304 | Mailbox->CheckSum = CalculateCheckSum8 ((UINT8 *)Mailbox, sizeof (DEBUG_AGENT_MAILBOX) - 2); \r | |
305 | }\r | |
306 | \r | |
307 | /**\r | |
308 | Verify Mailbox checksum.\r | |
309 | \r | |
310 | If checksum error, print debug message and run init dead loop.\r | |
311 | \r | |
312 | @param[in] Mailbox Debug Agent Mailbox pointer.\r | |
313 | \r | |
314 | **/\r | |
315 | VOID \r | |
316 | VerifyMailboxChecksum (\r | |
317 | IN DEBUG_AGENT_MAILBOX *Mailbox\r | |
318 | )\r | |
319 | {\r | |
320 | UINT8 CheckSum;\r | |
321 | \r | |
322 | CheckSum = CalculateCheckSum8 ((UINT8 *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX) - 2);\r | |
323 | //\r | |
324 | // The checksum updating process may be disturbed by hardware SMI, we need to check CheckSum field \r | |
325 | // and ToBeCheckSum field to validate the mail box.\r | |
326 | //\r | |
327 | if (CheckSum != Mailbox->CheckSum && CheckSum != Mailbox->ToBeCheckSum) {\r | |
328 | DEBUG ((EFI_D_ERROR, "DebugAgent: Mailbox checksum error, stack or heap crashed!\n"));\r | |
329 | DEBUG ((EFI_D_ERROR, "DebugAgent: CheckSum = %x, Mailbox->CheckSum = %x, Mailbox->ToBeCheckSum = %x\n", CheckSum, Mailbox->CheckSum, Mailbox->ToBeCheckSum));\r | |
330 | CpuDeadLoop ();\r | |
331 | }\r | |
332 | }\r | |
333 | \r | |
334 | /**\r | |
335 | Update Mailbox content by index.\r | |
336 | \r | |
337 | @param[in] Mailbox Debug Agent Mailbox pointer.\r | |
338 | @param[in] Index Mailbox content index.\r | |
339 | @param[in] Value Value to be set into Mailbox.\r | |
340 | \r | |
341 | **/\r | |
342 | VOID\r | |
343 | UpdateMailboxContent ( \r | |
344 | IN DEBUG_AGENT_MAILBOX *Mailbox,\r | |
345 | IN UINTN Index,\r | |
346 | IN UINT64 Value\r | |
347 | )\r | |
348 | {\r | |
349 | AcquireMpSpinLock (&mDebugMpContext.MailboxSpinLock);\r | |
350 | switch (Index) {\r | |
351 | case DEBUG_MAILBOX_DEBUG_FLAG_INDEX:\r | |
352 | Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugFlag.Uint64, sizeof(UINT64)) \r | |
353 | - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT64));\r | |
354 | Mailbox->DebugFlag.Uint64 = Value;\r | |
355 | break;\r | |
356 | case DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX:\r | |
357 | Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugPortHandle, sizeof(UINTN))\r | |
358 | - CalculateSum8 ((UINT8 *)&Value, sizeof(UINTN));\r | |
359 | Mailbox->DebugPortHandle = (UINTN) Value;\r | |
360 | break;\r | |
361 | case DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX:\r | |
362 | Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->ExceptionBufferPointer, sizeof(UINTN))\r | |
363 | - CalculateSum8 ((UINT8 *)&Value, sizeof(UINTN));\r | |
364 | Mailbox->ExceptionBufferPointer = (UINTN) Value;\r | |
365 | break;\r | |
366 | case DEBUG_MAILBOX_LAST_ACK:\r | |
367 | Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->LastAck, sizeof(UINT8))\r | |
368 | - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));\r | |
369 | Mailbox->LastAck = (UINT8) Value;\r | |
370 | break;\r | |
371 | case DEBUG_MAILBOX_SEQUENCE_NO_INDEX:\r | |
372 | Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->SequenceNo, sizeof(UINT8))\r | |
373 | - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));\r | |
374 | Mailbox->SequenceNo = (UINT8) Value;\r | |
375 | break;\r | |
376 | case DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX:\r | |
377 | Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->HostSequenceNo, sizeof(UINT8))\r | |
378 | - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));\r | |
379 | Mailbox->HostSequenceNo = (UINT8) Value;\r | |
380 | break;\r | |
381 | }\r | |
382 | UpdateMailboxChecksum (Mailbox);\r | |
383 | ReleaseMpSpinLock (&mDebugMpContext.MailboxSpinLock);\r | |
384 | }\r | |
385 | /**\r | |
386 | Set debug flag in mailbox.\r | |
387 | \r | |
388 | @param[in] FlagMask Debug flag mask value.\r | |
389 | @param[in] FlagValue Debug flag value.\r | |
390 | \r | |
391 | **/\r | |
392 | VOID \r | |
393 | SetDebugFlag (\r | |
394 | IN UINT64 FlagMask,\r | |
395 | IN UINT32 FlagValue \r | |
396 | )\r | |
397 | {\r | |
398 | DEBUG_AGENT_MAILBOX *Mailbox;\r | |
399 | UINT64 Data64;\r | |
400 | \r | |
401 | Mailbox = GetMailboxPointer ();\r | |
402 | Data64 = (Mailbox->DebugFlag.Uint64 & ~FlagMask) |\r | |
403 | (LShiftU64 ((UINT64)FlagValue, LowBitSet64 (FlagMask)) & FlagMask);\r | |
404 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_FLAG_INDEX, Data64);\r | |
405 | }\r | |
406 | \r | |
407 | /**\r | |
408 | Get debug flag in mailbox.\r | |
409 | \r | |
410 | @param[in] FlagMask Debug flag mask value.\r | |
411 | \r | |
412 | @return Debug flag value.\r | |
413 | \r | |
414 | **/\r | |
415 | UINT32\r | |
416 | GetDebugFlag (\r | |
417 | IN UINT64 FlagMask\r | |
418 | )\r | |
419 | {\r | |
420 | DEBUG_AGENT_MAILBOX *Mailbox;\r | |
421 | UINT32 DebugFlag;\r | |
422 | \r | |
423 | Mailbox = GetMailboxPointer ();\r | |
424 | DebugFlag = (UINT32) RShiftU64 (Mailbox->DebugFlag.Uint64 & FlagMask, LowBitSet64 (FlagMask));\r | |
425 | \r | |
426 | return DebugFlag;\r | |
427 | }\r | |
93c0bdec | 428 | \r |
18b144ea | 429 | /**\r |
93c0bdec | 430 | Send a debug message packet to the debug port.\r |
18b144ea | 431 | \r |
93c0bdec | 432 | @param[in] Buffer The debug message.\r |
433 | @param[in] Length The length of debug message.\r | |
18b144ea | 434 | \r |
435 | **/\r | |
93c0bdec | 436 | VOID\r |
437 | SendDebugMsgPacket (\r | |
438 | IN CHAR8 *Buffer,\r | |
439 | IN UINTN Length \r | |
18b144ea | 440 | )\r |
441 | {\r | |
93c0bdec | 442 | DEBUG_PACKET_HEADER DebugHeader;\r |
443 | DEBUG_PORT_HANDLE Handle;\r | |
444 | \r | |
445 | Handle = GetDebugPortHandle();\r | |
18b144ea | 446 | \r |
93c0bdec | 447 | DebugHeader.StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;\r |
448 | DebugHeader.Command = DEBUG_COMMAND_PRINT_MESSAGE;\r | |
449 | DebugHeader.Length = sizeof (DEBUG_PACKET_HEADER) + (UINT8) Length;\r | |
b422b62c | 450 | DebugHeader.SequenceNo = 0xEE;\r |
451 | DebugHeader.Crc = 0;\r | |
452 | DebugHeader.Crc = CalculateCrc16 (\r | |
453 | (UINT8 *)Buffer, Length,\r | |
454 | CalculateCrc16 ((UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0)\r | |
455 | );\r | |
18b144ea | 456 | \r |
93c0bdec | 457 | DebugPortWriteBuffer (Handle, (UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER));\r |
458 | DebugPortWriteBuffer (Handle, (UINT8 *)Buffer, Length);\r | |
459 | }\r | |
460 | \r | |
461 | /**\r | |
462 | Prints a debug message to the debug port if the specified error level is enabled.\r | |
463 | \r | |
464 | If any bit in ErrorLevel is also set in Mainbox, then print the message specified\r | |
465 | by Format and the associated variable argument list to the debug port.\r | |
466 | \r | |
467 | @param[in] ErrorLevel The error level of the debug message.\r | |
468 | @param[in] Format Format string for the debug message to print.\r | |
469 | @param[in] ... Variable argument list whose contents are accessed \r | |
470 | based on the format string specified by Format.\r | |
471 | \r | |
472 | **/\r | |
473 | VOID\r | |
474 | EFIAPI\r | |
475 | DebugAgentMsgPrint (\r | |
476 | IN UINT8 ErrorLevel,\r | |
477 | IN CHAR8 *Format,\r | |
478 | ...\r | |
479 | )\r | |
480 | {\r | |
93c0bdec | 481 | CHAR8 Buffer[DEBUG_DATA_MAXIMUM_REAL_DATA];\r |
482 | VA_LIST Marker;\r | |
483 | \r | |
93c0bdec | 484 | //\r |
485 | // Check driver debug mask value and global mask\r | |
486 | //\r | |
b422b62c | 487 | if ((ErrorLevel & GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL)) == 0) {\r |
93c0bdec | 488 | return;\r |
18b144ea | 489 | }\r |
93c0bdec | 490 | \r |
491 | //\r | |
492 | // Convert the DEBUG() message to an ASCII String\r | |
493 | //\r | |
494 | VA_START (Marker, Format);\r | |
495 | AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);\r | |
496 | VA_END (Marker);\r | |
497 | \r | |
498 | SendDebugMsgPacket (Buffer, AsciiStrLen (Buffer));\r | |
18b144ea | 499 | }\r |
500 | \r | |
501 | /**\r | |
93c0bdec | 502 | Prints a debug message to the debug output device if the specified error level is enabled.\r |
503 | \r | |
504 | If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function \r | |
505 | GetDebugPrintErrorLevel (), then print the message specified by Format and the \r | |
506 | associated variable argument list to the debug output device.\r | |
507 | \r | |
508 | If Format is NULL, then ASSERT().\r | |
509 | \r | |
510 | @param[in] ErrorLevel The error level of the debug message.\r | |
511 | @param[in] IsSend Flag of debug message to declare that the data is being sent or being received.\r | |
512 | @param[in] Data Variable argument list whose contents are accessed \r | |
513 | @param[in] Length based on the format string specified by Format.\r | |
18b144ea | 514 | \r |
515 | **/\r | |
516 | VOID\r | |
93c0bdec | 517 | EFIAPI\r |
518 | DebugAgentDataMsgPrint (\r | |
519 | IN UINT8 ErrorLevel,\r | |
520 | IN BOOLEAN IsSend,\r | |
521 | IN UINT8 *Data,\r | |
522 | IN UINT8 Length \r | |
18b144ea | 523 | )\r |
524 | {\r | |
93c0bdec | 525 | CHAR8 Buffer[DEBUG_DATA_MAXIMUM_REAL_DATA];\r |
526 | CHAR8 *DestBuffer;\r | |
527 | UINTN Index;\r | |
18b144ea | 528 | \r |
93c0bdec | 529 | //\r |
530 | // Check driver debug mask value and global mask\r | |
531 | //\r | |
b422b62c | 532 | if ((ErrorLevel & GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL)) == 0) {\r |
93c0bdec | 533 | return;\r |
534 | }\r | |
535 | \r | |
536 | DestBuffer = Buffer;\r | |
537 | if (IsSend) {\r | |
538 | DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA, "Sent data [ ");\r | |
539 | } else {\r | |
540 | DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA, "Received data [ ");\r | |
541 | }\r | |
18b144ea | 542 | \r |
93c0bdec | 543 | Index = 0;\r |
544 | while (TRUE) {\r | |
545 | if (DestBuffer - Buffer > DEBUG_DATA_MAXIMUM_REAL_DATA - 6) {\r | |
546 | //\r | |
547 | // If there was no enough space in buffer, send out the debug message, \r | |
548 | // reserving 6 bytes is for the last data and end characters "]\n".\r | |
549 | //\r | |
550 | SendDebugMsgPacket (Buffer, DestBuffer - Buffer);\r | |
551 | DestBuffer = Buffer;\r | |
552 | }\r | |
553 | DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA - (DestBuffer - Buffer), "%02x ", Data[Index]);\r | |
554 | Index ++;\r | |
555 | if (Index >= Length) {\r | |
b422b62c | 556 | //\r |
93c0bdec | 557 | // The last character of debug message has been foramtted in buffer\r |
558 | //\r | |
559 | DestBuffer += AsciiSPrint(DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA - (DestBuffer - Buffer), "]\n");\r | |
560 | SendDebugMsgPacket (Buffer, DestBuffer - Buffer);\r | |
561 | break;\r | |
562 | }\r | |
563 | }\r | |
18b144ea | 564 | }\r |
565 | \r | |
b422b62c | 566 | /**\r |
567 | Read remaing debug packet except for the start symbol\r | |
568 | \r | |
569 | @param[in] Handle Pointer to Debug Port handle.\r | |
570 | @param[in, out] DebugHeader Debug header buffer including start symbol.\r | |
571 | \r | |
572 | @retval EFI_SUCCESS Read the symbol in BreakSymbol.\r | |
573 | @retval EFI_CRC_ERROR CRC check fail.\r | |
574 | @retval EFI_TIMEOUT Timeout occurs when reading debug packet.\r | |
575 | \r | |
576 | **/\r | |
577 | EFI_STATUS\r | |
578 | ReadRemainingBreakPacket (\r | |
579 | IN DEBUG_PORT_HANDLE Handle,\r | |
580 | IN OUT DEBUG_PACKET_HEADER *DebugHeader\r | |
581 | )\r | |
582 | {\r | |
583 | UINT16 Crc;\r | |
584 | //\r | |
585 | // Has received start symbol, try to read the rest part\r | |
586 | //\r | |
587 | if (DebugPortReadBuffer (Handle, &DebugHeader->Command, sizeof (DEBUG_PACKET_HEADER) - 1, READ_PACKET_TIMEOUT) == 0) {\r | |
588 | //\r | |
589 | // Timeout occur, exit\r | |
590 | //\r | |
591 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Timeout in Debug Timer interrupt\n");\r | |
592 | return EFI_TIMEOUT;\r | |
593 | }\r | |
594 | \r | |
595 | Crc = DebugHeader->Crc;\r | |
596 | DebugHeader->Crc = 0;\r | |
597 | if (CalculateCrc16 ((UINT8 *)DebugHeader, DebugHeader->Length, 0) != Crc) {\r | |
598 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Debug Timer CRC (%x) against (%x)\n", Crc, CalculateCrc16 ((UINT8 *) &DebugHeader, DebugHeader->Length, 0));\r | |
599 | DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *)DebugHeader, DebugHeader->Length);\r | |
600 | return EFI_CRC_ERROR;\r | |
601 | }\r | |
602 | \r | |
603 | UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, DebugHeader->SequenceNo);\r | |
604 | return EFI_SUCCESS;\r | |
605 | }\r | |
93c0bdec | 606 | \r |
18b144ea | 607 | /**\r |
93c0bdec | 608 | Check if HOST is attached based on Mailbox.\r |
18b144ea | 609 | \r |
93c0bdec | 610 | @retval TRUE HOST is attached.\r |
611 | @retval FALSE HOST is not attached.\r | |
18b144ea | 612 | \r |
93c0bdec | 613 | **/\r |
614 | BOOLEAN\r | |
615 | IsHostAttached (\r | |
616 | VOID\r | |
617 | )\r | |
618 | {\r | |
b422b62c | 619 | return (BOOLEAN) (GetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED) == 1);\r |
93c0bdec | 620 | }\r |
621 | \r | |
622 | /**\r | |
623 | Set HOST connect flag in Mailbox.\r | |
624 | \r | |
625 | @param[in] Attached Attach status.\r | |
626 | \r | |
18b144ea | 627 | **/\r |
628 | VOID\r | |
93c0bdec | 629 | SetHostAttached (\r |
630 | IN BOOLEAN Attached\r | |
18b144ea | 631 | )\r |
632 | {\r | |
93c0bdec | 633 | DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Attach status is %d\n", Attached);\r |
b422b62c | 634 | SetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED, (UINT32)Attached);\r |
93c0bdec | 635 | }\r |
636 | \r | |
637 | /**\r | |
638 | Set debug setting of Debug Agent in Mailbox.\r | |
639 | \r | |
640 | @param DebugSetting Pointer to Debug Setting defined by transfer protocol.\r | |
641 | \r | |
642 | @retval RETURN_SUCCESS The setting is set successfully.\r | |
643 | @retval RETURN_UNSUPPORTED The Key value is not supported.\r | |
644 | \r | |
645 | **/\r | |
646 | RETURN_STATUS\r | |
647 | SetDebugSetting (\r | |
648 | IN DEBUG_DATA_SET_DEBUG_SETTING *DebugSetting \r | |
649 | )\r | |
650 | {\r | |
651 | RETURN_STATUS Status;\r | |
18b144ea | 652 | \r |
93c0bdec | 653 | Status = RETURN_SUCCESS;\r |
654 | switch (DebugSetting->Key) {\r | |
655 | case DEBUG_AGENT_SETTING_SMM_ENTRY_BREAK:\r | |
b422b62c | 656 | SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI, DebugSetting->Value);\r |
93c0bdec | 657 | break;\r |
658 | case DEBUG_AGENT_SETTING_PRINT_ERROR_LEVEL:\r | |
b422b62c | 659 | SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DebugSetting->Value);\r |
660 | break;\r | |
661 | case DEBUG_AGENT_SETTING_BOOT_SCRIPT_ENTRY_BREAK:\r | |
662 | SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT, DebugSetting->Value);\r | |
93c0bdec | 663 | break;\r |
664 | default:\r | |
665 | Status = RETURN_UNSUPPORTED;\r | |
18b144ea | 666 | }\r |
93c0bdec | 667 | return Status;\r |
18b144ea | 668 | }\r |
669 | \r | |
670 | /**\r | |
671 | Exectue GO command.\r | |
672 | \r | |
673 | @param[in] CpuContext Pointer to saved CPU context.\r | |
674 | \r | |
675 | **/\r | |
676 | VOID\r | |
677 | CommandGo (\r | |
678 | IN DEBUG_CPU_CONTEXT *CpuContext\r | |
679 | )\r | |
680 | {\r | |
681 | IA32_EFLAGS32 *Eflags;\r | |
682 | \r | |
683 | Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;\r | |
684 | Eflags->Bits.TF = 0;\r | |
685 | Eflags->Bits.RF = 1;\r | |
686 | }\r | |
687 | \r | |
688 | /**\r | |
689 | Exectue Stepping command.\r | |
690 | \r | |
691 | @param[in] CpuContext Pointer to saved CPU context.\r | |
692 | \r | |
693 | **/\r | |
694 | VOID\r | |
695 | CommandStepping (\r | |
696 | IN DEBUG_CPU_CONTEXT *CpuContext\r | |
697 | )\r | |
698 | {\r | |
699 | IA32_EFLAGS32 *Eflags;\r | |
700 | \r | |
701 | Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;\r | |
702 | Eflags->Bits.TF = 1;\r | |
703 | Eflags->Bits.RF = 1;\r | |
704 | }\r | |
705 | \r | |
706 | /**\r | |
707 | Set debug register for hardware breakpoint.\r | |
708 | \r | |
709 | @param[in] CpuContext Pointer to saved CPU context.\r | |
710 | @param[in] SetHwBreakpoint Hardware breakpoint to be set.\r | |
711 | \r | |
712 | **/\r | |
713 | VOID\r | |
714 | SetDebugRegister (\r | |
715 | IN DEBUG_CPU_CONTEXT *CpuContext,\r | |
716 | IN DEBUG_DATA_SET_HW_BREAKPOINT *SetHwBreakpoint\r | |
717 | )\r | |
718 | {\r | |
719 | UINT8 RegisterIndex;\r | |
720 | UINTN Dr7Value;\r | |
721 | \r | |
722 | RegisterIndex = SetHwBreakpoint->Type.Index;\r | |
723 | \r | |
724 | //\r | |
725 | // Set debug address\r | |
726 | //\r | |
727 | * ((UINTN *) &CpuContext->Dr0 + RegisterIndex) = (UINTN) SetHwBreakpoint->Address;\r | |
728 | \r | |
729 | Dr7Value = CpuContext->Dr7;\r | |
730 | \r | |
731 | //\r | |
732 | // Enable Gx, Lx\r | |
733 | //\r | |
734 | Dr7Value |= 0x3 << (RegisterIndex * 2);\r | |
735 | //\r | |
736 | // Set RWx and Lenx\r | |
737 | //\r | |
93c0bdec | 738 | Dr7Value &= ~(0xf << (16 + RegisterIndex * 4));\r |
739 | Dr7Value |= (UINTN) ((SetHwBreakpoint->Type.Length << 2) | SetHwBreakpoint->Type.Access) << (16 + RegisterIndex * 4);\r | |
18b144ea | 740 | //\r |
741 | // Enable GE, LE\r | |
742 | //\r | |
743 | Dr7Value |= 0x300;\r | |
744 | \r | |
745 | CpuContext->Dr7 = Dr7Value;\r | |
746 | }\r | |
747 | \r | |
748 | /**\r | |
749 | Clear debug register for hardware breakpoint.\r | |
750 | \r | |
751 | @param[in] CpuContext Pointer to saved CPU context.\r | |
752 | @param[in] ClearHwBreakpoint Hardware breakpoint to be cleared.\r | |
753 | \r | |
754 | **/\r | |
755 | VOID\r | |
756 | ClearDebugRegister (\r | |
757 | IN DEBUG_CPU_CONTEXT *CpuContext,\r | |
758 | IN DEBUG_DATA_CLEAR_HW_BREAKPOINT *ClearHwBreakpoint\r | |
759 | )\r | |
760 | {\r | |
761 | if ((ClearHwBreakpoint->IndexMask & BIT0) != 0) {\r | |
762 | CpuContext->Dr0 = 0;\r | |
763 | CpuContext->Dr7 &= ~(0x3 << 0);\r | |
764 | }\r | |
765 | if ((ClearHwBreakpoint->IndexMask & BIT1) != 0) {\r | |
766 | CpuContext->Dr1 = 0;\r | |
767 | CpuContext->Dr7 &= ~(0x3 << 2);\r | |
768 | }\r | |
769 | if ((ClearHwBreakpoint->IndexMask & BIT2) != 0) {\r | |
770 | CpuContext->Dr2 = 0;\r | |
771 | CpuContext->Dr7 &= ~(0x3 << 4);\r | |
772 | }\r | |
773 | if ((ClearHwBreakpoint->IndexMask & BIT3) != 0) {\r | |
774 | CpuContext->Dr3 = 0;\r | |
775 | CpuContext->Dr7 &= ~(0x3 << 6);\r | |
776 | }\r | |
777 | }\r | |
778 | \r | |
93c0bdec | 779 | \r |
18b144ea | 780 | /**\r |
93c0bdec | 781 | Return the offset of FP / MMX / XMM registers in the FPU saved state by register index.\r |
18b144ea | 782 | \r |
93c0bdec | 783 | @param[in] Index Register index.\r |
784 | @param[out] Width Register width returned.\r | |
785 | \r | |
786 | @return Offset in the FPU Save State.\r | |
787 | \r | |
788 | **/\r | |
789 | UINT16\r | |
790 | ArchReadFxStatOffset (\r | |
791 | IN UINT8 Index,\r | |
792 | OUT UINT8 *Width\r | |
793 | )\r | |
794 | {\r | |
795 | if (Index < SOFT_DEBUGGER_REGISTER_ST0) {\r | |
796 | switch (Index) {\r | |
797 | case SOFT_DEBUGGER_REGISTER_FP_FCW:\r | |
798 | *Width = (UINT8) sizeof (UINT16);\r | |
799 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Fcw);\r | |
800 | \r | |
801 | case SOFT_DEBUGGER_REGISTER_FP_FSW:\r | |
802 | *Width = (UINT8) sizeof (UINT16);\r | |
803 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Fsw);\r | |
804 | \r | |
805 | case SOFT_DEBUGGER_REGISTER_FP_FTW:\r | |
806 | *Width = (UINT8) sizeof (UINT16);\r | |
807 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Ftw);\r | |
808 | \r | |
809 | case SOFT_DEBUGGER_REGISTER_FP_OPCODE:\r | |
810 | *Width = (UINT8) sizeof (UINT16);\r | |
811 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Opcode);\r | |
812 | \r | |
813 | case SOFT_DEBUGGER_REGISTER_FP_EIP:\r | |
814 | *Width = (UINT8) sizeof (UINT32);\r | |
815 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Eip);\r | |
816 | \r | |
817 | case SOFT_DEBUGGER_REGISTER_FP_CS:\r | |
818 | *Width = (UINT8) sizeof (UINT16);\r | |
819 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Cs);\r | |
820 | \r | |
821 | case SOFT_DEBUGGER_REGISTER_FP_DATAOFFSET:\r | |
822 | *Width = (UINT8) sizeof (UINT32);\r | |
823 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, DataOffset);\r | |
824 | \r | |
825 | case SOFT_DEBUGGER_REGISTER_FP_DS:\r | |
826 | *Width = (UINT8) sizeof (UINT16);\r | |
827 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Ds);\r | |
828 | \r | |
829 | case SOFT_DEBUGGER_REGISTER_FP_MXCSR:\r | |
830 | *Width = (UINT8) sizeof (UINT32);\r | |
831 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Mxcsr);\r | |
832 | \r | |
833 | case SOFT_DEBUGGER_REGISTER_FP_MXCSR_MASK:\r | |
834 | *Width = (UINT8) sizeof (UINT32);\r | |
835 | return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Mxcsr_Mask);\r | |
836 | }\r | |
837 | }\r | |
838 | \r | |
839 | if (Index <= SOFT_DEBUGGER_REGISTER_ST7) {\r | |
840 | *Width = 10;\r | |
841 | } else if (Index <= SOFT_DEBUGGER_REGISTER_XMM15) {\r | |
842 | *Width = 16;\r | |
843 | } else {\r | |
844 | //\r | |
845 | // MMX register\r | |
846 | //\r | |
847 | *Width = 8;\r | |
848 | Index -= SOFT_DEBUGGER_REGISTER_MM0 - SOFT_DEBUGGER_REGISTER_ST0;\r | |
849 | }\r | |
850 | \r | |
851 | return OFFSET_OF (DEBUG_DATA_FX_SAVE_STATE, St0Mm0) + (Index - SOFT_DEBUGGER_REGISTER_ST0) * 16;\r | |
852 | }\r | |
853 | \r | |
854 | /**\r | |
855 | Return the pointer of the register value in the CPU saved context.\r | |
856 | \r | |
857 | @param[in] CpuContext Pointer to saved CPU context.\r | |
858 | @param[in] Index Register index value.\r | |
859 | @param[out] Width Data width to read.\r | |
860 | \r | |
861 | @return The pointer in the CPU saved context.\r | |
862 | \r | |
863 | **/\r | |
864 | UINT8 *\r | |
865 | ArchReadRegisterBuffer (\r | |
866 | IN DEBUG_CPU_CONTEXT *CpuContext,\r | |
867 | IN UINT8 Index,\r | |
868 | OUT UINT8 *Width\r | |
869 | )\r | |
870 | {\r | |
871 | UINT8 *Buffer;\r | |
872 | \r | |
873 | if (Index < SOFT_DEBUGGER_REGISTER_FP_BASE) {\r | |
874 | Buffer = (UINT8 *) CpuContext + OFFSET_OF (DEBUG_CPU_CONTEXT, Dr0) + Index * sizeof (UINTN);\r | |
875 | *Width = (UINT8) sizeof (UINTN);\r | |
876 | } else {\r | |
877 | //\r | |
878 | // FPU/MMX/XMM registers\r | |
879 | //\r | |
880 | Buffer = (UINT8 *) CpuContext + OFFSET_OF (DEBUG_CPU_CONTEXT, FxSaveState) + ArchReadFxStatOffset (Index, Width);\r | |
881 | }\r | |
882 | \r | |
883 | return Buffer;\r | |
884 | }\r | |
885 | \r | |
886 | /**\r | |
887 | Send the packet without data to HOST.\r | |
888 | \r | |
889 | @param[in] CommandType Type of Command.\r | |
b422b62c | 890 | @param[in] SequenceNo Sequence number.\r |
18b144ea | 891 | \r |
892 | **/\r | |
893 | VOID\r | |
93c0bdec | 894 | SendPacketWithoutData (\r |
b422b62c | 895 | IN UINT8 CommandType,\r |
896 | IN UINT8 SequenceNo\r | |
18b144ea | 897 | )\r |
898 | {\r | |
93c0bdec | 899 | DEBUG_PACKET_HEADER DebugHeader;\r |
18b144ea | 900 | DEBUG_PORT_HANDLE Handle;\r |
901 | \r | |
902 | Handle = GetDebugPortHandle();\r | |
903 | \r | |
93c0bdec | 904 | DebugHeader.StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;\r |
905 | DebugHeader.Command = CommandType;\r | |
906 | DebugHeader.Length = sizeof (DEBUG_PACKET_HEADER);\r | |
b422b62c | 907 | DebugHeader.SequenceNo = SequenceNo;\r |
908 | DebugHeader.Crc = 0;\r | |
909 | DebugHeader.Crc = CalculateCrc16 ((UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0);\r | |
18b144ea | 910 | \r |
93c0bdec | 911 | DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) &DebugHeader, DebugHeader.Length);\r |
912 | DebugPortWriteBuffer (Handle, (UINT8 *) &DebugHeader, DebugHeader.Length);\r | |
913 | }\r | |
914 | \r | |
915 | /**\r | |
916 | Send acknowledge packet to HOST.\r | |
917 | \r | |
918 | @param[in] AckCommand Type of Acknowledge packet.\r | |
919 | \r | |
920 | **/\r | |
921 | VOID\r | |
922 | SendAckPacket (\r | |
923 | IN UINT8 AckCommand\r | |
924 | )\r | |
925 | {\r | |
b422b62c | 926 | UINT8 SequenceNo;\r |
927 | DEBUG_AGENT_MAILBOX *Mailbox;\r | |
928 | \r | |
93c0bdec | 929 | if (AckCommand != DEBUG_COMMAND_OK) {\r |
b422b62c | 930 | //\r |
931 | // This is not ACK OK packet\r | |
932 | //\r | |
93c0bdec | 933 | DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Send ACK(%d)\n", AckCommand);\r |
934 | }\r | |
b422b62c | 935 | Mailbox = GetMailboxPointer();\r |
936 | SequenceNo = Mailbox->HostSequenceNo;\r | |
937 | DebugAgentMsgPrint (DEBUG_AGENT_INFO, "SendAckPacket: SequenceNo = %x\n", SequenceNo);\r | |
938 | SendPacketWithoutData (AckCommand, SequenceNo);\r | |
939 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_LAST_ACK, AckCommand);\r | |
18b144ea | 940 | }\r |
941 | \r | |
942 | /**\r | |
b422b62c | 943 | Receive valid packet from HOST.\r |
18b144ea | 944 | \r |
b422b62c | 945 | @param[out] InputPacket Buffer to receive packet.\r |
946 | @param[out] BreakReceived TRUE means break-in symbol received.\r | |
947 | FALSE means break-in symbol not received.\r | |
948 | @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return\r | |
949 | TRUE: Compatible packet received.\r | |
950 | FALSE: Incompatible packet received.\r | |
951 | @param[in] Timeout Time out value to wait for acknowlege from HOST.\r | |
952 | The unit is microsecond.\r | |
953 | @param[in] SkipStartSymbol TRUE: Skip time out when reading start symbol.\r | |
954 | FALSE: Does not Skip time out when reading start symbol.\r | |
18b144ea | 955 | \r |
b422b62c | 956 | @retval RETURN_SUCCESS A valid package was reveived in InputPacket.\r |
957 | @retval RETURN_TIMEOUT Timeout occurs.\r | |
18b144ea | 958 | \r |
959 | **/\r | |
960 | RETURN_STATUS\r | |
b422b62c | 961 | ReceivePacket (\r |
962 | OUT UINT8 *InputPacket,\r | |
963 | OUT BOOLEAN *BreakReceived,\r | |
964 | OUT BOOLEAN *IncompatibilityFlag, OPTIONAL\r | |
965 | IN UINTN Timeout,\r | |
966 | IN BOOLEAN SkipStartSymbol\r | |
18b144ea | 967 | )\r |
968 | {\r | |
b422b62c | 969 | DEBUG_PACKET_HEADER *DebugHeader;\r |
970 | UINTN Received;\r | |
971 | DEBUG_PORT_HANDLE Handle;\r | |
972 | UINT16 Crc;\r | |
973 | UINTN TimeoutForStartSymbol;\r | |
18b144ea | 974 | \r |
975 | Handle = GetDebugPortHandle();\r | |
b422b62c | 976 | if (SkipStartSymbol) {\r |
977 | TimeoutForStartSymbol = 0;\r | |
978 | } else {\r | |
979 | TimeoutForStartSymbol = Timeout;\r | |
980 | }\r | |
981 | \r | |
982 | DebugHeader = (DEBUG_PACKET_HEADER *) InputPacket;\r | |
18b144ea | 983 | while (TRUE) {\r |
b422b62c | 984 | //\r |
985 | // Find the valid start symbol\r | |
986 | //\r | |
987 | Received = DebugPortReadBuffer (Handle, &DebugHeader->StartSymbol, sizeof (DebugHeader->StartSymbol), TimeoutForStartSymbol);\r | |
988 | if (Received < sizeof (DebugHeader->StartSymbol)) {\r | |
989 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "DebugPortReadBuffer(StartSymbol) timeout\n");\r | |
18b144ea | 990 | return RETURN_TIMEOUT;\r |
991 | }\r | |
b422b62c | 992 | \r |
993 | if (DebugHeader->StartSymbol != DEBUG_STARTING_SYMBOL_NORMAL) {\r | |
994 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Invalid start symbol received [%02x]\n", DebugHeader->StartSymbol);\r | |
995 | continue;\r | |
18b144ea | 996 | }\r |
18b144ea | 997 | \r |
93c0bdec | 998 | //\r |
b422b62c | 999 | // Read Package header till field Length\r |
93c0bdec | 1000 | //\r |
b422b62c | 1001 | Received = DebugPortReadBuffer (\r |
1002 | Handle,\r | |
1003 | &DebugHeader->Command,\r | |
1004 | OFFSET_OF (DEBUG_PACKET_HEADER, Length) + sizeof (DebugHeader->Length) - sizeof (DebugHeader->StartSymbol),\r | |
1005 | Timeout\r | |
1006 | );\r | |
1007 | if (Received == 0) {\r | |
1008 | DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "DebugPortReadBuffer(Command) timeout\n");\r | |
93c0bdec | 1009 | return RETURN_TIMEOUT;\r |
1010 | }\r | |
b422b62c | 1011 | if (DebugHeader->Length < sizeof (DEBUG_PACKET_HEADER)) {\r |
1012 | if (IncompatibilityFlag != NULL) {\r | |
1013 | //\r | |
1014 | // This is one old version debug packet format, set Incompatibility flag\r | |
1015 | //\r | |
1016 | *IncompatibilityFlag = TRUE;\r | |
1017 | } else {\r | |
1018 | //\r | |
1019 | // Skip the bad small packet\r | |
1020 | //\r | |
1021 | continue;\r | |
1022 | }\r | |
1023 | } else {\r | |
1024 | //\r | |
1025 | // Read the payload data include the CRC field\r | |
1026 | //\r | |
1027 | Received = DebugPortReadBuffer (Handle, &DebugHeader->SequenceNo, (UINT8) (DebugHeader->Length - OFFSET_OF (DEBUG_PACKET_HEADER, SequenceNo)), Timeout);\r | |
1028 | if (Received == 0) {\r | |
1029 | DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "DebugPortReadBuffer(SequenceNo) timeout\n");\r | |
1030 | return RETURN_TIMEOUT;\r | |
1031 | }\r | |
1032 | //\r | |
1033 | // Calculate the CRC of Debug Packet\r | |
1034 | //\r | |
1035 | Crc = DebugHeader->Crc;\r | |
1036 | DebugHeader->Crc = 0;\r | |
1037 | if (Crc == CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0)) {\r | |
1038 | break;\r | |
1039 | }\r | |
1040 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "CRC Error (received CRC is %x)\n", Crc);\r | |
1041 | DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *) DebugHeader, DebugHeader->Length);\r | |
1042 | }\r | |
93c0bdec | 1043 | }\r |
1044 | \r | |
b422b62c | 1045 | DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *) DebugHeader, DebugHeader->Length);\r |
1046 | \r | |
18b144ea | 1047 | return RETURN_SUCCESS;\r |
1048 | }\r | |
1049 | \r | |
1050 | /**\r | |
1051 | Receive acknowledge packet OK from HOST in specified time.\r | |
1052 | \r | |
b422b62c | 1053 | @param[in] Command The command type issued by TARGET.\r |
1054 | @param[in] Timeout Time out value to wait for acknowlege from HOST.\r | |
1055 | The unit is microsecond.\r | |
1056 | @param[out] BreakReceived If BreakReceived is not NULL,\r | |
1057 | TRUE is retured if break-in symbol received.\r | |
1058 | FALSE is retured if break-in symbol not received.\r | |
1059 | @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return\r | |
1060 | TRUE: Compatible packet received.\r | |
1061 | FALSE: Incompatible packet received.\r | |
18b144ea | 1062 | \r |
1063 | @retval RETRUEN_SUCCESS Succeed to receive acknowlege packet from HOST,\r | |
1064 | the type of acknowlege packet saved in Ack.\r | |
1065 | @retval RETURN_TIMEOUT Specified timeout value was up.\r | |
1066 | \r | |
1067 | **/\r | |
1068 | RETURN_STATUS\r | |
93c0bdec | 1069 | SendCommandAndWaitForAckOK (\r |
1070 | IN UINT8 Command,\r | |
1071 | IN UINTN Timeout,\r | |
1072 | OUT BOOLEAN *BreakReceived, OPTIONAL\r | |
b422b62c | 1073 | OUT BOOLEAN *IncompatibilityFlag OPTIONAL\r |
18b144ea | 1074 | )\r |
1075 | {\r | |
93c0bdec | 1076 | RETURN_STATUS Status;\r |
b422b62c | 1077 | UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];\r |
1078 | DEBUG_PACKET_HEADER *DebugHeader;\r | |
1079 | UINT8 SequenceNo;\r | |
1080 | UINT8 HostSequenceNo;\r | |
1081 | UINT8 RetryCount;\r | |
1082 | \r | |
1083 | RetryCount = 3;\r | |
1084 | DebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;\r | |
1085 | Status = RETURN_TIMEOUT;\r | |
1086 | while (RetryCount > 0) {\r | |
1087 | SequenceNo = GetMailboxPointer()->SequenceNo;\r | |
1088 | HostSequenceNo = GetMailboxPointer()->HostSequenceNo;\r | |
1089 | SendPacketWithoutData (Command, SequenceNo);\r | |
1090 | Status = ReceivePacket ((UINT8 *) DebugHeader, BreakReceived, IncompatibilityFlag, Timeout, FALSE);\r | |
1091 | if (Status == RETURN_TIMEOUT) {\r | |
1092 | if (Command == DEBUG_COMMAND_INIT_BREAK) {\r | |
1093 | RetryCount--;\r | |
1094 | } else {\r | |
1095 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout when waiting for ACK packet.\n");\r | |
1096 | }\r | |
93c0bdec | 1097 | continue;\r |
1098 | }\r | |
b422b62c | 1099 | ASSERT_EFI_ERROR (Status);\r |
93c0bdec | 1100 | //\r |
b422b62c | 1101 | // Status == RETURN_SUCCESS\r |
93c0bdec | 1102 | //\r |
b422b62c | 1103 | if (DebugHeader->Command == DEBUG_COMMAND_OK && DebugHeader->SequenceNo == SequenceNo) {\r |
1104 | //\r | |
1105 | // Received Ack OK\r | |
1106 | //\r | |
1107 | UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_SEQUENCE_NO_INDEX, ++SequenceNo);\r | |
1108 | return Status;\r | |
18b144ea | 1109 | }\r |
b422b62c | 1110 | if (DebugHeader->Command == DEBUG_COMMAND_GO && (DebugHeader->SequenceNo == HostSequenceNo || Command == DEBUG_COMMAND_INIT_BREAK)) {\r |
1111 | //\r | |
1112 | // Received Old GO\r | |
1113 | //\r | |
1114 | if (Command == DEBUG_COMMAND_INIT_BREAK) {\r | |
1115 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive GO() in last boot\n");\r | |
1116 | }\r | |
1117 | SendPacketWithoutData (DEBUG_COMMAND_OK, DebugHeader->SequenceNo);\r | |
93c0bdec | 1118 | }\r |
18b144ea | 1119 | }\r |
1120 | \r | |
b422b62c | 1121 | ASSERT (Command == DEBUG_COMMAND_INIT_BREAK);\r |
1122 | return Status;\r | |
18b144ea | 1123 | }\r |
1124 | \r | |
1125 | /**\r | |
1126 | Get current break cause.\r | |
1127 | \r | |
1128 | @param[in] Vector Vector value of exception or interrupt.\r | |
1129 | @param[in] CpuContext Pointer to save CPU context.\r | |
1130 | \r | |
1131 | @return The type of break cause defined by XXXX\r | |
1132 | \r | |
1133 | **/\r | |
1134 | UINT8\r | |
1135 | GetBreakCause (\r | |
1136 | IN UINTN Vector,\r | |
1137 | IN DEBUG_CPU_CONTEXT *CpuContext\r | |
1138 | )\r | |
1139 | {\r | |
1140 | UINT8 Cause;\r | |
1141 | \r | |
1142 | Cause = DEBUG_DATA_BREAK_CAUSE_UNKNOWN;\r | |
1143 | \r | |
1144 | switch (Vector) {\r | |
1145 | case DEBUG_INT1_VECTOR:\r | |
1146 | case DEBUG_INT3_VECTOR:\r | |
1147 | \r | |
1148 | if (Vector == DEBUG_INT1_VECTOR) {\r | |
1149 | //\r | |
1150 | // INT 1\r | |
1151 | //\r | |
1152 | if ((CpuContext->Dr6 & BIT14) != 0) {\r | |
1153 | Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;\r | |
0a16169b | 1154 | //\r |
1155 | // If it's single step, no need to check DR0, to ensure single step work in PeCoffExtraActionLib\r | |
1156 | // (right after triggering a breakpoint to report image load/unload).\r | |
1157 | //\r | |
1158 | return Cause;\r | |
18b144ea | 1159 | \r |
1160 | } else {\r | |
1161 | Cause = DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT;\r | |
1162 | }\r | |
1163 | } else {\r | |
1164 | //\r | |
1165 | // INT 3\r | |
1166 | //\r | |
1167 | Cause = DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT;\r | |
1168 | }\r | |
1169 | \r | |
1170 | switch (CpuContext->Dr0) {\r | |
1171 | case IMAGE_LOAD_SIGNATURE:\r | |
1172 | case IMAGE_UNLOAD_SIGNATURE:\r | |
1173 | \r | |
1174 | if (CpuContext->Dr3 == IO_PORT_BREAKPOINT_ADDRESS) {\r | |
1175 | \r | |
1176 | Cause = (UINT8) ((CpuContext->Dr0 == IMAGE_LOAD_SIGNATURE) ? \r | |
1177 | DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD : DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD);\r | |
1178 | }\r | |
1179 | break;\r | |
1180 | \r | |
1181 | case SOFT_INTERRUPT_SIGNATURE:\r | |
1182 | \r | |
1183 | if (CpuContext->Dr1 == MEMORY_READY_SIGNATURE) {\r | |
1184 | Cause = DEBUG_DATA_BREAK_CAUSE_MEMORY_READY;\r | |
1185 | CpuContext->Dr0 = 0;\r | |
1186 | } else if (CpuContext->Dr1 == SYSTEM_RESET_SIGNATURE) {\r | |
1187 | Cause = DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET;\r | |
1188 | CpuContext->Dr0 = 0;\r | |
1189 | }\r | |
1190 | break;\r | |
1191 | \r | |
1192 | default:\r | |
1193 | break;\r | |
1194 | \r | |
1195 | }\r | |
1196 | \r | |
1197 | break;\r | |
1198 | \r | |
1199 | case DEBUG_TIMER_VECTOR:\r | |
1200 | Cause = DEBUG_DATA_BREAK_CAUSE_USER_HALT;\r | |
1201 | break;\r | |
1202 | \r | |
1203 | default:\r | |
1204 | if (Vector < 20) {\r | |
b422b62c | 1205 | if (GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) == 1) {\r |
93c0bdec | 1206 | //\r |
1207 | // If stepping command is executing\r | |
1208 | //\r | |
1209 | Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;\r | |
1210 | } else {\r | |
1211 | Cause = DEBUG_DATA_BREAK_CAUSE_EXCEPTION;\r | |
1212 | }\r | |
18b144ea | 1213 | }\r |
1214 | break;\r | |
1215 | }\r | |
1216 | \r | |
1217 | return Cause;\r | |
1218 | }\r | |
1219 | \r | |
1220 | /**\r | |
845a7fe0 | 1221 | Copy memory from source to destination with specified width.\r |
1222 | \r | |
1223 | @param[out] Dest A pointer to the destination buffer of the memory copy.\r | |
1224 | @param[in] Src A pointer to the source buffer of the memory copy.\r | |
1225 | @param[in] Count The number of data with specified width to copy from source to destination.\r | |
1226 | @param[in] Width Data width in byte.\r | |
1227 | \r | |
1228 | **/\r | |
1229 | VOID\r | |
1230 | CopyMemByWidth (\r | |
1231 | OUT UINT8 *Dest,\r | |
1232 | IN UINT8 *Src,\r | |
1233 | IN UINT16 Count,\r | |
1234 | IN UINT8 Width\r | |
1235 | )\r | |
1236 | {\r | |
1237 | UINT8 *Destination;\r | |
1238 | UINT8 *Source;\r | |
1239 | INT8 Step;\r | |
1240 | \r | |
1241 | if (Src > Dest) {\r | |
1242 | Destination = Dest;\r | |
1243 | Source = Src;\r | |
1244 | Step = Width;\r | |
1245 | } else {\r | |
1246 | //\r | |
1247 | // Copy memory from tail to avoid memory overlap\r | |
1248 | //\r | |
1249 | Destination = Dest + (Count - 1) * Width;\r | |
1250 | Source = Src + (Count - 1) * Width;\r | |
1251 | Step = -Width;\r | |
1252 | }\r | |
1253 | \r | |
1254 | while (Count-- != 0) {\r | |
1255 | switch (Width) {\r | |
1256 | case 1:\r | |
1257 | *(UINT8 *) Destination = MmioRead8 ((UINTN) Source);\r | |
1258 | break;\r | |
1259 | case 2:\r | |
1260 | *(UINT16 *) Destination = MmioRead16 ((UINTN) Source);\r | |
1261 | break;\r | |
1262 | case 4:\r | |
1263 | *(UINT32 *) Destination = MmioRead32 ((UINTN) Source);\r | |
1264 | break;\r | |
1265 | case 8:\r | |
1266 | *(UINT64 *) Destination = MmioRead64 ((UINTN) Source);\r | |
1267 | break;\r | |
1268 | default:\r | |
1269 | ASSERT (FALSE);\r | |
1270 | }\r | |
1271 | Source += Step;\r | |
1272 | Destination += Step;\r | |
1273 | }\r | |
1274 | }\r | |
1275 | \r | |
1276 | /**\r | |
1277 | Read memory with speicifed width and send packet with response data to HOST.\r | |
18b144ea | 1278 | \r |
18b144ea | 1279 | @param[in] Data Pointer to response data buffer.\r |
845a7fe0 | 1280 | @param[in] Count The number of data with specified Width.\r |
1281 | @param[in] Width Data width in byte.\r | |
18b144ea | 1282 | \r |
1283 | @retval RETURN_SUCCESS Response data was sent successfully.\r | |
18b144ea | 1284 | \r |
1285 | **/\r | |
1286 | RETURN_STATUS\r | |
845a7fe0 | 1287 | ReadMemoryAndSendResponsePacket (\r |
18b144ea | 1288 | IN UINT8 *Data,\r |
845a7fe0 | 1289 | IN UINT16 Count,\r |
1290 | IN UINT8 Width\r | |
18b144ea | 1291 | )\r |
1292 | {\r | |
b422b62c | 1293 | RETURN_STATUS Status;\r |
93c0bdec | 1294 | DEBUG_PACKET_HEADER *DebugHeader;\r |
18b144ea | 1295 | BOOLEAN LastPacket;\r |
b422b62c | 1296 | DEBUG_PACKET_HEADER *AckDebugHeader;\r |
845a7fe0 | 1297 | UINT8 DebugPacket[DEBUG_DATA_UPPER_LIMIT + sizeof (UINT64) - 1];\r |
b422b62c | 1298 | UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];\r |
18b144ea | 1299 | DEBUG_PORT_HANDLE Handle;\r |
b422b62c | 1300 | UINT8 SequenceNo;\r |
845a7fe0 | 1301 | UINTN RemainingDataSize;\r |
1302 | UINTN CurrentDataSize;\r | |
18b144ea | 1303 | \r |
1304 | Handle = GetDebugPortHandle();\r | |
1305 | \r | |
845a7fe0 | 1306 | //\r |
1307 | // Data is appended end of Debug Packet header, make sure data address\r | |
1308 | // in Debug Packet 8-byte alignment always\r | |
1309 | //\r | |
1310 | DebugHeader = (DEBUG_PACKET_HEADER *) (ALIGN_VALUE ((UINTN)&DebugPacket + sizeof (DEBUG_PACKET_HEADER), sizeof (UINT64))\r | |
1311 | - sizeof (DEBUG_PACKET_HEADER));\r | |
93c0bdec | 1312 | DebugHeader->StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;\r |
18b144ea | 1313 | \r |
845a7fe0 | 1314 | RemainingDataSize = Count * Width;\r |
18b144ea | 1315 | while (TRUE) {\r |
b422b62c | 1316 | SequenceNo = GetMailboxPointer()->HostSequenceNo;\r |
845a7fe0 | 1317 | if (RemainingDataSize <= DEBUG_DATA_MAXIMUM_REAL_DATA) {\r |
1318 | //\r | |
1319 | // If the remaining data is less one real packet size, this is the last data packet\r | |
1320 | //\r | |
1321 | CurrentDataSize = RemainingDataSize;\r | |
18b144ea | 1322 | LastPacket = TRUE;\r |
845a7fe0 | 1323 | DebugHeader->Command = DEBUG_COMMAND_OK;\r |
18b144ea | 1324 | } else {\r |
845a7fe0 | 1325 | //\r |
1326 | // Data is too larger to be sent in one packet, calculate the actual data size could\r | |
1327 | // be sent in one Maximum data packet\r | |
1328 | //\r | |
1329 | CurrentDataSize = (DEBUG_DATA_MAXIMUM_REAL_DATA / Width) * Width;\r | |
18b144ea | 1330 | LastPacket = FALSE;\r |
845a7fe0 | 1331 | DebugHeader->Command = DEBUG_COMMAND_IN_PROGRESS;\r |
18b144ea | 1332 | }\r |
93c0bdec | 1333 | //\r |
845a7fe0 | 1334 | // Construct the rest Debug header\r |
1335 | //\r | |
1336 | DebugHeader->Length = (UINT8)(CurrentDataSize + sizeof (DEBUG_PACKET_HEADER));\r | |
1337 | DebugHeader->SequenceNo = SequenceNo;\r | |
1338 | DebugHeader->Crc = 0;\r | |
1339 | CopyMemByWidth ((UINT8 *)(DebugHeader + 1), Data, (UINT16) CurrentDataSize / Width, Width);\r | |
1340 | //\r | |
1341 | // Calculate and fill the checksum, DebugHeader->Crc should be 0 before invoking CalculateCrc16 ()\r | |
93c0bdec | 1342 | //\r |
b422b62c | 1343 | DebugHeader->Crc = CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0);\r |
93c0bdec | 1344 | \r |
1345 | DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) DebugHeader, DebugHeader->Length);\r | |
1346 | \r | |
1347 | DebugPortWriteBuffer (Handle, (UINT8 *) DebugHeader, DebugHeader->Length);\r | |
18b144ea | 1348 | \r |
b422b62c | 1349 | while (TRUE) {\r |
1350 | Status = ReceivePacket (InputPacketBuffer, NULL, NULL, READ_PACKET_TIMEOUT, FALSE);\r | |
1351 | if (Status == RETURN_TIMEOUT) {\r | |
1352 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout in SendDataResponsePacket()\n");\r | |
1353 | break;\r | |
1354 | }\r | |
1355 | AckDebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;\r | |
1356 | SequenceNo = AckDebugHeader->SequenceNo;\r | |
1357 | if (AckDebugHeader->Command == DEBUG_COMMAND_OK &&\r | |
1358 | SequenceNo == DebugHeader->SequenceNo &&\r | |
1359 | LastPacket) {\r | |
18b144ea | 1360 | //\r |
1361 | // If this is the last packet, return RETURN_SUCCESS.\r | |
1362 | //\r | |
1363 | return RETURN_SUCCESS;\r | |
18b144ea | 1364 | }\r |
b422b62c | 1365 | if ((SequenceNo == (UINT8) (DebugHeader->SequenceNo + 1)) && (AckDebugHeader->Command == DEBUG_COMMAND_CONTINUE)) {\r |
1366 | //\r | |
845a7fe0 | 1367 | // Calculate the rest data size\r |
b422b62c | 1368 | //\r |
845a7fe0 | 1369 | Data += CurrentDataSize;\r |
1370 | RemainingDataSize -= CurrentDataSize;\r | |
b422b62c | 1371 | UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) SequenceNo);\r |
1372 | break;\r | |
1373 | }\r | |
1374 | if (SequenceNo >= DebugHeader->SequenceNo) {\r | |
1375 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Received one old or new command(SequenceNo is %x, last SequenceNo is %x)\n", SequenceNo, DebugHeader->SequenceNo);\r | |
1376 | break;\r | |
1377 | }\r | |
18b144ea | 1378 | }\r |
1379 | }\r | |
1380 | }\r | |
1381 | \r | |
845a7fe0 | 1382 | /**\r |
1383 | Send packet with response data to HOST.\r | |
1384 | \r | |
1385 | @param[in] Data Pointer to response data buffer.\r | |
1386 | @param[in] DataSize Size of response data in byte.\r | |
1387 | \r | |
1388 | @retval RETURN_SUCCESS Response data was sent successfully.\r | |
1389 | \r | |
1390 | **/\r | |
1391 | RETURN_STATUS\r | |
1392 | SendDataResponsePacket (\r | |
1393 | IN UINT8 *Data,\r | |
1394 | IN UINT16 DataSize\r | |
1395 | )\r | |
1396 | {\r | |
1397 | return ReadMemoryAndSendResponsePacket (Data, DataSize, 1);\r | |
1398 | }\r | |
1399 | \r | |
18b144ea | 1400 | /**\r |
1401 | Send break cause packet to HOST.\r | |
1402 | \r | |
1403 | @param[in] Vector Vector value of exception or interrutp.\r | |
1404 | @param[in] CpuContext Pointer to save CPU context.\r | |
1405 | \r | |
1406 | @retval RETURN_SUCCESS Response data was sent successfully.\r | |
1407 | @retval RETURN_DEVICE_ERROR Cannot receive DEBUG_COMMAND_OK from HOST.\r | |
1408 | \r | |
1409 | **/\r | |
1410 | RETURN_STATUS\r | |
1411 | SendBreakCausePacket (\r | |
1412 | IN UINTN Vector,\r | |
1413 | IN DEBUG_CPU_CONTEXT *CpuContext\r | |
1414 | )\r | |
1415 | {\r | |
1416 | DEBUG_DATA_RESPONSE_BREAK_CAUSE DebugDataBreakCause;\r | |
1417 | \r | |
1418 | DebugDataBreakCause.StopAddress = CpuContext->Eip;\r | |
1419 | DebugDataBreakCause.Cause = GetBreakCause (Vector, CpuContext);\r | |
1420 | \r | |
93c0bdec | 1421 | return SendDataResponsePacket ((UINT8 *) &DebugDataBreakCause, (UINT16) sizeof (DEBUG_DATA_RESPONSE_BREAK_CAUSE));\r |
18b144ea | 1422 | }\r |
1423 | \r | |
93c0bdec | 1424 | /**\r |
1425 | Try to attach the HOST.\r | |
1426 | \r | |
1427 | Send init break packet to HOST:\r | |
1428 | If no acknowlege received in specified Timeout, return RETURN_TIMEOUT. \r | |
1429 | If received acknowlege, check the revision of HOST. \r | |
1430 | Set Attach Flag if attach successfully. \r | |
1431 | \r | |
b422b62c | 1432 | @param[in] BreakCause Break cause of this break event.\r |
93c0bdec | 1433 | @param[in] Timeout Time out value to wait for acknowlege from HOST.\r |
1434 | The unit is microsecond.\r | |
1435 | @param[out] BreakReceived If BreakReceived is not NULL,\r | |
1436 | TRUE is retured if break-in symbol received.\r | |
1437 | FALSE is retured if break-in symbol not received.\r | |
1438 | **/\r | |
1439 | RETURN_STATUS\r | |
1440 | AttachHost (\r | |
b422b62c | 1441 | IN UINT8 BreakCause,\r |
93c0bdec | 1442 | IN UINTN Timeout,\r |
1443 | OUT BOOLEAN *BreakReceived\r | |
1444 | )\r | |
1445 | {\r | |
1446 | RETURN_STATUS Status;\r | |
1447 | DEBUG_PORT_HANDLE Handle;\r | |
b422b62c | 1448 | BOOLEAN IncompatibilityFlag;\r |
93c0bdec | 1449 | \r |
b422b62c | 1450 | IncompatibilityFlag = FALSE;\r |
93c0bdec | 1451 | Handle = GetDebugPortHandle();\r |
1452 | \r | |
1453 | //\r | |
1454 | // Send init break and wait ack in Timeout\r | |
1455 | //\r | |
1456 | DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgSendInitPacket, AsciiStrLen (mErrorMsgSendInitPacket));\r | |
b422b62c | 1457 | if (BreakCause == DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET) {\r |
1458 | Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_INIT_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);\r | |
1459 | } else {\r | |
1460 | Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_ATTACH_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);\r | |
93c0bdec | 1461 | }\r |
b422b62c | 1462 | if (IncompatibilityFlag) {\r |
93c0bdec | 1463 | //\r |
b422b62c | 1464 | // If the incompatible Debug Packet received, the HOST should be running transfer protocol before DEBUG_AGENT_REVISION.\r |
1465 | // It could be UDK Debugger for Windows v1.1/v1.2 or for Linux v0.8/v1.2.\r | |
93c0bdec | 1466 | //\r |
1467 | DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgVersionAlert, AsciiStrLen (mErrorMsgVersionAlert));\r | |
1468 | CpuDeadLoop ();\r | |
1469 | }\r | |
b422b62c | 1470 | \r |
1471 | if (RETURN_ERROR (Status)) {\r | |
1472 | DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectFail, AsciiStrLen (mErrorMsgConnectFail));\r | |
1473 | } else {\r | |
1474 | DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectOK, AsciiStrLen (mErrorMsgConnectOK));\r | |
1475 | //\r | |
1476 | // Set Attach flag\r | |
1477 | //\r | |
1478 | SetHostAttached (TRUE);\r | |
1479 | }\r | |
93c0bdec | 1480 | return Status;\r |
1481 | }\r | |
1482 | \r | |
1483 | /**\r | |
1484 | Send Break point packet to HOST. \r | |
1485 | \r | |
1486 | Only the first breaking processor could sent BREAK_POINT packet.\r | |
b422b62c | 1487 | \r |
1488 | @param[in] BreakCause Break cause of this break event.\r | |
93c0bdec | 1489 | @param[in] ProcessorIndex Processor index value.\r |
1490 | @param[out] BreakReceived If BreakReceived is not NULL,\r | |
1491 | TRUE is retured if break-in symbol received.\r | |
1492 | FALSE is retured if break-in symbol not received.\r | |
1493 | \r | |
1494 | **/\r | |
1495 | VOID\r | |
1496 | SendBreakPacketToHost (\r | |
b422b62c | 1497 | IN UINT8 BreakCause,\r |
93c0bdec | 1498 | IN UINT32 ProcessorIndex,\r |
1499 | OUT BOOLEAN *BreakReceived\r | |
1500 | )\r | |
1501 | {\r | |
1502 | UINT8 InputCharacter;\r | |
1503 | DEBUG_PORT_HANDLE Handle;\r | |
1504 | \r | |
1505 | Handle = GetDebugPortHandle();\r | |
1506 | \r | |
1507 | if (IsHostAttached ()) {\r | |
1508 | DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Send Break Packet to HOST.\n", ProcessorIndex);\r | |
b422b62c | 1509 | SendCommandAndWaitForAckOK (DEBUG_COMMAND_BREAK_POINT, READ_PACKET_TIMEOUT, BreakReceived, NULL);\r |
93c0bdec | 1510 | } else {\r |
1511 | DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Try to attach HOST.\n", ProcessorIndex);\r | |
1512 | //\r | |
1513 | // If HOST is not attached, try to attach it firstly.\r | |
1514 | //\r | |
1515 | //\r | |
1516 | // Poll Attach symbols from HOST and ack OK\r | |
1517 | // \r | |
1518 | do {\r | |
1519 | DebugPortReadBuffer (Handle, &InputCharacter, 1, 0);\r | |
1520 | } while (InputCharacter != DEBUG_STARTING_SYMBOL_ATTACH);\r | |
1521 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1522 | \r | |
1523 | //\r | |
1524 | // Try to attach HOST\r | |
1525 | //\r | |
b422b62c | 1526 | while (AttachHost (BreakCause, 0, NULL) != RETURN_SUCCESS);\r |
93c0bdec | 1527 | \r |
1528 | }\r | |
1529 | }\r | |
18b144ea | 1530 | \r |
1531 | /**\r | |
1532 | The main function to process communication with HOST.\r | |
1533 | \r | |
1534 | It received the command packet from HOST, and sent response data packet to HOST.\r | |
1535 | \r | |
1536 | @param[in] Vector Vector value of exception or interrutp.\r | |
1537 | @param[in, out] CpuContext Pointer to saved CPU context.\r | |
1538 | @param[in] BreakReceived TRUE means break-in symbol received.\r | |
1539 | FALSE means break-in symbol not received.\r | |
1540 | \r | |
1541 | **/\r | |
1542 | VOID\r | |
1543 | CommandCommunication (\r | |
1544 | IN UINTN Vector,\r | |
1545 | IN OUT DEBUG_CPU_CONTEXT *CpuContext,\r | |
1546 | IN BOOLEAN BreakReceived\r | |
1547 | )\r | |
1548 | {\r | |
93c0bdec | 1549 | RETURN_STATUS Status;\r |
845a7fe0 | 1550 | UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT + sizeof (UINT64) - 1];\r |
93c0bdec | 1551 | DEBUG_PACKET_HEADER *DebugHeader;\r |
1552 | UINT8 Width;\r | |
1553 | UINT8 Data8;\r | |
1554 | UINT32 Data32;\r | |
1555 | UINT64 Data64;\r | |
1556 | DEBUG_DATA_READ_MEMORY *MemoryRead;\r | |
1557 | DEBUG_DATA_WRITE_MEMORY *MemoryWrite;\r | |
1558 | DEBUG_DATA_READ_IO *IoRead;\r | |
1559 | DEBUG_DATA_WRITE_IO *IoWrite;\r | |
1560 | DEBUG_DATA_READ_REGISTER *RegisterRead;\r | |
1561 | DEBUG_DATA_WRITE_REGISTER *RegisterWrite;\r | |
1562 | UINT8 *RegisterBuffer;\r | |
1563 | DEBUG_DATA_READ_MSR *MsrRegisterRead;\r | |
1564 | DEBUG_DATA_WRITE_MSR *MsrRegisterWrite;\r | |
1565 | DEBUG_DATA_CPUID *Cpuid;\r | |
1566 | DEBUG_DATA_RESPONSE_CPUID CpuidResponse;\r | |
1567 | DEBUG_DATA_SEARCH_SIGNATURE *SearchSignature;\r | |
1568 | DEBUG_DATA_RESPONSE_GET_EXCEPTION Exception;\r | |
1569 | DEBUG_DATA_RESPONSE_GET_REVISION DebugAgentRevision;\r | |
1570 | DEBUG_DATA_SET_VIEWPOINT *SetViewPoint;\r | |
1571 | BOOLEAN HaltDeferred;\r | |
1572 | UINT32 ProcessorIndex;\r | |
1573 | DEBUG_PORT_HANDLE Handle;\r | |
1574 | DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer;\r | |
1575 | UINT32 IssuedViewPoint;\r | |
b422b62c | 1576 | DEBUG_AGENT_MAILBOX *Mailbox;\r |
845a7fe0 | 1577 | UINT8 *AlignedDataPtr;\r |
18b144ea | 1578 | \r |
1579 | ProcessorIndex = 0;\r | |
93c0bdec | 1580 | IssuedViewPoint = 0;\r |
1581 | HaltDeferred = BreakReceived;\r | |
18b144ea | 1582 | \r |
b422b62c | 1583 | if (MultiProcessorDebugSupport()) {\r |
18b144ea | 1584 | ProcessorIndex = GetProcessorIndex ();\r |
1585 | SetCpuStopFlagByIndex (ProcessorIndex, TRUE);\r | |
93c0bdec | 1586 | if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {\r |
1587 | //\r | |
1588 | // Only the current view processor could set AgentInProgress Flag. \r | |
1589 | //\r | |
1590 | IssuedViewPoint = ProcessorIndex;\r | |
1591 | }\r | |
18b144ea | 1592 | }\r |
1593 | \r | |
93c0bdec | 1594 | if (IssuedViewPoint == ProcessorIndex) {\r |
1595 | //\r | |
1596 | // Set AgentInProgress Flag.\r | |
1597 | //\r | |
b422b62c | 1598 | SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 1);\r |
93c0bdec | 1599 | } \r |
1600 | \r | |
1601 | Handle = GetDebugPortHandle();\r | |
1602 | \r | |
18b144ea | 1603 | while (TRUE) {\r |
1604 | \r | |
b422b62c | 1605 | if (MultiProcessorDebugSupport()) {\r |
93c0bdec | 1606 | //\r |
1607 | // Check if the current processor is HOST view point\r | |
1608 | //\r | |
18b144ea | 1609 | if (mDebugMpContext.ViewPointIndex != ProcessorIndex) {\r |
1610 | if (mDebugMpContext.RunCommandSet) {\r | |
93c0bdec | 1611 | //\r |
1612 | // If HOST view point sets RUN flag, run GO command to leave\r | |
1613 | //\r | |
18b144ea | 1614 | SetCpuStopFlagByIndex (ProcessorIndex, FALSE);\r |
1615 | CommandGo (CpuContext);\r | |
1616 | break;\r | |
1617 | } else {\r | |
93c0bdec | 1618 | //\r |
1619 | // Run into loop again\r | |
1620 | //\r | |
1621 | CpuPause ();\r | |
18b144ea | 1622 | continue;\r |
1623 | }\r | |
1624 | }\r | |
1625 | }\r | |
1626 | \r | |
b422b62c | 1627 | AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 1628 | \r |
b422b62c | 1629 | DebugHeader =(DEBUG_PACKET_HEADER *) InputPacketBuffer;\r |
18b144ea | 1630 | \r |
b422b62c | 1631 | DebugAgentMsgPrint (DEBUG_AGENT_INFO, "TARGET: Try to get command from HOST...\n");\r |
1632 | Status = ReceivePacket ((UINT8 *)DebugHeader, &BreakReceived, NULL, READ_PACKET_TIMEOUT, TRUE);\r | |
1633 | if (Status != RETURN_SUCCESS || (DebugHeader->Command & DEBUG_COMMAND_RESPONSE) != 0) {\r | |
1634 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command[%x] sequenceno[%x] returned status is [%x] \n", DebugHeader->Command, DebugHeader->SequenceNo, Status);\r | |
1635 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command failed or it's response packet not expected! \n");\r | |
1636 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r | |
1637 | continue;\r | |
18b144ea | 1638 | }\r |
1639 | \r | |
b422b62c | 1640 | Mailbox = GetMailboxPointer ();\r |
1641 | if (DebugHeader->SequenceNo == Mailbox->HostSequenceNo) {\r | |
1642 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive one old command[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);\r | |
1643 | SendAckPacket (Mailbox->LastAck);\r | |
1644 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r | |
1645 | continue;\r | |
1646 | } else if (DebugHeader->SequenceNo == (UINT8) (Mailbox->HostSequenceNo + 1)) {\r | |
1647 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) DebugHeader->SequenceNo);\r | |
1648 | } else {\r | |
1649 | DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Receive one invalid comamnd[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);\r | |
1650 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r | |
18b144ea | 1651 | continue;\r |
1652 | }\r | |
b422b62c | 1653 | \r |
93c0bdec | 1654 | //\r |
1655 | // Save CPU content before executing HOST commond\r | |
1656 | //\r | |
b422b62c | 1657 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX, (UINT64)(UINTN) &AgentExceptionBuffer.JumpBuffer);\r |
93c0bdec | 1658 | if (SetJump (&AgentExceptionBuffer.JumpBuffer) != 0) {\r |
1659 | //\r | |
1660 | // If HOST command failed, continue to wait for HOST's next command\r | |
1661 | // If needed, agent could send exception info to HOST.\r | |
1662 | //\r | |
1663 | SendAckPacket (DEBUG_COMMAND_ABORT);\r | |
b422b62c | 1664 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
93c0bdec | 1665 | continue;\r |
1666 | }\r | |
1667 | \r | |
1668 | DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Processor[%x]:Received one command(%x)\n", mDebugMpContext.ViewPointIndex, DebugHeader->Command);\r | |
1669 | \r | |
18b144ea | 1670 | switch (DebugHeader->Command) {\r |
1671 | \r | |
b422b62c | 1672 | case DEBUG_COMMAND_HALT:\r |
1673 | SendAckPacket (DEBUG_COMMAND_HALT_DEFERRED);\r | |
1674 | HaltDeferred = TRUE;\r | |
1675 | BreakReceived = FALSE;\r | |
1676 | Status = RETURN_SUCCESS;\r | |
1677 | break;\r | |
1678 | \r | |
18b144ea | 1679 | case DEBUG_COMMAND_RESET:\r |
1680 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
b422b62c | 1681 | SendAckPacket (DEBUG_COMMAND_OK);\r |
1682 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1683 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r | |
18b144ea | 1684 | \r |
1685 | ResetCold ();\r | |
1686 | //\r | |
b4ebbaff RN |
1687 | // Assume system resets in 2 seconds, otherwise send TIMEOUT packet.\r |
1688 | // PCD can be used if 2 seconds isn't long enough for some platforms.\r | |
18b144ea | 1689 | //\r |
b4ebbaff | 1690 | MicroSecondDelay (2000000);\r |
b422b62c | 1691 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, Mailbox->HostSequenceNo + 1);\r |
1692 | SendAckPacket (DEBUG_COMMAND_TIMEOUT);\r | |
1693 | SendAckPacket (DEBUG_COMMAND_TIMEOUT);\r | |
b4ebbaff | 1694 | SendAckPacket (DEBUG_COMMAND_TIMEOUT);\r |
18b144ea | 1695 | break;\r |
1696 | \r | |
1697 | case DEBUG_COMMAND_GO:\r | |
1698 | CommandGo (CpuContext);\r | |
93c0bdec | 1699 | //\r |
1700 | // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO\r | |
1701 | // If HOST changed Dr0 before GO, we will not change Dr0 here\r | |
1702 | //\r | |
1703 | Data8 = GetBreakCause (Vector, CpuContext);\r | |
1704 | if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {\r | |
1705 | CpuContext->Dr0 = 0;\r | |
1706 | }\r | |
1707 | //\r | |
1708 | // Clear Stepping Flag\r | |
1709 | //\r | |
b422b62c | 1710 | SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0);\r |
93c0bdec | 1711 | \r |
18b144ea | 1712 | if (!HaltDeferred) {\r |
1713 | //\r | |
1714 | // If no HALT command received when being in-active mode\r | |
1715 | //\r | |
b422b62c | 1716 | if (MultiProcessorDebugSupport()) {\r |
93c0bdec | 1717 | Data32 = FindNextPendingBreakCpu ();\r |
18b144ea | 1718 | if (Data32 != -1) {\r |
1719 | //\r | |
1720 | // If there are still others processors being in break state, \r | |
1721 | // send OK packet to HOST to finish this go command\r | |
1722 | //\r | |
1723 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1724 | CpuPause ();\r | |
1725 | //\r | |
1726 | // Set current view to the next breaking processor\r | |
1727 | //\r | |
1728 | mDebugMpContext.ViewPointIndex = Data32;\r | |
1729 | mDebugMpContext.BreakAtCpuIndex = mDebugMpContext.ViewPointIndex;\r | |
1730 | SetCpuBreakFlagByIndex (mDebugMpContext.ViewPointIndex, FALSE);\r | |
1731 | //\r | |
93c0bdec | 1732 | // Send break packet to HOST to let HOST break again\r |
1733 | //\r | |
b422b62c | 1734 | SendBreakPacketToHost (DEBUG_DATA_BREAK_CAUSE_UNKNOWN, mDebugMpContext.BreakAtCpuIndex, &BreakReceived);\r |
93c0bdec | 1735 | //\r |
1736 | // Continue to run into loop to read command packet from HOST\r | |
18b144ea | 1737 | //\r |
b422b62c | 1738 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 1739 | break;\r |
1740 | }\r | |
1741 | \r | |
1742 | //\r | |
1743 | // If no else processor break, set stop bitmask,\r | |
1744 | // and set Running flag for all processors.\r | |
1745 | //\r | |
1746 | SetCpuStopFlagByIndex (ProcessorIndex, FALSE);\r | |
1747 | SetCpuRunningFlag (TRUE);\r | |
1748 | CpuPause ();\r | |
1749 | //\r | |
1750 | // Wait for all processors are in running state \r | |
1751 | //\r | |
1752 | while (TRUE) {\r | |
1753 | if (IsAllCpuRunning ()) {\r | |
1754 | break;\r | |
1755 | }\r | |
1756 | }\r | |
1757 | //\r | |
1758 | // Set BSP to be current view point.\r | |
1759 | //\r | |
1760 | SetDebugViewPoint (mDebugMpContext.BspIndex);\r | |
1761 | CpuPause ();\r | |
1762 | //\r | |
1763 | // Clear breaking processor index and running flag\r | |
1764 | //\r | |
1765 | mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);\r | |
1766 | SetCpuRunningFlag (FALSE);\r | |
1767 | }\r | |
1768 | \r | |
1769 | //\r | |
1770 | // Send OK packet to HOST to finish this go command\r | |
1771 | //\r | |
1772 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1773 | \r | |
b422b62c | 1774 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 1775 | \r |
b422b62c | 1776 | if (!IsHostAttached()) {\r |
1777 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_SEQUENCE_NO_INDEX, 0);\r | |
1778 | UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, 0);\r | |
1779 | }\r | |
18b144ea | 1780 | return;\r |
1781 | \r | |
1782 | } else {\r | |
1783 | //\r | |
1784 | // If reveived HALT command, need to defer the GO command\r | |
1785 | //\r | |
1786 | SendAckPacket (DEBUG_COMMAND_HALT_PROCESSED);\r | |
1787 | HaltDeferred = FALSE;\r | |
18b144ea | 1788 | \r |
1789 | Vector = DEBUG_TIMER_VECTOR;\r | |
1790 | }\r | |
1791 | break;\r | |
1792 | \r | |
1793 | case DEBUG_COMMAND_BREAK_CAUSE:\r | |
1794 | \r | |
b422b62c | 1795 | if (MultiProcessorDebugSupport() && ProcessorIndex != mDebugMpContext.BreakAtCpuIndex) {\r |
18b144ea | 1796 | Status = SendBreakCausePacket (DEBUG_TIMER_VECTOR, CpuContext);\r |
1797 | \r | |
1798 | } else {\r | |
1799 | Status = SendBreakCausePacket (Vector, CpuContext);\r | |
1800 | }\r | |
1801 | \r | |
1802 | break;\r | |
1803 | \r | |
1804 | case DEBUG_COMMAND_SET_HW_BREAKPOINT:\r | |
1805 | SetDebugRegister (CpuContext, (DEBUG_DATA_SET_HW_BREAKPOINT *) (DebugHeader + 1));\r | |
1806 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1807 | break;\r | |
1808 | \r | |
1809 | case DEBUG_COMMAND_CLEAR_HW_BREAKPOINT:\r | |
1810 | ClearDebugRegister (CpuContext, (DEBUG_DATA_CLEAR_HW_BREAKPOINT *) (DebugHeader + 1));\r | |
1811 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1812 | break;\r | |
1813 | \r | |
1814 | case DEBUG_COMMAND_SINGLE_STEPPING:\r | |
1815 | CommandStepping (CpuContext);\r | |
93c0bdec | 1816 | //\r |
1817 | // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO\r | |
1818 | // If HOST changed Dr0 before GO, we will not change Dr0 here\r | |
1819 | //\r | |
1820 | Data8 = GetBreakCause (Vector, CpuContext);\r | |
1821 | if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {\r | |
1822 | CpuContext->Dr0 = 0;\r | |
1823 | }\r | |
18b144ea | 1824 | \r |
1825 | mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);\r | |
93c0bdec | 1826 | //\r |
1827 | // Set Stepping Flag\r | |
1828 | //\r | |
b422b62c | 1829 | SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1);\r |
1830 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r | |
18b144ea | 1831 | //\r |
93c0bdec | 1832 | // Executing stepping command directly without sending ACK packet,\r |
1833 | // ACK packet will be sent after stepping done.\r | |
18b144ea | 1834 | //\r |
1835 | return;\r | |
1836 | \r | |
1837 | case DEBUG_COMMAND_SET_SW_BREAKPOINT:\r | |
1838 | Data64 = (UINTN) (((DEBUG_DATA_SET_SW_BREAKPOINT *) (DebugHeader + 1))->Address);\r | |
1839 | Data8 = *(UINT8 *) (UINTN) Data64;\r | |
1840 | *(UINT8 *) (UINTN) Data64 = DEBUG_SW_BREAKPOINT_SYMBOL;\r | |
93c0bdec | 1841 | Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));\r |
18b144ea | 1842 | break;\r |
1843 | \r | |
93c0bdec | 1844 | case DEBUG_COMMAND_READ_MEMORY:\r |
1845 | MemoryRead = (DEBUG_DATA_READ_MEMORY *) (DebugHeader + 1);\r | |
845a7fe0 | 1846 | Status = ReadMemoryAndSendResponsePacket ((UINT8 *) (UINTN) MemoryRead->Address, MemoryRead->Count, MemoryRead->Width);\r |
18b144ea | 1847 | break;\r |
1848 | \r | |
93c0bdec | 1849 | case DEBUG_COMMAND_WRITE_MEMORY:\r |
1850 | MemoryWrite = (DEBUG_DATA_WRITE_MEMORY *) (DebugHeader + 1);\r | |
845a7fe0 | 1851 | //\r |
1852 | // Copy data into one memory with 8-byte alignment address\r | |
1853 | //\r | |
1854 | AlignedDataPtr = ALIGN_POINTER ((UINT8 *) &MemoryWrite->Data, sizeof (UINT64));\r | |
1855 | if (AlignedDataPtr != (UINT8 *) &MemoryWrite->Data) {\r | |
1856 | CopyMem (AlignedDataPtr, (UINT8 *) &MemoryWrite->Data, MemoryWrite->Count * MemoryWrite->Width);\r | |
1857 | }\r | |
1858 | CopyMemByWidth ((UINT8 *) (UINTN) MemoryWrite->Address, AlignedDataPtr, MemoryWrite->Count, MemoryWrite->Width);\r | |
18b144ea | 1859 | SendAckPacket (DEBUG_COMMAND_OK);\r |
1860 | break;\r | |
1861 | \r | |
1862 | case DEBUG_COMMAND_READ_IO:\r | |
1863 | IoRead = (DEBUG_DATA_READ_IO *) (DebugHeader + 1);\r | |
1864 | switch (IoRead->Width) {\r | |
1865 | case 1:\r | |
93c0bdec | 1866 | Data64 = IoRead8 ((UINTN) IoRead->Port);\r |
18b144ea | 1867 | break;\r |
1868 | case 2:\r | |
93c0bdec | 1869 | Data64 = IoRead16 ((UINTN) IoRead->Port);\r |
18b144ea | 1870 | break;\r |
1871 | case 4:\r | |
93c0bdec | 1872 | Data64 = IoRead32 ((UINTN) IoRead->Port);\r |
18b144ea | 1873 | break;\r |
1874 | case 8:\r | |
93c0bdec | 1875 | Data64 = IoRead64 ((UINTN) IoRead->Port);\r |
18b144ea | 1876 | break;\r |
1877 | default:\r | |
1878 | Data64 = (UINT64) -1;\r | |
1879 | }\r | |
93c0bdec | 1880 | Status = SendDataResponsePacket ((UINT8 *) &Data64, IoRead->Width);\r |
18b144ea | 1881 | break;\r |
1882 | \r | |
1883 | case DEBUG_COMMAND_WRITE_IO:\r | |
1884 | IoWrite = (DEBUG_DATA_WRITE_IO *) (DebugHeader + 1);\r | |
1885 | switch (IoWrite->Width) {\r | |
1886 | case 1:\r | |
93c0bdec | 1887 | Data64 = IoWrite8 ((UINTN) IoWrite->Port, *(UINT8 *) &IoWrite->Data);\r |
18b144ea | 1888 | break;\r |
1889 | case 2:\r | |
93c0bdec | 1890 | Data64 = IoWrite16 ((UINTN) IoWrite->Port, *(UINT16 *) &IoWrite->Data);\r |
18b144ea | 1891 | break;\r |
1892 | case 4:\r | |
93c0bdec | 1893 | Data64 = IoWrite32 ((UINTN) IoWrite->Port, *(UINT32 *) &IoWrite->Data);\r |
18b144ea | 1894 | break;\r |
1895 | case 8:\r | |
93c0bdec | 1896 | Data64 = IoWrite64 ((UINTN) IoWrite->Port, *(UINT64 *) &IoWrite->Data);\r |
18b144ea | 1897 | break;\r |
1898 | default:\r | |
1899 | Data64 = (UINT64) -1;\r | |
1900 | }\r | |
1901 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1902 | break;\r | |
1903 | \r | |
93c0bdec | 1904 | case DEBUG_COMMAND_READ_ALL_REGISTERS:\r |
1905 | Status = SendDataResponsePacket ((UINT8 *) CpuContext, sizeof (*CpuContext));\r | |
1906 | break;\r | |
1907 | \r | |
18b144ea | 1908 | case DEBUG_COMMAND_READ_REGISTER:\r |
1909 | RegisterRead = (DEBUG_DATA_READ_REGISTER *) (DebugHeader + 1);\r | |
1910 | \r | |
93c0bdec | 1911 | if (RegisterRead->Index <= SOFT_DEBUGGER_REGISTER_MAX) {\r |
1912 | RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterRead->Index, &Width);\r | |
1913 | Status = SendDataResponsePacket (RegisterBuffer, Width);\r | |
18b144ea | 1914 | } else {\r |
93c0bdec | 1915 | Status = RETURN_UNSUPPORTED;\r |
18b144ea | 1916 | }\r |
1917 | break;\r | |
1918 | \r | |
1919 | case DEBUG_COMMAND_WRITE_REGISTER:\r | |
1920 | RegisterWrite = (DEBUG_DATA_WRITE_REGISTER *) (DebugHeader + 1);\r | |
93c0bdec | 1921 | if (RegisterWrite->Index <= SOFT_DEBUGGER_REGISTER_MAX) {\r |
1922 | RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterWrite->Index, &Width);\r | |
1923 | ASSERT (Width == RegisterWrite->Length);\r | |
1924 | CopyMem (RegisterBuffer, RegisterWrite->Data, Width);\r | |
1925 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1926 | } else {\r | |
1927 | Status = RETURN_UNSUPPORTED;\r | |
1928 | }\r | |
18b144ea | 1929 | break;\r |
1930 | \r | |
1931 | case DEBUG_COMMAND_ARCH_MODE:\r | |
1932 | Data8 = DEBUG_ARCH_SYMBOL;\r | |
93c0bdec | 1933 | Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));\r |
18b144ea | 1934 | break;\r |
1935 | \r | |
1936 | case DEBUG_COMMAND_READ_MSR:\r | |
1937 | MsrRegisterRead = (DEBUG_DATA_READ_MSR *) (DebugHeader + 1);\r | |
1938 | Data64 = AsmReadMsr64 (MsrRegisterRead->Index);\r | |
93c0bdec | 1939 | Status = SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (UINT64));\r |
18b144ea | 1940 | break;\r |
1941 | \r | |
1942 | case DEBUG_COMMAND_WRITE_MSR:\r | |
1943 | MsrRegisterWrite = (DEBUG_DATA_WRITE_MSR *) (DebugHeader + 1);\r | |
1944 | AsmWriteMsr64 (MsrRegisterWrite->Index, MsrRegisterWrite->Value);\r | |
1945 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1946 | break;\r | |
1947 | \r | |
93c0bdec | 1948 | case DEBUG_COMMAND_SET_DEBUG_SETTING:\r |
1949 | Status = SetDebugSetting ((DEBUG_DATA_SET_DEBUG_SETTING *)(DebugHeader + 1));\r | |
1950 | if (Status == RETURN_SUCCESS) {\r | |
1951 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
1952 | }\r | |
18b144ea | 1953 | break;\r |
1954 | \r | |
1955 | case DEBUG_COMMAND_GET_REVISION:\r | |
1956 | DebugAgentRevision.Revision = DEBUG_AGENT_REVISION;\r | |
1957 | DebugAgentRevision.Capabilities = DEBUG_AGENT_CAPABILITIES;\r | |
93c0bdec | 1958 | Status = SendDataResponsePacket ((UINT8 *) &DebugAgentRevision, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_REVISION));\r |
18b144ea | 1959 | break;\r |
1960 | \r | |
1961 | case DEBUG_COMMAND_GET_EXCEPTION:\r | |
1962 | Exception.ExceptionNum = (UINT8) Vector;\r | |
93c0bdec | 1963 | Exception.ExceptionData = (UINT32) CpuContext->ExceptionData;\r |
1964 | Status = SendDataResponsePacket ((UINT8 *) &Exception, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_EXCEPTION));\r | |
18b144ea | 1965 | break;\r |
1966 | \r | |
1967 | case DEBUG_COMMAND_SET_VIEWPOINT:\r | |
93c0bdec | 1968 | SetViewPoint = (DEBUG_DATA_SET_VIEWPOINT *) (DebugHeader + 1);\r |
b422b62c | 1969 | if (MultiProcessorDebugSupport()) {\r |
93c0bdec | 1970 | if (IsCpuStopped (SetViewPoint->ViewPoint)) {\r |
1971 | SetDebugViewPoint (SetViewPoint->ViewPoint);\r | |
18b144ea | 1972 | SendAckPacket (DEBUG_COMMAND_OK);\r |
1973 | } else {\r | |
1974 | //\r | |
1975 | // If CPU is not halted\r | |
1976 | //\r | |
1977 | SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);\r | |
1978 | }\r | |
93c0bdec | 1979 | } else if (SetViewPoint->ViewPoint == 0) {\r |
18b144ea | 1980 | SendAckPacket (DEBUG_COMMAND_OK);\r |
1981 | \r | |
1982 | } else {\r | |
1983 | SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);\r | |
1984 | }\r | |
1985 | \r | |
1986 | break;\r | |
1987 | \r | |
1988 | case DEBUG_COMMAND_GET_VIEWPOINT:\r | |
1989 | Data32 = mDebugMpContext.ViewPointIndex;\r | |
93c0bdec | 1990 | SendDataResponsePacket((UINT8 *) &Data32, (UINT16) sizeof (UINT32));\r |
1991 | break;\r | |
1992 | \r | |
1993 | case DEBUG_COMMAND_MEMORY_READY:\r | |
b422b62c | 1994 | Data8 = (UINT8) GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY);\r |
93c0bdec | 1995 | SendDataResponsePacket (&Data8, (UINT16) sizeof (UINT8));\r |
1996 | break;\r | |
1997 | \r | |
1998 | case DEBUG_COMMAND_DETACH:\r | |
1999 | SetHostAttached (FALSE);\r | |
2000 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
2001 | break;\r | |
2002 | \r | |
2003 | case DEBUG_COMMAND_CPUID:\r | |
2004 | Cpuid = (DEBUG_DATA_CPUID *) (DebugHeader + 1);\r | |
2005 | AsmCpuidEx (\r | |
2006 | Cpuid->Eax, Cpuid->Ecx, \r | |
2007 | &CpuidResponse.Eax, &CpuidResponse.Ebx,\r | |
2008 | &CpuidResponse.Ecx, &CpuidResponse.Edx\r | |
2009 | );\r | |
2010 | SendDataResponsePacket ((UINT8 *) &CpuidResponse, (UINT16) sizeof (CpuidResponse));\r | |
2011 | break;\r | |
2012 | \r | |
2013 | case DEBUG_COMMAND_SEARCH_SIGNATURE:\r | |
2014 | SearchSignature = (DEBUG_DATA_SEARCH_SIGNATURE *) (DebugHeader + 1);\r | |
2015 | if ((SearchSignature->Alignment != 0) && \r | |
2016 | (SearchSignature->Alignment == GetPowerOfTwo32 (SearchSignature->Alignment))\r | |
2017 | ) {\r | |
2018 | if (SearchSignature->Positive) {\r | |
2019 | for (\r | |
2020 | Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start, SearchSignature->Alignment);\r | |
2021 | Data64 <= SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength;\r | |
2022 | Data64 += SearchSignature->Alignment\r | |
2023 | ) {\r | |
2024 | if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {\r | |
2025 | break;\r | |
2026 | }\r | |
2027 | }\r | |
2028 | if (Data64 > SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength) {\r | |
2029 | Data64 = (UINT64) -1;\r | |
2030 | }\r | |
2031 | } else {\r | |
2032 | for (\r | |
2033 | Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start - SearchSignature->Alignment, SearchSignature->Alignment);\r | |
2034 | Data64 >= SearchSignature->Start - SearchSignature->Count;\r | |
2035 | Data64 -= SearchSignature->Alignment\r | |
2036 | ) {\r | |
2037 | if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {\r | |
2038 | break;\r | |
2039 | }\r | |
2040 | }\r | |
2041 | if (Data64 < SearchSignature->Start - SearchSignature->Count) {\r | |
2042 | Data64 = (UINT64) -1;\r | |
2043 | }\r | |
2044 | }\r | |
2045 | SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (Data64));\r | |
2046 | } else {\r | |
2047 | Status = RETURN_UNSUPPORTED;\r | |
2048 | }\r | |
18b144ea | 2049 | break;\r |
2050 | \r | |
2051 | default:\r | |
2052 | SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);\r | |
2053 | break;\r | |
2054 | }\r | |
2055 | \r | |
2056 | if (Status == RETURN_UNSUPPORTED) {\r | |
2057 | SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);\r | |
2058 | } else if (Status != RETURN_SUCCESS) {\r | |
2059 | SendAckPacket (DEBUG_COMMAND_ABORT);\r | |
2060 | }\r | |
2061 | \r | |
b422b62c | 2062 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 2063 | CpuPause ();\r |
2064 | }\r | |
2065 | }\r | |
2066 | \r | |
2067 | /**\r | |
2068 | C function called in interrupt handler.\r | |
2069 | \r | |
2070 | @param[in] Vector Vector value of exception or interrutp.\r | |
2071 | @param[in] CpuContext Pointer to save CPU context.\r | |
2072 | \r | |
2073 | **/\r | |
2074 | VOID\r | |
2075 | EFIAPI\r | |
2076 | InterruptProcess (\r | |
2077 | IN UINT32 Vector,\r | |
2078 | IN DEBUG_CPU_CONTEXT *CpuContext\r | |
2079 | )\r | |
2080 | {\r | |
93c0bdec | 2081 | UINT8 InputCharacter;\r |
2082 | UINT8 BreakCause;\r | |
2083 | UINTN SavedEip;\r | |
2084 | BOOLEAN BreakReceived;\r | |
2085 | UINT32 ProcessorIndex;\r | |
2086 | UINT32 CurrentDebugTimerInitCount;\r | |
2087 | DEBUG_PORT_HANDLE Handle;\r | |
2088 | UINT8 Data8;\r | |
2089 | UINT8 *Al;\r | |
2090 | UINT32 IssuedViewPoint;\r | |
2091 | DEBUG_AGENT_EXCEPTION_BUFFER *ExceptionBuffer;\r | |
18b144ea | 2092 | \r |
93c0bdec | 2093 | ProcessorIndex = 0;\r |
2094 | IssuedViewPoint = 0;\r | |
2095 | BreakReceived = FALSE;\r | |
18b144ea | 2096 | \r |
b422b62c | 2097 | if (mSkipBreakpoint) {\r |
2098 | //\r | |
2099 | // If Skip Breakpoint flag is set, means communication is disturbed by hardware SMI, we need to ignore the break points in SMM\r | |
2100 | //\r | |
2101 | if ((Vector == DEBUG_INT1_VECTOR) || (Vector == DEBUG_INT3_VECTOR)) {\r | |
2102 | DebugPortWriteBuffer (GetDebugPortHandle(), (UINT8 *) mWarningMsgIngoreBreakpoint, AsciiStrLen (mWarningMsgIngoreBreakpoint));\r | |
2103 | return;\r | |
2104 | }\r | |
2105 | }\r | |
2106 | \r | |
2107 | if (MultiProcessorDebugSupport()) {\r | |
18b144ea | 2108 | ProcessorIndex = GetProcessorIndex ();\r |
93c0bdec | 2109 | //\r |
2110 | // If this processor has alreay halted before, need to check it later\r | |
2111 | //\r | |
2112 | if (IsCpuStopped (ProcessorIndex)) {\r | |
2113 | IssuedViewPoint = ProcessorIndex;\r | |
2114 | }\r | |
18b144ea | 2115 | }\r |
2116 | \r | |
b422b62c | 2117 | if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {\r |
93c0bdec | 2118 | //\r |
2119 | // Check if this exception is issued by Debug Agent itself\r | |
2120 | // If yes, fill the debug agent exception buffer and LongJump() back to\r | |
2121 | // the saved CPU content in CommandCommunication()\r | |
2122 | //\r | |
b422b62c | 2123 | if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {\r |
845a7fe0 | 2124 | DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n", Vector, (UINTN)CpuContext->Eip);\r |
93c0bdec | 2125 | ExceptionBuffer = (DEBUG_AGENT_EXCEPTION_BUFFER *) (UINTN) GetMailboxPointer()->ExceptionBufferPointer;\r |
2126 | ExceptionBuffer->ExceptionContent.ExceptionNum = (UINT8) Vector;\r | |
2127 | ExceptionBuffer->ExceptionContent.ExceptionData = (UINT32) CpuContext->ExceptionData;\r | |
b422b62c | 2128 | LongJump ((BASE_LIBRARY_JUMP_BUFFER *)(UINTN)(ExceptionBuffer), 1);\r |
93c0bdec | 2129 | }\r |
2130 | }\r | |
2131 | \r | |
b422b62c | 2132 | if (MultiProcessorDebugSupport()) {\r |
93c0bdec | 2133 | //\r |
2134 | // If RUN commmand is executing, wait for it done. \r | |
2135 | //\r | |
2136 | while (mDebugMpContext.RunCommandSet) {\r | |
2137 | CpuPause ();\r | |
2138 | }\r | |
2139 | }\r | |
2140 | \r | |
b422b62c | 2141 | Handle = GetDebugPortHandle();\r |
2142 | BreakCause = GetBreakCause (Vector, CpuContext);\r | |
18b144ea | 2143 | switch (Vector) {\r |
2144 | case DEBUG_INT1_VECTOR:\r | |
2145 | case DEBUG_INT3_VECTOR:\r | |
93c0bdec | 2146 | switch (BreakCause) {\r |
2147 | case DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET:\r | |
b422b62c | 2148 | if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {\r |
93c0bdec | 2149 | //\r |
2150 | // Try to connect HOST, return if fails\r | |
2151 | //\r | |
18b144ea | 2152 | break;\r |
2153 | }\r | |
18b144ea | 2154 | CommandCommunication (Vector, CpuContext, BreakReceived);\r |
93c0bdec | 2155 | break;\r |
18b144ea | 2156 | \r |
93c0bdec | 2157 | case DEBUG_DATA_BREAK_CAUSE_STEPPING:\r |
18b144ea | 2158 | //\r |
2159 | // Stepping is finished, send Ack package.\r | |
2160 | //\r | |
b422b62c | 2161 | if (MultiProcessorDebugSupport()) {\r |
18b144ea | 2162 | mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;\r |
2163 | }\r | |
2164 | SendAckPacket (DEBUG_COMMAND_OK);\r | |
2165 | CommandCommunication (Vector, CpuContext, BreakReceived);\r | |
93c0bdec | 2166 | break;\r |
18b144ea | 2167 | \r |
93c0bdec | 2168 | case DEBUG_DATA_BREAK_CAUSE_MEMORY_READY:\r |
18b144ea | 2169 | //\r |
2170 | // Memory is ready\r | |
2171 | //\r | |
b422b62c | 2172 | SendCommandAndWaitForAckOK (DEBUG_COMMAND_MEMORY_READY, READ_PACKET_TIMEOUT, &BreakReceived, NULL);\r |
18b144ea | 2173 | CommandCommunication (Vector, CpuContext, BreakReceived);\r |
93c0bdec | 2174 | break;\r |
18b144ea | 2175 | \r |
93c0bdec | 2176 | case DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD:\r |
2177 | case DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD:\r | |
2178 | //\r | |
2179 | // Set AL to DEBUG_AGENT_IMAGE_CONTINUE\r | |
2180 | //\r | |
2181 | Al = ArchReadRegisterBuffer (CpuContext, SOFT_DEBUGGER_REGISTER_AX, &Data8);\r | |
2182 | *Al = DEBUG_AGENT_IMAGE_CONTINUE;\r | |
18b144ea | 2183 | \r |
93c0bdec | 2184 | if (!IsHostAttached ()) {\r |
18b144ea | 2185 | //\r |
93c0bdec | 2186 | // If HOST is not connected for image load/unload, return\r |
18b144ea | 2187 | //\r |
93c0bdec | 2188 | break;\r |
18b144ea | 2189 | }\r |
93c0bdec | 2190 | //\r |
2191 | // Continue to run the following common code\r | |
2192 | //\r | |
2193 | \r | |
2194 | case DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT:\r | |
2195 | case DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT:\r | |
2196 | default:\r | |
2197 | //\r | |
2198 | // Send Break packet to HOST\r | |
2199 | //\r | |
b422b62c | 2200 | AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
93c0bdec | 2201 | //\r |
2202 | // Only the first breaking processor could send BREAK_POINT to HOST\r | |
2203 | // \r | |
2204 | if (IsFirstBreakProcessor (ProcessorIndex)) {\r | |
b422b62c | 2205 | SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);\r |
18b144ea | 2206 | }\r |
b422b62c | 2207 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
93c0bdec | 2208 | \r |
18b144ea | 2209 | if (Vector == DEBUG_INT3_VECTOR) {\r |
2210 | //\r | |
2211 | // go back address located "0xCC"\r | |
2212 | //\r | |
2213 | CpuContext->Eip--;\r | |
2214 | SavedEip = CpuContext->Eip;\r | |
2215 | CommandCommunication (Vector, CpuContext, BreakReceived);\r | |
2216 | if ((SavedEip == CpuContext->Eip) &&\r | |
2217 | (*(UINT8 *) (UINTN) CpuContext->Eip == DEBUG_SW_BREAKPOINT_SYMBOL)) {\r | |
2218 | //\r | |
2219 | // If this is not a software breakpoint set by HOST,\r | |
2220 | // restore EIP\r | |
2221 | //\r | |
2222 | CpuContext->Eip++;\r | |
2223 | }\r | |
2224 | } else {\r | |
2225 | CommandCommunication (Vector, CpuContext, BreakReceived);\r | |
2226 | }\r | |
93c0bdec | 2227 | break;\r |
18b144ea | 2228 | }\r |
2229 | \r | |
2230 | break;\r | |
2231 | \r | |
2232 | case DEBUG_TIMER_VECTOR:\r | |
2233 | \r | |
b422b62c | 2234 | AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
93c0bdec | 2235 | \r |
b422b62c | 2236 | if (MultiProcessorDebugSupport()) {\r |
18b144ea | 2237 | if (IsBsp (ProcessorIndex)) {\r |
2238 | //\r | |
2239 | // If current processor is BSP, check Apic timer's init count if changed,\r | |
2240 | // it may be re-written when switching BSP.\r | |
2241 | // If it changed, re-initialize debug timer\r | |
2242 | //\r | |
2243 | CurrentDebugTimerInitCount = GetApicTimerInitCount ();\r | |
2244 | if (mDebugMpContext.DebugTimerInitCount != CurrentDebugTimerInitCount) {\r | |
2245 | InitializeDebugTimer ();\r | |
2246 | }\r | |
2247 | }\r | |
2248 | \r | |
2249 | if (!IsBsp (ProcessorIndex) || mDebugMpContext.IpiSentByAp) {\r | |
b422b62c | 2250 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 2251 | //\r |
2252 | // If current processor is not BSP or this is one IPI sent by AP\r | |
2253 | //\r | |
2254 | if (mDebugMpContext.BreakAtCpuIndex != (UINT32) (-1)) {\r | |
2255 | CommandCommunication (Vector, CpuContext, FALSE);\r | |
2256 | }\r | |
2257 | \r | |
2258 | //\r | |
2259 | // Clear EOI before exiting interrupt process routine.\r | |
2260 | //\r | |
2261 | SendApicEoi ();\r | |
2262 | break;\r | |
2263 | }\r | |
2264 | }\r | |
2265 | \r | |
2266 | //\r | |
2267 | // Only BSP could run here\r | |
2268 | //\r | |
93c0bdec | 2269 | while (TRUE) {\r |
18b144ea | 2270 | //\r |
93c0bdec | 2271 | // If there is data in debug port, will check whether it is break(attach/break-in) symbol,\r |
18b144ea | 2272 | // If yes, go into communication mode with HOST.\r |
2273 | // If no, exit interrupt process.\r | |
2274 | //\r | |
93c0bdec | 2275 | if (DebugReadBreakSymbol (Handle, &InputCharacter) == EFI_NOT_FOUND) {\r |
2276 | break;\r | |
2277 | }\r | |
b422b62c | 2278 | \r |
93c0bdec | 2279 | if ((!IsHostAttached () && (InputCharacter == DEBUG_STARTING_SYMBOL_ATTACH)) ||\r |
b422b62c | 2280 | (IsHostAttached () && (InputCharacter == DEBUG_COMMAND_HALT)) ||\r |
2281 | (IsHostAttached () && (InputCharacter == DEBUG_COMMAND_GO))\r | |
93c0bdec | 2282 | ) {\r |
2283 | DebugAgentMsgPrint (DEBUG_AGENT_VERBOSE, "Received data [%02x]\n", InputCharacter);\r | |
2284 | //\r | |
2285 | // Ack OK for break-in symbol\r | |
2286 | //\r | |
18b144ea | 2287 | SendAckPacket (DEBUG_COMMAND_OK);\r |
93c0bdec | 2288 | \r |
b422b62c | 2289 | //\r |
2290 | // If receive GO command in Debug Timer, means HOST may lost ACK packet before.\r | |
2291 | //\r | |
2292 | if (InputCharacter == DEBUG_COMMAND_GO) {\r | |
2293 | break;\r | |
2294 | }\r | |
2295 | \r | |
93c0bdec | 2296 | if (!IsHostAttached ()) {\r |
2297 | //\r | |
2298 | // Try to attach HOST, if no ack received after 200ms, return\r | |
2299 | //\r | |
b422b62c | 2300 | if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {\r |
93c0bdec | 2301 | break;\r |
2302 | }\r | |
2303 | }\r | |
2304 | \r | |
b422b62c | 2305 | if (MultiProcessorDebugSupport()) {\r |
93c0bdec | 2306 | if(FindNextPendingBreakCpu () != -1) {\r |
18b144ea | 2307 | SetCpuBreakFlagByIndex (ProcessorIndex, TRUE);\r |
2308 | } else {\r | |
2309 | HaltOtherProcessors (ProcessorIndex);\r | |
2310 | }\r | |
2311 | }\r | |
b422b62c | 2312 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 2313 | CommandCommunication (Vector, CpuContext, BreakReceived);\r |
b422b62c | 2314 | AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 2315 | break;\r |
2316 | }\r | |
2317 | }\r | |
2318 | \r | |
2319 | //\r | |
2320 | // Clear EOI before exiting interrupt process routine.\r | |
2321 | //\r | |
2322 | SendApicEoi ();\r | |
2323 | \r | |
b422b62c | 2324 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 2325 | \r |
2326 | break;\r | |
2327 | \r | |
2328 | default:\r | |
18b144ea | 2329 | if (Vector <= DEBUG_EXCEPT_SIMD) {\r |
93c0bdec | 2330 | if (BreakCause == DEBUG_DATA_BREAK_CAUSE_STEPPING) {\r |
2331 | //\r | |
2332 | // Stepping is finished, send Ack package.\r | |
2333 | //\r | |
b422b62c | 2334 | if (MultiProcessorDebugSupport()) {\r |
93c0bdec | 2335 | mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;\r |
18b144ea | 2336 | }\r |
93c0bdec | 2337 | SendAckPacket (DEBUG_COMMAND_OK);\r |
2338 | } else {\r | |
2339 | //\r | |
2340 | // Exception occurs, send Break packet to HOST\r | |
2341 | //\r | |
b422b62c | 2342 | AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
93c0bdec | 2343 | //\r |
2344 | // Only the first breaking processor could send BREAK_POINT to HOST\r | |
2345 | // \r | |
2346 | if (IsFirstBreakProcessor (ProcessorIndex)) {\r | |
b422b62c | 2347 | SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);\r |
93c0bdec | 2348 | }\r |
b422b62c | 2349 | ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);\r |
18b144ea | 2350 | }\r |
93c0bdec | 2351 | \r |
18b144ea | 2352 | CommandCommunication (Vector, CpuContext, BreakReceived);\r |
2353 | }\r | |
2354 | break;\r | |
2355 | }\r | |
2356 | \r | |
b422b62c | 2357 | if (MultiProcessorDebugSupport()) {\r |
18b144ea | 2358 | //\r |
2359 | // Clear flag and wait for all processors run here\r | |
2360 | //\r | |
2361 | SetIpiSentByApFlag (FALSE);\r | |
93c0bdec | 2362 | while (mDebugMpContext.RunCommandSet) {\r |
2363 | CpuPause ();\r | |
2364 | }\r | |
2365 | \r | |
2366 | //\r | |
2367 | // Only current (view) processor could clean up AgentInProgress flag.\r | |
2368 | //\r | |
2369 | if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {\r | |
2370 | IssuedViewPoint = mDebugMpContext.ViewPointIndex;\r | |
2371 | }\r | |
2372 | }\r | |
2373 | \r | |
b422b62c | 2374 | if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {\r |
93c0bdec | 2375 | //\r |
b422b62c | 2376 | // If the command is not stepping, clean up AgentInProgress flag\r |
93c0bdec | 2377 | //\r |
b422b62c | 2378 | SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 0);\r |
18b144ea | 2379 | }\r |
2380 | \r | |
2381 | return;\r | |
2382 | }\r | |
2383 | \r |