1 /* $NetBSD: callcontext.c,v 1.21 2008/01/28 18:51:03 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.21 2008/01/28 18:51:03 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 <puffs.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <ucontext.h> 46 #include <unistd.h> 47 48 #include "puffs_priv.h" 49 50 /* 51 * Set the following to 1 to not handle each request on a separate 52 * stack. This is highly volatile kludge, therefore no external 53 * interface. 54 */ 55 int puffs_fakecc; 56 57 /* 58 * user stuff 59 */ 60 61 /* 62 * So, we need to get back to where we came from. This can happen in two 63 * different ways: 64 * 1) PCC_MLCONT is set, in which case we need to go to the mainloop 65 * 2) It is not set, and we simply jump to pcc_uc_ret. 66 */ 67 void 68 puffs_cc_yield(struct puffs_cc *pcc) 69 { 70 struct puffs_cc *jumpcc; 71 int rv; 72 73 assert(puffs_fakecc == 0); 74 75 pcc->pcc_flags &= ~PCC_BORROWED; 76 77 /* romanes eunt domus */ 78 if ((pcc->pcc_flags & PCC_MLCONT) == 0) { 79 swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret); 80 } else { 81 pcc->pcc_flags &= ~PCC_MLCONT; 82 rv = puffs__cc_create(pcc->pcc_pu, puffs__theloop, &jumpcc); 83 if (rv) 84 abort(); /* p-p-p-pa-pa-panic (XXX: fixme) */ 85 swapcontext(&pcc->pcc_uc, &jumpcc->pcc_uc); 86 } 87 } 88 89 /* 90 * Internal continue routine. This has slightly different semantics. 91 * We simply make our cc available in the freelist and jump to the 92 * indicated pcc. 93 */ 94 void 95 puffs__cc_cont(struct puffs_cc *pcc) 96 { 97 struct puffs_cc *mycc; 98 99 mycc = puffs_cc_getcc(pcc->pcc_pu); 100 101 /* 102 * XXX: race between setcontenxt() and recycle if 103 * we go multithreaded 104 */ 105 puffs__cc_destroy(mycc, 1); 106 pcc->pcc_flags |= PCC_MLCONT; 107 setcontext(&pcc->pcc_uc); 108 } 109 110 void 111 puffs_cc_continue(struct puffs_cc *pcc) 112 { 113 114 /* ramble on */ 115 if (puffs_fakecc) 116 pcc->pcc_func(pcc->pcc_farg); 117 else 118 swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc); 119 } 120 121 /* 122 * "Borrows" pcc, *NOT* called from pcc owner. Acts like continue. 123 * So the idea is to use this, give something the context back to 124 * run to completion and then jump back to where ever this was called 125 * from after the op dispatching is complete (or if the pcc decides to 126 * yield again). 127 */ 128 void 129 puffs__goto(struct puffs_cc *loanpcc) 130 { 131 132 loanpcc->pcc_flags |= PCC_BORROWED; 133 134 swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc); 135 } 136 137 void 138 puffs_cc_schedule(struct puffs_cc *pcc) 139 { 140 struct puffs_usermount *pu = pcc->pcc_pu; 141 142 assert(pu->pu_state & PU_INLOOP); 143 TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent); 144 } 145 146 int 147 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid) 148 { 149 150 if ((pcc->pcc_flags & PCC_HASCALLER) == 0) { 151 errno = ESRCH; 152 return -1; 153 } 154 155 if (pid) 156 *pid = pcc->pcc_pid; 157 if (lid) 158 *lid = pcc->pcc_lid; 159 return 0; 160 } 161 162 static struct puffs_cc fakecc; 163 164 static struct puffs_cc * 165 slowccalloc(struct puffs_usermount *pu) 166 { 167 struct puffs_cc *volatile pcc; 168 void *sp; 169 size_t stacksize = 1<<pu->pu_cc_stackshift; 170 long psize = sysconf(_SC_PAGESIZE); 171 172 if (puffs_fakecc) 173 return &fakecc; 174 175 sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE, 176 MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0); 177 if (sp == MAP_FAILED) 178 return NULL; 179 180 pcc = sp; 181 memset(pcc, 0, sizeof(struct puffs_cc)); 182 183 mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE); 184 185 /* initialize both ucontext's */ 186 if (getcontext(&pcc->pcc_uc) == -1) { 187 munmap(pcc, stacksize); 188 return NULL; 189 } 190 if (getcontext(&pcc->pcc_uc_ret) == -1) { 191 munmap(pcc, stacksize); 192 return NULL; 193 } 194 195 return pcc; 196 } 197 198 int 199 puffs__cc_create(struct puffs_usermount *pu, puffs_ccfunc func, 200 struct puffs_cc **pccp) 201 { 202 struct puffs_cc *pcc; 203 size_t stacksize = 1<<pu->pu_cc_stackshift; 204 stack_t *st; 205 206 /* Do we have a cached copy? */ 207 if (pu->pu_cc_nstored == 0) { 208 pcc = slowccalloc(pu); 209 if (pcc == NULL) 210 return -1; 211 pcc->pcc_pu = pu; 212 } else { 213 pcc = LIST_FIRST(&pu->pu_ccmagazin); 214 assert(pcc != NULL); 215 216 LIST_REMOVE(pcc, pcc_rope); 217 pu->pu_cc_nstored--; 218 } 219 assert(pcc->pcc_pu == pu); 220 221 if (puffs_fakecc) { 222 pcc->pcc_func = func; 223 pcc->pcc_farg = pcc; 224 } else { 225 /* link context */ 226 pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret; 227 228 /* setup stack 229 * 230 * XXX: I guess this should theoretically be preserved by 231 * swapcontext(). However, it gets lost. So reinit it. 232 */ 233 st = &pcc->pcc_uc.uc_stack; 234 st->ss_sp = pcc; 235 st->ss_size = stacksize; 236 st->ss_flags = 0; 237 238 /* 239 * Give us an initial context to jump to. 240 * 241 * Our manual page says that portable code shouldn't 242 * rely on being able to pass pointers through makecontext(). 243 * kjk says that NetBSD code doesn't need to worry about this. 244 * uwe says it would be like putting a "keep away from 245 * children" sign on a box of toys. 246 */ 247 makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc); 248 } 249 250 *pccp = pcc; 251 return 0; 252 } 253 254 void 255 puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid) 256 { 257 258 pcc->pcc_pid = pid; 259 pcc->pcc_lid = lid; 260 pcc->pcc_flags |= PCC_HASCALLER; 261 } 262 263 void 264 puffs__cc_destroy(struct puffs_cc *pcc, int nonuke) 265 { 266 struct puffs_usermount *pu = pcc->pcc_pu; 267 size_t stacksize = 1<<pu->pu_cc_stackshift; 268 269 assert(pcc->pcc_flags == 0); 270 271 /* not over limit? stuff away in the store, otherwise nuke */ 272 if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) { 273 pcc->pcc_pb = NULL; 274 LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope); 275 pu->pu_cc_nstored++; 276 } else { 277 assert(!puffs_fakecc); 278 munmap(pcc, stacksize); 279 } 280 } 281 282 struct puffs_cc * 283 puffs_cc_getcc(struct puffs_usermount *pu) 284 { 285 size_t stacksize = 1<<pu->pu_cc_stackshift; 286 uintptr_t bottom; 287 288 if (puffs_fakecc) 289 return &fakecc; 290 291 bottom = ((uintptr_t)&bottom) & ~(stacksize-1); 292 return (struct puffs_cc *)bottom; 293 } 294