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 <vm/vm_map.h>
85 * Routine: mach_msg_send_from_kernel
87 * Send a message from the kernel.
89 * This is used by the client side of KernelUser interfaces
90 * to implement SimpleRoutines. Currently, this includes
91 * memory_object messages.
95 * MACH_MSG_SUCCESS Sent the message.
96 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
97 * MACH_SEND_INVALID_DEST Bad destination port.
101 mach_msg_send_from_kernel(
102 mach_msg_header_t
*msg
,
103 mach_msg_size_t send_size
)
106 mach_msg_return_t mr
;
108 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
109 return MACH_SEND_INVALID_DEST
;
111 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
112 if (mr
!= MACH_MSG_SUCCESS
)
115 ipc_kmsg_copyin_from_kernel(kmsg
);
116 ipc_kmsg_send_always(kmsg
);
118 return MACH_MSG_SUCCESS
;
122 mach_msg_send_from_kernel_with_options(
123 mach_msg_header_t
*msg
,
124 mach_msg_size_t send_size
,
125 mach_msg_option_t option
,
126 mach_msg_timeout_t timeout_val
)
129 mach_msg_return_t mr
;
131 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
132 return MACH_SEND_INVALID_DEST
;
134 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
135 if (mr
!= MACH_MSG_SUCCESS
)
138 ipc_kmsg_copyin_from_kernel(kmsg
);
139 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
140 if (mr
!= MACH_MSG_SUCCESS
) {
148 * Routine: mach_msg_rpc_from_kernel
150 * Send a message from the kernel and receive a reply.
151 * Uses ith_rpc_reply for the reply port.
153 * This is used by the client side of KernelUser interfaces
154 * to implement Routines.
158 * MACH_MSG_SUCCESS Sent the message.
159 * MACH_RCV_PORT_DIED The reply port was deallocated.
163 mach_msg_rpc_from_kernel(
164 mach_msg_header_t
*msg
,
165 mach_msg_size_t send_size
,
166 mach_msg_size_t rcv_size
)
168 thread_t self
= current_thread();
171 mach_port_seqno_t seqno
;
172 mach_msg_return_t mr
;
174 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
175 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
177 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
178 if (mr
!= MACH_MSG_SUCCESS
)
181 reply
= self
->ith_rpc_reply
;
182 if (reply
== IP_NULL
) {
183 reply
= ipc_port_alloc_reply();
184 if ((reply
== IP_NULL
) ||
185 (self
->ith_rpc_reply
!= IP_NULL
))
186 panic("mach_msg_rpc_from_kernel");
187 self
->ith_rpc_reply
= reply
;
190 /* insert send-once right for the reply port */
191 kmsg
->ikm_header
->msgh_local_port
= reply
;
192 kmsg
->ikm_header
->msgh_bits
|=
193 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
195 ipc_port_reference(reply
);
197 ipc_kmsg_copyin_from_kernel(kmsg
);
199 ipc_kmsg_send_always(kmsg
);
205 if ( !ip_active(reply
)) {
207 ipc_port_release(reply
);
208 return MACH_RCV_PORT_DIED
;
212 ipc_port_release(reply
);
213 return MACH_RCV_INTERRUPTED
;
216 assert(reply
->ip_pset_count
== 0);
217 mqueue
= &reply
->ip_messages
;
220 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
222 ipc_mqueue_receive(mqueue
,
223 MACH_MSG_OPTION_NONE
,
225 MACH_MSG_TIMEOUT_NONE
,
226 THREAD_INTERRUPTIBLE
);
228 mr
= self
->ith_state
;
229 kmsg
= self
->ith_kmsg
;
230 seqno
= self
->ith_seqno
;
232 if (mr
== MACH_MSG_SUCCESS
)
237 assert(mr
== MACH_RCV_INTERRUPTED
);
239 if (self
->handlers
) {
240 ipc_port_release(reply
);
244 ipc_port_release(reply
);
247 * Check to see how much of the message/trailer can be received.
248 * We chose the maximum trailer that will fit, since we don't
249 * have options telling us which trailer elements the caller needed.
251 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
) {
252 mach_msg_format_0_trailer_t
*trailer
= (mach_msg_format_0_trailer_t
*)
253 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
255 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
+ MAX_TRAILER_SIZE
) {
256 /* Enough room for a maximum trailer */
257 trailer
->msgh_trailer_size
= MAX_TRAILER_SIZE
;
259 else if (rcv_size
< kmsg
->ikm_header
->msgh_size
+
260 trailer
->msgh_trailer_size
) {
261 /* no room for even the basic (default) trailer */
262 trailer
->msgh_trailer_size
= 0;
264 assert(trailer
->msgh_trailer_type
== MACH_MSG_TRAILER_FORMAT_0
);
265 rcv_size
= kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
;
266 mr
= MACH_MSG_SUCCESS
;
268 mr
= MACH_RCV_TOO_LARGE
;
273 * We want to preserve rights and memory in reply!
274 * We don't have to put them anywhere; just leave them
277 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
278 ipc_kmsg_put_to_kernel(msg
, kmsg
, rcv_size
);
283 /************** These Calls are set up for kernel-loaded tasks/threads **************/
286 * Routine: mach_msg_overwrite
288 * Like mach_msg_overwrite_trap except that message buffers
289 * live in kernel space. Doesn't handle any options.
291 * This is used by in-kernel server threads to make
292 * kernel calls, to receive request messages, and
293 * to send reply messages.
301 mach_msg_header_t
*msg
,
302 mach_msg_option_t option
,
303 mach_msg_size_t send_size
,
304 mach_msg_size_t rcv_size
,
305 mach_port_name_t rcv_name
,
306 __unused mach_msg_timeout_t msg_timeout
,
307 __unused mach_port_name_t notify
,
308 __unused mach_msg_header_t
*rcv_msg
,
309 __unused mach_msg_size_t rcv_msg_size
)
311 ipc_space_t space
= current_space();
312 vm_map_t map
= current_map();
314 mach_port_seqno_t seqno
;
315 mach_msg_return_t mr
;
316 mach_msg_format_0_trailer_t
*trailer
;
318 if (option
& MACH_SEND_MSG
) {
319 mach_msg_size_t msg_and_trailer_size
;
320 mach_msg_max_trailer_t
*max_trailer
;
322 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
323 return MACH_SEND_MSG_TOO_SMALL
;
325 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
326 return MACH_SEND_TOO_LARGE
;
328 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
329 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
331 if (kmsg
== IKM_NULL
)
332 return MACH_SEND_NO_BUFFER
;
334 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
336 kmsg
->ikm_header
->msgh_size
= send_size
;
339 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
340 * However, the internal size field of the trailer (msgh_trailer_size)
341 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
342 * the cases where no implicit data is requested.
344 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
345 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
346 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
347 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
348 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
350 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
351 if (mr
!= MACH_MSG_SUCCESS
) {
357 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
358 MACH_MSG_TIMEOUT_NONE
);
359 while (mr
== MACH_SEND_INTERRUPTED
);
360 assert(mr
== MACH_MSG_SUCCESS
);
363 if (option
& MACH_RCV_MSG
) {
364 thread_t self
= current_thread();
370 mr
= ipc_mqueue_copyin(space
, rcv_name
,
372 if (mr
!= MACH_MSG_SUCCESS
)
374 /* hold ref for object */
376 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
377 ipc_mqueue_receive(mqueue
,
378 MACH_MSG_OPTION_NONE
,
380 MACH_MSG_TIMEOUT_NONE
,
382 mr
= self
->ith_state
;
383 kmsg
= self
->ith_kmsg
;
384 seqno
= self
->ith_seqno
;
386 ipc_object_release(object
);
388 } while (mr
== MACH_RCV_INTERRUPTED
);
389 if (mr
!= MACH_MSG_SUCCESS
)
392 trailer
= (mach_msg_format_0_trailer_t
*)
393 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
394 if (option
& MACH_RCV_TRAILER_MASK
) {
395 trailer
->msgh_seqno
= seqno
;
396 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
399 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
)) {
400 ipc_kmsg_copyout_dest(kmsg
, space
);
401 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
403 return MACH_RCV_TOO_LARGE
;
406 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
408 if (mr
!= MACH_MSG_SUCCESS
) {
409 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
410 ipc_kmsg_put_to_kernel(msg
, kmsg
,
411 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
413 ipc_kmsg_copyout_dest(kmsg
, space
);
414 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
421 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
422 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
426 return MACH_MSG_SUCCESS
;
430 * Routine: mig_get_reply_port
432 * Called by client side interfaces living in the kernel
433 * to get a reply port.
436 mig_get_reply_port(void)
438 return (MACH_PORT_NULL
);
442 * Routine: mig_dealloc_reply_port
444 * Called by client side interfaces to get rid of a reply port.
448 mig_dealloc_reply_port(
449 __unused mach_port_t reply_port
)
451 panic("mig_dealloc_reply_port");
455 * Routine: mig_put_reply_port
457 * Called by client side interfaces after each RPC to
458 * let the client recycle the reply port if it wishes.
462 __unused mach_port_t reply_port
)
467 * mig_strncpy.c - by Joshua Block
469 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
470 * OUGHT to do: Copies the (null terminated) string in src into dest, a
471 * buffer of length len. Assures that the copy is still null terminated
472 * and doesn't overflow the buffer, truncating the copy if necessary.
476 * dest - Pointer to destination buffer.
478 * src - Pointer to source string.
480 * len - Length of destination buffer.
493 for (i
=1; i
<len
; i
++)
494 if (! (*dest
++ = *src
++))
505 return (char *)kalloc(size
);
517 * Routine: mig_object_init
519 * Initialize the base class portion of a MIG object. We
520 * will lazy init the port, so just clear it for now.
524 mig_object_t mig_object
,
525 const IMIGObject
*interface
)
527 if (mig_object
== MIG_OBJECT_NULL
)
528 return KERN_INVALID_ARGUMENT
;
529 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
530 mig_object
->port
= MACH_PORT_NULL
;
535 * Routine: mig_object_destroy
537 * The object is being freed. This call lets us clean
538 * up any state we have have built up over the object's
541 * Since notifications and the port hold references on
542 * on the object, neither can exist when this is called.
543 * This is a good place to assert() that condition.
547 __assert_only mig_object_t mig_object
)
549 assert(mig_object
->port
== MACH_PORT_NULL
);
554 * Routine: mig_object_reference
556 * Pure virtual helper to invoke the MIG object's AddRef
559 * MIG object port may be locked.
562 mig_object_reference(
563 mig_object_t mig_object
)
565 assert(mig_object
!= MIG_OBJECT_NULL
);
566 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
570 * Routine: mig_object_deallocate
572 * Pure virtual helper to invoke the MIG object's Release
578 mig_object_deallocate(
579 mig_object_t mig_object
)
581 assert(mig_object
!= MIG_OBJECT_NULL
);
582 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
586 * Routine: convert_mig_object_to_port [interface]
588 * Base implementation of MIG outtrans routine to convert from
589 * a mig object reference to a new send right on the object's
590 * port. The object reference is consumed.
592 * IP_NULL - Null MIG object supplied
593 * Otherwise, a newly made send right for the port
598 convert_mig_object_to_port(
599 mig_object_t mig_object
)
602 boolean_t deallocate
= TRUE
;
604 if (mig_object
== MIG_OBJECT_NULL
)
607 port
= mig_object
->port
;
608 while ((port
== IP_NULL
) ||
609 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
613 * Either the port was never set up, or it was just
614 * deallocated out from under us by the no-senders
615 * processing. In either case, we must:
616 * Attempt to make one
617 * Arrange for no senders
618 * Try to atomically register it with the object
619 * Destroy it if we are raced.
621 port
= ipc_port_alloc_kernel();
623 ipc_kobject_set_atomically(port
,
624 (ipc_kobject_t
) mig_object
,
627 /* make a sonce right for the notification */
631 ipc_port_nsrequest(port
, 1, port
, &previous
);
634 assert(previous
== IP_NULL
);
636 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
637 (uint32_t *)&mig_object
->port
)) {
640 ipc_port_dealloc_kernel(port
);
641 port
= mig_object
->port
;
646 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
653 * Routine: convert_port_to_mig_object [interface]
655 * Base implementation of MIG intrans routine to convert from
656 * an incoming port reference to a new reference on the
657 * underlying object. A new reference must be created, because
658 * the port's reference could go away asynchronously.
660 * NULL - Not an active MIG object port or iid not supported
661 * Otherwise, a reference to the underlying MIG interface
666 convert_port_to_mig_object(
670 mig_object_t mig_object
;
677 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
683 * Our port points to some MIG object interface. Now
684 * query it to get a reference to the desired interface.
687 mig_object
= (mig_object_t
)port
->ip_kobject
;
688 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
690 return (mig_object_t
)ppv
;
694 * Routine: mig_object_no_senders [interface]
696 * Base implementation of a no-senders notification handler
697 * for MIG objects. If there truly are no more senders, must
698 * destroy the port and drop its reference on the object.
700 * TRUE - port deallocate and reference dropped
701 * FALSE - more senders arrived, re-registered for notification
707 mig_object_no_senders(
709 mach_port_mscount_t mscount
)
711 mig_object_t mig_object
;
714 if (port
->ip_mscount
> mscount
) {
718 * Somebody created new send rights while the
719 * notification was in-flight. Just create a
720 * new send-once right and re-register with
721 * the new (higher) mscount threshold.
723 /* make a sonce right for the notification */
726 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
729 assert(previous
== IP_NULL
);
734 * Clear the port pointer while we have it locked.
736 mig_object
= (mig_object_t
)port
->ip_kobject
;
737 mig_object
->port
= IP_NULL
;
740 * Bring the sequence number and mscount in
741 * line with ipc_port_destroy assertion.
743 port
->ip_mscount
= 0;
744 port
->ip_messages
.imq_seqno
= 0;
745 ipc_port_destroy(port
); /* releases lock */
748 * Release the port's reference on the object.
750 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
755 * Kernel implementation of the notification chain for MIG object
756 * is kept separate from the actual objects, since there are expected
757 * to be much fewer of them than actual objects.
759 * The implementation of this part of MIG objects is coming
760 * "Real Soon Now"(TM).