2 * Copyright (c) 2000 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 <libkern/OSTypes.h>
54 #include <libkern/OSAtomic.h>
56 #include <mach/boolean.h>
57 #include <mach/port.h>
59 #include <mach/mig_errors.h>
60 #include <mach/mach_types.h>
61 #include <mach/mach_traps.h>
64 #include <kern/ipc_tt.h>
65 #include <kern/ipc_mig.h>
66 #include <kern/task.h>
67 #include <kern/thread.h>
68 #include <kern/ipc_kobject.h>
69 #include <kern/misc_protos.h>
71 #include <ipc/ipc_kmsg.h>
72 #include <ipc/ipc_entry.h>
73 #include <ipc/ipc_object.h>
74 #include <ipc/ipc_mqueue.h>
75 #include <ipc/ipc_space.h>
76 #include <ipc/ipc_port.h>
77 #include <ipc/ipc_pset.h>
78 #include <vm/vm_map.h>
81 * Routine: mach_msg_send_from_kernel
83 * Send a message from the kernel.
85 * This is used by the client side of KernelUser interfaces
86 * to implement SimpleRoutines. Currently, this includes
87 * memory_object messages.
91 * MACH_MSG_SUCCESS Sent the message.
92 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
93 * MACH_SEND_INVALID_DEST Bad destination port.
97 mach_msg_send_from_kernel(
98 mach_msg_header_t
*msg
,
99 mach_msg_size_t send_size
)
102 mach_msg_return_t mr
;
104 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
105 return MACH_SEND_INVALID_DEST
;
107 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
108 if (mr
!= MACH_MSG_SUCCESS
)
111 ipc_kmsg_copyin_from_kernel(kmsg
);
112 ipc_kmsg_send_always(kmsg
);
114 return MACH_MSG_SUCCESS
;
118 * Routine: mach_msg_rpc_from_kernel
120 * Send a message from the kernel and receive a reply.
121 * Uses ith_rpc_reply for the reply port.
123 * This is used by the client side of KernelUser interfaces
124 * to implement Routines.
128 * MACH_MSG_SUCCESS Sent the message.
129 * MACH_RCV_PORT_DIED The reply port was deallocated.
133 mach_msg_rpc_from_kernel(
134 mach_msg_header_t
*msg
,
135 mach_msg_size_t send_size
,
136 mach_msg_size_t rcv_size
)
138 thread_t self
= current_thread();
141 mach_port_seqno_t seqno
;
142 mach_msg_return_t mr
;
144 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
145 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
147 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
148 if (mr
!= MACH_MSG_SUCCESS
)
153 reply
= self
->ith_rpc_reply
;
154 if (reply
== IP_NULL
) {
156 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
);
172 ipc_kmsg_copyin_from_kernel(kmsg
);
174 ipc_kmsg_send_always(kmsg
);
180 if ( !ip_active(reply
)) {
182 ipc_port_release(reply
);
183 return MACH_RCV_PORT_DIED
;
185 if (!self
->top_act
|| !self
->top_act
->active
) {
187 ipc_port_release(reply
);
188 return MACH_RCV_INTERRUPTED
;
191 assert(reply
->ip_pset_count
== 0);
192 mqueue
= &reply
->ip_messages
;
195 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
197 ipc_mqueue_receive(mqueue
,
198 MACH_MSG_OPTION_NONE
,
200 MACH_MSG_TIMEOUT_NONE
,
201 THREAD_INTERRUPTIBLE
);
203 mr
= self
->ith_state
;
204 kmsg
= self
->ith_kmsg
;
205 seqno
= self
->ith_seqno
;
207 if (mr
== MACH_MSG_SUCCESS
)
212 assert(mr
== MACH_RCV_INTERRUPTED
);
214 if (self
->top_act
&& self
->top_act
->handlers
) {
215 ipc_port_release(reply
);
219 ipc_port_release(reply
);
222 * XXXXX Set manually for now ...
223 * No, why even bother, since the effort is wasted?
225 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
226 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
227 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
228 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
232 if (rcv_size
< kmsg
->ikm_header
.msgh_size
) {
233 ipc_kmsg_copyout_dest(kmsg
, ipc_space_reply
);
234 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
);
235 return MACH_RCV_TOO_LARGE
;
239 * We want to preserve rights and memory in reply!
240 * We don't have to put them anywhere; just leave them
244 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
245 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
);
246 return MACH_MSG_SUCCESS
;
250 /************** These Calls are set up for kernel-loaded tasks **************/
251 /************** Apple does not plan on supporting that. These **************/
252 /************** need to be reworked to deal with the kernel **************/
253 /************** proper to eliminate the kernel specific code MIG **************/
254 /************** must generate. **************/
260 * Like mach_msg_overwrite_trap except that message buffers
261 * live in kernel space. Doesn't handle any options.
263 * This is used by in-kernel server threads to make
264 * kernel calls, to receive request messages, and
265 * to send reply messages.
273 mach_msg_header_t
*msg
,
274 mach_msg_option_t option
,
275 mach_msg_size_t send_size
,
276 mach_msg_size_t rcv_size
,
277 mach_port_name_t rcv_name
,
278 mach_msg_timeout_t timeout
,
279 mach_port_name_t notify
,
280 mach_msg_header_t
*rcv_msg
,
281 mach_msg_size_t rcv_msg_size
)
283 ipc_space_t space
= current_space();
284 vm_map_t map
= current_map();
286 mach_port_seqno_t seqno
;
287 mach_msg_return_t mr
;
288 mach_msg_format_0_trailer_t
*trailer
;
290 if (option
& MACH_SEND_MSG
) {
291 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
292 if (mr
!= MACH_MSG_SUCCESS
)
295 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
296 if (mr
!= MACH_MSG_SUCCESS
) {
302 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
303 MACH_MSG_TIMEOUT_NONE
);
304 while (mr
== MACH_SEND_INTERRUPTED
);
305 assert(mr
== MACH_MSG_SUCCESS
);
308 if (option
& MACH_RCV_MSG
) {
309 thread_t self
= current_thread();
315 mr
= ipc_mqueue_copyin(space
, rcv_name
,
317 if (mr
!= MACH_MSG_SUCCESS
)
319 /* hold ref for object */
321 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
322 ipc_mqueue_receive(mqueue
,
323 MACH_MSG_OPTION_NONE
,
325 MACH_MSG_TIMEOUT_NONE
,
327 mr
= self
->ith_state
;
328 kmsg
= self
->ith_kmsg
;
329 seqno
= self
->ith_seqno
;
331 ipc_object_release(object
);
333 } while (mr
== MACH_RCV_INTERRUPTED
);
334 if (mr
!= MACH_MSG_SUCCESS
)
337 trailer
= (mach_msg_format_0_trailer_t
*)
338 ((vm_offset_t
)&kmsg
->ikm_header
+ kmsg
->ikm_header
.msgh_size
);
339 if (option
& MACH_RCV_TRAILER_MASK
) {
340 trailer
->msgh_seqno
= seqno
;
341 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
344 if (rcv_size
< (kmsg
->ikm_header
.msgh_size
+ trailer
->msgh_trailer_size
)) {
345 ipc_kmsg_copyout_dest(kmsg
, space
);
346 ipc_kmsg_put_to_kernel(msg
, kmsg
, sizeof *msg
);
347 return MACH_RCV_TOO_LARGE
;
350 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
352 if (mr
!= MACH_MSG_SUCCESS
) {
353 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
354 ipc_kmsg_put_to_kernel(msg
, kmsg
,
355 kmsg
->ikm_header
.msgh_size
+ trailer
->msgh_trailer_size
);
357 ipc_kmsg_copyout_dest(kmsg
, space
);
358 ipc_kmsg_put_to_kernel(msg
, kmsg
, sizeof *msg
);
364 ipc_kmsg_put_to_kernel(msg
, kmsg
,
365 kmsg
->ikm_header
.msgh_size
+ trailer
->msgh_trailer_size
);
368 return MACH_MSG_SUCCESS
;
372 * Routine: mig_get_reply_port
374 * Called by client side interfaces living in the kernel
375 * to get a reply port. This port is used for
376 * mach_msg() calls which are kernel calls.
379 mig_get_reply_port(void)
381 thread_t self
= current_thread();
383 assert(self
->ith_mig_reply
== (mach_port_t
)0);
386 * JMM - for now we have no real clients of this under the kernel
387 * loaded server model because we only have one of those. In order
388 * to avoid MIG changes, we just return null here - and return]
389 * references to ipc_port_t's instead of names.
391 * if (self->ith_mig_reply == MACH_PORT_NULL)
392 * self->ith_mig_reply = mach_reply_port();
394 return self
->ith_mig_reply
;
398 * Routine: mig_dealloc_reply_port
400 * Called by client side interfaces to get rid of a reply port.
401 * Shouldn't ever be called inside the kernel, because
402 * kernel calls shouldn't prompt Mig to call it.
406 mig_dealloc_reply_port(
407 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 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
);
471 kfree((vm_offset_t
)data
, 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 assert(mig_object
!= MIG_OBJECT_NULL
);
486 mig_object
->pVtbl
= (IMIGObjectVtbl
*)interface
;
487 mig_object
->port
= MACH_PORT_NULL
;
491 * Routine: mig_object_destroy
493 * The object is being freed. This call lets us clean
494 * up any state we have have built up over the object's
497 * Since notifications and the port hold references on
498 * on the object, neither can exist when this is called.
499 * This is a good place to assert() that condition.
503 mig_object_t mig_object
)
505 assert(mig_object
->port
== MACH_PORT_NULL
);
510 * Routine: mig_object_reference
512 * Pure virtual helper to invoke the MIG object's AddRef
515 * MIG object port may be locked.
518 mig_object_reference(
519 mig_object_t mig_object
)
521 assert(mig_object
!= MIG_OBJECT_NULL
);
522 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
526 * Routine: mig_object_deallocate
528 * Pure virtual helper to invoke the MIG object's Release
534 mig_object_deallocate(
535 mig_object_t mig_object
)
537 assert(mig_object
!= MIG_OBJECT_NULL
);
538 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
542 * Routine: convert_mig_object_to_port [interface]
544 * Base implementation of MIG outtrans routine to convert from
545 * a mig object reference to a new send right on the object's
546 * port. The object reference is consumed.
548 * IP_NULL - Null MIG object supplied
549 * Otherwise, a newly made send right for the port
554 convert_mig_object_to_port(
555 mig_object_t mig_object
)
558 boolean_t deallocate
= TRUE
;
560 if (mig_object
== MIG_OBJECT_NULL
)
563 port
= mig_object
->port
;
564 while ((port
== IP_NULL
) ||
565 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
569 * Either the port was never set up, or it was just
570 * deallocated out from under us by the no-senders
571 * processing. In either case, we must:
572 * Attempt to make one
573 * Arrange for no senders
574 * Try to atomically register it with the object
575 * Destroy it if we are raced.
577 port
= ipc_port_alloc_kernel();
579 ipc_kobject_set_atomically(port
,
580 (ipc_kobject_t
) mig_object
,
583 /* make a sonce right for the notification */
587 ipc_port_nsrequest(port
, 1, port
, &previous
);
590 assert(previous
== IP_NULL
);
592 if (OSCompareAndSwap((UInt32
)IP_NULL
,
594 (UInt32
*)&mig_object
->port
)) {
597 ipc_port_dealloc_kernel(port
);
598 port
= mig_object
->port
;
603 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
610 * Routine: convert_port_to_mig_object [interface]
612 * Base implementation of MIG intrans routine to convert from
613 * an incoming port reference to a new reference on the
614 * underlying object. A new reference must be created, because
615 * the port's reference could go away asynchronously.
617 * NULL - Not an active MIG object port or iid not supported
618 * Otherwise, a reference to the underlying MIG interface
623 convert_port_to_mig_object(
627 mig_object_t mig_object
;
634 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
640 * Our port points to some MIG object interface. Now
641 * query it to get a reference to the desired interface.
644 mig_object
= (mig_object_t
)port
->ip_kobject
;
645 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
647 return (mig_object_t
)ppv
;
651 * Routine: mig_object_no_senders [interface]
653 * Base implementation of a no-senders notification handler
654 * for MIG objects. If there truly are no more senders, must
655 * destroy the port and drop its reference on the object.
657 * TRUE - port deallocate and reference dropped
658 * FALSE - more senders arrived, re-registered for notification
664 mig_object_no_senders(
666 mach_port_mscount_t mscount
)
668 mig_object_t mig_object
;
671 if (port
->ip_mscount
> mscount
) {
675 * Somebody created new send rights while the
676 * notification was in-flight. Just create a
677 * new send-once right and re-register with
678 * the new (higher) mscount threshold.
680 /* make a sonce right for the notification */
683 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
686 assert(previous
== IP_NULL
);
691 * Clear the port pointer while we have it locked.
693 mig_object
= (mig_object_t
)port
->ip_kobject
;
694 mig_object
->port
= IP_NULL
;
697 * Bring the sequence number and mscount in
698 * line with ipc_port_destroy assertion.
700 port
->ip_mscount
= 0;
701 port
->ip_messages
.imq_seqno
= 0;
702 ipc_port_destroy(port
); /* releases lock */
705 * Release the port's reference on the object.
707 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
712 * Kernel implementation of the notification chain for MIG object
713 * is kept separate from the actual objects, since there are expected
714 * to be much fewer of them than actual objects.
716 * The implementation of this part of MIG objects is coming
717 * "Real Soon Now"(TM).