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; \
126 #define ikm_prealloc_clear_inuse(kmsg, port) \
128 (kmsg)->ikm_prealloc = IP_NULL; \
132 #define ikm_init(kmsg, size) \
134 (kmsg)->ikm_size = (size); \
135 (kmsg)->ikm_prealloc = IP_NULL; \
136 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
139 #define ikm_check_init(kmsg, size) \
141 assert((kmsg)->ikm_size == (size)); \
142 assert((kmsg)->ikm_prev == IKM_BOGUS); \
143 assert((kmsg)->ikm_next == IKM_BOGUS); \
146 struct ipc_kmsg_queue
{
147 struct ipc_kmsg
*ikmq_base
;
150 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
152 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
156 * Exported interfaces
159 #define ipc_kmsg_queue_init(queue) \
161 (queue)->ikmq_base = IKM_NULL; \
164 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
167 extern void ipc_kmsg_enqueue(
168 ipc_kmsg_queue_t queue
,
171 /* Dequeue and return a kmsg */
172 extern ipc_kmsg_t
ipc_kmsg_dequeue(
173 ipc_kmsg_queue_t queue
);
175 /* Pull a kmsg out of a queue */
176 extern void ipc_kmsg_rmqueue(
177 ipc_kmsg_queue_t queue
,
180 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
182 /* Return the kmsg following the given kmsg */
183 extern ipc_kmsg_t
ipc_kmsg_queue_next(
184 ipc_kmsg_queue_t queue
,
187 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
189 register ipc_kmsg_t _next; \
191 assert((queue)->ikmq_base == (kmsg)); \
193 _next = (kmsg)->ikm_next; \
194 if (_next == (kmsg)) { \
195 assert((kmsg)->ikm_prev == (kmsg)); \
196 (queue)->ikmq_base = IKM_NULL; \
198 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
200 (queue)->ikmq_base = _next; \
201 _next->ikm_prev = _prev; \
202 _prev->ikm_next = _next; \
204 /* XXX Debug paranoia ASSIGNMENTS */ \
205 assert(kmsg->ikm_next = IKM_BOGUS); \
206 assert(kmsg->ikm_prev = IKM_BOGUS); \
209 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
211 register ipc_kmsg_t _first = (queue)->ikmq_base; \
213 if (_first == IKM_NULL) { \
214 (queue)->ikmq_base = (kmsg); \
215 (kmsg)->ikm_next = (kmsg); \
216 (kmsg)->ikm_prev = (kmsg); \
218 register ipc_kmsg_t _last = _first->ikm_prev; \
220 (kmsg)->ikm_next = _first; \
221 (kmsg)->ikm_prev = _last; \
222 _first->ikm_prev = (kmsg); \
223 _last->ikm_next = (kmsg); \
227 /* scatter list macros */
229 #define SKIP_PORT_DESCRIPTORS(s, e) \
231 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
232 while ((s) < (e)) { \
233 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
238 (s) = MACH_MSG_DESCRIPTOR_NULL; \
242 #define INCREMENT_SCATTER(s) \
244 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
251 * ipc_kmsg_send_always(ipc_kmsg_t);
253 * Unfortunately, to avoid warnings/lint about unused variables
254 * when assertions are turned off, we need two versions of this.
258 #define ipc_kmsg_send_always(kmsg) \
260 mach_msg_return_t mr; \
262 mr = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
263 MACH_MSG_TIMEOUT_NONE); \
264 assert(mr == MACH_MSG_SUCCESS); \
267 #else /* MACH_ASSERT */
269 #define ipc_kmsg_send_always(kmsg) \
271 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
272 MACH_MSG_TIMEOUT_NONE); \
275 #endif /* MACH_ASSERT */
277 /* Allocate a kernel message */
278 extern ipc_kmsg_t
ipc_kmsg_alloc(
279 mach_msg_size_t size
);
281 /* Free a kernel message buffer */
282 extern void ipc_kmsg_free(
285 /* Destroy kernel message */
286 extern void ipc_kmsg_destroy(
289 /* Preallocate a kernel message buffer */
290 extern void ipc_kmsg_set_prealloc(
294 /* Clear a kernel message buffer */
295 extern void ipc_kmsg_clear_prealloc(
299 /* Allocate a kernel message buffer and copy a user message to the buffer */
300 extern mach_msg_return_t
ipc_kmsg_get(
301 mach_msg_header_t
*msg
,
302 mach_msg_size_t size
,
305 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
306 extern mach_msg_return_t
ipc_kmsg_get_from_kernel(
307 mach_msg_header_t
*msg
,
308 mach_msg_size_t size
,
311 /* Send a message to a port */
312 extern mach_msg_return_t
ipc_kmsg_send(
314 mach_msg_option_t option
,
315 mach_msg_timeout_t timeout
);
317 /* Copy a kernel message buffer to a user message */
318 extern mach_msg_return_t
ipc_kmsg_put(
319 mach_msg_header_t
*msg
,
321 mach_msg_size_t size
);
323 /* Copy a kernel message buffer to a kernel message */
324 extern void ipc_kmsg_put_to_kernel(
325 mach_msg_header_t
*msg
,
327 mach_msg_size_t size
);
329 /* Copyin port rights in the header of a message */
330 extern mach_msg_return_t
ipc_kmsg_copyin_header(
331 mach_msg_header_t
*msg
,
333 mach_port_name_t notify
);
335 /* Copyin port rights and out-of-line memory from a user message */
336 extern mach_msg_return_t
ipc_kmsg_copyin(
340 mach_port_name_t notify
);
342 /* Copyin port rights and out-of-line memory from a kernel message */
343 extern void ipc_kmsg_copyin_from_kernel(
346 /* Copyout port rights in the header of a message */
347 extern mach_msg_return_t
ipc_kmsg_copyout_header(
348 mach_msg_header_t
*msg
,
350 mach_port_name_t notify
);
352 /* Copyout a port right returning a name */
353 extern mach_msg_return_t
ipc_kmsg_copyout_object(
356 mach_msg_type_name_t msgt_name
,
357 mach_port_name_t
*namep
);
359 /* Copyout the header and body to a user message */
360 extern mach_msg_return_t
ipc_kmsg_copyout(
364 mach_port_name_t notify
,
365 mach_msg_body_t
*slist
);
367 /* Copyout port rights and out-of-line memory from the body of a message */
368 extern mach_msg_return_t
ipc_kmsg_copyout_body(
372 mach_msg_body_t
*slist
);
374 /* Copyout port rights and out-of-line memory to a user message,
375 not reversing the ports in the header */
376 extern mach_msg_return_t
ipc_kmsg_copyout_pseudo(
380 mach_msg_body_t
*slist
);
382 /* Copyout the destination port in the message */
383 extern void ipc_kmsg_copyout_dest(
387 /* kernel's version of ipc_kmsg_copyout_dest */
388 extern void ipc_kmsg_copyout_to_kernel(
392 /* copyin a scatter list and check consistency */
393 extern mach_msg_body_t
*ipc_kmsg_copyin_scatter(
394 mach_msg_header_t
*msg
,
395 mach_msg_size_t slist_size
,
398 /* free a scatter list */
399 extern void ipc_kmsg_free_scatter(
400 mach_msg_body_t
*slist
,
401 mach_msg_size_t slist_size
);
403 #include <mach_kdb.h>
406 /* Do a formatted dump of a kernel message */
407 extern void ipc_kmsg_print(
410 /* Do a formatted dump of a user message */
411 extern void ipc_msg_print(
412 mach_msg_header_t
*msgh
);
414 #endif /* MACH_KDB */
416 #endif /* _IPC_IPC_KMSG_H_ */