1 /*
2 * Copyright (c) 2000-2016 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 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: ipc/ipc_port.h
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Definitions for ports.
70 */
71
72 #ifndef _IPC_IPC_PORT_H_
73 #define _IPC_IPC_PORT_H_
74
75 #ifdef MACH_KERNEL_PRIVATE
76
77 #include <mach_assert.h>
78 #include <mach_debug.h>
79
80 #include <mach/mach_types.h>
81 #include <mach/boolean.h>
82 #include <mach/kern_return.h>
83 #include <mach/port.h>
84
85 #include <kern/assert.h>
86 #include <kern/kern_types.h>
87 #include <kern/turnstile.h>
88
89 #include <ipc/ipc_types.h>
90 #include <ipc/ipc_object.h>
91 #include <ipc/ipc_mqueue.h>
92 #include <ipc/ipc_space.h>
93
94 #include <security/_label.h>
95
96 #include <ptrauth.h>
97
98 struct task_watchport_elem;
99
100 typedef unsigned int ipc_port_timestamp_t;
101
102 struct ipc_port {
103 struct ipc_object ip_object;
104 union {
105 /*
106 * The waitq_eventmask field is only used on the global queues.
107 * We hence repurpose all those bits for our own use.
108 *
109 * Note: if too many bits are added, compilation will fail
110 * with errors about "negative bitfield sizes"
111 */
112 WAITQ_FLAGS(ip_waitq
113 , ip_fullwaiters:1 /* Whether there are senders blocked on a full queue */
114 , ip_sprequests:1 /* send-possible requests outstanding */
115 , ip_spimportant:1 /* ... at least one is importance donating */
116 , ip_impdonation:1 /* port supports importance donation */
117 , ip_tempowner:1 /* dont give donations to current receiver */
118 , ip_guarded:1 /* port guarded (use context value as guard) */
119 , ip_strict_guard:1 /* Strict guarding; Prevents user manipulation of context values directly */
120 , ip_specialreply:1 /* port is a special reply port */
121 , ip_sync_link_state:3 /* link the port to destination port/ Workloop */
122 , ip_sync_bootstrap_checkin:1 /* port part of sync bootstrap checkin, push on thread doing the checkin */
123 , ip_immovable_receive:1 /* the receive right cannot be moved out of a space, until it is destroyed */
124 , ip_no_grant:1 /* Port wont accept complex messages containing (ool) port descriptors */
125 , ip_immovable_send:1 /* No send(once) rights to this port can be moved out of a space, never unset */
126 , ip_tg_block_tracking:1 /* Track blocking relationship between thread groups during sync IPC */
127 , ip_pinned:1 /* Can't deallocate the last send right from a space while the bit is set */
128 , ip_service_port:1 /* port is a service port */
129 , ip_kobject_nsrequest:1 /* kobject no-senders armed, naked port-ref only */
130 #if DEVELOPMENT || DEBUG
131 , ip_srp_lost_link:1 /* special reply port turnstile link chain broken */
132 , ip_srp_msg_sent:1 /* special reply port msg sent */
133 #endif
134 );
135 struct waitq ip_waitq;
136 };
137
138 struct ipc_mqueue ip_messages;
139
140 /*
141 * IMPORTANT: Direct access of unionized fields are highly discouraged.
142 * Use accessor functions below and see header doc for possible states.
143 */
144 union {
145 struct ipc_space *XNU_PTRAUTH_SIGNED_PTR("ipc_port.ip_receiver") ip_receiver;
146 struct ipc_port *XNU_PTRAUTH_SIGNED_PTR("ipc_port.ip_destination") ip_destination;
147 ipc_port_timestamp_t ip_timestamp;
148 };
149
150 /* update ipc_kobject_upgrade() if this union is changed */
151 union {
152 uintptr_t ip_kobject; /* manually PAC-ed, see ipc_kobject_get_raw() */
153 ipc_importance_task_t ip_imp_task; /* use accessor ip_get_imp_task() */
154 struct ipc_port *ip_sync_inheritor_port;
155 struct knote *ip_sync_inheritor_knote;
156 struct turnstile *ip_sync_inheritor_ts;
157 };
158
159 struct ipc_port *ip_nsrequest;
160 struct ipc_port *ip_pdrequest;
161 struct ipc_port_request *ip_requests;
162 union {
163 struct ipc_kmsg *XNU_PTRAUTH_SIGNED_PTR("ipc_port.premsg") ip_premsg;
164 struct turnstile *ip_send_turnstile;
165 };
166 mach_vm_address_t ip_context;
167
168 natural_t ip_impcount; /* number of importance donations in nested queue */
169
170 mach_port_mscount_t ip_mscount;
171 mach_port_rights_t ip_srights;
172 mach_port_rights_t ip_sorights;
173
174 union {
175 ipc_kobject_label_t XNU_PTRAUTH_SIGNED_PTR("ipc_port.kolabel") ip_kolabel;
176 /* Union of service and connection ports' message filtering metadata */
177 void * XNU_PTRAUTH_SIGNED_PTR("ipc_port.ip_splabel") ip_splabel;
178 };
179
180 #if MACH_ASSERT
181 #define IP_NSPARES 4
182 #define IP_CALLSTACK_MAX 16
183 thread_t ip_thread; /* who made me? thread context */
184 unsigned long ip_timetrack; /* give an idea of "when" created */
185 uintptr_t ip_callstack[IP_CALLSTACK_MAX]; /* stack trace */
186 unsigned long ip_spares[IP_NSPARES]; /* for debugging */
187 #endif /* MACH_ASSERT */
188 };
189
190 #define ip_preposts ip_waitq.waitq_prepost_id
191
192 static inline bool
ip_in_pset(ipc_port_t port)193 ip_in_pset(ipc_port_t port)
194 {
195 return port->ip_waitq.waitq_set_id.wqr_value != 0;
196 }
197
198 #define ip_receiver_name ip_messages.imq_receiver_name
199 #define ip_reply_context ip_messages.imq_context
200 #define ip_klist ip_messages.imq_klist
201
202 #define port_send_turnstile(port) \
203 (IP_PREALLOC(port) ? (port)->ip_premsg->ikm_turnstile : (port)->ip_send_turnstile)
204
205 #define set_port_send_turnstile(port, value) \
206 MACRO_BEGIN \
207 if (IP_PREALLOC(port)) { \
208 (port)->ip_premsg->ikm_turnstile = (value); \
209 } else { \
210 (port)->ip_send_turnstile = (value); \
211 } \
212 MACRO_END
213
214 #define port_send_turnstile_address(port) \
215 (IP_PREALLOC(port) ? &((port)->ip_premsg->ikm_turnstile) : &((port)->ip_send_turnstile))
216
217 #define port_rcv_turnstile_address(port) (&(port)->ip_waitq.waitq_ts)
218
219
220 /*
221 * SYNC IPC state flags for special reply port/ rcv right.
222 *
223 * PORT_SYNC_LINK_ANY
224 * Special reply port is not linked to any other port
225 * or WL and linkage should be allowed.
226 *
227 * PORT_SYNC_LINK_PORT
228 * Special reply port is linked to the port and
229 * ip_sync_inheritor_port contains the inheritor
230 * port.
231 *
232 * PORT_SYNC_LINK_WORKLOOP_KNOTE
233 * Special reply port is linked to a WL (via a knote).
234 * ip_sync_inheritor_knote contains a pointer to the knote
235 * the port is stashed on.
236 *
237 * PORT_SYNC_LINK_WORKLOOP_STASH
238 * Special reply port is linked to a WL (via a knote stash).
239 * ip_sync_inheritor_ts contains a pointer to the turnstile with a +1
240 * the port is stashed on.
241 *
242 * PORT_SYNC_LINK_NO_LINKAGE
243 * Message sent to special reply port, do
244 * not allow any linkages till receive is
245 * complete.
246 *
247 * PORT_SYNC_LINK_RCV_THREAD
248 * Receive right copied out as a part of bootstrap check in,
249 * push on the thread which copied out the port.
250 */
251 #define PORT_SYNC_LINK_ANY (0)
252 #define PORT_SYNC_LINK_PORT (0x1)
253 #define PORT_SYNC_LINK_WORKLOOP_KNOTE (0x2)
254 #define PORT_SYNC_LINK_WORKLOOP_STASH (0x3)
255 #define PORT_SYNC_LINK_NO_LINKAGE (0x4)
256 #define PORT_SYNC_LINK_RCV_THREAD (0x5)
257
258 #define IP_NULL IPC_PORT_NULL
259 #define IP_DEAD IPC_PORT_DEAD
260 #define IP_VALID(port) IPC_PORT_VALID(port)
261
262 #define ip_object_to_port(io) __container_of(io, struct ipc_port, ip_object)
263 #define ip_to_object(port) (&(port)->ip_object)
264 #define ip_active(port) io_active(ip_to_object(port))
265 #define ip_mq_lock_held(port) io_lock_held(ip_to_object(port))
266 #define ip_mq_lock(port) io_lock(ip_to_object(port))
267 #define ip_mq_lock_try(port) io_lock_try(ip_to_object(port))
268 #define ip_mq_lock_held_kdp(port) io_lock_held_kdp(ip_to_object(port))
269 #define ip_mq_unlock(port) io_unlock(ip_to_object(port))
270
271 #define ip_reference(port) io_reference(ip_to_object(port))
272 #define ip_release(port) io_release(ip_to_object(port))
273 #define ip_release_safe(port) io_release_safe(ip_to_object(port))
274 #define ip_release_live(port) io_release_live(ip_to_object(port))
275
276 #define ip_from_waitq(wq) __container_of(wq, struct ipc_port, ip_waitq)
277 #define ip_from_mq(mq) __container_of(mq, struct ipc_port, ip_messages)
278
279 #define ip_kotype(port) io_kotype(ip_to_object(port))
280 #define ip_is_kobject(port) io_is_kobject(ip_to_object(port))
281 #define ip_is_control(port) \
282 (ip_is_kobject(port) && (ip_kotype(port) == IKOT_TASK_CONTROL || ip_kotype(port) == IKOT_THREAD_CONTROL))
283 #define ip_is_kolabeled(port) io_is_kolabeled(ip_to_object(port))
284
285 #define ip_full_kernel(port) imq_full_kernel(&(port)->ip_messages)
286 #define ip_full(port) imq_full(&(port)->ip_messages)
287
288 #define ip_is_immovable_send(port) ((port)->ip_immovable_send)
289 #define ip_is_pinned(port) ((port)->ip_pinned)
290
291 /* Bits reserved in IO_BITS_PORT_INFO are defined here */
292
293 /*
294 * JMM - Preallocation flag
295 * This flag indicates that there is a message buffer preallocated for this
296 * port and we should use that when sending (from the kernel) rather than
297 * allocate a new one. This avoids deadlocks during notification message
298 * sends by critical system threads (which may be needed to free memory and
299 * therefore cannot be blocked waiting for memory themselves).
300 */
301 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
302 #define IP_PREALLOC(port) (io_bits(ip_to_object(port)) & IP_BIT_PREALLOC)
303
304 #define IP_SET_PREALLOC(port, kmsg) \
305 MACRO_BEGIN \
306 io_bits_or(ip_to_object(port), IP_BIT_PREALLOC); \
307 (port)->ip_premsg = (kmsg); \
308 MACRO_END
309
310 /*
311 * This flag indicates that the port has opted into message filtering based
312 * on a policy defined in the Sandbox.
313 */
314 #define IP_BIT_FILTER_MSG 0x00001000
315 #define ip_enforce_msg_filtering(port) ((io_bits(ip_to_object(port)) & IP_BIT_FILTER_MSG) != 0)
316
317 /* JMM - address alignment/packing for LP64 */
318 struct ipc_port_request {
319 union {
320 struct ipc_port *port;
321 ipc_port_request_index_t index;
322 } notify;
323
324 union {
325 mach_port_name_t name;
326 struct ipc_table_size *size;
327 } name;
328 };
329
330 #define ipr_next notify.index
331 #define ipr_size name.size
332
333 #define ipr_soright notify.port
334 #define ipr_name name.name
335
336 /*
337 * Use the low bits in the ipr_soright to specify the request type
338 */
339 #define IPR_SOR_SPARM_MASK 1 /* send-possible armed */
340 #define IPR_SOR_SPREQ_MASK 2 /* send-possible requested */
341 #define IPR_SOR_SPBIT_MASK 3 /* combo */
342 #define IPR_SOR_SPARMED(sor) (((uintptr_t)(sor) & IPR_SOR_SPARM_MASK) != 0)
343 #define IPR_SOR_SPREQ(sor) (((uintptr_t)(sor) & IPR_SOR_SPREQ_MASK) != 0)
344 #define IPR_SOR_PORT(sor) ((ipc_port_t)((uintptr_t)(sor) & ~IPR_SOR_SPBIT_MASK))
345 #define IPR_SOR_MAKE(p, m) ((ipc_port_t)((uintptr_t)(p) | (m)))
346
347 extern lck_grp_t ipc_lck_grp;
348 extern lck_attr_t ipc_lck_attr;
349
350 /*
351 * Taking the ipc_port_multiple lock grants the privilege
352 * to lock multiple ports at once. No ports must locked
353 * when it is taken.
354 */
355
356 extern lck_spin_t ipc_port_multiple_lock_data;
357
358 #define ipc_port_multiple_lock() \
359 lck_spin_lock_grp(&ipc_port_multiple_lock_data, &ipc_lck_grp)
360
361 #define ipc_port_multiple_unlock() \
362 lck_spin_unlock(&ipc_port_multiple_lock_data)
363
364 /*
365 * Search for the end of the chain (a port not in transit),
366 * acquiring locks along the way.
367 */
368 extern boolean_t ipc_port_destination_chain_lock(
369 ipc_port_t port,
370 ipc_port_t *base);
371
372 /*
373 * The port timestamp facility provides timestamps
374 * for port destruction. It is used to serialize
375 * mach_port_names with port death.
376 */
377
378 extern ipc_port_timestamp_t ipc_port_timestamp_data;
379
380 /* Retrieve a port timestamp value */
381 extern ipc_port_timestamp_t ipc_port_timestamp(void);
382
383 /*
384 * Compares two timestamps, and returns TRUE if one
385 * happened before two. Note that this formulation
386 * works when the timestamp wraps around at 2^32,
387 * as long as one and two aren't too far apart.
388 */
389
390 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
391
392 extern void __abortlike __ipc_port_inactive_panic(ipc_port_t port);
393
394 static inline void
require_ip_active(ipc_port_t port)395 require_ip_active(ipc_port_t port)
396 {
397 if (!ip_active(port)) {
398 __ipc_port_inactive_panic(port);
399 }
400 }
401
402 /*
403 * A receive right (port) can be in ONE of the following four states:
404 *
405 * 1) INACTIVE: Dead
406 * 2) IN-SPACE: In a space
407 * 3) IN-TRANSIT: Enqueued in a message
408 * 4) IN-LIMBO
409 *
410 * If the port is active and ip_receiver_name != MACH_PORT_NULL, we can safely
411 * deference the union as ip_receiver, which points to the space that holds
412 * receive right (but doesn't hold a ref for it).
413 *
414 * If the port is active and ip_receiver_name == MACH_PORT_NULL, we can safely
415 * deference the union as ip_destination. The port is either IN-LIMBO (ip_destination == IP_NULL)
416 * or ip_destination points to the destination port and holds a ref for it.
417 *
418 * If the port is not active, we can safely deference the union as ip_timestamp,
419 * which contains a timestamp taken when the port was destroyed.
420 *
421 * If the port is in a space, ip_receiver_name denotes the port name its receive
422 * right occupies in the receiving space. The only exception, as an optimization trick,
423 * is task's self port (itk_self), whose ip_receiver_name actually denotes the name
424 * of mach_task_self() in owning task's space (a send right, with receive right in ipc_space_kernel).
425 */
426
427 static inline bool
ip_in_a_space(ipc_port_t port)428 ip_in_a_space(ipc_port_t port)
429 {
430 /* IN-SPACE */
431 return ip_active(port) && port->ip_receiver_name != MACH_PORT_NULL;
432 }
433
434 static inline bool
ip_in_space(ipc_port_t port,ipc_space_t space)435 ip_in_space(ipc_port_t port, ipc_space_t space)
436 {
437 ip_mq_lock_held(port); /* port must be locked, otherwise PAC could fail */
438 return ip_in_a_space(port) && port->ip_receiver == space;
439 }
440
441 /* use sparsely when port lock is not possible, just compare raw pointer */
442 static inline bool
ip_in_space_noauth(ipc_port_t port,void * space)443 ip_in_space_noauth(ipc_port_t port, void* space)
444 {
445 void *raw_ptr = ptrauth_strip(*(void **)&port->ip_receiver, ptrauth_key_process_independent_data);
446 return raw_ptr == space;
447 }
448
449 static inline bool
ip_in_transit(ipc_port_t port)450 ip_in_transit(ipc_port_t port)
451 {
452 /* IN-TRANSIT */
453 ip_mq_lock_held(port); /* port must be locked, otherwise PAC could fail */
454 return ip_active(port) && !ip_in_a_space(port) && port->ip_destination != IP_NULL;
455 }
456
457 static inline bool
ip_in_limbo(ipc_port_t port)458 ip_in_limbo(ipc_port_t port)
459 {
460 /* IN-LIMBO */
461 ip_mq_lock_held(port); /* port must be locked, otherwise PAC could fail */
462 return ip_active(port) && !ip_in_a_space(port) && port->ip_destination == IP_NULL;
463 }
464
465 static inline ipc_space_t
ip_get_receiver(ipc_port_t port)466 ip_get_receiver(ipc_port_t port)
467 {
468 ip_mq_lock_held(port); /* port must be locked, otherwise PAC could fail */
469 return ip_in_a_space(port) ? port->ip_receiver : NULL;
470 }
471
472 static inline void*
ip_get_receiver_ptr_noauth(ipc_port_t port)473 ip_get_receiver_ptr_noauth(ipc_port_t port)
474 {
475 void *raw_ptr = ptrauth_strip(*(void **)&port->ip_receiver, ptrauth_key_process_independent_data);
476 return raw_ptr;
477 }
478
479 static inline mach_port_name_t
ip_get_receiver_name(ipc_port_t port)480 ip_get_receiver_name(ipc_port_t port)
481 {
482 return ip_in_a_space(port) ? port->ip_receiver_name : MACH_PORT_NULL;
483 }
484
485 static inline ipc_port_t
ip_get_destination(ipc_port_t port)486 ip_get_destination(ipc_port_t port)
487 {
488 ip_mq_lock_held(port); /* port must be locked, otherwise PAC could fail */
489 return ip_active(port) && !ip_in_a_space(port) ? port->ip_destination : IP_NULL;
490 }
491
492 static inline ipc_port_timestamp_t
ip_get_death_time(ipc_port_t port)493 ip_get_death_time(ipc_port_t port)
494 {
495 assert(!ip_active(port));
496 return port->ip_timestamp;
497 }
498
499 static inline ipc_importance_task_t
ip_get_imp_task(ipc_port_t port)500 ip_get_imp_task(ipc_port_t port)
501 {
502 return (!ip_is_kobject(port) && !port->ip_specialreply && port->ip_tempowner) ? port->ip_imp_task : IIT_NULL;
503 }
504
505 extern kern_return_t ipc_port_translate_send(
506 ipc_space_t space,
507 mach_port_name_t name,
508 ipc_port_t *portp);
509
510 extern kern_return_t ipc_port_translate_receive(
511 ipc_space_t space,
512 mach_port_name_t name,
513 ipc_port_t *portp);
514
515 /* Allocate a notification request slot */
516 #if IMPORTANCE_INHERITANCE
517 extern kern_return_t ipc_port_request_alloc(
518 ipc_port_t port,
519 mach_port_name_t name,
520 ipc_port_t soright,
521 boolean_t send_possible,
522 boolean_t immediate,
523 ipc_port_request_index_t *indexp,
524 boolean_t *importantp);
525 #else
526 extern kern_return_t ipc_port_request_alloc(
527 ipc_port_t port,
528 mach_port_name_t name,
529 ipc_port_t soright,
530 boolean_t send_possible,
531 boolean_t immediate,
532 ipc_port_request_index_t *indexp);
533 #endif /* IMPORTANCE_INHERITANCE */
534
535 /* Grow one of a port's tables of notifcation requests */
536 extern kern_return_t ipc_port_request_grow(
537 ipc_port_t port,
538 ipc_table_elems_t target_size);
539
540 /* Return the type(s) of notification requests outstanding */
541 extern mach_port_type_t ipc_port_request_type(
542 ipc_port_t port,
543 mach_port_name_t name,
544 ipc_port_request_index_t index);
545
546 /* Cancel a notification request and return the send-once right */
547 extern ipc_port_t ipc_port_request_cancel(
548 ipc_port_t port,
549 mach_port_name_t name,
550 ipc_port_request_index_t index);
551
552 /* Arm any delayed send-possible notification */
553 extern boolean_t ipc_port_request_sparm(
554 ipc_port_t port,
555 mach_port_name_t name,
556 ipc_port_request_index_t index,
557 mach_msg_option_t option,
558 mach_msg_priority_t priority);
559
560 /* Make a port-deleted request */
561 extern void ipc_port_pdrequest(
562 ipc_port_t port,
563 ipc_port_t notify,
564 ipc_port_t *previousp);
565
566 /* Make a no-senders request */
567 extern void ipc_port_nsrequest(
568 ipc_port_t port,
569 mach_port_mscount_t sync,
570 ipc_port_t notify,
571 ipc_port_t *previousp);
572
573 /* Prepare a receive right for transmission/destruction */
574 extern boolean_t ipc_port_clear_receiver(
575 ipc_port_t port,
576 boolean_t should_destroy);
577
578 __options_decl(ipc_port_init_flags_t, uint32_t, {
579 IPC_PORT_INIT_NONE = 0x00000000,
580 IPC_PORT_INIT_MAKE_SEND_RIGHT = 0x00000001,
581 IPC_PORT_INIT_MESSAGE_QUEUE = 0x00000002,
582 IPC_PORT_INIT_SPECIAL_REPLY = 0x00000004,
583 IPC_PORT_INIT_FILTER_MESSAGE = 0x00000008,
584 IPC_PORT_INIT_TG_BLOCK_TRACKING = 0x00000010,
585 IPC_PORT_INIT_LOCKED = 0x00000020,
586 });
587
588 /* Initialize a newly-allocated port */
589 extern void ipc_port_init(
590 ipc_port_t port,
591 ipc_space_t space,
592 ipc_port_init_flags_t flags,
593 mach_port_name_t name);
594
595 /* Allocate a port */
596 extern kern_return_t ipc_port_alloc(
597 ipc_space_t space,
598 ipc_port_init_flags_t flags,
599 mach_port_name_t *namep,
600 ipc_port_t *portp);
601
602 /* Allocate a port, with a specific name */
603 extern kern_return_t ipc_port_alloc_name(
604 ipc_space_t space,
605 ipc_port_init_flags_t flags,
606 mach_port_name_t name,
607 ipc_port_t *portp);
608
609 /* Attach a label to the port */
610 extern void ipc_port_set_label(
611 ipc_port_t port,
612 ipc_label_t label);
613
614 /* Generate dead name notifications */
615 extern void ipc_port_dnnotify(
616 ipc_port_t port);
617
618 /* Generate send-possible notifications */
619 extern void ipc_port_spnotify(
620 ipc_port_t port);
621
622 /* Destroy a port */
623 extern void ipc_port_destroy(
624 ipc_port_t port);
625
626 /* Check if queueing "port" in a message for "dest" would create a circular
627 * group of ports and messages */
628 extern boolean_t
629 ipc_port_check_circularity(
630 ipc_port_t port,
631 ipc_port_t dest);
632
633 #if IMPORTANCE_INHERITANCE
634
635 enum {
636 IPID_OPTION_NORMAL = 0, /* normal boost */
637 IPID_OPTION_SENDPOSSIBLE = 1, /* send-possible induced boost */
638 };
639
640 /* link the destination port with special reply port */
641 void
642 ipc_port_link_special_reply_port(
643 ipc_port_t special_reply_port,
644 ipc_port_t dest_port,
645 boolean_t sync_bootstrap_checkin);
646
647 #define IPC_PORT_ADJUST_SR_NONE 0
648 #define IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE 0x1
649 #define IPC_PORT_ADJUST_SR_LINK_WORKLOOP 0x2
650 #define IPC_PORT_ADJUST_UNLINK_THREAD 0x4
651 #define IPC_PORT_ADJUST_SR_RECEIVED_MSG 0x8
652 #define IPC_PORT_ADJUST_SR_ENABLE_EVENT 0x10
653 #define IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN 0x20
654
655 void
656 ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port);
657
658 void
659 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port);
660
661 void
662 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port);
663
664 /* Adjust special reply port linkage */
665 void
666 ipc_port_adjust_special_reply_port_locked(
667 ipc_port_t special_reply_port,
668 struct knote *kn,
669 uint8_t flags,
670 boolean_t get_turnstile);
671
672 void
673 ipc_port_adjust_sync_link_state_locked(
674 ipc_port_t port,
675 int sync_link_state,
676 turnstile_inheritor_t inheritor);
677
678 /* Adjust special reply port linkage */
679 void
680 ipc_port_adjust_special_reply_port(
681 ipc_port_t special_reply_port,
682 uint8_t flags);
683
684 void
685 ipc_port_adjust_port_locked(
686 ipc_port_t port,
687 struct knote *kn,
688 boolean_t sync_bootstrap_checkin);
689
690 void
691 ipc_port_clear_sync_rcv_thread_boost_locked(
692 ipc_port_t port);
693
694 kern_return_t
695 ipc_port_add_watchport_elem_locked(
696 ipc_port_t port,
697 struct task_watchport_elem *watchport_elem,
698 struct task_watchport_elem **old_elem);
699
700 kern_return_t
701 ipc_port_clear_watchport_elem_internal_conditional_locked(
702 ipc_port_t port,
703 struct task_watchport_elem *watchport_elem);
704
705 kern_return_t
706 ipc_port_replace_watchport_elem_conditional_locked(
707 ipc_port_t port,
708 struct task_watchport_elem *old_watchport_elem,
709 struct task_watchport_elem *new_watchport_elem);
710
711 struct task_watchport_elem *
712 ipc_port_clear_watchport_elem_internal(
713 ipc_port_t port);
714
715 void
716 ipc_port_send_turnstile_prepare(ipc_port_t port);
717
718 void
719 ipc_port_send_turnstile_complete(ipc_port_t port);
720
721 struct waitq *
722 ipc_port_rcv_turnstile_waitq(struct waitq *waitq);
723
724 /* apply importance delta to port only */
725 extern mach_port_delta_t
726 ipc_port_impcount_delta(
727 ipc_port_t port,
728 mach_port_delta_t delta,
729 ipc_port_t base);
730
731 /* apply importance delta to port, and return task importance for update */
732 extern boolean_t
733 ipc_port_importance_delta_internal(
734 ipc_port_t port,
735 natural_t options,
736 mach_port_delta_t *deltap,
737 ipc_importance_task_t *imp_task);
738
739 /* Apply an importance delta to a port and reflect change in receiver task */
740 extern boolean_t
741 ipc_port_importance_delta(
742 ipc_port_t port,
743 natural_t options,
744 mach_port_delta_t delta);
745 #endif /* IMPORTANCE_INHERITANCE */
746
747 /* Make a naked send right from a receive right - port locked and active */
748 extern ipc_port_t ipc_port_make_send_locked(
749 ipc_port_t port);
750
751 /* Make a naked send right from a receive right */
752 extern ipc_port_t ipc_port_make_send(
753 ipc_port_t port) __result_use_check;
754
755 /* Make a naked send right from another naked send right - port locked */
756 extern void ipc_port_copy_send_locked(
757 ipc_port_t port);
758
759 /* Make a naked send right from another naked send right */
760 extern ipc_port_t ipc_port_copy_send(
761 ipc_port_t port) __result_use_check;
762
763 /* Copyout a naked send right */
764 extern mach_port_name_t ipc_port_copyout_send(
765 ipc_port_t sright,
766 ipc_space_t space);
767
768 extern mach_port_name_t ipc_port_copyout_send_pinned(
769 ipc_port_t sright,
770 ipc_space_t space);
771
772 extern void ipc_port_thread_group_blocked(
773 ipc_port_t port);
774
775 extern void ipc_port_thread_group_unblocked(void);
776
777 extern void ipc_port_release_send_and_unlock(
778 ipc_port_t port);
779 #endif /* MACH_KERNEL_PRIVATE */
780
781 #if KERNEL_PRIVATE
782
783 /* Release a (valid) naked send right */
784 extern void ipc_port_release_send(
785 ipc_port_t port);
786
787 extern void ipc_port_reference(
788 ipc_port_t port);
789
790 extern void ipc_port_release(
791 ipc_port_t port);
792
793 #endif /* KERNEL_PRIVATE */
794
795 #ifdef MACH_KERNEL_PRIVATE
796
797 /* Make a naked send-once right from a locked and active receive right */
798 extern ipc_port_t ipc_port_make_sonce_locked(
799 ipc_port_t port);
800
801 /* Make a naked send-once right from a receive right */
802 extern ipc_port_t ipc_port_make_sonce(
803 ipc_port_t port);
804
805 /* Release a naked send-once right */
806 extern void ipc_port_release_sonce(
807 ipc_port_t port);
808
809 /* Release a naked send-once right */
810 extern void ipc_port_release_sonce_and_unlock(
811 ipc_port_t port);
812
813 /* Release a naked (in limbo or in transit) receive right */
814 extern void ipc_port_release_receive(
815 ipc_port_t port);
816
817 /* Finalize the destruction of a port before it gets freed */
818 extern void ipc_port_finalize(
819 ipc_port_t port);
820
821 /* Get receiver task and its pid (if any) for port. */
822 extern pid_t ipc_port_get_receiver_task(ipc_port_t port, uintptr_t *task);
823
824 /* Allocate a port in a special space */
825 extern ipc_port_t ipc_port_alloc_special(
826 ipc_space_t space,
827 ipc_port_init_flags_t flags);
828
829 /* Deallocate a port in a special space */
830 extern void ipc_port_dealloc_special_and_unlock(
831 ipc_port_t port,
832 ipc_space_t space);
833
834 /* Deallocate a port in a special space */
835 extern void ipc_port_dealloc_special(
836 ipc_port_t port,
837 ipc_space_t space);
838
839 extern void ipc_port_recv_update_inheritor(ipc_port_t port,
840 struct turnstile *turnstile,
841 turnstile_update_flags_t flags);
842
843 extern void ipc_port_send_update_inheritor(ipc_port_t port,
844 struct turnstile *turnstile,
845 turnstile_update_flags_t flags);
846
847 extern int
848 ipc_special_reply_get_pid_locked(ipc_port_t port);
849
850 #define ipc_port_alloc_reply() \
851 ipc_port_alloc_special(ipc_space_reply, IPC_PORT_INIT_MESSAGE_QUEUE | IPC_PORT_INIT_SPECIAL_REPLY)
852 #define ipc_port_dealloc_reply(port) \
853 ipc_port_dealloc_special((port), ipc_space_reply)
854
855 #endif /* MACH_KERNEL_PRIVATE */
856
857 #endif /* _IPC_IPC_PORT_H_ */
858