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