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@
28 #include <kern/task.h>
29 #include <kern/thread.h>
30 #include <kern/assert.h>
31 #include <kern/clock.h>
32 #include <kern/locks.h>
33 #include <kern/sched_prim.h>
34 #include <kern/debug.h>
35 #include <mach/machine/thread_status.h>
36 #include <mach/thread_act.h>
37 #include <mach/branch_predicates.h>
39 #include <sys/kernel.h>
41 #include <sys/proc_internal.h>
42 #include <sys/syscall.h>
43 #include <sys/systm.h>
45 #include <sys/errno.h>
46 #include <sys/kdebug.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/kauth.h>
50 #include <sys/systm.h>
52 #include <security/audit/audit.h>
55 #include <i386/machine_routines.h>
56 #include <mach/i386/syscall_sw.h>
58 #include <machine/pal_routines.h>
61 extern int32_t dtrace_systrace_syscall(struct proc
*, void *, int *);
62 extern void dtrace_systrace_syscall_return(unsigned short, int, int *);
65 extern void unix_syscall(x86_saved_state_t
*);
66 extern void unix_syscall64(x86_saved_state_t
*);
67 extern void *find_user_regs(thread_t
);
69 /* dynamically generated at build time based on syscalls.master */
70 extern const char *syscallnames
[];
72 #define code_is_kdebug_trace(code) (((code) == SYS_kdebug_trace) || \
73 ((code) == SYS_kdebug_trace64) || \
74 ((code) == SYS_kdebug_trace_string))
77 * Function: unix_syscall
79 * Inputs: regs - pointer to i386 save area
83 __attribute__((noreturn
))
85 unix_syscall(x86_saved_state_t
*state
)
95 struct uthread
*uthread
;
96 x86_saved_state32_t
*regs
;
100 assert(is_saved_state32(state
));
101 regs
= saved_state32(state
);
103 if (regs
->eax
== 0x800)
104 thread_exception_return();
106 thread
= current_thread();
107 uthread
= get_bsdthread_info(thread
);
109 uthread_reset_proc_refcount(uthread
);
111 /* Get the approriate proc; may be different from task's for vfork() */
112 is_vfork
= uthread
->uu_flag
& UT_VFORK
;
113 if (__improbable(is_vfork
!= 0))
116 p
= (struct proc
*)get_bsdtask_info(current_task());
118 code
= regs
->eax
& I386_SYSCALL_NUMBER_MASK
;
119 DEBUG_KPRINT_SYSCALL_UNIX("unix_syscall: code=%d(%s) eip=%u\n",
120 code
, syscallnames
[code
>= nsysent
? SYS_invalid
: code
], (uint32_t)regs
->eip
);
121 params
= (vm_offset_t
) (regs
->uesp
+ sizeof (int));
123 regs
->efl
&= ~(EFL_CF
);
125 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
127 if (__improbable(callp
== sysent
)) {
128 code
= fuword(params
);
129 params
+= sizeof(int);
130 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
133 vt
= (void *)uthread
->uu_arg
;
135 if (callp
->sy_arg_bytes
!= 0) {
136 #if CONFIG_REQUIRES_U32_MUNGING
139 #error U32 syscalls on x86_64 kernel requires munging
143 assert((unsigned) callp
->sy_arg_bytes
<= sizeof (uthread
->uu_arg
));
144 nargs
= callp
->sy_arg_bytes
;
145 error
= copyin((user_addr_t
) params
, (char *) vt
, nargs
);
149 thread_exception_return();
153 if (__probable(!code_is_kdebug_trace(code
))) {
156 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
157 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
158 *ip
, *(ip
+1), *(ip
+2), *(ip
+3), 0);
161 #if CONFIG_REQUIRES_U32_MUNGING
162 mungerp
= callp
->sy_arg_munge32
;
168 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
169 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
173 * Delayed binding of thread credential to process credential, if we
174 * are not running with an explicitly set thread credential.
176 kauth_cred_uthread_update(uthread
, p
);
178 uthread
->uu_rval
[0] = 0;
179 uthread
->uu_rval
[1] = 0;
180 uthread
->uu_flag
|= UT_NOTCANCELPT
;
181 uthread
->syscall_code
= code
;
185 uthread
->uu_iocount
= 0;
186 uthread
->uu_vpindex
= 0;
189 AUDIT_SYSCALL_ENTER(code
, p
, uthread
);
190 error
= (*(callp
->sy_call
))((void *) p
, (void *) vt
, &(uthread
->uu_rval
[0]));
191 AUDIT_SYSCALL_EXIT(code
, p
, uthread
, error
);
194 if (uthread
->uu_iocount
)
195 printf("system call returned with uu_iocount != 0\n");
198 uthread
->t_dtrace_errno
= error
;
199 #endif /* CONFIG_DTRACE */
201 if (__improbable(error
== ERESTART
)) {
203 * Move the user's pc back to repeat the syscall:
204 * 5 bytes for a sysenter, or 2 for an int 8x.
205 * The SYSENTER_TF_CS covers single-stepping over a sysenter
206 * - see debug trap handler in idt.s/idt64.s
209 pal_syscall_restart(thread
, state
);
211 else if (error
!= EJUSTRETURN
) {
212 if (__improbable(error
)) {
214 regs
->efl
|= EFL_CF
; /* carry bit */
215 } else { /* (not error) */
217 * We split retval across two registers, in case the
218 * syscall had a 64-bit return value, in which case
219 * eax/edx matches the function call ABI.
221 regs
->eax
= uthread
->uu_rval
[0];
222 regs
->edx
= uthread
->uu_rval
[1];
226 DEBUG_KPRINT_SYSCALL_UNIX(
227 "unix_syscall: error=%d retval=(%u,%u)\n",
228 error
, regs
->eax
, regs
->edx
);
230 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
232 if (__improbable(uthread
->uu_lowpri_window
)) {
234 * task is marked as a low priority I/O type
235 * and the I/O we issued while in this system call
236 * collided with normal I/O operations... we'll
237 * delay in order to mitigate the impact of this
238 * task on the normal operation of the system
240 throttle_lowpri_io(1);
242 if (__probable(!code_is_kdebug_trace(code
)))
243 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
244 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
245 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], pid
, 0);
247 if (__improbable(!is_vfork
&& callp
->sy_call
== (sy_call_t
*)execve
&& !error
)) {
248 pal_execve_return(thread
);
252 if (__improbable(uthread_get_proc_refcount(uthread
) != 0)) {
253 panic("system call returned with uu_proc_refcount != 0");
257 thread_exception_return();
261 __attribute__((noreturn
))
263 unix_syscall64(x86_saved_state_t
*state
)
268 struct sysent
*callp
;
270 boolean_t args_start_at_rdi
;
273 struct uthread
*uthread
;
274 x86_saved_state64_t
*regs
;
277 assert(is_saved_state64(state
));
278 regs
= saved_state64(state
);
280 if (regs
->rax
== 0x2000800)
281 thread_exception_return();
283 thread
= current_thread();
284 uthread
= get_bsdthread_info(thread
);
286 uthread_reset_proc_refcount(uthread
);
288 /* Get the approriate proc; may be different from task's for vfork() */
289 if (__probable(!(uthread
->uu_flag
& UT_VFORK
)))
290 p
= (struct proc
*)get_bsdtask_info(current_task());
294 /* Verify that we are not being called from a task without a proc */
295 if (__improbable(p
== NULL
)) {
297 regs
->isf
.rflags
|= EFL_CF
;
298 task_terminate_internal(current_task());
299 thread_exception_return();
303 code
= regs
->rax
& SYSCALL_NUMBER_MASK
;
304 DEBUG_KPRINT_SYSCALL_UNIX(
305 "unix_syscall64: code=%d(%s) rip=%llx\n",
306 code
, syscallnames
[code
>= nsysent
? SYS_invalid
: code
], regs
->isf
.rip
);
307 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
309 vt
= (void *)uthread
->uu_arg
;
311 if (__improbable(callp
== sysent
)) {
313 * indirect system call... system call number
317 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
318 args_start_at_rdi
= FALSE
;
321 args_start_at_rdi
= TRUE
;
325 if (callp
->sy_narg
!= 0) {
326 assert(callp
->sy_narg
<= 8); /* size of uu_arg */
328 args_in_regs
= MIN(args_in_regs
, callp
->sy_narg
);
329 memcpy(vt
, args_start_at_rdi
? ®s
->rdi
: ®s
->rsi
, args_in_regs
* sizeof(syscall_arg_t
));
332 if (!code_is_kdebug_trace(code
)) {
333 uint64_t *ip
= (uint64_t *)vt
;
335 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
336 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
337 (int)(*ip
), (int)(*(ip
+1)), (int)(*(ip
+2)), (int)(*(ip
+3)), 0);
340 if (__improbable(callp
->sy_narg
> args_in_regs
)) {
343 copyin_count
= (callp
->sy_narg
- args_in_regs
) * sizeof(syscall_arg_t
);
345 error
= copyin((user_addr_t
)(regs
->isf
.rsp
+ sizeof(user_addr_t
)), (char *)&uthread
->uu_arg
[args_in_regs
], copyin_count
);
348 regs
->isf
.rflags
|= EFL_CF
;
349 thread_exception_return();
354 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
355 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
359 * Delayed binding of thread credential to process credential, if we
360 * are not running with an explicitly set thread credential.
362 kauth_cred_uthread_update(uthread
, p
);
364 uthread
->uu_rval
[0] = 0;
365 uthread
->uu_rval
[1] = 0;
366 uthread
->uu_flag
|= UT_NOTCANCELPT
;
367 uthread
->syscall_code
= code
;
371 uthread
->uu_iocount
= 0;
372 uthread
->uu_vpindex
= 0;
375 AUDIT_SYSCALL_ENTER(code
, p
, uthread
);
376 error
= (*(callp
->sy_call
))((void *) p
, vt
, &(uthread
->uu_rval
[0]));
377 AUDIT_SYSCALL_EXIT(code
, p
, uthread
, error
);
380 if (uthread
->uu_iocount
)
381 printf("system call returned with uu_iocount != 0\n");
385 uthread
->t_dtrace_errno
= error
;
386 #endif /* CONFIG_DTRACE */
388 if (__improbable(error
== ERESTART
)) {
390 * all system calls come through via the syscall instruction
391 * in 64 bit mode... its 2 bytes in length
392 * move the user's pc back to repeat the syscall:
394 pal_syscall_restart( thread
, state
);
396 else if (error
!= EJUSTRETURN
) {
397 if (__improbable(error
)) {
399 regs
->isf
.rflags
|= EFL_CF
; /* carry bit */
400 } else { /* (not error) */
402 switch (callp
->sy_return_type
) {
403 case _SYSCALL_RET_INT_T
:
404 regs
->rax
= uthread
->uu_rval
[0];
405 regs
->rdx
= uthread
->uu_rval
[1];
407 case _SYSCALL_RET_UINT_T
:
408 regs
->rax
= ((u_int
)uthread
->uu_rval
[0]);
409 regs
->rdx
= ((u_int
)uthread
->uu_rval
[1]);
411 case _SYSCALL_RET_OFF_T
:
412 case _SYSCALL_RET_ADDR_T
:
413 case _SYSCALL_RET_SIZE_T
:
414 case _SYSCALL_RET_SSIZE_T
:
415 case _SYSCALL_RET_UINT64_T
:
416 regs
->rax
= *((uint64_t *)(&uthread
->uu_rval
[0]));
419 case _SYSCALL_RET_NONE
:
422 panic("unix_syscall: unknown return type");
425 regs
->isf
.rflags
&= ~EFL_CF
;
429 DEBUG_KPRINT_SYSCALL_UNIX(
430 "unix_syscall64: error=%d retval=(%llu,%llu)\n",
431 error
, regs
->rax
, regs
->rdx
);
433 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
435 if (__improbable(uthread
->uu_lowpri_window
)) {
437 * task is marked as a low priority I/O type
438 * and the I/O we issued while in this system call
439 * collided with normal I/O operations... we'll
440 * delay in order to mitigate the impact of this
441 * task on the normal operation of the system
443 throttle_lowpri_io(1);
445 if (__probable(!code_is_kdebug_trace(code
)))
446 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
447 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
448 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], pid
, 0);
451 if (__improbable(uthread_get_proc_refcount(uthread
))) {
452 panic("system call returned with uu_proc_refcount != 0");
456 thread_exception_return();
462 unix_syscall_return(int error
)
465 struct uthread
*uthread
;
468 struct sysent
*callp
;
470 thread
= current_thread();
471 uthread
= get_bsdthread_info(thread
);
473 pal_register_cache_state(thread
, DIRTY
);
477 if (proc_is64bit(p
)) {
478 x86_saved_state64_t
*regs
;
480 regs
= saved_state64(find_user_regs(thread
));
482 code
= uthread
->syscall_code
;
483 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
486 if (callp
->sy_call
== dtrace_systrace_syscall
)
487 dtrace_systrace_syscall_return( code
, error
, uthread
->uu_rval
);
488 #endif /* CONFIG_DTRACE */
489 AUDIT_SYSCALL_EXIT(code
, p
, uthread
, error
);
491 if (error
== ERESTART
) {
495 pal_syscall_restart( thread
, find_user_regs(thread
) );
497 else if (error
!= EJUSTRETURN
) {
500 regs
->isf
.rflags
|= EFL_CF
; /* carry bit */
501 } else { /* (not error) */
503 switch (callp
->sy_return_type
) {
504 case _SYSCALL_RET_INT_T
:
505 regs
->rax
= uthread
->uu_rval
[0];
506 regs
->rdx
= uthread
->uu_rval
[1];
508 case _SYSCALL_RET_UINT_T
:
509 regs
->rax
= ((u_int
)uthread
->uu_rval
[0]);
510 regs
->rdx
= ((u_int
)uthread
->uu_rval
[1]);
512 case _SYSCALL_RET_OFF_T
:
513 case _SYSCALL_RET_ADDR_T
:
514 case _SYSCALL_RET_SIZE_T
:
515 case _SYSCALL_RET_SSIZE_T
:
516 case _SYSCALL_RET_UINT64_T
:
517 regs
->rax
= *((uint64_t *)(&uthread
->uu_rval
[0]));
520 case _SYSCALL_RET_NONE
:
523 panic("unix_syscall: unknown return type");
526 regs
->isf
.rflags
&= ~EFL_CF
;
529 DEBUG_KPRINT_SYSCALL_UNIX(
530 "unix_syscall_return: error=%d retval=(%llu,%llu)\n",
531 error
, regs
->rax
, regs
->rdx
);
533 x86_saved_state32_t
*regs
;
535 regs
= saved_state32(find_user_regs(thread
));
537 regs
->efl
&= ~(EFL_CF
);
539 code
= uthread
->syscall_code
;
540 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
543 if (callp
->sy_call
== dtrace_systrace_syscall
)
544 dtrace_systrace_syscall_return( code
, error
, uthread
->uu_rval
);
545 #endif /* CONFIG_DTRACE */
546 AUDIT_SYSCALL_EXIT(code
, p
, uthread
, error
);
548 if (error
== ERESTART
) {
549 pal_syscall_restart( thread
, find_user_regs(thread
) );
551 else if (error
!= EJUSTRETURN
) {
554 regs
->efl
|= EFL_CF
; /* carry bit */
555 } else { /* (not error) */
556 regs
->eax
= uthread
->uu_rval
[0];
557 regs
->edx
= uthread
->uu_rval
[1];
560 DEBUG_KPRINT_SYSCALL_UNIX(
561 "unix_syscall_return: error=%d retval=(%u,%u)\n",
562 error
, regs
->eax
, regs
->edx
);
566 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
568 if (uthread
->uu_lowpri_window
) {
570 * task is marked as a low priority I/O type
571 * and the I/O we issued while in this system call
572 * collided with normal I/O operations... we'll
573 * delay in order to mitigate the impact of this
574 * task on the normal operation of the system
576 throttle_lowpri_io(1);
578 if (!code_is_kdebug_trace(code
))
579 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
580 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
581 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], p
->p_pid
, 0);
583 thread_exception_return();