]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/sys_generic.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / bsd / kern / sys_generic.c
index bb215dfdd78a36842c5f1391c4a974203d2efdca..8692d514df1325bc49e51759356f657124d860fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  *
  *     @(#)sys_generic.c       8.9 (Berkeley) 2/14/95
  */
+/*
+ * NOTICE: This file was modified by SPARTA, Inc. in 2006 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 <sys/param.h>
 #include <sys/systm.h>
 #include <sys/file_internal.h>
 #include <sys/proc_internal.h>
 #include <sys/socketvar.h>
-#if KTRACE
 #include <sys/uio_internal.h>
-#else
-#include <sys/uio.h>
-#endif
 #include <sys/kernel.h>
+#include <sys/guarded.h>
 #include <sys/stat.h>
 #include <sys/malloc.h>
 #include <sys/sysproto.h>
@@ -91,6 +94,8 @@
 #include <sys/poll.h>
 #include <sys/event.h>
 #include <sys/eventvar.h>
+#include <sys/proc.h>
+#include <sys/kauth.h>
 
 #include <mach/mach_types.h>
 #include <kern/kern_types.h>
 #include <kern/kalloc.h>
 #include <kern/thread.h>
 #include <kern/clock.h>
+#include <kern/ledger.h>
+#include <kern/task.h>
+#include <kern/telemetry.h>
+#include <kern/waitq.h>
+#include <kern/sched_prim.h>
 
 #include <sys/mbuf.h>
+#include <sys/domain.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #include <sys/errno.h>
 #include <sys/syscall.h>
 #include <sys/pipe.h>
 
-#include <bsm/audit_kernel.h>
+#include <security/audit/audit.h>
 
 #include <net/if.h>
 #include <net/route.h>
 #include <netinet/tcpip.h>
 #include <netinet/tcp_debug.h>
 /* for wait queue based select */
-#include <kern/wait_queue.h>
+#include <kern/waitq.h>
 #include <kern/kalloc.h>
-#if KTRACE 
-#include <sys/ktrace.h>
-#endif
 #include <sys/vnode_internal.h>
 
+/* XXX should be in a header file somewhere */
+void evsofree(struct socket *);
+void evpipefree(struct pipe *);
+void postpipeevent(struct pipe *, int);
+void postevent(struct socket *, struct sockbuf *, int);
+extern kern_return_t IOBSDGetPlatformUUID(__darwin_uuid_t uuid, mach_timespec_t timeoutp);
+
 int rd_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
-int wr_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
-extern void    *get_bsduthreadarg(thread_t);
-extern int     *get_bsduthreadrval(thread_t);
+int wr_uio(struct proc *p, struct fileproc *fp, uio_t uio, user_ssize_t *retval);
 
-__private_extern__ int dofileread(struct proc *p, struct fileproc *fp, int fd, 
+__private_extern__ int dofileread(vfs_context_t ctx, struct fileproc *fp,
                                                                   user_addr_t bufp, user_size_t nbyte, 
                                                                   off_t offset, int flags, user_ssize_t *retval);
-__private_extern__ int dofilewrite(struct proc *p, struct fileproc *fp, int fd, 
+__private_extern__ int dofilewrite(vfs_context_t ctx, struct fileproc *fp,
                                                                        user_addr_t bufp, user_size_t nbyte, 
                                                                        off_t offset, int flags, user_ssize_t *retval);
 __private_extern__ int preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_vnode);
 __private_extern__ void        donefileread(struct proc *p, struct fileproc *fp_ret, int fd);
 
-#if NETAT
-extern int appletalk_inited;
-#endif /* NETAT */
+
+/* Conflict wait queue for when selects collide (opaque type) */
+struct waitq select_conflict_queue;
+
+/*
+ * Init routine called from bsd_init.c
+ */
+void select_waitq_init(void);
+void
+select_waitq_init(void)
+{
+       waitq_init(&select_conflict_queue, SYNC_POLICY_FIFO);
+}
 
 #define f_flag f_fglob->fg_flag
-#define f_type f_fglob->fg_type
+#define f_type f_fglob->fg_ops->fo_type
 #define f_msgcount f_fglob->fg_msgcount
 #define f_cred f_fglob->fg_cred
 #define f_ops f_fglob->fg_ops
 #define f_offset f_fglob->fg_offset
 #define f_data f_fglob->fg_data
+
 /*
  * Read system call.
+ *
+ * Returns:    0                       Success
+ *     preparefileread:EBADF
+ *     preparefileread:ESPIPE
+ *     preparefileread:ENXIO
+ *     preparefileread:EBADF
+ *     dofileread:???
  */
 int
-read(p, uap, retval)
-       struct proc *p;
-       register struct read_args *uap;
-       user_ssize_t *retval;
+read(struct proc *p, struct read_args *uap, user_ssize_t *retval)
+{
+       __pthread_testcancel(1);
+       return(read_nocancel(p, (struct read_nocancel_args *)uap, retval));
+}
+
+int
+read_nocancel(struct proc *p, struct read_nocancel_args *uap, user_ssize_t *retval)
 {
        struct fileproc *fp;
        int error;
        int fd = uap->fd;
+       struct vfs_context context;
 
        if ( (error = preparefileread(p, &fp, fd, 0)) )
                return (error);
 
-       error = dofileread(p, fp, uap->fd, uap->cbuf, uap->nbyte,
+       context = *(vfs_context_current());
+       context.vc_ucred = fp->f_fglob->fg_cred;
+
+       error = dofileread(&context, fp, uap->cbuf, uap->nbyte,
                           (off_t)-1, 0, retval);
 
        donefileread(p, fp, fd);
@@ -183,29 +222,44 @@ read(p, uap, retval)
 
 /* 
  * Pread system call
+ *
+ * Returns:    0                       Success
+ *     preparefileread:EBADF
+ *     preparefileread:ESPIPE
+ *     preparefileread:ENXIO
+ *     preparefileread:EBADF
+ *     dofileread:???
  */
 int
-pread(p, uap, retval)
-       struct proc *p;
-       register struct pread_args *uap;
-       user_ssize_t *retval;
+pread(struct proc *p, struct pread_args *uap, user_ssize_t *retval)
 {
-       struct fileproc *fp;
+       __pthread_testcancel(1);
+       return(pread_nocancel(p, (struct pread_nocancel_args *)uap, retval));
+}
+
+int
+pread_nocancel(struct proc *p, struct pread_nocancel_args *uap, user_ssize_t *retval)
+{
+       struct fileproc *fp = NULL;     /* fp set by preparefileread() */
        int fd = uap->fd;
        int error;
+       struct vfs_context context;
 
        if ( (error = preparefileread(p, &fp, fd, 1)) )
-               return (error);
+               goto out;
+
+       context = *(vfs_context_current());
+       context.vc_ucred = fp->f_fglob->fg_cred;
 
-       error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
+       error = dofileread(&context, fp, uap->buf, uap->nbyte,
                        uap->offset, FOF_OFFSET, retval);
        
        donefileread(p, fp, fd);
 
-       if (!error)
-           KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pread) | DBG_FUNC_NONE),
+       KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pread) | DBG_FUNC_NONE),
              uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
-       
+
+out:
        return (error);
 }
 
@@ -216,14 +270,19 @@ pread(p, uap, retval)
 void
 donefileread(struct proc *p, struct fileproc *fp, int fd)
 {
-       proc_fdlock(p);
-
-       fp->f_flags &= ~FP_INCHRREAD;
-
+       proc_fdlock_spin(p);
        fp_drop(p, fd, fp, 1);
         proc_fdunlock(p);
 }
 
+/*
+ * Returns:    0                       Success
+ *             EBADF
+ *             ESPIPE
+ *             ENXIO
+ *     fp_lookup:EBADF
+ *     fo_read:???
+ */
 int
 preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_pread)
 {
@@ -231,7 +290,9 @@ preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_
        int     error;
        struct fileproc *fp;
 
-       proc_fdlock(p);
+       AUDIT_ARG(fd, fd);
+
+       proc_fdlock_spin(p);
 
        error = fp_lookup(p, fd, &fp, 1);
 
@@ -250,8 +311,14 @@ preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_
        if (fp->f_type == DTYPE_VNODE) {
                vp = (struct vnode *)fp->f_fglob->fg_data;
 
-               if (vp->v_type == VCHR)
-                       fp->f_flags |= FP_INCHRREAD;
+               if (check_for_pread && (vnode_isfifo(vp))) {
+                       error = ESPIPE;
+                       goto out;
+               } 
+               if (check_for_pread && (vp->v_flag & VISTTY)) {
+                       error = ENXIO;
+                       goto out;
+               }
        }
 
        *fp_ret = fp;
@@ -266,31 +333,25 @@ out:
 }
 
 
+/*
+ * Returns:    0                       Success
+ *             EINVAL
+ *     fo_read:???
+ */
 __private_extern__ int
-dofileread(p, fp, fd, bufp, nbyte, offset, flags, retval)
-       struct proc *p;
-       struct fileproc *fp;
-       int fd, flags;
-       user_addr_t bufp;
-       user_size_t nbyte;
-       off_t offset;
-       user_ssize_t *retval;
+dofileread(vfs_context_t ctx, struct fileproc *fp,
+          user_addr_t bufp, user_size_t nbyte, off_t offset, int flags,
+          user_ssize_t *retval)
 {
        uio_t auio;
        user_ssize_t bytecnt;
        long error = 0;
        char uio_buf[ UIO_SIZEOF(1) ];
-#if KTRACE
-       uio_t ktruio = NULL;
-       char ktr_uio_buf[ UIO_SIZEOF(1) ];
-       int didktr = 0;
-#endif
 
-       // LP64todo - do we want to raise this?
        if (nbyte > INT_MAX)
                return (EINVAL);
 
-       if (IS_64BIT_PROCESS(p)) {
+       if (IS_64BIT_PROCESS(vfs_context_proc(ctx))) {
                auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ, 
                                                                          &uio_buf[0], sizeof(uio_buf));
        } else {
@@ -299,37 +360,14 @@ dofileread(p, fp, fd, bufp, nbyte, offset, flags, retval)
        }
        uio_addiov(auio, bufp, nbyte);
 
-#if KTRACE
-       /*
-       * if tracing, save a copy of iovec
-       */
-       if (KTRPOINT(p, KTR_GENIO)) {
-               didktr = 1;
-
-               if (IS_64BIT_PROCESS(p)) {
-                       ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ, 
-                                                                         &ktr_uio_buf[0], sizeof(ktr_uio_buf));
-               } else {
-                       ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_READ, 
-                                                                         &ktr_uio_buf[0], sizeof(ktr_uio_buf));
-               }
-               uio_addiov(ktruio, bufp, nbyte);
-       }
-#endif
        bytecnt = nbyte;
 
-       if ((error = fo_read(fp, auio, fp->f_cred, flags, p))) {
+       if ((error = fo_read(fp, auio, flags, ctx))) {
                if (uio_resid(auio) != bytecnt && (error == ERESTART ||
                        error == EINTR || error == EWOULDBLOCK))
                        error = 0;
        }
        bytecnt -= uio_resid(auio);
-#if KTRACE
-       if (didktr && error == 0) {
-               uio_setresid(ktruio, bytecnt);
-               ktrgenio(p->p_tracep, fd, UIO_READ, ktruio, error);
-       }
-#endif  
 
        *retval = bytecnt;
 
@@ -338,16 +376,25 @@ dofileread(p, fp, fd, bufp, nbyte, offset, flags, retval)
 
 /*      
  * Scatter read system call.
+ *
+ * Returns:    0                       Success
+ *             EINVAL
+ *             ENOMEM
+ *     copyin:EFAULT
+ *     rd_uio:???
  */
 int
-readv(p, uap, retval)
-       struct proc *p;
-       register struct readv_args *uap;
-       user_ssize_t *retval;
+readv(struct proc *p, struct readv_args *uap, user_ssize_t *retval)
+{
+       __pthread_testcancel(1);
+       return(readv_nocancel(p, (struct readv_nocancel_args *)uap, retval));
+}
+
+int
+readv_nocancel(struct proc *p, struct readv_nocancel_args *uap, user_ssize_t *retval)
 {
        uio_t auio = NULL;
        int error;
-       int size_of_iovec;
        struct user_iovec *iovp;
 
        /* Verify range bedfore calling uio_create() */
@@ -367,15 +414,19 @@ readv(p, uap, retval)
                error = ENOMEM;
                goto ExitThisRoutine;
        }
-       size_of_iovec = (IS_64BIT_PROCESS(p) ? sizeof(struct user_iovec) : sizeof(struct iovec));
-       error = copyin(uap->iovp, (caddr_t)iovp, (uap->iovcnt * size_of_iovec));
+       error = copyin_user_iovec_array(uap->iovp,
+               IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32,
+               uap->iovcnt, iovp);
        if (error) {
                goto ExitThisRoutine;
        }
        
        /* finalize uio_t for use and do the IO 
         */
-       uio_calculateresid(auio);
+       error = uio_calculateresid(auio);
+       if (error) {
+               goto ExitThisRoutine;
+       }
        error = rd_uio(p, uap->fd, auio, retval);
 
 ExitThisRoutine:
@@ -387,27 +438,49 @@ ExitThisRoutine:
 
 /*
  * Write system call
+ *
+ * Returns:    0                       Success
+ *             EBADF
+ *     fp_lookup:EBADF
+ *     dofilewrite:???
  */
 int
-write(p, uap, retval)
-       struct proc *p;
-       register struct write_args *uap;
-       user_ssize_t *retval;   
+write(struct proc *p, struct write_args *uap, user_ssize_t *retval)
+{
+       __pthread_testcancel(1);
+       return(write_nocancel(p, (struct write_nocancel_args *)uap, retval));
+
+}
+
+int
+write_nocancel(struct proc *p, struct write_nocancel_args *uap, user_ssize_t *retval)
 {
        struct fileproc *fp;
        int error;      
        int fd = uap->fd;
+       bool wrote_some = false;
+
+       AUDIT_ARG(fd, fd);
 
        error = fp_lookup(p,fd,&fp,0);
        if (error)
                return(error);
        if ((fp->f_flag & FWRITE) == 0) {
                error = EBADF;
+       } else if (FP_ISGUARDED(fp, GUARD_WRITE)) {
+               proc_fdlock(p);
+               error = fp_guard_exception(p, fd, fp, kGUARD_EXC_WRITE);
+               proc_fdunlock(p);
        } else {
-               error = dofilewrite(p, fp, uap->fd, uap->cbuf, uap->nbyte,
+               struct vfs_context context = *(vfs_context_current());
+               context.vc_ucred = fp->f_fglob->fg_cred;
+
+               error = dofilewrite(&context, fp, uap->cbuf, uap->nbyte,
                        (off_t)-1, 0, retval);
+
+               wrote_some = *retval > 0;
        }
-       if (error == 0)
+       if (wrote_some)
                fp_drop_written(p, fd, fp);
        else
                fp_drop(p, fd, fp, 0);
@@ -416,16 +489,32 @@ write(p, uap, retval)
 
 /*                          
  * pwrite system call
+ *
+ * Returns:    0                       Success
+ *             EBADF
+ *             ESPIPE
+ *             ENXIO
+ *             EINVAL
+ *     fp_lookup:EBADF
+ *     dofilewrite:???
  */
 int
-pwrite(p, uap, retval)
-       struct proc *p;
-       register struct pwrite_args *uap;
-       user_ssize_t *retval;   
+pwrite(struct proc *p, struct pwrite_args *uap, user_ssize_t *retval)
+{
+       __pthread_testcancel(1);
+       return(pwrite_nocancel(p, (struct pwrite_nocancel_args *)uap, retval));
+}
+
+int
+pwrite_nocancel(struct proc *p, struct pwrite_nocancel_args *uap, user_ssize_t *retval)
 {
         struct fileproc *fp;
         int error; 
        int fd = uap->fd;
+       vnode_t vp  = (vnode_t)0;
+       bool wrote_some = false;
+
+       AUDIT_ARG(fd, fd);
 
        error = fp_lookup(p,fd,&fp,0);
        if (error)
@@ -433,51 +522,70 @@ pwrite(p, uap, retval)
 
        if ((fp->f_flag & FWRITE) == 0) {
                error = EBADF;
+       } else if (FP_ISGUARDED(fp, GUARD_WRITE)) {
+               proc_fdlock(p);
+               error = fp_guard_exception(p, fd, fp, kGUARD_EXC_WRITE);
+               proc_fdunlock(p);
        } else {
+               struct vfs_context context = *vfs_context_current();
+               context.vc_ucred = fp->f_fglob->fg_cred;
+
                if (fp->f_type != DTYPE_VNODE) {
                        error = ESPIPE;
-               } else {
-                   error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
-                       uap->offset, FOF_OFFSET, retval);
+                       goto errout;
+               }
+               vp = (vnode_t)fp->f_fglob->fg_data;
+               if (vnode_isfifo(vp)) {
+                       error = ESPIPE;
+                       goto errout;
+               } 
+               if ((vp->v_flag & VISTTY)) {
+                       error = ENXIO;
+                       goto errout;
+               }
+               if (uap->offset == (off_t)-1) {
+                       error = EINVAL;
+                       goto errout;
                }
+
+                   error = dofilewrite(&context, fp, uap->buf, uap->nbyte,
+                       uap->offset, FOF_OFFSET, retval);
+                       wrote_some = *retval > 0;
         }
-       if (error == 0)
+errout:
+       if (wrote_some)
                fp_drop_written(p, fd, fp);
        else
                fp_drop(p, fd, fp, 0);
 
-       if (!error)
-           KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pwrite) | DBG_FUNC_NONE),
+       KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pwrite) | DBG_FUNC_NONE),
              uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
        
         return(error);
 }
 
