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