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