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 <mach_assert.h>
74 #include <mach/port.h>
75 #include <mach/kern_return.h>
76 #include <kern/ipc_kobject.h>
77 #include <kern/thread.h>
78 #include <kern/misc_protos.h>
79 #include <kern/waitq.h>
80 #include <kern/policy_internal.h>
81 #include <kern/debug.h>
82 #include <kern/kcdata.h>
83 #include <ipc/ipc_entry.h>
84 #include <ipc/ipc_space.h>
85 #include <ipc/ipc_object.h>
86 #include <ipc/ipc_port.h>
87 #include <ipc/ipc_pset.h>
88 #include <ipc/ipc_kmsg.h>
89 #include <ipc/ipc_mqueue.h>
90 #include <ipc/ipc_notify.h>
91 #include <ipc/ipc_table.h>
92 #include <ipc/ipc_importance.h>
93 #include <machine/limits.h>
94 #include <kern/turnstile.h>
95 #include <kern/machine.h>
97 #include <security/mac_mach_internal.h>
101 static TUNABLE(bool, prioritize_launch
, "prioritize_launch", true);
102 TUNABLE_WRITEABLE(int, ipc_portbt
, "ipc_portbt", false);
104 LCK_SPIN_DECLARE_ATTR(ipc_port_multiple_lock_data
, &ipc_lck_grp
, &ipc_lck_attr
);
105 ipc_port_timestamp_t ipc_port_timestamp_data
;
108 void ipc_port_init_debug(
110 uintptr_t *callstack
,
111 unsigned int callstack_max
);
113 void ipc_port_callstack_init_debug(
114 uintptr_t *callstack
,
115 unsigned int callstack_max
);
117 #endif /* MACH_ASSERT */
120 ipc_port_send_turnstile_recompute_push_locked(
124 ipc_port_get_watchport_inheritor(
128 ipc_port_release(ipc_port_t port
)
134 ipc_port_reference(ipc_port_t port
)
140 * Routine: ipc_port_timestamp
142 * Retrieve a timestamp value.
146 ipc_port_timestamp(void)
148 return OSIncrementAtomic(&ipc_port_timestamp_data
);
152 * Routine: ipc_port_request_alloc
154 * Try to allocate a request slot.
155 * If successful, returns the request index.
156 * Otherwise returns zero.
158 * The port is locked and active.
160 * KERN_SUCCESS A request index was found.
161 * KERN_NO_SPACE No index allocated.
164 #if IMPORTANCE_INHERITANCE
166 ipc_port_request_alloc(
168 mach_port_name_t name
,
170 boolean_t send_possible
,
172 ipc_port_request_index_t
*indexp
,
173 boolean_t
*importantp
)
176 ipc_port_request_alloc(
178 mach_port_name_t name
,
180 boolean_t send_possible
,
182 ipc_port_request_index_t
*indexp
)
183 #endif /* IMPORTANCE_INHERITANCE */
185 ipc_port_request_t ipr
, table
;
186 ipc_port_request_index_t index
;
189 #if IMPORTANCE_INHERITANCE
191 #endif /* IMPORTANCE_INHERITANCE */
193 require_ip_active(port
);
194 assert(name
!= MACH_PORT_NULL
);
195 assert(soright
!= IP_NULL
);
197 table
= port
->ip_requests
;
199 if (table
== IPR_NULL
) {
200 return KERN_NO_SPACE
;
203 index
= table
->ipr_next
;
205 return KERN_NO_SPACE
;
209 assert(ipr
->ipr_name
== MACH_PORT_NULL
);
211 table
->ipr_next
= ipr
->ipr_next
;
212 ipr
->ipr_name
= name
;
215 mask
|= IPR_SOR_SPREQ_MASK
;
217 mask
|= IPR_SOR_SPARM_MASK
;
218 if (port
->ip_sprequests
== 0) {
219 port
->ip_sprequests
= 1;
220 #if IMPORTANCE_INHERITANCE
221 /* TODO: Live importance support in send-possible */
222 if (port
->ip_impdonation
!= 0 &&
223 port
->ip_spimportant
== 0 &&
224 (task_is_importance_donor(current_task()))) {
227 #endif /* IMPORTANCE_INHERTANCE */
231 ipr
->ipr_soright
= IPR_SOR_MAKE(soright
, mask
);
239 * Routine: ipc_port_request_grow
241 * Grow a port's table of requests.
243 * The port must be locked and active.
244 * Nothing else locked; will allocate memory.
245 * Upon return the port is unlocked.
247 * KERN_SUCCESS Grew the table.
248 * KERN_SUCCESS Somebody else grew the table.
249 * KERN_SUCCESS The port died.
250 * KERN_RESOURCE_SHORTAGE Couldn't allocate new table.
251 * KERN_NO_SPACE Couldn't grow to desired size
255 ipc_port_request_grow(
257 ipc_table_elems_t target_size
)
259 ipc_table_size_t its
;
260 ipc_port_request_t otable
, ntable
;
261 require_ip_active(port
);
263 otable
= port
->ip_requests
;
264 if (otable
== IPR_NULL
) {
265 its
= &ipc_table_requests
[0];
267 its
= otable
->ipr_size
+ 1;
270 if (target_size
!= ITS_SIZE_NONE
) {
271 if ((otable
!= IPR_NULL
) &&
272 (target_size
<= otable
->ipr_size
->its_size
)) {
276 while ((its
->its_size
) && (its
->its_size
< target_size
)) {
279 if (its
->its_size
== 0) {
281 return KERN_NO_SPACE
;
288 if ((its
->its_size
== 0) ||
289 ((ntable
= it_requests_alloc(its
)) == IPR_NULL
)) {
291 return KERN_RESOURCE_SHORTAGE
;
297 * Check that port is still active and that nobody else
298 * has slipped in and grown the table on us. Note that
299 * just checking if the current table pointer == otable
300 * isn't sufficient; must check ipr_size.
303 if (ip_active(port
) && (port
->ip_requests
== otable
) &&
304 ((otable
== IPR_NULL
) || (otable
->ipr_size
+ 1 == its
))) {
305 ipc_table_size_t oits
;
306 ipc_table_elems_t osize
, nsize
;
307 ipc_port_request_index_t free
, i
;
309 /* copy old table to new table */
311 if (otable
!= IPR_NULL
) {
312 oits
= otable
->ipr_size
;
313 osize
= oits
->its_size
;
314 free
= otable
->ipr_next
;
316 (void) memcpy((void *)(ntable
+ 1),
317 (const void *)(otable
+ 1),
318 (osize
- 1) * sizeof(struct ipc_port_request
));
325 nsize
= its
->its_size
;
326 assert(nsize
> osize
);
328 /* add new elements to the new table's free list */
330 for (i
= osize
; i
< nsize
; i
++) {
331 ipc_port_request_t ipr
= &ntable
[i
];
333 ipr
->ipr_name
= MACH_PORT_NULL
;
334 ipr
->ipr_next
= free
;
338 ntable
->ipr_next
= free
;
339 ntable
->ipr_size
= its
;
340 port
->ip_requests
= ntable
;
344 if (otable
!= IPR_NULL
) {
345 it_requests_free(oits
, otable
);
350 it_requests_free(its
, ntable
);
357 * Routine: ipc_port_request_sparm
359 * Arm delayed send-possible request.
361 * The port must be locked and active.
363 * Returns TRUE if the request was armed
364 * (or armed with importance in that version).
368 ipc_port_request_sparm(
370 __assert_only mach_port_name_t name
,
371 ipc_port_request_index_t index
,
372 mach_msg_option_t option
,
373 mach_msg_priority_t priority
)
375 if (index
!= IE_REQ_NONE
) {
376 ipc_port_request_t ipr
, table
;
378 require_ip_active(port
);
380 table
= port
->ip_requests
;
381 assert(table
!= IPR_NULL
);
384 assert(ipr
->ipr_name
== name
);
386 /* Is there a valid destination? */
387 if (IPR_SOR_SPREQ(ipr
->ipr_soright
)) {
388 ipr
->ipr_soright
= IPR_SOR_MAKE(ipr
->ipr_soright
, IPR_SOR_SPARM_MASK
);
389 port
->ip_sprequests
= 1;
391 if (option
& MACH_SEND_OVERRIDE
) {
392 /* apply override to message queue */
393 mach_msg_qos_t qos_ovr
;
394 if (mach_msg_priority_is_pthread_priority(priority
)) {
395 qos_ovr
= _pthread_priority_thread_qos(priority
);
397 qos_ovr
= mach_msg_priority_overide_qos(priority
);
400 ipc_mqueue_override_send(&port
->ip_messages
, qos_ovr
);
404 #if IMPORTANCE_INHERITANCE
405 if (((option
& MACH_SEND_NOIMPORTANCE
) == 0) &&
406 (port
->ip_impdonation
!= 0) &&
407 (port
->ip_spimportant
== 0) &&
408 (((option
& MACH_SEND_IMPORTANCE
) != 0) ||
409 (task_is_importance_donor(current_task())))) {
414 #endif /* IMPORTANCE_INHERITANCE */
421 * Routine: ipc_port_request_type
423 * Determine the type(s) of port requests enabled for a name.
425 * The port must be locked or inactive (to avoid table growth).
426 * The index must not be IE_REQ_NONE and for the name in question.
429 ipc_port_request_type(
431 __assert_only mach_port_name_t name
,
432 ipc_port_request_index_t index
)
434 ipc_port_request_t ipr
, table
;
435 mach_port_type_t type
= 0;
437 table
= port
->ip_requests
;
438 assert(table
!= IPR_NULL
);
440 assert(index
!= IE_REQ_NONE
);
442 assert(ipr
->ipr_name
== name
);
444 if (IP_VALID(IPR_SOR_PORT(ipr
->ipr_soright
))) {
445 type
|= MACH_PORT_TYPE_DNREQUEST
;
447 if (IPR_SOR_SPREQ(ipr
->ipr_soright
)) {
448 type
|= MACH_PORT_TYPE_SPREQUEST
;
450 if (!IPR_SOR_SPARMED(ipr
->ipr_soright
)) {
451 type
|= MACH_PORT_TYPE_SPREQUEST_DELAYED
;
459 * Routine: ipc_port_request_cancel
461 * Cancel a dead-name/send-possible request and return the send-once right.
463 * The port must be locked and active.
464 * The index must not be IPR_REQ_NONE and must correspond with name.
468 ipc_port_request_cancel(
470 __assert_only mach_port_name_t name
,
471 ipc_port_request_index_t index
)
473 ipc_port_request_t ipr
, table
;
474 ipc_port_t request
= IP_NULL
;
476 require_ip_active(port
);
477 table
= port
->ip_requests
;
478 assert(table
!= IPR_NULL
);
480 assert(index
!= IE_REQ_NONE
);
482 assert(ipr
->ipr_name
== name
);
483 request
= IPR_SOR_PORT(ipr
->ipr_soright
);
485 /* return ipr to the free list inside the table */
486 ipr
->ipr_name
= MACH_PORT_NULL
;
487 ipr
->ipr_next
= table
->ipr_next
;
488 table
->ipr_next
= index
;
494 * Routine: ipc_port_pdrequest
496 * Make a port-deleted request, returning the
497 * previously registered send-once right.
498 * Just cancels the previous request if notify is IP_NULL.
500 * The port is locked and active. It is unlocked.
501 * Consumes a ref for notify (if non-null), and
502 * returns previous with a ref (if non-null).
509 ipc_port_t
*previousp
)
512 require_ip_active(port
);
514 previous
= port
->ip_pdrequest
;
515 port
->ip_pdrequest
= notify
;
518 *previousp
= previous
;
522 * Routine: ipc_port_nsrequest
524 * Make a no-senders request, returning the
525 * previously registered send-once right.
526 * Just cancels the previous request if notify is IP_NULL.
528 * The port is locked and active. It is unlocked.
529 * Consumes a ref for notify (if non-null), and
530 * returns previous with a ref (if non-null).
536 mach_port_mscount_t sync
,
538 ipc_port_t
*previousp
)
541 mach_port_mscount_t mscount
;
542 require_ip_active(port
);
544 previous
= port
->ip_nsrequest
;
545 mscount
= port
->ip_mscount
;
547 if ((port
->ip_srights
== 0) && (sync
<= mscount
) &&
548 (notify
!= IP_NULL
)) {
549 port
->ip_nsrequest
= IP_NULL
;
551 ipc_notify_no_senders(notify
, mscount
);
553 port
->ip_nsrequest
= notify
;
557 *previousp
= previous
;
562 * Routine: ipc_port_clear_receiver
564 * Prepares a receive right for transmission/destruction,
565 * optionally performs mqueue destruction (with port lock held)
568 * The port is locked and active.
570 * If should_destroy is TRUE, then the return value indicates
571 * whether the caller needs to reap kmsg structures that should
572 * be destroyed (by calling ipc_kmsg_reap_delayed)
574 * If should_destroy is FALSE, this always returns FALSE
578 ipc_port_clear_receiver(
580 boolean_t should_destroy
)
582 ipc_mqueue_t mqueue
= &port
->ip_messages
;
583 boolean_t reap_messages
= FALSE
;
586 * Pull ourselves out of any sets to which we belong.
587 * We hold the port locked, so even though this acquires and releases
588 * the mqueue lock, we know we won't be added to any other sets.
590 if (port
->ip_in_pset
!= 0) {
591 ipc_pset_remove_from_all(port
);
592 assert(port
->ip_in_pset
== 0);
596 * Send anyone waiting on the port's queue directly away.
597 * Also clear the mscount, seqno, guard bits
600 if (port
->ip_receiver_name
) {
601 ipc_mqueue_changed(port
->ip_receiver
, mqueue
);
603 ipc_mqueue_changed(NULL
, mqueue
);
605 port
->ip_mscount
= 0;
606 mqueue
->imq_seqno
= 0;
607 port
->ip_context
= port
->ip_guarded
= port
->ip_strict_guard
= 0;
609 * clear the immovable bit so the port can move back to anyone listening
610 * for the port destroy notification
612 port
->ip_immovable_receive
= 0;
614 if (should_destroy
) {
616 * Mark the port and mqueue invalid, preventing further send/receive
617 * operations from succeeding. It's important for this to be
618 * done under the same lock hold as the ipc_mqueue_changed
619 * call to avoid additional threads blocking on an mqueue
620 * that's being destroyed.
622 * The port active bit needs to be guarded under mqueue lock for
625 port
->ip_object
.io_bits
&= ~IO_BITS_ACTIVE
;
626 port
->ip_timestamp
= ipc_port_timestamp();
627 reap_messages
= ipc_mqueue_destroy_locked(mqueue
);
629 /* make port be in limbo */
630 port
->ip_receiver_name
= MACH_PORT_NULL
;
631 port
->ip_destination
= IP_NULL
;
634 imq_unlock(&port
->ip_messages
);
636 return reap_messages
;
640 * Routine: ipc_port_init
642 * Initializes a newly-allocated port.
643 * Doesn't touch the ip_object fields.
650 ipc_port_init_flags_t flags
,
651 mach_port_name_t name
)
653 /* port->ip_kobject doesn't have to be initialized */
655 port
->ip_receiver
= space
;
656 port
->ip_receiver_name
= name
;
658 port
->ip_mscount
= 0;
659 port
->ip_srights
= 0;
660 port
->ip_sorights
= 0;
661 if (flags
& IPC_PORT_INIT_MAKE_SEND_RIGHT
) {
662 port
->ip_srights
= 1;
663 port
->ip_mscount
= 1;
666 port
->ip_nsrequest
= IP_NULL
;
667 port
->ip_pdrequest
= IP_NULL
;
668 port
->ip_requests
= IPR_NULL
;
670 port
->ip_premsg
= IKM_NULL
;
671 port
->ip_context
= 0;
672 port
->ip_reply_context
= 0;
674 port
->ip_sprequests
= 0;
675 port
->ip_spimportant
= 0;
676 port
->ip_impdonation
= 0;
677 port
->ip_tempowner
= 0;
679 port
->ip_guarded
= 0;
680 port
->ip_strict_guard
= 0;
681 port
->ip_immovable_receive
= 0;
682 port
->ip_no_grant
= 0;
683 port
->ip_immovable_send
= 0;
684 port
->ip_impcount
= 0;
686 if (flags
& IPC_PORT_INIT_FILTER_MESSAGE
) {
687 port
->ip_object
.io_bits
|= IP_BIT_FILTER_MSG
;
690 port
->ip_tg_block_tracking
= (flags
& IPC_PORT_INIT_TG_BLOCK_TRACKING
) != 0;
691 port
->ip_specialreply
= (flags
& IPC_PORT_INIT_SPECIAL_REPLY
) != 0;
692 port
->ip_sync_link_state
= PORT_SYNC_LINK_ANY
;
693 port
->ip_sync_bootstrap_checkin
= 0;
695 ipc_special_reply_port_bits_reset(port
);
697 port
->ip_send_turnstile
= TURNSTILE_NULL
;
699 ipc_mqueue_kind_t kind
= IPC_MQUEUE_KIND_NONE
;
700 if (flags
& IPC_PORT_INIT_MESSAGE_QUEUE
) {
701 kind
= IPC_MQUEUE_KIND_PORT
;
703 ipc_mqueue_init(&port
->ip_messages
, kind
);
707 * Routine: ipc_port_alloc
711 * Nothing locked. If successful, the port is returned
712 * locked. (The caller doesn't have a reference.)
714 * KERN_SUCCESS The port is allocated.
715 * KERN_INVALID_TASK The space is dead.
716 * KERN_NO_SPACE No room for an entry in the space.
717 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
723 ipc_port_init_flags_t flags
,
724 mach_port_name_t
*namep
,
728 mach_port_name_t name
;
730 mach_port_type_t type
= MACH_PORT_TYPE_RECEIVE
;
731 mach_port_urefs_t urefs
= 0;
734 uintptr_t buf
[IP_CALLSTACK_MAX
];
735 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
736 #endif /* MACH_ASSERT */
738 if (flags
& IPC_PORT_INIT_MAKE_SEND_RIGHT
) {
739 type
|= MACH_PORT_TYPE_SEND
;
742 kr
= ipc_object_alloc(space
, IOT_PORT
, type
, urefs
,
743 &name
, (ipc_object_t
*) &port
);
744 if (kr
!= KERN_SUCCESS
) {
748 /* port and space are locked */
749 ipc_port_init(port
, space
, flags
, name
);
752 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
753 #endif /* MACH_ASSERT */
755 /* unlock space after init */
756 is_write_unlock(space
);
765 * Routine: ipc_port_alloc_name
767 * Allocate a port, with a specific name.
769 * Nothing locked. If successful, the port is returned
770 * locked. (The caller doesn't have a reference.)
772 * KERN_SUCCESS The port is allocated.
773 * KERN_INVALID_TASK The space is dead.
774 * KERN_NAME_EXISTS The name already denotes a right.
775 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
781 ipc_port_init_flags_t flags
,
782 mach_port_name_t name
,
787 mach_port_type_t type
= MACH_PORT_TYPE_RECEIVE
;
788 mach_port_urefs_t urefs
= 0;
791 uintptr_t buf
[IP_CALLSTACK_MAX
];
792 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
793 #endif /* MACH_ASSERT */
795 if (flags
& IPC_PORT_INIT_MAKE_SEND_RIGHT
) {
796 type
|= MACH_PORT_TYPE_SEND
;
799 kr
= ipc_object_alloc_name(space
, IOT_PORT
, type
, urefs
,
800 name
, (ipc_object_t
*) &port
);
801 if (kr
!= KERN_SUCCESS
) {
807 ipc_port_init(port
, space
, flags
, name
);
810 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
811 #endif /* MACH_ASSERT */
819 * Routine: ipc_port_spnotify
821 * Generate send-possible port notifications.
823 * Nothing locked, reference held on port.
829 ipc_port_request_index_t index
= 0;
830 ipc_table_elems_t size
= 0;
833 * If the port has no send-possible request
834 * armed, don't bother to lock the port.
836 if (port
->ip_sprequests
== 0) {
842 #if IMPORTANCE_INHERITANCE
843 if (port
->ip_spimportant
!= 0) {
844 port
->ip_spimportant
= 0;
845 if (ipc_port_importance_delta(port
, IPID_OPTION_NORMAL
, -1) == TRUE
) {
849 #endif /* IMPORTANCE_INHERITANCE */
851 if (port
->ip_sprequests
== 0) {
855 port
->ip_sprequests
= 0;
858 if (ip_active(port
)) {
859 ipc_port_request_t requests
;
861 /* table may change each time port unlocked (reload) */
862 requests
= port
->ip_requests
;
863 assert(requests
!= IPR_NULL
);
866 * no need to go beyond table size when first
867 * we entered - those are future notifications.
870 size
= requests
->ipr_size
->its_size
;
873 /* no need to backtrack either */
874 while (++index
< size
) {
875 ipc_port_request_t ipr
= &requests
[index
];
876 mach_port_name_t name
= ipr
->ipr_name
;
877 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
878 boolean_t armed
= IPR_SOR_SPARMED(ipr
->ipr_soright
);
880 if (MACH_PORT_VALID(name
) && armed
&& IP_VALID(soright
)) {
881 /* claim send-once right - slot still inuse */
882 ipr
->ipr_soright
= IP_NULL
;
885 ipc_notify_send_possible(soright
, name
);
897 * Routine: ipc_port_dnnotify
899 * Generate dead name notifications for
900 * all outstanding dead-name and send-
904 * Port must be inactive.
905 * Reference held on port.
911 ipc_port_request_t requests
= port
->ip_requests
;
913 assert(!ip_active(port
));
914 if (requests
!= IPR_NULL
) {
915 ipc_table_size_t its
= requests
->ipr_size
;
916 ipc_table_elems_t size
= its
->its_size
;
917 ipc_port_request_index_t index
;
918 for (index
= 1; index
< size
; index
++) {
919 ipc_port_request_t ipr
= &requests
[index
];
920 mach_port_name_t name
= ipr
->ipr_name
;
921 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
923 if (MACH_PORT_VALID(name
) && IP_VALID(soright
)) {
924 ipc_notify_dead_name(soright
, name
);
932 * Routine: ipc_port_destroy
934 * Destroys a port. Cleans up queued messages.
936 * If the port has a backup, it doesn't get destroyed,
937 * but is sent in a port-destroyed notification to the backup.
939 * The port is locked and alive; nothing else locked.
940 * The caller has a reference, which is consumed.
941 * Afterwards, the port is unlocked and dead.
945 ipc_port_destroy(ipc_port_t port
)
947 ipc_port_t pdrequest
, nsrequest
;
950 boolean_t special_reply
= port
->ip_specialreply
;
951 struct task_watchport_elem
*watchport_elem
= NULL
;
953 #if IMPORTANCE_INHERITANCE
954 ipc_importance_task_t release_imp_task
= IIT_NULL
;
955 thread_t self
= current_thread();
956 boolean_t top
= (self
->ith_assertions
== 0);
957 natural_t assertcnt
= 0;
958 #endif /* IMPORTANCE_INHERITANCE */
960 require_ip_active(port
);
961 /* port->ip_receiver_name is garbage */
962 /* port->ip_receiver/port->ip_destination is garbage */
964 /* clear any reply-port context */
965 port
->ip_reply_context
= 0;
967 /* check for a backup port */
968 pdrequest
= port
->ip_pdrequest
;
970 #if IMPORTANCE_INHERITANCE
971 /* determine how many assertions to drop and from whom */
972 if (port
->ip_tempowner
!= 0) {
974 release_imp_task
= port
->ip_imp_task
;
975 if (IIT_NULL
!= release_imp_task
) {
976 port
->ip_imp_task
= IIT_NULL
;
977 assertcnt
= port
->ip_impcount
;
979 /* Otherwise, nothing to drop */
981 assertcnt
= port
->ip_impcount
;
982 if (pdrequest
!= IP_NULL
) {
983 /* mark in limbo for the journey */
984 port
->ip_tempowner
= 1;
989 self
->ith_assertions
= assertcnt
;
991 #endif /* IMPORTANCE_INHERITANCE */
993 if (pdrequest
!= IP_NULL
) {
994 /* clear receiver, don't destroy the port */
995 (void)ipc_port_clear_receiver(port
, FALSE
);
996 assert(port
->ip_in_pset
== 0);
997 assert(port
->ip_mscount
== 0);
999 /* we assume the ref for pdrequest */
1000 port
->ip_pdrequest
= IP_NULL
;
1002 imq_lock(&port
->ip_messages
);
1003 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1004 ipc_port_send_turnstile_recompute_push_locked(port
);
1005 /* mqueue and port unlocked */
1007 if (special_reply
) {
1008 ipc_port_adjust_special_reply_port(port
,
1009 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
);
1012 if (watchport_elem
) {
1013 task_watchport_elem_deallocate(watchport_elem
);
1014 watchport_elem
= NULL
;
1016 /* consumes our refs for port and pdrequest */
1017 ipc_notify_port_destroyed(pdrequest
, port
);
1019 goto drop_assertions
;
1023 * The mach_msg_* paths don't hold a port lock, they only hold a
1024 * reference to the port object. If a thread raced us and is now
1025 * blocked waiting for message reception on this mqueue (or waiting
1026 * for ipc_mqueue_full), it will never be woken up. We call
1027 * ipc_port_clear_receiver() here, _after_ the port has been marked
1028 * inactive, to wakeup any threads which may be blocked and ensure
1029 * that no other thread can get lost waiting for a wake up on a
1030 * port/mqueue that's been destroyed.
1032 boolean_t reap_msgs
= FALSE
;
1033 reap_msgs
= ipc_port_clear_receiver(port
, TRUE
); /* marks port and mqueue inactive */
1034 assert(port
->ip_in_pset
== 0);
1035 assert(port
->ip_mscount
== 0);
1037 imq_lock(&port
->ip_messages
);
1038 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1039 imq_unlock(&port
->ip_messages
);
1040 nsrequest
= port
->ip_nsrequest
;
1043 * If the port has a preallocated message buffer and that buffer
1044 * is not inuse, free it. If it has an inuse one, then the kmsg
1045 * free will detect that we freed the association and it can free it
1046 * like a normal buffer.
1048 * Once the port is marked inactive we don't need to keep it locked.
1050 if (IP_PREALLOC(port
)) {
1051 ipc_port_t inuse_port
;
1053 kmsg
= port
->ip_premsg
;
1054 assert(kmsg
!= IKM_NULL
);
1055 inuse_port
= ikm_prealloc_inuse_port(kmsg
);
1056 ipc_kmsg_clear_prealloc(kmsg
, port
);
1058 imq_lock(&port
->ip_messages
);
1059 ipc_port_send_turnstile_recompute_push_locked(port
);
1060 /* mqueue and port unlocked */
1062 if (inuse_port
!= IP_NULL
) {
1063 assert(inuse_port
== port
);
1065 ipc_kmsg_free(kmsg
);
1068 imq_lock(&port
->ip_messages
);
1069 ipc_port_send_turnstile_recompute_push_locked(port
);
1070 /* mqueue and port unlocked */
1073 /* Deallocate the watchport element */
1074 if (watchport_elem
) {
1075 task_watchport_elem_deallocate(watchport_elem
);
1076 watchport_elem
= NULL
;
1079 /* unlink the kmsg from special reply port */
1080 if (special_reply
) {
1081 ipc_port_adjust_special_reply_port(port
,
1082 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
);
1085 /* throw away no-senders request */
1086 if (nsrequest
!= IP_NULL
) {
1087 ipc_notify_send_once(nsrequest
); /* consumes ref */
1090 * Reap any kmsg objects waiting to be destroyed.
1091 * This must be done after we've released the port lock.
1094 ipc_kmsg_reap_delayed();
1097 mqueue
= &port
->ip_messages
;
1099 /* cleanup waitq related resources */
1100 ipc_mqueue_deinit(mqueue
);
1102 /* generate dead-name notifications */
1103 ipc_port_dnnotify(port
);
1105 ipc_kobject_destroy(port
);
1107 ip_release(port
); /* consume caller's ref */
1110 #if IMPORTANCE_INHERITANCE
1111 if (release_imp_task
!= IIT_NULL
) {
1112 if (assertcnt
> 0) {
1114 self
->ith_assertions
= 0;
1115 assert(ipc_importance_task_is_any_receiver_type(release_imp_task
));
1116 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1118 ipc_importance_task_release(release_imp_task
);
1119 } else if (assertcnt
> 0) {
1121 self
->ith_assertions
= 0;
1122 release_imp_task
= current_task()->task_imp_base
;
1123 if (ipc_importance_task_is_any_receiver_type(release_imp_task
)) {
1124 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1128 #endif /* IMPORTANCE_INHERITANCE */
1132 * Routine: ipc_port_check_circularity
1134 * Check if queueing "port" in a message for "dest"
1135 * would create a circular group of ports and messages.
1137 * If no circularity (FALSE returned), then "port"
1138 * is changed from "in limbo" to "in transit".
1140 * That is, we want to set port->ip_destination == dest,
1141 * but guaranteeing that this doesn't create a circle
1142 * port->ip_destination->ip_destination->... == port
1145 * No ports locked. References held for "port" and "dest".
1149 ipc_port_check_circularity(
1153 #if IMPORTANCE_INHERITANCE
1154 /* adjust importance counts at the same time */
1155 return ipc_importance_check_circularity(port
, dest
);
1158 struct task_watchport_elem
*watchport_elem
= NULL
;
1160 assert(port
!= IP_NULL
);
1161 assert(dest
!= IP_NULL
);
1168 /* Check if destination needs a turnstile */
1169 ipc_port_send_turnstile_prepare(dest
);
1172 * First try a quick check that can run in parallel.
1173 * No circularity if dest is not in transit.
1176 if (ip_lock_try(dest
)) {
1177 if (!ip_active(dest
) ||
1178 (dest
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1179 (dest
->ip_destination
== IP_NULL
)) {
1183 /* dest is in transit; further checking necessary */
1189 ipc_port_multiple_lock(); /* massive serialization */
1192 * Search for the end of the chain (a port not in transit),
1193 * acquiring locks along the way.
1199 if (!ip_active(base
) ||
1200 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1201 (base
->ip_destination
== IP_NULL
)) {
1205 base
= base
->ip_destination
;
1208 /* all ports in chain from dest to base, inclusive, are locked */
1211 /* circularity detected! */
1213 ipc_port_multiple_unlock();
1215 /* port (== base) is in limbo */
1216 require_ip_active(port
);
1217 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1218 assert(port
->ip_destination
== IP_NULL
);
1221 while (base
!= IP_NULL
) {
1224 /* dest is in transit or in limbo */
1225 require_ip_active(base
);
1226 assert(base
->ip_receiver_name
== MACH_PORT_NULL
);
1228 next
= base
->ip_destination
;
1233 ipc_port_send_turnstile_complete(dest
);
1238 * The guarantee: lock port while the entire chain is locked.
1239 * Once port is locked, we can take a reference to dest,
1240 * add port to the chain, and unlock everything.
1244 ipc_port_multiple_unlock();
1247 imq_lock(&port
->ip_messages
);
1249 /* port is in limbo */
1250 require_ip_active(port
);
1251 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1252 assert(port
->ip_destination
== IP_NULL
);
1254 /* Clear the watchport boost */
1255 watchport_elem
= ipc_port_clear_watchport_elem_internal(port
);
1257 /* Check if the port is being enqueued as a part of sync bootstrap checkin */
1258 if (dest
->ip_specialreply
&& dest
->ip_sync_bootstrap_checkin
) {
1259 port
->ip_sync_bootstrap_checkin
= 1;
1263 port
->ip_destination
= dest
;
1265 /* Setup linkage for source port if it has sync ipc push */
1266 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
1267 if (port_send_turnstile(port
)) {
1268 send_turnstile
= turnstile_prepare((uintptr_t)port
,
1269 port_send_turnstile_address(port
),
1270 TURNSTILE_NULL
, TURNSTILE_SYNC_IPC
);
1273 * What ipc_port_adjust_port_locked would do,
1274 * but we need to also drop even more locks before
1275 * calling turnstile_update_inheritor_complete().
1277 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
1279 turnstile_update_inheritor(send_turnstile
, port_send_turnstile(dest
),
1280 (TURNSTILE_INHERITOR_TURNSTILE
| TURNSTILE_IMMEDIATE_UPDATE
));
1282 /* update complete and turnstile complete called after dropping all locks */
1284 imq_unlock(&port
->ip_messages
);
1286 /* now unlock chain */
1297 /* port is in transit */
1298 require_ip_active(dest
);
1299 assert(dest
->ip_receiver_name
== MACH_PORT_NULL
);
1300 assert(dest
->ip_destination
!= IP_NULL
);
1302 next
= dest
->ip_destination
;
1307 /* base is not in transit */
1308 assert(!ip_active(base
) ||
1309 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1310 (base
->ip_destination
== IP_NULL
));
1314 /* All locks dropped, call turnstile_update_inheritor_complete for source port's turnstile */
1315 if (send_turnstile
) {
1316 turnstile_update_inheritor_complete(send_turnstile
, TURNSTILE_INTERLOCK_NOT_HELD
);
1318 /* Take the mq lock to call turnstile complete */
1319 imq_lock(&port
->ip_messages
);
1320 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
), NULL
, TURNSTILE_SYNC_IPC
);
1321 send_turnstile
= TURNSTILE_NULL
;
1322 imq_unlock(&port
->ip_messages
);
1323 turnstile_cleanup();
1326 if (watchport_elem
) {
1327 task_watchport_elem_deallocate(watchport_elem
);
1331 #endif /* !IMPORTANCE_INHERITANCE */
1335 * Routine: ipc_port_watchport_elem
1337 * Get the port's watchport elem field
1342 static struct task_watchport_elem
*
1343 ipc_port_watchport_elem(ipc_port_t port
)
1345 return port
->ip_messages
.imq_wait_queue
.waitq_tspriv
;
1349 * Routine: ipc_port_update_watchport_elem
1351 * Set the port's watchport elem field
1356 static inline struct task_watchport_elem
*
1357 ipc_port_update_watchport_elem(ipc_port_t port
, struct task_watchport_elem
*we
)
1359 assert(!port
->ip_specialreply
);
1360 struct task_watchport_elem
*old_we
= ipc_port_watchport_elem(port
);
1361 port
->ip_messages
.imq_wait_queue
.waitq_tspriv
= we
;
1366 * Routine: ipc_special_reply_stash_pid_locked
1368 * Set the pid of process that copied out send once right to special reply port.
1374 ipc_special_reply_stash_pid_locked(ipc_port_t port
, int pid
)
1376 assert(port
->ip_specialreply
);
1377 port
->ip_messages
.imq_wait_queue
.waitq_priv_pid
= pid
;
1382 * Routine: ipc_special_reply_get_pid_locked
1384 * Get the pid of process that copied out send once right to special reply port.
1390 ipc_special_reply_get_pid_locked(ipc_port_t port
)
1392 assert(port
->ip_specialreply
);
1393 return port
->ip_messages
.imq_wait_queue
.waitq_priv_pid
;
1397 * Update the recv turnstile inheritor for a port.
1399 * Sync IPC through the port receive turnstile only happens for the special
1400 * reply port case. It has three sub-cases:
1402 * 1. a send-once right is in transit, and pushes on the send turnstile of its
1403 * destination mqueue.
1405 * 2. a send-once right has been stashed on a knote it was copied out "through",
1406 * as the first such copied out port.
1408 * 3. a send-once right has been stashed on a knote it was copied out "through",
1409 * as the second or more copied out port.
1412 ipc_port_recv_update_inheritor(
1414 struct turnstile
*rcv_turnstile
,
1415 turnstile_update_flags_t flags
)
1417 struct turnstile
*inheritor
= TURNSTILE_NULL
;
1420 if (ip_active(port
) && port
->ip_specialreply
) {
1421 imq_held(&port
->ip_messages
);
1423 switch (port
->ip_sync_link_state
) {
1424 case PORT_SYNC_LINK_PORT
:
1425 if (port
->ip_sync_inheritor_port
!= NULL
) {
1426 inheritor
= port_send_turnstile(port
->ip_sync_inheritor_port
);
1430 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1431 kn
= port
->ip_sync_inheritor_knote
;
1432 inheritor
= filt_ipc_kqueue_turnstile(kn
);
1435 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1436 inheritor
= port
->ip_sync_inheritor_ts
;
1441 turnstile_update_inheritor(rcv_turnstile
, inheritor
,
1442 flags
| TURNSTILE_INHERITOR_TURNSTILE
);
1446 * Update the send turnstile inheritor for a port.
1448 * Sync IPC through the port send turnstile has 7 possible reasons to be linked:
1450 * 1. a special reply port is part of sync ipc for bootstrap checkin and needs
1451 * to push on thread doing the sync ipc.
1453 * 2. a receive right is in transit, and pushes on the send turnstile of its
1454 * destination mqueue.
1456 * 3. port was passed as an exec watchport and port is pushing on main thread
1459 * 4. a receive right has been stashed on a knote it was copied out "through",
1460 * as the first such copied out port (same as PORT_SYNC_LINK_WORKLOOP_KNOTE
1461 * for the special reply port)
1463 * 5. a receive right has been stashed on a knote it was copied out "through",
1464 * as the second or more copied out port (same as
1465 * PORT_SYNC_LINK_WORKLOOP_STASH for the special reply port)
1467 * 6. a receive right has been copied out as a part of sync bootstrap checkin
1468 * and needs to push on thread doing the sync bootstrap checkin.
1470 * 7. the receive right is monitored by a knote, and pushes on any that is
1471 * registered on a workloop. filt_machport makes sure that if such a knote
1472 * exists, it is kept as the first item in the knote list, so we never need
1476 ipc_port_send_update_inheritor(
1478 struct turnstile
*send_turnstile
,
1479 turnstile_update_flags_t flags
)
1481 ipc_mqueue_t mqueue
= &port
->ip_messages
;
1482 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1484 turnstile_update_flags_t inheritor_flags
= TURNSTILE_INHERITOR_TURNSTILE
;
1486 assert(imq_held(mqueue
));
1488 if (!ip_active(port
)) {
1489 /* this port is no longer active, it should not push anywhere */
1490 } else if (port
->ip_specialreply
) {
1492 if (port
->ip_sync_bootstrap_checkin
&& prioritize_launch
) {
1493 inheritor
= port
->ip_messages
.imq_srp_owner_thread
;
1494 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1496 } else if (port
->ip_receiver_name
== MACH_PORT_NULL
&&
1497 port
->ip_destination
!= NULL
) {
1499 inheritor
= port_send_turnstile(port
->ip_destination
);
1500 } else if (ipc_port_watchport_elem(port
) != NULL
) {
1502 if (prioritize_launch
) {
1503 assert(port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
);
1504 inheritor
= ipc_port_get_watchport_inheritor(port
);
1505 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1507 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_WORKLOOP_KNOTE
) {
1509 inheritor
= filt_ipc_kqueue_turnstile(mqueue
->imq_inheritor_knote
);
1510 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_WORKLOOP_STASH
) {
1512 inheritor
= mqueue
->imq_inheritor_turnstile
;
1513 } else if (port
->ip_sync_link_state
== PORT_SYNC_LINK_RCV_THREAD
) {
1515 if (prioritize_launch
) {
1516 inheritor
= port
->ip_messages
.imq_inheritor_thread_ref
;
1517 inheritor_flags
= TURNSTILE_INHERITOR_THREAD
;
1519 } else if ((kn
= SLIST_FIRST(&mqueue
->imq_klist
))) {
1520 /* Case 7. Push on a workloop that is interested */
1521 if (filt_machport_kqueue_has_turnstile(kn
)) {
1522 assert(port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
);
1523 inheritor
= filt_ipc_kqueue_turnstile(kn
);
1527 turnstile_update_inheritor(send_turnstile
, inheritor
,
1528 flags
| inheritor_flags
);
1532 * Routine: ipc_port_send_turnstile_prepare
1534 * Get a reference on port's send turnstile, if
1535 * port does not have a send turnstile then allocate one.
1538 * Nothing is locked.
1541 ipc_port_send_turnstile_prepare(ipc_port_t port
)
1543 struct turnstile
*turnstile
= TURNSTILE_NULL
;
1544 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
1547 imq_lock(&port
->ip_messages
);
1549 if (port_send_turnstile(port
) == NULL
||
1550 port_send_turnstile(port
)->ts_port_ref
== 0) {
1551 if (turnstile
== TURNSTILE_NULL
) {
1552 imq_unlock(&port
->ip_messages
);
1553 turnstile
= turnstile_alloc();
1557 send_turnstile
= turnstile_prepare((uintptr_t)port
,
1558 port_send_turnstile_address(port
),
1559 turnstile
, TURNSTILE_SYNC_IPC
);
1560 turnstile
= TURNSTILE_NULL
;
1562 ipc_port_send_update_inheritor(port
, send_turnstile
,
1563 TURNSTILE_IMMEDIATE_UPDATE
);
1565 /* turnstile complete will be called in ipc_port_send_turnstile_complete */
1568 /* Increment turnstile counter */
1569 port_send_turnstile(port
)->ts_port_ref
++;
1570 imq_unlock(&port
->ip_messages
);
1572 if (send_turnstile
) {
1573 turnstile_update_inheritor_complete(send_turnstile
,
1574 TURNSTILE_INTERLOCK_NOT_HELD
);
1576 if (turnstile
!= TURNSTILE_NULL
) {
1577 turnstile_deallocate(turnstile
);
1583 * Routine: ipc_port_send_turnstile_complete
1585 * Drop a ref on the port's send turnstile, if the
1586 * ref becomes zero, deallocate the turnstile.
1589 * The space might be locked, use safe deallocate.
1592 ipc_port_send_turnstile_complete(ipc_port_t port
)
1594 struct turnstile
*turnstile
= TURNSTILE_NULL
;
1596 /* Drop turnstile count on dest port */
1597 imq_lock(&port
->ip_messages
);
1599 port_send_turnstile(port
)->ts_port_ref
--;
1600 if (port_send_turnstile(port
)->ts_port_ref
== 0) {
1601 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
),
1602 &turnstile
, TURNSTILE_SYNC_IPC
);
1603 assert(turnstile
!= TURNSTILE_NULL
);
1605 imq_unlock(&port
->ip_messages
);
1606 turnstile_cleanup();
1608 if (turnstile
!= TURNSTILE_NULL
) {
1609 turnstile_deallocate_safe(turnstile
);
1610 turnstile
= TURNSTILE_NULL
;
1615 * Routine: ipc_port_rcv_turnstile
1617 * Get the port's receive turnstile
1620 * mqueue locked or thread waiting on turnstile is locked.
1622 static struct turnstile
*
1623 ipc_port_rcv_turnstile(ipc_port_t port
)
1625 return *port_rcv_turnstile_address(port
);
1630 * Routine: ipc_port_link_special_reply_port
1632 * Link the special reply port with the destination port.
1633 * Allocates turnstile to dest port.
1636 * Nothing is locked.
1639 ipc_port_link_special_reply_port(
1640 ipc_port_t special_reply_port
,
1641 ipc_port_t dest_port
,
1642 boolean_t sync_bootstrap_checkin
)
1644 boolean_t drop_turnstile_ref
= FALSE
;
1645 boolean_t special_reply
= FALSE
;
1647 /* Check if dest_port needs a turnstile */
1648 ipc_port_send_turnstile_prepare(dest_port
);
1650 /* Lock the special reply port and establish the linkage */
1651 ip_lock(special_reply_port
);
1652 imq_lock(&special_reply_port
->ip_messages
);
1654 special_reply
= special_reply_port
->ip_specialreply
;
1656 if (sync_bootstrap_checkin
&& special_reply
) {
1657 special_reply_port
->ip_sync_bootstrap_checkin
= 1;
1660 /* Check if we need to drop the acquired turnstile ref on dest port */
1661 if (!special_reply
||
1662 special_reply_port
->ip_sync_link_state
!= PORT_SYNC_LINK_ANY
||
1663 special_reply_port
->ip_sync_inheritor_port
!= IPC_PORT_NULL
) {
1664 drop_turnstile_ref
= TRUE
;
1666 /* take a reference on dest_port */
1667 ip_reference(dest_port
);
1668 special_reply_port
->ip_sync_inheritor_port
= dest_port
;
1669 special_reply_port
->ip_sync_link_state
= PORT_SYNC_LINK_PORT
;
1672 imq_unlock(&special_reply_port
->ip_messages
);
1673 ip_unlock(special_reply_port
);
1675 if (special_reply
) {
1677 * For special reply ports, if the destination port is
1678 * marked with the thread group blocked tracking flag,
1679 * callout to the performance controller.
1681 ipc_port_thread_group_blocked(dest_port
);
1684 if (drop_turnstile_ref
) {
1685 ipc_port_send_turnstile_complete(dest_port
);
1692 * Routine: ipc_port_thread_group_blocked
1694 * Call thread_group_blocked callout if the port
1695 * has ip_tg_block_tracking bit set and the thread
1696 * has not made this callout already.
1699 * Nothing is locked.
1702 ipc_port_thread_group_blocked(ipc_port_t port __unused
)
1704 #if CONFIG_THREAD_GROUPS
1705 bool port_tg_block_tracking
= false;
1706 thread_t self
= current_thread();
1708 if (self
->thread_group
== NULL
||
1709 (self
->options
& TH_OPT_IPC_TG_BLOCKED
)) {
1713 port_tg_block_tracking
= port
->ip_tg_block_tracking
;
1714 if (!port_tg_block_tracking
) {
1718 machine_thread_group_blocked(self
->thread_group
, NULL
,
1719 PERFCONTROL_CALLOUT_BLOCKING_TG_RENDER_SERVER
, self
);
1721 self
->options
|= TH_OPT_IPC_TG_BLOCKED
;
1726 * Routine: ipc_port_thread_group_unblocked
1728 * Call thread_group_unblocked callout if the
1729 * thread had previously made a thread_group_blocked
1730 * callout before (indicated by TH_OPT_IPC_TG_BLOCKED
1731 * flag on the thread).
1734 * Nothing is locked.
1737 ipc_port_thread_group_unblocked(void)
1739 #if CONFIG_THREAD_GROUPS
1740 thread_t self
= current_thread();
1742 if (!(self
->options
& TH_OPT_IPC_TG_BLOCKED
)) {
1746 machine_thread_group_unblocked(self
->thread_group
, NULL
,
1747 PERFCONTROL_CALLOUT_BLOCKING_TG_RENDER_SERVER
, self
);
1749 self
->options
&= ~TH_OPT_IPC_TG_BLOCKED
;
1753 #if DEVELOPMENT || DEBUG
1755 ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port
)
1757 special_reply_port
->ip_srp_lost_link
= 0;
1758 special_reply_port
->ip_srp_msg_sent
= 0;
1762 ipc_special_reply_port_msg_sent_reset(ipc_port_t special_reply_port
)
1764 if (special_reply_port
->ip_specialreply
== 1) {
1765 special_reply_port
->ip_srp_msg_sent
= 0;
1770 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port
)
1772 if (special_reply_port
->ip_specialreply
== 1) {
1773 special_reply_port
->ip_srp_msg_sent
= 1;
1778 ipc_special_reply_port_lost_link(ipc_port_t special_reply_port
)
1780 if (special_reply_port
->ip_specialreply
== 1 && special_reply_port
->ip_srp_msg_sent
== 0) {
1781 special_reply_port
->ip_srp_lost_link
= 1;
1785 #else /* DEVELOPMENT || DEBUG */
1787 ipc_special_reply_port_bits_reset(__unused ipc_port_t special_reply_port
)
1793 ipc_special_reply_port_msg_sent_reset(__unused ipc_port_t special_reply_port
)
1799 ipc_special_reply_port_msg_sent(__unused ipc_port_t special_reply_port
)
1805 ipc_special_reply_port_lost_link(__unused ipc_port_t special_reply_port
)
1809 #endif /* DEVELOPMENT || DEBUG */
1812 * Routine: ipc_port_adjust_special_reply_port_locked
1814 * If the special port has a turnstile, update its inheritor.
1816 * Special reply port locked on entry.
1817 * Special reply port unlocked on return.
1818 * The passed in port is a special reply port.
1823 ipc_port_adjust_special_reply_port_locked(
1824 ipc_port_t special_reply_port
,
1827 boolean_t get_turnstile
)
1829 ipc_port_t dest_port
= IPC_PORT_NULL
;
1830 int sync_link_state
= PORT_SYNC_LINK_NO_LINKAGE
;
1831 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
1832 struct turnstile
*ts
= TURNSTILE_NULL
;
1834 ip_lock_held(special_reply_port
); // ip_sync_link_state is touched
1835 imq_lock(&special_reply_port
->ip_messages
);
1837 if (!special_reply_port
->ip_specialreply
) {
1838 // only mach_msg_receive_results_complete() calls this with any port
1839 assert(get_turnstile
);
1843 if (flags
& IPC_PORT_ADJUST_SR_RECEIVED_MSG
) {
1844 ipc_special_reply_port_msg_sent_reset(special_reply_port
);
1847 if (flags
& IPC_PORT_ADJUST_UNLINK_THREAD
) {
1848 special_reply_port
->ip_messages
.imq_srp_owner_thread
= NULL
;
1851 if (flags
& IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN
) {
1852 special_reply_port
->ip_sync_bootstrap_checkin
= 0;
1855 /* Check if the special reply port is marked non-special */
1856 if (special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_ANY
) {
1858 if (get_turnstile
) {
1859 turnstile_complete((uintptr_t)special_reply_port
,
1860 port_rcv_turnstile_address(special_reply_port
), NULL
, TURNSTILE_SYNC_IPC
);
1862 imq_unlock(&special_reply_port
->ip_messages
);
1863 ip_unlock(special_reply_port
);
1864 if (get_turnstile
) {
1865 turnstile_cleanup();
1870 if (flags
& IPC_PORT_ADJUST_SR_LINK_WORKLOOP
) {
1871 if (ITH_KNOTE_VALID(kn
, MACH_MSG_TYPE_PORT_SEND_ONCE
)) {
1872 inheritor
= filt_machport_stash_port(kn
, special_reply_port
,
1875 } else if (flags
& IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE
) {
1876 sync_link_state
= PORT_SYNC_LINK_ANY
;
1879 /* Check if need to break linkage */
1880 if (!get_turnstile
&& sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
&&
1881 special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
) {
1882 imq_unlock(&special_reply_port
->ip_messages
);
1883 ip_unlock(special_reply_port
);
1887 switch (special_reply_port
->ip_sync_link_state
) {
1888 case PORT_SYNC_LINK_PORT
:
1889 dest_port
= special_reply_port
->ip_sync_inheritor_port
;
1890 special_reply_port
->ip_sync_inheritor_port
= IPC_PORT_NULL
;
1892 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1893 special_reply_port
->ip_sync_inheritor_knote
= NULL
;
1895 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1896 special_reply_port
->ip_sync_inheritor_ts
= NULL
;
1901 * Stash (or unstash) the server's PID in the ip_sorights field of the
1902 * special reply port, so that stackshot can later retrieve who the client
1905 if (special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_PORT
&&
1906 sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
) {
1907 ipc_special_reply_stash_pid_locked(special_reply_port
, pid_from_task(current_task()));
1908 } else if (special_reply_port
->ip_sync_link_state
== PORT_SYNC_LINK_NO_LINKAGE
&&
1909 sync_link_state
== PORT_SYNC_LINK_ANY
) {
1910 /* If we are resetting the special reply port, remove the stashed pid. */
1911 ipc_special_reply_stash_pid_locked(special_reply_port
, 0);
1914 special_reply_port
->ip_sync_link_state
= sync_link_state
;
1916 switch (sync_link_state
) {
1917 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
1918 special_reply_port
->ip_sync_inheritor_knote
= kn
;
1920 case PORT_SYNC_LINK_WORKLOOP_STASH
:
1921 special_reply_port
->ip_sync_inheritor_ts
= inheritor
;
1923 case PORT_SYNC_LINK_NO_LINKAGE
:
1924 if (flags
& IPC_PORT_ADJUST_SR_ENABLE_EVENT
) {
1925 ipc_special_reply_port_lost_link(special_reply_port
);
1930 /* Get thread's turnstile donated to special reply port */
1931 if (get_turnstile
) {
1932 turnstile_complete((uintptr_t)special_reply_port
,
1933 port_rcv_turnstile_address(special_reply_port
), NULL
, TURNSTILE_SYNC_IPC
);
1935 ts
= ipc_port_rcv_turnstile(special_reply_port
);
1937 turnstile_reference(ts
);
1938 ipc_port_recv_update_inheritor(special_reply_port
, ts
,
1939 TURNSTILE_IMMEDIATE_UPDATE
);
1943 imq_unlock(&special_reply_port
->ip_messages
);
1944 ip_unlock(special_reply_port
);
1946 if (get_turnstile
) {
1947 turnstile_cleanup();
1949 /* Call turnstile cleanup after dropping the interlock */
1950 turnstile_update_inheritor_complete(ts
, TURNSTILE_INTERLOCK_NOT_HELD
);
1951 turnstile_deallocate_safe(ts
);
1954 /* Release the ref on the dest port and its turnstile */
1956 ipc_port_send_turnstile_complete(dest_port
);
1957 /* release the reference on the dest port */
1958 ip_release(dest_port
);
1963 * Routine: ipc_port_adjust_special_reply_port
1965 * If the special port has a turnstile, update its inheritor.
1972 ipc_port_adjust_special_reply_port(
1976 if (port
->ip_specialreply
) {
1978 ipc_port_adjust_special_reply_port_locked(port
, NULL
, flags
, FALSE
);
1983 * Routine: ipc_port_adjust_sync_link_state_locked
1985 * Update the sync link state of the port and the
1986 * turnstile inheritor.
1988 * Port and mqueue locked on entry.
1989 * Port and mqueue locked on return.
1994 ipc_port_adjust_sync_link_state_locked(
1996 int sync_link_state
,
1997 turnstile_inheritor_t inheritor
)
1999 switch (port
->ip_sync_link_state
) {
2000 case PORT_SYNC_LINK_RCV_THREAD
:
2001 /* deallocate the thread reference for the inheritor */
2002 thread_deallocate_safe(port
->ip_messages
.imq_inheritor_thread_ref
);
2005 klist_init(&port
->ip_messages
.imq_klist
);
2008 switch (sync_link_state
) {
2009 case PORT_SYNC_LINK_WORKLOOP_KNOTE
:
2010 port
->ip_messages
.imq_inheritor_knote
= inheritor
;
2012 case PORT_SYNC_LINK_WORKLOOP_STASH
:
2013 port
->ip_messages
.imq_inheritor_turnstile
= inheritor
;
2015 case PORT_SYNC_LINK_RCV_THREAD
:
2016 /* The thread could exit without clearing port state, take a thread ref */
2017 thread_reference((thread_t
)inheritor
);
2018 port
->ip_messages
.imq_inheritor_thread_ref
= inheritor
;
2021 klist_init(&port
->ip_messages
.imq_klist
);
2022 sync_link_state
= PORT_SYNC_LINK_ANY
;
2025 port
->ip_sync_link_state
= sync_link_state
;
2030 * Routine: ipc_port_adjust_port_locked
2032 * If the port has a turnstile, update its inheritor.
2034 * Port locked on entry.
2035 * Port unlocked on return.
2040 ipc_port_adjust_port_locked(
2043 boolean_t sync_bootstrap_checkin
)
2045 int sync_link_state
= PORT_SYNC_LINK_ANY
;
2046 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
2048 ip_lock_held(port
); // ip_sync_link_state is touched
2049 imq_held(&port
->ip_messages
);
2051 assert(!port
->ip_specialreply
);
2054 inheritor
= filt_machport_stash_port(kn
, port
, &sync_link_state
);
2055 if (sync_link_state
== PORT_SYNC_LINK_WORKLOOP_KNOTE
) {
2058 } else if (sync_bootstrap_checkin
) {
2059 inheritor
= current_thread();
2060 sync_link_state
= PORT_SYNC_LINK_RCV_THREAD
;
2063 ipc_port_adjust_sync_link_state_locked(port
, sync_link_state
, inheritor
);
2064 port
->ip_sync_bootstrap_checkin
= 0;
2066 ipc_port_send_turnstile_recompute_push_locked(port
);
2067 /* port and mqueue unlocked */
2071 * Routine: ipc_port_clear_sync_rcv_thread_boost_locked
2073 * If the port is pushing on rcv thread, clear it.
2075 * Port locked on entry
2076 * mqueue is not locked.
2077 * Port unlocked on return.
2082 ipc_port_clear_sync_rcv_thread_boost_locked(
2085 ip_lock_held(port
); // ip_sync_link_state is touched
2087 if (port
->ip_sync_link_state
!= PORT_SYNC_LINK_RCV_THREAD
) {
2092 imq_lock(&port
->ip_messages
);
2093 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
2095 ipc_port_send_turnstile_recompute_push_locked(port
);
2096 /* port and mqueue unlocked */
2100 * Routine: ipc_port_add_watchport_elem_locked
2102 * Transfer the turnstile boost of watchport to task calling exec.
2104 * Port locked on entry.
2105 * Port unlocked on return.
2107 * KERN_SUCESS on success.
2108 * KERN_FAILURE otherwise.
2111 ipc_port_add_watchport_elem_locked(
2113 struct task_watchport_elem
*watchport_elem
,
2114 struct task_watchport_elem
**old_elem
)
2117 imq_held(&port
->ip_messages
);
2119 /* Watchport boost only works for non-special active ports mapped in an ipc space */
2120 if (!ip_active(port
) || port
->ip_specialreply
||
2121 port
->ip_receiver_name
== MACH_PORT_NULL
) {
2122 imq_unlock(&port
->ip_messages
);
2124 return KERN_FAILURE
;
2127 if (port
->ip_sync_link_state
!= PORT_SYNC_LINK_ANY
) {
2128 /* Sever the linkage if the port was pushing on knote */
2129 ipc_port_adjust_sync_link_state_locked(port
, PORT_SYNC_LINK_ANY
, NULL
);
2132 *old_elem
= ipc_port_update_watchport_elem(port
, watchport_elem
);
2134 ipc_port_send_turnstile_recompute_push_locked(port
);
2135 /* port and mqueue unlocked */
2136 return KERN_SUCCESS
;
2140 * Routine: ipc_port_clear_watchport_elem_internal_conditional_locked
2142 * Remove the turnstile boost of watchport and recompute the push.
2144 * Port locked on entry.
2145 * Port unlocked on return.
2147 * KERN_SUCESS on success.
2148 * KERN_FAILURE otherwise.
2151 ipc_port_clear_watchport_elem_internal_conditional_locked(
2153 struct task_watchport_elem
*watchport_elem
)
2156 imq_held(&port
->ip_messages
);
2158 if (ipc_port_watchport_elem(port
) != watchport_elem
) {
2159 imq_unlock(&port
->ip_messages
);
2161 return KERN_FAILURE
;
2164 ipc_port_clear_watchport_elem_internal(port
);
2165 ipc_port_send_turnstile_recompute_push_locked(port
);
2166 /* port and mqueue unlocked */
2167 return KERN_SUCCESS
;
2171 * Routine: ipc_port_replace_watchport_elem_conditional_locked
2173 * Replace the turnstile boost of watchport and recompute the push.
2175 * Port locked on entry.
2176 * Port unlocked on return.
2178 * KERN_SUCESS on success.
2179 * KERN_FAILURE otherwise.
2182 ipc_port_replace_watchport_elem_conditional_locked(
2184 struct task_watchport_elem
*old_watchport_elem
,
2185 struct task_watchport_elem
*new_watchport_elem
)
2188 imq_held(&port
->ip_messages
);
2190 if (ipc_port_watchport_elem(port
) != old_watchport_elem
) {
2191 imq_unlock(&port
->ip_messages
);
2193 return KERN_FAILURE
;
2196 ipc_port_update_watchport_elem(port
, new_watchport_elem
);
2197 ipc_port_send_turnstile_recompute_push_locked(port
);
2198 /* port and mqueue unlocked */
2199 return KERN_SUCCESS
;
2203 * Routine: ipc_port_clear_watchport_elem_internal
2205 * Remove the turnstile boost of watchport.
2207 * Port locked on entry.
2208 * Port locked on return.
2210 * Old task_watchport_elem returned.
2212 struct task_watchport_elem
*
2213 ipc_port_clear_watchport_elem_internal(
2217 imq_held(&port
->ip_messages
);
2219 if (port
->ip_specialreply
) {
2223 return ipc_port_update_watchport_elem(port
, NULL
);
2227 * Routine: ipc_port_send_turnstile_recompute_push_locked
2229 * Update send turnstile inheritor of port and recompute the push.
2231 * Port locked on entry.
2232 * Port unlocked on return.
2237 ipc_port_send_turnstile_recompute_push_locked(
2240 struct turnstile
*send_turnstile
= port_send_turnstile(port
);
2241 if (send_turnstile
) {
2242 turnstile_reference(send_turnstile
);
2243 ipc_port_send_update_inheritor(port
, send_turnstile
,
2244 TURNSTILE_IMMEDIATE_UPDATE
);
2246 imq_unlock(&port
->ip_messages
);
2249 if (send_turnstile
) {
2250 turnstile_update_inheritor_complete(send_turnstile
,
2251 TURNSTILE_INTERLOCK_NOT_HELD
);
2252 turnstile_deallocate_safe(send_turnstile
);
2257 * Routine: ipc_port_get_watchport_inheritor
2259 * Returns inheritor for watchport.
2264 * watchport inheritor.
2267 ipc_port_get_watchport_inheritor(
2270 imq_held(&port
->ip_messages
);
2271 return ipc_port_watchport_elem(port
)->twe_task
->watchports
->tw_thread
;
2275 * Routine: ipc_port_impcount_delta
2277 * Adjust only the importance count associated with a port.
2278 * If there are any adjustments to be made to receiver task,
2279 * those are handled elsewhere.
2281 * For now, be defensive during deductions to make sure the
2282 * impcount for the port doesn't underflow zero. This will
2283 * go away when the port boost addition is made atomic (see
2284 * note in ipc_port_importance_delta()).
2286 * The port is referenced and locked.
2287 * Nothing else is locked.
2290 ipc_port_impcount_delta(
2292 mach_port_delta_t delta
,
2293 ipc_port_t __unused base
)
2295 mach_port_delta_t absdelta
;
2297 if (!ip_active(port
)) {
2301 /* adding/doing nothing is easy */
2303 port
->ip_impcount
+= delta
;
2307 absdelta
= 0 - delta
;
2308 if (port
->ip_impcount
>= absdelta
) {
2309 port
->ip_impcount
-= absdelta
;
2313 #if (DEVELOPMENT || DEBUG)
2314 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
2315 task_t target_task
= port
->ip_receiver
->is_task
;
2316 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
2317 const char *target_procname
;
2320 if (target_imp
!= IIT_NULL
) {
2321 target_procname
= target_imp
->iit_procname
;
2322 target_pid
= target_imp
->iit_bsd_pid
;
2324 target_procname
= "unknown";
2327 printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
2328 "dropping %d assertion(s) but port only has %d remaining.\n",
2329 port
->ip_receiver_name
,
2330 target_pid
, target_procname
,
2331 absdelta
, port
->ip_impcount
);
2332 } else if (base
!= IP_NULL
) {
2333 task_t target_task
= base
->ip_receiver
->is_task
;
2334 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
2335 const char *target_procname
;
2338 if (target_imp
!= IIT_NULL
) {
2339 target_procname
= target_imp
->iit_procname
;
2340 target_pid
= target_imp
->iit_bsd_pid
;
2342 target_procname
= "unknown";
2345 printf("Over-release of importance assertions for port 0x%lx "
2346 "enqueued on port 0x%x with receiver pid %d (%s), "
2347 "dropping %d assertion(s) but port only has %d remaining.\n",
2348 (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port
),
2349 base
->ip_receiver_name
,
2350 target_pid
, target_procname
,
2351 absdelta
, port
->ip_impcount
);
2355 delta
= 0 - port
->ip_impcount
;
2356 port
->ip_impcount
= 0;
2361 * Routine: ipc_port_importance_delta_internal
2363 * Adjust the importance count through the given port.
2364 * If the port is in transit, apply the delta throughout
2365 * the chain. Determine if the there is a task at the
2366 * base of the chain that wants/needs to be adjusted,
2367 * and if so, apply the delta.
2369 * The port is referenced and locked on entry.
2370 * Importance may be locked.
2371 * Nothing else is locked.
2372 * The lock may be dropped on exit.
2373 * Returns TRUE if lock was dropped.
2375 #if IMPORTANCE_INHERITANCE
2378 ipc_port_importance_delta_internal(
2381 mach_port_delta_t
*deltap
,
2382 ipc_importance_task_t
*imp_task
)
2384 ipc_port_t next
, base
;
2385 boolean_t dropped
= FALSE
;
2387 *imp_task
= IIT_NULL
;
2393 assert(options
== IPID_OPTION_NORMAL
|| options
== IPID_OPTION_SENDPOSSIBLE
);
2397 /* if port is in transit, have to search for end of chain */
2398 if (ip_active(port
) &&
2399 port
->ip_destination
!= IP_NULL
&&
2400 port
->ip_receiver_name
== MACH_PORT_NULL
) {
2404 ipc_port_multiple_lock(); /* massive serialization */
2407 while (ip_active(base
) &&
2408 base
->ip_destination
!= IP_NULL
&&
2409 base
->ip_receiver_name
== MACH_PORT_NULL
) {
2410 base
= base
->ip_destination
;
2413 ipc_port_multiple_unlock();
2417 * If the port lock is dropped b/c the port is in transit, there is a
2418 * race window where another thread can drain messages and/or fire a
2419 * send possible notification before we get here.
2421 * We solve this race by checking to see if our caller armed the send
2422 * possible notification, whether or not it's been fired yet, and
2423 * whether or not we've already set the port's ip_spimportant bit. If
2424 * we don't need a send-possible boost, then we'll just apply a
2425 * harmless 0-boost to the port.
2427 if (options
& IPID_OPTION_SENDPOSSIBLE
) {
2428 assert(*deltap
== 1);
2429 if (port
->ip_sprequests
&& port
->ip_spimportant
== 0) {
2430 port
->ip_spimportant
= 1;
2436 /* unlock down to the base, adjusting boost(s) at each level */
2438 *deltap
= ipc_port_impcount_delta(port
, *deltap
, base
);
2444 /* port is in transit */
2445 assert(port
->ip_tempowner
== 0);
2446 next
= port
->ip_destination
;
2451 /* find the task (if any) to boost according to the base */
2452 if (ip_active(base
)) {
2453 if (base
->ip_tempowner
!= 0) {
2454 if (IIT_NULL
!= base
->ip_imp_task
) {
2455 *imp_task
= base
->ip_imp_task
;
2457 /* otherwise don't boost */
2458 } else if (base
->ip_receiver_name
!= MACH_PORT_NULL
) {
2459 ipc_space_t space
= base
->ip_receiver
;
2461 /* only spaces with boost-accepting tasks */
2462 if (space
->is_task
!= TASK_NULL
&&
2463 ipc_importance_task_is_any_receiver_type(space
->is_task
->task_imp_base
)) {
2464 *imp_task
= space
->is_task
->task_imp_base
;
2470 * Only the base is locked. If we have to hold or drop task
2471 * importance assertions, we'll have to drop that lock as well.
2473 if (*imp_task
!= IIT_NULL
) {
2474 /* take a reference before unlocking base */
2475 ipc_importance_task_reference(*imp_task
);
2478 if (dropped
== TRUE
) {
2484 #endif /* IMPORTANCE_INHERITANCE */
2487 * Routine: ipc_port_importance_delta
2489 * Adjust the importance count through the given port.
2490 * If the port is in transit, apply the delta throughout
2493 * If there is a task at the base of the chain that wants/needs
2494 * to be adjusted, apply the delta.
2496 * The port is referenced and locked on entry.
2497 * Nothing else is locked.
2498 * The lock may be dropped on exit.
2499 * Returns TRUE if lock was dropped.
2501 #if IMPORTANCE_INHERITANCE
2504 ipc_port_importance_delta(
2507 mach_port_delta_t delta
)
2509 ipc_importance_task_t imp_task
= IIT_NULL
;
2512 dropped
= ipc_port_importance_delta_internal(port
, options
, &delta
, &imp_task
);
2514 if (IIT_NULL
== imp_task
|| delta
== 0) {
2522 assert(ipc_importance_task_is_any_receiver_type(imp_task
));
2525 ipc_importance_task_hold_internal_assertion(imp_task
, delta
);
2527 ipc_importance_task_drop_internal_assertion(imp_task
, -delta
);
2530 ipc_importance_task_release(imp_task
);
2533 #endif /* IMPORTANCE_INHERITANCE */
2536 * Routine: ipc_port_make_send_locked
2538 * Make a naked send right from a receive right.
2541 * port locked and active.
2544 ipc_port_make_send_locked(
2547 require_ip_active(port
);
2555 * Routine: ipc_port_make_send
2557 * Make a naked send right from a receive right.
2564 if (!IP_VALID(port
)) {
2569 if (ip_active(port
)) {
2570 ipc_port_make_send_locked(port
);
2579 * Routine: ipc_port_copy_send_locked
2581 * Make a naked send right from another naked send right.
2583 * port locked and active.
2586 ipc_port_copy_send_locked(
2589 assert(port
->ip_srights
> 0);
2595 * Routine: ipc_port_copy_send
2597 * Make a naked send right from another naked send right.
2598 * IP_NULL -> IP_NULL
2599 * IP_DEAD -> IP_DEAD
2600 * dead port -> IP_DEAD
2601 * live port -> port + ref
2603 * Nothing locked except possibly a space.
2612 if (!IP_VALID(port
)) {
2617 if (ip_active(port
)) {
2618 ipc_port_copy_send_locked(port
);
2629 * Routine: ipc_port_copyout_send
2631 * Copyout a naked send right (possibly null/dead),
2632 * or if that fails, destroy the right.
2638 ipc_port_copyout_send(
2642 mach_port_name_t name
;
2644 if (IP_VALID(sright
)) {
2647 kr
= ipc_object_copyout(space
, ip_to_object(sright
),
2648 MACH_MSG_TYPE_PORT_SEND
, NULL
, NULL
, &name
);
2649 if (kr
!= KERN_SUCCESS
) {
2650 ipc_port_release_send(sright
);
2652 if (kr
== KERN_INVALID_CAPABILITY
) {
2653 name
= MACH_PORT_DEAD
;
2655 name
= MACH_PORT_NULL
;
2659 name
= CAST_MACH_PORT_TO_NAME(sright
);
2666 * Routine: ipc_port_release_send
2668 * Release a naked send right.
2669 * Consumes a ref for the port.
2675 ipc_port_release_send(
2678 ipc_port_t nsrequest
= IP_NULL
;
2679 mach_port_mscount_t mscount
;
2681 if (!IP_VALID(port
)) {
2687 assert(port
->ip_srights
> 0);
2688 if (port
->ip_srights
== 0) {
2689 panic("Over-release of port %p send right!", port
);
2694 if (!ip_active(port
)) {
2700 if (port
->ip_srights
== 0 &&
2701 port
->ip_nsrequest
!= IP_NULL
) {
2702 nsrequest
= port
->ip_nsrequest
;
2703 port
->ip_nsrequest
= IP_NULL
;
2704 mscount
= port
->ip_mscount
;
2707 ipc_notify_no_senders(nsrequest
, mscount
);
2715 * Routine: ipc_port_make_sonce_locked
2717 * Make a naked send-once right from a receive right.
2719 * The port is locked and active.
2723 ipc_port_make_sonce_locked(
2726 require_ip_active(port
);
2727 port
->ip_sorights
++;
2733 * Routine: ipc_port_make_sonce
2735 * Make a naked send-once right from a receive right.
2737 * The port is not locked.
2741 ipc_port_make_sonce(
2744 if (!IP_VALID(port
)) {
2749 if (ip_active(port
)) {
2750 ipc_port_make_sonce_locked(port
);
2759 * Routine: ipc_port_release_sonce
2761 * Release a naked send-once right.
2762 * Consumes a ref for the port.
2764 * In normal situations, this is never used.
2765 * Send-once rights are only consumed when
2766 * a message (possibly a send-once notification)
2769 * Nothing locked except possibly a space.
2773 ipc_port_release_sonce(
2776 if (!IP_VALID(port
)) {
2780 ipc_port_adjust_special_reply_port(port
, IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN
);
2784 assert(port
->ip_sorights
> 0);
2785 if (port
->ip_sorights
== 0) {
2786 panic("Over-release of port %p send-once right!", port
);
2789 port
->ip_sorights
--;
2796 * Routine: ipc_port_release_receive
2798 * Release a naked (in limbo or in transit) receive right.
2799 * Consumes a ref for the port; destroys the port.
2805 ipc_port_release_receive(
2810 if (!IP_VALID(port
)) {
2815 require_ip_active(port
);
2816 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
2817 dest
= port
->ip_destination
;
2819 ipc_port_destroy(port
); /* consumes ref, unlocks */
2821 if (dest
!= IP_NULL
) {
2822 ipc_port_send_turnstile_complete(dest
);
2828 * Routine: ipc_port_alloc_special
2830 * Allocate a port in a special space.
2831 * The new port is returned with one ref.
2832 * If unsuccessful, IP_NULL is returned.
2838 ipc_port_alloc_special(
2840 ipc_port_init_flags_t flags
)
2844 port
= ip_object_to_port(io_alloc(IOT_PORT
));
2845 if (port
== IP_NULL
) {
2850 uintptr_t buf
[IP_CALLSTACK_MAX
];
2851 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
2852 #endif /* MACH_ASSERT */
2854 bzero((char *)port
, sizeof(*port
));
2855 io_lock_init(ip_to_object(port
));
2856 port
->ip_references
= 1;
2857 port
->ip_object
.io_bits
= io_makebits(TRUE
, IOT_PORT
, 0);
2859 ipc_port_init(port
, space
, flags
, 1);
2862 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
2863 #endif /* MACH_ASSERT */
2869 * Routine: ipc_port_dealloc_special
2871 * Deallocate a port in a special space.
2872 * Consumes one ref for the port.
2878 ipc_port_dealloc_special(
2880 __assert_only ipc_space_t space
)
2883 require_ip_active(port
);
2884 // assert(port->ip_receiver_name != MACH_PORT_NULL);
2885 assert(port
->ip_receiver
== space
);
2888 * We clear ip_receiver_name and ip_receiver to simplify
2889 * the ipc_space_kernel check in ipc_mqueue_send.
2892 imq_lock(&port
->ip_messages
);
2893 port
->ip_receiver_name
= MACH_PORT_NULL
;
2894 port
->ip_receiver
= IS_NULL
;
2895 imq_unlock(&port
->ip_messages
);
2897 /* relevant part of ipc_port_clear_receiver */
2898 port
->ip_mscount
= 0;
2899 port
->ip_messages
.imq_seqno
= 0;
2901 ipc_port_destroy(port
);
2905 * Routine: ipc_port_finalize
2907 * Called on last reference deallocate to
2908 * free any remaining data associated with the
2917 ipc_port_request_t requests
= port
->ip_requests
;
2919 assert(port_send_turnstile(port
) == TURNSTILE_NULL
);
2920 if (imq_is_turnstile_proxy(&port
->ip_messages
)) {
2921 assert(ipc_port_rcv_turnstile(port
) == TURNSTILE_NULL
);
2924 if (ip_active(port
)) {
2925 panic("Trying to free an active port. port %p", port
);
2928 if (requests
!= IPR_NULL
) {
2929 ipc_table_size_t its
= requests
->ipr_size
;
2930 it_requests_free(its
, requests
);
2931 port
->ip_requests
= IPR_NULL
;
2934 ipc_mqueue_deinit(&port
->ip_messages
);
2937 ipc_port_track_dealloc(port
);
2938 #endif /* MACH_ASSERT */
2942 * Routine: kdp_mqueue_send_find_owner
2944 * Discover the owner of the ipc_mqueue that contains the input
2945 * waitq object. The thread blocked on the waitq should be
2946 * waiting for an IPC_MQUEUE_FULL event.
2948 * The 'waitinfo->wait_type' value should already be set to
2949 * kThreadWaitPortSend.
2951 * If we find out that the containing port is actually in
2952 * transit, we reset the wait_type field to reflect this.
2955 kdp_mqueue_send_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
2957 struct turnstile
*turnstile
;
2958 assert(waitinfo
->wait_type
== kThreadWaitPortSend
);
2959 assert(event
== IPC_MQUEUE_FULL
);
2960 assert(waitq_is_turnstile_queue(waitq
));
2962 turnstile
= waitq_to_turnstile(waitq
);
2963 ipc_port_t port
= (ipc_port_t
)turnstile
->ts_proprietor
; /* we are blocking on send */
2965 zone_id_require(ZONE_ID_IPC_PORT
, sizeof(struct ipc_port
), port
);
2967 waitinfo
->owner
= 0;
2968 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
2969 if (ip_lock_held_kdp(port
)) {
2971 * someone has the port locked: it may be in an
2972 * inconsistent state: bail
2974 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
2978 if (ip_active(port
)) {
2979 if (port
->ip_tempowner
) {
2980 if (port
->ip_imp_task
!= IIT_NULL
&& port
->ip_imp_task
->iit_task
!= NULL
) {
2981 /* port is held by a tempowner */
2982 waitinfo
->owner
= pid_from_task(port
->ip_imp_task
->iit_task
);
2984 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
2986 } else if (port
->ip_receiver_name
) {
2987 /* port in a space */
2988 if (port
->ip_receiver
== ipc_space_kernel
) {
2990 * The kernel pid is 0, make this
2991 * distinguishable from no-owner and
2992 * inconsistent port state.
2994 waitinfo
->owner
= STACKSHOT_WAITOWNER_KERNEL
;
2996 waitinfo
->owner
= pid_from_task(port
->ip_receiver
->is_task
);
2998 } else if (port
->ip_destination
!= IP_NULL
) {
2999 /* port in transit */
3000 waitinfo
->wait_type
= kThreadWaitPortSendInTransit
;
3001 waitinfo
->owner
= VM_KERNEL_UNSLIDE_OR_PERM(port
->ip_destination
);
3007 * Routine: kdp_mqueue_recv_find_owner
3009 * Discover the "owner" of the ipc_mqueue that contains the input
3010 * waitq object. The thread blocked on the waitq is trying to
3011 * receive on the mqueue.
3013 * The 'waitinfo->wait_type' value should already be set to
3014 * kThreadWaitPortReceive.
3016 * If we find that we are actualy waiting on a port set, we reset
3017 * the wait_type field to reflect this.
3020 kdp_mqueue_recv_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
3022 assert(waitinfo
->wait_type
== kThreadWaitPortReceive
);
3023 assert(event
== IPC_MQUEUE_RECEIVE
);
3025 ipc_mqueue_t mqueue
= imq_from_waitq(waitq
);
3026 waitinfo
->owner
= 0;
3027 if (imq_is_set(mqueue
)) { /* we are waiting on a port set */
3028 ipc_pset_t set
= ips_from_mq(mqueue
);
3030 zone_id_require(ZONE_ID_IPC_PORT_SET
, sizeof(struct ipc_pset
), set
);
3032 /* Reset wait type to specify waiting on port set receive */
3033 waitinfo
->wait_type
= kThreadWaitPortSetReceive
;
3034 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(set
);
3035 if (ips_lock_held_kdp(set
)) {
3036 waitinfo
->owner
= STACKSHOT_WAITOWNER_PSET_LOCKED
;
3038 /* There is no specific owner "at the other end" of a port set, so leave unset. */
3040 ipc_port_t port
= ip_from_mq(mqueue
);
3042 zone_id_require(ZONE_ID_IPC_PORT
, sizeof(struct ipc_port
), port
);
3044 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
3045 if (ip_lock_held_kdp(port
)) {
3046 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
3050 if (ip_active(port
)) {
3051 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
3052 waitinfo
->owner
= port
->ip_receiver_name
;
3054 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
3061 #include <kern/machine.h>
3064 * Keep a list of all allocated ports.
3065 * Allocation is intercepted via ipc_port_init;
3066 * deallocation is intercepted via io_free.
3069 queue_head_t port_alloc_queue
= QUEUE_HEAD_INITIALIZER(port_alloc_queue
);
3070 LCK_SPIN_DECLARE(port_alloc_queue_lock
, &ipc_lck_grp
, &ipc_lck_attr
);
3073 unsigned long port_count
= 0;
3074 unsigned long port_count_warning
= 20000;
3075 unsigned long port_timestamp
= 0;
3077 void db_port_stack_trace(
3082 unsigned int verbose
,
3083 unsigned int display
,
3084 unsigned int ref_search
,
3085 unsigned int ref_target
);
3088 extern int proc_pid(struct proc
*);
3089 #endif /* MACH_BSD */
3092 * Initialize all of the debugging state in a port.
3093 * Insert the port into a global list of all allocated ports.
3096 ipc_port_init_debug(
3098 uintptr_t *callstack
,
3099 unsigned int callstack_max
)
3103 port
->ip_thread
= current_thread();
3104 port
->ip_timetrack
= port_timestamp
++;
3105 for (i
= 0; i
< callstack_max
; ++i
) {
3106 port
->ip_callstack
[i
] = callstack
[i
];
3108 for (i
= 0; i
< IP_NSPARES
; ++i
) {
3109 port
->ip_spares
[i
] = 0;
3113 task_t task
= current_task();
3114 if (task
!= TASK_NULL
) {
3115 struct proc
* proc
= (struct proc
*) get_bsdtask_info(task
);
3117 port
->ip_spares
[0] = proc_pid(proc
);
3120 #endif /* MACH_BSD */
3123 lck_spin_lock(&port_alloc_queue_lock
);
3125 if (port_count_warning
> 0 && port_count
>= port_count_warning
) {
3126 assert(port_count
< port_count_warning
);
3128 queue_enter(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
3129 lck_spin_unlock(&port_alloc_queue_lock
);
3134 * Routine: ipc_port_callstack_init_debug
3136 * Calls the machine-dependent routine to
3137 * fill in an array with up to IP_CALLSTACK_MAX
3138 * levels of return pc information
3140 * May block (via copyin)
3143 ipc_port_callstack_init_debug(
3144 uintptr_t *callstack
,
3145 unsigned int callstack_max
)
3149 /* guarantee the callstack is initialized */
3150 for (i
= 0; i
< callstack_max
; i
++) {
3155 machine_callstack(callstack
, callstack_max
);
3160 * Remove a port from the queue of allocated ports.
3161 * This routine should be invoked JUST prior to
3162 * deallocating the actual memory occupied by the port.
3166 ipc_port_track_dealloc(
3167 __unused ipc_port_t port
)
3172 ipc_port_track_dealloc(
3175 lck_spin_lock(&port_alloc_queue_lock
);
3176 assert(port_count
> 0);
3178 queue_remove(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
3179 lck_spin_unlock(&port_alloc_queue_lock
);
3184 #endif /* MACH_ASSERT */