2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: ipc/ipc_kmsg.h
57 * Definitions for kernel messages.
60 #ifndef _IPC_IPC_KMSG_H_
61 #define _IPC_IPC_KMSG_H_
63 #include <mach/vm_types.h>
64 #include <mach/message.h>
65 #include <kern/kern_types.h>
66 #include <kern/assert.h>
67 #include <kern/macro_help.h>
68 #include <ipc/ipc_types.h>
69 #include <ipc/ipc_object.h>
72 * This structure is only the header for a kmsg buffer;
73 * the actual buffer is normally larger. The rest of the buffer
74 * holds the body of the message.
76 * In a kmsg, the port fields hold pointers to ports instead
77 * of port names. These pointers hold references.
79 * The ikm_header.msgh_remote_port field is the destination
85 struct ipc_kmsg
*ikm_next
;
86 struct ipc_kmsg
*ikm_prev
;
87 ipc_port_t ikm_prealloc
; /* port we were preallocated from */
88 mach_msg_size_t ikm_size
;
89 mach_msg_header_t
*ikm_header
;
93 #define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
95 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
96 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
101 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
104 * The size of the kernel message buffers that will be cached.
105 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
107 extern zone_t ipc_kmsg_zone
;
108 #define IKM_SAVED_KMSG_SIZE 256
109 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
111 #define ikm_prealloc_inuse_port(kmsg) \
112 ((kmsg)->ikm_prealloc)
114 #define ikm_prealloc_inuse(kmsg) \
115 ((kmsg)->ikm_prealloc != IP_NULL)
117 #define ikm_prealloc_set_inuse(kmsg, port) \
119 assert((port) != IP_NULL); \
120 (kmsg)->ikm_prealloc = (port); \
121 ip_reference(port); \
124 #define ikm_prealloc_clear_inuse(kmsg, port) \
126 (kmsg)->ikm_prealloc = IP_NULL; \
131 #define ikm_init(kmsg, size) \
133 (kmsg)->ikm_size = (size); \
134 (kmsg)->ikm_prealloc = IP_NULL; \
135 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
138 #define ikm_check_init(kmsg, size) \
140 assert((kmsg)->ikm_size == (size)); \
141 assert((kmsg)->ikm_prev == IKM_BOGUS); \
142 assert((kmsg)->ikm_next == IKM_BOGUS); \
145 struct ipc_kmsg_queue
{
146 struct ipc_kmsg
*ikmq_base
;
149 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
151 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
155 * Exported interfaces
158 #define ipc_kmsg_queue_init(queue) \
160 (queue)->ikmq_base = IKM_NULL; \
163 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
166 extern void ipc_kmsg_enqueue(
167 ipc_kmsg_queue_t queue
,
170 /* Dequeue and return a kmsg */
171 extern ipc_kmsg_t
ipc_kmsg_dequeue(
172 ipc_kmsg_queue_t queue
);
174 /* Pull a kmsg out of a queue */
175 extern void ipc_kmsg_rmqueue(
176 ipc_kmsg_queue_t queue
,
179 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
181 /* Return the kmsg following the given kmsg */
182 extern ipc_kmsg_t
ipc_kmsg_queue_next(
183 ipc_kmsg_queue_t queue
,
186 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
188 register ipc_kmsg_t _next; \
190 assert((queue)->ikmq_base == (kmsg)); \
192 _next = (kmsg)->ikm_next; \
193 if (_next == (kmsg)) { \
194 assert((kmsg)->ikm_prev == (kmsg)); \
195 (queue)->ikmq_base = IKM_NULL; \
197 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
199 (queue)->ikmq_base = _next; \
200 _next->ikm_prev = _prev; \
201 _prev->ikm_next = _next; \
203 /* XXX Debug paranoia ASSIGNMENTS */ \
204 assert(kmsg->ikm_next = IKM_BOGUS); \
205 assert(kmsg->ikm_prev = IKM_BOGUS); \
208 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
210 register ipc_kmsg_t _first = (queue)->ikmq_base; \
212 if (_first == IKM_NULL) { \
213 (queue)->ikmq_base = (kmsg); \
214 (kmsg)->ikm_next = (kmsg); \
215 (kmsg)->ikm_prev = (kmsg); \
217 register ipc_kmsg_t _last = _first->ikm_prev; \
219 (kmsg)->ikm_next = _first; \
220 (kmsg)->ikm_prev = _last; \
221 _first->ikm_prev = (kmsg); \
222 _last->ikm_next = (kmsg); \
228 * ipc_kmsg_send_always(ipc_kmsg_t);
230 * Unfortunately, to avoid warnings/lint about unused variables
231 * when assertions are turned off, we need two versions of this.
235 #define ipc_kmsg_send_always(kmsg) \
237 mach_msg_return_t mr2; \
239 mr2 = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
240 MACH_MSG_TIMEOUT_NONE); \
241 assert(mr == MACH_MSG_SUCCESS); \
244 #else /* MACH_ASSERT */
246 #define ipc_kmsg_send_always(kmsg) \
248 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
249 MACH_MSG_TIMEOUT_NONE); \
252 #endif /* MACH_ASSERT */
255 /* Allocate a kernel message */
256 extern ipc_kmsg_t
ipc_kmsg_alloc(
257 mach_msg_size_t size
);
259 /* Free a kernel message buffer */
260 extern void ipc_kmsg_free(
263 /* Destroy kernel message */
264 extern void ipc_kmsg_destroy(
267 /* destroy kernel message and a reference on the dest */
268 extern void ipc_kmsg_destroy_dest(
272 /* Preallocate a kernel message buffer */
273 extern void ipc_kmsg_set_prealloc(
277 /* Clear a kernel message buffer */
278 extern void ipc_kmsg_clear_prealloc(
282 /* Allocate a kernel message buffer and copy a user message to the buffer */
283 extern mach_msg_return_t
ipc_kmsg_get(
284 mach_vm_address_t msg_addr
,
285 mach_msg_size_t size
,
288 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
289 extern mach_msg_return_t
ipc_kmsg_get_from_kernel(
290 mach_msg_header_t
*msg
,
291 mach_msg_size_t size
,
294 /* Send a message to a port */
295 extern mach_msg_return_t
ipc_kmsg_send(
297 mach_msg_option_t option
,
298 mach_msg_timeout_t timeout_val
);
300 /* Copy a kernel message buffer to a user message */
301 extern mach_msg_return_t
ipc_kmsg_put(
302 mach_vm_address_t msg_addr
,
304 mach_msg_size_t size
);
306 /* Copy a kernel message buffer to a kernel message */
307 extern void ipc_kmsg_put_to_kernel(
308 mach_msg_header_t
*msg
,
310 mach_msg_size_t size
);
312 /* Copyin port rights in the header of a message */
313 extern mach_msg_return_t
ipc_kmsg_copyin_header(
314 mach_msg_header_t
*msg
,
316 mach_port_name_t notify
);
318 /* Copyin port rights and out-of-line memory from a user message */
319 extern mach_msg_return_t
ipc_kmsg_copyin(
323 mach_port_name_t notify
);
325 /* Copyin port rights and out-of-line memory from a kernel message */
326 extern void ipc_kmsg_copyin_from_kernel(
329 /* Copyout port rights in the header of a message */
330 extern mach_msg_return_t
ipc_kmsg_copyout_header(
331 mach_msg_header_t
*msg
,
333 mach_port_name_t notify
);
335 /* Copyout a port right returning a name */
336 extern mach_msg_return_t
ipc_kmsg_copyout_object(
339 mach_msg_type_name_t msgt_name
,
340 mach_port_name_t
*namep
);
342 /* Copyout the header and body to a user message */
343 extern mach_msg_return_t
ipc_kmsg_copyout(
347 mach_port_name_t notify
,
348 mach_msg_body_t
*slist
);
350 /* Copyout port rights and out-of-line memory from the body of a message */
351 extern mach_msg_return_t
ipc_kmsg_copyout_body(
355 mach_msg_body_t
*slist
);
357 /* Copyout port rights and out-of-line memory to a user message,
358 not reversing the ports in the header */
359 extern mach_msg_return_t
ipc_kmsg_copyout_pseudo(
363 mach_msg_body_t
*slist
);
365 /* Compute size of message as copied out to the specified space/map */
366 extern mach_msg_size_t
ipc_kmsg_copyout_size(
370 /* Copyout the destination port in the message */
371 extern void ipc_kmsg_copyout_dest(
375 /* kernel's version of ipc_kmsg_copyout_dest */
376 extern void ipc_kmsg_copyout_to_kernel(
380 /* get a scatter list and check consistency */
381 extern mach_msg_body_t
*ipc_kmsg_get_scatter(
382 mach_vm_address_t msg_addr
,
383 mach_msg_size_t slist_size
,
386 /* free a scatter list */
387 extern void ipc_kmsg_free_scatter(
388 mach_msg_body_t
*slist
,
389 mach_msg_size_t slist_size
);
391 #endif /* _IPC_IPC_KMSG_H_ */