2 Debug Agent library implementation.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "SmmDebugAgentLib.h"
11 DEBUG_AGENT_MAILBOX
*mMailboxPointer
= NULL
;
12 DEBUG_AGENT_MAILBOX mLocalMailbox
;
13 UINTN mSavedDebugRegisters
[6];
14 IA32_IDT_GATE_DESCRIPTOR mIdtEntryTable
[33];
15 BOOLEAN mSkipBreakpoint
= FALSE
;
16 BOOLEAN mSmmDebugIdtInitFlag
= FALSE
;
17 BOOLEAN mApicTimerRestore
= FALSE
;
18 BOOLEAN mPeriodicMode
;
20 UINTN mApicTimerDivisor
;
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
) {
82 MailboxLocation
= (UINT64
*)(GET_GUID_HOB_DATA (GuidHob
));
83 Mailbox
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
84 VerifyMailboxChecksum (Mailbox
);
90 Get Debug Agent Mailbox pointer.
92 @return Mailbox pointer.
100 VerifyMailboxChecksum (mMailboxPointer
);
101 return mMailboxPointer
;
105 Get debug port handle.
107 @return Debug port handle.
115 return (DEBUG_PORT_HANDLE
)(UINTN
)(GetMailboxPointer ()->DebugPortHandle
);
119 Store debug register when SMI exit.
127 mSavedDebugRegisters
[0] = AsmReadDr0 ();
128 mSavedDebugRegisters
[1] = AsmReadDr1 ();
129 mSavedDebugRegisters
[2] = AsmReadDr2 ();
130 mSavedDebugRegisters
[3] = AsmReadDr3 ();
131 mSavedDebugRegisters
[4] = AsmReadDr6 ();
132 mSavedDebugRegisters
[5] = AsmReadDr7 ();
136 Restore debug register when SMI exit.
140 RestoreDebugRegister (
145 AsmWriteDr0 (mSavedDebugRegisters
[0]);
146 AsmWriteDr1 (mSavedDebugRegisters
[1]);
147 AsmWriteDr2 (mSavedDebugRegisters
[2]);
148 AsmWriteDr3 (mSavedDebugRegisters
[3]);
149 AsmWriteDr6 (mSavedDebugRegisters
[4]);
150 AsmWriteDr7 (mSavedDebugRegisters
[5]);
154 Initialize debug agent.
156 This function is used to set up debug environment for source level debug
159 If InitFlag is DEBUG_AGENT_INIT_SMM, it will override IDT table entries
160 and initialize debug port. It will get debug agent Mailbox from GUIDed HOB,
161 it it exists, debug agent wiil copied it into the local Mailbox in SMM space.
162 it will override IDT table entries and initialize debug port. Context will be
164 If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug
165 Registers and get local Mailbox in SMM space. Context will be NULL.
166 If InitFlag is DEBUG_AGENT_INIT_EXIT_SMI, debug agent will restore Debug
167 Registers. Context will be NULL.
169 @param[in] InitFlag Init flag is used to decide initialize process.
170 @param[in] Context Context needed according to InitFlag.
171 @param[in] Function Continue function called by debug agent library; it was
177 InitializeDebugAgent (
179 IN VOID
*Context OPTIONAL
,
180 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
184 UINT64 DebugPortHandle
;
185 IA32_IDT_GATE_DESCRIPTOR IdtEntry
[33];
186 IA32_DESCRIPTOR IdtDescriptor
;
187 IA32_DESCRIPTOR
*Ia32Idtr
;
188 IA32_IDT_ENTRY
*Ia32IdtEntry
;
189 IA32_DESCRIPTOR Idtr
;
190 UINT16 IdtEntryCount
;
191 DEBUG_AGENT_MAILBOX
*Mailbox
;
192 UINT64
*MailboxLocation
;
193 UINT32 DebugTimerFrequency
;
196 case DEBUG_AGENT_INIT_SMM
:
198 // Install configuration table for persisted vector handoff info
200 Status
= gSmst
->SmmInstallConfigurationTable (
202 &gEfiVectorHandoffTableGuid
,
203 (VOID
*)&mVectorHandoffInfoDebugAgent
[0],
204 sizeof (EFI_VECTOR_HANDOFF_INFO
) * mVectorHandoffInfoCount
206 if (EFI_ERROR (Status
)) {
207 DEBUG ((DEBUG_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
;
222 // Check if Debug Agent initialized in SEC/PEI phase
224 Mailbox
= GetMailboxFromHob ();
225 if (Mailbox
!= NULL
) {
226 mMailboxPointer
= Mailbox
;
231 // Debug Agent was not initialized before, use the local mailbox.
233 ZeroMem (&mLocalMailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
234 Mailbox
= &mLocalMailbox
;
236 // Save original IDT entries
238 AsmReadIdtr (&IdtDescriptor
);
239 CopyMem (&IdtEntry
, (VOID
*)IdtDescriptor
.Base
, 33 * sizeof (IA32_IDT_GATE_DESCRIPTOR
));
241 // Initialized Debug Agent
243 InitializeDebugIdt ();
245 // Initialize Debug Timer hardware and save its frequency
247 InitializeDebugTimer (&DebugTimerFrequency
, TRUE
);
248 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
250 DebugPortHandle
= (UINT64
)(UINTN
)DebugPortInitialize ((DEBUG_PORT_HANDLE
)(UINTN
)Mailbox
->DebugPortHandle
, NULL
);
251 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, DebugPortHandle
);
252 mMailboxPointer
= Mailbox
;
254 // Trigger one software interrupt to inform HOST
256 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE
);
258 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
, 1);
260 // Memory has been ready
262 if (IsHostAttached ()) {
264 // Trigger one software interrupt to inform HOST
266 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE
);
270 // Find and report PE/COFF image info to HOST
272 FindAndReportModuleImageInfo (SIZE_4KB
);
274 // Restore saved IDT entries
276 CopyMem ((VOID
*)IdtDescriptor
.Base
, &IdtEntry
, 33 * sizeof (IA32_IDT_GATE_DESCRIPTOR
));
280 case DEBUG_AGENT_INIT_ENTER_SMI
:
281 SaveDebugRegister ();
282 if (!mSmmDebugIdtInitFlag
) {
284 // We only need to initialize Debug IDT table at first SMI entry
285 // after SMM relocation.
287 InitializeDebugIdt ();
288 mSmmDebugIdtInitFlag
= TRUE
;
292 // Check if CPU APIC Timer is working, otherwise initialize it.
294 InitializeLocalApicSoftwareEnable (TRUE
);
295 GetApicTimerState (&mApicTimerDivisor
, &mPeriodicMode
, &mVector
);
296 mTimerCycle
= GetApicTimerInitCount ();
297 if (!mPeriodicMode
|| (mTimerCycle
== 0)) {
298 mApicTimerRestore
= TRUE
;
299 InitializeDebugTimer (NULL
, FALSE
);
302 Mailbox
= GetMailboxPointer ();
303 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
) == 1) {
305 // If Debug Agent has been communication state with HOST, we need skip
306 // any break points set in SMM, set Skip Breakpoint flag
308 mSkipBreakpoint
= TRUE
;
311 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI
) == 1) {
312 if (mSkipBreakpoint
) {
314 // Print warning message if ignore smm entry break
316 DebugPortWriteBuffer (
317 (DEBUG_PORT_HANDLE
)(UINTN
)Mailbox
->DebugPortHandle
,
318 (UINT8
*)mWarningMsgIgnoreSmmEntryBreak
,
319 AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak
)
323 // If SMM entry break is set, SMM code will be break at here.
331 case DEBUG_AGENT_INIT_EXIT_SMI
:
332 Mailbox
= GetMailboxPointer ();
334 // Clear Skip Breakpoint flag
336 mSkipBreakpoint
= FALSE
;
337 RestoreDebugRegister ();
339 // Restore APIC Timer
341 if (mApicTimerRestore
) {
342 InitializeApicTimer (mApicTimerDivisor
, mTimerCycle
, mPeriodicMode
, mVector
);
343 mApicTimerRestore
= FALSE
;
348 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
:
349 if (Context
== NULL
) {
350 DEBUG ((DEBUG_ERROR
, "DebugAgent: Input parameter Context cannot be NULL!\n"));
353 Ia32Idtr
= (IA32_DESCRIPTOR
*)Context
;
354 Ia32IdtEntry
= (IA32_IDT_ENTRY
*)(Ia32Idtr
->Base
);
355 MailboxLocation
= (UINT64
*)((UINTN
)Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetLow
+
356 ((UINTN
)Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetHigh
<< 16));
357 mMailboxPointer
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
358 VerifyMailboxChecksum (mMailboxPointer
);
360 // Get original IDT address and size.
362 AsmReadIdtr ((IA32_DESCRIPTOR
*)&Idtr
);
363 IdtEntryCount
= (UINT16
)((Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
));
364 if (IdtEntryCount
< 33) {
365 Idtr
.Limit
= (UINT16
)(sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33 - 1);
366 Idtr
.Base
= (UINTN
)&mIdtEntryTable
;
367 ZeroMem (&mIdtEntryTable
, Idtr
.Limit
+ 1);
368 AsmWriteIdtr ((IA32_DESCRIPTOR
*)&Idtr
);
371 InitializeDebugIdt ();
373 // Initialize Debug Timer hardware and save its frequency
375 InitializeDebugTimer (&DebugTimerFrequency
, TRUE
);
376 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
378 // Enable Debug Timer interrupt and CPU interrupt
380 SaveAndSetDebugTimerInterrupt (TRUE
);
383 FindAndReportModuleImageInfo (SIZE_4KB
);
390 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
391 // Debug Agent library instance.
393 DEBUG ((DEBUG_ERROR
, "Debug Agent: The InitFlag value is not allowed!\n"));