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