]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/kern/ipc_mig.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_mig.c
... / ...
CommitLineData
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
107mach_msg_return_t mach_msg_send_from_kernel(
108 mach_msg_header_t *msg,
109 mach_msg_size_t send_size);
110
111mach_msg_return_t
112mach_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 /*
130 * respect the thread's SEND_IMPORTANCE option to allow importance
131 * donation from the kernel-side of user threads
132 * (11938665 & 23925818)
133 */
134 mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
135 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
136 option &= ~MACH_SEND_NOIMPORTANCE;
137
138 mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
139 if (mr != MACH_MSG_SUCCESS) {
140 ipc_kmsg_destroy(kmsg);
141 }
142
143 return mr;
144}
145
146#endif /* IKM_SUPPORT_LEGACY */
147
148mach_msg_return_t
149mach_msg_send_from_kernel_proper(
150 mach_msg_header_t *msg,
151 mach_msg_size_t send_size)
152{
153 ipc_kmsg_t kmsg;
154 mach_msg_return_t mr;
155
156 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
157 if (mr != MACH_MSG_SUCCESS)
158 return mr;
159
160 mr = ipc_kmsg_copyin_from_kernel(kmsg);
161 if (mr != MACH_MSG_SUCCESS) {
162 ipc_kmsg_free(kmsg);
163 return mr;
164 }
165
166 /*
167 * respect the thread's SEND_IMPORTANCE option to force importance
168 * donation from the kernel-side of user threads
169 * (11938665 & 23925818)
170 */
171 mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
172 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
173 option &= ~MACH_SEND_NOIMPORTANCE;
174
175 mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
176 if (mr != MACH_MSG_SUCCESS) {
177 ipc_kmsg_destroy(kmsg);
178 }
179
180 return mr;
181}
182
183mach_msg_return_t
184mach_msg_send_from_kernel_with_options(
185 mach_msg_header_t *msg,
186 mach_msg_size_t send_size,
187 mach_msg_option_t option,
188 mach_msg_timeout_t timeout_val)
189{
190 ipc_kmsg_t kmsg;
191 mach_msg_return_t mr;
192
193 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
194 if (mr != MACH_MSG_SUCCESS)
195 return mr;
196
197 mr = ipc_kmsg_copyin_from_kernel(kmsg);
198 if (mr != MACH_MSG_SUCCESS) {
199 ipc_kmsg_free(kmsg);
200 return mr;
201 }
202
203 /*
204 * Until we are sure of its effects, we are disabling
205 * importance donation from the kernel-side of user
206 * threads in importance-donating tasks - unless the
207 * option to force importance donation is passed in,
208 * or the thread's SEND_IMPORTANCE option has been set.
209 * (11938665 & 23925818)
210 */
211 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
212 option &= ~MACH_SEND_NOIMPORTANCE;
213 else if ((option & MACH_SEND_IMPORTANCE) == 0)
214 option |= MACH_SEND_NOIMPORTANCE;
215
216 mr = ipc_kmsg_send(kmsg, option, timeout_val);
217
218 if (mr != MACH_MSG_SUCCESS) {
219 ipc_kmsg_destroy(kmsg);
220 }
221
222 return mr;
223}
224
225
226#if IKM_SUPPORT_LEGACY
227
228mach_msg_return_t
229mach_msg_send_from_kernel_with_options_legacy(
230 mach_msg_header_t *msg,
231 mach_msg_size_t send_size,
232 mach_msg_option_t option,
233 mach_msg_timeout_t timeout_val)
234{
235 ipc_kmsg_t kmsg;
236 mach_msg_return_t mr;
237
238 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
239 if (mr != MACH_MSG_SUCCESS)
240 return mr;
241
242 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
243 if (mr != MACH_MSG_SUCCESS) {
244 ipc_kmsg_free(kmsg);
245 return mr;
246 }
247
248 /*
249 * Until we are sure of its effects, we are disabling
250 * importance donation from the kernel-side of user
251 * threads in importance-donating tasks.
252 * (11938665 & 23925818)
253 */
254 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
255 option &= ~MACH_SEND_NOIMPORTANCE;
256 else
257 option |= MACH_SEND_NOIMPORTANCE;
258
259 mr = ipc_kmsg_send(kmsg, option, timeout_val);
260
261 if (mr != MACH_MSG_SUCCESS) {
262 ipc_kmsg_destroy(kmsg);
263 }
264
265 return mr;
266}
267
268#endif /* IKM_SUPPORT_LEGACY */
269
270/*
271 * Routine: mach_msg_rpc_from_kernel
272 * Purpose:
273 * Send a message from the kernel and receive a reply.
274 * Uses ith_rpc_reply for the reply port.
275 *
276 * This is used by the client side of KernelUser interfaces
277 * to implement Routines.
278 * Conditions:
279 * Nothing locked.
280 * Returns:
281 * MACH_MSG_SUCCESS Sent the message.
282 * MACH_RCV_PORT_DIED The reply port was deallocated.
283 */
284
285mach_msg_return_t mach_msg_rpc_from_kernel_body(mach_msg_header_t *msg,
286 mach_msg_size_t send_size, mach_msg_size_t rcv_size, boolean_t legacy);
287
288#if IKM_SUPPORT_LEGACY
289
290#undef mach_msg_rpc_from_kernel
291mach_msg_return_t
292mach_msg_rpc_from_kernel(
293 mach_msg_header_t *msg,
294 mach_msg_size_t send_size,
295 mach_msg_size_t rcv_size);
296
297mach_msg_return_t
298mach_msg_rpc_from_kernel(
299 mach_msg_header_t *msg,
300 mach_msg_size_t send_size,
301 mach_msg_size_t rcv_size)
302{
303 return mach_msg_rpc_from_kernel_body(msg, send_size, rcv_size, TRUE);
304}
305
306#endif /* IKM_SUPPORT_LEGACY */
307
308mach_msg_return_t
309mach_msg_rpc_from_kernel_proper(
310 mach_msg_header_t *msg,
311 mach_msg_size_t send_size,
312 mach_msg_size_t rcv_size)
313{
314 return mach_msg_rpc_from_kernel_body(msg, send_size, rcv_size, FALSE);
315}
316
317mach_msg_return_t
318mach_msg_rpc_from_kernel_body(
319 mach_msg_header_t *msg,
320 mach_msg_size_t send_size,
321 mach_msg_size_t rcv_size,
322#if !IKM_SUPPORT_LEGACY
323 __unused
324#endif
325 boolean_t legacy)
326{
327 thread_t self = current_thread();
328 ipc_port_t reply;
329 ipc_kmsg_t kmsg;
330 mach_port_seqno_t seqno;
331 mach_msg_return_t mr;
332
333 assert(msg->msgh_local_port == MACH_PORT_NULL);
334
335 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
336 if (mr != MACH_MSG_SUCCESS)
337 return mr;
338
339 reply = self->ith_rpc_reply;
340 if (reply == IP_NULL) {
341 reply = ipc_port_alloc_reply();
342 if ((reply == IP_NULL) ||
343 (self->ith_rpc_reply != IP_NULL))
344 panic("mach_msg_rpc_from_kernel");
345 self->ith_rpc_reply = reply;
346 }
347
348 /* insert send-once right for the reply port */
349 kmsg->ikm_header->msgh_local_port = reply;
350 kmsg->ikm_header->msgh_bits |=
351 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE);
352
353#if IKM_SUPPORT_LEGACY
354 if(legacy)
355 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
356 else
357 mr = ipc_kmsg_copyin_from_kernel(kmsg);
358#else
359 mr = ipc_kmsg_copyin_from_kernel(kmsg);
360#endif
361 if (mr != MACH_MSG_SUCCESS) {
362 ipc_kmsg_free(kmsg);
363 return mr;
364 }
365
366 /*
367 * respect the thread's SEND_IMPORTANCE option to force importance
368 * donation from the kernel-side of user threads
369 * (11938665 & 23925818)
370 */
371 mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
372 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
373 option &= ~MACH_SEND_NOIMPORTANCE;
374
375 mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
376 if (mr != MACH_MSG_SUCCESS) {
377 ipc_kmsg_destroy(kmsg);
378 return mr;
379 }
380
381 for (;;) {
382 ipc_mqueue_t mqueue;
383
384 assert(reply->ip_in_pset == 0);
385 assert(ip_active(reply));
386
387 /* JMM - why this check? */
388 if (!self->active && !self->inspection) {
389 ipc_port_dealloc_reply(reply);
390 self->ith_rpc_reply = IP_NULL;
391 return MACH_RCV_INTERRUPTED;
392 }
393
394 self->ith_continuation = (void (*)(mach_msg_return_t))0;
395
396 mqueue = &reply->ip_messages;
397 ipc_mqueue_receive(mqueue,
398 MACH_MSG_OPTION_NONE,
399 MACH_MSG_SIZE_MAX,
400 MACH_MSG_TIMEOUT_NONE,
401 THREAD_INTERRUPTIBLE);
402
403 mr = self->ith_state;
404 kmsg = self->ith_kmsg;
405 seqno = self->ith_seqno;
406
407 if (mr == MACH_MSG_SUCCESS)
408 {
409 break;
410 }
411
412 assert(mr == MACH_RCV_INTERRUPTED);
413
414 assert(reply == self->ith_rpc_reply);
415
416 if (self->ast & AST_APC) {
417 ipc_port_dealloc_reply(reply);
418 self->ith_rpc_reply = IP_NULL;
419 return(mr);
420 }
421 }
422
423 /*
424 * Check to see how much of the message/trailer can be received.
425 * We chose the maximum trailer that will fit, since we don't
426 * have options telling us which trailer elements the caller needed.
427 */
428 if (rcv_size >= kmsg->ikm_header->msgh_size) {
429 mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
430 ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
431
432 if (rcv_size >= kmsg->ikm_header->msgh_size + MAX_TRAILER_SIZE) {
433 /* Enough room for a maximum trailer */
434 trailer->msgh_trailer_size = MAX_TRAILER_SIZE;
435 }
436 else if (rcv_size < kmsg->ikm_header->msgh_size +
437 trailer->msgh_trailer_size) {
438 /* no room for even the basic (default) trailer */
439 trailer->msgh_trailer_size = 0;
440 }
441 assert(trailer->msgh_trailer_type == MACH_MSG_TRAILER_FORMAT_0);
442 rcv_size = kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size;
443 mr = MACH_MSG_SUCCESS;
444 } else {
445 mr = MACH_RCV_TOO_LARGE;
446 }
447
448
449 /*
450 * We want to preserve rights and memory in reply!
451 * We don't have to put them anywhere; just leave them
452 * as they are.
453 */
454#if IKM_SUPPORT_LEGACY
455 if(legacy)
456 ipc_kmsg_copyout_to_kernel_legacy(kmsg, ipc_space_reply);
457 else
458 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
459#else
460 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
461#endif
462 ipc_kmsg_put_to_kernel(msg, kmsg, rcv_size);
463 return mr;
464}
465
466
467/************** These Calls are set up for kernel-loaded tasks/threads **************/
468
469/*
470 * Routine: mach_msg_overwrite
471 * Purpose:
472 * Like mach_msg_overwrite_trap except that message buffers
473 * live in kernel space. Doesn't handle any options.
474 *
475 * This is used by in-kernel server threads to make
476 * kernel calls, to receive request messages, and
477 * to send reply messages.
478 * Conditions:
479 * Nothing locked.
480 * Returns:
481 */
482
483mach_msg_return_t
484mach_msg_overwrite(
485 mach_msg_header_t *msg,
486 mach_msg_option_t option,
487 mach_msg_size_t send_size,
488 mach_msg_size_t rcv_size,
489 mach_port_name_t rcv_name,
490 __unused mach_msg_timeout_t msg_timeout,
491 __unused mach_port_name_t notify,
492 __unused mach_msg_header_t *rcv_msg,
493 __unused mach_msg_size_t rcv_msg_size)
494{
495 ipc_space_t space = current_space();
496 vm_map_t map = current_map();
497 ipc_kmsg_t kmsg;
498 mach_port_seqno_t seqno;
499 mach_msg_return_t mr;
500 mach_msg_trailer_size_t trailer_size;
501
502 if (option & MACH_SEND_MSG) {
503 mach_msg_size_t msg_and_trailer_size;
504 mach_msg_max_trailer_t *max_trailer;
505
506 if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3))
507 return MACH_SEND_MSG_TOO_SMALL;
508
509 if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE)
510 return MACH_SEND_TOO_LARGE;
511
512 msg_and_trailer_size = send_size + MAX_TRAILER_SIZE;
513 kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
514
515 if (kmsg == IKM_NULL)
516 return MACH_SEND_NO_BUFFER;
517
518 (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size);
519
520 kmsg->ikm_header->msgh_size = send_size;
521
522 /*
523 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
524 * However, the internal size field of the trailer (msgh_trailer_size)
525 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
526 * the cases where no implicit data is requested.
527 */
528 max_trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size);
529 max_trailer->msgh_sender = current_thread()->task->sec_token;
530 max_trailer->msgh_audit = current_thread()->task->audit_token;
531 max_trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
532 max_trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
533
534 mr = ipc_kmsg_copyin(kmsg, space, map, &option);
535
536 if (mr != MACH_MSG_SUCCESS) {
537 ipc_kmsg_free(kmsg);
538 return mr;
539 }
540
541 do {
542 mr = ipc_kmsg_send(kmsg, MACH_MSG_OPTION_NONE, MACH_MSG_TIMEOUT_NONE);
543 } while (mr == MACH_SEND_INTERRUPTED);
544
545 assert(mr == MACH_MSG_SUCCESS);
546 }
547
548 if (option & MACH_RCV_MSG) {
549 thread_t self = current_thread();
550
551 do {
552 ipc_object_t object;
553 ipc_mqueue_t mqueue;
554
555 mr = ipc_mqueue_copyin(space, rcv_name,
556 &mqueue, &object);
557 if (mr != MACH_MSG_SUCCESS)
558 return mr;
559 /* hold ref for object */
560
561 self->ith_continuation = (void (*)(mach_msg_return_t))0;
562 ipc_mqueue_receive(mqueue,
563 MACH_MSG_OPTION_NONE,
564 MACH_MSG_SIZE_MAX,
565 MACH_MSG_TIMEOUT_NONE,
566 THREAD_ABORTSAFE);
567 mr = self->ith_state;
568 kmsg = self->ith_kmsg;
569 seqno = self->ith_seqno;
570
571 io_release(object);
572
573 } while (mr == MACH_RCV_INTERRUPTED);
574 if (mr != MACH_MSG_SUCCESS)
575 return mr;
576
577
578 trailer_size = ipc_kmsg_add_trailer(kmsg, space, option, current_thread(), seqno, TRUE,
579 kmsg->ikm_header->msgh_remote_port->ip_context);
580
581 if (rcv_size < (kmsg->ikm_header->msgh_size + trailer_size)) {
582 ipc_kmsg_copyout_dest(kmsg, space);
583 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
584 ipc_kmsg_free(kmsg);
585 return MACH_RCV_TOO_LARGE;
586 }
587
588 mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL, option);
589 if (mr != MACH_MSG_SUCCESS) {
590 if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
591 ipc_kmsg_put_to_kernel(msg, kmsg,
592 kmsg->ikm_header->msgh_size + trailer_size);
593 } else {
594 ipc_kmsg_copyout_dest(kmsg, space);
595 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
596 ipc_kmsg_free(kmsg);
597 }
598
599 return mr;
600 }
601
602 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header,
603 kmsg->ikm_header->msgh_size + trailer_size);
604 ipc_kmsg_free(kmsg);
605 }
606
607 return MACH_MSG_SUCCESS;
608}
609
610/*
611 * Routine: mig_get_reply_port
612 * Purpose:
613 * Called by client side interfaces living in the kernel
614 * to get a reply port.
615 */
616mach_port_t
617mig_get_reply_port(void)
618{
619 return (MACH_PORT_NULL);
620}
621
622/*
623 * Routine: mig_dealloc_reply_port
624 * Purpose:
625 * Called by client side interfaces to get rid of a reply port.
626 */
627
628void
629mig_dealloc_reply_port(
630 __unused mach_port_t reply_port)
631{
632}
633
634/*
635 * Routine: mig_put_reply_port
636 * Purpose:
637 * Called by client side interfaces after each RPC to
638 * let the client recycle the reply port if it wishes.
639 */
640void
641mig_put_reply_port(
642 __unused mach_port_t reply_port)
643{
644}
645
646/*
647 * mig_strncpy.c - by Joshua Block
648 *
649 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
650 * OUGHT to do: Copies the (null terminated) string in src into dest, a
651 * buffer of length len. Assures that the copy is still null terminated
652 * and doesn't overflow the buffer, truncating the copy if necessary.
653 *
654 * Parameters:
655 *
656 * dest - Pointer to destination buffer.
657 *
658 * src - Pointer to source string.
659 *
660 * len - Length of destination buffer.
661 */
662int
663mig_strncpy(
664 char *dest,
665 const char *src,
666 int len)
667{
668 int i = 0;
669
670 if (len > 0)
671 if (dest != NULL) {
672 if (src != NULL)
673 for (i=1; i<len; i++)
674 if (! (*dest++ = *src++))
675 return i;
676 *dest = '\0';
677 }
678 return i;
679}
680
681char *
682mig_user_allocate(
683 vm_size_t size)
684{
685 return (char *)kalloc(size);
686}
687
688void
689mig_user_deallocate(
690 char *data,
691 vm_size_t size)
692{
693 kfree(data, size);
694}
695
696/*
697 * Routine: mig_object_init
698 * Purpose:
699 * Initialize the base class portion of a MIG object. We
700 * will lazy init the port, so just clear it for now.
701 */
702kern_return_t
703mig_object_init(
704 mig_object_t mig_object,
705 const IMIGObject *interface)
706{
707 if (mig_object == MIG_OBJECT_NULL)
708 return KERN_INVALID_ARGUMENT;
709 mig_object->pVtbl = (const IMIGObjectVtbl *)interface;
710 mig_object->port = MACH_PORT_NULL;
711 return KERN_SUCCESS;
712}
713
714/*
715 * Routine: mig_object_destroy
716 * Purpose:
717 * The object is being freed. This call lets us clean
718 * up any state we have have built up over the object's
719 * lifetime.
720 * Conditions:
721 * Since notifications and the port hold references on
722 * on the object, neither can exist when this is called.
723 * This is a good place to assert() that condition.
724 */
725void
726mig_object_destroy(
727 __assert_only mig_object_t mig_object)
728{
729 assert(mig_object->port == MACH_PORT_NULL);
730 return;
731}
732
733/*
734 * Routine: mig_object_reference
735 * Purpose:
736 * Pure virtual helper to invoke the MIG object's AddRef
737 * method.
738 * Conditions:
739 * MIG object port may be locked.
740 */
741void
742mig_object_reference(
743 mig_object_t mig_object)
744{
745 assert(mig_object != MIG_OBJECT_NULL);
746 mig_object->pVtbl->AddRef((IMIGObject *)mig_object);
747}
748
749/*
750 * Routine: mig_object_deallocate
751 * Purpose:
752 * Pure virtual helper to invoke the MIG object's Release
753 * method.
754 * Conditions:
755 * Nothing locked.
756 */
757void
758mig_object_deallocate(
759 mig_object_t mig_object)
760{
761 assert(mig_object != MIG_OBJECT_NULL);
762 mig_object->pVtbl->Release((IMIGObject *)mig_object);
763}
764
765/*
766 * Routine: convert_mig_object_to_port [interface]
767 * Purpose:
768 * Base implementation of MIG outtrans routine to convert from
769 * a mig object reference to a new send right on the object's
770 * port. The object reference is consumed.
771 * Returns:
772 * IP_NULL - Null MIG object supplied
773 * Otherwise, a newly made send right for the port
774 * Conditions:
775 * Nothing locked.
776 */
777ipc_port_t
778convert_mig_object_to_port(
779 mig_object_t mig_object)
780{
781 ipc_port_t port;
782 boolean_t deallocate = TRUE;
783
784 if (mig_object == MIG_OBJECT_NULL)
785 return IP_NULL;
786
787 port = mig_object->port;
788 while ((port == IP_NULL) ||
789 ((port = ipc_port_make_send(port)) == IP_NULL)) {
790 ipc_port_t previous;
791
792 /*
793 * Either the port was never set up, or it was just
794 * deallocated out from under us by the no-senders
795 * processing. In either case, we must:
796 * Attempt to make one
797 * Arrange for no senders
798 * Try to atomically register it with the object
799 * Destroy it if we are raced.
800 */
801 port = ipc_port_alloc_kernel();
802 ip_lock(port);
803 ipc_kobject_set_atomically(port,
804 (ipc_kobject_t) mig_object,
805 IKOT_MIG);
806
807 /* make a sonce right for the notification */
808 port->ip_sorights++;
809 ip_reference(port);
810
811 ipc_port_nsrequest(port, 1, port, &previous);
812 /* port unlocked */
813
814 assert(previous == IP_NULL);
815
816 if (OSCompareAndSwapPtr((void *)IP_NULL, (void *)port,
817 (void * volatile *)&mig_object->port)) {
818 deallocate = FALSE;
819 } else {
820 ipc_port_dealloc_kernel(port);
821 port = mig_object->port;
822 }
823 }
824
825 if (deallocate)
826 mig_object->pVtbl->Release((IMIGObject *)mig_object);
827
828 return (port);
829}
830
831
832/*
833 * Routine: convert_port_to_mig_object [interface]
834 * Purpose:
835 * Base implementation of MIG intrans routine to convert from
836 * an incoming port reference to a new reference on the
837 * underlying object. A new reference must be created, because
838 * the port's reference could go away asynchronously.
839 * Returns:
840 * NULL - Not an active MIG object port or iid not supported
841 * Otherwise, a reference to the underlying MIG interface
842 * Conditions:
843 * Nothing locked.
844 */
845mig_object_t
846convert_port_to_mig_object(
847 ipc_port_t port,
848 const MIGIID *iid)
849{
850 mig_object_t mig_object;
851 void *ppv;
852
853 if (!IP_VALID(port))
854 return NULL;
855
856 ip_lock(port);
857 if (!ip_active(port) || (ip_kotype(port) != IKOT_MIG)) {
858 ip_unlock(port);
859 return NULL;
860 }
861
862 /*
863 * Our port points to some MIG object interface. Now
864 * query it to get a reference to the desired interface.
865 */
866 ppv = NULL;
867 mig_object = (mig_object_t)port->ip_kobject;
868 mig_object->pVtbl->QueryInterface((IMIGObject *)mig_object, iid, &ppv);
869 ip_unlock(port);
870 return (mig_object_t)ppv;
871}
872
873/*
874 * Routine: mig_object_no_senders [interface]
875 * Purpose:
876 * Base implementation of a no-senders notification handler
877 * for MIG objects. If there truly are no more senders, must
878 * destroy the port and drop its reference on the object.
879 * Returns:
880 * TRUE - port deallocate and reference dropped
881 * FALSE - more senders arrived, re-registered for notification
882 * Conditions:
883 * Nothing locked.
884 */
885
886boolean_t
887mig_object_no_senders(
888 ipc_port_t port,
889 mach_port_mscount_t mscount)
890{
891 mig_object_t mig_object;
892
893 ip_lock(port);
894 if (port->ip_mscount > mscount) {
895 ipc_port_t previous;
896
897 /*
898 * Somebody created new send rights while the
899 * notification was in-flight. Just create a
900 * new send-once right and re-register with
901 * the new (higher) mscount threshold.
902 */
903 /* make a sonce right for the notification */
904 port->ip_sorights++;
905 ip_reference(port);
906 ipc_port_nsrequest(port, mscount, port, &previous);
907 /* port unlocked */
908
909 assert(previous == IP_NULL);
910 return (FALSE);
911 }
912
913 /*
914 * Clear the port pointer while we have it locked.
915 */
916 mig_object = (mig_object_t)port->ip_kobject;
917 mig_object->port = IP_NULL;
918
919 /*
920 * Bring the sequence number and mscount in
921 * line with ipc_port_destroy assertion.
922 */
923 port->ip_mscount = 0;
924 port->ip_messages.imq_seqno = 0;
925 ipc_port_destroy(port); /* releases lock */
926
927 /*
928 * Release the port's reference on the object.
929 */
930 mig_object->pVtbl->Release((IMIGObject *)mig_object);
931 return (TRUE);
932}
933
934/*
935 * Kernel implementation of the notification chain for MIG object
936 * is kept separate from the actual objects, since there are expected
937 * to be much fewer of them than actual objects.
938 *
939 * The implementation of this part of MIG objects is coming
940 * "Real Soon Now"(TM).
941 */