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 <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>
61 #include <kern/ipc_tt.h>
62 #include <kern/ipc_mig.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/ipc_kobject.h>
66 #include <kern/misc_protos.h>
68 #include <ipc/ipc_kmsg.h>
69 #include <ipc/ipc_entry.h>
70 #include <ipc/ipc_object.h>
71 #include <ipc/ipc_mqueue.h>
72 #include <ipc/ipc_space.h>
73 #include <ipc/ipc_port.h>
74 #include <ipc/ipc_pset.h>
75 #include <vm/vm_map.h>
78 * Routine: mach_msg_send_from_kernel
80 * Send a message from the kernel.
82 * This is used by the client side of KernelUser interfaces
83 * to implement SimpleRoutines. Currently, this includes
84 * memory_object messages.
88 * MACH_MSG_SUCCESS Sent the message.
89 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
90 * MACH_SEND_INVALID_DEST Bad destination port.
94 mach_msg_send_from_kernel(
95 mach_msg_header_t
*msg
,
96 mach_msg_size_t send_size
)
101 if (!MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
))
102 return MACH_SEND_INVALID_DEST
;
104 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
105 if (mr
!= MACH_MSG_SUCCESS
)
108 ipc_kmsg_copyin_from_kernel(kmsg
);
109 ipc_kmsg_send_always(kmsg
);
111 return MACH_MSG_SUCCESS
;
115 * Routine: mach_msg_rpc_from_kernel
117 * Send a message from the kernel and receive a reply.
118 * Uses ith_rpc_reply for the reply port.
120 * This is used by the client side of KernelUser interfaces
121 * to implement Routines.
125 * MACH_MSG_SUCCESS Sent the message.
126 * MACH_RCV_PORT_DIED The reply port was deallocated.
130 mach_msg_rpc_from_kernel(
131 mach_msg_header_t
*msg
,
132 mach_msg_size_t send_size
,
133 mach_msg_size_t rcv_size
)
135 thread_t self
= current_thread();
138 mach_port_seqno_t seqno
;
139 mach_msg_return_t mr
;
141 assert(MACH_PORT_VALID((mach_port_name_t
)msg
->msgh_remote_port
));
142 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
144 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
145 if (mr
!= MACH_MSG_SUCCESS
)
148 reply
= self
->ith_rpc_reply
;
149 if (reply
== IP_NULL
) {
150 reply
= ipc_port_alloc_reply();
151 if ((reply
== IP_NULL
) ||
152 (self
->ith_rpc_reply
!= IP_NULL
))
153 panic("mach_msg_rpc_from_kernel");
154 self
->ith_rpc_reply
= reply
;
157 /* insert send-once right for the reply port */
158 kmsg
->ikm_header
.msgh_local_port
= reply
;
159 kmsg
->ikm_header
.msgh_bits
|=
160 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
162 ipc_port_reference(reply
);
164 ipc_kmsg_copyin_from_kernel(kmsg
);
166 ipc_kmsg_send_always(kmsg
);
172 if ( !ip_active(reply
)) {
174 ipc_port_release(reply
);
175 return MACH_RCV_PORT_DIED
;
177 if (!self
->top_act
|| !self
->top_act
->active
) {
179 ipc_port_release(reply
);
180 return MACH_RCV_INTERRUPTED
;
183 assert(reply
->ip_pset_count
== 0);
184 mqueue
= &reply
->ip_messages
;
187 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
189 ipc_mqueue_receive(mqueue
,
190 MACH_MSG_OPTION_NONE
,
192 MACH_MSG_TIMEOUT_NONE
,
193 THREAD_INTERRUPTIBLE
);
195 mr
= self
->ith_state
;
196 kmsg
= self
->ith_kmsg
;
197 seqno
= self
->ith_seqno
;
199 if (mr
== MACH_MSG_SUCCESS
)
204 assert(mr
== MACH_RCV_INTERRUPTED
);
206 if (self
->top_act
&& self
->top_act
->handlers
) {
207 ipc_port_release(reply
);
211 ipc_port_release(reply
);
214 * XXXXX Set manually for now ...
215 * No, why even bother, since the effort is wasted?
217 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
218 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
219 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
220 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
224 if (rcv_size
< kmsg
->ikm_header
.msgh_size
) {
225 ipc_kmsg_copyout_dest(kmsg
, ipc_space_reply
);
226 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
);
227 return MACH_RCV_TOO_LARGE
;
231 * We want to preserve rights and memory in reply!
232 * We don't have to put them anywhere; just leave them
236 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
237 ipc_kmsg_put_to_kernel(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
);
238 return MACH_MSG_SUCCESS
;
242 /************** These Calls are set up for kernel-loaded tasks **************/
243 /************** Apple does not plan on supporting that. These **************/
244 /************** need to be reworked to deal with the kernel **************/
245 /************** proper to eliminate the kernel specific code MIG **************/
246 /************** must generate. **************/
252 * Like mach_msg_overwrite_trap except that message buffers
253 * live in kernel space. Doesn't handle any options.
255 * This is used by in-kernel server threads to make
256 * kernel calls, to receive request messages, and
257 * to send reply messages.
265 mach_msg_header_t
*msg
,
266 mach_msg_option_t option
,
267 mach_msg_size_t send_size
,
268 mach_msg_size_t rcv_size
,
269 mach_port_name_t rcv_name
,
270 mach_msg_timeout_t timeout
,
271 mach_port_name_t notify
,
272 mach_msg_header_t
*rcv_msg
,
273 mach_msg_size_t rcv_msg_size
)
275 ipc_space_t space
= current_space();
276 vm_map_t map
= current_map();
278 mach_port_seqno_t seqno
;
279 mach_msg_return_t mr
;
280 mach_msg_format_0_trailer_t
*trailer
;
282 if (option
& MACH_SEND_MSG
) {
283 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
284 if (mr
!= MACH_MSG_SUCCESS
)
287 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
288 if (mr
!= MACH_MSG_SUCCESS
) {
294 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
295 MACH_MSG_TIMEOUT_NONE
);
296 while (mr
== MACH_SEND_INTERRUPTED
);
297 assert(mr
== MACH_MSG_SUCCESS
);
300 if (option
& MACH_RCV_MSG
) {
301 thread_t self
= current_thread();
307 mr
= ipc_mqueue_copyin(space
, rcv_name
,
309 if (mr
!= MACH_MSG_SUCCESS
)
311 /* hold ref for object */
313 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
314 ipc_mqueue_receive(mqueue
,
315 MACH_MSG_OPTION_NONE
,
317 MACH_MSG_TIMEOUT_NONE
,
319 mr
= self
->ith_state
;
320 kmsg
= self
->ith_kmsg
;
321 seqno
= self
->ith_seqno
;
323 ipc_object_release(object
);
325 } while (mr
== MACH_RCV_INTERRUPTED
);
326 if (mr
!= MACH_MSG_SUCCESS
)
329 trailer
= (mach_msg_format_0_trailer_t
*)
330 ((vm_offset_t
)&kmsg
->ikm_header
+ kmsg
->ikm_header
.msgh_size
);
331 if (option
& MACH_RCV_TRAILER_MASK
) {
332 trailer
->msgh_seqno
= seqno
;
333 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
336 if (rcv_size
< (kmsg
->ikm_header
.msgh_size
+ trailer
->msgh_trailer_size
)) {
337 ipc_kmsg_copyout_dest(kmsg
, space
);
338 ipc_kmsg_put_to_kernel(msg
, kmsg
, sizeof *msg
);
339 return MACH_RCV_TOO_LARGE
;
342 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
,
344 if (mr
!= MACH_MSG_SUCCESS
) {
345 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
346 ipc_kmsg_put_to_kernel(msg
, kmsg
,
347 kmsg
->ikm_header
.msgh_size
+ trailer
->msgh_trailer_size
);
349 ipc_kmsg_copyout_dest(kmsg
, space
);
350 ipc_kmsg_put_to_kernel(msg
, kmsg
, sizeof *msg
);
356 ipc_kmsg_put_to_kernel(msg
, kmsg
,
357 kmsg
->ikm_header
.msgh_size
+ trailer
->msgh_trailer_size
);
360 return MACH_MSG_SUCCESS
;
364 * Routine: mig_get_reply_port
366 * Called by client side interfaces living in the kernel
367 * to get a reply port. This port is used for
368 * mach_msg() calls which are kernel calls.
371 mig_get_reply_port(void)
373 thread_t self
= current_thread();
375 assert(self
->ith_mig_reply
== (mach_port_t
)0);
378 * JMM - for now we have no real clients of this under the kernel
379 * loaded server model because we only have one of those. In order
380 * to avoid MIG changes, we just return null here - and return]
381 * references to ipc_port_t's instead of names.
383 * if (self->ith_mig_reply == MACH_PORT_NULL)
384 * self->ith_mig_reply = mach_reply_port();
386 return self
->ith_mig_reply
;
390 * Routine: mig_dealloc_reply_port
392 * Called by client side interfaces to get rid of a reply port.
393 * Shouldn't ever be called inside the kernel, because
394 * kernel calls shouldn't prompt Mig to call it.
398 mig_dealloc_reply_port(
399 mach_port_t reply_port
)
401 panic("mig_dealloc_reply_port");
405 * Routine: mig_put_reply_port
407 * Called by client side interfaces after each RPC to
408 * let the client recycle the reply port if it wishes.
412 mach_port_t reply_port
)
417 * mig_strncpy.c - by Joshua Block
419 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
420 * OUGHT to do: Copies the (null terminated) string in src into dest, a
421 * buffer of length len. Assures that the copy is still null terminated
422 * and doesn't overflow the buffer, truncating the copy if necessary.
426 * dest - Pointer to destination buffer.
428 * src - Pointer to source string.
430 * len - Length of destination buffer.
443 for (i
=1; i
<len
; i
++)
444 if (! (*dest
++ = *src
++))
455 return (char *)kalloc(size
);
463 kfree((vm_offset_t
)data
, size
);
467 * Routine: mig_object_init
469 * Initialize the base class portion of a MIG object. We
470 * will lazy init the port, so just clear it for now.
474 mig_object_t mig_object
,
475 const IMIGObject
*interface
)
477 assert(mig_object
!= MIG_OBJECT_NULL
);
478 mig_object
->pVtbl
= (IMIGObjectVtbl
*)interface
;
479 mig_object
->port
= MACH_PORT_NULL
;
483 * Routine: mig_object_destroy
485 * The object is being freed. This call lets us clean
486 * up any state we have have built up over the object's
489 * Since notifications and the port hold references on
490 * on the object, neither can exist when this is called.
491 * This is a good place to assert() that condition.
495 mig_object_t mig_object
)
497 assert(mig_object
->port
== MACH_PORT_NULL
);
502 * Routine: mig_object_reference
504 * Pure virtual helper to invoke the MIG object's AddRef
507 * MIG object port may be locked.
510 mig_object_reference(
511 mig_object_t mig_object
)
513 assert(mig_object
!= MIG_OBJECT_NULL
);
514 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
518 * Routine: mig_object_deallocate
520 * Pure virtual helper to invoke the MIG object's Release
526 mig_object_deallocate(
527 mig_object_t mig_object
)
529 assert(mig_object
!= MIG_OBJECT_NULL
);
530 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
534 * Routine: convert_mig_object_to_port [interface]
536 * Base implementation of MIG outtrans routine to convert from
537 * a mig object reference to a new send right on the object's
538 * port. The object reference is consumed.
540 * IP_NULL - Null MIG object supplied
541 * Otherwise, a newly made send right for the port
546 convert_mig_object_to_port(
547 mig_object_t mig_object
)
550 boolean_t deallocate
= TRUE
;
552 if (mig_object
== MIG_OBJECT_NULL
)
555 port
= mig_object
->port
;
556 while ((port
== IP_NULL
) ||
557 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
561 * Either the port was never set up, or it was just
562 * deallocated out from under us by the no-senders
563 * processing. In either case, we must:
564 * Attempt to make one
565 * Arrange for no senders
566 * Try to atomically register it with the object
567 * Destroy it if we are raced.
569 port
= ipc_port_alloc_kernel();
571 ipc_kobject_set_atomically(port
,
572 (ipc_kobject_t
) mig_object
,
575 /* make a sonce right for the notification */
579 ipc_port_nsrequest(port
, 1, port
, &previous
);
582 assert(previous
== IP_NULL
);
584 if (hw_compare_and_store((uint32_t)IP_NULL
, (uint32_t)port
,
585 (uint32_t *)&mig_object
->port
)) {
588 ipc_port_dealloc_kernel(port
);
589 port
= mig_object
->port
;
594 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
601 * Routine: convert_port_to_mig_object [interface]
603 * Base implementation of MIG intrans routine to convert from
604 * an incoming port reference to a new reference on the
605 * underlying object. A new reference must be created, because
606 * the port's reference could go away asynchronously.
608 * NULL - Not an active MIG object port or iid not supported
609 * Otherwise, a reference to the underlying MIG interface
614 convert_port_to_mig_object(
618 mig_object_t mig_object
;
625 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
631 * Our port points to some MIG object interface. Now
632 * query it to get a reference to the desired interface.
635 mig_object
= (mig_object_t
)port
->ip_kobject
;
636 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
638 return (mig_object_t
)ppv
;
642 * Routine: mig_object_no_senders [interface]
644 * Base implementation of a no-senders notification handler
645 * for MIG objects. If there truly are no more senders, must
646 * destroy the port and drop its reference on the object.
648 * TRUE - port deallocate and reference dropped
649 * FALSE - more senders arrived, re-registered for notification
655 mig_object_no_senders(
657 mach_port_mscount_t mscount
)
659 mig_object_t mig_object
;
662 if (port
->ip_mscount
> mscount
) {
666 * Somebody created new send rights while the
667 * notification was in-flight. Just create a
668 * new send-once right and re-register with
669 * the new (higher) mscount threshold.
671 /* make a sonce right for the notification */
674 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
677 assert(previous
== IP_NULL
);
682 * Clear the port pointer while we have it locked.
684 mig_object
= (mig_object_t
)port
->ip_kobject
;
685 mig_object
->port
= IP_NULL
;
688 * Bring the sequence number and mscount in
689 * line with ipc_port_destroy assertion.
691 port
->ip_mscount
= 0;
692 port
->ip_messages
.imq_seqno
= 0;
693 ipc_port_destroy(port
); /* releases lock */
696 * Release the port's reference on the object.
698 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
703 * Kernel implementation of the notification chain for MIG object
704 * is kept separate from the actual objects, since there are expected
705 * to be much fewer of them than actual objects.
707 * The implementation of this part of MIG objects is coming
708 * "Real Soon Now"(TM).