]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_port.h
xnu-4570.20.62.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_port.h
1 /*
2 * Copyright (c) 2000-2016 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 #ifdef MACH_KERNEL_PRIVATE
76
77 #include <mach_assert.h>
78 #include <mach_debug.h>
79
80 #include <mach/mach_types.h>
81 #include <mach/boolean.h>
82 #include <mach/kern_return.h>
83 #include <mach/port.h>
84
85 #include <kern/assert.h>
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 ipc_importance_task_t imp_task;
131 ipc_port_t sync_qos_override_port;
132 } kdata;
133
134 struct ipc_port *ip_nsrequest;
135 struct ipc_port *ip_pdrequest;
136 struct ipc_port_request *ip_requests;
137 union {
138 struct ipc_kmsg *premsg;
139 struct {
140 sync_qos_count_t sync_qos[THREAD_QOS_LAST];
141 sync_qos_count_t special_port_qos;
142 } qos_counter;
143 } kdata2;
144
145 mach_vm_address_t ip_context;
146
147 natural_t ip_sprequests:1, /* send-possible requests outstanding */
148 ip_spimportant:1, /* ... at least one is importance donating */
149 ip_impdonation:1, /* port supports importance donation */
150 ip_tempowner:1, /* dont give donations to current receiver */
151 ip_guarded:1, /* port guarded (use context value as guard) */
152 ip_strict_guard:1, /* Strict guarding; Prevents user manipulation of context values directly */
153 ip_specialreply:1, /* port is a special reply port */
154 ip_link_sync_qos:1, /* link the special reply port to destination port */
155 ip_impcount:24; /* number of importance donations in nested queue */
156
157 mach_port_mscount_t ip_mscount;
158 mach_port_rights_t ip_srights;
159 mach_port_rights_t ip_sorights;
160
161 #if MACH_ASSERT
162 #define IP_NSPARES 4
163 #define IP_CALLSTACK_MAX 16
164 /* queue_chain_t ip_port_links;*//* all allocated ports */
165 thread_t ip_thread; /* who made me? thread context */
166 unsigned long ip_timetrack; /* give an idea of "when" created */
167 uintptr_t ip_callstack[IP_CALLSTACK_MAX]; /* stack trace */
168 unsigned long ip_spares[IP_NSPARES]; /* for debugging */
169 #endif /* MACH_ASSERT */
170 };
171
172
173 #define ip_references ip_object.io_references
174 #define ip_bits ip_object.io_bits
175
176 #define ip_receiver_name ip_messages.imq_receiver_name
177 #define ip_in_pset ip_messages.imq_in_pset
178
179 #define ip_receiver data.receiver
180 #define ip_destination data.destination
181 #define ip_timestamp data.timestamp
182
183 #define ip_kobject kdata.kobject
184 #define ip_imp_task kdata.imp_task
185 #define ip_sync_qos_override_port kdata.sync_qos_override_port
186
187 #define ip_premsg kdata2.premsg
188 #define ip_sync_qos kdata2.qos_counter.sync_qos
189 #define ip_special_port_qos kdata2.qos_counter.special_port_qos
190
191 #define port_sync_qos(port, i) (IP_PREALLOC(port) ? (port)->ip_premsg->sync_qos[(i)] : (port)->ip_sync_qos[(i)])
192 #define port_special_qos(port) (IP_PREALLOC(port) ? (port)->ip_premsg->special_port_qos : (port)->ip_special_port_qos)
193
194 #define set_port_sync_qos(port, i, value) \
195 MACRO_BEGIN \
196 if (IP_PREALLOC(port)) { \
197 (port)->ip_premsg->sync_qos[(i)] = (value); \
198 } else { \
199 (port)->ip_sync_qos[(i)] = (value); \
200 } \
201 MACRO_END
202
203 #define set_port_special_qos(port, value) \
204 MACRO_BEGIN \
205 if (IP_PREALLOC(port)) { \
206 (port)->ip_premsg->special_port_qos = (value); \
207 } else { \
208 (port)->ip_special_port_qos = (value); \
209 } \
210 MACRO_END
211
212 #define IP_NULL IPC_PORT_NULL
213 #define IP_DEAD IPC_PORT_DEAD
214 #define IP_VALID(port) IPC_PORT_VALID(port)
215
216 #define ip_active(port) io_active(&(port)->ip_object)
217 #define ip_lock_init(port) io_lock_init(&(port)->ip_object)
218 #define ip_lock(port) io_lock(&(port)->ip_object)
219 #define ip_lock_try(port) io_lock_try(&(port)->ip_object)
220 #define ip_lock_held_kdp(port) io_lock_held_kdp(&(port)->ip_object)
221 #define ip_unlock(port) io_unlock(&(port)->ip_object)
222
223 #define ip_reference(port) io_reference(&(port)->ip_object)
224 #define ip_release(port) io_release(&(port)->ip_object)
225
226 /* get an ipc_port pointer from an ipc_mqueue pointer */
227 #define ip_from_mq(mq) ((struct ipc_port *)((void *)( \
228 (char *)(mq) - \
229 __offsetof(struct ipc_port, ip_messages)) \
230 ))
231
232 #define ip_reference_mq(mq) ip_reference(ip_from_mq(mq))
233 #define ip_release_mq(mq) ip_release(ip_from_mq(mq))
234
235 #define ip_kotype(port) io_kotype(&(port)->ip_object)
236
237 #define ip_full_kernel(port) imq_full_kernel(&(port)->ip_messages)
238 #define ip_full(port) imq_full(&(port)->ip_messages)
239
240 /*
241 * JMM - Preallocation flag
242 * This flag indicates that there is a message buffer preallocated for this
243 * port and we should use that when sending (from the kernel) rather than
244 * allocate a new one. This avoids deadlocks during notification message
245 * sends by critical system threads (which may be needed to free memory and
246 * therefore cannot be blocked waiting for memory themselves).
247 */
248 #define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
249 #define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
250
251 #define IP_SET_PREALLOC(port, kmsg) \
252 MACRO_BEGIN \
253 (port)->ip_bits |= IP_BIT_PREALLOC; \
254 (port)->ip_premsg = (kmsg); \
255 MACRO_END
256
257 #define IP_CLEAR_PREALLOC(port, kmsg) \
258 MACRO_BEGIN \
259 assert((port)->ip_premsg == kmsg); \
260 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
261 (port)->ip_premsg = IKM_NULL; \
262 MACRO_END
263
264 /* JMM - address alignment/packing for LP64 */
265 struct ipc_port_request {
266 union {
267 struct ipc_port *port;
268 ipc_port_request_index_t index;
269 } notify;
270
271 union {
272 mach_port_name_t name;
273 struct ipc_table_size *size;
274 } name;
275 };
276
277 #define ipr_next notify.index
278 #define ipr_size name.size
279
280 #define ipr_soright notify.port
281 #define ipr_name name.name
282
283 /*
284 * Use the low bits in the ipr_soright to specify the request type
285 */
286 #define IPR_SOR_SPARM_MASK 1 /* send-possible armed */
287 #define IPR_SOR_SPREQ_MASK 2 /* send-possible requested */
288 #define IPR_SOR_SPBIT_MASK 3 /* combo */
289 #define IPR_SOR_SPARMED(sor) (((uintptr_t)(sor) & IPR_SOR_SPARM_MASK) != 0)
290 #define IPR_SOR_SPREQ(sor) (((uintptr_t)(sor) & IPR_SOR_SPREQ_MASK) != 0)
291 #define IPR_SOR_PORT(sor) ((ipc_port_t)((uintptr_t)(sor) & ~IPR_SOR_SPBIT_MASK))
292 #define IPR_SOR_MAKE(p,m) ((ipc_port_t)((uintptr_t)(p) | (m)))
293
294 extern lck_grp_t ipc_lck_grp;
295 extern lck_attr_t ipc_lck_attr;
296
297 /*
298 * Taking the ipc_port_multiple lock grants the privilege
299 * to lock multiple ports at once. No ports must locked
300 * when it is taken.
301 */
302
303 extern lck_spin_t ipc_port_multiple_lock_data;
304
305 #define ipc_port_multiple_lock_init() \
306 lck_spin_init(&ipc_port_multiple_lock_data, &ipc_lck_grp, &ipc_lck_attr)
307
308 #define ipc_port_multiple_lock() \
309 lck_spin_lock(&ipc_port_multiple_lock_data)
310
311 #define ipc_port_multiple_unlock() \
312 lck_spin_unlock(&ipc_port_multiple_lock_data)
313
314 /*
315 * The port timestamp facility provides timestamps
316 * for port destruction. It is used to serialize
317 * mach_port_names with port death.
318 */
319
320 extern ipc_port_timestamp_t ipc_port_timestamp_data;
321
322 /* Retrieve a port timestamp value */
323 extern ipc_port_timestamp_t ipc_port_timestamp(void);
324
325 /*
326 * Compares two timestamps, and returns TRUE if one
327 * happened before two. Note that this formulation
328 * works when the timestamp wraps around at 2^32,
329 * as long as one and two aren't too far apart.
330 */
331
332 #define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
333
334 #define ipc_port_translate_receive(space, name, portp) \
335 ipc_object_translate((space), (name), \
336 MACH_PORT_RIGHT_RECEIVE, \
337 (ipc_object_t *) (portp))
338
339 #define ipc_port_translate_send(space, name, portp) \
340 ipc_object_translate((space), (name), \
341 MACH_PORT_RIGHT_SEND, \
342 (ipc_object_t *) (portp))
343
344 /* Allocate a notification request slot */
345 #if IMPORTANCE_INHERITANCE
346 extern kern_return_t
347 ipc_port_request_alloc(
348 ipc_port_t port,
349 mach_port_name_t name,
350 ipc_port_t soright,
351 boolean_t send_possible,
352 boolean_t immediate,
353 ipc_port_request_index_t *indexp,
354 boolean_t *importantp);
355 #else
356 extern kern_return_t
357 ipc_port_request_alloc(
358 ipc_port_t port,
359 mach_port_name_t name,
360 ipc_port_t soright,
361 boolean_t send_possible,
362 boolean_t immediate,
363 ipc_port_request_index_t *indexp);
364 #endif /* IMPORTANCE_INHERITANCE */
365
366 /* Grow one of a port's tables of notifcation requests */
367 extern kern_return_t ipc_port_request_grow(
368 ipc_port_t port,
369 ipc_table_elems_t target_size);
370
371 /* Return the type(s) of notification requests outstanding */
372 extern mach_port_type_t ipc_port_request_type(
373 ipc_port_t port,
374 mach_port_name_t name,
375 ipc_port_request_index_t index);
376
377 /* Cancel a notification request and return the send-once right */
378 extern ipc_port_t ipc_port_request_cancel(
379 ipc_port_t port,
380 mach_port_name_t name,
381 ipc_port_request_index_t index);
382
383 /* Arm any delayed send-possible notification */
384 extern boolean_t ipc_port_request_sparm(
385 ipc_port_t port,
386 mach_port_name_t name,
387 ipc_port_request_index_t index,
388 mach_msg_option_t option,
389 mach_msg_priority_t override);
390
391 /* Macros for manipulating a port's dead name notificaiton requests */
392 #define ipc_port_request_rename(port, index, oname, nname) \
393 MACRO_BEGIN \
394 ipc_port_request_t ipr, table; \
395 \
396 assert(ip_active(port)); \
397 \
398 table = port->ip_requests; \
399 assert(table != IPR_NULL); \
400 \
401 ipr = &table[index]; \
402 assert(ipr->ipr_name == oname); \
403 \
404 ipr->ipr_name = nname; \
405 MACRO_END
406
407
408 /* Make a port-deleted request */
409 extern void ipc_port_pdrequest(
410 ipc_port_t port,
411 ipc_port_t notify,
412 ipc_port_t *previousp);
413
414 /* Make a no-senders request */
415 extern void ipc_port_nsrequest(
416 ipc_port_t port,
417 mach_port_mscount_t sync,
418 ipc_port_t notify,
419 ipc_port_t *previousp);
420
421 #define ipc_port_set_mscount(port, mscount) \
422 MACRO_BEGIN \
423 assert(ip_active(port)); \
424 \
425 (port)->ip_mscount = (mscount); \
426 MACRO_END
427
428 /* Prepare a receive right for transmission/destruction */
429 extern boolean_t ipc_port_clear_receiver(
430 ipc_port_t port,
431 boolean_t should_destroy);
432
433 /* Initialize a newly-allocated port */
434 extern void ipc_port_init(
435 ipc_port_t port,
436 ipc_space_t space,
437 mach_port_name_t name);
438
439 /* Allocate a port */
440 extern kern_return_t ipc_port_alloc(
441 ipc_space_t space,
442 mach_port_name_t *namep,
443 ipc_port_t *portp);
444
445 /* Allocate a port, with a specific name */
446 extern kern_return_t ipc_port_alloc_name(
447 ipc_space_t space,
448 mach_port_name_t name,
449 ipc_port_t *portp);
450
451 /* Generate dead name notifications */
452 extern void ipc_port_dnnotify(
453 ipc_port_t port);
454
455 /* Generate send-possible notifications */
456 extern void ipc_port_spnotify(
457 ipc_port_t port);
458
459 /* Destroy a port */
460 extern void ipc_port_destroy(
461 ipc_port_t port);
462
463 /* Check if queueing "port" in a message for "dest" would create a circular
464 group of ports and messages */
465 extern boolean_t
466 ipc_port_check_circularity(
467 ipc_port_t port,
468 ipc_port_t dest);
469
470 #if IMPORTANCE_INHERITANCE
471
472 enum {
473 IPID_OPTION_NORMAL = 0, /* normal boost */
474 IPID_OPTION_SENDPOSSIBLE = 1, /* send-possible induced boost */
475 };
476
477 /* link the destination port with special reply port */
478 kern_return_t
479 ipc_port_link_special_reply_port_with_qos(
480 ipc_port_t special_reply_port,
481 ipc_port_t dest_port,
482 int qos);
483
484 /* link the destination port with locked special reply port */
485 void ipc_port_unlink_special_reply_port_locked(
486 ipc_port_t special_reply_port,
487 struct knote *kn,
488 uint8_t flags);
489
490 /* Unlink the destination port from special reply port */
491 void
492 ipc_port_unlink_special_reply_port(
493 ipc_port_t special_reply_port,
494 uint8_t flags);
495
496 #define IPC_PORT_UNLINK_SR_NONE 0
497 #define IPC_PORT_UNLINK_SR_CLEAR_SPECIAL_REPLY 0x1
498 #define IPC_PORT_UNLINK_SR_ALLOW_SYNC_QOS_LINKAGE 0x2
499
500 /* Get the max sync qos override index applied to the port */
501 sync_qos_count_t
502 ipc_port_get_max_sync_qos_index(
503 ipc_port_t port);
504
505 /* Apply qos delta to the port */
506 boolean_t
507 ipc_port_sync_qos_delta(
508 ipc_port_t port,
509 sync_qos_count_t *sync_qos_delta_add,
510 sync_qos_count_t *sync_qos_delta_sub);
511
512 /* Adjust the sync qos of the port and it's destination port */
513 void
514 ipc_port_adjust_sync_qos(
515 ipc_port_t port,
516 sync_qos_count_t *sync_qos_delta_add,
517 sync_qos_count_t *sync_qos_delta_sub);
518
519 /* apply importance delta to port only */
520 extern mach_port_delta_t
521 ipc_port_impcount_delta(
522 ipc_port_t port,
523 mach_port_delta_t delta,
524 ipc_port_t base);
525
526 /* apply importance delta to port, and return task importance for update */
527 extern boolean_t
528 ipc_port_importance_delta_internal(
529 ipc_port_t port,
530 natural_t options,
531 mach_port_delta_t *deltap,
532 ipc_importance_task_t *imp_task);
533
534 /* Apply an importance delta to a port and reflect change in receiver task */
535 extern boolean_t
536 ipc_port_importance_delta(
537 ipc_port_t port,
538 natural_t options,
539 mach_port_delta_t delta);
540 #endif /* IMPORTANCE_INHERITANCE */
541
542 /* Make a send-once notify port from a receive right */
543 extern ipc_port_t ipc_port_lookup_notify(
544 ipc_space_t space,
545 mach_port_name_t name);
546
547 /* Make a naked send right from a receive right - port locked and active */
548 extern ipc_port_t ipc_port_make_send_locked(
549 ipc_port_t port);
550
551 /* Make a naked send right from a receive right */
552 extern ipc_port_t ipc_port_make_send(
553 ipc_port_t port);
554
555 /* Make a naked send right from another naked send right */
556 extern ipc_port_t ipc_port_copy_send(
557 ipc_port_t port);
558
559 /* Copyout a naked send right */
560 extern mach_port_name_t ipc_port_copyout_send(
561 ipc_port_t sright,
562 ipc_space_t space);
563
564 #endif /* MACH_KERNEL_PRIVATE */
565
566 #if KERNEL_PRIVATE
567
568 /* Release a (valid) naked send right */
569 extern void ipc_port_release_send(
570 ipc_port_t port);
571
572 extern void ipc_port_reference(
573 ipc_port_t port);
574
575 extern void ipc_port_release(
576 ipc_port_t port);
577
578 #endif /* KERNEL_PRIVATE */
579
580 #ifdef MACH_KERNEL_PRIVATE
581
582 /* Make a naked send-once right from a locked and active receive right */
583 extern ipc_port_t ipc_port_make_sonce_locked(
584 ipc_port_t port);
585
586 /* Make a naked send-once right from a receive right */
587 extern ipc_port_t ipc_port_make_sonce(
588 ipc_port_t port);
589
590 /* Release a naked send-once right */
591 extern void ipc_port_release_sonce(
592 ipc_port_t port);
593
594 /* Release a naked (in limbo or in transit) receive right */
595 extern void ipc_port_release_receive(
596 ipc_port_t port);
597
598 /* finalize the destruction of a port before it gets freed */
599 extern void ipc_port_finalize(
600 ipc_port_t port);
601
602 /* Allocate a port in a special space */
603 extern ipc_port_t ipc_port_alloc_special(
604 ipc_space_t space);
605
606 /* Deallocate a port in a special space */
607 extern void ipc_port_dealloc_special(
608 ipc_port_t port,
609 ipc_space_t space);
610
611 #if MACH_ASSERT
612 /* Track low-level port deallocation */
613 extern void ipc_port_track_dealloc(
614 ipc_port_t port);
615
616 /* Initialize general port debugging state */
617 extern void ipc_port_debug_init(void);
618 #endif /* MACH_ASSERT */
619
620 #define ipc_port_alloc_kernel() \
621 ipc_port_alloc_special(ipc_space_kernel)
622 #define ipc_port_dealloc_kernel(port) \
623 ipc_port_dealloc_special((port), ipc_space_kernel)
624
625 #define ipc_port_alloc_reply() \
626 ipc_port_alloc_special(ipc_space_reply)
627 #define ipc_port_dealloc_reply(port) \
628 ipc_port_dealloc_special((port), ipc_space_reply)
629
630 #endif /* MACH_KERNEL_PRIVATE */
631
632 #endif /* _IPC_IPC_PORT_H_ */