]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_exit.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / bsd / kern / kern_exit.c
index 157ec5f05ce9a51c2f90ce9b69010607fc4184dc..b8f5def79fd308f346eed191d51d1dbae0a4759e 100644 (file)
@@ -1,23 +1,29 @@
 /*
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2016 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,
  * 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@
  */
 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
 /*
  */
 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
 /*
  *
  *     @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
  */
  *
  *     @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
  */
+/*
+ * NOTICE: This file was modified by SPARTA, Inc. in 2005 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 <machine/reg.h>
 #include <machine/psl.h>
  
 #include <machine/reg.h>
 #include <machine/psl.h>
+#include <stdatomic.h>
 
 #include "compat_43.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/ioctl.h>
 
 #include "compat_43.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/ioctl.h>
+#include <sys/proc_internal.h>
 #include <sys/proc.h>
 #include <sys/proc.h>
+#include <sys/kauth.h>
 #include <sys/tty.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/kernel.h>
 #include <sys/tty.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/kernel.h>
-#include <sys/buf.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
-#include <sys/file.h>
-#include <sys/vnode.h>
+#include <sys/file_internal.h>
+#include <sys/vnode_internal.h>
 #include <sys/syslog.h>
 #include <sys/malloc.h>
 #include <sys/resourcevar.h>
 #include <sys/ptrace.h>
 #include <sys/syslog.h>
 #include <sys/malloc.h>
 #include <sys/resourcevar.h>
 #include <sys/ptrace.h>
+#include <sys/proc_info.h>
+#include <sys/reason.h>
+#include <sys/_types/_timeval64.h>
 #include <sys/user.h>
 #include <sys/aio_kern.h>
 #include <sys/user.h>
 #include <sys/aio_kern.h>
-
-#include <bsm/audit_kernel.h>
+#include <sys/sysproto.h>
+#include <sys/signalvar.h>
+#include <sys/kdebug.h>
+#include <sys/filedesc.h> /* fdfree */
+#include <sys/acct.h> /* acct_process */
+#include <sys/codesign.h>
+#include <sys/event.h> /* kevent_proc_copy_uptrs */
+#include <sys/sdt.h>
+
+#include <security/audit/audit.h>
 #include <bsm/audit_kevents.h>
 
 #include <mach/mach_types.h>
 #include <bsm/audit_kevents.h>
 
 #include <mach/mach_types.h>
+#include <mach/task.h>
+#include <mach/thread_act.h>
+
+#include <kern/exc_resource.h>
+#include <kern/kern_types.h>
+#include <kern/kalloc.h>
+#include <kern/task.h>
+#include <corpses/task_corpse.h>
 #include <kern/thread.h>
 #include <kern/thread.h>
-#include <kern/thread_act.h>
+#include <kern/thread_call.h>
 #include <kern/sched_prim.h>
 #include <kern/assert.h>
 #include <kern/sched_prim.h>
 #include <kern/assert.h>
-#if KTRACE   
-#include <sys/ktrace.h>
-#include <sys/ubc.h>
+#include <kern/policy_internal.h>
+#include <kern/exc_guard.h>
+
+#include <vm/vm_protos.h>
+
+#include <pexpert/pexpert.h>
+
+#if SYSV_SHM
+#include <sys/shm_internal.h>  /* shmexit */
+#endif /* SYSV_SHM */
+#if CONFIG_PERSONAS
+#include <sys/persona.h>
+#endif /* CONFIG_PERSONAS */
+#if CONFIG_MEMORYSTATUS
+#include <sys/kern_memorystatus.h>
+#endif /* CONFIG_MEMORYSTATUS */
+#if CONFIG_DTRACE
+/* Do not include dtrace.h, it redefines kmem_[alloc/free] */
+void dtrace_proc_exit(proc_t p);
+#include <sys/dtrace_ptss.h>
+#endif /* CONFIG_DTRACE */
+#if CONFIG_MACF
+#include <security/mac_framework.h>
+#include <security/mac_mach_internal.h>
+#include <sys/syscall.h>
+#endif /* CONFIG_MACF */
+
+void proc_prepareexit(proc_t p, int rv, boolean_t perf_notify);
+void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
+               mach_exception_data_type_t code, mach_exception_data_type_t subcode,
+               uint64_t *udata_buffer, int num_udata, void *reason);
+mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p);
+void vfork_exit(proc_t p, int rv);
+__private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
+__private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
+static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock);
+static void populate_corpse_crashinfo(proc_t p, task_t corpse_task,
+               struct rusage_superset *rup, mach_exception_data_type_t code,
+               mach_exception_data_type_t subcode, uint64_t *udata_buffer,
+               int num_udata, os_reason_t reason);
+static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode);
+extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg, char *buffer, uint32_t buffersize, int32_t *retval);
+static __attribute__((noinline)) void launchd_crashed_panic(proc_t p, int rv);
+extern void proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
+extern void task_coalition_ids(task_t task, uint64_t ids[COALITION_NUM_TYPES]);
+extern uint64_t        get_task_phys_footprint_limit(task_t);
+int proc_list_uptrs(void *p, uint64_t *udata_buffer, int size);
+extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task);
+
+
+/*
+ * Things which should have prototypes in headers, but don't
+ */
+void   proc_exit(proc_t p);
+int    wait1continue(int result);
+int    waitidcontinue(int result);
+kern_return_t sys_perf_notify(thread_t thread, int pid);
+kern_return_t task_exception_notify(exception_type_t exception,
+       mach_exception_data_type_t code, mach_exception_data_type_t subcode);
+kern_return_t task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
+void   delay(int);
+void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
+
+/*
+ * NOTE: Source and target may *NOT* overlap!
+ * XXX Should share code with bsd/dev/ppc/unix_signal.c
+ */
+void
+siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
+{
+       out->si_signo   = in->si_signo;
+       out->si_errno   = in->si_errno;
+       out->si_code    = in->si_code;
+       out->si_pid     = in->si_pid;
+       out->si_uid     = in->si_uid;
+       out->si_status  = in->si_status;
+       out->si_addr    = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_addr);
+       /* following cast works for sival_int because of padding */
+       out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_value.sival_ptr);
+       out->si_band    = in->si_band;                  /* range reduction */
+}
+
+void
+siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
+{
+       out->si_signo   = in->si_signo;
+       out->si_errno   = in->si_errno;
+       out->si_code    = in->si_code;
+       out->si_pid     = in->si_pid;
+       out->si_uid     = in->si_uid;
+       out->si_status  = in->si_status;
+       out->si_addr    = in->si_addr;
+       /* following cast works for sival_int because of padding */
+       out->si_value.sival_ptr = in->si_value.sival_ptr;
+       out->si_band    = in->si_band;                  /* range reduction */
+}
+
+static int
+copyoutsiginfo(user_siginfo_t *native, boolean_t is64, user_addr_t uaddr)
+{
+       if (is64) {
+               user64_siginfo_t sinfo64;
+
+               bzero(&sinfo64, sizeof (sinfo64));
+               siginfo_user_to_user64(native, &sinfo64);
+               return (copyout(&sinfo64, uaddr, sizeof (sinfo64)));
+       } else {
+               user32_siginfo_t sinfo32;
+
+               bzero(&sinfo32, sizeof (sinfo32));
+               siginfo_user_to_user32(native, &sinfo32);
+               return (copyout(&sinfo32, uaddr, sizeof (sinfo32)));
+       }
+}
+
+void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
+               mach_exception_data_type_t code, mach_exception_data_type_t subcode,
+               uint64_t *udata_buffer, int num_udata, void *reason)
+{
+       struct rusage_superset rup;
+
+       gather_rusage_info(p, &rup.ri, RUSAGE_INFO_CURRENT);
+       rup.ri.ri_phys_footprint = 0;
+       populate_corpse_crashinfo(p, corpse_task, &rup, code, subcode,
+                       udata_buffer, num_udata, reason);
+}
+
+static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode)
+{
+       mach_exception_data_type_t code_update = *code;
+       mach_exception_data_type_t subcode_update = *subcode;
+       if (p->p_exit_reason == OS_REASON_NULL) {
+               return;
+       }
+
+       switch (p->p_exit_reason->osr_namespace) {
+               case OS_REASON_JETSAM:
+                       if (p->p_exit_reason->osr_code == JETSAM_REASON_MEMORY_PERPROCESSLIMIT) {
+                               /* Update the code with EXC_RESOURCE code for high memory watermark */
+                               EXC_RESOURCE_ENCODE_TYPE(code_update, RESOURCE_TYPE_MEMORY);
+                               EXC_RESOURCE_ENCODE_FLAVOR(code_update, FLAVOR_HIGH_WATERMARK);
+                               EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update, ((get_task_phys_footprint_limit(p->task)) >> 20));
+                               subcode_update = 0;
+                               break;
+                       }
+
+                       break;
+               default:
+                       break;
+       }
+
+       *code = code_update;
+       *subcode = subcode_update;
+       return;
+}
+
+mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p)
+{
+       uint64_t subcode = 0;
+
+       if (p->p_exit_reason == OS_REASON_NULL) {
+               return 0;
+       }
+
+       /* Embed first 32 bits of osr_namespace and osr_code in exception code */
+       ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_namespace);
+       ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_code);
+       return (mach_exception_data_type_t)subcode;
+}
+
+static void
+populate_corpse_crashinfo(proc_t p, task_t corpse_task, struct rusage_superset *rup,
+               mach_exception_data_type_t code, mach_exception_data_type_t subcode,
+               uint64_t *udata_buffer, int num_udata, os_reason_t reason)
+{
+       mach_vm_address_t uaddr = 0;
+       mach_exception_data_type_t exc_codes[EXCEPTION_CODE_MAX];
+       exc_codes[0] = code;
+       exc_codes[1] = subcode;
+       cpu_type_t cputype;
+       struct proc_uniqidentifierinfo p_uniqidinfo;
+       struct proc_workqueueinfo pwqinfo;
+       int retval = 0;
+       uint64_t crashed_threadid = task_corpse_get_crashed_thread_id(corpse_task);
+       unsigned int pflags = 0;
+       uint64_t max_footprint_mb;
+       uint64_t max_footprint;
+       void *crash_info_ptr = task_get_corpseinfo(corpse_task);
+
+#if CONFIG_MEMORYSTATUS
+       int memstat_dirty_flags = 0;
+#endif
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_CODES, sizeof(exc_codes), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, exc_codes, sizeof(exc_codes));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PID, sizeof(p->p_pid), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_pid, sizeof(p->p_pid));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PPID, sizeof(p->p_ppid), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_ppid, sizeof(p->p_ppid));
+       }
+
+       /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
+       if ((p->p_exit_reason == OS_REASON_NULL) || !(p->p_exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASHED_TID)) {
+               if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CRASHED_THREADID, sizeof(uint64_t), &uaddr)) {
+                       kcdata_memcpy(crash_info_ptr, uaddr, &crashed_threadid, sizeof(uint64_t));
+               }
+       }
+
+       if (KERN_SUCCESS ==
+           kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_BSDINFOWITHUNIQID, sizeof(struct proc_uniqidentifierinfo), &uaddr)) {
+               proc_piduniqidentifierinfo(p, &p_uniqidinfo);
+               kcdata_memcpy(crash_info_ptr, uaddr, &p_uniqidinfo, sizeof(struct proc_uniqidentifierinfo));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RUSAGE_INFO, sizeof(rusage_info_current), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &rup->ri, sizeof(rusage_info_current));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_CSFLAGS, sizeof(p->p_csflags), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_csflags, sizeof(p->p_csflags));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_NAME, sizeof(p->p_comm), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_comm, sizeof(p->p_comm));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_STARTTIME, sizeof(p->p_start), &uaddr)) {
+               struct timeval64 t64;
+               t64.tv_sec = (int64_t)p->p_start.tv_sec;
+               t64.tv_usec = (int64_t)p->p_start.tv_usec;
+               kcdata_memcpy(crash_info_ptr, uaddr, &t64, sizeof(t64));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_USERSTACK, sizeof(p->user_stack), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->user_stack, sizeof(p->user_stack));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_ARGSLEN, sizeof(p->p_argslen), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argslen, sizeof(p->p_argslen));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_ARGC, sizeof(p->p_argc), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argc, sizeof(p->p_argc));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PATH, MAXPATHLEN, &uaddr)) {
+               char *buf = (char *) kalloc(MAXPATHLEN);
+               if (buf != NULL) {
+                       bzero(buf, MAXPATHLEN);
+                       proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, &retval);
+                       kcdata_memcpy(crash_info_ptr, uaddr, buf, MAXPATHLEN);
+                       kfree(buf, MAXPATHLEN);
+               }
+       }
+
+       pflags = p->p_flag & (P_LP64 | P_SUGID);
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_FLAGS, sizeof(pflags), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &pflags, sizeof(pflags));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_UID, sizeof(p->p_uid), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_uid, sizeof(p->p_uid));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_GID, sizeof(p->p_gid), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_gid, sizeof(p->p_gid));
+       }
+
+       cputype = cpu_type() & ~CPU_ARCH_MASK;
+       if (IS_64BIT_PROCESS(p))
+               cputype |= CPU_ARCH_ABI64;
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CPUTYPE, sizeof(cpu_type_t), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &cputype, sizeof(cpu_type_t));
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT, sizeof(max_footprint_mb), &uaddr)) {
+               max_footprint = get_task_phys_footprint_limit(p->task);
+               max_footprint_mb = max_footprint >> 20;
+               kcdata_memcpy(crash_info_ptr, uaddr, &max_footprint_mb, sizeof(max_footprint_mb));
+       }
+
+       bzero(&pwqinfo, sizeof(struct proc_workqueueinfo));
+       retval = fill_procworkqueue(p, &pwqinfo);
+       if (retval == 0) {
+               if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_WORKQUEUEINFO, sizeof(struct proc_workqueueinfo), &uaddr)) {
+                       kcdata_memcpy(crash_info_ptr, uaddr, &pwqinfo, sizeof(struct proc_workqueueinfo));
+               }
+       }
+
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RESPONSIBLE_PID, sizeof(p->p_responsible_pid), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &p->p_responsible_pid, sizeof(p->p_responsible_pid));
+       }
+
+#if CONFIG_COALITIONS
+       if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &uaddr)) {
+               uint64_t coalition_ids[COALITION_NUM_TYPES];
+               task_coalition_ids(p->task, coalition_ids);
+               kcdata_memcpy(crash_info_ptr, uaddr, coalition_ids, sizeof(coalition_ids));
+       }
+#endif /* CONFIG_COALITIONS */
+
+#if CONFIG_MEMORYSTATUS
+       memstat_dirty_flags = memorystatus_dirty_get(p);
+       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_DIRTY_FLAGS, sizeof(memstat_dirty_flags), &uaddr)) {
+               kcdata_memcpy(crash_info_ptr, uaddr, &memstat_dirty_flags, sizeof(memstat_dirty_flags));
+       }
 #endif
 
 #endif
 
