]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/bsd_i386.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / i386 / bsd_i386.c
index bdd39922a4da61b0bc7304201dc566e13128357c..896ebd976e46ee0b4dfe785bd360bb96b54038fc 100644 (file)
@@ -1,31 +1,37 @@
 /*
- * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2005 Apple Computer, 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@
  */
 #ifdef MACH_BSD
-#include <cpus.h>
 #include <mach_rt.h>
 #include <mach_debug.h>
 #include <mach_ldebug.h>
 
 #include <mach/kern_return.h>
+#include <mach/mach_traps.h>
 #include <mach/thread_status.h>
 #include <mach/vm_param.h>
 
@@ -34,7 +40,6 @@
 #include <kern/mach_param.h>
 #include <kern/task.h>
 #include <kern/thread.h>
-#include <kern/thread_swap.h>
 #include <kern/sched_prim.h>
 #include <kern/misc_protos.h>
 #include <kern/assert.h>
@@ -44,6 +49,8 @@
 #include <vm/vm_kern.h>
 #include <vm/pmap.h>
 
+#include <i386/cpu_data.h>
+#include <i386/cpu_number.h>
 #include <i386/thread.h>
 #include <i386/eflags.h>
 #include <i386/proc_reg.h>
 #include <i386/fpu.h>
 #include <i386/iopb_entries.h>
 #include <i386/machdep_call.h>
-
+#include <i386/misc_protos.h>
+#include <i386/cpu_data.h>
+#include <i386/cpu_number.h>
+#include <i386/mp_desc.h>
+#include <i386/vmparam.h>
+#include <i386/trap.h>
+#include <mach/i386/syscall_sw.h>
 #include <sys/syscall.h>
+#include <sys/kdebug.h>
 #include <sys/ktrace.h>
-struct proc;
+#include <sys/errno.h>
+#include <../bsd/sys/sysent.h>
+
+extern struct proc *current_proc(void);
+extern struct proc * kernproc;
 
 kern_return_t
 thread_userstack(
@@ -64,8 +82,8 @@ thread_userstack(
     int,
     thread_state_t,
     unsigned int,
-    vm_offset_t *,
-       int *
+    mach_vm_offset_t *,
+    int *
 );
 
 kern_return_t
@@ -74,12 +92,10 @@ thread_entrypoint(
     int,
     thread_state_t,
     unsigned int,
-    vm_offset_t *
+    mach_vm_offset_t *
 ); 
 
-struct i386_saved_state *
-get_user_regs(
-        thread_act_t);
+void * find_user_regs(thread_t);
 
 unsigned int get_msr_exportmask(void);
 
@@ -90,6 +106,8 @@ unsigned int get_msr_rbits(void);
 kern_return_t
 thread_compose_cthread_desc(unsigned int addr, pcb_t pcb);
 
+void IOSleep(int);
+
 /*
  * thread_userstack:
  *
@@ -98,106 +116,99 @@ thread_compose_cthread_desc(unsigned int addr, pcb_t pcb);
  */
 kern_return_t
 thread_userstack(
-    thread_t            thread,
-    int                 flavor,
-    thread_state_t      tstate,
-    unsigned int        count,
-    vm_offset_t         *user_stack,
-       int                                     *customstack
-)
+                __unused thread_t   thread,
+                int                 flavor,
+                thread_state_t      tstate,
+                __unused unsigned int        count,
+                user_addr_t    *user_stack,
+                int            *customstack
+                )
 {
-        struct i386_saved_state *state;
-       i386_thread_state_t *state25;
-       vm_offset_t     uesp;
-
         if (customstack)
-                       *customstack = 0;
+               *customstack = 0;
 
         switch (flavor) {
-       case i386_THREAD_STATE: /* FIXME */
-                state25 = (i386_thread_state_t *) tstate;
+           case OLD_i386_THREAD_STATE:
+           case x86_THREAD_STATE32:
+           {
+               x86_thread_state32_t *state25;
+
+                state25 = (x86_thread_state32_t *) tstate;
+
                if (state25->esp)
                        *user_stack = state25->esp;
+               else 
+                       *user_stack = VM_USRSTACK32;
                if (customstack && state25->esp)
                        *customstack = 1;
                else
                        *customstack = 0;
                break;
+           }
 
-        case i386_NEW_THREAD_STATE:
-                if (count < i386_NEW_THREAD_STATE_COUNT)
-                        return (KERN_INVALID_ARGUMENT);
-               else {
-                       state = (struct i386_saved_state *) tstate;
-                       uesp = state->uesp;
-               }
-
-                /* If a valid user stack is specified, use it. */
-               if (uesp)
-                       *user_stack = uesp;
-               if (customstack && uesp)
+           case x86_THREAD_STATE64:
+           {
+               x86_thread_state64_t *state25;
+
+                state25 = (x86_thread_state64_t *) tstate;
+
+               if (state25->rsp)
+                       *user_stack = state25->rsp;
+               else 
+                       *user_stack = VM_USRSTACK64;
+               if (customstack && state25->rsp)
                        *customstack = 1;
                else
                        *customstack = 0;
-                break;
-        default :
+               break;
+           }
+
+           default :
                 return (KERN_INVALID_ARGUMENT);
         }
                 
         return (KERN_SUCCESS);
 }    
 
