Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.20
      1  1.20  pooka /*	$NetBSD: callcontext.c,v 1.20 2008/01/28 18:35:49 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.20  pooka __RCSID("$NetBSD: callcontext.c,v 1.20 2008/01/28 18:35:49 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.1  pooka /*
     51  1.20  pooka  * Set the following to 1 to not handle each request on a separate
     52  1.20  pooka  * stack.  This is highly volatile kludge, therefore no external
     53  1.20  pooka  * interface.
     54  1.20  pooka  */
     55  1.20  pooka int puffs_fakecc;
     56  1.20  pooka 
     57  1.20  pooka /*
     58   1.1  pooka  * user stuff
     59   1.1  pooka  */
     60   1.1  pooka 
     61  1.20  pooka /*
     62  1.20  pooka  * So, we need to get back to where we came from.  This can happen in two
     63  1.20  pooka  * different ways:
     64  1.20  pooka  *  1) PCC_MLCONT is set, in which case we need to go to the mainloop
     65  1.20  pooka  *  2) It is not set, and we simply jump to pcc_uc_ret.
     66  1.20  pooka  */
     67   1.1  pooka void
     68   1.1  pooka puffs_cc_yield(struct puffs_cc *pcc)
     69   1.1  pooka {
     70  1.20  pooka 	struct puffs_cc *jumpcc;
     71  1.20  pooka 	int rv;
     72  1.20  pooka 
     73  1.20  pooka 	assert(puffs_fakecc == 0);
     74   1.1  pooka 
     75   1.6  pooka 	pcc->pcc_flags &= ~PCC_BORROWED;
     76   1.1  pooka 
     77   1.1  pooka 	/* romanes eunt domus */
     78  1.20  pooka 	if ((pcc->pcc_flags & PCC_MLCONT) == 0) {
     79  1.20  pooka 		swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
     80  1.20  pooka 	} else {
     81  1.20  pooka 		pcc->pcc_flags &= ~PCC_MLCONT;
     82  1.20  pooka 		rv = puffs__cc_create(pcc->pcc_pu, puffs__theloop, &jumpcc);
     83  1.20  pooka 		if (rv)
     84  1.20  pooka 			abort(); /* p-p-p-pa-pa-panic (XXX: fixme) */
     85  1.20  pooka 		swapcontext(&pcc->pcc_uc, &jumpcc->pcc_uc);
     86  1.20  pooka 	}
     87  1.20  pooka }
     88  1.20  pooka 
     89  1.20  pooka /*
     90  1.20  pooka  * Internal continue routine.  This has slightly different semantics.
     91  1.20  pooka  * We simply make our cc available in the freelist and jump to the
     92  1.20  pooka  * indicated pcc.
     93  1.20  pooka  */
     94  1.20  pooka void
     95  1.20  pooka puffs__cc_cont(struct puffs_cc *pcc)
     96  1.20  pooka {
     97  1.20  pooka 	struct puffs_cc *mycc;
     98  1.20  pooka 
     99  1.20  pooka 	mycc = puffs_cc_getcc(pcc->pcc_pu);
    100  1.20  pooka 
    101  1.20  pooka 	/*
    102  1.20  pooka 	 * XXX: race between setcontenxt() and recycle if
    103  1.20  pooka 	 * we go multithreaded
    104  1.20  pooka 	 */
    105  1.20  pooka 	puffs__cc_destroy(mycc, 1);
    106  1.20  pooka 	pcc->pcc_flags |= PCC_MLCONT;
    107  1.20  pooka 	setcontext(&pcc->pcc_uc);
    108   1.1  pooka }
    109   1.1  pooka 
    110   1.1  pooka void
    111   1.1  pooka puffs_cc_continue(struct puffs_cc *pcc)
    112   1.1  pooka {
    113   1.1  pooka 
    114   1.1  pooka 	/* ramble on */
    115  1.20  pooka 	if (puffs_fakecc)
    116  1.20  pooka 		pcc->pcc_func(pcc->pcc_farg);
    117  1.20  pooka 	else
    118  1.20  pooka 		swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
    119   1.1  pooka }
    120   1.1  pooka 
    121   1.6  pooka /*
    122   1.6  pooka  * "Borrows" pcc, *NOT* called from pcc owner.  Acts like continue.
    123   1.6  pooka  * So the idea is to use this, give something the context back to
    124   1.6  pooka  * run to completion and then jump back to where ever this was called
    125   1.6  pooka  * from after the op dispatching is complete (or if the pcc decides to
    126   1.6  pooka  * yield again).
    127   1.6  pooka  */
    128   1.6  pooka void
    129  1.20  pooka puffs__goto(struct puffs_cc *loanpcc)
    130   1.6  pooka {
    131   1.6  pooka 
    132   1.6  pooka 	loanpcc->pcc_flags |= PCC_BORROWED;
    133   1.6  pooka 
    134   1.6  pooka 	swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
    135   1.6  pooka }
    136   1.6  pooka 
    137  1.10  pooka void
    138  1.10  pooka puffs_cc_schedule(struct puffs_cc *pcc)
    139  1.10  pooka {
    140  1.10  pooka 	struct puffs_usermount *pu = pcc->pcc_pu;
    141  1.10  pooka 
    142  1.10  pooka 	assert(pu->pu_state & PU_INLOOP);
    143  1.18  pooka 	TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent);
    144  1.10  pooka }
    145  1.10  pooka 
    146   1.9  pooka int
    147   1.9  pooka puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
    148   1.9  pooka {
    149   1.9  pooka 
    150   1.9  pooka 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
    151   1.9  pooka 		errno = ESRCH;
    152   1.9  pooka 		return -1;
    153   1.9  pooka 	}
    154   1.9  pooka 
    155   1.9  pooka 	if (pid)
    156   1.9  pooka 		*pid = pcc->pcc_pid;
    157   1.9  pooka 	if (lid)
    158   1.9  pooka 		*lid = pcc->pcc_lid;
    159   1.9  pooka 	return 0;
    160   1.9  pooka }
    161   1.9  pooka 
    162  1.14  pooka static struct puffs_cc fakecc;
    163  1.14  pooka 
    164  1.20  pooka static struct puffs_cc *
    165  1.20  pooka slowccalloc(struct puffs_usermount *pu)
    166   1.1  pooka {
    167   1.1  pooka 	struct puffs_cc *volatile pcc;
    168  1.20  pooka 	void *sp;
    169  1.11  pooka 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    170  1.14  pooka 	long psize = sysconf(_SC_PAGESIZE);
    171   1.1  pooka 
    172  1.20  pooka 	if (puffs_fakecc)
    173  1.20  pooka 		return &fakecc;
    174  1.18  pooka 
    175  1.20  pooka 	sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
    176  1.20  pooka 	    MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0);
    177  1.20  pooka 	if (sp == MAP_FAILED)
    178  1.20  pooka 		return NULL;
    179  1.14  pooka 
    180  1.20  pooka 	pcc = sp;
    181   1.1  pooka 	memset(pcc, 0, sizeof(struct puffs_cc));
    182  1.12  pooka 
    183  1.20  pooka 	mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE);
    184   1.1  pooka 
    185   1.1  pooka 	/* initialize both ucontext's */
    186   1.1  pooka 	if (getcontext(&pcc->pcc_uc) == -1) {
    187  1.14  pooka 		munmap(pcc, stacksize);
    188  1.20  pooka 		return NULL;
    189   1.1  pooka 	}
    190   1.1  pooka 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    191  1.14  pooka 		munmap(pcc, stacksize);
    192  1.20  pooka 		return NULL;
    193   1.1  pooka 	}
    194   1.1  pooka 
    195  1.20  pooka 	return pcc;
    196  1.20  pooka }
    197  1.12  pooka 
    198  1.20  pooka int
    199  1.20  pooka puffs__cc_create(struct puffs_usermount *pu, puffs_ccfunc func,
    200  1.20  pooka 	struct puffs_cc **pccp)
    201  1.20  pooka {
    202  1.20  pooka 	struct puffs_cc *pcc;
    203  1.20  pooka 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    204  1.20  pooka 	stack_t *st;
    205  1.18  pooka 
    206  1.20  pooka 	/* Do we have a cached copy? */
    207  1.20  pooka 	if (pu->pu_cc_nstored == 0) {
    208  1.20  pooka 		pcc = slowccalloc(pu);
    209  1.20  pooka 		if (pcc == NULL)
    210  1.20  pooka 			return -1;
    211  1.20  pooka 		pcc->pcc_pu = pu;
    212  1.20  pooka 	} else {
    213  1.20  pooka 		pcc = LIST_FIRST(&pu->pu_ccmagazin);
    214  1.20  pooka 		assert(pcc != NULL);
    215  1.18  pooka 
    216  1.20  pooka 		LIST_REMOVE(pcc, pcc_rope);
    217  1.20  pooka 		pu->pu_cc_nstored--;
    218  1.20  pooka 	}
    219  1.20  pooka 	assert(pcc->pcc_pu == pu);
    220  1.18  pooka 
    221  1.20  pooka 	if (puffs_fakecc) {
    222  1.20  pooka 		pcc->pcc_func = func;
    223  1.20  pooka 		pcc->pcc_farg = pcc;
    224  1.20  pooka 	} else {
    225  1.20  pooka 		/* link context */
    226  1.20  pooka 		pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    227   1.1  pooka 
    228  1.20  pooka 		/* setup stack
    229  1.20  pooka 		 *
    230  1.20  pooka 		 * XXX: I guess this should theoretically be preserved by
    231  1.20  pooka 		 * swapcontext().  However, it gets lost.  So reinit it.
    232  1.20  pooka 		 */
    233  1.20  pooka 		st = &pcc->pcc_uc.uc_stack;
    234  1.20  pooka 		st->ss_sp = pcc;
    235  1.20  pooka 		st->ss_size = stacksize;
    236  1.20  pooka 		st->ss_flags = 0;
    237  1.20  pooka 
    238  1.20  pooka 		/*
    239  1.20  pooka 		 * Give us an initial context to jump to.
    240  1.20  pooka 		 *
    241  1.20  pooka 		 * Our manual page says that portable code shouldn't
    242  1.20  pooka 		 * rely on being able to pass pointers through makecontext().
    243  1.20  pooka 		 * kjk says that NetBSD code doesn't need to worry about this.
    244  1.20  pooka 		 * uwe says it would be like putting a "keep away from
    245  1.20  pooka 		 * children" sign on a box of toys.
    246  1.20  pooka 		 */
    247  1.20  pooka 		makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc);
    248  1.20  pooka 	}
    249  1.19  pooka 
    250  1.12  pooka 	*pccp = pcc;
    251  1.12  pooka 	return 0;
    252   1.1  pooka }
    253   1.1  pooka 
    254   1.1  pooka void
    255  1.20  pooka puffs__cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    256   1.9  pooka {
    257   1.9  pooka 
    258   1.9  pooka 	pcc->pcc_pid = pid;
    259   1.9  pooka 	pcc->pcc_lid = lid;
    260   1.9  pooka 	pcc->pcc_flags |= PCC_HASCALLER;
    261   1.9  pooka }
    262   1.9  pooka 
    263   1.9  pooka void
    264  1.20  pooka puffs__cc_destroy(struct puffs_cc *pcc, int nonuke)
    265   1.1  pooka {
    266  1.11  pooka 	struct puffs_usermount *pu = pcc->pcc_pu;
    267  1.11  pooka 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    268   1.1  pooka 
    269  1.20  pooka 	assert(pcc->pcc_flags = 0);
    270  1.20  pooka 
    271  1.20  pooka 	/* not over limit?  stuff away in the store, otherwise nuke */
    272  1.20  pooka 	if (nonuke || pu->pu_cc_nstored < PUFFS_CCMAXSTORE) {
    273  1.20  pooka 		pcc->pcc_pb = NULL;
    274  1.18  pooka 		LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
    275  1.18  pooka 		pu->pu_cc_nstored++;
    276  1.20  pooka 	} else {
    277  1.20  pooka 		assert(!puffs_fakecc);
    278  1.20  pooka 		munmap(pcc, stacksize);
    279  1.18  pooka 	}
    280  1.14  pooka }
    281  1.14  pooka 
    282  1.14  pooka struct puffs_cc *
    283  1.14  pooka puffs_cc_getcc(struct puffs_usermount *pu)
    284  1.14  pooka {
    285  1.14  pooka 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    286  1.14  pooka 	uintptr_t bottom;
    287  1.14  pooka 
    288  1.14  pooka 	if (puffs_fakecc)
    289  1.14  pooka 		return &fakecc;
    290  1.14  pooka 
    291  1.14  pooka 	bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
    292  1.14  pooka 	return (struct puffs_cc *)bottom;
    293   1.1  pooka }
    294