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