2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 #include <mach/boolean.h>
60 #include <mach/port.h>
62 #include <mach/mig_errors.h>
63 #include <mach/mach_types.h>
64 #include <mach/mach_traps.h>
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>
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>
85 #include <libkern/OSAtomic.h>
88 * Routine: mach_msg_send_from_kernel
90 * Send a message from the kernel.
92 * This is used by the client side of KernelUser interfaces
93 * to implement SimpleRoutines. Currently, this includes
94 * memory_object messages.
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
104 #if IKM_SUPPORT_LEGACY
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
);
112 mach_msg_send_from_kernel(
113 mach_msg_header_t
*msg
,
114 mach_msg_size_t send_size
)
117 mach_msg_return_t mr
;
119 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
121 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
122 if (mr
!= MACH_MSG_SUCCESS
) {
123 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
127 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
128 if (mr
!= MACH_MSG_SUCCESS
) {
130 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
135 * respect the thread's SEND_IMPORTANCE option to allow importance
136 * donation from the kernel-side of user threads
137 * (11938665 & 23925818)
139 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
140 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
141 option
&= ~MACH_SEND_NOIMPORTANCE
;
143 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
144 if (mr
!= MACH_MSG_SUCCESS
) {
145 ipc_kmsg_destroy(kmsg
);
146 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
152 #endif /* IKM_SUPPORT_LEGACY */
155 mach_msg_send_from_kernel_proper(
156 mach_msg_header_t
*msg
,
157 mach_msg_size_t send_size
)
160 mach_msg_return_t mr
;
162 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
164 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
165 if (mr
!= MACH_MSG_SUCCESS
) {
166 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
170 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
171 if (mr
!= MACH_MSG_SUCCESS
) {
173 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
178 * respect the thread's SEND_IMPORTANCE option to force importance
179 * donation from the kernel-side of user threads
180 * (11938665 & 23925818)
182 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
183 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
184 option
&= ~MACH_SEND_NOIMPORTANCE
;
186 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
187 if (mr
!= MACH_MSG_SUCCESS
) {
188 ipc_kmsg_destroy(kmsg
);
189 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
196 mach_msg_send_from_kernel_with_options(
197 mach_msg_header_t
*msg
,
198 mach_msg_size_t send_size
,
199 mach_msg_option_t option
,
200 mach_msg_timeout_t timeout_val
)
203 mach_msg_return_t mr
;
205 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
207 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
208 if (mr
!= MACH_MSG_SUCCESS
) {
209 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
213 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
214 if (mr
!= MACH_MSG_SUCCESS
) {
216 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
221 * Until we are sure of its effects, we are disabling
222 * importance donation from the kernel-side of user
223 * threads in importance-donating tasks - unless the
224 * option to force importance donation is passed in,
225 * or the thread's SEND_IMPORTANCE option has been set.
226 * (11938665 & 23925818)
228 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
229 option
&= ~MACH_SEND_NOIMPORTANCE
;
230 else if ((option
& MACH_SEND_IMPORTANCE
) == 0)
231 option
|= MACH_SEND_NOIMPORTANCE
;
233 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
235 if (mr
!= MACH_MSG_SUCCESS
) {
236 ipc_kmsg_destroy(kmsg
);
237 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
244 #if IKM_SUPPORT_LEGACY
247 mach_msg_send_from_kernel_with_options_legacy(
248 mach_msg_header_t
*msg
,
249 mach_msg_size_t send_size
,
250 mach_msg_option_t option
,
251 mach_msg_timeout_t timeout_val
)
254 mach_msg_return_t mr
;
256 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
258 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
259 if (mr
!= MACH_MSG_SUCCESS
) {
260 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
264 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
265 if (mr
!= MACH_MSG_SUCCESS
) {
267 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
272 * Until we are sure of its effects, we are disabling
273 * importance donation from the kernel-side of user
274 * threads in importance-donating tasks.
275 * (11938665 & 23925818)
277 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
278 option
&= ~MACH_SEND_NOIMPORTANCE
;
280 option
|= MACH_SEND_NOIMPORTANCE
;
282 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
284 if (mr
!= MACH_MSG_SUCCESS
) {
285 ipc_kmsg_destroy(kmsg
);
286 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
292 #endif /* IKM_SUPPORT_LEGACY */
295 * Routine: mach_msg_rpc_from_kernel
297 * Send a message from the kernel and receive a reply.
298 * Uses ith_rpc_reply for the reply port.
300 * This is used by the client side of KernelUser interfaces
301 * to implement Routines.
305 * MACH_MSG_SUCCESS Sent the message.
306 * MACH_RCV_PORT_DIED The reply port was deallocated.
309 mach_msg_return_t
mach_msg_rpc_from_kernel_body(mach_msg_header_t
*msg
,
310 mach_msg_size_t send_size
, mach_msg_size_t rcv_size
, boolean_t legacy
);
312 #if IKM_SUPPORT_LEGACY
314 #undef mach_msg_rpc_from_kernel
316 mach_msg_rpc_from_kernel(
317 mach_msg_header_t
*msg
,
318 mach_msg_size_t send_size
,
319 mach_msg_size_t rcv_size
);
322 mach_msg_rpc_from_kernel(
323 mach_msg_header_t
*msg
,
324 mach_msg_size_t send_size
,
325 mach_msg_size_t rcv_size
)
327 return mach_msg_rpc_from_kernel_body(msg
, send_size
, rcv_size
, TRUE
);
330 #endif /* IKM_SUPPORT_LEGACY */
333 mach_msg_rpc_from_kernel_proper(
334 mach_msg_header_t
*msg
,
335 mach_msg_size_t send_size
,
336 mach_msg_size_t rcv_size
)
338 return mach_msg_rpc_from_kernel_body(msg
, send_size
, rcv_size
, FALSE
);
342 mach_msg_rpc_from_kernel_body(
343 mach_msg_header_t
*msg
,
344 mach_msg_size_t send_size
,
345 mach_msg_size_t rcv_size
,
346 #if !IKM_SUPPORT_LEGACY
351 thread_t self
= current_thread();
354 mach_port_seqno_t seqno
;
355 mach_msg_return_t mr
;
357 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
359 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
361 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
362 if (mr
!= MACH_MSG_SUCCESS
) {
363 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
367 reply
= self
->ith_rpc_reply
;
368 if (reply
== IP_NULL
) {
369 reply
= ipc_port_alloc_reply();
370 if ((reply
== IP_NULL
) ||
371 (self
->ith_rpc_reply
!= IP_NULL
))
372 panic("mach_msg_rpc_from_kernel");
373 self
->ith_rpc_reply
= reply
;
376 /* insert send-once right for the reply port */
377 kmsg
->ikm_header
->msgh_local_port
= reply
;
378 kmsg
->ikm_header
->msgh_bits
|=
379 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
381 #if IKM_SUPPORT_LEGACY
383 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
385 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
387 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
389 if (mr
!= MACH_MSG_SUCCESS
) {
391 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
396 * respect the thread's SEND_IMPORTANCE option to force importance
397 * donation from the kernel-side of user threads
398 * (11938665 & 23925818)
400 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
401 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
402 option
&= ~MACH_SEND_NOIMPORTANCE
;
404 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
405 if (mr
!= MACH_MSG_SUCCESS
) {
406 ipc_kmsg_destroy(kmsg
);
407 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
414 assert(reply
->ip_in_pset
== 0);
415 assert(ip_active(reply
));
417 /* JMM - why this check? */
418 if (!self
->active
&& !self
->inspection
) {
419 ipc_port_dealloc_reply(reply
);
420 self
->ith_rpc_reply
= IP_NULL
;
421 return MACH_RCV_INTERRUPTED
;
424 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
426 mqueue
= &reply
->ip_messages
;
427 ipc_mqueue_receive(mqueue
,
428 MACH_MSG_OPTION_NONE
,
430 MACH_MSG_TIMEOUT_NONE
,
431 THREAD_INTERRUPTIBLE
);
433 mr
= self
->ith_state
;
434 kmsg
= self
->ith_kmsg
;
435 seqno
= self
->ith_seqno
;
437 if (mr
== MACH_MSG_SUCCESS
)
442 assert(mr
== MACH_RCV_INTERRUPTED
);
444 assert(reply
== self
->ith_rpc_reply
);
446 if (self
->ast
& AST_APC
) {
447 ipc_port_dealloc_reply(reply
);
448 self
->ith_rpc_reply
= IP_NULL
;
454 * Check to see how much of the message/trailer can be received.
455 * We chose the maximum trailer that will fit, since we don't
456 * have options telling us which trailer elements the caller needed.
458 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
) {
459 mach_msg_format_0_trailer_t
*trailer
= (mach_msg_format_0_trailer_t
*)
460 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
462 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
+ MAX_TRAILER_SIZE
) {
463 /* Enough room for a maximum trailer */
464 trailer
->msgh_trailer_size
= MAX_TRAILER_SIZE
;
466 else if (rcv_size
< kmsg
->ikm_header
->msgh_size
+
467 trailer
->msgh_trailer_size
) {
468 /* no room for even the basic (default) trailer */
469 trailer
->msgh_trailer_size
= 0;
471 assert(trailer
->msgh_trailer_type
== MACH_MSG_TRAILER_FORMAT_0
);
472 rcv_size
= kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
;
473 mr
= MACH_MSG_SUCCESS
;
475 mr
= MACH_RCV_TOO_LARGE
;
480 * We want to preserve rights and memory in reply!
481 * We don't have to put them anywhere; just leave them
484 #if IKM_SUPPORT_LEGACY
486 ipc_kmsg_copyout_to_kernel_legacy(kmsg
, ipc_space_reply
);
488 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
490 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
492 ipc_kmsg_put_to_kernel(msg
, kmsg
, rcv_size
);
497 /************** These Calls are set up for kernel-loaded tasks/threads **************/
500 * Routine: mach_msg_overwrite
502 * Like mach_msg_overwrite_trap except that message buffers
503 * live in kernel space. Doesn't handle any options.
505 * This is used by in-kernel server threads to make
506 * kernel calls, to receive request messages, and
507 * to send reply messages.
515 mach_msg_header_t
*msg
,
516 mach_msg_option_t option
,
517 mach_msg_size_t send_size
,
518 mach_msg_size_t rcv_size
,
519 mach_port_name_t rcv_name
,
520 __unused mach_msg_timeout_t msg_timeout
,
521 mach_msg_priority_t override
,
522 __unused mach_msg_header_t
*rcv_msg
,
523 __unused mach_msg_size_t rcv_msg_size
)
525 ipc_space_t space
= current_space();
526 vm_map_t map
= current_map();
528 mach_port_seqno_t seqno
;
529 mach_msg_return_t mr
;
530 mach_msg_trailer_size_t trailer_size
;
532 if (option
& MACH_SEND_MSG
) {
533 mach_msg_size_t msg_and_trailer_size
;
534 mach_msg_max_trailer_t
*max_trailer
;
536 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
537 return MACH_SEND_MSG_TOO_SMALL
;
539 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
540 return MACH_SEND_TOO_LARGE
;
542 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
544 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
545 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
547 if (kmsg
== IKM_NULL
) {
548 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_SEND_NO_BUFFER
);
549 return MACH_SEND_NO_BUFFER
;
552 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_LINK
) | DBG_FUNC_NONE
,
553 (uintptr_t)0, /* this should only be called from the kernel! */
554 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
557 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
559 kmsg
->ikm_header
->msgh_size
= send_size
;
562 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
563 * However, the internal size field of the trailer (msgh_trailer_size)
564 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
565 * the cases where no implicit data is requested.
567 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
568 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
569 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
570 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
571 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
573 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, override
, &option
);
575 if (mr
!= MACH_MSG_SUCCESS
) {
577 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
582 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
, MACH_MSG_TIMEOUT_NONE
);
583 } while (mr
== MACH_SEND_INTERRUPTED
);
585 assert(mr
== MACH_MSG_SUCCESS
);
588 if (option
& MACH_RCV_MSG
) {
589 thread_t self
= current_thread();
595 mr
= ipc_mqueue_copyin(space
, rcv_name
,
597 if (mr
!= MACH_MSG_SUCCESS
)
599 /* hold ref for object */
601 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
602 ipc_mqueue_receive(mqueue
,
603 MACH_MSG_OPTION_NONE
,
605 MACH_MSG_TIMEOUT_NONE
,
607 mr
= self
->ith_state
;
608 kmsg
= self
->ith_kmsg
;
609 seqno
= self
->ith_seqno
;
613 } while (mr
== MACH_RCV_INTERRUPTED
);
615 if (mr
!= MACH_MSG_SUCCESS
)
618 trailer_size
= ipc_kmsg_add_trailer(kmsg
, space
, option
, current_thread(), seqno
, TRUE
,
619 kmsg
->ikm_header
->msgh_remote_port
->ip_context
);
621 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer_size
)) {
622 ipc_kmsg_copyout_dest(kmsg
, space
);
623 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
625 return MACH_RCV_TOO_LARGE
;
628 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_MSG_BODY_NULL
, option
);
629 if (mr
!= MACH_MSG_SUCCESS
) {
630 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
631 ipc_kmsg_put_to_kernel(msg
, kmsg
,
632 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
634 ipc_kmsg_copyout_dest(kmsg
, space
);
635 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
642 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
643 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
647 return MACH_MSG_SUCCESS
;
651 * Routine: mig_get_reply_port
653 * Called by client side interfaces living in the kernel
654 * to get a reply port.
657 mig_get_reply_port(void)
659 return (MACH_PORT_NULL
);
663 * Routine: mig_dealloc_reply_port
665 * Called by client side interfaces to get rid of a reply port.
669 mig_dealloc_reply_port(
670 __unused mach_port_t reply_port
)
675 * Routine: mig_put_reply_port
677 * Called by client side interfaces after each RPC to
678 * let the client recycle the reply port if it wishes.
682 __unused mach_port_t reply_port
)
687 * mig_strncpy.c - by Joshua Block
689 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
690 * OUGHT to do: Copies the (null terminated) string in src into dest, a
691 * buffer of length len. Assures that the copy is still null terminated
692 * and doesn't overflow the buffer, truncating the copy if necessary.
696 * dest - Pointer to destination buffer.
698 * src - Pointer to source string.
700 * len - Length of destination buffer.
713 for (i
=1; i
<len
; i
++)
714 if (! (*dest
++ = *src
++))
722 * mig_strncpy_zerofill -- Bounded string copy. Does what the
723 * library routine strncpy OUGHT to do: Copies the (null terminated)
724 * string in src into dest, a buffer of length len. Assures that
725 * the copy is still null terminated and doesn't overflow the buffer,
726 * truncating the copy if necessary. If the string in src is smaller
727 * than given length len, it will zero fill the remaining bytes in dest.
731 * dest - Pointer to destination buffer.
733 * src - Pointer to source string.
735 * len - Length of destination buffer.
738 mig_strncpy_zerofill(
744 boolean_t terminated
= FALSE
;
747 if (len
<= 0 || dest
== NULL
) {
755 for (i
= 1; i
< len
; i
++) {
757 if (!(*dest
++ = *src
++)) {
778 return (char *)kalloc(size
);
790 * Routine: mig_object_init
792 * Initialize the base class portion of a MIG object. We
793 * will lazy init the port, so just clear it for now.
797 mig_object_t mig_object
,
798 const IMIGObject
*interface
)
800 if (mig_object
== MIG_OBJECT_NULL
)
801 return KERN_INVALID_ARGUMENT
;
802 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
803 mig_object
->port
= MACH_PORT_NULL
;
808 * Routine: mig_object_destroy
810 * The object is being freed. This call lets us clean
811 * up any state we have have built up over the object's
814 * Since notifications and the port hold references on
815 * on the object, neither can exist when this is called.
816 * This is a good place to assert() that condition.
820 __assert_only mig_object_t mig_object
)
822 assert(mig_object
->port
== MACH_PORT_NULL
);
827 * Routine: mig_object_reference
829 * Pure virtual helper to invoke the MIG object's AddRef
832 * MIG object port may be locked.
835 mig_object_reference(
836 mig_object_t mig_object
)
838 assert(mig_object
!= MIG_OBJECT_NULL
);
839 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
843 * Routine: mig_object_deallocate
845 * Pure virtual helper to invoke the MIG object's Release
851 mig_object_deallocate(
852 mig_object_t mig_object
)
854 assert(mig_object
!= MIG_OBJECT_NULL
);
855 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
859 * Routine: convert_mig_object_to_port [interface]
861 * Base implementation of MIG outtrans routine to convert from
862 * a mig object reference to a new send right on the object's
863 * port. The object reference is consumed.
865 * IP_NULL - Null MIG object supplied
866 * Otherwise, a newly made send right for the port
871 convert_mig_object_to_port(
872 mig_object_t mig_object
)
875 boolean_t deallocate
= TRUE
;
877 if (mig_object
== MIG_OBJECT_NULL
)
880 port
= mig_object
->port
;
881 while ((port
== IP_NULL
) ||
882 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
886 * Either the port was never set up, or it was just
887 * deallocated out from under us by the no-senders
888 * processing. In either case, we must:
889 * Attempt to make one
890 * Arrange for no senders
891 * Try to atomically register it with the object
892 * Destroy it if we are raced.
894 port
= ipc_port_alloc_kernel();
896 ipc_kobject_set_atomically(port
,
897 (ipc_kobject_t
) mig_object
,
900 /* make a sonce right for the notification */
904 ipc_port_nsrequest(port
, 1, port
, &previous
);
907 assert(previous
== IP_NULL
);
909 if (OSCompareAndSwapPtr((void *)IP_NULL
, (void *)port
,
910 (void * volatile *)&mig_object
->port
)) {
913 ipc_port_dealloc_kernel(port
);
914 port
= mig_object
->port
;
919 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
926 * Routine: convert_port_to_mig_object [interface]
928 * Base implementation of MIG intrans routine to convert from
929 * an incoming port reference to a new reference on the
930 * underlying object. A new reference must be created, because
931 * the port's reference could go away asynchronously.
933 * NULL - Not an active MIG object port or iid not supported
934 * Otherwise, a reference to the underlying MIG interface
939 convert_port_to_mig_object(
943 mig_object_t mig_object
;
950 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
956 * Our port points to some MIG object interface. Now
957 * query it to get a reference to the desired interface.
960 mig_object
= (mig_object_t
)port
->ip_kobject
;
961 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
963 return (mig_object_t
)ppv
;
967 * Routine: mig_object_no_senders [interface]
969 * Base implementation of a no-senders notification handler
970 * for MIG objects. If there truly are no more senders, must
971 * destroy the port and drop its reference on the object.
973 * TRUE - port deallocate and reference dropped
974 * FALSE - more senders arrived, re-registered for notification
980 mig_object_no_senders(
982 mach_port_mscount_t mscount
)
984 mig_object_t mig_object
;
987 if (port
->ip_mscount
> mscount
) {
991 * Somebody created new send rights while the
992 * notification was in-flight. Just create a
993 * new send-once right and re-register with
994 * the new (higher) mscount threshold.
996 /* make a sonce right for the notification */
999 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
1002 assert(previous
== IP_NULL
);
1007 * Clear the port pointer while we have it locked.
1009 mig_object
= (mig_object_t
)port
->ip_kobject
;
1010 mig_object
->port
= IP_NULL
;
1013 * Bring the sequence number and mscount in
1014 * line with ipc_port_destroy assertion.
1016 port
->ip_mscount
= 0;
1017 port
->ip_messages
.imq_seqno
= 0;
1018 ipc_port_destroy(port
); /* releases lock */
1021 * Release the port's reference on the object.
1023 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
1028 * Kernel implementation of the notification chain for MIG object
1029 * is kept separate from the actual objects, since there are expected
1030 * to be much fewer of them than actual objects.
1032 * The implementation of this part of MIG objects is coming
1033 * "Real Soon Now"(TM).