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