xref: /xnu-8792.61.2/tests/ldt_code32.s (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1*42e22086SApple OSS Distributions/*
2*42e22086SApple OSS Distributions * Copyright (c) 2019 Apple Inc. All rights reserved.
3*42e22086SApple OSS Distributions *
4*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*42e22086SApple OSS Distributions *
6*42e22086SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*42e22086SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*42e22086SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*42e22086SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*42e22086SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*42e22086SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*42e22086SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*42e22086SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*42e22086SApple OSS Distributions *
15*42e22086SApple OSS Distributions * Please obtain a copy of the License at
16*42e22086SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*42e22086SApple OSS Distributions *
18*42e22086SApple OSS Distributions * The Original Code and all software distributed under the License are
19*42e22086SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*42e22086SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*42e22086SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*42e22086SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*42e22086SApple OSS Distributions * Please see the License for the specific language governing rights and
24*42e22086SApple OSS Distributions * limitations under the License.
25*42e22086SApple OSS Distributions *
26*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*42e22086SApple OSS Distributions */
28*42e22086SApple OSS Distributions
29*42e22086SApple OSS Distributions.code64
30*42e22086SApple OSS Distributions.globl _compat_mode_trampoline
31*42e22086SApple OSS Distributions_compat_mode_trampoline:
32*42e22086SApple OSS Distributions	/*
33*42e22086SApple OSS Distributions	 * %rdi => address of far_call_t (64-bit offset, then 16-bit selector)
34*42e22086SApple OSS Distributions	 * %rsi => lowmem stack
35*42e22086SApple OSS Distributions	 * %rdx => argument to 32-bit function
36*42e22086SApple OSS Distributions	 * %rcx => address of long mode callback
37*42e22086SApple OSS Distributions	 * %r8  => 64-bit address of _thunk64
38*42e22086SApple OSS Distributions	 */
39*42e22086SApple OSS Distributions	movq	%rsp, %rax
40*42e22086SApple OSS Distributions	movq	%rsi, %rsp
41*42e22086SApple OSS Distributions	pushq	%rax		/* Save 64-bit stack pointer */
42*42e22086SApple OSS Distributions	leaq	1f(%rip), %rax
43*42e22086SApple OSS Distributions	movq	%rdx, %r9
44*42e22086SApple OSS Distributions	xorq	%rdx, %rdx
45*42e22086SApple OSS Distributions	movw	%cs, %dx
46*42e22086SApple OSS Distributions	shlq	$32, %rdx
47*42e22086SApple OSS Distributions	orq	%rdx, %rax
48*42e22086SApple OSS Distributions	movq	%r9, %rdx
49*42e22086SApple OSS Distributions	/*
50*42e22086SApple OSS Distributions	 * Save all callee-saved regs before calling down to compat mode,
51*42e22086SApple OSS Distributions	 * as there's no guarantee that the top 32 bits are preserved
52*42e22086SApple OSS Distributions	 * across compat mode/long mode switches.
53*42e22086SApple OSS Distributions	 */
54*42e22086SApple OSS Distributions	pushq	%rbp
55*42e22086SApple OSS Distributions	pushq	%rbx
56*42e22086SApple OSS Distributions	pushq	%r12
57*42e22086SApple OSS Distributions	pushq	%r13
58*42e22086SApple OSS Distributions	pushq	%r14
59*42e22086SApple OSS Distributions	pushq	%r15
60*42e22086SApple OSS Distributions
61*42e22086SApple OSS Distributions	pushq	%r8		/* Push the absolute address of _thunk64 below */
62*42e22086SApple OSS Distributions	pushq	%rcx		/* Push the 64-bit fn ptr that compat mode will call */
63*42e22086SApple OSS Distributions	pushq	%rdx		/* Push arg to 32-bit code */
64*42e22086SApple OSS Distributions	pushq	%rax		/* Push the return offset + segment onto the stack */
65*42e22086SApple OSS Distributions
66*42e22086SApple OSS Distributions	ljmpq	*(%rdi)
67*42e22086SApple OSS Distributions1:
68*42e22086SApple OSS Distributions	/*
69*42e22086SApple OSS Distributions	 * lretl from compat mode pops off the first 8 bytes,
70*42e22086SApple OSS Distributions	 * so manually reclaim the remaining 24 bytes
71*42e22086SApple OSS Distributions	 */
72*42e22086SApple OSS Distributions	addq	$0x18, %rsp
73*42e22086SApple OSS Distributions
74*42e22086SApple OSS Distributions	/* Restore callee-saved registers */
75*42e22086SApple OSS Distributions	popq	%r15
76*42e22086SApple OSS Distributions	popq	%r14
77*42e22086SApple OSS Distributions	popq	%r13
78*42e22086SApple OSS Distributions	popq	%r12
79*42e22086SApple OSS Distributions	popq	%rbx
80*42e22086SApple OSS Distributions	popq	%rbp
81*42e22086SApple OSS Distributions
82*42e22086SApple OSS Distributions	popq	%rsp
83*42e22086SApple OSS Distributions	retq
84*42e22086SApple OSS Distributions
85*42e22086SApple OSS Distributions
86*42e22086SApple OSS Distributions.code32
87*42e22086SApple OSS Distributions.globl _code_32
88*42e22086SApple OSS Distributions.align 12
89*42e22086SApple OSS Distributions_code_32:
90*42e22086SApple OSS Distributions	/*
91*42e22086SApple OSS Distributions	 * After the standard stack frame is established, the stack layout is as follows:
92*42e22086SApple OSS Distributions	 *
93*42e22086SApple OSS Distributions	 *     (%esp) -> old %ebp
94*42e22086SApple OSS Distributions	 *    4(%ebp) -> return %eip
95*42e22086SApple OSS Distributions	 *    8(%ebp) -> return %cs
96*42e22086SApple OSS Distributions	 *  0xc(%ebp) -> function arg (value to increment and return)
97*42e22086SApple OSS Distributions	 * 0x14(%ebp) -> 8-byte long mode function pointer to call via trampoline (with 0 args)
98*42e22086SApple OSS Distributions	 * 0x1c(%ebp) -> absolute (32-bit) base address of the 64-bit thunk
99*42e22086SApple OSS Distributions	 *               (Note that the caller pushed a 64-bit value here, so the 4 bytes
100*42e22086SApple OSS Distributions	 *               at 0x20(%ebp) are zeroes.)
101*42e22086SApple OSS Distributions	 */
102*42e22086SApple OSS Distributions	pushl	%ebp
103*42e22086SApple OSS Distributions	movl	%esp, %ebp
104*42e22086SApple OSS Distributions	pushl	%ebx
105*42e22086SApple OSS Distributions	call	1f
106*42e22086SApple OSS Distributions1:
107*42e22086SApple OSS Distributions	popl	%ebx		/* save EIP for use in PIC calculation below */
108*42e22086SApple OSS Distributions	subl	$8, %esp
109*42e22086SApple OSS Distributions
110*42e22086SApple OSS Distributions	movl	0x1c(%ebp), %eax
111*42e22086SApple OSS Distributions
112*42e22086SApple OSS Distributions	/* Populate the far call descriptor: */
113*42e22086SApple OSS Distributions	movl	%eax, -8(%ebp)
114*42e22086SApple OSS Distributions	movl	8(%ebp), %eax	/* The long-mode %cs from whence we came */
115*42e22086SApple OSS Distributions	movl	%eax, -4(%ebp)
116*42e22086SApple OSS Distributions
117*42e22086SApple OSS Distributions	pushl	$0	/* number of arguments */
118*42e22086SApple OSS Distributions	pushl	0x18(%ebp)	/* high 32-bits of long mode funcptr */
119*42e22086SApple OSS Distributions	pushl	0x14(%ebp)	/* low 32-bits of long mode funcptr */
120*42e22086SApple OSS Distributions
121*42e22086SApple OSS Distributions	/*
122*42e22086SApple OSS Distributions	 * The next 2 instructions are necessary because clang cannot deal with
123*42e22086SApple OSS Distributions	 * a "leal offset(index_reg), dest_reg" construct despite the fact that
124*42e22086SApple OSS Distributions	 * this code is marked .code32 (because the target is 64-bit and cannot
125*42e22086SApple OSS Distributions	 * process this uniquely-32-bit construct.)
126*42e22086SApple OSS Distributions	 */
127*42e22086SApple OSS Distributions	leal	2f - 1b, %eax
128*42e22086SApple OSS Distributions	addl	%ebx, %eax
129*42e22086SApple OSS Distributions
130*42e22086SApple OSS Distributions	pushl	$0
131*42e22086SApple OSS Distributions	pushl	%cs
132*42e22086SApple OSS Distributions	pushl	$0
133*42e22086SApple OSS Distributions	pushl	%eax
134*42e22086SApple OSS Distributions
135*42e22086SApple OSS Distributions	/*
136*42e22086SApple OSS Distributions	 * Note that the long-mode-based function that is called will need
137*42e22086SApple OSS Distributions	 * to restore GSbase before calling into any frameworks that might
138*42e22086SApple OSS Distributions	 * access %gs-relative data.
139*42e22086SApple OSS Distributions	 */
140*42e22086SApple OSS Distributions	ljmpl	*-8(%ebp)	/* far call to the long mode trampoline */
141*42e22086SApple OSS Distributions2:
142*42e22086SApple OSS Distributions	/*
143*42e22086SApple OSS Distributions	 * lretq from long mode pops 16 bytes, so reclaim the remaining 12
144*42e22086SApple OSS Distributions	 */
145*42e22086SApple OSS Distributions	addl	$12, %esp
146*42e22086SApple OSS Distributions
147*42e22086SApple OSS Distributions	/*
148*42e22086SApple OSS Distributions	 * Do a division-by-zero so the exception handler can catch it and
149*42e22086SApple OSS Distributions	 * restore execution right after.  If a signal handler is used,
150*42e22086SApple OSS Distributions	 * it must restore GSbase first if it intends to call into any
151*42e22086SApple OSS Distributions	 * frameworks / APIs that access %gs-relative data.
152*42e22086SApple OSS Distributions	 */
153*42e22086SApple OSS Distributions	xorl	%eax, %eax
154*42e22086SApple OSS Distributions	div	%eax
155*42e22086SApple OSS Distributions
156*42e22086SApple OSS Distributions.globl _first_invalid_opcode
157*42e22086SApple OSS Distributions_first_invalid_opcode:
158*42e22086SApple OSS Distributions	/*
159*42e22086SApple OSS Distributions	 * Next, try to perform a sysenter syscall -- which should result in
160*42e22086SApple OSS Distributions	 * a #UD.
161*42e22086SApple OSS Distributions	 */
162*42e22086SApple OSS Distributions	leal	3f - 1b, %edx
163*42e22086SApple OSS Distributions	addl	%ebx, %edx		/* return address is expected in %edx */
164*42e22086SApple OSS Distributions	pushl	%ecx
165*42e22086SApple OSS Distributions	movl	%esp, %ecx		/* stack ptr is expected in %ecx */
166*42e22086SApple OSS Distributions	sysenter
167*42e22086SApple OSS Distributions3:
168*42e22086SApple OSS Distributions	popl	%ecx
169*42e22086SApple OSS Distributions
170*42e22086SApple OSS Distributions	/*
171*42e22086SApple OSS Distributions	 * Do the same with each of the old-style INT syscalls.
172*42e22086SApple OSS Distributions	 */
173*42e22086SApple OSS Distributions	int $0x80
174*42e22086SApple OSS Distributions	int $0x81
175*42e22086SApple OSS Distributions.globl _last_invalid_opcode
176*42e22086SApple OSS Distributions_last_invalid_opcode:
177*42e22086SApple OSS Distributions	int $0x82
178*42e22086SApple OSS Distributions
179*42e22086SApple OSS Distributions	/*
180*42e22086SApple OSS Distributions	 * discard the return value from the trampolined function and
181*42e22086SApple OSS Distributions	 * increment the value passed in as this function's first argument
182*42e22086SApple OSS Distributions	 * then return that value + 1 so caller can verify a successful
183*42e22086SApple OSS Distributions	 * thunk.
184*42e22086SApple OSS Distributions	 */
185*42e22086SApple OSS Distributions	movl	0xc(%ebp), %eax
186*42e22086SApple OSS Distributions	incl	%eax
187*42e22086SApple OSS Distributions	addl	$8, %esp
188*42e22086SApple OSS Distributions	popl	%ebx
189*42e22086SApple OSS Distributions	popl	%ebp
190*42e22086SApple OSS Distributions	lret
191*42e22086SApple OSS Distributions
192*42e22086SApple OSS Distributions.code64
193*42e22086SApple OSS Distributions
194*42e22086SApple OSS Distributions.globl _thunk64
195*42e22086SApple OSS Distributions_thunk64:
196*42e22086SApple OSS Distributions	/*
197*42e22086SApple OSS Distributions	 * The thunk is a very simple code fragment that uses an
198*42e22086SApple OSS Distributions	 * absolute address modified at setup time to call into
199*42e22086SApple OSS Distributions	 * the long mode trampoline.far call data passed on the stack to jump to long mode
200*42e22086SApple OSS Distributions	 * code (where %rip-relative addressing will work properly.)
201*42e22086SApple OSS Distributions	 *
202*42e22086SApple OSS Distributions	 */
203*42e22086SApple OSS Distributions.globl _thunk64_movabs
204*42e22086SApple OSS Distributions_thunk64_movabs:
205*42e22086SApple OSS Distributions	movabs	$0xdeadbeeffeedface, %rax
206*42e22086SApple OSS Distributions	jmpq	*%rax
207*42e22086SApple OSS Distributions
208*42e22086SApple OSS Distributions
209*42e22086SApple OSS Distributions.globl _compat_mode_trampoline_len
210*42e22086SApple OSS Distributions_compat_mode_trampoline_len:
211*42e22086SApple OSS Distributions	.long   (. - _compat_mode_trampoline)
212*42e22086SApple OSS Distributions
213*42e22086SApple OSS Distributions
214*42e22086SApple OSS Distributions.globl _long_mode_trampoline
215*42e22086SApple OSS Distributions_long_mode_trampoline:
216*42e22086SApple OSS Distributions	/*
217*42e22086SApple OSS Distributions	 * After creating a standard stack frame, the stack layout is:
218*42e22086SApple OSS Distributions	 *
219*42e22086SApple OSS Distributions	 *    8(%rbp) => %eip of far return to compat mode
220*42e22086SApple OSS Distributions	 * 0x10(%rbp) => %cs of far return to compat mode
221*42e22086SApple OSS Distributions	 * 0x18(%rbp) => low 32-bits of function pointer
222*42e22086SApple OSS Distributions	 * 0x1C(%rbp) => high 32-bits of function pointer
223*42e22086SApple OSS Distributions	 * 0x20(%rbp) => number of parameters (0..4)
224*42e22086SApple OSS Distributions	 * 0x24(%rbp) => first argument [low 32-bits] (if needed)
225*42e22086SApple OSS Distributions	 * 0x28(%rbp) => first argument [high 32-bits] (if needed)
226*42e22086SApple OSS Distributions	 * 0x2c(%rbp) => second argument [low 32-bits] (if needed)
227*42e22086SApple OSS Distributions	 * 0x30(%rbp) => second argument [high 32-bits] (if needed)
228*42e22086SApple OSS Distributions	 * 0x34(%rbp) => third argument [low 32-bits] (if needed)
229*42e22086SApple OSS Distributions	 * 0x38(%rbp) => third argument [high 32-bits] (if needed)
230*42e22086SApple OSS Distributions	 * 0x3c(%rbp) => fourth argument [low 32-bits] (if needed)
231*42e22086SApple OSS Distributions	 * 0x40(%rbp) => fourth argument [high 32-bits] (if needed)
232*42e22086SApple OSS Distributions	 *
233*42e22086SApple OSS Distributions	 * Note that we continue to use the existing (<4G) stack
234*42e22086SApple OSS Distributions	 * after the call into long mode.
235*42e22086SApple OSS Distributions	 */
236*42e22086SApple OSS Distributions	pushq	%rbp
237*42e22086SApple OSS Distributions	movq	%rsp, %rbp
238*42e22086SApple OSS Distributions	pushq	%rdi
239*42e22086SApple OSS Distributions	pushq	%rsi
240*42e22086SApple OSS Distributions	pushq	%rcx
241*42e22086SApple OSS Distributions	movl	0x20(%rbp), %eax
242*42e22086SApple OSS Distributions
243*42e22086SApple OSS Distributions	testl	%eax, %eax
244*42e22086SApple OSS Distributions	jz	5f
245*42e22086SApple OSS Distributions
246*42e22086SApple OSS Distributions	movq	0x24(%rbp), %rdi
247*42e22086SApple OSS Distributions	decl	%eax
248*42e22086SApple OSS Distributions
249*42e22086SApple OSS Distributions2:
250*42e22086SApple OSS Distributions	testl	%eax, %eax
251*42e22086SApple OSS Distributions	jz	5f
252*42e22086SApple OSS Distributions
253*42e22086SApple OSS Distributions	movq	0x2c(%rbp), %rsi
254*42e22086SApple OSS Distributions	decl	%eax
255*42e22086SApple OSS Distributions
256*42e22086SApple OSS Distributions3:
257*42e22086SApple OSS Distributions	testl	%eax, %eax
258*42e22086SApple OSS Distributions	jz	5f
259*42e22086SApple OSS Distributions
260*42e22086SApple OSS Distributions	movq	0x34(%rbp), %rdx
261*42e22086SApple OSS Distributions	decl	%eax
262*42e22086SApple OSS Distributions
263*42e22086SApple OSS Distributions4:
264*42e22086SApple OSS Distributions	testl	%eax, %eax
265*42e22086SApple OSS Distributions	jnz	1f			/* too many arguments specified -- bail out and return */
266*42e22086SApple OSS Distributions
267*42e22086SApple OSS Distributions	movq	0x3c(%rbp), %rcx
268*42e22086SApple OSS Distributions
269*42e22086SApple OSS Distributions5:	/* Call passed-in function */
270*42e22086SApple OSS Distributions	/* Note that the stack MUST be 16-byte aligned before we call into frameworks in long mode */
271*42e22086SApple OSS Distributions
272*42e22086SApple OSS Distributions	pushq	%rbx
273*42e22086SApple OSS Distributions	movq	%rsp, %rbx
274*42e22086SApple OSS Distributions	subq	$0x10, %rsp
275*42e22086SApple OSS Distributions	andq	$0xffffffffffffffe0, %rsp
276*42e22086SApple OSS Distributions
277*42e22086SApple OSS Distributions	callq	*0x18(%rbp)
278*42e22086SApple OSS Distributions	movq	%rbx, %rsp
279*42e22086SApple OSS Distributions	popq	%rbx
280*42e22086SApple OSS Distributions1:
281*42e22086SApple OSS Distributions	popq	%rcx
282*42e22086SApple OSS Distributions	popq	%rsi
283*42e22086SApple OSS Distributions	popq	%rdi
284*42e22086SApple OSS Distributions	popq	%rbp
285*42e22086SApple OSS Distributions	lretq
286