2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1992 NeXT, Inc.
32 * 13 May 1992 ? at NeXT
36 #include <mach/mach_types.h>
37 #include <mach/exception.h>
39 #include <kern/thread.h>
41 #include <sys/systm.h>
42 #include <sys/param.h>
43 #include <sys/proc_internal.h>
45 #include <sys/sysproto.h>
46 #include <sys/sysent.h>
47 #include <sys/ucontext.h>
50 #include <sys/ux_exception.h>
52 #include <mach/thread_act.h> /* for thread_abort_safely */
53 #include <mach/thread_status.h>
55 #include <i386/eflags.h>
57 #include <i386/machine_routines.h>
61 #include <machine/pal_routines.h>
63 #include <sys/kdebug.h>
68 extern kern_return_t
thread_getstatus(thread_t act
, int flavor
,
69 thread_state_t tstate
, mach_msg_type_number_t
*count
);
70 extern kern_return_t
thread_setstatus(thread_t thread
, int flavor
,
71 thread_state_t tstate
, mach_msg_type_number_t count
);
73 /* Signal handler flavors supported */
74 /* These defns should match the Libc implmn */
77 #define UC_SET_ALT_STACK 0x40000000
78 #define UC_RESET_ALT_STACK 0x80000000
80 #define C_32_STK_ALIGN 16
81 #define C_64_STK_ALIGN 16
82 #define C_64_REDZONE_LEN 128
83 #define TRUNC_DOWN32(a,c) ((((uint32_t)a)-(c)) & ((uint32_t)(-(c))))
84 #define TRUNC_DOWN64(a,c) ((((uint64_t)a)-(c)) & ((uint64_t)(-(c))))
87 * Send an interrupt to process.
89 * Stack is set up to allow sigcode stored
90 * in u. to call routine, followed by chmk
91 * to sigreturn routine below. After sigreturn
92 * resets the signal mask, the stack, the frame
93 * pointer, and the argument pointer, it returns
94 * to the user specified pc, psl.
98 user32_addr_t catcher
; /* sig_t */
101 user32_addr_t sinfo
; /* siginfo32_t* */
102 user32_addr_t uctx
; /* struct ucontext32 */
107 * Declare table of structure flavors and sizes for 64-bit and 32-bit processes
108 * for the cases of extended states (plain FP, or AVX):
111 int flavor
; natural_t state_count
; size_t mcontext_size
;
113 static const xstate_info_t thread_state64
[] = {
114 [FP
] = { x86_FLOAT_STATE64
, x86_FLOAT_STATE64_COUNT
, sizeof(struct mcontext64
) },
115 [AVX
] = { x86_AVX_STATE64
, x86_AVX_STATE64_COUNT
, sizeof(struct mcontext_avx64
) },
116 #if !defined(RC_HIDE_XNU_J137)
117 [AVX512
] = { x86_AVX512_STATE64
, x86_AVX512_STATE64_COUNT
, sizeof(struct mcontext_avx512_64
) }
120 static const xstate_info_t thread_state32
[] = {
121 [FP
] = { x86_FLOAT_STATE32
, x86_FLOAT_STATE32_COUNT
, sizeof(struct mcontext32
) },
122 [AVX
] = { x86_AVX_STATE32
, x86_AVX_STATE32_COUNT
, sizeof(struct mcontext_avx32
) },
123 #if !defined(RC_HIDE_XNU_J137)
124 [AVX512
] = { x86_AVX512_STATE32
, x86_AVX512_STATE32_COUNT
, sizeof(struct mcontext_avx512_32
) }
129 * NOTE: Source and target may *NOT* overlap!
130 * XXX: Unify with bsd/kern/kern_exit.c
133 siginfo_user_to_user32_x86(user_siginfo_t
*in
, user32_siginfo_t
*out
)
135 out
->si_signo
= in
->si_signo
;
136 out
->si_errno
= in
->si_errno
;
137 out
->si_code
= in
->si_code
;
138 out
->si_pid
= in
->si_pid
;
139 out
->si_uid
= in
->si_uid
;
140 out
->si_status
= in
->si_status
;
141 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
);
142 /* following cast works for sival_int because of padding */
143 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
);
144 out
->si_band
= in
->si_band
; /* range reduction */
145 out
->__pad
[0] = in
->pad
[0]; /* mcontext.ss.r1 */
149 siginfo_user_to_user64_x86(user_siginfo_t
*in
, user64_siginfo_t
*out
)
151 out
->si_signo
= in
->si_signo
;
152 out
->si_errno
= in
->si_errno
;
153 out
->si_code
= in
->si_code
;
154 out
->si_pid
= in
->si_pid
;
155 out
->si_uid
= in
->si_uid
;
156 out
->si_status
= in
->si_status
;
157 out
->si_addr
= in
->si_addr
;
158 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
159 out
->si_band
= in
->si_band
; /* range reduction */
160 out
->__pad
[0] = in
->pad
[0]; /* mcontext.ss.r1 */
164 sendsig(struct proc
*p
, user_addr_t ua_catcher
, int sig
, int mask
, __unused
uint32_t code
)
167 struct mcontext_avx32 mctx_avx32
;
168 struct mcontext_avx64 mctx_avx64
;
169 #if !defined(RC_HIDE_XNU_J137)
170 struct mcontext_avx512_32 mctx_avx512_32
;
171 struct mcontext_avx512_64 mctx_avx512_64
;
173 } mctx_store
, *mctxp
= &mctx_store
;
179 user_addr_t ua_uctxp
;
180 user_addr_t ua_mctxp
;
181 user_siginfo_t sinfo64
;
183 struct sigacts
*ps
= p
->p_sigacts
;
184 int oonstack
, flavor
;
185 user_addr_t trampact
;
188 mach_msg_type_number_t state_count
;
193 int infostyle
= UC_TRAD
;
195 user_addr_t token_uctx
;
198 thread
= current_thread();
199 ut
= get_bsdthread_info(thread
);
201 if (p
->p_sigacts
->ps_siginfo
& sigmask(sig
))
202 infostyle
= UC_FLAVOR
;
204 oonstack
= ut
->uu_sigstk
.ss_flags
& SA_ONSTACK
;
205 trampact
= ps
->ps_trampact
[sig
];
206 sigonstack
= (ps
->ps_sigonstack
& sigmask(sig
));
213 bzero((caddr_t
)&sinfo64
, sizeof(sinfo64
));
214 sinfo64
.si_signo
= sig
;
216 bzero(mctxp
, sizeof(*mctxp
));
218 sig_xstate
= current_xstate();
220 if (proc_is64bit(p
)) {
221 x86_thread_state64_t
*tstate64
;
222 struct user_ucontext64 uctx64
;
225 flavor
= x86_THREAD_STATE64
;
226 state_count
= x86_THREAD_STATE64_COUNT
;
227 state
= (void *)&mctxp
->mctx_avx64
.ss
;
228 if (thread_getstatus(thread
, flavor
, (thread_state_t
)state
, &state_count
) != KERN_SUCCESS
)
231 flavor
= thread_state64
[sig_xstate
].flavor
;
232 state_count
= thread_state64
[sig_xstate
].state_count
;
233 state
= (void *)&mctxp
->mctx_avx64
.fs
;
234 if (thread_getstatus(thread
, flavor
, (thread_state_t
)state
, &state_count
) != KERN_SUCCESS
)
237 flavor
= x86_EXCEPTION_STATE64
;
238 state_count
= x86_EXCEPTION_STATE64_COUNT
;
239 state
= (void *)&mctxp
->mctx_avx64
.es
;
240 if (thread_getstatus(thread
, flavor
, (thread_state_t
)state
, &state_count
) != KERN_SUCCESS
)
243 tstate64
= &mctxp
->mctx_avx64
.ss
;
245 /* figure out where our new stack lives */
246 if ((ut
->uu_flag
& UT_ALTSTACK
) && !oonstack
&&
248 ua_sp
= ut
->uu_sigstk
.ss_sp
;
249 stack_size
= ut
->uu_sigstk
.ss_size
;
251 ut
->uu_sigstk
.ss_flags
|= SA_ONSTACK
;
253 ua_sp
= tstate64
->rsp
;
255 ua_cr2
= mctxp
->mctx_avx64
.es
.faultvaddr
;
257 /* The x86_64 ABI defines a 128-byte red zone. */
258 ua_sp
-= C_64_REDZONE_LEN
;
260 ua_sp
-= sizeof (struct user_ucontext64
);
261 ua_uctxp
= ua_sp
; // someone tramples the first word!
263 ua_sp
-= sizeof (user64_siginfo_t
);
266 ua_sp
-= thread_state64
[sig_xstate
].mcontext_size
;
270 * Align the frame and stack pointers to 16 bytes for SSE.
271 * (Note that we use 'ua_fp' as the base of the stack going forward)
273 ua_fp
= TRUNC_DOWN64(ua_sp
, C_64_STK_ALIGN
);
276 * But we need to account for the return address so the alignment is
277 * truly "correct" at _sigtramp
279 ua_fp
-= sizeof(user_addr_t
);
282 * Generate the validation token for sigreturn
284 token_uctx
= ua_uctxp
;
285 kr
= machine_thread_siguctx_pointer_convert_to_user(thread
, &token_uctx
);
286 assert(kr
== KERN_SUCCESS
);
287 token
= (user64_addr_t
)token_uctx
^ (user64_addr_t
)ps
->ps_sigreturn_token
;
290 * Build the signal context to be used by sigreturn.
292 bzero(&uctx64
, sizeof(uctx64
));
294 uctx64
.uc_onstack
= oonstack
;
295 uctx64
.uc_sigmask
= mask
;
296 uctx64
.uc_stack
.ss_sp
= ua_fp
;
297 uctx64
.uc_stack
.ss_size
= stack_size
;
300 uctx64
.uc_stack
.ss_flags
|= SS_ONSTACK
;
303 uctx64
.uc_mcsize
= thread_state64
[sig_xstate
].mcontext_size
;
304 uctx64
.uc_mcontext64
= ua_mctxp
;
306 if (copyout((caddr_t
)&uctx64
, ua_uctxp
, sizeof (uctx64
)))
309 if (copyout((caddr_t
)&mctx_store
, ua_mctxp
, thread_state64
[sig_xstate
].mcontext_size
))
312 sinfo64
.pad
[0] = tstate64
->rsp
;
313 sinfo64
.si_addr
= tstate64
->rip
;
315 tstate64
->rip
= trampact
;
316 tstate64
->rsp
= ua_fp
;
317 tstate64
->rflags
= get_eflags_exportmask();
319 * JOE - might not need to set these
321 tstate64
->cs
= USER64_CS
;
322 tstate64
->fs
= NULL_SEG
;
323 tstate64
->gs
= USER_CTHREAD
;
326 * Build the argument list for the signal handler.
327 * Handler should call sigreturn to get out of it
329 tstate64
->rdi
= ua_catcher
;
330 tstate64
->rsi
= infostyle
;
332 tstate64
->rcx
= ua_sip
;
333 tstate64
->r8
= ua_uctxp
;
334 tstate64
->r9
= token
;
336 x86_thread_state32_t
*tstate32
;
337 struct user_ucontext32 uctx32
;
338 struct sigframe32 frame32
;
341 flavor
= x86_THREAD_STATE32
;
342 state_count
= x86_THREAD_STATE32_COUNT
;
343 state
= (void *)&mctxp
->mctx_avx32
.ss
;
344 if (thread_getstatus(thread
, flavor
, (thread_state_t
)state
, &state_count
) != KERN_SUCCESS
)
347 flavor
= thread_state32
[sig_xstate
].flavor
;
348 state_count
= thread_state32
[sig_xstate
].state_count
;
349 state
= (void *)&mctxp
->mctx_avx32
.fs
;
350 if (thread_getstatus(thread
, flavor
, (thread_state_t
)state
, &state_count
) != KERN_SUCCESS
)
353 flavor
= x86_EXCEPTION_STATE32
;
354 state_count
= x86_EXCEPTION_STATE32_COUNT
;
355 state
= (void *)&mctxp
->mctx_avx32
.es
;
356 if (thread_getstatus(thread
, flavor
, (thread_state_t
)state
, &state_count
) != KERN_SUCCESS
)
359 tstate32
= &mctxp
->mctx_avx32
.ss
;
361 /* figure out where our new stack lives */
362 if ((ut
->uu_flag
& UT_ALTSTACK
) && !oonstack
&&
364 ua_sp
= ut
->uu_sigstk
.ss_sp
;
365 stack_size
= ut
->uu_sigstk
.ss_size
;
367 ut
->uu_sigstk
.ss_flags
|= SA_ONSTACK
;
369 ua_sp
= tstate32
->esp
;
371 ua_cr2
= mctxp
->mctx_avx32
.es
.faultvaddr
;
373 ua_sp
-= sizeof (struct user_ucontext32
);
374 ua_uctxp
= ua_sp
; // someone tramples the first word!
376 ua_sp
-= sizeof (user32_siginfo_t
);
379 ua_sp
-= thread_state32
[sig_xstate
].mcontext_size
;
382 ua_sp
-= sizeof (struct sigframe32
);
386 * Align the frame and stack pointers to 16 bytes for SSE.
387 * (Note that we use 'fp' as the base of the stack going forward)
389 ua_fp
= TRUNC_DOWN32(ua_fp
, C_32_STK_ALIGN
);
392 * But we need to account for the return address so the alignment is
393 * truly "correct" at _sigtramp
395 ua_fp
-= sizeof(frame32
.retaddr
);
398 * Generate the validation token for sigreturn
400 token_uctx
= ua_uctxp
;
401 kr
= machine_thread_siguctx_pointer_convert_to_user(thread
, &token_uctx
);
402 assert(kr
== KERN_SUCCESS
);
403 token
= CAST_DOWN_EXPLICIT(user32_addr_t
, token_uctx
) ^
404 CAST_DOWN_EXPLICIT(user32_addr_t
, ps
->ps_sigreturn_token
);
407 * Build the argument list for the signal handler.
408 * Handler should call sigreturn to get out of it
410 frame32
.retaddr
= -1;
411 frame32
.sigstyle
= infostyle
;
413 frame32
.catcher
= CAST_DOWN_EXPLICIT(user32_addr_t
, ua_catcher
);
414 frame32
.sinfo
= CAST_DOWN_EXPLICIT(user32_addr_t
, ua_sip
);
415 frame32
.uctx
= CAST_DOWN_EXPLICIT(user32_addr_t
, ua_uctxp
);
416 frame32
.token
= token
;
418 if (copyout((caddr_t
)&frame32
, ua_fp
, sizeof (frame32
)))
422 * Build the signal context to be used by sigreturn.
424 bzero(&uctx32
, sizeof(uctx32
));
426 uctx32
.uc_onstack
= oonstack
;
427 uctx32
.uc_sigmask
= mask
;
428 uctx32
.uc_stack
.ss_sp
= CAST_DOWN_EXPLICIT(user32_addr_t
, ua_fp
);
429 uctx32
.uc_stack
.ss_size
= stack_size
;
432 uctx32
.uc_stack
.ss_flags
|= SS_ONSTACK
;
435 uctx32
.uc_mcsize
= thread_state64
[sig_xstate
].mcontext_size
;
437 uctx32
.uc_mcontext
= CAST_DOWN_EXPLICIT(user32_addr_t
, ua_mctxp
);
439 if (copyout((caddr_t
)&uctx32
, ua_uctxp
, sizeof (uctx32
)))
442 if (copyout((caddr_t
)&mctx_store
, ua_mctxp
, thread_state32
[sig_xstate
].mcontext_size
))
445 sinfo64
.pad
[0] = tstate32
->esp
;
446 sinfo64
.si_addr
= tstate32
->eip
;
451 switch (ut
->uu_code
) {
453 sinfo64
.si_code
= ILL_ILLOPC
;
456 sinfo64
.si_code
= ILL_NOOP
;
460 #define FP_IE 0 /* Invalid operation */
461 #define FP_DE 1 /* Denormalized operand */
462 #define FP_ZE 2 /* Zero divide */
463 #define FP_OE 3 /* overflow */
464 #define FP_UE 4 /* underflow */
465 #define FP_PE 5 /* precision */
466 if (ut
->uu_code
== EXC_I386_DIV
) {
467 sinfo64
.si_code
= FPE_INTDIV
;
469 else if (ut
->uu_code
== EXC_I386_INTO
) {
470 sinfo64
.si_code
= FPE_INTOVF
;
472 else if (ut
->uu_subcode
& (1 << FP_ZE
)) {
473 sinfo64
.si_code
= FPE_FLTDIV
;
474 } else if (ut
->uu_subcode
& (1 << FP_OE
)) {
475 sinfo64
.si_code
= FPE_FLTOVF
;
476 } else if (ut
->uu_subcode
& (1 << FP_UE
)) {
477 sinfo64
.si_code
= FPE_FLTUND
;
478 } else if (ut
->uu_subcode
& (1 << FP_PE
)) {
479 sinfo64
.si_code
= FPE_FLTRES
;
480 } else if (ut
->uu_subcode
& (1 << FP_IE
)) {
481 sinfo64
.si_code
= FPE_FLTINV
;
483 sinfo64
.si_code
= FPE_NOOP
;
487 sinfo64
.si_code
= BUS_ADRERR
;
488 sinfo64
.si_addr
= ua_cr2
;
491 sinfo64
.si_code
= TRAP_BRKPT
;
494 sinfo64
.si_addr
= ua_cr2
;
496 switch (ut
->uu_code
) {
498 /* CR2 is meaningless after GP fault */
499 /* XXX namespace clash! */
500 sinfo64
.si_addr
= 0ULL;
503 case KERN_PROTECTION_FAILURE
:
504 sinfo64
.si_code
= SEGV_ACCERR
;
506 case KERN_INVALID_ADDRESS
:
507 sinfo64
.si_code
= SEGV_MAPERR
;
510 sinfo64
.si_code
= FPE_NOOP
;
515 int status_and_exitcode
;
518 * All other signals need to fill out a minimum set of
519 * information for the siginfo structure passed into
520 * the signal handler, if SA_SIGINFO was specified.
522 * p->si_status actually contains both the status and
523 * the exit code; we save it off in its own variable
524 * for later breakdown.
527 sinfo64
.si_pid
= p
->si_pid
;
529 status_and_exitcode
= p
->si_status
;
531 sinfo64
.si_uid
= p
->si_uid
;
533 sinfo64
.si_code
= p
->si_code
;
536 if (sinfo64
.si_code
== CLD_EXITED
) {
537 if (WIFEXITED(status_and_exitcode
))
538 sinfo64
.si_code
= CLD_EXITED
;
539 else if (WIFSIGNALED(status_and_exitcode
)) {
540 if (WCOREDUMP(status_and_exitcode
)) {
541 sinfo64
.si_code
= CLD_DUMPED
;
542 status_and_exitcode
= W_EXITCODE(status_and_exitcode
,status_and_exitcode
);
544 sinfo64
.si_code
= CLD_KILLED
;
545 status_and_exitcode
= W_EXITCODE(status_and_exitcode
,status_and_exitcode
);
550 * The recorded status contains the exit code and the
551 * signal information, but the information to be passed
552 * in the siginfo to the handler is supposed to only
553 * contain the status, so we have to shift it out.
555 sinfo64
.si_status
= (WEXITSTATUS(status_and_exitcode
) & 0x00FFFFFF) | (((uint32_t)(p
->p_xhighbits
) << 24) & 0xFF000000);
560 if (proc_is64bit(p
)) {
561 user64_siginfo_t sinfo64_user64
;
563 bzero((caddr_t
)&sinfo64_user64
, sizeof(sinfo64_user64
));
565 siginfo_user_to_user64_x86(&sinfo64
,&sinfo64_user64
);
568 bzero((caddr_t
)&(ut
->t_dtrace_siginfo
), sizeof(ut
->t_dtrace_siginfo
));
570 ut
->t_dtrace_siginfo
.si_signo
= sinfo64
.si_signo
;
571 ut
->t_dtrace_siginfo
.si_code
= sinfo64
.si_code
;
572 ut
->t_dtrace_siginfo
.si_pid
= sinfo64
.si_pid
;
573 ut
->t_dtrace_siginfo
.si_uid
= sinfo64
.si_uid
;
574 ut
->t_dtrace_siginfo
.si_status
= sinfo64
.si_status
;
575 /* XXX truncates faulting address to void * on K32 */
576 ut
->t_dtrace_siginfo
.si_addr
= CAST_DOWN(void *, sinfo64
.si_addr
);
578 /* Fire DTrace proc:::fault probe when signal is generated by hardware. */
580 case SIGILL
: case SIGBUS
: case SIGSEGV
: case SIGFPE
: case SIGTRAP
:
581 DTRACE_PROC2(fault
, int, (int)(ut
->uu_code
), siginfo_t
*, &(ut
->t_dtrace_siginfo
));
587 /* XXX truncates catcher address to uintptr_t */
588 DTRACE_PROC3(signal__handle
, int, sig
, siginfo_t
*, &(ut
->t_dtrace_siginfo
),
589 void (*)(void), CAST_DOWN(sig_t
, ua_catcher
));
590 #endif /* CONFIG_DTRACE */
592 if (copyout((caddr_t
)&sinfo64_user64
, ua_sip
, sizeof (sinfo64_user64
)))
595 flavor
= x86_THREAD_STATE64
;
596 state_count
= x86_THREAD_STATE64_COUNT
;
597 state
= (void *)&mctxp
->mctx_avx64
.ss
;
599 x86_thread_state32_t
*tstate32
;
600 user32_siginfo_t sinfo32
;
602 bzero((caddr_t
)&sinfo32
, sizeof(sinfo32
));
604 siginfo_user_to_user32_x86(&sinfo64
,&sinfo32
);
607 bzero((caddr_t
)&(ut
->t_dtrace_siginfo
), sizeof(ut
->t_dtrace_siginfo
));
609 ut
->t_dtrace_siginfo
.si_signo
= sinfo32
.si_signo
;
610 ut
->t_dtrace_siginfo
.si_code
= sinfo32
.si_code
;
611 ut
->t_dtrace_siginfo
.si_pid
= sinfo32
.si_pid
;
612 ut
->t_dtrace_siginfo
.si_uid
= sinfo32
.si_uid
;
613 ut
->t_dtrace_siginfo
.si_status
= sinfo32
.si_status
;
614 ut
->t_dtrace_siginfo
.si_addr
= CAST_DOWN(void *, sinfo32
.si_addr
);
616 /* Fire DTrace proc:::fault probe when signal is generated by hardware. */
618 case SIGILL
: case SIGBUS
: case SIGSEGV
: case SIGFPE
: case SIGTRAP
:
619 DTRACE_PROC2(fault
, int, (int)(ut
->uu_code
), siginfo_t
*, &(ut
->t_dtrace_siginfo
));
625 DTRACE_PROC3(signal__handle
, int, sig
, siginfo_t
*, &(ut
->t_dtrace_siginfo
),
626 void (*)(void), CAST_DOWN(sig_t
, ua_catcher
));
627 #endif /* CONFIG_DTRACE */
629 if (copyout((caddr_t
)&sinfo32
, ua_sip
, sizeof (sinfo32
)))
632 tstate32
= &mctxp
->mctx_avx32
.ss
;
634 tstate32
->eip
= CAST_DOWN_EXPLICIT(user32_addr_t
, trampact
);
635 tstate32
->esp
= CAST_DOWN_EXPLICIT(user32_addr_t
, ua_fp
);
637 tstate32
->eflags
= get_eflags_exportmask();
639 tstate32
->cs
= USER_CS
;
640 tstate32
->ss
= USER_DS
;
641 tstate32
->ds
= USER_DS
;
642 tstate32
->es
= USER_DS
;
643 tstate32
->fs
= NULL_SEG
;
644 tstate32
->gs
= USER_CTHREAD
;
646 flavor
= x86_THREAD_STATE32
;
647 state_count
= x86_THREAD_STATE32_COUNT
;
648 state
= (void *)tstate32
;
650 if (thread_setstatus(thread
, flavor
, (thread_state_t
)state
, state_count
) != KERN_SUCCESS
)
652 ml_fp_setvalid(FALSE
);
654 /* Tell the PAL layer about the signal */
655 pal_set_signal_delivery( thread
);
664 SIGACTION(p
, SIGILL
) = SIG_DFL
;
665 sig
= sigmask(SIGILL
);
666 p
->p_sigignore
&= ~sig
;
667 p
->p_sigcatch
&= ~sig
;
668 ut
->uu_sigmask
&= ~sig
;
669 /* sendsig is called with signal lock held */
671 psignal_locked(p
, SIGILL
);
677 * System call to cleanup state after a signal
678 * has been taken. Reset signal mask and
679 * stack state from context left by sendsig (above).
680 * Return to previous pc and psl as specified by
681 * context left by sendsig. Check carefully to
682 * make sure that the user has not modified the
683 * psl to gain improper priviledges or to cause
688 sigreturn(struct proc
*p
, struct sigreturn_args
*uap
, __unused
int *retval
)
691 struct mcontext_avx32 mctx_avx32
;
692 struct mcontext_avx64 mctx_avx64
;
693 #if !defined(RC_HIDE_XNU_J137)
694 struct mcontext_avx512_32 mctx_avx512_32
;
695 struct mcontext_avx512_64 mctx_avx512_64
;
697 } mctx_store
, *mctxp
= &mctx_store
;
699 thread_t thread
= current_thread();
701 struct sigacts
*ps
= p
->p_sigacts
;
705 mach_msg_type_number_t ts_count
;
706 unsigned int ts_flavor
;
708 mach_msg_type_number_t fs_count
;
709 unsigned int fs_flavor
;
711 int rval
= EJUSTRETURN
;
713 uint32_t sigreturn_validation
;
714 user_addr_t token_uctx
;
717 ut
= (struct uthread
*)get_bsdthread_info(thread
);
720 * If we are being asked to change the altstack flag on the thread, we
721 * just set/reset it and return (the uap->uctx is not used).
723 if ((unsigned int)uap
->infostyle
== UC_SET_ALT_STACK
) {
724 ut
->uu_sigstk
.ss_flags
|= SA_ONSTACK
;
726 } else if ((unsigned int)uap
->infostyle
== UC_RESET_ALT_STACK
) {
727 ut
->uu_sigstk
.ss_flags
&= ~SA_ONSTACK
;
731 bzero(mctxp
, sizeof(*mctxp
));
733 sig_xstate
= current_xstate();
735 sigreturn_validation
= atomic_load_explicit(
736 &ps
->ps_sigreturn_validation
, memory_order_relaxed
);
737 token_uctx
= uap
->uctx
;
738 kr
= machine_thread_siguctx_pointer_convert_to_user(thread
, &token_uctx
);
739 assert(kr
== KERN_SUCCESS
);
741 if (proc_is64bit(p
)) {
742 struct user_ucontext64 uctx64
;
745 if ((error
= copyin(uap
->uctx
, (void *)&uctx64
, sizeof (uctx64
))))
748 if ((error
= copyin(uctx64
.uc_mcontext64
, (void *)mctxp
, thread_state64
[sig_xstate
].mcontext_size
)))
751 onstack
= uctx64
.uc_onstack
& 01;
752 ut
->uu_sigmask
= uctx64
.uc_sigmask
& ~sigcantmask
;
754 ts_flavor
= x86_THREAD_STATE64
;
755 ts_count
= x86_THREAD_STATE64_COUNT
;
756 ts
= (void *)&mctxp
->mctx_avx64
.ss
;
758 fs_flavor
= thread_state64
[sig_xstate
].flavor
;
759 fs_count
= thread_state64
[sig_xstate
].state_count
;
760 fs
= (void *)&mctxp
->mctx_avx64
.fs
;
762 token
= (user64_addr_t
)token_uctx
^ (user64_addr_t
)ps
->ps_sigreturn_token
;
763 if ((user64_addr_t
)uap
->token
!= token
) {
764 #if DEVELOPMENT || DEBUG
765 printf("process %s[%d] sigreturn token mismatch: received 0x%llx expected 0x%llx\n",
766 p
->p_comm
, p
->p_pid
, (user64_addr_t
)uap
->token
, token
);
767 #endif /* DEVELOPMENT || DEBUG */
768 if (sigreturn_validation
!= PS_SIGRETURN_VALIDATION_DISABLED
) {
773 struct user_ucontext32 uctx32
;
776 if ((error
= copyin(uap
->uctx
, (void *)&uctx32
, sizeof (uctx32
))))
779 if ((error
= copyin(CAST_USER_ADDR_T(uctx32
.uc_mcontext
), (void *)mctxp
, thread_state32
[sig_xstate
].mcontext_size
)))
782 onstack
= uctx32
.uc_onstack
& 01;
783 ut
->uu_sigmask
= uctx32
.uc_sigmask
& ~sigcantmask
;
785 ts_flavor
= x86_THREAD_STATE32
;
786 ts_count
= x86_THREAD_STATE32_COUNT
;
787 ts
= (void *)&mctxp
->mctx_avx32
.ss
;
789 fs_flavor
= thread_state32
[sig_xstate
].flavor
;
790 fs_count
= thread_state32
[sig_xstate
].state_count
;
791 fs
= (void *)&mctxp
->mctx_avx32
.fs
;
793 token
= CAST_DOWN_EXPLICIT(user32_addr_t
, uap
->uctx
) ^
794 CAST_DOWN_EXPLICIT(user32_addr_t
, ps
->ps_sigreturn_token
);
795 if ((user32_addr_t
)uap
->token
!= token
) {
796 #if DEVELOPMENT || DEBUG
797 printf("process %s[%d] sigreturn token mismatch: received 0x%x expected 0x%x\n",
798 p
->p_comm
, p
->p_pid
, (user32_addr_t
)uap
->token
, token
);
799 #endif /* DEVELOPMENT || DEBUG */
800 if (sigreturn_validation
!= PS_SIGRETURN_VALIDATION_DISABLED
) {
807 ut
->uu_sigstk
.ss_flags
|= SA_ONSTACK
;
809 ut
->uu_sigstk
.ss_flags
&= ~SA_ONSTACK
;
811 if (ut
->uu_siglist
& ~ut
->uu_sigmask
)
812 signal_setast(thread
);
814 if (rval
== EINVAL
) {
819 * thread_set_state() does all the needed checks for the passed in
822 if (thread_setstatus(thread
, ts_flavor
, ts
, ts_count
) != KERN_SUCCESS
) {
824 #if DEVELOPMENT || DEBUG
825 printf("process %s[%d] sigreturn thread_setstatus error %d\n",
826 p
->p_comm
, p
->p_pid
, rval
);
827 #endif /* DEVELOPMENT || DEBUG */
831 ml_fp_setvalid(TRUE
);
833 if (thread_setstatus(thread
, fs_flavor
, fs
, fs_count
) != KERN_SUCCESS
) {
835 #if DEVELOPMENT || DEBUG
836 printf("process %s[%d] sigreturn thread_setstatus error %d\n",
837 p
->p_comm
, p
->p_pid
, rval
);
838 #endif /* DEVELOPMENT || DEBUG */
848 * machine_exception() performs machine-dependent translation
849 * of a mach exception to a unix signal.
852 machine_exception(int exception
,
853 mach_exception_code_t code
,
854 __unused mach_exception_subcode_t subcode
)
858 /* Map GP fault to SIGSEGV, otherwise defer to caller */
859 if (code
== EXC_I386_GPFLT
) {
864 case EXC_BAD_INSTRUCTION
:
871 if (code
== EXC_I386_BOUND
) {
873 * Map #BR, the Bound Range Exceeded exception, to