Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / kern / lwkt_msgport.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * NOTE! This file may be compiled for userland libraries as well as for
35  * the kernel.
36  *
37  * $DragonFly: src/sys/kern/lwkt_msgport.c,v 1.35 2006/11/07 18:50:06 dillon Exp $
38  */
39
40 #ifdef _KERNEL
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/rtprio.h>
47 #include <sys/queue.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
50 #include <sys/signalvar.h>
51 #include <machine/cpu.h>
52 #include <sys/lock.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_pager.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_zone.h>
63
64 #include <sys/thread2.h>
65 #include <sys/msgport2.h>
66
67 #include <machine/stdarg.h>
68 #include <machine/cpufunc.h>
69 #ifdef SMP
70 #include <machine/smp.h>
71 #endif
72
73 #include <sys/malloc.h>
74 MALLOC_DEFINE(M_LWKTMSG, "lwkt message", "lwkt message");
75
76 #else
77
78 #include <sys/stdint.h>
79 #include <libcaps/thread.h>
80 #include <sys/thread.h>
81 #include <sys/msgport.h>
82 #include <sys/errno.h>
83 #include <libcaps/globaldata.h>
84 #include <machine/cpufunc.h>
85 #include <sys/thread2.h>
86 #include <sys/msgport2.h>
87 #include <string.h>
88
89 #endif /* _KERNEL */
90
91
92 /************************************************************************
93  *                              MESSAGE FUNCTIONS                       *
94  ************************************************************************/
95
96 #ifdef SMP
97 static void lwkt_replyport_remote(lwkt_msg_t msg);
98 static void lwkt_putport_remote(lwkt_msg_t msg);
99 static void lwkt_abortmsg_remote(lwkt_msg_t msg);
100 #endif
101
102 /*
103  * lwkt_sendmsg()
104  *
105  *      Send a message asynchronously.  This function requests asynchronous
106  *      completion and calls lwkt_beginmsg().  If the target port decides to
107  *      run the message synchronously this function will automatically queue
108  *      the message to the current thread's message queue to present a
109  *      consistent interface to the caller. 
110  *
111  *      The message's ms_cmd must be initialized and its ms_flags must
112  *      be zero'd out.  lwkt_sendmsg() will initialize the ms_abort_port
113  *      (abort chasing port).  If abort is supported, ms_abort must also be
114  *      initialized.
115  *
116  *      NOTE: you cannot safely request an abort until lwkt_sendmsg() returns
117  *      to the caller.
118  *
119  *      NOTE: MSGF_DONE is left set.  The target port must clear it if the
120  *      message is to be handled asynchronously, while the synchronous case
121  *      can just ignore it.
122  */
123 void
124 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
125 {
126     int error;
127
128     msg->ms_flags |= MSGF_ASYNC;
129     msg->ms_flags &= ~(MSGF_REPLY1 | MSGF_REPLY2 | MSGF_QUEUED | \
130                         MSGF_ABORTED | MSGF_RETRIEVED);
131     KKASSERT(msg->ms_reply_port != NULL);
132     msg->ms_abort_port = msg->ms_reply_port;
133     if ((error = lwkt_beginmsg(port, msg)) != EASYNC) {
134         lwkt_replymsg(msg, error);
135     }
136 }
137
138 /*
139  * lwkt_domsg()
140  *
141  *      Send a message synchronously.  This function requests synchronous
142  *      completion and calls lwkt_beginmsg().  If the target port decides to
143  *      run the message asynchronously this function will block waiting for
144  *      the message to complete.  Since MSGF_ASYNC is not set the target
145  *      will not attempt to queue the reply to a reply port but will simply
146  *      wake up anyone waiting on the message.
147  *
148  *      A synchronous error code is always returned.
149  *
150  *      The message's ms_cmd must be initialized, and its ms_flags must be
151  *      at least zero'd out.  lwkt_domsg() will initialize the message's
152  *      ms_abort_port (abort chasing port).  If abort is supported, ms_abort
153  *      must also be initialized.
154  *
155  *      NOTE: you cannot safely request an abort until lwkt_domsg() blocks.
156  *      XXX this probably needs some work.
157  *
158  *      NOTE: MSGF_DONE is left set.  The target port must clear it if the
159  *      message is to be handled asynchronously, while the synchronous case
160  *      can just ignore it.
161  */
162 int
163 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg)
164 {
165     int error;
166
167     msg->ms_flags &= ~(MSGF_ASYNC | MSGF_REPLY1 | MSGF_REPLY2 | \
168                         MSGF_QUEUED | MSGF_ABORTED | MSGF_RETRIEVED);
169     KKASSERT(msg->ms_reply_port != NULL);
170     msg->ms_abort_port = msg->ms_reply_port;
171     if ((error = lwkt_beginmsg(port, msg)) == EASYNC) {
172         error = lwkt_waitmsg(msg);
173     }
174     return(error);
175 }
176
177 /************************************************************************
178  *                              PORT FUNCTIONS                          *
179  ************************************************************************/
180
181 /*
182  * lwkt_initport()
183  *
184  *      Initialize a port for use and assign it to the specified thread.
185  *      The default reply function is to return the message to the originator.
186  */
187 void
188 lwkt_initport(lwkt_port_t port, thread_t td)
189 {
190     bzero(port, sizeof(*port));
191     TAILQ_INIT(&port->mp_msgq);
192     port->mp_td = td;
193     port->mp_putport = lwkt_default_putport;
194     port->mp_waitport =  lwkt_default_waitport;
195     port->mp_replyport = lwkt_default_replyport;
196     port->mp_abortport = lwkt_default_abortport;
197 }
198
199 /*
200  * Similar to the standard initport, this function simply marks the message
201  * as being done and does not attempt to return it to an originating port.
202  */
203 void
204 lwkt_initport_null_rport(lwkt_port_t port, thread_t td)
205 {
206     lwkt_initport(port, td);
207     port->mp_replyport = lwkt_null_replyport;
208 }
209
210 /*
211  * lwkt_getport()
212  *
213  *      Retrieve the next message from the port's message queue, return NULL
214  *      if no messages are pending.  Note that callers CANNOT use the
215  *      MSGF_ABORTED flag as a litmus test to determine if a message
216  *      was aborted.  The flag only indicates that an abort was requested.
217  *      The message's error code will indicate whether an abort occured
218  *      (typically by returning EINTR).
219  *
220  *      Note that once a message has been dequeued it is subject to being
221  *      requeued via an IPI based abort request if it is not marked MSGF_DONE.
222  *
223  *      If the message has been aborted we have to guarentee that abort 
224  *      semantics are properly followed.   The target port will always see
225  *      the original message at least once, and if it does not reply the 
226  *      message before looping on its message port again it will then see
227  *      the message again with ms_cmd set to ms_abort.
228  *
229  *      The calling thread MUST own the port.
230  */
231
232 static __inline
233 void
234 _lwkt_pullmsg(lwkt_port_t port, lwkt_msg_t msg)
235 {
236     if ((msg->ms_flags & MSGF_ABORTED) == 0) {
237         /*
238          * normal case, remove and return the message.
239          */
240         TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
241         msg->ms_flags = (msg->ms_flags & ~MSGF_QUEUED) | MSGF_RETRIEVED;
242     } else {
243         if (msg->ms_flags & MSGF_RETRIEVED) {
244             /*
245              * abort case, message already returned once, remvoe and
246              * return the aborted message a second time after setting
247              * ms_cmd to ms_abort.
248              */
249             TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
250             msg->ms_flags &= ~MSGF_QUEUED;
251             msg->ms_cmd = msg->ms_abort;
252         } else {
253             /*
254              * abort case, abort races initial message retrieval.  The
255              * message is returned normally but not removed from the 
256              * queue.  On the next loop the 'aborted' message will be
257              * dequeued and returned.  Note that if the caller replies
258              * to the message it will be dequeued (the abort becomes a
259              * NOP).
260              */
261             msg->ms_flags |= MSGF_RETRIEVED;
262         }
263     }
264 }
265
266 void *
267 lwkt_getport(lwkt_port_t port)
268 {
269     lwkt_msg_t msg;
270
271     KKASSERT(port->mp_td == curthread);
272
273     crit_enter_quick(port->mp_td);
274     if ((msg = TAILQ_FIRST(&port->mp_msgq)) != NULL)
275         _lwkt_pullmsg(port, msg);
276     crit_exit_quick(port->mp_td);
277     return(msg);
278 }
279
280 /*
281  * This inline helper function completes processing of a reply from an
282  * unknown cpu context.
283  *
284  * The message is being returned to the specified port.  The port is
285  * owned by the mp_td thread.  If we are on the same cpu as the mp_td
286  * thread we can trivially queue the message to the reply port and schedule
287  * the target thread, otherwise we have to send an ipi message to the
288  * correct cpu.
289  *
290  * This inline must be entered with a critical section already held.
291  * Note that the IPIQ callback function (*_remote) is entered with a
292  * critical section already held, and we obtain one in lwkt_replyport().
293  */
294 static __inline
295 void
296 _lwkt_replyport(lwkt_port_t port, lwkt_msg_t msg, int force)
297 {
298     thread_t td = port->mp_td;
299
300     if (force || td->td_gd == mycpu) {
301         /*
302          * We can only reply the message if the abort has caught up with us,
303          * or if no abort was issued (same case).
304          */
305         if (msg->ms_abort_port == port) {
306             KKASSERT((msg->ms_flags & MSGF_QUEUED) == 0);
307             TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
308             msg->ms_flags |= MSGF_DONE | MSGF_QUEUED | MSGF_REPLY2;
309             if (port->mp_flags & MSGPORTF_WAITING)
310                 lwkt_schedule(td);
311         } 
312     } else {
313 #ifdef SMP
314         lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_replyport_remote, msg);
315 #else
316         panic("lwkt_replyport: thread %p has bad gd pointer", td);
317 #endif
318     }
319 }
320
321 #ifdef SMP
322
323 /*
324  * This function completes reply processing for the default case in the
325  * context of the originating cpu.
326  */
327 static
328 void
329 lwkt_replyport_remote(lwkt_msg_t msg)
330 {
331     _lwkt_replyport(msg->ms_reply_port, msg, 1);
332 }
333
334 #endif
335
336 /*
337  * This function is called in the context of the target to reply a message.
338  * The critical section protects us from IPIs on the this CPU.
339  */
340 void
341 lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg)
342 {
343     crit_enter();
344     msg->ms_flags |= MSGF_REPLY1;
345
346     /*
347      * An abort may have caught up to us while we were processing the
348      * message.  If this occured we have to dequeue the message from the
349      * target port in the context of our current cpu before we can finish
350      * replying it.
351      */
352     if (msg->ms_flags & MSGF_QUEUED) {
353         KKASSERT(msg->ms_flags & MSGF_ABORTED);
354         TAILQ_REMOVE(&msg->ms_target_port->mp_msgq, msg, ms_node);
355         msg->ms_flags &= ~MSGF_QUEUED;
356     }
357
358     /*
359      * Do reply port processing for async messages.  Just mark the message
360      * done and wakeup the owner of the reply port for synchronous messages.
361      */
362     if (msg->ms_flags & MSGF_ASYNC) {
363         _lwkt_replyport(port, msg, 0);
364     } else {
365         msg->ms_flags |= MSGF_DONE;
366         if (port->mp_flags & MSGPORTF_WAITING)
367             lwkt_schedule(port->mp_td);
368     }
369     crit_exit();
370 }
371
372 /*
373  * You can point a port's reply vector at this function if you just want
374  * the message marked done, without any queueing or signaling.  This is
375  * often used for structure-embedded messages.
376  */
377 void
378 lwkt_null_replyport(lwkt_port_t port, lwkt_msg_t msg)
379 {
380     crit_enter();
381     msg->ms_flags |= MSGF_DONE|MSGF_REPLY1;
382     crit_exit();
383 }
384
385 /*
386  * lwkt_default_putport()
387  *
388  *      This function is typically assigned to the mp_putport port vector.
389  *
390  *      Queue a message to the target port and wakeup the thread owning it.
391  *      This function always returns EASYNC and may be assigned to a
392  *      message port's mp_putport function vector.  Note that we must set
393  *      MSGF_QUEUED prior to sending any IPIs in order to interlock against
394  *      ABORT requests and other tests that might be performed.
395  *
396  *      Note that messages start out as synchronous entities, and as an
397  *      optimization MSGF_DONE is usually left set (so in the synchronous path
398  *      no modifications to ms_flags are ever required).  If a message becomes
399  *      async, i.e. you return EASYNC, then MSGF_DONE must be cleared or
400  *      lwkt_replymsg() will wind up being a NOP.
401  *
402  *      The inline must be called from a critical section (the remote function
403  *      is called from an IPI and will be in a critical section).
404  */
405 static
406 __inline
407 void
408 _lwkt_putport(lwkt_port_t port, lwkt_msg_t msg, int force)
409 {
410     thread_t td = port->mp_td;
411
412     if (force || td->td_gd == mycpu) {
413         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
414         if (port->mp_flags & MSGPORTF_WAITING)
415             lwkt_schedule(td);
416     } else {
417 #ifdef SMP
418         lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_putport_remote, msg);
419 #else
420         panic("lwkt_putport: thread %p has bad gd pointer", td);
421 #endif
422     }
423 }
424
425 #ifdef SMP
426
427 static
428 void
429 lwkt_putport_remote(lwkt_msg_t msg)
430 {
431 #ifdef INVARIANTS
432     /*
433      * try to catch a free-after-send issue.
434      */
435     if (msg->ms_target_port == (void *)0xdeadc0de) {
436         int i;
437         for (i = 0; i < 1000000; ++i) {
438                 if (msg->ms_target_port != (void *)0xdeadc0de)
439                         break;
440                 cpu_lfence();
441         }
442         panic("msg %p ms_target_port is bogus: reads %p after %d loops\n", msg, msg->ms_target_port, i);
443     }
444 #endif
445     _lwkt_putport(msg->ms_target_port, msg, 1);
446 }
447
448 #endif
449
450 int
451 lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg)
452 {
453     crit_enter();
454     msg->ms_flags |= MSGF_QUEUED;       /* abort interlock */
455     msg->ms_flags &= ~MSGF_DONE;
456     msg->ms_target_port = port;
457     _lwkt_putport(port, msg, 0);
458     crit_exit();
459     return(EASYNC);
460 }
461
462 /*
463  * lwkt_forwardmsg()
464  *
465  * Forward a message received on one port to another port.  The forwarding
466  * function must deal with a pending abort but othewise essentially just
467  * issues a putport to the target port.
468  *
469  * An abort may have two side effects:  First, the message may have been
470  * requeued to the current target port.  If so, we must dequeue it before
471  * we can forward it.
472  */
473 int
474 lwkt_forwardmsg(lwkt_port_t port, lwkt_msg_t msg)
475 {   
476     int error;
477
478     crit_enter();
479     if (msg->ms_flags & MSGF_QUEUED) {
480         KKASSERT(msg->ms_flags & MSGF_ABORTED);
481         TAILQ_REMOVE(&msg->ms_target_port->mp_msgq, msg, ms_node);
482         msg->ms_flags &= ~MSGF_QUEUED;
483     }
484     msg->ms_flags &= ~MSGF_RETRIEVED;
485     if ((error = port->mp_putport(port, msg)) != EASYNC)
486         lwkt_replymsg(msg, error);
487     crit_exit();
488     return(error);
489 }
490
491 /*
492  * lwkt_abortmsg()
493  *
494  *      Aborting a message is a fairly complex task.  The first order of
495  *      business is to get the message to the cpu that owns the target
496  *      port, during which we may have to do some port chasing due to 
497  *      message forwarding operations.
498  *
499  *      NOTE!  Since an aborted message is requeued all message processing
500  *      loops should check the MSGF_ABORTED flag.
501  */
502
503 void
504 lwkt_abortmsg(lwkt_msg_t msg)
505 {
506     lwkt_port_t port;
507     thread_t td;
508
509     /*
510      * A critical section protects us from reply IPIs on this cpu.   We 
511      * can only abort messages that have not yet completed (DONE), are not
512      * in the midst of being replied (REPLY1), and which support the
513      * abort function (ABORTABLE).
514      */
515     crit_enter();
516     if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY1|MSGF_ABORTABLE)) == MSGF_ABORTABLE) {
517         /*
518          * Chase the message.  If REPLY1 is set the message has been replied
519          * all the way back to the originator, otherwise it is sitting on
520          * ms_target_port (but we can only complete processing if we are
521          * on the same cpu as the selected port in order to avoid
522          * SMP cache synchronization issues).
523          *
524          * When chasing through multiple ports ms_flags may not be 
525          * synchronized to the current cpu, but it WILL be synchronized
526          * with regards to testing the MSGF_REPLY1 bit once we reach the
527          * target port that made the reply and since the cpu owning
528          * some port X stores the new port in ms_target_port if the message
529          * is forwarded, the current port will only ever equal the target
530          * port when we are on the correct cpu.
531          */
532         if (msg->ms_flags & MSGF_REPLY1)
533             port = msg->ms_reply_port;
534         else
535             port = msg->ms_target_port;
536
537         cpu_ccfence();  /* don't let the compiler reload ms_*_port */
538
539         /*
540          * The chase call must run on the cpu owning the port.  Fully
541          * synchronous ports (mp_td == NULL) can run the call on any cpu.
542          */
543         td = port->mp_td;
544         if (td && td->td_gd != mycpu) {
545 #ifdef SMP
546             lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_abortmsg_remote, msg);
547 #else
548             panic("lwkt_abortmsg: thread %p has bad gd pointer", td);
549 #endif
550         } else {
551             port->mp_abortport(port, msg);
552         }
553     }
554     crit_exit();
555 }
556
557 #ifdef SMP
558
559 static
560 void
561 lwkt_abortmsg_remote(lwkt_msg_t msg)
562 {
563     lwkt_port_t port;
564     thread_t td;
565
566     if (msg->ms_flags & MSGF_REPLY1)
567         port = msg->ms_reply_port;
568     else
569         port = msg->ms_target_port;
570     cpu_ccfence();      /* don't let the compiler reload ms_*_port */
571     td = port->mp_td;
572     if (td->td_gd != mycpu) {
573         lwkt_send_ipiq(td->td_gd, (ipifunc1_t)lwkt_abortmsg_remote, msg);
574     } else {
575         port->mp_abortport(port, msg);
576     }
577 }
578
579 #endif
580
581 /*
582  * The mp_abortport function is called when the abort has finally caught up
583  * to the target port or (if the message has been replied) the reply port.
584  */
585 void
586 lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg)
587 {
588     /*
589      * Set ms_abort_port to ms_reply_port to indicate the completion of
590      * the messaging chasing portion of the abort request.  Note that
591      * the passed port is the port that we finally caught up to, not
592      * necessarily the reply port.
593      */
594     msg->ms_abort_port = msg->ms_reply_port;
595
596     if (msg->ms_flags & MSGF_REPLY2) {
597         /*
598          * If REPLY2 is set we must have chased it all the way back to
599          * the reply port, but the replyport code has not queued the message
600          * (because it was waiting for the abort to catch up).  We become
601          * responsible for queueing the message to the reply port.
602          */
603         KKASSERT((msg->ms_flags & MSGF_QUEUED) == 0);
604         KKASSERT(port == msg->ms_reply_port);
605         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
606         msg->ms_flags |= MSGF_DONE | MSGF_QUEUED;
607         if (port->mp_flags & MSGPORTF_WAITING)
608             lwkt_schedule(port->mp_td);
609     } else if ((msg->ms_flags & (MSGF_QUEUED|MSGF_REPLY1)) == 0) {
610         /*
611          * Abort on the target port.  The message has not yet been replied
612          * and must be requeued to the target port.
613          */
614         msg->ms_flags |= MSGF_ABORTED | MSGF_QUEUED;
615         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
616         if (port->mp_flags & MSGPORTF_WAITING)
617             lwkt_schedule(port->mp_td);
618     } else if ((msg->ms_flags & MSGF_REPLY1) == 0) {
619         /*
620          * The message has not yet been retrieved by the target port, set
621          * MSGF_ABORTED so the target port can requeue the message abort after
622          * retrieving it.
623          */
624         msg->ms_flags |= MSGF_ABORTED;
625     }
626 }
627
628 /*
629  * lwkt_default_waitport()
630  *
631  *      If msg is NULL, dequeue the next message from the port's message
632  *      queue, block until a message is ready.  This function never
633  *      returns NULL.
634  *
635  *      If msg is non-NULL, block until the requested message has been returned
636  *      to the port then dequeue and return it.  DO NOT USE THIS TO WAIT FOR
637  *      INCOMING REQUESTS, ONLY USE THIS TO WAIT FOR REPLIES.
638  *
639  *      Note that the API does not currently support multiple threads waiting
640  *      on a port.  By virtue of owning the port it is controlled by our
641  *      cpu and we can safely manipulate it's contents.
642  */
643 void *
644 lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg)
645 {
646     thread_t td = curthread;
647     int sentabort;
648
649     KKASSERT(port->mp_td == td);
650     crit_enter_quick(td);
651     if (msg == NULL) {
652         if ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL) {
653             port->mp_flags |= MSGPORTF_WAITING;
654             td->td_flags |= TDF_BLOCKED;
655             do {
656                 lwkt_deschedule_self(td);
657                 lwkt_switch();
658             } while ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL);
659             td->td_flags &= ~TDF_BLOCKED;
660             port->mp_flags &= ~MSGPORTF_WAITING;
661         }
662         _lwkt_pullmsg(port, msg);
663     } else {
664         /*
665          * If a message is not marked done, or if it is queued, we have work
666          * to do.  Note that MSGF_DONE is always set in the context of the
667          * reply port's cpu.
668          */
669         if ((msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) != MSGF_DONE) {
670             /*
671              * We must own the reply port to safely mess with it's contents.
672              */
673             port = msg->ms_reply_port;
674             KKASSERT(port->mp_td == td);
675
676             if ((msg->ms_flags & MSGF_DONE) == 0) {
677                 port->mp_flags |= MSGPORTF_WAITING; /* saved by the BGL */
678                 sentabort = 0;
679                 do {
680 #ifdef _KERNEL
681                     /*
682                      * MSGF_PCATCH is only set by processes which wish to
683                      * abort the message they are blocked on when a signal
684                      * occurs.  Note that we still must wait for message
685                      * completion after sending an abort request.
686                      */
687                     if (msg->ms_flags & MSGF_PCATCH) {
688                         if (sentabort == 0 && CURSIG(port->mp_td->td_proc)) {
689                             sentabort = 1;
690                             lwkt_abortmsg(msg);
691                             continue;
692                         }
693                     }
694 #endif
695                     /*
696                      * XXX set TDF_SINTR so 'ps' knows the difference between
697                      * an interruptable wait and a disk wait.  YYY eventually
698                      * move P_SINTR to TDF_SINTR to reduce duplication.
699                      */
700                     td->td_flags |= TDF_SINTR | TDF_BLOCKED;
701                     lwkt_deschedule_self(td);
702                     lwkt_switch();
703                     td->td_flags &= ~(TDF_SINTR | TDF_BLOCKED);
704                 } while ((msg->ms_flags & MSGF_DONE) == 0);
705                 port->mp_flags &= ~MSGPORTF_WAITING; /* saved by the BGL */
706             }
707             /*
708              * We own the message now.
709              */
710             if (msg->ms_flags & MSGF_QUEUED) {
711                 msg->ms_flags &= ~MSGF_QUEUED;
712                 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
713             }
714         }
715     }
716     crit_exit_quick(td);
717     return(msg);
718 }
719