]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/dev/ppc/systemcalls.c
xnu-1504.3.12.tar.gz
[apple/xnu.git] / bsd / dev / ppc / systemcalls.c
index 4ec9ebdf571c8a11e8cdebbb61cd7f59d2c9ae4e..973d35899420d9aaed1a1fc6fa358d33916b4daf 100644 (file)
@@ -1,25 +1,39 @@
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
+ */
+/*
+ * NOTICE: This file was modified by McAfee Research in 2004 to introduce
+ * support for mandatory and extensible security protections.  This notice
+ * is included in support of clause 2.2 (b) of the Apple Public License,
+ * Version 2.0.
  */
 
+#include <mach/mach_traps.h>
+
 #include <kern/task.h>
 #include <kern/thread.h>
 #include <kern/assert.h>
@@ -27,6 +41,7 @@
 #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/systm.h>
 #include <sys/user.h>
 #include <sys/errno.h>
-#include <sys/ktrace.h>
 #include <sys/kdebug.h>
 #include <sys/sysent.h>
 #include <sys/sysproto.h>
 #include <sys/kauth.h>
 
-#include <bsm/audit_kernel.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);
-void
-unix_syscall_return(int error);
 
 extern struct savearea * 
 find_user_regs(
        thread_t act);
 
-extern void enter_funnel_section(funnel_t *funnel_lock);
-extern void exit_funnel_section(void);
+extern lck_spin_t * tz_slock;
 
 /*
  * Function:   unix_syscall
@@ -71,10 +87,8 @@ unix_syscall(struct savearea *regs)
        struct proc                     *proc;
        struct sysent           *callp;
        int                                     error;
-       unsigned short          code;
+       unsigned int            code;
        boolean_t                       flavor;
-       int funnel_type;
-       unsigned int cancel_enable;
 
        flavor = (((unsigned int)regs->save_r0) == 0)? 1: 0;
 
@@ -113,20 +127,9 @@ unix_syscall(struct savearea       *regs)
         * Delayed binding of thread credential to process credential, if we
         * are not running with an explicitly set thread credential.
         */
-       if (uthread->uu_ucred != proc->p_ucred &&
-           (uthread->uu_flag & UT_SETUID) == 0) {
-               kauth_cred_t old = uthread->uu_ucred;
-               proc_lock(proc);
-               uthread->uu_ucred = proc->p_ucred;
-               kauth_cred_ref(uthread->uu_ucred);
-               proc_unlock(proc);
-               if (old != NOCRED)
-                       kauth_cred_rele(old);
-       }
-
-       uthread->uu_ar0 = (int *)regs;
+       kauth_cred_uthread_update(uthread, proc);
 
