]> git.saurik.com Git - apple/xnu.git/blob - osfmk/mach/port.h
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / mach / port.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: mach/port.h
60 *
61 * Definition of a Mach port
62 *
63 * Mach ports are the endpoints to Mach-implemented communications
64 * channels (usually uni-directional message queues, but other types
65 * also exist).
66 *
67 * Unique collections of these endpoints are maintained for each
68 * Mach task. Each Mach port in the task's collection is given a
69 * [task-local] name to identify it - and the the various "rights"
70 * held by the task for that specific endpoint.
71 *
72 * This header defines the types used to identify these Mach ports
73 * and the various rights associated with them. For more info see:
74 *
75 * <mach/mach_port.h> - manipulation of port rights in a given space
76 * <mach/message.h> - message queue [and port right passing] mechanism
77 *
78 */
79
80 #ifndef _MACH_PORT_H_
81 #define _MACH_PORT_H_
82
83 #include <stdint.h>
84 #include <mach/boolean.h>
85 #include <mach/machine/vm_types.h>
86
87 /*
88 * mach_port_name_t - the local identity for a Mach port
89 *
90 * The name is Mach port namespace specific. It is used to
91 * identify the rights held for that port by the task whose
92 * namespace is implied [or specifically provided].
93 *
94 * Use of this type usually implies just a name - no rights.
95 * See mach_port_t for a type that implies a "named right."
96 *
97 */
98
99 typedef natural_t mach_port_name_t;
100 typedef mach_port_name_t *mach_port_name_array_t;
101
102 #ifdef KERNEL_PRIVATE
103
104 /*
105 * mach_port_t - a named port right
106 *
107 * In the kernel, "rights" are represented [named] by pointers to
108 * the ipc port object in question. There is no port namespace for the
109 * rights to be collected.
110 *
111 * Actually, there is namespace for the kernel task. But most kernel
112 * code - including, but not limited to, Mach IPC code - lives in the
113 * limbo between the current user-level task and the "next" task. Very
114 * little of the kernel code runs in full kernel task context. So very
115 * little of it gets to use the kernel task's port name space.
116 *
117 * Because of this implementation approach, all in-kernel rights for
118 * a given port coalesce [have the same name/pointer]. The actual
119 * references are counted in the port itself. It is up to the kernel
120 * code in question to "just remember" how many [and what type of]
121 * rights it holds and handle them appropriately.
122 *
123 */
124
125 #ifndef MACH_KERNEL_PRIVATE
126 /*
127 * For kernel code that resides outside of Mach proper, we opaque the
128 * port structure definition.
129 */
130 struct ipc_port ;
131
132 #endif /* MACH_KERNEL_PRIVATE */
133
134 typedef struct ipc_port *ipc_port_t;
135
136 #define IPC_PORT_NULL ((ipc_port_t) 0)
137 #define IPC_PORT_DEAD ((ipc_port_t)~0)
138 #define IPC_PORT_VALID(port) \
139 ((port) != IPC_PORT_NULL && (port) != IPC_PORT_DEAD)
140
141 typedef ipc_port_t mach_port_t;
142
143 #else /* KERNEL_PRIVATE */
144
145 /*
146 * mach_port_t - a named port right
147 *
148 * In user-space, "rights" are represented by the name of the
149 * right in the Mach port namespace. Even so, this type is
150 * presented as a unique one to more clearly denote the presence
151 * of a right coming along with the name.
152 *
153 * Often, various rights for a port held in a single name space
154 * will coalesce and are, therefore, be identified by a single name
155 * [this is the case for send and receive rights]. But not
156 * always [send-once rights currently get a unique name for
157 * each right].
158 *
159 */
160
161 #ifndef _MACH_PORT_T
162 #define _MACH_PORT_T
163 typedef mach_port_name_t mach_port_t;
164 #endif
165
166 #endif /* KERNEL_PRIVATE */
167
168 typedef mach_port_t *mach_port_array_t;
169
170 /*
171 * MACH_PORT_NULL is a legal value that can be carried in messages.
172 * It indicates the absence of any port or port rights. (A port
173 * argument keeps the message from being "simple", even if the
174 * value is MACH_PORT_NULL.) The value MACH_PORT_DEAD is also a legal
175 * value that can be carried in messages. It indicates
176 * that a port right was present, but it died.
177 */
178
179 #define MACH_PORT_NULL 0 /* intentional loose typing */
180 #define MACH_PORT_DEAD ((mach_port_name_t) ~0)
181 #define MACH_PORT_VALID(name) \
182 (((name) != MACH_PORT_NULL) && \
183 ((name) != MACH_PORT_DEAD))
184
185
186 /*
187 * For kernel-selected [assigned] port names, the name is
188 * comprised of two parts: a generation number and an index.
189 * This approach keeps the exact same name from being generated
190 * and reused too quickly [to catch right/reference counting bugs].
191 * The dividing line between the constituent parts is exposed so
192 * that efficient "mach_port_name_t to data structure pointer"
193 * conversion implementation can be made. But it is possible
194 * for user-level code to assign their own names to Mach ports.
195 * These are not required to participate in this algorithm. So
196 * care should be taken before "assuming" this model.
197 *
198 */
199
200 #ifndef NO_PORT_GEN
201
202 #define MACH_PORT_INDEX(name) ((name) >> 8)
203 #define MACH_PORT_GEN(name) (((name) & 0xff) << 24)
204 #define MACH_PORT_MAKE(index, gen) \
205 (((index) << 8) | (gen) >> 24)
206
207 #else /* NO_PORT_GEN */
208
209 #define MACH_PORT_INDEX(name) (name)
210 #define MACH_PORT_GEN(name) (0)
211 #define MACH_PORT_MAKE(index, gen) (index)
212
213 #endif /* NO_PORT_GEN */
214
215
216 /*
217 * These are the different rights a task may have for a port.
218 * The MACH_PORT_RIGHT_* definitions are used as arguments
219 * to mach_port_allocate, mach_port_get_refs, etc, to specify
220 * a particular right to act upon. The mach_port_names and
221 * mach_port_type calls return bitmasks using the MACH_PORT_TYPE_*
222 * definitions. This is because a single name may denote
223 * multiple rights.
224 */
225
226 typedef natural_t mach_port_right_t;
227
228 #define MACH_PORT_RIGHT_SEND ((mach_port_right_t) 0)
229 #define MACH_PORT_RIGHT_RECEIVE ((mach_port_right_t) 1)
230 #define MACH_PORT_RIGHT_SEND_ONCE ((mach_port_right_t) 2)
231 #define MACH_PORT_RIGHT_PORT_SET ((mach_port_right_t) 3)
232 #define MACH_PORT_RIGHT_DEAD_NAME ((mach_port_right_t) 4)
233 #define MACH_PORT_RIGHT_NUMBER ((mach_port_right_t) 5)
234
235 typedef natural_t mach_port_type_t;
236 typedef mach_port_type_t *mach_port_type_array_t;
237
238 #define MACH_PORT_TYPE(right) \
239 ((mach_port_type_t)(((mach_port_type_t) 1) \
240 << ((right) + ((mach_port_right_t) 16))))
241 #define MACH_PORT_TYPE_NONE ((mach_port_type_t) 0L)
242 #define MACH_PORT_TYPE_SEND MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND)
243 #define MACH_PORT_TYPE_RECEIVE MACH_PORT_TYPE(MACH_PORT_RIGHT_RECEIVE)
244 #define MACH_PORT_TYPE_SEND_ONCE MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND_ONCE)
245 #define MACH_PORT_TYPE_PORT_SET MACH_PORT_TYPE(MACH_PORT_RIGHT_PORT_SET)
246 #define MACH_PORT_TYPE_DEAD_NAME MACH_PORT_TYPE(MACH_PORT_RIGHT_DEAD_NAME)
247
248 /* Convenient combinations. */
249
250 #define MACH_PORT_TYPE_SEND_RECEIVE \
251 (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_RECEIVE)
252 #define MACH_PORT_TYPE_SEND_RIGHTS \
253 (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_SEND_ONCE)
254 #define MACH_PORT_TYPE_PORT_RIGHTS \
255 (MACH_PORT_TYPE_SEND_RIGHTS|MACH_PORT_TYPE_RECEIVE)
256 #define MACH_PORT_TYPE_PORT_OR_DEAD \
257 (MACH_PORT_TYPE_PORT_RIGHTS|MACH_PORT_TYPE_DEAD_NAME)
258 #define MACH_PORT_TYPE_ALL_RIGHTS \
259 (MACH_PORT_TYPE_PORT_OR_DEAD|MACH_PORT_TYPE_PORT_SET)
260
261 /* Dummy type bits that mach_port_type/mach_port_names can return. */
262
263 #define MACH_PORT_TYPE_DNREQUEST 0x80000000
264
265 /* User-references for capabilities. */
266
267 typedef natural_t mach_port_urefs_t;
268 typedef integer_t mach_port_delta_t; /* change in urefs */
269
270 /* Attributes of ports. (See mach_port_get_receive_status.) */
271
272 typedef natural_t mach_port_seqno_t; /* sequence number */
273 typedef natural_t mach_port_mscount_t; /* make-send count */
274 typedef natural_t mach_port_msgcount_t; /* number of msgs */
275 typedef natural_t mach_port_rights_t; /* number of rights */
276
277 /*
278 * Are there outstanding send rights for a given port?
279 */
280 #define MACH_PORT_SRIGHTS_NONE 0 /* no srights */
281 #define MACH_PORT_SRIGHTS_PRESENT 1 /* srights */
282 typedef unsigned int mach_port_srights_t; /* status of send rights */
283
284 typedef struct mach_port_status {
285 mach_port_name_t mps_pset; /* containing port set */
286 mach_port_seqno_t mps_seqno; /* sequence number */
287 mach_port_mscount_t mps_mscount; /* make-send count */
288 mach_port_msgcount_t mps_qlimit; /* queue limit */
289 mach_port_msgcount_t mps_msgcount; /* number in the queue */
290 mach_port_rights_t mps_sorights; /* how many send-once rights */
291 boolean_t mps_srights; /* do send rights exist? */
292 boolean_t mps_pdrequest; /* port-deleted requested? */
293 boolean_t mps_nsrequest; /* no-senders requested? */
294 natural_t mps_flags; /* port flags */
295 } mach_port_status_t;
296
297 #define MACH_PORT_QLIMIT_DEFAULT ((mach_port_msgcount_t) 5)
298 #define MACH_PORT_QLIMIT_MAX ((mach_port_msgcount_t) 16)
299
300 typedef struct mach_port_limits {
301 mach_port_msgcount_t mpl_qlimit; /* number of msgs */
302 } mach_port_limits_t;
303
304 typedef integer_t *mach_port_info_t; /* varying array of natural_t */
305
306 /* Flavors for mach_port_get/set_attributes() */
307 typedef int mach_port_flavor_t;
308 #define MACH_PORT_LIMITS_INFO 1 /* uses mach_port_status_t */
309 #define MACH_PORT_RECEIVE_STATUS 2 /* uses mach_port_limits_t */
310 #define MACH_PORT_DNREQUESTS_SIZE 3 /* info is int */
311
312 #define MACH_PORT_LIMITS_INFO_COUNT ((natural_t) \
313 (sizeof(mach_port_limits_t)/sizeof(natural_t)))
314 #define MACH_PORT_RECEIVE_STATUS_COUNT ((natural_t) \
315 (sizeof(mach_port_status_t)/sizeof(natural_t)))
316 #define MACH_PORT_DNREQUESTS_SIZE_COUNT 1
317
318 /*
319 * Structure used to pass information about port allocation requests.
320 * Must be padded to 64-bits total length.
321 */
322 typedef struct mach_port_qos {
323 boolean_t name:1; /* name given */
324 boolean_t prealloc:1; /* prealloced message */
325 boolean_t pad1:30;
326 natural_t len;
327 } mach_port_qos_t;
328
329 #if !defined(_POSIX_C_SOURCE) && !defined(_NO_PORT_T_FROM_MACH)
330 /*
331 * Mach 3.0 renamed everything to have mach_ in front of it.
332 * These types and macros are provided for backward compatibility
333 * but are deprecated.
334 */
335 typedef mach_port_t port_t;
336 typedef mach_port_name_t port_name_t;
337 typedef mach_port_name_t *port_name_array_t;
338
339 #define PORT_NULL ((port_t) 0)
340 #define PORT_DEAD ((port_t) ~0)
341 #define PORT_VALID(name) \
342 ((port_t)(name) != PORT_NULL && (port_t)(name) != PORT_DEAD)
343
344 #endif /* !_POSIX_C_SOURCE && !_NO_PORT_T_FROM_MACH */
345
346 #endif /* _MACH_PORT_H_ */