]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_mig.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_mig.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #include <mach/boolean.h>
60 #include <mach/port.h>
61 #include <mach/mig.h>
62 #include <mach/mig_errors.h>
63 #include <mach/mach_types.h>
64 #include <mach/mach_traps.h>
65
66 #include <kern/ipc_tt.h>
67 #include <kern/ipc_mig.h>
68 #include <kern/kalloc.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71 #include <kern/ipc_kobject.h>
72 #include <kern/misc_protos.h>
73
74 #include <ipc/port.h>
75 #include <ipc/ipc_kmsg.h>
76 #include <ipc/ipc_entry.h>
77 #include <ipc/ipc_object.h>
78 #include <ipc/ipc_mqueue.h>
79 #include <ipc/ipc_space.h>
80 #include <ipc/ipc_port.h>
81 #include <ipc/ipc_pset.h>
82 #include <ipc/ipc_notify.h>
83 #include <vm/vm_map.h>
84
85 #include <libkern/OSAtomic.h>
86
87 /*
88 * Routine: mach_msg_send_from_kernel
89 * Purpose:
90 * Send a message from the kernel.
91 *
92 * This is used by the client side of KernelUser interfaces
93 * to implement SimpleRoutines. Currently, this includes
94 * memory_object messages.
95 * Conditions:
96 * Nothing locked.
97 * Returns:
98 * MACH_MSG_SUCCESS Sent the message.
99 * MACH_SEND_INVALID_DEST Bad destination port.
100 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
101 * or destination is above kernel limit
102 */
103
104 #if IKM_SUPPORT_LEGACY
105
106 #undef mach_msg_send_from_kernel
107 mach_msg_return_t mach_msg_send_from_kernel(
108 mach_msg_header_t *msg,
109 mach_msg_size_t send_size);
110
111 mach_msg_return_t
112 mach_msg_send_from_kernel(
113 mach_msg_header_t *msg,
114 mach_msg_size_t send_size)
115 {
116 ipc_kmsg_t kmsg;
117 mach_msg_return_t mr;
118
119 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
120 if (mr != MACH_MSG_SUCCESS)
121 return mr;
122
123 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
124 if (mr != MACH_MSG_SUCCESS) {
125 ipc_kmsg_free(kmsg);
126 return mr;
127 }
128
129 mr = ipc_kmsg_send_always(kmsg);
130 if (mr != MACH_MSG_SUCCESS) {
131 ipc_kmsg_destroy(kmsg);
132 }
133
134 return mr;
135 }
136
137 #endif /* IKM_SUPPORT_LEGACY */
138
139 mach_msg_return_t
140 mach_msg_send_from_kernel_proper(
141 mach_msg_header_t *msg,
142 mach_msg_size_t send_size)
143 {
144 ipc_kmsg_t kmsg;
145 mach_msg_return_t mr;
146
147 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
148 if (mr != MACH_MSG_SUCCESS)
149 return mr;
150
151 mr = ipc_kmsg_copyin_from_kernel(kmsg);
152 if (mr != MACH_MSG_SUCCESS) {
153 ipc_kmsg_free(kmsg);
154 return mr;
155 }
156
157 mr = ipc_kmsg_send_always(kmsg);
158 if (mr != MACH_MSG_SUCCESS) {
159 ipc_kmsg_destroy(kmsg);
160 }
161
162 return mr;
163 }
164
165 #if IKM_SUPPORT_LEGACY
166
167 mach_msg_return_t
168 mach_msg_send_from_kernel_with_options(
169 mach_msg_header_t *msg,
170 mach_msg_size_t send_size,
171 mach_msg_option_t option,
172 mach_msg_timeout_t timeout_val)
173 {
174 ipc_kmsg_t kmsg;
175 mach_msg_return_t mr;
176
177 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
178 if (mr != MACH_MSG_SUCCESS)
179 return mr;
180
181 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
182 if (mr != MACH_MSG_SUCCESS) {
183 ipc_kmsg_free(kmsg);
184 return mr;
185 }
186
187 mr = ipc_kmsg_send(kmsg, option, timeout_val);
188 if (mr != MACH_MSG_SUCCESS) {
189 ipc_kmsg_destroy(kmsg);
190 }
191
192 return mr;
193 }
194
195 #endif /* IKM_SUPPORT_LEGACY */
196
197 /*
198 * Routine: mach_msg_rpc_from_kernel
199 * Purpose:
200 * Send a message from the kernel and receive a reply.
201 * Uses ith_rpc_reply for the reply port.
202 *
203 * This is used by the client side of KernelUser interfaces
204 * to implement Routines.
205 * Conditions:
206 * Nothing locked.
207 * Returns:
208 * MACH_MSG_SUCCESS Sent the message.
209 * MACH_RCV_PORT_DIED The reply port was deallocated.
210 */
211
212 mach_msg_return_t mach_msg_rpc_from_kernel_body(mach_msg_header_t *msg,
213 mach_msg_size_t send_size, mach_msg_size_t rcv_size, boolean_t legacy);
214
215 #if IKM_SUPPORT_LEGACY
216
217 #undef mach_msg_rpc_from_kernel
218 mach_msg_return_t
219 mach_msg_rpc_from_kernel(
220 mach_msg_header_t *msg,
221 mach_msg_size_t send_size,
222 mach_msg_size_t rcv_size);
223
224 mach_msg_return_t
225 mach_msg_rpc_from_kernel(
226 mach_msg_header_t *msg,
227 mach_msg_size_t send_size,
228 mach_msg_size_t rcv_size)
229 {
230 return mach_msg_rpc_from_kernel_body(msg, send_size, rcv_size, TRUE);
231 }
232
233 #endif /* IKM_SUPPORT_LEGACY */
234
235 mach_msg_return_t
236 mach_msg_rpc_from_kernel_proper(
237 mach_msg_header_t *msg,
238 mach_msg_size_t send_size,
239 mach_msg_size_t rcv_size)
240 {
241 return mach_msg_rpc_from_kernel_body(msg, send_size, rcv_size, FALSE);
242 }
243
244 mach_msg_return_t
245 mach_msg_rpc_from_kernel_body(
246 mach_msg_header_t *msg,
247 mach_msg_size_t send_size,
248 mach_msg_size_t rcv_size,
249 #if !IKM_SUPPORT_LEGACY
250 __unused
251 #endif
252 boolean_t legacy)
253 {
254 thread_t self = current_thread();
255 ipc_port_t reply;
256 ipc_kmsg_t kmsg;
257 mach_port_seqno_t seqno;
258 mach_msg_return_t mr;
259
260 assert(msg->msgh_local_port == MACH_PORT_NULL);
261
262 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
263 if (mr != MACH_MSG_SUCCESS)
264 return mr;
265
266 reply = self->ith_rpc_reply;
267 if (reply == IP_NULL) {
268 reply = ipc_port_alloc_reply();
269 if ((reply == IP_NULL) ||
270 (self->ith_rpc_reply != IP_NULL))
271 panic("mach_msg_rpc_from_kernel");
272 self->ith_rpc_reply = reply;
273 }
274
275 /* insert send-once right for the reply port */
276 kmsg->ikm_header->msgh_local_port = reply;
277 kmsg->ikm_header->msgh_bits |=
278 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE);
279
280 ipc_port_reference(reply);
281
282 #if IKM_SUPPORT_LEGACY
283 if(legacy)
284 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
285 else
286 mr = ipc_kmsg_copyin_from_kernel(kmsg);
287 #else
288 mr = ipc_kmsg_copyin_from_kernel(kmsg);
289 #endif
290 if (mr != MACH_MSG_SUCCESS) {
291 ipc_kmsg_free(kmsg);
292 return mr;
293 }
294 mr = ipc_kmsg_send_always(kmsg);
295 if (mr != MACH_MSG_SUCCESS) {
296 ipc_kmsg_destroy(kmsg);
297 return mr;
298 }
299
300 for (;;) {
301 ipc_mqueue_t mqueue;
302
303 ip_lock(reply);
304 if ( !ip_active(reply)) {
305 ip_unlock(reply);
306 ipc_port_release(reply);
307 return MACH_RCV_PORT_DIED;
308 }
309 if (!self->active) {
310 ip_unlock(reply);
311 ipc_port_release(reply);
312 return MACH_RCV_INTERRUPTED;
313 }
314
315 assert(reply->ip_pset_count == 0);
316 mqueue = &reply->ip_messages;
317 ip_unlock(reply);
318
319 self->ith_continuation = (void (*)(mach_msg_return_t))0;
320
321 ipc_mqueue_receive(mqueue,
322 MACH_MSG_OPTION_NONE,
323 MACH_MSG_SIZE_MAX,
324 MACH_MSG_TIMEOUT_NONE,
325 THREAD_INTERRUPTIBLE);
326
327 mr = self->ith_state;
328 kmsg = self->ith_kmsg;
329 seqno = self->ith_seqno;
330
331 if (mr == MACH_MSG_SUCCESS)
332 {
333 break;
334 }
335
336 assert(mr == MACH_RCV_INTERRUPTED);
337
338 if (self->handlers) {
339 ipc_port_release(reply);
340 return(mr);
341 }
342 }
343 ipc_port_release(reply);
344
345 /*
346 * Check to see how much of the message/trailer can be received.
347 * We chose the maximum trailer that will fit, since we don't
348 * have options telling us which trailer elements the caller needed.
349 */
350 if (rcv_size >= kmsg->ikm_header->msgh_size) {
351 mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
352 ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
353
354 if (rcv_size >= kmsg->ikm_header->msgh_size + MAX_TRAILER_SIZE) {
355 /* Enough room for a maximum trailer */
356 trailer->msgh_trailer_size = MAX_TRAILER_SIZE;
357 }
358 else if (rcv_size < kmsg->ikm_header->msgh_size +
359 trailer->msgh_trailer_size) {
360 /* no room for even the basic (default) trailer */
361 trailer->msgh_trailer_size = 0;
362 }
363 assert(trailer->msgh_trailer_type == MACH_MSG_TRAILER_FORMAT_0);
364 rcv_size = kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size;
365 mr = MACH_MSG_SUCCESS;
366 } else {
367 mr = MACH_RCV_TOO_LARGE;
368 }
369
370
371 /*
372 * We want to preserve rights and memory in reply!
373 * We don't have to put them anywhere; just leave them
374 * as they are.
375 */
376 #if IKM_SUPPORT_LEGACY
377 if(legacy)
378 ipc_kmsg_copyout_to_kernel_legacy(kmsg, ipc_space_reply);
379 else
380 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
381 #else
382 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
383 #endif
384 ipc_kmsg_put_to_kernel(msg, kmsg, rcv_size);
385 return mr;
386 }
387
388
389 /************** These Calls are set up for kernel-loaded tasks/threads **************/
390
391 /*
392 * Routine: mach_msg_overwrite
393 * Purpose:
394 * Like mach_msg_overwrite_trap except that message buffers
395 * live in kernel space. Doesn't handle any options.
396 *
397 * This is used by in-kernel server threads to make
398 * kernel calls, to receive request messages, and
399 * to send reply messages.
400 * Conditions:
401 * Nothing locked.
402 * Returns:
403 */
404
405 mach_msg_return_t
406 mach_msg_overwrite(
407 mach_msg_header_t *msg,
408 mach_msg_option_t option,
409 mach_msg_size_t send_size,
410 mach_msg_size_t rcv_size,
411 mach_port_name_t rcv_name,
412 __unused mach_msg_timeout_t msg_timeout,
413 __unused mach_port_name_t notify,
414 __unused mach_msg_header_t *rcv_msg,
415 __unused mach_msg_size_t rcv_msg_size)
416 {
417 ipc_space_t space = current_space();
418 vm_map_t map = current_map();
419 ipc_kmsg_t kmsg;
420 mach_port_seqno_t seqno;
421 mach_msg_return_t mr;
422 mach_msg_max_trailer_t *trailer;
423
424 if (option & MACH_SEND_MSG) {
425 mach_msg_size_t msg_and_trailer_size;
426 mach_msg_max_trailer_t *max_trailer;
427
428 if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3))
429 return MACH_SEND_MSG_TOO_SMALL;
430
431 if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE)
432 return MACH_SEND_TOO_LARGE;
433
434 msg_and_trailer_size = send_size + MAX_TRAILER_SIZE;
435 kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
436
437 if (kmsg == IKM_NULL)
438 return MACH_SEND_NO_BUFFER;
439
440 (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size);
441
442 kmsg->ikm_header->msgh_size = send_size;
443
444 /*
445 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
446 * However, the internal size field of the trailer (msgh_trailer_size)
447 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
448 * the cases where no implicit data is requested.
449 */
450 max_trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size);
451 max_trailer->msgh_sender = current_thread()->task->sec_token;
452 max_trailer->msgh_audit = current_thread()->task->audit_token;
453 max_trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
454 max_trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
455
456 mr = ipc_kmsg_copyin(kmsg, space, map, FALSE);
457 if (mr != MACH_MSG_SUCCESS) {
458 ipc_kmsg_free(kmsg);
459 return mr;
460 }
461
462 do
463 mr = ipc_kmsg_send(kmsg, MACH_MSG_OPTION_NONE,
464 MACH_MSG_TIMEOUT_NONE);
465 while (mr == MACH_SEND_INTERRUPTED);
466 assert(mr == MACH_MSG_SUCCESS);
467 }
468
469 if (option & MACH_RCV_MSG) {
470 thread_t self = current_thread();
471
472 do {
473 ipc_object_t object;
474 ipc_mqueue_t mqueue;
475
476 mr = ipc_mqueue_copyin(space, rcv_name,
477 &mqueue, &object);
478 if (mr != MACH_MSG_SUCCESS)
479 return mr;
480 /* hold ref for object */
481
482 self->ith_continuation = (void (*)(mach_msg_return_t))0;
483 ipc_mqueue_receive(mqueue,
484 MACH_MSG_OPTION_NONE,
485 MACH_MSG_SIZE_MAX,
486 MACH_MSG_TIMEOUT_NONE,
487 THREAD_ABORTSAFE);
488 mr = self->ith_state;
489 kmsg = self->ith_kmsg;
490 seqno = self->ith_seqno;
491
492 ipc_object_release(object);
493
494 } while (mr == MACH_RCV_INTERRUPTED);
495 if (mr != MACH_MSG_SUCCESS)
496 return mr;
497
498 trailer = (mach_msg_max_trailer_t *)
499 ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
500 if (option & MACH_RCV_TRAILER_MASK) {
501 trailer->msgh_seqno = seqno;
502 trailer->msgh_context =
503 kmsg->ikm_header->msgh_remote_port->ip_context;
504 trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(option);
505 }
506
507 if (rcv_size < (kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size)) {
508 ipc_kmsg_copyout_dest(kmsg, space);
509 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
510 ipc_kmsg_free(kmsg);
511 return MACH_RCV_TOO_LARGE;
512 }
513
514 mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL);
515 if (mr != MACH_MSG_SUCCESS) {
516 if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
517 ipc_kmsg_put_to_kernel(msg, kmsg,
518 kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size);
519 } else {
520 ipc_kmsg_copyout_dest(kmsg, space);
521 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
522 ipc_kmsg_free(kmsg);
523 }
524
525 return mr;
526 }
527
528 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header,
529 kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size);
530 ipc_kmsg_free(kmsg);
531 }
532
533 return MACH_MSG_SUCCESS;
534 }
535
536 /*
537 * Routine: mig_get_reply_port
538 * Purpose:
539 * Called by client side interfaces living in the kernel
540 * to get a reply port.
541 */
542 mach_port_t
543 mig_get_reply_port(void)
544 {
545 return (MACH_PORT_NULL);
546 }
547
548 /*
549 * Routine: mig_dealloc_reply_port
550 * Purpose:
551 * Called by client side interfaces to get rid of a reply port.
552 */
553
554 void
555 mig_dealloc_reply_port(
556 __unused mach_port_t reply_port)
557 {
558 }
559
560 /*
561 * Routine: mig_put_reply_port
562 * Purpose:
563 * Called by client side interfaces after each RPC to
564 * let the client recycle the reply port if it wishes.
565 */
566 void
567 mig_put_reply_port(
568 __unused mach_port_t reply_port)
569 {
570 }
571
572 /*
573 * mig_strncpy.c - by Joshua Block
574 *
575 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
576 * OUGHT to do: Copies the (null terminated) string in src into dest, a
577 * buffer of length len. Assures that the copy is still null terminated
578 * and doesn't overflow the buffer, truncating the copy if necessary.
579 *
580 * Parameters:
581 *
582 * dest - Pointer to destination buffer.
583 *
584 * src - Pointer to source string.
585 *
586 * len - Length of destination buffer.
587 */
588 int
589 mig_strncpy(
590 char *dest,
591 const char *src,
592 int len)
593 {
594 int i = 0;
595
596 if (len > 0)
597 if (dest != NULL) {
598 if (src != NULL)
599 for (i=1; i<len; i++)
600 if (! (*dest++ = *src++))
601 return i;
602 *dest = '\0';
603 }
604 return i;
605 }
606
607 char *
608 mig_user_allocate(
609 vm_size_t size)
610 {
611 return (char *)kalloc(size);
612 }
613
614 void
615 mig_user_deallocate(
616 char *data,
617 vm_size_t size)
618 {
619 kfree(data, size);
620 }
621
622 /*
623 * Routine: mig_object_init
624 * Purpose:
625 * Initialize the base class portion of a MIG object. We
626 * will lazy init the port, so just clear it for now.
627 */
628 kern_return_t
629 mig_object_init(
630 mig_object_t mig_object,
631 const IMIGObject *interface)
632 {
633 if (mig_object == MIG_OBJECT_NULL)
634 return KERN_INVALID_ARGUMENT;
635 mig_object->pVtbl = (const IMIGObjectVtbl *)interface;
636 mig_object->port = MACH_PORT_NULL;
637 return KERN_SUCCESS;
638 }
639
640 /*
641 * Routine: mig_object_destroy
642 * Purpose:
643 * The object is being freed. This call lets us clean
644 * up any state we have have built up over the object's
645 * lifetime.
646 * Conditions:
647 * Since notifications and the port hold references on
648 * on the object, neither can exist when this is called.
649 * This is a good place to assert() that condition.
650 */
651 void
652 mig_object_destroy(
653 __assert_only mig_object_t mig_object)
654 {
655 assert(mig_object->port == MACH_PORT_NULL);
656 return;
657 }
658
659 /*
660 * Routine: mig_object_reference
661 * Purpose:
662 * Pure virtual helper to invoke the MIG object's AddRef
663 * method.
664 * Conditions:
665 * MIG object port may be locked.
666 */
667 void
668 mig_object_reference(
669 mig_object_t mig_object)
670 {
671 assert(mig_object != MIG_OBJECT_NULL);
672 mig_object->pVtbl->AddRef((IMIGObject *)mig_object);
673 }
674
675 /*
676 * Routine: mig_object_deallocate
677 * Purpose:
678 * Pure virtual helper to invoke the MIG object's Release
679 * method.
680 * Conditions:
681 * Nothing locked.
682 */
683 void
684 mig_object_deallocate(
685 mig_object_t mig_object)
686 {
687 assert(mig_object != MIG_OBJECT_NULL);
688 mig_object->pVtbl->Release((IMIGObject *)mig_object);
689 }
690
691 /*
692 * Routine: convert_mig_object_to_port [interface]
693 * Purpose:
694 * Base implementation of MIG outtrans routine to convert from
695 * a mig object reference to a new send right on the object's
696 * port. The object reference is consumed.
697 * Returns:
698 * IP_NULL - Null MIG object supplied
699 * Otherwise, a newly made send right for the port
700 * Conditions:
701 * Nothing locked.
702 */
703 ipc_port_t
704 convert_mig_object_to_port(
705 mig_object_t mig_object)
706 {
707 ipc_port_t port;
708 boolean_t deallocate = TRUE;
709
710 if (mig_object == MIG_OBJECT_NULL)
711 return IP_NULL;
712
713 port = mig_object->port;
714 while ((port == IP_NULL) ||
715 ((port = ipc_port_make_send(port)) == IP_NULL)) {
716 ipc_port_t previous;
717
718 /*
719 * Either the port was never set up, or it was just
720 * deallocated out from under us by the no-senders
721 * processing. In either case, we must:
722 * Attempt to make one
723 * Arrange for no senders
724 * Try to atomically register it with the object
725 * Destroy it if we are raced.
726 */
727 port = ipc_port_alloc_kernel();
728 ip_lock(port);
729 ipc_kobject_set_atomically(port,
730 (ipc_kobject_t) mig_object,
731 IKOT_MIG);
732
733 /* make a sonce right for the notification */
734 port->ip_sorights++;
735 ip_reference(port);
736
737 ipc_port_nsrequest(port, 1, port, &previous);
738 /* port unlocked */
739
740 assert(previous == IP_NULL);
741
742 if (OSCompareAndSwapPtr((void *)IP_NULL, (void *)port,
743 (void * volatile *)&mig_object->port)) {
744 deallocate = FALSE;
745 } else {
746 ipc_port_dealloc_kernel(port);
747 port = mig_object->port;
748 }
749 }
750
751 if (deallocate)
752 mig_object->pVtbl->Release((IMIGObject *)mig_object);
753
754 return (port);
755 }
756
757
758 /*
759 * Routine: convert_port_to_mig_object [interface]
760 * Purpose:
761 * Base implementation of MIG intrans routine to convert from
762 * an incoming port reference to a new reference on the
763 * underlying object. A new reference must be created, because
764 * the port's reference could go away asynchronously.
765 * Returns:
766 * NULL - Not an active MIG object port or iid not supported
767 * Otherwise, a reference to the underlying MIG interface
768 * Conditions:
769 * Nothing locked.
770 */
771 mig_object_t
772 convert_port_to_mig_object(
773 ipc_port_t port,
774 const MIGIID *iid)
775 {
776 mig_object_t mig_object;
777 void *ppv;
778
779 if (!IP_VALID(port))
780 return NULL;
781
782 ip_lock(port);
783 if (!ip_active(port) || (ip_kotype(port) != IKOT_MIG)) {
784 ip_unlock(port);
785 return NULL;
786 }
787
788 /*
789 * Our port points to some MIG object interface. Now
790 * query it to get a reference to the desired interface.
791 */
792 ppv = NULL;
793 mig_object = (mig_object_t)port->ip_kobject;
794 mig_object->pVtbl->QueryInterface((IMIGObject *)mig_object, iid, &ppv);
795 ip_unlock(port);
796 return (mig_object_t)ppv;
797 }
798
799 /*
800 * Routine: mig_object_no_senders [interface]
801 * Purpose:
802 * Base implementation of a no-senders notification handler
803 * for MIG objects. If there truly are no more senders, must
804 * destroy the port and drop its reference on the object.
805 * Returns:
806 * TRUE - port deallocate and reference dropped
807 * FALSE - more senders arrived, re-registered for notification
808 * Conditions:
809 * Nothing locked.
810 */
811
812 boolean_t
813 mig_object_no_senders(
814 ipc_port_t port,
815 mach_port_mscount_t mscount)
816 {
817 mig_object_t mig_object;
818
819 ip_lock(port);
820 if (port->ip_mscount > mscount) {
821 ipc_port_t previous;
822
823 /*
824 * Somebody created new send rights while the
825 * notification was in-flight. Just create a
826 * new send-once right and re-register with
827 * the new (higher) mscount threshold.
828 */
829 /* make a sonce right for the notification */
830 port->ip_sorights++;
831 ip_reference(port);
832 ipc_port_nsrequest(port, mscount, port, &previous);
833 /* port unlocked */
834
835 assert(previous == IP_NULL);
836 return (FALSE);
837 }
838
839 /*
840 * Clear the port pointer while we have it locked.
841 */
842 mig_object = (mig_object_t)port->ip_kobject;
843 mig_object->port = IP_NULL;
844
845 /*
846 * Bring the sequence number and mscount in
847 * line with ipc_port_destroy assertion.
848 */
849 port->ip_mscount = 0;
850 port->ip_messages.imq_seqno = 0;
851 ipc_port_destroy(port); /* releases lock */
852
853 /*
854 * Release the port's reference on the object.
855 */
856 mig_object->pVtbl->Release((IMIGObject *)mig_object);
857 return (TRUE);
858 }
859
860 /*
861 * Kernel implementation of the notification chain for MIG object
862 * is kept separate from the actual objects, since there are expected
863 * to be much fewer of them than actual objects.
864 *
865 * The implementation of this part of MIG objects is coming
866 * "Real Soon Now"(TM).
867 */