]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugAgent/SmmDebugAgent/SmmDebugAgentLib.c
Removed unnecessary GLOBAL_REMOVE_IF_UNREFERENCED.
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / SmmDebugAgent / SmmDebugAgentLib.c
1 /** @file
2 Debug Agent library implementition.
3
4 Copyright (c) 2010, 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.
9
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.
12
13 **/
14
15 #include "SmmDebugAgentLib.h"
16
17 DEBUG_AGENT_MAILBOX *mMailboxPointer = NULL;
18 DEBUG_AGENT_MAILBOX mLocalMailbox;
19 UINTN mSavedDebugRegisters[6];
20 CONST BOOLEAN MultiProcessorDebugSupport = FALSE;
21
22
23 /**
24 Get Debug Agent Mailbox pointer.
25
26 @return Mailbox pointer.
27
28 **/
29 DEBUG_AGENT_MAILBOX *
30 GetMailboxPointer (
31 VOID
32 )
33 {
34 return mMailboxPointer;
35 }
36
37 /**
38 Get debug port handle.
39
40 @return Debug port handle.
41
42 **/
43 DEBUG_PORT_HANDLE
44 GetDebugPortHandle (
45 VOID
46 )
47 {
48 return (DEBUG_PORT_HANDLE) (UINTN)(mMailboxPointer->DebugPortHandle);
49 }
50
51 /**
52 Store debug register when SMI exit.
53
54 **/
55 VOID
56 SaveDebugRegister (
57 VOID
58 )
59 {
60 mSavedDebugRegisters[0] = AsmReadDr0 ();
61 mSavedDebugRegisters[1] = AsmReadDr1 ();
62 mSavedDebugRegisters[2] = AsmReadDr2 ();
63 mSavedDebugRegisters[3] = AsmReadDr3 ();
64 mSavedDebugRegisters[4] = AsmReadDr6 ();
65 mSavedDebugRegisters[5] = AsmReadDr7 ();
66 }
67
68 /**
69 Restore debug register when SMI exit.
70
71 **/
72 VOID
73 RestoreDebugRegister (
74 VOID
75 )
76 {
77 AsmWriteDr7 (0);
78 AsmWriteDr0 (mSavedDebugRegisters[0]);
79 AsmWriteDr1 (mSavedDebugRegisters[1]);
80 AsmWriteDr2 (mSavedDebugRegisters[2]);
81 AsmWriteDr3 (mSavedDebugRegisters[3]);
82 AsmWriteDr6 (mSavedDebugRegisters[4]);
83 AsmWriteDr7 (mSavedDebugRegisters[5]);
84 }
85
86 /**
87 Initialize debug agent.
88
89 This function is used to set up debug enviroment for source level debug
90 in SMM code.
91
92 If InitFlag is DEBUG_AGENT_INIT_SMM, it will overirde IDT table entries
93 and initialize debug port. It will get debug agent Mailbox from GUIDed HOB,
94 it it exists, debug agent wiil copied it into the local Mailbox in SMM space.
95 it will overirde IDT table entries and initialize debug port. Context will be
96 NULL.
97 If InitFlag is DEBUG_AGENT_INIT_ENTER_SMI, debug agent will save Debug
98 Registers and get local Mailbox in SMM space. Context will be NULL.
99 If InitFlag is DEBUG_AGENT_INIT_EXIT_SMI, debug agent will restore Debug
100 Registers. Context will be NULL.
101
102 @param[in] InitFlag Init flag is used to decide initialize process.
103 @param[in] Context Context needed according to InitFlag.
104 @param[in] Function Continue function called by debug agent library; it was
105 optional.
106
107 **/
108 VOID
109 EFIAPI
110 InitializeDebugAgent (
111 IN UINT32 InitFlag,
112 IN VOID *Context, OPTIONAL
113 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
114 )
115 {
116 EFI_STATUS Status;
117 UINT64 DebugPortHandle;
118
119 switch (InitFlag) {
120 case DEBUG_AGENT_INIT_SMM:
121 Status = EfiGetSystemConfigurationTable (&gEfiDebugAgentGuid, (VOID **) &mMailboxPointer);
122 if (EFI_ERROR (Status) || mMailboxPointer == NULL) {
123 ZeroMem (&mLocalMailbox, sizeof (DEBUG_AGENT_MAILBOX));
124 mMailboxPointer = &mLocalMailbox;
125 }
126
127 break;
128
129 case DEBUG_AGENT_INIT_ENTER_SMI:
130 SaveDebugRegister ();
131 InitializeDebugIdt ();
132
133 if (mMailboxPointer != NULL) {
134 //
135 // Initialize debug communication port
136 //
137 DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((DEBUG_PORT_HANDLE) (UINTN)mMailboxPointer->DebugPortHandle, NULL);
138 mMailboxPointer->DebugPortHandle = DebugPortHandle;
139
140 if (mMailboxPointer->DebugFlag.Bits.BreakOnNextSmi == 1) {
141 //
142 // If SMM entry break is set, SMM code will be break at here.
143 //
144 CpuBreakpoint ();
145 }
146 }
147 break;
148
149 case DEBUG_AGENT_INIT_EXIT_SMI:
150 RestoreDebugRegister ();
151 break;
152 }
153 }
154