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 <ipc/ipc_notify.h>
83 #include <vm/vm_map.h>
85 #include <libkern/OSAtomic.h>
88 mach_msg_receive_results_complete(ipc_object_t object
);
91 * Routine: mach_msg_send_from_kernel
93 * Send a message from the kernel.
95 * This is used by the client side of KernelUser interfaces
96 * to implement SimpleRoutines. Currently, this includes
97 * memory_object messages.
101 * MACH_MSG_SUCCESS Sent the message.
102 * MACH_SEND_INVALID_DEST Bad destination port.
103 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
104 * or destination is above kernel limit
107 #if IKM_SUPPORT_LEGACY
109 #undef mach_msg_send_from_kernel
110 mach_msg_return_t
mach_msg_send_from_kernel(
111 mach_msg_header_t
*msg
,
112 mach_msg_size_t send_size
);
115 mach_msg_send_from_kernel(
116 mach_msg_header_t
*msg
,
117 mach_msg_size_t send_size
)
120 mach_msg_return_t mr
;
122 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
124 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
125 if (mr
!= MACH_MSG_SUCCESS
) {
126 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
130 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
131 if (mr
!= MACH_MSG_SUCCESS
) {
133 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
138 * respect the thread's SEND_IMPORTANCE option to allow importance
139 * donation from the kernel-side of user threads
140 * (11938665 & 23925818)
142 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
143 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
144 option
&= ~MACH_SEND_NOIMPORTANCE
;
146 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
147 if (mr
!= MACH_MSG_SUCCESS
) {
148 ipc_kmsg_destroy(kmsg
);
149 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
155 #endif /* IKM_SUPPORT_LEGACY */
158 mach_msg_send_from_kernel_proper(
159 mach_msg_header_t
*msg
,
160 mach_msg_size_t send_size
)
163 mach_msg_return_t mr
;
165 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
167 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
168 if (mr
!= MACH_MSG_SUCCESS
) {
169 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
173 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
174 if (mr
!= MACH_MSG_SUCCESS
) {
176 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
181 * respect the thread's SEND_IMPORTANCE option to force importance
182 * donation from the kernel-side of user threads
183 * (11938665 & 23925818)
185 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
186 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
187 option
&= ~MACH_SEND_NOIMPORTANCE
;
189 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
190 if (mr
!= MACH_MSG_SUCCESS
) {
191 ipc_kmsg_destroy(kmsg
);
192 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
199 mach_msg_send_from_kernel_with_options(
200 mach_msg_header_t
*msg
,
201 mach_msg_size_t send_size
,
202 mach_msg_option_t option
,
203 mach_msg_timeout_t timeout_val
)
206 mach_msg_return_t mr
;
208 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
210 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
211 if (mr
!= MACH_MSG_SUCCESS
) {
212 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
216 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
217 if (mr
!= MACH_MSG_SUCCESS
) {
219 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
224 * Until we are sure of its effects, we are disabling
225 * importance donation from the kernel-side of user
226 * threads in importance-donating tasks - unless the
227 * option to force importance donation is passed in,
228 * or the thread's SEND_IMPORTANCE option has been set.
229 * (11938665 & 23925818)
231 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
232 option
&= ~MACH_SEND_NOIMPORTANCE
;
233 else if ((option
& MACH_SEND_IMPORTANCE
) == 0)
234 option
|= MACH_SEND_NOIMPORTANCE
;
236 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
238 if (mr
!= MACH_MSG_SUCCESS
) {
239 ipc_kmsg_destroy(kmsg
);
240 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
247 #if IKM_SUPPORT_LEGACY
250 mach_msg_send_from_kernel_with_options_legacy(
251 mach_msg_header_t
*msg
,
252 mach_msg_size_t send_size
,
253 mach_msg_option_t option
,
254 mach_msg_timeout_t timeout_val
)
257 mach_msg_return_t mr
;
259 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
261 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
262 if (mr
!= MACH_MSG_SUCCESS
) {
263 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
267 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
268 if (mr
!= MACH_MSG_SUCCESS
) {
270 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
275 * Until we are sure of its effects, we are disabling
276 * importance donation from the kernel-side of user
277 * threads in importance-donating tasks.
278 * (11938665 & 23925818)
280 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
281 option
&= ~MACH_SEND_NOIMPORTANCE
;
283 option
|= MACH_SEND_NOIMPORTANCE
;
285 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
287 if (mr
!= MACH_MSG_SUCCESS
) {
288 ipc_kmsg_destroy(kmsg
);
289 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
295 #endif /* IKM_SUPPORT_LEGACY */
298 * Routine: mach_msg_rpc_from_kernel
300 * Send a message from the kernel and receive a reply.
301 * Uses ith_rpc_reply for the reply port.
303 * This is used by the client side of KernelUser interfaces
304 * to implement Routines.
308 * MACH_MSG_SUCCESS Sent the message.
309 * MACH_RCV_PORT_DIED The reply port was deallocated.
312 mach_msg_return_t
mach_msg_rpc_from_kernel_body(mach_msg_header_t
*msg
,
313 mach_msg_size_t send_size
, mach_msg_size_t rcv_size
, boolean_t legacy
);
315 #if IKM_SUPPORT_LEGACY
317 #undef mach_msg_rpc_from_kernel
319 mach_msg_rpc_from_kernel(
320 mach_msg_header_t
*msg
,
321 mach_msg_size_t send_size
,
322 mach_msg_size_t rcv_size
);
325 mach_msg_rpc_from_kernel(
326 mach_msg_header_t
*msg
,
327 mach_msg_size_t send_size
,
328 mach_msg_size_t rcv_size
)
330 return mach_msg_rpc_from_kernel_body(msg
, send_size
, rcv_size
, TRUE
);
333 #endif /* IKM_SUPPORT_LEGACY */
336 mach_msg_rpc_from_kernel_proper(
337 mach_msg_header_t
*msg
,
338 mach_msg_size_t send_size
,
339 mach_msg_size_t rcv_size
)
341 return mach_msg_rpc_from_kernel_body(msg
, send_size
, rcv_size
, FALSE
);
345 mach_msg_rpc_from_kernel_body(
346 mach_msg_header_t
*msg
,
347 mach_msg_size_t send_size
,
348 mach_msg_size_t rcv_size
,
349 #if !IKM_SUPPORT_LEGACY
354 thread_t self
= current_thread();
357 mach_port_seqno_t seqno
;
358 mach_msg_return_t mr
;
360 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
362 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
364 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
365 if (mr
!= MACH_MSG_SUCCESS
) {
366 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
370 reply
= self
->ith_rpc_reply
;
371 if (reply
== IP_NULL
) {
372 reply
= ipc_port_alloc_reply();
373 if ((reply
== IP_NULL
) ||
374 (self
->ith_rpc_reply
!= IP_NULL
))
375 panic("mach_msg_rpc_from_kernel");
376 self
->ith_rpc_reply
= reply
;
379 /* insert send-once right for the reply port */
380 kmsg
->ikm_header
->msgh_local_port
= reply
;
381 kmsg
->ikm_header
->msgh_bits
|=
382 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
384 #if IKM_SUPPORT_LEGACY
386 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
388 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
390 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
392 if (mr
!= MACH_MSG_SUCCESS
) {
394 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
399 * respect the thread's SEND_IMPORTANCE option to force importance
400 * donation from the kernel-side of user threads
401 * (11938665 & 23925818)
403 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
404 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
)
405 option
&= ~MACH_SEND_NOIMPORTANCE
;
407 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
408 if (mr
!= MACH_MSG_SUCCESS
) {
409 ipc_kmsg_destroy(kmsg
);
410 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
418 assert(reply
->ip_in_pset
== 0);
419 assert(ip_active(reply
));
421 /* JMM - why this check? */
422 if (!self
->active
&& !self
->inspection
) {
423 ipc_port_dealloc_reply(reply
);
424 self
->ith_rpc_reply
= IP_NULL
;
425 return MACH_RCV_INTERRUPTED
;
428 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
430 mqueue
= &reply
->ip_messages
;
431 ipc_mqueue_receive(mqueue
,
432 MACH_MSG_OPTION_NONE
,
434 MACH_MSG_TIMEOUT_NONE
,
435 THREAD_INTERRUPTIBLE
);
437 mr
= self
->ith_state
;
438 kmsg
= self
->ith_kmsg
;
439 seqno
= self
->ith_seqno
;
441 __IGNORE_WCASTALIGN(object
= (ipc_object_t
) reply
);
442 mach_msg_receive_results_complete(object
);
444 if (mr
== MACH_MSG_SUCCESS
)
449 assert(mr
== MACH_RCV_INTERRUPTED
);
451 assert(reply
== self
->ith_rpc_reply
);
453 if (self
->ast
& AST_APC
) {
454 ipc_port_dealloc_reply(reply
);
455 self
->ith_rpc_reply
= IP_NULL
;
461 * Check to see how much of the message/trailer can be received.
462 * We chose the maximum trailer that will fit, since we don't
463 * have options telling us which trailer elements the caller needed.
465 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
) {
466 mach_msg_format_0_trailer_t
*trailer
= (mach_msg_format_0_trailer_t
*)
467 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
469 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
+ MAX_TRAILER_SIZE
) {
470 /* Enough room for a maximum trailer */
471 trailer
->msgh_trailer_size
= MAX_TRAILER_SIZE
;
473 else if (rcv_size
< kmsg
->ikm_header
->msgh_size
+
474 trailer
->msgh_trailer_size
) {
475 /* no room for even the basic (default) trailer */
476 trailer
->msgh_trailer_size
= 0;
478 assert(trailer
->msgh_trailer_type
== MACH_MSG_TRAILER_FORMAT_0
);
479 rcv_size
= kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
;
480 mr
= MACH_MSG_SUCCESS
;
482 mr
= MACH_RCV_TOO_LARGE
;
487 * We want to preserve rights and memory in reply!
488 * We don't have to put them anywhere; just leave them
491 #if IKM_SUPPORT_LEGACY
493 ipc_kmsg_copyout_to_kernel_legacy(kmsg
, ipc_space_reply
);
495 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
497 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
499 ipc_kmsg_put_to_kernel(msg
, kmsg
, rcv_size
);
504 /************** These Calls are set up for kernel-loaded tasks/threads **************/
507 * Routine: mach_msg_overwrite
509 * Like mach_msg_overwrite_trap except that message buffers
510 * live in kernel space. Doesn't handle any options.
512 * This is used by in-kernel server threads to make
513 * kernel calls, to receive request messages, and
514 * to send reply messages.
522 mach_msg_header_t
*msg
,
523 mach_msg_option_t option
,
524 mach_msg_size_t send_size
,
525 mach_msg_size_t rcv_size
,
526 mach_port_name_t rcv_name
,
527 __unused mach_msg_timeout_t msg_timeout
,
528 mach_msg_priority_t override
,
529 __unused mach_msg_header_t
*rcv_msg
,
530 __unused mach_msg_size_t rcv_msg_size
)
532 ipc_space_t space
= current_space();
533 vm_map_t map
= current_map();
535 mach_port_seqno_t seqno
;
536 mach_msg_return_t mr
;
537 mach_msg_trailer_size_t trailer_size
;
539 if (option
& MACH_SEND_MSG
) {
540 mach_msg_size_t msg_and_trailer_size
;
541 mach_msg_max_trailer_t
*max_trailer
;
543 if ((send_size
& 3) ||
544 send_size
< sizeof(mach_msg_header_t
) ||
545 (send_size
< sizeof(mach_msg_body_t
) && (msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)))
546 return MACH_SEND_MSG_TOO_SMALL
;
548 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
)
549 return MACH_SEND_TOO_LARGE
;
551 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
553 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
554 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
556 if (kmsg
== IKM_NULL
) {
557 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_SEND_NO_BUFFER
);
558 return MACH_SEND_NO_BUFFER
;
561 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_LINK
) | DBG_FUNC_NONE
,
562 (uintptr_t)0, /* this should only be called from the kernel! */
563 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
566 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
568 kmsg
->ikm_header
->msgh_size
= send_size
;
571 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
572 * However, the internal size field of the trailer (msgh_trailer_size)
573 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
574 * the cases where no implicit data is requested.
576 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
577 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
578 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
579 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
580 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
582 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, override
, &option
);
584 if (mr
!= MACH_MSG_SUCCESS
) {
586 KDBG(MACHDBG_CODE(DBG_MACH_IPC
,MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
591 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
, MACH_MSG_TIMEOUT_NONE
);
592 } while (mr
== MACH_SEND_INTERRUPTED
);
594 assert(mr
== MACH_MSG_SUCCESS
);
597 if (option
& MACH_RCV_MSG
) {
598 thread_t self
= current_thread();
604 mr
= ipc_mqueue_copyin(space
, rcv_name
,
606 if (mr
!= MACH_MSG_SUCCESS
)
609 /* hold ref for object */
611 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
612 ipc_mqueue_receive(mqueue
,
613 MACH_MSG_OPTION_NONE
,
615 MACH_MSG_TIMEOUT_NONE
,
617 mr
= self
->ith_state
;
618 kmsg
= self
->ith_kmsg
;
619 seqno
= self
->ith_seqno
;
621 mach_msg_receive_results_complete(object
);
624 } while (mr
== MACH_RCV_INTERRUPTED
);
626 if (mr
!= MACH_MSG_SUCCESS
)
629 trailer_size
= ipc_kmsg_add_trailer(kmsg
, space
, option
, current_thread(), seqno
, TRUE
,
630 kmsg
->ikm_header
->msgh_remote_port
->ip_context
);
632 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer_size
)) {
633 ipc_kmsg_copyout_dest(kmsg
, space
);
634 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
636 return MACH_RCV_TOO_LARGE
;
639 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_MSG_BODY_NULL
, option
);
640 if (mr
!= MACH_MSG_SUCCESS
) {
641 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
642 ipc_kmsg_put_to_kernel(msg
, kmsg
,
643 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
645 ipc_kmsg_copyout_dest(kmsg
, space
);
646 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
653 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
654 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
658 return MACH_MSG_SUCCESS
;
662 * Routine: mig_get_reply_port
664 * Called by client side interfaces living in the kernel
665 * to get a reply port.
668 mig_get_reply_port(void)
670 return (MACH_PORT_NULL
);
674 * Routine: mig_dealloc_reply_port
676 * Called by client side interfaces to get rid of a reply port.
680 mig_dealloc_reply_port(
681 __unused mach_port_t reply_port
)
686 * Routine: mig_put_reply_port
688 * Called by client side interfaces after each RPC to
689 * let the client recycle the reply port if it wishes.
693 __unused mach_port_t reply_port
)
698 * mig_strncpy.c - by Joshua Block
700 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
701 * OUGHT to do: Copies the (null terminated) string in src into dest, a
702 * buffer of length len. Assures that the copy is still null terminated
703 * and doesn't overflow the buffer, truncating the copy if necessary.
707 * dest - Pointer to destination buffer.
709 * src - Pointer to source string.
711 * len - Length of destination buffer.
724 for (i
=1; i
<len
; i
++)
725 if (! (*dest
++ = *src
++))
733 * mig_strncpy_zerofill -- Bounded string copy. Does what the
734 * library routine strncpy OUGHT to do: Copies the (null terminated)
735 * string in src into dest, a buffer of length len. Assures that
736 * the copy is still null terminated and doesn't overflow the buffer,
737 * truncating the copy if necessary. If the string in src is smaller
738 * than given length len, it will zero fill the remaining bytes in dest.
742 * dest - Pointer to destination buffer.
744 * src - Pointer to source string.
746 * len - Length of destination buffer.
749 mig_strncpy_zerofill(
755 boolean_t terminated
= FALSE
;
758 if (len
<= 0 || dest
== NULL
) {
766 for (i
= 1; i
< len
; i
++) {
768 if (!(*dest
++ = *src
++)) {
789 return (char *)kalloc(size
);
801 * Routine: mig_object_init
803 * Initialize the base class portion of a MIG object. We
804 * will lazy init the port, so just clear it for now.
808 mig_object_t mig_object
,
809 const IMIGObject
*interface
)
811 if (mig_object
== MIG_OBJECT_NULL
)
812 return KERN_INVALID_ARGUMENT
;
813 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
814 mig_object
->port
= MACH_PORT_NULL
;
819 * Routine: mig_object_destroy
821 * The object is being freed. This call lets us clean
822 * up any state we have have built up over the object's
825 * Since notifications and the port hold references on
826 * on the object, neither can exist when this is called.
827 * This is a good place to assert() that condition.
831 __assert_only mig_object_t mig_object
)
833 assert(mig_object
->port
== MACH_PORT_NULL
);
838 * Routine: mig_object_reference
840 * Pure virtual helper to invoke the MIG object's AddRef
843 * MIG object port may be locked.
846 mig_object_reference(
847 mig_object_t mig_object
)
849 assert(mig_object
!= MIG_OBJECT_NULL
);
850 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
854 * Routine: mig_object_deallocate
856 * Pure virtual helper to invoke the MIG object's Release
862 mig_object_deallocate(
863 mig_object_t mig_object
)
865 assert(mig_object
!= MIG_OBJECT_NULL
);
866 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
870 * Routine: convert_mig_object_to_port [interface]
872 * Base implementation of MIG outtrans routine to convert from
873 * a mig object reference to a new send right on the object's
874 * port. The object reference is consumed.
876 * IP_NULL - Null MIG object supplied
877 * Otherwise, a newly made send right for the port
882 convert_mig_object_to_port(
883 mig_object_t mig_object
)
886 boolean_t deallocate
= TRUE
;
888 if (mig_object
== MIG_OBJECT_NULL
)
891 port
= mig_object
->port
;
892 while ((port
== IP_NULL
) ||
893 ((port
= ipc_port_make_send(port
)) == IP_NULL
)) {
897 * Either the port was never set up, or it was just
898 * deallocated out from under us by the no-senders
899 * processing. In either case, we must:
900 * Attempt to make one
901 * Arrange for no senders
902 * Try to atomically register it with the object
903 * Destroy it if we are raced.
905 port
= ipc_port_alloc_kernel();
907 ipc_kobject_set_atomically(port
,
908 (ipc_kobject_t
) mig_object
,
911 /* make a sonce right for the notification */
915 ipc_port_nsrequest(port
, 1, port
, &previous
);
918 assert(previous
== IP_NULL
);
920 if (OSCompareAndSwapPtr((void *)IP_NULL
, (void *)port
,
921 (void * volatile *)&mig_object
->port
)) {
924 ipc_port_dealloc_kernel(port
);
925 port
= mig_object
->port
;
930 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
937 * Routine: convert_port_to_mig_object [interface]
939 * Base implementation of MIG intrans routine to convert from
940 * an incoming port reference to a new reference on the
941 * underlying object. A new reference must be created, because
942 * the port's reference could go away asynchronously.
944 * NULL - Not an active MIG object port or iid not supported
945 * Otherwise, a reference to the underlying MIG interface
950 convert_port_to_mig_object(
954 mig_object_t mig_object
;
961 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
967 * Our port points to some MIG object interface. Now
968 * query it to get a reference to the desired interface.
971 mig_object
= (mig_object_t
)port
->ip_kobject
;
972 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
974 return (mig_object_t
)ppv
;
978 * Routine: mig_object_no_senders [interface]
980 * Base implementation of a no-senders notification handler
981 * for MIG objects. If there truly are no more senders, must
982 * destroy the port and drop its reference on the object.
984 * TRUE - port deallocate and reference dropped
985 * FALSE - more senders arrived, re-registered for notification
991 mig_object_no_senders(
993 mach_port_mscount_t mscount
)
995 mig_object_t mig_object
;
998 if (port
->ip_mscount
> mscount
) {
1002 * Somebody created new send rights while the
1003 * notification was in-flight. Just create a
1004 * new send-once right and re-register with
1005 * the new (higher) mscount threshold.
1007 /* make a sonce right for the notification */
1008 port
->ip_sorights
++;
1010 ipc_port_nsrequest(port
, mscount
, port
, &previous
);
1013 assert(previous
== IP_NULL
);
1018 * Clear the port pointer while we have it locked.
1020 mig_object
= (mig_object_t
)port
->ip_kobject
;
1021 mig_object
->port
= IP_NULL
;
1024 * Bring the sequence number and mscount in
1025 * line with ipc_port_destroy assertion.
1027 port
->ip_mscount
= 0;
1028 port
->ip_messages
.imq_seqno
= 0;
1029 ipc_port_destroy(port
); /* releases lock */
1032 * Release the port's reference on the object.
1034 mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
);
1039 * Kernel implementation of the notification chain for MIG object
1040 * is kept separate from the actual objects, since there are expected
1041 * to be much fewer of them than actual objects.
1043 * The implementation of this part of MIG objects is coming
1044 * "Real Soon Now"(TM).