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