1 | /* |
1 | /* |
2 | * Copyright (c) 2001 Marc Alexander Lehmann <pcg@goof.com> |
2 | * Copyright (c) 2001-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
… | |
… | |
44 | # define STACK_ADJUST_PTR(sp,ss) (sp) |
44 | # define STACK_ADJUST_PTR(sp,ss) (sp) |
45 | # define STACK_ADJUST_SIZE(sp,ss) (ss) |
45 | # define STACK_ADJUST_SIZE(sp,ss) (ss) |
46 | # endif |
46 | # endif |
47 | #endif |
47 | #endif |
48 | |
48 | |
49 | #if CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
49 | #if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
50 | |
50 | |
|
|
51 | #include <stdlib.h> |
|
|
52 | |
|
|
53 | #if CORO_SJLJ |
|
|
54 | # include <stdio.h> |
51 | #include <signal.h> |
55 | # include <signal.h> |
|
|
56 | # include <unistd.h> |
|
|
57 | #endif |
52 | |
58 | |
53 | static volatile coro_func coro_init_func; |
59 | static volatile coro_func coro_init_func; |
54 | static volatile void *coro_init_arg; |
60 | static volatile void *coro_init_arg; |
55 | static volatile coro_context *new_coro, *create_coro; |
61 | static volatile coro_context *new_coro, *create_coro; |
56 | |
62 | |
… | |
… | |
72 | |
78 | |
73 | static volatile int trampoline_count; |
79 | static volatile int trampoline_count; |
74 | |
80 | |
75 | /* trampoline signal handler */ |
81 | /* trampoline signal handler */ |
76 | static void |
82 | static void |
77 | trampoline(int sig) |
83 | trampoline (int sig) |
78 | { |
84 | { |
79 | if (setjmp (((coro_context *)new_coro)->env)) |
85 | if (setjmp (((coro_context *)new_coro)->env)) |
80 | coro_init (); /* start it */ |
86 | coro_init (); /* start it */ |
81 | else |
87 | else |
82 | trampoline_count++; |
88 | trampoline_count++; |
… | |
… | |
94 | #if CORO_UCONTEXT |
100 | #if CORO_UCONTEXT |
95 | |
101 | |
96 | getcontext (&(ctx->uc)); |
102 | getcontext (&(ctx->uc)); |
97 | |
103 | |
98 | ctx->uc.uc_link = 0; |
104 | ctx->uc.uc_link = 0; |
99 | ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR(sptr,ssize); |
105 | ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize); |
100 | ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize); |
106 | ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize); |
101 | ctx->uc.uc_stack.ss_flags = 0; |
107 | ctx->uc.uc_stack.ss_flags = 0; |
102 | |
108 | |
103 | makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
109 | makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
104 | |
110 | |
105 | #elif CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX |
111 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX |
106 | |
112 | |
107 | # if CORO_SJLJ |
113 | # if CORO_SJLJ |
108 | stack_t ostk, nstk; |
114 | stack_t ostk, nstk; |
109 | struct sigaction osa, nsa; |
115 | struct sigaction osa, nsa; |
110 | sigset_t nsig, osig; |
116 | sigset_t nsig, osig; |
… | |
… | |
130 | |
136 | |
131 | if (sigaction (SIGUSR2, &nsa, &osa)) |
137 | if (sigaction (SIGUSR2, &nsa, &osa)) |
132 | perror ("sigaction"); |
138 | perror ("sigaction"); |
133 | |
139 | |
134 | /* set the new stack */ |
140 | /* set the new stack */ |
135 | nstk.ss_sp = STACK_ADJUST(sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
141 | nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ |
136 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
142 | nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); |
137 | nstk.ss_flags = 0; |
143 | nstk.ss_flags = 0; |
138 | |
144 | |
139 | if (sigaltstack (&nstk, &ostk) < 0) |
145 | if (sigaltstack (&nstk, &ostk) < 0) |
140 | perror ("sigaltstack"); |
146 | perror ("sigaltstack"); |
… | |
… | |
160 | |
166 | |
161 | sigaction (SIGUSR1, &osa, 0); |
167 | sigaction (SIGUSR1, &osa, 0); |
162 | |
168 | |
163 | sigprocmask (SIG_SETMASK, &osig, 0); |
169 | sigprocmask (SIG_SETMASK, &osig, 0); |
164 | |
170 | |
165 | # elif CORO_LOOSE |
171 | # elif CORO_LOSER |
166 | |
172 | |
167 | setjmp (ctx->env); |
173 | setjmp (ctx->env); |
168 | ctx->env[7] = (int)((char *)sptr + ssize); |
174 | ctx->env[7] = (long)((char *)sptr + ssize); |
169 | ctx->env[8] = (int)coro_init; |
175 | ctx->env[8] = (long)coro_init; |
170 | |
176 | |
171 | # elif CORO_LINUX |
177 | # elif CORO_LINUX |
172 | |
178 | |
173 | setjmp (ctx->env); |
179 | setjmp (ctx->env); |
174 | #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
180 | #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
175 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
181 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) |
176 | ctx->env[0].__jmpbuf[JB_PC] = (int)coro_init; |
182 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
177 | ctx->env[0].__jmpbuf[JB_SP] = (int)STACK_ADJUST_PTR(sptr,ssize); |
183 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr,ssize); |
178 | #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
184 | #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ |
179 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
185 | && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) |
180 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
186 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
181 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
187 | ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); |
182 | #elif defined(__GNU_LIBRARY__) && defined(__i386__) |
188 | #elif defined(__GNU_LIBRARY__) && defined(__i386__) |
183 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
189 | ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; |
184 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
190 | ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); |
|
|
191 | #elif defined(__GNU_LIBRARY__) && defined(__amd64__) |
|
|
192 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
|
|
193 | ctx->env[0].__jmpbuf[JB_RSP] = (long)((char *)sptr + ssize); |
185 | #else |
194 | #else |
186 | #error "linux libc or architecture not supported" |
195 | #error "linux libc or architecture not supported" |
187 | #endif |
196 | #endif |
188 | |
197 | |
189 | # elif CORO_IRIX |
198 | # elif CORO_IRIX |
190 | |
199 | |
191 | setjmp (ctx->env); |
200 | setjmp (ctx->env); |
192 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
201 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
193 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR(sptr,ssize); |
202 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr,ssize); |
194 | |
203 | |
195 | # endif |
204 | # endif |
196 | |
205 | |
197 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
206 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
198 | |
207 | |