Do not run mandoc for lintmanpages if MANPAGES is empty.
[netbsd-mini2440.git] / lib / libpuffs / callcontext.c
blob81e855a3aed84953ce3b0093872ac2a4f66c671e
1 /* $NetBSD: callcontext.c,v 1.22 2008/08/11 15:59:01 pooka Exp $ */
3 /*
4 * Copyright (c) 2006, 2007, 2008 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by the
7 * Research Foundation of Helsinki University of Technology
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.
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.
31 #include <sys/cdefs.h>
32 #if !defined(lint)
33 __RCSID("$NetBSD: callcontext.c,v 1.22 2008/08/11 15:59:01 pooka Exp $");
34 #endif /* !lint */
36 #include <sys/types.h>
37 #include <sys/mman.h>
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>
48 #include "puffs_priv.h"
50 #if 0
51 #define DPRINTF(x) printf x
52 #else
53 #define DPRINTF(x)
54 #endif
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.
61 int puffs_fakecc;
64 * user stuff
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.
73 void
74 puffs_cc_yield(struct puffs_cc *pcc)
76 struct puffs_cc *jumpcc;
77 int rv;
79 assert(puffs_fakecc == 0);
81 pcc->pcc_flags &= ~PCC_BORROWED;
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));
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.
104 void
105 puffs__cc_cont(struct puffs_cc *pcc)
107 struct puffs_cc *mycc;
109 mycc = puffs_cc_getcc(pcc->pcc_pu);
110 DPRINTF(("puffs__cc_cont: pcc %p, mycc %p\n", pcc, mycc));
113 * XXX: race between setcontenxt() and recycle if
114 * we go multithreaded
116 puffs__cc_destroy(mycc, 1);
117 pcc->pcc_flags |= PCC_MLCONT;
118 setcontext(&pcc->pcc_uc);
121 void
122 puffs_cc_continue(struct puffs_cc *pcc)
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);
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).
141 void
142 puffs__goto(struct puffs_cc *loanpcc)
145 loanpcc->pcc_flags |= PCC_BORROWED;
147 swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
150 void
151 puffs_cc_schedule(struct puffs_cc *pcc)
153 struct puffs_usermount *pu = pcc->pcc_pu;
155 assert(pu->pu_state & PU_INLOOP);
156 TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent);
160 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
163 if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
164 errno = ESRCH;
165 return -1;
168 if (pid)
169 *pid = pcc->pcc_pid;
170 if (lid)
171 *lid = pcc->pcc_lid;
172 return 0;
175 static struct puffs_cc fakecc;
177 static struct puffs_cc *
178 slowccalloc(struct puffs_usermount *pu)
180 struct puffs_cc *volatile pcc;
181 void *sp;
182 size_t stacksize = 1<<pu->pu_cc_stackshift;
183 long psize = sysconf(_SC_PAGESIZE);
185 if (puffs_fakecc)
186 return &fakecc;
188 sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
189 MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0);
190 if (sp == MAP_FAILED)
191 return NULL;
193 pcc = sp;
194 memset(pcc, 0, sizeof(struct puffs_cc));
196 mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE);
198 /* initialize both ucontext's */
199 if (getcontext(&pcc->pcc_uc) == -1) {
200 munmap(pcc, stacksize);
201 return NULL;
203 if (getcontext(&pcc->pcc_uc_ret) == -1) {
204 munmap(pcc, stacksize);
205 return NULL;
208 return pcc;
212 puffs__cc_create(struct puffs_usermount *pu, puffs_ccfunc func,
213 struct puffs_cc **pccp)
215 struct puffs_cc *pcc;
216 size_t stacksize = 1<<pu->pu_cc_stackshift;
217 stack_t *st;
219 /* Do we have a cached copy? */
220 if (pu->pu_cc_nstored == 0) {
221 pcc = slowccalloc(pu);
222 if (pcc == NULL)
223 return -1;
224 pcc->pcc_pu = pu;
225 DPRINTF(("puffs__cc_create: allocated pcc %p\n", pcc));
226 } else {
227 pcc = LIST_FIRST(&pu->pu_ccmagazin);
228 assert(pcc != NULL);
230 LIST_REMOVE(pcc, pcc_rope);
231 pu->pu_cc_nstored--;
232 DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc));
234 assert(pcc->pcc_pu == pu);
236 if (puffs_fakecc) {
237 pcc->pcc_func = func;
238 pcc->pcc_farg = pcc;
239 } else {
240 /* link context */
241 pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
243 /* setup stack
245 * XXX: I guess this should theoretically be preserved by
246 * swapcontext(). However, it gets lost. So reinit it.
248 st = &pcc->pcc_uc.uc_stack;
249 st->ss_sp = pcc;
250 st->ss_size = stacksize;
251 st->ss_flags = 0;
254 * Give us an initial context to jump to.
256 * Our manual page says that portable code shouldn't
257 * rely on being able to pass pointers through makecontext().
258 * kjk says that NetBSD code doesn't need to worry about this.
259 * uwe says it would be like putting a "keep away from
260 * children" sign on a box of toys.
262 makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc);
265 *pccp = pcc;
266 return 0;
269 void
270 puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
273 pcc->pcc_pid = pid;
274 pcc->pcc_lid = lid;
275 pcc->pcc_flags |= PCC_HASCALLER;
278 static void
279 cc_free(struct puffs_cc *pcc)
281 struct puffs_usermount *pu = pcc->pcc_pu;
282 size_t stacksize = 1<<pu->pu_cc_stackshift;
284 DPRINTF(("invalidating pcc %p\n", pcc));
285 assert(!puffs_fakecc);
286 munmap(pcc, stacksize);
289 void
290 puffs__cc_destroy(struct puffs_cc *pcc, int nonuke)
292 struct puffs_usermount *pu = pcc->pcc_pu;
294 assert(pcc->pcc_flags == 0);
295 assert(!puffs_fakecc);
297 /* not over limit? stuff away in the store, otherwise nuke */
298 if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) {
299 pcc->pcc_pb = NULL;
300 DPRINTF(("puffs__cc_destroy: storing pcc %p\n", pcc));
301 LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
302 pu->pu_cc_nstored++;
303 } else {
304 cc_free(pcc);
308 void
309 puffs__cc_exit(struct puffs_usermount *pu)
311 struct puffs_cc *pcc;
313 while ((pcc = LIST_FIRST(&pu->pu_ccmagazin)) != NULL) {
314 LIST_REMOVE(pcc, pcc_rope);
315 cc_free(pcc);
319 struct puffs_cc *
320 puffs_cc_getcc(struct puffs_usermount *pu)
322 size_t stacksize = 1<<pu->pu_cc_stackshift;
323 uintptr_t bottom;
325 if (puffs_fakecc)
326 return &fakecc;
328 bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
329 return (struct puffs_cc *)bottom;
333 puffs__cc_savemain(struct puffs_usermount *pu)
336 if (puffs_fakecc)
337 return 0;
339 PU_CLRSFLAG(pu, PU_MAINRESTORE);
340 return getcontext(&pu->pu_mainctx);
344 puffs__cc_restoremain(struct puffs_usermount *pu)
347 if (puffs_fakecc)
348 return 0;
350 puffs__cc_destroy(puffs_cc_getcc(pu), 1);
351 PU_SETSFLAG(pu, PU_MAINRESTORE);
352 return setcontext(&pu->pu_mainctx);