callcontext.c revision 1.1 1 1.1 pooka /* $NetBSD: callcontext.c,v 1.1 2006/12/29 15:28:11 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 * 3. The name of the company nor the name of the author may be used to
15 1.1 pooka * endorse or promote products derived from this software without specific
16 1.1 pooka * prior written permission.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <sys/cdefs.h>
32 1.1 pooka #if !defined(lint)
33 1.1 pooka __RCSID("$NetBSD: callcontext.c,v 1.1 2006/12/29 15:28:11 pooka Exp $");
34 1.1 pooka #endif /* !lint */
35 1.1 pooka
36 1.1 pooka #include <sys/types.h>
37 1.1 pooka
38 1.1 pooka #include <assert.h>
39 1.1 pooka #include <puffs.h>
40 1.1 pooka #include <stdio.h>
41 1.1 pooka #include <stdlib.h>
42 1.1 pooka #include <string.h>
43 1.1 pooka
44 1.1 pooka #include "puffs_priv.h"
45 1.1 pooka
46 1.1 pooka /*
47 1.1 pooka * user stuff
48 1.1 pooka */
49 1.1 pooka
50 1.1 pooka void
51 1.1 pooka puffs_cc_yield(struct puffs_cc *pcc)
52 1.1 pooka {
53 1.1 pooka
54 1.1 pooka assert((pcc->pcc_flags & PCC_DONE) == 0);
55 1.1 pooka
56 1.1 pooka /* romanes eunt domus */
57 1.1 pooka swapcontext(&pcc->pcc_uc, &pcc->pcc_uc_ret);
58 1.1 pooka }
59 1.1 pooka
60 1.1 pooka /* this alone doesn't really make any sense, but let's keep cc stuff here */
61 1.1 pooka void
62 1.1 pooka puffs_cc_continue(struct puffs_cc *pcc)
63 1.1 pooka {
64 1.1 pooka
65 1.1 pooka /* ramble on */
66 1.1 pooka swapcontext(&pcc->pcc_uc_ret, &pcc->pcc_uc);
67 1.1 pooka }
68 1.1 pooka
69 1.1 pooka struct puffs_usermount *
70 1.1 pooka puffs_cc_getusermount(struct puffs_cc *pcc)
71 1.1 pooka {
72 1.1 pooka
73 1.1 pooka return pcc->pcc_pu;
74 1.1 pooka }
75 1.1 pooka
76 1.1 pooka #if 0
77 1.1 pooka void *
78 1.1 pooka puffs_cc_getpriv(struct puffs_cc *pcc)
79 1.1 pooka {
80 1.1 pooka
81 1.1 pooka return pcc->pcc_priv;
82 1.1 pooka }
83 1.1 pooka
84 1.1 pooka void
85 1.1 pooka puffs_cc_setpriv(struct puffs_cc *pcc, void *priv, int freepriv)
86 1.1 pooka {
87 1.1 pooka
88 1.1 pooka pcc->pcc_priv = priv;
89 1.1 pooka
90 1.1 pooka if (freepriv)
91 1.1 pooka pcc->pcc_flags |= PCC_FREEPRIV;
92 1.1 pooka }
93 1.1 pooka #endif
94 1.1 pooka
95 1.1 pooka /*
96 1.1 pooka * implementation private helpers
97 1.1 pooka */
98 1.1 pooka
99 1.1 pooka struct puffs_cc *
100 1.1 pooka puffs_cc_create(struct puffs_usermount *pu, struct puffs_req *preq)
101 1.1 pooka {
102 1.1 pooka struct puffs_cc *volatile pcc;
103 1.1 pooka stack_t *st;
104 1.1 pooka
105 1.1 pooka pcc = malloc(sizeof(struct puffs_cc));
106 1.1 pooka if (!pcc)
107 1.1 pooka return NULL;
108 1.1 pooka memset(pcc, 0, sizeof(struct puffs_cc));
109 1.1 pooka pcc->pcc_pu = pu;
110 1.1 pooka pcc->pcc_priv = (void *)0xdeadbeef;
111 1.1 pooka pcc->pcc_flags = PCC_REALCC;
112 1.1 pooka
113 1.1 pooka /* initialize both ucontext's */
114 1.1 pooka if (getcontext(&pcc->pcc_uc) == -1) {
115 1.1 pooka free(pcc);
116 1.1 pooka return NULL;
117 1.1 pooka }
118 1.1 pooka if (getcontext(&pcc->pcc_uc_ret) == -1) {
119 1.1 pooka free(pcc);
120 1.1 pooka return NULL;
121 1.1 pooka }
122 1.1 pooka /* return here. it won't actually be "here" due to swapcontext() */
123 1.1 pooka pcc->pcc_uc.uc_link = &pcc->pcc_uc_ret;
124 1.1 pooka
125 1.1 pooka /* allocate stack for execution */
126 1.1 pooka st = &pcc->pcc_uc.uc_stack;
127 1.1 pooka st->ss_sp = pcc->pcc_stack = malloc(pu->pu_cc_stacksize);
128 1.1 pooka if (st->ss_sp == NULL) {
129 1.1 pooka free(pcc);
130 1.1 pooka return NULL;
131 1.1 pooka }
132 1.1 pooka st->ss_size = pu->pu_cc_stacksize;
133 1.1 pooka st->ss_flags = 0;
134 1.1 pooka
135 1.1 pooka /*
136 1.1 pooka * Give us an initial context to jump to.
137 1.1 pooka *
138 1.1 pooka * XXX: Our manual page says that portable code shouldn't rely on
139 1.1 pooka * being able to pass pointers through makecontext(). kjk says
140 1.1 pooka * that NetBSD code doesn't need to worry about this. uwe says
141 1.1 pooka * it would be like putting a "keep away from children" sign on a
142 1.1 pooka * box of toys. I didn't ask what simon says; he's probably busy
143 1.1 pooka * "fixing" typos in comments.
144 1.1 pooka */
145 1.1 pooka makecontext(&pcc->pcc_uc, (void *)puffs_calldispatcher,
146 1.1 pooka 1, (uintptr_t)pcc);
147 1.1 pooka
148 1.1 pooka /* and finally duplicate the request */
149 1.1 pooka pcc->pcc_preq = malloc(preq->preq_buflen);
150 1.1 pooka if (pcc->pcc_preq == NULL) {
151 1.1 pooka free(st->ss_sp);
152 1.1 pooka free(pcc);
153 1.1 pooka return NULL;
154 1.1 pooka }
155 1.1 pooka (void) memcpy(pcc->pcc_preq, preq, preq->preq_buflen);
156 1.1 pooka
157 1.1 pooka return pcc;
158 1.1 pooka }
159 1.1 pooka
160 1.1 pooka void
161 1.1 pooka puffs_cc_destroy(struct puffs_cc *pcc)
162 1.1 pooka {
163 1.1 pooka
164 1.1 pooka if (pcc->pcc_flags & PCC_FREEPRIV)
165 1.1 pooka free(pcc->pcc_priv);
166 1.1 pooka free(pcc->pcc_preq);
167 1.1 pooka free(pcc->pcc_stack);
168 1.1 pooka free(pcc);
169 1.1 pooka }
170