Add missing va_end(ap);
[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.28 2004/07/16 05:51:10 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/ipl.h>
69 #include <machine/cpufunc.h>
70 #ifdef SMP
71 #include <machine/smp.h>
72 #endif
73
74 #include <sys/malloc.h>
75 MALLOC_DEFINE(M_LWKTMSG, "lwkt message", "lwkt message");
76
77 #else
78
79 #include <sys/stdint.h>
80 #include <libcaps/thread.h>
81 #include <sys/thread.h>
82 #include <sys/msgport.h>
83 #include <sys/errno.h>
84 #include <libcaps/globaldata.h>
85 #include <machine/cpufunc.h>
86 #include <sys/thread2.h>
87 #include <sys/msgport2.h>
88 #include <string.h>
89
90 #endif /* _KERNEL */
91
92
93 /************************************************************************
94  *                              MESSAGE FUNCTIONS                       *
95  ************************************************************************/
96
97 static void lwkt_replyport_remote(lwkt_msg_t msg);
98 static void lwkt_putport_remote(lwkt_msg_t msg);
99
100 /*
101  * lwkt_sendmsg()
102  *
103  *      Send a message asynchronously.  This function requests asynchronous
104  *      completion and calls lwkt_beginmsg().  If the target port decides to
105  *      run the message synchronously this function will automatically queue
106  *      the message to the current thread's message queue to present a
107  *      consistent interface to the caller. 
108  *
109  *      The message's ms_cmd must be initialized and its ms_flags must
110  *      be zero'd out.  lwkt_sendmsg() will initialize the ms_abort_port
111  *      (abort chasing port).  If abort is supported, ms_abort must also be
112  *      initialized.
113  *
114  *      NOTE: you cannot safely request an abort until lwkt_sendmsg() returns
115  *      to the caller.
116  *
117  *      NOTE: MSGF_DONE is left set.  The target port must clear it if the
118  *      message is to be handled asynchronously, while the synchronous case
119  *      can just ignore it.
120  */
121 void
122 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
123 {
124     int error;
125
126     msg->ms_flags |= MSGF_ASYNC;
127     msg->ms_flags &= ~(MSGF_REPLY1 | MSGF_REPLY2 | MSGF_QUEUED | \
128                         MSGF_ABORTED | MSGF_RETRIEVED);
129     KKASSERT(msg->ms_reply_port != NULL);
130     msg->ms_abort_port = msg->ms_reply_port;
131     if ((error = lwkt_beginmsg(port, msg)) != EASYNC) {
132         lwkt_replymsg(msg, error);
133     }
134 }
135
136 /*
137  * lwkt_domsg()
138  *
139  *      Send a message synchronously.  This function requests synchronous
140  *      completion and calls lwkt_beginmsg().  If the target port decides to
141  *      run the message asynchronously this function will block waiting for
142  *      the message to complete.  Since MSGF_ASYNC is not set the target
143  *      will not attempt to queue the reply to a reply port but will simply
144  *      wake up anyone waiting on the message.
145  *
146  *      A synchronous error code is always returned.
147  *
148  *      The message's ms_cmd must be initialized, and its ms_flags must be
149  *      at least zero'd out.  lwkt_domsg() will initialize the message's
150  *      ms_abort_port (abort chasing port).  If abort is supported, ms_abort
151  *      must also be initialized.
152  *
153  *      NOTE: you cannot safely request an abort until lwkt_domsg() blocks.
154  *      XXX this probably needs some work.
155  *
156  *      NOTE: MSGF_DONE is left set.  The target port must clear it if the
157  *      message is to be handled asynchronously, while the synchronous case
158  *      can just ignore it.
159  */
160 int
161 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg)
162 {
163     int error;
164
165     msg->ms_flags &= ~(MSGF_ASYNC | MSGF_REPLY1 | MSGF_REPLY2 | \
166                         MSGF_QUEUED | MSGF_ABORTED | MSGF_RETRIEVED);
167     KKASSERT(msg->ms_reply_port != NULL);
168     msg->ms_abort_port = msg->ms_reply_port;
169     if ((error = lwkt_beginmsg(port, msg)) == EASYNC) {
170         error = lwkt_waitmsg(msg);
171     }
172     return(error);
173 }
174
175 /************************************************************************
176  *                              PORT FUNCTIONS                          *
177  ************************************************************************/
178
179 /*
180  * lwkt_initport()
181  *
182  *      Initialize a port for use and assign it to the specified thread.
183  */
184 void
185 lwkt_initport(lwkt_port_t port, thread_t td)
186 {
187     bzero(port, sizeof(*port));
188     TAILQ_INIT(&port->mp_msgq);
189     port->mp_td = td;
190     port->mp_putport = lwkt_default_putport;
191     port->mp_waitport =  lwkt_default_waitport;
192     port->mp_replyport = lwkt_default_replyport;
193     port->mp_abortport = lwkt_default_abortport;
194 }
195
196 /*
197  * lwkt_getport()
198  *
199  *      Retrieve the next message from the port's message queue, return NULL
200  *      if no messages are pending.  Note that callers CANNOT use the
201  *      MSGF_ABORTED flag as a litmus test to determine if a message
202  *      was aborted.  The flag only indicates that an abort was requested.
203  *      The message's error code will indicate whether an abort occured
204  *      (typically by returning EINTR).
205  *
206  *      Note that once a message has been dequeued it is subject to being
207  *      requeued via an IPI based abort request if it is not marked MSGF_DONE.
208  *
209  *      If the message has been aborted we have to guarentee that abort 
210  *      semantics are properly followed.   The target port will always see
211  *      the original message at least once, and if it does not reply the 
212  *      message before looping on its message port again it will then see
213  *      the message again with ms_cmd set to ms_abort.
214  *
215  *      The calling thread MUST own the port.
216  */
217
218 static __inline
219 void
220 _lwkt_pullmsg(lwkt_port_t port, lwkt_msg_t msg)
221 {
222     if ((msg->ms_flags & MSGF_ABORTED) == 0) {
223         /*
224          * normal case, remove and return the message.
225          */
226         TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
227         msg->ms_flags = (msg->ms_flags & ~MSGF_QUEUED) | MSGF_RETRIEVED;
228     } else {
229         if (msg->ms_flags & MSGF_RETRIEVED) {
230             /*
231              * abort case, message already returned once, remvoe and
232              * return the aborted message a second time after setting
233              * ms_cmd to ms_abort.
234              */
235             TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
236             msg->ms_flags &= ~MSGF_QUEUED;
237             msg->ms_cmd = msg->ms_abort;
238         } else {
239             /*
240              * abort case, abort races initial message retrieval.  The
241              * message is returned normally but not removed from the 
242              * queue.  On the next loop the 'aborted' message will be
243              * dequeued and returned.  Note that if the caller replies
244              * to the message it will be dequeued (the abort becomes a
245              * NOP).
246              */
247             msg->ms_flags |= MSGF_RETRIEVED;
248         }
249     }
250 }
251
252 void *
253 lwkt_getport(lwkt_port_t port)
254 {
255     lwkt_msg_t msg;
256
257     KKASSERT(port->mp_td == curthread);
258
259     crit_enter_quick(port->mp_td);
260     if ((msg = TAILQ_FIRST(&port->mp_msgq)) != NULL)
261         _lwkt_pullmsg(port, msg);
262     crit_exit_quick(port->mp_td);
263     return(msg);
264 }
265
266 /*
267  * This inline helper function completes processing of a reply from an
268  * unknown cpu context.
269  *
270  * The message is being returned to the specified port.  The port is
271  * owned by the mp_td thread.  If we are on the same cpu as the mp_td
272  * thread we can trivially queue the message to the reply port and schedule
273  * the target thread, otherwise we have to send an ipi message to the
274  * correct cpu.
275  *
276  * This inline must be entered with a critical section already held.
277  * Note that the IPIQ callback function (*_remote) is entered with a
278  * critical section already held, and we obtain one in lwkt_replyport().
279  */
280 static __inline
281 void
282 _lwkt_replyport(lwkt_port_t port, lwkt_msg_t msg, int force)
283 {
284     thread_t td = port->mp_td;
285
286     if (force || td->td_gd == mycpu) {
287         /*
288          * We can only reply the message if the abort has caught up with us,
289          * or if no abort was issued (same case).
290          */
291         if (msg->ms_abort_port == port) {
292             KKASSERT((msg->ms_flags & MSGF_QUEUED) == 0);
293             TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
294             msg->ms_flags |= MSGF_DONE | MSGF_QUEUED | MSGF_REPLY2;
295             if (port->mp_flags & MSGPORTF_WAITING)
296                 lwkt_schedule(td);
297         } 
298     } else {
299         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_replyport_remote, msg);
300     }
301 }
302
303 /*
304  * This function completes reply processing for the default case in the
305  * context of the originating cpu.
306  */
307 static
308 void
309 lwkt_replyport_remote(lwkt_msg_t msg)
310 {
311     _lwkt_replyport(msg->ms_reply_port, msg, 1);
312 }
313
314 /*
315  * This function is called in the context of the target to reply a message.
316  * The critical section protects us from IPIs on the this CPU.
317  */
318
319 void
320 lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg)
321 {
322     crit_enter();
323     msg->ms_flags |= MSGF_REPLY1;
324
325     /*
326      * An abort may have caught up to us while we were processing the
327      * message.  If this occured we have to dequeue the message from the
328      * target port in the context of our current cpu before we can finish
329      * replying it.
330      */
331     if (msg->ms_flags & MSGF_QUEUED) {
332         KKASSERT(msg->ms_flags & MSGF_ABORTED);
333         TAILQ_REMOVE(&msg->ms_target_port->mp_msgq, msg, ms_node);
334         msg->ms_flags &= ~MSGF_QUEUED;
335     }
336
337     /*
338      * Do reply port processing for async messages.  Just mark the message
339      * done and wakeup the owner of the reply port for synchronous messages.
340      */
341     if (msg->ms_flags & MSGF_ASYNC) {
342         _lwkt_replyport(port, msg, 0);
343     } else {
344         msg->ms_flags |= MSGF_DONE;
345         if (port->mp_flags & MSGPORTF_WAITING)
346             lwkt_schedule(port->mp_td);
347     }
348     crit_exit();
349 }
350
351 /*
352  * lwkt_default_putport()
353  *
354  *      This function is typically assigned to the mp_putport port vector.
355  *
356  *      Queue a message to the target port and wakeup the thread owning it.
357  *      This function always returns EASYNC and may be assigned to a
358  *      message port's mp_putport function vector.  Note that we must set
359  *      MSGF_QUEUED prior to sending any IPIs in order to interlock against
360  *      ABORT requests and other tests that might be performed.
361  *
362  *      Note that messages start out as synchronous entities, and as an
363  *      optimization MSGF_DONE is usually left set (so in the synchronous path
364  *      no modifications to ms_flags are ever required).  If a message becomes
365  *      async, i.e. you return EASYNC, then MSGF_DONE must be cleared or
366  *      lwkt_replymsg() will wind up being a NOP.
367  *
368  *      The inline must be called from a critical section (the remote function
369  *      is called from an IPI and will be in a critical section).
370  */
371 static
372 __inline
373 void
374 _lwkt_putport(lwkt_port_t port, lwkt_msg_t msg, int force)
375 {
376     thread_t td = port->mp_td;
377
378     if (force || td->td_gd == mycpu) {
379         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
380         if (port->mp_flags & MSGPORTF_WAITING)
381             lwkt_schedule(td);
382     } else {
383         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_putport_remote, msg);
384     }
385 }
386
387 static
388 void
389 lwkt_putport_remote(lwkt_msg_t msg)
390 {
391     _lwkt_putport(msg->ms_target_port, msg, 1);
392 }
393
394 int
395 lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg)
396 {
397     crit_enter();
398     msg->ms_flags |= MSGF_QUEUED;       /* abort interlock */
399     msg->ms_flags &= ~MSGF_DONE;
400     msg->ms_target_port = port;
401     _lwkt_putport(port, msg, 0);
402     crit_exit();
403     return(EASYNC);
404 }
405
406 /*
407  * lwkt_forwardmsg()
408  *
409  * Forward a message received on one port to another port.  The forwarding
410  * function must deal with a pending abort but othewise essentially just
411  * issues a putport to the target port.
412  *
413  * An abort may have two side effects:  First, the message may have been
414  * requeued to the current target port.  If so, we must dequeue it before
415  * we can forward it.
416  */
417 int
418 lwkt_forwardmsg(lwkt_port_t port, lwkt_msg_t msg)
419 {   
420     int error;
421
422     crit_enter();
423     if (msg->ms_flags & MSGF_QUEUED) {
424         KKASSERT(msg->ms_flags & MSGF_ABORTED);
425         TAILQ_REMOVE(&msg->ms_target_port->mp_msgq, msg, ms_node);
426         msg->ms_flags &= ~MSGF_QUEUED;
427     }
428     msg->ms_flags &= ~MSGF_RETRIEVED;
429     if ((error = port->mp_putport(port, msg)) != EASYNC)
430         lwkt_replymsg(msg, error);
431     crit_exit();
432     return(error);
433 }
434
435 /*
436  * lwkt_abortmsg()
437  *
438  *      Aborting a message is a fairly complex task.  The first order of
439  *      business is to get the message to the cpu that owns the target
440  *      port, during which we may have to do some port chasing due to 
441  *      message forwarding operations.
442  *
443  *      NOTE!  Since an aborted message is requeued all message processing
444  *      loops should check the MSGF_ABORTED flag.
445  */
446 static void lwkt_abortmsg_remote(lwkt_msg_t msg);
447
448 void
449 lwkt_abortmsg(lwkt_msg_t msg)
450 {
451     lwkt_port_t port;
452     thread_t td;
453
454     /*
455      * A critical section protects us from reply IPIs on this cpu.   We 
456      * can only abort messages that have not yet completed (DONE), are not
457      * in the midst of being replied (REPLY1), and which support the
458      * abort function (ABORTABLE).
459      */
460     crit_enter();
461     if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY1|MSGF_ABORTABLE)) == MSGF_ABORTABLE) {
462         /*
463          * Chase the message.  If REPLY1 is set the message has been replied
464          * all the way back to the originator, otherwise it is sitting on
465          * ms_target_port (but we can only complete processing if we are
466          * on the same cpu as the selected port in order to avoid
467          * SMP cache synchronization issues).
468          *
469          * When chasing through multiple ports ms_flags may not be 
470          * synchronized to the current cpu, but it WILL be synchronized
471          * with regards to testing the MSGF_REPLY1 bit once we reach the
472          * target port that made the reply and since the cpu owning
473          * some port X stores the new port in ms_target_port if the message
474          * is forwarded, the current port will only ever equal the target
475          * port when we are on the correct cpu.
476          */
477         if (msg->ms_flags & MSGF_REPLY1)
478             port = msg->ms_reply_port;
479         else
480             port = msg->ms_target_port;
481         cpu_mb1();
482
483         /*
484          * The chase call must run on the cpu owning the port.  Fully
485          * synchronous ports (mp_td == NULL) can run the call on any cpu.
486          */
487         td = port->mp_td;
488         if (td && td->td_gd != mycpu) {
489             lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_abortmsg_remote, msg);
490         } else {
491             port->mp_abortport(port, msg);
492         }
493     }
494     crit_exit();
495 }
496
497 static
498 void
499 lwkt_abortmsg_remote(lwkt_msg_t msg)
500 {
501     lwkt_port_t port;
502     thread_t td;
503
504     if (msg->ms_flags & MSGF_REPLY1)
505         port = msg->ms_reply_port;
506     else
507         port = msg->ms_target_port;
508     cpu_mb1();
509     td = port->mp_td;
510     if (td->td_gd != mycpu) {
511         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_abortmsg_remote, msg);
512     } else {
513         port->mp_abortport(port, msg);
514     }
515 }
516
517 /*
518  * The mp_abortport function is called when the abort has finally caught up
519  * to the target port or (if the message has been replied) the reply port.
520  */
521 void
522 lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg)
523 {
524     /*
525      * Set ms_abort_port to ms_reply_port to indicate the completion of
526      * the messaging chasing portion of the abort request.  Note that
527      * the passed port is the port that we finally caught up to, not
528      * necessarily the reply port.
529      */
530     msg->ms_abort_port = msg->ms_reply_port;
531
532     if (msg->ms_flags & MSGF_REPLY2) {
533         /*
534          * If REPLY2 is set we must have chased it all the way back to
535          * the reply port, but the replyport code has not queued the message
536          * (because it was waiting for the abort to catch up).  We become
537          * responsible for queueing the message to the reply port.
538          */
539         KKASSERT((msg->ms_flags & MSGF_QUEUED) == 0);
540         KKASSERT(port == msg->ms_reply_port);
541         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
542         msg->ms_flags |= MSGF_DONE | MSGF_QUEUED;
543         if (port->mp_flags & MSGPORTF_WAITING)
544             lwkt_schedule(port->mp_td);
545     } else if ((msg->ms_flags & (MSGF_QUEUED|MSGF_REPLY1)) == 0) {
546         /*
547          * Abort on the target port.  The message has not yet been replied
548          * and must be requeued to the target port.
549          */
550         msg->ms_flags |= MSGF_ABORTED | MSGF_QUEUED;
551         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
552         if (port->mp_flags & MSGPORTF_WAITING)
553             lwkt_schedule(port->mp_td);
554     } else if ((msg->ms_flags & MSGF_REPLY1) == 0) {
555         /*
556          * The message has not yet been retrieved by the target port, set
557          * MSGF_ABORTED so the target port can requeue the message abort after
558          * retrieving it.
559          */
560         msg->ms_flags |= MSGF_ABORTED;
561     }
562 }
563
564 /*
565  * lwkt_default_waitport()
566  *
567  *      If msg is NULL, dequeue the next message from the port's message
568  *      queue, block until a message is ready.  This function never
569  *      returns NULL.
570  *
571  *      If msg is non-NULL, block until the requested message has been returned
572  *      to the port then dequeue and return it.  DO NOT USE THIS TO WAIT FOR
573  *      INCOMING REQUESTS, ONLY USE THIS TO WAIT FOR REPLIES.
574  *
575  *      Note that the API does not currently support multiple threads waiting
576  *      on a port.  By virtue of owning the port it is controlled by our
577  *      cpu and we can safely manipulate it's contents.
578  */
579 void *
580 lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg)
581 {
582     thread_t td = curthread;
583     int sentabort;
584
585     KKASSERT(port->mp_td == td);
586     crit_enter_quick(td);
587     if (msg == NULL) {
588         if ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL) {
589             port->mp_flags |= MSGPORTF_WAITING;
590             do {
591                 lwkt_deschedule_self(td);
592                 lwkt_switch();
593             } while ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL);
594             port->mp_flags &= ~MSGPORTF_WAITING;
595         }
596         _lwkt_pullmsg(port, msg);
597     } else {
598         /*
599          * If a message is not marked done, or if it is queued, we have work
600          * to do.  Note that MSGF_DONE is always set in the context of the
601          * reply port's cpu.
602          */
603         if ((msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) != MSGF_DONE) {
604             /*
605              * We must own the reply port to safely mess with it's contents.
606              */
607             port = msg->ms_reply_port;
608             KKASSERT(port->mp_td == td);
609
610             if ((msg->ms_flags & MSGF_DONE) == 0) {
611                 port->mp_flags |= MSGPORTF_WAITING; /* saved by the BGL */
612                 sentabort = 0;
613                 do {
614 #ifdef _KERNEL
615                     /*
616                      * MSGF_PCATCH is only set by processes which wish to
617                      * abort the message they are blocked on when a signal
618                      * occurs.  Note that we still must wait for message
619                      * completion after sending an abort request.
620                      */
621                     if (msg->ms_flags & MSGF_PCATCH) {
622                         if (sentabort == 0 && CURSIG(port->mp_td->td_proc)) {
623                             sentabort = 1;
624                             lwkt_abortmsg(msg);
625                             continue;
626                         }
627                     }
628 #endif
629                     /*
630                      * XXX set TDF_SINTR so 'ps' knows the difference between
631                      * an interruptable wait and a disk wait.  YYY eventually
632                      * move P_SINTR to TDF_SINTR to reduce duplication.
633                      */
634                     td->td_flags |= TDF_SINTR;
635                     lwkt_deschedule_self(td);
636                     lwkt_switch();
637                     td->td_flags &= ~TDF_SINTR;
638                 } while ((msg->ms_flags & MSGF_DONE) == 0);
639                 port->mp_flags &= ~MSGPORTF_WAITING; /* saved by the BGL */
640             }
641             /*
642              * We own the message now.
643              */
644             if (msg->ms_flags & MSGF_QUEUED) {
645                 msg->ms_flags &= ~MSGF_QUEUED;
646                 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
647             }
648         }
649     }
650     crit_exit_quick(td);
651     return(msg);
652 }
653