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