]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/coregrind/m_sigframe/sigframe-arm-linux.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / coregrind / m_sigframe / sigframe-arm-linux.c
1
2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames.                       ---*/
4 /*---                                         sigframe-arm-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    Copyright (C) 2008-2010 Evan Geller
16       gaze@bea.ms
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of the
21    License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful, but
24    WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31    02111-1307, USA.
32
33    The GNU General Public License is contained in the file COPYING.
34 */
35
36 #if defined(VGP_arm_linux)
37
38 #include "pub_core_basics.h"
39 #include "pub_core_vki.h"
40 #include "pub_core_vkiscnums.h"
41 #include "pub_core_threadstate.h"
42 #include "pub_core_aspacemgr.h"
43 #include "pub_core_libcbase.h"
44 #include "pub_core_libcassert.h"
45 #include "pub_core_libcprint.h"
46 #include "pub_core_machine.h"
47 #include "pub_core_options.h"
48 #include "pub_core_sigframe.h"
49 #include "pub_core_signals.h"
50 #include "pub_core_tooliface.h"
51 #include "pub_core_trampoline.h"
52 #include "pub_core_transtab.h"      // VG_(discard_translations)
53
54
55 struct vg_sig_private {
56    UInt magicPI;
57    UInt sigNo_private;
58    VexGuestARMState vex_shadow1;
59    VexGuestARMState vex_shadow2;
60 };
61
62 struct sigframe {
63    struct vki_ucontext uc;
64    unsigned long retcode[2];
65    struct vg_sig_private vp;
66 };
67
68 struct rt_sigframe {
69    vki_siginfo_t info;
70    struct sigframe sig;
71 };
72
73 static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
74 {
75    ThreadId        tid = tst->tid;
76    NSegment const* stackseg = NULL;
77
78    if (VG_(extend_stack)(addr, tst->client_stack_szB)) {
79       stackseg = VG_(am_find_nsegment)(addr);
80       if (0 && stackseg)
81     VG_(printf)("frame=%#lx seg=%#lx-%#lx\n",
82            addr, stackseg->start, stackseg->end);
83    }
84
85    if (stackseg == NULL || !stackseg->hasR || !stackseg->hasW) {
86       VG_(message)(
87          Vg_UserMsg,
88          "Can't extend stack to %#lx during signal delivery for thread %d:",
89          addr, tid);
90       if (stackseg == NULL)
91          VG_(message)(Vg_UserMsg, "  no stack segment");
92       else
93          VG_(message)(Vg_UserMsg, "  too small or bad protection modes");
94
95       /* set SIGSEGV to default handler */
96       VG_(set_default_handler)(VKI_SIGSEGV);
97       VG_(synth_fault_mapping)(tid, addr);
98
99       /* The whole process should be about to die, since the default
100     action of SIGSEGV to kill the whole process. */
101       return False;
102    }
103
104    /* For tracking memory events, indicate the entire frame has been
105       allocated. */
106    VG_TRACK( new_mem_stack_signal, addr - VG_STACK_REDZONE_SZB,
107              size + VG_STACK_REDZONE_SZB, tid );
108
109    return True;
110 }
111
112 static void synth_ucontext( ThreadId tid, const vki_siginfo_t *si,
113                     UWord trapno, UWord err, const vki_sigset_t *set, 
114                     struct vki_ucontext *uc){
115
116    ThreadState *tst = VG_(get_ThreadState)(tid);
117    struct vki_sigcontext *sc = &uc->uc_mcontext;
118
119    VG_(memset)(uc, 0, sizeof(*uc));
120
121    uc->uc_flags = 0;
122    uc->uc_link = 0;
123    uc->uc_sigmask = *set;
124    uc->uc_stack = tst->altstack;
125
126 #  define SC2(reg,REG)  sc->arm_##reg = tst->arch.vex.guest_##REG
127    SC2(r0,R0);
128    SC2(r1,R1);
129    SC2(r2,R2);
130    SC2(r3,R3);
131    SC2(r4,R4);
132    SC2(r5,R5);
133    SC2(r6,R6);
134    SC2(r7,R7);
135    SC2(r8,R8);
136    SC2(r9,R9);
137    SC2(r10,R10);
138    SC2(fp,R11);
139    SC2(ip,R12);
140    SC2(sp,R13);
141    SC2(lr,R14);
142    SC2(pc,R15T);
143 #  undef SC2
144
145    sc->trap_no = trapno;
146    sc->error_code = err;
147    sc->fault_address = (UInt)si->_sifields._sigfault._addr;
148 }
149
150
151 static void build_sigframe(ThreadState *tst,
152             struct sigframe *frame,
153             const vki_siginfo_t *siginfo,
154             const struct vki_ucontext *siguc,
155             void *handler, UInt flags,
156             const vki_sigset_t *mask,
157             void *restorer){
158
159    UWord trapno;
160    UWord err;
161    Int  sigNo = siginfo->si_signo;
162    struct vg_sig_private *priv = &frame->vp;
163
164    VG_TRACK( pre_mem_write, Vg_CoreSignal, tst->tid, "signal handler frame",
165          (Addr)frame, offsetof(struct sigframe, vp));
166
167    if(siguc) {
168       trapno = siguc->uc_mcontext.trap_no;
169       err = siguc->uc_mcontext.error_code;
170    } else {
171       trapno = 0;
172       err = 0;
173    }
174
175    synth_ucontext(tst->tid, siginfo, trapno, err, mask, &frame->uc);
176
177    VG_TRACK( post_mem_write, Vg_CoreSignal, tst->tid,
178          (Addr)frame, offsetof(struct sigframe, vp));
179
180    priv->magicPI = 0x31415927;
181    priv->sigNo_private = sigNo;
182    priv->vex_shadow1 = tst->arch.vex_shadow1;
183    priv->vex_shadow2 = tst->arch.vex_shadow2;
184
185 }
186
187
188
189 /* EXPORTED */
190 void VG_(sigframe_create)( ThreadId tid, 
191                            Addr sp_top_of_frame,
192                            const vki_siginfo_t *siginfo,
193                            const struct vki_ucontext *siguc,
194                            void *handler, 
195                            UInt flags,
196                            const vki_sigset_t *mask,
197                            void *restorer )
198 {
199 //   struct vg_sig_private *priv;
200    Addr sp = sp_top_of_frame;
201    ThreadState *tst;
202    Int sigNo = siginfo->si_signo;
203 //   Addr faultaddr;
204    UInt size;
205
206    tst = VG_(get_ThreadState)(tid);
207
208    size = flags & VKI_SA_SIGINFO ? sizeof(struct rt_sigframe) :
209       sizeof(struct sigframe);
210
211    sp -= size;
212    sp = VG_ROUNDDN(sp, 16);
213
214    if(!extend(tst, sp, size))
215       I_die_here; // XXX Incorrect behavior
216
217
218    if (flags & VKI_SA_SIGINFO){
219       struct rt_sigframe *rsf = (struct rt_sigframe *)sp;
220       
221       /* Track our writes to siginfo */
222       VG_TRACK( pre_mem_write, Vg_CoreSignal, tst->tid,  /* VVVVV */
223             "signal handler siginfo", (Addr)rsf, 
224             offsetof(struct rt_sigframe, sig));
225
226       VG_(memcpy)(&rsf->info, siginfo, sizeof(vki_siginfo_t));
227
228       if(sigNo == VKI_SIGILL && siginfo->si_code > 0) {
229          rsf->info._sifields._sigfault._addr = (Addr *) (tst)->arch.vex.guest_R12; /* IP */
230       }
231       VG_TRACK( post_mem_write, Vg_CoreSignal, tst->tid, /* ^^^^^ */
232             (Addr)rsf, offsetof(struct rt_sigframe, sig));
233
234       build_sigframe(tst, &rsf->sig, siginfo, siguc,
235                              handler, flags, mask, restorer);
236       tst->arch.vex.guest_R1 = (Addr)&rsf->info;
237       tst->arch.vex.guest_R2 = (Addr)&rsf->sig.uc;
238    }
239    else {
240       build_sigframe(tst, (struct sigframe *)sp, siginfo, siguc,
241                              handler, flags, mask, restorer);
242    }
243
244    VG_(set_SP)(tid, sp);
245    VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_STACK_PTR,
246          sizeof(Addr));
247    tst->arch.vex.guest_R0  = sigNo; 
248
249    if (flags & VKI_SA_RESTORER)
250        tst->arch.vex.guest_R14 = (Addr) restorer; 
251
252    tst->arch.vex.guest_R15T = (Addr) handler; /* R15 == PC */
253 }
254
255
256 /*------------------------------------------------------------*/
257 /*--- Destroying signal frames                             ---*/
258 /*------------------------------------------------------------*/
259
260 /* EXPORTED */
261 void VG_(sigframe_destroy)( ThreadId tid, Bool isRT )
262 {
263    ThreadState *tst;
264    struct vg_sig_private *priv;
265    Addr sp;
266    UInt frame_size;
267    struct vki_sigcontext *mc;
268    Int sigNo;
269    Bool has_siginfo = isRT;
270
271    vg_assert(VG_(is_valid_tid)(tid));
272    tst = VG_(get_ThreadState)(tid);
273    sp = tst->arch.vex.guest_R13;
274
275    if (has_siginfo) {
276       struct rt_sigframe *frame = (struct rt_sigframe *)sp;
277       frame_size = sizeof(*frame);
278       mc = &frame->sig.uc.uc_mcontext;
279       priv = &frame->sig.vp;
280       vg_assert(priv->magicPI == 0x31415927);
281       tst->sig_mask = frame->sig.uc.uc_sigmask;
282    } else {
283       struct sigframe *frame = (struct sigframe *)sp;
284       frame_size = sizeof(*frame);
285       mc = &frame->uc.uc_mcontext;
286       priv = &frame->vp;
287       vg_assert(priv->magicPI == 0x31415927);
288       tst->sig_mask = frame->uc.uc_sigmask;
289       /*tst->sig_mask.sig[0] = frame->uc.uc_mcontext.oldmask;
290       tst->sig_mask.sig[1] = frame->uc.uc_mcontext._unused[3];
291       VG_(printf)("Setting signmask to %08x%08x\n",tst->sig_mask[0],tst->sig_mask[1]);
292 */
293    }
294    tst->tmp_sig_mask = tst->sig_mask;
295
296    sigNo = priv->sigNo_private;
297
298     //XXX: restore regs
299 #  define REST(reg,REG)  tst->arch.vex.guest_##REG = mc->arm_##reg;
300    REST(r0,R0);
301    REST(r1,R1);
302    REST(r2,R2);
303    REST(r3,R3);
304    REST(r4,R4);
305    REST(r5,R5);
306    REST(r6,R6);
307    REST(r7,R7);
308    REST(r8,R8);
309    REST(r9,R9);
310    REST(r10,R10);
311    REST(fp,R11);
312    REST(ip,R12);
313    REST(sp,R13);
314    REST(lr,R14);
315    REST(pc,R15T);
316 #  undef REST
317
318    tst->arch.vex_shadow1 = priv->vex_shadow1;
319    tst->arch.vex_shadow2 = priv->vex_shadow2;
320
321    VG_TRACK( die_mem_stack_signal, sp - VG_STACK_REDZONE_SZB,
322              frame_size + VG_STACK_REDZONE_SZB );
323              
324    if (VG_(clo_trace_signals))
325       VG_(message)(Vg_DebugMsg,
326                    "vg_pop_signal_frame (thread %d): "
327                    "isRT=%d valid magic; PC=%#x",
328                    tid, has_siginfo, tst->arch.vex.guest_R15T);
329
330    /* tell the tools */
331    VG_TRACK( post_deliver_signal, tid, sigNo );
332 }
333
334 #endif // defined(VGP_arm_linux)
335
336 /*--------------------------------------------------------------------*/
337 /*--- end                                     sigframe-arm-linux.c ---*/
338 /*--------------------------------------------------------------------*/