Add 469782 to NEWS
[valgrind.git] / coregrind / m_sigframe / sigframe-amd64-linux.c
blob0e24306096c7577f959b5c38c4af143e0f77a93d
2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames. ---*/
4 /*--- sigframe-amd64-linux.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2000-2017 Nicholas Nethercote
12 njn@valgrind.org
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
48 on amd64-linux.
50 Note, this file contains kernel-specific knowledge in the form of
51 'struct rt_sigframe'. How does that relate to the vki kernel
52 interface stuff?
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.
66 This will be fixed.
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 */
84 struct vg_sigframe
86 /* Sanity check word. */
87 UInt magicPI;
89 UInt handlerflags; /* flags for signal handler */
92 /* Safely-saved version of sigNo, as described above. */
93 Int sigNo_private;
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;
100 /* HACK ALERT */
101 VexGuestAMD64State vex;
102 /* end HACK ALERT */
104 /* saved signal mask to be restored when handler returns */
105 vki_sigset_t mask;
107 /* Sanity check word. Is the highest-addressed word; do not
108 move!*/
109 UInt magicE;
112 struct rt_sigframe
114 /* Sig handler's return address */
115 Addr retaddr;
117 /* ucontext */
118 struct vki_ucontext uContext;
120 /* siginfo */
121 vki_siginfo_t sigInfo;
122 struct _vki_fpstate fpstate;
124 struct vg_sigframe vg;
128 //:: /*------------------------------------------------------------*/
129 //:: /*--- Signal operations ---*/
130 //:: /*------------------------------------------------------------*/
131 //::
132 //:: /*
133 //:: Great gobs of FP state conversion taken wholesale from
134 //:: linux/arch/i386/kernel/i387.c
135 //:: */
136 //::
137 //:: /*
138 //:: * FXSR floating point environment conversions.
139 //:: */
140 //:: #define X86_FXSR_MAGIC 0x0000
141 //::
142 //:: /*
143 //:: * FPU tag word conversions.
144 //:: */
145 //::
146 //:: static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
147 //:: {
148 //:: unsigned int tmp; /* to avoid 16 bit prefixes in the code */
149 //::
150 //:: /* Transform each pair of bits into 01 (valid) or 00 (empty) */
151 //:: tmp = ~twd;
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 */
157 //:: return tmp;
158 //:: }
159 //::
160 //:: static unsigned long twd_fxsr_to_i387( const struct i387_fxsave_struct *fxsave )
161 //:: {
162 //:: struct _vki_fpxreg *st = NULL;
163 //:: unsigned long twd = (unsigned long) fxsave->twd;
164 //:: unsigned long tag;
165 //:: unsigned long ret = 0xffff0000u;
166 //:: int i;
167 //::
168 //:: #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
169 //::
170 //:: for ( i = 0 ; i < 8 ; i++ ) {
171 //:: if ( twd & 0x1 ) {
172 //:: st = (struct _vki_fpxreg *) FPREG_ADDR( fxsave, i );
173 //::
174 //:: switch ( st->exponent & 0x7fff ) {
175 //:: case 0x7fff:
176 //:: tag = 2; /* Special */
177 //:: break;
178 //:: case 0x0000:
179 //:: if ( !st->significand[0] &&
180 //:: !st->significand[1] &&
181 //:: !st->significand[2] &&
182 //:: !st->significand[3] ) {
183 //:: tag = 1; /* Zero */
184 //:: } else {
185 //:: tag = 2; /* Special */
186 //:: }
187 //:: break;
188 //:: default:
189 //:: if ( st->significand[3] & 0x8000 ) {
190 //:: tag = 0; /* Valid */
191 //:: } else {
192 //:: tag = 2; /* Special */
193 //:: }
194 //:: break;
195 //:: }
196 //:: } else {
197 //:: tag = 3; /* Empty */
198 //:: }
199 //:: ret |= (tag << (2 * i));
200 //:: twd = twd >> 1;
201 //:: }
202 //:: return ret;
203 //:: }
204 //::
205 //:: static void convert_fxsr_to_user( struct _vki_fpstate *buf,
206 //:: const struct i387_fxsave_struct *fxsave )
207 //:: {
208 //:: unsigned long env[7];
209 //:: struct _vki_fpreg *to;
210 //:: struct _vki_fpxreg *from;
211 //:: int i;
212 //::
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;
220 //::
221 //:: VG_(memcpy)(buf, env, 7 * sizeof(unsigned long));
222 //::
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;
228 //::
229 //:: t[0] = f[0];
230 //:: t[1] = f[1];
231 //:: to->exponent = from->exponent;
232 //:: }
233 //:: }
234 //::
235 //:: static void convert_fxsr_from_user( struct i387_fxsave_struct *fxsave,
236 //:: const struct _vki_fpstate *buf )
237 //:: {
238 //:: unsigned long env[7];
239 //:: struct _vki_fpxreg *to;
240 //:: const struct _vki_fpreg *from;
241 //:: int i;
242 //::
243 //:: VG_(memcpy)(env, buf, 7 * sizeof(long));
244 //::
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];
253 //::
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;
259 //::
260 //:: t[0] = f[0];
261 //:: t[1] = f[1];
262 //:: to->exponent = from->exponent;
263 //:: }
264 //:: }
265 //::
266 //:: static inline void save_i387_fsave( arch_thread_t *regs, struct _vki_fpstate *buf )
267 //:: {
268 //:: struct i387_fsave_struct *fs = &regs->m_sse.fsave;
269 //::
270 //:: fs->status = fs->swd;
271 //:: VG_(memcpy)(buf, fs, sizeof(*fs));
272 //:: }
273 //::
274 //:: static void save_i387_fxsave( arch_thread_t *regs, struct _vki_fpstate *buf )
275 //:: {
276 //:: const struct i387_fxsave_struct *fx = &regs->m_sse.fxsave;
277 //:: convert_fxsr_to_user( buf, fx );
278 //::
279 //:: buf->status = fx->swd;
280 //:: buf->magic = X86_FXSR_MAGIC;
281 //:: VG_(memcpy)(buf->_fxsr_env, fx, sizeof(struct i387_fxsave_struct));
282 //:: }
283 //::
284 //:: static void save_i387( arch_thread_t *regs, struct _vki_fpstate *buf )
285 //:: {
286 //:: if ( VG_(have_ssestate) )
287 //:: save_i387_fxsave( regs, buf );
288 //:: else
289 //:: save_i387_fsave( regs, buf );
290 //:: }
291 //::
292 //:: static inline void restore_i387_fsave( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
293 //:: {
294 //:: VG_(memcpy)( &regs->m_sse.fsave, buf, sizeof(struct i387_fsave_struct) );
295 //:: }
296 //::
297 //:: static void restore_i387_fxsave( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
298 //:: {
299 //:: VG_(memcpy)(&regs->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( &regs->m_sse.fxsave, buf );
304 //:: }
305 //::
306 //:: static void restore_i387( arch_thread_t *regs, const struct _vki_fpstate __user *buf )
307 //:: {
308 //:: if ( VG_(have_ssestate) ) {
309 //:: restore_i387_fxsave( regs, buf );
310 //:: } else {
311 //:: restore_i387_fsave( regs, buf );
312 //:: }
313 //:: }
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.
324 static
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));
334 uc->uc_flags = 0;
335 uc->uc_link = 0;
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
343 SC2(r8,R8);
344 SC2(r9,R9);
345 SC2(r10,R10);
346 SC2(r11,R11);
347 SC2(r12,R12);
348 SC2(r13,R13);
349 SC2(r14,R14);
350 SC2(r15,R15);
351 SC2(rdi,RDI);
352 SC2(rsi,RSI);
353 SC2(rbp,RBP);
354 SC2(rbx,RBX);
355 SC2(rdx,RDX);
356 SC2(rax,RAX);
357 SC2(rcx,RCX);
358 SC2(rsp,RSP);
360 SC2(rip,RIP);
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);
365 sc->trapno = trapno;
366 sc->err = err;
367 # undef SC2
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,
376 ThreadState *tst,
377 const vki_sigset_t *mask,
378 UInt flags,
379 Int sigNo)
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;
385 /* HACK ALERT */
386 frame->vex = tst->arch.vex;
387 /* end HACK ALERT */
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,
400 void *restorer)
402 struct rt_sigframe *frame;
403 Addr rsp = rsp_top_of_frame;
404 Int sigNo = siginfo->si_signo;
405 UWord trapno;
406 UWord err;
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;
421 else
422 frame->retaddr = (Addr)&VG_(amd64_linux_SUBST_FOR_rt_sigreturn);
424 if (siguc) {
425 trapno = siguc->uc_mcontext.trapno;
426 err = siguc->uc_mcontext.err;
427 } else {
428 trapno = 0;
429 err = 0;
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);
447 return rsp;
451 void VG_(sigframe_create)( ThreadId tid,
452 Bool on_altstack,
453 Addr rsp_top_of_frame,
454 const vki_siginfo_t *siginfo,
455 const struct vki_ucontext *siguc,
456 void *handler,
457 UInt flags,
458 const vki_sigset_t *mask,
459 void *restorer )
461 Addr rsp;
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
490 caller to do. */
492 if (0)
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. */
505 static
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",
513 tst->tid);
514 VG_(set_default_handler)(VKI_SIGSEGV);
515 VG_(synth_fault)(tst->tid);
516 *sigNo = VKI_SIGSEGV;
517 return False;
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;
523 /* HACK ALERT */
524 tst->arch.vex = frame->vex;
525 /* end HACK ALERT */
526 *sigNo = frame->sigNo_private;
527 return True;
530 static
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);
562 static
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 )
575 Addr rsp;
576 ThreadState* tst;
577 SizeT size;
578 Int sigNo;
580 vg_assert(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))
593 VG_(message)(
594 Vg_DebugMsg,
595 "VG_(sigframe_destroy) (thread %u): isRT=%d valid magic; RIP=%#llx\n",
596 tid, isRT, tst->arch.vex.guest_RIP);
598 /* tell the tools */
599 VG_TRACK( post_deliver_signal, tid, sigNo );
602 #endif // defined(VGP_amd64_linux)
604 /*--------------------------------------------------------------------*/
605 /*--- end ---*/
606 /*--------------------------------------------------------------------*/