-extern char init_task_failure_data[];
-int exit1 __P((struct proc *, int, int *));
-void proc_prepareexit(struct proc *p);
-int vfork_exit(struct proc *p, int rv);
-void vproc_exit(struct proc *p);
+       if (p->p_exit_reason != OS_REASON_NULL && reason == OS_REASON_NULL) {
+               reason = p->p_exit_reason;
+       }
+       if (reason != OS_REASON_NULL) {
+               if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &uaddr)) {
+                       struct exit_reason_snapshot ers = {
+                               .ers_namespace = reason->osr_namespace,
+                               .ers_code = reason->osr_code,
+                               .ers_flags = reason->osr_flags
+                       };
+
+                       kcdata_memcpy(crash_info_ptr, uaddr, &ers, sizeof(ers));
+               }
+
+               if (reason->osr_kcd_buf != 0) {
+                       uint32_t reason_buf_size = kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
+                       assert(reason_buf_size != 0);
+
+                       if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &uaddr)) {
+                               kcdata_memcpy(crash_info_ptr, uaddr, reason->osr_kcd_buf, reason_buf_size);
+                       }
+               }
+       }
+
+       if (num_udata > 0) {
+               if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_UDATA_PTRS,
+                                       sizeof(uint64_t), num_udata, &uaddr)) {
+                       kcdata_memcpy(crash_info_ptr, uaddr, udata_buffer, sizeof(uint64_t) * num_udata);
+               }
+       }
+}
+
+/*
+ * We only parse exit reason kcdata blobs for launchd when it dies
+ * and we're going to panic.
+ *
+ * Meant to be called immediately before panicking.
+ */
+char *
+launchd_exit_reason_get_string_desc(os_reason_t exit_reason)
+{
+       kcdata_iter_t iter;
+
+       if (exit_reason == OS_REASON_NULL || exit_reason->osr_kcd_buf == NULL ||
+                       exit_reason->osr_bufsize == 0) {
+               return NULL;
+       }
+
+       iter = kcdata_iter(exit_reason->osr_kcd_buf, exit_reason->osr_bufsize);
+       if (!kcdata_iter_valid(iter)) {
+#if DEBUG || DEVELOPMENT
+               printf("launchd exit reason has invalid exit reason buffer\n");
+#endif
+               return NULL;
+       }
+
+       if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON) {
+#if DEBUG || DEVELOPMENT
+               printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
+                       KCDATA_BUFFER_BEGIN_OS_REASON, kcdata_iter_type(iter));
+#endif
+               return NULL;
+       }
+
+       iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
+       if (!kcdata_iter_valid(iter)) {
+               return NULL;
+       }
+
+       return (char *)kcdata_iter_payload(iter);
+}
+
+static __attribute__((noinline)) void
+launchd_crashed_panic(proc_t p, int rv)
+{
+       char *launchd_exit_reason_desc = launchd_exit_reason_get_string_desc(p->p_exit_reason);
+
+       if (p->p_exit_reason == OS_REASON_NULL) {
+               printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
+                       WTERMSIG(rv), WEXITSTATUS(rv));
+       } else {
+               printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
+                       p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code, launchd_exit_reason_desc ?
+                       launchd_exit_reason_desc : "none");
+       }
+
+       const char *launchd_crashed_prefix_str;
+
+       if (strnstr(p->p_name, "preinit", sizeof(p->p_name))) {
+               launchd_crashed_prefix_str = "LTE preinit process exited";
+       } else {
+               launchd_crashed_prefix_str = "initproc exited";
+       }
+
+#if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
+       /*
+        * For debugging purposes, generate a core file of initproc before
+        * panicking. Leave at least 300 MB free on the root volume, and ignore
+        * the process's corefile ulimit. fsync() the file to ensure it lands on disk
+        * before the panic hits.
+        */
+
+       int             err;
+       uint64_t        coredump_start = mach_absolute_time();
+       uint64_t        coredump_end;
+       clock_sec_t     tv_sec;
+       clock_usec_t    tv_usec;
+       uint32_t        tv_msec;
+
+
+       err = coredump(p, 300, COREDUMP_IGNORE_ULIMIT | COREDUMP_FULLFSYNC);
+
+       coredump_end = mach_absolute_time();
+
+       absolutetime_to_microtime(coredump_end - coredump_start, &tv_sec, &tv_usec);
+
+       tv_msec = tv_usec / 1000;
+
+       if (err != 0) {
+               printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
+                      err, (uint32_t)tv_sec, tv_msec);
+       } else {
+               printf("Generated initproc core file in %d.%03d seconds\n",
+                      (uint32_t)tv_sec, tv_msec);
+       }
+#endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
+
+       sync(p, (void *)NULL, (int *)NULL);
+
+       if (p->p_exit_reason == OS_REASON_NULL) {
+               panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
+                               launchd_crashed_prefix_str, WTERMSIG(rv), WEXITSTATUS(rv), ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""));
+       } else {
+               panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s %s -- exit reason namespace %d subcode 0x%llx description: %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
+                               ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""),
+                               launchd_crashed_prefix_str, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
+                               launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
+       }
+}
+
+#define OS_REASON_IFLAG_USER_FAULT 0x1
+
+#define OS_REASON_TOTAL_USER_FAULTS_PER_PROC  5
+
+static int
+abort_with_payload_internal(proc_t p,
+               uint32_t reason_namespace, uint64_t reason_code,
+               user_addr_t payload, uint32_t payload_size,
+               user_addr_t reason_string, uint64_t reason_flags,
+               uint32_t internal_flags)
+{
+       os_reason_t exit_reason = OS_REASON_NULL;
+       kern_return_t kr = KERN_SUCCESS;
+
+       if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
+               uint32_t old_value = atomic_load_explicit(&p->p_user_faults,
+                               memory_order_relaxed);
+               for (;;) {
+                       if (old_value >= OS_REASON_TOTAL_USER_FAULTS_PER_PROC) {
+                               return EQFULL;
+                       }
+                       // this reloads the value in old_value
+                       if (atomic_compare_exchange_strong_explicit(&p->p_user_faults,
+                                       &old_value, old_value + 1, memory_order_relaxed,
+                                       memory_order_relaxed)) {
+                               break;
+                       }
+               }
+       }
+
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
+                                       p->p_pid, reason_namespace,
+                                       reason_code, 0, 0);
+
+       exit_reason = build_userspace_exit_reason(reason_namespace, reason_code,
+                       payload, payload_size, reason_string, reason_flags);
+
+       if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
+               mach_exception_code_t code = 0;
+
+               EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_USER); /* simulated EXC_GUARD */
+               EXC_GUARD_ENCODE_FLAVOR(code, 0);
+               EXC_GUARD_ENCODE_TARGET(code, reason_namespace);
+
+               if (exit_reason == OS_REASON_NULL) {
+                       kr = KERN_RESOURCE_SHORTAGE;
+               } else {
+                       kr = task_violated_guard(code, reason_code, exit_reason);
+               }
+               os_reason_free(exit_reason);
+       } else {
+               /*
+                * We use SIGABRT (rather than calling exit directly from here) so that
+                * the debugger can catch abort_with_{reason,payload} calls.
+                */
+               psignal_try_thread_with_reason(p, current_thread(), SIGABRT, exit_reason);
+       }
+
+       switch (kr) {
+       case KERN_SUCCESS:
+               return 0;
+       case KERN_NOT_SUPPORTED:
+               return ENOTSUP;
+       case KERN_INVALID_ARGUMENT:
+               return EINVAL;
+       case KERN_RESOURCE_SHORTAGE:
+       default:
+               return EBUSY;
+       }
+}
+
+int
+abort_with_payload(struct proc *cur_proc, struct abort_with_payload_args *args,
+                               __unused void *retval)
+{
+       abort_with_payload_internal(cur_proc, args->reason_namespace,
+                       args->reason_code, args->payload, args->payload_size,
+                       args->reason_string, args->reason_flags, 0);
+
+       return 0;
+}
+
+int
+os_fault_with_payload(struct proc *cur_proc,
+               struct os_fault_with_payload_args *args, __unused int *retval)
+{
+       return abort_with_payload_internal(cur_proc, args->reason_namespace,
+                       args->reason_code, args->payload, args->payload_size,
+                       args->reason_string, args->reason_flags, OS_REASON_IFLAG_USER_FAULT);
+}
+
 
 /*
  * exit --
  *     Death of process.
  */
 
 /*
  * exit --
  *     Death of process.
  */
-struct exit_args {
-       int     rval;
-};
+__attribute__((noreturn))
 void
 void
-exit(p, uap, retval)
-       struct proc *p;
-       struct exit_args *uap;
-       int *retval;
+exit(proc_t p, struct exit_args *uap, int *retval)
 {
 {
+       p->p_xhighbits = ((uint32_t)(uap->rval) & 0xFF000000) >> 24;
        exit1(p, W_EXITCODE(uap->rval, 0), retval);
 
        exit1(p, W_EXITCODE(uap->rval, 0), retval);
 
-       /* drop funnel before we return */
-       thread_funnel_set(kernel_flock, FALSE);
        thread_exception_return();
        /* NOTREACHED */
        while (TRUE)
        thread_exception_return();
        /* NOTREACHED */
        while (TRUE)
@@ -133,16 +694,29 @@ exit(p, uap, retval)
  * status and rusage for wait().  Check for child processes and orphan them.
  */
 int
  * status and rusage for wait().  Check for child processes and orphan them.
  */
 int
-exit1(p, rv, retval)
-       register struct proc *p;
-       int rv;
-       int * retval;
+exit1(proc_t p, int rv, int *retval)
 {
 {
-       register struct proc *q, *nq;
-       thread_act_t self = current_act();
+       return exit1_internal(p, rv, retval, TRUE, TRUE, 0);
+}
+
+int
+exit1_internal(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
+              int jetsam_flags)
+{
+       return exit_with_reason(p, rv, retval, thread_can_terminate, perf_notify, jetsam_flags, OS_REASON_NULL);
+}
+
+/*
+ * NOTE: exit_with_reason drops a reference on the passed exit_reason
+ */
+int
+exit_with_reason(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
+               int jetsam_flags, struct os_reason *exit_reason)
+{
+       thread_t self = current_thread();
        struct task *task = p->task;
        struct task *task = p->task;
-       register int i,s;
        struct uthread *ut;
        struct uthread *ut;
+       int error = 0;
 
        /*
         * If a thread in this task has already
 
        /*
         * If a thread in this task has already
@@ -151,114 +725,347 @@ exit1(p, rv, retval)
         */
 
         ut = get_bsdthread_info(self);
         */
 
         ut = get_bsdthread_info(self);
-        if (ut->uu_flag & P_VFORK) {
-               if (!vfork_exit(p, rv)) {
-                       vfork_return(self, p->p_pptr, p , retval);
-                       unix_syscall_return(0);
-                       /* NOT REACHED */
-               }  
-               return(EINVAL);
+        if ((p == current_proc()) &&
+            (ut->uu_flag & UT_VFORK)) {
+               os_reason_free(exit_reason);
+               if (!thread_can_terminate) {
+                       return EINVAL;
+               }
+
+               vfork_exit(p, rv);
+               vfork_return(p , retval, p->p_pid);
+               unix_syscall_return(0);
+               /* NOT REACHED */
         }
         }
-       AUDIT_SYSCALL_EXIT(0, p, ut); /* Exit is always successfull */
-        signal_lock(p);
+
+       /* 
+        * The parameter list of audit_syscall_exit() was augmented to
+        * take the Darwin syscall number as the first parameter,
+        * which is currently required by mac_audit_postselect().
+        */
+
+       /* 
+        * The BSM token contains two components: an exit status as passed
+        * to exit(), and a return value to indicate what sort of exit it 
+        * was.  The exit status is WEXITSTATUS(rv), but it's not clear
+        * what the return value is.
+        */
+       AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
+       /*
+        * TODO: what to audit here when jetsam calls exit and the uthread,
+        * 'ut' does not belong to the proc, 'p'.
+        */
+       AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
+
+       DTRACE_PROC1(exit, int, CLD_EXITED);
+
+       /* mark process is going to exit and pull out of DBG/disk throttle */
+       /* TODO: This should be done after becoming exit thread */
+       proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
+                            TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
+
+        proc_lock(p);
+       error = proc_transstart(p, 1, (jetsam_flags ? 1 : 0));
+       if (error == EDEADLK) {
+               /*
+                * If proc_transstart() returns EDEADLK, then another thread
+                * is either exec'ing or exiting. Return an error and allow
+                * the other thread to continue.
+                */
+               proc_unlock(p);
+               os_reason_free(exit_reason);
+               if (current_proc() == p){
+                       if (p->exit_thread == self) {
+                               printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
+                                      p->p_comm, p->p_pid);
+                       }
+
+                       if (thread_can_terminate) {
+                               thread_exception_return();
+                       }
+               }
+
+               return error;
+       }
+
        while (p->exit_thread != self) {
                if (sig_try_locked(p) <= 0) {
        while (p->exit_thread != self) {
                if (sig_try_locked(p) <= 0) {
+                       proc_transend(p, 1);
+                       os_reason_free(exit_reason);
+
                        if (get_threadtask(self) != task) {
                        if (get_threadtask(self) != task) {
-                                signal_unlock(p);
+                               proc_unlock(p);
                                return(0);
                         }
                                return(0);
                         }
-                       signal_unlock(p);
+                       proc_unlock(p);
+                       
                        thread_terminate(self);
                        thread_terminate(self);
-                       thread_funnel_set(kernel_flock, FALSE);
+                       if (!thread_can_terminate) {
+                               return 0;
+                       }
+
                        thread_exception_return();
                        /* NOTREACHED */
                }
                sig_lock_to_exit(p);
        }
                        thread_exception_return();
                        /* NOTREACHED */
                }
                sig_lock_to_exit(p);
        }
-        signal_unlock(p);
-       if (p->p_pid == 1) {
-               printf("pid 1 exited (signal %d, exit %d)",
-                   WTERMSIG(rv), WEXITSTATUS(rv));
-               panic("init died\nState at Last Exception:\n\n%s", 
-                                                       init_task_failure_data);
+
+       if (exit_reason != OS_REASON_NULL) {
+               KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_COMMIT) | DBG_FUNC_NONE,
+                                                       p->p_pid, exit_reason->osr_namespace,
+                                                       exit_reason->osr_code, 0, 0);
        }
 
        }
 
-       s = splsched();
-       p->p_flag |= P_WEXIT;
-       splx(s);
-       proc_prepareexit(p);
+       assert(p->p_exit_reason == OS_REASON_NULL);
+       p->p_exit_reason = exit_reason;
+
+       p->p_lflag |= P_LEXIT;
        p->p_xstat = rv;
        p->p_xstat = rv;
+       p->p_lflag |= jetsam_flags;
 
 
-       /* task terminate will call proc_terminate and that cleans it up */
+       proc_transend(p, 1);
+       proc_unlock(p);
+
+       proc_prepareexit(p, rv, perf_notify);
+
+       /* Last thread to terminate will call proc_exit() */
        task_terminate_internal(task);
 
        return(0);
 }
 
 void
        task_terminate_internal(task);
 
        return(0);
 }
 
 void
