]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Drivers/StandaloneMmCpu/EventHandle.c
StandaloneMmPkg: Fix check buffer address failed issue from TF-A
[mirror_edk2.git] / StandaloneMmPkg / Drivers / StandaloneMmCpu / 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 Copyright (c) 2021, Linaro Limited
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <Base.h>
12 #include <Pi/PiMmCis.h>
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/ArmFfaSvc.h>
26 #include <IndustryStandard/ArmStdSmc.h>
27
28 #include "StandaloneMmCpu.h"
29
30 EFI_STATUS
31 EFIAPI
32 MmFoundationEntryRegister (
33 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,
34 IN EFI_MM_ENTRY_POINT MmEntryPoint
35 );
36
37 //
38 // On ARM platforms every event is expected to have a GUID associated with
39 // it. It will be used by the MM Entry point to find the handler for the
40 // event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the
41 // caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver
42 // (e.g. during an asynchronous event). In either case, this context is
43 // maintained in an array which has an entry for each CPU. The pointer to this
44 // array is held in PerCpuGuidedEventContext. Memory is allocated once the
45 // number of CPUs in the system are made known through the
46 // MP_INFORMATION_HOB_DATA.
47 //
48 EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;
49
50 // Descriptor with whereabouts of memory used for communication with the normal world
51 EFI_MMRAM_DESCRIPTOR mNsCommBuffer;
52 EFI_MMRAM_DESCRIPTOR mSCommBuffer;
53
54 MP_INFORMATION_HOB_DATA *mMpInformationHobData;
55
56 EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {
57 0,
58 MmFoundationEntryRegister
59 };
60
61 STATIC EFI_MM_ENTRY_POINT mMmEntryPoint = NULL;
62
63 /**
64 Perform bounds check on the common buffer.
65
66 @param [in] BufferAddr Address of the common buffer.
67
68 @retval EFI_SUCCESS Success.
69 @retval EFI_ACCESS_DENIED Access not permitted.
70 **/
71 STATIC
72 EFI_STATUS
73 CheckBufferAddr (
74 IN UINTN BufferAddr
75 )
76 {
77 UINT64 NsCommBufferEnd;
78 UINT64 SCommBufferEnd;
79 UINT64 CommBufferEnd;
80
81 NsCommBufferEnd = mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize;
82 SCommBufferEnd = mSCommBuffer.PhysicalStart + mSCommBuffer.PhysicalSize;
83
84 if ((BufferAddr >= mNsCommBuffer.PhysicalStart) &&
85 (BufferAddr < NsCommBufferEnd))
86 {
87 CommBufferEnd = NsCommBufferEnd;
88 } else if ((BufferAddr >= mSCommBuffer.PhysicalStart) &&
89 (BufferAddr < SCommBufferEnd))
90 {
91 CommBufferEnd = SCommBufferEnd;
92 } else {
93 return EFI_ACCESS_DENIED;
94 }
95
96 if ((CommBufferEnd - BufferAddr) < sizeof (EFI_MM_COMMUNICATE_HEADER)) {
97 return EFI_ACCESS_DENIED;
98 }
99
100 // perform bounds check.
101 if ((CommBufferEnd - BufferAddr - sizeof (EFI_MM_COMMUNICATE_HEADER)) <
102 ((EFI_MM_COMMUNICATE_HEADER *)BufferAddr)->MessageLength)
103 {
104 return EFI_ACCESS_DENIED;
105 }
106
107 return EFI_SUCCESS;
108 }
109
110 /**
111 The PI Standalone MM entry point for the TF-A CPU driver.
112
113 @param [in] EventId The event Id.
114 @param [in] CpuNumber The CPU number.
115 @param [in] NsCommBufferAddr Address of the NS common buffer.
116
117 @retval EFI_SUCCESS Success.
118 @retval EFI_INVALID_PARAMETER A parameter was invalid.
119 @retval EFI_ACCESS_DENIED Access not permitted.
120 @retval EFI_OUT_OF_RESOURCES Out of resources.
121 @retval EFI_UNSUPPORTED Operation not supported.
122 **/
123 EFI_STATUS
124 PiMmStandaloneArmTfCpuDriverEntry (
125 IN UINTN EventId,
126 IN UINTN CpuNumber,
127 IN UINTN NsCommBufferAddr
128 )
129 {
130 EFI_MM_COMMUNICATE_HEADER *GuidedEventContext;
131 EFI_MM_ENTRY_CONTEXT MmEntryPointContext;
132 EFI_STATUS Status;
133 UINTN NsCommBufferSize;
134
135 DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));
136
137 Status = EFI_SUCCESS;
138 //
139 // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
140 // receipt of a synchronous MM request. Use the Event ID to distinguish
141 // between synchronous and asynchronous events.
142 //
143 if ((ARM_SMC_ID_MM_COMMUNICATE != EventId) &&
144 (ARM_SVC_ID_FFA_MSG_SEND_DIRECT_REQ != EventId))
145 {
146 DEBUG ((DEBUG_ERROR, "UnRecognized Event - 0x%x\n", EventId));
147 return EFI_INVALID_PARAMETER;
148 }
149
150 // Perform parameter validation of NsCommBufferAddr
151 if (NsCommBufferAddr == (UINTN)NULL) {
152 return EFI_INVALID_PARAMETER;
153 }
154
155 Status = CheckBufferAddr (NsCommBufferAddr);
156 if (EFI_ERROR (Status)) {
157 DEBUG ((DEBUG_ERROR, "Check Buffer failed: %r\n", Status));
158 return Status;
159 }
160
161 // Find out the size of the buffer passed
162 NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *)NsCommBufferAddr)->MessageLength +
163 sizeof (EFI_MM_COMMUNICATE_HEADER);
164
165 GuidedEventContext = NULL;
166 // Now that the secure world can see the normal world buffer, allocate
167 // memory to copy the communication buffer to the secure world.
168 Status = mMmst->MmAllocatePool (
169 EfiRuntimeServicesData,
170 NsCommBufferSize,
171 (VOID **)&GuidedEventContext
172 );
173
174 if (Status != EFI_SUCCESS) {
175 DEBUG ((DEBUG_ERROR, "Mem alloc failed - 0x%x\n", EventId));
176 return EFI_OUT_OF_RESOURCES;
177 }
178
179 // X1 contains the VA of the normal world memory accessible from
180 // S-EL0
181 CopyMem (GuidedEventContext, (CONST VOID *)NsCommBufferAddr, NsCommBufferSize);
182
183 // Stash the pointer to the allocated Event Context for this CPU
184 PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;
185
186 ZeroMem (&MmEntryPointContext, sizeof (EFI_MM_ENTRY_CONTEXT));
187
188 MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;
189 MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
190
191 // Populate the MM system table with MP and state information
192 mMmst->CurrentlyExecutingCpu = CpuNumber;
193 mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;
194 mMmst->CpuSaveStateSize = 0;
195 mMmst->CpuSaveState = NULL;
196
197 if (mMmEntryPoint == NULL) {
198 DEBUG ((DEBUG_ERROR, "Mm Entry point Not Found\n"));
199 return EFI_UNSUPPORTED;
200 }
201
202 mMmEntryPoint (&MmEntryPointContext);
203
204 // Free the memory allocation done earlier and reset the per-cpu context
205 ASSERT (GuidedEventContext);
206 CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *)GuidedEventContext, NsCommBufferSize);
207
208 Status = mMmst->MmFreePool ((VOID *)GuidedEventContext);
209 if (Status != EFI_SUCCESS) {
210 return EFI_OUT_OF_RESOURCES;
211 }
212
213 PerCpuGuidedEventContext[CpuNumber] = NULL;
214
215 return Status;
216 }
217
218 /**
219 Registers the MM foundation entry point.
220
221 @param [in] This Pointer to the MM Configuration protocol.
222 @param [in] MmEntryPoint Function pointer to the MM Entry point.
223
224 @retval EFI_SUCCESS Success.
225 **/
226 EFI_STATUS
227 EFIAPI
228 MmFoundationEntryRegister (
229 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,
230 IN EFI_MM_ENTRY_POINT MmEntryPoint
231 )
232 {
233 // store the entry point in a global
234 mMmEntryPoint = MmEntryPoint;
235 return EFI_SUCCESS;
236 }
237
238 /**
239 This function is the main entry point for an MM handler dispatch
240 or communicate-based callback.
241
242 @param DispatchHandle The unique handle assigned to this handler by
243 MmiHandlerRegister().
244 @param Context Points to an optional handler context which was
245 specified when the handler was registered.
246 @param CommBuffer A pointer to a collection of data in memory that will
247 be conveyed from a non-MM environment into an
248 MM environment.
249 @param CommBufferSize The size of the CommBuffer.
250
251 @return Status Code
252
253 **/
254 EFI_STATUS
255 EFIAPI
256 PiMmCpuTpFwRootMmiHandler (
257 IN EFI_HANDLE DispatchHandle,
258 IN CONST VOID *Context OPTIONAL,
259 IN OUT VOID *CommBuffer OPTIONAL,
260 IN OUT UINTN *CommBufferSize OPTIONAL
261 )
262 {
263 EFI_STATUS Status;
264 UINTN CpuNumber;
265
266 ASSERT (Context == NULL);
267 ASSERT (CommBuffer == NULL);
268 ASSERT (CommBufferSize == NULL);
269
270 CpuNumber = mMmst->CurrentlyExecutingCpu;
271 if (PerCpuGuidedEventContext[CpuNumber] == NULL) {
272 return EFI_NOT_FOUND;
273 }
274
275 DEBUG ((
276 DEBUG_INFO,
277 "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
278 PerCpuGuidedEventContext[CpuNumber],
279 PerCpuGuidedEventContext[CpuNumber]->MessageLength
280 ));
281
282 Status = mMmst->MmiManage (
283 &PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,
284 NULL,
285 PerCpuGuidedEventContext[CpuNumber]->Data,
286 &PerCpuGuidedEventContext[CpuNumber]->MessageLength
287 );
288
289 if (Status != EFI_SUCCESS) {
290 DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));
291 }
292
293 return Status;
294 }