]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_port.h
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_port.h
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 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/ipc_port.h
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Definitions for ports.
64 */
65
66#ifndef _IPC_IPC_PORT_H_
67#define _IPC_IPC_PORT_H_
68
69#include <norma_vm.h>
70#include <mach_rt.h>
71#include <mach_assert.h>
72#include <mach_debug.h>
73
91447636 74#include <mach/mach_types.h>
1c79356b
A
75#include <mach/boolean.h>
76#include <mach/kern_return.h>
1c79356b 77#include <mach/port.h>
1c79356b 78
91447636
A
79#include <kern/kern_types.h>
80
81#include <ipc/ipc_types.h>
1c79356b
A
82#include <ipc/ipc_object.h>
83#include <ipc/ipc_mqueue.h>
1c79356b
A
84#include <ipc/ipc_space.h>
85
86/*
87 * A receive right (port) can be in four states:
88 * 1) dead (not active, ip_timestamp has death time)
89 * 2) in a space (ip_receiver_name != 0, ip_receiver points
90 * to the space but doesn't hold a ref for it)
91 * 3) in transit (ip_receiver_name == 0, ip_destination points
92 * to the destination port and holds a ref for it)
93 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
94 *
95 * If the port is active, and ip_receiver points to some space,
96 * then ip_receiver_name != 0, and that space holds receive rights.
97 * If the port is not active, then ip_timestamp contains a timestamp
98 * taken when the port was destroyed.
99 */
100
101typedef unsigned int ipc_port_timestamp_t;
102
1c79356b
A
103struct ipc_port {
104
105 /*
106 * Initial sub-structure in common with ipc_pset and rpc_port
107 * First element is an ipc_object
108 */
109 struct ipc_object ip_object;
110
111 union {
112 struct ipc_space *receiver;
113 struct ipc_port *destination;
114 ipc_port_timestamp_t timestamp;
115 } data;
116
117 ipc_kobject_t ip_kobject;
1c79356b
A
118 mach_port_mscount_t ip_mscount;
119 mach_port_rights_t ip_srights;
120 mach_port_rights_t ip_sorights;
121
122 struct ipc_port *ip_nsrequest;
123 struct ipc_port *ip_pdrequest;
124 struct ipc_port_request *ip_dnrequests;
125
126 unsigned int ip_pset_count;
127 struct ipc_mqueue ip_messages;
128 struct ipc_kmsg *ip_premsg;
129
130#if NORMA_VM
131 /*
132 * These fields are needed for the use of XMM.
133 * Few ports need this information; it should
134 * be kept in XMM instead (TBD). XXX
135 */
136 long ip_norma_xmm_object_refs;
137 struct ipc_port *ip_norma_xmm_object;
138#endif
139
140#if MACH_ASSERT
141#define IP_NSPARES 10
142#define IP_CALLSTACK_MAX 10
143 queue_chain_t ip_port_links; /* all allocated ports */
91447636 144 thread_t ip_thread; /* who made me? thread context */
1c79356b
A
145 unsigned long ip_timetrack; /* give an idea of "when" created */
146 natural_t ip_callstack[IP_CALLSTACK_MAX]; /* stack trace */
147 unsigned long ip_spares[IP_NSPARES]; /* for debugging */
148#endif /* MACH_ASSERT */
149 int alias;
150};
151
152
153#define ip_references ip_object.io_references
154#define ip_bits ip_object.io_bits
155#define ip_receiver_name ip_object.io_receiver_name
156
1c79356b
A
157#define ip_receiver data.receiver
158#define ip_destination data.destination
159#define ip_timestamp data.timestamp
160
161#define IP_NULL IPC_PORT_NULL
162#define IP_DEAD IPC_PORT_DEAD
163#define IP_VALID(port) IPC_PORT_VALID(port)
164
165#define ip_active(port) io_active(&(port)->ip_object)
166#define ip_lock_init(port) io_lock_init(&(port)->ip_object)
167#define ip_lock(port) io_lock(&(port)->ip_object)
168#define ip_lock_try(port) io_lock_try(&(port)->ip_object)
169#define ip_unlock(port) io_unlock(&(port)->ip_object)
170#define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
171
172#define ip_reference(port) io_reference(&(port)->ip_object)
173#define ip_release(port) io_release(&(port)->ip_object)
174
175#define ip_kotype(port) io_kotype(&(port)->ip_object)
176
177/*
178 * JMM - Preallocation flag
179 * This flag indicates that there is a message buffer preallocated for this
180 * port and we should use that when sending (from the kernel) rather than
181 * allocate a new one. This avoids deadlocks during notification message
182 * sends by critical system threads (which may be needed to free memory and
183 * therefore cannot be blocked waiting for memory themselves).
184 */
185#define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
186#define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
187
188#define IP_SET_PREALLOC(port, kmsg) \
189MACRO_BEGIN \
190 (port)->ip_bits |= IP_BIT_PREALLOC; \
191 (port)->ip_premsg = (kmsg); \
192MACRO_END
193
194#define IP_CLEAR_PREALLOC(port, kmsg) \
195MACRO_BEGIN \
196 assert((port)->ip_premsg == kmsg); \
197 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
198 (port)->ip_premsg = IKM_NULL; \
199MACRO_END
200
1c79356b 201
91447636 202struct ipc_port_request {
1c79356b
A
203 union {
204 struct ipc_port *port;
205 ipc_port_request_index_t index;
206 } notify;
207
208 union {
209 mach_port_name_t name;
210 struct ipc_table_size *size;
211 } name;
91447636 212};
1c79356b
A
213
214#define ipr_next notify.index
215#define ipr_size name.size
216
217#define ipr_soright notify.port
218#define ipr_name name.name
219
1c79356b
A
220/*
221 * Taking the ipc_port_multiple lock grants the privilege
222 * to lock multiple ports at once. No ports must locked
223 * when it is taken.
224 */
225
226decl_mutex_data(extern,ipc_port_multiple_lock_data)
227
228#define ipc_port_multiple_lock_init() \
91447636 229 mutex_init(&ipc_port_multiple_lock_data, 0)
1c79356b
A
230
231#define ipc_port_multiple_lock() \
232 mutex_lock(&ipc_port_multiple_lock_data)
233
234#define ipc_port_multiple_unlock() \
235 mutex_unlock(&ipc_port_multiple_lock_data)
236
237/*
238 * The port timestamp facility provides timestamps
239 * for port destruction. It is used to serialize
240 * mach_port_names with port death.
241 */
242
243decl_mutex_data(extern,ipc_port_timestamp_lock_data)
244extern ipc_port_timestamp_t ipc_port_timestamp_data;
245
246#define ipc_port_timestamp_lock_init() \
91447636 247 mutex_init(&ipc_port_timestamp_lock_data, 0)
1c79356b
A
248
249#define ipc_port_timestamp_lock() \
250 mutex_lock(&ipc_port_timestamp_lock_data)
251
252#define ipc_port_timestamp_unlock() \
253 mutex_unlock(&ipc_port_timestamp_lock_data)
254
255/* Retrieve a port timestamp value */
256extern ipc_port_timestamp_t ipc_port_timestamp(void);
257
258/*
259 * Compares two timestamps, and returns TRUE if one
260 * happened before two. Note that this formulation
261 * works when the timestamp wraps around at 2^32,
262 * as long as one and two aren't too far apart.
263 */
264
265#define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
266
267#define ipc_port_translate_receive(space, name, portp) \
268 ipc_object_translate((space), (name), \
269 MACH_PORT_RIGHT_RECEIVE, \
270 (ipc_object_t *) (portp))
271
272#define ipc_port_translate_send(space, name, portp) \
273 ipc_object_translate((space), (name), \
274 MACH_PORT_RIGHT_SEND, \
275 (ipc_object_t *) (portp))
276
277/* Allocate a dead-name request slot */
278extern kern_return_t
279ipc_port_dnrequest(
280 ipc_port_t port,
281 mach_port_name_t name,
282 ipc_port_t soright,
283 ipc_port_request_index_t *indexp);
284
285/* Grow a port's table of dead-name requests */
286extern kern_return_t ipc_port_dngrow(
91447636
A
287 ipc_port_t port,
288 ipc_table_elems_t target_size);
1c79356b
A
289
290/* Cancel a dead-name request and return the send-once right */
291extern ipc_port_t ipc_port_dncancel(
292 ipc_port_t port,
293 mach_port_name_t name,
294 ipc_port_request_index_t index);
295
296#define ipc_port_dnrename(port, index, oname, nname) \
297MACRO_BEGIN \
298 ipc_port_request_t ipr, table; \
299 \
300 assert(ip_active(port)); \
301 \
302 table = port->ip_dnrequests; \
303 assert(table != IPR_NULL); \
304 \
305 ipr = &table[index]; \
306 assert(ipr->ipr_name == oname); \
307 \
308 ipr->ipr_name = nname; \
309MACRO_END
310
311/* Make a port-deleted request */
312extern void ipc_port_pdrequest(
313 ipc_port_t port,
314 ipc_port_t notify,
315 ipc_port_t *previousp);
316
317/* Make a no-senders request */
318extern void ipc_port_nsrequest(
319 ipc_port_t port,
320 mach_port_mscount_t sync,
321 ipc_port_t notify,
322 ipc_port_t *previousp);
323
324#define ipc_port_set_mscount(port, mscount) \
325MACRO_BEGIN \
326 assert(ip_active(port)); \
327 \
328 (port)->ip_mscount = (mscount); \
329MACRO_END
330
331/* Prepare a receive right for transmission/destruction */
332extern void ipc_port_clear_receiver(
333 ipc_port_t port);
334
335/* Initialize a newly-allocated port */
336extern void ipc_port_init(
337 ipc_port_t port,
338 ipc_space_t space,
339 mach_port_name_t name);
340
341/* Allocate a port */
342extern kern_return_t ipc_port_alloc(
343 ipc_space_t space,
344 mach_port_name_t *namep,
345 ipc_port_t *portp);
346
347/* Allocate a port, with a specific name */
348extern kern_return_t ipc_port_alloc_name(
349 ipc_space_t space,
350 mach_port_name_t name,
351 ipc_port_t *portp);
352
353/* Generate dead name notifications */
354extern void ipc_port_dnnotify(
355 ipc_port_t port,
356 ipc_port_request_t dnrequests);
357
358/* Destroy a port */
359extern void ipc_port_destroy(
360 ipc_port_t port);
361
362/* Check if queueing "port" in a message for "dest" would create a circular
363 group of ports and messages */
364extern boolean_t
365ipc_port_check_circularity(
366 ipc_port_t port,
367 ipc_port_t dest);
368
369/* Make a send-once notify port from a receive right */
370extern ipc_port_t ipc_port_lookup_notify(
371 ipc_space_t space,
372 mach_port_name_t name);
373
0b4e3aa0
A
374/* Make a naked send right from a receive right - port locked and active */
375extern ipc_port_t ipc_port_make_send_locked(
376 ipc_port_t port);
377
1c79356b
A
378/* Make a naked send right from a receive right */
379extern ipc_port_t ipc_port_make_send(
380 ipc_port_t port);
381
382/* Make a naked send right from another naked send right */
383extern ipc_port_t ipc_port_copy_send(
384 ipc_port_t port);
385
386/* Copyout a naked send right */
387extern mach_port_name_t ipc_port_copyout_send(
388 ipc_port_t sright,
389 ipc_space_t space);
390
391/* Release a (valid) naked send right */
392extern void ipc_port_release_send(
393 ipc_port_t port);
394
395/* Make a naked send-once right from a receive right */
396extern ipc_port_t ipc_port_make_sonce(
397 ipc_port_t port);
398
399/* Release a naked send-once right */
400extern void ipc_port_release_sonce(
401 ipc_port_t port);
402
403/* Release a naked (in limbo or in transit) receive right */
404extern void ipc_port_release_receive(
405 ipc_port_t port);
406
407/* Allocate a port in a special space */
408extern ipc_port_t ipc_port_alloc_special(
409 ipc_space_t space);
410
411/* Deallocate a port in a special space */
412extern void ipc_port_dealloc_special(
413 ipc_port_t port,
414 ipc_space_t space);
415
416#if MACH_ASSERT
417/* Track low-level port deallocation */
418extern void ipc_port_track_dealloc(
419 ipc_port_t port);
420
421/* Initialize general port debugging state */
422extern void ipc_port_debug_init(void);
423#endif /* MACH_ASSERT */
424
425#define ipc_port_alloc_kernel() \
426 ipc_port_alloc_special(ipc_space_kernel)
427#define ipc_port_dealloc_kernel(port) \
428 ipc_port_dealloc_special((port), ipc_space_kernel)
429
430#define ipc_port_alloc_reply() \
431 ipc_port_alloc_special(ipc_space_reply)
432#define ipc_port_dealloc_reply(port) \
433 ipc_port_dealloc_special((port), ipc_space_reply)
434
435#define ipc_port_reference(port) \
436 ipc_object_reference(&(port)->ip_object)
437
438#define ipc_port_release(port) \
439 ipc_object_release(&(port)->ip_object)
440
441#endif /* _IPC_IPC_PORT_H_ */