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