+
 kern_return_t
 thread_entrypoint(
-    thread_t            thread,
-    int                 flavor,
-    thread_state_t      tstate,
-    unsigned int        count,
-    vm_offset_t         *entry_point
-)
+                 __unused thread_t   thread,
+                 int                 flavor,
+                 thread_state_t      tstate,
+                 __unused unsigned int        count,
+                 mach_vm_offset_t    *entry_point
+                 )
 { 
-    struct i386_saved_state    *state;
-    i386_thread_state_t *state25;
-
-    /*
-     * Set a default.
-     */
-    if (*entry_point == 0)
-       *entry_point = VM_MIN_ADDRESS;
+        /*
+        * Set a default.
+        */
+        if (*entry_point == 0)
+               *entry_point = VM_MIN_ADDRESS;
                
-    switch (flavor) {
-    case i386_THREAD_STATE:
-       state25 = (i386_thread_state_t *) tstate;
-       *entry_point = state25->eip ? state25->eip: VM_MIN_ADDRESS;
-       break;
-
-    case i386_NEW_THREAD_STATE:
-       if (count < i386_THREAD_STATE_COUNT)
-           return (KERN_INVALID_ARGUMENT);
-       else {
-               state = (struct i386_saved_state *) tstate;
-
-               /*
-               * If a valid entry point is specified, use it.
-               */
-               *entry_point = state->eip ? state->eip: VM_MIN_ADDRESS;
-       }
-       break;
-    }
+       switch (flavor) {
+           case OLD_i386_THREAD_STATE:
+           case x86_THREAD_STATE32:
+           {
+               x86_thread_state32_t *state25;
+
+               state25 = (x86_thread_state32_t *) tstate;
+               *entry_point = state25->eip ? state25->eip: VM_MIN_ADDRESS;
+               break;
+           }
 
+           case x86_THREAD_STATE64:
+           {
+               x86_thread_state64_t *state25;
+
+               state25 = (x86_thread_state64_t *) tstate;
+               *entry_point = state25->rip ? state25->rip: VM_MIN_ADDRESS64;
+               break;
+           }
+    }
     return (KERN_SUCCESS);
 }   
 
-struct i386_saved_state *
-get_user_regs(thread_act_t th)
-{
-       if (th->mact.pcb)
-               return(USER_REGS(th));
-       else {
-               printf("[get_user_regs: thread does not have pcb]");
-               return NULL;
-       }
-}
 
 /*
  * Duplicate parent state in child
@@ -205,41 +216,53 @@ get_user_regs(thread_act_t th)
  */
 kern_return_t
 machine_thread_dup(
-    thread_act_t               parent,
-    thread_act_t               child
+    thread_t           parent,
+    thread_t           child
 )
 {
-       struct i386_saved_state *parent_state, *child_state;
-       struct i386_machine_state       *ims;
-       struct i386_float_state         floatregs;
-
-#ifdef XXX
-       /* Save the FPU state */
-       if ((pcb_t)(per_proc_info[cpu_number()].fpu_pcb) == parent->mact.pcb) {
-               fp_state_save(parent);
-       }
-#endif
+       
+       pcb_t           parent_pcb;
+       pcb_t           child_pcb;
 
-       if (child->mact.pcb == NULL || parent->mact.pcb == NULL)
+       if ((child_pcb = child->machine.pcb) == NULL ||
+           (parent_pcb = parent->machine.pcb) == NULL)
                return (KERN_FAILURE);
+       /*
+        * Copy over the i386_saved_state registers
+        */
+       if (cpu_mode_is64bit()) {
+               if (thread_is_64bit(parent))
+                       bcopy(USER_REGS64(parent), USER_REGS64(child), sizeof(x86_saved_state64_t));
+               else
+                       bcopy(USER_REGS32(parent), USER_REGS32(child), sizeof(x86_saved_state_compat32_t));
+       } else
+               bcopy(USER_REGS32(parent), USER_REGS32(child), sizeof(x86_saved_state32_t));
 
-       /* Copy over the i386_saved_state registers */
-       child->mact.pcb->iss = parent->mact.pcb->iss;
-
-       /* Check to see if parent is using floating point
+       /*
+        * Check to see if parent is using floating point
         * and if so, copy the registers to the child
-        * FIXME - make sure this works.
         */
+        fpu_dup_fxstate(parent, child);
 
-       if (parent->mact.pcb->ims.ifps)  {
-               if (fpu_get_state(parent, &floatregs) == KERN_SUCCESS)
-                       fpu_set_state(child, &floatregs);
-       }
-       
-       /* FIXME - should a user specified LDT, TSS and V86 info
-        * be duplicated as well?? - probably not.
+#ifdef MACH_BSD
+       /*
+        * Copy the parent's cthread id and USER_CTHREAD descriptor, if 32-bit.
         */
+       child_pcb->cthread_self = parent_pcb->cthread_self;
+       if (!thread_is_64bit(parent))
+               child_pcb->cthread_desc = parent_pcb->cthread_desc;
 
+       /*
+        * FIXME - should a user specified LDT, TSS and V86 info
+        * be duplicated as well?? - probably not.
+        */
+        // duplicate any use LDT entry that was set I think this is appropriate.
+        if (parent_pcb->uldt_selector!= 0) {
+               child_pcb->uldt_selector = parent_pcb->uldt_selector;
+               child_pcb->uldt_desc = parent_pcb->uldt_desc;
+        }
+#endif
+            
        return (KERN_SUCCESS);
 }
 
