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