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