2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 #include <mach/boolean.h>
62 #include <mach/port.h>
64 #include <mach/mig_errors.h>
65 #include <mach/mach_types.h>
66 #include <mach/mach_traps.h>
68 #include <kern/ipc_tt.h>
69 #include <kern/ipc_mig.h>
70 #include <kern/kalloc.h>
71 #include <kern/task.h>
72 #include <kern/thread.h>
73 #include <kern/ipc_kobject.h>
74 #include <kern/misc_protos.h>
77 #include <ipc/ipc_kmsg.h>
78 #include <ipc/ipc_entry.h>
79 #include <ipc/ipc_object.h>
80 #include <ipc/ipc_mqueue.h>
81 #include <ipc/ipc_space.h>
82 #include <ipc/ipc_port.h>
83 #include <ipc/ipc_pset.h>
84 #include <vm/vm_map.h>
87 * Routine: mach_msg_send_from_kernel
89 * Send a message from the kernel.
91 * This is used by the client side of KernelUser interfaces
92 * to implement SimpleRoutines. Currently, this includes
93 * memory_object messages.
97 * MACH_MSG_SUCCESS Sent the message.
98 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
99 * MACH_SEND_INVALID_DEST Bad destination port.
103 mach_msg_send_from_kernel(
104 mach_msg_header_t
*msg
,
105 mach_msg_size_t send_size
)
108 mach_msg_return_t mr
;
110 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
111 return MACH_SEND_INVALID_DEST
;
113 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
114 if (mr
!= MACH_MSG_SUCCESS
)
117 ipc_kmsg_copyin_from_kernel(kmsg
);
118 ipc_kmsg_send_always(kmsg
);
120 return MACH_MSG_SUCCESS
;
124 * Routine: mach_msg_rpc_from_kernel
126 * Send a message from the kernel and receive a reply.
127 * Uses ith_rpc_reply for the reply port.
129 * This is used by the client side of KernelUser interfaces
130 * to implement Routines.
134 * MACH_MSG_SUCCESS Sent the message.
135 * MACH_RCV_PORT_DIED The reply port was deallocated.
139 mach_msg_rpc_from_kernel(
140 mach_msg_header_t
*msg
,
141 mach_msg_size_t send_size
,
142 mach_msg_size_t rcv_size
)
144 thread_t self
= current_thread();
147 mach_port_seqno_t seqno
;
148 mach_msg_return_t mr
;
150 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
151 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
153 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
154 if (mr
!= MACH_MSG_SUCCESS
)
157 reply
= self
->ith_rpc_reply
;
158 if (reply
== IP_NULL
) {
159 reply
= ipc_port_alloc_reply();
160 if ((reply
== IP_NULL
) ||
161 (self
->ith_rpc_reply
!= IP_NULL
))
162 panic("mach_msg_rpc_from_kernel");
163 self
->ith_rpc_reply
= reply
;
166 /* insert send-once right for the reply port */
167 kmsg
->ikm_header
->msgh_local_port
= reply
;
168 kmsg
->ikm_header
->msgh_bits
|=
169 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
171 ipc_port_reference(reply
);
173 ipc_kmsg_copyin_from_kernel(kmsg
);
175 ipc_kmsg_send_always(kmsg
);
181 if ( !ip_active(reply
)) {
183 ipc_port_release(reply
);
184 return MACH_RCV_PORT_DIED
;
188 ipc_port_release(reply
);
189 return MACH_RCV_INTERRUPTED
;
192 assert(reply
->ip_pset_count
== 0);
193 mqueue
= &reply
->ip_messages
;
196 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
198 ipc_mqueue_receive(mqueue
,
199 MACH_MSG_OPTION_NONE
,
201 MACH_MSG_TIMEOUT_NONE
,
202 THREAD_INTERRUPTIBLE
);
204 mr
= self
->ith_state
;
205 kmsg
= self
->ith_kmsg
;
206 seqno
= self
->ith_seqno
;
208 if (mr
== MACH_MSG_SUCCESS
)
213 assert(mr
== MACH_RCV_INTERRUPTED
);
215 if (self
->handlers
) {
216 ipc_port_release(reply
);
220 ipc_port_release(reply
);
223 * XXXXX Set manually for now ...
224 * No, why even bother, since the effort is wasted?
226 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
227 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
228 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
229 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
233 if (rcv_size
< kmsg
->ikm_header
->msgh_size
) {
234 ipc_kmsg_copyout_dest(kmsg
, ipc_space_reply
);
235 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
236 return MACH_RCV_TOO_LARGE
;
240 * We want to preserve rights and memory in reply!
241 * We don't have to put them anywhere; just leave them
245 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
246 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
247 return MACH_MSG_SUCCESS
;
251 /************** These Calls are set up for kernel-loaded tasks/threads **************/
254 * Routine: mach_msg_overwrite
256 * Like mach_msg_overwrite_trap except that message buffers
257 * live in kernel space. Doesn't handle any options.
259 * This is used by in-kernel server threads to make
260 * kernel calls, to receive request messages, and
261 * to send reply messages.
269 mach_msg_header_t
*msg
,
270 mach_msg_option_t option
,
271 mach_msg_size_t send_size
,
272 mach_msg_size_t rcv_size
,
273 mach_port_name_t rcv_name
,
274 __unused mach_msg_timeout_t msg_timeout
,
275 __unused mach_port_name_t notify
,
276 __unused mach_msg_header_t
*rcv_msg
,
277 __unused mach_msg_size_t rcv_msg_size
)
279 ipc_space_t space
= current_space();
280 vm_map_t map
= current_map();
282 mach_port_seqno_t seqno
;
283 mach_msg_return_t mr
;
284 mach_msg_format_0_trailer_t
*trailer
;
286 if (option
& MACH_SEND_MSG
) {
287 mach_msg_size_t msg_and_trailer_size
;
288 mach_msg_max_trailer_t
*max_trailer
;
290 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
291 return MACH_SEND_MSG_TOO_SMALL
;
293 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
294 return MACH_SEND_TOO_LARGE
;
296 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
297 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
299 if (kmsg
== IKM_NULL
)
300 return MACH_SEND_NO_BUFFER
;
302 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
304 kmsg
->ikm_header
->msgh_size
= send_size
;
307 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
308 * However, the internal size field of the trailer (msgh_trailer_size)
309 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
310 * the cases where no implicit data is requested.
312 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
313 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
314 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
315 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
316 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
318 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
319 if (mr
!= MACH_MSG_SUCCESS
) {
325 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
326 MACH_MSG_TIMEOUT_NONE
);
327 while (mr
== MACH_SEND_INTERRUPTED
);
328 assert(mr
== MACH_MSG_SUCCESS
);
331 if (option
& MACH_RCV_MSG
) {
332 thread_t self
= current_thread();
338 mr
= ipc_mqueue_copyin(space
, rcv_name
,
340 if (mr
!= MACH_MSG_SUCCESS
)
342 /* hold ref for object */
344 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
345 ipc_mqueue_receive(mqueue
,
346 MACH_MSG_OPTION_NONE
,
348 MACH_MSG_TIMEOUT_NONE
,
350 mr
= self
->ith_state
;
351 kmsg
= self
->ith_kmsg
;
352 seqno
= self
->ith_seqno
;
354 ipc_object_release(object
);
356 } while (mr
== MACH_RCV_INTERRUPTED
);
357 if (mr
!= MACH_MSG_SUCCESS
)
360 trailer
= (mach_msg_format_0_trailer_t
*)
361 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
362 if (option
& MACH_RCV_TRAILER_MASK
) {
363 trailer
->msgh_seqno
= seqno
;
364 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
367 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
)) {
368 ipc_kmsg_copyout_dest(kmsg
, space
);
369 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
371 return MACH_RCV_TOO_LARGE
;
374 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
376 if (mr
!= MACH_MSG_SUCCESS
) {
377 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
378 ipc_kmsg_put_to_kernel(msg
, kmsg
,
379 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
381 ipc_kmsg_copyout_dest(kmsg
, space
);
382 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
389 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
390 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
394 return MACH_MSG_SUCCESS
;
398 * Routine: mig_get_reply_port
400 * Called by client side interfaces living in the kernel
401 * to get a reply port.
404 mig_get_reply_port(void)
406 return (MACH_PORT_NULL
);
410 * Routine: mig_dealloc_reply_port
412 * Called by client side interfaces to get rid of a reply port.
416 mig_dealloc_reply_port(
417 __unused mach_port_t reply_port
)
419 panic("mig_dealloc_reply_port");
423 * Routine: mig_put_reply_port
425 * Called by client side interfaces after each RPC to
426 * let the client recycle the reply port if it wishes.
430 __unused mach_port_t reply_port
)
435 * mig_strncpy.c - by Joshua Block
437 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
438 * OUGHT to do: Copies the (null terminated) string in src into dest, a
439 * buffer of length len. Assures that the copy is still null terminated
440 * and doesn't overflow the buffer, truncating the copy if necessary.
444 * dest - Pointer to destination buffer.
446 * src - Pointer to source string.
448 * len - Length of destination buffer.
461 for (i
=1; i
<len
; i
++)
462 if (! (*dest
++ = *src
++))
473 return (char *)kalloc(size
);
485 * Routine: mig_object_init
487 * Initialize the base class portion of a MIG object. We
488 * will lazy init the port, so just clear it for now.
492 mig_object_t mig_object
,
493 const IMIGObject
*interface
)
495 if (mig_object
== MIG_OBJECT_NULL
)
496 return KERN_INVALID_ARGUMENT
;
497 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
498 mig_object
->port
= MACH_PORT_NULL
;
503 * Routine: mig_object_destroy
505 * The object is being freed. This call lets us clean
506 * up any state we have have built up over the object's
509 * Since notifications and the port hold references on
510 * on the object, neither can exist when this is called.
511 * This is a good place to assert() that condition.
515 __assert_only mig_object_t mig_object
)
517 assert(mig_object
->port
== MACH_PORT_NULL
);
522 * Routine: mig_object_reference
524 * Pure virtual helper to invoke the MIG object's AddRef
527 * MIG object port may be locked.
530 mig_object_reference(
531 mig_object_t mig_object
)
533 assert(mig_object
!= MIG_OBJECT_NULL
);
534 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
538 * Routine: mig_object_deallocate
540 * Pure virtual helper to invoke the MIG object's Release
546 mig_object_deallocate(
547 mig_object_t mig_object
)
549 assert(mig_object
!= MIG_OBJECT_NULL
);
550 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
554 * Routine: convert_mig_object_to_port [interface]
556 * Base implementation of MIG outtrans routine to convert from
557 * a mig object reference to a new send right on the object's
558 * port. The object reference is consumed.
560 * IP_NULL - Null MIG object supplied
561 * Otherwise, a newly made send right for the port
566 convert_mig_object_to_port(
567 mig_object_t mig_object
)
570 boolean_t deallocate
= TRUE
;
572 if (mig_object
== MIG_OBJECT_NULL
)
575 port
= mig_object
->port
;
576 while ((port
== IP_NULL
) ||
577 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
581 * Either the port was never set up, or it was just
582 * deallocated out from under us by the no-senders
583 * processing. In either case, we must:
584 * Attempt to make one
585 * Arrange for no senders
586 * Try to atomically register it with the object
587 * Destroy it if we are raced.
589 port
= ipc_port_alloc_kernel();
591 ipc_kobject_set_atomically(port
,
592 (ipc_kobject_t
) mig_object
,
595 /* make a sonce right for the notification */
599 ipc_port_nsrequest(port
, 1, port
, &previous
);
602 assert(previous
== IP_NULL
);
604 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
605 (uint32_t *)&mig_object
->port
)) {
608 ipc_port_dealloc_kernel(port
);
609 port
= mig_object
->port
;
614 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
621 * Routine: convert_port_to_mig_object [interface]
623 * Base implementation of MIG intrans routine to convert from
624 * an incoming port reference to a new reference on the
625 * underlying object. A new reference must be created, because
626 * the port's reference could go away asynchronously.
628 * NULL - Not an active MIG object port or iid not supported
629 * Otherwise, a reference to the underlying MIG interface
634 convert_port_to_mig_object(
638 mig_object_t mig_object
;
645 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
651 * Our port points to some MIG object interface. Now
652 * query it to get a reference to the desired interface.
655 mig_object
= (mig_object_t
)port
->ip_kobject
;
656 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
658 return (mig_object_t
)ppv
;
662 * Routine: mig_object_no_senders [interface]
664 * Base implementation of a no-senders notification handler
665 * for MIG objects. If there truly are no more senders, must
666 * destroy the port and drop its reference on the object.
668 * TRUE - port deallocate and reference dropped
669 * FALSE - more senders arrived, re-registered for notification
675 mig_object_no_senders(
677 mach_port_mscount_t mscount
)
679 mig_object_t mig_object
;
682 if (port
->ip_mscount
> mscount
) {
686 * Somebody created new send rights while the
687 * notification was in-flight. Just create a
688 * new send-once right and re-register with
689 * the new (higher) mscount threshold.
691 /* make a sonce right for the notification */
694 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
697 assert(previous
== IP_NULL
);
702 * Clear the port pointer while we have it locked.
704 mig_object
= (mig_object_t
)port
->ip_kobject
;
705 mig_object
->port
= IP_NULL
;
708 * Bring the sequence number and mscount in
709 * line with ipc_port_destroy assertion.
711 port
->ip_mscount
= 0;
712 port
->ip_messages
.imq_seqno
= 0;
713 ipc_port_destroy(port
); /* releases lock */
716 * Release the port's reference on the object.
718 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
723 * Kernel implementation of the notification chain for MIG object
724 * is kept separate from the actual objects, since there are expected
725 * to be much fewer of them than actual objects.
727 * The implementation of this part of MIG objects is coming
728 * "Real Soon Now"(TM).