+/*
+ * Returns:    0                       Success
+ *             EINVAL
+ *     <fo_write>:EPIPE
+ *     <fo_write>:???                  [indirect through struct fileops]
+ */
 __private_extern__ int                  
-dofilewrite(p, fp, fd, bufp, nbyte, offset, flags, retval)
-       struct proc *p;
-       struct fileproc *fp; 
-       int fd, flags;
-       user_addr_t bufp;
-       user_size_t nbyte;   
-       off_t offset; 
-       user_ssize_t *retval;
+dofilewrite(vfs_context_t ctx, struct fileproc *fp,
+           user_addr_t bufp, user_size_t nbyte, off_t offset, int flags,
+           user_ssize_t *retval)
 {       
        uio_t auio;
        long error = 0;
        user_ssize_t bytecnt;
        char uio_buf[ UIO_SIZEOF(1) ];
-#if KTRACE
-       uio_t ktruio;
-       int didktr = 0; 
-       char ktr_uio_buf[ UIO_SIZEOF(1) ];
-#endif
 
-       // LP64todo - do we want to raise this?
-       if (nbyte > INT_MAX)   
+       if (nbyte > INT_MAX) {
+               *retval = 0;
                return (EINVAL);
+       }
 
-       if (IS_64BIT_PROCESS(p)) {
+       if (IS_64BIT_PROCESS(vfs_context_proc(ctx))) {
                auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE, 
                                                                          &uio_buf[0], sizeof(uio_buf));
        } else {
@@ -486,39 +594,19 @@ dofilewrite(p, fp, fd, bufp, nbyte, offset, flags, retval)
        }
        uio_addiov(auio, bufp, nbyte);
 
-#if KTRACE
-       /*
-       * if tracing, save a copy of iovec and uio
-       */
-       if (KTRPOINT(p, KTR_GENIO)) {
-               didktr = 1;
-
-               if (IS_64BIT_PROCESS(p)) {
-                       ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE, 
-                                                                                 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
-               } else {
-                       ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_WRITE, 
-                                                                                 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
-               }
-               uio_addiov(ktruio, bufp, nbyte);
-       }
-#endif  
        bytecnt = nbyte; 
-       if ((error = fo_write(fp, auio, fp->f_cred, flags, p))) {
+       if ((error = fo_write(fp, auio, flags, ctx))) {
                if (uio_resid(auio) != bytecnt && (error == ERESTART ||
                        error == EINTR || error == EWOULDBLOCK))
                        error = 0;
                /* The socket layer handles SIGPIPE */
-               if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
-                       psignal(p, SIGPIPE);
+               if (error == EPIPE && fp->f_type != DTYPE_SOCKET &&
+                   (fp->f_fglob->fg_lflags & FG_NOSIGPIPE) == 0) {
+                       /* XXX Raise the signal on the thread? */
+                       psignal(vfs_context_proc(ctx), SIGPIPE);
+               }
        }
        bytecnt -= uio_resid(auio);
-#if KTRACE 
-       if (didktr && error == 0) {
-               uio_setresid(ktruio, bytecnt);
-               ktrgenio(p->p_tracep, fd, UIO_WRITE, ktruio, error);
-       }
-#endif  
        *retval = bytecnt;
 
        return (error); 
@@ -528,15 +616,22 @@ dofilewrite(p, fp, fd, bufp, nbyte, offset, flags, retval)
  * Gather write system call  
  */     
 int
-writev(p, uap, retval)
-       struct proc *p;
-       register struct writev_args *uap;
-       user_ssize_t *retval;
+writev(struct proc *p, struct writev_args *uap, user_ssize_t *retval)
+{
+       __pthread_testcancel(1);
+       return(writev_nocancel(p, (struct writev_nocancel_args *)uap, retval));
+}
+
+int
+writev_nocancel(struct proc *p, struct writev_nocancel_args *uap, user_ssize_t *retval)
 {
        uio_t auio = NULL;
        int error;
-       int size_of_iovec;
+       struct fileproc *fp;
        struct user_iovec *iovp;
+       bool wrote_some = false;
+
+       AUDIT_ARG(fd, uap->fd);
 
        /* Verify range bedfore calling uio_create() */
        if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
@@ -555,16 +650,39 @@ writev(p, uap, retval)
                error = ENOMEM;
                goto ExitThisRoutine;
        }
-       size_of_iovec = (IS_64BIT_PROCESS(p) ? sizeof(struct user_iovec) : sizeof(struct iovec));
-       error = copyin(uap->iovp, (caddr_t)iovp, (uap->iovcnt * size_of_iovec));
+       error = copyin_user_iovec_array(uap->iovp,
+               IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32,
+               uap->iovcnt, iovp);
        if (error) {
                goto ExitThisRoutine;
        }
        
        /* finalize uio_t for use and do the IO 
         */
-       uio_calculateresid(auio);
-       error = wr_uio(p, uap->fd, auio, retval);
+       error = uio_calculateresid(auio);
+       if (error) {
+               goto ExitThisRoutine;
+       }
+
+       error = fp_lookup(p, uap->fd, &fp, 0);
+       if (error)
+               goto ExitThisRoutine;
+       
+       if ((fp->f_flag & FWRITE) == 0) {
+               error = EBADF;
+       } else if (FP_ISGUARDED(fp, GUARD_WRITE)) {
+               proc_fdlock(p);
+               error = fp_guard_exception(p, uap->fd, fp, kGUARD_EXC_WRITE);
+               proc_fdunlock(p);
+       } else {
+               error = wr_uio(p, fp, auio, retval);
+               wrote_some = *retval > 0;
+       }
+       
+       if (wrote_some)
+               fp_drop_written(p, uap->fd, fp);
+       else
+               fp_drop(p, uap->fd, fp, 0);
 
 ExitThisRoutine:
        if (auio != NULL) {
@@ -575,114 +693,47 @@ ExitThisRoutine:
 
 
 int
-wr_uio(p, fdes, uio, retval)
-       struct proc *p;
-       int fdes;
-       register uio_t uio;
-       user_ssize_t *retval;
+wr_uio(struct proc *p, struct fileproc *fp, uio_t uio, user_ssize_t *retval)
 {
-       struct fileproc *fp;
        int error;
        user_ssize_t count;
-#if KTRACE
-       struct iovec_64 *ktriov = NULL;
-       struct uio ktruio;
-       int didktr = 0;
-       u_int iovlen;
-#endif
-
-       error = fp_lookup(p,fdes,&fp,0);
-       if (error)
-               return(error);
+       struct vfs_context context = *vfs_context_current();
 
-       if ((fp->f_flag & FWRITE) == 0) {
-               error = EBADF;
-               goto out;
-       }
        count = uio_resid(uio);
-#if KTRACE
-       /*
-        * if tracing, save a copy of iovec
-        */
-       if (KTRPOINT(p, KTR_GENIO)) {
-               iovlen = uio->uio_iovcnt *
-                       (IS_64BIT_PROCESS(p) ? sizeof (struct iovec_64) : sizeof (struct iovec_32));
-               MALLOC(ktriov, struct iovec_64 *, iovlen, M_TEMP, M_WAITOK);
-               if (ktriov != NULL) {
-                       bcopy((caddr_t)uio->uio_iovs.iov64p, (caddr_t)ktriov, iovlen);
-                       ktruio = *uio;
-                       didktr = 1;
-               }
-       } 
-#endif  
-       error = fo_write(fp, uio, fp->f_cred, 0, p);
+
+       context.vc_ucred = fp->f_cred;
+       error = fo_write(fp, uio, 0, &context);
        if (error) {
                if (uio_resid(uio) != count && (error == ERESTART ||
                                                error == EINTR || error == EWOULDBLOCK))
                        error = 0;
                /* The socket layer handles SIGPIPE */
-               if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
+               if (error == EPIPE && fp->f_type != DTYPE_SOCKET &&
+                   (fp->f_fglob->fg_lflags & FG_NOSIGPIPE) == 0)
                        psignal(p, SIGPIPE);
        }
        *retval = count - uio_resid(uio);
 
-#if KTRACE
-       if (didktr) {
-               if (error == 0) {
-                       ktruio.uio_iovs.iov64p = ktriov; 
-                       uio_setresid(&ktruio, *retval);
-                       ktrgenio(p->p_tracep, fdes, UIO_WRITE, &ktruio, error);
-               }
-               FREE(ktriov, M_TEMP);
-       }
-#endif
-
-out:
-       if ( (error == 0) )
-               fp_drop_written(p, fdes, fp);
-       else
-               fp_drop(p, fdes, fp, 0);
        return(error);
 }
 
 
 int
-rd_uio(p, fdes, uio, retval)
-       struct proc *p;
-       int fdes;
-       register uio_t uio;
-       user_ssize_t *retval;
+rd_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval)
 {
        struct fileproc *fp;
        int error;
        user_ssize_t count;
-#if KTRACE
-       struct iovec_64 *ktriov = NULL;
-       struct uio ktruio;
-       int didktr = 0;
-       u_int iovlen;
-#endif
+       struct vfs_context context = *vfs_context_current();
 
        if ( (error = preparefileread(p, &fp, fdes, 0)) )
                return (error);
 
        count = uio_resid(uio);
-#if KTRACE
-       /*
-        * if tracing, save a copy of iovec
-        */
-       if (KTRPOINT(p, KTR_GENIO)) {
-               iovlen = uio->uio_iovcnt *
-                       (IS_64BIT_PROCESS(p) ? sizeof (struct iovec_64) : sizeof (struct iovec_32));
-               MALLOC(ktriov, struct iovec_64 *, iovlen, M_TEMP, M_WAITOK);
-               if (ktriov != NULL) {
-                       bcopy((caddr_t)uio->uio_iovs.iov64p, (caddr_t)ktriov, iovlen);
-                       ktruio = *uio;
-                       didktr = 1;
-               }
-       } 
-#endif  
-       error = fo_read(fp, uio, fp->f_cred, 0, p);
+
+       context.vc_ucred = fp->f_cred;
+
+       error = fo_read(fp, uio, 0, &context);
 
        if (error) {
                if (uio_resid(uio) != count && (error == ERESTART ||
@@ -691,16 +742,6 @@ rd_uio(p, fdes, uio, retval)
        }
        *retval = count - uio_resid(uio);
 
-#if KTRACE
-       if (didktr) {
-               if (error == 0) {
-                       ktruio.uio_iovs.iov64p = ktriov; 
-                       uio_setresid(&ktruio, *retval);
-                       ktrgenio(p->p_tracep, fdes, UIO_READ, &ktruio, error);
-               }
-               FREE(ktriov, M_TEMP);
-       }
-#endif
        donefileread(p, fp, fdes);
 
        return (error);
@@ -709,106 +750,60 @@ rd_uio(p, fdes, uio, retval)
 /*
  * Ioctl system call
  *
+ * Returns:    0                       Success
+ *             EBADF
+ *             ENOTTY
+ *             ENOMEM
+ *             ESRCH
+ *     copyin:EFAULT
+ *     copyoutEFAULT
+ *     fp_lookup:EBADF                 Bad file descriptor
+ *     fo_ioctl:???
  */
 int
-ioctl(struct proc *p, register struct ioctl_args *uap, __unused register_t *retval)
+ioctl(struct proc *p, struct ioctl_args *uap, __unused int32_t *retval)
 {
-       struct fileproc *fp;
-       register u_long com;
+       struct fileproc *fp = NULL;
        int error = 0;
-       register u_int size;
-       caddr_t datap, memp;
-       boolean_t is64bit;
-       int tmp;
+       u_int size = 0;
+       caddr_t datap = NULL, memp = NULL;
+       boolean_t is64bit = FALSE;
+       int tmp = 0;
 #define STK_PARAMS     128
        char stkbuf[STK_PARAMS];
        int fd = uap->fd;
+       u_long com = uap->com;
+       struct vfs_context context = *vfs_context_current();
 
        AUDIT_ARG(fd, uap->fd);
-       AUDIT_ARG(cmd, CAST_DOWN(int, uap->com)); /* LP64todo: uap->com is a user-land long */
        AUDIT_ARG(addr, uap->data);
 
        is64bit = proc_is64bit(p);
-
-       proc_fdlock(p);
-       error = fp_lookup(p,fd,&fp,1);
-       if (error)  {
-               proc_fdunlock(p);
-               return(error);
-       }
-
-       AUDIT_ARG(file, p, fp);
-
-       if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
-                       error = EBADF;
-                       goto out;
-       }
-               
-#if NETAT
-       /*
-        * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
-        * while implementing an ATioctl system call
-        */
-       {
-               if (appletalk_inited && ((uap->com & 0x0000FFFF) == 0xff99)) {
-                       u_long  fixed_command;
-#ifdef APPLETALK_DEBUG
-                       kprintf("ioctl: special AppleTalk \n");
-#endif
-                       datap = &stkbuf[0];
-                       *(user_addr_t *)datap = uap->data;
-                       fixed_command = _IOW(0, 0xff99, uap->data);
-                       error = fo_ioctl(fp, fixed_command, datap, p);
-                       goto out;
-               }
-       }
-
-#endif /* NETAT */
-
-
-       switch (com = uap->com) {
-       case FIONCLEX:
-               *fdflags(p, uap->fd) &= ~UF_EXCLOSE;
-               error =0;
-               goto out;
-       case FIOCLEX:
-               *fdflags(p, uap->fd) |= UF_EXCLOSE;
-               error =0;
-               goto out;
-       }
+#if CONFIG_AUDIT
+       if (is64bit)
+               AUDIT_ARG(value64, com);
+       else
+               AUDIT_ARG(cmd, CAST_DOWN_EXPLICIT(int, com));
+#endif /* CONFIG_AUDIT */
 
        /*
         * Interpret high order word to find amount of data to be
         * copied to/from the user's address space.
         */
        size = IOCPARM_LEN(com);
-       if (size > IOCPARM_MAX) {
-                       error = ENOTTY;
-                       goto out;
-       }
-       memp = NULL;
+       if (size > IOCPARM_MAX)
+                       return ENOTTY;
        if (size > sizeof (stkbuf)) {
-               proc_fdunlock(p);
-               if ((memp = (caddr_t)kalloc(size)) == 0) {
-                       proc_fdlock(p);
-                       error = ENOMEM;
-                       goto out;
-               }
-               proc_fdlock(p);
+               if ((memp = (caddr_t)kalloc(size)) == 0)
+                       return ENOMEM;
                datap = memp;
        } else
                datap = &stkbuf[0];
-       if (com&IOC_IN) {
+       if (com & IOC_IN) {
                if (size) {
-                       proc_fdunlock(p);
                        error = copyin(uap->data, datap, size);
-                       if (error) {
-                               if (memp)
-                                       kfree(memp, size);
-                               proc_fdlock(p);
-                               goto out;
-                       }
-                       proc_fdlock(p);
+                       if (error)
+                               goto out_nofp;
                } else {
                        /* XXX - IOC_IN and no size?  we should proably return an error here!! */
                        if (is64bit) {
@@ -818,13 +813,13 @@ ioctl(struct proc *p, register struct ioctl_args *uap, __unused register_t *retv
                                *(uint32_t *)datap = (uint32_t)uap->data;
                        }
                }
-       } else if ((com&IOC_OUT) && size)
+       } else if ((com & IOC_OUT) && size)
                /*
                 * Zero the buffer so the user always
                 * gets back something deterministic.
                 */
                bzero(datap, size);
-       else if (com&IOC_VOID) {
+       else if (com & IOC_VOID) {
                /* XXX - this is odd since IOC_VOID means no parameters */
                if (is64bit) {
                        *(user_addr_t *)datap = uap->data;
@@ -834,14 +829,43 @@ ioctl(struct proc *p, register struct ioctl_args *uap, __unused register_t *retv
                }
        }
 
+       proc_fdlock(p);
+       error = fp_lookup(p,fd,&fp,1);
+       if (error)  {
+               proc_fdunlock(p);
+               goto out_nofp;
+       }
+
+       AUDIT_ARG(file, p, fp);
+
+       if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
+                       error = EBADF;
+                       goto out;
+       }
+
+       context.vc_ucred = fp->f_fglob->fg_cred;
+
+#if CONFIG_MACF
+       error = mac_file_check_ioctl(context.vc_ucred, fp->f_fglob, com);
+       if (error)
+               goto out;
+#endif
+
        switch (com) {
+       case FIONCLEX:
+               *fdflags(p, fd) &= ~UF_EXCLOSE;
+               break;
+
+       case FIOCLEX:
+               *fdflags(p, fd) |= UF_EXCLOSE;
+               break;
 
        case FIONBIO:
                if ( (tmp = *(int *)datap) )
                        fp->f_flag |= FNONBLOCK;
                else
                        fp->f_flag &= ~FNONBLOCK;
-               error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
+               error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, &context);
                break;
 
        case FIOASYNC:
@@ -849,60 +873,59 @@ ioctl(struct proc *p, register struct ioctl_args *uap, __unused register_t *retv
                        fp->f_flag |= FASYNC;
                else
                        fp->f_flag &= ~FASYNC;
-               error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
+               error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, &context);
                break;
 
        case FIOSETOWN:
                tmp = *(int *)datap;
                if (fp->f_type == DTYPE_SOCKET) {
                        ((struct socket *)fp->f_data)->so_pgid = tmp;
-                       error = 0;
                        break;
                }
                if (fp->f_type == DTYPE_PIPE) {
-                       error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
+                       error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, &context);
                        break;
                }
                if (tmp <= 0) {
                        tmp = -tmp;
                } else {
-                       struct proc *p1 = pfind(tmp);
+                       struct proc *p1 = proc_find(tmp);
                        if (p1 == 0) {
                                error = ESRCH;
                                break;
                        }
-                       tmp = p1->p_pgrp->pg_id;
+                       tmp = p1->p_pgrpid;
+                       proc_rele(p1);
                }
-               error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
+               error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, &context);
                break;
 
        case FIOGETOWN:
                if (fp->f_type == DTYPE_SOCKET) {
-                       error = 0;
                        *(int *)datap = ((struct socket *)fp->f_data)->so_pgid;
                        break;
                }
-               error = fo_ioctl(fp, TIOCGPGRP, datap, p);
+               error = fo_ioctl(fp, TIOCGPGRP, datap, &context);
                *(int *)datap = -*(int *)datap;
                break;
 
        default:
-               error = fo_ioctl(fp, com, datap, p);
+               error = fo_ioctl(fp, com, datap, &context);
                /*
                 * Copy any data to user, size was
                 * already set and checked above.
                 */
-               if (error == 0 && (com&IOC_OUT) && size)
+               if (error == 0 && (com & IOC_OUT) && size)
                        error = copyout(datap, uap->data, (u_int)size);
                break;
        }
-       proc_fdunlock(p);
-       if (memp)
-               kfree(memp, size);
-       proc_fdlock(p);
 out:
        fp_drop(p, fd, fp, 1);
        proc_fdunlock(p);
+
+out_nofp:
+       if (memp)
+               kfree(memp, size);
        return(error);
 }
 
@@ -911,36 +934,169 @@ int      selwait, nselcoll;
 #define SEL_SECONDPASS 2
 extern int selcontinue(int error);
 extern int selprocess(int error, int sel_pass);
-static int selscan(struct proc *p, struct _select * sel,
-                       int nfd, register_t *retval, int sel_pass, wait_queue_sub_t wqsub);
-static int selcount(struct proc *p, u_int32_t *ibits, u_int32_t *obits,
-                       int nfd, int * count);
+static int selscan(struct proc *p, struct _select * sel, struct _select_data * seldata,
+                       int nfd, int32_t *retval, int sel_pass, struct waitq_set *wqset);
+static int selcount(struct proc *p, u_int32_t *ibits, int nfd, int *count);
+static int seldrop_locked(struct proc *p, u_int32_t *ibits, int nfd, int lim, int *need_wakeup, int fromselcount);
 static int seldrop(struct proc *p, u_int32_t *ibits, int nfd);
-extern uint64_t        tvtoabstime(struct timeval      *tvp);
+static int select_internal(struct proc *p, struct select_nocancel_args *uap, uint64_t timeout, int32_t *retval);
 
 /*
  * Select system call.
+ *
+ * Returns:    0                       Success
+ *             EINVAL                  Invalid argument
+ *             EAGAIN                  Nonconformant error if allocation fails
  */
 int
-select(struct proc *p, struct select_args *uap, register_t *retval)
+select(struct proc *p, struct select_args *uap, int32_t *retval)
 {
-       int error = 0;
-       u_int ni, nw, size;
-       thread_t th_act;
-       struct uthread  *uth;
-       struct _select *sel;
-       int needzerofill = 1;
-       int count = 0;
+       __pthread_testcancel(1);
+       return select_nocancel(p, (struct select_nocancel_args *)uap, retval);
+}
 
-       th_act = current_thread();
-       uth = get_bsdthread_info(th_act);
-       sel = &uth->uu_select;
-       retval = (int *)get_bsduthreadrval(th_act);
-       *retval = 0;
+int
+select_nocancel(struct proc *p, struct select_nocancel_args *uap, int32_t *retval)
+{
+       uint64_t timeout = 0;
 
-       if (uap->nd < 0) {
-               return (EINVAL);
-       }
+       if (uap->tv) {
+               int err;
+               struct timeval atv;
+               if (IS_64BIT_PROCESS(p)) {
+                       struct user64_timeval atv64;
+                       err = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
+                       /* Loses resolution - assume timeout < 68 years */
+                       atv.tv_sec = atv64.tv_sec;
+                       atv.tv_usec = atv64.tv_usec;
+               } else {
+                       struct user32_timeval atv32;
+                       err = copyin(uap->tv, (caddr_t)&atv32, sizeof(atv32));
+                       atv.tv_sec = atv32.tv_sec;
+                       atv.tv_usec = atv32.tv_usec;
+               }
+               if (err)
+                       return err;
+
+               if (itimerfix(&atv)) {
+                       err = EINVAL;
+                       return err;
+               }
+
+               clock_absolutetime_interval_to_deadline(tvtoabstime(&atv), &timeout);
+       }
+
+       return select_internal(p, uap, timeout, retval);
+}
+
+int
+pselect(struct proc *p, struct pselect_args *uap, int32_t *retval)
+{
+       __pthread_testcancel(1);
+       return pselect_nocancel(p, (struct pselect_nocancel_args *)uap, retval);
+}
+
+int
+pselect_nocancel(struct proc *p, struct pselect_nocancel_args *uap, int32_t *retval)
+{
+       int err;
+       struct uthread *ut;
+       uint64_t timeout = 0;
+
+       if (uap->ts) {
+               struct timespec ts;
+
+               if (IS_64BIT_PROCESS(p)) {
+                       struct user64_timespec ts64;
+                       err = copyin(uap->ts, (caddr_t)&ts64, sizeof(ts64));
+                       ts.tv_sec = ts64.tv_sec;
+                       ts.tv_nsec = ts64.tv_nsec;
+               } else {
+                       struct user32_timespec ts32;
+                       err = copyin(uap->ts, (caddr_t)&ts32, sizeof(ts32));
+                       ts.tv_sec = ts32.tv_sec;
+                       ts.tv_nsec = ts32.tv_nsec;
+               }
+               if (err) {
+                       return err;
+               }
+
+               if (!timespec_is_valid(&ts)) {
+                       return EINVAL;
+               }
+               clock_absolutetime_interval_to_deadline(tstoabstime(&ts), &timeout);
+       }
+
+       ut = get_bsdthread_info(current_thread());
+
+       if (uap->mask != USER_ADDR_NULL) {
+               /* save current mask, then copyin and set new mask */
+               sigset_t newset;
+               err = copyin(uap->mask, &newset, sizeof(sigset_t));
+               if (err) {
+                       return err;
+               }
+               ut->uu_oldmask = ut->uu_sigmask;
+               ut->uu_flag |= UT_SAS_OLDMASK;
+               ut->uu_sigmask = (newset & ~sigcantmask);
+       }
+
+       err = select_internal(p, (struct select_nocancel_args *)uap, timeout, retval);
+
+       if (err != EINTR && ut->uu_flag & UT_SAS_OLDMASK) {
+               /*
+                * Restore old mask (direct return case). NOTE: EINTR can also be returned
+                * if the thread is cancelled. In that case, we don't reset the signal
+                * mask to its original value (which usually happens in the signal
+                * delivery path). This behavior is permitted by POSIX.
+                */
+               ut->uu_sigmask = ut->uu_oldmask;
+               ut->uu_oldmask = 0;
+               ut->uu_flag &= ~UT_SAS_OLDMASK;
+       }
+
+       return err;
+}
+
+/*
+ * Generic implementation of {,p}select. Care: we type-pun uap across the two
+ * syscalls, which differ slightly. The first 4 arguments (nfds and the fd sets)
+ * are identical. The 5th (timeout) argument points to different types, so we
+ * unpack in the syscall-specific code, but the generic code still does a null
+ * check on this argument to determine if a timeout was specified.
+ */
+static int
+select_internal(struct proc *p, struct select_nocancel_args *uap, uint64_t timeout, int32_t *retval)
+{
+       int error = 0;
+       u_int ni, nw;
+       thread_t th_act;
+       struct uthread  *uth;
+       struct _select *sel;
+       struct _select_data *seldata;
+       int needzerofill = 1;
+       int count = 0;
+       size_t sz = 0;
+
+       th_act = current_thread();
+       uth = get_bsdthread_info(th_act);
+       sel = &uth->uu_select;
+       seldata = &uth->uu_kevent.ss_select_data;
+       *retval = 0;
+
+       seldata->args = uap;
+       seldata->retval = retval;
+       seldata->wqp = NULL;
+       seldata->count = 0;
+
+       if (uap->nd < 0) {
+               return (EINVAL);
+       }
+
+       /* select on thread of process that already called proc_exit() */
+       if (p->p_fd == NULL) {
+               return (EBADF);
+       }
 
        if (uap->nd > p->p_fd->fd_nfiles)
                uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
@@ -949,32 +1105,38 @@ select(struct proc *p, struct select_args *uap, register_t *retval)
        ni = nw * sizeof(fd_mask);
 
        /*
-        * if this is the first select by the thread 
-        * allocate the space for bits.
-        */
-       if (sel->nbytes == 0) {
-               sel->nbytes = 3 * ni;
-               MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
-               MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
-               if ((sel->ibits == NULL) || (sel->obits == NULL))
-                       panic("select out of memory");
-               needzerofill = 0;
-       }
-
-       /*
-        * if the previously allocated space for the bits
-        * is smaller than what is requested. Reallocate.
+        * if the previously allocated space for the bits is smaller than
+        * what is requested or no space has yet been allocated for this
+        * thread, allocate enough space now.
+        *
+        * Note: If this process fails, select() will return EAGAIN; this
+        * is the same thing pool() returns in a no-memory situation, but
+        * it is not a POSIX compliant error code for select().
         */
        if (sel->nbytes < (3 * ni)) {
-               sel->nbytes = (3 * ni);
-               FREE(sel->ibits, M_TEMP);
-               FREE(sel->obits, M_TEMP);
-               MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
-               MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
-               if ((sel->ibits == NULL) || (sel->obits == NULL))
-                       panic("select out of memory");
+               int nbytes = 3 * ni;
+
+               /* Free previous allocation, if any */
+               if (sel->ibits != NULL)
+                       FREE(sel->ibits, M_TEMP);
+               if (sel->obits != NULL) {
+                       FREE(sel->obits, M_TEMP);
+                       /* NULL out; subsequent ibits allocation may fail */
+                       sel->obits = NULL;
+               }
+
+               MALLOC(sel->ibits, u_int32_t *, nbytes, M_TEMP, M_WAITOK | M_ZERO);
+               if (sel->ibits == NULL)
+                       return (EAGAIN);
+               MALLOC(sel->obits, u_int32_t *, nbytes, M_TEMP, M_WAITOK | M_ZERO);
+               if (sel->obits == NULL) {
+                       FREE(sel->ibits, M_TEMP);
+                       sel->ibits = NULL;
+                       return (EAGAIN);
+               }
+               sel->nbytes = nbytes;
                needzerofill = 0;
-       }   
+       }
 
        if (needzerofill) {
                bzero((caddr_t)sel->ibits, sel->nbytes);
@@ -996,60 +1158,77 @@ select(struct proc *p, struct select_args *uap, register_t *retval)
        getbits(ex, 2);
 #undef getbits
 
-       if (uap->tv) {
-               struct timeval atv;
-               if (IS_64BIT_PROCESS(p)) {
-                       struct user_timeval atv64;
-                       error = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
-                       /* Loses resolution - assume timeout < 68 years */
-                       atv.tv_sec = atv64.tv_sec;
-                       atv.tv_usec = atv64.tv_usec;
-               } else {
-                       error = copyin(uap->tv, (caddr_t)&atv, sizeof(atv));
-               }
-               if (error)
-                       goto continuation;
-               if (itimerfix(&atv)) {
-                       error = EINVAL;
-                       goto continuation;
-               }
-
-               clock_absolutetime_interval_to_deadline(
-                                                                               tvtoabstime(&atv), &sel->abstime);
-       }
-       else
-               sel->abstime = 0;
+       seldata->abstime = timeout;
 
-       if ( (error = selcount(p, sel->ibits, sel->obits, uap->nd, &count)) ) {
+       if ( (error = selcount(p, sel->ibits, uap->nd, &count)) ) {
                        goto continuation;
        }
 
-       sel->count = count;
-       size = SIZEOF_WAITQUEUE_SET + (count * SIZEOF_WAITQUEUE_LINK);
-       if (sel->allocsize) {
-               if (sel->wqset == 0)
-                       panic("select: wql memory smashed");
-               /* needed for the select now */
-               if (size > sel->allocsize) {
-                       kfree(sel->wqset,  sel->allocsize);
-                       sel->allocsize = size;
-                       sel->wqset = (wait_queue_set_t)kalloc(size);
-                       if (sel->wqset == (wait_queue_set_t)NULL)
-                               panic("failed to allocate memory for waitqueue\n");
-               }
-       } else {
-               sel->count = count;
-               sel->allocsize = size;
-               sel->wqset = (wait_queue_set_t)kalloc(sel->allocsize);
-               if (sel->wqset == (wait_queue_set_t)NULL)
-                       panic("failed to allocate memory for waitqueue\n");
-       }
-       bzero(sel->wqset, size);
-       sel->wql = (char *)sel->wqset + SIZEOF_WAITQUEUE_SET;
-       wait_queue_set_init(sel->wqset, (SYNC_POLICY_FIFO | SYNC_POLICY_PREPOST));
+       /*
+        * We need an array of waitq pointers. This is due to the new way
+        * in which waitqs are linked to sets. When a thread selects on a
+        * file descriptor, a waitq (embedded in a selinfo structure) is
+        * added to the thread's local waitq set. There is no longer any
+        * way to directly iterate over all members of a given waitq set.
+        * The process of linking a waitq into a set may allocate a link
+        * table object. Because we can't iterate over all the waitqs to
+        * which our thread waitq set belongs, we need a way of removing
+        * this link object!
+        *
+        * Thus we need a buffer which will hold one waitq pointer
+        * per FD being selected. During the tear-down phase we can use
+        * these pointers to dis-associate the underlying selinfo's waitq
+        * from our thread's waitq set.
+        *
+        * Because we also need to allocate a waitq set for this thread,
+        * we use a bare buffer pointer to hold all the memory. Note that
+        * this memory is cached in the thread pointer and not reaped until
+        * the thread exists. This is generally OK because threads that
+        * call select tend to keep calling select repeatedly.
+        */
+       sz = ALIGN(sizeof(struct waitq_set)) + (count * sizeof(uint64_t));
+       if (sz > uth->uu_wqstate_sz) {
+               /* (re)allocate a buffer to hold waitq pointers */
+               if (uth->uu_wqset) {
+                       if (waitq_set_is_valid(uth->uu_wqset))
+                               waitq_set_deinit(uth->uu_wqset);
+                       FREE(uth->uu_wqset, M_SELECT);
+               } else if (uth->uu_wqstate_sz && !uth->uu_wqset)
+                       panic("select: thread structure corrupt! "
+                             "uu_wqstate_sz:%ld, wqstate_buf == NULL",
+                             uth->uu_wqstate_sz);
+               uth->uu_wqstate_sz = sz;
+               MALLOC(uth->uu_wqset, struct waitq_set *, sz, M_SELECT, M_WAITOK);
+               if (!uth->uu_wqset)
+                       panic("can't allocate %ld bytes for wqstate buffer",
+                             uth->uu_wqstate_sz);
+               waitq_set_init(uth->uu_wqset,
+                              SYNC_POLICY_FIFO|SYNC_POLICY_PREPOST, NULL, NULL);
+       }
+
+       if (!waitq_set_is_valid(uth->uu_wqset))
+               waitq_set_init(uth->uu_wqset,
+                              SYNC_POLICY_FIFO|SYNC_POLICY_PREPOST, NULL, NULL);
+
+       /* the last chunk of our buffer is an array of waitq pointers */
+       seldata->wqp = (uint64_t *)((char *)(uth->uu_wqset) + ALIGN(sizeof(struct waitq_set)));
+       bzero(seldata->wqp, sz - ALIGN(sizeof(struct waitq_set)));
+
+       seldata->count = count;
 
 continuation:
-       return selprocess(error, SEL_FIRSTPASS);
+
+       if (error) {
+               /*
+                * We have already cleaned up any state we established,
+                * either locally or as a result of selcount().  We don't
+                * need to wait_subqueue_unlink_all(), since we haven't set
+                * anything at this point.
+                */
+               return (error);
+       }
+
+       return selprocess(0, SEL_FIRSTPASS);
 }
 
 int
@@ -1058,6 +1237,13 @@ selcontinue(int error)
        return selprocess(error, SEL_SECONDPASS);
 }
 
+
+/*
+ * selprocess
+ *
+ * Parameters: error                   The error code from our caller
+ *             sel_pass                The pass we are on
+ */
 int
 selprocess(int error, int sel_pass)
 {
@@ -1066,9 +1252,10 @@ selprocess(int error, int sel_pass)
        thread_t th_act;
        struct uthread  *uth;
        struct proc *p;
-       struct select_args *uap;
+       struct select_nocancel_args *uap;
        int *retval;
        struct _select *sel;
+       struct _select_data *seldata;
        int unwind = 1;
        int prepost = 0;
        int somewakeup = 0;
@@ -1077,42 +1264,38 @@ selprocess(int error, int sel_pass)
 
        p = current_proc();
        th_act = current_thread();
-       uap = (struct select_args *)get_bsduthreadarg(th_act);
-       retval = (int *)get_bsduthreadrval(th_act);
        uth = get_bsdthread_info(th_act);
        sel = &uth->uu_select;
+       seldata = &uth->uu_kevent.ss_select_data;
+       uap = seldata->args;
+       retval = seldata->retval;
 
-       /* if it is first pass wait queue is not setup yet */
        if ((error != 0) && (sel_pass == SEL_FIRSTPASS))
-                       unwind = 0;
-       if (sel->count == 0)
-                       unwind = 0;
+               unwind = 0;
+       if (seldata->count == 0)
+               unwind = 0;
 retry:
-       if (error != 0) {
-         goto done;
-       }
+       if (error != 0)
+               goto done;
 
        ncoll = nselcoll;
-       p->p_flag |= P_SELECT;
-       /* skip scans if the select is just for timeouts */
-       if (sel->count) {
-               if (sel_pass == SEL_FIRSTPASS)
-                       wait_queue_sub_clearrefs(sel->wqset);
+       OSBitOrAtomic(P_SELECT, &p->p_flag);
 
-               error = selscan(p, sel, uap->nd, retval, sel_pass, sel->wqset);
+       /* skip scans if the select is just for timeouts */
+       if (seldata->count) {
+               error = selscan(p, sel, seldata, uap->nd, retval, sel_pass, uth->uu_wqset);
                if (error || *retval) {
                        goto done;
                }
-               if (prepost) {
-                       /* if the select of log, then we canwakeup and discover some one
-                       * else already read the data; go toselct again if time permits
-                       */
-                       prepost = 0;
-                       doretry = 1;
-               }
-               if (somewakeup) {
-                       somewakeup = 0;
-                       doretry = 1;
+               if (prepost || somewakeup) {
+                       /*
+                        * if the select of log, then we can wakeup and
+                        * discover some one else already read the data;
+                        * go to select again if time permits
+                        */
+                       prepost = 0;
+                       somewakeup = 0;
+                       doretry = 1;
                }
        }
 
@@ -1120,7 +1303,7 @@ retry:
                uint64_t        now;
 
                clock_get_uptime(&now);
-               if (now >= sel->abstime)
+               if (now >= seldata->abstime)
                        goto done;
        }
 
@@ -1135,7 +1318,7 @@ retry:
         * To effect a poll, the timeout argument should be
         * non-nil, pointing to a zero-valued timeval structure.
         */
-       if (uap->tv && sel->abstime == 0) {
+       if (uap->tv && seldata->abstime == 0) {
                goto done;
        }
 
@@ -1145,15 +1328,18 @@ retry:
                goto retry;
        }
 
-       p->p_flag &= ~P_SELECT;
+       OSBitAndAtomic(~((uint32_t)P_SELECT), &p->p_flag);
 
        /* if the select is just for timeout skip check */
-       if (sel->count &&(sel_pass == SEL_SECONDPASS))
+       if (seldata->count && (sel_pass == SEL_SECONDPASS))
                panic("selprocess: 2nd pass assertwaiting");
 
-       /* Wait Queue Subordinate has waitqueue as first element */
-       wait_result = wait_queue_assert_wait((wait_queue_t)sel->wqset,
-                                            &selwait, THREAD_ABORTSAFE, sel->abstime);
+       /* waitq_set has waitqueue as first element */
+       wait_result = waitq_assert_wait64_leeway((struct waitq *)uth->uu_wqset,
+                                                NO_EVENT64, THREAD_ABORTSAFE,
+                                                TIMEOUT_URGENCY_USER_NORMAL,
+                                                seldata->abstime,
+                                                TIMEOUT_NO_LEEWAY);
        if (wait_result != THREAD_AWAKENED) {
                /* there are no preposted events */
                error = tsleep1(NULL, PSOCK | PCATCH,
@@ -1163,18 +1349,24 @@ retry:
                error = 0;
        }
 
-       sel_pass = SEL_SECONDPASS;
        if (error == 0) {
+               sel_pass = SEL_SECONDPASS;
                if (!prepost)
-                       somewakeup =1;
+                       somewakeup = 1;
                goto retry;
        }
 done:
        if (unwind) {
-               wait_subqueue_unlink_all(sel->wqset);
                seldrop(p, sel->ibits, uap->nd);
+               waitq_set_deinit(uth->uu_wqset);
+               /*
+                * zero out the waitq pointer array to avoid use-after free
+                * errors in the selcount error path (seldrop_locked) if/when
+                * the thread re-calls select().
+                */
+               bzero((void *)uth->uu_wqset, uth->uu_wqstate_sz);
        }
-       p->p_flag &= ~P_SELECT;
+       OSBitAndAtomic(~((uint32_t)P_SELECT), &p->p_flag);
        /* select is not restarted after signals... */
        if (error == ERESTART)
                error = EINTR;
@@ -1198,30 +1390,164 @@ done:
                putbits(ex, 2);
 #undef putbits
        }
+
+       if (error != EINTR && sel_pass == SEL_SECONDPASS && uth->uu_flag & UT_SAS_OLDMASK) {
+               /* restore signal mask - continuation case */
+               uth->uu_sigmask = uth->uu_oldmask;
+               uth->uu_oldmask = 0;
+               uth->uu_flag &= ~UT_SAS_OLDMASK;
+       }
+
        return(error);
 }
 
+
+/**
+ * remove the fileproc's underlying waitq from the supplied waitq set;
+ * clear FP_INSELECT when appropriate
+ *
+ * Parameters:
+ *             fp      File proc that is potentially currently in select
+ *             wqset   Waitq set to which the fileproc may belong
+ *                     (usually this is the thread's private waitq set)
+ * Conditions:
+ *             proc_fdlock is held
+ */
+static void selunlinkfp(struct fileproc *fp, uint64_t wqp_id, struct waitq_set *wqset)
+{
+       int valid_set = waitq_set_is_valid(wqset);
+       int valid_q = !!wqp_id;
+
+       /*
+        * This could be called (from selcount error path) before we setup
+        * the thread's wqset. Check the wqset passed in, and only unlink if
+        * the set is valid.
+        */
+
+       /* unlink the underlying waitq from the input set (thread waitq set) */
+       if (valid_q && valid_set)
+               waitq_unlink_by_prepost_id(wqp_id, wqset);
+
+       /* allow passing a NULL/invalid fp for seldrop unwind */
+       if (!fp || !(fp->f_flags & (FP_INSELECT|FP_SELCONFLICT)))
+               return;
+
+       /*
+        * We can always remove the conflict queue from our thread's set: this
+        * will not affect other threads that potentially need to be awoken on
+        * the conflict queue during a fileproc_drain - those sets will still
+        * be linked with the global conflict queue, and the last waiter
+        * on the fp clears the CONFLICT marker.
+        */
+       if (valid_set && (fp->f_flags & FP_SELCONFLICT))
+               waitq_unlink(&select_conflict_queue, wqset);
+
+       /* jca: TODO:
+        * This isn't quite right - we don't actually know if this
+        * fileproc is in another select or not! Here we just assume
+        * that if we were the first thread to select on the FD, then
+        * we'll be the one to clear this flag...
+        */
+       if (valid_set && fp->f_wset == (void *)wqset) {
+               fp->f_flags &= ~FP_INSELECT;
+               fp->f_wset = NULL;
+       }
+}
+
+/**
+ * connect a fileproc to the given wqset, potentially bridging to a waitq
+ * pointed to indirectly by wq_data
+ *
+ * Parameters:
+ *             fp      File proc potentially currently in select
+ *             wq_data Pointer to a pointer to a waitq (could be NULL)
+ *             wqset   Waitq set to which the fileproc should now belong
+ *                     (usually this is the thread's private waitq set)
+ *
+ * Conditions:
+ *             proc_fdlock is held
+ */
+static uint64_t sellinkfp(struct fileproc *fp, void **wq_data, struct waitq_set *wqset)
+{
+       struct waitq *f_wq = NULL;
+
+       if ((fp->f_flags & FP_INSELECT) != FP_INSELECT) {
+               if (wq_data)
+                       panic("non-null data:%p on fp:%p not in select?!"
+                             "(wqset:%p)", wq_data, fp, wqset);
+               return 0;
+       }
+
+       if ((fp->f_flags & FP_SELCONFLICT) == FP_SELCONFLICT) {
+               /*
+                * The conflict queue requires disabling interrupts, so we
+                * need to explicitly reserve a link object to avoid a
+                * panic/assert in the waitq code. Hopefully this extra step
+                * can be avoided if we can split the waitq structure into
+                * blocking and linkage sub-structures.
+                */
+               uint64_t reserved_link = waitq_link_reserve(&select_conflict_queue);
+               waitq_link(&select_conflict_queue, wqset, WAITQ_SHOULD_LOCK, &reserved_link);
+               waitq_link_release(reserved_link);
+       }
+
+       /*
+        * The wq_data parameter has potentially been set by selrecord called
+        * from a subsystems fo_select() function. If the subsystem does not
+        * call selrecord, then wq_data will be NULL
+        *
+        * Use memcpy to get the value into a proper pointer because
+        * wq_data most likely points to a stack variable that could be
+        * unaligned on 32-bit systems.
+        */
+       if (wq_data) {
+               memcpy(&f_wq, wq_data, sizeof(f_wq));
+               if (!waitq_is_valid(f_wq))
+                       f_wq = NULL;
+       }
+
+       /* record the first thread's wqset in the fileproc structure */
+       if (!fp->f_wset)
+               fp->f_wset = (void *)wqset;
+
+       /* handles NULL f_wq */
+       return waitq_get_prepost_id(f_wq);
+}
+
+
+/*
+ * selscan
+ *
+ * Parameters: p                       Process performing the select
+ *             sel                     The per-thread select context structure
+ *             nfd                     The number of file descriptors to scan
+ *             retval                  The per thread system call return area
+ *             sel_pass                Which pass this is; allowed values are
+ *                                             SEL_FIRSTPASS and SEL_SECONDPASS
+ *             wqset                   The per thread wait queue set
+ *
+ * Returns:    0                       Success
+ *             EIO                     Invalid p->p_fd field XXX Obsolete?
+ *             EBADF                   One of the files in the bit vector is
+ *                                             invalid.
+ */
 static int
-selscan(p, sel, nfd, retval, sel_pass, wqsub)
-       struct proc *p;
-       struct _select *sel;
-       int nfd;
-       register_t *retval;
-       int sel_pass;
-       wait_queue_sub_t wqsub;
+selscan(struct proc *p, struct _select *sel, struct _select_data * seldata,
+       int nfd, int32_t *retval, int sel_pass, struct waitq_set *wqset)
 {
-       register struct filedesc *fdp = p->p_fd;
-       register int msk, i, j, fd;
-       register u_int32_t bits;
+       struct filedesc *fdp = p->p_fd;
+       int msk, i, j, fd;
+       u_int32_t bits;
        struct fileproc *fp;
-       int n = 0;
-       int nc = 0;
+       int n = 0;              /* count of bits */
+       int nc = 0;             /* bit vector offset (nc'th bit) */
        static int flag[3] = { FREAD, FWRITE, 0 };
        u_int32_t *iptr, *optr;
        u_int nw;
        u_int32_t *ibits, *obits;
-       char * wql;
-       char * wql_ptr;
+       uint64_t reserved_link, *rl_ptr = NULL;
+       int count;
+       struct vfs_context context = *vfs_context_current();
 
        /*
         * Problems when reboot; due to MacOSX signal probs
@@ -1233,54 +1559,98 @@ selscan(p, sel, nfd, retval, sel_pass, wqsub)
        }
        ibits = sel->ibits;
        obits = sel->obits;
-       wql = sel->wql;
 
        nw = howmany(nfd, NFDBITS);
 
+       count = seldata->count;
+
        nc = 0;
+       if (!count) {
+               *retval = 0;
+               return 0;
+       }
+
        proc_fdlock(p);
+       for (msk = 0; msk < 3; msk++) {
+               iptr = (u_int32_t *)&ibits[msk * nw];
+               optr = (u_int32_t *)&obits[msk * nw];
 
-       if (sel->count) {
-               for (msk = 0; msk < 3; msk++) {
-                       iptr = (u_int32_t *)&ibits[msk * nw];
-                       optr = (u_int32_t *)&obits[msk * nw];
+               for (i = 0; i < nfd; i += NFDBITS) {
+                       bits = iptr[i/NFDBITS];
 
-                       for (i = 0; i < nfd; i += NFDBITS) {
-                               bits = iptr[i/NFDBITS];
+                       while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
+                               bits &= ~(1 << j);
 
-                               while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
-                                       bits &= ~(1 << j);
+                               if (fd < fdp->fd_nfiles)
                                        fp = fdp->fd_ofiles[fd];
-
-                                       if (fp == NULL ||
-                                               (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
-                                               proc_fdunlock(p);
-                                               return(EBADF);
-                                       }
-                                       if (sel_pass == SEL_SECONDPASS) {
-                                               wql_ptr = (char *)0;
-                                               fp->f_flags &= ~FP_INSELECT;
-                                               fp->f_waddr = (void *)0;
-                                       } else {
-                                               wql_ptr = (wql + nc * SIZEOF_WAITQUEUE_LINK);
+                               else
+                                       fp = NULL;
+
+                               if (fp == NULL || (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
+                                       /*
+                                        * If we abort because of a bad
+                                        * fd, let the caller unwind...
+                                        */
+                                       proc_fdunlock(p);
+                                       return(EBADF);
+                               }
+                               if (sel_pass == SEL_SECONDPASS) {
+                                       reserved_link = 0;
+                                       rl_ptr = NULL;
+                                       selunlinkfp(fp, seldata->wqp[nc], wqset);
+                               } else {
+                                       reserved_link = waitq_link_reserve((struct waitq *)wqset);
+                                       rl_ptr = &reserved_link;
+                                       if (fp->f_flags & FP_INSELECT)
+                                               /* someone is already in select on this fp */
+                                               fp->f_flags |= FP_SELCONFLICT;
+                                       else
                                                fp->f_flags |= FP_INSELECT;
-                                               fp->f_waddr = (void *)wqsub;
-                                       }
-                                       if (fp->f_ops && fo_select(fp, flag[msk], wql_ptr, p)) {
-                                               optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
-                                               n++;
-                                       }
-                                       nc++;
                                }
+
+                               context.vc_ucred = fp->f_cred;
+
+                               /*
+                                * stash this value b/c fo_select may replace
+                                * reserved_link with a pointer to a waitq object
+                                */
+                               uint64_t rsvd = reserved_link;
+
+                               /* The select; set the bit, if true */
+                               if (fp->f_ops && fp->f_type
+                                       && fo_select(fp, flag[msk], rl_ptr, &context)) {
+                                       optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
+                                       n++;
+                               }
+                               if (sel_pass == SEL_FIRSTPASS) {
+                                       waitq_link_release(rsvd);
+                                       /*
+                                        * If the fp's supporting selinfo structure was linked
+                                        * to this thread's waitq set, then 'reserved_link'
+                                        * will have been updated by selrecord to be a pointer
+                                        * to the selinfo's waitq.
+                                        */
+                                       if (reserved_link == rsvd)
+                                               rl_ptr = NULL; /* fo_select never called selrecord() */
+                                       /*
+                                        * Hook up the thread's waitq set either to
+                                        * the fileproc structure, or to the global
+                                        * conflict queue: but only on the first
+                                        * select pass.
+                                        */
+                                       seldata->wqp[nc] = sellinkfp(fp, (void **)rl_ptr, wqset);
+                               }
+                               nc++;
                        }
                }
        }
        proc_fdunlock(p);
