2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/ioctl.h>
79 #include <sys/file_internal.h>
80 #include <sys/proc_internal.h>
81 #include <sys/socketvar.h>
82 #include <sys/uio_internal.h>
83 #include <sys/kernel.h>
84 #include <sys/guarded.h>
86 #include <sys/malloc.h>
87 #include <sys/sysproto.h>
89 #include <sys/mount_internal.h>
90 #include <sys/protosw.h>
93 #include <sys/kdebug.h>
95 #include <sys/event.h>
96 #include <sys/eventvar.h>
98 #include <sys/kauth.h>
100 #include <machine/smp.h>
101 #include <mach/mach_types.h>
102 #include <kern/kern_types.h>
103 #include <kern/assert.h>
104 #include <kern/kalloc.h>
105 #include <kern/thread.h>
106 #include <kern/clock.h>
107 #include <kern/ledger.h>
108 #include <kern/task.h>
109 #include <kern/telemetry.h>
110 #include <kern/waitq.h>
111 #include <kern/sched_prim.h>
113 #include <sys/mbuf.h>
114 #include <sys/domain.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/errno.h>
118 #include <sys/syscall.h>
119 #include <sys/pipe.h>
121 #include <security/audit/audit.h>
124 #include <net/route.h>
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/in_pcb.h>
130 #include <netinet/ip_var.h>
131 #include <netinet/ip6.h>
132 #include <netinet/tcp.h>
133 #include <netinet/tcp_fsm.h>
134 #include <netinet/tcp_seq.h>
135 #include <netinet/tcp_timer.h>
136 #include <netinet/tcp_var.h>
137 #include <netinet/tcpip.h>
138 #include <netinet/tcp_debug.h>
139 /* for wait queue based select */
140 #include <kern/waitq.h>
141 #include <kern/kalloc.h>
142 #include <sys/vnode_internal.h>
145 #include <security/mac_framework.h>
148 /* XXX should be in a header file somewhere */
149 void evsofree(struct socket
*);
150 void evpipefree(struct pipe
*);
151 void postpipeevent(struct pipe
*, int);
152 void postevent(struct socket
*, struct sockbuf
*, int);
153 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
155 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
156 int wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
);
158 __private_extern__
int dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
159 user_addr_t bufp
, user_size_t nbyte
,
160 off_t offset
, int flags
, user_ssize_t
*retval
);
161 __private_extern__
int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
162 user_addr_t bufp
, user_size_t nbyte
,
163 off_t offset
, int flags
, user_ssize_t
*retval
);
164 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
165 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
167 /* Conflict wait queue for when selects collide (opaque type) */
168 struct waitq select_conflict_queue
;
171 * Init routine called from bsd_init.c
173 void select_waitq_init(void);
175 select_waitq_init(void)
177 waitq_init(&select_conflict_queue
, SYNC_POLICY_FIFO
);
180 #define f_flag f_fglob->fg_flag
181 #define f_type f_fglob->fg_ops->fo_type
182 #define f_msgcount f_fglob->fg_msgcount
183 #define f_cred f_fglob->fg_cred
184 #define f_ops f_fglob->fg_ops
185 #define f_offset f_fglob->fg_offset
186 #define f_data f_fglob->fg_data
192 * preparefileread:EBADF
193 * preparefileread:ESPIPE
194 * preparefileread:ENXIO
195 * preparefileread:EBADF
199 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
201 __pthread_testcancel(1);
202 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
206 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
211 struct vfs_context context
;
213 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
216 context
= *(vfs_context_current());
217 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
219 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
220 (off_t
)-1, 0, retval
);
222 donefileread(p
, fp
, fd
);
231 * preparefileread:EBADF
232 * preparefileread:ESPIPE
233 * preparefileread:ENXIO
234 * preparefileread:EBADF
238 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
240 __pthread_testcancel(1);
241 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
245 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
247 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
250 struct vfs_context context
;
252 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
255 context
= *(vfs_context_current());
256 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
258 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
259 uap
->offset
, FOF_OFFSET
, retval
);
261 donefileread(p
, fp
, fd
);
263 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
264 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
271 * Code common for read and pread
275 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
278 fp_drop(p
, fd
, fp
, 1);
291 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
301 error
= fp_lookup(p
, fd
, &fp
, 1);
307 if ((fp
->f_flag
& FREAD
) == 0) {
311 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
315 if (fp
->f_type
== DTYPE_VNODE
) {
316 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
318 if (check_for_pread
&& (vnode_isfifo(vp
))) {
322 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
334 fp_drop(p
, fd
, fp
, 1);
345 __private_extern__
int
346 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
347 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
348 user_ssize_t
*retval
)
351 user_ssize_t bytecnt
;
353 char uio_buf
[ UIO_SIZEOF(1) ];
358 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
359 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
360 &uio_buf
[0], sizeof(uio_buf
));
362 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
363 &uio_buf
[0], sizeof(uio_buf
));
365 uio_addiov(auio
, bufp
, nbyte
);
369 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
370 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
371 error
== EINTR
|| error
== EWOULDBLOCK
))
374 bytecnt
-= uio_resid(auio
);
382 * Scatter read system call.
391 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
393 __pthread_testcancel(1);
394 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
398 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
402 struct user_iovec
*iovp
;
404 /* Verify range bedfore calling uio_create() */
405 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
408 /* allocate a uio large enough to hold the number of iovecs passed */
409 auio
= uio_create(uap
->iovcnt
, 0,
410 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
413 /* get location of iovecs within the uio. then copyin the iovecs from
416 iovp
= uio_iovsaddr(auio
);
419 goto ExitThisRoutine
;
421 error
= copyin_user_iovec_array(uap
->iovp
,
422 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
425 goto ExitThisRoutine
;
428 /* finalize uio_t for use and do the IO
430 error
= uio_calculateresid(auio
);
432 goto ExitThisRoutine
;
434 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
452 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
454 __pthread_testcancel(1);
455 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
460 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
465 bool wrote_some
= false;
469 error
= fp_lookup(p
,fd
,&fp
,0);
472 if ((fp
->f_flag
& FWRITE
) == 0) {
474 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
476 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
479 struct vfs_context context
= *(vfs_context_current());
480 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
482 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
483 (off_t
)-1, 0, retval
);
485 wrote_some
= *retval
> 0;
488 fp_drop_written(p
, fd
, fp
);
490 fp_drop(p
, fd
, fp
, 0);
506 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
508 __pthread_testcancel(1);
509 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
513 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
518 vnode_t vp
= (vnode_t
)0;
519 bool wrote_some
= false;
523 error
= fp_lookup(p
,fd
,&fp
,0);
527 if ((fp
->f_flag
& FWRITE
) == 0) {
529 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
531 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
534 struct vfs_context context
= *vfs_context_current();
535 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
537 if (fp
->f_type
!= DTYPE_VNODE
) {
541 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
542 if (vnode_isfifo(vp
)) {
546 if ((vp
->v_flag
& VISTTY
)) {
550 if (uap
->offset
== (off_t
)-1) {
555 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
556 uap
->offset
, FOF_OFFSET
, retval
);
557 wrote_some
= *retval
> 0;
561 fp_drop_written(p
, fd
, fp
);
563 fp_drop(p
, fd
, fp
, 0);
565 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
566 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
575 * <fo_write>:??? [indirect through struct fileops]
577 __private_extern__
int
578 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
579 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
580 user_ssize_t
*retval
)
584 user_ssize_t bytecnt
;
585 char uio_buf
[ UIO_SIZEOF(1) ];
587 if (nbyte
> INT_MAX
) {
592 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
593 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
594 &uio_buf
[0], sizeof(uio_buf
));
596 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
597 &uio_buf
[0], sizeof(uio_buf
));
599 uio_addiov(auio
, bufp
, nbyte
);
602 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
603 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
604 error
== EINTR
|| error
== EWOULDBLOCK
))
606 /* The socket layer handles SIGPIPE */
607 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
608 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0) {
609 /* XXX Raise the signal on the thread? */
610 psignal(vfs_context_proc(ctx
), SIGPIPE
);
613 bytecnt
-= uio_resid(auio
);
620 * Gather write system call
623 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
625 __pthread_testcancel(1);
626 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
630 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
635 struct user_iovec
*iovp
;
636 bool wrote_some
= false;
638 AUDIT_ARG(fd
, uap
->fd
);
640 /* Verify range bedfore calling uio_create() */
641 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
644 /* allocate a uio large enough to hold the number of iovecs passed */
645 auio
= uio_create(uap
->iovcnt
, 0,
646 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
649 /* get location of iovecs within the uio. then copyin the iovecs from
652 iovp
= uio_iovsaddr(auio
);
655 goto ExitThisRoutine
;
657 error
= copyin_user_iovec_array(uap
->iovp
,
658 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
661 goto ExitThisRoutine
;
664 /* finalize uio_t for use and do the IO
666 error
= uio_calculateresid(auio
);
668 goto ExitThisRoutine
;
671 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
673 goto ExitThisRoutine
;
675 if ((fp
->f_flag
& FWRITE
) == 0) {
677 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
679 error
= fp_guard_exception(p
, uap
->fd
, fp
, kGUARD_EXC_WRITE
);
682 error
= wr_uio(p
, fp
, auio
, retval
);
683 wrote_some
= *retval
> 0;
687 fp_drop_written(p
, uap
->fd
, fp
);
689 fp_drop(p
, uap
->fd
, fp
, 0);
700 wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
)
704 struct vfs_context context
= *vfs_context_current();
706 count
= uio_resid(uio
);
708 context
.vc_ucred
= fp
->f_cred
;
709 error
= fo_write(fp
, uio
, 0, &context
);
711 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
712 error
== EINTR
|| error
== EWOULDBLOCK
))
714 /* The socket layer handles SIGPIPE */
715 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
716 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0)
719 *retval
= count
- uio_resid(uio
);
726 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
731 struct vfs_context context
= *vfs_context_current();
733 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
736 count
= uio_resid(uio
);
738 context
.vc_ucred
= fp
->f_cred
;
740 error
= fo_read(fp
, uio
, 0, &context
);
743 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
744 error
== EINTR
|| error
== EWOULDBLOCK
))
747 *retval
= count
- uio_resid(uio
);
749 donefileread(p
, fp
, fdes
);
764 * fp_lookup:EBADF Bad file descriptor
768 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
770 struct fileproc
*fp
= NULL
;
773 caddr_t datap
= NULL
, memp
= NULL
;
774 boolean_t is64bit
= FALSE
;
776 #define STK_PARAMS 128
777 char stkbuf
[STK_PARAMS
] = {};
779 u_long com
= uap
->com
;
780 struct vfs_context context
= *vfs_context_current();
782 AUDIT_ARG(fd
, uap
->fd
);
783 AUDIT_ARG(addr
, uap
->data
);
785 is64bit
= proc_is64bit(p
);
788 AUDIT_ARG(value64
, com
);
790 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, com
));
791 #endif /* CONFIG_AUDIT */
794 * Interpret high order word to find amount of data to be
795 * copied to/from the user's address space.
797 size
= IOCPARM_LEN(com
);
798 if (size
> IOCPARM_MAX
)
800 if (size
> sizeof (stkbuf
)) {
801 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
808 error
= copyin(uap
->data
, datap
, size
);
812 /* XXX - IOC_IN and no size? we should proably return an error here!! */
814 *(user_addr_t
*)datap
= uap
->data
;
817 *(uint32_t *)datap
= (uint32_t)uap
->data
;
820 } else if ((com
& IOC_OUT
) && size
)
822 * Zero the buffer so the user always
823 * gets back something deterministic.
826 else if (com
& IOC_VOID
) {
827 /* XXX - this is odd since IOC_VOID means no parameters */
829 *(user_addr_t
*)datap
= uap
->data
;
832 *(uint32_t *)datap
= (uint32_t)uap
->data
;
837 error
= fp_lookup(p
,fd
,&fp
,1);
843 AUDIT_ARG(file
, p
, fp
);
845 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
850 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
853 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, com
);
860 *fdflags(p
, fd
) &= ~UF_EXCLOSE
;
864 *fdflags(p
, fd
) |= UF_EXCLOSE
;
868 if ( (tmp
= *(int *)datap
) )
869 fp
->f_flag
|= FNONBLOCK
;
871 fp
->f_flag
&= ~FNONBLOCK
;
872 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
876 if ( (tmp
= *(int *)datap
) )
877 fp
->f_flag
|= FASYNC
;
879 fp
->f_flag
&= ~FASYNC
;
880 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
885 if (fp
->f_type
== DTYPE_SOCKET
) {
886 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
889 if (fp
->f_type
== DTYPE_PIPE
) {
890 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
896 struct proc
*p1
= proc_find(tmp
);
904 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
908 if (fp
->f_type
== DTYPE_SOCKET
) {
909 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
912 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
913 *(int *)datap
= -*(int *)datap
;
917 error
= fo_ioctl(fp
, com
, datap
, &context
);
919 * Copy any data to user, size was
920 * already set and checked above.
922 if (error
== 0 && (com
& IOC_OUT
) && size
)
923 error
= copyout(datap
, uap
->data
, (u_int
)size
);
927 fp_drop(p
, fd
, fp
, 1);
936 int selwait
, nselcoll
;
937 #define SEL_FIRSTPASS 1
938 #define SEL_SECONDPASS 2
939 extern int selcontinue(int error
);
940 extern int selprocess(int error
, int sel_pass
);
941 static int selscan(struct proc
*p
, struct _select
* sel
, struct _select_data
* seldata
,
942 int nfd
, int32_t *retval
, int sel_pass
, struct waitq_set
*wqset
);
943 static int selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *count
);
944 static int seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
);
945 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
946 static int select_internal(struct proc
*p
, struct select_nocancel_args
*uap
, uint64_t timeout
, int32_t *retval
);
949 * Select system call.
952 * EINVAL Invalid argument
953 * EAGAIN Nonconformant error if allocation fails
956 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
958 __pthread_testcancel(1);
959 return select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
);
963 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
965 uint64_t timeout
= 0;
970 if (IS_64BIT_PROCESS(p
)) {
971 struct user64_timeval atv64
;
972 err
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
973 /* Loses resolution - assume timeout < 68 years */
974 atv
.tv_sec
= atv64
.tv_sec
;
975 atv
.tv_usec
= atv64
.tv_usec
;
977 struct user32_timeval atv32
;
978 err
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
979 atv
.tv_sec
= atv32
.tv_sec
;
980 atv
.tv_usec
= atv32
.tv_usec
;
985 if (itimerfix(&atv
)) {
990 clock_absolutetime_interval_to_deadline(tvtoabstime(&atv
), &timeout
);
993 return select_internal(p
, uap
, timeout
, retval
);
997 pselect(struct proc
*p
, struct pselect_args
*uap
, int32_t *retval
)
999 __pthread_testcancel(1);
1000 return pselect_nocancel(p
, (struct pselect_nocancel_args
*)uap
, retval
);
1004 pselect_nocancel(struct proc
*p
, struct pselect_nocancel_args
*uap
, int32_t *retval
)
1008 uint64_t timeout
= 0;
1013 if (IS_64BIT_PROCESS(p
)) {
1014 struct user64_timespec ts64
;
1015 err
= copyin(uap
->ts
, (caddr_t
)&ts64
, sizeof(ts64
));
1016 ts
.tv_sec
= ts64
.tv_sec
;
1017 ts
.tv_nsec
= ts64
.tv_nsec
;
1019 struct user32_timespec ts32
;
1020 err
= copyin(uap
->ts
, (caddr_t
)&ts32
, sizeof(ts32
));
1021 ts
.tv_sec
= ts32
.tv_sec
;
1022 ts
.tv_nsec
= ts32
.tv_nsec
;
1028 if (!timespec_is_valid(&ts
)) {
1031 clock_absolutetime_interval_to_deadline(tstoabstime(&ts
), &timeout
);
1034 ut
= get_bsdthread_info(current_thread());
1036 if (uap
->mask
!= USER_ADDR_NULL
) {
1037 /* save current mask, then copyin and set new mask */
1039 err
= copyin(uap
->mask
, &newset
, sizeof(sigset_t
));
1043 ut
->uu_oldmask
= ut
->uu_sigmask
;
1044 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1045 ut
->uu_sigmask
= (newset
& ~sigcantmask
);
1048 err
= select_internal(p
, (struct select_nocancel_args
*)uap
, timeout
, retval
);
1050 if (err
!= EINTR
&& ut
->uu_flag
& UT_SAS_OLDMASK
) {
1052 * Restore old mask (direct return case). NOTE: EINTR can also be returned
1053 * if the thread is cancelled. In that case, we don't reset the signal
1054 * mask to its original value (which usually happens in the signal
1055 * delivery path). This behavior is permitted by POSIX.
1057 ut
->uu_sigmask
= ut
->uu_oldmask
;
1059 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1066 * Generic implementation of {,p}select. Care: we type-pun uap across the two
1067 * syscalls, which differ slightly. The first 4 arguments (nfds and the fd sets)
1068 * are identical. The 5th (timeout) argument points to different types, so we
1069 * unpack in the syscall-specific code, but the generic code still does a null
1070 * check on this argument to determine if a timeout was specified.
1073 select_internal(struct proc
*p
, struct select_nocancel_args
*uap
, uint64_t timeout
, int32_t *retval
)
1078 struct uthread
*uth
;
1079 struct _select
*sel
;
1080 struct _select_data
*seldata
;
1081 int needzerofill
= 1;
1085 th_act
= current_thread();
1086 uth
= get_bsdthread_info(th_act
);
1087 sel
= &uth
->uu_select
;
1088 seldata
= &uth
->uu_save
.uus_select_data
;
1091 seldata
->args
= uap
;
1092 seldata
->retval
= retval
;
1093 seldata
->wqp
= NULL
;
1100 /* select on thread of process that already called proc_exit() */
1101 if (p
->p_fd
== NULL
) {
1105 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
1106 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
1108 nw
= howmany(uap
->nd
, NFDBITS
);
1109 ni
= nw
* sizeof(fd_mask
);
1112 * if the previously allocated space for the bits is smaller than
1113 * what is requested or no space has yet been allocated for this
1114 * thread, allocate enough space now.
1116 * Note: If this process fails, select() will return EAGAIN; this
1117 * is the same thing pool() returns in a no-memory situation, but
1118 * it is not a POSIX compliant error code for select().
1120 if (sel
->nbytes
< (3 * ni
)) {
1121 int nbytes
= 3 * ni
;
1123 /* Free previous allocation, if any */
1124 if (sel
->ibits
!= NULL
)
1125 FREE(sel
->ibits
, M_TEMP
);
1126 if (sel
->obits
!= NULL
) {
1127 FREE(sel
->obits
, M_TEMP
);
1128 /* NULL out; subsequent ibits allocation may fail */
1132 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1133 if (sel
->ibits
== NULL
)
1135 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1136 if (sel
->obits
== NULL
) {
1137 FREE(sel
->ibits
, M_TEMP
);
1141 sel
->nbytes
= nbytes
;
1146 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1147 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1151 * get the bits from the user address space
1153 #define getbits(name, x) \
1155 if (uap->name && (error = copyin(uap->name, \
1156 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1157 goto continuation; \
1165 seldata
->abstime
= timeout
;
1167 if ( (error
= selcount(p
, sel
->ibits
, uap
->nd
, &count
)) ) {
1172 * We need an array of waitq pointers. This is due to the new way
1173 * in which waitqs are linked to sets. When a thread selects on a
1174 * file descriptor, a waitq (embedded in a selinfo structure) is
1175 * added to the thread's local waitq set. There is no longer any
1176 * way to directly iterate over all members of a given waitq set.
1177 * The process of linking a waitq into a set may allocate a link
1178 * table object. Because we can't iterate over all the waitqs to
1179 * which our thread waitq set belongs, we need a way of removing
1182 * Thus we need a buffer which will hold one waitq pointer
1183 * per FD being selected. During the tear-down phase we can use
1184 * these pointers to dis-associate the underlying selinfo's waitq
1185 * from our thread's waitq set.
1187 * Because we also need to allocate a waitq set for this thread,
1188 * we use a bare buffer pointer to hold all the memory. Note that
1189 * this memory is cached in the thread pointer and not reaped until
1190 * the thread exists. This is generally OK because threads that
1191 * call select tend to keep calling select repeatedly.
1193 sz
= ALIGN(sizeof(struct waitq_set
)) + (count
* sizeof(uint64_t));
1194 if (sz
> uth
->uu_wqstate_sz
) {
1195 /* (re)allocate a buffer to hold waitq pointers */
1196 if (uth
->uu_wqset
) {
1197 if (waitq_set_is_valid(uth
->uu_wqset
))
1198 waitq_set_deinit(uth
->uu_wqset
);
1199 FREE(uth
->uu_wqset
, M_SELECT
);
1200 } else if (uth
->uu_wqstate_sz
&& !uth
->uu_wqset
)
1201 panic("select: thread structure corrupt! "
1202 "uu_wqstate_sz:%ld, wqstate_buf == NULL",
1203 uth
->uu_wqstate_sz
);
1204 uth
->uu_wqstate_sz
= sz
;
1205 MALLOC(uth
->uu_wqset
, struct waitq_set
*, sz
, M_SELECT
, M_WAITOK
);
1207 panic("can't allocate %ld bytes for wqstate buffer",
1208 uth
->uu_wqstate_sz
);
1209 waitq_set_init(uth
->uu_wqset
,
1210 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
, NULL
, NULL
);
1213 if (!waitq_set_is_valid(uth
->uu_wqset
))
1214 waitq_set_init(uth
->uu_wqset
,
1215 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
, NULL
, NULL
);
1217 /* the last chunk of our buffer is an array of waitq pointers */
1218 seldata
->wqp
= (uint64_t *)((char *)(uth
->uu_wqset
) + ALIGN(sizeof(struct waitq_set
)));
1219 bzero(seldata
->wqp
, sz
- ALIGN(sizeof(struct waitq_set
)));
1221 seldata
->count
= count
;
1227 * We have already cleaned up any state we established,
1228 * either locally or as a result of selcount(). We don't
1229 * need to wait_subqueue_unlink_all(), since we haven't set
1230 * anything at this point.
1235 return selprocess(0, SEL_FIRSTPASS
);
1239 selcontinue(int error
)
1241 return selprocess(error
, SEL_SECONDPASS
);
1248 * Parameters: error The error code from our caller
1249 * sel_pass The pass we are on
1252 selprocess(int error
, int sel_pass
)
1257 struct uthread
*uth
;
1259 struct select_nocancel_args
*uap
;
1261 struct _select
*sel
;
1262 struct _select_data
*seldata
;
1267 wait_result_t wait_result
;
1270 th_act
= current_thread();
1271 uth
= get_bsdthread_info(th_act
);
1272 sel
= &uth
->uu_select
;
1273 seldata
= &uth
->uu_save
.uus_select_data
;
1274 uap
= seldata
->args
;
1275 retval
= seldata
->retval
;
1277 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1279 if (seldata
->count
== 0)
1286 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1288 /* skip scans if the select is just for timeouts */
1289 if (seldata
->count
) {
1290 error
= selscan(p
, sel
, seldata
, uap
->nd
, retval
, sel_pass
, uth
->uu_wqset
);
1291 if (error
|| *retval
) {
1294 if (prepost
|| somewakeup
) {
1296 * if the select of log, then we can wakeup and
1297 * discover some one else already read the data;
1298 * go to select again if time permits
1309 clock_get_uptime(&now
);
1310 if (now
>= seldata
->abstime
)
1315 /* cleanup obits and try again */
1317 sel_pass
= SEL_FIRSTPASS
;
1322 * To effect a poll, the timeout argument should be
1323 * non-nil, pointing to a zero-valued timeval structure.
1325 if (uap
->tv
&& seldata
->abstime
== 0) {
1329 /* No spurious wakeups due to colls,no need to check for them */
1330 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1331 sel_pass
= SEL_FIRSTPASS
;
1335 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1337 /* if the select is just for timeout skip check */
1338 if (seldata
->count
&& (sel_pass
== SEL_SECONDPASS
))
1339 panic("selprocess: 2nd pass assertwaiting");
1341 /* waitq_set has waitqueue as first element */
1342 wait_result
= waitq_assert_wait64_leeway((struct waitq
*)uth
->uu_wqset
,
1343 NO_EVENT64
, THREAD_ABORTSAFE
,
1344 TIMEOUT_URGENCY_USER_NORMAL
,
1347 if (wait_result
!= THREAD_AWAKENED
) {
1348 /* there are no preposted events */
1349 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1350 "select", 0, selcontinue
);
1357 sel_pass
= SEL_SECONDPASS
;
1364 seldrop(p
, sel
->ibits
, uap
->nd
);
1365 waitq_set_deinit(uth
->uu_wqset
);
1367 * zero out the waitq pointer array to avoid use-after free
1368 * errors in the selcount error path (seldrop_locked) if/when
1369 * the thread re-calls select().
1371 bzero((void *)uth
->uu_wqset
, uth
->uu_wqstate_sz
);
1373 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1374 /* select is not restarted after signals... */
1375 if (error
== ERESTART
)
1377 if (error
== EWOULDBLOCK
)
1379 nw
= howmany(uap
->nd
, NFDBITS
);
1380 ni
= nw
* sizeof(fd_mask
);
1382 #define putbits(name, x) \
1384 if (uap->name && (error2 = \
1385 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1398 if (error
!= EINTR
&& sel_pass
== SEL_SECONDPASS
&& uth
->uu_flag
& UT_SAS_OLDMASK
) {
1399 /* restore signal mask - continuation case */
1400 uth
->uu_sigmask
= uth
->uu_oldmask
;
1401 uth
->uu_oldmask
= 0;
1402 uth
->uu_flag
&= ~UT_SAS_OLDMASK
;
1410 * remove the fileproc's underlying waitq from the supplied waitq set;
1411 * clear FP_INSELECT when appropriate
1414 * fp File proc that is potentially currently in select
1415 * wqset Waitq set to which the fileproc may belong
1416 * (usually this is the thread's private waitq set)
1418 * proc_fdlock is held
1420 static void selunlinkfp(struct fileproc
*fp
, uint64_t wqp_id
, struct waitq_set
*wqset
)
1422 int valid_set
= waitq_set_is_valid(wqset
);
1423 int valid_q
= !!wqp_id
;
1426 * This could be called (from selcount error path) before we setup
1427 * the thread's wqset. Check the wqset passed in, and only unlink if
1431 /* unlink the underlying waitq from the input set (thread waitq set) */
1432 if (valid_q
&& valid_set
)
1433 waitq_unlink_by_prepost_id(wqp_id
, wqset
);
1435 /* allow passing a NULL/invalid fp for seldrop unwind */
1436 if (!fp
|| !(fp
->f_flags
& (FP_INSELECT
|FP_SELCONFLICT
)))
1440 * We can always remove the conflict queue from our thread's set: this
1441 * will not affect other threads that potentially need to be awoken on
1442 * the conflict queue during a fileproc_drain - those sets will still
1443 * be linked with the global conflict queue, and the last waiter
1444 * on the fp clears the CONFLICT marker.
1446 if (valid_set
&& (fp
->f_flags
& FP_SELCONFLICT
))
1447 waitq_unlink(&select_conflict_queue
, wqset
);
1450 * This isn't quite right - we don't actually know if this
1451 * fileproc is in another select or not! Here we just assume
1452 * that if we were the first thread to select on the FD, then
1453 * we'll be the one to clear this flag...
1455 if (valid_set
&& fp
->f_wset
== (void *)wqset
) {
1456 fp
->f_flags
&= ~FP_INSELECT
;
1462 * connect a fileproc to the given wqset, potentially bridging to a waitq
1463 * pointed to indirectly by wq_data
1466 * fp File proc potentially currently in select
1467 * wq_data Pointer to a pointer to a waitq (could be NULL)
1468 * wqset Waitq set to which the fileproc should now belong
1469 * (usually this is the thread's private waitq set)
1472 * proc_fdlock is held
1474 static uint64_t sellinkfp(struct fileproc
*fp
, void **wq_data
, struct waitq_set
*wqset
)
1476 struct waitq
*f_wq
= NULL
;
1478 if ((fp
->f_flags
& FP_INSELECT
) != FP_INSELECT
) {
1480 panic("non-null data:%p on fp:%p not in select?!"
1481 "(wqset:%p)", wq_data
, fp
, wqset
);
1485 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
1486 waitq_link(&select_conflict_queue
, wqset
, WAITQ_SHOULD_LOCK
, NULL
);
1490 * The wq_data parameter has potentially been set by selrecord called
1491 * from a subsystems fo_select() function. If the subsystem does not
1492 * call selrecord, then wq_data will be NULL
1494 * Use memcpy to get the value into a proper pointer because
1495 * wq_data most likely points to a stack variable that could be
1496 * unaligned on 32-bit systems.
1499 memcpy(&f_wq
, wq_data
, sizeof(f_wq
));
1500 if (!waitq_is_valid(f_wq
))
1504 /* record the first thread's wqset in the fileproc structure */
1506 fp
->f_wset
= (void *)wqset
;
1508 /* handles NULL f_wq */
1509 return waitq_get_prepost_id(f_wq
);
1516 * Parameters: p Process performing the select
1517 * sel The per-thread select context structure
1518 * nfd The number of file descriptors to scan
1519 * retval The per thread system call return area
1520 * sel_pass Which pass this is; allowed values are
1521 * SEL_FIRSTPASS and SEL_SECONDPASS
1522 * wqset The per thread wait queue set
1524 * Returns: 0 Success
1525 * EIO Invalid p->p_fd field XXX Obsolete?
1526 * EBADF One of the files in the bit vector is
1530 selscan(struct proc
*p
, struct _select
*sel
, struct _select_data
* seldata
,
1531 int nfd
, int32_t *retval
, int sel_pass
, struct waitq_set
*wqset
)
1533 struct filedesc
*fdp
= p
->p_fd
;
1536 struct fileproc
*fp
;
1537 int n
= 0; /* count of bits */
1538 int nc
= 0; /* bit vector offset (nc'th bit) */
1539 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1540 u_int32_t
*iptr
, *optr
;
1542 u_int32_t
*ibits
, *obits
;
1543 uint64_t reserved_link
, *rl_ptr
= NULL
;
1545 struct vfs_context context
= *vfs_context_current();
1548 * Problems when reboot; due to MacOSX signal probs
1549 * in Beaker1C ; verify that the p->p_fd is valid
1558 nw
= howmany(nfd
, NFDBITS
);
1560 count
= seldata
->count
;
1569 for (msk
= 0; msk
< 3; msk
++) {
1570 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1571 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1573 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1574 bits
= iptr
[i
/NFDBITS
];
1576 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1579 if (fd
< fdp
->fd_nfiles
)
1580 fp
= fdp
->fd_ofiles
[fd
];
1584 if (fp
== NULL
|| (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1586 * If we abort because of a bad
1587 * fd, let the caller unwind...
1592 if (sel_pass
== SEL_SECONDPASS
) {
1595 selunlinkfp(fp
, seldata
->wqp
[nc
], wqset
);
1597 reserved_link
= waitq_link_reserve((struct waitq
*)wqset
);
1598 rl_ptr
= &reserved_link
;
1599 if (fp
->f_flags
& FP_INSELECT
)
1600 /* someone is already in select on this fp */
1601 fp
->f_flags
|= FP_SELCONFLICT
;
1603 fp
->f_flags
|= FP_INSELECT
;
1605 waitq_set_lazy_init_link(wqset
);
1608 context
.vc_ucred
= fp
->f_cred
;
1611 * stash this value b/c fo_select may replace
1612 * reserved_link with a pointer to a waitq object
1614 uint64_t rsvd
= reserved_link
;
1616 /* The select; set the bit, if true */
1617 if (fp
->f_ops
&& fp
->f_type
1618 && fo_select(fp
, flag
[msk
], rl_ptr
, &context
)) {
1619 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1622 if (sel_pass
== SEL_FIRSTPASS
) {
1623 waitq_link_release(rsvd
);
1625 * If the fp's supporting selinfo structure was linked
1626 * to this thread's waitq set, then 'reserved_link'
1627 * will have been updated by selrecord to be a pointer
1628 * to the selinfo's waitq.
1630 if (reserved_link
== rsvd
)
1631 rl_ptr
= NULL
; /* fo_select never called selrecord() */
1633 * Hook up the thread's waitq set either to
1634 * the fileproc structure, or to the global
1635 * conflict queue: but only on the first
1638 seldata
->wqp
[nc
] = sellinkfp(fp
, (void **)rl_ptr
, wqset
);
1650 int poll_callback(struct kqueue
*, struct kevent_internal_s
*, void *);
1652 struct poll_continue_args
{
1653 user_addr_t pca_fds
;
1659 poll(struct proc
*p
, struct poll_args
*uap
, int32_t *retval
)
1661 __pthread_testcancel(1);
1662 return(poll_nocancel(p
, (struct poll_nocancel_args
*)uap
, retval
));
1667 poll_nocancel(struct proc
*p
, struct poll_nocancel_args
*uap
, int32_t *retval
)
1669 struct poll_continue_args
*cont
;
1673 int ncoll
, error
= 0;
1674 u_int nfds
= uap
->nfds
;
1680 * This is kinda bogus. We have fd limits, but that is not
1681 * really related to the size of the pollfd array. Make sure
1682 * we let the process use at least FD_SETSIZE entries and at
1683 * least enough for the current limits. We want to be reasonably
1684 * safe, but not overly restrictive.
1686 if (nfds
> OPEN_MAX
||
1687 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& (proc_suser(p
) || nfds
> FD_SETSIZE
)))
1690 kq
= kqueue_alloc(p
, 0);
1694 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1695 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1701 fds
= (struct pollfd
*)&cont
[1];
1702 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1706 if (uap
->timeout
!= -1) {
1709 atv
.tv_sec
= uap
->timeout
/ 1000;
1710 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1711 if (itimerfix(&atv
)) {
1715 getmicrouptime(&rtv
);
1716 timevaladd(&atv
, &rtv
);
1722 /* JMM - all this P_SELECT stuff is bogus */
1724 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1725 for (i
= 0; i
< nfds
; i
++) {
1726 short events
= fds
[i
].events
;
1727 KNOTE_LOCK_CTX(knlc
);
1728 __assert_only
int rc
;
1730 /* per spec, ignore fd values below zero */
1731 if (fds
[i
].fd
< 0) {
1736 /* convert the poll event into a kqueue kevent */
1737 struct kevent_internal_s kev
= {
1739 .flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
,
1740 .udata
= CAST_USER_ADDR_T(&fds
[i
]) };
1742 /* Handle input events */
1743 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
| POLLHUP
)) {
1744 kev
.filter
= EVFILT_READ
;
1745 if (events
& ( POLLPRI
| POLLRDBAND
))
1746 kev
.flags
|= EV_OOBAND
;
1747 rc
= kevent_register(kq
, &kev
, &knlc
);
1748 assert((rc
& FILTER_REGISTER_WAIT
) == 0);
1751 /* Handle output events */
1752 if ((kev
.flags
& EV_ERROR
) == 0 &&
1753 (events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
))) {
1754 kev
.filter
= EVFILT_WRITE
;
1755 rc
= kevent_register(kq
, &kev
, &knlc
);
1756 assert((rc
& FILTER_REGISTER_WAIT
) == 0);
1759 /* Handle BSD extension vnode events */
1760 if ((kev
.flags
& EV_ERROR
) == 0 &&
1761 (events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
))) {
1762 kev
.filter
= EVFILT_VNODE
;
1764 if (events
& POLLEXTEND
)
1765 kev
.fflags
|= NOTE_EXTEND
;
1766 if (events
& POLLATTRIB
)
1767 kev
.fflags
|= NOTE_ATTRIB
;
1768 if (events
& POLLNLINK
)
1769 kev
.fflags
|= NOTE_LINK
;
1770 if (events
& POLLWRITE
)
1771 kev
.fflags
|= NOTE_WRITE
;
1772 rc
= kevent_register(kq
, &kev
, &knlc
);
1773 assert((rc
& FILTER_REGISTER_WAIT
) == 0);
1776 if (kev
.flags
& EV_ERROR
) {
1777 fds
[i
].revents
= POLLNVAL
;
1784 * Did we have any trouble registering?
1785 * If user space passed 0 FDs, then respect any timeout value passed.
1786 * This is an extremely inefficient sleep. If user space passed one or
1787 * more FDs, and we had trouble registering _all_ of them, then bail
1788 * out. If a subset of the provided FDs failed to register, then we
1789 * will still call the kqueue_scan function.
1791 if (nfds
&& (rfds
== nfds
))
1795 * If any events have trouble registering, an event has fired and we
1796 * shouldn't wait for events in kqueue_scan -- use the current time as
1800 getmicrouptime(&atv
);
1802 /* scan for, and possibly wait for, the kevents to trigger */
1803 cont
->pca_fds
= uap
->fds
;
1804 cont
->pca_nfds
= nfds
;
1805 cont
->pca_rfds
= rfds
;
1806 error
= kqueue_scan(kq
, poll_callback
, NULL
, cont
, NULL
, &atv
, p
);
1807 rfds
= cont
->pca_rfds
;
1810 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1811 /* poll is not restarted after signals... */
1812 if (error
== ERESTART
)
1814 if (error
== EWOULDBLOCK
)
1817 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1830 poll_callback(__unused
struct kqueue
*kq
, struct kevent_internal_s
*kevp
, void *data
)
1832 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1833 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1834 short prev_revents
= fds
->revents
;
1837 /* convert the results back into revents */
1838 if (kevp
->flags
& EV_EOF
)
1839 fds
->revents
|= POLLHUP
;
1840 if (kevp
->flags
& EV_ERROR
)
1841 fds
->revents
|= POLLERR
;
1843 switch (kevp
->filter
) {
1845 if (fds
->revents
& POLLHUP
)
1846 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1848 mask
= (POLLIN
| POLLRDNORM
);
1849 if (kevp
->flags
& EV_OOBAND
)
1850 mask
|= (POLLPRI
| POLLRDBAND
);
1852 fds
->revents
|= (fds
->events
& mask
);
1856 if (!(fds
->revents
& POLLHUP
))
1857 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1861 if (kevp
->fflags
& NOTE_EXTEND
)
1862 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1863 if (kevp
->fflags
& NOTE_ATTRIB
)
1864 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1865 if (kevp
->fflags
& NOTE_LINK
)
1866 fds
->revents
|= (fds
->events
& POLLNLINK
);
1867 if (kevp
->fflags
& NOTE_WRITE
)
1868 fds
->revents
|= (fds
->events
& POLLWRITE
);
1872 if (fds
->revents
!= 0 && prev_revents
== 0)
1879 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1888 * Count the number of bits set in the input bit vector, and establish an
1889 * outstanding fp->f_iocount for each of the descriptors which will be in
1890 * use in the select operation.
1892 * Parameters: p The process doing the select
1893 * ibits The input bit vector
1894 * nfd The number of fd's in the vector
1895 * countp Pointer to where to store the bit count
1897 * Returns: 0 Success
1898 * EIO Bad per process open file table
1899 * EBADF One of the bits in the input bit vector
1900 * references an invalid fd
1902 * Implicit: *countp (modified) Count of fd's
1904 * Notes: This function is the first pass under the proc_fdlock() that
1905 * permits us to recognize invalid descriptors in the bit vector;
1906 * the may, however, not remain valid through the drop and
1907 * later reacquisition of the proc_fdlock().
1910 selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *countp
)
1912 struct filedesc
*fdp
= p
->p_fd
;
1915 struct fileproc
*fp
;
1921 int need_wakeup
= 0;
1924 * Problems when reboot; due to MacOSX signal probs
1925 * in Beaker1C ; verify that the p->p_fd is valid
1931 nw
= howmany(nfd
, NFDBITS
);
1934 for (msk
= 0; msk
< 3; msk
++) {
1935 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1936 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1937 bits
= iptr
[i
/NFDBITS
];
1938 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1941 if (fd
< fdp
->fd_nfiles
)
1942 fp
= fdp
->fd_ofiles
[fd
];
1947 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1967 /* Ignore error return; it's already EBADF */
1968 (void)seldrop_locked(p
, ibits
, nfd
, n
, &need_wakeup
, 1);
1973 wakeup(&p
->p_fpdrainwait
);
1982 * Drop outstanding wait queue references set up during selscan(); drop the
1983 * outstanding per fileproc f_iocount() picked up during the selcount().
1985 * Parameters: p Process performing the select
1986 * ibits Input bit bector of fd's
1987 * nfd Number of fd's
1988 * lim Limit to number of vector entries to
1989 * consider, or -1 for "all"
1991 * need_wakeup Pointer to flag to set to do a wakeup
1992 * if f_iocont on any descriptor goes to 0
1994 * Returns: 0 Success
1995 * EBADF One or more fds in the bit vector
1996 * were invalid, but the rest
1997 * were successfully dropped
1999 * Notes: An fd make become bad while the proc_fdlock() is not held,
2000 * if a multithreaded application closes the fd out from under
2001 * the in progress select. In this case, we still have to
2002 * clean up after the set up on the remaining fds.
2005 seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
)
2007 struct filedesc
*fdp
= p
->p_fd
;
2008 int msk
, i
, j
, nc
, fd
;
2010 struct fileproc
*fp
;
2015 uthread_t uth
= get_bsdthread_info(current_thread());
2016 struct _select_data
*seldata
;
2021 * Problems when reboot; due to MacOSX signal probs
2022 * in Beaker1C ; verify that the p->p_fd is valid
2028 nw
= howmany(nfd
, NFDBITS
);
2029 seldata
= &uth
->uu_save
.uus_select_data
;
2032 for (msk
= 0; msk
< 3; msk
++) {
2033 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
2034 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
2035 bits
= iptr
[i
/NFDBITS
];
2036 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
2038 fp
= fdp
->fd_ofiles
[fd
];
2040 * If we've already dropped as many as were
2041 * counted/scanned, then we are done.
2043 if ((fromselcount
!= 0) && (++dropcount
> lim
))
2047 * unlink even potentially NULL fileprocs.
2048 * If the FD was closed from under us, we
2049 * still need to cleanup the waitq links!
2052 seldata
->wqp
? seldata
->wqp
[nc
] : 0,
2058 /* skip (now) bad fds */
2064 if (fp
->f_iocount
< 0)
2065 panic("f_iocount overdecrement!");
2067 if (fp
->f_iocount
== 0) {
2069 * The last iocount is responsible for clearing
2070 * selconfict flag - even if we didn't set it -
2071 * and is also responsible for waking up anyone
2072 * waiting on iocounts to drain.
2074 if (fp
->f_flags
& FP_SELCONFLICT
)
2075 fp
->f_flags
&= ~FP_SELCONFLICT
;
2076 if (p
->p_fpdrainwait
) {
2077 p
->p_fpdrainwait
= 0;
2090 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
2093 int need_wakeup
= 0;
2096 error
= seldrop_locked(p
, ibits
, nfd
, nfd
, &need_wakeup
, 0);
2099 wakeup(&p
->p_fpdrainwait
);
2105 * Record a select request.
2108 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void *s_data
)
2110 thread_t cur_act
= current_thread();
2111 struct uthread
* ut
= get_bsdthread_info(cur_act
);
2112 /* on input, s_data points to the 64-bit ID of a reserved link object */
2113 uint64_t *reserved_link
= (uint64_t *)s_data
;
2115 /* need to look at collisions */
2117 /*do not record if this is second pass of select */
2121 if ((sip
->si_flags
& SI_INITED
) == 0) {
2122 waitq_init(&sip
->si_waitq
, SYNC_POLICY_FIFO
);
2123 sip
->si_flags
|= SI_INITED
;
2124 sip
->si_flags
&= ~SI_CLEAR
;
2127 if (sip
->si_flags
& SI_RECORDED
)
2128 sip
->si_flags
|= SI_COLL
;
2130 sip
->si_flags
&= ~SI_COLL
;
2132 sip
->si_flags
|= SI_RECORDED
;
2133 /* note: this checks for pre-existing linkage */
2134 waitq_link(&sip
->si_waitq
, ut
->uu_wqset
,
2135 WAITQ_SHOULD_LOCK
, reserved_link
);
2138 * Always consume the reserved link.
2139 * We can always call waitq_link_release() safely because if
2140 * waitq_link is successful, it consumes the link and resets the
2141 * value to 0, in which case our call to release becomes a no-op.
2142 * If waitq_link fails, then the following release call will actually
2143 * release the reserved link object.
2145 waitq_link_release(*reserved_link
);
2149 * Use the s_data pointer as an output parameter as well
2150 * This avoids changing the prototype for this function which is
2151 * used by many kexts. We need to surface the waitq object
2152 * associated with the selinfo we just added to the thread's select
2153 * set. New waitq sets do not have back-pointers to set members, so
2154 * the only way to clear out set linkage objects is to go from the
2155 * waitq to the set. We use a memcpy because s_data could be
2156 * pointing to an unaligned value on the stack
2157 * (especially on 32-bit systems)
2159 void *wqptr
= (void *)&sip
->si_waitq
;
2160 memcpy((void *)s_data
, (void *)&wqptr
, sizeof(void *));
2166 selwakeup(struct selinfo
*sip
)
2169 if ((sip
->si_flags
& SI_INITED
) == 0) {
2173 if (sip
->si_flags
& SI_COLL
) {
2175 sip
->si_flags
&= ~SI_COLL
;
2177 /* will not support */
2178 //wakeup((caddr_t)&selwait);
2182 if (sip
->si_flags
& SI_RECORDED
) {
2183 waitq_wakeup64_all(&sip
->si_waitq
, NO_EVENT64
,
2184 THREAD_AWAKENED
, WAITQ_ALL_PRIORITIES
);
2185 sip
->si_flags
&= ~SI_RECORDED
;
2191 selthreadclear(struct selinfo
*sip
)
2195 if ((sip
->si_flags
& SI_INITED
) == 0) {
2198 if (sip
->si_flags
& SI_RECORDED
) {
2200 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
2202 sip
->si_flags
|= SI_CLEAR
;
2203 sip
->si_flags
&= ~SI_INITED
;
2205 wq
= &sip
->si_waitq
;
2208 * Higher level logic may have a handle on this waitq's prepost ID,
2209 * but that's OK because the waitq_deinit will remove/invalidate the
2210 * prepost object (as well as mark the waitq invalid). This de-couples
2211 * us from any callers that may have a handle to this waitq via the
2220 #define DBG_POST 0x10
2221 #define DBG_WATCH 0x11
2222 #define DBG_WAIT 0x12
2223 #define DBG_MOD 0x13
2224 #define DBG_EWAKEUP 0x14
2225 #define DBG_ENQUEUE 0x15
2226 #define DBG_DEQUEUE 0x16
2228 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
2229 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
2230 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
2231 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
2232 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
2233 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
2234 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
2237 #define EVPROCDEQUE(p, evq) do { \
2239 if (evq->ee_flags & EV_QUEUED) { \
2240 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
2241 evq->ee_flags &= ~EV_QUEUED; \
2248 * called upon socket close. deque and free all events for
2249 * the socket... socket must be locked by caller.
2252 evsofree(struct socket
*sp
)
2254 struct eventqelt
*evq
, *next
;
2260 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
2261 next
= evq
->ee_slist
.tqe_next
;
2264 if (evq
->ee_flags
& EV_QUEUED
) {
2265 EVPROCDEQUE(p
, evq
);
2267 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
2274 * called upon pipe close. deque and free all events for
2275 * the pipe... pipe must be locked by caller
2278 evpipefree(struct pipe
*cpipe
)
2280 struct eventqelt
*evq
, *next
;
2283 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
2284 next
= evq
->ee_slist
.tqe_next
;
2287 EVPROCDEQUE(p
, evq
);
2289 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
2296 * enqueue this event if it's not already queued. wakeup
2297 * the proc if we do queue this event to it...
2298 * entered with proc lock held... we drop it before
2299 * doing the wakeup and return in that state
2302 evprocenque(struct eventqelt
*evq
)
2309 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
2313 if (evq
->ee_flags
& EV_QUEUED
) {
2316 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2319 evq
->ee_flags
|= EV_QUEUED
;
2321 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
2325 wakeup(&p
->p_evlist
);
2327 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2332 * pipe lock must be taken by the caller
2335 postpipeevent(struct pipe
*pipep
, int event
)
2338 struct eventqelt
*evq
;
2342 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
2344 for (evq
= pipep
->pipe_evlist
.tqh_first
;
2345 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2347 if (evq
->ee_eventmask
== 0)
2351 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
2354 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
2356 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
2358 if ((evq
->ee_eventmask
& EV_WR
) &&
2359 (MAX(pipep
->pipe_buffer
.size
,PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
2361 if (pipep
->pipe_state
& PIPE_EOF
) {
2362 mask
|= EV_WR
|EV_RESET
;
2366 evq
->ee_req
.er_wcnt
= MAX(pipep
->pipe_buffer
.size
, PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
;
2372 if ((evq
->ee_eventmask
& EV_RE
)) {
2373 mask
|= EV_RE
|EV_RCLOSED
;
2375 if ((evq
->ee_eventmask
& EV_WR
)) {
2376 mask
|= EV_WR
|EV_WCLOSED
;
2385 * disarm... postevents are nops until this event is 'read' via
2386 * waitevent and then re-armed via modwatch
2388 evq
->ee_eventmask
= 0;
2391 * since events are disarmed until after the waitevent
2392 * the ee_req.er_xxxx fields can't change once we've
2393 * inserted this event into the proc queue...
2394 * therefore, the waitevent will see a 'consistent'
2395 * snapshot of the event, even though it won't hold
2396 * the pipe lock, and we're updating the event outside
2397 * of the proc lock, which it will hold
2399 evq
->ee_req
.er_eventbits
|= mask
;
2401 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2406 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2411 * given either a sockbuf or a socket run down the
2412 * event list and queue ready events found...
2413 * the socket must be locked by the caller
2416 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2419 struct eventqelt
*evq
;
2427 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2429 for (evq
= sp
->so_evlist
.tqh_first
;
2430 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2432 if (evq
->ee_eventmask
== 0)
2436 /* ready for reading:
2437 - byte cnt >= receive low water mark
2438 - read-half of conn closed
2439 - conn pending for listening sock
2440 - socket error pending
2443 - byte cnt avail >= send low water mark
2444 - write half of conn closed
2445 - socket error pending
2446 - non-blocking conn completed successfully
2450 - sock at out of band mark
2453 switch (event
& EV_DMASK
) {
2456 if ((evq
->ee_eventmask
& EV_EX
)) {
2457 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2458 mask
|= EV_EX
|EV_OOB
;
2462 case EV_RWBYTES
|EV_OOB
:
2463 if ((evq
->ee_eventmask
& EV_EX
)) {
2464 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2465 mask
|= EV_EX
|EV_OOB
;
2468 * fall into the next case
2471 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2472 /* for AFP/OT purposes; may go away in future */
2473 if ((SOCK_DOM(sp
) == PF_INET
||
2474 SOCK_DOM(sp
) == PF_INET6
) &&
2475 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2476 (sp
->so_error
== ECONNREFUSED
||
2477 sp
->so_error
== ECONNRESET
)) {
2478 if (sp
->so_pcb
== NULL
||
2479 sotoinpcb(sp
)->inp_state
==
2481 (tp
= sototcpcb(sp
)) == NULL
||
2482 tp
->t_state
== TCPS_CLOSED
) {
2483 mask
|= EV_RE
|EV_RESET
;
2488 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2490 if (sp
->so_state
& SS_CANTRCVMORE
) {
2495 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2496 /* for AFP/OT purposes; may go away in future */
2497 if ((SOCK_DOM(sp
) == PF_INET
||
2498 SOCK_DOM(sp
) == PF_INET6
) &&
2499 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2500 (sp
->so_error
== ECONNREFUSED
||
2501 sp
->so_error
== ECONNRESET
)) {
2502 if (sp
->so_pcb
== NULL
||
2503 sotoinpcb(sp
)->inp_state
==
2505 (tp
= sototcpcb(sp
)) == NULL
||
2506 tp
->t_state
== TCPS_CLOSED
) {
2507 mask
|= EV_WR
|EV_RESET
;
2512 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2517 if ((evq
->ee_eventmask
& EV_RE
)) {
2518 mask
|= EV_RE
|EV_RCONN
;
2519 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2524 if ((evq
->ee_eventmask
& EV_WR
)) {
2525 mask
|= EV_WR
|EV_WCONN
;
2530 if ((evq
->ee_eventmask
& EV_RE
)) {
2531 mask
|= EV_RE
|EV_RCLOSED
;
2536 if ((evq
->ee_eventmask
& EV_WR
)) {
2537 mask
|= EV_WR
|EV_WCLOSED
;
2542 if (evq
->ee_eventmask
& EV_RE
) {
2543 mask
|= EV_RE
|EV_FIN
;
2549 if (evq
->ee_eventmask
& EV_RE
) {
2550 mask
|= EV_RE
| event
;
2552 if (evq
->ee_eventmask
& EV_WR
) {
2553 mask
|= EV_WR
| event
;
2558 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2562 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2566 * disarm... postevents are nops until this event is 'read' via
2567 * waitevent and then re-armed via modwatch
2569 evq
->ee_eventmask
= 0;
2572 * since events are disarmed until after the waitevent
2573 * the ee_req.er_xxxx fields can't change once we've
2574 * inserted this event into the proc queue...
2575 * since waitevent can't see this event until we
2576 * enqueue it, waitevent will see a 'consistent'
2577 * snapshot of the event, even though it won't hold
2578 * the socket lock, and we're updating the event outside
2579 * of the proc lock, which it will hold
2581 evq
->ee_req
.er_eventbits
|= mask
;
2586 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2588 #endif /* SOCKETS */
2592 * watchevent system call. user passes us an event to watch
2593 * for. we malloc an event object, initialize it, and queue
2594 * it to the open socket. when the event occurs, postevent()
2595 * will enque it back to our proc where we can retrieve it
2598 * should this prevent duplicate events on same socket?
2601 * ENOMEM No memory for operation
2605 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2607 struct eventqelt
*evq
= (struct eventqelt
*)0;
2608 struct eventqelt
*np
= NULL
;
2609 struct eventreq64
*erp
;
2610 struct fileproc
*fp
= NULL
;
2613 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2615 // get a qelt and fill with users req
2616 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2622 // get users request pkt
2624 if (IS_64BIT_PROCESS(p
)) {
2625 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2627 struct eventreq32 er32
;
2629 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2632 * the user only passes in the
2633 * er_type, er_handle and er_data...
2634 * the other fields are initialized
2635 * below, so don't bother to copy
2637 erp
->er_type
= er32
.er_type
;
2638 erp
->er_handle
= er32
.er_handle
;
2639 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2644 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2648 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2650 // validate, freeing qelt if errors
2654 if (erp
->er_type
!= EV_FD
) {
2656 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2659 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2660 socket_lock((struct socket
*)fp
->f_data
, 1);
2661 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2662 #endif /* SOCKETS */
2663 } else if (fp
->f_type
== DTYPE_PIPE
) {
2664 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2665 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2667 fp_drop(p
, erp
->er_handle
, fp
, 1);
2675 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2680 * only allow one watch per file per proc
2682 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2683 if (np
->ee_proc
== p
) {
2685 if (fp
->f_type
== DTYPE_SOCKET
)
2686 socket_unlock((struct socket
*)fp
->f_data
, 1);
2688 #endif /* SOCKETS */
2689 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2690 fp_drop(p
, erp
->er_handle
, fp
, 0);
2693 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2697 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2699 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2703 if (fp
->f_type
== DTYPE_SOCKET
) {
2704 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2705 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2707 socket_unlock((struct socket
*)fp
->f_data
, 1);
2709 #endif /* SOCKETS */
2711 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2712 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2714 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2716 fp_drop_event(p
, erp
->er_handle
, fp
);
2718 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2725 * waitevent system call.
2726 * grabs the next waiting event for this proc and returns
2727 * it. if no events, user can request to sleep with timeout
2728 * or without or poll mode
2729 * ((tv != NULL && interval == 0) || tv == -1)
2732 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2735 struct eventqelt
*evq
;
2736 struct eventreq64
*erp
;
2737 uint64_t abstime
, interval
;
2738 boolean_t fast_poll
= FALSE
;
2740 struct eventreq64 er64
;
2741 struct eventreq32 er32
;
2749 * check for fast poll method
2751 if (IS_64BIT_PROCESS(p
)) {
2752 if (uap
->tv
== (user_addr_t
)-1)
2754 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2757 if (fast_poll
== TRUE
) {
2758 if (p
->p_evlist
.tqh_first
== NULL
) {
2759 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2769 if (IS_64BIT_PROCESS(p
)) {
2770 struct user64_timeval atv64
;
2771 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2772 /* Loses resolution - assume timeout < 68 years */
2773 atv
.tv_sec
= atv64
.tv_sec
;
2774 atv
.tv_usec
= atv64
.tv_usec
;
2776 struct user32_timeval atv32
;
2777 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2778 atv
.tv_sec
= atv32
.tv_sec
;
2779 atv
.tv_usec
= atv32
.tv_usec
;
2784 if (itimerfix(&atv
)) {
2788 interval
= tvtoabstime(&atv
);
2790 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2794 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2796 * found one... make a local copy while it's still on the queue
2797 * to prevent it from changing while in the midst of copying
2798 * don't want to hold the proc lock across a copyout because
2799 * it might block on a page fault at the target in user space
2803 if (IS_64BIT_PROCESS(p
))
2804 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2806 uer
.er32
.er_type
= erp
->er_type
;
2807 uer
.er32
.er_handle
= erp
->er_handle
;
2808 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2809 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2810 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2811 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2812 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2814 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2816 evq
->ee_flags
&= ~EV_QUEUED
;
2820 if (IS_64BIT_PROCESS(p
))
2821 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2823 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2825 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2826 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2830 if (uap
->tv
&& interval
== 0) {
2832 *retval
= 1; // poll failed
2834 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2838 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2842 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2844 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2846 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2850 if (error
== ERESTART
)
2852 if (error
== EWOULDBLOCK
) {
2859 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2865 * modwatch system call. user passes in event to modify.
2866 * if we find it we reset the event bits and que/deque event
2870 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2872 struct eventreq64 er
;
2873 struct eventreq64
*erp
= &er
;
2874 struct eventqelt
*evq
= NULL
; /* protected by error return */
2876 struct fileproc
*fp
;
2879 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2882 * get user's request pkt
2883 * just need the er_type and er_handle which sit above the
2884 * problematic er_data (32/64 issue)... so only copy in
2887 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2888 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2893 if (erp
->er_type
!= EV_FD
) {
2895 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2898 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2899 socket_lock((struct socket
*)fp
->f_data
, 1);
2900 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2901 #endif /* SOCKETS */
2902 } else if (fp
->f_type
== DTYPE_PIPE
) {
2903 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2904 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2906 fp_drop(p
, erp
->er_handle
, fp
, 1);
2912 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2916 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2917 fp
->f_flags
&= ~FP_WAITEVENT
;
2921 // locate event if possible
2922 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2923 if (evq
->ee_proc
== p
)
2928 if (fp
->f_type
== DTYPE_SOCKET
)
2929 socket_unlock((struct socket
*)fp
->f_data
, 1);
2931 #endif /* SOCKETS */
2932 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2933 fp_drop(p
, erp
->er_handle
, fp
, 0);
2934 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2937 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2939 if (uap
->u_eventmask
== EV_RM
) {
2940 EVPROCDEQUE(p
, evq
);
2943 if (fp
->f_type
== DTYPE_SOCKET
) {
2944 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2945 socket_unlock((struct socket
*)fp
->f_data
, 1);
2947 #endif /* SOCKETS */
2949 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2950 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2952 fp_drop(p
, erp
->er_handle
, fp
, 0);
2954 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2957 switch (uap
->u_eventmask
& EV_MASK
) {
2975 case EV_EX
|EV_RE
|EV_WR
:
2976 flag
= EV_OOB
|EV_RWBYTES
;
2981 if (fp
->f_type
== DTYPE_SOCKET
)
2982 socket_unlock((struct socket
*)fp
->f_data
, 1);
2984 #endif /* SOCKETS */
2985 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2986 fp_drop(p
, erp
->er_handle
, fp
, 0);
2987 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2991 * since we're holding the socket/pipe lock, the event
2992 * cannot go from the unqueued state to the queued state
2993 * however, it can go from the queued state to the unqueued state
2994 * since that direction is protected by the proc_lock...
2995 * so do a quick check for EV_QUEUED w/o holding the proc lock
2996 * since by far the common case will be NOT EV_QUEUED, this saves
2997 * us taking the proc_lock the majority of the time
2999 if (evq
->ee_flags
& EV_QUEUED
) {
3001 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
3003 EVPROCDEQUE(p
, evq
);
3006 * while the event is off the proc queue and
3007 * we're holding the socket/pipe lock
3008 * it's safe to update these fields...
3010 evq
->ee_req
.er_eventbits
= 0;
3011 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
3014 if (fp
->f_type
== DTYPE_SOCKET
) {
3015 postevent((struct socket
*)fp
->f_data
, 0, flag
);
3016 socket_unlock((struct socket
*)fp
->f_data
, 1);
3018 #endif /* SOCKETS */
3020 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
3021 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3023 fp_drop(p
, erp
->er_handle
, fp
, 0);
3024 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
3028 /* this routine is called from the close of fd with proc_fdlock held */
3030 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
3032 struct eventqelt
*evq
;
3035 fp
->f_flags
&= ~FP_WAITEVENT
;
3038 if (fp
->f_type
== DTYPE_SOCKET
) {
3039 socket_lock((struct socket
*)fp
->f_data
, 1);
3040 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
3042 #endif /* SOCKETS */
3043 if (fp
->f_type
== DTYPE_PIPE
) {
3044 PIPE_LOCK((struct pipe
*)fp
->f_data
);
3045 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
3053 // locate event if possible
3054 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
3055 if (evq
->ee_proc
== p
)
3060 if (fp
->f_type
== DTYPE_SOCKET
)
3061 socket_unlock((struct socket
*)fp
->f_data
, 1);
3063 #endif /* SOCKETS */
3064 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3070 EVPROCDEQUE(p
, evq
);
3073 if (fp
->f_type
== DTYPE_SOCKET
) {
3074 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
3075 socket_unlock((struct socket
*)fp
->f_data
, 1);
3077 #endif /* SOCKETS */
3079 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
3080 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3093 * Description: Get the host UUID from IOKit and return it to user space.
3095 * Parameters: uuid_buf Pointer to buffer to receive UUID
3096 * timeout Timespec for timout
3097 * spi SPI, skip sandbox check (temporary)
3099 * Returns: 0 Success
3100 * EWOULDBLOCK Timeout is too short
3101 * copyout:EFAULT Bad user buffer
3102 * mac_system_check_info:EPERM Client not allowed to perform this operation
3104 * Notes: A timeout seems redundant, since if it's tolerable to not
3105 * have a system UUID in hand, then why ask for one?
3108 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
3112 mach_timespec_t mach_ts
; /* for IOKit call */
3113 __darwin_uuid_t uuid_kern
= {}; /* for IOKit call */
3118 if ((error
= mac_system_check_info(kauth_cred_get(), "hw.uuid")) != 0) {
3119 /* EPERM invokes userspace upcall if present */
3126 /* Convert the 32/64 bit timespec into a mach_timespec_t */
3127 if ( proc_is64bit(p
) ) {
3128 struct user64_timespec ts
;
3129 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
3132 mach_ts
.tv_sec
= ts
.tv_sec
;
3133 mach_ts
.tv_nsec
= ts
.tv_nsec
;
3135 struct user32_timespec ts
;
3136 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
3139 mach_ts
.tv_sec
= ts
.tv_sec
;
3140 mach_ts
.tv_nsec
= ts
.tv_nsec
;
3143 /* Call IOKit with the stack buffer to get the UUID */
3144 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
3147 * If we get it, copy out the data to the user buffer; note that a
3148 * uuid_t is an array of characters, so this is size invariant for
3151 if (kret
== KERN_SUCCESS
) {
3152 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
3154 error
= EWOULDBLOCK
;
3163 * Description: Omnibus system call for ledger operations
3166 ledger(struct proc
*p
, struct ledger_args
*args
, __unused
int32_t *retval
)
3171 int rval
, pid
, len
, error
;
3173 struct ledger_limit_args lla
;
3178 /* Finish copying in the necessary args before taking the proc lock */
3181 if (args
->cmd
== LEDGER_ENTRY_INFO
)
3182 error
= copyin(args
->arg3
, (char *)&len
, sizeof (len
));
3183 else if (args
->cmd
== LEDGER_TEMPLATE_INFO
)
3184 error
= copyin(args
->arg2
, (char *)&len
, sizeof (len
));
3185 else if (args
->cmd
== LEDGER_LIMIT
)
3187 error
= copyin(args
->arg2
, (char *)&lla
, sizeof (lla
));
3191 else if ((args
->cmd
< 0) || (args
->cmd
> LEDGER_MAX_CMD
))
3200 if (args
->cmd
!= LEDGER_TEMPLATE_INFO
) {
3202 proc
= proc_find(pid
);
3207 error
= mac_proc_check_ledger(p
, proc
, args
->cmd
);
3217 switch (args
->cmd
) {
3219 case LEDGER_LIMIT
: {
3220 if (!kauth_cred_issuser(kauth_cred_get()))
3222 rval
= ledger_limit(task
, &lla
);
3228 struct ledger_info info
= {};
3230 rval
= ledger_info(task
, &info
);
3233 rval
= copyout(&info
, args
->arg2
,
3238 case LEDGER_ENTRY_INFO
: {
3242 rval
= ledger_get_task_entry_info_multiple(task
, &buf
, &len
);
3244 if ((rval
== 0) && (len
>= 0)) {
3245 sz
= len
* sizeof (struct ledger_entry_info
);
3246 rval
= copyout(buf
, args
->arg2
, sz
);
3250 rval
= copyout(&len
, args
->arg3
, sizeof (len
));
3254 case LEDGER_TEMPLATE_INFO
: {
3258 rval
= ledger_template_info(&buf
, &len
);
3259 if ((rval
== 0) && (len
>= 0)) {
3260 sz
= len
* sizeof (struct ledger_template_info
);
3261 rval
= copyout(buf
, args
->arg1
, sz
);
3265 rval
= copyout(&len
, args
->arg2
, sizeof (len
));
3270 panic("ledger syscall logic error -- command type %d", args
->cmd
);
3279 telemetry(__unused
struct proc
*p
, struct telemetry_args
*args
, __unused
int32_t *retval
)
3283 switch (args
->cmd
) {
3284 #if CONFIG_TELEMETRY
3285 case TELEMETRY_CMD_TIMER_EVENT
:
3286 error
= telemetry_timer_event(args
->deadline
, args
->interval
, args
->leeway
);
3288 case TELEMETRY_CMD_PMI_SETUP
:
3289 error
= telemetry_pmi_setup((enum telemetry_pmi
)args
->deadline
, args
->interval
);
3291 #endif /* CONFIG_TELEMETRY */
3292 case TELEMETRY_CMD_VOUCHER_NAME
:
3293 if (thread_set_voucher_name((mach_port_name_t
)args
->deadline
))
3305 #if DEVELOPMENT || DEBUG
3306 #if CONFIG_WAITQ_DEBUG
3307 static uint64_t g_wqset_num
= 0;
3310 struct waitq_set
*wqset
;
3313 static queue_head_t g_wqset_list
;
3314 static struct waitq_set
*g_waitq_set
= NULL
;
3316 static inline struct waitq_set
*sysctl_get_wqset(int idx
)
3318 struct g_wqset
*gwqs
;
3321 queue_init(&g_wqset_list
);
3323 /* don't bother with locks: this is test-only code! */
3324 qe_foreach_element(gwqs
, &g_wqset_list
, link
) {
3325 if ((int)(wqset_id(gwqs
->wqset
) & 0xffffffff) == idx
)
3329 /* allocate a new one */
3331 gwqs
= (struct g_wqset
*)kalloc(sizeof(*gwqs
));
3332 assert(gwqs
!= NULL
);
3334 gwqs
->wqset
= waitq_set_alloc(SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
, NULL
);
3335 enqueue_tail(&g_wqset_list
, &gwqs
->link
);
3336 printf("[WQ]: created new waitq set 0x%llx\n", wqset_id(gwqs
->wqset
));
3341 #define MAX_GLOBAL_TEST_QUEUES 64
3342 static int g_wq_init
= 0;
3343 static struct waitq g_wq
[MAX_GLOBAL_TEST_QUEUES
];
3345 static inline struct waitq
*global_test_waitq(int idx
)
3352 for (int i
= 0; i
< MAX_GLOBAL_TEST_QUEUES
; i
++)
3353 waitq_init(&g_wq
[i
], SYNC_POLICY_FIFO
);
3356 return &g_wq
[idx
% MAX_GLOBAL_TEST_QUEUES
];
3359 static int sysctl_waitq_wakeup_one SYSCTL_HANDLER_ARGS
3361 #pragma unused(oidp, arg1, arg2)
3364 struct waitq
*waitq
;
3366 int64_t event64
= 0;
3368 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3373 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3376 index
= (int)((-event64
) & 0xffffffff);
3377 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3380 index
= (int)event64
;
3381 waitq
= global_test_waitq(index
);
3386 printf("[WQ]: Waking one thread on waitq [%d] event:0x%llx\n",
3388 kr
= waitq_wakeup64_one(waitq
, (event64_t
)event64
, THREAD_AWAKENED
,
3389 WAITQ_ALL_PRIORITIES
);
3390 printf("[WQ]: \tkr=%d\n", kr
);
3392 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3394 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wakeup_one
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3395 0, 0, sysctl_waitq_wakeup_one
, "Q", "wakeup one thread waiting on given event");
3398 static int sysctl_waitq_wakeup_all SYSCTL_HANDLER_ARGS
3400 #pragma unused(oidp, arg1, arg2)
3403 struct waitq
*waitq
;
3405 int64_t event64
= 0;
3407 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3412 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3415 index
= (int)((-event64
) & 0xffffffff);
3416 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3419 index
= (int)event64
;
3420 waitq
= global_test_waitq(index
);
3425 printf("[WQ]: Waking all threads on waitq [%d] event:0x%llx\n",
3427 kr
= waitq_wakeup64_all(waitq
, (event64_t
)event64
,
3428 THREAD_AWAKENED
, WAITQ_ALL_PRIORITIES
);
3429 printf("[WQ]: \tkr=%d\n", kr
);
3431 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3433 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wakeup_all
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3434 0, 0, sysctl_waitq_wakeup_all
, "Q", "wakeup all threads waiting on given event");
3437 static int sysctl_waitq_wait SYSCTL_HANDLER_ARGS
3439 #pragma unused(oidp, arg1, arg2)
3442 struct waitq
*waitq
;
3444 int64_t event64
= 0;
3446 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3451 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3454 index
= (int)((-event64
) & 0xffffffff);
3455 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3458 index
= (int)event64
;
3459 waitq
= global_test_waitq(index
);
3464 printf("[WQ]: Current thread waiting on waitq [%d] event:0x%llx\n",
3466 kr
= waitq_assert_wait64(waitq
, (event64_t
)event64
, THREAD_INTERRUPTIBLE
, 0);
3467 if (kr
== THREAD_WAITING
)
3468 thread_block(THREAD_CONTINUE_NULL
);
3469 printf("[WQ]: \tWoke Up: kr=%d\n", kr
);
3471 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3473 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wait
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3474 0, 0, sysctl_waitq_wait
, "Q", "start waiting on given event");
3477 static int sysctl_wqset_select SYSCTL_HANDLER_ARGS
3479 #pragma unused(oidp, arg1, arg2)
3481 struct waitq_set
*wqset
;
3482 uint64_t event64
= 0;
3484 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3491 wqset
= sysctl_get_wqset((int)(event64
& 0xffffffff));
3492 g_waitq_set
= wqset
;
3494 event64
= wqset_id(wqset
);
3495 printf("[WQ]: selected wqset 0x%llx\n", event64
);
3499 event64
= wqset_id(g_waitq_set
);
3501 event64
= (uint64_t)(-1);
3503 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3505 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_select
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3506 0, 0, sysctl_wqset_select
, "Q", "select/create a global waitq set");
3509 static int sysctl_waitq_link SYSCTL_HANDLER_ARGS
3511 #pragma unused(oidp, arg1, arg2)
3514 struct waitq
*waitq
;
3515 struct waitq_set
*wqset
;
3517 uint64_t reserved_link
= 0;
3518 int64_t event64
= 0;
3520 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3525 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3528 g_waitq_set
= sysctl_get_wqset(1);
3529 wqset
= g_waitq_set
;
3532 struct waitq_set
*tmp
;
3533 index
= (int)((-event64
) & 0xffffffff);
3534 tmp
= sysctl_get_wqset(index
);
3537 waitq
= wqset_waitq(tmp
);
3540 index
= (int)event64
;
3541 waitq
= global_test_waitq(index
);
3544 printf("[WQ]: linking waitq [%d] to global wqset (0x%llx)\n",
3545 index
, wqset_id(wqset
));
3546 reserved_link
= waitq_link_reserve(waitq
);
3547 kr
= waitq_link(waitq
, wqset
, WAITQ_SHOULD_LOCK
, &reserved_link
);
3548 waitq_link_release(reserved_link
);
3550 printf("[WQ]: \tkr=%d\n", kr
);
3553 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3555 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_link
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3556 0, 0, sysctl_waitq_link
, "Q", "link global waitq to test waitq set");
3559 static int sysctl_waitq_unlink SYSCTL_HANDLER_ARGS
3561 #pragma unused(oidp, arg1, arg2)
3564 struct waitq
*waitq
;
3565 struct waitq_set
*wqset
;
3567 uint64_t event64
= 0;
3569 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3574 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3577 g_waitq_set
= sysctl_get_wqset(1);
3578 wqset
= g_waitq_set
;
3580 index
= (int)event64
;
3581 waitq
= global_test_waitq(index
);
3583 printf("[WQ]: unlinking waitq [%d] from global wqset (0x%llx)\n",
3584 index
, wqset_id(wqset
));
3586 kr
= waitq_unlink(waitq
, wqset
);
3587 printf("[WQ]: \tkr=%d\n", kr
);
3589 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3591 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_unlink
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3592 0, 0, sysctl_waitq_unlink
, "Q", "unlink global waitq from test waitq set");
3595 static int sysctl_waitq_clear_prepost SYSCTL_HANDLER_ARGS
3597 #pragma unused(oidp, arg1, arg2)
3598 struct waitq
*waitq
;
3599 uint64_t event64
= 0;
3602 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3607 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3609 index
= (int)event64
;
3610 waitq
= global_test_waitq(index
);
3612 printf("[WQ]: clearing prepost on waitq [%d]\n", index
);
3613 waitq_clear_prepost(waitq
);
3615 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3617 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_clear_prepost
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3618 0, 0, sysctl_waitq_clear_prepost
, "Q", "clear prepost on given waitq");
3621 static int sysctl_wqset_unlink_all SYSCTL_HANDLER_ARGS
3623 #pragma unused(oidp, arg1, arg2)
3625 struct waitq_set
*wqset
;
3627 uint64_t event64
= 0;
3629 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3634 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3637 g_waitq_set
= sysctl_get_wqset(1);
3638 wqset
= g_waitq_set
;
3640 printf("[WQ]: unlinking all queues from global wqset (0x%llx)\n",
3643 kr
= waitq_set_unlink_all(wqset
);
3644 printf("[WQ]: \tkr=%d\n", kr
);
3646 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3648 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_unlink_all
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3649 0, 0, sysctl_wqset_unlink_all
, "Q", "unlink all queues from test waitq set");
3652 static int sysctl_wqset_clear_preposts SYSCTL_HANDLER_ARGS
3654 #pragma unused(oidp, arg1, arg2)
3655 struct waitq_set
*wqset
= NULL
;
3656 uint64_t event64
= 0;
3659 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3666 index
= (int)((event64
) & 0xffffffff);
3667 wqset
= sysctl_get_wqset(index
);
3668 assert(wqset
!= NULL
);
3670 printf("[WQ]: clearing preposts on wqset 0x%llx\n", wqset_id(wqset
));
3671 waitq_set_clear_preposts(wqset
);
3675 event64
= wqset_id(wqset
);
3677 event64
= (uint64_t)(-1);
3679 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3681 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_clear_preposts
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3682 0, 0, sysctl_wqset_clear_preposts
, "Q", "clear preposts on given waitq set");
3684 #endif /* CONFIG_WAITQ_DEBUG */
3687 sysctl_waitq_set_nelem SYSCTL_HANDLER_ARGS
3689 #pragma unused(oidp, arg1, arg2)
3693 if (req
->newptr
!= USER_ADDR_NULL
)
3696 nelem
= sysctl_helper_waitq_set_nelem();
3698 return SYSCTL_OUT(req
, &nelem
, sizeof(nelem
));
3701 SYSCTL_PROC(_kern
, OID_AUTO
, n_ltable_entries
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
3702 0, 0, sysctl_waitq_set_nelem
, "I", "ltable elementis currently used");
3705 #endif /* DEVELOPMENT || DEBUG */