]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_kmsg.h
xnu-124.13.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_kmsg.h
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
86 typedef 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) \
121 MACRO_BEGIN \
122 assert(port != IP_NULL); \
123 (kmsg)->ikm_prealloc = port; \
124 MACRO_END
125
126 #define ikm_prealloc_clear_inuse(kmsg, port) \
127 MACRO_BEGIN \
128 (kmsg)->ikm_prealloc = IP_NULL; \
129 MACRO_END
130
131
132 #define ikm_init(kmsg, size) \
133 MACRO_BEGIN \
134 (kmsg)->ikm_size = (size); \
135 (kmsg)->ikm_prealloc = IP_NULL; \
136 assert((kmsg)->ikm_prev = (kmsg)->ikm_next = IKM_BOGUS); \
137 MACRO_END
138
139 #define ikm_check_init(kmsg, size) \
140 MACRO_BEGIN \
141 assert((kmsg)->ikm_size == (size)); \
142 assert((kmsg)->ikm_prev == IKM_BOGUS); \
143 assert((kmsg)->ikm_next == IKM_BOGUS); \
144 MACRO_END
145
146 struct ipc_kmsg_queue {
147 struct ipc_kmsg *ikmq_base;
148 };
149
150 typedef struct ipc_kmsg_queue *ipc_kmsg_queue_t;
151
152 #define IKMQ_NULL ((ipc_kmsg_queue_t) 0)
153
154
155 /*
156 * Exported interfaces
157 */
158
159 #define ipc_kmsg_queue_init(queue) \
160 MACRO_BEGIN \
161 (queue)->ikmq_base = IKM_NULL; \
162 MACRO_END
163
164 #define ipc_kmsg_queue_empty(queue) ((queue)->ikmq_base == IKM_NULL)
165
166 /* Enqueue a kmsg */
167 extern void ipc_kmsg_enqueue(
168 ipc_kmsg_queue_t queue,
169 ipc_kmsg_t kmsg);
170
171 /* Dequeue and return a kmsg */
172 extern ipc_kmsg_t ipc_kmsg_dequeue(
173 ipc_kmsg_queue_t queue);
174
175 /* Pull a kmsg out of a queue */
176 extern void ipc_kmsg_rmqueue(
177 ipc_kmsg_queue_t queue,
178 ipc_kmsg_t kmsg);
179
180 #define ipc_kmsg_queue_first(queue) ((queue)->ikmq_base)
181
182 /* Return the kmsg following the given kmsg */
183 extern ipc_kmsg_t ipc_kmsg_queue_next(
184 ipc_kmsg_queue_t queue,
185 ipc_kmsg_t kmsg);
186
187 #define ipc_kmsg_rmqueue_first_macro(queue, kmsg) \
188 MACRO_BEGIN \
189 register ipc_kmsg_t _next; \
190 \
191 assert((queue)->ikmq_base == (kmsg)); \
192 \
193 _next = (kmsg)->ikm_next; \
194 if (_next == (kmsg)) { \
195 assert((kmsg)->ikm_prev == (kmsg)); \
196 (queue)->ikmq_base = IKM_NULL; \
197 } else { \
198 register ipc_kmsg_t _prev = (kmsg)->ikm_prev; \
199 \
200 (queue)->ikmq_base = _next; \
201 _next->ikm_prev = _prev; \
202 _prev->ikm_next = _next; \
203 } \
204 /* XXX Debug paranoia ASSIGNMENTS */ \
205 assert(kmsg->ikm_next = IKM_BOGUS); \
206 assert(kmsg->ikm_prev = IKM_BOGUS); \
207 MACRO_END
208
209 #define ipc_kmsg_enqueue_macro(queue, kmsg) \
210 MACRO_BEGIN \
211 register ipc_kmsg_t _first = (queue)->ikmq_base; \
212 \
213 if (_first == IKM_NULL) { \
214 (queue)->ikmq_base = (kmsg); \
215 (kmsg)->ikm_next = (kmsg); \
216 (kmsg)->ikm_prev = (kmsg); \
217 } else { \
218 register ipc_kmsg_t _last = _first->ikm_prev; \
219 \
220 (kmsg)->ikm_next = _first; \
221 (kmsg)->ikm_prev = _last; \
222 _first->ikm_prev = (kmsg); \
223 _last->ikm_next = (kmsg); \
224 } \
225 MACRO_END
226
227 /* scatter list macros */
228
229 #define SKIP_PORT_DESCRIPTORS(s, e) \
230 MACRO_BEGIN \
231 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
232 while ((s) < (e)) { \
233 if ((s)->type.type != MACH_MSG_PORT_DESCRIPTOR) \
234 break; \
235 (s)++; \
236 } \
237 if ((s) >= (e)) \
238 (s) = MACH_MSG_DESCRIPTOR_NULL; \
239 } \
240 MACRO_END
241
242 #define INCREMENT_SCATTER(s) \
243 MACRO_BEGIN \
244 if ((s) != MACH_MSG_DESCRIPTOR_NULL) { \
245 (s)++; \
246 } \
247 MACRO_END
248
249 /*
250 * extern void
251 * ipc_kmsg_send_always(ipc_kmsg_t);
252 *
253 * Unfortunately, to avoid warnings/lint about unused variables
254 * when assertions are turned off, we need two versions of this.
255 */
256 #if MACH_ASSERT
257
258 #define ipc_kmsg_send_always(kmsg) \
259 MACRO_BEGIN \
260 mach_msg_return_t mr; \
261 \
262 mr = ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
263 MACH_MSG_TIMEOUT_NONE); \
264 assert(mr == MACH_MSG_SUCCESS); \
265 MACRO_END
266
267 #else /* MACH_ASSERT */
268
269 #define ipc_kmsg_send_always(kmsg) \
270 MACRO_BEGIN \
271 (void) ipc_kmsg_send((kmsg), MACH_SEND_ALWAYS, \
272 MACH_MSG_TIMEOUT_NONE); \
273 MACRO_END
274
275 #endif /* MACH_ASSERT */
276
277 /* Allocate a kernel message */
278 extern ipc_kmsg_t ipc_kmsg_alloc(
279 mach_msg_size_t size);
280
281 /* Free a kernel message buffer */
282 extern void ipc_kmsg_free(
283 ipc_kmsg_t kmsg);
284
285 /* Destroy kernel message */
286 extern void ipc_kmsg_destroy(
287 ipc_kmsg_t kmsg);
288
289 /* Preallocate a kernel message buffer */
290 extern void ipc_kmsg_set_prealloc(
291 ipc_kmsg_t kmsg,
292 ipc_port_t port);
293
294 /* Clear a kernel message buffer */
295 extern void ipc_kmsg_clear_prealloc(
296 ipc_kmsg_t kmsg,
297 ipc_port_t port);
298
299 /* Allocate a kernel message buffer and copy a user message to the buffer */
300 extern mach_msg_return_t ipc_kmsg_get(
301 mach_msg_header_t *msg,
302 mach_msg_size_t size,
303 ipc_kmsg_t *kmsgp);
304
305 /* Allocate a kernel message buffer and copy a kernel message to the buffer */
306 extern mach_msg_return_t ipc_kmsg_get_from_kernel(
307 mach_msg_header_t *msg,
308 mach_msg_size_t size,
309 ipc_kmsg_t *kmsgp);
310
311 /* Send a message to a port */
312 extern mach_msg_return_t ipc_kmsg_send(
313 ipc_kmsg_t kmsg,
314 mach_msg_option_t option,
315 mach_msg_timeout_t timeout);
316
317 /* Copy a kernel message buffer to a user message */
318 extern mach_msg_return_t ipc_kmsg_put(
319 mach_msg_header_t *msg,
320 ipc_kmsg_t kmsg,
321 mach_msg_size_t size);
322
323 /* Copy a kernel message buffer to a kernel message */
324 extern void ipc_kmsg_put_to_kernel(
325 mach_msg_header_t *msg,
326 ipc_kmsg_t kmsg,
327 mach_msg_size_t size);
328
329 /* Copyin port rights in the header of a message */
330 extern mach_msg_return_t ipc_kmsg_copyin_header(
331 mach_msg_header_t *msg,
332 ipc_space_t space,
333 mach_port_name_t notify);
334
335 /* Copyin port rights and out-of-line memory from a user message */
336 extern mach_msg_return_t ipc_kmsg_copyin(
337 ipc_kmsg_t kmsg,
338 ipc_space_t space,
339 vm_map_t map,
340 mach_port_name_t notify);
341
342 /* Copyin port rights and out-of-line memory from a kernel message */
343 extern void ipc_kmsg_copyin_from_kernel(
344 ipc_kmsg_t kmsg);
345
346 /* Copyout port rights in the header of a message */
347 extern mach_msg_return_t ipc_kmsg_copyout_header(
348 mach_msg_header_t *msg,
349 ipc_space_t space,
350 mach_port_name_t notify);
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_name_t *namep);
358
359 /* Copyout the header and body to a user message */
360 extern mach_msg_return_t ipc_kmsg_copyout(
361 ipc_kmsg_t kmsg,
362 ipc_space_t space,
363 vm_map_t map,
364 mach_port_name_t notify,
365 mach_msg_body_t *slist);
366
367 /* Copyout port rights and out-of-line memory from the body of a message */
368 extern mach_msg_return_t ipc_kmsg_copyout_body(
369 ipc_kmsg_t kmsg,
370 ipc_space_t space,
371 vm_map_t map,
372 mach_msg_body_t *slist);
373
374 /* Copyout port rights and out-of-line memory to a user message,
375 not reversing the ports in the header */
376 extern mach_msg_return_t ipc_kmsg_copyout_pseudo(
377 ipc_kmsg_t kmsg,
378 ipc_space_t space,
379 vm_map_t map,
380 mach_msg_body_t *slist);
381
382 /* Copyout the destination port in the message */
383 extern void ipc_kmsg_copyout_dest(
384 ipc_kmsg_t kmsg,
385 ipc_space_t space);
386
387 /* kernel's version of ipc_kmsg_copyout_dest */
388 extern void ipc_kmsg_copyout_to_kernel(
389 ipc_kmsg_t kmsg,
390 ipc_space_t space);
391
392 /* copyin a scatter list and check consistency */
393 extern mach_msg_body_t *ipc_kmsg_copyin_scatter(
394 mach_msg_header_t *msg,
395 mach_msg_size_t slist_size,
396 ipc_kmsg_t kmsg);
397
398 /* free a scatter list */
399 extern void ipc_kmsg_free_scatter(
400 mach_msg_body_t *slist,
401 mach_msg_size_t slist_size);
402
403 #include <mach_kdb.h>
404 #if MACH_KDB
405
406 /* Do a formatted dump of a kernel message */
407 extern void ipc_kmsg_print(
408 ipc_kmsg_t kmsg);
409
410 /* Do a formatted dump of a user message */
411 extern void ipc_msg_print(
412 mach_msg_header_t *msgh);
413
414 #endif /* MACH_KDB */
415
416 #endif /* _IPC_IPC_KMSG_H_ */