-proc_prepareexit(struct proc *p
+proc_prepareexit(proc_t p, int rv, boolean_t perf_notify
 {
 {
-       int s;
+       mach_exception_data_type_t code = 0, subcode = 0;
+
        struct uthread *ut;
        struct uthread *ut;
-       exception_data_t        code[EXCEPTION_CODE_MAX];
-       thread_act_t self = current_act();
+       thread_t self = current_thread();
+       ut = get_bsdthread_info(self);
+       struct rusage_superset *rup;
+       int kr = 0;
+       int create_corpse = FALSE;
+
+       if (p == initproc) {
+               launchd_crashed_panic(p, rv);
+               /* NOTREACHED */
+       }
 
 
-       code[0] = 0xFF000001;                   /* Set terminate code */
-       code[1] = p->p_pid;                             /* Pass out the pid     */
-       (void)sys_perf_notify(p->task, &code, 2);       /* Notify the perf server */
+       /*
+        * Generate a corefile/crashlog if:
+        *      The process doesn't have an exit reason that indicates no crash report should be created
+        *      AND any of the following are true:
+        *      - The process was terminated due to a fatal signal that generates a core
+        *      - The process was killed due to a code signing violation
+        *      - The process has an exit reason that indicates we should generate a crash report
+        *
+        * The first condition is necessary because abort_with_reason()/payload() use SIGABRT
+        * (which normally triggers a core) but may indicate that no crash report should be created.
+        */
+       if (!(PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) & OS_REASON_FLAG_NO_CRASH_REPORT)) &&
+               (hassigprop(WTERMSIG(rv), SA_CORE) || ((p->p_csflags & CS_KILLED) != 0) ||
+               (PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) &
+                                                       OS_REASON_FLAG_GENERATE_CRASH_REPORT)))) {
+               /* 
+                * Workaround for processes checking up on PT_DENY_ATTACH:
+                * should be backed out post-Leopard (details in 5431025).
+                */
+               if ((SIGSEGV == WTERMSIG(rv)) && 
+                               (p->p_pptr->p_lflag & P_LNOATTACH)) {
+                       goto skipcheck;
+               }
 
 
+               /*
+                * Crash Reporter looks for the signal value, original exception
+                * type, and low 20 bits of the original code in code[0] 
+                * (8, 4, and 20 bits respectively). code[1] is unmodified. 
+                */
+               code = ((WTERMSIG(rv) & 0xff) << 24) |
+                       ((ut->uu_exception & 0x0f) << 20) | 
+                       ((int)ut->uu_code & 0xfffff);
+               subcode = ut->uu_subcode;
+
+               kr = task_exception_notify(EXC_CRASH, code, subcode);
+
+               /* Nobody handled EXC_CRASH?? remember to make corpse */
+               if (kr != 0) {
+                       create_corpse = TRUE;
+               }
+       }
+
+skipcheck:
+       /* Notify the perf server? */
+       if (perf_notify) {
+               (void)sys_perf_notify(self, p->p_pid);
+       }
+
+
+       /* stash the usage into corpse data if making_corpse == true */
+       if (create_corpse == TRUE) {
+               kr = task_mark_corpse(p->task);
+               if (kr != KERN_SUCCESS) {
+                       if (kr == KERN_NO_SPACE) {
+                               printf("Process[%d] has no vm space for corpse info.\n", p->p_pid);
+                       } else if (kr == KERN_NOT_SUPPORTED) {
+                               printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p->p_pid);
+                       } else {
+                               printf("Process[%d] crashed: %s. Too many corpses being created.\n", p->p_pid, p->p_comm);
+                       }
+                       create_corpse = FALSE;
+               }
+       }
+
+       /*
+        * Before this process becomes a zombie, stash resource usage
+        * stats in the proc for external observers to query
+        * via proc_pid_rusage().
+        *
+        * If the zombie allocation fails, just punt the stats.
+        */
+       MALLOC_ZONE(rup, struct rusage_superset *,
+                       sizeof (*rup), M_ZOMBIE, M_WAITOK);
+       if (rup != NULL) {
+               gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
+               rup->ri.ri_phys_footprint = 0;
+               rup->ri.ri_proc_exit_abstime = mach_absolute_time();
+
+               /*
+                * Make the rusage_info visible to external observers
+                * only after it has been completely filled in.
+                */
+               p->p_ru = rup;
+       }
+       if (create_corpse) {
+               int est_knotes = 0, num_knotes = 0;
+               uint64_t *buffer = NULL;
+               int buf_size = 0;
+
+               /* Get all the udata pointers from kqueue */
+               est_knotes = kevent_proc_copy_uptrs(p, NULL, 0);
+               if (est_knotes > 0) {
+                       buf_size = (est_knotes + 32) * sizeof(uint64_t);
+                       buffer = (uint64_t *) kalloc(buf_size);
+                       num_knotes = kevent_proc_copy_uptrs(p, buffer, buf_size);
+                       if (num_knotes > est_knotes + 32) {
+                               num_knotes = est_knotes + 32;
+                       }
+               }
+
+               /* Update the code, subcode based on exit reason */
+               proc_update_corpse_exception_codes(p, &code, &subcode);
+               populate_corpse_crashinfo(p, p->task, rup,
+                               code, subcode, buffer, num_knotes, NULL);
+               if (buffer != NULL) {
+                       kfree(buffer, buf_size);
+               }
+       }
        /*
         * Remove proc from allproc queue and from pidhash chain.
         * Need to do this before we do anything that can block.
         * Not doing causes things like mount() find this on allproc
         * in partially cleaned state.
         */
        /*
         * Remove proc from allproc queue and from pidhash chain.
         * Need to do this before we do anything that can block.
         * Not doing causes things like mount() find this on allproc
         * in partially cleaned state.
         */
+
+       proc_list_lock();
+
+#if CONFIG_MEMORYSTATUS
+       memorystatus_remove(p, TRUE);
+#endif
+
        LIST_REMOVE(p, p_list);
        LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
        LIST_REMOVE(p, p_list);
        LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
-       LIST_REMOVE(p, p_hash);
+       /* will not be visible via proc_find */
+       p->p_listflag |= P_LIST_EXITED;
+
+       proc_list_unlock();
+
 
 #ifdef PGINPROF
        vmsizmon();
 #endif
        /*
         * If parent is waiting for us to exit or exec,
 
 #ifdef PGINPROF
        vmsizmon();
 #endif
        /*
         * If parent is waiting for us to exit or exec,
-        * P_PPWAIT is set; we will wakeup the parent below.
+        * P_LPPWAIT is set; we will wakeup the parent below.
         */
         */
-       p->p_flag &= ~(P_TRACED | P_PPWAIT);
-       p->p_sigignore = ~0;
-       p->p_siglist = 0;
-       ut = get_bsdthread_info(self);
+       proc_lock(p);
+       p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
+       p->p_sigignore = ~(sigcantmask);
        ut->uu_siglist = 0;
        ut->uu_siglist = 0;
-       untimeout(realitexpire, (caddr_t)p->p_pid);
+       proc_unlock(p);
 }
 
 void 
 }
 
 void 
-proc_exit(struct proc *p)
+proc_exit(proc_t p)
 {
 {
-       register struct proc *q, *nq, *pp;
+       proc_t q;
+       proc_t pp;
        struct task *task = p->task;
        struct task *task = p->task;
-       register int i,s;
-       boolean_t funnel_state;
+       vnode_t tvp = NULLVP;
+       struct pgrp * pg;
+       struct session *sessp;
+       struct uthread * uth;
+       pid_t pid;
+       int exitval;
+       int knote_hint;
+
+       uth = current_uthread();
+
+       proc_lock(p);
+       proc_transstart(p, 1, 0);
+       if( !(p->p_lflag & P_LEXIT)) {
+               /*
+                * This can happen if a thread_terminate() occurs
+                * in a single-threaded process.
+                */
+               p->p_lflag |= P_LEXIT;
+               proc_transend(p, 1);
+               proc_unlock(p);
+               proc_prepareexit(p, 0, TRUE);   
+               (void) task_terminate_internal(task);
+               proc_lock(p);
+       } else {
+               proc_transend(p, 1);
+       }
 
 
-       /* This can happen if thread_terminate of the single thread
-        * process 
-        */
+       p->p_lflag |= P_LPEXIT;
 
 
-       funnel_state = thread_funnel_set(kernel_flock, TRUE);
-       if( !(p->p_flag & P_WEXIT)) {
-               s = splsched();
-               p->p_flag |= P_WEXIT;
-               splx(s);
-               proc_prepareexit(p);    
+       /*
+        * Other kernel threads may be in the middle of signalling this process.
+        * Wait for those threads to wrap it up before making the process
+        * disappear on them.
+        */
+       if ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 0)) {
+               p->p_sigwaitcnt++;
+               while ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 1)) 
+                       msleep(&p->p_sigmask, &p->p_mlock, PWAIT, "proc_sigdrain", NULL);
+               p->p_sigwaitcnt--;
        }
 
        }
 
-       MALLOC_ZONE(p->p_ru, struct rusage *,
-                       sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
+       proc_unlock(p);
+       pid = p->p_pid;
+       exitval = p->p_xstat;
+       KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, 
+               BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START,
+               pid, exitval, 0, 0, 0);
+
+#if CONFIG_DTRACE
+       dtrace_proc_exit(p);
+#endif
+
+       nspace_proc_exit(p);
 
        /*
         * need to cancel async IO requests that can be cancelled and wait for those
         * already active.  MAY BLOCK!
         */
 
        /*
         * need to cancel async IO requests that can be cancelled and wait for those
         * already active.  MAY BLOCK!
         */
+       
+       proc_refdrain(p);
+
+       /* if any pending cpu limits action, clear it */
+       task_clear_cpuusage(p->task, TRUE);
+
+       workqueue_mark_exiting(p);
+
        _aio_exit( p );
 
        /*
        _aio_exit( p );
 
        /*
@@ -267,17 +1074,46 @@ proc_exit(struct proc *p)
         */
        fdfree(p);
 
         */
        fdfree(p);
 
+       /*
+        * Once all the knotes, kqueues & workloops are destroyed, get rid of the
+        * workqueue.
+        */
+       workqueue_exit(p);
+
+       if (uth->uu_lowpri_window) {
+               /*
+                * task is marked as a low priority I/O type
+                * and the I/O we issued while in flushing files on close
+                * collided with normal I/O operations...
+                * no need to throttle this thread since its going away
+                * but we do need to update our bookeeping w/r to throttled threads
+                */
+               throttle_lowpri_io(0);
+       }
+
+#if SYSV_SHM
        /* Close ref SYSV Shared memory*/
        if (p->vm_shm)
                shmexit(p);
        /* Close ref SYSV Shared memory*/
        if (p->vm_shm)
                shmexit(p);
+#endif
+#if SYSV_SEM
        /* Release SYSV semaphores */
        semexit(p);
        /* Release SYSV semaphores */
        semexit(p);
