Home | History | Annotate | Line # | Download | only in libpuffs
callcontext.c revision 1.7.4.2
      1  1.7.4.2   matt /*	$NetBSD: callcontext.c,v 1.7.4.2 2008/01/09 01:36:44 matt 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.7.4.2   matt __RCSID("$NetBSD: callcontext.c,v 1.7.4.2 2008/01/09 01:36:44 matt Exp $");
     31      1.1  pooka #endif /* !lint */
     32      1.1  pooka 
     33      1.1  pooka #include <sys/types.h>
     34  1.7.4.1   matt #include <sys/mman.h>
     35      1.1  pooka 
     36      1.1  pooka #include <assert.h>
     37  1.7.4.1   matt #include <errno.h>
     38  1.7.4.1   matt #ifdef PUFFS_WITH_THREADS
     39  1.7.4.1   matt #include <pthread.h>
     40  1.7.4.1   matt #endif
     41      1.1  pooka #include <puffs.h>
     42      1.1  pooka #include <stdio.h>
     43      1.1  pooka #include <stdlib.h>
     44      1.1  pooka #include <string.h>
     45      1.3  pooka #include <ucontext.h>
     46  1.7.4.2   matt #include <unistd.h>
     47      1.1  pooka 
     48      1.1  pooka #include "puffs_priv.h"
     49      1.1  pooka 
     50      1.1  pooka /*
     51      1.1  pooka  * user stuff
     52      1.1  pooka  */
     53      1.1  pooka 
     54      1.1  pooka void
     55      1.1  pooka puffs_cc_yield(struct puffs_cc *pcc)
     56      1.1  pooka {
     57      1.1  pooka 
     58      1.5  pooka 	assert(pcc->pcc_flags & PCC_REALCC);
     59      1.6  pooka 	pcc->pcc_flags &= ~PCC_BORROWED;
     60      1.1  pooka 
     61      1.1  pooka 	/* romanes eunt domus */
     62      1.1  pooka 	swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
     63      1.1  pooka }
     64      1.1  pooka 
     65      1.1  pooka void
     66      1.1  pooka puffs_cc_continue(struct puffs_cc *pcc)
     67      1.1  pooka {
     68      1.1  pooka 
     69      1.5  pooka 	assert(pcc->pcc_flags & PCC_REALCC);
     70      1.5  pooka 
     71      1.1  pooka 	/* ramble on */
     72      1.1  pooka 	swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
     73      1.1  pooka }
     74      1.1  pooka 
     75      1.6  pooka /*
     76      1.6  pooka  * "Borrows" pcc, *NOT* called from pcc owner.  Acts like continue.
     77      1.6  pooka  * So the idea is to use this, give something the context back to
     78      1.6  pooka  * run to completion and then jump back to where ever this was called
     79      1.6  pooka  * from after the op dispatching is complete (or if the pcc decides to
     80      1.6  pooka  * yield again).
     81      1.6  pooka  */
     82      1.6  pooka void
     83      1.6  pooka puffs_goto(struct puffs_cc *loanpcc)
     84      1.6  pooka {
     85      1.6  pooka 
     86      1.6  pooka 	assert(loanpcc->pcc_flags & PCC_REALCC);
     87      1.6  pooka 	loanpcc->pcc_flags |= PCC_BORROWED;
     88      1.6  pooka 
     89      1.6  pooka 	swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
     90      1.6  pooka }
     91      1.6  pooka 
     92  1.7.4.1   matt void
     93  1.7.4.1   matt puffs_cc_schedule(struct puffs_cc *pcc)
     94  1.7.4.1   matt {
     95  1.7.4.1   matt 	struct puffs_usermount *pu = pcc->pcc_pu;
     96  1.7.4.1   matt 
     97  1.7.4.1   matt 	assert(pu->pu_state & PU_INLOOP);
     98  1.7.4.1   matt 	TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, entries);
     99  1.7.4.1   matt }
    100  1.7.4.1   matt 
    101  1.7.4.1   matt int
    102  1.7.4.1   matt puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
    103  1.7.4.1   matt {
    104  1.7.4.1   matt 
    105  1.7.4.1   matt 	if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
    106  1.7.4.1   matt 		errno = ESRCH;
    107  1.7.4.1   matt 		return -1;
    108  1.7.4.1   matt 	}
    109  1.7.4.1   matt 
    110  1.7.4.1   matt 	if (pid)
    111  1.7.4.1   matt 		*pid = pcc->pcc_pid;
    112  1.7.4.1   matt 	if (lid)
    113  1.7.4.1   matt 		*lid = pcc->pcc_lid;
    114  1.7.4.1   matt 	return 0;
    115  1.7.4.1   matt }
    116  1.7.4.1   matt 
    117  1.7.4.1   matt #ifdef PUFFS_WITH_THREADS
    118  1.7.4.1   matt int pthread__stackid_setup(void *, size_t, pthread_t *);
    119  1.7.4.1   matt #endif
    120  1.7.4.1   matt 
    121  1.7.4.2   matt /* for fakecc-users, need only one */
    122  1.7.4.2   matt static struct puffs_cc fakecc;
    123  1.7.4.2   matt 
    124  1.7.4.1   matt int
    125  1.7.4.2   matt puffs_cc_create(struct puffs_usermount *pu, struct puffs_framebuf *pb,
    126  1.7.4.1   matt 	int type, struct puffs_cc **pccp)
    127      1.1  pooka {
    128      1.1  pooka 	struct puffs_cc *volatile pcc;
    129  1.7.4.1   matt 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    130  1.7.4.2   matt 	long psize = sysconf(_SC_PAGESIZE);
    131      1.1  pooka 	stack_t *st;
    132  1.7.4.2   matt 	void *volatile sp;
    133  1.7.4.1   matt 
    134  1.7.4.1   matt #ifdef PUFFS_WITH_THREADS
    135  1.7.4.1   matt 	extern size_t pthread__stacksize;
    136  1.7.4.1   matt 	stacksize = pthread__stacksize;
    137  1.7.4.1   matt #endif
    138      1.1  pooka 
    139  1.7.4.2   matt 	/*
    140  1.7.4.2   matt 	 * There are two paths and in the long run we don't have time to
    141  1.7.4.2   matt 	 * change the one we're on.  For non-real cc's, we just simply use
    142  1.7.4.2   matt 	 * a static copy.  For the other cases, we mmap the stack and
    143  1.7.4.2   matt 	 * manually reserve a bit from the top for the data structure
    144  1.7.4.2   matt 	 * (or, well, the bottom).
    145  1.7.4.2   matt 	 *
    146  1.7.4.2   matt 	 * XXX: threaded mode doesn't work very well now.  Not that it's
    147  1.7.4.2   matt 	 * supported anyhow.
    148  1.7.4.2   matt 	 */
    149  1.7.4.2   matt 	if (type == PCC_FAKECC) {
    150  1.7.4.2   matt 		pcc = &fakecc;
    151  1.7.4.2   matt 		sp = NULL;
    152  1.7.4.2   matt 	} else {
    153  1.7.4.2   matt 		sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
    154  1.7.4.2   matt 		    MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift),
    155  1.7.4.2   matt 		    -1, 0);
    156  1.7.4.2   matt 		if (sp == MAP_FAILED)
    157  1.7.4.2   matt 			return -1;
    158  1.7.4.2   matt 
    159  1.7.4.2   matt 		pcc = sp;
    160  1.7.4.2   matt 		sp = (uint8_t *)sp + psize;
    161  1.7.4.2   matt 	}
    162  1.7.4.2   matt 
    163      1.1  pooka 	memset(pcc, 0, sizeof(struct puffs_cc));
    164      1.1  pooka 	pcc->pcc_pu = pu;
    165  1.7.4.2   matt 	pcc->pcc_pb = pb;
    166  1.7.4.1   matt 	pcc->pcc_flags = type;
    167  1.7.4.2   matt 
    168  1.7.4.2   matt 	/* Not a real cc?  Don't need to init more */
    169  1.7.4.1   matt 	if (pcc->pcc_flags != PCC_REALCC)
    170  1.7.4.1   matt 		goto out;
    171      1.1  pooka 
    172      1.1  pooka 	/* initialize both ucontext's */
    173      1.1  pooka 	if (getcontext(&pcc->pcc_uc) == -1) {
    174  1.7.4.2   matt 		munmap(pcc, stacksize);
    175  1.7.4.1   matt 		return -1;
    176      1.1  pooka 	}
    177      1.1  pooka 	if (getcontext(&pcc->pcc_uc_ret) == -1) {
    178  1.7.4.2   matt 		munmap(pcc, stacksize);
    179  1.7.4.1   matt 		return -1;
    180      1.1  pooka 	}
    181      1.1  pooka 	/* return here.  it won't actually be "here" due to swapcontext() */
    182      1.1  pooka 	pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
    183      1.1  pooka 
    184      1.1  pooka 	/* allocate stack for execution */
    185      1.1  pooka 	st = &pcc->pcc_uc.uc_stack;
    186  1.7.4.2   matt 
    187  1.7.4.1   matt 	st->ss_sp = pcc->pcc_stack = sp;
    188  1.7.4.2   matt 	st->ss_size = stacksize - psize;
    189      1.1  pooka 	st->ss_flags = 0;
    190      1.1  pooka 
    191  1.7.4.1   matt #ifdef PUFFS_WITH_THREADS
    192  1.7.4.1   matt 	{
    193  1.7.4.1   matt 	pthread_t pt;
    194  1.7.4.1   matt 	extern int __isthreaded;
    195  1.7.4.1   matt 	if (__isthreaded)
    196  1.7.4.2   matt 		pthread__stackid_setup(sp, stacksize /* XXXb0rked */, &pt);
    197  1.7.4.1   matt 	}
    198  1.7.4.1   matt #endif
    199  1.7.4.1   matt 
    200      1.1  pooka 	/*
    201      1.1  pooka 	 * Give us an initial context to jump to.
    202      1.1  pooka 	 *
    203      1.1  pooka 	 * XXX: Our manual page says that portable code shouldn't rely on
    204      1.1  pooka 	 * being able to pass pointers through makecontext().  kjk says
    205      1.1  pooka 	 * that NetBSD code doesn't need to worry about this.  uwe says
    206      1.1  pooka 	 * it would be like putting a "keep away from children" sign on a
    207      1.1  pooka 	 * box of toys.  I didn't ask what simon says; he's probably busy
    208      1.1  pooka 	 * "fixing" typos in comments.
    209      1.1  pooka 	 */
    210      1.1  pooka 	makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
    211      1.1  pooka 	    1, (uintptr_t)pcc);
    212      1.1  pooka 
    213  1.7.4.1   matt  out:
    214  1.7.4.1   matt 	*pccp = pcc;
    215  1.7.4.1   matt 	return 0;
    216  1.7.4.1   matt }
    217  1.7.4.1   matt 
    218  1.7.4.1   matt void
    219  1.7.4.1   matt puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
    220  1.7.4.1   matt {
    221  1.7.4.1   matt 
    222  1.7.4.1   matt 	pcc->pcc_pid = pid;
    223  1.7.4.1   matt 	pcc->pcc_lid = lid;
    224  1.7.4.1   matt 	pcc->pcc_flags |= PCC_HASCALLER;
    225      1.1  pooka }
    226      1.1  pooka 
    227      1.1  pooka void
    228      1.1  pooka puffs_cc_destroy(struct puffs_cc *pcc)
    229      1.1  pooka {
    230  1.7.4.1   matt 	struct puffs_usermount *pu = pcc->pcc_pu;
    231  1.7.4.1   matt 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    232      1.1  pooka 
    233  1.7.4.1   matt #ifdef PUFFS_WITH_THREADS
    234  1.7.4.1   matt 	extern size_t pthread__stacksize;
    235  1.7.4.1   matt 	stacksize = pthread__stacksize;
    236  1.7.4.1   matt #endif
    237  1.7.4.1   matt 
    238  1.7.4.2   matt 	if ((pcc->pcc_flags & PCC_FAKECC) == 0)
    239  1.7.4.2   matt 		munmap(pcc, stacksize);
    240  1.7.4.2   matt }
    241  1.7.4.2   matt 
    242  1.7.4.2   matt struct puffs_cc *
    243  1.7.4.2   matt puffs_cc_getcc(struct puffs_usermount *pu)
    244  1.7.4.2   matt {
    245  1.7.4.2   matt 	extern int puffs_fakecc, puffs_usethreads;
    246  1.7.4.2   matt 	size_t stacksize = 1<<pu->pu_cc_stackshift;
    247  1.7.4.2   matt 	uintptr_t bottom;
    248  1.7.4.2   matt 
    249  1.7.4.2   matt 	if (puffs_fakecc)
    250  1.7.4.2   matt 		return &fakecc;
    251  1.7.4.2   matt #ifndef PUFFS_WITH_THREADS
    252  1.7.4.2   matt 	if (puffs_usethreads)
    253  1.7.4.2   matt 		return &fakecc;
    254  1.7.4.2   matt #endif
    255  1.7.4.2   matt 
    256  1.7.4.2   matt 	bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
    257  1.7.4.2   matt 	return (struct puffs_cc *)bottom;
    258      1.1  pooka }
    259