]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EbcDxe/AArch64/EbcLowLevel.S
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / AArch64 / EbcLowLevel.S
1 ///** @file
2 //
3 // This code provides low level routines that support the Virtual Machine
4 // for option ROMs.
5 //
6 // Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>
7 // Copyright (c) 2015, The Linux Foundation. All rights reserved.<BR>
8 // Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
9 //
10 // SPDX-License-Identifier: BSD-2-Clause-Patent
11 //
12 //**/
13
14 ASM_GLOBAL ASM_PFX(EbcLLCALLEXNative)
15 ASM_GLOBAL ASM_PFX(EbcLLEbcInterpret)
16 ASM_GLOBAL ASM_PFX(EbcLLExecuteEbcImageEntryPoint)
17
18 ASM_GLOBAL ASM_PFX(mEbcInstructionBufferTemplate)
19
20 //****************************************************************************
21 // EbcLLCALLEX
22 //
23 // This function is called to execute an EBC CALLEX instruction.
24 // This instruction requires that we thunk out to external native
25 // code. For AArch64, we copy the VM stack into the main stack and then pop
26 // the first 8 arguments off according to the AArch64 Procedure Call Standard
27 // On return, we restore the stack pointer to its original location.
28 //
29 //****************************************************************************
30 // UINTN EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)
31 ASM_PFX(EbcLLCALLEXNative):
32 mov x8, x0 // Preserve x0
33 mov x9, x1 // Preserve x1
34
35 //
36 // If the EBC stack frame is smaller than or equal to 64 bytes, we know there
37 // are no stacked arguments #9 and beyond that we need to copy to the native
38 // stack. In this case, we can perform a tail call which is much more
39 // efficient, since there is no need to touch the native stack at all.
40 //
41 sub x3, x2, x1 // Length = NewStackPointer - FramePtr
42 cmp x3, #64
43 b.gt 1f
44
45 //
46 // While probably harmless in practice, we should not access the VM stack
47 // outside of the interval [NewStackPointer, FramePtr), which means we
48 // should not blindly fill all 8 argument registers with VM stack data.
49 // So instead, calculate how many argument registers we can fill based on
50 // the size of the VM stack frame, and skip the remaining ones.
51 //
52 adr x0, 0f // Take address of 'br' instruction below
53 bic x3, x3, #7 // Ensure correct alignment
54 sub x0, x0, x3, lsr #1 // Subtract 4 bytes for each arg to unstack
55 br x0 // Skip remaining argument registers
56
57 ldr x7, [x9, #56] // Call with 8 arguments
58 ldr x6, [x9, #48] // |
59 ldr x5, [x9, #40] // |
60 ldr x4, [x9, #32] // |
61 ldr x3, [x9, #24] // |
62 ldr x2, [x9, #16] // |
63 ldr x1, [x9, #8] // V
64 ldr x0, [x9] // Call with 1 argument
65
66 0: br x8 // Call with no arguments
67
68 //
69 // More than 64 bytes: we need to build the full native stack frame and copy
70 // the part of the VM stack exceeding 64 bytes (which may contain stacked
71 // arguments) to the native stack
72 //
73 1: stp x29, x30, [sp, #-16]!
74 mov x29, sp
75
76 //
77 // Ensure that the stack pointer remains 16 byte aligned,
78 // even if the size of the VM stack frame is not a multiple of 16
79 //
80 add x1, x1, #64 // Skip over [potential] reg params
81 tbz x3, #3, 2f // Multiple of 16?
82 ldr x4, [x2, #-8]! // No? Then push one word
83 str x4, [sp, #-16]! // ... but use two slots
84 b 3f
85
86 2: ldp x4, x5, [x2, #-16]!
87 stp x4, x5, [sp, #-16]!
88 3: cmp x2, x1
89 b.gt 2b
90
91 ldp x0, x1, [x9]
92 ldp x2, x3, [x9, #16]
93 ldp x4, x5, [x9, #32]
94 ldp x6, x7, [x9, #48]
95
96 blr x8
97
98 mov sp, x29
99 ldp x29, x30, [sp], #16
100 ret
101
102 //****************************************************************************
103 // EbcLLEbcInterpret
104 //
105 // This function is called by the thunk code to handle an Native to EBC call
106 // This can handle up to 16 arguments (1-8 on in x0-x7, 9-16 are on the stack)
107 // x16 contains the Entry point that will be the first stacked argument when
108 // EBCInterpret is called.
109 //
110 //****************************************************************************
111 ASM_PFX(EbcLLEbcInterpret):
112 stp x29, x30, [sp, #-16]!
113 mov x29, sp
114
115 // push the entry point and the address of args #9 - #16 onto the stack
116 add x17, sp, #16
117 stp x16, x17, [sp, #-16]!
118
119 // call C-code
120 bl ASM_PFX(EbcInterpret)
121
122 add sp, sp, #16
123 ldp x29, x30, [sp], #16
124 ret
125
126 //****************************************************************************
127 // EbcLLExecuteEbcImageEntryPoint
128 //
129 // This function is called by the thunk code to handle the image entry point
130 // x16 contains the Entry point that will be the third argument when
131 // ExecuteEbcImageEntryPoint is called.
132 //
133 //****************************************************************************
134 ASM_PFX(EbcLLExecuteEbcImageEntryPoint):
135 mov x2, x16
136
137 // tail call to C code
138 b ASM_PFX(ExecuteEbcImageEntryPoint)
139
140 //****************************************************************************
141 // mEbcInstructionBufferTemplate
142 //****************************************************************************
143 .section ".rodata", "a"
144 .align 3
145 ASM_PFX(mEbcInstructionBufferTemplate):
146 adr x17, 0f
147 ldp x16, x17, [x17]
148 br x17
149
150 //
151 // Add a magic code here to help the VM recognize the thunk.
152 //
153 hlt #0xEBC
154
155 0: .quad 0 // EBC_ENTRYPOINT_SIGNATURE
156 .quad 0 // EBC_LL_EBC_ENTRYPOINT_SIGNATURE