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