]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_kmsg.h
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_kmsg.h
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
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.
0a7de745 41 *
1c79356b
A
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.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
2d21ac55
A
56/*
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,
60 * Version 2.0.
61 * Copyright (c) 2005 SPARTA, Inc.
62 */
1c79356b
A
63/*
64 */
65/*
66 * File: ipc/ipc_kmsg.h
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Definitions for kernel messages.
71 */
72
0a7de745 73#ifndef _IPC_IPC_KMSG_H_
1c79356b
A
74#define _IPC_IPC_KMSG_H_
75
1c79356b
A
76#include <mach/vm_types.h>
77#include <mach/message.h>
91447636 78#include <kern/kern_types.h>
1c79356b 79#include <kern/assert.h>
1c79356b 80#include <kern/macro_help.h>
91447636 81#include <ipc/ipc_types.h>
1c79356b 82#include <ipc/ipc_object.h>
39037602 83#include <sys/kdebug.h>
1c79356b 84
f427ee49 85typedef uint16_t ipc_kmsg_flags_t;
cb323159
A
86
87#define IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND 0x1 /* Dest port contains an immovable send right */
88
f427ee49
A
89#if (DEVELOPMENT || DEBUG)
90#define IKM_PARTIAL_SIG 1 /* Keep partial message signatures for better debug */
91#endif
92
1c79356b
A
93/*
94 * This structure is only the header for a kmsg buffer;
95 * the actual buffer is normally larger. The rest of the buffer
96 * holds the body of the message.
97 *
98 * In a kmsg, the port fields hold pointers to ports instead
99 * of port names. These pointers hold references.
100 *
101 * The ikm_header.msgh_remote_port field is the destination
102 * of the message.
5ba3f43e
A
103 *
104 * sync_qos and special_port_qos stores the qos for prealloced
105 * port, this fields could be deleted once we remove ip_prealloc.
1c79356b
A
106 */
107
91447636 108struct ipc_kmsg {
39037602 109 mach_msg_size_t ikm_size;
f427ee49 110 uint32_t ikm_ppriority; /* pthread priority of this kmsg */
39037602
A
111 struct ipc_kmsg *ikm_next; /* next message on port/discard queue */
112 struct ipc_kmsg *ikm_prev; /* prev message on port/discard queue */
f427ee49
A
113 union {
114 ipc_port_t XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_prealloc") ikmu_prealloc; /* port we were preallocated from */
115 void *XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_data") ikmu_data;
116 } ikm_u;
117 mach_msg_header_t *XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_header") ikm_header;
118 ipc_port_t XNU_PTRAUTH_SIGNED_PTR("kmsg.ikm_voucher") ikm_voucher; /* voucher port carried */
39037602
A
119 struct ipc_importance_elem *ikm_importance; /* inherited from */
120 queue_chain_t ikm_inheritance; /* inherited from link */
d9a64523 121 struct turnstile *ikm_turnstile; /* send turnstile for ikm_prealloc port */
39037602
A
122#if MACH_FLIPC
123 struct mach_node *ikm_node; /* Originating node - needed for ack */
124#endif
f427ee49
A
125#if IKM_PARTIAL_SIG
126 uintptr_t ikm_header_sig; /* sig for just the header */
127 uintptr_t ikm_headtrail_sig;/* sif for header and trailer */
128#endif
129 uintptr_t ikm_signature; /* sig for all kernel-processed data */
130 ipc_kmsg_flags_t ikm_flags;
131 mach_msg_qos_t ikm_qos_override; /* qos override on this kmsg */
132 mach_msg_filter_id ikm_filter_policy_id; /* Sandbox-specific policy id used for message filtering */
91447636 133};
f427ee49
A
134#define ikm_prealloc ikm_u.ikmu_prealloc
135#define ikm_data ikm_u.ikmu_data
1c79356b 136
b0d623f7 137#if defined(__i386__) || defined(__arm__)
0a7de745 138#define IKM_SUPPORT_LEGACY 1
b0d623f7 139#else
0a7de745 140#define IKM_SUPPORT_LEGACY 0
b0d623f7 141#endif
1c79356b 142
0a7de745 143#define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
1c79356b 144
0a7de745
A
145#define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
146#define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
1c79356b
A
147
148/*
149 * XXX For debugging.
150 */
0a7de745 151#define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
1c79356b
A
152
153/*
154 * The size of the kernel message buffers that will be cached.
155 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
156 */
91447636 157extern zone_t ipc_kmsg_zone;
0a7de745
A
158#define IKM_SAVED_KMSG_SIZE 256
159#define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
1c79356b 160
0a7de745 161#define ikm_prealloc_inuse_port(kmsg) \
1c79356b
A
162 ((kmsg)->ikm_prealloc)
163
0a7de745 164#define ikm_prealloc_inuse(kmsg) \
1c79356b
A
165 ((kmsg)->ikm_prealloc != IP_NULL)
166
0a7de745
A
167#define ikm_prealloc_set_inuse(kmsg, port) \
168MACRO_BEGIN \
169 assert((port) != IP_NULL); \
170 (kmsg)->ikm_prealloc = (port); \
171 ip_reference(port); \
1c79356b
A
172MACRO_END
173
0a7de745
A
174#define ikm_prealloc_clear_inuse(kmsg, port) \
175MACRO_BEGIN \
176 (kmsg)->ikm_prealloc = IP_NULL; \
1c79356b
A
177MACRO_END
178
39037602
A
179#if MACH_FLIPC
180#define ikm_flipc_init(kmsg) (kmsg)->ikm_node = MACH_NODE_NULL
181#else
182#define ikm_flipc_init(kmsg)
183#endif
184
f427ee49 185#if IKM_PARTIAL_SIG
0a7de745 186#define ikm_init(kmsg, size) \
39037602
A
187MACRO_BEGIN \
188 (kmsg)->ikm_size = (size); \
cb323159 189 (kmsg)->ikm_flags = 0; \
39037602 190 (kmsg)->ikm_prealloc = IP_NULL; \
f427ee49 191 (kmsg)->ikm_data = NULL; \
39037602
A
192 (kmsg)->ikm_voucher = IP_NULL; \
193 (kmsg)->ikm_importance = IIE_NULL; \
f427ee49
A
194 (kmsg)->ikm_filter_policy_id = 0; \
195 (kmsg)->ikm_header_sig = 0; \
196 (kmsg)->ikm_headtrail_sig = 0; \
197 (kmsg)->ikm_signature = 0; \
5ba3f43e 198 ikm_qos_init(kmsg); \
39037602
A
199 ikm_flipc_init(kmsg); \
200 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
1c79356b 201MACRO_END
f427ee49
A
202#else
203#define ikm_init(kmsg, size) \
204MACRO_BEGIN \
205 (kmsg)->ikm_size = (size); \
206 (kmsg)->ikm_flags = 0; \
207 (kmsg)->ikm_prealloc = IP_NULL; \
208 (kmsg)->ikm_data = NULL; \
209 (kmsg)->ikm_voucher = IP_NULL; \
210 (kmsg)->ikm_importance = IIE_NULL; \
211 (kmsg)->ikm_filter_policy_id = 0; \
212 (kmsg)->ikm_signature = 0; \
213 ikm_qos_init(kmsg); \
214 ikm_flipc_init(kmsg); \
215 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
1c79356b 216MACRO_END
f427ee49 217#endif
1c79356b 218
f427ee49 219#define ikm_qos_init(kmsg) \
0a7de745 220MACRO_BEGIN \
f427ee49
A
221 (kmsg)->ikm_ppriority = MACH_MSG_PRIORITY_UNSPECIFIED; \
222 (kmsg)->ikm_qos_override = THREAD_QOS_UNSPECIFIED; \
b7266188
A
223MACRO_END
224
1c79356b
A
225struct ipc_kmsg_queue {
226 struct ipc_kmsg *ikmq_base;
227};
228
229typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
230
0a7de745 231#define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
1c79356b
A
232
233
234/*
235 * Exported interfaces
236 */
237
0a7de745
A
238#define ipc_kmsg_queue_init(queue) \
239MACRO_BEGIN \
240 (queue)->ikmq_base = IKM_NULL; \
1c79356b
A
241MACRO_END
242
0a7de745 243#define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
1c79356b
A
244
245/* Enqueue a kmsg */
246extern void ipc_kmsg_enqueue(
0a7de745
A
247 ipc_kmsg_queue_t queue,
248 ipc_kmsg_t kmsg);
1c79356b 249
39037602 250extern boolean_t ipc_kmsg_enqueue_qos(
0a7de745
A
251 ipc_kmsg_queue_t queue,
252 ipc_kmsg_t kmsg);
39037602
A
253
254extern boolean_t ipc_kmsg_override_qos(
255 ipc_kmsg_queue_t queue,
256 ipc_kmsg_t kmsg,
f427ee49 257 mach_msg_qos_t qos_ovr);
39037602 258
1c79356b
A
259/* Dequeue and return a kmsg */
260extern ipc_kmsg_t ipc_kmsg_dequeue(
261 ipc_kmsg_queue_t queue);
262
263/* Pull a kmsg out of a queue */
264extern void ipc_kmsg_rmqueue(
0a7de745
A
265 ipc_kmsg_queue_t queue,
266 ipc_kmsg_t kmsg);
1c79356b 267
39037602
A
268/* Pull the (given) first kmsg out of a queue */
269extern void ipc_kmsg_rmqueue_first(
0a7de745 270 ipc_kmsg_queue_t queue,
39037602
A
271 ipc_kmsg_t kmsg);
272
0a7de745 273#define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
1c79356b
A
274
275/* Return the kmsg following the given kmsg */
276extern ipc_kmsg_t ipc_kmsg_queue_next(
0a7de745
A
277 ipc_kmsg_queue_t queue,
278 ipc_kmsg_t kmsg);
1c79356b 279
1c79356b
A
280/* Allocate a kernel message */
281extern ipc_kmsg_t ipc_kmsg_alloc(
0a7de745 282 mach_msg_size_t size);
1c79356b
A
283
284/* Free a kernel message buffer */
285extern void ipc_kmsg_free(
0a7de745 286 ipc_kmsg_t kmsg);
1c79356b
A
287
288/* Destroy kernel message */
289extern void ipc_kmsg_destroy(
0a7de745 290 ipc_kmsg_t kmsg);
1c79356b 291
6d2010ae
A
292/* Enqueue kernel message for deferred destruction */
293extern boolean_t ipc_kmsg_delayed_destroy(
294 ipc_kmsg_t kmsg);
295
296/* Process all the delayed message destroys */
297extern void ipc_kmsg_reap_delayed(void);
91447636 298
1c79356b 299/* Preallocate a kernel message buffer */
b7266188 300extern ipc_kmsg_t ipc_kmsg_prealloc(
0a7de745 301 mach_msg_size_t size);
b7266188
A
302
303/* bind a preallocated message buffer to a port */
1c79356b 304extern void ipc_kmsg_set_prealloc(
0a7de745
A
305 ipc_kmsg_t kmsg,
306 ipc_port_t port);
1c79356b 307
b7266188 308/* Clear preallocated message buffer binding */
1c79356b 309extern void ipc_kmsg_clear_prealloc(
0a7de745
A
310 ipc_kmsg_t kmsg,
311 ipc_port_t port);
1c79356b
A
312
313/* Allocate a kernel message buffer and copy a user message to the buffer */
314extern mach_msg_return_t ipc_kmsg_get(
0a7de745
A
315 mach_vm_address_t msg_addr,
316 mach_msg_size_t size,
317 ipc_kmsg_t *kmsgp);
1c79356b
A
318
319/* Allocate a kernel message buffer and copy a kernel message to the buffer */
320extern mach_msg_return_t ipc_kmsg_get_from_kernel(
0a7de745
A
321 mach_msg_header_t *msg,
322 mach_msg_size_t size,
323 ipc_kmsg_t *kmsgp);
1c79356b
A
324
325/* Send a message to a port */
326extern mach_msg_return_t ipc_kmsg_send(
0a7de745
A
327 ipc_kmsg_t kmsg,
328 mach_msg_option_t option,
329 mach_msg_timeout_t timeout_val);
1c79356b
A
330
331/* Copy a kernel message buffer to a user message */
332extern mach_msg_return_t ipc_kmsg_put(
0a7de745
A
333 ipc_kmsg_t kmsg,
334 mach_msg_option_t option,
335 mach_vm_address_t rcv_addr,
336 mach_msg_size_t rcv_size,
337 mach_msg_size_t trailer_size,
338 mach_msg_size_t *size);
1c79356b
A
339
340/* Copy a kernel message buffer to a kernel message */
341extern void ipc_kmsg_put_to_kernel(
0a7de745
A
342 mach_msg_header_t *msg,
343 ipc_kmsg_t kmsg,
344 mach_msg_size_t size);
1c79356b
A
345
346/* Copyin port rights in the header of a message */
347extern mach_msg_return_t ipc_kmsg_copyin_header(
fe8ab488 348 ipc_kmsg_t kmsg,
0a7de745 349 ipc_space_t space,
f427ee49 350 mach_msg_priority_t priority,
0a7de745 351 mach_msg_option_t *optionp);
1c79356b
A
352
353/* Copyin port rights and out-of-line memory from a user message */
354extern mach_msg_return_t ipc_kmsg_copyin(
0a7de745
A
355 ipc_kmsg_t kmsg,
356 ipc_space_t space,
357 vm_map_t map,
f427ee49 358 mach_msg_priority_t priority,
0a7de745 359 mach_msg_option_t *optionp);
1c79356b
A
360
361/* Copyin port rights and out-of-line memory from a kernel message */
6d2010ae 362extern mach_msg_return_t ipc_kmsg_copyin_from_kernel(
0a7de745 363 ipc_kmsg_t kmsg);
1c79356b 364
b0d623f7 365#if IKM_SUPPORT_LEGACY
6d2010ae 366extern mach_msg_return_t ipc_kmsg_copyin_from_kernel_legacy(
0a7de745 367 ipc_kmsg_t kmsg);
b0d623f7
A
368#endif
369
1c79356b
A
370/* Copyout port rights in the header of a message */
371extern mach_msg_return_t ipc_kmsg_copyout_header(
fe8ab488 372 ipc_kmsg_t kmsg,
0a7de745
A
373 ipc_space_t space,
374 mach_msg_option_t option);
1c79356b
A
375
376/* Copyout a port right returning a name */
377extern mach_msg_return_t ipc_kmsg_copyout_object(
0a7de745
A
378 ipc_space_t space,
379 ipc_object_t object,
380 mach_msg_type_name_t msgt_name,
cb323159
A
381 mach_port_context_t *context,
382 mach_msg_guard_flags_t *guard_flags,
0a7de745 383 mach_port_name_t *namep);
1c79356b
A
384
385/* Copyout the header and body to a user message */
386extern mach_msg_return_t ipc_kmsg_copyout(
0a7de745
A
387 ipc_kmsg_t kmsg,
388 ipc_space_t space,
389 vm_map_t map,
390 mach_msg_body_t *slist,
391 mach_msg_option_t option);
1c79356b
A
392
393/* Copyout port rights and out-of-line memory from the body of a message */
394extern mach_msg_return_t ipc_kmsg_copyout_body(
0a7de745
A
395 ipc_kmsg_t kmsg,
396 ipc_space_t space,
397 vm_map_t map,
cb323159 398 mach_msg_option_t option,
0a7de745 399 mach_msg_body_t *slist);
1c79356b
A
400
401/* Copyout port rights and out-of-line memory to a user message,
0a7de745 402 * not reversing the ports in the header */
1c79356b 403extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
0a7de745
A
404 ipc_kmsg_t kmsg,
405 ipc_space_t space,
406 vm_map_t map,
407 mach_msg_body_t *slist);
1c79356b 408
91447636
A
409/* Compute size of message as copied out to the specified space/map */
410extern mach_msg_size_t ipc_kmsg_copyout_size(
0a7de745
A
411 ipc_kmsg_t kmsg,
412 vm_map_t map);
91447636 413
1c79356b 414/* Copyout the destination port in the message */
0a7de745
A
415extern void ipc_kmsg_copyout_dest(
416 ipc_kmsg_t kmsg,
417 ipc_space_t space);
1c79356b
A
418
419/* kernel's version of ipc_kmsg_copyout_dest */
420extern void ipc_kmsg_copyout_to_kernel(
0a7de745
A
421 ipc_kmsg_t kmsg,
422 ipc_space_t space);
1c79356b 423
b0d623f7
A
424#if IKM_SUPPORT_LEGACY
425extern void ipc_kmsg_copyout_to_kernel_legacy(
0a7de745
A
426 ipc_kmsg_t kmsg,
427 ipc_space_t space);
b0d623f7
A
428#endif
429
316670eb 430extern mach_msg_trailer_size_t
f427ee49
A
431ipc_kmsg_trailer_size(mach_msg_option_t option, thread_t thread);
432extern void
0a7de745
A
433ipc_kmsg_add_trailer(ipc_kmsg_t kmsg, ipc_space_t space,
434 mach_msg_option_t option, thread_t thread,
435 mach_port_seqno_t seqno, boolean_t minimal_trailer,
436 mach_vm_offset_t context);
316670eb 437
39037602
A
438#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
439extern void ipc_kmsg_trace_send(ipc_kmsg_t kmsg,
0a7de745 440 mach_msg_option_t option);
39037602 441#else
0a7de745 442#define ipc_kmsg_trace_send(a, b) do { } while (0)
39037602
A
443#endif
444
cb323159
A
445extern mach_msg_header_t *
446 ipc_kmsg_msg_header(ipc_kmsg_t);
447
0a7de745 448#endif /* _IPC_IPC_KMSG_H_ */