2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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,
65 * File: ipc/ipc_port.c
69 * Functions to manipulate IPC ports.
72 #include <zone_debug.h>
73 #include <mach_assert.h>
75 #include <mach/port.h>
76 #include <mach/kern_return.h>
77 #include <kern/ipc_kobject.h>
78 #include <kern/thread.h>
79 #include <kern/misc_protos.h>
80 #include <kern/waitq.h>
81 #include <kern/policy_internal.h>
82 #include <kern/debug.h>
83 #include <kern/kcdata.h>
84 #include <ipc/ipc_entry.h>
85 #include <ipc/ipc_space.h>
86 #include <ipc/ipc_object.h>
87 #include <ipc/ipc_port.h>
88 #include <ipc/ipc_pset.h>
89 #include <ipc/ipc_kmsg.h>
90 #include <ipc/ipc_mqueue.h>
91 #include <ipc/ipc_notify.h>
92 #include <ipc/ipc_table.h>
93 #include <ipc/ipc_importance.h>
94 #include <machine/limits.h>
95 #include <kern/turnstile.h>
97 #include <security/mac_mach_internal.h>
101 decl_lck_spin_data(, ipc_port_multiple_lock_data
);
102 ipc_port_timestamp_t ipc_port_timestamp_data
;
104 extern int prioritize_launch
;
107 void ipc_port_init_debug(
109 uintptr_t *callstack
,
110 unsigned int callstack_max
);
112 void ipc_port_callstack_init_debug(
113 uintptr_t *callstack
,
114 unsigned int callstack_max
);
116 #endif /* MACH_ASSERT */
119 ipc_port_send_turnstile_recompute_push_locked(
123 ipc_port_get_watchport_inheritor(
127 ipc_port_release(ipc_port_t port
)
133 ipc_port_reference(ipc_port_t port
)
139 * Routine: ipc_port_timestamp
141 * Retrieve a timestamp value.
145 ipc_port_timestamp(void)
147 return OSIncrementAtomic(&ipc_port_timestamp_data
);
151 * Routine: ipc_port_request_alloc
153 * Try to allocate a request slot.
154 * If successful, returns the request index.
155 * Otherwise returns zero.
157 * The port is locked and active.
159 * KERN_SUCCESS A request index was found.
160 * KERN_NO_SPACE No index allocated.
163 #if IMPORTANCE_INHERITANCE
165 ipc_port_request_alloc(
167 mach_port_name_t name
,
169 boolean_t send_possible
,
171 ipc_port_request_index_t
*indexp
,
172 boolean_t
*importantp
)
175 ipc_port_request_alloc(
177 mach_port_name_t name
,
179 boolean_t send_possible
,
181 ipc_port_request_index_t
*indexp
)
182 #endif /* IMPORTANCE_INHERITANCE */
184 ipc_port_request_t ipr
, table
;
185 ipc_port_request_index_t index
;
188 #if IMPORTANCE_INHERITANCE
190 #endif /* IMPORTANCE_INHERITANCE */
192 require_ip_active(port
);
193 assert(name
!= MACH_PORT_NULL
);
194 assert(soright
!= IP_NULL
);
196 table
= port
->ip_requests
;
198 if (table
== IPR_NULL
) {
199 return KERN_NO_SPACE
;
202 index
= table
->ipr_next
;
204 return KERN_NO_SPACE
;
208 assert(ipr
->ipr_name
== MACH_PORT_NULL
);
210 table
->ipr_next
= ipr
->ipr_next
;
211 ipr
->ipr_name
= name
;
214 mask
|= IPR_SOR_SPREQ_MASK
;
216 mask
|= IPR_SOR_SPARM_MASK
;
217 if (port
->ip_sprequests
== 0) {
218 port
->ip_sprequests
= 1;
219 #if IMPORTANCE_INHERITANCE
220 /* TODO: Live importance support in send-possible */
221 if (port
->ip_impdonation
!= 0 &&
222 port
->ip_spimportant
== 0 &&
223 (task_is_importance_donor(current_task()))) {
226 #endif /* IMPORTANCE_INHERTANCE */
230 ipr
->ipr_soright
= IPR_SOR_MAKE(soright
, mask
);
238 * Routine: ipc_port_request_grow
240 * Grow a port's table of requests.
242 * The port must be locked and active.
243 * Nothing else locked; will allocate memory.
244 * Upon return the port is unlocked.
246 * KERN_SUCCESS Grew the table.
247 * KERN_SUCCESS Somebody else grew the table.
248 * KERN_SUCCESS The port died.
249 * KERN_RESOURCE_SHORTAGE Couldn't allocate new table.
250 * KERN_NO_SPACE Couldn't grow to desired size
254 ipc_port_request_grow(
256 ipc_table_elems_t target_size
)
258 ipc_table_size_t its
;
259 ipc_port_request_t otable
, ntable
;
260 require_ip_active(port
);
262 otable
= port
->ip_requests
;
263 if (otable
== IPR_NULL
) {
264 its
= &ipc_table_requests
[0];
266 its
= otable
->ipr_size
+ 1;
269 if (target_size
!= ITS_SIZE_NONE
) {
270 if ((otable
!= IPR_NULL
) &&
271 (target_size
<= otable
->ipr_size
->its_size
)) {
275 while ((its
->its_size
) && (its
->its_size
< target_size
)) {
278 if (its
->its_size
== 0) {
280 return KERN_NO_SPACE
;
287 if ((its
->its_size
== 0) ||
288 ((ntable
= it_requests_alloc(its
)) == IPR_NULL
)) {
290 return KERN_RESOURCE_SHORTAGE
;
296 * Check that port is still active and that nobody else
297 * has slipped in and grown the table on us. Note that
298 * just checking if the current table pointer == otable
299 * isn't sufficient; must check ipr_size.
302 if (ip_active(port
) && (port
->ip_requests
== otable
) &&
303 ((otable
== IPR_NULL
) || (otable
->ipr_size
+ 1 == its
))) {
304 ipc_table_size_t oits
;
305 ipc_table_elems_t osize
, nsize
;
306 ipc_port_request_index_t free
, i
;
308 /* copy old table to new table */
310 if (otable
!= IPR_NULL
) {
311 oits
= otable
->ipr_size
;
312 osize
= oits
->its_size
;
313 free
= otable
->ipr_next
;
315 (void) memcpy((void *)(ntable
+ 1),
316 (const void *)(otable
+ 1),
317 (osize
- 1) * sizeof(struct ipc_port_request
));
324 nsize
= its
->its_size
;
325 assert(nsize
> osize
);
327 /* add new elements to the new table's free list */
329 for (i
= osize
; i
< nsize
; i
++) {
330 ipc_port_request_t ipr
= &ntable
[i
];
332 ipr
->ipr_name
= MACH_PORT_NULL
;
333 ipr
->ipr_next
= free
;
337 ntable
->ipr_next
= free
;
338 ntable
->ipr_size
= its
;
339 port
->ip_requests
= ntable
;
343 if (otable
!= IPR_NULL
) {
344 it_requests_free(oits
, otable
);
349 it_requests_free(its
, ntable
);
356 * Routine: ipc_port_request_sparm
358 * Arm delayed send-possible request.
360 * The port must be locked and active.
362 * Returns TRUE if the request was armed
363 * (or armed with importance in that version).
367 ipc_port_request_sparm(
369 __assert_only mach_port_name_t name
,
370 ipc_port_request_index_t index
,
371 mach_msg_option_t option
,
372 mach_msg_priority_t override
)
374 if (index
!= IE_REQ_NONE
) {
375 ipc_port_request_t ipr
, table
;
377 require_ip_active(port
);
379 table
= port
->ip_requests
;
380 assert(table
!= IPR_NULL
);
383 assert(ipr
->ipr_name
== name
);
385 /* Is there a valid destination? */
386 if (IPR_SOR_SPREQ(ipr
->ipr_soright
)) {
387 ipr
->ipr_soright
= IPR_SOR_MAKE(ipr
->ipr_soright
, IPR_SOR_SPARM_MASK
);
388 port
->ip_sprequests
= 1;
390 if (option
& MACH_SEND_OVERRIDE
) {
391 /* apply override to message queue */
392 ipc_mqueue_override_send(&port
->ip_messages
, override
);
395 #if IMPORTANCE_INHERITANCE
396 if (((option
& MACH_SEND_NOIMPORTANCE
) == 0) &&
397 (port
->ip_impdonation
!= 0) &&
398 (port
->ip_spimportant
== 0) &&
399 (((option
& MACH_SEND_IMPORTANCE
) != 0) ||
400 (task_is_importance_donor(current_task())))) {
405 #endif /* IMPORTANCE_INHERITANCE */
412 * Routine: ipc_port_request_type
414 * Determine the type(s) of port requests enabled for a name.
416 * The port must be locked or inactive (to avoid table growth).
417 * The index must not be IE_REQ_NONE and for the name in question.
420 ipc_port_request_type(
422 __assert_only mach_port_name_t name
,
423 ipc_port_request_index_t index
)
425 ipc_port_request_t ipr
, table
;
426 mach_port_type_t type
= 0;
428 table
= port
->ip_requests
;
429 assert(table
!= IPR_NULL
);
431 assert(index
!= IE_REQ_NONE
);
433 assert(ipr
->ipr_name
== name
);
435 if (IP_VALID(IPR_SOR_PORT(ipr
->ipr_soright
))) {
436 type
|= MACH_PORT_TYPE_DNREQUEST
;
438 if (IPR_SOR_SPREQ(ipr
->ipr_soright
)) {
439 type
|= MACH_PORT_TYPE_SPREQUEST
;
441 if (!IPR_SOR_SPARMED(ipr
->ipr_soright
)) {
442 type
|= MACH_PORT_TYPE_SPREQUEST_DELAYED
;
450 * Routine: ipc_port_request_cancel
452 * Cancel a dead-name/send-possible request and return the send-once right.
454 * The port must be locked and active.
455 * The index must not be IPR_REQ_NONE and must correspond with name.
459 ipc_port_request_cancel(
461 __assert_only mach_port_name_t name
,
462 ipc_port_request_index_t index
)
464 ipc_port_request_t ipr
, table
;
465 ipc_port_t request
= IP_NULL
;
467 require_ip_active(port
);
468 table
= port
->ip_requests
;
469 assert(table
!= IPR_NULL
);
471 assert(index
!= IE_REQ_NONE
);
473 assert(ipr
->ipr_name
== name
);
474 request
= IPR_SOR_PORT(ipr
->ipr_soright
);
476 /* return ipr to the free list inside the table */
477 ipr
->ipr_name
= MACH_PORT_NULL
;
478 ipr
->ipr_next
= table
->ipr_next
;
479 table
->ipr_next
= index
;
485 * Routine: ipc_port_pdrequest
487 * Make a port-deleted request, returning the
488 * previously registered send-once right.
489 * Just cancels the previous request if notify is IP_NULL.
491 * The port is locked and active. It is unlocked.
492 * Consumes a ref for notify (if non-null), and
493 * returns previous with a ref (if non-null).
500 ipc_port_t
*previousp
)
503 require_ip_active(port
);
505 previous
= port
->ip_pdrequest
;
506 port
->ip_pdrequest
= notify
;
509 *previousp
= previous
;
513 * Routine: ipc_port_nsrequest
515 * Make a no-senders request, returning the
516 * previously registered send-once right.
517 * Just cancels the previous request if notify is IP_NULL.
519 * The port is locked and active. It is unlocked.
520 * Consumes a ref for notify (if non-null), and
521 * returns previous with a ref (if non-null).
527 mach_port_mscount_t sync
,
529 ipc_port_t
*previousp
)
532 mach_port_mscount_t mscount
;
533 require_ip_active(port
);
535 previous
= port
->ip_nsrequest
;
536 mscount
= port
->ip_mscount
;
538 if ((port
->ip_srights
== 0) && (sync
<= mscount
) &&
539 (notify
!= IP_NULL
)) {
540 port
->ip_nsrequest
= IP_NULL
;
542 ipc_notify_no_senders(notify
, mscount
);
544 port
->ip_nsrequest
= notify
;
548 *previousp
= previous
;
553 * Routine: ipc_port_clear_receiver
555 * Prepares a receive right for transmission/destruction,
556 * optionally performs mqueue destruction (with port lock held)
559 * The port is locked and active.
561 * If should_destroy is TRUE, then the return value indicates
562 * whether the caller needs to reap kmsg structures that should
563 * be destroyed (by calling ipc_kmsg_reap_delayed)
565 * If should_destroy is FALSE, this always returns FALSE
569 ipc_port_clear_receiver(
571 boolean_t should_destroy
)
573 ipc_mqueue_t mqueue
= &port
->ip_messages
;
574 boolean_t reap_messages
= FALSE
;
577 * Pull ourselves out of any sets to which we belong.
578 * We hold the port locked, so even though this acquires and releases
579 * the mqueue lock, we know we won't be added to any other sets.
581 if (port
->ip_in_pset
!= 0) {
582 ipc_pset_remove_from_all(port
);
583 assert(port
->ip_in_pset
== 0);
587 * Send anyone waiting on the port's queue directly away.
588 * Also clear the mscount, seqno, guard bits
591 if (port
->ip_receiver_name
) {
592 ipc_mqueue_changed(port
->ip_receiver
, mqueue
);
594 ipc_mqueue_changed(NULL
, mqueue
);
596 port
->ip_mscount
= 0;
597 mqueue
->imq_seqno
= 0;
598 port
->ip_context
= port
->ip_guarded
= port
->ip_strict_guard
= 0;
600 * clear the immovable bit so the port can move back to anyone listening
601 * for the port destroy notification
603 port
->ip_immovable_receive
= 0;
605 if (should_destroy
) {
607 * Mark the port and mqueue invalid, preventing further send/receive
608 * operations from succeeding. It's important for this to be
609 * done under the same lock hold as the ipc_mqueue_changed
610 * call to avoid additional threads blocking on an mqueue
611 * that's being destroyed.
613 * The port active bit needs to be guarded under mqueue lock for
616 port
->ip_object
.io_bits
&= ~IO_BITS_ACTIVE
;
617 port
->ip_timestamp
= ipc_port_timestamp();
618 reap_messages
= ipc_mqueue_destroy_locked(mqueue
);
620 /* make port be in limbo */
621 port
->ip_receiver_name
= MACH_PORT_NULL
;
622 port
->ip_destination
= IP_NULL
;
625 imq_unlock(&port
->ip_messages
);
627 return reap_messages
;
631 * Routine: ipc_port_init
633 * Initializes a newly-allocated port.
634 * Doesn't touch the ip_object fields.
641 ipc_port_init_flags_t flags
,
642 mach_port_name_t name
)
644 /* port->ip_kobject doesn't have to be initialized */
646 port
->ip_receiver
= space
;
647 port
->ip_receiver_name
= name
;
649 port
->ip_mscount
= 0;
650 port
->ip_srights
= 0;
651 port
->ip_sorights
= 0;
652 if (flags
& IPC_PORT_INIT_MAKE_SEND_RIGHT
) {
653 port
->ip_srights
= 1;
654 port
->ip_mscount
= 1;
657 port
->ip_nsrequest
= IP_NULL
;
658 port
->ip_pdrequest
= IP_NULL
;
659 port
->ip_requests
= IPR_NULL
;
661 port
->ip_premsg
= IKM_NULL
;
662 port
->ip_context
= 0;
663 port
->ip_reply_context
= 0;
665 port
->ip_sprequests
= 0;
666 port
->ip_spimportant
= 0;
667 port
->ip_impdonation
= 0;
668 port
->ip_tempowner
= 0;
670 port
->ip_guarded
= 0;
671 port
->ip_strict_guard
= 0;
672 port
->ip_immovable_receive
= 0;
673 port
->ip_no_grant
= 0;
674 port
->ip_immovable_send
= 0;
675 port
->ip_impcount
= 0;
677 port
->ip_specialreply
= (flags
& IPC_PORT_INIT_SPECIAL_REPLY
) != 0;
678 port
->ip_sync_link_state
= PORT_SYNC_LINK_ANY
;
679 port
->ip_sync_bootstrap_checkin
= 0;
681 ipc_special_reply_port_bits_reset(port
);
683 port
->ip_send_turnstile
= TURNSTILE_NULL
;
685 ipc_mqueue_kind_t kind
= IPC_MQUEUE_KIND_NONE
;
686 if (flags
& IPC_PORT_INIT_MESSAGE_QUEUE
) {
687 kind
= IPC_MQUEUE_KIND_PORT
;
689 ipc_mqueue_init(&port
->ip_messages
, kind
);
693 * Routine: ipc_port_alloc
697 * Nothing locked. If successful, the port is returned
698 * locked. (The caller doesn't have a reference.)
700 * KERN_SUCCESS The port is allocated.
701 * KERN_INVALID_TASK The space is dead.
702 * KERN_NO_SPACE No room for an entry in the space.
703 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
709 ipc_port_init_flags_t flags
,
710 mach_port_name_t
*namep
,
714 mach_port_name_t name
;
716 mach_port_type_t type
= MACH_PORT_TYPE_RECEIVE
;
717 mach_port_urefs_t urefs
= 0;
720 uintptr_t buf
[IP_CALLSTACK_MAX
];
721 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
722 #endif /* MACH_ASSERT */
724 if (flags
& IPC_PORT_INIT_MAKE_SEND_RIGHT
) {
725 type
|= MACH_PORT_TYPE_SEND
;
728 kr
= ipc_object_alloc(space
, IOT_PORT
, type
, urefs
,
729 &name
, (ipc_object_t
*) &port
);
730 if (kr
!= KERN_SUCCESS
) {
734 /* port and space are locked */
735 ipc_port_init(port
, space
, flags
, name
);
738 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
739 #endif /* MACH_ASSERT */
741 /* unlock space after init */
742 is_write_unlock(space
);
751 * Routine: ipc_port_alloc_name
753 * Allocate a port, with a specific name.
755 * Nothing locked. If successful, the port is returned
756 * locked. (The caller doesn't have a reference.)
758 * KERN_SUCCESS The port is allocated.
759 * KERN_INVALID_TASK The space is dead.
760 * KERN_NAME_EXISTS The name already denotes a right.
761 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
767 ipc_port_init_flags_t flags
,
768 mach_port_name_t name
,
773 mach_port_type_t type
= MACH_PORT_TYPE_RECEIVE
;
774 mach_port_urefs_t urefs
= 0;
777 uintptr_t buf
[IP_CALLSTACK_MAX
];
778 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
779 #endif /* MACH_ASSERT */
781 if (flags
& IPC_PORT_INIT_MAKE_SEND_RIGHT
) {
782 type
|= MACH_PORT_TYPE_SEND
;
785 kr
= ipc_object_alloc_name(space
, IOT_PORT
, type
, urefs
,
786 name
, (ipc_object_t
*) &port
);
787 if (kr
!= KERN_SUCCESS
) {
793 ipc_port_init(port
, space
, flags
, name
);
796 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
797 #endif /* MACH_ASSERT */
805 * Routine: ipc_port_spnotify
807 * Generate send-possible port notifications.
809 * Nothing locked, reference held on port.
815 ipc_port_request_index_t index
= 0;
816 ipc_table_elems_t size
= 0;
819 * If the port has no send-possible request
820 * armed, don't bother to lock the port.
822 if (port
->ip_sprequests
== 0) {
828 #if IMPORTANCE_INHERITANCE
829 if (port
->ip_spimportant
!= 0) {
830 port
->ip_spimportant
= 0;
831 if (ipc_port_importance_delta(port
, IPID_OPTION_NORMAL
, -1) == TRUE
) {
835 #endif /* IMPORTANCE_INHERITANCE */
837 if (port
->ip_sprequests
== 0) {
841 port
->ip_sprequests
= 0;
844 if (ip_active(port
)) {
845 ipc_port_request_t requests
;
847 /* table may change each time port unlocked (reload) */
848 requests
= port
->ip_requests
;
849 assert(requests
!= IPR_NULL
);
852 * no need to go beyond table size when first
853 * we entered - those are future notifications.
856 size
= requests
->ipr_size
->its_size
;
859 /* no need to backtrack either */
860 while (++index
< size
) {
861 ipc_port_request_t ipr
= &requests
[index
];
862 mach_port_name_t name
= ipr
->ipr_name
;
863 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
864 boolean_t armed
= IPR_SOR_SPARMED(ipr
->ipr_soright
);
866 if (MACH_PORT_VALID(name
) && armed
&& IP_VALID(soright
)) {
867 /* claim send-once right - slot still inuse */
868 ipr
->ipr_soright
= IP_NULL
;
871 ipc_notify_send_possible(soright
, name
);
883 * Routine: ipc_port_dnnotify
885 * Generate dead name notifications for
886 * all outstanding dead-name and send-
890 * Port must be inactive.
891 * Reference held on port.
897 ipc_port_request_t requests
= port
->ip_requests
;
899 assert(!ip_active(port
));
900 if (requests
!= IPR_NULL
) {
901 ipc_table_size_t its
= requests
->ipr_size
;
902 ipc_table_elems_t size
= its
->its_size
;
903 ipc_port_request_index_t index
;
904 for (index
= 1; index
< size
; index
++) {
905 ipc_port_request_t ipr
= &requests
[index
];
906 mach_port_name_t name
= ipr
->ipr_name
;
907 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
909 if (MACH_PORT_VALID(name
) && IP_VALID(soright
)) {
910 ipc_notify_dead_name(soright
, name
);
918 * Routine: ipc_port_destroy
920 * Destroys a port. Cleans up queued messages.
922 * If the port has a backup, it doesn't get destroyed,
923 * but is sent in a port-destroyed notification to the backup.
925 * The port is locked and alive; nothing else locked.
926 * The caller has a reference, which is consumed.
927 * Afterwards, the port is unlocked and dead.
931 ipc_port_destroy(ipc_port_t port
)
933 ipc_port_t pdrequest
, nsrequest
;
936 boolean_t special_reply
= port
->ip_specialreply
;
937 struct task_watchport_elem
*watchport_elem
= NULL
;
939 #if IMPORTANCE_INHERITANCE
940 ipc_importance_task_t release_imp_task
= IIT_NULL
;
941 thread_t self
= current_thread();
942 boolean_t top
= (self
->ith_assertions
== 0);
943 natural_t assertcnt
= 0;
944 #endif /* IMPORTANCE_INHERITANCE */
946 require_ip_active(port
);
947 /* port->ip_receiver_name is garbage */
948 /* port->ip_receiver/port->ip_destination is garbage */
950 /* clear any reply-port context */
951 port
->ip_reply_context
= 0;
953 /* check for a backup port */
954 pdrequest
= port
->ip_pdrequest
;
956 #if IMPORTANCE_INHERITANCE
957 /* determine how many assertions to drop and from whom */
958 if (port
->ip_tempowner
!= 0) {
960 release_imp_task
= port
->ip_imp_task
;
961 if (IIT_NULL
!= release_imp_task
) {
962 port
->ip_imp_task
= IIT_NULL
;
963 assertcnt
= port
->ip_impcount
;
965 /* Otherwise, nothing to drop */
967 assertcnt
= port
->ip_impcount
;
968 if (pdrequest
!= IP_NULL
) {
969 /* mark in limbo for the journey */
970 port
->ip_tempowner
= 1;
975 self
->ith_assertions
= assertcnt
;
977 #endif /* IMPORTANCE_INHERITANCE */
979 if (pdrequest
!= IP_NULL
) {
980 /* clear receiver, don't destroy the port */
981 (void)ipc_port_clear_receiver(port
, FALSE
);
982 assert(port
->ip_in_pset
== 0);
983 assert(port
->ip_mscount
== 0);
985 /* we assume the ref for pdrequest */
986 port
->ip_pdrequest
= IP_NULL
;
988 imq_lock(&port
->ip_messages
);
989 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
990 ipc_port_send_turnstile_recompute_push_locked(port
);
991 /* mqueue and port unlocked */
994 ipc_port_adjust_special_reply_port(port
,
995 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
);
998 if (watchport_elem
) {
999 task_watchport_elem_deallocate(watchport_elem
);
1000 watchport_elem
= NULL
;
1002 /* consumes our refs for port and pdrequest */
1003 ipc_notify_port_destroyed(pdrequest
, port
);
1005 goto drop_assertions
;
1009 * The mach_msg_* paths don't hold a port lock, they only hold a
1010 * reference to the port object. If a thread raced us and is now
1011 * blocked waiting for message reception on this mqueue (or waiting
1012 * for ipc_mqueue_full), it will never be woken up. We call
1013 * ipc_port_clear_receiver() here, _after_ the port has been marked
1014 * inactive, to wakeup any threads which may be blocked and ensure
1015 * that no other thread can get lost waiting for a wake up on a
1016 * port/mqueue that's been destroyed.
1018 boolean_t reap_msgs
= FALSE
;
1019 reap_msgs
= ipc_port_clear_receiver(port
, TRUE
); /* marks port and mqueue inactive */
1020 assert(port
->ip_in_pset
== 0);
1021 assert(port
->ip_mscount
== 0);
1023 imq_lock(&port
->ip_messages
);
1024 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1025 imq_unlock(&port
->ip_messages
);
1026 nsrequest
= port
->ip_nsrequest
;
1029 * If the port has a preallocated message buffer and that buffer
1030 * is not inuse, free it. If it has an inuse one, then the kmsg
1031 * free will detect that we freed the association and it can free it
1032 * like a normal buffer.
1034 * Once the port is marked inactive we don't need to keep it locked.
1036 if (IP_PREALLOC(port
)) {
1037 ipc_port_t inuse_port
;
1039 kmsg
= port
->ip_premsg
;
1040 assert(kmsg
!= IKM_NULL
);
1041 inuse_port
= ikm_prealloc_inuse_port(kmsg
);
1042 ipc_kmsg_clear_prealloc(kmsg
, port
);
1044 imq_lock(&port
->ip_messages
);
1045 ipc_port_send_turnstile_recompute_push_locked(port
);
1046 /* mqueue and port unlocked */
1048 if (inuse_port
!= IP_NULL
) {
1049 assert(inuse_port
== port
);
1051 ipc_kmsg_free(kmsg
);
1054 imq_lock(&port
->ip_messages
);
1055 ipc_port_send_turnstile_recompute_push_locked(port
);
1056 /* mqueue and port unlocked */
1059 /* Deallocate the watchport element */
1060 if (watchport_elem
) {
1061 task_watchport_elem_deallocate(watchport_elem
);
1062 watchport_elem
= NULL
;
1065 /* unlink the kmsg from special reply port */
1066 if (special_reply
) {
1067 ipc_port_adjust_special_reply_port(port
,
1068 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
);
1071 /* throw away no-senders request */
1072 if (nsrequest
!= IP_NULL
) {
1073 ipc_notify_send_once(nsrequest
); /* consumes ref */
1076 * Reap any kmsg objects waiting to be destroyed.
1077 * This must be done after we've released the port lock.
1080 ipc_kmsg_reap_delayed();
1083 mqueue
= &port
->ip_messages
;
1085 /* cleanup waitq related resources */
1086 ipc_mqueue_deinit(mqueue
);
1088 /* generate dead-name notifications */
1089 ipc_port_dnnotify(port
);
1091 ipc_kobject_destroy(port
);
1093 ip_release(port
); /* consume caller's ref */
1096 #if IMPORTANCE_INHERITANCE
1097 if (release_imp_task
!= IIT_NULL
) {
1098 if (assertcnt
> 0) {
1100 self
->ith_assertions
= 0;
1101 assert(ipc_importance_task_is_any_receiver_type(release_imp_task
));
1102 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1104 ipc_importance_task_release(release_imp_task
);
1105 } else if (assertcnt
> 0) {
1107 self
->ith_assertions
= 0;
1108 release_imp_task
= current_task()->task_imp_base
;
1109 if (ipc_importance_task_is_any_receiver_type(release_imp_task
)) {
1110 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1114 #endif /* IMPORTANCE_INHERITANCE */
1118 * Routine: ipc_port_check_circularity
1120 * Check if queueing "port" in a message for "dest"
1121 * would create a circular group of ports and messages.
1123 * If no circularity (FALSE returned), then "port"
1124 * is changed from "in limbo" to "in transit".
1126 * That is, we want to set port->ip_destination == dest,
1127 * but guaranteeing that this doesn't create a circle
1128 * port->ip_destination->ip_destination->... == port
1131 * No ports locked. References held for "port" and "dest".
1135 ipc_port_check_circularity(
1139 #if IMPORTANCE_INHERITANCE
1140 /* adjust importance counts at the same time */
1141 return ipc_importance_check_circularity(port
, dest
);
1144 struct task_watchport_elem
*watchport_elem
= NULL
;
1146 assert(port
!= IP_NULL
);
1147 assert(dest
!= IP_NULL
);
1154 /* Check if destination needs a turnstile */
1155 ipc_port_send_turnstile_prepare(dest
);
1158 * First try a quick check that can run in parallel.
1159 * No circularity if dest is not in transit.
1162 if (ip_lock_try(dest
)) {
1163 if (!ip_active(dest
) ||
1164 (dest
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1165 (dest
->ip_destination
== IP_NULL
)) {
1169 /* dest is in transit; further checking necessary */
1175 ipc_port_multiple_lock(); /* massive serialization */
1178 * Search for the end of the chain (a port not in transit),
1179 * acquiring locks along the way.
1185 if (!ip_active(base
) ||
1186 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1187 (base
->ip_destination
== IP_NULL
)) {
1191 base
= base
->ip_destination
;
1194 /* all ports in chain from dest to base, inclusive, are locked */
1197 /* circularity detected! */
1199 ipc_port_multiple_unlock();
1201 /* port (== base) is in limbo */
1202 require_ip_active(port
);
1203 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1204 assert(port
->ip_destination
== IP_NULL
);
1207 while (base
!= IP_NULL
) {
1210 /* dest is in transit or in limbo */
1211 require_ip_active(base
);
1212 assert(base
->ip_receiver_name
== MACH_PORT_NULL
);
1214 next
= base
->ip_destination
;
1219 ipc_port_send_turnstile_complete(dest
);
1224 * The guarantee: lock port while the entire chain is locked.
1225 * Once port is locked, we can take a reference to dest,
1226 * add port to the chain, and unlock everything.
1230 ipc_port_multiple_unlock();
1233 imq_lock(&port
->ip_messages
);
1235 /* port is in limbo */
1236 require_ip_active(port
);
1237 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1238 assert(port
->ip_destination
== IP_NULL
);
1240 /* Clear the watchport boost */
1241 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1243 /* Check if the port is being enqueued as a part of sync bootstrap checkin */
1244 if (dest
->ip_specialreply
&& dest
->ip_sync_bootstrap_checkin
) {
1245 port
->ip_sync_bootstrap_checkin
= 1;
1249 port
->ip_destination
= dest
;
1251 /* Setup linkage for source port if it has sync ipc push */
1252 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
1253 if (port_send_turnstile(port
)) {
1254 send_turnstile
= turnstile_prepare((uintptr_t)port
,
1255 port_send_turnstile_address(port
),
1256 TURNSTILE_NULL
, TURNSTILE_SYNC_IPC
);
1259 * What ipc_port_adjust_port_locked would do,
1260 * but we need to also drop even more locks before
1261 * calling turnstile_update_inheritor_complete().
1263 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1265 turnstile_update_inheritor(send_turnstile
, port_send_turnstile(dest
),
1266 (TURNSTILE_INHERITOR_TURNSTILE
| TURNSTILE_IMMEDIATE_UPDATE
));
1268 /* update complete and turnstile complete called after dropping all locks */
1270 imq_unlock(&port
->ip_messages
);
1272 /* now unlock chain */
1283 /* port is in transit */
1284 require_ip_active(dest
);
1285 assert(dest
->ip_receiver_name
== MACH_PORT_NULL
);
1286 assert(dest
->ip_destination
!= IP_NULL
);
1288 next
= dest
->ip_destination
;
1293 /* base is not in transit */
1294 assert(!ip_active(base
) ||
1295 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1296 (base
->ip_destination
== IP_NULL
));
1300 /* All locks dropped, call turnstile_update_inheritor_complete for source port's turnstile */
1301 if (send_turnstile
) {
1302 turnstile_update_inheritor_complete(send_turnstile
, TURNSTILE_INTERLOCK_NOT_HELD
);
1304 /* Take the mq lock to call turnstile complete */
1305 imq_lock(&port
->ip_messages
);
1306 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
), NULL
, TURNSTILE_SYNC_IPC
);
1307 send_turnstile
= TURNSTILE_NULL
;
1308 imq_unlock(&port
->ip_messages
);
1309 turnstile_cleanup();
1312 if (watchport_elem
) {
1313 task_watchport_elem_deallocate(watchport_elem
);
1317 #endif /* !IMPORTANCE_INHERITANCE */
1321 * Routine: ipc_port_watchport_elem
1323 * Get the port's watchport elem field
1328 static struct task_watchport_elem
*
1329 ipc_port_watchport_elem(ipc_port_t port
)
1331 return port
->ip_messages
.imq_wait_queue
.waitq_tspriv
;
1335 * Routine: ipc_port_update_watchport_elem
1337 * Set the port's watchport elem field
1342 static inline struct task_watchport_elem
*
1343 ipc_port_update_watchport_elem(ipc_port_t port
, struct task_watchport_elem
*we
)
1345 struct task_watchport_elem
*old_we
= ipc_port_watchport_elem(port
);
1346 port
->ip_messages
.imq_wait_queue
.waitq_tspriv
= we
;
1351 * Update the recv turnstile inheritor for a port.
1353 * Sync IPC through the port receive turnstile only happens for the special
1354 * reply port case. It has three sub-cases:
1356 * 1. a send-once right is in transit, and pushes on the send turnstile of its
1357 * destination mqueue.
1359 * 2. a send-once right has been stashed on a knote it was copied out "through",
1360 * as the first such copied out port.
1362 * 3. a send-once right has been stashed on a knote it was copied out "through",
1363 * as the second or more copied out port.
1366 ipc_port_recv_update_inheritor(
1368 struct turnstile
*rcv_turnstile
,
1369 turnstile_update_flags_t flags
)
1371 struct turnstile
*inheritor
= TURNSTILE_NULL
;
1374 if (ip_active(port
) && port
->ip_specialreply
) {
1375 imq_held(&port
->ip_messages
);
1377 switch (port
->ip_sync_link_state
) {
1378 case PORT_SYNC_LINK_PORT
:
1379 if (port
->ip_sync_inheritor_port
!= NULL
) {
1380 inheritor
= port_send_turnstile(port
->ip_sync_inheritor_port
);
1384 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1385 kn
= port
->ip_sync_inheritor_knote
;
1386 inheritor
= filt_ipc_kqueue_turnstile(kn
);
1389 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1390 inheritor
= port
->ip_sync_inheritor_ts
;
1395 turnstile_update_inheritor(rcv_turnstile
, inheritor
,
1396 flags
| TURNSTILE_INHERITOR_TURNSTILE
);
1400 * Update the send turnstile inheritor for a port.
1402 * Sync IPC through the port send turnstile has 7 possible reasons to be linked:
1404 * 1. a special reply port is part of sync ipc for bootstrap checkin and needs
1405 * to push on thread doing the sync ipc.
1407 * 2. a receive right is in transit, and pushes on the send turnstile of its
1408 * destination mqueue.
1410 * 3. port was passed as an exec watchport and port is pushing on main thread
1413 * 4. a receive right has been stashed on a knote it was copied out "through",
1414 * as the first such copied out port (same as PORT_SYNC_LINK_WORKLOOP_KNOTE
1415 * for the special reply port)
1417 * 5. a receive right has been stashed on a knote it was copied out "through",
1418 * as the second or more copied out port (same as
1419 * PORT_SYNC_LINK_WORKLOOP_STASH for the special reply port)
1421 * 6. a receive right has been copied out as a part of sync bootstrap checkin
1422 * and needs to push on thread doing the sync bootstrap checkin.
1424 * 7. the receive right is monitored by a knote, and pushes on any that is
1425 * registered on a workloop. filt_machport makes sure that if such a knote
1426 * exists, it is kept as the first item in the knote list, so we never need
1430 ipc_port_send_update_inheritor(
1432 struct turnstile
*send_turnstile
,
1433 turnstile_update_flags_t flags
)
1435 ipc_mqueue_t mqueue
= &port
->ip_messages
;
1436 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1438 turnstile_update_flags_t inheritor_flags
= TURNSTILE_INHERITOR_TURNSTILE
;
1440 assert(imq_held(mqueue
));
1442 if (!ip_active(port
)) {
1443 /* this port is no longer active, it should not push anywhere */
1444 } else if (port
->ip_specialreply
) {
1446 if (port
->ip_sync_bootstrap_checkin
&& prioritize_launch
) {
1447 inheritor
= port
->ip_messages
.imq_srp_owner_thread
;
1448 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1450 } else if (port
->ip_receiver_name
== MACH_PORT_NULL
&&
1451 port
->ip_destination
!= NULL
) {
1453 inheritor
= port_send_turnstile(port
->ip_destination
);
1454 } else if (ipc_port_watchport_elem(port
) != NULL
) {
1456 if (prioritize_launch
) {
1457 assert(port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
);
1458 inheritor
= ipc_port_get_watchport_inheritor(port
);
1459 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1461 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_WORKLOOP_KNOTE
) {
1463 inheritor
= filt_ipc_kqueue_turnstile(mqueue
->imq_inheritor_knote
);
1464 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_WORKLOOP_STASH
) {
1466 inheritor
= mqueue
->imq_inheritor_turnstile
;
1467 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_RCV_THREAD
) {
1469 if (prioritize_launch
) {
1470 inheritor
= port
->ip_messages
.imq_inheritor_thread_ref
;
1471 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1473 } else if ((kn
= SLIST_FIRST(&mqueue
->imq_klist
))) {
1474 /* Case 7. Push on a workloop that is interested */
1475 if (filt_machport_kqueue_has_turnstile(kn
)) {
1476 assert(port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
);
1477 inheritor
= filt_ipc_kqueue_turnstile(kn
);
1481 turnstile_update_inheritor(send_turnstile
, inheritor
,
1482 flags
| inheritor_flags
);
1486 * Routine: ipc_port_send_turnstile_prepare
1488 * Get a reference on port's send turnstile, if
1489 * port does not have a send turnstile then allocate one.
1492 * Nothing is locked.
1495 ipc_port_send_turnstile_prepare(ipc_port_t port
)
1497 struct turnstile
*turnstile
= TURNSTILE_NULL
;
1498 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
1501 imq_lock(&port
->ip_messages
);
1503 if (port_send_turnstile(port
) == NULL
||
1504 port_send_turnstile(port
)->ts_port_ref
== 0) {
1505 if (turnstile
== TURNSTILE_NULL
) {
1506 imq_unlock(&port
->ip_messages
);
1507 turnstile
= turnstile_alloc();
1511 send_turnstile
= turnstile_prepare((uintptr_t)port
,
1512 port_send_turnstile_address(port
),
1513 turnstile
, TURNSTILE_SYNC_IPC
);
1514 turnstile
= TURNSTILE_NULL
;
1516 ipc_port_send_update_inheritor(port
, send_turnstile
,
1517 TURNSTILE_IMMEDIATE_UPDATE
);
1519 /* turnstile complete will be called in ipc_port_send_turnstile_complete */
1522 /* Increment turnstile counter */
1523 port_send_turnstile(port
)->ts_port_ref
++;
1524 imq_unlock(&port
->ip_messages
);
1526 if (send_turnstile
) {
1527 turnstile_update_inheritor_complete(send_turnstile
,
1528 TURNSTILE_INTERLOCK_NOT_HELD
);
1530 if (turnstile
!= TURNSTILE_NULL
) {
1531 turnstile_deallocate(turnstile
);
1537 * Routine: ipc_port_send_turnstile_complete
1539 * Drop a ref on the port's send turnstile, if the
1540 * ref becomes zero, deallocate the turnstile.
1543 * The space might be locked, use safe deallocate.
1546 ipc_port_send_turnstile_complete(ipc_port_t port
)
1548 struct turnstile
*turnstile
= TURNSTILE_NULL
;
1550 /* Drop turnstile count on dest port */
1551 imq_lock(&port
->ip_messages
);
1553 port_send_turnstile(port
)->ts_port_ref
--;
1554 if (port_send_turnstile(port
)->ts_port_ref
== 0) {
1555 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
),
1556 &turnstile
, TURNSTILE_SYNC_IPC
);
1557 assert(turnstile
!= TURNSTILE_NULL
);
1559 imq_unlock(&port
->ip_messages
);
1560 turnstile_cleanup();
1562 if (turnstile
!= TURNSTILE_NULL
) {
1563 turnstile_deallocate_safe(turnstile
);
1564 turnstile
= TURNSTILE_NULL
;
1569 * Routine: ipc_port_rcv_turnstile
1571 * Get the port's receive turnstile
1574 * mqueue locked or thread waiting on turnstile is locked.
1576 static struct turnstile
*
1577 ipc_port_rcv_turnstile(ipc_port_t port
)
1579 return *port_rcv_turnstile_address(port
);
1584 * Routine: ipc_port_link_special_reply_port
1586 * Link the special reply port with the destination port.
1587 * Allocates turnstile to dest port.
1590 * Nothing is locked.
1593 ipc_port_link_special_reply_port(
1594 ipc_port_t special_reply_port
,
1595 ipc_port_t dest_port
,
1596 boolean_t sync_bootstrap_checkin
)
1598 boolean_t drop_turnstile_ref
= FALSE
;
1600 /* Check if dest_port needs a turnstile */
1601 ipc_port_send_turnstile_prepare(dest_port
);
1603 /* Lock the special reply port and establish the linkage */
1604 ip_lock(special_reply_port
);
1605 imq_lock(&special_reply_port
->ip_messages
);
1607 if (sync_bootstrap_checkin
&& special_reply_port
->ip_specialreply
) {
1608 special_reply_port
->ip_sync_bootstrap_checkin
= 1;
1611 /* Check if we need to drop the acquired turnstile ref on dest port */
1612 if (!special_reply_port
->ip_specialreply
||
1613 special_reply_port
->ip_sync_link_state
!= PORT_SYNC_LINK_ANY
||
1614 special_reply_port
->ip_sync_inheritor_port
!= IPC_PORT_NULL
) {
1615 drop_turnstile_ref
= TRUE
;
1617 /* take a reference on dest_port */
1618 ip_reference(dest_port
);
1619 special_reply_port
->ip_sync_inheritor_port
= dest_port
;
1620 special_reply_port
->ip_sync_link_state
= PORT_SYNC_LINK_PORT
;
1623 imq_unlock(&special_reply_port
->ip_messages
);
1624 ip_unlock(special_reply_port
);
1626 if (drop_turnstile_ref
) {
1627 ipc_port_send_turnstile_complete(dest_port
);
1633 #if DEVELOPMENT || DEBUG
1635 ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port
)
1637 special_reply_port
->ip_srp_lost_link
= 0;
1638 special_reply_port
->ip_srp_msg_sent
= 0;
1642 ipc_special_reply_port_msg_sent_reset(ipc_port_t special_reply_port
)
1644 if (special_reply_port
->ip_specialreply
== 1) {
1645 special_reply_port
->ip_srp_msg_sent
= 0;
1650 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port
)
1652 if (special_reply_port
->ip_specialreply
== 1) {
1653 special_reply_port
->ip_srp_msg_sent
= 1;
1658 ipc_special_reply_port_lost_link(ipc_port_t special_reply_port
)
1660 if (special_reply_port
->ip_specialreply
== 1 && special_reply_port
->ip_srp_msg_sent
== 0) {
1661 special_reply_port
->ip_srp_lost_link
= 1;
1665 #else /* DEVELOPMENT || DEBUG */
1667 ipc_special_reply_port_bits_reset(__unused ipc_port_t special_reply_port
)
1673 ipc_special_reply_port_msg_sent_reset(__unused ipc_port_t special_reply_port
)
1679 ipc_special_reply_port_msg_sent(__unused ipc_port_t special_reply_port
)
1685 ipc_special_reply_port_lost_link(__unused ipc_port_t special_reply_port
)
1689 #endif /* DEVELOPMENT || DEBUG */
1692 * Routine: ipc_port_adjust_special_reply_port_locked
1694 * If the special port has a turnstile, update its inheritor.
1696 * Special reply port locked on entry.
1697 * Special reply port unlocked on return.
1698 * The passed in port is a special reply port.
1703 ipc_port_adjust_special_reply_port_locked(
1704 ipc_port_t special_reply_port
,
1707 boolean_t get_turnstile
)
1709 ipc_port_t dest_port
= IPC_PORT_NULL
;
1710 int sync_link_state
= PORT_SYNC_LINK_NO_LINKAGE
;
1711 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1712 struct turnstile
*ts
= TURNSTILE_NULL
;
1714 ip_lock_held(special_reply_port
); // ip_sync_link_state is touched
1715 imq_lock(&special_reply_port
->ip_messages
);
1717 if (!special_reply_port
->ip_specialreply
) {
1718 // only mach_msg_receive_results_complete() calls this with any port
1719 assert(get_turnstile
);
1723 if (flags
& IPC_PORT_ADJUST_SR_RECEIVED_MSG
) {
1724 ipc_special_reply_port_msg_sent_reset(special_reply_port
);
1727 if (flags
& IPC_PORT_ADJUST_UNLINK_THREAD
) {
1728 special_reply_port
->ip_messages
.imq_srp_owner_thread
= NULL
;
1731 if (flags
& IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN
) {
1732 special_reply_port
->ip_sync_bootstrap_checkin
= 0;
1735 /* Check if the special reply port is marked non-special */
1736 if (special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
) {
1738 if (get_turnstile
) {
1739 turnstile_complete((uintptr_t)special_reply_port
,
1740 port_rcv_turnstile_address(special_reply_port
), NULL
, TURNSTILE_SYNC_IPC
);
1742 imq_unlock(&special_reply_port
->ip_messages
);
1743 ip_unlock(special_reply_port
);
1744 if (get_turnstile
) {
1745 turnstile_cleanup();
1750 if (flags
& IPC_PORT_ADJUST_SR_LINK_WORKLOOP
) {
1751 if (ITH_KNOTE_VALID(kn
, MACH_MSG_TYPE_PORT_SEND_ONCE
)) {
1752 inheritor
= filt_machport_stash_port(kn
, special_reply_port
,
1755 } else if (flags
& IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
) {
1756 sync_link_state
= PORT_SYNC_LINK_ANY
;
1759 /* Check if need to break linkage */
1760 if (!get_turnstile
&& sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
&&
1761 special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
) {
1762 imq_unlock(&special_reply_port
->ip_messages
);
1763 ip_unlock(special_reply_port
);
1767 switch (special_reply_port
->ip_sync_link_state
) {
1768 case PORT_SYNC_LINK_PORT
:
1769 dest_port
= special_reply_port
->ip_sync_inheritor_port
;
1770 special_reply_port
->ip_sync_inheritor_port
= IPC_PORT_NULL
;
1772 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1773 special_reply_port
->ip_sync_inheritor_knote
= NULL
;
1775 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1776 special_reply_port
->ip_sync_inheritor_ts
= NULL
;
1780 special_reply_port
->ip_sync_link_state
= sync_link_state
;
1782 switch (sync_link_state
) {
1783 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1784 special_reply_port
->ip_sync_inheritor_knote
= kn
;
1786 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1787 special_reply_port
->ip_sync_inheritor_ts
= inheritor
;
1789 case PORT_SYNC_LINK_NO_LINKAGE
:
1790 if (flags
& IPC_PORT_ADJUST_SR_ENABLE_EVENT
) {
1791 ipc_special_reply_port_lost_link(special_reply_port
);
1796 /* Get thread's turnstile donated to special reply port */
1797 if (get_turnstile
) {
1798 turnstile_complete((uintptr_t)special_reply_port
,
1799 port_rcv_turnstile_address(special_reply_port
), NULL
, TURNSTILE_SYNC_IPC
);
1801 ts
= ipc_port_rcv_turnstile(special_reply_port
);
1803 turnstile_reference(ts
);
1804 ipc_port_recv_update_inheritor(special_reply_port
, ts
,
1805 TURNSTILE_IMMEDIATE_UPDATE
);
1809 imq_unlock(&special_reply_port
->ip_messages
);
1810 ip_unlock(special_reply_port
);
1812 if (get_turnstile
) {
1813 turnstile_cleanup();
1815 /* Call turnstile cleanup after dropping the interlock */
1816 turnstile_update_inheritor_complete(ts
, TURNSTILE_INTERLOCK_NOT_HELD
);
1817 turnstile_deallocate_safe(ts
);
1820 /* Release the ref on the dest port and its turnstile */
1822 ipc_port_send_turnstile_complete(dest_port
);
1823 /* release the reference on the dest port */
1824 ip_release(dest_port
);
1829 * Routine: ipc_port_adjust_special_reply_port
1831 * If the special port has a turnstile, update its inheritor.
1838 ipc_port_adjust_special_reply_port(
1842 if (port
->ip_specialreply
) {
1844 ipc_port_adjust_special_reply_port_locked(port
, NULL
, flags
, FALSE
);
1849 * Routine: ipc_port_adjust_sync_link_state_locked
1851 * Update the sync link state of the port and the
1852 * turnstile inheritor.
1854 * Port and mqueue locked on entry.
1855 * Port and mqueue locked on return.
1860 ipc_port_adjust_sync_link_state_locked(
1862 int sync_link_state
,
1863 turnstile_inheritor_t inheritor
)
1865 switch (port
->ip_sync_link_state
) {
1866 case PORT_SYNC_LINK_RCV_THREAD
:
1867 /* deallocate the thread reference for the inheritor */
1868 thread_deallocate_safe(port
->ip_messages
.imq_inheritor_thread_ref
);
1872 klist_init(&port
->ip_messages
.imq_klist
);
1875 switch (sync_link_state
) {
1876 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1877 port
->ip_messages
.imq_inheritor_knote
= inheritor
;
1879 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1880 port
->ip_messages
.imq_inheritor_turnstile
= inheritor
;
1882 case PORT_SYNC_LINK_RCV_THREAD
:
1883 /* The thread could exit without clearing port state, take a thread ref */
1884 thread_reference((thread_t
)inheritor
);
1885 port
->ip_messages
.imq_inheritor_thread_ref
= inheritor
;
1888 klist_init(&port
->ip_messages
.imq_klist
);
1889 sync_link_state
= PORT_SYNC_LINK_ANY
;
1892 port
->ip_sync_link_state
= sync_link_state
;
1897 * Routine: ipc_port_adjust_port_locked
1899 * If the port has a turnstile, update its inheritor.
1901 * Port locked on entry.
1902 * Port unlocked on return.
1907 ipc_port_adjust_port_locked(
1910 boolean_t sync_bootstrap_checkin
)
1912 int sync_link_state
= PORT_SYNC_LINK_ANY
;
1913 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1915 ip_lock_held(port
); // ip_sync_link_state is touched
1916 imq_held(&port
->ip_messages
);
1918 assert(!port
->ip_specialreply
);
1921 inheritor
= filt_machport_stash_port(kn
, port
, &sync_link_state
);
1922 if (sync_link_state
== PORT_SYNC_LINK_WORKLOOP_KNOTE
) {
1925 } else if (sync_bootstrap_checkin
) {
1926 inheritor
= current_thread();
1927 sync_link_state
= PORT_SYNC_LINK_RCV_THREAD
;
1930 ipc_port_adjust_sync_link_state_locked(port
, sync_link_state
, inheritor
);
1931 port
->ip_sync_bootstrap_checkin
= 0;
1933 ipc_port_send_turnstile_recompute_push_locked(port
);
1934 /* port and mqueue unlocked */
1938 * Routine: ipc_port_clear_sync_rcv_thread_boost_locked
1940 * If the port is pushing on rcv thread, clear it.
1942 * Port locked on entry
1943 * mqueue is not locked.
1944 * Port unlocked on return.
1949 ipc_port_clear_sync_rcv_thread_boost_locked(
1952 ip_lock_held(port
); // ip_sync_link_state is touched
1954 if (port
->ip_sync_link_state
!= PORT_SYNC_LINK_RCV_THREAD
) {
1959 imq_lock(&port
->ip_messages
);
1960 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1962 ipc_port_send_turnstile_recompute_push_locked(port
);
1963 /* port and mqueue unlocked */
1967 * Routine: ipc_port_add_watchport_elem_locked
1969 * Transfer the turnstile boost of watchport to task calling exec.
1971 * Port locked on entry.
1972 * Port unlocked on return.
1974 * KERN_SUCESS on success.
1975 * KERN_FAILURE otherwise.
1978 ipc_port_add_watchport_elem_locked(
1980 struct task_watchport_elem
*watchport_elem
,
1981 struct task_watchport_elem
**old_elem
)
1984 imq_held(&port
->ip_messages
);
1986 /* Watchport boost only works for non-special active ports mapped in an ipc space */
1987 if (!ip_active(port
) || port
->ip_specialreply
||
1988 port
->ip_receiver_name
== MACH_PORT_NULL
) {
1989 imq_unlock(&port
->ip_messages
);
1991 return KERN_FAILURE
;
1994 if (port
->ip_sync_link_state
!= PORT_SYNC_LINK_ANY
) {
1995 /* Sever the linkage if the port was pushing on knote */
1996 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1999 *old_elem
= ipc_port_update_watchport_elem(port
, watchport_elem
);
2001 ipc_port_send_turnstile_recompute_push_locked(port
);
2002 /* port and mqueue unlocked */
2003 return KERN_SUCCESS
;
2007 * Routine: ipc_port_clear_watchport_elem_internal_conditional_locked
2009 * Remove the turnstile boost of watchport and recompute the push.
2011 * Port locked on entry.
2012 * Port unlocked on return.
2014 * KERN_SUCESS on success.
2015 * KERN_FAILURE otherwise.
2018 ipc_port_clear_watchport_elem_internal_conditional_locked(
2020 struct task_watchport_elem
*watchport_elem
)
2023 imq_held(&port
->ip_messages
);
2025 if (ipc_port_watchport_elem(port
) != watchport_elem
) {
2026 imq_unlock(&port
->ip_messages
);
2028 return KERN_FAILURE
;
2031 ipc_port_clear_watchport_elem_internal(port
);
2032 ipc_port_send_turnstile_recompute_push_locked(port
);
2033 /* port and mqueue unlocked */
2034 return KERN_SUCCESS
;
2038 * Routine: ipc_port_replace_watchport_elem_conditional_locked
2040 * Replace the turnstile boost of watchport and recompute the push.
2042 * Port locked on entry.
2043 * Port unlocked on return.
2045 * KERN_SUCESS on success.
2046 * KERN_FAILURE otherwise.
2049 ipc_port_replace_watchport_elem_conditional_locked(
2051 struct task_watchport_elem
*old_watchport_elem
,
2052 struct task_watchport_elem
*new_watchport_elem
)
2055 imq_held(&port
->ip_messages
);
2057 if (ipc_port_watchport_elem(port
) != old_watchport_elem
) {
2058 imq_unlock(&port
->ip_messages
);
2060 return KERN_FAILURE
;
2063 ipc_port_update_watchport_elem(port
, new_watchport_elem
);
2064 ipc_port_send_turnstile_recompute_push_locked(port
);
2065 /* port and mqueue unlocked */
2066 return KERN_SUCCESS
;
2070 * Routine: ipc_port_clear_watchport_elem_internal
2072 * Remove the turnstile boost of watchport.
2074 * Port locked on entry.
2075 * Port locked on return.
2077 * Old task_watchport_elem returned.
2079 struct task_watchport_elem
*
2080 ipc_port_clear_watchport_elem_internal(
2084 imq_held(&port
->ip_messages
);
2086 return ipc_port_update_watchport_elem(port
, NULL
);
2090 * Routine: ipc_port_send_turnstile_recompute_push_locked
2092 * Update send turnstile inheritor of port and recompute the push.
2094 * Port locked on entry.
2095 * Port unlocked on return.
2100 ipc_port_send_turnstile_recompute_push_locked(
2103 struct turnstile
*send_turnstile
= port_send_turnstile(port
);
2104 if (send_turnstile
) {
2105 turnstile_reference(send_turnstile
);
2106 ipc_port_send_update_inheritor(port
, send_turnstile
,
2107 TURNSTILE_IMMEDIATE_UPDATE
);
2109 imq_unlock(&port
->ip_messages
);
2112 if (send_turnstile
) {
2113 turnstile_update_inheritor_complete(send_turnstile
,
2114 TURNSTILE_INTERLOCK_NOT_HELD
);
2115 turnstile_deallocate_safe(send_turnstile
);
2120 * Routine: ipc_port_get_watchport_inheritor
2122 * Returns inheritor for watchport.
2127 * watchport inheritor.
2130 ipc_port_get_watchport_inheritor(
2133 imq_held(&port
->ip_messages
);
2134 return ipc_port_watchport_elem(port
)->twe_task
->watchports
->tw_thread
;
2138 * Routine: ipc_port_impcount_delta
2140 * Adjust only the importance count associated with a port.
2141 * If there are any adjustments to be made to receiver task,
2142 * those are handled elsewhere.
2144 * For now, be defensive during deductions to make sure the
2145 * impcount for the port doesn't underflow zero. This will
2146 * go away when the port boost addition is made atomic (see
2147 * note in ipc_port_importance_delta()).
2149 * The port is referenced and locked.
2150 * Nothing else is locked.
2153 ipc_port_impcount_delta(
2155 mach_port_delta_t delta
,
2156 ipc_port_t __unused base
)
2158 mach_port_delta_t absdelta
;
2160 if (!ip_active(port
)) {
2164 /* adding/doing nothing is easy */
2166 port
->ip_impcount
+= delta
;
2170 absdelta
= 0 - delta
;
2171 if (port
->ip_impcount
>= absdelta
) {
2172 port
->ip_impcount
-= absdelta
;
2176 #if (DEVELOPMENT || DEBUG)
2177 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
2178 task_t target_task
= port
->ip_receiver
->is_task
;
2179 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
2180 const char *target_procname
;
2183 if (target_imp
!= IIT_NULL
) {
2184 target_procname
= target_imp
->iit_procname
;
2185 target_pid
= target_imp
->iit_bsd_pid
;
2187 target_procname
= "unknown";
2190 printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
2191 "dropping %d assertion(s) but port only has %d remaining.\n",
2192 port
->ip_receiver_name
,
2193 target_pid
, target_procname
,
2194 absdelta
, port
->ip_impcount
);
2195 } else if (base
!= IP_NULL
) {
2196 task_t target_task
= base
->ip_receiver
->is_task
;
2197 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
2198 const char *target_procname
;
2201 if (target_imp
!= IIT_NULL
) {
2202 target_procname
= target_imp
->iit_procname
;
2203 target_pid
= target_imp
->iit_bsd_pid
;
2205 target_procname
= "unknown";
2208 printf("Over-release of importance assertions for port 0x%lx "
2209 "enqueued on port 0x%x with receiver pid %d (%s), "
2210 "dropping %d assertion(s) but port only has %d remaining.\n",
2211 (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port
),
2212 base
->ip_receiver_name
,
2213 target_pid
, target_procname
,
2214 absdelta
, port
->ip_impcount
);
2218 delta
= 0 - port
->ip_impcount
;
2219 port
->ip_impcount
= 0;
2224 * Routine: ipc_port_importance_delta_internal
2226 * Adjust the importance count through the given port.
2227 * If the port is in transit, apply the delta throughout
2228 * the chain. Determine if the there is a task at the
2229 * base of the chain that wants/needs to be adjusted,
2230 * and if so, apply the delta.
2232 * The port is referenced and locked on entry.
2233 * Importance may be locked.
2234 * Nothing else is locked.
2235 * The lock may be dropped on exit.
2236 * Returns TRUE if lock was dropped.
2238 #if IMPORTANCE_INHERITANCE
2241 ipc_port_importance_delta_internal(
2244 mach_port_delta_t
*deltap
,
2245 ipc_importance_task_t
*imp_task
)
2247 ipc_port_t next
, base
;
2248 boolean_t dropped
= FALSE
;
2250 *imp_task
= IIT_NULL
;
2256 assert(options
== IPID_OPTION_NORMAL
|| options
== IPID_OPTION_SENDPOSSIBLE
);
2260 /* if port is in transit, have to search for end of chain */
2261 if (ip_active(port
) &&
2262 port
->ip_destination
!= IP_NULL
&&
2263 port
->ip_receiver_name
== MACH_PORT_NULL
) {
2267 ipc_port_multiple_lock(); /* massive serialization */
2270 while (ip_active(base
) &&
2271 base
->ip_destination
!= IP_NULL
&&
2272 base
->ip_receiver_name
== MACH_PORT_NULL
) {
2273 base
= base
->ip_destination
;
2276 ipc_port_multiple_unlock();
2280 * If the port lock is dropped b/c the port is in transit, there is a
2281 * race window where another thread can drain messages and/or fire a
2282 * send possible notification before we get here.
2284 * We solve this race by checking to see if our caller armed the send
2285 * possible notification, whether or not it's been fired yet, and
2286 * whether or not we've already set the port's ip_spimportant bit. If
2287 * we don't need a send-possible boost, then we'll just apply a
2288 * harmless 0-boost to the port.
2290 if (options
& IPID_OPTION_SENDPOSSIBLE
) {
2291 assert(*deltap
== 1);
2292 if (port
->ip_sprequests
&& port
->ip_spimportant
== 0) {
2293 port
->ip_spimportant
= 1;
2299 /* unlock down to the base, adjusting boost(s) at each level */
2301 *deltap
= ipc_port_impcount_delta(port
, *deltap
, base
);
2307 /* port is in transit */
2308 assert(port
->ip_tempowner
== 0);
2309 next
= port
->ip_destination
;
2314 /* find the task (if any) to boost according to the base */
2315 if (ip_active(base
)) {
2316 if (base
->ip_tempowner
!= 0) {
2317 if (IIT_NULL
!= base
->ip_imp_task
) {
2318 *imp_task
= base
->ip_imp_task
;
2320 /* otherwise don't boost */
2321 } else if (base
->ip_receiver_name
!= MACH_PORT_NULL
) {
2322 ipc_space_t space
= base
->ip_receiver
;
2324 /* only spaces with boost-accepting tasks */
2325 if (space
->is_task
!= TASK_NULL
&&
2326 ipc_importance_task_is_any_receiver_type(space
->is_task
->task_imp_base
)) {
2327 *imp_task
= space
->is_task
->task_imp_base
;
2333 * Only the base is locked. If we have to hold or drop task
2334 * importance assertions, we'll have to drop that lock as well.
2336 if (*imp_task
!= IIT_NULL
) {
2337 /* take a reference before unlocking base */
2338 ipc_importance_task_reference(*imp_task
);
2341 if (dropped
== TRUE
) {
2347 #endif /* IMPORTANCE_INHERITANCE */
2350 * Routine: ipc_port_importance_delta
2352 * Adjust the importance count through the given port.
2353 * If the port is in transit, apply the delta throughout
2356 * If there is a task at the base of the chain that wants/needs
2357 * to be adjusted, apply the delta.
2359 * The port is referenced and locked on entry.
2360 * Nothing else is locked.
2361 * The lock may be dropped on exit.
2362 * Returns TRUE if lock was dropped.
2364 #if IMPORTANCE_INHERITANCE
2367 ipc_port_importance_delta(
2370 mach_port_delta_t delta
)
2372 ipc_importance_task_t imp_task
= IIT_NULL
;
2375 dropped
= ipc_port_importance_delta_internal(port
, options
, &delta
, &imp_task
);
2377 if (IIT_NULL
== imp_task
|| delta
== 0) {
2385 assert(ipc_importance_task_is_any_receiver_type(imp_task
));
2388 ipc_importance_task_hold_internal_assertion(imp_task
, delta
);
2390 ipc_importance_task_drop_internal_assertion(imp_task
, -delta
);
2393 ipc_importance_task_release(imp_task
);
2396 #endif /* IMPORTANCE_INHERITANCE */
2399 * Routine: ipc_port_make_send_locked
2401 * Make a naked send right from a receive right.
2404 * port locked and active.
2407 ipc_port_make_send_locked(
2410 require_ip_active(port
);
2418 * Routine: ipc_port_make_send
2420 * Make a naked send right from a receive right.
2427 if (!IP_VALID(port
)) {
2432 if (ip_active(port
)) {
2433 ipc_port_make_send_locked(port
);
2442 * Routine: ipc_port_copy_send_locked
2444 * Make a naked send right from another naked send right.
2446 * port locked and active.
2449 ipc_port_copy_send_locked(
2452 assert(port
->ip_srights
> 0);
2458 * Routine: ipc_port_copy_send
2460 * Make a naked send right from another naked send right.
2461 * IP_NULL -> IP_NULL
2462 * IP_DEAD -> IP_DEAD
2463 * dead port -> IP_DEAD
2464 * live port -> port + ref
2466 * Nothing locked except possibly a space.
2475 if (!IP_VALID(port
)) {
2480 if (ip_active(port
)) {
2481 ipc_port_copy_send_locked(port
);
2492 * Routine: ipc_port_copyout_send
2494 * Copyout a naked send right (possibly null/dead),
2495 * or if that fails, destroy the right.
2501 ipc_port_copyout_send(
2505 mach_port_name_t name
;
2507 if (IP_VALID(sright
)) {
2510 kr
= ipc_object_copyout(space
, ip_to_object(sright
),
2511 MACH_MSG_TYPE_PORT_SEND
, NULL
, NULL
, &name
);
2512 if (kr
!= KERN_SUCCESS
) {
2513 ipc_port_release_send(sright
);
2515 if (kr
== KERN_INVALID_CAPABILITY
) {
2516 name
= MACH_PORT_DEAD
;
2518 name
= MACH_PORT_NULL
;
2522 name
= CAST_MACH_PORT_TO_NAME(sright
);
2529 * Routine: ipc_port_release_send
2531 * Release a naked send right.
2532 * Consumes a ref for the port.
2538 ipc_port_release_send(
2541 ipc_port_t nsrequest
= IP_NULL
;
2542 mach_port_mscount_t mscount
;
2544 if (!IP_VALID(port
)) {
2550 assert(port
->ip_srights
> 0);
2551 if (port
->ip_srights
== 0) {
2552 panic("Over-release of port %p send right!", port
);
2557 if (!ip_active(port
)) {
2563 if (port
->ip_srights
== 0 &&
2564 port
->ip_nsrequest
!= IP_NULL
) {
2565 nsrequest
= port
->ip_nsrequest
;
2566 port
->ip_nsrequest
= IP_NULL
;
2567 mscount
= port
->ip_mscount
;
2570 ipc_notify_no_senders(nsrequest
, mscount
);
2578 * Routine: ipc_port_make_sonce_locked
2580 * Make a naked send-once right from a receive right.
2582 * The port is locked and active.
2586 ipc_port_make_sonce_locked(
2589 require_ip_active(port
);
2590 port
->ip_sorights
++;
2596 * Routine: ipc_port_make_sonce
2598 * Make a naked send-once right from a receive right.
2600 * The port is not locked.
2604 ipc_port_make_sonce(
2607 if (!IP_VALID(port
)) {
2612 if (ip_active(port
)) {
2613 ipc_port_make_sonce_locked(port
);
2622 * Routine: ipc_port_release_sonce
2624 * Release a naked send-once right.
2625 * Consumes a ref for the port.
2627 * In normal situations, this is never used.
2628 * Send-once rights are only consumed when
2629 * a message (possibly a send-once notification)
2632 * Nothing locked except possibly a space.
2636 ipc_port_release_sonce(
2639 if (!IP_VALID(port
)) {
2643 ipc_port_adjust_special_reply_port(port
, IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN
);
2647 assert(port
->ip_sorights
> 0);
2648 if (port
->ip_sorights
== 0) {
2649 panic("Over-release of port %p send-once right!", port
);
2652 port
->ip_sorights
--;
2659 * Routine: ipc_port_release_receive
2661 * Release a naked (in limbo or in transit) receive right.
2662 * Consumes a ref for the port; destroys the port.
2668 ipc_port_release_receive(
2673 if (!IP_VALID(port
)) {
2678 require_ip_active(port
);
2679 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
2680 dest
= port
->ip_destination
;
2682 ipc_port_destroy(port
); /* consumes ref, unlocks */
2684 if (dest
!= IP_NULL
) {
2685 ipc_port_send_turnstile_complete(dest
);
2691 * Routine: ipc_port_alloc_special
2693 * Allocate a port in a special space.
2694 * The new port is returned with one ref.
2695 * If unsuccessful, IP_NULL is returned.
2701 ipc_port_alloc_special(
2703 ipc_port_init_flags_t flags
)
2707 port
= ip_object_to_port(io_alloc(IOT_PORT
));
2708 if (port
== IP_NULL
) {
2713 uintptr_t buf
[IP_CALLSTACK_MAX
];
2714 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
2715 #endif /* MACH_ASSERT */
2717 bzero((char *)port
, sizeof(*port
));
2718 io_lock_init(ip_to_object(port
));
2719 port
->ip_references
= 1;
2720 port
->ip_object
.io_bits
= io_makebits(TRUE
, IOT_PORT
, 0);
2722 ipc_port_init(port
, space
, flags
, 1);
2725 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
2726 #endif /* MACH_ASSERT */
2732 * Routine: ipc_port_dealloc_special
2734 * Deallocate a port in a special space.
2735 * Consumes one ref for the port.
2741 ipc_port_dealloc_special(
2743 __assert_only ipc_space_t space
)
2746 require_ip_active(port
);
2747 // assert(port->ip_receiver_name != MACH_PORT_NULL);
2748 assert(port
->ip_receiver
== space
);
2751 * We clear ip_receiver_name and ip_receiver to simplify
2752 * the ipc_space_kernel check in ipc_mqueue_send.
2755 imq_lock(&port
->ip_messages
);
2756 port
->ip_receiver_name
= MACH_PORT_NULL
;
2757 port
->ip_receiver
= IS_NULL
;
2758 imq_unlock(&port
->ip_messages
);
2760 /* relevant part of ipc_port_clear_receiver */
2761 port
->ip_mscount
= 0;
2762 port
->ip_messages
.imq_seqno
= 0;
2764 ipc_port_destroy(port
);
2768 * Routine: ipc_port_finalize
2770 * Called on last reference deallocate to
2771 * free any remaining data associated with the
2780 ipc_port_request_t requests
= port
->ip_requests
;
2782 assert(port_send_turnstile(port
) == TURNSTILE_NULL
);
2783 if (imq_is_turnstile_proxy(&port
->ip_messages
)) {
2784 assert(ipc_port_rcv_turnstile(port
) == TURNSTILE_NULL
);
2787 if (ip_active(port
)) {
2788 panic("Trying to free an active port. port %p", port
);
2791 if (requests
!= IPR_NULL
) {
2792 ipc_table_size_t its
= requests
->ipr_size
;
2793 it_requests_free(its
, requests
);
2794 port
->ip_requests
= IPR_NULL
;
2797 ipc_mqueue_deinit(&port
->ip_messages
);
2800 ipc_port_track_dealloc(port
);
2801 #endif /* MACH_ASSERT */
2805 * Routine: kdp_mqueue_send_find_owner
2807 * Discover the owner of the ipc_mqueue that contains the input
2808 * waitq object. The thread blocked on the waitq should be
2809 * waiting for an IPC_MQUEUE_FULL event.
2811 * The 'waitinfo->wait_type' value should already be set to
2812 * kThreadWaitPortSend.
2814 * If we find out that the containing port is actually in
2815 * transit, we reset the wait_type field to reflect this.
2818 kdp_mqueue_send_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
2820 struct turnstile
*turnstile
;
2821 assert(waitinfo
->wait_type
== kThreadWaitPortSend
);
2822 assert(event
== IPC_MQUEUE_FULL
);
2823 assert(waitq_is_turnstile_queue(waitq
));
2825 turnstile
= waitq_to_turnstile(waitq
);
2826 ipc_port_t port
= (ipc_port_t
)turnstile
->ts_proprietor
; /* we are blocking on send */
2827 assert(kdp_is_in_zone(port
, "ipc ports"));
2829 waitinfo
->owner
= 0;
2830 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
2831 if (ip_lock_held_kdp(port
)) {
2833 * someone has the port locked: it may be in an
2834 * inconsistent state: bail
2836 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
2840 if (ip_active(port
)) {
2841 if (port
->ip_tempowner
) {
2842 if (port
->ip_imp_task
!= IIT_NULL
&& port
->ip_imp_task
->iit_task
!= NULL
) {
2843 /* port is held by a tempowner */
2844 waitinfo
->owner
= pid_from_task(port
->ip_imp_task
->iit_task
);
2846 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
2848 } else if (port
->ip_receiver_name
) {
2849 /* port in a space */
2850 if (port
->ip_receiver
== ipc_space_kernel
) {
2852 * The kernel pid is 0, make this
2853 * distinguishable from no-owner and
2854 * inconsistent port state.
2856 waitinfo
->owner
= STACKSHOT_WAITOWNER_KERNEL
;
2858 waitinfo
->owner
= pid_from_task(port
->ip_receiver
->is_task
);
2860 } else if (port
->ip_destination
!= IP_NULL
) {
2861 /* port in transit */
2862 waitinfo
->wait_type
= kThreadWaitPortSendInTransit
;
2863 waitinfo
->owner
= VM_KERNEL_UNSLIDE_OR_PERM(port
->ip_destination
);
2869 * Routine: kdp_mqueue_recv_find_owner
2871 * Discover the "owner" of the ipc_mqueue that contains the input
2872 * waitq object. The thread blocked on the waitq is trying to
2873 * receive on the mqueue.
2875 * The 'waitinfo->wait_type' value should already be set to
2876 * kThreadWaitPortReceive.
2878 * If we find that we are actualy waiting on a port set, we reset
2879 * the wait_type field to reflect this.
2882 kdp_mqueue_recv_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
2884 assert(waitinfo
->wait_type
== kThreadWaitPortReceive
);
2885 assert(event
== IPC_MQUEUE_RECEIVE
);
2887 ipc_mqueue_t mqueue
= imq_from_waitq(waitq
);
2888 waitinfo
->owner
= 0;
2889 if (imq_is_set(mqueue
)) { /* we are waiting on a port set */
2890 ipc_pset_t set
= ips_from_mq(mqueue
);
2891 assert(kdp_is_in_zone(set
, "ipc port sets"));
2893 /* Reset wait type to specify waiting on port set receive */
2894 waitinfo
->wait_type
= kThreadWaitPortSetReceive
;
2895 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(set
);
2896 if (ips_lock_held_kdp(set
)) {
2897 waitinfo
->owner
= STACKSHOT_WAITOWNER_PSET_LOCKED
;
2899 /* There is no specific owner "at the other end" of a port set, so leave unset. */
2901 ipc_port_t port
= ip_from_mq(mqueue
);
2902 assert(kdp_is_in_zone(port
, "ipc ports"));
2904 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
2905 if (ip_lock_held_kdp(port
)) {
2906 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
2910 if (ip_active(port
)) {
2911 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
2912 waitinfo
->owner
= port
->ip_receiver_name
;
2914 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
2921 #include <kern/machine.h>
2924 * Keep a list of all allocated ports.
2925 * Allocation is intercepted via ipc_port_init;
2926 * deallocation is intercepted via io_free.
2929 queue_head_t port_alloc_queue
;
2930 lck_spin_t port_alloc_queue_lock
;
2933 unsigned long port_count
= 0;
2934 unsigned long port_count_warning
= 20000;
2935 unsigned long port_timestamp
= 0;
2937 void db_port_stack_trace(
2942 unsigned int verbose
,
2943 unsigned int display
,
2944 unsigned int ref_search
,
2945 unsigned int ref_target
);
2948 * Initialize global state needed for run-time
2952 ipc_port_debug_init(void)
2955 queue_init(&port_alloc_queue
);
2956 lck_spin_init(&port_alloc_queue_lock
, &ipc_lck_grp
, &ipc_lck_attr
);
2959 if (!PE_parse_boot_argn("ipc_portbt", &ipc_portbt
, sizeof(ipc_portbt
))) {
2965 extern int proc_pid(struct proc
*);
2966 #endif /* MACH_BSD */
2969 * Initialize all of the debugging state in a port.
2970 * Insert the port into a global list of all allocated ports.
2973 ipc_port_init_debug(
2975 uintptr_t *callstack
,
2976 unsigned int callstack_max
)
2980 port
->ip_thread
= current_thread();
2981 port
->ip_timetrack
= port_timestamp
++;
2982 for (i
= 0; i
< callstack_max
; ++i
) {
2983 port
->ip_callstack
[i
] = callstack
[i
];
2985 for (i
= 0; i
< IP_NSPARES
; ++i
) {
2986 port
->ip_spares
[i
] = 0;
2990 task_t task
= current_task();
2991 if (task
!= TASK_NULL
) {
2992 struct proc
* proc
= (struct proc
*) get_bsdtask_info(task
);
2994 port
->ip_spares
[0] = proc_pid(proc
);
2997 #endif /* MACH_BSD */
3000 lck_spin_lock(&port_alloc_queue_lock
);
3002 if (port_count_warning
> 0 && port_count
>= port_count_warning
) {
3003 assert(port_count
< port_count_warning
);
3005 queue_enter(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
3006 lck_spin_unlock(&port_alloc_queue_lock
);
3011 * Routine: ipc_port_callstack_init_debug
3013 * Calls the machine-dependent routine to
3014 * fill in an array with up to IP_CALLSTACK_MAX
3015 * levels of return pc information
3017 * May block (via copyin)
3020 ipc_port_callstack_init_debug(
3021 uintptr_t *callstack
,
3022 unsigned int callstack_max
)
3026 /* guarantee the callstack is initialized */
3027 for (i
= 0; i
< callstack_max
; i
++) {
3032 machine_callstack(callstack
, callstack_max
);
3037 * Remove a port from the queue of allocated ports.
3038 * This routine should be invoked JUST prior to
3039 * deallocating the actual memory occupied by the port.
3043 ipc_port_track_dealloc(
3044 __unused ipc_port_t port
)
3049 ipc_port_track_dealloc(
3052 lck_spin_lock(&port_alloc_queue_lock
);
3053 assert(port_count
> 0);
3055 queue_remove(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
3056 lck_spin_unlock(&port_alloc_queue_lock
);
3061 #endif /* MACH_ASSERT */