xref: /NextBSD/sys/sys/mach/port.h (revision 33da5adc555b3bc29986eeadca03829e4ad06b1e)
1 /*
2  * Copyright (c) 2000-2006 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  * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58  * support for mandatory and extensible security protections.  This notice
59  * is included in support of clause 2.2 (b) of the Apple Public License,
60  * Version 2.0.
61  */
62 /*
63  */
64 /*
65  *	File:	mach/port.h
66  *
67  *	Definition of a Mach port
68  *
69  *	Mach ports are the endpoints to Mach-implemented communications
70  *	channels (usually uni-directional message queues, but other types
71  *	also exist).
72  *
73  *	Unique collections of these endpoints are maintained for each
74  *	Mach task.  Each Mach port in the task's collection is given a
75  *	[task-local] name to identify it - and the the various "rights"
76  *	held by the task for that specific endpoint.
77  *
78  *	This header defines the types used to identify these Mach ports
79  *	and the various rights associated with them.  For more info see:
80  *
81  *	<mach/mach_port.h> - manipulation of port rights in a given space
82  *	<mach/message.h> - message queue [and port right passing] mechanism
83  *
84  */
85 
86 #ifndef	_MACH_PORT_H_
87 #define _MACH_PORT_H_
88 
89 #include <sys/cdefs.h>
90 #include <sys/types.h>
91 #include <vm/vm.h>
92 #include <sys/mach/vm_types.h>
93 
94 
95 /*
96  *	mach_port_name_t - the local identity for a Mach port
97  *
98  *	The name is Mach port namespace specific.  It is used to
99  *	identify the rights held for that port by the task whose
100  *	namespace is implied [or specifically provided].
101  *
102  *	Use of this type usually implies just a name - no rights.
103  *	See mach_port_t for a type that implies a "named right."
104  *
105  */
106 
107 #ifdef	_KERNEL
108 
109 /*
110  *	mach_port_t - a named port right
111  *
112  *	In the kernel, "rights" are represented [named] by pointers to
113  *	the ipc port object in question. There is no port namespace for the
114  *	rights to be collected.
115  *
116  *	Actually, there is namespace for the kernel task.  But most kernel
117  *	code - including, but not limited to, Mach IPC code - lives in the
118  *	limbo between the current user-level task and the "next" task. Very
119  *	little of the kernel code runs in full kernel task context.  So very
120  *	little of it gets to use the kernel task's port name space.
121  *
122  *	Because of this implementation approach, all in-kernel rights for
123  *	a given port coalesce [have the same name/pointer].  The actual
124  *	references are counted in the port itself.  It is up to the kernel
125  *	code in question to "just remember" how many [and what type of]
126  *	rights it holds and handle them appropriately.
127  *
128  */
129 
130 #ifndef	MACH_KERNEL_PRIVATE
131 /*
132  *	For kernel code that resides outside of Mach proper, we opaque the
133  *	port structure definition.
134  */
135 struct ipc_port ;
136 
137 #endif	/* MACH_KERNEL_PRIVATE */
138 
139 typedef struct ipc_port *ipc_port_t;
140 typedef ipc_port_t		mach_port_t;
141 
142 
143 #define IPC_PORT_NULL		((ipc_port_t) 0UL)
144 #define IPC_PORT_DEAD		((ipc_port_t)~0UL)
145 #define IPC_PORT_VALID(port) \
146 	((port) != IPC_PORT_NULL && (port) != IPC_PORT_DEAD)
147 
148 /*
149  * Since the 32-bit and 64-bit representations of ~0 are different,
150  * explicitly handle MACH_PORT_DEAD
151  */
152 
153 #else	/* KERNEL */
154 
155 /*
156  *	mach_port_t - a named port right
157  *
158  *	In user-space, "rights" are represented by the name of the
159  *	right in the Mach port namespace.  Even so, this type is
160  *	presented as a unique one to more clearly denote the presence
161  *	of a right coming along with the name.
162  *
163  *	Often, various rights for a port held in a single name space
164  *	will coalesce and are, therefore, be identified by a single name
165  *	[this is the case for send and receive rights].  But not
166  *	always [send-once rights currently get a unique name for
167  *	each right].
168  *
169  */
170 #if 0
171 #include <sys/_types.h>
172 #include <sys/_types/_mach_port_t.h>
173 #endif
174 typedef natural_t mach_port_t;
175 
176 #endif	/* KERNEL */
177 
178 typedef natural_t mach_port_name_t;
179 typedef mach_port_name_t *mach_port_name_array_t;
180 
181 typedef mach_port_t			*mach_port_array_t;
182 
183 /*
184  *  MACH_PORT_NULL is a legal value that can be carried in messages.
185  *  It indicates the absence of any port or port rights.  (A port
186  *  argument keeps the message from being "simple", even if the
187  *  value is MACH_PORT_NULL.)  The value MACH_PORT_DEAD is also a legal
188  *  value that can be carried in messages.  It indicates
189  *  that a port right was present, but it died.
190  */
191 
192 #define MACH_PORT_NULL		((mach_port_t)0)
193 #define MACH_PORT_DEAD		((mach_port_t) ~0)
194 #define MACH_PORT_VALID(name)				\
195 		(((name) != MACH_PORT_NULL) && 		\
196 		 ((name) != MACH_PORT_DEAD))
197 
198 #define MACH_PORT_NAME_NULL		0  /* intentional loose typing */
199 #define MACH_PORT_NAME_DEAD		((mach_port_name_t) ~0)
200 #define MACH_PORT_NAME_VALID(name)			\
201 		(((name) != MACH_PORT_NAME_NULL) && 		\
202 		 ((name) != MACH_PORT_NAME_DEAD))
203 
204 
205 /*
206  *	For kernel-selected [assigned] port names, the name is
207  *	comprised of two parts: a generation number and an index.
208  *	This approach keeps the exact same name from being generated
209  *	and reused too quickly [to catch right/reference counting bugs].
210  *	The dividing line between the constituent parts is exposed so
211  *	that efficient "mach_port_name_t to data structure pointer"
212  *	conversion implementation can be made.  But it is possible
213  *	for user-level code to assign their own names to Mach ports.
214  *	These are not required to participate in this algorithm.  So
215  *	care should be taken before "assuming" this model.
216  *
217  */
218 
219 #if 0
220 
221 #define	MACH_PORT_INDEX(name)		((name) >> 8)
222 #define	MACH_PORT_GEN(name)		(((name) & 0xff) << 24)
223 #define	MACH_PORT_MAKE(index, gen)	\
224 		(((index) << 8) | (gen) >> 24)
225 
226 #else	/* NO_PORT_GEN */
227 
228 #define	MACH_PORT_INDEX(name)		(name)
229 #define	MACH_PORT_GEN(name)		(0)
230 #define	MACH_PORT_MAKE(index, gen)	(index)
231 
232 #endif	/* NO_PORT_GEN */
233 #define MACH_PORT_MAKEB(index, bits)    \
234                 MACH_PORT_MAKE(index, IE_BITS_GEN(bits))
235 
236 #define MACH_PORT_VALID(name)   \
237                 (((name) != MACH_PORT_NULL) && ((name) != MACH_PORT_DEAD))
238 
239 
240 
241 #include <sys/mach/ipc/port.h>
242 /*
243  *  These are the different rights a task may have for a port.
244  *  The MACH_PORT_RIGHT_* definitions are used as arguments
245  *  to mach_port_allocate, mach_port_get_refs, etc, to specify
246  *  a particular right to act upon.  The mach_port_names and
247  *  mach_port_type calls return bitmasks using the MACH_PORT_TYPE_*
248  *  definitions.  This is because a single name may denote
249  *  multiple rights.
250  */
251 
252 typedef natural_t mach_port_right_t;
253 
254 #define MACH_PORT_RIGHT_SEND		((mach_port_right_t) 0)
255 #define MACH_PORT_RIGHT_RECEIVE		((mach_port_right_t) 1)
256 #define MACH_PORT_RIGHT_SEND_ONCE	((mach_port_right_t) 2)
257 #define MACH_PORT_RIGHT_PORT_SET	((mach_port_right_t) 3)
258 #define MACH_PORT_RIGHT_DEAD_NAME	((mach_port_right_t) 4)
259 #define MACH_PORT_RIGHT_LABELH	        ((mach_port_right_t) 5)
260 #define MACH_PORT_RIGHT_NUMBER		((mach_port_right_t) 6)
261 
262 typedef natural_t mach_port_type_t;
263 typedef mach_port_type_t *mach_port_type_array_t;
264 
265 #define MACH_PORT_TYPE(right)						\
266 		((mach_port_type_t)(((mach_port_type_t) 1)  		\
267 		<< ((right) + ((mach_port_right_t) 16))))
268 #define MACH_PORT_TYPE_NONE	    ((mach_port_type_t) 0L)
269 #define MACH_PORT_TYPE_SEND	    MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND)
270 #define MACH_PORT_TYPE_RECEIVE	    MACH_PORT_TYPE(MACH_PORT_RIGHT_RECEIVE)
271 #define MACH_PORT_TYPE_SEND_ONCE    MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND_ONCE)
272 #define MACH_PORT_TYPE_PORT_SET	    MACH_PORT_TYPE(MACH_PORT_RIGHT_PORT_SET)
273 #define MACH_PORT_TYPE_DEAD_NAME    MACH_PORT_TYPE(MACH_PORT_RIGHT_DEAD_NAME)
274 #define MACH_PORT_TYPE_LABELH       MACH_PORT_TYPE(MACH_PORT_RIGHT_LABELH)
275 
276 /* Convenient combinations. */
277 
278 #define MACH_PORT_TYPE_SEND_RECEIVE					\
279 		(MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_RECEIVE)
280 #define	MACH_PORT_TYPE_SEND_RIGHTS					\
281 		(MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_SEND_ONCE)
282 #define	MACH_PORT_TYPE_PORT_RIGHTS					\
283 		(MACH_PORT_TYPE_SEND_RIGHTS|MACH_PORT_TYPE_RECEIVE)
284 #define	MACH_PORT_TYPE_PORT_OR_DEAD					\
285 		(MACH_PORT_TYPE_PORT_RIGHTS|MACH_PORT_TYPE_DEAD_NAME)
286 #define MACH_PORT_TYPE_ALL_RIGHTS					\
287 		(MACH_PORT_TYPE_PORT_OR_DEAD|MACH_PORT_TYPE_PORT_SET)
288 
289 /* Dummy type bits that mach_port_type/mach_port_names can return. */
290 
291 #define MACH_PORT_TYPE_DNREQUEST		0x80000000
292 #define MACH_PORT_TYPE_SPREQUEST		0x40000000
293 #define MACH_PORT_TYPE_SPREQUEST_DELAYED	0x20000000
294 
295 /* User-references for capabilities. */
296 
297 typedef natural_t mach_port_urefs_t;
298 typedef integer_t mach_port_delta_t;			/* change in urefs */
299 
300 /* Attributes of ports.  (See mach_port_get_receive_status.) */
301 
302 typedef natural_t mach_port_seqno_t;		/* sequence number */
303 typedef natural_t mach_port_mscount_t;		/* make-send count */
304 typedef natural_t mach_port_msgcount_t;		/* number of msgs */
305 typedef natural_t mach_port_rights_t;		/* number of rights */
306 
307 /*
308  *	Are there outstanding send rights for a given port?
309  */
310 #define	MACH_PORT_SRIGHTS_NONE		0		/* no srights */
311 #define	MACH_PORT_SRIGHTS_PRESENT	1		/* srights */
312 typedef unsigned int mach_port_srights_t;	/* status of send rights */
313 
314 typedef struct mach_port_status {
315 	mach_port_rights_t	mps_pset;	/* count of containing port sets */
316 	mach_port_seqno_t	mps_seqno;	/* sequence number */
317 	mach_port_mscount_t	mps_mscount;	/* make-send count */
318 	mach_port_msgcount_t	mps_qlimit;	/* queue limit */
319 	mach_port_msgcount_t	mps_msgcount;	/* number in the queue */
320 	mach_port_rights_t	mps_sorights;	/* how many send-once rights */
321 	boolean_t		mps_srights;	/* do send rights exist? */
322 	boolean_t		mps_pdrequest;	/* port-deleted requested? */
323 	boolean_t		mps_nsrequest;	/* no-senders requested? */
324 	natural_t		mps_flags;		/* port flags */
325 } mach_port_status_t;
326 
327 /* System-wide values for setting queue limits on a port */
328 #define MACH_PORT_QLIMIT_ZERO		((mach_port_msgcount_t) 0)
329 #define MACH_PORT_QLIMIT_BASIC		((mach_port_msgcount_t) 5)
330 #define MACH_PORT_QLIMIT_SMALL		((mach_port_msgcount_t) 16)
331 #define MACH_PORT_QLIMIT_LARGE		((mach_port_msgcount_t) 1024)
332 #define MACH_PORT_QLIMIT_KERNEL		((mach_port_msgcount_t) 65536)
333 #define MACH_PORT_QLIMIT_MIN		MACH_PORT_QLIMIT_ZERO
334 #define MACH_PORT_QLIMIT_DEFAULT	MACH_PORT_QLIMIT_BASIC
335 #define MACH_PORT_QLIMIT_MAX		MACH_PORT_QLIMIT_LARGE
336 
337 typedef struct mach_port_limits {
338 	mach_port_msgcount_t	mpl_qlimit;	/* number of msgs */
339 } mach_port_limits_t;
340 
341 /* Possible values for mps_flags (part of mach_port_status_t) */
342 #define MACH_PORT_STATUS_FLAG_TEMPOWNER		0x01
343 #define MACH_PORT_STATUS_FLAG_GUARDED		0x02
344 #define MACH_PORT_STATUS_FLAG_STRICT_GUARD	0x04
345 #define MACH_PORT_STATUS_FLAG_IMP_DONATION	0x08
346 #define MACH_PORT_STATUS_FLAG_REVIVE		0x10
347 #define MACH_PORT_STATUS_FLAG_TASKPTR		0x20
348 
349 typedef struct mach_port_info_ext {
350 	mach_port_status_t	mpie_status;
351 	mach_port_msgcount_t	mpie_boost_cnt;
352 	uint32_t		reserved[6];
353 } mach_port_info_ext_t;
354 
355 typedef integer_t *mach_port_info_t;		/* varying array of natural_t */
356 
357 /* Flavors for mach_port_get/set_attributes() */
358 typedef int	mach_port_flavor_t;
359 #define MACH_PORT_LIMITS_INFO		1	/* uses mach_port_status_t */
360 #define MACH_PORT_RECEIVE_STATUS	2	/* uses mach_port_limits_t */
361 #define MACH_PORT_DNREQUESTS_SIZE	3	/* info is int */
362 #define MACH_PORT_TEMPOWNER		4	/* indicates receive right will be reassigned to another task */
363 #define MACH_PORT_IMPORTANCE_RECEIVER	5	/* indicates recieve right accepts priority donation */
364 #define MACH_PORT_DENAP_RECEIVER	6	/* indicates receive right accepts de-nap donation */
365 #define MACH_PORT_INFO_EXT		7	/* uses mach_port_info_ext_t */
366 
367 #define MACH_PORT_LIMITS_INFO_COUNT	((natural_t) \
368 	(sizeof(mach_port_limits_t)/sizeof(natural_t)))
369 #define MACH_PORT_RECEIVE_STATUS_COUNT	((natural_t) \
370 	(sizeof(mach_port_status_t)/sizeof(natural_t)))
371 #define MACH_PORT_DNREQUESTS_SIZE_COUNT 1
372 #define MACH_PORT_INFO_EXT_COUNT	((natural_t) \
373 	(sizeof(mach_port_info_ext_t)/sizeof(natural_t)))
374 /*
375  * Structure used to pass information about port allocation requests.
376  * Must be padded to 64-bits total length.
377  */
378 typedef struct mach_port_qos {
379 	unsigned int		name:1;		/* name given */
380 	unsigned int 		prealloc:1;	/* prealloced message */
381 	boolean_t		pad1:30;
382 	natural_t		len;
383 } mach_port_qos_t;
384 
385 /* Mach Port Guarding definitions */
386 
387 /*
388  * Flags for mach_port_options (used for
389  * invocation of mach_port_construct).
390  * Indicates attributes to be set for the newly
391  * allocated port.
392  */
393 #define MPO_CONTEXT_AS_GUARD	0x01	/* Add guard to the port */
394 #define MPO_QLIMIT		0x02	/* Set qlimit for the port msg queue */
395 #define MPO_TEMPOWNER		0x04	/* Set the tempowner bit of the port */
396 #define MPO_IMPORTANCE_RECEIVER 0x08	/* Mark the port as importance receiver */
397 #define MPO_INSERT_SEND_RIGHT   0x10	/* Insert a send right for the port */
398 #define MPO_STRICT		0x20	/* Apply strict guarding for port */
399 #define MPO_DENAP_RECEIVER	0x40	/* Mark the port as App de-nap receiver */
400 /*
401  * Structure to define optional attributes for a newly
402  * constructed port.
403  */
404 typedef struct mach_port_options {
405 	uint32_t		flags;		/* Flags defining attributes for port */
406 	mach_port_limits_t	mpl;		/* Message queue limit for port */
407 	uint64_t		reserved[2];	/* Reserved */
408 }mach_port_options_t;
409 
410 typedef mach_port_options_t *mach_port_options_ptr_t;
411 
412 /*
413  * EXC_GUARD represents a guard violation for both
414  * mach ports and file descriptors. GUARD_TYPE_ is used
415  * to differentiate among them.
416  */
417 #define GUARD_TYPE_MACH_PORT	0x1
418 
419 /* Reasons for exception for a guarded mach port */
420 enum mach_port_guard_exception_codes {
421 	kGUARD_EXC_DESTROY		= 1u << 0,
422 	kGUARD_EXC_MOD_REFS		= 1u << 1,
423 	kGUARD_EXC_SET_CONTEXT		= 1u << 2,
424 	kGUARD_EXC_UNGUARDED		= 1u << 3,
425 	kGUARD_EXC_INCORRECT_GUARD	= 1u << 4
426 };
427 
428 #if	0 && !__DARWIN_UNIX03 && !defined(_NO_PORT_T_FROM_MACH)
429 /*
430  *  Mach 3.0 renamed everything to have mach_ in front of it.
431  *  These types and macros are provided for backward compatibility
432  *	but are deprecated.
433  */
434 typedef mach_port_t		port_t;
435 typedef mach_port_name_t	port_name_t;
436 typedef mach_port_name_t	*port_name_array_t;
437 
438 #define PORT_NULL		((port_t) 0)
439 #define PORT_DEAD		((port_t) ~0)
440 #define PORT_VALID(name) \
441 		((port_t)(name) != PORT_NULL && (port_t)(name) != PORT_DEAD)
442 
443 #endif	/* !__DARWIN_UNIX03 && !_NO_PORT_T_FROM_MACH */
444 
445 
446 #define CAST_MACH_PORT_TO_NAME(x) ((mach_port_name_t)(uintptr_t)(x))
447 #define CAST_MACH_NAME_TO_PORT(x) ((x) == MACH_PORT_NAME_DEAD ? (mach_port_t)IPC_PORT_DEAD : (mach_port_t)(uintptr_t)(x))
448 
449 
450 
451 #endif	/* _MACH_PORT_H_ */
452