2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames. ---*/
4 /*--- sigframe-solaris.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2011-2014 Petr Pavlu
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, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 The GNU General Public License is contained in the file COPYING.
32 #if defined(VGP_x86_solaris) || defined(VGP_amd64_solaris)
34 #include "pub_core_basics.h"
35 #include "pub_core_vki.h"
36 #include "pub_core_threadstate.h"
37 #include "pub_core_aspacemgr.h"
38 #include "pub_core_libcassert.h"
39 #include "pub_core_libcbase.h"
40 #include "pub_core_libcprint.h"
41 #include "pub_core_machine.h"
42 #include "pub_core_options.h"
43 #include "pub_core_signals.h"
44 #include "pub_core_tooliface.h"
45 #include "pub_core_sigframe.h" /* Self */
46 #include "pub_core_syswrap.h"
47 #include "priv_sigframe.h"
49 /* This module creates and removes signal frames for signal deliveries
50 on x86/amd64-solaris. */
52 /* Create a signal frame for thread 'tid'. Make a 3-arg frame regardless of
53 whether the client originally requested a 1-arg version (no SA_SIGINFO) or
54 a 3-arg one (SA_SIGINFO) since in the former case, the x86/amd64 calling
55 conventions will simply cause the extra 2 args to be ignored (inside the
57 void VG_(sigframe_create
)(ThreadId tid
, Bool on_altstack
,
58 Addr sp_top_of_frame
, const vki_siginfo_t
*siginfo
,
59 const struct vki_ucontext
*siguc
,
60 void *handler
, UInt flags
, const vki_sigset_t
*mask
,
63 ThreadState
*tst
= VG_(get_ThreadState
)(tid
);
65 vki_sigframe_t
*frame
;
66 Int signo
= siginfo
->si_signo
;
68 /* Calculate new stack pointer. */
69 esp
= sp_top_of_frame
- sizeof(vki_sigframe_t
);
70 esp
= VG_ROUNDDN(esp
, 16) - sizeof(UWord
);
72 if (!ML_(sf_maybe_extend_stack
)(tst
, esp
, sp_top_of_frame
- esp
, flags
))
75 /* Fill in the frame. */
76 frame
= (vki_sigframe_t
*)esp
;
78 /* Set a bogus return address. This return address should be never used
79 because to return from a signal handler a program has to call
80 setcontext() explicitly. */
81 frame
->return_addr
= (void*)~0UL;
83 /* Save current context. (This has to be done before the thread state is
84 modified in any way.) */
85 VG_(save_context
)(tid
, &frame
->ucontext
, Vg_CoreSignal
);
87 /* Fill in the siginfo. */
88 frame
->siginfo
= *siginfo
;
89 /* Set expected si_addr value.
91 Manual page siginfo.h(3HEAD) describes that some signals define si_addr
92 to be an address of the faulting instruction (SIGILL). Then it is needed
93 to change the real CPU address to the VCPU address. Some signals define
94 si_addr to be an address of the faulting memory reference (SIGSEGV,
95 SIGBUS). Then the address should be passed unmodified.
97 However documentation contained in the manpage does not reflect the
98 reality found in the Solaris kernel - uts/<arch>/os/trap.c. Here one can
99 observe that in some cases si_addr is set to address provided by the
100 underlying subsystem. In some cases si_addr is set to the current
101 program counter. Other signals are missing documentation altogether.
102 It is almost impossible to determine what value is stored in si_addr
103 based on the information provided by kernel to the signal handler.
105 POSIX.1-2008 says about si_addr:
106 SIGILL, SIGFPE ... Address of faulting instruction.
107 SIGSEGV, SIGBUS ... Address of faulting memory reference.
108 For some implementations, the value of si_addr may be inaccurate.
110 See tests none/tests/faultstatus and none/tests/x86/badseg for examples.
111 The code below simply follows the POSIX standard, but propagates any
112 possibly incorrect values from the kernel to the user.
116 switch (siginfo
->si_code
) {
117 case VKI_SEGV_ACCERR
:
118 case VKI_SEGV_MAPERR
:
121 case VKI_SEGV_MADE_UP_GPF
:
122 /* Translate si_code synthesized by Valgrind to SEGV_MAPPER. */
123 frame
->siginfo
.si_code
= VKI_SEGV_MAPERR
;
132 frame
->siginfo
.si_addr
= (void*)VG_(get_IP
)(tid
);
135 frame
->siginfo
.si_faddr
= (void*)VG_(get_IP
)(tid
);
140 VG_TRACK(post_mem_write
, Vg_CoreSignal
, tid
, (Addr
)&frame
->siginfo
,
141 sizeof(frame
->siginfo
));
143 /* Save the signal number in an unused slot. Later, when a return from the
144 signal is made, this value is used to inform the tool that the
145 processing for the given signal has ended. */
146 VKI_UC_SIGNO(&frame
->ucontext
) = signo
| ((~(UWord
)signo
& 0xFFFF) << 16);
147 /* Old context has to point to the saved ucontext. */
148 tst
->os_state
.oldcontext
= &frame
->ucontext
;
149 /* Save ERR and TRAPNO if siguc is present. */
151 frame
->ucontext
.uc_mcontext
.gregs
[VKI_REG_ERR
]
152 = siguc
->uc_mcontext
.gregs
[VKI_REG_ERR
];
153 VG_TRACK(post_mem_write
, Vg_CoreSignal
, tid
,
154 (Addr
)&frame
->ucontext
.uc_mcontext
.gregs
[VKI_REG_ERR
],
156 frame
->ucontext
.uc_mcontext
.gregs
[VKI_REG_TRAPNO
]
157 = siguc
->uc_mcontext
.gregs
[VKI_REG_TRAPNO
];
158 VG_TRACK(post_mem_write
, Vg_CoreSignal
, tid
,
159 (Addr
)&frame
->ucontext
.uc_mcontext
.gregs
[VKI_REG_TRAPNO
],
163 /* Prepare parameters for a signal handler. */
164 frame
->a1_signo
= signo
;
165 /* The first parameter has to be 16-byte aligned, resembling function
169 vg_assert(VG_IS_16_ALIGNED(&frame->a1_signo));
170 seems to get miscompiled on amd64 with GCC 4.7.2. */
171 Addr signo_addr
= (Addr
)&frame
->a1_signo
;
172 vg_assert(VG_IS_16_ALIGNED(signo_addr
));
174 frame
->a2_siginfo
= &frame
->siginfo
;
175 VG_TRACK(post_mem_write
, Vg_CoreSignal
, tid
, (Addr
)&frame
->a1_signo
,
176 sizeof(frame
->a1_signo
) + sizeof(frame
->a2_siginfo
));
177 #if defined(VGP_x86_solaris)
178 frame
->a3_ucontext
= &frame
->ucontext
;
179 VG_TRACK(post_mem_write
, Vg_CoreSignal
, tid
, (Addr
)&frame
->a3_ucontext
,
180 sizeof(frame
->a3_ucontext
));
181 #elif defined(VGP_amd64_solaris)
182 tst
->arch
.vex
.guest_RDI
= signo
;
183 VG_TRACK(post_reg_write
, Vg_CoreSignal
, tid
, offsetof(VexGuestAMD64State
,
184 guest_RDI
), sizeof(ULong
));
185 tst
->arch
.vex
.guest_RSI
= (Addr
)&frame
->siginfo
;
186 VG_TRACK(post_reg_write
, Vg_CoreSignal
, tid
, offsetof(VexGuestAMD64State
,
187 guest_RSI
), sizeof(ULong
));
188 tst
->arch
.vex
.guest_RDX
= (Addr
)&frame
->ucontext
;
189 VG_TRACK(post_reg_write
, Vg_CoreSignal
, tid
, offsetof(VexGuestAMD64State
,
190 guest_RDX
), sizeof(ULong
));
193 /* Set up the stack pointer. */
194 vg_assert(esp
== (Addr
)&frame
->return_addr
);
195 VG_(set_SP
)(tid
, esp
);
196 VG_TRACK(post_reg_write
, Vg_CoreSignal
, tid
, VG_O_STACK_PTR
, sizeof(Addr
));
198 /* Set up the program counter. Note that we don't inform a tool about IP
199 write because IP is always defined. */
200 VG_(set_IP
)(tid
, (Addr
)handler
);
202 /* If the signal is delivered on the alternate stack, copy it out to
203 ustack. This has to be done after setting a new IP so the SS_ONSTACK
204 flag is set by VG_(do_sys_sigaltstack)(). */
205 if (on_altstack
&& tst
->os_state
.ustack
206 && VG_(am_is_valid_for_client
)((Addr
)tst
->os_state
.ustack
,
207 sizeof(*tst
->os_state
.ustack
),
210 vki_stack_t altstack
;
212 /* Get information about alternate stack. */
213 res
= VG_(do_sys_sigaltstack
)(tid
, NULL
, &altstack
);
214 vg_assert(!sr_isError(res
));
216 /* Copy it to ustack. */
217 *tst
->os_state
.ustack
= altstack
;
218 VG_TRACK(post_mem_write
, Vg_CoreSignal
, tid
, (Addr
)tst
->os_state
.ustack
,
219 sizeof(*tst
->os_state
.ustack
));
222 if (VG_(clo_trace_signals
))
223 VG_(message
)(Vg_DebugMsg
,
224 "sigframe_create (thread %d): next IP=%#lx, "
226 tid
, (Addr
)handler
, (Addr
)frame
);
229 void VG_(sigframe_destroy
)(ThreadId tid
, Bool isRT
)
231 /* Not used on Solaris. */
235 void VG_(sigframe_return
)(ThreadId tid
, const vki_ucontext_t
*uc
)
239 /* Check if a signal number was saved in the restored context. */
240 signo
= VKI_UC_SIGNO_CONST(uc
) & 0xFFFF;
241 if (!signo
|| signo
!= ((~VKI_UC_SIGNO_CONST(uc
) >> 16) & 0xFFFF))
244 /* Note: The active tool should be informed here about the dead stack area.
245 However, this was already done when the original context was restored (in
246 VG_(restore_context)()) so it is not necessary to do it here again.
248 There is a small nuance though, VG_(restore_context)() triggers the
249 die_mem_stack event while in this case, it should really trigger the
250 die_mem_stack_signal event. This is not currently a problem because all
251 official tools handle these two events in the same way.
253 If a return from an alternate stack is made then no die_mem_stack event
254 is currently triggered. */
256 /* Returning from a signal handler. */
257 if (VG_(clo_trace_signals
))
258 VG_(message
)(Vg_DebugMsg
,
259 "sigframe_return (thread %d): IP=%#lx\n",
260 tid
, VG_(get_IP
)(tid
));
263 VG_TRACK(post_deliver_signal
, tid
, signo
);
266 #endif // defined(VGP_x86_solaris) || defined(VGP_amd64_solaris)
268 /*--------------------------------------------------------------------*/
270 /*--------------------------------------------------------------------*/