2 Entry point to the Standalone MM Foundation when initialized during the SEC
5 Copyright (c) 2017 - 2018, 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>
27 #include <IndustryStandard/ArmStdSmc.h>
28 #include <IndustryStandard/ArmMmSvc.h>
30 #define SPM_MAJOR_VER_MASK 0xFFFF0000
31 #define SPM_MINOR_VER_MASK 0x0000FFFF
32 #define SPM_MAJOR_VER_SHIFT 16
34 CONST UINT32 SPM_MAJOR_VER
= 0;
35 CONST UINT32 SPM_MINOR_VER
= 1;
37 CONST UINT8 BOOT_PAYLOAD_VERSION
= 1;
39 PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT CpuDriverEntryPoint
= NULL
;
42 Retrieve a pointer to and print the boot information passed by privileged
45 @param SharedBufAddress The pointer memory shared with privileged firmware
48 EFI_SECURE_PARTITION_BOOT_INFO
*
49 GetAndPrintBootinformation (
50 IN VOID
*SharedBufAddress
53 EFI_SECURE_PARTITION_BOOT_INFO
*PayloadBootInfo
;
54 EFI_SECURE_PARTITION_CPU_INFO
*PayloadCpuInfo
;
57 PayloadBootInfo
= (EFI_SECURE_PARTITION_BOOT_INFO
*) SharedBufAddress
;
59 if (PayloadBootInfo
== NULL
) {
60 DEBUG ((DEBUG_ERROR
, "PayloadBootInfo NULL\n"));
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
));
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
));
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
));
85 DEBUG ((DEBUG_INFO
, "NumCpus - 0x%x\n", PayloadBootInfo
->NumCpus
));
86 DEBUG ((DEBUG_INFO
, "CpuInfo - 0x%p\n", PayloadBootInfo
->CpuInfo
));
88 PayloadCpuInfo
= (EFI_SECURE_PARTITION_CPU_INFO
*) PayloadBootInfo
->CpuInfo
;
90 if (PayloadCpuInfo
== NULL
) {
91 DEBUG ((DEBUG_ERROR
, "PayloadCpuInfo NULL\n"));
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
));
101 return PayloadBootInfo
;
107 IN ARM_SVC_ARGS
*EventCompleteSvcArgs
114 ArmCallSvc (EventCompleteSvcArgs
);
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
));
122 Status
= CpuDriverEntryPoint (
123 EventCompleteSvcArgs
->Arg0
,
124 EventCompleteSvcArgs
->Arg3
,
125 EventCompleteSvcArgs
->Arg1
128 if (EFI_ERROR (Status
)) {
129 DEBUG ((DEBUG_ERROR
, "Failed delegated event 0x%x, Status 0x%x\n",
130 EventCompleteSvcArgs
->Arg0
, Status
));
135 SvcStatus
= ARM_SVC_SPM_RET_SUCCESS
;
137 case EFI_INVALID_PARAMETER
:
138 SvcStatus
= ARM_SVC_SPM_RET_INVALID_PARAMS
;
140 case EFI_ACCESS_DENIED
:
141 SvcStatus
= ARM_SVC_SPM_RET_DENIED
;
143 case EFI_OUT_OF_RESOURCES
:
144 SvcStatus
= ARM_SVC_SPM_RET_NO_MEMORY
;
146 case EFI_UNSUPPORTED
:
147 SvcStatus
= ARM_SVC_SPM_RET_NOT_SUPPORTED
;
150 SvcStatus
= ARM_SVC_SPM_RET_NOT_SUPPORTED
;
154 EventCompleteSvcArgs
->Arg0
= ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64
;
155 EventCompleteSvcArgs
->Arg1
= SvcStatus
;
164 UINT16 SpmMajorVersion
;
165 UINT16 SpmMinorVersion
;
167 ARM_SVC_ARGS SpmVersionArgs
;
169 SpmVersionArgs
.Arg0
= ARM_SVC_ID_SPM_VERSION_AARCH32
;
171 ArmCallSvc (&SpmVersionArgs
);
173 SpmVersion
= SpmVersionArgs
.Arg0
;
175 SpmMajorVersion
= ((SpmVersion
& SPM_MAJOR_VER_MASK
) >> SPM_MAJOR_VER_SHIFT
);
176 SpmMinorVersion
= ((SpmVersion
& SPM_MINOR_VER_MASK
) >> 0);
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
))
188 DEBUG ((DEBUG_INFO
, "SPM Version: Major=0x%x, Minor=0x%x\n",
189 SpmMajorVersion
, SpmMinorVersion
));
190 Status
= EFI_SUCCESS
;
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
;
203 The entry point of Standalone MM Foundation.
205 @param SharedBufAddress Pointer to the Buffer between SPM and SP.
213 IN VOID
*SharedBufAddress
,
214 IN UINT64 SharedBufSize
,
219 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
220 EFI_SECURE_PARTITION_BOOT_INFO
*PayloadBootInfo
;
221 ARM_SVC_ARGS InitMmFoundationSvcArgs
;
223 UINT32 SectionHeaderOffset
;
224 UINT16 NumberOfSections
;
228 EFI_PHYSICAL_ADDRESS ImageBase
;
230 // Get Secure Partition Manager Version Information
231 Status
= GetSpmVersion ();
232 if (EFI_ERROR (Status
)) {
236 PayloadBootInfo
= GetAndPrintBootinformation (SharedBufAddress
);
237 if (PayloadBootInfo
== NULL
) {
238 Status
= EFI_UNSUPPORTED
;
242 // Locate PE/COFF File information for the Standalone MM core module
243 Status
= LocateStandaloneMmCorePeCoffData (
244 (EFI_FIRMWARE_VOLUME_HEADER
*) PayloadBootInfo
->SpImageBase
,
249 if (EFI_ERROR (Status
)) {
253 // Obtain the PE/COFF Section information for the Standalone MM core module
254 Status
= GetStandaloneMmCorePeCoffSections (
258 &SectionHeaderOffset
,
262 if (EFI_ERROR (Status
)) {
267 // ImageBase may deviate from ImageContext.ImageAddress if we are dealing
268 // with a TE image, in which case the latter points to the actual offset
269 // of the image, whereas ImageBase refers to the address where the image
270 // would start if the stripped PE headers were still in place. In either
271 // case, we need to fix up ImageBase so it refers to the actual current
274 ImageBase
+= (UINTN
)TeData
- ImageContext
.ImageAddress
;
276 // Update the memory access permissions of individual sections in the
277 // Standalone MM core module
278 Status
= UpdateMmFoundationPeCoffPermissions (
283 ArmSetMemoryRegionNoExec
,
284 ArmSetMemoryRegionReadOnly
,
285 ArmClearMemoryRegionReadOnly
288 if (EFI_ERROR (Status
)) {
292 if (ImageContext
.ImageAddress
!= (UINTN
)TeData
) {
293 ImageContext
.ImageAddress
= (UINTN
)TeData
;
294 ArmSetMemoryRegionNoExec (ImageBase
, SIZE_4KB
);
295 ArmClearMemoryRegionReadOnly (ImageBase
, SIZE_4KB
);
297 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
298 ASSERT_EFI_ERROR (Status
);
302 // Create Hoblist based upon boot information passed by privileged software
304 HobStart
= CreateHobListFromBootInfo (&CpuDriverEntryPoint
, PayloadBootInfo
);
307 // Call the MM Core entry point
309 ProcessModuleEntryPointList (HobStart
);
311 DEBUG ((DEBUG_INFO
, "Shared Cpu Driver EP 0x%lx\n", (UINT64
) CpuDriverEntryPoint
));
314 ZeroMem (&InitMmFoundationSvcArgs
, sizeof(InitMmFoundationSvcArgs
));
315 InitMmFoundationSvcArgs
.Arg0
= ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64
;
316 InitMmFoundationSvcArgs
.Arg1
= Status
;
317 DelegatedEventLoop (&InitMmFoundationSvcArgs
);