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