]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_kmsg.h
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_kmsg.h
1 /*
2 * Copyright (c) 2000-2004 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 */
58 /*
59 * File: ipc/ipc_kmsg.h
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Definitions for kernel messages.
64 */
65
66 #ifndef _IPC_IPC_KMSG_H_
67 #define _IPC_IPC_KMSG_H_
68
69 #include <mach/vm_types.h>
70 #include <mach/message.h>
71 #include <kern/kern_types.h>
72 #include <kern/assert.h>
73 #include <kern/macro_help.h>
74 #include <ipc/ipc_types.h>
75 #include <ipc/ipc_object.h>
76
77 /*
78 * This structure is only the header for a kmsg buffer;
79 * the actual buffer is normally larger. The rest of the buffer
80 * holds the body of the message.
81 *
82 * In a kmsg, the port fields hold pointers to ports instead
83 * of port names. These pointers hold references.
84 *
85 * The ikm_header.msgh_remote_port field is the destination
86 * of the message.
87 */
88
89
90 struct ipc_kmsg {
91 struct ipc_kmsg *ikm_next;
92 struct ipc_kmsg *ikm_prev;
93 ipc_port_t ikm_prealloc; /* port we were preallocated from */
94 mach_msg_size_t ikm_size;
95 mach_msg_header_t *ikm_header;
96 };
97
98
99 #define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
100
101 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
102 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
103
104 /*
105 * XXX For debugging.
106 */
107 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
108
109 /*
110 * The size of the kernel message buffers that will be cached.
111 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
112 */
113 extern zone_t ipc_kmsg_zone;
114 #define IKM_SAVED_KMSG_SIZE 256
115 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
116
117 #define ikm_prealloc_inuse_port(kmsg) \
118 ((kmsg)->ikm_prealloc)
119
120 #define ikm_prealloc_inuse(kmsg) \
121 ((kmsg)->ikm_prealloc != IP_NULL)
122
123 #define ikm_prealloc_set_inuse(kmsg, port) \
124 MACRO_BEGIN \
125 assert((port) != IP_NULL); \
126 (kmsg)->ikm_prealloc = (port); \
127 ip_reference(port); \
128 MACRO_END
129
130 #define ikm_prealloc_clear_inuse(kmsg, port) \
131 MACRO_BEGIN \
132 (kmsg)->ikm_prealloc = IP_NULL; \
133 ip_release(port); \
134 MACRO_END
135
136
137 #define ikm_init(kmsg, size) \
138 MACRO_BEGIN \
139 (kmsg)->ikm_size = (size); \
140 (kmsg)->ikm_prealloc = IP_NULL; \
141 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
142 MACRO_END
143
144 #define ikm_check_init(kmsg, size) \
145 MACRO_BEGIN \
146 assert((kmsg)->ikm_size == (size)); \
147 assert((kmsg)->ikm_prev == IKM_BOGUS); \
148 assert((kmsg)->ikm_next == IKM_BOGUS); \
149 MACRO_END
150
151 struct ipc_kmsg_queue {
152 struct ipc_kmsg *ikmq_base;
153 };
154
155 typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
156
157 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
158
159
160 /*
161 * Exported interfaces
162 */
163
164 #define ipc_kmsg_queue_init(queue) \
165 MACRO_BEGIN \
166 (queue)->ikmq_base = IKM_NULL; \
167 MACRO_END
168
169 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
170
171 /* Enqueue a kmsg */
172 extern void ipc_kmsg_enqueue(
173 ipc_kmsg_queue_t queue,
174 ipc_kmsg_t kmsg);
175
176 /* Dequeue and return a kmsg */
177 extern ipc_kmsg_t ipc_kmsg_dequeue(
178 ipc_kmsg_queue_t queue);
179
180 /* Pull a kmsg out of a queue */
181 extern void ipc_kmsg_rmqueue(
182 ipc_kmsg_queue_t queue,
183 ipc_kmsg_t kmsg);
184
185 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
186
187 /* Return the kmsg following the given kmsg */
188 extern ipc_kmsg_t ipc_kmsg_queue_next(
189 ipc_kmsg_queue_t queue,
190 ipc_kmsg_t kmsg);
191
192 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
193 MACRO_BEGIN \
194 register ipc_kmsg_t _next; \
195 \
196 assert((queue)->ikmq_base == (kmsg)); \
197 \
198 _next = (kmsg)->ikm_next; \
199 if (_next == (kmsg)) { \
200 assert((kmsg)->ikm_prev == (kmsg)); \
201 (queue)->ikmq_base = IKM_NULL; \
202 } else { \
203 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
204 \
205 (queue)->ikmq_base = _next; \
206 _next->ikm_prev = _prev; \
207 _prev->ikm_next = _next; \
208 } \
209 /* XXX Debug paranoia ASSIGNMENTS */ \
210 assert(kmsg->ikm_next = IKM_BOGUS); \
211 assert(kmsg->ikm_prev = IKM_BOGUS); \
212 MACRO_END
213
214 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
215 MACRO_BEGIN \
216 register ipc_kmsg_t _first = (queue)->ikmq_base; \
217 \
218 if (_first == IKM_NULL) { \
219 (queue)->ikmq_base = (kmsg); \
220 (kmsg)->ikm_next = (kmsg); \
221 (kmsg)->ikm_prev = (kmsg); \
222 } else { \
223 register ipc_kmsg_t _last = _first->ikm_prev; \
224 \
225 (kmsg)->ikm_next = _first; \
226 (kmsg)->ikm_prev = _last; \
227 _first->ikm_prev = (kmsg); \
228 _last->ikm_next = (kmsg); \
229 } \
230 MACRO_END
231
232 /*
233 * extern void
234 * ipc_kmsg_send_always(ipc_kmsg_t);
235 *
236 * Unfortunately, to avoid warnings/lint about unused variables
237 * when assertions are turned off, we need two versions of this.
238 */
239 #if MACH_ASSERT
240
241 #define ipc_kmsg_send_always(kmsg) \
242 MACRO_BEGIN \
243 mach_msg_return_t mr2; \
244 \
245 mr2 = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
246 MACH_MSG_TIMEOUT_NONE); \
247 assert(mr == MACH_MSG_SUCCESS); \
248 MACRO_END
249
250 #else /* MACH_ASSERT */
251
252 #define ipc_kmsg_send_always(kmsg) \
253 MACRO_BEGIN \
254 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
255 MACH_MSG_TIMEOUT_NONE); \
256 MACRO_END
257
258 #endif /* MACH_ASSERT */
259
260
261 /* Allocate a kernel message */
262 extern ipc_kmsg_t ipc_kmsg_alloc(
263 mach_msg_size_t size);
264
265 /* Free a kernel message buffer */
266 extern void ipc_kmsg_free(
267 ipc_kmsg_t kmsg);
268
269 /* Destroy kernel message */
270 extern void ipc_kmsg_destroy(
271 ipc_kmsg_t kmsg);
272
273 /* destroy kernel message and a reference on the dest */
274 extern void ipc_kmsg_destroy_dest(
275 ipc_kmsg_t kmsg);
276
277
278 /* Preallocate a kernel message buffer */
279 extern void ipc_kmsg_set_prealloc(
280 ipc_kmsg_t kmsg,
281 ipc_port_t port);
282
283 /* Clear a kernel message buffer */
284 extern void ipc_kmsg_clear_prealloc(
285 ipc_kmsg_t kmsg,
286 ipc_port_t port);
287
288 /* Allocate a kernel message buffer and copy a user message to the buffer */
289 extern mach_msg_return_t ipc_kmsg_get(
290 mach_vm_address_t msg_addr,
291 mach_msg_size_t size,
292 ipc_kmsg_t *kmsgp);
293
294 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
295 extern mach_msg_return_t ipc_kmsg_get_from_kernel(
296 mach_msg_header_t *msg,
297 mach_msg_size_t size,
298 ipc_kmsg_t *kmsgp);
299
300 /* Send a message to a port */
301 extern mach_msg_return_t ipc_kmsg_send(
302 ipc_kmsg_t kmsg,
303 mach_msg_option_t option,
304 mach_msg_timeout_t timeout_val);
305
306 /* Copy a kernel message buffer to a user message */
307 extern mach_msg_return_t ipc_kmsg_put(
308 mach_vm_address_t msg_addr,
309 ipc_kmsg_t kmsg,
310 mach_msg_size_t size);
311
312 /* Copy a kernel message buffer to a kernel message */
313 extern void ipc_kmsg_put_to_kernel(
314 mach_msg_header_t *msg,
315 ipc_kmsg_t kmsg,
316 mach_msg_size_t size);
317
318 /* Copyin port rights in the header of a message */
319 extern mach_msg_return_t ipc_kmsg_copyin_header(
320 mach_msg_header_t *msg,
321 ipc_space_t space,
322 mach_port_name_t notify);
323
324 /* Copyin port rights and out-of-line memory from a user message */
325 extern mach_msg_return_t ipc_kmsg_copyin(
326 ipc_kmsg_t kmsg,
327 ipc_space_t space,
328 vm_map_t map,
329 mach_port_name_t notify);
330
331 /* Copyin port rights and out-of-line memory from a kernel message */
332 extern void ipc_kmsg_copyin_from_kernel(
333 ipc_kmsg_t kmsg);
334
335 /* Copyout port rights in the header of a message */
336 extern mach_msg_return_t ipc_kmsg_copyout_header(
337 mach_msg_header_t *msg,
338 ipc_space_t space,
339 mach_port_name_t notify);
340
341 /* Copyout a port right returning a name */
342 extern mach_msg_return_t ipc_kmsg_copyout_object(
343 ipc_space_t space,
344 ipc_object_t object,
345 mach_msg_type_name_t msgt_name,
346 mach_port_name_t *namep);
347
348 /* Copyout the header and body to a user message */
349 extern mach_msg_return_t ipc_kmsg_copyout(
350 ipc_kmsg_t kmsg,
351 ipc_space_t space,
352 vm_map_t map,
353 mach_port_name_t notify,
354 mach_msg_body_t *slist);
355
356 /* Copyout port rights and out-of-line memory from the body of a message */
357 extern mach_msg_return_t ipc_kmsg_copyout_body(
358 ipc_kmsg_t kmsg,
359 ipc_space_t space,
360 vm_map_t map,
361 mach_msg_body_t *slist);
362
363 /* Copyout port rights and out-of-line memory to a user message,
364 not reversing the ports in the header */
365 extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
366 ipc_kmsg_t kmsg,
367 ipc_space_t space,
368 vm_map_t map,
369 mach_msg_body_t *slist);
370
371 /* Compute size of message as copied out to the specified space/map */
372 extern mach_msg_size_t ipc_kmsg_copyout_size(
373 ipc_kmsg_t kmsg,
374 vm_map_t map);
375
376 /* Copyout the destination port in the message */
377 extern void ipc_kmsg_copyout_dest(
378 ipc_kmsg_t kmsg,
379 ipc_space_t space);
380
381 /* kernel's version of ipc_kmsg_copyout_dest */
382 extern void ipc_kmsg_copyout_to_kernel(
383 ipc_kmsg_t kmsg,
384 ipc_space_t space);
385
386 /* get a scatter list and check consistency */
387 extern mach_msg_body_t *ipc_kmsg_get_scatter(
388 mach_vm_address_t msg_addr,
389 mach_msg_size_t slist_size,
390 ipc_kmsg_t kmsg);
391
392 /* free a scatter list */
393 extern void ipc_kmsg_free_scatter(
394 mach_msg_body_t *slist,
395 mach_msg_size_t slist_size);
396
397 #endif /* _IPC_IPC_KMSG_H_ */