]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/bsd_i386.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / i386 / bsd_i386.c
index d7caa5a15534c7aaeb1a87d892e681e2a4ac390d..ad24633c2ee2c9b00bf74ce5c49946152f0a72f6 100644 (file)
@@ -1,21 +1,24 @@
 /*
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  *
  * @APPLE_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.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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. 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,
  * 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_LICENSE_HEADER_END@
  */
@@ -28,7 +31,6 @@
 #include <mach/kern_return.h>
 #include <mach/thread_status.h>
 #include <mach/vm_param.h>
 #include <mach/kern_return.h>
 #include <mach/thread_status.h>
 #include <mach/vm_param.h>
-#include <mach/rpc.h>
 
 #include <kern/counters.h>
 #include <kern/cpu_data.h>
 
 #include <kern/counters.h>
 #include <kern/cpu_data.h>
@@ -54,7 +56,9 @@
 #include <i386/iopb_entries.h>
 #include <i386/machdep_call.h>
 
 #include <i386/iopb_entries.h>
 #include <i386/machdep_call.h>
 
-#define       USRSTACK        0xc0000000
+#include <sys/syscall.h>
+#include <sys/ktrace.h>
+struct proc;
 
 kern_return_t
 thread_userstack(
 
 kern_return_t
 thread_userstack(
@@ -111,22 +115,18 @@ thread_userstack(
        i386_thread_state_t *state25;
        vm_offset_t     uesp;
 
        i386_thread_state_t *state25;
        vm_offset_t     uesp;
 
-        /*
-         * Set a default.
-         */
-        if (*user_stack == 0)
-                *user_stack = USRSTACK;
-
         if (customstack)
                        *customstack = 0;
         if (customstack)
                        *customstack = 0;
+
         switch (flavor) {
        case i386_THREAD_STATE: /* FIXME */
                 state25 = (i386_thread_state_t *) tstate;
         switch (flavor) {
        case i386_THREAD_STATE: /* FIXME */
                 state25 = (i386_thread_state_t *) tstate;
-                *user_stack = state25->esp ? state25->esp : USRSTACK;
-                               if (customstack && state25->esp)
-                                               *customstack = 1;
-                               else
-                                               *customstack = 0;
+               if (state25->esp)
+                       *user_stack = state25->esp;
+               if (customstack && state25->esp)
+                       *customstack = 1;
+               else
+                       *customstack = 0;
                break;
 
         case i386_NEW_THREAD_STATE:
                break;
 
         case i386_NEW_THREAD_STATE:
@@ -137,14 +137,13 @@ thread_userstack(
                        uesp = state->uesp;
                }
 
                        uesp = state->uesp;
                }
 
-                /*
-                 * If a valid user stack is specified, use it.
-                 */
-                *user_stack = uesp ? uesp : USRSTACK;
-                               if (customstack && uesp)
-                                               *customstack = 1;
-                               else
-                                               *customstack = 0;
+                /* If a valid user stack is specified, use it. */
+               if (uesp)
+                       *user_stack = uesp;
+               if (customstack && uesp)
+                       *customstack = 1;
+               else
+                       *customstack = 0;
                 break;
         default :
                 return (KERN_INVALID_ARGUMENT);
                 break;
         default :
                 return (KERN_INVALID_ARGUMENT);
@@ -302,6 +301,7 @@ struct sysent {             /* system call table */
        unsigned long           (*sy_call)(void *, void *, int *);      /* implementing function */
 };
 
        unsigned long           (*sy_call)(void *, void *, int *);      /* implementing function */
 };
 
+#define NO_FUNNEL 0
 #define KERNEL_FUNNEL 1
 #define NETWORK_FUNNEL 2
 
 #define KERNEL_FUNNEL 1
 #define NETWORK_FUNNEL 2
 
@@ -310,26 +310,39 @@ extern funnel_t * network_flock;
 
 extern struct sysent sysent[];
 
 
 extern struct sysent sysent[];
 
-
 int set_bsduthreadargs (thread_act_t, struct i386_saved_state *, void *);
 
 void * get_bsduthreadarg(thread_act_t);
 
 void unix_syscall(struct i386_saved_state *);
 
 int set_bsduthreadargs (thread_act_t, struct i386_saved_state *, void *);
 
 void * get_bsduthreadarg(thread_act_t);
 
 void unix_syscall(struct i386_saved_state *);
 
-/* USED ONLY FROM VFORK/EXIT */
 void
 unix_syscall_return(int error)
 {
     thread_act_t               thread;
        volatile int *rval;
        struct i386_saved_state *regs;
 void
 unix_syscall_return(int error)
 {
     thread_act_t               thread;
        volatile int *rval;
        struct i386_saved_state *regs;
+       struct proc *p;
+       struct proc *current_proc();
+       unsigned short code;
+       vm_offset_t params;
+       struct sysent *callp;
+       extern int nsysent;
 
     thread = current_act();
     rval = (int *)get_bsduthreadrval(thread);
 
     thread = current_act();
     rval = (int *)get_bsduthreadrval(thread);
+       p = current_proc();
 
        regs = USER_REGS(thread);
 
 
        regs = USER_REGS(thread);
 
+       /* reconstruct code for tracing before blasting eax */
+       code = regs->eax;
+       params = (vm_offset_t) ((caddr_t)regs->uesp + sizeof (int));
+       callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
+       if (callp == sysent) {
+         code = fuword(params);
+       }
+
        if (error == ERESTART) {
                regs->eip -= 7;
        }
        if (error == ERESTART) {
                regs->eip -= 7;
        }
@@ -344,7 +357,15 @@ unix_syscall_return(int error)
                } 
        }
 
                } 
        }
 
-    (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
+       ktrsysret(p, code, error, rval[0], callp->sy_funnel);
+
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
+               error, rval[0], rval[1], 0, 0);
+
+       if (callp->sy_funnel != NO_FUNNEL) {
+               assert(thread_funnel_get() == THR_FUNNEL_NULL);
+               (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
+       }
 
     thread_exception_return();
     /* NOTREACHED */
 
     thread_exception_return();
     /* NOTREACHED */
@@ -355,14 +376,16 @@ void
 unix_syscall(struct i386_saved_state *regs)
 {
     thread_act_t               thread;
 unix_syscall(struct i386_saved_state *regs)
 {
     thread_act_t               thread;
-    void       *p, *vt; 
+    void       *vt; 
     unsigned short     code;
     struct sysent              *callp;
        int     nargs, error;
        volatile int *rval;
     unsigned short     code;
     struct sysent              *callp;
        int     nargs, error;
        volatile int *rval;
-        int funnel_type;
+       int funnel_type;
     vm_offset_t                params;
     extern int nsysent;
     vm_offset_t                params;
     extern int nsysent;
+       struct proc *p;
+       struct proc *current_proc();
 
     thread = current_act();
     p = current_proc();
 
     thread = current_act();
     p = current_proc();
@@ -391,17 +414,24 @@ unix_syscall(struct i386_saved_state *regs)
     rval[0] = 0;
     rval[1] = regs->edx;
 
     rval[0] = 0;
     rval[1] = regs->edx;
 
-         if(callp->sy_funnel == NETWORK_FUNNEL) {
-            (void) thread_funnel_set(network_flock, TRUE);
-          }
-        else {
-            (void) thread_funnel_set(kernel_flock, TRUE);
-         }
+       funnel_type = callp->sy_funnel;
+       if(funnel_type == KERNEL_FUNNEL)
+               (void) thread_funnel_set(kernel_flock, TRUE);
+       else if (funnel_type == NETWORK_FUNNEL)
+               (void) thread_funnel_set(network_flock, TRUE);
+       
    set_bsduthreadargs(thread, regs, NULL);
 
     if (callp->sy_narg > 8)
        panic("unix_syscall max arg count exceeded (%d)", callp->sy_narg);
 
    set_bsduthreadargs(thread, regs, NULL);
 
     if (callp->sy_narg > 8)
        panic("unix_syscall max arg count exceeded (%d)", callp->sy_narg);
 
+       ktrsyscall(p, code, callp->sy_narg, vt, funnel_type);
+
+       { 
+         int *ip = (int *)vt;
+         KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
+             *ip, *(ip+1), *(ip+2), *(ip+3), 0);
+       }
 
     error = (*(callp->sy_call))(p, (void *) vt, rval);
        
 
     error = (*(callp->sy_call))(p, (void *) vt, rval);
        
@@ -423,7 +453,13 @@ unix_syscall(struct i386_saved_state *regs)
                } 
        }
 
                } 
        }
 
