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