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