2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames. ---*/
4 /*--- sigframe-amd64-linux.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2013 Nicholas Nethercote
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 The GNU General Public License is contained in the file COPYING.
32 #if defined(VGP_amd64_linux)
34 #include "pub_core_basics.h"
35 #include "pub_core_vki.h"
36 #include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy
37 #include "pub_core_threadstate.h"
38 #include "pub_core_aspacemgr.h"
39 #include "pub_core_libcbase.h"
40 #include "pub_core_libcassert.h"
41 #include "pub_core_libcprint.h"
42 #include "pub_core_machine.h"
43 #include "pub_core_options.h"
44 #include "pub_core_signals.h"
45 #include "pub_core_tooliface.h"
46 #include "pub_core_trampoline.h"
47 #include "pub_core_sigframe.h" /* self */
49 /* This module creates and removes signal frames for signal deliveries
52 Note, this file contains kernel-specific knowledge in the form of
53 'struct rt_sigframe'. How does that relate to the vki kernel
56 A 'struct rtsigframe' is pushed onto the client's stack. This
57 contains a subsidiary vki_ucontext. That holds the vcpu's state
58 across the signal, so that the sighandler can mess with the vcpu
59 state if it really wants.
61 FIXME: sigcontexting is basically broken for the moment. When
62 delivering a signal, the integer registers and %rflags are
63 correctly written into the sigcontext, however the FP and SSE state
64 is not. When returning from a signal, only the integer registers
65 are restored from the sigcontext; the rest of the CPU state is
66 restored to what it was before the signal.
72 /*------------------------------------------------------------*/
73 /*--- Signal frame layouts ---*/
74 /*------------------------------------------------------------*/
76 // A structure in which to save the application's registers
77 // during the execution of signal handlers.
79 // In theory, so long as we get the arguments to the handler function
80 // right, it doesn't matter what the exact layout of the rest of the
81 // frame is. Unfortunately, things like gcc's exception unwinding
82 // make assumptions about the locations of various parts of the frame,
83 // so we need to duplicate it exactly.
85 /* Valgrind-specific parts of the signal frame */
88 /* Sanity check word. */
91 UInt handlerflags
; /* flags for signal handler */
94 /* Safely-saved version of sigNo, as described above. */
97 /* XXX This is wrong. Surely we should store the shadow values
98 into the shadow memory behind the actual values? */
99 VexGuestAMD64State vex_shadow1
;
100 VexGuestAMD64State vex_shadow2
;
103 VexGuestAMD64State vex
;
106 /* saved signal mask to be restored when handler returns */
109 /* Sanity check word. Is the highest-addressed word; do not
116 /* Sig handler's return address */
120 struct vki_ucontext uContext
;
123 vki_siginfo_t sigInfo
;
124 struct _vki_fpstate fpstate
;
126 struct vg_sigframe vg
;
130 //:: /*------------------------------------------------------------*/
131 //:: /*--- Signal operations ---*/
132 //:: /*------------------------------------------------------------*/
135 //:: Great gobs of FP state conversion taken wholesale from
136 //:: linux/arch/i386/kernel/i387.c
140 //:: * FXSR floating point environment conversions.
142 //:: #define X86_FXSR_MAGIC 0x0000
145 //:: * FPU tag word conversions.
148 //:: static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
150 //:: unsigned int tmp; /* to avoid 16 bit prefixes in the code */
152 //:: /* Transform each pair of bits into 01 (valid) or 00 (empty) */
154 //:: tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
155 //:: /* and move the valid bits to the lower byte. */
156 //:: tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
157 //:: tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
158 //:: tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
162 //:: static unsigned long twd_fxsr_to_i387( const struct i387_fxsave_struct *fxsave )
164 //:: struct _vki_fpxreg *st = NULL;
165 //:: unsigned long twd = (unsigned long) fxsave->twd;
166 //:: unsigned long tag;
167 //:: unsigned long ret = 0xffff0000u;
170 //:: #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
172 //:: for ( i = 0 ; i < 8 ; i++ ) {
173 //:: if ( twd & 0x1 ) {
174 //:: st = (struct _vki_fpxreg *) FPREG_ADDR( fxsave, i );
176 //:: switch ( st->exponent & 0x7fff ) {
178 //:: tag = 2; /* Special */
181 //:: if ( !st->significand[0] &&
182 //:: !st->significand[1] &&
183 //:: !st->significand[2] &&
184 //:: !st->significand[3] ) {
185 //:: tag = 1; /* Zero */
187 //:: tag = 2; /* Special */
191 //:: if ( st->significand[3] & 0x8000 ) {
192 //:: tag = 0; /* Valid */
194 //:: tag = 2; /* Special */
199 //:: tag = 3; /* Empty */
201 //:: ret |= (tag << (2 * i));
207 //:: static void convert_fxsr_to_user( struct _vki_fpstate *buf,
208 //:: const struct i387_fxsave_struct *fxsave )
210 //:: unsigned long env[7];
211 //:: struct _vki_fpreg *to;
212 //:: struct _vki_fpxreg *from;
215 //:: env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
216 //:: env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
217 //:: env[2] = twd_fxsr_to_i387(fxsave);
218 //:: env[3] = fxsave->fip;
219 //:: env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
220 //:: env[5] = fxsave->foo;
221 //:: env[6] = fxsave->fos;
223 //:: VG_(memcpy)(buf, env, 7 * sizeof(unsigned long));
225 //:: to = &buf->_st[0];
226 //:: from = (struct _vki_fpxreg *) &fxsave->st_space[0];
227 //:: for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
228 //:: unsigned long __user *t = (unsigned long __user *)to;
229 //:: unsigned long *f = (unsigned long *)from;
233 //:: to->exponent = from->exponent;
237 //:: static void convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
238 //:: const struct _vki_fpstate *buf )
240 //:: unsigned long env[7];
241 //:: struct _vki_fpxreg *to;
242 //:: const struct _vki_fpreg *from;
245 //:: VG_(memcpy)(env, buf, 7 * sizeof(long));
247 //:: fxsave->cwd = (unsigned short)(env[0] & 0xffff);
248 //:: fxsave->swd = (unsigned short)(env[1] & 0xffff);
249 //:: fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
250 //:: fxsave->fip = env[3];
251 //:: fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
252 //:: fxsave->fcs = (env[4] & 0xffff);
253 //:: fxsave->foo = env[5];
254 //:: fxsave->fos = env[6];
256 //:: to = (struct _vki_fpxreg *) &fxsave->st_space[0];
257 //:: from = &buf->_st[0];
258 //:: for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
259 //:: unsigned long *t = (unsigned long *)to;
260 //:: unsigned long __user *f = (unsigned long __user *)from;
264 //:: to->exponent = from->exponent;
268 //:: static inline void save_i387_fsave( arch_thread_t *regs, struct _vki_fpstate *buf )
270 //:: struct i387_fsave_struct *fs = ®s->m_sse.fsave;
272 //:: fs->status = fs->swd;
273 //:: VG_(memcpy)(buf, fs, sizeof(*fs));
276 //:: static void save_i387_fxsave( arch_thread_t *regs, struct _vki_fpstate *buf )
278 //:: const struct i387_fxsave_struct *fx = ®s->m_sse.fxsave;
279 //:: convert_fxsr_to_user( buf, fx );
281 //:: buf->status = fx->swd;
282 //:: buf->magic = X86_FXSR_MAGIC;
283 //:: VG_(memcpy)(buf->_fxsr_env, fx, sizeof(struct i387_fxsave_struct));
286 //:: static void save_i387( arch_thread_t *regs, struct _vki_fpstate *buf )
288 //:: if ( VG_(have_ssestate) )
289 //:: save_i387_fxsave( regs, buf );
291 //:: save_i387_fsave( regs, buf );
294 //:: static inline void restore_i387_fsave( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
296 //:: VG_(memcpy)( ®s->m_sse.fsave, buf, sizeof(struct i387_fsave_struct) );
299 //:: static void restore_i387_fxsave( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
301 //:: VG_(memcpy)(®s->m_sse.fxsave, &buf->_fxsr_env[0],
302 //:: sizeof(struct i387_fxsave_struct) );
303 //:: /* mxcsr reserved bits must be masked to zero for security reasons */
304 //:: regs->m_sse.fxsave.mxcsr &= 0xffbf;
305 //:: convert_fxsr_from_user( ®s->m_sse.fxsave, buf );
308 //:: static void restore_i387( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
310 //:: if ( VG_(have_ssestate) ) {
311 //:: restore_i387_fxsave( regs, buf );
313 //:: restore_i387_fsave( regs, buf );
318 /*------------------------------------------------------------*/
319 /*--- Creating signal frames ---*/
320 /*------------------------------------------------------------*/
322 /* Create a plausible-looking sigcontext from the thread's
323 Vex guest state. NOTE: does not fill in the FP or SSE
324 bits of sigcontext at the moment.
327 void synth_ucontext(ThreadId tid
, const vki_siginfo_t
*si
,
328 UWord trapno
, UWord err
, const vki_sigset_t
*set
,
329 struct vki_ucontext
*uc
, struct _vki_fpstate
*fpstate
)
331 ThreadState
*tst
= VG_(get_ThreadState
)(tid
);
332 struct vki_sigcontext
*sc
= &uc
->uc_mcontext
;
334 VG_(memset
)(uc
, 0, sizeof(*uc
));
338 uc
->uc_sigmask
= *set
;
339 uc
->uc_stack
= tst
->altstack
;
340 sc
->fpstate
= fpstate
;
342 // FIXME: save_i387(&tst->arch, fpstate);
344 # define SC2(reg,REG) sc->reg = tst->arch.vex.guest_##REG
363 sc
->eflags
= LibVEX_GuestAMD64_get_rflags(&tst
->arch
.vex
);
364 // FIXME: SC2(cs,CS);
365 // FIXME: SC2(gs,GS);
366 // FIXME: SC2(fs,FS);
371 sc
->cr2
= (UWord
)si
->_sifields
._sigfault
._addr
;
375 /* Extend the stack segment downwards if needed so as to ensure the
376 new signal frames are mapped to something. Return a Bool
377 indicating whether or not the operation was successful.
379 static Bool
extend ( ThreadState
*tst
, Addr addr
, SizeT size
)
381 ThreadId tid
= tst
->tid
;
382 NSegment
const* stackseg
= NULL
;
384 if (VG_(extend_stack
)(addr
, tst
->client_stack_szB
)) {
385 stackseg
= VG_(am_find_nsegment
)(addr
);
387 VG_(printf
)("frame=%#lx seg=%#lx-%#lx\n",
388 addr
, stackseg
->start
, stackseg
->end
);
391 if (stackseg
== NULL
|| !stackseg
->hasR
|| !stackseg
->hasW
) {
394 "Can't extend stack to %#lx during signal delivery for thread %d:\n",
396 if (stackseg
== NULL
)
397 VG_(message
)(Vg_UserMsg
, " no stack segment\n");
399 VG_(message
)(Vg_UserMsg
, " too small or bad protection modes\n");
401 /* set SIGSEGV to default handler */
402 VG_(set_default_handler
)(VKI_SIGSEGV
);
403 VG_(synth_fault_mapping
)(tid
, addr
);
405 /* The whole process should be about to die, since the default
406 action of SIGSEGV to kill the whole process. */
410 /* For tracking memory events, indicate the entire frame has been
412 VG_TRACK( new_mem_stack_signal
, addr
- VG_STACK_REDZONE_SZB
,
413 size
+ VG_STACK_REDZONE_SZB
, tid
);
419 /* Build the Valgrind-specific part of a signal frame. */
421 static void build_vg_sigframe(struct vg_sigframe
*frame
,
423 const vki_sigset_t
*mask
,
427 frame
->sigNo_private
= sigNo
;
428 frame
->magicPI
= 0x31415927;
429 frame
->vex_shadow1
= tst
->arch
.vex_shadow1
;
430 frame
->vex_shadow2
= tst
->arch
.vex_shadow2
;
432 frame
->vex
= tst
->arch
.vex
;
434 frame
->mask
= tst
->sig_mask
;
435 frame
->handlerflags
= flags
;
436 frame
->magicE
= 0x27182818;
440 static Addr
build_rt_sigframe(ThreadState
*tst
,
441 Addr rsp_top_of_frame
,
442 const vki_siginfo_t
*siginfo
,
443 const struct vki_ucontext
*siguc
,
444 void *handler
, UInt flags
,
445 const vki_sigset_t
*mask
,
448 struct rt_sigframe
*frame
;
449 Addr rsp
= rsp_top_of_frame
;
450 Int sigNo
= siginfo
->si_signo
;
454 rsp
-= sizeof(*frame
);
455 rsp
= VG_ROUNDDN(rsp
, 16) - 8;
456 frame
= (struct rt_sigframe
*)rsp
;
458 if (!extend(tst
, rsp
, sizeof(*frame
)))
459 return rsp_top_of_frame
;
461 /* retaddr, siginfo, uContext fields are to be written */
462 VG_TRACK( pre_mem_write
, Vg_CoreSignal
, tst
->tid
, "rt signal handler frame",
463 rsp
, offsetof(struct rt_sigframe
, vg
) );
465 if (flags
& VKI_SA_RESTORER
)
466 frame
->retaddr
= (Addr
)restorer
;
468 frame
->retaddr
= (Addr
)&VG_(amd64_linux_SUBST_FOR_rt_sigreturn
);
471 trapno
= siguc
->uc_mcontext
.trapno
;
472 err
= siguc
->uc_mcontext
.err
;
478 VG_(memcpy
)(&frame
->sigInfo
, siginfo
, sizeof(vki_siginfo_t
));
480 /* SIGILL defines addr to be the faulting address */
481 if (sigNo
== VKI_SIGILL
&& siginfo
->si_code
> 0)
482 frame
->sigInfo
._sifields
._sigfault
._addr
483 = (void*)tst
->arch
.vex
.guest_RIP
;
485 synth_ucontext(tst
->tid
, siginfo
, trapno
, err
, mask
,
486 &frame
->uContext
, &frame
->fpstate
);
488 VG_TRACK( post_mem_write
, Vg_CoreSignal
, tst
->tid
,
489 rsp
, offsetof(struct rt_sigframe
, vg
) );
491 build_vg_sigframe(&frame
->vg
, tst
, mask
, flags
, sigNo
);
497 void VG_(sigframe_create
)( ThreadId tid
,
498 Addr rsp_top_of_frame
,
499 const vki_siginfo_t
*siginfo
,
500 const struct vki_ucontext
*siguc
,
503 const vki_sigset_t
*mask
,
507 struct rt_sigframe
*frame
;
508 ThreadState
* tst
= VG_(get_ThreadState
)(tid
);
510 rsp
= build_rt_sigframe(tst
, rsp_top_of_frame
, siginfo
, siguc
,
511 handler
, flags
, mask
, restorer
);
512 frame
= (struct rt_sigframe
*)rsp
;
514 /* Set the thread so it will next run the handler. */
515 /* tst->m_rsp = rsp; also notify the tool we've updated RSP */
516 VG_(set_SP
)(tid
, rsp
);
517 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tid
, VG_O_STACK_PTR
, sizeof(Addr
));
519 //VG_(printf)("handler = %p\n", handler);
520 tst
->arch
.vex
.guest_RIP
= (Addr
) handler
;
521 tst
->arch
.vex
.guest_RDI
= (ULong
) siginfo
->si_signo
;
522 tst
->arch
.vex
.guest_RSI
= (Addr
) &frame
->sigInfo
;
523 tst
->arch
.vex
.guest_RDX
= (Addr
) &frame
->uContext
;
524 /* And tell the tool that these registers have been written. */
525 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
526 offsetof(VexGuestAMD64State
,guest_RIP
), sizeof(UWord
) );
527 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
528 offsetof(VexGuestAMD64State
,guest_RDI
), sizeof(UWord
) );
529 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
530 offsetof(VexGuestAMD64State
,guest_RSI
), sizeof(UWord
) );
531 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
532 offsetof(VexGuestAMD64State
,guest_RDX
), sizeof(UWord
) );
534 /* This thread needs to be marked runnable, but we leave that the
538 VG_(printf
)("pushed signal frame; %%RSP now = %#lx, "
539 "next %%RIP = %#llx, status=%d\n",
540 rsp
, tst
->arch
.vex
.guest_RIP
, tst
->status
);
544 /*------------------------------------------------------------*/
545 /*--- Destroying signal frames ---*/
546 /*------------------------------------------------------------*/
548 /* Return False and don't do anything, just set the client to take a
549 segfault, if it looks like the frame is corrupted. */
551 Bool
restore_vg_sigframe ( ThreadState
*tst
,
552 struct vg_sigframe
*frame
, Int
*sigNo
)
554 if (frame
->magicPI
!= 0x31415927 ||
555 frame
->magicE
!= 0x27182818) {
556 VG_(message
)(Vg_UserMsg
, "Thread %d return signal frame "
557 "corrupted. Killing process.\n",
559 VG_(set_default_handler
)(VKI_SIGSEGV
);
560 VG_(synth_fault
)(tst
->tid
);
561 *sigNo
= VKI_SIGSEGV
;
564 tst
->sig_mask
= frame
->mask
;
565 tst
->tmp_sig_mask
= frame
->mask
;
566 tst
->arch
.vex_shadow1
= frame
->vex_shadow1
;
567 tst
->arch
.vex_shadow2
= frame
->vex_shadow2
;
569 tst
->arch
.vex
= frame
->vex
;
571 *sigNo
= frame
->sigNo_private
;
576 void restore_sigcontext( ThreadState
*tst
,
577 struct vki_sigcontext
*sc
,
578 struct _vki_fpstate
*fpstate
)
580 tst
->arch
.vex
.guest_RAX
= sc
->rax
;
581 tst
->arch
.vex
.guest_RCX
= sc
->rcx
;
582 tst
->arch
.vex
.guest_RDX
= sc
->rdx
;
583 tst
->arch
.vex
.guest_RBX
= sc
->rbx
;
584 tst
->arch
.vex
.guest_RBP
= sc
->rbp
;
585 tst
->arch
.vex
.guest_RSP
= sc
->rsp
;
586 tst
->arch
.vex
.guest_RSI
= sc
->rsi
;
587 tst
->arch
.vex
.guest_RDI
= sc
->rdi
;
588 tst
->arch
.vex
.guest_R8
= sc
->r8
;
589 tst
->arch
.vex
.guest_R9
= sc
->r9
;
590 tst
->arch
.vex
.guest_R10
= sc
->r10
;
591 tst
->arch
.vex
.guest_R11
= sc
->r11
;
592 tst
->arch
.vex
.guest_R12
= sc
->r12
;
593 tst
->arch
.vex
.guest_R13
= sc
->r13
;
594 tst
->arch
.vex
.guest_R14
= sc
->r14
;
595 tst
->arch
.vex
.guest_R15
= sc
->r15
;
596 //:: tst->arch.vex.guest_rflags = sc->rflags;
597 tst
->arch
.vex
.guest_RIP
= sc
->rip
;
599 //:: tst->arch.vex.guest_CS = sc->cs;
600 //:: tst->arch.vex.guest_FS = sc->fs;
601 //:: tst->arch.vex.guest_GS = sc->gs;
603 //:: restore_i387(&tst->arch, fpstate);
608 SizeT
restore_rt_sigframe ( ThreadState
*tst
,
609 struct rt_sigframe
*frame
, Int
*sigNo
)
611 if (restore_vg_sigframe(tst
, &frame
->vg
, sigNo
))
612 restore_sigcontext(tst
, &frame
->uContext
.uc_mcontext
, &frame
->fpstate
);
614 return sizeof(*frame
);
618 void VG_(sigframe_destroy
)( ThreadId tid
, Bool isRT
)
627 tst
= VG_(get_ThreadState
)(tid
);
629 /* Correctly reestablish the frame base address. */
630 rsp
= tst
->arch
.vex
.guest_RSP
;
632 size
= restore_rt_sigframe(tst
, (struct rt_sigframe
*)rsp
, &sigNo
);
634 VG_TRACK( die_mem_stack_signal
, rsp
- VG_STACK_REDZONE_SZB
,
635 size
+ VG_STACK_REDZONE_SZB
);
637 if (VG_(clo_trace_signals
))
640 "VG_(signal_return) (thread %d): isRT=%d valid magic; RIP=%#llx\n",
641 tid
, isRT
, tst
->arch
.vex
.guest_RIP
);
644 VG_TRACK( post_deliver_signal
, tid
, sigNo
);
647 #endif // defined(VGP_amd64_linux)
649 /*--------------------------------------------------------------------*/
651 /*--------------------------------------------------------------------*/