]>
Commit | Line | Data |
---|---|---|
1c79356b A |
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.c | |
54 | * Author: Rich Draves | |
55 | * Date: 1989 | |
56 | * | |
57 | * Operations on kernel messages. | |
58 | */ | |
59 | ||
60 | #include <cpus.h> | |
61 | #include <norma_vm.h> | |
62 | ||
63 | #include <mach/boolean.h> | |
64 | #include <mach/kern_return.h> | |
65 | #include <mach/message.h> | |
66 | #include <mach/port.h> | |
67 | #include <mach/vm_statistics.h> | |
68 | #include <kern/assert.h> | |
69 | #include <kern/kalloc.h> | |
70 | #include <kern/thread.h> | |
71 | #include <kern/sched_prim.h> | |
72 | #include <kern/spl.h> | |
73 | #include <kern/misc_protos.h> | |
74 | #include <kern/counters.h> | |
75 | #include <vm/vm_map.h> | |
76 | #include <vm/vm_object.h> | |
77 | #include <vm/vm_kern.h> | |
78 | #include <ipc/port.h> | |
79 | #include <ipc/ipc_entry.h> | |
80 | #include <ipc/ipc_kmsg.h> | |
81 | #include <ipc/ipc_notify.h> | |
82 | #include <ipc/ipc_object.h> | |
83 | #include <ipc/ipc_space.h> | |
84 | #include <ipc/ipc_port.h> | |
85 | #include <ipc/ipc_right.h> | |
86 | #include <ipc/ipc_hash.h> | |
87 | #include <ipc/ipc_table.h> | |
88 | ||
89 | #include <string.h> | |
90 | ||
91 | extern vm_map_t ipc_kernel_copy_map; | |
92 | extern vm_size_t ipc_kmsg_max_vm_space; | |
93 | extern vm_size_t msg_ool_size_small; | |
94 | ||
95 | #define MSG_OOL_SIZE_SMALL msg_ool_size_small | |
96 | ||
97 | ||
98 | /* | |
99 | * Forward declarations | |
100 | */ | |
101 | ||
102 | void ipc_kmsg_clean( | |
103 | ipc_kmsg_t kmsg); | |
104 | ||
105 | void ipc_kmsg_clean_body( | |
106 | ipc_kmsg_t kmsg, | |
107 | mach_msg_type_number_t number); | |
108 | ||
109 | void ipc_kmsg_clean_partial( | |
110 | ipc_kmsg_t kmsg, | |
111 | mach_msg_type_number_t number, | |
112 | vm_offset_t paddr, | |
113 | vm_size_t length); | |
114 | ||
115 | mach_msg_return_t ipc_kmsg_copyout_body( | |
116 | ipc_kmsg_t kmsg, | |
117 | ipc_space_t space, | |
118 | vm_map_t map, | |
119 | mach_msg_body_t *slist); | |
120 | ||
121 | mach_msg_return_t ipc_kmsg_copyin_body( | |
122 | ipc_kmsg_t kmsg, | |
123 | ipc_space_t space, | |
124 | vm_map_t map); | |
125 | ||
126 | void ikm_cache_init(void); | |
127 | /* | |
128 | * We keep a per-processor cache of kernel message buffers. | |
129 | * The cache saves the overhead/locking of using kalloc/kfree. | |
130 | * The per-processor cache seems to miss less than a per-thread cache, | |
131 | * and it also uses less memory. Access to the cache doesn't | |
132 | * require locking. | |
133 | */ | |
134 | #define IKM_STASH 16 /* # of cache entries per cpu */ | |
135 | ipc_kmsg_t ipc_kmsg_cache[ NCPUS ][ IKM_STASH ]; | |
136 | unsigned int ipc_kmsg_cache_avail[NCPUS]; | |
137 | ||
138 | /* | |
139 | * Routine: ipc_kmsg_init | |
140 | * Purpose: | |
141 | * Initialize the kmsg system. For each CPU, we need to | |
142 | * pre-stuff the kmsg cache. | |
143 | */ | |
144 | void | |
145 | ipc_kmsg_init() | |
146 | { | |
147 | unsigned int cpu, i; | |
148 | ||
149 | for (cpu = 0; cpu < NCPUS; ++cpu) { | |
150 | for (i = 0; i < IKM_STASH; ++i) { | |
151 | ipc_kmsg_t kmsg; | |
152 | ||
153 | kmsg = (ipc_kmsg_t) | |
154 | kalloc(ikm_plus_overhead(IKM_SAVED_MSG_SIZE)); | |
155 | if (kmsg == IKM_NULL) | |
156 | panic("ipc_kmsg_init"); | |
157 | ikm_init(kmsg, IKM_SAVED_MSG_SIZE); | |
158 | ipc_kmsg_cache[cpu][i] = kmsg; | |
159 | } | |
160 | ipc_kmsg_cache_avail[cpu] = IKM_STASH; | |
161 | } | |
162 | } | |
163 | ||
164 | /* | |
165 | * Routine: ipc_kmsg_alloc | |
166 | * Purpose: | |
167 | * Allocate a kernel message structure. If we can get one from | |
168 | * the cache, that is best. Otherwise, allocate a new one. | |
169 | * Conditions: | |
170 | * Nothing locked. | |
171 | */ | |
172 | ipc_kmsg_t | |
173 | ipc_kmsg_alloc( | |
174 | mach_msg_size_t msg_and_trailer_size) | |
175 | { | |
176 | ipc_kmsg_t kmsg; | |
177 | ||
178 | if ((msg_and_trailer_size <= IKM_SAVED_MSG_SIZE)) { | |
179 | unsigned int cpu, i; | |
180 | ||
181 | disable_preemption(); | |
182 | cpu = cpu_number(); | |
183 | if ((i = ipc_kmsg_cache_avail[cpu]) > 0) { | |
184 | assert(i <= IKM_STASH); | |
185 | kmsg = ipc_kmsg_cache[cpu][--i]; | |
186 | ipc_kmsg_cache_avail[cpu] = i; | |
187 | ikm_check_init(kmsg, IKM_SAVED_MSG_SIZE); | |
188 | enable_preemption(); | |
189 | return (kmsg); | |
190 | } | |
191 | enable_preemption(); | |
192 | } | |
193 | ||
194 | /* round up for ikm_cache */ | |
195 | if (msg_and_trailer_size < IKM_SAVED_MSG_SIZE) | |
196 | msg_and_trailer_size = IKM_SAVED_MSG_SIZE; | |
197 | ||
198 | kmsg = (ipc_kmsg_t)kalloc(ikm_plus_overhead(msg_and_trailer_size)); | |
199 | if (kmsg != IKM_NULL) { | |
200 | ikm_init(kmsg, msg_and_trailer_size); | |
201 | } | |
202 | return(kmsg); | |
203 | } | |
204 | ||
205 | /* | |
206 | * Routine: ipc_kmsg_free | |
207 | * Purpose: | |
208 | * Free a kernel message buffer. If the kms is preallocated | |
209 | * to a port, just "put it back (marked unused)." We have to | |
210 | * do this with the port locked. The port may have its hold | |
211 | * on our message released. In that case, we have to just | |
212 | * revert the message to a traditional one and free it normally. | |
213 | * Conditions: | |
214 | * Nothing locked. | |
215 | */ | |
216 | ||
217 | void | |
218 | ipc_kmsg_free( | |
219 | ipc_kmsg_t kmsg) | |
220 | { | |
221 | mach_msg_size_t size = kmsg->ikm_size; | |
222 | ipc_port_t port; | |
223 | ||
224 | /* | |
225 | * Check to see if the message is bound to the port. If so, | |
226 | * mark it not in use. If the port isn't already dead, then | |
227 | * leave the message associated with it. Otherwise, free it | |
228 | * (not to the cache). | |
229 | */ | |
230 | port = ikm_prealloc_inuse_port(kmsg); | |
231 | if (port != IP_NULL) { | |
232 | ip_lock(port); | |
233 | ikm_prealloc_clear_inuse(kmsg, port); | |
234 | if (ip_active(port) && (port->ip_premsg == kmsg)) { | |
235 | assert(IP_PREALLOC(port)); | |
236 | ip_unlock(port); | |
237 | return; | |
238 | } | |
239 | ip_unlock(port); | |
240 | goto free_it; | |
241 | } | |
242 | ||
243 | /* | |
244 | * Peek and see if it has to go back in the cache. | |
245 | */ | |
246 | if (kmsg->ikm_size == IKM_SAVED_MSG_SIZE && | |
247 | ipc_kmsg_cache_avail[cpu_number()] < IKM_STASH) { | |
248 | unsigned int cpu, i; | |
249 | ||
250 | disable_preemption(); | |
251 | cpu = cpu_number(); | |
252 | ||
253 | i = ipc_kmsg_cache_avail[cpu]; | |
254 | if (i < IKM_STASH) { | |
255 | assert(i >= 0); | |
256 | ipc_kmsg_cache[cpu][i] = kmsg; | |
257 | ipc_kmsg_cache_avail[cpu] = i + 1; | |
258 | enable_preemption(); | |
259 | return; | |
260 | } | |
261 | enable_preemption(); | |
262 | } | |
263 | ||
264 | free_it: | |
265 | kfree((vm_offset_t) kmsg, ikm_plus_overhead(size)); | |
266 | } | |
267 | ||
268 | ||
269 | /* | |
270 | * Routine: ipc_kmsg_enqueue | |
271 | * Purpose: | |
272 | * Enqueue a kmsg. | |
273 | */ | |
274 | ||
275 | void | |
276 | ipc_kmsg_enqueue( | |
277 | ipc_kmsg_queue_t queue, | |
278 | ipc_kmsg_t kmsg) | |
279 | { | |
280 | ipc_kmsg_enqueue_macro(queue, kmsg); | |
281 | } | |
282 | ||
283 | /* | |
284 | * Routine: ipc_kmsg_dequeue | |
285 | * Purpose: | |
286 | * Dequeue and return a kmsg. | |
287 | */ | |
288 | ||
289 | ipc_kmsg_t | |
290 | ipc_kmsg_dequeue( | |
291 | ipc_kmsg_queue_t queue) | |
292 | { | |
293 | ipc_kmsg_t first; | |
294 | ||
295 | first = ipc_kmsg_queue_first(queue); | |
296 | ||
297 | if (first != IKM_NULL) | |
298 | ipc_kmsg_rmqueue_first_macro(queue, first); | |
299 | ||
300 | return first; | |
301 | } | |
302 | ||
303 | /* | |
304 | * Routine: ipc_kmsg_rmqueue | |
305 | * Purpose: | |
306 | * Pull a kmsg out of a queue. | |
307 | */ | |
308 | ||
309 | void | |
310 | ipc_kmsg_rmqueue( | |
311 | ipc_kmsg_queue_t queue, | |
312 | ipc_kmsg_t kmsg) | |
313 | { | |
314 | ipc_kmsg_t next, prev; | |
315 | ||
316 | assert(queue->ikmq_base != IKM_NULL); | |
317 | ||
318 | next = kmsg->ikm_next; | |
319 | prev = kmsg->ikm_prev; | |
320 | ||
321 | if (next == kmsg) { | |
322 | assert(prev == kmsg); | |
323 | assert(queue->ikmq_base == kmsg); | |
324 | ||
325 | queue->ikmq_base = IKM_NULL; | |
326 | } else { | |
327 | if (queue->ikmq_base == kmsg) | |
328 | queue->ikmq_base = next; | |
329 | ||
330 | next->ikm_prev = prev; | |
331 | prev->ikm_next = next; | |
332 | } | |
333 | /* XXX Temporary debug logic */ | |
334 | assert(kmsg->ikm_next = IKM_BOGUS); | |
335 | assert(kmsg->ikm_prev = IKM_BOGUS); | |
336 | } | |
337 | ||
338 | /* | |
339 | * Routine: ipc_kmsg_queue_next | |
340 | * Purpose: | |
341 | * Return the kmsg following the given kmsg. | |
342 | * (Or IKM_NULL if it is the last one in the queue.) | |
343 | */ | |
344 | ||
345 | ipc_kmsg_t | |
346 | ipc_kmsg_queue_next( | |
347 | ipc_kmsg_queue_t queue, | |
348 | ipc_kmsg_t kmsg) | |
349 | { | |
350 | ipc_kmsg_t next; | |
351 | ||
352 | assert(queue->ikmq_base != IKM_NULL); | |
353 | ||
354 | next = kmsg->ikm_next; | |
355 | if (queue->ikmq_base == next) | |
356 | next = IKM_NULL; | |
357 | ||
358 | return next; | |
359 | } | |
360 | ||
361 | /* | |
362 | * Routine: ipc_kmsg_destroy | |
363 | * Purpose: | |
364 | * Destroys a kernel message. Releases all rights, | |
365 | * references, and memory held by the message. | |
366 | * Frees the message. | |
367 | * Conditions: | |
368 | * No locks held. | |
369 | */ | |
370 | ||
371 | void | |
372 | ipc_kmsg_destroy( | |
373 | ipc_kmsg_t kmsg) | |
374 | { | |
375 | ipc_kmsg_queue_t queue; | |
376 | boolean_t empty; | |
377 | ||
378 | /* | |
379 | * ipc_kmsg_clean can cause more messages to be destroyed. | |
380 | * Curtail recursion by queueing messages. If a message | |
381 | * is already queued, then this is a recursive call. | |
382 | */ | |
383 | ||
384 | queue = &(current_thread()->ith_messages); | |
385 | empty = ipc_kmsg_queue_empty(queue); | |
386 | ipc_kmsg_enqueue(queue, kmsg); | |
387 | ||
388 | if (empty) { | |
389 | /* must leave kmsg in queue while cleaning it */ | |
390 | ||
391 | while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) { | |
392 | ipc_kmsg_clean(kmsg); | |
393 | ipc_kmsg_rmqueue(queue, kmsg); | |
394 | ipc_kmsg_free(kmsg); | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | /* | |
400 | * Routine: ipc_kmsg_destroy_dest | |
401 | * Purpose: | |
402 | * Destroys a kernel message. Releases all rights, | |
403 | * references, and memory held by the message (including | |
404 | * the destination port reference. | |
405 | * Frees the message. | |
406 | * Conditions: | |
407 | * No locks held. | |
408 | */ | |
409 | ||
410 | ipc_kmsg_destroy_dest( | |
411 | ipc_kmsg_t kmsg) | |
412 | { | |
413 | ipc_port_t port; | |
414 | ||
415 | port = kmsg->ikm_header.msgh_remote_port; | |
416 | ||
417 | ipc_port_release(port); | |
418 | kmsg->ikm_header.msgh_remote_port = MACH_PORT_NULL; | |
419 | ipc_kmsg_destroy(kmsg); | |
420 | } | |
421 | ||
422 | /* | |
423 | * Routine: ipc_kmsg_clean_body | |
424 | * Purpose: | |
425 | * Cleans the body of a kernel message. | |
426 | * Releases all rights, references, and memory. | |
427 | * | |
428 | * Conditions: | |
429 | * No locks held. | |
430 | */ | |
431 | ||
432 | void | |
433 | ipc_kmsg_clean_body( | |
434 | ipc_kmsg_t kmsg, | |
435 | mach_msg_type_number_t number) | |
436 | { | |
437 | mach_msg_descriptor_t *saddr, *eaddr; | |
438 | ||
439 | if ( number == 0 ) | |
440 | return; | |
441 | ||
442 | saddr = (mach_msg_descriptor_t *) | |
443 | ((mach_msg_base_t *) &kmsg->ikm_header + 1); | |
444 | eaddr = saddr + number; | |
445 | ||
446 | for ( ; saddr < eaddr; saddr++ ) { | |
447 | ||
448 | switch (saddr->type.type) { | |
449 | ||
450 | case MACH_MSG_PORT_DESCRIPTOR: { | |
451 | mach_msg_port_descriptor_t *dsc; | |
452 | ||
453 | dsc = &saddr->port; | |
454 | ||
455 | /* | |
456 | * Destroy port rights carried in the message | |
457 | */ | |
458 | if (!IO_VALID((ipc_object_t) dsc->name)) | |
459 | continue; | |
460 | ipc_object_destroy((ipc_object_t) dsc->name, dsc->disposition); | |
461 | break; | |
462 | } | |
463 | case MACH_MSG_OOL_VOLATILE_DESCRIPTOR: | |
464 | case MACH_MSG_OOL_DESCRIPTOR : { | |
465 | mach_msg_ool_descriptor_t *dsc; | |
466 | ||
467 | dsc = &saddr->out_of_line; | |
468 | ||
469 | /* | |
470 | * Destroy memory carried in the message | |
471 | */ | |
472 | if (dsc->size == 0) { | |
473 | assert(dsc->address == (void *) 0); | |
474 | } else { | |
475 | vm_map_copy_discard((vm_map_copy_t) dsc->address); | |
476 | } | |
477 | break; | |
478 | } | |
479 | case MACH_MSG_OOL_PORTS_DESCRIPTOR : { | |
480 | ipc_object_t *objects; | |
481 | mach_msg_type_number_t j; | |
482 | mach_msg_ool_ports_descriptor_t *dsc; | |
483 | ||
484 | dsc = &saddr->ool_ports; | |
485 | objects = (ipc_object_t *) dsc->address; | |
486 | ||
487 | if (dsc->count == 0) { | |
488 | break; | |
489 | } | |
490 | ||
491 | assert(objects != (ipc_object_t *) 0); | |
492 | ||
493 | /* destroy port rights carried in the message */ | |
494 | ||
495 | for (j = 0; j < dsc->count; j++) { | |
496 | ipc_object_t object = objects[j]; | |
497 | ||
498 | if (!IO_VALID(object)) | |
499 | continue; | |
500 | ||
501 | ipc_object_destroy(object, dsc->disposition); | |
502 | } | |
503 | ||
504 | /* destroy memory carried in the message */ | |
505 | ||
506 | assert(dsc->count != 0); | |
507 | ||
508 | kfree((vm_offset_t) dsc->address, | |
509 | (vm_size_t) dsc->count * sizeof(mach_port_name_t)); | |
510 | break; | |
511 | } | |
512 | default : { | |
513 | printf("cleanup: don't understand this type of descriptor\n"); | |
514 | } | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
519 | /* | |
520 | * Routine: ipc_kmsg_clean_partial | |
521 | * Purpose: | |
522 | * Cleans a partially-acquired kernel message. | |
523 | * number is the index of the type descriptor | |
524 | * in the body of the message that contained the error. | |
525 | * If dolast, the memory and port rights in this last | |
526 | * type spec are also cleaned. In that case, number | |
527 | * specifies the number of port rights to clean. | |
528 | * Conditions: | |
529 | * Nothing locked. | |
530 | */ | |
531 | ||
532 | void | |
533 | ipc_kmsg_clean_partial( | |
534 | ipc_kmsg_t kmsg, | |
535 | mach_msg_type_number_t number, | |
536 | vm_offset_t paddr, | |
537 | vm_size_t length) | |
538 | { | |
539 | ipc_object_t object; | |
540 | mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits; | |
541 | ||
542 | object = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
543 | assert(IO_VALID(object)); | |
544 | ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits)); | |
545 | ||
546 | object = (ipc_object_t) kmsg->ikm_header.msgh_local_port; | |
547 | if (IO_VALID(object)) | |
548 | ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits)); | |
549 | ||
550 | if (paddr) { | |
551 | (void) vm_deallocate(ipc_kernel_copy_map, paddr, length); | |
552 | } | |
553 | ||
554 | ipc_kmsg_clean_body(kmsg, number); | |
555 | } | |
556 | ||
557 | /* | |
558 | * Routine: ipc_kmsg_clean | |
559 | * Purpose: | |
560 | * Cleans a kernel message. Releases all rights, | |
561 | * references, and memory held by the message. | |
562 | * Conditions: | |
563 | * No locks held. | |
564 | */ | |
565 | ||
566 | void | |
567 | ipc_kmsg_clean( | |
568 | ipc_kmsg_t kmsg) | |
569 | { | |
570 | ipc_object_t object; | |
571 | mach_msg_bits_t mbits; | |
572 | ||
573 | mbits = kmsg->ikm_header.msgh_bits; | |
574 | object = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
575 | if (IO_VALID(object)) | |
576 | ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits)); | |
577 | ||
578 | object = (ipc_object_t) kmsg->ikm_header.msgh_local_port; | |
579 | if (IO_VALID(object)) | |
580 | ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits)); | |
581 | ||
582 | if (mbits & MACH_MSGH_BITS_COMPLEX) { | |
583 | mach_msg_body_t *body; | |
584 | ||
585 | body = (mach_msg_body_t *) (&kmsg->ikm_header + 1); | |
586 | ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count); | |
587 | } | |
588 | } | |
589 | ||
590 | /* | |
591 | * Routine: ipc_kmsg_set_prealloc | |
592 | * Purpose: | |
593 | * Assign a kmsg as a preallocated message buffer to a port. | |
594 | * Conditions: | |
595 | * port locked. | |
596 | */ | |
597 | ||
598 | void | |
599 | ipc_kmsg_set_prealloc( | |
600 | ipc_kmsg_t kmsg, | |
601 | ipc_port_t port) | |
602 | { | |
603 | assert(kmsg->ikm_prealloc == IP_NULL); | |
604 | ||
605 | kmsg->ikm_prealloc = IP_NULL; | |
606 | IP_SET_PREALLOC(port, kmsg); | |
607 | } | |
608 | ||
609 | /* | |
610 | * Routine: ipc_kmsg_clear_prealloc | |
611 | * Purpose: | |
612 | * Release the Assignment of a preallocated message buffer from a port. | |
613 | * Conditions: | |
614 | * port locked. | |
615 | */ | |
616 | void | |
617 | ipc_kmsg_clear_prealloc( | |
618 | ipc_kmsg_t kmsg, | |
619 | ipc_port_t port) | |
620 | { | |
621 | assert(kmsg->ikm_prealloc == port); | |
622 | ||
623 | kmsg->ikm_prealloc = IP_NULL; | |
624 | IP_CLEAR_PREALLOC(port, kmsg); | |
625 | } | |
626 | ||
627 | /* | |
628 | * Routine: ipc_kmsg_get | |
629 | * Purpose: | |
630 | * Allocates a kernel message buffer. | |
631 | * Copies a user message to the message buffer. | |
632 | * Conditions: | |
633 | * Nothing locked. | |
634 | * Returns: | |
635 | * MACH_MSG_SUCCESS Acquired a message buffer. | |
636 | * MACH_SEND_MSG_TOO_SMALL Message smaller than a header. | |
637 | * MACH_SEND_MSG_TOO_SMALL Message size not long-word multiple. | |
638 | * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer. | |
639 | * MACH_SEND_INVALID_DATA Couldn't copy message data. | |
640 | */ | |
641 | ||
642 | mach_msg_return_t | |
643 | ipc_kmsg_get( | |
644 | mach_msg_header_t *msg, | |
645 | mach_msg_size_t size, | |
646 | ipc_kmsg_t *kmsgp) | |
647 | { | |
648 | mach_msg_size_t msg_and_trailer_size; | |
649 | ipc_kmsg_t kmsg; | |
650 | mach_msg_format_0_trailer_t *trailer; | |
651 | mach_port_name_t dest_name; | |
652 | ipc_entry_t dest_entry; | |
653 | ipc_port_t dest_port; | |
654 | ||
655 | if ((size < sizeof(mach_msg_header_t)) || (size & 3)) | |
656 | return MACH_SEND_MSG_TOO_SMALL; | |
657 | ||
658 | msg_and_trailer_size = size + MAX_TRAILER_SIZE; | |
659 | ||
660 | kmsg = ipc_kmsg_alloc(msg_and_trailer_size); | |
661 | ||
662 | if (kmsg == IKM_NULL) | |
663 | return MACH_SEND_NO_BUFFER; | |
664 | ||
665 | if (copyinmsg((char *) msg, (char *) &kmsg->ikm_header, size)) { | |
666 | ipc_kmsg_free(kmsg); | |
667 | return MACH_SEND_INVALID_DATA; | |
668 | } | |
669 | ||
670 | kmsg->ikm_header.msgh_size = size; | |
671 | ||
672 | /* | |
673 | * I reserve for the trailer the largest space (MAX_TRAILER_SIZE) | |
674 | * However, the internal size field of the trailer (msgh_trailer_size) | |
675 | * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize | |
676 | * the cases where no implicit data is requested. | |
677 | */ | |
678 | trailer = (mach_msg_format_0_trailer_t *) ((vm_offset_t)&kmsg->ikm_header + size); | |
679 | trailer->msgh_sender = current_thread()->top_act->task->sec_token; | |
680 | trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0; | |
681 | trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE; | |
682 | ||
683 | *kmsgp = kmsg; | |
684 | return MACH_MSG_SUCCESS; | |
685 | } | |
686 | ||
687 | /* | |
688 | * Routine: ipc_kmsg_get_from_kernel | |
689 | * Purpose: | |
690 | * Allocates a kernel message buffer. | |
691 | * Copies a kernel message to the message buffer. | |
692 | * Only resource errors are allowed. | |
693 | * Conditions: | |
694 | * Nothing locked. | |
695 | * Ports in header are ipc_port_t. | |
696 | * Returns: | |
697 | * MACH_MSG_SUCCESS Acquired a message buffer. | |
698 | * MACH_SEND_NO_BUFFER Couldn't allocate a message buffer. | |
699 | */ | |
700 | ||
701 | mach_msg_return_t | |
702 | ipc_kmsg_get_from_kernel( | |
703 | mach_msg_header_t *msg, | |
704 | mach_msg_size_t size, | |
705 | ipc_kmsg_t *kmsgp) | |
706 | { | |
707 | ipc_kmsg_t kmsg; | |
708 | mach_msg_size_t msg_and_trailer_size; | |
709 | mach_msg_format_0_trailer_t *trailer; | |
710 | ipc_port_t dest_port; | |
711 | ||
712 | assert(size >= sizeof(mach_msg_header_t)); | |
713 | assert((size & 3) == 0); | |
714 | ||
715 | assert(IP_VALID((ipc_port_t) msg->msgh_remote_port)); | |
716 | dest_port = (ipc_port_t)msg->msgh_remote_port; | |
717 | ||
718 | msg_and_trailer_size = size + MAX_TRAILER_SIZE; | |
719 | ||
720 | /* | |
721 | * See if the port has a pre-allocated kmsg for kernel | |
722 | * clients. These are set up for those kernel clients | |
723 | * which cannot afford to wait. | |
724 | */ | |
725 | if (IP_PREALLOC(dest_port)) { | |
726 | ip_lock(dest_port); | |
727 | if (!ip_active(dest_port)) { | |
728 | ip_unlock(dest_port); | |
729 | return MACH_SEND_NO_BUFFER; | |
730 | } | |
731 | assert(IP_PREALLOC(dest_port)); | |
732 | kmsg = dest_port->ip_premsg; | |
733 | if (msg_and_trailer_size > kmsg->ikm_size) { | |
734 | ip_unlock(dest_port); | |
735 | return MACH_SEND_TOO_LARGE; | |
736 | } | |
737 | if (ikm_prealloc_inuse(kmsg)) { | |
738 | ip_unlock(dest_port); | |
739 | return MACH_SEND_NO_BUFFER; | |
740 | } | |
741 | ikm_prealloc_set_inuse(kmsg, dest_port); | |
742 | ip_unlock(dest_port); | |
743 | } else { | |
744 | kmsg = ipc_kmsg_alloc(msg_and_trailer_size); | |
745 | if (kmsg == IKM_NULL) | |
746 | return MACH_SEND_NO_BUFFER; | |
747 | } | |
748 | ||
749 | (void) memcpy((void *) &kmsg->ikm_header, (const void *) msg, size); | |
750 | ||
751 | kmsg->ikm_header.msgh_size = size; | |
752 | ||
753 | /* | |
754 | * I reserve for the trailer the largest space (MAX_TRAILER_SIZE) | |
755 | * However, the internal size field of the trailer (msgh_trailer_size) | |
756 | * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to | |
757 | * optimize the cases where no implicit data is requested. | |
758 | */ | |
759 | trailer = (mach_msg_format_0_trailer_t *) | |
760 | ((vm_offset_t)&kmsg->ikm_header + size); | |
761 | trailer->msgh_sender = KERNEL_SECURITY_TOKEN; | |
762 | trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0; | |
763 | trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE; | |
764 | ||
765 | *kmsgp = kmsg; | |
766 | return MACH_MSG_SUCCESS; | |
767 | } | |
768 | ||
769 | /* | |
770 | * Routine: ipc_kmsg_send | |
771 | * Purpose: | |
772 | * Send a message. The message holds a reference | |
773 | * for the destination port in the msgh_remote_port field. | |
774 | * | |
775 | * If unsuccessful, the caller still has possession of | |
776 | * the message and must do something with it. If successful, | |
777 | * the message is queued, given to a receiver, destroyed, | |
778 | * or handled directly by the kernel via mach_msg. | |
779 | * Conditions: | |
780 | * Nothing locked. | |
781 | * Returns: | |
782 | * MACH_MSG_SUCCESS The message was accepted. | |
783 | * MACH_SEND_TIMED_OUT Caller still has message. | |
784 | * MACH_SEND_INTERRUPTED Caller still has message. | |
785 | */ | |
786 | mach_msg_return_t | |
787 | ipc_kmsg_send( | |
788 | ipc_kmsg_t kmsg, | |
789 | mach_msg_option_t option, | |
790 | mach_msg_timeout_t timeout) | |
791 | { | |
792 | kern_return_t save_wait_result; | |
793 | ||
794 | ipc_port_t port; | |
795 | port = (ipc_port_t) kmsg->ikm_header.msgh_remote_port; | |
796 | assert(IP_VALID(port)); | |
797 | ||
798 | ip_lock(port); | |
799 | ||
800 | if (port->ip_receiver == ipc_space_kernel) { | |
801 | ||
802 | /* | |
803 | * We can check ip_receiver == ipc_space_kernel | |
804 | * before checking that the port is active because | |
805 | * ipc_port_dealloc_kernel clears ip_receiver | |
806 | * before destroying a kernel port. | |
807 | */ | |
808 | assert(ip_active(port)); | |
809 | port->ip_messages.imq_seqno++; | |
810 | ip_unlock(port); | |
811 | ||
812 | current_task()->messages_sent++; | |
813 | ||
814 | /* | |
815 | * Call the server routine, and get the reply message to send. | |
816 | */ | |
817 | kmsg = ipc_kobject_server(kmsg); | |
818 | if (kmsg == IKM_NULL) | |
819 | return MACH_MSG_SUCCESS; | |
820 | ||
821 | port = (ipc_port_t) kmsg->ikm_header.msgh_remote_port; | |
822 | assert(IP_VALID(port)); | |
823 | ip_lock(port); | |
824 | /* fall thru with reply - same options */ | |
825 | } | |
826 | ||
827 | /* | |
828 | * Can't deliver to a dead port. | |
829 | * However, we can pretend it got sent | |
830 | * and was then immediately destroyed. | |
831 | */ | |
832 | if (!ip_active(port)) { | |
833 | /* | |
834 | * We can't let ipc_kmsg_destroy deallocate | |
835 | * the port right, because we might end up | |
836 | * in an infinite loop trying to deliver | |
837 | * a send-once notification. | |
838 | */ | |
839 | ||
840 | ip_release(port); | |
841 | ip_check_unlock(port); | |
842 | kmsg->ikm_header.msgh_remote_port = MACH_PORT_NULL; | |
843 | ipc_kmsg_destroy(kmsg); | |
844 | return MACH_MSG_SUCCESS; | |
845 | } | |
846 | ||
847 | if (kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_CIRCULAR) { | |
848 | ip_unlock(port); | |
849 | ||
850 | /* don't allow the creation of a circular loop */ | |
851 | ||
852 | ipc_kmsg_destroy(kmsg); | |
853 | return MACH_MSG_SUCCESS; | |
854 | } | |
855 | ||
856 | /* | |
857 | * We have a valid message and a valid reference on the port. | |
858 | * we can unlock the port and call mqueue_send() on it's message | |
859 | * queue. | |
860 | */ | |
861 | ip_unlock(port); | |
862 | return (ipc_mqueue_send(&port->ip_messages, kmsg, option, timeout)); | |
863 | } | |
864 | ||
865 | /* | |
866 | * Routine: ipc_kmsg_put | |
867 | * Purpose: | |
868 | * Copies a message buffer to a user message. | |
869 | * Copies only the specified number of bytes. | |
870 | * Frees the message buffer. | |
871 | * Conditions: | |
872 | * Nothing locked. The message buffer must have clean | |
873 | * header fields. | |
874 | * Returns: | |
875 | * MACH_MSG_SUCCESS Copied data out of message buffer. | |
876 | * MACH_RCV_INVALID_DATA Couldn't copy to user message. | |
877 | */ | |
878 | ||
879 | mach_msg_return_t | |
880 | ipc_kmsg_put( | |
881 | mach_msg_header_t *msg, | |
882 | ipc_kmsg_t kmsg, | |
883 | mach_msg_size_t size) | |
884 | { | |
885 | mach_msg_return_t mr; | |
886 | ||
887 | if (copyoutmsg((const char *) &kmsg->ikm_header, (char *) msg, size)) | |
888 | mr = MACH_RCV_INVALID_DATA; | |
889 | else | |
890 | mr = MACH_MSG_SUCCESS; | |
891 | ||
892 | ipc_kmsg_free(kmsg); | |
893 | return mr; | |
894 | } | |
895 | ||
896 | /* | |
897 | * Routine: ipc_kmsg_put_to_kernel | |
898 | * Purpose: | |
899 | * Copies a message buffer to a kernel message. | |
900 | * Frees the message buffer. | |
901 | * No errors allowed. | |
902 | * Conditions: | |
903 | * Nothing locked. | |
904 | */ | |
905 | ||
906 | void | |
907 | ipc_kmsg_put_to_kernel( | |
908 | mach_msg_header_t *msg, | |
909 | ipc_kmsg_t kmsg, | |
910 | mach_msg_size_t size) | |
911 | { | |
912 | (void) memcpy((void *) msg, (const void *) &kmsg->ikm_header, size); | |
913 | ||
914 | ipc_kmsg_free(kmsg); | |
915 | } | |
916 | ||
917 | /* | |
918 | * Routine: ipc_kmsg_copyin_header | |
919 | * Purpose: | |
920 | * "Copy-in" port rights in the header of a message. | |
921 | * Operates atomically; if it doesn't succeed the | |
922 | * message header and the space are left untouched. | |
923 | * If it does succeed the remote/local port fields | |
924 | * contain object pointers instead of port names, | |
925 | * and the bits field is updated. The destination port | |
926 | * will be a valid port pointer. | |
927 | * | |
928 | * The notify argument implements the MACH_SEND_CANCEL option. | |
929 | * If it is not MACH_PORT_NULL, it should name a receive right. | |
930 | * If the processing of the destination port would generate | |
931 | * a port-deleted notification (because the right for the | |
932 | * destination port is destroyed and it had a request for | |
933 | * a dead-name notification registered), and the port-deleted | |
934 | * notification would be sent to the named receive right, | |
935 | * then it isn't sent and the send-once right for the notify | |
936 | * port is quietly destroyed. | |
937 | * | |
938 | * Conditions: | |
939 | * Nothing locked. | |
940 | * Returns: | |
941 | * MACH_MSG_SUCCESS Successful copyin. | |
942 | * MACH_SEND_INVALID_HEADER | |
943 | * Illegal value in the message header bits. | |
944 | * MACH_SEND_INVALID_DEST The space is dead. | |
945 | * MACH_SEND_INVALID_NOTIFY | |
946 | * Notify is non-null and doesn't name a receive right. | |
947 | * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.) | |
948 | * MACH_SEND_INVALID_DEST Can't copyin destination port. | |
949 | * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.) | |
950 | * MACH_SEND_INVALID_REPLY Can't copyin reply port. | |
951 | * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.) | |
952 | */ | |
953 | ||
954 | mach_msg_return_t | |
955 | ipc_kmsg_copyin_header( | |
956 | mach_msg_header_t *msg, | |
957 | ipc_space_t space, | |
958 | mach_port_name_t notify) | |
959 | { | |
960 | mach_msg_bits_t mbits = msg->msgh_bits & MACH_MSGH_BITS_USER; | |
961 | mach_port_name_t dest_name = (mach_port_name_t)msg->msgh_remote_port; | |
962 | mach_port_name_t reply_name = (mach_port_name_t)msg->msgh_local_port; | |
963 | kern_return_t kr; | |
964 | ||
965 | mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits); | |
966 | mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits); | |
967 | ipc_object_t dest_port, reply_port; | |
968 | ipc_port_t dest_soright, reply_soright; | |
969 | ipc_port_t notify_port; | |
970 | ||
971 | if ((mbits != msg->msgh_bits) || | |
972 | (!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type)) || | |
973 | ((reply_type == 0) ? | |
974 | (reply_name != MACH_PORT_NULL) : | |
975 | !MACH_MSG_TYPE_PORT_ANY_SEND(reply_type))) | |
976 | return MACH_SEND_INVALID_HEADER; | |
977 | ||
978 | reply_soright = IP_NULL; /* in case we go to invalid dest early */ | |
979 | ||
980 | is_write_lock(space); | |
981 | if (!space->is_active) | |
982 | goto invalid_dest; | |
983 | ||
984 | if (!MACH_PORT_VALID(dest_name)) | |
985 | goto invalid_dest; | |
986 | ||
987 | if (notify != MACH_PORT_NULL) { | |
988 | ipc_entry_t entry; | |
989 | ||
990 | if ((entry = ipc_entry_lookup(space, notify)) == IE_NULL) { | |
991 | is_write_unlock(space); | |
992 | return MACH_SEND_INVALID_NOTIFY; | |
993 | } | |
994 | if((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) { | |
995 | is_write_unlock(space); | |
996 | return MACH_SEND_INVALID_NOTIFY; | |
997 | } | |
998 | ||
999 | notify_port = (ipc_port_t) entry->ie_object; | |
1000 | } | |
1001 | ||
1002 | if (dest_name == reply_name) { | |
1003 | ipc_entry_t entry; | |
1004 | mach_port_name_t name = dest_name; | |
1005 | ||
1006 | /* | |
1007 | * Destination and reply ports are the same! | |
1008 | * This is a little tedious to make atomic, because | |
1009 | * there are 25 combinations of dest_type/reply_type. | |
1010 | * However, most are easy. If either is move-sonce, | |
1011 | * then there must be an error. If either are | |
1012 | * make-send or make-sonce, then we must be looking | |
1013 | * at a receive right so the port can't die. | |
1014 | * The hard cases are the combinations of | |
1015 | * copy-send and make-send. | |
1016 | */ | |
1017 | ||
1018 | entry = ipc_entry_lookup(space, name); | |
1019 | if (entry == IE_NULL) | |
1020 | goto invalid_dest; | |
1021 | ||
1022 | assert(reply_type != 0); /* because name not null */ | |
1023 | ||
1024 | if (!ipc_right_copyin_check(space, name, entry, reply_type)) | |
1025 | goto invalid_reply; | |
1026 | ||
1027 | if ((dest_type == MACH_MSG_TYPE_MOVE_SEND_ONCE) || | |
1028 | (reply_type == MACH_MSG_TYPE_MOVE_SEND_ONCE)) { | |
1029 | /* | |
1030 | * Why must there be an error? To get a valid | |
1031 | * destination, this entry must name a live | |
1032 | * port (not a dead name or dead port). However | |
1033 | * a successful move-sonce will destroy a | |
1034 | * live entry. Therefore the other copyin, | |
1035 | * whatever it is, would fail. We've already | |
1036 | * checked for reply port errors above, | |
1037 | * so report a destination error. | |
1038 | */ | |
1039 | ||
1040 | goto invalid_dest; | |
1041 | } else if ((dest_type == MACH_MSG_TYPE_MAKE_SEND) || | |
1042 | (dest_type == MACH_MSG_TYPE_MAKE_SEND_ONCE) || | |
1043 | (reply_type == MACH_MSG_TYPE_MAKE_SEND) || | |
1044 | (reply_type == MACH_MSG_TYPE_MAKE_SEND_ONCE)) { | |
1045 | kr = ipc_right_copyin(space, name, entry, | |
1046 | dest_type, FALSE, | |
1047 | &dest_port, &dest_soright); | |
1048 | if (kr != KERN_SUCCESS) | |
1049 | goto invalid_dest; | |
1050 | ||
1051 | /* | |
1052 | * Either dest or reply needs a receive right. | |
1053 | * We know the receive right is there, because | |
1054 | * of the copyin_check and copyin calls. Hence | |
1055 | * the port is not in danger of dying. If dest | |
1056 | * used the receive right, then the right needed | |
1057 | * by reply (and verified by copyin_check) will | |
1058 | * still be there. | |
1059 | */ | |
1060 | ||
1061 | assert(IO_VALID(dest_port)); | |
1062 | assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE); | |
1063 | assert(dest_soright == IP_NULL); | |
1064 | ||
1065 | kr = ipc_right_copyin(space, name, entry, | |
1066 | reply_type, TRUE, | |
1067 | &reply_port, &reply_soright); | |
1068 | ||
1069 | assert(kr == KERN_SUCCESS); | |
1070 | assert(reply_port == dest_port); | |
1071 | assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE); | |
1072 | assert(reply_soright == IP_NULL); | |
1073 | } else if ((dest_type == MACH_MSG_TYPE_COPY_SEND) && | |
1074 | (reply_type == MACH_MSG_TYPE_COPY_SEND)) { | |
1075 | /* | |
1076 | * To make this atomic, just do one copy-send, | |
1077 | * and dup the send right we get out. | |
1078 | */ | |
1079 | ||
1080 | kr = ipc_right_copyin(space, name, entry, | |
1081 | dest_type, FALSE, | |
1082 | &dest_port, &dest_soright); | |
1083 | if (kr != KERN_SUCCESS) | |
1084 | goto invalid_dest; | |
1085 | ||
1086 | assert(entry->ie_bits & MACH_PORT_TYPE_SEND); | |
1087 | assert(dest_soright == IP_NULL); | |
1088 | ||
1089 | /* | |
1090 | * It's OK if the port we got is dead now, | |
1091 | * so reply_port is IP_DEAD, because the msg | |
1092 | * won't go anywhere anyway. | |
1093 | */ | |
1094 | ||
1095 | reply_port = (ipc_object_t) | |
1096 | ipc_port_copy_send((ipc_port_t) dest_port); | |
1097 | reply_soright = IP_NULL; | |
1098 | } else if ((dest_type == MACH_MSG_TYPE_MOVE_SEND) && | |
1099 | (reply_type == MACH_MSG_TYPE_MOVE_SEND)) { | |
1100 | /* | |
1101 | * This is an easy case. Just use our | |
1102 | * handy-dandy special-purpose copyin call | |
1103 | * to get two send rights for the price of one. | |
1104 | */ | |
1105 | ||
1106 | kr = ipc_right_copyin_two(space, name, entry, | |
1107 | &dest_port, &dest_soright); | |
1108 | if (kr != KERN_SUCCESS) | |
1109 | goto invalid_dest; | |
1110 | ||
1111 | /* the entry might need to be deallocated */ | |
1112 | if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE) | |
1113 | ipc_entry_dealloc(space, name, entry); | |
1114 | ||
1115 | reply_port = dest_port; | |
1116 | reply_soright = IP_NULL; | |
1117 | } else { | |
1118 | ipc_port_t soright; | |
1119 | ||
1120 | assert(((dest_type == MACH_MSG_TYPE_COPY_SEND) && | |
1121 | (reply_type == MACH_MSG_TYPE_MOVE_SEND)) || | |
1122 | ((dest_type == MACH_MSG_TYPE_MOVE_SEND) && | |
1123 | (reply_type == MACH_MSG_TYPE_COPY_SEND))); | |
1124 | ||
1125 | /* | |
1126 | * To make this atomic, just do a move-send, | |
1127 | * and dup the send right we get out. | |
1128 | */ | |
1129 | ||
1130 | kr = ipc_right_copyin(space, name, entry, | |
1131 | MACH_MSG_TYPE_MOVE_SEND, FALSE, | |
1132 | &dest_port, &soright); | |
1133 | if (kr != KERN_SUCCESS) | |
1134 | goto invalid_dest; | |
1135 | ||
1136 | /* the entry might need to be deallocated */ | |
1137 | ||
1138 | if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE) | |
1139 | ipc_entry_dealloc(space, name, entry); | |
1140 | ||
1141 | /* | |
1142 | * It's OK if the port we got is dead now, | |
1143 | * so reply_port is IP_DEAD, because the msg | |
1144 | * won't go anywhere anyway. | |
1145 | */ | |
1146 | ||
1147 | reply_port = (ipc_object_t) | |
1148 | ipc_port_copy_send((ipc_port_t) dest_port); | |
1149 | ||
1150 | if (dest_type == MACH_MSG_TYPE_MOVE_SEND) { | |
1151 | dest_soright = soright; | |
1152 | reply_soright = IP_NULL; | |
1153 | } else { | |
1154 | dest_soright = IP_NULL; | |
1155 | reply_soright = soright; | |
1156 | } | |
1157 | } | |
1158 | } else if (!MACH_PORT_VALID(reply_name)) { | |
1159 | ipc_entry_t entry; | |
1160 | ||
1161 | /* | |
1162 | * No reply port! This is an easy case | |
1163 | * to make atomic. Just copyin the destination. | |
1164 | */ | |
1165 | ||
1166 | entry = ipc_entry_lookup(space, dest_name); | |
1167 | if (entry == IE_NULL) | |
1168 | goto invalid_dest; | |
1169 | ||
1170 | kr = ipc_right_copyin(space, dest_name, entry, | |
1171 | dest_type, FALSE, | |
1172 | &dest_port, &dest_soright); | |
1173 | if (kr != KERN_SUCCESS) | |
1174 | goto invalid_dest; | |
1175 | ||
1176 | /* the entry might need to be deallocated */ | |
1177 | ||
1178 | if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE) | |
1179 | ipc_entry_dealloc(space, dest_name, entry); | |
1180 | ||
1181 | reply_port = (ipc_object_t) reply_name; | |
1182 | reply_soright = IP_NULL; | |
1183 | } else { | |
1184 | ipc_entry_t dest_entry, reply_entry; | |
1185 | ipc_port_t saved_reply; | |
1186 | ||
1187 | /* | |
1188 | * This is the tough case to make atomic. | |
1189 | * The difficult problem is serializing with port death. | |
1190 | * At the time we copyin dest_port, it must be alive. | |
1191 | * If reply_port is alive when we copyin it, then | |
1192 | * we are OK, because we serialize before the death | |
1193 | * of both ports. Assume reply_port is dead at copyin. | |
1194 | * Then if dest_port dies/died after reply_port died, | |
1195 | * we are OK, because we serialize between the death | |
1196 | * of the two ports. So the bad case is when dest_port | |
1197 | * dies after its copyin, reply_port dies before its | |
1198 | * copyin, and dest_port dies before reply_port. Then | |
1199 | * the copyins operated as if dest_port was alive | |
1200 | * and reply_port was dead, which shouldn't have happened | |
1201 | * because they died in the other order. | |
1202 | * | |
1203 | * Note that it is easy for a user task to tell if | |
1204 | * a copyin happened before or after a port died. | |
1205 | * For example, suppose both dest and reply are | |
1206 | * send-once rights (types are both move-sonce) and | |
1207 | * both rights have dead-name requests registered. | |
1208 | * If a port dies before copyin, a dead-name notification | |
1209 | * is generated and the dead name's urefs are incremented, | |
1210 | * and if the copyin happens first, a port-deleted | |
1211 | * notification is generated. | |
1212 | * | |
1213 | * Note that although the entries are different, | |
1214 | * dest_port and reply_port might still be the same. | |
1215 | * | |
1216 | * JMM - The code to handle this was too expensive and, anyway, | |
1217 | * we intend to separate the dest lookup from the reply copyin | |
1218 | * by a wide margin, so the user will have to learn to deal! | |
1219 | * I will be making the change soon! | |
1220 | */ | |
1221 | ||
1222 | dest_entry = ipc_entry_lookup(space, dest_name); | |
1223 | if (dest_entry == IE_NULL) | |
1224 | goto invalid_dest; | |
1225 | ||
1226 | reply_entry = ipc_entry_lookup(space, reply_name); | |
1227 | if (reply_entry == IE_NULL) | |
1228 | goto invalid_reply; | |
1229 | ||
1230 | assert(dest_entry != reply_entry); /* names are not equal */ | |
1231 | assert(reply_type != 0); /* because reply_name not null */ | |
1232 | ||
1233 | if (!ipc_right_copyin_check(space, reply_name, reply_entry, | |
1234 | reply_type)) | |
1235 | goto invalid_reply; | |
1236 | ||
1237 | kr = ipc_right_copyin(space, dest_name, dest_entry, | |
1238 | dest_type, FALSE, | |
1239 | &dest_port, &dest_soright); | |
1240 | if (kr != KERN_SUCCESS) | |
1241 | goto invalid_dest; | |
1242 | ||
1243 | assert(IO_VALID(dest_port)); | |
1244 | ||
1245 | kr = ipc_right_copyin(space, reply_name, reply_entry, | |
1246 | reply_type, TRUE, | |
1247 | &reply_port, &reply_soright); | |
1248 | ||
1249 | assert(kr == KERN_SUCCESS); | |
1250 | ||
1251 | /* the entries might need to be deallocated */ | |
1252 | ||
1253 | if (IE_BITS_TYPE(reply_entry->ie_bits) == MACH_PORT_TYPE_NONE) | |
1254 | ipc_entry_dealloc(space, reply_name, reply_entry); | |
1255 | ||
1256 | if (IE_BITS_TYPE(dest_entry->ie_bits) == MACH_PORT_TYPE_NONE) | |
1257 | ipc_entry_dealloc(space, dest_name, dest_entry); | |
1258 | } | |
1259 | ||
1260 | /* | |
1261 | * At this point, dest_port, reply_port, | |
1262 | * dest_soright, reply_soright are all initialized. | |
1263 | * Any defunct entries have been deallocated. | |
1264 | * The space is still write-locked, and we need to | |
1265 | * make the MACH_SEND_CANCEL check. The notify_port pointer | |
1266 | * is still usable, because the copyin code above won't ever | |
1267 | * deallocate a receive right, so its entry still exists | |
1268 | * and holds a ref. Note notify_port might even equal | |
1269 | * dest_port or reply_port. | |
1270 | */ | |
1271 | ||
1272 | if ((notify != MACH_PORT_NULL) && | |
1273 | (dest_soright == notify_port)) { | |
1274 | ipc_port_release_sonce(dest_soright); | |
1275 | dest_soright = IP_NULL; | |
1276 | } | |
1277 | ||
1278 | is_write_unlock(space); | |
1279 | ||
1280 | if (dest_soright != IP_NULL) | |
1281 | ipc_notify_port_deleted(dest_soright, dest_name); | |
1282 | ||
1283 | if (reply_soright != IP_NULL) | |
1284 | ipc_notify_port_deleted(reply_soright, reply_name); | |
1285 | ||
1286 | dest_type = ipc_object_copyin_type(dest_type); | |
1287 | reply_type = ipc_object_copyin_type(reply_type); | |
1288 | ||
1289 | msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) | | |
1290 | MACH_MSGH_BITS(dest_type, reply_type)); | |
1291 | msg->msgh_remote_port = (ipc_port_t)dest_port; | |
1292 | msg->msgh_local_port = (ipc_port_t)reply_port; | |
1293 | ||
1294 | return MACH_MSG_SUCCESS; | |
1295 | ||
1296 | invalid_reply: | |
1297 | is_write_unlock(space); | |
1298 | return MACH_SEND_INVALID_REPLY; | |
1299 | ||
1300 | invalid_dest: | |
1301 | is_write_unlock(space); | |
1302 | if (reply_soright != IP_NULL) | |
1303 | ipc_notify_port_deleted(reply_soright, reply_name); | |
1304 | return MACH_SEND_INVALID_DEST; | |
1305 | } | |
1306 | ||
1307 | /* | |
1308 | * Routine: ipc_kmsg_copyin_body | |
1309 | * Purpose: | |
1310 | * "Copy-in" port rights and out-of-line memory | |
1311 | * in the message body. | |
1312 | * | |
1313 | * In all failure cases, the message is left holding | |
1314 | * no rights or memory. However, the message buffer | |
1315 | * is not deallocated. If successful, the message | |
1316 | * contains a valid destination port. | |
1317 | * Conditions: | |
1318 | * Nothing locked. | |
1319 | * Returns: | |
1320 | * MACH_MSG_SUCCESS Successful copyin. | |
1321 | * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory. | |
1322 | * MACH_SEND_INVALID_RIGHT Can't copyin port right in body. | |
1323 | * MACH_SEND_INVALID_TYPE Bad type specification. | |
1324 | * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data. | |
1325 | * MACH_SEND_INVALID_RT_OOL_SIZE OOL Buffer too large for RT | |
1326 | * MACH_MSG_INVALID_RT_DESCRIPTOR Dealloc and RT are incompatible | |
1327 | */ | |
1328 | ||
1329 | mach_msg_return_t | |
1330 | ipc_kmsg_copyin_body( | |
1331 | ipc_kmsg_t kmsg, | |
1332 | ipc_space_t space, | |
1333 | vm_map_t map) | |
1334 | { | |
1335 | ipc_object_t dest; | |
1336 | mach_msg_body_t *body; | |
1337 | mach_msg_descriptor_t *saddr, *eaddr; | |
1338 | boolean_t complex; | |
1339 | mach_msg_return_t mr; | |
1c79356b A |
1340 | int i; |
1341 | kern_return_t kr; | |
1342 | vm_size_t space_needed = 0; | |
1343 | vm_offset_t paddr = 0; | |
1344 | mach_msg_descriptor_t *sstart; | |
1345 | vm_map_copy_t copy = VM_MAP_COPY_NULL; | |
1346 | ||
1347 | /* | |
1348 | * Determine if the target is a kernel port. | |
1349 | */ | |
1350 | dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
1351 | complex = FALSE; | |
1c79356b A |
1352 | |
1353 | body = (mach_msg_body_t *) (&kmsg->ikm_header + 1); | |
1354 | saddr = (mach_msg_descriptor_t *) (body + 1); | |
1355 | eaddr = saddr + body->msgh_descriptor_count; | |
1356 | ||
1357 | /* make sure the message does not ask for more msg descriptors | |
1358 | * than the message can hold. | |
1359 | */ | |
1360 | ||
1361 | if (eaddr <= saddr || | |
1362 | eaddr > (mach_msg_descriptor_t *) (&kmsg->ikm_header + | |
1363 | kmsg->ikm_header.msgh_size)) { | |
1364 | ipc_kmsg_clean_partial(kmsg,0,0,0); | |
1365 | return MACH_SEND_MSG_TOO_SMALL; | |
1366 | } | |
1367 | ||
1368 | /* | |
1369 | * Make an initial pass to determine kernal VM space requirements for | |
1370 | * physical copies. | |
1371 | */ | |
1372 | for (sstart = saddr; sstart < eaddr; sstart++) { | |
1373 | ||
1374 | if (sstart->type.type == MACH_MSG_OOL_DESCRIPTOR || | |
1375 | sstart->type.type == MACH_MSG_OOL_VOLATILE_DESCRIPTOR) { | |
1376 | ||
1c79356b A |
1377 | if (sstart->out_of_line.copy != MACH_MSG_PHYSICAL_COPY && |
1378 | sstart->out_of_line.copy != MACH_MSG_VIRTUAL_COPY) { | |
1379 | /* | |
1380 | * Invalid copy option | |
1381 | */ | |
1382 | ipc_kmsg_clean_partial(kmsg,0,0,0); | |
1383 | return MACH_SEND_INVALID_TYPE; | |
1384 | } | |
1385 | ||
1386 | if ((sstart->out_of_line.size >= MSG_OOL_SIZE_SMALL) && | |
1387 | (sstart->out_of_line.copy == MACH_MSG_PHYSICAL_COPY) && | |
1388 | !(sstart->out_of_line.deallocate)) { | |
1389 | ||
1390 | /* | |
1391 | * Out-of-line memory descriptor, accumulate kernel | |
1392 | * memory requirements | |
1393 | */ | |
1394 | space_needed += round_page(sstart->out_of_line.size); | |
1395 | if (space_needed > ipc_kmsg_max_vm_space) { | |
1396 | ||
1397 | /* | |
1398 | * Per message kernel memory limit exceeded | |
1399 | */ | |
1400 | ipc_kmsg_clean_partial(kmsg,0,0,0); | |
1401 | return MACH_MSG_VM_KERNEL; | |
1402 | } | |
1403 | } | |
1404 | } | |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * Allocate space in the pageable kernel ipc copy map for all the | |
1409 | * ool data that is to be physically copied. Map is marked wait for | |
1410 | * space. | |
1411 | */ | |
1412 | if (space_needed) { | |
1413 | if (vm_allocate(ipc_kernel_copy_map, &paddr, space_needed, TRUE) != | |
1414 | KERN_SUCCESS) { | |
1415 | ipc_kmsg_clean_partial(kmsg,0,0,0); | |
1416 | return MACH_MSG_VM_KERNEL; | |
1417 | } | |
1418 | } | |
1419 | ||
1420 | /* | |
1421 | * handle the OOL regions and port descriptors. | |
1422 | * the check for complex messages was done earlier. | |
1423 | */ | |
1424 | ||
1425 | for (i = 0, sstart = saddr; sstart < eaddr; sstart++) { | |
1426 | ||
1427 | switch (sstart->type.type) { | |
1428 | ||
1429 | case MACH_MSG_PORT_DESCRIPTOR: { | |
1430 | mach_msg_type_name_t name; | |
1431 | ipc_object_t object; | |
1432 | mach_msg_port_descriptor_t *dsc; | |
1433 | ||
1434 | dsc = &sstart->port; | |
1435 | ||
1436 | /* this is really the type SEND, SEND_ONCE, etc. */ | |
1437 | name = dsc->disposition; | |
1438 | dsc->disposition = ipc_object_copyin_type(name); | |
1439 | ||
1440 | if (!MACH_PORT_VALID((mach_port_name_t)dsc->name)) { | |
1441 | complex = TRUE; | |
1442 | break; | |
1443 | } | |
1444 | kr = ipc_object_copyin(space, (mach_port_name_t)dsc->name, name, &object); | |
1445 | if (kr != KERN_SUCCESS) { | |
1446 | ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed); | |
1447 | return MACH_SEND_INVALID_RIGHT; | |
1448 | } | |
1449 | if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) && | |
1450 | ipc_port_check_circularity((ipc_port_t) object, | |
1451 | (ipc_port_t) dest)) { | |
1452 | kmsg->ikm_header.msgh_bits |= MACH_MSGH_BITS_CIRCULAR; | |
1453 | } | |
1454 | dsc->name = (ipc_port_t) object; | |
1455 | complex = TRUE; | |
1456 | break; | |
1457 | } | |
1458 | case MACH_MSG_OOL_VOLATILE_DESCRIPTOR: | |
1459 | case MACH_MSG_OOL_DESCRIPTOR: { | |
1460 | vm_size_t length; | |
1461 | boolean_t dealloc; | |
1462 | vm_offset_t addr; | |
1463 | vm_offset_t kaddr; | |
1464 | mach_msg_ool_descriptor_t *dsc; | |
1465 | ||
1466 | dsc = &sstart->out_of_line; | |
1467 | dealloc = dsc->deallocate; | |
1468 | addr = (vm_offset_t) dsc->address; | |
1469 | ||
1470 | length = dsc->size; | |
1471 | ||
1472 | if (length == 0) { | |
1473 | dsc->address = 0; | |
1c79356b A |
1474 | } else if ((length >= MSG_OOL_SIZE_SMALL) && |
1475 | (dsc->copy == MACH_MSG_PHYSICAL_COPY) && !dealloc) { | |
1476 | ||
1477 | /* | |
1478 | * If the request is a physical copy and the source | |
1479 | * is not being deallocated, then allocate space | |
1480 | * in the kernel's pageable ipc copy map and copy | |
1481 | * the data in. The semantics guarantee that the | |
1482 | * data will have been physically copied before | |
1483 | * the send operation terminates. Thus if the data | |
1484 | * is not being deallocated, we must be prepared | |
1485 | * to page if the region is sufficiently large. | |
1486 | */ | |
1487 | if (copyin((const char *) addr, (char *) paddr, | |
1488 | length)) { | |
1489 | ipc_kmsg_clean_partial(kmsg, i, paddr, | |
1490 | space_needed); | |
1491 | return MACH_SEND_INVALID_MEMORY; | |
1492 | } | |
1493 | ||
1494 | /* | |
1495 | * The kernel ipc copy map is marked no_zero_fill. | |
1496 | * If the transfer is not a page multiple, we need | |
1497 | * to zero fill the balance. | |
1498 | */ | |
1499 | if (!page_aligned(length)) { | |
1500 | (void) memset((void *) (paddr + length), 0, | |
1501 | round_page(length) - length); | |
1502 | } | |
1503 | if (vm_map_copyin(ipc_kernel_copy_map, paddr, length, | |
1504 | TRUE, ©) != KERN_SUCCESS) { | |
1505 | ipc_kmsg_clean_partial(kmsg, i, paddr, | |
1506 | space_needed); | |
1507 | return MACH_MSG_VM_KERNEL; | |
1508 | } | |
1509 | dsc->address = (void *) copy; | |
1510 | paddr += round_page(length); | |
1511 | space_needed -= round_page(length); | |
1512 | } else { | |
1513 | ||
1514 | /* | |
1515 | * Make a vm_map_copy_t of the of the data. If the | |
1516 | * data is small, this will do an optimized physical | |
1517 | * copy. Otherwise, it will do a virtual copy. | |
1518 | * | |
1519 | * NOTE: A virtual copy is OK if the original is being | |
1520 | * deallocted, even if a physical copy was requested. | |
1521 | */ | |
1522 | kr = vm_map_copyin(map, addr, length, dealloc, ©); | |
1523 | if (kr != KERN_SUCCESS) { | |
1524 | ipc_kmsg_clean_partial(kmsg,i,paddr,space_needed); | |
1525 | return (kr == KERN_RESOURCE_SHORTAGE) ? | |
1526 | MACH_MSG_VM_KERNEL : | |
1527 | MACH_SEND_INVALID_MEMORY; | |
1528 | } | |
1529 | dsc->address = (void *) copy; | |
1530 | } | |
1531 | complex = TRUE; | |
1532 | break; | |
1533 | } | |
1534 | case MACH_MSG_OOL_PORTS_DESCRIPTOR: { | |
1535 | vm_size_t length; | |
1536 | vm_offset_t data; | |
1537 | vm_offset_t addr; | |
1538 | ipc_object_t *objects; | |
1539 | int j; | |
1540 | mach_msg_type_name_t name; | |
1541 | mach_msg_ool_ports_descriptor_t *dsc; | |
1542 | ||
1543 | dsc = &sstart->ool_ports; | |
1544 | addr = (vm_offset_t) dsc->address; | |
1545 | ||
1546 | /* calculate length of data in bytes, rounding up */ | |
1547 | length = dsc->count * sizeof(mach_port_name_t); | |
1548 | ||
1549 | if (length == 0) { | |
1550 | complex = TRUE; | |
1551 | dsc->address = (void *) 0; | |
1552 | break; | |
1553 | } | |
1554 | ||
1555 | data = kalloc(length); | |
1556 | ||
1557 | if (data == 0) { | |
1558 | ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed); | |
1559 | return MACH_SEND_NO_BUFFER; | |
1560 | } | |
1561 | ||
1562 | if (copyinmap(map, addr, data, length)) { | |
1563 | kfree(data, length); | |
1564 | ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed); | |
1565 | return MACH_SEND_INVALID_MEMORY; | |
1566 | } | |
1567 | ||
1568 | if (dsc->deallocate) { | |
1569 | (void) vm_deallocate(map, addr, length); | |
1570 | } | |
1571 | ||
1572 | dsc->address = (void *) data; | |
1573 | ||
1574 | /* this is really the type SEND, SEND_ONCE, etc. */ | |
1575 | name = dsc->disposition; | |
1576 | dsc->disposition = ipc_object_copyin_type(name); | |
1577 | ||
1578 | objects = (ipc_object_t *) data; | |
1579 | ||
1580 | for ( j = 0; j < dsc->count; j++) { | |
1581 | mach_port_name_t port = (mach_port_name_t) objects[j]; | |
1582 | ipc_object_t object; | |
1583 | ||
1584 | if (!MACH_PORT_VALID(port)) | |
1585 | continue; | |
1586 | ||
1587 | kr = ipc_object_copyin(space, port, name, &object); | |
1588 | ||
1589 | if (kr != KERN_SUCCESS) { | |
1590 | int k; | |
1591 | ||
1592 | for(k = 0; k < j; k++) { | |
1593 | object = objects[k]; | |
1594 | if (!MACH_PORT_VALID(port)) | |
1595 | continue; | |
1596 | ipc_object_destroy(object, dsc->disposition); | |
1597 | } | |
1598 | kfree(data, length); | |
1599 | ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed); | |
1600 | return MACH_SEND_INVALID_RIGHT; | |
1601 | } | |
1602 | ||
1603 | if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) && | |
1604 | ipc_port_check_circularity( | |
1605 | (ipc_port_t) object, | |
1606 | (ipc_port_t) dest)) | |
1607 | kmsg->ikm_header.msgh_bits |= MACH_MSGH_BITS_CIRCULAR; | |
1608 | ||
1609 | objects[j] = object; | |
1610 | } | |
1611 | ||
1612 | complex = TRUE; | |
1613 | break; | |
1614 | } | |
1615 | default: { | |
1616 | /* | |
1617 | * Invalid descriptor | |
1618 | */ | |
1619 | ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed); | |
1620 | return MACH_SEND_INVALID_TYPE; | |
1621 | } | |
1622 | } | |
1623 | i++ ; | |
1624 | } | |
1625 | ||
1626 | if (!complex) | |
1627 | kmsg->ikm_header.msgh_bits &= ~MACH_MSGH_BITS_COMPLEX; | |
1628 | return MACH_MSG_SUCCESS; | |
1629 | } | |
1630 | ||
1631 | ||
1632 | /* | |
1633 | * Routine: ipc_kmsg_copyin | |
1634 | * Purpose: | |
1635 | * "Copy-in" port rights and out-of-line memory | |
1636 | * in the message. | |
1637 | * | |
1638 | * In all failure cases, the message is left holding | |
1639 | * no rights or memory. However, the message buffer | |
1640 | * is not deallocated. If successful, the message | |
1641 | * contains a valid destination port. | |
1642 | * Conditions: | |
1643 | * Nothing locked. | |
1644 | * Returns: | |
1645 | * MACH_MSG_SUCCESS Successful copyin. | |
1646 | * MACH_SEND_INVALID_HEADER | |
1647 | * Illegal value in the message header bits. | |
1648 | * MACH_SEND_INVALID_NOTIFY Bad notify port. | |
1649 | * MACH_SEND_INVALID_DEST Can't copyin destination port. | |
1650 | * MACH_SEND_INVALID_REPLY Can't copyin reply port. | |
1651 | * MACH_SEND_INVALID_MEMORY Can't grab out-of-line memory. | |
1652 | * MACH_SEND_INVALID_RIGHT Can't copyin port right in body. | |
1653 | * MACH_SEND_INVALID_TYPE Bad type specification. | |
1654 | * MACH_SEND_MSG_TOO_SMALL Body is too small for types/data. | |
1655 | */ | |
1656 | ||
1657 | mach_msg_return_t | |
1658 | ipc_kmsg_copyin( | |
1659 | ipc_kmsg_t kmsg, | |
1660 | ipc_space_t space, | |
1661 | vm_map_t map, | |
1662 | mach_port_name_t notify) | |
1663 | { | |
1664 | mach_msg_return_t mr; | |
1665 | ||
1666 | mr = ipc_kmsg_copyin_header(&kmsg->ikm_header, space, notify); | |
1667 | if (mr != MACH_MSG_SUCCESS) | |
1668 | return mr; | |
1669 | ||
1670 | if ((kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) == 0) | |
1671 | return MACH_MSG_SUCCESS; | |
1672 | ||
1673 | return( ipc_kmsg_copyin_body( kmsg, space, map) ); | |
1674 | } | |
1675 | ||
1676 | /* | |
1677 | * Routine: ipc_kmsg_copyin_from_kernel | |
1678 | * Purpose: | |
1679 | * "Copy-in" port rights and out-of-line memory | |
1680 | * in a message sent from the kernel. | |
1681 | * | |
1682 | * Because the message comes from the kernel, | |
1683 | * the implementation assumes there are no errors | |
1684 | * or peculiarities in the message. | |
1685 | * | |
1686 | * Returns TRUE if queueing the message | |
1687 | * would result in a circularity. | |
1688 | * Conditions: | |
1689 | * Nothing locked. | |
1690 | */ | |
1691 | ||
1692 | void | |
1693 | ipc_kmsg_copyin_from_kernel( | |
1694 | ipc_kmsg_t kmsg) | |
1695 | { | |
1696 | mach_msg_bits_t bits = kmsg->ikm_header.msgh_bits; | |
1697 | mach_msg_type_name_t rname = MACH_MSGH_BITS_REMOTE(bits); | |
1698 | mach_msg_type_name_t lname = MACH_MSGH_BITS_LOCAL(bits); | |
1699 | ipc_object_t remote = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
1700 | ipc_object_t local = (ipc_object_t) kmsg->ikm_header.msgh_local_port; | |
1701 | ||
1702 | /* translate the destination and reply ports */ | |
1703 | ||
1704 | ipc_object_copyin_from_kernel(remote, rname); | |
1705 | if (IO_VALID(local)) | |
1706 | ipc_object_copyin_from_kernel(local, lname); | |
1707 | ||
1708 | /* | |
1709 | * The common case is a complex message with no reply port, | |
1710 | * because that is what the memory_object interface uses. | |
1711 | */ | |
1712 | ||
1713 | if (bits == (MACH_MSGH_BITS_COMPLEX | | |
1714 | MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0))) { | |
1715 | bits = (MACH_MSGH_BITS_COMPLEX | | |
1716 | MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0)); | |
1717 | ||
1718 | kmsg->ikm_header.msgh_bits = bits; | |
1719 | } else { | |
1720 | bits = (MACH_MSGH_BITS_OTHER(bits) | | |
1721 | MACH_MSGH_BITS(ipc_object_copyin_type(rname), | |
1722 | ipc_object_copyin_type(lname))); | |
1723 | ||
1724 | kmsg->ikm_header.msgh_bits = bits; | |
1725 | if ((bits & MACH_MSGH_BITS_COMPLEX) == 0) | |
1726 | return; | |
1727 | } | |
1728 | { | |
1729 | mach_msg_descriptor_t *saddr, *eaddr; | |
1730 | mach_msg_body_t *body; | |
1731 | ||
1732 | body = (mach_msg_body_t *) (&kmsg->ikm_header + 1); | |
1733 | saddr = (mach_msg_descriptor_t *) (body + 1); | |
1734 | eaddr = (mach_msg_descriptor_t *) saddr + body->msgh_descriptor_count; | |
1735 | ||
1736 | for ( ; saddr < eaddr; saddr++) { | |
1737 | ||
1738 | switch (saddr->type.type) { | |
1739 | ||
1740 | case MACH_MSG_PORT_DESCRIPTOR: { | |
1741 | mach_msg_type_name_t name; | |
1742 | ipc_object_t object; | |
1743 | mach_msg_port_descriptor_t *dsc; | |
1744 | ||
1745 | dsc = &saddr->port; | |
1746 | ||
1747 | /* this is really the type SEND, SEND_ONCE, etc. */ | |
1748 | name = dsc->disposition; | |
1749 | object = (ipc_object_t) dsc->name; | |
1750 | dsc->disposition = ipc_object_copyin_type(name); | |
1751 | ||
1752 | if (!IO_VALID(object)) { | |
1753 | break; | |
1754 | } | |
1755 | ||
1756 | ipc_object_copyin_from_kernel(object, name); | |
1757 | ||
1758 | /* CDY avoid circularity when the destination is also */ | |
1759 | /* the kernel. This check should be changed into an */ | |
1760 | /* assert when the new kobject model is in place since*/ | |
1761 | /* ports will not be used in kernel to kernel chats */ | |
1762 | ||
1763 | if (((ipc_port_t)remote)->ip_receiver != ipc_space_kernel) { | |
1764 | if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) && | |
1765 | ipc_port_check_circularity((ipc_port_t) object, | |
1766 | (ipc_port_t) remote)) { | |
1767 | kmsg->ikm_header.msgh_bits |= | |
1768 | MACH_MSGH_BITS_CIRCULAR; | |
1769 | } | |
1770 | } | |
1771 | break; | |
1772 | } | |
1773 | case MACH_MSG_OOL_VOLATILE_DESCRIPTOR: | |
1774 | case MACH_MSG_OOL_DESCRIPTOR: { | |
1775 | /* | |
1776 | * The sender should supply ready-made memory, i.e. | |
1777 | * a vm_map_copy_t, so we don't need to do anything. | |
1778 | */ | |
1779 | break; | |
1780 | } | |
1781 | case MACH_MSG_OOL_PORTS_DESCRIPTOR: { | |
1782 | ipc_object_t *objects; | |
1783 | int j; | |
1784 | mach_msg_type_name_t name; | |
1785 | mach_msg_ool_ports_descriptor_t *dsc; | |
1786 | ||
1787 | dsc = &saddr->ool_ports; | |
1788 | ||
1789 | /* this is really the type SEND, SEND_ONCE, etc. */ | |
1790 | name = dsc->disposition; | |
1791 | dsc->disposition = ipc_object_copyin_type(name); | |
1792 | ||
1793 | objects = (ipc_object_t *) dsc->address; | |
1794 | ||
1795 | for ( j = 0; j < dsc->count; j++) { | |
1796 | ipc_object_t object = objects[j]; | |
1797 | ||
1798 | if (!IO_VALID(object)) | |
1799 | continue; | |
1800 | ||
1801 | ipc_object_copyin_from_kernel(object, name); | |
1802 | ||
1803 | if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) && | |
1804 | ipc_port_check_circularity( | |
1805 | (ipc_port_t) object, | |
1806 | (ipc_port_t) remote)) | |
1807 | kmsg->ikm_header.msgh_bits |= MACH_MSGH_BITS_CIRCULAR; | |
1808 | } | |
1809 | break; | |
1810 | } | |
1811 | default: { | |
1812 | #if MACH_ASSERT | |
1813 | panic("ipc_kmsg_copyin_from_kernel: bad descriptor"); | |
1814 | #endif /* MACH_ASSERT */ | |
1815 | } | |
1816 | } | |
1817 | } | |
1818 | } | |
1819 | } | |
1820 | ||
1821 | /* | |
1822 | * Routine: ipc_kmsg_copyout_header | |
1823 | * Purpose: | |
1824 | * "Copy-out" port rights in the header of a message. | |
1825 | * Operates atomically; if it doesn't succeed the | |
1826 | * message header and the space are left untouched. | |
1827 | * If it does succeed the remote/local port fields | |
1828 | * contain port names instead of object pointers, | |
1829 | * and the bits field is updated. | |
1830 | * | |
1831 | * The notify argument implements the MACH_RCV_NOTIFY option. | |
1832 | * If it is not MACH_PORT_NULL, it should name a receive right. | |
1833 | * If the process of receiving the reply port creates a | |
1834 | * new right in the receiving task, then the new right is | |
1835 | * automatically registered for a dead-name notification, | |
1836 | * with the notify port supplying the send-once right. | |
1837 | * Conditions: | |
1838 | * Nothing locked. | |
1839 | * Returns: | |
1840 | * MACH_MSG_SUCCESS Copied out port rights. | |
1841 | * MACH_RCV_INVALID_NOTIFY | |
1842 | * Notify is non-null and doesn't name a receive right. | |
1843 | * (Either KERN_INVALID_NAME or KERN_INVALID_RIGHT.) | |
1844 | * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE | |
1845 | * The space is dead. | |
1846 | * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE | |
1847 | * No room in space for another name. | |
1848 | * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL | |
1849 | * Couldn't allocate memory for the reply port. | |
1850 | * MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_KERNEL | |
1851 | * Couldn't allocate memory for the dead-name request. | |
1852 | */ | |
1853 | ||
1854 | mach_msg_return_t | |
1855 | ipc_kmsg_copyout_header( | |
1856 | mach_msg_header_t *msg, | |
1857 | ipc_space_t space, | |
1858 | mach_port_name_t notify) | |
1859 | { | |
1860 | mach_msg_bits_t mbits = msg->msgh_bits; | |
1861 | ipc_port_t dest = (ipc_port_t) msg->msgh_remote_port; | |
1862 | ||
1863 | assert(IP_VALID(dest)); | |
1864 | ||
1865 | { | |
1866 | mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits); | |
1867 | mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits); | |
1868 | ipc_port_t reply = (ipc_port_t) msg->msgh_local_port; | |
1869 | mach_port_name_t dest_name, reply_name; | |
1870 | ||
1871 | if (IP_VALID(reply)) { | |
1872 | ipc_port_t notify_port; | |
1873 | ipc_entry_t entry; | |
1874 | kern_return_t kr; | |
1875 | ||
1876 | /* | |
1877 | * Handling notify (for MACH_RCV_NOTIFY) is tricky. | |
1878 | * The problem is atomically making a send-once right | |
1879 | * from the notify port and installing it for a | |
1880 | * dead-name request in the new entry, because this | |
1881 | * requires two port locks (on the notify port and | |
1882 | * the reply port). However, we can safely make | |
1883 | * and consume send-once rights for the notify port | |
1884 | * as long as we hold the space locked. This isn't | |
1885 | * an atomicity problem, because the only way | |
1886 | * to detect that a send-once right has been created | |
1887 | * and then consumed if it wasn't needed is by getting | |
1888 | * at the receive right to look at ip_sorights, and | |
1889 | * because the space is write-locked status calls can't | |
1890 | * lookup the notify port receive right. When we make | |
1891 | * the send-once right, we lock the notify port, | |
1892 | * so any status calls in progress will be done. | |
1893 | */ | |
1894 | ||
1895 | is_write_lock(space); | |
1896 | ||
1897 | for (;;) { | |
1898 | ipc_port_request_index_t request; | |
1899 | ||
1900 | if (!space->is_active) { | |
1901 | is_write_unlock(space); | |
1902 | return (MACH_RCV_HEADER_ERROR| | |
1903 | MACH_MSG_IPC_SPACE); | |
1904 | } | |
1905 | ||
1906 | if (notify != MACH_PORT_NULL) { | |
1907 | notify_port = ipc_port_lookup_notify(space, | |
1908 | notify); | |
1909 | if (notify_port == IP_NULL) { | |
1910 | is_write_unlock(space); | |
1911 | return MACH_RCV_INVALID_NOTIFY; | |
1912 | } | |
1913 | } else | |
1914 | notify_port = IP_NULL; | |
1915 | ||
1916 | if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) && | |
1917 | ipc_right_reverse(space, (ipc_object_t) reply, | |
1918 | &reply_name, &entry)) { | |
1919 | /* reply port is locked and active */ | |
1920 | ||
1921 | /* | |
1922 | * We don't need the notify_port | |
1923 | * send-once right, but we can't release | |
1924 | * it here because reply port is locked. | |
1925 | * Wait until after the copyout to | |
1926 | * release the notify port right. | |
1927 | */ | |
1928 | ||
1929 | assert(entry->ie_bits & | |
1930 | MACH_PORT_TYPE_SEND_RECEIVE); | |
1931 | break; | |
1932 | } | |
1933 | ||
1934 | ip_lock(reply); | |
1935 | if (!ip_active(reply)) { | |
1936 | ip_release(reply); | |
1937 | ip_check_unlock(reply); | |
1938 | ||
1939 | if (notify_port != IP_NULL) | |
1940 | ipc_port_release_sonce(notify_port); | |
1941 | ||
1942 | ip_lock(dest); | |
1943 | is_write_unlock(space); | |
1944 | ||
1945 | reply = IP_DEAD; | |
1946 | reply_name = MACH_PORT_DEAD; | |
1947 | goto copyout_dest; | |
1948 | } | |
1949 | ||
1950 | reply_name = (mach_port_name_t)reply; | |
1951 | kr = ipc_entry_get(space, &reply_name, &entry); | |
1952 | if (kr != KERN_SUCCESS) { | |
1953 | ip_unlock(reply); | |
1954 | ||
1955 | if (notify_port != IP_NULL) | |
1956 | ipc_port_release_sonce(notify_port); | |
1957 | ||
1958 | /* space is locked */ | |
1959 | kr = ipc_entry_grow_table(space, | |
1960 | ITS_SIZE_NONE); | |
1961 | if (kr != KERN_SUCCESS) { | |
1962 | /* space is unlocked */ | |
1963 | ||
1964 | if (kr == KERN_RESOURCE_SHORTAGE) | |
1965 | return (MACH_RCV_HEADER_ERROR| | |
1966 | MACH_MSG_IPC_KERNEL); | |
1967 | else | |
1968 | return (MACH_RCV_HEADER_ERROR| | |
1969 | MACH_MSG_IPC_SPACE); | |
1970 | } | |
1971 | /* space is locked again; start over */ | |
1972 | ||
1973 | continue; | |
1974 | } | |
1975 | assert(IE_BITS_TYPE(entry->ie_bits) == | |
1976 | MACH_PORT_TYPE_NONE); | |
1977 | assert(entry->ie_object == IO_NULL); | |
1978 | ||
1979 | if (notify_port == IP_NULL) { | |
1980 | /* not making a dead-name request */ | |
1981 | ||
1982 | entry->ie_object = (ipc_object_t) reply; | |
1983 | break; | |
1984 | } | |
1985 | ||
1986 | kr = ipc_port_dnrequest(reply, reply_name, | |
1987 | notify_port, &request); | |
1988 | if (kr != KERN_SUCCESS) { | |
1989 | ip_unlock(reply); | |
1990 | ||
1991 | ipc_port_release_sonce(notify_port); | |
1992 | ||
1993 | ipc_entry_dealloc(space, reply_name, entry); | |
1994 | is_write_unlock(space); | |
1995 | ||
1996 | ip_lock(reply); | |
1997 | if (!ip_active(reply)) { | |
1998 | /* will fail next time around loop */ | |
1999 | ||
2000 | ip_unlock(reply); | |
2001 | is_write_lock(space); | |
2002 | continue; | |
2003 | } | |
2004 | ||
2005 | kr = ipc_port_dngrow(reply, ITS_SIZE_NONE); | |
2006 | /* port is unlocked */ | |
2007 | if (kr != KERN_SUCCESS) | |
2008 | return (MACH_RCV_HEADER_ERROR| | |
2009 | MACH_MSG_IPC_KERNEL); | |
2010 | ||
2011 | is_write_lock(space); | |
2012 | continue; | |
2013 | } | |
2014 | ||
2015 | notify_port = IP_NULL; /* don't release right below */ | |
2016 | ||
2017 | entry->ie_object = (ipc_object_t) reply; | |
2018 | entry->ie_request = request; | |
2019 | break; | |
2020 | } | |
2021 | ||
2022 | /* space and reply port are locked and active */ | |
2023 | ||
2024 | ip_reference(reply); /* hold onto the reply port */ | |
2025 | ||
2026 | kr = ipc_right_copyout(space, reply_name, entry, | |
2027 | reply_type, TRUE, (ipc_object_t) reply); | |
2028 | /* reply port is unlocked */ | |
2029 | assert(kr == KERN_SUCCESS); | |
2030 | ||
2031 | if (notify_port != IP_NULL) | |
2032 | ipc_port_release_sonce(notify_port); | |
2033 | ||
2034 | ip_lock(dest); | |
2035 | is_write_unlock(space); | |
2036 | } else { | |
2037 | /* | |
2038 | * No reply port! This is an easy case. | |
2039 | * We only need to have the space locked | |
2040 | * when checking notify and when locking | |
2041 | * the destination (to ensure atomicity). | |
2042 | */ | |
2043 | ||
2044 | is_read_lock(space); | |
2045 | if (!space->is_active) { | |
2046 | is_read_unlock(space); | |
2047 | return MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE; | |
2048 | } | |
2049 | ||
2050 | if (notify != MACH_PORT_NULL) { | |
2051 | ipc_entry_t entry; | |
2052 | ||
2053 | /* must check notify even though it won't be used */ | |
2054 | ||
2055 | if ((entry = ipc_entry_lookup(space, notify)) == IE_NULL) { | |
2056 | is_read_unlock(space); | |
2057 | return MACH_RCV_INVALID_NOTIFY; | |
2058 | } | |
2059 | ||
2060 | if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) { | |
2061 | is_read_unlock(space); | |
2062 | return MACH_RCV_INVALID_NOTIFY; | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | ip_lock(dest); | |
2067 | is_read_unlock(space); | |
2068 | ||
2069 | reply_name = (mach_port_name_t) reply; | |
2070 | } | |
2071 | ||
2072 | /* | |
2073 | * At this point, the space is unlocked and the destination | |
2074 | * port is locked. (Lock taken while space was locked.) | |
2075 | * reply_name is taken care of; we still need dest_name. | |
2076 | * We still hold a ref for reply (if it is valid). | |
2077 | * | |
2078 | * If the space holds receive rights for the destination, | |
2079 | * we return its name for the right. Otherwise the task | |
2080 | * managed to destroy or give away the receive right between | |
2081 | * receiving the message and this copyout. If the destination | |
2082 | * is dead, return MACH_PORT_DEAD, and if the receive right | |
2083 | * exists somewhere else (another space, in transit) | |
2084 | * return MACH_PORT_NULL. | |
2085 | * | |
2086 | * Making this copyout operation atomic with the previous | |
2087 | * copyout of the reply port is a bit tricky. If there was | |
2088 | * no real reply port (it wasn't IP_VALID) then this isn't | |
2089 | * an issue. If the reply port was dead at copyout time, | |
2090 | * then we are OK, because if dest is dead we serialize | |
2091 | * after the death of both ports and if dest is alive | |
2092 | * we serialize after reply died but before dest's (later) death. | |
2093 | * So assume reply was alive when we copied it out. If dest | |
2094 | * is alive, then we are OK because we serialize before | |
2095 | * the ports' deaths. So assume dest is dead when we look at it. | |
2096 | * If reply dies/died after dest, then we are OK because | |
2097 | * we serialize after dest died but before reply dies. | |
2098 | * So the hard case is when reply is alive at copyout, | |
2099 | * dest is dead at copyout, and reply died before dest died. | |
2100 | * In this case pretend that dest is still alive, so | |
2101 | * we serialize while both ports are alive. | |
2102 | * | |
2103 | * Because the space lock is held across the copyout of reply | |
2104 | * and locking dest, the receive right for dest can't move | |
2105 | * in or out of the space while the copyouts happen, so | |
2106 | * that isn't an atomicity problem. In the last hard case | |
2107 | * above, this implies that when dest is dead that the | |
2108 | * space couldn't have had receive rights for dest at | |
2109 | * the time reply was copied-out, so when we pretend | |
2110 | * that dest is still alive, we can return MACH_PORT_NULL. | |
2111 | * | |
2112 | * If dest == reply, then we have to make it look like | |
2113 | * either both copyouts happened before the port died, | |
2114 | * or both happened after the port died. This special | |
2115 | * case works naturally if the timestamp comparison | |
2116 | * is done correctly. | |
2117 | */ | |
2118 | ||
2119 | copyout_dest: | |
2120 | ||
2121 | if (ip_active(dest)) { | |
2122 | ipc_object_copyout_dest(space, (ipc_object_t) dest, | |
2123 | dest_type, &dest_name); | |
2124 | /* dest is unlocked */ | |
2125 | } else { | |
2126 | ipc_port_timestamp_t timestamp; | |
2127 | ||
2128 | timestamp = dest->ip_timestamp; | |
2129 | ip_release(dest); | |
2130 | ip_check_unlock(dest); | |
2131 | ||
2132 | if (IP_VALID(reply)) { | |
2133 | ip_lock(reply); | |
2134 | if (ip_active(reply) || | |
2135 | IP_TIMESTAMP_ORDER(timestamp, | |
2136 | reply->ip_timestamp)) | |
2137 | dest_name = MACH_PORT_DEAD; | |
2138 | else | |
2139 | dest_name = MACH_PORT_NULL; | |
2140 | ip_unlock(reply); | |
2141 | } else | |
2142 | dest_name = MACH_PORT_DEAD; | |
2143 | } | |
2144 | ||
2145 | if (IP_VALID(reply)) | |
2146 | ipc_port_release(reply); | |
2147 | ||
2148 | msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) | | |
2149 | MACH_MSGH_BITS(reply_type, dest_type)); | |
2150 | msg->msgh_local_port = (ipc_port_t)dest_name; | |
2151 | msg->msgh_remote_port = (ipc_port_t)reply_name; | |
2152 | } | |
2153 | ||
2154 | return MACH_MSG_SUCCESS; | |
2155 | } | |
2156 | ||
2157 | /* | |
2158 | * Routine: ipc_kmsg_copyout_object | |
2159 | * Purpose: | |
2160 | * Copy-out a port right. Always returns a name, | |
2161 | * even for unsuccessful return codes. Always | |
2162 | * consumes the supplied object. | |
2163 | * Conditions: | |
2164 | * Nothing locked. | |
2165 | * Returns: | |
2166 | * MACH_MSG_SUCCESS The space acquired the right | |
2167 | * (name is valid) or the object is dead (MACH_PORT_DEAD). | |
2168 | * MACH_MSG_IPC_SPACE No room in space for the right, | |
2169 | * or the space is dead. (Name is MACH_PORT_NULL.) | |
2170 | * MACH_MSG_IPC_KERNEL Kernel resource shortage. | |
2171 | * (Name is MACH_PORT_NULL.) | |
2172 | */ | |
2173 | ||
2174 | mach_msg_return_t | |
2175 | ipc_kmsg_copyout_object( | |
2176 | ipc_space_t space, | |
2177 | ipc_object_t object, | |
2178 | mach_msg_type_name_t msgt_name, | |
2179 | mach_port_name_t *namep) | |
2180 | { | |
2181 | kern_return_t kr; | |
2182 | ||
2183 | if (!IO_VALID(object)) { | |
2184 | *namep = (mach_port_name_t) object; | |
2185 | return MACH_MSG_SUCCESS; | |
2186 | } | |
2187 | ||
2188 | kr = ipc_object_copyout(space, object, msgt_name, TRUE, namep); | |
2189 | if (kr != KERN_SUCCESS) { | |
2190 | ipc_object_destroy(object, msgt_name); | |
2191 | ||
2192 | if (kr == KERN_INVALID_CAPABILITY) | |
2193 | *namep = MACH_PORT_DEAD; | |
2194 | else { | |
2195 | *namep = MACH_PORT_NULL; | |
2196 | ||
2197 | if (kr == KERN_RESOURCE_SHORTAGE) | |
2198 | return MACH_MSG_IPC_KERNEL; | |
2199 | else | |
2200 | return MACH_MSG_IPC_SPACE; | |
2201 | } | |
2202 | } | |
2203 | ||
2204 | return MACH_MSG_SUCCESS; | |
2205 | } | |
2206 | ||
2207 | /* | |
2208 | * Routine: ipc_kmsg_copyout_body | |
2209 | * Purpose: | |
2210 | * "Copy-out" port rights and out-of-line memory | |
2211 | * in the body of a message. | |
2212 | * | |
2213 | * The error codes are a combination of special bits. | |
2214 | * The copyout proceeds despite errors. | |
2215 | * Conditions: | |
2216 | * Nothing locked. | |
2217 | * Returns: | |
2218 | * MACH_MSG_SUCCESS Successful copyout. | |
2219 | * MACH_MSG_IPC_SPACE No room for port right in name space. | |
2220 | * MACH_MSG_VM_SPACE No room for memory in address space. | |
2221 | * MACH_MSG_IPC_KERNEL Resource shortage handling port right. | |
2222 | * MACH_MSG_VM_KERNEL Resource shortage handling memory. | |
2223 | * MACH_MSG_INVALID_RT_DESCRIPTOR Descriptor incompatible with RT | |
2224 | */ | |
2225 | ||
2226 | mach_msg_return_t | |
2227 | ipc_kmsg_copyout_body( | |
2228 | ipc_kmsg_t kmsg, | |
2229 | ipc_space_t space, | |
2230 | vm_map_t map, | |
2231 | mach_msg_body_t *slist) | |
2232 | { | |
2233 | mach_msg_body_t *body; | |
2234 | mach_msg_descriptor_t *saddr, *eaddr; | |
2235 | mach_msg_return_t mr = MACH_MSG_SUCCESS; | |
2236 | kern_return_t kr; | |
2237 | vm_offset_t data; | |
2238 | mach_msg_descriptor_t *sstart, *send; | |
2239 | ||
2240 | body = (mach_msg_body_t *) (&kmsg->ikm_header + 1); | |
2241 | saddr = (mach_msg_descriptor_t *) (body + 1); | |
2242 | eaddr = saddr + body->msgh_descriptor_count; | |
2243 | ||
2244 | /* | |
2245 | * Do scatter list setup | |
2246 | */ | |
2247 | if (slist != MACH_MSG_BODY_NULL) { | |
2248 | sstart = (mach_msg_descriptor_t *) (slist + 1); | |
2249 | send = sstart + slist->msgh_descriptor_count; | |
2250 | } | |
2251 | else { | |
2252 | sstart = MACH_MSG_DESCRIPTOR_NULL; | |
2253 | } | |
2254 | ||
2255 | for ( ; saddr < eaddr; saddr++ ) { | |
2256 | ||
2257 | switch (saddr->type.type) { | |
2258 | ||
2259 | case MACH_MSG_PORT_DESCRIPTOR: { | |
2260 | mach_msg_port_descriptor_t *dsc; | |
2261 | ||
2262 | /* | |
2263 | * Copyout port right carried in the message | |
2264 | */ | |
2265 | dsc = &saddr->port; | |
2266 | mr |= ipc_kmsg_copyout_object(space, | |
2267 | (ipc_object_t) dsc->name, | |
2268 | dsc->disposition, | |
2269 | (mach_port_name_t *) &dsc->name); | |
2270 | ||
2271 | break; | |
2272 | } | |
2273 | case MACH_MSG_OOL_VOLATILE_DESCRIPTOR: | |
2274 | case MACH_MSG_OOL_DESCRIPTOR : { | |
2275 | vm_offset_t rcv_addr; | |
2276 | vm_offset_t snd_addr; | |
2277 | mach_msg_ool_descriptor_t *dsc; | |
2278 | mach_msg_copy_options_t copy_option; | |
2279 | ||
2280 | SKIP_PORT_DESCRIPTORS(sstart, send); | |
2281 | ||
2282 | dsc = &saddr->out_of_line; | |
2283 | ||
2284 | assert(dsc->copy != MACH_MSG_KALLOC_COPY_T); | |
1c79356b A |
2285 | |
2286 | copy_option = dsc->copy; | |
2287 | ||
2288 | if ((snd_addr = (vm_offset_t) dsc->address) != 0) { | |
2289 | if (sstart != MACH_MSG_DESCRIPTOR_NULL && | |
2290 | sstart->out_of_line.copy == MACH_MSG_OVERWRITE) { | |
2291 | ||
2292 | /* | |
2293 | * There is an overwrite descriptor specified in the | |
2294 | * scatter list for this ool data. The descriptor | |
2295 | * has already been verified | |
2296 | */ | |
2297 | rcv_addr = (vm_offset_t) sstart->out_of_line.address; | |
2298 | dsc->copy = MACH_MSG_OVERWRITE; | |
2299 | } else { | |
2300 | dsc->copy = MACH_MSG_ALLOCATE; | |
2301 | } | |
2302 | ||
2303 | /* | |
2304 | * Whether the data was virtually or physically | |
2305 | * copied we have a vm_map_copy_t for it. | |
2306 | * If there's an overwrite region specified | |
2307 | * overwrite it, otherwise do a virtual copy out. | |
2308 | */ | |
2309 | if (dsc->copy == MACH_MSG_OVERWRITE) { | |
2310 | kr = vm_map_copy_overwrite(map, rcv_addr, | |
2311 | (vm_map_copy_t) dsc->address, TRUE); | |
2312 | } else { | |
2313 | kr = vm_map_copyout(map, &rcv_addr, | |
2314 | (vm_map_copy_t) dsc->address); | |
2315 | } | |
2316 | if (kr != KERN_SUCCESS) { | |
2317 | if (kr == KERN_RESOURCE_SHORTAGE) | |
2318 | mr |= MACH_MSG_VM_KERNEL; | |
2319 | else | |
2320 | mr |= MACH_MSG_VM_SPACE; | |
2321 | vm_map_copy_discard((vm_map_copy_t) dsc->address); | |
2322 | dsc->address = 0; | |
2323 | INCREMENT_SCATTER(sstart); | |
2324 | break; | |
2325 | } | |
2326 | dsc->address = (void *) rcv_addr; | |
2327 | } | |
2328 | INCREMENT_SCATTER(sstart); | |
2329 | break; | |
2330 | } | |
2331 | case MACH_MSG_OOL_PORTS_DESCRIPTOR : { | |
2332 | vm_offset_t addr; | |
2333 | mach_port_name_t *objects; | |
2334 | mach_msg_type_number_t j; | |
2335 | vm_size_t length; | |
2336 | mach_msg_ool_ports_descriptor_t *dsc; | |
2337 | ||
2338 | SKIP_PORT_DESCRIPTORS(sstart, send); | |
2339 | ||
2340 | dsc = &saddr->ool_ports; | |
2341 | ||
2342 | length = dsc->count * sizeof(mach_port_name_t); | |
2343 | ||
2344 | if (length != 0) { | |
2345 | if (sstart != MACH_MSG_DESCRIPTOR_NULL && | |
2346 | sstart->ool_ports.copy == MACH_MSG_OVERWRITE) { | |
2347 | ||
2348 | /* | |
2349 | * There is an overwrite descriptor specified in the | |
2350 | * scatter list for this ool data. The descriptor | |
2351 | * has already been verified | |
2352 | */ | |
2353 | addr = (vm_offset_t) sstart->out_of_line.address; | |
2354 | dsc->copy = MACH_MSG_OVERWRITE; | |
2355 | } | |
2356 | else { | |
2357 | ||
2358 | /* | |
2359 | * Dynamically allocate the region | |
2360 | */ | |
2361 | int anywhere = VM_MAKE_TAG(VM_MEMORY_MACH_MSG)| | |
2362 | VM_FLAGS_ANYWHERE; | |
2363 | ||
2364 | dsc->copy = MACH_MSG_ALLOCATE; | |
2365 | if ((kr = vm_allocate(map, &addr, length, | |
2366 | anywhere)) != KERN_SUCCESS) { | |
2367 | ipc_kmsg_clean_body(kmsg, | |
2368 | body->msgh_descriptor_count); | |
2369 | dsc->address = 0; | |
2370 | ||
2371 | if (kr == KERN_RESOURCE_SHORTAGE){ | |
2372 | mr |= MACH_MSG_VM_KERNEL; | |
2373 | } else { | |
2374 | mr |= MACH_MSG_VM_SPACE; | |
2375 | } | |
2376 | INCREMENT_SCATTER(sstart); | |
2377 | break; | |
2378 | } | |
2379 | } | |
2380 | } else { | |
2381 | INCREMENT_SCATTER(sstart); | |
2382 | break; | |
2383 | } | |
2384 | ||
2385 | ||
2386 | objects = (mach_port_name_t *) dsc->address ; | |
2387 | ||
2388 | /* copyout port rights carried in the message */ | |
2389 | ||
2390 | for ( j = 0; j < dsc->count ; j++) { | |
2391 | ipc_object_t object = | |
2392 | (ipc_object_t) objects[j]; | |
2393 | ||
2394 | mr |= ipc_kmsg_copyout_object(space, object, | |
2395 | dsc->disposition, &objects[j]); | |
2396 | } | |
2397 | ||
2398 | /* copyout to memory allocated above */ | |
2399 | ||
2400 | data = (vm_offset_t) dsc->address; | |
2401 | (void) copyoutmap(map, data, addr, length); | |
2402 | kfree(data, length); | |
2403 | ||
2404 | dsc->address = (void *) addr; | |
2405 | INCREMENT_SCATTER(sstart); | |
2406 | break; | |
2407 | } | |
2408 | default : { | |
2409 | panic("untyped IPC copyout body: invalid message descriptor"); | |
2410 | } | |
2411 | } | |
2412 | } | |
2413 | return mr; | |
2414 | } | |
2415 | ||
2416 | /* | |
2417 | * Routine: ipc_kmsg_copyout | |
2418 | * Purpose: | |
2419 | * "Copy-out" port rights and out-of-line memory | |
2420 | * in the message. | |
2421 | * Conditions: | |
2422 | * Nothing locked. | |
2423 | * Returns: | |
2424 | * MACH_MSG_SUCCESS Copied out all rights and memory. | |
2425 | * MACH_RCV_INVALID_NOTIFY Bad notify port. | |
2426 | * Rights and memory in the message are intact. | |
2427 | * MACH_RCV_HEADER_ERROR + special bits | |
2428 | * Rights and memory in the message are intact. | |
2429 | * MACH_RCV_BODY_ERROR + special bits | |
2430 | * The message header was successfully copied out. | |
2431 | * As much of the body was handled as possible. | |
2432 | */ | |
2433 | ||
2434 | mach_msg_return_t | |
2435 | ipc_kmsg_copyout( | |
2436 | ipc_kmsg_t kmsg, | |
2437 | ipc_space_t space, | |
2438 | vm_map_t map, | |
2439 | mach_port_name_t notify, | |
2440 | mach_msg_body_t *slist) | |
2441 | { | |
2442 | mach_msg_return_t mr; | |
2443 | ||
2444 | mr = ipc_kmsg_copyout_header(&kmsg->ikm_header, space, notify); | |
2445 | if (mr != MACH_MSG_SUCCESS) | |
2446 | return mr; | |
2447 | ||
2448 | if (kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) { | |
2449 | mr = ipc_kmsg_copyout_body(kmsg, space, map, slist); | |
2450 | ||
2451 | if (mr != MACH_MSG_SUCCESS) | |
2452 | mr |= MACH_RCV_BODY_ERROR; | |
2453 | } | |
2454 | ||
2455 | return mr; | |
2456 | } | |
2457 | ||
2458 | /* | |
2459 | * Routine: ipc_kmsg_copyout_pseudo | |
2460 | * Purpose: | |
2461 | * Does a pseudo-copyout of the message. | |
2462 | * This is like a regular copyout, except | |
2463 | * that the ports in the header are handled | |
2464 | * as if they are in the body. They aren't reversed. | |
2465 | * | |
2466 | * The error codes are a combination of special bits. | |
2467 | * The copyout proceeds despite errors. | |
2468 | * Conditions: | |
2469 | * Nothing locked. | |
2470 | * Returns: | |
2471 | * MACH_MSG_SUCCESS Successful copyout. | |
2472 | * MACH_MSG_IPC_SPACE No room for port right in name space. | |
2473 | * MACH_MSG_VM_SPACE No room for memory in address space. | |
2474 | * MACH_MSG_IPC_KERNEL Resource shortage handling port right. | |
2475 | * MACH_MSG_VM_KERNEL Resource shortage handling memory. | |
2476 | */ | |
2477 | ||
2478 | mach_msg_return_t | |
2479 | ipc_kmsg_copyout_pseudo( | |
2480 | ipc_kmsg_t kmsg, | |
2481 | ipc_space_t space, | |
2482 | vm_map_t map, | |
2483 | mach_msg_body_t *slist) | |
2484 | { | |
2485 | mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits; | |
2486 | ipc_object_t dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
2487 | ipc_object_t reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port; | |
2488 | mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits); | |
2489 | mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits); | |
2490 | mach_port_name_t dest_name, reply_name; | |
2491 | mach_msg_return_t mr; | |
2492 | ||
2493 | assert(IO_VALID(dest)); | |
2494 | ||
2495 | mr = (ipc_kmsg_copyout_object(space, dest, dest_type, &dest_name) | | |
2496 | ipc_kmsg_copyout_object(space, reply, reply_type, &reply_name)); | |
2497 | ||
2498 | kmsg->ikm_header.msgh_bits = mbits &~ MACH_MSGH_BITS_CIRCULAR; | |
2499 | kmsg->ikm_header.msgh_remote_port = (ipc_port_t)dest_name; | |
2500 | kmsg->ikm_header.msgh_local_port = (ipc_port_t)reply_name; | |
2501 | ||
2502 | if (mbits & MACH_MSGH_BITS_COMPLEX) { | |
2503 | mr |= ipc_kmsg_copyout_body(kmsg, space, map, slist); | |
2504 | } | |
2505 | ||
2506 | return mr; | |
2507 | } | |
2508 | ||
2509 | /* | |
2510 | * Routine: ipc_kmsg_copyout_dest | |
2511 | * Purpose: | |
2512 | * Copies out the destination port in the message. | |
2513 | * Destroys all other rights and memory in the message. | |
2514 | * Conditions: | |
2515 | * Nothing locked. | |
2516 | */ | |
2517 | ||
2518 | void | |
2519 | ipc_kmsg_copyout_dest( | |
2520 | ipc_kmsg_t kmsg, | |
2521 | ipc_space_t space) | |
2522 | { | |
2523 | mach_msg_bits_t mbits; | |
2524 | ipc_object_t dest; | |
2525 | ipc_object_t reply; | |
2526 | mach_msg_type_name_t dest_type; | |
2527 | mach_msg_type_name_t reply_type; | |
2528 | mach_port_name_t dest_name, reply_name; | |
2529 | ||
2530 | mbits = kmsg->ikm_header.msgh_bits; | |
2531 | dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
2532 | reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port; | |
2533 | dest_type = MACH_MSGH_BITS_REMOTE(mbits); | |
2534 | reply_type = MACH_MSGH_BITS_LOCAL(mbits); | |
2535 | ||
2536 | assert(IO_VALID(dest)); | |
2537 | ||
2538 | io_lock(dest); | |
2539 | if (io_active(dest)) { | |
2540 | ipc_object_copyout_dest(space, dest, dest_type, &dest_name); | |
2541 | /* dest is unlocked */ | |
2542 | } else { | |
2543 | io_release(dest); | |
2544 | io_check_unlock(dest); | |
2545 | dest_name = MACH_PORT_DEAD; | |
2546 | } | |
2547 | ||
2548 | if (IO_VALID(reply)) { | |
2549 | ipc_object_destroy(reply, reply_type); | |
2550 | reply_name = MACH_PORT_NULL; | |
2551 | } else | |
2552 | reply_name = (mach_port_name_t) reply; | |
2553 | ||
2554 | kmsg->ikm_header.msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) | | |
2555 | MACH_MSGH_BITS(reply_type, dest_type)); | |
2556 | kmsg->ikm_header.msgh_local_port = (ipc_port_t)dest_name; | |
2557 | kmsg->ikm_header.msgh_remote_port = (ipc_port_t)reply_name; | |
2558 | ||
2559 | if (mbits & MACH_MSGH_BITS_COMPLEX) { | |
2560 | mach_msg_body_t *body; | |
2561 | ||
2562 | body = (mach_msg_body_t *) (&kmsg->ikm_header + 1); | |
2563 | ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count); | |
2564 | } | |
2565 | } | |
2566 | /* | |
2567 | * Routine: ipc_kmsg_copyin_scatter | |
2568 | * Purpose: | |
2569 | * allocate and copyin a scatter list | |
2570 | * Algorithm: | |
2571 | * The gather (kmsg) is valid since it has been copied in. | |
2572 | * Gather list descriptors are sequentially paired with scatter | |
2573 | * list descriptors, with port descriptors in either list ignored. | |
2574 | * Descriptors are consistent if the type fileds match and size | |
2575 | * of the scatter descriptor is less than or equal to the | |
2576 | * size of the gather descriptor. A MACH_MSG_ALLOCATE copy | |
2577 | * strategy in a scatter descriptor matches any size in the | |
2578 | * corresponding gather descriptor assuming they are the same type. | |
2579 | * Either list may be larger than the other. During the | |
2580 | * subsequent copy out, excess scatter descriptors are ignored | |
2581 | * and excess gather descriptors default to dynamic allocation. | |
2582 | * | |
2583 | * In the case of a size error, the scatter list is released. | |
2584 | * Conditions: | |
2585 | * Nothing locked. | |
2586 | * Returns: | |
2587 | * the allocated message body containing the scatter list. | |
2588 | */ | |
2589 | ||
2590 | mach_msg_body_t * | |
2591 | ipc_kmsg_copyin_scatter( | |
2592 | mach_msg_header_t *msg, | |
2593 | mach_msg_size_t slist_size, | |
2594 | ipc_kmsg_t kmsg) | |
2595 | { | |
2596 | mach_msg_body_t *slist; | |
2597 | mach_msg_body_t *body; | |
2598 | mach_msg_descriptor_t *gstart, *gend; | |
2599 | mach_msg_descriptor_t *sstart, *send; | |
2600 | ||
2601 | ||
2602 | if (slist_size < sizeof(mach_msg_base_t)) | |
2603 | return MACH_MSG_BODY_NULL; | |
2604 | ||
2605 | slist_size -= sizeof(mach_msg_header_t); | |
2606 | slist = (mach_msg_body_t *)kalloc(slist_size); | |
2607 | if (slist == MACH_MSG_BODY_NULL) | |
2608 | return slist; | |
2609 | ||
2610 | if (copyin((char *) (msg + 1), (char *)slist, slist_size)) { | |
2611 | kfree((vm_offset_t)slist, slist_size); | |
2612 | return MACH_MSG_BODY_NULL; | |
2613 | } | |
2614 | ||
2615 | if ((slist->msgh_descriptor_count* sizeof(mach_msg_descriptor_t) | |
2616 | + sizeof(mach_msg_size_t)) > slist_size) { | |
2617 | kfree((vm_offset_t)slist, slist_size); | |
2618 | return MACH_MSG_BODY_NULL; | |
2619 | } | |
2620 | ||
2621 | body = (mach_msg_body_t *) (&kmsg->ikm_header + 1); | |
2622 | gstart = (mach_msg_descriptor_t *) (body + 1); | |
2623 | gend = gstart + body->msgh_descriptor_count; | |
2624 | ||
2625 | sstart = (mach_msg_descriptor_t *) (slist + 1); | |
2626 | send = sstart + slist->msgh_descriptor_count; | |
2627 | ||
2628 | while (gstart < gend) { | |
2629 | mach_msg_descriptor_type_t g_type; | |
2630 | ||
2631 | /* | |
2632 | * Skip port descriptors in gather list. | |
2633 | */ | |
2634 | g_type = gstart->type.type; | |
2635 | ||
2636 | if (g_type != MACH_MSG_PORT_DESCRIPTOR) { | |
2637 | ||
2638 | /* | |
2639 | * A scatter list with a 0 descriptor count is treated as an | |
2640 | * automatic size mismatch. | |
2641 | */ | |
2642 | if (slist->msgh_descriptor_count == 0) { | |
2643 | kfree((vm_offset_t)slist, slist_size); | |
2644 | return MACH_MSG_BODY_NULL; | |
2645 | } | |
2646 | ||
2647 | /* | |
2648 | * Skip port descriptors in scatter list. | |
2649 | */ | |
2650 | while (sstart < send) { | |
2651 | if (sstart->type.type != MACH_MSG_PORT_DESCRIPTOR) | |
2652 | break; | |
2653 | sstart++; | |
2654 | } | |
2655 | ||
2656 | /* | |
2657 | * No more scatter descriptors, we're done | |
2658 | */ | |
2659 | if (sstart >= send) { | |
2660 | break; | |
2661 | } | |
2662 | ||
2663 | /* | |
2664 | * Check type, copy and size fields | |
2665 | */ | |
2666 | if (g_type == MACH_MSG_OOL_DESCRIPTOR || | |
2667 | g_type == MACH_MSG_OOL_VOLATILE_DESCRIPTOR) { | |
2668 | if (sstart->type.type != MACH_MSG_OOL_DESCRIPTOR && | |
2669 | sstart->type.type != MACH_MSG_OOL_VOLATILE_DESCRIPTOR) { | |
2670 | kfree((vm_offset_t)slist, slist_size); | |
2671 | return MACH_MSG_BODY_NULL; | |
2672 | } | |
2673 | if (sstart->out_of_line.copy == MACH_MSG_OVERWRITE && | |
2674 | gstart->out_of_line.size > sstart->out_of_line.size) { | |
2675 | kfree((vm_offset_t)slist, slist_size); | |
2676 | return MACH_MSG_BODY_NULL; | |
2677 | } | |
2678 | } | |
2679 | else { | |
2680 | if (sstart->type.type != MACH_MSG_OOL_PORTS_DESCRIPTOR) { | |
2681 | kfree((vm_offset_t)slist, slist_size); | |
2682 | return MACH_MSG_BODY_NULL; | |
2683 | } | |
2684 | if (sstart->ool_ports.copy == MACH_MSG_OVERWRITE && | |
2685 | gstart->ool_ports.count > sstart->ool_ports.count) { | |
2686 | kfree((vm_offset_t)slist, slist_size); | |
2687 | return MACH_MSG_BODY_NULL; | |
2688 | } | |
2689 | } | |
2690 | sstart++; | |
2691 | } | |
2692 | gstart++; | |
2693 | } | |
2694 | return slist; | |
2695 | } | |
2696 | ||
2697 | ||
2698 | /* | |
2699 | * Routine: ipc_kmsg_free_scatter | |
2700 | * Purpose: | |
2701 | * Deallocate a scatter list. Since we actually allocated | |
2702 | * a body without a header, and since the header was originally | |
2703 | * accounted for in slist_size, we have to ajust it down | |
2704 | * before freeing the scatter list. | |
2705 | */ | |
2706 | void | |
2707 | ipc_kmsg_free_scatter( | |
2708 | mach_msg_body_t *slist, | |
2709 | mach_msg_size_t slist_size) | |
2710 | { | |
2711 | slist_size -= sizeof(mach_msg_header_t); | |
2712 | kfree((vm_offset_t)slist, slist_size); | |
2713 | } | |
2714 | ||
2715 | ||
2716 | /* | |
2717 | * Routine: ipc_kmsg_copyout_to_kernel | |
2718 | * Purpose: | |
2719 | * Copies out the destination and reply ports in the message. | |
2720 | * Leaves all other rights and memory in the message alone. | |
2721 | * Conditions: | |
2722 | * Nothing locked. | |
2723 | * | |
2724 | * Derived from ipc_kmsg_copyout_dest. | |
2725 | * Use by mach_msg_rpc_from_kernel (which used to use copyout_dest). | |
2726 | * We really do want to save rights and memory. | |
2727 | */ | |
2728 | ||
2729 | void | |
2730 | ipc_kmsg_copyout_to_kernel( | |
2731 | ipc_kmsg_t kmsg, | |
2732 | ipc_space_t space) | |
2733 | { | |
2734 | ipc_object_t dest; | |
2735 | ipc_object_t reply; | |
2736 | mach_msg_type_name_t dest_type; | |
2737 | mach_msg_type_name_t reply_type; | |
2738 | mach_port_name_t dest_name, reply_name; | |
2739 | ||
2740 | dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port; | |
2741 | reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port; | |
2742 | dest_type = MACH_MSGH_BITS_REMOTE(kmsg->ikm_header.msgh_bits); | |
2743 | reply_type = MACH_MSGH_BITS_LOCAL(kmsg->ikm_header.msgh_bits); | |
2744 | ||
2745 | assert(IO_VALID(dest)); | |
2746 | ||
2747 | io_lock(dest); | |
2748 | if (io_active(dest)) { | |
2749 | ipc_object_copyout_dest(space, dest, dest_type, &dest_name); | |
2750 | /* dest is unlocked */ | |
2751 | } else { | |
2752 | io_release(dest); | |
2753 | io_check_unlock(dest); | |
2754 | dest_name = MACH_PORT_DEAD; | |
2755 | } | |
2756 | ||
2757 | reply_name = (mach_port_name_t) reply; | |
2758 | ||
2759 | kmsg->ikm_header.msgh_bits = | |
2760 | (MACH_MSGH_BITS_OTHER(kmsg->ikm_header.msgh_bits) | | |
2761 | MACH_MSGH_BITS(reply_type, dest_type)); | |
2762 | kmsg->ikm_header.msgh_local_port = (ipc_port_t)dest_name; | |
2763 | kmsg->ikm_header.msgh_remote_port = (ipc_port_t)reply_name; | |
2764 | } | |
2765 | ||
2766 | #include <mach_kdb.h> | |
2767 | #if MACH_KDB | |
2768 | ||
2769 | #include <ddb/db_output.h> | |
2770 | #include <ipc/ipc_print.h> | |
2771 | /* | |
2772 | * Forward declarations | |
2773 | */ | |
2774 | void ipc_msg_print_untyped( | |
2775 | mach_msg_body_t *body); | |
2776 | ||
2777 | char * ipc_type_name( | |
2778 | int type_name, | |
2779 | boolean_t received); | |
2780 | ||
2781 | void ipc_print_type_name( | |
2782 | int type_name); | |
2783 | ||
2784 | char * | |
2785 | msgh_bit_decode( | |
2786 | mach_msg_bits_t bit); | |
2787 | ||
2788 | char * | |
2789 | mm_copy_options_string( | |
2790 | mach_msg_copy_options_t option); | |
2791 | ||
2792 | void db_print_msg_uid(mach_msg_header_t *); | |
2793 | ||
2794 | ||
2795 | char * | |
2796 | ipc_type_name( | |
2797 | int type_name, | |
2798 | boolean_t received) | |
2799 | { | |
2800 | switch (type_name) { | |
2801 | case MACH_MSG_TYPE_PORT_NAME: | |
2802 | return "port_name"; | |
2803 | ||
2804 | case MACH_MSG_TYPE_MOVE_RECEIVE: | |
2805 | if (received) { | |
2806 | return "port_receive"; | |
2807 | } else { | |
2808 | return "move_receive"; | |
2809 | } | |
2810 | ||
2811 | case MACH_MSG_TYPE_MOVE_SEND: | |
2812 | if (received) { | |
2813 | return "port_send"; | |
2814 | } else { | |
2815 | return "move_send"; | |
2816 | } | |
2817 | ||
2818 | case MACH_MSG_TYPE_MOVE_SEND_ONCE: | |
2819 | if (received) { | |
2820 | return "port_send_once"; | |
2821 | } else { | |
2822 | return "move_send_once"; | |
2823 | } | |
2824 | ||
2825 | case MACH_MSG_TYPE_COPY_SEND: | |
2826 | return "copy_send"; | |
2827 | ||
2828 | case MACH_MSG_TYPE_MAKE_SEND: | |
2829 | return "make_send"; | |
2830 | ||
2831 | case MACH_MSG_TYPE_MAKE_SEND_ONCE: | |
2832 | return "make_send_once"; | |
2833 | ||
2834 | default: | |
2835 | return (char *) 0; | |
2836 | } | |
2837 | } | |
2838 | ||
2839 | void | |
2840 | ipc_print_type_name( | |
2841 | int type_name) | |
2842 | { | |
2843 | char *name = ipc_type_name(type_name, TRUE); | |
2844 | if (name) { | |
2845 | printf("%s", name); | |
2846 | } else { | |
2847 | printf("type%d", type_name); | |
2848 | } | |
2849 | } | |
2850 | ||
2851 | /* | |
2852 | * ipc_kmsg_print [ debug ] | |
2853 | */ | |
2854 | void | |
2855 | ipc_kmsg_print( | |
2856 | ipc_kmsg_t kmsg) | |
2857 | { | |
2858 | iprintf("kmsg=0x%x\n", kmsg); | |
2859 | iprintf("ikm_next=0x%x, prev=0x%x, size=%d", | |
2860 | kmsg->ikm_next, | |
2861 | kmsg->ikm_prev, | |
2862 | kmsg->ikm_size); | |
2863 | printf("\n"); | |
2864 | ipc_msg_print(&kmsg->ikm_header); | |
2865 | } | |
2866 | ||
2867 | char * | |
2868 | msgh_bit_decode( | |
2869 | mach_msg_bits_t bit) | |
2870 | { | |
2871 | switch (bit) { | |
2872 | case MACH_MSGH_BITS_COMPLEX: return "complex"; | |
2873 | case MACH_MSGH_BITS_CIRCULAR: return "circular"; | |
2874 | default: return (char *) 0; | |
2875 | } | |
2876 | } | |
2877 | ||
2878 | /* | |
2879 | * ipc_msg_print [ debug ] | |
2880 | */ | |
2881 | void | |
2882 | ipc_msg_print( | |
2883 | mach_msg_header_t *msgh) | |
2884 | { | |
2885 | mach_msg_bits_t mbits; | |
2886 | unsigned int bit, i; | |
2887 | char *bit_name; | |
2888 | int needs_comma; | |
2889 | ||
2890 | mbits = msgh->msgh_bits; | |
2891 | iprintf("msgh_bits=0x%x: l=0x%x,r=0x%x\n", | |
2892 | mbits, | |
2893 | MACH_MSGH_BITS_LOCAL(msgh->msgh_bits), | |
2894 | MACH_MSGH_BITS_REMOTE(msgh->msgh_bits)); | |
2895 | ||
2896 | mbits = MACH_MSGH_BITS_OTHER(mbits) & MACH_MSGH_BITS_USED; | |
2897 | db_indent += 2; | |
2898 | if (mbits) | |
2899 | iprintf("decoded bits: "); | |
2900 | needs_comma = 0; | |
2901 | for (i = 0, bit = 1; i < sizeof(mbits) * 8; ++i, bit <<= 1) { | |
2902 | if ((mbits & bit) == 0) | |
2903 | continue; | |
2904 | bit_name = msgh_bit_decode((mach_msg_bits_t)bit); | |
2905 | if (bit_name) | |
2906 | printf("%s%s", needs_comma ? "," : "", bit_name); | |
2907 | else | |
2908 | printf("%sunknown(0x%x),", needs_comma ? "," : "", bit); | |
2909 | ++needs_comma; | |
2910 | } | |
2911 | if (msgh->msgh_bits & ~MACH_MSGH_BITS_USED) { | |
2912 | printf("%sunused=0x%x,", needs_comma ? "," : "", | |
2913 | msgh->msgh_bits & ~MACH_MSGH_BITS_USED); | |
2914 | } | |
2915 | printf("\n"); | |
2916 | db_indent -= 2; | |
2917 | ||
2918 | needs_comma = 1; | |
2919 | if (msgh->msgh_remote_port) { | |
2920 | iprintf("remote=0x%x(", msgh->msgh_remote_port); | |
2921 | ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh->msgh_bits)); | |
2922 | printf(")"); | |
2923 | } else { | |
2924 | iprintf("remote=null"); | |
2925 | } | |
2926 | ||
2927 | if (msgh->msgh_local_port) { | |
2928 | printf("%slocal=0x%x(", needs_comma ? "," : "", | |
2929 | msgh->msgh_local_port); | |
2930 | ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh->msgh_bits)); | |
2931 | printf(")\n"); | |
2932 | } else { | |
2933 | printf("local=null\n"); | |
2934 | } | |
2935 | ||
2936 | iprintf("msgh_id=%d, size=%d\n", | |
2937 | msgh->msgh_id, | |
2938 | msgh->msgh_size); | |
2939 | ||
2940 | if (mbits & MACH_MSGH_BITS_COMPLEX) { | |
2941 | ipc_msg_print_untyped((mach_msg_body_t *) (msgh + 1)); | |
2942 | } | |
2943 | } | |
2944 | ||
2945 | ||
2946 | char * | |
2947 | mm_copy_options_string( | |
2948 | mach_msg_copy_options_t option) | |
2949 | { | |
2950 | char *name; | |
2951 | ||
2952 | switch (option) { | |
2953 | case MACH_MSG_PHYSICAL_COPY: | |
2954 | name = "PHYSICAL"; | |
2955 | break; | |
2956 | case MACH_MSG_VIRTUAL_COPY: | |
2957 | name = "VIRTUAL"; | |
2958 | break; | |
2959 | case MACH_MSG_OVERWRITE: | |
2960 | name = "OVERWRITE"; | |
2961 | break; | |
2962 | case MACH_MSG_ALLOCATE: | |
2963 | name = "ALLOCATE"; | |
2964 | break; | |
2965 | case MACH_MSG_KALLOC_COPY_T: | |
2966 | name = "KALLOC_COPY_T"; | |
2967 | break; | |
1c79356b A |
2968 | default: |
2969 | name = "unknown"; | |
2970 | break; | |
2971 | } | |
2972 | return name; | |
2973 | } | |
2974 | ||
2975 | void | |
2976 | ipc_msg_print_untyped( | |
2977 | mach_msg_body_t *body) | |
2978 | { | |
2979 | mach_msg_descriptor_t *saddr, *send; | |
2980 | mach_msg_descriptor_type_t type; | |
2981 | ||
2982 | iprintf("%d descriptors %d: \n", body->msgh_descriptor_count); | |
2983 | ||
2984 | saddr = (mach_msg_descriptor_t *) (body + 1); | |
2985 | send = saddr + body->msgh_descriptor_count; | |
2986 | ||
2987 | for ( ; saddr < send; saddr++ ) { | |
2988 | ||
2989 | type = saddr->type.type; | |
2990 | ||
2991 | switch (type) { | |
2992 | ||
2993 | case MACH_MSG_PORT_DESCRIPTOR: { | |
2994 | mach_msg_port_descriptor_t *dsc; | |
2995 | ||
2996 | dsc = &saddr->port; | |
2997 | iprintf("-- PORT name = 0x%x disp = ", dsc->name); | |
2998 | ipc_print_type_name(dsc->disposition); | |
2999 | printf("\n"); | |
3000 | break; | |
3001 | } | |
3002 | case MACH_MSG_OOL_VOLATILE_DESCRIPTOR: | |
3003 | case MACH_MSG_OOL_DESCRIPTOR: { | |
3004 | mach_msg_ool_descriptor_t *dsc; | |
3005 | ||
3006 | dsc = &saddr->out_of_line; | |
3007 | iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n", | |
3008 | type == MACH_MSG_OOL_DESCRIPTOR ? "" : " VOLATILE", | |
3009 | dsc->address, dsc->size, | |
3010 | mm_copy_options_string(dsc->copy), | |
3011 | dsc->deallocate ? "DEALLOC" : ""); | |
3012 | break; | |
3013 | } | |
3014 | case MACH_MSG_OOL_PORTS_DESCRIPTOR : { | |
3015 | mach_msg_ool_ports_descriptor_t *dsc; | |
3016 | ||
3017 | dsc = &saddr->ool_ports; | |
3018 | ||
3019 | iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ", | |
3020 | dsc->address, dsc->count); | |
3021 | printf("disp = "); | |
3022 | ipc_print_type_name(dsc->disposition); | |
3023 | printf(" copy = %s %s\n", | |
3024 | mm_copy_options_string(dsc->copy), | |
3025 | dsc->deallocate ? "DEALLOC" : ""); | |
3026 | break; | |
3027 | } | |
3028 | ||
3029 | default: { | |
3030 | iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type); | |
3031 | break; | |
3032 | } | |
3033 | } | |
3034 | } | |
3035 | } | |
3036 | #endif /* MACH_KDB */ |