usched: Allow process to change self cpu affinity
[dragonfly.git] / lib / libpuffs / callcontext.c
blob2322a427a4c8e793ef232f9a1a1057f5a4098094
1 /* $NetBSD: callcontext.c,v 1.25 2011/03/04 09:47:47 yamt 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/types.h>
32 #include <sys/mman.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ucontext.h>
40 #include <unistd.h>
42 #include "puffs.h"
43 #include "puffs_priv.h"
45 #if 0
46 #define DPRINTF(x) printf x
47 #else
48 #define DPRINTF(x)
49 #endif
52 * Set the following to 1 to not handle each request on a separate
53 * stack. This is highly volatile kludge, therefore no external
54 * interface.
56 int puffs_fakecc;
59 * user stuff
63 * So, we need to get back to where we came from. This can happen in two
64 * different ways:
65 * 1) PCC_MLCONT is set, in which case we need to go to the mainloop
66 * 2) It is not set, and we simply jump to pcc_uc_ret.
68 void
69 puffs_cc_yield(struct puffs_cc *pcc)
71 struct puffs_cc *jumpcc;
72 int rv;
74 assert(puffs_fakecc == 0);
76 pcc->pcc_flags &= ~PCC_BORROWED;
78 /* romanes eunt domus */
79 DPRINTF(("puffs_cc_yield: "));
80 if ((pcc->pcc_flags & PCC_MLCONT) == 0) {
81 DPRINTF(("no mlcont, pcc %p\n", pcc));
82 swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
83 } else {
84 DPRINTF(("mlcont, pcc %p\n", pcc));
85 pcc->pcc_flags &= ~PCC_MLCONT;
86 rv = puffs__cc_create(pcc->pcc_pu, puffs__theloop, &jumpcc);
87 if (rv)
88 abort(); /* p-p-p-pa-pa-panic (XXX: fixme) */
89 swapcontext(&pcc->pcc_uc, &jumpcc->pcc_uc);
90 DPRINTF(("puffs_cc_yield: post swap pcc %p\n", pcc));
95 * Internal continue routine. This has slightly different semantics.
96 * We simply make our cc available in the freelist and jump to the
97 * indicated pcc.
99 void
100 puffs__cc_cont(struct puffs_cc *pcc)
102 struct puffs_cc *mycc;
104 mycc = puffs_cc_getcc(pcc->pcc_pu);
105 DPRINTF(("puffs__cc_cont: pcc %p, mycc %p\n", pcc, mycc));
108 * XXX: race between setcontext() and recycle if
109 * we go multithreaded
111 puffs__cc_destroy(mycc, 1);
112 pcc->pcc_flags |= PCC_MLCONT;
113 setcontext(&pcc->pcc_uc);
116 void
117 puffs_cc_continue(struct puffs_cc *pcc)
120 /* ramble on */
121 DPRINTF(("puffs_cc_continue: pcc %p\n", pcc));
122 if (puffs_fakecc) {
123 pcc->pcc_func(pcc->pcc_farg);
124 } else {
125 swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
130 * "Borrows" pcc, *NOT* called from pcc owner. Acts like continue.
131 * So the idea is to use this, give something the context back to
132 * run to completion and then jump back to where ever this was called
133 * from after the op dispatching is complete (or if the pcc decides to
134 * yield again).
136 void
137 puffs__goto(struct puffs_cc *loanpcc)
140 loanpcc->pcc_flags |= PCC_BORROWED;
142 swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
145 void
146 puffs_cc_schedule(struct puffs_cc *pcc)
148 struct puffs_usermount *pu = pcc->pcc_pu;
150 assert(pu->pu_state & PU_INLOOP);
151 TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent);
155 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
158 if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
159 errno = ESRCH;
160 return -1;
163 if (pid)
164 *pid = pcc->pcc_pid;
165 if (lid)
166 *lid = pcc->pcc_lid;
167 return 0;
170 static struct puffs_cc fakecc;
172 static struct puffs_cc *
173 slowccalloc(struct puffs_usermount *pu)
175 struct puffs_cc *volatile pcc;
176 void *sp;
177 size_t stacksize = 1<<pu->pu_cc_stackshift;
178 size_t stackalign;
179 long psize = sysconf(_SC_PAGESIZE);
181 if (puffs_fakecc)
182 return &fakecc;
185 * Emulate MAP_ALIGNED(pu->pu_cc_stackshift) by allocating stacksize*2
186 * bytes and unmapping extra pages
188 sp = mmap(NULL, stacksize * 2, PROT_READ|PROT_WRITE,
189 MAP_ANON|MAP_PRIVATE, -1, 0);
190 if (sp == MAP_FAILED)
191 return NULL;
192 stackalign = ((uintptr_t)sp) & (stacksize-1);
193 if (stackalign != 0) {
194 munmap(sp, stacksize - stackalign);
195 sp = (uint8_t *)sp + stacksize - stackalign;
196 munmap((uint8_t *)sp + stacksize, stackalign);
197 } else {
198 munmap((uint8_t *)sp + stacksize, stacksize);
201 pcc = sp;
202 memset(pcc, 0, sizeof(struct puffs_cc));
204 mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE);
206 /* initialize both ucontext's */
207 if (getcontext(&pcc->pcc_uc) == -1) {
208 munmap(pcc, stacksize);
209 return NULL;
211 if (getcontext(&pcc->pcc_uc_ret) == -1) {
212 munmap(pcc, stacksize);
213 return NULL;
216 return pcc;
220 puffs__cc_create(struct puffs_usermount *pu, puffs_ccfunc func,
221 struct puffs_cc **pccp)
223 struct puffs_cc *pcc;
224 size_t stacksize = 1<<pu->pu_cc_stackshift;
225 stack_t *st;
227 /* Do we have a cached copy? */
228 if (pu->pu_cc_nstored == 0) {
229 pcc = slowccalloc(pu);
230 if (pcc == NULL)
231 return -1;
232 pcc->pcc_pu = pu;
233 DPRINTF(("puffs__cc_create: allocated pcc %p\n", pcc));
234 } else {
235 pcc = LIST_FIRST(&pu->pu_ccmagazin);
236 assert(pcc != NULL);
238 LIST_REMOVE(pcc, pcc_rope);
239 pu->pu_cc_nstored--;
240 DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc));
242 assert(pcc->pcc_pu == pu);
244 if (puffs_fakecc) {
245 pcc->pcc_func = func;
246 pcc->pcc_farg = pcc;
247 } else {
248 /* link context */
249 pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
251 /* setup stack
253 * XXX: I guess this should theoretically be preserved by
254 * swapcontext(). However, it gets lost. So reinit it.
256 st = &pcc->pcc_uc.uc_stack;
257 st->ss_sp = (void *)pcc;
258 st->ss_size = stacksize;
259 st->ss_flags = 0;
262 * Give us an initial context to jump to.
264 * Our manual page says that portable code shouldn't
265 * rely on being able to pass pointers through makecontext().
266 * kjk says that NetBSD code doesn't need to worry about this.
267 * uwe says it would be like putting a "keep away from
268 * children" sign on a box of toys.
270 makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc);
273 *pccp = pcc;
274 return 0;
277 void
278 puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
281 pcc->pcc_pid = pid;
282 pcc->pcc_lid = lid;
283 pcc->pcc_flags |= PCC_HASCALLER;
286 static void
287 cc_free(struct puffs_cc *pcc)
289 struct puffs_usermount *pu = pcc->pcc_pu;
290 size_t stacksize = 1<<pu->pu_cc_stackshift;
292 DPRINTF(("invalidating pcc %p\n", pcc));
293 assert(!puffs_fakecc);
294 munmap(pcc, stacksize);
297 void
298 puffs__cc_destroy(struct puffs_cc *pcc, int nonuke)
300 struct puffs_usermount *pu = pcc->pcc_pu;
302 pcc->pcc_flags &= ~PCC_HASCALLER;
303 assert(pcc->pcc_flags == 0);
304 assert(!puffs_fakecc);
306 /* not over limit? stuff away in the store, otherwise nuke */
307 if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) {
308 pcc->pcc_pb = NULL;
309 DPRINTF(("puffs__cc_destroy: storing pcc %p\n", pcc));
310 LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
311 pu->pu_cc_nstored++;
312 } else {
313 cc_free(pcc);
317 void
318 puffs__cc_exit(struct puffs_usermount *pu)
320 struct puffs_cc *pcc;
322 while ((pcc = LIST_FIRST(&pu->pu_ccmagazin)) != NULL) {
323 LIST_REMOVE(pcc, pcc_rope);
324 cc_free(pcc);
328 struct puffs_cc *
329 puffs_cc_getcc(struct puffs_usermount *pu)
331 size_t stacksize = 1<<pu->pu_cc_stackshift;
332 uintptr_t bottom;
334 if (puffs_fakecc)
335 return &fakecc;
337 bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
338 return (struct puffs_cc *)bottom;
342 puffs__cc_savemain(struct puffs_usermount *pu)
345 if (puffs_fakecc)
346 return 0;
348 PU_CLRSFLAG(pu, PU_MAINRESTORE);
349 return getcontext(&pu->pu_mainctx);
353 puffs__cc_restoremain(struct puffs_usermount *pu)
356 if (puffs_fakecc)
357 return 0;
359 puffs__cc_destroy(puffs_cc_getcc(pu), 1);
360 PU_SETSFLAG(pu, PU_MAINRESTORE);
361 return setcontext(&pu->pu_mainctx);