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