2 * Copyright (c) 2000-2007 Apple 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@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 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 * File: ipc/ipc_mqueue.c
63 * Functions to manipulate IPC message queues.
66 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
67 * support for mandatory and extensible security protections. This notice
68 * is included in support of clause 2.2 (b) of the Apple Public License,
73 #include <mach/port.h>
74 #include <mach/message.h>
75 #include <mach/sync_policy.h>
77 #include <kern/assert.h>
78 #include <kern/counters.h>
79 #include <kern/sched_prim.h>
80 #include <kern/ipc_kobject.h>
81 #include <kern/ipc_mig.h> /* XXX - for mach_msg_receive_continue */
82 #include <kern/misc_protos.h>
83 #include <kern/task.h>
84 #include <kern/thread.h>
85 #include <kern/waitq.h>
87 #include <ipc/ipc_mqueue.h>
88 #include <ipc/ipc_kmsg.h>
89 #include <ipc/ipc_port.h>
90 #include <ipc/ipc_pset.h>
91 #include <ipc/ipc_space.h>
94 #include <ipc/flipc.h>
98 #include <vm/vm_map.h>
101 #include <sys/event.h>
103 extern char *proc_name_address(void *p
);
105 int ipc_mqueue_full
; /* address is event for queue space */
106 int ipc_mqueue_rcv
; /* address is event for message arrival */
108 /* forward declarations */
109 void ipc_mqueue_receive_results(wait_result_t result
);
110 static void ipc_mqueue_peek_on_thread(
111 ipc_mqueue_t port_mq
,
112 mach_msg_option_t option
,
116 * Routine: ipc_mqueue_init
118 * Initialize a newly-allocated message queue.
126 waitq_set_init(&mqueue
->imq_set_queue
,
127 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
,
130 waitq_init(&mqueue
->imq_wait_queue
, SYNC_POLICY_FIFO
| SYNC_POLICY_PORT
);
131 ipc_kmsg_queue_init(&mqueue
->imq_messages
);
132 mqueue
->imq_seqno
= 0;
133 mqueue
->imq_msgcount
= 0;
134 mqueue
->imq_qlimit
= MACH_PORT_QLIMIT_DEFAULT
;
135 mqueue
->imq_fullwaiters
= FALSE
;
137 mqueue
->imq_fport
= FPORT_NULL
;
140 klist_init(&mqueue
->imq_klist
);
143 void ipc_mqueue_deinit(
146 boolean_t is_set
= imq_is_set(mqueue
);
149 waitq_set_deinit(&mqueue
->imq_set_queue
);
151 waitq_deinit(&mqueue
->imq_wait_queue
);
155 * Routine: imq_reserve_and_lock
157 * Atomically lock an ipc_mqueue_t object and reserve
158 * an appropriate number of prepost linkage objects for
159 * use in wakeup operations.
164 imq_reserve_and_lock(ipc_mqueue_t mq
, uint64_t *reserved_prepost
)
166 *reserved_prepost
= waitq_prepost_reserve(&mq
->imq_wait_queue
, 0,
173 * Routine: imq_release_and_unlock
175 * Unlock an ipc_mqueue_t object, re-enable interrupts,
176 * and release any unused prepost object reservations.
181 imq_release_and_unlock(ipc_mqueue_t mq
, uint64_t reserved_prepost
)
183 assert(imq_held(mq
));
184 waitq_unlock(&mq
->imq_wait_queue
);
185 waitq_prepost_release_reserve(reserved_prepost
);
190 * Routine: ipc_mqueue_member
192 * Indicate whether the (port) mqueue is a member of
193 * this portset's mqueue. We do this by checking
194 * whether the portset mqueue's waitq is an member of
195 * the port's mqueue waitq.
197 * the portset's mqueue is not already a member
198 * this may block while allocating linkage structures.
203 ipc_mqueue_t port_mqueue
,
204 ipc_mqueue_t set_mqueue
)
206 struct waitq
*port_waitq
= &port_mqueue
->imq_wait_queue
;
207 struct waitq_set
*set_waitq
= &set_mqueue
->imq_set_queue
;
209 return waitq_member(port_waitq
, set_waitq
);
214 * Routine: ipc_mqueue_remove
216 * Remove the association between the queue and the specified
223 ipc_mqueue_t set_mqueue
)
225 struct waitq
*mq_waitq
= &mqueue
->imq_wait_queue
;
226 struct waitq_set
*set_waitq
= &set_mqueue
->imq_set_queue
;
228 return waitq_unlink(mq_waitq
, set_waitq
);
232 * Routine: ipc_mqueue_remove_from_all
234 * Remove the mqueue from all the sets it is a member of
238 * mqueue unlocked and set links deallocated
241 ipc_mqueue_remove_from_all(ipc_mqueue_t mqueue
)
243 struct waitq
*mq_waitq
= &mqueue
->imq_wait_queue
;
248 assert(waitq_valid(mq_waitq
));
249 kr
= waitq_unlink_all_unlock(mq_waitq
);
250 /* mqueue unlocked and set links deallocated */
254 * Routine: ipc_mqueue_remove_all
256 * Remove all the member queues from the specified set.
257 * Also removes the queue from any containing sets.
261 * mqueue unlocked all set links deallocated
264 ipc_mqueue_remove_all(ipc_mqueue_t mqueue
)
266 struct waitq_set
*mq_setq
= &mqueue
->imq_set_queue
;
269 assert(waitqs_is_set(mq_setq
));
270 waitq_set_unlink_all_unlock(mq_setq
);
271 /* mqueue unlocked set links deallocated */
276 * Routine: ipc_mqueue_add
278 * Associate the portset's mqueue with the port's mqueue.
279 * This has to be done so that posting the port will wakeup
280 * a portset waiter. If there are waiters on the portset
281 * mqueue and messages on the port mqueue, try to match them
288 ipc_mqueue_t port_mqueue
,
289 ipc_mqueue_t set_mqueue
,
290 uint64_t *reserved_link
,
291 uint64_t *reserved_prepost
)
293 struct waitq
*port_waitq
= &port_mqueue
->imq_wait_queue
;
294 struct waitq_set
*set_waitq
= &set_mqueue
->imq_set_queue
;
295 ipc_kmsg_queue_t kmsgq
;
296 ipc_kmsg_t kmsg
, next
;
299 assert(reserved_link
&& *reserved_link
!= 0);
300 assert(waitqs_is_linked(set_waitq
));
302 imq_lock(port_mqueue
);
305 * The link operation is now under the same lock-hold as
306 * message iteration and thread wakeup, but doesn't have to be...
308 kr
= waitq_link(port_waitq
, set_waitq
, WAITQ_ALREADY_LOCKED
, reserved_link
);
309 if (kr
!= KERN_SUCCESS
) {
310 imq_unlock(port_mqueue
);
315 * Now that the set has been added to the port, there may be
316 * messages queued on the port and threads waiting on the set
317 * waitq. Lets get them together.
319 kmsgq
= &port_mqueue
->imq_messages
;
320 for (kmsg
= ipc_kmsg_queue_first(kmsgq
);
323 next
= ipc_kmsg_queue_next(kmsgq
, kmsg
);
327 mach_msg_size_t msize
;
330 th
= waitq_wakeup64_identify_locked(
333 THREAD_AWAKENED
, &th_spl
,
334 reserved_prepost
, WAITQ_ALL_PRIORITIES
,
336 /* waitq/mqueue still locked, thread locked */
338 if (th
== THREAD_NULL
)
342 * If the receiver waited with a facility not directly
343 * related to Mach messaging, then it isn't prepared to get
344 * handed the message directly. Just set it running, and
345 * go look for another thread that can.
347 if (th
->ith_state
!= MACH_RCV_IN_PROGRESS
) {
348 if (th
->ith_state
== MACH_PEEK_IN_PROGRESS
) {
350 * wakeup the peeking thread, but
351 * continue to loop over the threads
352 * waiting on the port's mqueue to see
353 * if there are any actual receivers
355 ipc_mqueue_peek_on_thread(port_mqueue
,
365 * Found a receiver. see if they can handle the message
366 * correctly (the message is not too large for them, or
367 * they didn't care to be informed that the message was
368 * too large). If they can't handle it, take them off
369 * the list and let them go back and figure it out and
370 * just move onto the next.
372 msize
= ipc_kmsg_copyout_size(kmsg
, th
->map
);
374 (msize
+ REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(th
), th
->ith_option
))) {
375 th
->ith_state
= MACH_RCV_TOO_LARGE
;
376 th
->ith_msize
= msize
;
377 if (th
->ith_option
& MACH_RCV_LARGE
) {
379 * let him go without message
381 th
->ith_receiver_name
= port_mqueue
->imq_receiver_name
;
382 th
->ith_kmsg
= IKM_NULL
;
386 continue; /* find another thread */
389 th
->ith_state
= MACH_MSG_SUCCESS
;
393 * This thread is going to take this message,
396 ipc_kmsg_rmqueue(kmsgq
, kmsg
);
398 mach_node_t node
= kmsg
->ikm_node
;
400 ipc_mqueue_release_msgcount(port_mqueue
, IMQ_NULL
);
403 th
->ith_seqno
= port_mqueue
->imq_seqno
++;
407 if (MACH_NODE_VALID(node
) && FPORT_VALID(port_mqueue
->imq_fport
))
408 flipc_msg_ack(node
, port_mqueue
, TRUE
);
410 break; /* go to next message */
414 imq_unlock(port_mqueue
);
419 * Routine: ipc_mqueue_changed
421 * Wake up receivers waiting in a message queue.
423 * The message queue is locked.
430 if (IMQ_KLIST_VALID(mqueue
)) {
432 * Indicate that this message queue is vanishing
434 * When this is called, the associated receive right may be in flight
435 * between two tasks: the one it used to live in, and the one that armed
436 * a port destroyed notification for it.
438 * The new process may want to register the port it gets back with an
439 * EVFILT_MACHPORT filter again, and may have pending sync IPC on this
440 * port pending already, in which case we want the imq_klist field to be
441 * reusable for nefarious purposes (see IMQ_SET_INHERITOR).
443 * Fortunately, we really don't need this linkage anymore after this
444 * point as EV_VANISHED / EV_EOF will be the last thing delivered ever.
446 knote_vanish(&mqueue
->imq_klist
);
447 klist_init(&mqueue
->imq_klist
);
450 waitq_wakeup64_all_locked(&mqueue
->imq_wait_queue
,
454 WAITQ_ALL_PRIORITIES
,
462 * Routine: ipc_mqueue_send
464 * Send a message to a message queue. The message holds a reference
465 * for the destination port for this message queue in the
466 * msgh_remote_port field.
468 * If unsuccessful, the caller still has possession of
469 * the message and must do something with it. If successful,
470 * the message is queued, given to a receiver, or destroyed.
474 * MACH_MSG_SUCCESS The message was accepted.
475 * MACH_SEND_TIMED_OUT Caller still has message.
476 * MACH_SEND_INTERRUPTED Caller still has message.
482 mach_msg_option_t option
,
483 mach_msg_timeout_t send_timeout
)
489 * 1) We're under the queue limit.
490 * 2) Caller used the MACH_SEND_ALWAYS internal option.
491 * 3) Message is sent to a send-once right.
493 if (!imq_full(mqueue
) ||
494 (!imq_full_kernel(mqueue
) &&
495 ((option
& MACH_SEND_ALWAYS
) ||
496 (MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
->msgh_bits
) ==
497 MACH_MSG_TYPE_PORT_SEND_ONCE
)))) {
498 mqueue
->imq_msgcount
++;
499 assert(mqueue
->imq_msgcount
> 0);
502 thread_t cur_thread
= current_thread();
503 ipc_port_t port
= ip_from_mq(mqueue
);
504 struct turnstile
*send_turnstile
= TURNSTILE_NULL
;
505 turnstile_inheritor_t inheritor
= TURNSTILE_INHERITOR_NULL
;
509 * We have to wait for space to be granted to us.
511 if ((option
& MACH_SEND_TIMEOUT
) && (send_timeout
== 0)) {
513 return MACH_SEND_TIMED_OUT
;
515 if (imq_full_kernel(mqueue
)) {
517 return MACH_SEND_NO_BUFFER
;
519 mqueue
->imq_fullwaiters
= TRUE
;
521 if (option
& MACH_SEND_TIMEOUT
)
522 clock_interval_to_deadline(send_timeout
, 1000*NSEC_PER_USEC
, &deadline
);
526 thread_set_pending_block_hint(cur_thread
, kThreadWaitPortSend
);
528 send_turnstile
= turnstile_prepare((uintptr_t)port
,
529 port_send_turnstile_address(port
),
530 TURNSTILE_NULL
, TURNSTILE_SYNC_IPC
);
532 /* Check if the port in is in transit, get the destination port's turnstile */
533 if (ip_active(port
) &&
534 port
->ip_receiver_name
== MACH_PORT_NULL
&&
535 port
->ip_destination
!= NULL
) {
536 inheritor
= port_send_turnstile(port
->ip_destination
);
538 inheritor
= ipc_port_get_inheritor(port
);
541 turnstile_update_inheritor(send_turnstile
, inheritor
,
542 TURNSTILE_DELAYED_UPDATE
| TURNSTILE_INHERITOR_TURNSTILE
);
544 wresult
= waitq_assert_wait64_leeway(
545 &send_turnstile
->ts_waitq
,
548 TIMEOUT_URGENCY_USER_NORMAL
,
553 turnstile_update_inheritor_complete(send_turnstile
,
554 TURNSTILE_INTERLOCK_NOT_HELD
);
556 if (wresult
== THREAD_WAITING
) {
557 wresult
= thread_block(THREAD_CONTINUE_NULL
);
558 counter(c_ipc_mqueue_send_block
++);
561 /* Call turnstile complete with interlock held */
563 turnstile_complete((uintptr_t)port
, port_send_turnstile_address(port
), NULL
);
566 /* Call cleanup after dropping the interlock */
571 case THREAD_AWAKENED
:
573 * we can proceed - inherited msgcount from waker
574 * or the message queue has been destroyed and the msgcount
575 * has been reset to zero (will detect in ipc_mqueue_post()).
579 case THREAD_TIMED_OUT
:
580 assert(option
& MACH_SEND_TIMEOUT
);
581 return MACH_SEND_TIMED_OUT
;
583 case THREAD_INTERRUPTED
:
584 return MACH_SEND_INTERRUPTED
;
587 /* mqueue is being destroyed */
588 return MACH_SEND_INVALID_DEST
;
590 panic("ipc_mqueue_send");
594 ipc_mqueue_post(mqueue
, kmsg
, option
);
595 return MACH_MSG_SUCCESS
;
599 * Routine: ipc_mqueue_override_send
601 * Set an override qos on the first message in the queue
602 * (if the queue is full). This is a send-possible override
603 * that will go away as soon as we drain a message from the
607 * The message queue is not locked.
608 * The caller holds a reference on the message queue.
610 extern void ipc_mqueue_override_send(
612 mach_msg_priority_t override
)
614 boolean_t __unused full_queue_empty
= FALSE
;
617 assert(imq_valid(mqueue
));
618 assert(!imq_is_set(mqueue
));
620 if (imq_full(mqueue
)) {
621 ipc_kmsg_t first
= ipc_kmsg_queue_first(&mqueue
->imq_messages
);
623 if (first
&& ipc_kmsg_override_qos(&mqueue
->imq_messages
, first
, override
)) {
624 if (IMQ_KLIST_VALID(mqueue
))
625 KNOTE(&mqueue
->imq_klist
, 0);
628 full_queue_empty
= TRUE
;
632 #if DEVELOPMENT || DEBUG
633 if (full_queue_empty
) {
634 ipc_port_t port
= ip_from_mq(mqueue
);
636 if (ip_active(port
) && !port
->ip_tempowner
&&
637 port
->ip_receiver_name
&& port
->ip_receiver
&&
638 port
->ip_receiver
!= ipc_space_kernel
) {
639 dst_pid
= task_pid(port
->ip_receiver
->is_task
);
646 * Routine: ipc_mqueue_release_msgcount
648 * Release a message queue reference in the case where we
652 * The message queue is locked.
653 * The message corresponding to this reference is off the queue.
654 * There is no need to pass reserved preposts because this will
655 * never prepost to anyone
658 ipc_mqueue_release_msgcount(ipc_mqueue_t port_mq
, ipc_mqueue_t set_mq
)
660 struct turnstile
*send_turnstile
= port_send_turnstile(ip_from_mq(port_mq
));
662 assert(imq_held(port_mq
));
663 assert(port_mq
->imq_msgcount
> 1 || ipc_kmsg_queue_empty(&port_mq
->imq_messages
));
665 port_mq
->imq_msgcount
--;
667 if (!imq_full(port_mq
) && port_mq
->imq_fullwaiters
&&
668 send_turnstile
!= TURNSTILE_NULL
) {
670 * boost the priority of the awoken thread
671 * (WAITQ_PROMOTE_PRIORITY) to ensure it uses
672 * the message queue slot we've just reserved.
674 * NOTE: this will never prepost
676 * The wakeup happens on a turnstile waitq
677 * which will wakeup the highest priority waiter.
678 * A potential downside of this would be starving low
679 * priority senders if there is a constant churn of
680 * high priority threads trying to send to this port.
682 if (waitq_wakeup64_one(&send_turnstile
->ts_waitq
,
685 WAITQ_PROMOTE_PRIORITY
) != KERN_SUCCESS
) {
686 port_mq
->imq_fullwaiters
= FALSE
;
688 /* gave away our slot - add reference back */
689 port_mq
->imq_msgcount
++;
693 if (ipc_kmsg_queue_empty(&port_mq
->imq_messages
)) {
694 /* no more msgs: invalidate the port's prepost object */
695 waitq_clear_prepost_locked(&port_mq
->imq_wait_queue
);
700 * Routine: ipc_mqueue_post
702 * Post a message to a waiting receiver or enqueue it. If a
703 * receiver is waiting, we can release our reserved space in
708 * If we need to queue, our space in the message queue is reserved.
714 mach_msg_option_t __unused option
)
716 uint64_t reserved_prepost
= 0;
717 boolean_t destroy_msg
= FALSE
;
719 ipc_kmsg_trace_send(kmsg
, option
);
722 * While the msg queue is locked, we have control of the
723 * kmsg, so the ref in it for the port is still good.
725 * Check for a receiver for the message.
727 imq_reserve_and_lock(mqueue
, &reserved_prepost
);
729 /* we may have raced with port destruction! */
730 if (!imq_valid(mqueue
)) {
736 struct waitq
*waitq
= &mqueue
->imq_wait_queue
;
739 mach_msg_size_t msize
;
741 receiver
= waitq_wakeup64_identify_locked(waitq
,
746 WAITQ_ALL_PRIORITIES
,
748 /* waitq still locked, thread locked */
750 if (receiver
== THREAD_NULL
) {
753 * no receivers; queue kmsg if space still reserved
754 * Reservations are cancelled when the port goes inactive.
755 * note that this will enqueue the message for any
756 * "peeking" receivers.
758 * Also, post the knote to wake up any threads waiting
759 * on that style of interface if this insertion is of
760 * note (first insertion, or adjusted override qos all
761 * the way to the head of the queue).
763 * This is just for ports. portset knotes are stay-active,
764 * and their threads get awakened through the !MACH_RCV_IN_PROGRESS
767 if (mqueue
->imq_msgcount
> 0) {
768 if (ipc_kmsg_enqueue_qos(&mqueue
->imq_messages
, kmsg
)) {
769 if (IMQ_KLIST_VALID(mqueue
))
770 KNOTE(&mqueue
->imq_klist
, 0);
776 * Otherwise, the message queue must belong to an inactive
777 * port, so just destroy the message and pretend it was posted.
784 * If a thread is attempting a "peek" into the message queue
785 * (MACH_PEEK_IN_PROGRESS), then we enqueue the message and set the
786 * thread running. A successful peek is essentially the same as
787 * message delivery since the peeking thread takes responsibility
788 * for delivering the message and (eventually) removing it from
789 * the mqueue. Only one thread can successfully use the peek
790 * facility on any given port, so we exit the waitq loop after
791 * encountering such a thread.
793 if (receiver
->ith_state
== MACH_PEEK_IN_PROGRESS
&& mqueue
->imq_msgcount
> 0) {
794 ipc_kmsg_enqueue_qos(&mqueue
->imq_messages
, kmsg
);
795 ipc_mqueue_peek_on_thread(mqueue
, receiver
->ith_option
, receiver
);
796 thread_unlock(receiver
);
798 break; /* Message was posted, so break out of loop */
802 * If the receiver waited with a facility not directly related
803 * to Mach messaging, then it isn't prepared to get handed the
804 * message directly. Just set it running, and go look for
805 * another thread that can.
807 if (receiver
->ith_state
!= MACH_RCV_IN_PROGRESS
) {
808 thread_unlock(receiver
);
815 * We found a waiting thread.
816 * If the message is too large or the scatter list is too small
817 * the thread we wake up will get that as its status.
819 msize
= ipc_kmsg_copyout_size(kmsg
, receiver
->map
);
820 if (receiver
->ith_rsize
<
821 (msize
+ REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(receiver
), receiver
->ith_option
))) {
822 receiver
->ith_msize
= msize
;
823 receiver
->ith_state
= MACH_RCV_TOO_LARGE
;
825 receiver
->ith_state
= MACH_MSG_SUCCESS
;
829 * If there is no problem with the upcoming receive, or the
830 * receiver thread didn't specifically ask for special too
831 * large error condition, go ahead and select it anyway.
833 if ((receiver
->ith_state
== MACH_MSG_SUCCESS
) ||
834 !(receiver
->ith_option
& MACH_RCV_LARGE
)) {
835 receiver
->ith_kmsg
= kmsg
;
836 receiver
->ith_seqno
= mqueue
->imq_seqno
++;
838 mach_node_t node
= kmsg
->ikm_node
;
840 thread_unlock(receiver
);
843 /* we didn't need our reserved spot in the queue */
844 ipc_mqueue_release_msgcount(mqueue
, IMQ_NULL
);
847 if (MACH_NODE_VALID(node
) && FPORT_VALID(mqueue
->imq_fport
))
848 flipc_msg_ack(node
, mqueue
, TRUE
);
854 * Otherwise, this thread needs to be released to run
855 * and handle its error without getting the message. We
856 * need to go back and pick another one.
858 receiver
->ith_receiver_name
= mqueue
->imq_receiver_name
;
859 receiver
->ith_kmsg
= IKM_NULL
;
860 receiver
->ith_seqno
= 0;
861 thread_unlock(receiver
);
866 /* clear the waitq boost we may have been given */
867 waitq_clear_promotion_locked(&mqueue
->imq_wait_queue
, current_thread());
868 imq_release_and_unlock(mqueue
, reserved_prepost
);
870 ipc_kmsg_destroy(kmsg
);
872 current_task()->messages_sent
++;
878 ipc_mqueue_receive_results(wait_result_t saved_wait_result
)
880 thread_t self
= current_thread();
881 mach_msg_option_t option
= self
->ith_option
;
884 * why did we wake up?
886 switch (saved_wait_result
) {
887 case THREAD_TIMED_OUT
:
888 self
->ith_state
= MACH_RCV_TIMED_OUT
;
891 case THREAD_INTERRUPTED
:
892 self
->ith_state
= MACH_RCV_INTERRUPTED
;
896 /* something bad happened to the port/set */
897 self
->ith_state
= MACH_RCV_PORT_CHANGED
;
900 case THREAD_AWAKENED
:
902 * We do not need to go select a message, somebody
903 * handed us one (or a too-large indication).
905 switch (self
->ith_state
) {
906 case MACH_RCV_SCATTER_SMALL
:
907 case MACH_RCV_TOO_LARGE
:
909 * Somebody tried to give us a too large
910 * message. If we indicated that we cared,
911 * then they only gave us the indication,
912 * otherwise they gave us the indication
913 * AND the message anyway.
915 if (option
& MACH_RCV_LARGE
) {
919 case MACH_MSG_SUCCESS
:
920 case MACH_PEEK_READY
:
924 panic("ipc_mqueue_receive_results: strange ith_state");
928 panic("ipc_mqueue_receive_results: strange wait_result");
933 ipc_mqueue_receive_continue(
934 __unused
void *param
,
935 wait_result_t wresult
)
937 ipc_mqueue_receive_results(wresult
);
938 mach_msg_receive_continue(); /* hard-coded for now */
942 * Routine: ipc_mqueue_receive
944 * Receive a message from a message queue.
947 * Our caller must hold a reference for the port or port set
948 * to which this queue belongs, to keep the queue
949 * from being deallocated.
951 * The kmsg is returned with clean header fields
952 * and with the circular bit turned off through the ith_kmsg
953 * field of the thread's receive continuation state.
955 * MACH_MSG_SUCCESS Message returned in ith_kmsg.
956 * MACH_RCV_TOO_LARGE Message size returned in ith_msize.
957 * MACH_RCV_TIMED_OUT No message obtained.
958 * MACH_RCV_INTERRUPTED No message obtained.
959 * MACH_RCV_PORT_DIED Port/set died; no message.
960 * MACH_RCV_PORT_CHANGED Port moved into set; no msg.
967 mach_msg_option_t option
,
968 mach_msg_size_t max_size
,
969 mach_msg_timeout_t rcv_timeout
,
972 wait_result_t wresult
;
973 thread_t self
= current_thread();
976 wresult
= ipc_mqueue_receive_on_thread(mqueue
, option
, max_size
,
977 rcv_timeout
, interruptible
,
979 /* mqueue unlocked */
980 if (wresult
== THREAD_NOT_WAITING
)
983 if (wresult
== THREAD_WAITING
) {
984 counter((interruptible
== THREAD_ABORTSAFE
) ?
985 c_ipc_mqueue_receive_block_user
++ :
986 c_ipc_mqueue_receive_block_kernel
++);
988 if (self
->ith_continuation
)
989 thread_block(ipc_mqueue_receive_continue
);
992 wresult
= thread_block(THREAD_CONTINUE_NULL
);
994 ipc_mqueue_receive_results(wresult
);
997 static int mqueue_process_prepost_receive(void *ctx
, struct waitq
*waitq
,
998 struct waitq_set
*wqset
)
1000 ipc_mqueue_t port_mq
, *pmq_ptr
;
1003 port_mq
= (ipc_mqueue_t
)waitq
;
1006 * If there are no messages on this queue, skip it and remove
1007 * it from the prepost list
1009 if (ipc_kmsg_queue_empty(&port_mq
->imq_messages
))
1010 return WQ_ITERATE_INVALIDATE_CONTINUE
;
1013 * There are messages waiting on this port.
1014 * Instruct the prepost iteration logic to break, but keep the
1017 pmq_ptr
= (ipc_mqueue_t
*)ctx
;
1020 return WQ_ITERATE_BREAK_KEEP_LOCKED
;
1024 * Routine: ipc_mqueue_receive_on_thread
1026 * Receive a message from a message queue using a specified thread.
1027 * If no message available, assert_wait on the appropriate waitq.
1030 * Assumes thread is self.
1031 * Called with mqueue locked.
1032 * Returns with mqueue unlocked.
1033 * May have assert-waited. Caller must block in those cases.
1036 ipc_mqueue_receive_on_thread(
1037 ipc_mqueue_t mqueue
,
1038 mach_msg_option_t option
,
1039 mach_msg_size_t max_size
,
1040 mach_msg_timeout_t rcv_timeout
,
1044 wait_result_t wresult
;
1046 struct turnstile
*rcv_turnstile
= TURNSTILE_NULL
;
1047 turnstile_inheritor_t inheritor
= NULL
;
1049 /* called with mqueue locked */
1051 /* no need to reserve anything: we never prepost to anyone */
1053 if (!imq_valid(mqueue
)) {
1054 /* someone raced us to destroy this mqueue/port! */
1057 * ipc_mqueue_receive_results updates the thread's ith_state
1058 * TODO: differentiate between rights being moved and
1059 * rights/ports being destroyed (21885327)
1061 return THREAD_RESTART
;
1064 if (imq_is_set(mqueue
)) {
1065 ipc_mqueue_t port_mq
= IMQ_NULL
;
1067 (void)waitq_set_iterate_preposts(&mqueue
->imq_set_queue
,
1069 mqueue_process_prepost_receive
);
1071 if (port_mq
!= IMQ_NULL
) {
1073 * We get here if there is at least one message
1074 * waiting on port_mq. We have instructed the prepost
1075 * iteration logic to leave both the port_mq and the
1076 * set mqueue locked.
1078 * TODO: previously, we would place this port at the
1079 * back of the prepost list...
1084 * Continue on to handling the message with just
1085 * the port mqueue locked.
1087 if (option
& MACH_PEEK_MSG
)
1088 ipc_mqueue_peek_on_thread(port_mq
, option
, thread
);
1090 ipc_mqueue_select_on_thread(port_mq
, mqueue
, option
,
1093 imq_unlock(port_mq
);
1094 return THREAD_NOT_WAITING
;
1096 } else if (imq_is_queue(mqueue
)) {
1097 ipc_kmsg_queue_t kmsgs
;
1100 * Receive on a single port. Just try to get the messages.
1102 kmsgs
= &mqueue
->imq_messages
;
1103 if (ipc_kmsg_queue_first(kmsgs
) != IKM_NULL
) {
1104 if (option
& MACH_PEEK_MSG
)
1105 ipc_mqueue_peek_on_thread(mqueue
, option
, thread
);
1107 ipc_mqueue_select_on_thread(mqueue
, IMQ_NULL
, option
,
1110 return THREAD_NOT_WAITING
;
1113 panic("Unknown mqueue type 0x%x: likely memory corruption!\n",
1114 mqueue
->imq_wait_queue
.waitq_type
);
1118 * Looks like we'll have to block. The mqueue we will
1119 * block on (whether the set's or the local port's) is
1122 if (option
& MACH_RCV_TIMEOUT
) {
1123 if (rcv_timeout
== 0) {
1125 thread
->ith_state
= MACH_RCV_TIMED_OUT
;
1126 return THREAD_NOT_WAITING
;
1130 thread
->ith_option
= option
;
1131 thread
->ith_rsize
= max_size
;
1132 thread
->ith_msize
= 0;
1134 if (option
& MACH_PEEK_MSG
)
1135 thread
->ith_state
= MACH_PEEK_IN_PROGRESS
;
1137 thread
->ith_state
= MACH_RCV_IN_PROGRESS
;
1139 if (option
& MACH_RCV_TIMEOUT
)
1140 clock_interval_to_deadline(rcv_timeout
, 1000*NSEC_PER_USEC
, &deadline
);
1145 * Threads waiting on a port (not portset)
1146 * will wait on port's receive turnstile.
1147 * Donate waiting thread's turnstile and
1148 * setup inheritor for special reply port.
1149 * Based on the state of the special reply
1150 * port, the inheritor would be the send
1151 * turnstile of the connection port on which
1152 * the send of sync ipc would happen or
1153 * workloop's turnstile who would reply to
1154 * the sync ipc message.
1156 * Pass in mqueue wait in waitq_assert_wait to
1157 * support port set wakeup. The mqueue waitq of port
1158 * will be converted to to turnstile waitq
1159 * in waitq_assert_wait instead of global waitqs.
1161 if (imq_is_queue(mqueue
)) {
1162 ipc_port_t port
= ip_from_mq(mqueue
);
1163 rcv_turnstile
= turnstile_prepare((uintptr_t)port
,
1164 port_rcv_turnstile_address(port
),
1165 TURNSTILE_NULL
, TURNSTILE_SYNC_IPC
);
1167 if (port
->ip_specialreply
) {
1168 inheritor
= ipc_port_get_special_reply_port_inheritor(port
);
1171 turnstile_update_inheritor(rcv_turnstile
, inheritor
,
1172 (TURNSTILE_INHERITOR_TURNSTILE
| TURNSTILE_DELAYED_UPDATE
));
1175 thread_set_pending_block_hint(thread
, kThreadWaitPortReceive
);
1176 wresult
= waitq_assert_wait64_locked(&mqueue
->imq_wait_queue
,
1179 TIMEOUT_URGENCY_USER_NORMAL
,
1183 /* preposts should be detected above, not here */
1184 if (wresult
== THREAD_AWAKENED
)
1185 panic("ipc_mqueue_receive_on_thread: sleep walking");
1189 /* Check if its a port mqueue and if it needs to call turnstile_update_inheritor_complete */
1190 if (rcv_turnstile
!= TURNSTILE_NULL
) {
1191 turnstile_update_inheritor_complete(rcv_turnstile
, TURNSTILE_INTERLOCK_NOT_HELD
);
1193 /* Its callers responsibility to call turnstile_complete to get the turnstile back */
1200 * Routine: ipc_mqueue_peek_on_thread
1202 * A receiver discovered that there was a message on the queue
1203 * before he had to block. Tell a thread about the message queue,
1204 * but don't pick off any messages.
1207 * at least one message on port_mq's message queue
1209 * Returns: (on thread->ith_state)
1210 * MACH_PEEK_READY ith_peekq contains a message queue
1213 ipc_mqueue_peek_on_thread(
1214 ipc_mqueue_t port_mq
,
1215 mach_msg_option_t option
,
1219 assert(option
& MACH_PEEK_MSG
);
1220 assert(ipc_kmsg_queue_first(&port_mq
->imq_messages
) != IKM_NULL
);
1223 * Take a reference on the mqueue's associated port:
1224 * the peeking thread will be responsible to release this reference
1225 * using ip_release_mq()
1227 ip_reference_mq(port_mq
);
1228 thread
->ith_peekq
= port_mq
;
1229 thread
->ith_state
= MACH_PEEK_READY
;
1233 * Routine: ipc_mqueue_select_on_thread
1235 * A receiver discovered that there was a message on the queue
1236 * before he had to block. Pick the message off the queue and
1237 * "post" it to thread.
1240 * thread not locked.
1241 * There is a message.
1242 * No need to reserve prepost objects - it will never prepost
1245 * MACH_MSG_SUCCESS Actually selected a message for ourselves.
1246 * MACH_RCV_TOO_LARGE May or may not have pull it, but it is large
1249 ipc_mqueue_select_on_thread(
1250 ipc_mqueue_t port_mq
,
1251 ipc_mqueue_t set_mq
,
1252 mach_msg_option_t option
,
1253 mach_msg_size_t max_size
,
1257 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
1258 mach_msg_size_t msize
;
1261 * Do some sanity checking of our ability to receive
1262 * before pulling the message off the queue.
1264 kmsg
= ipc_kmsg_queue_first(&port_mq
->imq_messages
);
1265 assert(kmsg
!= IKM_NULL
);
1268 * If we really can't receive it, but we had the
1269 * MACH_RCV_LARGE option set, then don't take it off
1270 * the queue, instead return the appropriate error
1271 * (and size needed).
1273 msize
= ipc_kmsg_copyout_size(kmsg
, thread
->map
);
1274 if (msize
+ REQUESTED_TRAILER_SIZE(thread_is_64bit_addr(thread
), option
) > max_size
) {
1275 mr
= MACH_RCV_TOO_LARGE
;
1276 if (option
& MACH_RCV_LARGE
) {
1277 thread
->ith_receiver_name
= port_mq
->imq_receiver_name
;
1278 thread
->ith_kmsg
= IKM_NULL
;
1279 thread
->ith_msize
= msize
;
1280 thread
->ith_seqno
= 0;
1281 thread
->ith_state
= mr
;
1286 ipc_kmsg_rmqueue(&port_mq
->imq_messages
, kmsg
);
1288 if (MACH_NODE_VALID(kmsg
->ikm_node
) && FPORT_VALID(port_mq
->imq_fport
))
1289 flipc_msg_ack(kmsg
->ikm_node
, port_mq
, TRUE
);
1291 ipc_mqueue_release_msgcount(port_mq
, set_mq
);
1292 thread
->ith_seqno
= port_mq
->imq_seqno
++;
1293 thread
->ith_kmsg
= kmsg
;
1294 thread
->ith_state
= mr
;
1296 current_task()->messages_received
++;
1301 * Routine: ipc_mqueue_peek_locked
1303 * Peek at a (non-set) message queue to see if it has a message
1304 * matching the sequence number provided (if zero, then the
1305 * first message in the queue) and return vital info about the
1309 * The ipc_mqueue_t is locked by callers.
1310 * Other locks may be held by callers, so this routine cannot block.
1311 * Caller holds reference on the message queue.
1314 ipc_mqueue_peek_locked(ipc_mqueue_t mq
,
1315 mach_port_seqno_t
* seqnop
,
1316 mach_msg_size_t
* msg_sizep
,
1317 mach_msg_id_t
* msg_idp
,
1318 mach_msg_max_trailer_t
* msg_trailerp
,
1321 ipc_kmsg_queue_t kmsgq
;
1323 mach_port_seqno_t seqno
, msgoff
;
1326 assert(!imq_is_set(mq
));
1333 seqno
= mq
->imq_seqno
;
1335 } else if (seqno
>= mq
->imq_seqno
&&
1336 seqno
< mq
->imq_seqno
+ mq
->imq_msgcount
) {
1337 msgoff
= seqno
- mq
->imq_seqno
;
1341 /* look for the message that would match that seqno */
1342 kmsgq
= &mq
->imq_messages
;
1343 kmsg
= ipc_kmsg_queue_first(kmsgq
);
1344 while (msgoff
-- && kmsg
!= IKM_NULL
) {
1345 kmsg
= ipc_kmsg_queue_next(kmsgq
, kmsg
);
1347 if (kmsg
== IKM_NULL
)
1350 /* found one - return the requested info */
1353 if (msg_sizep
!= NULL
)
1354 *msg_sizep
= kmsg
->ikm_header
->msgh_size
;
1355 if (msg_idp
!= NULL
)
1356 *msg_idp
= kmsg
->ikm_header
->msgh_id
;
1357 if (msg_trailerp
!= NULL
)
1358 memcpy(msg_trailerp
,
1359 (mach_msg_max_trailer_t
*)((vm_offset_t
)kmsg
->ikm_header
+
1360 round_msg(kmsg
->ikm_header
->msgh_size
)),
1361 sizeof(mach_msg_max_trailer_t
));
1373 * Routine: ipc_mqueue_peek
1375 * Peek at a (non-set) message queue to see if it has a message
1376 * matching the sequence number provided (if zero, then the
1377 * first message in the queue) and return vital info about the
1381 * The ipc_mqueue_t is unlocked.
1382 * Locks may be held by callers, so this routine cannot block.
1383 * Caller holds reference on the message queue.
1386 ipc_mqueue_peek(ipc_mqueue_t mq
,
1387 mach_port_seqno_t
* seqnop
,
1388 mach_msg_size_t
* msg_sizep
,
1389 mach_msg_id_t
* msg_idp
,
1390 mach_msg_max_trailer_t
* msg_trailerp
,
1397 res
= ipc_mqueue_peek_locked(mq
, seqnop
, msg_sizep
, msg_idp
,
1398 msg_trailerp
, kmsgp
);
1405 * Routine: ipc_mqueue_release_peek_ref
1407 * Release the reference on an mqueue's associated port which was
1408 * granted to a thread in ipc_mqueue_peek_on_thread (on the
1409 * MACH_PEEK_MSG thread wakeup path).
1412 * The ipc_mqueue_t should be locked on entry.
1413 * The ipc_mqueue_t will be _unlocked_ on return
1414 * (and potentially invalid!)
1417 void ipc_mqueue_release_peek_ref(ipc_mqueue_t mq
)
1419 assert(!imq_is_set(mq
));
1420 assert(imq_held(mq
));
1423 * clear any preposts this mq may have generated
1424 * (which would cause subsequent immediate wakeups)
1426 waitq_clear_prepost_locked(&mq
->imq_wait_queue
);
1431 * release the port reference: we need to do this outside the lock
1432 * because we might be holding the last port reference!
1438 * peek at the contained port message queues, break prepost iteration as soon
1439 * as we spot a message on one of the message queues referenced by the set's
1440 * prepost list. No need to lock each message queue, as only the head of each
1441 * queue is checked. If a message wasn't there before we entered here, no need
1442 * to find it (if we do, great).
1444 static int mqueue_peek_iterator(void *ctx
, struct waitq
*waitq
,
1445 struct waitq_set
*wqset
)
1447 ipc_mqueue_t port_mq
= (ipc_mqueue_t
)waitq
;
1448 ipc_kmsg_queue_t kmsgs
= &port_mq
->imq_messages
;
1453 if (ipc_kmsg_queue_first(kmsgs
) != IKM_NULL
)
1454 return WQ_ITERATE_BREAK
; /* break out of the prepost iteration */
1456 return WQ_ITERATE_CONTINUE
;
1460 * Routine: ipc_mqueue_set_peek
1462 * Peek at a message queue set to see if it has any ports
1466 * Locks may be held by callers, so this routine cannot block.
1467 * Caller holds reference on the message queue.
1470 ipc_mqueue_set_peek(ipc_mqueue_t mq
)
1477 * We may have raced with port destruction where the mqueue is marked
1478 * as invalid. In that case, even though we don't have messages, we
1479 * have an end-of-life event to deliver.
1481 if (!imq_is_valid(mq
))
1484 ret
= waitq_set_iterate_preposts(&mq
->imq_set_queue
, NULL
,
1485 mqueue_peek_iterator
);
1489 return (ret
== WQ_ITERATE_BREAK
);
1493 * Routine: ipc_mqueue_set_gather_member_names
1495 * Discover all ports which are members of a given port set.
1496 * Because the waitq linkage mechanism was redesigned to save
1497 * significan amounts of memory, it no longer keeps back-pointers
1498 * from a port set to a port. Therefore, we must iterate over all
1499 * ports within a given IPC space and individually query them to
1500 * see if they are members of the given set. Port names of ports
1501 * found to be members of the given set will be gathered into the
1502 * provided 'names' array. Actual returned names are limited to
1503 * maxnames entries, but we keep counting the actual number of
1504 * members to let the caller decide to retry if necessary.
1507 * Locks may be held by callers, so this routine cannot block.
1508 * Caller holds reference on the message queue (via port set).
1511 ipc_mqueue_set_gather_member_names(
1513 ipc_mqueue_t set_mq
,
1514 ipc_entry_num_t maxnames
,
1515 mach_port_name_t
*names
,
1516 ipc_entry_num_t
*actualp
)
1519 ipc_entry_num_t tsize
;
1520 struct waitq_set
*wqset
;
1521 ipc_entry_num_t actual
= 0;
1523 assert(set_mq
!= IMQ_NULL
);
1524 wqset
= &set_mq
->imq_set_queue
;
1526 assert(space
!= IS_NULL
);
1527 is_read_lock(space
);
1528 if (!is_active(space
)) {
1529 is_read_unlock(space
);
1533 if (!waitq_set_is_valid(wqset
)) {
1534 is_read_unlock(space
);
1538 table
= space
->is_table
;
1539 tsize
= space
->is_table_size
;
1540 for (ipc_entry_num_t idx
= 0; idx
< tsize
; idx
++) {
1541 ipc_entry_t entry
= &table
[idx
];
1543 /* only receive rights can be members of port sets */
1544 if ((entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) != MACH_PORT_TYPE_NONE
) {
1545 __IGNORE_WCASTALIGN(ipc_port_t port
= (ipc_port_t
)entry
->ie_object
);
1546 ipc_mqueue_t mq
= &port
->ip_messages
;
1548 assert(IP_VALID(port
));
1549 if (ip_active(port
) &&
1550 waitq_member(&mq
->imq_wait_queue
, wqset
)) {
1551 if (actual
< maxnames
)
1552 names
[actual
] = mq
->imq_receiver_name
;
1558 is_read_unlock(space
);
1566 * Routine: ipc_mqueue_destroy_locked
1568 * Destroy a (non-set) message queue.
1569 * Set any blocked senders running.
1570 * Destroy the kmsgs in the queue.
1573 * Receivers were removed when the receive right was "changed"
1576 ipc_mqueue_destroy_locked(ipc_mqueue_t mqueue
)
1578 ipc_kmsg_queue_t kmqueue
;
1580 boolean_t reap
= FALSE
;
1581 struct turnstile
*send_turnstile
= port_send_turnstile(ip_from_mq(mqueue
));
1583 assert(!imq_is_set(mqueue
));
1586 * rouse all blocked senders
1587 * (don't boost anyone - we're tearing this queue down)
1590 mqueue
->imq_fullwaiters
= FALSE
;
1592 if (send_turnstile
!= TURNSTILE_NULL
) {
1593 waitq_wakeup64_all(&send_turnstile
->ts_waitq
,
1596 WAITQ_ALL_PRIORITIES
);
1600 * Move messages from the specified queue to the per-thread
1601 * clean/drain queue while we have the mqueue lock.
1603 kmqueue
= &mqueue
->imq_messages
;
1604 while ((kmsg
= ipc_kmsg_dequeue(kmqueue
)) != IKM_NULL
) {
1606 if (MACH_NODE_VALID(kmsg
->ikm_node
) && FPORT_VALID(mqueue
->imq_fport
))
1607 flipc_msg_ack(kmsg
->ikm_node
, mqueue
, TRUE
);
1610 first
= ipc_kmsg_delayed_destroy(kmsg
);
1616 * Wipe out message count, both for messages about to be
1617 * reaped and for reserved space for (previously) woken senders.
1618 * This is the indication to them that their reserved space is gone
1619 * (the mqueue was destroyed).
1621 mqueue
->imq_msgcount
= 0;
1623 /* invalidate the waitq for subsequent mqueue operations */
1624 waitq_invalidate_locked(&mqueue
->imq_wait_queue
);
1626 /* clear out any preposting we may have done */
1627 waitq_clear_prepost_locked(&mqueue
->imq_wait_queue
);
1630 * assert that we are destroying / invalidating a queue that's
1631 * not a member of any other queue.
1633 assert(mqueue
->imq_preposts
== 0);
1634 assert(mqueue
->imq_in_pset
== 0);
1640 * Routine: ipc_mqueue_set_qlimit
1642 * Changes a message queue limit; the maximum number
1643 * of messages which may be queued.
1649 ipc_mqueue_set_qlimit(
1650 ipc_mqueue_t mqueue
,
1651 mach_port_msgcount_t qlimit
)
1654 assert(qlimit
<= MACH_PORT_QLIMIT_MAX
);
1656 /* wake up senders allowed by the new qlimit */
1658 if (qlimit
> mqueue
->imq_qlimit
) {
1659 mach_port_msgcount_t i
, wakeup
;
1660 struct turnstile
*send_turnstile
= port_send_turnstile(ip_from_mq(mqueue
));
1662 /* caution: wakeup, qlimit are unsigned */
1663 wakeup
= qlimit
- mqueue
->imq_qlimit
;
1665 for (i
= 0; i
< wakeup
; i
++) {
1667 * boost the priority of the awoken thread
1668 * (WAITQ_PROMOTE_PRIORITY) to ensure it uses
1669 * the message queue slot we've just reserved.
1671 * NOTE: this will never prepost
1673 if (send_turnstile
== TURNSTILE_NULL
||
1674 waitq_wakeup64_one(&send_turnstile
->ts_waitq
,
1677 WAITQ_PROMOTE_PRIORITY
) == KERN_NOT_WAITING
) {
1678 mqueue
->imq_fullwaiters
= FALSE
;
1681 mqueue
->imq_msgcount
++; /* give it to the awakened thread */
1684 mqueue
->imq_qlimit
= qlimit
;
1689 * Routine: ipc_mqueue_set_seqno
1691 * Changes an mqueue's sequence number.
1693 * Caller holds a reference to the queue's containing object.
1696 ipc_mqueue_set_seqno(
1697 ipc_mqueue_t mqueue
,
1698 mach_port_seqno_t seqno
)
1701 mqueue
->imq_seqno
= seqno
;
1707 * Routine: ipc_mqueue_copyin
1709 * Convert a name in a space to a message queue.
1711 * Nothing locked. If successful, the caller gets a ref for
1712 * for the object. This ref ensures the continued existence of
1715 * MACH_MSG_SUCCESS Found a message queue.
1716 * MACH_RCV_INVALID_NAME The space is dead.
1717 * MACH_RCV_INVALID_NAME The name doesn't denote a right.
1718 * MACH_RCV_INVALID_NAME
1719 * The denoted right is not receive or port set.
1720 * MACH_RCV_IN_SET Receive right is a member of a set.
1726 mach_port_name_t name
,
1727 ipc_mqueue_t
*mqueuep
,
1728 ipc_object_t
*objectp
)
1731 ipc_object_t object
;
1732 ipc_mqueue_t mqueue
;
1734 is_read_lock(space
);
1735 if (!is_active(space
)) {
1736 is_read_unlock(space
);
1737 return MACH_RCV_INVALID_NAME
;
1740 entry
= ipc_entry_lookup(space
, name
);
1741 if (entry
== IE_NULL
) {
1742 is_read_unlock(space
);
1743 return MACH_RCV_INVALID_NAME
;
1746 object
= entry
->ie_object
;
1748 if (entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) {
1751 __IGNORE_WCASTALIGN(port
= (ipc_port_t
) object
);
1752 assert(port
!= IP_NULL
);
1755 assert(ip_active(port
));
1756 assert(port
->ip_receiver_name
== name
);
1757 assert(port
->ip_receiver
== space
);
1758 is_read_unlock(space
);
1759 mqueue
= &port
->ip_messages
;
1761 } else if (entry
->ie_bits
& MACH_PORT_TYPE_PORT_SET
) {
1764 __IGNORE_WCASTALIGN(pset
= (ipc_pset_t
) object
);
1765 assert(pset
!= IPS_NULL
);
1768 assert(ips_active(pset
));
1769 is_read_unlock(space
);
1771 mqueue
= &pset
->ips_messages
;
1773 is_read_unlock(space
);
1774 return MACH_RCV_INVALID_NAME
;
1778 * At this point, the object is locked and active,
1779 * the space is unlocked, and mqueue is initialized.
1782 io_reference(object
);
1787 return MACH_MSG_SUCCESS
;