+#endif
        
        
-       if (SESS_LEADER(p)) {
-               register struct session *sp = p->p_session;
+#if PSYNCH
+       pth_proc_hashdelete(p);
+#endif /* PSYNCH */
+
+       sessp = proc_session(p);
+       if (SESS_LEADER(p, sessp)) {
 
 
-               if (sp->s_ttyvp) {
+               if (sessp->s_ttyvp != NULLVP) {
                        struct vnode *ttyvp;
                        struct vnode *ttyvp;
+                       int ttyvid;
+                       int cttyflag = 0;
+                       struct vfs_context context;
+                       struct tty *tp;
 
                        /*
                         * Controlling process.
 
                        /*
                         * Controlling process.
@@ -285,114 +1121,215 @@ proc_exit(struct proc *p)
                         * drain controlling terminal
                         * and revoke access to controlling terminal.
                         */
                         * drain controlling terminal
                         * and revoke access to controlling terminal.
                         */
-                       if (sp->s_ttyp->t_session == sp) {
-                               if (sp->s_ttyp->t_pgrp)
-                                       pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
-                               (void) ttywait(sp->s_ttyp);
+                       session_lock(sessp);
+                       tp = SESSION_TP(sessp);
+                       if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
+                               session_unlock(sessp);
+
                                /*
                                /*
-                                * The tty could have been revoked
-                                * if we blocked.
+                                * We're going to SIGHUP the foreground process
+                                * group. It can't change from this point on
+                                * until the revoke is complete.
+                                * The process group changes under both the tty
+                                * lock and proc_list_lock but we need only one
                                 */
                                 */
-                               if (sp->s_ttyvp)
-                                       VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
+                               tty_lock(tp);
+                               ttysetpgrphup(tp);
+                               tty_unlock(tp);
+
+                               tty_pgsignal(tp, SIGHUP, 1);
+
+                               session_lock(sessp);
+                               tp = SESSION_TP(sessp);
+                       }
+                       cttyflag = sessp->s_flags & S_CTTYREF;
+                       sessp->s_flags &= ~S_CTTYREF;
+                       ttyvp = sessp->s_ttyvp;
+                       ttyvid = sessp->s_ttyvid;
+                       sessp->s_ttyvp = NULLVP;
+                       sessp->s_ttyvid = 0;
+                       sessp->s_ttyp = TTY_NULL;
+                       sessp->s_ttypgrpid = NO_PID;
+                       session_unlock(sessp);
+
+                       if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
+                               if (tp != TTY_NULL) {
+                                       tty_lock(tp);
+                                       (void) ttywait(tp);
+                                       tty_unlock(tp);
+                               }
+                               context.vc_thread = proc_thread(p); /* XXX */
+                               context.vc_ucred = kauth_cred_proc_ref(p);
+                               VNOP_REVOKE(ttyvp, REVOKEALL, &context);
+                               if (cttyflag) {
+                                       /*
+                                        * Release the extra usecount taken in cttyopen.
+                                        * usecount should be released after VNOP_REVOKE is called.
+                                        * This usecount was taken to ensure that
+                                        * the VNOP_REVOKE results in a close to
+                                        * the tty since cttyclose is a no-op.
+                                        */
+                                       vnode_rele(ttyvp);
+                               }
+                               vnode_put(ttyvp);
+                               kauth_cred_unref(&context.vc_ucred);
+                               ttyvp = NULLVP;
+                       }
+                       if (tp) {
+                               /*
+                                * This is cleared even if not set. This is also done in
+                                * spec_close to ensure that the flag is cleared.
+                                */
+                               tty_lock(tp);
+                               ttyclrpgrphup(tp);
+                               tty_unlock(tp);
+
+                               ttyfree(tp);
                        }
                        }
-                       ttyvp = sp->s_ttyvp;
-                       sp->s_ttyvp = NULL;
-                       if (ttyvp)
-                               vrele(ttyvp);
-                       /*
-                        * s_ttyp is not zero'd; we use this to indicate
-                        * that the session once had a controlling terminal.
-                        * (for logging and informational purposes)
-                        */
                }
                }
-               sp->s_leader = NULL;
+               session_lock(sessp);
+               sessp->s_leader = NULL;
+               session_unlock(sessp);
        }
        }
+       session_rele(sessp);
+
+       pg = proc_pgrp(p);
+       fixjobc(p, pg, 0);
+       pg_rele(pg);
 
 
-       fixjobc(p, p->p_pgrp, 0);
        p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
        (void)acct_process(p);
 
        p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
        (void)acct_process(p);
 
-#if KTRACE
-       /* 
-        * release trace file
-        */
-       p->p_traceflag = 0;     /* don't trace the vrele() */
-       if (p->p_tracep) {
-               struct vnode *tvp = p->p_tracep;
-               p->p_tracep = NULL;
+       proc_list_lock();
 
 
-               if (UBCINFOEXISTS(tvp))
-                       ubc_rele(tvp);
-               vrele(tvp);
+       if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
+               p->p_listflag &= ~P_LIST_EXITCOUNT;
+               proc_shutdown_exitcount--;
+               if (proc_shutdown_exitcount == 0)
+                       wakeup(&proc_shutdown_exitcount);
        }
        }
-#endif
 
 
-       q = p->p_children.lh_first;
-       if (q)          /* only need this if any child is S_ZOMB */
-               wakeup((caddr_t) initproc);
-       for (; q != 0; q = nq) {
-               nq = q->p_sibling.le_next;
-               proc_reparent(q, initproc);
-               /*
-                * Traced processes are killed
-                * since their existence means someone is messing up.
-                */
-               if (q->p_flag & P_TRACED) {
-                       q->p_flag &= ~P_TRACED;
-                       if (q->sigwait_thread) {
+       /* wait till parentrefs are dropped and grant no more */
+       proc_childdrainstart(p);
+       while ((q = p->p_children.lh_first) != NULL) {
+               int reparentedtoinit = (q->p_listflag & P_LIST_DEADPARENT) ? 1 : 0;
+               if (q->p_stat == SZOMB) {
+                       if (p != q->p_pptr)
+                               panic("parent child linkage broken");
+                       /* check for sysctl zomb lookup */
+                       while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
+                               msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
+                       }
+                       q->p_listflag |= P_LIST_WAITING;
+                       /*
+                        * This is a named reference and it is not granted
+                        * if the reap is already in progress. So we get
+                        * the reference here exclusively and their can be
+                        * no waiters. So there is no need for a wakeup
+                        * after we are done.  Also the reap frees the structure
+                        * and the proc struct cannot be used for wakeups as well. 
+                        * It is safe to use q here as this is system reap
+                        */
+                       (void)reap_child_locked(p, q, 1, reparentedtoinit, 1, 0);
+               } else {
+                       /*
+                       * Traced processes are killed
+                       * since their existence means someone is messing up.
+                       */
+                       if (q->p_lflag & P_LTRACED) {
+                               struct proc *opp;
+
                                /*
                                /*
-                                * The sigwait_thread could be stopped at a
-                                * breakpoint. Wake it up to kill.
-                                * Need to do this as it could be a thread which is not
-                                * the first thread in the task. So any attempts to kill
-                                * the process would result into a deadlock on q->sigwait.
+                                * Take a reference on the child process to
+                                * ensure it doesn't exit and disappear between
+                                * the time we drop the list_lock and attempt
+                                * to acquire its proc_lock.
                                 */
                                 */
-                               thread_resume((thread_act_t)q->sigwait_thread);
-                               clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
-                               threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
+                               if (proc_ref_locked(q) != q)
+                                       continue;
+
+                               proc_list_unlock();
+
+                               opp = proc_find(q->p_oppid);
+                               if (opp != PROC_NULL) {
+                                       proc_list_lock();
+                                       q->p_oppid = 0;
+                                       proc_list_unlock();
+                                       proc_reparentlocked(q, opp, 0, 0);
+                                       proc_rele(opp);
+                               } else {
+                                       /* original parent exited while traced */
+                                       proc_list_lock();
+                                       q->p_listflag |= P_LIST_DEADPARENT;
+                                       q->p_oppid = 0;
+                                       proc_list_unlock();
+                                       proc_reparentlocked(q, initproc, 0, 0);
+                               }
+
+                               proc_lock(q);
+                               q->p_lflag &= ~P_LTRACED;
+
+                               if (q->sigwait_thread) {
+                                       thread_t thread = q->sigwait_thread;
+
+                                       proc_unlock(q);
+                                       /*
+                                       * The sigwait_thread could be stopped at a
+                                       * breakpoint. Wake it up to kill.
+                                       * Need to do this as it could be a thread which is not
+                                       * the first thread in the task. So any attempts to kill
+                                       * the process would result into a deadlock on q->sigwait.
+                                       */
+                                       thread_resume(thread);
+                                       clear_wait(thread, THREAD_INTERRUPTED);
+                                       threadsignal(thread, SIGKILL, 0, TRUE);
+                               } else {
+                                       proc_unlock(q);
+                               }
+
+                               psignal(q, SIGKILL);
+                               proc_list_lock();
+                               proc_rele_locked(q);
+                       } else {
+                               q->p_listflag |= P_LIST_DEADPARENT;
+                               proc_reparentlocked(q, initproc, 0, 1);
                        }
                        }
-                       psignal(q, SIGKILL);
                }
        }
 
                }
        }
 
+       proc_childdrainend(p);
+       proc_list_unlock();
+
+#if CONFIG_MACF
        /*
        /*
-        * Save exit status and final rusage info, adding in child rusage
-        * info and self times.
+        * Notify MAC policies that proc is dead.
+        * This should be replaced with proper label management
+        * (rdar://problem/32126399).
         */
         */
-       *p->p_ru = p->p_stats->p_ru;
-
-       timerclear(&p->p_ru->ru_utime);
-       timerclear(&p->p_ru->ru_stime);
-
-       if (task) {
-               task_basic_info_data_t tinfo;
-               task_thread_times_info_data_t ttimesinfo;
-               int task_info_stuff, task_ttimes_stuff;
-               struct timeval ut,st;
+       mac_proc_notify_exit(p);
+#endif
 
 
-               task_info_stuff = TASK_BASIC_INFO_COUNT;
-               task_info(task, TASK_BASIC_INFO,
-                         &tinfo, &task_info_stuff);
-               p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
-               p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
-               p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
-               p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
+       /*
+        * Release reference to text vnode
+        */
+       tvp = p->p_textvp;
+       p->p_textvp = NULL;
+       if (tvp != NULLVP) {
+               vnode_rele(tvp);
+       }
 
 
-               task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
-               task_info(task, TASK_THREAD_TIMES_INFO,
-                         &ttimesinfo, &task_ttimes_stuff);
+       /*
+        * Save exit status and final rusage info, adding in child rusage
+        * info and self times.  If we were unable to allocate a zombie
+        * structure, this information is lost.
+        */
+       if (p->p_ru != NULL) {
+           calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, NULL);
+           p->p_ru->ru = p->p_stats->p_ru;
 
 
-               ut.tv_sec = ttimesinfo.user_time.seconds;
-               ut.tv_usec = ttimesinfo.user_time.microseconds;
-               st.tv_sec = ttimesinfo.system_time.seconds;
-               st.tv_usec = ttimesinfo.system_time.microseconds;
-               timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
-               timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
+           ruadd(&(p->p_ru->ru), &p->p_stats->p_cru);
        }
 
        }
 
-       ruadd(p->p_ru, &p->p_stats->p_cru);
-
        /*
         * Free up profiling buffers.
         */
        /*
         * Free up profiling buffers.
         */
@@ -405,25 +1342,25 @@ proc_exit(struct proc *p)
 
                for (; p1 != NULL; p1 = pn) {
                        pn = p1->pr_next;
 
                for (; p1 != NULL; p1 = pn) {
                        pn = p1->pr_next;
-                       kfree((vm_offset_t)p1, sizeof *p1);
+                       kfree(p1, sizeof *p1);
                }
        }
 
                }
        }
 
+       proc_free_realitimer(p);
+
        /*
         * Other substructures are freed from wait().
         */
        /*
         * Other substructures are freed from wait().
         */
-       FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
+       FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
        p->p_stats = NULL;
 
        p->p_stats = NULL;
 
-       FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
+       FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
        p->p_sigacts = NULL;
 
        p->p_sigacts = NULL;
 
-       if (--p->p_limit->p_refcnt == 0)
-               FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
+       proc_limitdrop(p, 1);
        p->p_limit = NULL;
 
        p->p_limit = NULL;
 
-       /* Free the auditing info */
-       audit_proc_free(p);
+       vm_purgeable_disown(p->task);
 
        /*
         * Finish up by terminating the task
 
        /*
         * Finish up by terminating the task
@@ -431,353 +1368,856 @@ proc_exit(struct proc *p)
         * member of the task exiting).
         */
        p->task = TASK_NULL;
         * member of the task exiting).
         */
        p->task = TASK_NULL;
-       //task->proc = NULL;
        set_bsdtask_info(task, NULL);
 
        set_bsdtask_info(task, NULL);
 
-       KNOTE(&p->p_klist, NOTE_EXIT);
+       knote_hint = NOTE_EXIT | (p->p_xstat & 0xffff);
+       proc_knote(p, knote_hint);
 
 
+       /* mark the thread as the one that is doing proc_exit
+        * no need to hold proc lock in uthread_free
+        */
+       uth->uu_flag |= UT_PROCEXIT;
        /*
         * Notify parent that we're gone.
         */
        /*
         * Notify parent that we're gone.
         */
-       if (p->p_pptr->p_flag & P_NOCLDWAIT) {
-               struct proc * pp = p->p_pptr;
+       pp = proc_parent(p);
+       if (pp->p_flag & P_NOCLDWAIT) {
 
 
+               if (p->p_ru != NULL) {
+                       proc_lock(pp);
+#if 3839178
+               /*
+                * If the parent is ignoring SIGCHLD, then POSIX requires
+                * us to not add the resource usage to the parent process -
+                * we are only going to hand it off to init to get reaped.
+                * We should contest the standard in this case on the basis
+                * of RLIMIT_CPU.
+                */
+#else  /* !3839178 */
                /*
                 * Add child resource usage to parent before giving
                /*
                 * Add child resource usage to parent before giving
-                * zombie to init
+                * zombie to init.  If we were unable to allocate a
+                * zombie structure, this information is lost.
+                */
+                       ruadd(&pp->p_stats->p_cru, &p->p_ru->ru);
+#endif /* !3839178 */
+                       update_rusage_info_child(&pp->p_stats->ri_child, &p->p_ru->ri);
+                       proc_unlock(pp);
+               }
+               
+               /* kernel can reap this one, no need to move it to launchd */
+               proc_list_lock();
+               p->p_listflag |= P_LIST_DEADPARENT;
+               proc_list_unlock();
+       }
+       if ((p->p_listflag & P_LIST_DEADPARENT) == 0 || p->p_oppid) {
+               if (pp != initproc) {
+                       proc_lock(pp);
+                       pp->si_pid = p->p_pid;
+                       pp->p_xhighbits = p->p_xhighbits;
+                       p->p_xhighbits = 0;
+                       pp->si_status = p->p_xstat;
+                       pp->si_code = CLD_EXITED;
+                       /*
+                        * p_ucred usage is safe as it is an exiting process
+                        * and reference is dropped in reap
+                        */
+                       pp->si_uid = kauth_cred_getruid(p->p_ucred);
+                       proc_unlock(pp);
+               }
+               /* mark as a zombie */
+               /* No need to take proc lock as all refs are drained and
+                * no one except parent (reaping ) can look at this.
+                * The write is to an int and is coherent. Also parent is
+                *  keyed off of list lock for reaping
+                */
+               KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
+                       BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
+                       pid, exitval, 0, 0, 0);
+               p->p_stat = SZOMB;
+               /* 
+                * The current process can be reaped so, no one
+                * can depend on this
                 */
                 */
-               ruadd(&p->p_pptr->p_stats->p_cru, p->p_ru);
 
 
-               proc_reparent(p, initproc);
-               /* If there are no more children wakeup parent */
-               if (LIST_EMPTY(&pp->p_children))
-                       wakeup((caddr_t)pp);
+               psignal(pp, SIGCHLD);
+       
+               /* and now wakeup the parent */
+               proc_list_lock();
+               wakeup((caddr_t)pp);
+               proc_list_unlock();
+       } else {
+               /* should be fine as parent proc would be initproc */
+               /* mark as a zombie */
+               /* No need to take proc lock as all refs are drained and
+                * no one except parent (reaping ) can look at this.
+                * The write is to an int and is coherent. Also parent is
+                *  keyed off of list lock for reaping
+                */
+               proc_list_lock();
+               KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
+                       BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
+                       pid, exitval, 0, 0, 0);
+               /* check for sysctl zomb lookup */
+               while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
+                       msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
+               }
+               /* safe to use p as this is a system reap */
+               p->p_stat = SZOMB;
+               p->p_listflag |= P_LIST_WAITING;
+
+               /*
+                * This is a named reference and it is not granted
+                * if the reap is already in progress. So we get
+                * the reference here exclusively and their can be
+                * no waiters. So there is no need for a wakeup
+                * after we are done. AlsO  the reap frees the structure
+                * and the proc struct cannot be used for wakeups as well. 
+                * It is safe to use p here as this is system reap
+                */
+               (void)reap_child_locked(pp, p, 1, 0, 1, 1);
+               /* list lock dropped by reap_child_locked */
        }
        }
