Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.19
      1 /*	$NetBSD: callcontext.c,v 1.19 2008/01/17 17:43:14 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #if !defined(lint)
     30 __RCSID("$NetBSD: callcontext.c,v 1.19 2008/01/17 17:43:14 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <sys/types.h>
     34 #include <sys/mman.h>
     35 
     36 #include <assert.h>
     37 #include <errno.h>
     38 #ifdef PUFFS_WITH_THREADS
     39 #include <pthread.h>
     40 #endif
     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 /*
     51  * user stuff
     52  */
     53 
     54 void
     55 puffs_cc_yield(struct puffs_cc *pcc)
     56 {
     57 
     58 	assert(pcc->pcc_flags & PCC_REALCC);
     59 	pcc->pcc_flags &= ~PCC_BORROWED;
     60 
     61 	/* romanes eunt domus */
     62 	swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
     63 }
     64 
     65 void
     66 puffs_cc_continue(struct puffs_cc *pcc)
     67 {
     68 
     69 	assert(pcc->pcc_flags & PCC_REALCC);
     70 
     71 	/* ramble on */
     72 	swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
     73 }
     74 
     75 /*
     76  * "Borrows" pcc, *NOT* called from pcc owner.  Acts like continue.
     77  * So the idea is to use this, give something the context back to
     78  * run to completion and then jump back to where ever this was called
     79  * from after the op dispatching is complete (or if the pcc decides to
     80  * yield again).
     81  */
     82 void
     83 puffs_goto(struct puffs_cc *loanpcc)
     84 {
     85 
     86 	assert(loanpcc->pcc_flags & PCC_REALCC);
     87 	loanpcc->pcc_flags |= PCC_BORROWED;
     88 
     89 	swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
     90 }
     91 
     92 void
     93 puffs_cc_schedule(struct puffs_cc *pcc)
     94 {
     95 	struct puffs_usermount *pu = pcc->pcc_pu;
     96 
     97 	assert(pu->pu_state & PU_INLOOP);
     98 	TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent);
     99 }
    100 
    101 int
    102 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
    103 {
    104 
    105 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
    106 		errno = ESRCH;
    107 		return -1;
    108 	}
    109 
    110 	if (pid)
    111 		*pid = pcc->pcc_pid;
    112 	if (lid)
    113 		*lid = pcc->pcc_lid;
    114 	return 0;
    115 }
    116 
    117 #ifdef PUFFS_WITH_THREADS
    118 int pthread__stackid_setup(void *, size_t, pthread_t *);
    119 #endif
    120 
    121 /* for fakecc-users, need only one */
    122 static struct puffs_cc fakecc;
    123 
    124 int
    125 puffs_cc_create(struct puffs_usermount *pu, struct puffs_framebuf *pb,
    126 	int type, struct puffs_cc **pccp)
    127 {
    128 	struct puffs_cc *volatile pcc;
    129 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    130 	long psize = sysconf(_SC_PAGESIZE);
    131 	stack_t *st;
    132 	void *volatile sp;
    133 
    134 #ifdef PUFFS_WITH_THREADS
    135 	extern size_t pthread__stacksize;
    136 	stacksize = pthread__stacksize;
    137 #endif
    138 
    139 	/* Do we have a cached copy? */
    140 	if (pu->pu_cc_nstored) {
    141 		pcc = LIST_FIRST(&pu->pu_ccmagazin);
    142 		assert(pcc != NULL);
    143 
    144 		LIST_REMOVE(pcc, pcc_rope);
    145 		pu->pu_cc_nstored--;
    146 		goto finalize;
    147 	}
    148 
    149 	/*
    150 	 * There are two paths and in the long run we don't have time to
    151 	 * change the one we're on.  For non-real cc's, we just simply use
    152 	 * a static copy.  For the other cases, we mmap the stack and
    153 	 * manually reserve a bit from the top for the data structure
    154 	 * (or, well, the bottom).
    155 	 *
    156 	 * XXX: threaded mode doesn't work very well now.  Not that it's
    157 	 * supported anyhow.
    158 	 */
    159 	if (type == PCC_FAKECC) {
    160 		pcc = &fakecc;
    161 		sp = NULL;
    162 	} else {
    163 		sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
    164 		    MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift),
    165 		    -1, 0);
    166 		if (sp == MAP_FAILED)
    167 			return -1;
    168 
    169 		pcc = sp;
    170 		mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE);
    171 	}
    172 
    173 	memset(pcc, 0, sizeof(struct puffs_cc));
    174 	pcc->pcc_pu = pu;
    175 
    176 	/* Not a real cc?  Don't need to init more */
    177 	if (type != PCC_REALCC)
    178 		goto out;
    179 
    180 	/* initialize both ucontext's */
    181 	if (getcontext(&pcc->pcc_uc) == -1) {
    182 		munmap(pcc, stacksize);
    183 		return -1;
    184 	}
    185 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    186 		munmap(pcc, stacksize);
    187 		return -1;
    188 	}
    189 
    190 #ifdef PUFFS_WITH_THREADS
    191 	{
    192 	pthread_t pt;
    193 	extern int __isthreaded;
    194 	if (__isthreaded)
    195 		pthread__stackid_setup(sp, stacksize /* XXXb0rked */, &pt);
    196 	}
    197 #endif
    198 
    199  finalize:
    200 	assert(pcc->pcc_pu == pu);
    201 
    202 	/* return here.  it won't actually be "here" due to swapcontext() */
    203 	pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    204 
    205 	/* setup stack
    206 	 *
    207 	 * XXX: I guess this should theoretically be preserved by
    208 	 * swapcontext().  However, it gets lost.  So reinit it.
    209 	 */
    210 	st = &pcc->pcc_uc.uc_stack;
    211 	st->ss_sp = pcc;
    212 	st->ss_size = stacksize;
    213 	st->ss_flags = 0;
    214 
    215 	/*
    216 	 * Give us an initial context to jump to.
    217 	 *
    218 	 * XXX: Our manual page says that portable code shouldn't rely on
    219 	 * being able to pass pointers through makecontext().  kjk says
    220 	 * that NetBSD code doesn't need to worry about this.  uwe says
    221 	 * it would be like putting a "keep away from children" sign on a
    222 	 * box of toys.
    223 	 */
    224 	makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
    225 	    1, (uintptr_t)pcc);
    226 
    227  out:
    228 	pcc->pcc_pb = pb;
    229 	pcc->pcc_flags = type;
    230 
    231 	*pccp = pcc;
    232 	return 0;
    233 }
    234 
    235 void
    236 puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    237 {
    238 
    239 	pcc->pcc_pid = pid;
    240 	pcc->pcc_lid = lid;
    241 	pcc->pcc_flags |= PCC_HASCALLER;
    242 }
    243 
    244 void
    245 puffs_cc_destroy(struct puffs_cc *pcc)
    246 {
    247 	struct puffs_usermount *pu = pcc->pcc_pu;
    248 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    249 
    250 	/* not over limit?  stuff away in the store */
    251 	if (pu->pu_cc_nstored < PUFFS_CCMAXSTORE
    252 	    && pcc->pcc_flags & PCC_REALCC) {
    253 		pcc->pcc_flags &= ~PCC_DONE;
    254 		LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
    255 		pu->pu_cc_nstored++;
    256 		return;
    257 	}
    258 
    259 	/* else: just dump it */
    260 
    261 #ifdef PUFFS_WITH_THREADS
    262 	extern size_t pthread__stacksize;
    263 	stacksize = pthread__stacksize;
    264 #endif
    265 
    266 	if ((pcc->pcc_flags & PCC_FAKECC) == 0)
    267 		munmap(pcc, stacksize);
    268 }
    269 
    270 struct puffs_cc *
    271 puffs_cc_getcc(struct puffs_usermount *pu)
    272 {
    273 	extern int puffs_fakecc, puffs_usethreads;
    274 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    275 	uintptr_t bottom;
    276 
    277 	if (puffs_fakecc)
    278 		return &fakecc;
    279 #ifndef PUFFS_WITH_THREADS
    280 	if (puffs_usethreads)
    281 		return &fakecc;
    282 #endif
    283 
    284 	bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
    285 	return (struct puffs_cc *)bottom;
    286 }
    287