-       callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
+       callp = (code >= NUM_SYSENT) ? &sysent[63] : &sysent[code];
 
        if (callp->sy_narg != 0) {
                void            *regsp;
@@ -134,7 +137,7 @@ unix_syscall(struct savearea        *regs)
                
                if (IS_64BIT_PROCESS(proc)) {
                        /* XXX Turn 64 bit unsafe calls into nosys() */
-                       if (callp->sy_funnel & UNSAFE_64BIT) {
+                       if (callp->sy_flags & UNSAFE_64BIT) {
                                callp = &sysent[63];
                                goto unsafe;
                        }
@@ -158,27 +161,9 @@ unix_syscall(struct savearea       *regs)
        }
 
 unsafe:
-       cancel_enable = callp->sy_cancel;
        
-       if (cancel_enable == _SYSCALL_CANCEL_NONE) {
-                       uthread->uu_flag |= UT_NOTCANCELPT;
-       } else {
-               if((uthread->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
-                       if (cancel_enable == _SYSCALL_CANCEL_PRE) {
-                                       /* system call cancelled; return to handle cancellation */
-                                       regs->save_r3 = (long long)EINTR;
-                                       thread_exception_return();
-                                       /* NOTREACHED */
-                       } else {
-                        thread_abort_safely(thread_act);
-                       }
-               }
-       }
+       uthread->uu_flag |= UT_NOTCANCELPT;
 
-       funnel_type = (int)(callp->sy_funnel & FUNNEL_MASK);
-       if (funnel_type == KERNEL_FUNNEL) 
-                enter_funnel_section(kernel_flock);
-       
        uthread->uu_rval[0] = 0;
 
        /*
@@ -196,21 +181,26 @@ unsafe:
         */
        regs->save_srr0 += 4;
 
-       if (KTRPOINT(proc, KTR_SYSCALL))
-               ktrsyscall(proc, code, callp->sy_narg, uthread->uu_arg);
-
 #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(error, proc, uthread);
+       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)
-               joe_debug("system call returned with uu_iocount != 0");
+               printf("system call returned with uu_iocount != 0\n");
 #endif
+#if CONFIG_DTRACE
+       uthread->t_dtrace_errno = error;
+#endif /* CONFIG_DTRACE */
+
        regs = find_user_regs(thread_act);
 
        if (error == ERESTART) {
@@ -267,29 +257,12 @@ unsafe:
        /* else  (error == EJUSTRETURN) { nothing } */
 
 
-       if (KTRPOINT(proc, KTR_SYSRET)) {
-               switch(callp->sy_return_type) {
-               case _SYSCALL_RET_ADDR_T:
-               case _SYSCALL_RET_SIZE_T:
-               case _SYSCALL_RET_SSIZE_T:
-                       /*
-                        * Trace the value of the least significant bits,
-                        * until we can revise the ktrace API safely.
-                        */
-                       ktrsysret(proc, code, error, uthread->uu_rval[1]);
-                       break;
-               default:
-                       ktrsysret(proc, code, error, uthread->uu_rval[0]);
-                       break;
-               }
-       }
-
-       if (cancel_enable == _SYSCALL_CANCEL_NONE)
-                uthread->uu_flag &= ~UT_NOTCANCELPT;
+       uthread->uu_flag &= ~UT_NOTCANCELPT;
 
-       exit_funnel_section();
+       /* panic if funnel is held */
+       syscall_exit_funnelcheck();
 
-       if (uthread->uu_lowpri_delay) {
+       if (uthread->uu_lowpri_window) {
                /*
                 * task is marked as a low priority I/O type
                 * and the I/O we issued while in this system call
@@ -297,17 +270,16 @@ unsafe:
                 * delay in order to mitigate the impact of this
                 * task on the normal operation of the system
                 */
-               IOSleep(uthread->uu_lowpri_delay);
-               uthread->uu_lowpri_delay = 0;
+               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, 0, 0);
+                                             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], 0, 0);
+                                             error, uthread->uu_rval[0], uthread->uu_rval[1], proc->p_pid, 0);
        }
 
        thread_exception_return();
@@ -321,10 +293,8 @@ unix_syscall_return(int error)
        struct uthread                          *uthread;
        struct proc                                     *proc;
        struct savearea                         *regs;
-       unsigned short                          code;
+       unsigned int                            code;
        struct sysent                           *callp;
-       int funnel_type;
-       unsigned int cancel_enable;
 
        thread_act = current_thread();
        proc = current_proc();
@@ -337,7 +307,13 @@ unix_syscall_return(int error)
        else
                code = regs->save_r3;
 
-       callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
+       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 /* CONFIG_DTRACE */
+       AUDIT_SYSCALL_EXIT(code, proc, uthread, error);
 
        /*
         * Get index into sysent table
@@ -393,31 +369,13 @@ unix_syscall_return(int error)
        }
        /* else  (error == EJUSTRETURN) { nothing } */
 
-       if (KTRPOINT(proc, KTR_SYSRET)) {
-               switch(callp->sy_return_type) {
-               case _SYSCALL_RET_ADDR_T:
-               case _SYSCALL_RET_SIZE_T:
-               case _SYSCALL_RET_SSIZE_T:
-                       /*
-                        * Trace the value of the least significant bits,
-                        * until we can revise the ktrace API safely.
-                        */
-                       ktrsysret(proc, code, error, uthread->uu_rval[1]);
-                       break;
-               default:
-                       ktrsysret(proc, code, error, uthread->uu_rval[0]);
-                       break;
-               }
-       }
-
-       cancel_enable = callp->sy_cancel;
 
-       if (cancel_enable == _SYSCALL_CANCEL_NONE)
-                uthread->uu_flag &= ~UT_NOTCANCELPT;
+       uthread->uu_flag &= ~UT_NOTCANCELPT;
 
-       exit_funnel_section();
+       /* panic if funnel is held */
+       syscall_exit_funnelcheck();
 
