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_SEND_INVALID_DEST Bad destination port.
97 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
98 * or destination is above kernel limit
102 mach_msg_send_from_kernel(
103 mach_msg_header_t
*msg
,
104 mach_msg_size_t send_size
)
107 mach_msg_return_t mr
;
109 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
110 return MACH_SEND_INVALID_DEST
;
112 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
113 if (mr
!= MACH_MSG_SUCCESS
)
116 ipc_kmsg_copyin_from_kernel(kmsg
);
118 mr
= ipc_kmsg_send_always(kmsg
);
119 if (mr
!= MACH_MSG_SUCCESS
) {
120 ipc_kmsg_destroy(kmsg
);
127 mach_msg_send_from_kernel_with_options(
128 mach_msg_header_t
*msg
,
129 mach_msg_size_t send_size
,
130 mach_msg_option_t option
,
131 mach_msg_timeout_t timeout_val
)
134 mach_msg_return_t mr
;
136 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
137 return MACH_SEND_INVALID_DEST
;
139 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
140 if (mr
!= MACH_MSG_SUCCESS
)
143 ipc_kmsg_copyin_from_kernel(kmsg
);
144 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
145 if (mr
!= MACH_MSG_SUCCESS
) {
146 ipc_kmsg_destroy(kmsg
);
153 * Routine: mach_msg_rpc_from_kernel
155 * Send a message from the kernel and receive a reply.
156 * Uses ith_rpc_reply for the reply port.
158 * This is used by the client side of KernelUser interfaces
159 * to implement Routines.
163 * MACH_MSG_SUCCESS Sent the message.
164 * MACH_RCV_PORT_DIED The reply port was deallocated.
168 mach_msg_rpc_from_kernel(
169 mach_msg_header_t
*msg
,
170 mach_msg_size_t send_size
,
171 mach_msg_size_t rcv_size
)
173 thread_t self
= current_thread();
176 mach_port_seqno_t seqno
;
177 mach_msg_return_t mr
;
179 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
180 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
182 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
183 if (mr
!= MACH_MSG_SUCCESS
)
186 reply
= self
->ith_rpc_reply
;
187 if (reply
== IP_NULL
) {
188 reply
= ipc_port_alloc_reply();
189 if ((reply
== IP_NULL
) ||
190 (self
->ith_rpc_reply
!= IP_NULL
))
191 panic("mach_msg_rpc_from_kernel");
192 self
->ith_rpc_reply
= reply
;
195 /* insert send-once right for the reply port */
196 kmsg
->ikm_header
->msgh_local_port
= reply
;
197 kmsg
->ikm_header
->msgh_bits
|=
198 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
200 ipc_port_reference(reply
);
202 ipc_kmsg_copyin_from_kernel(kmsg
);
204 mr
= ipc_kmsg_send_always(kmsg
);
205 if (mr
!= MACH_MSG_SUCCESS
) {
206 ipc_kmsg_destroy(kmsg
);
214 if ( !ip_active(reply
)) {
216 ipc_port_release(reply
);
217 return MACH_RCV_PORT_DIED
;
221 ipc_port_release(reply
);
222 return MACH_RCV_INTERRUPTED
;
225 assert(reply
->ip_pset_count
== 0);
226 mqueue
= &reply
->ip_messages
;
229 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
231 ipc_mqueue_receive(mqueue
,
232 MACH_MSG_OPTION_NONE
,
234 MACH_MSG_TIMEOUT_NONE
,
235 THREAD_INTERRUPTIBLE
);
237 mr
= self
->ith_state
;
238 kmsg
= self
->ith_kmsg
;
239 seqno
= self
->ith_seqno
;
241 if (mr
== MACH_MSG_SUCCESS
)
246 assert(mr
== MACH_RCV_INTERRUPTED
);
248 if (self
->handlers
) {
249 ipc_port_release(reply
);
253 ipc_port_release(reply
);
256 * Check to see how much of the message/trailer can be received.
257 * We chose the maximum trailer that will fit, since we don't
258 * have options telling us which trailer elements the caller needed.
260 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
) {
261 mach_msg_format_0_trailer_t
*trailer
= (mach_msg_format_0_trailer_t
*)
262 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
264 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
+ MAX_TRAILER_SIZE
) {
265 /* Enough room for a maximum trailer */
266 trailer
->msgh_trailer_size
= MAX_TRAILER_SIZE
;
268 else if (rcv_size
< kmsg
->ikm_header
->msgh_size
+
269 trailer
->msgh_trailer_size
) {
270 /* no room for even the basic (default) trailer */
271 trailer
->msgh_trailer_size
= 0;
273 assert(trailer
->msgh_trailer_type
== MACH_MSG_TRAILER_FORMAT_0
);
274 rcv_size
= kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
;
275 mr
= MACH_MSG_SUCCESS
;
277 mr
= MACH_RCV_TOO_LARGE
;
282 * We want to preserve rights and memory in reply!
283 * We don't have to put them anywhere; just leave them
286 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
287 ipc_kmsg_put_to_kernel(msg
, kmsg
, rcv_size
);
292 /************** These Calls are set up for kernel-loaded tasks/threads **************/
295 * Routine: mach_msg_overwrite
297 * Like mach_msg_overwrite_trap except that message buffers
298 * live in kernel space. Doesn't handle any options.
300 * This is used by in-kernel server threads to make
301 * kernel calls, to receive request messages, and
302 * to send reply messages.
310 mach_msg_header_t
*msg
,
311 mach_msg_option_t option
,
312 mach_msg_size_t send_size
,
313 mach_msg_size_t rcv_size
,
314 mach_port_name_t rcv_name
,
315 __unused mach_msg_timeout_t msg_timeout
,
316 __unused mach_port_name_t notify
,
317 __unused mach_msg_header_t
*rcv_msg
,
318 __unused mach_msg_size_t rcv_msg_size
)
320 ipc_space_t space
= current_space();
321 vm_map_t map
= current_map();
323 mach_port_seqno_t seqno
;
324 mach_msg_return_t mr
;
325 mach_msg_format_0_trailer_t
*trailer
;
327 if (option
& MACH_SEND_MSG
) {
328 mach_msg_size_t msg_and_trailer_size
;
329 mach_msg_max_trailer_t
*max_trailer
;
331 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
332 return MACH_SEND_MSG_TOO_SMALL
;
334 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
335 return MACH_SEND_TOO_LARGE
;
337 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
338 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
340 if (kmsg
== IKM_NULL
)
341 return MACH_SEND_NO_BUFFER
;
343 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
345 kmsg
->ikm_header
->msgh_size
= send_size
;
348 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
349 * However, the internal size field of the trailer (msgh_trailer_size)
350 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
351 * the cases where no implicit data is requested.
353 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
354 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
355 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
356 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
357 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
359 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
360 if (mr
!= MACH_MSG_SUCCESS
) {
366 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
367 MACH_MSG_TIMEOUT_NONE
);
368 while (mr
== MACH_SEND_INTERRUPTED
);
369 assert(mr
== MACH_MSG_SUCCESS
);
372 if (option
& MACH_RCV_MSG
) {
373 thread_t self
= current_thread();
379 mr
= ipc_mqueue_copyin(space
, rcv_name
,
381 if (mr
!= MACH_MSG_SUCCESS
)
383 /* hold ref for object */
385 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
386 ipc_mqueue_receive(mqueue
,
387 MACH_MSG_OPTION_NONE
,
389 MACH_MSG_TIMEOUT_NONE
,
391 mr
= self
->ith_state
;
392 kmsg
= self
->ith_kmsg
;
393 seqno
= self
->ith_seqno
;
395 ipc_object_release(object
);
397 } while (mr
== MACH_RCV_INTERRUPTED
);
398 if (mr
!= MACH_MSG_SUCCESS
)
401 trailer
= (mach_msg_format_0_trailer_t
*)
402 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
403 if (option
& MACH_RCV_TRAILER_MASK
) {
404 trailer
->msgh_seqno
= seqno
;
405 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
408 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
)) {
409 ipc_kmsg_copyout_dest(kmsg
, space
);
410 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
412 return MACH_RCV_TOO_LARGE
;
415 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
417 if (mr
!= MACH_MSG_SUCCESS
) {
418 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
419 ipc_kmsg_put_to_kernel(msg
, kmsg
,
420 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
422 ipc_kmsg_copyout_dest(kmsg
, space
);
423 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
430 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
431 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
435 return MACH_MSG_SUCCESS
;
439 * Routine: mig_get_reply_port
441 * Called by client side interfaces living in the kernel
442 * to get a reply port.
445 mig_get_reply_port(void)
447 return (MACH_PORT_NULL
);
451 * Routine: mig_dealloc_reply_port
453 * Called by client side interfaces to get rid of a reply port.
457 mig_dealloc_reply_port(
458 __unused mach_port_t reply_port
)
460 panic("mig_dealloc_reply_port");
464 * Routine: mig_put_reply_port
466 * Called by client side interfaces after each RPC to
467 * let the client recycle the reply port if it wishes.
471 __unused mach_port_t reply_port
)
476 * mig_strncpy.c - by Joshua Block
478 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
479 * OUGHT to do: Copies the (null terminated) string in src into dest, a
480 * buffer of length len. Assures that the copy is still null terminated
481 * and doesn't overflow the buffer, truncating the copy if necessary.
485 * dest - Pointer to destination buffer.
487 * src - Pointer to source string.
489 * len - Length of destination buffer.
502 for (i
=1; i
<len
; i
++)
503 if (! (*dest
++ = *src
++))
514 return (char *)kalloc(size
);
526 * Routine: mig_object_init
528 * Initialize the base class portion of a MIG object. We
529 * will lazy init the port, so just clear it for now.
533 mig_object_t mig_object
,
534 const IMIGObject
*interface
)
536 if (mig_object
== MIG_OBJECT_NULL
)
537 return KERN_INVALID_ARGUMENT
;
538 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
539 mig_object
->port
= MACH_PORT_NULL
;
544 * Routine: mig_object_destroy
546 * The object is being freed. This call lets us clean
547 * up any state we have have built up over the object's
550 * Since notifications and the port hold references on
551 * on the object, neither can exist when this is called.
552 * This is a good place to assert() that condition.
556 __assert_only mig_object_t mig_object
)
558 assert(mig_object
->port
== MACH_PORT_NULL
);
563 * Routine: mig_object_reference
565 * Pure virtual helper to invoke the MIG object's AddRef
568 * MIG object port may be locked.
571 mig_object_reference(
572 mig_object_t mig_object
)
574 assert(mig_object
!= MIG_OBJECT_NULL
);
575 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
579 * Routine: mig_object_deallocate
581 * Pure virtual helper to invoke the MIG object's Release
587 mig_object_deallocate(
588 mig_object_t mig_object
)
590 assert(mig_object
!= MIG_OBJECT_NULL
);
591 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
595 * Routine: convert_mig_object_to_port [interface]
597 * Base implementation of MIG outtrans routine to convert from
598 * a mig object reference to a new send right on the object's
599 * port. The object reference is consumed.
601 * IP_NULL - Null MIG object supplied
602 * Otherwise, a newly made send right for the port
607 convert_mig_object_to_port(
608 mig_object_t mig_object
)
611 boolean_t deallocate
= TRUE
;
613 if (mig_object
== MIG_OBJECT_NULL
)
616 port
= mig_object
->port
;
617 while ((port
== IP_NULL
) ||
618 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
622 * Either the port was never set up, or it was just
623 * deallocated out from under us by the no-senders
624 * processing. In either case, we must:
625 * Attempt to make one
626 * Arrange for no senders
627 * Try to atomically register it with the object
628 * Destroy it if we are raced.
630 port
= ipc_port_alloc_kernel();
632 ipc_kobject_set_atomically(port
,
633 (ipc_kobject_t
) mig_object
,
636 /* make a sonce right for the notification */
640 ipc_port_nsrequest(port
, 1, port
, &previous
);
643 assert(previous
== IP_NULL
);
645 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
646 (uint32_t *)&mig_object
->port
)) {
649 ipc_port_dealloc_kernel(port
);
650 port
= mig_object
->port
;
655 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
662 * Routine: convert_port_to_mig_object [interface]
664 * Base implementation of MIG intrans routine to convert from
665 * an incoming port reference to a new reference on the
666 * underlying object. A new reference must be created, because
667 * the port's reference could go away asynchronously.
669 * NULL - Not an active MIG object port or iid not supported
670 * Otherwise, a reference to the underlying MIG interface
675 convert_port_to_mig_object(
679 mig_object_t mig_object
;
686 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
692 * Our port points to some MIG object interface. Now
693 * query it to get a reference to the desired interface.
696 mig_object
= (mig_object_t
)port
->ip_kobject
;
697 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
699 return (mig_object_t
)ppv
;
703 * Routine: mig_object_no_senders [interface]
705 * Base implementation of a no-senders notification handler
706 * for MIG objects. If there truly are no more senders, must
707 * destroy the port and drop its reference on the object.
709 * TRUE - port deallocate and reference dropped
710 * FALSE - more senders arrived, re-registered for notification
716 mig_object_no_senders(
718 mach_port_mscount_t mscount
)
720 mig_object_t mig_object
;
723 if (port
->ip_mscount
> mscount
) {
727 * Somebody created new send rights while the
728 * notification was in-flight. Just create a
729 * new send-once right and re-register with
730 * the new (higher) mscount threshold.
732 /* make a sonce right for the notification */
735 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
738 assert(previous
== IP_NULL
);
743 * Clear the port pointer while we have it locked.
745 mig_object
= (mig_object_t
)port
->ip_kobject
;
746 mig_object
->port
= IP_NULL
;
749 * Bring the sequence number and mscount in
750 * line with ipc_port_destroy assertion.
752 port
->ip_mscount
= 0;
753 port
->ip_messages
.imq_seqno
= 0;
754 ipc_port_destroy(port
); /* releases lock */
757 * Release the port's reference on the object.
759 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
764 * Kernel implementation of the notification chain for MIG object
765 * is kept separate from the actual objects, since there are expected
766 * to be much fewer of them than actual objects.
768 * The implementation of this part of MIG objects is coming
769 * "Real Soon Now"(TM).