1 /*
2 ** GNU Pth - The GNU Portable Threads
3 ** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4 **
5 ** This file is part of GNU Pth, a non-preemptive thread scheduling
6 ** library which can be found at http://www.gnu.org/software/pth/.
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 ** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 **
23 ** pth_uctx.c: Pth user-space context handling (stand-alone sub-API)
24 */
25 /* ``It worries me however, to realize
26 how tough an ass-hole I have had to
27 be, in order to get to stick to the
28 principle of doing things right,
29 rather than "just hack it in".''
30 -- Poul-Henning Kamp <phk@FreeBSD.org> */
31 #include "pth_p.h"
32
33 /* user-space context structure */
34 struct pth_uctx_st {
35 int uc_stack_own; /* whether stack were allocated by us */
36 char *uc_stack_ptr; /* pointer to start address of stack area */
37 size_t uc_stack_len; /* size of stack area */
38 int uc_mctx_set; /* whether uc_mctx is set */
39 pth_mctx_t uc_mctx; /* saved underlying machine context */
40 };
41
42 /* create user-space context structure */
43 int
pth_uctx_create(pth_uctx_t * puctx)44 pth_uctx_create(
45 pth_uctx_t *puctx)
46 {
47 pth_uctx_t uctx;
48
49 /* argument sanity checking */
50 if (puctx == NULL)
51 return pth_error(FALSE, EINVAL);
52
53 /* allocate the context structure */
54 if ((uctx = (pth_uctx_t)malloc(sizeof(struct pth_uctx_st))) == NULL)
55 return pth_error(FALSE, errno);
56
57 /* initialize the context structure */
58 uctx->uc_stack_own = FALSE;
59 uctx->uc_stack_ptr = NULL;
60 uctx->uc_stack_len = 0;
61 uctx->uc_mctx_set = FALSE;
62 memset((void *)&uctx->uc_mctx, 0, sizeof(pth_mctx_t));
63
64 /* pass result to caller */
65 *puctx = uctx;
66
67 return TRUE;
68 }
69
70 /* trampoline context */
71 typedef struct {
72 pth_mctx_t *mctx_parent;
73 pth_uctx_t uctx_this;
74 pth_uctx_t uctx_after;
75 void (*start_func)(void *);
76 void *start_arg;
77 } pth_uctx_trampoline_t;
78 pth_uctx_trampoline_t pth_uctx_trampoline_ctx;
79
80 /* trampoline function for pth_uctx_make() */
pth_uctx_trampoline(void)81 static void pth_uctx_trampoline(void)
82 {
83 volatile pth_uctx_trampoline_t ctx;
84
85 /* move context information from global to local storage */
86 ctx.mctx_parent = pth_uctx_trampoline_ctx.mctx_parent;
87 ctx.uctx_this = pth_uctx_trampoline_ctx.uctx_this;
88 ctx.uctx_after = pth_uctx_trampoline_ctx.uctx_after;
89 ctx.start_func = pth_uctx_trampoline_ctx.start_func;
90 ctx.start_arg = pth_uctx_trampoline_ctx.start_arg;
91
92 /* switch back to parent */
93 pth_mctx_switch(&(ctx.uctx_this->uc_mctx), ctx.mctx_parent);
94
95 /* enter start function */
96 (*ctx.start_func)(ctx.start_arg);
97
98 /* switch to successor user-space context */
99 if (ctx.uctx_after != NULL)
100 pth_mctx_restore(&(ctx.uctx_after->uc_mctx));
101
102 /* terminate process (the only reasonable thing to do here) */
103 exit(0);
104
105 /* NOTREACHED */
106 return;
107 }
108
109 /* make setup of user-space context structure */
110 int
pth_uctx_make(pth_uctx_t uctx,char * sk_addr,size_t sk_size,const sigset_t * sigmask,void (* start_func)(void *),void * start_arg,pth_uctx_t uctx_after)111 pth_uctx_make(
112 pth_uctx_t uctx,
113 char *sk_addr, size_t sk_size,
114 const sigset_t *sigmask,
115 void (*start_func)(void *), void *start_arg,
116 pth_uctx_t uctx_after)
117 {
118 pth_mctx_t mctx_parent;
119 sigset_t ss;
120
121 /* argument sanity checking */
122 if (uctx == NULL || start_func == NULL || sk_size < 16*1024)
123 return pth_error(FALSE, EINVAL);
124
125 /* configure run-time stack */
126 if (sk_addr == NULL) {
127 if ((sk_addr = (char *)malloc(sk_size)) == NULL)
128 return pth_error(FALSE, errno);
129 uctx->uc_stack_own = TRUE;
130 }
131 else
132 uctx->uc_stack_own = FALSE;
133 uctx->uc_stack_ptr = sk_addr;
134 uctx->uc_stack_len = sk_size;
135
136 /* configure the underlying machine context */
137 if (!pth_mctx_set(&uctx->uc_mctx, pth_uctx_trampoline,
138 uctx->uc_stack_ptr, uctx->uc_stack_ptr+uctx->uc_stack_len))
139 return pth_error(FALSE, errno);
140
141 /* move context information into global storage for the trampoline jump */
142 pth_uctx_trampoline_ctx.mctx_parent = &mctx_parent;
143 pth_uctx_trampoline_ctx.uctx_this = uctx;
144 pth_uctx_trampoline_ctx.uctx_after = uctx_after;
145 pth_uctx_trampoline_ctx.start_func = start_func;
146 pth_uctx_trampoline_ctx.start_arg = start_arg;
147
148 /* optionally establish temporary signal mask */
149 if (sigmask != NULL)
150 sigprocmask(SIG_SETMASK, sigmask, &ss);
151
152 /* perform the trampoline step */
153 pth_mctx_switch(&mctx_parent, &(uctx->uc_mctx));
154
155 /* optionally restore original signal mask */
156 if (sigmask != NULL)
157 sigprocmask(SIG_SETMASK, &ss, NULL);
158
159 /* finally flag that the context is now configured */
160 uctx->uc_mctx_set = TRUE;
161
162 return TRUE;
163 }
164
165 /* switch from current to other user-space context */
166 int
pth_uctx_switch(pth_uctx_t uctx_from,pth_uctx_t uctx_to)167 pth_uctx_switch(
168 pth_uctx_t uctx_from,
169 pth_uctx_t uctx_to)
170 {
171 /* argument sanity checking */
172 if (uctx_from == NULL || uctx_to == NULL)
173 return pth_error(FALSE, EINVAL);
174 if (!(uctx_to->uc_mctx_set))
175 return pth_error(FALSE, EPERM);
176
177 /* switch underlying machine context */
178 uctx_from->uc_mctx_set = TRUE;
179 pth_mctx_switch(&(uctx_from->uc_mctx), &(uctx_to->uc_mctx));
180
181 return TRUE;
182 }
183
184 /* destroy user-space context structure */
185 int
pth_uctx_destroy(pth_uctx_t uctx)186 pth_uctx_destroy(
187 pth_uctx_t uctx)
188 {
189 /* argument sanity checking */
190 if (uctx == NULL)
191 return pth_error(FALSE, EINVAL);
192
193 /* deallocate dynamically allocated stack */
194 if (uctx->uc_stack_own && uctx->uc_stack_ptr != NULL)
195 free(uctx->uc_stack_ptr);
196
197 /* deallocate context structure */
198 free(uctx);
199
200 return TRUE;
201 }
202
203