1 /* $NetBSD: callcontext.c,v 1.23 2008/08/11 16:23:37 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2007, 2008 Antti Kantee. All Rights Reserved. 5 * 6 * Development of this software was supported by the 7 * Research Foundation of Helsinki University of Technology 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 #if !defined(lint) 33 __RCSID("$NetBSD: callcontext.c,v 1.23 2008/08/11 16:23:37 pooka Exp $"); 34 #endif /* !lint */ 35 36 #include <sys/types.h> 37 #include <sys/mman.h> 38 39 #include <assert.h> 40 #include <errno.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <ucontext.h> 45 #include <unistd.h> 46 47 #include "puffs.h" 48 #include "puffs_priv.h" 49 50 #if 0 51 #define DPRINTF(x) printf x 52 #else 53 #define DPRINTF(x) 54 #endif 55 56 /* 57 * Set the following to 1 to not handle each request on a separate 58 * stack. This is highly volatile kludge, therefore no external 59 * interface. 60 */ 61 int puffs_fakecc; 62 63 /* 64 * user stuff 65 */ 66 67 /* 68 * So, we need to get back to where we came from. This can happen in two 69 * different ways: 70 * 1) PCC_MLCONT is set, in which case we need to go to the mainloop 71 * 2) It is not set, and we simply jump to pcc_uc_ret. 72 */ 73 void 74 puffs_cc_yield(struct puffs_cc *pcc) 75 { 76 struct puffs_cc *jumpcc; 77 int rv; 78 79 assert(puffs_fakecc == 0); 80 81 pcc->pcc_flags &= ~PCC_BORROWED; 82 83 /* romanes eunt domus */ 84 DPRINTF(("puffs_cc_yield: ")); 85 if ((pcc->pcc_flags & PCC_MLCONT) == 0) { 86 DPRINTF(("no mlcont, pcc %p\n", pcc)); 87 swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret); 88 } else { 89 DPRINTF(("mlcont, pcc %p\n", pcc)); 90 pcc->pcc_flags &= ~PCC_MLCONT; 91 rv = puffs__cc_create(pcc->pcc_pu, puffs__theloop, &jumpcc); 92 if (rv) 93 abort(); /* p-p-p-pa-pa-panic (XXX: fixme) */ 94 swapcontext(&pcc->pcc_uc, &jumpcc->pcc_uc); 95 DPRINTF(("puffs_cc_yield: post swap pcc %p\n", pcc)); 96 } 97 } 98 99 /* 100 * Internal continue routine. This has slightly different semantics. 101 * We simply make our cc available in the freelist and jump to the 102 * indicated pcc. 103 */ 104 void 105 puffs__cc_cont(struct puffs_cc *pcc) 106 { 107 struct puffs_cc *mycc; 108 109 mycc = puffs_cc_getcc(pcc->pcc_pu); 110 DPRINTF(("puffs__cc_cont: pcc %p, mycc %p\n", pcc, mycc)); 111 112 /* 113 * XXX: race between setcontenxt() and recycle if 114 * we go multithreaded 115 */ 116 puffs__cc_destroy(mycc, 1); 117 pcc->pcc_flags |= PCC_MLCONT; 118 setcontext(&pcc->pcc_uc); 119 } 120 121 void 122 puffs_cc_continue(struct puffs_cc *pcc) 123 { 124 125 /* ramble on */ 126 DPRINTF(("puffs_cc_continue: pcc %p\n", pcc)); 127 if (puffs_fakecc) { 128 pcc->pcc_func(pcc->pcc_farg); 129 } else { 130 swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc); 131 } 132 } 133 134 /* 135 * "Borrows" pcc, *NOT* called from pcc owner. Acts like continue. 136 * So the idea is to use this, give something the context back to 137 * run to completion and then jump back to where ever this was called 138 * from after the op dispatching is complete (or if the pcc decides to 139 * yield again). 140 */ 141 void 142 puffs__goto(struct puffs_cc *loanpcc) 143 { 144 145 loanpcc->pcc_flags |= PCC_BORROWED; 146 147 swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc); 148 } 149 150 void 151 puffs_cc_schedule(struct puffs_cc *pcc) 152 { 153 struct puffs_usermount *pu = pcc->pcc_pu; 154 155 assert(pu->pu_state & PU_INLOOP); 156 TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent); 157 } 158 159 int 160 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid) 161 { 162 163 if ((pcc->pcc_flags & PCC_HASCALLER) == 0) { 164 errno = ESRCH; 165 return -1; 166 } 167 168 if (pid) 169 *pid = pcc->pcc_pid; 170 if (lid) 171 *lid = pcc->pcc_lid; 172 return 0; 173 } 174 175 static struct puffs_cc fakecc; 176 177 static struct puffs_cc * 178 slowccalloc(struct puffs_usermount *pu) 179 { 180 struct puffs_cc *volatile pcc; 181 void *sp; 182 size_t stacksize = 1<<pu->pu_cc_stackshift; 183 184 if (puffs_fakecc) 185 return &fakecc; 186 187 sp = minix_mmap(NULL, stacksize, PROT_READ|PROT_WRITE, 188 MAP_ANON|MAP_PRIVATE, -1, 0); 189 if (sp == MAP_FAILED) 190 return NULL; 191 192 pcc = sp; 193 memset(pcc, 0, sizeof(struct puffs_cc)); 194 195 /* initialize both ucontext's */ 196 if (getcontext(&pcc->pcc_uc) == -1) { 197 minix_munmap(pcc, stacksize); 198 return NULL; 199 } 200 if (getcontext(&pcc->pcc_uc_ret) == -1) { 201 minix_munmap(pcc, stacksize); 202 return NULL; 203 } 204 205 return pcc; 206 } 207 208 int 209 puffs__cc_create(struct puffs_usermount *pu, puffs_ccfunc func, 210 struct puffs_cc **pccp) 211 { 212 struct puffs_cc *pcc; 213 size_t stacksize = 1<<pu->pu_cc_stackshift; 214 stack_t *st; 215 216 /* Do we have a cached copy? */ 217 if (pu->pu_cc_nstored == 0) { 218 pcc = slowccalloc(pu); 219 if (pcc == NULL) 220 return -1; 221 pcc->pcc_pu = pu; 222 DPRINTF(("puffs__cc_create: allocated pcc %p\n", pcc)); 223 } else { 224 pcc = LIST_FIRST(&pu->pu_ccmagazin); 225 assert(pcc != NULL); 226 227 LIST_REMOVE(pcc, pcc_rope); 228 pu->pu_cc_nstored--; 229 DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc)); 230 } 231 assert(pcc->pcc_pu == pu); 232 233 if (puffs_fakecc) { 234 pcc->pcc_func = func; 235 pcc->pcc_farg = pcc; 236 } else { 237 /* link context */ 238 pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret; 239 240 /* setup stack 241 * 242 * XXX: I guess this should theoretically be preserved by 243 * swapcontext(). However, it gets lost. So reinit it. 244 */ 245 st = &pcc->pcc_uc.uc_stack; 246 st->ss_sp = pcc; 247 st->ss_size = stacksize; 248 st->ss_flags = 0; 249 250 /* 251 * Give us an initial context to jump to. 252 * 253 * Our manual page says that portable code shouldn't 254 * rely on being able to pass pointers through makecontext(). 255 * kjk says that NetBSD code doesn't need to worry about this. 256 * uwe says it would be like putting a "keep away from 257 * children" sign on a box of toys. 258 */ 259 makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc); 260 } 261 262 *pccp = pcc; 263 return 0; 264 } 265 266 void 267 puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid) 268 { 269 270 pcc->pcc_pid = pid; 271 pcc->pcc_lid = lid; 272 pcc->pcc_flags |= PCC_HASCALLER; 273 } 274 275 static void 276 cc_free(struct puffs_cc *pcc) 277 { 278 struct puffs_usermount *pu = pcc->pcc_pu; 279 size_t stacksize = 1<<pu->pu_cc_stackshift; 280 281 DPRINTF(("invalidating pcc %p\n", pcc)); 282 assert(!puffs_fakecc); 283 minix_munmap(pcc, stacksize); 284 } 285 286 void 287 puffs__cc_destroy(struct puffs_cc *pcc, int nonuke) 288 { 289 struct puffs_usermount *pu = pcc->pcc_pu; 290 291 assert(pcc->pcc_flags == 0); 292 assert(!puffs_fakecc); 293 294 /* not over limit? stuff away in the store, otherwise nuke */ 295 if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) { 296 pcc->pcc_pb = NULL; 297 DPRINTF(("puffs__cc_destroy: storing pcc %p\n", pcc)); 298 LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope); 299 pu->pu_cc_nstored++; 300 } else { 301 cc_free(pcc); 302 } 303 } 304 305 void 306 puffs__cc_exit(struct puffs_usermount *pu) 307 { 308 struct puffs_cc *pcc; 309 310 while ((pcc = LIST_FIRST(&pu->pu_ccmagazin)) != NULL) { 311 LIST_REMOVE(pcc, pcc_rope); 312 cc_free(pcc); 313 } 314 } 315 316 struct puffs_cc * 317 puffs_cc_getcc(struct puffs_usermount *pu) 318 { 319 size_t stacksize = 1<<pu->pu_cc_stackshift; 320 uintptr_t bottom; 321 322 if (puffs_fakecc) 323 return &fakecc; 324 325 bottom = ((uintptr_t)&bottom) & ~(stacksize-1); 326 return (struct puffs_cc *)bottom; 327 } 328 329 int 330 puffs__cc_savemain(struct puffs_usermount *pu) 331 { 332 333 if (puffs_fakecc) 334 return 0; 335 336 PU_CLRSFLAG(pu, PU_MAINRESTORE); 337 return getcontext(&pu->pu_mainctx); 338 } 339 340 int 341 puffs__cc_restoremain(struct puffs_usermount *pu) 342 { 343 344 if (puffs_fakecc) 345 return 0; 346 347 puffs__cc_destroy(puffs_cc_getcc(pu), 1); 348 PU_SETSFLAG(pu, PU_MAINRESTORE); 349 return setcontext(&pu->pu_mainctx); 350 } 351