ms-manual.xml: Put stray ':' inside para.
[valgrind.git] / coregrind / m_sigframe / sigframe-x86-darwin.c
blob9bc82196235d97f64b41d7084debb083ba0b720d
2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames. ---*/
4 /*--- sigframe-x86-darwin.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2006-2017 OpenWorks Ltd
12 info@open-works.co.uk
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_x86_darwin)
32 #include "pub_core_basics.h"
33 #include "pub_core_vki.h"
34 #include "pub_core_vkiscnums.h"
35 #include "pub_core_threadstate.h"
36 #include "pub_core_aspacemgr.h"
37 #include "pub_core_libcbase.h"
38 #include "pub_core_libcassert.h"
39 #include "pub_core_libcprint.h"
40 #include "pub_core_machine.h"
41 #include "pub_core_options.h"
42 #include "pub_core_signals.h"
43 #include "pub_core_tooliface.h"
44 #include "pub_core_trampoline.h"
45 #include "pub_core_sigframe.h" /* self */
46 #include "priv_sigframe.h"
49 /* Originally copied from ppc32-aix5 code.
50 Produce a frame with layout entirely of our own choosing.
52 This module creates and removes signal frames for signal deliveries
53 on x86-darwin. The machine state is saved in a ucontext and retrieved
54 from it later, so the handler can modify it and return.
56 Frame should have a 16-aligned size, just in case that turns out to
57 be important for Darwin. (be conservative)
59 struct hacky_sigframe {
60 /* first four words look like a call to a 3-arg x86 function */
61 UInt returnAddr;
62 UInt a1_signo;
63 UInt a2_siginfo;
64 UInt a3_ucontext;
65 UChar lower_guardzone[512]; // put nothing here
66 VexGuestX86State vex;
67 VexGuestX86State vex_shadow1;
68 VexGuestX86State vex_shadow2;
69 vki_siginfo_t fake_siginfo;
70 struct vki_ucontext fake_ucontext;
71 UInt magicPI;
72 UInt sigNo_private;
73 vki_sigset_t mask; // saved sigmask; restore when hdlr returns
74 UInt __pad[3];
75 UChar upper_guardzone[512]; // put nothing here
76 // and don't zero it, since that might overwrite the client's
77 // stack redzone, at least on archs which have one
80 /* Create a plausible-looking sigcontext from the thread's
81 Vex guest state. NOTE: does not fill in the FP or SSE
82 bits of sigcontext at the moment.
84 static void synthesize_ucontext(ThreadState *tst,
85 struct vki_ucontext *uc,
86 const struct vki_ucontext *siguc)
88 VG_(memset)(uc, 0, sizeof(*uc));
90 if (siguc) uc->uc_sigmask = siguc->uc_sigmask;
91 uc->uc_stack = tst->altstack;
92 uc->uc_mcontext = &uc->__mcontext_data;
94 # define SC2(reg,REG) uc->__mcontext_data.__ss.reg = tst->arch.vex.guest_##REG
95 SC2(__edi,EDI);
96 SC2(__esi,ESI);
97 SC2(__ebp,EBP);
98 SC2(__ebx,EBX);
99 SC2(__edx,EDX);
100 SC2(__eax,EAX);
101 SC2(__ecx,ECX);
102 SC2(__esp,ESP);
103 SC2(__eip,EIP);
104 uc->__mcontext_data.__ss.__eflags = LibVEX_GuestX86_get_eflags(&tst->arch.vex);
106 if (siguc)
107 uc->__mcontext_data.__es = siguc->__mcontext_data.__es;
108 # undef SC2
111 static void restore_from_ucontext(ThreadState *tst,
112 const struct vki_ucontext *uc)
114 # define SC2(REG,reg) tst->arch.vex.guest_##REG = uc->__mcontext_data.__ss.reg
115 SC2(EDI,__edi);
116 SC2(ESI,__esi);
117 SC2(EBP,__ebp);
118 SC2(EBX,__ebx);
119 SC2(EDX,__edx);
120 SC2(EAX,__eax);
121 SC2(ECX,__ecx);
122 SC2(ESP,__esp);
123 SC2(EIP,__eip);
124 /* There doesn't seem to be an easy way to restore eflags */
125 # undef SC2
128 /* Create a signal frame for thread 'tid'. Make a 3-arg frame
129 regardless of whether the client originally requested a 1-arg
130 version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
131 former case, the x86 calling conventions will simply cause the
132 extra 2 args to be ignored (inside the handler). */
133 void VG_(sigframe_create) ( ThreadId tid,
134 Bool on_altstack,
135 Addr sp_top_of_frame,
136 const vki_siginfo_t *siginfo,
137 const struct vki_ucontext *siguc,
138 void *handler,
139 UInt flags,
140 const vki_sigset_t *mask,
141 void *restorer )
143 ThreadState* tst;
144 Addr esp;
145 struct hacky_sigframe* frame;
146 Int sigNo = siginfo->si_signo;
148 vg_assert(VG_IS_16_ALIGNED(sizeof(struct hacky_sigframe)));
150 sp_top_of_frame &= ~0xf;
151 esp = sp_top_of_frame - sizeof(struct hacky_sigframe);
152 esp -= 4; /* ELF ABI says that esp+4 must be 16 aligned on
153 entry to a function. */
155 tst = VG_(get_ThreadState)(tid);
156 if (! ML_(sf_maybe_extend_stack)(tst, esp, sp_top_of_frame - esp, flags))
157 return;
159 vg_assert(VG_IS_16_ALIGNED(esp+4));
161 frame = (struct hacky_sigframe *) esp;
163 /* clear it (very conservatively) */
164 VG_(memset)(&frame->lower_guardzone, 0, sizeof frame->lower_guardzone);
165 VG_(memset)(&frame->vex, 0, sizeof(VexGuestX86State));
166 VG_(memset)(&frame->vex_shadow1, 0, sizeof(VexGuestX86State));
167 VG_(memset)(&frame->vex_shadow2, 0, sizeof(VexGuestX86State));
168 VG_(memset)(&frame->fake_siginfo, 0, sizeof(frame->fake_siginfo));
169 VG_(memset)(&frame->fake_ucontext, 0, sizeof(frame->fake_ucontext));
171 /* save stuff in frame */
172 frame->vex = tst->arch.vex;
173 frame->vex_shadow1 = tst->arch.vex_shadow1;
174 frame->vex_shadow2 = tst->arch.vex_shadow2;
175 frame->sigNo_private = sigNo;
176 frame->mask = tst->sig_mask;
177 frame->magicPI = 0x31415927;
179 /* Fill in the siginfo and ucontext. */
180 synthesize_ucontext(tst, &frame->fake_ucontext, siguc);
181 frame->fake_siginfo = *siginfo;
183 /* Set up stack pointer */
184 vg_assert(esp == (Addr)&frame->returnAddr);
185 VG_(set_SP)(tid, esp);
186 VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_STACK_PTR, sizeof(UInt));
188 /* Set up program counter */
189 VG_(set_IP)(tid, (UInt)handler);
190 VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_INSTR_PTR, sizeof(UInt));
192 /* Set up RA and args for the frame */
193 VG_TRACK( pre_mem_write, Vg_CoreSignal, tid, "signal handler frame",
194 (Addr)frame, 4*sizeof(UInt) );
195 frame->returnAddr = (UInt)&VG_(x86_darwin_SUBST_FOR_sigreturn);
197 frame->a1_signo = sigNo;
198 frame->a2_siginfo = (UInt) &frame->fake_siginfo;
199 frame->a3_ucontext = (UInt) &frame->fake_ucontext;
201 VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
202 (Addr)frame, 4*sizeof(UInt) );
203 VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
204 (Addr)&frame->fake_siginfo, sizeof(frame->fake_siginfo));
205 VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
206 (Addr)&frame->fake_ucontext, sizeof(frame->fake_ucontext));
208 if (VG_(clo_trace_signals))
209 VG_(message)(Vg_DebugMsg,
210 "sigframe_create (thread %u): "
211 "next EIP=%#lx, next ESP=%#lx\n",
212 tid, (Addr)handler, (Addr)frame );
216 /* Remove a signal frame from thread 'tid's stack, and restore the CPU
217 state from it. Note, isRT is irrelevant here. */
218 void VG_(sigframe_destroy)( ThreadId tid, Bool isRT )
220 ThreadState *tst;
221 Addr esp;
222 Int sigNo;
223 struct hacky_sigframe* frame;
225 vg_assert(VG_(is_valid_tid)(tid));
226 tst = VG_(get_ThreadState)(tid);
228 /* Check that the stack frame looks valid */
229 esp = VG_(get_SP)(tid);
231 /* why -4 ? because the signal handler's return will have popped
232 the return address off the stack; and the return address is the
233 lowest-addressed element of hacky_sigframe. */
234 frame = (struct hacky_sigframe*)(esp - 4);
235 vg_assert(frame->magicPI == 0x31415927);
237 /* This +4 is because of the -4 referred to in the ELF ABI comment
238 in VG_(sigframe_create) just above. */
239 vg_assert(VG_IS_16_ALIGNED((Addr)frame + 4));
241 /* restore the entire guest state, and shadows, from the frame. */
242 tst->arch.vex = frame->vex;
243 tst->arch.vex_shadow1 = frame->vex_shadow1;
244 tst->arch.vex_shadow2 = frame->vex_shadow2;
245 restore_from_ucontext(tst, &frame->fake_ucontext);
247 tst->sig_mask = frame->mask;
248 tst->tmp_sig_mask = frame->mask;
249 sigNo = frame->sigNo_private;
251 if (VG_(clo_trace_signals))
252 VG_(message)(Vg_DebugMsg,
253 "sigframe_destroy (thread %u): "
254 "valid magic; next EIP=%#x\n",
255 tid, tst->arch.vex.guest_EIP);
257 VG_TRACK( die_mem_stack_signal,
258 (Addr)frame - VG_STACK_REDZONE_SZB,
259 sizeof(struct hacky_sigframe) );
261 /* tell the tools */
262 VG_TRACK( post_deliver_signal, tid, sigNo );
265 #endif // defined(VGP_x86_darwin)
267 /*--------------------------------------------------------------------*/
268 /*--- end ---*/
269 /*--------------------------------------------------------------------*/