2 * Copyright (c) 2000 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_
65 #include <mach/vm_types.h>
66 #include <mach/message.h>
67 #include <kern/assert.h>
68 #include <kern/cpu_number.h>
69 #include <kern/macro_help.h>
70 #include <kern/kalloc.h>
71 #include <ipc/ipc_object.h>
74 * This structure is only the header for a kmsg buffer;
75 * the actual buffer is normally larger. The rest of the buffer
76 * holds the body of the message.
78 * In a kmsg, the port fields hold pointers to ports instead
79 * of port names. These pointers hold references.
81 * The ikm_header.msgh_remote_port field is the destination
86 typedef struct ipc_kmsg
{
87 struct ipc_kmsg
*ikm_next
;
88 struct ipc_kmsg
*ikm_prev
;
89 ipc_port_t ikm_prealloc
; /* port we were preallocated from */
90 mach_msg_size_t ikm_size
;
91 mach_msg_header_t ikm_header
;
94 #define IKM_NULL ((ipc_kmsg_t) 0)
96 #define IKM_OVERHEAD \
97 (sizeof(struct ipc_kmsg) - sizeof(mach_msg_header_t))
99 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
100 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
105 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
108 * The size of the kernel message buffers that will be cached.
109 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
112 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(256)
114 #define ikm_prealloc_inuse_port(kmsg) \
115 ((kmsg)->ikm_prealloc)
117 #define ikm_prealloc_inuse(kmsg) \
118 ((kmsg)->ikm_prealloc != IP_NULL)
120 #define ikm_prealloc_set_inuse(kmsg, port) \
122 assert((port) != IP_NULL); \
123 (kmsg)->ikm_prealloc = (port); \
124 ip_reference(port); \
127 #define ikm_prealloc_clear_inuse(kmsg, port) \
129 (kmsg)->ikm_prealloc = IP_NULL; \
134 #define ikm_init(kmsg, size) \
136 (kmsg)->ikm_size = (size); \
137 (kmsg)->ikm_prealloc = IP_NULL; \
138 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
141 #define ikm_check_init(kmsg, size) \
143 assert((kmsg)->ikm_size == (size)); \
144 assert((kmsg)->ikm_prev == IKM_BOGUS); \
145 assert((kmsg)->ikm_next == IKM_BOGUS); \
148 struct ipc_kmsg_queue
{
149 struct ipc_kmsg
*ikmq_base
;
152 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
154 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
158 * Exported interfaces
161 #define ipc_kmsg_queue_init(queue) \
163 (queue)->ikmq_base = IKM_NULL; \
166 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
169 extern void ipc_kmsg_enqueue(
170 ipc_kmsg_queue_t queue
,
173 /* Dequeue and return a kmsg */
174 extern ipc_kmsg_t
ipc_kmsg_dequeue(
175 ipc_kmsg_queue_t queue
);
177 /* Pull a kmsg out of a queue */
178 extern void ipc_kmsg_rmqueue(
179 ipc_kmsg_queue_t queue
,
182 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
184 /* Return the kmsg following the given kmsg */
185 extern ipc_kmsg_t
ipc_kmsg_queue_next(
186 ipc_kmsg_queue_t queue
,
189 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
191 register ipc_kmsg_t _next; \
193 assert((queue)->ikmq_base == (kmsg)); \
195 _next = (kmsg)->ikm_next; \
196 if (_next == (kmsg)) { \
197 assert((kmsg)->ikm_prev == (kmsg)); \
198 (queue)->ikmq_base = IKM_NULL; \
200 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
202 (queue)->ikmq_base = _next; \
203 _next->ikm_prev = _prev; \
204 _prev->ikm_next = _next; \
206 /* XXX Debug paranoia ASSIGNMENTS */ \
207 assert(kmsg->ikm_next = IKM_BOGUS); \
208 assert(kmsg->ikm_prev = IKM_BOGUS); \
211 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
213 register ipc_kmsg_t _first = (queue)->ikmq_base; \
215 if (_first == IKM_NULL) { \
216 (queue)->ikmq_base = (kmsg); \
217 (kmsg)->ikm_next = (kmsg); \
218 (kmsg)->ikm_prev = (kmsg); \
220 register ipc_kmsg_t _last = _first->ikm_prev; \
222 (kmsg)->ikm_next = _first; \
223 (kmsg)->ikm_prev = _last; \
224 _first->ikm_prev = (kmsg); \
225 _last->ikm_next = (kmsg); \
229 /* scatter list macros */
231 #define SKIP_PORT_DESCRIPTORS(s, e) \
233 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
234 while ((s) < (e)) { \
235 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
240 (s) = MACH_MSG_DESCRIPTOR_NULL; \
244 #define INCREMENT_SCATTER(s) \
246 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
253 * ipc_kmsg_send_always(ipc_kmsg_t);
255 * Unfortunately, to avoid warnings/lint about unused variables
256 * when assertions are turned off, we need two versions of this.
260 #define ipc_kmsg_send_always(kmsg) \
262 mach_msg_return_t mr; \
264 mr = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
265 MACH_MSG_TIMEOUT_NONE); \
266 assert(mr == MACH_MSG_SUCCESS); \
269 #else /* MACH_ASSERT */
271 #define ipc_kmsg_send_always(kmsg) \
273 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
274 MACH_MSG_TIMEOUT_NONE); \
277 #endif /* MACH_ASSERT */
279 /* Allocate a kernel message */
280 extern ipc_kmsg_t
ipc_kmsg_alloc(
281 mach_msg_size_t size
);
283 /* Free a kernel message buffer */
284 extern void ipc_kmsg_free(
287 /* Destroy kernel message */
288 extern void ipc_kmsg_destroy(
291 /* Preallocate a kernel message buffer */
292 extern void ipc_kmsg_set_prealloc(
296 /* Clear a kernel message buffer */
297 extern void ipc_kmsg_clear_prealloc(
301 /* Allocate a kernel message buffer and copy a user message to the buffer */
302 extern mach_msg_return_t
ipc_kmsg_get(
303 mach_msg_header_t
*msg
,
304 mach_msg_size_t size
,
307 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
308 extern mach_msg_return_t
ipc_kmsg_get_from_kernel(
309 mach_msg_header_t
*msg
,
310 mach_msg_size_t size
,
313 /* Send a message to a port */
314 extern mach_msg_return_t
ipc_kmsg_send(
316 mach_msg_option_t option
,
317 mach_msg_timeout_t timeout
);
319 /* Copy a kernel message buffer to a user message */
320 extern mach_msg_return_t
ipc_kmsg_put(
321 mach_msg_header_t
*msg
,
323 mach_msg_size_t size
);
325 /* Copy a kernel message buffer to a kernel message */
326 extern void ipc_kmsg_put_to_kernel(
327 mach_msg_header_t
*msg
,
329 mach_msg_size_t size
);
331 /* Copyin port rights in the header of a message */
332 extern mach_msg_return_t
ipc_kmsg_copyin_header(
333 mach_msg_header_t
*msg
,
335 mach_port_name_t notify
);
337 /* Copyin port rights and out-of-line memory from a user message */
338 extern mach_msg_return_t
ipc_kmsg_copyin(
342 mach_port_name_t notify
);
344 /* Copyin port rights and out-of-line memory from a kernel message */
345 extern void ipc_kmsg_copyin_from_kernel(
348 /* Copyout port rights in the header of a message */
349 extern mach_msg_return_t
ipc_kmsg_copyout_header(
350 mach_msg_header_t
*msg
,
352 mach_port_name_t notify
);
354 /* Copyout a port right returning a name */
355 extern mach_msg_return_t
ipc_kmsg_copyout_object(
358 mach_msg_type_name_t msgt_name
,
359 mach_port_name_t
*namep
);
361 /* Copyout the header and body to a user message */
362 extern mach_msg_return_t
ipc_kmsg_copyout(
366 mach_port_name_t notify
,
367 mach_msg_body_t
*slist
);
369 /* Copyout port rights and out-of-line memory from the body of a message */
370 extern mach_msg_return_t
ipc_kmsg_copyout_body(
374 mach_msg_body_t
*slist
);
376 /* Copyout port rights and out-of-line memory to a user message,
377 not reversing the ports in the header */
378 extern mach_msg_return_t
ipc_kmsg_copyout_pseudo(
382 mach_msg_body_t
*slist
);
384 /* Copyout the destination port in the message */
385 extern void ipc_kmsg_copyout_dest(
389 /* kernel's version of ipc_kmsg_copyout_dest */
390 extern void ipc_kmsg_copyout_to_kernel(
394 /* copyin a scatter list and check consistency */
395 extern mach_msg_body_t
*ipc_kmsg_copyin_scatter(
396 mach_msg_header_t
*msg
,
397 mach_msg_size_t slist_size
,
400 /* free a scatter list */
401 extern void ipc_kmsg_free_scatter(
402 mach_msg_body_t
*slist
,
403 mach_msg_size_t slist_size
);
405 #include <mach_kdb.h>
408 /* Do a formatted dump of a kernel message */
409 extern void ipc_kmsg_print(
412 /* Do a formatted dump of a user message */
413 extern void ipc_msg_print(
414 mach_msg_header_t
*msgh
);
416 #endif /* MACH_KDB */
418 #endif /* _IPC_IPC_KMSG_H_ */