2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
6 #include <kern/thread.h>
7 #include <kern/assert.h>
8 #include <kern/clock.h>
9 #include <kern/locks.h>
10 #include <kern/sched_prim.h>
11 #include <mach/machine/thread_status.h>
12 #include <mach/thread_act.h>
13 #include <machine/machine_routines.h>
14 #include <arm/thread.h>
15 #include <arm/proc_reg.h>
16 #include <pexpert/pexpert.h>
18 #include <sys/kernel.h>
20 #include <sys/proc_internal.h>
21 #include <sys/syscall.h>
22 #include <sys/systm.h>
24 #include <sys/errno.h>
25 #include <sys/kdebug.h>
26 #include <sys/sysent.h>
27 #include <sys/sysproto.h>
28 #include <sys/kauth.h>
30 #include <security/audit/audit.h>
33 extern int32_t dtrace_systrace_syscall(struct proc
*, void *, int *);
34 extern void dtrace_systrace_syscall_return(unsigned short, int, int *);
35 #endif /* CONFIG_DTRACE */
38 unix_syscall(struct arm_saved_state
* regs
, thread_t thread_act
,
39 struct uthread
* uthread
, struct proc
* proc
);
41 static int arm_get_syscall_args(uthread_t
, struct arm_saved_state
*, struct sysent
*);
42 static int arm_get_u32_syscall_args(uthread_t
, arm_saved_state32_t
*, struct sysent
*);
43 static void arm_prepare_u32_syscall_return(struct sysent
*, arm_saved_state_t
*, uthread_t
, int);
44 static void arm_prepare_syscall_return(struct sysent
*, struct arm_saved_state
*, uthread_t
, int);
45 static int arm_get_syscall_number(struct arm_saved_state
*);
46 static void arm_trace_unix_syscall(int, struct arm_saved_state
*);
47 static void arm_clear_syscall_error(struct arm_saved_state
*);
58 #define save_r10 r[10]
59 #define save_r11 r[11]
60 #define save_r12 r[12]
61 #define save_r13 r[13]
64 __XNU_PRIVATE_EXTERN
int do_count_syscalls
= 1;
65 __XNU_PRIVATE_EXTERN
int syscalls_log
[SYS_MAXSYSCALL
];
68 #define code_is_kdebug_trace(code) (((code) == SYS_kdebug_trace) || \
69 ((code) == SYS_kdebug_trace64) || \
70 ((code) == SYS_kdebug_trace_string))
73 * Function: unix_syscall
75 * Inputs: regs - pointer to Process Control Block
80 __attribute__((noreturn
))
84 struct arm_saved_state
* state
,
85 __unused thread_t thread_act
,
86 struct uthread
* uthread
,
95 assert(is_saved_state32(state
));
98 uthread_reset_proc_refcount(uthread
);
100 code
= arm_get_syscall_number(state
);
102 #define unix_syscall_kprintf(x...) /* kprintf("unix_syscall: " x) */
104 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
105 if (kdebug_enable
&& !code_is_kdebug_trace(code
)) {
106 arm_trace_unix_syscall(code
, state
);
110 if ((uthread
->uu_flag
& UT_VFORK
))
111 proc
= current_proc();
113 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
116 * sy_narg is inaccurate on ARM if a 64 bit parameter is specified. Since user_addr_t
117 * is currently a 32 bit type, this is really a long word count. See rdar://problem/6104668.
119 if (callp
->sy_narg
!= 0) {
120 if (arm_get_syscall_args(uthread
, state
, callp
) != 0) {
121 /* Too many arguments, or something failed */
122 unix_syscall_kprintf("arm_get_syscall_args failed.\n");
123 callp
= &sysent
[SYS_invalid
];
127 uthread
->uu_flag
|= UT_NOTCANCELPT
;
128 uthread
->syscall_code
= code
;
130 uthread
->uu_rval
[0] = 0;
133 * r4 is volatile, if we set it to regs->save_r4 here the child
134 * will have parents r4 after execve
136 uthread
->uu_rval
[1] = 0;
141 * ARM runtime will call cerror if the carry bit is set after a
142 * system call, so clear it here for the common case of success.
144 arm_clear_syscall_error(state
);
147 if (do_count_syscalls
> 0) {
148 syscalls_log
[code
]++;
151 pid
= proc_pid(proc
);
154 uthread
->uu_iocount
= 0;
155 uthread
->uu_vpindex
= 0;
157 unix_syscall_kprintf("code %d (pid %d - %s, tid %lld)\n", code
,
158 pid
, proc
->p_comm
, thread_tid(current_thread()));
160 AUDIT_SYSCALL_ENTER(code
, proc
, uthread
);
161 error
= (*(callp
->sy_call
)) (proc
, &uthread
->uu_arg
[0], &(uthread
->uu_rval
[0]));
162 AUDIT_SYSCALL_EXIT(code
, proc
, uthread
, error
);
164 unix_syscall_kprintf("code %d, error %d, results %x, %x (pid %d - %s, tid %lld)\n", code
, error
,
165 uthread
->uu_rval
[0], uthread
->uu_rval
[1],
166 pid
, get_bsdtask_info(current_task()) ? proc
->p_comm
: "unknown" , thread_tid(current_thread()));
169 if (uthread
->uu_iocount
) {
170 printf("system call returned with uu_iocount != 0");
174 uthread
->t_dtrace_errno
= error
;
175 #endif /* CONFIG_DTRACE */
176 #if DEBUG || DEVELOPMENT
177 kern_allocation_name_t
178 prior __assert_only
= thread_set_allocation_name(NULL
);
179 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
180 #endif /* DEBUG || DEVELOPMENT */
182 arm_prepare_syscall_return(callp
, state
, uthread
, error
);
184 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
185 uthread
->syscall_code
= 0;
187 if (uthread
->uu_lowpri_window
) {
189 * task is marked as a low priority I/O type
190 * and the I/O we issued while in this system call
191 * collided with normal I/O operations... we'll
192 * delay in order to mitigate the impact of this
193 * task on the normal operation of the system
195 throttle_lowpri_io(1);
197 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
198 if (kdebug_enable
&& !code_is_kdebug_trace(code
)) {
199 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
200 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
201 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], pid
, 0);
206 if (__improbable(uthread_get_proc_refcount(uthread
) != 0)) {
207 panic("system call returned with uu_proc_refcount != 0");
212 thread_exception_return();
217 unix_syscall_return(int error
)
220 struct uthread
*uthread
;
222 struct arm_saved_state
*regs
;
224 struct sysent
*callp
;
226 #define unix_syscall_return_kprintf(x...) /* kprintf("unix_syscall_retur
229 thread_act
= current_thread();
230 proc
= current_proc();
231 uthread
= get_bsdthread_info(thread_act
);
233 regs
= find_user_regs(thread_act
);
234 code
= uthread
->syscall_code
;
235 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
238 if (callp
->sy_call
== dtrace_systrace_syscall
)
239 dtrace_systrace_syscall_return( code
, error
, uthread
->uu_rval
);
240 #endif /* CONFIG_DTRACE */
241 #if DEBUG || DEVELOPMENT
242 kern_allocation_name_t
243 prior __assert_only
= thread_set_allocation_name(NULL
);
244 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
245 #endif /* DEBUG || DEVELOPMENT */
247 AUDIT_SYSCALL_EXIT(code
, proc
, uthread
, error
);
250 * Get index into sysent table
252 arm_prepare_syscall_return(callp
, regs
, uthread
, error
);
254 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
255 uthread
->syscall_code
= 0;
257 if (uthread
->uu_lowpri_window
) {
259 * task is marked as a low priority I/O type
260 * and the I/O we issued while in this system call
261 * collided with normal I/O operations... we'll
262 * delay in order to mitigate the impact of this
263 * task on the normal operation of the system
265 throttle_lowpri_io(1);
267 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
268 if (kdebug_enable
&& !code_is_kdebug_trace(code
)) {
269 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
270 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
271 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], proc
->p_pid
, 0);
275 thread_exception_return();
280 arm_prepare_u32_syscall_return(struct sysent
*callp
, arm_saved_state_t
*regs
, uthread_t uthread
, int error
)
282 assert(is_saved_state32(regs
));
284 arm_saved_state32_t
*ss32
= saved_state32(regs
);
286 if (error
== ERESTART
) {
288 } else if (error
!= EJUSTRETURN
) {
290 ss32
->save_r0
= error
;
292 /* set the carry bit to execute cerror routine */
293 ss32
->cpsr
|= PSR_CF
;
294 unix_syscall_return_kprintf("error: setting carry to trigger cerror call\n");
295 } else { /* (not error) */
296 switch (callp
->sy_return_type
) {
297 case _SYSCALL_RET_INT_T
:
298 case _SYSCALL_RET_UINT_T
:
299 case _SYSCALL_RET_OFF_T
:
300 case _SYSCALL_RET_ADDR_T
:
301 case _SYSCALL_RET_SIZE_T
:
302 case _SYSCALL_RET_SSIZE_T
:
303 case _SYSCALL_RET_UINT64_T
:
304 ss32
->save_r0
= uthread
->uu_rval
[0];
305 ss32
->save_r1
= uthread
->uu_rval
[1];
307 case _SYSCALL_RET_NONE
:
312 panic("unix_syscall: unknown return type");
317 /* else (error == EJUSTRETURN) { nothing } */
322 arm_trace_u32_unix_syscall(int code
, arm_saved_state32_t
*regs
)
324 boolean_t indirect
= (regs
->save_r12
== 0);
326 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
327 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
328 regs
->save_r1
, regs
->save_r2
, regs
->save_r3
, regs
->save_r4
, 0);
330 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
331 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
332 regs
->save_r0
, regs
->save_r1
, regs
->save_r2
, regs
->save_r3
, 0);
336 arm_clear_u32_syscall_error(arm_saved_state32_t
*regs
)
338 regs
->cpsr
&= ~PSR_CF
;
344 arm_get_syscall_args(uthread_t uthread
, struct arm_saved_state
*state
, struct sysent
*callp
)
346 assert(is_saved_state32(state
));
347 return arm_get_u32_syscall_args(uthread
, saved_state32(state
), callp
);
350 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
352 * For armv7k, the alignment constraints of the ABI mean we don't know how the userspace
353 * arguments are arranged without knowing the the prototype of the syscall. So we use mungers
354 * to marshal the userspace data into the uu_arg. This also means we need the same convention
355 * as mach syscalls. That means we use r8 to pass arguments in the BSD case as well.
358 arm_get_u32_syscall_args(uthread_t uthread
, arm_saved_state32_t
*regs
, struct sysent
*callp
)
362 /* This check is probably not very useful since these both come from build-time */
363 if (callp
->sy_arg_bytes
> sizeof(uthread
->uu_arg
))
366 /* get the munger and use it to marshal in the data from userspace */
367 munger
= callp
->sy_arg_munge32
;
368 if (munger
== NULL
|| (callp
->sy_arg_bytes
== 0))
371 return munger(regs
, uthread
->uu_arg
);
375 * For an AArch32 kernel, where we know that we have only AArch32 userland,
376 * we do not do any munging (which is a little confusing, as it is a contrast
377 * to the i386 kernel, where, like the x86_64 kernel, we always munge
378 * arguments from a 32-bit userland out to 64-bit.
381 arm_get_u32_syscall_args(uthread_t uthread
, arm_saved_state32_t
*regs
, struct sysent
*callp
)
384 int flavor
= (regs
->save_r12
== 0 ? 1 : 0);
386 regparams
= (7 - flavor
); /* Indirect value consumes a register */
388 assert((unsigned) callp
->sy_arg_bytes
<= sizeof (uthread
->uu_arg
));
390 if (callp
->sy_arg_bytes
<= (sizeof(uint32_t) * regparams
)) {
392 * Seven arguments or less are passed in registers.
394 memcpy(&uthread
->uu_arg
[0], ®s
->r
[flavor
], callp
->sy_arg_bytes
);
395 } else if (callp
->sy_arg_bytes
<= sizeof(uthread
->uu_arg
)) {
397 * In this case, we composite - take the first args from registers,
398 * the remainder from the stack (offset by the 7 regs therein).
400 unix_syscall_kprintf("%s: spillover...\n", __FUNCTION__
);
401 memcpy(&uthread
->uu_arg
[0] , ®s
->r
[flavor
], regparams
* sizeof(int));
402 if (copyin((user_addr_t
)regs
->sp
+ 7 * sizeof(int), (int *)&uthread
->uu_arg
[0] + regparams
,
403 (callp
->sy_arg_bytes
- (sizeof(uint32_t) * regparams
))) != 0) {
415 arm_get_syscall_number(struct arm_saved_state
*regs
)
417 if (regs
->save_r12
!= 0) {
418 return regs
->save_r12
;
420 return regs
->save_r0
;
425 arm_prepare_syscall_return(struct sysent
*callp
, struct arm_saved_state
*state
, uthread_t uthread
, int error
)
427 assert(is_saved_state32(state
));
428 arm_prepare_u32_syscall_return(callp
, state
, uthread
, error
);
432 arm_trace_unix_syscall(int code
, struct arm_saved_state
*state
)
434 assert(is_saved_state32(state
));
435 arm_trace_u32_unix_syscall(code
, saved_state32(state
));
439 arm_clear_syscall_error(struct arm_saved_state
* state
)
441 assert(is_saved_state32(state
));
442 arm_clear_u32_syscall_error(saved_state32(state
));
445 #elif defined(__arm64__)
446 static void arm_prepare_u64_syscall_return(struct sysent
*, arm_saved_state_t
*, uthread_t
, int);
447 static int arm_get_u64_syscall_args(uthread_t
, arm_saved_state64_t
*, struct sysent
*);
450 arm_get_syscall_args(uthread_t uthread
, struct arm_saved_state
*state
, struct sysent
*callp
)
452 if (is_saved_state32(state
)) {
453 return arm_get_u32_syscall_args(uthread
, saved_state32(state
), callp
);
455 return arm_get_u64_syscall_args(uthread
, saved_state64(state
), callp
);
460 * 64-bit: all arguments in registers. We're willing to use x9, a temporary
461 * register per the ABI, to pass an argument to the kernel for one case,
462 * an indirect syscall with 8 arguments. No munging required, as all arguments
463 * are in 64-bit wide registers already.
466 arm_get_u64_syscall_args(uthread_t uthread
, arm_saved_state64_t
*regs
, struct sysent
*callp
)
470 #if CONFIG_REQUIRES_U32_MUNGING
474 indirect_offset
= (regs
->x
[ARM64_SYSCALL_CODE_REG_NUM
] == 0) ? 1 : 0;
477 * Everything should fit in registers for now.
479 if (callp
->sy_narg
> (int)(sizeof(uthread
->uu_arg
) / sizeof(uthread
->uu_arg
[0]))) {
483 memcpy(&uthread
->uu_arg
[0], ®s
->x
[indirect_offset
], callp
->sy_narg
* sizeof(uint64_t));
485 #if CONFIG_REQUIRES_U32_MUNGING
487 * The indirect system call interface is vararg based. For armv7k, arm64_32,
488 * and arm64, this means we simply lay the values down on the stack, padded to
489 * a width multiple (4 bytes for armv7k and arm64_32, 8 bytes for arm64).
490 * The arm64(_32) stub for syscall will load this data into the registers and
491 * then trap. This gives us register state that corresponds to what we would
492 * expect from a armv7 task, so in this particular case we need to munge the
495 * TODO: Is there a cleaner way to do this check? What we're actually
496 * interested in is whether the task is arm64_32. We don't appear to guarantee
497 * that uu_proc is populated here, which is why this currently uses the
500 mungerp
= callp
->sy_arg_munge32
;
501 assert(uthread
->uu_thread
);
503 if (indirect_offset
&& !ml_thread_is64bit(uthread
->uu_thread
)) {
504 (*mungerp
)(&uthread
->uu_arg
[0]);
511 * When the kernel is running AArch64, munge arguments from 32-bit
512 * userland out to 64-bit.
514 * flavor == 1 indicates an indirect syscall.
517 arm_get_u32_syscall_args(uthread_t uthread
, arm_saved_state32_t
*regs
, struct sysent
*callp
)
520 #if CONFIG_REQUIRES_U32_MUNGING
523 #error U32 syscalls on ARM64 kernel requires munging
525 int flavor
= (regs
->save_r12
== 0 ? 1 : 0);
527 regparams
= (7 - flavor
); /* Indirect value consumes a register */
529 assert((unsigned) callp
->sy_arg_bytes
<= sizeof (uthread
->uu_arg
));
531 if (callp
->sy_arg_bytes
<= (sizeof(uint32_t) * regparams
)) {
533 * Seven arguments or less are passed in registers.
535 memcpy(&uthread
->uu_arg
[0], ®s
->r
[flavor
], callp
->sy_arg_bytes
);
536 } else if (callp
->sy_arg_bytes
<= sizeof(uthread
->uu_arg
)) {
538 * In this case, we composite - take the first args from registers,
539 * the remainder from the stack (offset by the 7 regs therein).
541 unix_syscall_kprintf("%s: spillover...\n", __FUNCTION__
);
542 memcpy(&uthread
->uu_arg
[0] , ®s
->r
[flavor
], regparams
* sizeof(int));
543 if (copyin((user_addr_t
)regs
->sp
+ 7 * sizeof(int), (int *)&uthread
->uu_arg
[0] + regparams
,
544 (callp
->sy_arg_bytes
- (sizeof(uint32_t) * regparams
))) != 0) {
551 #if CONFIG_REQUIRES_U32_MUNGING
553 mungerp
= callp
->sy_arg_munge32
;
554 if (mungerp
!= NULL
) {
555 (*mungerp
)(&uthread
->uu_arg
[0]);
564 arm_get_syscall_number(struct arm_saved_state
*state
)
566 if (is_saved_state32(state
)) {
567 if (saved_state32(state
)->save_r12
!= 0) {
568 return saved_state32(state
)->save_r12
;
570 return saved_state32(state
)->save_r0
;
573 if (saved_state64(state
)->x
[ARM64_SYSCALL_CODE_REG_NUM
] != 0) {
574 return saved_state64(state
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
576 return saved_state64(state
)->x
[0];
583 arm_prepare_syscall_return(struct sysent
*callp
, struct arm_saved_state
*state
, uthread_t uthread
, int error
)
585 if (is_saved_state32(state
)) {
586 arm_prepare_u32_syscall_return(callp
, state
, uthread
, error
);
588 arm_prepare_u64_syscall_return(callp
, state
, uthread
, error
);
593 arm_prepare_u64_syscall_return(struct sysent
*callp
, arm_saved_state_t
*regs
, uthread_t uthread
, int error
)
595 assert(is_saved_state64(regs
));
597 arm_saved_state64_t
*ss64
= saved_state64(regs
);
599 if (error
== ERESTART
) {
601 } else if (error
!= EJUSTRETURN
) {
606 * Set the carry bit to execute cerror routine.
607 * ARM64_TODO: should we have a separate definition?
608 * The bits are the same.
610 ss64
->cpsr
|= PSR_CF
;
611 unix_syscall_return_kprintf("error: setting carry to trigger cerror call\n");
612 } else { /* (not error) */
613 switch (callp
->sy_return_type
) {
614 case _SYSCALL_RET_INT_T
:
615 ss64
->x
[0] = uthread
->uu_rval
[0];
616 ss64
->x
[1] = uthread
->uu_rval
[1];
618 case _SYSCALL_RET_UINT_T
:
619 ss64
->x
[0] = (u_int
)uthread
->uu_rval
[0];
620 ss64
->x
[1] = (u_int
)uthread
->uu_rval
[1];
622 case _SYSCALL_RET_OFF_T
:
623 case _SYSCALL_RET_ADDR_T
:
624 case _SYSCALL_RET_SIZE_T
:
625 case _SYSCALL_RET_SSIZE_T
:
626 case _SYSCALL_RET_UINT64_T
:
627 ss64
->x
[0] = *((uint64_t *)(&uthread
->uu_rval
[0]));
630 case _SYSCALL_RET_NONE
:
633 panic("unix_syscall: unknown return type");
638 /* else (error == EJUSTRETURN) { nothing } */
643 arm_trace_u64_unix_syscall(int code
, arm_saved_state64_t
*regs
)
645 boolean_t indirect
= (regs
->x
[ARM64_SYSCALL_CODE_REG_NUM
] == 0);
647 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
648 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
649 regs
->x
[1], regs
->x
[2], regs
->x
[3], regs
->x
[4], 0);
651 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
652 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
653 regs
->x
[0], regs
->x
[1], regs
->x
[2], regs
->x
[3], 0);
657 arm_trace_unix_syscall(int code
, struct arm_saved_state
*state
)
659 if (is_saved_state32(state
)) {
660 arm_trace_u32_unix_syscall(code
, saved_state32(state
));
662 arm_trace_u64_unix_syscall(code
, saved_state64(state
));
667 arm_clear_u64_syscall_error(arm_saved_state64_t
*regs
)
670 * ARM64_TODO: should we have a separate definition?
671 * The bits are the same.
673 regs
->cpsr
&= ~PSR_CF
;
677 arm_clear_syscall_error(struct arm_saved_state
* state
)
679 if (is_saved_state32(state
)) {
680 arm_clear_u32_syscall_error(saved_state32(state
));
682 arm_clear_u64_syscall_error(saved_state64(state
));
687 #error Unknown architecture.