2 * Copyright (c) 2000-2004 Apple Computer, 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 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.
59 * File: ipc/mach_debug.c
63 * Exported IPC debug calls.
65 #include <mach_ipc_debug.h>
67 #include <mach/vm_param.h>
68 #include <mach/kern_return.h>
69 #include <mach/machine/vm_types.h>
70 #include <mach/mach_host_server.h>
71 #include <mach/mach_port_server.h>
72 #include <mach_debug/ipc_info.h>
73 #include <mach_debug/hash_info.h>
76 #include <kern/host.h>
77 #include <kern/misc_protos.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_kern.h>
81 #include <ipc/ipc_types.h>
82 #include <ipc/ipc_space.h>
83 #include <ipc/ipc_port.h>
84 #include <ipc/ipc_hash.h>
85 #include <ipc/ipc_table.h>
86 #include <ipc/ipc_right.h>
88 #include <security/mac_mach_internal.h>
89 #include <device/device_types.h>
93 * Routine: mach_port_get_srights [kernel call]
95 * Retrieve the number of extant send rights
96 * that a receive right has.
100 * KERN_SUCCESS Retrieved number of send rights.
101 * KERN_INVALID_TASK The space is null.
102 * KERN_INVALID_TASK The space is dead.
103 * KERN_INVALID_NAME The name doesn't denote a right.
104 * KERN_INVALID_RIGHT Name doesn't denote receive rights.
109 mach_port_get_srights(
110 __unused ipc_space_t space
,
111 __unused mach_port_name_t name
,
112 __unused mach_port_rights_t
*srightsp
)
118 mach_port_get_srights(
120 mach_port_name_t name
,
121 mach_port_rights_t
*srightsp
)
125 mach_port_rights_t srights
;
127 if (space
== IS_NULL
) {
128 return KERN_INVALID_TASK
;
131 kr
= ipc_port_translate_receive(space
, name
, &port
);
132 if (kr
!= KERN_SUCCESS
) {
135 /* port is locked and active */
137 srights
= port
->ip_srights
;
143 #endif /* MACH_IPC_DEBUG */
147 * Routine: mach_port_space_info
149 * Returns information about an IPC space.
151 * Nothing locked. Obeys CountInOut protocol.
153 * KERN_SUCCESS Returned information.
154 * KERN_INVALID_TASK The space is null.
155 * KERN_INVALID_TASK The space is dead.
156 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
161 mach_port_space_info_from_user(
162 __unused mach_port_t port
,
163 __unused ipc_info_space_t
*infop
,
164 __unused ipc_info_name_array_t
*tablep
,
165 __unused mach_msg_type_number_t
*tableCntp
,
166 __unused ipc_info_tree_name_array_t
*treep
,
167 __unused mach_msg_type_number_t
*treeCntp
)
174 mach_port_space_info(
176 ipc_info_space_t
*infop
,
177 ipc_info_name_array_t
*tablep
,
178 mach_msg_type_number_t
*tableCntp
,
179 __unused ipc_info_tree_name_array_t
*treep
,
180 __unused mach_msg_type_number_t
*treeCntp
);
183 mach_port_space_info(
185 ipc_info_space_t
*infop
,
186 ipc_info_name_array_t
*tablep
,
187 mach_msg_type_number_t
*tableCntp
,
188 __unused ipc_info_tree_name_array_t
*treep
,
189 __unused mach_msg_type_number_t
*treeCntp
)
191 ipc_info_name_t
*table_info
;
192 vm_offset_t table_addr
;
193 vm_size_t table_size
, table_size_needed
;
195 ipc_entry_num_t tsize
;
196 mach_port_index_t index
;
201 if (space
== IS_NULL
) {
202 return KERN_INVALID_TASK
;
205 #if !(DEVELOPMENT || DEBUG) && CONFIG_MACF
206 const boolean_t dbg_ok
= (mac_task_check_expose_task(kernel_task
, TASK_FLAVOR_CONTROL
) == 0);
208 const boolean_t dbg_ok
= TRUE
;
211 /* start with in-line memory */
217 if (!is_active(space
)) {
218 is_read_unlock(space
);
219 if (table_size
!= 0) {
220 kmem_free(ipc_kernel_map
,
221 table_addr
, table_size
);
223 return KERN_INVALID_TASK
;
227 vm_map_round_page((space
->is_table_size
228 * sizeof(ipc_info_name_t
)),
229 VM_MAP_PAGE_MASK(ipc_kernel_map
));
231 if (table_size_needed
== table_size
) {
235 is_read_unlock(space
);
237 if (table_size
!= table_size_needed
) {
238 if (table_size
!= 0) {
239 kmem_free(ipc_kernel_map
, table_addr
, table_size
);
241 kr
= kmem_alloc(ipc_kernel_map
, &table_addr
, table_size_needed
, VM_KERN_MEMORY_IPC
);
242 if (kr
!= KERN_SUCCESS
) {
243 return KERN_RESOURCE_SHORTAGE
;
245 table_size
= table_size_needed
;
248 /* space is read-locked and active; we have enough wired memory */
250 /* get the overall space info */
251 infop
->iis_genno_mask
= MACH_PORT_NGEN(MACH_PORT_DEAD
);
252 infop
->iis_table_size
= space
->is_table_size
;
253 infop
->iis_table_next
= space
->is_table_next
->its_size
;
255 /* walk the table for this space */
256 table
= space
->is_table
;
257 tsize
= space
->is_table_size
;
258 table_info
= (ipc_info_name_array_t
)table_addr
;
259 for (index
= 0; index
< tsize
; index
++) {
260 ipc_info_name_t
*iin
= &table_info
[index
];
261 ipc_entry_t entry
= &table
[index
];
262 ipc_entry_bits_t bits
;
264 bits
= entry
->ie_bits
;
265 iin
->iin_name
= MACH_PORT_MAKE(index
, IE_BITS_GEN(bits
));
266 iin
->iin_collision
= 0;
267 iin
->iin_type
= IE_BITS_TYPE(bits
);
268 if ((entry
->ie_bits
& MACH_PORT_TYPE_PORT_RIGHTS
) != MACH_PORT_TYPE_NONE
&&
269 entry
->ie_request
!= IE_REQ_NONE
) {
270 ipc_port_t port
= ip_object_to_port(entry
->ie_object
);
272 assert(IP_VALID(port
));
274 iin
->iin_type
|= ipc_port_request_type(port
, iin
->iin_name
, entry
->ie_request
);
278 iin
->iin_urefs
= IE_BITS_UREFS(bits
);
279 iin
->iin_object
= (dbg_ok
) ? (natural_t
)VM_KERNEL_ADDRPERM((uintptr_t)entry
->ie_object
) : 0;
280 iin
->iin_next
= entry
->ie_next
;
281 iin
->iin_hash
= entry
->ie_index
;
284 is_read_unlock(space
);
286 /* prepare the table out-of-line data for return */
287 if (table_size
> 0) {
288 vm_size_t used_table_size
;
290 used_table_size
= infop
->iis_table_size
* sizeof(ipc_info_name_t
);
291 if (table_size
> used_table_size
) {
292 bzero((char *)&table_info
[infop
->iis_table_size
],
293 table_size
- used_table_size
);
298 vm_map_trunc_page(table_addr
,
299 VM_MAP_PAGE_MASK(ipc_kernel_map
)),
300 vm_map_round_page(table_addr
+ table_size
,
301 VM_MAP_PAGE_MASK(ipc_kernel_map
)),
303 assert(kr
== KERN_SUCCESS
);
304 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)table_addr
,
305 (vm_map_size_t
)used_table_size
, TRUE
, ©
);
306 assert(kr
== KERN_SUCCESS
);
307 *tablep
= (ipc_info_name_t
*)copy
;
308 *tableCntp
= infop
->iis_table_size
;
310 *tablep
= (ipc_info_name_t
*)0;
314 /* splay tree is obsolete, no work to do... */
315 *treep
= (ipc_info_tree_name_t
*)0;
321 mach_port_space_info_from_user(
323 ipc_info_space_t
*infop
,
324 ipc_info_name_array_t
*tablep
,
325 mach_msg_type_number_t
*tableCntp
,
326 __unused ipc_info_tree_name_array_t
*treep
,
327 __unused mach_msg_type_number_t
*treeCntp
)
331 ipc_space_t space
= convert_port_to_space_check_type(port
, NULL
, TASK_FLAVOR_READ
, FALSE
);
333 if (space
== IPC_SPACE_NULL
) {
334 return KERN_INVALID_ARGUMENT
;
337 kr
= mach_port_space_info(space
, infop
, tablep
, tableCntp
, treep
, treeCntp
);
339 ipc_space_release(space
);
342 #endif /* MACH_IPC_DEBUG */
345 * Routine: mach_port_space_basic_info
347 * Returns basic information about an IPC space.
351 * KERN_SUCCESS Returned information.
352 * KERN_FAILURE The call is not supported.
353 * KERN_INVALID_TASK The space is dead.
358 mach_port_space_basic_info(
359 __unused ipc_space_t space
,
360 __unused ipc_info_space_basic_t
*infop
)
366 mach_port_space_basic_info(
368 ipc_info_space_basic_t
*infop
)
370 if (space
== IS_NULL
) {
371 return KERN_INVALID_TASK
;
376 if (!is_active(space
)) {
377 is_read_unlock(space
);
378 return KERN_INVALID_TASK
;
381 /* get the basic space info */
382 infop
->iisb_genno_mask
= MACH_PORT_NGEN(MACH_PORT_DEAD
);
383 infop
->iisb_table_size
= space
->is_table_size
;
384 infop
->iisb_table_next
= space
->is_table_next
->its_size
;
385 infop
->iisb_table_inuse
= space
->is_table_size
- space
->is_table_free
- 1;
386 infop
->iisb_reserved
[0] = 0;
387 infop
->iisb_reserved
[1] = 0;
389 is_read_unlock(space
);
393 #endif /* MACH_IPC_DEBUG */
396 * Routine: mach_port_dnrequest_info
398 * Returns information about the dead-name requests
399 * registered with the named receive right.
403 * KERN_SUCCESS Retrieved information.
404 * KERN_INVALID_TASK The space is null.
405 * KERN_INVALID_TASK The space is dead.
406 * KERN_INVALID_NAME The name doesn't denote a right.
407 * KERN_INVALID_RIGHT Name doesn't denote receive rights.
412 mach_port_dnrequest_info(
413 __unused ipc_space_t space
,
414 __unused mach_port_name_t name
,
415 __unused
unsigned int *totalp
,
416 __unused
unsigned int *usedp
)
422 mach_port_dnrequest_info(
424 mach_port_name_t name
,
425 unsigned int *totalp
,
428 unsigned int total
, used
;
432 if (space
== IS_NULL
) {
433 return KERN_INVALID_TASK
;
436 kr
= ipc_port_translate_receive(space
, name
, &port
);
437 if (kr
!= KERN_SUCCESS
) {
440 /* port is locked and active */
442 if (port
->ip_requests
== IPR_NULL
) {
446 ipc_port_request_t requests
= port
->ip_requests
;
447 ipc_port_request_index_t index
;
449 total
= requests
->ipr_size
->its_size
;
451 for (index
= 1, used
= 0;
452 index
< total
; index
++) {
453 ipc_port_request_t ipr
= &requests
[index
];
455 if (ipr
->ipr_name
!= MACH_PORT_NULL
) {
466 #endif /* MACH_IPC_DEBUG */
469 * Routine: mach_port_kobject [kernel call]
471 * Retrieve the type and address of the kernel object
472 * represented by a send or receive right. Returns
473 * the kernel address in a mach_vm_address_t to
474 * mask potential differences in kernel address space
479 * KERN_SUCCESS Retrieved kernel object info.
480 * KERN_INVALID_TASK The space is null.
481 * KERN_INVALID_TASK The space is dead.
482 * KERN_INVALID_NAME The name doesn't denote a right.
483 * KERN_INVALID_RIGHT Name doesn't denote
484 * send or receive rights.
489 mach_port_kobject_from_user(
490 __unused mach_port_t port
,
491 __unused mach_port_name_t name
,
492 __unused natural_t
*typep
,
493 __unused mach_vm_address_t
*addrp
)
499 mach_port_kobject_description_from_user(
500 __unused mach_port_t port
,
501 __unused mach_port_name_t name
,
502 __unused natural_t
*typep
,
503 __unused mach_vm_address_t
*addrp
,
504 __unused kobject_description_t des
)
510 mach_port_kobject_description(
512 mach_port_name_t name
,
514 mach_vm_address_t
*addrp
,
515 kobject_description_t desc
);
518 mach_port_kobject_description(
520 mach_port_name_t name
,
522 mach_vm_address_t
*addrp
,
523 kobject_description_t desc
)
528 mach_vm_address_t kaddr
;
529 io_object_t obj
= NULL
;
531 if (space
== IS_NULL
) {
532 return KERN_INVALID_TASK
;
535 kr
= ipc_right_lookup_read(space
, name
, &entry
);
536 if (kr
!= KERN_SUCCESS
) {
539 /* space is read-locked and active */
541 if ((entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
) == 0) {
542 is_read_unlock(space
);
543 return KERN_INVALID_RIGHT
;
546 port
= ip_object_to_port(entry
->ie_object
);
547 assert(port
!= IP_NULL
);
550 is_read_unlock(space
);
552 if (!ip_active(port
)) {
554 return KERN_INVALID_RIGHT
;
557 *typep
= (unsigned int) ip_kotype(port
);
558 kaddr
= (mach_vm_address_t
)ip_get_kobject(port
);
563 switch (ip_kotype(port
)) {
564 case IKOT_IOKIT_OBJECT
:
565 case IKOT_IOKIT_CONNECT
:
566 case IKOT_IOKIT_IDENT
:
567 case IKOT_UEXT_OBJECT
:
568 obj
= (io_object_t
) kaddr
;
569 iokit_add_reference(obj
, IKOT_IOKIT_OBJECT
);
576 #if (DEVELOPMENT || DEBUG)
577 *addrp
= VM_KERNEL_UNSLIDE_OR_PERM(kaddr
);
583 iokit_port_object_description(obj
, desc
);
584 iokit_remove_reference(obj
);
593 mach_port_name_t name
,
595 mach_vm_address_t
*addrp
);
600 mach_port_name_t name
,
602 mach_vm_address_t
*addrp
)
604 return mach_port_kobject_description(space
, name
, typep
, addrp
, NULL
);
608 mach_port_kobject_description_from_user(
610 mach_port_name_t name
,
612 mach_vm_address_t
*addrp
,
613 kobject_description_t desc
)
617 ipc_space_t space
= convert_port_to_space_check_type(port
, NULL
, TASK_FLAVOR_READ
, FALSE
);
619 if (space
== IPC_SPACE_NULL
) {
620 return KERN_INVALID_ARGUMENT
;
623 kr
= mach_port_kobject_description(space
, name
, typep
, addrp
, desc
);
625 ipc_space_release(space
);
630 mach_port_kobject_from_user(
632 mach_port_name_t name
,
634 mach_vm_address_t
*addrp
)
636 return mach_port_kobject_description_from_user(port
, name
, typep
, addrp
, NULL
);
639 #endif /* MACH_IPC_DEBUG */
642 * Routine: mach_port_kernel_object [Legacy kernel call]
644 * Retrieve the type and address of the kernel object
645 * represented by a send or receive right. Hard-coded
646 * to return only the low-order 32-bits of the kernel
651 * KERN_SUCCESS Retrieved kernel object info.
652 * KERN_INVALID_TASK The space is null.
653 * KERN_INVALID_TASK The space is dead.
654 * KERN_INVALID_NAME The name doesn't denote a right.
655 * KERN_INVALID_RIGHT Name doesn't denote
656 * send or receive rights.
661 mach_port_kernel_object_from_user(
662 __unused mach_port_t port
,
663 __unused mach_port_name_t name
,
664 __unused
unsigned int *typep
,
665 __unused
unsigned int *addrp
)
671 mach_port_kernel_object(
673 mach_port_name_t name
,
675 unsigned int *addrp
);
678 mach_port_kernel_object(
680 mach_port_name_t name
,
684 mach_vm_address_t addr
= 0;
687 kr
= mach_port_kobject(space
, name
, typep
, &addr
);
688 *addrp
= (unsigned int) addr
;
693 mach_port_kernel_object_from_user(
695 mach_port_name_t name
,
701 ipc_space_t space
= convert_port_to_space_check_type(port
, NULL
, TASK_FLAVOR_READ
, FALSE
);
703 if (space
== IPC_SPACE_NULL
) {
704 return KERN_INVALID_ARGUMENT
;
707 kr
= mach_port_kernel_object(space
, name
, typep
, addrp
);
709 ipc_space_release(space
);
712 #endif /* MACH_IPC_DEBUG */
714 #if (DEVELOPMENT || DEBUG)
716 mach_port_special_reply_port_reset_link(
718 mach_port_name_t name
,
719 boolean_t
*srp_lost_link
)
723 thread_t thread
= current_thread();
725 if (space
!= current_space()) {
726 return KERN_INVALID_TASK
;
729 if (!MACH_PORT_VALID(name
)) {
730 return KERN_INVALID_NAME
;
733 if (!IP_VALID(thread
->ith_special_reply_port
)) {
734 return KERN_INVALID_VALUE
;
737 kr
= ipc_port_translate_receive(space
, name
, &port
);
738 if (kr
!= KERN_SUCCESS
) {
742 if (thread
->ith_special_reply_port
!= port
) {
744 return KERN_INVALID_ARGUMENT
;
747 imq_lock(&port
->ip_messages
);
748 *srp_lost_link
= (port
->ip_srp_lost_link
== 1)? TRUE
: FALSE
;
749 port
->ip_srp_lost_link
= 0;
750 imq_unlock(&port
->ip_messages
);
757 mach_port_special_reply_port_reset_link(
758 __unused ipc_space_t space
,
759 __unused mach_port_name_t name
,
760 __unused boolean_t
*srp_lost_link
)
762 return KERN_NOT_SUPPORTED
;