-       if (uthread->uu_lowpri_delay) {
+       if (uthread->uu_lowpri_window) {
                /*
                 * task is marked as a low priority I/O type
                 * and the I/O we issued while in this system call
@@ -425,106 +383,51 @@ unix_syscall_return(int error)
                 * delay in order to mitigate the impact of this
                 * task on the normal operation of the system
                 */
-               IOSleep(uthread->uu_lowpri_delay);
-               uthread->uu_lowpri_delay = 0;
+               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, 0, 0);
+                                             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], 0, 0);
+                                             error, uthread->uu_rval[0], uthread->uu_rval[1], proc->p_pid, 0);
        }
 
        thread_exception_return();
        /* NOTREACHED */
 }
 
-/* 
- * Time of day and interval timer support.
- *
- * These routines provide the kernel entry points to get and set
- * the time-of-day and per-process interval timers.  Subroutines
- * here provide support for adding and subtracting timeval structures
- * and decrementing interval timers, optionally reloading the interval
- * timers when they expire.
- */
-/*  NOTE THIS implementation is for  ppc architectures only.
- *  It is infrequently called, since the commpage intercepts
- *  most calls in user mode.
- *
- * XXX Y2038 bug because of assumed return of 32 bit seconds value, and
- * XXX first parameter to clock_gettimeofday()
- */
-int
-ppc_gettimeofday(__unused struct proc *p, 
-                                register struct ppc_gettimeofday_args *uap, 
-                                register_t *retval)
+void
+munge_lwww(
+       const void      *in32,
+       void            *out64)
 {
-       int error = 0;
-       extern lck_spin_t * tz_slock;
-
-       if (uap->tp)
-               clock_gettimeofday(&retval[0], &retval[1]);
-       
-       if (uap->tzp) {
-               struct timezone ltz;
+       const uint32_t  *arg32;
+       uint64_t        *arg64;
 
-               lck_spin_lock(tz_slock);
-               ltz = tz;
-               lck_spin_unlock(tz_slock);
-               error = copyout((caddr_t)&ltz, uap->tzp, sizeof (tz));
-       }
-
-       return (error);
-}
+       arg32 = (const uint32_t *) in32;
+       arg64 = (uint64_t *) out64;
 
-#ifdef JOE_DEBUG
-joe_debug(char *p) {
-
-        printf("%s\n", p);
+       arg64[3] = arg32[9];    /* lwwW */
+       arg64[2] = arg32[7];    /* lwWw */
+       arg64[1] = arg32[5];    /* lWww */
+       arg64[0] = ((uint64_t) arg32[1]) << 32; /* Lwww (hi) */
+       arg64[0] |= (uint64_t) arg32[3];        /* Lwww (lo) */
 }
-#endif
 
-
-/* 
- * WARNING - this is a temporary workaround for binary compatibility issues
- * with anti-piracy software that relies on patching ptrace (3928003).
- * This KPI will be removed in the system release after Tiger.
- */
-uintptr_t temp_patch_ptrace(uintptr_t new_ptrace)
+void
+munge_lw(
+       const void      *in32,
+       void            *out64)
 {
-       struct sysent *         callp;
-       sy_call_t *                     old_ptrace;
+       const uint32_t  *arg32;
+       uint64_t        *arg64;
 
-       if (new_ptrace == 0)
-               return(0);
-               
-       enter_funnel_section(kernel_flock);
-       callp = &sysent[26];
-       old_ptrace = callp->sy_call;
-       
-       /* only allow one patcher of ptrace */
-       if (old_ptrace == (sy_call_t *) ptrace) {
-               callp->sy_call = (sy_call_t *) new_ptrace;
-       }
-       else {
-               old_ptrace = NULL;
-       }
-       exit_funnel_section( );
-       
-       return((uintptr_t)old_ptrace);
-}
+       arg32 = (const uint32_t *) in32;
+       arg64 = (uint64_t *) out64;
 
-void temp_unpatch_ptrace(void)
-{
-       struct sysent *         callp;
-               
-       enter_funnel_section(kernel_flock);
-       callp = &sysent[26];
-       callp->sy_call = (sy_call_t *) ptrace;
-       exit_funnel_section( );
-       
-       return;
+       arg64[1] = arg32[5];    /* lW */
+       arg64[0] = ((uint64_t) arg32[1]) << 32; /* Lw (hi) */
+       arg64[0] |= (uint64_t) arg32[3];        /* Lw (lo) */
 }