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