xref: /xnu-8792.41.9/bsd/sys/pipe.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2004-2020 Apple Computer, Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions /*
29*5c2921b0SApple OSS Distributions  * Copyright (c) 1996 John S. Dyson
30*5c2921b0SApple OSS Distributions  * All rights reserved.
31*5c2921b0SApple OSS Distributions  *
32*5c2921b0SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*5c2921b0SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*5c2921b0SApple OSS Distributions  * are met:
35*5c2921b0SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*5c2921b0SApple OSS Distributions  *    notice immediately at the beginning of the file, without modification,
37*5c2921b0SApple OSS Distributions  *    this list of conditions, and the following disclaimer.
38*5c2921b0SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
39*5c2921b0SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
40*5c2921b0SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
41*5c2921b0SApple OSS Distributions  * 3. Absolutely no warranty of function or purpose is made by the author
42*5c2921b0SApple OSS Distributions  *    John S. Dyson.
43*5c2921b0SApple OSS Distributions  * 4. This work was done expressly for inclusion into FreeBSD.  Other use
44*5c2921b0SApple OSS Distributions  *    is allowed if this notation is included.
45*5c2921b0SApple OSS Distributions  * 5. Modifications may be freely made to this file if the above conditions
46*5c2921b0SApple OSS Distributions  *    are met.
47*5c2921b0SApple OSS Distributions  *
48*5c2921b0SApple OSS Distributions  * $FreeBSD: src/sys/sys/pipe.h,v 1.24 2003/08/13 20:01:38 alc Exp $
49*5c2921b0SApple OSS Distributions  */
50*5c2921b0SApple OSS Distributions /*
51*5c2921b0SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
52*5c2921b0SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
53*5c2921b0SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
54*5c2921b0SApple OSS Distributions  * Version 2.0.
55*5c2921b0SApple OSS Distributions  */
56*5c2921b0SApple OSS Distributions 
57*5c2921b0SApple OSS Distributions #ifndef _SYS_PIPE_H_
58*5c2921b0SApple OSS Distributions #define _SYS_PIPE_H_
59*5c2921b0SApple OSS Distributions 
60*5c2921b0SApple OSS Distributions #ifdef  KERNEL
61*5c2921b0SApple OSS Distributions #include <libkern/locks.h>
62*5c2921b0SApple OSS Distributions #include <os/base.h>
63*5c2921b0SApple OSS Distributions #endif
64*5c2921b0SApple OSS Distributions #include <sys/queue.h>                  /* for TAILQ macros */
65*5c2921b0SApple OSS Distributions #include <sys/ev.h>
66*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
67*5c2921b0SApple OSS Distributions #include <sys/_types/_caddr_t.h>
68*5c2921b0SApple OSS Distributions #include <sys/_types/_u_int.h>
69*5c2921b0SApple OSS Distributions 
70*5c2921b0SApple OSS Distributions /*
71*5c2921b0SApple OSS Distributions  * Pipe buffer size, keep moderate in value, pipes take kva space.
72*5c2921b0SApple OSS Distributions  */
73*5c2921b0SApple OSS Distributions #ifndef PIPE_SIZE
74*5c2921b0SApple OSS Distributions #define PIPE_SIZE       16384
75*5c2921b0SApple OSS Distributions #endif
76*5c2921b0SApple OSS Distributions 
77*5c2921b0SApple OSS Distributions #define PIPE_KVAMAX     (1024 * 1024 * 16)
78*5c2921b0SApple OSS Distributions 
79*5c2921b0SApple OSS Distributions #ifndef BIG_PIPE_SIZE
80*5c2921b0SApple OSS Distributions #define BIG_PIPE_SIZE   (64*1024)
81*5c2921b0SApple OSS Distributions #endif
82*5c2921b0SApple OSS Distributions 
83*5c2921b0SApple OSS Distributions #ifndef SMALL_PIPE_SIZE
84*5c2921b0SApple OSS Distributions #define SMALL_PIPE_SIZE PAGE_SIZE
85*5c2921b0SApple OSS Distributions #endif
86*5c2921b0SApple OSS Distributions 
87*5c2921b0SApple OSS Distributions /*
88*5c2921b0SApple OSS Distributions  * PIPE_MINDIRECT MUST be smaller than PIPE_SIZE and MUST be bigger
89*5c2921b0SApple OSS Distributions  * than PIPE_BUF.
90*5c2921b0SApple OSS Distributions  */
91*5c2921b0SApple OSS Distributions #ifndef PIPE_MINDIRECT
92*5c2921b0SApple OSS Distributions #define PIPE_MINDIRECT  8192
93*5c2921b0SApple OSS Distributions #endif
94*5c2921b0SApple OSS Distributions 
95*5c2921b0SApple OSS Distributions #define PIPENPAGES      (BIG_PIPE_SIZE / PAGE_SIZE + 1)
96*5c2921b0SApple OSS Distributions 
97*5c2921b0SApple OSS Distributions /*
98*5c2921b0SApple OSS Distributions  * Pipe buffer information.
99*5c2921b0SApple OSS Distributions  * Separate in, out, cnt are used to simplify calculations.
100*5c2921b0SApple OSS Distributions  * Buffered write is active when the buffer.cnt field is set.
101*5c2921b0SApple OSS Distributions  */
102*5c2921b0SApple OSS Distributions struct pipebuf {
103*5c2921b0SApple OSS Distributions 	u_int   cnt;            /* number of chars currently in buffer */
104*5c2921b0SApple OSS Distributions 	u_int   in;             /* in pointer */
105*5c2921b0SApple OSS Distributions 	u_int   out;            /* out pointer */
106*5c2921b0SApple OSS Distributions 	u_int   size;           /* size of buffer */
107*5c2921b0SApple OSS Distributions #if KERNEL
108*5c2921b0SApple OSS Distributions 	caddr_t OS_PTRAUTH_SIGNED_PTR("pipe.buffer") buffer; /* kva of buffer */
109*5c2921b0SApple OSS Distributions #else
110*5c2921b0SApple OSS Distributions 	caddr_t buffer; /* kva of buffer */
111*5c2921b0SApple OSS Distributions #endif /* KERNEL */
112*5c2921b0SApple OSS Distributions };
113*5c2921b0SApple OSS Distributions 
114*5c2921b0SApple OSS Distributions 
115*5c2921b0SApple OSS Distributions #ifdef PIPE_DIRECT
116*5c2921b0SApple OSS Distributions /*
117*5c2921b0SApple OSS Distributions  * Information to support direct transfers between processes for pipes.
118*5c2921b0SApple OSS Distributions  */
119*5c2921b0SApple OSS Distributions struct pipemapping {
120*5c2921b0SApple OSS Distributions 	vm_offset_t     kva;            /* kernel virtual address */
121*5c2921b0SApple OSS Distributions 	vm_size_t       cnt;            /* number of chars in buffer */
122*5c2921b0SApple OSS Distributions 	vm_size_t       pos;            /* current position of transfer */
123*5c2921b0SApple OSS Distributions 	int             npages;         /* number of pages */
124*5c2921b0SApple OSS Distributions 	vm_page_t       ms[PIPENPAGES]; /* pages in source process */
125*5c2921b0SApple OSS Distributions };
126*5c2921b0SApple OSS Distributions #endif
127*5c2921b0SApple OSS Distributions 
128*5c2921b0SApple OSS Distributions /*
129*5c2921b0SApple OSS Distributions  * Bits in pipe_state.
130*5c2921b0SApple OSS Distributions  */
131*5c2921b0SApple OSS Distributions #define PIPE_ASYNC      0x004   /* Async? I/O. */
132*5c2921b0SApple OSS Distributions #define PIPE_WANTR      0x008   /* Reader wants some characters. */
133*5c2921b0SApple OSS Distributions #define PIPE_WANTW      0x010   /* Writer wants space to put characters. */
134*5c2921b0SApple OSS Distributions #define PIPE_WANT       0x020   /* Pipe is wanted to be run-down. */
135*5c2921b0SApple OSS Distributions // was  PIPE_SEL        0x040   /* Pipe has a select active. */
136*5c2921b0SApple OSS Distributions #define PIPE_EOF        0x080   /* Pipe is in EOF condition. */
137*5c2921b0SApple OSS Distributions #define PIPE_LOCKFL     0x100   /* Process has exclusive access to pointers/data. */
138*5c2921b0SApple OSS Distributions #define PIPE_LWANT      0x200   /* Process wants exclusive access to pointers/data. */
139*5c2921b0SApple OSS Distributions #define PIPE_DIRECTW    0x400   /* Pipe direct write active. */
140*5c2921b0SApple OSS Distributions #define PIPE_DIRECTOK   0x800   /* Direct mode ok. */
141*5c2921b0SApple OSS Distributions // was  PIPE_KNOTE      0x1000
142*5c2921b0SApple OSS Distributions #define PIPE_DRAIN      0x2000  /* Waiting for I/O to drop for a close.  Treated like EOF;
143*5c2921b0SApple OSS Distributions 	                         *       only separate for easier debugging. */
144*5c2921b0SApple OSS Distributions #define PIPE_WSELECT    0x4000  /* Some thread has done an FWRITE select on the pipe */
145*5c2921b0SApple OSS Distributions #define PIPE_DEAD       0x8000  /* Pipe is dead and needs garbage collection */
146*5c2921b0SApple OSS Distributions 
147*5c2921b0SApple OSS Distributions #ifdef  KERNEL
148*5c2921b0SApple OSS Distributions 
149*5c2921b0SApple OSS Distributions struct label;
150*5c2921b0SApple OSS Distributions 
151*5c2921b0SApple OSS Distributions /*
152*5c2921b0SApple OSS Distributions  * Per-pipe data structure.
153*5c2921b0SApple OSS Distributions  * Two of these are linked together to produce bi-directional pipes.
154*5c2921b0SApple OSS Distributions  */
155*5c2921b0SApple OSS Distributions struct pipe {
156*5c2921b0SApple OSS Distributions 	struct  pipebuf pipe_buffer;    /* data storage */
157*5c2921b0SApple OSS Distributions #ifdef PIPE_DIRECT
158*5c2921b0SApple OSS Distributions 	struct  pipemapping pipe_map;   /* pipe mapping for direct I/O */
159*5c2921b0SApple OSS Distributions #endif
160*5c2921b0SApple OSS Distributions 	struct  selinfo pipe_sel;       /* for compat with select */
161*5c2921b0SApple OSS Distributions 	pid_t   pipe_pgid;              /* information for async I/O */
162*5c2921b0SApple OSS Distributions 	struct  pipe *pipe_peer;        /* link with other direction */
163*5c2921b0SApple OSS Distributions 	u_int   pipe_state;             /* pipe status info */
164*5c2921b0SApple OSS Distributions 	int     pipe_busy;              /* busy flag, mostly to handle rundown sanely */
165*5c2921b0SApple OSS Distributions 	lck_mtx_t *pipe_mtxp;           /* shared mutex between both pipes */
166*5c2921b0SApple OSS Distributions 	struct  timespec st_atimespec;  /* time of last access */
167*5c2921b0SApple OSS Distributions 	struct  timespec st_mtimespec;  /* time of last data modification */
168*5c2921b0SApple OSS Distributions 	struct  timespec st_ctimespec;  /* time of last status change */
169*5c2921b0SApple OSS Distributions 	struct  label *pipe_label;      /* pipe MAC label - shared */
170*5c2921b0SApple OSS Distributions };
171*5c2921b0SApple OSS Distributions 
172*5c2921b0SApple OSS Distributions #define PIPE_MTX(pipe)          ((pipe)->pipe_mtxp)
173*5c2921b0SApple OSS Distributions 
174*5c2921b0SApple OSS Distributions #define PIPE_LOCK(pipe)         lck_mtx_lock(PIPE_MTX(pipe))
175*5c2921b0SApple OSS Distributions #define PIPE_UNLOCK(pipe)       lck_mtx_unlock(PIPE_MTX(pipe))
176*5c2921b0SApple OSS Distributions #define PIPE_LOCK_ASSERT(pipe, type)  LCK_MTX_ASSERT(PIPE_MTX(pipe), (type))
177*5c2921b0SApple OSS Distributions 
178*5c2921b0SApple OSS Distributions __BEGIN_DECLS
179*5c2921b0SApple OSS Distributions extern int pipe_stat(struct pipe *, void *, int);
180*5c2921b0SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE
181*5c2921b0SApple OSS Distributions extern uint64_t pipe_id(struct pipe *);
182*5c2921b0SApple OSS Distributions #endif
183*5c2921b0SApple OSS Distributions __END_DECLS
184*5c2921b0SApple OSS Distributions 
185*5c2921b0SApple OSS Distributions #endif  /* KERNEL */
186*5c2921b0SApple OSS Distributions 
187*5c2921b0SApple OSS Distributions #endif /* !_SYS_PIPE_H_ */
188