@@ -247,274 +270,184 @@ machine_thread_dup(
  * FIXME - thread_set_child
  */
 
-void thread_set_child(thread_act_t child, int pid);
+void thread_set_child(thread_t child, int pid);
 void
-thread_set_child(thread_act_t child, int pid)
+thread_set_child(thread_t child, int pid)
 {
-       child->mact.pcb->iss.eax = pid;
-       child->mact.pcb->iss.edx = 1;
-       child->mact.pcb->iss.efl &= ~EFL_CF;
-}
-void thread_set_parent(thread_act_t parent, int pid);
-void
-thread_set_parent(thread_act_t parent, int pid)
-{
-       parent->mact.pcb->iss.eax = pid;
-       parent->mact.pcb->iss.edx = 0;
-       parent->mact.pcb->iss.efl &= ~EFL_CF;
-}
 
+        if (thread_is_64bit(child)) {
+               x86_saved_state64_t     *iss64;
+               
+               iss64 = USER_REGS64(child);
 
+               iss64->rax = pid;
+               iss64->rdx = 1;
+               iss64->isf.rflags &= ~EFL_CF;
+       } else {
+               x86_saved_state32_t     *iss32;
+               
+               iss32 = USER_REGS32(child);
 
-/*
- * Move pages from one kernel virtual address to another.
- * Both addresses are assumed to reside in the Sysmap,
- * and size must be a multiple of the page size.
- */
-void
-pagemove(
-       register caddr_t from,
-       register caddr_t to,
-       int size)
-{
-       pmap_movepage((unsigned long)from, (unsigned long)to, (vm_size_t)size);
+               iss32->eax = pid;
+               iss32->edx = 1;
+               iss32->efl &= ~EFL_CF;
+       }
 }
 
-/*
- * System Call handling code
- */
-
-#define        ERESTART        -1              /* restart syscall */
-#define        EJUSTRETURN     -2              /* don't modify regs, just return */
 
-struct sysent {                /* system call table */
-       unsigned short          sy_narg;                /* number of args */
-       char                    sy_parallel;    /* can execute in parallel */
-        char                   sy_funnel;      /* funnel type */
-       unsigned long           (*sy_call)(void *, void *, int *);      /* implementing function */
-};
-
-#define NO_FUNNEL 0
-#define KERNEL_FUNNEL 1
-#define NETWORK_FUNNEL 2
+void thread_set_parent(thread_t parent, int pid);
+void
+thread_set_parent(thread_t parent, int pid)
+{
 
-extern funnel_t * kernel_flock;
-extern funnel_t * network_flock;
+        if (thread_is_64bit(parent)) {
+               x86_saved_state64_t     *iss64;
+               
+               iss64 = USER_REGS64(parent);
 
-extern struct sysent sysent[];
+               iss64->rax = pid;
+               iss64->rdx = 0;
+               iss64->isf.rflags &= ~EFL_CF;
+       } else {
+               x86_saved_state32_t     *iss32;
+               
+               iss32 = USER_REGS32(parent);
 
-int set_bsduthreadargs (thread_act_t, struct i386_saved_state *, void *);
+               iss32->eax = pid;
+               iss32->edx = 0;
+               iss32->efl &= ~EFL_CF;
+       }
+}
 
-void * get_bsduthreadarg(thread_act_t);
 
-void unix_syscall(struct i386_saved_state *);
 
-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);
-       p = current_proc();
-
-       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);
-       }
+/*
+ * System Call handling code
+ */
 
