2 Debug Agent library implementition.
4 Copyright (c) 2010 - 2016, 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
;
22 BOOLEAN mSmmDebugIdtInitFlag
= FALSE
;
24 CHAR8 mWarningMsgIgnoreSmmEntryBreak
[] = "Ignore smmentrybreak setting for SMI issued during DXE debugging!\r\n";
27 Check if debug agent support multi-processor.
29 @retval TRUE Multi-processor is supported.
30 @retval FALSE Multi-processor is not supported.
34 MultiProcessorDebugSupport (
42 Read the Attach/Break-in symbols from the debug port.
44 @param[in] Handle Pointer to Debug Port handle.
45 @param[out] BreakSymbol Returned break symbol.
47 @retval EFI_SUCCESS Read the symbol in BreakSymbol.
48 @retval EFI_NOT_FOUND No read the break symbol.
52 DebugReadBreakSymbol (
53 IN DEBUG_PORT_HANDLE Handle
,
54 OUT UINT8
*BreakSymbol
58 // Smm instance has no debug timer to poll break symbol.
64 Get the pointer to Mailbox from the GUIDed HOB.
66 @return Pointer to Mailbox.
74 EFI_HOB_GUID_TYPE
*GuidHob
;
75 UINT64
*MailboxLocation
;
76 DEBUG_AGENT_MAILBOX
*Mailbox
;
78 GuidHob
= GetFirstGuidHob (&gEfiDebugAgentGuid
);
79 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 enviroment for source level debug
159 If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde 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 overirde 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
;
194 BOOLEAN PeriodicMode
;
198 case DEBUG_AGENT_INIT_SMM
:
200 // Install configuration table for persisted vector handoff info
202 Status
= gSmst
->SmmInstallConfigurationTable (
204 &gEfiVectorHandoffTableGuid
,
205 (VOID
*) &mVectorHandoffInfoDebugAgent
[0],
206 sizeof (EFI_VECTOR_HANDOFF_INFO
) * mVectorHandoffInfoCount
208 if (EFI_ERROR (Status
)) {
209 DEBUG ((EFI_D_ERROR
, "DebugAgent: Cannot install configuration table for persisted vector handoff info!\n"));
213 // Check if Debug Agent initialized in DXE phase
215 Status
= EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid
, (VOID
**) &Mailbox
);
216 if (Status
== EFI_SUCCESS
&& Mailbox
!= NULL
) {
217 VerifyMailboxChecksum (Mailbox
);
218 mMailboxPointer
= Mailbox
;
222 // Check if Debug Agent initialized in SEC/PEI phase
224 Mailbox
= GetMailboxFromHob ();
225 if (Mailbox
!= NULL
) {
226 mMailboxPointer
= Mailbox
;
230 // Debug Agent was not initialized before, use the local mailbox.
232 ZeroMem (&mLocalMailbox
, sizeof (DEBUG_AGENT_MAILBOX
));
233 Mailbox
= &mLocalMailbox
;
235 // Save original IDT entries
237 AsmReadIdtr (&IdtDescriptor
);
238 CopyMem (&IdtEntry
, (VOID
*)IdtDescriptor
.Base
, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR
));
240 // Initialized Debug Agent
242 InitializeDebugIdt ();
244 // Initialize Debug Timer hardware and save its frequency
246 InitializeDebugTimer (&DebugTimerFrequency
, TRUE
);
247 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
249 DebugPortHandle
= (UINT64
) (UINTN
)DebugPortInitialize ((DEBUG_PORT_HANDLE
) (UINTN
)Mailbox
->DebugPortHandle
, NULL
);
250 UpdateMailboxContent (Mailbox
, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX
, DebugPortHandle
);
251 mMailboxPointer
= Mailbox
;
253 // Trigger one software interrupt to inform HOST
255 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE
);
257 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY
, 1);
259 // Memory has been ready
261 if (IsHostAttached ()) {
263 // Trigger one software interrupt to inform HOST
265 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE
);
268 // Find and report PE/COFF image info to HOST
270 FindAndReportModuleImageInfo (SIZE_4KB
);
272 // Restore saved IDT entries
274 CopyMem ((VOID
*)IdtDescriptor
.Base
, &IdtEntry
, 33 * sizeof(IA32_IDT_GATE_DESCRIPTOR
));
278 case DEBUG_AGENT_INIT_ENTER_SMI
:
279 SaveDebugRegister ();
280 if (!mSmmDebugIdtInitFlag
) {
282 // We only need to initialize Debug IDT table at first SMI entry
283 // after SMM relocation.
285 InitializeDebugIdt ();
286 mSmmDebugIdtInitFlag
= TRUE
;
289 // Check if CPU APIC Timer is working, otherwise initialize it.
291 InitializeLocalApicSoftwareEnable (TRUE
);
292 GetApicTimerState (NULL
, &PeriodicMode
, NULL
);
293 TimerCycle
= GetApicTimerInitCount ();
294 if (!PeriodicMode
|| TimerCycle
== 0) {
295 InitializeDebugTimer (NULL
, FALSE
);
297 Mailbox
= GetMailboxPointer ();
298 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS
) == 1) {
300 // If Debug Agent has been communicaton state with HOST, we need skip
301 // any break points set in SMM, set Skip Breakpoint flag
303 mSkipBreakpoint
= TRUE
;
305 if (GetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI
) == 1) {
306 if (mSkipBreakpoint
) {
308 // Print warning message if ignore smm entry break
310 DebugPortWriteBuffer ((DEBUG_PORT_HANDLE
) (UINTN
)Mailbox
->DebugPortHandle
,
311 (UINT8
*)mWarningMsgIgnoreSmmEntryBreak
,
312 AsciiStrLen (mWarningMsgIgnoreSmmEntryBreak
)
316 // If SMM entry break is set, SMM code will be break at here.
323 case DEBUG_AGENT_INIT_EXIT_SMI
:
324 Mailbox
= GetMailboxPointer ();
326 // Clear Skip Breakpoint flag
328 mSkipBreakpoint
= FALSE
;
329 RestoreDebugRegister ();
332 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
:
333 if (Context
== NULL
) {
334 DEBUG ((EFI_D_ERROR
, "DebugAgent: Input parameter Context cannot be NULL!\n"));
337 Ia32Idtr
= (IA32_DESCRIPTOR
*) Context
;
338 Ia32IdtEntry
= (IA32_IDT_ENTRY
*)(Ia32Idtr
->Base
);
339 MailboxLocation
= (UINT64
*) (UINTN
) (Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetLow
+
340 (UINT32
) (Ia32IdtEntry
[DEBUG_MAILBOX_VECTOR
].Bits
.OffsetHigh
<< 16));
341 mMailboxPointer
= (DEBUG_AGENT_MAILBOX
*)(UINTN
)(*MailboxLocation
);
342 VerifyMailboxChecksum (mMailboxPointer
);
344 // Get original IDT address and size.
346 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
347 IdtEntryCount
= (UINT16
) ((Idtr
.Limit
+ 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR
));
348 if (IdtEntryCount
< 33) {
349 Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 33 - 1);
350 Idtr
.Base
= (UINTN
) &mIdtEntryTable
;
351 ZeroMem (&mIdtEntryTable
, Idtr
.Limit
+ 1);
352 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Idtr
);
355 InitializeDebugIdt ();
357 // Initialize Debug Timer hardware and save its frequency
359 InitializeDebugTimer (&DebugTimerFrequency
, TRUE
);
360 UpdateMailboxContent (mMailboxPointer
, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY
, DebugTimerFrequency
);
362 // Enable Debug Timer interrupt and CPU interrupt
364 SaveAndSetDebugTimerInterrupt (TRUE
);
367 FindAndReportModuleImageInfo (SIZE_4KB
);
373 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this
374 // Debug Agent library instance.
376 DEBUG ((EFI_D_ERROR
, "Debug Agent: The InitFlag value is not allowed!\n"));