DebugAgent.c 85 KB

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