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