sigframe-ppc64-linux.c revision 9bea4c13fca0e3bb4b719dcb3ed63d47d479294e
1 2/*--------------------------------------------------------------------*/ 3/*--- Create/destroy signal delivery frames. ---*/ 4/*--- sigframe-ppc64-linux.c ---*/ 5/*--------------------------------------------------------------------*/ 6 7/* 8 This file is part of Valgrind, a dynamic binary instrumentation 9 framework. 10 11 Copyright (C) 2000-2010 Nicholas Nethercote 12 njn@valgrind.org 13 Copyright (C) 2004-2010 Paul Mackerras 14 paulus@samba.org 15 16 This program is free software; you can redistribute it and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of the 19 License, or (at your option) any later version. 20 21 This program is distributed in the hope that it will be useful, but 22 WITHOUT ANY WARRANTY; without even the implied warranty of 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 General Public License for more details. 25 26 You should have received a copy of the GNU General Public License 27 along with this program; if not, write to the Free Software 28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 29 02111-1307, USA. 30 31 The GNU General Public License is contained in the file COPYING. 32*/ 33 34#if defined(VGP_ppc64_linux) 35 36#include "pub_core_basics.h" 37#include "pub_core_vki.h" 38#include "pub_core_vkiscnums.h" 39#include "pub_core_threadstate.h" 40#include "pub_core_aspacemgr.h" 41#include "pub_core_libcbase.h" 42#include "pub_core_libcassert.h" 43#include "pub_core_libcprint.h" 44#include "pub_core_machine.h" 45#include "pub_core_options.h" 46#include "pub_core_sigframe.h" 47#include "pub_core_signals.h" 48#include "pub_core_tooliface.h" 49#include "pub_core_trampoline.h" 50#include "pub_core_transtab.h" // VG_(discard_translations) 51 52 53/* This module creates and removes signal frames for signal deliveries 54 on ppc64-linux. 55 56 Note, this file contains kernel-specific knowledge in the form of 57 'struct sigframe' and 'struct rt_sigframe'. How does that relate 58 to the vki kernel interface stuff? 59 60 Either a 'struct sigframe' or a 'struct rtsigframe' is pushed 61 onto the client's stack. This contains a subsidiary 62 vki_ucontext. That holds the vcpu's state across the signal, 63 so that the sighandler can mess with the vcpu state if it 64 really wants. 65 66 FIXME: sigcontexting is basically broken for the moment. When 67 delivering a signal, the integer registers and %eflags are 68 correctly written into the sigcontext, however the FP and SSE state 69 is not. When returning from a signal, only the integer registers 70 are restored from the sigcontext; the rest of the CPU state is 71 restored to what it was before the signal. 72 73 This will be fixed. 74*/ 75 76 77/*------------------------------------------------------------*/ 78/*--- Signal frame layouts ---*/ 79/*------------------------------------------------------------*/ 80 81// A structure in which to save the application's registers 82// during the execution of signal handlers. 83 84// On ppc64-linux, rt_sigframe is used for all signals. 85 86// In theory, so long as we get the arguments to the handler function 87// right, it doesn't matter what the exact layout of the rest of the 88// frame is. Unfortunately, things like gcc's exception unwinding 89// make assumptions about the locations of various parts of the frame, 90// so we need to duplicate it exactly. 91 92/* Many of these byzantine details derived from 93 linux-2.6.13/arch/ppc64/kernel/signal.c */ 94 95#define TRAMP_SIZE 6 /* who knows why - it only needs to be 2. */ 96 97/* Structure containing bits of information that we want to save 98 on signal delivery. */ 99struct vg_sig_private { 100 UInt magicPI; 101 UInt sigNo_private; 102 ULong _unused; /* makes the struct size be zero % 16 */ 103 VexGuestPPC64State vex_shadow1; 104 VexGuestPPC64State vex_shadow2; 105}; 106 107/* Structure put on stack for all signal handlers. */ 108struct rt_sigframe { 109 struct vki_ucontext uc; 110 ULong _unused[2]; 111 UInt tramp[TRAMP_SIZE]; 112 struct vki_siginfo* pinfo; 113 void* puc; 114 vki_siginfo_t info; 115 struct vg_sig_private priv; 116 UChar abigap[288]; 117}; 118 119#define SET_SIGNAL_LR(zztst, zzval) \ 120 do { tst->arch.vex.guest_LR = (zzval); \ 121 VG_TRACK( post_reg_write, Vg_CoreSignal, tst->tid, \ 122 offsetof(VexGuestPPC64State,guest_LR), \ 123 sizeof(UWord) ); \ 124 } while (0) 125 126#define SET_SIGNAL_GPR(zztst, zzn, zzval) \ 127 do { tst->arch.vex.guest_GPR##zzn = (zzval); \ 128 VG_TRACK( post_reg_write, Vg_CoreSignal, tst->tid, \ 129 offsetof(VexGuestPPC64State,guest_GPR##zzn), \ 130 sizeof(UWord) ); \ 131 } while (0) 132 133 134/* Extend the stack segment downwards if needed so as to ensure the 135 new signal frames are mapped to something. Return a Bool 136 indicating whether or not the operation was successful. 137*/ 138static Bool extend ( ThreadState *tst, Addr addr, SizeT size ) 139{ 140 ThreadId tid = tst->tid; 141 NSegment const* stackseg = NULL; 142 143 if (VG_(extend_stack)(addr, tst->client_stack_szB)) { 144 stackseg = VG_(am_find_nsegment)(addr); 145 if (0 && stackseg) 146 VG_(printf)("frame=%#lx seg=%#lx-%#lx\n", 147 addr, stackseg->start, stackseg->end); 148 } 149 150 if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) { 151 VG_(message)( 152 Vg_UserMsg, 153 "Can't extend stack to %#lx during signal delivery for thread %d:\n", 154 addr, tid); 155 if (stackseg == NULL) 156 VG_(message)(Vg_UserMsg, " no stack segment\n"); 157 else 158 VG_(message)(Vg_UserMsg, " too small or bad protection modes\n"); 159 160 /* set SIGSEGV to default handler */ 161 VG_(set_default_handler)(VKI_SIGSEGV); 162 VG_(synth_fault_mapping)(tid, addr); 163 164 /* The whole process should be about to die, since the default 165 action of SIGSEGV to kill the whole process. */ 166 return False; 167 } 168 169 /* For tracking memory events, indicate the entire frame has been 170 allocated. */ 171 VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB, 172 size + VG_STACK_REDZONE_SZB, tid ); 173 174 return True; 175} 176 177 178/* EXPORTED */ 179void VG_(sigframe_create)( ThreadId tid, 180 Addr sp_top_of_frame, 181 const vki_siginfo_t *siginfo, 182 const struct vki_ucontext *siguc, 183 void *handler, 184 UInt flags, 185 const vki_sigset_t *mask, 186 void *restorer ) 187{ 188 struct vg_sig_private* priv; 189 Addr sp; 190 ThreadState* tst; 191 Int sigNo = siginfo->si_signo; 192 Addr faultaddr; 193 struct rt_sigframe* frame; 194 195 /* Stack must be 16-byte aligned */ 196 vg_assert(VG_IS_16_ALIGNED(sizeof(struct vg_sig_private))); 197 vg_assert(VG_IS_16_ALIGNED(sizeof(struct rt_sigframe))); 198 199 sp_top_of_frame &= ~0xf; 200 sp = sp_top_of_frame - sizeof(struct rt_sigframe); 201 202 tst = VG_(get_ThreadState)(tid); 203 if (!extend(tst, sp, sp_top_of_frame - sp)) 204 return; 205 206 vg_assert(VG_IS_16_ALIGNED(sp)); 207 208 frame = (struct rt_sigframe *) sp; 209 210 /* clear it (conservatively) */ 211 VG_(memset)(frame, 0, sizeof(*frame)); 212 213 ///////// 214 frame->pinfo = &frame->info; 215 frame->puc = &frame->uc; 216 217 frame->uc.uc_flags = 0; 218 frame->uc.uc_link = 0; 219 ///////// 220 221 /* Set up the stack chain pointer */ 222 VG_TRACK( pre_mem_write, Vg_CoreSignal, tid, "signal handler frame", 223 sp, sizeof(UWord) ); 224 *(Addr *)sp = tst->arch.vex.guest_GPR1; 225 VG_TRACK( post_mem_write, Vg_CoreSignal, tid, 226 sp, sizeof(UWord) ); 227 228 faultaddr = (Addr)siginfo->_sifields._sigfault._addr; 229 if (sigNo == VKI_SIGILL && siginfo->si_code > 0) 230 faultaddr = tst->arch.vex.guest_CIA; 231 232 VG_(memcpy)(&frame->info, siginfo, sizeof(*siginfo)); 233 VG_TRACK( post_mem_write, Vg_CoreSignal, tid, 234 (Addr)&frame->info, sizeof(frame->info) ); 235 236 frame->uc.uc_flags = 0; 237 frame->uc.uc_link = 0; 238 frame->uc.uc_stack = tst->altstack; 239 frame->uc.uc_sigmask = tst->sig_mask; 240 VG_TRACK( post_mem_write, Vg_CoreSignal, tid, 241 (Addr)(&frame->uc), sizeof(frame->uc) ); 242 243# define DO(gpr) frame->uc.uc_mcontext.gp_regs[VKI_PT_R0+gpr] \ 244 = tst->arch.vex.guest_GPR##gpr 245 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 246 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 247 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 248 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 249# undef DO 250 251 frame->uc.uc_mcontext.gp_regs[VKI_PT_NIP] = tst->arch.vex.guest_CIA; 252 frame->uc.uc_mcontext.gp_regs[VKI_PT_MSR] = 0xf032; /* pretty arbitrary */ 253 frame->uc.uc_mcontext.gp_regs[VKI_PT_ORIG_R3] = tst->arch.vex.guest_GPR3; 254 frame->uc.uc_mcontext.gp_regs[VKI_PT_CTR] = tst->arch.vex.guest_CTR; 255 frame->uc.uc_mcontext.gp_regs[VKI_PT_LNK] = tst->arch.vex.guest_LR; 256 frame->uc.uc_mcontext.gp_regs[VKI_PT_XER] = LibVEX_GuestPPC64_get_XER( 257 &tst->arch.vex); 258 frame->uc.uc_mcontext.gp_regs[VKI_PT_CCR] = LibVEX_GuestPPC64_get_CR( 259 &tst->arch.vex); 260 //mc->mc_gregs[VKI_PT_MQ] = 0; 261 //mc->mc_gregs[VKI_PT_TRAP] = 0; 262 //mc->mc_gregs[VKI_PT_DAR] = fault_addr; 263 //mc->mc_gregs[VKI_PT_DSISR] = 0; 264 //mc->mc_gregs[VKI_PT_RESULT] = 0; 265 266 /* XXX should do FP and vector regs */ 267 268 /* set up signal return trampoline */ 269 /* NB. 5 Sept 07. mc->mc_pad[0..1] used to contain a the code to 270 which the signal handler returns, and it just did sys_sigreturn 271 or sys_rt_sigreturn. But this doesn't work if the stack is 272 non-executable, and it isn't consistent with the x86-linux and 273 amd64-linux scheme for removing the stack frame. So instead be 274 consistent and use a stub in m_trampoline. Then it doesn't 275 matter whether or not the (guest) stack is executable. This 276 fixes #149519 and #145837. */ 277 frame->tramp[0] = 0; /* invalid */ 278 frame->tramp[1] = 0; /* invalid */ 279 VG_TRACK(post_mem_write, Vg_CoreSignal, tst->tid, 280 (Addr)&frame->tramp, sizeof(frame->tramp)); 281 282 /* invalidate any translation of this area */ 283 VG_(discard_translations)( (Addr64)&frame->tramp[0], 284 sizeof(frame->tramp), "stack_mcontext" ); 285 286 /* set the signal handler to return to the trampoline */ 287 SET_SIGNAL_LR(tst, (Addr)&VG_(ppc64_linux_SUBST_FOR_rt_sigreturn)); 288 289 /* Stack pointer for the handler .. (note, back chain set 290 earlier) */ 291 SET_SIGNAL_GPR(tid, 1, sp); 292 293 /* Args for the handler .. */ 294 SET_SIGNAL_GPR(tid, 3, sigNo); 295 SET_SIGNAL_GPR(tid, 4, (Addr) &frame->info); 296 SET_SIGNAL_GPR(tid, 5, (Addr) &frame->uc); 297 /* the kernel sets this, though it doesn't seem to be in the ABI */ 298 SET_SIGNAL_GPR(tid, 6, (Addr) &frame->info); 299 300 /* Handler is in fact a standard ppc64-linux function descriptor, 301 so extract the function entry point and also the toc ptr to use. */ 302 SET_SIGNAL_GPR(tid, 2, (Addr) ((ULong*)handler)[1]); 303 tst->arch.vex.guest_CIA = (Addr) ((ULong*)handler)[0]; 304 305 priv = &frame->priv; 306 priv->magicPI = 0x31415927; 307 priv->sigNo_private = sigNo; 308 priv->vex_shadow1 = tst->arch.vex_shadow1; 309 priv->vex_shadow2 = tst->arch.vex_shadow2; 310 311 if (0) 312 VG_(printf)("pushed signal frame; %%R1 now = %#lx, " 313 "next %%CIA = %#llx, status=%d\n", 314 sp, tst->arch.vex.guest_CIA, tst->status); 315} 316 317 318/*------------------------------------------------------------*/ 319/*--- Destroying signal frames ---*/ 320/*------------------------------------------------------------*/ 321 322/* EXPORTED */ 323void VG_(sigframe_destroy)( ThreadId tid, Bool isRT ) 324{ 325 ThreadState *tst; 326 struct vg_sig_private *priv; 327 Addr sp; 328 UInt frame_size; 329 struct rt_sigframe *frame; 330 Int sigNo; 331 Bool has_siginfo = isRT; 332 333 vg_assert(VG_(is_valid_tid)(tid)); 334 tst = VG_(get_ThreadState)(tid); 335 336 /* Check that the stack frame looks valid */ 337 sp = tst->arch.vex.guest_GPR1; 338 vg_assert(VG_IS_16_ALIGNED(sp)); 339 /* JRS 17 Nov 05: This code used to check that *sp -- which should 340 have been set by the stwu at the start of the handler -- points 341 to just above the frame (ie, the previous frame). However, that 342 isn't valid when delivering signals on alt stacks. So I removed 343 it. The frame is still sanity-checked using the priv->magicPI 344 field. */ 345 346 frame = (struct rt_sigframe *)sp; 347 frame_size = sizeof(*frame); 348 priv = &frame->priv; 349 vg_assert(priv->magicPI == 0x31415927); 350 tst->sig_mask = frame->uc.uc_sigmask; 351 tst->tmp_sig_mask = tst->sig_mask; 352 353 sigNo = priv->sigNo_private; 354 355# define DO(gpr) tst->arch.vex.guest_GPR##gpr \ 356 = frame->uc.uc_mcontext.gp_regs[VKI_PT_R0+gpr] 357 DO(0); DO(1); DO(2); DO(3); DO(4); DO(5); DO(6); DO(7); 358 DO(8); DO(9); DO(10); DO(11); DO(12); DO(13); DO(14); DO(15); 359 DO(16); DO(17); DO(18); DO(19); DO(20); DO(21); DO(22); DO(23); 360 DO(24); DO(25); DO(26); DO(27); DO(28); DO(29); DO(30); DO(31); 361# undef DO 362 363 tst->arch.vex.guest_CIA = frame->uc.uc_mcontext.gp_regs[VKI_PT_NIP]; 364 365 LibVEX_GuestPPC64_put_CR( frame->uc.uc_mcontext.gp_regs[VKI_PT_CCR], 366 &tst->arch.vex ); 367 368 tst->arch.vex.guest_LR = frame->uc.uc_mcontext.gp_regs[VKI_PT_LNK]; 369 tst->arch.vex.guest_CTR = frame->uc.uc_mcontext.gp_regs[VKI_PT_CTR]; 370 LibVEX_GuestPPC64_put_XER( frame->uc.uc_mcontext.gp_regs[VKI_PT_XER], 371 &tst->arch.vex ); 372 373 tst->arch.vex_shadow1 = priv->vex_shadow1; 374 tst->arch.vex_shadow2 = priv->vex_shadow2; 375 376 VG_TRACK(die_mem_stack_signal, sp, frame_size); 377 378 if (VG_(clo_trace_signals)) 379 VG_(message)(Vg_DebugMsg, 380 "vg_pop_signal_frame (thread %d): isRT=%d " 381 "valid magic; EIP=%#llx\n", 382 tid, has_siginfo, tst->arch.vex.guest_CIA); 383 384 /* tell the tools */ 385 VG_TRACK( post_deliver_signal, tid, sigNo ); 386} 387 388#endif // defined(VGP_ppc64_linux) 389 390/*--------------------------------------------------------------------*/ 391/*--- end ---*/ 392/*--------------------------------------------------------------------*/ 393