-       if (error == ERESTART) {
-               regs->eip -= 7;
-       }
-       else if (error != EJUSTRETURN) {
-               if (error) {
-                   regs->eax = error;
-                   regs->efl |= EFL_CF;        /* carry bit */
-               } else { /* (not error) */
-                   regs->eax = rval[0];
-                   regs->edx = rval[1];
-                   regs->efl &= ~EFL_CF;
-               } 
-       }
+extern struct proc * i386_current_proc(void);
 
-       ktrsysret(p, code, error, rval[0], callp->sy_funnel);
+extern long fuword(vm_offset_t);
 
-       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)
-               (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
+/* following implemented in bsd/dev/i386/unix_signal.c */
+int __pthread_cset(struct sysent *);
 
-    thread_exception_return();
-    /* NOTREACHED */
-}
+void __pthread_creset(struct sysent *);
 
 
 void
-unix_syscall(struct i386_saved_state *regs)
+machdep_syscall(x86_saved_state_t *state)
 {
-    thread_act_t               thread;
-    void       *vt; 
-    unsigned short     code;
-    struct sysent              *callp;
-       int     nargs, error;
-       volatile int *rval;
-       int funnel_type;
-    vm_offset_t                params;
-    extern int nsysent;
-       struct proc *p;
-       struct proc *current_proc();
-
-    thread = current_act();
-    p = current_proc();
-    rval = (int *)get_bsduthreadrval(thread);
-
-    //printf("[scall : eax %x]",  regs->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);
-       params += sizeof (int);
-       callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
-    }
+       int                     args[machdep_call_count];
+        int                    trapno;
+       int                     nargs;
+       machdep_call_t          *entry;
+       x86_saved_state32_t     *regs;
+
+       assert(is_saved_state32(state));
+       regs = saved_state32(state);
     
-    vt = get_bsduthreadarg(thread);
+       trapno = regs->eax;
+#if DEBUG_TRACE
+       kprintf("machdep_syscall(0x%08x) code=%d\n", regs, trapno);
+#endif
 
-    if ((nargs = (callp->sy_narg * sizeof (int))) &&
-           (error = copyin((char *) params, (char *)vt , nargs)) != 0) {
-       regs->eax = error;
-       regs->efl |= EFL_CF;
-       thread_exception_return();
-       /* NOTREACHED */
-    }
-    
-    rval[0] = 0;
-    rval[1] = regs->edx;
-
-       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 (trapno < 0 || trapno >= machdep_call_count) {
+               regs->eax = (unsigned int)kern_invalid(NULL);
 
-    if (callp->sy_narg > 8)
-       panic("unix_syscall max arg count exceeded (%d)", callp->sy_narg);
+               thread_exception_return();
+               /* NOTREACHED */
+       }
+       entry = &machdep_call_table[trapno];
+       nargs = entry->nargs;
 
-       ktrsyscall(p, code, callp->sy_narg, vt, funnel_type);
+       if (nargs != 0) {
+               if (copyin((user_addr_t) regs->uesp + sizeof (int),
+                          (char *) args, (nargs * sizeof (int)))) {
+                       regs->eax = KERN_INVALID_ADDRESS;
 
-       { 
-         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);
+                       thread_exception_return();
+                       /* NOTREACHED */
+               }
        }
