2 * Copyright (c) 2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 .globl _compat_mode_trampoline
31 _compat_mode_trampoline:
33 * %rdi => address of far_call_t (64-bit offset, then 16-bit selector)
34 * %rsi => lowmem stack
35 * %rdx => argument to 32-bit function
36 * %rcx => address of long mode callback
37 * %r8 => 64-bit address of _thunk64
41 pushq %rax /* Save 64-bit stack pointer */
50 * Save all callee-saved regs before calling down to compat mode,
51 * as there's no guarantee that the top 32 bits are preserved
52 * across compat mode/long mode switches.
61 pushq %r8 /* Push the absolute address of _thunk64 below */
62 pushq %rcx /* Push the 64-bit fn ptr that compat mode will call */
63 pushq %rdx /* Push arg to 32-bit code */
64 pushq %rax /* Push the return offset + segment onto the stack */
69 * lretl from compat mode pops off the first 8 bytes,
70 * so manually reclaim the remaining 24 bytes
74 /* Restore callee-saved registers */
91 * After the standard stack frame is established, the stack layout is as follows:
94 * 4(%ebp) -> return %eip
95 * 8(%ebp) -> return %cs
96 * 0xc(%ebp) -> function arg (value to increment and return)
97 * 0x14(%ebp) -> 8-byte long mode function pointer to call via trampoline (with 0 args)
98 * 0x1c(%ebp) -> absolute (32-bit) base address of the 64-bit thunk
99 * (Note that the caller pushed a 64-bit value here, so the 4 bytes
100 * at 0x20(%ebp) are zeroes.)
107 popl %ebx /* save EIP for use in PIC calculation below */
110 movl 0x1c(%ebp), %eax
112 /* Populate the far call descriptor: */
114 movl 8(%ebp), %eax /* The long-mode %cs from whence we came */
117 pushl $0 /* number of arguments */
118 pushl 0x18(%ebp) /* high 32-bits of long mode funcptr */
119 pushl 0x14(%ebp) /* low 32-bits of long mode funcptr */
122 * The next 2 instructions are necessary because clang cannot deal with
123 * a "leal offset(index_reg), dest_reg" construct despite the fact that
124 * this code is marked .code32 (because the target is 64-bit and cannot
125 * process this uniquely-32-bit construct.)
136 * Note that the long-mode-based function that is called will need
137 * to restore GSbase before calling into any frameworks that might
138 * access %gs-relative data.
140 ljmpl *-8(%ebp) /* far call to the long mode trampoline */
143 * lretq from long mode pops 16 bytes, so reclaim the remaining 12
148 * Do a division-by-zero so the exception handler can catch it and
149 * restore execution right after. If a signal handler is used,
150 * it must restore GSbase first if it intends to call into any
151 * frameworks / APIs that access %gs-relative data.
156 .globl _first_invalid_opcode
157 _first_invalid_opcode:
159 * Next, try to perform a sysenter syscall -- which should result in
163 addl %ebx, %edx /* return address is expected in %edx */
165 movl %esp, %ecx /* stack ptr is expected in %ecx */
171 * Do the same with each of the old-style INT syscalls.
175 .globl _last_invalid_opcode
176 _last_invalid_opcode:
180 * discard the return value from the trampolined function and
181 * increment the value passed in as this function's first argument
182 * then return that value + 1 so caller can verify a successful
197 * The thunk is a very simple code fragment that uses an
198 * absolute address modified at setup time to call into
199 * the long mode trampoline.far call data passed on the stack to jump to long mode
200 * code (where %rip-relative addressing will work properly.)
203 .globl _thunk64_movabs
205 movabs $0xdeadbeeffeedface, %rax
209 .globl _compat_mode_trampoline_len
210 _compat_mode_trampoline_len:
211 .long (. - _compat_mode_trampoline)
214 .globl _long_mode_trampoline
215 _long_mode_trampoline:
217 * After creating a standard stack frame, the stack layout is:
219 * 8(%rbp) => %eip of far return to compat mode
220 * 0x10(%rbp) => %cs of far return to compat mode
221 * 0x18(%rbp) => low 32-bits of function pointer
222 * 0x1C(%rbp) => high 32-bits of function pointer
223 * 0x20(%rbp) => number of parameters (0..4)
224 * 0x24(%rbp) => first argument [low 32-bits] (if needed)
225 * 0x28(%rbp) => first argument [high 32-bits] (if needed)
226 * 0x2c(%rbp) => second argument [low 32-bits] (if needed)
227 * 0x30(%rbp) => second argument [high 32-bits] (if needed)
228 * 0x34(%rbp) => third argument [low 32-bits] (if needed)
229 * 0x38(%rbp) => third argument [high 32-bits] (if needed)
230 * 0x3c(%rbp) => fourth argument [low 32-bits] (if needed)
231 * 0x40(%rbp) => fourth argument [high 32-bits] (if needed)
233 * Note that we continue to use the existing (<4G) stack
234 * after the call into long mode.
241 movl 0x20(%rbp), %eax
246 movq 0x24(%rbp), %rdi
253 movq 0x2c(%rbp), %rsi
260 movq 0x34(%rbp), %rdx
265 jnz 1f /* too many arguments specified -- bail out and return */
267 movq 0x3c(%rbp), %rcx
269 5: /* Call passed-in function */
270 /* Note that the stack MUST be 16-byte aligned before we call into frameworks in long mode */
275 andq $0xffffffffffffffe0, %rsp