drd/tests/swapcontext: Improve the portability of this test further
[valgrind.git] / coregrind / m_sigframe / sigframe-amd64-darwin.c
blobb29413125be51330c7893b52b446cf16f0e3ba9f
2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames. ---*/
4 /*--- sigframe-amd64-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_amd64_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 amd64-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 word looks like a call to a 3-arg amd64-ELF function */
61 ULong returnAddr;
62 UChar lower_guardzone[512]; // put nothing here
63 VexGuestAMD64State vex;
64 VexGuestAMD64State vex_shadow1;
65 VexGuestAMD64State vex_shadow2;
66 vki_siginfo_t fake_siginfo;
67 struct vki_ucontext fake_ucontext;
68 UInt magicPI;
69 UInt sigNo_private;
70 vki_sigset_t mask; // saved sigmask; restore when hdlr returns
71 UChar upper_guardzone[512]; // put nothing here
72 // and don't zero it, since that might overwrite the client's
73 // stack redzone, at least on archs which have one
76 /* Create a plausible-looking sigcontext from the thread's
77 Vex guest state. NOTE: does not fill in the FP or SSE
78 bits of sigcontext at the moment.
80 static void synthesize_ucontext(ThreadState *tst,
81 struct vki_ucontext *uc,
82 const struct vki_ucontext *siguc)
84 VG_(memset)(uc, 0, sizeof(*uc));
86 if (siguc) uc->uc_sigmask = siguc->uc_sigmask;
87 uc->uc_stack = tst->altstack;
88 uc->uc_mcontext = &uc->__mcontext_data;
90 # define SC2(reg,REG) uc->__mcontext_data.__ss.reg = tst->arch.vex.guest_##REG
91 SC2(__r8,R8);
92 SC2(__r9,R9);
93 SC2(__r10,R10);
94 SC2(__r11,R11);
95 SC2(__r12,R12);
96 SC2(__r13,R13);
97 SC2(__r14,R14);
98 SC2(__r15,R15);
99 SC2(__rdi,RDI);
100 SC2(__rsi,RSI);
101 SC2(__rbp,RBP);
102 SC2(__rbx,RBX);
103 SC2(__rdx,RDX);
104 SC2(__rax,RAX);
105 SC2(__rcx,RCX);
106 SC2(__rsp,RSP);
107 SC2(__rip,RIP);
108 uc->__mcontext_data.__ss.__rflags = LibVEX_GuestAMD64_get_rflags(&tst->arch.vex);
110 if (siguc)
111 uc->__mcontext_data.__es = siguc->__mcontext_data.__es;
112 # undef SC2
115 static void restore_from_ucontext(ThreadState *tst,
116 const struct vki_ucontext *uc)
118 # define SC2(REG,reg) tst->arch.vex.guest_##REG = uc->__mcontext_data.__ss.reg
119 SC2(R8,__r8);
120 SC2(R9,__r9);
121 SC2(R10,__r10);
122 SC2(R11,__r11);
123 SC2(R12,__r12);
124 SC2(R13,__r13);
125 SC2(R14,__r14);
126 SC2(R15,__r15);
127 SC2(RDI,__rdi);
128 SC2(RSI,__rsi);
129 SC2(RBP,__rbp);
130 SC2(RBX,__rbx);
131 SC2(RDX,__rdx);
132 SC2(RAX,__rax);
133 SC2(RCX,__rcx);
134 SC2(RSP,__rsp);
135 SC2(RIP,__rip);
136 /* There doesn't seem to be an easy way to restore rflags */
137 # undef SC2
140 /* Create a signal frame for thread 'tid'. Make a 3-arg frame
141 regardless of whether the client originally requested a 1-arg
142 version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
143 former case, the amd64 calling conventions will simply cause the
144 extra 2 args to be ignored (inside the handler). (We hope!) */
145 void VG_(sigframe_create) ( ThreadId tid,
146 Bool on_altstack,
147 Addr sp_top_of_frame,
148 const vki_siginfo_t *siginfo,
149 const struct vki_ucontext *siguc,
150 void *handler,
151 UInt flags,
152 const vki_sigset_t *mask,
153 void *restorer )
155 ThreadState* tst;
156 Addr rsp;
157 struct hacky_sigframe* frame;
158 Int sigNo = siginfo->si_signo;
160 vg_assert(VG_IS_16_ALIGNED(sizeof(struct hacky_sigframe)));
162 sp_top_of_frame &= ~0xfUL;
163 rsp = sp_top_of_frame - sizeof(struct hacky_sigframe);
164 rsp -= 8; /* ELF ABI says that rsp+8 must be 16 aligned on
165 entry to a function. */
167 tst = VG_(get_ThreadState)(tid);
168 if (! ML_(sf_maybe_extend_stack)(tst, rsp, sp_top_of_frame - rsp, flags))
169 return;
171 vg_assert(VG_IS_16_ALIGNED(rsp+8));
173 frame = (struct hacky_sigframe *) rsp;
175 /* clear it (very conservatively) */
176 VG_(memset)(&frame->lower_guardzone, 0, sizeof frame->lower_guardzone);
177 VG_(memset)(&frame->vex, 0, sizeof(VexGuestAMD64State));
178 VG_(memset)(&frame->vex_shadow1, 0, sizeof(VexGuestAMD64State));
179 VG_(memset)(&frame->vex_shadow2, 0, sizeof(VexGuestAMD64State));
180 VG_(memset)(&frame->fake_siginfo, 0, sizeof(frame->fake_siginfo));
181 VG_(memset)(&frame->fake_ucontext, 0, sizeof(frame->fake_ucontext));
183 /* save stuff in frame */
184 frame->vex = tst->arch.vex;
185 frame->vex_shadow1 = tst->arch.vex_shadow1;
186 frame->vex_shadow2 = tst->arch.vex_shadow2;
187 frame->sigNo_private = sigNo;
188 frame->mask = tst->sig_mask;
189 frame->magicPI = 0x31415927;
191 /* Fill in the siginfo and ucontext. */
192 synthesize_ucontext(tst, &frame->fake_ucontext, siguc);
193 frame->fake_siginfo = *siginfo;
195 /* Set up stack pointer */
196 vg_assert(rsp == (Addr)&frame->returnAddr);
197 VG_(set_SP)(tid, rsp);
198 VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_STACK_PTR, sizeof(ULong));
200 /* Set up program counter */
201 VG_(set_IP)(tid, (ULong)handler);
202 VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_INSTR_PTR, sizeof(ULong));
204 /* Set up RA and args for the frame */
205 VG_TRACK( pre_mem_write, Vg_CoreSignal, tid, "signal handler frame",
206 (Addr)frame, 1*sizeof(ULong) );
207 frame->returnAddr = (ULong)&VG_(amd64_darwin_SUBST_FOR_sigreturn);
209 /* XXX should tell the tool that these regs got written */
210 tst->arch.vex.guest_RDI = (ULong) sigNo;
211 tst->arch.vex.guest_RSI = (Addr) &frame->fake_siginfo;
212 tst->arch.vex.guest_RDX = (Addr) &frame->fake_ucontext;
214 VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
215 (Addr)frame, 1*sizeof(ULong) );
216 VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
217 (Addr)&frame->fake_siginfo, sizeof(frame->fake_siginfo));
218 VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
219 (Addr)&frame->fake_ucontext, sizeof(frame->fake_ucontext));
221 if (VG_(clo_trace_signals))
222 VG_(message)(Vg_DebugMsg,
223 "sigframe_create (thread %u): "
224 "next RIP=%#lx, next RSP=%#lx\n",
225 tid, (Addr)handler, (Addr)frame );
229 /* Remove a signal frame from thread 'tid's stack, and restore the CPU
230 state from it. Note, isRT is irrelevant here. */
231 void VG_(sigframe_destroy)( ThreadId tid, Bool isRT )
233 ThreadState *tst;
234 Addr rsp;
235 Int sigNo;
236 struct hacky_sigframe* frame;
238 vg_assert(VG_(is_valid_tid)(tid));
239 tst = VG_(get_ThreadState)(tid);
241 /* Check that the stack frame looks valid */
242 rsp = VG_(get_SP)(tid);
244 /* why -8 ? because the signal handler's return will have popped
245 the return address off the stack; and the return address is the
246 lowest-addressed element of hacky_sigframe. */
247 frame = (struct hacky_sigframe*)(rsp - 8);
248 vg_assert(frame->magicPI == 0x31415927);
250 /* This +8 is because of the -8 referred to in the ELF ABI comment
251 in VG_(sigframe_create) just above. */
252 vg_assert(VG_IS_16_ALIGNED((Addr)frame + 8));
254 /* restore the entire guest state, and shadows, from the frame. */
255 tst->arch.vex = frame->vex;
256 tst->arch.vex_shadow1 = frame->vex_shadow1;
257 tst->arch.vex_shadow2 = frame->vex_shadow2;
258 restore_from_ucontext(tst, &frame->fake_ucontext);
260 tst->sig_mask = frame->mask;
261 tst->tmp_sig_mask = frame->mask;
262 sigNo = frame->sigNo_private;
264 if (VG_(clo_trace_signals))
265 VG_(message)(Vg_DebugMsg,
266 "sigframe_destroy (thread %d): "
267 "valid magic; next RIP=%#llx\n",
268 tid, tst->arch.vex.guest_RIP);
270 VG_TRACK( die_mem_stack_signal,
271 (Addr)frame - VG_STACK_REDZONE_SZB,
272 sizeof(struct hacky_sigframe) );
274 /* tell the tools */
275 VG_TRACK( post_deliver_signal, tid, sigNo );
278 #endif // defined(VGP_amd64_darwin)
280 /*--------------------------------------------------------------------*/
281 /*--- end sigframe-amd64-darwin.c ---*/
282 /*--------------------------------------------------------------------*/