2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 #include <mach/boolean.h>
54 #include <mach/port.h>
56 #include <mach/mig_errors.h>
57 #include <mach/mach_types.h>
58 #include <mach/mach_traps.h>
60 #include <kern/ipc_tt.h>
61 #include <kern/ipc_mig.h>
62 #include <kern/kalloc.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/ipc_kobject.h>
66 #include <kern/misc_protos.h>
69 #include <ipc/ipc_kmsg.h>
70 #include <ipc/ipc_entry.h>
71 #include <ipc/ipc_object.h>
72 #include <ipc/ipc_mqueue.h>
73 #include <ipc/ipc_space.h>
74 #include <ipc/ipc_port.h>
75 #include <ipc/ipc_pset.h>
76 #include <vm/vm_map.h>
79 * Routine: mach_msg_send_from_kernel
81 * Send a message from the kernel.
83 * This is used by the client side of KernelUser interfaces
84 * to implement SimpleRoutines. Currently, this includes
85 * memory_object messages.
89 * MACH_MSG_SUCCESS Sent the message.
90 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
91 * MACH_SEND_INVALID_DEST Bad destination port.
95 mach_msg_send_from_kernel(
96 mach_msg_header_t
*msg
,
97 mach_msg_size_t send_size
)
100 mach_msg_return_t mr
;
102 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
103 return MACH_SEND_INVALID_DEST
;
105 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
106 if (mr
!= MACH_MSG_SUCCESS
)
109 ipc_kmsg_copyin_from_kernel(kmsg
);
110 ipc_kmsg_send_always(kmsg
);
112 return MACH_MSG_SUCCESS
;
116 * Routine: mach_msg_rpc_from_kernel
118 * Send a message from the kernel and receive a reply.
119 * Uses ith_rpc_reply for the reply port.
121 * This is used by the client side of KernelUser interfaces
122 * to implement Routines.
126 * MACH_MSG_SUCCESS Sent the message.
127 * MACH_RCV_PORT_DIED The reply port was deallocated.
131 mach_msg_rpc_from_kernel(
132 mach_msg_header_t
*msg
,
133 mach_msg_size_t send_size
,
134 mach_msg_size_t rcv_size
)
136 thread_t self
= current_thread();
139 mach_port_seqno_t seqno
;
140 mach_msg_return_t mr
;
142 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
143 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
145 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
146 if (mr
!= MACH_MSG_SUCCESS
)
149 reply
= self
->ith_rpc_reply
;
150 if (reply
== IP_NULL
) {
151 reply
= ipc_port_alloc_reply();
152 if ((reply
== IP_NULL
) ||
153 (self
->ith_rpc_reply
!= IP_NULL
))
154 panic("mach_msg_rpc_from_kernel");
155 self
->ith_rpc_reply
= reply
;
158 /* insert send-once right for the reply port */
159 kmsg
->ikm_header
->msgh_local_port
= reply
;
160 kmsg
->ikm_header
->msgh_bits
|=
161 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
163 ipc_port_reference(reply
);
165 ipc_kmsg_copyin_from_kernel(kmsg
);
167 ipc_kmsg_send_always(kmsg
);
173 if ( !ip_active(reply
)) {
175 ipc_port_release(reply
);
176 return MACH_RCV_PORT_DIED
;
180 ipc_port_release(reply
);
181 return MACH_RCV_INTERRUPTED
;
184 assert(reply
->ip_pset_count
== 0);
185 mqueue
= &reply
->ip_messages
;
188 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
190 ipc_mqueue_receive(mqueue
,
191 MACH_MSG_OPTION_NONE
,
193 MACH_MSG_TIMEOUT_NONE
,
194 THREAD_INTERRUPTIBLE
);
196 mr
= self
->ith_state
;
197 kmsg
= self
->ith_kmsg
;
198 seqno
= self
->ith_seqno
;
200 if (mr
== MACH_MSG_SUCCESS
)
205 assert(mr
== MACH_RCV_INTERRUPTED
);
207 if (self
->handlers
) {
208 ipc_port_release(reply
);
212 ipc_port_release(reply
);
215 * XXXXX Set manually for now ...
216 * No, why even bother, since the effort is wasted?
218 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
219 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
220 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
221 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
225 if (rcv_size
< kmsg
->ikm_header
->msgh_size
) {
226 ipc_kmsg_copyout_dest(kmsg
, ipc_space_reply
);
227 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
228 return MACH_RCV_TOO_LARGE
;
232 * We want to preserve rights and memory in reply!
233 * We don't have to put them anywhere; just leave them
237 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
238 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
->msgh_size
);
239 return MACH_MSG_SUCCESS
;
243 /************** These Calls are set up for kernel-loaded tasks/threads **************/
246 * Routine: mach_msg_overwrite
248 * Like mach_msg_overwrite_trap except that message buffers
249 * live in kernel space. Doesn't handle any options.
251 * This is used by in-kernel server threads to make
252 * kernel calls, to receive request messages, and
253 * to send reply messages.
261 mach_msg_header_t
*msg
,
262 mach_msg_option_t option
,
263 mach_msg_size_t send_size
,
264 mach_msg_size_t rcv_size
,
265 mach_port_name_t rcv_name
,
266 __unused mach_msg_timeout_t msg_timeout
,
267 __unused mach_port_name_t notify
,
268 __unused mach_msg_header_t
*rcv_msg
,
269 __unused mach_msg_size_t rcv_msg_size
)
271 ipc_space_t space
= current_space();
272 vm_map_t map
= current_map();
274 mach_port_seqno_t seqno
;
275 mach_msg_return_t mr
;
276 mach_msg_format_0_trailer_t
*trailer
;
278 if (option
& MACH_SEND_MSG
) {
279 mach_msg_size_t msg_and_trailer_size
;
280 mach_msg_max_trailer_t
*max_trailer
;
282 if ((send_size
< sizeof(mach_msg_header_t
)) || (send_size
& 3))
283 return MACH_SEND_MSG_TOO_SMALL
;
285 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
287 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
289 if (kmsg
== IKM_NULL
)
290 return MACH_SEND_NO_BUFFER
;
292 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
294 kmsg
->ikm_header
->msgh_size
= send_size
;
297 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
298 * However, the internal size field of the trailer (msgh_trailer_size)
299 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
300 * the cases where no implicit data is requested.
302 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
303 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
304 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
305 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
306 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
308 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
309 if (mr
!= MACH_MSG_SUCCESS
) {
315 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
316 MACH_MSG_TIMEOUT_NONE
);
317 while (mr
== MACH_SEND_INTERRUPTED
);
318 assert(mr
== MACH_MSG_SUCCESS
);
321 if (option
& MACH_RCV_MSG
) {
322 thread_t self
= current_thread();
328 mr
= ipc_mqueue_copyin(space
, rcv_name
,
330 if (mr
!= MACH_MSG_SUCCESS
)
332 /* hold ref for object */
334 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
335 ipc_mqueue_receive(mqueue
,
336 MACH_MSG_OPTION_NONE
,
338 MACH_MSG_TIMEOUT_NONE
,
340 mr
= self
->ith_state
;
341 kmsg
= self
->ith_kmsg
;
342 seqno
= self
->ith_seqno
;
344 ipc_object_release(object
);
346 } while (mr
== MACH_RCV_INTERRUPTED
);
347 if (mr
!= MACH_MSG_SUCCESS
)
350 trailer
= (mach_msg_format_0_trailer_t
*)
351 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
352 if (option
& MACH_RCV_TRAILER_MASK
) {
353 trailer
->msgh_seqno
= seqno
;
354 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
357 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
)) {
358 ipc_kmsg_copyout_dest(kmsg
, space
);
359 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
361 return MACH_RCV_TOO_LARGE
;
364 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
366 if (mr
!= MACH_MSG_SUCCESS
) {
367 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
368 ipc_kmsg_put_to_kernel(msg
, kmsg
,
369 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
371 ipc_kmsg_copyout_dest(kmsg
, space
);
372 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
379 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
380 kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
);
384 return MACH_MSG_SUCCESS
;
388 * Routine: mig_get_reply_port
390 * Called by client side interfaces living in the kernel
391 * to get a reply port.
394 mig_get_reply_port(void)
396 return (MACH_PORT_NULL
);
400 * Routine: mig_dealloc_reply_port
402 * Called by client side interfaces to get rid of a reply port.
406 mig_dealloc_reply_port(
407 __unused mach_port_t reply_port
)
409 panic("mig_dealloc_reply_port");
413 * Routine: mig_put_reply_port
415 * Called by client side interfaces after each RPC to
416 * let the client recycle the reply port if it wishes.
420 __unused mach_port_t reply_port
)
425 * mig_strncpy.c - by Joshua Block
427 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
428 * OUGHT to do: Copies the (null terminated) string in src into dest, a
429 * buffer of length len. Assures that the copy is still null terminated
430 * and doesn't overflow the buffer, truncating the copy if necessary.
434 * dest - Pointer to destination buffer.
436 * src - Pointer to source string.
438 * len - Length of destination buffer.
451 for (i
=1; i
<len
; i
++)
452 if (! (*dest
++ = *src
++))
463 return (char *)kalloc(size
);
475 * Routine: mig_object_init
477 * Initialize the base class portion of a MIG object. We
478 * will lazy init the port, so just clear it for now.
482 mig_object_t mig_object
,
483 const IMIGObject
*interface
)
485 if (mig_object
== MIG_OBJECT_NULL
)
486 return KERN_INVALID_ARGUMENT
;
487 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
488 mig_object
->port
= MACH_PORT_NULL
;
493 * Routine: mig_object_destroy
495 * The object is being freed. This call lets us clean
496 * up any state we have have built up over the object's
499 * Since notifications and the port hold references on
500 * on the object, neither can exist when this is called.
501 * This is a good place to assert() that condition.
505 __assert_only mig_object_t mig_object
)
507 assert(mig_object
->port
== MACH_PORT_NULL
);
512 * Routine: mig_object_reference
514 * Pure virtual helper to invoke the MIG object's AddRef
517 * MIG object port may be locked.
520 mig_object_reference(
521 mig_object_t mig_object
)
523 assert(mig_object
!= MIG_OBJECT_NULL
);
524 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
528 * Routine: mig_object_deallocate
530 * Pure virtual helper to invoke the MIG object's Release
536 mig_object_deallocate(
537 mig_object_t mig_object
)
539 assert(mig_object
!= MIG_OBJECT_NULL
);
540 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
544 * Routine: convert_mig_object_to_port [interface]
546 * Base implementation of MIG outtrans routine to convert from
547 * a mig object reference to a new send right on the object's
548 * port. The object reference is consumed.
550 * IP_NULL - Null MIG object supplied
551 * Otherwise, a newly made send right for the port
556 convert_mig_object_to_port(
557 mig_object_t mig_object
)
560 boolean_t deallocate
= TRUE
;
562 if (mig_object
== MIG_OBJECT_NULL
)
565 port
= mig_object
->port
;
566 while ((port
== IP_NULL
) ||
567 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
571 * Either the port was never set up, or it was just
572 * deallocated out from under us by the no-senders
573 * processing. In either case, we must:
574 * Attempt to make one
575 * Arrange for no senders
576 * Try to atomically register it with the object
577 * Destroy it if we are raced.
579 port
= ipc_port_alloc_kernel();
581 ipc_kobject_set_atomically(port
,
582 (ipc_kobject_t
) mig_object
,
585 /* make a sonce right for the notification */
589 ipc_port_nsrequest(port
, 1, port
, &previous
);
592 assert(previous
== IP_NULL
);
594 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
595 (uint32_t *)&mig_object
->port
)) {
598 ipc_port_dealloc_kernel(port
);
599 port
= mig_object
->port
;
604 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
611 * Routine: convert_port_to_mig_object [interface]
613 * Base implementation of MIG intrans routine to convert from
614 * an incoming port reference to a new reference on the
615 * underlying object. A new reference must be created, because
616 * the port's reference could go away asynchronously.
618 * NULL - Not an active MIG object port or iid not supported
619 * Otherwise, a reference to the underlying MIG interface
624 convert_port_to_mig_object(
628 mig_object_t mig_object
;
635 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
641 * Our port points to some MIG object interface. Now
642 * query it to get a reference to the desired interface.
645 mig_object
= (mig_object_t
)port
->ip_kobject
;
646 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
648 return (mig_object_t
)ppv
;
652 * Routine: mig_object_no_senders [interface]
654 * Base implementation of a no-senders notification handler
655 * for MIG objects. If there truly are no more senders, must
656 * destroy the port and drop its reference on the object.
658 * TRUE - port deallocate and reference dropped
659 * FALSE - more senders arrived, re-registered for notification
665 mig_object_no_senders(
667 mach_port_mscount_t mscount
)
669 mig_object_t mig_object
;
672 if (port
->ip_mscount
> mscount
) {
676 * Somebody created new send rights while the
677 * notification was in-flight. Just create a
678 * new send-once right and re-register with
679 * the new (higher) mscount threshold.
681 /* make a sonce right for the notification */
684 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
687 assert(previous
== IP_NULL
);
692 * Clear the port pointer while we have it locked.
694 mig_object
= (mig_object_t
)port
->ip_kobject
;
695 mig_object
->port
= IP_NULL
;
698 * Bring the sequence number and mscount in
699 * line with ipc_port_destroy assertion.
701 port
->ip_mscount
= 0;
702 port
->ip_messages
.imq_seqno
= 0;
703 ipc_port_destroy(port
); /* releases lock */
706 * Release the port's reference on the object.
708 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
713 * Kernel implementation of the notification chain for MIG object
714 * is kept separate from the actual objects, since there are expected
715 * to be much fewer of them than actual objects.
717 * The implementation of this part of MIG objects is coming
718 * "Real Soon Now"(TM).