]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
Fixed one bug: gSmmCorePrivate->InSmm is not set to FALSE correctly before exiting
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
1 /** @file
2 SMM Core Main Entry Point
3
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 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 "PiSmmCore.h"
16
17 //
18 // Physical pointer to private structure shared between SMM IPL and the SMM Core
19 //
20 SMM_CORE_PRIVATE_DATA *gSmmCorePrivate;
21
22 //
23 // SMM Core global variable for SMM System Table. Only accessed as a physical structure in SMRAM.
24 //
25 EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = {
26 {
27 SMM_SMST_SIGNATURE,
28 EFI_SMM_SYSTEM_TABLE2_REVISION,
29 sizeof (gSmmCoreSmst.Hdr)
30 },
31 NULL, // SmmFirmwareVendor
32 0, // SmmFirmwareRevision
33 SmmInstallConfigurationTable,
34 {
35 {
36 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmMemRead
37 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmMemWrite
38 },
39 {
40 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmIoRead
41 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmIoWrite
42 }
43 },
44 SmmAllocatePool,
45 SmmFreePool,
46 SmmAllocatePages,
47 SmmFreePages,
48 NULL, // SmmStartupThisAp
49 0, // CurrentlyExecutingCpu
50 0, // NumberOfCpus
51 NULL, // CpuSaveStateSize
52 NULL, // CpuSaveState
53 0, // NumberOfTableEntries
54 NULL, // SmmConfigurationTable
55 SmmInstallProtocolInterface,
56 SmmUninstallProtocolInterface,
57 SmmHandleProtocol,
58 SmmRegisterProtocolNotify,
59 SmmLocateHandle,
60 SmmLocateProtocol,
61 SmiManage,
62 SmiHandlerRegister,
63 SmiHandlerUnRegister
64 };
65
66 //
67 // Flag to determine if the platform has performed a legacy boot.
68 // If this flag is TRUE, then the runtime code and runtime data associated with the
69 // SMM IPL are converted to free memory, so the SMM COre must guarantee that is
70 // does not touch of the code/data associated with the SMM IPL if this flag is TRUE.
71 //
72 BOOLEAN mInLegacyBoot = FALSE;
73
74 //
75 // Table of SMI Handlers that are registered by the SMM Core when it is initialized
76 //
77 SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers[] = {
78 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },
79 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE },
80 { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE },
81 { NULL, NULL, NULL, FALSE }
82 };
83
84 /**
85 Place holder function until all the SMM System Table Service are available.
86
87 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
88
89 @param Arg1 Undefined
90 @param Arg2 Undefined
91 @param Arg3 Undefined
92 @param Arg4 Undefined
93 @param Arg5 Undefined
94
95 @return EFI_NOT_AVAILABLE_YET
96
97 **/
98 EFI_STATUS
99 EFIAPI
100 SmmEfiNotAvailableYetArg5 (
101 UINTN Arg1,
102 UINTN Arg2,
103 UINTN Arg3,
104 UINTN Arg4,
105 UINTN Arg5
106 )
107 {
108 //
109 // This function should never be executed. If it does, then the architectural protocols
110 // have not been designed correctly.
111 //
112 return EFI_NOT_AVAILABLE_YET;
113 }
114
115 /**
116 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM
117 Core uses this signal to know that a Legacy Boot has been performed and that
118 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can
119 not be accessed from SMM anymore since that structure is considered free memory by
120 a legacy OS.
121
122 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
123 @param Context Points to an optional handler context which was specified when the handler was registered.
124 @param CommBuffer A pointer to a collection of data in memory that will
125 be conveyed from a non-SMM environment into an SMM environment.
126 @param CommBufferSize The size of the CommBuffer.
127
128 @return Status Code
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 SmmLegacyBootHandler (
134 IN EFI_HANDLE DispatchHandle,
135 IN CONST VOID *Context, OPTIONAL
136 IN OUT VOID *CommBuffer, OPTIONAL
137 IN OUT UINTN *CommBufferSize OPTIONAL
138 )
139 {
140 mInLegacyBoot = TRUE;
141 return EFI_SUCCESS;
142 }
143
144 /**
145 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
146 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
147 Software SMIs that are nor required after SMRAM is locked and installs the
148 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
149 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed
150 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
151
152 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
153 @param Context Points to an optional handler context which was specified when the handler was registered.
154 @param CommBuffer A pointer to a collection of data in memory that will
155 be conveyed from a non-SMM environment into an SMM environment.
156 @param CommBufferSize The size of the CommBuffer.
157
158 @return Status Code
159
160 **/
161 EFI_STATUS
162 EFIAPI
163 SmmReadyToLockHandler (
164 IN EFI_HANDLE DispatchHandle,
165 IN CONST VOID *Context, OPTIONAL
166 IN OUT VOID *CommBuffer, OPTIONAL
167 IN OUT UINTN *CommBufferSize OPTIONAL
168 )
169 {
170 EFI_STATUS Status;
171 UINTN Index;
172 EFI_HANDLE SmmHandle;
173 VOID *Interface;
174
175 //
176 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
177 //
178 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
179 if (mSmmCoreSmiHandlers[Index].UnRegister) {
180 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
181 }
182 }
183
184 //
185 // Install SMM Ready to lock protocol
186 //
187 SmmHandle = NULL;
188 Status = SmmInstallProtocolInterface (
189 &SmmHandle,
190 &gEfiSmmReadyToLockProtocolGuid,
191 EFI_NATIVE_INTERFACE,
192 NULL
193 );
194
195 //
196 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
197 //
198 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);
199
200 //
201 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
202 //
203 DEBUG_CODE_BEGIN ();
204 if (EFI_ERROR (Status)) {
205 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
206 }
207 DEBUG_CODE_END ();
208
209 //
210 // Assert if the CPU I/O 2 Protocol is not installed
211 //
212 ASSERT_EFI_ERROR (Status);
213
214 //
215 // Display any drivers that were not dispatched because dependency expression
216 // evaluated to false if this is a debug build
217 //
218 DEBUG_CODE_BEGIN ();
219 SmmDisplayDiscoveredNotDispatched ();
220 DEBUG_CODE_END ();
221
222 //
223 // Not allowed to use gST or gBS after lock
224 //
225 gST = NULL;
226 gBS = NULL;
227
228 return Status;
229 }
230
231 /**
232 The main entry point to SMM Foundation.
233
234 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
235
236 @param SmmEntryContext Processor information and functionality
237 needed by SMM Foundation.
238
239 **/
240 VOID
241 EFIAPI
242 SmmEntryPoint (
243 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext
244 )
245 {
246 EFI_STATUS Status;
247 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
248 BOOLEAN InLegacyBoot;
249
250 PERF_START (NULL, "SMM", NULL, 0) ;
251
252 //
253 // Update SMST using the context
254 //
255 CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT));
256
257 //
258 // Call platform hook before Smm Dispatch
259 //
260 PlatformHookBeforeSmmDispatch ();
261
262 //
263 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
264 //
265 InLegacyBoot = mInLegacyBoot;
266 if (!InLegacyBoot) {
267 //
268 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
269 //
270 gSmmCorePrivate->InSmm = TRUE;
271
272 //
273 // Check to see if this is a Synchronous SMI sent through the SMM Communication
274 // Protocol or an Asynchronous SMI
275 //
276 if (gSmmCorePrivate->CommunicationBuffer != NULL) {
277 //
278 // Synchronous SMI for SMM Core or request from Communicate protocol
279 //
280 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;
281 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
282 Status = SmiManage (
283 &CommunicateHeader->HeaderGuid,
284 NULL,
285 CommunicateHeader->Data,
286 &gSmmCorePrivate->BufferSize
287 );
288
289 //
290 // Update CommunicationBuffer, BufferSize and ReturnStatus
291 // Communicate service finished, reset the pointer to CommBuffer to NULL
292 //
293 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
294 gSmmCorePrivate->CommunicationBuffer = NULL;
295 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
296 }
297 }
298
299 //
300 // Process Asynchronous SMI sources
301 //
302 SmiManage (NULL, NULL, NULL, NULL);
303
304 //
305 // Call platform hook after Smm Dispatch
306 //
307 PlatformHookAfterSmmDispatch ();
308
309 //
310 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
311 //
312 if (!InLegacyBoot) {
313 //
314 // Clear the InSmm flag as we are going to leave SMM
315 //
316 gSmmCorePrivate->InSmm = FALSE;
317 }
318
319 PERF_END (NULL, "SMM", NULL, 0) ;
320 }
321
322 /**
323 The Entry Point for SMM Core
324
325 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
326 EntryPoint on the SMI vector.
327
328 Note: This function is called for both DXE invocation and SMRAM invocation.
329
330 @param ImageHandle The firmware allocated handle for the EFI image.
331 @param SystemTable A pointer to the EFI System Table.
332
333 @retval EFI_SUCCESS The entry point is executed successfully.
334 @retval Other Some error occurred when executing this entry point.
335
336 **/
337 EFI_STATUS
338 EFIAPI
339 SmmMain (
340 IN EFI_HANDLE ImageHandle,
341 IN EFI_SYSTEM_TABLE *SystemTable
342 )
343 {
344 EFI_STATUS Status;
345 UINTN Index;
346
347 //
348 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
349 //
350 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
351
352 //
353 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
354 //
355 gSmmCorePrivate->Smst = &gSmmCoreSmst;
356 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
357
358 //
359 // Initialize memory service using free SMRAM
360 //
361 SmmInitializeMemoryServices (gSmmCorePrivate->SmramRangeCount, gSmmCorePrivate->SmramRanges);
362
363 //
364 // Register all SMI Handlers required by the SMM Core
365 //
366 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
367 Status = SmiHandlerRegister (
368 mSmmCoreSmiHandlers[Index].Handler,
369 mSmmCoreSmiHandlers[Index].HandlerType,
370 &mSmmCoreSmiHandlers[Index].DispatchHandle
371 );
372 ASSERT_EFI_ERROR (Status);
373 }
374
375 return EFI_SUCCESS;
376 }