]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/EbcDxe/AArch64/EbcLowLevel.S
MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / AArch64 / EbcLowLevel.S
index e858227586a8bbe590f2c3b645e6bf263f60e94f..a84d5b3305b81e839929430873782306abc4a254 100644 (file)
-#/** @file\r
-#\r
-#    This code provides low level routines that support the Virtual Machine\r
-#   for option ROMs.\r
-#\r
-#  Copyright (c) 2015, The Linux Foundation. All rights reserved.\r
-#  Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
-#  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
-ASM_GLOBAL ASM_PFX(CopyMem);\r
-ASM_GLOBAL ASM_PFX(EbcInterpret);\r
-ASM_GLOBAL ASM_PFX(ExecuteEbcImageEntryPoint);\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 AArch64, we copy the VM stack into the main stack and then pop\r
-# the first 8 arguments off according to the AArch64 Procedure Call Standard\r
-# On return, we restore the stack pointer to its original location.\r
-#\r
-#****************************************************************************\r
-# UINTN EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
-ASM_GLOBAL ASM_PFX(EbcLLCALLEXNative);\r
+///** @file\r
+//\r
+//  This code provides low level routines that support the Virtual Machine\r
+//  for option ROMs.\r
+//\r
+//  Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>\r
+//  Copyright (c) 2015, The Linux Foundation. All rights reserved.<BR>\r
+//  Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+//\r
+//  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+//\r
+//**/\r
+\r
+ASM_GLOBAL ASM_PFX(EbcLLCALLEXNative)\r
+ASM_GLOBAL ASM_PFX(EbcLLEbcInterpret)\r
+ASM_GLOBAL ASM_PFX(EbcLLExecuteEbcImageEntryPoint)\r
+\r
+ASM_GLOBAL ASM_PFX(mEbcInstructionBufferTemplate)\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 AArch64, we copy the VM stack into the main stack and then pop\r
+// the first 8 arguments off according to the AArch64 Procedure Call Standard\r
+// On return, we restore the stack pointer to its original location.\r
+//\r
+//****************************************************************************\r
+// UINTN EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
 ASM_PFX(EbcLLCALLEXNative):\r
