]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c
StandaloneMmPkg: Fix ECC error 5007 in StandaloneMmCpu
[mirror_edk2.git] / StandaloneMmPkg / Drivers / StandaloneMmCpu / AArch64 / EventHandle.c
1 /** @file
2
3 Copyright (c) 2016 HP Development Company, L.P.
4 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Base.h>
11 #include <Pi/PiMmCis.h>
12
13
14 #include <Library/ArmSvcLib.h>
15 #include <Library/ArmLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/HobLib.h>
19
20 #include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
21
22 #include <Guid/ZeroGuid.h>
23 #include <Guid/MmramMemoryReserve.h>
24
25 #include <IndustryStandard/ArmStdSmc.h>
26
27 #include "StandaloneMmCpu.h"
28
29 EFI_STATUS
30 EFIAPI
31 MmFoundationEntryRegister (
32 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,
33 IN EFI_MM_ENTRY_POINT MmEntryPoint
34 );
35
36 //
37 // On ARM platforms every event is expected to have a GUID associated with
38 // it. It will be used by the MM Entry point to find the handler for the
39 // event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the
40 // caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver
41 // (e.g. during an asynchronous event). In either case, this context is
42 // maintained in an array which has an entry for each CPU. The pointer to this
43 // array is held in PerCpuGuidedEventContext. Memory is allocated once the
44 // number of CPUs in the system are made known through the
45 // MP_INFORMATION_HOB_DATA.
46 //
47 EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
48
49 // Descriptor with whereabouts of memory used for communication with the normal world
50 EFI_MMRAM_DESCRIPTOR mNsCommBuffer;
51
52 MP_INFORMATION_HOB_DATA *mMpInformationHobData;
53
54 EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
55 0,
56 MmFoundationEntryRegister
57 };
58
59 STATIC EFI_MM_ENTRY_POINT mMmEntryPoint = NULL;
60
61 EFI_STATUS
62 PiMmStandaloneArmTfCpuDriverEntry (
63 IN UINTN EventId,
64 IN UINTN CpuNumber,
65 IN UINTN NsCommBufferAddr
66 )
67 {
68 EFI_MM_COMMUNICATE_HEADER *GuidedEventContext;
69 EFI_MM_ENTRY_CONTEXT MmEntryPointContext;
70 EFI_STATUS Status;
71 UINTN NsCommBufferSize;
72
73 DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));
74
75 Status = EFI_SUCCESS;
76 //
77 // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
78 // receipt of a synchronous MM request. Use the Event ID to distinguish
79 // between synchronous and asynchronous events.
80 //
81 if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {
82 DEBUG ((DEBUG_INFO, "UnRecognized Event - 0x%x\n", EventId));
83 return EFI_INVALID_PARAMETER;
84 }
85
86 // Perform parameter validation of NsCommBufferAddr
87 if (NsCommBufferAddr == (UINTN)NULL) {
88 return EFI_INVALID_PARAMETER;
89 }
90
91 if (NsCommBufferAddr < mNsCommBuffer.PhysicalStart) {
92 return EFI_ACCESS_DENIED;
93 }
94
95 if ((NsCommBufferAddr + sizeof (EFI_MM_COMMUNICATE_HEADER)) >=
96 (mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)) {
97 return EFI_INVALID_PARAMETER;
98 }
99
100 // Find out the size of the buffer passed
101 NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +
102 sizeof (EFI_MM_COMMUNICATE_HEADER);
103
104 // perform bounds check.
105 if (NsCommBufferAddr + NsCommBufferSize >=
106 mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize) {
107 return EFI_ACCESS_DENIED;
108 }
109
110 GuidedEventContext = NULL;
111 // Now that the secure world can see the normal world buffer, allocate
112 // memory to copy the communication buffer to the secure world.
113 Status = mMmst->MmAllocatePool (
114 EfiRuntimeServicesData,
115 NsCommBufferSize,
116 (VOID **) &GuidedEventContext
117 );
118
119 if (Status != EFI_SUCCESS) {
120 DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));
121 return EFI_OUT_OF_RESOURCES;
122 }
123
124 // X1 contains the VA of the normal world memory accessible from
125 // S-EL0
126 CopyMem (GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);
127
128 // Stash the pointer to the allocated Event Context for this CPU
129 PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
130
131 ZeroMem (&MmEntryPointContext, sizeof (EFI_MM_ENTRY_CONTEXT));
132
133 MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
134 MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
135
136 // Populate the MM system table with MP and state information
137 mMmst->CurrentlyExecutingCpu = CpuNumber;
138 mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
139 mMmst->CpuSaveStateSize = 0;
140 mMmst->CpuSaveState = NULL;
141
142 if (mMmEntryPoint == NULL) {
143 DEBUG ((DEBUG_INFO, "Mm Entry point Not Found\n"));
144 return EFI_UNSUPPORTED;
145 }
146
147 mMmEntryPoint (&MmEntryPointContext);
148
149 // Free the memory allocation done earlier and reset the per-cpu context
150 ASSERT (GuidedEventContext);
151 CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);
152
153 Status = mMmst->MmFreePool ((VOID *) GuidedEventContext);
154 if (Status != EFI_SUCCESS) {
155 return EFI_OUT_OF_RESOURCES;
156 }
157 PerCpuGuidedEventContext[CpuNumber] = NULL;
158
159 return Status;
160 }
161
162 EFI_STATUS
163 EFIAPI
164 MmFoundationEntryRegister (
165 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,
166 IN EFI_MM_ENTRY_POINT MmEntryPoint
167 )
168 {
169 // store the entry point in a global
170 mMmEntryPoint = MmEntryPoint;
171 return EFI_SUCCESS;
172 }
173
174 /**
175 This function is the main entry point for an MM handler dispatch
176 or communicate-based callback.
177
178 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
179 @param Context Points to an optional handler context which was specified when the handler was registered.
180 @param CommBuffer A pointer to a collection of data in memory that will
181 be conveyed from a non-MM environment into an MM environment.
182 @param CommBufferSize The size of the CommBuffer.
183
184 @return Status Code
185
186 **/
187 EFI_STATUS
188 EFIAPI
189 PiMmCpuTpFwRootMmiHandler (
190 IN EFI_HANDLE DispatchHandle,
191 IN CONST VOID *Context, OPTIONAL
192 IN OUT VOID *CommBuffer, OPTIONAL
193 IN OUT UINTN *CommBufferSize OPTIONAL
194 )
195 {
196 EFI_STATUS Status;
197 UINTN CpuNumber;
198
199 ASSERT (Context == NULL);
200 ASSERT (CommBuffer == NULL);
201 ASSERT (CommBufferSize == NULL);
202
203 CpuNumber = mMmst->CurrentlyExecutingCpu;
204 if (PerCpuGuidedEventContext[CpuNumber] == NULL) {
205 return EFI_NOT_FOUND;
206 }
207
208 DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
209 PerCpuGuidedEventContext[CpuNumber],
210 PerCpuGuidedEventContext[CpuNumber]->MessageLength));
211
212 Status = mMmst->MmiManage (
213 &PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
214 NULL,
215 PerCpuGuidedEventContext[CpuNumber]->Data,
216 &PerCpuGuidedEventContext[CpuNumber]->MessageLength
217 );
218
219 if (Status != EFI_SUCCESS) {
220 DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));
221 }
222
223 return Status;
224 }