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