2 * Copyright (c) 2000-2016 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
105 case x86_THREAD_STATE32
:
107 x86_thread_state32_t
*state25
;
109 state25
= (x86_thread_state32_t
*) tstate
;
112 *user_stack
= state25
->esp
;
116 *user_stack
= VM_USRSTACK32
;
123 case x86_THREAD_STATE64
:
125 x86_thread_state64_t
*state25
;
127 state25
= (x86_thread_state64_t
*) tstate
;
130 *user_stack
= state25
->rsp
;
134 *user_stack
= VM_USRSTACK64
;
142 return (KERN_INVALID_ARGUMENT
);
145 return (KERN_SUCCESS
);
149 * thread_userstackdefault:
151 * Return the default stack location for the
152 * thread, if otherwise unknown.
155 thread_userstackdefault(
156 mach_vm_offset_t
*default_user_stack
,
160 *default_user_stack
= VM_USRSTACK64
;
162 *default_user_stack
= VM_USRSTACK32
;
164 return (KERN_SUCCESS
);
169 __unused thread_t thread
,
171 thread_state_t tstate
,
172 __unused
unsigned int count
,
173 mach_vm_offset_t
*entry_point
179 if (*entry_point
== 0)
180 *entry_point
= VM_MIN_ADDRESS
;
183 case x86_THREAD_STATE32
:
185 x86_thread_state32_t
*state25
;
187 state25
= (i386_thread_state_t
*) tstate
;
188 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
192 case x86_THREAD_STATE64
:
194 x86_thread_state64_t
*state25
;
196 state25
= (x86_thread_state64_t
*) tstate
;
197 *entry_point
= state25
->rip
? state25
->rip
: VM_MIN_ADDRESS64
;
201 return (KERN_SUCCESS
);
205 * FIXME - thread_set_child
208 void thread_set_child(thread_t child
, int pid
);
210 thread_set_child(thread_t child
, int pid
)
212 pal_register_cache_state(child
, DIRTY
);
214 if (thread_is_64bit(child
)) {
215 x86_saved_state64_t
*iss64
;
217 iss64
= USER_REGS64(child
);
221 iss64
->isf
.rflags
&= ~EFL_CF
;
223 x86_saved_state32_t
*iss32
;
225 iss32
= USER_REGS32(child
);
229 iss32
->efl
&= ~EFL_CF
;
236 * System Call handling code
239 extern long fuword(vm_offset_t
);
241 __attribute__((noreturn
))
243 machdep_syscall(x86_saved_state_t
*state
)
245 int args
[machdep_call_count
];
248 const machdep_call_t
*entry
;
249 x86_saved_state32_t
*regs
;
251 assert(is_saved_state32(state
));
252 regs
= saved_state32(state
);
256 kprintf("machdep_syscall(0x%08x) code=%d\n", regs
, trapno
);
259 DEBUG_KPRINT_SYSCALL_MDEP(
260 "machdep_syscall: trapno=%d\n", trapno
);
262 if (trapno
< 0 || trapno
>= machdep_call_count
) {
263 regs
->eax
= (unsigned int)kern_invalid(NULL
);
265 thread_exception_return();
268 entry
= &machdep_call_table
[trapno
];
269 nargs
= entry
->nargs
;
272 if (copyin((user_addr_t
) regs
->uesp
+ sizeof (int),
273 (char *) args
, (nargs
* sizeof (int)))) {
274 regs
->eax
= KERN_INVALID_ADDRESS
;
276 thread_exception_return();
282 regs
->eax
= (*entry
->routine
.args_0
)();
285 regs
->eax
= (*entry
->routine
.args_1
)(args
[0]);
288 regs
->eax
= (*entry
->routine
.args_2
)(args
[0],args
[1]);
291 if (!entry
->bsd_style
)
292 regs
->eax
= (*entry
->routine
.args_3
)(args
[0],args
[1],args
[2]);
297 error
= (*entry
->routine
.args_bsd_3
)(&rval
, args
[0], args
[1], args
[2]);
300 regs
->efl
|= EFL_CF
; /* carry bit */
303 regs
->efl
&= ~EFL_CF
;
308 regs
->eax
= (*entry
->routine
.args_4
)(args
[0], args
[1], args
[2], args
[3]);
312 panic("machdep_syscall: too many args");
315 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs
->eax
);
317 #if DEBUG || DEVELOPMENT
318 kern_allocation_name_t
319 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
320 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
321 #endif /* DEBUG || DEVELOPMENT */
323 throttle_lowpri_io(1);
325 thread_exception_return();
329 __attribute__((noreturn
))
331 machdep_syscall64(x86_saved_state_t
*state
)
334 const machdep_call_t
*entry
;
335 x86_saved_state64_t
*regs
;
337 assert(is_saved_state64(state
));
338 regs
= saved_state64(state
);
340 trapno
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
342 DEBUG_KPRINT_SYSCALL_MDEP(
343 "machdep_syscall64: trapno=%d\n", trapno
);
345 if (trapno
< 0 || trapno
>= machdep_call_count
) {
346 regs
->rax
= (unsigned int)kern_invalid(NULL
);
348 thread_exception_return();
351 entry
= &machdep_call_table64
[trapno
];
353 switch (entry
->nargs
) {
355 regs
->rax
= (*entry
->routine
.args_0
)();
358 regs
->rax
= (*entry
->routine
.args64_1
)(regs
->rdi
);
361 regs
->rax
= (*entry
->routine
.args64_2
)(regs
->rdi
, regs
->rsi
);
364 panic("machdep_syscall64: too many args");
367 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs
->rax
);
369 #if DEBUG || DEVELOPMENT
370 kern_allocation_name_t
371 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
372 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
373 #endif /* DEBUG || DEVELOPMENT */
375 throttle_lowpri_io(1);
377 thread_exception_return();
381 #endif /* MACH_BSD */
384 typedef kern_return_t (*mach_call_t
)(void *);
386 struct mach_call_args
{
399 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
);
403 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
)
405 if (copyin((user_addr_t
)(sp
+ sizeof(int)), (char *)args
, trapp
->mach_trap_u32_words
* sizeof (int)))
406 return KERN_INVALID_ARGUMENT
;
407 #if CONFIG_REQUIRES_U32_MUNGING
408 trapp
->mach_trap_arg_munge32(args
);
410 #error U32 mach traps on x86_64 kernel requires munging
416 __private_extern__
void mach_call_munger(x86_saved_state_t
*state
);
418 extern const char *mach_syscall_name_table
[];
420 __attribute__((noreturn
))
422 mach_call_munger(x86_saved_state_t
*state
)
426 mach_call_t mach_call
;
427 kern_return_t retval
;
428 struct mach_call_args args
= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
429 x86_saved_state32_t
*regs
;
431 struct uthread
*ut
= get_bsdthread_info(current_thread());
432 uthread_reset_proc_refcount(ut
);
434 assert(is_saved_state32(state
));
435 regs
= saved_state32(state
);
437 call_number
= -(regs
->eax
);
439 DEBUG_KPRINT_SYSCALL_MACH(
440 "mach_call_munger: code=%d(%s)\n",
441 call_number
, mach_syscall_name_table
[call_number
]);
443 kprintf("mach_call_munger(0x%08x) code=%d\n", regs
, call_number
);
446 if (call_number
< 0 || call_number
>= mach_trap_count
) {
447 i386_exception(EXC_SYSCALL
, call_number
, 1);
450 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
452 if (mach_call
== (mach_call_t
)kern_invalid
) {
453 DEBUG_KPRINT_SYSCALL_MACH(
454 "mach_call_munger: kern_invalid 0x%x\n", regs
->eax
);
455 i386_exception(EXC_SYSCALL
, call_number
, 1);
459 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
461 retval
= mach_call_arg_munger32(regs
->uesp
, &args
, &mach_trap_table
[call_number
]);
462 if (retval
!= KERN_SUCCESS
) {
465 DEBUG_KPRINT_SYSCALL_MACH(
466 "mach_call_munger: retval=0x%x\n", retval
);
468 thread_exception_return();
474 mach_kauth_cred_uthread_update();
477 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
478 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
479 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
481 retval
= mach_call(&args
);
483 DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval
);
485 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
486 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
491 #if DEBUG || DEVELOPMENT
492 kern_allocation_name_t
493 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
494 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
495 #endif /* DEBUG || DEVELOPMENT */
497 throttle_lowpri_io(1);
500 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
501 panic("system call returned with uu_proc_refcount != 0");
505 thread_exception_return();
510 __private_extern__
void mach_call_munger64(x86_saved_state_t
*regs
);
512 __attribute__((noreturn
))
514 mach_call_munger64(x86_saved_state_t
*state
)
518 mach_call_t mach_call
;
519 struct mach_call_args args
= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
520 x86_saved_state64_t
*regs
;
522 struct uthread
*ut
= get_bsdthread_info(current_thread());
523 uthread_reset_proc_refcount(ut
);
525 assert(is_saved_state64(state
));
526 regs
= saved_state64(state
);
528 call_number
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
530 DEBUG_KPRINT_SYSCALL_MACH(
531 "mach_call_munger64: code=%d(%s)\n",
532 call_number
, mach_syscall_name_table
[call_number
]);
534 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
535 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_START
,
536 regs
->rdi
, regs
->rsi
, regs
->rdx
, regs
->r10
, 0);
538 if (call_number
< 0 || call_number
>= mach_trap_count
) {
539 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
542 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
544 if (mach_call
== (mach_call_t
)kern_invalid
) {
545 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
548 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
550 int args_in_regs
= MIN(6, argc
);
552 memcpy(&args
.arg1
, ®s
->rdi
, args_in_regs
* sizeof(syscall_arg_t
));
558 copyin_count
= (argc
- 6) * (int)sizeof(syscall_arg_t
);
560 if (copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)&args
.arg7
, copyin_count
)) {
561 regs
->rax
= KERN_INVALID_ARGUMENT
;
563 thread_exception_return();
570 mach_kauth_cred_uthread_update();
573 regs
->rax
= (uint64_t)mach_call((void *)&args
);
575 DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs
->rax
);
577 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
578 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
579 regs
->rax
, 0, 0, 0, 0);
581 #if DEBUG || DEVELOPMENT
582 kern_allocation_name_t
583 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
584 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
585 #endif /* DEBUG || DEVELOPMENT */
587 throttle_lowpri_io(1);
590 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
591 panic("system call returned with uu_proc_refcount != 0");
595 thread_exception_return();
601 * thread_setuserstack:
603 * Sets the user stack pointer into the machine
604 * dependent thread state info.
609 mach_vm_address_t user_stack
)
611 pal_register_cache_state(thread
, DIRTY
);
612 if (thread_is_64bit(thread
)) {
613 x86_saved_state64_t
*iss64
;
615 iss64
= USER_REGS64(thread
);
617 iss64
->isf
.rsp
= (uint64_t)user_stack
;
619 x86_saved_state32_t
*iss32
;
621 iss32
= USER_REGS32(thread
);
623 iss32
->uesp
= CAST_DOWN_EXPLICIT(unsigned int, user_stack
);
628 * thread_adjuserstack:
630 * Returns the adjusted user stack pointer from the machine
631 * dependent thread state info. Used for small (<2G) deltas.
638 pal_register_cache_state(thread
, DIRTY
);
639 if (thread_is_64bit(thread
)) {
640 x86_saved_state64_t
*iss64
;
642 iss64
= USER_REGS64(thread
);
644 iss64
->isf
.rsp
+= adjust
;
646 return iss64
->isf
.rsp
;
648 x86_saved_state32_t
*iss32
;
650 iss32
= USER_REGS32(thread
);
652 iss32
->uesp
+= adjust
;
654 return CAST_USER_ADDR_T(iss32
->uesp
);
659 * thread_setentrypoint:
661 * Sets the user PC into the machine
662 * dependent thread state info.
665 thread_setentrypoint(thread_t thread
, mach_vm_address_t entry
)
667 pal_register_cache_state(thread
, DIRTY
);
668 if (thread_is_64bit(thread
)) {
669 x86_saved_state64_t
*iss64
;
671 iss64
= USER_REGS64(thread
);
673 iss64
->isf
.rip
= (uint64_t)entry
;
675 x86_saved_state32_t
*iss32
;
677 iss32
= USER_REGS32(thread
);
679 iss32
->eip
= CAST_DOWN_EXPLICIT(unsigned int, entry
);
685 thread_setsinglestep(thread_t thread
, int on
)
687 pal_register_cache_state(thread
, DIRTY
);
688 if (thread_is_64bit(thread
)) {
689 x86_saved_state64_t
*iss64
;
691 iss64
= USER_REGS64(thread
);
694 iss64
->isf
.rflags
|= EFL_TF
;
696 iss64
->isf
.rflags
&= ~EFL_TF
;
698 x86_saved_state32_t
*iss32
;
700 iss32
= USER_REGS32(thread
);
703 iss32
->efl
|= EFL_TF
;
705 if (iss32
->cs
== SYSENTER_CS
)
706 iss32
->cs
= SYSENTER_TF_CS
;
709 iss32
->efl
&= ~EFL_TF
;
712 return (KERN_SUCCESS
);
716 get_user_regs(thread_t th
)
718 pal_register_cache_state(th
, DIRTY
);
719 return(USER_STATE(th
));
723 find_user_regs(thread_t thread
)
725 return get_user_regs(thread
);
730 * DTrace would like to have a peek at the kernel interrupt state, if available.
732 x86_saved_state_t
*find_kern_regs(thread_t
);
735 find_kern_regs(thread_t thread
)
737 if (thread
== current_thread() &&
738 NULL
!= current_cpu_datap()->cpu_int_state
&&
739 !(USER_STATE(thread
) == current_cpu_datap()->cpu_int_state
&&
740 current_cpu_datap()->cpu_interrupt_level
== 1)) {
742 return current_cpu_datap()->cpu_int_state
;
748 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
751 dtrace_get_cpu_int_stack_top(void)
753 return current_cpu_datap()->cpu_int_stack_top
;