HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / kern / kern_upcall.c
blob0d05377700c47c78acd7834f7b085183a22f6b10
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 <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>
59 #include <machine/cpu.h>
61 MALLOC_DEFINE(M_UPCALL, "upcalls", "upcall registration structures");
63 #ifdef SMP
65 static void
66 sigupcall_remote(void *arg)
68 struct lwp *lp = arg;
69 if (lp == lwkt_preempted_proc())
70 sigupcall();
73 #endif
76 * upc_register:
78 * Register an upcall context wrapper and procedure. Note that the
79 * upcall context is set globally for the process, not for each upcall.
81 * ARGS(struct upcall *upc, upcall_func_t ctx, upcall_func_t func, void *data)
83 int
84 sys_upc_register(struct upc_register_args *uap)
86 struct lwp *lp = curthread->td_lwp;
87 struct vmspace *vm = curproc->p_vmspace;
88 struct vmupcall *vu;
90 if (vm->vm_upccount >= UPCALL_MAXCOUNT)
91 return(EFBIG);
93 vu = kmalloc(sizeof(struct vmupcall), M_UPCALL, M_WAITOK|M_ZERO);
94 vu->vu_ctx = uap->ctxfunc;
95 vu->vu_func = uap->func;
96 vu->vu_data = uap->data;
97 vu->vu_lwp = lp;
98 lp->lwp_upcall = uap->upc;
100 if (vm->vm_upcalls != NULL)
101 vu->vu_id = vm->vm_upcalls->vu_id + 1;
102 else
103 vu->vu_id = UPC_RESERVED;
104 vu->vu_next = vm->vm_upcalls;
105 vm->vm_upcalls = vu;
106 ++vm->vm_upccount;
107 uap->sysmsg_result = vu->vu_id;
108 return(0);
112 * upc_control:
114 * ARGS(int cmd, int upcid, void *data)
117 sys_upc_control(struct upc_control_args *uap)
119 struct lwp *lp = curthread->td_lwp;
120 struct lwp *targlp;
121 struct vmspace *vms = curproc->p_vmspace;
122 struct vmupcall *vu;
123 struct vmupcall *vu_send;
124 struct vmupcall **vupp;
125 int error;
127 switch(uap->cmd) {
128 case UPC_CONTROL_DISPATCH:
130 * Dispatch the specified upcall id or the next pending id if -1.
131 * the upcall will be marked pending but an actual upcall will only
132 * occur if userland is not in a critical section and the userland
133 * pending bit is not set.
135 * You can dispatch an upcall associated with your process or another
136 * process sharing the same VM space.
138 error = (uap->upcid == -1) ? 0 : ENOENT;
139 for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
140 if (vu->vu_id == uap->upcid ||
141 (uap->upcid == -1 && vu->vu_pending >= (int)uap->data && vu->vu_lwp == lp)
143 if (vu->vu_pending < (int)uap->data)
144 vu->vu_pending = (int)uap->data;
145 error = 0;
146 targlp = vu->vu_lwp;
147 targlp->lwp_proc->p_flag |= P_UPCALLPEND; /* XXX lwp flags */
148 if (targlp->lwp_proc->p_flag & P_UPCALLWAIT)
149 wakeup(&targlp->lwp_upcall);
150 #ifdef SMP
151 if (targlp->lwp_thread->td_gd != mycpu)
152 lwkt_send_ipiq(targlp->lwp_thread->td_gd, sigupcall_remote, targlp);
153 else
154 sigupcall();
155 #else
156 sigupcall();
157 #endif
158 break;
161 break;
162 case UPC_CONTROL_NEXT:
164 * This is used by the context code to fetch the next pending upcall.
165 * The context code has two choices: (A) it can drop
166 * upcall->crit_count and set upcall->pending then make this call
167 * unconditionally or * (B) it can drop upcall->crit_count and then
168 * test upcall->pending and only make this call if upcall->pending
169 * is set. If upcall->pending is clear the context code can pop
170 * the upcall stack itself and return without entering into the kernel
171 * again. (B) is more efficient but leaves a small window of
172 * opportunity where multiple upcalls can pushdown the stack.
174 * If another upcall is pending the crit_count will be bumped and
175 * the function, data, and context pointers will be returned in
176 * registers (C cannot call this routine). If no more upcalls are
177 * pending the pending bit will be cleared and the 'data' argument
178 * is expected to be pointing at the upcall context which we will
179 * then pop, returning to the original code that was interrupted
180 * (NOT the context code).
182 vu_send = NULL;
183 for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
184 if (vu->vu_lwp == lp && vu->vu_pending) {
185 if (vu_send)
186 break;
187 vu_send = vu;
191 * vu_send may be NULL, indicating that no more upcalls are pending
192 * for this cpu. We set the userland pending bit based on whether
193 * additional upcalls are pending or not.
195 error = fetchupcall(vu_send, vu != NULL, uap->data);
196 break;
197 case UPC_CONTROL_DELETE:
199 * Delete the specified upcall id. If the upcall id is -1, delete
200 * all upcall id's associated with the current process.
202 error = (uap->upcid == -1) ? 0 : ENOENT;
203 vupp = &vms->vm_upcalls;
204 while ((vu = *vupp) != NULL) {
205 if (vu->vu_id == uap->upcid ||
206 (uap->upcid == -1 && vu->vu_lwp == lp)
208 *vupp = vu->vu_next;
209 error = 0;
210 kfree(vu, M_UPCALL);
211 } else {
212 vupp = &vu->vu_next;
215 break;
216 case UPC_CONTROL_POLL:
217 case UPC_CONTROL_POLLANDCLEAR:
218 case UPC_CONTROL_WAIT:
220 * If upcid is -1 poll for the first pending upcall and return the
221 * id or 0 if no upcalls are pending.
223 * If upcid is a particular upcall then poll that upcall and return
224 * its pending status (0 or 1). For POLLANDCLEAR, also clear the
225 * pending status. The userland pending bit is not modified by
226 * this call (maybe we should modify it for poll-and-clear).
228 error = (uap->upcid == -1) ? 0 : ENOENT;
229 for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
230 if (vu->vu_id == uap->upcid ||
231 (uap->upcid == -1 && vu->vu_pending >= (int)uap->data && vu->vu_lwp == lp)
233 error = 0;
234 if (uap->upcid == -1)
235 uap->sysmsg_result = vu->vu_id;
236 else
237 uap->sysmsg_result = vu->vu_pending;
238 if (uap->cmd == UPC_CONTROL_POLLANDCLEAR)
239 vu->vu_pending = 0;
240 break;
243 if (uap->cmd == UPC_CONTROL_WAIT && vu == NULL) {
244 lp->lwp_proc->p_flag |= P_UPCALLWAIT; /* XXX lwp flags */
245 tsleep(&lp->lwp_upcall, PCATCH, "wupcall", 0);
246 lp->lwp_proc->p_flag &= ~P_UPCALLWAIT; /* XXX lwp flags */
248 break;
249 default:
250 error = EINVAL;
251 break;
253 return(error);
256 void
257 upc_release(struct vmspace *vm, struct lwp *lp)
259 struct vmupcall **vupp;
260 struct vmupcall *vu;
262 vupp = &vm->vm_upcalls;
263 while ((vu = *vupp) != NULL) {
264 if (vu->vu_lwp == lp) {
265 *vupp = vu->vu_next;
266 kfree(vu, M_UPCALL);
267 --vm->vm_upccount;
268 } else {
269 vupp = &vu->vu_next;
275 * XXX eventually we should sort by vu_pending priority and dispatch
276 * the highest priority upcall first.
278 void
279 postupcall(struct lwp *lp)
281 struct vmspace *vm = lp->lwp_proc->p_vmspace;
282 struct vmupcall *vu;
283 struct vmupcall *vu_send = NULL;
285 for (vu = vm->vm_upcalls; vu; vu = vu->vu_next) {
286 if (vu->vu_lwp == lp && vu->vu_pending) {
287 if (vu_send) {
288 sendupcall(vu, 1);
289 return;
291 vu_send = vu;
294 if (vu_send)
295 sendupcall(vu_send, 0);