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>
53 #include <Library/HobLib.h>
54 #include <Library/SmmMemLib.h>
56 #include "PiSmmCorePrivateData.h"
59 // Used to build a table of SMI Handlers that the SMM Core registers
62 EFI_SMM_HANDLER_ENTRY_POINT2 Handler
;
63 EFI_GUID
*HandlerType
;
64 EFI_HANDLE DispatchHandle
;
66 } SMM_CORE_SMI_HANDLERS
;
69 // Structure for recording the state of an SMM Driver
71 #define EFI_SMM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
75 LIST_ENTRY Link
; // mDriverList
77 LIST_ENTRY ScheduledLink
; // mScheduledQueue
81 EFI_DEVICE_PATH_PROTOCOL
*FvFileDevicePath
;
82 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
89 EFI_GUID BeforeAfterGuid
;
94 BOOLEAN DepexProtocolError
;
96 EFI_HANDLE ImageHandle
;
97 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
99 // Image EntryPoint in SMRAM
101 PHYSICAL_ADDRESS ImageEntryPoint
;
103 // Image Buffer in SMRAM
105 PHYSICAL_ADDRESS ImageBuffer
;
110 } EFI_SMM_DRIVER_ENTRY
;
112 #define EFI_HANDLE_SIGNATURE SIGNATURE_32('h','n','d','l')
115 /// IHANDLE - contains a list of protocol handles
119 /// All handles list of IHANDLE
120 LIST_ENTRY AllHandles
;
121 /// List of PROTOCOL_INTERFACE's for this handle
122 LIST_ENTRY Protocols
;
126 #define ASSERT_IS_HANDLE(a) ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
128 #define PROTOCOL_ENTRY_SIGNATURE SIGNATURE_32('p','r','t','e')
131 /// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
132 /// database. Each handler that supports this protocol is listed, along
133 /// with a list of registered notifies.
137 /// Link Entry inserted to mProtocolDatabase
138 LIST_ENTRY AllEntries
;
139 /// ID of the protocol
141 /// All protocol interfaces
142 LIST_ENTRY Protocols
;
143 /// Registerd notification handlers
147 #define PROTOCOL_INTERFACE_SIGNATURE SIGNATURE_32('p','i','f','c')
150 /// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
151 /// with a protocol interface structure
155 /// Link on IHANDLE.Protocols
159 /// Link on PROTOCOL_ENTRY.Protocols
160 LIST_ENTRY ByProtocol
;
162 PROTOCOL_ENTRY
*Protocol
;
163 /// The interface value
165 } PROTOCOL_INTERFACE
;
167 #define PROTOCOL_NOTIFY_SIGNATURE SIGNATURE_32('p','r','t','n')
170 /// PROTOCOL_NOTIFY - used for each register notification for a protocol
174 PROTOCOL_ENTRY
*Protocol
;
175 /// All notifications for this protocol
177 /// Notification function
178 EFI_SMM_NOTIFY_FN Function
;
179 /// Last position notified
180 LIST_ENTRY
*Position
;
184 // SMM Core Global Variables
186 extern SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
;
187 extern EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst
;
188 extern LIST_ENTRY gHandleList
;
189 extern EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase
;
192 Called to initialize the memory service.
194 @param SmramRangeCount Number of SMRAM Regions
195 @param SmramRanges Pointer to SMRAM Descriptors
199 SmmInitializeMemoryServices (
200 IN UINTN SmramRangeCount
,
201 IN EFI_SMRAM_DESCRIPTOR
*SmramRanges
205 The SmmInstallConfigurationTable() function is used to maintain the list
206 of configuration tables that are stored in the System Management System
207 Table. The list is stored as an array of (GUID, Pointer) pairs. The list
208 must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
210 @param SystemTable A pointer to the SMM System Table (SMST).
211 @param Guid A pointer to the GUID for the entry to add, update, or remove.
212 @param Table A pointer to the buffer of the table to add.
213 @param TableSize The size of the table to install.
215 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
216 @retval EFI_INVALID_PARAMETER Guid is not valid.
217 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
218 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
223 SmmInstallConfigurationTable (
224 IN CONST EFI_SMM_SYSTEM_TABLE2
*SystemTable
,
225 IN CONST EFI_GUID
*Guid
,
231 Wrapper function to SmmInstallProtocolInterfaceNotify. This is the public API which
232 Calls the private one which contains a BOOLEAN parameter for notifications
234 @param UserHandle The handle to install the protocol handler on,
235 or NULL if a new handle is to be allocated
236 @param Protocol The protocol to add to the handle
237 @param InterfaceType Indicates whether Interface is supplied in
239 @param Interface The interface for the protocol being added
246 SmmInstallProtocolInterface (
247 IN OUT EFI_HANDLE
*UserHandle
,
248 IN EFI_GUID
*Protocol
,
249 IN EFI_INTERFACE_TYPE InterfaceType
,
254 Allocates pages from the memory map.
256 @param Type The type of allocation to perform
257 @param MemoryType The type of memory to turn the allocated pages
259 @param NumberOfPages The number of pages to allocate
260 @param Memory A pointer to receive the base allocated memory
263 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
264 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
265 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
266 @retval EFI_SUCCESS Pages successfully allocated.
272 IN EFI_ALLOCATE_TYPE Type
,
273 IN EFI_MEMORY_TYPE MemoryType
,
274 IN UINTN NumberOfPages
,
275 OUT EFI_PHYSICAL_ADDRESS
*Memory
279 Allocates pages from the memory map.
281 @param Type The type of allocation to perform
282 @param MemoryType The type of memory to turn the allocated pages
284 @param NumberOfPages The number of pages to allocate
285 @param Memory A pointer to receive the base allocated memory
288 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
289 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
290 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
291 @retval EFI_SUCCESS Pages successfully allocated.
296 SmmInternalAllocatePages (
297 IN EFI_ALLOCATE_TYPE Type
,
298 IN EFI_MEMORY_TYPE MemoryType
,
299 IN UINTN NumberOfPages
,
300 OUT EFI_PHYSICAL_ADDRESS
*Memory
304 Frees previous allocated pages.
306 @param Memory Base address of memory being freed
307 @param NumberOfPages The number of pages to free
309 @retval EFI_NOT_FOUND Could not find the entry that covers the range
310 @retval EFI_INVALID_PARAMETER Address not aligned
311 @return EFI_SUCCESS Pages successfully freed.
317 IN EFI_PHYSICAL_ADDRESS Memory
,
318 IN UINTN NumberOfPages
322 Frees previous allocated pages.
324 @param Memory Base address of memory being freed
325 @param NumberOfPages The number of pages to free
327 @retval EFI_NOT_FOUND Could not find the entry that covers the range
328 @retval EFI_INVALID_PARAMETER Address not aligned
329 @return EFI_SUCCESS Pages successfully freed.
334 SmmInternalFreePages (
335 IN EFI_PHYSICAL_ADDRESS Memory
,
336 IN UINTN NumberOfPages
340 Allocate pool of a particular type.
342 @param PoolType Type of pool to allocate
343 @param Size The amount of pool to allocate
344 @param Buffer The address to return a pointer to the allocated
347 @retval EFI_INVALID_PARAMETER PoolType not valid
348 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
349 @retval EFI_SUCCESS Pool successfully allocated.
355 IN EFI_MEMORY_TYPE PoolType
,
361 Allocate pool of a particular type.
363 @param PoolType Type of pool to allocate
364 @param Size The amount of pool to allocate
365 @param Buffer The address to return a pointer to the allocated
368 @retval EFI_INVALID_PARAMETER PoolType not valid
369 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
370 @retval EFI_SUCCESS Pool successfully allocated.
375 SmmInternalAllocatePool (
376 IN EFI_MEMORY_TYPE PoolType
,
384 @param Buffer The allocated pool entry to free
386 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
387 @retval EFI_SUCCESS Pool successfully freed.
399 @param Buffer The allocated pool entry to free
401 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
402 @retval EFI_SUCCESS Pool successfully freed.
407 SmmInternalFreePool (
412 Installs a protocol interface into the boot services environment.
414 @param UserHandle The handle to install the protocol handler on,
415 or NULL if a new handle is to be allocated
416 @param Protocol The protocol to add to the handle
417 @param InterfaceType Indicates whether Interface is supplied in
419 @param Interface The interface for the protocol being added
420 @param Notify indicates whether notify the notification list
423 @retval EFI_INVALID_PARAMETER Invalid parameter
424 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
425 @retval EFI_SUCCESS Protocol interface successfully installed
429 SmmInstallProtocolInterfaceNotify (
430 IN OUT EFI_HANDLE
*UserHandle
,
431 IN EFI_GUID
*Protocol
,
432 IN EFI_INTERFACE_TYPE InterfaceType
,
438 Uninstalls all instances of a protocol:interfacer from a handle.
439 If the last protocol interface is remove from the handle, the
442 @param UserHandle The handle to remove the protocol handler from
443 @param Protocol The protocol, of protocol:interface, to remove
444 @param Interface The interface, of protocol:interface, to remove
446 @retval EFI_INVALID_PARAMETER Protocol is NULL.
447 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
452 SmmUninstallProtocolInterface (
453 IN EFI_HANDLE UserHandle
,
454 IN EFI_GUID
*Protocol
,
459 Queries a handle to determine if it supports a specified protocol.
461 @param UserHandle The handle being queried.
462 @param Protocol The published unique identifier of the protocol.
463 @param Interface Supplies the address where a pointer to the
464 corresponding Protocol Interface is returned.
466 @return The requested protocol interface for the handle
472 IN EFI_HANDLE UserHandle
,
473 IN EFI_GUID
*Protocol
,
478 Add a new protocol notification record for the request protocol.
480 @param Protocol The requested protocol to add the notify
482 @param Function Points to the notification function
483 @param Registration Returns the registration record
485 @retval EFI_INVALID_PARAMETER Invalid parameter
486 @retval EFI_SUCCESS Successfully returned the registration record
492 SmmRegisterProtocolNotify (
493 IN CONST EFI_GUID
*Protocol
,
494 IN EFI_SMM_NOTIFY_FN Function
,
495 OUT VOID
**Registration
499 Locates the requested handle(s) and returns them in Buffer.
501 @param SearchType The type of search to perform to locate the
503 @param Protocol The protocol to search for
504 @param SearchKey Dependant on SearchType
505 @param BufferSize On input the size of Buffer. On output the
506 size of data returned.
507 @param Buffer The buffer to return the results in
509 @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is
510 returned in BufferSize.
511 @retval EFI_INVALID_PARAMETER Invalid parameter
512 @retval EFI_SUCCESS Successfully found the requested handle(s) and
513 returns them in Buffer.
519 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
520 IN EFI_GUID
*Protocol OPTIONAL
,
521 IN VOID
*SearchKey OPTIONAL
,
522 IN OUT UINTN
*BufferSize
,
523 OUT EFI_HANDLE
*Buffer
527 Return the first Protocol Interface that matches the Protocol GUID. If
528 Registration is pasased in return a Protocol Instance that was just add
529 to the system. If Retistration is NULL return the first Protocol Interface
532 @param Protocol The protocol to search for
533 @param Registration Optional Registration Key returned from
534 RegisterProtocolNotify()
535 @param Interface Return the Protocol interface (instance).
537 @retval EFI_SUCCESS If a valid Interface is returned
538 @retval EFI_INVALID_PARAMETER Invalid parameter
539 @retval EFI_NOT_FOUND Protocol interface not found
545 IN EFI_GUID
*Protocol
,
546 IN VOID
*Registration OPTIONAL
,
551 Manage SMI of a particular type.
553 @param HandlerType Points to the handler type or NULL for root SMI handlers.
554 @param Context Points to an optional context buffer.
555 @param CommBuffer Points to the optional communication buffer.
556 @param CommBufferSize Points to the size of the optional communication buffer.
558 @retval EFI_SUCCESS Interrupt source was processed successfully but not quiesced.
559 @retval EFI_INTERRUPT_PENDING One or more SMI sources could not be quiesced.
560 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING Interrupt source was not handled or quiesced.
561 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
567 IN CONST EFI_GUID
*HandlerType
,
568 IN CONST VOID
*Context OPTIONAL
,
569 IN OUT VOID
*CommBuffer OPTIONAL
,
570 IN OUT UINTN
*CommBufferSize OPTIONAL
574 Registers a handler to execute within SMM.
576 @param Handler Handler service funtion pointer.
577 @param HandlerType Points to the handler type or NULL for root SMI handlers.
578 @param DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
580 @retval EFI_SUCCESS Handler register success.
581 @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
587 IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler
,
588 IN CONST EFI_GUID
*HandlerType OPTIONAL
,
589 OUT EFI_HANDLE
*DispatchHandle
593 Unregister a handler in SMM.
595 @param DispatchHandle The handle that was specified when the handler was registered.
597 @retval EFI_SUCCESS Handler function was successfully unregistered.
598 @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
603 SmiHandlerUnRegister (
604 IN EFI_HANDLE DispatchHandle
608 This function is the main entry point for an SMM handler dispatch
609 or communicate-based callback.
611 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
612 @param Context Points to an optional handler context which was specified when the handler was registered.
613 @param CommBuffer A pointer to a collection of data in memory that will
614 be conveyed from a non-SMM environment into an SMM environment.
615 @param CommBufferSize The size of the CommBuffer.
622 SmmDriverDispatchHandler (
623 IN EFI_HANDLE DispatchHandle
,
624 IN CONST VOID
*Context
, OPTIONAL
625 IN OUT VOID
*CommBuffer
, OPTIONAL
626 IN OUT UINTN
*CommBufferSize OPTIONAL
630 This function is the main entry point for an SMM handler dispatch
631 or communicate-based callback.
633 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
634 @param Context Points to an optional handler context which was specified when the handler was registered.
635 @param CommBuffer A pointer to a collection of data in memory that will
636 be conveyed from a non-SMM environment into an SMM environment.
637 @param CommBufferSize The size of the CommBuffer.
644 SmmLegacyBootHandler (
645 IN EFI_HANDLE DispatchHandle
,
646 IN CONST VOID
*Context
, OPTIONAL
647 IN OUT VOID
*CommBuffer
, OPTIONAL
648 IN OUT UINTN
*CommBufferSize OPTIONAL
652 This function is the main entry point for an SMM handler dispatch
653 or communicate-based callback.
655 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
656 @param Context Points to an optional handler context which was specified when the handler was registered.
657 @param CommBuffer A pointer to a collection of data in memory that will
658 be conveyed from a non-SMM environment into an SMM environment.
659 @param CommBufferSize The size of the CommBuffer.
666 SmmReadyToLockHandler (
667 IN EFI_HANDLE DispatchHandle
,
668 IN CONST VOID
*Context
, OPTIONAL
669 IN OUT VOID
*CommBuffer
, OPTIONAL
670 IN OUT UINTN
*CommBufferSize OPTIONAL
674 This function is the main entry point for an SMM handler dispatch
675 or communicate-based callback.
677 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
678 @param Context Points to an optional handler context which was specified when the handler was registered.
679 @param CommBuffer A pointer to a collection of data in memory that will
680 be conveyed from a non-SMM environment into an SMM environment.
681 @param CommBufferSize The size of the CommBuffer.
689 IN EFI_HANDLE DispatchHandle
,
690 IN CONST VOID
*Context
, OPTIONAL
691 IN OUT VOID
*CommBuffer
, OPTIONAL
692 IN OUT UINTN
*CommBufferSize OPTIONAL
696 Place holder function until all the SMM System Table Service are available.
698 @param Arg1 Undefined
699 @param Arg2 Undefined
700 @param Arg3 Undefined
701 @param Arg4 Undefined
702 @param Arg5 Undefined
704 @return EFI_NOT_AVAILABLE_YET
709 SmmEfiNotAvailableYetArg5 (
718 //Functions used during debug buils
722 Traverse the discovered list for any drivers that were discovered but not loaded
723 because the dependency experessions evaluated to false.
727 SmmDisplayDiscoveredNotDispatched (
732 Add free SMRAM region for use by memory service.
734 @param MemBase Base address of memory region.
735 @param MemLength Length of the memory region.
736 @param Type Memory type.
737 @param Attributes Memory region state.
742 IN EFI_PHYSICAL_ADDRESS MemBase
,
744 IN EFI_MEMORY_TYPE Type
,
749 Finds the protocol entry for the requested protocol.
751 @param Protocol The ID of the protocol
752 @param Create Create a new entry if not found
754 @return Protocol entry
758 SmmFindProtocolEntry (
759 IN EFI_GUID
*Protocol
,
764 Signal event for every protocol in protocol entry.
766 @param Prot Protocol interface
771 IN PROTOCOL_INTERFACE
*Prot
775 Finds the protocol instance for the requested handle and protocol.
776 Note: This function doesn't do parameters checking, it's caller's responsibility
777 to pass in valid parameters.
779 @param Handle The handle to search the protocol on
780 @param Protocol GUID of the protocol
781 @param Interface The interface for the protocol being searched
783 @return Protocol instance (NULL: Not found)
787 SmmFindProtocolInterface (
789 IN EFI_GUID
*Protocol
,
794 Removes Protocol from the protocol list (but not the handle list).
796 @param Handle The handle to remove protocol on.
797 @param Protocol GUID of the protocol to be moved
798 @param Interface The interface of the protocol
800 @return Protocol Entry
804 SmmRemoveInterfaceFromProtocol (
806 IN EFI_GUID
*Protocol
,
811 This is the POSTFIX version of the dependency evaluator. This code does
812 not need to handle Before or After, as it is not valid to call this
813 routine in this case. POSTFIX means all the math is done on top of the stack.
815 @param DriverEntry DriverEntry element to update.
817 @retval TRUE If driver is ready to run.
818 @retval FALSE If driver is not ready to run or some fatal error
824 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
832 Initialize SMRAM profile.
841 Register SMM image to SMRAM profile.
843 @param DriverEntry SMM image info.
844 @param RegisterToDxe Register image to DXE.
846 @retval TRUE Register success.
847 @retval FALSE Register fail.
851 RegisterSmramProfileImage (
852 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
,
853 IN BOOLEAN RegisterToDxe
857 Unregister image from SMRAM profile.
859 @param DriverEntry SMM image info.
860 @param UnregisterToDxe Unregister image from DXE.
862 @retval TRUE Unregister success.
863 @retval FALSE Unregister fail.
867 UnregisterSmramProfileImage (
868 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
,
869 IN BOOLEAN UnregisterToDxe
873 Update SMRAM profile information.
875 @param CallerAddress Address of caller who call Allocate or Free.
876 @param Action This Allocate or Free action.
877 @param MemoryType Memory type.
878 @param Size Buffer size.
879 @param Buffer Buffer address.
881 @retval TRUE Profile udpate success.
882 @retval FALSE Profile update fail.
886 SmmCoreUpdateProfile (
887 IN EFI_PHYSICAL_ADDRESS CallerAddress
,
888 IN MEMORY_PROFILE_ACTION Action
,
889 IN EFI_MEMORY_TYPE MemoryType
, // Valid for AllocatePages/AllocatePool
890 IN UINTN Size
, // Valid for AllocatePages/FreePages/AllocatePool
895 Register SMRAM profile handler.
899 RegisterSmramProfileHandler (
904 SMRAM profile ready to lock callback function.
908 SmramProfileReadyToLock (
913 Dump SMRAM infromation.
921 extern UINTN mFullSmramRangeCount
;
922 extern EFI_SMRAM_DESCRIPTOR
*mFullSmramRanges
;