2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * NOTE! This file may be compiled for userland libraries as well as for
29 * $DragonFly: src/sys/kern/lwkt_msgport.c,v 1.14 2004/02/12 06:57:48 dillon Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/rtprio.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
41 #include <sys/kthread.h>
42 #include <machine/cpu.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_pager.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_zone.h>
55 #include <sys/thread2.h>
56 #include <sys/msgport2.h>
58 #include <machine/stdarg.h>
59 #include <machine/ipl.h>
61 #include <machine/smp.h>
66 #include <sys/stdint.h>
67 #include <libcaps/thread.h>
68 #include <sys/thread.h>
69 #include <sys/msgport.h>
70 #include <sys/errno.h>
71 #include <libcaps/globaldata.h>
72 #include <sys/thread2.h>
73 #include <sys/msgport2.h>
79 /************************************************************************
81 ************************************************************************/
83 static int lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg);
84 static void *lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg);
85 static void lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg);
86 static void lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg);
87 static void lwkt_replyport_remote(lwkt_msg_t msg);
88 static void lwkt_putport_remote(lwkt_msg_t msg);
91 lwkt_initmsg_td(lwkt_msg_t msg, thread_t td)
93 lwkt_initmsg(msg, &td->td_msgport, 0);
99 * Send a message asynchronously. This function requests asynchronous
100 * completion and calls lwkt_beginmsg(). If the target port decides to
101 * run the message synchronously this function will automatically queue
102 * the message to the current thread's message queue to present a
103 * consistent interface to the caller.
105 * The message's ms_cmd must be initialized and its ms_flags must be
106 * at least zero'd out. lwkt_sendmsg() will initialize the message's
107 * reply port to the current thread's built-in reply port.
110 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
114 msg->ms_flags |= MSGF_ASYNC;
115 msg->ms_flags &= ~(MSGF_REPLY | MSGF_QUEUED);
116 msg->ms_reply_port = &curthread->td_msgport;
117 if ((error = lwkt_beginmsg(port, msg)) != EASYNC) {
118 lwkt_replymsg(msg, error);
125 * Send a message synchronously. This function requests synchronous
126 * completion and calls lwkt_beginmsg(). If the target port decides to
127 * run the message asynchronously this function will block waiting for
128 * the message to complete. Since MSGF_ASYNC is not set the target
129 * will not attempt to queue the reply to a reply port but will simply
130 * wake up anyone waiting on the message.
132 * A synchronous error code is always returned.
134 * The message's ms_cmd must be initialized and its ms_flags must be
135 * at least zero'd out. lwkt_domsg() will initialize the message's
136 * reply port to the current thread's built-in reply port.
139 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg)
143 msg->ms_flags &= ~(MSGF_ASYNC | MSGF_REPLY | MSGF_QUEUED);
144 msg->ms_reply_port = &curthread->td_msgport;
145 if ((error = lwkt_beginmsg(port, msg)) == EASYNC) {
146 error = lwkt_waitmsg(msg);
151 /************************************************************************
153 ************************************************************************/
158 * Initialize a port for use and assign it to the specified thread.
161 lwkt_initport(lwkt_port_t port, thread_t td)
163 bzero(port, sizeof(*port));
164 TAILQ_INIT(&port->mp_msgq);
166 port->mp_putport = lwkt_default_putport;
167 port->mp_waitport = lwkt_default_waitport;
168 port->mp_replyport = lwkt_default_replyport;
169 port->mp_abortport = lwkt_default_abortport;
175 * Retrieve the next message from the port's message queue, return NULL
176 * if no messages are pending.
178 * The calling thread MUST own the port.
181 lwkt_getport(lwkt_port_t port)
185 KKASSERT(port->mp_td == curthread);
188 if ((msg = TAILQ_FIRST(&port->mp_msgq)) != NULL) {
189 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
190 msg->ms_flags &= ~MSGF_QUEUED;
197 * lwkt_default_replyport()
199 * This function is typically assigned to the mp_replyport port vector.
201 * The message is being returned to the specified port. The port is
202 * owned by the mp_td thread. If we are on the same cpu as the mp_td
203 * thread we can trivially queue the message to the messageq, otherwise
204 * we have to send an ipi message to the correct cpu. We then schedule
207 * If MSGF_ASYNC is not set we do not bother queueing the message, we
208 * just set the DONE bit.
210 * This inline must be entered with a critical section already held.
211 * Note that the IPIQ callback function (*_remote) is entered with a
212 * critical section already held, and we obtain one in lwkt_replyport().
216 _lwkt_replyport(lwkt_port_t port, lwkt_msg_t msg)
218 thread_t td = port->mp_td;
220 if (td->td_gd == mycpu) {
221 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
222 msg->ms_flags |= MSGF_DONE | MSGF_REPLY | MSGF_QUEUED;
223 if (port->mp_flags & MSGPORTF_WAITING)
226 lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_replyport_remote, msg);
232 lwkt_replyport_remote(lwkt_msg_t msg)
234 _lwkt_replyport(msg->ms_reply_port, msg);
239 lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg)
242 if (msg->ms_flags & MSGF_ASYNC) {
243 _lwkt_replyport(port, msg);
245 msg->ms_flags |= MSGF_DONE;
246 if (port->mp_flags & MSGPORTF_WAITING)
247 lwkt_schedule(port->mp_td);
253 * lwkt_default_putport()
255 * This function is typically assigned to the mp_putport port vector.
257 * Queue a message to the target port and wakeup the thread owning it.
258 * This function always returns EASYNC and may be assigned to a
259 * message port's mp_putport function vector.
261 * You must already be in a critical section when calling
262 * the inline function. The _remote function will be in a critical
263 * section due to being called from the IPI, and lwkt_default_putport()
264 * enters a critical section.
269 _lwkt_putport(lwkt_port_t port, lwkt_msg_t msg)
271 thread_t td = port->mp_td;
273 if (td->td_gd == mycpu) {
274 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
275 msg->ms_flags |= MSGF_QUEUED;
276 if (port->mp_flags & MSGPORTF_WAITING)
279 msg->ms_target_port = port;
280 lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_putport_remote, msg);
286 lwkt_putport_remote(lwkt_msg_t msg)
288 _lwkt_putport(msg->ms_target_port, msg);
293 lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg)
296 msg->ms_flags &= ~MSGF_DONE;
297 _lwkt_putport(port, msg);
303 * lwkt_default_abortport()
305 * This function is typically assigned to the mp_abortport port vector.
307 * This vector is typically called via the message's ms_target_port
308 * pointer. It should be noted that ms_target_port may race against
309 * a forwarding operation run on a different cpu. Any implementation
310 * of lwkt_abortport() must deal with potential races by following
311 * the message to the next appropriate port.
313 * This function is a NOP. by defaults message ports have no abort
314 * capabilities. Remember that aborts are always optional so doing
315 * nothing is perfectly reasonable.
319 lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg)
325 * lwkt_default_waitport()
327 * If msg is NULL, dequeue the next message from the port's message
328 * queue, block until a message is ready. This function never
331 * If msg is non-NULL, block until the requested message has been returned
332 * to the port then dequeue and return it.
334 * Note that the API does not currently support multiple threads waiting
335 * on a port. By virtue of owning the port it is controlled by our
336 * cpu and we can safely manipulate it's contents.
340 lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg)
342 KKASSERT(port->mp_td == curthread);
346 if ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL) {
347 port->mp_flags |= MSGPORTF_WAITING;
349 lwkt_deschedule_self();
351 } while ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL);
352 port->mp_flags &= ~MSGPORTF_WAITING;
354 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
355 msg->ms_flags &= ~MSGF_QUEUED;
358 * If the message is marked done by not queued it has already been
359 * pulled off the port and returned and we do not have to do anything.
360 * Otherwise we do not own the message have to wait for message
361 * completion. Beware of cpu races if MSGF_DONE is not foudn to be
364 if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY)) != MSGF_DONE) {
366 * We must own the reply port to safely mess with it's contents.
368 port = msg->ms_reply_port;
369 KKASSERT(port->mp_td == curthread);
371 if ((msg->ms_flags & MSGF_DONE) == 0) {
372 port->mp_flags |= MSGPORTF_WAITING; /* saved by the BGL */
374 lwkt_deschedule_self();
376 } while ((msg->ms_flags & MSGF_DONE) == 0);
377 port->mp_flags &= ~MSGPORTF_WAITING; /* saved by the BGL */
380 * We own the message now.
382 if (msg->ms_flags & MSGF_QUEUED) {
383 msg->ms_flags &= ~MSGF_QUEUED;
384 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);