2 * Copyright (c) 2000-2010 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);
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);
84 extern void throttle_lowpri_io(int);
89 * Return the user stack pointer from the machine
90 * dependent thread state info.
94 __unused thread_t thread
,
96 thread_state_t tstate
,
97 __unused
unsigned int count
,
98 mach_vm_offset_t
*user_stack
,
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
);
152 __unused thread_t thread
,
154 thread_state_t tstate
,
155 __unused
unsigned int count
,
156 mach_vm_offset_t
*entry_point
162 if (*entry_point
== 0)
163 *entry_point
= VM_MIN_ADDRESS
;
166 case x86_THREAD_STATE32
:
168 x86_thread_state32_t
*state25
;
170 state25
= (i386_thread_state_t
*) tstate
;
171 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
175 case x86_THREAD_STATE64
:
177 x86_thread_state64_t
*state25
;
179 state25
= (x86_thread_state64_t
*) tstate
;
180 *entry_point
= state25
->rip
? state25
->rip
: VM_MIN_ADDRESS64
;
184 return (KERN_SUCCESS
);
188 * FIXME - thread_set_child
191 void thread_set_child(thread_t child
, int pid
);
193 thread_set_child(thread_t child
, int pid
)
195 pal_register_cache_state(child
, DIRTY
);
197 if (thread_is_64bit(child
)) {
198 x86_saved_state64_t
*iss64
;
200 iss64
= USER_REGS64(child
);
204 iss64
->isf
.rflags
&= ~EFL_CF
;
206 x86_saved_state32_t
*iss32
;
208 iss32
= USER_REGS32(child
);
212 iss32
->efl
&= ~EFL_CF
;
219 * System Call handling code
222 extern long fuword(vm_offset_t
);
227 machdep_syscall(x86_saved_state_t
*state
)
229 int args
[machdep_call_count
];
232 machdep_call_t
*entry
;
233 x86_saved_state32_t
*regs
;
235 assert(is_saved_state32(state
));
236 regs
= saved_state32(state
);
240 kprintf("machdep_syscall(0x%08x) code=%d\n", regs
, trapno
);
243 DEBUG_KPRINT_SYSCALL_MDEP(
244 "machdep_syscall: trapno=%d\n", trapno
);
246 if (trapno
< 0 || trapno
>= machdep_call_count
) {
247 regs
->eax
= (unsigned int)kern_invalid(NULL
);
249 thread_exception_return();
252 entry
= &machdep_call_table
[trapno
];
253 nargs
= entry
->nargs
;
256 if (copyin((user_addr_t
) regs
->uesp
+ sizeof (int),
257 (char *) args
, (nargs
* sizeof (int)))) {
258 regs
->eax
= KERN_INVALID_ADDRESS
;
260 thread_exception_return();
266 regs
->eax
= (*entry
->routine
.args_0
)();
269 regs
->eax
= (*entry
->routine
.args_1
)(args
[0]);
272 regs
->eax
= (*entry
->routine
.args_2
)(args
[0],args
[1]);
275 if (!entry
->bsd_style
)
276 regs
->eax
= (*entry
->routine
.args_3
)(args
[0],args
[1],args
[2]);
281 error
= (*entry
->routine
.args_bsd_3
)(&rval
, args
[0], args
[1], args
[2]);
284 regs
->efl
|= EFL_CF
; /* carry bit */
287 regs
->efl
&= ~EFL_CF
;
292 regs
->eax
= (*entry
->routine
.args_4
)(args
[0], args
[1], args
[2], args
[3]);
296 panic("machdep_syscall: too many args");
298 if (current_thread()->funnel_lock
)
299 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
301 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs
->eax
);
303 throttle_lowpri_io(TRUE
);
305 thread_exception_return();
311 machdep_syscall64(x86_saved_state_t
*state
)
314 machdep_call_t
*entry
;
315 x86_saved_state64_t
*regs
;
317 assert(is_saved_state64(state
));
318 regs
= saved_state64(state
);
320 trapno
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
322 DEBUG_KPRINT_SYSCALL_MDEP(
323 "machdep_syscall64: trapno=%d\n", trapno
);
325 if (trapno
< 0 || trapno
>= machdep_call_count
) {
326 regs
->rax
= (unsigned int)kern_invalid(NULL
);
328 thread_exception_return();
331 entry
= &machdep_call_table64
[trapno
];
333 switch (entry
->nargs
) {
335 regs
->rax
= (*entry
->routine
.args_0
)();
338 regs
->rax
= (*entry
->routine
.args64_1
)(regs
->rdi
);
341 panic("machdep_syscall64: too many args");
343 if (current_thread()->funnel_lock
)
344 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
346 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs
->rax
);
348 throttle_lowpri_io(TRUE
);
350 thread_exception_return();
354 #endif /* MACH_BSD */
357 typedef kern_return_t (*mach_call_t
)(void *);
359 struct mach_call_args
{
372 mach_call_arg_munger32(uint32_t sp
, int nargs
, int call_number
, struct mach_call_args
*args
);
376 mach_call_arg_munger32(uint32_t sp
, int nargs
, int call_number
, struct mach_call_args
*args
)
378 unsigned int args32
[9];
380 if (copyin((user_addr_t
)(sp
+ sizeof(int)), (char *)args32
, nargs
* sizeof (int)))
381 return KERN_INVALID_ARGUMENT
;
384 case 9: args
->arg9
= args32
[8];
385 case 8: args
->arg8
= args32
[7];
386 case 7: args
->arg7
= args32
[6];
387 case 6: args
->arg6
= args32
[5];
388 case 5: args
->arg5
= args32
[4];
389 case 4: args
->arg4
= args32
[3];
390 case 3: args
->arg3
= args32
[2];
391 case 2: args
->arg2
= args32
[1];
392 case 1: args
->arg1
= args32
[0];
394 if (call_number
== 90) {
395 /* munge_l for mach_wait_until_trap() */
396 args
->arg1
= (((uint64_t)(args32
[0])) | ((((uint64_t)(args32
[1]))<<32)));
398 if (call_number
== 93) {
399 /* munge_wl for mk_timer_arm_trap() */
400 args
->arg2
= (((uint64_t)(args32
[1])) | ((((uint64_t)(args32
[2]))<<32)));
407 __private_extern__
void mach_call_munger(x86_saved_state_t
*state
);
409 extern const char *mach_syscall_name_table
[];
412 mach_call_munger(x86_saved_state_t
*state
)
416 mach_call_t mach_call
;
417 kern_return_t retval
;
418 struct mach_call_args args
= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
419 x86_saved_state32_t
*regs
;
421 assert(is_saved_state32(state
));
422 regs
= saved_state32(state
);
424 call_number
= -(regs
->eax
);
426 DEBUG_KPRINT_SYSCALL_MACH(
427 "mach_call_munger: code=%d(%s)\n",
428 call_number
, mach_syscall_name_table
[call_number
]);
430 kprintf("mach_call_munger(0x%08x) code=%d\n", regs
, call_number
);
433 if (call_number
< 0 || call_number
>= mach_trap_count
) {
434 i386_exception(EXC_SYSCALL
, call_number
, 1);
437 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
439 if (mach_call
== (mach_call_t
)kern_invalid
) {
440 DEBUG_KPRINT_SYSCALL_MACH(
441 "mach_call_munger: kern_invalid 0x%x\n", regs
->eax
);
442 i386_exception(EXC_SYSCALL
, call_number
, 1);
446 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
448 retval
= mach_call_arg_munger32(regs
->uesp
, argc
, call_number
, &args
);
449 if (retval
!= KERN_SUCCESS
) {
452 DEBUG_KPRINT_SYSCALL_MACH(
453 "mach_call_munger: retval=0x%x\n", retval
);
455 thread_exception_return();
461 mach_kauth_cred_uthread_update();
463 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
464 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
466 retval
= mach_call(&args
);
468 DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval
);
470 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
474 throttle_lowpri_io(TRUE
);
476 thread_exception_return();
481 __private_extern__
void mach_call_munger64(x86_saved_state_t
*regs
);
484 mach_call_munger64(x86_saved_state_t
*state
)
488 mach_call_t mach_call
;
489 x86_saved_state64_t
*regs
;
491 assert(is_saved_state64(state
));
492 regs
= saved_state64(state
);
494 call_number
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
496 DEBUG_KPRINT_SYSCALL_MACH(
497 "mach_call_munger64: code=%d(%s)\n",
498 call_number
, mach_syscall_name_table
[call_number
]);
500 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
,
501 (call_number
)) | DBG_FUNC_START
,
502 regs
->rdi
, regs
->rsi
,
503 regs
->rdx
, regs
->r10
, 0);
505 if (call_number
< 0 || call_number
>= mach_trap_count
) {
506 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
509 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
511 if (mach_call
== (mach_call_t
)kern_invalid
) {
512 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
515 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
520 copyin_count
= (argc
- 6) * (int)sizeof(uint64_t);
522 if (copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)®s
->v_arg6
, copyin_count
)) {
523 regs
->rax
= KERN_INVALID_ARGUMENT
;
525 thread_exception_return();
531 mach_kauth_cred_uthread_update();
534 regs
->rax
= (uint64_t)mach_call((void *)(®s
->rdi
));
536 DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs
->rax
);
538 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
,
539 (call_number
)) | DBG_FUNC_END
,
540 regs
->rax
, 0, 0, 0, 0);
542 throttle_lowpri_io(TRUE
);
544 thread_exception_return();
550 * thread_setuserstack:
552 * Sets the user stack pointer into the machine
553 * dependent thread state info.
558 mach_vm_address_t user_stack
)
560 pal_register_cache_state(thread
, DIRTY
);
561 if (thread_is_64bit(thread
)) {
562 x86_saved_state64_t
*iss64
;
564 iss64
= USER_REGS64(thread
);
566 iss64
->isf
.rsp
= (uint64_t)user_stack
;
568 x86_saved_state32_t
*iss32
;
570 iss32
= USER_REGS32(thread
);
572 iss32
->uesp
= CAST_DOWN_EXPLICIT(unsigned int, user_stack
);
577 * thread_adjuserstack:
579 * Returns the adjusted user stack pointer from the machine
580 * dependent thread state info. Used for small (<2G) deltas.
587 pal_register_cache_state(thread
, DIRTY
);
588 if (thread_is_64bit(thread
)) {
589 x86_saved_state64_t
*iss64
;
591 iss64
= USER_REGS64(thread
);
593 iss64
->isf
.rsp
+= adjust
;
595 return iss64
->isf
.rsp
;
597 x86_saved_state32_t
*iss32
;
599 iss32
= USER_REGS32(thread
);
601 iss32
->uesp
+= adjust
;
603 return CAST_USER_ADDR_T(iss32
->uesp
);
608 * thread_setentrypoint:
610 * Sets the user PC into the machine
611 * dependent thread state info.
614 thread_setentrypoint(thread_t thread
, mach_vm_address_t entry
)
616 pal_register_cache_state(thread
, DIRTY
);
617 if (thread_is_64bit(thread
)) {
618 x86_saved_state64_t
*iss64
;
620 iss64
= USER_REGS64(thread
);
622 iss64
->isf
.rip
= (uint64_t)entry
;
624 x86_saved_state32_t
*iss32
;
626 iss32
= USER_REGS32(thread
);
628 iss32
->eip
= CAST_DOWN_EXPLICIT(unsigned int, entry
);
634 thread_setsinglestep(thread_t thread
, int on
)
636 pal_register_cache_state(thread
, DIRTY
);
637 if (thread_is_64bit(thread
)) {
638 x86_saved_state64_t
*iss64
;
640 iss64
= USER_REGS64(thread
);
643 iss64
->isf
.rflags
|= EFL_TF
;
645 iss64
->isf
.rflags
&= ~EFL_TF
;
647 x86_saved_state32_t
*iss32
;
649 iss32
= USER_REGS32(thread
);
652 iss32
->efl
|= EFL_TF
;
654 if (iss32
->cs
== SYSENTER_CS
)
655 iss32
->cs
= SYSENTER_TF_CS
;
658 iss32
->efl
&= ~EFL_TF
;
661 return (KERN_SUCCESS
);
666 /* XXX this should be a struct savearea so that CHUD will work better on x86 */
668 find_user_regs(thread_t thread
)
670 pal_register_cache_state(thread
, DIRTY
);
671 return USER_STATE(thread
);
675 get_user_regs(thread_t th
)
677 pal_register_cache_state(th
, DIRTY
);
678 return(USER_STATE(th
));
683 * DTrace would like to have a peek at the kernel interrupt state, if available.
684 * Based on osfmk/chud/i386/chud_thread_i386.c:chudxnu_thread_get_state(), which see.
686 x86_saved_state_t
*find_kern_regs(thread_t
);
689 find_kern_regs(thread_t thread
)
691 if (thread
== current_thread() &&
692 NULL
!= current_cpu_datap()->cpu_int_state
&&
693 !(USER_STATE(thread
) == current_cpu_datap()->cpu_int_state
&&
694 current_cpu_datap()->cpu_interrupt_level
== 1)) {
696 return current_cpu_datap()->cpu_int_state
;
702 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
705 dtrace_get_cpu_int_stack_top(void)
707 return current_cpu_datap()->cpu_int_stack_top
;