]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmCore.h
b8a490a8c3b599f1bc955f2e9ecfed45734ff7ab
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.h
1 /** @file
2 The internal header file includes the common header files, defines
3 internal structure and functions used by SmmCore module.
4
5 Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #ifndef _SMM_CORE_H_
11 #define _SMM_CORE_H_
12
13 #include <PiSmm.h>
14
15 #include <Protocol/DxeSmmReadyToLock.h>
16 #include <Protocol/SmmReadyToLock.h>
17 #include <Protocol/SmmEndOfDxe.h>
18 #include <Protocol/CpuIo2.h>
19 #include <Protocol/SmmCommunication.h>
20 #include <Protocol/SmmAccess2.h>
21 #include <Protocol/FirmwareVolume2.h>
22 #include <Protocol/LoadedImage.h>
23 #include <Protocol/DevicePath.h>
24 #include <Protocol/Security.h>
25 #include <Protocol/Security2.h>
26 #include <Protocol/SmmExitBootServices.h>
27 #include <Protocol/SmmLegacyBoot.h>
28 #include <Protocol/SmmReadyToBoot.h>
29 #include <Protocol/SmmMemoryAttribute.h>
30 #include <Protocol/SmmSxDispatch2.h>
31
32 #include <Guid/Apriori.h>
33 #include <Guid/EventGroup.h>
34 #include <Guid/EventLegacyBios.h>
35 #include <Guid/MemoryProfile.h>
36 #include <Guid/LoadModuleAtFixedAddress.h>
37 #include <Guid/SmiHandlerProfile.h>
38 #include <Guid/EndOfS3Resume.h>
39 #include <Guid/S3SmmInitDone.h>
40
41 #include <Library/BaseLib.h>
42 #include <Library/BaseMemoryLib.h>
43 #include <Library/PeCoffLib.h>
44 #include <Library/PeCoffGetEntryPointLib.h>
45 #include <Library/CacheMaintenanceLib.h>
46 #include <Library/DebugLib.h>
47 #include <Library/ReportStatusCodeLib.h>
48 #include <Library/MemoryAllocationLib.h>
49 #include <Library/DevicePathLib.h>
50 #include <Library/UefiLib.h>
51 #include <Library/UefiBootServicesTableLib.h>
52 #include <Library/PcdLib.h>
53 #include <Library/SmmCorePlatformHookLib.h>
54 #include <Library/PerformanceLib.h>
55 #include <Library/HobLib.h>
56 #include <Library/SmmMemLib.h>
57 #include <Library/SafeIntLib.h>
58
59 #include "PiSmmCorePrivateData.h"
60 #include "HeapGuard.h"
61
62 //
63 // Used to build a table of SMI Handlers that the SMM Core registers
64 //
65 typedef struct {
66 EFI_SMM_HANDLER_ENTRY_POINT2 Handler;
67 EFI_GUID *HandlerType;
68 EFI_HANDLE DispatchHandle;
69 BOOLEAN UnRegister;
70 } SMM_CORE_SMI_HANDLERS;
71
72 //
73 // SMM_HANDLER - used for each SMM handler
74 //
75
76 #define SMI_ENTRY_SIGNATURE SIGNATURE_32('s','m','i','e')
77
78 typedef struct {
79 UINTN Signature;
80 LIST_ENTRY AllEntries; // All entries
81
82 EFI_GUID HandlerType; // Type of interrupt
83 LIST_ENTRY SmiHandlers; // All handlers
84 } SMI_ENTRY;
85
86 #define SMI_HANDLER_SIGNATURE SIGNATURE_32('s','m','i','h')
87
88 typedef struct {
89 UINTN Signature;
90 LIST_ENTRY Link; // Link on SMI_ENTRY.SmiHandlers
91 EFI_SMM_HANDLER_ENTRY_POINT2 Handler; // The smm handler's entry point
92 UINTN CallerAddr; // The address of caller who register the SMI handler.
93 SMI_ENTRY *SmiEntry;
94 VOID *Context; // for profile
95 UINTN ContextSize; // for profile
96 } SMI_HANDLER;
97
98 //
99 // Structure for recording the state of an SMM Driver
100 //
101 #define EFI_SMM_DRIVER_ENTRY_SIGNATURE SIGNATURE_32('s', 'd','r','v')
102
103 typedef struct {
104 UINTN Signature;
105 LIST_ENTRY Link; // mDriverList
106
107 LIST_ENTRY ScheduledLink; // mScheduledQueue
108
109 EFI_HANDLE FvHandle;
110 EFI_GUID FileName;
111 EFI_DEVICE_PATH_PROTOCOL *FvFileDevicePath;
112 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
113
114 VOID *Depex;
115 UINTN DepexSize;
116
117 BOOLEAN Before;
118 BOOLEAN After;
119 EFI_GUID BeforeAfterGuid;
120
121 BOOLEAN Dependent;
122 BOOLEAN Scheduled;
123 BOOLEAN Initialized;
124 BOOLEAN DepexProtocolError;
125
126 EFI_HANDLE ImageHandle;
127 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
128 //
129 // Image EntryPoint in SMRAM
130 //
131 PHYSICAL_ADDRESS ImageEntryPoint;
132 //
133 // Image Buffer in SMRAM
134 //
135 PHYSICAL_ADDRESS ImageBuffer;
136 //
137 // Image Page Number
138 //
139 UINTN NumberOfPage;
140 EFI_HANDLE SmmImageHandle;
141 EFI_LOADED_IMAGE_PROTOCOL SmmLoadedImage;
142 } EFI_SMM_DRIVER_ENTRY;
143
144 #define EFI_HANDLE_SIGNATURE SIGNATURE_32('s','h','d','l')
145
146 ///
147 /// IHANDLE - contains a list of protocol handles
148 ///
149 typedef struct {
150 UINTN Signature;
151 /// All handles list of IHANDLE
152 LIST_ENTRY AllHandles;
153 /// List of PROTOCOL_INTERFACE's for this handle
154 LIST_ENTRY Protocols;
155 UINTN LocateRequest;
156 } IHANDLE;
157
158 #define ASSERT_IS_HANDLE(a) ASSERT((a)->Signature == EFI_HANDLE_SIGNATURE)
159
160 #define PROTOCOL_ENTRY_SIGNATURE SIGNATURE_32('s','p','t','e')
161
162 ///
163 /// PROTOCOL_ENTRY - each different protocol has 1 entry in the protocol
164 /// database. Each handler that supports this protocol is listed, along
165 /// with a list of registered notifies.
166 ///
167 typedef struct {
168 UINTN Signature;
169 /// Link Entry inserted to mProtocolDatabase
170 LIST_ENTRY AllEntries;
171 /// ID of the protocol
172 EFI_GUID ProtocolID;
173 /// All protocol interfaces
174 LIST_ENTRY Protocols;
175 /// Registered notification handlers
176 LIST_ENTRY Notify;
177 } PROTOCOL_ENTRY;
178
179 #define PROTOCOL_INTERFACE_SIGNATURE SIGNATURE_32('s','p','i','f')
180
181 ///
182 /// PROTOCOL_INTERFACE - each protocol installed on a handle is tracked
183 /// with a protocol interface structure
184 ///
185 typedef struct {
186 UINTN Signature;
187 /// Link on IHANDLE.Protocols
188 LIST_ENTRY Link;
189 /// Back pointer
190 IHANDLE *Handle;
191 /// Link on PROTOCOL_ENTRY.Protocols
192 LIST_ENTRY ByProtocol;
193 /// The protocol ID
194 PROTOCOL_ENTRY *Protocol;
195 /// The interface value
196 VOID *Interface;
197 } PROTOCOL_INTERFACE;
198
199 #define PROTOCOL_NOTIFY_SIGNATURE SIGNATURE_32('s','p','t','n')
200
201 ///
202 /// PROTOCOL_NOTIFY - used for each register notification for a protocol
203 ///
204 typedef struct {
205 UINTN Signature;
206 PROTOCOL_ENTRY *Protocol;
207 /// All notifications for this protocol
208 LIST_ENTRY Link;
209 /// Notification function
210 EFI_SMM_NOTIFY_FN Function;
211 /// Last position notified
212 LIST_ENTRY *Position;
213 } PROTOCOL_NOTIFY;
214
215 //
216 // SMM Core Global Variables
217 //
218 extern SMM_CORE_PRIVATE_DATA *gSmmCorePrivate;
219 extern EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst;
220 extern LIST_ENTRY gHandleList;
221 extern EFI_PHYSICAL_ADDRESS gLoadModuleAtFixAddressSmramBase;
222
223 /**
224 Called to initialize the memory service.
225
226 @param SmramRangeCount Number of SMRAM Regions
227 @param SmramRanges Pointer to SMRAM Descriptors
228
229 **/
230 VOID
231 SmmInitializeMemoryServices (
232 IN UINTN SmramRangeCount,
233 IN EFI_SMRAM_DESCRIPTOR *SmramRanges
234 );
235
236 /**
237 The SmmInstallConfigurationTable() function is used to maintain the list
238 of configuration tables that are stored in the System Management System
239 Table. The list is stored as an array of (GUID, Pointer) pairs. The list
240 must be allocated from pool memory with PoolType set to EfiRuntimeServicesData.
241
242 @param SystemTable A pointer to the SMM System Table (SMST).
243 @param Guid A pointer to the GUID for the entry to add, update, or remove.
244 @param Table A pointer to the buffer of the table to add.
245 @param TableSize The size of the table to install.
246
247 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
248 @retval EFI_INVALID_PARAMETER Guid is not valid.
249 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
250 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
251
252 **/
253 EFI_STATUS
254 EFIAPI
255 SmmInstallConfigurationTable (
256 IN CONST EFI_SMM_SYSTEM_TABLE2 *SystemTable,
257 IN CONST EFI_GUID *Guid,
258 IN VOID *Table,
259 IN UINTN TableSize
260 );
261
262 /**
263 Wrapper function to SmmInstallProtocolInterfaceNotify. This is the public API which
264 Calls the private one which contains a BOOLEAN parameter for notifications
265
266 @param UserHandle The handle to install the protocol handler on,
267 or NULL if a new handle is to be allocated
268 @param Protocol The protocol to add to the handle
269 @param InterfaceType Indicates whether Interface is supplied in
270 native form.
271 @param Interface The interface for the protocol being added
272
273 @return Status code
274
275 **/
276 EFI_STATUS
277 EFIAPI
278 SmmInstallProtocolInterface (
279 IN OUT EFI_HANDLE *UserHandle,
280 IN EFI_GUID *Protocol,
281 IN EFI_INTERFACE_TYPE InterfaceType,
282 IN VOID *Interface
283 );
284
285 /**
286 Allocates pages from the memory map.
287
288 @param Type The type of allocation to perform
289 @param MemoryType The type of memory to turn the allocated pages
290 into
291 @param NumberOfPages The number of pages to allocate
292 @param Memory A pointer to receive the base allocated memory
293 address
294
295 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
296 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
297 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
298 @retval EFI_SUCCESS Pages successfully allocated.
299
300 **/
301 EFI_STATUS
302 EFIAPI
303 SmmAllocatePages (
304 IN EFI_ALLOCATE_TYPE Type,
305 IN EFI_MEMORY_TYPE MemoryType,
306 IN UINTN NumberOfPages,
307 OUT EFI_PHYSICAL_ADDRESS *Memory
308 );
309
310 /**
311 Allocates pages from the memory map.
312
313 @param Type The type of allocation to perform
314 @param MemoryType The type of memory to turn the allocated pages
315 into
316 @param NumberOfPages The number of pages to allocate
317 @param Memory A pointer to receive the base allocated memory
318 address
319 @param NeedGuard Flag to indicate Guard page is needed or not
320
321 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
322 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
323 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
324 @retval EFI_SUCCESS Pages successfully allocated.
325
326 **/
327 EFI_STATUS
328 EFIAPI
329 SmmInternalAllocatePages (
330 IN EFI_ALLOCATE_TYPE Type,
331 IN EFI_MEMORY_TYPE MemoryType,
332 IN UINTN NumberOfPages,
333 OUT EFI_PHYSICAL_ADDRESS *Memory,
334 IN BOOLEAN NeedGuard
335 );
336
337 /**
338 Frees previous allocated pages.
339
340 @param Memory Base address of memory being freed
341 @param NumberOfPages The number of pages to free
342
343 @retval EFI_NOT_FOUND Could not find the entry that covers the range
344 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
345 @return EFI_SUCCESS Pages successfully freed.
346
347 **/
348 EFI_STATUS
349 EFIAPI
350 SmmFreePages (
351 IN EFI_PHYSICAL_ADDRESS Memory,
352 IN UINTN NumberOfPages
353 );
354
355 /**
356 Frees previous allocated pages.
357
358 @param Memory Base address of memory being freed
359 @param NumberOfPages The number of pages to free
360 @param IsGuarded Flag to indicate if the memory is guarded
361 or not
362
363 @retval EFI_NOT_FOUND Could not find the entry that covers the range
364 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
365 @return EFI_SUCCESS Pages successfully freed.
366
367 **/
368 EFI_STATUS
369 EFIAPI
370 SmmInternalFreePages (
371 IN EFI_PHYSICAL_ADDRESS Memory,
372 IN UINTN NumberOfPages,
373 IN BOOLEAN IsGuarded
374 );
375
376 /**
377 Allocate pool of a particular type.
378
379 @param PoolType Type of pool to allocate
380 @param Size The amount of pool to allocate
381 @param Buffer The address to return a pointer to the allocated
382 pool
383
384 @retval EFI_INVALID_PARAMETER PoolType not valid
385 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
386 @retval EFI_SUCCESS Pool successfully allocated.
387
388 **/
389 EFI_STATUS
390 EFIAPI
391 SmmAllocatePool (
392 IN EFI_MEMORY_TYPE PoolType,
393 IN UINTN Size,
394 OUT VOID **Buffer
395 );
396
397 /**
398 Allocate pool of a particular type.
399
400 @param PoolType Type of pool to allocate
401 @param Size The amount of pool to allocate
402 @param Buffer The address to return a pointer to the allocated
403 pool
404
405 @retval EFI_INVALID_PARAMETER PoolType not valid
406 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
407 @retval EFI_SUCCESS Pool successfully allocated.
408
409 **/
410 EFI_STATUS
411 EFIAPI
412 SmmInternalAllocatePool (
413 IN EFI_MEMORY_TYPE PoolType,
414 IN UINTN Size,
415 OUT VOID **Buffer
416 );
417
418 /**
419 Frees pool.
420
421 @param Buffer The allocated pool entry to free
422
423 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
424 @retval EFI_SUCCESS Pool successfully freed.
425
426 **/
427 EFI_STATUS
428 EFIAPI
429 SmmFreePool (
430 IN VOID *Buffer
431 );
432
433 /**
434 Frees pool.
435
436 @param Buffer The allocated pool entry to free
437
438 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
439 @retval EFI_SUCCESS Pool successfully freed.
440
441 **/
442 EFI_STATUS
443 EFIAPI
444 SmmInternalFreePool (
445 IN VOID *Buffer
446 );
447
448 /**
449 Installs a protocol interface into the boot services environment.
450
451 @param UserHandle The handle to install the protocol handler on,
452 or NULL if a new handle is to be allocated
453 @param Protocol The protocol to add to the handle
454 @param InterfaceType Indicates whether Interface is supplied in
455 native form.
456 @param Interface The interface for the protocol being added
457 @param Notify indicates whether notify the notification list
458 for this protocol
459
460 @retval EFI_INVALID_PARAMETER Invalid parameter
461 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
462 @retval EFI_SUCCESS Protocol interface successfully installed
463
464 **/
465 EFI_STATUS
466 SmmInstallProtocolInterfaceNotify (
467 IN OUT EFI_HANDLE *UserHandle,
468 IN EFI_GUID *Protocol,
469 IN EFI_INTERFACE_TYPE InterfaceType,
470 IN VOID *Interface,
471 IN BOOLEAN Notify
472 );
473
474 /**
475 Uninstalls all instances of a protocol:interfacer from a handle.
476 If the last protocol interface is remove from the handle, the
477 handle is freed.
478
479 @param UserHandle The handle to remove the protocol handler from
480 @param Protocol The protocol, of protocol:interface, to remove
481 @param Interface The interface, of protocol:interface, to remove
482
483 @retval EFI_INVALID_PARAMETER Protocol is NULL.
484 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
485
486 **/
487 EFI_STATUS
488 EFIAPI
489 SmmUninstallProtocolInterface (
490 IN EFI_HANDLE UserHandle,
491 IN EFI_GUID *Protocol,
492 IN VOID *Interface
493 );
494
495 /**
496 Queries a handle to determine if it supports a specified protocol.
497
498 @param UserHandle The handle being queried.
499 @param Protocol The published unique identifier of the protocol.
500 @param Interface Supplies the address where a pointer to the
501 corresponding Protocol Interface is returned.
502
503 @return The requested protocol interface for the handle
504
505 **/
506 EFI_STATUS
507 EFIAPI
508 SmmHandleProtocol (
509 IN EFI_HANDLE UserHandle,
510 IN EFI_GUID *Protocol,
511 OUT VOID **Interface
512 );
513
514 /**
515 Add a new protocol notification record for the request protocol.
516
517 @param Protocol The requested protocol to add the notify
518 registration
519 @param Function Points to the notification function
520 @param Registration Returns the registration record
521
522 @retval EFI_INVALID_PARAMETER Invalid parameter
523 @retval EFI_SUCCESS Successfully returned the registration record
524 that has been added
525
526 **/
527 EFI_STATUS
528 EFIAPI
529 SmmRegisterProtocolNotify (
530 IN CONST EFI_GUID *Protocol,
531 IN EFI_SMM_NOTIFY_FN Function,
532 OUT VOID **Registration
533 );
534
535 /**
536 Locates the requested handle(s) and returns them in Buffer.
537
538 @param SearchType The type of search to perform to locate the
539 handles
540 @param Protocol The protocol to search for
541 @param SearchKey Dependant on SearchType
542 @param BufferSize On input the size of Buffer. On output the
543 size of data returned.
544 @param Buffer The buffer to return the results in
545
546 @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is
547 returned in BufferSize.
548 @retval EFI_INVALID_PARAMETER Invalid parameter
549 @retval EFI_SUCCESS Successfully found the requested handle(s) and
550 returns them in Buffer.
551
552 **/
553 EFI_STATUS
554 EFIAPI
555 SmmLocateHandle (
556 IN EFI_LOCATE_SEARCH_TYPE SearchType,
557 IN EFI_GUID *Protocol OPTIONAL,
558 IN VOID *SearchKey OPTIONAL,
559 IN OUT UINTN *BufferSize,
560 OUT EFI_HANDLE *Buffer
561 );
562
563 /**
564 Return the first Protocol Interface that matches the Protocol GUID. If
565 Registration is pasased in return a Protocol Instance that was just add
566 to the system. If Registration is NULL return the first Protocol Interface
567 you find.
568
569 @param Protocol The protocol to search for
570 @param Registration Optional Registration Key returned from
571 RegisterProtocolNotify()
572 @param Interface Return the Protocol interface (instance).
573
574 @retval EFI_SUCCESS If a valid Interface is returned
575 @retval EFI_INVALID_PARAMETER Invalid parameter
576 @retval EFI_NOT_FOUND Protocol interface not found
577
578 **/
579 EFI_STATUS
580 EFIAPI
581 SmmLocateProtocol (
582 IN EFI_GUID *Protocol,
583 IN VOID *Registration OPTIONAL,
584 OUT VOID **Interface
585 );
586
587 /**
588 Function returns an array of handles that support the requested protocol
589 in a buffer allocated from pool. This is a version of SmmLocateHandle()
590 that allocates a buffer for the caller.
591
592 @param SearchType Specifies which handle(s) are to be returned.
593 @param Protocol Provides the protocol to search by. This
594 parameter is only valid for SearchType
595 ByProtocol.
596 @param SearchKey Supplies the search key depending on the
597 SearchType.
598 @param NumberHandles The number of handles returned in Buffer.
599 @param Buffer A pointer to the buffer to return the requested
600 array of handles that support Protocol.
601
602 @retval EFI_SUCCESS The result array of handles was returned.
603 @retval EFI_NOT_FOUND No handles match the search.
604 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
605 matching results.
606 @retval EFI_INVALID_PARAMETER One or more parameters are not valid.
607
608 **/
609 EFI_STATUS
610 EFIAPI
611 SmmLocateHandleBuffer (
612 IN EFI_LOCATE_SEARCH_TYPE SearchType,
613 IN EFI_GUID *Protocol OPTIONAL,
614 IN VOID *SearchKey OPTIONAL,
615 IN OUT UINTN *NumberHandles,
616 OUT EFI_HANDLE **Buffer
617 );
618
619 /**
620 Manage SMI of a particular type.
621
622 @param HandlerType Points to the handler type or NULL for root SMI handlers.
623 @param Context Points to an optional context buffer.
624 @param CommBuffer Points to the optional communication buffer.
625 @param CommBufferSize Points to the size of the optional communication buffer.
626
627 @retval EFI_SUCCESS Interrupt source was processed successfully but not quiesced.
628 @retval EFI_INTERRUPT_PENDING One or more SMI sources could not be quiesced.
629 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING Interrupt source was not handled or quiesced.
630 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED Interrupt source was handled and quiesced.
631
632 **/
633 EFI_STATUS
634 EFIAPI
635 SmiManage (
636 IN CONST EFI_GUID *HandlerType,
637 IN CONST VOID *Context OPTIONAL,
638 IN OUT VOID *CommBuffer OPTIONAL,
639 IN OUT UINTN *CommBufferSize OPTIONAL
640 );
641
642 /**
643 Registers a handler to execute within SMM.
644
645 @param Handler Handler service function pointer.
646 @param HandlerType Points to the handler type or NULL for root SMI handlers.
647 @param DispatchHandle On return, contains a unique handle which can be used to later unregister the handler function.
648
649 @retval EFI_SUCCESS Handler register success.
650 @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
651
652 **/
653 EFI_STATUS
654 EFIAPI
655 SmiHandlerRegister (
656 IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler,
657 IN CONST EFI_GUID *HandlerType OPTIONAL,
658 OUT EFI_HANDLE *DispatchHandle
659 );
660
661 /**
662 Unregister a handler in SMM.
663
664 @param DispatchHandle The handle that was specified when the handler was registered.
665
666 @retval EFI_SUCCESS Handler function was successfully unregistered.
667 @retval EFI_INVALID_PARAMETER DispatchHandle does not refer to a valid handle.
668
669 **/
670 EFI_STATUS
671 EFIAPI
672 SmiHandlerUnRegister (
673 IN EFI_HANDLE DispatchHandle
674 );
675
676 /**
677 This function is the main entry point for an SMM handler dispatch
678 or communicate-based callback.
679
680 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
681 @param Context Points to an optional handler context which was specified when the handler was registered.
682 @param CommBuffer A pointer to a collection of data in memory that will
683 be conveyed from a non-SMM environment into an SMM environment.
684 @param CommBufferSize The size of the CommBuffer.
685
686 @return Status Code
687
688 **/
689 EFI_STATUS
690 EFIAPI
691 SmmDriverDispatchHandler (
692 IN EFI_HANDLE DispatchHandle,
693 IN CONST VOID *Context OPTIONAL,
694 IN OUT VOID *CommBuffer OPTIONAL,
695 IN OUT UINTN *CommBufferSize OPTIONAL
696 );
697
698 /**
699 This function is the main entry point for an SMM handler dispatch
700 or communicate-based callback.
701
702 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
703 @param Context Points to an optional handler context which was specified when the handler was registered.
704 @param CommBuffer A pointer to a collection of data in memory that will
705 be conveyed from a non-SMM environment into an SMM environment.
706 @param CommBufferSize The size of the CommBuffer.
707
708 @return Status Code
709
710 **/
711 EFI_STATUS
712 EFIAPI
713 SmmLegacyBootHandler (
714 IN EFI_HANDLE DispatchHandle,
715 IN CONST VOID *Context OPTIONAL,
716 IN OUT VOID *CommBuffer OPTIONAL,
717 IN OUT UINTN *CommBufferSize OPTIONAL
718 );
719
720 /**
721 This function is the main entry point for an SMM handler dispatch
722 or communicate-based callback.
723
724 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
725 @param Context Points to an optional handler context which was specified when the handler was registered.
726 @param CommBuffer A pointer to a collection of data in memory that will
727 be conveyed from a non-SMM environment into an SMM environment.
728 @param CommBufferSize The size of the CommBuffer.
729
730 @return Status Code
731
732 **/
733 EFI_STATUS
734 EFIAPI
735 SmmReadyToLockHandler (
736 IN EFI_HANDLE DispatchHandle,
737 IN CONST VOID *Context OPTIONAL,
738 IN OUT VOID *CommBuffer OPTIONAL,
739 IN OUT UINTN *CommBufferSize OPTIONAL
740 );
741
742 /**
743 This function is the main entry point for an SMM handler dispatch
744 or communicate-based callback.
745
746 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
747 @param Context Points to an optional handler context which was specified when the handler was registered.
748 @param CommBuffer A pointer to a collection of data in memory that will
749 be conveyed from a non-SMM environment into an SMM environment.
750 @param CommBufferSize The size of the CommBuffer.
751
752 @return Status Code
753
754 **/
755 EFI_STATUS
756 EFIAPI
757 SmmEndOfDxeHandler (
758 IN EFI_HANDLE DispatchHandle,
759 IN CONST VOID *Context OPTIONAL,
760 IN OUT VOID *CommBuffer OPTIONAL,
761 IN OUT UINTN *CommBufferSize OPTIONAL
762 );
763
764 /**
765 This function is the main entry point for an SMM handler dispatch
766 or communicate-based callback.
767
768 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
769 @param Context Points to an optional handler context which was specified when the handler was registered.
770 @param CommBuffer A pointer to a collection of data in memory that will
771 be conveyed from a non-SMM environment into an SMM environment.
772 @param CommBufferSize The size of the CommBuffer.
773
774 @return Status Code
775
776 **/
777 EFI_STATUS
778 EFIAPI
779 SmmExitBootServicesHandler (
780 IN EFI_HANDLE DispatchHandle,
781 IN CONST VOID *Context OPTIONAL,
782 IN OUT VOID *CommBuffer OPTIONAL,
783 IN OUT UINTN *CommBufferSize OPTIONAL
784 );
785
786 /**
787 This function is the main entry point for an SMM handler dispatch
788 or communicate-based callback.
789
790 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
791 @param Context Points to an optional handler context which was specified when the handler was registered.
792 @param CommBuffer A pointer to a collection of data in memory that will
793 be conveyed from a non-SMM environment into an SMM environment.
794 @param CommBufferSize The size of the CommBuffer.
795
796 @return Status Code
797
798 **/
799 EFI_STATUS
800 EFIAPI
801 SmmReadyToBootHandler (
802 IN EFI_HANDLE DispatchHandle,
803 IN CONST VOID *Context OPTIONAL,
804 IN OUT VOID *CommBuffer OPTIONAL,
805 IN OUT UINTN *CommBufferSize OPTIONAL
806 );
807
808 /**
809 Software SMI handler that is called when the S3SmmInitDone signal is triggered.
810 This function installs the SMM S3SmmInitDone Protocol so SMM Drivers are informed that
811 S3 SMM initialization has been done.
812
813 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
814 @param Context Points to an optional handler context which was specified when the handler was registered.
815 @param CommBuffer A pointer to a collection of data in memory that will
816 be conveyed from a non-SMM environment into an SMM environment.
817 @param CommBufferSize The size of the CommBuffer.
818
819 @return Status Code
820
821 **/
822 EFI_STATUS
823 EFIAPI
824 SmmS3SmmInitDoneHandler (
825 IN EFI_HANDLE DispatchHandle,
826 IN CONST VOID *Context OPTIONAL,
827 IN OUT VOID *CommBuffer OPTIONAL,
828 IN OUT UINTN *CommBufferSize OPTIONAL
829 );
830
831 /**
832 Software SMI handler that is called when the EndOfS3Resume event is trigged.
833 This function installs the SMM EndOfS3Resume Protocol so SMM Drivers are informed that
834 S3 resume has finished.
835
836 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
837 @param Context Points to an optional handler context which was specified when the handler was registered.
838 @param CommBuffer A pointer to a collection of data in memory that will
839 be conveyed from a non-SMM environment into an SMM environment.
840 @param CommBufferSize The size of the CommBuffer.
841
842 @return Status Code
843
844 **/
845 EFI_STATUS
846 EFIAPI
847 SmmEndOfS3ResumeHandler (
848 IN EFI_HANDLE DispatchHandle,
849 IN CONST VOID *Context OPTIONAL,
850 IN OUT VOID *CommBuffer OPTIONAL,
851 IN OUT UINTN *CommBufferSize OPTIONAL
852 );
853
854 /**
855 Place holder function until all the SMM System Table Service are available.
856
857 @param Arg1 Undefined
858 @param Arg2 Undefined
859 @param Arg3 Undefined
860 @param Arg4 Undefined
861 @param Arg5 Undefined
862
863 @return EFI_NOT_AVAILABLE_YET
864
865 **/
866 EFI_STATUS
867 EFIAPI
868 SmmEfiNotAvailableYetArg5 (
869 UINTN Arg1,
870 UINTN Arg2,
871 UINTN Arg3,
872 UINTN Arg4,
873 UINTN Arg5
874 );
875
876 //
877 // Functions used during debug builds
878 //
879
880 /**
881 Traverse the discovered list for any drivers that were discovered but not loaded
882 because the dependency expressions evaluated to false.
883
884 **/
885 VOID
886 SmmDisplayDiscoveredNotDispatched (
887 VOID
888 );
889
890 /**
891 Add free SMRAM region for use by memory service.
892
893 @param MemBase Base address of memory region.
894 @param MemLength Length of the memory region.
895 @param Type Memory type.
896 @param Attributes Memory region state.
897
898 **/
899 VOID
900 SmmAddMemoryRegion (
901 IN EFI_PHYSICAL_ADDRESS MemBase,
902 IN UINT64 MemLength,
903 IN EFI_MEMORY_TYPE Type,
904 IN UINT64 Attributes
905 );
906
907 /**
908 Finds the protocol entry for the requested protocol.
909
910 @param Protocol The ID of the protocol
911 @param Create Create a new entry if not found
912
913 @return Protocol entry
914
915 **/
916 PROTOCOL_ENTRY *
917 SmmFindProtocolEntry (
918 IN EFI_GUID *Protocol,
919 IN BOOLEAN Create
920 );
921
922 /**
923 Signal event for every protocol in protocol entry.
924
925 @param Prot Protocol interface
926
927 **/
928 VOID
929 SmmNotifyProtocol (
930 IN PROTOCOL_INTERFACE *Prot
931 );
932
933 /**
934 Finds the protocol instance for the requested handle and protocol.
935 Note: This function doesn't do parameters checking, it's caller's responsibility
936 to pass in valid parameters.
937
938 @param Handle The handle to search the protocol on
939 @param Protocol GUID of the protocol
940 @param Interface The interface for the protocol being searched
941
942 @return Protocol instance (NULL: Not found)
943
944 **/
945 PROTOCOL_INTERFACE *
946 SmmFindProtocolInterface (
947 IN IHANDLE *Handle,
948 IN EFI_GUID *Protocol,
949 IN VOID *Interface
950 );
951
952 /**
953 Removes Protocol from the protocol list (but not the handle list).
954
955 @param Handle The handle to remove protocol on.
956 @param Protocol GUID of the protocol to be moved
957 @param Interface The interface of the protocol
958
959 @return Protocol Entry
960
961 **/
962 PROTOCOL_INTERFACE *
963 SmmRemoveInterfaceFromProtocol (
964 IN IHANDLE *Handle,
965 IN EFI_GUID *Protocol,
966 IN VOID *Interface
967 );
968
969 /**
970 This is the POSTFIX version of the dependency evaluator. This code does
971 not need to handle Before or After, as it is not valid to call this
972 routine in this case. POSTFIX means all the math is done on top of the stack.
973
974 @param DriverEntry DriverEntry element to update.
975
976 @retval TRUE If driver is ready to run.
977 @retval FALSE If driver is not ready to run or some fatal error
978 was found.
979
980 **/
981 BOOLEAN
982 SmmIsSchedulable (
983 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
984 );
985
986 //
987 // SmramProfile
988 //
989
990 /**
991 Initialize SMRAM profile.
992
993 **/
994 VOID
995 SmramProfileInit (
996 VOID
997 );
998
999 /**
1000 Install SMRAM profile protocol.
1001
1002 **/
1003 VOID
1004 SmramProfileInstallProtocol (
1005 VOID
1006 );
1007
1008 /**
1009 Register SMM image to SMRAM profile.
1010
1011 @param DriverEntry SMM image info.
1012 @param RegisterToDxe Register image to DXE.
1013
1014 @return EFI_SUCCESS Register successfully.
1015 @return EFI_UNSUPPORTED Memory profile unsupported,
1016 or memory profile for the image is not required.
1017 @return EFI_OUT_OF_RESOURCES No enough resource for this register.
1018
1019 **/
1020 EFI_STATUS
1021 RegisterSmramProfileImage (
1022 IN EFI_SMM_DRIVER_ENTRY *DriverEntry,
1023 IN BOOLEAN RegisterToDxe
1024 );
1025
1026 /**
1027 Unregister image from SMRAM profile.
1028
1029 @param DriverEntry SMM image info.
1030 @param UnregisterToDxe Unregister image from DXE.
1031
1032 @return EFI_SUCCESS Unregister successfully.
1033 @return EFI_UNSUPPORTED Memory profile unsupported,
1034 or memory profile for the image is not required.
1035 @return EFI_NOT_FOUND The image is not found.
1036
1037 **/
1038 EFI_STATUS
1039 UnregisterSmramProfileImage (
1040 IN EFI_SMM_DRIVER_ENTRY *DriverEntry,
1041 IN BOOLEAN UnregisterToDxe
1042 );
1043
1044 /**
1045 Update SMRAM profile information.
1046
1047 @param CallerAddress Address of caller who call Allocate or Free.
1048 @param Action This Allocate or Free action.
1049 @param MemoryType Memory type.
1050 EfiMaxMemoryType means the MemoryType is unknown.
1051 @param Size Buffer size.
1052 @param Buffer Buffer address.
1053 @param ActionString String for memory profile action.
1054 Only needed for user defined allocate action.
1055
1056 @return EFI_SUCCESS Memory profile is updated.
1057 @return EFI_UNSUPPORTED Memory profile is unsupported,
1058 or memory profile for the image is not required,
1059 or memory profile for the memory type is not required.
1060 @return EFI_ACCESS_DENIED It is during memory profile data getting.
1061 @return EFI_ABORTED Memory profile recording is not enabled.
1062 @return EFI_OUT_OF_RESOURCES No enough resource to update memory profile for allocate action.
1063 @return EFI_NOT_FOUND No matched allocate info found for free action.
1064
1065 **/
1066 EFI_STATUS
1067 EFIAPI
1068 SmmCoreUpdateProfile (
1069 IN PHYSICAL_ADDRESS CallerAddress,
1070 IN MEMORY_PROFILE_ACTION Action,
1071 IN EFI_MEMORY_TYPE MemoryType, // Valid for AllocatePages/AllocatePool
1072 IN UINTN Size, // Valid for AllocatePages/FreePages/AllocatePool
1073 IN VOID *Buffer,
1074 IN CHAR8 *ActionString OPTIONAL
1075 );
1076
1077 /**
1078 Register SMRAM profile handler.
1079
1080 **/
1081 VOID
1082 RegisterSmramProfileHandler (
1083 VOID
1084 );
1085
1086 /**
1087 SMRAM profile ready to lock callback function.
1088
1089 **/
1090 VOID
1091 SmramProfileReadyToLock (
1092 VOID
1093 );
1094
1095 /**
1096 Initialize MemoryAttributes support.
1097 **/
1098 VOID
1099 EFIAPI
1100 SmmCoreInitializeMemoryAttributesTable (
1101 VOID
1102 );
1103
1104 /**
1105 This function returns a copy of the current memory map. The map is an array of
1106 memory descriptors, each of which describes a contiguous block of memory.
1107
1108 @param[in, out] MemoryMapSize A pointer to the size, in bytes, of the
1109 MemoryMap buffer. On input, this is the size of
1110 the buffer allocated by the caller. On output,
1111 it is the size of the buffer returned by the
1112 firmware if the buffer was large enough, or the
1113 size of the buffer needed to contain the map if
1114 the buffer was too small.
1115 @param[in, out] MemoryMap A pointer to the buffer in which firmware places
1116 the current memory map.
1117 @param[out] MapKey A pointer to the location in which firmware
1118 returns the key for the current memory map.
1119 @param[out] DescriptorSize A pointer to the location in which firmware
1120 returns the size, in bytes, of an individual
1121 EFI_MEMORY_DESCRIPTOR.
1122 @param[out] DescriptorVersion A pointer to the location in which firmware
1123 returns the version number associated with the
1124 EFI_MEMORY_DESCRIPTOR.
1125
1126 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1127 buffer.
1128 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1129 buffer size needed to hold the memory map is
1130 returned in MemoryMapSize.
1131 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1132
1133 **/
1134 EFI_STATUS
1135 EFIAPI
1136 SmmCoreGetMemoryMap (
1137 IN OUT UINTN *MemoryMapSize,
1138 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
1139 OUT UINTN *MapKey,
1140 OUT UINTN *DescriptorSize,
1141 OUT UINT32 *DescriptorVersion
1142 );
1143
1144 /**
1145 Initialize SmiHandler profile feature.
1146 **/
1147 VOID
1148 SmmCoreInitializeSmiHandlerProfile (
1149 VOID
1150 );
1151
1152 /**
1153 This function is called by SmmChildDispatcher module to report
1154 a new SMI handler is registered, to SmmCore.
1155
1156 @param This The protocol instance
1157 @param HandlerGuid The GUID to identify the type of the handler.
1158 For the SmmChildDispatch protocol, the HandlerGuid
1159 must be the GUID of SmmChildDispatch protocol.
1160 @param Handler The SMI handler.
1161 @param CallerAddress The address of the module who registers the SMI handler.
1162 @param Context The context of the SMI handler.
1163 For the SmmChildDispatch protocol, the Context
1164 must match the one defined for SmmChildDispatch protocol.
1165 @param ContextSize The size of the context in bytes.
1166 For the SmmChildDispatch protocol, the Context
1167 must match the one defined for SmmChildDispatch protocol.
1168
1169 @retval EFI_SUCCESS The information is recorded.
1170 @retval EFI_OUT_OF_RESOURCES There is no enough resource to record the information.
1171 **/
1172 EFI_STATUS
1173 EFIAPI
1174 SmiHandlerProfileRegisterHandler (
1175 IN SMI_HANDLER_PROFILE_PROTOCOL *This,
1176 IN EFI_GUID *HandlerGuid,
1177 IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler,
1178 IN PHYSICAL_ADDRESS CallerAddress,
1179 IN VOID *Context OPTIONAL,
1180 IN UINTN ContextSize OPTIONAL
1181 );
1182
1183 /**
1184 This function is called by SmmChildDispatcher module to report
1185 an existing SMI handler is unregistered, to SmmCore.
1186
1187 @param This The protocol instance
1188 @param HandlerGuid The GUID to identify the type of the handler.
1189 For the SmmChildDispatch protocol, the HandlerGuid
1190 must be the GUID of SmmChildDispatch protocol.
1191 @param Handler The SMI handler.
1192 @param Context The context of the SMI handler.
1193 If it is NOT NULL, it will be used to check what is registered.
1194 @param ContextSize The size of the context in bytes.
1195 If Context is NOT NULL, it will be used to check what is registered.
1196
1197 @retval EFI_SUCCESS The original record is removed.
1198 @retval EFI_NOT_FOUND There is no record for the HandlerGuid and handler.
1199 **/
1200 EFI_STATUS
1201 EFIAPI
1202 SmiHandlerProfileUnregisterHandler (
1203 IN SMI_HANDLER_PROFILE_PROTOCOL *This,
1204 IN EFI_GUID *HandlerGuid,
1205 IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler,
1206 IN VOID *Context OPTIONAL,
1207 IN UINTN ContextSize OPTIONAL
1208 );
1209
1210 extern UINTN mFullSmramRangeCount;
1211 extern EFI_SMRAM_DESCRIPTOR *mFullSmramRanges;
1212
1213 extern EFI_SMM_DRIVER_ENTRY *mSmmCoreDriverEntry;
1214
1215 extern EFI_LOADED_IMAGE_PROTOCOL *mSmmCoreLoadedImage;
1216
1217 //
1218 // Page management
1219 //
1220
1221 typedef struct {
1222 LIST_ENTRY Link;
1223 UINTN NumberOfPages;
1224 } FREE_PAGE_LIST;
1225
1226 extern LIST_ENTRY mSmmMemoryMap;
1227
1228 //
1229 // Pool management
1230 //
1231
1232 //
1233 // MIN_POOL_SHIFT must not be less than 5
1234 //
1235 #define MIN_POOL_SHIFT 6
1236 #define MIN_POOL_SIZE (1 << MIN_POOL_SHIFT)
1237
1238 //
1239 // MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1
1240 //
1241 #define MAX_POOL_SHIFT (EFI_PAGE_SHIFT - 1)
1242 #define MAX_POOL_SIZE (1 << MAX_POOL_SHIFT)
1243
1244 //
1245 // MAX_POOL_INDEX are calculated by maximum and minimum pool sizes
1246 //
1247 #define MAX_POOL_INDEX (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)
1248
1249 #define POOL_HEAD_SIGNATURE SIGNATURE_32('s','p','h','d')
1250
1251 typedef struct {
1252 UINT32 Signature;
1253 BOOLEAN Available;
1254 EFI_MEMORY_TYPE Type;
1255 UINTN Size;
1256 } POOL_HEADER;
1257
1258 #define POOL_TAIL_SIGNATURE SIGNATURE_32('s','p','t','l')
1259
1260 typedef struct {
1261 UINT32 Signature;
1262 UINT32 Reserved;
1263 UINTN Size;
1264 } POOL_TAIL;
1265
1266 #define POOL_OVERHEAD (sizeof(POOL_HEADER) + sizeof(POOL_TAIL))
1267
1268 #define HEAD_TO_TAIL(a) \
1269 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
1270
1271 typedef struct {
1272 POOL_HEADER Header;
1273 LIST_ENTRY Link;
1274 } FREE_POOL_HEADER;
1275
1276 typedef enum {
1277 SmmPoolTypeCode,
1278 SmmPoolTypeData,
1279 SmmPoolTypeMax,
1280 } SMM_POOL_TYPE;
1281
1282 extern LIST_ENTRY mSmmPoolLists[SmmPoolTypeMax][MAX_POOL_INDEX];
1283
1284 /**
1285 Internal Function. Allocate n pages from given free page node.
1286
1287 @param Pages The free page node.
1288 @param NumberOfPages Number of pages to be allocated.
1289 @param MaxAddress Request to allocate memory below this address.
1290
1291 @return Memory address of allocated pages.
1292
1293 **/
1294 UINTN
1295 InternalAllocPagesOnOneNode (
1296 IN OUT FREE_PAGE_LIST *Pages,
1297 IN UINTN NumberOfPages,
1298 IN UINTN MaxAddress
1299 );
1300
1301 /**
1302 Update SMM memory map entry.
1303
1304 @param[in] Type The type of allocation to perform.
1305 @param[in] Memory The base of memory address.
1306 @param[in] NumberOfPages The number of pages to allocate.
1307 @param[in] AddRegion If this memory is new added region.
1308 **/
1309 VOID
1310 ConvertSmmMemoryMapEntry (
1311 IN EFI_MEMORY_TYPE Type,
1312 IN EFI_PHYSICAL_ADDRESS Memory,
1313 IN UINTN NumberOfPages,
1314 IN BOOLEAN AddRegion
1315 );
1316
1317 /**
1318 Internal function. Moves any memory descriptors that are on the
1319 temporary descriptor stack to heap.
1320
1321 **/
1322 VOID
1323 CoreFreeMemoryMapStack (
1324 VOID
1325 );
1326
1327 /**
1328 Frees previous allocated pages.
1329
1330 @param[in] Memory Base address of memory being freed.
1331 @param[in] NumberOfPages The number of pages to free.
1332 @param[in] AddRegion If this memory is new added region.
1333
1334 @retval EFI_NOT_FOUND Could not find the entry that covers the range.
1335 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
1336 @return EFI_SUCCESS Pages successfully freed.
1337
1338 **/
1339 EFI_STATUS
1340 SmmInternalFreePagesEx (
1341 IN EFI_PHYSICAL_ADDRESS Memory,
1342 IN UINTN NumberOfPages,
1343 IN BOOLEAN AddRegion
1344 );
1345
1346 /**
1347 Hook function used to set all Guard pages after entering SMM mode.
1348 **/
1349 VOID
1350 SmmEntryPointMemoryManagementHook (
1351 VOID
1352 );
1353
1354 #endif