]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/ipc/ipc_kmsg.h
xnu-344.23.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_kmsg.h
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
63#include <cpus.h>
64
65#include <mach/vm_types.h>
66#include <mach/message.h>
67#include <kern/assert.h>
68#include <kern/cpu_number.h>
69#include <kern/macro_help.h>
70#include <kern/kalloc.h>
71#include <ipc/ipc_object.h>
72
73/*
74 * This structure is only the header for a kmsg buffer;
75 * the actual buffer is normally larger. The rest of the buffer
76 * holds the body of the message.
77 *
78 * In a kmsg, the port fields hold pointers to ports instead
79 * of port names. These pointers hold references.
80 *
81 * The ikm_header.msgh_remote_port field is the destination
82 * of the message.
83 */
84
85
86typedef struct ipc_kmsg {
87 struct ipc_kmsg *ikm_next;
88 struct ipc_kmsg *ikm_prev;
89 ipc_port_t ikm_prealloc; /* port we were preallocated from */
90 mach_msg_size_t ikm_size;
91 mach_msg_header_t ikm_header;
92} *ipc_kmsg_t;
93
94#define IKM_NULL ((ipc_kmsg_t) 0)
95
96#define IKM_OVERHEAD \
97 (sizeof(struct ipc_kmsg) - sizeof(mach_msg_header_t))
98
99#define ikm_plus_overhead(size) ((mach_msg_size_t)((size) + IKM_OVERHEAD))
100#define ikm_less_overhead(size) ((mach_msg_size_t)((size) - IKM_OVERHEAD))
101
102/*
103 * XXX For debugging.
104 */
105#define IKM_BOGUS ((ipc_kmsg_t) 0xffffff10)
106
107/*
108 * The size of the kernel message buffers that will be cached.
109 * IKM_SAVED_KMSG_SIZE includes overhead; IKM_SAVED_MSG_SIZE doesn't.
110 */
111
112#define IKM_SAVED_MSG_SIZE ikm_less_overhead(256)
113
114#define ikm_prealloc_inuse_port(kmsg) \
115 ((kmsg)->ikm_prealloc)
116
117#define ikm_prealloc_inuse(kmsg) \
118 ((kmsg)->ikm_prealloc != IP_NULL)
119
120#define ikm_prealloc_set_inuse(kmsg, port) \
121MACRO_BEGIN \
122 assert((port) != IP_NULL); \
123 (kmsg)->ikm_prealloc = (port); \
124 ip_reference(port); \
125MACRO_END
126
127#define ikm_prealloc_clear_inuse(kmsg, port) \
128MACRO_BEGIN \
129 (kmsg)->ikm_prealloc = IP_NULL; \
130 ip_release(port); \
131MACRO_END
132
133
134#define ikm_init(kmsg, size) \
135MACRO_BEGIN \
136 (kmsg)->ikm_size = (size); \
137 (kmsg)->ikm_prealloc = IP_NULL; \
138 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
139MACRO_END
140
141#define ikm_check_init(kmsg, size) \
142MACRO_BEGIN \
143 assert((kmsg)->ikm_size == (size)); \
144 assert((kmsg)->ikm_prev == IKM_BOGUS); \
145 assert((kmsg)->ikm_next == IKM_BOGUS); \
146MACRO_END
147
148struct ipc_kmsg_queue {
149 struct ipc_kmsg *ikmq_base;
150};
151
152typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
153
154#define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
155
156
157/*
158 * Exported interfaces
159 */
160
161#define ipc_kmsg_queue_init(queue) \
162MACRO_BEGIN \
163 (queue)->ikmq_base = IKM_NULL; \
164MACRO_END
165
166#define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
167
168/* Enqueue a kmsg */
169extern void ipc_kmsg_enqueue(
170 ipc_kmsg_queue_t queue,
171 ipc_kmsg_t kmsg);
172
173/* Dequeue and return a kmsg */
174extern ipc_kmsg_t ipc_kmsg_dequeue(
175 ipc_kmsg_queue_t queue);
176
177/* Pull a kmsg out of a queue */
178extern void ipc_kmsg_rmqueue(
179 ipc_kmsg_queue_t queue,
180 ipc_kmsg_t kmsg);
181
182#define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
183
184/* Return the kmsg following the given kmsg */
185extern ipc_kmsg_t ipc_kmsg_queue_next(
186 ipc_kmsg_queue_t queue,
187 ipc_kmsg_t kmsg);
188
189#define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
190MACRO_BEGIN \
191 register ipc_kmsg_t _next; \
192 \
193 assert((queue)->ikmq_base == (kmsg)); \
194 \
195 _next = (kmsg)->ikm_next; \
196 if (_next == (kmsg)) { \
197 assert((kmsg)->ikm_prev == (kmsg)); \
198 (queue)->ikmq_base = IKM_NULL; \
199 } else { \
200 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
201 \
202 (queue)->ikmq_base = _next; \
203 _next->ikm_prev = _prev; \
204 _prev->ikm_next = _next; \
205 } \
206 /* XXX Debug paranoia ASSIGNMENTS */ \
207 assert(kmsg->ikm_next = IKM_BOGUS); \
208 assert(kmsg->ikm_prev = IKM_BOGUS); \
209MACRO_END
210
211#define ipc_kmsg_enqueue_macro(queue, kmsg) \
212MACRO_BEGIN \
213 register ipc_kmsg_t _first = (queue)->ikmq_base; \
214 \
215 if (_first == IKM_NULL) { \
216 (queue)->ikmq_base = (kmsg); \
217 (kmsg)->ikm_next = (kmsg); \
218 (kmsg)->ikm_prev = (kmsg); \
219 } else { \
220 register ipc_kmsg_t _last = _first->ikm_prev; \
221 \
222 (kmsg)->ikm_next = _first; \
223 (kmsg)->ikm_prev = _last; \
224 _first->ikm_prev = (kmsg); \
225 _last->ikm_next = (kmsg); \
226 } \
227MACRO_END
228
229/* scatter list macros */
230
231#define SKIP_PORT_DESCRIPTORS(s, e) \
232MACRO_BEGIN \
233 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
234 while ((s) < (e)) { \
235 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
236 break; \
237 (s)++; \
238 } \
239 if ((s) >= (e)) \
240 (s) = MACH_MSG_DESCRIPTOR_NULL; \
241 } \
242MACRO_END
243
244#define INCREMENT_SCATTER(s) \
245MACRO_BEGIN \
246 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
247 (s)++; \
248 } \
249MACRO_END
250
251/*
252 * extern void
253 * ipc_kmsg_send_always(ipc_kmsg_t);
254 *
255 * Unfortunately, to avoid warnings/lint about unused variables
256 * when assertions are turned off, we need two versions of this.
257 */
258#if MACH_ASSERT
259
260#define ipc_kmsg_send_always(kmsg) \
261MACRO_BEGIN \
262 mach_msg_return_t mr; \
263 \
264 mr = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
265 MACH_MSG_TIMEOUT_NONE); \
266 assert(mr == MACH_MSG_SUCCESS); \
267MACRO_END
268
269#else /* MACH_ASSERT */
270
271#define ipc_kmsg_send_always(kmsg) \
272MACRO_BEGIN \
273 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
274 MACH_MSG_TIMEOUT_NONE); \
275MACRO_END
276
277#endif /* MACH_ASSERT */
278
279/* Allocate a kernel message */
280extern ipc_kmsg_t ipc_kmsg_alloc(
281 mach_msg_size_t size);
282
283/* Free a kernel message buffer */
284extern void ipc_kmsg_free(
285 ipc_kmsg_t kmsg);
286
287/* Destroy kernel message */
288extern void ipc_kmsg_destroy(
289 ipc_kmsg_t kmsg);
290
291/* Preallocate a kernel message buffer */
292extern void ipc_kmsg_set_prealloc(
293 ipc_kmsg_t kmsg,
294 ipc_port_t port);
295
296/* Clear a kernel message buffer */
297extern void ipc_kmsg_clear_prealloc(
298 ipc_kmsg_t kmsg,
299 ipc_port_t port);
300
301/* Allocate a kernel message buffer and copy a user message to the buffer */
302extern mach_msg_return_t ipc_kmsg_get(
303 mach_msg_header_t *msg,
304 mach_msg_size_t size,
305 ipc_kmsg_t *kmsgp);
306
307/* Allocate a kernel message buffer and copy a kernel message to the buffer */
308extern mach_msg_return_t ipc_kmsg_get_from_kernel(
309 mach_msg_header_t *msg,
310 mach_msg_size_t size,
311 ipc_kmsg_t *kmsgp);
312
313/* Send a message to a port */
314extern mach_msg_return_t ipc_kmsg_send(
315 ipc_kmsg_t kmsg,
316 mach_msg_option_t option,
317 mach_msg_timeout_t timeout);
318
319/* Copy a kernel message buffer to a user message */
320extern mach_msg_return_t ipc_kmsg_put(
321 mach_msg_header_t *msg,
322 ipc_kmsg_t kmsg,
323 mach_msg_size_t size);
324
325/* Copy a kernel message buffer to a kernel message */
326extern void ipc_kmsg_put_to_kernel(
327 mach_msg_header_t *msg,
328 ipc_kmsg_t kmsg,
329 mach_msg_size_t size);
330
331/* Copyin port rights in the header of a message */
332extern mach_msg_return_t ipc_kmsg_copyin_header(
333 mach_msg_header_t *msg,
334 ipc_space_t space,
335 mach_port_name_t notify);
336
337/* Copyin port rights and out-of-line memory from a user message */
338extern mach_msg_return_t ipc_kmsg_copyin(
339 ipc_kmsg_t kmsg,
340 ipc_space_t space,
341 vm_map_t map,
342 mach_port_name_t notify);
343
344/* Copyin port rights and out-of-line memory from a kernel message */
345extern void ipc_kmsg_copyin_from_kernel(
346 ipc_kmsg_t kmsg);
347
348/* Copyout port rights in the header of a message */
349extern mach_msg_return_t ipc_kmsg_copyout_header(
350 mach_msg_header_t *msg,
351 ipc_space_t space,
352 mach_port_name_t notify);
353
354/* Copyout a port right returning a name */
355extern mach_msg_return_t ipc_kmsg_copyout_object(
356 ipc_space_t space,
357 ipc_object_t object,
358 mach_msg_type_name_t msgt_name,
359 mach_port_name_t *namep);
360
361/* Copyout the header and body to a user message */
362extern mach_msg_return_t ipc_kmsg_copyout(
363 ipc_kmsg_t kmsg,
364 ipc_space_t space,
365 vm_map_t map,
366 mach_port_name_t notify,
367 mach_msg_body_t *slist);
368
369/* Copyout port rights and out-of-line memory from the body of a message */
370extern 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_body_t *slist);
375
376/* Copyout port rights and out-of-line memory to a user message,
377 not reversing the ports in the header */
378extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
379 ipc_kmsg_t kmsg,
380 ipc_space_t space,
381 vm_map_t map,
382 mach_msg_body_t *slist);
383
384/* Copyout the destination port in the message */
385extern void ipc_kmsg_copyout_dest(
386 ipc_kmsg_t kmsg,
387 ipc_space_t space);
388
389/* kernel's version of ipc_kmsg_copyout_dest */
390extern void ipc_kmsg_copyout_to_kernel(
391 ipc_kmsg_t kmsg,
392 ipc_space_t space);
393
394/* copyin a scatter list and check consistency */
395extern mach_msg_body_t *ipc_kmsg_copyin_scatter(
396 mach_msg_header_t *msg,
397 mach_msg_size_t slist_size,
398 ipc_kmsg_t kmsg);
399
400/* free a scatter list */
401extern void ipc_kmsg_free_scatter(
402 mach_msg_body_t *slist,
403 mach_msg_size_t slist_size);
404
405#include <mach_kdb.h>
406#if MACH_KDB
407
408/* Do a formatted dump of a kernel message */
409extern void ipc_kmsg_print(
410 ipc_kmsg_t kmsg);
411
412/* Do a formatted dump of a user message */
413extern void ipc_msg_print(
414 mach_msg_header_t *msgh);
415
416#endif /* MACH_KDB */
417
418#endif /* _IPC_IPC_KMSG_H_ */