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