2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
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.
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.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: ipc/ipc_kmsg.h
60 * Definitions for kernel messages.
63 #ifndef _IPC_IPC_KMSG_H_
64 #define _IPC_IPC_KMSG_H_
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>
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.
81 * In a kmsg, the port fields hold pointers to ports instead
82 * of port names. These pointers hold references.
84 * The ikm_header.msgh_remote_port field is the destination
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
;
97 #define IKM_NULL ((ipc_kmsg_t) 0)
99 #define IKM_OVERHEAD \
100 (sizeof(struct ipc_kmsg) - sizeof(mach_msg_header_t))
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))
108 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
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.
115 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(256)
117 #define ikm_prealloc_inuse_port(kmsg) \
118 ((kmsg)->ikm_prealloc)
120 #define ikm_prealloc_inuse(kmsg) \
121 ((kmsg)->ikm_prealloc != IP_NULL)
123 #define ikm_prealloc_set_inuse(kmsg, port) \
125 assert((port) != IP_NULL); \
126 (kmsg)->ikm_prealloc = (port); \
127 ip_reference(port); \
130 #define ikm_prealloc_clear_inuse(kmsg, port) \
132 (kmsg)->ikm_prealloc = IP_NULL; \
137 #define ikm_init(kmsg, size) \
139 (kmsg)->ikm_size = (size); \
140 (kmsg)->ikm_prealloc = IP_NULL; \
141 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
144 #define ikm_check_init(kmsg, size) \
146 assert((kmsg)->ikm_size == (size)); \
147 assert((kmsg)->ikm_prev == IKM_BOGUS); \
148 assert((kmsg)->ikm_next == IKM_BOGUS); \
151 struct ipc_kmsg_queue
{
152 struct ipc_kmsg
*ikmq_base
;
155 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
157 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
161 * Exported interfaces
164 #define ipc_kmsg_queue_init(queue) \
166 (queue)->ikmq_base = IKM_NULL; \
169 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
172 extern void ipc_kmsg_enqueue(
173 ipc_kmsg_queue_t queue
,
176 /* Dequeue and return a kmsg */
177 extern ipc_kmsg_t
ipc_kmsg_dequeue(
178 ipc_kmsg_queue_t queue
);
180 /* Pull a kmsg out of a queue */
181 extern void ipc_kmsg_rmqueue(
182 ipc_kmsg_queue_t queue
,
185 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
187 /* Return the kmsg following the given kmsg */
188 extern ipc_kmsg_t
ipc_kmsg_queue_next(
189 ipc_kmsg_queue_t queue
,
192 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
194 register ipc_kmsg_t _next; \
196 assert((queue)->ikmq_base == (kmsg)); \
198 _next = (kmsg)->ikm_next; \
199 if (_next == (kmsg)) { \
200 assert((kmsg)->ikm_prev == (kmsg)); \
201 (queue)->ikmq_base = IKM_NULL; \
203 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
205 (queue)->ikmq_base = _next; \
206 _next->ikm_prev = _prev; \
207 _prev->ikm_next = _next; \
209 /* XXX Debug paranoia ASSIGNMENTS */ \
210 assert(kmsg->ikm_next = IKM_BOGUS); \
211 assert(kmsg->ikm_prev = IKM_BOGUS); \
214 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
216 register ipc_kmsg_t _first = (queue)->ikmq_base; \
218 if (_first == IKM_NULL) { \
219 (queue)->ikmq_base = (kmsg); \
220 (kmsg)->ikm_next = (kmsg); \
221 (kmsg)->ikm_prev = (kmsg); \
223 register ipc_kmsg_t _last = _first->ikm_prev; \
225 (kmsg)->ikm_next = _first; \
226 (kmsg)->ikm_prev = _last; \
227 _first->ikm_prev = (kmsg); \
228 _last->ikm_next = (kmsg); \
232 /* scatter list macros */
234 #define SKIP_PORT_DESCRIPTORS(s, e) \
236 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
237 while ((s) < (e)) { \
238 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
243 (s) = MACH_MSG_DESCRIPTOR_NULL; \
247 #define INCREMENT_SCATTER(s) \
249 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
256 * ipc_kmsg_send_always(ipc_kmsg_t);
258 * Unfortunately, to avoid warnings/lint about unused variables
259 * when assertions are turned off, we need two versions of this.
263 #define ipc_kmsg_send_always(kmsg) \
265 mach_msg_return_t mr; \
267 mr = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
268 MACH_MSG_TIMEOUT_NONE); \
269 assert(mr == MACH_MSG_SUCCESS); \
272 #else /* MACH_ASSERT */
274 #define ipc_kmsg_send_always(kmsg) \
276 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
277 MACH_MSG_TIMEOUT_NONE); \
280 #endif /* MACH_ASSERT */
282 /* Allocate a kernel message */
283 extern ipc_kmsg_t
ipc_kmsg_alloc(
284 mach_msg_size_t size
);
286 /* Free a kernel message buffer */
287 extern void ipc_kmsg_free(
290 /* Destroy kernel message */
291 extern void ipc_kmsg_destroy(
294 /* Preallocate a kernel message buffer */
295 extern void ipc_kmsg_set_prealloc(
299 /* Clear a kernel message buffer */
300 extern void ipc_kmsg_clear_prealloc(
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
,
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
,
316 /* Send a message to a port */
317 extern mach_msg_return_t
ipc_kmsg_send(
319 mach_msg_option_t option
,
320 mach_msg_timeout_t timeout
);
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
,
326 mach_msg_size_t size
);
328 /* Copy a kernel message buffer to a kernel message */
329 extern void ipc_kmsg_put_to_kernel(
330 mach_msg_header_t
*msg
,
332 mach_msg_size_t size
);
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
,
338 mach_port_name_t notify
);
340 /* Copyin port rights and out-of-line memory from a user message */
341 extern mach_msg_return_t
ipc_kmsg_copyin(
345 mach_port_name_t notify
);
347 /* Copyin port rights and out-of-line memory from a kernel message */
348 extern void ipc_kmsg_copyin_from_kernel(
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
,
355 mach_port_name_t notify
);
357 /* Copyout a port right returning a name */
358 extern mach_msg_return_t
ipc_kmsg_copyout_object(
361 mach_msg_type_name_t msgt_name
,
362 mach_port_name_t
*namep
);
364 /* Copyout the header and body to a user message */
365 extern mach_msg_return_t
ipc_kmsg_copyout(
369 mach_port_name_t notify
,
370 mach_msg_body_t
*slist
);
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(
377 mach_msg_body_t
*slist
);
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(
385 mach_msg_body_t
*slist
);
387 /* Copyout the destination port in the message */
388 extern void ipc_kmsg_copyout_dest(
392 /* kernel's version of ipc_kmsg_copyout_dest */
393 extern void ipc_kmsg_copyout_to_kernel(
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
,
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
);
408 #include <mach_kdb.h>
411 /* Do a formatted dump of a kernel message */
412 extern void ipc_kmsg_print(
415 /* Do a formatted dump of a user message */
416 extern void ipc_msg_print(
417 mach_msg_header_t
*msgh
);
419 #endif /* MACH_KDB */
421 #endif /* _IPC_IPC_KMSG_H_ */