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 * Routine: mach_msg_rpc_from_kernel
124 * Send a message from the kernel and receive a reply.
125 * Uses ith_rpc_reply for the reply port.
127 * This is used by the client side of KernelUser interfaces
128 * to implement Routines.
132 * MACH_MSG_SUCCESS Sent the message.
133 * MACH_RCV_PORT_DIED The reply port was deallocated.
137 mach_msg_rpc_from_kernel(
138 mach_msg_header_t
*msg
,
139 mach_msg_size_t send_size
,
140 mach_msg_size_t rcv_size
)
142 thread_t self
= current_thread();
145 mach_port_seqno_t seqno
;
146 mach_msg_return_t mr
;
148 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
149 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
151 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
152 if (mr
!= MACH_MSG_SUCCESS
)
155 reply
= self
->ith_rpc_reply
;
156 if (reply
== IP_NULL
) {
157 reply
= ipc_port_alloc_reply();
158 if ((reply
== IP_NULL
) ||
159 (self
->ith_rpc_reply
!= IP_NULL
))
160 panic("mach_msg_rpc_from_kernel");
161 self
->ith_rpc_reply
= reply
;
164 /* insert send-once right for the reply port */
165 kmsg
->ikm_header
->msgh_local_port
= reply
;
166 kmsg
->ikm_header
->msgh_bits
|=
167 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
169 ipc_port_reference(reply
);
171 ipc_kmsg_copyin_from_kernel(kmsg
);
173 ipc_kmsg_send_always(kmsg
);
179 if ( !ip_active(reply
)) {
181 ipc_port_release(reply
);
182 return MACH_RCV_PORT_DIED
;
186 ipc_port_release(reply
);
187 return MACH_RCV_INTERRUPTED
;
190 assert(reply
->ip_pset_count
== 0);
191 mqueue
= &reply
->ip_messages
;
194 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
196 ipc_mqueue_receive(mqueue
,
197 MACH_MSG_OPTION_NONE
,
199 MACH_MSG_TIMEOUT_NONE
,
200 THREAD_INTERRUPTIBLE
);
202 mr
= self
->ith_state
;
203 kmsg
= self
->ith_kmsg
;
204 seqno
= self
->ith_seqno
;
206 if (mr
== MACH_MSG_SUCCESS
)
211 assert(mr
== MACH_RCV_INTERRUPTED
);
213 if (self
->handlers
) {
214 ipc_port_release(reply
);
218 ipc_port_release(reply
);
221 * XXXXX Set manually for now ...
222 * No, why even bother, since the effort is wasted?
224 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
225 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
226 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
227 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
231 if (rcv_size
< kmsg
->ikm_header
->msgh_size
) {
232 ipc_kmsg_copyout_dest(kmsg
, ipc_space_reply
);
233 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
234 return MACH_RCV_TOO_LARGE
;
238 * We want to preserve rights and memory in reply!
239 * We don't have to put them anywhere; just leave them
243 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
244 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
245 return MACH_MSG_SUCCESS
;
249 /************** These Calls are set up for kernel-loaded tasks/threads **************/
252 * Routine: mach_msg_overwrite
254 * Like mach_msg_overwrite_trap except that message buffers
255 * live in kernel space. Doesn't handle any options.
257 * This is used by in-kernel server threads to make
258 * kernel calls, to receive request messages, and
259 * to send reply messages.
267 mach_msg_header_t
*msg
,
268 mach_msg_option_t option
,
269 mach_msg_size_t send_size
,
270 mach_msg_size_t rcv_size
,
271 mach_port_name_t rcv_name
,
272 __unused mach_msg_timeout_t msg_timeout
,
273 __unused mach_port_name_t notify
,
274 __unused mach_msg_header_t
*rcv_msg
,
275 __unused mach_msg_size_t rcv_msg_size
)
277 ipc_space_t space
= current_space();
278 vm_map_t map
= current_map();
280 mach_port_seqno_t seqno
;
281 mach_msg_return_t mr
;
282 mach_msg_format_0_trailer_t
*trailer
;
284 if (option
& MACH_SEND_MSG
) {
285 mach_msg_size_t msg_and_trailer_size
;
286 mach_msg_max_trailer_t
*max_trailer
;
288 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
289 return MACH_SEND_MSG_TOO_SMALL
;
291 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
292 return MACH_SEND_TOO_LARGE
;
294 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
295 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
297 if (kmsg
== IKM_NULL
)
298 return MACH_SEND_NO_BUFFER
;
300 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
302 kmsg
->ikm_header
->msgh_size
= send_size
;
305 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
306 * However, the internal size field of the trailer (msgh_trailer_size)
307 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
308 * the cases where no implicit data is requested.
310 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
311 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
312 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
313 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
314 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
316 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
317 if (mr
!= MACH_MSG_SUCCESS
) {
323 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
324 MACH_MSG_TIMEOUT_NONE
);
325 while (mr
== MACH_SEND_INTERRUPTED
);
326 assert(mr
== MACH_MSG_SUCCESS
);
329 if (option
& MACH_RCV_MSG
) {
330 thread_t self
= current_thread();
336 mr
= ipc_mqueue_copyin(space
, rcv_name
,
338 if (mr
!= MACH_MSG_SUCCESS
)
340 /* hold ref for object */
342 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
343 ipc_mqueue_receive(mqueue
,
344 MACH_MSG_OPTION_NONE
,
346 MACH_MSG_TIMEOUT_NONE
,
348 mr
= self
->ith_state
;
349 kmsg
= self
->ith_kmsg
;
350 seqno
= self
->ith_seqno
;
352 ipc_object_release(object
);
354 } while (mr
== MACH_RCV_INTERRUPTED
);
355 if (mr
!= MACH_MSG_SUCCESS
)
358 trailer
= (mach_msg_format_0_trailer_t
*)
359 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
360 if (option
& MACH_RCV_TRAILER_MASK
) {
361 trailer
->msgh_seqno
= seqno
;
362 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
365 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
)) {
366 ipc_kmsg_copyout_dest(kmsg
, space
);
367 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
369 return MACH_RCV_TOO_LARGE
;
372 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
374 if (mr
!= MACH_MSG_SUCCESS
) {
375 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
376 ipc_kmsg_put_to_kernel(msg
, kmsg
,
377 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
379 ipc_kmsg_copyout_dest(kmsg
, space
);
380 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
387 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
388 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
392 return MACH_MSG_SUCCESS
;
396 * Routine: mig_get_reply_port
398 * Called by client side interfaces living in the kernel
399 * to get a reply port.
402 mig_get_reply_port(void)
404 return (MACH_PORT_NULL
);
408 * Routine: mig_dealloc_reply_port
410 * Called by client side interfaces to get rid of a reply port.
414 mig_dealloc_reply_port(
415 __unused mach_port_t reply_port
)
417 panic("mig_dealloc_reply_port");
421 * Routine: mig_put_reply_port
423 * Called by client side interfaces after each RPC to
424 * let the client recycle the reply port if it wishes.
428 __unused mach_port_t reply_port
)
433 * mig_strncpy.c - by Joshua Block
435 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
436 * OUGHT to do: Copies the (null terminated) string in src into dest, a
437 * buffer of length len. Assures that the copy is still null terminated
438 * and doesn't overflow the buffer, truncating the copy if necessary.
442 * dest - Pointer to destination buffer.
444 * src - Pointer to source string.
446 * len - Length of destination buffer.
459 for (i
=1; i
<len
; i
++)
460 if (! (*dest
++ = *src
++))
471 return (char *)kalloc(size
);
483 * Routine: mig_object_init
485 * Initialize the base class portion of a MIG object. We
486 * will lazy init the port, so just clear it for now.
490 mig_object_t mig_object
,
491 const IMIGObject
*interface
)
493 if (mig_object
== MIG_OBJECT_NULL
)
494 return KERN_INVALID_ARGUMENT
;
495 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
496 mig_object
->port
= MACH_PORT_NULL
;
501 * Routine: mig_object_destroy
503 * The object is being freed. This call lets us clean
504 * up any state we have have built up over the object's
507 * Since notifications and the port hold references on
508 * on the object, neither can exist when this is called.
509 * This is a good place to assert() that condition.
513 __assert_only mig_object_t mig_object
)
515 assert(mig_object
->port
== MACH_PORT_NULL
);
520 * Routine: mig_object_reference
522 * Pure virtual helper to invoke the MIG object's AddRef
525 * MIG object port may be locked.
528 mig_object_reference(
529 mig_object_t mig_object
)
531 assert(mig_object
!= MIG_OBJECT_NULL
);
532 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
536 * Routine: mig_object_deallocate
538 * Pure virtual helper to invoke the MIG object's Release
544 mig_object_deallocate(
545 mig_object_t mig_object
)
547 assert(mig_object
!= MIG_OBJECT_NULL
);
548 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
552 * Routine: convert_mig_object_to_port [interface]
554 * Base implementation of MIG outtrans routine to convert from
555 * a mig object reference to a new send right on the object's
556 * port. The object reference is consumed.
558 * IP_NULL - Null MIG object supplied
559 * Otherwise, a newly made send right for the port
564 convert_mig_object_to_port(
565 mig_object_t mig_object
)
568 boolean_t deallocate
= TRUE
;
570 if (mig_object
== MIG_OBJECT_NULL
)
573 port
= mig_object
->port
;
574 while ((port
== IP_NULL
) ||
575 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
579 * Either the port was never set up, or it was just
580 * deallocated out from under us by the no-senders
581 * processing. In either case, we must:
582 * Attempt to make one
583 * Arrange for no senders
584 * Try to atomically register it with the object
585 * Destroy it if we are raced.
587 port
= ipc_port_alloc_kernel();
589 ipc_kobject_set_atomically(port
,
590 (ipc_kobject_t
) mig_object
,
593 /* make a sonce right for the notification */
597 ipc_port_nsrequest(port
, 1, port
, &previous
);
600 assert(previous
== IP_NULL
);
602 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
603 (uint32_t *)&mig_object
->port
)) {
606 ipc_port_dealloc_kernel(port
);
607 port
= mig_object
->port
;
612 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
619 * Routine: convert_port_to_mig_object [interface]
621 * Base implementation of MIG intrans routine to convert from
622 * an incoming port reference to a new reference on the
623 * underlying object. A new reference must be created, because
624 * the port's reference could go away asynchronously.
626 * NULL - Not an active MIG object port or iid not supported
627 * Otherwise, a reference to the underlying MIG interface
632 convert_port_to_mig_object(
636 mig_object_t mig_object
;
643 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
649 * Our port points to some MIG object interface. Now
650 * query it to get a reference to the desired interface.
653 mig_object
= (mig_object_t
)port
->ip_kobject
;
654 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
656 return (mig_object_t
)ppv
;
660 * Routine: mig_object_no_senders [interface]
662 * Base implementation of a no-senders notification handler
663 * for MIG objects. If there truly are no more senders, must
664 * destroy the port and drop its reference on the object.
666 * TRUE - port deallocate and reference dropped
667 * FALSE - more senders arrived, re-registered for notification
673 mig_object_no_senders(
675 mach_port_mscount_t mscount
)
677 mig_object_t mig_object
;
680 if (port
->ip_mscount
> mscount
) {
684 * Somebody created new send rights while the
685 * notification was in-flight. Just create a
686 * new send-once right and re-register with
687 * the new (higher) mscount threshold.
689 /* make a sonce right for the notification */
692 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
695 assert(previous
== IP_NULL
);
700 * Clear the port pointer while we have it locked.
702 mig_object
= (mig_object_t
)port
->ip_kobject
;
703 mig_object
->port
= IP_NULL
;
706 * Bring the sequence number and mscount in
707 * line with ipc_port_destroy assertion.
709 port
->ip_mscount
= 0;
710 port
->ip_messages
.imq_seqno
= 0;
711 ipc_port_destroy(port
); /* releases lock */
714 * Release the port's reference on the object.
716 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
721 * Kernel implementation of the notification chain for MIG object
722 * is kept separate from the actual objects, since there are expected
723 * to be much fewer of them than actual objects.
725 * The implementation of this part of MIG objects is coming
726 * "Real Soon Now"(TM).