]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_kmsg.h
xnu-792.13.8.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_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60 /*
61 * File: ipc/ipc_kmsg.h
62 * Author: Rich Draves
63 * Date: 1989
64 *
65 * Definitions for kernel messages.
66 */
67
68 #ifndef _IPC_IPC_KMSG_H_
69 #define _IPC_IPC_KMSG_H_
70
71 #include <mach/vm_types.h>
72 #include <mach/message.h>
73 #include <kern/kern_types.h>
74 #include <kern/assert.h>
75 #include <kern/macro_help.h>
76 #include <ipc/ipc_types.h>
77 #include <ipc/ipc_object.h>
78
79 /*
80 * This structure is only the header for a kmsg buffer;
81 * the actual buffer is normally larger. The rest of the buffer
82 * holds the body of the message.
83 *
84 * In a kmsg, the port fields hold pointers to ports instead
85 * of port names. These pointers hold references.
86 *
87 * The ikm_header.msgh_remote_port field is the destination
88 * of the message.
89 */
90
91
92 struct ipc_kmsg {
93 struct ipc_kmsg *ikm_next;
94 struct ipc_kmsg *ikm_prev;
95 ipc_port_t ikm_prealloc; /* port we were preallocated from */
96 mach_msg_size_t ikm_size;
97 mach_msg_header_t *ikm_header;
98 };
99
100
101 #define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
102
103 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
104 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
105
106 /*
107 * XXX For debugging.
108 */
109 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
110
111 /*
112 * The size of the kernel message buffers that will be cached.
113 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
114 */
115 extern zone_t ipc_kmsg_zone;
116 #define IKM_SAVED_KMSG_SIZE 256
117 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
118
119 #define ikm_prealloc_inuse_port(kmsg) \
120 ((kmsg)->ikm_prealloc)
121
122 #define ikm_prealloc_inuse(kmsg) \
123 ((kmsg)->ikm_prealloc != IP_NULL)
124
125 #define ikm_prealloc_set_inuse(kmsg, port) \
126 MACRO_BEGIN \
127 assert((port) != IP_NULL); \
128 (kmsg)->ikm_prealloc = (port); \
129 ip_reference(port); \
130 MACRO_END
131
132 #define ikm_prealloc_clear_inuse(kmsg, port) \
133 MACRO_BEGIN \
134 (kmsg)->ikm_prealloc = IP_NULL; \
135 ip_release(port); \
136 MACRO_END
137
138
139 #define ikm_init(kmsg, size) \
140 MACRO_BEGIN \
141 (kmsg)->ikm_size = (size); \
142 (kmsg)->ikm_prealloc = IP_NULL; \
143 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
144 MACRO_END
145
146 #define ikm_check_init(kmsg, size) \
147 MACRO_BEGIN \
148 assert((kmsg)->ikm_size == (size)); \
149 assert((kmsg)->ikm_prev == IKM_BOGUS); \
150 assert((kmsg)->ikm_next == IKM_BOGUS); \
151 MACRO_END
152
153 struct ipc_kmsg_queue {
154 struct ipc_kmsg *ikmq_base;
155 };
156
157 typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
158
159 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
160
161
162 /*
163 * Exported interfaces
164 */
165
166 #define ipc_kmsg_queue_init(queue) \
167 MACRO_BEGIN \
168 (queue)->ikmq_base = IKM_NULL; \
169 MACRO_END
170
171 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
172
173 /* Enqueue a kmsg */
174 extern void ipc_kmsg_enqueue(
175 ipc_kmsg_queue_t queue,
176 ipc_kmsg_t kmsg);
177
178 /* Dequeue and return a kmsg */
179 extern ipc_kmsg_t ipc_kmsg_dequeue(
180 ipc_kmsg_queue_t queue);
181
182 /* Pull a kmsg out of a queue */
183 extern void ipc_kmsg_rmqueue(
184 ipc_kmsg_queue_t queue,
185 ipc_kmsg_t kmsg);
186
187 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
188
189 /* Return the kmsg following the given kmsg */
190 extern ipc_kmsg_t ipc_kmsg_queue_next(
191 ipc_kmsg_queue_t queue,
192 ipc_kmsg_t kmsg);
193
194 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
195 MACRO_BEGIN \
196 register ipc_kmsg_t _next; \
197 \
198 assert((queue)->ikmq_base == (kmsg)); \
199 \
200 _next = (kmsg)->ikm_next; \
201 if (_next == (kmsg)) { \
202 assert((kmsg)->ikm_prev == (kmsg)); \
203 (queue)->ikmq_base = IKM_NULL; \
204 } else { \
205 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
206 \
207 (queue)->ikmq_base = _next; \
208 _next->ikm_prev = _prev; \
209 _prev->ikm_next = _next; \
210 } \
211 /* XXX Debug paranoia ASSIGNMENTS */ \
212 assert(kmsg->ikm_next = IKM_BOGUS); \
213 assert(kmsg->ikm_prev = IKM_BOGUS); \
214 MACRO_END
215
216 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
217 MACRO_BEGIN \
218 register ipc_kmsg_t _first = (queue)->ikmq_base; \
219 \
220 if (_first == IKM_NULL) { \
221 (queue)->ikmq_base = (kmsg); \
222 (kmsg)->ikm_next = (kmsg); \
223 (kmsg)->ikm_prev = (kmsg); \
224 } else { \
225 register ipc_kmsg_t _last = _first->ikm_prev; \
226 \
227 (kmsg)->ikm_next = _first; \
228 (kmsg)->ikm_prev = _last; \
229 _first->ikm_prev = (kmsg); \
230 _last->ikm_next = (kmsg); \
231 } \
232 MACRO_END
233
234 /*
235 * extern void
236 * ipc_kmsg_send_always(ipc_kmsg_t);
237 *
238 * Unfortunately, to avoid warnings/lint about unused variables
239 * when assertions are turned off, we need two versions of this.
240 */
241 #if MACH_ASSERT
242
243 #define ipc_kmsg_send_always(kmsg) \
244 MACRO_BEGIN \
245 mach_msg_return_t mr2; \
246 \
247 mr2 = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
248 MACH_MSG_TIMEOUT_NONE); \
249 assert(mr == MACH_MSG_SUCCESS); \
250 MACRO_END
251
252 #else /* MACH_ASSERT */
253
254 #define ipc_kmsg_send_always(kmsg) \
255 MACRO_BEGIN \
256 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
257 MACH_MSG_TIMEOUT_NONE); \
258 MACRO_END
259
260 #endif /* MACH_ASSERT */
261
262
263 /* Allocate a kernel message */
264 extern ipc_kmsg_t ipc_kmsg_alloc(
265 mach_msg_size_t size);
266
267 /* Free a kernel message buffer */
268 extern void ipc_kmsg_free(
269 ipc_kmsg_t kmsg);
270
271 /* Destroy kernel message */
272 extern void ipc_kmsg_destroy(
273 ipc_kmsg_t kmsg);
274
275 /* destroy kernel message and a reference on the dest */
276 extern void ipc_kmsg_destroy_dest(
277 ipc_kmsg_t kmsg);
278
279
280 /* Preallocate a kernel message buffer */
281 extern void ipc_kmsg_set_prealloc(
282 ipc_kmsg_t kmsg,
283 ipc_port_t port);
284
285 /* Clear a kernel message buffer */
286 extern void ipc_kmsg_clear_prealloc(
287 ipc_kmsg_t kmsg,
288 ipc_port_t port);
289
290 /* Allocate a kernel message buffer and copy a user message to the buffer */
291 extern mach_msg_return_t ipc_kmsg_get(
292 mach_vm_address_t msg_addr,
293 mach_msg_size_t size,
294 ipc_kmsg_t *kmsgp);
295
296 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
297 extern mach_msg_return_t ipc_kmsg_get_from_kernel(
298 mach_msg_header_t *msg,
299 mach_msg_size_t size,
300 ipc_kmsg_t *kmsgp);
301
302 /* Send a message to a port */
303 extern mach_msg_return_t ipc_kmsg_send(
304 ipc_kmsg_t kmsg,
305 mach_msg_option_t option,
306 mach_msg_timeout_t timeout_val);
307
308 /* Copy a kernel message buffer to a user message */
309 extern mach_msg_return_t ipc_kmsg_put(
310 mach_vm_address_t msg_addr,
311 ipc_kmsg_t kmsg,
312 mach_msg_size_t size);
313
314 /* Copy a kernel message buffer to a kernel message */
315 extern void ipc_kmsg_put_to_kernel(
316 mach_msg_header_t *msg,
317 ipc_kmsg_t kmsg,
318 mach_msg_size_t size);
319
320 /* Copyin port rights in the header of a message */
321 extern mach_msg_return_t ipc_kmsg_copyin_header(
322 mach_msg_header_t *msg,
323 ipc_space_t space,
324 mach_port_name_t notify);
325
326 /* Copyin port rights and out-of-line memory from a user message */
327 extern mach_msg_return_t ipc_kmsg_copyin(
328 ipc_kmsg_t kmsg,
329 ipc_space_t space,
330 vm_map_t map,
331 mach_port_name_t notify);
332
333 /* Copyin port rights and out-of-line memory from a kernel message */
334 extern void ipc_kmsg_copyin_from_kernel(
335 ipc_kmsg_t kmsg);
336
337 /* Copyout port rights in the header of a message */
338 extern mach_msg_return_t ipc_kmsg_copyout_header(
339 mach_msg_header_t *msg,
340 ipc_space_t space,
341 mach_port_name_t notify);
342
343 /* Copyout a port right returning a name */
344 extern mach_msg_return_t ipc_kmsg_copyout_object(
345 ipc_space_t space,
346 ipc_object_t object,
347 mach_msg_type_name_t msgt_name,
348 mach_port_name_t *namep);
349
350 /* Copyout the header and body to a user message */
351 extern mach_msg_return_t ipc_kmsg_copyout(
352 ipc_kmsg_t kmsg,
353 ipc_space_t space,
354 vm_map_t map,
355 mach_port_name_t notify,
356 mach_msg_body_t *slist);
357
358 /* Copyout port rights and out-of-line memory from the body of a message */
359 extern mach_msg_return_t ipc_kmsg_copyout_body(
360 ipc_kmsg_t kmsg,
361 ipc_space_t space,
362 vm_map_t map,
363 mach_msg_body_t *slist);
364
365 /* Copyout port rights and out-of-line memory to a user message,
366 not reversing the ports in the header */
367 extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
368 ipc_kmsg_t kmsg,
369 ipc_space_t space,
370 vm_map_t map,
371 mach_msg_body_t *slist);
372
373 /* Compute size of message as copied out to the specified space/map */
374 extern mach_msg_size_t ipc_kmsg_copyout_size(
375 ipc_kmsg_t kmsg,
376 vm_map_t map);
377
378 /* Copyout the destination port in the message */
379 extern void ipc_kmsg_copyout_dest(
380 ipc_kmsg_t kmsg,
381 ipc_space_t space);
382
383 /* kernel's version of ipc_kmsg_copyout_dest */
384 extern void ipc_kmsg_copyout_to_kernel(
385 ipc_kmsg_t kmsg,
386 ipc_space_t space);
387
388 /* get a scatter list and check consistency */
389 extern mach_msg_body_t *ipc_kmsg_get_scatter(
390 mach_vm_address_t msg_addr,
391 mach_msg_size_t slist_size,
392 ipc_kmsg_t kmsg);
393
394 /* free a scatter list */
395 extern void ipc_kmsg_free_scatter(
396 mach_msg_body_t *slist,
397 mach_msg_size_t slist_size);
398
399 #endif /* _IPC_IPC_KMSG_H_ */