2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "signal-common.h"
22 #include "linux-user/trace.h"
24 /* from the Linux kernel - /arch/x86/include/uapi/asm/sigcontext.h */
27 uint16_t significand
[4];
31 struct target_fpxreg
{
32 uint16_t significand
[4];
37 struct target_xmmreg
{
41 struct target_fpstate_32
{
42 /* Regular FPU environment */
50 struct target_fpreg st
[8];
52 uint16_t magic
; /* 0xffff = regular FPU data only */
54 /* FXSR FPU environment */
55 uint32_t _fxsr_env
[6]; /* FXSR FPU env is ignored */
58 struct target_fpxreg fxsr_st
[8]; /* FXSR FPU reg data is ignored */
59 struct target_xmmreg xmm
[8];
63 struct target_fpstate_64
{
73 uint32_t st_space
[32];
74 uint32_t xmm_space
[64];
75 uint32_t reserved
[24];
79 # define target_fpstate target_fpstate_32
81 # define target_fpstate target_fpstate_64
84 struct target_sigcontext_32
{
102 uint32_t esp_at_signal
;
104 uint32_t fpstate
; /* pointer */
109 struct target_sigcontext_64
{
141 uint64_t fpstate
; /* pointer */
145 #ifndef TARGET_X86_64
146 # define target_sigcontext target_sigcontext_32
148 # define target_sigcontext target_sigcontext_64
151 /* see Linux/include/uapi/asm-generic/ucontext.h */
152 struct target_ucontext
{
155 target_stack_t tuc_stack
;
156 struct target_sigcontext tuc_mcontext
;
157 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
160 #ifndef TARGET_X86_64
164 struct target_sigcontext sc
;
165 struct target_fpstate fpstate
;
166 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
175 struct target_siginfo info
;
176 struct target_ucontext uc
;
177 struct target_fpstate fpstate
;
185 struct target_ucontext uc
;
186 struct target_siginfo info
;
187 struct target_fpstate fpstate
;
193 * Set up a signal frame.
196 /* XXX: save x87 state */
197 static void setup_sigcontext(struct target_sigcontext
*sc
,
198 struct target_fpstate
*fpstate
, CPUX86State
*env
, abi_ulong mask
,
199 abi_ulong fpstate_addr
)
201 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
202 #ifndef TARGET_X86_64
205 /* already locked in setup_frame() */
206 __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
207 __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
208 __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
209 __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
210 __put_user(env
->regs
[R_EDI
], &sc
->edi
);
211 __put_user(env
->regs
[R_ESI
], &sc
->esi
);
212 __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
213 __put_user(env
->regs
[R_ESP
], &sc
->esp
);
214 __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
215 __put_user(env
->regs
[R_EDX
], &sc
->edx
);
216 __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
217 __put_user(env
->regs
[R_EAX
], &sc
->eax
);
218 __put_user(cs
->exception_index
, &sc
->trapno
);
219 __put_user(env
->error_code
, &sc
->err
);
220 __put_user(env
->eip
, &sc
->eip
);
221 __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
222 __put_user(env
->eflags
, &sc
->eflags
);
223 __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
224 __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
226 cpu_x86_fsave(env
, fpstate_addr
, 1);
227 fpstate
->status
= fpstate
->sw
;
229 __put_user(magic
, &fpstate
->magic
);
230 __put_user(fpstate_addr
, &sc
->fpstate
);
232 /* non-iBCS2 extensions.. */
233 __put_user(mask
, &sc
->oldmask
);
234 __put_user(env
->cr
[2], &sc
->cr2
);
236 __put_user(env
->regs
[R_EDI
], &sc
->rdi
);
237 __put_user(env
->regs
[R_ESI
], &sc
->rsi
);
238 __put_user(env
->regs
[R_EBP
], &sc
->rbp
);
239 __put_user(env
->regs
[R_ESP
], &sc
->rsp
);
240 __put_user(env
->regs
[R_EBX
], &sc
->rbx
);
241 __put_user(env
->regs
[R_EDX
], &sc
->rdx
);
242 __put_user(env
->regs
[R_ECX
], &sc
->rcx
);
243 __put_user(env
->regs
[R_EAX
], &sc
->rax
);
245 __put_user(env
->regs
[8], &sc
->r8
);
246 __put_user(env
->regs
[9], &sc
->r9
);
247 __put_user(env
->regs
[10], &sc
->r10
);
248 __put_user(env
->regs
[11], &sc
->r11
);
249 __put_user(env
->regs
[12], &sc
->r12
);
250 __put_user(env
->regs
[13], &sc
->r13
);
251 __put_user(env
->regs
[14], &sc
->r14
);
252 __put_user(env
->regs
[15], &sc
->r15
);
254 __put_user(cs
->exception_index
, &sc
->trapno
);
255 __put_user(env
->error_code
, &sc
->err
);
256 __put_user(env
->eip
, &sc
->rip
);
258 __put_user(env
->eflags
, &sc
->eflags
);
259 __put_user(env
->segs
[R_CS
].selector
, &sc
->cs
);
260 __put_user((uint16_t)0, &sc
->gs
);
261 __put_user((uint16_t)0, &sc
->fs
);
262 __put_user(env
->segs
[R_SS
].selector
, &sc
->ss
);
264 __put_user(mask
, &sc
->oldmask
);
265 __put_user(env
->cr
[2], &sc
->cr2
);
267 /* fpstate_addr must be 16 byte aligned for fxsave */
268 assert(!(fpstate_addr
& 0xf));
270 cpu_x86_fxsave(env
, fpstate_addr
);
271 __put_user(fpstate_addr
, &sc
->fpstate
);
276 * Determine which stack to use..
279 static inline abi_ulong
280 get_sigframe(struct target_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
284 /* Default to using normal stack */
285 esp
= get_sp_from_cpustate(env
);
287 esp
-= 128; /* this is the redzone */
290 /* This is the X/Open sanctioned signal stack switching. */
291 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
292 esp
= target_sigsp(esp
, ka
);
294 #ifndef TARGET_X86_64
295 /* This is the legacy signal stack switching. */
296 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
297 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
299 esp
= (unsigned long) ka
->sa_restorer
;
304 #ifndef TARGET_X86_64
305 return (esp
- frame_size
) & -8ul;
307 return ((esp
- frame_size
) & (~15ul)) - 8;
311 #ifndef TARGET_X86_64
312 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
313 void setup_frame(int sig
, struct target_sigaction
*ka
,
314 target_sigset_t
*set
, CPUX86State
*env
)
316 abi_ulong frame_addr
;
317 struct sigframe
*frame
;
320 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
321 trace_user_setup_frame(env
, frame_addr
);
323 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
326 __put_user(sig
, &frame
->sig
);
328 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0],
329 frame_addr
+ offsetof(struct sigframe
, fpstate
));
331 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
332 __put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]);
335 /* Set up to return from userspace. If provided, use a stub
336 already in userspace. */
337 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
338 __put_user(ka
->sa_restorer
, &frame
->pretcode
);
341 abi_ulong retcode_addr
;
342 retcode_addr
= frame_addr
+ offsetof(struct sigframe
, retcode
);
343 __put_user(retcode_addr
, &frame
->pretcode
);
344 /* This is popl %eax ; movl $,%eax ; int $0x80 */
346 __put_user(val16
, (uint16_t *)(frame
->retcode
+0));
347 __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
349 __put_user(val16
, (uint16_t *)(frame
->retcode
+6));
352 /* Set up registers for signal handler */
353 env
->regs
[R_ESP
] = frame_addr
;
354 env
->eip
= ka
->_sa_handler
;
356 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
357 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
358 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
359 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
360 env
->eflags
&= ~TF_MASK
;
362 unlock_user_struct(frame
, frame_addr
, 1);
371 /* compare linux/arch/x86/kernel/signal.c:setup_rt_frame() */
372 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
373 target_siginfo_t
*info
,
374 target_sigset_t
*set
, CPUX86State
*env
)
376 abi_ulong frame_addr
;
377 #ifndef TARGET_X86_64
380 struct rt_sigframe
*frame
;
383 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
384 trace_user_setup_rt_frame(env
, frame_addr
);
386 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
389 /* These fields are only in rt_sigframe on 32 bit */
390 #ifndef TARGET_X86_64
391 __put_user(sig
, &frame
->sig
);
392 addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
393 __put_user(addr
, &frame
->pinfo
);
394 addr
= frame_addr
+ offsetof(struct rt_sigframe
, uc
);
395 __put_user(addr
, &frame
->puc
);
397 if (ka
->sa_flags
& TARGET_SA_SIGINFO
) {
398 tswap_siginfo(&frame
->info
, info
);
401 /* Create the ucontext. */
402 __put_user(0, &frame
->uc
.tuc_flags
);
403 __put_user(0, &frame
->uc
.tuc_link
);
404 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
405 setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
, env
,
406 set
->sig
[0], frame_addr
+ offsetof(struct rt_sigframe
, fpstate
));
408 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
409 __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
412 /* Set up to return from userspace. If provided, use a stub
413 already in userspace. */
414 #ifndef TARGET_X86_64
415 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
416 __put_user(ka
->sa_restorer
, &frame
->pretcode
);
419 addr
= frame_addr
+ offsetof(struct rt_sigframe
, retcode
);
420 __put_user(addr
, &frame
->pretcode
);
421 /* This is movl $,%eax ; int $0x80 */
422 __put_user(0xb8, (char *)(frame
->retcode
+0));
423 __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
425 __put_user(val16
, (uint16_t *)(frame
->retcode
+5));
428 /* XXX: Would be slightly better to return -EFAULT here if test fails
429 assert(ka->sa_flags & TARGET_SA_RESTORER); */
430 __put_user(ka
->sa_restorer
, &frame
->pretcode
);
433 /* Set up registers for signal handler */
434 env
->regs
[R_ESP
] = frame_addr
;
435 env
->eip
= ka
->_sa_handler
;
437 #ifndef TARGET_X86_64
438 env
->regs
[R_EAX
] = sig
;
439 env
->regs
[R_EDX
] = (unsigned long)&frame
->info
;
440 env
->regs
[R_ECX
] = (unsigned long)&frame
->uc
;
442 env
->regs
[R_EAX
] = 0;
443 env
->regs
[R_EDI
] = sig
;
444 env
->regs
[R_ESI
] = (unsigned long)&frame
->info
;
445 env
->regs
[R_EDX
] = (unsigned long)&frame
->uc
;
448 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
449 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
450 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
451 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
452 env
->eflags
&= ~TF_MASK
;
454 unlock_user_struct(frame
, frame_addr
, 1);
463 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
)
465 unsigned int err
= 0;
466 abi_ulong fpstate_addr
;
467 unsigned int tmpflags
;
469 #ifndef TARGET_X86_64
470 cpu_x86_load_seg(env
, R_GS
, tswap16(sc
->gs
));
471 cpu_x86_load_seg(env
, R_FS
, tswap16(sc
->fs
));
472 cpu_x86_load_seg(env
, R_ES
, tswap16(sc
->es
));
473 cpu_x86_load_seg(env
, R_DS
, tswap16(sc
->ds
));
475 env
->regs
[R_EDI
] = tswapl(sc
->edi
);
476 env
->regs
[R_ESI
] = tswapl(sc
->esi
);
477 env
->regs
[R_EBP
] = tswapl(sc
->ebp
);
478 env
->regs
[R_ESP
] = tswapl(sc
->esp
);
479 env
->regs
[R_EBX
] = tswapl(sc
->ebx
);
480 env
->regs
[R_EDX
] = tswapl(sc
->edx
);
481 env
->regs
[R_ECX
] = tswapl(sc
->ecx
);
482 env
->regs
[R_EAX
] = tswapl(sc
->eax
);
484 env
->eip
= tswapl(sc
->eip
);
486 env
->regs
[8] = tswapl(sc
->r8
);
487 env
->regs
[9] = tswapl(sc
->r9
);
488 env
->regs
[10] = tswapl(sc
->r10
);
489 env
->regs
[11] = tswapl(sc
->r11
);
490 env
->regs
[12] = tswapl(sc
->r12
);
491 env
->regs
[13] = tswapl(sc
->r13
);
492 env
->regs
[14] = tswapl(sc
->r14
);
493 env
->regs
[15] = tswapl(sc
->r15
);
495 env
->regs
[R_EDI
] = tswapl(sc
->rdi
);
496 env
->regs
[R_ESI
] = tswapl(sc
->rsi
);
497 env
->regs
[R_EBP
] = tswapl(sc
->rbp
);
498 env
->regs
[R_EBX
] = tswapl(sc
->rbx
);
499 env
->regs
[R_EDX
] = tswapl(sc
->rdx
);
500 env
->regs
[R_EAX
] = tswapl(sc
->rax
);
501 env
->regs
[R_ECX
] = tswapl(sc
->rcx
);
502 env
->regs
[R_ESP
] = tswapl(sc
->rsp
);
504 env
->eip
= tswapl(sc
->rip
);
507 cpu_x86_load_seg(env
, R_CS
, lduw_p(&sc
->cs
) | 3);
508 cpu_x86_load_seg(env
, R_SS
, lduw_p(&sc
->ss
) | 3);
510 tmpflags
= tswapl(sc
->eflags
);
511 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
512 // regs->orig_eax = -1; /* disable syscall checks */
514 fpstate_addr
= tswapl(sc
->fpstate
);
515 if (fpstate_addr
!= 0) {
516 if (!access_ok(VERIFY_READ
, fpstate_addr
,
517 sizeof(struct target_fpstate
)))
519 #ifndef TARGET_X86_64
520 cpu_x86_frstor(env
, fpstate_addr
, 1);
522 cpu_x86_fxrstor(env
, fpstate_addr
);
531 /* Note: there is no sigreturn on x86_64, there is only rt_sigreturn */
532 #ifndef TARGET_X86_64
533 long do_sigreturn(CPUX86State
*env
)
535 struct sigframe
*frame
;
536 abi_ulong frame_addr
= env
->regs
[R_ESP
] - 8;
537 target_sigset_t target_set
;
541 trace_user_do_sigreturn(env
, frame_addr
);
542 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
544 /* set blocked signals */
545 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
546 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
547 __get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]);
550 target_to_host_sigset_internal(&set
, &target_set
);
553 /* restore registers */
554 if (restore_sigcontext(env
, &frame
->sc
))
556 unlock_user_struct(frame
, frame_addr
, 0);
557 return -TARGET_QEMU_ESIGRETURN
;
560 unlock_user_struct(frame
, frame_addr
, 0);
561 force_sig(TARGET_SIGSEGV
);
562 return -TARGET_QEMU_ESIGRETURN
;
566 long do_rt_sigreturn(CPUX86State
*env
)
568 abi_ulong frame_addr
;
569 struct rt_sigframe
*frame
;
572 frame_addr
= env
->regs
[R_ESP
] - sizeof(abi_ulong
);
573 trace_user_do_rt_sigreturn(env
, frame_addr
);
574 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
576 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
579 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
)) {
583 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe
, uc
.tuc_stack
), 0,
584 get_sp_from_cpustate(env
)) == -EFAULT
) {
588 unlock_user_struct(frame
, frame_addr
, 0);
589 return -TARGET_QEMU_ESIGRETURN
;
592 unlock_user_struct(frame
, frame_addr
, 0);
593 force_sig(TARGET_SIGSEGV
);
594 return -TARGET_QEMU_ESIGRETURN
;