2008-01-17 00:29:59 +03:00
|
|
|
/* $NetBSD: callcontext.c,v 1.18 2008/01/16 21:29:59 pooka Exp $ */
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Antti Kantee. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if !defined(lint)
|
2008-01-17 00:29:59 +03:00
|
|
|
__RCSID("$NetBSD: callcontext.c,v 1.18 2008/01/16 21:29:59 pooka Exp $");
|
2006-12-29 18:28:11 +03:00
|
|
|
#endif /* !lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-10-26 17:51:14 +04:00
|
|
|
#include <sys/mman.h>
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
#include <assert.h>
|
2007-10-21 18:28:05 +04:00
|
|
|
#include <errno.h>
|
2007-10-26 21:35:01 +04:00
|
|
|
#ifdef PUFFS_WITH_THREADS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2006-12-29 18:28:11 +03:00
|
|
|
#include <puffs.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-02-18 02:00:16 +03:00
|
|
|
#include <ucontext.h>
|
2007-11-29 20:47:54 +03:00
|
|
|
#include <unistd.h>
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
#include "puffs_priv.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* user stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_cc_yield(struct puffs_cc *pcc)
|
|
|
|
{
|
|
|
|
|
2007-05-10 16:36:44 +04:00
|
|
|
assert(pcc->pcc_flags & PCC_REALCC);
|
2007-05-15 17:44:46 +04:00
|
|
|
pcc->pcc_flags &= ~PCC_BORROWED;
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
/* romanes eunt domus */
|
|
|
|
swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_cc_continue(struct puffs_cc *pcc)
|
|
|
|
{
|
|
|
|
|
2007-05-10 16:36:44 +04:00
|
|
|
assert(pcc->pcc_flags & PCC_REALCC);
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
/* ramble on */
|
|
|
|
swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
|
|
|
|
}
|
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
/*
|
|
|
|
* "Borrows" pcc, *NOT* called from pcc owner. Acts like continue.
|
|
|
|
* So the idea is to use this, give something the context back to
|
|
|
|
* run to completion and then jump back to where ever this was called
|
|
|
|
* from after the op dispatching is complete (or if the pcc decides to
|
|
|
|
* yield again).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
puffs_goto(struct puffs_cc *loanpcc)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(loanpcc->pcc_flags & PCC_REALCC);
|
|
|
|
loanpcc->pcc_flags |= PCC_BORROWED;
|
|
|
|
|
|
|
|
swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
|
|
|
|
}
|
|
|
|
|
2007-10-21 23:25:58 +04:00
|
|
|
void
|
|
|
|
puffs_cc_schedule(struct puffs_cc *pcc)
|
|
|
|
{
|
|
|
|
struct puffs_usermount *pu = pcc->pcc_pu;
|
|
|
|
|
|
|
|
assert(pu->pu_state & PU_INLOOP);
|
2008-01-17 00:29:59 +03:00
|
|
|
TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, pcc_schedent);
|
2007-10-21 23:25:58 +04:00
|
|
|
}
|
|
|
|
|
2007-10-21 18:28:05 +04:00
|
|
|
int
|
|
|
|
puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
|
|
|
|
errno = ESRCH;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid)
|
|
|
|
*pid = pcc->pcc_pid;
|
|
|
|
if (lid)
|
|
|
|
*lid = pcc->pcc_lid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-26 21:35:01 +04:00
|
|
|
#ifdef PUFFS_WITH_THREADS
|
|
|
|
int pthread__stackid_setup(void *, size_t, pthread_t *);
|
|
|
|
#endif
|
|
|
|
|
2007-11-29 20:47:54 +03:00
|
|
|
/* for fakecc-users, need only one */
|
|
|
|
static struct puffs_cc fakecc;
|
|
|
|
|
2007-10-26 21:35:01 +04:00
|
|
|
int
|
2007-12-05 00:24:10 +03:00
|
|
|
puffs_cc_create(struct puffs_usermount *pu, struct puffs_framebuf *pb,
|
2007-10-26 21:35:01 +04:00
|
|
|
int type, struct puffs_cc **pccp)
|
2006-12-29 18:28:11 +03:00
|
|
|
{
|
|
|
|
struct puffs_cc *volatile pcc;
|
2007-10-26 17:51:14 +04:00
|
|
|
size_t stacksize = 1<<pu->pu_cc_stackshift;
|
2007-11-29 20:47:54 +03:00
|
|
|
long psize = sysconf(_SC_PAGESIZE);
|
2006-12-29 18:28:11 +03:00
|
|
|
stack_t *st;
|
2007-11-29 20:47:54 +03:00
|
|
|
void *volatile sp;
|
2006-12-29 18:28:11 +03:00
|
|
|
|
2007-10-26 21:35:01 +04:00
|
|
|
#ifdef PUFFS_WITH_THREADS
|
|
|
|
extern size_t pthread__stacksize;
|
|
|
|
stacksize = pthread__stacksize;
|
|
|
|
#endif
|
|
|
|
|
2008-01-17 00:29:59 +03:00
|
|
|
/* Do we have a cached copy? */
|
|
|
|
if (pu->pu_cc_nstored) {
|
|
|
|
pcc = LIST_FIRST(&pu->pu_ccmagazin);
|
|
|
|
assert(pcc != NULL);
|
|
|
|
|
|
|
|
LIST_REMOVE(pcc, pcc_rope);
|
|
|
|
pu->pu_cc_nstored--;
|
|
|
|
goto finalize;
|
|
|
|
}
|
|
|
|
|
2007-11-29 20:47:54 +03:00
|
|
|
/*
|
|
|
|
* There are two paths and in the long run we don't have time to
|
|
|
|
* change the one we're on. For non-real cc's, we just simply use
|
|
|
|
* a static copy. For the other cases, we mmap the stack and
|
|
|
|
* manually reserve a bit from the top for the data structure
|
|
|
|
* (or, well, the bottom).
|
|
|
|
*
|
|
|
|
* XXX: threaded mode doesn't work very well now. Not that it's
|
|
|
|
* supported anyhow.
|
|
|
|
*/
|
|
|
|
if (type == PCC_FAKECC) {
|
|
|
|
pcc = &fakecc;
|
|
|
|
sp = NULL;
|
|
|
|
} else {
|
|
|
|
sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift),
|
|
|
|
-1, 0);
|
|
|
|
if (sp == MAP_FAILED)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pcc = sp;
|
2008-01-16 03:29:42 +03:00
|
|
|
mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE);
|
2007-11-29 20:47:54 +03:00
|
|
|
}
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
memset(pcc, 0, sizeof(struct puffs_cc));
|
|
|
|
pcc->pcc_pu = pu;
|
2007-11-29 20:47:54 +03:00
|
|
|
|
|
|
|
/* Not a real cc? Don't need to init more */
|
2008-01-17 00:29:59 +03:00
|
|
|
if (type != PCC_REALCC)
|
2007-10-26 21:35:01 +04:00
|
|
|
goto out;
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
/* initialize both ucontext's */
|
|
|
|
if (getcontext(&pcc->pcc_uc) == -1) {
|
2007-11-29 20:47:54 +03:00
|
|
|
munmap(pcc, stacksize);
|
2007-10-26 21:35:01 +04:00
|
|
|
return -1;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
if (getcontext(&pcc->pcc_uc_ret) == -1) {
|
2007-11-29 20:47:54 +03:00
|
|
|
munmap(pcc, stacksize);
|
2007-10-26 21:35:01 +04:00
|
|
|
return -1;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
2007-10-26 21:35:01 +04:00
|
|
|
#ifdef PUFFS_WITH_THREADS
|
|
|
|
{
|
|
|
|
pthread_t pt;
|
|
|
|
extern int __isthreaded;
|
|
|
|
if (__isthreaded)
|
2007-11-29 20:47:54 +03:00
|
|
|
pthread__stackid_setup(sp, stacksize /* XXXb0rked */, &pt);
|
2007-10-26 21:35:01 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-17 00:29:59 +03:00
|
|
|
finalize:
|
|
|
|
assert(pcc->pcc_pu == pu);
|
|
|
|
pcc->pcc_pb = pb;
|
|
|
|
pcc->pcc_flags = type;
|
|
|
|
|
|
|
|
/* return here. it won't actually be "here" due to swapcontext() */
|
|
|
|
pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
|
|
|
|
|
|
|
|
/* setup stack
|
|
|
|
*
|
|
|
|
* XXX: I guess this should theoretically be preserved by
|
|
|
|
* swapcontext(). However, it gets lost. So reinit it.
|
|
|
|
*/
|
|
|
|
st = &pcc->pcc_uc.uc_stack;
|
|
|
|
st->ss_sp = pcc;
|
|
|
|
st->ss_size = stacksize;
|
|
|
|
st->ss_flags = 0;
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
/*
|
|
|
|
* Give us an initial context to jump to.
|
|
|
|
*
|
|
|
|
* XXX: Our manual page says that portable code shouldn't rely on
|
|
|
|
* being able to pass pointers through makecontext(). kjk says
|
|
|
|
* that NetBSD code doesn't need to worry about this. uwe says
|
|
|
|
* it would be like putting a "keep away from children" sign on a
|
2008-01-17 00:29:59 +03:00
|
|
|
* box of toys.
|
2006-12-29 18:28:11 +03:00
|
|
|
*/
|
|
|
|
makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
|
|
|
|
1, (uintptr_t)pcc);
|
|
|
|
|
2007-10-26 21:35:01 +04:00
|
|
|
out:
|
|
|
|
*pccp = pcc;
|
|
|
|
return 0;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
2007-10-21 18:28:05 +04:00
|
|
|
void
|
|
|
|
puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
|
|
|
|
{
|
|
|
|
|
|
|
|
pcc->pcc_pid = pid;
|
|
|
|
pcc->pcc_lid = lid;
|
|
|
|
pcc->pcc_flags |= PCC_HASCALLER;
|
|
|
|
}
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
void
|
|
|
|
puffs_cc_destroy(struct puffs_cc *pcc)
|
|
|
|
{
|
2007-10-26 17:51:14 +04:00
|
|
|
struct puffs_usermount *pu = pcc->pcc_pu;
|
|
|
|
size_t stacksize = 1<<pu->pu_cc_stackshift;
|
2006-12-29 18:28:11 +03:00
|
|
|
|
2008-01-17 00:29:59 +03:00
|
|
|
/* not over limit? stuff away in the store */
|
|
|
|
if (pu->pu_cc_nstored < PUFFS_CCMAXSTORE) {
|
|
|
|
pcc->pcc_flags &= ~PCC_DONE;
|
|
|
|
LIST_INSERT_HEAD(&pu->pu_ccmagazin, pcc, pcc_rope);
|
|
|
|
pu->pu_cc_nstored++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else: just dump it */
|
|
|
|
|
2007-10-31 19:09:09 +03:00
|
|
|
#ifdef PUFFS_WITH_THREADS
|
|
|
|
extern size_t pthread__stacksize;
|
|
|
|
stacksize = pthread__stacksize;
|
|
|
|
#endif
|
|
|
|
|
2007-11-29 20:47:54 +03:00
|
|
|
if ((pcc->pcc_flags & PCC_FAKECC) == 0)
|
|
|
|
munmap(pcc, stacksize);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct puffs_cc *
|
|
|
|
puffs_cc_getcc(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
extern int puffs_fakecc, puffs_usethreads;
|
|
|
|
size_t stacksize = 1<<pu->pu_cc_stackshift;
|
|
|
|
uintptr_t bottom;
|
|
|
|
|
|
|
|
if (puffs_fakecc)
|
|
|
|
return &fakecc;
|
|
|
|
#ifndef PUFFS_WITH_THREADS
|
|
|
|
if (puffs_usethreads)
|
|
|
|
return &fakecc;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
|
|
|
|
return (struct puffs_cc *)bottom;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|