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