+
        *retval = n;
        return (0);
 }
 
-static int poll_callback(struct kqueue *, struct kevent *, void *);
+int poll_callback(struct kqueue *, struct kevent_internal_s *, void *);
 
 struct poll_continue_args {
        user_addr_t pca_fds;
@@ -1289,7 +1659,15 @@ struct poll_continue_args {
 };
 
 int
-poll(struct proc *p, struct poll_args *uap, register_t *retval)
+poll(struct proc *p, struct poll_args *uap, int32_t *retval)
+{
+       __pthread_testcancel(1);
+       return(poll_nocancel(p, (struct poll_nocancel_args *)uap, retval));
+}
+
+
+int
+poll_nocancel(struct proc *p, struct poll_nocancel_args *uap, int32_t *retval)
 {
        struct poll_continue_args *cont;
        struct pollfd *fds;
@@ -1309,10 +1687,10 @@ poll(struct proc *p, struct poll_args *uap, register_t *retval)
         * safe, but not overly restrictive.
         */
        if (nfds > OPEN_MAX ||
-           (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE))
+           (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && (proc_suser(p) || nfds > FD_SETSIZE)))
                return (EINVAL);
 
-       kq = kqueue_alloc(p);
+       kq = kqueue_alloc(p, 0);
        if (kq == NULL)
                return (EAGAIN);
 
@@ -1346,12 +1724,9 @@ poll(struct proc *p, struct poll_args *uap, register_t *retval)
 
        /* JMM - all this P_SELECT stuff is bogus */
        ncoll = nselcoll;
-       p->p_flag |= P_SELECT;
-
+       OSBitOrAtomic(P_SELECT, &p->p_flag);
        for (i = 0; i < nfds; i++) {
                short events = fds[i].events;
-               struct kevent kev;
-               int kerror = 0;
 
                /* per spec, ignore fd values below zero */
                if (fds[i].fd < 0) {
@@ -1360,30 +1735,29 @@ poll(struct proc *p, struct poll_args *uap, register_t *retval)
                }
 
                /* convert the poll event into a kqueue kevent */
-               kev.ident = fds[i].fd;
-               kev.flags = EV_ADD | EV_ONESHOT | EV_POLL;
-               kev.fflags = NOTE_LOWAT;
-               kev.data = 1; /* efficiency be damned: any data should trigger */
-               kev.udata = CAST_USER_ADDR_T(&fds[i]);
+               struct kevent_internal_s kev = {
+                       .ident = fds[i].fd,
+                       .flags = EV_ADD | EV_ONESHOT | EV_POLL,
+                       .udata = CAST_USER_ADDR_T(&fds[i]) };
 
                /* Handle input events */
-               if (events & ( POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND )) {
+               if (events & ( POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND | POLLHUP )) {
                        kev.filter = EVFILT_READ;
-                       if (!(events & ( POLLIN | POLLRDNORM )))
+                       if (events & ( POLLPRI | POLLRDBAND ))
                                kev.flags |= EV_OOBAND;
-                       kerror = kevent_register(kq, &kev, p);
+                       kevent_register(kq, &kev, p);
                }
 
                /* Handle output events */
-               if (kerror == 0 &&
-                   events & ( POLLOUT | POLLWRNORM | POLLWRBAND )) {
+               if ((kev.flags & EV_ERROR) == 0 &&
+                   (events & ( POLLOUT | POLLWRNORM | POLLWRBAND ))) {
                        kev.filter = EVFILT_WRITE;
-                       kerror = kevent_register(kq, &kev, p);
+                       kevent_register(kq, &kev, p);
                }
 
                /* Handle BSD extension vnode events */
-               if (kerror == 0 &&
-                   events & ( POLLEXTEND | POLLATTRIB | POLLNLINK | POLLWRITE )) {
+               if ((kev.flags & EV_ERROR) == 0 &&
+                   (events & ( POLLEXTEND | POLLATTRIB | POLLNLINK | POLLWRITE ))) {
                        kev.filter = EVFILT_VNODE;
                        kev.fflags = 0;
                        if (events & POLLEXTEND)
@@ -1394,10 +1768,10 @@ poll(struct proc *p, struct poll_args *uap, register_t *retval)
                                kev.fflags |= NOTE_LINK;
                        if (events & POLLWRITE)
                                kev.fflags |= NOTE_WRITE;
-                       kerror = kevent_register(kq, &kev, p);
+                       kevent_register(kq, &kev, p);
                }
 
-               if (kerror != 0) {
+               if (kev.flags & EV_ERROR) {
                        fds[i].revents = POLLNVAL;
                        rfds++;
                } else
@@ -1405,18 +1779,18 @@ poll(struct proc *p, struct poll_args *uap, register_t *retval)
        }
 
        /* Did we have any trouble registering? */
-       if (rfds > 0)
+       if (rfds == nfds)
                goto done;
 
        /* scan for, and possibly wait for, the kevents to trigger */
        cont->pca_fds = uap->fds;
        cont->pca_nfds = nfds;
        cont->pca_rfds = rfds;
-       error = kevent_scan(kq, poll_callback, NULL, cont, &atv, p);
+       error = kqueue_scan(kq, poll_callback, NULL, cont, NULL, &atv, p);
        rfds = cont->pca_rfds;
 
  done:
-       p->p_flag &= ~P_SELECT;
+       OSBitAndAtomic(~((uint32_t)P_SELECT), &p->p_flag);
        /* poll is not restarted after signals... */
        if (error == ERESTART)
                error = EINTR;
@@ -1431,34 +1805,32 @@ poll(struct proc *p, struct poll_args *uap, register_t *retval)
        if (NULL != cont)
                FREE(cont, M_TEMP);
 
-       kqueue_dealloc(kq, p);
+       kqueue_dealloc(kq);
        return (error);
 }
 
-static int
-poll_callback(__unused struct kqueue *kq, struct kevent *kevp, void *data)
+int
+poll_callback(__unused struct kqueue *kq, struct kevent_internal_s *kevp, void *data)
 {
        struct poll_continue_args *cont = (struct poll_continue_args *)data;
        struct pollfd *fds = CAST_DOWN(struct pollfd *, kevp->udata);
-       short mask;
+       short prev_revents = fds->revents;
+       short mask = 0;
 
        /* convert the results back into revents */
        if (kevp->flags & EV_EOF)
                fds->revents |= POLLHUP;
        if (kevp->flags & EV_ERROR)
                fds->revents |= POLLERR;
-       cont->pca_rfds++;
 
        switch (kevp->filter) {
        case EVFILT_READ:
                if (fds->revents & POLLHUP)
                        mask = (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND );
                else {
-                       mask = 0;
-                       if (kevp->data != 0)
-                               mask |= (POLLIN | POLLRDNORM );
+                       mask = (POLLIN | POLLRDNORM);
                        if (kevp->flags & EV_OOBAND)
-                               mask |= ( POLLPRI | POLLRDBAND );
+                               mask |= (POLLPRI | POLLRDBAND);
                }
                fds->revents |= (fds->events & mask);
                break;
@@ -1468,7 +1840,7 @@ poll_callback(__unused struct kqueue *kq, struct kevent *kevp, void *data)
                        fds->revents |= (fds->events & ( POLLOUT | POLLWRNORM | POLLWRBAND ));
                break;
 
-       case EVFILT_PROC:
+       case EVFILT_VNODE:
                if (kevp->fflags & NOTE_EXTEND)
                        fds->revents |= (fds->events & POLLEXTEND);
                if (kevp->fflags & NOTE_ATTRIB)
@@ -1479,6 +1851,10 @@ poll_callback(__unused struct kqueue *kq, struct kevent *kevp, void *data)
                        fds->revents |= (fds->events & POLLWRITE);
                break;
        }
+
+       if (fds->revents != 0 && prev_revents == 0)
+               cont->pca_rfds++;
+
        return 0;
 }
        
@@ -1489,26 +1865,50 @@ seltrue(__unused dev_t dev, __unused int flag, __unused struct proc *p)
        return (1);
 }
 
+/*
+ * selcount
+ *
+ * Count the number of bits set in the input bit vector, and establish an
+ * outstanding fp->f_iocount for each of the descriptors which will be in
+ * use in the select operation.
+ *
+ * Parameters: p                       The process doing the select
+ *             ibits                   The input bit vector
+ *             nfd                     The number of fd's in the vector
+ *             countp                  Pointer to where to store the bit count
+ *
+ * Returns:    0                       Success
+ *             EIO                     Bad per process open file table
+ *             EBADF                   One of the bits in the input bit vector
+ *                                             references an invalid fd
+ *
+ * Implicit:   *countp (modified)      Count of fd's
+ *
+ * Notes:      This function is the first pass under the proc_fdlock() that
+ *             permits us to recognize invalid descriptors in the bit vector;
+ *             the may, however, not remain valid through the drop and
+ *             later reacquisition of the proc_fdlock().
+ */
 static int
-selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits, 
-                int nfd, int *count)
+selcount(struct proc *p, u_int32_t *ibits, int nfd, int *countp)
 {
-       register struct filedesc *fdp = p->p_fd;
-       register int msk, i, j, fd;
-       register u_int32_t bits;
+       struct filedesc *fdp = p->p_fd;
+       int msk, i, j, fd;
+       u_int32_t bits;
        struct fileproc *fp;
        int n = 0;
        u_int32_t *iptr;
        u_int nw;
        int error=0; 
        int dropcount;
+       int need_wakeup = 0;
 
        /*
         * Problems when reboot; due to MacOSX signal probs
         * in Beaker1C ; verify that the p->p_fd is valid
         */
        if (fdp == NULL) {
-               *count=0;
+               *countp = 0;
                return(EIO);
        }
        nw = howmany(nfd, NFDBITS);
@@ -1520,10 +1920,15 @@ selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits,
                        bits = iptr[i/NFDBITS];
                        while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
                                bits &= ~(1 << j);
-                               fp = fdp->fd_ofiles[fd];
+
+                               if (fd < fdp->fd_nfiles)
+                                       fp = fdp->fd_ofiles[fd];
+                               else
+                                       fp = NULL;
+
                                if (fp == NULL ||
                                        (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
-                                               *count=0;
+                                               *countp = 0;
                                                error = EBADF;
                                                goto bad;
                                }
@@ -1534,51 +1939,66 @@ selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits,
        }
        proc_fdunlock(p);
 
-       *count = n;
+       *countp = n;
        return (0);
+
 bad:
        dropcount = 0;
        
-       if (n== 0)
+       if (n == 0)
                goto out;
-       /* undo the iocounts */
-       for (msk = 0; msk < 3; msk++) {
-               iptr = (u_int32_t *)&ibits[msk * nw];
-               for (i = 0; i < nfd; i += NFDBITS) {
-                       bits = iptr[i/NFDBITS];
-                       while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
-                               bits &= ~(1 << j);
-                               fp = fdp->fd_ofiles[fd];
-                               if (dropcount >= n)
-                                       goto out;
-                               fp->f_iocount--;
+       /* Ignore error return; it's already EBADF */
+       (void)seldrop_locked(p, ibits, nfd, n, &need_wakeup, 1);
 
-                               if (p->p_fpdrainwait && fp->f_iocount == 0) {
-                                       p->p_fpdrainwait = 0;
-                                       wakeup(&p->p_fpdrainwait);
-                               }
-                               dropcount++;
-                       }
-               }
-       }
 out:
        proc_fdunlock(p);
+       if (need_wakeup) {
+               wakeup(&p->p_fpdrainwait);
+       }
        return(error);
 }
 
+
+/*
+ * seldrop_locked
+ *
+ * Drop outstanding wait queue references set up during selscan(); drop the
+ * outstanding per fileproc f_iocount() picked up during the selcount().
+ *
+ * Parameters: p                       Process performing the select
+ *             ibits                   Input bit bector of fd's
+ *             nfd                     Number of fd's
+ *             lim                     Limit to number of vector entries to
+ *                                             consider, or -1 for "all"
+ *             inselect                True if
+ *             need_wakeup             Pointer to flag to set to do a wakeup
+ *                                     if f_iocont on any descriptor goes to 0
+ *
+ * Returns:    0                       Success
+ *             EBADF                   One or more fds in the bit vector
+ *                                             were invalid, but the rest
+ *                                             were successfully dropped
+ *
+ * Notes:      An fd make become bad while the proc_fdlock() is not held,
+ *             if a multithreaded application closes the fd out from under
+ *             the in progress select.  In this case, we still have to
+ *             clean up after the set up on the remaining fds.
+ */
 static int
-seldrop(p, ibits, nfd)
-       struct proc *p;
-       u_int32_t *ibits;
-       int nfd;
+seldrop_locked(struct proc *p, u_int32_t *ibits, int nfd, int lim, int *need_wakeup, int fromselcount)
 {
-       register struct filedesc *fdp = p->p_fd;
-       register int msk, i, j, fd;
-       register u_int32_t bits;
+       struct filedesc *fdp = p->p_fd;
+       int msk, i, j, nc, fd;
+       u_int32_t bits;
        struct fileproc *fp;
-       int n = 0;
        u_int32_t *iptr;
        u_int nw;
+       int error = 0;
+       int dropcount = 0;
+       uthread_t uth = get_bsdthread_info(current_thread());
+       struct _select_data *seldata;
+
+       *need_wakeup = 0;
 
        /*
         * Problems when reboot; due to MacOSX signal probs
@@ -1589,9 +2009,9 @@ seldrop(p, ibits, nfd)
        }
 
        nw = howmany(nfd, NFDBITS);
+       seldata = &uth->uu_kevent.ss_select_data;
 
-
-       proc_fdlock(p);
+       nc = 0;
        for (msk = 0; msk < 3; msk++) {
                iptr = (u_int32_t *)&ibits[msk * nw];
                for (i = 0; i < nfd; i += NFDBITS) {
@@ -1599,72 +2019,134 @@ seldrop(p, ibits, nfd)
                        while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
                                bits &= ~(1 << j);
                                fp = fdp->fd_ofiles[fd];
-                               if (fp == NULL 
-#if 0
-                       /* if you are here then it is being closed */
-                                       || (fdp->fd_ofileflags[fd] & UF_RESERVED)
-#endif
-                                       ) {
-                                               proc_fdunlock(p);
-                                               return(EBADF);
+                               /*
+                                * If we've already dropped as many as were
+                                * counted/scanned, then we are done.  
+                                */
+                               if ((fromselcount != 0) && (++dropcount > lim))
+                                       goto done;
+
+                               /*
+                                * unlink even potentially NULL fileprocs.
+                                * If the FD was closed from under us, we
+                                * still need to cleanup the waitq links!
+                                */
+                               selunlinkfp(fp,
+                                           seldata->wqp ? seldata->wqp[nc] : 0,
+                                           uth->uu_wqset);
+
+                               nc++;
+
+                               if (fp == NULL) {
+                                       /* skip (now) bad fds */
+                                       error = EBADF;
+                                       continue;
                                }
-                               n++;
-                               fp->f_iocount--;
-                               fp->f_flags &= ~FP_INSELECT;
 
-                               if (p->p_fpdrainwait && fp->f_iocount == 0) {
-                                       p->p_fpdrainwait = 0;
-                                       wakeup(&p->p_fpdrainwait);
+                               fp->f_iocount--;
+                               if (fp->f_iocount < 0)
+                                       panic("f_iocount overdecrement!");
+
+                               if (fp->f_iocount == 0) {
+                                       /*
+                                        * The last iocount is responsible for clearing
+                                        * selconfict flag - even if we didn't set it -
+                                        * and is also responsible for waking up anyone
+                                        * waiting on iocounts to drain.
+                                        */
+                                       if (fp->f_flags & FP_SELCONFLICT)
+                                               fp->f_flags &= ~FP_SELCONFLICT;
+                                       if (p->p_fpdrainwait) {
+                                               p->p_fpdrainwait = 0;
+                                               *need_wakeup = 1;
+                                       }
                                }
                        }
                }
        }
