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
;
186 if (uthread
->uu_lowpri_window
) {
188 * task is marked as a low priority I/O type
189 * and the I/O we issued while in this system call
190 * collided with normal I/O operations... we'll
191 * delay in order to mitigate the impact of this
192 * task on the normal operation of the system
194 throttle_lowpri_io(1);
196 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
197 if (kdebug_enable
&& !code_is_kdebug_trace(code
)) {
198 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
199 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
200 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], pid
, 0);
205 if (__improbable(uthread_get_proc_refcount(uthread
) != 0)) {
206 panic("system call returned with uu_proc_refcount != 0");
211 thread_exception_return();
216 unix_syscall_return(int error
)
219 struct uthread
*uthread
;
221 struct arm_saved_state
*regs
;
223 struct sysent
*callp
;
225 #define unix_syscall_return_kprintf(x...) /* kprintf("unix_syscall_retur
228 thread_act
= current_thread();
229 proc
= current_proc();
230 uthread
= get_bsdthread_info(thread_act
);
232 regs
= find_user_regs(thread_act
);
233 code
= uthread
->syscall_code
;
234 callp
= (code
>= nsysent
) ? &sysent
[SYS_invalid
] : &sysent
[code
];
237 if (callp
->sy_call
== dtrace_systrace_syscall
)
238 dtrace_systrace_syscall_return( code
, error
, uthread
->uu_rval
);
239 #endif /* CONFIG_DTRACE */
240 #if DEBUG || DEVELOPMENT
241 kern_allocation_name_t
242 prior __assert_only
= thread_set_allocation_name(NULL
);
243 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
244 #endif /* DEBUG || DEVELOPMENT */
246 AUDIT_SYSCALL_EXIT(code
, proc
, uthread
, error
);
249 * Get index into sysent table
251 arm_prepare_syscall_return(callp
, regs
, uthread
, error
);
253 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
255 if (uthread
->uu_lowpri_window
) {
257 * task is marked as a low priority I/O type
258 * and the I/O we issued while in this system call
259 * collided with normal I/O operations... we'll
260 * delay in order to mitigate the impact of this
261 * task on the normal operation of the system
263 throttle_lowpri_io(1);
265 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
266 if (kdebug_enable
&& !code_is_kdebug_trace(code
)) {
267 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
268 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
269 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], proc
->p_pid
, 0);
273 thread_exception_return();
278 arm_prepare_u32_syscall_return(struct sysent
*callp
, arm_saved_state_t
*regs
, uthread_t uthread
, int error
)
280 assert(is_saved_state32(regs
));
282 arm_saved_state32_t
*ss32
= saved_state32(regs
);
284 if (error
== ERESTART
) {
286 } else if (error
!= EJUSTRETURN
) {
288 ss32
->save_r0
= error
;
290 /* set the carry bit to execute cerror routine */
291 ss32
->cpsr
|= PSR_CF
;
292 unix_syscall_return_kprintf("error: setting carry to trigger cerror call\n");
293 } else { /* (not error) */
294 switch (callp
->sy_return_type
) {
295 case _SYSCALL_RET_INT_T
:
296 case _SYSCALL_RET_UINT_T
:
297 case _SYSCALL_RET_OFF_T
:
298 case _SYSCALL_RET_ADDR_T
:
299 case _SYSCALL_RET_SIZE_T
:
300 case _SYSCALL_RET_SSIZE_T
:
301 case _SYSCALL_RET_UINT64_T
:
302 ss32
->save_r0
= uthread
->uu_rval
[0];
303 ss32
->save_r1
= uthread
->uu_rval
[1];
305 case _SYSCALL_RET_NONE
:
310 panic("unix_syscall: unknown return type");
315 /* else (error == EJUSTRETURN) { nothing } */
320 arm_trace_u32_unix_syscall(int code
, arm_saved_state32_t
*regs
)
322 boolean_t indirect
= (regs
->save_r12
== 0);
324 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
325 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
326 regs
->save_r1
, regs
->save_r2
, regs
->save_r3
, regs
->save_r4
, 0);
328 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
329 BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
330 regs
->save_r0
, regs
->save_r1
, regs
->save_r2
, regs
->save_r3
, 0);
334 arm_clear_u32_syscall_error(arm_saved_state32_t
*regs
)
336 regs
->cpsr
&= ~PSR_CF
;
342 arm_get_syscall_args(uthread_t uthread
, struct arm_saved_state
*state
, struct sysent
*callp
)
344 assert(is_saved_state32(state
));
345 return arm_get_u32_syscall_args(uthread
, saved_state32(state
), callp
);
348 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
350 * For armv7k, the alignment constraints of the ABI mean we don't know how the userspace
351 * arguments are arranged without knowing the the prototype of the syscall. So we use mungers
352 * to marshal the userspace data into the uu_arg. This also means we need the same convention
353 * as mach syscalls. That means we use r8 to pass arguments in the BSD case as well.
356 arm_get_u32_syscall_args(uthread_t uthread
, arm_saved_state32_t
*regs
, struct sysent
*callp
)
360 /* This check is probably not very useful since these both come from build-time */
361 if (callp
->sy_arg_bytes
> sizeof(uthread
->uu_arg
))
364 /* get the munger and use it to marshal in the data from userspace */
365 munger
= callp
->sy_arg_munge32
;
366 if (munger
== NULL
|| (callp
->sy_arg_bytes
== 0))
369 return munger(regs
, uthread
->uu_arg
);
373 * For an AArch32 kernel, where we know that we have only AArch32 userland,
374 * we do not do any munging (which is a little confusing, as it is a contrast
375 * to the i386 kernel, where, like the x86_64 kernel, we always munge
376 * arguments from a 32-bit userland out to 64-bit.
379 arm_get_u32_syscall_args(uthread_t uthread
, arm_saved_state32_t
*regs
, struct sysent
*callp
)
382 int flavor
= (regs
->save_r12
== 0 ? 1 : 0);
384 regparams
= (7 - flavor
); /* Indirect value consumes a register */
386 assert((unsigned) callp
->sy_arg_bytes
<= sizeof (uthread
->uu_arg
));
388 if (callp
->sy_arg_bytes
<= (sizeof(uint32_t) * regparams
)) {
390 * Seven arguments or less are passed in registers.
392 memcpy(&uthread
->uu_arg
[0], ®s
->r
[flavor
], callp
->sy_arg_bytes
);
393 } else if (callp
->sy_arg_bytes
<= sizeof(uthread
->uu_arg
)) {
395 * In this case, we composite - take the first args from registers,
396 * the remainder from the stack (offset by the 7 regs therein).
398 unix_syscall_kprintf("%s: spillover...\n", __FUNCTION__
);
399 memcpy(&uthread
->uu_arg
[0] , ®s
->r
[flavor
], regparams
* sizeof(int));
400 if (copyin((user_addr_t
)regs
->sp
+ 7 * sizeof(int), (int *)&uthread
->uu_arg
[0] + regparams
,
401 (callp
->sy_arg_bytes
- (sizeof(uint32_t) * regparams
))) != 0) {
413 arm_get_syscall_number(struct arm_saved_state
*regs
)
415 if (regs
->save_r12
!= 0) {
416 return regs
->save_r12
;
418 return regs
->save_r0
;
423 arm_prepare_syscall_return(struct sysent
*callp
, struct arm_saved_state
*state
, uthread_t uthread
, int error
)
425 assert(is_saved_state32(state
));
426 arm_prepare_u32_syscall_return(callp
, state
, uthread
, error
);
430 arm_trace_unix_syscall(int code
, struct arm_saved_state
*state
)
432 assert(is_saved_state32(state
));
433 arm_trace_u32_unix_syscall(code
, saved_state32(state
));
437 arm_clear_syscall_error(struct arm_saved_state
* state
)
439 assert(is_saved_state32(state
));
440 arm_clear_u32_syscall_error(saved_state32(state
));
443 #elif defined(__arm64__)
444 static void arm_prepare_u64_syscall_return(struct sysent
*, arm_saved_state_t
*, uthread_t
, int);
445 static int arm_get_u64_syscall_args(uthread_t
, arm_saved_state64_t
*, struct sysent
*);
448 arm_get_syscall_args(uthread_t uthread
, struct arm_saved_state
*state
, struct sysent
*callp
)
450 if (is_saved_state32(state
)) {
451 return arm_get_u32_syscall_args(uthread
, saved_state32(state
), callp
);
453 return arm_get_u64_syscall_args(uthread
, saved_state64(state
), callp
);
458 * 64-bit: all arguments in registers. We're willing to use x9, a temporary
459 * register per the ABI, to pass an argument to the kernel for one case,
460 * an indirect syscall with 8 arguments. No munging required, as all arguments
461 * are in 64-bit wide registers already.
464 arm_get_u64_syscall_args(uthread_t uthread
, arm_saved_state64_t
*regs
, struct sysent
*callp
)
466 int indirect_offset
, regparams
;
468 #if CONFIG_REQUIRES_U32_MUNGING
472 indirect_offset
= (regs
->x
[ARM64_SYSCALL_CODE_REG_NUM
] == 0) ? 1 : 0;
473 regparams
= 9 - indirect_offset
;
476 * Everything should fit in registers for now.
478 assert(callp
->sy_narg
<= 8);
479 if (callp
->sy_narg
> regparams
) {
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.