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