Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.22
      1  1.22  pooka /*	$NetBSD: callcontext.c,v 1.22 2008/08/11 15:59:01 pooka 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.22  pooka __RCSID("$NetBSD: callcontext.c,v 1.22 2008/08/11 15:59:01 pooka 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.22  pooka #if 0
     51  1.22  pooka #define DPRINTF(x) printf x
     52  1.22  pooka #else
     53  1.22  pooka #define DPRINTF(x)
     54  1.22  pooka #endif
     55  1.22  pooka 
     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.22  pooka 	DPRINTF(("puffs_cc_yield: "));
     85  1.20  pooka 	if ((pcc->pcc_flags & PCC_MLCONT) == 0) {
     86  1.22  pooka 		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.22  pooka 		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.22  pooka 		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.22  pooka 	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.22  pooka 	DPRINTF(("puffs_cc_continue: pcc %p\n", pcc));
    127  1.22  pooka 	if (puffs_fakecc) {
    128  1.20  pooka 		pcc->pcc_func(pcc->pcc_farg);
    129  1.22  pooka 	} else {
    130  1.20  pooka 		swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
    131  1.22  pooka 	}
    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.20  pooka 	} else {
    226  1.20  pooka 		pcc = LIST_FIRST(&pu->pu_ccmagazin);
    227  1.20  pooka 		assert(pcc != NULL);
    228  1.18  pooka 
    229  1.20  pooka 		LIST_REMOVE(pcc, pcc_rope);
    230  1.20  pooka 		pu->pu_cc_nstored--;
    231  1.20  pooka 	}
    232  1.20  pooka 	assert(pcc->pcc_pu == pu);
    233  1.18  pooka 
    234  1.20  pooka 	if (puffs_fakecc) {
    235  1.20  pooka 		pcc->pcc_func = func;
    236  1.20  pooka 		pcc->pcc_farg = pcc;
    237  1.20  pooka 	} else {
    238  1.20  pooka 		/* link context */
    239  1.20  pooka 		pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    240   1.1  pooka 
    241  1.20  pooka 		/* setup stack
    242  1.20  pooka 		 *
    243  1.20  pooka 		 * XXX: I guess this should theoretically be preserved by
    244  1.20  pooka 		 * swapcontext().  However, it gets lost.  So reinit it.
    245  1.20  pooka 		 */
    246  1.20  pooka 		st = &pcc->pcc_uc.uc_stack;
    247  1.20  pooka 		st->ss_sp = pcc;
    248  1.20  pooka 		st->ss_size = stacksize;
    249  1.20  pooka 		st->ss_flags = 0;
    250  1.20  pooka 
    251  1.20  pooka 		/*
    252  1.20  pooka 		 * Give us an initial context to jump to.
    253  1.20  pooka 		 *
    254  1.20  pooka 		 * Our manual page says that portable code shouldn't
    255  1.20  pooka 		 * rely on being able to pass pointers through makecontext().
    256  1.20  pooka 		 * kjk says that NetBSD code doesn't need to worry about this.
    257  1.20  pooka 		 * uwe says it would be like putting a "keep away from
    258  1.20  pooka 		 * children" sign on a box of toys.
    259  1.20  pooka 		 */
    260  1.20  pooka 		makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc);
    261  1.20  pooka 	}
    262  1.19  pooka 
    263  1.22  pooka 	DPRINTF(("puffs__cc_create: created pcc %p with fun %p\n", pcc, func));
    264  1.12  pooka 	*pccp = pcc;
    265  1.12  pooka 	return 0;
    266   1.1  pooka }
    267   1.1  pooka 
    268   1.1  pooka void
    269  1.20  pooka puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    270   1.9  pooka {
    271   1.9  pooka 
    272   1.9  pooka 	pcc->pcc_pid = pid;
    273   1.9  pooka 	pcc->pcc_lid = lid;
    274   1.9  pooka 	pcc->pcc_flags |= PCC_HASCALLER;
    275   1.9  pooka }
    276   1.9  pooka 
    277   1.9  pooka void
    278  1.20  pooka puffs__cc_destroy(struct puffs_cc *pcc, int nonuke)
    279   1.1  pooka {
    280  1.11  pooka 	struct puffs_usermount *pu = pcc->pcc_pu;
    281  1.11  pooka 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    282   1.1  pooka 
    283  1.21  pooka 	assert(pcc->pcc_flags == 0);
    284  1.20  pooka 
    285  1.20  pooka 	/* not over limit?  stuff away in the store, otherwise nuke */
    286  1.20  pooka 	if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) {
    287  1.20  pooka 		pcc->pcc_pb = NULL;
    288  1.18  pooka 		LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
    289  1.18  pooka 		pu->pu_cc_nstored++;
    290  1.20  pooka 	} else {
    291  1.20  pooka 		assert(!puffs_fakecc);
    292  1.20  pooka 		munmap(pcc, stacksize);
    293  1.18  pooka 	}
    294  1.14  pooka }
    295  1.14  pooka 
    296  1.14  pooka struct puffs_cc *
    297  1.14  pooka puffs_cc_getcc(struct puffs_usermount *pu)
    298  1.14  pooka {
    299  1.14  pooka 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    300  1.14  pooka 	uintptr_t bottom;
    301  1.14  pooka 
    302  1.14  pooka 	if (puffs_fakecc)
    303  1.14  pooka 		return &fakecc;
    304  1.14  pooka 
    305  1.14  pooka 	bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
    306  1.14  pooka 	return (struct puffs_cc *)bottom;
    307   1.1  pooka }
    308  1.22  pooka 
    309  1.22  pooka int
    310  1.22  pooka puffs__cc_savemain(struct puffs_usermount *pu)
    311  1.22  pooka {
    312  1.22  pooka 
    313  1.22  pooka 	PU_CLRSFLAG(pu, PU_MAINRESTORE);
    314  1.22  pooka 	return getcontext(&pu->pu_mainctx);
    315  1.22  pooka }
    316  1.22  pooka 
    317  1.22  pooka int
    318  1.22  pooka puffs__cc_restoremain(struct puffs_usermount *pu)
    319  1.22  pooka {
    320  1.22  pooka 
    321  1.22  pooka 	PU_SETSFLAG(pu, PU_MAINRESTORE);
    322  1.22  pooka 	return setcontext(&pu->pu_mainctx);
    323  1.22  pooka }
    324