+done:
+       return (error);
+}
+
+
+static int
+seldrop(struct proc *p, u_int32_t *ibits, int nfd)
+{
+       int error;
+       int need_wakeup = 0;
+
+       proc_fdlock(p);
+       error =  seldrop_locked(p, ibits, nfd, nfd, &need_wakeup, 0);
        proc_fdunlock(p);
-       return (0);
+       if (need_wakeup) {
+               wakeup(&p->p_fpdrainwait);
+       }
+       return (error);
 }
 
 /*
  * Record a select request.
  */
 void
-selrecord(__unused struct proc *selector, struct selinfo *sip, void * p_wql)
+selrecord(__unused struct proc *selector, struct selinfo *sip, void *s_data)
 {
        thread_t        cur_act = current_thread();
        struct uthread * ut = get_bsdthread_info(cur_act);
+       /* on input, s_data points to the 64-bit ID of a reserved link object */
+       uint64_t *reserved_link = (uint64_t *)s_data;
 
        /* need to look at collisions */
 
-       if ((p_wql == (void *)0) && ((sip->si_flags & SI_INITED) == 0)) {
-               return;
-       }
-
        /*do not record if this is second pass of select */
-       if((p_wql == (void *)0)) {
+       if (!s_data)
                return;
-       }
 
        if ((sip->si_flags & SI_INITED) == 0) {
-               wait_queue_init(&sip->si_wait_queue, SYNC_POLICY_FIFO);
+               waitq_init(&sip->si_waitq, SYNC_POLICY_FIFO);
                sip->si_flags |= SI_INITED;
                sip->si_flags &= ~SI_CLEAR;
        }
 
-       if (sip->si_flags & SI_RECORDED) {
+       if (sip->si_flags & SI_RECORDED)
                sip->si_flags |= SI_COLL;
-       else
+       else
                sip->si_flags &= ~SI_COLL;
 
        sip->si_flags |= SI_RECORDED;
-       if (!wait_queue_member(&sip->si_wait_queue, ut->uu_select.wqset))
-               wait_queue_link_noalloc(&sip->si_wait_queue, ut->uu_select.wqset,
-                                       (wait_queue_link_t)p_wql);
+       /* note: this checks for pre-existing linkage */
+       waitq_link(&sip->si_waitq, ut->uu_wqset,
+                  WAITQ_SHOULD_LOCK, reserved_link);
+
+       /*
+        * Always consume the reserved link.
+        * We can always call waitq_link_release() safely because if
+        * waitq_link is successful, it consumes the link and resets the
+        * value to 0, in which case our call to release becomes a no-op.
+        * If waitq_link fails, then the following release call will actually
+        * release the reserved link object.
+        */
+       waitq_link_release(*reserved_link);
+       *reserved_link = 0;
+
+       /*
+        * Use the s_data pointer as an output parameter as well
+        * This avoids changing the prototype for this function which is
+        * used by many kexts. We need to surface the waitq object
+        * associated with the selinfo we just added to the thread's select
+        * set. New waitq sets do not have back-pointers to set members, so
+        * the only way to clear out set linkage objects is to go from the
+        * waitq to the set. We use a memcpy because s_data could be
+        * pointing to an unaligned value on the stack
+        * (especially on 32-bit systems)
+        */
+       void *wqptr = (void *)&sip->si_waitq;
+       memcpy((void *)s_data, (void *)&wqptr, sizeof(void *));
 
        return;
 }
 
 void
-selwakeup(sip)
-       register struct selinfo *sip;
+selwakeup(struct selinfo *sip)
 {
        
        if ((sip->si_flags & SI_INITED) == 0) {
@@ -1681,16 +2163,17 @@ selwakeup(sip)
        }
 
        if (sip->si_flags & SI_RECORDED) {
-               wait_queue_wakeup_all(&sip->si_wait_queue, &selwait, THREAD_AWAKENED);
+               waitq_wakeup64_all(&sip->si_waitq, NO_EVENT64,
+                                  THREAD_AWAKENED, WAITQ_ALL_PRIORITIES);
                sip->si_flags &= ~SI_RECORDED;
        }
 
 }
 
 void 
-selthreadclear(sip)
-       register struct selinfo *sip;
+selthreadclear(struct selinfo *sip)
 {
+       struct waitq *wq;
 
        if ((sip->si_flags & SI_INITED) == 0) {
                return;
@@ -1700,7 +2183,18 @@ selthreadclear(sip)
                        sip->si_flags &= ~(SI_RECORDED | SI_COLL);
        }
        sip->si_flags |= SI_CLEAR;
-       wait_queue_unlinkall_nofree(&sip->si_wait_queue);
+       sip->si_flags &= ~SI_INITED;
+
+       wq = &sip->si_waitq;
+
+       /*
+        * Higher level logic may have a handle on this waitq's prepost ID,
+        * but that's OK because the waitq_deinit will remove/invalidate the
+        * prepost object (as well as mark the waitq invalid). This de-couples
+        * us from any callers that may have a handle to this waitq via the
+        * prepost ID.
+        */
+       waitq_deinit(wq);
 }
 
 
@@ -1795,7 +2289,7 @@ evprocenque(struct eventqelt *evq)
        assert(evq);
        p = evq->ee_proc;
 
-       KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, evq, evq->ee_flags, evq->ee_eventmask,0,0);
+       KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, (uint32_t)evq, evq->ee_flags, evq->ee_eventmask,0,0);
 
        proc_lock(p);
 
