1 /** @file
2 PEI Services Table Pointer Library.
3
4 This library is used for PEIM which does executed from flash device directly but
5 executed in memory.
6
7 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
8 Portiions copyrigth (c) 2011, Apple Inc. All rights reserved.
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php.
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include <PiPei.h>
20 #include <Library/PeiServicesTablePointerLib.h>
21 #include <Library/DebugLib.h>
22
23 #include <Ppi/MemoryDiscovered.h>
24
25
26 CONST EFI_PEI_SERVICES **gPeiServices = NULL;
27
28 /**
29 Caches a pointer PEI Services Table.
30
31 Caches the pointer to the PEI Services Table specified by PeiServicesTablePointer
32 in a CPU specific manner as specified in the CPU binding section of the Platform Initialization
33 Pre-EFI Initialization Core Interface Specification.
34
35 If PeiServicesTablePointer is NULL, then ASSERT().
36
37 @param PeiServicesTablePointer The address of PeiServices pointer.
38 **/
39 VOID
40 EFIAPI
SetPeiServicesTablePointer(IN CONST EFI_PEI_SERVICES ** PeiServicesTablePointer)41 SetPeiServicesTablePointer (
42 IN CONST EFI_PEI_SERVICES ** PeiServicesTablePointer
43 )
44 {
45 ASSERT (PeiServicesTablePointer != NULL);
46 ASSERT (*PeiServicesTablePointer != NULL);
47 gPeiServices = PeiServicesTablePointer;
48 }
49
50 /**
51 Retrieves the cached value of the PEI Services Table pointer.
52
53 Returns the cached value of the PEI Services Table pointer in a CPU specific manner
54 as specified in the CPU binding section of the Platform Initialization Pre-EFI
55 Initialization Core Interface Specification.
56
57 If the cached PEI Services Table pointer is NULL, then ASSERT().
58
59 @return The pointer to PeiServices.
60
61 **/
62 CONST EFI_PEI_SERVICES **
63 EFIAPI
GetPeiServicesTablePointer(VOID)64 GetPeiServicesTablePointer (
65 VOID
66 )
67 {
68 ASSERT (gPeiServices != NULL);
69 ASSERT (*gPeiServices != NULL);
70 return gPeiServices;
71 }
72
73
74
75 /**
76 Notification service to be called when gEmuThunkPpiGuid is installed.
77
78 @param PeiServices Indirect reference to the PEI Services Table.
79 @param NotifyDescriptor Address of the notification descriptor data structure. Type
80 EFI_PEI_NOTIFY_DESCRIPTOR is defined above.
81 @param Ppi Address of the PPI that was installed.
82
83 @retval EFI_STATUS This function will install a PPI to PPI database. The status
84 code will be the code for (*PeiServices)->InstallPpi.
85
86 **/
87 EFI_STATUS
88 EFIAPI
PeiServicesTablePointerNotifyCallback(IN EFI_PEI_SERVICES ** PeiServices,IN EFI_PEI_NOTIFY_DESCRIPTOR * NotifyDescriptor,IN VOID * Ppi)89 PeiServicesTablePointerNotifyCallback (
90 IN EFI_PEI_SERVICES **PeiServices,
91 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
92 IN VOID *Ppi
93 )
94 {
95 gPeiServices = (CONST EFI_PEI_SERVICES **)PeiServices;
96
97 return EFI_SUCCESS;
98 }
99
100
101 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnThunkList = {
102 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
103 &gEfiPeiMemoryDiscoveredPpiGuid,
104 PeiServicesTablePointerNotifyCallback
105 };
106
107
108 /**
109 Constructor register notification on when PPI updates. If PPI is
110 alreay installed registering the notify will cause the handle to
111 run.
112
113 @param FileHandle The handle of FFS header the loaded driver.
114 @param PeiServices The pointer to the PEI services.
115
116 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
117
118 **/
119 EFI_STATUS
120 EFIAPI
PeiServicesTablePointerLibConstructor(IN EFI_PEI_FILE_HANDLE FileHandle,IN CONST EFI_PEI_SERVICES ** PeiServices)121 PeiServicesTablePointerLibConstructor (
122 IN EFI_PEI_FILE_HANDLE FileHandle,
123 IN CONST EFI_PEI_SERVICES **PeiServices
124 )
125 {
126 EFI_STATUS Status;
127
128 gPeiServices = (CONST EFI_PEI_SERVICES **)PeiServices;
129
130 // register to be told when PeiServices pointer is updated
131 Status = (*PeiServices)->NotifyPpi (PeiServices, &mNotifyOnThunkList);
132 ASSERT_EFI_ERROR (Status);
133 return Status;
134 }
135
136 /**
137 Perform CPU specific actions required to migrate the PEI Services Table
138 pointer from temporary RAM to permanent RAM.
139
140 For IA32 CPUs, the PEI Services Table pointer is stored in the 4 bytes
141 immediately preceding the Interrupt Descriptor Table (IDT) in memory.
142 For X64 CPUs, the PEI Services Table pointer is stored in the 8 bytes
143 immediately preceding the Interrupt Descriptor Table (IDT) in memory.
144 For Itanium and ARM CPUs, a the PEI Services Table Pointer is stored in
145 a dedicated CPU register. This means that there is no memory storage
146 associated with storing the PEI Services Table pointer, so no additional
147 migration actions are required for Itanium or ARM CPUs.
148
149 **/
150 VOID
151 EFIAPI
MigratePeiServicesTablePointer(VOID)152 MigratePeiServicesTablePointer (
153 VOID
154 )
155 {
156 //
157 // PEI Services Table pointer is cached in the global variable. No additional
158 // migration actions are required.
159 //
160 return;
161 }
162
163