2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: ipc/mach_msg.c
60 * Exported message traps. See mach/message.h.
65 #include <mach/kern_return.h>
66 #include <mach/port.h>
67 #include <mach/message.h>
68 #include <mach/mig_errors.h>
69 #include <kern/assert.h>
70 #include <kern/counters.h>
71 #include <kern/cpu_number.h>
72 #include <kern/task.h>
73 #include <kern/thread.h>
74 #include <kern/lock.h>
75 #include <kern/sched_prim.h>
76 #include <kern/exception.h>
77 #include <kern/misc_protos.h>
78 #include <vm/vm_map.h>
79 #include <ipc/ipc_kmsg.h>
80 #include <ipc/ipc_mqueue.h>
81 #include <ipc/ipc_object.h>
82 #include <ipc/ipc_notify.h>
83 #include <ipc/ipc_port.h>
84 #include <ipc/ipc_pset.h>
85 #include <ipc/ipc_space.h>
86 #include <ipc/ipc_entry.h>
87 #include <kern/kalloc.h>
88 #include <kern/thread_swap.h>
89 #include <kern/processor.h>
91 #include <kern/mk_sp.h>
93 #include <machine/machine_routines.h>
94 #include <sys/kdebug.h>
97 * Forward declarations
100 mach_msg_return_t
mach_msg_send(
101 mach_msg_header_t
*msg
,
102 mach_msg_option_t option
,
103 mach_msg_size_t send_size
,
104 mach_msg_timeout_t timeout
,
105 mach_port_name_t notify
);
107 mach_msg_return_t
mach_msg_receive(
108 mach_msg_header_t
*msg
,
109 mach_msg_option_t option
,
110 mach_msg_size_t rcv_size
,
111 mach_port_name_t rcv_name
,
112 mach_msg_timeout_t timeout
,
113 void (*continuation
)(mach_msg_return_t
),
114 mach_msg_size_t slist_size
);
117 mach_msg_return_t
msg_receive_error(
119 mach_msg_header_t
*msg
,
120 mach_msg_option_t option
,
121 mach_port_seqno_t seqno
,
124 security_token_t KERNEL_SECURITY_TOKEN
= KERNEL_SECURITY_TOKEN_VALUE
;
125 audit_token_t KERNEL_AUDIT_TOKEN
= KERNEL_AUDIT_TOKEN_VALUE
;
127 mach_msg_format_0_trailer_t trailer_template
= {
128 /* mach_msg_trailer_type_t */ MACH_MSG_TRAILER_FORMAT_0
,
129 /* mach_msg_trailer_size_t */ MACH_MSG_TRAILER_MINIMUM_SIZE
,
130 /* mach_port_seqno_t */ 0,
131 /* security_token_t */ KERNEL_SECURITY_TOKEN_VALUE
135 * Routine: mach_msg_send
141 * MACH_MSG_SUCCESS Sent the message.
142 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
143 * MACH_SEND_NO_BUFFER Couldn't allocate buffer.
144 * MACH_SEND_INVALID_DATA Couldn't copy message data.
145 * MACH_SEND_INVALID_HEADER
146 * Illegal value in the message header bits.
147 * MACH_SEND_INVALID_DEST The space is dead.
148 * MACH_SEND_INVALID_NOTIFY Bad notify port.
149 * MACH_SEND_INVALID_DEST Can't copyin destination port.
150 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
151 * MACH_SEND_TIMED_OUT Timeout expired without delivery.
152 * MACH_SEND_INTERRUPTED Delivery interrupted.
153 * MACH_SEND_NO_NOTIFY Can't allocate a msg-accepted request.
154 * MACH_SEND_WILL_NOTIFY Msg-accepted notif. requested.
155 * MACH_SEND_NOTIFY_IN_PROGRESS
156 * This space has already forced a message to this port.
161 mach_msg_header_t
*msg
,
162 mach_msg_option_t option
,
163 mach_msg_size_t send_size
,
164 mach_msg_timeout_t timeout
,
165 mach_port_name_t notify
)
167 ipc_space_t space
= current_space();
168 vm_map_t map
= current_map();
170 mach_msg_return_t mr
;
172 mr
= ipc_kmsg_get(msg
, send_size
, &kmsg
);
174 if (mr
!= MACH_MSG_SUCCESS
)
177 if (option
& MACH_SEND_CANCEL
) {
178 if (notify
== MACH_PORT_NULL
)
179 mr
= MACH_SEND_INVALID_NOTIFY
;
181 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, notify
);
183 mr
= ipc_kmsg_copyin(kmsg
, space
, map
, MACH_PORT_NULL
);
184 if (mr
!= MACH_MSG_SUCCESS
) {
189 mr
= ipc_kmsg_send(kmsg
, option
& MACH_SEND_TIMEOUT
, timeout
);
191 if (mr
!= MACH_MSG_SUCCESS
) {
192 mr
|= ipc_kmsg_copyout_pseudo(kmsg
, space
, map
, MACH_MSG_BODY_NULL
);
193 (void) ipc_kmsg_put(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
);
200 * Routine: mach_msg_receive
206 * MACH_MSG_SUCCESS Received a message.
207 * MACH_RCV_INVALID_NAME The name doesn't denote a right,
208 * or the denoted right is not receive or port set.
209 * MACH_RCV_IN_SET Receive right is a member of a set.
210 * MACH_RCV_TOO_LARGE Message wouldn't fit into buffer.
211 * MACH_RCV_TIMED_OUT Timeout expired without a message.
212 * MACH_RCV_INTERRUPTED Reception interrupted.
213 * MACH_RCV_PORT_DIED Port/set died while receiving.
214 * MACH_RCV_PORT_CHANGED Port moved into set while receiving.
215 * MACH_RCV_INVALID_DATA Couldn't copy to user buffer.
216 * MACH_RCV_INVALID_NOTIFY Bad notify port.
217 * MACH_RCV_HEADER_ERROR
221 mach_msg_receive_results(void)
223 thread_t self
= current_thread();
224 ipc_space_t space
= current_space();
225 vm_map_t map
= current_map();
227 ipc_object_t object
= self
->ith_object
;
228 mach_msg_return_t mr
= self
->ith_state
;
229 mach_msg_header_t
*msg
= self
->ith_msg
;
230 mach_msg_option_t option
= self
->ith_option
;
231 ipc_kmsg_t kmsg
= self
->ith_kmsg
;
232 mach_port_seqno_t seqno
= self
->ith_seqno
;
233 mach_msg_size_t slist_size
= self
->ith_scatter_list_size
;
235 mach_msg_format_0_trailer_t
*trailer
;
237 ipc_object_release(object
);
239 if (mr
!= MACH_MSG_SUCCESS
) {
241 if (mr
== MACH_RCV_TOO_LARGE
) {
242 if (option
& MACH_RCV_LARGE
) {
244 * We need to inform the user-level code that it needs more
245 * space. The value for how much space was returned in the
246 * msize save area instead of the message (which was left on
249 if (copyout((char *) &self
->ith_msize
,
250 (char *) &msg
->msgh_size
,
251 sizeof(mach_msg_size_t
)))
252 mr
= MACH_RCV_INVALID_DATA
;
256 if (msg_receive_error(kmsg
, msg
, option
, seqno
, space
)
257 == MACH_RCV_INVALID_DATA
)
258 mr
= MACH_RCV_INVALID_DATA
;
263 trailer
= (mach_msg_format_0_trailer_t
*)
264 ((vm_offset_t
)&kmsg
->ikm_header
+
265 round_msg(kmsg
->ikm_header
.msgh_size
));
266 if (option
& MACH_RCV_TRAILER_MASK
) {
267 trailer
->msgh_seqno
= seqno
;
268 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
272 * If MACH_RCV_OVERWRITE was specified, try to get the scatter
273 * list and verify it against the contents of the message. If
274 * there is any problem with it, we will continue without it as
277 if (option
& MACH_RCV_OVERWRITE
) {
278 mach_msg_size_t slist_size
= self
->ith_scatter_list_size
;
279 mach_msg_body_t
*slist
;
281 slist
= ipc_kmsg_copyin_scatter(msg
, slist_size
, kmsg
);
282 mr
= ipc_kmsg_copyout(kmsg
, space
, map
, MACH_PORT_NULL
, slist
);
283 ipc_kmsg_free_scatter(slist
, slist_size
);
285 mr
= ipc_kmsg_copyout(kmsg
, space
, map
,
286 MACH_PORT_NULL
, MACH_MSG_BODY_NULL
);
289 if (mr
!= MACH_MSG_SUCCESS
) {
290 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
291 if (ipc_kmsg_put(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
+
292 trailer
->msgh_trailer_size
) == MACH_RCV_INVALID_DATA
)
293 mr
= MACH_RCV_INVALID_DATA
;
296 if (msg_receive_error(kmsg
, msg
, option
, seqno
, space
)
297 == MACH_RCV_INVALID_DATA
)
298 mr
= MACH_RCV_INVALID_DATA
;
302 mr
= ipc_kmsg_put(msg
,
304 kmsg
->ikm_header
.msgh_size
+
305 trailer
->msgh_trailer_size
);
312 mach_msg_header_t
*msg
,
313 mach_msg_option_t option
,
314 mach_msg_size_t rcv_size
,
315 mach_port_name_t rcv_name
,
316 mach_msg_timeout_t timeout
,
317 void (*continuation
)(mach_msg_return_t
),
318 mach_msg_size_t slist_size
)
320 thread_t self
= current_thread();
321 ipc_space_t space
= current_space();
322 vm_map_t map
= current_map();
326 mach_port_seqno_t seqno
;
327 mach_msg_return_t mr
;
328 mach_msg_body_t
*slist
;
329 mach_msg_format_0_trailer_t
*trailer
;
331 mr
= ipc_mqueue_copyin(space
, rcv_name
, &mqueue
, &object
);
332 if (mr
!= MACH_MSG_SUCCESS
) {
335 /* hold ref for object */
338 self
->ith_object
= object
;
339 self
->ith_msize
= rcv_size
;
340 self
->ith_option
= option
;
341 self
->ith_scatter_list_size
= slist_size
;
342 self
->ith_continuation
= continuation
;
344 ipc_mqueue_receive(mqueue
, option
, rcv_size
, timeout
, THREAD_ABORTSAFE
);
345 if ((option
& MACH_RCV_TIMEOUT
) && timeout
== 0)
346 _mk_sp_thread_perhaps_yield(self
);
347 return mach_msg_receive_results();
351 mach_msg_receive_continue(void)
353 thread_t self
= current_thread();
355 (*self
->ith_continuation
)(mach_msg_receive_results());
359 * Toggle this to compile the hotpath in/out
360 * If compiled in, the run-time toggle "enable_hotpath" below
361 * eases testing & debugging
363 #define ENABLE_HOTPATH 1 /* Hacked on for now */
367 * These counters allow tracing of hotpath behavior under test loads.
368 * A couple key counters are unconditional (see below).
370 #define HOTPATH_DEBUG 0 /* Toggle to include lots of counters */
372 #define HOT(expr) expr
374 unsigned int c_mmot_FIRST
= 0; /* Unused First Counter */
375 unsigned int c_mmot_combined_S_R
= 0; /* hotpath candidates */
376 unsigned int c_mach_msg_trap_switch_fast
= 0; /* hotpath successes */
377 unsigned int c_mmot_kernel_send
= 0; /* kernel server */
378 unsigned int c_mmot_cold_000
= 0; /* see below ... */
379 unsigned int c_mmot_smallsendsize
= 0;
380 unsigned int c_mmot_oddsendsize
= 0;
381 unsigned int c_mmot_bigsendsize
= 0;
382 unsigned int c_mmot_copyinmsg_fail
= 0;
383 unsigned int c_mmot_g_slow_copyin3
= 0;
384 unsigned int c_mmot_cold_006
= 0;
385 unsigned int c_mmot_cold_007
= 0;
386 unsigned int c_mmot_cold_008
= 0;
387 unsigned int c_mmot_cold_009
= 0;
388 unsigned int c_mmot_cold_010
= 0;
389 unsigned int c_mmot_cold_012
= 0;
390 unsigned int c_mmot_cold_013
= 0;
391 unsigned int c_mmot_cold_014
= 0;
392 unsigned int c_mmot_cold_016
= 0;
393 unsigned int c_mmot_cold_018
= 0;
394 unsigned int c_mmot_cold_019
= 0;
395 unsigned int c_mmot_cold_020
= 0;
396 unsigned int c_mmot_cold_021
= 0;
397 unsigned int c_mmot_cold_022
= 0;
398 unsigned int c_mmot_cold_023
= 0;
399 unsigned int c_mmot_cold_024
= 0;
400 unsigned int c_mmot_cold_025
= 0;
401 unsigned int c_mmot_cold_026
= 0;
402 unsigned int c_mmot_cold_027
= 0;
403 unsigned int c_mmot_hot_fSR_ok
= 0;
404 unsigned int c_mmot_cold_029
= 0;
405 unsigned int c_mmot_cold_030
= 0;
406 unsigned int c_mmot_cold_031
= 0;
407 unsigned int c_mmot_cold_032
= 0;
408 unsigned int c_mmot_cold_033
= 0;
409 unsigned int c_mmot_bad_rcvr
= 0;
410 unsigned int c_mmot_rcvr_swapped
= 0;
411 unsigned int c_mmot_rcvr_locked
= 0;
412 unsigned int c_mmot_rcvr_tswapped
= 0;
413 unsigned int c_mmot_rcvr_freed
= 0;
414 unsigned int c_mmot_g_slow_copyout6
= 0;
415 unsigned int c_mmot_g_slow_copyout5
= 0;
416 unsigned int c_mmot_cold_037
= 0;
417 unsigned int c_mmot_cold_038
= 0;
418 unsigned int c_mmot_cold_039
= 0;
419 unsigned int c_mmot_g_slow_copyout4
= 0;
420 unsigned int c_mmot_g_slow_copyout3
= 0;
421 unsigned int c_mmot_hot_ok1
= 0;
422 unsigned int c_mmot_hot_ok2
= 0;
423 unsigned int c_mmot_hot_ok3
= 0;
424 unsigned int c_mmot_g_slow_copyout1
= 0;
425 unsigned int c_mmot_g_slow_copyout2
= 0;
426 unsigned int c_mmot_getback_fast_copyin
= 0;
427 unsigned int c_mmot_cold_048
= 0;
428 unsigned int c_mmot_getback_FastSR
= 0;
429 unsigned int c_mmot_cold_050
= 0;
430 unsigned int c_mmot_cold_051
= 0;
431 unsigned int c_mmot_cold_052
= 0;
432 unsigned int c_mmot_cold_053
= 0;
433 unsigned int c_mmot_fastkernelreply
= 0;
434 unsigned int c_mmot_cold_055
= 0;
435 unsigned int c_mmot_getback_fast_put
= 0;
436 unsigned int c_mmot_LAST
= 0; /* End Marker - Unused */
438 void db_mmot_zero_counters(void); /* forward; */
439 void db_mmot_show_counters(void); /* forward; */
441 void /* Call from the debugger to clear all counters */
442 db_mmot_zero_counters(void)
444 register unsigned int *ip
= &c_mmot_FIRST
;
445 while (ip
<= &c_mmot_LAST
)
449 void /* Call from the debugger to show all counters */
450 db_mmot_show_counters(void)
452 #define xx(str) printf("%s: %d\n", # str, str);
454 xx(c_mmot_combined_S_R
);
455 xx(c_mach_msg_trap_switch_fast
);
456 xx(c_mmot_kernel_send
);
458 xx(c_mmot_smallsendsize
);
459 xx(c_mmot_oddsendsize
);
460 xx(c_mmot_bigsendsize
);
461 xx(c_mmot_copyinmsg_fail
);
462 xx(c_mmot_g_slow_copyin3
);
482 xx(c_mmot_hot_fSR_ok
);
489 xx(c_mmot_rcvr_swapped
);
490 xx(c_mmot_rcvr_locked
);
491 xx(c_mmot_rcvr_tswapped
);
492 xx(c_mmot_rcvr_freed
);
493 xx(c_mmot_g_slow_copyout6
);
494 xx(c_mmot_g_slow_copyout5
);
498 xx(c_mmot_g_slow_copyout4
);
499 xx(c_mmot_g_slow_copyout3
);
500 xx(c_mmot_g_slow_copyout1
);
504 xx(c_mmot_g_slow_copyout2
);
505 xx(c_mmot_getback_fast_copyin
);
507 xx(c_mmot_getback_FastSR
);
512 xx(c_mmot_fastkernelreply
);
514 xx(c_mmot_getback_fast_put
);
519 #else /* !HOTPATH_DEBUG */
522 * Duplicate just these few so we can always do a quick sanity check
524 unsigned int c_mmot_combined_S_R
= 0; /* hotpath candidates */
525 unsigned int c_mach_msg_trap_switch_fast
= 0; /* hotpath successes */
526 unsigned int c_mmot_kernel_send
= 0; /* kernel server calls */
527 #define HOT(expr) /* no optional counters */
529 #endif /* !HOTPATH_DEBUG */
531 boolean_t enable_hotpath
= TRUE
; /* Patchable, just in case ... */
532 #endif /* HOTPATH_ENABLE */
535 * Routine: mach_msg_overwrite_trap [mach trap]
537 * Possibly send a message; possibly receive a message.
541 * All of mach_msg_send and mach_msg_receive error codes.
545 mach_msg_overwrite_trap(
546 mach_msg_header_t
*msg
,
547 mach_msg_option_t option
,
548 mach_msg_size_t send_size
,
549 mach_msg_size_t rcv_size
,
550 mach_port_name_t rcv_name
,
551 mach_msg_timeout_t timeout
,
552 mach_port_name_t notify
,
553 mach_msg_header_t
*rcv_msg
,
554 mach_msg_size_t scatter_list_size
)
556 register mach_msg_header_t
*hdr
;
557 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
558 /* mask out some of the options before entering the hot path */
559 mach_msg_option_t masked_option
=
560 option
& ~(MACH_SEND_TRAILER
|MACH_RCV_TRAILER_MASK
|MACH_RCV_LARGE
);
564 /* BEGINNING OF HOT PATH */
565 if ((masked_option
== (MACH_SEND_MSG
|MACH_RCV_MSG
)) && enable_hotpath
) {
566 register thread_t self
= current_thread();
567 register mach_msg_format_0_trailer_t
*trailer
;
569 ipc_space_t space
= current_act()->task
->itk_space
;
571 register ipc_port_t dest_port
;
572 ipc_object_t rcv_object
;
573 register ipc_mqueue_t rcv_mqueue
;
574 mach_msg_size_t reply_size
;
577 c_mmot_combined_S_R
++;
580 * This case is divided into ten sections, each
581 * with a label. There are five optimized
582 * sections and six unoptimized sections, which
583 * do the same thing but handle all possible
584 * cases and are slower.
586 * The five sections for an RPC are
587 * 1) Get request message into a buffer.
588 * 2) Copyin request message and rcv_name.
589 * (fast_copyin or slow_copyin)
590 * 3) Enqueue request and dequeue reply.
591 * (fast_send_receive or
592 * slow_send and slow_receive)
593 * 4) Copyout reply message.
594 * (fast_copyout or slow_copyout)
595 * 5) Put reply message to user's buffer.
597 * Keep the locking hierarchy firmly in mind.
598 * (First spaces, then ports, then port sets,
599 * then message queues.) Only a non-blocking
600 * attempt can be made to acquire locks out of
601 * order, or acquire two locks on the same level.
602 * Acquiring two locks on the same level will
603 * fail if the objects are really the same,
604 * unless simple locking is disabled. This is OK,
605 * because then the extra unlock does nothing.
607 * There are two major reasons these RPCs can't use
608 * ipc_thread_switch, and use slow_send/slow_receive:
610 * 2) Servers fall behind clients, so
611 * client doesn't find a blocked server thread and
612 * server finds waiting messages and can't block.
615 mr
= ipc_kmsg_get(msg
, send_size
, &kmsg
);
616 if (mr
!= KERN_SUCCESS
) {
619 hdr
= &kmsg
->ikm_header
;
620 trailer
= (mach_msg_format_0_trailer_t
*) ((vm_offset_t
) hdr
+
625 * optimized ipc_kmsg_copyin/ipc_mqueue_copyin
627 * We have the request message data in kmsg.
628 * Must still do copyin, send, receive, etc.
630 * If the message isn't simple, we can't combine
631 * ipc_kmsg_copyin_header and ipc_mqueue_copyin,
632 * because copyin of the message body might
636 switch (hdr
->msgh_bits
) {
637 case MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
638 MACH_MSG_TYPE_MAKE_SEND_ONCE
): {
639 register ipc_entry_t table
;
640 register ipc_entry_num_t size
;
641 register ipc_port_t reply_port
;
643 /* sending a request message */
646 register mach_port_index_t index
;
647 register mach_port_gen_t gen
;
650 register mach_port_name_t reply_name
=
651 (mach_port_name_t
)hdr
->msgh_local_port
;
653 if (reply_name
!= rcv_name
) {
654 HOT(c_mmot_g_slow_copyin3
++);
658 /* optimized ipc_entry_lookup of reply_name */
660 index
= MACH_PORT_INDEX(reply_name
);
661 gen
= MACH_PORT_GEN(reply_name
);
664 assert(space
->is_active
);
666 size
= space
->is_table_size
;
667 table
= space
->is_table
;
670 register ipc_entry_t entry
;
671 register ipc_entry_bits_t bits
;
674 entry
= &table
[index
];
675 bits
= entry
->ie_bits
;
676 if (IE_BITS_GEN(bits
) != gen
||
677 (bits
& IE_BITS_COLLISION
)) {
683 if (entry
== IE_NULL
) {
684 entry
= ipc_entry_lookup(space
, reply_name
);
685 if (entry
== IE_NULL
) {
686 HOT(c_mmot_cold_006
++);
687 goto abort_request_copyin
;
689 bits
= entry
->ie_bits
;
694 if (! (bits
& MACH_PORT_TYPE_RECEIVE
)) {
695 HOT(c_mmot_cold_007
++);
696 goto abort_request_copyin
;
699 reply_port
= (ipc_port_t
) entry
->ie_object
;
700 assert(reply_port
!= IP_NULL
);
705 /* optimized ipc_entry_lookup of dest_name */
708 register mach_port_index_t index
;
709 register mach_port_gen_t gen
;
712 register mach_port_name_t dest_name
=
713 (mach_port_name_t
)hdr
->msgh_remote_port
;
715 index
= MACH_PORT_INDEX(dest_name
);
716 gen
= MACH_PORT_GEN(dest_name
);
719 register ipc_entry_t entry
;
720 register ipc_entry_bits_t bits
;
723 entry
= &table
[index
];
724 bits
= entry
->ie_bits
;
725 if (IE_BITS_GEN(bits
) != gen
||
726 (bits
& IE_BITS_COLLISION
)) {
732 if (entry
== IE_NULL
) {
733 entry
= ipc_entry_lookup(space
, dest_name
);
734 if (entry
== IE_NULL
) {
735 HOT(c_mmot_cold_008
++);
736 goto abort_request_copyin
;
738 bits
= entry
->ie_bits
;
743 if (! (bits
& MACH_PORT_TYPE_SEND
)) {
744 HOT(c_mmot_cold_009
++);
745 goto abort_request_copyin
;
748 assert(IE_BITS_UREFS(bits
) > 0);
750 dest_port
= (ipc_port_t
) entry
->ie_object
;
751 assert(dest_port
!= IP_NULL
);
757 * To do an atomic copyin, need simultaneous
758 * locks on both ports and the space. If
759 * dest_port == reply_port, and simple locking is
760 * enabled, then we will abort. Otherwise it's
761 * OK to unlock twice.
765 if (!ip_active(dest_port
) ||
766 !ip_lock_try(reply_port
)) {
767 ip_unlock(dest_port
);
768 HOT(c_mmot_cold_010
++);
769 goto abort_request_copyin
;
771 is_read_unlock(space
);
773 assert(dest_port
->ip_srights
> 0);
774 dest_port
->ip_srights
++;
775 ip_reference(dest_port
);
777 assert(ip_active(reply_port
));
778 assert(reply_port
->ip_receiver_name
==
779 (mach_port_name_t
)hdr
->msgh_local_port
);
780 assert(reply_port
->ip_receiver
== space
);
782 reply_port
->ip_sorights
++;
783 ip_reference(reply_port
);
786 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
,
787 MACH_MSG_TYPE_PORT_SEND_ONCE
);
788 hdr
->msgh_remote_port
= dest_port
;
789 hdr
->msgh_local_port
= reply_port
;
791 /* make sure we can queue to the destination */
793 if (dest_port
->ip_receiver
== ipc_space_kernel
) {
795 * The kernel server has a reference to
796 * the reply port, which it hands back
797 * to us in the reply message. We do
798 * not need to keep another reference to
801 ip_unlock(reply_port
);
803 assert(ip_active(dest_port
));
804 dest_port
->ip_messages
.imq_seqno
++;
805 ip_unlock(dest_port
);
809 if (imq_full(&dest_port
->ip_messages
)) {
810 HOT(c_mmot_cold_013
++);
811 goto abort_request_send_receive
;
814 /* optimized ipc_mqueue_copyin */
816 rcv_object
= (ipc_object_t
) reply_port
;
817 io_reference(rcv_object
);
818 rcv_mqueue
= &reply_port
->ip_messages
;
819 io_unlock(rcv_object
);
820 HOT(c_mmot_hot_fSR_ok
++);
821 goto fast_send_receive
;
823 abort_request_copyin
:
824 is_read_unlock(space
);
827 abort_request_send_receive
:
828 ip_unlock(dest_port
);
829 ip_unlock(reply_port
);
833 case MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0): {
834 register ipc_entry_num_t size
;
835 register ipc_entry_t table
;
837 /* sending a reply message */
840 register mach_port_name_t reply_name
=
841 (mach_port_name_t
)hdr
->msgh_local_port
;
843 if (reply_name
!= MACH_PORT_NULL
) {
844 HOT(c_mmot_cold_018
++);
849 is_write_lock(space
);
850 assert(space
->is_active
);
852 /* optimized ipc_entry_lookup */
854 size
= space
->is_table_size
;
855 table
= space
->is_table
;
858 register ipc_entry_t entry
;
859 register mach_port_gen_t gen
;
860 register mach_port_index_t index
;
861 ipc_table_index_t
*requests
;
864 register mach_port_name_t dest_name
=
865 (mach_port_name_t
)hdr
->msgh_remote_port
;
867 index
= MACH_PORT_INDEX(dest_name
);
868 gen
= MACH_PORT_GEN(dest_name
);
872 HOT(c_mmot_cold_019
++);
873 goto abort_reply_dest_copyin
;
876 entry
= &table
[index
];
878 /* check generation, collision bit, and type bit */
880 if ((entry
->ie_bits
& (IE_BITS_GEN_MASK
|
882 MACH_PORT_TYPE_SEND_ONCE
)) !=
883 (gen
| MACH_PORT_TYPE_SEND_ONCE
)) {
884 HOT(c_mmot_cold_020
++);
885 goto abort_reply_dest_copyin
;
888 /* optimized ipc_right_copyin */
890 assert(IE_BITS_TYPE(entry
->ie_bits
) ==
891 MACH_PORT_TYPE_SEND_ONCE
);
892 assert(IE_BITS_UREFS(entry
->ie_bits
) == 1);
894 if (entry
->ie_request
!= 0) {
895 HOT(c_mmot_cold_021
++);
896 goto abort_reply_dest_copyin
;
899 dest_port
= (ipc_port_t
) entry
->ie_object
;
900 assert(dest_port
!= IP_NULL
);
903 if (!ip_active(dest_port
)) {
904 ip_unlock(dest_port
);
905 HOT(c_mmot_cold_022
++);
906 goto abort_reply_dest_copyin
;
909 assert(dest_port
->ip_sorights
> 0);
911 /* optimized ipc_entry_dealloc */
914 entry
->ie_bits
= gen
;
915 entry
->ie_next
= table
->ie_next
;
916 table
->ie_next
= index
;
917 entry
->ie_object
= IO_NULL
;
921 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE
,
923 hdr
->msgh_remote_port
= dest_port
;
925 /* make sure we can queue to the destination */
927 assert(dest_port
->ip_receiver
!= ipc_space_kernel
);
929 /* optimized ipc_entry_lookup/ipc_mqueue_copyin */
932 register ipc_entry_t entry
;
933 register ipc_entry_bits_t bits
;
936 register mach_port_index_t index
;
937 register mach_port_gen_t gen
;
939 index
= MACH_PORT_INDEX(rcv_name
);
940 gen
= MACH_PORT_GEN(rcv_name
);
943 entry
= &table
[index
];
944 bits
= entry
->ie_bits
;
945 if (IE_BITS_GEN(bits
) != gen
||
946 (bits
& IE_BITS_COLLISION
)) {
952 if (entry
== IE_NULL
) {
953 entry
= ipc_entry_lookup(space
, rcv_name
);
954 if (entry
== IE_NULL
) {
955 HOT(c_mmot_cold_024
++);
956 goto abort_reply_rcv_copyin
;
958 bits
= entry
->ie_bits
;
963 /* check type bits; looking for receive or set */
966 * JMM - The check below for messages in the receive
967 * mqueue is insufficient to work with port sets, since
968 * the messages stay in the port queues. For now, don't
969 * allow portsets (but receiving on portsets when sending
970 * a message to a send-once right is actually a very
971 * common case (so we should re-enable).
973 if (bits
& MACH_PORT_TYPE_PORT_SET
) {
974 register ipc_pset_t rcv_pset
;
976 rcv_pset
= (ipc_pset_t
) entry
->ie_object
;
977 assert(rcv_pset
!= IPS_NULL
);
980 assert(ips_active(rcv_pset
));
982 rcv_object
= (ipc_object_t
) rcv_pset
;
983 rcv_mqueue
= &rcv_pset
->ips_messages
;
986 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
987 register ipc_port_t rcv_port
;
989 rcv_port
= (ipc_port_t
) entry
->ie_object
;
990 assert(rcv_port
!= IP_NULL
);
992 if (!ip_lock_try(rcv_port
)) {
993 HOT(c_mmot_cold_025
++);
994 goto abort_reply_rcv_copyin
;
996 assert(ip_active(rcv_port
));
998 if (rcv_port
->ip_pset_count
!= 0) {
1000 HOT(c_mmot_cold_026
++);
1001 goto abort_reply_rcv_copyin
;
1004 rcv_object
= (ipc_object_t
) rcv_port
;
1005 rcv_mqueue
= &rcv_port
->ip_messages
;
1007 HOT(c_mmot_cold_027
++);
1008 goto abort_reply_rcv_copyin
;
1012 is_write_unlock(space
);
1013 io_reference(rcv_object
);
1014 io_unlock(rcv_object
);
1015 HOT(c_mmot_hot_fSR_ok
++);
1016 goto fast_send_receive
;
1018 abort_reply_dest_copyin
:
1019 is_write_unlock(space
);
1020 HOT(c_mmot_cold_029
++);
1023 abort_reply_rcv_copyin
:
1024 ip_unlock(dest_port
);
1025 is_write_unlock(space
);
1026 HOT(c_mmot_cold_030
++);
1031 HOT(c_mmot_cold_031
++);
1038 * optimized ipc_mqueue_send/ipc_mqueue_receive
1040 * Finished get/copyin of kmsg and copyin of rcv_name.
1041 * space is unlocked, dest_port is locked,
1042 * we can queue kmsg to dest_port,
1043 * rcv_mqueue is set, and rcv_object holds a ref
1044 * so the mqueue cannot go away.
1046 * JMM - For now, rcv_object is just a port. Portsets
1047 * are disabled for the time being.
1050 assert(ip_active(dest_port
));
1051 assert(dest_port
->ip_receiver
!= ipc_space_kernel
);
1052 // assert(!imq_full(&dest_port->ip_messages) ||
1053 // (MACH_MSGH_BITS_REMOTE(hdr->msgh_bits) ==
1054 // MACH_MSG_TYPE_PORT_SEND_ONCE));
1055 assert((hdr
->msgh_bits
& MACH_MSGH_BITS_CIRCULAR
) == 0);
1058 register ipc_mqueue_t dest_mqueue
;
1061 processor_t processor
;
1065 processor
= current_processor();
1066 if (processor
->current_pri
>= BASEPRI_RTQUEUES
)
1067 goto abort_send_receive1
;
1069 dest_mqueue
= &dest_port
->ip_messages
;
1070 waitq
= &dest_mqueue
->imq_wait_queue
;
1071 imq_lock(dest_mqueue
);
1073 wait_queue_peek64_locked(waitq
, IPC_MQUEUE_RECEIVE
, &receiver
, &waitq
);
1074 /* queue still locked, thread locked - but still on q */
1076 if ( receiver
== THREAD_NULL
) {
1078 imq_unlock(dest_mqueue
);
1079 abort_send_receive1
:
1081 ip_unlock(dest_port
);
1082 ipc_object_release(rcv_object
);
1083 HOT(c_mmot_cold_032
++);
1087 assert(receiver
->wait_queue
== waitq
);
1088 assert(receiver
->wait_event
== IPC_MQUEUE_RECEIVE
);
1091 * Make sure that the scheduling state of the receiver is such
1092 * that we can handoff to it here. If not, fall off.
1094 * JMM - We have an opportunity here. If the thread is locked
1095 * and we find it runnable, it may still be trying to get into
1096 * thread_block on itself. We could just "hand him the message"
1097 * and let him go (thread_go_locked()) and then fall down into a
1098 * slow receive for ourselves. Only his RECEIVE_TOO_LARGE handling
1099 * runs afoul of that. Clean this up!
1101 if ((receiver
->state
& (TH_RUN
|TH_WAIT
)) != TH_WAIT
||
1102 receiver
->sched_pri
>= BASEPRI_RTQUEUES
||
1103 receiver
->processor_set
!= processor
->processor_set
||
1104 (receiver
->bound_processor
!= PROCESSOR_NULL
&&
1105 receiver
->bound_processor
!= processor
)) {
1106 HOT(c_mmot_cold_033
++);
1108 thread_unlock(receiver
);
1109 if (waitq
!= &dest_mqueue
->imq_wait_queue
)
1110 wait_queue_unlock(waitq
);
1111 goto abort_send_receive
;
1115 * Check that the receiver can stay on the hot path.
1117 if (send_size
+ REQUESTED_TRAILER_SIZE(receiver
->ith_option
) >
1118 receiver
->ith_msize
) {
1120 * The receiver can't accept the message.
1122 HOT(c_mmot_bad_rcvr
++);
1127 * Before committing to the handoff, make sure that we are
1128 * really going to block (i.e. there are no messages already
1129 * queued for us. This violates lock ordering, so make sure
1130 * we don't deadlock. After the trylock succeeds below, we
1131 * may have up to 3 message queues locked:
1132 * - the dest port mqueue
1133 * - a portset mqueue (where waiting receiver was found)
1134 * - finally our own rcv_mqueue
1136 * JMM - Need to make this check appropriate for portsets as
1137 * well before re-enabling them.
1139 if (!imq_lock_try(rcv_mqueue
)) {
1142 if (ipc_kmsg_queue_first(&rcv_mqueue
->imq_messages
) != IKM_NULL
) {
1143 imq_unlock(rcv_mqueue
);
1144 HOT(c_mmot_cold_033
++);
1148 /* At this point we are committed to do the "handoff". */
1149 c_mach_msg_trap_switch_fast
++;
1152 * JMM - Go ahead and pull the receiver from the runq. If the
1153 * runq wasn't the one for the mqueue, unlock it.
1155 wait_queue_pull_thread_locked(waitq
,
1157 (waitq
!= &dest_mqueue
->imq_wait_queue
));
1160 * Store the kmsg and seqno where the receiver can pick it up.
1162 receiver
->ith_state
= MACH_MSG_SUCCESS
;
1163 receiver
->ith_kmsg
= kmsg
;
1164 receiver
->ith_seqno
= dest_mqueue
->imq_seqno
++;
1167 * Update the scheduling state for the handoff.
1169 receiver
->state
&= ~(TH_WAIT
|TH_UNINT
);
1170 receiver
->state
|= TH_RUN
;
1172 pset_run_incr(receiver
->processor_set
);
1173 if (receiver
->sched_mode
& TH_MODE_TIMESHARE
)
1174 pset_share_incr(receiver
->processor_set
);
1176 receiver
->wait_result
= THREAD_AWAKENED
;
1178 receiver
->computation_metered
= 0;
1179 receiver
->reason
= AST_NONE
;
1181 thread_unlock(receiver
);
1183 imq_unlock(dest_mqueue
);
1184 ip_unlock(dest_port
);
1185 current_task()->messages_sent
++;
1189 * Put self on receive port's queue.
1190 * Also save state that the sender of
1191 * our reply message needs to determine if it
1192 * can hand off directly back to us.
1195 self
->ith_msg
= (rcv_msg
) ? rcv_msg
: msg
;
1196 self
->ith_object
= rcv_object
; /* still holds reference */
1197 self
->ith_msize
= rcv_size
;
1198 self
->ith_option
= option
;
1199 self
->ith_scatter_list_size
= scatter_list_size
;
1200 self
->ith_continuation
= thread_syscall_return
;
1202 waitq
= &rcv_mqueue
->imq_wait_queue
;
1203 (void)wait_queue_assert_wait64_locked(waitq
,
1207 thread_unlock(self
);
1208 imq_unlock(rcv_mqueue
);
1211 * Switch directly to receiving thread, and block
1212 * this thread as though it had called ipc_mqueue_receive.
1214 thread_run(self
, ipc_mqueue_receive_continue
, receiver
);
1220 * Nothing locked and no references held, except
1221 * we have kmsg with msgh_seqno filled in. Must
1222 * still check against rcv_size and do
1223 * ipc_kmsg_copyout/ipc_kmsg_put.
1226 reply_size
= send_size
+ trailer
->msgh_trailer_size
;
1227 if (rcv_size
< reply_size
) {
1228 HOT(c_mmot_g_slow_copyout6
++);
1232 /* optimized ipc_kmsg_copyout/ipc_kmsg_copyout_header */
1234 switch (hdr
->msgh_bits
) {
1235 case MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND
,
1236 MACH_MSG_TYPE_PORT_SEND_ONCE
): {
1237 ipc_port_t reply_port
=
1238 (ipc_port_t
) hdr
->msgh_local_port
;
1239 mach_port_name_t dest_name
, reply_name
;
1241 /* receiving a request message */
1243 if (!IP_VALID(reply_port
)) {
1244 HOT(c_mmot_g_slow_copyout5
++);
1248 is_write_lock(space
);
1249 assert(space
->is_active
);
1252 * To do an atomic copyout, need simultaneous
1253 * locks on both ports and the space. If
1254 * dest_port == reply_port, and simple locking is
1255 * enabled, then we will abort. Otherwise it's
1256 * OK to unlock twice.
1260 if (!ip_active(dest_port
) ||
1261 !ip_lock_try(reply_port
)) {
1262 HOT(c_mmot_cold_037
++);
1263 goto abort_request_copyout
;
1266 if (!ip_active(reply_port
)) {
1267 ip_unlock(reply_port
);
1268 HOT(c_mmot_cold_038
++);
1269 goto abort_request_copyout
;
1272 assert(reply_port
->ip_sorights
> 0);
1273 ip_unlock(reply_port
);
1276 register ipc_entry_t table
;
1277 register ipc_entry_t entry
;
1278 register mach_port_index_t index
;
1280 /* optimized ipc_entry_get */
1282 table
= space
->is_table
;
1283 index
= table
->ie_next
;
1286 HOT(c_mmot_cold_039
++);
1287 goto abort_request_copyout
;
1290 entry
= &table
[index
];
1291 table
->ie_next
= entry
->ie_next
;
1292 entry
->ie_request
= 0;
1295 register mach_port_gen_t gen
;
1297 assert((entry
->ie_bits
&~ IE_BITS_GEN_MASK
) == 0);
1298 gen
= IE_BITS_NEW_GEN(entry
->ie_bits
);
1300 reply_name
= MACH_PORT_MAKE(index
, gen
);
1302 /* optimized ipc_right_copyout */
1304 entry
->ie_bits
= gen
| (MACH_PORT_TYPE_SEND_ONCE
| 1);
1307 assert(MACH_PORT_VALID(reply_name
));
1308 entry
->ie_object
= (ipc_object_t
) reply_port
;
1309 is_write_unlock(space
);
1312 /* optimized ipc_object_copyout_dest */
1314 assert(dest_port
->ip_srights
> 0);
1315 ip_release(dest_port
);
1317 if (dest_port
->ip_receiver
== space
)
1318 dest_name
= dest_port
->ip_receiver_name
;
1320 dest_name
= MACH_PORT_NULL
;
1322 if ((--dest_port
->ip_srights
== 0) &&
1323 (dest_port
->ip_nsrequest
!= IP_NULL
)) {
1324 ipc_port_t nsrequest
;
1325 mach_port_mscount_t mscount
;
1327 /* a rather rare case */
1329 nsrequest
= dest_port
->ip_nsrequest
;
1330 mscount
= dest_port
->ip_mscount
;
1331 dest_port
->ip_nsrequest
= IP_NULL
;
1332 ip_unlock(dest_port
);
1333 ipc_notify_no_senders(nsrequest
, mscount
);
1335 ip_unlock(dest_port
);
1338 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE
,
1339 MACH_MSG_TYPE_PORT_SEND
);
1340 hdr
->msgh_remote_port
= (mach_port_t
)reply_name
;
1341 hdr
->msgh_local_port
= (mach_port_t
)dest_name
;
1342 HOT(c_mmot_hot_ok1
++);
1345 abort_request_copyout
:
1346 ip_unlock(dest_port
);
1347 is_write_unlock(space
);
1348 HOT(c_mmot_g_slow_copyout4
++);
1352 case MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE
, 0): {
1353 register mach_port_name_t dest_name
;
1355 /* receiving a reply message */
1358 if (!ip_active(dest_port
)) {
1359 ip_unlock(dest_port
);
1360 HOT(c_mmot_g_slow_copyout3
++);
1364 /* optimized ipc_object_copyout_dest */
1366 assert(dest_port
->ip_sorights
> 0);
1368 if (dest_port
->ip_receiver
== space
) {
1369 ip_release(dest_port
);
1370 dest_port
->ip_sorights
--;
1371 dest_name
= dest_port
->ip_receiver_name
;
1372 ip_unlock(dest_port
);
1374 ip_unlock(dest_port
);
1376 ipc_notify_send_once(dest_port
);
1377 dest_name
= MACH_PORT_NULL
;
1380 hdr
->msgh_bits
= MACH_MSGH_BITS(0,
1381 MACH_MSG_TYPE_PORT_SEND_ONCE
);
1382 hdr
->msgh_remote_port
= MACH_PORT_NULL
;
1383 hdr
->msgh_local_port
= (ipc_port_t
)dest_name
;
1384 HOT(c_mmot_hot_ok2
++);
1388 case MACH_MSGH_BITS_COMPLEX
|
1389 MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND_ONCE
, 0): {
1390 register mach_port_name_t dest_name
;
1392 /* receiving a complex reply message */
1395 if (!ip_active(dest_port
)) {
1396 ip_unlock(dest_port
);
1397 HOT(c_mmot_g_slow_copyout1
++);
1401 /* optimized ipc_object_copyout_dest */
1403 assert(dest_port
->ip_sorights
> 0);
1405 if (dest_port
->ip_receiver
== space
) {
1406 ip_release(dest_port
);
1407 dest_port
->ip_sorights
--;
1408 dest_name
= dest_port
->ip_receiver_name
;
1409 ip_unlock(dest_port
);
1411 ip_unlock(dest_port
);
1413 ipc_notify_send_once(dest_port
);
1414 dest_name
= MACH_PORT_NULL
;
1418 MACH_MSGH_BITS_COMPLEX
|
1419 MACH_MSGH_BITS(0, MACH_MSG_TYPE_PORT_SEND_ONCE
);
1420 hdr
->msgh_remote_port
= MACH_PORT_NULL
;
1421 hdr
->msgh_local_port
= (mach_port_t
)dest_name
;
1423 mr
= ipc_kmsg_copyout_body(kmsg
, space
,
1425 MACH_MSG_BODY_NULL
);
1426 if (mr
!= MACH_MSG_SUCCESS
) {
1427 if (ipc_kmsg_put(msg
, kmsg
, hdr
->msgh_size
+
1428 trailer
->msgh_trailer_size
) ==
1429 MACH_RCV_INVALID_DATA
)
1430 return MACH_RCV_INVALID_DATA
;
1432 return mr
| MACH_RCV_BODY_ERROR
;
1434 HOT(c_mmot_hot_ok3
++);
1439 HOT(c_mmot_g_slow_copyout2
++);
1445 mr
= ipc_kmsg_put(rcv_msg
? rcv_msg
: msg
,
1447 hdr
->msgh_size
+ trailer
->msgh_trailer_size
);
1448 if (mr
!= MACH_MSG_SUCCESS
) {
1449 return MACH_RCV_INVALID_DATA
;
1451 current_task()->messages_received
++;
1455 /* BEGINNING OF WARM PATH */
1458 * The slow path has a few non-register temporary
1459 * variables used only for call-by-reference.
1464 ipc_kmsg_t temp_kmsg
;
1465 mach_port_seqno_t temp_seqno
;
1466 ipc_object_t temp_rcv_object
;
1467 ipc_mqueue_t temp_rcv_mqueue
;
1468 register mach_port_name_t reply_name
=
1469 (mach_port_name_t
)hdr
->msgh_local_port
;
1473 * We have the message data in kmsg, but
1474 * we still need to copyin, send it,
1475 * receive a reply, and do copyout.
1478 mr
= ipc_kmsg_copyin(kmsg
, space
, current_map(),
1480 if (mr
!= MACH_MSG_SUCCESS
) {
1481 ipc_kmsg_free(kmsg
);
1485 /* try to get back on optimized path */
1487 if ((reply_name
!= rcv_name
) ||
1488 (hdr
->msgh_bits
& MACH_MSGH_BITS_CIRCULAR
)) {
1489 HOT(c_mmot_cold_048
++);
1493 dest_port
= (ipc_port_t
) hdr
->msgh_remote_port
;
1494 assert(IP_VALID(dest_port
));
1497 if (!ip_active(dest_port
)) {
1498 ip_unlock(dest_port
);
1502 if (dest_port
->ip_receiver
== ipc_space_kernel
) {
1503 dest_port
->ip_messages
.imq_seqno
++;
1504 ip_unlock(dest_port
);
1508 if (!imq_full(&dest_port
->ip_messages
) ||
1509 (MACH_MSGH_BITS_REMOTE(hdr
->msgh_bits
) ==
1510 MACH_MSG_TYPE_PORT_SEND_ONCE
))
1513 * Try an optimized ipc_mqueue_copyin.
1514 * It will work if this is a request message.
1517 register ipc_port_t reply_port
;
1519 reply_port
= (ipc_port_t
) hdr
->msgh_local_port
;
1520 if (IP_VALID(reply_port
)) {
1521 if (ip_lock_try(reply_port
)) {
1522 if (ip_active(reply_port
) &&
1523 reply_port
->ip_receiver
== space
&&
1524 reply_port
->ip_receiver_name
== rcv_name
&&
1525 reply_port
->ip_pset_count
== 0)
1527 /* Grab a reference to the reply port. */
1528 rcv_object
= (ipc_object_t
) reply_port
;
1529 io_reference(rcv_object
);
1530 rcv_mqueue
= &reply_port
->ip_messages
;
1531 io_unlock(rcv_object
);
1532 HOT(c_mmot_getback_FastSR
++);
1533 goto fast_send_receive
;
1535 ip_unlock(reply_port
);
1540 ip_unlock(dest_port
);
1541 HOT(c_mmot_cold_050
++);
1546 * Special case: send message to kernel services.
1547 * The request message has been copied into the
1548 * kmsg. Nothing is locked.
1552 register ipc_port_t reply_port
;
1553 mach_port_seqno_t local_seqno
;
1557 * Perform the kernel function.
1559 c_mmot_kernel_send
++;
1561 current_task()->messages_sent
++;
1563 kmsg
= ipc_kobject_server(kmsg
);
1564 if (kmsg
== IKM_NULL
) {
1566 * No reply. Take the
1567 * slow receive path.
1569 HOT(c_mmot_cold_051
++);
1570 goto slow_get_rcv_port
;
1575 * the reply port is alive
1576 * we hold the receive right
1577 * the name has not changed.
1578 * the port is not in a set
1579 * If any of these are not true,
1580 * we cannot directly receive the reply
1583 hdr
= &kmsg
->ikm_header
;
1584 send_size
= hdr
->msgh_size
;
1585 trailer
= (mach_msg_format_0_trailer_t
*) ((vm_offset_t
) hdr
+
1586 round_msg(send_size
));
1587 reply_port
= (ipc_port_t
) hdr
->msgh_remote_port
;
1588 ip_lock(reply_port
);
1590 if ((!ip_active(reply_port
)) ||
1591 (reply_port
->ip_receiver
!= space
) ||
1592 (reply_port
->ip_receiver_name
!= rcv_name
) ||
1593 (reply_port
->ip_pset_count
!= 0))
1595 ip_unlock(reply_port
);
1596 ipc_kmsg_send_always(kmsg
);
1597 HOT(c_mmot_cold_052
++);
1598 goto slow_get_rcv_port
;
1602 rcv_mqueue
= &reply_port
->ip_messages
;
1603 imq_lock(rcv_mqueue
);
1605 /* keep port locked, and don`t change ref count yet */
1608 * If there are messages on the port
1609 * or other threads waiting for a message,
1610 * we cannot directly receive the reply.
1612 if (!wait_queue_empty(&rcv_mqueue
->imq_wait_queue
) ||
1613 (ipc_kmsg_queue_first(&rcv_mqueue
->imq_messages
) != IKM_NULL
))
1615 imq_unlock(rcv_mqueue
);
1617 ip_unlock(reply_port
);
1618 ipc_kmsg_send_always(kmsg
);
1619 HOT(c_mmot_cold_053
++);
1620 goto slow_get_rcv_port
;
1624 * We can directly receive this reply.
1625 * Since there were no messages queued
1626 * on the reply port, there should be
1627 * no threads blocked waiting to send.
1629 dest_port
= reply_port
;
1630 local_seqno
= rcv_mqueue
->imq_seqno
++;
1631 imq_unlock(rcv_mqueue
);
1635 * inline ipc_object_release.
1636 * Port is still locked.
1637 * Reference count was not incremented.
1639 ip_check_unlock(reply_port
);
1641 if (option
& MACH_RCV_TRAILER_MASK
) {
1642 trailer
->msgh_seqno
= local_seqno
;
1643 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
1645 /* copy out the kernel reply */
1646 HOT(c_mmot_fastkernelreply
++);
1652 * Nothing is locked. We have acquired kmsg, but
1653 * we still need to send it and receive a reply.
1656 mr
= ipc_kmsg_send(kmsg
, MACH_MSG_OPTION_NONE
,
1657 MACH_MSG_TIMEOUT_NONE
);
1658 if (mr
!= MACH_MSG_SUCCESS
) {
1659 mr
|= ipc_kmsg_copyout_pseudo(kmsg
, space
,
1661 MACH_MSG_BODY_NULL
);
1663 (void) ipc_kmsg_put(msg
, kmsg
, hdr
->msgh_size
);
1669 * We have sent the message. Copy in the receive port.
1671 mr
= ipc_mqueue_copyin(space
, rcv_name
,
1672 &temp_rcv_mqueue
, &temp_rcv_object
);
1673 if (mr
!= MACH_MSG_SUCCESS
) {
1676 rcv_mqueue
= temp_rcv_mqueue
;
1677 rcv_object
= temp_rcv_object
;
1678 /* hold ref for rcv_object */
1682 * Now we have sent the request and copied in rcv_name,
1683 * and hold ref for rcv_object (to keep mqueue alive).
1684 * Just receive a reply and try to get back to fast path.
1687 self
->ith_continuation
= (void (*)(mach_msg_return_t
))0;
1688 ipc_mqueue_receive(rcv_mqueue
,
1689 MACH_MSG_OPTION_NONE
,
1691 MACH_MSG_TIMEOUT_NONE
,
1694 mr
= self
->ith_state
;
1695 temp_kmsg
= self
->ith_kmsg
;
1696 temp_seqno
= self
->ith_seqno
;
1698 ipc_object_release(rcv_object
);
1700 if (mr
!= MACH_MSG_SUCCESS
) {
1705 hdr
= &kmsg
->ikm_header
;
1706 send_size
= hdr
->msgh_size
;
1707 trailer
= (mach_msg_format_0_trailer_t
*) ((vm_offset_t
) hdr
+
1708 round_msg(send_size
));
1709 if (option
& MACH_RCV_TRAILER_MASK
) {
1710 trailer
->msgh_seqno
= temp_seqno
;
1711 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
1713 dest_port
= (ipc_port_t
) hdr
->msgh_remote_port
;
1714 HOT(c_mmot_cold_055
++);
1719 * Nothing locked and no references held, except
1720 * we have kmsg with msgh_seqno filled in. Must
1721 * still check against rcv_size and do
1722 * ipc_kmsg_copyout/ipc_kmsg_put.
1725 reply_size
= send_size
+ trailer
->msgh_trailer_size
;
1726 if (rcv_size
< reply_size
) {
1727 if (msg_receive_error(kmsg
, msg
, option
, temp_seqno
,
1728 space
) == MACH_RCV_INVALID_DATA
) {
1729 mr
= MACH_RCV_INVALID_DATA
;
1733 mr
= MACH_RCV_TOO_LARGE
;
1738 mr
= ipc_kmsg_copyout(kmsg
, space
, current_map(),
1739 MACH_PORT_NULL
, MACH_MSG_BODY_NULL
);
1740 if (mr
!= MACH_MSG_SUCCESS
) {
1741 if ((mr
&~ MACH_MSG_MASK
) == MACH_RCV_BODY_ERROR
) {
1742 if (ipc_kmsg_put(msg
, kmsg
, reply_size
) ==
1743 MACH_RCV_INVALID_DATA
)
1744 mr
= MACH_RCV_INVALID_DATA
;
1747 if (msg_receive_error(kmsg
, msg
, option
,
1748 temp_seqno
, space
) == MACH_RCV_INVALID_DATA
)
1749 mr
= MACH_RCV_INVALID_DATA
;
1755 /* try to get back on optimized path */
1756 HOT(c_mmot_getback_fast_put
++);
1761 } /* END OF HOT PATH */
1762 #endif /* ENABLE_HOTPATH */
1764 if (option
& MACH_SEND_MSG
) {
1765 mr
= mach_msg_send(msg
, option
, send_size
,
1767 if (mr
!= MACH_MSG_SUCCESS
) {
1772 if (option
& MACH_RCV_MSG
) {
1773 mach_msg_header_t
*rcv
;
1776 * 1. MACH_RCV_OVERWRITE is on, and rcv_msg is our scatter list
1777 * and receive buffer
1778 * 2. MACH_RCV_OVERWRITE is off, and rcv_msg might be the
1779 * alternate receive buffer (separate send and receive buffers).
1781 if (option
& MACH_RCV_OVERWRITE
)
1783 else if (rcv_msg
!= MACH_MSG_NULL
)
1787 mr
= mach_msg_receive(rcv
, option
, rcv_size
, rcv_name
,
1788 timeout
, thread_syscall_return
, scatter_list_size
);
1789 thread_syscall_return(mr
);
1792 return MACH_MSG_SUCCESS
;
1796 * Routine: mach_msg_trap [mach trap]
1798 * Possibly send a message; possibly receive a message.
1802 * All of mach_msg_send and mach_msg_receive error codes.
1807 mach_msg_header_t
*msg
,
1808 mach_msg_option_t option
,
1809 mach_msg_size_t send_size
,
1810 mach_msg_size_t rcv_size
,
1811 mach_port_name_t rcv_name
,
1812 mach_msg_timeout_t timeout
,
1813 mach_port_name_t notify
)
1815 return mach_msg_overwrite_trap(msg
,
1822 (mach_msg_header_t
*)0,
1823 (mach_msg_size_t
)0);
1828 * Routine: msg_receive_error [internal]
1830 * Builds a minimal header/trailer and copies it to
1831 * the user message buffer. Invoked when in the case of a
1832 * MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error.
1836 * MACH_MSG_SUCCESS minimal header/trailer copied
1837 * MACH_RCV_INVALID_DATA copyout to user buffer failed
1843 mach_msg_header_t
*msg
,
1844 mach_msg_option_t option
,
1845 mach_port_seqno_t seqno
,
1848 mach_msg_format_0_trailer_t
*trailer
;
1851 * Copy out the destination port in the message.
1852 * Destroy all other rights and memory in the message.
1854 ipc_kmsg_copyout_dest(kmsg
, space
);
1857 * Build a minimal message with the requested trailer.
1859 trailer
= (mach_msg_format_0_trailer_t
*)
1860 ((vm_offset_t
)&kmsg
->ikm_header
+
1861 round_msg(sizeof(mach_msg_header_t
)));
1862 kmsg
->ikm_header
.msgh_size
= sizeof(mach_msg_header_t
);
1863 bcopy( (char *)&trailer_template
,
1865 sizeof(trailer_template
));
1866 if (option
& MACH_RCV_TRAILER_MASK
) {
1867 trailer
->msgh_seqno
= seqno
;
1868 trailer
->msgh_trailer_size
= REQUESTED_TRAILER_SIZE(option
);
1872 * Copy the message to user space
1874 if (ipc_kmsg_put(msg
, kmsg
, kmsg
->ikm_header
.msgh_size
+
1875 trailer
->msgh_trailer_size
) == MACH_RCV_INVALID_DATA
)
1876 return(MACH_RCV_INVALID_DATA
);
1878 return(MACH_MSG_SUCCESS
);