--- /dev/null
+#/** @file\r
+# \r
+# This code provides low level routines that support the Virtual Machine\r
+# for option ROMs.\r
+# \r
+# Copyright (c) 2007 - 2008, Intel Corporation. <BR>\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+# \r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# \r
+#**/\r
+\r
+#---------------------------------------------------------------------------\r
+# Equate files needed.\r
+#---------------------------------------------------------------------------\r
+\r
+#---------------------------------------------------------------------------\r
+##GenericPostSegment SEGMENT USE16\r
+#---------------------------------------------------------------------------\r
+\r
+#****************************************************************************\r
+# EbcLLCALLEX\r
+#\r
+# This function is called to execute an EBC CALLEX instruction.\r
+# This instruction requires that we thunk out to external native\r
+# code. For x64, we switch stacks, copy the arguments to the stack\r
+# and jump to the specified function.\r
+# On return, we restore the stack pointer to its original location.\r
+#\r
+# Destroys no working registers.\r
+#****************************************************************************\r
+.global _CopyMem;\r
+\r
+# VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
+.global _EbcLLCALLEXNative;\r
+_EbcLLCALLEXNative:\r
+ push %rbp\r
+ push %rbx\r
+ mov %rsp, %rbp\r
+ # Function prolog\r
+\r
+ # Copy FuncAddr to a preserved register.\r
+ mov %rcx, %rbx\r
+\r
+ # Set stack pointer to new value\r
+ sub %r8, %rdx\r
+ sub %rsp, %r8\r
+ mov %rsp, %rcx\r
+ sub %rsp, 0x20\r
+ call _CopyMem\r
+ add %rsp, 0x20\r
+\r
+ # Considering the worst case, load 4 potiential arguments\r
+ # into registers.\r
+ mov (%rsp), %rcx\r
+ mov 8(%rsp), %rdx\r
+ mov 10(%rsp), %r8\r
+ mov 18(%rsp), %r9\r
+\r
+ # Now call the external routine\r
+ call *%rbx\r
+\r
+ # Function epilog\r
+ mov %rbp, %rsp\r
+ pop %rbx\r
+ pop %rbp\r
+ ret\r
+\r
+\r
+# UINTN EbcLLGetEbcEntryPoint(VOID);\r
+# Routine Description:\r
+# The VM thunk code stuffs an EBC entry point into a processor\r
+# register. Since we can't use inline assembly to get it from\r
+# the interpreter C code, stuff it into the return value\r
+# register and return.\r
+#\r
+# Arguments:\r
+# None.\r
+#\r
+# Returns:\r
+# The contents of the register in which the entry point is passed.\r
+#\r
+.global _EbcLLGetEbcEntryPoint;\r
+_EbcLLGetEbcEntryPoint:\r
+ ret\r
+\r
+#/*++\r
+#\r
+#Routine Description:\r
+#\r
+# Return the caller's value of the stack pointer.\r
+#\r
+#Arguments:\r
+#\r
+# None.\r
+#\r
+#Returns:\r
+#\r
+# The current value of the stack pointer for the caller. We\r
+# adjust it by 4 here because when they called us, the return address\r
+# is put on the stack, thereby lowering it by 4 bytes.\r
+#\r
+#--*/\r
+\r
+# UINTN EbcLLGetStackPointer()\r
+.global _EbcLLGetStackPointer;\r
+_EbcLLGetStackPointer:\r
+ mov %rsp, %rax\r
+ # Stack adjusted by this much when we were called,\r
+ # For this function, it's 4.\r
+ add $4, %rax\r
+ ret\r
+\r
+.global _EbcLLGetReturnValue;\r
+_EbcLLGetReturnValue:\r
+# UINT64 EbcLLGetReturnValue(VOID);\r
+# Routine Description:\r
+# When EBC calls native, on return the VM has to stuff the return\r
+# value into a VM register. It's assumed here that the value is still\r
+# in the register, so simply return and the caller should get the\r
+# return result properly.\r
+#\r
+# Arguments:\r
+# None.\r
+#\r
+# Returns:\r
+# The unmodified value returned by the native code.\r
+#\r
+ ret\r
--- /dev/null
+;/** @file\r
+; \r
+; This code provides low level routines that support the Virtual Machine.\r
+; for option ROMs.\r
+; \r
+; Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
+; All rights reserved. This program and the accompanying materials\r
+; are licensed and made available under the terms and conditions of the BSD License\r
+; which accompanies this distribution. The full text of the license may be found at\r
+; http://opensource.org/licenses/bsd-license.php\r
+; \r
+; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+; \r
+;**/\r
+\r
+ page ,132\r
+ title VM ASSEMBLY LANGUAGE ROUTINES\r
+\r
+;---------------------------------------------------------------------------\r
+; Equate files needed.\r
+;---------------------------------------------------------------------------\r
+\r
+text SEGMENT\r
+\r
+;---------------------------------------------------------------------------\r
+;;GenericPostSegment SEGMENT USE16\r
+;---------------------------------------------------------------------------\r
+\r
+;****************************************************************************\r
+; EbcLLCALLEX\r
+;\r
+; This function is called to execute an EBC CALLEX instruction.\r
+; This instruction requires that we thunk out to external native\r
+; code. For x64, we switch stacks, copy the arguments to the stack\r
+; and jump to the specified function.\r
+; On return, we restore the stack pointer to its original location.\r
+;\r
+; Destroys no working registers.\r
+;****************************************************************************\r
+; VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
+\r
+CopyMem PROTO Destination:PTR DWORD, Source:PTR DWORD, Count:DWORD\r
+\r
+\r
+EbcLLCALLEXNative PROC PUBLIC\r
+ push rbp\r
+ push rbx\r
+ mov rbp, rsp\r
+ ; Function prolog\r
+\r
+ ; Copy FuncAddr to a preserved register.\r
+ mov rbx, rcx\r
+\r
+ ; Set stack pointer to new value\r
+ sub r8, rdx\r
+ sub rsp, r8\r
+ mov rcx, rsp\r
+ sub rsp, 20h\r
+ call CopyMem\r
+ add rsp, 20h\r
+\r
+ ; Considering the worst case, load 4 potiential arguments\r
+ ; into registers.\r
+ mov rcx, qword ptr [rsp]\r
+ mov rdx, qword ptr [rsp+8h]\r
+ mov r8, qword ptr [rsp+10h]\r
+ mov r9, qword ptr [rsp+18h]\r
+\r
+ ; Now call the external routine\r
+ call rbx\r
+\r
+ ; Function epilog\r
+ mov rsp, rbp\r
+ pop rbx\r
+ pop rbp\r
+ ret\r
+EbcLLCALLEXNative ENDP\r
+\r
+\r
+; UINTN EbcLLGetEbcEntryPoint(VOID);\r
+; Routine Description:\r
+; The VM thunk code stuffs an EBC entry point into a processor\r
+; register. Since we can't use inline assembly to get it from\r
+; the interpreter C code, stuff it into the return value\r
+; register and return.\r
+;\r
+; Arguments:\r
+; None.\r
+;\r
+; Returns:\r
+; The contents of the register in which the entry point is passed.\r
+;\r
+EbcLLGetEbcEntryPoint PROC PUBLIC\r
+ ret\r
+EbcLLGetEbcEntryPoint ENDP\r
+\r
+;/*++\r
+;\r
+;Routine Description:\r
+;\r
+; Return the caller's value of the stack pointer.\r
+;\r
+;Arguments:\r
+;\r
+; None.\r
+;\r
+;Returns:\r
+;\r
+; The current value of the stack pointer for the caller. We\r
+; adjust it by 4 here because when they called us, the return address\r
+; is put on the stack, thereby lowering it by 4 bytes.\r
+;\r
+;--*/\r
+\r
+; UINTN EbcLLGetStackPointer()\r
+EbcLLGetStackPointer PROC PUBLIC\r
+ mov rax, rsp ; get current stack pointer\r
+ ; Stack adjusted by this much when we were called,\r
+ ; For this function, it's 4.\r
+ add rax, 4\r
+ ret\r
+EbcLLGetStackPointer ENDP\r
+\r
+; UINT64 EbcLLGetReturnValue(VOID);\r
+; Routine Description:\r
+; When EBC calls native, on return the VM has to stuff the return\r
+; value into a VM register. It's assumed here that the value is still\r
+; in the register, so simply return and the caller should get the\r
+; return result properly.\r
+;\r
+; Arguments:\r
+; None.\r
+;\r
+; Returns:\r
+; The unmodified value returned by the native code.\r
+;\r
+EbcLLGetReturnValue PROC PUBLIC\r
+ ret\r
+EbcLLGetReturnValue ENDP\r
+\r
+text ENDS\r
+END\r
+\r
--- /dev/null
+/** @file\r
+ This module contains EBC support routines that are customized based on\r
+ the target x64 processor.\r
+\r
+Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+\r
+//\r
+// NOTE: This is the stack size allocated for the interpreter\r
+// when it executes an EBC image. The requirements can change\r
+// based on whether or not a debugger is present, and other\r
+// platform-specific configurations.\r
+//\r
+#define VM_STACK_SIZE (1024 * 8)\r
+#define EBC_THUNK_SIZE 64\r
+\r
+#define STACK_REMAIN_SIZE (1024 * 4)\r
+\r
+\r
+/**\r
+ Pushes a 64 bit unsigned value to the VM stack.\r
+\r
+ @param VmPtr The pointer to current VM context.\r
+ @param Arg The value to be pushed.\r
+\r
+**/\r
+VOID\r
+PushU64 (\r
+ IN VM_CONTEXT *VmPtr,\r
+ IN UINT64 Arg\r
+ )\r
+{\r
+ //\r
+ // Advance the VM stack down, and then copy the argument to the stack.\r
+ // Hope it's aligned.\r
+ //\r
+ VmPtr->R[0] -= sizeof (UINT64);\r
+ *(UINT64 *) VmPtr->R[0] = Arg;\r
+ return;\r
+}\r
+\r
+\r
+/**\r
+ Begin executing an EBC image. The address of the entry point is passed\r
+ in via a processor register, so we'll need to make a call to get the\r
+ value.\r
+\r
+ This is a thunk function. Microsoft x64 compiler only provide fast_call\r
+ calling convention, so the first four arguments are passed by rcx, rdx,\r
+ r8, and r9, while other arguments are passed in stack.\r
+\r
+ @param Arg1 The 1st argument.\r
+ @param Arg2 The 2nd argument.\r
+ @param Arg3 The 3rd argument.\r
+ @param Arg4 The 4th argument.\r
+ @param Arg5 The 5th argument.\r
+ @param Arg6 The 6th argument.\r
+ @param Arg7 The 7th argument.\r
+ @param Arg8 The 8th argument.\r
+ @param Arg9 The 9th argument.\r
+ @param Arg10 The 10th argument.\r
+ @param Arg11 The 11th argument.\r
+ @param Arg12 The 12th argument.\r
+ @param Arg13 The 13th argument.\r
+ @param Arg14 The 14th argument.\r
+ @param Arg15 The 15th argument.\r
+ @param Arg16 The 16th argument.\r
+\r
+ @return The value returned by the EBC application we're going to run.\r
+\r
+**/\r
+UINT64\r
+EbcInterpret (\r
+ IN OUT UINTN Arg1,\r
+ IN OUT UINTN Arg2,\r
+ IN OUT UINTN Arg3,\r
+ IN OUT UINTN Arg4,\r
+ IN OUT UINTN Arg5,\r
+ IN OUT UINTN Arg6,\r
+ IN OUT UINTN Arg7,\r
+ IN OUT UINTN Arg8,\r
+ IN OUT UINTN Arg9,\r
+ IN OUT UINTN Arg10,\r
+ IN OUT UINTN Arg11,\r
+ IN OUT UINTN Arg12,\r
+ IN OUT UINTN Arg13,\r
+ IN OUT UINTN Arg14,\r
+ IN OUT UINTN Arg15,\r
+ IN OUT UINTN Arg16\r
+ )\r
+{\r
+ //\r
+ // Create a new VM context on the stack\r
+ //\r
+ VM_CONTEXT VmContext;\r
+ UINTN Addr;\r
+ EFI_STATUS Status;\r
+ UINTN StackIndex;\r
+\r
+ //\r
+ // Get the EBC entry point from the processor register.\r
+ // Don't call any function before getting the EBC entry\r
+ // point because this will collab the return register.\r
+ //\r
+ Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+ //\r
+ // Now clear out our context\r
+ //\r
+ ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+ //\r
+ // Set the VM instruction pointer to the correct location in memory.\r
+ //\r
+ VmContext.Ip = (VMIP) Addr;\r
+\r
+ //\r
+ // Initialize the stack pointer for the EBC. Get the current system stack\r
+ // pointer and adjust it down by the max needed for the interpreter.\r
+ //\r
+ Addr = EbcLLGetStackPointer ();\r
+\r
+ //\r
+ // Adjust the VM's stack pointer down.\r
+ //\r
+\r
+ Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
+ }\r
+ VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+ VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+ VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+ VmContext.R[0] -= sizeof (UINTN);\r
+\r
+ //\r
+ // Align the stack on a natural boundary.\r
+ //\r
+ VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
+\r
+ //\r
+ // Put a magic value in the stack gap, then adjust down again.\r
+ //\r
+ *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
+ VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+ //\r
+ // The stack upper to LowStackTop is belong to the VM.\r
+ //\r
+ VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
+\r
+ //\r
+ // For the worst case, assume there are 4 arguments passed in registers, store\r
+ // them to VM's stack.\r
+ //\r
+ PushU64 (&VmContext, (UINT64) Arg16);\r
+ PushU64 (&VmContext, (UINT64) Arg15);\r
+ PushU64 (&VmContext, (UINT64) Arg14);\r
+ PushU64 (&VmContext, (UINT64) Arg13);\r
+ PushU64 (&VmContext, (UINT64) Arg12);\r
+ PushU64 (&VmContext, (UINT64) Arg11);\r
+ PushU64 (&VmContext, (UINT64) Arg10);\r
+ PushU64 (&VmContext, (UINT64) Arg9);\r
+ PushU64 (&VmContext, (UINT64) Arg8);\r
+ PushU64 (&VmContext, (UINT64) Arg7);\r
+ PushU64 (&VmContext, (UINT64) Arg6);\r
+ PushU64 (&VmContext, (UINT64) Arg5);\r
+ PushU64 (&VmContext, (UINT64) Arg4);\r
+ PushU64 (&VmContext, (UINT64) Arg3);\r
+ PushU64 (&VmContext, (UINT64) Arg2);\r
+ PushU64 (&VmContext, (UINT64) Arg1);\r
+\r
+ //\r
+ // Interpreter assumes 64-bit return address is pushed on the stack.\r
+ // The x64 does not do this so pad the stack accordingly.\r
+ //\r
+ PushU64 (&VmContext, (UINT64) 0);\r
+ PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
+\r
+ //\r
+ // For x64, this is where we say our return address is\r
+ //\r
+ VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
+\r
+ //\r
+ // We need to keep track of where the EBC stack starts. This way, if the EBC\r
+ // accesses any stack variables above its initial stack setting, then we know\r
+ // it's accessing variables passed into it, which means the data is on the\r
+ // VM's stack.\r
+ // When we're called, on the stack (high to low) we have the parameters, the\r
+ // return address, then the saved ebp. Save the pointer to the return address.\r
+ // EBC code knows that's there, so should look above it for function parameters.\r
+ // The offset is the size of locals (VMContext + Addr + saved ebp).\r
+ // Note that the interpreter assumes there is a 16 bytes of return address on\r
+ // the stack too, so adjust accordingly.\r
+ // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
+ //\r
+\r
+ //\r
+ // Begin executing the EBC code\r
+ //\r
+ EbcExecute (&VmContext);\r
+\r
+ //\r
+ // Return the value in R[7] unless there was an error\r
+ //\r
+ ReturnEBCStack(StackIndex);\r
+ return (UINT64) VmContext.R[7];\r
+}\r
+\r
+\r
+/**\r
+ Begin executing an EBC image. The address of the entry point is passed\r
+ in via a processor register, so we'll need to make a call to get the\r
+ value.\r
+\r
+ @param ImageHandle image handle for the EBC application we're executing\r
+ @param SystemTable standard system table passed into an driver's entry\r
+ point\r
+\r
+ @return The value returned by the EBC application we're going to run.\r
+\r
+**/\r
+UINT64\r
+ExecuteEbcImageEntryPoint (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ //\r
+ // Create a new VM context on the stack\r
+ //\r
+ VM_CONTEXT VmContext;\r
+ UINTN Addr;\r
+ EFI_STATUS Status;\r
+ UINTN StackIndex;\r
+\r
+ //\r
+ // Get the EBC entry point from the processor register. Make sure you don't\r
+ // call any functions before this or you could mess up the register the\r
+ // entry point is passed in.\r
+ //\r
+ Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+ //\r
+ // Now clear out our context\r
+ //\r
+ ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+ //\r
+ // Save the image handle so we can track the thunks created for this image\r
+ //\r
+ VmContext.ImageHandle = ImageHandle;\r
+ VmContext.SystemTable = SystemTable;\r
+\r
+ //\r
+ // Set the VM instruction pointer to the correct location in memory.\r
+ //\r
+ VmContext.Ip = (VMIP) Addr;\r
+\r
+ //\r
+ // Initialize the stack pointer for the EBC. Get the current system stack\r
+ // pointer and adjust it down by the max needed for the interpreter.\r
+ //\r
+ Addr = EbcLLGetStackPointer ();\r
+\r
+ Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
+ }\r
+ VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+ VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+ VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+ VmContext.R[0] -= sizeof (UINTN);\r
+\r
+\r
+ //\r
+ // Put a magic value in the stack gap, then adjust down again\r
+ //\r
+ *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
+ VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+ //\r
+ // Align the stack on a natural boundary\r
+ VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
+ //\r
+ VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
+\r
+ //\r
+ // Simply copy the image handle and system table onto the EBC stack.\r
+ // Greatly simplifies things by not having to spill the args.\r
+ //\r
+ PushU64 (&VmContext, (UINT64) SystemTable);\r
+ PushU64 (&VmContext, (UINT64) ImageHandle);\r
+\r
+ //\r
+ // VM pushes 16-bytes for return address. Simulate that here.\r
+ //\r
+ PushU64 (&VmContext, (UINT64) 0);\r
+ PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
+\r
+ //\r
+ // For x64, this is where we say our return address is\r
+ //\r
+ VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
+\r
+ //\r
+ // Entry function needn't access high stack context, simply\r
+ // put the stack pointer here.\r
+ //\r
+\r
+ //\r
+ // Begin executing the EBC code\r
+ //\r
+ EbcExecute (&VmContext);\r
+\r
+ //\r
+ // Return the value in R[7] unless there was an error\r
+ //\r
+ ReturnEBCStack(StackIndex);\r
+ return (UINT64) VmContext.R[7];\r
+}\r
+\r
+\r
+/**\r
+ Create thunks for an EBC image entry point, or an EBC protocol service.\r
+\r
+ @param ImageHandle Image handle for the EBC image. If not null, then\r
+ we're creating a thunk for an image entry point.\r
+ @param EbcEntryPoint Address of the EBC code that the thunk is to call\r
+ @param Thunk Returned thunk we create here\r
+ @param Flags Flags indicating options for creating the thunk\r
+\r
+ @retval EFI_SUCCESS The thunk was created successfully.\r
+ @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r
+ aligned.\r
+ @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r
+ Thunk.\r
+ @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r
+\r
+**/\r
+EFI_STATUS\r
+EbcCreateThunks (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN VOID *EbcEntryPoint,\r
+ OUT VOID **Thunk,\r
+ IN UINT32 Flags\r
+ )\r
+{\r
+ UINT8 *Ptr;\r
+ UINT8 *ThunkBase;\r
+ UINT32 Index;\r
+ UINT64 Addr;\r
+ INT32 Size;\r
+ INT32 ThunkSize;\r
+\r
+ //\r
+ // Check alignment of pointer to EBC code\r
+ //\r
+ if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Size = EBC_THUNK_SIZE;\r
+ ThunkSize = Size;\r
+\r
+ Ptr = AllocatePool (Size);\r
+\r
+ if (Ptr == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ //\r
+ // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
+ //\r
+ // Save the start address so we can add a pointer to it to a list later.\r
+ //\r
+ ThunkBase = Ptr;\r
+\r
+ //\r
+ // Give them the address of our buffer we're going to fix up\r
+ //\r
+ *Thunk = (VOID *) Ptr;\r
+\r
+ //\r
+ // Add a magic code here to help the VM recognize the thunk..\r
+ // mov rax, ca112ebccall2ebch => 48 B8 BC 2E 11 CA BC 2E 11 CA\r
+ //\r
+ *Ptr = 0x48;\r
+ Ptr++;\r
+ Size--;\r
+ *Ptr = 0xB8;\r
+ Ptr++;\r
+ Size--;\r
+ Addr = (UINT64) 0xCA112EBCCA112EBCULL;\r
+ for (Index = 0; Index < sizeof (Addr); Index++) {\r
+ *Ptr = (UINT8) (UINTN) Addr;\r
+ Addr >>= 8;\r
+ Ptr++;\r
+ Size--;\r
+ }\r
+\r
+ //\r
+ // Add code bytes to load up a processor register with the EBC entry point.\r
+ // mov rax, 123456789abcdef0h => 48 B8 F0 DE BC 9A 78 56 34 12\r
+ // The first 8 bytes of the thunk entry is the address of the EBC\r
+ // entry point.\r
+ //\r
+ *Ptr = 0x48;\r
+ Ptr++;\r
+ Size--;\r
+ *Ptr = 0xB8;\r
+ Ptr++;\r
+ Size--;\r
+ Addr = (UINT64) EbcEntryPoint;\r
+ for (Index = 0; Index < sizeof (Addr); Index++) {\r
+ *Ptr = (UINT8) (UINTN) Addr;\r
+ Addr >>= 8;\r
+ Ptr++;\r
+ Size--;\r
+ }\r
+\r
+ //\r
+ // Stick in a load of ecx with the address of appropriate VM function.\r
+ // Using r11 because it's a volatile register and won't be used in this\r
+ // point.\r
+ // mov r11 123456789abcdef0h => 49 BB F0 DE BC 9A 78 56 34 12\r
+ //\r
+ if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
+ Addr = (UINTN) ExecuteEbcImageEntryPoint;\r
+ } else {\r
+ Addr = (UINTN) EbcInterpret;\r
+ }\r
+\r
+ //\r
+ // mov r11 Addr => 0x49 0xBB\r
+ //\r
+ *Ptr = 0x49;\r
+ Ptr++;\r
+ Size--;\r
+ *Ptr = 0xBB;\r
+ Ptr++;\r
+ Size--;\r
+ for (Index = 0; Index < sizeof (Addr); Index++) {\r
+ *Ptr = (UINT8) Addr;\r
+ Addr >>= 8;\r
+ Ptr++;\r
+ Size--;\r
+ }\r
+ //\r
+ // Stick in jump opcode bytes for jmp r11 => 0x41 0xFF 0xE3\r
+ //\r
+ *Ptr = 0x41;\r
+ Ptr++;\r
+ Size--;\r
+ *Ptr = 0xFF;\r
+ Ptr++;\r
+ Size--;\r
+ *Ptr = 0xE3;\r
+ Size--;\r
+\r
+ //\r
+ // Double check that our defined size is ok (application error)\r
+ //\r
+ if (Size < 0) {\r
+ ASSERT (FALSE);\r
+ return EFI_BUFFER_TOO_SMALL;\r
+ }\r
+ //\r
+ // Add the thunk to the list for this image. Do this last since the add\r
+ // function flushes the cache for us.\r
+ //\r
+ EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ This function is called to execute an EBC CALLEX instruction.\r
+ The function check the callee's content to see whether it is common native\r
+ code or a thunk to another piece of EBC code.\r
+ If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
+ otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
+ be startup which cost time and stack space.\r
+\r
+ @param VmPtr Pointer to a VM context.\r
+ @param FuncAddr Callee's address\r
+ @param NewStackPointer New stack pointer after the call\r
+ @param FramePtr New frame pointer after the call\r
+ @param Size The size of call instruction\r
+\r
+**/\r
+VOID\r
+EbcLLCALLEX (\r
+ IN VM_CONTEXT *VmPtr,\r
+ IN UINTN FuncAddr,\r
+ IN UINTN NewStackPointer,\r
+ IN VOID *FramePtr,\r
+ IN UINT8 Size\r
+ )\r
+{\r
+ UINTN IsThunk;\r
+ UINTN TargetEbcAddr;\r
+\r
+ IsThunk = 1;\r
+ TargetEbcAddr = 0;\r
+\r
+ //\r
+ // Processor specific code to check whether the callee is a thunk to EBC.\r
+ //\r
+ if (*((UINT8 *)FuncAddr) != 0x48) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 1) != 0xB8) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 2) != 0xBC) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 3) != 0x2E) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 4) != 0x11) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 5) != 0xCA) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 6) != 0xBC) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 7) != 0x2E) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 8) != 0x11) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 9) != 0xCA) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 10) != 0x48) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+ if (*((UINT8 *)FuncAddr + 11) != 0xB8) {\r
+ IsThunk = 0;\r
+ goto Action;\r
+ }\r
+\r
+ CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + 12, 8);\r
+\r
+Action:\r
+ if (IsThunk == 1){\r
+ //\r
+ // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
+ // put our return address and frame pointer on the VM stack.\r
+ // Then set the VM's IP to new EBC code.\r
+ //\r
+ VmPtr->R[0] -= 8;\r
+ VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
+ VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
+ VmPtr->R[0] -= 8;\r
+ VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
+\r
+ VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
+ } else {\r
+ //\r
+ // The callee is not a thunk to EBC, call native code.\r
+ //\r
+ EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
+\r
+ //\r
+ // Get return value and advance the IP.\r
+ //\r
+ VmPtr->R[7] = EbcLLGetReturnValue ();\r
+ VmPtr->Ip += Size;\r
+ }\r
+}\r
+\r
+++ /dev/null
-#/** @file\r
-# \r
-# This code provides low level routines that support the Virtual Machine\r
-# for option ROMs.\r
-# \r
-# Copyright (c) 2007 - 2008, Intel Corporation. <BR>\r
-# All rights reserved. This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-# \r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-# \r
-#**/\r
-\r
-#---------------------------------------------------------------------------\r
-# Equate files needed.\r
-#---------------------------------------------------------------------------\r
-\r
-#---------------------------------------------------------------------------\r
-##GenericPostSegment SEGMENT USE16\r
-#---------------------------------------------------------------------------\r
-\r
-#****************************************************************************\r
-# EbcLLCALLEX\r
-#\r
-# This function is called to execute an EBC CALLEX instruction.\r
-# This instruction requires that we thunk out to external native\r
-# code. For x64, we switch stacks, copy the arguments to the stack\r
-# and jump to the specified function.\r
-# On return, we restore the stack pointer to its original location.\r
-#\r
-# Destroys no working registers.\r
-#****************************************************************************\r
-.global _CopyMem;\r
-\r
-# VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
-.global _EbcLLCALLEXNative;\r
-_EbcLLCALLEXNative:\r
- push %rbp\r
- push %rbx\r
- mov %rsp, %rbp\r
- # Function prolog\r
-\r
- # Copy FuncAddr to a preserved register.\r
- mov %rcx, %rbx\r
-\r
- # Set stack pointer to new value\r
- sub %r8, %rdx\r
- sub %rsp, %r8\r
- mov %rsp, %rcx\r
- sub %rsp, 0x20\r
- call _CopyMem\r
- add %rsp, 0x20\r
-\r
- # Considering the worst case, load 4 potiential arguments\r
- # into registers.\r
- mov (%rsp), %rcx\r
- mov 8(%rsp), %rdx\r
- mov 10(%rsp), %r8\r
- mov 18(%rsp), %r9\r
-\r
- # Now call the external routine\r
- call *%rbx\r
-\r
- # Function epilog\r
- mov %rbp, %rsp\r
- pop %rbx\r
- pop %rbp\r
- ret\r
-\r
-\r
-# UINTN EbcLLGetEbcEntryPoint(VOID);\r
-# Routine Description:\r
-# The VM thunk code stuffs an EBC entry point into a processor\r
-# register. Since we can't use inline assembly to get it from\r
-# the interpreter C code, stuff it into the return value\r
-# register and return.\r
-#\r
-# Arguments:\r
-# None.\r
-#\r
-# Returns:\r
-# The contents of the register in which the entry point is passed.\r
-#\r
-.global _EbcLLGetEbcEntryPoint;\r
-_EbcLLGetEbcEntryPoint:\r
- ret\r
-\r
-#/*++\r
-#\r
-#Routine Description:\r
-#\r
-# Return the caller's value of the stack pointer.\r
-#\r
-#Arguments:\r
-#\r
-# None.\r
-#\r
-#Returns:\r
-#\r
-# The current value of the stack pointer for the caller. We\r
-# adjust it by 4 here because when they called us, the return address\r
-# is put on the stack, thereby lowering it by 4 bytes.\r
-#\r
-#--*/\r
-\r
-# UINTN EbcLLGetStackPointer()\r
-.global _EbcLLGetStackPointer;\r
-_EbcLLGetStackPointer:\r
- mov %rsp, %rax\r
- # Stack adjusted by this much when we were called,\r
- # For this function, it's 4.\r
- add $4, %rax\r
- ret\r
-\r
-.global _EbcLLGetReturnValue;\r
-_EbcLLGetReturnValue:\r
-# UINT64 EbcLLGetReturnValue(VOID);\r
-# Routine Description:\r
-# When EBC calls native, on return the VM has to stuff the return\r
-# value into a VM register. It's assumed here that the value is still\r
-# in the register, so simply return and the caller should get the\r
-# return result properly.\r
-#\r
-# Arguments:\r
-# None.\r
-#\r
-# Returns:\r
-# The unmodified value returned by the native code.\r
-#\r
- ret\r
+++ /dev/null
-;/** @file\r
-; \r
-; This code provides low level routines that support the Virtual Machine.\r
-; for option ROMs.\r
-; \r
-; Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-; All rights reserved. This program and the accompanying materials\r
-; are licensed and made available under the terms and conditions of the BSD License\r
-; which accompanies this distribution. The full text of the license may be found at\r
-; http://opensource.org/licenses/bsd-license.php\r
-; \r
-; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-; \r
-;**/\r
-\r
- page ,132\r
- title VM ASSEMBLY LANGUAGE ROUTINES\r
-\r
-;---------------------------------------------------------------------------\r
-; Equate files needed.\r
-;---------------------------------------------------------------------------\r
-\r
-text SEGMENT\r
-\r
-;---------------------------------------------------------------------------\r
-;;GenericPostSegment SEGMENT USE16\r
-;---------------------------------------------------------------------------\r
-\r
-;****************************************************************************\r
-; EbcLLCALLEX\r
-;\r
-; This function is called to execute an EBC CALLEX instruction.\r
-; This instruction requires that we thunk out to external native\r
-; code. For x64, we switch stacks, copy the arguments to the stack\r
-; and jump to the specified function.\r
-; On return, we restore the stack pointer to its original location.\r
-;\r
-; Destroys no working registers.\r
-;****************************************************************************\r
-; VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
-\r
-CopyMem PROTO Destination:PTR DWORD, Source:PTR DWORD, Count:DWORD\r
-\r
-\r
-EbcLLCALLEXNative PROC PUBLIC\r
- push rbp\r
- push rbx\r
- mov rbp, rsp\r
- ; Function prolog\r
-\r
- ; Copy FuncAddr to a preserved register.\r
- mov rbx, rcx\r
-\r
- ; Set stack pointer to new value\r
- sub r8, rdx\r
- sub rsp, r8\r
- mov rcx, rsp\r
- sub rsp, 20h\r
- call CopyMem\r
- add rsp, 20h\r
-\r
- ; Considering the worst case, load 4 potiential arguments\r
- ; into registers.\r
- mov rcx, qword ptr [rsp]\r
- mov rdx, qword ptr [rsp+8h]\r
- mov r8, qword ptr [rsp+10h]\r
- mov r9, qword ptr [rsp+18h]\r
-\r
- ; Now call the external routine\r
- call rbx\r
-\r
- ; Function epilog\r
- mov rsp, rbp\r
- pop rbx\r
- pop rbp\r
- ret\r
-EbcLLCALLEXNative ENDP\r
-\r
-\r
-; UINTN EbcLLGetEbcEntryPoint(VOID);\r
-; Routine Description:\r
-; The VM thunk code stuffs an EBC entry point into a processor\r
-; register. Since we can't use inline assembly to get it from\r
-; the interpreter C code, stuff it into the return value\r
-; register and return.\r
-;\r
-; Arguments:\r
-; None.\r
-;\r
-; Returns:\r
-; The contents of the register in which the entry point is passed.\r
-;\r
-EbcLLGetEbcEntryPoint PROC PUBLIC\r
- ret\r
-EbcLLGetEbcEntryPoint ENDP\r
-\r
-;/*++\r
-;\r
-;Routine Description:\r
-;\r
-; Return the caller's value of the stack pointer.\r
-;\r
-;Arguments:\r
-;\r
-; None.\r
-;\r
-;Returns:\r
-;\r
-; The current value of the stack pointer for the caller. We\r
-; adjust it by 4 here because when they called us, the return address\r
-; is put on the stack, thereby lowering it by 4 bytes.\r
-;\r
-;--*/\r
-\r
-; UINTN EbcLLGetStackPointer()\r
-EbcLLGetStackPointer PROC PUBLIC\r
- mov rax, rsp ; get current stack pointer\r
- ; Stack adjusted by this much when we were called,\r
- ; For this function, it's 4.\r
- add rax, 4\r
- ret\r
-EbcLLGetStackPointer ENDP\r
-\r
-; UINT64 EbcLLGetReturnValue(VOID);\r
-; Routine Description:\r
-; When EBC calls native, on return the VM has to stuff the return\r
-; value into a VM register. It's assumed here that the value is still\r
-; in the register, so simply return and the caller should get the\r
-; return result properly.\r
-;\r
-; Arguments:\r
-; None.\r
-;\r
-; Returns:\r
-; The unmodified value returned by the native code.\r
-;\r
-EbcLLGetReturnValue PROC PUBLIC\r
- ret\r
-EbcLLGetReturnValue ENDP\r
-\r
-text ENDS\r
-END\r
-\r
+++ /dev/null
-/** @file\r
- This module contains EBC support routines that are customized based on\r
- the target x64 processor.\r
-\r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "EbcInt.h"\r
-#include "EbcExecute.h"\r
-\r
-//\r
-// NOTE: This is the stack size allocated for the interpreter\r
-// when it executes an EBC image. The requirements can change\r
-// based on whether or not a debugger is present, and other\r
-// platform-specific configurations.\r
-//\r
-#define VM_STACK_SIZE (1024 * 8)\r
-#define EBC_THUNK_SIZE 64\r
-\r
-#define STACK_REMAIN_SIZE (1024 * 4)\r
-\r
-\r
-/**\r
- Pushes a 64 bit unsigned value to the VM stack.\r
-\r
- @param VmPtr The pointer to current VM context.\r
- @param Arg The value to be pushed.\r
-\r
-**/\r
-VOID\r
-PushU64 (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINT64 Arg\r
- )\r
-{\r
- //\r
- // Advance the VM stack down, and then copy the argument to the stack.\r
- // Hope it's aligned.\r
- //\r
- VmPtr->R[0] -= sizeof (UINT64);\r
- *(UINT64 *) VmPtr->R[0] = Arg;\r
- return;\r
-}\r
-\r
-\r
-/**\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
-\r
- This is a thunk function. Microsoft x64 compiler only provide fast_call\r
- calling convention, so the first four arguments are passed by rcx, rdx,\r
- r8, and r9, while other arguments are passed in stack.\r
-\r
- @param Arg1 The 1st argument.\r
- @param Arg2 The 2nd argument.\r
- @param Arg3 The 3rd argument.\r
- @param Arg4 The 4th argument.\r
- @param Arg5 The 5th argument.\r
- @param Arg6 The 6th argument.\r
- @param Arg7 The 7th argument.\r
- @param Arg8 The 8th argument.\r
- @param Arg9 The 9th argument.\r
- @param Arg10 The 10th argument.\r
- @param Arg11 The 11th argument.\r
- @param Arg12 The 12th argument.\r
- @param Arg13 The 13th argument.\r
- @param Arg14 The 14th argument.\r
- @param Arg15 The 15th argument.\r
- @param Arg16 The 16th argument.\r
-\r
- @return The value returned by the EBC application we're going to run.\r
-\r
-**/\r
-UINT64\r
-EbcInterpret (\r
- IN OUT UINTN Arg1,\r
- IN OUT UINTN Arg2,\r
- IN OUT UINTN Arg3,\r
- IN OUT UINTN Arg4,\r
- IN OUT UINTN Arg5,\r
- IN OUT UINTN Arg6,\r
- IN OUT UINTN Arg7,\r
- IN OUT UINTN Arg8,\r
- IN OUT UINTN Arg9,\r
- IN OUT UINTN Arg10,\r
- IN OUT UINTN Arg11,\r
- IN OUT UINTN Arg12,\r
- IN OUT UINTN Arg13,\r
- IN OUT UINTN Arg14,\r
- IN OUT UINTN Arg15,\r
- IN OUT UINTN Arg16\r
- )\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register.\r
- // Don't call any function before getting the EBC entry\r
- // point because this will collab the return register.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
-\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
- Addr = EbcLLGetStackPointer ();\r
-\r
- //\r
- // Adjust the VM's stack pointer down.\r
- //\r
-\r
- Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
- //\r
- // Align the stack on a natural boundary.\r
- //\r
- VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
-\r
- //\r
- // Put a magic value in the stack gap, then adjust down again.\r
- //\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
-\r
- //\r
- // The stack upper to LowStackTop is belong to the VM.\r
- //\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
-\r
- //\r
- // For the worst case, assume there are 4 arguments passed in registers, store\r
- // them to VM's stack.\r
- //\r
- PushU64 (&VmContext, (UINT64) Arg16);\r
- PushU64 (&VmContext, (UINT64) Arg15);\r
- PushU64 (&VmContext, (UINT64) Arg14);\r
- PushU64 (&VmContext, (UINT64) Arg13);\r
- PushU64 (&VmContext, (UINT64) Arg12);\r
- PushU64 (&VmContext, (UINT64) Arg11);\r
- PushU64 (&VmContext, (UINT64) Arg10);\r
- PushU64 (&VmContext, (UINT64) Arg9);\r
- PushU64 (&VmContext, (UINT64) Arg8);\r
- PushU64 (&VmContext, (UINT64) Arg7);\r
- PushU64 (&VmContext, (UINT64) Arg6);\r
- PushU64 (&VmContext, (UINT64) Arg5);\r
- PushU64 (&VmContext, (UINT64) Arg4);\r
- PushU64 (&VmContext, (UINT64) Arg3);\r
- PushU64 (&VmContext, (UINT64) Arg2);\r
- PushU64 (&VmContext, (UINT64) Arg1);\r
-\r
- //\r
- // Interpreter assumes 64-bit return address is pushed on the stack.\r
- // The x64 does not do this so pad the stack accordingly.\r
- //\r
- PushU64 (&VmContext, (UINT64) 0);\r
- PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
-\r
- //\r
- // For x64, this is where we say our return address is\r
- //\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
-\r
- //\r
- // We need to keep track of where the EBC stack starts. This way, if the EBC\r
- // accesses any stack variables above its initial stack setting, then we know\r
- // it's accessing variables passed into it, which means the data is on the\r
- // VM's stack.\r
- // When we're called, on the stack (high to low) we have the parameters, the\r
- // return address, then the saved ebp. Save the pointer to the return address.\r
- // EBC code knows that's there, so should look above it for function parameters.\r
- // The offset is the size of locals (VMContext + Addr + saved ebp).\r
- // Note that the interpreter assumes there is a 16 bytes of return address on\r
- // the stack too, so adjust accordingly.\r
- // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
- //\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-\r
-/**\r
- Begin executing an EBC image. The address of the entry point is passed\r
- in via a processor register, so we'll need to make a call to get the\r
- value.\r
-\r
- @param ImageHandle image handle for the EBC application we're executing\r
- @param SystemTable standard system table passed into an driver's entry\r
- point\r
-\r
- @return The value returned by the EBC application we're going to run.\r
-\r
-**/\r
-UINT64\r
-ExecuteEbcImageEntryPoint (\r
- IN EFI_HANDLE ImageHandle,\r
- IN EFI_SYSTEM_TABLE *SystemTable\r
- )\r
-{\r
- //\r
- // Create a new VM context on the stack\r
- //\r
- VM_CONTEXT VmContext;\r
- UINTN Addr;\r
- EFI_STATUS Status;\r
- UINTN StackIndex;\r
-\r
- //\r
- // Get the EBC entry point from the processor register. Make sure you don't\r
- // call any functions before this or you could mess up the register the\r
- // entry point is passed in.\r
- //\r
- Addr = EbcLLGetEbcEntryPoint ();\r
-\r
- //\r
- // Now clear out our context\r
- //\r
- ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
-\r
- //\r
- // Save the image handle so we can track the thunks created for this image\r
- //\r
- VmContext.ImageHandle = ImageHandle;\r
- VmContext.SystemTable = SystemTable;\r
-\r
- //\r
- // Set the VM instruction pointer to the correct location in memory.\r
- //\r
- VmContext.Ip = (VMIP) Addr;\r
-\r
- //\r
- // Initialize the stack pointer for the EBC. Get the current system stack\r
- // pointer and adjust it down by the max needed for the interpreter.\r
- //\r
- Addr = EbcLLGetStackPointer ();\r
-\r
- Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
- if (EFI_ERROR(Status)) {\r
- return Status;\r
- }\r
- VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
- VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
- VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
- VmContext.R[0] -= sizeof (UINTN);\r
-\r
-\r
- //\r
- // Put a magic value in the stack gap, then adjust down again\r
- //\r
- *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
- VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
-\r
- //\r
- // Align the stack on a natural boundary\r
- VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
- //\r
- VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
-\r
- //\r
- // Simply copy the image handle and system table onto the EBC stack.\r
- // Greatly simplifies things by not having to spill the args.\r
- //\r
- PushU64 (&VmContext, (UINT64) SystemTable);\r
- PushU64 (&VmContext, (UINT64) ImageHandle);\r
-\r
- //\r
- // VM pushes 16-bytes for return address. Simulate that here.\r
- //\r
- PushU64 (&VmContext, (UINT64) 0);\r
- PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
-\r
- //\r
- // For x64, this is where we say our return address is\r
- //\r
- VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
-\r
- //\r
- // Entry function needn't access high stack context, simply\r
- // put the stack pointer here.\r
- //\r
-\r
- //\r
- // Begin executing the EBC code\r
- //\r
- EbcExecute (&VmContext);\r
-\r
- //\r
- // Return the value in R[7] unless there was an error\r
- //\r
- ReturnEBCStack(StackIndex);\r
- return (UINT64) VmContext.R[7];\r
-}\r
-\r
-\r
-/**\r
- Create thunks for an EBC image entry point, or an EBC protocol service.\r
-\r
- @param ImageHandle Image handle for the EBC image. If not null, then\r
- we're creating a thunk for an image entry point.\r
- @param EbcEntryPoint Address of the EBC code that the thunk is to call\r
- @param Thunk Returned thunk we create here\r
- @param Flags Flags indicating options for creating the thunk\r
-\r
- @retval EFI_SUCCESS The thunk was created successfully.\r
- @retval EFI_INVALID_PARAMETER The parameter of EbcEntryPoint is not 16-bit\r
- aligned.\r
- @retval EFI_OUT_OF_RESOURCES There is not enough memory to created the EBC\r
- Thunk.\r
- @retval EFI_BUFFER_TOO_SMALL EBC_THUNK_SIZE is not larger enough.\r
-\r
-**/\r
-EFI_STATUS\r
-EbcCreateThunks (\r
- IN EFI_HANDLE ImageHandle,\r
- IN VOID *EbcEntryPoint,\r
- OUT VOID **Thunk,\r
- IN UINT32 Flags\r
- )\r
-{\r
- UINT8 *Ptr;\r
- UINT8 *ThunkBase;\r
- UINT32 Index;\r
- UINT64 Addr;\r
- INT32 Size;\r
- INT32 ThunkSize;\r
-\r
- //\r
- // Check alignment of pointer to EBC code\r
- //\r
- if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- Size = EBC_THUNK_SIZE;\r
- ThunkSize = Size;\r
-\r
- Ptr = AllocatePool (Size);\r
-\r
- if (Ptr == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- //\r
- // Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
- //\r
- // Save the start address so we can add a pointer to it to a list later.\r
- //\r
- ThunkBase = Ptr;\r
-\r
- //\r
- // Give them the address of our buffer we're going to fix up\r
- //\r
- *Thunk = (VOID *) Ptr;\r
-\r
- //\r
- // Add a magic code here to help the VM recognize the thunk..\r
- // mov rax, ca112ebccall2ebch => 48 B8 BC 2E 11 CA BC 2E 11 CA\r
- //\r
- *Ptr = 0x48;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xB8;\r
- Ptr++;\r
- Size--;\r
- Addr = (UINT64) 0xCA112EBCCA112EBCULL;\r
- for (Index = 0; Index < sizeof (Addr); Index++) {\r
- *Ptr = (UINT8) (UINTN) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
-\r
- //\r
- // Add code bytes to load up a processor register with the EBC entry point.\r
- // mov rax, 123456789abcdef0h => 48 B8 F0 DE BC 9A 78 56 34 12\r
- // The first 8 bytes of the thunk entry is the address of the EBC\r
- // entry point.\r
- //\r
- *Ptr = 0x48;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xB8;\r
- Ptr++;\r
- Size--;\r
- Addr = (UINT64) EbcEntryPoint;\r
- for (Index = 0; Index < sizeof (Addr); Index++) {\r
- *Ptr = (UINT8) (UINTN) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
-\r
- //\r
- // Stick in a load of ecx with the address of appropriate VM function.\r
- // Using r11 because it's a volatile register and won't be used in this\r
- // point.\r
- // mov r11 123456789abcdef0h => 49 BB F0 DE BC 9A 78 56 34 12\r
- //\r
- if ((Flags & FLAG_THUNK_ENTRY_POINT) != 0) {\r
- Addr = (UINTN) ExecuteEbcImageEntryPoint;\r
- } else {\r
- Addr = (UINTN) EbcInterpret;\r
- }\r
-\r
- //\r
- // mov r11 Addr => 0x49 0xBB\r
- //\r
- *Ptr = 0x49;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xBB;\r
- Ptr++;\r
- Size--;\r
- for (Index = 0; Index < sizeof (Addr); Index++) {\r
- *Ptr = (UINT8) Addr;\r
- Addr >>= 8;\r
- Ptr++;\r
- Size--;\r
- }\r
- //\r
- // Stick in jump opcode bytes for jmp r11 => 0x41 0xFF 0xE3\r
- //\r
- *Ptr = 0x41;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xFF;\r
- Ptr++;\r
- Size--;\r
- *Ptr = 0xE3;\r
- Size--;\r
-\r
- //\r
- // Double check that our defined size is ok (application error)\r
- //\r
- if (Size < 0) {\r
- ASSERT (FALSE);\r
- return EFI_BUFFER_TOO_SMALL;\r
- }\r
- //\r
- // Add the thunk to the list for this image. Do this last since the add\r
- // function flushes the cache for us.\r
- //\r
- EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
- This function is called to execute an EBC CALLEX instruction.\r
- The function check the callee's content to see whether it is common native\r
- code or a thunk to another piece of EBC code.\r
- If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
- otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
- be startup which cost time and stack space.\r
-\r
- @param VmPtr Pointer to a VM context.\r
- @param FuncAddr Callee's address\r
- @param NewStackPointer New stack pointer after the call\r
- @param FramePtr New frame pointer after the call\r
- @param Size The size of call instruction\r
-\r
-**/\r
-VOID\r
-EbcLLCALLEX (\r
- IN VM_CONTEXT *VmPtr,\r
- IN UINTN FuncAddr,\r
- IN UINTN NewStackPointer,\r
- IN VOID *FramePtr,\r
- IN UINT8 Size\r
- )\r
-{\r
- UINTN IsThunk;\r
- UINTN TargetEbcAddr;\r
-\r
- IsThunk = 1;\r
- TargetEbcAddr = 0;\r
-\r
- //\r
- // Processor specific code to check whether the callee is a thunk to EBC.\r
- //\r
- if (*((UINT8 *)FuncAddr) != 0x48) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 1) != 0xB8) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 2) != 0xBC) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 3) != 0x2E) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 4) != 0x11) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 5) != 0xCA) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 6) != 0xBC) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 7) != 0x2E) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 8) != 0x11) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 9) != 0xCA) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 10) != 0x48) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
- if (*((UINT8 *)FuncAddr + 11) != 0xB8) {\r
- IsThunk = 0;\r
- goto Action;\r
- }\r
-\r
- CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + 12, 8);\r
-\r
-Action:\r
- if (IsThunk == 1){\r
- //\r
- // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
- // put our return address and frame pointer on the VM stack.\r
- // Then set the VM's IP to new EBC code.\r
- //\r
- VmPtr->R[0] -= 8;\r
- VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
- VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
- VmPtr->R[0] -= 8;\r
- VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
-\r
- VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
- } else {\r
- //\r
- // The callee is not a thunk to EBC, call native code.\r
- //\r
- EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
-\r
- //\r
- // Get return value and advance the IP.\r
- //\r
- VmPtr->R[7] = EbcLLGetReturnValue ();\r
- VmPtr->Ip += Size;\r
- }\r
-}\r
-\r