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