-      stp  x19, x20, [sp, #-16]!\r
-      stp  x29, x30, [sp, #-16]!\r
-\r
-      mov  x19, x0\r
-      mov  x20, sp\r
-      sub  x2, x2, x1   // Length = NewStackPointer-FramePtr\r
-      sub  sp, sp, x2\r
-      sub  sp, sp, #64  // Make sure there is room for at least 8 args in the new stack\r
-      mov  x0, sp\r
-\r
-      bl   CopyMem      // Sp, NewStackPointer, Length\r
-\r
-      ldp  x0, x1, [sp], #16\r
-      ldp  x2, x3, [sp], #16\r
-      ldp  x4, x5, [sp], #16\r
-      ldp  x6, x7, [sp], #16\r
-\r
-      blr  x19\r
-\r
-      mov  sp,  x20\r
-      ldp  x29, x30, [sp], #16\r
-      ldp  x19, x20, [sp], #16\r
-\r
-      ret\r
-\r
-#****************************************************************************\r
-# EbcLLEbcInterpret\r
-#\r
-# This function is called by the thunk code to handle an Native to EBC call\r
-# This can handle up to 16 arguments (1-8 on in x0-x7, 9-16 are on the stack)\r
-# x9 contains the Entry point that will be the first argument when\r
-# EBCInterpret is called.\r
-#\r
-#****************************************************************************\r
-ASM_GLOBAL ASM_PFX(EbcLLEbcInterpret);\r
+    mov     x8, x0                 // Preserve x0\r
+    mov     x9, x1                 // Preserve x1\r
+\r
+    //\r
+    // If the EBC stack frame is smaller than or equal to 64 bytes, we know there\r
+    // are no stacked arguments #9 and beyond that we need to copy to the native\r
+    // stack. In this case, we can perform a tail call which is much more\r
+    // efficient, since there is no need to touch the native stack at all.\r
+    //\r
+    sub     x3, x2, x1              // Length = NewStackPointer - FramePtr\r
+    cmp     x3, #64\r
+    b.gt    1f\r
+\r
+    //\r
+    // While probably harmless in practice, we should not access the VM stack\r
+    // outside of the interval [NewStackPointer, FramePtr), which means we\r
+    // should not blindly fill all 8 argument registers with VM stack data.\r
+    // So instead, calculate how many argument registers we can fill based on\r
+    // the size of the VM stack frame, and skip the remaining ones.\r
+    //\r
+    adr     x0, 0f                  // Take address of 'br' instruction below\r
+    bic     x3, x3, #7              // Ensure correct alignment\r
+    sub     x0, x0, x3, lsr #1      // Subtract 4 bytes for each arg to unstack\r
+    br      x0                      // Skip remaining argument registers\r
+\r
+    ldr     x7, [x9, #56]           // Call with 8 arguments\r
+    ldr     x6, [x9, #48]           //  |\r
+    ldr     x5, [x9, #40]           //  |\r
+    ldr     x4, [x9, #32]           //  |\r
+    ldr     x3, [x9, #24]           //  |\r
+    ldr     x2, [x9, #16]           //  |\r
+    ldr     x1, [x9, #8]            //  V\r
+    ldr     x0, [x9]                // Call with 1 argument\r
+\r
+0:  br      x8                      // Call with no arguments\r
+\r
+    //\r
+    // More than 64 bytes: we need to build the full native stack frame and copy\r
+    // the part of the VM stack exceeding 64 bytes (which may contain stacked\r
+    // arguments) to the native stack\r
+    //\r
+1:  stp     x29, x30, [sp, #-16]!\r
+    mov     x29, sp\r
+\r
+    //\r
+    // Ensure that the stack pointer remains 16 byte aligned,\r
+    // even if the size of the VM stack frame is not a multiple of 16\r
+    //\r
+    add     x1, x1, #64             // Skip over [potential] reg params\r
+    tbz     x3, #3, 2f              // Multiple of 16?\r
+    ldr     x4, [x2, #-8]!          // No? Then push one word\r
+    str     x4, [sp, #-16]!         // ... but use two slots\r
+    b       3f\r
+\r
+2:  ldp     x4, x5, [x2, #-16]!\r
+    stp     x4, x5, [sp, #-16]!\r
+3:  cmp     x2, x1\r
+    b.gt    2b\r
+\r
+    ldp     x0, x1, [x9]\r
+    ldp     x2, x3, [x9, #16]\r
+    ldp     x4, x5, [x9, #32]\r
+    ldp     x6, x7, [x9, #48]\r
+\r
+    blr     x8\r
+\r
+    mov     sp, x29\r
+    ldp     x29, x30, [sp], #16\r
+    ret\r
+\r
+//****************************************************************************\r
+// EbcLLEbcInterpret\r
+//\r
+// This function is called by the thunk code to handle an Native to EBC call\r
+// This can handle up to 16 arguments (1-8 on in x0-x7, 9-16 are on the stack)\r
+// x16 contains the Entry point that will be the first stacked argument when\r
+// EBCInterpret is called.\r
+//\r
+//****************************************************************************\r
 ASM_PFX(EbcLLEbcInterpret):\r
-    stp  x29, x30, [sp, #-16]!\r
-\r
-    // copy the current arguments 9-16 from old location and add arg 7 to stack\r
-    // keeping 16 byte stack alignment\r
-    sub sp, sp, #80\r
-    str x7, [sp]\r
-    ldr x11, [sp, #96]\r
-    str x11, [sp, #8]\r
-    ldr x11, [sp, #104]\r
-    str x11, [sp, #16]\r
-    ldr x11, [sp, #112]\r
-    str x11, [sp, #24]\r
-    ldr x11, [sp, #120]\r
-    str x11, [sp, #32]\r
-    ldr x11, [sp, #128]\r
-    str x11, [sp, #40]\r
-    ldr x11, [sp, #136]\r
-    str x11, [sp, #48]\r
-    ldr x11, [sp, #144]\r
-    str x11, [sp, #56]\r
-    ldr x11, [sp, #152]\r
-    str x11, [sp, #64]\r
-\r
-    // Shift arguments and add entry point and as argument 1\r
-    mov x7, x6\r
-    mov x6, x5\r
-    mov x5, x4\r
-    mov x4, x3\r
-    mov x3, x2\r
-    mov x2, x1\r
-    mov x1, x0\r
-    mov x0, x9\r
-\r
-    # call C-code\r
-    bl ASM_PFX(EbcInterpret)\r
-    add sp, sp, #80\r
-\r
-    ldp  x29, x30, [sp], #16\r
+    stp     x29, x30, [sp, #-16]!\r
+    mov     x29, sp\r
+\r
+    // push the entry point and the address of args #9 - #16 onto the stack\r
+    add     x17, sp, #16\r
+    stp     x16, x17, [sp, #-16]!\r
 \r
+    // call C-code\r
+    bl      ASM_PFX(EbcInterpret)\r
+\r
+    add     sp, sp, #16\r
+    ldp     x29, x30, [sp], #16\r
     ret\r
 \r
-#****************************************************************************\r
-# EbcLLExecuteEbcImageEntryPoint\r
-#\r
-# This function is called by the thunk code to handle the image entry point\r
-# x9 contains the Entry point that will be the first argument when\r
-# ExecuteEbcImageEntryPoint is called.\r
-#\r
-#****************************************************************************\r
-ASM_GLOBAL ASM_PFX(EbcLLExecuteEbcImageEntryPoint);\r
+//****************************************************************************\r
+// EbcLLExecuteEbcImageEntryPoint\r
+//\r
+// This function is called by the thunk code to handle the image entry point\r
+// x16 contains the Entry point that will be the third argument when\r
+// ExecuteEbcImageEntryPoint is called.\r
+//\r
+//****************************************************************************\r
 ASM_PFX(EbcLLExecuteEbcImageEntryPoint):\r
-    stp  x29, x30, [sp, #-16]!\r
-    # build new paramater calling convention\r
-    mov  x2, x1\r
-    mov  x1, x0\r
-    mov  x0, x9\r
-\r
-    # call C-code\r
-    bl ASM_PFX(ExecuteEbcImageEntryPoint)\r
-    ldp  x29, x30, [sp], #16\r
-    ret\r
+    mov     x2, x16\r
+\r
+    // tail call to C code\r
+    b       ASM_PFX(ExecuteEbcImageEntryPoint)\r
+\r
+//****************************************************************************\r
+// mEbcInstructionBufferTemplate\r
+//****************************************************************************\r
+    .section    ".rodata", "a"\r
+    .align      3\r
+ASM_PFX(mEbcInstructionBufferTemplate):\r
+    adr     x17, 0f\r
+    ldp     x16, x17, [x17]\r
+    br      x17\r
+\r
+    //\r
+    // Add a magic code here to help the VM recognize the thunk.\r
+    //\r
+    hlt     #0xEBC\r
+\r
+0:  .quad   0   // EBC_ENTRYPOINT_SIGNATURE\r
+    .quad   0   // EBC_LL_EBC_ENTRYPOINT_SIGNATURE\r