Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.13
      1 /*	$NetBSD: callcontext.c,v 1.13 2007/10/31 16:09:09 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.13 2007/10/31 16:09:09 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 
     47 #include "puffs_priv.h"
     48 
     49 /*
     50  * user stuff
     51  */
     52 
     53 void
     54 puffs_cc_yield(struct puffs_cc *pcc)
     55 {
     56 
     57 	assert(pcc->pcc_flags & PCC_REALCC);
     58 	pcc->pcc_flags &= ~PCC_BORROWED;
     59 
     60 	/* romanes eunt domus */
     61 	swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
     62 }
     63 
     64 void
     65 puffs_cc_continue(struct puffs_cc *pcc)
     66 {
     67 
     68 	assert(pcc->pcc_flags & PCC_REALCC);
     69 
     70 	/* ramble on */
     71 	swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
     72 }
     73 
     74 /*
     75  * "Borrows" pcc, *NOT* called from pcc owner.  Acts like continue.
     76  * So the idea is to use this, give something the context back to
     77  * run to completion and then jump back to where ever this was called
     78  * from after the op dispatching is complete (or if the pcc decides to
     79  * yield again).
     80  */
     81 void
     82 puffs_goto(struct puffs_cc *loanpcc)
     83 {
     84 
     85 	assert(loanpcc->pcc_flags & PCC_REALCC);
     86 	loanpcc->pcc_flags |= PCC_BORROWED;
     87 
     88 	swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
     89 }
     90 
     91 void
     92 puffs_cc_schedule(struct puffs_cc *pcc)
     93 {
     94 	struct puffs_usermount *pu = pcc->pcc_pu;
     95 
     96 	assert(pu->pu_state & PU_INLOOP);
     97 	TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, entries);
     98 }
     99 
    100 struct puffs_usermount *
    101 puffs_cc_getusermount(struct puffs_cc *pcc)
    102 {
    103 
    104 	return pcc->pcc_pu;
    105 }
    106 
    107 void *
    108 puffs_cc_getspecific(struct puffs_cc *pcc)
    109 {
    110 
    111 	return puffs_getspecific(pcc->pcc_pu);
    112 }
    113 
    114 int
    115 puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
    116 {
    117 
    118 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
    119 		errno = ESRCH;
    120 		return -1;
    121 	}
    122 
    123 	if (pid)
    124 		*pid = pcc->pcc_pid;
    125 	if (lid)
    126 		*lid = pcc->pcc_lid;
    127 	return 0;
    128 }
    129 
    130 #ifdef PUFFS_WITH_THREADS
    131 int pthread__stackid_setup(void *, size_t, pthread_t *);
    132 #endif
    133 
    134 int
    135 puffs_cc_create(struct puffs_usermount *pu, struct puffs_req *preq,
    136 	int type, struct puffs_cc **pccp)
    137 {
    138 	struct puffs_cc *volatile pcc;
    139 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    140 	stack_t *st;
    141 	void *sp;
    142 
    143 #ifdef PUFFS_WITH_THREADS
    144 	extern size_t pthread__stacksize;
    145 	stacksize = pthread__stacksize;
    146 #endif
    147 
    148 	pcc = malloc(sizeof(struct puffs_cc));
    149 	if (!pcc)
    150 		return -1;
    151 	memset(pcc, 0, sizeof(struct puffs_cc));
    152 	pcc->pcc_pu = pu;
    153 	pcc->pcc_preq = preq;
    154 
    155 	pcc->pcc_flags = type;
    156 	if (pcc->pcc_flags != PCC_REALCC)
    157 		goto out;
    158 
    159 	/* initialize both ucontext's */
    160 	if (getcontext(&pcc->pcc_uc) == -1) {
    161 		free(pcc);
    162 		return -1;
    163 	}
    164 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    165 		free(pcc);
    166 		return -1;
    167 	}
    168 	/* return here.  it won't actually be "here" due to swapcontext() */
    169 	pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    170 
    171 	/* allocate stack for execution */
    172 	st = &pcc->pcc_uc.uc_stack;
    173 	sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
    174 	    MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0);
    175 	if (sp == MAP_FAILED) {
    176 		free(pcc);
    177 		return -1;
    178 	}
    179 	st->ss_sp = pcc->pcc_stack = sp;
    180 	st->ss_size = stacksize;
    181 	st->ss_flags = 0;
    182 
    183 #ifdef PUFFS_WITH_THREADS
    184 	{
    185 	pthread_t pt;
    186 	extern int __isthreaded;
    187 	if (__isthreaded)
    188 		pthread__stackid_setup(sp, stacksize, &pt);
    189 	}
    190 #endif
    191 
    192 	/*
    193 	 * Give us an initial context to jump to.
    194 	 *
    195 	 * XXX: Our manual page says that portable code shouldn't rely on
    196 	 * being able to pass pointers through makecontext().  kjk says
    197 	 * that NetBSD code doesn't need to worry about this.  uwe says
    198 	 * it would be like putting a "keep away from children" sign on a
    199 	 * box of toys.  I didn't ask what simon says; he's probably busy
    200 	 * "fixing" typos in comments.
    201 	 */
    202 	makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
    203 	    1, (uintptr_t)pcc);
    204 
    205  out:
    206 	*pccp = pcc;
    207 	return 0;
    208 }
    209 
    210 void
    211 puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    212 {
    213 
    214 	pcc->pcc_pid = pid;
    215 	pcc->pcc_lid = lid;
    216 	pcc->pcc_flags |= PCC_HASCALLER;
    217 }
    218 
    219 void
    220 puffs_cc_destroy(struct puffs_cc *pcc)
    221 {
    222 	struct puffs_usermount *pu = pcc->pcc_pu;
    223 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    224 
    225 #ifdef PUFFS_WITH_THREADS
    226 	extern size_t pthread__stacksize;
    227 	stacksize = pthread__stacksize;
    228 #endif
    229 
    230 	if (pcc->pcc_flags & PCC_REALCC)
    231 		munmap(pcc->pcc_stack, stacksize);
    232 	free(pcc->pcc_preq);
    233 	free(pcc);
    234 }
    235