2 Entry point to the Standalone MM Foundation when initialized during the SEC
5 Copyright (c) 2017 - 2021, Arm Ltd. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/AArch64/StandaloneMmCoreEntryPoint.h>
16 #include <Guid/MmramMemoryReserve.h>
17 #include <Guid/MpInformation.h>
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 #include <Library/PcdLib.h>
28 #include <IndustryStandard/ArmStdSmc.h>
29 #include <IndustryStandard/ArmMmSvc.h>
30 #include <IndustryStandard/ArmFfaSvc.h>
32 #define SPM_MAJOR_VER_MASK 0xFFFF0000
33 #define SPM_MINOR_VER_MASK 0x0000FFFF
34 #define SPM_MAJOR_VER_SHIFT 16
35 #define FFA_NOT_SUPPORTED -1
37 STATIC CONST UINT32 mSpmMajorVer
= SPM_MAJOR_VERSION
;
38 STATIC CONST UINT32 mSpmMinorVer
= SPM_MINOR_VERSION
;
40 STATIC CONST UINT32 mSpmMajorVerFfa
= SPM_MAJOR_VERSION_FFA
;
41 STATIC CONST UINT32 mSpmMinorVerFfa
= SPM_MINOR_VERSION_FFA
;
43 #define BOOT_PAYLOAD_VERSION 1
45 PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT CpuDriverEntryPoint
= NULL
;
48 Retrieve a pointer to and print the boot information passed by privileged
51 @param [in] SharedBufAddress The pointer memory shared with privileged
55 EFI_SECURE_PARTITION_BOOT_INFO
*
56 GetAndPrintBootinformation (
57 IN VOID
*SharedBufAddress
60 EFI_SECURE_PARTITION_BOOT_INFO
*PayloadBootInfo
;
61 EFI_SECURE_PARTITION_CPU_INFO
*PayloadCpuInfo
;
64 PayloadBootInfo
= (EFI_SECURE_PARTITION_BOOT_INFO
*) SharedBufAddress
;
66 if (PayloadBootInfo
== NULL
) {
67 DEBUG ((DEBUG_ERROR
, "PayloadBootInfo NULL\n"));
71 if (PayloadBootInfo
->Header
.Version
!= BOOT_PAYLOAD_VERSION
) {
72 DEBUG ((DEBUG_ERROR
, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n",
73 PayloadBootInfo
->Header
.Version
, BOOT_PAYLOAD_VERSION
));
77 DEBUG ((DEBUG_INFO
, "NumSpMemRegions - 0x%x\n", PayloadBootInfo
->NumSpMemRegions
));
78 DEBUG ((DEBUG_INFO
, "SpMemBase - 0x%lx\n", PayloadBootInfo
->SpMemBase
));
79 DEBUG ((DEBUG_INFO
, "SpMemLimit - 0x%lx\n", PayloadBootInfo
->SpMemLimit
));
80 DEBUG ((DEBUG_INFO
, "SpImageBase - 0x%lx\n", PayloadBootInfo
->SpImageBase
));
81 DEBUG ((DEBUG_INFO
, "SpStackBase - 0x%lx\n", PayloadBootInfo
->SpStackBase
));
82 DEBUG ((DEBUG_INFO
, "SpHeapBase - 0x%lx\n", PayloadBootInfo
->SpHeapBase
));
83 DEBUG ((DEBUG_INFO
, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo
->SpNsCommBufBase
));
84 DEBUG ((DEBUG_INFO
, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo
->SpSharedBufBase
));
86 DEBUG ((DEBUG_INFO
, "SpImageSize - 0x%x\n", PayloadBootInfo
->SpImageSize
));
87 DEBUG ((DEBUG_INFO
, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo
->SpPcpuStackSize
));
88 DEBUG ((DEBUG_INFO
, "SpHeapSize - 0x%x\n", PayloadBootInfo
->SpHeapSize
));
89 DEBUG ((DEBUG_INFO
, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo
->SpNsCommBufSize
));
90 DEBUG ((DEBUG_INFO
, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo
->SpPcpuSharedBufSize
));
92 DEBUG ((DEBUG_INFO
, "NumCpus - 0x%x\n", PayloadBootInfo
->NumCpus
));
93 DEBUG ((DEBUG_INFO
, "CpuInfo - 0x%p\n", PayloadBootInfo
->CpuInfo
));
95 PayloadCpuInfo
= (EFI_SECURE_PARTITION_CPU_INFO
*) PayloadBootInfo
->CpuInfo
;
97 if (PayloadCpuInfo
== NULL
) {
98 DEBUG ((DEBUG_ERROR
, "PayloadCpuInfo NULL\n"));
102 for (Index
= 0; Index
< PayloadBootInfo
->NumCpus
; Index
++) {
103 DEBUG ((DEBUG_INFO
, "Mpidr - 0x%lx\n", PayloadCpuInfo
[Index
].Mpidr
));
104 DEBUG ((DEBUG_INFO
, "LinearId - 0x%x\n", PayloadCpuInfo
[Index
].LinearId
));
105 DEBUG ((DEBUG_INFO
, "Flags - 0x%x\n", PayloadCpuInfo
[Index
].Flags
));
108 return PayloadBootInfo
;
112 A loop to delegated events.
114 @param [in] EventCompleteSvcArgs Pointer to the event completion arguments.
120 IN ARM_SVC_ARGS
*EventCompleteSvcArgs
127 ArmCallSvc (EventCompleteSvcArgs
);
129 DEBUG ((DEBUG_INFO
, "Received delegated event\n"));
130 DEBUG ((DEBUG_INFO
, "X0 : 0x%x\n", (UINT32
) EventCompleteSvcArgs
->Arg0
));
131 DEBUG ((DEBUG_INFO
, "X1 : 0x%x\n", (UINT32
) EventCompleteSvcArgs
->Arg1
));
132 DEBUG ((DEBUG_INFO
, "X2 : 0x%x\n", (UINT32
) EventCompleteSvcArgs
->Arg2
));
133 DEBUG ((DEBUG_INFO
, "X3 : 0x%x\n", (UINT32
) EventCompleteSvcArgs
->Arg3
));
135 Status
= CpuDriverEntryPoint (
136 EventCompleteSvcArgs
->Arg0
,
137 EventCompleteSvcArgs
->Arg3
,
138 EventCompleteSvcArgs
->Arg1
141 if (EFI_ERROR (Status
)) {
142 DEBUG ((DEBUG_ERROR
, "Failed delegated event 0x%x, Status 0x%x\n",
143 EventCompleteSvcArgs
->Arg0
, Status
));
148 SvcStatus
= ARM_SVC_SPM_RET_SUCCESS
;
150 case EFI_INVALID_PARAMETER
:
151 SvcStatus
= ARM_SVC_SPM_RET_INVALID_PARAMS
;
153 case EFI_ACCESS_DENIED
:
154 SvcStatus
= ARM_SVC_SPM_RET_DENIED
;
156 case EFI_OUT_OF_RESOURCES
:
157 SvcStatus
= ARM_SVC_SPM_RET_NO_MEMORY
;
159 case EFI_UNSUPPORTED
:
160 SvcStatus
= ARM_SVC_SPM_RET_NOT_SUPPORTED
;
163 SvcStatus
= ARM_SVC_SPM_RET_NOT_SUPPORTED
;
167 EventCompleteSvcArgs
->Arg0
= ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64
;
168 EventCompleteSvcArgs
->Arg1
= SvcStatus
;
173 Query the SPM version, check compatibility and return success if compatible.
175 @retval EFI_SUCCESS SPM versions compatible.
176 @retval EFI_UNSUPPORTED SPM versions not compatible.
183 UINT16 CalleeSpmMajorVer
;
184 UINT16 CallerSpmMajorVer
;
185 UINT16 CalleeSpmMinorVer
;
186 UINT16 CallerSpmMinorVer
;
188 ARM_SVC_ARGS SpmVersionArgs
;
190 if (FeaturePcdGet (PcdFfaEnable
)) {
191 SpmVersionArgs
.Arg0
= ARM_SVC_ID_FFA_VERSION_AARCH32
;
192 SpmVersionArgs
.Arg1
= mSpmMajorVerFfa
<< SPM_MAJOR_VER_SHIFT
;
193 SpmVersionArgs
.Arg1
|= mSpmMinorVerFfa
;
194 CallerSpmMajorVer
= mSpmMajorVerFfa
;
195 CallerSpmMinorVer
= mSpmMinorVerFfa
;
197 SpmVersionArgs
.Arg0
= ARM_SVC_ID_SPM_VERSION_AARCH32
;
198 CallerSpmMajorVer
= mSpmMajorVer
;
199 CallerSpmMinorVer
= mSpmMinorVer
;
202 ArmCallSvc (&SpmVersionArgs
);
204 SpmVersion
= SpmVersionArgs
.Arg0
;
205 if (SpmVersion
== FFA_NOT_SUPPORTED
) {
206 return EFI_UNSUPPORTED
;
209 CalleeSpmMajorVer
= ((SpmVersion
& SPM_MAJOR_VER_MASK
) >> SPM_MAJOR_VER_SHIFT
);
210 CalleeSpmMinorVer
= ((SpmVersion
& SPM_MINOR_VER_MASK
) >> 0);
212 // Different major revision values indicate possibly incompatible functions.
213 // For two revisions, A and B, for which the major revision values are
214 // identical, if the minor revision value of revision B is greater than
215 // the minor revision value of revision A, then every function in
216 // revision A must work in a compatible way with revision B.
217 // However, it is possible for revision B to have a higher
218 // function count than revision A.
219 if ((CalleeSpmMajorVer
== CallerSpmMajorVer
) &&
220 (CalleeSpmMinorVer
>= CallerSpmMinorVer
))
222 DEBUG ((DEBUG_INFO
, "SPM Version: Major=0x%x, Minor=0x%x\n",
223 CalleeSpmMajorVer
, CalleeSpmMinorVer
));
224 Status
= EFI_SUCCESS
;
228 DEBUG ((DEBUG_INFO
, "Incompatible SPM Versions.\n Callee Version: Major=0x%x, Minor=0x%x.\n Caller: Major=0x%x, Minor>=0x%x.\n",
229 CalleeSpmMajorVer
, CalleeSpmMinorVer
, CallerSpmMajorVer
, CallerSpmMinorVer
));
230 Status
= EFI_UNSUPPORTED
;
237 The entry point of Standalone MM Foundation.
239 @param [in] SharedBufAddress Pointer to the Buffer between SPM and SP.
240 @param [in] SharedBufSize Size of the shared buffer.
241 @param [in] cookie1 Cookie 1
242 @param [in] cookie2 Cookie 2
248 IN VOID
*SharedBufAddress
,
249 IN UINT64 SharedBufSize
,
254 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
255 EFI_SECURE_PARTITION_BOOT_INFO
*PayloadBootInfo
;
256 ARM_SVC_ARGS InitMmFoundationSvcArgs
;
258 UINT32 SectionHeaderOffset
;
259 UINT16 NumberOfSections
;
263 EFI_PHYSICAL_ADDRESS ImageBase
;
265 // Get Secure Partition Manager Version Information
266 Status
= GetSpmVersion ();
267 if (EFI_ERROR (Status
)) {
271 PayloadBootInfo
= GetAndPrintBootinformation (SharedBufAddress
);
272 if (PayloadBootInfo
== NULL
) {
273 Status
= EFI_UNSUPPORTED
;
277 // Locate PE/COFF File information for the Standalone MM core module
278 Status
= LocateStandaloneMmCorePeCoffData (
279 (EFI_FIRMWARE_VOLUME_HEADER
*) PayloadBootInfo
->SpImageBase
,
284 if (EFI_ERROR (Status
)) {
288 // Obtain the PE/COFF Section information for the Standalone MM core module
289 Status
= GetStandaloneMmCorePeCoffSections (
293 &SectionHeaderOffset
,
297 if (EFI_ERROR (Status
)) {
302 // ImageBase may deviate from ImageContext.ImageAddress if we are dealing
303 // with a TE image, in which case the latter points to the actual offset
304 // of the image, whereas ImageBase refers to the address where the image
305 // would start if the stripped PE headers were still in place. In either
306 // case, we need to fix up ImageBase so it refers to the actual current
309 ImageBase
+= (UINTN
)TeData
- ImageContext
.ImageAddress
;
311 // Update the memory access permissions of individual sections in the
312 // Standalone MM core module
313 Status
= UpdateMmFoundationPeCoffPermissions (
318 ArmSetMemoryRegionNoExec
,
319 ArmSetMemoryRegionReadOnly
,
320 ArmClearMemoryRegionReadOnly
323 if (EFI_ERROR (Status
)) {
327 if (ImageContext
.ImageAddress
!= (UINTN
)TeData
) {
328 ImageContext
.ImageAddress
= (UINTN
)TeData
;
329 ArmSetMemoryRegionNoExec (ImageBase
, SIZE_4KB
);
330 ArmClearMemoryRegionReadOnly (ImageBase
, SIZE_4KB
);
332 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
333 ASSERT_EFI_ERROR (Status
);
337 // Create Hoblist based upon boot information passed by privileged software
339 HobStart
= CreateHobListFromBootInfo (&CpuDriverEntryPoint
, PayloadBootInfo
);
342 // Call the MM Core entry point
344 ProcessModuleEntryPointList (HobStart
);
346 DEBUG ((DEBUG_INFO
, "Shared Cpu Driver EP 0x%lx\n", (UINT64
) CpuDriverEntryPoint
));
349 ZeroMem (&InitMmFoundationSvcArgs
, sizeof(InitMmFoundationSvcArgs
));
350 InitMmFoundationSvcArgs
.Arg0
= ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64
;
351 InitMmFoundationSvcArgs
.Arg1
= Status
;
352 DelegatedEventLoop (&InitMmFoundationSvcArgs
);