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 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
120 if (mr
!= MACH_MSG_SUCCESS
)
123 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
124 if (mr
!= MACH_MSG_SUCCESS
) {
129 mr
= ipc_kmsg_send_always(kmsg
);
130 if (mr
!= MACH_MSG_SUCCESS
) {
131 ipc_kmsg_destroy(kmsg
);
137 #endif /* IKM_SUPPORT_LEGACY */
140 mach_msg_send_from_kernel_proper(
141 mach_msg_header_t
*msg
,
142 mach_msg_size_t send_size
)
145 mach_msg_return_t mr
;
147 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
148 if (mr
!= MACH_MSG_SUCCESS
)
151 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
152 if (mr
!= MACH_MSG_SUCCESS
) {
157 mr
= ipc_kmsg_send_always(kmsg
);
158 if (mr
!= MACH_MSG_SUCCESS
) {
159 ipc_kmsg_destroy(kmsg
);
165 #if IKM_SUPPORT_LEGACY
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
)
175 mach_msg_return_t mr
;
177 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
178 if (mr
!= MACH_MSG_SUCCESS
)
181 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
182 if (mr
!= MACH_MSG_SUCCESS
) {
187 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
188 if (mr
!= MACH_MSG_SUCCESS
) {
189 ipc_kmsg_destroy(kmsg
);
195 #endif /* IKM_SUPPORT_LEGACY */
198 * Routine: mach_msg_rpc_from_kernel
200 * Send a message from the kernel and receive a reply.
201 * Uses ith_rpc_reply for the reply port.
203 * This is used by the client side of KernelUser interfaces
204 * to implement Routines.
208 * MACH_MSG_SUCCESS Sent the message.
209 * MACH_RCV_PORT_DIED The reply port was deallocated.
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
);
215 #if IKM_SUPPORT_LEGACY
217 #undef mach_msg_rpc_from_kernel
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
);
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
)
230 return mach_msg_rpc_from_kernel_body(msg
, send_size
, rcv_size
, TRUE
);
233 #endif /* IKM_SUPPORT_LEGACY */
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
)
241 return mach_msg_rpc_from_kernel_body(msg
, send_size
, rcv_size
, FALSE
);
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
254 thread_t self
= current_thread();
257 mach_port_seqno_t seqno
;
258 mach_msg_return_t mr
;
260 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
262 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
263 if (mr
!= MACH_MSG_SUCCESS
)
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
;
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
);
280 ipc_port_reference(reply
);
282 #if IKM_SUPPORT_LEGACY
284 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
286 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
288 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
290 if (mr
!= MACH_MSG_SUCCESS
) {
294 mr
= ipc_kmsg_send_always(kmsg
);
295 if (mr
!= MACH_MSG_SUCCESS
) {
296 ipc_kmsg_destroy(kmsg
);
304 if ( !ip_active(reply
)) {
306 ipc_port_release(reply
);
307 return MACH_RCV_PORT_DIED
;
311 ipc_port_release(reply
);
312 return MACH_RCV_INTERRUPTED
;
315 assert(reply
->ip_pset_count
== 0);
316 mqueue
= &reply
->ip_messages
;
319 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
321 ipc_mqueue_receive(mqueue
,
322 MACH_MSG_OPTION_NONE
,
324 MACH_MSG_TIMEOUT_NONE
,
325 THREAD_INTERRUPTIBLE
);
327 mr
= self
->ith_state
;
328 kmsg
= self
->ith_kmsg
;
329 seqno
= self
->ith_seqno
;
331 if (mr
== MACH_MSG_SUCCESS
)
336 assert(mr
== MACH_RCV_INTERRUPTED
);
338 if (self
->handlers
) {
339 ipc_port_release(reply
);
343 ipc_port_release(reply
);
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.
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
);
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
;
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;
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
;
367 mr
= MACH_RCV_TOO_LARGE
;
372 * We want to preserve rights and memory in reply!
373 * We don't have to put them anywhere; just leave them
376 #if IKM_SUPPORT_LEGACY
378 ipc_kmsg_copyout_to_kernel_legacy(kmsg
, ipc_space_reply
);
380 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
382 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
384 ipc_kmsg_put_to_kernel(msg
, kmsg
, rcv_size
);
389 /************** These Calls are set up for kernel-loaded tasks/threads **************/
392 * Routine: mach_msg_overwrite
394 * Like mach_msg_overwrite_trap except that message buffers
395 * live in kernel space. Doesn't handle any options.
397 * This is used by in-kernel server threads to make
398 * kernel calls, to receive request messages, and
399 * to send reply messages.
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
)
417 ipc_space_t space
= current_space();
418 vm_map_t map
= current_map();
420 mach_port_seqno_t seqno
;
421 mach_msg_return_t mr
;
422 mach_msg_max_trailer_t
*trailer
;
424 if (option
& MACH_SEND_MSG
) {
425 mach_msg_size_t msg_and_trailer_size
;
426 mach_msg_max_trailer_t
*max_trailer
;
428 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
429 return MACH_SEND_MSG_TOO_SMALL
;
431 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
432 return MACH_SEND_TOO_LARGE
;
434 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
435 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
437 if (kmsg
== IKM_NULL
)
438 return MACH_SEND_NO_BUFFER
;
440 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
442 kmsg
->ikm_header
->msgh_size
= send_size
;
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.
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
;
456 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, FALSE
);
457 if (mr
!= MACH_MSG_SUCCESS
) {
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
);
469 if (option
& MACH_RCV_MSG
) {
470 thread_t self
= current_thread();
476 mr
= ipc_mqueue_copyin(space
, rcv_name
,
478 if (mr
!= MACH_MSG_SUCCESS
)
480 /* hold ref for object */
482 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
483 ipc_mqueue_receive(mqueue
,
484 MACH_MSG_OPTION_NONE
,
486 MACH_MSG_TIMEOUT_NONE
,
488 mr
= self
->ith_state
;
489 kmsg
= self
->ith_kmsg
;
490 seqno
= self
->ith_seqno
;
492 ipc_object_release(object
);
494 } while (mr
== MACH_RCV_INTERRUPTED
);
495 if (mr
!= MACH_MSG_SUCCESS
)
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
);
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
);
511 return MACH_RCV_TOO_LARGE
;
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
);
520 ipc_kmsg_copyout_dest(kmsg
, space
);
521 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
528 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
529 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
533 return MACH_MSG_SUCCESS
;
537 * Routine: mig_get_reply_port
539 * Called by client side interfaces living in the kernel
540 * to get a reply port.
543 mig_get_reply_port(void)
545 return (MACH_PORT_NULL
);
549 * Routine: mig_dealloc_reply_port
551 * Called by client side interfaces to get rid of a reply port.
555 mig_dealloc_reply_port(
556 __unused mach_port_t reply_port
)
561 * Routine: mig_put_reply_port
563 * Called by client side interfaces after each RPC to
564 * let the client recycle the reply port if it wishes.
568 __unused mach_port_t reply_port
)
573 * mig_strncpy.c - by Joshua Block
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.
582 * dest - Pointer to destination buffer.
584 * src - Pointer to source string.
586 * len - Length of destination buffer.
599 for (i
=1; i
<len
; i
++)
600 if (! (*dest
++ = *src
++))
611 return (char *)kalloc(size
);
623 * Routine: mig_object_init
625 * Initialize the base class portion of a MIG object. We
626 * will lazy init the port, so just clear it for now.
630 mig_object_t mig_object
,
631 const IMIGObject
*interface
)
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
;
641 * Routine: mig_object_destroy
643 * The object is being freed. This call lets us clean
644 * up any state we have have built up over the object's
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.
653 __assert_only mig_object_t mig_object
)
655 assert(mig_object
->port
== MACH_PORT_NULL
);
660 * Routine: mig_object_reference
662 * Pure virtual helper to invoke the MIG object's AddRef
665 * MIG object port may be locked.
668 mig_object_reference(
669 mig_object_t mig_object
)
671 assert(mig_object
!= MIG_OBJECT_NULL
);
672 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
676 * Routine: mig_object_deallocate
678 * Pure virtual helper to invoke the MIG object's Release
684 mig_object_deallocate(
685 mig_object_t mig_object
)
687 assert(mig_object
!= MIG_OBJECT_NULL
);
688 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
692 * Routine: convert_mig_object_to_port [interface]
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.
698 * IP_NULL - Null MIG object supplied
699 * Otherwise, a newly made send right for the port
704 convert_mig_object_to_port(
705 mig_object_t mig_object
)
708 boolean_t deallocate
= TRUE
;
710 if (mig_object
== MIG_OBJECT_NULL
)
713 port
= mig_object
->port
;
714 while ((port
== IP_NULL
) ||
715 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
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.
727 port
= ipc_port_alloc_kernel();
729 ipc_kobject_set_atomically(port
,
730 (ipc_kobject_t
) mig_object
,
733 /* make a sonce right for the notification */
737 ipc_port_nsrequest(port
, 1, port
, &previous
);
740 assert(previous
== IP_NULL
);
742 if (OSCompareAndSwapPtr((void *)IP_NULL
, (void *)port
,
743 (void * volatile *)&mig_object
->port
)) {
746 ipc_port_dealloc_kernel(port
);
747 port
= mig_object
->port
;
752 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
759 * Routine: convert_port_to_mig_object [interface]
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.
766 * NULL - Not an active MIG object port or iid not supported
767 * Otherwise, a reference to the underlying MIG interface
772 convert_port_to_mig_object(
776 mig_object_t mig_object
;
783 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
789 * Our port points to some MIG object interface. Now
790 * query it to get a reference to the desired interface.
793 mig_object
= (mig_object_t
)port
->ip_kobject
;
794 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
796 return (mig_object_t
)ppv
;
800 * Routine: mig_object_no_senders [interface]
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.
806 * TRUE - port deallocate and reference dropped
807 * FALSE - more senders arrived, re-registered for notification
813 mig_object_no_senders(
815 mach_port_mscount_t mscount
)
817 mig_object_t mig_object
;
820 if (port
->ip_mscount
> mscount
) {
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.
829 /* make a sonce right for the notification */
832 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
835 assert(previous
== IP_NULL
);
840 * Clear the port pointer while we have it locked.
842 mig_object
= (mig_object_t
)port
->ip_kobject
;
843 mig_object
->port
= IP_NULL
;
846 * Bring the sequence number and mscount in
847 * line with ipc_port_destroy assertion.
849 port
->ip_mscount
= 0;
850 port
->ip_messages
.imq_seqno
= 0;
851 ipc_port_destroy(port
); /* releases lock */
854 * Release the port's reference on the object.
856 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
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.
865 * The implementation of this part of MIG objects is coming
866 * "Real Soon Now"(TM).