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