@@ -1845,14 +2339,14 @@ postpipeevent(struct pipe *pipep, int event)
                          evq->ee_req.er_rcnt = pipep->pipe_buffer.cnt;
                  }
                  if ((evq->ee_eventmask & EV_WR) && 
-                     (pipep->pipe_buffer.size - pipep->pipe_buffer.cnt) >= PIPE_BUF) {
+                     (MAX(pipep->pipe_buffer.size,PIPE_SIZE) - pipep->pipe_buffer.cnt) >= PIPE_BUF) {
 
                          if (pipep->pipe_state & PIPE_EOF) {
                                  mask |= EV_WR|EV_RESET;
                                  break;
                          }
                          mask |= EV_WR;
-                         evq->ee_req.er_wcnt = pipep->pipe_buffer.size - pipep->pipe_buffer.cnt;
+                         evq->ee_req.er_wcnt = MAX(pipep->pipe_buffer.size, PIPE_SIZE) - pipep->pipe_buffer.cnt;
                  }
                  break;
 
@@ -1887,7 +2381,7 @@ postpipeevent(struct pipe *pipep, int event)
                         */
                        evq->ee_req.er_eventbits |= mask;
 
-                       KERNEL_DEBUG(DBG_MISC_POST, evq, evq->ee_req.er_eventbits, mask, 1,0);
+                       KERNEL_DEBUG(DBG_MISC_POST, (uint32_t)evq, evq->ee_req.er_eventbits, mask, 1,0);
 
                        evprocenque(evq);
                }
