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
;
147 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
148 if (mr
!= MACH_MSG_SUCCESS
) {
149 ipc_kmsg_destroy(kmsg
);
150 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
156 #endif /* IKM_SUPPORT_LEGACY */
159 mach_msg_send_from_kernel_proper(
160 mach_msg_header_t
*msg
,
161 mach_msg_size_t send_size
)
164 mach_msg_return_t mr
;
166 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
168 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
169 if (mr
!= MACH_MSG_SUCCESS
) {
170 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
174 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
175 if (mr
!= MACH_MSG_SUCCESS
) {
177 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
182 * respect the thread's SEND_IMPORTANCE option to force importance
183 * donation from the kernel-side of user threads
184 * (11938665 & 23925818)
186 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
187 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
) {
188 option
&= ~MACH_SEND_NOIMPORTANCE
;
191 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
192 if (mr
!= MACH_MSG_SUCCESS
) {
193 ipc_kmsg_destroy(kmsg
);
194 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
201 mach_msg_send_from_kernel_with_options(
202 mach_msg_header_t
*msg
,
203 mach_msg_size_t send_size
,
204 mach_msg_option_t option
,
205 mach_msg_timeout_t timeout_val
)
207 return kernel_mach_msg_send(msg
, send_size
, option
, timeout_val
, NULL
);
211 kernel_mach_msg_send(
212 mach_msg_header_t
*msg
,
213 mach_msg_size_t send_size
,
214 mach_msg_option_t option
,
215 mach_msg_timeout_t timeout_val
,
216 boolean_t
*message_moved
)
219 mach_msg_return_t mr
;
221 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
224 *message_moved
= FALSE
;
227 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
228 if (mr
!= MACH_MSG_SUCCESS
) {
229 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
233 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
234 if (mr
!= MACH_MSG_SUCCESS
) {
236 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
241 *message_moved
= TRUE
;
245 * Until we are sure of its effects, we are disabling
246 * importance donation from the kernel-side of user
247 * threads in importance-donating tasks - unless the
248 * option to force importance donation is passed in,
249 * or the thread's SEND_IMPORTANCE option has been set.
250 * (11938665 & 23925818)
252 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
) {
253 option
&= ~MACH_SEND_NOIMPORTANCE
;
254 } else if ((option
& MACH_SEND_IMPORTANCE
) == 0) {
255 option
|= MACH_SEND_NOIMPORTANCE
;
258 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
260 if (mr
!= MACH_MSG_SUCCESS
) {
261 ipc_kmsg_destroy(kmsg
);
262 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
269 #if IKM_SUPPORT_LEGACY
272 mach_msg_send_from_kernel_with_options_legacy(
273 mach_msg_header_t
*msg
,
274 mach_msg_size_t send_size
,
275 mach_msg_option_t option
,
276 mach_msg_timeout_t timeout_val
)
279 mach_msg_return_t mr
;
281 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
283 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
284 if (mr
!= MACH_MSG_SUCCESS
) {
285 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
289 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
290 if (mr
!= MACH_MSG_SUCCESS
) {
292 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
297 * Until we are sure of its effects, we are disabling
298 * importance donation from the kernel-side of user
299 * threads in importance-donating tasks.
300 * (11938665 & 23925818)
302 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
) {
303 option
&= ~MACH_SEND_NOIMPORTANCE
;
305 option
|= MACH_SEND_NOIMPORTANCE
;
308 mr
= ipc_kmsg_send(kmsg
, option
, timeout_val
);
310 if (mr
!= MACH_MSG_SUCCESS
) {
311 ipc_kmsg_destroy(kmsg
);
312 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
318 #endif /* IKM_SUPPORT_LEGACY */
321 * Routine: mach_msg_rpc_from_kernel
323 * Send a message from the kernel and receive a reply.
324 * Uses ith_rpc_reply for the reply port.
326 * This is used by the client side of KernelUser interfaces
327 * to implement Routines.
331 * MACH_MSG_SUCCESS Sent the message.
332 * MACH_RCV_PORT_DIED The reply port was deallocated.
335 #if IKM_SUPPORT_LEGACY
337 #undef mach_msg_rpc_from_kernel
339 mach_msg_rpc_from_kernel(
340 mach_msg_header_t
*msg
,
341 mach_msg_size_t send_size
,
342 mach_msg_size_t rcv_size
);
345 mach_msg_rpc_from_kernel(
346 mach_msg_header_t
*msg
,
347 mach_msg_size_t send_size
,
348 mach_msg_size_t rcv_size
)
350 return kernel_mach_msg_rpc(msg
, send_size
, rcv_size
, TRUE
, TRUE
, NULL
);
352 #endif /* IKM_SUPPORT_LEGACY */
355 mach_msg_rpc_from_kernel_proper(
356 mach_msg_header_t
*msg
,
357 mach_msg_size_t send_size
,
358 mach_msg_size_t rcv_size
)
360 return kernel_mach_msg_rpc(msg
, send_size
, rcv_size
, FALSE
, TRUE
, NULL
);
365 mach_msg_header_t
*msg
,
366 mach_msg_size_t send_size
,
367 mach_msg_size_t rcv_size
,
368 #if !IKM_SUPPORT_LEGACY
372 boolean_t interruptible
,
373 boolean_t
*message_moved
)
375 thread_t self
= current_thread();
378 mach_port_seqno_t seqno
;
379 mach_msg_return_t mr
;
381 assert(msg
->msgh_local_port
== MACH_PORT_NULL
);
383 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
386 *message_moved
= FALSE
;
389 mr
= ipc_kmsg_get_from_kernel(msg
, send_size
, &kmsg
);
390 if (mr
!= MACH_MSG_SUCCESS
) {
391 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
395 reply
= self
->ith_rpc_reply
;
396 if (reply
== IP_NULL
) {
397 reply
= ipc_port_alloc_reply();
398 if ((reply
== IP_NULL
) ||
399 (self
->ith_rpc_reply
!= IP_NULL
)) {
400 panic("mach_msg_rpc_from_kernel");
402 self
->ith_rpc_reply
= reply
;
405 /* insert send-once right for the reply port */
406 kmsg
->ikm_header
->msgh_local_port
= reply
;
407 kmsg
->ikm_header
->msgh_bits
|=
408 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE
);
410 #if IKM_SUPPORT_LEGACY
412 mr
= ipc_kmsg_copyin_from_kernel_legacy(kmsg
);
414 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
417 mr
= ipc_kmsg_copyin_from_kernel(kmsg
);
419 if (mr
!= MACH_MSG_SUCCESS
) {
421 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
426 *message_moved
= TRUE
;
430 * respect the thread's SEND_IMPORTANCE option to force importance
431 * donation from the kernel-side of user threads
432 * (11938665 & 23925818)
434 mach_msg_option_t option
= MACH_SEND_KERNEL_DEFAULT
;
435 if (current_thread()->options
& TH_OPT_SEND_IMPORTANCE
) {
436 option
&= ~MACH_SEND_NOIMPORTANCE
;
439 mr
= ipc_kmsg_send(kmsg
, option
, MACH_MSG_TIMEOUT_NONE
);
440 if (mr
!= MACH_MSG_SUCCESS
) {
441 ipc_kmsg_destroy(kmsg
);
442 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
449 assert(reply
->ip_in_pset
== 0);
450 require_ip_active(reply
);
452 /* JMM - why this check? */
453 if (interruptible
&& !self
->active
&& !self
->inspection
) {
454 ipc_port_dealloc_reply(reply
);
455 self
->ith_rpc_reply
= IP_NULL
;
456 return MACH_RCV_INTERRUPTED
;
459 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
461 mqueue
= &reply
->ip_messages
;
462 ipc_mqueue_receive(mqueue
,
463 MACH_MSG_OPTION_NONE
,
465 MACH_MSG_TIMEOUT_NONE
,
466 interruptible
? THREAD_INTERRUPTIBLE
: THREAD_UNINT
);
468 mr
= self
->ith_state
;
469 kmsg
= self
->ith_kmsg
;
470 seqno
= self
->ith_seqno
;
472 mach_msg_receive_results_complete(ip_to_object(reply
));
474 if (mr
== MACH_MSG_SUCCESS
) {
478 assert(mr
== MACH_RCV_INTERRUPTED
);
479 assert(interruptible
);
480 assert(reply
== self
->ith_rpc_reply
);
482 if (self
->ast
& AST_APC
) {
483 ipc_port_dealloc_reply(reply
);
484 self
->ith_rpc_reply
= IP_NULL
;
489 mach_msg_format_0_trailer_t
*trailer
= (mach_msg_format_0_trailer_t
*)
490 ((vm_offset_t
)kmsg
->ikm_header
+ kmsg
->ikm_header
->msgh_size
);
492 /* must be able to receive message proper */
493 if (rcv_size
< kmsg
->ikm_header
->msgh_size
) {
494 ipc_kmsg_destroy(kmsg
);
495 return MACH_RCV_TOO_LARGE
;
499 * We want to preserve rights and memory in reply!
500 * We don't have to put them anywhere; just leave them
503 #if IKM_SUPPORT_LEGACY
505 ipc_kmsg_copyout_to_kernel_legacy(kmsg
, ipc_space_reply
);
507 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
510 ipc_kmsg_copyout_to_kernel(kmsg
, ipc_space_reply
);
513 /* Determine what trailer bits we can receive (as no option specified) */
514 if (rcv_size
< kmsg
->ikm_header
->msgh_size
+ MACH_MSG_TRAILER_MINIMUM_SIZE
) {
515 rcv_size
= kmsg
->ikm_header
->msgh_size
;
517 if (rcv_size
>= kmsg
->ikm_header
->msgh_size
+ MAX_TRAILER_SIZE
) {
519 * Enough room for a maximum trailer.
520 * JMM - we really should set the expected receiver-set fields:
521 * (seqno, context, filterid, etc...) but nothing currently
522 * expects them anyway.
524 trailer
->msgh_trailer_size
= MAX_TRAILER_SIZE
;
526 assert(trailer
->msgh_trailer_size
== MACH_MSG_TRAILER_MINIMUM_SIZE
);
528 rcv_size
= kmsg
->ikm_header
->msgh_size
+ trailer
->msgh_trailer_size
;
530 assert(trailer
->msgh_trailer_type
== MACH_MSG_TRAILER_FORMAT_0
);
531 mr
= MACH_MSG_SUCCESS
;
533 ipc_kmsg_put_to_kernel(msg
, kmsg
, rcv_size
);
538 * Routine: mach_msg_destroy_from_kernel_proper
540 * mach_msg_destroy_from_kernel_proper is used to destroy
541 * an unwanted/unexpected reply message from a MIG
542 * kernel-specific user-side stub. It is like ipc_kmsg_destroy(),
543 * except we no longer have the kmsg - just the contents.
546 mach_msg_destroy_from_kernel_proper(mach_msg_header_t
*msg
)
548 mach_msg_bits_t mbits
= msg
->msgh_bits
;
551 object
= (ipc_object_t
) msg
->msgh_remote_port
;
552 if (IO_VALID(object
)) {
553 ipc_object_destroy(object
, MACH_MSGH_BITS_REMOTE(mbits
));
557 * The destination (now in msg->msgh_local_port via
558 * ipc_kmsg_copyout_to_kernel) has been consumed with
559 * ipc_object_copyout_dest.
562 /* MIG kernel users don't receive vouchers */
563 assert(!MACH_MSGH_BITS_VOUCHER(mbits
));
565 /* For simple messages, we're done */
566 if ((mbits
& MACH_MSGH_BITS_COMPLEX
) == 0) {
570 /* Discard descriptor contents */
571 mach_msg_body_t
*body
= (mach_msg_body_t
*)(msg
+ 1);
572 mach_msg_descriptor_t
*daddr
= (mach_msg_descriptor_t
*)(body
+ 1);
575 for (i
= 0; i
< body
->msgh_descriptor_count
; i
++, daddr
++) {
576 switch (daddr
->type
.type
) {
577 case MACH_MSG_PORT_DESCRIPTOR
: {
578 mach_msg_port_descriptor_t
*dsc
= &daddr
->port
;
579 if (IO_VALID((ipc_object_t
) dsc
->name
)) {
580 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
584 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
:
585 case MACH_MSG_OOL_DESCRIPTOR
: {
586 mach_msg_ool_descriptor_t
*dsc
=
587 (mach_msg_ool_descriptor_t
*)&daddr
->out_of_line
;
590 vm_map_copy_discard((vm_map_copy_t
) dsc
->address
);
592 assert(dsc
->address
== (void *) 0);
596 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
597 ipc_object_t
*objects
;
598 mach_msg_type_number_t j
;
599 mach_msg_ool_ports_descriptor_t
*dsc
;
601 dsc
= (mach_msg_ool_ports_descriptor_t
*)&daddr
->ool_ports
;
602 objects
= (ipc_object_t
*) dsc
->address
;
604 if (dsc
->count
== 0) {
607 assert(objects
!= 0);
608 for (j
= 0; j
< dsc
->count
; j
++) {
610 if (IO_VALID(object
)) {
611 ipc_object_destroy(object
, dsc
->disposition
);
614 kfree(dsc
->address
, (vm_size_t
) dsc
->count
* sizeof(mach_port_t
));
617 case MACH_MSG_GUARDED_PORT_DESCRIPTOR
: {
618 mach_msg_guarded_port_descriptor_t
*dsc
= (mach_msg_guarded_port_descriptor_t
*)&daddr
->guarded_port
;
619 if (IO_VALID((ipc_object_t
) dsc
->name
)) {
620 ipc_object_destroy((ipc_object_t
) dsc
->name
, dsc
->disposition
);
630 /************** These Calls are set up for kernel-loaded tasks/threads **************/
633 * Routine: mach_msg_overwrite
635 * Like mach_msg_overwrite_trap except that message buffers
636 * live in kernel space. Doesn't handle any options.
638 * This is used by in-kernel server threads to make
639 * kernel calls, to receive request messages, and
640 * to send reply messages.
648 mach_msg_header_t
*msg
,
649 mach_msg_option_t option
,
650 mach_msg_size_t send_size
,
651 mach_msg_size_t rcv_size
,
652 mach_port_name_t rcv_name
,
653 __unused mach_msg_timeout_t msg_timeout
,
654 mach_msg_priority_t priority
,
655 __unused mach_msg_header_t
*rcv_msg
,
656 __unused mach_msg_size_t rcv_msg_size
)
658 ipc_space_t space
= current_space();
659 vm_map_t map
= current_map();
661 mach_port_seqno_t seqno
;
662 mach_msg_return_t mr
;
663 mach_msg_trailer_size_t trailer_size
;
665 if (option
& MACH_SEND_MSG
) {
666 mach_msg_size_t msg_and_trailer_size
;
667 mach_msg_max_trailer_t
*max_trailer
;
669 if ((send_size
& 3) ||
670 send_size
< sizeof(mach_msg_header_t
) ||
671 (send_size
< sizeof(mach_msg_base_t
) && (msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
))) {
672 return MACH_SEND_MSG_TOO_SMALL
;
675 if (send_size
> MACH_MSG_SIZE_MAX
- MAX_TRAILER_SIZE
) {
676 return MACH_SEND_TOO_LARGE
;
679 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_START
);
681 msg_and_trailer_size
= send_size
+ MAX_TRAILER_SIZE
;
682 kmsg
= ipc_kmsg_alloc(msg_and_trailer_size
);
684 if (kmsg
== IKM_NULL
) {
685 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, MACH_SEND_NO_BUFFER
);
686 return MACH_SEND_NO_BUFFER
;
689 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_LINK
) | DBG_FUNC_NONE
,
690 (uintptr_t)0, /* this should only be called from the kernel! */
691 VM_KERNEL_ADDRPERM((uintptr_t)kmsg
),
694 (void) memcpy((void *) kmsg
->ikm_header
, (const void *) msg
, send_size
);
696 kmsg
->ikm_header
->msgh_size
= send_size
;
699 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
700 * However, the internal size field of the trailer (msgh_trailer_size)
701 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
702 * the cases where no implicit data is requested.
704 max_trailer
= (mach_msg_max_trailer_t
*) ((vm_offset_t
)kmsg
->ikm_header
+ send_size
);
705 bzero(max_trailer
, sizeof(*max_trailer
));
706 max_trailer
->msgh_sender
= current_thread()->task
->sec_token
;
707 max_trailer
->msgh_audit
= current_thread()->task
->audit_token
;
708 max_trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
709 max_trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
711 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, priority
, &option
);
713 if (mr
!= MACH_MSG_SUCCESS
) {
715 KDBG(MACHDBG_CODE(DBG_MACH_IPC
, MACH_IPC_KMSG_INFO
) | DBG_FUNC_END
, mr
);
720 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
, MACH_MSG_TIMEOUT_NONE
);
721 } while (mr
== MACH_SEND_INTERRUPTED
);
723 assert(mr
== MACH_MSG_SUCCESS
);
726 if (option
& MACH_RCV_MSG
) {
727 thread_t self
= current_thread();
728 mach_vm_address_t context
;
734 mr
= ipc_mqueue_copyin(space
, rcv_name
,
736 if (mr
!= MACH_MSG_SUCCESS
) {
740 /* hold ref for object */
742 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
743 ipc_mqueue_receive(mqueue
,
744 MACH_MSG_OPTION_NONE
,
746 MACH_MSG_TIMEOUT_NONE
,
748 mr
= self
->ith_state
;
749 kmsg
= self
->ith_kmsg
;
750 seqno
= self
->ith_seqno
;
752 mach_msg_receive_results_complete(object
);
754 } while (mr
== MACH_RCV_INTERRUPTED
);
756 if (mr
!= MACH_MSG_SUCCESS
) {
760 trailer_size
= ipc_kmsg_trailer_size(option
, self
);
762 if (rcv_size
< (kmsg
->ikm_header
->msgh_size
+ trailer_size
)) {
763 ipc_kmsg_copyout_dest(kmsg
, space
);
764 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
766 return MACH_RCV_TOO_LARGE
;
769 /* Save destination port context for the trailer before copyout */
770 context
= kmsg
->ikm_header
->msgh_remote_port
->ip_context
;
772 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_MSG_BODY_NULL
, option
);
774 if (mr
!= MACH_MSG_SUCCESS
) {
775 if ((mr
& ~MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
776 ipc_kmsg_add_trailer(kmsg
, space
, option
, self
, seqno
, TRUE
, context
);
777 ipc_kmsg_put_to_kernel(msg
, kmsg
,
778 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
780 ipc_kmsg_copyout_dest(kmsg
, space
);
781 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
, sizeof *msg
);
787 ipc_kmsg_add_trailer(kmsg
, space
, option
, self
, seqno
, TRUE
, context
);
788 (void) memcpy((void *) msg
, (const void *) kmsg
->ikm_header
,
789 kmsg
->ikm_header
->msgh_size
+ trailer_size
);
793 return MACH_MSG_SUCCESS
;
797 * Routine: mig_get_reply_port
799 * Called by client side interfaces living in the kernel
800 * to get a reply port.
803 mig_get_reply_port(void)
805 return MACH_PORT_NULL
;
809 * Routine: mig_dealloc_reply_port
811 * Called by client side interfaces to get rid of a reply port.
815 mig_dealloc_reply_port(
816 __unused mach_port_t reply_port
)
821 * Routine: mig_put_reply_port
823 * Called by client side interfaces after each RPC to
824 * let the client recycle the reply port if it wishes.
828 __unused mach_port_t reply_port
)
833 * mig_strncpy.c - by Joshua Block
835 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
836 * OUGHT to do: Copies the (null terminated) string in src into dest, a
837 * buffer of length len. Assures that the copy is still null terminated
838 * and doesn't overflow the buffer, truncating the copy if necessary.
842 * dest - Pointer to destination buffer.
844 * src - Pointer to source string.
846 * len - Length of destination buffer.
859 for (i
= 1; i
< len
; i
++) {
860 if (!(*dest
++ = *src
++)) {
872 * mig_strncpy_zerofill -- Bounded string copy. Does what the
873 * library routine strncpy OUGHT to do: Copies the (null terminated)
874 * string in src into dest, a buffer of length len. Assures that
875 * the copy is still null terminated and doesn't overflow the buffer,
876 * truncating the copy if necessary. If the string in src is smaller
877 * than given length len, it will zero fill the remaining bytes in dest.
881 * dest - Pointer to destination buffer.
883 * src - Pointer to source string.
885 * len - Length of destination buffer.
888 mig_strncpy_zerofill(
894 boolean_t terminated
= FALSE
;
897 if (len
<= 0 || dest
== NULL
) {
905 for (i
= 1; i
< len
; i
++) {
907 if (!(*dest
++ = *src
++)) {
928 return (char *)kalloc(size
);
940 * Routine: mig_object_init
942 * Initialize the base class portion of a MIG object. We
943 * will lazy init the port, so just clear it for now.
947 mig_object_t mig_object
,
948 const IMIGObject
*interface
)
950 if (mig_object
== MIG_OBJECT_NULL
) {
951 return KERN_INVALID_ARGUMENT
;
953 mig_object
->pVtbl
= (const IMIGObjectVtbl
*)interface
;
954 mig_object
->port
= MACH_PORT_NULL
;
959 * Routine: mig_object_destroy
961 * The object is being freed. This call lets us clean
962 * up any state we have have built up over the object's
965 * Since notifications and the port hold references on
966 * on the object, neither can exist when this is called.
967 * This is a good place to assert() that condition.
971 __assert_only mig_object_t mig_object
)
973 assert(mig_object
->port
== MACH_PORT_NULL
);
978 * Routine: mig_object_reference
980 * Pure virtual helper to invoke the MIG object's AddRef
983 * MIG object port may be locked.
986 mig_object_reference(
987 mig_object_t mig_object
)
989 assert(mig_object
!= MIG_OBJECT_NULL
);
990 mig_object
->pVtbl
->AddRef((IMIGObject
*)mig_object
);
994 * Routine: mig_object_deallocate
996 * Pure virtual helper to invoke the MIG object's Release
1002 mig_object_deallocate(
1003 mig_object_t mig_object
)
1005 assert(mig_object
!= MIG_OBJECT_NULL
);
1006 ipc_port_t port
= mig_object
->port
;
1007 if (mig_object
->pVtbl
->Release((IMIGObject
*)mig_object
) == 0) {
1008 if (IP_VALID(port
)) {
1009 assert(!port
->ip_srights
);
1010 ipc_port_dealloc_kernel(port
);
1016 * Routine: convert_mig_object_to_port [interface]
1018 * Base implementation of MIG outtrans routine to convert from
1019 * a mig object reference to a new send right on the object's
1020 * port. The object reference is consumed.
1022 * IP_NULL - Null MIG object supplied
1023 * Otherwise, a newly made send right for the port
1028 convert_mig_object_to_port(
1029 mig_object_t mig_object
)
1031 if (mig_object
== MIG_OBJECT_NULL
) {
1036 * make a send right and donate our reference for mig_object_no_senders
1037 * if this is the first send right
1039 if (!ipc_kobject_make_send_lazy_alloc_port(&mig_object
->port
,
1040 (ipc_kobject_t
) mig_object
, IKOT_MIG
, IPC_KOBJECT_ALLOC_NONE
, false, 0)) {
1041 mig_object_deallocate(mig_object
);
1044 return mig_object
->port
;
1049 * Routine: convert_port_to_mig_object [interface]
1051 * Base implementation of MIG intrans routine to convert from
1052 * an incoming port reference to a new reference on the
1053 * underlying object. A new reference must be created, because
1054 * the port's reference could go away asynchronously.
1056 * NULL - Not an active MIG object port or iid not supported
1057 * Otherwise, a reference to the underlying MIG interface
1062 convert_port_to_mig_object(
1066 mig_object_t mig_object
;
1069 if (!IP_VALID(port
)) {
1074 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_MIG
)) {
1080 * Our port points to some MIG object interface. Now
1081 * query it to get a reference to the desired interface.
1084 mig_object
= (mig_object_t
) ip_get_kobject(port
);
1085 mig_object
->pVtbl
->QueryInterface((IMIGObject
*)mig_object
, iid
, &ppv
);
1087 return (mig_object_t
)ppv
;
1091 * Routine: mig_object_no_senders [interface]
1093 * Base implementation of a no-senders notification handler
1094 * for MIG objects. If there truly are no more senders, must
1095 * destroy the port and drop its reference on the object.
1100 mig_object_no_senders(
1103 require_ip_active(port
);
1104 assert(IKOT_MIG
== ip_kotype(port
));
1106 /* consume the reference donated by convert_mig_object_to_port */
1107 mig_object_deallocate((mig_object_t
) ip_get_kobject(port
));
1111 * Kernel implementation of the notification chain for MIG object
1112 * is kept separate from the actual objects, since there are expected
1113 * to be much fewer of them than actual objects.
1115 * The implementation of this part of MIG objects is coming
1116 * "Real Soon Now"(TM).