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