]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/AArch64/StandaloneMmCoreEntryPoint.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreEntryPoint / AArch64 / StandaloneMmCoreEntryPoint.c
1 /** @file
2 Entry point to the Standalone MM Foundation when initialized during the SEC
3 phase on ARM platforms
4
5 Copyright (c) 2017 - 2018, ARM Ltd. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10
11 #include <PiMm.h>
12
13 #include <Library/AArch64/StandaloneMmCoreEntryPoint.h>
14
15 #include <PiPei.h>
16 #include <Guid/MmramMemoryReserve.h>
17 #include <Guid/MpInformation.h>
18
19 #include <Library/ArmMmuLib.h>
20 #include <Library/ArmSvcLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/SerialPortLib.h>
26
27 #include <IndustryStandard/ArmStdSmc.h>
28 #include <IndustryStandard/ArmMmSvc.h>
29
30 #define SPM_MAJOR_VER_MASK 0xFFFF0000
31 #define SPM_MINOR_VER_MASK 0x0000FFFF
32 #define SPM_MAJOR_VER_SHIFT 16
33
34 CONST UINT32 SPM_MAJOR_VER = 0;
35 CONST UINT32 SPM_MINOR_VER = 1;
36
37 CONST UINT8 BOOT_PAYLOAD_VERSION = 1;
38
39 PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT CpuDriverEntryPoint = NULL;
40
41 /**
42 Retrieve a pointer to and print the boot information passed by privileged
43 secure firmware
44
45 @param SharedBufAddress The pointer memory shared with privileged firmware
46
47 **/
48 EFI_SECURE_PARTITION_BOOT_INFO *
49 GetAndPrintBootinformation (
50 IN VOID *SharedBufAddress
51 )
52 {
53 EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;
54 EFI_SECURE_PARTITION_CPU_INFO *PayloadCpuInfo;
55 UINTN Index;
56
57 PayloadBootInfo = (EFI_SECURE_PARTITION_BOOT_INFO *) SharedBufAddress;
58
59 if (PayloadBootInfo == NULL) {
60 DEBUG ((DEBUG_ERROR, "PayloadBootInfo NULL\n"));
61 return NULL;
62 }
63
64 if (PayloadBootInfo->Header.Version != BOOT_PAYLOAD_VERSION) {
65 DEBUG ((DEBUG_ERROR, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n",
66 PayloadBootInfo->Header.Version, BOOT_PAYLOAD_VERSION));
67 return NULL;
68 }
69
70 DEBUG ((DEBUG_INFO, "NumSpMemRegions - 0x%x\n", PayloadBootInfo->NumSpMemRegions));
71 DEBUG ((DEBUG_INFO, "SpMemBase - 0x%lx\n", PayloadBootInfo->SpMemBase));
72 DEBUG ((DEBUG_INFO, "SpMemLimit - 0x%lx\n", PayloadBootInfo->SpMemLimit));
73 DEBUG ((DEBUG_INFO, "SpImageBase - 0x%lx\n", PayloadBootInfo->SpImageBase));
74 DEBUG ((DEBUG_INFO, "SpStackBase - 0x%lx\n", PayloadBootInfo->SpStackBase));
75 DEBUG ((DEBUG_INFO, "SpHeapBase - 0x%lx\n", PayloadBootInfo->SpHeapBase));
76 DEBUG ((DEBUG_INFO, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo->SpNsCommBufBase));
77 DEBUG ((DEBUG_INFO, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo->SpSharedBufBase));
78
79 DEBUG ((DEBUG_INFO, "SpImageSize - 0x%x\n", PayloadBootInfo->SpImageSize));
80 DEBUG ((DEBUG_INFO, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo->SpPcpuStackSize));
81 DEBUG ((DEBUG_INFO, "SpHeapSize - 0x%x\n", PayloadBootInfo->SpHeapSize));
82 DEBUG ((DEBUG_INFO, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo->SpNsCommBufSize));
83 DEBUG ((DEBUG_INFO, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo->SpPcpuSharedBufSize));
84
85 DEBUG ((DEBUG_INFO, "NumCpus - 0x%x\n", PayloadBootInfo->NumCpus));
86 DEBUG ((DEBUG_INFO, "CpuInfo - 0x%p\n", PayloadBootInfo->CpuInfo));
87
88 PayloadCpuInfo = (EFI_SECURE_PARTITION_CPU_INFO *) PayloadBootInfo->CpuInfo;
89
90 if (PayloadCpuInfo == NULL) {
91 DEBUG ((DEBUG_ERROR, "PayloadCpuInfo NULL\n"));
92 return NULL;
93 }
94
95 for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {
96 DEBUG ((DEBUG_INFO, "Mpidr - 0x%lx\n", PayloadCpuInfo[Index].Mpidr));
97 DEBUG ((DEBUG_INFO, "LinearId - 0x%x\n", PayloadCpuInfo[Index].LinearId));
98 DEBUG ((DEBUG_INFO, "Flags - 0x%x\n", PayloadCpuInfo[Index].Flags));
99 }
100
101 return PayloadBootInfo;
102 }
103
104 VOID
105 EFIAPI
106 DelegatedEventLoop (
107 IN ARM_SVC_ARGS *EventCompleteSvcArgs
108 )
109 {
110 EFI_STATUS Status;
111 UINTN SvcStatus;
112
113 while (TRUE) {
114 ArmCallSvc (EventCompleteSvcArgs);
115
116 DEBUG ((DEBUG_INFO, "Received delegated event\n"));
117 DEBUG ((DEBUG_INFO, "X0 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg0));
118 DEBUG ((DEBUG_INFO, "X1 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg1));
119 DEBUG ((DEBUG_INFO, "X2 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg2));
120 DEBUG ((DEBUG_INFO, "X3 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg3));
121
122 Status = CpuDriverEntryPoint (
123 EventCompleteSvcArgs->Arg0,
124 EventCompleteSvcArgs->Arg3,
125 EventCompleteSvcArgs->Arg1
126 );
127
128 if (EFI_ERROR (Status)) {
129 DEBUG ((DEBUG_ERROR, "Failed delegated event 0x%x, Status 0x%x\n",
130 EventCompleteSvcArgs->Arg0, Status));
131 }
132
133 switch (Status) {
134 case EFI_SUCCESS:
135 SvcStatus = ARM_SVC_SPM_RET_SUCCESS;
136 break;
137 case EFI_INVALID_PARAMETER:
138 SvcStatus = ARM_SVC_SPM_RET_INVALID_PARAMS;
139 break;
140 case EFI_ACCESS_DENIED:
141 SvcStatus = ARM_SVC_SPM_RET_DENIED;
142 break;
143 case EFI_OUT_OF_RESOURCES:
144 SvcStatus = ARM_SVC_SPM_RET_NO_MEMORY;
145 break;
146 case EFI_UNSUPPORTED:
147 SvcStatus = ARM_SVC_SPM_RET_NOT_SUPPORTED;
148 break;
149 default:
150 SvcStatus = ARM_SVC_SPM_RET_NOT_SUPPORTED;
151 break;
152 }
153
154 EventCompleteSvcArgs->Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
155 EventCompleteSvcArgs->Arg1 = SvcStatus;
156 }
157 }
158
159 STATIC
160 EFI_STATUS
161 GetSpmVersion (VOID)
162 {
163 EFI_STATUS Status;
164 UINT16 SpmMajorVersion;
165 UINT16 SpmMinorVersion;
166 UINT32 SpmVersion;
167 ARM_SVC_ARGS SpmVersionArgs;
168
169 SpmVersionArgs.Arg0 = ARM_SVC_ID_SPM_VERSION_AARCH32;
170
171 ArmCallSvc (&SpmVersionArgs);
172
173 SpmVersion = SpmVersionArgs.Arg0;
174
175 SpmMajorVersion = ((SpmVersion & SPM_MAJOR_VER_MASK) >> SPM_MAJOR_VER_SHIFT);
176 SpmMinorVersion = ((SpmVersion & SPM_MINOR_VER_MASK) >> 0);
177
178 // Different major revision values indicate possibly incompatible functions.
179 // For two revisions, A and B, for which the major revision values are
180 // identical, if the minor revision value of revision B is greater than
181 // the minor revision value of revision A, then every function in
182 // revision A must work in a compatible way with revision B.
183 // However, it is possible for revision B to have a higher
184 // function count than revision A.
185 if ((SpmMajorVersion == SPM_MAJOR_VER) &&
186 (SpmMinorVersion >= SPM_MINOR_VER))
187 {
188 DEBUG ((DEBUG_INFO, "SPM Version: Major=0x%x, Minor=0x%x\n",
189 SpmMajorVersion, SpmMinorVersion));
190 Status = EFI_SUCCESS;
191 }
192 else
193 {
194 DEBUG ((DEBUG_INFO, "Incompatible SPM Versions.\n Current Version: Major=0x%x, Minor=0x%x.\n Expected: Major=0x%x, Minor>=0x%x.\n",
195 SpmMajorVersion, SpmMinorVersion, SPM_MAJOR_VER, SPM_MINOR_VER));
196 Status = EFI_UNSUPPORTED;
197 }
198
199 return Status;
200 }
201
202 /**
203 The entry point of Standalone MM Foundation.
204
205 @param SharedBufAddress Pointer to the Buffer between SPM and SP.
206 @param cookie1.
207 @param cookie2.
208
209 **/
210 VOID
211 EFIAPI
212 _ModuleEntryPoint (
213 IN VOID *SharedBufAddress,
214 IN UINT64 SharedBufSize,
215 IN UINT64 cookie1,
216 IN UINT64 cookie2
217 )
218 {
219 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
220 EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;
221 ARM_SVC_ARGS InitMmFoundationSvcArgs;
222 EFI_STATUS Status;
223 UINT32 SectionHeaderOffset;
224 UINT16 NumberOfSections;
225 VOID *HobStart;
226 VOID *TeData;
227 UINTN TeDataSize;
228
229 // Get Secure Partition Manager Version Information
230 Status = GetSpmVersion ();
231 if (EFI_ERROR (Status)) {
232 goto finish;
233 }
234
235 PayloadBootInfo = GetAndPrintBootinformation (SharedBufAddress);
236 if (PayloadBootInfo == NULL) {
237 Status = EFI_UNSUPPORTED;
238 goto finish;
239 }
240
241 // Locate PE/COFF File information for the Standalone MM core module
242 Status = LocateStandaloneMmCorePeCoffData (
243 (EFI_FIRMWARE_VOLUME_HEADER *) PayloadBootInfo->SpImageBase,
244 &TeData,
245 &TeDataSize
246 );
247
248 if (EFI_ERROR (Status)) {
249 goto finish;
250 }
251
252 // Obtain the PE/COFF Section information for the Standalone MM core module
253 Status = GetStandaloneMmCorePeCoffSections (
254 TeData,
255 &ImageContext,
256 &SectionHeaderOffset,
257 &NumberOfSections
258 );
259
260 if (EFI_ERROR (Status)) {
261 goto finish;
262 }
263
264 // Update the memory access permissions of individual sections in the
265 // Standalone MM core module
266 Status = UpdateMmFoundationPeCoffPermissions (
267 &ImageContext,
268 SectionHeaderOffset,
269 NumberOfSections,
270 ArmSetMemoryRegionNoExec,
271 ArmSetMemoryRegionReadOnly,
272 ArmClearMemoryRegionReadOnly
273 );
274
275 if (EFI_ERROR (Status)) {
276 goto finish;
277 }
278
279 //
280 // Create Hoblist based upon boot information passed by privileged software
281 //
282 HobStart = CreateHobListFromBootInfo (&CpuDriverEntryPoint, PayloadBootInfo);
283
284 //
285 // Call the MM Core entry point
286 //
287 ProcessModuleEntryPointList (HobStart);
288
289 DEBUG ((DEBUG_INFO, "Shared Cpu Driver EP 0x%lx\n", (UINT64) CpuDriverEntryPoint));
290
291 finish:
292 ZeroMem (&InitMmFoundationSvcArgs, sizeof(InitMmFoundationSvcArgs));
293 InitMmFoundationSvcArgs.Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;
294 InitMmFoundationSvcArgs.Arg1 = Status;
295 DelegatedEventLoop (&InitMmFoundationSvcArgs);
296 }