2 Debug Agent library implementition.
4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "SmmDebugAgentLib.h"
17 DEBUG_AGENT_MAILBOX
*mMailboxPointer
= NULL
;
18 DEBUG_AGENT_MAILBOX mLocalMailbox
;
19 UINTN mSavedDebugRegisters
[6];
20 IA32_IDT_GATE_DESCRIPTOR mIdtEntryTable
[33];
21 BOOLEAN mSkipBreakpoint
= FALSE
;
23 CHAR8 mWarningMsgIgnoreSmmEntryBreak
[] = "Ignore smmentrybreak setting for SMI issued during DXE debugging!\r\n";
26 Check if debug agent support multi-processor.
28 @retval TRUE Multi-processor is supported.
29 @retval FALSE Multi-processor is not supported.
33 MultiProcessorDebugSupport (
41 Read the Attach/Break-in symbols from the debug port.
43 @param[in] Handle Pointer to Debug Port handle.
44 @param[out] BreakSymbol Returned break symbol.
46 @retval EFI_SUCCESS Read the symbol in BreakSymbol.
47 @retval EFI_NOT_FOUND No read the break symbol.
51 DebugReadBreakSymbol (
52 IN DEBUG_PORT_HANDLE Handle
,
53 OUT UINT8
*BreakSymbol
57 // Smm instance has no debug timer to poll break symbol.
63 Get the pointer to Mailbox from the GUIDed HOB.
65 @return Pointer to Mailbox.
73 EFI_HOB_GUID_TYPE
*GuidHob
;
74 UINT64
*MailboxLocation
;
75 DEBUG_AGENT_MAILBOX
*Mailbox
;
77 GuidHob
= GetFirstGuidHob (&gEfiDebugAgentGuid
);
78 if (GuidHob
== NULL
) {
81 MailboxLocation
= (UINT64
*) (GET_GUID_HOB_DATA(GuidHob
));
82 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
83 VerifyMailboxChecksum (Mailbox
);
89 Get Debug Agent Mailbox pointer.
91 @return Mailbox pointer.
99 VerifyMailboxChecksum (mMailboxPointer
);
100 return mMailboxPointer
;
104 Get debug port handle.
106 @return Debug port handle.
114 return (DEBUG_PORT_HANDLE
) (UINTN
)(GetMailboxPointer()->DebugPortHandle
);
118 Store debug register when SMI exit.
126 mSavedDebugRegisters
[0] = AsmReadDr0 ();
127 mSavedDebugRegisters
[1] = AsmReadDr1 ();
128 mSavedDebugRegisters
[2] = AsmReadDr2 ();
129 mSavedDebugRegisters
[3] = AsmReadDr3 ();
130 mSavedDebugRegisters
[4] = AsmReadDr6 ();
131 mSavedDebugRegisters
[5] = AsmReadDr7 ();
135 Restore debug register when SMI exit.
139 RestoreDebugRegister (
144 AsmWriteDr0 (mSavedDebugRegisters
[0]);
145 AsmWriteDr1 (mSavedDebugRegisters
[1]);
146 AsmWriteDr2 (mSavedDebugRegisters
[2]);
147 AsmWriteDr3 (mSavedDebugRegisters
[3]);
148 AsmWriteDr6 (mSavedDebugRegisters
[4]);
149 AsmWriteDr7 (mSavedDebugRegisters
[5]);
153 Initialize debug agent.
155 This function is used to set up debug enviroment for source level debug
158 If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde IDT table entries
159 and initialize debug port. It will get debug agent Mailbox from GUIDed HOB,
160 it it exists, debug agent wiil copied it into the local Mailbox in SMM space.
161 it will overirde IDT table entries and initialize debug port. Context will be
163 If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug
164 Registers and get local Mailbox in SMM space. Context will be NULL.
165 If InitFlag is DEBUG_AGENT_INIT_EXIT_SMI, debug agent will restore Debug
166 Registers. Context will be NULL.
168 @param[in] InitFlag Init flag is used to decide initialize process.
169 @param[in] Context Context needed according to InitFlag.
170 @param[in] Function Continue function called by debug agent library; it was
176 InitializeDebugAgent (
178 IN VOID
*Context
, OPTIONAL
179 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
183 UINT64 DebugPortHandle
;
184 IA32_IDT_GATE_DESCRIPTOR IdtEntry
[33];
185 IA32_DESCRIPTOR IdtDescriptor
;
186 IA32_DESCRIPTOR
*Ia32Idtr
;
187 IA32_IDT_ENTRY
*Ia32IdtEntry
;
188 IA32_DESCRIPTOR Idtr
;
189 UINT16 IdtEntryCount
;
190 DEBUG_AGENT_MAILBOX
*Mailbox
;
191 UINT64
*MailboxLocation
;
192 UINT32 DebugTimerFrequency
;
193 BOOLEAN PeriodicMode
;
197 case DEBUG_AGENT_INIT_SMM
:
199 // Install configuration table for persisted vector handoff info
201 Status
= gSmst
->SmmInstallConfigurationTable (
203 &gEfiVectorHandoffTableGuid
,
204 (VOID
*) &mVectorHandoffInfoDebugAgent
[0],
205 sizeof (EFI_VECTOR_HANDOFF_INFO
) * mVectorHandoffInfoCount
207 if (EFI_ERROR (Status
)) {
208 DEBUG ((EFI_D_ERROR
, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));
212 // Check if Debug Agent initialized in DXE phase
214 Status
= EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid
, (VOID
**) &Mailbox
);
215 if (Status
== EFI_SUCCESS
&& Mailbox
!= NULL
) {
216 VerifyMailboxChecksum (Mailbox
);
217 mMailboxPointer
= Mailbox
;
221 // Check if Debug Agent initialized in SEC/PEI phase
223 Mailbox
= GetMailboxFromHob ();
224 if (Mailbox
!= NULL
) {
225 mMailboxPointer
= Mailbox
;
229 // Debug Agent was not initialized before, use the local mailbox.
231 ZeroMem (&mLocalMailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
232 Mailbox
= &mLocalMailbox
;
234 // Save original IDT entries
236 AsmReadIdtr (&IdtDescriptor
);
237 CopyMem (&IdtEntry
, (VOID
*)IdtDescriptor
.Base
, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR
));
239 // Initialized Debug Agent
241 InitializeDebugIdt ();
243 // Initialize Debug Timer hardware and save its frequency
245 InitializeDebugTimer (&DebugTimerFrequency
);
246 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
248 DebugPortHandle
= (UINT64
) (UINTN
)DebugPortInitialize ((DEBUG_PORT_HANDLE
) (UINTN
)Mailbox
->DebugPortHandle
, NULL
);
249 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, DebugPortHandle
);
250 mMailboxPointer
= Mailbox
;
252 // Trigger one software interrupt to inform HOST
254 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE
);
256 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
, 1);
258 // Memory has been ready
260 if (IsHostAttached ()) {
262 // Trigger one software interrupt to inform HOST
264 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE
);
267 // Find and report PE/COFF image info to HOST
269 FindAndReportModuleImageInfo (SIZE_4KB
);
271 // Restore saved IDT entries
273 CopyMem ((VOID
*)IdtDescriptor
.Base
, &IdtEntry
, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR
));
277 case DEBUG_AGENT_INIT_ENTER_SMI
:
278 SaveDebugRegister ();
279 InitializeDebugIdt ();
281 // Check if CPU APIC Timer is working, otherwise initialize it.
283 GetApicTimerState (NULL
, &PeriodicMode
, NULL
);
284 TimerCycle
= GetApicTimerInitCount ();
285 if (PeriodicMode
!= TRUE
|| TimerCycle
== 0) {
286 InitializeDebugTimer (NULL
);
287 DisableApicTimerInterrupt ();
289 Mailbox
= GetMailboxPointer ();
290 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
) == 1) {
292 // If Debug Agent has been communicaton state with HOST, we need skip
293 // any break points set in SMM, set Skip Breakpoint flag
295 mSkipBreakpoint
= TRUE
;
297 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI
) == 1) {
298 if (mSkipBreakpoint
) {
300 // Print warning message if ignore smm entry break
302 DebugPortWriteBuffer ((DEBUG_PORT_HANDLE
) (UINTN
)Mailbox
->DebugPortHandle
,
303 (UINT8
*)mWarningMsgIgnoreSmmEntryBreak
,
304 AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak
)
308 // If SMM entry break is set, SMM code will be break at here.
315 case DEBUG_AGENT_INIT_EXIT_SMI
:
316 Mailbox
= GetMailboxPointer ();
318 // Clear Skip Breakpoint flag
320 mSkipBreakpoint
= FALSE
;
321 RestoreDebugRegister ();
324 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
:
325 if (Context
== NULL
) {
326 DEBUG ((EFI_D_ERROR
, "DebugAgent: Input parameter Context cannot be NULL!\n"));
329 Ia32Idtr
= (IA32_DESCRIPTOR
*) Context
;
330 Ia32IdtEntry
= (IA32_IDT_ENTRY
*)(Ia32Idtr
->Base
);
331 MailboxLocation
= (UINT64
*) (UINTN
) (Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetLow
+
332 (Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetHigh
<< 16));
333 mMailboxPointer
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
334 VerifyMailboxChecksum (mMailboxPointer
);
336 // Get original IDT address and size.
338 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
339 IdtEntryCount
= (UINT16
) ((Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
));
340 if (IdtEntryCount
< 33) {
341 Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33 - 1);
342 Idtr
.Base
= (UINTN
) &mIdtEntryTable
;
343 ZeroMem (&mIdtEntryTable
, Idtr
.Limit
+ 1);
344 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
347 InitializeDebugIdt ();
349 // Initialize Debug Timer hardware and save its frequency
351 InitializeDebugTimer (&DebugTimerFrequency
);
352 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
355 FindAndReportModuleImageInfo (SIZE_4KB
);
361 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
362 // Debug Agent library instance.
364 DEBUG ((EFI_D_ERROR
, "Debug Agent: The InitFlag value is not allowed!\n"));