Start removing the old build infrastructure for the a.out
[dragonfly.git] / sys / kern / lwkt_msgport.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * NOTE! This file may be compiled for userland libraries as well as for
27  * the kernel.
28  *
29  * $DragonFly: src/sys/kern/lwkt_msgport.c,v 1.16 2004/03/06 19:40:28 dillon Exp $
30  */
31
32 #ifdef _KERNEL
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.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>
43 #include <sys/lock.h>
44
45 #include <vm/vm.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>
54
55 #include <sys/thread2.h>
56 #include <sys/msgport2.h>
57
58 #include <machine/stdarg.h>
59 #include <machine/ipl.h>
60 #ifdef SMP
61 #include <machine/smp.h>
62 #endif
63
64 #include <sys/malloc.h>
65 MALLOC_DEFINE(M_LWKTMSG, "lwkt message", "lwkt message");
66
67 #else
68
69 #include <sys/stdint.h>
70 #include <libcaps/thread.h>
71 #include <sys/thread.h>
72 #include <sys/msgport.h>
73 #include <sys/errno.h>
74 #include <libcaps/globaldata.h>
75 #include <sys/thread2.h>
76 #include <sys/msgport2.h>
77 #include <string.h>
78
79 #endif /* _KERNEL */
80
81
82 /************************************************************************
83  *                              MESSAGE FUNCTIONS                       *
84  ************************************************************************/
85
86 static int lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg);
87 static void *lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg);
88 static void lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg);
89 static void lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg);
90 static void lwkt_replyport_remote(lwkt_msg_t msg);
91 static void lwkt_putport_remote(lwkt_msg_t msg);
92
93 /*
94  * lwkt_sendmsg()
95  *
96  *      Send a message asynchronously.  This function requests asynchronous
97  *      completion and calls lwkt_beginmsg().  If the target port decides to
98  *      run the message synchronously this function will automatically queue
99  *      the message to the current thread's message queue to present a
100  *      consistent interface to the caller. 
101  *
102  *      The message's ms_cmd must be initialized and its ms_flags must be
103  *      at least zero'd out.  lwkt_sendmsg() will initialize the message's
104  *      reply port to the current thread's built-in reply port.
105  */
106 void
107 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
108 {
109     int error;
110
111     msg->ms_flags |= MSGF_ASYNC;
112     msg->ms_flags &= ~(MSGF_REPLY | MSGF_QUEUED);
113     KKASSERT(msg->ms_reply_port != NULL);
114     if ((error = lwkt_beginmsg(port, msg)) != EASYNC) {
115         lwkt_replymsg(msg, error);
116     }
117 }
118
119 /*
120  * lwkt_domsg()
121  *
122  *      Send a message synchronously.  This function requests synchronous
123  *      completion and calls lwkt_beginmsg().  If the target port decides to
124  *      run the message asynchronously this function will block waiting for
125  *      the message to complete.  Since MSGF_ASYNC is not set the target
126  *      will not attempt to queue the reply to a reply port but will simply
127  *      wake up anyone waiting on the message.
128  *
129  *      A synchronous error code is always returned.
130  *
131  *      The message's ms_cmd must be initialized and its ms_flags must be
132  *      at least zero'd out.  lwkt_domsg() will initialize the message's
133  *      reply port to the current thread's built-in reply port.
134  */
135 int
136 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg)
137 {
138     int error;
139
140     msg->ms_flags &= ~(MSGF_ASYNC | MSGF_REPLY | MSGF_QUEUED);
141     KKASSERT(msg->ms_reply_port != NULL);
142     if ((error = lwkt_beginmsg(port, msg)) == EASYNC) {
143         error = lwkt_waitmsg(msg);
144     }
145     return(error);
146 }
147
148 /************************************************************************
149  *                              PORT FUNCTIONS                          *
150  ************************************************************************/
151
152 /*
153  * lwkt_initport()
154  *
155  *      Initialize a port for use and assign it to the specified thread.
156  */
157 void
158 lwkt_initport(lwkt_port_t port, thread_t td)
159 {
160     bzero(port, sizeof(*port));
161     TAILQ_INIT(&port->mp_msgq);
162     port->mp_td = td;
163     port->mp_putport = lwkt_default_putport;
164     port->mp_waitport =  lwkt_default_waitport;
165     port->mp_replyport = lwkt_default_replyport;
166     port->mp_abortport = lwkt_default_abortport;
167 }
168
169 /*
170  * lwkt_getport()
171  *
172  *      Retrieve the next message from the port's message queue, return NULL
173  *      if no messages are pending.
174  *
175  *      The calling thread MUST own the port.
176  */
177 void *
178 lwkt_getport(lwkt_port_t port)
179 {
180     lwkt_msg_t msg;
181
182     KKASSERT(port->mp_td == curthread);
183
184     crit_enter();
185     if ((msg = TAILQ_FIRST(&port->mp_msgq)) != NULL) {
186         TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
187         msg->ms_flags &= ~MSGF_QUEUED;
188     }
189     crit_exit();
190     return(msg);
191 }
192
193 /*
194  * lwkt_default_replyport()
195  *
196  *      This function is typically assigned to the mp_replyport port vector.
197  *
198  *      The message is being returned to the specified port.  The port is
199  *      owned by the mp_td thread.  If we are on the same cpu as the mp_td
200  *      thread we can trivially queue the message to the messageq, otherwise
201  *      we have to send an ipi message to the correct cpu.   We then schedule
202  *      the target thread.
203  *
204  *      If MSGF_ASYNC is not set we do not bother queueing the message, we
205  *      just set the DONE bit.  
206  *
207  *      This inline must be entered with a critical section already held.
208  *      Note that the IPIQ callback function (*_remote) is entered with a
209  *      critical section already held, and we obtain one in lwkt_replyport().
210  */
211 static __inline
212 void
213 _lwkt_replyport(lwkt_port_t port, lwkt_msg_t msg)
214 {
215     thread_t td = port->mp_td;
216
217     if (td->td_gd == mycpu) {
218         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
219         msg->ms_flags |= MSGF_DONE | MSGF_REPLY | MSGF_QUEUED;
220         if (port->mp_flags & MSGPORTF_WAITING)
221             lwkt_schedule(td);
222     } else {
223         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_replyport_remote, msg);
224     }
225 }
226
227 static
228 void
229 lwkt_replyport_remote(lwkt_msg_t msg)
230 {
231     _lwkt_replyport(msg->ms_reply_port, msg);
232 }
233
234 static
235 void
236 lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg)
237 {
238     crit_enter();
239     if (msg->ms_flags & MSGF_ASYNC) {
240         _lwkt_replyport(port, msg);
241     } else {
242         msg->ms_flags |= MSGF_DONE;
243         if (port->mp_flags & MSGPORTF_WAITING)
244             lwkt_schedule(port->mp_td);
245     }
246     crit_exit();
247 }
248
249 /*
250  * lwkt_default_putport()
251  *
252  *      This function is typically assigned to the mp_putport port vector.
253  *
254  *      Queue a message to the target port and wakeup the thread owning it.
255  *      This function always returns EASYNC and may be assigned to a
256  *      message port's mp_putport function vector.
257  *
258  *      You must already be in a critical section when calling
259  *      the inline function.  The _remote function will be in a critical
260  *      section due to being called from the IPI, and lwkt_default_putport() 
261  *      enters a critical section.
262  */
263 static
264 __inline
265 void
266 _lwkt_putport(lwkt_port_t port, lwkt_msg_t msg)
267 {
268     thread_t td = port->mp_td;
269
270     if (td->td_gd == mycpu) {
271         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
272         msg->ms_flags |= MSGF_QUEUED;
273         if (port->mp_flags & MSGPORTF_WAITING)
274             lwkt_schedule(td);
275     } else {
276         msg->ms_target_port = port;
277         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_putport_remote, msg);
278     }
279 }
280
281 static
282 void
283 lwkt_putport_remote(lwkt_msg_t msg)
284 {
285     _lwkt_putport(msg->ms_target_port, msg);
286 }
287
288 static
289 int
290 lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg)
291 {
292     crit_enter();
293     msg->ms_flags &= ~MSGF_DONE;
294     _lwkt_putport(port, msg);
295     crit_exit();
296     return(EASYNC);
297 }
298
299 /*
300  * lwkt_default_abortport()
301  *
302  *      This function is typically assigned to the mp_abortport port vector.
303  *
304  *      This vector is typically called via the message's ms_target_port
305  *      pointer.  It should be noted that ms_target_port may race against
306  *      a forwarding operation run on a different cpu.  Any implementation
307  *      of lwkt_abortport() must deal with potential races by following
308  *      the message to the next appropriate port.
309  *
310  *      This function is a NOP.  by defaults message ports have no abort
311  *      capabilities.  Remember that aborts are always optional so doing 
312  *      nothing is perfectly reasonable.
313  */
314 static
315 void
316 lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg)
317 {
318     /* NOP */
319 }
320
321 /*
322  * lwkt_default_waitport()
323  *
324  *      If msg is NULL, dequeue the next message from the port's message
325  *      queue, block until a message is ready.  This function never
326  *      returns NULL.
327  *
328  *      If msg is non-NULL, block until the requested message has been returned
329  *      to the port then dequeue and return it.
330  *
331  *      Note that the API does not currently support multiple threads waiting
332  *      on a port.  By virtue of owning the port it is controlled by our
333  *      cpu and we can safely manipulate it's contents.
334  */
335 static
336 void *
337 lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg)
338 {
339     KKASSERT(port->mp_td == curthread);
340
341     crit_enter();
342     if (msg == NULL) {
343         if ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL) {
344             port->mp_flags |= MSGPORTF_WAITING;
345             do {
346                 lwkt_deschedule_self();
347                 lwkt_switch();
348             } while ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL);
349             port->mp_flags &= ~MSGPORTF_WAITING;
350         }
351         TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
352         msg->ms_flags &= ~MSGF_QUEUED;
353     } else {
354         /*
355          * If the message is marked done by not queued it has already been
356          * pulled off the port and returned and we do not have to do anything.
357          * Otherwise we do not own the message have to wait for message
358          * completion.  Beware of cpu races if MSGF_DONE is not foudn to be
359          * set!
360          */
361         if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY)) != MSGF_DONE) {
362             /*
363              * We must own the reply port to safely mess with it's contents.
364              */
365             port = msg->ms_reply_port;
366             KKASSERT(port->mp_td == curthread);
367
368             if ((msg->ms_flags & MSGF_DONE) == 0) {
369                 port->mp_flags |= MSGPORTF_WAITING; /* saved by the BGL */
370                 do {
371                     lwkt_deschedule_self();
372                     lwkt_switch();
373                 } while ((msg->ms_flags & MSGF_DONE) == 0);
374                 port->mp_flags &= ~MSGPORTF_WAITING; /* saved by the BGL */
375             }
376             /*
377              * We own the message now.
378              */
379             if (msg->ms_flags & MSGF_QUEUED) {
380                 msg->ms_flags &= ~MSGF_QUEUED;
381                 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
382             }
383         }
384     }
385     crit_exit();
386     return(msg);
387 }
388