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>
55 #include "PiSmmCorePrivateData.h"
58 // Used to build a table of SMI Handlers that the SMM Core registers
61 EFI_SMM_HANDLER_ENTRY_POINT2 Handler
;
62 EFI_GUID
*HandlerType
;
63 EFI_HANDLE DispatchHandle
;
65 } SMM_CORE_SMI_HANDLERS
;
68 // Structure for recording the state of an SMM Driver
70 #define EFI_SMM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
74 LIST_ENTRY Link
; // mDriverList
76 LIST_ENTRY ScheduledLink
; // mScheduledQueue
80 EFI_DEVICE_PATH_PROTOCOL
*FvFileDevicePath
;
81 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
88 EFI_GUID BeforeAfterGuid
;
93 BOOLEAN DepexProtocolError
;
95 EFI_HANDLE ImageHandle
;
96 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
98 // Image EntryPoint in SMRAM
100 PHYSICAL_ADDRESS ImageEntryPoint
;
102 // Image Buffer in SMRAM
104 PHYSICAL_ADDRESS ImageBuffer
;
109 } EFI_SMM_DRIVER_ENTRY
;
111 #define EFI_HANDLE_SIGNATURE SIGNATURE_32('h','n','d','l')
114 /// IHANDLE - contains a list of protocol handles
118 /// All handles list of IHANDLE
119 LIST_ENTRY AllHandles
;
120 /// List of PROTOCOL_INTERFACE's for this handle
121 LIST_ENTRY Protocols
;
125 #define ASSERT_IS_HANDLE(a) ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
127 #define PROTOCOL_ENTRY_SIGNATURE SIGNATURE_32('p','r','t','e')
130 /// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
131 /// database. Each handler that supports this protocol is listed, along
132 /// with a list of registered notifies.
136 /// Link Entry inserted to mProtocolDatabase
137 LIST_ENTRY AllEntries
;
138 /// ID of the protocol
140 /// All protocol interfaces
141 LIST_ENTRY Protocols
;
142 /// Registerd notification handlers
146 #define PROTOCOL_INTERFACE_SIGNATURE SIGNATURE_32('p','i','f','c')
149 /// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
150 /// with a protocol interface structure
154 /// Link on IHANDLE.Protocols
158 /// Link on PROTOCOL_ENTRY.Protocols
159 LIST_ENTRY ByProtocol
;
161 PROTOCOL_ENTRY
*Protocol
;
162 /// The interface value
164 } PROTOCOL_INTERFACE
;
166 #define PROTOCOL_NOTIFY_SIGNATURE SIGNATURE_32('p','r','t','n')
169 /// PROTOCOL_NOTIFY - used for each register notification for a protocol
173 PROTOCOL_ENTRY
*Protocol
;
174 /// All notifications for this protocol
176 /// Notification function
177 EFI_SMM_NOTIFY_FN Function
;
178 /// Last position notified
179 LIST_ENTRY
*Position
;
183 // SMM Core Global Variables
185 extern SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
;
186 extern EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst
;
187 extern LIST_ENTRY gHandleList
;
188 extern EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase
;
191 Called to initialize the memory service.
193 @param SmramRangeCount Number of SMRAM Regions
194 @param SmramRanges Pointer to SMRAM Descriptors
198 SmmInitializeMemoryServices (
199 IN UINTN SmramRangeCount
,
200 IN EFI_SMRAM_DESCRIPTOR
*SmramRanges
204 The SmmInstallConfigurationTable() function is used to maintain the list
205 of configuration tables that are stored in the System Management System
206 Table. The list is stored as an array of (GUID, Pointer) pairs. The list
207 must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
209 @param SystemTable A pointer to the SMM System Table (SMST).
210 @param Guid A pointer to the GUID for the entry to add, update, or remove.
211 @param Table A pointer to the buffer of the table to add.
212 @param TableSize The size of the table to install.
214 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
215 @retval EFI_INVALID_PARAMETER Guid is not valid.
216 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
217 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
222 SmmInstallConfigurationTable (
223 IN CONST EFI_SMM_SYSTEM_TABLE2
*SystemTable
,
224 IN CONST EFI_GUID
*Guid
,
230 Wrapper function to SmmInstallProtocolInterfaceNotify. This is the public API which
231 Calls the private one which contains a BOOLEAN parameter for notifications
233 @param UserHandle The handle to install the protocol handler on,
234 or NULL if a new handle is to be allocated
235 @param Protocol The protocol to add to the handle
236 @param InterfaceType Indicates whether Interface is supplied in
238 @param Interface The interface for the protocol being added
245 SmmInstallProtocolInterface (
246 IN OUT EFI_HANDLE
*UserHandle
,
247 IN EFI_GUID
*Protocol
,
248 IN EFI_INTERFACE_TYPE InterfaceType
,
253 Allocates pages from the memory map.
255 @param Type The type of allocation to perform
256 @param MemoryType The type of memory to turn the allocated pages
258 @param NumberOfPages The number of pages to allocate
259 @param Memory A pointer to receive the base allocated memory
262 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
263 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
264 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
265 @retval EFI_SUCCESS Pages successfully allocated.
271 IN EFI_ALLOCATE_TYPE Type
,
272 IN EFI_MEMORY_TYPE MemoryType
,
273 IN UINTN NumberOfPages
,
274 OUT EFI_PHYSICAL_ADDRESS
*Memory
278 Allocates pages from the memory map.
280 @param Type The type of allocation to perform
281 @param MemoryType The type of memory to turn the allocated pages
283 @param NumberOfPages The number of pages to allocate
284 @param Memory A pointer to receive the base allocated memory
287 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
288 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
289 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
290 @retval EFI_SUCCESS Pages successfully allocated.
295 SmmInternalAllocatePages (
296 IN EFI_ALLOCATE_TYPE Type
,
297 IN EFI_MEMORY_TYPE MemoryType
,
298 IN UINTN NumberOfPages
,
299 OUT EFI_PHYSICAL_ADDRESS
*Memory
303 Frees previous allocated pages.
305 @param Memory Base address of memory being freed
306 @param NumberOfPages The number of pages to free
308 @retval EFI_NOT_FOUND Could not find the entry that covers the range
309 @retval EFI_INVALID_PARAMETER Address not aligned
310 @return EFI_SUCCESS Pages successfully freed.
316 IN EFI_PHYSICAL_ADDRESS Memory
,
317 IN UINTN NumberOfPages
321 Frees previous allocated pages.
323 @param Memory Base address of memory being freed
324 @param NumberOfPages The number of pages to free
326 @retval EFI_NOT_FOUND Could not find the entry that covers the range
327 @retval EFI_INVALID_PARAMETER Address not aligned
328 @return EFI_SUCCESS Pages successfully freed.
333 SmmInternalFreePages (
334 IN EFI_PHYSICAL_ADDRESS Memory
,
335 IN UINTN NumberOfPages
339 Allocate pool of a particular type.
341 @param PoolType Type of pool to allocate
342 @param Size The amount of pool to allocate
343 @param Buffer The address to return a pointer to the allocated
346 @retval EFI_INVALID_PARAMETER PoolType not valid
347 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
348 @retval EFI_SUCCESS Pool successfully allocated.
354 IN EFI_MEMORY_TYPE PoolType
,
360 Allocate pool of a particular type.
362 @param PoolType Type of pool to allocate
363 @param Size The amount of pool to allocate
364 @param Buffer The address to return a pointer to the allocated
367 @retval EFI_INVALID_PARAMETER PoolType not valid
368 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
369 @retval EFI_SUCCESS Pool successfully allocated.
374 SmmInternalAllocatePool (
375 IN EFI_MEMORY_TYPE PoolType
,
383 @param Buffer The allocated pool entry to free
385 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
386 @retval EFI_SUCCESS Pool successfully freed.
398 @param Buffer The allocated pool entry to free
400 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
401 @retval EFI_SUCCESS Pool successfully freed.
406 SmmInternalFreePool (
411 Installs a protocol interface into the boot services environment.
413 @param UserHandle The handle to install the protocol handler on,
414 or NULL if a new handle is to be allocated
415 @param Protocol The protocol to add to the handle
416 @param InterfaceType Indicates whether Interface is supplied in
418 @param Interface The interface for the protocol being added
419 @param Notify indicates whether notify the notification list
422 @retval EFI_INVALID_PARAMETER Invalid parameter
423 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
424 @retval EFI_SUCCESS Protocol interface successfully installed
428 SmmInstallProtocolInterfaceNotify (
429 IN OUT EFI_HANDLE
*UserHandle
,
430 IN EFI_GUID
*Protocol
,
431 IN EFI_INTERFACE_TYPE InterfaceType
,
437 Uninstalls all instances of a protocol:interfacer from a handle.
438 If the last protocol interface is remove from the handle, the
441 @param UserHandle The handle to remove the protocol handler from
442 @param Protocol The protocol, of protocol:interface, to remove
443 @param Interface The interface, of protocol:interface, to remove
445 @retval EFI_INVALID_PARAMETER Protocol is NULL.
446 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
451 SmmUninstallProtocolInterface (
452 IN EFI_HANDLE UserHandle
,
453 IN EFI_GUID
*Protocol
,
458 Queries a handle to determine if it supports a specified protocol.
460 @param UserHandle The handle being queried.
461 @param Protocol The published unique identifier of the protocol.
462 @param Interface Supplies the address where a pointer to the
463 corresponding Protocol Interface is returned.
465 @return The requested protocol interface for the handle
471 IN EFI_HANDLE UserHandle
,
472 IN EFI_GUID
*Protocol
,
477 Add a new protocol notification record for the request protocol.
479 @param Protocol The requested protocol to add the notify
481 @param Function Points to the notification function
482 @param Registration Returns the registration record
484 @retval EFI_INVALID_PARAMETER Invalid parameter
485 @retval EFI_SUCCESS Successfully returned the registration record
491 SmmRegisterProtocolNotify (
492 IN CONST EFI_GUID
*Protocol
,
493 IN EFI_SMM_NOTIFY_FN Function
,
494 OUT VOID
**Registration
498 Locates the requested handle(s) and returns them in Buffer.
500 @param SearchType The type of search to perform to locate the
502 @param Protocol The protocol to search for
503 @param SearchKey Dependant on SearchType
504 @param BufferSize On input the size of Buffer. On output the
505 size of data returned.
506 @param Buffer The buffer to return the results in
508 @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is
509 returned in BufferSize.
510 @retval EFI_INVALID_PARAMETER Invalid parameter
511 @retval EFI_SUCCESS Successfully found the requested handle(s) and
512 returns them in Buffer.
518 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
519 IN EFI_GUID
*Protocol OPTIONAL
,
520 IN VOID
*SearchKey OPTIONAL
,
521 IN OUT UINTN
*BufferSize
,
522 OUT EFI_HANDLE
*Buffer
526 Return the first Protocol Interface that matches the Protocol GUID. If
527 Registration is pasased in return a Protocol Instance that was just add
528 to the system. If Retistration is NULL return the first Protocol Interface
531 @param Protocol The protocol to search for
532 @param Registration Optional Registration Key returned from
533 RegisterProtocolNotify()
534 @param Interface Return the Protocol interface (instance).
536 @retval EFI_SUCCESS If a valid Interface is returned
537 @retval EFI_INVALID_PARAMETER Invalid parameter
538 @retval EFI_NOT_FOUND Protocol interface not found
544 IN EFI_GUID
*Protocol
,
545 IN VOID
*Registration OPTIONAL
,
550 Manage SMI of a particular type.
552 @param HandlerType Points to the handler type or NULL for root SMI handlers.
553 @param Context Points to an optional context buffer.
554 @param CommBuffer Points to the optional communication buffer.
555 @param CommBufferSize Points to the size of the optional communication buffer.
557 @retval EFI_SUCCESS Interrupt source was processed successfully but not quiesced.
558 @retval EFI_INTERRUPT_PENDING One or more SMI sources could not be quiesced.
559 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING Interrupt source was not handled or quiesced.
560 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
566 IN CONST EFI_GUID
*HandlerType
,
567 IN CONST VOID
*Context OPTIONAL
,
568 IN OUT VOID
*CommBuffer OPTIONAL
,
569 IN OUT UINTN
*CommBufferSize OPTIONAL
573 Registers a handler to execute within SMM.
575 @param Handler Handler service funtion pointer.
576 @param HandlerType Points to the handler type or NULL for root SMI handlers.
577 @param DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
579 @retval EFI_SUCCESS Handler register success.
580 @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
586 IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler
,
587 IN CONST EFI_GUID
*HandlerType OPTIONAL
,
588 OUT EFI_HANDLE
*DispatchHandle
592 Unregister a handler in SMM.
594 @param DispatchHandle The handle that was specified when the handler was registered.
596 @retval EFI_SUCCESS Handler function was successfully unregistered.
597 @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
602 SmiHandlerUnRegister (
603 IN EFI_HANDLE DispatchHandle
607 This function is the main entry point for an SMM handler dispatch
608 or communicate-based callback.
610 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
611 @param Context Points to an optional handler context which was specified when the handler was registered.
612 @param CommBuffer A pointer to a collection of data in memory that will
613 be conveyed from a non-SMM environment into an SMM environment.
614 @param CommBufferSize The size of the CommBuffer.
621 SmmDriverDispatchHandler (
622 IN EFI_HANDLE DispatchHandle
,
623 IN CONST VOID
*Context
, OPTIONAL
624 IN OUT VOID
*CommBuffer
, OPTIONAL
625 IN OUT UINTN
*CommBufferSize OPTIONAL
629 This function is the main entry point for an SMM handler dispatch
630 or communicate-based callback.
632 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
633 @param Context Points to an optional handler context which was specified when the handler was registered.
634 @param CommBuffer A pointer to a collection of data in memory that will
635 be conveyed from a non-SMM environment into an SMM environment.
636 @param CommBufferSize The size of the CommBuffer.
643 SmmLegacyBootHandler (
644 IN EFI_HANDLE DispatchHandle
,
645 IN CONST VOID
*Context
, OPTIONAL
646 IN OUT VOID
*CommBuffer
, OPTIONAL
647 IN OUT UINTN
*CommBufferSize OPTIONAL
651 This function is the main entry point for an SMM handler dispatch
652 or communicate-based callback.
654 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
655 @param Context Points to an optional handler context which was specified when the handler was registered.
656 @param CommBuffer A pointer to a collection of data in memory that will
657 be conveyed from a non-SMM environment into an SMM environment.
658 @param CommBufferSize The size of the CommBuffer.
665 SmmReadyToLockHandler (
666 IN EFI_HANDLE DispatchHandle
,
667 IN CONST VOID
*Context
, OPTIONAL
668 IN OUT VOID
*CommBuffer
, OPTIONAL
669 IN OUT UINTN
*CommBufferSize OPTIONAL
673 This function is the main entry point for an SMM handler dispatch
674 or communicate-based callback.
676 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
677 @param Context Points to an optional handler context which was specified when the handler was registered.
678 @param CommBuffer A pointer to a collection of data in memory that will
679 be conveyed from a non-SMM environment into an SMM environment.
680 @param CommBufferSize The size of the CommBuffer.
688 IN EFI_HANDLE DispatchHandle
,
689 IN CONST VOID
*Context
, OPTIONAL
690 IN OUT VOID
*CommBuffer
, OPTIONAL
691 IN OUT UINTN
*CommBufferSize OPTIONAL
695 Place holder function until all the SMM System Table Service are available.
697 @param Arg1 Undefined
698 @param Arg2 Undefined
699 @param Arg3 Undefined
700 @param Arg4 Undefined
701 @param Arg5 Undefined
703 @return EFI_NOT_AVAILABLE_YET
708 SmmEfiNotAvailableYetArg5 (
717 //Functions used during debug buils
721 Traverse the discovered list for any drivers that were discovered but not loaded
722 because the dependency experessions evaluated to false.
726 SmmDisplayDiscoveredNotDispatched (
731 Add free SMRAM region for use by memory service.
733 @param MemBase Base address of memory region.
734 @param MemLength Length of the memory region.
735 @param Type Memory type.
736 @param Attributes Memory region state.
741 IN EFI_PHYSICAL_ADDRESS MemBase
,
743 IN EFI_MEMORY_TYPE Type
,
748 Finds the protocol entry for the requested protocol.
750 @param Protocol The ID of the protocol
751 @param Create Create a new entry if not found
753 @return Protocol entry
757 SmmFindProtocolEntry (
758 IN EFI_GUID
*Protocol
,
763 Signal event for every protocol in protocol entry.
765 @param Prot Protocol interface
770 IN PROTOCOL_INTERFACE
*Prot
774 Finds the protocol instance for the requested handle and protocol.
775 Note: This function doesn't do parameters checking, it's caller's responsibility
776 to pass in valid parameters.
778 @param Handle The handle to search the protocol on
779 @param Protocol GUID of the protocol
780 @param Interface The interface for the protocol being searched
782 @return Protocol instance (NULL: Not found)
786 SmmFindProtocolInterface (
788 IN EFI_GUID
*Protocol
,
793 Removes Protocol from the protocol list (but not the handle list).
795 @param Handle The handle to remove protocol on.
796 @param Protocol GUID of the protocol to be moved
797 @param Interface The interface of the protocol
799 @return Protocol Entry
803 SmmRemoveInterfaceFromProtocol (
805 IN EFI_GUID
*Protocol
,
810 This is the POSTFIX version of the dependency evaluator. This code does
811 not need to handle Before or After, as it is not valid to call this
812 routine in this case. POSTFIX means all the math is done on top of the stack.
814 @param DriverEntry DriverEntry element to update.
816 @retval TRUE If driver is ready to run.
817 @retval FALSE If driver is not ready to run or some fatal error
823 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
831 Initialize SMRAM profile.
840 Register SMM image to SMRAM profile.
842 @param DriverEntry SMM image info.
843 @param RegisterToDxe Register image to DXE.
845 @retval TRUE Register success.
846 @retval FALSE Register fail.
850 RegisterSmramProfileImage (
851 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
,
852 IN BOOLEAN RegisterToDxe
856 Unregister image from SMRAM profile.
858 @param DriverEntry SMM image info.
859 @param UnregisterToDxe Unregister image from DXE.
861 @retval TRUE Unregister success.
862 @retval FALSE Unregister fail.
866 UnregisterSmramProfileImage (
867 IN EFI_SMM_DRIVER_ENTRY
*DriverEntry
,
868 IN BOOLEAN UnregisterToDxe
872 Update SMRAM profile information.
874 @param CallerAddress Address of caller who call Allocate or Free.
875 @param Action This Allocate or Free action.
876 @param MemoryType Memory type.
877 @param Size Buffer size.
878 @param Buffer Buffer address.
880 @retval TRUE Profile udpate success.
881 @retval FALSE Profile update fail.
885 SmmCoreUpdateProfile (
886 IN EFI_PHYSICAL_ADDRESS CallerAddress
,
887 IN MEMORY_PROFILE_ACTION Action
,
888 IN EFI_MEMORY_TYPE MemoryType
, // Valid for AllocatePages/AllocatePool
889 IN UINTN Size
, // Valid for AllocatePages/FreePages/AllocatePool
894 Register SMRAM profile handler.
898 RegisterSmramProfileHandler (
903 SMRAM profile ready to lock callback function.
907 SmramProfileReadyToLock (
912 Dump SMRAM infromation.
920 extern UINTN mFullSmramRangeCount
;
921 extern EFI_SMRAM_DESCRIPTOR
*mFullSmramRanges
;