2 * Copyright (c) 2000-2007 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>
95 #include <security/mac_mach_internal.h>
99 decl_lck_spin_data(, ipc_port_multiple_lock_data
)
100 ipc_port_timestamp_t ipc_port_timestamp_data
;
104 void ipc_port_init_debug(
106 uintptr_t *callstack
,
107 unsigned int callstack_max
);
109 void ipc_port_callstack_init_debug(
110 uintptr_t *callstack
,
111 unsigned int callstack_max
);
113 #endif /* MACH_ASSERT */
115 void kdp_mqueue_send_find_owner(struct waitq
* waitq
, event64_t event
, thread_waitinfo_t
*waitinfo
);
116 void kdp_mqueue_recv_find_owner(struct waitq
* waitq
, event64_t event
, thread_waitinfo_t
*waitinfo
);
119 ipc_port_release(ipc_port_t port
)
125 ipc_port_reference(ipc_port_t port
)
131 * Routine: ipc_port_timestamp
133 * Retrieve a timestamp value.
137 ipc_port_timestamp(void)
139 return OSIncrementAtomic(&ipc_port_timestamp_data
);
143 * Routine: ipc_port_request_alloc
145 * Try to allocate a request slot.
146 * If successful, returns the request index.
147 * Otherwise returns zero.
149 * The port is locked and active.
151 * KERN_SUCCESS A request index was found.
152 * KERN_NO_SPACE No index allocated.
155 #if IMPORTANCE_INHERITANCE
157 ipc_port_request_alloc(
159 mach_port_name_t name
,
161 boolean_t send_possible
,
163 ipc_port_request_index_t
*indexp
,
164 boolean_t
*importantp
)
167 ipc_port_request_alloc(
169 mach_port_name_t name
,
171 boolean_t send_possible
,
173 ipc_port_request_index_t
*indexp
)
174 #endif /* IMPORTANCE_INHERITANCE */
176 ipc_port_request_t ipr
, table
;
177 ipc_port_request_index_t index
;
180 #if IMPORTANCE_INHERITANCE
182 #endif /* IMPORTANCE_INHERITANCE */
184 assert(ip_active(port
));
185 assert(name
!= MACH_PORT_NULL
);
186 assert(soright
!= IP_NULL
);
188 table
= port
->ip_requests
;
190 if (table
== IPR_NULL
)
191 return KERN_NO_SPACE
;
193 index
= table
->ipr_next
;
195 return KERN_NO_SPACE
;
198 assert(ipr
->ipr_name
== MACH_PORT_NULL
);
200 table
->ipr_next
= ipr
->ipr_next
;
201 ipr
->ipr_name
= name
;
204 mask
|= IPR_SOR_SPREQ_MASK
;
206 mask
|= IPR_SOR_SPARM_MASK
;
207 if (port
->ip_sprequests
== 0) {
208 port
->ip_sprequests
= 1;
209 #if IMPORTANCE_INHERITANCE
210 /* TODO: Live importance support in send-possible */
211 if (port
->ip_impdonation
!= 0 &&
212 port
->ip_spimportant
== 0 &&
213 (task_is_importance_donor(current_task()))) {
216 #endif /* IMPORTANCE_INHERTANCE */
220 ipr
->ipr_soright
= IPR_SOR_MAKE(soright
, mask
);
228 * Routine: ipc_port_request_grow
230 * Grow a port's table of requests.
232 * The port must be locked and active.
233 * Nothing else locked; will allocate memory.
234 * Upon return the port is unlocked.
236 * KERN_SUCCESS Grew the table.
237 * KERN_SUCCESS Somebody else grew the table.
238 * KERN_SUCCESS The port died.
239 * KERN_RESOURCE_SHORTAGE Couldn't allocate new table.
240 * KERN_NO_SPACE Couldn't grow to desired size
244 ipc_port_request_grow(
246 ipc_table_elems_t target_size
)
248 ipc_table_size_t its
;
249 ipc_port_request_t otable
, ntable
;
251 assert(ip_active(port
));
253 otable
= port
->ip_requests
;
254 if (otable
== IPR_NULL
)
255 its
= &ipc_table_requests
[0];
257 its
= otable
->ipr_size
+ 1;
259 if (target_size
!= ITS_SIZE_NONE
) {
260 if ((otable
!= IPR_NULL
) &&
261 (target_size
<= otable
->ipr_size
->its_size
)) {
265 while ((its
->its_size
) && (its
->its_size
< target_size
)) {
268 if (its
->its_size
== 0) {
270 return KERN_NO_SPACE
;
277 if ((its
->its_size
== 0) ||
278 ((ntable
= it_requests_alloc(its
)) == IPR_NULL
)) {
280 return KERN_RESOURCE_SHORTAGE
;
286 * Check that port is still active and that nobody else
287 * has slipped in and grown the table on us. Note that
288 * just checking if the current table pointer == otable
289 * isn't sufficient; must check ipr_size.
292 if (ip_active(port
) && (port
->ip_requests
== otable
) &&
293 ((otable
== IPR_NULL
) || (otable
->ipr_size
+1 == its
))) {
294 ipc_table_size_t oits
;
295 ipc_table_elems_t osize
, nsize
;
296 ipc_port_request_index_t free
, i
;
298 /* copy old table to new table */
300 if (otable
!= IPR_NULL
) {
301 oits
= otable
->ipr_size
;
302 osize
= oits
->its_size
;
303 free
= otable
->ipr_next
;
305 (void) memcpy((void *)(ntable
+ 1),
306 (const void *)(otable
+ 1),
307 (osize
- 1) * sizeof(struct ipc_port_request
));
314 nsize
= its
->its_size
;
315 assert(nsize
> osize
);
317 /* add new elements to the new table's free list */
319 for (i
= osize
; i
< nsize
; i
++) {
320 ipc_port_request_t ipr
= &ntable
[i
];
322 ipr
->ipr_name
= MACH_PORT_NULL
;
323 ipr
->ipr_next
= free
;
327 ntable
->ipr_next
= free
;
328 ntable
->ipr_size
= its
;
329 port
->ip_requests
= ntable
;
333 if (otable
!= IPR_NULL
) {
334 it_requests_free(oits
, otable
);
339 it_requests_free(its
, ntable
);
346 * Routine: ipc_port_request_sparm
348 * Arm delayed send-possible request.
350 * The port must be locked and active.
352 * Returns TRUE if the request was armed
353 * (or armed with importance in that version).
357 ipc_port_request_sparm(
359 __assert_only mach_port_name_t name
,
360 ipc_port_request_index_t index
,
361 mach_msg_option_t option
,
362 mach_msg_priority_t override
)
364 if (index
!= IE_REQ_NONE
) {
365 ipc_port_request_t ipr
, table
;
367 assert(ip_active(port
));
369 table
= port
->ip_requests
;
370 assert(table
!= IPR_NULL
);
373 assert(ipr
->ipr_name
== name
);
375 /* Is there a valid destination? */
376 if (IPR_SOR_SPREQ(ipr
->ipr_soright
)) {
377 ipr
->ipr_soright
= IPR_SOR_MAKE(ipr
->ipr_soright
, IPR_SOR_SPARM_MASK
);
378 port
->ip_sprequests
= 1;
380 if (option
& MACH_SEND_OVERRIDE
) {
381 /* apply override to message queue */
382 ipc_mqueue_override_send(&port
->ip_messages
, override
);
385 #if IMPORTANCE_INHERITANCE
386 if (((option
& MACH_SEND_NOIMPORTANCE
) == 0) &&
387 (port
->ip_impdonation
!= 0) &&
388 (port
->ip_spimportant
== 0) &&
389 (((option
& MACH_SEND_IMPORTANCE
) != 0) ||
390 (task_is_importance_donor(current_task())))) {
395 #endif /* IMPORTANCE_INHERITANCE */
402 * Routine: ipc_port_request_type
404 * Determine the type(s) of port requests enabled for a name.
406 * The port must be locked or inactive (to avoid table growth).
407 * The index must not be IE_REQ_NONE and for the name in question.
410 ipc_port_request_type(
412 __assert_only mach_port_name_t name
,
413 ipc_port_request_index_t index
)
415 ipc_port_request_t ipr
, table
;
416 mach_port_type_t type
= 0;
418 table
= port
->ip_requests
;
419 assert (table
!= IPR_NULL
);
421 assert(index
!= IE_REQ_NONE
);
423 assert(ipr
->ipr_name
== name
);
425 if (IP_VALID(IPR_SOR_PORT(ipr
->ipr_soright
))) {
426 type
|= MACH_PORT_TYPE_DNREQUEST
;
428 if (IPR_SOR_SPREQ(ipr
->ipr_soright
)) {
429 type
|= MACH_PORT_TYPE_SPREQUEST
;
431 if (!IPR_SOR_SPARMED(ipr
->ipr_soright
)) {
432 type
|= MACH_PORT_TYPE_SPREQUEST_DELAYED
;
440 * Routine: ipc_port_request_cancel
442 * Cancel a dead-name/send-possible request and return the send-once right.
444 * The port must be locked and active.
445 * The index must not be IPR_REQ_NONE and must correspond with name.
449 ipc_port_request_cancel(
451 __assert_only mach_port_name_t name
,
452 ipc_port_request_index_t index
)
454 ipc_port_request_t ipr
, table
;
455 ipc_port_t request
= IP_NULL
;
457 assert(ip_active(port
));
458 table
= port
->ip_requests
;
459 assert(table
!= IPR_NULL
);
461 assert (index
!= IE_REQ_NONE
);
463 assert(ipr
->ipr_name
== name
);
464 request
= IPR_SOR_PORT(ipr
->ipr_soright
);
466 /* return ipr to the free list inside the table */
467 ipr
->ipr_name
= MACH_PORT_NULL
;
468 ipr
->ipr_next
= table
->ipr_next
;
469 table
->ipr_next
= index
;
475 * Routine: ipc_port_pdrequest
477 * Make a port-deleted request, returning the
478 * previously registered send-once right.
479 * Just cancels the previous request if notify is IP_NULL.
481 * The port is locked and active. It is unlocked.
482 * Consumes a ref for notify (if non-null), and
483 * returns previous with a ref (if non-null).
490 ipc_port_t
*previousp
)
494 assert(ip_active(port
));
496 previous
= port
->ip_pdrequest
;
497 port
->ip_pdrequest
= notify
;
500 *previousp
= previous
;
504 * Routine: ipc_port_nsrequest
506 * Make a no-senders request, returning the
507 * previously registered send-once right.
508 * Just cancels the previous request if notify is IP_NULL.
510 * The port is locked and active. It is unlocked.
511 * Consumes a ref for notify (if non-null), and
512 * returns previous with a ref (if non-null).
518 mach_port_mscount_t sync
,
520 ipc_port_t
*previousp
)
523 mach_port_mscount_t mscount
;
525 assert(ip_active(port
));
527 previous
= port
->ip_nsrequest
;
528 mscount
= port
->ip_mscount
;
530 if ((port
->ip_srights
== 0) && (sync
<= mscount
) &&
531 (notify
!= IP_NULL
)) {
532 port
->ip_nsrequest
= IP_NULL
;
534 ipc_notify_no_senders(notify
, mscount
);
536 port
->ip_nsrequest
= notify
;
540 *previousp
= previous
;
545 * Routine: ipc_port_clear_receiver
547 * Prepares a receive right for transmission/destruction,
548 * optionally performs mqueue destruction (with port lock held)
551 * The port is locked and active.
553 * If should_destroy is TRUE, then the return value indicates
554 * whether the caller needs to reap kmsg structures that should
555 * be destroyed (by calling ipc_kmsg_reap_delayed)
557 * If should_destroy is FALSE, this always returns FALSE
561 ipc_port_clear_receiver(
563 boolean_t should_destroy
)
565 ipc_mqueue_t mqueue
= &port
->ip_messages
;
566 boolean_t reap_messages
= FALSE
;
569 * Pull ourselves out of any sets to which we belong.
570 * We hold the port locked, so even though this acquires and releases
571 * the mqueue lock, we know we won't be added to any other sets.
573 if (port
->ip_in_pset
!= 0) {
574 ipc_pset_remove_from_all(port
);
575 assert(port
->ip_in_pset
== 0);
579 * Send anyone waiting on the port's queue directly away.
580 * Also clear the mscount and seqno.
583 ipc_mqueue_changed(mqueue
);
584 port
->ip_mscount
= 0;
585 mqueue
->imq_seqno
= 0;
586 port
->ip_context
= port
->ip_guarded
= port
->ip_strict_guard
= 0;
588 if (should_destroy
) {
590 * Mark the mqueue invalid, preventing further send/receive
591 * operations from succeeding. It's important for this to be
592 * done under the same lock hold as the ipc_mqueue_changed
593 * call to avoid additional threads blocking on an mqueue
594 * that's being destroyed.
596 reap_messages
= ipc_mqueue_destroy_locked(mqueue
);
599 imq_unlock(&port
->ip_messages
);
601 return reap_messages
;
605 * Routine: ipc_port_init
607 * Initializes a newly-allocated port.
608 * Doesn't touch the ip_object fields.
615 mach_port_name_t name
)
617 /* port->ip_kobject doesn't have to be initialized */
619 port
->ip_receiver
= space
;
620 port
->ip_receiver_name
= name
;
622 port
->ip_mscount
= 0;
623 port
->ip_srights
= 0;
624 port
->ip_sorights
= 0;
626 port
->ip_nsrequest
= IP_NULL
;
627 port
->ip_pdrequest
= IP_NULL
;
628 port
->ip_requests
= IPR_NULL
;
630 port
->ip_premsg
= IKM_NULL
;
631 port
->ip_context
= 0;
633 port
->ip_sprequests
= 0;
634 port
->ip_spimportant
= 0;
635 port
->ip_impdonation
= 0;
636 port
->ip_tempowner
= 0;
638 port
->ip_guarded
= 0;
639 port
->ip_strict_guard
= 0;
640 port
->ip_impcount
= 0;
642 port
->ip_reserved
= 0;
644 ipc_mqueue_init(&port
->ip_messages
,
645 FALSE
/* !set */, NULL
/* no reserved link */);
649 * Routine: ipc_port_alloc
653 * Nothing locked. If successful, the port is returned
654 * locked. (The caller doesn't have a reference.)
656 * KERN_SUCCESS The port is allocated.
657 * KERN_INVALID_TASK The space is dead.
658 * KERN_NO_SPACE No room for an entry in the space.
659 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
665 mach_port_name_t
*namep
,
669 mach_port_name_t name
;
673 uintptr_t buf
[IP_CALLSTACK_MAX
];
674 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
675 #endif /* MACH_ASSERT */
677 kr
= ipc_object_alloc(space
, IOT_PORT
,
678 MACH_PORT_TYPE_RECEIVE
, 0,
679 &name
, (ipc_object_t
*) &port
);
680 if (kr
!= KERN_SUCCESS
)
683 /* port and space are locked */
684 ipc_port_init(port
, space
, name
);
687 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
688 #endif /* MACH_ASSERT */
690 /* unlock space after init */
691 is_write_unlock(space
);
700 * Routine: ipc_port_alloc_name
702 * Allocate a port, with a specific name.
704 * Nothing locked. If successful, the port is returned
705 * locked. (The caller doesn't have a reference.)
707 * KERN_SUCCESS The port is allocated.
708 * KERN_INVALID_TASK The space is dead.
709 * KERN_NAME_EXISTS The name already denotes a right.
710 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
716 mach_port_name_t name
,
723 uintptr_t buf
[IP_CALLSTACK_MAX
];
724 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
725 #endif /* MACH_ASSERT */
727 kr
= ipc_object_alloc_name(space
, IOT_PORT
,
728 MACH_PORT_TYPE_RECEIVE
, 0,
729 name
, (ipc_object_t
*) &port
);
730 if (kr
!= KERN_SUCCESS
)
735 ipc_port_init(port
, space
, name
);
738 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
739 #endif /* MACH_ASSERT */
747 * Routine: ipc_port_spnotify
749 * Generate send-possible port notifications.
751 * Nothing locked, reference held on port.
757 ipc_port_request_index_t index
= 0;
758 ipc_table_elems_t size
= 0;
761 * If the port has no send-possible request
762 * armed, don't bother to lock the port.
764 if (port
->ip_sprequests
== 0)
769 #if IMPORTANCE_INHERITANCE
770 if (port
->ip_spimportant
!= 0) {
771 port
->ip_spimportant
= 0;
772 if (ipc_port_importance_delta(port
, IPID_OPTION_NORMAL
, -1) == TRUE
) {
776 #endif /* IMPORTANCE_INHERITANCE */
778 if (port
->ip_sprequests
== 0) {
782 port
->ip_sprequests
= 0;
785 if (ip_active(port
)) {
786 ipc_port_request_t requests
;
788 /* table may change each time port unlocked (reload) */
789 requests
= port
->ip_requests
;
790 assert(requests
!= IPR_NULL
);
793 * no need to go beyond table size when first
794 * we entered - those are future notifications.
797 size
= requests
->ipr_size
->its_size
;
799 /* no need to backtrack either */
800 while (++index
< size
) {
801 ipc_port_request_t ipr
= &requests
[index
];
802 mach_port_name_t name
= ipr
->ipr_name
;
803 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
804 boolean_t armed
= IPR_SOR_SPARMED(ipr
->ipr_soright
);
806 if (MACH_PORT_VALID(name
) && armed
&& IP_VALID(soright
)) {
807 /* claim send-once right - slot still inuse */
808 ipr
->ipr_soright
= IP_NULL
;
811 ipc_notify_send_possible(soright
, name
);
823 * Routine: ipc_port_dnnotify
825 * Generate dead name notifications for
826 * all outstanding dead-name and send-
830 * Port must be inactive.
831 * Reference held on port.
837 ipc_port_request_t requests
= port
->ip_requests
;
839 assert(!ip_active(port
));
840 if (requests
!= IPR_NULL
) {
841 ipc_table_size_t its
= requests
->ipr_size
;
842 ipc_table_elems_t size
= its
->its_size
;
843 ipc_port_request_index_t index
;
844 for (index
= 1; index
< size
; index
++) {
845 ipc_port_request_t ipr
= &requests
[index
];
846 mach_port_name_t name
= ipr
->ipr_name
;
847 ipc_port_t soright
= IPR_SOR_PORT(ipr
->ipr_soright
);
849 if (MACH_PORT_VALID(name
) && IP_VALID(soright
)) {
850 ipc_notify_dead_name(soright
, name
);
858 * Routine: ipc_port_destroy
860 * Destroys a port. Cleans up queued messages.
862 * If the port has a backup, it doesn't get destroyed,
863 * but is sent in a port-destroyed notification to the backup.
865 * The port is locked and alive; nothing else locked.
866 * The caller has a reference, which is consumed.
867 * Afterwards, the port is unlocked and dead.
871 ipc_port_destroy(ipc_port_t port
)
873 ipc_port_t pdrequest
, nsrequest
;
877 #if IMPORTANCE_INHERITANCE
878 ipc_importance_task_t release_imp_task
= IIT_NULL
;
879 thread_t self
= current_thread();
880 boolean_t top
= (self
->ith_assertions
== 0);
881 natural_t assertcnt
= 0;
882 #endif /* IMPORTANCE_INHERITANCE */
884 assert(ip_active(port
));
885 /* port->ip_receiver_name is garbage */
886 /* port->ip_receiver/port->ip_destination is garbage */
888 /* check for a backup port */
889 pdrequest
= port
->ip_pdrequest
;
891 #if IMPORTANCE_INHERITANCE
892 /* determine how many assertions to drop and from whom */
893 if (port
->ip_tempowner
!= 0) {
895 release_imp_task
= port
->ip_imp_task
;
896 if (IIT_NULL
!= release_imp_task
) {
897 port
->ip_imp_task
= IIT_NULL
;
898 assertcnt
= port
->ip_impcount
;
900 /* Otherwise, nothing to drop */
902 assertcnt
= port
->ip_impcount
;
903 if (pdrequest
!= IP_NULL
)
904 /* mark in limbo for the journey */
905 port
->ip_tempowner
= 1;
909 self
->ith_assertions
= assertcnt
;
910 #endif /* IMPORTANCE_INHERITANCE */
912 if (pdrequest
!= IP_NULL
) {
913 /* clear receiver, don't destroy the port */
914 (void)ipc_port_clear_receiver(port
, FALSE
);
915 assert(port
->ip_in_pset
== 0);
916 assert(port
->ip_mscount
== 0);
918 /* we assume the ref for pdrequest */
919 port
->ip_pdrequest
= IP_NULL
;
921 /* make port be in limbo */
922 port
->ip_receiver_name
= MACH_PORT_NULL
;
923 port
->ip_destination
= IP_NULL
;
926 /* consumes our refs for port and pdrequest */
927 ipc_notify_port_destroyed(pdrequest
, port
);
929 goto drop_assertions
;
932 port
->ip_object
.io_bits
&= ~IO_BITS_ACTIVE
;
933 port
->ip_timestamp
= ipc_port_timestamp();
934 nsrequest
= port
->ip_nsrequest
;
937 * The mach_msg_* paths don't hold a port lock, they only hold a
938 * reference to the port object. If a thread raced us and is now
939 * blocked waiting for message reception on this mqueue (or waiting
940 * for ipc_mqueue_full), it will never be woken up. We call
941 * ipc_port_clear_receiver() here, _after_ the port has been marked
942 * inactive, to wakeup any threads which may be blocked and ensure
943 * that no other thread can get lost waiting for a wake up on a
944 * port/mqueue that's been destroyed.
946 boolean_t reap_msgs
= FALSE
;
947 reap_msgs
= ipc_port_clear_receiver(port
, TRUE
); /* marks mqueue inactive */
948 assert(port
->ip_in_pset
== 0);
949 assert(port
->ip_mscount
== 0);
952 * If the port has a preallocated message buffer and that buffer
953 * is not inuse, free it. If it has an inuse one, then the kmsg
954 * free will detect that we freed the association and it can free it
955 * like a normal buffer.
957 * Once the port is marked inactive we don't need to keep it locked.
959 if (IP_PREALLOC(port
)) {
960 ipc_port_t inuse_port
;
962 kmsg
= port
->ip_premsg
;
963 assert(kmsg
!= IKM_NULL
);
964 inuse_port
= ikm_prealloc_inuse_port(kmsg
);
965 IP_CLEAR_PREALLOC(port
, kmsg
);
967 if (inuse_port
!= IP_NULL
) {
968 assert(inuse_port
== port
);
976 /* throw away no-senders request */
977 if (nsrequest
!= IP_NULL
)
978 ipc_notify_send_once(nsrequest
); /* consumes ref */
981 * Reap any kmsg objects waiting to be destroyed.
982 * This must be done after we've released the port lock.
985 ipc_kmsg_reap_delayed();
987 mqueue
= &port
->ip_messages
;
989 /* cleanup waitq related resources */
990 ipc_mqueue_deinit(mqueue
);
992 /* generate dead-name notifications */
993 ipc_port_dnnotify(port
);
995 ipc_kobject_destroy(port
);
997 ip_release(port
); /* consume caller's ref */
1000 #if IMPORTANCE_INHERITANCE
1001 if (release_imp_task
!= IIT_NULL
) {
1002 if (assertcnt
> 0) {
1004 self
->ith_assertions
= 0;
1005 assert(ipc_importance_task_is_any_receiver_type(release_imp_task
));
1006 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1008 ipc_importance_task_release(release_imp_task
);
1010 } else if (assertcnt
> 0) {
1012 self
->ith_assertions
= 0;
1013 release_imp_task
= current_task()->task_imp_base
;
1014 if (ipc_importance_task_is_any_receiver_type(release_imp_task
)) {
1015 ipc_importance_task_drop_internal_assertion(release_imp_task
, assertcnt
);
1019 #endif /* IMPORTANCE_INHERITANCE */
1023 * Routine: ipc_port_check_circularity
1025 * Check if queueing "port" in a message for "dest"
1026 * would create a circular group of ports and messages.
1028 * If no circularity (FALSE returned), then "port"
1029 * is changed from "in limbo" to "in transit".
1031 * That is, we want to set port->ip_destination == dest,
1032 * but guaranteeing that this doesn't create a circle
1033 * port->ip_destination->ip_destination->... == port
1036 * No ports locked. References held for "port" and "dest".
1040 ipc_port_check_circularity(
1044 #if IMPORTANCE_INHERITANCE
1045 /* adjust importance counts at the same time */
1046 return ipc_importance_check_circularity(port
, dest
);
1050 assert(port
!= IP_NULL
);
1051 assert(dest
!= IP_NULL
);
1058 * First try a quick check that can run in parallel.
1059 * No circularity if dest is not in transit.
1062 if (ip_lock_try(dest
)) {
1063 if (!ip_active(dest
) ||
1064 (dest
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1065 (dest
->ip_destination
== IP_NULL
))
1068 /* dest is in transit; further checking necessary */
1074 ipc_port_multiple_lock(); /* massive serialization */
1077 * Search for the end of the chain (a port not in transit),
1078 * acquiring locks along the way.
1084 if (!ip_active(base
) ||
1085 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1086 (base
->ip_destination
== IP_NULL
))
1089 base
= base
->ip_destination
;
1092 /* all ports in chain from dest to base, inclusive, are locked */
1095 /* circularity detected! */
1097 ipc_port_multiple_unlock();
1099 /* port (== base) is in limbo */
1101 assert(ip_active(port
));
1102 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1103 assert(port
->ip_destination
== IP_NULL
);
1105 while (dest
!= IP_NULL
) {
1108 /* dest is in transit or in limbo */
1110 assert(ip_active(dest
));
1111 assert(dest
->ip_receiver_name
== MACH_PORT_NULL
);
1113 next
= dest
->ip_destination
;
1122 * The guarantee: lock port while the entire chain is locked.
1123 * Once port is locked, we can take a reference to dest,
1124 * add port to the chain, and unlock everything.
1128 ipc_port_multiple_unlock();
1132 /* port is in limbo */
1134 assert(ip_active(port
));
1135 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1136 assert(port
->ip_destination
== IP_NULL
);
1139 port
->ip_destination
= dest
;
1141 /* now unlock chain */
1149 /* port is in transit */
1151 assert(ip_active(dest
));
1152 assert(dest
->ip_receiver_name
== MACH_PORT_NULL
);
1153 assert(dest
->ip_destination
!= IP_NULL
);
1155 port
= dest
->ip_destination
;
1160 /* base is not in transit */
1161 assert(!ip_active(base
) ||
1162 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
1163 (base
->ip_destination
== IP_NULL
));
1168 #endif /* !IMPORTANCE_INHERITANCE */
1172 * Routine: ipc_port_impcount_delta
1174 * Adjust only the importance count associated with a port.
1175 * If there are any adjustments to be made to receiver task,
1176 * those are handled elsewhere.
1178 * For now, be defensive during deductions to make sure the
1179 * impcount for the port doesn't underflow zero. This will
1180 * go away when the port boost addition is made atomic (see
1181 * note in ipc_port_importance_delta()).
1183 * The port is referenced and locked.
1184 * Nothing else is locked.
1187 ipc_port_impcount_delta(
1189 mach_port_delta_t delta
,
1190 ipc_port_t __unused base
)
1192 mach_port_delta_t absdelta
;
1194 if (!ip_active(port
)) {
1198 /* adding/doing nothing is easy */
1200 port
->ip_impcount
+= delta
;
1204 absdelta
= 0 - delta
;
1205 if (port
->ip_impcount
>= absdelta
) {
1206 port
->ip_impcount
-= absdelta
;
1210 #if (DEVELOPMENT || DEBUG)
1211 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
1212 task_t target_task
= port
->ip_receiver
->is_task
;
1213 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
1214 const char *target_procname
;
1217 if (target_imp
!= IIT_NULL
) {
1218 target_procname
= target_imp
->iit_procname
;
1219 target_pid
= target_imp
->iit_bsd_pid
;
1221 target_procname
= "unknown";
1224 printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
1225 "dropping %d assertion(s) but port only has %d remaining.\n",
1226 port
->ip_receiver_name
,
1227 target_pid
, target_procname
,
1228 absdelta
, port
->ip_impcount
);
1230 } else if (base
!= IP_NULL
) {
1231 task_t target_task
= base
->ip_receiver
->is_task
;
1232 ipc_importance_task_t target_imp
= target_task
->task_imp_base
;
1233 const char *target_procname
;
1236 if (target_imp
!= IIT_NULL
) {
1237 target_procname
= target_imp
->iit_procname
;
1238 target_pid
= target_imp
->iit_bsd_pid
;
1240 target_procname
= "unknown";
1243 printf("Over-release of importance assertions for port 0x%lx "
1244 "enqueued on port 0x%x with receiver pid %d (%s), "
1245 "dropping %d assertion(s) but port only has %d remaining.\n",
1246 (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port
),
1247 base
->ip_receiver_name
,
1248 target_pid
, target_procname
,
1249 absdelta
, port
->ip_impcount
);
1253 delta
= 0 - port
->ip_impcount
;
1254 port
->ip_impcount
= 0;
1259 * Routine: ipc_port_importance_delta_internal
1261 * Adjust the importance count through the given port.
1262 * If the port is in transit, apply the delta throughout
1263 * the chain. Determine if the there is a task at the
1264 * base of the chain that wants/needs to be adjusted,
1265 * and if so, apply the delta.
1267 * The port is referenced and locked on entry.
1268 * Importance may be locked.
1269 * Nothing else is locked.
1270 * The lock may be dropped on exit.
1271 * Returns TRUE if lock was dropped.
1273 #if IMPORTANCE_INHERITANCE
1276 ipc_port_importance_delta_internal(
1279 mach_port_delta_t
*deltap
,
1280 ipc_importance_task_t
*imp_task
)
1282 ipc_port_t next
, base
;
1283 boolean_t dropped
= FALSE
;
1285 *imp_task
= IIT_NULL
;
1290 assert(options
== IPID_OPTION_NORMAL
|| options
== IPID_OPTION_SENDPOSSIBLE
);
1294 /* if port is in transit, have to search for end of chain */
1295 if (ip_active(port
) &&
1296 port
->ip_destination
!= IP_NULL
&&
1297 port
->ip_receiver_name
== MACH_PORT_NULL
) {
1302 ipc_port_multiple_lock(); /* massive serialization */
1305 while(ip_active(base
) &&
1306 base
->ip_destination
!= IP_NULL
&&
1307 base
->ip_receiver_name
== MACH_PORT_NULL
) {
1309 base
= base
->ip_destination
;
1312 ipc_port_multiple_unlock();
1316 * If the port lock is dropped b/c the port is in transit, there is a
1317 * race window where another thread can drain messages and/or fire a
1318 * send possible notification before we get here.
1320 * We solve this race by checking to see if our caller armed the send
1321 * possible notification, whether or not it's been fired yet, and
1322 * whether or not we've already set the port's ip_spimportant bit. If
1323 * we don't need a send-possible boost, then we'll just apply a
1324 * harmless 0-boost to the port.
1326 if (options
& IPID_OPTION_SENDPOSSIBLE
) {
1327 assert(*deltap
== 1);
1328 if (port
->ip_sprequests
&& port
->ip_spimportant
== 0)
1329 port
->ip_spimportant
= 1;
1334 /* unlock down to the base, adjusting boost(s) at each level */
1336 *deltap
= ipc_port_impcount_delta(port
, *deltap
, base
);
1342 /* port is in transit */
1343 assert(port
->ip_tempowner
== 0);
1344 next
= port
->ip_destination
;
1349 /* find the task (if any) to boost according to the base */
1350 if (ip_active(base
)) {
1351 if (base
->ip_tempowner
!= 0) {
1352 if (IIT_NULL
!= base
->ip_imp_task
)
1353 *imp_task
= base
->ip_imp_task
;
1354 /* otherwise don't boost */
1356 } else if (base
->ip_receiver_name
!= MACH_PORT_NULL
) {
1357 ipc_space_t space
= base
->ip_receiver
;
1359 /* only spaces with boost-accepting tasks */
1360 if (space
->is_task
!= TASK_NULL
&&
1361 ipc_importance_task_is_any_receiver_type(space
->is_task
->task_imp_base
)) {
1362 *imp_task
= space
->is_task
->task_imp_base
;
1368 * Only the base is locked. If we have to hold or drop task
1369 * importance assertions, we'll have to drop that lock as well.
1371 if (*imp_task
!= IIT_NULL
) {
1372 /* take a reference before unlocking base */
1373 ipc_importance_task_reference(*imp_task
);
1376 if (dropped
== TRUE
) {
1382 #endif /* IMPORTANCE_INHERITANCE */
1385 * Routine: ipc_port_importance_delta
1387 * Adjust the importance count through the given port.
1388 * If the port is in transit, apply the delta throughout
1391 * If there is a task at the base of the chain that wants/needs
1392 * to be adjusted, apply the delta.
1394 * The port is referenced and locked on entry.
1395 * Nothing else is locked.
1396 * The lock may be dropped on exit.
1397 * Returns TRUE if lock was dropped.
1399 #if IMPORTANCE_INHERITANCE
1402 ipc_port_importance_delta(
1405 mach_port_delta_t delta
)
1407 ipc_importance_task_t imp_task
= IIT_NULL
;
1410 dropped
= ipc_port_importance_delta_internal(port
, options
, &delta
, &imp_task
);
1412 if (IIT_NULL
== imp_task
|| delta
== 0)
1418 assert(ipc_importance_task_is_any_receiver_type(imp_task
));
1421 ipc_importance_task_hold_internal_assertion(imp_task
, delta
);
1423 ipc_importance_task_drop_internal_assertion(imp_task
, -delta
);
1425 ipc_importance_task_release(imp_task
);
1428 #endif /* IMPORTANCE_INHERITANCE */
1431 * Routine: ipc_port_lookup_notify
1433 * Make a send-once notify port from a receive right.
1434 * Returns IP_NULL if name doesn't denote a receive right.
1436 * The space must be locked (read or write) and active.
1437 * Being the active space, we can rely on thread server_id
1438 * context to give us the proper server level sub-order
1443 ipc_port_lookup_notify(
1445 mach_port_name_t name
)
1450 assert(is_active(space
));
1452 entry
= ipc_entry_lookup(space
, name
);
1453 if (entry
== IE_NULL
)
1455 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1458 __IGNORE_WCASTALIGN(port
= (ipc_port_t
) entry
->ie_object
);
1459 assert(port
!= IP_NULL
);
1462 assert(ip_active(port
));
1463 assert(port
->ip_receiver_name
== name
);
1464 assert(port
->ip_receiver
== space
);
1467 port
->ip_sorights
++;
1474 * Routine: ipc_port_make_send_locked
1476 * Make a naked send right from a receive right.
1479 * port locked and active.
1482 ipc_port_make_send_locked(
1485 assert(ip_active(port
));
1493 * Routine: ipc_port_make_send
1495 * Make a naked send right from a receive right.
1503 if (!IP_VALID(port
))
1507 if (ip_active(port
)) {
1519 * Routine: ipc_port_copy_send
1521 * Make a naked send right from another naked send right.
1522 * IP_NULL -> IP_NULL
1523 * IP_DEAD -> IP_DEAD
1524 * dead port -> IP_DEAD
1525 * live port -> port + ref
1527 * Nothing locked except possibly a space.
1536 if (!IP_VALID(port
))
1540 if (ip_active(port
)) {
1541 assert(port
->ip_srights
> 0);
1554 * Routine: ipc_port_copyout_send
1556 * Copyout a naked send right (possibly null/dead),
1557 * or if that fails, destroy the right.
1563 ipc_port_copyout_send(
1567 mach_port_name_t name
;
1569 if (IP_VALID(sright
)) {
1572 kr
= ipc_object_copyout(space
, (ipc_object_t
) sright
,
1573 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
1574 if (kr
!= KERN_SUCCESS
) {
1575 ipc_port_release_send(sright
);
1577 if (kr
== KERN_INVALID_CAPABILITY
)
1578 name
= MACH_PORT_DEAD
;
1580 name
= MACH_PORT_NULL
;
1583 name
= CAST_MACH_PORT_TO_NAME(sright
);
1589 * Routine: ipc_port_release_send
1591 * Release a naked send right.
1592 * Consumes a ref for the port.
1598 ipc_port_release_send(
1601 ipc_port_t nsrequest
= IP_NULL
;
1602 mach_port_mscount_t mscount
;
1604 if (!IP_VALID(port
))
1609 assert(port
->ip_srights
> 0);
1612 if (!ip_active(port
)) {
1618 if (port
->ip_srights
== 0 &&
1619 port
->ip_nsrequest
!= IP_NULL
) {
1620 nsrequest
= port
->ip_nsrequest
;
1621 port
->ip_nsrequest
= IP_NULL
;
1622 mscount
= port
->ip_mscount
;
1625 ipc_notify_no_senders(nsrequest
, mscount
);
1633 * Routine: ipc_port_make_sonce_locked
1635 * Make a naked send-once right from a receive right.
1637 * The port is locked and active.
1641 ipc_port_make_sonce_locked(
1644 assert(ip_active(port
));
1645 port
->ip_sorights
++;
1651 * Routine: ipc_port_make_sonce
1653 * Make a naked send-once right from a receive right.
1655 * The port is not locked.
1659 ipc_port_make_sonce(
1662 if (!IP_VALID(port
))
1666 if (ip_active(port
)) {
1667 port
->ip_sorights
++;
1677 * Routine: ipc_port_release_sonce
1679 * Release a naked send-once right.
1680 * Consumes a ref for the port.
1682 * In normal situations, this is never used.
1683 * Send-once rights are only consumed when
1684 * a message (possibly a send-once notification)
1687 * Nothing locked except possibly a space.
1691 ipc_port_release_sonce(
1694 if (!IP_VALID(port
))
1699 assert(port
->ip_sorights
> 0);
1701 port
->ip_sorights
--;
1708 * Routine: ipc_port_release_receive
1710 * Release a naked (in limbo or in transit) receive right.
1711 * Consumes a ref for the port; destroys the port.
1717 ipc_port_release_receive(
1722 if (!IP_VALID(port
))
1726 assert(ip_active(port
));
1727 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1728 dest
= port
->ip_destination
;
1730 ipc_port_destroy(port
); /* consumes ref, unlocks */
1732 if (dest
!= IP_NULL
)
1737 * Routine: ipc_port_alloc_special
1739 * Allocate a port in a special space.
1740 * The new port is returned with one ref.
1741 * If unsuccessful, IP_NULL is returned.
1747 ipc_port_alloc_special(
1752 __IGNORE_WCASTALIGN(port
= (ipc_port_t
) io_alloc(IOT_PORT
));
1753 if (port
== IP_NULL
)
1757 uintptr_t buf
[IP_CALLSTACK_MAX
];
1758 ipc_port_callstack_init_debug(&buf
[0], IP_CALLSTACK_MAX
);
1759 #endif /* MACH_ASSERT */
1761 bzero((char *)port
, sizeof(*port
));
1762 io_lock_init(&port
->ip_object
);
1763 port
->ip_references
= 1;
1764 port
->ip_object
.io_bits
= io_makebits(TRUE
, IOT_PORT
, 0);
1766 ipc_port_init(port
, space
, 1);
1769 ipc_port_init_debug(port
, &buf
[0], IP_CALLSTACK_MAX
);
1770 #endif /* MACH_ASSERT */
1776 * Routine: ipc_port_dealloc_special
1778 * Deallocate a port in a special space.
1779 * Consumes one ref for the port.
1785 ipc_port_dealloc_special(
1787 __assert_only ipc_space_t space
)
1790 assert(ip_active(port
));
1791 // assert(port->ip_receiver_name != MACH_PORT_NULL);
1792 assert(port
->ip_receiver
== space
);
1795 * We clear ip_receiver_name and ip_receiver to simplify
1796 * the ipc_space_kernel check in ipc_mqueue_send.
1799 port
->ip_receiver_name
= MACH_PORT_NULL
;
1800 port
->ip_receiver
= IS_NULL
;
1802 /* relevant part of ipc_port_clear_receiver */
1803 ipc_port_set_mscount(port
, 0);
1804 port
->ip_messages
.imq_seqno
= 0;
1806 ipc_port_destroy(port
);
1810 * Routine: ipc_port_finalize
1812 * Called on last reference deallocate to
1813 * free any remaining data associated with the
1822 ipc_port_request_t requests
= port
->ip_requests
;
1824 assert(!ip_active(port
));
1825 if (requests
!= IPR_NULL
) {
1826 ipc_table_size_t its
= requests
->ipr_size
;
1827 it_requests_free(its
, requests
);
1828 port
->ip_requests
= IPR_NULL
;
1831 ipc_mqueue_deinit(&port
->ip_messages
);
1834 ipc_port_track_dealloc(port
);
1835 #endif /* MACH_ASSERT */
1839 * Routine: kdp_mqueue_send_find_owner
1841 * Discover the owner of the ipc_mqueue that contains the input
1842 * waitq object. The thread blocked on the waitq should be
1843 * waiting for an IPC_MQUEUE_FULL event.
1845 * The 'waitinfo->wait_type' value should already be set to
1846 * kThreadWaitPortSend.
1848 * If we find out that the containing port is actually in
1849 * transit, we reset the wait_type field to reflect this.
1852 kdp_mqueue_send_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
1854 assert(waitinfo
->wait_type
== kThreadWaitPortSend
);
1855 assert(event
== IPC_MQUEUE_FULL
);
1857 ipc_mqueue_t mqueue
= imq_from_waitq(waitq
);
1858 ipc_port_t port
= ip_from_mq(mqueue
); /* we are blocking on send */
1859 assert(kdp_is_in_zone(port
, "ipc ports"));
1861 waitinfo
->owner
= 0;
1862 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
1863 if (ip_lock_held_kdp(port
)) {
1865 * someone has the port locked: it may be in an
1866 * inconsistent state: bail
1868 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
1872 if (ip_active(port
)) {
1873 if (port
->ip_tempowner
) {
1874 if (port
->ip_imp_task
!= IIT_NULL
) {
1875 /* port is held by a tempowner */
1876 waitinfo
->owner
= pid_from_task(port
->ip_imp_task
->iit_task
);
1878 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
1880 } else if (port
->ip_receiver_name
) {
1881 /* port in a space */
1882 if (port
->ip_receiver
== ipc_space_kernel
) {
1884 * The kernel pid is 0, make this
1885 * distinguishable from no-owner and
1886 * inconsistent port state.
1888 waitinfo
->owner
= STACKSHOT_WAITOWNER_KERNEL
;
1890 waitinfo
->owner
= pid_from_task(port
->ip_receiver
->is_task
);
1892 } else if (port
->ip_destination
!= IP_NULL
) {
1893 /* port in transit */
1894 waitinfo
->wait_type
= kThreadWaitPortSendInTransit
;
1895 waitinfo
->owner
= VM_KERNEL_UNSLIDE_OR_PERM(port
->ip_destination
);
1901 * Routine: kdp_mqueue_recv_find_owner
1903 * Discover the "owner" of the ipc_mqueue that contains the input
1904 * waitq object. The thread blocked on the waitq is trying to
1905 * receive on the mqueue.
1907 * The 'waitinfo->wait_type' value should already be set to
1908 * kThreadWaitPortReceive.
1910 * If we find that we are actualy waiting on a port set, we reset
1911 * the wait_type field to reflect this.
1914 kdp_mqueue_recv_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
1916 assert(waitinfo
->wait_type
== kThreadWaitPortReceive
);
1917 assert(event
== IPC_MQUEUE_RECEIVE
);
1919 ipc_mqueue_t mqueue
= imq_from_waitq(waitq
);
1920 waitinfo
->owner
= 0;
1921 if (imq_is_set(mqueue
)) { /* we are waiting on a port set */
1922 ipc_pset_t set
= ips_from_mq(mqueue
);
1923 assert(kdp_is_in_zone(set
, "ipc port sets"));
1925 /* Reset wait type to specify waiting on port set receive */
1926 waitinfo
->wait_type
= kThreadWaitPortSetReceive
;
1927 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(set
);
1928 if (ips_lock_held_kdp(set
)) {
1929 waitinfo
->owner
= STACKSHOT_WAITOWNER_PSET_LOCKED
;
1931 /* There is no specific owner "at the other end" of a port set, so leave unset. */
1933 ipc_port_t port
= ip_from_mq(mqueue
);
1934 assert(kdp_is_in_zone(port
, "ipc ports"));
1936 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(port
);
1937 if (ip_lock_held_kdp(port
)) {
1938 waitinfo
->owner
= STACKSHOT_WAITOWNER_PORT_LOCKED
;
1942 if (ip_active(port
)) {
1943 if (port
->ip_receiver_name
!= MACH_PORT_NULL
) {
1944 waitinfo
->owner
= port
->ip_receiver_name
;
1946 waitinfo
->owner
= STACKSHOT_WAITOWNER_INTRANSIT
;
1953 #include <kern/machine.h>
1956 * Keep a list of all allocated ports.
1957 * Allocation is intercepted via ipc_port_init;
1958 * deallocation is intercepted via io_free.
1961 queue_head_t port_alloc_queue
;
1962 lck_spin_t port_alloc_queue_lock
;
1965 unsigned long port_count
= 0;
1966 unsigned long port_count_warning
= 20000;
1967 unsigned long port_timestamp
= 0;
1969 void db_port_stack_trace(
1974 unsigned int verbose
,
1975 unsigned int display
,
1976 unsigned int ref_search
,
1977 unsigned int ref_target
);
1980 * Initialize global state needed for run-time
1984 ipc_port_debug_init(void)
1987 queue_init(&port_alloc_queue
);
1988 lck_spin_init(&port_alloc_queue_lock
, &ipc_lck_grp
, &ipc_lck_attr
);
1991 if (!PE_parse_boot_argn("ipc_portbt", &ipc_portbt
, sizeof (ipc_portbt
)))
1996 extern int proc_pid(struct proc
*);
1997 #endif /* MACH_BSD */
2000 * Initialize all of the debugging state in a port.
2001 * Insert the port into a global list of all allocated ports.
2004 ipc_port_init_debug(
2006 uintptr_t *callstack
,
2007 unsigned int callstack_max
)
2011 port
->ip_thread
= current_thread();
2012 port
->ip_timetrack
= port_timestamp
++;
2013 for (i
= 0; i
< callstack_max
; ++i
)
2014 port
->ip_callstack
[i
] = callstack
[i
];
2015 for (i
= 0; i
< IP_NSPARES
; ++i
)
2016 port
->ip_spares
[i
] = 0;
2019 task_t task
= current_task();
2020 if (task
!= TASK_NULL
) {
2021 struct proc
* proc
= (struct proc
*) get_bsdtask_info(task
);
2023 port
->ip_spares
[0] = proc_pid(proc
);
2025 #endif /* MACH_BSD */
2028 lck_spin_lock(&port_alloc_queue_lock
);
2030 if (port_count_warning
> 0 && port_count
>= port_count_warning
)
2031 assert(port_count
< port_count_warning
);
2032 queue_enter(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
2033 lck_spin_unlock(&port_alloc_queue_lock
);
2038 * Routine: ipc_port_callstack_init_debug
2040 * Calls the machine-dependent routine to
2041 * fill in an array with up to IP_CALLSTACK_MAX
2042 * levels of return pc information
2044 * May block (via copyin)
2047 ipc_port_callstack_init_debug(
2048 uintptr_t *callstack
,
2049 unsigned int callstack_max
)
2053 /* guarantee the callstack is initialized */
2054 for (i
=0; i
< callstack_max
; i
++)
2058 machine_callstack(callstack
, callstack_max
);
2062 * Remove a port from the queue of allocated ports.
2063 * This routine should be invoked JUST prior to
2064 * deallocating the actual memory occupied by the port.
2068 ipc_port_track_dealloc(
2069 __unused ipc_port_t port
)
2074 ipc_port_track_dealloc(
2077 lck_spin_lock(&port_alloc_queue_lock
);
2078 assert(port_count
> 0);
2080 queue_remove(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
2081 lck_spin_unlock(&port_alloc_queue_lock
);
2086 #endif /* MACH_ASSERT */