]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_port.h
xnu-2422.1.72.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_port.h
1 /*
2 * Copyright (c) 2000-2008 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 */
62 /*
63 */
64 /*
65 * File: ipc/ipc_port.h
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Definitions for ports.
70 */
71
72 #ifndef _IPC_IPC_PORT_H_
73 #define _IPC_IPC_PORT_H_
74
75 #if MACH_KERNEL_PRIVATE
76
77 #include <mach_rt.h>
78 #include <mach_assert.h>
79 #include <mach_debug.h>
80
81 #include <mach/mach_types.h>
82 #include <mach/boolean.h>
83 #include <mach/kern_return.h>
84 #include <mach/port.h>
85
86 #include <kern/kern_types.h>
87
88 #include <ipc/ipc_types.h>
89 #include <ipc/ipc_object.h>
90 #include <ipc/ipc_mqueue.h>
91 #include <ipc/ipc_space.h>
92
93 #include <security/_label.h>
94
95 /*
96 * A receive right (port) can be in four states:
97 * 1) dead (not active, ip_timestamp has death time)
98 * 2) in a space (ip_receiver_name != 0, ip_receiver points
99 * to the space but doesn't hold a ref for it)
100 * 3) in transit (ip_receiver_name == 0, ip_destination points
101 * to the destination port and holds a ref for it)
102 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
103 *
104 * If the port is active, and ip_receiver points to some space,
105 * then ip_receiver_name != 0, and that space holds receive rights.
106 * If the port is not active, then ip_timestamp contains a timestamp
107 * taken when the port was destroyed.
108 */
109
110 typedef unsigned int ipc_port_timestamp_t;
111
112 struct ipc_port {
113
114 /*
115 * Initial sub-structure in common with ipc_pset
116 * First element is an ipc_object second is a
117 * message queue
118 */
119 struct ipc_object ip_object;
120 struct ipc_mqueue ip_messages;
121
122 union {
123 struct ipc_space *receiver;
124 struct ipc_port *destination;
125 ipc_port_timestamp_t timestamp;
126 } data;
127
128 union {
129 ipc_kobject_t kobject;
130 task_t imp_task;
131 uintptr_t alias;
132 } kdata;
133
134 struct ipc_port *ip_nsrequest;
135 struct ipc_port *ip_pdrequest;
136 struct ipc_port_request *ip_requests;
137 struct ipc_kmsg *ip_premsg;
138
139 mach_port_mscount_t ip_mscount;
140 mach_port_rights_t ip_srights;
141 mach_port_rights_t ip_sorights;
142
143 natural_t ip_sprequests:1, /* send-possible requests outstanding */
144 ip_spimportant:1, /* ... at least one is importance donating */
145 ip_impdonation:1, /* port supports importance donation */
146 ip_tempowner:1, /* dont give donations to current receiver */
147 ip_taskptr:1, /* ... instead give them to a specified task */
148 ip_guarded:1, /* port guarded (use context value as guard) */
149 ip_strict_guard:1, /* Strict guarding; Prevents user manipulation of context values directly */
150 ip_reserved:1,
151 ip_impcount:24; /* number of importance donations in nested queue */
152
153 mach_vm_address_t ip_context;
154
155
156 #if MACH_ASSERT
157 #define IP_NSPARES 4
158 #define IP_CALLSTACK_MAX 16
159 queue_chain_t ip_port_links; /* all allocated ports */
160 thread_t ip_thread; /* who made me? thread context */
161 unsigned long ip_timetrack; /* give an idea of "when" created */
162 uintptr_t ip_callstack[IP_CALLSTACK_MAX]; /* stack trace */
163 unsigned long ip_spares[IP_NSPARES]; /* for debugging */
164 #endif /* MACH_ASSERT */
165
166 #if CONFIG_MACF_MACH
167 struct label ip_label;
168 #endif
169 };
170
171
172 #define ip_references ip_object.io_references
173 #define ip_bits ip_object.io_bits
174
175 #define ip_receiver_name ip_messages.imq_receiver_name
176 #define ip_pset_count ip_messages.imq_pset_count
177
178 #define ip_receiver data.receiver
179 #define ip_destination data.destination
180 #define ip_timestamp data.timestamp
181
182 #define ip_kobject kdata.kobject
183 #define ip_imp_task kdata.imp_task
184 #define ip_alias kdata.alias
185
186 #define IP_NULL IPC_PORT_NULL
187 #define IP_DEAD IPC_PORT_DEAD
188 #define IP_VALID(port) IPC_PORT_VALID(port)
189
190 #define ip_active(port) io_active(&(port)->ip_object)
191 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
192 #define ip_lock(port) io_lock(&(port)->ip_object)
193 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
194 #define ip_unlock(port) io_unlock(&(port)->ip_object)
195
196 #define ip_reference(port) io_reference(&(port)->ip_object)
197 #define ip_release(port) io_release(&(port)->ip_object)
198
199 #define ip_kotype(port) io_kotype(&(port)->ip_object)
200
201 #define ip_full_kernel(port) imq_full_kernel(&(port)->ip_messages)
202 #define ip_full(port) imq_full(&(port)->ip_messages)
203
204 /*
205 * JMM - Preallocation flag
206 * This flag indicates that there is a message buffer preallocated for this
207 * port and we should use that when sending (from the kernel) rather than
208 * allocate a new one. This avoids deadlocks during notification message
209 * sends by critical system threads (which may be needed to free memory and
210 * therefore cannot be blocked waiting for memory themselves).
211 */
212 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
213 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
214
215 #define IP_SET_PREALLOC(port, kmsg) \
216 MACRO_BEGIN \
217 (port)->ip_bits |= IP_BIT_PREALLOC; \
218 (port)->ip_premsg = (kmsg); \
219 MACRO_END
220
221 #define IP_CLEAR_PREALLOC(port, kmsg) \
222 MACRO_BEGIN \
223 assert((port)->ip_premsg == kmsg); \
224 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
225 (port)->ip_premsg = IKM_NULL; \
226 MACRO_END
227
228 /* JMM - address alignment/packing for LP64 */
229 struct ipc_port_request {
230 union {
231 struct ipc_port *port;
232 ipc_port_request_index_t index;
233 } notify;
234
235 union {
236 mach_port_name_t name;
237 struct ipc_table_size *size;
238 } name;
239 };
240
241 #define ipr_next notify.index
242 #define ipr_size name.size
243
244 #define ipr_soright notify.port
245 #define ipr_name name.name
246
247 /*
248 * Use the low bits in the ipr_soright to specify the request type
249 */
250 #define IPR_SOR_SPARM_MASK 1 /* send-possible armed */
251 #define IPR_SOR_SPREQ_MASK 2 /* send-possible requested */
252 #define IPR_SOR_SPBIT_MASK 3 /* combo */
253 #define IPR_SOR_SPARMED(sor) (((uintptr_t)(sor) & IPR_SOR_SPARM_MASK) != 0)
254 #define IPR_SOR_SPREQ(sor) (((uintptr_t)(sor) & IPR_SOR_SPREQ_MASK) != 0)
255 #define IPR_SOR_PORT(sor) ((ipc_port_t)((uintptr_t)(sor) & ~IPR_SOR_SPBIT_MASK))
256 #define IPR_SOR_MAKE(p,m) ((ipc_port_t)((uintptr_t)(p) | (m)))
257
258 extern lck_grp_t ipc_lck_grp;
259 extern lck_attr_t ipc_lck_attr;
260
261 /*
262 * Taking the ipc_port_multiple lock grants the privilege
263 * to lock multiple ports at once. No ports must locked
264 * when it is taken.
265 */
266
267 #if 1
268 decl_lck_mtx_data(extern,ipc_port_multiple_lock_data)
269 extern lck_mtx_ext_t ipc_port_multiple_lock_data_ext;
270
271 #define ipc_port_multiple_lock_init() \
272 lck_mtx_init_ext(&ipc_port_multiple_lock_data, &ipc_port_multiple_lock_data_ext, &ipc_lck_grp, &ipc_lck_attr)
273
274 #define ipc_port_multiple_lock() \
275 lck_mtx_lock(&ipc_port_multiple_lock_data)
276
277 #define ipc_port_multiple_unlock() \
278 lck_mtx_unlock(&ipc_port_multiple_lock_data)
279 #else
280 lck_spin_t ipc_port_multiple_lock_data;
281
282 #define ipc_port_multiple_lock_init() \
283 lck_spin_init(&ipc_port_multiple_lock_data, &ipc_lck_grp, &ipc_lck_attr)
284
285 #define ipc_port_multiple_lock() \
286 lck_spin_lock(&ipc_port_multiple_lock_data)
287
288 #define ipc_port_multiple_unlock() \
289 lck_spin_unlock(&ipc_port_multiple_lock_data)
290 #endif
291
292 /*
293 * The port timestamp facility provides timestamps
294 * for port destruction. It is used to serialize
295 * mach_port_names with port death.
296 */
297
298 extern ipc_port_timestamp_t ipc_port_timestamp_data;
299
300 /* Retrieve a port timestamp value */
301 extern ipc_port_timestamp_t ipc_port_timestamp(void);
302
303 /*
304 * Compares two timestamps, and returns TRUE if one
305 * happened before two. Note that this formulation
306 * works when the timestamp wraps around at 2^32,
307 * as long as one and two aren't too far apart.
308 */
309
310 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
311
312 #define ipc_port_translate_receive(space, name, portp) \
313 ipc_object_translate((space), (name), \
314 MACH_PORT_RIGHT_RECEIVE, \
315 (ipc_object_t *) (portp))
316
317 #define ipc_port_translate_send(space, name, portp) \
318 ipc_object_translate((space), (name), \
319 MACH_PORT_RIGHT_SEND, \
320 (ipc_object_t *) (portp))
321
322 /* Allocate a notification request slot */
323 #if IMPORTANCE_INHERITANCE
324 extern kern_return_t
325 ipc_port_request_alloc(
326 ipc_port_t port,
327 mach_port_name_t name,
328 ipc_port_t soright,
329 boolean_t send_possible,
330 boolean_t immediate,
331 ipc_port_request_index_t *indexp,
332 boolean_t *importantp);
333 #else
334 extern kern_return_t
335 ipc_port_request_alloc(
336 ipc_port_t port,
337 mach_port_name_t name,
338 ipc_port_t soright,
339 boolean_t send_possible,
340 boolean_t immediate,
341 ipc_port_request_index_t *indexp);
342 #endif /* IMPORTANCE_INHERITANCE */
343
344 /* Grow one of a port's tables of notifcation requests */
345 extern kern_return_t ipc_port_request_grow(
346 ipc_port_t port,
347 ipc_table_elems_t target_size);
348
349 /* Return the type(s) of notification requests outstanding */
350 extern mach_port_type_t ipc_port_request_type(
351 ipc_port_t port,
352 mach_port_name_t name,
353 ipc_port_request_index_t index);
354
355 /* Cancel a notification request and return the send-once right */
356 extern ipc_port_t ipc_port_request_cancel(
357 ipc_port_t port,
358 mach_port_name_t name,
359 ipc_port_request_index_t index);
360
361 /* Arm any delayed send-possible notification */
362 #if IMPORTANCE_INHERITANCE
363 extern boolean_t ipc_port_request_sparm(
364 ipc_port_t port,
365 mach_port_name_t name,
366 ipc_port_request_index_t index,
367 mach_msg_option_t option);
368 #else
369 extern boolean_t ipc_port_request_sparm(
370 ipc_port_t port,
371 mach_port_name_t name,
372 ipc_port_request_index_t index);
373 #endif /* IMPORTANCE_INHERITANCE */
374
375 /* Macros for manipulating a port's dead name notificaiton requests */
376 #define ipc_port_request_rename(port, index, oname, nname) \
377 MACRO_BEGIN \
378 ipc_port_request_t ipr, table; \
379 \
380 assert(ip_active(port)); \
381 \
382 table = port->ip_requests; \
383 assert(table != IPR_NULL); \
384 \
385 ipr = &table[index]; \
386 assert(ipr->ipr_name == oname); \
387 \
388 ipr->ipr_name = nname; \
389 MACRO_END
390
391
392 /* Make a port-deleted request */
393 extern void ipc_port_pdrequest(
394 ipc_port_t port,
395 ipc_port_t notify,
396 ipc_port_t *previousp);
397
398 /* Make a no-senders request */
399 extern void ipc_port_nsrequest(
400 ipc_port_t port,
401 mach_port_mscount_t sync,
402 ipc_port_t notify,
403 ipc_port_t *previousp);
404
405 #define ipc_port_set_mscount(port, mscount) \
406 MACRO_BEGIN \
407 assert(ip_active(port)); \
408 \
409 (port)->ip_mscount = (mscount); \
410 MACRO_END
411
412 /* Prepare a receive right for transmission/destruction */
413 extern void ipc_port_clear_receiver(
414 ipc_port_t port,
415 queue_t links);
416
417 /* Initialize a newly-allocated port */
418 extern void ipc_port_init(
419 ipc_port_t port,
420 ipc_space_t space,
421 mach_port_name_t name);
422
423 /* Allocate a port */
424 extern kern_return_t ipc_port_alloc(
425 ipc_space_t space,
426 mach_port_name_t *namep,
427 ipc_port_t *portp);
428
429 /* Allocate a port, with a specific name */
430 extern kern_return_t ipc_port_alloc_name(
431 ipc_space_t space,
432 mach_port_name_t name,
433 ipc_port_t *portp);
434
435 /* Generate dead name notifications */
436 extern void ipc_port_dnnotify(
437 ipc_port_t port);
438
439 /* Generate send-possible notifications */
440 extern void ipc_port_spnotify(
441 ipc_port_t port);
442
443 /* Destroy a port */
444 extern void ipc_port_destroy(
445 ipc_port_t port);
446
447 /* Check if queueing "port" in a message for "dest" would create a circular
448 group of ports and messages */
449 extern boolean_t
450 ipc_port_check_circularity(
451 ipc_port_t port,
452 ipc_port_t dest);
453
454 #if IMPORTANCE_INHERITANCE
455 /* Apply an importance delta to a port */
456 extern boolean_t
457 ipc_port_importance_delta(
458 ipc_port_t port,
459 mach_port_delta_t delta);
460 #endif /* IMPORTANCE_INHERITANCE */
461
462 /* Make a send-once notify port from a receive right */
463 extern ipc_port_t ipc_port_lookup_notify(
464 ipc_space_t space,
465 mach_port_name_t name);
466
467 /* Make a naked send right from a receive right - port locked and active */
468 extern ipc_port_t ipc_port_make_send_locked(
469 ipc_port_t port);
470
471 /* Make a naked send right from a receive right */
472 extern ipc_port_t ipc_port_make_send(
473 ipc_port_t port);
474
475 /* Make a naked send right from another naked send right */
476 extern ipc_port_t ipc_port_copy_send(
477 ipc_port_t port);
478
479 /* Copyout a naked send right */
480 extern mach_port_name_t ipc_port_copyout_send(
481 ipc_port_t sright,
482 ipc_space_t space);
483
484 #endif /* MACH_KERNEL_PRIVATE */
485
486 #if KERNEL_PRIVATE
487
488 /* Release a (valid) naked send right */
489 extern void ipc_port_release_send(
490 ipc_port_t port);
491
492 extern void ipc_port_reference(
493 ipc_port_t port);
494
495 extern void ipc_port_release(
496 ipc_port_t port);
497
498 #endif /* KERNEL_PRIVATE */
499
500 #if MACH_KERNEL_PRIVATE
501
502 /* Make a naked send-once right from a locked and active receive right */
503 extern ipc_port_t ipc_port_make_sonce_locked(
504 ipc_port_t port);
505
506 /* Make a naked send-once right from a receive right */
507 extern ipc_port_t ipc_port_make_sonce(
508 ipc_port_t port);
509
510 /* Release a naked send-once right */
511 extern void ipc_port_release_sonce(
512 ipc_port_t port);
513
514 /* Release a naked (in limbo or in transit) receive right */
515 extern void ipc_port_release_receive(
516 ipc_port_t port);
517
518 /* finalize the destruction of a port before it gets freed */
519 extern void ipc_port_finalize(
520 ipc_port_t port);
521
522 /* Allocate a port in a special space */
523 extern ipc_port_t ipc_port_alloc_special(
524 ipc_space_t space);
525
526 /* Deallocate a port in a special space */
527 extern void ipc_port_dealloc_special(
528 ipc_port_t port,
529 ipc_space_t space);
530
531 #if MACH_ASSERT
532 /* Track low-level port deallocation */
533 extern void ipc_port_track_dealloc(
534 ipc_port_t port);
535
536 /* Initialize general port debugging state */
537 extern void ipc_port_debug_init(void);
538 #endif /* MACH_ASSERT */
539
540 #define ipc_port_alloc_kernel() \
541 ipc_port_alloc_special(ipc_space_kernel)
542 #define ipc_port_dealloc_kernel(port) \
543 ipc_port_dealloc_special((port), ipc_space_kernel)
544
545 #define ipc_port_alloc_reply() \
546 ipc_port_alloc_special(ipc_space_reply)
547 #define ipc_port_dealloc_reply(port) \
548 ipc_port_dealloc_special((port), ipc_space_reply)
549
550 #endif /* MACH_KERNEL_PRIVATE */
551
552 #endif /* _IPC_IPC_PORT_H_ */