@@ -1895,7 +2389,7 @@ postpipeevent(struct pipe *pipep, int event)
        KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,1,0);
 }
 
-
+#if SOCKETS
 /*
  * given either a sockbuf or a socket run down the
  * event list and queue ready events found...
@@ -1958,13 +2452,19 @@ postevent(struct socket *sp, struct sockbuf *sb, int event)
                   */
                case EV_RWBYTES:
                  if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
-                         if (sp->so_error) {
-                                 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
-                                         if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
-                                             (tp->t_state == TCPS_CLOSED)) {
-                                                 mask |= EV_RE|EV_RESET;
-                                                 break;
-                                         }
+                         /* for AFP/OT purposes; may go away in future */
+                         if ((SOCK_DOM(sp) == PF_INET ||
+                             SOCK_DOM(sp) == PF_INET6) &&
+                             SOCK_PROTO(sp) == IPPROTO_TCP &&
+                             (sp->so_error == ECONNREFUSED ||
+                             sp->so_error == ECONNRESET)) {
+                                 if (sp->so_pcb == NULL ||
+                                     sotoinpcb(sp)->inp_state ==
+                                     INPCB_STATE_DEAD ||
+                                     (tp = sototcpcb(sp)) == NULL ||
+                                     tp->t_state == TCPS_CLOSED) {
+                                         mask |= EV_RE|EV_RESET;
+                                         break;
                                  }
                          }
                          mask |= EV_RE;
@@ -1976,13 +2476,19 @@ postevent(struct socket *sp, struct sockbuf *sb, int event)
                          }
                  }
                  if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
