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 <mach/mach_types.h>
101 #include <kern/kern_types.h>
102 #include <kern/assert.h>
103 #include <kern/kalloc.h>
104 #include <kern/thread.h>
105 #include <kern/clock.h>
106 #include <kern/ledger.h>
107 #include <kern/task.h>
108 #include <kern/telemetry.h>
109 #include <kern/waitq.h>
110 #include <kern/sched_prim.h>
112 #include <sys/mbuf.h>
113 #include <sys/domain.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/errno.h>
117 #include <sys/syscall.h>
118 #include <sys/pipe.h>
120 #include <security/audit/audit.h>
123 #include <net/route.h>
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/ip_var.h>
130 #include <netinet/ip6.h>
131 #include <netinet/tcp.h>
132 #include <netinet/tcp_fsm.h>
133 #include <netinet/tcp_seq.h>
134 #include <netinet/tcp_timer.h>
135 #include <netinet/tcp_var.h>
136 #include <netinet/tcpip.h>
137 #include <netinet/tcp_debug.h>
138 /* for wait queue based select */
139 #include <kern/waitq.h>
140 #include <kern/kalloc.h>
141 #include <sys/vnode_internal.h>
143 /* XXX should be in a header file somewhere */
144 void evsofree(struct socket
*);
145 void evpipefree(struct pipe
*);
146 void postpipeevent(struct pipe
*, int);
147 void postevent(struct socket
*, struct sockbuf
*, int);
148 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
149 extern void delay(int);
151 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
152 int wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
);
154 __private_extern__
int dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
155 user_addr_t bufp
, user_size_t nbyte
,
156 off_t offset
, int flags
, user_ssize_t
*retval
);
157 __private_extern__
int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
158 user_addr_t bufp
, user_size_t nbyte
,
159 off_t offset
, int flags
, user_ssize_t
*retval
);
160 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
161 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
164 /* Conflict wait queue for when selects collide (opaque type) */
165 struct waitq select_conflict_queue
;
168 * Init routine called from bsd_init.c
170 void select_waitq_init(void);
172 select_waitq_init(void)
174 waitq_init(&select_conflict_queue
, SYNC_POLICY_FIFO
| SYNC_POLICY_DISABLE_IRQ
);
177 #define f_flag f_fglob->fg_flag
178 #define f_type f_fglob->fg_ops->fo_type
179 #define f_msgcount f_fglob->fg_msgcount
180 #define f_cred f_fglob->fg_cred
181 #define f_ops f_fglob->fg_ops
182 #define f_offset f_fglob->fg_offset
183 #define f_data f_fglob->fg_data
189 * preparefileread:EBADF
190 * preparefileread:ESPIPE
191 * preparefileread:ENXIO
192 * preparefileread:EBADF
196 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
198 __pthread_testcancel(1);
199 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
203 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
208 struct vfs_context context
;
210 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
213 context
= *(vfs_context_current());
214 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
216 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
217 (off_t
)-1, 0, retval
);
219 donefileread(p
, fp
, fd
);
228 * preparefileread:EBADF
229 * preparefileread:ESPIPE
230 * preparefileread:ENXIO
231 * preparefileread:EBADF
235 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
237 __pthread_testcancel(1);
238 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
242 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
244 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
247 struct vfs_context context
;
249 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
252 context
= *(vfs_context_current());
253 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
255 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
256 uap
->offset
, FOF_OFFSET
, retval
);
258 donefileread(p
, fp
, fd
);
260 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
261 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
268 * Code common for read and pread
272 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
275 fp_drop(p
, fd
, fp
, 1);
288 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
298 error
= fp_lookup(p
, fd
, &fp
, 1);
304 if ((fp
->f_flag
& FREAD
) == 0) {
308 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
312 if (fp
->f_type
== DTYPE_VNODE
) {
313 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
315 if (check_for_pread
&& (vnode_isfifo(vp
))) {
319 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
331 fp_drop(p
, fd
, fp
, 1);
342 __private_extern__
int
343 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
344 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
345 user_ssize_t
*retval
)
348 user_ssize_t bytecnt
;
350 char uio_buf
[ UIO_SIZEOF(1) ];
355 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
356 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
357 &uio_buf
[0], sizeof(uio_buf
));
359 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
360 &uio_buf
[0], sizeof(uio_buf
));
362 uio_addiov(auio
, bufp
, nbyte
);
366 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
367 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
368 error
== EINTR
|| error
== EWOULDBLOCK
))
371 bytecnt
-= uio_resid(auio
);
379 * Scatter read system call.
388 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
390 __pthread_testcancel(1);
391 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
395 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
399 struct user_iovec
*iovp
;
401 /* Verify range bedfore calling uio_create() */
402 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
405 /* allocate a uio large enough to hold the number of iovecs passed */
406 auio
= uio_create(uap
->iovcnt
, 0,
407 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
410 /* get location of iovecs within the uio. then copyin the iovecs from
413 iovp
= uio_iovsaddr(auio
);
416 goto ExitThisRoutine
;
418 error
= copyin_user_iovec_array(uap
->iovp
,
419 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
422 goto ExitThisRoutine
;
425 /* finalize uio_t for use and do the IO
427 error
= uio_calculateresid(auio
);
429 goto ExitThisRoutine
;
431 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
449 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
451 __pthread_testcancel(1);
452 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
457 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
462 bool wrote_some
= false;
466 error
= fp_lookup(p
,fd
,&fp
,0);
469 if ((fp
->f_flag
& FWRITE
) == 0) {
471 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
473 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
476 struct vfs_context context
= *(vfs_context_current());
477 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
479 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
480 (off_t
)-1, 0, retval
);
482 wrote_some
= *retval
> 0;
485 fp_drop_written(p
, fd
, fp
);
487 fp_drop(p
, fd
, fp
, 0);
503 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
505 __pthread_testcancel(1);
506 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
510 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
515 vnode_t vp
= (vnode_t
)0;
516 bool wrote_some
= false;
520 error
= fp_lookup(p
,fd
,&fp
,0);
524 if ((fp
->f_flag
& FWRITE
) == 0) {
526 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
528 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
531 struct vfs_context context
= *vfs_context_current();
532 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
534 if (fp
->f_type
!= DTYPE_VNODE
) {
538 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
539 if (vnode_isfifo(vp
)) {
543 if ((vp
->v_flag
& VISTTY
)) {
547 if (uap
->offset
== (off_t
)-1) {
552 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
553 uap
->offset
, FOF_OFFSET
, retval
);
554 wrote_some
= *retval
> 0;
558 fp_drop_written(p
, fd
, fp
);
560 fp_drop(p
, fd
, fp
, 0);
562 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
563 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
572 * <fo_write>:??? [indirect through struct fileops]
574 __private_extern__
int
575 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
576 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
577 user_ssize_t
*retval
)
581 user_ssize_t bytecnt
;
582 char uio_buf
[ UIO_SIZEOF(1) ];
584 if (nbyte
> INT_MAX
) {
589 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
590 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
591 &uio_buf
[0], sizeof(uio_buf
));
593 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
594 &uio_buf
[0], sizeof(uio_buf
));
596 uio_addiov(auio
, bufp
, nbyte
);
599 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
600 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
601 error
== EINTR
|| error
== EWOULDBLOCK
))
603 /* The socket layer handles SIGPIPE */
604 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
605 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0) {
606 /* XXX Raise the signal on the thread? */
607 psignal(vfs_context_proc(ctx
), SIGPIPE
);
610 bytecnt
-= uio_resid(auio
);
617 * Gather write system call
620 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
622 __pthread_testcancel(1);
623 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
627 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
632 struct user_iovec
*iovp
;
633 bool wrote_some
= false;
635 AUDIT_ARG(fd
, uap
->fd
);
637 /* Verify range bedfore calling uio_create() */
638 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
641 /* allocate a uio large enough to hold the number of iovecs passed */
642 auio
= uio_create(uap
->iovcnt
, 0,
643 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
646 /* get location of iovecs within the uio. then copyin the iovecs from
649 iovp
= uio_iovsaddr(auio
);
652 goto ExitThisRoutine
;
654 error
= copyin_user_iovec_array(uap
->iovp
,
655 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
658 goto ExitThisRoutine
;
661 /* finalize uio_t for use and do the IO
663 error
= uio_calculateresid(auio
);
665 goto ExitThisRoutine
;
668 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
670 goto ExitThisRoutine
;
672 if ((fp
->f_flag
& FWRITE
) == 0) {
674 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
676 error
= fp_guard_exception(p
, uap
->fd
, fp
, kGUARD_EXC_WRITE
);
679 error
= wr_uio(p
, fp
, auio
, retval
);
680 wrote_some
= *retval
> 0;
684 fp_drop_written(p
, uap
->fd
, fp
);
686 fp_drop(p
, uap
->fd
, fp
, 0);
697 wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
)
701 struct vfs_context context
= *vfs_context_current();
703 count
= uio_resid(uio
);
705 context
.vc_ucred
= fp
->f_cred
;
706 error
= fo_write(fp
, uio
, 0, &context
);
708 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
709 error
== EINTR
|| error
== EWOULDBLOCK
))
711 /* The socket layer handles SIGPIPE */
712 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
713 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0)
716 *retval
= count
- uio_resid(uio
);
723 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
728 struct vfs_context context
= *vfs_context_current();
730 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
733 count
= uio_resid(uio
);
735 context
.vc_ucred
= fp
->f_cred
;
737 error
= fo_read(fp
, uio
, 0, &context
);
740 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
741 error
== EINTR
|| error
== EWOULDBLOCK
))
744 *retval
= count
- uio_resid(uio
);
746 donefileread(p
, fp
, fdes
);
761 * fp_lookup:EBADF Bad file descriptor
765 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
767 struct fileproc
*fp
= NULL
;
770 caddr_t datap
= NULL
, memp
= NULL
;
771 boolean_t is64bit
= FALSE
;
773 #define STK_PARAMS 128
774 char stkbuf
[STK_PARAMS
];
776 u_long com
= uap
->com
;
777 struct vfs_context context
= *vfs_context_current();
779 AUDIT_ARG(fd
, uap
->fd
);
780 AUDIT_ARG(addr
, uap
->data
);
782 is64bit
= proc_is64bit(p
);
785 AUDIT_ARG(value64
, com
);
787 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, com
));
788 #endif /* CONFIG_AUDIT */
791 * Interpret high order word to find amount of data to be
792 * copied to/from the user's address space.
794 size
= IOCPARM_LEN(com
);
795 if (size
> IOCPARM_MAX
)
797 if (size
> sizeof (stkbuf
)) {
798 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
805 error
= copyin(uap
->data
, datap
, size
);
809 /* XXX - IOC_IN and no size? we should proably return an error here!! */
811 *(user_addr_t
*)datap
= uap
->data
;
814 *(uint32_t *)datap
= (uint32_t)uap
->data
;
817 } else if ((com
& IOC_OUT
) && size
)
819 * Zero the buffer so the user always
820 * gets back something deterministic.
823 else if (com
& IOC_VOID
) {
824 /* XXX - this is odd since IOC_VOID means no parameters */
826 *(user_addr_t
*)datap
= uap
->data
;
829 *(uint32_t *)datap
= (uint32_t)uap
->data
;
834 error
= fp_lookup(p
,fd
,&fp
,1);
840 AUDIT_ARG(file
, p
, fp
);
842 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
847 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
850 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, com
);
857 *fdflags(p
, fd
) &= ~UF_EXCLOSE
;
861 *fdflags(p
, fd
) |= UF_EXCLOSE
;
865 if ( (tmp
= *(int *)datap
) )
866 fp
->f_flag
|= FNONBLOCK
;
868 fp
->f_flag
&= ~FNONBLOCK
;
869 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
873 if ( (tmp
= *(int *)datap
) )
874 fp
->f_flag
|= FASYNC
;
876 fp
->f_flag
&= ~FASYNC
;
877 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
882 if (fp
->f_type
== DTYPE_SOCKET
) {
883 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
886 if (fp
->f_type
== DTYPE_PIPE
) {
887 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
893 struct proc
*p1
= proc_find(tmp
);
901 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
905 if (fp
->f_type
== DTYPE_SOCKET
) {
906 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
909 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
910 *(int *)datap
= -*(int *)datap
;
914 error
= fo_ioctl(fp
, com
, datap
, &context
);
916 * Copy any data to user, size was
917 * already set and checked above.
919 if (error
== 0 && (com
& IOC_OUT
) && size
)
920 error
= copyout(datap
, uap
->data
, (u_int
)size
);
924 fp_drop(p
, fd
, fp
, 1);
933 int selwait
, nselcoll
;
934 #define SEL_FIRSTPASS 1
935 #define SEL_SECONDPASS 2
936 extern int selcontinue(int error
);
937 extern int selprocess(int error
, int sel_pass
);
938 static int selscan(struct proc
*p
, struct _select
* sel
, struct _select_data
* seldata
,
939 int nfd
, int32_t *retval
, int sel_pass
, struct waitq_set
*wqset
);
940 static int selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *count
);
941 static int seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
);
942 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
943 static int select_internal(struct proc
*p
, struct select_nocancel_args
*uap
, uint64_t timeout
, int32_t *retval
);
946 * Select system call.
949 * EINVAL Invalid argument
950 * EAGAIN Nonconformant error if allocation fails
953 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
955 __pthread_testcancel(1);
956 return select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
);
960 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
962 uint64_t timeout
= 0;
967 if (IS_64BIT_PROCESS(p
)) {
968 struct user64_timeval atv64
;
969 err
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
970 /* Loses resolution - assume timeout < 68 years */
971 atv
.tv_sec
= atv64
.tv_sec
;
972 atv
.tv_usec
= atv64
.tv_usec
;
974 struct user32_timeval atv32
;
975 err
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
976 atv
.tv_sec
= atv32
.tv_sec
;
977 atv
.tv_usec
= atv32
.tv_usec
;
982 if (itimerfix(&atv
)) {
987 clock_absolutetime_interval_to_deadline(tvtoabstime(&atv
), &timeout
);
990 return select_internal(p
, uap
, timeout
, retval
);
994 pselect(struct proc
*p
, struct pselect_args
*uap
, int32_t *retval
)
996 __pthread_testcancel(1);
997 return pselect_nocancel(p
, (struct pselect_nocancel_args
*)uap
, retval
);
1001 pselect_nocancel(struct proc
*p
, struct pselect_nocancel_args
*uap
, int32_t *retval
)
1005 uint64_t timeout
= 0;
1010 if (IS_64BIT_PROCESS(p
)) {
1011 struct user64_timespec ts64
;
1012 err
= copyin(uap
->ts
, (caddr_t
)&ts64
, sizeof(ts64
));
1013 ts
.tv_sec
= ts64
.tv_sec
;
1014 ts
.tv_nsec
= ts64
.tv_nsec
;
1016 struct user32_timespec ts32
;
1017 err
= copyin(uap
->ts
, (caddr_t
)&ts32
, sizeof(ts32
));
1018 ts
.tv_sec
= ts32
.tv_sec
;
1019 ts
.tv_nsec
= ts32
.tv_nsec
;
1025 if (!timespec_is_valid(&ts
)) {
1028 clock_absolutetime_interval_to_deadline(tstoabstime(&ts
), &timeout
);
1031 ut
= get_bsdthread_info(current_thread());
1033 if (uap
->mask
!= USER_ADDR_NULL
) {
1034 /* save current mask, then copyin and set new mask */
1036 err
= copyin(uap
->mask
, &newset
, sizeof(sigset_t
));
1040 ut
->uu_oldmask
= ut
->uu_sigmask
;
1041 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1042 ut
->uu_sigmask
= (newset
& ~sigcantmask
);
1045 err
= select_internal(p
, (struct select_nocancel_args
*)uap
, timeout
, retval
);
1047 if (err
!= EINTR
&& ut
->uu_flag
& UT_SAS_OLDMASK
) {
1049 * Restore old mask (direct return case). NOTE: EINTR can also be returned
1050 * if the thread is cancelled. In that case, we don't reset the signal
1051 * mask to its original value (which usually happens in the signal
1052 * delivery path). This behavior is permitted by POSIX.
1054 ut
->uu_sigmask
= ut
->uu_oldmask
;
1056 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1063 * Generic implementation of {,p}select. Care: we type-pun uap across the two
1064 * syscalls, which differ slightly. The first 4 arguments (nfds and the fd sets)
1065 * are identical. The 5th (timeout) argument points to different types, so we
1066 * unpack in the syscall-specific code, but the generic code still does a null
1067 * check on this argument to determine if a timeout was specified.
1070 select_internal(struct proc
*p
, struct select_nocancel_args
*uap
, uint64_t timeout
, int32_t *retval
)
1075 struct uthread
*uth
;
1076 struct _select
*sel
;
1077 struct _select_data
*seldata
;
1078 int needzerofill
= 1;
1082 th_act
= current_thread();
1083 uth
= get_bsdthread_info(th_act
);
1084 sel
= &uth
->uu_select
;
1085 seldata
= &uth
->uu_kevent
.ss_select_data
;
1088 seldata
->args
= uap
;
1089 seldata
->retval
= retval
;
1090 seldata
->wqp
= NULL
;
1097 /* select on thread of process that already called proc_exit() */
1098 if (p
->p_fd
== NULL
) {
1102 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
1103 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
1105 nw
= howmany(uap
->nd
, NFDBITS
);
1106 ni
= nw
* sizeof(fd_mask
);
1109 * if the previously allocated space for the bits is smaller than
1110 * what is requested or no space has yet been allocated for this
1111 * thread, allocate enough space now.
1113 * Note: If this process fails, select() will return EAGAIN; this
1114 * is the same thing pool() returns in a no-memory situation, but
1115 * it is not a POSIX compliant error code for select().
1117 if (sel
->nbytes
< (3 * ni
)) {
1118 int nbytes
= 3 * ni
;
1120 /* Free previous allocation, if any */
1121 if (sel
->ibits
!= NULL
)
1122 FREE(sel
->ibits
, M_TEMP
);
1123 if (sel
->obits
!= NULL
) {
1124 FREE(sel
->obits
, M_TEMP
);
1125 /* NULL out; subsequent ibits allocation may fail */
1129 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1130 if (sel
->ibits
== NULL
)
1132 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1133 if (sel
->obits
== NULL
) {
1134 FREE(sel
->ibits
, M_TEMP
);
1138 sel
->nbytes
= nbytes
;
1143 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1144 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1148 * get the bits from the user address space
1150 #define getbits(name, x) \
1152 if (uap->name && (error = copyin(uap->name, \
1153 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1154 goto continuation; \
1162 seldata
->abstime
= timeout
;
1164 if ( (error
= selcount(p
, sel
->ibits
, uap
->nd
, &count
)) ) {
1169 * We need an array of waitq pointers. This is due to the new way
1170 * in which waitqs are linked to sets. When a thread selects on a
1171 * file descriptor, a waitq (embedded in a selinfo structure) is
1172 * added to the thread's local waitq set. There is no longer any
1173 * way to directly iterate over all members of a given waitq set.
1174 * The process of linking a waitq into a set may allocate a link
1175 * table object. Because we can't iterate over all the waitqs to
1176 * which our thread waitq set belongs, we need a way of removing
1179 * Thus we need a buffer which will hold one waitq pointer
1180 * per FD being selected. During the tear-down phase we can use
1181 * these pointers to dis-associate the underlying selinfo's waitq
1182 * from our thread's waitq set.
1184 * Because we also need to allocate a waitq set for this thread,
1185 * we use a bare buffer pointer to hold all the memory. Note that
1186 * this memory is cached in the thread pointer and not reaped until
1187 * the thread exists. This is generally OK because threads that
1188 * call select tend to keep calling select repeatedly.
1190 sz
= ALIGN(sizeof(struct waitq_set
)) + (count
* sizeof(uint64_t));
1191 if (sz
> uth
->uu_wqstate_sz
) {
1192 /* (re)allocate a buffer to hold waitq pointers */
1193 if (uth
->uu_wqset
) {
1194 if (waitq_set_is_valid(uth
->uu_wqset
))
1195 waitq_set_deinit(uth
->uu_wqset
);
1196 FREE(uth
->uu_wqset
, M_SELECT
);
1197 } else if (uth
->uu_wqstate_sz
&& !uth
->uu_wqset
)
1198 panic("select: thread structure corrupt! "
1199 "uu_wqstate_sz:%ld, wqstate_buf == NULL",
1200 uth
->uu_wqstate_sz
);
1201 uth
->uu_wqstate_sz
= sz
;
1202 MALLOC(uth
->uu_wqset
, struct waitq_set
*, sz
, M_SELECT
, M_WAITOK
);
1204 panic("can't allocate %ld bytes for wqstate buffer",
1205 uth
->uu_wqstate_sz
);
1206 waitq_set_init(uth
->uu_wqset
,
1207 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
|SYNC_POLICY_DISABLE_IRQ
, NULL
);
1210 if (!waitq_set_is_valid(uth
->uu_wqset
))
1211 waitq_set_init(uth
->uu_wqset
,
1212 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
|SYNC_POLICY_DISABLE_IRQ
, NULL
);
1214 /* the last chunk of our buffer is an array of waitq pointers */
1215 seldata
->wqp
= (uint64_t *)((char *)(uth
->uu_wqset
) + ALIGN(sizeof(struct waitq_set
)));
1216 bzero(seldata
->wqp
, sz
- ALIGN(sizeof(struct waitq_set
)));
1218 seldata
->count
= count
;
1224 * We have already cleaned up any state we established,
1225 * either locally or as a result of selcount(). We don't
1226 * need to wait_subqueue_unlink_all(), since we haven't set
1227 * anything at this point.
1232 return selprocess(0, SEL_FIRSTPASS
);
1236 selcontinue(int error
)
1238 return selprocess(error
, SEL_SECONDPASS
);
1245 * Parameters: error The error code from our caller
1246 * sel_pass The pass we are on
1249 selprocess(int error
, int sel_pass
)
1254 struct uthread
*uth
;
1256 struct select_nocancel_args
*uap
;
1258 struct _select
*sel
;
1259 struct _select_data
*seldata
;
1264 wait_result_t wait_result
;
1267 th_act
= current_thread();
1268 uth
= get_bsdthread_info(th_act
);
1269 sel
= &uth
->uu_select
;
1270 seldata
= &uth
->uu_kevent
.ss_select_data
;
1271 uap
= seldata
->args
;
1272 retval
= seldata
->retval
;
1274 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1276 if (seldata
->count
== 0)
1283 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1285 /* skip scans if the select is just for timeouts */
1286 if (seldata
->count
) {
1287 error
= selscan(p
, sel
, seldata
, uap
->nd
, retval
, sel_pass
, uth
->uu_wqset
);
1288 if (error
|| *retval
) {
1291 if (prepost
|| somewakeup
) {
1293 * if the select of log, then we can wakeup and
1294 * discover some one else already read the data;
1295 * go to select again if time permits
1306 clock_get_uptime(&now
);
1307 if (now
>= seldata
->abstime
)
1312 /* cleanup obits and try again */
1314 sel_pass
= SEL_FIRSTPASS
;
1319 * To effect a poll, the timeout argument should be
1320 * non-nil, pointing to a zero-valued timeval structure.
1322 if (uap
->tv
&& seldata
->abstime
== 0) {
1326 /* No spurious wakeups due to colls,no need to check for them */
1327 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1328 sel_pass
= SEL_FIRSTPASS
;
1332 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1334 /* if the select is just for timeout skip check */
1335 if (seldata
->count
&& (sel_pass
== SEL_SECONDPASS
))
1336 panic("selprocess: 2nd pass assertwaiting");
1338 /* waitq_set has waitqueue as first element */
1339 wait_result
= waitq_assert_wait64_leeway((struct waitq
*)uth
->uu_wqset
,
1340 NO_EVENT64
, THREAD_ABORTSAFE
,
1341 TIMEOUT_URGENCY_USER_NORMAL
,
1344 if (wait_result
!= THREAD_AWAKENED
) {
1345 /* there are no preposted events */
1346 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1347 "select", 0, selcontinue
);
1354 sel_pass
= SEL_SECONDPASS
;
1361 seldrop(p
, sel
->ibits
, uap
->nd
);
1362 waitq_set_deinit(uth
->uu_wqset
);
1364 * zero out the waitq pointer array to avoid use-after free
1365 * errors in the selcount error path (seldrop_locked) if/when
1366 * the thread re-calls select().
1368 bzero((void *)uth
->uu_wqset
, uth
->uu_wqstate_sz
);
1370 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1371 /* select is not restarted after signals... */
1372 if (error
== ERESTART
)
1374 if (error
== EWOULDBLOCK
)
1376 nw
= howmany(uap
->nd
, NFDBITS
);
1377 ni
= nw
* sizeof(fd_mask
);
1379 #define putbits(name, x) \
1381 if (uap->name && (error2 = \
1382 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1395 if (error
!= EINTR
&& sel_pass
== SEL_SECONDPASS
&& uth
->uu_flag
& UT_SAS_OLDMASK
) {
1396 /* restore signal mask - continuation case */
1397 uth
->uu_sigmask
= uth
->uu_oldmask
;
1398 uth
->uu_oldmask
= 0;
1399 uth
->uu_flag
&= ~UT_SAS_OLDMASK
;
1407 * remove the fileproc's underlying waitq from the supplied waitq set;
1408 * clear FP_INSELECT when appropriate
1411 * fp File proc that is potentially currently in select
1412 * wqset Waitq set to which the fileproc may belong
1413 * (usually this is the thread's private waitq set)
1415 * proc_fdlock is held
1417 static void selunlinkfp(struct fileproc
*fp
, uint64_t wqp_id
, struct waitq_set
*wqset
)
1419 int valid_set
= waitq_set_is_valid(wqset
);
1420 int valid_q
= !!wqp_id
;
1423 * This could be called (from selcount error path) before we setup
1424 * the thread's wqset. Check the wqset passed in, and only unlink if
1428 /* unlink the underlying waitq from the input set (thread waitq set) */
1429 if (valid_q
&& valid_set
)
1430 waitq_unlink_by_prepost_id(wqp_id
, wqset
);
1432 /* allow passing a NULL/invalid fp for seldrop unwind */
1433 if (!fp
|| !(fp
->f_flags
& (FP_INSELECT
|FP_SELCONFLICT
)))
1437 * We can always remove the conflict queue from our thread's set: this
1438 * will not affect other threads that potentially need to be awoken on
1439 * the conflict queue during a fileproc_drain - those sets will still
1440 * be linked with the global conflict queue, and the last waiter
1441 * on the fp clears the CONFLICT marker.
1443 if (valid_set
&& (fp
->f_flags
& FP_SELCONFLICT
))
1444 waitq_unlink(&select_conflict_queue
, wqset
);
1447 * This isn't quite right - we don't actually know if this
1448 * fileproc is in another select or not! Here we just assume
1449 * that if we were the first thread to select on the FD, then
1450 * we'll be the one to clear this flag...
1452 if (valid_set
&& fp
->f_wset
== (void *)wqset
) {
1453 fp
->f_flags
&= ~FP_INSELECT
;
1459 * connect a fileproc to the given wqset, potentially bridging to a waitq
1460 * pointed to indirectly by wq_data
1463 * fp File proc potentially currently in select
1464 * wq_data Pointer to a pointer to a waitq (could be NULL)
1465 * wqset Waitq set to which the fileproc should now belong
1466 * (usually this is the thread's private waitq set)
1469 * proc_fdlock is held
1471 static uint64_t sellinkfp(struct fileproc
*fp
, void **wq_data
, struct waitq_set
*wqset
)
1473 struct waitq
*f_wq
= NULL
;
1475 if ((fp
->f_flags
& FP_INSELECT
) != FP_INSELECT
) {
1477 panic("non-null data:%p on fp:%p not in select?!"
1478 "(wqset:%p)", wq_data
, fp
, wqset
);
1482 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
1484 * The conflict queue requires disabling interrupts, so we
1485 * need to explicitly reserve a link object to avoid a
1486 * panic/assert in the waitq code. Hopefully this extra step
1487 * can be avoided if we can split the waitq structure into
1488 * blocking and linkage sub-structures.
1490 uint64_t reserved_link
= waitq_link_reserve(&select_conflict_queue
);
1491 waitq_link(&select_conflict_queue
, wqset
, WAITQ_SHOULD_LOCK
, &reserved_link
);
1492 waitq_link_release(reserved_link
);
1496 * The wq_data parameter has potentially been set by selrecord called
1497 * from a subsystems fo_select() function. If the subsystem does not
1498 * call selrecord, then wq_data will be NULL
1500 * Use memcpy to get the value into a proper pointer because
1501 * wq_data most likely points to a stack variable that could be
1502 * unaligned on 32-bit systems.
1505 memcpy(&f_wq
, wq_data
, sizeof(f_wq
));
1506 if (!waitq_is_valid(f_wq
))
1510 /* record the first thread's wqset in the fileproc structure */
1512 fp
->f_wset
= (void *)wqset
;
1514 /* handles NULL f_wq */
1515 return waitq_get_prepost_id(f_wq
);
1522 * Parameters: p Process performing the select
1523 * sel The per-thread select context structure
1524 * nfd The number of file descriptors to scan
1525 * retval The per thread system call return area
1526 * sel_pass Which pass this is; allowed values are
1527 * SEL_FIRSTPASS and SEL_SECONDPASS
1528 * wqset The per thread wait queue set
1530 * Returns: 0 Success
1531 * EIO Invalid p->p_fd field XXX Obsolete?
1532 * EBADF One of the files in the bit vector is
1536 selscan(struct proc
*p
, struct _select
*sel
, struct _select_data
* seldata
,
1537 int nfd
, int32_t *retval
, int sel_pass
, struct waitq_set
*wqset
)
1539 struct filedesc
*fdp
= p
->p_fd
;
1542 struct fileproc
*fp
;
1543 int n
= 0; /* count of bits */
1544 int nc
= 0; /* bit vector offset (nc'th bit) */
1545 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1546 u_int32_t
*iptr
, *optr
;
1548 u_int32_t
*ibits
, *obits
;
1549 uint64_t reserved_link
, *rl_ptr
= NULL
;
1551 struct vfs_context context
= *vfs_context_current();
1554 * Problems when reboot; due to MacOSX signal probs
1555 * in Beaker1C ; verify that the p->p_fd is valid
1564 nw
= howmany(nfd
, NFDBITS
);
1566 count
= seldata
->count
;
1575 for (msk
= 0; msk
< 3; msk
++) {
1576 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1577 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1579 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1580 bits
= iptr
[i
/NFDBITS
];
1582 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1585 if (fd
< fdp
->fd_nfiles
)
1586 fp
= fdp
->fd_ofiles
[fd
];
1590 if (fp
== NULL
|| (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1592 * If we abort because of a bad
1593 * fd, let the caller unwind...
1598 if (sel_pass
== SEL_SECONDPASS
) {
1601 selunlinkfp(fp
, seldata
->wqp
[nc
], wqset
);
1603 reserved_link
= waitq_link_reserve((struct waitq
*)wqset
);
1604 rl_ptr
= &reserved_link
;
1605 if (fp
->f_flags
& FP_INSELECT
)
1606 /* someone is already in select on this fp */
1607 fp
->f_flags
|= FP_SELCONFLICT
;
1609 fp
->f_flags
|= FP_INSELECT
;
1612 context
.vc_ucred
= fp
->f_cred
;
1615 * stash this value b/c fo_select may replace
1616 * reserved_link with a pointer to a waitq object
1618 uint64_t rsvd
= reserved_link
;
1620 /* The select; set the bit, if true */
1621 if (fp
->f_ops
&& fp
->f_type
1622 && fo_select(fp
, flag
[msk
], rl_ptr
, &context
)) {
1623 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1626 if (sel_pass
== SEL_FIRSTPASS
) {
1627 waitq_link_release(rsvd
);
1629 * If the fp's supporting selinfo structure was linked
1630 * to this thread's waitq set, then 'reserved_link'
1631 * will have been updated by selrecord to be a pointer
1632 * to the selinfo's waitq.
1634 if (reserved_link
== rsvd
)
1635 rl_ptr
= NULL
; /* fo_select never called selrecord() */
1637 * Hook up the thread's waitq set either to
1638 * the fileproc structure, or to the global
1639 * conflict queue: but only on the first
1642 seldata
->wqp
[nc
] = sellinkfp(fp
, (void **)rl_ptr
, wqset
);
1654 int poll_callback(struct kqueue
*, struct kevent_internal_s
*, void *);
1656 struct poll_continue_args
{
1657 user_addr_t pca_fds
;
1663 poll(struct proc
*p
, struct poll_args
*uap
, int32_t *retval
)
1665 __pthread_testcancel(1);
1666 return(poll_nocancel(p
, (struct poll_nocancel_args
*)uap
, retval
));
1671 poll_nocancel(struct proc
*p
, struct poll_nocancel_args
*uap
, int32_t *retval
)
1673 struct poll_continue_args
*cont
;
1677 int ncoll
, error
= 0;
1678 u_int nfds
= uap
->nfds
;
1684 * This is kinda bogus. We have fd limits, but that is not
1685 * really related to the size of the pollfd array. Make sure
1686 * we let the process use at least FD_SETSIZE entries and at
1687 * least enough for the current limits. We want to be reasonably
1688 * safe, but not overly restrictive.
1690 if (nfds
> OPEN_MAX
||
1691 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& (proc_suser(p
) || nfds
> FD_SETSIZE
)))
1694 kq
= kqueue_alloc(p
);
1698 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1699 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1705 fds
= (struct pollfd
*)&cont
[1];
1706 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1710 if (uap
->timeout
!= -1) {
1713 atv
.tv_sec
= uap
->timeout
/ 1000;
1714 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1715 if (itimerfix(&atv
)) {
1719 getmicrouptime(&rtv
);
1720 timevaladd(&atv
, &rtv
);
1726 /* JMM - all this P_SELECT stuff is bogus */
1728 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1729 for (i
= 0; i
< nfds
; i
++) {
1730 short events
= fds
[i
].events
;
1733 /* per spec, ignore fd values below zero */
1734 if (fds
[i
].fd
< 0) {
1739 /* convert the poll event into a kqueue kevent */
1740 struct kevent_internal_s kev
= {
1742 .flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
,
1743 .udata
= CAST_USER_ADDR_T(&fds
[i
]) };
1745 /* Handle input events */
1746 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
| POLLHUP
)) {
1747 kev
.filter
= EVFILT_READ
;
1748 if (events
& ( POLLPRI
| POLLRDBAND
))
1749 kev
.flags
|= EV_OOBAND
;
1750 kerror
= kevent_register(kq
, &kev
, p
);
1753 /* Handle output events */
1755 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1756 kev
.filter
= EVFILT_WRITE
;
1757 kerror
= kevent_register(kq
, &kev
, p
);
1760 /* Handle BSD extension vnode events */
1762 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1763 kev
.filter
= EVFILT_VNODE
;
1765 if (events
& POLLEXTEND
)
1766 kev
.fflags
|= NOTE_EXTEND
;
1767 if (events
& POLLATTRIB
)
1768 kev
.fflags
|= NOTE_ATTRIB
;
1769 if (events
& POLLNLINK
)
1770 kev
.fflags
|= NOTE_LINK
;
1771 if (events
& POLLWRITE
)
1772 kev
.fflags
|= NOTE_WRITE
;
1773 kerror
= kevent_register(kq
, &kev
, p
);
1777 fds
[i
].revents
= POLLNVAL
;
1783 /* Did we have any trouble registering? */
1787 /* scan for, and possibly wait for, the kevents to trigger */
1788 cont
->pca_fds
= uap
->fds
;
1789 cont
->pca_nfds
= nfds
;
1790 cont
->pca_rfds
= rfds
;
1791 error
= kqueue_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1792 rfds
= cont
->pca_rfds
;
1795 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1796 /* poll is not restarted after signals... */
1797 if (error
== ERESTART
)
1799 if (error
== EWOULDBLOCK
)
1802 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1815 poll_callback(__unused
struct kqueue
*kq
, struct kevent_internal_s
*kevp
, void *data
)
1817 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1818 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1819 short prev_revents
= fds
->revents
;
1822 /* convert the results back into revents */
1823 if (kevp
->flags
& EV_EOF
)
1824 fds
->revents
|= POLLHUP
;
1825 if (kevp
->flags
& EV_ERROR
)
1826 fds
->revents
|= POLLERR
;
1828 switch (kevp
->filter
) {
1830 if (fds
->revents
& POLLHUP
)
1831 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1833 mask
= (POLLIN
| POLLRDNORM
);
1834 if (kevp
->flags
& EV_OOBAND
)
1835 mask
|= (POLLPRI
| POLLRDBAND
);
1837 fds
->revents
|= (fds
->events
& mask
);
1841 if (!(fds
->revents
& POLLHUP
))
1842 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1846 if (kevp
->fflags
& NOTE_EXTEND
)
1847 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1848 if (kevp
->fflags
& NOTE_ATTRIB
)
1849 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1850 if (kevp
->fflags
& NOTE_LINK
)
1851 fds
->revents
|= (fds
->events
& POLLNLINK
);
1852 if (kevp
->fflags
& NOTE_WRITE
)
1853 fds
->revents
|= (fds
->events
& POLLWRITE
);
1857 if (fds
->revents
!= 0 && prev_revents
== 0)
1864 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1873 * Count the number of bits set in the input bit vector, and establish an
1874 * outstanding fp->f_iocount for each of the descriptors which will be in
1875 * use in the select operation.
1877 * Parameters: p The process doing the select
1878 * ibits The input bit vector
1879 * nfd The number of fd's in the vector
1880 * countp Pointer to where to store the bit count
1882 * Returns: 0 Success
1883 * EIO Bad per process open file table
1884 * EBADF One of the bits in the input bit vector
1885 * references an invalid fd
1887 * Implicit: *countp (modified) Count of fd's
1889 * Notes: This function is the first pass under the proc_fdlock() that
1890 * permits us to recognize invalid descriptors in the bit vector;
1891 * the may, however, not remain valid through the drop and
1892 * later reacquisition of the proc_fdlock().
1895 selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *countp
)
1897 struct filedesc
*fdp
= p
->p_fd
;
1900 struct fileproc
*fp
;
1906 int need_wakeup
= 0;
1909 * Problems when reboot; due to MacOSX signal probs
1910 * in Beaker1C ; verify that the p->p_fd is valid
1916 nw
= howmany(nfd
, NFDBITS
);
1919 for (msk
= 0; msk
< 3; msk
++) {
1920 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1921 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1922 bits
= iptr
[i
/NFDBITS
];
1923 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1926 if (fd
< fdp
->fd_nfiles
)
1927 fp
= fdp
->fd_ofiles
[fd
];
1932 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1952 /* Ignore error return; it's already EBADF */
1953 (void)seldrop_locked(p
, ibits
, nfd
, n
, &need_wakeup
, 1);
1958 wakeup(&p
->p_fpdrainwait
);
1967 * Drop outstanding wait queue references set up during selscan(); drop the
1968 * outstanding per fileproc f_iocount() picked up during the selcount().
1970 * Parameters: p Process performing the select
1971 * ibits Input bit bector of fd's
1972 * nfd Number of fd's
1973 * lim Limit to number of vector entries to
1974 * consider, or -1 for "all"
1976 * need_wakeup Pointer to flag to set to do a wakeup
1977 * if f_iocont on any descriptor goes to 0
1979 * Returns: 0 Success
1980 * EBADF One or more fds in the bit vector
1981 * were invalid, but the rest
1982 * were successfully dropped
1984 * Notes: An fd make become bad while the proc_fdlock() is not held,
1985 * if a multithreaded application closes the fd out from under
1986 * the in progress select. In this case, we still have to
1987 * clean up after the set up on the remaining fds.
1990 seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
)
1992 struct filedesc
*fdp
= p
->p_fd
;
1993 int msk
, i
, j
, nc
, fd
;
1995 struct fileproc
*fp
;
2000 uthread_t uth
= get_bsdthread_info(current_thread());
2001 struct _select_data
*seldata
;
2006 * Problems when reboot; due to MacOSX signal probs
2007 * in Beaker1C ; verify that the p->p_fd is valid
2013 nw
= howmany(nfd
, NFDBITS
);
2014 seldata
= &uth
->uu_kevent
.ss_select_data
;
2017 for (msk
= 0; msk
< 3; msk
++) {
2018 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
2019 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
2020 bits
= iptr
[i
/NFDBITS
];
2021 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
2023 fp
= fdp
->fd_ofiles
[fd
];
2025 * If we've already dropped as many as were
2026 * counted/scanned, then we are done.
2028 if ((fromselcount
!= 0) && (++dropcount
> lim
))
2032 * unlink even potentially NULL fileprocs.
2033 * If the FD was closed from under us, we
2034 * still need to cleanup the waitq links!
2037 seldata
->wqp
? seldata
->wqp
[nc
] : 0,
2043 /* skip (now) bad fds */
2049 if (fp
->f_iocount
< 0)
2050 panic("f_iocount overdecrement!");
2052 if (fp
->f_iocount
== 0) {
2054 * The last iocount is responsible for clearing
2055 * selconfict flag - even if we didn't set it -
2056 * and is also responsible for waking up anyone
2057 * waiting on iocounts to drain.
2059 if (fp
->f_flags
& FP_SELCONFLICT
)
2060 fp
->f_flags
&= ~FP_SELCONFLICT
;
2061 if (p
->p_fpdrainwait
) {
2062 p
->p_fpdrainwait
= 0;
2075 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
2078 int need_wakeup
= 0;
2081 error
= seldrop_locked(p
, ibits
, nfd
, nfd
, &need_wakeup
, 0);
2084 wakeup(&p
->p_fpdrainwait
);
2090 * Record a select request.
2093 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void *s_data
)
2095 thread_t cur_act
= current_thread();
2096 struct uthread
* ut
= get_bsdthread_info(cur_act
);
2097 /* on input, s_data points to the 64-bit ID of a reserved link object */
2098 uint64_t *reserved_link
= (uint64_t *)s_data
;
2100 /* need to look at collisions */
2102 /*do not record if this is second pass of select */
2106 if ((sip
->si_flags
& SI_INITED
) == 0) {
2107 waitq_init(&sip
->si_waitq
, SYNC_POLICY_FIFO
| SYNC_POLICY_DISABLE_IRQ
);
2108 sip
->si_flags
|= SI_INITED
;
2109 sip
->si_flags
&= ~SI_CLEAR
;
2112 if (sip
->si_flags
& SI_RECORDED
)
2113 sip
->si_flags
|= SI_COLL
;
2115 sip
->si_flags
&= ~SI_COLL
;
2117 sip
->si_flags
|= SI_RECORDED
;
2118 /* note: this checks for pre-existing linkage */
2119 waitq_link(&sip
->si_waitq
, ut
->uu_wqset
,
2120 WAITQ_SHOULD_LOCK
, reserved_link
);
2123 * Always consume the reserved link.
2124 * We can always call waitq_link_release() safely because if
2125 * waitq_link is successful, it consumes the link and resets the
2126 * value to 0, in which case our call to release becomes a no-op.
2127 * If waitq_link fails, then the following release call will actually
2128 * release the reserved link object.
2130 waitq_link_release(*reserved_link
);
2134 * Use the s_data pointer as an output parameter as well
2135 * This avoids changing the prototype for this function which is
2136 * used by many kexts. We need to surface the waitq object
2137 * associated with the selinfo we just added to the thread's select
2138 * set. New waitq sets do not have back-pointers to set members, so
2139 * the only way to clear out set linkage objects is to go from the
2140 * waitq to the set. We use a memcpy because s_data could be
2141 * pointing to an unaligned value on the stack
2142 * (especially on 32-bit systems)
2144 void *wqptr
= (void *)&sip
->si_waitq
;
2145 memcpy((void *)s_data
, (void *)&wqptr
, sizeof(void *));
2151 selwakeup(struct selinfo
*sip
)
2154 if ((sip
->si_flags
& SI_INITED
) == 0) {
2158 if (sip
->si_flags
& SI_COLL
) {
2160 sip
->si_flags
&= ~SI_COLL
;
2162 /* will not support */
2163 //wakeup((caddr_t)&selwait);
2167 if (sip
->si_flags
& SI_RECORDED
) {
2168 waitq_wakeup64_all(&sip
->si_waitq
, NO_EVENT64
,
2169 THREAD_AWAKENED
, WAITQ_ALL_PRIORITIES
);
2170 sip
->si_flags
&= ~SI_RECORDED
;
2176 selthreadclear(struct selinfo
*sip
)
2180 if ((sip
->si_flags
& SI_INITED
) == 0) {
2183 if (sip
->si_flags
& SI_RECORDED
) {
2185 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
2187 sip
->si_flags
|= SI_CLEAR
;
2188 sip
->si_flags
&= ~SI_INITED
;
2190 wq
= &sip
->si_waitq
;
2193 * Higher level logic may have a handle on this waitq's prepost ID,
2194 * but that's OK because the waitq_deinit will remove/invalidate the
2195 * prepost object (as well as mark the waitq invalid). This de-couples
2196 * us from any callers that may have a handle to this waitq via the
2205 #define DBG_POST 0x10
2206 #define DBG_WATCH 0x11
2207 #define DBG_WAIT 0x12
2208 #define DBG_MOD 0x13
2209 #define DBG_EWAKEUP 0x14
2210 #define DBG_ENQUEUE 0x15
2211 #define DBG_DEQUEUE 0x16
2213 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
2214 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
2215 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
2216 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
2217 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
2218 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
2219 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
2222 #define EVPROCDEQUE(p, evq) do { \
2224 if (evq->ee_flags & EV_QUEUED) { \
2225 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
2226 evq->ee_flags &= ~EV_QUEUED; \
2233 * called upon socket close. deque and free all events for
2234 * the socket... socket must be locked by caller.
2237 evsofree(struct socket
*sp
)
2239 struct eventqelt
*evq
, *next
;
2245 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
2246 next
= evq
->ee_slist
.tqe_next
;
2249 if (evq
->ee_flags
& EV_QUEUED
) {
2250 EVPROCDEQUE(p
, evq
);
2252 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
2259 * called upon pipe close. deque and free all events for
2260 * the pipe... pipe must be locked by caller
2263 evpipefree(struct pipe
*cpipe
)
2265 struct eventqelt
*evq
, *next
;
2268 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
2269 next
= evq
->ee_slist
.tqe_next
;
2272 EVPROCDEQUE(p
, evq
);
2274 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
2281 * enqueue this event if it's not already queued. wakeup
2282 * the proc if we do queue this event to it...
2283 * entered with proc lock held... we drop it before
2284 * doing the wakeup and return in that state
2287 evprocenque(struct eventqelt
*evq
)
2294 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
2298 if (evq
->ee_flags
& EV_QUEUED
) {
2301 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2304 evq
->ee_flags
|= EV_QUEUED
;
2306 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
2310 wakeup(&p
->p_evlist
);
2312 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2317 * pipe lock must be taken by the caller
2320 postpipeevent(struct pipe
*pipep
, int event
)
2323 struct eventqelt
*evq
;
2327 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
2329 for (evq
= pipep
->pipe_evlist
.tqh_first
;
2330 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2332 if (evq
->ee_eventmask
== 0)
2336 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
2339 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
2341 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
2343 if ((evq
->ee_eventmask
& EV_WR
) &&
2344 (MAX(pipep
->pipe_buffer
.size
,PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
2346 if (pipep
->pipe_state
& PIPE_EOF
) {
2347 mask
|= EV_WR
|EV_RESET
;
2351 evq
->ee_req
.er_wcnt
= MAX(pipep
->pipe_buffer
.size
, PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
;
2357 if ((evq
->ee_eventmask
& EV_RE
)) {
2358 mask
|= EV_RE
|EV_RCLOSED
;
2360 if ((evq
->ee_eventmask
& EV_WR
)) {
2361 mask
|= EV_WR
|EV_WCLOSED
;
2370 * disarm... postevents are nops until this event is 'read' via
2371 * waitevent and then re-armed via modwatch
2373 evq
->ee_eventmask
= 0;
2376 * since events are disarmed until after the waitevent
2377 * the ee_req.er_xxxx fields can't change once we've
2378 * inserted this event into the proc queue...
2379 * therefore, the waitevent will see a 'consistent'
2380 * snapshot of the event, even though it won't hold
2381 * the pipe lock, and we're updating the event outside
2382 * of the proc lock, which it will hold
2384 evq
->ee_req
.er_eventbits
|= mask
;
2386 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2391 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2396 * given either a sockbuf or a socket run down the
2397 * event list and queue ready events found...
2398 * the socket must be locked by the caller
2401 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2404 struct eventqelt
*evq
;
2412 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2414 for (evq
= sp
->so_evlist
.tqh_first
;
2415 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2417 if (evq
->ee_eventmask
== 0)
2421 /* ready for reading:
2422 - byte cnt >= receive low water mark
2423 - read-half of conn closed
2424 - conn pending for listening sock
2425 - socket error pending
2428 - byte cnt avail >= send low water mark
2429 - write half of conn closed
2430 - socket error pending
2431 - non-blocking conn completed successfully
2435 - sock at out of band mark
2438 switch (event
& EV_DMASK
) {
2441 if ((evq
->ee_eventmask
& EV_EX
)) {
2442 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2443 mask
|= EV_EX
|EV_OOB
;
2447 case EV_RWBYTES
|EV_OOB
:
2448 if ((evq
->ee_eventmask
& EV_EX
)) {
2449 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2450 mask
|= EV_EX
|EV_OOB
;
2453 * fall into the next case
2456 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2457 /* for AFP/OT purposes; may go away in future */
2458 if ((SOCK_DOM(sp
) == PF_INET
||
2459 SOCK_DOM(sp
) == PF_INET6
) &&
2460 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2461 (sp
->so_error
== ECONNREFUSED
||
2462 sp
->so_error
== ECONNRESET
)) {
2463 if (sp
->so_pcb
== NULL
||
2464 sotoinpcb(sp
)->inp_state
==
2466 (tp
= sototcpcb(sp
)) == NULL
||
2467 tp
->t_state
== TCPS_CLOSED
) {
2468 mask
|= EV_RE
|EV_RESET
;
2473 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2475 if (sp
->so_state
& SS_CANTRCVMORE
) {
2480 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2481 /* for AFP/OT purposes; may go away in future */
2482 if ((SOCK_DOM(sp
) == PF_INET
||
2483 SOCK_DOM(sp
) == PF_INET6
) &&
2484 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2485 (sp
->so_error
== ECONNREFUSED
||
2486 sp
->so_error
== ECONNRESET
)) {
2487 if (sp
->so_pcb
== NULL
||
2488 sotoinpcb(sp
)->inp_state
==
2490 (tp
= sototcpcb(sp
)) == NULL
||
2491 tp
->t_state
== TCPS_CLOSED
) {
2492 mask
|= EV_WR
|EV_RESET
;
2497 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2502 if ((evq
->ee_eventmask
& EV_RE
)) {
2503 mask
|= EV_RE
|EV_RCONN
;
2504 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2509 if ((evq
->ee_eventmask
& EV_WR
)) {
2510 mask
|= EV_WR
|EV_WCONN
;
2515 if ((evq
->ee_eventmask
& EV_RE
)) {
2516 mask
|= EV_RE
|EV_RCLOSED
;
2521 if ((evq
->ee_eventmask
& EV_WR
)) {
2522 mask
|= EV_WR
|EV_WCLOSED
;
2527 if (evq
->ee_eventmask
& EV_RE
) {
2528 mask
|= EV_RE
|EV_FIN
;
2534 if (evq
->ee_eventmask
& EV_RE
) {
2535 mask
|= EV_RE
| event
;
2537 if (evq
->ee_eventmask
& EV_WR
) {
2538 mask
|= EV_WR
| event
;
2543 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2547 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2551 * disarm... postevents are nops until this event is 'read' via
2552 * waitevent and then re-armed via modwatch
2554 evq
->ee_eventmask
= 0;
2557 * since events are disarmed until after the waitevent
2558 * the ee_req.er_xxxx fields can't change once we've
2559 * inserted this event into the proc queue...
2560 * since waitevent can't see this event until we
2561 * enqueue it, waitevent will see a 'consistent'
2562 * snapshot of the event, even though it won't hold
2563 * the socket lock, and we're updating the event outside
2564 * of the proc lock, which it will hold
2566 evq
->ee_req
.er_eventbits
|= mask
;
2571 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2573 #endif /* SOCKETS */
2577 * watchevent system call. user passes us an event to watch
2578 * for. we malloc an event object, initialize it, and queue
2579 * it to the open socket. when the event occurs, postevent()
2580 * will enque it back to our proc where we can retrieve it
2583 * should this prevent duplicate events on same socket?
2586 * ENOMEM No memory for operation
2590 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2592 struct eventqelt
*evq
= (struct eventqelt
*)0;
2593 struct eventqelt
*np
= NULL
;
2594 struct eventreq64
*erp
;
2595 struct fileproc
*fp
= NULL
;
2598 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2600 // get a qelt and fill with users req
2601 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2607 // get users request pkt
2609 if (IS_64BIT_PROCESS(p
)) {
2610 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2612 struct eventreq32 er32
;
2614 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2617 * the user only passes in the
2618 * er_type, er_handle and er_data...
2619 * the other fields are initialized
2620 * below, so don't bother to copy
2622 erp
->er_type
= er32
.er_type
;
2623 erp
->er_handle
= er32
.er_handle
;
2624 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2629 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2633 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2635 // validate, freeing qelt if errors
2639 if (erp
->er_type
!= EV_FD
) {
2641 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2644 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2645 socket_lock((struct socket
*)fp
->f_data
, 1);
2646 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2647 #endif /* SOCKETS */
2648 } else if (fp
->f_type
== DTYPE_PIPE
) {
2649 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2650 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2652 fp_drop(p
, erp
->er_handle
, fp
, 1);
2660 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2665 * only allow one watch per file per proc
2667 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2668 if (np
->ee_proc
== p
) {
2670 if (fp
->f_type
== DTYPE_SOCKET
)
2671 socket_unlock((struct socket
*)fp
->f_data
, 1);
2673 #endif /* SOCKETS */
2674 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2675 fp_drop(p
, erp
->er_handle
, fp
, 0);
2678 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2682 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2684 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2688 if (fp
->f_type
== DTYPE_SOCKET
) {
2689 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2690 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2692 socket_unlock((struct socket
*)fp
->f_data
, 1);
2694 #endif /* SOCKETS */
2696 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2697 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2699 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2701 fp_drop_event(p
, erp
->er_handle
, fp
);
2703 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2710 * waitevent system call.
2711 * grabs the next waiting event for this proc and returns
2712 * it. if no events, user can request to sleep with timeout
2713 * or without or poll mode
2714 * ((tv != NULL && interval == 0) || tv == -1)
2717 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2720 struct eventqelt
*evq
;
2721 struct eventreq64
*erp
;
2722 uint64_t abstime
, interval
;
2723 boolean_t fast_poll
= FALSE
;
2725 struct eventreq64 er64
;
2726 struct eventreq32 er32
;
2734 * check for fast poll method
2736 if (IS_64BIT_PROCESS(p
)) {
2737 if (uap
->tv
== (user_addr_t
)-1)
2739 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2742 if (fast_poll
== TRUE
) {
2743 if (p
->p_evlist
.tqh_first
== NULL
) {
2744 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2754 if (IS_64BIT_PROCESS(p
)) {
2755 struct user64_timeval atv64
;
2756 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2757 /* Loses resolution - assume timeout < 68 years */
2758 atv
.tv_sec
= atv64
.tv_sec
;
2759 atv
.tv_usec
= atv64
.tv_usec
;
2761 struct user32_timeval atv32
;
2762 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2763 atv
.tv_sec
= atv32
.tv_sec
;
2764 atv
.tv_usec
= atv32
.tv_usec
;
2769 if (itimerfix(&atv
)) {
2773 interval
= tvtoabstime(&atv
);
2775 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2779 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2781 * found one... make a local copy while it's still on the queue
2782 * to prevent it from changing while in the midst of copying
2783 * don't want to hold the proc lock across a copyout because
2784 * it might block on a page fault at the target in user space
2788 if (IS_64BIT_PROCESS(p
))
2789 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2791 uer
.er32
.er_type
= erp
->er_type
;
2792 uer
.er32
.er_handle
= erp
->er_handle
;
2793 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2794 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2795 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2796 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2797 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2799 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2801 evq
->ee_flags
&= ~EV_QUEUED
;
2805 if (IS_64BIT_PROCESS(p
))
2806 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2808 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2810 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2811 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2815 if (uap
->tv
&& interval
== 0) {
2817 *retval
= 1; // poll failed
2819 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2823 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2827 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2829 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2831 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2835 if (error
== ERESTART
)
2837 if (error
== EWOULDBLOCK
) {
2844 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2850 * modwatch system call. user passes in event to modify.
2851 * if we find it we reset the event bits and que/deque event
2855 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2857 struct eventreq64 er
;
2858 struct eventreq64
*erp
= &er
;
2859 struct eventqelt
*evq
= NULL
; /* protected by error return */
2861 struct fileproc
*fp
;
2864 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2867 * get user's request pkt
2868 * just need the er_type and er_handle which sit above the
2869 * problematic er_data (32/64 issue)... so only copy in
2872 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2873 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2878 if (erp
->er_type
!= EV_FD
) {
2880 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2883 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2884 socket_lock((struct socket
*)fp
->f_data
, 1);
2885 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2886 #endif /* SOCKETS */
2887 } else if (fp
->f_type
== DTYPE_PIPE
) {
2888 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2889 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2891 fp_drop(p
, erp
->er_handle
, fp
, 1);
2897 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2901 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2902 fp
->f_flags
&= ~FP_WAITEVENT
;
2906 // locate event if possible
2907 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2908 if (evq
->ee_proc
== p
)
2913 if (fp
->f_type
== DTYPE_SOCKET
)
2914 socket_unlock((struct socket
*)fp
->f_data
, 1);
2916 #endif /* SOCKETS */
2917 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2918 fp_drop(p
, erp
->er_handle
, fp
, 0);
2919 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2922 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2924 if (uap
->u_eventmask
== EV_RM
) {
2925 EVPROCDEQUE(p
, evq
);
2928 if (fp
->f_type
== DTYPE_SOCKET
) {
2929 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2930 socket_unlock((struct socket
*)fp
->f_data
, 1);
2932 #endif /* SOCKETS */
2934 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2935 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2937 fp_drop(p
, erp
->er_handle
, fp
, 0);
2939 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2942 switch (uap
->u_eventmask
& EV_MASK
) {
2960 case EV_EX
|EV_RE
|EV_WR
:
2961 flag
= EV_OOB
|EV_RWBYTES
;
2966 if (fp
->f_type
== DTYPE_SOCKET
)
2967 socket_unlock((struct socket
*)fp
->f_data
, 1);
2969 #endif /* SOCKETS */
2970 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2971 fp_drop(p
, erp
->er_handle
, fp
, 0);
2972 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2976 * since we're holding the socket/pipe lock, the event
2977 * cannot go from the unqueued state to the queued state
2978 * however, it can go from the queued state to the unqueued state
2979 * since that direction is protected by the proc_lock...
2980 * so do a quick check for EV_QUEUED w/o holding the proc lock
2981 * since by far the common case will be NOT EV_QUEUED, this saves
2982 * us taking the proc_lock the majority of the time
2984 if (evq
->ee_flags
& EV_QUEUED
) {
2986 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2988 EVPROCDEQUE(p
, evq
);
2991 * while the event is off the proc queue and
2992 * we're holding the socket/pipe lock
2993 * it's safe to update these fields...
2995 evq
->ee_req
.er_eventbits
= 0;
2996 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2999 if (fp
->f_type
== DTYPE_SOCKET
) {
3000 postevent((struct socket
*)fp
->f_data
, 0, flag
);
3001 socket_unlock((struct socket
*)fp
->f_data
, 1);
3003 #endif /* SOCKETS */
3005 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
3006 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3008 fp_drop(p
, erp
->er_handle
, fp
, 0);
3009 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
3013 /* this routine is called from the close of fd with proc_fdlock held */
3015 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
3017 struct eventqelt
*evq
;
3020 fp
->f_flags
&= ~FP_WAITEVENT
;
3023 if (fp
->f_type
== DTYPE_SOCKET
) {
3024 socket_lock((struct socket
*)fp
->f_data
, 1);
3025 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
3027 #endif /* SOCKETS */
3028 if (fp
->f_type
== DTYPE_PIPE
) {
3029 PIPE_LOCK((struct pipe
*)fp
->f_data
);
3030 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
3038 // locate event if possible
3039 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
3040 if (evq
->ee_proc
== p
)
3045 if (fp
->f_type
== DTYPE_SOCKET
)
3046 socket_unlock((struct socket
*)fp
->f_data
, 1);
3048 #endif /* SOCKETS */
3049 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3055 EVPROCDEQUE(p
, evq
);
3058 if (fp
->f_type
== DTYPE_SOCKET
) {
3059 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
3060 socket_unlock((struct socket
*)fp
->f_data
, 1);
3062 #endif /* SOCKETS */
3064 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
3065 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3078 * Description: Get the host UUID from IOKit and return it to user space.
3080 * Parameters: uuid_buf Pointer to buffer to receive UUID
3081 * timeout Timespec for timout
3082 * spi SPI, skip sandbox check (temporary)
3084 * Returns: 0 Success
3085 * EWOULDBLOCK Timeout is too short
3086 * copyout:EFAULT Bad user buffer
3087 * mac_system_check_info:EPERM Client not allowed to perform this operation
3089 * Notes: A timeout seems redundant, since if it's tolerable to not
3090 * have a system UUID in hand, then why ask for one?
3093 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
3097 mach_timespec_t mach_ts
; /* for IOKit call */
3098 __darwin_uuid_t uuid_kern
; /* for IOKit call */
3103 /* Convert the 32/64 bit timespec into a mach_timespec_t */
3104 if ( proc_is64bit(p
) ) {
3105 struct user64_timespec ts
;
3106 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
3109 mach_ts
.tv_sec
= ts
.tv_sec
;
3110 mach_ts
.tv_nsec
= ts
.tv_nsec
;
3112 struct user32_timespec ts
;
3113 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
3116 mach_ts
.tv_sec
= ts
.tv_sec
;
3117 mach_ts
.tv_nsec
= ts
.tv_nsec
;
3120 /* Call IOKit with the stack buffer to get the UUID */
3121 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
3124 * If we get it, copy out the data to the user buffer; note that a
3125 * uuid_t is an array of characters, so this is size invariant for
3128 if (kret
== KERN_SUCCESS
) {
3129 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
3131 error
= EWOULDBLOCK
;
3140 * Description: Omnibus system call for ledger operations
3143 ledger(struct proc
*p
, struct ledger_args
*args
, __unused
int32_t *retval
)
3148 int rval
, pid
, len
, error
;
3150 struct ledger_limit_args lla
;
3155 /* Finish copying in the necessary args before taking the proc lock */
3158 if (args
->cmd
== LEDGER_ENTRY_INFO
)
3159 error
= copyin(args
->arg3
, (char *)&len
, sizeof (len
));
3160 else if (args
->cmd
== LEDGER_TEMPLATE_INFO
)
3161 error
= copyin(args
->arg2
, (char *)&len
, sizeof (len
));
3163 else if (args
->cmd
== LEDGER_LIMIT
)
3164 error
= copyin(args
->arg2
, (char *)&lla
, sizeof (lla
));
3172 if (args
->cmd
!= LEDGER_TEMPLATE_INFO
) {
3174 proc
= proc_find(pid
);
3179 error
= mac_proc_check_ledger(p
, proc
, args
->cmd
);
3189 switch (args
->cmd
) {
3191 case LEDGER_LIMIT
: {
3192 if (!kauth_cred_issuser(kauth_cred_get()))
3194 rval
= ledger_limit(task
, &lla
);
3200 struct ledger_info info
;
3202 rval
= ledger_info(task
, &info
);
3205 rval
= copyout(&info
, args
->arg2
,
3210 case LEDGER_ENTRY_INFO
: {
3214 rval
= ledger_get_task_entry_info_multiple(task
, &buf
, &len
);
3216 if ((rval
== 0) && (len
> 0)) {
3217 sz
= len
* sizeof (struct ledger_entry_info
);
3218 rval
= copyout(buf
, args
->arg2
, sz
);
3222 rval
= copyout(&len
, args
->arg3
, sizeof (len
));
3226 case LEDGER_TEMPLATE_INFO
: {
3230 rval
= ledger_template_info(&buf
, &len
);
3231 if ((rval
== 0) && (len
> 0)) {
3232 sz
= len
* sizeof (struct ledger_template_info
);
3233 rval
= copyout(buf
, args
->arg1
, sz
);
3237 rval
= copyout(&len
, args
->arg2
, sizeof (len
));
3249 telemetry(__unused
struct proc
*p
, struct telemetry_args
*args
, __unused
int32_t *retval
)
3253 switch (args
->cmd
) {
3254 #if CONFIG_TELEMETRY
3255 case TELEMETRY_CMD_TIMER_EVENT
:
3256 error
= telemetry_timer_event(args
->deadline
, args
->interval
, args
->leeway
);
3258 #endif /* CONFIG_TELEMETRY */
3259 case TELEMETRY_CMD_VOUCHER_NAME
:
3260 if (thread_set_voucher_name((mach_port_name_t
)args
->deadline
))
3272 #if defined(DEVELOPMENT) || defined(DEBUG)
3273 #if CONFIG_WAITQ_DEBUG
3274 static uint64_t g_wqset_num
= 0;
3277 struct waitq_set
*wqset
;
3280 static queue_head_t g_wqset_list
;
3281 static struct waitq_set
*g_waitq_set
= NULL
;
3283 static inline struct waitq_set
*sysctl_get_wqset(int idx
)
3285 struct g_wqset
*gwqs
;
3288 queue_init(&g_wqset_list
);
3290 /* don't bother with locks: this is test-only code! */
3291 qe_foreach_element(gwqs
, &g_wqset_list
, link
) {
3292 if ((int)(wqset_id(gwqs
->wqset
) & 0xffffffff) == idx
)
3296 /* allocate a new one */
3298 gwqs
= (struct g_wqset
*)kalloc(sizeof(*gwqs
));
3299 assert(gwqs
!= NULL
);
3301 gwqs
->wqset
= waitq_set_alloc(SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
|SYNC_POLICY_DISABLE_IRQ
);
3302 enqueue_tail(&g_wqset_list
, &gwqs
->link
);
3303 printf("[WQ]: created new waitq set 0x%llx\n", wqset_id(gwqs
->wqset
));
3308 #define MAX_GLOBAL_TEST_QUEUES 64
3309 static int g_wq_init
= 0;
3310 static struct waitq g_wq
[MAX_GLOBAL_TEST_QUEUES
];
3312 static inline struct waitq
*global_test_waitq(int idx
)
3319 for (int i
= 0; i
< MAX_GLOBAL_TEST_QUEUES
; i
++)
3320 waitq_init(&g_wq
[i
], SYNC_POLICY_FIFO
|SYNC_POLICY_DISABLE_IRQ
);
3323 return &g_wq
[idx
% MAX_GLOBAL_TEST_QUEUES
];
3326 static int sysctl_waitq_wakeup_one SYSCTL_HANDLER_ARGS
3328 #pragma unused(oidp, arg1, arg2)
3331 struct waitq
*waitq
;
3333 int64_t event64
= 0;
3335 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3340 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3343 index
= (int)((-event64
) & 0xffffffff);
3344 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3347 index
= (int)event64
;
3348 waitq
= global_test_waitq(index
);
3353 printf("[WQ]: Waking one thread on waitq [%d] event:0x%llx\n",
3355 kr
= waitq_wakeup64_one(waitq
, (event64_t
)event64
, THREAD_AWAKENED
,
3356 WAITQ_ALL_PRIORITIES
);
3357 printf("[WQ]: \tkr=%d\n", kr
);
3359 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3361 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wakeup_one
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3362 0, 0, sysctl_waitq_wakeup_one
, "Q", "wakeup one thread waiting on given event");
3365 static int sysctl_waitq_wakeup_all SYSCTL_HANDLER_ARGS
3367 #pragma unused(oidp, arg1, arg2)
3370 struct waitq
*waitq
;
3372 int64_t event64
= 0;
3374 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3379 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3382 index
= (int)((-event64
) & 0xffffffff);
3383 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3386 index
= (int)event64
;
3387 waitq
= global_test_waitq(index
);
3392 printf("[WQ]: Waking all threads on waitq [%d] event:0x%llx\n",
3394 kr
= waitq_wakeup64_all(waitq
, (event64_t
)event64
,
3395 THREAD_AWAKENED
, WAITQ_ALL_PRIORITIES
);
3396 printf("[WQ]: \tkr=%d\n", kr
);
3398 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3400 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wakeup_all
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3401 0, 0, sysctl_waitq_wakeup_all
, "Q", "wakeup all threads waiting on given event");
3404 static int sysctl_waitq_wait SYSCTL_HANDLER_ARGS
3406 #pragma unused(oidp, arg1, arg2)
3409 struct waitq
*waitq
;
3411 int64_t event64
= 0;
3413 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3418 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3421 index
= (int)((-event64
) & 0xffffffff);
3422 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3425 index
= (int)event64
;
3426 waitq
= global_test_waitq(index
);
3431 printf("[WQ]: Current thread waiting on waitq [%d] event:0x%llx\n",
3433 kr
= waitq_assert_wait64(waitq
, (event64_t
)event64
, THREAD_INTERRUPTIBLE
, 0);
3434 if (kr
== THREAD_WAITING
)
3435 thread_block(THREAD_CONTINUE_NULL
);
3436 printf("[WQ]: \tWoke Up: kr=%d\n", kr
);
3438 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3440 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wait
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3441 0, 0, sysctl_waitq_wait
, "Q", "start waiting on given event");
3444 static int sysctl_wqset_select SYSCTL_HANDLER_ARGS
3446 #pragma unused(oidp, arg1, arg2)
3448 struct waitq_set
*wqset
;
3449 uint64_t event64
= 0;
3451 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3458 wqset
= sysctl_get_wqset((int)(event64
& 0xffffffff));
3459 g_waitq_set
= wqset
;
3461 event64
= wqset_id(wqset
);
3462 printf("[WQ]: selected wqset 0x%llx\n", event64
);
3466 event64
= wqset_id(g_waitq_set
);
3468 event64
= (uint64_t)(-1);
3470 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3472 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_select
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3473 0, 0, sysctl_wqset_select
, "Q", "select/create a global waitq set");
3476 static int sysctl_waitq_link SYSCTL_HANDLER_ARGS
3478 #pragma unused(oidp, arg1, arg2)
3481 struct waitq
*waitq
;
3482 struct waitq_set
*wqset
;
3484 uint64_t reserved_link
= 0;
3485 int64_t event64
= 0;
3487 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3492 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3495 g_waitq_set
= sysctl_get_wqset(1);
3496 wqset
= g_waitq_set
;
3499 struct waitq_set
*tmp
;
3500 index
= (int)((-event64
) & 0xffffffff);
3501 tmp
= sysctl_get_wqset(index
);
3504 waitq
= wqset_waitq(tmp
);
3507 index
= (int)event64
;
3508 waitq
= global_test_waitq(index
);
3511 printf("[WQ]: linking waitq [%d] to global wqset (0x%llx)\n",
3512 index
, wqset_id(wqset
));
3513 reserved_link
= waitq_link_reserve(waitq
);
3514 kr
= waitq_link(waitq
, wqset
, WAITQ_SHOULD_LOCK
, &reserved_link
);
3515 waitq_link_release(reserved_link
);
3517 printf("[WQ]: \tkr=%d\n", kr
);
3520 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3522 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_link
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3523 0, 0, sysctl_waitq_link
, "Q", "link global waitq to test waitq set");
3526 static int sysctl_waitq_unlink SYSCTL_HANDLER_ARGS
3528 #pragma unused(oidp, arg1, arg2)
3531 struct waitq
*waitq
;
3532 struct waitq_set
*wqset
;
3534 uint64_t event64
= 0;
3536 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3541 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3544 g_waitq_set
= sysctl_get_wqset(1);
3545 wqset
= g_waitq_set
;
3547 index
= (int)event64
;
3548 waitq
= global_test_waitq(index
);
3550 printf("[WQ]: unlinking waitq [%d] from global wqset (0x%llx)\n",
3551 index
, wqset_id(wqset
));
3553 kr
= waitq_unlink(waitq
, wqset
);
3554 printf("[WQ]: \tkr=%d\n", kr
);
3556 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3558 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_unlink
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3559 0, 0, sysctl_waitq_unlink
, "Q", "unlink global waitq from test waitq set");
3562 static int sysctl_waitq_clear_prepost SYSCTL_HANDLER_ARGS
3564 #pragma unused(oidp, arg1, arg2)
3565 struct waitq
*waitq
;
3566 uint64_t event64
= 0;
3569 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3574 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3576 index
= (int)event64
;
3577 waitq
= global_test_waitq(index
);
3579 printf("[WQ]: clearing prepost on waitq [%d]\n", index
);
3580 waitq_clear_prepost(waitq
);
3582 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3584 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_clear_prepost
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3585 0, 0, sysctl_waitq_clear_prepost
, "Q", "clear prepost on given waitq");
3588 static int sysctl_wqset_unlink_all SYSCTL_HANDLER_ARGS
3590 #pragma unused(oidp, arg1, arg2)
3592 struct waitq_set
*wqset
;
3594 uint64_t event64
= 0;
3596 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3601 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3604 g_waitq_set
= sysctl_get_wqset(1);
3605 wqset
= g_waitq_set
;
3607 printf("[WQ]: unlinking all queues from global wqset (0x%llx)\n",
3610 kr
= waitq_set_unlink_all(wqset
);
3611 printf("[WQ]: \tkr=%d\n", kr
);
3613 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3615 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_unlink_all
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3616 0, 0, sysctl_wqset_unlink_all
, "Q", "unlink all queues from test waitq set");
3619 static int sysctl_wqset_clear_preposts SYSCTL_HANDLER_ARGS
3621 #pragma unused(oidp, arg1, arg2)
3622 struct waitq_set
*wqset
= NULL
;
3623 uint64_t event64
= 0;
3626 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3633 index
= (int)((event64
) & 0xffffffff);
3634 wqset
= sysctl_get_wqset(index
);
3635 assert(wqset
!= NULL
);
3637 printf("[WQ]: clearing preposts on wqset 0x%llx\n", wqset_id(wqset
));
3638 waitq_set_clear_preposts(wqset
);
3642 event64
= wqset_id(wqset
);
3644 event64
= (uint64_t)(-1);
3646 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3648 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_clear_preposts
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3649 0, 0, sysctl_wqset_clear_preposts
, "Q", "clear preposts on given waitq set");
3651 #endif /* CONFIG_WAITQ_DEBUG */
3652 #endif /* defined(DEVELOPMENT) || defined(DEBUG) */