-       /* should be fine as parent proc would be initproc */
-       pp = p->p_pptr;
-       if (pp != initproc) {
-               pp->si_pid = p->p_pid;
-               pp->si_status = p->p_xstat;
-               pp->si_code = CLD_EXITED;
-               pp->si_uid = p->p_cred->p_ruid;
+       if (uth->uu_lowpri_window) {
+               /*
+                * task is marked as a low priority I/O type and we've
+                * somehow picked up another throttle during exit processing...
+                * no need to throttle this thread since its going away
+                * but we do need to update our bookeeping w/r to throttled threads
+                */
+               throttle_lowpri_io(0);
        }
        }
-       psignal(pp, SIGCHLD);
 
 
+       proc_rele(pp);
 
 
-       /* mark as a zombie */
-       p->p_stat = SZOMB;
+}
 
 
-       /* and now wakeup the parent */
-       wakeup((caddr_t)p->p_pptr);
 
 
-       (void) thread_funnel_set(kernel_flock, funnel_state);
-}
+/*
+ * reap_child_locked
+ *
+ * Description:        Given a process from which all status information needed
+ *             has already been extracted, if the process is a ptrace
+ *             attach process, detach it and give it back to its real
+ *             parent, else recover all resources remaining associated
+ *             with it.
+ *
+ * Parameters: proc_t parent           Parent of process being reaped
+ *             proc_t child            Process to reap
+ *
+ * Returns:    0                       Process was not reaped because it
+ *                                     came from an attach
+ *             1                       Process was reaped
+ */
+static int
+reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock)
+{
+       proc_t trace_parent = PROC_NULL;        /* Traced parent process, if tracing */
 
 
+       if (locked == 1)
+               proc_list_unlock();
+       
+       /*
+        * If we got the child via a ptrace 'attach',
+        * we need to give it back to the old parent.
+        *
+        * Exception: someone who has been reparented to launchd before being
+        * ptraced can simply be reaped, refer to radar 5677288
+        *      p_oppid                  -> ptraced
+        *      trace_parent == initproc -> away from launchd
+        *      reparentedtoinit         -> came to launchd by reparenting
+        */
+       if (child->p_oppid) {
+               int knote_hint;
+               pid_t oppid;
+
+               proc_lock(child);
+               oppid = child->p_oppid;
+               child->p_oppid = 0;
+               knote_hint = NOTE_EXIT | (child->p_xstat & 0xffff);
+               proc_unlock(child);
+
+               if ((trace_parent = proc_find(oppid))
+                       && !((trace_parent == initproc) && reparentedtoinit)) {
+                               
+                       if (trace_parent != initproc) {
+                               /* 
+                                * proc internal fileds  and p_ucred usage safe 
+                                * here as child is dead and is not reaped or 
+                                * reparented yet 
+                                */
+                               proc_lock(trace_parent);
+                               trace_parent->si_pid = child->p_pid;
+                               trace_parent->si_status = child->p_xstat;
+                               trace_parent->si_code = CLD_CONTINUED;
+                               trace_parent->si_uid = kauth_cred_getruid(child->p_ucred);
+                               proc_unlock(trace_parent);
+                       }
+                       proc_reparentlocked(child, trace_parent, 1, 0);
+                       
+                       /* resend knote to original parent (and others) after reparenting */
+                       proc_knote(child, knote_hint);
+                       
+                       psignal(trace_parent, SIGCHLD);
+                       proc_list_lock();
+                       wakeup((caddr_t)trace_parent);
+                       child->p_listflag &= ~P_LIST_WAITING;
+                       wakeup(&child->p_stat);
+                       proc_list_unlock();
+                       proc_rele(trace_parent);
+                       if ((locked == 1) && (droplock == 0))
+                               proc_list_lock();
+                       return (0);
+               }
 
 
-struct wait4_args {
-       int     pid;
-       int *status;
-       int options;
-       struct rusage *rusage;
-};
+               /*
+                * If we can't reparent (e.g. the original parent exited while child was being debugged, or
+                * original parent is the same as the debugger currently exiting), we still need to satisfy
+                * the knote lifecycle for other observers on the system. While the debugger was attached,
+                * the NOTE_EXIT would not have been broadcast during initial child termination.
+                */
+               proc_knote(child, knote_hint);
 
 
-#if COMPAT_43
-int
-owait(p, uap, retval)
-       struct proc *p;
-       void *uap;
-       int *retval;
-{
-       struct wait4_args *a;
+               if (trace_parent != PROC_NULL) {
+                       proc_rele(trace_parent);
+               }
+       }
+       
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+       proc_knote(child, NOTE_REAP);
+#pragma clang diagnostic pop
 
 
-       a = (struct wait4_args *)get_bsduthreadarg(current_act());
+       proc_knote_drain(child);
 
 
-       a->options = 0;
-       a->rusage = NULL;
-       a->pid = WAIT_ANY;
-       a->status = NULL;
-       return (wait1(p, a, retval, 1));
-}
+       child->p_xstat = 0;
+       if (child->p_ru) {
+               proc_lock(parent);
+#if 3839178
+               /*
+                * If the parent is ignoring SIGCHLD, then POSIX requires
+                * us to not add the resource usage to the parent process -
+                * we are only going to hand it off to init to get reaped.
+                * We should contest the standard in this case on the basis
+                * of RLIMIT_CPU.
+                */
+               if (!(parent->p_flag & P_NOCLDWAIT))
+#endif /* 3839178 */
+                       ruadd(&parent->p_stats->p_cru, &child->p_ru->ru);
+               update_rusage_info_child(&parent->p_stats->ri_child, &child->p_ru->ri);
+               proc_unlock(parent);
+               FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE);
+               child->p_ru = NULL;
+       } else {
+               printf("Warning : lost p_ru for %s\n", child->p_comm);
+       }
 
 
-int
-wait4(p, uap, retval)
-       struct proc *p;
-       struct wait4_args *uap;
-       int *retval;
-{
-       return (wait1(p, uap, retval, 0));
-}
+       AUDIT_SESSION_PROCEXIT(child);
+
+       /*
+        * Decrement the count of procs running with this uid.
+        * p_ucred usage is safe here as it is an exited process.
+        * and refernce is dropped after these calls down below
+        * (locking protection is provided by list lock held in chgproccnt)
+        */
+#if CONFIG_PERSONAS
+       /*
+        * persona_proc_drop calls chgproccnt(-1) on the persona uid,
+        * and (+1) on the child->p_ucred uid
+        */
+       persona_proc_drop(child);
+#endif
+       (void)chgproccnt(kauth_cred_getruid(child->p_ucred), -1);
+
+       os_reason_free(child->p_exit_reason);
+
+       /*
+        * Free up credentials.
+        */
+       if (IS_VALID_CRED(child->p_ucred)) {
+               kauth_cred_unref(&child->p_ucred);
+       }
+
+       /*  XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
+
+       /*
+        * Finally finished with old proc entry.
+        * Unlink it from its process group and free it.
+        */
+       leavepgrp(child);
 
 
-struct owait3_args {
-       int *status;
-       int options;
-       struct rusage *rusage;
-};
+       proc_list_lock();
+       LIST_REMOVE(child, p_list);     /* off zombproc */
+       parent->p_childrencnt--;
+       LIST_REMOVE(child, p_sibling);
+       /* If there are no more children wakeup parent */
+       if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children)))
+               wakeup((caddr_t)parent);        /* with list lock held */
+       child->p_listflag &= ~P_LIST_WAITING;
+       wakeup(&child->p_stat);
+
+       /* Take it out of process hash */
+       LIST_REMOVE(child, p_hash);
+       child->p_listflag &= ~P_LIST_INHASH;
+       proc_checkdeadrefs(child);
+       nprocs--;
+
+       if (deadparent) {
+               /*
+                * If a child zombie is being reaped because its parent
+                * is exiting, make sure we update the list flag
+                */
+               child->p_listflag |= P_LIST_DEADPARENT;
+       }
 
 
-int
-owait3(p, uap, retval)
-       struct proc *p;
-       struct owait3_args *uap;
-       int *retval;
-{
-       struct wait4_args *a;
+       proc_list_unlock();
 
 
-       a = (struct wait4_args *)get_bsduthreadarg(current_act());
+#if CONFIG_FINE_LOCK_GROUPS
+       lck_mtx_destroy(&child->p_mlock, proc_mlock_grp);
+       lck_mtx_destroy(&child->p_ucred_mlock, proc_ucred_mlock_grp);
+       lck_mtx_destroy(&child->p_fdmlock, proc_fdmlock_grp);
+#if CONFIG_DTRACE
+       lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
+#endif
+       lck_spin_destroy(&child->p_slock, proc_slock_grp);
+#else /* CONFIG_FINE_LOCK_GROUPS */
+       lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
+       lck_mtx_destroy(&child->p_ucred_mlock, proc_lck_grp);
+       lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
+#if CONFIG_DTRACE
+       lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
+#endif
+       lck_spin_destroy(&child->p_slock, proc_lck_grp);
+#endif /* CONFIG_FINE_LOCK_GROUPS */
 
 
-       a->rusage = uap->rusage;
-       a->options = uap->options;
-       a->status = uap->status;
-       a->pid = WAIT_ANY;
+       FREE_ZONE(child, sizeof *child, M_PROC);
+       if ((locked == 1) && (droplock == 0))
+               proc_list_lock();
 
 
-       return (wait1(p, a, retval, 1));
+       return (1);
 }
 
 }
 
-#else
-#define        wait1   wait4
-#endif
 
 int
 
 int
-wait1continue(result)
+wait1continue(int result)
 {
 {
-       void *vt;
-       thread_act_t thread;
+       proc_t p;
+       thread_t thread;
+       uthread_t uth;
+       struct _wait4_data *wait4_data;
+       struct wait4_nocancel_args *uap;
        int *retval;
        int *retval;
-       struct proc *p;
 
        if (result)
                return(result);
 
        p = current_proc();
 
        if (result)
                return(result);
 
        p = current_proc();
-       thread = current_act();
-       vt = (void *)get_bsduthreadarg(thread);
-       retval = (int *)get_bsduthreadrval(thread);
-       return(wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0));
+       thread = current_thread();
+       uth = (struct uthread *)get_bsdthread_info(thread);
+
+       wait4_data = &uth->uu_kevent.uu_wait4_data;
+       uap = wait4_data->args;
+       retval = wait4_data->retval;
+       return(wait4_nocancel(p, uap, retval));
 }
 
 int
 }
 
 int
