callcontext.c revision 1.14 1 1.14 pooka /* $NetBSD: callcontext.c,v 1.14 2007/11/29 17:47:54 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.14 pooka __RCSID("$NetBSD: callcontext.c,v 1.14 2007/11/29 17:47:54 pooka Exp $");
31 1.1 pooka #endif /* !lint */
32 1.1 pooka
33 1.1 pooka #include <sys/types.h>
34 1.11 pooka #include <sys/mman.h>
35 1.1 pooka
36 1.1 pooka #include <assert.h>
37 1.9 pooka #include <errno.h>
38 1.12 pooka #ifdef PUFFS_WITH_THREADS
39 1.12 pooka #include <pthread.h>
40 1.12 pooka #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.14 pooka #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.10 pooka void
93 1.10 pooka puffs_cc_schedule(struct puffs_cc *pcc)
94 1.10 pooka {
95 1.10 pooka struct puffs_usermount *pu = pcc->pcc_pu;
96 1.10 pooka
97 1.10 pooka assert(pu->pu_state & PU_INLOOP);
98 1.10 pooka TAILQ_INSERT_TAIL(&pu->pu_sched, pcc, entries);
99 1.10 pooka }
100 1.10 pooka
101 1.1 pooka struct puffs_usermount *
102 1.1 pooka puffs_cc_getusermount(struct puffs_cc *pcc)
103 1.1 pooka {
104 1.1 pooka
105 1.1 pooka return pcc->pcc_pu;
106 1.1 pooka }
107 1.1 pooka
108 1.4 pooka void *
109 1.4 pooka puffs_cc_getspecific(struct puffs_cc *pcc)
110 1.4 pooka {
111 1.4 pooka
112 1.4 pooka return puffs_getspecific(pcc->pcc_pu);
113 1.4 pooka }
114 1.4 pooka
115 1.9 pooka int
116 1.9 pooka puffs_cc_getcaller(struct puffs_cc *pcc, pid_t *pid, lwpid_t *lid)
117 1.9 pooka {
118 1.9 pooka
119 1.9 pooka if ((pcc->pcc_flags & PCC_HASCALLER) == 0) {
120 1.9 pooka errno = ESRCH;
121 1.9 pooka return -1;
122 1.9 pooka }
123 1.9 pooka
124 1.9 pooka if (pid)
125 1.9 pooka *pid = pcc->pcc_pid;
126 1.9 pooka if (lid)
127 1.9 pooka *lid = pcc->pcc_lid;
128 1.9 pooka return 0;
129 1.9 pooka }
130 1.9 pooka
131 1.12 pooka #ifdef PUFFS_WITH_THREADS
132 1.12 pooka int pthread__stackid_setup(void *, size_t, pthread_t *);
133 1.12 pooka #endif
134 1.12 pooka
135 1.14 pooka /* for fakecc-users, need only one */
136 1.14 pooka static struct puffs_cc fakecc;
137 1.14 pooka
138 1.12 pooka int
139 1.12 pooka puffs_cc_create(struct puffs_usermount *pu, struct puffs_req *preq,
140 1.12 pooka int type, struct puffs_cc **pccp)
141 1.1 pooka {
142 1.1 pooka struct puffs_cc *volatile pcc;
143 1.11 pooka size_t stacksize = 1<<pu->pu_cc_stackshift;
144 1.14 pooka long psize = sysconf(_SC_PAGESIZE);
145 1.1 pooka stack_t *st;
146 1.14 pooka void *volatile sp;
147 1.1 pooka
148 1.12 pooka #ifdef PUFFS_WITH_THREADS
149 1.12 pooka extern size_t pthread__stacksize;
150 1.12 pooka stacksize = pthread__stacksize;
151 1.12 pooka #endif
152 1.12 pooka
153 1.14 pooka /*
154 1.14 pooka * There are two paths and in the long run we don't have time to
155 1.14 pooka * change the one we're on. For non-real cc's, we just simply use
156 1.14 pooka * a static copy. For the other cases, we mmap the stack and
157 1.14 pooka * manually reserve a bit from the top for the data structure
158 1.14 pooka * (or, well, the bottom).
159 1.14 pooka *
160 1.14 pooka * XXX: threaded mode doesn't work very well now. Not that it's
161 1.14 pooka * supported anyhow.
162 1.14 pooka */
163 1.14 pooka if (type == PCC_FAKECC) {
164 1.14 pooka pcc = &fakecc;
165 1.14 pooka sp = NULL;
166 1.14 pooka } else {
167 1.14 pooka sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE,
168 1.14 pooka MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift),
169 1.14 pooka -1, 0);
170 1.14 pooka if (sp == MAP_FAILED)
171 1.14 pooka return -1;
172 1.14 pooka
173 1.14 pooka pcc = sp;
174 1.14 pooka sp = (uint8_t *)sp + psize;
175 1.14 pooka }
176 1.14 pooka
177 1.1 pooka memset(pcc, 0, sizeof(struct puffs_cc));
178 1.1 pooka pcc->pcc_pu = pu;
179 1.12 pooka pcc->pcc_preq = preq;
180 1.14 pooka pcc->pcc_flags = type;
181 1.12 pooka
182 1.14 pooka /* Not a real cc? Don't need to init more */
183 1.12 pooka if (pcc->pcc_flags != PCC_REALCC)
184 1.12 pooka goto out;
185 1.1 pooka
186 1.1 pooka /* initialize both ucontext's */
187 1.1 pooka if (getcontext(&pcc->pcc_uc) == -1) {
188 1.14 pooka munmap(pcc, stacksize);
189 1.12 pooka return -1;
190 1.1 pooka }
191 1.1 pooka if (getcontext(&pcc->pcc_uc_ret) == -1) {
192 1.14 pooka munmap(pcc, stacksize);
193 1.12 pooka return -1;
194 1.1 pooka }
195 1.1 pooka /* return here. it won't actually be "here" due to swapcontext() */
196 1.1 pooka pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
197 1.1 pooka
198 1.1 pooka /* allocate stack for execution */
199 1.1 pooka st = &pcc->pcc_uc.uc_stack;
200 1.14 pooka
201 1.11 pooka st->ss_sp = pcc->pcc_stack = sp;
202 1.14 pooka st->ss_size = stacksize - psize;
203 1.1 pooka st->ss_flags = 0;
204 1.1 pooka
205 1.12 pooka #ifdef PUFFS_WITH_THREADS
206 1.12 pooka {
207 1.12 pooka pthread_t pt;
208 1.12 pooka extern int __isthreaded;
209 1.12 pooka if (__isthreaded)
210 1.14 pooka pthread__stackid_setup(sp, stacksize /* XXXb0rked */, &pt);
211 1.12 pooka }
212 1.12 pooka #endif
213 1.12 pooka
214 1.1 pooka /*
215 1.1 pooka * Give us an initial context to jump to.
216 1.1 pooka *
217 1.1 pooka * XXX: Our manual page says that portable code shouldn't rely on
218 1.1 pooka * being able to pass pointers through makecontext(). kjk says
219 1.1 pooka * that NetBSD code doesn't need to worry about this. uwe says
220 1.1 pooka * it would be like putting a "keep away from children" sign on a
221 1.1 pooka * box of toys. I didn't ask what simon says; he's probably busy
222 1.1 pooka * "fixing" typos in comments.
223 1.1 pooka */
224 1.1 pooka makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
225 1.1 pooka 1, (uintptr_t)pcc);
226 1.1 pooka
227 1.12 pooka out:
228 1.12 pooka *pccp = pcc;
229 1.12 pooka return 0;
230 1.1 pooka }
231 1.1 pooka
232 1.1 pooka void
233 1.9 pooka puffs_cc_setcaller(struct puffs_cc *pcc, pid_t pid, lwpid_t lid)
234 1.9 pooka {
235 1.9 pooka
236 1.9 pooka pcc->pcc_pid = pid;
237 1.9 pooka pcc->pcc_lid = lid;
238 1.9 pooka pcc->pcc_flags |= PCC_HASCALLER;
239 1.9 pooka }
240 1.9 pooka
241 1.9 pooka void
242 1.1 pooka puffs_cc_destroy(struct puffs_cc *pcc)
243 1.1 pooka {
244 1.11 pooka struct puffs_usermount *pu = pcc->pcc_pu;
245 1.11 pooka size_t stacksize = 1<<pu->pu_cc_stackshift;
246 1.1 pooka
247 1.13 pooka #ifdef PUFFS_WITH_THREADS
248 1.13 pooka extern size_t pthread__stacksize;
249 1.13 pooka stacksize = pthread__stacksize;
250 1.13 pooka #endif
251 1.13 pooka
252 1.14 pooka free(pcc->pcc_preq); /* XXX */
253 1.14 pooka if ((pcc->pcc_flags & PCC_FAKECC) == 0)
254 1.14 pooka munmap(pcc, stacksize);
255 1.14 pooka }
256 1.14 pooka
257 1.14 pooka struct puffs_cc *
258 1.14 pooka puffs_cc_getcc(struct puffs_usermount *pu)
259 1.14 pooka {
260 1.14 pooka extern int puffs_fakecc, puffs_usethreads;
261 1.14 pooka size_t stacksize = 1<<pu->pu_cc_stackshift;
262 1.14 pooka uintptr_t bottom;
263 1.14 pooka
264 1.14 pooka if (puffs_fakecc)
265 1.14 pooka return &fakecc;
266 1.14 pooka #ifndef PUFFS_WITH_THREADS
267 1.14 pooka if (puffs_usethreads)
268 1.14 pooka return &fakecc;
269 1.14 pooka #endif
270 1.14 pooka
271 1.14 pooka bottom = ((uintptr_t)&bottom) & ~(stacksize-1);
272 1.14 pooka return (struct puffs_cc *)bottom;
273 1.1 pooka }
274