]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/EbcDxe/AArch64/EbcSupport.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / AArch64 / EbcSupport.c
CommitLineData
a15e5bc2
JB
1/** @file\r
2 This module contains EBC support routines that are customized based on\r
3 the target AArch64 processor.\r
4\r
4d1f5a21
AB
5Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>\r
6Copyright (c) 2015, The Linux Foundation. All rights reserved.<BR>\r
a15e5bc2 7Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
4d1f5a21 8\r
9d510e61 9SPDX-License-Identifier: BSD-2-Clause-Patent\r
a15e5bc2
JB
10\r
11**/\r
12\r
13#include "EbcInt.h"\r
14#include "EbcExecute.h"\r
6f0a3cd2 15#include "EbcDebuggerHook.h"\r
a15e5bc2
JB
16\r
17//\r
18// Amount of space that is not used in the stack\r
19//\r
20#define STACK_REMAIN_SIZE (1024 * 4)\r
21\r
4d1f5a21
AB
22#pragma pack(1)\r
23typedef struct {\r
24 UINT32 Instr[3];\r
25 UINT32 Magic;\r
26 UINT64 EbcEntryPoint;\r
27 UINT64 EbcLlEntryPoint;\r
28} EBC_INSTRUCTION_BUFFER;\r
29#pragma pack()\r
30\r
31extern CONST EBC_INSTRUCTION_BUFFER mEbcInstructionBufferTemplate;\r
a15e5bc2
JB
32\r
33/**\r
34 Begin executing an EBC image.\r
35 This is used for Ebc Thunk call.\r
36\r
37 @return The value returned by the EBC application we're going to run.\r
38\r
39**/\r
40UINT64\r
41EFIAPI\r
42EbcLLEbcInterpret (\r
43 VOID\r
44 );\r
45\r
46/**\r
47 Begin executing an EBC image.\r
48 This is used for Ebc image entrypoint.\r
49\r
50 @return The value returned by the EBC application we're going to run.\r
51\r
52**/\r
53UINT64\r
54EFIAPI\r
55EbcLLExecuteEbcImageEntryPoint (\r
56 VOID\r
57 );\r
58\r
59/**\r
60 Pushes a 64 bit unsigned value to the VM stack.\r
61\r
62 @param VmPtr The pointer to current VM context.\r
63 @param Arg The value to be pushed.\r
64\r
65**/\r
66VOID\r
67PushU64 (\r
68 IN VM_CONTEXT *VmPtr,\r
69 IN UINT64 Arg\r
70 )\r
71{\r
72 //\r
73 // Advance the VM stack down, and then copy the argument to the stack.\r
74 // Hope it's aligned.\r
75 //\r
76 VmPtr->Gpr[0] -= sizeof (UINT64);\r
77 *(UINT64 *) VmPtr->Gpr[0] = Arg;\r
78 return;\r
79}\r
80\r
81\r
82/**\r
83 Begin executing an EBC image.\r
84\r
85 This is a thunk function.\r
86\r
a15e5bc2
JB
87 @param Arg1 The 1st argument.\r
88 @param Arg2 The 2nd argument.\r
89 @param Arg3 The 3rd argument.\r
90 @param Arg4 The 4th argument.\r
91 @param Arg5 The 5th argument.\r
92 @param Arg6 The 6th argument.\r
93 @param Arg7 The 7th argument.\r
94 @param Arg8 The 8th argument.\r
4a2aaff2
AB
95 @param EntryPoint The entrypoint of EBC code.\r
96 @param Args9_16[] Array containing arguments #9 to #16.\r
a15e5bc2
JB
97\r
98 @return The value returned by the EBC application we're going to run.\r
99\r
100**/\r
101UINT64\r
102EFIAPI\r
103EbcInterpret (\r
4a2aaff2
AB
104 IN UINTN Arg1,\r
105 IN UINTN Arg2,\r
106 IN UINTN Arg3,\r
107 IN UINTN Arg4,\r
108 IN UINTN Arg5,\r
109 IN UINTN Arg6,\r
110 IN UINTN Arg7,\r
111 IN UINTN Arg8,\r
112 IN UINTN EntryPoint,\r
113 IN CONST UINTN Args9_16[]\r
a15e5bc2
JB
114 )\r
115{\r
116 //\r
117 // Create a new VM context on the stack\r
118 //\r
119 VM_CONTEXT VmContext;\r
120 UINTN Addr;\r
121 EFI_STATUS Status;\r
122 UINTN StackIndex;\r
123\r
124 //\r
125 // Get the EBC entry point\r
126 //\r
127 Addr = EntryPoint;\r
128\r
129 //\r
130 // Now clear out our context\r
131 //\r
132 ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
133\r
134 //\r
135 // Set the VM instruction pointer to the correct location in memory.\r
136 //\r
137 VmContext.Ip = (VMIP) Addr;\r
138\r
139 //\r
140 // Initialize the stack pointer for the EBC. Get the current system stack\r
141 // pointer and adjust it down by the max needed for the interpreter.\r
142 //\r
143\r
144 //\r
145 // Adjust the VM's stack pointer down.\r
146 //\r
147\r
148 Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
149 if (EFI_ERROR(Status)) {\r
150 return Status;\r
151 }\r
152 VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
153 VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
154 VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r
155 VmContext.Gpr[0] -= sizeof (UINTN);\r
156\r
157 //\r
158 // Align the stack on a natural boundary.\r
159 //\r
160 VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof (UINTN) - 1);\r
161\r
162 //\r
163 // Put a magic value in the stack gap, then adjust down again.\r
164 //\r
165 *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
166 VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];\r
167\r
168 //\r
169 // The stack upper to LowStackTop is belong to the VM.\r
170 //\r
171 VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];\r
172\r
173 //\r
174 // For the worst case, assume there are 4 arguments passed in registers, store\r
175 // them to VM's stack.\r
176 //\r
4a2aaff2
AB
177 PushU64 (&VmContext, (UINT64) Args9_16[7]);\r
178 PushU64 (&VmContext, (UINT64) Args9_16[6]);\r
179 PushU64 (&VmContext, (UINT64) Args9_16[5]);\r
180 PushU64 (&VmContext, (UINT64) Args9_16[4]);\r
181 PushU64 (&VmContext, (UINT64) Args9_16[3]);\r
182 PushU64 (&VmContext, (UINT64) Args9_16[2]);\r
183 PushU64 (&VmContext, (UINT64) Args9_16[1]);\r
184 PushU64 (&VmContext, (UINT64) Args9_16[0]);\r
a15e5bc2
JB
185 PushU64 (&VmContext, (UINT64) Arg8);\r
186 PushU64 (&VmContext, (UINT64) Arg7);\r
187 PushU64 (&VmContext, (UINT64) Arg6);\r
188 PushU64 (&VmContext, (UINT64) Arg5);\r
189 PushU64 (&VmContext, (UINT64) Arg4);\r
190 PushU64 (&VmContext, (UINT64) Arg3);\r
191 PushU64 (&VmContext, (UINT64) Arg2);\r
192 PushU64 (&VmContext, (UINT64) Arg1);\r
193\r
194 //\r
195 // Interpreter assumes 64-bit return address is pushed on the stack.\r
196 // AArch64 does not do this so pad the stack accordingly.\r
197 //\r
198 PushU64 (&VmContext, (UINT64) 0);\r
199 PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
200\r
201 //\r
202 // For AArch64, this is where we say our return address is\r
203 //\r
204 VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];\r
205\r
206 //\r
207 // We need to keep track of where the EBC stack starts. This way, if the EBC\r
208 // accesses any stack variables above its initial stack setting, then we know\r
209 // it's accessing variables passed into it, which means the data is on the\r
210 // VM's stack.\r
211 // When we're called, on the stack (high to low) we have the parameters, the\r
212 // return address, then the saved ebp. Save the pointer to the return address.\r
213 // EBC code knows that's there, so should look above it for function parameters.\r
214 // The offset is the size of locals (VMContext + Addr + saved ebp).\r
215 // Note that the interpreter assumes there is a 16 bytes of return address on\r
216 // the stack too, so adjust accordingly.\r
217 // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
218 //\r
219\r
220 //\r
221 // Begin executing the EBC code\r
222 //\r
6f0a3cd2 223 EbcDebuggerHookEbcInterpret (&VmContext);\r
a15e5bc2
JB
224 EbcExecute (&VmContext);\r
225\r
226 //\r
227 // Return the value in R[7] unless there was an error\r
228 //\r
229 ReturnEBCStack(StackIndex);\r
230 return (UINT64) VmContext.Gpr[7];\r
231}\r
232\r
233\r
234/**\r
235 Begin executing an EBC image.\r
236\r
a15e5bc2
JB
237 @param ImageHandle image handle for the EBC application we're executing\r
238 @param SystemTable standard system table passed into an driver's entry\r
239 point\r
4a2aaff2 240 @param EntryPoint The entrypoint of EBC code.\r
a15e5bc2
JB
241\r
242 @return The value returned by the EBC application we're going to run.\r
243\r
244**/\r
245UINT64\r
246EFIAPI\r
247ExecuteEbcImageEntryPoint (\r
a15e5bc2 248 IN EFI_HANDLE ImageHandle,\r
4a2aaff2
AB
249 IN EFI_SYSTEM_TABLE *SystemTable,\r
250 IN UINTN EntryPoint\r
a15e5bc2
JB
251 )\r
252{\r
253 //\r
254 // Create a new VM context on the stack\r
255 //\r
256 VM_CONTEXT VmContext;\r
257 UINTN Addr;\r
258 EFI_STATUS Status;\r
259 UINTN StackIndex;\r
260\r
261 //\r
262 // Get the EBC entry point\r
263 //\r
264 Addr = EntryPoint;\r
265\r
266 //\r
267 // Now clear out our context\r
268 //\r
269 ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
270\r
271 //\r
272 // Save the image handle so we can track the thunks created for this image\r
273 //\r
274 VmContext.ImageHandle = ImageHandle;\r
275 VmContext.SystemTable = SystemTable;\r
276\r
277 //\r
278 // Set the VM instruction pointer to the correct location in memory.\r
279 //\r
280 VmContext.Ip = (VMIP) Addr;\r
281\r
282 //\r
283 // Initialize the stack pointer for the EBC. Get the current system stack\r
284 // pointer and adjust it down by the max needed for the interpreter.\r
285 //\r
286\r
287 Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
288 if (EFI_ERROR(Status)) {\r
289 return Status;\r
290 }\r
291 VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
292 VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
293 VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0];\r
294 VmContext.Gpr[0] -= sizeof (UINTN);\r
295\r
296\r
297 //\r
298 // Put a magic value in the stack gap, then adjust down again\r
299 //\r
300 *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
301 VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0];\r
302\r
303 //\r
304 // Align the stack on a natural boundary\r
305 VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof(UINTN) - 1);\r
306 //\r
307 VmContext.LowStackTop = (UINTN) VmContext.Gpr[0];\r
308\r
309 //\r
310 // Simply copy the image handle and system table onto the EBC stack.\r
311 // Greatly simplifies things by not having to spill the args.\r
312 //\r
313 PushU64 (&VmContext, (UINT64) SystemTable);\r
314 PushU64 (&VmContext, (UINT64) ImageHandle);\r
315\r
316 //\r
317 // VM pushes 16-bytes for return address. Simulate that here.\r
318 //\r
319 PushU64 (&VmContext, (UINT64) 0);\r
320 PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
321\r
322 //\r
323 // For AArch64, this is where we say our return address is\r
324 //\r
325 VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0];\r
326\r
327 //\r
328 // Entry function needn't access high stack context, simply\r
329 // put the stack pointer here.\r
330 //\r
331\r
332 //\r
333 // Begin executing the EBC code\r
334 //\r
6f0a3cd2 335 EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext);\r
a15e5bc2
JB
336 EbcExecute (&VmContext);\r
337\r
338 //\r
339 // Return the value in R[7] unless there was an error\r
340 //\r
341 ReturnEBCStack(StackIndex);\r
342 return (UINT64) VmContext.Gpr[7];\r
343}\r
344\r
345\r
346/**\r
347 Create thunks for an EBC image entry point, or an EBC protocol service.\r
348\r
349 @param ImageHandle Image handle for the EBC image. If not null, then\r
350 we're creating a thunk for an image entry point.\r
351 @param EbcEntryPoint Address of the EBC code that the thunk is to call\r
352 @param Thunk Returned thunk we create here\r
353 @param Flags Flags indicating options for creating the thunk\r
354\r
355 @retval EFI_SUCCESS The thunk was created successfully.\r
356 @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r
357 aligned.\r
358 @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r
359 Thunk.\r
360 @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r
361\r
362**/\r
363EFI_STATUS\r
364EbcCreateThunks (\r
365 IN EFI_HANDLE ImageHandle,\r
366 IN VOID *EbcEntryPoint,\r
367 OUT VOID **Thunk,\r
368 IN UINT32 Flags\r
369 )\r
370{\r
4d1f5a21 371 EBC_INSTRUCTION_BUFFER *InstructionBuffer;\r
a15e5bc2
JB
372\r
373 //\r
374 // Check alignment of pointer to EBC code\r
375 //\r
376 if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
377 return EFI_INVALID_PARAMETER;\r
378 }\r
379\r
16dc5b68 380 InstructionBuffer = EbcAllocatePoolForThunk (sizeof (EBC_INSTRUCTION_BUFFER));\r
4d1f5a21 381 if (InstructionBuffer == NULL) {\r
a15e5bc2
JB
382 return EFI_OUT_OF_RESOURCES;\r
383 }\r
a15e5bc2
JB
384\r
385 //\r
386 // Give them the address of our buffer we're going to fix up\r
387 //\r
4d1f5a21 388 *Thunk = InstructionBuffer;\r
a15e5bc2
JB
389\r
390 //\r
391 // Copy whole thunk instruction buffer template\r
392 //\r
4d1f5a21
AB
393 CopyMem (InstructionBuffer, &mEbcInstructionBufferTemplate,\r
394 sizeof (EBC_INSTRUCTION_BUFFER));\r
a15e5bc2
JB
395\r
396 //\r
397 // Patch EbcEntryPoint and EbcLLEbcInterpret\r
398 //\r
4d1f5a21
AB
399 InstructionBuffer->EbcEntryPoint = (UINT64)EbcEntryPoint;\r
400 if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
401 InstructionBuffer->EbcLlEntryPoint = (UINT64)EbcLLExecuteEbcImageEntryPoint;\r
402 } else {\r
403 InstructionBuffer->EbcLlEntryPoint = (UINT64)EbcLLEbcInterpret;\r
a15e5bc2
JB
404 }\r
405\r
406 //\r
407 // Add the thunk to the list for this image. Do this last since the add\r
408 // function flushes the cache for us.\r
409 //\r
4d1f5a21
AB
410 EbcAddImageThunk (ImageHandle, InstructionBuffer,\r
411 sizeof (EBC_INSTRUCTION_BUFFER));\r
a15e5bc2
JB
412\r
413 return EFI_SUCCESS;\r
414}\r
415\r
416\r
417/**\r
418 This function is called to execute an EBC CALLEX instruction.\r
419 The function check the callee's content to see whether it is common native\r
420 code or a thunk to another piece of EBC code.\r
421 If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
422 otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
423 be startup which cost time and stack space.\r
424\r
425 @param VmPtr Pointer to a VM context.\r
426 @param FuncAddr Callee's address\r
427 @param NewStackPointer New stack pointer after the call\r
428 @param FramePtr New frame pointer after the call\r
429 @param Size The size of call instruction\r
430\r
431**/\r
432VOID\r
433EbcLLCALLEX (\r
434 IN VM_CONTEXT *VmPtr,\r
435 IN UINTN FuncAddr,\r
436 IN UINTN NewStackPointer,\r
437 IN VOID *FramePtr,\r
438 IN UINT8 Size\r
439 )\r
440{\r
4d1f5a21 441 CONST EBC_INSTRUCTION_BUFFER *InstructionBuffer;\r
a15e5bc2
JB
442\r
443 //\r
444 // Processor specific code to check whether the callee is a thunk to EBC.\r
445 //\r
4d1f5a21 446 InstructionBuffer = (EBC_INSTRUCTION_BUFFER *)FuncAddr;\r
a15e5bc2 447\r
4d1f5a21
AB
448 if (CompareMem (InstructionBuffer, &mEbcInstructionBufferTemplate,\r
449 sizeof(EBC_INSTRUCTION_BUFFER) - 2 * sizeof (UINT64)) == 0) {\r
a15e5bc2
JB
450 //\r
451 // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
452 // put our return address and frame pointer on the VM stack.\r
453 // Then set the VM's IP to new EBC code.\r
454 //\r
455 VmPtr->Gpr[0] -= 8;\r
456 VmWriteMemN (VmPtr, (UINTN) VmPtr->Gpr[0], (UINTN) FramePtr);\r
457 VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->Gpr[0];\r
458 VmPtr->Gpr[0] -= 8;\r
459 VmWriteMem64 (VmPtr, (UINTN) VmPtr->Gpr[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r
460\r
4d1f5a21 461 VmPtr->Ip = (VMIP) InstructionBuffer->EbcEntryPoint;\r
a15e5bc2
JB
462 } else {\r
463 //\r
464 // The callee is not a thunk to EBC, call native code,\r
465 // and get return value.\r
466 //\r
467 VmPtr->Gpr[7] = EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
468\r
469 //\r
470 // Advance the IP.\r
471 //\r
472 VmPtr->Ip += Size;\r
473 }\r
474}\r
475\r