| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: callcontext.c,v 1.26 2011/11/02 16:43:04 yamt Exp $ */ | | 1 | /* $NetBSD: callcontext.c,v 1.27 2011/12/06 21:15:39 skrll Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2006, 2007, 2008 Antti Kantee. All Rights Reserved. | | 4 | * Copyright (c) 2006, 2007, 2008 Antti Kantee. All Rights Reserved. |
5 | * | | 5 | * |
6 | * Development of this software was supported by the | | 6 | * Development of this software was supported by the |
7 | * Research Foundation of Helsinki University of Technology | | 7 | * Research Foundation of Helsinki University of Technology |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| @@ -20,27 +20,27 @@ | | | @@ -20,27 +20,27 @@ |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | | 21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 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 | | 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 | | 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 | * SUCH DAMAGE. | | 28 | * SUCH DAMAGE. |
29 | */ | | 29 | */ |
30 | | | 30 | |
31 | #include <sys/cdefs.h> | | 31 | #include <sys/cdefs.h> |
32 | #if !defined(lint) | | 32 | #if !defined(lint) |
33 | __RCSID("$NetBSD: callcontext.c,v 1.26 2011/11/02 16:43:04 yamt Exp $"); | | 33 | __RCSID("$NetBSD: callcontext.c,v 1.27 2011/12/06 21:15:39 skrll Exp $"); |
34 | #endif /* !lint */ | | 34 | #endif /* !lint */ |
35 | | | 35 | |
36 | #include <sys/types.h> | | 36 | #include <sys/types.h> |
37 | #include <sys/mman.h> | | 37 | #include <sys/mman.h> |
38 | | | 38 | |
39 | #include <assert.h> | | 39 | #include <assert.h> |
40 | #include <errno.h> | | 40 | #include <errno.h> |
41 | #include <puffs.h> | | 41 | #include <puffs.h> |
42 | #include <stdio.h> | | 42 | #include <stdio.h> |
43 | #include <stdlib.h> | | 43 | #include <stdlib.h> |
44 | #include <string.h> | | 44 | #include <string.h> |
45 | #include <ucontext.h> | | 45 | #include <ucontext.h> |
46 | #include <unistd.h> | | 46 | #include <unistd.h> |
| @@ -178,40 +178,44 @@ puffs_cc_getcaller(struct puffs_cc *pcc, | | | @@ -178,40 +178,44 @@ puffs_cc_getcaller(struct puffs_cc *pcc, |
178 | if (lid) | | 178 | if (lid) |
179 | *lid = pcc->pcc_lid; | | 179 | *lid = pcc->pcc_lid; |
180 | return 0; | | 180 | return 0; |
181 | } | | 181 | } |
182 | | | 182 | |
183 | static struct puffs_cc fakecc; | | 183 | static struct puffs_cc fakecc; |
184 | | | 184 | |
185 | static struct puffs_cc * | | 185 | static struct puffs_cc * |
186 | slowccalloc(struct puffs_usermount *pu) | | 186 | slowccalloc(struct puffs_usermount *pu) |
187 | { | | 187 | { |
188 | struct puffs_cc *volatile pcc; | | 188 | struct puffs_cc *volatile pcc; |
189 | void *sp; | | 189 | void *sp; |
190 | size_t stacksize = 1<<pu->pu_cc_stackshift; | | 190 | size_t stacksize = 1<<pu->pu_cc_stackshift; |
191 | long psize = sysconf(_SC_PAGESIZE); | | 191 | const long psize = sysconf(_SC_PAGESIZE); |
192 | | | 192 | |
193 | if (puffs_fakecc) | | 193 | if (puffs_fakecc) |
194 | return &fakecc; | | 194 | return &fakecc; |
195 | | | 195 | |
196 | sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE, | | 196 | sp = mmap(NULL, stacksize, PROT_READ|PROT_WRITE, |
197 | MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0); | | 197 | MAP_ANON|MAP_PRIVATE|MAP_ALIGNED(pu->pu_cc_stackshift), -1, 0); |
198 | if (sp == MAP_FAILED) | | 198 | if (sp == MAP_FAILED) |
199 | return NULL; | | 199 | return NULL; |
200 | | | 200 | |
201 | pcc = sp; | | 201 | pcc = sp; |
202 | memset(pcc, 0, sizeof(struct puffs_cc)); | | 202 | memset(pcc, 0, sizeof(struct puffs_cc)); |
203 | | | 203 | |
| | | 204 | #ifndef __MACHINE_STACK_GROWS_UP |
204 | mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE); | | 205 | mprotect((uint8_t *)sp + psize, (size_t)psize, PROT_NONE); |
| | | 206 | #else |
| | | 207 | mprotect((uint8_t *)sp + stacksize - psize, (size_t)psize, PROT_NONE); |
| | | 208 | #endif |
205 | | | 209 | |
206 | /* initialize both ucontext's */ | | 210 | /* initialize both ucontext's */ |
207 | if (getcontext(&pcc->pcc_uc) == -1) { | | 211 | if (getcontext(&pcc->pcc_uc) == -1) { |
208 | munmap(pcc, stacksize); | | 212 | munmap(pcc, stacksize); |
209 | return NULL; | | 213 | return NULL; |
210 | } | | 214 | } |
211 | if (getcontext(&pcc->pcc_uc_ret) == -1) { | | 215 | if (getcontext(&pcc->pcc_uc_ret) == -1) { |
212 | munmap(pcc, stacksize); | | 216 | munmap(pcc, stacksize); |
213 | return NULL; | | 217 | return NULL; |
214 | } | | 218 | } |
215 | | | 219 | |
216 | return pcc; | | 220 | return pcc; |
217 | } | | 221 | } |
| @@ -235,37 +239,39 @@ puffs__cc_create(struct puffs_usermount | | | @@ -235,37 +239,39 @@ puffs__cc_create(struct puffs_usermount |
235 | pcc = LIST_FIRST(&pu->pu_ccmagazin); | | 239 | pcc = LIST_FIRST(&pu->pu_ccmagazin); |
236 | assert(pcc != NULL); | | 240 | assert(pcc != NULL); |
237 | | | 241 | |
238 | LIST_REMOVE(pcc, pcc_rope); | | 242 | LIST_REMOVE(pcc, pcc_rope); |
239 | pu->pu_cc_nstored--; | | 243 | pu->pu_cc_nstored--; |
240 | DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc)); | | 244 | DPRINTF(("puffs__cc_create: magazin pcc %p\n", pcc)); |
241 | } | | 245 | } |
242 | assert(pcc->pcc_pu == pu); | | 246 | assert(pcc->pcc_pu == pu); |
243 | | | 247 | |
244 | if (puffs_fakecc) { | | 248 | if (puffs_fakecc) { |
245 | pcc->pcc_func = func; | | 249 | pcc->pcc_func = func; |
246 | pcc->pcc_farg = pcc; | | 250 | pcc->pcc_farg = pcc; |
247 | } else { | | 251 | } else { |
| | | 252 | const long psize = sysconf(_SC_PAGESIZE); |
| | | 253 | |
248 | /* link context */ | | 254 | /* link context */ |
249 | pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret; | | 255 | pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret; |
250 | | | 256 | |
251 | /* setup stack | | 257 | /* setup stack |
252 | * | | 258 | * |
253 | * XXX: I guess this should theoretically be preserved by | | 259 | * XXX: I guess this should theoretically be preserved by |
254 | * swapcontext(). However, it gets lost. So reinit it. | | 260 | * swapcontext(). However, it gets lost. So reinit it. |
255 | */ | | 261 | */ |
256 | st = &pcc->pcc_uc.uc_stack; | | 262 | st = &pcc->pcc_uc.uc_stack; |
257 | st->ss_sp = pcc; | | 263 | st->ss_sp = ((uint8_t *)(void *)pcc) + psize; |
258 | st->ss_size = stacksize; | | 264 | st->ss_size = stacksize - psize; |
259 | st->ss_flags = 0; | | 265 | st->ss_flags = 0; |
260 | | | 266 | |
261 | /* | | 267 | /* |
262 | * Give us an initial context to jump to. | | 268 | * Give us an initial context to jump to. |
263 | * | | 269 | * |
264 | * Our manual page says that portable code shouldn't | | 270 | * Our manual page says that portable code shouldn't |
265 | * rely on being able to pass pointers through makecontext(). | | 271 | * rely on being able to pass pointers through makecontext(). |
266 | * kjk says that NetBSD code doesn't need to worry about this. | | 272 | * kjk says that NetBSD code doesn't need to worry about this. |
267 | * uwe says it would be like putting a "keep away from | | 273 | * uwe says it would be like putting a "keep away from |
268 | * children" sign on a box of toys. | | 274 | * children" sign on a box of toys. |
269 | */ | | 275 | */ |
270 | makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc); | | 276 | makecontext(&pcc->pcc_uc, (void *)func, 1, (uintptr_t)pcc); |
271 | } | | 277 | } |