-wait1(q, uap, retval, compat)
-       register struct proc *q;
-       register struct wait4_args *uap;
-       register_t *retval;
-#if COMPAT_43
-       int compat;
-#endif
+wait4(proc_t q, struct wait4_args *uap, int32_t *retval)
+{
+       __pthread_testcancel(1);
+       return(wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval));
+}
+
+int
+wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval)
 {
 {
-       register int nfound;
-       register struct proc *p, *t;
+       int nfound;
+       int sibling_count;
+       proc_t p;
        int status, error;
        int status, error;
-       struct vnode *tvp;
+       uthread_t uth;
+       struct _wait4_data *wait4_data;
+
+       AUDIT_ARG(pid, uap->pid);
 
 
-retry:
        if (uap->pid == 0)
        if (uap->pid == 0)
-               uap->pid = -q->p_pgid;
+               uap->pid = -q->p_pgrpid;
 
 loop:
 
 loop:
+       proc_list_lock();
+loop1:
        nfound = 0;
        nfound = 0;
-       for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
+       sibling_count = 0;
+
+       PCHILDREN_FOREACH(q, p) {
+               if ( p->p_sibling.le_next != 0 )
+                       sibling_count++;
                if (uap->pid != WAIT_ANY &&
                    p->p_pid != uap->pid &&
                if (uap->pid != WAIT_ANY &&
                    p->p_pid != uap->pid &&
-                   p->p_pgid != -(uap->pid))
+                   p->p_pgrpid != -(uap->pid))
                        continue;
                        continue;
+
                nfound++;
                nfound++;
-               if (p->p_flag & P_WAITING) {
-                       (void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0);
-                       goto loop;
+
+               /* XXX This is racy because we don't get the lock!!!! */
+
+               if (p->p_listflag & P_LIST_WAITING) {
+                       (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
+                       goto loop1;
                }
                }
-               p->p_flag |= P_WAITING;   /* only allow single thread to wait() */
+               p->p_listflag |= P_LIST_WAITING;   /* only allow single thread to wait() */
+
 
                if (p->p_stat == SZOMB) {
 
                if (p->p_stat == SZOMB) {
-                       retval[0] = p->p_pid;
-#if COMPAT_43
-                       if (compat)
-                               retval[1] = p->p_xstat;
-                       else
+                       int reparentedtoinit = (p->p_listflag & P_LIST_DEADPARENT) ? 1 : 0;
+
+                       proc_list_unlock();
+#if CONFIG_MACF
+                       if ((error = mac_proc_check_wait(q, p)) != 0)
+                               goto out;
 #endif
 #endif
+                       retval[0] = p->p_pid;
                        if (uap->status) {
                        if (uap->status) {
-                               status = p->p_xstat;    /* convert to int */
-                               if (error = copyout((caddr_t)&status,
-                                   (caddr_t)uap->status,
-                                                   sizeof(status))) {
-                                       p->p_flag &= ~P_WAITING;
-                                       wakeup(&p->p_stat);
-                                       return (error);
-                               }
-                       }
-                       if (uap->rusage &&
-                           (error = copyout((caddr_t)p->p_ru,
-                           (caddr_t)uap->rusage,
-                                            sizeof (struct rusage)))) {
-                               p->p_flag &= ~P_WAITING;
-                               wakeup(&p->p_stat);
-                               return (error);
+                               /* Legacy apps expect only 8 bits of status */
+                               status = 0xffff & p->p_xstat;   /* convert to int */
+                               error = copyout((caddr_t)&status,
+                                                       uap->status,
+                                                   sizeof(status));
+                               if (error) 
+                                       goto out;
                        }
                        }
-                       /*
-                        * If we got the child via a ptrace 'attach',
-                        * we need to give it back to the old parent.
-                        */
-                       if (p->p_oppid && (t = pfind(p->p_oppid))) {
-                               p->p_oppid = 0;
-                               proc_reparent(p, t);
-                               if (t != initproc) {
-                                       t->si_pid = p->p_pid;
-                                       t->si_status = p->p_xstat;
-                                       t->si_code = CLD_CONTINUED;
-                                       t->si_uid = p->p_cred->p_ruid;
+                       if (uap->rusage) {
+                               if (p->p_ru == NULL) {
+                                       error = ENOMEM;
+                               } else {
+                                       if (IS_64BIT_PROCESS(q)) {
+                                               struct user64_rusage    my_rusage;
+                                               munge_user64_rusage(&p->p_ru->ru, &my_rusage);
+                                               error = copyout((caddr_t)&my_rusage,
+                                                       uap->rusage,
+                                                       sizeof (my_rusage));
+                                       }
+                                       else {
+                                               struct user32_rusage    my_rusage;
+                                               munge_user32_rusage(&p->p_ru->ru, &my_rusage);
+                                               error = copyout((caddr_t)&my_rusage,
+                                                       uap->rusage,
+                                                       sizeof (my_rusage));
+                                       }
                                }
                                }
-                               psignal(t, SIGCHLD);
-                               wakeup((caddr_t)t);
-                               p->p_flag &= ~P_WAITING;
-                               wakeup(&p->p_stat);
-                               return (0);
-                       }
-                       p->p_xstat = 0;
-                       if (p->p_ru) {
-                               ruadd(&q->p_stats->p_cru, p->p_ru);
-                               FREE_ZONE(p->p_ru, sizeof *p->p_ru, M_ZOMBIE);
-                               p->p_ru = NULL;
-                       } else {
-                               printf("Warning : lost p_ru for %s\n", p->p_comm);
+                               /* information unavailable? */
+                               if (error) 
+                                       goto out;
                        }
 
                        }
 
-                       /*
-                        * Decrement the count of procs running with this uid.
-                        */
-                       (void)chgproccnt(p->p_cred->p_ruid, -1);
-
-                       /*
-                        * Free up credentials.
+                       /* Conformance change for 6577252.
+                        * When SIGCHLD is blocked and wait() returns because the status
+                        * of a child process is available and there are no other 
+                        * children processes, then any pending SIGCHLD signal is cleared.
                         */
                         */
-                       if (--p->p_cred->p_refcnt == 0) {
-                               struct ucred *ucr = p->p_ucred;
-                               struct pcred *pcr;
-
-                               if (ucr != NOCRED) {
-                                       p->p_ucred = NOCRED;
-                                       crfree(ucr);
+                       if ( sibling_count == 0 ) {
+                               int mask = sigmask(SIGCHLD);
+                               uth = current_uthread();
+
+                               if ( (uth->uu_sigmask & mask) != 0 ) {
+                                       /* we are blocking SIGCHLD signals.  clear any pending SIGCHLD.
+                                        * This locking looks funny but it is protecting access to the 
+                                        * thread via p_uthlist.
+                                        */
+                                       proc_lock(q); 
+                                       uth->uu_siglist &= ~mask;       /* clear pending signal */
+                                       proc_unlock(q);
                                }
                                }
-                               pcr = p->p_cred;
-                               p->p_cred = NULL;
-                               FREE_ZONE(pcr, sizeof *pcr, M_SUBPROC);
                        }
                        }
+                       
+                       /* Clean up */
+                       (void)reap_child_locked(q, p, 0, reparentedtoinit, 0, 0);
 
 
-                       /*
-                        * Release reference to text vnode
-                        */
-                       tvp = p->p_textvp;
-                       p->p_textvp = NULL;
-                       if (tvp)
-                               vrele(tvp);
-
-                       /*
-                        * Finally finished with old proc entry.
-                        * Unlink it from its process group and free it.
-                        */
-                       leavepgrp(p);
-                       LIST_REMOVE(p, p_list); /* off zombproc */
-                       LIST_REMOVE(p, p_sibling);
-                       p->p_flag &= ~P_WAITING;
-                       FREE_ZONE(p, sizeof *p, M_PROC);
-                       nprocs--;
-                       wakeup(&p->p_stat);
                        return (0);
                }
                        return (0);
                }
-               if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
-                   (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
-                       p->p_flag |= P_WAITED;
+               if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 &&
+                   (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) {
+                       proc_list_unlock();
+#if CONFIG_MACF
+                       if ((error = mac_proc_check_wait(q, p)) != 0)
+                               goto out;
+#endif
+                       proc_lock(p);
+                       p->p_lflag |= P_LWAITED;
+                       proc_unlock(p);
                        retval[0] = p->p_pid;
                        retval[0] = p->p_pid;
-#if COMPAT_43
-                       if (compat) {
-                               retval[1] = W_STOPCODE(p->p_xstat);
-                               error = 0;
+                       if (uap->status) {
+                               status = W_STOPCODE(p->p_xstat);
+                               error = copyout((caddr_t)&status,
+                                       uap->status,
+                                   sizeof(status));
                        } else
                        } else
+                               error = 0;
+                       goto out;
+               }
+               /*
+                * If we are waiting for continued processses, and this
+                * process was continued
+                */
+               if ((uap->options & WCONTINUED) &&
+                   (p->p_flag & P_CONTINUED)) {
+                       proc_list_unlock();
+#if CONFIG_MACF
+                       if ((error = mac_proc_check_wait(q, p)) != 0)
+                               goto out;
 #endif
 #endif
+
+                       /* Prevent other process for waiting for this event */
+                       OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
+                       retval[0] = p->p_pid;
                        if (uap->status) {
                        if (uap->status) {
-                               status = W_STOPCODE(p->p_xstat);
+                               status = W_STOPCODE(SIGCONT);
                                error = copyout((caddr_t)&status,
                                error = copyout((caddr_t)&status,
-                                   (caddr_t)uap->status,
+                                       uap->status,
                                    sizeof(status));
                        } else
                                error = 0;
                                    sizeof(status));
                        } else
                                error = 0;
-                       p->p_flag &= ~P_WAITING;
-                       wakeup(&p->p_stat);
-                       return (error);
+                       goto out;
                }
                }
-               p->p_flag &= ~P_WAITING;
+               p->p_listflag &= ~P_LIST_WAITING;
                wakeup(&p->p_stat);
        }
                wakeup(&p->p_stat);
        }
-       if (nfound == 0)
+       /* list lock is held when we get here any which way */
+       if (nfound == 0) {
+               proc_list_unlock();
                return (ECHILD);
                return (ECHILD);
+       }
 
        if (uap->options & WNOHANG) {
                retval[0] = 0;
 
        if (uap->options & WNOHANG) {
                retval[0] = 0;
+               proc_list_unlock();
                return (0);
        }
 
                return (0);
        }
 
-       if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue))
+       /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
+       uth = current_uthread();
+       wait4_data = &uth->uu_kevent.uu_wait4_data;
+       wait4_data->args = uap;
+       wait4_data->retval = retval;
+
+       if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue)))
                return (error);
 
        goto loop;
                return (error);
 
        goto loop;
+out:
+       proc_list_lock();
+       p->p_listflag &= ~P_LIST_WAITING;
+       wakeup(&p->p_stat);
+       proc_list_unlock();
+       return (error);
 }
 
 }
 
-/*
- * make process 'parent' the new parent of process 'child'.
- */
-void
-proc_reparent(child, parent)
-       register struct proc *child;
-       register struct proc *parent;
+#if DEBUG
+#define ASSERT_LCK_MTX_OWNED(lock)     \
+                               lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
+#else
+#define ASSERT_LCK_MTX_OWNED(lock)     /* nothing */
+#endif
+
+int
+waitidcontinue(int result)
 {
 {
+       proc_t p;
+       thread_t thread;
+       uthread_t uth;
+       struct _waitid_data *waitid_data;
+       struct waitid_nocancel_args *uap;
+       int *retval;
 
 
-       if (child->p_pptr == parent)
-               return;
+       if (result)
+               return (result);
 
 
-       LIST_REMOVE(child, p_sibling);
-       LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
-       child->p_pptr = parent;
+       p = current_proc();
+       thread = current_thread();
+       uth = (struct uthread *)get_bsdthread_info(thread);
+
+       waitid_data = &uth->uu_kevent.uu_waitid_data;
+       uap = waitid_data->args;
+       retval = waitid_data->retval;
+       return(waitid_nocancel(p, uap, retval));
 }
 
 /*
 }
 
 /*
- *     Make the current process an "init" process, meaning
- *     that it doesn't have a parent, and that it won't be
- *     gunned down by kill(-1, 0).
+ * Description:        Suspend the calling thread until one child of the process
+ *             containing the calling thread changes state.
+ *
+ * Parameters: uap->idtype             one of P_PID, P_PGID, P_ALL
+ *             uap->id                 pid_t or gid_t or ignored
+ *             uap->infop              Address of siginfo_t struct in
+ *                                     user space into which to return status
+ *             uap->options            flag values
+ *
+ * Returns:    0                       Success
+ *             !0                      Error returning status to user space
  */
  */
-kern_return_t
-init_process(void)
+int
+waitid(proc_t q, struct waitid_args *uap, int32_t *retval)
 {
 {
-       register struct proc *p = current_proc();
+       __pthread_testcancel(1);
+       return (waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval));
+}
 
 
-       AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS);
-       if (suser(p->p_ucred, &p->p_acflag)) {
-               AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS);
-               return(KERN_NO_ACCESS);
+int
+waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap,
+       __unused int32_t *retval)
+{
+       user_siginfo_t  siginfo;        /* siginfo data to return to caller */
+       boolean_t caller64 = IS_64BIT_PROCESS(q);
+       int nfound;
+       proc_t p;
+       int error;
+       uthread_t uth;
+       struct _waitid_data *waitid_data;
+
+       if (uap->options == 0 ||
+           (uap->options & ~(WNOHANG|WNOWAIT|WCONTINUED|WSTOPPED|WEXITED)))
+               return (EINVAL);        /* bits set that aren't recognized */
+
+       switch (uap->idtype) {
+       case P_PID:     /* child with process ID equal to... */
+       case P_PGID:    /* child with process group ID equal to... */
+               if (((int)uap->id) < 0)
+                       return (EINVAL);
+               break;
+       case P_ALL:     /* any child */
+               break;
        }
 
        }
 
