]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c
SourceLevelDebugPkg/DebugTimer: Dump Debug Timer parameter
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / SmmDebugAgent / SmmDebugAgentLib.c
CommitLineData
18b144ea 1/** @file\r
2 Debug Agent library implementition.\r
3\r
08021523 4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
18b144ea 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "SmmDebugAgentLib.h"\r
16\r
17DEBUG_AGENT_MAILBOX *mMailboxPointer = NULL;\r
b6ee5898 18DEBUG_AGENT_MAILBOX mLocalMailbox;\r
19UINTN mSavedDebugRegisters[6];\r
b422b62c 20IA32_IDT_GATE_DESCRIPTOR mIdtEntryTable[33];\r
21BOOLEAN mSkipBreakpoint = FALSE;\r
22\r
23CHAR8 mWarningMsgIgnoreSmmEntryBreak[] = "Ignore smmentrybreak setting for SMI issued during DXE debugging!\r\n";\r
24\r
25/**\r
26 Check if debug agent support multi-processor.\r
27\r
28 @retval TRUE Multi-processor is supported.\r
29 @retval FALSE Multi-processor is not supported.\r
30\r
31**/\r
32BOOLEAN\r
33MultiProcessorDebugSupport (\r
34 VOID\r
35 )\r
36{\r
37 return FALSE;\r
38}\r
18b144ea 39\r
93c0bdec 40/**\r
41 Read the Attach/Break-in symbols from the debug port.\r
42\r
43 @param[in] Handle Pointer to Debug Port handle.\r
44 @param[out] BreakSymbol Returned break symbol.\r
45\r
46 @retval EFI_SUCCESS Read the symbol in BreakSymbol.\r
47 @retval EFI_NOT_FOUND No read the break symbol.\r
48\r
49**/\r
50EFI_STATUS\r
51DebugReadBreakSymbol (\r
52 IN DEBUG_PORT_HANDLE Handle,\r
53 OUT UINT8 *BreakSymbol\r
54 )\r
55{\r
56 //\r
57 // Smm instance has no debug timer to poll break symbol.\r
58 //\r
59 return EFI_NOT_FOUND;\r
60}\r
18b144ea 61\r
b422b62c 62/**\r
63 Get the pointer to Mailbox from the GUIDed HOB.\r
64\r
65 @return Pointer to Mailbox.\r
66\r
67**/\r
68DEBUG_AGENT_MAILBOX *\r
69GetMailboxFromHob (\r
70 VOID\r
71 )\r
72{\r
73 EFI_HOB_GUID_TYPE *GuidHob;\r
74 UINT64 *MailboxLocation; \r
75 DEBUG_AGENT_MAILBOX *Mailbox;\r
76\r
77 GuidHob = GetFirstGuidHob (&gEfiDebugAgentGuid);\r
78 if (GuidHob == NULL) {\r
79 return NULL;\r
80 }\r
81 MailboxLocation = (UINT64 *) (GET_GUID_HOB_DATA(GuidHob));\r
82 Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
83 VerifyMailboxChecksum (Mailbox);\r
84 \r
85 return Mailbox;\r
86}\r
87\r
18b144ea 88/**\r
89 Get Debug Agent Mailbox pointer.\r
90\r
91 @return Mailbox pointer.\r
92\r
93**/\r
94DEBUG_AGENT_MAILBOX *\r
95GetMailboxPointer (\r
96 VOID\r
97 )\r
98{\r
b422b62c 99 VerifyMailboxChecksum (mMailboxPointer);\r
18b144ea 100 return mMailboxPointer;\r
101}\r
102\r
103/**\r
104 Get debug port handle.\r
105\r
106 @return Debug port handle.\r
107\r
108**/\r
109DEBUG_PORT_HANDLE\r
110GetDebugPortHandle (\r
111 VOID\r
112 )\r
113{\r
b422b62c 114 return (DEBUG_PORT_HANDLE) (UINTN)(GetMailboxPointer()->DebugPortHandle);\r
18b144ea 115}\r
116\r
117/**\r
118 Store debug register when SMI exit.\r
119\r
120**/\r
121VOID\r
122SaveDebugRegister (\r
123 VOID\r
124 )\r
125{\r
126 mSavedDebugRegisters[0] = AsmReadDr0 ();\r
127 mSavedDebugRegisters[1] = AsmReadDr1 ();\r
128 mSavedDebugRegisters[2] = AsmReadDr2 ();\r
129 mSavedDebugRegisters[3] = AsmReadDr3 ();\r
130 mSavedDebugRegisters[4] = AsmReadDr6 ();\r
131 mSavedDebugRegisters[5] = AsmReadDr7 ();\r
132}\r
133\r
134/**\r
135 Restore debug register when SMI exit.\r
136\r
137**/\r
138VOID\r
139RestoreDebugRegister (\r
140 VOID\r
141 )\r
142{\r
143 AsmWriteDr7 (0);\r
144 AsmWriteDr0 (mSavedDebugRegisters[0]);\r
145 AsmWriteDr1 (mSavedDebugRegisters[1]);\r
146 AsmWriteDr2 (mSavedDebugRegisters[2]);\r
147 AsmWriteDr3 (mSavedDebugRegisters[3]);\r
148 AsmWriteDr6 (mSavedDebugRegisters[4]);\r
149 AsmWriteDr7 (mSavedDebugRegisters[5]);\r
150}\r
151\r
152/**\r
153 Initialize debug agent.\r
154\r
155 This function is used to set up debug enviroment for source level debug\r
156 in SMM code.\r
157\r
158 If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde IDT table entries\r
159 and initialize debug port. It will get debug agent Mailbox from GUIDed HOB,\r
160 it it exists, debug agent wiil copied it into the local Mailbox in SMM space.\r
161 it will overirde IDT table entries and initialize debug port. Context will be\r
162 NULL.\r
163 If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug\r
164 Registers and get local Mailbox in SMM space. Context will be NULL.\r
165 If InitFlag is DEBUG_AGENT_INIT_EXIT_SMI, debug agent will restore Debug\r
166 Registers. Context will be NULL.\r
167\r
168 @param[in] InitFlag Init flag is used to decide initialize process.\r
169 @param[in] Context Context needed according to InitFlag.\r
170 @param[in] Function Continue function called by debug agent library; it was\r
171 optional.\r
172\r
173**/\r
174VOID\r
175EFIAPI\r
176InitializeDebugAgent (\r
177 IN UINT32 InitFlag,\r
178 IN VOID *Context, OPTIONAL\r
179 IN DEBUG_AGENT_CONTINUE Function OPTIONAL\r
180 )\r
181{\r
b422b62c 182 EFI_STATUS Status;\r
183 UINT64 DebugPortHandle;\r
184 IA32_IDT_GATE_DESCRIPTOR IdtEntry[33];\r
185 IA32_DESCRIPTOR IdtDescriptor;\r
186 IA32_DESCRIPTOR *Ia32Idtr;\r
187 IA32_IDT_ENTRY *Ia32IdtEntry;\r
188 IA32_DESCRIPTOR Idtr;\r
189 UINT16 IdtEntryCount;\r
190 DEBUG_AGENT_MAILBOX *Mailbox;\r
191 UINT64 *MailboxLocation;\r
08021523 192 UINT32 DebugTimerFrequency;\r
edbb2713
JF
193 BOOLEAN PeriodicMode;\r
194 UINTN TimerCycle;\r
18b144ea 195\r
196 switch (InitFlag) {\r
197 case DEBUG_AGENT_INIT_SMM:\r
8cc26df4
JF
198 //\r
199 // Install configuration table for persisted vector handoff info\r
200 //\r
201 Status = gSmst->SmmInstallConfigurationTable (\r
202 gSmst,\r
203 &gEfiVectorHandoffTableGuid,\r
204 (VOID *) &mVectorHandoffInfoDebugAgent[0],\r
205 sizeof (EFI_VECTOR_HANDOFF_INFO) * mVectorHandoffInfoCount\r
206 );\r
207 if (EFI_ERROR (Status)) {\r
208 DEBUG ((EFI_D_ERROR, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));\r
209 CpuDeadLoop ();\r
210 }\r
b422b62c 211 //\r
212 // Check if Debug Agent initialized in DXE phase\r
213 //\r
214 Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &Mailbox);\r
215 if (Status == EFI_SUCCESS && Mailbox != NULL) {\r
216 VerifyMailboxChecksum (Mailbox);\r
217 mMailboxPointer = Mailbox;\r
218 break;\r
18b144ea 219 }\r
b422b62c 220 //\r
221 // Check if Debug Agent initialized in SEC/PEI phase\r
222 //\r
223 Mailbox = GetMailboxFromHob (); \r
224 if (Mailbox != NULL) {\r
225 mMailboxPointer = Mailbox;\r
226 break;\r
227 }\r
228 //\r
8cc26df4 229 // Debug Agent was not initialized before, use the local mailbox.\r
b422b62c 230 //\r
231 ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
232 Mailbox = &mLocalMailbox;\r
233 //\r
234 // Save original IDT entries\r
235 //\r
236 AsmReadIdtr (&IdtDescriptor); \r
237 CopyMem (&IdtEntry, (VOID *)IdtDescriptor.Base, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR));\r
238 //\r
239 // Initialized Debug Agent\r
240 //\r
241 InitializeDebugIdt ();\r
08021523
JF
242 //\r
243 // Initialize Debug Timer hardware and save its frequency\r
244 //\r
86d13652 245 InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
08021523
JF
246 UpdateMailboxContent (mMailboxPointer, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
247\r
b422b62c 248 DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE) (UINTN)Mailbox->DebugPortHandle, NULL);\r
249 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
250 mMailboxPointer = Mailbox;\r
251 //\r
252 // Trigger one software interrupt to inform HOST\r
253 //\r
254 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);\r
255\r
256 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
257 //\r
258 // Memory has been ready\r
259 //\r
260 if (IsHostAttached ()) {\r
261 //\r
262 // Trigger one software interrupt to inform HOST\r
263 //\r
264 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
265 }\r
266 //\r
267 // Find and report PE/COFF image info to HOST\r
268 // \r
269 FindAndReportModuleImageInfo (SIZE_4KB);\r
270 //\r
271 // Restore saved IDT entries\r
272 // \r
273 CopyMem ((VOID *)IdtDescriptor.Base, &IdtEntry, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR));\r
18b144ea 274\r
275 break;\r
276\r
277 case DEBUG_AGENT_INIT_ENTER_SMI:\r
278 SaveDebugRegister ();\r
279 InitializeDebugIdt ();\r
edbb2713
JF
280 //\r
281 // Check if CPU APIC Timer is working, otherwise initialize it.\r
282 //\r
283 GetApicTimerState (NULL, &PeriodicMode, NULL);\r
284 TimerCycle = GetApicTimerInitCount ();\r
285 if (PeriodicMode != TRUE || TimerCycle == 0) {\r
86d13652 286 InitializeDebugTimer (NULL, FALSE);\r
edbb2713
JF
287 DisableApicTimerInterrupt ();\r
288 }\r
b422b62c 289 Mailbox = GetMailboxPointer ();\r
290 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {\r
18b144ea 291 //\r
b422b62c 292 // If Debug Agent has been communicaton state with HOST, we need skip\r
293 // any break points set in SMM, set Skip Breakpoint flag\r
18b144ea 294 //\r
b422b62c 295 mSkipBreakpoint = TRUE;\r
296 }\r
297 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI) == 1) {\r
298 if (mSkipBreakpoint) {\r
299 //\r
300 // Print warning message if ignore smm entry break\r
301 //\r
302 DebugPortWriteBuffer ((DEBUG_PORT_HANDLE) (UINTN)Mailbox->DebugPortHandle,\r
303 (UINT8 *)mWarningMsgIgnoreSmmEntryBreak,\r
304 AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak)\r
305 );\r
306 } else {\r
18b144ea 307 //\r
308 // If SMM entry break is set, SMM code will be break at here.\r
309 //\r
310 CpuBreakpoint ();\r
311 }\r
312 }\r
313 break;\r
314\r
315 case DEBUG_AGENT_INIT_EXIT_SMI:\r
b422b62c 316 Mailbox = GetMailboxPointer ();\r
317 //\r
318 // Clear Skip Breakpoint flag\r
319 //\r
320 mSkipBreakpoint = FALSE;\r
18b144ea 321 RestoreDebugRegister ();\r
322 break;\r
b422b62c 323\r
324 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:\r
325 if (Context == NULL) {\r
326 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
327 CpuDeadLoop ();\r
328 } else {\r
329 Ia32Idtr = (IA32_DESCRIPTOR *) Context;\r
330 Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
331 MailboxLocation = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow + \r
332 (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
333 mMailboxPointer = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
334 VerifyMailboxChecksum (mMailboxPointer);\r
335 //\r
336 // Get original IDT address and size.\r
337 //\r
338 AsmReadIdtr ((IA32_DESCRIPTOR *) &Idtr);\r
339 IdtEntryCount = (UINT16) ((Idtr.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
340 if (IdtEntryCount < 33) {\r
341 Idtr.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * 33 - 1);\r
342 Idtr.Base = (UINTN) &mIdtEntryTable;\r
343 ZeroMem (&mIdtEntryTable, Idtr.Limit + 1);\r
344 AsmWriteIdtr ((IA32_DESCRIPTOR *) &Idtr);\r
345 }\r
346\r
347 InitializeDebugIdt ();\r
348 //\r
08021523 349 // Initialize Debug Timer hardware and save its frequency\r
b422b62c 350 //\r
86d13652 351 InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
08021523 352 UpdateMailboxContent (mMailboxPointer, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
b422b62c 353 EnableInterrupts ();\r
354\r
355 FindAndReportModuleImageInfo (SIZE_4KB);\r
356 }\r
357 break;\r
358\r
359 default:\r
360 //\r
361 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this \r
362 // Debug Agent library instance.\r
363 //\r
364 DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
365 CpuDeadLoop ();\r
366 break; \r
18b144ea 367 }\r
368}\r
369\r