1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--------------------------------------------------------------------*/
3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--- Create/destroy signal delivery frames.                       ---*/
4ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*---                                        sigframe-x86-darwin.c ---*/
5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--------------------------------------------------------------------*/
6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*
8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   This file is part of Valgrind, a dynamic binary instrumentation
9ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   framework.
10ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
11b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov   Copyright (C) 2006-2011 OpenWorks Ltd
12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      info@open-works.co.uk
13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   This program is free software; you can redistribute it and/or
15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   modify it under the terms of the GNU General Public License as
16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   published by the Free Software Foundation; either version 2 of the
17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   License, or (at your option) any later version.
18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   This program is distributed in the hope that it will be useful, but
20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   WITHOUT ANY WARRANTY; without even the implied warranty of
21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   General Public License for more details.
23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   You should have received a copy of the GNU General Public License
25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   along with this program; if not, write to the Free Software
26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   02111-1307, USA.
28ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
29ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   The GNU General Public License is contained in the file COPYING.
30ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown*/
31ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
32ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#if defined(VGP_x86_darwin)
33ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
34ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_basics.h"
35ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_vki.h"
36ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_vkiscnums.h"
37b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "pub_core_libcsetjmp.h"    // to keep _threadstate.h happy
38ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_threadstate.h"
39ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_aspacemgr.h"
40ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_libcbase.h"
41ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_libcassert.h"
42ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_libcprint.h"
43ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_machine.h"
44ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_options.h"
45ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_signals.h"
46ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_tooliface.h"
47ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_trampoline.h"
48ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_sigframe.h"      /* self */
49ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
50ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
51ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Cheap-ass hack copied from ppc32-aix5 code, just to get started.
52ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Produce a frame with layout entirely of our own choosing. */
53ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
54ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* This module creates and removes signal frames for signal deliveries
55ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   on x86-darwin.  Kludgey; the machine state ought to be saved in a
56ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   ucontext and retrieved from it later, so the handler can modify it
57ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   and return.  However .. for now .. just stick the vex guest state
58ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   in the frame and snarf it again later.
59ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
60ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Also, don't bother with creating siginfo and ucontext in the
61ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   handler, although do point them somewhere non-faulting.
62ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
63ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Frame should have a 16-aligned size, just in case that turns out to
64ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   be important for Darwin.  (be conservative)
65ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown*/
66ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstruct hacky_sigframe {
67ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* first four words look like a call to a 3-arg x86 function */
68ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             returnAddr;
69ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             a1_signo;
70ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             a2_siginfo;
71ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             a3_ucontext;
72ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UChar            lower_guardzone[512];  // put nothing here
73ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VexGuestX86State gst;
74ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VexGuestX86State gshadow1;
75ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VexGuestX86State gshadow2;
76ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vki_siginfo_t    fake_siginfo;
77ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct vki_ucontext fake_ucontext;
78ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             magicPI;
79ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             sigNo_private;
80ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vki_sigset_t     mask; // saved sigmask; restore when hdlr returns
81ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UInt             __pad[1];
82ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   UChar            upper_guardzone[512]; // put nothing here
83ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // and don't zero it, since that might overwrite the client's
84ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // stack redzone, at least on archs which have one
85ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown};
86ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
87ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
88ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Extend the stack segment downwards if needed so as to ensure the
89ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new signal frames are mapped to something.  Return a Bool
90ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   indicating whether or not the operation was successful.
91ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown*/
92ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic Bool extend ( ThreadState *tst, Addr addr, SizeT size )
93ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
94ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   ThreadId tid = tst->tid;
95ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* For tracking memory events, indicate the entire frame has been
96ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      allocated.  Except, don't mess with the area which
97ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      overlaps the previous frame's redzone. */
98ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* XXX is the following call really right?  compared with the
99ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      amd64-linux version, this doesn't appear to handle the redzone
100ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      in the same way. */
101ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( new_mem_stack_signal,
102ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             addr - VG_STACK_REDZONE_SZB, size, tid );
103ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return True;
104ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
105ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
106ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
107ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Create a signal frame for thread 'tid'.  Make a 3-arg frame
108ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   regardless of whether the client originally requested a 1-arg
109ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
110ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   former case, the x86 calling conventions will simply cause the
111ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   extra 2 args to be ignored (inside the handler). */
112ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid VG_(sigframe_create) ( ThreadId tid,
113ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            Addr sp_top_of_frame,
114ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            const vki_siginfo_t *siginfo,
115ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            const struct vki_ucontext *siguc,
116ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            void *handler,
117ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            UInt flags,
118ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            const vki_sigset_t *mask,
119ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                            void *restorer )
120ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
121ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   ThreadState* tst;
122ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Addr esp;
123ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct hacky_sigframe* frame;
124ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Int sigNo = siginfo->si_signo;
125ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
126ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vg_assert(VG_IS_16_ALIGNED(sizeof(struct hacky_sigframe)));
127ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
128ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   sp_top_of_frame &= ~0xf;
129ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   esp = sp_top_of_frame - sizeof(struct hacky_sigframe);
130ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
131ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst = VG_(get_ThreadState)(tid);
132ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (!extend(tst, esp, sp_top_of_frame - esp))
133ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return;
134ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
135ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vg_assert(VG_IS_16_ALIGNED(esp));
136ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
137ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame = (struct hacky_sigframe *) esp;
138ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
139ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* clear it (very conservatively) (why so conservatively??) */
140ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(memset)(&frame->lower_guardzone, 0, 512);
141ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(memset)(&frame->gst,      0, sizeof(VexGuestX86State));
142ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(memset)(&frame->gshadow1, 0, sizeof(VexGuestX86State));
143ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(memset)(&frame->gshadow2, 0, sizeof(VexGuestX86State));
144ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(memset)(&frame->fake_siginfo,  0, sizeof(frame->fake_siginfo));
145ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(memset)(&frame->fake_ucontext, 0, sizeof(frame->fake_ucontext));
146ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
147ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* save stuff in frame */
148ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->gst           = tst->arch.vex;
149ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->gshadow1      = tst->arch.vex_shadow1;
150ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->gshadow2      = tst->arch.vex_shadow2;
151ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->sigNo_private = sigNo;
152ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->mask          = tst->sig_mask;
153ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->magicPI       = 0x31415927;
154ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
155ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Minimally fill in the siginfo and ucontext.  Note, utter
156ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      lameness prevails.  Be underwhelmed, be very underwhelmed. */
157ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->fake_siginfo.si_signo = sigNo;
158ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->fake_siginfo.si_code  = siginfo->si_code;
159ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
160ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Set up stack pointer */
161ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vg_assert(esp == (Addr)&frame->returnAddr);
162ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(set_SP)(tid, esp);
163ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_STACK_PTR, sizeof(UInt));
164ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
165ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Set up program counter */
166ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(set_IP)(tid, (UInt)handler);
167ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_INSTR_PTR, sizeof(UInt));
168ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
169ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Set up RA and args for the frame */
170ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( pre_mem_write, Vg_CoreSignal, tid, "signal handler frame",
171ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             (Addr)frame, 4*sizeof(UInt) );
172ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->returnAddr  = (UInt)&VG_(x86_darwin_SUBST_FOR_sigreturn);
173ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->a1_signo    = sigNo;
174ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->a2_siginfo  = (UInt)&frame->fake_siginfo;  /* oh well */
175ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame->a3_ucontext = (UInt)&frame->fake_ucontext; /* oh well */
176ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
177ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             (Addr)frame, 4*sizeof(UInt) );
178ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
179ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             (Addr)&frame->fake_siginfo, sizeof(frame->fake_siginfo));
180ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
181ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             (Addr)&frame->fake_ucontext, sizeof(frame->fake_ucontext));
182ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
183ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (VG_(clo_trace_signals))
184ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(message)(Vg_DebugMsg,
185ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                   "sigframe_create (thread %d): next EIP=%#lx, next ESP=%#lx",
186ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                   tid, (Addr)handler, (Addr)frame );
187ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
188ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
189ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
190ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Remove a signal frame from thread 'tid's stack, and restore the CPU
191ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   state from it.  Note, isRT is irrelevant here. */
192ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownvoid VG_(sigframe_destroy)( ThreadId tid, Bool isRT )
193ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
194ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   ThreadState *tst;
195ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Addr esp;
196ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Int sigNo;
197ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct hacky_sigframe* frame;
198ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
199ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vg_assert(VG_(is_valid_tid)(tid));
200ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst = VG_(get_ThreadState)(tid);
201ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
202ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Check that the stack frame looks valid */
203ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   esp = VG_(get_SP)(tid);
204ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
205ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* why -4 ? because the signal handler's return will have popped
206ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      the return address of the stack; and the return address is the
207ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      lowest-addressed element of hacky_sigframe. */
208ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   frame = (struct hacky_sigframe*)(esp - 4);
209ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vg_assert(frame->magicPI == 0x31415927);
210ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vg_assert(VG_IS_16_ALIGNED(frame));
211ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
212ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* restore the entire guest state, and shadows, from the
213ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      frame.  Note, as per comments above, this is a kludge - should
214ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      restore it from saved ucontext.  Oh well. */
215ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst->arch.vex = frame->gst;
216ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst->arch.vex_shadow1 = frame->gshadow1;
217ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst->arch.vex_shadow2 = frame->gshadow2;
218ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst->sig_mask = frame->mask;
219ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   tst->tmp_sig_mask = frame->mask;
220ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   sigNo = frame->sigNo_private;
221ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
222ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (VG_(clo_trace_signals))
223ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(message)(Vg_DebugMsg,
224ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                   "sigframe_destroy (thread %d): valid magic; next EIP=%#x",
225ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                   tid, tst->arch.vex.guest_EIP);
226ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
227ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( die_mem_stack_signal,
228ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             (Addr)frame - VG_STACK_REDZONE_SZB,
229ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             sizeof(struct hacky_sigframe) );
230ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
231ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* tell the tools */
232ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_TRACK( post_deliver_signal, tid, sigNo );
233ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
234ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
235ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#endif // defined(VGP_x86_darwin)
236ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
237ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--------------------------------------------------------------------*/
238ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--- end                                                          ---*/
239ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--------------------------------------------------------------------*/
240