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@
30 #include <mach_debug.h>
31 #include <mach_ldebug.h>
33 #include <mach/kern_return.h>
34 #include <mach/mach_traps.h>
35 #include <mach/thread_status.h>
36 #include <mach/vm_param.h>
38 #include <kern/counters.h>
39 #include <kern/cpu_data.h>
40 #include <kern/mach_param.h>
41 #include <kern/task.h>
42 #include <kern/thread.h>
43 #include <kern/sched_prim.h>
44 #include <kern/misc_protos.h>
45 #include <kern/assert.h>
46 #include <kern/debug.h>
48 #include <kern/syscall_sw.h>
49 #include <ipc/ipc_port.h>
50 #include <vm/vm_kern.h>
53 #include <i386/cpu_number.h>
54 #include <i386/eflags.h>
55 #include <i386/proc_reg.h>
57 #include <i386/user_ldt.h>
59 #include <i386/machdep_call.h>
60 #include <i386/vmparam.h>
61 #include <i386/mp_desc.h>
62 #include <i386/misc_protos.h>
63 #include <i386/thread.h>
64 #include <i386/trap.h>
66 #include <mach/i386/syscall_sw.h>
67 #include <sys/syscall.h>
68 #include <sys/kdebug.h>
69 #include <sys/errno.h>
70 #include <../bsd/sys/sysent.h>
73 extern void mach_kauth_cred_uthread_update(void);
74 extern void throttle_lowpri_io(int);
77 void * find_user_regs(thread_t
);
79 unsigned int get_msr_exportmask(void);
81 unsigned int get_msr_nbits(void);
83 unsigned int get_msr_rbits(void);
88 * Return the user stack pointer from the machine
89 * dependent thread state info.
93 __unused thread_t thread
,
95 thread_state_t tstate
,
96 __unused
unsigned int count
,
97 mach_vm_offset_t
*user_stack
,
99 __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
;
117 *user_stack
= VM_USRSTACK32
;
124 case x86_THREAD_STATE64
:
126 x86_thread_state64_t
*state25
;
128 state25
= (x86_thread_state64_t
*) tstate
;
131 *user_stack
= state25
->rsp
;
135 *user_stack
= VM_USRSTACK64
;
143 return (KERN_INVALID_ARGUMENT
);
146 return (KERN_SUCCESS
);
150 * thread_userstackdefault:
152 * Return the default stack location for the
153 * thread, if otherwise unknown.
156 thread_userstackdefault(
157 mach_vm_offset_t
*default_user_stack
,
161 *default_user_stack
= VM_USRSTACK64
;
163 *default_user_stack
= VM_USRSTACK32
;
165 return (KERN_SUCCESS
);
170 __unused thread_t thread
,
172 thread_state_t tstate
,
173 __unused
unsigned int count
,
174 mach_vm_offset_t
*entry_point
180 if (*entry_point
== 0)
181 *entry_point
= VM_MIN_ADDRESS
;
184 case x86_THREAD_STATE32
:
186 x86_thread_state32_t
*state25
;
188 state25
= (i386_thread_state_t
*) tstate
;
189 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
193 case x86_THREAD_STATE64
:
195 x86_thread_state64_t
*state25
;
197 state25
= (x86_thread_state64_t
*) tstate
;
198 *entry_point
= state25
->rip
? state25
->rip
: VM_MIN_ADDRESS64
;
202 return (KERN_SUCCESS
);
206 * FIXME - thread_set_child
209 void thread_set_child(thread_t child
, int pid
);
211 thread_set_child(thread_t child
, int pid
)
213 pal_register_cache_state(child
, DIRTY
);
215 if (thread_is_64bit(child
)) {
216 x86_saved_state64_t
*iss64
;
218 iss64
= USER_REGS64(child
);
222 iss64
->isf
.rflags
&= ~EFL_CF
;
224 x86_saved_state32_t
*iss32
;
226 iss32
= USER_REGS32(child
);
230 iss32
->efl
&= ~EFL_CF
;
237 * System Call handling code
240 extern long fuword(vm_offset_t
);
242 __attribute__((noreturn
))
244 machdep_syscall(x86_saved_state_t
*state
)
246 int args
[machdep_call_count
];
249 const machdep_call_t
*entry
;
250 x86_saved_state32_t
*regs
;
252 assert(is_saved_state32(state
));
253 regs
= saved_state32(state
);
257 kprintf("machdep_syscall(0x%08x) code=%d\n", regs
, trapno
);
260 DEBUG_KPRINT_SYSCALL_MDEP(
261 "machdep_syscall: trapno=%d\n", trapno
);
263 if (trapno
< 0 || trapno
>= machdep_call_count
) {
264 regs
->eax
= (unsigned int)kern_invalid(NULL
);
266 thread_exception_return();
269 entry
= &machdep_call_table
[trapno
];
270 nargs
= entry
->nargs
;
273 if (copyin((user_addr_t
) regs
->uesp
+ sizeof (int),
274 (char *) args
, (nargs
* sizeof (int)))) {
275 regs
->eax
= KERN_INVALID_ADDRESS
;
277 thread_exception_return();
283 regs
->eax
= (*entry
->routine
.args_0
)();
286 regs
->eax
= (*entry
->routine
.args_1
)(args
[0]);
289 regs
->eax
= (*entry
->routine
.args_2
)(args
[0],args
[1]);
292 if (!entry
->bsd_style
)
293 regs
->eax
= (*entry
->routine
.args_3
)(args
[0],args
[1],args
[2]);
298 error
= (*entry
->routine
.args_bsd_3
)(&rval
, args
[0], args
[1], args
[2]);
301 regs
->efl
|= EFL_CF
; /* carry bit */
304 regs
->efl
&= ~EFL_CF
;
309 regs
->eax
= (*entry
->routine
.args_4
)(args
[0], args
[1], args
[2], args
[3]);
313 panic("machdep_syscall: too many args");
316 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs
->eax
);
318 throttle_lowpri_io(1);
320 thread_exception_return();
324 __attribute__((noreturn
))
326 machdep_syscall64(x86_saved_state_t
*state
)
329 const machdep_call_t
*entry
;
330 x86_saved_state64_t
*regs
;
332 assert(is_saved_state64(state
));
333 regs
= saved_state64(state
);
335 trapno
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
337 DEBUG_KPRINT_SYSCALL_MDEP(
338 "machdep_syscall64: trapno=%d\n", trapno
);
340 if (trapno
< 0 || trapno
>= machdep_call_count
) {
341 regs
->rax
= (unsigned int)kern_invalid(NULL
);
343 thread_exception_return();
346 entry
= &machdep_call_table64
[trapno
];
348 switch (entry
->nargs
) {
350 regs
->rax
= (*entry
->routine
.args_0
)();
353 regs
->rax
= (*entry
->routine
.args64_1
)(regs
->rdi
);
356 regs
->rax
= (*entry
->routine
.args64_2
)(regs
->rdi
, regs
->rsi
);
359 panic("machdep_syscall64: too many args");
362 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs
->rax
);
364 throttle_lowpri_io(1);
366 thread_exception_return();
370 #endif /* MACH_BSD */
373 typedef kern_return_t (*mach_call_t
)(void *);
375 struct mach_call_args
{
388 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
);
392 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
)
394 if (copyin((user_addr_t
)(sp
+ sizeof(int)), (char *)args
, trapp
->mach_trap_u32_words
* sizeof (int)))
395 return KERN_INVALID_ARGUMENT
;
396 #if CONFIG_REQUIRES_U32_MUNGING
397 trapp
->mach_trap_arg_munge32(args
);
399 #error U32 mach traps on x86_64 kernel requires munging
405 __private_extern__
void mach_call_munger(x86_saved_state_t
*state
);
407 extern const char *mach_syscall_name_table
[];
409 __attribute__((noreturn
))
411 mach_call_munger(x86_saved_state_t
*state
)
415 mach_call_t mach_call
;
416 kern_return_t retval
;
417 struct mach_call_args args
= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
418 x86_saved_state32_t
*regs
;
420 struct uthread
*ut
= get_bsdthread_info(current_thread());
421 uthread_reset_proc_refcount(ut
);
423 assert(is_saved_state32(state
));
424 regs
= saved_state32(state
);
426 call_number
= -(regs
->eax
);
428 DEBUG_KPRINT_SYSCALL_MACH(
429 "mach_call_munger: code=%d(%s)\n",
430 call_number
, mach_syscall_name_table
[call_number
]);
432 kprintf("mach_call_munger(0x%08x) code=%d\n", regs
, call_number
);
435 if (call_number
< 0 || call_number
>= mach_trap_count
) {
436 i386_exception(EXC_SYSCALL
, call_number
, 1);
439 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
441 if (mach_call
== (mach_call_t
)kern_invalid
) {
442 DEBUG_KPRINT_SYSCALL_MACH(
443 "mach_call_munger: kern_invalid 0x%x\n", regs
->eax
);
444 i386_exception(EXC_SYSCALL
, call_number
, 1);
448 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
450 retval
= mach_call_arg_munger32(regs
->uesp
, &args
, &mach_trap_table
[call_number
]);
451 if (retval
!= KERN_SUCCESS
) {
454 DEBUG_KPRINT_SYSCALL_MACH(
455 "mach_call_munger: retval=0x%x\n", retval
);
457 thread_exception_return();
463 mach_kauth_cred_uthread_update();
466 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
467 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
468 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
470 retval
= mach_call(&args
);
472 DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval
);
474 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
475 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
480 throttle_lowpri_io(1);
483 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
484 panic("system call returned with uu_proc_refcount != 0");
488 thread_exception_return();
493 __private_extern__
void mach_call_munger64(x86_saved_state_t
*regs
);
495 __attribute__((noreturn
))
497 mach_call_munger64(x86_saved_state_t
*state
)
501 mach_call_t mach_call
;
502 struct mach_call_args args
= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
503 x86_saved_state64_t
*regs
;
505 struct uthread
*ut
= get_bsdthread_info(current_thread());
506 uthread_reset_proc_refcount(ut
);
508 assert(is_saved_state64(state
));
509 regs
= saved_state64(state
);
511 call_number
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
513 DEBUG_KPRINT_SYSCALL_MACH(
514 "mach_call_munger64: code=%d(%s)\n",
515 call_number
, mach_syscall_name_table
[call_number
]);
517 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
518 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_START
,
519 regs
->rdi
, regs
->rsi
, regs
->rdx
, regs
->r10
, 0);
521 if (call_number
< 0 || call_number
>= mach_trap_count
) {
522 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
525 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
527 if (mach_call
== (mach_call_t
)kern_invalid
) {
528 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
531 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
533 int args_in_regs
= MIN(6, argc
);
535 memcpy(&args
.arg1
, ®s
->rdi
, args_in_regs
* sizeof(syscall_arg_t
));
541 copyin_count
= (argc
- 6) * (int)sizeof(syscall_arg_t
);
543 if (copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)&args
.arg7
, copyin_count
)) {
544 regs
->rax
= KERN_INVALID_ARGUMENT
;
546 thread_exception_return();
553 mach_kauth_cred_uthread_update();
556 regs
->rax
= (uint64_t)mach_call((void *)&args
);
558 DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs
->rax
);
560 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
561 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
562 regs
->rax
, 0, 0, 0, 0);
564 throttle_lowpri_io(1);
567 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
568 panic("system call returned with uu_proc_refcount != 0");
572 thread_exception_return();
578 * thread_setuserstack:
580 * Sets the user stack pointer into the machine
581 * dependent thread state info.
586 mach_vm_address_t user_stack
)
588 pal_register_cache_state(thread
, DIRTY
);
589 if (thread_is_64bit(thread
)) {
590 x86_saved_state64_t
*iss64
;
592 iss64
= USER_REGS64(thread
);
594 iss64
->isf
.rsp
= (uint64_t)user_stack
;
596 x86_saved_state32_t
*iss32
;
598 iss32
= USER_REGS32(thread
);
600 iss32
->uesp
= CAST_DOWN_EXPLICIT(unsigned int, user_stack
);
605 * thread_adjuserstack:
607 * Returns the adjusted user stack pointer from the machine
608 * dependent thread state info. Used for small (<2G) deltas.
615 pal_register_cache_state(thread
, DIRTY
);
616 if (thread_is_64bit(thread
)) {
617 x86_saved_state64_t
*iss64
;
619 iss64
= USER_REGS64(thread
);
621 iss64
->isf
.rsp
+= adjust
;
623 return iss64
->isf
.rsp
;
625 x86_saved_state32_t
*iss32
;
627 iss32
= USER_REGS32(thread
);
629 iss32
->uesp
+= adjust
;
631 return CAST_USER_ADDR_T(iss32
->uesp
);
636 * thread_setentrypoint:
638 * Sets the user PC into the machine
639 * dependent thread state info.
642 thread_setentrypoint(thread_t thread
, mach_vm_address_t entry
)
644 pal_register_cache_state(thread
, DIRTY
);
645 if (thread_is_64bit(thread
)) {
646 x86_saved_state64_t
*iss64
;
648 iss64
= USER_REGS64(thread
);
650 iss64
->isf
.rip
= (uint64_t)entry
;
652 x86_saved_state32_t
*iss32
;
654 iss32
= USER_REGS32(thread
);
656 iss32
->eip
= CAST_DOWN_EXPLICIT(unsigned int, entry
);
662 thread_setsinglestep(thread_t thread
, int on
)
664 pal_register_cache_state(thread
, DIRTY
);
665 if (thread_is_64bit(thread
)) {
666 x86_saved_state64_t
*iss64
;
668 iss64
= USER_REGS64(thread
);
671 iss64
->isf
.rflags
|= EFL_TF
;
673 iss64
->isf
.rflags
&= ~EFL_TF
;
675 x86_saved_state32_t
*iss32
;
677 iss32
= USER_REGS32(thread
);
680 iss32
->efl
|= EFL_TF
;
682 if (iss32
->cs
== SYSENTER_CS
)
683 iss32
->cs
= SYSENTER_TF_CS
;
686 iss32
->efl
&= ~EFL_TF
;
689 return (KERN_SUCCESS
);
693 get_user_regs(thread_t th
)
695 pal_register_cache_state(th
, DIRTY
);
696 return(USER_STATE(th
));
700 find_user_regs(thread_t thread
)
702 return get_user_regs(thread
);
707 * DTrace would like to have a peek at the kernel interrupt state, if available.
708 * Based on osfmk/chud/i386/chud_thread_i386.c:chudxnu_thread_get_state(), which see.
710 x86_saved_state_t
*find_kern_regs(thread_t
);
713 find_kern_regs(thread_t thread
)
715 if (thread
== current_thread() &&
716 NULL
!= current_cpu_datap()->cpu_int_state
&&
717 !(USER_STATE(thread
) == current_cpu_datap()->cpu_int_state
&&
718 current_cpu_datap()->cpu_interrupt_level
== 1)) {
720 return current_cpu_datap()->cpu_int_state
;
726 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
729 dtrace_get_cpu_int_stack_top(void)
731 return current_cpu_datap()->cpu_int_stack_top
;