xref: /minix/lib/libpuffs/callcontext.c (revision ebfedea0)
1 /*	$NetBSD: callcontext.c,v 1.27 2011/12/06 21:15:39 skrll 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.27 2011/12/06 21:15:39 skrll 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 #if 0
51 #define DPRINTF(x) printf x
52 #else
53 #define DPRINTF(x)
54 #endif
55 
56 /*
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.
60  */
61 int puffs_fakecc;
62 
63 /*
64  * user stuff
65  */
66 
67 /*
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.
72  */
73 void
74 puffs_cc_yield(struct puffs_cc *pcc)
75 {
76 	struct puffs_cc *jumpcc;
77 	int rv;
78 
79 	assert(puffs_fakecc == 0);
80 
81 	if ((~pcc->pcc_flags & (PCC_BORROWED|PCC_DONE)) == 0) {
82 		pcc->pcc_flags &= ~(PCC_BORROWED|PCC_DONE);
83 		/*
84 		 * see the XXX comment in puffs__cc_cont
85 		 */
86 		puffs__cc_destroy(pcc, 1);
87 		setcontext(&pcc->pcc_uc_ret);
88 	}
89 	pcc->pcc_flags &= ~PCC_BORROWED;
90 
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);
96 	} else {
97 		DPRINTF(("mlcont, pcc %p\n", pcc));
98 		pcc->pcc_flags &= ~PCC_MLCONT;
99 		rv = puffs__cc_create(pcc->pcc_pu, puffs__theloop, &jumpcc);
100 		if (rv)
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));
104 	}
105 }
106 
107 /*
108  * Internal continue routine.  This has slightly different semantics.
109  * We simply make our cc available in the freelist and jump to the
110  * indicated pcc.
111  */
112 void
113 puffs__cc_cont(struct puffs_cc *pcc)
114 {
115 	struct puffs_cc *mycc;
116 
117 	mycc = puffs_cc_getcc(pcc->pcc_pu);
118 	DPRINTF(("puffs__cc_cont: pcc %p, mycc %p\n", pcc, mycc));
119 
120 	/*
121 	 * XXX: race between setcontext() and recycle if
122 	 * we go multithreaded
123 	 */
124 	puffs__cc_destroy(mycc, 1);
125 	pcc->pcc_flags |= PCC_MLCONT;
126 	setcontext(&pcc->pcc_uc);
127 }
128 
129 void
130 puffs_cc_continue(struct puffs_cc *pcc)
131 {
132 
133 	/* ramble on */
134 	DPRINTF(("puffs_cc_continue: pcc %p\n", pcc));
135 	if (puffs_fakecc) {
136 		pcc->pcc_func(pcc->pcc_farg);
137 	} else {
138 		swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
139 	}
140 }
141 
142 /*
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
147  * yield again).
148  */
149 void
150 puffs__goto(struct puffs_cc *loanpcc)
151 {
152 
153 	loanpcc->pcc_flags |= PCC_BORROWED;
154 
155 	swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
156 }
157 
158 void
159 puffs_cc_schedule(struct puffs_cc *pcc)
160 {
161 	struct puffs_usermount *pu = pcc->pcc_pu;
162 
163 	assert(pu->pu_state & PU_INLOOP);
164 	TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent);
165 }
166 
167 int
168 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
169 {
170 
171 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
172 		errno = ESRCH;
173 		return -1;
174 	}
175 
176 	if (pid)
177 		*pid = pcc->pcc_pid;
178 	if (lid)
179 		*lid = pcc->pcc_lid;
180 	return 0;
181 }
182 
183 static struct puffs_cc fakecc;
184 
185 static struct puffs_cc *
186 slowccalloc(struct puffs_usermount *pu)
187 {
188 	struct puffs_cc *volatile pcc;
189 	void *sp;
190 	size_t stacksize = 1<<pu->pu_cc_stackshift;
191 #ifndef __minix
192 	const long psize = sysconf(_SC_PAGESIZE);
193 #endif /* !__minix */
194 
195 	if (puffs_fakecc)
196 		return &fakecc;
197 
198 	sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
199 	    MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0);
200 	if (sp == MAP_FAILED)
201 		return NULL;
202 
203 	pcc = sp;
204 	memset(pcc, 0, sizeof(struct puffs_cc));
205 
206 #ifndef __minix
207 #ifndef __MACHINE_STACK_GROWS_UP
208 	mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE);
209 #else
210 	mprotect((uint8_t *)sp + stacksize - psize, (size_t)psize, PROT_NONE);
211 #endif
212 #endif /* !__minix */
213 
214 	/* initialize both ucontext's */
215 	if (getcontext(&pcc->pcc_uc) == -1) {
216 		munmap(pcc, stacksize);
217 		return NULL;
218 	}
219 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
220 		munmap(pcc, stacksize);
221 		return NULL;
222 	}
223 
224 	return pcc;
225 }
226 
227 int
228 puffs__cc_create(struct puffs_usermount *pu, puffs_ccfunc func,
229 	struct puffs_cc **pccp)
230 {
231 	struct puffs_cc *pcc;
232 	size_t stacksize = 1<<pu->pu_cc_stackshift;
233 	stack_t *st;
234 
235 	/* Do we have a cached copy? */
236 	if (pu->pu_cc_nstored == 0) {
237 		pcc = slowccalloc(pu);
238 		if (pcc == NULL)
239 			return -1;
240 		pcc->pcc_pu = pu;
241 		DPRINTF(("puffs__cc_create: allocated pcc %p\n", pcc));
242 	} else {
243 		pcc = LIST_FIRST(&pu->pu_ccmagazin);
244 		assert(pcc != NULL);
245 
246 		LIST_REMOVE(pcc, pcc_rope);
247 		pu->pu_cc_nstored--;
248 		DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc));
249 	}
250 	assert(pcc->pcc_pu == pu);
251 
252 	if (puffs_fakecc) {
253 		pcc->pcc_func = func;
254 		pcc->pcc_farg = pcc;
255 	} else {
256 		const long psize = sysconf(_SC_PAGESIZE);
257 
258 		/* link context */
259 		pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
260 
261 		/* setup stack
262 		 *
263 		 * XXX: I guess this should theoretically be preserved by
264 		 * swapcontext().  However, it gets lost.  So reinit it.
265 		 */
266 		st = &pcc->pcc_uc.uc_stack;
267 		st->ss_sp = ((uint8_t *)(void *)pcc) + psize;
268 		st->ss_size = stacksize - psize;
269 		st->ss_flags = 0;
270 
271 		/*
272 		 * Give us an initial context to jump to.
273 		 *
274 		 * Our manual page says that portable code shouldn't
275 		 * rely on being able to pass pointers through makecontext().
276 		 * kjk says that NetBSD code doesn't need to worry about this.
277 		 * uwe says it would be like putting a "keep away from
278 		 * children" sign on a box of toys.
279 		 */
280 		makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc);
281 	}
282 
283 	*pccp = pcc;
284 	return 0;
285 }
286 
287 void
288 puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
289 {
290 
291 	pcc->pcc_pid = pid;
292 	pcc->pcc_lid = lid;
293 	pcc->pcc_flags |= PCC_HASCALLER;
294 }
295 
296 static void
297 cc_free(struct puffs_cc *pcc)
298 {
299 	struct puffs_usermount *pu = pcc->pcc_pu;
300 	size_t stacksize = 1<<pu->pu_cc_stackshift;
301 
302 	DPRINTF(("invalidating pcc %p\n", pcc));
303 	assert(!puffs_fakecc);
304 	munmap(pcc, stacksize);
305 }
306 
307 void
308 puffs__cc_destroy(struct puffs_cc *pcc, int nonuke)
309 {
310 	struct puffs_usermount *pu = pcc->pcc_pu;
311 
312 	pcc->pcc_flags &= ~PCC_HASCALLER;
313 	assert(pcc->pcc_flags == 0);
314 	assert(!puffs_fakecc);
315 
316 	/* not over limit?  stuff away in the store, otherwise nuke */
317 	if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) {
318 		pcc->pcc_pb = NULL;
319 		DPRINTF(("puffs__cc_destroy: storing pcc %p\n", pcc));
320 		LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
321 		pu->pu_cc_nstored++;
322 	} else {
323 		cc_free(pcc);
324 	}
325 }
326 
327 void
328 puffs__cc_exit(struct puffs_usermount *pu)
329 {
330 	struct puffs_cc *pcc;
331 
332 	while ((pcc = LIST_FIRST(&pu->pu_ccmagazin)) != NULL) {
333 		LIST_REMOVE(pcc, pcc_rope);
334 		cc_free(pcc);
335 	}
336 }
337 
338 struct puffs_cc *
339 puffs_cc_getcc(struct puffs_usermount *pu)
340 {
341 	size_t stacksize = 1<<pu->pu_cc_stackshift;
342 	uintptr_t bottom;
343 
344 	if (puffs_fakecc)
345 		return &fakecc;
346 
347 	bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
348 	return (struct puffs_cc *)bottom;
349 }
350 
351 int
352 puffs__cc_savemain(struct puffs_usermount *pu)
353 {
354 
355 	if (puffs_fakecc)
356 		return 0;
357 
358 	PU_CLRSFLAG(pu, PU_MAINRESTORE);
359 	return getcontext(&pu->pu_mainctx);
360 }
361 
362 int
363 puffs__cc_restoremain(struct puffs_usermount *pu)
364 {
365 
366 	if (puffs_fakecc)
367 		return 0;
368 
369 	puffs__cc_destroy(puffs_cc_getcc(pu), 1);
370 	PU_SETSFLAG(pu, PU_MAINRESTORE);
371 	return setcontext(&pu->pu_mainctx);
372 }
373