]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/ipc/ipc_port.h
xnu-124.13.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_port.h
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52/*
53 * File: ipc/ipc_port.h
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Definitions for ports.
58 */
59
60#ifndef _IPC_IPC_PORT_H_
61#define _IPC_IPC_PORT_H_
62
63#include <norma_vm.h>
64#include <mach_rt.h>
65#include <mach_assert.h>
66#include <mach_debug.h>
67
68#include <mach/boolean.h>
69#include <mach/kern_return.h>
70#include <mach_debug.h>
71#include <mach/port.h>
72#include <kern/lock.h>
73#include <kern/ipc_kobject.h>
74#include <kern/wait_queue.h>
75#include <kern/thread_pool.h>
76
77#include <ipc/ipc_object.h>
78#include <ipc/ipc_mqueue.h>
79#include <ipc/ipc_table.h>
80#include <ipc/ipc_types.h>
81#include <ipc/ipc_entry.h>
82#include <ipc/ipc_space.h>
83
84/*
85 * A receive right (port) can be in four states:
86 * 1) dead (not active, ip_timestamp has death time)
87 * 2) in a space (ip_receiver_name != 0, ip_receiver points
88 * to the space but doesn't hold a ref for it)
89 * 3) in transit (ip_receiver_name == 0, ip_destination points
90 * to the destination port and holds a ref for it)
91 * 4) in limbo (ip_receiver_name == 0, ip_destination == IP_NULL)
92 *
93 * If the port is active, and ip_receiver points to some space,
94 * then ip_receiver_name != 0, and that space holds receive rights.
95 * If the port is not active, then ip_timestamp contains a timestamp
96 * taken when the port was destroyed.
97 */
98
99typedef unsigned int ipc_port_timestamp_t;
100
101typedef unsigned int ipc_port_flags_t;
102
103struct ipc_port {
104
105 /*
106 * Initial sub-structure in common with ipc_pset and rpc_port
107 * First element is an ipc_object
108 */
109 struct ipc_object ip_object;
110
111 union {
112 struct ipc_space *receiver;
113 struct ipc_port *destination;
114 ipc_port_timestamp_t timestamp;
115 } data;
116
117 ipc_kobject_t ip_kobject;
118 struct rpc_subsystem * ip_subsystem;
119 mach_port_mscount_t ip_mscount;
120 mach_port_rights_t ip_srights;
121 mach_port_rights_t ip_sorights;
122
123 struct ipc_port *ip_nsrequest;
124 struct ipc_port *ip_pdrequest;
125 struct ipc_port_request *ip_dnrequests;
126
127 unsigned int ip_pset_count;
128 struct ipc_mqueue ip_messages;
129 struct ipc_kmsg *ip_premsg;
130
131#if NORMA_VM
132 /*
133 * These fields are needed for the use of XMM.
134 * Few ports need this information; it should
135 * be kept in XMM instead (TBD). XXX
136 */
137 long ip_norma_xmm_object_refs;
138 struct ipc_port *ip_norma_xmm_object;
139#endif
140
141#if MACH_ASSERT
142#define IP_NSPARES 10
143#define IP_CALLSTACK_MAX 10
144 queue_chain_t ip_port_links; /* all allocated ports */
145 natural_t ip_thread; /* who made me? thread context */
146 unsigned long ip_timetrack; /* give an idea of "when" created */
147 natural_t ip_callstack[IP_CALLSTACK_MAX]; /* stack trace */
148 unsigned long ip_spares[IP_NSPARES]; /* for debugging */
149#endif /* MACH_ASSERT */
150 int alias;
151};
152
153
154#define ip_references ip_object.io_references
155#define ip_bits ip_object.io_bits
156#define ip_receiver_name ip_object.io_receiver_name
157
158#define ip_thread_pool ip_object.io_thread_pool
159
160#define ip_receiver data.receiver
161#define ip_destination data.destination
162#define ip_timestamp data.timestamp
163
164#define IP_NULL IPC_PORT_NULL
165#define IP_DEAD IPC_PORT_DEAD
166#define IP_VALID(port) IPC_PORT_VALID(port)
167
168#define ip_active(port) io_active(&(port)->ip_object)
169#define ip_lock_init(port) io_lock_init(&(port)->ip_object)
170#define ip_lock(port) io_lock(&(port)->ip_object)
171#define ip_lock_try(port) io_lock_try(&(port)->ip_object)
172#define ip_unlock(port) io_unlock(&(port)->ip_object)
173#define ip_check_unlock(port) io_check_unlock(&(port)->ip_object)
174
175#define ip_reference(port) io_reference(&(port)->ip_object)
176#define ip_release(port) io_release(&(port)->ip_object)
177
178#define ip_kotype(port) io_kotype(&(port)->ip_object)
179
180/*
181 * JMM - Preallocation flag
182 * This flag indicates that there is a message buffer preallocated for this
183 * port and we should use that when sending (from the kernel) rather than
184 * allocate a new one. This avoids deadlocks during notification message
185 * sends by critical system threads (which may be needed to free memory and
186 * therefore cannot be blocked waiting for memory themselves).
187 */
188#define IP_BIT_PREALLOC 0x00008000 /* preallocated mesg */
189#define IP_PREALLOC(port) ((port)->ip_bits & IP_BIT_PREALLOC)
190
191#define IP_SET_PREALLOC(port, kmsg) \
192MACRO_BEGIN \
193 (port)->ip_bits |= IP_BIT_PREALLOC; \
194 (port)->ip_premsg = (kmsg); \
195MACRO_END
196
197#define IP_CLEAR_PREALLOC(port, kmsg) \
198MACRO_BEGIN \
199 assert((port)->ip_premsg == kmsg); \
200 (port)->ip_bits &= ~IP_BIT_PREALLOC; \
201 (port)->ip_premsg = IKM_NULL; \
202MACRO_END
203
204typedef ipc_table_index_t ipc_port_request_index_t;
205
206typedef struct ipc_port_request {
207 union {
208 struct ipc_port *port;
209 ipc_port_request_index_t index;
210 } notify;
211
212 union {
213 mach_port_name_t name;
214 struct ipc_table_size *size;
215 } name;
216} *ipc_port_request_t;
217
218#define ipr_next notify.index
219#define ipr_size name.size
220
221#define ipr_soright notify.port
222#define ipr_name name.name
223
224#define IPR_NULL ((ipc_port_request_t) 0)
225
226/*
227 * Taking the ipc_port_multiple lock grants the privilege
228 * to lock multiple ports at once. No ports must locked
229 * when it is taken.
230 */
231
232decl_mutex_data(extern,ipc_port_multiple_lock_data)
233
234#define ipc_port_multiple_lock_init() \
235 mutex_init(&ipc_port_multiple_lock_data, ETAP_IPC_PORT_MULT)
236
237#define ipc_port_multiple_lock() \
238 mutex_lock(&ipc_port_multiple_lock_data)
239
240#define ipc_port_multiple_unlock() \
241 mutex_unlock(&ipc_port_multiple_lock_data)
242
243/*
244 * The port timestamp facility provides timestamps
245 * for port destruction. It is used to serialize
246 * mach_port_names with port death.
247 */
248
249decl_mutex_data(extern,ipc_port_timestamp_lock_data)
250extern ipc_port_timestamp_t ipc_port_timestamp_data;
251
252#define ipc_port_timestamp_lock_init() \
253 mutex_init(&ipc_port_timestamp_lock_data, ETAP_IPC_PORT_TIME)
254
255#define ipc_port_timestamp_lock() \
256 mutex_lock(&ipc_port_timestamp_lock_data)
257
258#define ipc_port_timestamp_unlock() \
259 mutex_unlock(&ipc_port_timestamp_lock_data)
260
261/* Retrieve a port timestamp value */
262extern ipc_port_timestamp_t ipc_port_timestamp(void);
263
264/*
265 * Compares two timestamps, and returns TRUE if one
266 * happened before two. Note that this formulation
267 * works when the timestamp wraps around at 2^32,
268 * as long as one and two aren't too far apart.
269 */
270
271#define IP_TIMESTAMP_ORDER(one, two) ((int) ((one) - (two)) < 0)
272
273#define ipc_port_translate_receive(space, name, portp) \
274 ipc_object_translate((space), (name), \
275 MACH_PORT_RIGHT_RECEIVE, \
276 (ipc_object_t *) (portp))
277
278#define ipc_port_translate_send(space, name, portp) \
279 ipc_object_translate((space), (name), \
280 MACH_PORT_RIGHT_SEND, \
281 (ipc_object_t *) (portp))
282
283/* Allocate a dead-name request slot */
284extern kern_return_t
285ipc_port_dnrequest(
286 ipc_port_t port,
287 mach_port_name_t name,
288 ipc_port_t soright,
289 ipc_port_request_index_t *indexp);
290
291/* Grow a port's table of dead-name requests */
292extern kern_return_t ipc_port_dngrow(
293 ipc_port_t port,
294 int target_size);
295
296/* Cancel a dead-name request and return the send-once right */
297extern ipc_port_t ipc_port_dncancel(
298 ipc_port_t port,
299 mach_port_name_t name,
300 ipc_port_request_index_t index);
301
302#define ipc_port_dnrename(port, index, oname, nname) \
303MACRO_BEGIN \
304 ipc_port_request_t ipr, table; \
305 \
306 assert(ip_active(port)); \
307 \
308 table = port->ip_dnrequests; \
309 assert(table != IPR_NULL); \
310 \
311 ipr = &table[index]; \
312 assert(ipr->ipr_name == oname); \
313 \
314 ipr->ipr_name = nname; \
315MACRO_END
316
317/* Make a port-deleted request */
318extern void ipc_port_pdrequest(
319 ipc_port_t port,
320 ipc_port_t notify,
321 ipc_port_t *previousp);
322
323/* Make a no-senders request */
324extern void ipc_port_nsrequest(
325 ipc_port_t port,
326 mach_port_mscount_t sync,
327 ipc_port_t notify,
328 ipc_port_t *previousp);
329
330#define ipc_port_set_mscount(port, mscount) \
331MACRO_BEGIN \
332 assert(ip_active(port)); \
333 \
334 (port)->ip_mscount = (mscount); \
335MACRO_END
336
337/* Prepare a receive right for transmission/destruction */
338extern void ipc_port_clear_receiver(
339 ipc_port_t port);
340
341/* Initialize a newly-allocated port */
342extern void ipc_port_init(
343 ipc_port_t port,
344 ipc_space_t space,
345 mach_port_name_t name);
346
347/* Allocate a port */
348extern kern_return_t ipc_port_alloc(
349 ipc_space_t space,
350 mach_port_name_t *namep,
351 ipc_port_t *portp);
352
353/* Allocate a port, with a specific name */
354extern kern_return_t ipc_port_alloc_name(
355 ipc_space_t space,
356 mach_port_name_t name,
357 ipc_port_t *portp);
358
359/* Generate dead name notifications */
360extern void ipc_port_dnnotify(
361 ipc_port_t port,
362 ipc_port_request_t dnrequests);
363
364/* Destroy a port */
365extern void ipc_port_destroy(
366 ipc_port_t port);
367
368/* Check if queueing "port" in a message for "dest" would create a circular
369 group of ports and messages */
370extern boolean_t
371ipc_port_check_circularity(
372 ipc_port_t port,
373 ipc_port_t dest);
374
375/* Make a send-once notify port from a receive right */
376extern ipc_port_t ipc_port_lookup_notify(
377 ipc_space_t space,
378 mach_port_name_t name);
379
380/* Make a naked send right from a receive right */
381extern ipc_port_t ipc_port_make_send(
382 ipc_port_t port);
383
384/* Make a naked send right from another naked send right */
385extern ipc_port_t ipc_port_copy_send(
386 ipc_port_t port);
387
388/* Copyout a naked send right */
389extern mach_port_name_t ipc_port_copyout_send(
390 ipc_port_t sright,
391 ipc_space_t space);
392
393/* Release a (valid) naked send right */
394extern void ipc_port_release_send(
395 ipc_port_t port);
396
397/* Make a naked send-once right from a receive right */
398extern ipc_port_t ipc_port_make_sonce(
399 ipc_port_t port);
400
401/* Release a naked send-once right */
402extern void ipc_port_release_sonce(
403 ipc_port_t port);
404
405/* Release a naked (in limbo or in transit) receive right */
406extern void ipc_port_release_receive(
407 ipc_port_t port);
408
409/* Allocate a port in a special space */
410extern ipc_port_t ipc_port_alloc_special(
411 ipc_space_t space);
412
413/* Deallocate a port in a special space */
414extern void ipc_port_dealloc_special(
415 ipc_port_t port,
416 ipc_space_t space);
417
418#if MACH_ASSERT
419/* Track low-level port deallocation */
420extern void ipc_port_track_dealloc(
421 ipc_port_t port);
422
423/* Initialize general port debugging state */
424extern void ipc_port_debug_init(void);
425#endif /* MACH_ASSERT */
426
427#define ipc_port_alloc_kernel() \
428 ipc_port_alloc_special(ipc_space_kernel)
429#define ipc_port_dealloc_kernel(port) \
430 ipc_port_dealloc_special((port), ipc_space_kernel)
431
432#define ipc_port_alloc_reply() \
433 ipc_port_alloc_special(ipc_space_reply)
434#define ipc_port_dealloc_reply(port) \
435 ipc_port_dealloc_special((port), ipc_space_reply)
436
437#define ipc_port_reference(port) \
438 ipc_object_reference(&(port)->ip_object)
439
440#define ipc_port_release(port) \
441 ipc_object_release(&(port)->ip_object)
442
443#endif /* _IPC_IPC_PORT_H_ */