Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.5
      1 /*	$NetBSD: callcontext.c,v 1.5 2007/05/10 12:36:44 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  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if !defined(lint)
     33 __RCSID("$NetBSD: callcontext.c,v 1.5 2007/05/10 12:36:44 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <sys/types.h>
     37 
     38 #include <assert.h>
     39 #include <puffs.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <ucontext.h>
     44 
     45 #include "puffs_priv.h"
     46 
     47 /*
     48  * user stuff
     49  */
     50 
     51 void
     52 puffs_cc_yield(struct puffs_cc *pcc)
     53 {
     54 
     55 	assert((pcc->pcc_flags & PCC_DONE) == 0);
     56 	assert(pcc->pcc_flags & PCC_REALCC);
     57 
     58 	/* romanes eunt domus */
     59 	swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
     60 }
     61 
     62 void
     63 puffs_cc_continue(struct puffs_cc *pcc)
     64 {
     65 
     66 	assert(pcc->pcc_flags & PCC_REALCC);
     67 
     68 	/* ramble on */
     69 	swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
     70 }
     71 
     72 struct puffs_usermount *
     73 puffs_cc_getusermount(struct puffs_cc *pcc)
     74 {
     75 
     76 	return pcc->pcc_pu;
     77 }
     78 
     79 void *
     80 puffs_cc_getspecific(struct puffs_cc *pcc)
     81 {
     82 
     83 	return puffs_getspecific(pcc->pcc_pu);
     84 }
     85 
     86 struct puffs_cc *
     87 puffs_cc_create(struct puffs_usermount *pu)
     88 {
     89 	struct puffs_cc *volatile pcc;
     90 	stack_t *st;
     91 
     92 	pcc = malloc(sizeof(struct puffs_cc));
     93 	if (!pcc)
     94 		return NULL;
     95 	memset(pcc, 0, sizeof(struct puffs_cc));
     96 	pcc->pcc_pu = pu;
     97 	pcc->pcc_priv = (void *)0xdeadbeef;
     98 	pcc->pcc_flags = PCC_REALCC;
     99 
    100 	/* initialize both ucontext's */
    101 	if (getcontext(&pcc->pcc_uc) == -1) {
    102 		free(pcc);
    103 		return NULL;
    104 	}
    105 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    106 		free(pcc);
    107 		return NULL;
    108 	}
    109 	/* return here.  it won't actually be "here" due to swapcontext() */
    110 	pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    111 
    112 	/* allocate stack for execution */
    113 	st = &pcc->pcc_uc.uc_stack;
    114 	st->ss_sp = pcc->pcc_stack = malloc(pu->pu_cc_stacksize);
    115 	if (st->ss_sp == NULL) {
    116 		free(pcc);
    117 		return NULL;
    118 	}
    119 	st->ss_size = pu->pu_cc_stacksize;
    120 	st->ss_flags = 0;
    121 
    122 	/*
    123 	 * Give us an initial context to jump to.
    124 	 *
    125 	 * XXX: Our manual page says that portable code shouldn't rely on
    126 	 * being able to pass pointers through makecontext().  kjk says
    127 	 * that NetBSD code doesn't need to worry about this.  uwe says
    128 	 * it would be like putting a "keep away from children" sign on a
    129 	 * box of toys.  I didn't ask what simon says; he's probably busy
    130 	 * "fixing" typos in comments.
    131 	 */
    132 	makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
    133 	    1, (uintptr_t)pcc);
    134 
    135 	return pcc;
    136 }
    137 
    138 void
    139 puffs_cc_destroy(struct puffs_cc *pcc)
    140 {
    141 
    142 	if (pcc->pcc_preq)
    143 		free(pcc->pcc_preq);
    144 	free(pcc->pcc_stack);
    145 	free(pcc);
    146 }
    147