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);
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
,
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(
157 mach_vm_offset_t
*default_user_stack
)
159 if (thread_is_64bit(thread
)) {
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
);
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");
315 if (current_thread()->funnel_lock
)
316 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
318 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs
->eax
);
320 throttle_lowpri_io(1);
322 thread_exception_return();
328 machdep_syscall64(x86_saved_state_t
*state
)
331 const machdep_call_t
*entry
;
332 x86_saved_state64_t
*regs
;
334 assert(is_saved_state64(state
));
335 regs
= saved_state64(state
);
337 trapno
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
339 DEBUG_KPRINT_SYSCALL_MDEP(
340 "machdep_syscall64: trapno=%d\n", trapno
);
342 if (trapno
< 0 || trapno
>= machdep_call_count
) {
343 regs
->rax
= (unsigned int)kern_invalid(NULL
);
345 thread_exception_return();
348 entry
= &machdep_call_table64
[trapno
];
350 switch (entry
->nargs
) {
352 regs
->rax
= (*entry
->routine
.args_0
)();
355 regs
->rax
= (*entry
->routine
.args64_1
)(regs
->rdi
);
358 panic("machdep_syscall64: too many args");
360 if (current_thread()->funnel_lock
)
361 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
363 DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs
->rax
);
365 throttle_lowpri_io(1);
367 thread_exception_return();
371 #endif /* MACH_BSD */
374 typedef kern_return_t (*mach_call_t
)(void *);
376 struct mach_call_args
{
389 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
);
393 mach_call_arg_munger32(uint32_t sp
, struct mach_call_args
*args
, const mach_trap_t
*trapp
)
395 if (copyin((user_addr_t
)(sp
+ sizeof(int)), (char *)args
, trapp
->mach_trap_u32_words
* sizeof (int)))
396 return KERN_INVALID_ARGUMENT
;
397 trapp
->mach_trap_arg_munge32(NULL
, args
);
402 __private_extern__
void mach_call_munger(x86_saved_state_t
*state
);
404 extern const char *mach_syscall_name_table
[];
407 mach_call_munger(x86_saved_state_t
*state
)
411 mach_call_t mach_call
;
412 kern_return_t retval
;
413 struct mach_call_args args
= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
414 x86_saved_state32_t
*regs
;
416 assert(is_saved_state32(state
));
417 regs
= saved_state32(state
);
419 call_number
= -(regs
->eax
);
421 DEBUG_KPRINT_SYSCALL_MACH(
422 "mach_call_munger: code=%d(%s)\n",
423 call_number
, mach_syscall_name_table
[call_number
]);
425 kprintf("mach_call_munger(0x%08x) code=%d\n", regs
, call_number
);
428 if (call_number
< 0 || call_number
>= mach_trap_count
) {
429 i386_exception(EXC_SYSCALL
, call_number
, 1);
432 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
434 if (mach_call
== (mach_call_t
)kern_invalid
) {
435 DEBUG_KPRINT_SYSCALL_MACH(
436 "mach_call_munger: kern_invalid 0x%x\n", regs
->eax
);
437 i386_exception(EXC_SYSCALL
, call_number
, 1);
441 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
443 retval
= mach_call_arg_munger32(regs
->uesp
, &args
, &mach_trap_table
[call_number
]);
444 if (retval
!= KERN_SUCCESS
) {
447 DEBUG_KPRINT_SYSCALL_MACH(
448 "mach_call_munger: retval=0x%x\n", retval
);
450 thread_exception_return();
456 mach_kauth_cred_uthread_update();
459 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
460 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
461 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
463 retval
= mach_call(&args
);
465 DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval
);
467 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
468 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
473 throttle_lowpri_io(1);
475 thread_exception_return();
480 __private_extern__
void mach_call_munger64(x86_saved_state_t
*regs
);
483 mach_call_munger64(x86_saved_state_t
*state
)
487 mach_call_t mach_call
;
488 x86_saved_state64_t
*regs
;
490 assert(is_saved_state64(state
));
491 regs
= saved_state64(state
);
493 call_number
= (int)(regs
->rax
& SYSCALL_NUMBER_MASK
);
495 DEBUG_KPRINT_SYSCALL_MACH(
496 "mach_call_munger64: code=%d(%s)\n",
497 call_number
, mach_syscall_name_table
[call_number
]);
499 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
500 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_START
,
501 regs
->rdi
, regs
->rsi
, regs
->rdx
, regs
->r10
, 0);
503 if (call_number
< 0 || call_number
>= mach_trap_count
) {
504 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
507 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
509 if (mach_call
== (mach_call_t
)kern_invalid
) {
510 i386_exception(EXC_SYSCALL
, regs
->rax
, 1);
513 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
518 copyin_count
= (argc
- 6) * (int)sizeof(uint64_t);
520 if (copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)®s
->v_arg6
, copyin_count
)) {
521 regs
->rax
= KERN_INVALID_ARGUMENT
;
523 thread_exception_return();
529 mach_kauth_cred_uthread_update();
532 regs
->rax
= (uint64_t)mach_call((void *)(®s
->rdi
));
534 DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs
->rax
);
536 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
537 MACHDBG_CODE(DBG_MACH_EXCP_SC
,(call_number
)) | DBG_FUNC_END
,
538 regs
->rax
, 0, 0, 0, 0);
540 throttle_lowpri_io(1);
542 thread_exception_return();
548 * thread_setuserstack:
550 * Sets the user stack pointer into the machine
551 * dependent thread state info.
556 mach_vm_address_t user_stack
)
558 pal_register_cache_state(thread
, DIRTY
);
559 if (thread_is_64bit(thread
)) {
560 x86_saved_state64_t
*iss64
;
562 iss64
= USER_REGS64(thread
);
564 iss64
->isf
.rsp
= (uint64_t)user_stack
;
566 x86_saved_state32_t
*iss32
;
568 iss32
= USER_REGS32(thread
);
570 iss32
->uesp
= CAST_DOWN_EXPLICIT(unsigned int, user_stack
);
575 * thread_adjuserstack:
577 * Returns the adjusted user stack pointer from the machine
578 * dependent thread state info. Used for small (<2G) deltas.
585 pal_register_cache_state(thread
, DIRTY
);
586 if (thread_is_64bit(thread
)) {
587 x86_saved_state64_t
*iss64
;
589 iss64
= USER_REGS64(thread
);
591 iss64
->isf
.rsp
+= adjust
;
593 return iss64
->isf
.rsp
;
595 x86_saved_state32_t
*iss32
;
597 iss32
= USER_REGS32(thread
);
599 iss32
->uesp
+= adjust
;
601 return CAST_USER_ADDR_T(iss32
->uesp
);
606 * thread_setentrypoint:
608 * Sets the user PC into the machine
609 * dependent thread state info.
612 thread_setentrypoint(thread_t thread
, mach_vm_address_t entry
)
614 pal_register_cache_state(thread
, DIRTY
);
615 if (thread_is_64bit(thread
)) {
616 x86_saved_state64_t
*iss64
;
618 iss64
= USER_REGS64(thread
);
620 iss64
->isf
.rip
= (uint64_t)entry
;
622 x86_saved_state32_t
*iss32
;
624 iss32
= USER_REGS32(thread
);
626 iss32
->eip
= CAST_DOWN_EXPLICIT(unsigned int, entry
);
632 thread_setsinglestep(thread_t thread
, int on
)
634 pal_register_cache_state(thread
, DIRTY
);
635 if (thread_is_64bit(thread
)) {
636 x86_saved_state64_t
*iss64
;
638 iss64
= USER_REGS64(thread
);
641 iss64
->isf
.rflags
|= EFL_TF
;
643 iss64
->isf
.rflags
&= ~EFL_TF
;
645 x86_saved_state32_t
*iss32
;
647 iss32
= USER_REGS32(thread
);
650 iss32
->efl
|= EFL_TF
;
652 if (iss32
->cs
== SYSENTER_CS
)
653 iss32
->cs
= SYSENTER_TF_CS
;
656 iss32
->efl
&= ~EFL_TF
;
659 return (KERN_SUCCESS
);
664 /* XXX this should be a struct savearea so that CHUD will work better on x86 */
666 find_user_regs(thread_t thread
)
668 pal_register_cache_state(thread
, DIRTY
);
669 return USER_STATE(thread
);
673 get_user_regs(thread_t th
)
675 pal_register_cache_state(th
, DIRTY
);
676 return(USER_STATE(th
));
681 * DTrace would like to have a peek at the kernel interrupt state, if available.
682 * Based on osfmk/chud/i386/chud_thread_i386.c:chudxnu_thread_get_state(), which see.
684 x86_saved_state_t
*find_kern_regs(thread_t
);
687 find_kern_regs(thread_t thread
)
689 if (thread
== current_thread() &&
690 NULL
!= current_cpu_datap()->cpu_int_state
&&
691 !(USER_STATE(thread
) == current_cpu_datap()->cpu_int_state
&&
692 current_cpu_datap()->cpu_interrupt_level
== 1)) {
694 return current_cpu_datap()->cpu_int_state
;
700 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
703 dtrace_get_cpu_int_stack_top(void)
705 return current_cpu_datap()->cpu_int_stack_top
;