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/cpu_data.h>
38 #include <kern/mach_param.h>
39 #include <kern/task.h>
40 #include <kern/thread.h>
41 #include <kern/sched_prim.h>
42 #include <kern/misc_protos.h>
43 #include <kern/assert.h>
44 #include <kern/debug.h>
46 #include <kern/syscall_sw.h>
47 #include <ipc/ipc_port.h>
48 #include <vm/vm_kern.h>
51 #include <i386/cpu_number.h>
52 #include <i386/eflags.h>
53 #include <i386/proc_reg.h>
55 #include <i386/user_ldt.h>
57 #include <i386/machdep_call.h>
58 #include <i386/vmparam.h>
59 #include <i386/mp_desc.h>
60 #include <i386/misc_protos.h>
61 #include <i386/thread.h>
62 #include <i386/trap.h>
64 #include <mach/i386/syscall_sw.h>
65 #include <sys/syscall.h>
66 #include <sys/kdebug.h>
67 #include <sys/errno.h>
68 #include <../bsd/sys/sysent.h>
71 extern void mach_kauth_cred_uthread_update(void);
72 extern void throttle_lowpri_io(int);
76 #include <security/mac_mach_internal.h>
79 void * find_user_regs(thread_t
);
81 unsigned int get_msr_exportmask(void);
83 unsigned int get_msr_nbits(void);
85 unsigned int get_msr_rbits(void);
90 * Return the user stack pointer from the machine
91 * dependent thread state info.
95 __unused thread_t thread
,
97 thread_state_t tstate
,
99 mach_vm_offset_t
*user_stack
,
101 __unused boolean_t is64bit
109 case x86_THREAD_STATE32
:
111 x86_thread_state32_t
*state25
;
113 if (__improbable(count
!= x86_THREAD_STATE32_COUNT
)) {
114 return KERN_INVALID_ARGUMENT
;
117 state25
= (x86_thread_state32_t
*) tstate
;
120 *user_stack
= state25
->esp
;
125 *user_stack
= VM_USRSTACK32
;
133 case x86_THREAD_FULL_STATE64
:
135 x86_thread_full_state64_t
*state25
;
137 if (__improbable(count
!= x86_THREAD_FULL_STATE64_COUNT
)) {
138 return KERN_INVALID_ARGUMENT
;
141 state25
= (x86_thread_full_state64_t
*) tstate
;
143 if (state25
->ss64
.rsp
) {
144 *user_stack
= state25
->ss64
.rsp
;
149 *user_stack
= VM_USRSTACK64
;
157 case x86_THREAD_STATE64
:
159 x86_thread_state64_t
*state25
;
161 if (__improbable(count
!= x86_THREAD_STATE64_COUNT
)) {
162 return KERN_INVALID_ARGUMENT
;
165 state25
= (x86_thread_state64_t
*) tstate
;
168 *user_stack
= state25
->rsp
;
173 *user_stack
= VM_USRSTACK64
;
182 return KERN_INVALID_ARGUMENT
;
189 * thread_userstackdefault:
191 * Return the default stack location for the
192 * thread, if otherwise unknown.
195 thread_userstackdefault(
196 mach_vm_offset_t
*default_user_stack
,
200 *default_user_stack
= VM_USRSTACK64
;
202 *default_user_stack
= VM_USRSTACK32
;
209 __unused thread_t thread
,
211 thread_state_t tstate
,
213 mach_vm_offset_t
*entry_point
219 if (*entry_point
== 0) {
220 *entry_point
= VM_MIN_ADDRESS
;
224 case x86_THREAD_STATE32
:
226 x86_thread_state32_t
*state25
;
228 if (count
!= x86_THREAD_STATE32_COUNT
) {
229 return KERN_INVALID_ARGUMENT
;
232 state25
= (i386_thread_state_t
*) tstate
;
233 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
237 case x86_THREAD_STATE64
:
239 x86_thread_state64_t
*state25
;
241 if (count
!= x86_THREAD_STATE64_COUNT
) {
242 return KERN_INVALID_ARGUMENT
;
245 state25
= (x86_thread_state64_t
*) tstate
;
246 *entry_point
= state25
->rip
? state25
->rip
: VM_MIN_ADDRESS64
;
254 * FIXME - thread_set_child
257 void thread_set_child(thread_t child
, int pid
);
259 thread_set_child(thread_t child
, int pid
)
261 pal_register_cache_state(child
, DIRTY
);
263 if (thread_is_64bit_addr(child
)) {
264 x86_saved_state64_t
*iss64
;
266 iss64
= USER_REGS64(child
);
270 iss64
->isf
.rflags
&= ~EFL_CF
;
272 x86_saved_state32_t
*iss32
;
274 iss32
= USER_REGS32(child
);
278 iss32
->efl
&= ~EFL_CF
;
285 * System Call handling code
288 extern long fuword(vm_offset_t
);
290 __attribute__((noreturn
))
292 machdep_syscall(x86_saved_state_t
*state
)
294 int args
[machdep_call_count
];
297 const machdep_call_t
*entry
;
298 x86_saved_state32_t
*regs
;
300 assert(is_saved_state32(state
));
301 regs
= saved_state32(state
);
305 kprintf("machdep_syscall(0x%08x) code=%d\n", regs
, trapno
);
308 DEBUG_KPRINT_SYSCALL_MDEP(
309 "machdep_syscall: trapno=%d\n", trapno
);
311 if (trapno
< 0 || trapno
>= machdep_call_count
) {
312 regs
->eax
= (unsigned int)kern_invalid(NULL
);
314 thread_exception_return();
317 entry
= &machdep_call_table
[trapno
];
318 nargs
= entry
->nargs
;
321 if (copyin((user_addr_t
) regs
->uesp
+ sizeof(int),
322 (char *) args
, (nargs
* sizeof(int)))) {
323 regs
->eax
= KERN_INVALID_ADDRESS
;
325 thread_exception_return();
331 regs
->eax
= (*entry
->routine
.args_0
)();
334 regs
->eax
= (*entry
->routine
.args_1
)(args
[0]);
337 regs
->eax
= (*entry
->routine
.args_2
)(args
[0], args
[1]);
340 if (!entry
->bsd_style
) {
341 regs
->eax
= (*entry
->routine
.args_3
)(args
[0], args
[1], args
[2]);
346 error
= (*entry
->routine
.args_bsd_3
)(&rval
, args
[0], args
[1], args
[2]);
349 regs
->efl
|= EFL_CF
; /* carry bit */
352 regs
->efl
&= ~EFL_CF
;
357 regs
->eax
= (*entry
->routine
.args_4
)(args
[0], args
[1], args
[2], args
[3]);
361 panic("machdep_syscall: too many args");
364 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs
->eax
);
366 #if DEBUG || DEVELOPMENT
367 kern_allocation_name_t
368 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
369 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
370 #endif /* DEBUG || DEVELOPMENT */
372 throttle_lowpri_io(1);
374 thread_exception_return();
378 __attribute__((noreturn
))
380 machdep_syscall64(x86_saved_state_t
*state
)
383 const machdep_call_t
*entry
;
384 x86_saved_state64_t
*regs
;
386 assert(is_saved_state64(state
));
387 regs
= saved_state64(state
);
389 trapno
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
391 DEBUG_KPRINT_SYSCALL_MDEP(
392 "machdep_syscall64: trapno=%d\n", trapno
);
394 if (trapno
< 0 || trapno
>= machdep_call_count
) {
395 regs
->rax
= (unsigned int)kern_invalid(NULL
);
397 thread_exception_return();
400 entry
= &machdep_call_table64
[trapno
];
402 switch (entry
->nargs
) {
404 regs
->rax
= (*entry
->routine
.args_0
)();
407 regs
->rax
= (*entry
->routine
.args64_1
)(regs
->rdi
);
410 regs
->rax
= (*entry
->routine
.args64_2
)(regs
->rdi
, regs
->rsi
);
413 if (!entry
->bsd_style
) {
414 regs
->rax
= (*entry
->routine
.args64_3
)(regs
->rdi
, regs
->rsi
, regs
->rdx
);
419 error
= (*entry
->routine
.args64_bsd_3
)(&rval
, regs
->rdi
, regs
->rsi
, regs
->rdx
);
421 regs
->rax
= (uint64_t)error
;
422 regs
->isf
.rflags
|= EFL_CF
; /* carry bit */
425 regs
->isf
.rflags
&= ~(uint64_t)EFL_CF
;
430 panic("machdep_syscall64: too many args");
433 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs
->rax
);
435 #if DEBUG || DEVELOPMENT
436 kern_allocation_name_t
437 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
438 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
439 #endif /* DEBUG || DEVELOPMENT */
441 throttle_lowpri_io(1);
443 thread_exception_return();
447 #endif /* MACH_BSD */
450 typedef kern_return_t (*mach_call_t
)(void *);
452 struct mach_call_args
{
465 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
);
469 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
)
471 if (copyin((user_addr_t
)(sp
+ sizeof(int)), (char *)args
, trapp
->mach_trap_u32_words
* sizeof(int))) {
472 return KERN_INVALID_ARGUMENT
;
474 #if CONFIG_REQUIRES_U32_MUNGING
475 trapp
->mach_trap_arg_munge32(args
);
477 #error U32 mach traps on x86_64 kernel requires munging
483 __private_extern__
void mach_call_munger(x86_saved_state_t
*state
);
485 extern const char *const mach_syscall_name_table
[];
487 __attribute__((noreturn
))
489 mach_call_munger(x86_saved_state_t
*state
)
493 mach_call_t mach_call
;
494 kern_return_t retval
;
495 struct mach_call_args args
= {
506 x86_saved_state32_t
*regs
;
508 struct uthread
*ut
= get_bsdthread_info(current_thread());
509 uthread_reset_proc_refcount(ut
);
511 assert(is_saved_state32(state
));
512 regs
= saved_state32(state
);
514 call_number
= -(regs
->eax
);
516 DEBUG_KPRINT_SYSCALL_MACH(
517 "mach_call_munger: code=%d(%s)\n",
518 call_number
, mach_syscall_name_table
[call_number
]);
520 kprintf("mach_call_munger(0x%08x) code=%d\n", regs
, call_number
);
523 if (call_number
< 0 || call_number
>= mach_trap_count
) {
524 i386_exception(EXC_SYSCALL
, call_number
, 1);
527 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
529 if (mach_call
== (mach_call_t
)kern_invalid
) {
530 DEBUG_KPRINT_SYSCALL_MACH(
531 "mach_call_munger: kern_invalid 0x%x\n", regs
->eax
);
532 i386_exception(EXC_SYSCALL
, call_number
, 1);
536 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
538 retval
= mach_call_arg_munger32(regs
->uesp
, &args
, &mach_trap_table
[call_number
]);
539 if (retval
!= KERN_SUCCESS
) {
542 DEBUG_KPRINT_SYSCALL_MACH(
543 "mach_call_munger: retval=0x%x\n", retval
);
545 thread_exception_return();
551 mach_kauth_cred_uthread_update();
554 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
555 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
556 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
559 /* Check mach trap filter mask, if exists. */
560 task_t task
= current_task();
561 uint8_t *filter_mask
= task
->mach_trap_filter_mask
;
563 if (__improbable(filter_mask
!= NULL
&&
564 !bitstr_test(filter_mask
, call_number
))) {
565 /* Not in filter mask, evaluate policy. */
566 if (mac_task_mach_trap_evaluate
!= NULL
) {
567 retval
= mac_task_mach_trap_evaluate(get_bsdtask_info(task
),
574 #endif /* CONFIG_MACF */
576 retval
= mach_call(&args
);
582 DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval
);
584 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
585 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_END
,
590 #if DEBUG || DEVELOPMENT
591 kern_allocation_name_t
592 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
593 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
594 #endif /* DEBUG || DEVELOPMENT */
596 throttle_lowpri_io(1);
599 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
600 panic("system call returned with uu_proc_refcount != 0");
604 thread_exception_return();
609 __private_extern__
void mach_call_munger64(x86_saved_state_t
*regs
);
611 __attribute__((noreturn
))
613 mach_call_munger64(x86_saved_state_t
*state
)
617 mach_call_t mach_call
;
618 struct mach_call_args args
= {
629 x86_saved_state64_t
*regs
;
631 struct uthread
*ut
= get_bsdthread_info(current_thread());
632 uthread_reset_proc_refcount(ut
);
634 assert(is_saved_state64(state
));
635 regs
= saved_state64(state
);
637 call_number
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
639 DEBUG_KPRINT_SYSCALL_MACH(
640 "mach_call_munger64: code=%d(%s)\n",
641 call_number
, mach_syscall_name_table
[call_number
]);
643 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
644 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
645 regs
->rdi
, regs
->rsi
, regs
->rdx
, regs
->r10
, 0);
647 if (call_number
< 0 || call_number
>= mach_trap_count
) {
648 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
651 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
653 if (mach_call
== (mach_call_t
)kern_invalid
) {
654 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
657 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
659 int args_in_regs
= MIN(6, argc
);
660 __nochk_memcpy(&args
.arg1
, ®s
->rdi
, args_in_regs
* sizeof(syscall_arg_t
));
666 copyin_count
= (argc
- 6) * (int)sizeof(syscall_arg_t
);
668 if (copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)&args
.arg7
, copyin_count
)) {
669 regs
->rax
= KERN_INVALID_ARGUMENT
;
671 thread_exception_return();
678 mach_kauth_cred_uthread_update();
682 /* Check syscall filter mask, if exists. */
683 task_t task
= current_task();
684 uint8_t *filter_mask
= task
->mach_trap_filter_mask
;
686 if (__improbable(filter_mask
!= NULL
&&
687 !bitstr_test(filter_mask
, call_number
))) {
688 /* Not in filter mask, evaluate policy. */
689 if (mac_task_mach_trap_evaluate
!= NULL
) {
690 regs
->rax
= mac_task_mach_trap_evaluate(get_bsdtask_info(task
),
697 #endif /* CONFIG_MACF */
699 regs
->rax
= (uint64_t)mach_call((void *)&args
);
705 DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs
->rax
);
707 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
708 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_END
,
709 regs
->rax
, 0, 0, 0, 0);
711 #if DEBUG || DEVELOPMENT
712 kern_allocation_name_t
713 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
714 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
715 #endif /* DEBUG || DEVELOPMENT */
717 throttle_lowpri_io(1);
720 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
721 panic("system call returned with uu_proc_refcount != 0");
725 thread_exception_return();
731 * thread_setuserstack:
733 * Sets the user stack pointer into the machine
734 * dependent thread state info.
739 mach_vm_address_t user_stack
)
741 pal_register_cache_state(thread
, DIRTY
);
742 if (thread_is_64bit_addr(thread
)) {
743 x86_saved_state64_t
*iss64
;
745 iss64
= USER_REGS64(thread
);
747 iss64
->isf
.rsp
= (uint64_t)user_stack
;
749 x86_saved_state32_t
*iss32
;
751 iss32
= USER_REGS32(thread
);
753 iss32
->uesp
= CAST_DOWN_EXPLICIT(unsigned int, user_stack
);
758 * thread_adjuserstack:
760 * Returns the adjusted user stack pointer from the machine
761 * dependent thread state info. Used for small (<2G) deltas.
768 pal_register_cache_state(thread
, DIRTY
);
769 if (thread_is_64bit_addr(thread
)) {
770 x86_saved_state64_t
*iss64
;
772 iss64
= USER_REGS64(thread
);
774 iss64
->isf
.rsp
+= adjust
;
776 return iss64
->isf
.rsp
;
778 x86_saved_state32_t
*iss32
;
780 iss32
= USER_REGS32(thread
);
782 iss32
->uesp
+= adjust
;
784 return CAST_USER_ADDR_T(iss32
->uesp
);
789 * thread_setentrypoint:
791 * Sets the user PC into the machine
792 * dependent thread state info.
795 thread_setentrypoint(thread_t thread
, mach_vm_address_t entry
)
797 pal_register_cache_state(thread
, DIRTY
);
798 if (thread_is_64bit_addr(thread
)) {
799 x86_saved_state64_t
*iss64
;
801 iss64
= USER_REGS64(thread
);
803 iss64
->isf
.rip
= (uint64_t)entry
;
805 x86_saved_state32_t
*iss32
;
807 iss32
= USER_REGS32(thread
);
809 iss32
->eip
= CAST_DOWN_EXPLICIT(unsigned int, entry
);
815 thread_setsinglestep(thread_t thread
, int on
)
817 pal_register_cache_state(thread
, DIRTY
);
818 if (thread_is_64bit_addr(thread
)) {
819 x86_saved_state64_t
*iss64
;
821 iss64
= USER_REGS64(thread
);
824 iss64
->isf
.rflags
|= EFL_TF
;
826 iss64
->isf
.rflags
&= ~EFL_TF
;
829 x86_saved_state32_t
*iss32
;
831 iss32
= USER_REGS32(thread
);
834 iss32
->efl
|= EFL_TF
;
836 if (iss32
->cs
== SYSENTER_CS
) {
837 iss32
->cs
= SYSENTER_TF_CS
;
840 iss32
->efl
&= ~EFL_TF
;
848 get_user_regs(thread_t th
)
850 pal_register_cache_state(th
, DIRTY
);
851 return USER_STATE(th
);
855 find_user_regs(thread_t thread
)
857 return get_user_regs(thread
);
862 * DTrace would like to have a peek at the kernel interrupt state, if available.
864 x86_saved_state_t
*find_kern_regs(thread_t
);
867 find_kern_regs(thread_t thread
)
869 if (thread
== current_thread() &&
870 NULL
!= current_cpu_datap()->cpu_int_state
&&
871 !(USER_STATE(thread
) == current_cpu_datap()->cpu_int_state
&&
872 current_cpu_datap()->cpu_interrupt_level
== 1)) {
873 return current_cpu_datap()->cpu_int_state
;
879 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
882 dtrace_get_cpu_int_stack_top(void)
884 return current_cpu_datap()->cpu_int_stack_top
;