xref: /xnu-10063.141.1/osfmk/mach/port.h (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
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  [email protected]
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 <stdint.h>
91 #include <mach/boolean.h>
92 #include <mach/machine/vm_types.h>
93 #if XNU_KERNEL_PRIVATE
94 #include <ptrauth.h>
95 #endif /* XNU_KERNEL_PRIVATE */
96 
97 /*
98  *	mach_port_name_t - the local identity for a Mach port
99  *
100  *	The name is Mach port namespace specific.  It is used to
101  *	identify the rights held for that port by the task whose
102  *	namespace is implied [or specifically provided].
103  *
104  *	Use of this type usually implies just a name - no rights.
105  *	See mach_port_t for a type that implies a "named right."
106  *
107  */
108 
109 typedef natural_t mach_port_name_t;
110 typedef mach_port_name_t *mach_port_name_array_t;
111 
112 #ifdef  KERNEL
113 
114 /*
115  *	mach_port_t - a named port right
116  *
117  *	In the kernel, "rights" are represented [named] by pointers to
118  *	the ipc port object in question. There is no port namespace for the
119  *	rights to be collected.
120  *
121  *	Actually, there is namespace for the kernel task.  But most kernel
122  *	code - including, but not limited to, Mach IPC code - lives in the
123  *	limbo between the current user-level task and the "next" task. Very
124  *	little of the kernel code runs in full kernel task context.  So very
125  *	little of it gets to use the kernel task's port name space.
126  *
127  *	Because of this implementation approach, all in-kernel rights for
128  *	a given port coalesce [have the same name/pointer].  The actual
129  *	references are counted in the port itself.  It is up to the kernel
130  *	code in question to "just remember" how many [and what type of]
131  *	rights it holds and handle them appropriately.
132  *
133  */
134 
135 #ifndef MACH_KERNEL_PRIVATE
136 /*
137  *	For kernel code that resides outside of Mach proper, we opaque the
138  *	port structure definition.
139  */
140 struct ipc_port;
141 
142 #endif  /* MACH_KERNEL_PRIVATE */
143 
144 typedef struct ipc_port         *ipc_port_t;
145 
146 #define IPC_PORT_NULL           __unsafe_forge_single(ipc_port_t, NULL)
147 #define IPC_PORT_DEAD           __unsafe_forge_single(ipc_port_t, ~0UL)
148 #define IPC_PORT_VALID(port)    ipc_port_valid(port)
149 
150 static inline boolean_t
ipc_port_valid(ipc_port_t port)151 ipc_port_valid(ipc_port_t port)
152 {
153 	return port != IPC_PORT_DEAD && port;
154 }
155 
156 typedef ipc_port_t              mach_port_t;
157 
158 /*
159  * Since the 32-bit and 64-bit representations of ~0 are different,
160  * explicitly handle MACH_PORT_DEAD
161  */
162 
163 #define CAST_MACH_PORT_TO_NAME(x) ((mach_port_name_t)(uintptr_t)(x))
164 #define CAST_MACH_NAME_TO_PORT(x) ((x) == MACH_PORT_DEAD ? (mach_port_t)IPC_PORT_DEAD : (mach_port_t)(uintptr_t)(x))
165 
166 #else   /* KERNEL */
167 
168 /*
169  *	mach_port_t - a named port right
170  *
171  *	In user-space, "rights" are represented by the name of the
172  *	right in the Mach port namespace.  Even so, this type is
173  *	presented as a unique one to more clearly denote the presence
174  *	of a right coming along with the name.
175  *
176  *	Often, various rights for a port held in a single name space
177  *	will coalesce and are, therefore, be identified by a single name
178  *	[this is the case for send and receive rights].  But not
179  *	always [send-once rights currently get a unique name for
180  *	each right].
181  *
182  */
183 
184 #include <sys/_types.h>
185 #include <sys/_types/_mach_port_t.h>
186 
187 #endif  /* KERNEL */
188 
189 #if XNU_KERNEL_PRIVATE
190 #if __has_feature(ptrauth_calls)
191 #define __mach_port_array_auth \
192 	__ptrauth(ptrauth_key_process_independent_data, 1, \
193 	    ptrauth_string_discriminator("mach_port_ool_t"))
194 #else
195 #define __mach_port_array_auth
196 #endif
197 typedef struct {
198 	mach_port_t __mach_port_array_auth port;
199 } mach_port_ool_t;
200 typedef mach_port_ool_t                 *mach_port_array_t;
201 #else
202 typedef mach_port_t                     *mach_port_array_t;
203 #endif
204 
205 /*
206  *  MACH_PORT_NULL is a legal value that can be carried in messages.
207  *  It indicates the absence of any port or port rights.  (A port
208  *  argument keeps the message from being "simple", even if the
209  *  value is MACH_PORT_NULL.)  The value MACH_PORT_DEAD is also a legal
210  *  value that can be carried in messages.  It indicates
211  *  that a port right was present, but it died.
212  */
213 
214 #if defined(XNU_KERNEL_PRIVATE) && defined(__cplusplus)
215 #define MACH_PORT_NULL          NULL
216 #else
217 #define MACH_PORT_NULL          0  /* intentional loose typing */
218 #endif
219 #define MACH_PORT_DEAD          ((mach_port_name_t) ~0)
220 #if MACH_KERNEL_PRIVATE
221 #define MACH_PORT_SPECIAL_DEFAULT ((mach_port_name_t)1)
222 #endif /* MACH_KERNEL_PRIVATE */
223 #define MACH_PORT_VALID(name)                           \
224 	        (((name) != MACH_PORT_NULL) &&          \
225 	         ((name) != MACH_PORT_DEAD))
226 
227 
228 /*
229  *	For kernel-selected [assigned] port names, the name is
230  *	comprised of two parts: a generation number and an index.
231  *	This approach keeps the exact same name from being generated
232  *	and reused too quickly [to catch right/reference counting bugs].
233  *	The dividing line between the constituent parts is exposed so
234  *	that efficient "mach_port_name_t to data structure pointer"
235  *	conversion implementation can be made.  But it is possible
236  *	for user-level code to assign their own names to Mach ports.
237  *	These are not required to participate in this algorithm.  So
238  *	care should be taken before "assuming" this model.
239  *
240  */
241 
242 #ifndef NO_PORT_GEN
243 
244 #define MACH_PORT_INDEX(name)           ((name) >> 8)
245 #define MACH_PORT_GEN(name)             (((name) & 0xff) << 24)
246 #define MACH_PORT_MAKE(index, gen)      \
247 	        (((index) << 8) | (gen) >> 24)
248 
249 #else   /* NO_PORT_GEN */
250 
251 #define MACH_PORT_INDEX(name)           (name)
252 #define MACH_PORT_GEN(name)             (0)
253 #define MACH_PORT_MAKE(index, gen)      (index)
254 
255 #endif  /* NO_PORT_GEN */
256 
257 
258 /*
259  *  These are the different rights a task may have for a port.
260  *  The MACH_PORT_RIGHT_* definitions are used as arguments
261  *  to mach_port_allocate, mach_port_get_refs, etc, to specify
262  *  a particular right to act upon.  The mach_port_names and
263  *  mach_port_type calls return bitmasks using the MACH_PORT_TYPE_*
264  *  definitions.  This is because a single name may denote
265  *  multiple rights.
266  */
267 
268 typedef natural_t mach_port_right_t;
269 
270 #define MACH_PORT_RIGHT_SEND            ((mach_port_right_t) 0)
271 #define MACH_PORT_RIGHT_RECEIVE         ((mach_port_right_t) 1)
272 #define MACH_PORT_RIGHT_SEND_ONCE       ((mach_port_right_t) 2)
273 #define MACH_PORT_RIGHT_PORT_SET        ((mach_port_right_t) 3)
274 #define MACH_PORT_RIGHT_DEAD_NAME       ((mach_port_right_t) 4)
275 #define MACH_PORT_RIGHT_LABELH          ((mach_port_right_t) 5) /* obsolete right */
276 #define MACH_PORT_RIGHT_NUMBER          ((mach_port_right_t) 6) /* right not implemented */
277 
278 #ifdef MACH_KERNEL_PRIVATE
279 #define MACH_PORT_RIGHT_VALID_TRANSLATE(right) \
280 	((right) >= MACH_PORT_RIGHT_SEND && (right) <= MACH_PORT_RIGHT_DEAD_NAME)
281 #endif
282 
283 typedef natural_t mach_port_type_t;
284 typedef mach_port_type_t *mach_port_type_array_t;
285 
286 #define MACH_PORT_TYPE(right)                                           \
287 	        ((mach_port_type_t)(((mach_port_type_t) 1)              \
288 	        << ((right) + ((mach_port_right_t) 16))))
289 #define MACH_PORT_TYPE_NONE         ((mach_port_type_t) 0L)
290 #define MACH_PORT_TYPE_SEND         MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND)
291 #define MACH_PORT_TYPE_RECEIVE      MACH_PORT_TYPE(MACH_PORT_RIGHT_RECEIVE)
292 #define MACH_PORT_TYPE_SEND_ONCE    MACH_PORT_TYPE(MACH_PORT_RIGHT_SEND_ONCE)
293 #define MACH_PORT_TYPE_PORT_SET     MACH_PORT_TYPE(MACH_PORT_RIGHT_PORT_SET)
294 #define MACH_PORT_TYPE_DEAD_NAME    MACH_PORT_TYPE(MACH_PORT_RIGHT_DEAD_NAME)
295 #define MACH_PORT_TYPE_LABELH       MACH_PORT_TYPE(MACH_PORT_RIGHT_LABELH) /* obsolete */
296 
297 #ifdef MACH_KERNEL_PRIVATE
298 /* Holder used to have a receive right - remembered to filter exceptions */
299 #define MACH_PORT_TYPE_EX_RECEIVE   MACH_PORT_TYPE_LABELH
300 #endif
301 
302 /* Convenient combinations. */
303 
304 #define MACH_PORT_TYPE_SEND_RECEIVE                                     \
305 	        (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_RECEIVE)
306 #define MACH_PORT_TYPE_SEND_RIGHTS                                      \
307 	        (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_SEND_ONCE)
308 #define MACH_PORT_TYPE_PORT_RIGHTS                                      \
309 	        (MACH_PORT_TYPE_SEND_RIGHTS|MACH_PORT_TYPE_RECEIVE)
310 #define MACH_PORT_TYPE_PORT_OR_DEAD                                     \
311 	        (MACH_PORT_TYPE_PORT_RIGHTS|MACH_PORT_TYPE_DEAD_NAME)
312 #define MACH_PORT_TYPE_ALL_RIGHTS                                       \
313 	        (MACH_PORT_TYPE_PORT_OR_DEAD|MACH_PORT_TYPE_PORT_SET)
314 
315 /* Dummy type bits that mach_port_type/mach_port_names can return. */
316 
317 #define MACH_PORT_TYPE_DNREQUEST                0x80000000
318 #define MACH_PORT_TYPE_SPREQUEST                0x40000000
319 #define MACH_PORT_TYPE_SPREQUEST_DELAYED        0x20000000
320 
321 /* User-references for capabilities. */
322 
323 typedef natural_t mach_port_urefs_t;
324 typedef integer_t mach_port_delta_t;                    /* change in urefs */
325 
326 /* Attributes of ports.  (See mach_port_get_receive_status.) */
327 
328 typedef natural_t mach_port_seqno_t;            /* sequence number */
329 typedef natural_t mach_port_mscount_t;          /* make-send count */
330 typedef natural_t mach_port_msgcount_t;         /* number of msgs */
331 typedef natural_t mach_port_rights_t;           /* number of rights */
332 
333 /*
334  *	Are there outstanding send rights for a given port?
335  */
336 #define MACH_PORT_SRIGHTS_NONE          0               /* no srights */
337 #define MACH_PORT_SRIGHTS_PRESENT       1               /* srights */
338 typedef unsigned int mach_port_srights_t;       /* status of send rights */
339 
340 typedef struct mach_port_status {
341 	mach_port_rights_t      mps_pset;       /* count of containing port sets */
342 	mach_port_seqno_t       mps_seqno;      /* sequence number */
343 	mach_port_mscount_t     mps_mscount;    /* make-send count */
344 	mach_port_msgcount_t    mps_qlimit;     /* queue limit */
345 	mach_port_msgcount_t    mps_msgcount;   /* number in the queue */
346 	mach_port_rights_t      mps_sorights;   /* how many send-once rights */
347 	boolean_t               mps_srights;    /* do send rights exist? */
348 	boolean_t               mps_pdrequest;  /* port-deleted requested? */
349 	boolean_t               mps_nsrequest;  /* no-senders requested? */
350 	natural_t               mps_flags;              /* port flags */
351 } mach_port_status_t;
352 
353 /* System-wide values for setting queue limits on a port */
354 #define MACH_PORT_QLIMIT_ZERO           (0)
355 #define MACH_PORT_QLIMIT_BASIC          (5)
356 #define MACH_PORT_QLIMIT_SMALL          (16)
357 #define MACH_PORT_QLIMIT_LARGE          (1024)
358 #define MACH_PORT_QLIMIT_KERNEL         (65534)
359 #define MACH_PORT_QLIMIT_MIN            MACH_PORT_QLIMIT_ZERO
360 #define MACH_PORT_QLIMIT_DEFAULT        MACH_PORT_QLIMIT_BASIC
361 #define MACH_PORT_QLIMIT_MAX            MACH_PORT_QLIMIT_LARGE
362 
363 typedef struct mach_port_limits {
364 	mach_port_msgcount_t    mpl_qlimit;     /* number of msgs */
365 } mach_port_limits_t;
366 
367 /* Possible values for mps_flags (part of mach_port_status_t) */
368 #define MACH_PORT_STATUS_FLAG_TEMPOWNER         0x01
369 #define MACH_PORT_STATUS_FLAG_GUARDED           0x02
370 #define MACH_PORT_STATUS_FLAG_STRICT_GUARD      0x04
371 #define MACH_PORT_STATUS_FLAG_IMP_DONATION      0x08
372 #define MACH_PORT_STATUS_FLAG_REVIVE            0x10
373 #define MACH_PORT_STATUS_FLAG_TASKPTR           0x20
374 #define MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE 0x40
375 #define MACH_PORT_STATUS_FLAG_NO_GRANT          0x80
376 
377 typedef struct mach_port_info_ext {
378 	mach_port_status_t      mpie_status;
379 	mach_port_msgcount_t    mpie_boost_cnt;
380 	uint32_t                reserved[6];
381 } mach_port_info_ext_t;
382 
383 typedef struct mach_port_guard_info {
384 	uint64_t    mpgi_guard;     /* guard value */
385 } mach_port_guard_info_t;
386 
387 typedef integer_t *mach_port_info_t;            /* varying array of natural_t */
388 
389 /* Flavors for mach_port_get/set/assert_attributes() */
390 typedef int     mach_port_flavor_t;
391 #define MACH_PORT_LIMITS_INFO           1       /* uses mach_port_limits_t */
392 #define MACH_PORT_RECEIVE_STATUS        2       /* uses mach_port_status_t */
393 #define MACH_PORT_DNREQUESTS_SIZE       3       /* info is int */
394 #define MACH_PORT_TEMPOWNER             4       /* indicates receive right will be reassigned to another task */
395 #define MACH_PORT_IMPORTANCE_RECEIVER   5       /* indicates recieve right accepts priority donation */
396 #define MACH_PORT_DENAP_RECEIVER        6       /* indicates receive right accepts de-nap donation */
397 #define MACH_PORT_INFO_EXT              7       /* uses mach_port_info_ext_t */
398 #define MACH_PORT_GUARD_INFO            8       /* asserts if the strict guard value is correct */
399 #define MACH_PORT_SERVICE_THROTTLED     9       /* info is an integer that indicates if service port is throttled or not */
400 
401 #define MACH_PORT_LIMITS_INFO_COUNT     ((natural_t) \
402 	(sizeof(mach_port_limits_t)/sizeof(natural_t)))
403 #define MACH_PORT_RECEIVE_STATUS_COUNT  ((natural_t) \
404 	(sizeof(mach_port_status_t)/sizeof(natural_t)))
405 #define MACH_PORT_DNREQUESTS_SIZE_COUNT 1
406 #define MACH_PORT_INFO_EXT_COUNT        ((natural_t) \
407 	(sizeof(mach_port_info_ext_t)/sizeof(natural_t)))
408 #define MACH_PORT_GUARD_INFO_COUNT      ((natural_t) \
409 	(sizeof(mach_port_guard_info_t)/sizeof(natural_t)))
410 #define MACH_PORT_SERVICE_THROTTLED_COUNT 1
411 
412 /*
413  * Structure used to pass information about port allocation requests.
414  * Must be padded to 64-bits total length.
415  */
416 typedef struct mach_port_qos {
417 	unsigned int            name:1;         /* name given */
418 	unsigned int            prealloc:1;     /* prealloced message */
419 	boolean_t               pad1:30;
420 	natural_t               len;
421 } mach_port_qos_t;
422 
423 /*
424  * Structure used to pass information about the service port
425  */
426 #define MACH_SERVICE_PORT_INFO_STRING_NAME_MAX_BUF_LEN  255    /* Maximum length of the port string name buffer */
427 
428 typedef struct mach_service_port_info {
429 	char                    mspi_string_name[MACH_SERVICE_PORT_INFO_STRING_NAME_MAX_BUF_LEN]; /* Service port's string name */
430 	uint8_t                 mspi_domain_type;          /* Service port domain */
431 } mach_service_port_info_data_t;
432 
433 #define MACH_SERVICE_PORT_INFO_COUNT ((char) \
434 	(sizeof(mach_service_port_info_data_t)/sizeof(char)))
435 
436 typedef struct mach_service_port_info * mach_service_port_info_t;
437 
438 /*
439  * Flags for mach_port_options (used for
440  * invocation of mach_port_construct).
441  * Indicates attributes to be set for the newly
442  * allocated port.
443  */
444 #define MPO_CONTEXT_AS_GUARD               0x01    /* Add guard to the port */
445 #define MPO_QLIMIT                         0x02    /* Set qlimit for the port msg queue */
446 #define MPO_TEMPOWNER                      0x04    /* Set the tempowner bit of the port */
447 #define MPO_IMPORTANCE_RECEIVER            0x08    /* Mark the port as importance receiver */
448 #define MPO_INSERT_SEND_RIGHT              0x10    /* Insert a send right for the port */
449 #define MPO_STRICT                         0x20    /* Apply strict guarding for port */
450 #define MPO_DENAP_RECEIVER                 0x40    /* Mark the port as App de-nap receiver */
451 #define MPO_IMMOVABLE_RECEIVE              0x80    /* Mark the port as immovable; protected by the guard context */
452 #define MPO_FILTER_MSG                     0x100   /* Allow message filtering */
453 #define MPO_TG_BLOCK_TRACKING              0x200   /* Track blocking relationship for thread group during sync IPC */
454 #define MPO_SERVICE_PORT                   0x400   /* Create a service port with the given name; should be used only by launchd */
455 #define MPO_CONNECTION_PORT                0x800   /* Derive new peer connection port from a given service port */
456 #define MPO_REPLY_PORT                     0x1000  /* Designate port as a reply port. */
457 #define MPO_ENFORCE_REPLY_PORT_SEMANTICS   0x2000  /* When talking to this port, local port of mach msg needs to follow reply port semantics.*/
458 #define MPO_PROVISIONAL_REPLY_PORT         0x4000  /* Designate port as a provisional reply port. */
459 #define MPO_PROVISIONAL_ID_PROT_OPTOUT     0x8000  /* Opted out of EXCEPTION_IDENTITY_PROTECTED violation for now */
460 
461 
462 /*
463  * Structure to define optional attributes for a newly
464  * constructed port.
465  */
466 typedef struct mach_port_options {
467 	uint32_t                flags;          /* Flags defining attributes for port */
468 	mach_port_limits_t      mpl;            /* Message queue limit for port */
469 	union {
470 		uint64_t                   reserved[2];           /* Reserved */
471 		mach_port_name_t           work_interval_port;    /* Work interval port */
472 #if KERNEL
473 		uint32_t                   service_port_info32;   /* Service port (MPO_SERVICE_PORT) */
474 		uint64_t                   service_port_info64;   /* Service port (MPO_SERVICE_PORT) */
475 #else
476 		mach_service_port_info_t   service_port_info;     /* Service port (MPO_SERVICE_PORT) */
477 #endif
478 		mach_port_name_t           service_port_name;     /* Service port (MPO_CONNECTION_PORT) */
479 	};
480 }mach_port_options_t;
481 
482 typedef mach_port_options_t *mach_port_options_ptr_t;
483 
484 /* Mach Port Guarding definitions */
485 
486 /*
487  * EXC_GUARD represents a guard violation for both
488  * mach ports and file descriptors. GUARD_TYPE_ is used
489  * to differentiate among them.
490  */
491 #define GUARD_TYPE_MACH_PORT    0x1
492 
493 /* Reasons for exception for a guarded mach port */
494 enum mach_port_guard_exception_codes {
495 	kGUARD_EXC_DESTROY                   = 1,
496 	kGUARD_EXC_MOD_REFS                  = 2,
497 	kGUARD_EXC_INVALID_OPTIONS           = 3,
498 	kGUARD_EXC_SET_CONTEXT               = 4,
499 	kGUARD_EXC_THREAD_SET_STATE          = 5,
500 	kGUARD_EXC_EXCEPTION_BEHAVIOR_ENFORCE= 6,
501 	kGUARD_EXC_UNGUARDED                 = 1u << 3,
502 	kGUARD_EXC_INCORRECT_GUARD           = 1u << 4,
503 	kGUARD_EXC_IMMOVABLE                 = 1u << 5,
504 	kGUARD_EXC_STRICT_REPLY              = 1u << 6,
505 	kGUARD_EXC_MSG_FILTERED              = 1u << 7,
506 	/* start of [optionally] non-fatal guards */
507 	kGUARD_EXC_INVALID_RIGHT         = 1u << 8,
508 	kGUARD_EXC_INVALID_NAME          = 1u << 9,
509 	kGUARD_EXC_INVALID_VALUE         = 1u << 10,
510 	kGUARD_EXC_INVALID_ARGUMENT      = 1u << 11,
511 	kGUARD_EXC_RIGHT_EXISTS          = 1u << 12,
512 	kGUARD_EXC_KERN_NO_SPACE         = 1u << 13,
513 	kGUARD_EXC_KERN_FAILURE          = 1u << 14,
514 	kGUARD_EXC_KERN_RESOURCE         = 1u << 15,
515 	kGUARD_EXC_SEND_INVALID_REPLY    = 1u << 16,
516 	kGUARD_EXC_SEND_INVALID_VOUCHER  = 1u << 17,
517 	kGUARD_EXC_SEND_INVALID_RIGHT    = 1u << 18,
518 	kGUARD_EXC_RCV_INVALID_NAME      = 1u << 19,
519 	/* start of always non-fatal guards */
520 	kGUARD_EXC_RCV_GUARDED_DESC             = 1u << 20, /* for development only */
521 	kGUARD_EXC_MOD_REFS_NON_FATAL           = 1u << 21,
522 	kGUARD_EXC_IMMOVABLE_NON_FATAL          = 1u << 22,
523 	kGUARD_EXC_REQUIRE_REPLY_PORT_SEMANTICS = 1u << 23,
524 };
525 
526 #define MAX_FATAL_kGUARD_EXC_CODE (1u << 7)
527 
528 /*
529  * Mach port guard flags.
530  */
531 #define MPG_FLAGS_NONE                             (0x00ull)
532 
533 #define MAX_OPTIONAL_kGUARD_EXC_CODE (1u << 19)
534 
535 /*
536  * These flags are used as bits in the subcode of kGUARD_EXC_STRICT_REPLY exceptions.
537  */
538 #define MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_DISP  (0x01ull << 56)
539 #define MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_PORT  (0x02ull << 56)
540 #define MPG_FLAGS_STRICT_REPLY_INVALID_VOUCHER     (0x04ull << 56)
541 #define MPG_FLAGS_STRICT_REPLY_NO_BANK_ATTR        (0x08ull << 56)
542 #define MPG_FLAGS_STRICT_REPLY_MISMATCHED_PERSONA  (0x10ull << 56)
543 #define MPG_FLAGS_STRICT_REPLY_MASK                (0xffull << 56)
544 
545 /*
546  * These flags are used as bits in the subcode of kGUARD_EXC_MOD_REFS exceptions.
547  */
548 #define MPG_FLAGS_MOD_REFS_PINNED_DEALLOC          (0x01ull << 56)
549 #define MPG_FLAGS_MOD_REFS_PINNED_DESTROY          (0x02ull << 56)
550 #define MPG_FLAGS_MOD_REFS_PINNED_COPYIN           (0x04ull << 56)
551 
552 /*
553  * These flags are used as bits in the subcode of kGUARD_EXC_IMMOVABLE exceptions.
554  */
555 #define MPG_FLAGS_IMMOVABLE_PINNED                 (0x01ull << 56)
556 
557 /*
558  * Flags for mach_port_guard_with_flags. These flags extend
559  * the attributes associated with a guarded port.
560  */
561 #define MPG_STRICT              0x01    /* Apply strict guarding for a port */
562 #define MPG_IMMOVABLE_RECEIVE   0x02    /* Receive right cannot be moved out of the space */
563 
564 #if     !__DARWIN_UNIX03 && !defined(_NO_PORT_T_FROM_MACH)
565 /*
566  *  Mach 3.0 renamed everything to have mach_ in front of it.
567  *  These types and macros are provided for backward compatibility
568  *	but are deprecated.
569  */
570 typedef mach_port_t             port_t;
571 typedef mach_port_name_t        port_name_t;
572 typedef mach_port_name_t        *port_name_array_t;
573 
574 #define PORT_NULL               ((port_t) 0)
575 #define PORT_DEAD               ((port_t) ~0)
576 #define PORT_VALID(name) \
577 	        ((port_t)(name) != PORT_NULL && (port_t)(name) != PORT_DEAD)
578 
579 #endif  /* !__DARWIN_UNIX03 && !_NO_PORT_T_FROM_MACH */
580 
581 #endif  /* _MACH_PORT_H_ */
582