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
& 3) ||
537 send_size
< sizeof(mach_msg_header_t
) ||
538 (send_size
< sizeof(mach_msg_body_t
) && (msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)))
539 return MACH_SEND_MSG_TOO_SMALL
;
541 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
542 return MACH_SEND_TOO_LARGE
;
544 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
546 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
547 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
549 if (kmsg
== IKM_NULL
) {
550 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_SEND_NO_BUFFER
);
551 return MACH_SEND_NO_BUFFER
;
554 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_LINK
) | DBG_FUNC_NONE
,
555 (uintptr_t)0, /* this should only be called from the kernel! */
556 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
559 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
561 kmsg
->ikm_header
->msgh_size
= send_size
;
564 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
565 * However, the internal size field of the trailer (msgh_trailer_size)
566 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
567 * the cases where no implicit data is requested.
569 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
570 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
571 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
572 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
573 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
575 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, override
, &option
);
577 if (mr
!= MACH_MSG_SUCCESS
) {
579 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
584 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
, MACH_MSG_TIMEOUT_NONE
);
585 } while (mr
== MACH_SEND_INTERRUPTED
);
587 assert(mr
== MACH_MSG_SUCCESS
);
590 if (option
& MACH_RCV_MSG
) {
591 thread_t self
= current_thread();
597 mr
= ipc_mqueue_copyin(space
, rcv_name
,
599 if (mr
!= MACH_MSG_SUCCESS
)
601 /* hold ref for object */
603 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
604 ipc_mqueue_receive(mqueue
,
605 MACH_MSG_OPTION_NONE
,
607 MACH_MSG_TIMEOUT_NONE
,
609 mr
= self
->ith_state
;
610 kmsg
= self
->ith_kmsg
;
611 seqno
= self
->ith_seqno
;
615 } while (mr
== MACH_RCV_INTERRUPTED
);
617 if (mr
!= MACH_MSG_SUCCESS
)
620 trailer_size
= ipc_kmsg_add_trailer(kmsg
, space
, option
, current_thread(), seqno
, TRUE
,
621 kmsg
->ikm_header
->msgh_remote_port
->ip_context
);
623 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer_size
)) {
624 ipc_kmsg_copyout_dest(kmsg
, space
);
625 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
627 return MACH_RCV_TOO_LARGE
;
630 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_MSG_BODY_NULL
, option
);
631 if (mr
!= MACH_MSG_SUCCESS
) {
632 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
633 ipc_kmsg_put_to_kernel(msg
, kmsg
,
634 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
636 ipc_kmsg_copyout_dest(kmsg
, space
);
637 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
644 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
645 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
649 return MACH_MSG_SUCCESS
;
653 * Routine: mig_get_reply_port
655 * Called by client side interfaces living in the kernel
656 * to get a reply port.
659 mig_get_reply_port(void)
661 return (MACH_PORT_NULL
);
665 * Routine: mig_dealloc_reply_port
667 * Called by client side interfaces to get rid of a reply port.
671 mig_dealloc_reply_port(
672 __unused mach_port_t reply_port
)
677 * Routine: mig_put_reply_port
679 * Called by client side interfaces after each RPC to
680 * let the client recycle the reply port if it wishes.
684 __unused mach_port_t reply_port
)
689 * mig_strncpy.c - by Joshua Block
691 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
692 * OUGHT to do: Copies the (null terminated) string in src into dest, a
693 * buffer of length len. Assures that the copy is still null terminated
694 * and doesn't overflow the buffer, truncating the copy if necessary.
698 * dest - Pointer to destination buffer.
700 * src - Pointer to source string.
702 * len - Length of destination buffer.
715 for (i
=1; i
<len
; i
++)
716 if (! (*dest
++ = *src
++))
724 * mig_strncpy_zerofill -- Bounded string copy. Does what the
725 * library routine strncpy OUGHT to do: Copies the (null terminated)
726 * string in src into dest, a buffer of length len. Assures that
727 * the copy is still null terminated and doesn't overflow the buffer,
728 * truncating the copy if necessary. If the string in src is smaller
729 * than given length len, it will zero fill the remaining bytes in dest.
733 * dest - Pointer to destination buffer.
735 * src - Pointer to source string.
737 * len - Length of destination buffer.
740 mig_strncpy_zerofill(
746 boolean_t terminated
= FALSE
;
749 if (len
<= 0 || dest
== NULL
) {
757 for (i
= 1; i
< len
; i
++) {
759 if (!(*dest
++ = *src
++)) {
780 return (char *)kalloc(size
);
792 * Routine: mig_object_init
794 * Initialize the base class portion of a MIG object. We
795 * will lazy init the port, so just clear it for now.
799 mig_object_t mig_object
,
800 const IMIGObject
*interface
)
802 if (mig_object
== MIG_OBJECT_NULL
)
803 return KERN_INVALID_ARGUMENT
;
804 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
805 mig_object
->port
= MACH_PORT_NULL
;
810 * Routine: mig_object_destroy
812 * The object is being freed. This call lets us clean
813 * up any state we have have built up over the object's
816 * Since notifications and the port hold references on
817 * on the object, neither can exist when this is called.
818 * This is a good place to assert() that condition.
822 __assert_only mig_object_t mig_object
)
824 assert(mig_object
->port
== MACH_PORT_NULL
);
829 * Routine: mig_object_reference
831 * Pure virtual helper to invoke the MIG object's AddRef
834 * MIG object port may be locked.
837 mig_object_reference(
838 mig_object_t mig_object
)
840 assert(mig_object
!= MIG_OBJECT_NULL
);
841 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
845 * Routine: mig_object_deallocate
847 * Pure virtual helper to invoke the MIG object's Release
853 mig_object_deallocate(
854 mig_object_t mig_object
)
856 assert(mig_object
!= MIG_OBJECT_NULL
);
857 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
861 * Routine: convert_mig_object_to_port [interface]
863 * Base implementation of MIG outtrans routine to convert from
864 * a mig object reference to a new send right on the object's
865 * port. The object reference is consumed.
867 * IP_NULL - Null MIG object supplied
868 * Otherwise, a newly made send right for the port
873 convert_mig_object_to_port(
874 mig_object_t mig_object
)
877 boolean_t deallocate
= TRUE
;
879 if (mig_object
== MIG_OBJECT_NULL
)
882 port
= mig_object
->port
;
883 while ((port
== IP_NULL
) ||
884 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
888 * Either the port was never set up, or it was just
889 * deallocated out from under us by the no-senders
890 * processing. In either case, we must:
891 * Attempt to make one
892 * Arrange for no senders
893 * Try to atomically register it with the object
894 * Destroy it if we are raced.
896 port
= ipc_port_alloc_kernel();
898 ipc_kobject_set_atomically(port
,
899 (ipc_kobject_t
) mig_object
,
902 /* make a sonce right for the notification */
906 ipc_port_nsrequest(port
, 1, port
, &previous
);
909 assert(previous
== IP_NULL
);
911 if (OSCompareAndSwapPtr((void *)IP_NULL
, (void *)port
,
912 (void * volatile *)&mig_object
->port
)) {
915 ipc_port_dealloc_kernel(port
);
916 port
= mig_object
->port
;
921 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
928 * Routine: convert_port_to_mig_object [interface]
930 * Base implementation of MIG intrans routine to convert from
931 * an incoming port reference to a new reference on the
932 * underlying object. A new reference must be created, because
933 * the port's reference could go away asynchronously.
935 * NULL - Not an active MIG object port or iid not supported
936 * Otherwise, a reference to the underlying MIG interface
941 convert_port_to_mig_object(
945 mig_object_t mig_object
;
952 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
958 * Our port points to some MIG object interface. Now
959 * query it to get a reference to the desired interface.
962 mig_object
= (mig_object_t
)port
->ip_kobject
;
963 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
965 return (mig_object_t
)ppv
;
969 * Routine: mig_object_no_senders [interface]
971 * Base implementation of a no-senders notification handler
972 * for MIG objects. If there truly are no more senders, must
973 * destroy the port and drop its reference on the object.
975 * TRUE - port deallocate and reference dropped
976 * FALSE - more senders arrived, re-registered for notification
982 mig_object_no_senders(
984 mach_port_mscount_t mscount
)
986 mig_object_t mig_object
;
989 if (port
->ip_mscount
> mscount
) {
993 * Somebody created new send rights while the
994 * notification was in-flight. Just create a
995 * new send-once right and re-register with
996 * the new (higher) mscount threshold.
998 /* make a sonce right for the notification */
1001 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
1004 assert(previous
== IP_NULL
);
1009 * Clear the port pointer while we have it locked.
1011 mig_object
= (mig_object_t
)port
->ip_kobject
;
1012 mig_object
->port
= IP_NULL
;
1015 * Bring the sequence number and mscount in
1016 * line with ipc_port_destroy assertion.
1018 port
->ip_mscount
= 0;
1019 port
->ip_messages
.imq_seqno
= 0;
1020 ipc_port_destroy(port
); /* releases lock */
1023 * Release the port's reference on the object.
1025 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
1030 * Kernel implementation of the notification chain for MIG object
1031 * is kept separate from the actual objects, since there are expected
1032 * to be much fewer of them than actual objects.
1034 * The implementation of this part of MIG objects is coming
1035 * "Real Soon Now"(TM).