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