root / linux-user / signal.c @ 78c34e98
History | View | Annotate | Download (24.9 kB)
1 | 31e31b8a | bellard | /*
|
---|---|---|---|
2 | 66fb9763 | bellard | * Emulation of Linux signals
|
3 | 31e31b8a | bellard | *
|
4 | 31e31b8a | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 31e31b8a | bellard | *
|
6 | 31e31b8a | bellard | * This program is free software; you can redistribute it and/or modify
|
7 | 31e31b8a | bellard | * it under the terms of the GNU General Public License as published by
|
8 | 31e31b8a | bellard | * the Free Software Foundation; either version 2 of the License, or
|
9 | 31e31b8a | bellard | * (at your option) any later version.
|
10 | 31e31b8a | bellard | *
|
11 | 31e31b8a | bellard | * This program is distributed in the hope that it will be useful,
|
12 | 31e31b8a | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 31e31b8a | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 31e31b8a | bellard | * GNU General Public License for more details.
|
15 | 31e31b8a | bellard | *
|
16 | 31e31b8a | bellard | * You should have received a copy of the GNU General Public License
|
17 | 31e31b8a | bellard | * along with this program; if not, write to the Free Software
|
18 | 31e31b8a | bellard | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 | 31e31b8a | bellard | */
|
20 | 31e31b8a | bellard | #include <stdlib.h> |
21 | 31e31b8a | bellard | #include <stdio.h> |
22 | 66fb9763 | bellard | #include <string.h> |
23 | 31e31b8a | bellard | #include <stdarg.h> |
24 | 2677e107 | bellard | #include <unistd.h> |
25 | 31e31b8a | bellard | #include <signal.h> |
26 | 66fb9763 | bellard | #include <errno.h> |
27 | 31e31b8a | bellard | #include <sys/ucontext.h> |
28 | 31e31b8a | bellard | |
29 | 0d330196 | bellard | #ifdef __ia64__
|
30 | 0d330196 | bellard | #undef uc_mcontext
|
31 | 0d330196 | bellard | #undef uc_sigmask
|
32 | 0d330196 | bellard | #undef uc_stack
|
33 | 0d330196 | bellard | #undef uc_link
|
34 | 0d330196 | bellard | #endif
|
35 | 0d330196 | bellard | |
36 | 3ef693a0 | bellard | #include "qemu.h" |
37 | 66fb9763 | bellard | |
38 | 66fb9763 | bellard | //#define DEBUG_SIGNAL
|
39 | 66fb9763 | bellard | |
40 | 66fb9763 | bellard | #define MAX_SIGQUEUE_SIZE 1024 |
41 | 66fb9763 | bellard | |
42 | 66fb9763 | bellard | struct sigqueue {
|
43 | 66fb9763 | bellard | struct sigqueue *next;
|
44 | 9de5e440 | bellard | target_siginfo_t info; |
45 | 66fb9763 | bellard | }; |
46 | 31e31b8a | bellard | |
47 | 31e31b8a | bellard | struct emulated_sigaction {
|
48 | 31e31b8a | bellard | struct target_sigaction sa;
|
49 | 66fb9763 | bellard | int pending; /* true if signal is pending */ |
50 | 66fb9763 | bellard | struct sigqueue *first;
|
51 | 66fb9763 | bellard | struct sigqueue info; /* in order to always have memory for the |
52 | 66fb9763 | bellard | first signal, we put it here */
|
53 | 31e31b8a | bellard | }; |
54 | 31e31b8a | bellard | |
55 | 66fb9763 | bellard | static struct emulated_sigaction sigact_table[TARGET_NSIG]; |
56 | 66fb9763 | bellard | static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */ |
57 | 66fb9763 | bellard | static struct sigqueue *first_free; /* first free siginfo queue entry */ |
58 | 66fb9763 | bellard | static int signal_pending; /* non zero if a signal may be pending */ |
59 | 31e31b8a | bellard | |
60 | 66fb9763 | bellard | static void host_signal_handler(int host_signum, siginfo_t *info, |
61 | 66fb9763 | bellard | void *puc);
|
62 | 66fb9763 | bellard | |
63 | 66fb9763 | bellard | /* XXX: do it properly */
|
64 | 31e31b8a | bellard | static inline int host_to_target_signal(int sig) |
65 | 31e31b8a | bellard | { |
66 | 31e31b8a | bellard | return sig;
|
67 | 31e31b8a | bellard | } |
68 | 31e31b8a | bellard | |
69 | 31e31b8a | bellard | static inline int target_to_host_signal(int sig) |
70 | 31e31b8a | bellard | { |
71 | 31e31b8a | bellard | return sig;
|
72 | 31e31b8a | bellard | } |
73 | 31e31b8a | bellard | |
74 | 66fb9763 | bellard | void host_to_target_sigset(target_sigset_t *d, sigset_t *s)
|
75 | 66fb9763 | bellard | { |
76 | 66fb9763 | bellard | int i;
|
77 | 66fb9763 | bellard | for(i = 0;i < TARGET_NSIG_WORDS; i++) { |
78 | 66fb9763 | bellard | d->sig[i] = tswapl(((unsigned long *)s)[i]); |
79 | 66fb9763 | bellard | } |
80 | 66fb9763 | bellard | } |
81 | 66fb9763 | bellard | |
82 | 66fb9763 | bellard | void target_to_host_sigset(sigset_t *d, target_sigset_t *s)
|
83 | 66fb9763 | bellard | { |
84 | 66fb9763 | bellard | int i;
|
85 | 66fb9763 | bellard | for(i = 0;i < TARGET_NSIG_WORDS; i++) { |
86 | 66fb9763 | bellard | ((unsigned long *)d)[i] = tswapl(s->sig[i]); |
87 | 66fb9763 | bellard | } |
88 | 66fb9763 | bellard | } |
89 | 66fb9763 | bellard | |
90 | 66fb9763 | bellard | void host_to_target_old_sigset(target_ulong *old_sigset,
|
91 | 66fb9763 | bellard | const sigset_t *sigset)
|
92 | 66fb9763 | bellard | { |
93 | 66fb9763 | bellard | *old_sigset = tswap32(*(unsigned long *)sigset & 0xffffffff); |
94 | 66fb9763 | bellard | } |
95 | 66fb9763 | bellard | |
96 | 66fb9763 | bellard | void target_to_host_old_sigset(sigset_t *sigset,
|
97 | 66fb9763 | bellard | const target_ulong *old_sigset)
|
98 | 66fb9763 | bellard | { |
99 | 66fb9763 | bellard | sigemptyset(sigset); |
100 | 66fb9763 | bellard | *(unsigned long *)sigset = tswapl(*old_sigset); |
101 | 66fb9763 | bellard | } |
102 | 66fb9763 | bellard | |
103 | 9de5e440 | bellard | /* siginfo conversion */
|
104 | 9de5e440 | bellard | |
105 | 9de5e440 | bellard | static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, |
106 | 9de5e440 | bellard | const siginfo_t *info)
|
107 | 66fb9763 | bellard | { |
108 | 9de5e440 | bellard | int sig;
|
109 | 9de5e440 | bellard | sig = host_to_target_signal(info->si_signo); |
110 | 9de5e440 | bellard | tinfo->si_signo = sig; |
111 | 9de5e440 | bellard | tinfo->si_errno = 0;
|
112 | 9de5e440 | bellard | tinfo->si_code = 0;
|
113 | 447db213 | bellard | if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
|
114 | 447db213 | bellard | sig == SIGBUS || sig == SIGTRAP) { |
115 | 9de5e440 | bellard | /* should never come here, but who knows. The information for
|
116 | 9de5e440 | bellard | the target is irrelevant */
|
117 | 9de5e440 | bellard | tinfo->_sifields._sigfault._addr = 0;
|
118 | 9de5e440 | bellard | } else if (sig >= TARGET_SIGRTMIN) { |
119 | 9de5e440 | bellard | tinfo->_sifields._rt._pid = info->si_pid; |
120 | 9de5e440 | bellard | tinfo->_sifields._rt._uid = info->si_uid; |
121 | 9de5e440 | bellard | /* XXX: potential problem if 64 bit */
|
122 | 9de5e440 | bellard | tinfo->_sifields._rt._sigval.sival_ptr = |
123 | 9de5e440 | bellard | (target_ulong)info->si_value.sival_ptr; |
124 | 9de5e440 | bellard | } |
125 | 9de5e440 | bellard | } |
126 | 9de5e440 | bellard | |
127 | 9de5e440 | bellard | static void tswap_siginfo(target_siginfo_t *tinfo, |
128 | 9de5e440 | bellard | const target_siginfo_t *info)
|
129 | 9de5e440 | bellard | { |
130 | 9de5e440 | bellard | int sig;
|
131 | 9de5e440 | bellard | sig = info->si_signo; |
132 | 9de5e440 | bellard | tinfo->si_signo = tswap32(sig); |
133 | 66fb9763 | bellard | tinfo->si_errno = tswap32(info->si_errno); |
134 | 66fb9763 | bellard | tinfo->si_code = tswap32(info->si_code); |
135 | 447db213 | bellard | if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
|
136 | 447db213 | bellard | sig == SIGBUS || sig == SIGTRAP) { |
137 | 9de5e440 | bellard | tinfo->_sifields._sigfault._addr = |
138 | 9de5e440 | bellard | tswapl(info->_sifields._sigfault._addr); |
139 | 9de5e440 | bellard | } else if (sig >= TARGET_SIGRTMIN) { |
140 | 9de5e440 | bellard | tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid); |
141 | 9de5e440 | bellard | tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid); |
142 | 9de5e440 | bellard | tinfo->_sifields._rt._sigval.sival_ptr = |
143 | 9de5e440 | bellard | tswapl(info->_sifields._rt._sigval.sival_ptr); |
144 | 9de5e440 | bellard | } |
145 | 9de5e440 | bellard | } |
146 | 9de5e440 | bellard | |
147 | 9de5e440 | bellard | |
148 | 9de5e440 | bellard | void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) |
149 | 9de5e440 | bellard | { |
150 | 9de5e440 | bellard | host_to_target_siginfo_noswap(tinfo, info); |
151 | 9de5e440 | bellard | tswap_siginfo(tinfo, tinfo); |
152 | 66fb9763 | bellard | } |
153 | 66fb9763 | bellard | |
154 | 9de5e440 | bellard | /* XXX: we support only POSIX RT signals are used. */
|
155 | 9de5e440 | bellard | /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
|
156 | 9de5e440 | bellard | void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo) |
157 | 66fb9763 | bellard | { |
158 | 66fb9763 | bellard | info->si_signo = tswap32(tinfo->si_signo); |
159 | 66fb9763 | bellard | info->si_errno = tswap32(tinfo->si_errno); |
160 | 66fb9763 | bellard | info->si_code = tswap32(tinfo->si_code); |
161 | 9de5e440 | bellard | info->si_pid = tswap32(tinfo->_sifields._rt._pid); |
162 | 9de5e440 | bellard | info->si_uid = tswap32(tinfo->_sifields._rt._uid); |
163 | 9de5e440 | bellard | info->si_value.sival_ptr = |
164 | 9de5e440 | bellard | (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
|
165 | 66fb9763 | bellard | } |
166 | 66fb9763 | bellard | |
167 | 31e31b8a | bellard | void signal_init(void) |
168 | 31e31b8a | bellard | { |
169 | 31e31b8a | bellard | struct sigaction act;
|
170 | 31e31b8a | bellard | int i;
|
171 | 31e31b8a | bellard | |
172 | 9de5e440 | bellard | /* set all host signal handlers. ALL signals are blocked during
|
173 | 9de5e440 | bellard | the handlers to serialize them. */
|
174 | 9de5e440 | bellard | sigfillset(&act.sa_mask); |
175 | 31e31b8a | bellard | act.sa_flags = SA_SIGINFO; |
176 | 31e31b8a | bellard | act.sa_sigaction = host_signal_handler; |
177 | 31e31b8a | bellard | for(i = 1; i < NSIG; i++) { |
178 | 66fb9763 | bellard | sigaction(i, &act, NULL);
|
179 | 31e31b8a | bellard | } |
180 | 31e31b8a | bellard | |
181 | 31e31b8a | bellard | memset(sigact_table, 0, sizeof(sigact_table)); |
182 | 66fb9763 | bellard | |
183 | 66fb9763 | bellard | first_free = &sigqueue_table[0];
|
184 | 66fb9763 | bellard | for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) |
185 | 66fb9763 | bellard | sigqueue_table[i].next = &sigqueue_table[i + 1];
|
186 | 66fb9763 | bellard | sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL; |
187 | 66fb9763 | bellard | } |
188 | 66fb9763 | bellard | |
189 | 66fb9763 | bellard | /* signal queue handling */
|
190 | 66fb9763 | bellard | |
191 | 66fb9763 | bellard | static inline struct sigqueue *alloc_sigqueue(void) |
192 | 66fb9763 | bellard | { |
193 | 66fb9763 | bellard | struct sigqueue *q = first_free;
|
194 | 66fb9763 | bellard | if (!q)
|
195 | 66fb9763 | bellard | return NULL; |
196 | 66fb9763 | bellard | first_free = q->next; |
197 | 66fb9763 | bellard | return q;
|
198 | 31e31b8a | bellard | } |
199 | 31e31b8a | bellard | |
200 | 66fb9763 | bellard | static inline void free_sigqueue(struct sigqueue *q) |
201 | 66fb9763 | bellard | { |
202 | 66fb9763 | bellard | q->next = first_free; |
203 | 66fb9763 | bellard | first_free = q; |
204 | 66fb9763 | bellard | } |
205 | 66fb9763 | bellard | |
206 | 9de5e440 | bellard | /* abort execution with signal */
|
207 | 9de5e440 | bellard | void __attribute((noreturn)) force_sig(int sig) |
208 | 66fb9763 | bellard | { |
209 | 66fb9763 | bellard | int host_sig;
|
210 | 66fb9763 | bellard | host_sig = target_to_host_signal(sig); |
211 | bc8a22cc | bellard | fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
|
212 | 66fb9763 | bellard | sig, strsignal(host_sig)); |
213 | 9de5e440 | bellard | #if 1 |
214 | 66fb9763 | bellard | _exit(-host_sig); |
215 | 9de5e440 | bellard | #else
|
216 | 9de5e440 | bellard | { |
217 | 9de5e440 | bellard | struct sigaction act;
|
218 | 9de5e440 | bellard | sigemptyset(&act.sa_mask); |
219 | 9de5e440 | bellard | act.sa_flags = SA_SIGINFO; |
220 | 9de5e440 | bellard | act.sa_sigaction = SIG_DFL; |
221 | 9de5e440 | bellard | sigaction(SIGABRT, &act, NULL);
|
222 | 9de5e440 | bellard | abort(); |
223 | 9de5e440 | bellard | } |
224 | 9de5e440 | bellard | #endif
|
225 | 66fb9763 | bellard | } |
226 | 66fb9763 | bellard | |
227 | 9de5e440 | bellard | /* queue a signal so that it will be send to the virtual CPU as soon
|
228 | 9de5e440 | bellard | as possible */
|
229 | 9de5e440 | bellard | int queue_signal(int sig, target_siginfo_t *info) |
230 | 31e31b8a | bellard | { |
231 | 66fb9763 | bellard | struct emulated_sigaction *k;
|
232 | 9de5e440 | bellard | struct sigqueue *q, **pq;
|
233 | 66fb9763 | bellard | target_ulong handler; |
234 | 66fb9763 | bellard | |
235 | 9de5e440 | bellard | #if defined(DEBUG_SIGNAL)
|
236 | bc8a22cc | bellard | fprintf(stderr, "queue_signal: sig=%d\n",
|
237 | 9de5e440 | bellard | sig); |
238 | 66fb9763 | bellard | #endif
|
239 | 9de5e440 | bellard | k = &sigact_table[sig - 1];
|
240 | 66fb9763 | bellard | handler = k->sa._sa_handler; |
241 | 66fb9763 | bellard | if (handler == TARGET_SIG_DFL) {
|
242 | 66fb9763 | bellard | /* default handler : ignore some signal. The other are fatal */
|
243 | 66fb9763 | bellard | if (sig != TARGET_SIGCHLD &&
|
244 | 66fb9763 | bellard | sig != TARGET_SIGURG && |
245 | 66fb9763 | bellard | sig != TARGET_SIGWINCH) { |
246 | 66fb9763 | bellard | force_sig(sig); |
247 | 9de5e440 | bellard | } else {
|
248 | 9de5e440 | bellard | return 0; /* indicate ignored */ |
249 | 66fb9763 | bellard | } |
250 | 66fb9763 | bellard | } else if (handler == TARGET_SIG_IGN) { |
251 | 66fb9763 | bellard | /* ignore signal */
|
252 | 9de5e440 | bellard | return 0; |
253 | 66fb9763 | bellard | } else if (handler == TARGET_SIG_ERR) { |
254 | 66fb9763 | bellard | force_sig(sig); |
255 | 66fb9763 | bellard | } else {
|
256 | 9de5e440 | bellard | pq = &k->first; |
257 | 9de5e440 | bellard | if (sig < TARGET_SIGRTMIN) {
|
258 | 9de5e440 | bellard | /* if non real time signal, we queue exactly one signal */
|
259 | 9de5e440 | bellard | if (!k->pending)
|
260 | 9de5e440 | bellard | q = &k->info; |
261 | 9de5e440 | bellard | else
|
262 | 9de5e440 | bellard | return 0; |
263 | 9de5e440 | bellard | } else {
|
264 | 9de5e440 | bellard | if (!k->pending) {
|
265 | 9de5e440 | bellard | /* first signal */
|
266 | 9de5e440 | bellard | q = &k->info; |
267 | 9de5e440 | bellard | } else {
|
268 | 9de5e440 | bellard | q = alloc_sigqueue(); |
269 | 9de5e440 | bellard | if (!q)
|
270 | 9de5e440 | bellard | return -EAGAIN;
|
271 | 9de5e440 | bellard | while (*pq != NULL) |
272 | 9de5e440 | bellard | pq = &(*pq)->next; |
273 | 9de5e440 | bellard | } |
274 | 9de5e440 | bellard | } |
275 | 9de5e440 | bellard | *pq = q; |
276 | 9de5e440 | bellard | q->info = *info; |
277 | 9de5e440 | bellard | q->next = NULL;
|
278 | 9de5e440 | bellard | k->pending = 1;
|
279 | 9de5e440 | bellard | /* signal that a new signal is pending */
|
280 | 9de5e440 | bellard | signal_pending = 1;
|
281 | 9de5e440 | bellard | return 1; /* indicates that the signal was queued */ |
282 | 9de5e440 | bellard | } |
283 | 9de5e440 | bellard | } |
284 | 9de5e440 | bellard | |
285 | 9de5e440 | bellard | #if defined(DEBUG_SIGNAL)
|
286 | 9de5e440 | bellard | #ifdef __i386__
|
287 | 9de5e440 | bellard | static void dump_regs(struct ucontext *uc) |
288 | 9de5e440 | bellard | { |
289 | 9de5e440 | bellard | fprintf(stderr, |
290 | 9de5e440 | bellard | "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
|
291 | 9de5e440 | bellard | "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
|
292 | 9de5e440 | bellard | "EFL=%08x EIP=%08x\n",
|
293 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EAX], |
294 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EBX], |
295 | 9de5e440 | bellard | uc->uc_mcontext.gregs[ECX], |
296 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EDX], |
297 | 9de5e440 | bellard | uc->uc_mcontext.gregs[ESI], |
298 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EDI], |
299 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EBP], |
300 | 9de5e440 | bellard | uc->uc_mcontext.gregs[ESP], |
301 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EFL], |
302 | 9de5e440 | bellard | uc->uc_mcontext.gregs[EIP]); |
303 | 9de5e440 | bellard | } |
304 | 9de5e440 | bellard | #else
|
305 | 9de5e440 | bellard | static void dump_regs(struct ucontext *uc) |
306 | 9de5e440 | bellard | { |
307 | 9de5e440 | bellard | } |
308 | 9de5e440 | bellard | #endif
|
309 | 9de5e440 | bellard | |
310 | 9de5e440 | bellard | #endif
|
311 | 9de5e440 | bellard | |
312 | 9de5e440 | bellard | static void host_signal_handler(int host_signum, siginfo_t *info, |
313 | 9de5e440 | bellard | void *puc)
|
314 | 9de5e440 | bellard | { |
315 | 9de5e440 | bellard | int sig;
|
316 | 9de5e440 | bellard | target_siginfo_t tinfo; |
317 | 9de5e440 | bellard | |
318 | 9de5e440 | bellard | /* the CPU emulator uses some host signals to detect exceptions,
|
319 | 9de5e440 | bellard | we we forward to it some signals */
|
320 | 9de5e440 | bellard | if (host_signum == SIGSEGV || host_signum == SIGBUS) {
|
321 | 9de5e440 | bellard | if (cpu_x86_signal_handler(host_signum, info, puc))
|
322 | 9de5e440 | bellard | return;
|
323 | 9de5e440 | bellard | } |
324 | 9de5e440 | bellard | |
325 | 9de5e440 | bellard | /* get target signal number */
|
326 | 9de5e440 | bellard | sig = host_to_target_signal(host_signum); |
327 | 9de5e440 | bellard | if (sig < 1 || sig > TARGET_NSIG) |
328 | 9de5e440 | bellard | return;
|
329 | 9de5e440 | bellard | #if defined(DEBUG_SIGNAL)
|
330 | bc8a22cc | bellard | fprintf(stderr, "qemu: got signal %d\n", sig);
|
331 | 9de5e440 | bellard | dump_regs(puc); |
332 | 9de5e440 | bellard | #endif
|
333 | 9de5e440 | bellard | host_to_target_siginfo_noswap(&tinfo, info); |
334 | 9de5e440 | bellard | if (queue_signal(sig, &tinfo) == 1) { |
335 | 9de5e440 | bellard | /* interrupt the virtual CPU as soon as possible */
|
336 | 9de5e440 | bellard | cpu_x86_interrupt(global_env); |
337 | 66fb9763 | bellard | } |
338 | 66fb9763 | bellard | } |
339 | 66fb9763 | bellard | |
340 | 66fb9763 | bellard | int do_sigaction(int sig, const struct target_sigaction *act, |
341 | 66fb9763 | bellard | struct target_sigaction *oact)
|
342 | 66fb9763 | bellard | { |
343 | 66fb9763 | bellard | struct emulated_sigaction *k;
|
344 | 66fb9763 | bellard | |
345 | 66fb9763 | bellard | if (sig < 1 || sig > TARGET_NSIG) |
346 | 66fb9763 | bellard | return -EINVAL;
|
347 | 66fb9763 | bellard | k = &sigact_table[sig - 1];
|
348 | 66fb9763 | bellard | #if defined(DEBUG_SIGNAL) && 0 |
349 | 66fb9763 | bellard | fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
|
350 | 66fb9763 | bellard | sig, (int)act, (int)oact); |
351 | 66fb9763 | bellard | #endif
|
352 | 66fb9763 | bellard | if (oact) {
|
353 | 66fb9763 | bellard | oact->_sa_handler = tswapl(k->sa._sa_handler); |
354 | 66fb9763 | bellard | oact->sa_flags = tswapl(k->sa.sa_flags); |
355 | 66fb9763 | bellard | oact->sa_restorer = tswapl(k->sa.sa_restorer); |
356 | 66fb9763 | bellard | oact->sa_mask = k->sa.sa_mask; |
357 | 66fb9763 | bellard | } |
358 | 66fb9763 | bellard | if (act) {
|
359 | 66fb9763 | bellard | k->sa._sa_handler = tswapl(act->_sa_handler); |
360 | 66fb9763 | bellard | k->sa.sa_flags = tswapl(act->sa_flags); |
361 | 66fb9763 | bellard | k->sa.sa_restorer = tswapl(act->sa_restorer); |
362 | 66fb9763 | bellard | k->sa.sa_mask = act->sa_mask; |
363 | 66fb9763 | bellard | } |
364 | 66fb9763 | bellard | return 0; |
365 | 66fb9763 | bellard | } |
366 | 66fb9763 | bellard | |
367 | 66fb9763 | bellard | #ifdef TARGET_I386
|
368 | 66fb9763 | bellard | |
369 | 66fb9763 | bellard | /* from the Linux kernel */
|
370 | 66fb9763 | bellard | |
371 | 66fb9763 | bellard | struct target_fpreg {
|
372 | 66fb9763 | bellard | uint16_t significand[4];
|
373 | 66fb9763 | bellard | uint16_t exponent; |
374 | 66fb9763 | bellard | }; |
375 | 66fb9763 | bellard | |
376 | 66fb9763 | bellard | struct target_fpxreg {
|
377 | 66fb9763 | bellard | uint16_t significand[4];
|
378 | 66fb9763 | bellard | uint16_t exponent; |
379 | 66fb9763 | bellard | uint16_t padding[3];
|
380 | 66fb9763 | bellard | }; |
381 | 66fb9763 | bellard | |
382 | 66fb9763 | bellard | struct target_xmmreg {
|
383 | 66fb9763 | bellard | target_ulong element[4];
|
384 | 66fb9763 | bellard | }; |
385 | 66fb9763 | bellard | |
386 | 66fb9763 | bellard | struct target_fpstate {
|
387 | 66fb9763 | bellard | /* Regular FPU environment */
|
388 | 66fb9763 | bellard | target_ulong cw; |
389 | 66fb9763 | bellard | target_ulong sw; |
390 | 66fb9763 | bellard | target_ulong tag; |
391 | 66fb9763 | bellard | target_ulong ipoff; |
392 | 66fb9763 | bellard | target_ulong cssel; |
393 | 66fb9763 | bellard | target_ulong dataoff; |
394 | 66fb9763 | bellard | target_ulong datasel; |
395 | 66fb9763 | bellard | struct target_fpreg _st[8]; |
396 | 66fb9763 | bellard | uint16_t status; |
397 | 66fb9763 | bellard | uint16_t magic; /* 0xffff = regular FPU data only */
|
398 | 66fb9763 | bellard | |
399 | 66fb9763 | bellard | /* FXSR FPU environment */
|
400 | 66fb9763 | bellard | target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */ |
401 | 66fb9763 | bellard | target_ulong mxcsr; |
402 | 66fb9763 | bellard | target_ulong reserved; |
403 | 66fb9763 | bellard | struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */ |
404 | 66fb9763 | bellard | struct target_xmmreg _xmm[8]; |
405 | 66fb9763 | bellard | target_ulong padding[56];
|
406 | 66fb9763 | bellard | }; |
407 | 66fb9763 | bellard | |
408 | 66fb9763 | bellard | #define X86_FXSR_MAGIC 0x0000 |
409 | 66fb9763 | bellard | |
410 | 66fb9763 | bellard | struct target_sigcontext {
|
411 | 66fb9763 | bellard | uint16_t gs, __gsh; |
412 | 66fb9763 | bellard | uint16_t fs, __fsh; |
413 | 66fb9763 | bellard | uint16_t es, __esh; |
414 | 66fb9763 | bellard | uint16_t ds, __dsh; |
415 | 66fb9763 | bellard | target_ulong edi; |
416 | 66fb9763 | bellard | target_ulong esi; |
417 | 66fb9763 | bellard | target_ulong ebp; |
418 | 66fb9763 | bellard | target_ulong esp; |
419 | 66fb9763 | bellard | target_ulong ebx; |
420 | 66fb9763 | bellard | target_ulong edx; |
421 | 66fb9763 | bellard | target_ulong ecx; |
422 | 66fb9763 | bellard | target_ulong eax; |
423 | 66fb9763 | bellard | target_ulong trapno; |
424 | 66fb9763 | bellard | target_ulong err; |
425 | 66fb9763 | bellard | target_ulong eip; |
426 | 66fb9763 | bellard | uint16_t cs, __csh; |
427 | 66fb9763 | bellard | target_ulong eflags; |
428 | 66fb9763 | bellard | target_ulong esp_at_signal; |
429 | 66fb9763 | bellard | uint16_t ss, __ssh; |
430 | 66fb9763 | bellard | target_ulong fpstate; /* pointer */
|
431 | 66fb9763 | bellard | target_ulong oldmask; |
432 | 66fb9763 | bellard | target_ulong cr2; |
433 | 66fb9763 | bellard | }; |
434 | 66fb9763 | bellard | |
435 | 66fb9763 | bellard | typedef struct target_sigaltstack { |
436 | 66fb9763 | bellard | target_ulong ss_sp; |
437 | 66fb9763 | bellard | int ss_flags;
|
438 | 66fb9763 | bellard | target_ulong ss_size; |
439 | 66fb9763 | bellard | } target_stack_t; |
440 | 66fb9763 | bellard | |
441 | 66fb9763 | bellard | struct target_ucontext {
|
442 | 66fb9763 | bellard | target_ulong uc_flags; |
443 | 66fb9763 | bellard | target_ulong uc_link; |
444 | 66fb9763 | bellard | target_stack_t uc_stack; |
445 | 66fb9763 | bellard | struct target_sigcontext uc_mcontext;
|
446 | 66fb9763 | bellard | target_sigset_t uc_sigmask; /* mask last for extensibility */
|
447 | 66fb9763 | bellard | }; |
448 | 66fb9763 | bellard | |
449 | 66fb9763 | bellard | struct sigframe
|
450 | 66fb9763 | bellard | { |
451 | 66fb9763 | bellard | target_ulong pretcode; |
452 | 66fb9763 | bellard | int sig;
|
453 | 66fb9763 | bellard | struct target_sigcontext sc;
|
454 | 66fb9763 | bellard | struct target_fpstate fpstate;
|
455 | 66fb9763 | bellard | target_ulong extramask[TARGET_NSIG_WORDS-1];
|
456 | 66fb9763 | bellard | char retcode[8]; |
457 | 66fb9763 | bellard | }; |
458 | 66fb9763 | bellard | |
459 | 66fb9763 | bellard | struct rt_sigframe
|
460 | 66fb9763 | bellard | { |
461 | 66fb9763 | bellard | target_ulong pretcode; |
462 | 66fb9763 | bellard | int sig;
|
463 | 66fb9763 | bellard | target_ulong pinfo; |
464 | 66fb9763 | bellard | target_ulong puc; |
465 | 66fb9763 | bellard | struct target_siginfo info;
|
466 | 66fb9763 | bellard | struct target_ucontext uc;
|
467 | 66fb9763 | bellard | struct target_fpstate fpstate;
|
468 | 66fb9763 | bellard | char retcode[8]; |
469 | 66fb9763 | bellard | }; |
470 | 66fb9763 | bellard | |
471 | 66fb9763 | bellard | /*
|
472 | 66fb9763 | bellard | * Set up a signal frame.
|
473 | 66fb9763 | bellard | */
|
474 | 66fb9763 | bellard | |
475 | 66fb9763 | bellard | #define __put_user(x,ptr)\
|
476 | 66fb9763 | bellard | ({\ |
477 | 66fb9763 | bellard | int size = sizeof(*ptr);\ |
478 | 66fb9763 | bellard | switch(size) {\
|
479 | 66fb9763 | bellard | case 1:\ |
480 | 66fb9763 | bellard | stb(ptr, (typeof(*ptr))(x));\ |
481 | 66fb9763 | bellard | break;\
|
482 | 66fb9763 | bellard | case 2:\ |
483 | 66fb9763 | bellard | stw(ptr, (typeof(*ptr))(x));\ |
484 | 66fb9763 | bellard | break;\
|
485 | 66fb9763 | bellard | case 4:\ |
486 | 66fb9763 | bellard | stl(ptr, (typeof(*ptr))(x));\ |
487 | 66fb9763 | bellard | break;\
|
488 | 66fb9763 | bellard | case 8:\ |
489 | 66fb9763 | bellard | stq(ptr, (typeof(*ptr))(x));\ |
490 | 66fb9763 | bellard | break;\
|
491 | 66fb9763 | bellard | default:\
|
492 | 66fb9763 | bellard | abort();\ |
493 | 66fb9763 | bellard | }\ |
494 | 66fb9763 | bellard | 0;\
|
495 | 66fb9763 | bellard | }) |
496 | 66fb9763 | bellard | |
497 | 66fb9763 | bellard | #define get_user(val, ptr) (typeof(*ptr))(*(ptr))
|
498 | 66fb9763 | bellard | |
499 | 66fb9763 | bellard | |
500 | 66fb9763 | bellard | #define __copy_to_user(dst, src, size)\
|
501 | 66fb9763 | bellard | ({\ |
502 | 66fb9763 | bellard | memcpy(dst, src, size);\ |
503 | 66fb9763 | bellard | 0;\
|
504 | 66fb9763 | bellard | }) |
505 | 66fb9763 | bellard | |
506 | 9de5e440 | bellard | static inline int copy_siginfo_to_user(target_siginfo_t *tinfo, |
507 | 9de5e440 | bellard | const target_siginfo_t *info)
|
508 | 66fb9763 | bellard | { |
509 | 9de5e440 | bellard | tswap_siginfo(tinfo, info); |
510 | 66fb9763 | bellard | return 0; |
511 | 66fb9763 | bellard | } |
512 | 66fb9763 | bellard | |
513 | 66fb9763 | bellard | /* XXX: save x87 state */
|
514 | 66fb9763 | bellard | static int |
515 | 66fb9763 | bellard | setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate, |
516 | 66fb9763 | bellard | CPUX86State *env, unsigned long mask) |
517 | 66fb9763 | bellard | { |
518 | 66fb9763 | bellard | int err = 0; |
519 | 66fb9763 | bellard | |
520 | 66fb9763 | bellard | err |= __put_user(env->segs[R_GS], (unsigned int *)&sc->gs); |
521 | 66fb9763 | bellard | err |= __put_user(env->segs[R_FS], (unsigned int *)&sc->fs); |
522 | 66fb9763 | bellard | err |= __put_user(env->segs[R_ES], (unsigned int *)&sc->es); |
523 | 66fb9763 | bellard | err |= __put_user(env->segs[R_DS], (unsigned int *)&sc->ds); |
524 | 66fb9763 | bellard | err |= __put_user(env->regs[R_EDI], &sc->edi); |
525 | 66fb9763 | bellard | err |= __put_user(env->regs[R_ESI], &sc->esi); |
526 | 66fb9763 | bellard | err |= __put_user(env->regs[R_EBP], &sc->ebp); |
527 | 66fb9763 | bellard | err |= __put_user(env->regs[R_ESP], &sc->esp); |
528 | 66fb9763 | bellard | err |= __put_user(env->regs[R_EBX], &sc->ebx); |
529 | 66fb9763 | bellard | err |= __put_user(env->regs[R_EDX], &sc->edx); |
530 | 66fb9763 | bellard | err |= __put_user(env->regs[R_ECX], &sc->ecx); |
531 | 66fb9763 | bellard | err |= __put_user(env->regs[R_EAX], &sc->eax); |
532 | 66099dd9 | bellard | err |= __put_user(env->exception_index, &sc->trapno); |
533 | 66099dd9 | bellard | err |= __put_user(env->error_code, &sc->err); |
534 | 66fb9763 | bellard | err |= __put_user(env->eip, &sc->eip); |
535 | 66fb9763 | bellard | err |= __put_user(env->segs[R_CS], (unsigned int *)&sc->cs); |
536 | 66fb9763 | bellard | err |= __put_user(env->eflags, &sc->eflags); |
537 | 66fb9763 | bellard | err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal); |
538 | 66fb9763 | bellard | err |= __put_user(env->segs[R_SS], (unsigned int *)&sc->ss); |
539 | 66fb9763 | bellard | #if 0
|
540 | 66fb9763 | bellard | tmp = save_i387(fpstate);
|
541 | 66fb9763 | bellard | if (tmp < 0)
|
542 | 66fb9763 | bellard | err = 1;
|
543 | 66fb9763 | bellard | else
|
544 | 66fb9763 | bellard | err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
|
545 | 66fb9763 | bellard | #else
|
546 | 66fb9763 | bellard | err |= __put_user(0, &sc->fpstate);
|
547 | 66fb9763 | bellard | #endif
|
548 | 66fb9763 | bellard | /* non-iBCS2 extensions.. */
|
549 | 66fb9763 | bellard | err |= __put_user(mask, &sc->oldmask); |
550 | 66fb9763 | bellard | err |= __put_user(/*current->thread.cr2*/ 0, &sc->cr2); |
551 | 66fb9763 | bellard | return err;
|
552 | 31e31b8a | bellard | } |
553 | 31e31b8a | bellard | |
554 | 66fb9763 | bellard | /*
|
555 | 66fb9763 | bellard | * Determine which stack to use..
|
556 | 66fb9763 | bellard | */
|
557 | 31e31b8a | bellard | |
558 | 66fb9763 | bellard | static inline void * |
559 | 66fb9763 | bellard | get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
|
560 | 31e31b8a | bellard | { |
561 | 66fb9763 | bellard | unsigned long esp; |
562 | 66fb9763 | bellard | |
563 | 66fb9763 | bellard | /* Default to using normal stack */
|
564 | 66fb9763 | bellard | esp = env->regs[R_ESP]; |
565 | 66fb9763 | bellard | #if 0
|
566 | 66fb9763 | bellard | /* This is the X/Open sanctioned signal stack switching. */
|
567 | 66fb9763 | bellard | if (ka->sa.sa_flags & SA_ONSTACK) {
|
568 | 66fb9763 | bellard | if (sas_ss_flags(esp) == 0)
|
569 | 66fb9763 | bellard | esp = current->sas_ss_sp + current->sas_ss_size;
|
570 | 66fb9763 | bellard | }
|
571 | 66fb9763 | bellard | |
572 | 66fb9763 | bellard | /* This is the legacy signal stack switching. */
|
573 | 66fb9763 | bellard | else if ((regs->xss & 0xffff) != __USER_DS &&
|
574 | 66fb9763 | bellard | !(ka->sa.sa_flags & SA_RESTORER) &&
|
575 | 66fb9763 | bellard | ka->sa.sa_restorer) {
|
576 | 66fb9763 | bellard | esp = (unsigned long) ka->sa.sa_restorer;
|
577 | 66fb9763 | bellard | }
|
578 | 66fb9763 | bellard | #endif
|
579 | 66fb9763 | bellard | return (void *)((esp - frame_size) & -8ul); |
580 | 66fb9763 | bellard | } |
581 | 66fb9763 | bellard | |
582 | 66fb9763 | bellard | static void setup_frame(int sig, struct emulated_sigaction *ka, |
583 | 66fb9763 | bellard | target_sigset_t *set, CPUX86State *env) |
584 | 66fb9763 | bellard | { |
585 | 66fb9763 | bellard | struct sigframe *frame;
|
586 | 66fb9763 | bellard | int err = 0; |
587 | 66fb9763 | bellard | |
588 | 66fb9763 | bellard | frame = get_sigframe(ka, env, sizeof(*frame));
|
589 | 66fb9763 | bellard | |
590 | 66fb9763 | bellard | #if 0
|
591 | 66fb9763 | bellard | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
|
592 | 66fb9763 | bellard | goto give_sigsegv;
|
593 | 66fb9763 | bellard | #endif
|
594 | 66fb9763 | bellard | err |= __put_user((/*current->exec_domain
|
595 | 66fb9763 | bellard | && current->exec_domain->signal_invmap
|
596 | 66fb9763 | bellard | && sig < 32
|
597 | 66fb9763 | bellard | ? current->exec_domain->signal_invmap[sig]
|
598 | 66fb9763 | bellard | : */ sig),
|
599 | 66fb9763 | bellard | &frame->sig); |
600 | 66fb9763 | bellard | if (err)
|
601 | 66fb9763 | bellard | goto give_sigsegv;
|
602 | 66fb9763 | bellard | |
603 | 66fb9763 | bellard | setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
|
604 | 66fb9763 | bellard | if (err)
|
605 | 66fb9763 | bellard | goto give_sigsegv;
|
606 | 66fb9763 | bellard | |
607 | 66fb9763 | bellard | if (TARGET_NSIG_WORDS > 1) { |
608 | 66fb9763 | bellard | err |= __copy_to_user(frame->extramask, &set->sig[1],
|
609 | 66fb9763 | bellard | sizeof(frame->extramask));
|
610 | 66fb9763 | bellard | } |
611 | 66fb9763 | bellard | if (err)
|
612 | 66fb9763 | bellard | goto give_sigsegv;
|
613 | 66fb9763 | bellard | |
614 | 66fb9763 | bellard | /* Set up to return from userspace. If provided, use a stub
|
615 | 66fb9763 | bellard | already in userspace. */
|
616 | 66fb9763 | bellard | if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
|
617 | 66fb9763 | bellard | err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); |
618 | 66fb9763 | bellard | } else {
|
619 | 66fb9763 | bellard | err |= __put_user(frame->retcode, &frame->pretcode); |
620 | 66fb9763 | bellard | /* This is popl %eax ; movl $,%eax ; int $0x80 */
|
621 | 66fb9763 | bellard | err |= __put_user(0xb858, (short *)(frame->retcode+0)); |
622 | 66fb9763 | bellard | err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2)); |
623 | 66fb9763 | bellard | err |= __put_user(0x80cd, (short *)(frame->retcode+6)); |
624 | 66fb9763 | bellard | } |
625 | 66fb9763 | bellard | |
626 | 66fb9763 | bellard | if (err)
|
627 | 66fb9763 | bellard | goto give_sigsegv;
|
628 | 66fb9763 | bellard | |
629 | 66fb9763 | bellard | /* Set up registers for signal handler */
|
630 | 66fb9763 | bellard | env->regs[R_ESP] = (unsigned long) frame; |
631 | 66fb9763 | bellard | env->eip = (unsigned long) ka->sa._sa_handler; |
632 | 66fb9763 | bellard | |
633 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_DS, __USER_DS); |
634 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_ES, __USER_DS); |
635 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_SS, __USER_DS); |
636 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_CS, __USER_CS); |
637 | 66fb9763 | bellard | env->eflags &= ~TF_MASK; |
638 | 66fb9763 | bellard | |
639 | 66fb9763 | bellard | return;
|
640 | 66fb9763 | bellard | |
641 | 66fb9763 | bellard | give_sigsegv:
|
642 | 66fb9763 | bellard | if (sig == TARGET_SIGSEGV)
|
643 | 66fb9763 | bellard | ka->sa._sa_handler = TARGET_SIG_DFL; |
644 | 66fb9763 | bellard | force_sig(TARGET_SIGSEGV /* , current */);
|
645 | 66fb9763 | bellard | } |
646 | 66fb9763 | bellard | |
647 | 9de5e440 | bellard | static void setup_rt_frame(int sig, struct emulated_sigaction *ka, |
648 | 9de5e440 | bellard | target_siginfo_t *info, |
649 | 66fb9763 | bellard | target_sigset_t *set, CPUX86State *env) |
650 | 66fb9763 | bellard | { |
651 | 66fb9763 | bellard | struct rt_sigframe *frame;
|
652 | 66fb9763 | bellard | int err = 0; |
653 | 66fb9763 | bellard | |
654 | 66fb9763 | bellard | frame = get_sigframe(ka, env, sizeof(*frame));
|
655 | 66fb9763 | bellard | |
656 | 66fb9763 | bellard | #if 0
|
657 | 66fb9763 | bellard | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
|
658 | 66fb9763 | bellard | goto give_sigsegv;
|
659 | 66fb9763 | bellard | #endif
|
660 | 66fb9763 | bellard | |
661 | 66fb9763 | bellard | err |= __put_user((/*current->exec_domain
|
662 | 66fb9763 | bellard | && current->exec_domain->signal_invmap
|
663 | 66fb9763 | bellard | && sig < 32
|
664 | 66fb9763 | bellard | ? current->exec_domain->signal_invmap[sig]
|
665 | 66fb9763 | bellard | : */sig),
|
666 | 66fb9763 | bellard | &frame->sig); |
667 | 66fb9763 | bellard | err |= __put_user((target_ulong)&frame->info, &frame->pinfo); |
668 | 66fb9763 | bellard | err |= __put_user((target_ulong)&frame->uc, &frame->puc); |
669 | 66fb9763 | bellard | err |= copy_siginfo_to_user(&frame->info, info); |
670 | 66fb9763 | bellard | if (err)
|
671 | 66fb9763 | bellard | goto give_sigsegv;
|
672 | 31e31b8a | bellard | |
673 | 66fb9763 | bellard | /* Create the ucontext. */
|
674 | 66fb9763 | bellard | err |= __put_user(0, &frame->uc.uc_flags);
|
675 | 66fb9763 | bellard | err |= __put_user(0, &frame->uc.uc_link);
|
676 | 66fb9763 | bellard | err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp); |
677 | 66fb9763 | bellard | err |= __put_user(/* sas_ss_flags(regs->esp) */ 0, |
678 | 66fb9763 | bellard | &frame->uc.uc_stack.ss_flags); |
679 | 66fb9763 | bellard | err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size); |
680 | 66fb9763 | bellard | err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate, |
681 | 66fb9763 | bellard | env, set->sig[0]);
|
682 | 66fb9763 | bellard | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
|
683 | 66fb9763 | bellard | if (err)
|
684 | 66fb9763 | bellard | goto give_sigsegv;
|
685 | 31e31b8a | bellard | |
686 | 66fb9763 | bellard | /* Set up to return from userspace. If provided, use a stub
|
687 | 66fb9763 | bellard | already in userspace. */
|
688 | 66fb9763 | bellard | if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
|
689 | 66fb9763 | bellard | err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); |
690 | 66fb9763 | bellard | } else {
|
691 | 66fb9763 | bellard | err |= __put_user(frame->retcode, &frame->pretcode); |
692 | 66fb9763 | bellard | /* This is movl $,%eax ; int $0x80 */
|
693 | 66fb9763 | bellard | err |= __put_user(0xb8, (char *)(frame->retcode+0)); |
694 | 66fb9763 | bellard | err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1)); |
695 | 66fb9763 | bellard | err |= __put_user(0x80cd, (short *)(frame->retcode+5)); |
696 | 66fb9763 | bellard | } |
697 | 66fb9763 | bellard | |
698 | 66fb9763 | bellard | if (err)
|
699 | 66fb9763 | bellard | goto give_sigsegv;
|
700 | 66fb9763 | bellard | |
701 | 66fb9763 | bellard | /* Set up registers for signal handler */
|
702 | 66fb9763 | bellard | env->regs[R_ESP] = (unsigned long) frame; |
703 | 66fb9763 | bellard | env->eip = (unsigned long) ka->sa._sa_handler; |
704 | 66fb9763 | bellard | |
705 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_DS, __USER_DS); |
706 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_ES, __USER_DS); |
707 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_SS, __USER_DS); |
708 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_CS, __USER_CS); |
709 | 66fb9763 | bellard | env->eflags &= ~TF_MASK; |
710 | 66fb9763 | bellard | |
711 | 66fb9763 | bellard | return;
|
712 | 66fb9763 | bellard | |
713 | 66fb9763 | bellard | give_sigsegv:
|
714 | 66fb9763 | bellard | if (sig == TARGET_SIGSEGV)
|
715 | 66fb9763 | bellard | ka->sa._sa_handler = TARGET_SIG_DFL; |
716 | 66fb9763 | bellard | force_sig(TARGET_SIGSEGV /* , current */);
|
717 | 66fb9763 | bellard | } |
718 | 66fb9763 | bellard | |
719 | 66fb9763 | bellard | static int |
720 | 66fb9763 | bellard | restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax) |
721 | 66fb9763 | bellard | { |
722 | 66fb9763 | bellard | unsigned int err = 0; |
723 | 66fb9763 | bellard | |
724 | 66fb9763 | bellard | |
725 | 66fb9763 | bellard | |
726 | 66fb9763 | bellard | #define COPY(x) err |= __get_user(regs->x, &sc->x)
|
727 | 66fb9763 | bellard | |
728 | 66fb9763 | bellard | #define COPY_SEG(seg) \
|
729 | 66fb9763 | bellard | { unsigned short tmp; \ |
730 | 66fb9763 | bellard | err |= __get_user(tmp, &sc->seg); \ |
731 | 66fb9763 | bellard | regs->x##seg = tmp; } |
732 | 66fb9763 | bellard | |
733 | 66fb9763 | bellard | #define COPY_SEG_STRICT(seg) \
|
734 | 66fb9763 | bellard | { unsigned short tmp; \ |
735 | 66fb9763 | bellard | err |= __get_user(tmp, &sc->seg); \ |
736 | 66fb9763 | bellard | regs->x##seg = tmp|3; } |
737 | 66fb9763 | bellard | |
738 | 66fb9763 | bellard | #define GET_SEG(seg) \
|
739 | 66fb9763 | bellard | { unsigned short tmp; \ |
740 | 66fb9763 | bellard | err |= __get_user(tmp, &sc->seg); \ |
741 | 66fb9763 | bellard | loadsegment(seg,tmp); } |
742 | 66fb9763 | bellard | |
743 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_GS, lduw(&sc->gs)); |
744 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_FS, lduw(&sc->fs)); |
745 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_ES, lduw(&sc->es)); |
746 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_DS, lduw(&sc->ds)); |
747 | 66fb9763 | bellard | |
748 | 66fb9763 | bellard | env->regs[R_EDI] = ldl(&sc->edi); |
749 | 66fb9763 | bellard | env->regs[R_ESI] = ldl(&sc->esi); |
750 | 66fb9763 | bellard | env->regs[R_EBP] = ldl(&sc->ebp); |
751 | 66fb9763 | bellard | env->regs[R_ESP] = ldl(&sc->esp); |
752 | 66fb9763 | bellard | env->regs[R_EBX] = ldl(&sc->ebx); |
753 | 66fb9763 | bellard | env->regs[R_EDX] = ldl(&sc->edx); |
754 | 66fb9763 | bellard | env->regs[R_ECX] = ldl(&sc->ecx); |
755 | 66fb9763 | bellard | env->eip = ldl(&sc->eip); |
756 | 66fb9763 | bellard | |
757 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
|
758 | 66fb9763 | bellard | cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
|
759 | 66fb9763 | bellard | |
760 | 66fb9763 | bellard | { |
761 | 66fb9763 | bellard | unsigned int tmpflags; |
762 | 66fb9763 | bellard | tmpflags = ldl(&sc->eflags); |
763 | 66fb9763 | bellard | env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5); |
764 | 66fb9763 | bellard | // regs->orig_eax = -1; /* disable syscall checks */
|
765 | 66fb9763 | bellard | } |
766 | 66fb9763 | bellard | |
767 | 66fb9763 | bellard | #if 0
|
768 | 66fb9763 | bellard | {
|
769 | 66fb9763 | bellard | struct _fpstate * buf;
|
770 | 66fb9763 | bellard | err |= __get_user(buf, &sc->fpstate);
|
771 | 66fb9763 | bellard | if (buf) {
|
772 | 66fb9763 | bellard | if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
|
773 | 66fb9763 | bellard | goto badframe;
|
774 | 66fb9763 | bellard | err |= restore_i387(buf);
|
775 | 66fb9763 | bellard | }
|
776 | 66fb9763 | bellard | }
|
777 | 66fb9763 | bellard | #endif
|
778 | 66fb9763 | bellard | *peax = ldl(&sc->eax); |
779 | 66fb9763 | bellard | return err;
|
780 | 66fb9763 | bellard | #if 0
|
781 | 66fb9763 | bellard | badframe:
|
782 | 66fb9763 | bellard | return 1;
|
783 | 66fb9763 | bellard | #endif
|
784 | 66fb9763 | bellard | } |
785 | 66fb9763 | bellard | |
786 | 66fb9763 | bellard | long do_sigreturn(CPUX86State *env)
|
787 | 66fb9763 | bellard | { |
788 | 66fb9763 | bellard | struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8); |
789 | 66fb9763 | bellard | target_sigset_t target_set; |
790 | 66fb9763 | bellard | sigset_t set; |
791 | 66fb9763 | bellard | int eax, i;
|
792 | 66fb9763 | bellard | |
793 | 447db213 | bellard | #if defined(DEBUG_SIGNAL)
|
794 | 447db213 | bellard | fprintf(stderr, "do_sigreturn\n");
|
795 | 447db213 | bellard | #endif
|
796 | 66fb9763 | bellard | /* set blocked signals */
|
797 | 66fb9763 | bellard | target_set.sig[0] = frame->sc.oldmask;
|
798 | 66fb9763 | bellard | for(i = 1; i < TARGET_NSIG_WORDS; i++) |
799 | 66fb9763 | bellard | target_set.sig[i] = frame->extramask[i - 1];
|
800 | 66fb9763 | bellard | |
801 | 66fb9763 | bellard | target_to_host_sigset(&set, &target_set); |
802 | 66fb9763 | bellard | sigprocmask(SIG_SETMASK, &set, NULL);
|
803 | 66fb9763 | bellard | |
804 | 66fb9763 | bellard | /* restore registers */
|
805 | 66fb9763 | bellard | if (restore_sigcontext(env, &frame->sc, &eax))
|
806 | 66fb9763 | bellard | goto badframe;
|
807 | 66fb9763 | bellard | return eax;
|
808 | 66fb9763 | bellard | |
809 | 66fb9763 | bellard | badframe:
|
810 | 66fb9763 | bellard | force_sig(TARGET_SIGSEGV); |
811 | 66fb9763 | bellard | return 0; |
812 | 66fb9763 | bellard | } |
813 | 66fb9763 | bellard | |
814 | 66fb9763 | bellard | long do_rt_sigreturn(CPUX86State *env)
|
815 | 66fb9763 | bellard | { |
816 | 66fb9763 | bellard | struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4); |
817 | 66fb9763 | bellard | target_sigset_t target_set; |
818 | 66fb9763 | bellard | sigset_t set; |
819 | 66fb9763 | bellard | // stack_t st;
|
820 | 66fb9763 | bellard | int eax;
|
821 | 66fb9763 | bellard | |
822 | 66fb9763 | bellard | #if 0
|
823 | 66fb9763 | bellard | if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
|
824 | 66fb9763 | bellard | goto badframe;
|
825 | 66fb9763 | bellard | #endif
|
826 | 66fb9763 | bellard | memcpy(&target_set, &frame->uc.uc_sigmask, sizeof(target_sigset_t));
|
827 | 66fb9763 | bellard | |
828 | 66fb9763 | bellard | target_to_host_sigset(&set, &target_set); |
829 | 66fb9763 | bellard | sigprocmask(SIG_SETMASK, &set, NULL);
|
830 | 66fb9763 | bellard | |
831 | 66fb9763 | bellard | if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
|
832 | 66fb9763 | bellard | goto badframe;
|
833 | 66fb9763 | bellard | |
834 | 66fb9763 | bellard | #if 0
|
835 | 66fb9763 | bellard | if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
|
836 | 66fb9763 | bellard | goto badframe;
|
837 | 66fb9763 | bellard | /* It is more difficult to avoid calling this function than to
|
838 | 66fb9763 | bellard | call it and ignore errors. */
|
839 | 66fb9763 | bellard | do_sigaltstack(&st, NULL, regs->esp);
|
840 | 66fb9763 | bellard | #endif
|
841 | 66fb9763 | bellard | return eax;
|
842 | 66fb9763 | bellard | |
843 | 66fb9763 | bellard | badframe:
|
844 | 66fb9763 | bellard | force_sig(TARGET_SIGSEGV); |
845 | 66fb9763 | bellard | return 0; |
846 | 66fb9763 | bellard | } |
847 | 66fb9763 | bellard | |
848 | 66fb9763 | bellard | #endif
|
849 | 66fb9763 | bellard | |
850 | 66fb9763 | bellard | void process_pending_signals(void *cpu_env) |
851 | 66fb9763 | bellard | { |
852 | 66fb9763 | bellard | int sig;
|
853 | 66fb9763 | bellard | target_ulong handler; |
854 | 9de5e440 | bellard | sigset_t set, old_set; |
855 | 9de5e440 | bellard | target_sigset_t target_old_set; |
856 | 66fb9763 | bellard | struct emulated_sigaction *k;
|
857 | 66fb9763 | bellard | struct sigqueue *q;
|
858 | 66fb9763 | bellard | |
859 | 31e31b8a | bellard | if (!signal_pending)
|
860 | 31e31b8a | bellard | return;
|
861 | 31e31b8a | bellard | |
862 | 66fb9763 | bellard | k = sigact_table; |
863 | 66fb9763 | bellard | for(sig = 1; sig <= TARGET_NSIG; sig++) { |
864 | 66fb9763 | bellard | if (k->pending)
|
865 | 31e31b8a | bellard | goto handle_signal;
|
866 | 66fb9763 | bellard | k++; |
867 | 31e31b8a | bellard | } |
868 | 31e31b8a | bellard | /* if no signal is pending, just return */
|
869 | 31e31b8a | bellard | signal_pending = 0;
|
870 | 31e31b8a | bellard | return;
|
871 | 66fb9763 | bellard | |
872 | 31e31b8a | bellard | handle_signal:
|
873 | 66fb9763 | bellard | #ifdef DEBUG_SIGNAL
|
874 | bc8a22cc | bellard | fprintf(stderr, "qemu: process signal %d\n", sig);
|
875 | 66fb9763 | bellard | #endif
|
876 | 66fb9763 | bellard | /* dequeue signal */
|
877 | 66fb9763 | bellard | q = k->first; |
878 | 66fb9763 | bellard | k->first = q->next; |
879 | 66fb9763 | bellard | if (!k->first)
|
880 | 66fb9763 | bellard | k->pending = 0;
|
881 | 66fb9763 | bellard | |
882 | 66fb9763 | bellard | handler = k->sa._sa_handler; |
883 | 66fb9763 | bellard | if (handler == TARGET_SIG_DFL) {
|
884 | 66fb9763 | bellard | /* default handler : ignore some signal. The other are fatal */
|
885 | 66fb9763 | bellard | if (sig != TARGET_SIGCHLD &&
|
886 | 66fb9763 | bellard | sig != TARGET_SIGURG && |
887 | 66fb9763 | bellard | sig != TARGET_SIGWINCH) { |
888 | 66fb9763 | bellard | force_sig(sig); |
889 | 66fb9763 | bellard | } |
890 | 66fb9763 | bellard | } else if (handler == TARGET_SIG_IGN) { |
891 | 66fb9763 | bellard | /* ignore sig */
|
892 | 66fb9763 | bellard | } else if (handler == TARGET_SIG_ERR) { |
893 | 66fb9763 | bellard | force_sig(sig); |
894 | 66fb9763 | bellard | } else {
|
895 | 9de5e440 | bellard | /* compute the blocked signals during the handler execution */
|
896 | 9de5e440 | bellard | target_to_host_sigset(&set, &k->sa.sa_mask); |
897 | 9de5e440 | bellard | /* SA_NODEFER indicates that the current signal should not be
|
898 | 9de5e440 | bellard | blocked during the handler */
|
899 | 9de5e440 | bellard | if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
|
900 | 9de5e440 | bellard | sigaddset(&set, target_to_host_signal(sig)); |
901 | 9de5e440 | bellard | |
902 | 9de5e440 | bellard | /* block signals in the handler using Linux */
|
903 | 9de5e440 | bellard | sigprocmask(SIG_BLOCK, &set, &old_set); |
904 | 9de5e440 | bellard | /* save the previous blocked signal state to restore it at the
|
905 | 9de5e440 | bellard | end of the signal execution (see do_sigreturn) */
|
906 | 9de5e440 | bellard | host_to_target_sigset(&target_old_set, &old_set); |
907 | 9de5e440 | bellard | |
908 | bc8a22cc | bellard | /* if the CPU is in VM86 mode, we restore the 32 bit values */
|
909 | bc8a22cc | bellard | #ifdef TARGET_I386
|
910 | bc8a22cc | bellard | { |
911 | bc8a22cc | bellard | CPUX86State *env = cpu_env; |
912 | bc8a22cc | bellard | if (env->eflags & VM_MASK)
|
913 | bc8a22cc | bellard | save_v86_state(env); |
914 | bc8a22cc | bellard | } |
915 | bc8a22cc | bellard | #endif
|
916 | 9de5e440 | bellard | /* prepare the stack frame of the virtual CPU */
|
917 | 66fb9763 | bellard | if (k->sa.sa_flags & TARGET_SA_SIGINFO)
|
918 | 9de5e440 | bellard | setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env); |
919 | 66fb9763 | bellard | else
|
920 | 9de5e440 | bellard | setup_frame(sig, k, &target_old_set, cpu_env); |
921 | 66fb9763 | bellard | if (k->sa.sa_flags & TARGET_SA_RESETHAND)
|
922 | 66fb9763 | bellard | k->sa._sa_handler = TARGET_SIG_DFL; |
923 | 31e31b8a | bellard | } |
924 | 66fb9763 | bellard | if (q != &k->info)
|
925 | 66fb9763 | bellard | free_sigqueue(q); |
926 | 66fb9763 | bellard | } |
927 | 31e31b8a | bellard |