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