+       switch (nargs) {
+           case 0:
+               regs->eax = (*entry->routine.args_0)();
+               break;
+           case 1:
+               regs->eax = (*entry->routine.args_1)(args[0]);
+               break;
+           case 2:
+               regs->eax = (*entry->routine.args_2)(args[0], args[1]);
+               break;
+           case 3:
+               if (!entry->bsd_style)
+                       regs->eax = (*entry->routine.args_3)(args[0], args[1], args[2]);
+               else {
+                       int     error;
+                       int     rval;
+
+                       error = (*entry->routine.args_bsd_3)(&rval, args[0], args[1], args[2]);
+                       if (error) {
+                               regs->eax = error;
+                               regs->efl |= EFL_CF;    /* carry bit */
+                       } else {
+                               regs->eax = rval;
+                               regs->efl &= ~EFL_CF;
+                       }
+               }
+               break;
+           case 4:
+               regs->eax = (*entry->routine.args_4)(args[0], args[1], args[2], args[3]);
+               break;
 
-    error = (*(callp->sy_call))(p, (void *) vt, (int *) &rval[0]);
-       
-#if 0
-       /* May be needed with vfork changes */
-       regs = USER_REGS(thread);
-#endif
-       if (error == ERESTART) {
-               regs->eip -= 7;
-       }
-       else if (error != EJUSTRETURN) {
-               if (error) {
-                   regs->eax = error;
-                   regs->efl |= EFL_CF;        /* carry bit */
-               } else { /* (not error) */
-                   regs->eax = rval[0];
-                   regs->edx = rval[1];
-                   regs->efl &= ~EFL_CF;
-               } 
+           default:
+               panic("machdep_syscall: too many args");
        }
+       if (current_thread()->funnel_lock)
+               (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 */
 }
 
 
 void
-machdep_syscall( struct i386_saved_state *regs)
+machdep_syscall64(x86_saved_state_t *state)
 {
-    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) {
-       regs->eax = (unsigned int)kern_invalid();
+        int                    trapno;
+       machdep_call_t          *entry;
+       x86_saved_state64_t     *regs;
 
-       thread_exception_return();
-       /* NOTREACHED */
-    }
+       assert(is_saved_state64(state));
+       regs = saved_state64(state);
     
-    entry = &machdep_call_table[trapno];
-    nargs = entry->nargs;
-
-    if (nargs > 0) {
-       int                     args[nargs];
+       trapno = regs->rax & SYSCALL_NUMBER_MASK;
 
-       if (copyin((char *) regs->uesp + sizeof (int),
-                   (char *) args,
-                   nargs * sizeof (int))) {
+       if (trapno < 0 || trapno >= machdep_call_count) {
+               regs->rax = (unsigned int)kern_invalid(NULL);
 
-           regs->eax = KERN_INVALID_ADDRESS;
-
-           thread_exception_return();
-           /* NOTREACHED */
+               thread_exception_return();
+               /* NOTREACHED */
        }
+       entry = &machdep_call_table64[trapno];
 
-       switch (nargs) {
-           case 1:
-               regs->eax = (*entry->routine)(args[0]);
-               break;
-           case 2:
-               regs->eax = (*entry->routine)(args[0],args[1]);
+       switch (entry->nargs) {
+           case 0:
+               regs->rax = (*entry->routine.args_0)();
                break;
-           case 3:
-               regs->eax = (*entry->routine)(args[0],args[1],args[2]);
-               break;
-           case 4:
-               regs->eax = (*entry->routine)(args[0],args[1],args[2],args[3]);
+           case 1:
+               regs->rax = (*entry->routine.args64_1)(regs->rdi);
                break;
            default:
-               panic("machdep_syscall(): too many args");
+               panic("machdep_syscall64: too many args");
        }
-    }
-    else
-       regs->eax = (*entry->routine)();
+       if (current_thread()->funnel_lock)
+               (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 */
 }
 
 
@@ -522,11 +455,9 @@ kern_return_t
 thread_compose_cthread_desc(unsigned int addr, pcb_t pcb)
 {
   struct real_descriptor desc;
-  extern struct fake_descriptor *mp_ldt[];
-  struct real_descriptor  *ldtp;
-  int mycpu = cpu_number();
 
-  ldtp = (struct real_descriptor *)mp_ldt[mycpu];
+  mp_disable_preemption();
+
   desc.limit_low = 1;
   desc.limit_high = 0;
   desc.base_low = addr & 0xffff;
@@ -535,14 +466,17 @@ thread_compose_cthread_desc(unsigned int addr, pcb_t pcb)
   desc.access = ACC_P|ACC_PL_U|ACC_DATA_W;
   desc.granularity = SZ_32|SZ_G;
   pcb->cthread_desc = desc;
-  ldtp[sel_idx(USER_CTHREAD)] = desc;
+  *ldt_desc_p(USER_CTHREAD) = desc;
+
+  mp_enable_preemption();
+
   return(KERN_SUCCESS);
 }
 
 kern_return_t
-thread_set_cthread_self(int self)
+thread_set_cthread_self(uint32_t self)
 {
-   current_act()->mact.pcb->cthread_self = (unsigned int)self;
+   current_thread()->machine.pcb->cthread_self = (uint64_t) self;
    
    return (KERN_SUCCESS);
 }
@@ -550,72 +484,396 @@ thread_set_cthread_self(int self)
 kern_return_t
 thread_get_cthread_self(void)
 {
-    return ((kern_return_t)current_act()->mact.pcb->cthread_self);
+    return ((kern_return_t)current_thread()->machine.pcb->cthread_self);
 }
 
 kern_return_t
-thread_fast_set_cthread_self(int self)
+thread_fast_set_cthread_self(uint32_t self)
 {
-  pcb_t pcb;
-  pcb = (pcb_t)current_act()->mact.pcb;
-  thread_compose_cthread_desc((unsigned int)self, pcb);
-  pcb->cthread_self = (unsigned int)self; /* preserve old func too */
-  return (USER_CTHREAD);
+       pcb_t                   pcb;
+       x86_saved_state32_t     *iss;
+
+       pcb = (pcb_t)current_thread()->machine.pcb;
+       thread_compose_cthread_desc(self, pcb);
+       pcb->cthread_self = (uint64_t) self; /* preserve old func too */
+       iss = saved_state32(pcb->iss);
+       iss->gs = USER_CTHREAD;
+
+       return (USER_CTHREAD);
 }
 
-void
-mach25_syscall(struct i386_saved_state *regs)
+kern_return_t
+thread_fast_set_cthread_self64(uint64_t self)
 {
-       printf("*** Atttempt to execute a Mach 2.5 system call at EIP=%x EAX=%x(%d)\n",
-                       regs->eip, regs->eax, -regs->eax);
-       panic("FIXME!");
+       pcb_t                   pcb;
+       x86_saved_state64_t     *iss;
+
+       pcb = current_thread()->machine.pcb;
+
+       /* check for canonical address, set 0 otherwise  */
+       if (!IS_USERADDR64_CANONICAL(self))
+               self = 0ULL;
+       pcb->cthread_self = self;
+       current_cpu_datap()->cpu_uber.cu_user_gs_base = self;
+
+       /* XXX for 64-in-32 */
+       iss = saved_state64(pcb->iss);
+       iss->gs = USER_CTHREAD;
+       thread_compose_cthread_desc((uint32_t) self, pcb);
+
+       return (USER_CTHREAD);
+}
+
+/*
+ * thread_set_user_ldt routine is the interface for the user level
+ * settable ldt entry feature.  allowing a user to create arbitrary
+ * ldt entries seems to be too large of a security hole, so instead
+ * this mechanism is in place to allow user level processes to have
+ * an ldt entry that can be used in conjunction with the FS register.
+ *
+ * Swapping occurs inside the pcb.c file along with initialization
+ * when a thread is created. The basic functioning theory is that the
+ * pcb->uldt_selector variable will contain either 0 meaning the
+ * process has not set up any entry, or the selector to be used in
+ * the FS register. pcb->uldt_desc contains the actual descriptor the
+ * user has set up stored in machine usable ldt format.
+ *
+ * Currently one entry is shared by all threads (USER_SETTABLE), but
+ * this could be changed in the future by changing how this routine
+ * allocates the selector. There seems to be no real reason at this
+ * time to have this added feature, but in the future it might be
+ * needed.
+ *
+ * address is the linear address of the start of the data area size
+ * is the size in bytes of the area flags should always be set to 0
+ * for now. in the future it could be used to set R/W permisions or
+ * other functions. Currently the segment is created as a data segment
+ * up to 1 megabyte in size with full read/write permisions only.
+ *
+ * this call returns the segment selector or -1 if any error occurs
+ */
+kern_return_t
+thread_set_user_ldt(uint32_t address, uint32_t size, uint32_t flags)
+{
+    pcb_t pcb;
+    struct fake_descriptor temp;
+    int mycpu;
+
+    if (flags != 0)
+       return -1;              // flags not supported
+    if (size > 0xFFFFF)
+       return -1;              // size too big, 1 meg is the limit
+
+    mp_disable_preemption();
+    mycpu = cpu_number();
+    
+    // create a "fake" descriptor so we can use fix_desc()
+    // to build a real one...
+    //   32 bit default operation size
+    //   standard read/write perms for a data segment
+    pcb = (pcb_t)current_thread()->machine.pcb;
+    temp.offset = address;
+    temp.lim_or_seg = size;
+    temp.size_or_wdct = SZ_32;
+    temp.access = ACC_P|ACC_PL_U|ACC_DATA_W;
+
+    // turn this into a real descriptor
+    fix_desc(&temp,1);
+
+    // set up our data in the pcb
+    pcb->uldt_desc = *(struct real_descriptor*)&temp;
+    pcb->uldt_selector = USER_SETTABLE;                // set the selector value
+
+    // now set it up in the current table...
+    *ldt_desc_p(USER_SETTABLE) = *(struct real_descriptor*)&temp;
+
+    mp_enable_preemption();
+
+    return USER_SETTABLE;
 }
+
 #endif /* MACH_BSD */
 
 
-/* This routine is called from assembly before each and every mach trap.
- */
+typedef kern_return_t (*mach_call_t)(void *);
+
+struct mach_call_args {
+       syscall_arg_t arg1;
+       syscall_arg_t arg2;
+       syscall_arg_t arg3;
+       syscall_arg_t arg4;
+       syscall_arg_t arg5;
+       syscall_arg_t arg6;
+       syscall_arg_t arg7;
+       syscall_arg_t arg8;
+       syscall_arg_t arg9;
+};
+
+
+static kern_return_t
+mach_call_arg_munger32(uint32_t sp, int nargs, int call_number, struct mach_call_args *args);
+
+
+static kern_return_t
+mach_call_arg_munger32(uint32_t sp, int nargs, int call_number, struct mach_call_args *args)
+{
+       unsigned int args32[9];
+
+       if (copyin((user_addr_t)(sp + sizeof(int)), (char *)args32, nargs * sizeof (int)))
+               return KERN_INVALID_ARGUMENT;
+
+       switch (nargs) {
+           case 9: args->arg9 = args32[8];
+           case 8: args->arg8 = args32[7];
+           case 7: args->arg7 = args32[6];
+           case 6: args->arg6 = args32[5];
+           case 5: args->arg5 = args32[4];
+           case 4: args->arg4 = args32[3];
+           case 3: args->arg3 = args32[2];
+           case 2: args->arg2 = args32[1];
+           case 1: args->arg1 = args32[0];
+       }
+       if (call_number == 90) {
+               /* munge_l for mach_wait_until_trap() */
+               args->arg1 = (((uint64_t)(args32[0])) | ((((uint64_t)(args32[1]))<<32)));
+       }
+       if (call_number == 93) {
+               /* munge_wl for mk_timer_arm_trap() */
+               args->arg2 = (((uint64_t)(args32[1])) | ((((uint64_t)(args32[2]))<<32)));
+       }
+
+       return KERN_SUCCESS;
+}
+
+
+__private_extern__ void
+mach_call_munger(x86_saved_state_t *state);
 
-extern unsigned int mach_call_start(unsigned int, unsigned int *);
 
 __private_extern__
-unsigned int
-mach_call_start(unsigned int call_number, unsigned int *args)
+void
+mach_call_munger(x86_saved_state_t *state)
 {
-       int i, argc;
-       unsigned int kdarg[3];
+       int argc;
+       int call_number;
+       mach_call_t mach_call;
+       kern_return_t retval;
+       struct mach_call_args args = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+       x86_saved_state32_t     *regs;
+
+       assert(is_saved_state32(state));
+       regs = saved_state32(state);
+
+       call_number = -(regs->eax);
+#if DEBUG_TRACE
+       kprintf("mach_call_munger(0x%08x) code=%d\n", regs, call_number);
+#endif
 
-/* Always prepare to trace mach system calls */
+       if (call_number < 0 || call_number >= mach_trap_count) {
+               i386_exception(EXC_SYSCALL, call_number, 1);
+               /* NOTREACHED */
+       }
+       mach_call = (mach_call_t)mach_trap_table[call_number].mach_trap_function;
+       
+       if (mach_call == (mach_call_t)kern_invalid) {
+               i386_exception(EXC_SYSCALL, call_number, 1);
+               /* NOTREACHED */
+       }
+       argc = mach_trap_table[call_number].mach_trap_arg_count;
 
-       kdarg[0]=0;
-       kdarg[1]=0;
-       kdarg[2]=0;
+       if (argc) {
+               retval = mach_call_arg_munger32(regs->uesp, argc, call_number, &args);
+               
+               if (retval != KERN_SUCCESS) {
+                       regs->eax = retval;
+       
+                       thread_exception_return();
+                       /* NOTREACHED */
+               }
+       }
+       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) | DBG_FUNC_START,
+                             (int) args.arg1, (int) args.arg2, (int) args.arg3, (int) args.arg4, 0);
+       
+       retval = mach_call(&args);
 
-       argc = mach_trap_table[call_number>>4].mach_trap_arg_count;
+       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number)) | DBG_FUNC_END,
+                             retval, 0, 0, 0, 0);
+       regs->eax = retval;
        