-                         if (sp->so_error) {
-                                 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
-                                         if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
-                                             (tp->t_state == TCPS_CLOSED)) {
-                                                 mask |= EV_WR|EV_RESET;
-                                                 break;
-                                         }
+                         /* for AFP/OT purposes; may go away in future */
+                         if ((SOCK_DOM(sp) == PF_INET ||
+                             SOCK_DOM(sp) == PF_INET6) &&
+                             SOCK_PROTO(sp) == IPPROTO_TCP &&
+                             (sp->so_error == ECONNREFUSED ||
+                             sp->so_error == ECONNRESET)) {
+                                 if (sp->so_pcb == NULL ||
+                                     sotoinpcb(sp)->inp_state ==
+                                     INPCB_STATE_DEAD ||
+                                     (tp = sototcpcb(sp)) == NULL ||
+                                     tp->t_state == TCPS_CLOSED) {
+                                         mask |= EV_WR|EV_RESET;
+                                         break;
                                  }
                          }
                          mask |= EV_WR;
@@ -2062,6 +2568,7 @@ postevent(struct socket *sp, struct sockbuf *sb, int event)
        }
        KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, 0, 0, 0, 0);
 }
+#endif /* SOCKETS */
 
 
 /*
@@ -2072,13 +2579,17 @@ postevent(struct socket *sp, struct sockbuf *sb, int event)
  * via waitevent().
  *
  * should this prevent duplicate events on same socket?
+ *
+ * Returns:
+ *             ENOMEM                  No memory for operation
+ *     copyin:EFAULT
  */
 int
 watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
 {
        struct eventqelt *evq = (struct eventqelt *)0;
        struct eventqelt *np = NULL;
-       struct eventreq *erp;
+       struct eventreq64 *erp;
        struct fileproc *fp = NULL;
        int error;
 
@@ -2088,18 +2599,36 @@ watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
        MALLOC(evq, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
 
        if (evq == NULL)
-               panic("can't MALLOC evq");
+               return (ENOMEM);
        erp = &evq->ee_req;
 
        // get users request pkt
-       if ( (error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
-                          sizeof(struct eventreq))) ) {
-               FREE(evq, M_TEMP);
 
+       if (IS_64BIT_PROCESS(p)) {
+               error = copyin(uap->u_req, (caddr_t)erp, sizeof(struct eventreq64));
+       } else {
+               struct eventreq32 er32;
+
+               error = copyin(uap->u_req, (caddr_t)&er32, sizeof(struct eventreq32));
+               if (error == 0) {
+                      /*
+                       * the user only passes in the
+                       * er_type, er_handle and er_data...
+                       * the other fields are initialized
+                       * below, so don't bother to copy
+                       */
+                       erp->er_type = er32.er_type;
+                       erp->er_handle = er32.er_handle;
+                       erp->er_data = (user_addr_t)er32.er_data;
+               }
+       }
+       if (error) {
+               FREE(evq, M_TEMP);
                KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
-               return(error);
+
+               return(error);          
        }
-       KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,evq,0,0);
+       KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,(uint32_t)evq,0,0);
 
        // validate, freeing qelt if errors
        error = 0;
@@ -2109,9 +2638,11 @@ watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
                error = EINVAL;
        } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
                error = EBADF;
+#if SOCKETS
        } else if (fp->f_type == DTYPE_SOCKET) {
                socket_lock((struct socket *)fp->f_data, 1);
                np = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
+#endif /* SOCKETS */
        } else if (fp->f_type == DTYPE_PIPE) {
                PIPE_LOCK((struct pipe *)fp->f_data);
                np = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
@@ -2133,9 +2664,11 @@ watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
         */
        for ( ; np != NULL; np = np->ee_slist.tqe_next) {
                if (np->ee_proc == p) {
+#if SOCKETS
                        if (fp->f_type == DTYPE_SOCKET)
                                socket_unlock((struct socket *)fp->f_data, 1);
                        else 
+#endif /* SOCKETS */
                                PIPE_UNLOCK((struct pipe *)fp->f_data);
                        fp_drop(p, erp->er_handle, fp, 0);
                        FREE(evq, M_TEMP);
@@ -2149,12 +2682,15 @@ watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
        evq->ee_eventmask = uap->u_eventmask & EV_MASK;
        evq->ee_flags = 0;
 
+#if SOCKETS
        if (fp->f_type == DTYPE_SOCKET) {
                TAILQ_INSERT_TAIL(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
                postevent((struct socket *)fp->f_data, 0, EV_RWBYTES); // catch existing events
 
                socket_unlock((struct socket *)fp->f_data, 1);
-       } else {
+       } else
+#endif /* SOCKETS */
+       {
                TAILQ_INSERT_TAIL(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
                postpipeevent((struct pipe *)fp->f_data, EV_RWBYTES);
 
@@ -2172,20 +2708,60 @@ watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
  * waitevent system call.
  * grabs the next waiting event for this proc and returns
  * it. if no events, user can request to sleep with timeout
- * or poll mode (tv=NULL);
+ * or without or poll mode
+ *    ((tv != NULL && interval == 0) || tv == -1)
  */
 int
 waitevent(proc_t p, struct waitevent_args *uap, int *retval)
 {
         int error = 0;
        struct eventqelt *evq;
-       struct eventreq   er;
+       struct eventreq64 *erp;
        uint64_t abstime, interval;
+       boolean_t fast_poll = FALSE;
+       union {
+               struct eventreq64 er64;
+               struct eventreq32 er32;
+       } uer;
+
+       interval = 0;
 
        if (uap->tv) {
                struct timeval atv;
+               /*
+                * check for fast poll method
+                */
+               if (IS_64BIT_PROCESS(p)) {
+                       if (uap->tv == (user_addr_t)-1)
+                               fast_poll = TRUE;
+               } else if (uap->tv == (user_addr_t)((uint32_t)-1))
+                       fast_poll = TRUE;
+
+               if (fast_poll == TRUE) {
+                       if (p->p_evlist.tqh_first == NULL) {
+                               KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_NONE, -1,0,0,0,0);
+                               /*
+                                * poll failed
+                                */
+                               *retval = 1;
+                               return (0);
+                       }
+                       proc_lock(p);
+                       goto retry;
+               }
+               if (IS_64BIT_PROCESS(p)) {
+                       struct user64_timeval atv64;
+                       error = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
+                       /* Loses resolution - assume timeout < 68 years */
+                       atv.tv_sec = atv64.tv_sec;
+                       atv.tv_usec = atv64.tv_usec;
+               } else {
+                       struct user32_timeval atv32;
+                       error = copyin(uap->tv, (caddr_t)&atv32, sizeof(atv32));
+                       atv.tv_sec = atv32.tv_sec;
+                       atv.tv_usec = atv32.tv_usec;
+               }
 
-               error = copyin(CAST_USER_ADDR_T(uap->tv), (caddr_t)&atv, sizeof (atv));
                if (error)
                        return(error);
                if (itimerfix(&atv)) {
@@ -2193,9 +2769,7 @@ waitevent(proc_t p, struct waitevent_args *uap, int *retval)
                        return(error);
                }
                interval = tvtoabstime(&atv);
-       } else
-               interval = 0;
-
+       }
        KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
 
        proc_lock(p);
@@ -2207,18 +2781,32 @@ retry:
                 * don't want to hold the proc lock across a copyout because
                 * it might block on a page fault at the target in user space
                 */
-               bcopy((caddr_t)&evq->ee_req, (caddr_t)&er, sizeof (struct eventreq));
+               erp = &evq->ee_req;
 
+               if (IS_64BIT_PROCESS(p))
+                       bcopy((caddr_t)erp, (caddr_t)&uer.er64, sizeof (struct eventreq64));
+               else {
+                       uer.er32.er_type  = erp->er_type;
+                       uer.er32.er_handle  = erp->er_handle;
+                       uer.er32.er_data  = (uint32_t)erp->er_data;
+                       uer.er32.er_ecnt  = erp->er_ecnt;
+                       uer.er32.er_rcnt  = erp->er_rcnt;
+                       uer.er32.er_wcnt  = erp->er_wcnt;
+                       uer.er32.er_eventbits = erp->er_eventbits;
+               }
                TAILQ_REMOVE(&p->p_evlist, evq, ee_plist);
 
                evq->ee_flags &= ~EV_QUEUED;
 
                proc_unlock(p);
 
-               error = copyout((caddr_t)&er, CAST_USER_ADDR_T(uap->u_req), sizeof(struct eventreq));
+               if (IS_64BIT_PROCESS(p))
+                       error = copyout((caddr_t)&uer.er64, uap->u_req, sizeof(struct eventreq64));
+               else
+                       error = copyout((caddr_t)&uer.er32, uap->u_req, sizeof(struct eventreq32));
 
                KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
-                            evq->ee_req.er_handle,evq->ee_req.er_eventbits,evq,0);
+                            evq->ee_req.er_handle,evq->ee_req.er_eventbits,(uint32_t)evq,0);
                return (error);
        }
        else {
@@ -2234,11 +2822,11 @@ retry:
                else
                        abstime = 0;
 
-               KERNEL_DEBUG(DBG_MISC_WAIT, 1,&p->p_evlist,0,0,0);
+               KERNEL_DEBUG(DBG_MISC_WAIT, 1,(uint32_t)&p->p_evlist,0,0,0);
 
                error = msleep1(&p->p_evlist, &p->p_mlock, (PSOCK | PCATCH), "waitevent", abstime);
 
-               KERNEL_DEBUG(DBG_MISC_WAIT, 2,&p->p_evlist,0,0,0);
+               KERNEL_DEBUG(DBG_MISC_WAIT, 2,(uint32_t)&p->p_evlist,0,0,0);
 
                if (error == 0)
                        goto retry;
@@ -2264,9 +2852,9 @@ retry:
 int
 modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
 {
-       struct eventreq er;
-       struct eventreq *erp = &er;
-       struct eventqelt *evq;
+       struct eventreq64 er;
+       struct eventreq64 *erp = &er;
+       struct eventqelt *evq = NULL;   /* protected by error return */
        int error;
        struct fileproc *fp;
        int flag;
@@ -2275,10 +2863,12 @@ modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
 
        /*
         * get user's request pkt
+        * just need the er_type and er_handle which sit above the
+        * problematic er_data (32/64 issue)... so only copy in
+        * those 2 fields
         */
-       if ((error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
-                            sizeof(struct eventreq)))) {
-                       KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
+       if ((error = copyin(uap->u_req, (caddr_t)erp, sizeof(er.er_type) + sizeof(er.er_handle)))) {
+               KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
                return(error);
        }
        proc_fdlock(p);
@@ -2287,9 +2877,11 @@ modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
                error = EINVAL;
        } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
                error = EBADF;
+#if SOCKETS
        } else if (fp->f_type == DTYPE_SOCKET) {
                socket_lock((struct socket *)fp->f_data, 1);
                evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
+#endif /* SOCKETS */
        } else if (fp->f_type == DTYPE_PIPE) {
                PIPE_LOCK((struct pipe *)fp->f_data);
                evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
@@ -2315,23 +2907,28 @@ modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
                        break;
        }
        if (evq == NULL) {
+#if SOCKETS
                if (fp->f_type == DTYPE_SOCKET) 
                        socket_unlock((struct socket *)fp->f_data, 1);
-               else 
+               else
+#endif /* SOCKETS */
                        PIPE_UNLOCK((struct pipe *)fp->f_data);
                fp_drop(p, erp->er_handle, fp, 0);
                KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
                return(EINVAL);
        }
-       KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,evq,0,0);
+       KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,(uint32_t)evq,0,0);
 
        if (uap->u_eventmask == EV_RM) {
                EVPROCDEQUE(p, evq);
 
+#if SOCKETS
                if (fp->f_type == DTYPE_SOCKET) {
                        TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
                        socket_unlock((struct socket *)fp->f_data, 1);
-               } else {
+               } else
+#endif /* SOCKETS */
+               {
                        TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
                        PIPE_UNLOCK((struct pipe *)fp->f_data);
                }
@@ -2363,9 +2960,11 @@ modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
                break;
 
        default:
+#if SOCKETS
                if (fp->f_type == DTYPE_SOCKET) 
                        socket_unlock((struct socket *)fp->f_data, 1);
                else 
+#endif /* SOCKETS */
                        PIPE_UNLOCK((struct pipe *)fp->f_data);
                fp_drop(p, erp->er_handle, fp, 0);
                KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
@@ -2394,16 +2993,18 @@ modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
        evq->ee_req.er_eventbits = 0;
        evq->ee_eventmask = uap->u_eventmask & EV_MASK;
 
+#if SOCKETS
        if (fp->f_type == DTYPE_SOCKET) {
                postevent((struct socket *)fp->f_data, 0, flag);
                socket_unlock((struct socket *)fp->f_data, 1);
-       }
-       else {
+       } else
+#endif /* SOCKETS */
+       {
                postpipeevent((struct pipe *)fp->f_data, flag);
                PIPE_UNLOCK((struct pipe *)fp->f_data);
        }
        fp_drop(p, erp->er_handle, fp, 0);
-       KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,fp->f_data,flag,0);
+       KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,(uint32_t)fp->f_data,flag,0);
        return(0);
 }
 
@@ -2416,11 +3017,13 @@ waitevent_close(struct proc *p, struct fileproc *fp)
 
        fp->f_flags &= ~FP_WAITEVENT;
 
+#if SOCKETS
        if (fp->f_type == DTYPE_SOCKET) {
                socket_lock((struct socket *)fp->f_data, 1);
                evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
-       }
-       else if (fp->f_type == DTYPE_PIPE) {
+       } else
+#endif /* SOCKETS */
+       if (fp->f_type == DTYPE_PIPE) {
                PIPE_LOCK((struct pipe *)fp->f_data);
                evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
        }
@@ -2436,9 +3039,11 @@ waitevent_close(struct proc *p, struct fileproc *fp)
                        break;
        }
        if (evq == NULL) {
+#if SOCKETS
                if (fp->f_type == DTYPE_SOCKET) 
                        socket_unlock((struct socket *)fp->f_data, 1);
                else 
+#endif /* SOCKETS */
                        PIPE_UNLOCK((struct pipe *)fp->f_data);
 
                proc_fdlock(p);
@@ -2447,10 +3052,13 @@ waitevent_close(struct proc *p, struct fileproc *fp)
        }
        EVPROCDEQUE(p, evq);
 
+#if SOCKETS
        if (fp->f_type == DTYPE_SOCKET) {
                TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
                socket_unlock((struct socket *)fp->f_data, 1);
-       } else {
+       } else
+#endif /* SOCKETS */
+       {
                TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
                PIPE_UNLOCK((struct pipe *)fp->f_data);
        }
@@ -2461,3 +3069,587 @@ waitevent_close(struct proc *p, struct fileproc *fp)
        return(0);
 }
 
