4 #include <machine/stackframe.h>
14 void ctx_start(void (*)(void), int, ...);
16 /*===========================================================================*
18 *===========================================================================*/
19 int setuctx(const ucontext_t
*ucp
)
28 if (!(ucp
->uc_flags
& _UC_IGNSIGM
)) {
30 if ((r
= sigprocmask(SIG_SETMASK
, &ucp
->uc_sigmask
, NULL
)) == -1)
34 if (!(ucp
->uc_flags
& _UC_IGNFPU
)) {
35 if ((r
= setmcontext(&(ucp
->uc_mcontext
))) == -1)
43 /*===========================================================================*
45 *===========================================================================*/
46 int getuctx(ucontext_t
*ucp
)
55 if (!(ucp
->uc_flags
& _UC_IGNSIGM
)) {
57 if ((r
= sigprocmask(0, NULL
, &ucp
->uc_sigmask
)) == -1)
61 if (!(ucp
->uc_flags
& _UC_IGNFPU
)) {
62 if ((r
= getmcontext(&(ucp
->uc_mcontext
))) != 0)
70 /*===========================================================================*
72 *===========================================================================*/
73 void makecontext(ucontext_t
*ucp
, void (*func
)(void), int argc
, ...)
76 unsigned int *stack_top
;
78 /* There are a number of situations that are erroneous, but we can't actually
79 tell the caller something is wrong, because this is a void function.
80 Instead, mcontext_t contains a magic field that has to be set
81 properly before it can be used. */
84 } else if ((ucp
->uc_stack
.ss_sp
== NULL
) ||
85 (ucp
->uc_stack
.ss_size
< MINSIGSTKSZ
)) {
86 ucp
->uc_mcontext
.mc_magic
= 0;
87 _UC_MACHINE_SET_STACK(ucp
, 0);
91 if (ucp
->uc_mcontext
.mc_magic
== MCF_MAGIC
) {
93 /* The caller provides a pointer to a stack that we can use to run our
94 context on. When the context starts, control is given to a wrapped
95 start routine, which calls a function and cleans up the stack
96 afterwards. The wrapper needs the address of that function on the
98 The stack will be prepared as follows:
99 func() - start routine
100 arg1 - first argument
103 ucp - context, esp points here when `func' returns
104 _ctx_start pops the address of `func' from the stack and calls it.
105 The stack will then be setup with all arguments for `func'. When
106 `func' returns, _ctx_start cleans up the stack such that ucp is at
107 the top of the stack, ready to be used by resumecontext.
108 Resumecontext, in turn, checks whether another context is ready to
109 be executed (i.e., uc_link != NULL) or exit(2)s the process. */
111 /* Find the top of the stack from which we grow downwards. */
112 stack_top
= (unsigned int *) ((uintptr_t ) ucp
->uc_stack
.ss_sp
+
113 ucp
->uc_stack
.ss_size
);
115 /* Align the arguments to 16 bytes (we might lose a few bytes of stack
117 stack_top
= (unsigned int *) ((uintptr_t) stack_top
& ~0xf);
119 /* Make room for 'func', the `func' routine arguments, and ucp. */
120 stack_top
-= (1 + argc
+ 1);
122 /* Adjust the machine context to point to the top of this stack and the
123 program counter to the context start wrapper. */
124 _UC_MACHINE_SET_EBP(ucp
, 0); /* Clear frame pointer */
125 _UC_MACHINE_SET_STACK(ucp
, (reg_t
) stack_top
);
126 _UC_MACHINE_SET_PC(ucp
, (reg_t
) ctx_start
);
128 *stack_top
++ = (uintptr_t) func
;
130 /* Copy arguments to the stack. */
133 *stack_top
++ = va_arg(ap
, uintptr_t);
137 /* Store ucp on the stack */
138 *stack_top
= (uintptr_t) ucp
;
140 /* Set ESI to point to the base of the stack where ucp is stored, so
141 that the wrapper function knows how to clean up the stack after
142 calling `func' (i.e., how to adjust ESP). */
143 _UC_MACHINE_SET_ESI(ucp
, (reg_t
) stack_top
);
146 /* If we ran out of stack space, invalidate stack pointer. Eventually,
147 swapcontext will choke on this and return ENOMEM. */
148 if (stack_top
== ucp
->uc_stack
.ss_sp
) {
149 _UC_MACHINE_SET_STACK(ucp
, 0);
151 #elif defined(__arm__)
152 /* The caller provides a pointer to a stack that we can use to run our
153 context on. When the context starts, control is given to the
154 requested function. When the function finishes, it returns to the
155 _ctx_start wrapper that calls resumecontext (after setting up
156 resumecontext's parameter).
158 The first four arguments for the function will be passed in
159 regs r0-r3 as specified by the ABI, and the rest will go on
160 the stack. The ucp is saved in r4 so that we can
161 eventually pass it to resumecontext. The r4 register is
162 callee-preserved, so the ucp will remain valid in r4 when
163 _ctx_start runs. _ctx_start will move the ucp from r4 into
164 r0, so that the ucp is the first paramater for resumecontext.
165 Then, _ctx_start will call resumecontext. Resumecontext, in turn,
166 checks whether another context is ready to be executed
167 (i.e., uc_link != NULL) or exit(2)s the process. */
169 /* Find the top of the stack from which we grow downwards. */
170 stack_top
= (unsigned int *) ((uintptr_t ) ucp
->uc_stack
.ss_sp
+
171 ucp
->uc_stack
.ss_size
);
173 /* Align the arguments to 16 bytes (we might lose a few bytes of stack
175 stack_top
= (unsigned int *) ((uintptr_t) stack_top
& ~0xf);
177 /* Make room for `func' routine arguments that don't fit in r0-r3 */
179 stack_top
-= argc
- 4;
181 /* Adjust the machine context to point to the top of this stack and the
182 program counter to the 'func' entry point. Set lr to ctx_start, so
183 ctx_start runs after 'func'. Save ucp in r4 */
184 _UC_MACHINE_SET_FP(ucp
, 0); /* Clear frame pointer */
185 _UC_MACHINE_SET_STACK(ucp
, (reg_t
) stack_top
);
186 _UC_MACHINE_SET_PC(ucp
, (reg_t
) func
);
187 _UC_MACHINE_SET_LR(ucp
, (reg_t
) ctx_start
);
188 _UC_MACHINE_SET_R4(ucp
, (reg_t
) ucp
);
190 /* Copy arguments to r0-r3 and stack. */
192 /* Pass up to four arguments in registers. */
194 _UC_MACHINE_SET_R0(ucp
, va_arg(ap
, uintptr_t));
196 _UC_MACHINE_SET_R1(ucp
, va_arg(ap
, uintptr_t));
198 _UC_MACHINE_SET_R2(ucp
, va_arg(ap
, uintptr_t));
200 _UC_MACHINE_SET_R3(ucp
, va_arg(ap
, uintptr_t));
201 /* Pass the rest on the stack. */
203 *stack_top
++ = va_arg(ap
, uintptr_t);
207 /* If we ran out of stack space, invalidate stack pointer. Eventually,
208 swapcontext will choke on this and return ENOMEM. */
209 if (stack_top
== ucp
->uc_stack
.ss_sp
) {
210 _UC_MACHINE_SET_STACK(ucp
, 0);
213 # error "Unsupported platform"
219 /*===========================================================================*
221 *===========================================================================*/
222 int swapcontext(ucontext_t
*oucp
, const ucontext_t
*ucp
)
226 if ((oucp
== NULL
) || (ucp
== NULL
)) {
231 if (_UC_MACHINE_STACK(ucp
) == 0) {
232 /* No stack space. Bail out. */
237 oucp
->uc_flags
&= ~_UC_SWAPPED
;
238 r
= getcontext(oucp
);
239 if ((r
== 0) && !(oucp
->uc_flags
& _UC_SWAPPED
)) {
240 oucp
->uc_flags
|= _UC_SWAPPED
;
248 /*===========================================================================*
250 *===========================================================================*/
252 void resumecontext(ucontext_t
*ucp
)
254 if (ucp
->uc_link
== NULL
) exit(0);
256 /* Error handling? Where should the error go to? */
257 (void) setcontext((const ucontext_t
*) ucp
->uc_link
);
259 exit(1); /* Never reached */