2 * Copyright (c) 2000-2019 Apple 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 #include <mach_debug.h>
30 #include <mach_ldebug.h>
32 #include <mach/kern_return.h>
33 #include <mach/mach_traps.h>
34 #include <mach/thread_status.h>
35 #include <mach/vm_param.h>
37 #include <kern/counters.h>
38 #include <kern/cpu_data.h>
39 #include <kern/mach_param.h>
40 #include <kern/task.h>
41 #include <kern/thread.h>
42 #include <kern/sched_prim.h>
43 #include <kern/misc_protos.h>
44 #include <kern/assert.h>
45 #include <kern/debug.h>
47 #include <kern/syscall_sw.h>
48 #include <ipc/ipc_port.h>
49 #include <vm/vm_kern.h>
52 #include <i386/cpu_number.h>
53 #include <i386/eflags.h>
54 #include <i386/proc_reg.h>
56 #include <i386/user_ldt.h>
58 #include <i386/machdep_call.h>
59 #include <i386/vmparam.h>
60 #include <i386/mp_desc.h>
61 #include <i386/misc_protos.h>
62 #include <i386/thread.h>
63 #include <i386/trap.h>
65 #include <mach/i386/syscall_sw.h>
66 #include <sys/syscall.h>
67 #include <sys/kdebug.h>
68 #include <sys/errno.h>
69 #include <../bsd/sys/sysent.h>
72 extern void mach_kauth_cred_uthread_update(void);
73 extern void throttle_lowpri_io(int);
76 void * find_user_regs(thread_t
);
78 unsigned int get_msr_exportmask(void);
80 unsigned int get_msr_nbits(void);
82 unsigned int get_msr_rbits(void);
87 * Return the user stack pointer from the machine
88 * dependent thread state info.
92 __unused thread_t thread
,
94 thread_state_t tstate
,
95 __unused
unsigned int count
,
96 mach_vm_offset_t
*user_stack
,
98 __unused boolean_t is64bit
106 case x86_THREAD_STATE32
:
108 x86_thread_state32_t
*state25
;
110 state25
= (x86_thread_state32_t
*) tstate
;
113 *user_stack
= state25
->esp
;
118 *user_stack
= VM_USRSTACK32
;
126 case x86_THREAD_FULL_STATE64
:
128 case x86_THREAD_STATE64
:
130 x86_thread_state64_t
*state25
;
132 state25
= (x86_thread_state64_t
*) tstate
;
135 *user_stack
= state25
->rsp
;
140 *user_stack
= VM_USRSTACK64
;
149 return KERN_INVALID_ARGUMENT
;
156 * thread_userstackdefault:
158 * Return the default stack location for the
159 * thread, if otherwise unknown.
162 thread_userstackdefault(
163 mach_vm_offset_t
*default_user_stack
,
167 *default_user_stack
= VM_USRSTACK64
;
169 *default_user_stack
= VM_USRSTACK32
;
176 __unused thread_t thread
,
178 thread_state_t tstate
,
179 __unused
unsigned int count
,
180 mach_vm_offset_t
*entry_point
186 if (*entry_point
== 0) {
187 *entry_point
= VM_MIN_ADDRESS
;
191 case x86_THREAD_STATE32
:
193 x86_thread_state32_t
*state25
;
195 state25
= (i386_thread_state_t
*) tstate
;
196 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
200 case x86_THREAD_STATE64
:
202 x86_thread_state64_t
*state25
;
204 state25
= (x86_thread_state64_t
*) tstate
;
205 *entry_point
= state25
->rip
? state25
->rip
: VM_MIN_ADDRESS64
;
213 * FIXME - thread_set_child
216 void thread_set_child(thread_t child
, int pid
);
218 thread_set_child(thread_t child
, int pid
)
220 pal_register_cache_state(child
, DIRTY
);
222 if (thread_is_64bit_addr(child
)) {
223 x86_saved_state64_t
*iss64
;
225 iss64
= USER_REGS64(child
);
229 iss64
->isf
.rflags
&= ~EFL_CF
;
231 x86_saved_state32_t
*iss32
;
233 iss32
= USER_REGS32(child
);
237 iss32
->efl
&= ~EFL_CF
;
244 * System Call handling code
247 extern long fuword(vm_offset_t
);
249 __attribute__((noreturn
))
251 machdep_syscall(x86_saved_state_t
*state
)
253 int args
[machdep_call_count
];
256 const machdep_call_t
*entry
;
257 x86_saved_state32_t
*regs
;
259 assert(is_saved_state32(state
));
260 regs
= saved_state32(state
);
264 kprintf("machdep_syscall(0x%08x) code=%d\n", regs
, trapno
);
267 DEBUG_KPRINT_SYSCALL_MDEP(
268 "machdep_syscall: trapno=%d\n", trapno
);
270 if (trapno
< 0 || trapno
>= machdep_call_count
) {
271 regs
->eax
= (unsigned int)kern_invalid(NULL
);
273 thread_exception_return();
276 entry
= &machdep_call_table
[trapno
];
277 nargs
= entry
->nargs
;
280 if (copyin((user_addr_t
) regs
->uesp
+ sizeof(int),
281 (char *) args
, (nargs
* sizeof(int)))) {
282 regs
->eax
= KERN_INVALID_ADDRESS
;
284 thread_exception_return();
290 regs
->eax
= (*entry
->routine
.args_0
)();
293 regs
->eax
= (*entry
->routine
.args_1
)(args
[0]);
296 regs
->eax
= (*entry
->routine
.args_2
)(args
[0], args
[1]);
299 if (!entry
->bsd_style
) {
300 regs
->eax
= (*entry
->routine
.args_3
)(args
[0], args
[1], args
[2]);
305 error
= (*entry
->routine
.args_bsd_3
)(&rval
, args
[0], args
[1], args
[2]);
308 regs
->efl
|= EFL_CF
; /* carry bit */
311 regs
->efl
&= ~EFL_CF
;
316 regs
->eax
= (*entry
->routine
.args_4
)(args
[0], args
[1], args
[2], args
[3]);
320 panic("machdep_syscall: too many args");
323 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs
->eax
);
325 #if DEBUG || DEVELOPMENT
326 kern_allocation_name_t
327 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
328 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
329 #endif /* DEBUG || DEVELOPMENT */
331 throttle_lowpri_io(1);
333 thread_exception_return();
337 __attribute__((noreturn
))
339 machdep_syscall64(x86_saved_state_t
*state
)
342 const machdep_call_t
*entry
;
343 x86_saved_state64_t
*regs
;
345 assert(is_saved_state64(state
));
346 regs
= saved_state64(state
);
348 trapno
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
350 DEBUG_KPRINT_SYSCALL_MDEP(
351 "machdep_syscall64: trapno=%d\n", trapno
);
353 if (trapno
< 0 || trapno
>= machdep_call_count
) {
354 regs
->rax
= (unsigned int)kern_invalid(NULL
);
356 thread_exception_return();
359 entry
= &machdep_call_table64
[trapno
];
361 switch (entry
->nargs
) {
363 regs
->rax
= (*entry
->routine
.args_0
)();
366 regs
->rax
= (*entry
->routine
.args64_1
)(regs
->rdi
);
369 regs
->rax
= (*entry
->routine
.args64_2
)(regs
->rdi
, regs
->rsi
);
372 if (!entry
->bsd_style
) {
373 regs
->rax
= (*entry
->routine
.args64_3
)(regs
->rdi
, regs
->rsi
, regs
->rdx
);
378 error
= (*entry
->routine
.args64_bsd_3
)(&rval
, regs
->rdi
, regs
->rsi
, regs
->rdx
);
380 regs
->rax
= (uint64_t)error
;
381 regs
->isf
.rflags
|= EFL_CF
; /* carry bit */
384 regs
->isf
.rflags
&= ~(uint64_t)EFL_CF
;
389 panic("machdep_syscall64: too many args");
392 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs
->rax
);
394 #if DEBUG || DEVELOPMENT
395 kern_allocation_name_t
396 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
397 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
398 #endif /* DEBUG || DEVELOPMENT */
400 throttle_lowpri_io(1);
402 thread_exception_return();
406 #endif /* MACH_BSD */
409 typedef kern_return_t (*mach_call_t
)(void *);
411 struct mach_call_args
{
424 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
);
428 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
)
430 if (copyin((user_addr_t
)(sp
+ sizeof(int)), (char *)args
, trapp
->mach_trap_u32_words
* sizeof(int))) {
431 return KERN_INVALID_ARGUMENT
;
433 #if CONFIG_REQUIRES_U32_MUNGING
434 trapp
->mach_trap_arg_munge32(args
);
436 #error U32 mach traps on x86_64 kernel requires munging
442 __private_extern__
void mach_call_munger(x86_saved_state_t
*state
);
444 extern const char *mach_syscall_name_table
[];
446 __attribute__((noreturn
))
448 mach_call_munger(x86_saved_state_t
*state
)
452 mach_call_t mach_call
;
453 kern_return_t retval
;
454 struct mach_call_args args
= {
465 x86_saved_state32_t
*regs
;
467 struct uthread
*ut
= get_bsdthread_info(current_thread());
468 uthread_reset_proc_refcount(ut
);
470 assert(is_saved_state32(state
));
471 regs
= saved_state32(state
);
473 call_number
= -(regs
->eax
);
475 DEBUG_KPRINT_SYSCALL_MACH(
476 "mach_call_munger: code=%d(%s)\n",
477 call_number
, mach_syscall_name_table
[call_number
]);
479 kprintf("mach_call_munger(0x%08x) code=%d\n", regs
, call_number
);
482 if (call_number
< 0 || call_number
>= mach_trap_count
) {
483 i386_exception(EXC_SYSCALL
, call_number
, 1);
486 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
488 if (mach_call
== (mach_call_t
)kern_invalid
) {
489 DEBUG_KPRINT_SYSCALL_MACH(
490 "mach_call_munger: kern_invalid 0x%x\n", regs
->eax
);
491 i386_exception(EXC_SYSCALL
, call_number
, 1);
495 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
497 retval
= mach_call_arg_munger32(regs
->uesp
, &args
, &mach_trap_table
[call_number
]);
498 if (retval
!= KERN_SUCCESS
) {
501 DEBUG_KPRINT_SYSCALL_MACH(
502 "mach_call_munger: retval=0x%x\n", retval
);
504 thread_exception_return();
510 mach_kauth_cred_uthread_update();
513 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
514 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
515 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
517 retval
= mach_call(&args
);
519 DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval
);
521 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
522 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_END
,
527 #if DEBUG || DEVELOPMENT
528 kern_allocation_name_t
529 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
530 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
531 #endif /* DEBUG || DEVELOPMENT */
533 throttle_lowpri_io(1);
536 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
537 panic("system call returned with uu_proc_refcount != 0");
541 thread_exception_return();
546 __private_extern__
void mach_call_munger64(x86_saved_state_t
*regs
);
548 __attribute__((noreturn
))
550 mach_call_munger64(x86_saved_state_t
*state
)
554 mach_call_t mach_call
;
555 struct mach_call_args args
= {
566 x86_saved_state64_t
*regs
;
568 struct uthread
*ut
= get_bsdthread_info(current_thread());
569 uthread_reset_proc_refcount(ut
);
571 assert(is_saved_state64(state
));
572 regs
= saved_state64(state
);
574 call_number
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
576 DEBUG_KPRINT_SYSCALL_MACH(
577 "mach_call_munger64: code=%d(%s)\n",
578 call_number
, mach_syscall_name_table
[call_number
]);
580 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
581 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
582 regs
->rdi
, regs
->rsi
, regs
->rdx
, regs
->r10
, 0);
584 if (call_number
< 0 || call_number
>= mach_trap_count
) {
585 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
588 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
590 if (mach_call
== (mach_call_t
)kern_invalid
) {
591 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
594 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
596 int args_in_regs
= MIN(6, argc
);
597 __nochk_memcpy(&args
.arg1
, ®s
->rdi
, args_in_regs
* sizeof(syscall_arg_t
));
603 copyin_count
= (argc
- 6) * (int)sizeof(syscall_arg_t
);
605 if (copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)&args
.arg7
, copyin_count
)) {
606 regs
->rax
= KERN_INVALID_ARGUMENT
;
608 thread_exception_return();
615 mach_kauth_cred_uthread_update();
618 regs
->rax
= (uint64_t)mach_call((void *)&args
);
620 DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs
->rax
);
622 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
623 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_END
,
624 regs
->rax
, 0, 0, 0, 0);
626 #if DEBUG || DEVELOPMENT
627 kern_allocation_name_t
628 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
629 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
630 #endif /* DEBUG || DEVELOPMENT */
632 throttle_lowpri_io(1);
635 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
636 panic("system call returned with uu_proc_refcount != 0");
640 thread_exception_return();
646 * thread_setuserstack:
648 * Sets the user stack pointer into the machine
649 * dependent thread state info.
654 mach_vm_address_t user_stack
)
656 pal_register_cache_state(thread
, DIRTY
);
657 if (thread_is_64bit_addr(thread
)) {
658 x86_saved_state64_t
*iss64
;
660 iss64
= USER_REGS64(thread
);
662 iss64
->isf
.rsp
= (uint64_t)user_stack
;
664 x86_saved_state32_t
*iss32
;
666 iss32
= USER_REGS32(thread
);
668 iss32
->uesp
= CAST_DOWN_EXPLICIT(unsigned int, user_stack
);
673 * thread_adjuserstack:
675 * Returns the adjusted user stack pointer from the machine
676 * dependent thread state info. Used for small (<2G) deltas.
683 pal_register_cache_state(thread
, DIRTY
);
684 if (thread_is_64bit_addr(thread
)) {
685 x86_saved_state64_t
*iss64
;
687 iss64
= USER_REGS64(thread
);
689 iss64
->isf
.rsp
+= adjust
;
691 return iss64
->isf
.rsp
;
693 x86_saved_state32_t
*iss32
;
695 iss32
= USER_REGS32(thread
);
697 iss32
->uesp
+= adjust
;
699 return CAST_USER_ADDR_T(iss32
->uesp
);
704 * thread_setentrypoint:
706 * Sets the user PC into the machine
707 * dependent thread state info.
710 thread_setentrypoint(thread_t thread
, mach_vm_address_t entry
)
712 pal_register_cache_state(thread
, DIRTY
);
713 if (thread_is_64bit_addr(thread
)) {
714 x86_saved_state64_t
*iss64
;
716 iss64
= USER_REGS64(thread
);
718 iss64
->isf
.rip
= (uint64_t)entry
;
720 x86_saved_state32_t
*iss32
;
722 iss32
= USER_REGS32(thread
);
724 iss32
->eip
= CAST_DOWN_EXPLICIT(unsigned int, entry
);
730 thread_setsinglestep(thread_t thread
, int on
)
732 pal_register_cache_state(thread
, DIRTY
);
733 if (thread_is_64bit_addr(thread
)) {
734 x86_saved_state64_t
*iss64
;
736 iss64
= USER_REGS64(thread
);
739 iss64
->isf
.rflags
|= EFL_TF
;
741 iss64
->isf
.rflags
&= ~EFL_TF
;
744 x86_saved_state32_t
*iss32
;
746 iss32
= USER_REGS32(thread
);
749 iss32
->efl
|= EFL_TF
;
751 if (iss32
->cs
== SYSENTER_CS
) {
752 iss32
->cs
= SYSENTER_TF_CS
;
755 iss32
->efl
&= ~EFL_TF
;
763 get_user_regs(thread_t th
)
765 pal_register_cache_state(th
, DIRTY
);
766 return USER_STATE(th
);
770 find_user_regs(thread_t thread
)
772 return get_user_regs(thread
);
777 * DTrace would like to have a peek at the kernel interrupt state, if available.
779 x86_saved_state_t
*find_kern_regs(thread_t
);
782 find_kern_regs(thread_t thread
)
784 if (thread
== current_thread() &&
785 NULL
!= current_cpu_datap()->cpu_int_state
&&
786 !(USER_STATE(thread
) == current_cpu_datap()->cpu_int_state
&&
787 current_cpu_datap()->cpu_interrupt_level
== 1)) {
788 return current_cpu_datap()->cpu_int_state
;
794 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
797 dtrace_get_cpu_int_stack_top(void)
799 return current_cpu_datap()->cpu_int_stack_top
;