-       if (argc > 3)
-               argc = 3;
+       thread_exception_return();
+       /* NOTREACHED */
+}
+
+
+
+__private_extern__ void
+mach_call_munger64(x86_saved_state_t *state);
+
+
+__private_extern__
+void
+mach_call_munger64(x86_saved_state_t *state)
+{
+       int call_number;
+       int argc;
+       mach_call_t mach_call;
+       x86_saved_state64_t     *regs;
+
+       assert(is_saved_state64(state));
+       regs = saved_state64(state);
+
+       call_number = regs->rax & SYSCALL_NUMBER_MASK;
+
+       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) | DBG_FUNC_START,
+                             (int) regs->rdi, (int) regs->rsi, (int) regs->rdx, (int) regs->r10, 0);
        
-       for (i=0; i < argc; i++)
-         kdarg[i] = (int)*(args + i);
+       if (call_number < 0 || call_number >= mach_trap_count) {
+               i386_exception(EXC_SYSCALL, regs->rax, 1);
+               /* NOTREACHED */
+       }
+       mach_call = (mach_call_t)mach_trap_table[call_number].mach_trap_function;
+
+       if (mach_call == (mach_call_t)kern_invalid) {
+               i386_exception(EXC_SYSCALL, regs->rax, 1);
+               /* NOTREACHED */
+       }
+       argc = mach_trap_table[call_number].mach_trap_arg_count;
+
+       if (argc > 6) {
+               int copyin_count;
+
+               copyin_count = (argc - 6) * sizeof(uint64_t);
+
+               if (copyin((user_addr_t)(regs->isf.rsp + sizeof(user_addr_t)), (char *)&regs->v_arg6, copyin_count)) {
+                       regs->rax = KERN_INVALID_ARGUMENT;
+                       
+                       thread_exception_return();
+                       /* NOTREACHED */
+               }
+       }
+       regs->rax = (uint64_t)mach_call((void *)(&regs->rdi));
        