-    (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
+       ktrsysret(p, code, error, rval[0], funnel_type);
+
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
+               error, rval[0], rval[1], 0, 0);
+
+       if(funnel_type != NO_FUNNEL)
+               (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
 
     thread_exception_return();
     /* NOTREACHED */
 
     thread_exception_return();
     /* NOTREACHED */
@@ -436,6 +472,8 @@ machdep_syscall( struct i386_saved_state *regs)
     int                                trapno, nargs;
     machdep_call_t             *entry;
     thread_t                   thread;
     int                                trapno, nargs;
     machdep_call_t             *entry;
     thread_t                   thread;
+       struct proc *p;
+       struct proc *current_proc();
     
     trapno = regs->eax;
     if (trapno < 0 || trapno >= machdep_call_count) {
     
     trapno = regs->eax;
     if (trapno < 0 || trapno >= machdep_call_count) {
@@ -481,7 +519,8 @@ machdep_syscall( struct i386_saved_state *regs)
     else
        regs->eax = (unsigned int)(*entry->routine)();
 
     else
        regs->eax = (unsigned int)(*entry->routine)();
 
-    (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
+       if (current_thread()->funnel_lock)
+               (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
 
     thread_exception_return();
     /* NOTREACHED */
 
     thread_exception_return();
     /* NOTREACHED */
@@ -513,7 +552,7 @@ mach25_syscall(struct i386_saved_state *regs)
 #endif /* MACH_BSD */
 
 #undef current_thread
 #endif /* MACH_BSD */
 
 #undef current_thread
-thread_act_t
+thread_t
 current_thread(void)
 {
   return(current_thread_fast());
 current_thread(void)
 {
   return(current_thread_fast());