1 /* $NetBSD: callcontext.c,v 1.27 2011/12/06 21:15:39 skrll Exp $ */
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
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
31 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: callcontext.c,v 1.27 2011/12/06 21:15:39 skrll Exp $");
36 #include <sys/types.h>
48 #include "puffs_priv.h"
51 #define DPRINTF(x) printf x
57 * Set the following to 1 to not handle each request on a separate
58 * stack. This is highly volatile kludge, therefore no external
68 * So, we need to get back to where we came from. This can happen in two
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.
74 puffs_cc_yield(struct puffs_cc
*pcc
)
76 struct puffs_cc
*jumpcc
;
79 assert(puffs_fakecc
== 0);
81 if ((~pcc
->pcc_flags
& (PCC_BORROWED
|PCC_DONE
)) == 0) {
82 pcc
->pcc_flags
&= ~(PCC_BORROWED
|PCC_DONE
);
84 * see the XXX comment in puffs__cc_cont
86 puffs__cc_destroy(pcc
, 1);
87 setcontext(&pcc
->pcc_uc_ret
);
89 pcc
->pcc_flags
&= ~PCC_BORROWED
;
91 /* romanes eunt domus */
92 DPRINTF(("puffs_cc_yield: "));
93 if ((pcc
->pcc_flags
& PCC_MLCONT
) == 0) {
94 DPRINTF(("no mlcont, pcc %p\n", pcc
));
95 swapcontext(&pcc
->pcc_uc
, &pcc
->pcc_uc_ret
);
97 DPRINTF(("mlcont, pcc %p\n", pcc
));
98 pcc
->pcc_flags
&= ~PCC_MLCONT
;
99 rv
= puffs__cc_create(pcc
->pcc_pu
, puffs__theloop
, &jumpcc
);
101 abort(); /* p-p-p-pa-pa-panic (XXX: fixme) */
102 swapcontext(&pcc
->pcc_uc
, &jumpcc
->pcc_uc
);
103 DPRINTF(("puffs_cc_yield: post swap pcc %p\n", pcc
));
108 * Internal continue routine. This has slightly different semantics.
109 * We simply make our cc available in the freelist and jump to the
113 puffs__cc_cont(struct puffs_cc
*pcc
)
115 struct puffs_cc
*mycc
;
117 mycc
= puffs_cc_getcc(pcc
->pcc_pu
);
118 DPRINTF(("puffs__cc_cont: pcc %p, mycc %p\n", pcc
, mycc
));
121 * XXX: race between setcontext() and recycle if
122 * we go multithreaded
124 puffs__cc_destroy(mycc
, 1);
125 pcc
->pcc_flags
|= PCC_MLCONT
;
126 setcontext(&pcc
->pcc_uc
);
130 puffs_cc_continue(struct puffs_cc
*pcc
)
134 DPRINTF(("puffs_cc_continue: pcc %p\n", pcc
));
136 pcc
->pcc_func(pcc
->pcc_farg
);
138 swapcontext(&pcc
->pcc_uc_ret
, &pcc
->pcc_uc
);
143 * "Borrows" pcc, *NOT* called from pcc owner. Acts like continue.
144 * So the idea is to use this, give something the context back to
145 * run to completion and then jump back to where ever this was called
146 * from after the op dispatching is complete (or if the pcc decides to
150 puffs__goto(struct puffs_cc
*loanpcc
)
153 loanpcc
->pcc_flags
|= PCC_BORROWED
;
155 swapcontext(&loanpcc
->pcc_uc_ret
, &loanpcc
->pcc_uc
);
159 puffs_cc_schedule(struct puffs_cc
*pcc
)
161 struct puffs_usermount
*pu
= pcc
->pcc_pu
;
163 assert(pu
->pu_state
& PU_INLOOP
);
164 TAILQ_INSERT_TAIL(&pu
->pu_sched
, pcc
, pcc_schedent
);
168 puffs_cc_getcaller(struct puffs_cc
*pcc
, pid_t
*pid
, lwpid_t
*lid
)
171 if ((pcc
->pcc_flags
& PCC_HASCALLER
) == 0) {
183 static struct puffs_cc fakecc
;
185 static struct puffs_cc
*
186 slowccalloc(struct puffs_usermount
*pu
)
188 struct puffs_cc
*volatile pcc
;
190 size_t stacksize
= 1<<pu
->pu_cc_stackshift
;
195 sp
= mmap(NULL
, stacksize
, PROT_READ
|PROT_WRITE
,
196 MAP_ANON
|MAP_PRIVATE
, -1, 0);
197 if (sp
== MAP_FAILED
)
201 memset(pcc
, 0, sizeof(struct puffs_cc
));
203 /* initialize both ucontext's */
204 if (getcontext(&pcc
->pcc_uc
) == -1) {
205 munmap(pcc
, stacksize
);
208 if (getcontext(&pcc
->pcc_uc_ret
) == -1) {
209 munmap(pcc
, stacksize
);
217 puffs__cc_create(struct puffs_usermount
*pu
, puffs_ccfunc func
,
218 struct puffs_cc
**pccp
)
220 struct puffs_cc
*pcc
;
221 size_t stacksize
= 1<<pu
->pu_cc_stackshift
;
224 /* Do we have a cached copy? */
225 if (pu
->pu_cc_nstored
== 0) {
226 pcc
= slowccalloc(pu
);
230 DPRINTF(("puffs__cc_create: allocated pcc %p\n", pcc
));
232 pcc
= LIST_FIRST(&pu
->pu_ccmagazin
);
235 LIST_REMOVE(pcc
, pcc_rope
);
237 DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc
));
239 assert(pcc
->pcc_pu
== pu
);
242 pcc
->pcc_func
= func
;
246 pcc
->pcc_uc
.uc_link
= &pcc
->pcc_uc_ret
;
250 * XXX: I guess this should theoretically be preserved by
251 * swapcontext(). However, it gets lost. So reinit it.
253 st
= &pcc
->pcc_uc
.uc_stack
;
255 st
->ss_size
= stacksize
;
259 * Give us an initial context to jump to.
261 * Our manual page says that portable code shouldn't
262 * rely on being able to pass pointers through makecontext().
263 * kjk says that NetBSD code doesn't need to worry about this.
264 * uwe says it would be like putting a "keep away from
265 * children" sign on a box of toys.
267 makecontext(&pcc
->pcc_uc
, (void *)func
, 1, (uintptr_t)pcc
);
275 puffs__cc_setcaller(struct puffs_cc
*pcc
, pid_t pid
, lwpid_t lid
)
280 pcc
->pcc_flags
|= PCC_HASCALLER
;
284 cc_free(struct puffs_cc
*pcc
)
286 struct puffs_usermount
*pu
= pcc
->pcc_pu
;
287 size_t stacksize
= 1<<pu
->pu_cc_stackshift
;
289 DPRINTF(("invalidating pcc %p\n", pcc
));
290 assert(!puffs_fakecc
);
291 munmap(pcc
, stacksize
);
295 puffs__cc_destroy(struct puffs_cc
*pcc
, int nonuke
)
297 struct puffs_usermount
*pu
= pcc
->pcc_pu
;
299 pcc
->pcc_flags
&= ~PCC_HASCALLER
;
300 assert(pcc
->pcc_flags
== 0);
301 assert(!puffs_fakecc
);
303 /* not over limit? stuff away in the store, otherwise nuke */
304 if (nonuke
|| pu
->pu_cc_nstored
< PUFFS_CCMAXSTORE
) {
306 DPRINTF(("puffs__cc_destroy: storing pcc %p\n", pcc
));
307 LIST_INSERT_HEAD(&pu
->pu_ccmagazin
, pcc
, pcc_rope
);
315 puffs__cc_exit(struct puffs_usermount
*pu
)
317 struct puffs_cc
*pcc
;
319 while ((pcc
= LIST_FIRST(&pu
->pu_ccmagazin
)) != NULL
) {
320 LIST_REMOVE(pcc
, pcc_rope
);
326 puffs_cc_getcc(struct puffs_usermount
*pu
)
328 size_t stacksize
= 1<<pu
->pu_cc_stackshift
;
334 bottom
= ((uintptr_t)&bottom
) & ~(stacksize
-1);
335 return (struct puffs_cc
*)bottom
;
339 puffs__cc_savemain(struct puffs_usermount
*pu
)
345 PU_CLRSFLAG(pu
, PU_MAINRESTORE
);
346 return getcontext(&pu
->pu_mainctx
);
350 puffs__cc_restoremain(struct puffs_usermount
*pu
)
356 puffs__cc_destroy(puffs_cc_getcc(pu
), 1);
357 PU_SETSFLAG(pu
, PU_MAINRESTORE
);
358 return setcontext(&pu
->pu_mainctx
);