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