2 * Copyright (c) 2003,2004,2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
36 * Implement upcall registration and dispatch.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/upcall.h>
44 #include <sys/thread2.h>
45 #include <sys/malloc.h>
46 #include <sys/sysproto.h>
48 #include <sys/signalvar.h>
50 #include <sys/mplock2.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_kern.h>
56 #include <vm/vm_map.h>
58 #include <machine/cpu.h>
60 MALLOC_DEFINE(M_UPCALL, "upcalls", "upcall registration structures");
65 sigupcall_remote(void *arg)
68 if (lp == lwkt_preempted_proc())
77 * Register an upcall context wrapper and procedure. Note that the
78 * upcall context is set globally for the process, not for each upcall.
80 * ARGS(struct upcall *upc, upcall_func_t ctx, upcall_func_t func, void *data)
85 sys_upc_register(struct upc_register_args *uap)
87 struct lwp *lp = curthread->td_lwp;
88 struct vmspace *vm = curproc->p_vmspace;
92 * Note: inconsequential MP race
94 if (vm->vm_upccount >= UPCALL_MAXCOUNT)
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;
102 lp->lwp_upcall = uap->upc;
105 if (vm->vm_upcalls != NULL)
106 vu->vu_id = vm->vm_upcalls->vu_id + 1;
108 vu->vu_id = UPC_RESERVED;
109 vu->vu_next = vm->vm_upcalls;
113 uap->sysmsg_result = vu->vu_id;
120 * ARGS(int cmd, int upcid, void *data)
125 sys_upc_control(struct upc_control_args *uap)
127 struct lwp *lp = curthread->td_lwp;
129 struct vmspace *vms = curproc->p_vmspace;
131 struct vmupcall *vu_send;
132 struct vmupcall **vupp;
137 case UPC_CONTROL_DISPATCH:
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.
144 * You can dispatch an upcall associated with your process or another
145 * process sharing the same VM space.
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 ||
151 vu->vu_pending >= (int)(intptr_t)uap->data && vu->vu_lwp == lp)
153 if (vu->vu_pending < (int)(intptr_t)uap->data)
154 vu->vu_pending = (int)(intptr_t)uap->data;
157 targlp->lwp_proc->p_flags |= P_UPCALLPEND; /* XXX lwp flags */
158 if (targlp->lwp_proc->p_flags & P_UPCALLWAIT)
159 wakeup(&targlp->lwp_upcall);
161 if (targlp->lwp_thread->td_gd != mycpu)
162 lwkt_send_ipiq(targlp->lwp_thread->td_gd, sigupcall_remote, targlp);
172 case UPC_CONTROL_NEXT:
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.
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).
193 for (vu = vms->vm_upcalls; vu; vu = vu->vu_next) {
194 if (vu->vu_lwp == lp && vu->vu_pending) {
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.
205 error = fetchupcall(vu_send, vu != NULL, uap->data);
207 case UPC_CONTROL_DELETE:
209 * Delete the specified upcall id. If the upcall id is -1, delete
210 * all upcall id's associated with the current process.
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)
226 case UPC_CONTROL_POLL:
227 case UPC_CONTROL_POLLANDCLEAR:
228 case UPC_CONTROL_WAIT:
230 * If upcid is -1 poll for the first pending upcall and return the
231 * id or 0 if no upcalls are pending.
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).
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 ||
242 vu->vu_pending >= (int)(intptr_t)uap->data && vu->vu_lwp == lp)
245 if (uap->upcid == -1)
246 uap->sysmsg_result = vu->vu_id;
248 uap->sysmsg_result = vu->vu_pending;
249 if (uap->cmd == UPC_CONTROL_POLLANDCLEAR)
254 if (uap->cmd == UPC_CONTROL_WAIT && vu == NULL) {
255 lp->lwp_proc->p_flags |= P_UPCALLWAIT; /* XXX lwp flags */
256 tsleep(&lp->lwp_upcall, PCATCH, "wupcall", 0);
257 lp->lwp_proc->p_flags &= ~P_UPCALLWAIT; /* XXX lwp flags */
269 upc_release(struct vmspace *vm, struct lwp *lp)
271 struct vmupcall **vupp;
274 vupp = &vm->vm_upcalls;
275 while ((vu = *vupp) != NULL) {
276 if (vu->vu_lwp == lp) {
287 * XXX eventually we should sort by vu_pending priority and dispatch
288 * the highest priority upcall first.
291 postupcall(struct lwp *lp)
293 struct vmspace *vm = lp->lwp_proc->p_vmspace;
295 struct vmupcall *vu_send = NULL;
297 for (vu = vm->vm_upcalls; vu; vu = vu->vu_next) {
298 if (vu->vu_lwp == lp && vu->vu_pending) {
307 sendupcall(vu_send, 0);