-       if (p->p_pid != 1 && p->p_pgid != p->p_pid)
-               enterpgrp(p, p->p_pid, 0);
-       p->p_flag |= P_SYSTEM;
+loop:
+       proc_list_lock();
+loop1:
+       nfound = 0;
+
+       PCHILDREN_FOREACH(q, p) {
+               switch (uap->idtype) {
+               case P_PID:     /* child with process ID equal to... */
+                       if (p->p_pid != (pid_t)uap->id)
+                               continue;
+                       break;
+               case P_PGID:    /* child with process group ID equal to... */
+                       if (p->p_pgrpid != (pid_t)uap->id)
+                               continue;
+                       break;
+               case P_ALL:     /* any child */
+                       break;
+               }
 
 
-       /*
-        *      Take us out of the sibling chain, and
-        *      out of our parent's child chain.
-        */
-       LIST_REMOVE(p, p_sibling);
-       p->p_sibling.le_prev = NULL;
-       p->p_sibling.le_next = NULL;
-       p->p_pptr = kernproc;
+               /* XXX This is racy because we don't get the lock!!!! */
+
+               /*
+                * Wait collision; go to sleep and restart; used to maintain
+                * the single return for waited process guarantee.
+                */
+               if (p->p_listflag & P_LIST_WAITING) {
+                       (void) msleep(&p->p_stat, proc_list_mlock,
+                               PWAIT, "waitidcoll", 0);
+                       goto loop1;
+               }
+               p->p_listflag |= P_LIST_WAITING;                /* mark busy */
+
+               nfound++;
+
+               bzero(&siginfo, sizeof (siginfo));
+
+               switch (p->p_stat) {
+               case SZOMB:             /* Exited */
+                       if (!(uap->options & WEXITED))
+                               break;
+                       proc_list_unlock();
+#if CONFIG_MACF
+                       if ((error = mac_proc_check_wait(q, p)) != 0)
+                               goto out;
+#endif
+                       siginfo.si_signo = SIGCHLD;
+                       siginfo.si_pid = p->p_pid;
+                       siginfo.si_status = (WEXITSTATUS(p->p_xstat) & 0x00FFFFFF) | (((uint32_t)(p->p_xhighbits) << 24) & 0xFF000000);
+                       p->p_xhighbits = 0;
+                       if (WIFSIGNALED(p->p_xstat)) {
+                               siginfo.si_code = WCOREDUMP(p->p_xstat) ?
+                                       CLD_DUMPED : CLD_KILLED;
+                       } else
+                               siginfo.si_code = CLD_EXITED;
+
+                       if ((error = copyoutsiginfo(&siginfo,
+                           caller64, uap->infop)) != 0)
+                               goto out;
+
+                       /* Prevent other process for waiting for this event? */
+                       if (!(uap->options & WNOWAIT)) {
+                               (void) reap_child_locked(q, p, 0, 0, 0, 0);
+                               return (0);
+                       }
+                       goto out;
+
+               case SSTOP:             /* Stopped */
+                       /*
+                        * If we are not interested in stopped processes, then
+                        * ignore this one.
+                        */
+                       if (!(uap->options & WSTOPPED))
+                               break;
+
+                       /*
+                        * If someone has already waited it, we lost a race
+                        * to be the one to return status.
+                        */
+                       if ((p->p_lflag & P_LWAITED) != 0)
+                               break;
+                       proc_list_unlock();
+#if CONFIG_MACF
+                       if ((error = mac_proc_check_wait(q, p)) != 0)
+                               goto out;
+#endif
+                       siginfo.si_signo = SIGCHLD;
+                       siginfo.si_pid = p->p_pid;
+                       siginfo.si_status = p->p_xstat; /* signal number */
+                       siginfo.si_code = CLD_STOPPED;
+
+                       if ((error = copyoutsiginfo(&siginfo,
+                           caller64, uap->infop)) != 0)
+                               goto out;
+
+                       /* Prevent other process for waiting for this event? */
+                       if (!(uap->options & WNOWAIT)) {
+                               proc_lock(p);
+                               p->p_lflag |= P_LWAITED;
+                               proc_unlock(p);
+                       }
+                       goto out;
+
+               default:                /* All other states => Continued */
+                       if (!(uap->options & WCONTINUED))
+                               break;
+
+                       /*
+                        * If the flag isn't set, then this process has not
+                        * been stopped and continued, or the status has
+                        * already been reaped by another caller of waitid().
+                        */
+                       if ((p->p_flag & P_CONTINUED) == 0)
+                               break;
+                       proc_list_unlock();
+#if CONFIG_MACF
+                       if ((error = mac_proc_check_wait(q, p)) != 0)
+                               goto out;
+#endif
+                       siginfo.si_signo = SIGCHLD;
+                       siginfo.si_code = CLD_CONTINUED;
+                       proc_lock(p);
+                       siginfo.si_pid = p->p_contproc;
+                       siginfo.si_status = p->p_xstat;
+                       proc_unlock(p);
+
+                       if ((error = copyoutsiginfo(&siginfo,
+                           caller64, uap->infop)) != 0)
+                               goto out;
+
+                       /* Prevent other process for waiting for this event? */
+                       if (!(uap->options & WNOWAIT)) {
+                               OSBitAndAtomic(~((uint32_t)P_CONTINUED),
+                                   &p->p_flag);
+                       }
+                       goto out;
+               }
+               ASSERT_LCK_MTX_OWNED(proc_list_mlock);
+
+               /* Not a process we are interested in; go on to next child */
+
+               p->p_listflag &= ~P_LIST_WAITING;
+               wakeup(&p->p_stat);
+       }
+       ASSERT_LCK_MTX_OWNED(proc_list_mlock);
+
+       /* No child processes that could possibly satisfy the request? */
+
+       if (nfound == 0) {
+               proc_list_unlock();
+               return (ECHILD);
+       }
+
+       if (uap->options & WNOHANG) {
+               proc_list_unlock();
+#if CONFIG_MACF
+               if ((error = mac_proc_check_wait(q, p)) != 0)
+                       return (error);
+#endif
+               /*
+                * The state of the siginfo structure in this case
+                * is undefined.  Some implementations bzero it, some
+                * (like here) leave it untouched for efficiency.
+                *
+                * Thus the most portable check for "no matching pid with
+                * WNOHANG" is to store a zero into si_pid before
+                * invocation, then check for a non-zero value afterwards.
+                */
+               return (0);
+       }
+
+       /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
+       uth = current_uthread();
+       waitid_data = &uth->uu_kevent.uu_waitid_data;
+       waitid_data->args = uap;
+       waitid_data->retval = retval;
+
+       if ((error = msleep0(q, proc_list_mlock,
+           PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)) != 0)
+               return (error);
 
 
-       AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS);
-       return(KERN_SUCCESS);
+       goto loop;
+out:
+       proc_list_lock();
+       p->p_listflag &= ~P_LIST_WAITING;
+       wakeup(&p->p_stat);
+       proc_list_unlock();
+       return (error);
 }
 
 }
 
+/*
+ * make process 'parent' the new parent of process 'child'.
+ */
 void
 void
-process_terminate_self(void)
+proc_reparentlocked(proc_t child, proc_t parent, int cansignal, int locked)
 {
 {
-       struct proc *p = current_proc();
+       proc_t oldparent = PROC_NULL;
 
 
-       if (p != NULL) {
-               exit1(p, W_EXITCODE(0, SIGKILL), (int *)NULL);
-               /*NOTREACHED*/
-       }
+       if (child->p_pptr == parent)
+               return;
+
+       if (locked == 0)
+               proc_list_lock();
+
+       oldparent = child->p_pptr;
+#if __PROC_INTERNAL_DEBUG
+       if (oldparent == PROC_NULL)
+               panic("proc_reparent: process %p does not have a parent\n", child);
+#endif
+
+       LIST_REMOVE(child, p_sibling);
+#if __PROC_INTERNAL_DEBUG
+       if (oldparent->p_childrencnt == 0)
+               panic("process children count already 0\n");
+#endif
+       oldparent->p_childrencnt--;
+#if __PROC_INTERNAL_DEBUG1
+       if (oldparent->p_childrencnt < 0)
+               panic("process children count -ve\n");
+#endif
+       LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
+       parent->p_childrencnt++;        
+       child->p_pptr = parent;
+       child->p_ppid = parent->p_pid;
+
+       proc_list_unlock();
+
+       if ((cansignal != 0) && (initproc == parent) && (child->p_stat == SZOMB))
+               psignal(initproc, SIGCHLD);
+       if (locked == 1)
+               proc_list_lock();
 }
 
 /*
 }
 
 /*
@@ -786,31 +2226,53 @@ process_terminate_self(void)
  * status and rusage for wait().  Check for child processes and orphan them.
  */
 
  * status and rusage for wait().  Check for child processes and orphan them.
  */
 
-int
-vfork_exit(p, rv)
-       struct proc *p;
-       int rv;
+void
+vfork_exit(proc_t p, int rv)
+{
+       vfork_exit_internal(p, rv, 0);
+}
+
+void
+vfork_exit_internal(proc_t p, int rv, int forceexit)
 {
 {
-       register struct proc *q, *nq;
-       thread_act_t self = current_act();
+       thread_t self = current_thread();
+#ifdef FIXME
        struct task *task = p->task;
        struct task *task = p->task;
-       register int i,s;
+#endif
        struct uthread *ut;
        struct uthread *ut;
-       exception_data_t        code[EXCEPTION_CODE_MAX];
 
 
-       ut = get_bsdthread_info(self);
-       if (p->exit_thread) {
-               return(1);
-       } 
-       p->exit_thread = self;
-       
-       s = splsched();
-       p->p_flag |= P_WEXIT;
-       splx(s);
+       /*
+        * If a thread in this task has already
+        * called exit(), then halt any others
+        * right here.
+        */
 
 
-       code[0] = 0xFF000001;                   /* Set terminate code */
-       code[1] = p->p_pid;                             /* Pass out the pid     */
-       (void)sys_perf_notify(p->task, &code, 2);       /* Notify the perf server */
+        ut = get_bsdthread_info(self);
+
+
+       proc_lock(p);
+        if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) {
+               /* 
+               * This happens when a parent exits/killed and vfork is in progress  
+               * other threads. But shutdown code for ex has already called exit1()
+               */
+               proc_unlock(p);
+               return;
+       }
+       p->p_lflag |= (P_LEXIT | P_LPEXIT);
+       proc_unlock(p);
+
+       if (forceexit == 0) {
+               /*
+                * parent of a vfork child has already called exit() and the 
+                * thread that has vfork in proress terminates. So there is no
+                * separate address space here and it has already been marked for
+                * termination. This was never covered before and could cause problems
+                * if we block here for outside code.
+                */
+               /* Notify the perf server */
+               (void)sys_perf_notify(self, p->p_pid);
+       }
 
        /*
         * Remove proc from allproc queue and from pidhash chain.
 
        /*
         * Remove proc from allproc queue and from pidhash chain.
@@ -818,36 +2280,44 @@ vfork_exit(p, rv)
         * Not doing causes things like mount() find this on allproc
         * in partially cleaned state.
         */
         * Not doing causes things like mount() find this on allproc
         * in partially cleaned state.
         */
+
+       proc_list_lock();
+
+#if CONFIG_MEMORYSTATUS
+       memorystatus_remove(p, TRUE);
+#endif
+
        LIST_REMOVE(p, p_list);
        LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
        LIST_REMOVE(p, p_list);
        LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
-       LIST_REMOVE(p, p_hash);
-       /*
-        * If parent is waiting for us to exit or exec,
-        * P_PPWAIT is set; we will wakeup the parent below.
-        */
-       p->p_flag &= ~(P_TRACED | P_PPWAIT);
+       /* will not be visible via proc_find */
+       p->p_listflag |= P_LIST_EXITED;
+
+       proc_list_unlock();
+
+       proc_lock(p);
+       p->p_xstat = rv;
+       p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
        p->p_sigignore = ~0;
        p->p_sigignore = ~0;
-       p->p_siglist = 0;
+       proc_unlock(p);
 
        ut->uu_siglist = 0;
 
        ut->uu_siglist = 0;
-       untimeout(realitexpire, (caddr_t)p->p_pid);
 
 
-       p->p_xstat = rv;
+       /* begin vproc_exit */
 
 
-       vproc_exit(p);
-       return(0);
-}
+       proc_t q;
+       proc_t pp;
 
 
-void 
-vproc_exit(struct proc *p)
-{
-       register struct proc *q, *nq, *pp;
-       struct task *task = p->task;
-       register int i,s;
-       boolean_t funnel_state;
+       vnode_t tvp;
+
+       struct pgrp * pg;
+       struct session *sessp;
+       struct rusage_superset *rup;
 
 
-       MALLOC_ZONE(p->p_ru, struct rusage *,
-                       sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
+       /* XXX Zombie allocation may fail, in which case stats get lost */
+       MALLOC_ZONE(rup, struct rusage_superset *,
+                       sizeof (*rup), M_ZOMBIE, M_WAITOK);
+
+       proc_refdrain(p);
 
        /*
         * Close open files and release open-file table.
 
        /*
         * Close open files and release open-file table.
@@ -855,11 +2325,15 @@ vproc_exit(struct proc *p)
         */
        fdfree(p);
 
         */
        fdfree(p);
 
-       if (SESS_LEADER(p)) {
-               register struct session *sp = p->p_session;
-
-               if (sp->s_ttyvp) {
+       sessp = proc_session(p);
+       if (SESS_LEADER(p, sessp)) {
+               
+               if (sessp->s_ttyvp != NULLVP) {
                        struct vnode *ttyvp;
                        struct vnode *ttyvp;
+                       int ttyvid;
+                       int cttyflag = 0;
+                       struct vfs_context context;
+                       struct tty *tp;
 
                        /*
                         * Controlling process.
 
                        /*
                         * Controlling process.
@@ -867,99 +2341,197 @@ vproc_exit(struct proc *p)
                         * drain controlling terminal
                         * and revoke access to controlling terminal.
                         */
                         * drain controlling terminal
                         * and revoke access to controlling terminal.
                         */
-                       if (sp->s_ttyp->t_session == sp) {
-                               if (sp->s_ttyp->t_pgrp)
-                                       pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
-                               (void) ttywait(sp->s_ttyp);
+                       session_lock(sessp);
+                       tp = SESSION_TP(sessp);
+                       if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
+                               session_unlock(sessp);
+
                                /*
                                /*
-                                * The tty could have been revoked
-                                * if we blocked.
+                                * We're going to SIGHUP the foreground process
+                                * group. It can't change from this point on
+                                * until the revoke is complete.
+                                * The process group changes under both the tty
+                                * lock and proc_list_lock but we need only one
                                 */
                                 */
-                               if (sp->s_ttyvp)
-                                       VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
+                               tty_lock(tp);
+                               ttysetpgrphup(tp);
+                               tty_unlock(tp);
+
+                               tty_pgsignal(tp, SIGHUP, 1);
+
+                               session_lock(sessp);
+                               tp = SESSION_TP(sessp);
+                       }
+                       cttyflag = sessp->s_flags & S_CTTYREF;
+                       sessp->s_flags &= ~S_CTTYREF;
+                       ttyvp = sessp->s_ttyvp;
+                       ttyvid = sessp->s_ttyvid;
+                       sessp->s_ttyvp = NULL;
+                       sessp->s_ttyvid = 0;
+                       sessp->s_ttyp = TTY_NULL;
+                       sessp->s_ttypgrpid = NO_PID;
+                       session_unlock(sessp);
+
+                      if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
+                               if (tp != TTY_NULL) {
+                                       tty_lock(tp);
+                                       (void) ttywait(tp);
+                                       tty_unlock(tp);
+                               }
+                               context.vc_thread = proc_thread(p); /* XXX */
+                               context.vc_ucred = kauth_cred_proc_ref(p);
+                               VNOP_REVOKE(ttyvp, REVOKEALL, &context);
+                               if (cttyflag) {
+                                       /*
+                                        * Release the extra usecount taken in cttyopen.
+                                        * usecount should be released after VNOP_REVOKE is called.
+                                        * This usecount was taken to ensure that
+                                        * the VNOP_REVOKE results in a close to
+                                        * the tty since cttyclose is a no-op.
+                                        */
+                                       vnode_rele(ttyvp);
+                               }
+                               vnode_put(ttyvp);
+                               kauth_cred_unref(&context.vc_ucred);
+                               ttyvp = NULLVP;
+                       }
+                       if (tp) {
+                               /*
+                                * This is cleared even if not set. This is also done in
+                                * spec_close to ensure that the flag is cleared.
+                                */
+                               tty_lock(tp);
+                               ttyclrpgrphup(tp);
+                               tty_unlock(tp);
+
+                               ttyfree(tp);
                        }
                        }
-                       ttyvp = sp->s_ttyvp;
-                       sp->s_ttyvp = NULL;
-                       if (ttyvp)
-                               vrele(ttyvp);
-                       /*
-                        * s_ttyp is not zero'd; we use this to indicate
-                        * that the session once had a controlling terminal.
-                        * (for logging and informational purposes)
-                        */
                }
                }
-               sp->s_leader = NULL;
+               session_lock(sessp);
+               sessp->s_leader = NULL;
+               session_unlock(sessp);
        }
        }
+       session_rele(sessp);
+
+       pg = proc_pgrp(p);
+       fixjobc(p, pg, 0);
+       pg_rele(pg);
 
 
-       fixjobc(p, p->p_pgrp, 0);
        p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
 
        p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
 
-#if KTRACE
-       /* 
-        * release trace file
-        */
-       p->p_traceflag = 0;     /* don't trace the vrele() */
-       if (p->p_tracep) {
-               struct vnode *tvp = p->p_tracep;
-               p->p_tracep = NULL;
+       proc_list_lock();
+       proc_childdrainstart(p);
+       while ((q = p->p_children.lh_first) != NULL) {
+               if (q->p_stat == SZOMB) {
+                       if (p != q->p_pptr)
+                               panic("parent child linkage broken");
+                       /* check for lookups by zomb sysctl */
+                       while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
+                               msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
+                       }
+                       q->p_listflag |= P_LIST_WAITING;
+                       /*
+                        * This is a named reference and it is not granted
+                        * if the reap is already in progress. So we get
+                        * the reference here exclusively and their can be
+                        * no waiters. So there is no need for a wakeup
+                        * after we are done. AlsO  the reap frees the structure
+                        * and the proc struct cannot be used for wakeups as well. 
+                        * It is safe to use q here as this is system reap
+                        */
+                       (void)reap_child_locked(p, q, 1, 0, 1, 0);
+               } else {
+                       /*
+                       * Traced processes are killed
+                       * since their existence means someone is messing up.
+                       */
+                       if (q->p_lflag & P_LTRACED) {
+                               struct proc *opp;
+
+                               proc_list_unlock();
+
+                               opp = proc_find(q->p_oppid);
+                               if (opp != PROC_NULL) {
+                                       proc_list_lock();
+                                       q->p_oppid = 0;
+                                       proc_list_unlock();
+                                       proc_reparentlocked(q, opp, 0, 0);
+                                       proc_rele(opp);
+                               } else {
+                                       /* original parent exited while traced */
+                                       proc_list_lock();
+                                       q->p_listflag |= P_LIST_DEADPARENT;
+                                       q->p_oppid = 0;
+                                       proc_list_unlock();
+                                       proc_reparentlocked(q, initproc, 0, 0);
+                               }
 
 
-               if (UBCINFOEXISTS(tvp))
-                       ubc_rele(tvp);
-               vrele(tvp);
-       }
-#endif
+                               proc_lock(q);
+                               q->p_lflag &= ~P_LTRACED;
+
+                               if (q->sigwait_thread) {
+                                       thread_t thread = q->sigwait_thread;
+
+                                       proc_unlock(q);
+                                       /*
+                                       * The sigwait_thread could be stopped at a
+                                       * breakpoint. Wake it up to kill.
+                                       * Need to do this as it could be a thread which is not
+                                       * the first thread in the task. So any attempts to kill
+                                       * the process would result into a deadlock on q->sigwait.
+                                       */
+                                       thread_resume(thread);
+                                       clear_wait(thread, THREAD_INTERRUPTED);
+                                       threadsignal(thread, SIGKILL, 0, TRUE);
+                               } else {
+                                       proc_unlock(q);
+                               }
 
 
-       q = p->p_children.lh_first;
-       if (q)          /* only need this if any child is S_ZOMB */
-               wakeup((caddr_t) initproc);
-       for (; q != 0; q = nq) {
-               nq = q->p_sibling.le_next;
-               proc_reparent(q, initproc);
-               /*
-                * Traced processes are killed
-                * since their existence means someone is messing up.
-                */
-               if (q->p_flag & P_TRACED) {
-                       q->p_flag &= ~P_TRACED;
-                       if (q->sigwait_thread) {
-                               /*
-                                * The sigwait_thread could be stopped at a
-                                * breakpoint. Wake it up to kill.
-                                * Need to do this as it could be a thread which is not
-                                * the first thread in the task. So any attempts to kill
-                                * the process would result into a deadlock on q->sigwait.
-                                */
-                               thread_resume((thread_act_t)q->sigwait_thread);
-                               clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
-                               threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
+                               psignal(q, SIGKILL);
+                               proc_list_lock();
+                       } else {
+                               q->p_listflag |= P_LIST_DEADPARENT;
+                               proc_reparentlocked(q, initproc, 0, 1);
                        }
                        }
-                       psignal(q, SIGKILL);
                }
        }
 
                }
        }
 
