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