]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
Add PI1.2.1 EndOfDxe support.
[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 { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, FALSE },
82 { NULL, NULL, NULL, FALSE }
83 };
84
85 /**
86 Place holder function until all the SMM System Table Service are available.
87
88 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
89
90 @param Arg1 Undefined
91 @param Arg2 Undefined
92 @param Arg3 Undefined
93 @param Arg4 Undefined
94 @param Arg5 Undefined
95
96 @return EFI_NOT_AVAILABLE_YET
97
98 **/
99 EFI_STATUS
100 EFIAPI
101 SmmEfiNotAvailableYetArg5 (
102 UINTN Arg1,
103 UINTN Arg2,
104 UINTN Arg3,
105 UINTN Arg4,
106 UINTN Arg5
107 )
108 {
109 //
110 // This function should never be executed. If it does, then the architectural protocols
111 // have not been designed correctly.
112 //
113 return EFI_NOT_AVAILABLE_YET;
114 }
115
116 /**
117 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM
118 Core uses this signal to know that a Legacy Boot has been performed and that
119 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can
120 not be accessed from SMM anymore since that structure is considered free memory by
121 a legacy OS.
122
123 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
124 @param Context Points to an optional handler context which was specified when the handler was registered.
125 @param CommBuffer A pointer to a collection of data in memory that will
126 be conveyed from a non-SMM environment into an SMM environment.
127 @param CommBufferSize The size of the CommBuffer.
128
129 @return Status Code
130
131 **/
132 EFI_STATUS
133 EFIAPI
134 SmmLegacyBootHandler (
135 IN EFI_HANDLE DispatchHandle,
136 IN CONST VOID *Context, OPTIONAL
137 IN OUT VOID *CommBuffer, OPTIONAL
138 IN OUT UINTN *CommBufferSize OPTIONAL
139 )
140 {
141 mInLegacyBoot = TRUE;
142 return EFI_SUCCESS;
143 }
144
145 /**
146 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added
147 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the
148 Software SMIs that are nor required after SMRAM is locked and installs the
149 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about
150 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed
151 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.
152
153 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
154 @param Context Points to an optional handler context which was specified when the handler was registered.
155 @param CommBuffer A pointer to a collection of data in memory that will
156 be conveyed from a non-SMM environment into an SMM environment.
157 @param CommBufferSize The size of the CommBuffer.
158
159 @return Status Code
160
161 **/
162 EFI_STATUS
163 EFIAPI
164 SmmReadyToLockHandler (
165 IN EFI_HANDLE DispatchHandle,
166 IN CONST VOID *Context, OPTIONAL
167 IN OUT VOID *CommBuffer, OPTIONAL
168 IN OUT UINTN *CommBufferSize OPTIONAL
169 )
170 {
171 EFI_STATUS Status;
172 UINTN Index;
173 EFI_HANDLE SmmHandle;
174 VOID *Interface;
175
176 //
177 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped
178 //
179 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
180 if (mSmmCoreSmiHandlers[Index].UnRegister) {
181 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);
182 }
183 }
184
185 //
186 // Install SMM Ready to lock protocol
187 //
188 SmmHandle = NULL;
189 Status = SmmInstallProtocolInterface (
190 &SmmHandle,
191 &gEfiSmmReadyToLockProtocolGuid,
192 EFI_NATIVE_INTERFACE,
193 NULL
194 );
195
196 //
197 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database
198 //
199 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);
200
201 //
202 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed
203 //
204 DEBUG_CODE_BEGIN ();
205 if (EFI_ERROR (Status)) {
206 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));
207 }
208 DEBUG_CODE_END ();
209
210 //
211 // Assert if the CPU I/O 2 Protocol is not installed
212 //
213 ASSERT_EFI_ERROR (Status);
214
215 //
216 // Display any drivers that were not dispatched because dependency expression
217 // evaluated to false if this is a debug build
218 //
219 DEBUG_CODE_BEGIN ();
220 SmmDisplayDiscoveredNotDispatched ();
221 DEBUG_CODE_END ();
222
223 //
224 // Not allowed to use gST or gBS after lock
225 //
226 gST = NULL;
227 gBS = NULL;
228
229 return Status;
230 }
231
232 /**
233 Software SMI handler that is called when the EndOfDxe event is signalled.
234 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that
235 platform code will invoke 3rd part code.
236
237 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
238 @param Context Points to an optional handler context which was specified when the handler was registered.
239 @param CommBuffer A pointer to a collection of data in memory that will
240 be conveyed from a non-SMM environment into an SMM environment.
241 @param CommBufferSize The size of the CommBuffer.
242
243 @return Status Code
244
245 **/
246 EFI_STATUS
247 EFIAPI
248 SmmEndOfDxeHandler (
249 IN EFI_HANDLE DispatchHandle,
250 IN CONST VOID *Context, OPTIONAL
251 IN OUT VOID *CommBuffer, OPTIONAL
252 IN OUT UINTN *CommBufferSize OPTIONAL
253 )
254 {
255 EFI_STATUS Status;
256 EFI_HANDLE SmmHandle;
257
258 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));
259 //
260 // Install SMM EndOfDxe protocol
261 //
262 SmmHandle = NULL;
263 Status = SmmInstallProtocolInterface (
264 &SmmHandle,
265 &gEfiSmmEndOfDxeProtocolGuid,
266 EFI_NATIVE_INTERFACE,
267 NULL
268 );
269 return EFI_SUCCESS;
270 }
271
272 /**
273 The main entry point to SMM Foundation.
274
275 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.
276
277 @param SmmEntryContext Processor information and functionality
278 needed by SMM Foundation.
279
280 **/
281 VOID
282 EFIAPI
283 SmmEntryPoint (
284 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext
285 )
286 {
287 EFI_STATUS Status;
288 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
289 BOOLEAN InLegacyBoot;
290
291 PERF_START (NULL, "SMM", NULL, 0) ;
292
293 //
294 // Update SMST using the context
295 //
296 CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT));
297
298 //
299 // Call platform hook before Smm Dispatch
300 //
301 PlatformHookBeforeSmmDispatch ();
302
303 //
304 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
305 //
306 InLegacyBoot = mInLegacyBoot;
307 if (!InLegacyBoot) {
308 //
309 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol
310 //
311 gSmmCorePrivate->InSmm = TRUE;
312
313 //
314 // Check to see if this is a Synchronous SMI sent through the SMM Communication
315 // Protocol or an Asynchronous SMI
316 //
317 if (gSmmCorePrivate->CommunicationBuffer != NULL) {
318 //
319 // Synchronous SMI for SMM Core or request from Communicate protocol
320 //
321 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;
322 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
323 Status = SmiManage (
324 &CommunicateHeader->HeaderGuid,
325 NULL,
326 CommunicateHeader->Data,
327 &gSmmCorePrivate->BufferSize
328 );
329
330 //
331 // Update CommunicationBuffer, BufferSize and ReturnStatus
332 // Communicate service finished, reset the pointer to CommBuffer to NULL
333 //
334 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
335 gSmmCorePrivate->CommunicationBuffer = NULL;
336 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
337 }
338 }
339
340 //
341 // Process Asynchronous SMI sources
342 //
343 SmiManage (NULL, NULL, NULL, NULL);
344
345 //
346 // Call platform hook after Smm Dispatch
347 //
348 PlatformHookAfterSmmDispatch ();
349
350 //
351 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed
352 //
353 if (!InLegacyBoot) {
354 //
355 // Clear the InSmm flag as we are going to leave SMM
356 //
357 gSmmCorePrivate->InSmm = FALSE;
358 }
359
360 PERF_END (NULL, "SMM", NULL, 0) ;
361 }
362
363 /**
364 The Entry Point for SMM Core
365
366 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core
367 EntryPoint on the SMI vector.
368
369 Note: This function is called for both DXE invocation and SMRAM invocation.
370
371 @param ImageHandle The firmware allocated handle for the EFI image.
372 @param SystemTable A pointer to the EFI System Table.
373
374 @retval EFI_SUCCESS The entry point is executed successfully.
375 @retval Other Some error occurred when executing this entry point.
376
377 **/
378 EFI_STATUS
379 EFIAPI
380 SmmMain (
381 IN EFI_HANDLE ImageHandle,
382 IN EFI_SYSTEM_TABLE *SystemTable
383 )
384 {
385 EFI_STATUS Status;
386 UINTN Index;
387
388 //
389 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.
390 //
391 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
392
393 //
394 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.
395 //
396 gSmmCorePrivate->Smst = &gSmmCoreSmst;
397 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
398
399 //
400 // Initialize memory service using free SMRAM
401 //
402 SmmInitializeMemoryServices (gSmmCorePrivate->SmramRangeCount, gSmmCorePrivate->SmramRanges);
403
404 //
405 // Register all SMI Handlers required by the SMM Core
406 //
407 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {
408 Status = SmiHandlerRegister (
409 mSmmCoreSmiHandlers[Index].Handler,
410 mSmmCoreSmiHandlers[Index].HandlerType,
411 &mSmmCoreSmiHandlers[Index].DispatchHandle
412 );
413 ASSERT_EFI_ERROR (Status);
414 }
415
416 return EFI_SUCCESS;
417 }