]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_kmsg.h
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_kmsg.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * File: ipc/ipc_kmsg.h
57 * Author: Rich Draves
58 * Date: 1989
59 *
60 * Definitions for kernel messages.
61 */
62
63 #ifndef _IPC_IPC_KMSG_H_
64 #define _IPC_IPC_KMSG_H_
65
66 #include <cpus.h>
67
68 #include <mach/vm_types.h>
69 #include <mach/message.h>
70 #include <kern/assert.h>
71 #include <kern/cpu_number.h>
72 #include <kern/macro_help.h>
73 #include <kern/kalloc.h>
74 #include <ipc/ipc_object.h>
75
76 /*
77 * This structure is only the header for a kmsg buffer;
78 * the actual buffer is normally larger. The rest of the buffer
79 * holds the body of the message.
80 *
81 * In a kmsg, the port fields hold pointers to ports instead
82 * of port names. These pointers hold references.
83 *
84 * The ikm_header.msgh_remote_port field is the destination
85 * of the message.
86 */
87
88
89 typedef struct ipc_kmsg {
90 struct ipc_kmsg *ikm_next;
91 struct ipc_kmsg *ikm_prev;
92 ipc_port_t ikm_prealloc; /* port we were preallocated from */
93 mach_msg_size_t ikm_size;
94 mach_msg_header_t ikm_header;
95 } *ipc_kmsg_t;
96
97 #define IKM_NULL ((ipc_kmsg_t) 0)
98
99 #define IKM_OVERHEAD \
100 (sizeof(struct ipc_kmsg) - sizeof(mach_msg_header_t))
101
102 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
103 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
104
105 /*
106 * XXX For debugging.
107 */
108 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
109
110 /*
111 * The size of the kernel message buffers that will be cached.
112 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
113 */
114
115 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(256)
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 /* scatter list macros */
233
234 #define SKIP_PORT_DESCRIPTORS(s, e) \
235 MACRO_BEGIN \
236 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
237 while ((s) < (e)) { \
238 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
239 break; \
240 (s)++; \
241 } \
242 if ((s) >= (e)) \
243 (s) = MACH_MSG_DESCRIPTOR_NULL; \
244 } \
245 MACRO_END
246
247 #define INCREMENT_SCATTER(s) \
248 MACRO_BEGIN \
249 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
250 (s)++; \
251 } \
252 MACRO_END
253
254 /*
255 * extern void
256 * ipc_kmsg_send_always(ipc_kmsg_t);
257 *
258 * Unfortunately, to avoid warnings/lint about unused variables
259 * when assertions are turned off, we need two versions of this.
260 */
261 #if MACH_ASSERT
262
263 #define ipc_kmsg_send_always(kmsg) \
264 MACRO_BEGIN \
265 mach_msg_return_t mr; \
266 \
267 mr = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
268 MACH_MSG_TIMEOUT_NONE); \
269 assert(mr == MACH_MSG_SUCCESS); \
270 MACRO_END
271
272 #else /* MACH_ASSERT */
273
274 #define ipc_kmsg_send_always(kmsg) \
275 MACRO_BEGIN \
276 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
277 MACH_MSG_TIMEOUT_NONE); \
278 MACRO_END
279
280 #endif /* MACH_ASSERT */
281
282 /* Allocate a kernel message */
283 extern ipc_kmsg_t ipc_kmsg_alloc(
284 mach_msg_size_t size);
285
286 /* Free a kernel message buffer */
287 extern void ipc_kmsg_free(
288 ipc_kmsg_t kmsg);
289
290 /* Destroy kernel message */
291 extern void ipc_kmsg_destroy(
292 ipc_kmsg_t kmsg);
293
294 /* Preallocate a kernel message buffer */
295 extern void ipc_kmsg_set_prealloc(
296 ipc_kmsg_t kmsg,
297 ipc_port_t port);
298
299 /* Clear a kernel message buffer */
300 extern void ipc_kmsg_clear_prealloc(
301 ipc_kmsg_t kmsg,
302 ipc_port_t port);
303
304 /* Allocate a kernel message buffer and copy a user message to the buffer */
305 extern mach_msg_return_t ipc_kmsg_get(
306 mach_msg_header_t *msg,
307 mach_msg_size_t size,
308 ipc_kmsg_t *kmsgp);
309
310 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
311 extern mach_msg_return_t ipc_kmsg_get_from_kernel(
312 mach_msg_header_t *msg,
313 mach_msg_size_t size,
314 ipc_kmsg_t *kmsgp);
315
316 /* Send a message to a port */
317 extern mach_msg_return_t ipc_kmsg_send(
318 ipc_kmsg_t kmsg,
319 mach_msg_option_t option,
320 mach_msg_timeout_t timeout);
321
322 /* Copy a kernel message buffer to a user message */
323 extern mach_msg_return_t ipc_kmsg_put(
324 mach_msg_header_t *msg,
325 ipc_kmsg_t kmsg,
326 mach_msg_size_t size);
327
328 /* Copy a kernel message buffer to a kernel message */
329 extern void ipc_kmsg_put_to_kernel(
330 mach_msg_header_t *msg,
331 ipc_kmsg_t kmsg,
332 mach_msg_size_t size);
333
334 /* Copyin port rights in the header of a message */
335 extern mach_msg_return_t ipc_kmsg_copyin_header(
336 mach_msg_header_t *msg,
337 ipc_space_t space,
338 mach_port_name_t notify);
339
340 /* Copyin port rights and out-of-line memory from a user message */
341 extern mach_msg_return_t ipc_kmsg_copyin(
342 ipc_kmsg_t kmsg,
343 ipc_space_t space,
344 vm_map_t map,
345 mach_port_name_t notify);
346
347 /* Copyin port rights and out-of-line memory from a kernel message */
348 extern void ipc_kmsg_copyin_from_kernel(
349 ipc_kmsg_t kmsg);
350
351 /* Copyout port rights in the header of a message */
352 extern mach_msg_return_t ipc_kmsg_copyout_header(
353 mach_msg_header_t *msg,
354 ipc_space_t space,
355 mach_port_name_t notify);
356
357 /* Copyout a port right returning a name */
358 extern mach_msg_return_t ipc_kmsg_copyout_object(
359 ipc_space_t space,
360 ipc_object_t object,
361 mach_msg_type_name_t msgt_name,
362 mach_port_name_t *namep);
363
364 /* Copyout the header and body to a user message */
365 extern mach_msg_return_t ipc_kmsg_copyout(
366 ipc_kmsg_t kmsg,
367 ipc_space_t space,
368 vm_map_t map,
369 mach_port_name_t notify,
370 mach_msg_body_t *slist);
371
372 /* Copyout port rights and out-of-line memory from the body of a message */
373 extern mach_msg_return_t ipc_kmsg_copyout_body(
374 ipc_kmsg_t kmsg,
375 ipc_space_t space,
376 vm_map_t map,
377 mach_msg_body_t *slist);
378
379 /* Copyout port rights and out-of-line memory to a user message,
380 not reversing the ports in the header */
381 extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
382 ipc_kmsg_t kmsg,
383 ipc_space_t space,
384 vm_map_t map,
385 mach_msg_body_t *slist);
386
387 /* Copyout the destination port in the message */
388 extern void ipc_kmsg_copyout_dest(
389 ipc_kmsg_t kmsg,
390 ipc_space_t space);
391
392 /* kernel's version of ipc_kmsg_copyout_dest */
393 extern void ipc_kmsg_copyout_to_kernel(
394 ipc_kmsg_t kmsg,
395 ipc_space_t space);
396
397 /* copyin a scatter list and check consistency */
398 extern mach_msg_body_t *ipc_kmsg_copyin_scatter(
399 mach_msg_header_t *msg,
400 mach_msg_size_t slist_size,
401 ipc_kmsg_t kmsg);
402
403 /* free a scatter list */
404 extern void ipc_kmsg_free_scatter(
405 mach_msg_body_t *slist,
406 mach_msg_size_t slist_size);
407
408 #include <mach_kdb.h>
409 #if MACH_KDB
410
411 /* Do a formatted dump of a kernel message */
412 extern void ipc_kmsg_print(
413 ipc_kmsg_t kmsg);
414
415 /* Do a formatted dump of a user message */
416 extern void ipc_msg_print(
417 mach_msg_header_t *msgh);
418
419 #endif /* MACH_KDB */
420
421 #endif /* _IPC_IPC_KMSG_H_ */