]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_mqueue.h
xnu-6153.61.1.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_mqueue.h
1 /*
2 * Copyright (c) 2000-2007 Apple 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 */
58 /*
59 * File: ipc/ipc_mqueue.h
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Definitions for message queues.
64 */
65
66 #ifndef _IPC_IPC_MQUEUE_H_
67 #define _IPC_IPC_MQUEUE_H_
68
69 #include <mach_assert.h>
70
71 #include <mach/message.h>
72
73 #include <kern/assert.h>
74 #include <kern/macro_help.h>
75 #include <kern/kern_types.h>
76 #include <kern/waitq.h>
77
78 #include <ipc/ipc_kmsg.h>
79 #include <ipc/ipc_object.h>
80 #include <ipc/ipc_types.h>
81
82 #include <sys/event.h>
83
84 typedef struct ipc_mqueue {
85 union {
86 struct {
87 struct waitq waitq;
88 struct ipc_kmsg_queue messages;
89 mach_port_seqno_t seqno;
90 mach_port_name_t receiver_name;
91 uint16_t msgcount;
92 uint16_t qlimit;
93 #ifdef __LP64__
94 uint32_t qcontext;
95 #endif
96 #if MACH_FLIPC
97 struct flipc_port *fport; // Null for local port, or ptr to flipc port
98 #endif
99 } port;
100 struct {
101 struct waitq_set setq;
102 } pset;
103 } data;
104 union {
105 /*
106 * Port Sets:
107 * only use imq_klist
108 *
109 * Special Reply Ports (ip_specialreply == true):
110 * only use imq_srp_owner_thread
111 *
112 * Ports, based on ip_sync_link_state, use:
113 * - PORT_SYNC_LINK_ANY: imq_klist
114 * - PORT_SYNC_LINK_WORKLOOP_KNOTE: imq_inheritor_knote
115 * - PORT_SYNC_LINK_WORKLOOP_STASH: imq_inheritor_turnstile
116 * - PORT_SYNC_LINK_RCV_THREAD: imq_inheritor_thread_ref
117 */
118 struct klist imq_klist;
119 struct knote *imq_inheritor_knote;
120 struct turnstile *imq_inheritor_turnstile;
121 thread_t imq_inheritor_thread_ref;
122 thread_t imq_srp_owner_thread;
123 };
124 #ifndef __LP64__
125 uint32_t qcontext;
126 #endif
127 } *ipc_mqueue_t;
128
129 #define IMQ_NULL ((ipc_mqueue_t) 0)
130
131 #define imq_wait_queue data.port.waitq
132 #define imq_messages data.port.messages
133 #define imq_msgcount data.port.msgcount
134 #define imq_qlimit data.port.qlimit
135 #define imq_seqno data.port.seqno
136 #define imq_receiver_name data.port.receiver_name
137 #if MACH_FLIPC
138 #define imq_fport data.port.fport
139 #endif
140
141 /*
142 * The qcontext structure member fills in a 32-bit padding gap in ipc_mqueue.
143 * However, the 32-bits are in slightly different places on 32 and 64 bit systems.
144 */
145 #ifdef __LP64__
146 #define imq_context data.port.qcontext
147 #else
148 #define imq_context qcontext
149 #endif
150
151 /*
152 * we can use the 'eventmask' bits of the waitq b/c
153 * they are only used by global queues
154 */
155 #define imq_fullwaiters data.port.waitq.waitq_eventmask
156 #define imq_in_pset data.port.waitq.waitq_set_id
157 #define imq_preposts data.port.waitq.waitq_prepost_id
158
159 #define imq_set_queue data.pset.setq
160 #define imq_is_set(mq) waitqs_is_set(&(mq)->imq_set_queue)
161 #define imq_is_queue(mq) waitq_is_queue(&(mq)->imq_wait_queue)
162 #define imq_is_turnstile_proxy(mq) \
163 waitq_is_turnstile_proxy(&(mq)->imq_wait_queue)
164 #define imq_is_valid(mq) waitq_is_valid(&(mq)->imq_wait_queue)
165
166 #define imq_unlock(mq) waitq_unlock(&(mq)->imq_wait_queue)
167 #define imq_held(mq) waitq_held(&(mq)->imq_wait_queue)
168 #define imq_valid(mq) waitq_valid(&(mq)->imq_wait_queue)
169
170 extern void imq_lock(ipc_mqueue_t mq);
171 extern unsigned int imq_lock_try(ipc_mqueue_t mq);
172
173 /*
174 * Get an ipc_mqueue pointer from a waitq pointer. These are traditionally the
175 * same pointer, but this conversion makes no assumptions on union structure
176 * member positions - it should allow the waitq to move around in either the
177 * port-set mqueue or the port mqueue independently.
178 */
179 #define imq_from_waitq(waitq) (waitq_is_set(waitq) ? \
180 __container_of(waitq, struct ipc_mqueue, imq_set_queue.wqset_q) : \
181 __container_of(waitq, struct ipc_mqueue, imq_wait_queue))
182
183 #define imq_to_object(mq) ip_to_object(ip_from_mq(mq))
184
185 extern void imq_reserve_and_lock(ipc_mqueue_t mq,
186 uint64_t *reserved_prepost);
187
188 extern void imq_release_and_unlock(ipc_mqueue_t mq,
189 uint64_t reserved_prepost);
190
191 #define imq_full(mq) ((mq)->imq_msgcount >= (mq)->imq_qlimit)
192 #define imq_full_kernel(mq) ((mq)->imq_msgcount >= MACH_PORT_QLIMIT_KERNEL)
193
194 extern int ipc_mqueue_full;
195 // extern int ipc_mqueue_rcv;
196
197 #define IPC_MQUEUE_FULL CAST_EVENT64_T(&ipc_mqueue_full)
198 #define IPC_MQUEUE_RECEIVE NO_EVENT64
199
200 /*
201 * Exported interfaces
202 */
203
204 __enum_closed_decl(ipc_mqueue_kind_t, int, {
205 IPC_MQUEUE_KIND_NONE, /* this mqueue really isn't used */
206 IPC_MQUEUE_KIND_PORT, /* this queue is a regular port queue */
207 IPC_MQUEUE_KIND_SET, /* this queue is a portset queue */
208 });
209
210 /* Initialize a newly-allocated message queue */
211 extern void ipc_mqueue_init(
212 ipc_mqueue_t mqueue,
213 ipc_mqueue_kind_t kind);
214
215 /* de-initialize / cleanup an mqueue (specifically waitq resources) */
216 extern void ipc_mqueue_deinit(
217 ipc_mqueue_t mqueue);
218
219 /* destroy an mqueue */
220 extern boolean_t ipc_mqueue_destroy_locked(
221 ipc_mqueue_t mqueue);
222
223 /* Wake up receivers waiting in a message queue */
224 extern void ipc_mqueue_changed(
225 ipc_space_t space,
226 ipc_mqueue_t mqueue);
227
228 /* Add the specific mqueue as a member of the set */
229 extern kern_return_t ipc_mqueue_add(
230 ipc_mqueue_t mqueue,
231 ipc_mqueue_t set_mqueue,
232 uint64_t *reserved_link,
233 uint64_t *reserved_prepost);
234
235 /* Check to see if mqueue is member of set_mqueue */
236 extern boolean_t ipc_mqueue_member(
237 ipc_mqueue_t mqueue,
238 ipc_mqueue_t set_mqueue);
239
240 /* Remove an mqueue from a specific set */
241 extern kern_return_t ipc_mqueue_remove(
242 ipc_mqueue_t mqueue,
243 ipc_mqueue_t set_mqueue);
244
245 /* Remove an mqueue from all sets */
246 extern void ipc_mqueue_remove_from_all(
247 ipc_mqueue_t mqueue);
248
249 /* Remove all the members of the specifiied set */
250 extern void ipc_mqueue_remove_all(
251 ipc_mqueue_t mqueue);
252
253 /* Send a message to a port */
254 extern mach_msg_return_t ipc_mqueue_send(
255 ipc_mqueue_t mqueue,
256 ipc_kmsg_t kmsg,
257 mach_msg_option_t option,
258 mach_msg_timeout_t timeout_val);
259
260 /* check for queue send queue full of a port */
261 extern mach_msg_return_t ipc_mqueue_preflight_send(
262 ipc_mqueue_t mqueue,
263 ipc_kmsg_t kmsg,
264 mach_msg_option_t option,
265 mach_msg_timeout_t timeout_val);
266
267 /* Set a [send-possible] override on the mqueue */
268 extern void ipc_mqueue_override_send(
269 ipc_mqueue_t mqueue,
270 mach_msg_priority_t override);
271
272 /* Deliver message to message queue or waiting receiver */
273 extern void ipc_mqueue_post(
274 ipc_mqueue_t mqueue,
275 ipc_kmsg_t kmsg,
276 mach_msg_option_t option);
277
278 /* Receive a message from a message queue */
279 extern void ipc_mqueue_receive(
280 ipc_mqueue_t mqueue,
281 mach_msg_option_t option,
282 mach_msg_size_t max_size,
283 mach_msg_timeout_t timeout_val,
284 int interruptible);
285
286 /* Receive a message from a message queue using a specified thread */
287 extern wait_result_t ipc_mqueue_receive_on_thread(
288 ipc_mqueue_t mqueue,
289 mach_msg_option_t option,
290 mach_msg_size_t max_size,
291 mach_msg_timeout_t rcv_timeout,
292 int interruptible,
293 thread_t thread);
294
295 /* Continuation routine for message receive */
296 extern void ipc_mqueue_receive_continue(
297 void *param,
298 wait_result_t wresult);
299
300 /* Select a message from a queue and try to post it to ourself */
301 extern void ipc_mqueue_select_on_thread(
302 ipc_mqueue_t port_mq,
303 ipc_mqueue_t set_mq,
304 mach_msg_option_t option,
305 mach_msg_size_t max_size,
306 thread_t thread);
307
308 /* Peek into a messaqe queue to see if there are messages */
309 extern unsigned ipc_mqueue_peek(
310 ipc_mqueue_t mqueue,
311 mach_port_seqno_t *msg_seqnop,
312 mach_msg_size_t *msg_sizep,
313 mach_msg_id_t *msg_idp,
314 mach_msg_max_trailer_t *msg_trailerp,
315 ipc_kmsg_t *kmsgp);
316
317 /* Peek into a locked messaqe queue to see if there are messages */
318 extern unsigned ipc_mqueue_peek_locked(
319 ipc_mqueue_t mqueue,
320 mach_port_seqno_t *msg_seqnop,
321 mach_msg_size_t *msg_sizep,
322 mach_msg_id_t *msg_idp,
323 mach_msg_max_trailer_t *msg_trailerp,
324 ipc_kmsg_t *kmsgp);
325
326 /* Peek into a messaqe queue set to see if there are queues with messages */
327 extern unsigned ipc_mqueue_set_peek(
328 ipc_mqueue_t mqueue);
329
330 /* Release an mqueue/port reference that was granted by MACH_PEEK_MSG */
331 extern void ipc_mqueue_release_peek_ref(
332 ipc_mqueue_t mqueue);
333
334 /* Gather the names of member port for a given set */
335 extern void ipc_mqueue_set_gather_member_names(
336 ipc_space_t space,
337 ipc_mqueue_t set_mq,
338 ipc_entry_num_t maxnames,
339 mach_port_name_t *names,
340 ipc_entry_num_t *actualp);
341
342 /* Clear a message count reservation */
343 extern void ipc_mqueue_release_msgcount(
344 ipc_mqueue_t port_mq,
345 ipc_mqueue_t set_mq);
346
347 /* Change a queue limit */
348 extern void ipc_mqueue_set_qlimit(
349 ipc_mqueue_t mqueue,
350 mach_port_msgcount_t qlimit);
351
352 /* Change a queue's sequence number */
353 extern void ipc_mqueue_set_seqno(
354 ipc_mqueue_t mqueue,
355 mach_port_seqno_t seqno);
356
357 /* Convert a name in a space to a message queue */
358 extern mach_msg_return_t ipc_mqueue_copyin(
359 ipc_space_t space,
360 mach_port_name_t name,
361 ipc_mqueue_t *mqueuep,
362 ipc_object_t *objectp);
363
364 #endif /* _IPC_IPC_MQUEUE_H_ */