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