callcontext.c revision 1.6 1 /* $NetBSD: callcontext.c,v 1.6 2007/05/15 13:44:46 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.6 2007/05/15 13:44:46 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_REALCC);
56 pcc->pcc_flags &= ~PCC_BORROWED;
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 /*
73 * "Borrows" pcc, *NOT* called from pcc owner. Acts like continue.
74 * So the idea is to use this, give something the context back to
75 * run to completion and then jump back to where ever this was called
76 * from after the op dispatching is complete (or if the pcc decides to
77 * yield again).
78 */
79 void
80 puffs_goto(struct puffs_cc *loanpcc)
81 {
82
83 assert(loanpcc->pcc_flags & PCC_REALCC);
84 loanpcc->pcc_flags |= PCC_BORROWED;
85
86 swapcontext(&loanpcc->pcc_uc_ret, &loanpcc->pcc_uc);
87 }
88
89 struct puffs_usermount *
90 puffs_cc_getusermount(struct puffs_cc *pcc)
91 {
92
93 return pcc->pcc_pu;
94 }
95
96 void *
97 puffs_cc_getspecific(struct puffs_cc *pcc)
98 {
99
100 return puffs_getspecific(pcc->pcc_pu);
101 }
102
103 struct puffs_cc *
104 puffs_cc_create(struct puffs_usermount *pu)
105 {
106 struct puffs_cc *volatile pcc;
107 stack_t *st;
108
109 pcc = malloc(sizeof(struct puffs_cc));
110 if (!pcc)
111 return NULL;
112 memset(pcc, 0, sizeof(struct puffs_cc));
113 pcc->pcc_pu = pu;
114 pcc->pcc_flags = PCC_REALCC;
115
116 /* initialize both ucontext's */
117 if (getcontext(&pcc->pcc_uc) == -1) {
118 free(pcc);
119 return NULL;
120 }
121 if (getcontext(&pcc->pcc_uc_ret) == -1) {
122 free(pcc);
123 return NULL;
124 }
125 /* return here. it won't actually be "here" due to swapcontext() */
126 pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
127
128 /* allocate stack for execution */
129 st = &pcc->pcc_uc.uc_stack;
130 st->ss_sp = pcc->pcc_stack = malloc(pu->pu_cc_stacksize);
131 if (st->ss_sp == NULL) {
132 free(pcc);
133 return NULL;
134 }
135 st->ss_size = pu->pu_cc_stacksize;
136 st->ss_flags = 0;
137
138 /*
139 * Give us an initial context to jump to.
140 *
141 * XXX: Our manual page says that portable code shouldn't rely on
142 * being able to pass pointers through makecontext(). kjk says
143 * that NetBSD code doesn't need to worry about this. uwe says
144 * it would be like putting a "keep away from children" sign on a
145 * box of toys. I didn't ask what simon says; he's probably busy
146 * "fixing" typos in comments.
147 */
148 makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
149 1, (uintptr_t)pcc);
150
151 return pcc;
152 }
153
154 void
155 puffs_cc_destroy(struct puffs_cc *pcc)
156 {
157
158 if (pcc->pcc_preq)
159 free(pcc->pcc_preq);
160 free(pcc->pcc_stack);
161 free(pcc);
162 }
163