2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
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.
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.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: ipc/ipc_kmsg.h
65 * Definitions for kernel messages.
68 #ifndef _IPC_IPC_KMSG_H_
69 #define _IPC_IPC_KMSG_H_
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>
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.
84 * In a kmsg, the port fields hold pointers to ports instead
85 * of port names. These pointers hold references.
87 * The ikm_header.msgh_remote_port field is the destination
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
;
101 #define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
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))
109 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
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.
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)
119 #define ikm_prealloc_inuse_port(kmsg) \
120 ((kmsg)->ikm_prealloc)
122 #define ikm_prealloc_inuse(kmsg) \
123 ((kmsg)->ikm_prealloc != IP_NULL)
125 #define ikm_prealloc_set_inuse(kmsg, port) \
127 assert((port) != IP_NULL); \
128 (kmsg)->ikm_prealloc = (port); \
129 ip_reference(port); \
132 #define ikm_prealloc_clear_inuse(kmsg, port) \
134 (kmsg)->ikm_prealloc = IP_NULL; \
139 #define ikm_init(kmsg, size) \
141 (kmsg)->ikm_size = (size); \
142 (kmsg)->ikm_prealloc = IP_NULL; \
143 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
146 #define ikm_check_init(kmsg, size) \
148 assert((kmsg)->ikm_size == (size)); \
149 assert((kmsg)->ikm_prev == IKM_BOGUS); \
150 assert((kmsg)->ikm_next == IKM_BOGUS); \
153 struct ipc_kmsg_queue
{
154 struct ipc_kmsg
*ikmq_base
;
157 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
159 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
163 * Exported interfaces
166 #define ipc_kmsg_queue_init(queue) \
168 (queue)->ikmq_base = IKM_NULL; \
171 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
174 extern void ipc_kmsg_enqueue(
175 ipc_kmsg_queue_t queue
,
178 /* Dequeue and return a kmsg */
179 extern ipc_kmsg_t
ipc_kmsg_dequeue(
180 ipc_kmsg_queue_t queue
);
182 /* Pull a kmsg out of a queue */
183 extern void ipc_kmsg_rmqueue(
184 ipc_kmsg_queue_t queue
,
187 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
189 /* Return the kmsg following the given kmsg */
190 extern ipc_kmsg_t
ipc_kmsg_queue_next(
191 ipc_kmsg_queue_t queue
,
194 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
196 register ipc_kmsg_t _next; \
198 assert((queue)->ikmq_base == (kmsg)); \
200 _next = (kmsg)->ikm_next; \
201 if (_next == (kmsg)) { \
202 assert((kmsg)->ikm_prev == (kmsg)); \
203 (queue)->ikmq_base = IKM_NULL; \
205 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
207 (queue)->ikmq_base = _next; \
208 _next->ikm_prev = _prev; \
209 _prev->ikm_next = _next; \
211 /* XXX Debug paranoia ASSIGNMENTS */ \
212 assert(kmsg->ikm_next = IKM_BOGUS); \
213 assert(kmsg->ikm_prev = IKM_BOGUS); \
216 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
218 register ipc_kmsg_t _first = (queue)->ikmq_base; \
220 if (_first == IKM_NULL) { \
221 (queue)->ikmq_base = (kmsg); \
222 (kmsg)->ikm_next = (kmsg); \
223 (kmsg)->ikm_prev = (kmsg); \
225 register ipc_kmsg_t _last = _first->ikm_prev; \
227 (kmsg)->ikm_next = _first; \
228 (kmsg)->ikm_prev = _last; \
229 _first->ikm_prev = (kmsg); \
230 _last->ikm_next = (kmsg); \
236 * ipc_kmsg_send_always(ipc_kmsg_t);
238 * Unfortunately, to avoid warnings/lint about unused variables
239 * when assertions are turned off, we need two versions of this.
243 #define ipc_kmsg_send_always(kmsg) \
245 mach_msg_return_t mr2; \
247 mr2 = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
248 MACH_MSG_TIMEOUT_NONE); \
249 assert(mr == MACH_MSG_SUCCESS); \
252 #else /* MACH_ASSERT */
254 #define ipc_kmsg_send_always(kmsg) \
256 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
257 MACH_MSG_TIMEOUT_NONE); \
260 #endif /* MACH_ASSERT */
263 /* Allocate a kernel message */
264 extern ipc_kmsg_t
ipc_kmsg_alloc(
265 mach_msg_size_t size
);
267 /* Free a kernel message buffer */
268 extern void ipc_kmsg_free(
271 /* Destroy kernel message */
272 extern void ipc_kmsg_destroy(
275 /* destroy kernel message and a reference on the dest */
276 extern void ipc_kmsg_destroy_dest(
280 /* Preallocate a kernel message buffer */
281 extern void ipc_kmsg_set_prealloc(
285 /* Clear a kernel message buffer */
286 extern void ipc_kmsg_clear_prealloc(
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
,
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
,
302 /* Send a message to a port */
303 extern mach_msg_return_t
ipc_kmsg_send(
305 mach_msg_option_t option
,
306 mach_msg_timeout_t timeout_val
);
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
,
312 mach_msg_size_t size
);
314 /* Copy a kernel message buffer to a kernel message */
315 extern void ipc_kmsg_put_to_kernel(
316 mach_msg_header_t
*msg
,
318 mach_msg_size_t size
);
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
,
324 mach_port_name_t notify
);
326 /* Copyin port rights and out-of-line memory from a user message */
327 extern mach_msg_return_t
ipc_kmsg_copyin(
331 mach_port_name_t notify
);
333 /* Copyin port rights and out-of-line memory from a kernel message */
334 extern void ipc_kmsg_copyin_from_kernel(
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
,
341 mach_port_name_t notify
);
343 /* Copyout a port right returning a name */
344 extern mach_msg_return_t
ipc_kmsg_copyout_object(
347 mach_msg_type_name_t msgt_name
,
348 mach_port_name_t
*namep
);
350 /* Copyout the header and body to a user message */
351 extern mach_msg_return_t
ipc_kmsg_copyout(
355 mach_port_name_t notify
,
356 mach_msg_body_t
*slist
);
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(
363 mach_msg_body_t
*slist
);
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(
371 mach_msg_body_t
*slist
);
373 /* Compute size of message as copied out to the specified space/map */
374 extern mach_msg_size_t
ipc_kmsg_copyout_size(
378 /* Copyout the destination port in the message */
379 extern void ipc_kmsg_copyout_dest(
383 /* kernel's version of ipc_kmsg_copyout_dest */
384 extern void ipc_kmsg_copyout_to_kernel(
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
,
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
);
399 #endif /* _IPC_IPC_KMSG_H_ */