+       proc_childdrainend(p);
+       proc_list_unlock();
+
        /*
        /*
-        * Save exit status and final rusage info, adding in child rusage
-        * info and self times.
+        * Release reference to text vnode
         */
         */
-       *p->p_ru = p->p_stats->p_ru;
+       tvp = p->p_textvp;
+       p->p_textvp = NULL;
+       if (tvp != NULLVP) {
+               vnode_rele(tvp);
+       }
 
 
-       timerclear(&p->p_ru->ru_utime);
-       timerclear(&p->p_ru->ru_stime);
+       /*
+        * Save exit status and final rusage info, adding in child rusage
+        * info and self times.  If we were unable to allocate a zombie
+        * structure, this information is lost.
+        */
+       if (rup != NULL) {
+           rup->ru = p->p_stats->p_ru;
+           timerclear(&rup->ru.ru_utime);
+           timerclear(&rup->ru.ru_stime);
 
 #ifdef  FIXME
 
 #ifdef  FIXME
-       if (task) {
-               task_basic_info_data_t tinfo;
+           if (task) {
+               mach_task_basic_info_data_t tinfo;
                task_thread_times_info_data_t ttimesinfo;
                int task_info_stuff, task_ttimes_stuff;
                struct timeval ut,st;
 
                task_thread_times_info_data_t ttimesinfo;
                int task_info_stuff, task_ttimes_stuff;
                struct timeval ut,st;
 
-               task_info_stuff = TASK_BASIC_INFO_COUNT;
-               task_info(task, TASK_BASIC_INFO,
+               task_info_stuff = MACH_TASK_BASIC_INFO_COUNT;
+               task_info(task, MACH_TASK_BASIC_INFO,
                          &tinfo, &task_info_stuff);
                          &tinfo, &task_info_stuff);
-               p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
-               p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
-               p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
-               p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
+               p->p_ru->ru.ru_utime.tv_sec = tinfo.user_time.seconds;
+               p->p_ru->ru.ru_utime.tv_usec = tinfo.user_time.microseconds;
+               p->p_ru->ru.ru_stime.tv_sec = tinfo.system_time.seconds;
+               p->p_ru->ru.ru_stime.tv_usec = tinfo.system_time.microseconds;
 
                task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
                task_info(task, TASK_THREAD_TIMES_INFO,
 
                task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
                task_info(task, TASK_THREAD_TIMES_INFO,
@@ -969,12 +2541,23 @@ vproc_exit(struct proc *p)
                ut.tv_usec = ttimesinfo.user_time.microseconds;
                st.tv_sec = ttimesinfo.system_time.seconds;
                st.tv_usec = ttimesinfo.system_time.microseconds;
                ut.tv_usec = ttimesinfo.user_time.microseconds;
                st.tv_sec = ttimesinfo.system_time.seconds;
                st.tv_usec = ttimesinfo.system_time.microseconds;
-               timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
-               timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
-       }
+               timeradd(&ut,&p->p_ru->ru.ru_utime,&p->p_ru->ru.ru_utime);
+                       timeradd(&st,&p->p_ru->ru.ru_stime,&p->p_ru->ru.ru_stime);
+           }
 #endif /* FIXME */
 
 #endif /* FIXME */
 
-       ruadd(p->p_ru, &p->p_stats->p_cru);
+           ruadd(&rup->ru, &p->p_stats->p_cru);
+
+               gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
+               rup->ri.ri_phys_footprint = 0;
+               rup->ri.ri_proc_exit_abstime = mach_absolute_time();
+
+               /*
+                * Now that we have filled in the rusage info, make it
+                * visible to an external observer via proc_pid_rusage().
+                */
+               p->p_ru = rup;
+       }
 
        /*
         * Free up profiling buffers.
 
        /*
         * Free up profiling buffers.
@@ -988,21 +2571,26 @@ vproc_exit(struct proc *p)
 
                for (; p1 != NULL; p1 = pn) {
                        pn = p1->pr_next;
 
                for (; p1 != NULL; p1 = pn) {
                        pn = p1->pr_next;
-                       kfree((vm_offset_t)p1, sizeof *p1);
+                       kfree(p1, sizeof *p1);
                }
        }
 
                }
        }
 
+#if PSYNCH
+       pth_proc_hashdelete(p);
+#endif /* PSYNCH */
+
+       proc_free_realitimer(p);
+
        /*
         * Other substructures are freed from wait().
         */
        /*
         * Other substructures are freed from wait().
         */
-       FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
+       FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
        p->p_stats = NULL;
 
        p->p_stats = NULL;
 
-       FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
+       FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
        p->p_sigacts = NULL;
 
        p->p_sigacts = NULL;
 
-       if (--p->p_limit->p_refcnt == 0)
-               FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
+       proc_limitdrop(p, 1);
        p->p_limit = NULL;
 
        /*
        p->p_limit = NULL;
 
        /*
@@ -1015,18 +2603,121 @@ vproc_exit(struct proc *p)
        /*
         * Notify parent that we're gone.
         */
        /*
         * Notify parent that we're gone.
         */
-       pp = p->p_pptr;
-       if (pp != initproc) {
-               pp->si_pid = p->p_pid;
-               pp->si_status = p->p_xstat;
-               pp->si_code = CLD_EXITED;
-               pp->si_uid = p->p_cred->p_ruid;
+       pp = proc_parent(p);
+       if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
+               if (pp != initproc) {
+                       proc_lock(pp);
+                       pp->si_pid = p->p_pid;
+                       pp->p_xhighbits = p->p_xhighbits;
+                       p->p_xhighbits = 0;
+                       pp->si_status = p->p_xstat;
+                       pp->si_code = CLD_EXITED;
+                       /*
+                        * p_ucred usage is safe as it is an exiting process
+                        * and reference is dropped in reap
+                        */
+                       pp->si_uid = kauth_cred_getruid(p->p_ucred);
+                       proc_unlock(pp);
+               }
+               /* mark as a zombie */
+               /* mark as a zombie */
+               /* No need to take proc lock as all refs are drained and
+                * no one except parent (reaping ) can look at this.
+                * The write is to an int and is coherent. Also parent is
+                *  keyed off of list lock for reaping
+                */
+               p->p_stat = SZOMB;
+
+               psignal(pp, SIGCHLD);
+
+               /* and now wakeup the parent */
+               proc_list_lock();
+               wakeup((caddr_t)pp);
+               proc_list_unlock();
+       } else {
+               proc_list_lock();
+               /* check for lookups by zomb sysctl */
+               while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
+                       msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
+               }
+               p->p_stat = SZOMB;
+               p->p_listflag |= P_LIST_WAITING;
+
+               /*
+                * This is a named reference and it is not granted
+                * if the reap is already in progress. So we get
+                * the reference here exclusively and their can be
+                * no waiters. So there is no need for a wakeup
+                * after we are done. AlsO  the reap frees the structure
+                * and the proc struct cannot be used for wakeups as well. 
+                * It is safe to use p here as this is system reap
+                */
+               (void)reap_child_locked(pp, p, 0, 0, 1, 1);
+               /* list lock dropped by reap_child_locked */
        }
        }
-       psignal(p->p_pptr, SIGCHLD);
+       proc_rele(pp);
+}
 
 
-       /* mark as a zombie */
-       p->p_stat = SZOMB;
+                                               
+/*
+ * munge_rusage
+ *     LP64 support - long is 64 bits if we are dealing with a 64 bit user
+ *     process.  We munge the kernel version of rusage into the
+ *     64 bit version.
+ */
+__private_extern__  void 
+munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p)
+{
+       /* timeval changes size, so utime and stime need special handling */
+       a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
+       a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
+       a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
+       a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
+       /*
+        * everything else can be a direct assign, since there is no loss
+        * of precision implied boing 32->64.
+        */
+       a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
+       a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
+       a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
+       a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
+       a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
+       a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
+       a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
+       a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
+       a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
+       a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
+       a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
+       a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
+       a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
+       a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
+}
 
 
-       /* and now wakeup the parent */
-       wakeup((caddr_t)p->p_pptr);
+/* For a 64-bit kernel and 32-bit userspace, munging may be needed */
+__private_extern__  void 
+munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p)
+{
+       /* timeval changes size, so utime and stime need special handling */
+       a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
+       a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
+       a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
+       a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
+       /*
+        * everything else can be a direct assign. We currently ignore
+        * the loss of precision
+        */
+       a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
+       a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
+       a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
+       a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
+       a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
+       a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
+       a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
+       a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
+       a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
+       a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
+       a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
+       a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
+       a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
+       a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
 }
 }