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