Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.14
      1 /*	$NetBSD: callcontext.c,v 1.14 2007/11/29 17:47:54 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.14 2007/11/29 17:47:54 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, entries);
     99 }
    100 
    101 struct puffs_usermount *
    102 puffs_cc_getusermount(struct puffs_cc *pcc)
    103 {
    104 
    105 	return pcc->pcc_pu;
    106 }
    107 
    108 void *
    109 puffs_cc_getspecific(struct puffs_cc *pcc)
    110 {
    111 
    112 	return puffs_getspecific(pcc->pcc_pu);
    113 }
    114 
    115 int
    116 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
    117 {
    118 
    119 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
    120 		errno = ESRCH;
    121 		return -1;
    122 	}
    123 
    124 	if (pid)
    125 		*pid = pcc->pcc_pid;
    126 	if (lid)
    127 		*lid = pcc->pcc_lid;
    128 	return 0;
    129 }
    130 
    131 #ifdef PUFFS_WITH_THREADS
    132 int pthread__stackid_setup(void *, size_t, pthread_t *);
    133 #endif
    134 
    135 /* for fakecc-users, need only one */
    136 static struct puffs_cc fakecc;
    137 
    138 int
    139 puffs_cc_create(struct puffs_usermount *pu, struct puffs_req *preq,
    140 	int type, struct puffs_cc **pccp)
    141 {
    142 	struct puffs_cc *volatile pcc;
    143 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    144 	long psize = sysconf(_SC_PAGESIZE);
    145 	stack_t *st;
    146 	void *volatile sp;
    147 
    148 #ifdef PUFFS_WITH_THREADS
    149 	extern size_t pthread__stacksize;
    150 	stacksize = pthread__stacksize;
    151 #endif
    152 
    153 	/*
    154 	 * There are two paths and in the long run we don't have time to
    155 	 * change the one we're on.  For non-real cc's, we just simply use
    156 	 * a static copy.  For the other cases, we mmap the stack and
    157 	 * manually reserve a bit from the top for the data structure
    158 	 * (or, well, the bottom).
    159 	 *
    160 	 * XXX: threaded mode doesn't work very well now.  Not that it's
    161 	 * supported anyhow.
    162 	 */
    163 	if (type == PCC_FAKECC) {
    164 		pcc = &fakecc;
    165 		sp = NULL;
    166 	} else {
    167 		sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
    168 		    MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift),
    169 		    -1, 0);
    170 		if (sp == MAP_FAILED)
    171 			return -1;
    172 
    173 		pcc = sp;
    174 		sp = (uint8_t *)sp + psize;
    175 	}
    176 
    177 	memset(pcc, 0, sizeof(struct puffs_cc));
    178 	pcc->pcc_pu = pu;
    179 	pcc->pcc_preq = preq;
    180 	pcc->pcc_flags = type;
    181 
    182 	/* Not a real cc?  Don't need to init more */
    183 	if (pcc->pcc_flags != PCC_REALCC)
    184 		goto out;
    185 
    186 	/* initialize both ucontext's */
    187 	if (getcontext(&pcc->pcc_uc) == -1) {
    188 		munmap(pcc, stacksize);
    189 		return -1;
    190 	}
    191 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    192 		munmap(pcc, stacksize);
    193 		return -1;
    194 	}
    195 	/* return here.  it won't actually be "here" due to swapcontext() */
    196 	pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    197 
    198 	/* allocate stack for execution */
    199 	st = &pcc->pcc_uc.uc_stack;
    200 
    201 	st->ss_sp = pcc->pcc_stack = sp;
    202 	st->ss_size = stacksize - psize;
    203 	st->ss_flags = 0;
    204 
    205 #ifdef PUFFS_WITH_THREADS
    206 	{
    207 	pthread_t pt;
    208 	extern int __isthreaded;
    209 	if (__isthreaded)
    210 		pthread__stackid_setup(sp, stacksize /* XXXb0rked */, &pt);
    211 	}
    212 #endif
    213 
    214 	/*
    215 	 * Give us an initial context to jump to.
    216 	 *
    217 	 * XXX: Our manual page says that portable code shouldn't rely on
    218 	 * being able to pass pointers through makecontext().  kjk says
    219 	 * that NetBSD code doesn't need to worry about this.  uwe says
    220 	 * it would be like putting a "keep away from children" sign on a
    221 	 * box of toys.  I didn't ask what simon says; he's probably busy
    222 	 * "fixing" typos in comments.
    223 	 */
    224 	makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
    225 	    1, (uintptr_t)pcc);
    226 
    227  out:
    228 	*pccp = pcc;
    229 	return 0;
    230 }
    231 
    232 void
    233 puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    234 {
    235 
    236 	pcc->pcc_pid = pid;
    237 	pcc->pcc_lid = lid;
    238 	pcc->pcc_flags |= PCC_HASCALLER;
    239 }
    240 
    241 void
    242 puffs_cc_destroy(struct puffs_cc *pcc)
    243 {
    244 	struct puffs_usermount *pu = pcc->pcc_pu;
    245 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    246 
    247 #ifdef PUFFS_WITH_THREADS
    248 	extern size_t pthread__stacksize;
    249 	stacksize = pthread__stacksize;
    250 #endif
    251 
    252 	free(pcc->pcc_preq); /* XXX */
    253 	if ((pcc->pcc_flags & PCC_FAKECC) == 0)
    254 		munmap(pcc, stacksize);
    255 }
    256 
    257 struct puffs_cc *
    258 puffs_cc_getcc(struct puffs_usermount *pu)
    259 {
    260 	extern int puffs_fakecc, puffs_usethreads;
    261 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    262 	uintptr_t bottom;
    263 
    264 	if (puffs_fakecc)
    265 		return &fakecc;
    266 #ifndef PUFFS_WITH_THREADS
    267 	if (puffs_usethreads)
    268 		return &fakecc;
    269 #endif
    270 
    271 	bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
    272 	return (struct puffs_cc *)bottom;
    273 }
    274