1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
80#include <ipc/port.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>
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
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
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{
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;
137}
138#endif /* MACH_IPC_DEBUG */
139
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
154#if !MACH_IPC_DEBUG
155kern_return_t
156mach_port_space_info(
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(
169	ipc_space_t			space,
170	ipc_info_space_t		*infop,
171	ipc_info_name_array_t		*tablep,
172	mach_msg_type_number_t 		*tableCntp,
173	__unused ipc_info_tree_name_array_t	*treep,
174	__unused mach_msg_type_number_t         *treeCntp)
175{
176	ipc_info_name_t *table_info;
177	vm_offset_t table_addr;
178	vm_size_t table_size, table_size_needed;
179	ipc_entry_t table;
180	ipc_entry_num_t tsize;
181	mach_port_index_t index;
182	kern_return_t kr;
183	vm_map_copy_t copy;
184
185
186	if (space == IS_NULL)
187		return KERN_INVALID_TASK;
188
189	/* start with in-line memory */
190
191	table_size = 0;
192
193	for (;;) {
194		is_read_lock(space);
195		if (!is_active(space)) {
196			is_read_unlock(space);
197			if (table_size != 0)
198				kmem_free(ipc_kernel_map,
199					  table_addr, table_size);
200			return KERN_INVALID_TASK;
201		}
202
203		table_size_needed = round_page(space->is_table_size
204					       * sizeof(ipc_info_name_t));
205
206		if (table_size_needed == table_size)
207			break;
208
209		is_read_unlock(space);
210
211		if (table_size != table_size_needed) {
212			if (table_size != 0)
213				kmem_free(ipc_kernel_map, table_addr, table_size);
214			kr = kmem_alloc(ipc_kernel_map,	&table_addr, table_size_needed);
215			if (kr != KERN_SUCCESS) {
216				return KERN_RESOURCE_SHORTAGE;
217			}
218			table_size = table_size_needed;
219		}
220
221	}
222	/* space is read-locked and active; we have enough wired memory */
223
224	/* get the overall space info */
225	infop->iis_genno_mask = MACH_PORT_NGEN(MACH_PORT_DEAD);
226	infop->iis_table_size = space->is_table_size;
227	infop->iis_table_next = space->is_table_next->its_size;
228
229	/* walk the table for this space */
230	table = space->is_table;
231	tsize = space->is_table_size;
232	table_info = (ipc_info_name_array_t)table_addr;
233	for (index = 0; index < tsize; index++) {
234		ipc_info_name_t *iin = &table_info[index];
235		ipc_entry_t entry = &table[index];
236		ipc_entry_bits_t bits;
237
238		bits = entry->ie_bits;
239		iin->iin_name = MACH_PORT_MAKE(index, IE_BITS_GEN(bits));
240		iin->iin_type = IE_BITS_TYPE(bits);
241		if ((entry->ie_bits & MACH_PORT_TYPE_PORT_RIGHTS) != MACH_PORT_TYPE_NONE &&
242		    entry->ie_request != IE_REQ_NONE) {
243			ipc_port_t port = (ipc_port_t) entry->ie_object;
244
245			assert(IP_VALID(port));
246			ip_lock(port);
247			iin->iin_type |= ipc_port_request_type(port, iin->iin_name, entry->ie_request);
248			ip_unlock(port);
249		}
250
251		iin->iin_urefs = IE_BITS_UREFS(bits);
252		iin->iin_object = (natural_t)VM_KERNEL_ADDRPERM((uintptr_t)entry->ie_object);
253		iin->iin_next = entry->ie_next;
254		iin->iin_hash = entry->ie_index;
255	}
256
257	is_read_unlock(space);
258
259	/* prepare the table out-of-line data for return */
260	if (table_size > 0) {
261		if (table_size > infop->iis_table_size * sizeof(ipc_info_name_t))
262			bzero((char *)&table_info[infop->iis_table_size],
263			      table_size - infop->iis_table_size * sizeof(ipc_info_name_t));
264
265		kr = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(table_addr),
266				   vm_map_round_page(table_addr + table_size), FALSE);
267		assert(kr == KERN_SUCCESS);
268		kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)table_addr,
269				   (vm_map_size_t)table_size, TRUE, &copy);
270		assert(kr == KERN_SUCCESS);
271		*tablep = (ipc_info_name_t *)copy;
272		*tableCntp = infop->iis_table_size;
273	} else {
274		*tablep = (ipc_info_name_t *)0;
275		*tableCntp = 0;
276	}
277
278	/* splay tree is obsolete, no work to do... */
279	*treep = (ipc_info_tree_name_t *)0;
280	*treeCntp = 0;
281	return KERN_SUCCESS;
282}
283#endif /* MACH_IPC_DEBUG */
284
285/*
286 *	Routine:	mach_port_dnrequest_info
287 *	Purpose:
288 *		Returns information about the dead-name requests
289 *		registered with the named receive right.
290 *	Conditions:
291 *		Nothing locked.
292 *	Returns:
293 *		KERN_SUCCESS		Retrieved information.
294 *		KERN_INVALID_TASK	The space is null.
295 *		KERN_INVALID_TASK	The space is dead.
296 *		KERN_INVALID_NAME	The name doesn't denote a right.
297 *		KERN_INVALID_RIGHT	Name doesn't denote receive rights.
298 */
299
300#if !MACH_IPC_DEBUG
301kern_return_t
302mach_port_dnrequest_info(
303	__unused ipc_space_t		space,
304	__unused mach_port_name_t	name,
305	__unused unsigned int	*totalp,
306	__unused unsigned int	*usedp)
307{
308        return KERN_FAILURE;
309}
310#else
311kern_return_t
312mach_port_dnrequest_info(
313	ipc_space_t			space,
314	mach_port_name_t		name,
315	unsigned int			*totalp,
316	unsigned int			*usedp)
317{
318	unsigned int total, used;
319	ipc_port_t port;
320	kern_return_t kr;
321
322	if (space == IS_NULL)
323		return KERN_INVALID_TASK;
324
325	kr = ipc_port_translate_receive(space, name, &port);
326	if (kr != KERN_SUCCESS)
327		return kr;
328	/* port is locked and active */
329
330	if (port->ip_requests == IPR_NULL) {
331		total = 0;
332		used = 0;
333	} else {
334		ipc_port_request_t requests = port->ip_requests;
335		ipc_port_request_index_t index;
336
337		total = requests->ipr_size->its_size;
338
339		for (index = 1, used = 0;
340		     index < total; index++) {
341			ipc_port_request_t ipr = &requests[index];
342
343			if (ipr->ipr_name != MACH_PORT_NULL)
344				used++;
345		}
346	}
347	ip_unlock(port);
348
349	*totalp = total;
350	*usedp = used;
351	return KERN_SUCCESS;
352}
353#endif /* MACH_IPC_DEBUG */
354
355/*
356 *	Routine:	mach_port_kobject [kernel call]
357 *	Purpose:
358 *		Retrieve the type and address of the kernel object
359 *		represented by a send or receive right. Returns
360 *		the kernel address in a mach_vm_address_t to
361 *		mask potential differences in kernel address space
362 *		size.
363 *	Conditions:
364 *		Nothing locked.
365 *	Returns:
366 *		KERN_SUCCESS		Retrieved kernel object info.
367 *		KERN_INVALID_TASK	The space is null.
368 *		KERN_INVALID_TASK	The space is dead.
369 *		KERN_INVALID_NAME	The name doesn't denote a right.
370 *		KERN_INVALID_RIGHT	Name doesn't denote
371 *					send or receive rights.
372 */
373
374#if !MACH_IPC_DEBUG
375kern_return_t
376mach_port_kobject(
377	__unused ipc_space_t		space,
378	__unused mach_port_name_t	name,
379	__unused natural_t		*typep,
380	__unused mach_vm_address_t	*addrp)
381{
382        return KERN_FAILURE;
383}
384#else
385kern_return_t
386mach_port_kobject(
387	ipc_space_t			space,
388	mach_port_name_t		name,
389	natural_t			*typep,
390	mach_vm_address_t		*addrp)
391{
392	ipc_entry_t entry;
393	ipc_port_t port;
394	kern_return_t kr;
395	mach_vm_address_t kaddr;
396
397	if (space == IS_NULL)
398		return KERN_INVALID_TASK;
399
400	kr = ipc_right_lookup_read(space, name, &entry);
401	if (kr != KERN_SUCCESS)
402		return kr;
403	/* space is read-locked and active */
404
405	if ((entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE) == 0) {
406		is_read_unlock(space);
407		return KERN_INVALID_RIGHT;
408	}
409
410	port = (ipc_port_t) entry->ie_object;
411	assert(port != IP_NULL);
412
413	ip_lock(port);
414	is_read_unlock(space);
415
416	if (!ip_active(port)) {
417		ip_unlock(port);
418		return KERN_INVALID_RIGHT;
419	}
420
421	*typep = (unsigned int) ip_kotype(port);
422	kaddr = (mach_vm_address_t)port->ip_kobject;
423	ip_unlock(port);
424
425	if (0 != kaddr && is_ipc_kobject(*typep))
426		*addrp = VM_KERNEL_ADDRPERM(VM_KERNEL_UNSLIDE(kaddr));
427	else
428		*addrp = 0;
429
430	return KERN_SUCCESS;
431}
432#endif /* MACH_IPC_DEBUG */
433/*
434 *	Routine:	mach_port_kernel_object [Legacy kernel call]
435 *	Purpose:
436 *		Retrieve the type and address of the kernel object
437 *		represented by a send or receive right. Hard-coded
438 *		to return only the low-order 32-bits of the kernel
439 *		object.
440 *	Conditions:
441 *		Nothing locked.
442 *	Returns:
443 *		KERN_SUCCESS		Retrieved kernel object info.
444 *		KERN_INVALID_TASK	The space is null.
445 *		KERN_INVALID_TASK	The space is dead.
446 *		KERN_INVALID_NAME	The name doesn't denote a right.
447 *		KERN_INVALID_RIGHT	Name doesn't denote
448 *					send or receive rights.
449 */
450
451#if !MACH_IPC_DEBUG
452kern_return_t
453mach_port_kernel_object(
454	__unused ipc_space_t		space,
455	__unused mach_port_name_t	name,
456	__unused unsigned int		*typep,
457	__unused unsigned int		*addrp)
458{
459        return KERN_FAILURE;
460}
461#else
462kern_return_t
463mach_port_kernel_object(
464	ipc_space_t			space,
465	mach_port_name_t		name,
466	unsigned int			*typep,
467	unsigned int			*addrp)
468{
469	mach_vm_address_t addr = 0;
470	kern_return_t kr;
471
472	kr = mach_port_kobject(space, name, typep, &addr);
473	*addrp = (unsigned int) addr;
474	return kr;
475}
476#endif /* MACH_IPC_DEBUG */
477