]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_kmsg.h
xnu-792.6.61.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
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52/*
53 * File: ipc/ipc_kmsg.h
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Definitions for kernel messages.
58 */
59
60#ifndef _IPC_IPC_KMSG_H_
61#define _IPC_IPC_KMSG_H_
62
1c79356b
A
63#include <mach/vm_types.h>
64#include <mach/message.h>
91447636 65#include <kern/kern_types.h>
1c79356b 66#include <kern/assert.h>
1c79356b 67#include <kern/macro_help.h>
91447636 68#include <ipc/ipc_types.h>
1c79356b
A
69#include <ipc/ipc_object.h>
70
71/*
72 * This structure is only the header for a kmsg buffer;
73 * the actual buffer is normally larger. The rest of the buffer
74 * holds the body of the message.
75 *
76 * In a kmsg, the port fields hold pointers to ports instead
77 * of port names. These pointers hold references.
78 *
79 * The ikm_header.msgh_remote_port field is the destination
80 * of the message.
81 */
82
83
91447636 84struct ipc_kmsg {
1c79356b
A
85 struct ipc_kmsg *ikm_next;
86 struct ipc_kmsg *ikm_prev;
87 ipc_port_t ikm_prealloc; /* port we were preallocated from */
88 mach_msg_size_t ikm_size;
91447636
A
89 mach_msg_header_t *ikm_header;
90};
1c79356b 91
1c79356b 92
91447636 93#define IKM_OVERHEAD (sizeof(struct ipc_kmsg))
1c79356b
A
94
95#define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
96#define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
97
98/*
99 * XXX For debugging.
100 */
101#define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
102
103/*
104 * The size of the kernel message buffers that will be cached.
105 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
106 */
91447636
A
107extern zone_t ipc_kmsg_zone;
108#define IKM_SAVED_KMSG_SIZE 256
109#define IKM_SAVED_MSG_SIZE ikm_less_overhead(IKM_SAVED_KMSG_SIZE)
1c79356b
A
110
111#define ikm_prealloc_inuse_port(kmsg) \
112 ((kmsg)->ikm_prealloc)
113
114#define ikm_prealloc_inuse(kmsg) \
115 ((kmsg)->ikm_prealloc != IP_NULL)
116
117#define ikm_prealloc_set_inuse(kmsg, port) \
118MACRO_BEGIN \
9bccf70c
A
119 assert((port) != IP_NULL); \
120 (kmsg)->ikm_prealloc = (port); \
121 ip_reference(port); \
1c79356b
A
122MACRO_END
123
124#define ikm_prealloc_clear_inuse(kmsg, port) \
125MACRO_BEGIN \
126 (kmsg)->ikm_prealloc = IP_NULL; \
9bccf70c 127 ip_release(port); \
1c79356b
A
128MACRO_END
129
130
131#define ikm_init(kmsg, size) \
132MACRO_BEGIN \
133 (kmsg)->ikm_size = (size); \
134 (kmsg)->ikm_prealloc = IP_NULL; \
135 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
136MACRO_END
137
138#define ikm_check_init(kmsg, size) \
139MACRO_BEGIN \
140 assert((kmsg)->ikm_size == (size)); \
141 assert((kmsg)->ikm_prev == IKM_BOGUS); \
142 assert((kmsg)->ikm_next == IKM_BOGUS); \
143MACRO_END
144
145struct ipc_kmsg_queue {
146 struct ipc_kmsg *ikmq_base;
147};
148
149typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
150
151#define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
152
153
154/*
155 * Exported interfaces
156 */
157
158#define ipc_kmsg_queue_init(queue) \
159MACRO_BEGIN \
160 (queue)->ikmq_base = IKM_NULL; \
161MACRO_END
162
163#define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
164
165/* Enqueue a kmsg */
166extern void ipc_kmsg_enqueue(
167 ipc_kmsg_queue_t queue,
168 ipc_kmsg_t kmsg);
169
170/* Dequeue and return a kmsg */
171extern ipc_kmsg_t ipc_kmsg_dequeue(
172 ipc_kmsg_queue_t queue);
173
174/* Pull a kmsg out of a queue */
175extern void ipc_kmsg_rmqueue(
176 ipc_kmsg_queue_t queue,
177 ipc_kmsg_t kmsg);
178
179#define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
180
181/* Return the kmsg following the given kmsg */
182extern ipc_kmsg_t ipc_kmsg_queue_next(
183 ipc_kmsg_queue_t queue,
184 ipc_kmsg_t kmsg);
185
186#define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
187MACRO_BEGIN \
188 register ipc_kmsg_t _next; \
189 \
190 assert((queue)->ikmq_base == (kmsg)); \
191 \
192 _next = (kmsg)->ikm_next; \
193 if (_next == (kmsg)) { \
194 assert((kmsg)->ikm_prev == (kmsg)); \
195 (queue)->ikmq_base = IKM_NULL; \
196 } else { \
197 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
198 \
199 (queue)->ikmq_base = _next; \
200 _next->ikm_prev = _prev; \
201 _prev->ikm_next = _next; \
202 } \
203 /* XXX Debug paranoia ASSIGNMENTS */ \
204 assert(kmsg->ikm_next = IKM_BOGUS); \
205 assert(kmsg->ikm_prev = IKM_BOGUS); \
206MACRO_END
207
208#define ipc_kmsg_enqueue_macro(queue, kmsg) \
209MACRO_BEGIN \
210 register ipc_kmsg_t _first = (queue)->ikmq_base; \
211 \
212 if (_first == IKM_NULL) { \
213 (queue)->ikmq_base = (kmsg); \
214 (kmsg)->ikm_next = (kmsg); \
215 (kmsg)->ikm_prev = (kmsg); \
216 } else { \
217 register ipc_kmsg_t _last = _first->ikm_prev; \
218 \
219 (kmsg)->ikm_next = _first; \
220 (kmsg)->ikm_prev = _last; \
221 _first->ikm_prev = (kmsg); \
222 _last->ikm_next = (kmsg); \
223 } \
224MACRO_END
225
1c79356b
A
226/*
227 * extern void
228 * ipc_kmsg_send_always(ipc_kmsg_t);
229 *
230 * Unfortunately, to avoid warnings/lint about unused variables
231 * when assertions are turned off, we need two versions of this.
232 */
233#if MACH_ASSERT
234
235#define ipc_kmsg_send_always(kmsg) \
236MACRO_BEGIN \
91447636 237 mach_msg_return_t mr2; \
1c79356b 238 \
91447636 239 mr2 = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
1c79356b
A
240 MACH_MSG_TIMEOUT_NONE); \
241 assert(mr == MACH_MSG_SUCCESS); \
242MACRO_END
243
244#else /* MACH_ASSERT */
245
246#define ipc_kmsg_send_always(kmsg) \
247MACRO_BEGIN \
248 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
249 MACH_MSG_TIMEOUT_NONE); \
250MACRO_END
251
252#endif /* MACH_ASSERT */
253
91447636 254
1c79356b
A
255/* Allocate a kernel message */
256extern ipc_kmsg_t ipc_kmsg_alloc(
257 mach_msg_size_t size);
258
259/* Free a kernel message buffer */
260extern void ipc_kmsg_free(
261 ipc_kmsg_t kmsg);
262
263/* Destroy kernel message */
264extern void ipc_kmsg_destroy(
265 ipc_kmsg_t kmsg);
266
91447636
A
267/* destroy kernel message and a reference on the dest */
268extern void ipc_kmsg_destroy_dest(
269 ipc_kmsg_t kmsg);
270
271
1c79356b
A
272/* Preallocate a kernel message buffer */
273extern void ipc_kmsg_set_prealloc(
274 ipc_kmsg_t kmsg,
275 ipc_port_t port);
276
277/* Clear a kernel message buffer */
278extern void ipc_kmsg_clear_prealloc(
279 ipc_kmsg_t kmsg,
280 ipc_port_t port);
281
282/* Allocate a kernel message buffer and copy a user message to the buffer */
283extern mach_msg_return_t ipc_kmsg_get(
91447636 284 mach_vm_address_t msg_addr,
1c79356b
A
285 mach_msg_size_t size,
286 ipc_kmsg_t *kmsgp);
287
288/* Allocate a kernel message buffer and copy a kernel message to the buffer */
289extern mach_msg_return_t ipc_kmsg_get_from_kernel(
290 mach_msg_header_t *msg,
291 mach_msg_size_t size,
292 ipc_kmsg_t *kmsgp);
293
294/* Send a message to a port */
295extern mach_msg_return_t ipc_kmsg_send(
296 ipc_kmsg_t kmsg,
297 mach_msg_option_t option,
91447636 298 mach_msg_timeout_t timeout_val);
1c79356b
A
299
300/* Copy a kernel message buffer to a user message */
301extern mach_msg_return_t ipc_kmsg_put(
91447636 302 mach_vm_address_t msg_addr,
1c79356b
A
303 ipc_kmsg_t kmsg,
304 mach_msg_size_t size);
305
306/* Copy a kernel message buffer to a kernel message */
307extern void ipc_kmsg_put_to_kernel(
308 mach_msg_header_t *msg,
309 ipc_kmsg_t kmsg,
310 mach_msg_size_t size);
311
312/* Copyin port rights in the header of a message */
313extern mach_msg_return_t ipc_kmsg_copyin_header(
314 mach_msg_header_t *msg,
315 ipc_space_t space,
316 mach_port_name_t notify);
317
318/* Copyin port rights and out-of-line memory from a user message */
319extern mach_msg_return_t ipc_kmsg_copyin(
320 ipc_kmsg_t kmsg,
321 ipc_space_t space,
322 vm_map_t map,
323 mach_port_name_t notify);
324
325/* Copyin port rights and out-of-line memory from a kernel message */
326extern void ipc_kmsg_copyin_from_kernel(
327 ipc_kmsg_t kmsg);
328
329/* Copyout port rights in the header of a message */
330extern mach_msg_return_t ipc_kmsg_copyout_header(
331 mach_msg_header_t *msg,
332 ipc_space_t space,
333 mach_port_name_t notify);
334
335/* Copyout a port right returning a name */
336extern mach_msg_return_t ipc_kmsg_copyout_object(
337 ipc_space_t space,
338 ipc_object_t object,
339 mach_msg_type_name_t msgt_name,
340 mach_port_name_t *namep);
341
342/* Copyout the header and body to a user message */
343extern mach_msg_return_t ipc_kmsg_copyout(
344 ipc_kmsg_t kmsg,
345 ipc_space_t space,
346 vm_map_t map,
347 mach_port_name_t notify,
348 mach_msg_body_t *slist);
349
350/* Copyout port rights and out-of-line memory from the body of a message */
351extern mach_msg_return_t ipc_kmsg_copyout_body(
352 ipc_kmsg_t kmsg,
353 ipc_space_t space,
354 vm_map_t map,
355 mach_msg_body_t *slist);
356
357/* Copyout port rights and out-of-line memory to a user message,
358 not reversing the ports in the header */
359extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
360 ipc_kmsg_t kmsg,
361 ipc_space_t space,
362 vm_map_t map,
363 mach_msg_body_t *slist);
364
91447636
A
365/* Compute size of message as copied out to the specified space/map */
366extern mach_msg_size_t ipc_kmsg_copyout_size(
367 ipc_kmsg_t kmsg,
368 vm_map_t map);
369
1c79356b
A
370/* Copyout the destination port in the message */
371extern void ipc_kmsg_copyout_dest(
372 ipc_kmsg_t kmsg,
373 ipc_space_t space);
374
375/* kernel's version of ipc_kmsg_copyout_dest */
376extern void ipc_kmsg_copyout_to_kernel(
377 ipc_kmsg_t kmsg,
378 ipc_space_t space);
379
91447636
A
380/* get a scatter list and check consistency */
381extern mach_msg_body_t *ipc_kmsg_get_scatter(
382 mach_vm_address_t msg_addr,
1c79356b
A
383 mach_msg_size_t slist_size,
384 ipc_kmsg_t kmsg);
385
386/* free a scatter list */
387extern void ipc_kmsg_free_scatter(
388 mach_msg_body_t *slist,
389 mach_msg_size_t slist_size);
390
1c79356b 391#endif /* _IPC_IPC_KMSG_H_ */