| Commit | Line | Data |
|---|---|---|
| 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 | ||
| 60 | MALLOC_DEFINE(M_UPCALL, "upcalls", "upcall registration structures"); | |
| 61 | ||
| 62 | #ifdef SMP | |
| 63 | ||
| 64 | static void | |
| 65 | sigupcall_remote(void *arg) | |
| 66 | { | |
| 4170cc8b | 67 | struct lwp *lp = arg; |
| 553ea3c8 | 68 | if (lp == lwkt_preempted_proc()) |
| a722be49 MD |
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) | |
| 3919ced0 MD |
81 | * |
| 82 | * MPALMOSTSAFE | |
| a722be49 MD |
83 | */ |
| 84 | int | |
| 753fd850 | 85 | sys_upc_register(struct upc_register_args *uap) |
| a722be49 | 86 | { |
| 4170cc8b SS |
87 | struct lwp *lp = curthread->td_lwp; |
| 88 | struct vmspace *vm = curproc->p_vmspace; | |
| a722be49 MD |
89 | struct vmupcall *vu; |
| 90 | ||
| 3919ced0 MD |
91 | /* |
| 92 | * Note: inconsequential MP race | |
| 93 | */ | |
| a722be49 MD |
94 | if (vm->vm_upccount >= UPCALL_MAXCOUNT) |
| 95 | return(EFBIG); | |
| 96 | ||
| efda3bd0 | 97 | vu = kmalloc(sizeof(struct vmupcall), M_UPCALL, M_WAITOK|M_ZERO); |
| a722be49 MD |
98 | vu->vu_ctx = uap->ctxfunc; |
| 99 | vu->vu_func = uap->func; | |
| 100 | vu->vu_data = uap->data; | |
| 4170cc8b SS |
101 | vu->vu_lwp = lp; |
| 102 | lp->lwp_upcall = uap->upc; | |
| a722be49 | 103 | |
| 3919ced0 | 104 | get_mplock(); |
| a722be49 MD |
105 | if (vm->vm_upcalls != NULL) |
| 106 | vu->vu_id = vm->vm_upcalls->vu_id + 1; | |
| 107 | else | |
| 1f33f833 | 108 | vu->vu_id = UPC_RESERVED; |
| a722be49 MD |
109 | vu->vu_next = vm->vm_upcalls; |
| 110 | vm->vm_upcalls = vu; | |
| 111 | ++vm->vm_upccount; | |
| 3919ced0 | 112 | rel_mplock(); |
| a722be49 MD |
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) | |
| 3919ced0 MD |
121 | * |
| 122 | * MPALMOSTSAFE | |
| a722be49 MD |
123 | */ |
| 124 | int | |
| 753fd850 | 125 | sys_upc_control(struct upc_control_args *uap) |
| a722be49 | 126 | { |
| 4170cc8b SS |
127 | struct lwp *lp = curthread->td_lwp; |
| 128 | struct lwp *targlp; | |
| 129 | struct vmspace *vms = curproc->p_vmspace; | |
| a722be49 MD |
130 | struct vmupcall *vu; |
| 131 | struct vmupcall *vu_send; | |
| 132 | struct vmupcall **vupp; | |
| 133 | int error; | |
| 134 | ||
| 3919ced0 | 135 | get_mplock(); |
| a722be49 MD |
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 || | |
| 973c11b9 MD |
150 | (uap->upcid == -1 && |
| 151 | vu->vu_pending >= (int)(intptr_t)uap->data && vu->vu_lwp == lp) | |
| a722be49 | 152 | ) { |
| 973c11b9 MD |
153 | if (vu->vu_pending < (int)(intptr_t)uap->data) |
| 154 | vu->vu_pending = (int)(intptr_t)uap->data; | |
| a722be49 | 155 | error = 0; |
| 4170cc8b | 156 | targlp = vu->vu_lwp; |
| 4643740a MD |
157 | targlp->lwp_proc->p_flags |= P_UPCALLPEND; /* XXX lwp flags */ |
| 158 | if (targlp->lwp_proc->p_flags & P_UPCALLWAIT) | |
| 4170cc8b | 159 | wakeup(&targlp->lwp_upcall); |
| a722be49 | 160 | #ifdef SMP |
| 4170cc8b SS |
161 | if (targlp->lwp_thread->td_gd != mycpu) |
| 162 | lwkt_send_ipiq(targlp->lwp_thread->td_gd, sigupcall_remote, targlp); | |
| a722be49 MD |
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) { | |
| 4170cc8b | 194 | if (vu->vu_lwp == lp && vu->vu_pending) { |
| a722be49 MD |
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 | */ | |
| 0a455ac5 | 205 | error = fetchupcall(vu_send, vu != NULL, uap->data); |
| a722be49 MD |
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 || | |
| 4170cc8b | 216 | (uap->upcid == -1 && vu->vu_lwp == lp) |
| a722be49 MD |
217 | ) { |
| 218 | *vupp = vu->vu_next; | |
| 219 | error = 0; | |
| efda3bd0 | 220 | kfree(vu, M_UPCALL); |
| a722be49 MD |
221 | } else { |
| 222 | vupp = &vu->vu_next; | |
| 223 | } | |
| 224 | } | |
| 225 | break; | |
| 226 | case UPC_CONTROL_POLL: | |
| 227 | case UPC_CONTROL_POLLANDCLEAR: | |
| fe8c5e17 | 228 | case UPC_CONTROL_WAIT: |
| a722be49 MD |
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 || | |
| 973c11b9 MD |
241 | (uap->upcid == -1 && |
| 242 | vu->vu_pending >= (int)(intptr_t)uap->data && vu->vu_lwp == lp) | |
| a722be49 MD |
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 | } | |
| fe8c5e17 | 254 | if (uap->cmd == UPC_CONTROL_WAIT && vu == NULL) { |
| 4643740a | 255 | lp->lwp_proc->p_flags |= P_UPCALLWAIT; /* XXX lwp flags */ |
| 4170cc8b | 256 | tsleep(&lp->lwp_upcall, PCATCH, "wupcall", 0); |
| 4643740a | 257 | lp->lwp_proc->p_flags &= ~P_UPCALLWAIT; /* XXX lwp flags */ |
| fe8c5e17 | 258 | } |
| a722be49 MD |
259 | break; |
| 260 | default: | |
| 261 | error = EINVAL; | |
| 262 | break; | |
| 263 | } | |
| 3919ced0 | 264 | rel_mplock(); |
| a722be49 MD |
265 | return(error); |
| 266 | } | |
| 267 | ||
| 268 | void | |
| 4170cc8b | 269 | upc_release(struct vmspace *vm, struct lwp *lp) |
| a722be49 MD |
270 | { |
| 271 | struct vmupcall **vupp; | |
| 272 | struct vmupcall *vu; | |
| 273 | ||
| 274 | vupp = &vm->vm_upcalls; | |
| 275 | while ((vu = *vupp) != NULL) { | |
| 4170cc8b | 276 | if (vu->vu_lwp == lp) { |
| a722be49 | 277 | *vupp = vu->vu_next; |
| efda3bd0 | 278 | kfree(vu, M_UPCALL); |
| a722be49 MD |
279 | --vm->vm_upccount; |
| 280 | } else { | |
| 281 | vupp = &vu->vu_next; | |
| 282 | } | |
| 283 | } | |
| 284 | } | |
| 285 | ||
| 093dd88e MD |
286 | /* |
| 287 | * XXX eventually we should sort by vu_pending priority and dispatch | |
| 288 | * the highest priority upcall first. | |
| 289 | */ | |
| a722be49 | 290 | void |
| 4170cc8b | 291 | postupcall(struct lwp *lp) |
| a722be49 | 292 | { |
| 4170cc8b | 293 | struct vmspace *vm = lp->lwp_proc->p_vmspace; |
| a722be49 MD |
294 | struct vmupcall *vu; |
| 295 | struct vmupcall *vu_send = NULL; | |
| 296 | ||
| 297 | for (vu = vm->vm_upcalls; vu; vu = vu->vu_next) { | |
| 4170cc8b | 298 | if (vu->vu_lwp == lp && vu->vu_pending) { |
| a722be49 MD |
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 |