2 The internal header file includes the common header files, defines
3 internal structure and functions used by SmmCore module.
5 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Protocol/DxeSmmReadyToLock.h>
22 #include <Protocol/SmmReadyToLock.h>
23 #include <Protocol/SmmEndOfDxe.h>
24 #include <Protocol/CpuIo2.h>
25 #include <Protocol/SmmCommunication.h>
26 #include <Protocol/SmmAccess2.h>
27 #include <Protocol/FirmwareVolume2.h>
28 #include <Protocol/LoadedImage.h>
29 #include <Protocol/DevicePath.h>
30 #include <Protocol/Security.h>
31 #include <Protocol/Security2.h>
33 #include <Guid/Apriori.h>
34 #include <Guid/EventGroup.h>
35 #include <Guid/EventLegacyBios.h>
36 #include <Guid/ZeroGuid.h>
37 #include <Guid/MemoryProfile.h>
39 #include <Library/BaseLib.h>
40 #include <Library/BaseMemoryLib.h>
41 #include <Library/PeCoffLib.h>
42 #include <Library/CacheMaintenanceLib.h>
43 #include <Library/DebugLib.h>
44 #include <Library/ReportStatusCodeLib.h>
45 #include <Library/MemoryAllocationLib.h>
46 #include <Library/DevicePathLib.h>
47 #include <Library/UefiLib.h>
48 #include <Library/UefiBootServicesTableLib.h>
49 #include <Library/PcdLib.h>
50 #include <Library/SmmCorePlatformHookLib.h>
51 #include <Library/PerformanceLib.h>
52 #include <Library/TimerLib.h>
54 #include "PiSmmCorePrivateData.h"
57 // Used to build a table of SMI Handlers that the SMM Core registers
60 EFI_SMM_HANDLER_ENTRY_POINT2 Handler
;
61 EFI_GUID
*HandlerType
;
62 EFI_HANDLE DispatchHandle
;
64 } SMM_CORE_SMI_HANDLERS
;
67 // Structure for recording the state of an SMM Driver
69 #define EFI_SMM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
73 LIST_ENTRY Link
; // mDriverList
75 LIST_ENTRY ScheduledLink
; // mScheduledQueue
79 EFI_DEVICE_PATH_PROTOCOL
*FvFileDevicePath
;
80 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
87 EFI_GUID BeforeAfterGuid
;
92 BOOLEAN DepexProtocolError
;
94 EFI_HANDLE ImageHandle
;
95 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
97 // Image EntryPoint in SMRAM
99 PHYSICAL_ADDRESS ImageEntryPoint
;
101 // Image Buffer in SMRAM
103 PHYSICAL_ADDRESS ImageBuffer
;
108 } EFI_SMM_DRIVER_ENTRY
;
110 #define EFI_HANDLE_SIGNATURE SIGNATURE_32('h','n','d','l')
113 /// IHANDLE - contains a list of protocol handles
117 /// All handles list of IHANDLE
118 LIST_ENTRY AllHandles
;
119 /// List of PROTOCOL_INTERFACE's for this handle
120 LIST_ENTRY Protocols
;
124 #define ASSERT_IS_HANDLE(a) ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
126 #define PROTOCOL_ENTRY_SIGNATURE SIGNATURE_32('p','r','t','e')
129 /// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
130 /// database. Each handler that supports this protocol is listed, along
131 /// with a list of registered notifies.
135 /// Link Entry inserted to mProtocolDatabase
136 LIST_ENTRY AllEntries
;
137 /// ID of the protocol
139 /// All protocol interfaces
140 LIST_ENTRY Protocols
;
141 /// Registerd notification handlers
145 #define PROTOCOL_INTERFACE_SIGNATURE SIGNATURE_32('p','i','f','c')
148 /// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
149 /// with a protocol interface structure
153 /// Link on IHANDLE.Protocols
157 /// Link on PROTOCOL_ENTRY.Protocols
158 LIST_ENTRY ByProtocol
;
160 PROTOCOL_ENTRY
*Protocol
;
161 /// The interface value
163 } PROTOCOL_INTERFACE
;
165 #define PROTOCOL_NOTIFY_SIGNATURE SIGNATURE_32('p','r','t','n')
168 /// PROTOCOL_NOTIFY - used for each register notification for a protocol
172 PROTOCOL_ENTRY
*Protocol
;
173 /// All notifications for this protocol
175 /// Notification function
176 EFI_SMM_NOTIFY_FN Function
;
177 /// Last position notified
178 LIST_ENTRY
*Position
;
182 // SMM Core Global Variables
184 extern SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
;
185 extern EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst
;
186 extern LIST_ENTRY gHandleList
;
187 extern EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase
;
190 Called to initialize the memory service.
192 @param SmramRangeCount Number of SMRAM Regions
193 @param SmramRanges Pointer to SMRAM Descriptors
197 SmmInitializeMemoryServices (
198 IN UINTN SmramRangeCount
,
199 IN EFI_SMRAM_DESCRIPTOR
*SmramRanges
203 The SmmInstallConfigurationTable() function is used to maintain the list
204 of configuration tables that are stored in the System Management System
205 Table. The list is stored as an array of (GUID, Pointer) pairs. The list
206 must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
208 @param SystemTable A pointer to the SMM System Table (SMST).
209 @param Guid A pointer to the GUID for the entry to add, update, or remove.
210 @param Table A pointer to the buffer of the table to add.
211 @param TableSize The size of the table to install.
213 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
214 @retval EFI_INVALID_PARAMETER Guid is not valid.
215 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
216 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
221 SmmInstallConfigurationTable (
222 IN CONST EFI_SMM_SYSTEM_TABLE2
*SystemTable
,
223 IN CONST EFI_GUID
*Guid
,
229 Wrapper function to SmmInstallProtocolInterfaceNotify. This is the public API which
230 Calls the private one which contains a BOOLEAN parameter for notifications
232 @param UserHandle The handle to install the protocol handler on,
233 or NULL if a new handle is to be allocated
234 @param Protocol The protocol to add to the handle
235 @param InterfaceType Indicates whether Interface is supplied in
237 @param Interface The interface for the protocol being added
244 SmmInstallProtocolInterface (
245 IN OUT EFI_HANDLE
*UserHandle
,
246 IN EFI_GUID
*Protocol
,
247 IN EFI_INTERFACE_TYPE InterfaceType
,
252 Allocates pages from the memory map.
254 @param Type The type of allocation to perform
255 @param MemoryType The type of memory to turn the allocated pages
257 @param NumberOfPages The number of pages to allocate
258 @param Memory A pointer to receive the base allocated memory
261 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
262 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
263 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
264 @retval EFI_SUCCESS Pages successfully allocated.
270 IN EFI_ALLOCATE_TYPE Type
,
271 IN EFI_MEMORY_TYPE MemoryType
,
272 IN UINTN NumberOfPages
,
273 OUT EFI_PHYSICAL_ADDRESS
*Memory
277 Allocates pages from the memory map.
279 @param Type The type of allocation to perform
280 @param MemoryType The type of memory to turn the allocated pages
282 @param NumberOfPages The number of pages to allocate
283 @param Memory A pointer to receive the base allocated memory
286 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
287 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
288 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
289 @retval EFI_SUCCESS Pages successfully allocated.
294 SmmInternalAllocatePages (
295 IN EFI_ALLOCATE_TYPE Type
,
296 IN EFI_MEMORY_TYPE MemoryType
,
297 IN UINTN NumberOfPages
,
298 OUT EFI_PHYSICAL_ADDRESS
*Memory
302 Frees previous allocated pages.
304 @param Memory Base address of memory being freed
305 @param NumberOfPages The number of pages to free
307 @retval EFI_NOT_FOUND Could not find the entry that covers the range
308 @retval EFI_INVALID_PARAMETER Address not aligned
309 @return EFI_SUCCESS Pages successfully freed.
315 IN EFI_PHYSICAL_ADDRESS Memory
,
316 IN UINTN NumberOfPages
320 Frees previous allocated pages.
322 @param Memory Base address of memory being freed
323 @param NumberOfPages The number of pages to free
325 @retval EFI_NOT_FOUND Could not find the entry that covers the range
326 @retval EFI_INVALID_PARAMETER Address not aligned
327 @return EFI_SUCCESS Pages successfully freed.
332 SmmInternalFreePages (
333 IN EFI_PHYSICAL_ADDRESS Memory
,
334 IN UINTN NumberOfPages
338 Allocate pool of a particular type.
340 @param PoolType Type of pool to allocate
341 @param Size The amount of pool to allocate
342 @param Buffer The address to return a pointer to the allocated
345 @retval EFI_INVALID_PARAMETER PoolType not valid
346 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
347 @retval EFI_SUCCESS Pool successfully allocated.
353 IN EFI_MEMORY_TYPE PoolType
,
359 Allocate pool of a particular type.
361 @param PoolType Type of pool to allocate
362 @param Size The amount of pool to allocate
363 @param Buffer The address to return a pointer to the allocated
366 @retval EFI_INVALID_PARAMETER PoolType not valid
367 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
368 @retval EFI_SUCCESS Pool successfully allocated.
373 SmmInternalAllocatePool (
374 IN EFI_MEMORY_TYPE PoolType
,
382 @param Buffer The allocated pool entry to free
384 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
385 @retval EFI_SUCCESS Pool successfully freed.
397 @param Buffer The allocated pool entry to free
399 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
400 @retval EFI_SUCCESS Pool successfully freed.
405 SmmInternalFreePool (
410 Installs a protocol interface into the boot services environment.
412 @param UserHandle The handle to install the protocol handler on,
413 or NULL if a new handle is to be allocated
414 @param Protocol The protocol to add to the handle
415 @param InterfaceType Indicates whether Interface is supplied in
417 @param Interface The interface for the protocol being added
418 @param Notify indicates whether notify the notification list
421 @retval EFI_INVALID_PARAMETER Invalid parameter
422 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
423 @retval EFI_SUCCESS Protocol interface successfully installed
427 SmmInstallProtocolInterfaceNotify (
428 IN OUT EFI_HANDLE
*UserHandle
,
429 IN EFI_GUID
*Protocol
,
430 IN EFI_INTERFACE_TYPE InterfaceType
,
436 Uninstalls all instances of a protocol:interfacer from a handle.
437 If the last protocol interface is remove from the handle, the
440 @param UserHandle The handle to remove the protocol handler from
441 @param Protocol The protocol, of protocol:interface, to remove
442 @param Interface The interface, of protocol:interface, to remove
444 @retval EFI_INVALID_PARAMETER Protocol is NULL.
445 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
450 SmmUninstallProtocolInterface (
451 IN EFI_HANDLE UserHandle
,
452 IN EFI_GUID
*Protocol
,
457 Queries a handle to determine if it supports a specified protocol.
459 @param UserHandle The handle being queried.
460 @param Protocol The published unique identifier of the protocol.
461 @param Interface Supplies the address where a pointer to the
462 corresponding Protocol Interface is returned.
464 @return The requested protocol interface for the handle
470 IN EFI_HANDLE UserHandle
,
471 IN EFI_GUID
*Protocol
,
476 Add a new protocol notification record for the request protocol.
478 @param Protocol The requested protocol to add the notify
480 @param Function Points to the notification function
481 @param Registration Returns the registration record
483 @retval EFI_INVALID_PARAMETER Invalid parameter
484 @retval EFI_SUCCESS Successfully returned the registration record
490 SmmRegisterProtocolNotify (
491 IN CONST EFI_GUID
*Protocol
,
492 IN EFI_SMM_NOTIFY_FN Function
,
493 OUT VOID
**Registration
497 Locates the requested handle(s) and returns them in Buffer.
499 @param SearchType The type of search to perform to locate the
501 @param Protocol The protocol to search for
502 @param SearchKey Dependant on SearchType
503 @param BufferSize On input the size of Buffer. On output the
504 size of data returned.
505 @param Buffer The buffer to return the results in
507 @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is
508 returned in BufferSize.
509 @retval EFI_INVALID_PARAMETER Invalid parameter
510 @retval EFI_SUCCESS Successfully found the requested handle(s) and
511 returns them in Buffer.
517 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
518 IN EFI_GUID
*Protocol OPTIONAL
,
519 IN VOID
*SearchKey OPTIONAL
,
520 IN OUT UINTN
*BufferSize
,
521 OUT EFI_HANDLE
*Buffer
525 Return the first Protocol Interface that matches the Protocol GUID. If
526 Registration is pasased in return a Protocol Instance that was just add
527 to the system. If Retistration is NULL return the first Protocol Interface
530 @param Protocol The protocol to search for
531 @param Registration Optional Registration Key returned from
532 RegisterProtocolNotify()
533 @param Interface Return the Protocol interface (instance).
535 @retval EFI_SUCCESS If a valid Interface is returned
536 @retval EFI_INVALID_PARAMETER Invalid parameter
537 @retval EFI_NOT_FOUND Protocol interface not found
543 IN EFI_GUID
*Protocol
,
544 IN VOID
*Registration OPTIONAL
,
549 Manage SMI of a particular type.
551 @param HandlerType Points to the handler type or NULL for root SMI handlers.
552 @param Context Points to an optional context buffer.
553 @param CommBuffer Points to the optional communication buffer.
554 @param CommBufferSize Points to the size of the optional communication buffer.
556 @retval EFI_SUCCESS Interrupt source was processed successfully but not quiesced.
557 @retval EFI_INTERRUPT_PENDING One or more SMI sources could not be quiesced.
558 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING Interrupt source was not handled or quiesced.
559 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
565 IN CONST EFI_GUID
*HandlerType
,
566 IN CONST VOID
*Context OPTIONAL
,
567 IN OUT VOID
*CommBuffer OPTIONAL
,
568 IN OUT UINTN
*CommBufferSize OPTIONAL
572 Registers a handler to execute within SMM.
574 @param Handler Handler service funtion pointer.
575 @param HandlerType Points to the handler type or NULL for root SMI handlers.
576 @param DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
578 @retval EFI_SUCCESS Handler register success.
579 @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
585 IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler
,
586 IN CONST EFI_GUID
*HandlerType OPTIONAL
,
587 OUT EFI_HANDLE
*DispatchHandle
591 Unregister a handler in SMM.
593 @param DispatchHandle The handle that was specified when the handler was registered.
595 @retval EFI_SUCCESS Handler function was successfully unregistered.
596 @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
601 SmiHandlerUnRegister (
602 IN EFI_HANDLE DispatchHandle
606 This function is the main entry point for an SMM handler dispatch
607 or communicate-based callback.
609 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
610 @param Context Points to an optional handler context which was specified when the handler was registered.
611 @param CommBuffer A pointer to a collection of data in memory that will
612 be conveyed from a non-SMM environment into an SMM environment.
613 @param CommBufferSize The size of the CommBuffer.
620 SmmDriverDispatchHandler (
621 IN EFI_HANDLE DispatchHandle
,
622 IN CONST VOID
*Context
, OPTIONAL
623 IN OUT VOID
*CommBuffer
, OPTIONAL
624 IN OUT UINTN
*CommBufferSize OPTIONAL
628 This function is the main entry point for an SMM handler dispatch
629 or communicate-based callback.
631 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
632 @param Context Points to an optional handler context which was specified when the handler was registered.
633 @param CommBuffer A pointer to a collection of data in memory that will
634 be conveyed from a non-SMM environment into an SMM environment.
635 @param CommBufferSize The size of the CommBuffer.
642 SmmLegacyBootHandler (
643 IN EFI_HANDLE DispatchHandle
,
644 IN CONST VOID
*Context
, OPTIONAL
645 IN OUT VOID
*CommBuffer
, OPTIONAL
646 IN OUT UINTN
*CommBufferSize OPTIONAL
650 This function is the main entry point for an SMM handler dispatch
651 or communicate-based callback.
653 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
654 @param Context Points to an optional handler context which was specified when the handler was registered.
655 @param CommBuffer A pointer to a collection of data in memory that will
656 be conveyed from a non-SMM environment into an SMM environment.
657 @param CommBufferSize The size of the CommBuffer.
664 SmmReadyToLockHandler (
665 IN EFI_HANDLE DispatchHandle
,
666 IN CONST VOID
*Context
, OPTIONAL
667 IN OUT VOID
*CommBuffer
, OPTIONAL
668 IN OUT UINTN
*CommBufferSize OPTIONAL
672 This function is the main entry point for an SMM handler dispatch
673 or communicate-based callback.
675 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
676 @param Context Points to an optional handler context which was specified when the handler was registered.
677 @param CommBuffer A pointer to a collection of data in memory that will
678 be conveyed from a non-SMM environment into an SMM environment.
679 @param CommBufferSize The size of the CommBuffer.
687 IN EFI_HANDLE DispatchHandle
,
688 IN CONST VOID
*Context
, OPTIONAL
689 IN OUT VOID
*CommBuffer
, OPTIONAL
690 IN OUT UINTN
*CommBufferSize OPTIONAL
694 Place holder function until all the SMM System Table Service are available.
696 @param Arg1 Undefined
697 @param Arg2 Undefined
698 @param Arg3 Undefined
699 @param Arg4 Undefined
700 @param Arg5 Undefined
702 @return EFI_NOT_AVAILABLE_YET
707 SmmEfiNotAvailableYetArg5 (
716 //Functions used during debug buils
720 Traverse the discovered list for any drivers that were discovered but not loaded
721 because the dependency experessions evaluated to false.
725 SmmDisplayDiscoveredNotDispatched (
730 Add free SMRAM region for use by memory service.
732 @param MemBase Base address of memory region.
733 @param MemLength Length of the memory region.
734 @param Type Memory type.
735 @param Attributes Memory region state.
740 IN EFI_PHYSICAL_ADDRESS MemBase
,
742 IN EFI_MEMORY_TYPE Type
,
747 Finds the protocol entry for the requested protocol.
749 @param Protocol The ID of the protocol
750 @param Create Create a new entry if not found
752 @return Protocol entry
756 SmmFindProtocolEntry (
757 IN EFI_GUID
*Protocol
,
762 Signal event for every protocol in protocol entry.
764 @param Prot Protocol interface
769 IN PROTOCOL_INTERFACE
*Prot
773 Finds the protocol instance for the requested handle and protocol.
774 Note: This function doesn't do parameters checking, it's caller's responsibility
775 to pass in valid parameters.
777 @param Handle The handle to search the protocol on
778 @param Protocol GUID of the protocol
779 @param Interface The interface for the protocol being searched
781 @return Protocol instance (NULL: Not found)
785 SmmFindProtocolInterface (
787 IN EFI_GUID
*Protocol
,
792 Removes Protocol from the protocol list (but not the handle list).
794 @param Handle The handle to remove protocol on.
795 @param Protocol GUID of the protocol to be moved
796 @param Interface The interface of the protocol
798 @return Protocol Entry
802 SmmRemoveInterfaceFromProtocol (
804 IN EFI_GUID
*Protocol
,
809 This is the POSTFIX version of the dependency evaluator. This code does
810 not need to handle Before or After, as it is not valid to call this
811 routine in this case. POSTFIX means all the math is done on top of the stack.
813 @param DriverEntry DriverEntry element to update.
815 @retval TRUE If driver is ready to run.
816 @retval FALSE If driver is not ready to run or some fatal error
822 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
830 Initialize SMRAM profile.
839 Register SMM image to SMRAM profile.
841 @param DriverEntry SMM image info.
842 @param RegisterToDxe Register image to DXE.
844 @retval TRUE Register success.
845 @retval FALSE Register fail.
849 RegisterSmramProfileImage (
850 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
,
851 IN BOOLEAN RegisterToDxe
855 Unregister image from SMRAM profile.
857 @param DriverEntry SMM image info.
858 @param UnregisterToDxe Unregister image from DXE.
860 @retval TRUE Unregister success.
861 @retval FALSE Unregister fail.
865 UnregisterSmramProfileImage (
866 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
,
867 IN BOOLEAN UnregisterToDxe
871 Update SMRAM profile information.
873 @param CallerAddress Address of caller who call Allocate or Free.
874 @param Action This Allocate or Free action.
875 @param MemoryType Memory type.
876 @param Size Buffer size.
877 @param Buffer Buffer address.
879 @retval TRUE Profile udpate success.
880 @retval FALSE Profile update fail.
884 SmmCoreUpdateProfile (
885 IN EFI_PHYSICAL_ADDRESS CallerAddress
,
886 IN MEMORY_PROFILE_ACTION Action
,
887 IN EFI_MEMORY_TYPE MemoryType
, // Valid for AllocatePages/AllocatePool
888 IN UINTN Size
, // Valid for AllocatePages/FreePages/AllocatePool
893 Register SMRAM profile handler.
897 RegisterSmramProfileHandler (
902 SMRAM profile ready to lock callback function.
906 SmramProfileReadyToLock (
911 Dump SMRAM infromation.
919 extern UINTN mFullSmramRangeCount
;
920 extern EFI_SMRAM_DESCRIPTOR
*mFullSmramRanges
;