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