1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions * Copyright (c) 2019 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions *
4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions *
6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions *
15*043036a2SApple OSS Distributions * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions *
18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions * limitations under the License.
25*043036a2SApple OSS Distributions *
26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions */
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions // #define STANDALONE
30*043036a2SApple OSS Distributions
31*043036a2SApple OSS Distributions #ifndef STANDALONE
32*043036a2SApple OSS Distributions #include <darwintest.h>
33*043036a2SApple OSS Distributions #endif
34*043036a2SApple OSS Distributions #include <architecture/i386/table.h>
35*043036a2SApple OSS Distributions #include <i386/user_ldt.h>
36*043036a2SApple OSS Distributions #include <mach/i386/vm_param.h>
37*043036a2SApple OSS Distributions #include <mach/i386/thread_status.h>
38*043036a2SApple OSS Distributions #include <mach/mach.h>
39*043036a2SApple OSS Distributions #include <signal.h>
40*043036a2SApple OSS Distributions #include <stdio.h>
41*043036a2SApple OSS Distributions #include <stdlib.h>
42*043036a2SApple OSS Distributions #include <strings.h>
43*043036a2SApple OSS Distributions #include <sys/mman.h>
44*043036a2SApple OSS Distributions #include <sys/types.h>
45*043036a2SApple OSS Distributions #include <sys/signal.h>
46*043036a2SApple OSS Distributions #include <sys/sysctl.h>
47*043036a2SApple OSS Distributions #include <assert.h>
48*043036a2SApple OSS Distributions #include <errno.h>
49*043036a2SApple OSS Distributions #include <fcntl.h>
50*043036a2SApple OSS Distributions #include <pthread.h>
51*043036a2SApple OSS Distributions #include <unistd.h>
52*043036a2SApple OSS Distributions #include <ldt_mach_exc.h>
53*043036a2SApple OSS Distributions
54*043036a2SApple OSS Distributions #ifndef STANDALONE
55*043036a2SApple OSS Distributions T_GLOBAL_META(
56*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.intel"),
57*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
58*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("intel"),
59*043036a2SApple OSS Distributions T_META_OWNER("seth_goldberg"),
60*043036a2SApple OSS Distributions T_META_CHECK_LEAKS(false)
61*043036a2SApple OSS Distributions );
62*043036a2SApple OSS Distributions #endif
63*043036a2SApple OSS Distributions
64*043036a2SApple OSS Distributions #define COMPAT_MODE_CS_SELECTOR 0x1f
65*043036a2SApple OSS Distributions #define SYSENTER_SELECTOR 0xb
66*043036a2SApple OSS Distributions /* #define DEBUG 1 */
67*043036a2SApple OSS Distributions #define P2ROUNDUP(x, align) (-(-((long)x) & -((long)align)))
68*043036a2SApple OSS Distributions #define MSG 2048
69*043036a2SApple OSS Distributions
70*043036a2SApple OSS Distributions #define NORMAL_RUN_TIME (10)
71*043036a2SApple OSS Distributions #define TIMEOUT_OVERHEAD (10)
72*043036a2SApple OSS Distributions
73*043036a2SApple OSS Distributions /*
74*043036a2SApple OSS Distributions * General theory of operation:
75*043036a2SApple OSS Distributions * ----------------------------
76*043036a2SApple OSS Distributions * (1) Ensure that all code and data to be accessed from compatibility mode is
77*043036a2SApple OSS Distributions * located in the low 4GiB of virtual address space.
78*043036a2SApple OSS Distributions * (2) Allocate required segments via the i386_set_ldt() system call, making
79*043036a2SApple OSS Distributions * sure to set the descriptor type correctly (code vs. data). Creating
80*043036a2SApple OSS Distributions * 64-bit code segments is not allowed (just use the existing 0x2b selector.)
81*043036a2SApple OSS Distributions * (3) Once you know which selector is associated with the desired code, use a
82*043036a2SApple OSS Distributions * trampoline (or thunk) to (a) switch to a stack that's located below 4GiB
83*043036a2SApple OSS Distributions * and (b) save ABI-mandated caller-saved state so that if it's trashed by
84*043036a2SApple OSS Distributions * compatibility-mode code, it can be restored before returning to 64-bit
85*043036a2SApple OSS Distributions * mode (if desired), and finally (c) long-jump or long-call (aka far call)
86*043036a2SApple OSS Distributions * to the segment and desired offset (this example uses an offset of 0 for
87*043036a2SApple OSS Distributions * simplicity.)
88*043036a2SApple OSS Distributions * (4) Once in compatibility mode, if a framework call or system call is required,
89*043036a2SApple OSS Distributions * the code must trampoline back to 64-bit mode to do so. System calls from
90*043036a2SApple OSS Distributions * compatibility mode code are not supported and will result in invalid opcode
91*043036a2SApple OSS Distributions * exceptions. This example includes a simple 64-bit trampoline (which must
92*043036a2SApple OSS Distributions * be located in the low 4GiB of virtual address space, since it's executed
93*043036a2SApple OSS Distributions * by compatibility-mode code.) Note that since the 64-bit ABI mandates that
94*043036a2SApple OSS Distributions * the stack must be aligned to a 16-byte boundary, the sample trampoline
95*043036a2SApple OSS Distributions * performs that rounding, to simplify compatibility-mode code. Additionally,
96*043036a2SApple OSS Distributions * since 64-bit native code makes use of thread-local storage, the user-mode
97*043036a2SApple OSS Distributions * GSbase must be restored. This sample includes two ways to do that-- (a) by
98*043036a2SApple OSS Distributions * calling into a C implementation that associates the thread-local storage
99*043036a2SApple OSS Distributions * pointer with a stack range (which will be unique for each thread.), and
100*043036a2SApple OSS Distributions * (b) by storing the original GSbase in a block of memory installed into
101*043036a2SApple OSS Distributions * GSbase before calling into compatibility-mode code. A special machdep
102*043036a2SApple OSS Distributions * system call restores GSbase as needed. Note that the sample trampoline
103*043036a2SApple OSS Distributions * does not save and restore %gs (or most other register state, so that is an
104*043036a2SApple OSS Distributions * area that may be tailored to the application's requirements.)
105*043036a2SApple OSS Distributions * (5) Once running in compatibility mode, should synchronous or asynchronous
106*043036a2SApple OSS Distributions * exceptions occur, this sample shows how a mach exception handler (running
107*043036a2SApple OSS Distributions * in a detached thread, handling exceptions for the entire task) can catch
108*043036a2SApple OSS Distributions * such exceptions and manipulate thread state to perform recovery (or not.)
109*043036a2SApple OSS Distributions * Other ways to handle exceptions include installing per-thread exception
110*043036a2SApple OSS Distributions * servers. Alternatively, BSD signal handlers can be used. Note that once a
111*043036a2SApple OSS Distributions * process installs a custom LDT, *ALL* future signal deliveries will include
112*043036a2SApple OSS Distributions * ucontext pointers to mcontext structures that include enhanced thread
113*043036a2SApple OSS Distributions * state embedded (e.g. the %ds, %es, %ss, and GSBase registers) [This assumes
114*043036a2SApple OSS Distributions * that the SA_SIGINFO is passed to sigaction(2) when registering handlers].
115*043036a2SApple OSS Distributions * The mcontext size (part of the ucontext) can be used to differentiate between
116*043036a2SApple OSS Distributions * different mcontext flavors (e.g. those with/without full thread state plus
117*043036a2SApple OSS Distributions * x87 FP state, AVX state, or AVX2/3 state).
118*043036a2SApple OSS Distributions */
119*043036a2SApple OSS Distributions
120*043036a2SApple OSS Distributions /*
121*043036a2SApple OSS Distributions * This test exercises the custom LDT functionality exposed via the i386_{get,set}_ldt
122*043036a2SApple OSS Distributions * system calls.
123*043036a2SApple OSS Distributions *
124*043036a2SApple OSS Distributions * Tests include:
125*043036a2SApple OSS Distributions * (1a) Exception handling (due to an exception or another thread sending a signal) while
126*043036a2SApple OSS Distributions * running in compatibility mode;
127*043036a2SApple OSS Distributions * (1b) Signal handling while running in compatibility mode;
128*043036a2SApple OSS Distributions * (2) Thunking back to 64-bit mode and executing a framework function (e.g. printf)
129*043036a2SApple OSS Distributions * (3) Ensuring that transitions to compatibility mode and back to 64-bit mode
130*043036a2SApple OSS Distributions * do not negatively impact system calls and framework calls in 64-bit mode
131*043036a2SApple OSS Distributions * (4) Use of thread_get_state / thread_set_state to configure a thread to
132*043036a2SApple OSS Distributions * execute in compatibility mode with the proper LDT code segment (this is
133*043036a2SApple OSS Distributions * effectively what the exception handler does when the passed-in new_state
134*043036a2SApple OSS Distributions * is changed (or what the BSD signal handler return handling does when the
135*043036a2SApple OSS Distributions * mcontext is modified).)
136*043036a2SApple OSS Distributions * (5) Ensure that compatibility mode code cannot make system calls via sysenter or
137*043036a2SApple OSS Distributions * old-style int {0x80..0x82}.
138*043036a2SApple OSS Distributions * (6) Negative testing to ensure errors are returned if the consumer tries
139*043036a2SApple OSS Distributions * to set a disallowed segment type / Long flag. [TBD]
140*043036a2SApple OSS Distributions */
141*043036a2SApple OSS Distributions
142*043036a2SApple OSS Distributions /*
143*043036a2SApple OSS Distributions * Note that these addresses are not necessarily available due to ASLR, so
144*043036a2SApple OSS Distributions * a robust implementation should determine the proper range to use via
145*043036a2SApple OSS Distributions * another means.
146*043036a2SApple OSS Distributions */
147*043036a2SApple OSS Distributions #ifndef STANDALONE
148*043036a2SApple OSS Distributions /* libdarwintest needs LOTs of stack */
149*043036a2SApple OSS Distributions #endif
150*043036a2SApple OSS Distributions #define FIXED_STACK_SIZE (PAGE_SIZE * 16)
151*043036a2SApple OSS Distributions #define FIXED_TRAMP_MAXLEN (PAGE_SIZE * 8)
152*043036a2SApple OSS Distributions
153*043036a2SApple OSS Distributions #pragma pack(1)
154*043036a2SApple OSS Distributions typedef struct {
155*043036a2SApple OSS Distributions uint64_t off;
156*043036a2SApple OSS Distributions uint16_t seg;
157*043036a2SApple OSS Distributions } far_call_t;
158*043036a2SApple OSS Distributions #pragma pack()
159*043036a2SApple OSS Distributions
160*043036a2SApple OSS Distributions typedef struct {
161*043036a2SApple OSS Distributions uint64_t stack_base;
162*043036a2SApple OSS Distributions uint64_t stack_limit;
163*043036a2SApple OSS Distributions uint64_t GSbase;
164*043036a2SApple OSS Distributions } stackaddr_to_gsbase_t;
165*043036a2SApple OSS Distributions
166*043036a2SApple OSS Distributions typedef struct thread_arg {
167*043036a2SApple OSS Distributions pthread_mutex_t mutex;
168*043036a2SApple OSS Distributions pthread_cond_t condvar;
169*043036a2SApple OSS Distributions volatile boolean_t done;
170*043036a2SApple OSS Distributions uint32_t compat_stackaddr; /* Compatibility mode stack address */
171*043036a2SApple OSS Distributions } thread_arg_t;
172*043036a2SApple OSS Distributions
173*043036a2SApple OSS Distributions typedef struct custom_tsd {
174*043036a2SApple OSS Distributions struct custom_tsd * this_tsd_base;
175*043036a2SApple OSS Distributions uint64_t orig_tsd_base;
176*043036a2SApple OSS Distributions } custom_tsd_t;
177*043036a2SApple OSS Distributions
178*043036a2SApple OSS Distributions typedef uint64_t (*compat_tramp_t)(far_call_t *fcp, void *lowmemstk, uint64_t arg_for_32bit,
179*043036a2SApple OSS Distributions uint64_t callback, uint64_t absolute_addr_of_thunk64);
180*043036a2SApple OSS Distributions
181*043036a2SApple OSS Distributions #define GS_RELATIVE volatile __attribute__((address_space(256)))
182*043036a2SApple OSS Distributions static custom_tsd_t GS_RELATIVE *mytsd = (custom_tsd_t GS_RELATIVE *)0;
183*043036a2SApple OSS Distributions
184*043036a2SApple OSS Distributions static far_call_t input_desc = { .seg = COMPAT_MODE_CS_SELECTOR, .off = 0 };
185*043036a2SApple OSS Distributions static uint64_t stackAddr = 0;
186*043036a2SApple OSS Distributions static compat_tramp_t thunkit = NULL;
187*043036a2SApple OSS Distributions static uint64_t thunk64_addr;
188*043036a2SApple OSS Distributions /* stack2gs[0] is initialized in map_lowmem_stack() */
189*043036a2SApple OSS Distributions static stackaddr_to_gsbase_t stack2gs[] = { { 0 } };
190*043036a2SApple OSS Distributions
191*043036a2SApple OSS Distributions extern int compat_mode_trampoline(far_call_t *, void *, uint64_t);
192*043036a2SApple OSS Distributions extern void long_mode_trampoline(void);
193*043036a2SApple OSS Distributions extern boolean_t mach_exc_server(mach_msg_header_t *InHeadP, mach_msg_header_t *OutHeadP);
194*043036a2SApple OSS Distributions
195*043036a2SApple OSS Distributions extern void code_32(void);
196*043036a2SApple OSS Distributions
197*043036a2SApple OSS Distributions kern_return_t catch_mach_exception_raise_state_identity(mach_port_t exception_port,
198*043036a2SApple OSS Distributions mach_port_t thread,
199*043036a2SApple OSS Distributions mach_port_t task,
200*043036a2SApple OSS Distributions exception_type_t exception,
201*043036a2SApple OSS Distributions mach_exception_data_t code,
202*043036a2SApple OSS Distributions mach_msg_type_number_t code_count,
203*043036a2SApple OSS Distributions int * flavor,
204*043036a2SApple OSS Distributions thread_state_t old_state,
205*043036a2SApple OSS Distributions mach_msg_type_number_t old_state_count,
206*043036a2SApple OSS Distributions thread_state_t new_state,
207*043036a2SApple OSS Distributions mach_msg_type_number_t * new_state_count);
208*043036a2SApple OSS Distributions
209*043036a2SApple OSS Distributions kern_return_t
210*043036a2SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
211*043036a2SApple OSS Distributions exception_type_t exception,
212*043036a2SApple OSS Distributions const mach_exception_data_t code,
213*043036a2SApple OSS Distributions mach_msg_type_number_t codeCnt,
214*043036a2SApple OSS Distributions int *flavor,
215*043036a2SApple OSS Distributions const thread_state_t old_state,
216*043036a2SApple OSS Distributions mach_msg_type_number_t old_stateCnt,
217*043036a2SApple OSS Distributions thread_state_t new_state,
218*043036a2SApple OSS Distributions mach_msg_type_number_t *new_stateCnt);
219*043036a2SApple OSS Distributions
220*043036a2SApple OSS Distributions kern_return_t
221*043036a2SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
222*043036a2SApple OSS Distributions mach_port_t thread,
223*043036a2SApple OSS Distributions mach_port_t task,
224*043036a2SApple OSS Distributions exception_type_t exception,
225*043036a2SApple OSS Distributions mach_exception_data_t code,
226*043036a2SApple OSS Distributions mach_msg_type_number_t codeCnt,
227*043036a2SApple OSS Distributions int *flavor,
228*043036a2SApple OSS Distributions thread_state_t old_state,
229*043036a2SApple OSS Distributions mach_msg_type_number_t old_stateCnt,
230*043036a2SApple OSS Distributions thread_state_t new_state,
231*043036a2SApple OSS Distributions mach_msg_type_number_t *new_stateCnt);
232*043036a2SApple OSS Distributions
233*043036a2SApple OSS Distributions extern void _thread_set_tsd_base(uint64_t);
234*043036a2SApple OSS Distributions static uint64_t stack_range_to_GSbase(uint64_t stackptr, uint64_t GSbase);
235*043036a2SApple OSS Distributions void restore_gsbase(uint64_t stackptr);
236*043036a2SApple OSS Distributions
237*043036a2SApple OSS Distributions static uint64_t
get_gsbase(void)238*043036a2SApple OSS Distributions get_gsbase(void)
239*043036a2SApple OSS Distributions {
240*043036a2SApple OSS Distributions struct thread_identifier_info tiinfo;
241*043036a2SApple OSS Distributions unsigned int info_count = THREAD_IDENTIFIER_INFO_COUNT;
242*043036a2SApple OSS Distributions kern_return_t kr;
243*043036a2SApple OSS Distributions
244*043036a2SApple OSS Distributions if ((kr = thread_info(mach_thread_self(), THREAD_IDENTIFIER_INFO,
245*043036a2SApple OSS Distributions (thread_info_t) &tiinfo, &info_count)) != KERN_SUCCESS) {
246*043036a2SApple OSS Distributions fprintf(stderr, "Could not get tsd base address. This will not end well.\n");
247*043036a2SApple OSS Distributions return 0;
248*043036a2SApple OSS Distributions }
249*043036a2SApple OSS Distributions
250*043036a2SApple OSS Distributions return (uint64_t)tiinfo.thread_handle;
251*043036a2SApple OSS Distributions }
252*043036a2SApple OSS Distributions
253*043036a2SApple OSS Distributions void
restore_gsbase(uint64_t stackptr)254*043036a2SApple OSS Distributions restore_gsbase(uint64_t stackptr)
255*043036a2SApple OSS Distributions {
256*043036a2SApple OSS Distributions /* Restore GSbase so tsd is accessible in long mode */
257*043036a2SApple OSS Distributions uint64_t orig_GSbase = stack_range_to_GSbase(stackptr, 0);
258*043036a2SApple OSS Distributions
259*043036a2SApple OSS Distributions assert(orig_GSbase != 0);
260*043036a2SApple OSS Distributions _thread_set_tsd_base(orig_GSbase);
261*043036a2SApple OSS Distributions }
262*043036a2SApple OSS Distributions
263*043036a2SApple OSS Distributions /*
264*043036a2SApple OSS Distributions * Though we've directed all exceptions through the catch_mach_exception_raise_state_identity
265*043036a2SApple OSS Distributions * entry point, we still must provide these two other entry points, otherwise a linker error
266*043036a2SApple OSS Distributions * will occur.
267*043036a2SApple OSS Distributions */
268*043036a2SApple OSS Distributions kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t codeCnt,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_stateCnt,thread_state_t new_state,mach_msg_type_number_t * new_stateCnt)269*043036a2SApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
270*043036a2SApple OSS Distributions mach_port_t thread,
271*043036a2SApple OSS Distributions mach_port_t task,
272*043036a2SApple OSS Distributions exception_type_t exception,
273*043036a2SApple OSS Distributions mach_exception_data_t code,
274*043036a2SApple OSS Distributions mach_msg_type_number_t codeCnt,
275*043036a2SApple OSS Distributions int *flavor,
276*043036a2SApple OSS Distributions thread_state_t old_state,
277*043036a2SApple OSS Distributions mach_msg_type_number_t old_stateCnt,
278*043036a2SApple OSS Distributions thread_state_t new_state,
279*043036a2SApple OSS Distributions mach_msg_type_number_t *new_stateCnt)
280*043036a2SApple OSS Distributions {
281*043036a2SApple OSS Distributions #pragma unused(exception_port, thread, task, exception, code, codeCnt, flavor, old_state, old_stateCnt, new_state, new_stateCnt)
282*043036a2SApple OSS Distributions fprintf(stderr, "Unexpected exception handler called: %s\n", __func__);
283*043036a2SApple OSS Distributions return KERN_FAILURE;
284*043036a2SApple OSS Distributions }
285*043036a2SApple OSS Distributions
286*043036a2SApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t exception,const mach_exception_data_t code,mach_msg_type_number_t codeCnt,int * flavor,const thread_state_t old_state,mach_msg_type_number_t old_stateCnt,thread_state_t new_state,mach_msg_type_number_t * new_stateCnt)287*043036a2SApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
288*043036a2SApple OSS Distributions exception_type_t exception,
289*043036a2SApple OSS Distributions const mach_exception_data_t code,
290*043036a2SApple OSS Distributions mach_msg_type_number_t codeCnt,
291*043036a2SApple OSS Distributions int *flavor,
292*043036a2SApple OSS Distributions const thread_state_t old_state,
293*043036a2SApple OSS Distributions mach_msg_type_number_t old_stateCnt,
294*043036a2SApple OSS Distributions thread_state_t new_state,
295*043036a2SApple OSS Distributions mach_msg_type_number_t *new_stateCnt)
296*043036a2SApple OSS Distributions {
297*043036a2SApple OSS Distributions #pragma unused(exception_port, exception, code, codeCnt, flavor, old_state, old_stateCnt, new_state, new_stateCnt)
298*043036a2SApple OSS Distributions fprintf(stderr, "Unexpected exception handler called: %s\n", __func__);
299*043036a2SApple OSS Distributions return KERN_FAILURE;
300*043036a2SApple OSS Distributions }
301*043036a2SApple OSS Distributions
302*043036a2SApple OSS Distributions static void
handle_arithmetic_exception(_STRUCT_X86_THREAD_FULL_STATE64 * xtfs64,uint64_t * ip_skip_countp)303*043036a2SApple OSS Distributions handle_arithmetic_exception(_STRUCT_X86_THREAD_FULL_STATE64 *xtfs64, uint64_t *ip_skip_countp)
304*043036a2SApple OSS Distributions {
305*043036a2SApple OSS Distributions fprintf(stderr, "Caught divide-error exception\n");
306*043036a2SApple OSS Distributions fprintf(stderr, "cs=0x%x rip=0x%x gs=0x%x ss=0x%x rsp=0x%llx\n",
307*043036a2SApple OSS Distributions (unsigned)xtfs64->__ss64.__cs,
308*043036a2SApple OSS Distributions (unsigned)xtfs64->__ss64.__rip, (unsigned)xtfs64->__ss64.__gs,
309*043036a2SApple OSS Distributions (unsigned)xtfs64->__ss, xtfs64->__ss64.__rsp);
310*043036a2SApple OSS Distributions *ip_skip_countp = 2;
311*043036a2SApple OSS Distributions }
312*043036a2SApple OSS Distributions
313*043036a2SApple OSS Distributions static void
handle_badinsn_exception(_STRUCT_X86_THREAD_FULL_STATE64 * xtfs64,uint64_t __unused * ip_skip_countp)314*043036a2SApple OSS Distributions handle_badinsn_exception(_STRUCT_X86_THREAD_FULL_STATE64 *xtfs64, uint64_t __unused *ip_skip_countp)
315*043036a2SApple OSS Distributions {
316*043036a2SApple OSS Distributions extern void first_invalid_opcode(void);
317*043036a2SApple OSS Distributions extern void last_invalid_opcode(void);
318*043036a2SApple OSS Distributions
319*043036a2SApple OSS Distributions uint64_t start_addr = ((uintptr_t)first_invalid_opcode - (uintptr_t)code_32);
320*043036a2SApple OSS Distributions uint64_t end_addr = ((uintptr_t)last_invalid_opcode - (uintptr_t)code_32);
321*043036a2SApple OSS Distributions
322*043036a2SApple OSS Distributions fprintf(stderr, "Caught invalid opcode exception\n");
323*043036a2SApple OSS Distributions fprintf(stderr, "cs=%x rip=%x gs=%x ss=0x%x rsp=0x%llx | handling between 0x%llx and 0x%llx\n",
324*043036a2SApple OSS Distributions (unsigned)xtfs64->__ss64.__cs,
325*043036a2SApple OSS Distributions (unsigned)xtfs64->__ss64.__rip, (unsigned)xtfs64->__ss64.__gs,
326*043036a2SApple OSS Distributions (unsigned)xtfs64->__ss, xtfs64->__ss64.__rsp,
327*043036a2SApple OSS Distributions start_addr, end_addr);
328*043036a2SApple OSS Distributions
329*043036a2SApple OSS Distributions /*
330*043036a2SApple OSS Distributions * We expect to handle 4 invalid opcode exceptions:
331*043036a2SApple OSS Distributions * (1) sysenter
332*043036a2SApple OSS Distributions * (2) int $0x80
333*043036a2SApple OSS Distributions * (3) int $0x81
334*043036a2SApple OSS Distributions * (4) int $0x82
335*043036a2SApple OSS Distributions * (Note that due to the way the invalid opcode indication was implemented,
336*043036a2SApple OSS Distributions * %rip is already set to the next instruction.)
337*043036a2SApple OSS Distributions */
338*043036a2SApple OSS Distributions if (xtfs64->__ss64.__rip >= start_addr && xtfs64->__ss64.__rip <= end_addr) {
339*043036a2SApple OSS Distributions /*
340*043036a2SApple OSS Distributions * On return from the failed sysenter, %cs is changed to the
341*043036a2SApple OSS Distributions * sysenter code selector and %ss is set to 0x23, so switch them
342*043036a2SApple OSS Distributions * back to sane values.
343*043036a2SApple OSS Distributions */
344*043036a2SApple OSS Distributions if ((unsigned)xtfs64->__ss64.__cs == SYSENTER_SELECTOR) {
345*043036a2SApple OSS Distributions xtfs64->__ss64.__cs = COMPAT_MODE_CS_SELECTOR;
346*043036a2SApple OSS Distributions xtfs64->__ss = 0x23; /* XXX */
347*043036a2SApple OSS Distributions }
348*043036a2SApple OSS Distributions }
349*043036a2SApple OSS Distributions }
350*043036a2SApple OSS Distributions
351*043036a2SApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t codeCnt,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_stateCnt,thread_state_t new_state,mach_msg_type_number_t * new_stateCnt)352*043036a2SApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
353*043036a2SApple OSS Distributions mach_port_t thread,
354*043036a2SApple OSS Distributions mach_port_t task,
355*043036a2SApple OSS Distributions exception_type_t exception,
356*043036a2SApple OSS Distributions mach_exception_data_t code,
357*043036a2SApple OSS Distributions mach_msg_type_number_t codeCnt,
358*043036a2SApple OSS Distributions int * flavor,
359*043036a2SApple OSS Distributions thread_state_t old_state,
360*043036a2SApple OSS Distributions mach_msg_type_number_t old_stateCnt,
361*043036a2SApple OSS Distributions thread_state_t new_state,
362*043036a2SApple OSS Distributions mach_msg_type_number_t * new_stateCnt)
363*043036a2SApple OSS Distributions {
364*043036a2SApple OSS Distributions #pragma unused(exception_port, thread, task)
365*043036a2SApple OSS Distributions
366*043036a2SApple OSS Distributions _STRUCT_X86_THREAD_FULL_STATE64 *xtfs64 = (_STRUCT_X86_THREAD_FULL_STATE64 *)(void *)old_state;
367*043036a2SApple OSS Distributions _STRUCT_X86_THREAD_FULL_STATE64 *new_xtfs64 = (_STRUCT_X86_THREAD_FULL_STATE64 *)(void *)new_state;
368*043036a2SApple OSS Distributions uint64_t rip_skip_count = 0;
369*043036a2SApple OSS Distributions
370*043036a2SApple OSS Distributions /*
371*043036a2SApple OSS Distributions * Check the exception code and thread state.
372*043036a2SApple OSS Distributions * If we were executing 32-bit code (or 64-bit code on behalf of
373*043036a2SApple OSS Distributions * 32-bit code), we could update the thread state to effectively longjmp
374*043036a2SApple OSS Distributions * back to a safe location where the victim thread can recover.
375*043036a2SApple OSS Distributions * Then again, we could return KERN_NOT_SUPPORTED and allow the process
376*043036a2SApple OSS Distributions * to be nuked.
377*043036a2SApple OSS Distributions */
378*043036a2SApple OSS Distributions
379*043036a2SApple OSS Distributions switch (exception) {
380*043036a2SApple OSS Distributions case EXC_ARITHMETIC:
381*043036a2SApple OSS Distributions if (codeCnt >= 1 && code[0] == EXC_I386_DIV) {
382*043036a2SApple OSS Distributions handle_arithmetic_exception(xtfs64, &rip_skip_count);
383*043036a2SApple OSS Distributions }
384*043036a2SApple OSS Distributions break;
385*043036a2SApple OSS Distributions
386*043036a2SApple OSS Distributions case EXC_BAD_INSTRUCTION:
387*043036a2SApple OSS Distributions {
388*043036a2SApple OSS Distributions if (codeCnt >= 1 && code[0] == EXC_I386_INVOP) {
389*043036a2SApple OSS Distributions handle_badinsn_exception(xtfs64, &rip_skip_count);
390*043036a2SApple OSS Distributions }
391*043036a2SApple OSS Distributions break;
392*043036a2SApple OSS Distributions }
393*043036a2SApple OSS Distributions
394*043036a2SApple OSS Distributions default:
395*043036a2SApple OSS Distributions fprintf(stderr, "Unsupported catch_mach_exception_raise_state_identity: code 0x%llx sub 0x%llx\n",
396*043036a2SApple OSS Distributions code[0], codeCnt > 1 ? code[1] : 0LL);
397*043036a2SApple OSS Distributions fprintf(stderr, "flavor=%d %%cs=0x%x %%rip=0x%llx\n", *flavor, (unsigned)xtfs64->__ss64.__cs,
398*043036a2SApple OSS Distributions xtfs64->__ss64.__rip);
399*043036a2SApple OSS Distributions }
400*043036a2SApple OSS Distributions
401*043036a2SApple OSS Distributions /*
402*043036a2SApple OSS Distributions * If this exception happened in compatibility mode,
403*043036a2SApple OSS Distributions * assume it was the intentional division-by-zero and set the
404*043036a2SApple OSS Distributions * new state's cs register to just after the div instruction
405*043036a2SApple OSS Distributions * to enable the thread to resume.
406*043036a2SApple OSS Distributions */
407*043036a2SApple OSS Distributions if ((unsigned)xtfs64->__ss64.__cs == COMPAT_MODE_CS_SELECTOR) {
408*043036a2SApple OSS Distributions *new_stateCnt = old_stateCnt;
409*043036a2SApple OSS Distributions *new_xtfs64 = *xtfs64;
410*043036a2SApple OSS Distributions new_xtfs64->__ss64.__rip += rip_skip_count;
411*043036a2SApple OSS Distributions fprintf(stderr, "new cs=0x%x rip=0x%llx\n", (unsigned)new_xtfs64->__ss64.__cs,
412*043036a2SApple OSS Distributions new_xtfs64->__ss64.__rip);
413*043036a2SApple OSS Distributions return KERN_SUCCESS;
414*043036a2SApple OSS Distributions } else {
415*043036a2SApple OSS Distributions return KERN_NOT_SUPPORTED;
416*043036a2SApple OSS Distributions }
417*043036a2SApple OSS Distributions }
418*043036a2SApple OSS Distributions
419*043036a2SApple OSS Distributions static void *
handle_exceptions(void * arg)420*043036a2SApple OSS Distributions handle_exceptions(void *arg)
421*043036a2SApple OSS Distributions {
422*043036a2SApple OSS Distributions mach_port_t ePort = (mach_port_t)arg;
423*043036a2SApple OSS Distributions kern_return_t kret;
424*043036a2SApple OSS Distributions
425*043036a2SApple OSS Distributions kret = mach_msg_server(mach_exc_server, MACH_MSG_SIZE_RELIABLE, ePort, 0);
426*043036a2SApple OSS Distributions if (kret != KERN_SUCCESS) {
427*043036a2SApple OSS Distributions fprintf(stderr, "mach_msg_server: %s (%d)", mach_error_string(kret), kret);
428*043036a2SApple OSS Distributions }
429*043036a2SApple OSS Distributions
430*043036a2SApple OSS Distributions return NULL;
431*043036a2SApple OSS Distributions }
432*043036a2SApple OSS Distributions
433*043036a2SApple OSS Distributions static void
init_task_exception_server(void)434*043036a2SApple OSS Distributions init_task_exception_server(void)
435*043036a2SApple OSS Distributions {
436*043036a2SApple OSS Distributions kern_return_t kr;
437*043036a2SApple OSS Distributions task_t me = mach_task_self();
438*043036a2SApple OSS Distributions pthread_t handler_thread;
439*043036a2SApple OSS Distributions pthread_attr_t attr;
440*043036a2SApple OSS Distributions mach_port_t ePort;
441*043036a2SApple OSS Distributions
442*043036a2SApple OSS Distributions kr = mach_port_allocate(me, MACH_PORT_RIGHT_RECEIVE, &ePort);
443*043036a2SApple OSS Distributions if (kr != KERN_SUCCESS) {
444*043036a2SApple OSS Distributions fprintf(stderr, "allocate receive right: %d\n", kr);
445*043036a2SApple OSS Distributions return;
446*043036a2SApple OSS Distributions }
447*043036a2SApple OSS Distributions
448*043036a2SApple OSS Distributions kr = mach_port_insert_right(me, ePort, ePort, MACH_MSG_TYPE_MAKE_SEND);
449*043036a2SApple OSS Distributions if (kr != KERN_SUCCESS) {
450*043036a2SApple OSS Distributions fprintf(stderr, "insert right into port=[%d]: %d\n", ePort, kr);
451*043036a2SApple OSS Distributions return;
452*043036a2SApple OSS Distributions }
453*043036a2SApple OSS Distributions
454*043036a2SApple OSS Distributions kr = task_set_exception_ports(me, EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC, ePort,
455*043036a2SApple OSS Distributions (exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES), x86_THREAD_FULL_STATE64);
456*043036a2SApple OSS Distributions if (kr != KERN_SUCCESS) {
457*043036a2SApple OSS Distributions fprintf(stderr, "abort: error setting task exception ports on task=[%d], handler=[%d]: %d\n", me, ePort, kr);
458*043036a2SApple OSS Distributions exit(1);
459*043036a2SApple OSS Distributions }
460*043036a2SApple OSS Distributions
461*043036a2SApple OSS Distributions pthread_attr_init(&attr);
462*043036a2SApple OSS Distributions pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
463*043036a2SApple OSS Distributions
464*043036a2SApple OSS Distributions if (pthread_create(&handler_thread, &attr, handle_exceptions, (void *)(uintptr_t)ePort) != 0) {
465*043036a2SApple OSS Distributions perror("pthread create error");
466*043036a2SApple OSS Distributions return;
467*043036a2SApple OSS Distributions }
468*043036a2SApple OSS Distributions
469*043036a2SApple OSS Distributions pthread_attr_destroy(&attr);
470*043036a2SApple OSS Distributions }
471*043036a2SApple OSS Distributions
472*043036a2SApple OSS Distributions static union ldt_entry *descs = 0;
473*043036a2SApple OSS Distributions static uint64_t idx;
474*043036a2SApple OSS Distributions static int saw_ud2 = 0;
475*043036a2SApple OSS Distributions static boolean_t ENV_set_ldt_in_sighandler = FALSE;
476*043036a2SApple OSS Distributions
477*043036a2SApple OSS Distributions static void
signal_handler(int signo,siginfo_t * sinfop,void * ucontext)478*043036a2SApple OSS Distributions signal_handler(int signo, siginfo_t *sinfop, void *ucontext)
479*043036a2SApple OSS Distributions {
480*043036a2SApple OSS Distributions uint64_t rip_skip_count = 0;
481*043036a2SApple OSS Distributions ucontext_t *uctxp = (ucontext_t *)ucontext;
482*043036a2SApple OSS Distributions union {
483*043036a2SApple OSS Distributions _STRUCT_MCONTEXT_AVX512_64 *avx512_basep;
484*043036a2SApple OSS Distributions _STRUCT_MCONTEXT_AVX512_64_FULL *avx512_fullp;
485*043036a2SApple OSS Distributions _STRUCT_MCONTEXT_AVX64 *avx64_basep;
486*043036a2SApple OSS Distributions _STRUCT_MCONTEXT_AVX64_FULL *avx64_fullp;
487*043036a2SApple OSS Distributions _STRUCT_MCONTEXT64 *fp_basep;
488*043036a2SApple OSS Distributions _STRUCT_MCONTEXT64_FULL *fp_fullp;
489*043036a2SApple OSS Distributions } mctx;
490*043036a2SApple OSS Distributions
491*043036a2SApple OSS Distributions mctx.fp_fullp = (_STRUCT_MCONTEXT64_FULL *)uctxp->uc_mcontext;
492*043036a2SApple OSS Distributions
493*043036a2SApple OSS Distributions /*
494*043036a2SApple OSS Distributions * Note that GSbase must be restored before calling into any frameworks
495*043036a2SApple OSS Distributions * that might access anything %gs-relative (e.g. TSD) if the signal
496*043036a2SApple OSS Distributions * handler was triggered while the thread was running with a non-default
497*043036a2SApple OSS Distributions * (system-established) GSbase.
498*043036a2SApple OSS Distributions */
499*043036a2SApple OSS Distributions
500*043036a2SApple OSS Distributions if ((signo != SIGFPE && signo != SIGILL) || sinfop->si_signo != signo) {
501*043036a2SApple OSS Distributions #ifndef STANDALONE
502*043036a2SApple OSS Distributions T_ASSERT_FAIL("Unexpected signal %d\n", signo);
503*043036a2SApple OSS Distributions #else
504*043036a2SApple OSS Distributions restore_gsbase(mctx.fp_fullp->__ss.__ss64.__rsp);
505*043036a2SApple OSS Distributions fprintf(stderr, "Not handling signal %d\n", signo);
506*043036a2SApple OSS Distributions abort();
507*043036a2SApple OSS Distributions #endif
508*043036a2SApple OSS Distributions }
509*043036a2SApple OSS Distributions
510*043036a2SApple OSS Distributions if (uctxp->uc_mcsize == sizeof(_STRUCT_MCONTEXT_AVX512_64) ||
511*043036a2SApple OSS Distributions uctxp->uc_mcsize == sizeof(_STRUCT_MCONTEXT_AVX64) ||
512*043036a2SApple OSS Distributions uctxp->uc_mcsize == sizeof(_STRUCT_MCONTEXT64)) {
513*043036a2SApple OSS Distributions _STRUCT_X86_THREAD_STATE64 *ss64 = &mctx.fp_basep->__ss;
514*043036a2SApple OSS Distributions
515*043036a2SApple OSS Distributions /*
516*043036a2SApple OSS Distributions * The following block is an illustration of what NOT to do.
517*043036a2SApple OSS Distributions * Configuring an LDT for the first time in a signal handler
518*043036a2SApple OSS Distributions * will likely cause the process to crash.
519*043036a2SApple OSS Distributions */
520*043036a2SApple OSS Distributions if (ENV_set_ldt_in_sighandler == TRUE && !saw_ud2) {
521*043036a2SApple OSS Distributions /* Set the LDT: */
522*043036a2SApple OSS Distributions int cnt = i386_set_ldt((int)idx, &descs[idx], 1);
523*043036a2SApple OSS Distributions if (cnt != (int)idx) {
524*043036a2SApple OSS Distributions #ifdef DEBUG
525*043036a2SApple OSS Distributions fprintf(stderr, "i386_set_ldt unexpectedly returned %d (errno = %s)\n", cnt, strerror(errno));
526*043036a2SApple OSS Distributions #endif
527*043036a2SApple OSS Distributions #ifndef STANDALONE
528*043036a2SApple OSS Distributions T_LOG("i386_set_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
529*043036a2SApple OSS Distributions T_ASSERT_FAIL("i386_set_ldt failure");
530*043036a2SApple OSS Distributions #else
531*043036a2SApple OSS Distributions exit(1);
532*043036a2SApple OSS Distributions #endif
533*043036a2SApple OSS Distributions }
534*043036a2SApple OSS Distributions #ifdef DEBUG
535*043036a2SApple OSS Distributions printf("i386_set_ldt returned %d\n", cnt);
536*043036a2SApple OSS Distributions #endif
537*043036a2SApple OSS Distributions ss64->__rip += 2; /* ud2 is 2 bytes */
538*043036a2SApple OSS Distributions
539*043036a2SApple OSS Distributions saw_ud2 = 1;
540*043036a2SApple OSS Distributions
541*043036a2SApple OSS Distributions /*
542*043036a2SApple OSS Distributions * When we return here, the sigreturn processing code will try to copy a FULL
543*043036a2SApple OSS Distributions * thread context from the signal stack, which will likely cause the resumed
544*043036a2SApple OSS Distributions * thread to fault and be terminated.
545*043036a2SApple OSS Distributions */
546*043036a2SApple OSS Distributions return;
547*043036a2SApple OSS Distributions }
548*043036a2SApple OSS Distributions
549*043036a2SApple OSS Distributions restore_gsbase(ss64->__rsp);
550*043036a2SApple OSS Distributions
551*043036a2SApple OSS Distributions /*
552*043036a2SApple OSS Distributions * If we're in this block, either we are dispatching a signal received
553*043036a2SApple OSS Distributions * before we installed a custom LDT or we are on a kernel without
554*043036a2SApple OSS Distributions * BSD-signalling-sending-full-thread-state support. It's likely the latter case.
555*043036a2SApple OSS Distributions */
556*043036a2SApple OSS Distributions #ifndef STANDALONE
557*043036a2SApple OSS Distributions T_ASSERT_FAIL("This system doesn't support BSD signals with full thread state.");
558*043036a2SApple OSS Distributions #else
559*043036a2SApple OSS Distributions fprintf(stderr, "This system doesn't support BSD signals with full thread state. Aborting.\n");
560*043036a2SApple OSS Distributions abort();
561*043036a2SApple OSS Distributions #endif
562*043036a2SApple OSS Distributions } else if (uctxp->uc_mcsize == sizeof(_STRUCT_MCONTEXT_AVX512_64_FULL) ||
563*043036a2SApple OSS Distributions uctxp->uc_mcsize == sizeof(_STRUCT_MCONTEXT_AVX64_FULL) ||
564*043036a2SApple OSS Distributions uctxp->uc_mcsize == sizeof(_STRUCT_MCONTEXT64_FULL)) {
565*043036a2SApple OSS Distributions _STRUCT_X86_THREAD_FULL_STATE64 *ss64 = &mctx.fp_fullp->__ss;
566*043036a2SApple OSS Distributions
567*043036a2SApple OSS Distributions /*
568*043036a2SApple OSS Distributions * Since we're handing this signal on the same thread, we may need to
569*043036a2SApple OSS Distributions * restore GSbase.
570*043036a2SApple OSS Distributions */
571*043036a2SApple OSS Distributions uint64_t orig_gsbase = stack_range_to_GSbase(ss64->__ss64.__rsp, 0);
572*043036a2SApple OSS Distributions if (orig_gsbase != 0 && orig_gsbase != ss64->__gsbase) {
573*043036a2SApple OSS Distributions restore_gsbase(ss64->__ss64.__rsp);
574*043036a2SApple OSS Distributions }
575*043036a2SApple OSS Distributions
576*043036a2SApple OSS Distributions if (signo == SIGFPE) {
577*043036a2SApple OSS Distributions handle_arithmetic_exception(ss64, &rip_skip_count);
578*043036a2SApple OSS Distributions } else if (signo == SIGILL) {
579*043036a2SApple OSS Distributions handle_badinsn_exception(ss64, &rip_skip_count);
580*043036a2SApple OSS Distributions }
581*043036a2SApple OSS Distributions
582*043036a2SApple OSS Distributions /*
583*043036a2SApple OSS Distributions * If this exception happened in compatibility mode,
584*043036a2SApple OSS Distributions * assume it was the intentional division-by-zero and set the
585*043036a2SApple OSS Distributions * new state's cs register to just after the div instruction
586*043036a2SApple OSS Distributions * to enable the thread to resume.
587*043036a2SApple OSS Distributions */
588*043036a2SApple OSS Distributions if ((unsigned)ss64->__ss64.__cs == COMPAT_MODE_CS_SELECTOR) {
589*043036a2SApple OSS Distributions ss64->__ss64.__rip += rip_skip_count;
590*043036a2SApple OSS Distributions fprintf(stderr, "new cs=0x%x rip=0x%llx\n", (unsigned)ss64->__ss64.__cs,
591*043036a2SApple OSS Distributions ss64->__ss64.__rip);
592*043036a2SApple OSS Distributions }
593*043036a2SApple OSS Distributions } else {
594*043036a2SApple OSS Distributions _STRUCT_X86_THREAD_STATE64 *ss64 = &mctx.fp_basep->__ss;
595*043036a2SApple OSS Distributions
596*043036a2SApple OSS Distributions restore_gsbase(ss64->__rsp);
597*043036a2SApple OSS Distributions #ifndef STANDALONE
598*043036a2SApple OSS Distributions T_ASSERT_FAIL("Unknown mcontext size %lu: Aborting.", uctxp->uc_mcsize);
599*043036a2SApple OSS Distributions #else
600*043036a2SApple OSS Distributions fprintf(stderr, "Unknown mcontext size %lu: Aborting.\n", uctxp->uc_mcsize);
601*043036a2SApple OSS Distributions abort();
602*043036a2SApple OSS Distributions #endif
603*043036a2SApple OSS Distributions }
604*043036a2SApple OSS Distributions }
605*043036a2SApple OSS Distributions
606*043036a2SApple OSS Distributions static void
setup_signal_handling(void)607*043036a2SApple OSS Distributions setup_signal_handling(void)
608*043036a2SApple OSS Distributions {
609*043036a2SApple OSS Distributions int rv;
610*043036a2SApple OSS Distributions
611*043036a2SApple OSS Distributions struct sigaction sa = {
612*043036a2SApple OSS Distributions .__sigaction_u = { .__sa_sigaction = signal_handler },
613*043036a2SApple OSS Distributions .sa_flags = SA_SIGINFO
614*043036a2SApple OSS Distributions };
615*043036a2SApple OSS Distributions
616*043036a2SApple OSS Distributions sigfillset(&sa.sa_mask);
617*043036a2SApple OSS Distributions
618*043036a2SApple OSS Distributions rv = sigaction(SIGFPE, &sa, NULL);
619*043036a2SApple OSS Distributions if (rv != 0) {
620*043036a2SApple OSS Distributions #ifndef STANDALONE
621*043036a2SApple OSS Distributions T_ASSERT_FAIL("Failed to configure SIGFPE signal handler\n");
622*043036a2SApple OSS Distributions #else
623*043036a2SApple OSS Distributions fprintf(stderr, "Failed to configure SIGFPE signal handler\n");
624*043036a2SApple OSS Distributions abort();
625*043036a2SApple OSS Distributions #endif
626*043036a2SApple OSS Distributions }
627*043036a2SApple OSS Distributions
628*043036a2SApple OSS Distributions rv = sigaction(SIGILL, &sa, NULL);
629*043036a2SApple OSS Distributions if (rv != 0) {
630*043036a2SApple OSS Distributions #ifndef STANDALONE
631*043036a2SApple OSS Distributions T_ASSERT_FAIL("Failed to configure SIGILL signal handler\n");
632*043036a2SApple OSS Distributions #else
633*043036a2SApple OSS Distributions fprintf(stderr, "Failed to configure SIGILL signal handler\n");
634*043036a2SApple OSS Distributions abort();
635*043036a2SApple OSS Distributions #endif
636*043036a2SApple OSS Distributions }
637*043036a2SApple OSS Distributions }
638*043036a2SApple OSS Distributions
639*043036a2SApple OSS Distributions static void
teardown_signal_handling(void)640*043036a2SApple OSS Distributions teardown_signal_handling(void)
641*043036a2SApple OSS Distributions {
642*043036a2SApple OSS Distributions if (signal(SIGFPE, SIG_DFL) == SIG_ERR) {
643*043036a2SApple OSS Distributions #ifndef STANDALONE
644*043036a2SApple OSS Distributions T_ASSERT_FAIL("Error resetting SIGFPE signal disposition\n");
645*043036a2SApple OSS Distributions #else
646*043036a2SApple OSS Distributions fprintf(stderr, "Error resetting SIGFPE signal disposition\n");
647*043036a2SApple OSS Distributions abort();
648*043036a2SApple OSS Distributions #endif
649*043036a2SApple OSS Distributions }
650*043036a2SApple OSS Distributions
651*043036a2SApple OSS Distributions if (signal(SIGILL, SIG_DFL) == SIG_ERR) {
652*043036a2SApple OSS Distributions #ifndef STANDALONE
653*043036a2SApple OSS Distributions T_ASSERT_FAIL("Error resetting SIGILL signal disposition\n");
654*043036a2SApple OSS Distributions #else
655*043036a2SApple OSS Distributions fprintf(stderr, "Error resetting SIGILL signal disposition\n");
656*043036a2SApple OSS Distributions abort();
657*043036a2SApple OSS Distributions #endif
658*043036a2SApple OSS Distributions }
659*043036a2SApple OSS Distributions }
660*043036a2SApple OSS Distributions
661*043036a2SApple OSS Distributions #ifdef DEBUG
662*043036a2SApple OSS Distributions static void
dump_desc(union ldt_entry * entp)663*043036a2SApple OSS Distributions dump_desc(union ldt_entry *entp)
664*043036a2SApple OSS Distributions {
665*043036a2SApple OSS Distributions printf("base %p lim %p type 0x%x dpl %x present %x opsz %x granular %x\n",
666*043036a2SApple OSS Distributions (void *)(uintptr_t)(entp->code.base00 + (entp->code.base16 << 16) + (entp->code.base24 << 24)),
667*043036a2SApple OSS Distributions (void *)(uintptr_t)(entp->code.limit00 + (entp->code.limit16 << 16)),
668*043036a2SApple OSS Distributions entp->code.type,
669*043036a2SApple OSS Distributions entp->code.dpl,
670*043036a2SApple OSS Distributions entp->code.present,
671*043036a2SApple OSS Distributions entp->code.opsz,
672*043036a2SApple OSS Distributions entp->code.granular);
673*043036a2SApple OSS Distributions }
674*043036a2SApple OSS Distributions #endif
675*043036a2SApple OSS Distributions
676*043036a2SApple OSS Distributions static int
map_lowmem_stack(void ** lowmemstk)677*043036a2SApple OSS Distributions map_lowmem_stack(void **lowmemstk)
678*043036a2SApple OSS Distributions {
679*043036a2SApple OSS Distributions void *addr;
680*043036a2SApple OSS Distributions int err;
681*043036a2SApple OSS Distributions
682*043036a2SApple OSS Distributions if ((addr = mmap(0, FIXED_STACK_SIZE + PAGE_SIZE, PROT_READ | PROT_WRITE,
683*043036a2SApple OSS Distributions MAP_32BIT | MAP_PRIVATE | MAP_ANON, -1, 0)) == MAP_FAILED) {
684*043036a2SApple OSS Distributions return errno;
685*043036a2SApple OSS Distributions }
686*043036a2SApple OSS Distributions
687*043036a2SApple OSS Distributions if ((uintptr_t)addr > 0xFFFFF000ULL) {
688*043036a2SApple OSS Distributions /* Error: This kernel does not support MAP_32BIT or there's a bug. */
689*043036a2SApple OSS Distributions #ifndef STANDALONE
690*043036a2SApple OSS Distributions T_ASSERT_FAIL("%s: failed to map a 32-bit-accessible stack", __func__);
691*043036a2SApple OSS Distributions #else
692*043036a2SApple OSS Distributions fprintf(stderr, "This kernel returned a virtual address > 4G (%p) despite MAP_32BIT. Aborting.\n", addr);
693*043036a2SApple OSS Distributions exit(1);
694*043036a2SApple OSS Distributions #endif
695*043036a2SApple OSS Distributions }
696*043036a2SApple OSS Distributions
697*043036a2SApple OSS Distributions /* Enforce one page of redzone at the bottom of the stack */
698*043036a2SApple OSS Distributions if (mprotect(addr, PAGE_SIZE, PROT_NONE) < 0) {
699*043036a2SApple OSS Distributions err = errno;
700*043036a2SApple OSS Distributions (void) munmap(addr, FIXED_STACK_SIZE + PAGE_SIZE);
701*043036a2SApple OSS Distributions return err;
702*043036a2SApple OSS Distributions }
703*043036a2SApple OSS Distributions
704*043036a2SApple OSS Distributions if (lowmemstk) {
705*043036a2SApple OSS Distributions stack2gs[0].stack_base = (uintptr_t)addr + PAGE_SIZE;
706*043036a2SApple OSS Distributions stack2gs[0].stack_limit = stack2gs[0].stack_base + FIXED_STACK_SIZE;
707*043036a2SApple OSS Distributions *lowmemstk = (void *)((uintptr_t)addr + PAGE_SIZE);
708*043036a2SApple OSS Distributions }
709*043036a2SApple OSS Distributions
710*043036a2SApple OSS Distributions return 0;
711*043036a2SApple OSS Distributions }
712*043036a2SApple OSS Distributions
713*043036a2SApple OSS Distributions static int
map_32bit_code_impl(uint8_t * code_src,size_t code_len,void ** codeptr,size_t szlimit)714*043036a2SApple OSS Distributions map_32bit_code_impl(uint8_t *code_src, size_t code_len, void **codeptr,
715*043036a2SApple OSS Distributions size_t szlimit)
716*043036a2SApple OSS Distributions {
717*043036a2SApple OSS Distributions void *addr;
718*043036a2SApple OSS Distributions size_t sz = (size_t)P2ROUNDUP(code_len, (unsigned)PAGE_SIZE);
719*043036a2SApple OSS Distributions
720*043036a2SApple OSS Distributions if (code_len > szlimit) {
721*043036a2SApple OSS Distributions return E2BIG;
722*043036a2SApple OSS Distributions }
723*043036a2SApple OSS Distributions
724*043036a2SApple OSS Distributions #ifdef DEBUG
725*043036a2SApple OSS Distributions printf("size = %lu, szlimit = %u\n", sz, (unsigned)szlimit);
726*043036a2SApple OSS Distributions #endif
727*043036a2SApple OSS Distributions
728*043036a2SApple OSS Distributions if ((addr = mmap(0, sz, PROT_READ | PROT_WRITE | PROT_EXEC,
729*043036a2SApple OSS Distributions MAP_32BIT | MAP_PRIVATE | MAP_ANON, -1, 0)) == MAP_FAILED) {
730*043036a2SApple OSS Distributions return errno;
731*043036a2SApple OSS Distributions }
732*043036a2SApple OSS Distributions
733*043036a2SApple OSS Distributions if ((uintptr_t)addr > 0xFFFFF000ULL) {
734*043036a2SApple OSS Distributions /* Error: This kernel does not support MAP_32BIT or there's a bug. */
735*043036a2SApple OSS Distributions #ifndef STANDALONE
736*043036a2SApple OSS Distributions T_ASSERT_FAIL("%s: failed to map a 32-bit-accessible trampoline", __func__);
737*043036a2SApple OSS Distributions #else
738*043036a2SApple OSS Distributions fprintf(stderr, "This kernel returned a virtual address > 4G (%p) despite MAP_32BIT. Aborting.\n", addr);
739*043036a2SApple OSS Distributions exit(1);
740*043036a2SApple OSS Distributions #endif
741*043036a2SApple OSS Distributions }
742*043036a2SApple OSS Distributions
743*043036a2SApple OSS Distributions #ifdef DEBUG
744*043036a2SApple OSS Distributions printf("Mapping code @%p..%p => %p..%p\n", (void *)code_src,
745*043036a2SApple OSS Distributions (void *)((uintptr_t)code_src + (unsigned)code_len),
746*043036a2SApple OSS Distributions addr, (void *)((uintptr_t)addr + (unsigned)code_len));
747*043036a2SApple OSS Distributions #endif
748*043036a2SApple OSS Distributions
749*043036a2SApple OSS Distributions bcopy(code_src, addr, code_len);
750*043036a2SApple OSS Distributions
751*043036a2SApple OSS Distributions /* Fill the rest of the page with NOPs */
752*043036a2SApple OSS Distributions if ((sz - code_len) > 0) {
753*043036a2SApple OSS Distributions memset((void *)((uintptr_t)addr + code_len), 0x90, sz - code_len);
754*043036a2SApple OSS Distributions }
755*043036a2SApple OSS Distributions
756*043036a2SApple OSS Distributions if (codeptr) {
757*043036a2SApple OSS Distributions *codeptr = addr;
758*043036a2SApple OSS Distributions }
759*043036a2SApple OSS Distributions
760*043036a2SApple OSS Distributions return 0;
761*043036a2SApple OSS Distributions }
762*043036a2SApple OSS Distributions
763*043036a2SApple OSS Distributions static int
map_32bit_trampoline(compat_tramp_t * lowmemtrampp)764*043036a2SApple OSS Distributions map_32bit_trampoline(compat_tramp_t *lowmemtrampp)
765*043036a2SApple OSS Distributions {
766*043036a2SApple OSS Distributions extern int compat_mode_trampoline_len;
767*043036a2SApple OSS Distributions
768*043036a2SApple OSS Distributions return map_32bit_code_impl((uint8_t *)&compat_mode_trampoline,
769*043036a2SApple OSS Distributions (size_t)compat_mode_trampoline_len, (void **)lowmemtrampp,
770*043036a2SApple OSS Distributions FIXED_TRAMP_MAXLEN);
771*043036a2SApple OSS Distributions }
772*043036a2SApple OSS Distributions
773*043036a2SApple OSS Distributions static uint64_t
stack_range_to_GSbase(uint64_t stackptr,uint64_t GSbase)774*043036a2SApple OSS Distributions stack_range_to_GSbase(uint64_t stackptr, uint64_t GSbase)
775*043036a2SApple OSS Distributions {
776*043036a2SApple OSS Distributions unsigned long i;
777*043036a2SApple OSS Distributions
778*043036a2SApple OSS Distributions for (i = 0; i < sizeof(stack2gs) / sizeof(stack2gs[0]); i++) {
779*043036a2SApple OSS Distributions if (stackptr >= stack2gs[i].stack_base &&
780*043036a2SApple OSS Distributions stackptr < stack2gs[i].stack_limit) {
781*043036a2SApple OSS Distributions if (GSbase != 0) {
782*043036a2SApple OSS Distributions #ifdef DEBUG
783*043036a2SApple OSS Distributions fprintf(stderr, "Updated gsbase for stack at 0x%llx..0x%llx to 0x%llx\n",
784*043036a2SApple OSS Distributions stack2gs[i].stack_base, stack2gs[i].stack_limit, GSbase);
785*043036a2SApple OSS Distributions #endif
786*043036a2SApple OSS Distributions stack2gs[i].GSbase = GSbase;
787*043036a2SApple OSS Distributions }
788*043036a2SApple OSS Distributions return stack2gs[i].GSbase;
789*043036a2SApple OSS Distributions }
790*043036a2SApple OSS Distributions }
791*043036a2SApple OSS Distributions return 0;
792*043036a2SApple OSS Distributions }
793*043036a2SApple OSS Distributions
794*043036a2SApple OSS Distributions static uint64_t
call_compatmode(uint32_t stackaddr,uint64_t compat_arg,uint64_t callback)795*043036a2SApple OSS Distributions call_compatmode(uint32_t stackaddr, uint64_t compat_arg, uint64_t callback)
796*043036a2SApple OSS Distributions {
797*043036a2SApple OSS Distributions uint64_t rv;
798*043036a2SApple OSS Distributions
799*043036a2SApple OSS Distributions /*
800*043036a2SApple OSS Distributions * Depending on how this is used, this allocation may need to be
801*043036a2SApple OSS Distributions * made with an allocator that returns virtual addresses below 4G.
802*043036a2SApple OSS Distributions */
803*043036a2SApple OSS Distributions custom_tsd_t *new_GSbase = malloc(PAGE_SIZE);
804*043036a2SApple OSS Distributions
805*043036a2SApple OSS Distributions /*
806*043036a2SApple OSS Distributions * Change the GSbase (so things like printf will fail unless GSbase is
807*043036a2SApple OSS Distributions * restored)
808*043036a2SApple OSS Distributions */
809*043036a2SApple OSS Distributions if (new_GSbase != NULL) {
810*043036a2SApple OSS Distributions #ifdef DEBUG
811*043036a2SApple OSS Distributions fprintf(stderr, "Setting new GS base: %p\n", (void *)new_GSbase);
812*043036a2SApple OSS Distributions #endif
813*043036a2SApple OSS Distributions new_GSbase->this_tsd_base = new_GSbase;
814*043036a2SApple OSS Distributions new_GSbase->orig_tsd_base = get_gsbase();
815*043036a2SApple OSS Distributions _thread_set_tsd_base((uintptr_t)new_GSbase);
816*043036a2SApple OSS Distributions } else {
817*043036a2SApple OSS Distributions #ifndef STANDALONE
818*043036a2SApple OSS Distributions T_ASSERT_FAIL("Failed to allocate a page for new GSbase");
819*043036a2SApple OSS Distributions #else
820*043036a2SApple OSS Distributions fprintf(stderr, "Failed to allocate a page for new GSbase");
821*043036a2SApple OSS Distributions abort();
822*043036a2SApple OSS Distributions #endif
823*043036a2SApple OSS Distributions }
824*043036a2SApple OSS Distributions
825*043036a2SApple OSS Distributions rv = thunkit(&input_desc, (void *)(uintptr_t)stackaddr, compat_arg,
826*043036a2SApple OSS Distributions callback, thunk64_addr);
827*043036a2SApple OSS Distributions
828*043036a2SApple OSS Distributions restore_gsbase(stackaddr);
829*043036a2SApple OSS Distributions
830*043036a2SApple OSS Distributions free(new_GSbase);
831*043036a2SApple OSS Distributions
832*043036a2SApple OSS Distributions return rv;
833*043036a2SApple OSS Distributions }
834*043036a2SApple OSS Distributions
835*043036a2SApple OSS Distributions static uint64_t
get_cursp(void)836*043036a2SApple OSS Distributions get_cursp(void)
837*043036a2SApple OSS Distributions {
838*043036a2SApple OSS Distributions uint64_t curstk;
839*043036a2SApple OSS Distributions __asm__ __volatile__ ("movq %%rsp, %0" : "=r" (curstk) :: "memory");
840*043036a2SApple OSS Distributions return curstk;
841*043036a2SApple OSS Distributions }
842*043036a2SApple OSS Distributions
843*043036a2SApple OSS Distributions static void
hello_from_32bit(void)844*043036a2SApple OSS Distributions hello_from_32bit(void)
845*043036a2SApple OSS Distributions {
846*043036a2SApple OSS Distributions uint64_t cur_tsd_base = (uint64_t)(uintptr_t)mytsd->this_tsd_base;
847*043036a2SApple OSS Distributions restore_gsbase(get_cursp());
848*043036a2SApple OSS Distributions
849*043036a2SApple OSS Distributions printf("Hello on behalf of 32-bit compatibility mode!\n");
850*043036a2SApple OSS Distributions
851*043036a2SApple OSS Distributions _thread_set_tsd_base(cur_tsd_base);
852*043036a2SApple OSS Distributions }
853*043036a2SApple OSS Distributions
854*043036a2SApple OSS Distributions /*
855*043036a2SApple OSS Distributions * Thread for executing 32-bit code
856*043036a2SApple OSS Distributions */
857*043036a2SApple OSS Distributions static void *
thread_32bit(void * arg)858*043036a2SApple OSS Distributions thread_32bit(void *arg)
859*043036a2SApple OSS Distributions {
860*043036a2SApple OSS Distributions thread_arg_t *targp = (thread_arg_t *)arg;
861*043036a2SApple OSS Distributions uint64_t cthread_self = 0;
862*043036a2SApple OSS Distributions
863*043036a2SApple OSS Distributions /* Save the GSbase for context switch back to 64-bit mode */
864*043036a2SApple OSS Distributions cthread_self = get_gsbase();
865*043036a2SApple OSS Distributions
866*043036a2SApple OSS Distributions /*
867*043036a2SApple OSS Distributions * Associate GSbase with the compat-mode stack (which will be used for long mode
868*043036a2SApple OSS Distributions * thunk calls as well.)
869*043036a2SApple OSS Distributions */
870*043036a2SApple OSS Distributions (void)stack_range_to_GSbase(targp->compat_stackaddr, cthread_self);
871*043036a2SApple OSS Distributions
872*043036a2SApple OSS Distributions #ifdef DEBUG
873*043036a2SApple OSS Distributions printf("[thread %p] tsd base => %p\n", (void *)pthread_self(), (void *)cthread_self);
874*043036a2SApple OSS Distributions #endif
875*043036a2SApple OSS Distributions
876*043036a2SApple OSS Distributions pthread_mutex_lock(&targp->mutex);
877*043036a2SApple OSS Distributions
878*043036a2SApple OSS Distributions do {
879*043036a2SApple OSS Distributions if (targp->done == FALSE) {
880*043036a2SApple OSS Distributions pthread_cond_wait(&targp->condvar, &targp->mutex);
881*043036a2SApple OSS Distributions }
882*043036a2SApple OSS Distributions
883*043036a2SApple OSS Distributions /* Finally, execute the test */
884*043036a2SApple OSS Distributions if (call_compatmode(targp->compat_stackaddr, 0,
885*043036a2SApple OSS Distributions (uint64_t)&hello_from_32bit) == 1) {
886*043036a2SApple OSS Distributions printf("32-bit code test passed\n");
887*043036a2SApple OSS Distributions } else {
888*043036a2SApple OSS Distributions printf("32-bit code test failed\n");
889*043036a2SApple OSS Distributions }
890*043036a2SApple OSS Distributions } while (targp->done == FALSE);
891*043036a2SApple OSS Distributions
892*043036a2SApple OSS Distributions pthread_mutex_unlock(&targp->mutex);
893*043036a2SApple OSS Distributions
894*043036a2SApple OSS Distributions return 0;
895*043036a2SApple OSS Distributions }
896*043036a2SApple OSS Distributions
897*043036a2SApple OSS Distributions static void
join_32bit_thread(pthread_t * thridp,thread_arg_t * cmargp)898*043036a2SApple OSS Distributions join_32bit_thread(pthread_t *thridp, thread_arg_t *cmargp)
899*043036a2SApple OSS Distributions {
900*043036a2SApple OSS Distributions (void)pthread_mutex_lock(&cmargp->mutex);
901*043036a2SApple OSS Distributions cmargp->done = TRUE;
902*043036a2SApple OSS Distributions (void)pthread_cond_signal(&cmargp->condvar);
903*043036a2SApple OSS Distributions (void)pthread_mutex_unlock(&cmargp->mutex);
904*043036a2SApple OSS Distributions (void)pthread_join(*thridp, NULL);
905*043036a2SApple OSS Distributions *thridp = 0;
906*043036a2SApple OSS Distributions }
907*043036a2SApple OSS Distributions
908*043036a2SApple OSS Distributions static int
create_worker_thread(thread_arg_t * cmargp,uint32_t stackaddr,pthread_t * cmthreadp)909*043036a2SApple OSS Distributions create_worker_thread(thread_arg_t *cmargp, uint32_t stackaddr, pthread_t *cmthreadp)
910*043036a2SApple OSS Distributions {
911*043036a2SApple OSS Distributions *cmargp = (thread_arg_t) { .mutex = PTHREAD_MUTEX_INITIALIZER,
912*043036a2SApple OSS Distributions .condvar = PTHREAD_COND_INITIALIZER,
913*043036a2SApple OSS Distributions .done = FALSE,
914*043036a2SApple OSS Distributions .compat_stackaddr = stackaddr };
915*043036a2SApple OSS Distributions
916*043036a2SApple OSS Distributions return pthread_create(cmthreadp, NULL, thread_32bit, cmargp);
917*043036a2SApple OSS Distributions }
918*043036a2SApple OSS Distributions
919*043036a2SApple OSS Distributions static void
ldt64_test_setup(pthread_t * cmthreadp,thread_arg_t * cmargp,boolean_t setldt_in_sighandler)920*043036a2SApple OSS Distributions ldt64_test_setup(pthread_t *cmthreadp, thread_arg_t *cmargp, boolean_t setldt_in_sighandler)
921*043036a2SApple OSS Distributions {
922*043036a2SApple OSS Distributions extern void thunk64(void);
923*043036a2SApple OSS Distributions extern void thunk64_movabs(void);
924*043036a2SApple OSS Distributions int cnt = 0, err;
925*043036a2SApple OSS Distributions void *addr;
926*043036a2SApple OSS Distributions uintptr_t code_addr;
927*043036a2SApple OSS Distributions uintptr_t thunk64_movabs_addr;
928*043036a2SApple OSS Distributions
929*043036a2SApple OSS Distributions descs = malloc(sizeof(union ldt_entry) * 256);
930*043036a2SApple OSS Distributions if (descs == 0) {
931*043036a2SApple OSS Distributions #ifndef STANDALONE
932*043036a2SApple OSS Distributions T_ASSERT_FAIL("Could not allocate descriptor storage");
933*043036a2SApple OSS Distributions #else
934*043036a2SApple OSS Distributions fprintf(stderr, "Could not allocate descriptor storage\n");
935*043036a2SApple OSS Distributions abort();
936*043036a2SApple OSS Distributions #endif
937*043036a2SApple OSS Distributions }
938*043036a2SApple OSS Distributions
939*043036a2SApple OSS Distributions #ifdef DEBUG
940*043036a2SApple OSS Distributions printf("32-bit code is at %p\n", (void *)&code_32);
941*043036a2SApple OSS Distributions #endif
942*043036a2SApple OSS Distributions
943*043036a2SApple OSS Distributions if ((err = map_lowmem_stack(&addr)) != 0) {
944*043036a2SApple OSS Distributions #ifndef STANDALONE
945*043036a2SApple OSS Distributions T_ASSERT_FAIL("failed to mmap lowmem stack: %s", strerror(err));
946*043036a2SApple OSS Distributions #else
947*043036a2SApple OSS Distributions fprintf(stderr, "Failed to mmap lowmem stack: %s\n", strerror(err));
948*043036a2SApple OSS Distributions exit(1);
949*043036a2SApple OSS Distributions #endif
950*043036a2SApple OSS Distributions }
951*043036a2SApple OSS Distributions
952*043036a2SApple OSS Distributions stackAddr = (uintptr_t)addr + FIXED_STACK_SIZE - 16;
953*043036a2SApple OSS Distributions #ifdef DEBUG
954*043036a2SApple OSS Distributions printf("lowstack addr = %p\n", (void *)stackAddr);
955*043036a2SApple OSS Distributions #endif
956*043036a2SApple OSS Distributions
957*043036a2SApple OSS Distributions if ((err = map_32bit_trampoline(&thunkit)) != 0) {
958*043036a2SApple OSS Distributions #ifndef STANDALONE
959*043036a2SApple OSS Distributions T_LOG("Failed to map trampoline into lowmem: %s\n", strerror(err));
960*043036a2SApple OSS Distributions T_ASSERT_FAIL("Failed to map trampoline into lowmem");
961*043036a2SApple OSS Distributions #else
962*043036a2SApple OSS Distributions fprintf(stderr, "Failed to map trampoline into lowmem: %s\n", strerror(err));
963*043036a2SApple OSS Distributions exit(1);
964*043036a2SApple OSS Distributions #endif
965*043036a2SApple OSS Distributions }
966*043036a2SApple OSS Distributions
967*043036a2SApple OSS Distributions /*
968*043036a2SApple OSS Distributions * Store long_mode_trampoline's address into the constant part of the movabs
969*043036a2SApple OSS Distributions * instruction in thunk64
970*043036a2SApple OSS Distributions */
971*043036a2SApple OSS Distributions thunk64_movabs_addr = (uintptr_t)thunkit + ((uintptr_t)thunk64_movabs - (uintptr_t)compat_mode_trampoline);
972*043036a2SApple OSS Distributions *((uint64_t *)(thunk64_movabs_addr + 2)) = (uint64_t)&long_mode_trampoline;
973*043036a2SApple OSS Distributions
974*043036a2SApple OSS Distributions bzero(descs, sizeof(union ldt_entry) * 256);
975*043036a2SApple OSS Distributions
976*043036a2SApple OSS Distributions if ((cnt = i386_get_ldt(0, descs, 1)) <= 0) {
977*043036a2SApple OSS Distributions #ifndef STANDALONE
978*043036a2SApple OSS Distributions T_LOG("i386_get_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
979*043036a2SApple OSS Distributions T_ASSERT_FAIL("i386_get_ldt failure");
980*043036a2SApple OSS Distributions #else
981*043036a2SApple OSS Distributions fprintf(stderr, "i386_get_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
982*043036a2SApple OSS Distributions exit(1);
983*043036a2SApple OSS Distributions #endif
984*043036a2SApple OSS Distributions }
985*043036a2SApple OSS Distributions
986*043036a2SApple OSS Distributions #ifdef DEBUG
987*043036a2SApple OSS Distributions printf("i386_get_ldt returned %d\n", cnt);
988*043036a2SApple OSS Distributions #endif
989*043036a2SApple OSS Distributions
990*043036a2SApple OSS Distributions idx = (unsigned)cnt; /* Put the desired descriptor in the first available slot */
991*043036a2SApple OSS Distributions
992*043036a2SApple OSS Distributions /*
993*043036a2SApple OSS Distributions * code_32's address for the purposes of this descriptor is the base mapped address of
994*043036a2SApple OSS Distributions * the thunkit function + the offset of code_32 from compat_mode_trampoline.
995*043036a2SApple OSS Distributions */
996*043036a2SApple OSS Distributions code_addr = (uintptr_t)thunkit + ((uintptr_t)code_32 - (uintptr_t)compat_mode_trampoline);
997*043036a2SApple OSS Distributions thunk64_addr = (uintptr_t)thunkit + ((uintptr_t)thunk64 - (uintptr_t)compat_mode_trampoline);
998*043036a2SApple OSS Distributions
999*043036a2SApple OSS Distributions /* Initialize desired descriptor */
1000*043036a2SApple OSS Distributions descs[idx].code.limit00 = (unsigned short)(((code_addr >> 12) + 1) & 0xFFFF);
1001*043036a2SApple OSS Distributions descs[idx].code.limit16 = (unsigned char)((((code_addr >> 12) + 1) >> 16) & 0xF);
1002*043036a2SApple OSS Distributions descs[idx].code.base00 = (unsigned short)((code_addr) & 0xFFFF);
1003*043036a2SApple OSS Distributions descs[idx].code.base16 = (unsigned char)((code_addr >> 16) & 0xFF);
1004*043036a2SApple OSS Distributions descs[idx].code.base24 = (unsigned char)((code_addr >> 24) & 0xFF);
1005*043036a2SApple OSS Distributions descs[idx].code.type = DESC_CODE_READ;
1006*043036a2SApple OSS Distributions descs[idx].code.opsz = DESC_CODE_32B;
1007*043036a2SApple OSS Distributions descs[idx].code.granular = DESC_GRAN_PAGE;
1008*043036a2SApple OSS Distributions descs[idx].code.dpl = 3;
1009*043036a2SApple OSS Distributions descs[idx].code.present = 1;
1010*043036a2SApple OSS Distributions
1011*043036a2SApple OSS Distributions if (setldt_in_sighandler == FALSE) {
1012*043036a2SApple OSS Distributions /* Set the LDT: */
1013*043036a2SApple OSS Distributions cnt = i386_set_ldt((int)idx, &descs[idx], 1);
1014*043036a2SApple OSS Distributions if (cnt != (int)idx) {
1015*043036a2SApple OSS Distributions #ifndef STANDALONE
1016*043036a2SApple OSS Distributions T_LOG("i386_set_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
1017*043036a2SApple OSS Distributions T_ASSERT_FAIL("i386_set_ldt failure");
1018*043036a2SApple OSS Distributions #else
1019*043036a2SApple OSS Distributions fprintf(stderr, "i386_set_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
1020*043036a2SApple OSS Distributions exit(1);
1021*043036a2SApple OSS Distributions #endif
1022*043036a2SApple OSS Distributions }
1023*043036a2SApple OSS Distributions #ifdef DEBUG
1024*043036a2SApple OSS Distributions printf("i386_set_ldt returned %d\n", cnt);
1025*043036a2SApple OSS Distributions #endif
1026*043036a2SApple OSS Distributions } else {
1027*043036a2SApple OSS Distributions __asm__ __volatile__ ("ud2" ::: "memory");
1028*043036a2SApple OSS Distributions }
1029*043036a2SApple OSS Distributions
1030*043036a2SApple OSS Distributions
1031*043036a2SApple OSS Distributions /* Read back the LDT to ensure it was set properly */
1032*043036a2SApple OSS Distributions if ((cnt = i386_get_ldt(0, descs, (int)idx)) > 0) {
1033*043036a2SApple OSS Distributions #ifdef DEBUG
1034*043036a2SApple OSS Distributions for (int i = 0; i < cnt; i++) {
1035*043036a2SApple OSS Distributions dump_desc(&descs[i]);
1036*043036a2SApple OSS Distributions }
1037*043036a2SApple OSS Distributions #endif
1038*043036a2SApple OSS Distributions } else {
1039*043036a2SApple OSS Distributions #ifndef STANDALONE
1040*043036a2SApple OSS Distributions T_LOG("i386_get_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
1041*043036a2SApple OSS Distributions T_ASSERT_FAIL("i386_get_ldt failure");
1042*043036a2SApple OSS Distributions #else
1043*043036a2SApple OSS Distributions fprintf(stderr, "i386_get_ldt unexpectedly returned %d (errno: %s)\n", cnt, strerror(errno));
1044*043036a2SApple OSS Distributions exit(1);
1045*043036a2SApple OSS Distributions #endif
1046*043036a2SApple OSS Distributions }
1047*043036a2SApple OSS Distributions
1048*043036a2SApple OSS Distributions free(descs);
1049*043036a2SApple OSS Distributions
1050*043036a2SApple OSS Distributions if ((err = create_worker_thread(cmargp, (uint32_t)stackAddr, cmthreadp)) != 0) {
1051*043036a2SApple OSS Distributions #ifdef DEBUG
1052*043036a2SApple OSS Distributions fprintf(stderr, "Fatal: Could not create thread: %s\n", strerror(err));
1053*043036a2SApple OSS Distributions #endif
1054*043036a2SApple OSS Distributions #ifndef STANDALONE
1055*043036a2SApple OSS Distributions T_LOG("Fatal: Could not create thread: %s\n", strerror(err));
1056*043036a2SApple OSS Distributions T_ASSERT_FAIL("Thread creation failure");
1057*043036a2SApple OSS Distributions #else
1058*043036a2SApple OSS Distributions exit(1);
1059*043036a2SApple OSS Distributions #endif
1060*043036a2SApple OSS Distributions }
1061*043036a2SApple OSS Distributions }
1062*043036a2SApple OSS Distributions
1063*043036a2SApple OSS Distributions #ifdef STANDALONE
1064*043036a2SApple OSS Distributions static void
test_ldt64_with_bsdsig(void)1065*043036a2SApple OSS Distributions test_ldt64_with_bsdsig(void)
1066*043036a2SApple OSS Distributions #else
1067*043036a2SApple OSS Distributions /*
1068*043036a2SApple OSS Distributions * Main test declarations
1069*043036a2SApple OSS Distributions */
1070*043036a2SApple OSS Distributions T_DECL(ldt64_with_bsd_sighandling,
1071*043036a2SApple OSS Distributions "Ensures that a 64-bit process can create LDT entries and can execute code in "
1072*043036a2SApple OSS Distributions "compatibility mode with BSD signal handling",
1073*043036a2SApple OSS Distributions T_META_TIMEOUT(NORMAL_RUN_TIME + TIMEOUT_OVERHEAD))
1074*043036a2SApple OSS Distributions #endif
1075*043036a2SApple OSS Distributions {
1076*043036a2SApple OSS Distributions pthread_t cmthread;
1077*043036a2SApple OSS Distributions thread_arg_t cmarg;
1078*043036a2SApple OSS Distributions
1079*043036a2SApple OSS Distributions int translated = 0;
1080*043036a2SApple OSS Distributions size_t translated_size = sizeof(int);
1081*043036a2SApple OSS Distributions
1082*043036a2SApple OSS Distributions sysctlbyname("sysctl.proc_translated", &translated, &translated_size, NULL, 0);
1083*043036a2SApple OSS Distributions
1084*043036a2SApple OSS Distributions if (translated) {
1085*043036a2SApple OSS Distributions T_SKIP("Skipping this test because it is translated");
1086*043036a2SApple OSS Distributions }
1087*043036a2SApple OSS Distributions
1088*043036a2SApple OSS Distributions setup_signal_handling();
1089*043036a2SApple OSS Distributions
1090*043036a2SApple OSS Distributions #ifndef STANDALONE
1091*043036a2SApple OSS Distributions T_SETUPBEGIN;
1092*043036a2SApple OSS Distributions #endif
1093*043036a2SApple OSS Distributions ENV_set_ldt_in_sighandler = (getenv("LDT_SET_IN_SIGHANDLER") != NULL) ? TRUE : FALSE;
1094*043036a2SApple OSS Distributions ldt64_test_setup(&cmthread, &cmarg, ENV_set_ldt_in_sighandler);
1095*043036a2SApple OSS Distributions #ifndef STANDALONE
1096*043036a2SApple OSS Distributions T_SETUPEND;
1097*043036a2SApple OSS Distributions #endif
1098*043036a2SApple OSS Distributions
1099*043036a2SApple OSS Distributions join_32bit_thread(&cmthread, &cmarg);
1100*043036a2SApple OSS Distributions
1101*043036a2SApple OSS Distributions teardown_signal_handling();
1102*043036a2SApple OSS Distributions
1103*043036a2SApple OSS Distributions #ifndef STANDALONE
1104*043036a2SApple OSS Distributions T_PASS("Successfully completed ldt64 test with BSD signal handling");
1105*043036a2SApple OSS Distributions #else
1106*043036a2SApple OSS Distributions fprintf(stderr, "PASSED: ldt64_with_bsd_signal_handling\n");
1107*043036a2SApple OSS Distributions #endif
1108*043036a2SApple OSS Distributions }
1109*043036a2SApple OSS Distributions
1110*043036a2SApple OSS Distributions #ifdef STANDALONE
1111*043036a2SApple OSS Distributions static void
test_ldt64_with_machexc(void)1112*043036a2SApple OSS Distributions test_ldt64_with_machexc(void)
1113*043036a2SApple OSS Distributions #else
1114*043036a2SApple OSS Distributions T_DECL(ldt64_with_mach_exception_handling,
1115*043036a2SApple OSS Distributions "Ensures that a 64-bit process can create LDT entries and can execute code in "
1116*043036a2SApple OSS Distributions "compatibility mode with Mach exception handling",
1117*043036a2SApple OSS Distributions T_META_TIMEOUT(NORMAL_RUN_TIME + TIMEOUT_OVERHEAD))
1118*043036a2SApple OSS Distributions #endif
1119*043036a2SApple OSS Distributions {
1120*043036a2SApple OSS Distributions pthread_t cmthread;
1121*043036a2SApple OSS Distributions thread_arg_t cmarg;
1122*043036a2SApple OSS Distributions
1123*043036a2SApple OSS Distributions int translated = 0;
1124*043036a2SApple OSS Distributions size_t translated_size = sizeof(int);
1125*043036a2SApple OSS Distributions
1126*043036a2SApple OSS Distributions sysctlbyname("sysctl.proc_translated", &translated, &translated_size, NULL, 0);
1127*043036a2SApple OSS Distributions
1128*043036a2SApple OSS Distributions if (translated) {
1129*043036a2SApple OSS Distributions T_SKIP("Skipping this test because it is translated");
1130*043036a2SApple OSS Distributions }
1131*043036a2SApple OSS Distributions
1132*043036a2SApple OSS Distributions #ifndef STANDALONE
1133*043036a2SApple OSS Distributions T_SETUPBEGIN;
1134*043036a2SApple OSS Distributions #endif
1135*043036a2SApple OSS Distributions ldt64_test_setup(&cmthread, &cmarg, FALSE);
1136*043036a2SApple OSS Distributions #ifndef STANDALONE
1137*043036a2SApple OSS Distributions T_SETUPEND;
1138*043036a2SApple OSS Distributions #endif
1139*043036a2SApple OSS Distributions
1140*043036a2SApple OSS Distributions /* Now repeat with Mach exception handling */
1141*043036a2SApple OSS Distributions init_task_exception_server();
1142*043036a2SApple OSS Distributions
1143*043036a2SApple OSS Distributions join_32bit_thread(&cmthread, &cmarg);
1144*043036a2SApple OSS Distributions
1145*043036a2SApple OSS Distributions #ifndef STANDALONE
1146*043036a2SApple OSS Distributions T_PASS("Successfully completed ldt64 test with mach exception handling");
1147*043036a2SApple OSS Distributions #else
1148*043036a2SApple OSS Distributions fprintf(stderr, "PASSED: ldt64_with_mach_exception_handling\n");
1149*043036a2SApple OSS Distributions #endif
1150*043036a2SApple OSS Distributions }
1151*043036a2SApple OSS Distributions
1152*043036a2SApple OSS Distributions #ifdef STANDALONE
1153*043036a2SApple OSS Distributions int
main(int __unused argc,char ** __unused argv)1154*043036a2SApple OSS Distributions main(int __unused argc, char ** __unused argv)
1155*043036a2SApple OSS Distributions {
1156*043036a2SApple OSS Distributions test_ldt64_with_bsdsig();
1157*043036a2SApple OSS Distributions test_ldt64_with_machexc();
1158*043036a2SApple OSS Distributions }
1159*043036a2SApple OSS Distributions #endif
1160