xref: /xnu-8020.140.41/osfmk/mach/message.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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  [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  * Copyright (c) 2005 SPARTA, Inc.
62  */
63 /*
64  */
65 /*
66  *	File:	mach/message.h
67  *
68  *	Mach IPC message and primitive function definitions.
69  */
70 
71 #ifndef _MACH_MESSAGE_H_
72 #define _MACH_MESSAGE_H_
73 
74 #include <stdint.h>
75 #include <mach/port.h>
76 #include <mach/boolean.h>
77 #include <mach/kern_return.h>
78 #include <mach/machine/vm_types.h>
79 
80 #include <sys/cdefs.h>
81 #include <sys/appleapiopts.h>
82 #include <Availability.h>
83 #if !KERNEL && PRIVATE
84 #include <TargetConditionals.h>
85 #endif
86 
87 /*
88  *  The timeout mechanism uses mach_msg_timeout_t values,
89  *  passed by value.  The timeout units are milliseconds.
90  *  It is controlled with the MACH_SEND_TIMEOUT
91  *  and MACH_RCV_TIMEOUT options.
92  */
93 
94 typedef natural_t mach_msg_timeout_t;
95 
96 /*
97  *  The value to be used when there is no timeout.
98  *  (No MACH_SEND_TIMEOUT/MACH_RCV_TIMEOUT option.)
99  */
100 
101 #define MACH_MSG_TIMEOUT_NONE           ((mach_msg_timeout_t) 0)
102 
103 /*
104  *  The kernel uses MACH_MSGH_BITS_COMPLEX as a hint.  If it isn't on, it
105  *  assumes the body of the message doesn't contain port rights or OOL
106  *  data.  The field is set in received messages.  A user task must
107  *  use caution in interpreting the body of a message if the bit isn't
108  *  on, because the mach_msg_type's in the body might "lie" about the
109  *  contents.  If the bit isn't on, but the mach_msg_types
110  *  in the body specify rights or OOL data, the behavior is undefined.
111  *  (Ie, an error may or may not be produced.)
112  *
113  *  The value of MACH_MSGH_BITS_REMOTE determines the interpretation
114  *  of the msgh_remote_port field.  It is handled like a msgt_name,
115  *  but must result in a send or send-once type right.
116  *
117  *  The value of MACH_MSGH_BITS_LOCAL determines the interpretation
118  *  of the msgh_local_port field.  It is handled like a msgt_name,
119  *  and also must result in a send or send-once type right.
120  *
121  *  The value of MACH_MSGH_BITS_VOUCHER determines the interpretation
122  *  of the msgh_voucher_port field.  It is handled like a msgt_name,
123  *  but must result in a send right (and the msgh_voucher_port field
124  *  must be the name of a send right to a Mach voucher kernel object.
125  *
126  *  MACH_MSGH_BITS() combines two MACH_MSG_TYPE_* values, for the remote
127  *  and local fields, into a single value suitable for msgh_bits.
128  *
129  *  MACH_MSGH_BITS_CIRCULAR should be zero; is is used internally.
130  *
131  *  The unused bits should be zero and are reserved for the kernel
132  *  or for future interface expansion.
133  */
134 
135 #define MACH_MSGH_BITS_ZERO             0x00000000
136 
137 #define MACH_MSGH_BITS_REMOTE_MASK      0x0000001f
138 #define MACH_MSGH_BITS_LOCAL_MASK       0x00001f00
139 #define MACH_MSGH_BITS_VOUCHER_MASK     0x001f0000
140 
141 #define MACH_MSGH_BITS_PORTS_MASK               \
142 	        (MACH_MSGH_BITS_REMOTE_MASK |   \
143 	         MACH_MSGH_BITS_LOCAL_MASK |    \
144 	         MACH_MSGH_BITS_VOUCHER_MASK)
145 
146 #define MACH_MSGH_BITS_COMPLEX          0x80000000U     /* message is complex */
147 
148 #define MACH_MSGH_BITS_USER             0x801f1f1fU     /* allowed bits user->kernel */
149 
150 #define MACH_MSGH_BITS_RAISEIMP         0x20000000U     /* importance raised due to msg */
151 #define MACH_MSGH_BITS_DENAP            MACH_MSGH_BITS_RAISEIMP
152 
153 #define MACH_MSGH_BITS_IMPHOLDASRT      0x10000000U     /* assertion help, userland private */
154 #define MACH_MSGH_BITS_DENAPHOLDASRT    MACH_MSGH_BITS_IMPHOLDASRT
155 
156 #define MACH_MSGH_BITS_CIRCULAR         0x10000000U     /* message circular, kernel private */
157 
158 #define MACH_MSGH_BITS_USED             0xb01f1f1fU
159 
160 /* setter macros for the bits */
161 #define MACH_MSGH_BITS(remote, local)  /* legacy */             \
162 	        ((remote) | ((local) << 8))
163 #define MACH_MSGH_BITS_SET_PORTS(remote, local, voucher)        \
164 	(((remote) & MACH_MSGH_BITS_REMOTE_MASK) |              \
165 	 (((local) << 8) & MACH_MSGH_BITS_LOCAL_MASK) |         \
166 	 (((voucher) << 16) & MACH_MSGH_BITS_VOUCHER_MASK))
167 #define MACH_MSGH_BITS_SET(remote, local, voucher, other)       \
168 	(MACH_MSGH_BITS_SET_PORTS((remote), (local), (voucher)) \
169 	 | ((other) &~ MACH_MSGH_BITS_PORTS_MASK))
170 
171 /* getter macros for pulling values out of the bits field */
172 #define MACH_MSGH_BITS_REMOTE(bits)                             \
173 	        ((bits) & MACH_MSGH_BITS_REMOTE_MASK)
174 #define MACH_MSGH_BITS_LOCAL(bits)                              \
175 	        (((bits) & MACH_MSGH_BITS_LOCAL_MASK) >> 8)
176 #define MACH_MSGH_BITS_VOUCHER(bits)                            \
177 	        (((bits) & MACH_MSGH_BITS_VOUCHER_MASK) >> 16)
178 #define MACH_MSGH_BITS_PORTS(bits)                              \
179 	((bits) & MACH_MSGH_BITS_PORTS_MASK)
180 #define MACH_MSGH_BITS_OTHER(bits)                              \
181 	        ((bits) &~ MACH_MSGH_BITS_PORTS_MASK)
182 
183 /* checking macros */
184 #define MACH_MSGH_BITS_HAS_REMOTE(bits)                         \
185 	(MACH_MSGH_BITS_REMOTE(bits) != MACH_MSGH_BITS_ZERO)
186 #define MACH_MSGH_BITS_HAS_LOCAL(bits)                          \
187 	(MACH_MSGH_BITS_LOCAL(bits) != MACH_MSGH_BITS_ZERO)
188 #define MACH_MSGH_BITS_HAS_VOUCHER(bits)                        \
189 	(MACH_MSGH_BITS_VOUCHER(bits) != MACH_MSGH_BITS_ZERO)
190 #define MACH_MSGH_BITS_IS_COMPLEX(bits)                         \
191 	(((bits) & MACH_MSGH_BITS_COMPLEX) != MACH_MSGH_BITS_ZERO)
192 
193 /* importance checking macros */
194 #define MACH_MSGH_BITS_RAISED_IMPORTANCE(bits)                  \
195 	(((bits) & MACH_MSGH_BITS_RAISEIMP) != MACH_MSGH_BITS_ZERO)
196 #define MACH_MSGH_BITS_HOLDS_IMPORTANCE_ASSERTION(bits)         \
197 	(((bits) & MACH_MSGH_BITS_IMPHOLDASRT) != MACH_MSGH_BITS_ZERO)
198 
199 /*
200  *  Every message starts with a message header.
201  *  Following the message header, if the message is complex, are a count
202  *  of type descriptors and the type descriptors themselves
203  *  (mach_msg_descriptor_t). The size of the message must be specified in
204  *  bytes, and includes the message header, descriptor count, descriptors,
205  *  and inline data.
206  *
207  *  The msgh_remote_port field specifies the destination of the message.
208  *  It must specify a valid send or send-once right for a port.
209  *
210  *  The msgh_local_port field specifies a "reply port".  Normally,
211  *  This field carries a send-once right that the receiver will use
212  *  to reply to the message.  It may carry the values MACH_PORT_NULL,
213  *  MACH_PORT_DEAD, a send-once right, or a send right.
214  *
215  *  The msgh_voucher_port field specifies a Mach voucher port. Only
216  *  send rights to kernel-implemented Mach Voucher kernel objects in
217  *  addition to MACH_PORT_NULL or MACH_PORT_DEAD may be passed.
218  *
219  *  The msgh_id field is uninterpreted by the message primitives.
220  *  It normally carries information specifying the format
221  *  or meaning of the message.
222  */
223 
224 typedef unsigned int mach_msg_bits_t;
225 typedef natural_t mach_msg_size_t;
226 typedef integer_t mach_msg_id_t;
227 
228 #define MACH_MSG_SIZE_NULL (mach_msg_size_t *) 0
229 
230 typedef unsigned int mach_msg_priority_t;
231 
232 #define MACH_MSG_PRIORITY_UNSPECIFIED (mach_msg_priority_t) 0
233 
234 #if PRIVATE
235 typedef uint8_t mach_msg_qos_t; // same as thread_qos_t
236 #define MACH_MSG_QOS_UNSPECIFIED        0
237 #define MACH_MSG_QOS_MAINTENANCE        1
238 #define MACH_MSG_QOS_BACKGROUND         2
239 #define MACH_MSG_QOS_UTILITY            3
240 #define MACH_MSG_QOS_DEFAULT            4
241 #define MACH_MSG_QOS_USER_INITIATED     5
242 #define MACH_MSG_QOS_USER_INTERACTIVE   6
243 #define MACH_MSG_QOS_LAST               6
244 
245 extern int mach_msg_priority_is_pthread_priority(mach_msg_priority_t pri);
246 extern mach_msg_priority_t mach_msg_priority_encode(
247 	mach_msg_qos_t override_qos,
248 	mach_msg_qos_t qos,
249 	int relpri);
250 extern mach_msg_qos_t mach_msg_priority_overide_qos(mach_msg_priority_t pri);
251 extern mach_msg_qos_t mach_msg_priority_qos(mach_msg_priority_t pri);
252 extern int mach_msg_priority_relpri(mach_msg_priority_t pri);
253 
254 #if KERNEL || !TARGET_OS_SIMULATOR
255 static inline int
mach_msg_priority_is_pthread_priority_inline(mach_msg_priority_t pri)256 mach_msg_priority_is_pthread_priority_inline(mach_msg_priority_t pri)
257 {
258 	return (pri & 0xff) == 0xff;
259 }
260 
261 #define MACH_MSG_PRIORITY_RELPRI_SHIFT    8
262 #define MACH_MSG_PRIORITY_RELPRI_MASK     (0xff << MACH_MSG_PRIORITY_RELPRI_SHIFT)
263 #define MACH_MSG_PRIORITY_QOS_SHIFT       16
264 #define MACH_MSG_PRIORITY_QOS_MASK        (0xf << MACH_MSG_PRIORITY_QOS_SHIFT)
265 #define MACH_MSG_PRIORITY_OVERRIDE_SHIFT  20
266 #define MACH_MSG_PRIORITY_OVERRIDE_MASK   (0xf << MACH_MSG_PRIORITY_OVERRIDE_SHIFT)
267 
268 static inline mach_msg_priority_t
mach_msg_priority_encode_inline(mach_msg_qos_t override_qos,mach_msg_qos_t qos,int relpri)269 mach_msg_priority_encode_inline(mach_msg_qos_t override_qos, mach_msg_qos_t qos, int relpri)
270 {
271 	mach_msg_priority_t pri = 0;
272 	if (qos > 0 && qos <= MACH_MSG_QOS_LAST) {
273 		pri |= (uint32_t)(qos << MACH_MSG_PRIORITY_QOS_SHIFT);
274 		pri |= (uint32_t)((uint8_t)(relpri - 1) << MACH_MSG_PRIORITY_RELPRI_SHIFT);
275 	}
276 	if (override_qos > 0 && override_qos <= MACH_MSG_QOS_LAST) {
277 		pri |= (uint32_t)(override_qos << MACH_MSG_PRIORITY_OVERRIDE_SHIFT);
278 	}
279 	return pri;
280 }
281 
282 static inline mach_msg_qos_t
mach_msg_priority_overide_qos_inline(mach_msg_priority_t pri)283 mach_msg_priority_overide_qos_inline(mach_msg_priority_t pri)
284 {
285 	pri &= MACH_MSG_PRIORITY_OVERRIDE_MASK;
286 	pri >>= MACH_MSG_PRIORITY_OVERRIDE_SHIFT;
287 	return (mach_msg_qos_t)(pri <= MACH_MSG_QOS_LAST ? pri : 0);
288 }
289 
290 static inline mach_msg_qos_t
mach_msg_priority_qos_inline(mach_msg_priority_t pri)291 mach_msg_priority_qos_inline(mach_msg_priority_t pri)
292 {
293 	pri &= MACH_MSG_PRIORITY_QOS_MASK;
294 	pri >>= MACH_MSG_PRIORITY_QOS_SHIFT;
295 	return (mach_msg_qos_t)(pri <= MACH_MSG_QOS_LAST ? pri : 0);
296 }
297 
298 static inline int
mach_msg_priority_relpri_inline(mach_msg_priority_t pri)299 mach_msg_priority_relpri_inline(mach_msg_priority_t pri)
300 {
301 	if (mach_msg_priority_qos_inline(pri)) {
302 		return (int8_t)(pri >> MACH_MSG_PRIORITY_RELPRI_SHIFT) + 1;
303 	}
304 	return 0;
305 }
306 
307 #define mach_msg_priority_is_pthread_priority(...) \
308 	mach_msg_priority_is_pthread_priority_inline(__VA_ARGS__)
309 #define mach_msg_priority_encode(...) \
310 	mach_msg_priority_encode_inline(__VA_ARGS__)
311 #define mach_msg_priority_overide_qos(...) \
312 	mach_msg_priority_overide_qos_inline(__VA_ARGS__)
313 #define mach_msg_priority_qos(...) \
314 	mach_msg_priority_qos_inline(__VA_ARGS__)
315 #define mach_msg_priority_relpri(...) \
316 	mach_msg_priority_relpri_inline(__VA_ARGS__)
317 #endif
318 
319 #endif // PRIVATE
320 
321 typedef unsigned int mach_msg_type_name_t;
322 
323 #define MACH_MSG_TYPE_MOVE_RECEIVE      16      /* Must hold receive right */
324 #define MACH_MSG_TYPE_MOVE_SEND         17      /* Must hold send right(s) */
325 #define MACH_MSG_TYPE_MOVE_SEND_ONCE    18      /* Must hold sendonce right */
326 #define MACH_MSG_TYPE_COPY_SEND         19      /* Must hold send right(s) */
327 #define MACH_MSG_TYPE_MAKE_SEND         20      /* Must hold receive right */
328 #define MACH_MSG_TYPE_MAKE_SEND_ONCE    21      /* Must hold receive right */
329 #define MACH_MSG_TYPE_COPY_RECEIVE      22      /* NOT VALID */
330 #define MACH_MSG_TYPE_DISPOSE_RECEIVE   24      /* must hold receive right */
331 #define MACH_MSG_TYPE_DISPOSE_SEND      25      /* must hold send right(s) */
332 #define MACH_MSG_TYPE_DISPOSE_SEND_ONCE 26      /* must hold sendonce right */
333 
334 typedef unsigned int mach_msg_copy_options_t;
335 
336 #define MACH_MSG_PHYSICAL_COPY          0
337 #define MACH_MSG_VIRTUAL_COPY           1
338 #define MACH_MSG_ALLOCATE               2
339 #define MACH_MSG_OVERWRITE              3       /* deprecated */
340 #ifdef  MACH_KERNEL
341 #define MACH_MSG_KALLOC_COPY_T          4
342 #endif  /* MACH_KERNEL */
343 
344 #define MACH_MSG_GUARD_FLAGS_NONE                   0x0000
345 #define MACH_MSG_GUARD_FLAGS_IMMOVABLE_RECEIVE      0x0001    /* Move the receive right and mark it as immovable */
346 #define MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND      0x0002    /* Verify that the port is unguarded */
347 #define MACH_MSG_GUARD_FLAGS_MASK                   0x0003    /* Valid flag bits */
348 typedef unsigned int mach_msg_guard_flags_t;
349 
350 /*
351  * In a complex mach message, the mach_msg_header_t is followed by
352  * a descriptor count, then an array of that number of descriptors
353  * (mach_msg_*_descriptor_t). The type field of mach_msg_type_descriptor_t
354  * (which any descriptor can be cast to) indicates the flavor of the
355  * descriptor.
356  *
357  * Note that in LP64, the various types of descriptors are no longer all
358  * the same size as mach_msg_descriptor_t, so the array cannot be indexed
359  * as expected.
360  */
361 
362 typedef unsigned int mach_msg_descriptor_type_t;
363 
364 #define MACH_MSG_PORT_DESCRIPTOR                0
365 #define MACH_MSG_OOL_DESCRIPTOR                 1
366 #define MACH_MSG_OOL_PORTS_DESCRIPTOR           2
367 #define MACH_MSG_OOL_VOLATILE_DESCRIPTOR        3
368 #define MACH_MSG_GUARDED_PORT_DESCRIPTOR        4
369 
370 #pragma pack(push, 4)
371 
372 typedef struct{
373 	natural_t                     pad1;
374 	mach_msg_size_t               pad2;
375 	unsigned int                  pad3 : 24;
376 	mach_msg_descriptor_type_t    type : 8;
377 } mach_msg_type_descriptor_t;
378 
379 typedef struct{
380 	mach_port_t                   name;
381 #if !(defined(KERNEL) && defined(__LP64__))
382 // Pad to 8 bytes everywhere except the K64 kernel where mach_port_t is 8 bytes
383 	mach_msg_size_t               pad1;
384 #endif
385 	unsigned int                  pad2 : 16;
386 	mach_msg_type_name_t          disposition : 8;
387 	mach_msg_descriptor_type_t    type : 8;
388 #if defined(KERNEL)
389 	uint32_t          pad_end;
390 #endif
391 } mach_msg_port_descriptor_t;
392 
393 #if MACH_KERNEL_PRIVATE
394 typedef struct {
395 	mach_port_name_t              name;
396 	mach_msg_size_t               pad1;
397 	uint32_t                      pad2 : 16;
398 	mach_msg_type_name_t          disposition : 8;
399 	mach_msg_descriptor_type_t    type : 8;
400 } mach_msg_user_port_descriptor_t;
401 #endif /* MACH_KERNEL_PRIVATE */
402 
403 typedef struct{
404 	uint32_t                      address;
405 	mach_msg_size_t               size;
406 	boolean_t                     deallocate: 8;
407 	mach_msg_copy_options_t       copy: 8;
408 	unsigned int                  pad1: 8;
409 	mach_msg_descriptor_type_t    type: 8;
410 } mach_msg_ool_descriptor32_t;
411 
412 typedef struct{
413 	uint64_t                      address;
414 	boolean_t                     deallocate: 8;
415 	mach_msg_copy_options_t       copy: 8;
416 	unsigned int                  pad1: 8;
417 	mach_msg_descriptor_type_t    type: 8;
418 	mach_msg_size_t               size;
419 } mach_msg_ool_descriptor64_t;
420 
421 typedef struct{
422 	void*                         address;
423 #if !defined(__LP64__)
424 	mach_msg_size_t               size;
425 #endif
426 	boolean_t                     deallocate: 8;
427 	mach_msg_copy_options_t       copy: 8;
428 	unsigned int                  pad1: 8;
429 	mach_msg_descriptor_type_t    type: 8;
430 #if defined(__LP64__)
431 	mach_msg_size_t               size;
432 #endif
433 #if defined(KERNEL) && !defined(__LP64__)
434 	uint32_t          pad_end;
435 #endif
436 } mach_msg_ool_descriptor_t;
437 
438 typedef struct{
439 	uint32_t                      address;
440 	mach_msg_size_t               count;
441 	boolean_t                     deallocate: 8;
442 	mach_msg_copy_options_t       copy: 8;
443 	mach_msg_type_name_t          disposition : 8;
444 	mach_msg_descriptor_type_t    type : 8;
445 } mach_msg_ool_ports_descriptor32_t;
446 
447 typedef struct{
448 	uint64_t                      address;
449 	boolean_t                     deallocate: 8;
450 	mach_msg_copy_options_t       copy: 8;
451 	mach_msg_type_name_t          disposition : 8;
452 	mach_msg_descriptor_type_t    type : 8;
453 	mach_msg_size_t               count;
454 } mach_msg_ool_ports_descriptor64_t;
455 
456 typedef struct{
457 	void*                         address;
458 #if !defined(__LP64__)
459 	mach_msg_size_t               count;
460 #endif
461 	boolean_t                     deallocate: 8;
462 	mach_msg_copy_options_t       copy: 8;
463 	mach_msg_type_name_t          disposition : 8;
464 	mach_msg_descriptor_type_t    type : 8;
465 #if defined(__LP64__)
466 	mach_msg_size_t               count;
467 #endif
468 #if defined(KERNEL) && !defined(__LP64__)
469 	uint32_t          pad_end;
470 #endif
471 } mach_msg_ool_ports_descriptor_t;
472 
473 typedef struct{
474 	uint32_t                      context;
475 	mach_port_name_t              name;
476 	mach_msg_guard_flags_t        flags : 16;
477 	mach_msg_type_name_t          disposition : 8;
478 	mach_msg_descriptor_type_t    type : 8;
479 } mach_msg_guarded_port_descriptor32_t;
480 
481 typedef struct{
482 	uint64_t                      context;
483 	mach_msg_guard_flags_t        flags : 16;
484 	mach_msg_type_name_t          disposition : 8;
485 	mach_msg_descriptor_type_t    type : 8;
486 	mach_port_name_t              name;
487 } mach_msg_guarded_port_descriptor64_t;
488 
489 typedef struct{
490 #if defined(KERNEL)
491 	mach_port_t                   name;
492 #if !defined(__LP64__)
493 	uint32_t                      pad1;
494 #endif
495 	mach_msg_guard_flags_t        flags : 16;
496 	mach_msg_type_name_t          disposition : 8;
497 	mach_msg_descriptor_type_t    type : 8;
498 #if defined(__LP64__)
499 	uint32_t                      pad_end;
500 #endif /* defined(__LP64__) */
501 #else
502 	mach_port_context_t           context;
503 #if !defined(__LP64__)
504 	mach_port_name_t              name;
505 #endif
506 	mach_msg_guard_flags_t        flags : 16;
507 	mach_msg_type_name_t          disposition : 8;
508 	mach_msg_descriptor_type_t    type : 8;
509 #if defined(__LP64__)
510 	mach_port_name_t              name;
511 #endif /* defined(__LP64__) */
512 #endif /* defined(KERNEL) */
513 } mach_msg_guarded_port_descriptor_t;
514 
515 /*
516  * LP64support - This union definition is not really
517  * appropriate in LP64 mode because not all descriptors
518  * are of the same size in that environment.
519  */
520 #if defined(__LP64__) && defined(KERNEL)
521 typedef union{
522 	mach_msg_port_descriptor_t            port;
523 	mach_msg_ool_descriptor32_t           out_of_line;
524 	mach_msg_ool_ports_descriptor32_t     ool_ports;
525 	mach_msg_type_descriptor_t            type;
526 	mach_msg_guarded_port_descriptor32_t  guarded_port;
527 } mach_msg_descriptor_t;
528 #else
529 typedef union{
530 	mach_msg_port_descriptor_t            port;
531 	mach_msg_ool_descriptor_t             out_of_line;
532 	mach_msg_ool_ports_descriptor_t       ool_ports;
533 	mach_msg_type_descriptor_t            type;
534 	mach_msg_guarded_port_descriptor_t    guarded_port;
535 } mach_msg_descriptor_t;
536 #endif
537 
538 typedef struct{
539 	mach_msg_size_t msgh_descriptor_count;
540 } mach_msg_body_t;
541 
542 #define MACH_MSG_BODY_NULL            ((mach_msg_body_t *) 0)
543 #define MACH_MSG_DESCRIPTOR_NULL      ((mach_msg_descriptor_t *) 0)
544 
545 typedef struct{
546 	mach_msg_bits_t               msgh_bits;
547 	mach_msg_size_t               msgh_size;
548 	mach_port_t                   msgh_remote_port;
549 	mach_port_t                   msgh_local_port;
550 	mach_port_name_t              msgh_voucher_port;
551 	mach_msg_id_t                 msgh_id;
552 } mach_msg_header_t;
553 
554 #define msgh_reserved                 msgh_voucher_port
555 #define MACH_MSG_NULL                 ((mach_msg_header_t *) 0)
556 
557 typedef struct{
558 	mach_msg_header_t             header;
559 	mach_msg_body_t               body;
560 } mach_msg_base_t;
561 
562 #if MACH_KERNEL_PRIVATE
563 typedef struct {
564 	mach_msg_bits_t               msgh_bits;
565 	mach_msg_size_t               msgh_size;
566 	mach_port_name_t              msgh_remote_port;
567 	mach_port_name_t              msgh_local_port;
568 	mach_port_name_t              msgh_voucher_port;
569 	mach_msg_id_t                 msgh_id;
570 } mach_msg_user_header_t;
571 
572 typedef struct {
573 	mach_msg_user_header_t        header;
574 	mach_msg_body_t               body;
575 } mach_msg_user_base_t;
576 #endif /* MACH_KERNEL_PRIVATE */
577 
578 typedef unsigned int mach_msg_trailer_type_t;
579 
580 #define MACH_MSG_TRAILER_FORMAT_0       0
581 
582 typedef unsigned int mach_msg_trailer_size_t;
583 typedef char *mach_msg_trailer_info_t;
584 
585 typedef struct{
586 	mach_msg_trailer_type_t       msgh_trailer_type;
587 	mach_msg_trailer_size_t       msgh_trailer_size;
588 } mach_msg_trailer_t;
589 
590 /*
591  *  The msgh_seqno field carries a sequence number
592  *  associated with the received-from port.  A port's
593  *  sequence number is incremented every time a message
594  *  is received from it and included in the received
595  *  trailer to help put messages back in sequence if
596  *  multiple threads receive and/or process received
597  *  messages.
598  */
599 typedef struct{
600 	mach_msg_trailer_type_t       msgh_trailer_type;
601 	mach_msg_trailer_size_t       msgh_trailer_size;
602 	mach_port_seqno_t             msgh_seqno;
603 } mach_msg_seqno_trailer_t;
604 
605 typedef struct{
606 	unsigned int                  val[2];
607 } security_token_t;
608 
609 typedef struct{
610 	mach_msg_trailer_type_t       msgh_trailer_type;
611 	mach_msg_trailer_size_t       msgh_trailer_size;
612 	mach_port_seqno_t             msgh_seqno;
613 	security_token_t              msgh_sender;
614 } mach_msg_security_trailer_t;
615 
616 /*
617  * The audit token is an opaque token which identifies
618  * Mach tasks and senders of Mach messages as subjects
619  * to the BSM audit system.  Only the appropriate BSM
620  * library routines should be used to interpret the
621  * contents of the audit token as the representation
622  * of the subject identity within the token may change
623  * over time.
624  */
625 typedef struct{
626 	unsigned int                  val[8];
627 } audit_token_t;
628 
629 typedef struct{
630 	mach_msg_trailer_type_t       msgh_trailer_type;
631 	mach_msg_trailer_size_t       msgh_trailer_size;
632 	mach_port_seqno_t             msgh_seqno;
633 	security_token_t              msgh_sender;
634 	audit_token_t                 msgh_audit;
635 } mach_msg_audit_trailer_t;
636 
637 typedef struct{
638 	mach_msg_trailer_type_t       msgh_trailer_type;
639 	mach_msg_trailer_size_t       msgh_trailer_size;
640 	mach_port_seqno_t             msgh_seqno;
641 	security_token_t              msgh_sender;
642 	audit_token_t                 msgh_audit;
643 	mach_port_context_t           msgh_context;
644 } mach_msg_context_trailer_t;
645 
646 #if defined(MACH_KERNEL_PRIVATE) && defined(__arm64__)
647 typedef struct{
648 	mach_msg_trailer_type_t       msgh_trailer_type;
649 	mach_msg_trailer_size_t       msgh_trailer_size;
650 	mach_port_seqno_t             msgh_seqno;
651 	security_token_t              msgh_sender;
652 	audit_token_t                 msgh_audit;
653 	mach_port_context32_t         msgh_context;
654 } mach_msg_context_trailer32_t;
655 
656 typedef struct{
657 	mach_msg_trailer_type_t       msgh_trailer_type;
658 	mach_msg_trailer_size_t       msgh_trailer_size;
659 	mach_port_seqno_t             msgh_seqno;
660 	security_token_t              msgh_sender;
661 	audit_token_t                 msgh_audit;
662 	mach_port_context64_t         msgh_context;
663 } mach_msg_context_trailer64_t;
664 #endif
665 
666 
667 typedef struct{
668 	mach_port_name_t sender;
669 } msg_labels_t;
670 
671 typedef int mach_msg_filter_id;
672 #define MACH_MSG_FILTER_POLICY_ALLOW (mach_msg_filter_id)0
673 
674 /*
675  *  Trailer type to pass MAC policy label info as a mach message trailer.
676  *
677  */
678 
679 typedef struct{
680 	mach_msg_trailer_type_t       msgh_trailer_type;
681 	mach_msg_trailer_size_t       msgh_trailer_size;
682 	mach_port_seqno_t             msgh_seqno;
683 	security_token_t              msgh_sender;
684 	audit_token_t                 msgh_audit;
685 	mach_port_context_t           msgh_context;
686 	mach_msg_filter_id            msgh_ad;
687 	msg_labels_t                  msgh_labels;
688 } mach_msg_mac_trailer_t;
689 
690 #if defined(MACH_KERNEL_PRIVATE) && defined(__arm64__)
691 typedef struct{
692 	mach_msg_trailer_type_t       msgh_trailer_type;
693 	mach_msg_trailer_size_t       msgh_trailer_size;
694 	mach_port_seqno_t             msgh_seqno;
695 	security_token_t              msgh_sender;
696 	audit_token_t                 msgh_audit;
697 	mach_port_context32_t         msgh_context;
698 	mach_msg_filter_id            msgh_ad;
699 	msg_labels_t                  msgh_labels;
700 } mach_msg_mac_trailer32_t;
701 
702 typedef struct{
703 	mach_msg_trailer_type_t       msgh_trailer_type;
704 	mach_msg_trailer_size_t       msgh_trailer_size;
705 	mach_port_seqno_t             msgh_seqno;
706 	security_token_t              msgh_sender;
707 	audit_token_t                 msgh_audit;
708 	mach_port_context64_t         msgh_context;
709 	mach_msg_filter_id            msgh_ad;
710 	msg_labels_t                  msgh_labels;
711 } mach_msg_mac_trailer64_t;
712 
713 #endif
714 
715 #define MACH_MSG_TRAILER_MINIMUM_SIZE  sizeof(mach_msg_trailer_t)
716 
717 /*
718  * These values can change from release to release - but clearly
719  * code cannot request additional trailer elements one was not
720  * compiled to understand.  Therefore, it is safe to use this
721  * constant when the same module specified the receive options.
722  * Otherwise, you run the risk that the options requested by
723  * another module may exceed the local modules notion of
724  * MAX_TRAILER_SIZE.
725  */
726 #if defined(MACH_KERNEL_PRIVATE) && defined(__arm64__)
727 typedef mach_msg_mac_trailer64_t mach_msg_max_trailer64_t;
728 typedef mach_msg_mac_trailer32_t mach_msg_max_trailer32_t;
729 #endif
730 
731 typedef mach_msg_mac_trailer_t mach_msg_max_trailer_t;
732 #define MAX_TRAILER_SIZE ((mach_msg_size_t)sizeof(mach_msg_max_trailer_t))
733 
734 /*
735  * Legacy requirements keep us from ever updating these defines (even
736  * when the format_0 trailers gain new option data fields in the future).
737  * Therefore, they shouldn't be used going forward.  Instead, the sizes
738  * should be compared against the specific element size requested using
739  * REQUESTED_TRAILER_SIZE.
740  */
741 typedef mach_msg_security_trailer_t mach_msg_format_0_trailer_t;
742 
743 /*typedef mach_msg_mac_trailer_t mach_msg_format_0_trailer_t;
744  */
745 
746 #define MACH_MSG_TRAILER_FORMAT_0_SIZE sizeof(mach_msg_format_0_trailer_t)
747 
748 #define   KERNEL_SECURITY_TOKEN_VALUE  { {0, 1} }
749 extern const security_token_t KERNEL_SECURITY_TOKEN;
750 
751 #define   KERNEL_AUDIT_TOKEN_VALUE  { {0, 0, 0, 0, 0, 0, 0, 0} }
752 extern const audit_token_t KERNEL_AUDIT_TOKEN;
753 
754 typedef integer_t mach_msg_options_t;
755 
756 typedef struct{
757 	mach_msg_header_t     header;
758 } mach_msg_empty_send_t;
759 
760 typedef struct{
761 	mach_msg_header_t     header;
762 	mach_msg_trailer_t    trailer;
763 } mach_msg_empty_rcv_t;
764 
765 typedef union{
766 	mach_msg_empty_send_t send;
767 	mach_msg_empty_rcv_t  rcv;
768 } mach_msg_empty_t;
769 
770 #pragma pack(pop)
771 
772 /* utility to round the message size - will become machine dependent */
773 #define round_msg(x)    (((mach_msg_size_t)(x) + sizeof (natural_t) - 1) & \
774 	                        ~(sizeof (natural_t) - 1))
775 
776 #ifdef XNU_KERNEL_PRIVATE
777 
778 #include <os/base.h>
779 #include <os/overflow.h>
780 #include <kern/debug.h>
781 
782 #define round_msg_overflow(in, out) __os_warn_unused(({ \
783 	        bool __ovr = os_add_overflow(in, (__typeof__(*out))(sizeof(natural_t) - 1), out); \
784 	        *out &= ~((__typeof__(*out))(sizeof(natural_t) - 1)); \
785 	        __ovr; \
786 	}))
787 
788 static inline mach_msg_size_t
mach_round_msg(mach_msg_size_t x)789 mach_round_msg(mach_msg_size_t x)
790 {
791 	if (round_msg_overflow(x, &x)) {
792 		panic("round msg overflow");
793 	}
794 	return x;
795 }
796 #endif /* XNU_KERNEL_PRIVATE */
797 
798 /*
799  *  There is no fixed upper bound to the size of Mach messages.
800  */
801 #define MACH_MSG_SIZE_MAX       ((mach_msg_size_t) ~0)
802 
803 #if defined(__APPLE_API_PRIVATE)
804 /*
805  *  But architectural limits of a given implementation, or
806  *  temporal conditions may cause unpredictable send failures
807  *  for messages larger than MACH_MSG_SIZE_RELIABLE.
808  *
809  *  In either case, waiting for memory is [currently] outside
810  *  the scope of send timeout values provided to IPC.
811  */
812 #define MACH_MSG_SIZE_RELIABLE  ((mach_msg_size_t) 256 * 1024)
813 #endif
814 /*
815  *  Compatibility definitions, for code written
816  *  when there was a msgh_kind instead of msgh_seqno.
817  */
818 #define MACH_MSGH_KIND_NORMAL           0x00000000
819 #define MACH_MSGH_KIND_NOTIFICATION     0x00000001
820 #define msgh_kind                       msgh_seqno
821 #define mach_msg_kind_t                 mach_port_seqno_t
822 
823 typedef natural_t mach_msg_type_size_t;
824 typedef natural_t mach_msg_type_number_t;
825 
826 /*
827  *  Values received/carried in messages.  Tells the receiver what
828  *  sort of port right he now has.
829  *
830  *  MACH_MSG_TYPE_PORT_NAME is used to transfer a port name
831  *  which should remain uninterpreted by the kernel.  (Port rights
832  *  are not transferred, just the port name.)
833  */
834 
835 #define MACH_MSG_TYPE_PORT_NONE         0
836 
837 #define MACH_MSG_TYPE_PORT_NAME         15
838 #define MACH_MSG_TYPE_PORT_RECEIVE      MACH_MSG_TYPE_MOVE_RECEIVE
839 #define MACH_MSG_TYPE_PORT_SEND         MACH_MSG_TYPE_MOVE_SEND
840 #define MACH_MSG_TYPE_PORT_SEND_ONCE    MACH_MSG_TYPE_MOVE_SEND_ONCE
841 
842 #define MACH_MSG_TYPE_LAST              22              /* Last assigned */
843 
844 /*
845  *  A dummy value.  Mostly used to indicate that the actual value
846  *  will be filled in later, dynamically.
847  */
848 
849 #define MACH_MSG_TYPE_POLYMORPHIC       ((mach_msg_type_name_t) -1)
850 
851 /*
852  *	Is a given item a port type?
853  */
854 
855 #define MACH_MSG_TYPE_PORT_ANY(x)                       \
856 	(((x) >= MACH_MSG_TYPE_MOVE_RECEIVE) &&         \
857 	 ((x) <= MACH_MSG_TYPE_MAKE_SEND_ONCE))
858 
859 #define MACH_MSG_TYPE_PORT_ANY_SEND(x)                  \
860 	(((x) >= MACH_MSG_TYPE_MOVE_SEND) &&            \
861 	 ((x) <= MACH_MSG_TYPE_MAKE_SEND_ONCE))
862 
863 #define MACH_MSG_TYPE_PORT_ANY_RIGHT(x)                 \
864 	(((x) >= MACH_MSG_TYPE_MOVE_RECEIVE) &&         \
865 	 ((x) <= MACH_MSG_TYPE_MOVE_SEND_ONCE))
866 
867 typedef integer_t mach_msg_option_t;
868 
869 #define MACH_MSG_OPTION_NONE    0x00000000
870 
871 #define MACH_SEND_MSG           0x00000001
872 #define MACH_RCV_MSG            0x00000002
873 
874 #define MACH_RCV_LARGE          0x00000004      /* report large message sizes */
875 #define MACH_RCV_LARGE_IDENTITY 0x00000008      /* identify source of large messages */
876 
877 #define MACH_SEND_TIMEOUT       0x00000010      /* timeout value applies to send */
878 #define MACH_SEND_OVERRIDE      0x00000020      /* priority override for send */
879 #define MACH_SEND_INTERRUPT     0x00000040      /* don't restart interrupted sends */
880 #define MACH_SEND_NOTIFY        0x00000080      /* arm send-possible notify */
881 #define MACH_SEND_ALWAYS        0x00010000      /* ignore qlimits - kernel only */
882 #define MACH_SEND_FILTER_NONFATAL        0x00010000      /* rejection by message filter should return failure - user only */
883 #define MACH_SEND_TRAILER       0x00020000      /* sender-provided trailer */
884 #define MACH_SEND_NOIMPORTANCE  0x00040000      /* msg won't carry importance */
885 #define MACH_SEND_NODENAP       MACH_SEND_NOIMPORTANCE
886 #define MACH_SEND_IMPORTANCE    0x00080000      /* msg carries importance - kernel only */
887 #define MACH_SEND_SYNC_OVERRIDE 0x00100000      /* msg should do sync IPC override (on legacy kernels) */
888 #define MACH_SEND_PROPAGATE_QOS 0x00200000      /* IPC should propagate the caller's QoS */
889 #define MACH_SEND_SYNC_USE_THRPRI       MACH_SEND_PROPAGATE_QOS /* obsolete name */
890 #define MACH_SEND_KERNEL        0x00400000      /* full send from kernel space - kernel only */
891 #define MACH_SEND_SYNC_BOOTSTRAP_CHECKIN  0x00800000      /* special reply port should boost thread doing sync bootstrap checkin */
892 
893 #define MACH_RCV_TIMEOUT        0x00000100      /* timeout value applies to receive */
894 #define MACH_RCV_NOTIFY         0x00000000      /* legacy name (value was: 0x00000200) */
895 #define MACH_RCV_INTERRUPT      0x00000400      /* don't restart interrupted receive */
896 #define MACH_RCV_VOUCHER        0x00000800      /* willing to receive voucher port */
897 #define MACH_RCV_OVERWRITE      0x00000000      /* scatter receive (deprecated) */
898 #define MACH_RCV_GUARDED_DESC   0x00001000      /* Can receive new guarded descriptor */
899 #define MACH_RCV_SYNC_WAIT      0x00004000      /* sync waiter waiting for rcv */
900 #define MACH_RCV_SYNC_PEEK      0x00008000      /* sync waiter waiting to peek */
901 
902 #define MACH_MSG_STRICT_REPLY   0x00000200      /* Enforce specific properties about the reply port, and
903 	                                         * the context in which a thread replies to a message.
904 	                                         * This flag must be passed on both the SEND and RCV */
905 
906 #ifdef XNU_KERNEL_PRIVATE
907 
908 #define MACH_RCV_STACK          0x00002000      /* receive into highest addr of buffer */
909 
910 /*
911  * NOTE:
912  * This internal-only flag is intended for use by a single thread per-port/set!
913  * If more than one thread attempts to MACH_PEEK_MSG on a port or set, one of
914  * the threads may miss messages (in fact, it may never wake up).
915  */
916 #define MACH_PEEK_MSG           0x80000000      /* receive, but leave msgs queued */
917 
918 #endif
919 
920 /*
921  * NOTE: a 0x00------ RCV mask implies to ask for
922  * a MACH_MSG_TRAILER_FORMAT_0 with 0 Elements,
923  * which is equivalent to a mach_msg_trailer_t.
924  *
925  * XXXMAC: unlike the rest of the MACH_RCV_* flags, MACH_RCV_TRAILER_LABELS
926  * needs its own private bit since we only calculate its fields when absolutely
927  * required.
928  */
929 #define MACH_RCV_TRAILER_NULL   0
930 #define MACH_RCV_TRAILER_SEQNO  1
931 #define MACH_RCV_TRAILER_SENDER 2
932 #define MACH_RCV_TRAILER_AUDIT  3
933 #define MACH_RCV_TRAILER_CTX    4
934 #define MACH_RCV_TRAILER_AV     7
935 #define MACH_RCV_TRAILER_LABELS 8
936 
937 #define MACH_RCV_TRAILER_TYPE(x)     (((x) & 0xf) << 28)
938 #define MACH_RCV_TRAILER_ELEMENTS(x) (((x) & 0xf) << 24)
939 #define MACH_RCV_TRAILER_MASK        ((0xf << 24))
940 
941 #define GET_RCV_ELEMENTS(y) (((y) >> 24) & 0xf)
942 
943 #ifdef MACH_KERNEL_PRIVATE
944 /*
945  * The options that the kernel honors when passed from user space, not including
946  * user-only options that alias kernel-only options.
947  */
948 #define MACH_SEND_USER (MACH_SEND_MSG | MACH_SEND_TIMEOUT | \
949 	                                        MACH_SEND_NOTIFY | MACH_SEND_OVERRIDE | \
950 	                                        MACH_SEND_TRAILER | MACH_SEND_NOIMPORTANCE | \
951 	                                        MACH_SEND_SYNC_OVERRIDE | MACH_SEND_PROPAGATE_QOS | \
952 	                                        MACH_SEND_SYNC_BOOTSTRAP_CHECKIN | \
953 	                                        MACH_MSG_STRICT_REPLY | MACH_RCV_GUARDED_DESC)
954 
955 #define MACH_RCV_USER (MACH_RCV_MSG | MACH_RCV_TIMEOUT | \
956 	                                   MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY | \
957 	                                   MACH_RCV_VOUCHER | MACH_RCV_TRAILER_MASK | \
958 	                                   MACH_RCV_SYNC_WAIT | MACH_RCV_SYNC_PEEK  | \
959 	                                   MACH_RCV_GUARDED_DESC | MACH_MSG_STRICT_REPLY)
960 
961 #define MACH_MSG_OPTION_USER     (MACH_SEND_USER | MACH_RCV_USER)
962 
963 /* The options implemented by the library interface to mach_msg et. al. */
964 #define MACH_MSG_OPTION_LIB      (MACH_SEND_INTERRUPT | MACH_RCV_INTERRUPT)
965 
966 /*
967  * Default options to use when sending from the kernel.
968  *
969  * Until we are sure of its effects, we are disabling
970  * importance donation from the kernel-side of user
971  * threads in importance-donating tasks.
972  * (11938665 & 23925818)
973  */
974 #define MACH_SEND_KERNEL_DEFAULT (MACH_SEND_MSG | \
975 	                          MACH_SEND_ALWAYS | MACH_SEND_NOIMPORTANCE)
976 
977 #define MACH_SEND_WITH_STRICT_REPLY(_opts) (((_opts) & (MACH_MSG_STRICT_REPLY | MACH_SEND_MSG)) == \
978 	                                    (MACH_MSG_STRICT_REPLY | MACH_SEND_MSG))
979 
980 #define MACH_SEND_REPLY_IS_IMMOVABLE(_opts) (((_opts) & (MACH_MSG_STRICT_REPLY | \
981 	                                                 MACH_SEND_MSG | MACH_RCV_MSG | \
982 	                                                 MACH_RCV_GUARDED_DESC)) == \
983 	                                     (MACH_MSG_STRICT_REPLY | MACH_SEND_MSG | MACH_RCV_GUARDED_DESC))
984 
985 #define MACH_RCV_WITH_STRICT_REPLY(_opts)  (((_opts) & (MACH_MSG_STRICT_REPLY | MACH_RCV_MSG)) == \
986 	                                    (MACH_MSG_STRICT_REPLY | MACH_RCV_MSG))
987 
988 #define MACH_RCV_WITH_IMMOVABLE_REPLY(_opts) (((_opts) & (MACH_MSG_STRICT_REPLY | \
989 	                                                  MACH_RCV_MSG | MACH_RCV_GUARDED_DESC)) == \
990 	                                      (MACH_MSG_STRICT_REPLY | MACH_RCV_MSG | MACH_RCV_GUARDED_DESC))
991 
992 #endif /* MACH_KERNEL_PRIVATE */
993 
994 /*
995  * XXXMAC: note that in the case of MACH_RCV_TRAILER_LABELS,
996  * we just fall through to mach_msg_max_trailer_t.
997  * This is correct behavior since mach_msg_max_trailer_t is defined as
998  * mac_msg_mac_trailer_t which is used for the LABELS trailer.
999  * It also makes things work properly if MACH_RCV_TRAILER_LABELS is ORed
1000  * with one of the other options.
1001  */
1002 
1003 #define REQUESTED_TRAILER_SIZE_NATIVE(y)                        \
1004 	((mach_msg_trailer_size_t)                              \
1005 	 ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_NULL) ?      \
1006 	  sizeof(mach_msg_trailer_t) :                          \
1007 	  ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_SEQNO) ?    \
1008 	   sizeof(mach_msg_seqno_trailer_t) :                   \
1009 	  ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_SENDER) ?   \
1010 	   sizeof(mach_msg_security_trailer_t) :                \
1011 	   ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_AUDIT) ?   \
1012 	    sizeof(mach_msg_audit_trailer_t) :                  \
1013 	    ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_CTX) ?    \
1014 	     sizeof(mach_msg_context_trailer_t) :               \
1015 	     ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_AV) ?    \
1016 	      sizeof(mach_msg_mac_trailer_t) :                  \
1017 	     sizeof(mach_msg_max_trailer_t))))))))
1018 
1019 
1020 #ifdef XNU_KERNEL_PRIVATE
1021 
1022 #if defined(__arm64__)
1023 #define REQUESTED_TRAILER_SIZE(is64, y)                                 \
1024 	((mach_msg_trailer_size_t)                              \
1025 	 ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_NULL) ?      \
1026 	  sizeof(mach_msg_trailer_t) :                          \
1027 	  ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_SEQNO) ?    \
1028 	   sizeof(mach_msg_seqno_trailer_t) :                   \
1029 	  ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_SENDER) ?   \
1030 	   sizeof(mach_msg_security_trailer_t) :                \
1031 	   ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_AUDIT) ?   \
1032 	    sizeof(mach_msg_audit_trailer_t) :                  \
1033 	    ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_CTX) ?    \
1034 	     ((is64) ? sizeof(mach_msg_context_trailer64_t) : sizeof(mach_msg_context_trailer32_t)) : \
1035 	     ((GET_RCV_ELEMENTS(y) == MACH_RCV_TRAILER_AV) ?    \
1036 	      ((is64) ? sizeof(mach_msg_mac_trailer64_t) : sizeof(mach_msg_mac_trailer32_t)) : \
1037 	       sizeof(mach_msg_max_trailer_t))))))))
1038 #else
1039 #define REQUESTED_TRAILER_SIZE(is64, y) REQUESTED_TRAILER_SIZE_NATIVE(y)
1040 #endif
1041 
1042 #else /* XNU_KERNEL_PRIVATE */
1043 #define REQUESTED_TRAILER_SIZE(y) REQUESTED_TRAILER_SIZE_NATIVE(y)
1044 #endif /* XNU_KERNEL_PRIVATE */
1045 
1046 /*
1047  *  Much code assumes that mach_msg_return_t == kern_return_t.
1048  *  This definition is useful for descriptive purposes.
1049  *
1050  *  See <mach/error.h> for the format of error codes.
1051  *  IPC errors are system 4.  Send errors are subsystem 0;
1052  *  receive errors are subsystem 1.  The code field is always non-zero.
1053  *  The high bits of the code field communicate extra information
1054  *  for some error codes.  MACH_MSG_MASK masks off these special bits.
1055  */
1056 
1057 typedef kern_return_t mach_msg_return_t;
1058 
1059 #define MACH_MSG_SUCCESS                0x00000000
1060 
1061 
1062 #define MACH_MSG_MASK                   0x00003e00
1063 /* All special error code bits defined below. */
1064 #define MACH_MSG_IPC_SPACE              0x00002000
1065 /* No room in IPC name space for another capability name. */
1066 #define MACH_MSG_VM_SPACE               0x00001000
1067 /* No room in VM address space for out-of-line memory. */
1068 #define MACH_MSG_IPC_KERNEL             0x00000800
1069 /* Kernel resource shortage handling an IPC capability. */
1070 #define MACH_MSG_VM_KERNEL              0x00000400
1071 /* Kernel resource shortage handling out-of-line memory. */
1072 
1073 #define MACH_SEND_IN_PROGRESS           0x10000001
1074 /* Thread is waiting to send.  (Internal use only.) */
1075 #define MACH_SEND_INVALID_DATA          0x10000002
1076 /* Bogus in-line data. */
1077 #define MACH_SEND_INVALID_DEST          0x10000003
1078 /* Bogus destination port. */
1079 #define MACH_SEND_TIMED_OUT             0x10000004
1080 /* Message not sent before timeout expired. */
1081 #define MACH_SEND_INVALID_VOUCHER       0x10000005
1082 /* Bogus voucher port. */
1083 #define MACH_SEND_INTERRUPTED           0x10000007
1084 /* Software interrupt. */
1085 #define MACH_SEND_MSG_TOO_SMALL         0x10000008
1086 /* Data doesn't contain a complete message. */
1087 #define MACH_SEND_INVALID_REPLY         0x10000009
1088 /* Bogus reply port. */
1089 #define MACH_SEND_INVALID_RIGHT         0x1000000a
1090 /* Bogus port rights in the message body. */
1091 #define MACH_SEND_INVALID_NOTIFY        0x1000000b
1092 /* Bogus notify port argument. */
1093 #define MACH_SEND_INVALID_MEMORY        0x1000000c
1094 /* Invalid out-of-line memory pointer. */
1095 #define MACH_SEND_NO_BUFFER             0x1000000d
1096 /* No message buffer is available. */
1097 #define MACH_SEND_TOO_LARGE             0x1000000e
1098 /* Send is too large for port */
1099 #define MACH_SEND_INVALID_TYPE          0x1000000f
1100 /* Invalid msg-type specification. */
1101 #define MACH_SEND_INVALID_HEADER        0x10000010
1102 /* A field in the header had a bad value. */
1103 #define MACH_SEND_INVALID_TRAILER       0x10000011
1104 /* The trailer to be sent does not match kernel format. */
1105 #define MACH_SEND_INVALID_CONTEXT       0x10000012
1106 /* The sending thread context did not match the context on the dest port */
1107 #define MACH_SEND_INVALID_RT_OOL_SIZE   0x10000015
1108 /* compatibility: no longer a returned error */
1109 #define MACH_SEND_NO_GRANT_DEST         0x10000016
1110 /* The destination port doesn't accept ports in body */
1111 #define MACH_SEND_MSG_FILTERED          0x10000017
1112 /* Message send was rejected by message filter */
1113 
1114 #define MACH_RCV_IN_PROGRESS            0x10004001
1115 /* Thread is waiting for receive.  (Internal use only.) */
1116 #define MACH_RCV_INVALID_NAME           0x10004002
1117 /* Bogus name for receive port/port-set. */
1118 #define MACH_RCV_TIMED_OUT              0x10004003
1119 /* Didn't get a message within the timeout value. */
1120 #define MACH_RCV_TOO_LARGE              0x10004004
1121 /* Message buffer is not large enough for inline data. */
1122 #define MACH_RCV_INTERRUPTED            0x10004005
1123 /* Software interrupt. */
1124 #define MACH_RCV_PORT_CHANGED           0x10004006
1125 /* compatibility: no longer a returned error */
1126 #define MACH_RCV_INVALID_NOTIFY         0x10004007
1127 /* Bogus notify port argument. */
1128 #define MACH_RCV_INVALID_DATA           0x10004008
1129 /* Bogus message buffer for inline data. */
1130 #define MACH_RCV_PORT_DIED              0x10004009
1131 /* Port/set was sent away/died during receive. */
1132 #define MACH_RCV_IN_SET                 0x1000400a
1133 /* compatibility: no longer a returned error */
1134 #define MACH_RCV_HEADER_ERROR           0x1000400b
1135 /* Error receiving message header.  See special bits. */
1136 #define MACH_RCV_BODY_ERROR             0x1000400c
1137 /* Error receiving message body.  See special bits. */
1138 #define MACH_RCV_INVALID_TYPE           0x1000400d
1139 /* Invalid msg-type specification in scatter list. */
1140 #define MACH_RCV_SCATTER_SMALL          0x1000400e
1141 /* Out-of-line overwrite region is not large enough */
1142 #define MACH_RCV_INVALID_TRAILER        0x1000400f
1143 /* trailer type or number of trailer elements not supported */
1144 #define MACH_RCV_IN_PROGRESS_TIMED      0x10004011
1145 /* Waiting for receive with timeout. (Internal use only.) */
1146 #define MACH_RCV_INVALID_REPLY          0x10004012
1147 /* invalid reply port used in a STRICT_REPLY message */
1148 
1149 #ifdef XNU_KERNEL_PRIVATE
1150 #define MACH_PEEK_IN_PROGRESS           0x10008001
1151 /* Waiting for a peek. (Internal use only.) */
1152 #define MACH_PEEK_READY                 0x10008002
1153 /* Waiting for a peek. (Internal use only.) */
1154 #endif
1155 
1156 
1157 __BEGIN_DECLS
1158 
1159 /*
1160  *	Routine:	mach_msg_overwrite
1161  *	Purpose:
1162  *		Send and/or receive a message.  If the message operation
1163  *		is interrupted, and the user did not request an indication
1164  *		of that fact, then restart the appropriate parts of the
1165  *		operation silently (trap version does not restart).
1166  *
1167  *		Distinct send and receive buffers may be specified.  If
1168  *		no separate receive buffer is specified, the msg parameter
1169  *		will be used for both send and receive operations.
1170  *
1171  *		In addition to a distinct receive buffer, that buffer may
1172  *		already contain scatter control information to direct the
1173  *		receiving of the message.
1174  */
1175 __WATCHOS_PROHIBITED __TVOS_PROHIBITED
1176 extern mach_msg_return_t        mach_msg_overwrite(
1177 	mach_msg_header_t *msg,
1178 	mach_msg_option_t option,
1179 	mach_msg_size_t send_size,
1180 	mach_msg_size_t rcv_size,
1181 	mach_port_name_t rcv_name,
1182 	mach_msg_timeout_t timeout,
1183 	mach_port_name_t notify,
1184 	mach_msg_header_t *rcv_msg,
1185 	mach_msg_size_t rcv_limit);
1186 
1187 #ifndef KERNEL
1188 
1189 /*
1190  *	Routine:	mach_msg
1191  *	Purpose:
1192  *		Send and/or receive a message.  If the message operation
1193  *		is interrupted, and the user did not request an indication
1194  *		of that fact, then restart the appropriate parts of the
1195  *		operation silently (trap version does not restart).
1196  */
1197 __WATCHOS_PROHIBITED __TVOS_PROHIBITED
1198 extern mach_msg_return_t        mach_msg(
1199 	mach_msg_header_t *msg,
1200 	mach_msg_option_t option,
1201 	mach_msg_size_t send_size,
1202 	mach_msg_size_t rcv_size,
1203 	mach_port_name_t rcv_name,
1204 	mach_msg_timeout_t timeout,
1205 	mach_port_name_t notify);
1206 
1207 /*
1208  *	Routine:	mach_voucher_deallocate
1209  *	Purpose:
1210  *		Deallocate a mach voucher created or received in a message.  Drops
1211  *		one (send right) reference to the voucher.
1212  */
1213 __WATCHOS_PROHIBITED __TVOS_PROHIBITED
1214 extern kern_return_t            mach_voucher_deallocate(
1215 	mach_port_name_t voucher);
1216 
1217 #elif defined(MACH_KERNEL_PRIVATE)
1218 
1219 extern mach_msg_return_t        mach_msg_receive_results(mach_msg_size_t *size);
1220 
1221 #endif  /* KERNEL */
1222 
1223 __END_DECLS
1224 
1225 #endif  /* _MACH_MESSAGE_H_ */
1226