More -Wwrite-strings cleanup and make sure you can actually play it.
[dragonfly.git] / sys / kern / lwkt_msgport.c
blob85cf350395f680fdbe78bdfa8bbcad0f70075263
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:
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.
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.
34 * NOTE! This file may be compiled for userland libraries as well as for
35 * the kernel.
37 * $DragonFly: src/sys/kern/lwkt_msgport.c,v 1.31 2005/01/19 17:41:20 dillon Exp $
40 #ifdef _KERNEL
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>
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>
64 #include <sys/thread2.h>
65 #include <sys/msgport2.h>
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
74 #include <sys/malloc.h>
75 MALLOC_DEFINE(M_LWKTMSG, "lwkt message", "lwkt message");
77 #else
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>
90 #endif /* _KERNEL */
93 /************************************************************************
94 * MESSAGE FUNCTIONS *
95 ************************************************************************/
97 static void lwkt_replyport_remote(lwkt_msg_t msg);
98 static void lwkt_putport_remote(lwkt_msg_t msg);
101 * lwkt_sendmsg()
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.
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.
114 * NOTE: you cannot safely request an abort until lwkt_sendmsg() returns
115 * to the caller.
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.
121 void
122 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
124 int error;
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);
137 * lwkt_domsg()
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.
146 * A synchronous error code is always returned.
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.
153 * NOTE: you cannot safely request an abort until lwkt_domsg() blocks.
154 * XXX this probably needs some work.
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.
161 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg)
163 int error;
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);
172 return(error);
175 /************************************************************************
176 * PORT FUNCTIONS *
177 ************************************************************************/
180 * lwkt_initport()
182 * Initialize a port for use and assign it to the specified thread.
183 * The default reply function is to return the message to the originator.
185 void
186 lwkt_initport(lwkt_port_t port, thread_t td)
188 bzero(port, sizeof(*port));
189 TAILQ_INIT(&port->mp_msgq);
190 port->mp_td = td;
191 port->mp_putport = lwkt_default_putport;
192 port->mp_waitport = lwkt_default_waitport;
193 port->mp_replyport = lwkt_default_replyport;
194 port->mp_abortport = lwkt_default_abortport;
198 * Similar to the standard initport, this function simply marks the message
199 * as being done and does not attempt to return it to an originating port.
201 void
202 lwkt_initport_null_rport(lwkt_port_t port, thread_t td)
204 lwkt_initport(port, td);
205 port->mp_replyport = lwkt_null_replyport;
209 * lwkt_getport()
211 * Retrieve the next message from the port's message queue, return NULL
212 * if no messages are pending. Note that callers CANNOT use the
213 * MSGF_ABORTED flag as a litmus test to determine if a message
214 * was aborted. The flag only indicates that an abort was requested.
215 * The message's error code will indicate whether an abort occured
216 * (typically by returning EINTR).
218 * Note that once a message has been dequeued it is subject to being
219 * requeued via an IPI based abort request if it is not marked MSGF_DONE.
221 * If the message has been aborted we have to guarentee that abort
222 * semantics are properly followed. The target port will always see
223 * the original message at least once, and if it does not reply the
224 * message before looping on its message port again it will then see
225 * the message again with ms_cmd set to ms_abort.
227 * The calling thread MUST own the port.
230 static __inline
231 void
232 _lwkt_pullmsg(lwkt_port_t port, lwkt_msg_t msg)
234 if ((msg->ms_flags & MSGF_ABORTED) == 0) {
236 * normal case, remove and return the message.
238 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
239 msg->ms_flags = (msg->ms_flags & ~MSGF_QUEUED) | MSGF_RETRIEVED;
240 } else {
241 if (msg->ms_flags & MSGF_RETRIEVED) {
243 * abort case, message already returned once, remvoe and
244 * return the aborted message a second time after setting
245 * ms_cmd to ms_abort.
247 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
248 msg->ms_flags &= ~MSGF_QUEUED;
249 msg->ms_cmd = msg->ms_abort;
250 } else {
252 * abort case, abort races initial message retrieval. The
253 * message is returned normally but not removed from the
254 * queue. On the next loop the 'aborted' message will be
255 * dequeued and returned. Note that if the caller replies
256 * to the message it will be dequeued (the abort becomes a
257 * NOP).
259 msg->ms_flags |= MSGF_RETRIEVED;
264 void *
265 lwkt_getport(lwkt_port_t port)
267 lwkt_msg_t msg;
269 KKASSERT(port->mp_td == curthread);
271 crit_enter_quick(port->mp_td);
272 if ((msg = TAILQ_FIRST(&port->mp_msgq)) != NULL)
273 _lwkt_pullmsg(port, msg);
274 crit_exit_quick(port->mp_td);
275 return(msg);
279 * This inline helper function completes processing of a reply from an
280 * unknown cpu context.
282 * The message is being returned to the specified port. The port is
283 * owned by the mp_td thread. If we are on the same cpu as the mp_td
284 * thread we can trivially queue the message to the reply port and schedule
285 * the target thread, otherwise we have to send an ipi message to the
286 * correct cpu.
288 * This inline must be entered with a critical section already held.
289 * Note that the IPIQ callback function (*_remote) is entered with a
290 * critical section already held, and we obtain one in lwkt_replyport().
292 static __inline
293 void
294 _lwkt_replyport(lwkt_port_t port, lwkt_msg_t msg, int force)
296 thread_t td = port->mp_td;
298 if (force || td->td_gd == mycpu) {
300 * We can only reply the message if the abort has caught up with us,
301 * or if no abort was issued (same case).
303 if (msg->ms_abort_port == port) {
304 KKASSERT((msg->ms_flags & MSGF_QUEUED) == 0);
305 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
306 msg->ms_flags |= MSGF_DONE | MSGF_QUEUED | MSGF_REPLY2;
307 if (port->mp_flags & MSGPORTF_WAITING)
308 lwkt_schedule(td);
310 } else {
311 lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_replyport_remote, msg);
316 * This function completes reply processing for the default case in the
317 * context of the originating cpu.
319 static
320 void
321 lwkt_replyport_remote(lwkt_msg_t msg)
323 _lwkt_replyport(msg->ms_reply_port, msg, 1);
327 * This function is called in the context of the target to reply a message.
328 * The critical section protects us from IPIs on the this CPU.
330 void
331 lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg)
333 crit_enter();
334 msg->ms_flags |= MSGF_REPLY1;
337 * An abort may have caught up to us while we were processing the
338 * message. If this occured we have to dequeue the message from the
339 * target port in the context of our current cpu before we can finish
340 * replying it.
342 if (msg->ms_flags & MSGF_QUEUED) {
343 KKASSERT(msg->ms_flags & MSGF_ABORTED);
344 TAILQ_REMOVE(&msg->ms_target_port->mp_msgq, msg, ms_node);
345 msg->ms_flags &= ~MSGF_QUEUED;
349 * Do reply port processing for async messages. Just mark the message
350 * done and wakeup the owner of the reply port for synchronous messages.
352 if (msg->ms_flags & MSGF_ASYNC) {
353 _lwkt_replyport(port, msg, 0);
354 } else {
355 msg->ms_flags |= MSGF_DONE;
356 if (port->mp_flags & MSGPORTF_WAITING)
357 lwkt_schedule(port->mp_td);
359 crit_exit();
363 * You can point a port's reply vector at this function if you just want
364 * the message marked done, without any queueing or signaling. This is
365 * often used for structure-embedded messages.
367 void
368 lwkt_null_replyport(lwkt_port_t port, lwkt_msg_t msg)
370 crit_enter();
371 msg->ms_flags |= MSGF_DONE|MSGF_REPLY1;
372 crit_exit();
376 * lwkt_default_putport()
378 * This function is typically assigned to the mp_putport port vector.
380 * Queue a message to the target port and wakeup the thread owning it.
381 * This function always returns EASYNC and may be assigned to a
382 * message port's mp_putport function vector. Note that we must set
383 * MSGF_QUEUED prior to sending any IPIs in order to interlock against
384 * ABORT requests and other tests that might be performed.
386 * Note that messages start out as synchronous entities, and as an
387 * optimization MSGF_DONE is usually left set (so in the synchronous path
388 * no modifications to ms_flags are ever required). If a message becomes
389 * async, i.e. you return EASYNC, then MSGF_DONE must be cleared or
390 * lwkt_replymsg() will wind up being a NOP.
392 * The inline must be called from a critical section (the remote function
393 * is called from an IPI and will be in a critical section).
395 static
396 __inline
397 void
398 _lwkt_putport(lwkt_port_t port, lwkt_msg_t msg, int force)
400 thread_t td = port->mp_td;
402 if (force || td->td_gd == mycpu) {
403 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
404 if (port->mp_flags & MSGPORTF_WAITING)
405 lwkt_schedule(td);
406 } else {
407 lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_putport_remote, msg);
411 static
412 void
413 lwkt_putport_remote(lwkt_msg_t msg)
415 _lwkt_putport(msg->ms_target_port, msg, 1);
419 lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg)
421 crit_enter();
422 msg->ms_flags |= MSGF_QUEUED; /* abort interlock */
423 msg->ms_flags &= ~MSGF_DONE;
424 msg->ms_target_port = port;
425 _lwkt_putport(port, msg, 0);
426 crit_exit();
427 return(EASYNC);
431 * lwkt_forwardmsg()
433 * Forward a message received on one port to another port. The forwarding
434 * function must deal with a pending abort but othewise essentially just
435 * issues a putport to the target port.
437 * An abort may have two side effects: First, the message may have been
438 * requeued to the current target port. If so, we must dequeue it before
439 * we can forward it.
442 lwkt_forwardmsg(lwkt_port_t port, lwkt_msg_t msg)
444 int error;
446 crit_enter();
447 if (msg->ms_flags & MSGF_QUEUED) {
448 KKASSERT(msg->ms_flags & MSGF_ABORTED);
449 TAILQ_REMOVE(&msg->ms_target_port->mp_msgq, msg, ms_node);
450 msg->ms_flags &= ~MSGF_QUEUED;
452 msg->ms_flags &= ~MSGF_RETRIEVED;
453 if ((error = port->mp_putport(port, msg)) != EASYNC)
454 lwkt_replymsg(msg, error);
455 crit_exit();
456 return(error);
460 * lwkt_abortmsg()
462 * Aborting a message is a fairly complex task. The first order of
463 * business is to get the message to the cpu that owns the target
464 * port, during which we may have to do some port chasing due to
465 * message forwarding operations.
467 * NOTE! Since an aborted message is requeued all message processing
468 * loops should check the MSGF_ABORTED flag.
470 static void lwkt_abortmsg_remote(lwkt_msg_t msg);
472 void
473 lwkt_abortmsg(lwkt_msg_t msg)
475 lwkt_port_t port;
476 thread_t td;
479 * A critical section protects us from reply IPIs on this cpu. We
480 * can only abort messages that have not yet completed (DONE), are not
481 * in the midst of being replied (REPLY1), and which support the
482 * abort function (ABORTABLE).
484 crit_enter();
485 if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY1|MSGF_ABORTABLE)) == MSGF_ABORTABLE) {
487 * Chase the message. If REPLY1 is set the message has been replied
488 * all the way back to the originator, otherwise it is sitting on
489 * ms_target_port (but we can only complete processing if we are
490 * on the same cpu as the selected port in order to avoid
491 * SMP cache synchronization issues).
493 * When chasing through multiple ports ms_flags may not be
494 * synchronized to the current cpu, but it WILL be synchronized
495 * with regards to testing the MSGF_REPLY1 bit once we reach the
496 * target port that made the reply and since the cpu owning
497 * some port X stores the new port in ms_target_port if the message
498 * is forwarded, the current port will only ever equal the target
499 * port when we are on the correct cpu.
501 if (msg->ms_flags & MSGF_REPLY1)
502 port = msg->ms_reply_port;
503 else
504 port = msg->ms_target_port;
505 cpu_mb1();
508 * The chase call must run on the cpu owning the port. Fully
509 * synchronous ports (mp_td == NULL) can run the call on any cpu.
511 td = port->mp_td;
512 if (td && td->td_gd != mycpu) {
513 lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_abortmsg_remote, msg);
514 } else {
515 port->mp_abortport(port, msg);
518 crit_exit();
521 static
522 void
523 lwkt_abortmsg_remote(lwkt_msg_t msg)
525 lwkt_port_t port;
526 thread_t td;
528 if (msg->ms_flags & MSGF_REPLY1)
529 port = msg->ms_reply_port;
530 else
531 port = msg->ms_target_port;
532 cpu_mb1();
533 td = port->mp_td;
534 if (td->td_gd != mycpu) {
535 lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_abortmsg_remote, msg);
536 } else {
537 port->mp_abortport(port, msg);
542 * The mp_abortport function is called when the abort has finally caught up
543 * to the target port or (if the message has been replied) the reply port.
545 void
546 lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg)
549 * Set ms_abort_port to ms_reply_port to indicate the completion of
550 * the messaging chasing portion of the abort request. Note that
551 * the passed port is the port that we finally caught up to, not
552 * necessarily the reply port.
554 msg->ms_abort_port = msg->ms_reply_port;
556 if (msg->ms_flags & MSGF_REPLY2) {
558 * If REPLY2 is set we must have chased it all the way back to
559 * the reply port, but the replyport code has not queued the message
560 * (because it was waiting for the abort to catch up). We become
561 * responsible for queueing the message to the reply port.
563 KKASSERT((msg->ms_flags & MSGF_QUEUED) == 0);
564 KKASSERT(port == msg->ms_reply_port);
565 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
566 msg->ms_flags |= MSGF_DONE | MSGF_QUEUED;
567 if (port->mp_flags & MSGPORTF_WAITING)
568 lwkt_schedule(port->mp_td);
569 } else if ((msg->ms_flags & (MSGF_QUEUED|MSGF_REPLY1)) == 0) {
571 * Abort on the target port. The message has not yet been replied
572 * and must be requeued to the target port.
574 msg->ms_flags |= MSGF_ABORTED | MSGF_QUEUED;
575 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
576 if (port->mp_flags & MSGPORTF_WAITING)
577 lwkt_schedule(port->mp_td);
578 } else if ((msg->ms_flags & MSGF_REPLY1) == 0) {
580 * The message has not yet been retrieved by the target port, set
581 * MSGF_ABORTED so the target port can requeue the message abort after
582 * retrieving it.
584 msg->ms_flags |= MSGF_ABORTED;
589 * lwkt_default_waitport()
591 * If msg is NULL, dequeue the next message from the port's message
592 * queue, block until a message is ready. This function never
593 * returns NULL.
595 * If msg is non-NULL, block until the requested message has been returned
596 * to the port then dequeue and return it. DO NOT USE THIS TO WAIT FOR
597 * INCOMING REQUESTS, ONLY USE THIS TO WAIT FOR REPLIES.
599 * Note that the API does not currently support multiple threads waiting
600 * on a port. By virtue of owning the port it is controlled by our
601 * cpu and we can safely manipulate it's contents.
603 void *
604 lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg)
606 thread_t td = curthread;
607 int sentabort;
609 KKASSERT(port->mp_td == td);
610 crit_enter_quick(td);
611 if (msg == NULL) {
612 if ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL) {
613 port->mp_flags |= MSGPORTF_WAITING;
614 td->td_flags |= TDF_BLOCKED;
615 do {
616 lwkt_deschedule_self(td);
617 lwkt_switch();
618 } while ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL);
619 td->td_flags &= ~TDF_BLOCKED;
620 port->mp_flags &= ~MSGPORTF_WAITING;
622 _lwkt_pullmsg(port, msg);
623 } else {
625 * If a message is not marked done, or if it is queued, we have work
626 * to do. Note that MSGF_DONE is always set in the context of the
627 * reply port's cpu.
629 if ((msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) != MSGF_DONE) {
631 * We must own the reply port to safely mess with it's contents.
633 port = msg->ms_reply_port;
634 KKASSERT(port->mp_td == td);
636 if ((msg->ms_flags & MSGF_DONE) == 0) {
637 port->mp_flags |= MSGPORTF_WAITING; /* saved by the BGL */
638 sentabort = 0;
639 do {
640 #ifdef _KERNEL
642 * MSGF_PCATCH is only set by processes which wish to
643 * abort the message they are blocked on when a signal
644 * occurs. Note that we still must wait for message
645 * completion after sending an abort request.
647 if (msg->ms_flags & MSGF_PCATCH) {
648 if (sentabort == 0 && CURSIG(port->mp_td->td_proc)) {
649 sentabort = 1;
650 lwkt_abortmsg(msg);
651 continue;
654 #endif
656 * XXX set TDF_SINTR so 'ps' knows the difference between
657 * an interruptable wait and a disk wait. YYY eventually
658 * move P_SINTR to TDF_SINTR to reduce duplication.
660 td->td_flags |= TDF_SINTR | TDF_BLOCKED;
661 lwkt_deschedule_self(td);
662 lwkt_switch();
663 td->td_flags &= ~(TDF_SINTR | TDF_BLOCKED);
664 } while ((msg->ms_flags & MSGF_DONE) == 0);
665 port->mp_flags &= ~MSGPORTF_WAITING; /* saved by the BGL */
668 * We own the message now.
670 if (msg->ms_flags & MSGF_QUEUED) {
671 msg->ms_flags &= ~MSGF_QUEUED;
672 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
676 crit_exit_quick(td);
677 return(msg);