kernel - Move MP lock inward, plus misc other stuff
[dragonfly.git] / sys / kern / kern_upcall.c
1 /*
2  * Copyright (c) 2003,2004,2006 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  * $DragonFly: src/sys/kern/kern_upcall.c,v 1.11 2006/09/10 21:35:10 dillon Exp $
35  */
36
37 /*
38  * Implement upcall registration and dispatch.
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/upcall.h>
46 #include <sys/thread2.h>
47 #include <sys/upcall.h>
48 #include <sys/malloc.h>
49 #include <sys/sysproto.h>
50 #include <sys/lock.h>
51 #include <sys/signalvar.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_kern.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58
59 #include <machine/cpu.h>
60
61 MALLOC_DEFINE(M_UPCALL, "upcalls", "upcall registration structures");
62
63 #ifdef SMP
64
65 static void
66 sigupcall_remote(void *arg)
67 {
68         struct lwp *lp = arg;
69         if (lp == lwkt_preempted_proc())
70                 sigupcall();
71 }
72
73 #endif
74
75 /*
76  * upc_register:
77  *
78  *      Register an upcall context wrapper and procedure.  Note that the
79  *      upcall context is set globally for the process, not for each upcall.
80  *
81  * ARGS(struct upcall *upc, upcall_func_t ctx, upcall_func_t func, void *data)
82  *
83  * MPALMOSTSAFE
84  */
85 int
86 sys_upc_register(struct upc_register_args *uap)
87 {
88     struct lwp *lp = curthread->td_lwp;
89     struct vmspace *vm = curproc->p_vmspace;
90     struct vmupcall *vu;
91
92     /*
93      * Note: inconsequential MP race
94      */
95     if (vm->vm_upccount >= UPCALL_MAXCOUNT)
96         return(EFBIG);
97
98     vu = kmalloc(sizeof(struct vmupcall), M_UPCALL, M_WAITOK|M_ZERO);
99     vu->vu_ctx = uap->ctxfunc;
100     vu->vu_func = uap->func;
101     vu->vu_data = uap->data;
102     vu->vu_lwp = lp;
103     lp->lwp_upcall = uap->upc;
104
105     get_mplock();
106     if (vm->vm_upcalls != NULL)
107         vu->vu_id = vm->vm_upcalls->vu_id + 1;
108     else
109         vu->vu_id = UPC_RESERVED;
110     vu->vu_next = vm->vm_upcalls;
111     vm->vm_upcalls = vu;
112     ++vm->vm_upccount;
113     rel_mplock();
114     uap->sysmsg_result = vu->vu_id;
115     return(0);
116 }
117
118 /*
119  * upc_control:
120  *
121  * ARGS(int cmd, int upcid, void *data)
122  *
123  * MPALMOSTSAFE
124  */
125 int
126 sys_upc_control(struct upc_control_args *uap)
127 {
128     struct lwp *lp = curthread->td_lwp;
129     struct lwp *targlp;
130     struct vmspace *vms = curproc->p_vmspace;
131     struct vmupcall *vu;
132     struct vmupcall *vu_send;
133     struct vmupcall **vupp;
134     int error;
135
136     get_mplock();
137     switch(uap->cmd) {
138     case UPC_CONTROL_DISPATCH:
139         /*
140          * Dispatch the specified upcall id or the next pending id if -1.
141          * the upcall will be marked pending but an actual upcall will only
142          * occur if userland is not in a critical section and the userland
143          * pending bit is not set.
144          *
145          * You can dispatch an upcall associated with your process or another
146          * process sharing the same VM space.
147          */
148         error = (uap->upcid == -1) ? 0 : ENOENT;
149         for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
150             if (vu->vu_id == uap->upcid || 
151                 (uap->upcid == -1 &&
152                 vu->vu_pending >= (int)(intptr_t)uap->data && vu->vu_lwp == lp)
153             ) {
154                 if (vu->vu_pending < (int)(intptr_t)uap->data)
155                     vu->vu_pending = (int)(intptr_t)uap->data;
156                 error = 0;
157                 targlp = vu->vu_lwp;
158                 targlp->lwp_proc->p_flag |= P_UPCALLPEND;       /* XXX lwp flags */
159                 if (targlp->lwp_proc->p_flag & P_UPCALLWAIT)
160                     wakeup(&targlp->lwp_upcall);
161 #ifdef SMP
162                 if (targlp->lwp_thread->td_gd != mycpu)
163                     lwkt_send_ipiq(targlp->lwp_thread->td_gd, sigupcall_remote, targlp);
164                 else
165                     sigupcall();
166 #else
167                 sigupcall();
168 #endif
169                 break;
170             }
171         }
172         break;
173     case UPC_CONTROL_NEXT:
174         /*
175          * This is used by the context code to fetch the next pending upcall.
176          * The context code has two choices:  (A) it can drop
177          * upcall->crit_count and set upcall->pending then make this call
178          * unconditionally or * (B) it can drop upcall->crit_count and then
179          * test upcall->pending and only make this call if upcall->pending
180          * is set.  If upcall->pending is clear the context code can pop
181          * the upcall stack itself and return without entering into the kernel
182          * again.  (B) is more efficient but leaves a small window of
183          * opportunity where multiple upcalls can pushdown the stack.
184          *
185          * If another upcall is pending the crit_count will be bumped and
186          * the function, data, and context pointers will be returned in
187          * registers (C cannot call this routine).  If no more upcalls are
188          * pending the pending bit will be cleared and the 'data' argument
189          * is expected to be pointing at the upcall context which we will
190          * then pop, returning to the original code that was interrupted
191          * (NOT the context code).
192          */
193         vu_send = NULL;
194         for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
195             if (vu->vu_lwp == lp && vu->vu_pending) {
196                 if (vu_send)
197                     break;
198                 vu_send = vu;
199             }
200         }
201         /*
202          * vu_send may be NULL, indicating that no more upcalls are pending
203          * for this cpu.  We set the userland pending bit based on whether
204          * additional upcalls are pending or not.
205          */
206         error = fetchupcall(vu_send, vu != NULL, uap->data);
207         break;
208     case UPC_CONTROL_DELETE:
209         /*
210          * Delete the specified upcall id.  If the upcall id is -1, delete
211          * all upcall id's associated with the current process.
212          */
213         error = (uap->upcid == -1) ? 0 : ENOENT;
214         vupp = &vms->vm_upcalls;
215         while ((vu = *vupp) != NULL) {
216             if (vu->vu_id == uap->upcid || 
217                 (uap->upcid == -1 && vu->vu_lwp == lp)
218             ) {
219                 *vupp = vu->vu_next;
220                 error = 0;
221                 kfree(vu, M_UPCALL);
222             } else {
223                 vupp = &vu->vu_next;
224             }
225         }
226         break;
227     case UPC_CONTROL_POLL:
228     case UPC_CONTROL_POLLANDCLEAR:
229     case UPC_CONTROL_WAIT:
230         /*
231          * If upcid is -1 poll for the first pending upcall and return the
232          * id or 0 if no upcalls are pending.
233          *
234          * If upcid is a particular upcall then poll that upcall and return
235          * its pending status (0 or 1).  For POLLANDCLEAR, also clear the
236          * pending status.  The userland pending bit is not modified by
237          * this call (maybe we should modify it for poll-and-clear).
238          */
239         error = (uap->upcid == -1) ? 0 : ENOENT;
240         for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
241             if (vu->vu_id == uap->upcid || 
242                 (uap->upcid == -1 &&
243                  vu->vu_pending >= (int)(intptr_t)uap->data && vu->vu_lwp == lp)
244             ) {
245                 error = 0;
246                 if (uap->upcid == -1)
247                     uap->sysmsg_result = vu->vu_id;
248                 else
249                     uap->sysmsg_result = vu->vu_pending;
250                 if (uap->cmd == UPC_CONTROL_POLLANDCLEAR)
251                     vu->vu_pending = 0;
252                 break;
253             }
254         }
255         if (uap->cmd == UPC_CONTROL_WAIT && vu == NULL) {
256             lp->lwp_proc->p_flag |= P_UPCALLWAIT;       /* XXX lwp flags */
257             tsleep(&lp->lwp_upcall, PCATCH, "wupcall", 0);
258             lp->lwp_proc->p_flag &= ~P_UPCALLWAIT;      /* XXX lwp flags */
259         }
260         break;
261     default:
262         error = EINVAL;
263         break;
264     }
265     rel_mplock();
266     return(error);
267 }
268
269 void
270 upc_release(struct vmspace *vm, struct lwp *lp)
271 {
272     struct vmupcall **vupp;
273     struct vmupcall *vu;
274
275     vupp = &vm->vm_upcalls;
276     while ((vu = *vupp) != NULL) {
277         if (vu->vu_lwp == lp) {
278             *vupp = vu->vu_next;
279             kfree(vu, M_UPCALL);
280             --vm->vm_upccount;
281         } else {
282             vupp = &vu->vu_next;
283         }
284     }
285 }
286
287 /*
288  * XXX eventually we should sort by vu_pending priority and dispatch
289  * the highest priority upcall first.
290  */
291 void
292 postupcall(struct lwp *lp)
293 {
294     struct vmspace *vm = lp->lwp_proc->p_vmspace;
295     struct vmupcall *vu;
296     struct vmupcall *vu_send = NULL;
297
298     for (vu = vm->vm_upcalls; vu; vu = vu->vu_next) {
299         if (vu->vu_lwp == lp && vu->vu_pending) {
300             if (vu_send) {
301                 sendupcall(vu, 1);
302                 return;
303             }
304             vu_send = vu;
305         }
306     }
307     if (vu_send)
308         sendupcall(vu_send, 0);
309 }
310