]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_kmsg.h
xnu-1456.1.26.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@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
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
73#ifndef _IPC_IPC_KMSG_H_
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
A
82#include <ipc/ipc_object.h>
83
84/*
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.
88 *
89 * In a kmsg, the port fields hold pointers to ports instead
90 * of port names. These pointers hold references.
91 *
92 * The ikm_header.msgh_remote_port field is the destination
93 * of the message.
94 */
95
2d21ac55 96struct ipc_labelh;
1c79356b 97
91447636 98struct ipc_kmsg {
1c79356b
A
99 struct ipc_kmsg *ikm_next;
100 struct ipc_kmsg *ikm_prev;
101 ipc_port_t ikm_prealloc; /* port we were preallocated from */
102 mach_msg_size_t ikm_size;
2d21ac55 103 struct ipc_labelh *ikm_sender;
91447636
A
104 mach_msg_header_t *ikm_header;
105};
1c79356b 106
b0d623f7
A
107#if defined(__i386__) || defined(__arm__)
108#define IKM_SUPPORT_LEGACY 1
109#else
110#define IKM_SUPPORT_LEGACY 0
111#endif
1c79356b 112
91447636 113#define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
1c79356b
A
114
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))
117
118/*
119 * XXX For debugging.
120 */
121#define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
122
123/*
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.
126 */
91447636
A
127extern 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)
1c79356b
A
130
131#define ikm_prealloc_inuse_port(kmsg) \
132 ((kmsg)->ikm_prealloc)
133
134#define ikm_prealloc_inuse(kmsg) \
135 ((kmsg)->ikm_prealloc != IP_NULL)
136
137#define ikm_prealloc_set_inuse(kmsg, port) \
138MACRO_BEGIN \
9bccf70c
A
139 assert((port) != IP_NULL); \
140 (kmsg)->ikm_prealloc = (port); \
141 ip_reference(port); \
1c79356b
A
142MACRO_END
143
144#define ikm_prealloc_clear_inuse(kmsg, port) \
145MACRO_BEGIN \
146 (kmsg)->ikm_prealloc = IP_NULL; \
9bccf70c 147 ip_release(port); \
1c79356b
A
148MACRO_END
149
1c79356b
A
150#define ikm_init(kmsg, size) \
151MACRO_BEGIN \
152 (kmsg)->ikm_size = (size); \
153 (kmsg)->ikm_prealloc = IP_NULL; \
2d21ac55 154 (kmsg)->ikm_sender = NULL; \
1c79356b
A
155 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
156MACRO_END
157
158#define ikm_check_init(kmsg, size) \
159MACRO_BEGIN \
160 assert((kmsg)->ikm_size == (size)); \
161 assert((kmsg)->ikm_prev == IKM_BOGUS); \
162 assert((kmsg)->ikm_next == IKM_BOGUS); \
163MACRO_END
164
165struct ipc_kmsg_queue {
166 struct ipc_kmsg *ikmq_base;
167};
168
169typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
170
171#define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
172
173
174/*
175 * Exported interfaces
176 */
177
178#define ipc_kmsg_queue_init(queue) \
179MACRO_BEGIN \
180 (queue)->ikmq_base = IKM_NULL; \
181MACRO_END
182
183#define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
184
185/* Enqueue a kmsg */
186extern void ipc_kmsg_enqueue(
187 ipc_kmsg_queue_t queue,
188 ipc_kmsg_t kmsg);
189
190/* Dequeue and return a kmsg */
191extern ipc_kmsg_t ipc_kmsg_dequeue(
192 ipc_kmsg_queue_t queue);
193
194/* Pull a kmsg out of a queue */
195extern void ipc_kmsg_rmqueue(
196 ipc_kmsg_queue_t queue,
197 ipc_kmsg_t kmsg);
198
199#define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
200
201/* Return the kmsg following the given kmsg */
202extern ipc_kmsg_t ipc_kmsg_queue_next(
203 ipc_kmsg_queue_t queue,
204 ipc_kmsg_t kmsg);
205
206#define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
207MACRO_BEGIN \
208 register ipc_kmsg_t _next; \
209 \
210 assert((queue)->ikmq_base == (kmsg)); \
211 \
212 _next = (kmsg)->ikm_next; \
213 if (_next == (kmsg)) { \
214 assert((kmsg)->ikm_prev == (kmsg)); \
215 (queue)->ikmq_base = IKM_NULL; \
216 } else { \
217 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
218 \
219 (queue)->ikmq_base = _next; \
220 _next->ikm_prev = _prev; \
221 _prev->ikm_next = _next; \
222 } \
223 /* XXX Debug paranoia ASSIGNMENTS */ \
224 assert(kmsg->ikm_next = IKM_BOGUS); \
225 assert(kmsg->ikm_prev = IKM_BOGUS); \
226MACRO_END
227
228#define ipc_kmsg_enqueue_macro(queue, kmsg) \
229MACRO_BEGIN \
230 register ipc_kmsg_t _first = (queue)->ikmq_base; \
231 \
232 if (_first == IKM_NULL) { \
233 (queue)->ikmq_base = (kmsg); \
234 (kmsg)->ikm_next = (kmsg); \
235 (kmsg)->ikm_prev = (kmsg); \
236 } else { \
237 register ipc_kmsg_t _last = _first->ikm_prev; \
238 \
239 (kmsg)->ikm_next = _first; \
240 (kmsg)->ikm_prev = _last; \
241 _first->ikm_prev = (kmsg); \
242 _last->ikm_next = (kmsg); \
243 } \
244MACRO_END
245
1c79356b
A
246/*
247 * extern void
248 * ipc_kmsg_send_always(ipc_kmsg_t);
1c79356b 249 */
1c79356b 250#define ipc_kmsg_send_always(kmsg) \
c910b4d9 251 ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, MACH_MSG_TIMEOUT_NONE)
1c79356b 252
91447636 253
1c79356b
A
254/* Allocate a kernel message */
255extern ipc_kmsg_t ipc_kmsg_alloc(
256 mach_msg_size_t size);
257
258/* Free a kernel message buffer */
259extern void ipc_kmsg_free(
260 ipc_kmsg_t kmsg);
261
262/* Destroy kernel message */
263extern void ipc_kmsg_destroy(
264 ipc_kmsg_t kmsg);
265
91447636
A
266/* destroy kernel message and a reference on the dest */
267extern void ipc_kmsg_destroy_dest(
268 ipc_kmsg_t kmsg);
269
270
1c79356b
A
271/* Preallocate a kernel message buffer */
272extern void ipc_kmsg_set_prealloc(
273 ipc_kmsg_t kmsg,
274 ipc_port_t port);
275
276/* Clear a kernel message buffer */
277extern void ipc_kmsg_clear_prealloc(
278 ipc_kmsg_t kmsg,
279 ipc_port_t port);
280
281/* Allocate a kernel message buffer and copy a user message to the buffer */
282extern mach_msg_return_t ipc_kmsg_get(
91447636 283 mach_vm_address_t msg_addr,
1c79356b
A
284 mach_msg_size_t size,
285 ipc_kmsg_t *kmsgp);
286
287/* Allocate a kernel message buffer and copy a kernel message to the buffer */
288extern mach_msg_return_t ipc_kmsg_get_from_kernel(
289 mach_msg_header_t *msg,
290 mach_msg_size_t size,
291 ipc_kmsg_t *kmsgp);
292
293/* Send a message to a port */
294extern mach_msg_return_t ipc_kmsg_send(
295 ipc_kmsg_t kmsg,
296 mach_msg_option_t option,
91447636 297 mach_msg_timeout_t timeout_val);
1c79356b
A
298
299/* Copy a kernel message buffer to a user message */
300extern mach_msg_return_t ipc_kmsg_put(
91447636 301 mach_vm_address_t msg_addr,
1c79356b
A
302 ipc_kmsg_t kmsg,
303 mach_msg_size_t size);
304
305/* Copy a kernel message buffer to a kernel message */
306extern void ipc_kmsg_put_to_kernel(
307 mach_msg_header_t *msg,
308 ipc_kmsg_t kmsg,
309 mach_msg_size_t size);
310
311/* Copyin port rights in the header of a message */
312extern mach_msg_return_t ipc_kmsg_copyin_header(
313 mach_msg_header_t *msg,
314 ipc_space_t space,
315 mach_port_name_t notify);
316
317/* Copyin port rights and out-of-line memory from a user message */
318extern mach_msg_return_t ipc_kmsg_copyin(
319 ipc_kmsg_t kmsg,
320 ipc_space_t space,
321 vm_map_t map,
322 mach_port_name_t notify);
323
324/* Copyin port rights and out-of-line memory from a kernel message */
325extern void ipc_kmsg_copyin_from_kernel(
326 ipc_kmsg_t kmsg);
327
b0d623f7
A
328#if IKM_SUPPORT_LEGACY
329extern void ipc_kmsg_copyin_from_kernel_legacy(
330 ipc_kmsg_t kmsg);
331#endif
332
1c79356b
A
333/* Copyout port rights in the header of a message */
334extern mach_msg_return_t ipc_kmsg_copyout_header(
335 mach_msg_header_t *msg,
336 ipc_space_t space,
337 mach_port_name_t notify);
338
339/* Copyout a port right returning a name */
340extern mach_msg_return_t ipc_kmsg_copyout_object(
341 ipc_space_t space,
342 ipc_object_t object,
343 mach_msg_type_name_t msgt_name,
344 mach_port_name_t *namep);
345
346/* Copyout the header and body to a user message */
347extern mach_msg_return_t ipc_kmsg_copyout(
348 ipc_kmsg_t kmsg,
349 ipc_space_t space,
350 vm_map_t map,
351 mach_port_name_t notify,
352 mach_msg_body_t *slist);
353
354/* Copyout port rights and out-of-line memory from the body of a message */
355extern mach_msg_return_t ipc_kmsg_copyout_body(
356 ipc_kmsg_t kmsg,
357 ipc_space_t space,
358 vm_map_t map,
359 mach_msg_body_t *slist);
360
361/* Copyout port rights and out-of-line memory to a user message,
362 not reversing the ports in the header */
363extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
364 ipc_kmsg_t kmsg,
365 ipc_space_t space,
366 vm_map_t map,
367 mach_msg_body_t *slist);
368
91447636
A
369/* Compute size of message as copied out to the specified space/map */
370extern mach_msg_size_t ipc_kmsg_copyout_size(
371 ipc_kmsg_t kmsg,
372 vm_map_t map);
373
1c79356b
A
374/* Copyout the destination port in the message */
375extern void ipc_kmsg_copyout_dest(
376 ipc_kmsg_t kmsg,
377 ipc_space_t space);
378
379/* kernel's version of ipc_kmsg_copyout_dest */
380extern void ipc_kmsg_copyout_to_kernel(
381 ipc_kmsg_t kmsg,
382 ipc_space_t space);
383
b0d623f7
A
384#if IKM_SUPPORT_LEGACY
385extern void ipc_kmsg_copyout_to_kernel_legacy(
386 ipc_kmsg_t kmsg,
387 ipc_space_t space);
388#endif
389
91447636
A
390/* get a scatter list and check consistency */
391extern mach_msg_body_t *ipc_kmsg_get_scatter(
392 mach_vm_address_t msg_addr,
1c79356b
A
393 mach_msg_size_t slist_size,
394 ipc_kmsg_t kmsg);
395
396/* free a scatter list */
397extern void ipc_kmsg_free_scatter(
398 mach_msg_body_t *slist,
399 mach_msg_size_t slist_size);
400
1c79356b 401#endif /* _IPC_IPC_KMSG_H_ */
2d21ac55 402