2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 #include <mach/boolean.h>
55 #include <mach/port.h>
57 #include <mach/mig_errors.h>
58 #include <mach/mach_types.h>
59 #include <mach/mach_traps.h>
61 #include <kern/ipc_tt.h>
62 #include <kern/ipc_mig.h>
63 #include <kern/kalloc.h>
64 #include <kern/task.h>
65 #include <kern/thread.h>
66 #include <kern/ipc_kobject.h>
67 #include <kern/misc_protos.h>
70 #include <ipc/ipc_kmsg.h>
71 #include <ipc/ipc_entry.h>
72 #include <ipc/ipc_object.h>
73 #include <ipc/ipc_mqueue.h>
74 #include <ipc/ipc_space.h>
75 #include <ipc/ipc_port.h>
76 #include <ipc/ipc_pset.h>
77 #include <vm/vm_map.h>
80 * Routine: mach_msg_send_from_kernel
82 * Send a message from the kernel.
84 * This is used by the client side of KernelUser interfaces
85 * to implement SimpleRoutines. Currently, this includes
86 * memory_object messages.
90 * MACH_MSG_SUCCESS Sent the message.
91 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
92 * MACH_SEND_INVALID_DEST Bad destination port.
96 mach_msg_send_from_kernel(
97 mach_msg_header_t
*msg
,
98 mach_msg_size_t send_size
)
101 mach_msg_return_t mr
;
103 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
104 return MACH_SEND_INVALID_DEST
;
106 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
107 if (mr
!= MACH_MSG_SUCCESS
)
110 ipc_kmsg_copyin_from_kernel(kmsg
);
111 ipc_kmsg_send_always(kmsg
);
113 return MACH_MSG_SUCCESS
;
117 * Routine: mach_msg_rpc_from_kernel
119 * Send a message from the kernel and receive a reply.
120 * Uses ith_rpc_reply for the reply port.
122 * This is used by the client side of KernelUser interfaces
123 * to implement Routines.
127 * MACH_MSG_SUCCESS Sent the message.
128 * MACH_RCV_PORT_DIED The reply port was deallocated.
132 mach_msg_rpc_from_kernel(
133 mach_msg_header_t
*msg
,
134 mach_msg_size_t send_size
,
135 mach_msg_size_t rcv_size
)
137 thread_t self
= current_thread();
140 mach_port_seqno_t seqno
;
141 mach_msg_return_t mr
;
143 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
144 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
146 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
147 if (mr
!= MACH_MSG_SUCCESS
)
150 reply
= self
->ith_rpc_reply
;
151 if (reply
== IP_NULL
) {
152 reply
= ipc_port_alloc_reply();
153 if ((reply
== IP_NULL
) ||
154 (self
->ith_rpc_reply
!= IP_NULL
))
155 panic("mach_msg_rpc_from_kernel");
156 self
->ith_rpc_reply
= reply
;
159 /* insert send-once right for the reply port */
160 kmsg
->ikm_header
->msgh_local_port
= reply
;
161 kmsg
->ikm_header
->msgh_bits
|=
162 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
164 ipc_port_reference(reply
);
166 ipc_kmsg_copyin_from_kernel(kmsg
);
168 ipc_kmsg_send_always(kmsg
);
174 if ( !ip_active(reply
)) {
176 ipc_port_release(reply
);
177 return MACH_RCV_PORT_DIED
;
181 ipc_port_release(reply
);
182 return MACH_RCV_INTERRUPTED
;
185 assert(reply
->ip_pset_count
== 0);
186 mqueue
= &reply
->ip_messages
;
189 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
191 ipc_mqueue_receive(mqueue
,
192 MACH_MSG_OPTION_NONE
,
194 MACH_MSG_TIMEOUT_NONE
,
195 THREAD_INTERRUPTIBLE
);
197 mr
= self
->ith_state
;
198 kmsg
= self
->ith_kmsg
;
199 seqno
= self
->ith_seqno
;
201 if (mr
== MACH_MSG_SUCCESS
)
206 assert(mr
== MACH_RCV_INTERRUPTED
);
208 if (self
->handlers
) {
209 ipc_port_release(reply
);
213 ipc_port_release(reply
);
216 * XXXXX Set manually for now ...
217 * No, why even bother, since the effort is wasted?
219 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
220 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
221 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
222 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
226 if (rcv_size
< kmsg
->ikm_header
->msgh_size
) {
227 ipc_kmsg_copyout_dest(kmsg
, ipc_space_reply
);
228 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
229 return MACH_RCV_TOO_LARGE
;
233 * We want to preserve rights and memory in reply!
234 * We don't have to put them anywhere; just leave them
238 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
239 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
240 return MACH_MSG_SUCCESS
;
244 /************** These Calls are set up for kernel-loaded tasks/threads **************/
247 * Routine: mach_msg_overwrite
249 * Like mach_msg_overwrite_trap except that message buffers
250 * live in kernel space. Doesn't handle any options.
252 * This is used by in-kernel server threads to make
253 * kernel calls, to receive request messages, and
254 * to send reply messages.
262 mach_msg_header_t
*msg
,
263 mach_msg_option_t option
,
264 mach_msg_size_t send_size
,
265 mach_msg_size_t rcv_size
,
266 mach_port_name_t rcv_name
,
267 __unused mach_msg_timeout_t msg_timeout
,
268 __unused mach_port_name_t notify
,
269 __unused mach_msg_header_t
*rcv_msg
,
270 __unused mach_msg_size_t rcv_msg_size
)
272 ipc_space_t space
= current_space();
273 vm_map_t map
= current_map();
275 mach_port_seqno_t seqno
;
276 mach_msg_return_t mr
;
277 mach_msg_format_0_trailer_t
*trailer
;
279 if (option
& MACH_SEND_MSG
) {
280 mach_msg_size_t msg_and_trailer_size
;
281 mach_msg_max_trailer_t
*max_trailer
;
283 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
284 return MACH_SEND_MSG_TOO_SMALL
;
286 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
288 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
290 if (kmsg
== IKM_NULL
)
291 return MACH_SEND_NO_BUFFER
;
293 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
295 kmsg
->ikm_header
->msgh_size
= send_size
;
298 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
299 * However, the internal size field of the trailer (msgh_trailer_size)
300 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
301 * the cases where no implicit data is requested.
303 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
304 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
305 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
306 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
307 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
309 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
310 if (mr
!= MACH_MSG_SUCCESS
) {
316 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
317 MACH_MSG_TIMEOUT_NONE
);
318 while (mr
== MACH_SEND_INTERRUPTED
);
319 assert(mr
== MACH_MSG_SUCCESS
);
322 if (option
& MACH_RCV_MSG
) {
323 thread_t self
= current_thread();
329 mr
= ipc_mqueue_copyin(space
, rcv_name
,
331 if (mr
!= MACH_MSG_SUCCESS
)
333 /* hold ref for object */
335 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
336 ipc_mqueue_receive(mqueue
,
337 MACH_MSG_OPTION_NONE
,
339 MACH_MSG_TIMEOUT_NONE
,
341 mr
= self
->ith_state
;
342 kmsg
= self
->ith_kmsg
;
343 seqno
= self
->ith_seqno
;
345 ipc_object_release(object
);
347 } while (mr
== MACH_RCV_INTERRUPTED
);
348 if (mr
!= MACH_MSG_SUCCESS
)
351 trailer
= (mach_msg_format_0_trailer_t
*)
352 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
353 if (option
& MACH_RCV_TRAILER_MASK
) {
354 trailer
->msgh_seqno
= seqno
;
355 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
358 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
)) {
359 ipc_kmsg_copyout_dest(kmsg
, space
);
360 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
362 return MACH_RCV_TOO_LARGE
;
365 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
367 if (mr
!= MACH_MSG_SUCCESS
) {
368 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
369 ipc_kmsg_put_to_kernel(msg
, kmsg
,
370 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
372 ipc_kmsg_copyout_dest(kmsg
, space
);
373 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
380 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
381 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
385 return MACH_MSG_SUCCESS
;
389 * Routine: mig_get_reply_port
391 * Called by client side interfaces living in the kernel
392 * to get a reply port.
395 mig_get_reply_port(void)
397 return (MACH_PORT_NULL
);
401 * Routine: mig_dealloc_reply_port
403 * Called by client side interfaces to get rid of a reply port.
407 mig_dealloc_reply_port(
408 __unused mach_port_t reply_port
)
410 panic("mig_dealloc_reply_port");
414 * Routine: mig_put_reply_port
416 * Called by client side interfaces after each RPC to
417 * let the client recycle the reply port if it wishes.
421 __unused mach_port_t reply_port
)
426 * mig_strncpy.c - by Joshua Block
428 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
429 * OUGHT to do: Copies the (null terminated) string in src into dest, a
430 * buffer of length len. Assures that the copy is still null terminated
431 * and doesn't overflow the buffer, truncating the copy if necessary.
435 * dest - Pointer to destination buffer.
437 * src - Pointer to source string.
439 * len - Length of destination buffer.
452 for (i
=1; i
<len
; i
++)
453 if (! (*dest
++ = *src
++))
464 return (char *)kalloc(size
);
476 * Routine: mig_object_init
478 * Initialize the base class portion of a MIG object. We
479 * will lazy init the port, so just clear it for now.
483 mig_object_t mig_object
,
484 const IMIGObject
*interface
)
486 if (mig_object
== MIG_OBJECT_NULL
)
487 return KERN_INVALID_ARGUMENT
;
488 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
489 mig_object
->port
= MACH_PORT_NULL
;
494 * Routine: mig_object_destroy
496 * The object is being freed. This call lets us clean
497 * up any state we have have built up over the object's
500 * Since notifications and the port hold references on
501 * on the object, neither can exist when this is called.
502 * This is a good place to assert() that condition.
506 __assert_only mig_object_t mig_object
)
508 assert(mig_object
->port
== MACH_PORT_NULL
);
513 * Routine: mig_object_reference
515 * Pure virtual helper to invoke the MIG object's AddRef
518 * MIG object port may be locked.
521 mig_object_reference(
522 mig_object_t mig_object
)
524 assert(mig_object
!= MIG_OBJECT_NULL
);
525 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
529 * Routine: mig_object_deallocate
531 * Pure virtual helper to invoke the MIG object's Release
537 mig_object_deallocate(
538 mig_object_t mig_object
)
540 assert(mig_object
!= MIG_OBJECT_NULL
);
541 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
545 * Routine: convert_mig_object_to_port [interface]
547 * Base implementation of MIG outtrans routine to convert from
548 * a mig object reference to a new send right on the object's
549 * port. The object reference is consumed.
551 * IP_NULL - Null MIG object supplied
552 * Otherwise, a newly made send right for the port
557 convert_mig_object_to_port(
558 mig_object_t mig_object
)
561 boolean_t deallocate
= TRUE
;
563 if (mig_object
== MIG_OBJECT_NULL
)
566 port
= mig_object
->port
;
567 while ((port
== IP_NULL
) ||
568 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
572 * Either the port was never set up, or it was just
573 * deallocated out from under us by the no-senders
574 * processing. In either case, we must:
575 * Attempt to make one
576 * Arrange for no senders
577 * Try to atomically register it with the object
578 * Destroy it if we are raced.
580 port
= ipc_port_alloc_kernel();
582 ipc_kobject_set_atomically(port
,
583 (ipc_kobject_t
) mig_object
,
586 /* make a sonce right for the notification */
590 ipc_port_nsrequest(port
, 1, port
, &previous
);
593 assert(previous
== IP_NULL
);
595 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
596 (uint32_t *)&mig_object
->port
)) {
599 ipc_port_dealloc_kernel(port
);
600 port
= mig_object
->port
;
605 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
612 * Routine: convert_port_to_mig_object [interface]
614 * Base implementation of MIG intrans routine to convert from
615 * an incoming port reference to a new reference on the
616 * underlying object. A new reference must be created, because
617 * the port's reference could go away asynchronously.
619 * NULL - Not an active MIG object port or iid not supported
620 * Otherwise, a reference to the underlying MIG interface
625 convert_port_to_mig_object(
629 mig_object_t mig_object
;
636 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
642 * Our port points to some MIG object interface. Now
643 * query it to get a reference to the desired interface.
646 mig_object
= (mig_object_t
)port
->ip_kobject
;
647 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
649 return (mig_object_t
)ppv
;
653 * Routine: mig_object_no_senders [interface]
655 * Base implementation of a no-senders notification handler
656 * for MIG objects. If there truly are no more senders, must
657 * destroy the port and drop its reference on the object.
659 * TRUE - port deallocate and reference dropped
660 * FALSE - more senders arrived, re-registered for notification
666 mig_object_no_senders(
668 mach_port_mscount_t mscount
)
670 mig_object_t mig_object
;
673 if (port
->ip_mscount
> mscount
) {
677 * Somebody created new send rights while the
678 * notification was in-flight. Just create a
679 * new send-once right and re-register with
680 * the new (higher) mscount threshold.
682 /* make a sonce right for the notification */
685 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
688 assert(previous
== IP_NULL
);
693 * Clear the port pointer while we have it locked.
695 mig_object
= (mig_object_t
)port
->ip_kobject
;
696 mig_object
->port
= IP_NULL
;
699 * Bring the sequence number and mscount in
700 * line with ipc_port_destroy assertion.
702 port
->ip_mscount
= 0;
703 port
->ip_messages
.imq_seqno
= 0;
704 ipc_port_destroy(port
); /* releases lock */
707 * Release the port's reference on the object.
709 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
714 * Kernel implementation of the notification chain for MIG object
715 * is kept separate from the actual objects, since there are expected
716 * to be much fewer of them than actual objects.
718 * The implementation of this part of MIG objects is coming
719 * "Real Soon Now"(TM).