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