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 mach_port_name_t name
)
643 /* port->ip_kobject doesn't have to be initialized */
645 port
->ip_receiver
= space
;
646 port
->ip_receiver_name
= name
;
648 port
->ip_mscount
= 0;
649 port
->ip_srights
= 0;
650 port
->ip_sorights
= 0;
652 port
->ip_nsrequest
= IP_NULL
;
653 port
->ip_pdrequest
= IP_NULL
;
654 port
->ip_requests
= IPR_NULL
;
656 port
->ip_premsg
= IKM_NULL
;
657 port
->ip_context
= 0;
658 port
->ip_reply_context
= 0;
660 port
->ip_sprequests
= 0;
661 port
->ip_spimportant
= 0;
662 port
->ip_impdonation
= 0;
663 port
->ip_tempowner
= 0;
665 port
->ip_guarded
= 0;
666 port
->ip_strict_guard
= 0;
667 port
->ip_immovable_receive
= 0;
668 port
->ip_no_grant
= 0;
669 port
->ip_immovable_send
= 0;
670 port
->ip_impcount
= 0;
672 port
->ip_specialreply
= 0;
673 port
->ip_sync_link_state
= PORT_SYNC_LINK_ANY
;
674 port
->ip_sync_bootstrap_checkin
= 0;
675 port
->ip_watchport_elem
= NULL
;
677 ipc_special_reply_port_bits_reset(port
);
679 port
->ip_send_turnstile
= TURNSTILE_NULL
;
681 ipc_mqueue_init(&port
->ip_messages
,
686 * Routine: ipc_port_alloc
690 * Nothing locked. If successful, the port is returned
691 * locked. (The caller doesn't have a reference.)
693 * KERN_SUCCESS The port is allocated.
694 * KERN_INVALID_TASK The space is dead.
695 * KERN_NO_SPACE No room for an entry in the space.
696 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
702 bool make_send_right
,
703 mach_port_name_t
*namep
,
707 mach_port_name_t name
;
709 mach_port_type_t type
= MACH_PORT_TYPE_RECEIVE
;
710 mach_port_urefs_t urefs
= 0;
713 uintptr_t buf
[IP_CALLSTACK_MAX
];
714 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
715 #endif /* MACH_ASSERT */
717 if (make_send_right
) {
718 type
|= MACH_PORT_TYPE_SEND
;
721 kr
= ipc_object_alloc(space
, IOT_PORT
, type
, urefs
,
722 &name
, (ipc_object_t
*) &port
);
723 if (kr
!= KERN_SUCCESS
) {
727 /* port and space are locked */
728 ipc_port_init(port
, space
, name
);
730 if (make_send_right
) {
731 /* ipc_object_alloc() already made the entry reference */
737 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
738 #endif /* MACH_ASSERT */
740 /* unlock space after init */
741 is_write_unlock(space
);
750 * Routine: ipc_port_alloc_name
752 * Allocate a port, with a specific name.
754 * Nothing locked. If successful, the port is returned
755 * locked. (The caller doesn't have a reference.)
757 * KERN_SUCCESS The port is allocated.
758 * KERN_INVALID_TASK The space is dead.
759 * KERN_NAME_EXISTS The name already denotes a right.
760 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
766 mach_port_name_t name
,
773 uintptr_t buf
[IP_CALLSTACK_MAX
];
774 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
775 #endif /* MACH_ASSERT */
777 kr
= ipc_object_alloc_name(space
, IOT_PORT
,
778 MACH_PORT_TYPE_RECEIVE
, 0,
779 name
, (ipc_object_t
*) &port
);
780 if (kr
!= KERN_SUCCESS
) {
786 ipc_port_init(port
, space
, name
);
789 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
790 #endif /* MACH_ASSERT */
798 * Routine: ipc_port_spnotify
800 * Generate send-possible port notifications.
802 * Nothing locked, reference held on port.
808 ipc_port_request_index_t index
= 0;
809 ipc_table_elems_t size
= 0;
812 * If the port has no send-possible request
813 * armed, don't bother to lock the port.
815 if (port
->ip_sprequests
== 0) {
821 #if IMPORTANCE_INHERITANCE
822 if (port
->ip_spimportant
!= 0) {
823 port
->ip_spimportant
= 0;
824 if (ipc_port_importance_delta(port
, IPID_OPTION_NORMAL
, -1) == TRUE
) {
828 #endif /* IMPORTANCE_INHERITANCE */
830 if (port
->ip_sprequests
== 0) {
834 port
->ip_sprequests
= 0;
837 if (ip_active(port
)) {
838 ipc_port_request_t requests
;
840 /* table may change each time port unlocked (reload) */
841 requests
= port
->ip_requests
;
842 assert(requests
!= IPR_NULL
);
845 * no need to go beyond table size when first
846 * we entered - those are future notifications.
849 size
= requests
->ipr_size
->its_size
;
852 /* no need to backtrack either */
853 while (++index
< size
) {
854 ipc_port_request_t ipr
= &requests
[index
];
855 mach_port_name_t name
= ipr
->ipr_name
;
856 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
857 boolean_t armed
= IPR_SOR_SPARMED(ipr
->ipr_soright
);
859 if (MACH_PORT_VALID(name
) && armed
&& IP_VALID(soright
)) {
860 /* claim send-once right - slot still inuse */
861 ipr
->ipr_soright
= IP_NULL
;
864 ipc_notify_send_possible(soright
, name
);
876 * Routine: ipc_port_dnnotify
878 * Generate dead name notifications for
879 * all outstanding dead-name and send-
883 * Port must be inactive.
884 * Reference held on port.
890 ipc_port_request_t requests
= port
->ip_requests
;
892 assert(!ip_active(port
));
893 if (requests
!= IPR_NULL
) {
894 ipc_table_size_t its
= requests
->ipr_size
;
895 ipc_table_elems_t size
= its
->its_size
;
896 ipc_port_request_index_t index
;
897 for (index
= 1; index
< size
; index
++) {
898 ipc_port_request_t ipr
= &requests
[index
];
899 mach_port_name_t name
= ipr
->ipr_name
;
900 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
902 if (MACH_PORT_VALID(name
) && IP_VALID(soright
)) {
903 ipc_notify_dead_name(soright
, name
);
911 * Routine: ipc_port_destroy
913 * Destroys a port. Cleans up queued messages.
915 * If the port has a backup, it doesn't get destroyed,
916 * but is sent in a port-destroyed notification to the backup.
918 * The port is locked and alive; nothing else locked.
919 * The caller has a reference, which is consumed.
920 * Afterwards, the port is unlocked and dead.
924 ipc_port_destroy(ipc_port_t port
)
926 ipc_port_t pdrequest
, nsrequest
;
929 boolean_t special_reply
= port
->ip_specialreply
;
930 struct task_watchport_elem
*watchport_elem
= NULL
;
932 #if IMPORTANCE_INHERITANCE
933 ipc_importance_task_t release_imp_task
= IIT_NULL
;
934 thread_t self
= current_thread();
935 boolean_t top
= (self
->ith_assertions
== 0);
936 natural_t assertcnt
= 0;
937 #endif /* IMPORTANCE_INHERITANCE */
939 require_ip_active(port
);
940 /* port->ip_receiver_name is garbage */
941 /* port->ip_receiver/port->ip_destination is garbage */
943 /* clear any reply-port context */
944 port
->ip_reply_context
= 0;
946 /* check for a backup port */
947 pdrequest
= port
->ip_pdrequest
;
949 #if IMPORTANCE_INHERITANCE
950 /* determine how many assertions to drop and from whom */
951 if (port
->ip_tempowner
!= 0) {
953 release_imp_task
= port
->ip_imp_task
;
954 if (IIT_NULL
!= release_imp_task
) {
955 port
->ip_imp_task
= IIT_NULL
;
956 assertcnt
= port
->ip_impcount
;
958 /* Otherwise, nothing to drop */
960 assertcnt
= port
->ip_impcount
;
961 if (pdrequest
!= IP_NULL
) {
962 /* mark in limbo for the journey */
963 port
->ip_tempowner
= 1;
968 self
->ith_assertions
= assertcnt
;
970 #endif /* IMPORTANCE_INHERITANCE */
972 if (pdrequest
!= IP_NULL
) {
973 /* clear receiver, don't destroy the port */
974 (void)ipc_port_clear_receiver(port
, FALSE
);
975 assert(port
->ip_in_pset
== 0);
976 assert(port
->ip_mscount
== 0);
978 /* we assume the ref for pdrequest */
979 port
->ip_pdrequest
= IP_NULL
;
981 imq_lock(&port
->ip_messages
);
982 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
983 ipc_port_send_turnstile_recompute_push_locked(port
);
984 /* mqueue and port unlocked */
987 ipc_port_adjust_special_reply_port(port
,
988 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
, FALSE
);
991 if (watchport_elem
) {
992 task_watchport_elem_deallocate(watchport_elem
);
993 watchport_elem
= NULL
;
995 /* consumes our refs for port and pdrequest */
996 ipc_notify_port_destroyed(pdrequest
, port
);
998 goto drop_assertions
;
1002 * The mach_msg_* paths don't hold a port lock, they only hold a
1003 * reference to the port object. If a thread raced us and is now
1004 * blocked waiting for message reception on this mqueue (or waiting
1005 * for ipc_mqueue_full), it will never be woken up. We call
1006 * ipc_port_clear_receiver() here, _after_ the port has been marked
1007 * inactive, to wakeup any threads which may be blocked and ensure
1008 * that no other thread can get lost waiting for a wake up on a
1009 * port/mqueue that's been destroyed.
1011 boolean_t reap_msgs
= FALSE
;
1012 reap_msgs
= ipc_port_clear_receiver(port
, TRUE
); /* marks port and mqueue inactive */
1013 assert(port
->ip_in_pset
== 0);
1014 assert(port
->ip_mscount
== 0);
1016 imq_lock(&port
->ip_messages
);
1017 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1018 imq_unlock(&port
->ip_messages
);
1019 nsrequest
= port
->ip_nsrequest
;
1022 * If the port has a preallocated message buffer and that buffer
1023 * is not inuse, free it. If it has an inuse one, then the kmsg
1024 * free will detect that we freed the association and it can free it
1025 * like a normal buffer.
1027 * Once the port is marked inactive we don't need to keep it locked.
1029 if (IP_PREALLOC(port
)) {
1030 ipc_port_t inuse_port
;
1032 kmsg
= port
->ip_premsg
;
1033 assert(kmsg
!= IKM_NULL
);
1034 inuse_port
= ikm_prealloc_inuse_port(kmsg
);
1035 ipc_kmsg_clear_prealloc(kmsg
, port
);
1037 imq_lock(&port
->ip_messages
);
1038 ipc_port_send_turnstile_recompute_push_locked(port
);
1039 /* mqueue and port unlocked */
1041 if (inuse_port
!= IP_NULL
) {
1042 assert(inuse_port
== port
);
1044 ipc_kmsg_free(kmsg
);
1047 imq_lock(&port
->ip_messages
);
1048 ipc_port_send_turnstile_recompute_push_locked(port
);
1049 /* mqueue and port unlocked */
1052 /* Deallocate the watchport element */
1053 if (watchport_elem
) {
1054 task_watchport_elem_deallocate(watchport_elem
);
1055 watchport_elem
= NULL
;
1058 /* unlink the kmsg from special reply port */
1059 if (special_reply
) {
1060 ipc_port_adjust_special_reply_port(port
,
1061 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
, FALSE
);
1064 /* throw away no-senders request */
1065 if (nsrequest
!= IP_NULL
) {
1066 ipc_notify_send_once(nsrequest
); /* consumes ref */
1069 * Reap any kmsg objects waiting to be destroyed.
1070 * This must be done after we've released the port lock.
1073 ipc_kmsg_reap_delayed();
1076 mqueue
= &port
->ip_messages
;
1078 /* cleanup waitq related resources */
1079 ipc_mqueue_deinit(mqueue
);
1081 /* generate dead-name notifications */
1082 ipc_port_dnnotify(port
);
1084 ipc_kobject_destroy(port
);
1086 ip_release(port
); /* consume caller's ref */
1089 #if IMPORTANCE_INHERITANCE
1090 if (release_imp_task
!= IIT_NULL
) {
1091 if (assertcnt
> 0) {
1093 self
->ith_assertions
= 0;
1094 assert(ipc_importance_task_is_any_receiver_type(release_imp_task
));
1095 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1097 ipc_importance_task_release(release_imp_task
);
1098 } else if (assertcnt
> 0) {
1100 self
->ith_assertions
= 0;
1101 release_imp_task
= current_task()->task_imp_base
;
1102 if (ipc_importance_task_is_any_receiver_type(release_imp_task
)) {
1103 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1107 #endif /* IMPORTANCE_INHERITANCE */
1111 * Routine: ipc_port_check_circularity
1113 * Check if queueing "port" in a message for "dest"
1114 * would create a circular group of ports and messages.
1116 * If no circularity (FALSE returned), then "port"
1117 * is changed from "in limbo" to "in transit".
1119 * That is, we want to set port->ip_destination == dest,
1120 * but guaranteeing that this doesn't create a circle
1121 * port->ip_destination->ip_destination->... == port
1124 * No ports locked. References held for "port" and "dest".
1128 ipc_port_check_circularity(
1132 #if IMPORTANCE_INHERITANCE
1133 /* adjust importance counts at the same time */
1134 return ipc_importance_check_circularity(port
, dest
);
1137 struct task_watchport_elem
*watchport_elem
= NULL
;
1139 assert(port
!= IP_NULL
);
1140 assert(dest
!= IP_NULL
);
1147 /* Check if destination needs a turnstile */
1148 ipc_port_send_turnstile_prepare(dest
);
1151 * First try a quick check that can run in parallel.
1152 * No circularity if dest is not in transit.
1155 if (ip_lock_try(dest
)) {
1156 if (!ip_active(dest
) ||
1157 (dest
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1158 (dest
->ip_destination
== IP_NULL
)) {
1162 /* dest is in transit; further checking necessary */
1168 ipc_port_multiple_lock(); /* massive serialization */
1171 * Search for the end of the chain (a port not in transit),
1172 * acquiring locks along the way.
1178 if (!ip_active(base
) ||
1179 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1180 (base
->ip_destination
== IP_NULL
)) {
1184 base
= base
->ip_destination
;
1187 /* all ports in chain from dest to base, inclusive, are locked */
1190 /* circularity detected! */
1192 ipc_port_multiple_unlock();
1194 /* port (== base) is in limbo */
1195 require_ip_active(port
);
1196 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1197 assert(port
->ip_destination
== IP_NULL
);
1200 while (base
!= IP_NULL
) {
1203 /* dest is in transit or in limbo */
1204 require_ip_active(base
);
1205 assert(base
->ip_receiver_name
== MACH_PORT_NULL
);
1207 next
= base
->ip_destination
;
1212 ipc_port_send_turnstile_complete(dest
);
1217 * The guarantee: lock port while the entire chain is locked.
1218 * Once port is locked, we can take a reference to dest,
1219 * add port to the chain, and unlock everything.
1223 ipc_port_multiple_unlock();
1226 imq_lock(&port
->ip_messages
);
1228 /* port is in limbo */
1229 require_ip_active(port
);
1230 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1231 assert(port
->ip_destination
== IP_NULL
);
1233 /* Clear the watchport boost */
1234 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1236 /* Check if the port is being enqueued as a part of sync bootstrap checkin */
1237 if (dest
->ip_specialreply
&& dest
->ip_sync_bootstrap_checkin
) {
1238 port
->ip_sync_bootstrap_checkin
= 1;
1242 port
->ip_destination
= dest
;
1244 /* Setup linkage for source port if it has sync ipc push */
1245 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
1246 if (port_send_turnstile(port
)) {
1247 send_turnstile
= turnstile_prepare((uintptr_t)port
,
1248 port_send_turnstile_address(port
),
1249 TURNSTILE_NULL
, TURNSTILE_SYNC_IPC
);
1252 * What ipc_port_adjust_port_locked would do,
1253 * but we need to also drop even more locks before
1254 * calling turnstile_update_inheritor_complete().
1256 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1258 turnstile_update_inheritor(send_turnstile
, port_send_turnstile(dest
),
1259 (TURNSTILE_INHERITOR_TURNSTILE
| TURNSTILE_IMMEDIATE_UPDATE
));
1261 /* update complete and turnstile complete called after dropping all locks */
1263 imq_unlock(&port
->ip_messages
);
1265 /* now unlock chain */
1276 /* port is in transit */
1277 require_ip_active(dest
);
1278 assert(dest
->ip_receiver_name
== MACH_PORT_NULL
);
1279 assert(dest
->ip_destination
!= IP_NULL
);
1281 next
= dest
->ip_destination
;
1286 /* base is not in transit */
1287 assert(!ip_active(base
) ||
1288 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1289 (base
->ip_destination
== IP_NULL
));
1293 /* All locks dropped, call turnstile_update_inheritor_complete for source port's turnstile */
1294 if (send_turnstile
) {
1295 turnstile_update_inheritor_complete(send_turnstile
, TURNSTILE_INTERLOCK_NOT_HELD
);
1297 /* Take the mq lock to call turnstile complete */
1298 imq_lock(&port
->ip_messages
);
1299 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
), NULL
, TURNSTILE_SYNC_IPC
);
1300 send_turnstile
= TURNSTILE_NULL
;
1301 imq_unlock(&port
->ip_messages
);
1302 turnstile_cleanup();
1305 if (watchport_elem
) {
1306 task_watchport_elem_deallocate(watchport_elem
);
1310 #endif /* !IMPORTANCE_INHERITANCE */
1314 * Update the recv turnstile inheritor for a port.
1316 * Sync IPC through the port receive turnstile only happens for the special
1317 * reply port case. It has three sub-cases:
1319 * 1. a send-once right is in transit, and pushes on the send turnstile of its
1320 * destination mqueue.
1322 * 2. a send-once right has been stashed on a knote it was copied out "through",
1323 * as the first such copied out port.
1325 * 3. a send-once right has been stashed on a knote it was copied out "through",
1326 * as the second or more copied out port.
1329 ipc_port_recv_update_inheritor(
1331 struct turnstile
*rcv_turnstile
,
1332 turnstile_update_flags_t flags
)
1334 struct turnstile
*inheritor
= TURNSTILE_NULL
;
1337 if (ip_active(port
) && port
->ip_specialreply
) {
1338 imq_held(&port
->ip_messages
);
1340 switch (port
->ip_sync_link_state
) {
1341 case PORT_SYNC_LINK_PORT
:
1342 if (port
->ip_sync_inheritor_port
!= NULL
) {
1343 inheritor
= port_send_turnstile(port
->ip_sync_inheritor_port
);
1347 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1348 kn
= port
->ip_sync_inheritor_knote
;
1349 inheritor
= filt_ipc_kqueue_turnstile(kn
);
1352 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1353 inheritor
= port
->ip_sync_inheritor_ts
;
1358 turnstile_update_inheritor(rcv_turnstile
, inheritor
,
1359 flags
| TURNSTILE_INHERITOR_TURNSTILE
);
1363 * Update the send turnstile inheritor for a port.
1365 * Sync IPC through the port send turnstile has 7 possible reasons to be linked:
1367 * 1. a special reply port is part of sync ipc for bootstrap checkin and needs
1368 * to push on thread doing the sync ipc.
1370 * 2. a receive right is in transit, and pushes on the send turnstile of its
1371 * destination mqueue.
1373 * 3. port was passed as an exec watchport and port is pushing on main thread
1376 * 4. a receive right has been stashed on a knote it was copied out "through",
1377 * as the first such copied out port (same as PORT_SYNC_LINK_WORKLOOP_KNOTE
1378 * for the special reply port)
1380 * 5. a receive right has been stashed on a knote it was copied out "through",
1381 * as the second or more copied out port (same as
1382 * PORT_SYNC_LINK_WORKLOOP_STASH for the special reply port)
1384 * 6. a receive right has been copied out as a part of sync bootstrap checkin
1385 * and needs to push on thread doing the sync bootstrap checkin.
1387 * 7. the receive right is monitored by a knote, and pushes on any that is
1388 * registered on a workloop. filt_machport makes sure that if such a knote
1389 * exists, it is kept as the first item in the knote list, so we never need
1393 ipc_port_send_update_inheritor(
1395 struct turnstile
*send_turnstile
,
1396 turnstile_update_flags_t flags
)
1398 ipc_mqueue_t mqueue
= &port
->ip_messages
;
1399 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1401 turnstile_update_flags_t inheritor_flags
= TURNSTILE_INHERITOR_TURNSTILE
;
1403 assert(imq_held(mqueue
));
1405 if (!ip_active(port
)) {
1406 /* this port is no longer active, it should not push anywhere */
1407 } else if (port
->ip_specialreply
) {
1409 if (port
->ip_sync_bootstrap_checkin
&& prioritize_launch
) {
1410 inheritor
= port
->ip_messages
.imq_srp_owner_thread
;
1411 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1413 } else if (port
->ip_receiver_name
== MACH_PORT_NULL
&&
1414 port
->ip_destination
!= NULL
) {
1416 inheritor
= port_send_turnstile(port
->ip_destination
);
1417 } else if (port
->ip_watchport_elem
!= NULL
) {
1419 if (prioritize_launch
) {
1420 assert(port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
);
1421 inheritor
= ipc_port_get_watchport_inheritor(port
);
1422 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1424 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_WORKLOOP_KNOTE
) {
1426 inheritor
= filt_ipc_kqueue_turnstile(mqueue
->imq_inheritor_knote
);
1427 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_WORKLOOP_STASH
) {
1429 inheritor
= mqueue
->imq_inheritor_turnstile
;
1430 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_RCV_THREAD
) {
1432 if (prioritize_launch
) {
1433 inheritor
= port
->ip_messages
.imq_inheritor_thread_ref
;
1434 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1436 } else if ((kn
= SLIST_FIRST(&mqueue
->imq_klist
))) {
1437 /* Case 7. Push on a workloop that is interested */
1438 if (filt_machport_kqueue_has_turnstile(kn
)) {
1439 assert(port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
);
1440 inheritor
= filt_ipc_kqueue_turnstile(kn
);
1444 turnstile_update_inheritor(send_turnstile
, inheritor
,
1445 flags
| inheritor_flags
);
1449 * Routine: ipc_port_send_turnstile_prepare
1451 * Get a reference on port's send turnstile, if
1452 * port does not have a send turnstile then allocate one.
1455 * Nothing is locked.
1458 ipc_port_send_turnstile_prepare(ipc_port_t port
)
1460 struct turnstile
*turnstile
= TURNSTILE_NULL
;
1461 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
1464 imq_lock(&port
->ip_messages
);
1466 if (port_send_turnstile(port
) == NULL
||
1467 port_send_turnstile(port
)->ts_port_ref
== 0) {
1468 if (turnstile
== TURNSTILE_NULL
) {
1469 imq_unlock(&port
->ip_messages
);
1470 turnstile
= turnstile_alloc();
1474 send_turnstile
= turnstile_prepare((uintptr_t)port
,
1475 port_send_turnstile_address(port
),
1476 turnstile
, TURNSTILE_SYNC_IPC
);
1477 turnstile
= TURNSTILE_NULL
;
1479 ipc_port_send_update_inheritor(port
, send_turnstile
,
1480 TURNSTILE_IMMEDIATE_UPDATE
);
1482 /* turnstile complete will be called in ipc_port_send_turnstile_complete */
1485 /* Increment turnstile counter */
1486 port_send_turnstile(port
)->ts_port_ref
++;
1487 imq_unlock(&port
->ip_messages
);
1489 if (send_turnstile
) {
1490 turnstile_update_inheritor_complete(send_turnstile
,
1491 TURNSTILE_INTERLOCK_NOT_HELD
);
1493 if (turnstile
!= TURNSTILE_NULL
) {
1494 turnstile_deallocate(turnstile
);
1500 * Routine: ipc_port_send_turnstile_complete
1502 * Drop a ref on the port's send turnstile, if the
1503 * ref becomes zero, deallocate the turnstile.
1506 * The space might be locked, use safe deallocate.
1509 ipc_port_send_turnstile_complete(ipc_port_t port
)
1511 struct turnstile
*turnstile
= TURNSTILE_NULL
;
1513 /* Drop turnstile count on dest port */
1514 imq_lock(&port
->ip_messages
);
1516 port_send_turnstile(port
)->ts_port_ref
--;
1517 if (port_send_turnstile(port
)->ts_port_ref
== 0) {
1518 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
),
1519 &turnstile
, TURNSTILE_SYNC_IPC
);
1520 assert(turnstile
!= TURNSTILE_NULL
);
1522 imq_unlock(&port
->ip_messages
);
1523 turnstile_cleanup();
1525 if (turnstile
!= TURNSTILE_NULL
) {
1526 turnstile_deallocate_safe(turnstile
);
1527 turnstile
= TURNSTILE_NULL
;
1532 * Routine: ipc_port_rcv_turnstile
1534 * Get the port's receive turnstile
1537 * mqueue locked or thread waiting on turnstile is locked.
1539 static struct turnstile
*
1540 ipc_port_rcv_turnstile(ipc_port_t port
)
1542 return turnstile_lookup_by_proprietor((uintptr_t)port
, TURNSTILE_SYNC_IPC
);
1547 * Routine: ipc_port_rcv_turnstile_waitq
1549 * Given the mqueue's waitq, find the port's
1550 * rcv turnstile and return its waitq.
1553 * mqueue locked or thread waiting on turnstile is locked.
1556 ipc_port_rcv_turnstile_waitq(struct waitq
*waitq
)
1558 struct waitq
*safeq
;
1560 ipc_mqueue_t mqueue
= imq_from_waitq(waitq
);
1561 ipc_port_t port
= ip_from_mq(mqueue
);
1562 struct turnstile
*rcv_turnstile
= ipc_port_rcv_turnstile(port
);
1564 /* Check if the port has a rcv turnstile */
1565 if (rcv_turnstile
!= TURNSTILE_NULL
) {
1566 safeq
= &rcv_turnstile
->ts_waitq
;
1568 safeq
= global_eventq(waitq
);
1575 * Routine: ipc_port_link_special_reply_port
1577 * Link the special reply port with the destination port.
1578 * Allocates turnstile to dest port.
1581 * Nothing is locked.
1584 ipc_port_link_special_reply_port(
1585 ipc_port_t special_reply_port
,
1586 ipc_port_t dest_port
,
1587 boolean_t sync_bootstrap_checkin
)
1589 boolean_t drop_turnstile_ref
= FALSE
;
1591 /* Check if dest_port needs a turnstile */
1592 ipc_port_send_turnstile_prepare(dest_port
);
1594 /* Lock the special reply port and establish the linkage */
1595 ip_lock(special_reply_port
);
1596 imq_lock(&special_reply_port
->ip_messages
);
1598 if (sync_bootstrap_checkin
&& special_reply_port
->ip_specialreply
) {
1599 special_reply_port
->ip_sync_bootstrap_checkin
= 1;
1602 /* Check if we need to drop the acquired turnstile ref on dest port */
1603 if (!special_reply_port
->ip_specialreply
||
1604 special_reply_port
->ip_sync_link_state
!= PORT_SYNC_LINK_ANY
||
1605 special_reply_port
->ip_sync_inheritor_port
!= IPC_PORT_NULL
) {
1606 drop_turnstile_ref
= TRUE
;
1608 /* take a reference on dest_port */
1609 ip_reference(dest_port
);
1610 special_reply_port
->ip_sync_inheritor_port
= dest_port
;
1611 special_reply_port
->ip_sync_link_state
= PORT_SYNC_LINK_PORT
;
1614 imq_unlock(&special_reply_port
->ip_messages
);
1615 ip_unlock(special_reply_port
);
1617 if (drop_turnstile_ref
) {
1618 ipc_port_send_turnstile_complete(dest_port
);
1624 #if DEVELOPMENT || DEBUG
1626 ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port
)
1628 special_reply_port
->ip_srp_lost_link
= 0;
1629 special_reply_port
->ip_srp_msg_sent
= 0;
1633 ipc_special_reply_port_msg_sent_reset(ipc_port_t special_reply_port
)
1635 if (special_reply_port
->ip_specialreply
== 1) {
1636 special_reply_port
->ip_srp_msg_sent
= 0;
1641 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port
)
1643 if (special_reply_port
->ip_specialreply
== 1) {
1644 special_reply_port
->ip_srp_msg_sent
= 1;
1649 ipc_special_reply_port_lost_link(ipc_port_t special_reply_port
)
1651 if (special_reply_port
->ip_specialreply
== 1 && special_reply_port
->ip_srp_msg_sent
== 0) {
1652 special_reply_port
->ip_srp_lost_link
= 1;
1656 #else /* DEVELOPMENT || DEBUG */
1658 ipc_special_reply_port_bits_reset(__unused ipc_port_t special_reply_port
)
1664 ipc_special_reply_port_msg_sent_reset(__unused ipc_port_t special_reply_port
)
1670 ipc_special_reply_port_msg_sent(__unused ipc_port_t special_reply_port
)
1676 ipc_special_reply_port_lost_link(__unused ipc_port_t special_reply_port
)
1680 #endif /* DEVELOPMENT || DEBUG */
1683 * Routine: ipc_port_adjust_special_reply_port_locked
1685 * If the special port has a turnstile, update its inheritor.
1687 * Special reply port locked on entry.
1688 * Special reply port unlocked on return.
1689 * The passed in port is a special reply port.
1694 ipc_port_adjust_special_reply_port_locked(
1695 ipc_port_t special_reply_port
,
1698 boolean_t get_turnstile
)
1700 ipc_port_t dest_port
= IPC_PORT_NULL
;
1701 int sync_link_state
= PORT_SYNC_LINK_NO_LINKAGE
;
1702 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1703 struct turnstile
*ts
= TURNSTILE_NULL
;
1705 assert(special_reply_port
->ip_specialreply
);
1707 ip_lock_held(special_reply_port
); // ip_sync_link_state is touched
1708 imq_lock(&special_reply_port
->ip_messages
);
1710 if (flags
& IPC_PORT_ADJUST_SR_RECEIVED_MSG
) {
1711 ipc_special_reply_port_msg_sent_reset(special_reply_port
);
1714 if (flags
& IPC_PORT_ADJUST_UNLINK_THREAD
) {
1715 special_reply_port
->ip_messages
.imq_srp_owner_thread
= NULL
;
1718 if (flags
& IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN
) {
1719 special_reply_port
->ip_sync_bootstrap_checkin
= 0;
1722 /* Check if the special reply port is marked non-special */
1723 if (special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
) {
1724 if (get_turnstile
) {
1725 turnstile_complete((uintptr_t)special_reply_port
,
1726 port_rcv_turnstile_address(special_reply_port
), NULL
, TURNSTILE_SYNC_IPC
);
1728 imq_unlock(&special_reply_port
->ip_messages
);
1729 ip_unlock(special_reply_port
);
1730 if (get_turnstile
) {
1731 turnstile_cleanup();
1736 if (flags
& IPC_PORT_ADJUST_SR_LINK_WORKLOOP
) {
1737 if (ITH_KNOTE_VALID(kn
, MACH_MSG_TYPE_PORT_SEND_ONCE
)) {
1738 inheritor
= filt_machport_stash_port(kn
, special_reply_port
,
1741 } else if (flags
& IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
) {
1742 sync_link_state
= PORT_SYNC_LINK_ANY
;
1745 /* Check if need to break linkage */
1746 if (!get_turnstile
&& sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
&&
1747 special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
) {
1748 imq_unlock(&special_reply_port
->ip_messages
);
1749 ip_unlock(special_reply_port
);
1753 switch (special_reply_port
->ip_sync_link_state
) {
1754 case PORT_SYNC_LINK_PORT
:
1755 dest_port
= special_reply_port
->ip_sync_inheritor_port
;
1756 special_reply_port
->ip_sync_inheritor_port
= IPC_PORT_NULL
;
1758 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1759 special_reply_port
->ip_sync_inheritor_knote
= NULL
;
1761 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1762 special_reply_port
->ip_sync_inheritor_ts
= NULL
;
1766 special_reply_port
->ip_sync_link_state
= sync_link_state
;
1768 switch (sync_link_state
) {
1769 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1770 special_reply_port
->ip_sync_inheritor_knote
= kn
;
1772 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1773 special_reply_port
->ip_sync_inheritor_ts
= inheritor
;
1775 case PORT_SYNC_LINK_NO_LINKAGE
:
1776 if (flags
& IPC_PORT_ADJUST_SR_ENABLE_EVENT
) {
1777 ipc_special_reply_port_lost_link(special_reply_port
);
1782 /* Get thread's turnstile donated to special reply port */
1783 if (get_turnstile
) {
1784 turnstile_complete((uintptr_t)special_reply_port
,
1785 port_rcv_turnstile_address(special_reply_port
), NULL
, TURNSTILE_SYNC_IPC
);
1787 ts
= ipc_port_rcv_turnstile(special_reply_port
);
1789 turnstile_reference(ts
);
1790 ipc_port_recv_update_inheritor(special_reply_port
, ts
,
1791 TURNSTILE_IMMEDIATE_UPDATE
);
1795 imq_unlock(&special_reply_port
->ip_messages
);
1796 ip_unlock(special_reply_port
);
1798 if (get_turnstile
) {
1799 turnstile_cleanup();
1801 /* Call turnstile cleanup after dropping the interlock */
1802 turnstile_update_inheritor_complete(ts
, TURNSTILE_INTERLOCK_NOT_HELD
);
1803 turnstile_deallocate_safe(ts
);
1806 /* Release the ref on the dest port and its turnstile */
1808 ipc_port_send_turnstile_complete(dest_port
);
1809 /* release the reference on the dest port */
1810 ip_release(dest_port
);
1815 * Routine: ipc_port_adjust_special_reply_port
1817 * If the special port has a turnstile, update its inheritor.
1824 ipc_port_adjust_special_reply_port(
1825 ipc_port_t special_reply_port
,
1827 boolean_t get_turnstile
)
1829 if (special_reply_port
->ip_specialreply
) {
1830 ip_lock(special_reply_port
);
1831 ipc_port_adjust_special_reply_port_locked(special_reply_port
, NULL
,
1832 flags
, get_turnstile
);
1833 /* special_reply_port unlocked */
1835 if (get_turnstile
) {
1836 assert(current_thread()->turnstile
!= TURNSTILE_NULL
);
1841 * Routine: ipc_port_adjust_sync_link_state_locked
1843 * Update the sync link state of the port and the
1844 * turnstile inheritor.
1846 * Port and mqueue locked on entry.
1847 * Port and mqueue locked on return.
1852 ipc_port_adjust_sync_link_state_locked(
1854 int sync_link_state
,
1855 turnstile_inheritor_t inheritor
)
1857 switch (port
->ip_sync_link_state
) {
1858 case PORT_SYNC_LINK_RCV_THREAD
:
1859 /* deallocate the thread reference for the inheritor */
1860 thread_deallocate_safe(port
->ip_messages
.imq_inheritor_thread_ref
);
1864 klist_init(&port
->ip_messages
.imq_klist
);
1867 switch (sync_link_state
) {
1868 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1869 port
->ip_messages
.imq_inheritor_knote
= inheritor
;
1871 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1872 port
->ip_messages
.imq_inheritor_turnstile
= inheritor
;
1874 case PORT_SYNC_LINK_RCV_THREAD
:
1875 /* The thread could exit without clearing port state, take a thread ref */
1876 thread_reference((thread_t
)inheritor
);
1877 port
->ip_messages
.imq_inheritor_thread_ref
= inheritor
;
1880 klist_init(&port
->ip_messages
.imq_klist
);
1881 sync_link_state
= PORT_SYNC_LINK_ANY
;
1884 port
->ip_sync_link_state
= sync_link_state
;
1889 * Routine: ipc_port_adjust_port_locked
1891 * If the port has a turnstile, update its inheritor.
1893 * Port locked on entry.
1894 * Port unlocked on return.
1899 ipc_port_adjust_port_locked(
1902 boolean_t sync_bootstrap_checkin
)
1904 int sync_link_state
= PORT_SYNC_LINK_ANY
;
1905 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1907 ip_lock_held(port
); // ip_sync_link_state is touched
1908 imq_held(&port
->ip_messages
);
1910 assert(!port
->ip_specialreply
);
1913 inheritor
= filt_machport_stash_port(kn
, port
, &sync_link_state
);
1914 if (sync_link_state
== PORT_SYNC_LINK_WORKLOOP_KNOTE
) {
1917 } else if (sync_bootstrap_checkin
) {
1918 inheritor
= current_thread();
1919 sync_link_state
= PORT_SYNC_LINK_RCV_THREAD
;
1922 ipc_port_adjust_sync_link_state_locked(port
, sync_link_state
, inheritor
);
1923 port
->ip_sync_bootstrap_checkin
= 0;
1925 ipc_port_send_turnstile_recompute_push_locked(port
);
1926 /* port and mqueue unlocked */
1930 * Routine: ipc_port_clear_sync_rcv_thread_boost_locked
1932 * If the port is pushing on rcv thread, clear it.
1934 * Port locked on entry
1935 * mqueue is not locked.
1936 * Port unlocked on return.
1941 ipc_port_clear_sync_rcv_thread_boost_locked(
1944 ip_lock_held(port
); // ip_sync_link_state is touched
1946 if (port
->ip_sync_link_state
!= PORT_SYNC_LINK_RCV_THREAD
) {
1951 imq_lock(&port
->ip_messages
);
1952 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1954 ipc_port_send_turnstile_recompute_push_locked(port
);
1955 /* port and mqueue unlocked */
1959 * Routine: ipc_port_add_watchport_elem_locked
1961 * Transfer the turnstile boost of watchport to task calling exec.
1963 * Port locked on entry.
1964 * Port unlocked on return.
1966 * KERN_SUCESS on success.
1967 * KERN_FAILURE otherwise.
1970 ipc_port_add_watchport_elem_locked(
1972 struct task_watchport_elem
*watchport_elem
,
1973 struct task_watchport_elem
**old_elem
)
1976 imq_held(&port
->ip_messages
);
1978 /* Watchport boost only works for non-special active ports mapped in an ipc space */
1979 if (!ip_active(port
) || port
->ip_specialreply
||
1980 port
->ip_receiver_name
== MACH_PORT_NULL
) {
1981 imq_unlock(&port
->ip_messages
);
1983 return KERN_FAILURE
;
1986 if (port
->ip_sync_link_state
!= PORT_SYNC_LINK_ANY
) {
1987 /* Sever the linkage if the port was pushing on knote */
1988 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1991 *old_elem
= port
->ip_watchport_elem
;
1992 port
->ip_watchport_elem
= watchport_elem
;
1994 ipc_port_send_turnstile_recompute_push_locked(port
);
1995 /* port and mqueue unlocked */
1996 return KERN_SUCCESS
;
2000 * Routine: ipc_port_clear_watchport_elem_internal_conditional_locked
2002 * Remove the turnstile boost of watchport and recompute the push.
2004 * Port locked on entry.
2005 * Port unlocked on return.
2007 * KERN_SUCESS on success.
2008 * KERN_FAILURE otherwise.
2011 ipc_port_clear_watchport_elem_internal_conditional_locked(
2013 struct task_watchport_elem
*watchport_elem
)
2016 imq_held(&port
->ip_messages
);
2018 if (port
->ip_watchport_elem
!= watchport_elem
) {
2019 imq_unlock(&port
->ip_messages
);
2021 return KERN_FAILURE
;
2024 ipc_port_clear_watchport_elem_internal(port
);
2025 ipc_port_send_turnstile_recompute_push_locked(port
);
2026 /* port and mqueue unlocked */
2027 return KERN_SUCCESS
;
2031 * Routine: ipc_port_replace_watchport_elem_conditional_locked
2033 * Replace the turnstile boost of watchport and recompute the push.
2035 * Port locked on entry.
2036 * Port unlocked on return.
2038 * KERN_SUCESS on success.
2039 * KERN_FAILURE otherwise.
2042 ipc_port_replace_watchport_elem_conditional_locked(
2044 struct task_watchport_elem
*old_watchport_elem
,
2045 struct task_watchport_elem
*new_watchport_elem
)
2048 imq_held(&port
->ip_messages
);
2050 if (port
->ip_watchport_elem
!= old_watchport_elem
) {
2051 imq_unlock(&port
->ip_messages
);
2053 return KERN_FAILURE
;
2056 port
->ip_watchport_elem
= new_watchport_elem
;
2057 ipc_port_send_turnstile_recompute_push_locked(port
);
2058 /* port and mqueue unlocked */
2059 return KERN_SUCCESS
;
2063 * Routine: ipc_port_clear_watchport_elem_internal
2065 * Remove the turnstile boost of watchport.
2067 * Port locked on entry.
2068 * Port locked on return.
2070 * Old task_watchport_elem returned.
2072 struct task_watchport_elem
*
2073 ipc_port_clear_watchport_elem_internal(
2076 struct task_watchport_elem
*watchport_elem
;
2079 imq_held(&port
->ip_messages
);
2081 watchport_elem
= port
->ip_watchport_elem
;
2082 port
->ip_watchport_elem
= NULL
;
2084 return watchport_elem
;
2088 * Routine: ipc_port_send_turnstile_recompute_push_locked
2090 * Update send turnstile inheritor of port and recompute the push.
2092 * Port locked on entry.
2093 * Port unlocked on return.
2098 ipc_port_send_turnstile_recompute_push_locked(
2101 struct turnstile
*send_turnstile
= port_send_turnstile(port
);
2102 if (send_turnstile
) {
2103 turnstile_reference(send_turnstile
);
2104 ipc_port_send_update_inheritor(port
, send_turnstile
,
2105 TURNSTILE_IMMEDIATE_UPDATE
);
2107 imq_unlock(&port
->ip_messages
);
2110 if (send_turnstile
) {
2111 turnstile_update_inheritor_complete(send_turnstile
,
2112 TURNSTILE_INTERLOCK_NOT_HELD
);
2113 turnstile_deallocate_safe(send_turnstile
);
2118 * Routine: ipc_port_get_watchport_inheritor
2120 * Returns inheritor for watchport.
2125 * watchport inheritor.
2128 ipc_port_get_watchport_inheritor(
2131 imq_held(&port
->ip_messages
);
2132 return port
->ip_watchport_elem
->twe_task
->watchports
->tw_thread
;
2136 * Routine: ipc_port_impcount_delta
2138 * Adjust only the importance count associated with a port.
2139 * If there are any adjustments to be made to receiver task,
2140 * those are handled elsewhere.
2142 * For now, be defensive during deductions to make sure the
2143 * impcount for the port doesn't underflow zero. This will
2144 * go away when the port boost addition is made atomic (see
2145 * note in ipc_port_importance_delta()).
2147 * The port is referenced and locked.
2148 * Nothing else is locked.
2151 ipc_port_impcount_delta(
2153 mach_port_delta_t delta
,
2154 ipc_port_t __unused base
)
2156 mach_port_delta_t absdelta
;
2158 if (!ip_active(port
)) {
2162 /* adding/doing nothing is easy */
2164 port
->ip_impcount
+= delta
;
2168 absdelta
= 0 - delta
;
2169 if (port
->ip_impcount
>= absdelta
) {
2170 port
->ip_impcount
-= absdelta
;
2174 #if (DEVELOPMENT || DEBUG)
2175 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
2176 task_t target_task
= port
->ip_receiver
->is_task
;
2177 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
2178 const char *target_procname
;
2181 if (target_imp
!= IIT_NULL
) {
2182 target_procname
= target_imp
->iit_procname
;
2183 target_pid
= target_imp
->iit_bsd_pid
;
2185 target_procname
= "unknown";
2188 printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
2189 "dropping %d assertion(s) but port only has %d remaining.\n",
2190 port
->ip_receiver_name
,
2191 target_pid
, target_procname
,
2192 absdelta
, port
->ip_impcount
);
2193 } else if (base
!= IP_NULL
) {
2194 task_t target_task
= base
->ip_receiver
->is_task
;
2195 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
2196 const char *target_procname
;
2199 if (target_imp
!= IIT_NULL
) {
2200 target_procname
= target_imp
->iit_procname
;
2201 target_pid
= target_imp
->iit_bsd_pid
;
2203 target_procname
= "unknown";
2206 printf("Over-release of importance assertions for port 0x%lx "
2207 "enqueued on port 0x%x with receiver pid %d (%s), "
2208 "dropping %d assertion(s) but port only has %d remaining.\n",
2209 (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port
),
2210 base
->ip_receiver_name
,
2211 target_pid
, target_procname
,
2212 absdelta
, port
->ip_impcount
);
2216 delta
= 0 - port
->ip_impcount
;
2217 port
->ip_impcount
= 0;
2222 * Routine: ipc_port_importance_delta_internal
2224 * Adjust the importance count through the given port.
2225 * If the port is in transit, apply the delta throughout
2226 * the chain. Determine if the there is a task at the
2227 * base of the chain that wants/needs to be adjusted,
2228 * and if so, apply the delta.
2230 * The port is referenced and locked on entry.
2231 * Importance may be locked.
2232 * Nothing else is locked.
2233 * The lock may be dropped on exit.
2234 * Returns TRUE if lock was dropped.
2236 #if IMPORTANCE_INHERITANCE
2239 ipc_port_importance_delta_internal(
2242 mach_port_delta_t
*deltap
,
2243 ipc_importance_task_t
*imp_task
)
2245 ipc_port_t next
, base
;
2246 boolean_t dropped
= FALSE
;
2248 *imp_task
= IIT_NULL
;
2254 assert(options
== IPID_OPTION_NORMAL
|| options
== IPID_OPTION_SENDPOSSIBLE
);
2258 /* if port is in transit, have to search for end of chain */
2259 if (ip_active(port
) &&
2260 port
->ip_destination
!= IP_NULL
&&
2261 port
->ip_receiver_name
== MACH_PORT_NULL
) {
2265 ipc_port_multiple_lock(); /* massive serialization */
2268 while (ip_active(base
) &&
2269 base
->ip_destination
!= IP_NULL
&&
2270 base
->ip_receiver_name
== MACH_PORT_NULL
) {
2271 base
= base
->ip_destination
;
2274 ipc_port_multiple_unlock();
2278 * If the port lock is dropped b/c the port is in transit, there is a
2279 * race window where another thread can drain messages and/or fire a
2280 * send possible notification before we get here.
2282 * We solve this race by checking to see if our caller armed the send
2283 * possible notification, whether or not it's been fired yet, and
2284 * whether or not we've already set the port's ip_spimportant bit. If
2285 * we don't need a send-possible boost, then we'll just apply a
2286 * harmless 0-boost to the port.
2288 if (options
& IPID_OPTION_SENDPOSSIBLE
) {
2289 assert(*deltap
== 1);
2290 if (port
->ip_sprequests
&& port
->ip_spimportant
== 0) {
2291 port
->ip_spimportant
= 1;
2297 /* unlock down to the base, adjusting boost(s) at each level */
2299 *deltap
= ipc_port_impcount_delta(port
, *deltap
, base
);
2305 /* port is in transit */
2306 assert(port
->ip_tempowner
== 0);
2307 next
= port
->ip_destination
;
2312 /* find the task (if any) to boost according to the base */
2313 if (ip_active(base
)) {
2314 if (base
->ip_tempowner
!= 0) {
2315 if (IIT_NULL
!= base
->ip_imp_task
) {
2316 *imp_task
= base
->ip_imp_task
;
2318 /* otherwise don't boost */
2319 } else if (base
->ip_receiver_name
!= MACH_PORT_NULL
) {
2320 ipc_space_t space
= base
->ip_receiver
;
2322 /* only spaces with boost-accepting tasks */
2323 if (space
->is_task
!= TASK_NULL
&&
2324 ipc_importance_task_is_any_receiver_type(space
->is_task
->task_imp_base
)) {
2325 *imp_task
= space
->is_task
->task_imp_base
;
2331 * Only the base is locked. If we have to hold or drop task
2332 * importance assertions, we'll have to drop that lock as well.
2334 if (*imp_task
!= IIT_NULL
) {
2335 /* take a reference before unlocking base */
2336 ipc_importance_task_reference(*imp_task
);
2339 if (dropped
== TRUE
) {
2345 #endif /* IMPORTANCE_INHERITANCE */
2348 * Routine: ipc_port_importance_delta
2350 * Adjust the importance count through the given port.
2351 * If the port is in transit, apply the delta throughout
2354 * If there is a task at the base of the chain that wants/needs
2355 * to be adjusted, apply the delta.
2357 * The port is referenced and locked on entry.
2358 * Nothing else is locked.
2359 * The lock may be dropped on exit.
2360 * Returns TRUE if lock was dropped.
2362 #if IMPORTANCE_INHERITANCE
2365 ipc_port_importance_delta(
2368 mach_port_delta_t delta
)
2370 ipc_importance_task_t imp_task
= IIT_NULL
;
2373 dropped
= ipc_port_importance_delta_internal(port
, options
, &delta
, &imp_task
);
2375 if (IIT_NULL
== imp_task
|| delta
== 0) {
2383 assert(ipc_importance_task_is_any_receiver_type(imp_task
));
2386 ipc_importance_task_hold_internal_assertion(imp_task
, delta
);
2388 ipc_importance_task_drop_internal_assertion(imp_task
, -delta
);
2391 ipc_importance_task_release(imp_task
);
2394 #endif /* IMPORTANCE_INHERITANCE */
2397 * Routine: ipc_port_make_send_locked
2399 * Make a naked send right from a receive right.
2402 * port locked and active.
2405 ipc_port_make_send_locked(
2408 require_ip_active(port
);
2416 * Routine: ipc_port_make_send
2418 * Make a naked send right from a receive right.
2425 if (!IP_VALID(port
)) {
2430 if (ip_active(port
)) {
2431 ipc_port_make_send_locked(port
);
2440 * Routine: ipc_port_copy_send_locked
2442 * Make a naked send right from another naked send right.
2444 * port locked and active.
2447 ipc_port_copy_send_locked(
2450 assert(port
->ip_srights
> 0);
2456 * Routine: ipc_port_copy_send
2458 * Make a naked send right from another naked send right.
2459 * IP_NULL -> IP_NULL
2460 * IP_DEAD -> IP_DEAD
2461 * dead port -> IP_DEAD
2462 * live port -> port + ref
2464 * Nothing locked except possibly a space.
2473 if (!IP_VALID(port
)) {
2478 if (ip_active(port
)) {
2479 ipc_port_copy_send_locked(port
);
2490 * Routine: ipc_port_copyout_send
2492 * Copyout a naked send right (possibly null/dead),
2493 * or if that fails, destroy the right.
2499 ipc_port_copyout_send(
2503 mach_port_name_t name
;
2505 if (IP_VALID(sright
)) {
2508 kr
= ipc_object_copyout(space
, ip_to_object(sright
),
2509 MACH_MSG_TYPE_PORT_SEND
, NULL
, NULL
, &name
);
2510 if (kr
!= KERN_SUCCESS
) {
2511 ipc_port_release_send(sright
);
2513 if (kr
== KERN_INVALID_CAPABILITY
) {
2514 name
= MACH_PORT_DEAD
;
2516 name
= MACH_PORT_NULL
;
2520 name
= CAST_MACH_PORT_TO_NAME(sright
);
2527 * Routine: ipc_port_release_send
2529 * Release a naked send right.
2530 * Consumes a ref for the port.
2536 ipc_port_release_send(
2539 ipc_port_t nsrequest
= IP_NULL
;
2540 mach_port_mscount_t mscount
;
2542 if (!IP_VALID(port
)) {
2548 assert(port
->ip_srights
> 0);
2549 if (port
->ip_srights
== 0) {
2550 panic("Over-release of port %p send right!", port
);
2555 if (!ip_active(port
)) {
2561 if (port
->ip_srights
== 0 &&
2562 port
->ip_nsrequest
!= IP_NULL
) {
2563 nsrequest
= port
->ip_nsrequest
;
2564 port
->ip_nsrequest
= IP_NULL
;
2565 mscount
= port
->ip_mscount
;
2568 ipc_notify_no_senders(nsrequest
, mscount
);
2576 * Routine: ipc_port_make_sonce_locked
2578 * Make a naked send-once right from a receive right.
2580 * The port is locked and active.
2584 ipc_port_make_sonce_locked(
2587 require_ip_active(port
);
2588 port
->ip_sorights
++;
2594 * Routine: ipc_port_make_sonce
2596 * Make a naked send-once right from a receive right.
2598 * The port is not locked.
2602 ipc_port_make_sonce(
2605 if (!IP_VALID(port
)) {
2610 if (ip_active(port
)) {
2611 ipc_port_make_sonce_locked(port
);
2620 * Routine: ipc_port_release_sonce
2622 * Release a naked send-once right.
2623 * Consumes a ref for the port.
2625 * In normal situations, this is never used.
2626 * Send-once rights are only consumed when
2627 * a message (possibly a send-once notification)
2630 * Nothing locked except possibly a space.
2634 ipc_port_release_sonce(
2637 if (!IP_VALID(port
)) {
2641 ipc_port_adjust_special_reply_port(port
, IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN
, FALSE
);
2645 assert(port
->ip_sorights
> 0);
2646 if (port
->ip_sorights
== 0) {
2647 panic("Over-release of port %p send-once right!", port
);
2650 port
->ip_sorights
--;
2657 * Routine: ipc_port_release_receive
2659 * Release a naked (in limbo or in transit) receive right.
2660 * Consumes a ref for the port; destroys the port.
2666 ipc_port_release_receive(
2671 if (!IP_VALID(port
)) {
2676 require_ip_active(port
);
2677 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
2678 dest
= port
->ip_destination
;
2680 ipc_port_destroy(port
); /* consumes ref, unlocks */
2682 if (dest
!= IP_NULL
) {
2683 ipc_port_send_turnstile_complete(dest
);
2689 * Routine: ipc_port_alloc_special
2691 * Allocate a port in a special space.
2692 * The new port is returned with one ref.
2693 * If unsuccessful, IP_NULL is returned.
2699 ipc_port_alloc_special(
2704 port
= ip_object_to_port(io_alloc(IOT_PORT
));
2705 if (port
== IP_NULL
) {
2710 uintptr_t buf
[IP_CALLSTACK_MAX
];
2711 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
2712 #endif /* MACH_ASSERT */
2714 bzero((char *)port
, sizeof(*port
));
2715 io_lock_init(ip_to_object(port
));
2716 port
->ip_references
= 1;
2717 port
->ip_object
.io_bits
= io_makebits(TRUE
, IOT_PORT
, 0);
2719 ipc_port_init(port
, space
, 1);
2722 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
2723 #endif /* MACH_ASSERT */
2729 * Routine: ipc_port_dealloc_special
2731 * Deallocate a port in a special space.
2732 * Consumes one ref for the port.
2738 ipc_port_dealloc_special(
2740 __assert_only ipc_space_t space
)
2743 require_ip_active(port
);
2744 // assert(port->ip_receiver_name != MACH_PORT_NULL);
2745 assert(port
->ip_receiver
== space
);
2748 * We clear ip_receiver_name and ip_receiver to simplify
2749 * the ipc_space_kernel check in ipc_mqueue_send.
2752 imq_lock(&port
->ip_messages
);
2753 port
->ip_receiver_name
= MACH_PORT_NULL
;
2754 port
->ip_receiver
= IS_NULL
;
2755 imq_unlock(&port
->ip_messages
);
2757 /* relevant part of ipc_port_clear_receiver */
2758 port
->ip_mscount
= 0;
2759 port
->ip_messages
.imq_seqno
= 0;
2761 ipc_port_destroy(port
);
2765 * Routine: ipc_port_finalize
2767 * Called on last reference deallocate to
2768 * free any remaining data associated with the
2777 ipc_port_request_t requests
= port
->ip_requests
;
2779 assert(port_send_turnstile(port
) == TURNSTILE_NULL
);
2780 assert(ipc_port_rcv_turnstile(port
) == TURNSTILE_NULL
);
2782 if (ip_active(port
)) {
2783 panic("Trying to free an active port. port %p", port
);
2786 if (requests
!= IPR_NULL
) {
2787 ipc_table_size_t its
= requests
->ipr_size
;
2788 it_requests_free(its
, requests
);
2789 port
->ip_requests
= IPR_NULL
;
2792 ipc_mqueue_deinit(&port
->ip_messages
);
2795 ipc_port_track_dealloc(port
);
2796 #endif /* MACH_ASSERT */
2800 * Routine: kdp_mqueue_send_find_owner
2802 * Discover the owner of the ipc_mqueue that contains the input
2803 * waitq object. The thread blocked on the waitq should be
2804 * waiting for an IPC_MQUEUE_FULL event.
2806 * The 'waitinfo->wait_type' value should already be set to
2807 * kThreadWaitPortSend.
2809 * If we find out that the containing port is actually in
2810 * transit, we reset the wait_type field to reflect this.
2813 kdp_mqueue_send_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
2815 struct turnstile
*turnstile
;
2816 assert(waitinfo
->wait_type
== kThreadWaitPortSend
);
2817 assert(event
== IPC_MQUEUE_FULL
);
2818 assert(waitq_is_turnstile_queue(waitq
));
2820 turnstile
= waitq_to_turnstile(waitq
);
2821 ipc_port_t port
= (ipc_port_t
)turnstile
->ts_proprietor
; /* we are blocking on send */
2822 assert(kdp_is_in_zone(port
, "ipc ports"));
2824 waitinfo
->owner
= 0;
2825 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
2826 if (ip_lock_held_kdp(port
)) {
2828 * someone has the port locked: it may be in an
2829 * inconsistent state: bail
2831 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
2835 if (ip_active(port
)) {
2836 if (port
->ip_tempowner
) {
2837 if (port
->ip_imp_task
!= IIT_NULL
&& port
->ip_imp_task
->iit_task
!= NULL
) {
2838 /* port is held by a tempowner */
2839 waitinfo
->owner
= pid_from_task(port
->ip_imp_task
->iit_task
);
2841 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
2843 } else if (port
->ip_receiver_name
) {
2844 /* port in a space */
2845 if (port
->ip_receiver
== ipc_space_kernel
) {
2847 * The kernel pid is 0, make this
2848 * distinguishable from no-owner and
2849 * inconsistent port state.
2851 waitinfo
->owner
= STACKSHOT_WAITOWNER_KERNEL
;
2853 waitinfo
->owner
= pid_from_task(port
->ip_receiver
->is_task
);
2855 } else if (port
->ip_destination
!= IP_NULL
) {
2856 /* port in transit */
2857 waitinfo
->wait_type
= kThreadWaitPortSendInTransit
;
2858 waitinfo
->owner
= VM_KERNEL_UNSLIDE_OR_PERM(port
->ip_destination
);
2864 * Routine: kdp_mqueue_recv_find_owner
2866 * Discover the "owner" of the ipc_mqueue that contains the input
2867 * waitq object. The thread blocked on the waitq is trying to
2868 * receive on the mqueue.
2870 * The 'waitinfo->wait_type' value should already be set to
2871 * kThreadWaitPortReceive.
2873 * If we find that we are actualy waiting on a port set, we reset
2874 * the wait_type field to reflect this.
2877 kdp_mqueue_recv_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
2879 assert(waitinfo
->wait_type
== kThreadWaitPortReceive
);
2880 assert(event
== IPC_MQUEUE_RECEIVE
);
2882 ipc_mqueue_t mqueue
= imq_from_waitq(waitq
);
2883 waitinfo
->owner
= 0;
2884 if (imq_is_set(mqueue
)) { /* we are waiting on a port set */
2885 ipc_pset_t set
= ips_from_mq(mqueue
);
2886 assert(kdp_is_in_zone(set
, "ipc port sets"));
2888 /* Reset wait type to specify waiting on port set receive */
2889 waitinfo
->wait_type
= kThreadWaitPortSetReceive
;
2890 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(set
);
2891 if (ips_lock_held_kdp(set
)) {
2892 waitinfo
->owner
= STACKSHOT_WAITOWNER_PSET_LOCKED
;
2894 /* There is no specific owner "at the other end" of a port set, so leave unset. */
2896 ipc_port_t port
= ip_from_mq(mqueue
);
2897 assert(kdp_is_in_zone(port
, "ipc ports"));
2899 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
2900 if (ip_lock_held_kdp(port
)) {
2901 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
2905 if (ip_active(port
)) {
2906 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
2907 waitinfo
->owner
= port
->ip_receiver_name
;
2909 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
2916 #include <kern/machine.h>
2919 * Keep a list of all allocated ports.
2920 * Allocation is intercepted via ipc_port_init;
2921 * deallocation is intercepted via io_free.
2924 queue_head_t port_alloc_queue
;
2925 lck_spin_t port_alloc_queue_lock
;
2928 unsigned long port_count
= 0;
2929 unsigned long port_count_warning
= 20000;
2930 unsigned long port_timestamp
= 0;
2932 void db_port_stack_trace(
2937 unsigned int verbose
,
2938 unsigned int display
,
2939 unsigned int ref_search
,
2940 unsigned int ref_target
);
2943 * Initialize global state needed for run-time
2947 ipc_port_debug_init(void)
2950 queue_init(&port_alloc_queue
);
2951 lck_spin_init(&port_alloc_queue_lock
, &ipc_lck_grp
, &ipc_lck_attr
);
2954 if (!PE_parse_boot_argn("ipc_portbt", &ipc_portbt
, sizeof(ipc_portbt
))) {
2960 extern int proc_pid(struct proc
*);
2961 #endif /* MACH_BSD */
2964 * Initialize all of the debugging state in a port.
2965 * Insert the port into a global list of all allocated ports.
2968 ipc_port_init_debug(
2970 uintptr_t *callstack
,
2971 unsigned int callstack_max
)
2975 port
->ip_thread
= current_thread();
2976 port
->ip_timetrack
= port_timestamp
++;
2977 for (i
= 0; i
< callstack_max
; ++i
) {
2978 port
->ip_callstack
[i
] = callstack
[i
];
2980 for (i
= 0; i
< IP_NSPARES
; ++i
) {
2981 port
->ip_spares
[i
] = 0;
2985 task_t task
= current_task();
2986 if (task
!= TASK_NULL
) {
2987 struct proc
* proc
= (struct proc
*) get_bsdtask_info(task
);
2989 port
->ip_spares
[0] = proc_pid(proc
);
2992 #endif /* MACH_BSD */
2995 lck_spin_lock(&port_alloc_queue_lock
);
2997 if (port_count_warning
> 0 && port_count
>= port_count_warning
) {
2998 assert(port_count
< port_count_warning
);
3000 queue_enter(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
3001 lck_spin_unlock(&port_alloc_queue_lock
);
3006 * Routine: ipc_port_callstack_init_debug
3008 * Calls the machine-dependent routine to
3009 * fill in an array with up to IP_CALLSTACK_MAX
3010 * levels of return pc information
3012 * May block (via copyin)
3015 ipc_port_callstack_init_debug(
3016 uintptr_t *callstack
,
3017 unsigned int callstack_max
)
3021 /* guarantee the callstack is initialized */
3022 for (i
= 0; i
< callstack_max
; i
++) {
3027 machine_callstack(callstack
, callstack_max
);
3032 * Remove a port from the queue of allocated ports.
3033 * This routine should be invoked JUST prior to
3034 * deallocating the actual memory occupied by the port.
3038 ipc_port_track_dealloc(
3039 __unused ipc_port_t port
)
3044 ipc_port_track_dealloc(
3047 lck_spin_lock(&port_alloc_queue_lock
);
3048 assert(port_count
> 0);
3050 queue_remove(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
3051 lck_spin_unlock(&port_alloc_queue_lock
);
3056 #endif /* MACH_ASSERT */