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-2017 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, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #if defined(VGP_amd64_linux)
32 #include "pub_core_basics.h"
33 #include "pub_core_vki.h"
34 #include "pub_core_threadstate.h"
35 #include "pub_core_aspacemgr.h"
36 #include "pub_core_libcbase.h"
37 #include "pub_core_libcassert.h"
38 #include "pub_core_libcprint.h"
39 #include "pub_core_machine.h"
40 #include "pub_core_options.h"
41 #include "pub_core_signals.h"
42 #include "pub_core_tooliface.h"
43 #include "pub_core_trampoline.h"
44 #include "pub_core_sigframe.h" /* self */
45 #include "priv_sigframe.h"
47 /* This module creates and removes signal frames for signal deliveries
50 Note, this file contains kernel-specific knowledge in the form of
51 'struct rt_sigframe'. How does that relate to the vki kernel
54 A 'struct rtsigframe' is pushed onto the client's stack. This
55 contains a subsidiary vki_ucontext. That holds the vcpu's state
56 across the signal, so that the sighandler can mess with the vcpu
57 state if it really wants.
59 FIXME: sigcontexting is basically broken for the moment. When
60 delivering a signal, the integer registers and %rflags are
61 correctly written into the sigcontext, however the FP and SSE state
62 is not. When returning from a signal, only the integer registers
63 are restored from the sigcontext; the rest of the CPU state is
64 restored to what it was before the signal.
70 /*------------------------------------------------------------*/
71 /*--- Signal frame layouts ---*/
72 /*------------------------------------------------------------*/
74 // A structure in which to save the application's registers
75 // during the execution of signal handlers.
77 // In theory, so long as we get the arguments to the handler function
78 // right, it doesn't matter what the exact layout of the rest of the
79 // frame is. Unfortunately, things like gcc's exception unwinding
80 // make assumptions about the locations of various parts of the frame,
81 // so we need to duplicate it exactly.
83 /* Valgrind-specific parts of the signal frame */
86 /* Sanity check word. */
89 UInt handlerflags
; /* flags for signal handler */
92 /* Safely-saved version of sigNo, as described above. */
95 /* XXX This is wrong. Surely we should store the shadow values
96 into the shadow memory behind the actual values? */
97 VexGuestAMD64State vex_shadow1
;
98 VexGuestAMD64State vex_shadow2
;
101 VexGuestAMD64State vex
;
104 /* saved signal mask to be restored when handler returns */
107 /* Sanity check word. Is the highest-addressed word; do not
114 /* Sig handler's return address */
118 struct vki_ucontext uContext
;
121 vki_siginfo_t sigInfo
;
122 struct _vki_fpstate fpstate
;
124 struct vg_sigframe vg
;
128 //:: /*------------------------------------------------------------*/
129 //:: /*--- Signal operations ---*/
130 //:: /*------------------------------------------------------------*/
133 //:: Great gobs of FP state conversion taken wholesale from
134 //:: linux/arch/i386/kernel/i387.c
138 //:: * FXSR floating point environment conversions.
140 //:: #define X86_FXSR_MAGIC 0x0000
143 //:: * FPU tag word conversions.
146 //:: static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
148 //:: unsigned int tmp; /* to avoid 16 bit prefixes in the code */
150 //:: /* Transform each pair of bits into 01 (valid) or 00 (empty) */
152 //:: tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
153 //:: /* and move the valid bits to the lower byte. */
154 //:: tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
155 //:: tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
156 //:: tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
160 //:: static unsigned long twd_fxsr_to_i387( const struct i387_fxsave_struct *fxsave )
162 //:: struct _vki_fpxreg *st = NULL;
163 //:: unsigned long twd = (unsigned long) fxsave->twd;
164 //:: unsigned long tag;
165 //:: unsigned long ret = 0xffff0000u;
168 //:: #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
170 //:: for ( i = 0 ; i < 8 ; i++ ) {
171 //:: if ( twd & 0x1 ) {
172 //:: st = (struct _vki_fpxreg *) FPREG_ADDR( fxsave, i );
174 //:: switch ( st->exponent & 0x7fff ) {
176 //:: tag = 2; /* Special */
179 //:: if ( !st->significand[0] &&
180 //:: !st->significand[1] &&
181 //:: !st->significand[2] &&
182 //:: !st->significand[3] ) {
183 //:: tag = 1; /* Zero */
185 //:: tag = 2; /* Special */
189 //:: if ( st->significand[3] & 0x8000 ) {
190 //:: tag = 0; /* Valid */
192 //:: tag = 2; /* Special */
197 //:: tag = 3; /* Empty */
199 //:: ret |= (tag << (2 * i));
205 //:: static void convert_fxsr_to_user( struct _vki_fpstate *buf,
206 //:: const struct i387_fxsave_struct *fxsave )
208 //:: unsigned long env[7];
209 //:: struct _vki_fpreg *to;
210 //:: struct _vki_fpxreg *from;
213 //:: env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
214 //:: env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
215 //:: env[2] = twd_fxsr_to_i387(fxsave);
216 //:: env[3] = fxsave->fip;
217 //:: env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
218 //:: env[5] = fxsave->foo;
219 //:: env[6] = fxsave->fos;
221 //:: VG_(memcpy)(buf, env, 7 * sizeof(unsigned long));
223 //:: to = &buf->_st[0];
224 //:: from = (struct _vki_fpxreg *) &fxsave->st_space[0];
225 //:: for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
226 //:: unsigned long __user *t = (unsigned long __user *)to;
227 //:: unsigned long *f = (unsigned long *)from;
231 //:: to->exponent = from->exponent;
235 //:: static void convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
236 //:: const struct _vki_fpstate *buf )
238 //:: unsigned long env[7];
239 //:: struct _vki_fpxreg *to;
240 //:: const struct _vki_fpreg *from;
243 //:: VG_(memcpy)(env, buf, 7 * sizeof(long));
245 //:: fxsave->cwd = (unsigned short)(env[0] & 0xffff);
246 //:: fxsave->swd = (unsigned short)(env[1] & 0xffff);
247 //:: fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
248 //:: fxsave->fip = env[3];
249 //:: fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
250 //:: fxsave->fcs = (env[4] & 0xffff);
251 //:: fxsave->foo = env[5];
252 //:: fxsave->fos = env[6];
254 //:: to = (struct _vki_fpxreg *) &fxsave->st_space[0];
255 //:: from = &buf->_st[0];
256 //:: for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
257 //:: unsigned long *t = (unsigned long *)to;
258 //:: unsigned long __user *f = (unsigned long __user *)from;
262 //:: to->exponent = from->exponent;
266 //:: static inline void save_i387_fsave( arch_thread_t *regs, struct _vki_fpstate *buf )
268 //:: struct i387_fsave_struct *fs = ®s->m_sse.fsave;
270 //:: fs->status = fs->swd;
271 //:: VG_(memcpy)(buf, fs, sizeof(*fs));
274 //:: static void save_i387_fxsave( arch_thread_t *regs, struct _vki_fpstate *buf )
276 //:: const struct i387_fxsave_struct *fx = ®s->m_sse.fxsave;
277 //:: convert_fxsr_to_user( buf, fx );
279 //:: buf->status = fx->swd;
280 //:: buf->magic = X86_FXSR_MAGIC;
281 //:: VG_(memcpy)(buf->_fxsr_env, fx, sizeof(struct i387_fxsave_struct));
284 //:: static void save_i387( arch_thread_t *regs, struct _vki_fpstate *buf )
286 //:: if ( VG_(have_ssestate) )
287 //:: save_i387_fxsave( regs, buf );
289 //:: save_i387_fsave( regs, buf );
292 //:: static inline void restore_i387_fsave( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
294 //:: VG_(memcpy)( ®s->m_sse.fsave, buf, sizeof(struct i387_fsave_struct) );
297 //:: static void restore_i387_fxsave( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
299 //:: VG_(memcpy)(®s->m_sse.fxsave, &buf->_fxsr_env[0],
300 //:: sizeof(struct i387_fxsave_struct) );
301 //:: /* mxcsr reserved bits must be masked to zero for security reasons */
302 //:: regs->m_sse.fxsave.mxcsr &= 0xffbf;
303 //:: convert_fxsr_from_user( ®s->m_sse.fxsave, buf );
306 //:: static void restore_i387( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
308 //:: if ( VG_(have_ssestate) ) {
309 //:: restore_i387_fxsave( regs, buf );
311 //:: restore_i387_fsave( regs, buf );
316 /*------------------------------------------------------------*/
317 /*--- Creating signal frames ---*/
318 /*------------------------------------------------------------*/
320 /* Create a plausible-looking sigcontext from the thread's
321 Vex guest state. NOTE: does not fill in the FP or SSE
322 bits of sigcontext at the moment.
325 void synth_ucontext(ThreadId tid
, const vki_siginfo_t
*si
,
326 UWord trapno
, UWord err
, const vki_sigset_t
*set
,
327 struct vki_ucontext
*uc
, struct _vki_fpstate
*fpstate
)
329 ThreadState
*tst
= VG_(get_ThreadState
)(tid
);
330 struct vki_sigcontext
*sc
= &uc
->uc_mcontext
;
332 VG_(memset
)(uc
, 0, sizeof(*uc
));
336 uc
->uc_sigmask
= *set
;
337 uc
->uc_stack
= tst
->altstack
;
338 sc
->fpstate
= fpstate
;
340 // FIXME: save_i387(&tst->arch, fpstate);
342 # define SC2(reg,REG) sc->reg = tst->arch.vex.guest_##REG
361 sc
->eflags
= LibVEX_GuestAMD64_get_rflags(&tst
->arch
.vex
);
362 // FIXME: SC2(cs,CS);
363 // FIXME: SC2(gs,GS);
364 // FIXME: SC2(fs,FS);
369 sc
->cr2
= (UWord
)si
->_sifields
._sigfault
._addr
;
373 /* Build the Valgrind-specific part of a signal frame. */
375 static void build_vg_sigframe(struct vg_sigframe
*frame
,
377 const vki_sigset_t
*mask
,
381 frame
->sigNo_private
= sigNo
;
382 frame
->magicPI
= 0x31415927;
383 frame
->vex_shadow1
= tst
->arch
.vex_shadow1
;
384 frame
->vex_shadow2
= tst
->arch
.vex_shadow2
;
386 frame
->vex
= tst
->arch
.vex
;
388 frame
->mask
= tst
->sig_mask
;
389 frame
->handlerflags
= flags
;
390 frame
->magicE
= 0x27182818;
394 static Addr
build_rt_sigframe(ThreadState
*tst
,
395 Addr rsp_top_of_frame
,
396 const vki_siginfo_t
*siginfo
,
397 const struct vki_ucontext
*siguc
,
398 void *handler
, UInt flags
,
399 const vki_sigset_t
*mask
,
402 struct rt_sigframe
*frame
;
403 Addr rsp
= rsp_top_of_frame
;
404 Int sigNo
= siginfo
->si_signo
;
408 rsp
-= sizeof(*frame
);
409 rsp
= VG_ROUNDDN(rsp
, 16) - 8;
410 frame
= (struct rt_sigframe
*)rsp
;
412 if (! ML_(sf_maybe_extend_stack
)(tst
, rsp
, sizeof(*frame
), flags
))
413 return rsp_top_of_frame
;
415 /* retaddr, siginfo, uContext fields are to be written */
416 VG_TRACK( pre_mem_write
, Vg_CoreSignal
, tst
->tid
, "rt signal handler frame",
417 rsp
, offsetof(struct rt_sigframe
, vg
) );
419 if (flags
& VKI_SA_RESTORER
)
420 frame
->retaddr
= (Addr
)restorer
;
422 frame
->retaddr
= (Addr
)&VG_(amd64_linux_SUBST_FOR_rt_sigreturn
);
425 trapno
= siguc
->uc_mcontext
.trapno
;
426 err
= siguc
->uc_mcontext
.err
;
432 VG_(memcpy
)(&frame
->sigInfo
, siginfo
, sizeof(vki_siginfo_t
));
434 /* SIGILL defines addr to be the faulting address */
435 if (sigNo
== VKI_SIGILL
&& siginfo
->si_code
> 0)
436 frame
->sigInfo
._sifields
._sigfault
._addr
437 = (void*)tst
->arch
.vex
.guest_RIP
;
439 synth_ucontext(tst
->tid
, siginfo
, trapno
, err
, mask
,
440 &frame
->uContext
, &frame
->fpstate
);
442 VG_TRACK( post_mem_write
, Vg_CoreSignal
, tst
->tid
,
443 rsp
, offsetof(struct rt_sigframe
, vg
) );
445 build_vg_sigframe(&frame
->vg
, tst
, mask
, flags
, sigNo
);
451 void VG_(sigframe_create
)( ThreadId tid
,
453 Addr rsp_top_of_frame
,
454 const vki_siginfo_t
*siginfo
,
455 const struct vki_ucontext
*siguc
,
458 const vki_sigset_t
*mask
,
462 struct rt_sigframe
*frame
;
463 ThreadState
* tst
= VG_(get_ThreadState
)(tid
);
465 rsp
= build_rt_sigframe(tst
, rsp_top_of_frame
, siginfo
, siguc
,
466 handler
, flags
, mask
, restorer
);
467 frame
= (struct rt_sigframe
*)rsp
;
469 /* Set the thread so it will next run the handler. */
470 /* tst->m_rsp = rsp; also notify the tool we've updated RSP */
471 VG_(set_SP
)(tid
, rsp
);
472 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tid
, VG_O_STACK_PTR
, sizeof(Addr
));
474 //VG_(printf)("handler = %p\n", handler);
475 tst
->arch
.vex
.guest_RIP
= (Addr
) handler
;
476 tst
->arch
.vex
.guest_RDI
= (ULong
) siginfo
->si_signo
;
477 tst
->arch
.vex
.guest_RSI
= (Addr
) &frame
->sigInfo
;
478 tst
->arch
.vex
.guest_RDX
= (Addr
) &frame
->uContext
;
479 /* And tell the tool that these registers have been written. */
480 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
481 offsetof(VexGuestAMD64State
,guest_RIP
), sizeof(UWord
) );
482 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
483 offsetof(VexGuestAMD64State
,guest_RDI
), sizeof(UWord
) );
484 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
485 offsetof(VexGuestAMD64State
,guest_RSI
), sizeof(UWord
) );
486 VG_TRACK( post_reg_write
, Vg_CoreSignal
, tst
->tid
,
487 offsetof(VexGuestAMD64State
,guest_RDX
), sizeof(UWord
) );
489 /* This thread needs to be marked runnable, but we leave that the
493 VG_(printf
)("pushed signal frame; %%RSP now = %#lx, "
494 "next %%RIP = %#llx, status=%d\n",
495 rsp
, tst
->arch
.vex
.guest_RIP
, (Int
)tst
->status
);
499 /*------------------------------------------------------------*/
500 /*--- Destroying signal frames ---*/
501 /*------------------------------------------------------------*/
503 /* Return False and don't do anything, just set the client to take a
504 segfault, if it looks like the frame is corrupted. */
506 Bool
restore_vg_sigframe ( ThreadState
*tst
,
507 struct vg_sigframe
*frame
, Int
*sigNo
)
509 if (frame
->magicPI
!= 0x31415927 ||
510 frame
->magicE
!= 0x27182818) {
511 VG_(message
)(Vg_UserMsg
, "Thread %u return signal frame "
512 "corrupted. Killing process.\n",
514 VG_(set_default_handler
)(VKI_SIGSEGV
);
515 VG_(synth_fault
)(tst
->tid
);
516 *sigNo
= VKI_SIGSEGV
;
519 tst
->sig_mask
= frame
->mask
;
520 tst
->tmp_sig_mask
= frame
->mask
;
521 tst
->arch
.vex_shadow1
= frame
->vex_shadow1
;
522 tst
->arch
.vex_shadow2
= frame
->vex_shadow2
;
524 tst
->arch
.vex
= frame
->vex
;
526 *sigNo
= frame
->sigNo_private
;
531 void restore_sigcontext( ThreadState
*tst
,
532 struct vki_sigcontext
*sc
,
533 struct _vki_fpstate
*fpstate
)
535 tst
->arch
.vex
.guest_RAX
= sc
->rax
;
536 tst
->arch
.vex
.guest_RCX
= sc
->rcx
;
537 tst
->arch
.vex
.guest_RDX
= sc
->rdx
;
538 tst
->arch
.vex
.guest_RBX
= sc
->rbx
;
539 tst
->arch
.vex
.guest_RBP
= sc
->rbp
;
540 tst
->arch
.vex
.guest_RSP
= sc
->rsp
;
541 tst
->arch
.vex
.guest_RSI
= sc
->rsi
;
542 tst
->arch
.vex
.guest_RDI
= sc
->rdi
;
543 tst
->arch
.vex
.guest_R8
= sc
->r8
;
544 tst
->arch
.vex
.guest_R9
= sc
->r9
;
545 tst
->arch
.vex
.guest_R10
= sc
->r10
;
546 tst
->arch
.vex
.guest_R11
= sc
->r11
;
547 tst
->arch
.vex
.guest_R12
= sc
->r12
;
548 tst
->arch
.vex
.guest_R13
= sc
->r13
;
549 tst
->arch
.vex
.guest_R14
= sc
->r14
;
550 tst
->arch
.vex
.guest_R15
= sc
->r15
;
551 //:: tst->arch.vex.guest_rflags = sc->rflags;
552 tst
->arch
.vex
.guest_RIP
= sc
->rip
;
554 //:: tst->arch.vex.guest_CS = sc->cs;
555 //:: tst->arch.vex.guest_FS = sc->fs;
556 //:: tst->arch.vex.guest_GS = sc->gs;
558 //:: restore_i387(&tst->arch, fpstate);
563 SizeT
restore_rt_sigframe ( ThreadState
*tst
,
564 struct rt_sigframe
*frame
, Int
*sigNo
)
566 if (restore_vg_sigframe(tst
, &frame
->vg
, sigNo
))
567 restore_sigcontext(tst
, &frame
->uContext
.uc_mcontext
, &frame
->fpstate
);
569 return sizeof(*frame
);
573 void VG_(sigframe_destroy
)( ThreadId tid
, Bool isRT
)
582 tst
= VG_(get_ThreadState
)(tid
);
584 /* Correctly reestablish the frame base address. */
585 rsp
= tst
->arch
.vex
.guest_RSP
;
587 size
= restore_rt_sigframe(tst
, (struct rt_sigframe
*)rsp
, &sigNo
);
589 VG_TRACK( die_mem_stack_signal
, rsp
- VG_STACK_REDZONE_SZB
,
590 size
+ VG_STACK_REDZONE_SZB
);
592 if (VG_(clo_trace_signals
))
595 "VG_(sigframe_destroy) (thread %u): isRT=%d valid magic; RIP=%#llx\n",
596 tid
, isRT
, tst
->arch
.vex
.guest_RIP
);
599 VG_TRACK( post_deliver_signal
, tid
, sigNo
);
602 #endif // defined(VGP_amd64_linux)
604 /*--------------------------------------------------------------------*/
606 /*--------------------------------------------------------------------*/