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