-       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number>>4)) | DBG_FUNC_START,
-                             kdarg[0], kdarg[1], kdarg[2], 0, 0);
+       KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number)) | DBG_FUNC_END,
+                             (int)regs->rax, 0, 0, 0, 0);
 
-       return call_number; /* pass this back thru */
+       thread_exception_return();
+       /* NOTREACHED */
 }
 
-/* This routine is called from assembly after each mach system call
+
+
+/*
+ * thread_setuserstack:
+ *
+ * Sets the user stack pointer into the machine
+ * dependent thread state info.
  */
+void
+thread_setuserstack(
+       thread_t        thread,
+       mach_vm_address_t       user_stack)
+{
+        if (thread_is_64bit(thread)) {
+               x86_saved_state64_t     *iss64;
 
-extern unsigned int mach_call_end(unsigned int, unsigned int);
+               iss64 = USER_REGS64(thread);
 
-__private_extern__
-unsigned int
-mach_call_end(unsigned int call_number, unsigned int retval)
+               iss64->isf.rsp = (uint64_t)user_stack;
+       } else {
+               x86_saved_state32_t     *iss32;
+               
+               iss32 = USER_REGS32(thread);
+
+               iss32->uesp = CAST_DOWN(unsigned int, user_stack);
+       }
+}
+
+/*
+ * thread_adjuserstack:
+ *
+ * Returns the adjusted user stack pointer from the machine
+ * dependent thread state info.  Used for small (<2G) deltas.
+ */
+uint64_t
+thread_adjuserstack(
+       thread_t        thread,
+       int             adjust)
+{
+        if (thread_is_64bit(thread)) {
+               x86_saved_state64_t     *iss64;
+
+               iss64 = USER_REGS64(thread);
+
+               iss64->isf.rsp += adjust;
+
+               return iss64->isf.rsp;
+       } else {
+               x86_saved_state32_t     *iss32;
+               
+               iss32 = USER_REGS32(thread);
+
+               iss32->uesp += adjust;
+
+               return CAST_USER_ADDR_T(iss32->uesp);
+       }
+}
+
+/*
+ * thread_setentrypoint:
+ *
+ * Sets the user PC into the machine
+ * dependent thread state info.
+ */
+void
+thread_setentrypoint(thread_t thread, mach_vm_address_t entry)
+{
+        if (thread_is_64bit(thread)) {
+               x86_saved_state64_t     *iss64;
+
+               iss64 = USER_REGS64(thread);
+
+               iss64->isf.rip = (uint64_t)entry;
+       } else {
+               x86_saved_state32_t     *iss32;
+               
+               iss32 = USER_REGS32(thread);
+
+               iss32->eip = CAST_DOWN(unsigned int, entry);
+       }
+}
+
+
+void
+thread_setsinglestep(thread_t thread, int on)
+{
+        if (thread_is_64bit(thread)) {
+               x86_saved_state64_t     *iss64;
+
+               iss64 = USER_REGS64(thread);
+
+               if (on)
+                       iss64->isf.rflags |= EFL_TF;
+               else
+                       iss64->isf.rflags &= ~EFL_TF;
+       } else {
+               x86_saved_state32_t     *iss32;
+               
+               iss32 = USER_REGS32(thread);
+
+               if (on)
+                       iss32->efl |= EFL_TF;
+               else
+                       iss32->efl &= ~EFL_TF;
+       }
+}
+
+
+
+/* XXX this should be a struct savearea so that CHUD will work better on x86 */
+void *
+find_user_regs(
+       thread_t        thread)
 {
-  KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number>>4)) | DBG_FUNC_END,
-               retval, 0, 0, 0, 0);
-       return retval;  /* pass this back thru */
+       return USER_STATE(thread);
 }