callcontext.c revision 1.4 1 /* $NetBSD: callcontext.c,v 1.4 2007/04/19 14:45:03 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.4 2007/04/19 14:45:03 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_DONE) == 0);
56
57 /* romanes eunt domus */
58 swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
59 }
60
61 void
62 puffs_cc_continue(struct puffs_cc *pcc)
63 {
64
65 /* ramble on */
66 swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
67 }
68
69 struct puffs_usermount *
70 puffs_cc_getusermount(struct puffs_cc *pcc)
71 {
72
73 return pcc->pcc_pu;
74 }
75
76 void *
77 puffs_cc_getspecific(struct puffs_cc *pcc)
78 {
79
80 return puffs_getspecific(pcc->pcc_pu);
81 }
82
83 struct puffs_cc *
84 puffs_cc_create(struct puffs_usermount *pu)
85 {
86 struct puffs_cc *volatile pcc;
87 stack_t *st;
88
89 pcc = malloc(sizeof(struct puffs_cc));
90 if (!pcc)
91 return NULL;
92 memset(pcc, 0, sizeof(struct puffs_cc));
93 pcc->pcc_pu = pu;
94 pcc->pcc_priv = (void *)0xdeadbeef;
95 pcc->pcc_flags = PCC_REALCC;
96
97 /* initialize both ucontext's */
98 if (getcontext(&pcc->pcc_uc) == -1) {
99 free(pcc);
100 return NULL;
101 }
102 if (getcontext(&pcc->pcc_uc_ret) == -1) {
103 free(pcc);
104 return NULL;
105 }
106 /* return here. it won't actually be "here" due to swapcontext() */
107 pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
108
109 /* allocate stack for execution */
110 st = &pcc->pcc_uc.uc_stack;
111 st->ss_sp = pcc->pcc_stack = malloc(pu->pu_cc_stacksize);
112 if (st->ss_sp == NULL) {
113 free(pcc);
114 return NULL;
115 }
116 st->ss_size = pu->pu_cc_stacksize;
117 st->ss_flags = 0;
118
119 /*
120 * Give us an initial context to jump to.
121 *
122 * XXX: Our manual page says that portable code shouldn't rely on
123 * being able to pass pointers through makecontext(). kjk says
124 * that NetBSD code doesn't need to worry about this. uwe says
125 * it would be like putting a "keep away from children" sign on a
126 * box of toys. I didn't ask what simon says; he's probably busy
127 * "fixing" typos in comments.
128 */
129 makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
130 1, (uintptr_t)pcc);
131
132 return pcc;
133 }
134
135 void
136 puffs_cc_destroy(struct puffs_cc *pcc)
137 {
138
139 if (pcc->pcc_preq)
140 free(pcc->pcc_preq);
141 free(pcc->pcc_stack);
142 free(pcc);
143 }
144