/** Begin executing an EBC image. @param EntryPoint The entrypoint of EBC code. @param ImageHandle image handle for the EBC application we're executing @param SystemTable standard system table passed into an driver's entry point @return The value returned by the EBC application we're going to run. **/ UINT64 EFIAPI ExecuteEbcImageEntryPoint ( IN UINTN EntryPoint, IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { // // Create a new VM context on the stack // VM_CONTEXT VmContext; UINTN Addr; EFI_STATUS Status; UINTN StackIndex; // // Get the EBC entry point // Addr = EntryPoint; // // Now clear out our context // ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT)); // // Save the image handle so we can track the thunks created for this image // VmContext.ImageHandle = ImageHandle; VmContext.SystemTable = SystemTable; // // Set the VM instruction pointer to the correct location in memory. // VmContext.Ip = (VMIP) Addr; // // Initialize the stack pointer for the EBC. Get the current system stack // pointer and adjust it down by the max needed for the interpreter. // Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex); if (EFI_ERROR(Status)) { return Status; } VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE); VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE); VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0]; VmContext.Gpr[0] -= sizeof (UINTN); // // Put a magic value in the stack gap, then adjust down again // *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE; VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0]; // // Align the stack on a natural boundary VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof(UINTN) - 1); // VmContext.LowStackTop = (UINTN) VmContext.Gpr[0]; // // Simply copy the image handle and system table onto the EBC stack. // Greatly simplifies things by not having to spill the args. // PushU64 (&VmContext, (UINT64) SystemTable); PushU64 (&VmContext, (UINT64) ImageHandle); // // VM pushes 16-bytes for return address. Simulate that here. // PushU64 (&VmContext, (UINT64) 0); PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL); // // For x64, this is where we say our return address is // VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0]; // // Entry function needn't access high stack context, simply // put the stack pointer here. // // // Begin executing the EBC code // EbcExecute (&VmContext); // // Return the value in R[7] unless there was an error // ReturnEBCStack(StackIndex); return (UINT64) VmContext.Gpr[7]; }
/** Begin executing an EBC image. @param EntryPoint The entrypoint of EBC code. @param ImageHandle image handle for the EBC application we're executing @param SystemTable standard system table passed into an driver's entry point @return The value returned by the EBC application we're going to run. **/ UINT64 EFIAPI ExecuteEbcImageEntryPoint ( IN UINTN EntryPoint, IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { // // Create a new VM context on the stack // VM_CONTEXT VmContext; UINTN Addr; EFI_STATUS Status; UINTN StackIndex; // // Get the EBC entry point // Addr = EntryPoint; // // Now clear out our context // ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT)); // // Save the image handle so we can track the thunks created for this image // VmContext.ImageHandle = ImageHandle; VmContext.SystemTable = SystemTable; // // Set the VM instruction pointer to the correct location in memory. // VmContext.Ip = (VMIP) Addr; // // Initialize the stack pointer for the EBC. Get the current system stack // pointer and adjust it down by the max needed for the interpreter. // // // Allocate stack pool // Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex); if (EFI_ERROR(Status)) { return Status; } VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE); VmContext.Gpr[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE); VmContext.HighStackBottom = (UINTN)VmContext.Gpr[0]; VmContext.Gpr[0] -= sizeof (UINTN); // // Put a magic value in the stack gap, then adjust down again // *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE; VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0]; // // Align the stack on a natural boundary // VmContext.Gpr[0] &= ~(sizeof(UINTN) - 1); // VmContext.LowStackTop = (UINTN) VmContext.Gpr[0]; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) SystemTable; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) ImageHandle; VmContext.Gpr[0] -= 16; VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0]; // // VM pushes 16-bytes for return address. Simulate that here. // // // Begin executing the EBC code // EbcDebuggerHookExecuteEbcImageEntryPoint (&VmContext); EbcExecute (&VmContext); // // Return the value in Gpr[7] unless there was an error // ReturnEBCStack(StackIndex); return (UINT64) VmContext.Gpr[7]; }
/** Begin executing an EBC image. This is a thunk function. Microsoft x64 compiler only provide fast_call calling convention, so the first four arguments are passed by rcx, rdx, r8, and r9, while other arguments are passed in stack. @param EntryPoint The entrypoint of EBC code. @param Arg1 The 1st argument. @param Arg2 The 2nd argument. @param Arg3 The 3rd argument. @param Arg4 The 4th argument. @param Arg5 The 5th argument. @param Arg6 The 6th argument. @param Arg7 The 7th argument. @param Arg8 The 8th argument. @param Arg9 The 9th argument. @param Arg10 The 10th argument. @param Arg11 The 11th argument. @param Arg12 The 12th argument. @param Arg13 The 13th argument. @param Arg14 The 14th argument. @param Arg15 The 15th argument. @param Arg16 The 16th argument. @return The value returned by the EBC application we're going to run. **/ UINT64 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 ) { // // Create a new VM context on the stack // VM_CONTEXT VmContext; UINTN Addr; EFI_STATUS Status; UINTN StackIndex; // // Get the EBC entry point // Addr = EntryPoint; // // Now clear out our context // ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT)); // // Set the VM instruction pointer to the correct location in memory. // VmContext.Ip = (VMIP) Addr; // // Initialize the stack pointer for the EBC. Get the current system stack // pointer and adjust it down by the max needed for the interpreter. // // // Adjust the VM's stack pointer down. // Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex); if (EFI_ERROR(Status)) { return Status; } VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE); VmContext.Gpr[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE); VmContext.HighStackBottom = (UINTN) VmContext.Gpr[0]; VmContext.Gpr[0] -= sizeof (UINTN); // // Align the stack on a natural boundary. // VmContext.Gpr[0] &= ~(VM_REGISTER)(sizeof (UINTN) - 1); // // Put a magic value in the stack gap, then adjust down again. // *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE; VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0]; // // The stack upper to LowStackTop is belong to the VM. // VmContext.LowStackTop = (UINTN) VmContext.Gpr[0]; // // For the worst case, assume there are 4 arguments passed in registers, store // them to VM's stack. // PushU64 (&VmContext, (UINT64) Arg16); PushU64 (&VmContext, (UINT64) Arg15); PushU64 (&VmContext, (UINT64) Arg14); PushU64 (&VmContext, (UINT64) Arg13); PushU64 (&VmContext, (UINT64) Arg12); PushU64 (&VmContext, (UINT64) Arg11); PushU64 (&VmContext, (UINT64) Arg10); PushU64 (&VmContext, (UINT64) Arg9); PushU64 (&VmContext, (UINT64) Arg8); PushU64 (&VmContext, (UINT64) Arg7); PushU64 (&VmContext, (UINT64) Arg6); PushU64 (&VmContext, (UINT64) Arg5); PushU64 (&VmContext, (UINT64) Arg4); PushU64 (&VmContext, (UINT64) Arg3); PushU64 (&VmContext, (UINT64) Arg2); PushU64 (&VmContext, (UINT64) Arg1); // // Interpreter assumes 64-bit return address is pushed on the stack. // The x64 does not do this so pad the stack accordingly. // PushU64 (&VmContext, (UINT64) 0); PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL); // // For x64, this is where we say our return address is // VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0]; // // We need to keep track of where the EBC stack starts. This way, if the EBC // accesses any stack variables above its initial stack setting, then we know // it's accessing variables passed into it, which means the data is on the // VM's stack. // When we're called, on the stack (high to low) we have the parameters, the // return address, then the saved ebp. Save the pointer to the return address. // EBC code knows that's there, so should look above it for function parameters. // The offset is the size of locals (VMContext + Addr + saved ebp). // Note that the interpreter assumes there is a 16 bytes of return address on // the stack too, so adjust accordingly. // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr)); // // // Begin executing the EBC code // EbcExecute (&VmContext); // // Return the value in R[7] unless there was an error // ReturnEBCStack(StackIndex); return (UINT64) VmContext.Gpr[7]; }
/** Begin executing an EBC image. The address of the entry point is passed in via a processor register, so we'll need to make a call to get the value. This is a thunk function. Microsoft x64 compiler only provide fast_call calling convention, so the first four arguments are passed by rcx, rdx, r8, and r9, while other arguments are passed in stack. @param Arg1 The 1st argument. @param Arg2 The 2nd argument. @param Arg3 The 3rd argument. @param Arg4 The 4th argument. @param Arg5 The 5th argument. @param Arg6 The 6th argument. @param Arg7 The 7th argument. @param Arg8 The 8th argument. @param Arg9 The 9th argument. @param Arg10 The 10th argument. @param Arg11 The 11th argument. @param Arg12 The 12th argument. @param Arg13 The 13th argument. @param Arg14 The 14th argument. @param Arg15 The 15th argument. @param Arg16 The 16th argument. @return The value returned by the EBC application we're going to run. **/ UINT64 EbcInterpret ( IN OUT UINTN Arg1, IN OUT UINTN Arg2, IN OUT UINTN Arg3, IN OUT UINTN Arg4, IN OUT UINTN Arg5, IN OUT UINTN Arg6, IN OUT UINTN Arg7, IN OUT UINTN Arg8, IN OUT UINTN Arg9, IN OUT UINTN Arg10, IN OUT UINTN Arg11, IN OUT UINTN Arg12, IN OUT UINTN Arg13, IN OUT UINTN Arg14, IN OUT UINTN Arg15, IN OUT UINTN Arg16 ) { // // Create a new VM context on the stack // VM_CONTEXT VmContext; UINTN Addr; EFI_STATUS Status; UINTN StackIndex; // // Get the EBC entry point from the processor register. // Addr = EbcLLGetEbcEntryPoint (); // // Now clear out our context // ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT)); // // Set the VM instruction pointer to the correct location in memory. // VmContext.Ip = (VMIP) Addr; // // Initialize the stack pointer for the EBC. Get the current system stack // pointer and adjust it down by the max needed for the interpreter. // // // Align the stack on a natural boundary // // // Allocate stack pool // Status = GetEBCStack((EFI_HANDLE)-1, &VmContext.StackPool, &StackIndex); if (EFI_ERROR(Status)) { return Status; } VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE); VmContext.Gpr[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE); VmContext.HighStackBottom = (UINTN)VmContext.Gpr[0]; VmContext.Gpr[0] &= ~(sizeof (UINTN) - 1); VmContext.Gpr[0] -= sizeof (UINTN); // // Put a magic value in the stack gap, then adjust down again // *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) VM_STACK_KEY_VALUE; VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.Gpr[0]; VmContext.LowStackTop = (UINTN) VmContext.Gpr[0]; // // For IA32, this is where we say our return address is // VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg16; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg15; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg14; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg13; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg12; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg11; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg10; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg9; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg8; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg7; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg6; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg5; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg4; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg3; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg2; VmContext.Gpr[0] -= sizeof (UINTN); *(UINTN *) (UINTN) (VmContext.Gpr[0]) = (UINTN) Arg1; VmContext.Gpr[0] -= 16; VmContext.StackRetAddr = (UINT64) VmContext.Gpr[0]; // // We need to keep track of where the EBC stack starts. This way, if the EBC // accesses any stack variables above its initial stack setting, then we know // it's accessing variables passed into it, which means the data is on the // VM's stack. // When we're called, on the stack (high to low) we have the parameters, the // return address, then the saved ebp. Save the pointer to the return address. // EBC code knows that's there, so should look above it for function parameters. // The offset is the size of locals (VMContext + Addr + saved ebp). // Note that the interpreter assumes there is a 16 bytes of return address on // the stack too, so adjust accordingly. // VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr)); // // // Begin executing the EBC code // EbcExecute (&VmContext); // // Return the value in R[7] unless there was an error // ReturnEBCStack(StackIndex); return (UINT64) VmContext.Gpr[7]; }