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>
83 #include <sys/kdebug.h>
85 typedef uint16_t ipc_kmsg_flags_t
;
87 #define IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND 0x1 /* Dest port contains an immovable send right */
89 #if (DEVELOPMENT || DEBUG)
90 /* Turn on to keep partial message signatures for better debug */
91 #define IKM_PARTIAL_SIG 0
95 * This structure is only the header for a kmsg buffer;
96 * the actual buffer is normally larger. The rest of the buffer
97 * holds the body of the message.
99 * In a kmsg, the port fields hold pointers to ports instead
100 * of port names. These pointers hold references.
102 * The ikm_header.msgh_remote_port field is the destination
105 * sync_qos and special_port_qos stores the qos for prealloced
106 * port, this fields could be deleted once we remove ip_prealloc.
110 mach_msg_size_t ikm_size
;
111 uint32_t ikm_ppriority
; /* pthread priority of this kmsg */
112 struct ipc_kmsg
*ikm_next
; /* next message on port/discard queue */
113 struct ipc_kmsg
*ikm_prev
; /* prev message on port/discard queue */
115 ipc_port_t
XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_prealloc") ikmu_prealloc
; /* port we were preallocated from */
116 void *XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_data") ikmu_data
;
118 mach_msg_header_t
*XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_header") ikm_header
;
119 ipc_port_t
XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_voucher") ikm_voucher
; /* voucher port carried */
120 struct ipc_importance_elem
*ikm_importance
; /* inherited from */
121 queue_chain_t ikm_inheritance
; /* inherited from link */
122 struct turnstile
*ikm_turnstile
; /* send turnstile for ikm_prealloc port */
124 struct mach_node
*ikm_node
; /* Originating node - needed for ack */
127 uintptr_t ikm_header_sig
; /* sig for just the header */
128 uintptr_t ikm_headtrail_sig
;/* sif for header and trailer */
130 uintptr_t ikm_signature
; /* sig for all kernel-processed data */
131 ipc_kmsg_flags_t ikm_flags
;
132 mach_msg_qos_t ikm_qos_override
; /* qos override on this kmsg */
133 mach_msg_filter_id ikm_filter_policy_id
; /* Sandbox-specific policy id used for message filtering */
135 #define ikm_prealloc ikm_u.ikmu_prealloc
136 #define ikm_data ikm_u.ikmu_data
138 #if defined(__i386__) || defined(__arm__)
139 #define IKM_SUPPORT_LEGACY 1
141 #define IKM_SUPPORT_LEGACY 0
144 #define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
146 #define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
147 #define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
152 #define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
155 * The size of the kernel message buffers that will be cached.
156 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
158 extern zone_t ipc_kmsg_zone
;
159 #define IKM_SAVED_KMSG_SIZE 256
160 #define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
162 #define ikm_prealloc_inuse_port(kmsg) \
163 ((kmsg)->ikm_prealloc)
165 #define ikm_prealloc_inuse(kmsg) \
166 ((kmsg)->ikm_prealloc != IP_NULL)
168 #define ikm_prealloc_set_inuse(kmsg, port) \
170 assert((port) != IP_NULL); \
171 (kmsg)->ikm_prealloc = (port); \
172 ip_reference(port); \
175 #define ikm_prealloc_clear_inuse(kmsg, port) \
177 (kmsg)->ikm_prealloc = IP_NULL; \
180 #define ikm_qos_init(kmsg) \
182 (kmsg)->ikm_ppriority = MACH_MSG_PRIORITY_UNSPECIFIED; \
183 (kmsg)->ikm_qos_override = THREAD_QOS_UNSPECIFIED; \
186 struct ipc_kmsg_queue
{
187 struct ipc_kmsg
*ikmq_base
;
190 typedef struct ipc_kmsg_queue
*ipc_kmsg_queue_t
;
192 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
196 * Exported interfaces
199 #define ipc_kmsg_queue_init(queue) \
201 (queue)->ikmq_base = IKM_NULL; \
204 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
207 extern void ipc_kmsg_enqueue(
208 ipc_kmsg_queue_t queue
,
211 extern boolean_t
ipc_kmsg_enqueue_qos(
212 ipc_kmsg_queue_t queue
,
215 extern boolean_t
ipc_kmsg_override_qos(
216 ipc_kmsg_queue_t queue
,
218 mach_msg_qos_t qos_ovr
);
220 /* Dequeue and return a kmsg */
221 extern ipc_kmsg_t
ipc_kmsg_dequeue(
222 ipc_kmsg_queue_t queue
);
224 /* Pull a kmsg out of a queue */
225 extern void ipc_kmsg_rmqueue(
226 ipc_kmsg_queue_t queue
,
229 /* Pull the (given) first kmsg out of a queue */
230 extern void ipc_kmsg_rmqueue_first(
231 ipc_kmsg_queue_t queue
,
234 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
236 /* Return the kmsg following the given kmsg */
237 extern ipc_kmsg_t
ipc_kmsg_queue_next(
238 ipc_kmsg_queue_t queue
,
241 /* Allocate a kernel message */
242 extern ipc_kmsg_t
ipc_kmsg_alloc(
243 mach_msg_size_t size
);
245 /* Free a kernel message buffer */
246 extern void ipc_kmsg_free(
249 /* Destroy kernel message */
250 extern void ipc_kmsg_destroy(
253 /* Enqueue kernel message for deferred destruction */
254 extern boolean_t
ipc_kmsg_delayed_destroy(
257 /* Process all the delayed message destroys */
258 extern void ipc_kmsg_reap_delayed(void);
260 /* Preallocate a kernel message buffer */
261 extern ipc_kmsg_t
ipc_kmsg_prealloc(
262 mach_msg_size_t size
);
264 /* bind a preallocated message buffer to a port */
265 extern void ipc_kmsg_set_prealloc(
269 /* Clear preallocated message buffer binding */
270 extern void ipc_kmsg_clear_prealloc(
274 /* Allocate a kernel message buffer and copy a user message to the buffer */
275 extern mach_msg_return_t
ipc_kmsg_get(
276 mach_vm_address_t msg_addr
,
277 mach_msg_size_t size
,
280 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
281 extern mach_msg_return_t
ipc_kmsg_get_from_kernel(
282 mach_msg_header_t
*msg
,
283 mach_msg_size_t size
,
286 /* Send a message to a port */
287 extern mach_msg_return_t
ipc_kmsg_send(
289 mach_msg_option_t option
,
290 mach_msg_timeout_t timeout_val
);
292 /* Copy a kernel message buffer to a user message */
293 extern mach_msg_return_t
ipc_kmsg_put(
295 mach_msg_option_t option
,
296 mach_vm_address_t rcv_addr
,
297 mach_msg_size_t rcv_size
,
298 mach_msg_size_t trailer_size
,
299 mach_msg_size_t
*size
);
301 /* Copy a kernel message buffer to a kernel message */
302 extern void ipc_kmsg_put_to_kernel(
303 mach_msg_header_t
*msg
,
305 mach_msg_size_t size
);
307 /* Copyin port rights in the header of a message */
308 extern mach_msg_return_t
ipc_kmsg_copyin_header(
311 mach_msg_priority_t priority
,
312 mach_msg_option_t
*optionp
);
314 /* Copyin port rights and out-of-line memory from a user message */
315 extern mach_msg_return_t
ipc_kmsg_copyin(
319 mach_msg_priority_t priority
,
320 mach_msg_option_t
*optionp
);
322 /* Copyin port rights and out-of-line memory from a kernel message */
323 extern mach_msg_return_t
ipc_kmsg_copyin_from_kernel(
326 #if IKM_SUPPORT_LEGACY
327 extern mach_msg_return_t
ipc_kmsg_copyin_from_kernel_legacy(
331 /* Copyout port rights in the header of a message */
332 extern mach_msg_return_t
ipc_kmsg_copyout_header(
335 mach_msg_option_t option
);
337 /* Copyout a port right returning a name */
338 extern mach_msg_return_t
ipc_kmsg_copyout_object(
341 mach_msg_type_name_t msgt_name
,
342 mach_port_context_t
*context
,
343 mach_msg_guard_flags_t
*guard_flags
,
344 mach_port_name_t
*namep
);
346 /* Copyout the header and body to a user message */
347 extern mach_msg_return_t
ipc_kmsg_copyout(
351 mach_msg_body_t
*slist
,
352 mach_msg_option_t option
);
354 /* Copyout port rights and out-of-line memory from the body of a message */
355 extern mach_msg_return_t
ipc_kmsg_copyout_body(
359 mach_msg_option_t option
,
360 mach_msg_body_t
*slist
);
362 /* Copyout port rights and out-of-line memory to a user message,
363 * not reversing the ports in the header */
364 extern mach_msg_return_t
ipc_kmsg_copyout_pseudo(
368 mach_msg_body_t
*slist
);
370 /* Compute size of message as copied out to the specified space/map */
371 extern mach_msg_size_t
ipc_kmsg_copyout_size(
375 /* Copyout the destination port in the message */
376 extern void ipc_kmsg_copyout_dest(
380 /* kernel's version of ipc_kmsg_copyout_dest */
381 extern void ipc_kmsg_copyout_to_kernel(
385 #if IKM_SUPPORT_LEGACY
386 extern void ipc_kmsg_copyout_to_kernel_legacy(
391 extern mach_msg_trailer_size_t
392 ipc_kmsg_trailer_size(mach_msg_option_t option
, thread_t thread
);
394 ipc_kmsg_add_trailer(ipc_kmsg_t kmsg
, ipc_space_t space
,
395 mach_msg_option_t option
, thread_t thread
,
396 mach_port_seqno_t seqno
, boolean_t minimal_trailer
,
397 mach_vm_offset_t context
);
399 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
400 extern void ipc_kmsg_trace_send(ipc_kmsg_t kmsg
,
401 mach_msg_option_t option
);
403 #define ipc_kmsg_trace_send(a, b) do { } while (0)
406 extern mach_msg_header_t
*
407 ipc_kmsg_msg_header(ipc_kmsg_t
);
409 #endif /* _IPC_IPC_KMSG_H_ */