#include <mach/mach_traps.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/assert.h>
#include <kern/clock.h>
#include <kern/locks.h>
#include <kern/sched_prim.h>
#include <mach/machine/thread_status.h>
#include <mach/thread_act.h>
#include <ppc/savearea.h>
#include <sys/kernel.h>
#include <sys/vm.h>
#include <sys/proc_internal.h>
#include <sys/syscall.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/kdebug.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/kauth.h>
#include <security/audit/audit.h>
#if CONFIG_DTRACE
extern int32_t dtrace_systrace_syscall(struct proc *, void *, int *);
extern void dtrace_systrace_syscall_return(unsigned short, int, int *);
#endif
extern void
unix_syscall(struct savearea *regs);
extern struct savearea *
find_user_regs(
thread_t act);
extern lck_spin_t * tz_slock;
void
unix_syscall(struct savearea *regs)
{
thread_t thread_act;
struct uthread *uthread;
struct proc *proc;
struct sysent *callp;
int error;
unsigned int code;
boolean_t flavor;
flavor = (((unsigned int)regs->save_r0) == 0)? 1: 0;
if (flavor)
code = regs->save_r3;
else
code = regs->save_r0;
if (kdebug_enable && (code != 180)) {
if (flavor)
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
regs->save_r4, regs->save_r5, regs->save_r6, regs->save_r7, 0);
else
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
regs->save_r3, regs->save_r4, regs->save_r5, regs->save_r6, 0);
}
thread_act = current_thread();
uthread = get_bsdthread_info(thread_act);
if (!(uthread->uu_flag & UT_VFORK))
proc = (struct proc *)get_bsdtask_info(current_task());
else
proc = current_proc();
if (proc == NULL) {
regs->save_r3 = (long long)EPERM;
regs->save_srr0 -= 4;
task_terminate_internal(current_task());
thread_exception_return();
}
kauth_cred_uthread_update(uthread, proc);
callp = (code >= NUM_SYSENT) ? &sysent[63] : &sysent[code];
if (callp->sy_narg != 0) {
void *regsp;
sy_munge_t *mungerp;
if (IS_64BIT_PROCESS(proc)) {
if (callp->sy_flags & UNSAFE_64BIT) {
callp = &sysent[63];
goto unsafe;
}
mungerp = callp->sy_arg_munge64;
}
else {
mungerp = callp->sy_arg_munge32;
}
if ( !flavor) {
regsp = (void *) ®s->save_r3;
} else {
if (callp->sy_narg > 7) {
callp = &sysent[63];
goto unsafe;
}
regsp = (void *) ®s->save_r4;
}
(*mungerp)(regsp, (void *) &uthread->uu_arg[0]);
}
unsafe:
uthread->uu_flag |= UT_NOTCANCELPT;
uthread->uu_rval[0] = 0;
uthread->uu_rval[1] = 0;
error = 0;
regs->save_srr0 += 4;
#ifdef JOE_DEBUG
uthread->uu_iocount = 0;
uthread->uu_vpindex = 0;
#endif
AUDIT_SYSCALL_ENTER(code, proc, uthread);
error = (*(callp->sy_call))(proc, (void *)uthread->uu_arg, &(uthread->uu_rval[0]));
AUDIT_SYSCALL_EXIT(code, proc, uthread, error);
#if CONFIG_MACF
mac_thread_userret(code, error, thread_act);
#endif
#ifdef JOE_DEBUG
if (uthread->uu_iocount)
printf("system call returned with uu_iocount != 0\n");
#endif
#if CONFIG_DTRACE
uthread->t_dtrace_errno = error;
#endif
regs = find_user_regs(thread_act);
if (error == ERESTART) {
regs->save_srr0 -= 8;
} else if (error != EJUSTRETURN) {
if (error) {
regs->save_r3 = (long long)error;
regs->save_srr0 -= 4;
} else {
switch (callp->sy_return_type) {
case _SYSCALL_RET_INT_T:
regs->save_r3 = uthread->uu_rval[0];
regs->save_r4 = uthread->uu_rval[1];
break;
case _SYSCALL_RET_UINT_T:
regs->save_r3 = ((u_int)uthread->uu_rval[0]);
regs->save_r4 = ((u_int)uthread->uu_rval[1]);
break;
case _SYSCALL_RET_OFF_T:
case _SYSCALL_RET_UINT64_T:
if (IS_64BIT_PROCESS(proc)) {
u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
regs->save_r3 = *retp;
regs->save_r4 = 0;
}
else {
regs->save_r3 = uthread->uu_rval[0];
regs->save_r4 = uthread->uu_rval[1];
}
break;
case _SYSCALL_RET_ADDR_T:
case _SYSCALL_RET_SIZE_T:
case _SYSCALL_RET_SSIZE_T:
{
user_addr_t *retp = (user_addr_t *)&uthread->uu_rval[0];
regs->save_r3 = *retp;
regs->save_r4 = 0;
}
break;
case _SYSCALL_RET_NONE:
break;
default:
panic("unix_syscall: unknown return type");
break;
}
}
}
uthread->uu_flag &= ~UT_NOTCANCELPT;
syscall_exit_funnelcheck();
if (uthread->uu_lowpri_window) {
throttle_lowpri_io(TRUE);
}
if (kdebug_enable && (code != 180)) {
if (callp->sy_return_type == _SYSCALL_RET_SSIZE_T)
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
error, uthread->uu_rval[1], 0, proc->p_pid, 0);
else
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
error, uthread->uu_rval[0], uthread->uu_rval[1], proc->p_pid, 0);
}
thread_exception_return();
}
void
unix_syscall_return(int error)
{
thread_t thread_act;
struct uthread *uthread;
struct proc *proc;
struct savearea *regs;
unsigned int code;
struct sysent *callp;
thread_act = current_thread();
proc = current_proc();
uthread = get_bsdthread_info(thread_act);
regs = find_user_regs(thread_act);
if (regs->save_r0 != 0)
code = regs->save_r0;
else
code = regs->save_r3;
callp = (code >= NUM_SYSENT) ? &sysent[63] : &sysent[code];
#if CONFIG_DTRACE
if (callp->sy_call == dtrace_systrace_syscall)
dtrace_systrace_syscall_return( code, error, uthread->uu_rval );
#endif
AUDIT_SYSCALL_EXIT(code, proc, uthread, error);
if (error == ERESTART) {
regs->save_srr0 -= 8;
} else if (error != EJUSTRETURN) {
if (error) {
regs->save_r3 = (long long)error;
regs->save_srr0 -= 4;
} else {
switch (callp->sy_return_type) {
case _SYSCALL_RET_INT_T:
regs->save_r3 = uthread->uu_rval[0];
regs->save_r4 = uthread->uu_rval[1];
break;
case _SYSCALL_RET_UINT_T:
regs->save_r3 = ((u_int)uthread->uu_rval[0]);
regs->save_r4 = ((u_int)uthread->uu_rval[1]);
break;
case _SYSCALL_RET_OFF_T:
case _SYSCALL_RET_UINT64_T:
if (IS_64BIT_PROCESS(proc)) {
u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
regs->save_r3 = *retp;
}
else {
regs->save_r3 = uthread->uu_rval[0];
regs->save_r4 = uthread->uu_rval[1];
}
break;
case _SYSCALL_RET_ADDR_T:
case _SYSCALL_RET_SIZE_T:
case _SYSCALL_RET_SSIZE_T:
{
u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
regs->save_r3 = *retp;
}
break;
case _SYSCALL_RET_NONE:
break;
default:
panic("unix_syscall: unknown return type");
break;
}
}
}
uthread->uu_flag &= ~UT_NOTCANCELPT;
syscall_exit_funnelcheck();
if (uthread->uu_lowpri_window) {
throttle_lowpri_io(TRUE);
}
if (kdebug_enable && (code != 180)) {
if (callp->sy_return_type == _SYSCALL_RET_SSIZE_T)
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
error, uthread->uu_rval[1], 0, proc->p_pid, 0);
else
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
error, uthread->uu_rval[0], uthread->uu_rval[1], proc->p_pid, 0);
}
thread_exception_return();
}
void
munge_lwww(
const void *in32,
void *out64)
{
const uint32_t *arg32;
uint64_t *arg64;
arg32 = (const uint32_t *) in32;
arg64 = (uint64_t *) out64;
arg64[3] = arg32[9];
arg64[2] = arg32[7];
arg64[1] = arg32[5];
arg64[0] = ((uint64_t) arg32[1]) << 32;
arg64[0] |= (uint64_t) arg32[3];
}
void
munge_lw(
const void *in32,
void *out64)
{
const uint32_t *arg32;
uint64_t *arg64;
arg32 = (const uint32_t *) in32;
arg64 = (uint64_t *) out64;
arg64[1] = arg32[5];
arg64[0] = ((uint64_t) arg32[1]) << 32;
arg64[0] |= (uint64_t) arg32[3];
}