]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_mqueue.c
xnu-517.7.7.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_mqueue.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_FREE_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: ipc/ipc_mqueue.c
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Functions to manipulate IPC message queues.
58 */
59
60 #include <mach/port.h>
61 #include <mach/message.h>
62 #include <mach/sync_policy.h>
63
64 #include <kern/assert.h>
65 #include <kern/counters.h>
66 #include <kern/sched_prim.h>
67 #include <kern/ipc_kobject.h>
68 #include <kern/misc_protos.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71 #include <kern/wait_queue.h>
72
73 #include <ipc/ipc_mqueue.h>
74 #include <ipc/ipc_kmsg.h>
75 #include <ipc/ipc_port.h>
76 #include <ipc/ipc_pset.h>
77 #include <ipc/ipc_space.h>
78
79 #include <ddb/tr.h>
80
81 int ipc_mqueue_full; /* address is event for queue space */
82 int ipc_mqueue_rcv; /* address is event for message arrival */
83
84 #define TR_ENABLE 0
85
86 /*
87 * Routine: ipc_mqueue_init
88 * Purpose:
89 * Initialize a newly-allocated message queue.
90 */
91 void
92 ipc_mqueue_init(
93 ipc_mqueue_t mqueue,
94 boolean_t is_set)
95 {
96 if (is_set) {
97 wait_queue_set_init(&mqueue->imq_set_queue, SYNC_POLICY_FIFO);
98 } else {
99 wait_queue_init(&mqueue->imq_wait_queue, SYNC_POLICY_FIFO);
100 ipc_kmsg_queue_init(&mqueue->imq_messages);
101 mqueue->imq_seqno = 0;
102 mqueue->imq_msgcount = 0;
103 mqueue->imq_qlimit = MACH_PORT_QLIMIT_DEFAULT;
104 mqueue->imq_fullwaiters = FALSE;
105 }
106 }
107
108 /*
109 * Routine: ipc_mqueue_member
110 * Purpose:
111 * Indicate whether the (port) mqueue is a member of
112 * this portset's mqueue. We do this by checking
113 * whether the portset mqueue's waitq is an member of
114 * the port's mqueue waitq.
115 * Conditions:
116 * the portset's mqueue is not already a member
117 * this may block while allocating linkage structures.
118 */
119
120 boolean_t
121 ipc_mqueue_member(
122 ipc_mqueue_t port_mqueue,
123 ipc_mqueue_t set_mqueue)
124 {
125 wait_queue_t port_waitq = &port_mqueue->imq_wait_queue;
126 wait_queue_t set_waitq = &set_mqueue->imq_wait_queue;
127
128 return (wait_queue_member(port_waitq, set_waitq));
129
130 }
131
132 /*
133 * Routine: ipc_mqueue_remove
134 * Purpose:
135 * Remove the association between the queue and the specified
136 * set message queue.
137 */
138
139 kern_return_t
140 ipc_mqueue_remove(
141 ipc_mqueue_t mqueue,
142 ipc_mqueue_t set_mqueue)
143 {
144 wait_queue_t mq_waitq = &mqueue->imq_wait_queue;
145 wait_queue_set_t set_waitq = &set_mqueue->imq_set_queue;
146
147 return wait_queue_unlink(mq_waitq, set_waitq);
148 }
149
150 /*
151 * Routine: ipc_mqueue_remove_from_all
152 * Purpose:
153 * Remove the mqueue from all the sets it is a member of
154 * Conditions:
155 * Nothing locked.
156 */
157 void
158 ipc_mqueue_remove_from_all(
159 ipc_mqueue_t mqueue)
160 {
161 wait_queue_t mq_waitq = &mqueue->imq_wait_queue;
162
163 wait_queue_unlink_all(mq_waitq);
164 return;
165 }
166
167 /*
168 * Routine: ipc_mqueue_remove_all
169 * Purpose:
170 * Remove all the member queues from the specified set.
171 * Conditions:
172 * Nothing locked.
173 */
174 void
175 ipc_mqueue_remove_all(
176 ipc_mqueue_t mqueue)
177 {
178 wait_queue_set_t mq_setq = &mqueue->imq_set_queue;
179
180 wait_queue_set_unlink_all(mq_setq);
181 return;
182 }
183
184
185 /*
186 * Routine: ipc_mqueue_add
187 * Purpose:
188 * Associate the portset's mqueue with the port's mqueue.
189 * This has to be done so that posting the port will wakeup
190 * a portset waiter. If there are waiters on the portset
191 * mqueue and messages on the port mqueue, try to match them
192 * up now.
193 * Conditions:
194 * May block.
195 */
196 kern_return_t
197 ipc_mqueue_add(
198 ipc_mqueue_t port_mqueue,
199 ipc_mqueue_t set_mqueue)
200 {
201 wait_queue_t port_waitq = &port_mqueue->imq_wait_queue;
202 wait_queue_set_t set_waitq = &set_mqueue->imq_set_queue;
203 ipc_kmsg_queue_t kmsgq;
204 ipc_kmsg_t kmsg, next;
205 kern_return_t kr;
206 spl_t s;
207
208 kr = wait_queue_link(port_waitq, set_waitq);
209 if (kr != KERN_SUCCESS)
210 return kr;
211
212 /*
213 * Now that the set has been added to the port, there may be
214 * messages queued on the port and threads waiting on the set
215 * waitq. Lets get them together.
216 */
217 s = splsched();
218 imq_lock(port_mqueue);
219 kmsgq = &port_mqueue->imq_messages;
220 for (kmsg = ipc_kmsg_queue_first(kmsgq);
221 kmsg != IKM_NULL;
222 kmsg = next) {
223 next = ipc_kmsg_queue_next(kmsgq, kmsg);
224
225 for (;;) {
226 thread_t th;
227
228 th = wait_queue_wakeup64_identity_locked(
229 port_waitq,
230 IPC_MQUEUE_RECEIVE,
231 THREAD_AWAKENED,
232 FALSE);
233 /* waitq/mqueue still locked, thread locked */
234
235 if (th == THREAD_NULL)
236 goto leave;
237
238 /*
239 * Found a receiver. see if they can handle the message
240 * correctly (the message is not too large for them, or
241 * they didn't care to be informed that the message was
242 * too large). If they can't handle it, take them off
243 * the list and let them go back and figure it out and
244 * just move onto the next.
245 */
246 if (th->ith_msize <
247 kmsg->ikm_header.msgh_size +
248 REQUESTED_TRAILER_SIZE(th->ith_option)) {
249 th->ith_state = MACH_RCV_TOO_LARGE;
250 th->ith_msize = kmsg->ikm_header.msgh_size;
251 if (th->ith_option & MACH_RCV_LARGE) {
252 /*
253 * let him go without message
254 */
255 th->ith_kmsg = IKM_NULL;
256 th->ith_seqno = 0;
257 thread_unlock(th);
258 continue; /* find another thread */
259 }
260 } else {
261 th->ith_state = MACH_MSG_SUCCESS;
262 }
263
264 /*
265 * This thread is going to take this message,
266 * so give it to him.
267 */
268 ipc_mqueue_release_msgcount(port_mqueue);
269 ipc_kmsg_rmqueue(kmsgq, kmsg);
270 th->ith_kmsg = kmsg;
271 th->ith_seqno = port_mqueue->imq_seqno++;
272 thread_unlock(th);
273 break; /* go to next message */
274 }
275
276 }
277 leave:
278 imq_unlock(port_mqueue);
279 splx(s);
280 return KERN_SUCCESS;
281 }
282
283 /*
284 * Routine: ipc_mqueue_changed
285 * Purpose:
286 * Wake up receivers waiting in a message queue.
287 * Conditions:
288 * The message queue is locked.
289 */
290
291 void
292 ipc_mqueue_changed(
293 ipc_mqueue_t mqueue)
294 {
295 wait_queue_wakeup64_all_locked(
296 &mqueue->imq_wait_queue,
297 IPC_MQUEUE_RECEIVE,
298 THREAD_RESTART,
299 FALSE); /* unlock waitq? */
300 }
301
302
303
304
305 /*
306 * Routine: ipc_mqueue_send
307 * Purpose:
308 * Send a message to a message queue. The message holds a reference
309 * for the destination port for this message queue in the
310 * msgh_remote_port field.
311 *
312 * If unsuccessful, the caller still has possession of
313 * the message and must do something with it. If successful,
314 * the message is queued, given to a receiver, or destroyed.
315 * Conditions:
316 * Nothing locked.
317 * Returns:
318 * MACH_MSG_SUCCESS The message was accepted.
319 * MACH_SEND_TIMED_OUT Caller still has message.
320 * MACH_SEND_INTERRUPTED Caller still has message.
321 */
322 mach_msg_return_t
323 ipc_mqueue_send(
324 ipc_mqueue_t mqueue,
325 ipc_kmsg_t kmsg,
326 mach_msg_option_t option,
327 mach_msg_timeout_t timeout)
328 {
329 int wresult;
330 spl_t s;
331
332 /*
333 * Don't block if:
334 * 1) We're under the queue limit.
335 * 2) Caller used the MACH_SEND_ALWAYS internal option.
336 * 3) Message is sent to a send-once right.
337 */
338 s = splsched();
339 imq_lock(mqueue);
340
341 if (!imq_full(mqueue) ||
342 (option & MACH_SEND_ALWAYS) ||
343 (MACH_MSGH_BITS_REMOTE(kmsg->ikm_header.msgh_bits) ==
344 MACH_MSG_TYPE_PORT_SEND_ONCE)) {
345 mqueue->imq_msgcount++;
346 imq_unlock(mqueue);
347 splx(s);
348 } else {
349 thread_t cur_thread = current_thread();
350
351 /*
352 * We have to wait for space to be granted to us.
353 */
354 if ((option & MACH_SEND_TIMEOUT) && (timeout == 0)) {
355 imq_unlock(mqueue);
356 splx(s);
357 return MACH_SEND_TIMED_OUT;
358 }
359 mqueue->imq_fullwaiters = TRUE;
360 thread_lock(cur_thread);
361 wresult = wait_queue_assert_wait64_locked(
362 &mqueue->imq_wait_queue,
363 IPC_MQUEUE_FULL,
364 THREAD_ABORTSAFE,
365 cur_thread);
366 thread_unlock(cur_thread);
367 imq_unlock(mqueue);
368 splx(s);
369
370 if (wresult == THREAD_WAITING) {
371 if (option & MACH_SEND_TIMEOUT) {
372 thread_set_timer(timeout, 1000*NSEC_PER_USEC);
373 wresult = thread_block(THREAD_CONTINUE_NULL);
374 if (wresult != THREAD_TIMED_OUT)
375 thread_cancel_timer();
376 } else {
377 wresult = thread_block(THREAD_CONTINUE_NULL);
378 }
379 counter(c_ipc_mqueue_send_block++);
380 }
381
382 switch (wresult) {
383 case THREAD_TIMED_OUT:
384 assert(option & MACH_SEND_TIMEOUT);
385 return MACH_SEND_TIMED_OUT;
386
387 case THREAD_AWAKENED:
388 /* we can proceed - inherited msgcount from waker */
389 break;
390
391 case THREAD_INTERRUPTED:
392 return MACH_SEND_INTERRUPTED;
393
394 case THREAD_RESTART:
395 default:
396 panic("ipc_mqueue_send");
397 }
398 }
399
400 ipc_mqueue_post(mqueue, kmsg);
401 return MACH_MSG_SUCCESS;
402 }
403
404 /*
405 * Routine: ipc_mqueue_release_msgcount
406 * Purpose:
407 * Release a message queue reference in the case where we
408 * found a waiter.
409 *
410 * Conditions:
411 * The message queue is locked
412 */
413 void
414 ipc_mqueue_release_msgcount(
415 ipc_mqueue_t mqueue)
416 {
417 assert(imq_held(mqueue));
418 assert(mqueue->imq_msgcount > 0);
419
420 mqueue->imq_msgcount--;
421 if (!imq_full(mqueue) && mqueue->imq_fullwaiters) {
422 if (wait_queue_wakeup64_one_locked(
423 &mqueue->imq_wait_queue,
424 IPC_MQUEUE_FULL,
425 THREAD_AWAKENED,
426 FALSE) != KERN_SUCCESS) {
427 mqueue->imq_fullwaiters = FALSE;
428 } else {
429 mqueue->imq_msgcount++; /* gave it away */
430 }
431 }
432 }
433
434 /*
435 * Routine: ipc_mqueue_post
436 * Purpose:
437 * Post a message to a waiting receiver or enqueue it. If a
438 * receiver is waiting, we can release our reserved space in
439 * the message queue.
440 *
441 * Conditions:
442 * If we need to queue, our space in the message queue is reserved.
443 */
444 void
445 ipc_mqueue_post(
446 register ipc_mqueue_t mqueue,
447 register ipc_kmsg_t kmsg)
448 {
449
450 spl_t s;
451
452 /*
453 * While the msg queue is locked, we have control of the
454 * kmsg, so the ref in it for the port is still good.
455 *
456 * Check for a receiver for the message.
457 */
458 s = splsched();
459 imq_lock(mqueue);
460 for (;;) {
461 wait_queue_t waitq = &mqueue->imq_wait_queue;
462 thread_t receiver;
463
464 receiver = wait_queue_wakeup64_identity_locked(
465 waitq,
466 IPC_MQUEUE_RECEIVE,
467 THREAD_AWAKENED,
468 FALSE);
469 /* waitq still locked, thread locked */
470
471 if (receiver == THREAD_NULL) {
472 /*
473 * no receivers; queue kmsg
474 */
475 assert(mqueue->imq_msgcount > 0);
476 ipc_kmsg_enqueue_macro(&mqueue->imq_messages, kmsg);
477 break;
478 }
479
480 /*
481 * We found a waiting thread.
482 * If the message is too large or the scatter list is too small
483 * the thread we wake up will get that as its status.
484 */
485 if (receiver->ith_msize <
486 (kmsg->ikm_header.msgh_size) +
487 REQUESTED_TRAILER_SIZE(receiver->ith_option)) {
488 receiver->ith_msize = kmsg->ikm_header.msgh_size;
489 receiver->ith_state = MACH_RCV_TOO_LARGE;
490 } else {
491 receiver->ith_state = MACH_MSG_SUCCESS;
492 }
493
494 /*
495 * If there is no problem with the upcoming receive, or the
496 * receiver thread didn't specifically ask for special too
497 * large error condition, go ahead and select it anyway.
498 */
499 if ((receiver->ith_state == MACH_MSG_SUCCESS) ||
500 !(receiver->ith_option & MACH_RCV_LARGE)) {
501
502 receiver->ith_kmsg = kmsg;
503 receiver->ith_seqno = mqueue->imq_seqno++;
504 thread_unlock(receiver);
505
506 /* we didn't need our reserved spot in the queue */
507 ipc_mqueue_release_msgcount(mqueue);
508 break;
509 }
510
511 /*
512 * Otherwise, this thread needs to be released to run
513 * and handle its error without getting the message. We
514 * need to go back and pick another one.
515 */
516 receiver->ith_kmsg = IKM_NULL;
517 receiver->ith_seqno = 0;
518 thread_unlock(receiver);
519 }
520
521 imq_unlock(mqueue);
522 splx(s);
523
524 current_task()->messages_sent++;
525 return;
526 }
527
528
529 kern_return_t
530 ipc_mqueue_receive_results(void)
531 {
532 thread_t self = current_thread();
533 mach_msg_option_t option = self->ith_option;
534 kern_return_t saved_wait_result = self->wait_result;
535 kern_return_t mr;
536
537 /*
538 * why did we wake up?
539 */
540 switch (saved_wait_result) {
541 case THREAD_TIMED_OUT:
542 self->ith_state = MACH_RCV_TIMED_OUT;
543 return;
544
545 case THREAD_INTERRUPTED:
546 if (option & MACH_RCV_TIMEOUT)
547 thread_cancel_timer();
548 self->ith_state = MACH_RCV_INTERRUPTED;
549 return;
550
551 case THREAD_RESTART:
552 /* something bad happened to the port/set */
553 if (option & MACH_RCV_TIMEOUT)
554 thread_cancel_timer();
555 self->ith_state = MACH_RCV_PORT_CHANGED;
556 return;
557
558 case THREAD_AWAKENED:
559 /*
560 * We do not need to go select a message, somebody
561 * handed us one (or a too-large indication).
562 */
563 if (option & MACH_RCV_TIMEOUT)
564 thread_cancel_timer();
565
566 mr = MACH_MSG_SUCCESS;
567
568 switch (self->ith_state) {
569 case MACH_RCV_SCATTER_SMALL:
570 case MACH_RCV_TOO_LARGE:
571 /*
572 * Somebody tried to give us a too large
573 * message. If we indicated that we cared,
574 * then they only gave us the indication,
575 * otherwise they gave us the indication
576 * AND the message anyway.
577 */
578 if (option & MACH_RCV_LARGE) {
579 return;
580 }
581
582 case MACH_MSG_SUCCESS:
583 return;
584
585 default:
586 panic("ipc_mqueue_receive_results: strange ith_state");
587 }
588
589 default:
590 panic("ipc_mqueue_receive_results: strange wait_result");
591 }
592 }
593
594 void
595 ipc_mqueue_receive_continue(void)
596 {
597 ipc_mqueue_receive_results();
598 mach_msg_receive_continue(); /* hard-coded for now */
599 }
600
601 /*
602 * Routine: ipc_mqueue_receive
603 * Purpose:
604 * Receive a message from a message queue.
605 *
606 * If continuation is non-zero, then we might discard
607 * our kernel stack when we block. We will continue
608 * after unblocking by executing continuation.
609 *
610 * If resume is true, then we are resuming a receive
611 * operation after a blocked receive discarded our stack.
612 * Conditions:
613 * Our caller must hold a reference for the port or port set
614 * to which this queue belongs, to keep the queue
615 * from being deallocated.
616 *
617 * The kmsg is returned with clean header fields
618 * and with the circular bit turned off.
619 * Returns:
620 * MACH_MSG_SUCCESS Message returned in kmsgp.
621 * MACH_RCV_TOO_LARGE Message size returned in kmsgp.
622 * MACH_RCV_TIMED_OUT No message obtained.
623 * MACH_RCV_INTERRUPTED No message obtained.
624 * MACH_RCV_PORT_DIED Port/set died; no message.
625 * MACH_RCV_PORT_CHANGED Port moved into set; no msg.
626 *
627 */
628
629 void
630 ipc_mqueue_receive(
631 ipc_mqueue_t mqueue,
632 mach_msg_option_t option,
633 mach_msg_size_t max_size,
634 mach_msg_timeout_t timeout,
635 int interruptible)
636 {
637 ipc_port_t port;
638 mach_msg_return_t mr, mr2;
639 ipc_kmsg_queue_t kmsgs;
640 wait_result_t wresult;
641 thread_t self;
642 ipc_kmsg_t *kmsgp;
643 mach_port_seqno_t *seqnop;
644 spl_t s;
645
646 s = splsched();
647 imq_lock(mqueue);
648
649 if (imq_is_set(mqueue)) {
650 wait_queue_link_t wql;
651 ipc_mqueue_t port_mq;
652 queue_t q;
653
654 q = &mqueue->imq_setlinks;
655
656 /*
657 * If we are waiting on a portset mqueue, we need to see if
658 * any of the member ports have work for us. If so, try to
659 * deliver one of those messages. By holding the portset's
660 * mqueue lock during the search, we tie up any attempts by
661 * mqueue_deliver or portset membership changes that may
662 * cross our path. But this is a lock order violation, so we
663 * have to do it "softly." If we don't find a message waiting
664 * for us, we will assert our intention to wait while still
665 * holding that lock. When we release the lock, the deliver/
666 * change will succeed and find us.
667 */
668 search_set:
669 queue_iterate(q, wql, wait_queue_link_t, wql_setlinks) {
670 port_mq = (ipc_mqueue_t)wql->wql_queue;
671 kmsgs = &port_mq->imq_messages;
672
673 if (!imq_lock_try(port_mq)) {
674 imq_unlock(mqueue);
675 splx(s);
676 delay(1);
677 s = splsched();
678 imq_lock(mqueue);
679 goto search_set; /* start again at beginning - SMP */
680 }
681
682 /*
683 * If there is still a message to be had, we will
684 * try to select it (may not succeed because of size
685 * and options). In any case, we deliver those
686 * results back to the user.
687 *
688 * We also move the port's linkage to the tail of the
689 * list for this set (fairness). Future versions will
690 * sort by timestamp or priority.
691 */
692 if (ipc_kmsg_queue_first(kmsgs) == IKM_NULL) {
693 imq_unlock(port_mq);
694 continue;
695 }
696 queue_remove(q, wql, wait_queue_link_t, wql_setlinks);
697 queue_enter(q, wql, wait_queue_link_t, wql_setlinks);
698 imq_unlock(mqueue);
699
700 ipc_mqueue_select(port_mq, option, max_size);
701 imq_unlock(port_mq);
702 splx(s);
703 return;
704
705 }
706
707 } else {
708
709 /*
710 * Receive on a single port. Just try to get the messages.
711 */
712 kmsgs = &mqueue->imq_messages;
713 if (ipc_kmsg_queue_first(kmsgs) != IKM_NULL) {
714 ipc_mqueue_select(mqueue, option, max_size);
715 imq_unlock(mqueue);
716 splx(s);
717 return;
718 }
719 }
720
721 /*
722 * Looks like we'll have to block. The mqueue we will
723 * block on (whether the set's or the local port's) is
724 * still locked.
725 */
726 self = current_thread();
727 if (option & MACH_RCV_TIMEOUT) {
728 if (timeout == 0) {
729 imq_unlock(mqueue);
730 splx(s);
731 self->ith_state = MACH_RCV_TIMED_OUT;
732 return;
733 }
734 }
735
736 thread_lock(self);
737 self->ith_state = MACH_RCV_IN_PROGRESS;
738 self->ith_option = option;
739 self->ith_msize = max_size;
740
741 wresult = wait_queue_assert_wait64_locked(&mqueue->imq_wait_queue,
742 IPC_MQUEUE_RECEIVE,
743 interruptible,
744 self);
745 thread_unlock(self);
746 imq_unlock(mqueue);
747 splx(s);
748
749 if (wresult == THREAD_WAITING) {
750 if (option & MACH_RCV_TIMEOUT)
751 thread_set_timer(timeout, 1000*NSEC_PER_USEC);
752
753 if (interruptible == THREAD_ABORTSAFE)
754 counter(c_ipc_mqueue_receive_block_user++);
755 else
756 counter(c_ipc_mqueue_receive_block_kernel++);
757
758 if (self->ith_continuation)
759 thread_block(ipc_mqueue_receive_continue);
760 /* NOTREACHED */
761
762 thread_block(THREAD_CONTINUE_NULL);
763 }
764 ipc_mqueue_receive_results();
765 }
766
767
768 /*
769 * Routine: ipc_mqueue_select
770 * Purpose:
771 * A receiver discovered that there was a message on the queue
772 * before he had to block. Pick the message off the queue and
773 * "post" it to himself.
774 * Conditions:
775 * mqueue locked.
776 * There is a message.
777 * Returns:
778 * MACH_MSG_SUCCESS Actually selected a message for ourselves.
779 * MACH_RCV_TOO_LARGE May or may not have pull it, but it is large
780 */
781 void
782 ipc_mqueue_select(
783 ipc_mqueue_t mqueue,
784 mach_msg_option_t option,
785 mach_msg_size_t max_size)
786 {
787 thread_t self = current_thread();
788 ipc_kmsg_t kmsg;
789 mach_port_seqno_t seqno;
790 mach_msg_return_t mr;
791
792 mr = MACH_MSG_SUCCESS;
793
794
795 /*
796 * Do some sanity checking of our ability to receive
797 * before pulling the message off the queue.
798 */
799 kmsg = ipc_kmsg_queue_first(&mqueue->imq_messages);
800
801 assert(kmsg != IKM_NULL);
802
803 if (kmsg->ikm_header.msgh_size +
804 REQUESTED_TRAILER_SIZE(option) > max_size) {
805 mr = MACH_RCV_TOO_LARGE;
806 }
807
808 /*
809 * If we really can't receive it, but we had the
810 * MACH_RCV_LARGE option set, then don't take it off
811 * the queue, instead return the appropriate error
812 * (and size needed).
813 */
814 if ((mr == MACH_RCV_TOO_LARGE) && (option & MACH_RCV_LARGE)) {
815 self->ith_kmsg = IKM_NULL;
816 self->ith_msize = kmsg->ikm_header.msgh_size;
817 self->ith_seqno = 0;
818 self->ith_state = mr;
819 return;
820 }
821
822 ipc_kmsg_rmqueue_first_macro(&mqueue->imq_messages, kmsg);
823 ipc_mqueue_release_msgcount(mqueue);
824 self->ith_seqno = mqueue->imq_seqno++;
825 self->ith_kmsg = kmsg;
826 self->ith_state = mr;
827
828 current_task()->messages_received++;
829 return;
830 }
831
832 /*
833 * Routine: ipc_mqueue_destroy
834 * Purpose:
835 * Destroy a message queue. Set any blocked senders running.
836 * Destroy the kmsgs in the queue.
837 * Conditions:
838 * Nothing locked.
839 * Receivers were removed when the receive right was "changed"
840 */
841 void
842 ipc_mqueue_destroy(
843 ipc_mqueue_t mqueue)
844 {
845 ipc_kmsg_queue_t kmqueue;
846 ipc_kmsg_t kmsg;
847 spl_t s;
848
849
850 s = splsched();
851 imq_lock(mqueue);
852 /*
853 * rouse all blocked senders
854 */
855 mqueue->imq_fullwaiters = FALSE;
856 wait_queue_wakeup64_all_locked(
857 &mqueue->imq_wait_queue,
858 IPC_MQUEUE_FULL,
859 THREAD_AWAKENED,
860 FALSE);
861
862 kmqueue = &mqueue->imq_messages;
863
864 while ((kmsg = ipc_kmsg_dequeue(kmqueue)) != IKM_NULL) {
865 imq_unlock(mqueue);
866 splx(s);
867
868 ipc_kmsg_destroy_dest(kmsg);
869
870 s = splsched();
871 imq_lock(mqueue);
872 }
873 imq_unlock(mqueue);
874 splx(s);
875 }
876
877 /*
878 * Routine: ipc_mqueue_set_qlimit
879 * Purpose:
880 * Changes a message queue limit; the maximum number
881 * of messages which may be queued.
882 * Conditions:
883 * Nothing locked.
884 */
885
886 void
887 ipc_mqueue_set_qlimit(
888 ipc_mqueue_t mqueue,
889 mach_port_msgcount_t qlimit)
890 {
891 spl_t s;
892
893 /* wake up senders allowed by the new qlimit */
894 s = splsched();
895 imq_lock(mqueue);
896 if (qlimit > mqueue->imq_qlimit) {
897 mach_port_msgcount_t i, wakeup;
898
899 /* caution: wakeup, qlimit are unsigned */
900 wakeup = qlimit - mqueue->imq_qlimit;
901
902 for (i = 0; i < wakeup; i++) {
903 if (wait_queue_wakeup64_one_locked(
904 &mqueue->imq_wait_queue,
905 IPC_MQUEUE_FULL,
906 THREAD_AWAKENED,
907 FALSE) == KERN_NOT_WAITING) {
908 mqueue->imq_fullwaiters = FALSE;
909 break;
910 }
911 }
912 }
913 mqueue->imq_qlimit = qlimit;
914 imq_unlock(mqueue);
915 splx(s);
916 }
917
918 /*
919 * Routine: ipc_mqueue_set_seqno
920 * Purpose:
921 * Changes an mqueue's sequence number.
922 * Conditions:
923 * Caller holds a reference to the queue's containing object.
924 */
925 void
926 ipc_mqueue_set_seqno(
927 ipc_mqueue_t mqueue,
928 mach_port_seqno_t seqno)
929 {
930 spl_t s;
931
932 s = splsched();
933 imq_lock(mqueue);
934 mqueue->imq_seqno = seqno;
935 imq_unlock(mqueue);
936 splx(s);
937 }
938
939
940 /*
941 * Routine: ipc_mqueue_copyin
942 * Purpose:
943 * Convert a name in a space to a message queue.
944 * Conditions:
945 * Nothing locked. If successful, the caller gets a ref for
946 * for the object. This ref ensures the continued existence of
947 * the queue.
948 * Returns:
949 * MACH_MSG_SUCCESS Found a message queue.
950 * MACH_RCV_INVALID_NAME The space is dead.
951 * MACH_RCV_INVALID_NAME The name doesn't denote a right.
952 * MACH_RCV_INVALID_NAME
953 * The denoted right is not receive or port set.
954 * MACH_RCV_IN_SET Receive right is a member of a set.
955 */
956
957 mach_msg_return_t
958 ipc_mqueue_copyin(
959 ipc_space_t space,
960 mach_port_name_t name,
961 ipc_mqueue_t *mqueuep,
962 ipc_object_t *objectp)
963 {
964 ipc_entry_t entry;
965 ipc_object_t object;
966 ipc_mqueue_t mqueue;
967
968 is_read_lock(space);
969 if (!space->is_active) {
970 is_read_unlock(space);
971 return MACH_RCV_INVALID_NAME;
972 }
973
974 entry = ipc_entry_lookup(space, name);
975 if (entry == IE_NULL) {
976 is_read_unlock(space);
977 return MACH_RCV_INVALID_NAME;
978 }
979
980 object = entry->ie_object;
981
982 if (entry->ie_bits & MACH_PORT_TYPE_RECEIVE) {
983 ipc_port_t port;
984 ipc_pset_t pset;
985
986 port = (ipc_port_t) object;
987 assert(port != IP_NULL);
988
989 ip_lock(port);
990 assert(ip_active(port));
991 assert(port->ip_receiver_name == name);
992 assert(port->ip_receiver == space);
993 is_read_unlock(space);
994 mqueue = &port->ip_messages;
995
996 } else if (entry->ie_bits & MACH_PORT_TYPE_PORT_SET) {
997 ipc_pset_t pset;
998
999 pset = (ipc_pset_t) object;
1000 assert(pset != IPS_NULL);
1001
1002 ips_lock(pset);
1003 assert(ips_active(pset));
1004 assert(pset->ips_local_name == name);
1005 is_read_unlock(space);
1006
1007 mqueue = &pset->ips_messages;
1008 } else {
1009 is_read_unlock(space);
1010 return MACH_RCV_INVALID_NAME;
1011 }
1012
1013 /*
1014 * At this point, the object is locked and active,
1015 * the space is unlocked, and mqueue is initialized.
1016 */
1017
1018 io_reference(object);
1019 io_unlock(object);
1020
1021 *objectp = object;
1022 *mqueuep = mqueue;
1023 return MACH_MSG_SUCCESS;
1024 }
1025