Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.10
      1  1.10  pooka /*	$NetBSD: callcontext.c,v 1.10 2007/10/21 19:25:58 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1  pooka  * modification, are permitted provided that the following conditions
      8   1.1  pooka  * are met:
      9   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1  pooka  *
     15   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1  pooka  * SUCH DAMAGE.
     26   1.1  pooka  */
     27   1.1  pooka 
     28   1.1  pooka #include <sys/cdefs.h>
     29   1.1  pooka #if !defined(lint)
     30  1.10  pooka __RCSID("$NetBSD: callcontext.c,v 1.10 2007/10/21 19:25:58 pooka Exp $");
     31   1.1  pooka #endif /* !lint */
     32   1.1  pooka 
     33   1.1  pooka #include <sys/types.h>
     34   1.1  pooka 
     35   1.1  pooka #include <assert.h>
     36   1.9  pooka #include <errno.h>
     37   1.1  pooka #include <puffs.h>
     38   1.1  pooka #include <stdio.h>
     39   1.1  pooka #include <stdlib.h>
     40   1.1  pooka #include <string.h>
     41   1.3  pooka #include <ucontext.h>
     42   1.1  pooka 
     43   1.1  pooka #include "puffs_priv.h"
     44   1.1  pooka 
     45   1.1  pooka /*
     46   1.1  pooka  * user stuff
     47   1.1  pooka  */
     48   1.1  pooka 
     49   1.1  pooka void
     50   1.1  pooka puffs_cc_yield(struct puffs_cc *pcc)
     51   1.1  pooka {
     52   1.1  pooka 
     53   1.5  pooka 	assert(pcc->pcc_flags & PCC_REALCC);
     54   1.6  pooka 	pcc->pcc_flags &= ~PCC_BORROWED;
     55   1.1  pooka 
     56   1.1  pooka 	/* romanes eunt domus */
     57   1.1  pooka 	swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
     58   1.1  pooka }
     59   1.1  pooka 
     60   1.1  pooka void
     61   1.1  pooka puffs_cc_continue(struct puffs_cc *pcc)
     62   1.1  pooka {
     63   1.1  pooka 
     64   1.5  pooka 	assert(pcc->pcc_flags & PCC_REALCC);
     65   1.5  pooka 
     66   1.1  pooka 	/* ramble on */
     67   1.1  pooka 	swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
     68   1.1  pooka }
     69   1.1  pooka 
     70   1.6  pooka /*
     71   1.6  pooka  * "Borrows" pcc, *NOT* called from pcc owner.  Acts like continue.
     72   1.6  pooka  * So the idea is to use this, give something the context back to
     73   1.6  pooka  * run to completion and then jump back to where ever this was called
     74   1.6  pooka  * from after the op dispatching is complete (or if the pcc decides to
     75   1.6  pooka  * yield again).
     76   1.6  pooka  */
     77   1.6  pooka void
     78   1.6  pooka puffs_goto(struct puffs_cc *loanpcc)
     79   1.6  pooka {
     80   1.6  pooka 
     81   1.6  pooka 	assert(loanpcc->pcc_flags & PCC_REALCC);
     82   1.6  pooka 	loanpcc->pcc_flags |= PCC_BORROWED;
     83   1.6  pooka 
     84   1.6  pooka 	swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
     85   1.6  pooka }
     86   1.6  pooka 
     87  1.10  pooka void
     88  1.10  pooka puffs_cc_schedule(struct puffs_cc *pcc)
     89  1.10  pooka {
     90  1.10  pooka 	struct puffs_usermount *pu = pcc->pcc_pu;
     91  1.10  pooka 
     92  1.10  pooka 	assert(pu->pu_state & PU_INLOOP);
     93  1.10  pooka 	TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, entries);
     94  1.10  pooka }
     95  1.10  pooka 
     96   1.1  pooka struct puffs_usermount *
     97   1.1  pooka puffs_cc_getusermount(struct puffs_cc *pcc)
     98   1.1  pooka {
     99   1.1  pooka 
    100   1.1  pooka 	return pcc->pcc_pu;
    101   1.1  pooka }
    102   1.1  pooka 
    103   1.4  pooka void *
    104   1.4  pooka puffs_cc_getspecific(struct puffs_cc *pcc)
    105   1.4  pooka {
    106   1.4  pooka 
    107   1.4  pooka 	return puffs_getspecific(pcc->pcc_pu);
    108   1.4  pooka }
    109   1.4  pooka 
    110   1.9  pooka int
    111   1.9  pooka puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
    112   1.9  pooka {
    113   1.9  pooka 
    114   1.9  pooka 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
    115   1.9  pooka 		errno = ESRCH;
    116   1.9  pooka 		return -1;
    117   1.9  pooka 	}
    118   1.9  pooka 
    119   1.9  pooka 	if (pid)
    120   1.9  pooka 		*pid = pcc->pcc_pid;
    121   1.9  pooka 	if (lid)
    122   1.9  pooka 		*lid = pcc->pcc_lid;
    123   1.9  pooka 	return 0;
    124   1.9  pooka }
    125   1.9  pooka 
    126   1.1  pooka struct puffs_cc *
    127   1.2  pooka puffs_cc_create(struct puffs_usermount *pu)
    128   1.1  pooka {
    129   1.1  pooka 	struct puffs_cc *volatile pcc;
    130   1.1  pooka 	stack_t *st;
    131   1.1  pooka 
    132   1.1  pooka 	pcc = malloc(sizeof(struct puffs_cc));
    133   1.1  pooka 	if (!pcc)
    134   1.1  pooka 		return NULL;
    135   1.1  pooka 	memset(pcc, 0, sizeof(struct puffs_cc));
    136   1.1  pooka 	pcc->pcc_pu = pu;
    137   1.1  pooka 	pcc->pcc_flags = PCC_REALCC;
    138   1.1  pooka 
    139   1.1  pooka 	/* initialize both ucontext's */
    140   1.1  pooka 	if (getcontext(&pcc->pcc_uc) == -1) {
    141   1.1  pooka 		free(pcc);
    142   1.1  pooka 		return NULL;
    143   1.1  pooka 	}
    144   1.1  pooka 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    145   1.1  pooka 		free(pcc);
    146   1.1  pooka 		return NULL;
    147   1.1  pooka 	}
    148   1.1  pooka 	/* return here.  it won't actually be "here" due to swapcontext() */
    149   1.1  pooka 	pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    150   1.1  pooka 
    151   1.1  pooka 	/* allocate stack for execution */
    152   1.1  pooka 	st = &pcc->pcc_uc.uc_stack;
    153   1.1  pooka 	st->ss_sp = pcc->pcc_stack = malloc(pu->pu_cc_stacksize);
    154   1.1  pooka 	if (st->ss_sp == NULL) {
    155   1.1  pooka 		free(pcc);
    156   1.1  pooka 		return NULL;
    157   1.1  pooka 	}
    158   1.1  pooka 	st->ss_size = pu->pu_cc_stacksize;
    159   1.1  pooka 	st->ss_flags = 0;
    160   1.1  pooka 
    161   1.1  pooka 	/*
    162   1.1  pooka 	 * Give us an initial context to jump to.
    163   1.1  pooka 	 *
    164   1.1  pooka 	 * XXX: Our manual page says that portable code shouldn't rely on
    165   1.1  pooka 	 * being able to pass pointers through makecontext().  kjk says
    166   1.1  pooka 	 * that NetBSD code doesn't need to worry about this.  uwe says
    167   1.1  pooka 	 * it would be like putting a "keep away from children" sign on a
    168   1.1  pooka 	 * box of toys.  I didn't ask what simon says; he's probably busy
    169   1.1  pooka 	 * "fixing" typos in comments.
    170   1.1  pooka 	 */
    171   1.1  pooka 	makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
    172   1.1  pooka 	    1, (uintptr_t)pcc);
    173   1.1  pooka 
    174   1.1  pooka 	return pcc;
    175   1.1  pooka }
    176   1.1  pooka 
    177   1.1  pooka void
    178   1.9  pooka puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    179   1.9  pooka {
    180   1.9  pooka 
    181   1.9  pooka 	pcc->pcc_pid = pid;
    182   1.9  pooka 	pcc->pcc_lid = lid;
    183   1.9  pooka 	pcc->pcc_flags |= PCC_HASCALLER;
    184   1.9  pooka }
    185   1.9  pooka 
    186   1.9  pooka void
    187   1.1  pooka puffs_cc_destroy(struct puffs_cc *pcc)
    188   1.1  pooka {
    189   1.1  pooka 
    190   1.1  pooka 	free(pcc->pcc_stack);
    191   1.1  pooka 	free(pcc);
    192   1.1  pooka }
    193