2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: ipc/ipc_port.c
57 * Functions to manipulate IPC ports.
62 #include <zone_debug.h>
63 #include <mach_assert.h>
65 #include <mach/port.h>
66 #include <mach/kern_return.h>
67 #include <kern/lock.h>
68 #include <kern/ipc_kobject.h>
69 #include <kern/thread.h>
70 #include <kern/misc_protos.h>
71 #include <kern/wait_queue.h>
72 #include <ipc/ipc_entry.h>
73 #include <ipc/ipc_space.h>
74 #include <ipc/ipc_object.h>
75 #include <ipc/ipc_port.h>
76 #include <ipc/ipc_pset.h>
77 #include <ipc/ipc_kmsg.h>
78 #include <ipc/ipc_mqueue.h>
79 #include <ipc/ipc_notify.h>
80 #include <ipc/ipc_print.h>
81 #include <ipc/ipc_table.h>
84 #include <machine/db_machdep.h>
85 #include <ddb/db_command.h>
86 #include <ddb/db_expr.h>
91 decl_mutex_data(, ipc_port_multiple_lock_data
)
92 decl_mutex_data(, ipc_port_timestamp_lock_data
)
93 ipc_port_timestamp_t ipc_port_timestamp_data
;
96 void ipc_port_init_debug(
98 #endif /* MACH_ASSERT */
100 #if MACH_KDB && ZONE_DEBUG
102 void print_type_ports(unsigned, unsigned);
103 void print_ports(void);
104 #endif /* MACH_KDB && ZONE_DEBUG */
107 * Routine: ipc_port_timestamp
109 * Retrieve a timestamp value.
113 ipc_port_timestamp(void)
115 ipc_port_timestamp_t timestamp
;
117 ipc_port_timestamp_lock();
118 timestamp
= ipc_port_timestamp_data
++;
119 ipc_port_timestamp_unlock();
125 * Routine: ipc_port_dnrequest
127 * Try to allocate a dead-name request slot.
128 * If successful, returns the request index.
129 * Otherwise returns zero.
131 * The port is locked and active.
133 * KERN_SUCCESS A request index was found.
134 * KERN_NO_SPACE No index allocated.
140 mach_port_name_t name
,
142 ipc_port_request_index_t
*indexp
)
144 ipc_port_request_t ipr
, table
;
145 ipc_port_request_index_t index
;
147 assert(ip_active(port
));
148 assert(name
!= MACH_PORT_NULL
);
149 assert(soright
!= IP_NULL
);
151 table
= port
->ip_dnrequests
;
152 if (table
== IPR_NULL
)
153 return KERN_NO_SPACE
;
155 index
= table
->ipr_next
;
157 return KERN_NO_SPACE
;
160 assert(ipr
->ipr_name
== MACH_PORT_NULL
);
162 table
->ipr_next
= ipr
->ipr_next
;
163 ipr
->ipr_name
= name
;
164 ipr
->ipr_soright
= soright
;
171 * Routine: ipc_port_dngrow
173 * Grow a port's table of dead-name requests.
175 * The port must be locked and active.
176 * Nothing else locked; will allocate memory.
177 * Upon return the port is unlocked.
179 * KERN_SUCCESS Grew the table.
180 * KERN_SUCCESS Somebody else grew the table.
181 * KERN_SUCCESS The port died.
182 * KERN_RESOURCE_SHORTAGE Couldn't allocate new table.
183 * KERN_NO_SPACE Couldn't grow to desired size
189 ipc_table_elems_t target_size
)
191 ipc_table_size_t its
;
192 ipc_port_request_t otable
, ntable
;
194 assert(ip_active(port
));
196 otable
= port
->ip_dnrequests
;
197 if (otable
== IPR_NULL
)
198 its
= &ipc_table_dnrequests
[0];
200 its
= otable
->ipr_size
+ 1;
202 if (target_size
!= ITS_SIZE_NONE
) {
203 if ((otable
!= IPR_NULL
) &&
204 (target_size
<= otable
->ipr_size
->its_size
)) {
208 while ((its
->its_size
) && (its
->its_size
< target_size
)) {
211 if (its
->its_size
== 0) {
213 return KERN_NO_SPACE
;
220 if ((its
->its_size
== 0) ||
221 ((ntable
= it_dnrequests_alloc(its
)) == IPR_NULL
)) {
222 ipc_port_release(port
);
223 return KERN_RESOURCE_SHORTAGE
;
230 * Check that port is still active and that nobody else
231 * has slipped in and grown the table on us. Note that
232 * just checking port->ip_dnrequests == otable isn't
233 * sufficient; must check ipr_size.
236 if (ip_active(port
) &&
237 (port
->ip_dnrequests
== otable
) &&
238 ((otable
== IPR_NULL
) || (otable
->ipr_size
+1 == its
))) {
239 ipc_table_size_t oits
;
240 ipc_table_elems_t osize
, nsize
;
241 ipc_port_request_index_t free
, i
;
243 /* copy old table to new table */
245 if (otable
!= IPR_NULL
) {
246 oits
= otable
->ipr_size
;
247 osize
= oits
->its_size
;
248 free
= otable
->ipr_next
;
250 (void) memcpy((void *)(ntable
+ 1),
251 (const void *)(otable
+ 1),
252 (osize
- 1) * sizeof(struct ipc_port_request
));
259 nsize
= its
->its_size
;
260 assert(nsize
> osize
);
262 /* add new elements to the new table's free list */
264 for (i
= osize
; i
< nsize
; i
++) {
265 ipc_port_request_t ipr
= &ntable
[i
];
267 ipr
->ipr_name
= MACH_PORT_NULL
;
268 ipr
->ipr_next
= free
;
272 ntable
->ipr_next
= free
;
273 ntable
->ipr_size
= its
;
274 port
->ip_dnrequests
= ntable
;
277 if (otable
!= IPR_NULL
) {
278 it_dnrequests_free(oits
, otable
);
281 ip_check_unlock(port
);
282 it_dnrequests_free(its
, ntable
);
289 * Routine: ipc_port_dncancel
291 * Cancel a dead-name request and return the send-once right.
293 * The port must locked and active.
299 __assert_only mach_port_name_t name
,
300 ipc_port_request_index_t index
)
302 ipc_port_request_t ipr
, table
;
303 ipc_port_t dnrequest
;
305 assert(ip_active(port
));
306 assert(name
!= MACH_PORT_NULL
);
309 table
= port
->ip_dnrequests
;
310 assert(table
!= IPR_NULL
);
313 dnrequest
= ipr
->ipr_soright
;
314 assert(ipr
->ipr_name
== name
);
316 /* return ipr to the free list inside the table */
318 ipr
->ipr_name
= MACH_PORT_NULL
;
319 ipr
->ipr_next
= table
->ipr_next
;
320 table
->ipr_next
= index
;
326 * Routine: ipc_port_pdrequest
328 * Make a port-deleted request, returning the
329 * previously registered send-once right.
330 * Just cancels the previous request if notify is IP_NULL.
332 * The port is locked and active. It is unlocked.
333 * Consumes a ref for notify (if non-null), and
334 * returns previous with a ref (if non-null).
341 ipc_port_t
*previousp
)
345 assert(ip_active(port
));
347 previous
= port
->ip_pdrequest
;
348 port
->ip_pdrequest
= notify
;
351 *previousp
= previous
;
355 * Routine: ipc_port_nsrequest
357 * Make a no-senders request, returning the
358 * previously registered send-once right.
359 * Just cancels the previous request if notify is IP_NULL.
361 * The port is locked and active. It is unlocked.
362 * Consumes a ref for notify (if non-null), and
363 * returns previous with a ref (if non-null).
369 mach_port_mscount_t sync
,
371 ipc_port_t
*previousp
)
374 mach_port_mscount_t mscount
;
376 assert(ip_active(port
));
378 previous
= port
->ip_nsrequest
;
379 mscount
= port
->ip_mscount
;
381 if ((port
->ip_srights
== 0) && (sync
<= mscount
) &&
382 (notify
!= IP_NULL
)) {
383 port
->ip_nsrequest
= IP_NULL
;
385 ipc_notify_no_senders(notify
, mscount
);
387 port
->ip_nsrequest
= notify
;
391 *previousp
= previous
;
396 * Routine: ipc_port_clear_receiver
398 * Prepares a receive right for transmission/destruction.
400 * The port is locked and active.
404 ipc_port_clear_receiver(
409 assert(ip_active(port
));
412 * pull ourselves from any sets.
414 if (port
->ip_pset_count
!= 0) {
415 ipc_pset_remove_from_all(port
);
416 assert(port
->ip_pset_count
== 0);
420 * Send anyone waiting on the port's queue directly away.
421 * Also clear the mscount and seqno.
424 imq_lock(&port
->ip_messages
);
425 ipc_mqueue_changed(&port
->ip_messages
);
426 ipc_port_set_mscount(port
, 0);
427 port
->ip_messages
.imq_seqno
= 0;
428 imq_unlock(&port
->ip_messages
);
433 * Routine: ipc_port_init
435 * Initializes a newly-allocated port.
436 * Doesn't touch the ip_object fields.
443 mach_port_name_t name
)
445 /* port->ip_kobject doesn't have to be initialized */
447 port
->ip_receiver
= space
;
448 port
->ip_receiver_name
= name
;
450 port
->ip_mscount
= 0;
451 port
->ip_srights
= 0;
452 port
->ip_sorights
= 0;
454 port
->ip_nsrequest
= IP_NULL
;
455 port
->ip_pdrequest
= IP_NULL
;
456 port
->ip_dnrequests
= IPR_NULL
;
458 port
->ip_pset_count
= 0;
459 port
->ip_premsg
= IKM_NULL
;
462 ipc_port_init_debug(port
);
463 #endif /* MACH_ASSERT */
465 ipc_mqueue_init(&port
->ip_messages
, FALSE
/* set */);
469 * Routine: ipc_port_alloc
473 * Nothing locked. If successful, the port is returned
474 * locked. (The caller doesn't have a reference.)
476 * KERN_SUCCESS The port is allocated.
477 * KERN_INVALID_TASK The space is dead.
478 * KERN_NO_SPACE No room for an entry in the space.
479 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
485 mach_port_name_t
*namep
,
489 mach_port_name_t name
;
492 kr
= ipc_object_alloc(space
, IOT_PORT
,
493 MACH_PORT_TYPE_RECEIVE
, 0,
494 &name
, (ipc_object_t
*) &port
);
495 if (kr
!= KERN_SUCCESS
)
500 ipc_port_init(port
, space
, name
);
509 * Routine: ipc_port_alloc_name
511 * Allocate a port, with a specific name.
513 * Nothing locked. If successful, the port is returned
514 * locked. (The caller doesn't have a reference.)
516 * KERN_SUCCESS The port is allocated.
517 * KERN_INVALID_TASK The space is dead.
518 * KERN_NAME_EXISTS The name already denotes a right.
519 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
525 mach_port_name_t name
,
531 kr
= ipc_object_alloc_name(space
, IOT_PORT
,
532 MACH_PORT_TYPE_RECEIVE
, 0,
533 name
, (ipc_object_t
*) &port
);
534 if (kr
!= KERN_SUCCESS
)
539 ipc_port_init(port
, space
, name
);
547 * Generate dead name notifications. Called from ipc_port_destroy.
548 * Port is unlocked but still has reference(s);
549 * dnrequests was taken from port while the port
550 * was locked but the port now has port->ip_dnrequests set to IPR_NULL.
554 __unused ipc_port_t port
,
555 ipc_port_request_t dnrequests
)
557 ipc_table_size_t its
= dnrequests
->ipr_size
;
558 ipc_table_elems_t size
= its
->its_size
;
559 ipc_port_request_index_t index
;
561 for (index
= 1; index
< size
; index
++) {
562 ipc_port_request_t ipr
= &dnrequests
[index
];
563 mach_port_name_t name
= ipr
->ipr_name
;
566 if (name
== MACH_PORT_NULL
)
569 soright
= ipr
->ipr_soright
;
570 assert(soright
!= IP_NULL
);
572 ipc_notify_dead_name(soright
, name
);
575 it_dnrequests_free(its
, dnrequests
);
579 * Routine: ipc_port_destroy
581 * Destroys a port. Cleans up queued messages.
583 * If the port has a backup, it doesn't get destroyed,
584 * but is sent in a port-destroyed notification to the backup.
586 * The port is locked and alive; nothing else locked.
587 * The caller has a reference, which is consumed.
588 * Afterwards, the port is unlocked and dead.
595 ipc_port_t pdrequest
, nsrequest
;
598 ipc_port_request_t dnrequests
;
600 assert(ip_active(port
));
601 /* port->ip_receiver_name is garbage */
602 /* port->ip_receiver/port->ip_destination is garbage */
603 assert(port
->ip_pset_count
== 0);
604 assert(port
->ip_mscount
== 0);
606 /* first check for a backup port */
608 pdrequest
= port
->ip_pdrequest
;
609 if (pdrequest
!= IP_NULL
) {
610 /* we assume the ref for pdrequest */
611 port
->ip_pdrequest
= IP_NULL
;
613 /* make port be in limbo */
614 port
->ip_receiver_name
= MACH_PORT_NULL
;
615 port
->ip_destination
= IP_NULL
;
618 /* consumes our refs for port and pdrequest */
619 ipc_notify_port_destroyed(pdrequest
, port
);
623 /* once port is dead, we don't need to keep it locked */
625 port
->ip_object
.io_bits
&= ~IO_BITS_ACTIVE
;
626 port
->ip_timestamp
= ipc_port_timestamp();
629 dnrequests
= port
->ip_dnrequests
;
630 port
->ip_dnrequests
= IPR_NULL
;
633 * If the port has a preallocated message buffer and that buffer
634 * is not inuse, free it. If it has an inuse one, then the kmsg
635 * free will detect that we freed the association and it can free it
636 * like a normal buffer.
638 if (IP_PREALLOC(port
)) {
639 kmsg
= port
->ip_premsg
;
640 assert(kmsg
!= IKM_NULL
);
641 IP_CLEAR_PREALLOC(port
, kmsg
);
642 if (!ikm_prealloc_inuse(kmsg
))
647 /* throw away no-senders request */
649 nsrequest
= port
->ip_nsrequest
;
650 if (nsrequest
!= IP_NULL
)
651 ipc_notify_send_once(nsrequest
); /* consumes ref */
653 /* destroy any queued messages */
654 mqueue
= &port
->ip_messages
;
655 ipc_mqueue_destroy(mqueue
);
657 /* generate dead-name notifications */
658 if (dnrequests
!= IPR_NULL
) {
659 ipc_port_dnnotify(port
, dnrequests
);
662 ipc_kobject_destroy(port
);
664 ipc_port_release(port
); /* consume caller's ref */
668 * Routine: ipc_port_check_circularity
670 * Check if queueing "port" in a message for "dest"
671 * would create a circular group of ports and messages.
673 * If no circularity (FALSE returned), then "port"
674 * is changed from "in limbo" to "in transit".
676 * That is, we want to set port->ip_destination == dest,
677 * but guaranteeing that this doesn't create a circle
678 * port->ip_destination->ip_destination->... == port
680 * No ports locked. References held for "port" and "dest".
684 ipc_port_check_circularity(
690 assert(port
!= IP_NULL
);
691 assert(dest
!= IP_NULL
);
698 * First try a quick check that can run in parallel.
699 * No circularity if dest is not in transit.
703 if (ip_lock_try(dest
)) {
704 if (!ip_active(dest
) ||
705 (dest
->ip_receiver_name
!= MACH_PORT_NULL
) ||
706 (dest
->ip_destination
== IP_NULL
))
709 /* dest is in transit; further checking necessary */
715 ipc_port_multiple_lock(); /* massive serialization */
718 * Search for the end of the chain (a port not in transit),
719 * acquiring locks along the way.
725 if (!ip_active(base
) ||
726 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
727 (base
->ip_destination
== IP_NULL
))
730 base
= base
->ip_destination
;
733 /* all ports in chain from dest to base, inclusive, are locked */
736 /* circularity detected! */
738 ipc_port_multiple_unlock();
740 /* port (== base) is in limbo */
742 assert(ip_active(port
));
743 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
744 assert(port
->ip_destination
== IP_NULL
);
746 while (dest
!= IP_NULL
) {
749 /* dest is in transit or in limbo */
751 assert(ip_active(dest
));
752 assert(dest
->ip_receiver_name
== MACH_PORT_NULL
);
754 next
= dest
->ip_destination
;
763 * The guarantee: lock port while the entire chain is locked.
764 * Once port is locked, we can take a reference to dest,
765 * add port to the chain, and unlock everything.
769 ipc_port_multiple_unlock();
773 /* port is in limbo */
775 assert(ip_active(port
));
776 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
777 assert(port
->ip_destination
== IP_NULL
);
780 port
->ip_destination
= dest
;
782 /* now unlock chain */
784 while (port
!= base
) {
787 /* port is in transit */
789 assert(ip_active(port
));
790 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
791 assert(port
->ip_destination
!= IP_NULL
);
793 next
= port
->ip_destination
;
798 /* base is not in transit */
800 assert(!ip_active(base
) ||
801 (base
->ip_receiver_name
!= MACH_PORT_NULL
) ||
802 (base
->ip_destination
== IP_NULL
));
809 * Routine: ipc_port_lookup_notify
811 * Make a send-once notify port from a receive right.
812 * Returns IP_NULL if name doesn't denote a receive right.
814 * The space must be locked (read or write) and active.
815 * Being the active space, we can rely on thread server_id
816 * context to give us the proper server level sub-order
821 ipc_port_lookup_notify(
823 mach_port_name_t name
)
828 assert(space
->is_active
);
830 entry
= ipc_entry_lookup(space
, name
);
831 if (entry
== IE_NULL
)
833 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
836 port
= (ipc_port_t
) entry
->ie_object
;
837 assert(port
!= IP_NULL
);
840 assert(ip_active(port
));
841 assert(port
->ip_receiver_name
== name
);
842 assert(port
->ip_receiver
== space
);
852 * Routine: ipc_port_make_send_locked
854 * Make a naked send right from a receive right.
857 * port locked and active.
860 ipc_port_make_send_locked(
863 assert(ip_active(port
));
872 * Routine: ipc_port_make_send
874 * Make a naked send right from a receive right.
886 if (ip_active(port
)) {
898 * Routine: ipc_port_copy_send
900 * Make a naked send right from another naked send right.
903 * dead port -> IP_DEAD
904 * live port -> port + ref
906 * Nothing locked except possibly a space.
919 if (ip_active(port
)) {
920 assert(port
->ip_srights
> 0);
933 * Routine: ipc_port_copyout_send
935 * Copyout a naked send right (possibly null/dead),
936 * or if that fails, destroy the right.
942 ipc_port_copyout_send(
946 mach_port_name_t name
;
948 if (IP_VALID(sright
)) {
951 kr
= ipc_object_copyout(space
, (ipc_object_t
) sright
,
952 MACH_MSG_TYPE_PORT_SEND
, TRUE
, &name
);
953 if (kr
!= KERN_SUCCESS
) {
954 ipc_port_release_send(sright
);
956 if (kr
== KERN_INVALID_CAPABILITY
)
957 name
= MACH_PORT_DEAD
;
959 name
= MACH_PORT_NULL
;
962 name
= (mach_port_name_t
) sright
;
968 * Routine: ipc_port_release_send
970 * Release a (valid) naked send right.
971 * Consumes a ref for the port.
977 ipc_port_release_send(
980 ipc_port_t nsrequest
= IP_NULL
;
981 mach_port_mscount_t mscount
;
983 assert(IP_VALID(port
));
988 if (!ip_active(port
)) {
989 ip_check_unlock(port
);
993 assert(port
->ip_srights
> 0);
995 if (--port
->ip_srights
== 0 &&
996 port
->ip_nsrequest
!= IP_NULL
) {
997 nsrequest
= port
->ip_nsrequest
;
998 port
->ip_nsrequest
= IP_NULL
;
999 mscount
= port
->ip_mscount
;
1001 ipc_notify_no_senders(nsrequest
, mscount
);
1007 * Routine: ipc_port_make_sonce
1009 * Make a naked send-once right from a receive right.
1011 * The port is not locked but it is active.
1015 ipc_port_make_sonce(
1018 assert(IP_VALID(port
));
1021 assert(ip_active(port
));
1022 port
->ip_sorights
++;
1030 * Routine: ipc_port_release_sonce
1032 * Release a naked send-once right.
1033 * Consumes a ref for the port.
1035 * In normal situations, this is never used.
1036 * Send-once rights are only consumed when
1037 * a message (possibly a send-once notification)
1040 * Nothing locked except possibly a space.
1044 ipc_port_release_sonce(
1047 assert(IP_VALID(port
));
1051 assert(port
->ip_sorights
> 0);
1053 port
->ip_sorights
--;
1057 if (!ip_active(port
)) {
1058 ip_check_unlock(port
);
1066 * Routine: ipc_port_release_receive
1068 * Release a naked (in limbo or in transit) receive right.
1069 * Consumes a ref for the port; destroys the port.
1075 ipc_port_release_receive(
1080 assert(IP_VALID(port
));
1083 assert(ip_active(port
));
1084 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
1085 dest
= port
->ip_destination
;
1087 ipc_port_destroy(port
); /* consumes ref, unlocks */
1089 if (dest
!= IP_NULL
)
1090 ipc_port_release(dest
);
1094 * Routine: ipc_port_alloc_special
1096 * Allocate a port in a special space.
1097 * The new port is returned with one ref.
1098 * If unsuccessful, IP_NULL is returned.
1104 ipc_port_alloc_special(
1109 port
= (ipc_port_t
) io_alloc(IOT_PORT
);
1110 if (port
== IP_NULL
)
1113 bzero((char *)port
, sizeof(*port
));
1114 io_lock_init(&port
->ip_object
);
1115 port
->ip_references
= 1;
1116 port
->ip_object
.io_bits
= io_makebits(TRUE
, IOT_PORT
, 0);
1118 ipc_port_init(port
, space
, 1);
1124 * Routine: ipc_port_dealloc_special
1126 * Deallocate a port in a special space.
1127 * Consumes one ref for the port.
1133 ipc_port_dealloc_special(
1135 __assert_only ipc_space_t space
)
1138 assert(ip_active(port
));
1139 // assert(port->ip_receiver_name != MACH_PORT_NULL);
1140 assert(port
->ip_receiver
== space
);
1143 * We clear ip_receiver_name and ip_receiver to simplify
1144 * the ipc_space_kernel check in ipc_mqueue_send.
1147 port
->ip_receiver_name
= MACH_PORT_NULL
;
1148 port
->ip_receiver
= IS_NULL
;
1150 /* relevant part of ipc_port_clear_receiver */
1151 ipc_port_set_mscount(port
, 0);
1152 port
->ip_messages
.imq_seqno
= 0;
1154 ipc_port_destroy(port
);
1159 #include <kern/machine.h>
1162 * Keep a list of all allocated ports.
1163 * Allocation is intercepted via ipc_port_init;
1164 * deallocation is intercepted via io_free.
1166 queue_head_t port_alloc_queue
;
1167 decl_mutex_data(,port_alloc_queue_lock
)
1169 unsigned long port_count
= 0;
1170 unsigned long port_count_warning
= 20000;
1171 unsigned long port_timestamp
= 0;
1173 void db_port_stack_trace(
1178 unsigned int verbose
,
1179 unsigned int display
,
1180 unsigned int ref_search
,
1181 unsigned int ref_target
);
1184 * Initialize global state needed for run-time
1188 ipc_port_debug_init(void)
1190 queue_init(&port_alloc_queue
);
1191 mutex_init(&port_alloc_queue_lock
, 0);
1196 * Initialize all of the debugging state in a port.
1197 * Insert the port into a global list of all allocated ports.
1200 ipc_port_init_debug(
1205 port
->ip_thread
= current_thread();
1206 port
->ip_timetrack
= port_timestamp
++;
1207 for (i
= 0; i
< IP_CALLSTACK_MAX
; ++i
)
1208 port
->ip_callstack
[i
] = 0;
1209 for (i
= 0; i
< IP_NSPARES
; ++i
)
1210 port
->ip_spares
[i
] = 0;
1213 * Machine-dependent routine to fill in an
1214 * array with up to IP_CALLSTACK_MAX levels
1215 * of return pc information.
1217 machine_callstack(&port
->ip_callstack
[0], IP_CALLSTACK_MAX
);
1220 mutex_lock(&port_alloc_queue_lock
);
1222 if (port_count_warning
> 0 && port_count
>= port_count_warning
)
1223 assert(port_count
< port_count_warning
);
1224 queue_enter(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
1225 mutex_unlock(&port_alloc_queue_lock
);
1231 * Remove a port from the queue of allocated ports.
1232 * This routine should be invoked JUST prior to
1233 * deallocating the actual memory occupied by the port.
1237 ipc_port_track_dealloc(
1238 __unused ipc_port_t port
)
1243 ipc_port_track_dealloc(
1246 mutex_lock(&port_alloc_queue_lock
);
1247 assert(port_count
> 0);
1249 queue_remove(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
);
1250 mutex_unlock(&port_alloc_queue_lock
);
1254 #endif /* MACH_ASSERT */
1259 #include <ddb/db_output.h>
1260 #include <ddb/db_print.h>
1262 #define printf kdbprintf
1265 db_port_queue_print(
1269 * Routine: ipc_port_print
1271 * Pretty-print a port for kdb.
1273 int ipc_port_print_long
= 0; /* set for more detail */
1278 __unused boolean_t have_addr
,
1279 __unused db_expr_t count
,
1287 int i
, needs_db_indent
, items_printed
;
1288 #endif /* MACH_ASSERT */
1290 if (db_option(modif
, 'l') || db_option(modif
, 'v'))
1293 printf("port 0x%x\n", port
);
1297 ipc_object_print(&port
->ip_object
);
1299 if (ipc_port_print_long
) {
1303 if (!ip_active(port
)) {
1304 iprintf("timestamp=0x%x", port
->ip_timestamp
);
1305 } else if (port
->ip_receiver_name
== MACH_PORT_NULL
) {
1306 iprintf("destination=0x%x (", port
->ip_destination
);
1307 if (port
->ip_destination
!= MACH_PORT_NULL
&&
1308 (task
= db_task_from_space(port
->ip_destination
->
1309 ip_receiver
, &task_id
)))
1310 printf("task%d at 0x%x", task_id
, task
);
1315 iprintf("receiver=0x%x (", port
->ip_receiver
);
1316 if (port
->ip_receiver
== ipc_space_kernel
)
1318 else if (port
->ip_receiver
== ipc_space_reply
)
1320 else if (port
->ip_receiver
== default_pager_space
)
1321 printf("default_pager");
1322 else if ((task
= db_task_from_space(port
->ip_receiver
, &task_id
)) != (db_addr_t
)0)
1323 printf("task%d at 0x%x", task_id
, task
);
1328 printf(", receiver_name=0x%x\n", port
->ip_receiver_name
);
1330 iprintf("mscount=%d", port
->ip_mscount
);
1331 printf(", srights=%d", port
->ip_srights
);
1332 printf(", sorights=%d\n", port
->ip_sorights
);
1334 iprintf("nsrequest=0x%x", port
->ip_nsrequest
);
1335 printf(", pdrequest=0x%x", port
->ip_pdrequest
);
1336 printf(", dnrequests=0x%x\n", port
->ip_dnrequests
);
1338 iprintf("pset_count=0x%x", port
->ip_pset_count
);
1339 printf(", seqno=%d", port
->ip_messages
.imq_seqno
);
1340 printf(", msgcount=%d", port
->ip_messages
.imq_msgcount
);
1341 printf(", qlimit=%d\n", port
->ip_messages
.imq_qlimit
);
1343 iprintf("kmsgs=0x%x", port
->ip_messages
.imq_messages
.ikmq_base
);
1344 printf(", rcvrs queue=0x%x", port
->ip_messages
.imq_wait_queue
);
1345 printf(", kobj=0x%x\n", port
->ip_kobject
);
1347 iprintf("premsg=0x%x", port
->ip_premsg
);
1350 /* don't bother printing callstack or queue links */
1351 iprintf("ip_thread=0x%x, ip_timetrack=0x%x\n",
1352 port
->ip_thread
, port
->ip_timetrack
);
1354 needs_db_indent
= 1;
1355 for (i
= 0; i
< IP_NSPARES
; ++i
) {
1356 if (port
->ip_spares
[i
] != 0) {
1357 if (needs_db_indent
) {
1359 needs_db_indent
= 0;
1361 printf("%sip_spares[%d] = %d",
1362 items_printed
? ", " : "", i
,
1363 port
->ip_spares
[i
]);
1364 if (++items_printed
>= 4) {
1365 needs_db_indent
= 1;
1371 #endif /* MACH_ASSERT */
1374 iprintf("kmsg queue contents:\n");
1376 nmsgs
= db_port_queue_print(port
);
1378 iprintf("...total kmsgs: %d\n", nmsgs
);
1387 mach_port_name_t name
)
1392 if (task
== TASK_NULL
) {
1393 db_printf("port_name_to_data: task is null\n");
1396 if ((space
= task
->itk_space
) == 0) {
1397 db_printf("port_name_to_data: task->itk_space is null\n");
1400 if (!space
->is_active
) {
1401 db_printf("port_name_to_data: task->itk_space not active\n");
1404 if ((entry
= ipc_entry_lookup(space
, name
)) == 0) {
1405 db_printf("port_name_to_data: lookup yields zero\n");
1408 return ((ipc_port_t
)entry
->ie_object
);
1413 print_type_ports(type
, dead
)
1421 for (port
= (ipc_port_t
)first_element(ipc_object_zones
[IOT_PORT
]);
1423 port
= (ipc_port_t
)next_element(ipc_object_zones
[IOT_PORT
],
1425 if (ip_kotype(port
) == type
&&
1426 (!dead
|| !ip_active(port
))) {
1428 printf("0x%x\t", port
);
1430 printf("0x%x\n", port
);
1440 int total_port_count
;
1441 int space_null_count
;
1442 int space_kernel_count
;
1443 int space_reply_count
;
1444 int space_pager_count
;
1445 int space_other_count
;
1450 } port_types
[IKOT_MAX_TYPE
];
1452 total_port_count
= 0;
1454 bzero((char *)&port_types
[0], sizeof(port_types
));
1455 space_null_count
= 0;
1456 space_kernel_count
= 0;
1457 space_reply_count
= 0;
1458 space_pager_count
= 0;
1459 space_other_count
= 0;
1461 for (port
= (ipc_port_t
)first_element(ipc_object_zones
[IOT_PORT
]);
1463 port
= (ipc_port_t
)next_element(ipc_object_zones
[IOT_PORT
],
1466 if (ip_kotype(port
) >= IKOT_MAX_TYPE
) {
1467 port_types
[IKOT_UNKNOWN
].total_count
++;
1468 if (!io_active(&port
->ip_object
))
1469 port_types
[IKOT_UNKNOWN
].dead_count
++;
1471 port_types
[ip_kotype(port
)].total_count
++;
1472 if (!io_active(&port
->ip_object
))
1473 port_types
[ip_kotype(port
)].dead_count
++;
1476 if (!port
->ip_receiver
)
1478 else if (port
->ip_receiver
== ipc_space_kernel
)
1479 space_kernel_count
++;
1480 else if (port
->ip_receiver
== ipc_space_reply
)
1481 space_reply_count
++;
1482 else if (port
->ip_receiver
== default_pager_space
)
1483 space_pager_count
++;
1485 space_other_count
++;
1487 printf("\n%7d total ports\n\n", total_port_count
);
1489 #define PRINT_ONE_PORT_TYPE(name) \
1490 printf("%7d %s", port_types[IKOT_##name].total_count, # name); \
1491 if (port_types[IKOT_##name].dead_count) \
1492 printf(" (%d dead ports)", port_types[IKOT_##name].dead_count);\
1495 PRINT_ONE_PORT_TYPE(NONE
);
1496 PRINT_ONE_PORT_TYPE(THREAD
);
1497 PRINT_ONE_PORT_TYPE(TASK
);
1498 PRINT_ONE_PORT_TYPE(HOST
);
1499 PRINT_ONE_PORT_TYPE(HOST_PRIV
);
1500 PRINT_ONE_PORT_TYPE(PROCESSOR
);
1501 PRINT_ONE_PORT_TYPE(PSET
);
1502 PRINT_ONE_PORT_TYPE(PSET_NAME
);
1503 PRINT_ONE_PORT_TYPE(TIMER
);
1504 PRINT_ONE_PORT_TYPE(PAGING_REQUEST
);
1505 PRINT_ONE_PORT_TYPE(MIG
);
1506 PRINT_ONE_PORT_TYPE(MEMORY_OBJECT
);
1507 PRINT_ONE_PORT_TYPE(XMM_PAGER
);
1508 PRINT_ONE_PORT_TYPE(XMM_KERNEL
);
1509 PRINT_ONE_PORT_TYPE(XMM_REPLY
);
1510 PRINT_ONE_PORT_TYPE(UND_REPLY
);
1511 PRINT_ONE_PORT_TYPE(HOST_NOTIFY
);
1512 PRINT_ONE_PORT_TYPE(HOST_SECURITY
);
1513 PRINT_ONE_PORT_TYPE(LEDGER
);
1514 PRINT_ONE_PORT_TYPE(MASTER_DEVICE
);
1515 PRINT_ONE_PORT_TYPE(TASK_NAME
);
1516 PRINT_ONE_PORT_TYPE(SUBSYSTEM
);
1517 PRINT_ONE_PORT_TYPE(IO_DONE_QUEUE
);
1518 PRINT_ONE_PORT_TYPE(SEMAPHORE
);
1519 PRINT_ONE_PORT_TYPE(LOCK_SET
);
1520 PRINT_ONE_PORT_TYPE(CLOCK
);
1521 PRINT_ONE_PORT_TYPE(CLOCK_CTRL
);
1522 PRINT_ONE_PORT_TYPE(IOKIT_SPARE
);
1523 PRINT_ONE_PORT_TYPE(NAMED_ENTRY
);
1524 PRINT_ONE_PORT_TYPE(IOKIT_CONNECT
);
1525 PRINT_ONE_PORT_TYPE(IOKIT_OBJECT
);
1526 PRINT_ONE_PORT_TYPE(UPL
);
1527 PRINT_ONE_PORT_TYPE(MEM_OBJ_CONTROL
);
1529 PRINT_ONE_PORT_TYPE(UNKNOWN
);
1530 printf("\nipc_space:\n\n");
1531 printf("NULL KERNEL REPLY PAGER OTHER\n");
1532 printf("%d %d %d %d %d\n",
1541 #endif /* ZONE_DEBUG */
1545 * Print out all the kmsgs in a queue. Aggregate kmsgs with
1546 * identical message ids into a single entry. Count up the
1547 * amount of inline and out-of-line data consumed by each
1552 #define KMSG_MATCH_FIELD(kmsg) (kmsg->ikm_header->msgh_id)
1553 #define DKQP_LONG(kmsg) FALSE
1554 const char *dkqp_long_format
= "(%3d) <%10d> 0x%x %10d %10d\n";
1555 const char *dkqp_format
= "(%3d) <%10d> 0x%x %10d %10d\n";
1558 db_kmsg_queue_print(
1561 db_kmsg_queue_print(
1564 ipc_kmsg_t ikmsg
, first_kmsg
;
1565 register int icount
;
1566 mach_msg_id_t cur_id
;
1567 unsigned int inline_total
, ool_total
;
1570 iprintf("Count msgh_id kmsg addr inline bytes ool bytes\n");
1571 inline_total
= ool_total
= (vm_size_t
) 0;
1572 cur_id
= KMSG_MATCH_FIELD(kmsg
);
1573 for (icount
= 0, nmsgs
= 0, first_kmsg
= ikmsg
= kmsg
;
1574 kmsg
!= IKM_NULL
&& (kmsg
!= first_kmsg
|| nmsgs
== 0);
1575 kmsg
= kmsg
->ikm_next
) {
1577 if (!(KMSG_MATCH_FIELD(kmsg
) == cur_id
)) {
1578 iprintf(DKQP_LONG(kmsg
) ? dkqp_long_format
:dkqp_format
,
1579 icount
, cur_id
, ikmsg
, inline_total
,ool_total
);
1580 cur_id
= KMSG_MATCH_FIELD(kmsg
);
1583 inline_total
= ool_total
= 0;
1587 if (DKQP_LONG(kmsg
))
1588 inline_total
+= kmsg
->ikm_size
;
1590 inline_total
+= kmsg
->ikm_header
->msgh_size
;
1592 iprintf(DKQP_LONG(kmsg
) ? dkqp_long_format
: dkqp_format
,
1593 icount
, cur_id
, ikmsg
, inline_total
, ool_total
);
1599 * Process all of the messages on a port - prints out the
1600 * number of occurences of each message type, and the first
1601 * kmsg with a particular msgh_id.
1604 db_port_queue_print(
1609 if (ipc_kmsg_queue_empty(&port
->ip_messages
.imq_messages
))
1611 kmsg
= ipc_kmsg_queue_first(&port
->ip_messages
.imq_messages
);
1612 return db_kmsg_queue_print(kmsg
);
1617 #include <ddb/db_sym.h>
1618 #include <ddb/db_access.h>
1620 #define FUNC_NULL ((void (*)) 0)
1621 #define MAX_REFS 5 /* bins for tracking ref counts */
1624 * Translate port's cache of call stack pointers
1625 * into symbolic names.
1628 db_port_stack_trace(
1633 for (i
= 0; i
< IP_CALLSTACK_MAX
; ++i
) {
1634 iprintf("[%d] 0x%x\t", i
, port
->ip_callstack
[i
]);
1635 if (port
->ip_callstack
[i
] != 0 &&
1636 DB_VALID_KERN_ADDR(port
->ip_callstack
[i
]))
1637 db_printsym(port
->ip_callstack
[i
], DB_STGY_PROC
);
1643 typedef struct port_item
{
1645 unsigned long count
;
1649 #define ITEM_MAX 400
1650 typedef struct port_track
{
1653 unsigned long warning
;
1654 port_item items
[ITEM_MAX
];
1657 port_track port_callers
; /* match against calling addresses */
1658 port_track port_threads
; /* match against allocating threads */
1659 port_track port_spaces
; /* match against ipc spaces */
1661 void port_track_init(
1666 unsigned long item
);
1667 void port_track_sort(
1668 port_track
*trackp
);
1669 void port_track_print(
1671 void (*func
)(port_item
*));
1672 void port_callers_print(
1682 trackp
->max
= trackp
->warning
= 0;
1683 trackp
->name
= name
;
1684 for (i
= trackp
->items
; i
< trackp
->items
+ ITEM_MAX
; ++i
)
1685 i
->item
= i
->count
= 0;
1694 port_item
*limit
, *i
;
1696 limit
= trackp
->items
+ trackp
->max
;
1697 for (i
= trackp
->items
; i
< limit
; ++i
)
1698 if (i
->item
== item
) {
1702 if (trackp
->max
>= ITEM_MAX
) {
1703 if (trackp
->warning
++ == 0)
1704 iprintf("%s: no room\n", trackp
->name
);
1714 * Simple (and slow) bubble sort.
1720 port_item
*limit
, *p
;
1724 limit
= trackp
->items
+ trackp
->max
- 1;
1727 for (p
= trackp
->items
; p
< limit
- 1; ++p
) {
1728 if (p
->count
< (p
+1)->count
) {
1735 } while (unsorted
== TRUE
);
1742 void (*func
)(port_item
*))
1744 port_item
*limit
, *p
;
1746 limit
= trackp
->items
+ trackp
->max
;
1747 iprintf("%s:\n", trackp
->name
);
1748 for (p
= trackp
->items
; p
< limit
; ++p
) {
1749 if (func
!= FUNC_NULL
)
1752 iprintf("0x%x\t%8d\n", p
->item
, p
->count
);
1761 iprintf("0x%x\t%8d\t", p
->item
, p
->count
);
1762 db_printsym(p
->item
, DB_STGY_PROC
);
1768 * Show all ports with a given reference count.
1774 db_port_walk(1, 1, 1, refs
);
1779 * Examine all currently allocated ports.
1781 * verbose display suspicious ports
1782 * display print out each port encountered
1783 * ref_search restrict examination to ports with
1784 * a specified reference count
1785 * ref_target reference count for ref_search
1789 unsigned int verbose
,
1790 unsigned int display
,
1791 unsigned int ref_search
,
1792 unsigned int ref_target
)
1795 unsigned int ref_overflow
, refs
, i
, ref_inactive_overflow
;
1796 unsigned int no_receiver
, no_match
;
1797 unsigned int ref_counts
[MAX_REFS
];
1798 unsigned int inactive
[MAX_REFS
];
1799 unsigned int ipc_ports
= 0;
1801 iprintf("Allocated port count is %d\n", port_count
);
1802 no_receiver
= no_match
= ref_overflow
= 0;
1803 ref_inactive_overflow
= 0;
1804 for (i
= 0; i
< MAX_REFS
; ++i
) {
1808 port_track_init(&port_callers
, "port callers");
1809 port_track_init(&port_threads
, "port threads");
1810 port_track_init(&port_spaces
, "port spaces");
1812 iprintf("Walking ports of ref_count=%d.\n", ref_target
);
1814 iprintf("Walking all ports.\n");
1816 queue_iterate(&port_alloc_queue
, port
, ipc_port_t
, ip_port_links
) {
1817 const char *port_type
;
1819 port_type
= " IPC port";
1820 if (ip_active(port
))
1823 refs
= port
->ip_references
;
1824 if (ref_search
&& refs
!= ref_target
)
1827 if (refs
>= MAX_REFS
) {
1828 if (ip_active(port
))
1831 ++ref_inactive_overflow
;
1833 if (refs
== 0 && verbose
)
1834 iprintf("%s 0x%x has ref count of zero!\n",
1836 if (ip_active(port
))
1841 port_item_add(&port_threads
, (unsigned long) port
->ip_thread
);
1842 for (i
= 0; i
< IP_CALLSTACK_MAX
; ++i
) {
1843 if (port
->ip_callstack
[i
] != 0 &&
1844 DB_VALID_KERN_ADDR(port
->ip_callstack
[i
]))
1845 port_item_add(&port_callers
,
1846 port
->ip_callstack
[i
]);
1848 if (!ip_active(port
)) {
1850 iprintf("%s 0x%x, inactive, refcnt %d\n",
1851 port_type
, port
, refs
);
1855 if (port
->ip_receiver_name
== MACH_PORT_NULL
) {
1856 iprintf("%s 0x%x, no receiver, refcnt %d\n",
1861 if (port
->ip_receiver
== ipc_space_kernel
||
1862 port
->ip_receiver
== ipc_space_reply
||
1863 ipc_entry_lookup(port
->ip_receiver
,
1864 port
->ip_receiver_name
)
1866 port_item_add(&port_spaces
,
1867 (unsigned long)port
->ip_receiver
);
1869 iprintf( "%s 0x%x time 0x%x ref_cnt %d\n",
1871 port
->ip_timetrack
, refs
);
1875 iprintf("%s 0x%x, rcvr 0x%x, name 0x%x, ref %d, no match\n",
1876 port_type
, port
, port
->ip_receiver
,
1877 port
->ip_receiver_name
, refs
);
1880 iprintf("Active port type summary:\n");
1881 iprintf("\tlocal IPC %6d\n", ipc_ports
);
1882 iprintf("summary:\tcallers %d threads %d spaces %d\n",
1883 port_callers
.max
, port_threads
.max
, port_spaces
.max
);
1885 iprintf("\tref_counts:\n");
1886 for (i
= 0; i
< MAX_REFS
; ++i
)
1887 iprintf("\t ref_counts[%d] = %d\n", i
, ref_counts
[i
]);
1889 iprintf("\t%d ports w/o receivers, %d w/o matches\n",
1890 no_receiver
, no_match
);
1892 iprintf("\tinactives:");
1893 if ( ref_inactive_overflow
|| inactive
[0] || inactive
[1] ||
1894 inactive
[2] || inactive
[3] || inactive
[4] )
1895 printf(" [0]=%d [1]=%d [2]=%d [3]=%d [4]=%d [5+]=%d\n",
1896 inactive
[0], inactive
[1], inactive
[2],
1897 inactive
[3], inactive
[4], ref_inactive_overflow
);
1899 printf(" No inactive ports.\n");
1901 port_track_sort(&port_spaces
);
1902 port_track_print(&port_spaces
, FUNC_NULL
);
1903 port_track_sort(&port_threads
);
1904 port_track_print(&port_threads
, FUNC_NULL
);
1905 port_track_sort(&port_callers
);
1906 port_track_print(&port_callers
, port_callers_print
);
1911 #endif /* MACH_ASSERT */
1913 #endif /* MACH_KDB */