]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/mach_debug.c
xnu-2782.10.72.tar.gz
[apple/xnu.git] / osfmk / ipc / mach_debug.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: ipc/mach_debug.c
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Exported IPC debug calls.
64 */
65#include <mach_ipc_debug.h>
66
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>
74
75#if MACH_IPC_DEBUG
76#include <kern/host.h>
77#include <kern/misc_protos.h>
78#include <vm/vm_map.h>
79#include <vm/vm_kern.h>
91447636
A
80#include <ipc/port.h>
81#include <ipc/ipc_types.h>
1c79356b
A
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>
87#endif
88
89/*
90 * Routine: mach_port_get_srights [kernel call]
91 * Purpose:
92 * Retrieve the number of extant send rights
93 * that a receive right has.
94 * Conditions:
95 * Nothing locked.
96 * Returns:
97 * KERN_SUCCESS Retrieved number of send rights.
98 * KERN_INVALID_TASK The space is null.
99 * KERN_INVALID_TASK The space is dead.
100 * KERN_INVALID_NAME The name doesn't denote a right.
101 * KERN_INVALID_RIGHT Name doesn't denote receive rights.
102 */
103
91447636
A
104#if !MACH_IPC_DEBUG
105kern_return_t
106mach_port_get_srights(
107 __unused ipc_space_t space,
108 __unused mach_port_name_t name,
109 __unused mach_port_rights_t *srightsp)
110{
111 return KERN_FAILURE;
112}
113#else
1c79356b
A
114kern_return_t
115mach_port_get_srights(
116 ipc_space_t space,
117 mach_port_name_t name,
118 mach_port_rights_t *srightsp)
119{
1c79356b
A
120 ipc_port_t port;
121 kern_return_t kr;
122 mach_port_rights_t srights;
123
124 if (space == IS_NULL)
125 return KERN_INVALID_TASK;
126
127 kr = ipc_port_translate_receive(space, name, &port);
128 if (kr != KERN_SUCCESS)
129 return kr;
130 /* port is locked and active */
131
132 srights = port->ip_srights;
133 ip_unlock(port);
134
135 *srightsp = srights;
136 return KERN_SUCCESS;
1c79356b 137}
91447636 138#endif /* MACH_IPC_DEBUG */
1c79356b 139
1c79356b
A
140
141/*
142 * Routine: mach_port_space_info
143 * Purpose:
144 * Returns information about an IPC space.
145 * Conditions:
146 * Nothing locked. Obeys CountInOut protocol.
147 * Returns:
148 * KERN_SUCCESS Returned information.
149 * KERN_INVALID_TASK The space is null.
150 * KERN_INVALID_TASK The space is dead.
151 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
152 */
153
91447636 154#if !MACH_IPC_DEBUG
1c79356b
A
155kern_return_t
156mach_port_space_info(
91447636
A
157 __unused ipc_space_t space,
158 __unused ipc_info_space_t *infop,
159 __unused ipc_info_name_array_t *tablep,
160 __unused mach_msg_type_number_t *tableCntp,
161 __unused ipc_info_tree_name_array_t *treep,
162 __unused mach_msg_type_number_t *treeCntp)
163{
164 return KERN_FAILURE;
165}
166#else
167kern_return_t
168mach_port_space_info(
2d21ac55
A
169 ipc_space_t space,
170 ipc_info_space_t *infop,
1c79356b
A
171 ipc_info_name_array_t *tablep,
172 mach_msg_type_number_t *tableCntp,
316670eb
A
173 __unused ipc_info_tree_name_array_t *treep,
174 __unused mach_msg_type_number_t *treeCntp)
1c79356b 175{
1c79356b 176 ipc_info_name_t *table_info;
1c79356b 177 vm_offset_t table_addr;
2d21ac55 178 vm_size_t table_size, table_size_needed;
1c79356b
A
179 ipc_entry_t table;
180 ipc_entry_num_t tsize;
181 mach_port_index_t index;
182 kern_return_t kr;
2d21ac55
A
183 vm_map_copy_t copy;
184
1c79356b
A
185
186 if (space == IS_NULL)
187 return KERN_INVALID_TASK;
188
189 /* start with in-line memory */
190
91447636 191 table_size = 0;
1c79356b
A
192
193 for (;;) {
194 is_read_lock(space);
316670eb 195 if (!is_active(space)) {
1c79356b 196 is_read_unlock(space);
2d21ac55 197 if (table_size != 0)
1c79356b
A
198 kmem_free(ipc_kernel_map,
199 table_addr, table_size);
1c79356b
A
200 return KERN_INVALID_TASK;
201 }
202
39236c6e
A
203 table_size_needed =
204 vm_map_round_page((space->is_table_size
205 * sizeof(ipc_info_name_t)),
206 VM_MAP_PAGE_MASK(ipc_kernel_map));
1c79356b 207
316670eb 208 if (table_size_needed == table_size)
1c79356b
A
209 break;
210
211 is_read_unlock(space);
212
2d21ac55
A
213 if (table_size != table_size_needed) {
214 if (table_size != 0)
215 kmem_free(ipc_kernel_map, table_addr, table_size);
216 kr = kmem_alloc(ipc_kernel_map, &table_addr, table_size_needed);
1c79356b 217 if (kr != KERN_SUCCESS) {
1c79356b
A
218 return KERN_RESOURCE_SHORTAGE;
219 }
2d21ac55 220 table_size = table_size_needed;
1c79356b 221 }
316670eb 222
1c79356b
A
223 }
224 /* space is read-locked and active; we have enough wired memory */
225
2d21ac55 226 /* get the overall space info */
1c79356b
A
227 infop->iis_genno_mask = MACH_PORT_NGEN(MACH_PORT_DEAD);
228 infop->iis_table_size = space->is_table_size;
229 infop->iis_table_next = space->is_table_next->its_size;
1c79356b 230
2d21ac55 231 /* walk the table for this space */
1c79356b
A
232 table = space->is_table;
233 tsize = space->is_table_size;
2d21ac55 234 table_info = (ipc_info_name_array_t)table_addr;
1c79356b
A
235 for (index = 0; index < tsize; index++) {
236 ipc_info_name_t *iin = &table_info[index];
237 ipc_entry_t entry = &table[index];
238 ipc_entry_bits_t bits;
239
240 bits = entry->ie_bits;
241 iin->iin_name = MACH_PORT_MAKE(index, IE_BITS_GEN(bits));
39236c6e 242 iin->iin_collision = 0;
1c79356b 243 iin->iin_type = IE_BITS_TYPE(bits);
6d2010ae
A
244 if ((entry->ie_bits & MACH_PORT_TYPE_PORT_RIGHTS) != MACH_PORT_TYPE_NONE &&
245 entry->ie_request != IE_REQ_NONE) {
246 ipc_port_t port = (ipc_port_t) entry->ie_object;
247
248 assert(IP_VALID(port));
249 ip_lock(port);
250 iin->iin_type |= ipc_port_request_type(port, iin->iin_name, entry->ie_request);
251 ip_unlock(port);
252 }
253
1c79356b 254 iin->iin_urefs = IE_BITS_UREFS(bits);
99c3a104 255 iin->iin_object = (natural_t)VM_KERNEL_ADDRPERM((uintptr_t)entry->ie_object);
1c79356b
A
256 iin->iin_next = entry->ie_next;
257 iin->iin_hash = entry->ie_index;
258 }
259
1c79356b
A
260 is_read_unlock(space);
261
2d21ac55
A
262 /* prepare the table out-of-line data for return */
263 if (table_size > 0) {
264 if (table_size > infop->iis_table_size * sizeof(ipc_info_name_t))
265 bzero((char *)&table_info[infop->iis_table_size],
266 table_size - infop->iis_table_size * sizeof(ipc_info_name_t));
1c79356b 267
39236c6e
A
268 kr = vm_map_unwire(
269 ipc_kernel_map,
270 vm_map_trunc_page(table_addr,
271 VM_MAP_PAGE_MASK(ipc_kernel_map)),
272 vm_map_round_page(table_addr + table_size,
273 VM_MAP_PAGE_MASK(ipc_kernel_map)),
274 FALSE);
1c79356b 275 assert(kr == KERN_SUCCESS);
91447636 276 kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)table_addr,
2d21ac55 277 (vm_map_size_t)table_size, TRUE, &copy);
1c79356b 278 assert(kr == KERN_SUCCESS);
2d21ac55
A
279 *tablep = (ipc_info_name_t *)copy;
280 *tableCntp = infop->iis_table_size;
1c79356b 281 } else {
2d21ac55
A
282 *tablep = (ipc_info_name_t *)0;
283 *tableCntp = 0;
284 }
1c79356b 285
316670eb
A
286 /* splay tree is obsolete, no work to do... */
287 *treep = (ipc_info_tree_name_t *)0;
288 *treeCntp = 0;
1c79356b 289 return KERN_SUCCESS;
1c79356b 290}
91447636 291#endif /* MACH_IPC_DEBUG */
1c79356b 292
fe8ab488
A
293/*
294 * Routine: mach_port_space_basic_info
295 * Purpose:
296 * Returns basic information about an IPC space.
297 * Conditions:
298 * Nothing locked.
299 * Returns:
300 * KERN_SUCCESS Returned information.
301 * KERN_FAILURE The call is not supported.
302 * KERN_INVALID_TASK The space is dead.
303 */
304
305#if !MACH_IPC_DEBUG
306kern_return_t
307mach_port_space_basic_info(
308 __unused ipc_space_t space,
309 __unused ipc_info_space_basic_t *infop)
310{
311 return KERN_FAILURE;
312}
313#else
314kern_return_t
315mach_port_space_basic_info(
316 ipc_space_t space,
317 ipc_info_space_basic_t *infop)
318{
319 if (space == IS_NULL)
320 return KERN_INVALID_TASK;
321
322
323 is_read_lock(space);
324 if (!is_active(space)) {
325 is_read_unlock(space);
326 return KERN_INVALID_TASK;
327 }
328
329 /* get the basic space info */
330 infop->iisb_genno_mask = MACH_PORT_NGEN(MACH_PORT_DEAD);
331 infop->iisb_table_size = space->is_table_size;
332 infop->iisb_table_next = space->is_table_next->its_size;
333 infop->iisb_table_inuse = space->is_table_size - space->is_table_free - 1;
334 infop->iisb_reserved[0] = 0;
335 infop->iisb_reserved[1] = 0;
336
337 is_read_unlock(space);
338
339 return KERN_SUCCESS;
340}
341#endif /* MACH_IPC_DEBUG */
342
1c79356b
A
343/*
344 * Routine: mach_port_dnrequest_info
345 * Purpose:
346 * Returns information about the dead-name requests
347 * registered with the named receive right.
348 * Conditions:
349 * Nothing locked.
350 * Returns:
351 * KERN_SUCCESS Retrieved information.
352 * KERN_INVALID_TASK The space is null.
353 * KERN_INVALID_TASK The space is dead.
354 * KERN_INVALID_NAME The name doesn't denote a right.
355 * KERN_INVALID_RIGHT Name doesn't denote receive rights.
356 */
357
91447636 358#if !MACH_IPC_DEBUG
1c79356b
A
359kern_return_t
360mach_port_dnrequest_info(
91447636
A
361 __unused ipc_space_t space,
362 __unused mach_port_name_t name,
363 __unused unsigned int *totalp,
364 __unused unsigned int *usedp)
1c79356b 365{
1c79356b 366 return KERN_FAILURE;
91447636 367}
1c79356b 368#else
91447636
A
369kern_return_t
370mach_port_dnrequest_info(
371 ipc_space_t space,
372 mach_port_name_t name,
373 unsigned int *totalp,
374 unsigned int *usedp)
375{
1c79356b
A
376 unsigned int total, used;
377 ipc_port_t port;
378 kern_return_t kr;
379
380 if (space == IS_NULL)
381 return KERN_INVALID_TASK;
382
383 kr = ipc_port_translate_receive(space, name, &port);
384 if (kr != KERN_SUCCESS)
385 return kr;
386 /* port is locked and active */
387
6d2010ae 388 if (port->ip_requests == IPR_NULL) {
1c79356b
A
389 total = 0;
390 used = 0;
391 } else {
6d2010ae 392 ipc_port_request_t requests = port->ip_requests;
1c79356b
A
393 ipc_port_request_index_t index;
394
6d2010ae 395 total = requests->ipr_size->its_size;
1c79356b
A
396
397 for (index = 1, used = 0;
398 index < total; index++) {
6d2010ae 399 ipc_port_request_t ipr = &requests[index];
1c79356b
A
400
401 if (ipr->ipr_name != MACH_PORT_NULL)
402 used++;
403 }
404 }
405 ip_unlock(port);
406
407 *totalp = total;
408 *usedp = used;
409 return KERN_SUCCESS;
1c79356b 410}
91447636 411#endif /* MACH_IPC_DEBUG */
1c79356b
A
412
413/*
b0d623f7 414 * Routine: mach_port_kobject [kernel call]
1c79356b
A
415 * Purpose:
416 * Retrieve the type and address of the kernel object
b0d623f7
A
417 * represented by a send or receive right. Returns
418 * the kernel address in a mach_vm_address_t to
419 * mask potential differences in kernel address space
420 * size.
1c79356b
A
421 * Conditions:
422 * Nothing locked.
423 * Returns:
424 * KERN_SUCCESS Retrieved kernel object info.
425 * KERN_INVALID_TASK The space is null.
426 * KERN_INVALID_TASK The space is dead.
427 * KERN_INVALID_NAME The name doesn't denote a right.
428 * KERN_INVALID_RIGHT Name doesn't denote
429 * send or receive rights.
430 */
431
91447636 432#if !MACH_IPC_DEBUG
1c79356b 433kern_return_t
b0d623f7 434mach_port_kobject(
91447636
A
435 __unused ipc_space_t space,
436 __unused mach_port_name_t name,
b0d623f7
A
437 __unused natural_t *typep,
438 __unused mach_vm_address_t *addrp)
1c79356b 439{
1c79356b 440 return KERN_FAILURE;
91447636 441}
1c79356b 442#else
91447636 443kern_return_t
b0d623f7 444mach_port_kobject(
91447636
A
445 ipc_space_t space,
446 mach_port_name_t name,
b0d623f7
A
447 natural_t *typep,
448 mach_vm_address_t *addrp)
91447636 449{
1c79356b
A
450 ipc_entry_t entry;
451 ipc_port_t port;
452 kern_return_t kr;
316670eb 453 mach_vm_address_t kaddr;
1c79356b 454
2d21ac55
A
455 if (space == IS_NULL)
456 return KERN_INVALID_TASK;
457
1c79356b
A
458 kr = ipc_right_lookup_read(space, name, &entry);
459 if (kr != KERN_SUCCESS)
460 return kr;
461 /* space is read-locked and active */
462
463 if ((entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE) == 0) {
464 is_read_unlock(space);
465 return KERN_INVALID_RIGHT;
466 }
467
468 port = (ipc_port_t) entry->ie_object;
469 assert(port != IP_NULL);
470
471 ip_lock(port);
472 is_read_unlock(space);
473
474 if (!ip_active(port)) {
475 ip_unlock(port);
476 return KERN_INVALID_RIGHT;
477 }
478
479 *typep = (unsigned int) ip_kotype(port);
316670eb 480 kaddr = (mach_vm_address_t)port->ip_kobject;
1c79356b 481 ip_unlock(port);
1c79356b 482
316670eb 483 if (0 != kaddr && is_ipc_kobject(*typep))
fe8ab488 484 *addrp = VM_KERNEL_UNSLIDE_OR_PERM(kaddr);
316670eb
A
485 else
486 *addrp = 0;
487
488 return KERN_SUCCESS;
1c79356b 489}
91447636 490#endif /* MACH_IPC_DEBUG */
b0d623f7
A
491/*
492 * Routine: mach_port_kernel_object [Legacy kernel call]
493 * Purpose:
494 * Retrieve the type and address of the kernel object
495 * represented by a send or receive right. Hard-coded
496 * to return only the low-order 32-bits of the kernel
497 * object.
498 * Conditions:
499 * Nothing locked.
500 * Returns:
501 * KERN_SUCCESS Retrieved kernel object info.
502 * KERN_INVALID_TASK The space is null.
503 * KERN_INVALID_TASK The space is dead.
504 * KERN_INVALID_NAME The name doesn't denote a right.
505 * KERN_INVALID_RIGHT Name doesn't denote
506 * send or receive rights.
507 */
508
509#if !MACH_IPC_DEBUG
510kern_return_t
511mach_port_kernel_object(
512 __unused ipc_space_t space,
513 __unused mach_port_name_t name,
514 __unused unsigned int *typep,
515 __unused unsigned int *addrp)
516{
517 return KERN_FAILURE;
518}
519#else
520kern_return_t
521mach_port_kernel_object(
522 ipc_space_t space,
523 mach_port_name_t name,
524 unsigned int *typep,
525 unsigned int *addrp)
526{
527 mach_vm_address_t addr = 0;
528 kern_return_t kr;
529
530 kr = mach_port_kobject(space, name, typep, &addr);
531 *addrp = (unsigned int) addr;
532 return kr;
533}
534#endif /* MACH_IPC_DEBUG */