2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
61 * Copyright (c) 2005 SPARTA, Inc.
66 * File: ipc/ipc_kmsg.h
70 * Definitions for kernel messages.
73 #ifndef _IPC_IPC_KMSG_H_
74 #define _IPC_IPC_KMSG_H_
76 #include <mach/vm_types.h>
77 #include <mach/message.h>
78 #include <kern/kern_types.h>
79 #include <kern/assert.h>
80 #include <kern/macro_help.h>
81 #include <ipc/ipc_types.h>
82 #include <ipc/ipc_object.h>
85 * This structure is only the header for a kmsg buffer;
86 * the actual buffer is normally larger. The rest of the buffer
87 * holds the body of the message.
89 * In a kmsg, the port fields hold pointers to ports instead
90 * of port names. These pointers hold references.
92 * The ikm_header.msgh_remote_port field is the destination
97 mach_msg_size_t ikm_size
;
98 struct ipc_kmsg
*ikm_next
; /* next message on port/discard queue */
99 struct ipc_kmsg
*ikm_prev
; /* prev message on port/discard queue */
100 mach_msg_header_t
*ikm_header
;
101 ipc_port_t ikm_prealloc
; /* port we were preallocated from */
102 ipc_port_t ikm_voucher
; /* voucher port carried */
103 struct ipc_importance_elem
*ikm_importance
;/* inherited from */
104 queue_chain_t ikm_inheritance
;/* inherited from link */
107 #if defined(__i386__) || defined(__arm__)
108 #define IKM_SUPPORT_LEGACY 1
110 #define IKM_SUPPORT_LEGACY 0
113 #define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
115 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
116 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
121 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
124 * The size of the kernel message buffers that will be cached.
125 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
127 extern zone_t ipc_kmsg_zone
;
128 #define IKM_SAVED_KMSG_SIZE 256
129 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
131 #define ikm_prealloc_inuse_port(kmsg) \
132 ((kmsg)->ikm_prealloc)
134 #define ikm_prealloc_inuse(kmsg) \
135 ((kmsg)->ikm_prealloc != IP_NULL)
137 #define ikm_prealloc_set_inuse(kmsg, port) \
139 assert((port) != IP_NULL); \
140 (kmsg)->ikm_prealloc = (port); \
141 ip_reference(port); \
144 #define ikm_prealloc_clear_inuse(kmsg, port) \
146 (kmsg)->ikm_prealloc = IP_NULL; \
149 #define ikm_init(kmsg, size) \
151 (kmsg)->ikm_size = (size); \
152 (kmsg)->ikm_prealloc = IP_NULL; \
153 (kmsg)->ikm_voucher = IP_NULL; \
154 (kmsg)->ikm_importance = IIE_NULL; \
155 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
158 #define ikm_check_init(kmsg, size) \
160 assert((kmsg)->ikm_size == (size)); \
161 assert((kmsg)->ikm_prev == IKM_BOGUS); \
162 assert((kmsg)->ikm_next == IKM_BOGUS); \
165 #define ikm_set_header(kmsg, mtsize) \
167 (kmsg)->ikm_header = (mach_msg_header_t *) \
168 ((vm_offset_t)((kmsg) + 1) + (kmsg)->ikm_size - (mtsize)); \
171 struct ipc_kmsg_queue
{
172 struct ipc_kmsg
*ikmq_base
;
175 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
177 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
181 * Exported interfaces
184 #define ipc_kmsg_queue_init(queue) \
186 (queue)->ikmq_base = IKM_NULL; \
189 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
192 extern void ipc_kmsg_enqueue(
193 ipc_kmsg_queue_t queue
,
196 /* Dequeue and return a kmsg */
197 extern ipc_kmsg_t
ipc_kmsg_dequeue(
198 ipc_kmsg_queue_t queue
);
200 /* Pull a kmsg out of a queue */
201 extern void ipc_kmsg_rmqueue(
202 ipc_kmsg_queue_t queue
,
205 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
207 /* Return the kmsg following the given kmsg */
208 extern ipc_kmsg_t
ipc_kmsg_queue_next(
209 ipc_kmsg_queue_t queue
,
212 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
214 register ipc_kmsg_t _next; \
216 assert((queue)->ikmq_base == (kmsg)); \
218 _next = (kmsg)->ikm_next; \
219 if (_next == (kmsg)) { \
220 assert((kmsg)->ikm_prev == (kmsg)); \
221 (queue)->ikmq_base = IKM_NULL; \
223 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
225 (queue)->ikmq_base = _next; \
226 _next->ikm_prev = _prev; \
227 _prev->ikm_next = _next; \
229 /* XXX Debug paranoia ASSIGNMENTS */ \
230 assert(kmsg->ikm_next = IKM_BOGUS); \
231 assert(kmsg->ikm_prev = IKM_BOGUS); \
234 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
236 register ipc_kmsg_t _first = (queue)->ikmq_base; \
238 if (_first == IKM_NULL) { \
239 (queue)->ikmq_base = (kmsg); \
240 (kmsg)->ikm_next = (kmsg); \
241 (kmsg)->ikm_prev = (kmsg); \
243 register ipc_kmsg_t _last = _first->ikm_prev; \
245 (kmsg)->ikm_next = _first; \
246 (kmsg)->ikm_prev = _last; \
247 _first->ikm_prev = (kmsg); \
248 _last->ikm_next = (kmsg); \
252 /* Allocate a kernel message */
253 extern ipc_kmsg_t
ipc_kmsg_alloc(
254 mach_msg_size_t size
);
256 /* Free a kernel message buffer */
257 extern void ipc_kmsg_free(
260 /* Destroy kernel message */
261 extern void ipc_kmsg_destroy(
264 /* Enqueue kernel message for deferred destruction */
265 extern boolean_t
ipc_kmsg_delayed_destroy(
268 /* Process all the delayed message destroys */
269 extern void ipc_kmsg_reap_delayed(void);
271 /* Preallocate a kernel message buffer */
272 extern ipc_kmsg_t
ipc_kmsg_prealloc(
273 mach_msg_size_t size
);
275 /* bind a preallocated message buffer to a port */
276 extern void ipc_kmsg_set_prealloc(
280 /* Clear preallocated message buffer binding */
281 extern void ipc_kmsg_clear_prealloc(
285 /* Allocate a kernel message buffer and copy a user message to the buffer */
286 extern mach_msg_return_t
ipc_kmsg_get(
287 mach_vm_address_t msg_addr
,
288 mach_msg_size_t size
,
291 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
292 extern mach_msg_return_t
ipc_kmsg_get_from_kernel(
293 mach_msg_header_t
*msg
,
294 mach_msg_size_t size
,
297 /* Send a message to a port */
298 extern mach_msg_return_t
ipc_kmsg_send(
300 mach_msg_option_t option
,
301 mach_msg_timeout_t timeout_val
);
303 /* Copy a kernel message buffer to a user message */
304 extern mach_msg_return_t
ipc_kmsg_put(
305 mach_vm_address_t msg_addr
,
307 mach_msg_size_t size
);
309 /* Copy a kernel message buffer to a kernel message */
310 extern void ipc_kmsg_put_to_kernel(
311 mach_msg_header_t
*msg
,
313 mach_msg_size_t size
);
315 /* Copyin port rights in the header of a message */
316 extern mach_msg_return_t
ipc_kmsg_copyin_header(
319 mach_msg_option_t
*optionp
);
321 /* Copyin port rights and out-of-line memory from a user message */
322 extern mach_msg_return_t
ipc_kmsg_copyin(
326 mach_msg_option_t
*optionp
);
328 /* Copyin port rights and out-of-line memory from a kernel message */
329 extern mach_msg_return_t
ipc_kmsg_copyin_from_kernel(
332 #if IKM_SUPPORT_LEGACY
333 extern mach_msg_return_t
ipc_kmsg_copyin_from_kernel_legacy(
337 /* Copyout port rights in the header of a message */
338 extern mach_msg_return_t
ipc_kmsg_copyout_header(
341 mach_msg_option_t option
);
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_msg_body_t
*slist
,
356 mach_msg_option_t option
);
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 #if IKM_SUPPORT_LEGACY
389 extern void ipc_kmsg_copyout_to_kernel_legacy(
394 /* get a scatter list and check consistency */
395 extern mach_msg_body_t
*ipc_kmsg_get_scatter(
396 mach_vm_address_t msg_addr
,
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
);
406 extern mach_msg_trailer_size_t
407 ipc_kmsg_add_trailer(ipc_kmsg_t kmsg
, ipc_space_t space
,
408 mach_msg_option_t option
, thread_t thread
,
409 mach_port_seqno_t seqno
, boolean_t minimal_trailer
,
410 mach_vm_offset_t context
);
412 #endif /* _IPC_IPC_KMSG_H_ */