+
+/*
+ * gethostuuid
+ *
+ * Description:        Get the host UUID from IOKit and return it to user space.
+ *
+ * Parameters: uuid_buf                Pointer to buffer to receive UUID
+ *             timeout                 Timespec for timout
+ *             spi                             SPI, skip sandbox check (temporary)
+ *
+ * Returns:    0                       Success
+ *             EWOULDBLOCK             Timeout is too short
+ *             copyout:EFAULT          Bad user buffer
+ *             mac_system_check_info:EPERM             Client not allowed to perform this operation
+ *
+ * Notes:      A timeout seems redundant, since if it's tolerable to not
+ *             have a system UUID in hand, then why ask for one?
+ */
+int
+gethostuuid(struct proc *p, struct gethostuuid_args *uap, __unused int32_t *retval)
+{
+       kern_return_t kret;
+       int error;
+       mach_timespec_t mach_ts;        /* for IOKit call */
+       __darwin_uuid_t uuid_kern;      /* for IOKit call */
+
+       if (!uap->spi) {
+       }
+
+       /* Convert the 32/64 bit timespec into a mach_timespec_t */
+       if ( proc_is64bit(p) ) {
+               struct user64_timespec ts;
+               error = copyin(uap->timeoutp, &ts, sizeof(ts));
+               if (error)
+                       return (error);
+               mach_ts.tv_sec = ts.tv_sec;
+               mach_ts.tv_nsec = ts.tv_nsec;
+       } else {
+               struct user32_timespec ts;
+               error = copyin(uap->timeoutp, &ts, sizeof(ts) );
+               if (error)
+                       return (error);
+               mach_ts.tv_sec = ts.tv_sec;
+               mach_ts.tv_nsec = ts.tv_nsec;
+       }
+
+       /* Call IOKit with the stack buffer to get the UUID */
+       kret = IOBSDGetPlatformUUID(uuid_kern, mach_ts);
+
+       /*
+        * If we get it, copy out the data to the user buffer; note that a
+        * uuid_t is an array of characters, so this is size invariant for
+        * 32 vs. 64 bit.
+        */
+       if (kret == KERN_SUCCESS) {
+               error = copyout(uuid_kern, uap->uuid_buf, sizeof(uuid_kern));
+       } else {
+               error = EWOULDBLOCK;
+       }
+
+       return (error);
+}
+
+/*
+ * ledger
+ *
+ * Description:        Omnibus system call for ledger operations
+ */
+int
+ledger(struct proc *p, struct ledger_args *args, __unused int32_t *retval)
+{
+#if !CONFIG_MACF
+#pragma unused(p)
+#endif
+       int rval, pid, len, error;
+#ifdef LEDGER_DEBUG
+       struct ledger_limit_args lla;
+#endif
+       task_t task;
+       proc_t proc;
+
+       /* Finish copying in the necessary args before taking the proc lock */
+       error = 0;
+       len = 0;
+       if (args->cmd == LEDGER_ENTRY_INFO)
+               error = copyin(args->arg3, (char *)&len, sizeof (len));
+       else if (args->cmd == LEDGER_TEMPLATE_INFO)
+               error = copyin(args->arg2, (char *)&len, sizeof (len));
+#ifdef LEDGER_DEBUG
+       else if (args->cmd == LEDGER_LIMIT)
+               error = copyin(args->arg2, (char *)&lla, sizeof (lla));
+#endif
+       else if ((args->cmd < 0) || (args->cmd > LEDGER_MAX_CMD))
+               return (EINVAL);
+
+       if (error)
+               return (error);
+       if (len < 0)
+               return (EINVAL);
+
+       rval = 0;
+       if (args->cmd != LEDGER_TEMPLATE_INFO) {
+               pid = args->arg1;
+               proc = proc_find(pid);
+               if (proc == NULL)
+                       return (ESRCH);
+
+#if CONFIG_MACF
+               error = mac_proc_check_ledger(p, proc, args->cmd);
+               if (error) {
+                       proc_rele(proc);
+                       return (error);
+               }
+#endif
+
+               task = proc->task;
+       }
+               
+       switch (args->cmd) {
+#ifdef LEDGER_DEBUG
+               case LEDGER_LIMIT: {
+                       if (!kauth_cred_issuser(kauth_cred_get()))
+                               rval = EPERM;
+                       rval = ledger_limit(task, &lla);
+                       proc_rele(proc);
+                       break;
+               }
+#endif
+               case LEDGER_INFO: {
+                       struct ledger_info info;
+
+                       rval = ledger_info(task, &info);
+                       proc_rele(proc);
+                       if (rval == 0)
+                               rval = copyout(&info, args->arg2,
+                                   sizeof (info));
+                       break;
+               }
+
+               case LEDGER_ENTRY_INFO: {
+                       void *buf;
+                       int sz;
+
+                       rval = ledger_get_task_entry_info_multiple(task, &buf, &len);
+                       proc_rele(proc);
+                       if ((rval == 0) && (len >= 0)) {
+                               sz = len * sizeof (struct ledger_entry_info);
+                               rval = copyout(buf, args->arg2, sz);
+                               kfree(buf, sz);
+                       }
+                       if (rval == 0)
+                               rval = copyout(&len, args->arg3, sizeof (len));
+                       break;
+               }
+
+               case LEDGER_TEMPLATE_INFO: {
+                       void *buf;
+                       int sz;
+
+                       rval = ledger_template_info(&buf, &len);
+                       if ((rval == 0) && (len >= 0)) {
+                               sz = len * sizeof (struct ledger_template_info);
+                               rval = copyout(buf, args->arg1, sz);
+                               kfree(buf, sz);
+                       }
+                       if (rval == 0)
+                               rval = copyout(&len, args->arg2, sizeof (len));
+                       break;
+               }
+
+               default:
+                       panic("ledger syscall logic error -- command type %d", args->cmd);
+                       proc_rele(proc);
+                       rval = EINVAL;
+       }
+
+       return (rval);
+}
+
+int
+telemetry(__unused struct proc *p, struct telemetry_args *args, __unused int32_t *retval)
+{
+       int error = 0;
+
+       switch (args->cmd) {
+#if CONFIG_TELEMETRY
+       case TELEMETRY_CMD_TIMER_EVENT:
+               error = telemetry_timer_event(args->deadline, args->interval, args->leeway);
+               break;
+#endif /* CONFIG_TELEMETRY */
+       case TELEMETRY_CMD_VOUCHER_NAME:
+               if (thread_set_voucher_name((mach_port_name_t)args->deadline))
+                       error = EINVAL;
+               break;
+
+       default:
+               error = EINVAL;
+               break;
+       }
+
+       return (error);
+}
+
+#if defined(DEVELOPMENT) || defined(DEBUG)
+#if CONFIG_WAITQ_DEBUG
+static uint64_t g_wqset_num = 0;
+struct g_wqset {
+       queue_chain_t      link;
+       struct waitq_set  *wqset;
+};
+
+static queue_head_t         g_wqset_list;
+static struct waitq_set    *g_waitq_set = NULL;
+
+static inline struct waitq_set *sysctl_get_wqset(int idx)
+{
+       struct g_wqset *gwqs;
+
+       if (!g_wqset_num)
+               queue_init(&g_wqset_list);
+
+       /* don't bother with locks: this is test-only code! */
+       qe_foreach_element(gwqs, &g_wqset_list, link) {
+               if ((int)(wqset_id(gwqs->wqset) & 0xffffffff) == idx)
+                       return gwqs->wqset;
+       }
+
+       /* allocate a new one */
+       ++g_wqset_num;
+       gwqs = (struct g_wqset *)kalloc(sizeof(*gwqs));
+       assert(gwqs != NULL);
+
+       gwqs->wqset = waitq_set_alloc(SYNC_POLICY_FIFO|SYNC_POLICY_PREPOST, NULL);
+       enqueue_tail(&g_wqset_list, &gwqs->link);
+       printf("[WQ]: created new waitq set 0x%llx\n", wqset_id(gwqs->wqset));
+
+       return gwqs->wqset;
+}
+
+#define MAX_GLOBAL_TEST_QUEUES 64
+static int g_wq_init = 0;
+static struct waitq  g_wq[MAX_GLOBAL_TEST_QUEUES];
+
+static inline struct waitq *global_test_waitq(int idx)
+{
+       if (idx < 0)
+               return NULL;
+
+       if (!g_wq_init) {
+               g_wq_init = 1;
+               for (int i = 0; i < MAX_GLOBAL_TEST_QUEUES; i++)
+                       waitq_init(&g_wq[i], SYNC_POLICY_FIFO);
+       }
+
+       return &g_wq[idx % MAX_GLOBAL_TEST_QUEUES];
+}
+
+static int sysctl_waitq_wakeup_one SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       int index;
+       struct waitq *waitq;
+       kern_return_t kr;
+       int64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       if (event64 < 0) {
+               index = (int)((-event64) & 0xffffffff);
+               waitq = wqset_waitq(sysctl_get_wqset(index));
+               index = -index;
+       } else {
+               index = (int)event64;
+               waitq = global_test_waitq(index);
+       }
+
+       event64 = 0;
+
+       printf("[WQ]: Waking one thread on waitq [%d] event:0x%llx\n",
+              index, event64);
+       kr = waitq_wakeup64_one(waitq, (event64_t)event64, THREAD_AWAKENED,
+                               WAITQ_ALL_PRIORITIES);
+       printf("[WQ]: \tkr=%d\n", kr);
+
+       return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_wakeup_one, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_waitq_wakeup_one, "Q", "wakeup one thread waiting on given event");
+
+
+static int sysctl_waitq_wakeup_all SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       int index;
+       struct waitq *waitq;
+       kern_return_t kr;
+       int64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       if (event64 < 0) {
+               index = (int)((-event64) & 0xffffffff);
+               waitq = wqset_waitq(sysctl_get_wqset(index));
+               index = -index;
+       } else {
+               index = (int)event64;
+               waitq = global_test_waitq(index);
+       }
+
+       event64 = 0;
+
+       printf("[WQ]: Waking all threads on waitq [%d] event:0x%llx\n",
+              index, event64);
+       kr = waitq_wakeup64_all(waitq, (event64_t)event64,
+                               THREAD_AWAKENED, WAITQ_ALL_PRIORITIES);
+       printf("[WQ]: \tkr=%d\n", kr);
+
+       return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_wakeup_all, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_waitq_wakeup_all, "Q", "wakeup all threads waiting on given event");
+
+
+static int sysctl_waitq_wait SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       int index;
+       struct waitq *waitq;
+       kern_return_t kr;
+       int64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       if (event64 < 0) {
+               index = (int)((-event64) & 0xffffffff);
+               waitq = wqset_waitq(sysctl_get_wqset(index));
+               index = -index;
+       } else {
+               index = (int)event64;
+               waitq = global_test_waitq(index);
+       }
+
+       event64 = 0;
+
+       printf("[WQ]: Current thread waiting on waitq [%d] event:0x%llx\n",
+              index, event64);
+       kr = waitq_assert_wait64(waitq, (event64_t)event64, THREAD_INTERRUPTIBLE, 0);
+       if (kr == THREAD_WAITING)
+               thread_block(THREAD_CONTINUE_NULL);
+       printf("[WQ]: \tWoke Up: kr=%d\n", kr);
+
+       return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_wait, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_waitq_wait, "Q", "start waiting on given event");
+
+
+static int sysctl_wqset_select SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       struct waitq_set *wqset;
+       uint64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               goto out;
+
+       wqset = sysctl_get_wqset((int)(event64 & 0xffffffff));
+       g_waitq_set = wqset;
+
+       event64 = wqset_id(wqset);
+       printf("[WQ]: selected wqset 0x%llx\n", event64);
+
+out:
+       if (g_waitq_set)
+               event64 = wqset_id(g_waitq_set);
+       else
+               event64 = (uint64_t)(-1);
+
+       return SYSCTL_OUT(req, &event64, sizeof(event64));
+}
+SYSCTL_PROC(_kern, OID_AUTO, wqset_select, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_wqset_select, "Q", "select/create a global waitq set");
+
+
+static int sysctl_waitq_link SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       int index;
+       struct waitq *waitq;
+       struct waitq_set *wqset;
+       kern_return_t kr;
+       uint64_t reserved_link = 0;
+       int64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       if (!g_waitq_set)
+               g_waitq_set = sysctl_get_wqset(1);
+       wqset = g_waitq_set;
+
+       if (event64 < 0) {
+               struct waitq_set *tmp;
+               index = (int)((-event64) & 0xffffffff);
+               tmp = sysctl_get_wqset(index);
+               if (tmp == wqset)
+                       goto out;
+               waitq = wqset_waitq(tmp);
+               index = -index;
+       } else {
+               index = (int)event64;
+               waitq = global_test_waitq(index);
+       }
+
+       printf("[WQ]: linking waitq [%d] to global wqset (0x%llx)\n",
+              index, wqset_id(wqset));
+       reserved_link = waitq_link_reserve(waitq);
+       kr = waitq_link(waitq, wqset, WAITQ_SHOULD_LOCK, &reserved_link);
+       waitq_link_release(reserved_link);
+
+       printf("[WQ]: \tkr=%d\n", kr);
+
+out:
+       return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_link, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_waitq_link, "Q", "link global waitq to test waitq set");
+
+
+static int sysctl_waitq_unlink SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       int index;
+       struct waitq *waitq;
+       struct waitq_set *wqset;
+       kern_return_t kr;
+       uint64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       if (!g_waitq_set)
+               g_waitq_set = sysctl_get_wqset(1);
+       wqset = g_waitq_set;
+
+       index = (int)event64;
+       waitq = global_test_waitq(index);
+
+       printf("[WQ]: unlinking waitq [%d] from global wqset (0x%llx)\n",
+              index, wqset_id(wqset));
+
+       kr = waitq_unlink(waitq, wqset);
+       printf("[WQ]: \tkr=%d\n", kr);
+
+       return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_unlink, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_waitq_unlink, "Q", "unlink global waitq from test waitq set");
+
+
+static int sysctl_waitq_clear_prepost SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       struct waitq *waitq;
+       uint64_t event64 = 0;
+       int error, index;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       index = (int)event64;
+       waitq = global_test_waitq(index);
+
+       printf("[WQ]: clearing prepost on waitq [%d]\n", index);
+       waitq_clear_prepost(waitq);
+
+       return SYSCTL_OUT(req, &event64, sizeof(event64));
+}
+SYSCTL_PROC(_kern, OID_AUTO, waitq_clear_prepost, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_waitq_clear_prepost, "Q", "clear prepost on given waitq");
+
+
+static int sysctl_wqset_unlink_all SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       int error;
+       struct waitq_set *wqset;
+       kern_return_t kr;
+       uint64_t event64 = 0;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               return SYSCTL_OUT(req, &event64, sizeof(event64));
+
+       if (!g_waitq_set)
+               g_waitq_set = sysctl_get_wqset(1);
+       wqset = g_waitq_set;
+
+       printf("[WQ]: unlinking all queues from global wqset (0x%llx)\n",
+              wqset_id(wqset));
+
+       kr = waitq_set_unlink_all(wqset);
+       printf("[WQ]: \tkr=%d\n", kr);
+
+       return SYSCTL_OUT(req, &kr, sizeof(kr));
+}
+SYSCTL_PROC(_kern, OID_AUTO, wqset_unlink_all, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_wqset_unlink_all, "Q", "unlink all queues from test waitq set");
+
+
+static int sysctl_wqset_clear_preposts SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+       struct waitq_set *wqset = NULL;
+       uint64_t event64 = 0;
+       int error, index;
+
+       error = SYSCTL_IN(req, &event64, sizeof(event64));
+       if (error)
+               return error;
+
+       if (!req->newptr)
+               goto out;
+
+       index = (int)((event64) & 0xffffffff);
+       wqset = sysctl_get_wqset(index);
+       assert(wqset != NULL);
+
+       printf("[WQ]: clearing preposts on wqset 0x%llx\n", wqset_id(wqset));
+       waitq_set_clear_preposts(wqset);
+
+out:
+       if (wqset)
+               event64 = wqset_id(wqset);
+       else
+               event64 = (uint64_t)(-1);
+
+       return SYSCTL_OUT(req, &event64, sizeof(event64));
+}
+SYSCTL_PROC(_kern, OID_AUTO, wqset_clear_preposts, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+           0, 0, sysctl_wqset_clear_preposts, "Q", "clear preposts on given waitq set");
+
+#endif /* CONFIG_WAITQ_DEBUG */
+#endif /* defined(DEVELOPMENT) || defined(DEBUG) */