2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: ipc/mach_debug.c
65 * Exported IPC debug calls.
67 #include <mach_ipc_debug.h>
69 #include <mach/vm_param.h>
70 #include <mach/kern_return.h>
71 #include <mach/machine/vm_types.h>
72 #include <mach/mach_host_server.h>
73 #include <mach/mach_port_server.h>
74 #include <mach_debug/ipc_info.h>
75 #include <mach_debug/hash_info.h>
78 #include <kern/host.h>
79 #include <kern/misc_protos.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_kern.h>
83 #include <ipc/ipc_types.h>
84 #include <ipc/ipc_space.h>
85 #include <ipc/ipc_port.h>
86 #include <ipc/ipc_hash.h>
87 #include <ipc/ipc_table.h>
88 #include <ipc/ipc_right.h>
92 * Routine: mach_port_get_srights [kernel call]
94 * Retrieve the number of extant send rights
95 * that a receive right has.
99 * KERN_SUCCESS Retrieved number of send rights.
100 * KERN_INVALID_TASK The space is null.
101 * KERN_INVALID_TASK The space is dead.
102 * KERN_INVALID_NAME The name doesn't denote a right.
103 * KERN_INVALID_RIGHT Name doesn't denote receive rights.
108 mach_port_get_srights(
109 __unused ipc_space_t space
,
110 __unused mach_port_name_t name
,
111 __unused mach_port_rights_t
*srightsp
)
117 mach_port_get_srights(
119 mach_port_name_t name
,
120 mach_port_rights_t
*srightsp
)
124 mach_port_rights_t srights
;
126 if (space
== IS_NULL
)
127 return KERN_INVALID_TASK
;
129 kr
= ipc_port_translate_receive(space
, name
, &port
);
130 if (kr
!= KERN_SUCCESS
)
132 /* port is locked and active */
134 srights
= port
->ip_srights
;
140 #endif /* MACH_IPC_DEBUG */
143 * Routine: host_ipc_hash_info
145 * Return information about the global reverse hash table.
147 * Nothing locked. Obeys CountInOut protocol.
149 * KERN_SUCCESS Returned information.
150 * KERN_INVALID_HOST The host is null.
151 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
157 __unused host_t host
,
158 __unused hash_info_bucket_array_t
*infop
,
159 __unused mach_msg_type_number_t
*countp
)
167 hash_info_bucket_array_t
*infop
,
168 mach_msg_type_number_t
*countp
)
172 hash_info_bucket_t
*info
;
173 unsigned int potential
, actual
;
176 if (host
== HOST_NULL
)
177 return KERN_INVALID_HOST
;
179 /* start with in-line data */
185 actual
= ipc_hash_info(info
, potential
);
186 if (actual
<= potential
)
189 /* allocate more memory */
192 kmem_free(ipc_kernel_map
, addr
, size
);
194 size
= round_page(actual
* sizeof *info
);
195 kr
= kmem_alloc_pageable(ipc_kernel_map
, &addr
, size
);
196 if (kr
!= KERN_SUCCESS
)
197 return KERN_RESOURCE_SHORTAGE
;
199 info
= (hash_info_bucket_t
*) addr
;
200 potential
= size
/sizeof *info
;
203 if (info
== *infop
) {
204 /* data fit in-line; nothing to deallocate */
207 } else if (actual
== 0) {
208 kmem_free(ipc_kernel_map
, addr
, size
);
215 used
= round_page(actual
* sizeof *info
);
218 kmem_free(ipc_kernel_map
, addr
+ used
, size
- used
);
220 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
221 (vm_map_size_t
)used
, TRUE
, ©
);
222 assert(kr
== KERN_SUCCESS
);
224 *infop
= (hash_info_bucket_t
*) copy
;
230 #endif /* MACH_IPC_DEBUG */
233 * Routine: mach_port_space_info
235 * Returns information about an IPC space.
237 * Nothing locked. Obeys CountInOut protocol.
239 * KERN_SUCCESS Returned information.
240 * KERN_INVALID_TASK The space is null.
241 * KERN_INVALID_TASK The space is dead.
242 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
247 mach_port_space_info(
248 __unused ipc_space_t space
,
249 __unused ipc_info_space_t
*infop
,
250 __unused ipc_info_name_array_t
*tablep
,
251 __unused mach_msg_type_number_t
*tableCntp
,
252 __unused ipc_info_tree_name_array_t
*treep
,
253 __unused mach_msg_type_number_t
*treeCntp
)
259 mach_port_space_info(
261 ipc_info_space_t
*infop
,
262 ipc_info_name_array_t
*tablep
,
263 mach_msg_type_number_t
*tableCntp
,
264 ipc_info_tree_name_array_t
*treep
,
265 mach_msg_type_number_t
*treeCntp
)
267 ipc_info_name_t
*table_info
;
268 unsigned int table_potential
, table_actual
;
269 vm_offset_t table_addr
;
270 vm_size_t table_size
;
271 ipc_info_tree_name_t
*tree_info
;
272 unsigned int tree_potential
, tree_actual
;
273 vm_offset_t tree_addr
;
275 ipc_tree_entry_t tentry
;
277 ipc_entry_num_t tsize
;
278 mach_port_index_t index
;
281 if (space
== IS_NULL
)
282 return KERN_INVALID_TASK
;
284 /* start with in-line memory */
287 table_info
= *tablep
;
288 table_potential
= *tableCntp
;
291 tree_potential
= *treeCntp
;
295 if (!space
->is_active
) {
296 is_read_unlock(space
);
297 if (table_info
!= *tablep
)
298 kmem_free(ipc_kernel_map
,
299 table_addr
, table_size
);
300 if (tree_info
!= *treep
)
301 kmem_free(ipc_kernel_map
,
302 tree_addr
, tree_size
);
303 return KERN_INVALID_TASK
;
306 table_actual
= space
->is_table_size
;
307 tree_actual
= space
->is_tree_total
;
309 if ((table_actual
<= table_potential
) &&
310 (tree_actual
<= tree_potential
))
313 is_read_unlock(space
);
315 if (table_actual
> table_potential
) {
316 if (table_info
!= *tablep
)
317 kmem_free(ipc_kernel_map
,
318 table_addr
, table_size
);
320 table_size
= round_page(table_actual
*
322 kr
= kmem_alloc(ipc_kernel_map
,
323 &table_addr
, table_size
);
324 if (kr
!= KERN_SUCCESS
) {
325 if (tree_info
!= *treep
)
326 kmem_free(ipc_kernel_map
,
327 tree_addr
, tree_size
);
329 return KERN_RESOURCE_SHORTAGE
;
332 table_info
= (ipc_info_name_t
*) table_addr
;
333 table_potential
= table_size
/sizeof *table_info
;
336 if (tree_actual
> tree_potential
) {
337 if (tree_info
!= *treep
)
338 kmem_free(ipc_kernel_map
,
339 tree_addr
, tree_size
);
341 tree_size
= round_page(tree_actual
*
343 kr
= kmem_alloc(ipc_kernel_map
,
344 &tree_addr
, tree_size
);
345 if (kr
!= KERN_SUCCESS
) {
346 if (table_info
!= *tablep
)
347 kmem_free(ipc_kernel_map
,
348 table_addr
, table_size
);
350 return KERN_RESOURCE_SHORTAGE
;
353 tree_info
= (ipc_info_tree_name_t
*) tree_addr
;
354 tree_potential
= tree_size
/sizeof *tree_info
;
357 /* space is read-locked and active; we have enough wired memory */
359 infop
->iis_genno_mask
= MACH_PORT_NGEN(MACH_PORT_DEAD
);
360 infop
->iis_table_size
= space
->is_table_size
;
361 infop
->iis_table_next
= space
->is_table_next
->its_size
;
362 infop
->iis_tree_size
= space
->is_tree_total
;
363 infop
->iis_tree_small
= space
->is_tree_small
;
364 infop
->iis_tree_hash
= space
->is_tree_hash
;
366 table
= space
->is_table
;
367 tsize
= space
->is_table_size
;
369 for (index
= 0; index
< tsize
; index
++) {
370 ipc_info_name_t
*iin
= &table_info
[index
];
371 ipc_entry_t entry
= &table
[index
];
372 ipc_entry_bits_t bits
;
374 bits
= entry
->ie_bits
;
375 iin
->iin_name
= MACH_PORT_MAKE(index
, IE_BITS_GEN(bits
));
376 iin
->iin_collision
= (bits
& IE_BITS_COLLISION
) ? TRUE
: FALSE
;
377 iin
->iin_type
= IE_BITS_TYPE(bits
);
378 iin
->iin_urefs
= IE_BITS_UREFS(bits
);
379 iin
->iin_object
= (vm_offset_t
) entry
->ie_object
;
380 iin
->iin_next
= entry
->ie_next
;
381 iin
->iin_hash
= entry
->ie_index
;
384 for (tentry
= ipc_splay_traverse_start(&space
->is_tree
), index
= 0;
386 tentry
= ipc_splay_traverse_next(&space
->is_tree
, FALSE
)) {
387 ipc_info_tree_name_t
*iitn
= &tree_info
[index
++];
388 ipc_info_name_t
*iin
= &iitn
->iitn_name
;
389 ipc_entry_t entry
= &tentry
->ite_entry
;
390 ipc_entry_bits_t bits
= entry
->ie_bits
;
392 assert(IE_BITS_TYPE(bits
) != MACH_PORT_TYPE_NONE
);
394 iin
->iin_name
= tentry
->ite_name
;
395 iin
->iin_collision
= (bits
& IE_BITS_COLLISION
) ? TRUE
: FALSE
;
396 iin
->iin_type
= IE_BITS_TYPE(bits
);
397 iin
->iin_urefs
= IE_BITS_UREFS(bits
);
398 iin
->iin_object
= (vm_offset_t
) entry
->ie_object
;
399 iin
->iin_next
= entry
->ie_next
;
400 iin
->iin_hash
= entry
->ie_index
;
402 if (tentry
->ite_lchild
== ITE_NULL
)
403 iitn
->iitn_lchild
= MACH_PORT_NULL
;
405 iitn
->iitn_lchild
= tentry
->ite_lchild
->ite_name
;
407 if (tentry
->ite_rchild
== ITE_NULL
)
408 iitn
->iitn_rchild
= MACH_PORT_NULL
;
410 iitn
->iitn_rchild
= tentry
->ite_rchild
->ite_name
;
413 ipc_splay_traverse_finish(&space
->is_tree
);
414 is_read_unlock(space
);
416 if (table_info
== *tablep
) {
417 /* data fit in-line; nothing to deallocate */
419 *tableCntp
= table_actual
;
420 } else if (table_actual
== 0) {
421 kmem_free(ipc_kernel_map
, table_addr
, table_size
);
425 vm_size_t size_used
, rsize_used
;
428 /* kmem_alloc doesn't zero memory */
430 size_used
= table_actual
* sizeof *table_info
;
431 rsize_used
= round_page(size_used
);
433 if (rsize_used
!= table_size
)
434 kmem_free(ipc_kernel_map
,
435 table_addr
+ rsize_used
,
436 table_size
- rsize_used
);
438 if (size_used
!= rsize_used
)
439 bzero((char *) (table_addr
+ size_used
),
440 rsize_used
- size_used
);
442 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(table_addr
),
443 vm_map_round_page(table_addr
+ rsize_used
), FALSE
);
444 assert(kr
== KERN_SUCCESS
);
446 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)table_addr
,
447 (vm_map_size_t
)rsize_used
, TRUE
, ©
);
448 assert(kr
== KERN_SUCCESS
);
450 *tablep
= (ipc_info_name_t
*) copy
;
451 *tableCntp
= table_actual
;
454 if (tree_info
== *treep
) {
455 /* data fit in-line; nothing to deallocate */
457 *treeCntp
= tree_actual
;
458 } else if (tree_actual
== 0) {
459 kmem_free(ipc_kernel_map
, tree_addr
, tree_size
);
463 vm_size_t size_used
, rsize_used
;
466 /* kmem_alloc doesn't zero memory */
468 size_used
= tree_actual
* sizeof *tree_info
;
469 rsize_used
= round_page(size_used
);
471 if (rsize_used
!= tree_size
)
472 kmem_free(ipc_kernel_map
,
473 tree_addr
+ rsize_used
,
474 tree_size
- rsize_used
);
476 if (size_used
!= rsize_used
)
477 bzero((char *) (tree_addr
+ size_used
),
478 rsize_used
- size_used
);
480 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(tree_addr
),
481 vm_map_round_page(tree_addr
+ rsize_used
), FALSE
);
482 assert(kr
== KERN_SUCCESS
);
484 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)tree_addr
,
485 (vm_map_size_t
)rsize_used
, TRUE
, ©
);
486 assert(kr
== KERN_SUCCESS
);
488 *treep
= (ipc_info_tree_name_t
*) copy
;
489 *treeCntp
= tree_actual
;
494 #endif /* MACH_IPC_DEBUG */
497 * Routine: mach_port_dnrequest_info
499 * Returns information about the dead-name requests
500 * registered with the named receive right.
504 * KERN_SUCCESS Retrieved information.
505 * KERN_INVALID_TASK The space is null.
506 * KERN_INVALID_TASK The space is dead.
507 * KERN_INVALID_NAME The name doesn't denote a right.
508 * KERN_INVALID_RIGHT Name doesn't denote receive rights.
513 mach_port_dnrequest_info(
514 __unused ipc_space_t space
,
515 __unused mach_port_name_t name
,
516 __unused
unsigned int *totalp
,
517 __unused
unsigned int *usedp
)
523 mach_port_dnrequest_info(
525 mach_port_name_t name
,
526 unsigned int *totalp
,
529 unsigned int total
, used
;
533 if (space
== IS_NULL
)
534 return KERN_INVALID_TASK
;
536 kr
= ipc_port_translate_receive(space
, name
, &port
);
537 if (kr
!= KERN_SUCCESS
)
539 /* port is locked and active */
541 if (port
->ip_dnrequests
== IPR_NULL
) {
545 ipc_port_request_t dnrequests
= port
->ip_dnrequests
;
546 ipc_port_request_index_t index
;
548 total
= dnrequests
->ipr_size
->its_size
;
550 for (index
= 1, used
= 0;
551 index
< total
; index
++) {
552 ipc_port_request_t ipr
= &dnrequests
[index
];
554 if (ipr
->ipr_name
!= MACH_PORT_NULL
)
564 #endif /* MACH_IPC_DEBUG */
567 * Routine: mach_port_kernel_object [kernel call]
569 * Retrieve the type and address of the kernel object
570 * represented by a send or receive right.
574 * KERN_SUCCESS Retrieved kernel object info.
575 * KERN_INVALID_TASK The space is null.
576 * KERN_INVALID_TASK The space is dead.
577 * KERN_INVALID_NAME The name doesn't denote a right.
578 * KERN_INVALID_RIGHT Name doesn't denote
579 * send or receive rights.
584 mach_port_kernel_object(
585 __unused ipc_space_t space
,
586 __unused mach_port_name_t name
,
587 __unused
unsigned int *typep
,
588 __unused vm_offset_t
*addrp
)
594 mach_port_kernel_object(
596 mach_port_name_t name
,
604 kr
= ipc_right_lookup_read(space
, name
, &entry
);
605 if (kr
!= KERN_SUCCESS
)
607 /* space is read-locked and active */
609 if ((entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
) == 0) {
610 is_read_unlock(space
);
611 return KERN_INVALID_RIGHT
;
614 port
= (ipc_port_t
) entry
->ie_object
;
615 assert(port
!= IP_NULL
);
618 is_read_unlock(space
);
620 if (!ip_active(port
)) {
622 return KERN_INVALID_RIGHT
;
625 *typep
= (unsigned int) ip_kotype(port
);
626 *addrp
= (vm_offset_t
) port
->ip_kobject
;
631 #endif /* MACH_IPC_DEBUG */