sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | ed6b824 | 2005-06-01 00:03:17 +0000 | [diff] [blame] | 3 | /*--- Implementation of POSIX signals. m_signals.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
njn | ed6b824 | 2005-06-01 00:03:17 +0000 | [diff] [blame] | 5 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2008 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 31 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 32 | Signal handling. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 33 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 34 | There are 4 distinct classes of signal: |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 35 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 36 | 1. Synchronous, instruction-generated (SIGILL, FPE, BUS, SEGV and |
| 37 | TRAP): these are signals as a result of an instruction fault. If |
| 38 | we get one while running client code, then we just do the |
| 39 | appropriate thing. If it happens while running Valgrind code, then |
| 40 | it indicates a Valgrind bug. Note that we "manually" implement |
| 41 | automatic stack growth, such that if a fault happens near the |
| 42 | client process stack, it is extended in the same way the kernel |
| 43 | would, and the fault is never reported to the client program. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 44 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 45 | 2. Asynchronous varients of the above signals: If the kernel tries |
| 46 | to deliver a sync signal while it is blocked, it just kills the |
| 47 | process. Therefore, we can't block those signals if we want to be |
| 48 | able to report on bugs in Valgrind. This means that we're also |
| 49 | open to receiving those signals from other processes, sent with |
| 50 | kill. We could get away with just dropping them, since they aren't |
| 51 | really signals that processes send to each other. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 52 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 53 | 3. Synchronous, general signals. If a thread/process sends itself |
| 54 | a signal with kill, its expected to be synchronous: ie, the signal |
| 55 | will have been delivered by the time the syscall finishes. |
| 56 | |
| 57 | 4. Asyncronous, general signals. All other signals, sent by |
| 58 | another process with kill. These are generally blocked, except for |
| 59 | two special cases: we poll for them each time we're about to run a |
| 60 | thread for a time quanta, and while running blocking syscalls. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 61 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 62 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 63 | In addition, we define two signals for internal use: SIGVGCHLD and |
| 64 | SIGVGKILL. SIGVGCHLD is used to indicate thread death to any |
| 65 | reaping thread (the master thread). It is always blocked and never |
| 66 | delivered as a signal; it is always polled with sigtimedwait. |
| 67 | |
| 68 | SIGVGKILL is used to terminate threads. When one thread wants |
| 69 | another to exit, it will set its exitreason and send it SIGVGKILL |
| 70 | if it appears to be blocked in a syscall. |
| 71 | |
| 72 | |
| 73 | We use a kernel thread for each application thread. When the |
| 74 | thread allows itself to be open to signals, it sets the thread |
| 75 | signal mask to what the client application set it to. This means |
| 76 | that we get the kernel to do all signal routing: under Valgrind, |
| 77 | signals get delivered in the same way as in the non-Valgrind case |
| 78 | (the exception being for the sync signal set, since they're almost |
| 79 | always unblocked). |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 80 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 81 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 82 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 83 | #include "pub_core_vki.h" |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 84 | #include "pub_core_vkiscnums.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 85 | #include "pub_core_debuglog.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 86 | #include "pub_core_threadstate.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 87 | #include "pub_core_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 88 | #include "pub_core_clientstate.h" |
njn | 2521d32 | 2005-05-08 14:45:13 +0000 | [diff] [blame] | 89 | #include "pub_core_aspacemgr.h" |
njn | 634a652 | 2005-07-02 01:59:28 +0000 | [diff] [blame] | 90 | #include "pub_core_debugger.h" // For VG_(start_debugger) |
njn | d2b1711 | 2005-04-19 04:10:25 +0000 | [diff] [blame] | 91 | #include "pub_core_errormgr.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 92 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 93 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 94 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 95 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 96 | #include "pub_core_libcsignal.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 97 | #include "pub_core_machine.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 98 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 99 | #include "pub_core_options.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 100 | #include "pub_core_scheduler.h" |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 101 | #include "pub_core_signals.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 102 | #include "pub_core_sigframe.h" // For VG_(sigframe_create)() |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 103 | #include "pub_core_stacks.h" // For VG_(change_stack)() |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 104 | #include "pub_core_stacktrace.h" // For VG_(get_and_pp_StackTrace)() |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 105 | #include "pub_core_syscall.h" |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 106 | #include "pub_core_syswrap.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 107 | #include "pub_core_tooliface.h" |
sewardj | cf4ac71 | 2005-11-01 00:03:40 +0000 | [diff] [blame] | 108 | #include "pub_core_coredump.h" |
sewardj | 985fabb | 2005-04-24 14:18:14 +0000 | [diff] [blame] | 109 | |
njn | d2b1711 | 2005-04-19 04:10:25 +0000 | [diff] [blame] | 110 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 111 | /* --------------------------------------------------------------------- |
| 112 | Forwards decls. |
| 113 | ------------------------------------------------------------------ */ |
| 114 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 115 | static void sync_signalhandler ( Int sigNo, vki_siginfo_t *info, struct vki_ucontext * ); |
| 116 | static void async_signalhandler ( Int sigNo, vki_siginfo_t *info, struct vki_ucontext * ); |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 117 | static void sigvgkill_handler ( Int sigNo, vki_siginfo_t *info, struct vki_ucontext * ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 118 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 119 | static const Char *signame(Int sigNo); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 120 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 121 | /* Maximum usable signal. */ |
| 122 | Int VG_(max_signal) = _VKI_NSIG; |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 123 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 124 | #define N_QUEUED_SIGNALS 8 |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 125 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 126 | typedef struct SigQueue { |
| 127 | Int next; |
| 128 | vki_siginfo_t sigs[N_QUEUED_SIGNALS]; |
| 129 | } SigQueue; |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 130 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 131 | /* ------ Macros for pulling stuff out of ucontexts ------ */ |
| 132 | |
njn | 605f488 | 2005-05-29 17:50:40 +0000 | [diff] [blame] | 133 | #if defined(VGP_x86_linux) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 134 | # define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.eip) |
| 135 | # define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.esp) |
| 136 | # define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.ebp) |
| 137 | # define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_mcontext.eax) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 138 | # define VG_UCONTEXT_SYSCALL_SYSRES(uc) \ |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 139 | /* Convert the value in uc_mcontext.eax into a SysRes. */ \ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 140 | VG_(mk_SysRes_x86_linux)( (uc)->uc_mcontext.eax ) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 141 | # define VG_UCONTEXT_LINK_REG(uc) 0 /* Dude, where's my LR? */ |
sewardj | e7aa4ae | 2005-06-09 12:43:42 +0000 | [diff] [blame] | 142 | |
njn | 605f488 | 2005-05-29 17:50:40 +0000 | [diff] [blame] | 143 | #elif defined(VGP_amd64_linux) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 144 | # define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.rip) |
| 145 | # define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.rsp) |
| 146 | # define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.rbp) |
| 147 | # define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_mcontext.rax) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 148 | # define VG_UCONTEXT_SYSCALL_SYSRES(uc) \ |
sewardj | e7aa4ae | 2005-06-09 12:43:42 +0000 | [diff] [blame] | 149 | /* Convert the value in uc_mcontext.rax into a SysRes. */ \ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 150 | VG_(mk_SysRes_amd64_linux)( (uc)->uc_mcontext.rax ) |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 151 | # define VG_UCONTEXT_LINK_REG(uc) 0 /* No LR on amd64 either */ |
sewardj | e7aa4ae | 2005-06-09 12:43:42 +0000 | [diff] [blame] | 152 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 153 | #elif defined(VGP_ppc32_linux) |
sewardj | a36dcfa | 2005-11-25 02:16:58 +0000 | [diff] [blame] | 154 | /* Comments from Paul Mackerras 25 Nov 05: |
| 155 | |
| 156 | > I'm tracking down a problem where V's signal handling doesn't |
| 157 | > work properly on a ppc440gx running 2.4.20. The problem is that |
| 158 | > the ucontext being presented to V's sighandler seems completely |
| 159 | > bogus. |
| 160 | |
| 161 | > V's kernel headers and hence ucontext layout are derived from |
| 162 | > 2.6.9. I compared include/asm-ppc/ucontext.h from 2.4.20 and |
| 163 | > 2.6.13. |
| 164 | |
| 165 | > Can I just check my interpretation: the 2.4.20 one contains the |
| 166 | > uc_mcontext field in line, whereas the 2.6.13 one has a pointer |
| 167 | > to said struct? And so if V is using the 2.6.13 struct then a |
| 168 | > 2.4.20 one will make no sense to it. |
| 169 | |
| 170 | Not quite... what is inline in the 2.4.20 version is a |
| 171 | sigcontext_struct, not an mcontext. The sigcontext looks like |
| 172 | this: |
| 173 | |
| 174 | struct sigcontext_struct { |
| 175 | unsigned long _unused[4]; |
| 176 | int signal; |
| 177 | unsigned long handler; |
| 178 | unsigned long oldmask; |
| 179 | struct pt_regs *regs; |
| 180 | }; |
| 181 | |
| 182 | The regs pointer of that struct ends up at the same offset as the |
| 183 | uc_regs of the 2.6 struct ucontext, and a struct pt_regs is the |
| 184 | same as the mc_gregs field of the mcontext. In fact the integer |
| 185 | regs are followed in memory by the floating point regs on 2.4.20. |
| 186 | |
| 187 | Thus if you are using the 2.6 definitions, it should work on 2.4.20 |
| 188 | provided that you go via uc->uc_regs rather than looking in |
| 189 | uc->uc_mcontext directly. |
| 190 | |
| 191 | There is another subtlety: 2.4.20 doesn't save the vector regs when |
| 192 | delivering a signal, and 2.6.x only saves the vector regs if the |
| 193 | process has ever used an altivec instructions. If 2.6.x does save |
| 194 | the vector regs, it sets the MSR_VEC bit in |
| 195 | uc->uc_regs->mc_gregs[PT_MSR], otherwise it clears it. That bit |
| 196 | will always be clear under 2.4.20. So you can use that bit to tell |
| 197 | whether uc->uc_regs->mc_vregs is valid. */ |
| 198 | # define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_NIP]) |
| 199 | # define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_R1]) |
| 200 | # define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_R1]) |
| 201 | # define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_R0]) |
sewardj | 39a7c1d | 2005-11-24 03:54:38 +0000 | [diff] [blame] | 202 | # define VG_UCONTEXT_SYSCALL_SYSRES(uc) \ |
| 203 | /* Convert the values in uc_mcontext r3,cr into a SysRes. */ \ |
| 204 | VG_(mk_SysRes_ppc32_linux)( \ |
sewardj | a36dcfa | 2005-11-25 02:16:58 +0000 | [diff] [blame] | 205 | (uc)->uc_regs->mc_gregs[VKI_PT_R3], \ |
| 206 | (((uc)->uc_regs->mc_gregs[VKI_PT_CCR] >> 28) & 1) \ |
sewardj | 39a7c1d | 2005-11-24 03:54:38 +0000 | [diff] [blame] | 207 | ) |
sewardj | a36dcfa | 2005-11-25 02:16:58 +0000 | [diff] [blame] | 208 | # define VG_UCONTEXT_LINK_REG(uc) ((uc)->uc_regs->mc_gregs[VKI_PT_LNK]) |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 209 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 210 | #elif defined(VGP_ppc64_linux) |
| 211 | # define VG_UCONTEXT_INSTR_PTR(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_NIP]) |
| 212 | # define VG_UCONTEXT_STACK_PTR(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_R1]) |
| 213 | # define VG_UCONTEXT_FRAME_PTR(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_R1]) |
| 214 | # define VG_UCONTEXT_SYSCALL_NUM(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_R0]) |
sewardj | dcb6ec8 | 2006-01-19 03:44:10 +0000 | [diff] [blame] | 215 | /* Dubious hack: if there is an error, only consider the lowest 8 |
| 216 | bits of r3. memcheck/tests/post-syscall shows a case where an |
| 217 | interrupted syscall should have produced a ucontext with 0x4 |
| 218 | (VKI_EINTR) in r3 but is in fact producing 0x204. */ |
| 219 | /* Awaiting clarification from PaulM. Evidently 0x204 is |
| 220 | ERESTART_RESTARTBLOCK, which shouldn't have made it into user |
| 221 | space. */ |
| 222 | static inline SysRes VG_UCONTEXT_SYSCALL_SYSRES( struct vki_ucontext* uc ) |
| 223 | { |
| 224 | ULong err = (uc->uc_mcontext.gp_regs[VKI_PT_CCR] >> 28) & 1; |
| 225 | ULong r3 = uc->uc_mcontext.gp_regs[VKI_PT_R3]; |
| 226 | if (err) r3 &= 0xFF; |
| 227 | return VG_(mk_SysRes_ppc64_linux)( r3, err ); |
| 228 | } |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 229 | # define VG_UCONTEXT_LINK_REG(uc) ((uc)->uc_mcontext.gp_regs[VKI_PT_LNK]) |
| 230 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 231 | #elif defined(VGP_ppc32_aix5) |
| 232 | |
| 233 | /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */ |
| 234 | # include <ucontext.h> |
| 235 | /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */ |
| 236 | static inline Addr VG_UCONTEXT_INSTR_PTR( void* ucV ) { |
| 237 | ucontext_t* uc = (ucontext_t*)ucV; |
| 238 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 239 | struct mstsave* jc = &mc->jmp_context; |
| 240 | return jc->iar; |
| 241 | } |
| 242 | static inline Addr VG_UCONTEXT_STACK_PTR( void* ucV ) { |
| 243 | ucontext_t* uc = (ucontext_t*)ucV; |
| 244 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 245 | struct mstsave* jc = &mc->jmp_context; |
| 246 | return jc->gpr[1]; |
| 247 | } |
| 248 | static inline Addr VG_UCONTEXT_SYSCALL_NUM( void* ucV ) { |
| 249 | ucontext_t* uc = (ucontext_t*)ucV; |
| 250 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 251 | struct mstsave* jc = &mc->jmp_context; |
| 252 | return jc->gpr[2]; |
| 253 | } |
| 254 | static inline SysRes VG_UCONTEXT_SYSCALL_SYSRES( void* ucV ) { |
| 255 | ucontext_t* uc = (ucontext_t*)ucV; |
| 256 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 257 | struct mstsave* jc = &mc->jmp_context; |
| 258 | return VG_(mk_SysRes_ppc32_aix5)( jc->gpr[3], jc->gpr[4] ); |
| 259 | } |
| 260 | static inline Addr VG_UCONTEXT_LINK_REG( void* ucV ) { |
| 261 | ucontext_t* uc = (ucontext_t*)ucV; |
| 262 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 263 | struct mstsave* jc = &mc->jmp_context; |
| 264 | return jc->lr; |
| 265 | } |
| 266 | static inline Addr VG_UCONTEXT_FRAME_PTR( void* ucV ) { |
| 267 | return VG_UCONTEXT_STACK_PTR(ucV); |
| 268 | } |
| 269 | |
| 270 | #elif defined(VGP_ppc64_aix5) |
| 271 | |
| 272 | /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */ |
| 273 | # include <ucontext.h> |
| 274 | /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */ |
| 275 | static inline Addr VG_UCONTEXT_INSTR_PTR( void* ucV ) { |
| 276 | ucontext_t* uc = (ucontext_t*)ucV; |
| 277 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 278 | struct __context64* jc = &mc->jmp_context; |
| 279 | return jc->iar; |
| 280 | } |
| 281 | static inline Addr VG_UCONTEXT_STACK_PTR( void* ucV ) { |
| 282 | ucontext_t* uc = (ucontext_t*)ucV; |
| 283 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 284 | struct __context64* jc = &mc->jmp_context; |
| 285 | return jc->gpr[1]; |
| 286 | } |
| 287 | static inline Addr VG_UCONTEXT_SYSCALL_NUM( void* ucV ) { |
| 288 | ucontext_t* uc = (ucontext_t*)ucV; |
| 289 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 290 | struct __context64* jc = &mc->jmp_context; |
| 291 | return jc->gpr[2]; |
| 292 | } |
| 293 | static inline SysRes VG_UCONTEXT_SYSCALL_SYSRES( void* ucV ) { |
| 294 | ucontext_t* uc = (ucontext_t*)ucV; |
| 295 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 296 | struct __context64* jc = &mc->jmp_context; |
| 297 | return VG_(mk_SysRes_ppc32_aix5)( jc->gpr[3], jc->gpr[4] ); |
| 298 | } |
| 299 | static inline Addr VG_UCONTEXT_LINK_REG( void* ucV ) { |
| 300 | ucontext_t* uc = (ucontext_t*)ucV; |
| 301 | struct __jmpbuf* mc = &(uc->uc_mcontext); |
| 302 | struct __context64* jc = &mc->jmp_context; |
| 303 | return jc->lr; |
| 304 | } |
| 305 | static inline Addr VG_UCONTEXT_FRAME_PTR( void* ucV ) { |
| 306 | return VG_UCONTEXT_STACK_PTR(ucV); |
| 307 | } |
| 308 | |
| 309 | #else |
njn | 605f488 | 2005-05-29 17:50:40 +0000 | [diff] [blame] | 310 | # error Unknown platform |
| 311 | #endif |
| 312 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 313 | |
| 314 | /* ------ Macros for pulling stuff out of siginfos ------ */ |
| 315 | |
| 316 | /* These macros allow use of uniform names when working with |
| 317 | both the Linux and AIX vki definitions. */ |
| 318 | #if defined(VGO_linux) |
| 319 | # define VKI_SIGINFO_si_addr _sifields._sigfault._addr |
| 320 | # define VKI_SIGINFO_si_pid _sifields._kill._pid |
| 321 | #elif defined(VGO_aix5) |
| 322 | # define VKI_SIGINFO_si_addr si_addr |
| 323 | # define VKI_SIGINFO_si_pid si_pid |
| 324 | #else |
| 325 | # error Unknown OS |
| 326 | #endif |
| 327 | |
| 328 | |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 329 | /* --------------------------------------------------------------------- |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 330 | HIGH LEVEL STUFF TO DO WITH SIGNALS: POLICY (MOSTLY) |
| 331 | ------------------------------------------------------------------ */ |
| 332 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 333 | /* --------------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 334 | Signal state for this process. |
| 335 | ------------------------------------------------------------------ */ |
| 336 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 337 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 338 | /* Base-ment of these arrays[_VKI_NSIG]. |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 339 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 340 | Valid signal numbers are 1 .. _VKI_NSIG inclusive. |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 341 | Rather than subtracting 1 for indexing these arrays, which |
| 342 | is tedious and error-prone, they are simply dimensioned 1 larger, |
| 343 | and entry [0] is not used. |
| 344 | */ |
| 345 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 346 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 347 | /* ----------------------------------------------------- |
| 348 | Static client signal state (SCSS). This is the state |
| 349 | that the client thinks it has the kernel in. |
| 350 | SCSS records verbatim the client's settings. These |
| 351 | are mashed around only when SKSS is calculated from it. |
| 352 | -------------------------------------------------- */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 353 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 354 | typedef |
| 355 | struct { |
| 356 | void* scss_handler; /* VKI_SIG_DFL or VKI_SIG_IGN or ptr to |
| 357 | client's handler */ |
| 358 | UInt scss_flags; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 359 | vki_sigset_t scss_mask; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 360 | void* scss_restorer; /* where sigreturn goes */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 361 | } |
| 362 | SCSS_Per_Signal; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 363 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 364 | typedef |
| 365 | struct { |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 366 | /* per-signal info */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 367 | SCSS_Per_Signal scss_per_sig[1+_VKI_NSIG]; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 368 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 369 | /* Additional elements to SCSS not stored here: |
| 370 | - for each thread, the thread's blocking mask |
| 371 | - for each thread in WaitSIG, the set of waited-on sigs |
| 372 | */ |
| 373 | } |
| 374 | SCSS; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 375 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 376 | static SCSS scss; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 377 | |
| 378 | |
| 379 | /* ----------------------------------------------------- |
| 380 | Static kernel signal state (SKSS). This is the state |
| 381 | that we have the kernel in. It is computed from SCSS. |
| 382 | -------------------------------------------------- */ |
| 383 | |
| 384 | /* Let's do: |
| 385 | sigprocmask assigns to all thread masks |
| 386 | so that at least everything is always consistent |
| 387 | Flags: |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 388 | SA_SIGINFO -- we always set it, and honour it for the client |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 389 | SA_NOCLDSTOP -- passed to kernel |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 390 | SA_ONESHOT or SA_RESETHAND -- pass through |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 391 | SA_RESTART -- we observe this but set our handlers to always restart |
| 392 | SA_NOMASK or SA_NODEFER -- we observe this, but our handlers block everything |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 393 | SA_ONSTACK -- pass through |
| 394 | SA_NOCLDWAIT -- pass through |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 395 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 396 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 397 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 398 | typedef |
| 399 | struct { |
| 400 | void* skss_handler; /* VKI_SIG_DFL or VKI_SIG_IGN |
| 401 | or ptr to our handler */ |
| 402 | UInt skss_flags; |
| 403 | /* There is no skss_mask, since we know that we will always ask |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 404 | for all signals to be blocked in our sighandlers. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 405 | /* Also there is no skss_restorer. */ |
| 406 | } |
| 407 | SKSS_Per_Signal; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 408 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 409 | typedef |
| 410 | struct { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 411 | SKSS_Per_Signal skss_per_sig[1+_VKI_NSIG]; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 412 | } |
| 413 | SKSS; |
| 414 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 415 | static SKSS skss; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 416 | |
njn | 07ef6c0 | 2005-05-18 19:43:09 +0000 | [diff] [blame] | 417 | static Bool is_sig_ign(Int sigNo) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 418 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 419 | vg_assert(sigNo >= 1 && sigNo <= _VKI_NSIG); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 420 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 421 | return scss.scss_per_sig[sigNo].scss_handler == VKI_SIG_IGN; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 422 | } |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 423 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 424 | /* --------------------------------------------------------------------- |
| 425 | Compute the SKSS required by the current SCSS. |
| 426 | ------------------------------------------------------------------ */ |
| 427 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 428 | static |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 429 | void pp_SKSS ( void ) |
| 430 | { |
| 431 | Int sig; |
| 432 | VG_(printf)("\n\nSKSS:\n"); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 433 | for (sig = 1; sig <= _VKI_NSIG; sig++) { |
dirk | 6178038 | 2007-10-01 10:33:41 +0000 | [diff] [blame] | 434 | VG_(printf)("sig %d: handler %p, flags 0x%x\n", sig, |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 435 | skss.skss_per_sig[sig].skss_handler, |
| 436 | skss.skss_per_sig[sig].skss_flags ); |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 437 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 438 | } |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 439 | } |
| 440 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 441 | /* This is the core, clever bit. Computation is as follows: |
| 442 | |
| 443 | For each signal |
| 444 | handler = if client has a handler, then our handler |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 445 | else if client is DFL, then our handler as well |
| 446 | else (client must be IGN) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 447 | then hander is IGN |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 448 | */ |
| 449 | static |
| 450 | void calculate_SKSS_from_SCSS ( SKSS* dst ) |
| 451 | { |
| 452 | Int sig; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 453 | UInt scss_flags; |
| 454 | UInt skss_flags; |
| 455 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 456 | for (sig = 1; sig <= _VKI_NSIG; sig++) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 457 | void *skss_handler; |
| 458 | void *scss_handler; |
| 459 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 460 | scss_handler = scss.scss_per_sig[sig].scss_handler; |
| 461 | scss_flags = scss.scss_per_sig[sig].scss_flags; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 462 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 463 | switch(sig) { |
| 464 | case VKI_SIGSEGV: |
| 465 | case VKI_SIGBUS: |
| 466 | case VKI_SIGFPE: |
| 467 | case VKI_SIGILL: |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 468 | case VKI_SIGTRAP: |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 469 | /* For these, we always want to catch them and report, even |
| 470 | if the client code doesn't. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 471 | skss_handler = sync_signalhandler; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 472 | break; |
| 473 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 474 | case VKI_SIGCONT: |
| 475 | /* Let the kernel handle SIGCONT unless the client is actually |
| 476 | catching it. */ |
njn | a530fc6 | 2005-03-13 04:46:36 +0000 | [diff] [blame] | 477 | case VKI_SIGCHLD: |
| 478 | case VKI_SIGWINCH: |
| 479 | case VKI_SIGURG: |
| 480 | /* For signals which are have a default action of Ignore, |
| 481 | only set a handler if the client has set a signal handler. |
| 482 | Otherwise the kernel will interrupt a syscall which |
| 483 | wouldn't have otherwise been interrupted. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 484 | if (scss.scss_per_sig[sig].scss_handler == VKI_SIG_DFL) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 485 | skss_handler = VKI_SIG_DFL; |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 486 | else if (scss.scss_per_sig[sig].scss_handler == VKI_SIG_IGN) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 487 | skss_handler = VKI_SIG_IGN; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 488 | else |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 489 | skss_handler = async_signalhandler; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 490 | break; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 491 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 492 | default: |
njn | a530fc6 | 2005-03-13 04:46:36 +0000 | [diff] [blame] | 493 | // VKI_SIGVG* are runtime variables, so we can't make them |
| 494 | // cases in the switch, so we handle them in the 'default' case. |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 495 | if (sig == VG_SIGVGKILL) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 496 | skss_handler = sigvgkill_handler; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 497 | else { |
| 498 | if (scss_handler == VKI_SIG_IGN) |
| 499 | skss_handler = VKI_SIG_IGN; |
| 500 | else |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 501 | skss_handler = async_signalhandler; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 502 | } |
| 503 | break; |
| 504 | } |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 505 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 506 | /* Flags */ |
| 507 | |
| 508 | skss_flags = 0; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 509 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 510 | /* SA_NOCLDSTOP, SA_NOCLDWAIT: pass to kernel */ |
| 511 | skss_flags |= scss_flags & (VKI_SA_NOCLDSTOP | VKI_SA_NOCLDWAIT); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 512 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 513 | /* SA_ONESHOT: ignore client setting */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 514 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 515 | /* SA_RESTART: ignore client setting and always set it for us. |
| 516 | Though we never rely on the kernel to restart a |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 517 | syscall, we observe whether it wanted to restart the syscall |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 518 | or not, which is needed by |
| 519 | VG_(fixup_guest_state_after_syscall_interrupted) */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 520 | skss_flags |= VKI_SA_RESTART; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 521 | |
| 522 | /* SA_NOMASK: ignore it */ |
| 523 | |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 524 | /* SA_ONSTACK: client setting is irrelevant here */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 525 | /* We don't set a signal stack, so ignore */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 526 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 527 | /* always ask for SA_SIGINFO */ |
| 528 | skss_flags |= VKI_SA_SIGINFO; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 529 | |
fitzhardinge | 4f10ada | 2004-06-03 10:00:42 +0000 | [diff] [blame] | 530 | /* use our own restorer */ |
| 531 | skss_flags |= VKI_SA_RESTORER; |
| 532 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 533 | /* Create SKSS entry for this signal. */ |
sewardj | 6a3c26e | 2002-05-23 17:09:43 +0000 | [diff] [blame] | 534 | if (sig != VKI_SIGKILL && sig != VKI_SIGSTOP) |
| 535 | dst->skss_per_sig[sig].skss_handler = skss_handler; |
| 536 | else |
| 537 | dst->skss_per_sig[sig].skss_handler = VKI_SIG_DFL; |
| 538 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 539 | dst->skss_per_sig[sig].skss_flags = skss_flags; |
| 540 | } |
| 541 | |
| 542 | /* Sanity checks. */ |
nethercote | 5fd72bb | 2004-11-04 19:28:38 +0000 | [diff] [blame] | 543 | vg_assert(dst->skss_per_sig[VKI_SIGKILL].skss_handler == VKI_SIG_DFL); |
| 544 | vg_assert(dst->skss_per_sig[VKI_SIGSTOP].skss_handler == VKI_SIG_DFL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 545 | |
| 546 | if (0) |
| 547 | pp_SKSS(); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | /* --------------------------------------------------------------------- |
| 552 | After a possible SCSS change, update SKSS and the kernel itself. |
| 553 | ------------------------------------------------------------------ */ |
| 554 | |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 555 | // We need two levels of macro-expansion here to convert __NR_rt_sigreturn |
| 556 | // to a number before converting it to a string... sigh. |
sewardj | 03d8aa8 | 2005-10-14 11:25:49 +0000 | [diff] [blame] | 557 | extern void my_sigreturn(void); |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 558 | |
| 559 | #if defined(VGP_x86_linux) |
| 560 | # define _MYSIG(name) \ |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 561 | ".text\n" \ |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 562 | "my_sigreturn:\n" \ |
| 563 | " movl $" #name ", %eax\n" \ |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 564 | " int $0x80\n" \ |
| 565 | ".previous\n" |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 566 | #elif defined(VGP_amd64_linux) |
| 567 | # define _MYSIG(name) \ |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 568 | ".text\n" \ |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 569 | "my_sigreturn:\n" \ |
| 570 | " movq $" #name ", %rax\n" \ |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 571 | " syscall\n" \ |
| 572 | ".previous\n" |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 573 | #elif defined(VGP_ppc32_linux) |
| 574 | # define _MYSIG(name) \ |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 575 | ".text\n" \ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 576 | "my_sigreturn:\n" \ |
| 577 | " li 0, " #name "\n" \ |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 578 | " sc\n" \ |
| 579 | ".previous\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 580 | #elif defined(VGP_ppc64_linux) |
| 581 | # define _MYSIG(name) \ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 582 | ".align 2\n" \ |
| 583 | ".globl my_sigreturn\n" \ |
| 584 | ".section \".opd\",\"aw\"\n" \ |
| 585 | ".align 3\n" \ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 586 | "my_sigreturn:\n" \ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 587 | ".quad .my_sigreturn,.TOC.@tocbase,0\n" \ |
| 588 | ".previous\n" \ |
| 589 | ".type .my_sigreturn,@function\n" \ |
| 590 | ".globl .my_sigreturn\n" \ |
| 591 | ".my_sigreturn:\n" \ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 592 | " li 0, " #name "\n" \ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 593 | " sc\n" |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 594 | #elif defined(VGP_ppc32_aix5) |
| 595 | # define _MYSIG(name) \ |
| 596 | ".globl my_sigreturn\n" \ |
| 597 | "my_sigreturn:\n" \ |
| 598 | ".long 0\n" |
| 599 | #elif defined(VGP_ppc64_aix5) |
| 600 | # define _MYSIG(name) \ |
| 601 | ".globl my_sigreturn\n" \ |
| 602 | "my_sigreturn:\n" \ |
| 603 | ".long 0\n" |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 604 | #else |
| 605 | # error Unknown platform |
| 606 | #endif |
| 607 | |
| 608 | #define MYSIG(name) _MYSIG(name) |
| 609 | asm( |
| 610 | MYSIG(__NR_rt_sigreturn) |
| 611 | ); |
| 612 | |
| 613 | |
nethercote | 9dd1151 | 2004-08-04 15:31:30 +0000 | [diff] [blame] | 614 | static void handle_SCSS_change ( Bool force_update ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 615 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 616 | Int res, sig; |
| 617 | SKSS skss_old; |
| 618 | struct vki_sigaction ksa, ksa_old; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 619 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 620 | /* Remember old SKSS and calculate new one. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 621 | skss_old = skss; |
| 622 | calculate_SKSS_from_SCSS ( &skss ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 623 | |
| 624 | /* Compare the new SKSS entries vs the old ones, and update kernel |
| 625 | where they differ. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 626 | for (sig = 1; sig <= VG_(max_signal); sig++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 627 | |
| 628 | /* Trying to do anything with SIGKILL is pointless; just ignore |
| 629 | it. */ |
| 630 | if (sig == VKI_SIGKILL || sig == VKI_SIGSTOP) |
| 631 | continue; |
| 632 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 633 | if (!force_update) { |
| 634 | if ((skss_old.skss_per_sig[sig].skss_handler |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 635 | == skss.skss_per_sig[sig].skss_handler) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 636 | && (skss_old.skss_per_sig[sig].skss_flags |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 637 | == skss.skss_per_sig[sig].skss_flags)) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 638 | /* no difference */ |
| 639 | continue; |
| 640 | } |
| 641 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 642 | ksa.ksa_handler = skss.skss_per_sig[sig].skss_handler; |
| 643 | ksa.sa_flags = skss.skss_per_sig[sig].skss_flags; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 644 | # if !defined(VGP_ppc32_linux) && !defined(VGP_ppc32_aix5) \ |
| 645 | && !defined(VGP_ppc64_aix5) |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 646 | ksa.sa_restorer = my_sigreturn; |
sewardj | ce2a615 | 2005-07-08 18:25:13 +0000 | [diff] [blame] | 647 | # endif |
sewardj | 162bebd | 2005-07-09 10:43:45 +0000 | [diff] [blame] | 648 | /* Re above ifdef (also the assertion below), PaulM says: |
| 649 | The sa_restorer field is not used at all on ppc. Glibc |
| 650 | converts the sigaction you give it into a kernel sigaction, |
| 651 | but it doesn't put anything in the sa_restorer field. |
| 652 | */ |
fitzhardinge | 4f10ada | 2004-06-03 10:00:42 +0000 | [diff] [blame] | 653 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 654 | /* block all signals in handler */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 655 | VG_(sigfillset)( &ksa.sa_mask ); |
| 656 | VG_(sigdelset)( &ksa.sa_mask, VKI_SIGKILL ); |
| 657 | VG_(sigdelset)( &ksa.sa_mask, VKI_SIGSTOP ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 658 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 659 | if (VG_(clo_trace_signals) && VG_(clo_verbosity) > 2) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 660 | VG_(message)(Vg_DebugMsg, |
| 661 | "setting ksig %d to: hdlr 0x%x, flags 0x%x, " |
| 662 | "mask(63..0) 0x%x 0x%x", |
| 663 | sig, ksa.ksa_handler, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 664 | ksa.sa_flags, |
| 665 | ksa.sa_mask.sig[1], |
| 666 | ksa.sa_mask.sig[0] |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 667 | ); |
| 668 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 669 | res = VG_(sigaction)( sig, &ksa, &ksa_old ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 670 | vg_assert(res == 0); |
| 671 | |
| 672 | /* Since we got the old sigaction more or less for free, might |
| 673 | as well extract the maximum sanity-check value from it. */ |
| 674 | if (!force_update) { |
| 675 | vg_assert(ksa_old.ksa_handler |
| 676 | == skss_old.skss_per_sig[sig].skss_handler); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 677 | vg_assert(ksa_old.sa_flags |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 678 | == skss_old.skss_per_sig[sig].skss_flags); |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 679 | # if !defined(VGP_ppc32_linux) && !defined(VGP_ppc32_aix5) \ |
| 680 | && !defined(VGP_ppc64_aix5) |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 681 | vg_assert(ksa_old.sa_restorer |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 682 | == my_sigreturn); |
sewardj | ce2a615 | 2005-07-08 18:25:13 +0000 | [diff] [blame] | 683 | # endif |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 684 | VG_(sigaddset)( &ksa_old.sa_mask, VKI_SIGKILL ); |
| 685 | VG_(sigaddset)( &ksa_old.sa_mask, VKI_SIGSTOP ); |
| 686 | vg_assert(VG_(isfullsigset)( &ksa_old.sa_mask )); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
| 692 | /* --------------------------------------------------------------------- |
| 693 | Update/query SCSS in accordance with client requests. |
| 694 | ------------------------------------------------------------------ */ |
| 695 | |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 696 | /* Logic for this alt-stack stuff copied directly from do_sigaltstack |
| 697 | in kernel/signal.[ch] */ |
| 698 | |
| 699 | /* True if we are on the alternate signal stack. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 700 | static Bool on_sig_stack ( ThreadId tid, Addr m_SP ) |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 701 | { |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 702 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 703 | |
nethercote | 511e406 | 2004-09-11 13:34:08 +0000 | [diff] [blame] | 704 | return (m_SP - (Addr)tst->altstack.ss_sp < tst->altstack.ss_size); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 705 | } |
| 706 | |
nethercote | 511e406 | 2004-09-11 13:34:08 +0000 | [diff] [blame] | 707 | static Int sas_ss_flags ( ThreadId tid, Addr m_SP ) |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 708 | { |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 709 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 710 | |
| 711 | return (tst->altstack.ss_size == 0 |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 712 | ? VKI_SS_DISABLE |
nethercote | 511e406 | 2004-09-11 13:34:08 +0000 | [diff] [blame] | 713 | : on_sig_stack(tid, m_SP) ? VKI_SS_ONSTACK : 0); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 717 | SysRes VG_(do_sys_sigaltstack) ( ThreadId tid, vki_stack_t* ss, vki_stack_t* oss ) |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 718 | { |
njn | 502badb | 2005-05-08 02:04:49 +0000 | [diff] [blame] | 719 | Addr m_SP; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 720 | |
| 721 | vg_assert(VG_(is_valid_tid)(tid)); |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 722 | m_SP = VG_(get_SP)(tid); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 723 | |
| 724 | if (VG_(clo_trace_signals)) |
| 725 | VG_(message)(Vg_DebugExtraMsg, |
nethercote | 04d3a80 | 2004-11-16 18:26:11 +0000 | [diff] [blame] | 726 | "sys_sigaltstack: tid %d, " |
nethercote | 511e406 | 2004-09-11 13:34:08 +0000 | [diff] [blame] | 727 | "ss %p, oss %p (current SP %p)", |
| 728 | tid, (void*)ss, (void*)oss, (void*)m_SP ); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 729 | |
| 730 | if (oss != NULL) { |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 731 | oss->ss_sp = VG_(threads)[tid].altstack.ss_sp; |
| 732 | oss->ss_size = VG_(threads)[tid].altstack.ss_size; |
nethercote | 511e406 | 2004-09-11 13:34:08 +0000 | [diff] [blame] | 733 | oss->ss_flags = VG_(threads)[tid].altstack.ss_flags | sas_ss_flags(tid, m_SP); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | if (ss != NULL) { |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 737 | if (on_sig_stack(tid, VG_(get_SP)(tid))) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 738 | return VG_(mk_SysRes_Error)( VKI_EPERM ); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 739 | } |
| 740 | if (ss->ss_flags != VKI_SS_DISABLE |
| 741 | && ss->ss_flags != VKI_SS_ONSTACK |
| 742 | && ss->ss_flags != 0) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 743 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 744 | } |
| 745 | if (ss->ss_flags == VKI_SS_DISABLE) { |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 746 | VG_(threads)[tid].altstack.ss_flags = VKI_SS_DISABLE; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 747 | } else { |
| 748 | if (ss->ss_size < VKI_MINSIGSTKSZ) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 749 | return VG_(mk_SysRes_Error)( VKI_ENOMEM ); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 750 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 751 | |
nethercote | 20283c6 | 2004-11-04 19:43:22 +0000 | [diff] [blame] | 752 | VG_(threads)[tid].altstack.ss_sp = ss->ss_sp; |
| 753 | VG_(threads)[tid].altstack.ss_size = ss->ss_size; |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 754 | VG_(threads)[tid].altstack.ss_flags = 0; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 755 | } |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 756 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 757 | return VG_(mk_SysRes_Success)( 0 ); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 761 | SysRes VG_(do_sys_sigaction) ( Int signo, |
| 762 | const struct vki_sigaction *new_act, |
| 763 | struct vki_sigaction *old_act ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 764 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 765 | if (VG_(clo_trace_signals)) |
| 766 | VG_(message)(Vg_DebugExtraMsg, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 767 | "sys_sigaction: sigNo %d, " |
nethercote | 93246cf | 2004-11-04 18:56:47 +0000 | [diff] [blame] | 768 | "new %p, old %p, new flags 0x%llx", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 769 | signo, (UWord)new_act, (UWord)old_act, |
nethercote | 93246cf | 2004-11-04 18:56:47 +0000 | [diff] [blame] | 770 | (ULong)(new_act ? new_act->sa_flags : 0) ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 771 | |
| 772 | /* Rule out various error conditions. The aim is to ensure that if |
| 773 | when the call is passed to the kernel it will definitely |
| 774 | succeed. */ |
| 775 | |
| 776 | /* Reject out-of-range signal numbers. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 777 | if (signo < 1 || signo > VG_(max_signal)) goto bad_signo; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 778 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 779 | /* don't let them use our signals */ |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 780 | if ( (signo > VG_SIGVGRTUSERMAX) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 781 | && new_act |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 782 | && !(new_act->ksa_handler == VKI_SIG_DFL |
| 783 | || new_act->ksa_handler == VKI_SIG_IGN) ) |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 784 | goto bad_signo_reserved; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 785 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 786 | /* Reject attempts to set a handler (or set ignore) for SIGKILL. */ |
| 787 | if ( (signo == VKI_SIGKILL || signo == VKI_SIGSTOP) |
| 788 | && new_act |
| 789 | && new_act->ksa_handler != VKI_SIG_DFL) |
| 790 | goto bad_sigkill_or_sigstop; |
| 791 | |
| 792 | /* If the client supplied non-NULL old_act, copy the relevant SCSS |
| 793 | entry into it. */ |
| 794 | if (old_act) { |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 795 | old_act->ksa_handler = scss.scss_per_sig[signo].scss_handler; |
| 796 | old_act->sa_flags = scss.scss_per_sig[signo].scss_flags; |
| 797 | old_act->sa_mask = scss.scss_per_sig[signo].scss_mask; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 798 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 799 | old_act->sa_restorer = scss.scss_per_sig[signo].scss_restorer; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 800 | # endif |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* And now copy new SCSS entry from new_act. */ |
| 804 | if (new_act) { |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 805 | scss.scss_per_sig[signo].scss_handler = new_act->ksa_handler; |
| 806 | scss.scss_per_sig[signo].scss_flags = new_act->sa_flags; |
| 807 | scss.scss_per_sig[signo].scss_mask = new_act->sa_mask; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 808 | |
| 809 | scss.scss_per_sig[signo].scss_restorer = 0; |
| 810 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 811 | scss.scss_per_sig[signo].scss_restorer = new_act->sa_restorer; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 812 | # endif |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 813 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 814 | VG_(sigdelset)(&scss.scss_per_sig[signo].scss_mask, VKI_SIGKILL); |
| 815 | VG_(sigdelset)(&scss.scss_per_sig[signo].scss_mask, VKI_SIGSTOP); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* All happy bunnies ... */ |
| 819 | if (new_act) { |
nethercote | 9dd1151 | 2004-08-04 15:31:30 +0000 | [diff] [blame] | 820 | handle_SCSS_change( False /* lazy update */ ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 821 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 822 | return VG_(mk_SysRes_Success)( 0 ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 823 | |
| 824 | bad_signo: |
sewardj | ec6e27c | 2007-11-17 21:31:48 +0000 | [diff] [blame] | 825 | if (VG_(showing_core_errors)() && !VG_(clo_xml)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 826 | VG_(message)(Vg_UserMsg, |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 827 | "Warning: bad signal number %d in sigaction()", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 828 | signo); |
sewardj | 9a3d8bd | 2005-05-23 14:47:52 +0000 | [diff] [blame] | 829 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 830 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 831 | |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 832 | bad_signo_reserved: |
sewardj | ec6e27c | 2007-11-17 21:31:48 +0000 | [diff] [blame] | 833 | if (VG_(showing_core_errors)() && !VG_(clo_xml)) { |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 834 | VG_(message)(Vg_UserMsg, |
| 835 | "Warning: ignored attempt to set %s handler in sigaction();", |
| 836 | signame(signo)); |
| 837 | VG_(message)(Vg_UserMsg, |
| 838 | " the %s signal is used internally by Valgrind", |
| 839 | signame(signo)); |
fitzhardinge | bf45987 | 2003-11-18 16:55:33 +0000 | [diff] [blame] | 840 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 841 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 842 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 843 | bad_sigkill_or_sigstop: |
sewardj | ec6e27c | 2007-11-17 21:31:48 +0000 | [diff] [blame] | 844 | if (VG_(showing_core_errors)() && !VG_(clo_xml)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 845 | VG_(message)(Vg_UserMsg, |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 846 | "Warning: ignored attempt to set %s handler in sigaction();", |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 847 | signame(signo)); |
nethercote | 9c42a0f | 2003-11-17 10:37:19 +0000 | [diff] [blame] | 848 | VG_(message)(Vg_UserMsg, |
| 849 | " the %s signal is uncatchable", |
| 850 | signame(signo)); |
sewardj | 9a3d8bd | 2005-05-23 14:47:52 +0000 | [diff] [blame] | 851 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 852 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | |
| 856 | static |
| 857 | void do_sigprocmask_bitops ( Int vki_how, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 858 | vki_sigset_t* orig_set, |
| 859 | vki_sigset_t* modifier ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 860 | { |
| 861 | switch (vki_how) { |
| 862 | case VKI_SIG_BLOCK: |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 863 | VG_(sigaddset_from_set)( orig_set, modifier ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 864 | break; |
| 865 | case VKI_SIG_UNBLOCK: |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 866 | VG_(sigdelset_from_set)( orig_set, modifier ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 867 | break; |
| 868 | case VKI_SIG_SETMASK: |
| 869 | *orig_set = *modifier; |
| 870 | break; |
| 871 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 872 | VG_(core_panic)("do_sigprocmask_bitops"); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | } |
| 876 | |
tom | f7781e9 | 2005-11-02 15:31:21 +0000 | [diff] [blame] | 877 | static |
| 878 | const Char *format_sigset ( const vki_sigset_t* set ) |
| 879 | { |
| 880 | static Char buf[128]; |
| 881 | int w; |
| 882 | |
| 883 | VG_(strcpy)(buf, ""); |
| 884 | |
| 885 | for (w = _VKI_NSIG_WORDS - 1; w >= 0; w--) |
| 886 | { |
| 887 | #if _VKI_NSIG_BPW == 32 |
| 888 | VG_(sprintf)(buf + VG_(strlen)(buf), "%08lx", set ? set->sig[w] : 0); |
| 889 | #elif _VKI_NSIG_BPW == 64 |
| 890 | VG_(sprintf)(buf + VG_(strlen)(buf), "%16lx", set ? set->sig[w] : 0); |
| 891 | #else |
| 892 | #error "Unsupported value for _VKI_NSIG_BPW" |
| 893 | #endif |
| 894 | } |
| 895 | |
| 896 | return buf; |
| 897 | } |
| 898 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 899 | /* |
| 900 | This updates the thread's signal mask. There's no such thing as a |
| 901 | process-wide signal mask. |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 902 | |
| 903 | Note that the thread signal masks are an implicit part of SCSS, |
| 904 | which is why this routine is allowed to mess with them. |
| 905 | */ |
| 906 | static |
| 907 | void do_setmask ( ThreadId tid, |
| 908 | Int how, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 909 | vki_sigset_t* newset, |
| 910 | vki_sigset_t* oldset ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 911 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 912 | if (VG_(clo_trace_signals)) |
sewardj | a464e5c | 2002-05-23 17:34:49 +0000 | [diff] [blame] | 913 | VG_(message)(Vg_DebugExtraMsg, |
tom | f7781e9 | 2005-11-02 15:31:21 +0000 | [diff] [blame] | 914 | "do_setmask: tid = %d how = %d (%s), set = %p %s", |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 915 | tid, how, |
| 916 | how==VKI_SIG_BLOCK ? "SIG_BLOCK" : ( |
| 917 | how==VKI_SIG_UNBLOCK ? "SIG_UNBLOCK" : ( |
| 918 | how==VKI_SIG_SETMASK ? "SIG_SETMASK" : "???")), |
tom | f7781e9 | 2005-11-02 15:31:21 +0000 | [diff] [blame] | 919 | newset, format_sigset(newset)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 920 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 921 | /* Just do this thread. */ |
| 922 | vg_assert(VG_(is_valid_tid)(tid)); |
| 923 | if (oldset) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 924 | *oldset = VG_(threads)[tid].sig_mask; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 925 | if (VG_(clo_trace_signals)) |
| 926 | VG_(message)(Vg_DebugExtraMsg, |
tom | 1399c74 | 2005-11-02 15:32:19 +0000 | [diff] [blame] | 927 | "\toldset=%p %s", |
tom | f7781e9 | 2005-11-02 15:31:21 +0000 | [diff] [blame] | 928 | oldset, format_sigset(oldset)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 929 | } |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 930 | if (newset) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 931 | do_sigprocmask_bitops (how, &VG_(threads)[tid].sig_mask, newset ); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 932 | VG_(sigdelset)(&VG_(threads)[tid].sig_mask, VKI_SIGKILL); |
| 933 | VG_(sigdelset)(&VG_(threads)[tid].sig_mask, VKI_SIGSTOP); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 934 | VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
| 938 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 939 | SysRes VG_(do_sys_sigprocmask) ( ThreadId tid, |
| 940 | Int how, |
| 941 | vki_sigset_t* set, |
| 942 | vki_sigset_t* oldset ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 943 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 944 | switch(how) { |
| 945 | case VKI_SIG_BLOCK: |
| 946 | case VKI_SIG_UNBLOCK: |
| 947 | case VKI_SIG_SETMASK: |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 948 | vg_assert(VG_(is_valid_tid)(tid)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 949 | do_setmask ( tid, how, set, oldset ); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 950 | return VG_(mk_SysRes_Success)( 0 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 951 | |
| 952 | default: |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 953 | VG_(message)(Vg_DebugMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 954 | "sigprocmask: unknown 'how' field %d", how); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 955 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| 959 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 960 | /* --------------------------------------------------------------------- |
| 961 | LOW LEVEL STUFF TO DO WITH SIGNALS: IMPLEMENTATION |
| 962 | ------------------------------------------------------------------ */ |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 963 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 964 | /* --------------------------------------------------------------------- |
| 965 | Handy utilities to block/restore all host signals. |
| 966 | ------------------------------------------------------------------ */ |
| 967 | |
| 968 | /* Block all host signals, dumping the old mask in *saved_mask. */ |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 969 | static void block_all_host_signals ( /* OUT */ vki_sigset_t* saved_mask ) |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 970 | { |
| 971 | Int ret; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 972 | vki_sigset_t block_procmask; |
| 973 | VG_(sigfillset)(&block_procmask); |
| 974 | ret = VG_(sigprocmask) |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 975 | (VKI_SIG_SETMASK, &block_procmask, saved_mask); |
| 976 | vg_assert(ret == 0); |
| 977 | } |
| 978 | |
| 979 | /* Restore the blocking mask using the supplied saved one. */ |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 980 | static void restore_all_host_signals ( /* IN */ vki_sigset_t* saved_mask ) |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 981 | { |
| 982 | Int ret; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 983 | ret = VG_(sigprocmask)(VKI_SIG_SETMASK, saved_mask, NULL); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 984 | vg_assert(ret == 0); |
| 985 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 986 | |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 987 | void VG_(clear_out_queued_signals)( ThreadId tid, vki_sigset_t* saved_mask ) |
| 988 | { |
| 989 | block_all_host_signals(saved_mask); |
| 990 | if (VG_(threads)[tid].sig_queue != NULL) { |
| 991 | VG_(arena_free)(VG_AR_CORE, VG_(threads)[tid].sig_queue); |
| 992 | VG_(threads)[tid].sig_queue = NULL; |
| 993 | } |
| 994 | restore_all_host_signals(saved_mask); |
| 995 | } |
| 996 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 997 | /* --------------------------------------------------------------------- |
| 998 | The signal simulation proper. A simplified version of what the |
| 999 | Linux kernel does. |
| 1000 | ------------------------------------------------------------------ */ |
| 1001 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1002 | /* Set up a stack frame (VgSigContext) for the client's signal |
nethercote | fedd810 | 2004-09-13 15:19:34 +0000 | [diff] [blame] | 1003 | handler. */ |
sewardj | 2c5ffbe | 2005-03-12 13:32:06 +0000 | [diff] [blame] | 1004 | static |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1005 | void push_signal_frame ( ThreadId tid, const vki_siginfo_t *siginfo, const struct vki_ucontext *uc ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1006 | { |
nethercote | 6eec460 | 2004-09-13 14:15:36 +0000 | [diff] [blame] | 1007 | Addr esp_top_of_frame; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1008 | ThreadState* tst; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1009 | Int sigNo = siginfo->si_signo; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1010 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1011 | vg_assert(sigNo >= 1 && sigNo <= VG_(max_signal)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1012 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1013 | tst = & VG_(threads)[tid]; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1014 | |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 1015 | if (VG_(clo_trace_signals)) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1016 | VG_(message)(Vg_DebugMsg, |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1017 | "push_signal_frame (thread %d): signal %d", tid, sigNo); |
sewardj | a672ea3 | 2006-04-29 18:03:14 +0000 | [diff] [blame] | 1018 | VG_(get_and_pp_StackTrace)(tid, 10); |
| 1019 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1020 | |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1021 | if (/* this signal asked to run on an alt stack */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1022 | (scss.scss_per_sig[sigNo].scss_flags & VKI_SA_ONSTACK ) |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1023 | && /* there is a defined and enabled alt stack, which we're not |
| 1024 | already using. Logic from get_sigframe in |
| 1025 | arch/i386/kernel/signal.c. */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1026 | sas_ss_flags(tid, VG_(get_SP)(tid)) == 0 |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1027 | ) { |
| 1028 | esp_top_of_frame |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 1029 | = (Addr)(tst->altstack.ss_sp) + tst->altstack.ss_size; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1030 | if (VG_(clo_trace_signals)) |
| 1031 | VG_(message)(Vg_DebugMsg, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1032 | "delivering signal %d (%s) to thread %d: on ALT STACK (%p-%p; %d bytes)", |
| 1033 | sigNo, signame(sigNo), tid, |
| 1034 | tst->altstack.ss_sp, |
tom | 151a639 | 2005-11-11 12:30:36 +0000 | [diff] [blame] | 1035 | (UChar *)tst->altstack.ss_sp + tst->altstack.ss_size, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1036 | tst->altstack.ss_size ); |
njn | fdc28af | 2003-02-24 10:36:48 +0000 | [diff] [blame] | 1037 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1038 | /* Signal delivery to tools */ |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 1039 | VG_TRACK( pre_deliver_signal, tid, sigNo, /*alt_stack*/True ); |
njn | fdc28af | 2003-02-24 10:36:48 +0000 | [diff] [blame] | 1040 | |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1041 | } else { |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1042 | esp_top_of_frame = VG_(get_SP)(tid) - VG_STACK_REDZONE_SZB; |
njn | fdc28af | 2003-02-24 10:36:48 +0000 | [diff] [blame] | 1043 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1044 | /* Signal delivery to tools */ |
fitzhardinge | 98c4dc0 | 2004-03-16 08:27:29 +0000 | [diff] [blame] | 1045 | VG_TRACK( pre_deliver_signal, tid, sigNo, /*alt_stack*/False ); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1046 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1047 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1048 | vg_assert(scss.scss_per_sig[sigNo].scss_handler != VKI_SIG_IGN); |
| 1049 | vg_assert(scss.scss_per_sig[sigNo].scss_handler != VKI_SIG_DFL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1050 | |
| 1051 | /* This may fail if the client stack is busted; if that happens, |
| 1052 | the whole process will exit rather than simply calling the |
| 1053 | signal handler. */ |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1054 | VG_(sigframe_create) (tid, esp_top_of_frame, siginfo, uc, |
sewardj | 985fabb | 2005-04-24 14:18:14 +0000 | [diff] [blame] | 1055 | scss.scss_per_sig[sigNo].scss_handler, |
| 1056 | scss.scss_per_sig[sigNo].scss_flags, |
| 1057 | &tst->sig_mask, |
| 1058 | scss.scss_per_sig[sigNo].scss_restorer); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1061 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1062 | static const Char *signame(Int sigNo) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1063 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1064 | static Char buf[10]; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1065 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1066 | switch(sigNo) { |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1067 | case VKI_SIGHUP: return "SIGHUP"; |
| 1068 | case VKI_SIGINT: return "SIGINT"; |
| 1069 | case VKI_SIGQUIT: return "SIGQUIT"; |
| 1070 | case VKI_SIGILL: return "SIGILL"; |
| 1071 | case VKI_SIGTRAP: return "SIGTRAP"; |
| 1072 | case VKI_SIGABRT: return "SIGABRT"; |
| 1073 | case VKI_SIGBUS: return "SIGBUS"; |
| 1074 | case VKI_SIGFPE: return "SIGFPE"; |
| 1075 | case VKI_SIGKILL: return "SIGKILL"; |
| 1076 | case VKI_SIGUSR1: return "SIGUSR1"; |
| 1077 | case VKI_SIGUSR2: return "SIGUSR2"; |
| 1078 | case VKI_SIGSEGV: return "SIGSEGV"; |
| 1079 | case VKI_SIGPIPE: return "SIGPIPE"; |
| 1080 | case VKI_SIGALRM: return "SIGALRM"; |
| 1081 | case VKI_SIGTERM: return "SIGTERM"; |
| 1082 | # if defined(VKI_SIGSTKFLT) |
| 1083 | case VKI_SIGSTKFLT: return "SIGSTKFLT"; |
| 1084 | # endif |
| 1085 | case VKI_SIGCHLD: return "SIGCHLD"; |
| 1086 | case VKI_SIGCONT: return "SIGCONT"; |
| 1087 | case VKI_SIGSTOP: return "SIGSTOP"; |
| 1088 | case VKI_SIGTSTP: return "SIGTSTP"; |
| 1089 | case VKI_SIGTTIN: return "SIGTTIN"; |
| 1090 | case VKI_SIGTTOU: return "SIGTTOU"; |
| 1091 | case VKI_SIGURG: return "SIGURG"; |
| 1092 | case VKI_SIGXCPU: return "SIGXCPU"; |
| 1093 | case VKI_SIGXFSZ: return "SIGXFSZ"; |
| 1094 | case VKI_SIGVTALRM: return "SIGVTALRM"; |
| 1095 | case VKI_SIGPROF: return "SIGPROF"; |
| 1096 | case VKI_SIGWINCH: return "SIGWINCH"; |
| 1097 | case VKI_SIGIO: return "SIGIO"; |
| 1098 | case VKI_SIGPWR: return "SIGPWR"; |
| 1099 | # if defined(VKI_SIGUNUSED) |
| 1100 | case VKI_SIGUNUSED: return "SIGUNUSED"; |
| 1101 | # endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1102 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1103 | case VKI_SIGRTMIN ... VKI_SIGRTMAX: |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1104 | VG_(sprintf)(buf, "SIGRT%d", sigNo-VKI_SIGRTMIN); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1105 | return buf; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1106 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1107 | default: |
| 1108 | VG_(sprintf)(buf, "SIG%d", sigNo); |
| 1109 | return buf; |
| 1110 | } |
| 1111 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1112 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1113 | /* Hit ourselves with a signal using the default handler */ |
| 1114 | void VG_(kill_self)(Int sigNo) |
| 1115 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1116 | vki_sigset_t mask, origmask; |
| 1117 | struct vki_sigaction sa, origsa; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1118 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1119 | sa.ksa_handler = VKI_SIG_DFL; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1120 | sa.sa_flags = 0; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1121 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1122 | sa.sa_restorer = 0; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1123 | # endif |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1124 | VG_(sigemptyset)(&sa.sa_mask); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1125 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1126 | VG_(sigaction)(sigNo, &sa, &origsa); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1127 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1128 | VG_(sigemptyset)(&mask); |
| 1129 | VG_(sigaddset)(&mask, sigNo); |
| 1130 | VG_(sigprocmask)(VKI_SIG_UNBLOCK, &mask, &origmask); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1131 | |
tom | b9b10c7 | 2005-07-20 16:05:28 +0000 | [diff] [blame] | 1132 | VG_(kill)(VG_(getpid)(), sigNo); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1133 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1134 | VG_(sigaction)(sigNo, &origsa, NULL); |
| 1135 | VG_(sigprocmask)(VKI_SIG_SETMASK, &origmask, NULL); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1139 | Perform the default action of a signal. If the signal is fatal, it |
| 1140 | marks all threads as needing to exit, but it doesn't actually kill |
| 1141 | the process or thread. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1142 | |
| 1143 | If we're not being quiet, then print out some more detail about |
| 1144 | fatal signals (esp. core dumping signals). |
| 1145 | */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1146 | static void default_action(const vki_siginfo_t *info, ThreadId tid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1147 | { |
| 1148 | Int sigNo = info->si_signo; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1149 | Bool terminate = False; /* kills process */ |
| 1150 | Bool core = False; /* kills process w/ core */ |
| 1151 | struct vki_rlimit corelim; |
| 1152 | Bool could_core; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1153 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1154 | vg_assert(VG_(is_running_thread)(tid)); |
| 1155 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1156 | switch(sigNo) { |
| 1157 | case VKI_SIGQUIT: /* core */ |
| 1158 | case VKI_SIGILL: /* core */ |
| 1159 | case VKI_SIGABRT: /* core */ |
| 1160 | case VKI_SIGFPE: /* core */ |
| 1161 | case VKI_SIGSEGV: /* core */ |
| 1162 | case VKI_SIGBUS: /* core */ |
| 1163 | case VKI_SIGTRAP: /* core */ |
| 1164 | case VKI_SIGXCPU: /* core */ |
| 1165 | case VKI_SIGXFSZ: /* core */ |
| 1166 | terminate = True; |
| 1167 | core = True; |
| 1168 | break; |
| 1169 | |
| 1170 | case VKI_SIGHUP: /* term */ |
| 1171 | case VKI_SIGINT: /* term */ |
| 1172 | case VKI_SIGKILL: /* term - we won't see this */ |
| 1173 | case VKI_SIGPIPE: /* term */ |
| 1174 | case VKI_SIGALRM: /* term */ |
| 1175 | case VKI_SIGTERM: /* term */ |
| 1176 | case VKI_SIGUSR1: /* term */ |
| 1177 | case VKI_SIGUSR2: /* term */ |
| 1178 | case VKI_SIGIO: /* term */ |
| 1179 | case VKI_SIGPWR: /* term */ |
| 1180 | case VKI_SIGSYS: /* term */ |
| 1181 | case VKI_SIGPROF: /* term */ |
| 1182 | case VKI_SIGVTALRM: /* term */ |
| 1183 | case VKI_SIGRTMIN ... VKI_SIGRTMAX: /* term */ |
| 1184 | terminate = True; |
| 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | vg_assert(!core || (core && terminate)); |
| 1189 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1190 | if (VG_(clo_trace_signals)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1191 | VG_(message)(Vg_DebugMsg, "delivering %d (code %d) to default handler; action: %s%s", |
| 1192 | sigNo, info->si_code, terminate ? "terminate" : "ignore", core ? "+core" : ""); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1193 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1194 | if (!terminate) |
| 1195 | return; /* nothing to do */ |
fitzhardinge | 4a4d108 | 2004-03-15 23:46:54 +0000 | [diff] [blame] | 1196 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1197 | could_core = core; |
| 1198 | |
| 1199 | if (core) { |
| 1200 | /* If they set the core-size limit to zero, don't generate a |
| 1201 | core file */ |
fitzhardinge | 61a5341 | 2004-03-15 23:44:11 +0000 | [diff] [blame] | 1202 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1203 | VG_(getrlimit)(VKI_RLIMIT_CORE, &corelim); |
fitzhardinge | 61a5341 | 2004-03-15 23:44:11 +0000 | [diff] [blame] | 1204 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1205 | if (corelim.rlim_cur == 0) |
| 1206 | core = False; |
| 1207 | } |
fitzhardinge | 61a5341 | 2004-03-15 23:44:11 +0000 | [diff] [blame] | 1208 | |
sewardj | ec6e27c | 2007-11-17 21:31:48 +0000 | [diff] [blame] | 1209 | if ( (VG_(clo_verbosity) > 1 || (could_core && info->si_code > VKI_SI_USER)) |
| 1210 | && !VG_(clo_xml) ) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1211 | VG_(message)(Vg_UserMsg, ""); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1212 | VG_(message)(Vg_UserMsg, |
| 1213 | "Process terminating with default action of signal %d (%s)%s", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1214 | sigNo, signame(sigNo), core ? ": dumping core" : ""); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1215 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1216 | /* Be helpful - decode some more details about this fault */ |
| 1217 | if (info->si_code > VKI_SI_USER) { |
| 1218 | const Char *event = NULL; |
| 1219 | Bool haveaddr = True; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1220 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1221 | switch(sigNo) { |
| 1222 | case VKI_SIGSEGV: |
| 1223 | switch(info->si_code) { |
tom | 148250b | 2005-11-12 00:13:20 +0000 | [diff] [blame] | 1224 | case VKI_SEGV_MAPERR: event = "Access not within mapped region"; break; |
| 1225 | case VKI_SEGV_ACCERR: event = "Bad permissions for mapped region"; break; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1226 | case 128: |
| 1227 | /* General Protection Fault: The CPU/kernel |
| 1228 | isn't telling us anything useful, but this |
| 1229 | is commonly the result of exceeding a |
sewardj | 74b4cca | 2005-10-20 01:37:15 +0000 | [diff] [blame] | 1230 | segment limit. */ |
| 1231 | event = "General Protection Fault"; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1232 | haveaddr = False; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1233 | break; |
| 1234 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1235 | #if 0 |
| 1236 | { |
| 1237 | HChar buf[110]; |
| 1238 | VG_(am_show_nsegments)(0,"post segfault"); |
| 1239 | VG_(sprintf)(buf, "/bin/cat /proc/%d/maps", VG_(getpid)()); |
| 1240 | VG_(system)(buf); |
| 1241 | } |
| 1242 | #endif |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1243 | break; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1244 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1245 | case VKI_SIGILL: |
| 1246 | switch(info->si_code) { |
tom | 148250b | 2005-11-12 00:13:20 +0000 | [diff] [blame] | 1247 | case VKI_ILL_ILLOPC: event = "Illegal opcode"; break; |
| 1248 | case VKI_ILL_ILLOPN: event = "Illegal operand"; break; |
| 1249 | case VKI_ILL_ILLADR: event = "Illegal addressing mode"; break; |
| 1250 | case VKI_ILL_ILLTRP: event = "Illegal trap"; break; |
| 1251 | case VKI_ILL_PRVOPC: event = "Privileged opcode"; break; |
| 1252 | case VKI_ILL_PRVREG: event = "Privileged register"; break; |
| 1253 | case VKI_ILL_COPROC: event = "Coprocessor error"; break; |
| 1254 | case VKI_ILL_BADSTK: event = "Internal stack error"; break; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1255 | } |
| 1256 | break; |
| 1257 | |
| 1258 | case VKI_SIGFPE: |
| 1259 | switch (info->si_code) { |
tom | 148250b | 2005-11-12 00:13:20 +0000 | [diff] [blame] | 1260 | case VKI_FPE_INTDIV: event = "Integer divide by zero"; break; |
| 1261 | case VKI_FPE_INTOVF: event = "Integer overflow"; break; |
| 1262 | case VKI_FPE_FLTDIV: event = "FP divide by zero"; break; |
| 1263 | case VKI_FPE_FLTOVF: event = "FP overflow"; break; |
| 1264 | case VKI_FPE_FLTUND: event = "FP underflow"; break; |
| 1265 | case VKI_FPE_FLTRES: event = "FP inexact"; break; |
| 1266 | case VKI_FPE_FLTINV: event = "FP invalid operation"; break; |
| 1267 | case VKI_FPE_FLTSUB: event = "FP subscript out of range"; break; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1268 | } |
| 1269 | break; |
| 1270 | |
| 1271 | case VKI_SIGBUS: |
| 1272 | switch (info->si_code) { |
tom | 148250b | 2005-11-12 00:13:20 +0000 | [diff] [blame] | 1273 | case VKI_BUS_ADRALN: event = "Invalid address alignment"; break; |
| 1274 | case VKI_BUS_ADRERR: event = "Non-existent physical address"; break; |
| 1275 | case VKI_BUS_OBJERR: event = "Hardware error"; break; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1276 | } |
| 1277 | break; |
sewardj | 3059d27 | 2007-12-21 01:24:59 +0000 | [diff] [blame] | 1278 | } /* switch (sigNo) */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (event != NULL) { |
| 1281 | if (haveaddr) |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 1282 | VG_(message)(Vg_UserMsg, " %s at address %p", |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1283 | event, info->VKI_SIGINFO_si_addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1284 | else |
| 1285 | VG_(message)(Vg_UserMsg, " %s", event); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1286 | } |
| 1287 | } |
sewardj | 3059d27 | 2007-12-21 01:24:59 +0000 | [diff] [blame] | 1288 | /* Print a stack trace. Be cautious if the thread's SP is in an |
| 1289 | obviously stupid place (not mapped readable) that would |
| 1290 | likely cause a segfault. */ |
| 1291 | if (VG_(is_valid_tid)(tid)) { |
| 1292 | ExeContext* ec = VG_(am_is_valid_for_client) |
| 1293 | (VG_(get_SP)(tid), sizeof(Addr), VKI_PROT_READ) |
| 1294 | ? VG_(record_ExeContext)( tid, 0/*first_ip_delta*/ ) |
| 1295 | : VG_(record_depth_1_ExeContext)( tid ); |
| 1296 | vg_assert(ec); |
| 1297 | VG_(pp_ExeContext)( ec ); |
fitzhardinge | 126c64f | 2003-12-08 21:58:37 +0000 | [diff] [blame] | 1298 | } |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 1299 | if (sigNo == VKI_SIGSEGV |
| 1300 | && info && info->si_code > VKI_SI_USER |
| 1301 | && info->si_code == VKI_SEGV_MAPERR) { |
| 1302 | VG_(message)(Vg_UserMsg, " If you believe this happened as a " |
| 1303 | "result of a stack overflow in your"); |
| 1304 | VG_(message)(Vg_UserMsg, " program's main thread (unlikely but" |
| 1305 | " possible), you can try to increase"); |
| 1306 | VG_(message)(Vg_UserMsg, " the size of the main thread stack" |
| 1307 | " using the --main-stacksize= flag."); |
| 1308 | // FIXME: assumes main ThreadId == 1 |
| 1309 | if (VG_(is_valid_tid)(1)) { |
| 1310 | VG_(message)(Vg_UserMsg, |
| 1311 | " The main thread stack size used in this run was %d.", |
| 1312 | (Int)VG_(threads)[1].client_stack_szB); |
| 1313 | } |
| 1314 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | if (VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) )) { |
| 1318 | VG_(start_debugger)( tid ); |
| 1319 | } |
fitzhardinge | 126c64f | 2003-12-08 21:58:37 +0000 | [diff] [blame] | 1320 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1321 | if (core) { |
| 1322 | const static struct vki_rlimit zero = { 0, 0 }; |
fitzhardinge | 4a4d108 | 2004-03-15 23:46:54 +0000 | [diff] [blame] | 1323 | |
njn | 6722983 | 2005-08-28 04:38:12 +0000 | [diff] [blame] | 1324 | VG_(make_coredump)(tid, info, corelim.rlim_cur); |
fitzhardinge | d65dcad | 2004-03-13 02:06:58 +0000 | [diff] [blame] | 1325 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1326 | /* Make sure we don't get a confusing kernel-generated |
| 1327 | coredump when we finally exit */ |
| 1328 | VG_(setrlimit)(VKI_RLIMIT_CORE, &zero); |
| 1329 | } |
fitzhardinge | d65dcad | 2004-03-13 02:06:58 +0000 | [diff] [blame] | 1330 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1331 | /* stash fatal signal in main thread */ |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 1332 | // what's this for? |
| 1333 | //VG_(threads)[VG_(master_tid)].os_state.fatalsig = sigNo; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1334 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1335 | /* everyone dies */ |
| 1336 | VG_(nuke_all_threads_except)(tid, VgSrc_FatalSig); |
| 1337 | VG_(threads)[tid].exitreason = VgSrc_FatalSig; |
| 1338 | VG_(threads)[tid].os_state.fatalsig = sigNo; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1341 | /* |
| 1342 | This does the business of delivering a signal to a thread. It may |
| 1343 | be called from either a real signal handler, or from normal code to |
| 1344 | cause the thread to enter the signal handler. |
sewardj | 5e2f001 | 2004-12-13 14:10:34 +0000 | [diff] [blame] | 1345 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1346 | This updates the thread state, but it does not set it to be |
| 1347 | Runnable. |
| 1348 | */ |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1349 | static void deliver_signal ( ThreadId tid, const vki_siginfo_t *info, const struct vki_ucontext *uc ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1350 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1351 | Int sigNo = info->si_signo; |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1352 | SCSS_Per_Signal *handler = &scss.scss_per_sig[sigNo]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1353 | void *handler_fn; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1354 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 1355 | |
| 1356 | if (VG_(clo_trace_signals)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1357 | VG_(message)(Vg_DebugMsg,"delivering signal %d (%s):%d to thread %d", |
| 1358 | sigNo, signame(sigNo), info->si_code, tid ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1359 | |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 1360 | if (sigNo == VG_SIGVGKILL) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1361 | /* If this is a SIGVGKILL, we're expecting it to interrupt any |
| 1362 | blocked syscall. It doesn't matter whether the VCPU state is |
| 1363 | set to restart or not, because we don't expect it will |
| 1364 | execute any more client instructions. */ |
| 1365 | vg_assert(VG_(is_exiting)(tid)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1366 | return; |
| 1367 | } |
| 1368 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1369 | /* If the client specifies SIG_IGN, treat it as SIG_DFL. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1370 | |
njn | 9ec0f3e | 2005-03-13 06:00:47 +0000 | [diff] [blame] | 1371 | If deliver_signal() is being called on a thread, we want |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1372 | the signal to get through no matter what; if they're ignoring |
| 1373 | it, then we do this override (this is so we can send it SIGSEGV, |
| 1374 | etc). */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1375 | handler_fn = handler->scss_handler; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1376 | if (handler_fn == VKI_SIG_IGN) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1377 | handler_fn = VKI_SIG_DFL; |
| 1378 | |
| 1379 | vg_assert(handler_fn != VKI_SIG_IGN); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1380 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1381 | if (handler_fn == VKI_SIG_DFL) { |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1382 | default_action(info, tid); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1383 | } else { |
| 1384 | /* Create a signal delivery frame, and set the client's %ESP and |
| 1385 | %EIP so that when execution continues, we will enter the |
| 1386 | signal handler with the frame on top of the client's stack, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1387 | as it expects. |
| 1388 | |
| 1389 | Signal delivery can fail if the client stack is too small or |
| 1390 | missing, and we can't push the frame. If that happens, |
| 1391 | push_signal_frame will cause the whole process to exit when |
| 1392 | we next hit the scheduler. |
| 1393 | */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1394 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1395 | |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1396 | push_signal_frame ( tid, info, uc ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (handler->scss_flags & VKI_SA_ONESHOT) { |
| 1399 | /* Do the ONESHOT thing. */ |
| 1400 | handler->scss_handler = VKI_SIG_DFL; |
| 1401 | |
nethercote | 9dd1151 | 2004-08-04 15:31:30 +0000 | [diff] [blame] | 1402 | handle_SCSS_change( False /* lazy update */ ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1403 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1404 | |
| 1405 | /* At this point: |
| 1406 | tst->sig_mask is the current signal mask |
| 1407 | tst->tmp_sig_mask is the same as sig_mask, unless we're in sigsuspend |
| 1408 | handler->scss_mask is the mask set by the handler |
| 1409 | |
| 1410 | Handler gets a mask of tmp_sig_mask|handler_mask|signo |
| 1411 | */ |
| 1412 | tst->sig_mask = tst->tmp_sig_mask; |
| 1413 | if (!(handler->scss_flags & VKI_SA_NOMASK)) { |
| 1414 | VG_(sigaddset_from_set)(&tst->sig_mask, &handler->scss_mask); |
| 1415 | VG_(sigaddset)(&tst->sig_mask, sigNo); |
| 1416 | |
| 1417 | tst->tmp_sig_mask = tst->sig_mask; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | /* Thread state is ready to go - just add Runnable */ |
| 1422 | } |
| 1423 | |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1424 | static void resume_scheduler(ThreadId tid) |
| 1425 | { |
| 1426 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 1427 | |
| 1428 | vg_assert(tst->os_state.lwpid == VG_(gettid)()); |
| 1429 | |
| 1430 | if (tst->sched_jmpbuf_valid) { |
| 1431 | /* Can't continue; must longjmp back to the scheduler and thus |
| 1432 | enter the sighandler immediately. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1433 | __builtin_longjmp(tst->sched_jmpbuf, True); |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
njn | 9ec0f3e | 2005-03-13 06:00:47 +0000 | [diff] [blame] | 1437 | static void synth_fault_common(ThreadId tid, Addr addr, Int si_code) |
| 1438 | { |
| 1439 | vki_siginfo_t info; |
| 1440 | |
| 1441 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1442 | |
| 1443 | info.si_signo = VKI_SIGSEGV; |
| 1444 | info.si_code = si_code; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1445 | info.VKI_SIGINFO_si_addr = (void*)addr; |
njn | 9ec0f3e | 2005-03-13 06:00:47 +0000 | [diff] [blame] | 1446 | |
| 1447 | /* If they're trying to block the signal, force it to be delivered */ |
| 1448 | if (VG_(sigismember)(&VG_(threads)[tid].sig_mask, VKI_SIGSEGV)) |
| 1449 | VG_(set_default_handler)(VKI_SIGSEGV); |
| 1450 | |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1451 | deliver_signal(tid, &info, NULL); |
njn | 9ec0f3e | 2005-03-13 06:00:47 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // Synthesize a fault where the address is OK, but the page |
| 1455 | // permissions are bad. |
| 1456 | void VG_(synth_fault_perms)(ThreadId tid, Addr addr) |
| 1457 | { |
| 1458 | synth_fault_common(tid, addr, 2); |
| 1459 | } |
| 1460 | |
| 1461 | // Synthesize a fault where the address there's nothing mapped at the address. |
| 1462 | void VG_(synth_fault_mapping)(ThreadId tid, Addr addr) |
| 1463 | { |
| 1464 | synth_fault_common(tid, addr, 1); |
| 1465 | } |
| 1466 | |
| 1467 | // Synthesize a misc memory fault. |
| 1468 | void VG_(synth_fault)(ThreadId tid) |
| 1469 | { |
| 1470 | synth_fault_common(tid, 0, 0x80); |
| 1471 | } |
| 1472 | |
| 1473 | // Synthesise a SIGILL. |
| 1474 | void VG_(synth_sigill)(ThreadId tid, Addr addr) |
| 1475 | { |
| 1476 | vki_siginfo_t info; |
| 1477 | |
| 1478 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1479 | |
| 1480 | info.si_signo = VKI_SIGILL; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1481 | info.si_code = VKI_ILL_ILLOPC; /* jrs: no idea what this should be */ |
| 1482 | info.VKI_SIGINFO_si_addr = (void*)addr; |
njn | 9ec0f3e | 2005-03-13 06:00:47 +0000 | [diff] [blame] | 1483 | |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1484 | resume_scheduler(tid); |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1485 | deliver_signal(tid, &info, NULL); |
njn | 9ec0f3e | 2005-03-13 06:00:47 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
sewardj | 86df155 | 2006-02-07 20:56:41 +0000 | [diff] [blame] | 1488 | // Synthesise a SIGTRAP. |
| 1489 | void VG_(synth_sigtrap)(ThreadId tid) |
| 1490 | { |
| 1491 | vki_siginfo_t info; |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1492 | struct vki_ucontext uc; |
sewardj | 86df155 | 2006-02-07 20:56:41 +0000 | [diff] [blame] | 1493 | |
| 1494 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1495 | |
| 1496 | info.si_signo = VKI_SIGTRAP; |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1497 | info.si_code = VKI_TRAP_BRKPT; /* tjh: only ever called for a brkpt ins */ |
| 1498 | #if defined(VGA_x86) || defined(VGA_amd64) |
| 1499 | uc.uc_mcontext.trapno = 3; /* tjh: this is the x86 trap number |
| 1500 | for a breakpoint trap... */ |
| 1501 | #endif |
sewardj | 86df155 | 2006-02-07 20:56:41 +0000 | [diff] [blame] | 1502 | |
| 1503 | resume_scheduler(tid); |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1504 | deliver_signal(tid, &info, &uc); |
sewardj | 86df155 | 2006-02-07 20:56:41 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1507 | /* Make a signal pending for a thread, for later delivery. |
| 1508 | VG_(poll_signals) will arrange for it to be delivered at the right |
| 1509 | time. |
| 1510 | |
| 1511 | tid==0 means add it to the process-wide queue, and not sent it to a |
| 1512 | specific thread. |
| 1513 | */ |
sewardj | 2c5ffbe | 2005-03-12 13:32:06 +0000 | [diff] [blame] | 1514 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1515 | void queue_signal(ThreadId tid, const vki_siginfo_t *si) |
| 1516 | { |
| 1517 | ThreadState *tst; |
| 1518 | SigQueue *sq; |
| 1519 | vki_sigset_t savedmask; |
| 1520 | |
| 1521 | tst = VG_(get_ThreadState)(tid); |
| 1522 | |
| 1523 | /* Protect the signal queue against async deliveries */ |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 1524 | block_all_host_signals(&savedmask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1525 | |
| 1526 | if (tst->sig_queue == NULL) { |
| 1527 | tst->sig_queue = VG_(arena_malloc)(VG_AR_CORE, sizeof(*tst->sig_queue)); |
| 1528 | VG_(memset)(tst->sig_queue, 0, sizeof(*tst->sig_queue)); |
| 1529 | } |
| 1530 | sq = tst->sig_queue; |
| 1531 | |
| 1532 | if (VG_(clo_trace_signals)) |
| 1533 | VG_(message)(Vg_DebugMsg, "Queueing signal %d (idx %d) to thread %d", |
| 1534 | si->si_signo, sq->next, tid); |
| 1535 | |
| 1536 | /* Add signal to the queue. If the queue gets overrun, then old |
| 1537 | queued signals may get lost. |
| 1538 | |
| 1539 | XXX We should also keep a sigset of pending signals, so that at |
| 1540 | least a non-siginfo signal gets deliviered. |
| 1541 | */ |
| 1542 | if (sq->sigs[sq->next].si_signo != 0) |
| 1543 | VG_(message)(Vg_UserMsg, "Signal %d being dropped from thread %d's queue", |
| 1544 | sq->sigs[sq->next].si_signo, tid); |
| 1545 | |
| 1546 | sq->sigs[sq->next] = *si; |
| 1547 | sq->next = (sq->next+1) % N_QUEUED_SIGNALS; |
| 1548 | |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 1549 | restore_all_host_signals(&savedmask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | Returns the next queued signal for thread tid which is in "set". |
| 1554 | tid==0 means process-wide signal. Set si_signo to 0 when the |
| 1555 | signal has been delivered. |
| 1556 | |
| 1557 | Must be called with all signals blocked, to protect against async |
| 1558 | deliveries. |
| 1559 | */ |
| 1560 | static vki_siginfo_t *next_queued(ThreadId tid, const vki_sigset_t *set) |
| 1561 | { |
| 1562 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 1563 | SigQueue *sq; |
| 1564 | Int idx; |
| 1565 | vki_siginfo_t *ret = NULL; |
| 1566 | |
| 1567 | sq = tst->sig_queue; |
| 1568 | if (sq == NULL) |
| 1569 | goto out; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1570 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1571 | idx = sq->next; |
| 1572 | do { |
| 1573 | if (0) |
| 1574 | VG_(printf)("idx=%d si_signo=%d inset=%d\n", idx, |
| 1575 | sq->sigs[idx].si_signo, VG_(sigismember)(set, sq->sigs[idx].si_signo)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1576 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1577 | if (sq->sigs[idx].si_signo != 0 && VG_(sigismember)(set, sq->sigs[idx].si_signo)) { |
| 1578 | if (VG_(clo_trace_signals)) |
| 1579 | VG_(message)(Vg_DebugMsg, "Returning queued signal %d (idx %d) for thread %d", |
| 1580 | sq->sigs[idx].si_signo, idx, tid); |
| 1581 | ret = &sq->sigs[idx]; |
| 1582 | goto out; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1585 | idx = (idx + 1) % N_QUEUED_SIGNALS; |
| 1586 | } while(idx != sq->next); |
| 1587 | out: |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1588 | return ret; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1592 | Receive an async signal from the kernel. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1593 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1594 | This should only happen when the thread is blocked in a syscall, |
| 1595 | since that's the only time this set of signals is unblocked. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1596 | */ |
| 1597 | static |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1598 | void async_signalhandler ( Int sigNo, vki_siginfo_t *info, struct vki_ucontext *uc ) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1599 | { |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 1600 | ThreadId tid = VG_(lwpid_to_vgtid)(VG_(gettid)()); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1601 | ThreadState *tst = VG_(get_ThreadState)(tid); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1602 | |
tom | 9f4a7bf | 2005-11-13 00:01:20 +0000 | [diff] [blame] | 1603 | #ifdef VGO_linux |
| 1604 | /* The linux kernel uses the top 16 bits of si_code for it's own |
| 1605 | use and only exports the bottom 16 bits to user space - at least |
| 1606 | that is the theory, but it turns out that there are some kernels |
tom | c8cb9d0 | 2005-11-14 14:37:02 +0000 | [diff] [blame] | 1607 | around that forget to mask out the top 16 bits so we do it here. |
| 1608 | |
| 1609 | The kernel treats the bottom 16 bits as signed and (when it does |
| 1610 | mask them off) sign extends them when exporting to user space so |
| 1611 | we do the same thing here. */ |
tom | 95458c6 | 2005-11-13 11:52:39 +0000 | [diff] [blame] | 1612 | info->si_code = (Short)info->si_code; |
tom | 9f4a7bf | 2005-11-13 00:01:20 +0000 | [diff] [blame] | 1613 | #endif |
| 1614 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1615 | if (VG_(clo_trace_signals)) |
| 1616 | VG_(message)(Vg_DebugMsg, "Async handler got signal %d for tid %d info %d", |
| 1617 | sigNo, tid, info->si_code); |
| 1618 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1619 | vg_assert(tst->status == VgTs_WaitSys); |
| 1620 | |
| 1621 | /* The thread isn't currently running, make it so before going on */ |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1622 | VG_(acquire_BigLock)(tid, "async_signalhandler"); |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1623 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1624 | /* Update thread state properly */ |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1625 | VG_(fixup_guest_state_after_syscall_interrupted)( |
| 1626 | tid, |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1627 | VG_UCONTEXT_INSTR_PTR(uc), |
| 1628 | VG_UCONTEXT_SYSCALL_NUM(uc), |
| 1629 | VG_UCONTEXT_SYSCALL_SYSRES(uc), |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1630 | !!(scss.scss_per_sig[sigNo].scss_flags & VKI_SA_RESTART) |
| 1631 | ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1632 | |
| 1633 | /* Set up the thread's state to deliver a signal */ |
njn | 07ef6c0 | 2005-05-18 19:43:09 +0000 | [diff] [blame] | 1634 | if (!is_sig_ign(info->si_signo)) |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1635 | deliver_signal(tid, info, uc); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1636 | |
| 1637 | /* longjmp back to the thread's main loop to start executing the |
| 1638 | handler. */ |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1639 | resume_scheduler(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1640 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1641 | VG_(core_panic)("async_signalhandler: got unexpected signal while outside of scheduler"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1644 | /* Extend the stack to cover addr. maxsize is the limit the stack can grow to. |
| 1645 | |
| 1646 | Returns True on success, False on failure. |
| 1647 | |
| 1648 | Succeeds without doing anything if addr is already within a segment. |
| 1649 | |
| 1650 | Failure could be caused by: |
| 1651 | - addr not below a growable segment |
| 1652 | - new stack size would exceed maxsize |
| 1653 | - mmap failed for some other reason |
| 1654 | */ |
| 1655 | Bool VG_(extend_stack)(Addr addr, UInt maxsize) |
| 1656 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1657 | SizeT udelta; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1658 | |
| 1659 | /* Find the next Segment above addr */ |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1660 | NSegment const* seg |
| 1661 | = VG_(am_find_nsegment)(addr); |
| 1662 | NSegment const* seg_next |
| 1663 | = seg ? VG_(am_next_nsegment)( (NSegment*)seg, True/*fwds*/ ) |
| 1664 | : NULL; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (seg && seg->kind == SkAnonC) |
| 1667 | /* addr is already mapped. Nothing to do. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1668 | return True; |
| 1669 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1670 | /* Check that the requested new base is in a shrink-down |
| 1671 | reservation section which abuts an anonymous mapping that |
| 1672 | belongs to the client. */ |
| 1673 | if ( ! (seg |
| 1674 | && seg->kind == SkResvn |
| 1675 | && seg->smode == SmUpper |
| 1676 | && seg_next |
| 1677 | && seg_next->kind == SkAnonC |
| 1678 | && seg->end+1 == seg_next->start)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1679 | return False; |
| 1680 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1681 | udelta = VG_PGROUNDUP(seg_next->start - addr); |
| 1682 | VG_(debugLog)(1, "signals", |
| 1683 | "extending a stack base 0x%llx down by %lld\n", |
| 1684 | (ULong)seg_next->start, (ULong)udelta); |
| 1685 | if (! VG_(am_extend_into_adjacent_reservation_client) |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1686 | ( (NSegment*)seg_next, -(SSizeT)udelta )) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1687 | VG_(debugLog)(1, "signals", "extending a stack base: FAILED\n"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1688 | return False; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1689 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1690 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1691 | /* When we change the main stack, we have to let the stack handling |
| 1692 | code know about it. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1693 | VG_(change_stack)(VG_(clstk_id), addr, VG_(clstk_end)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1694 | |
| 1695 | if (VG_(clo_sanity_level) > 2) |
| 1696 | VG_(sanity_check_general)(False); |
| 1697 | |
| 1698 | return True; |
| 1699 | } |
| 1700 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1701 | static void (*fault_catcher)(Int sig, Addr addr) = NULL; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1702 | |
| 1703 | void VG_(set_fault_catcher)(void (*catcher)(Int, Addr)) |
| 1704 | { |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1705 | if (0) |
| 1706 | VG_(debugLog)(0, "signals", "set fault catcher to %p\n", catcher); |
njn | 50ae1a7 | 2005-04-08 23:28:23 +0000 | [diff] [blame] | 1707 | vg_assert2(NULL == catcher || NULL == fault_catcher, |
| 1708 | "Fault catcher is already registered"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1709 | |
| 1710 | fault_catcher = catcher; |
| 1711 | } |
| 1712 | |
| 1713 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1714 | /* |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1715 | Receive a sync signal from the host. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1716 | */ |
| 1717 | static |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1718 | void sync_signalhandler ( Int sigNo, vki_siginfo_t *info, struct vki_ucontext *uc ) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1719 | { |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 1720 | ThreadId tid = VG_(lwpid_to_vgtid)(VG_(gettid)()); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1721 | |
| 1722 | vg_assert(info != NULL); |
| 1723 | vg_assert(info->si_signo == sigNo); |
| 1724 | vg_assert(sigNo == VKI_SIGSEGV || |
| 1725 | sigNo == VKI_SIGBUS || |
| 1726 | sigNo == VKI_SIGFPE || |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1727 | sigNo == VKI_SIGILL || |
| 1728 | sigNo == VKI_SIGTRAP); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1729 | |
tom | 9f4a7bf | 2005-11-13 00:01:20 +0000 | [diff] [blame] | 1730 | #ifdef VGO_linux |
| 1731 | /* The linux kernel uses the top 16 bits of si_code for it's own |
| 1732 | use and only exports the bottom 16 bits to user space - at least |
| 1733 | that is the theory, but it turns out that there are some kernels |
tom | c8cb9d0 | 2005-11-14 14:37:02 +0000 | [diff] [blame] | 1734 | around that forget to mask out the top 16 bits so we do it here. |
| 1735 | |
| 1736 | The kernel treats the bottom 16 bits as signed and (when it does |
| 1737 | mask them off) sign extends them when exporting to user space so |
| 1738 | we do the same thing here. */ |
tom | 95458c6 | 2005-11-13 11:52:39 +0000 | [diff] [blame] | 1739 | info->si_code = (Short)info->si_code; |
tom | 9f4a7bf | 2005-11-13 00:01:20 +0000 | [diff] [blame] | 1740 | #endif |
| 1741 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1742 | if (info->si_code <= VKI_SI_USER) { |
| 1743 | /* If some user-process sent us one of these signals (ie, |
| 1744 | they're not the result of a faulting instruction), then treat |
| 1745 | it as an async signal. This is tricky because we could get |
| 1746 | this almost anywhere: |
| 1747 | - while generated client code |
| 1748 | Action: queue signal and return |
| 1749 | - while running Valgrind code |
| 1750 | Action: queue signal and return |
| 1751 | - while blocked in a syscall |
| 1752 | Action: make thread runnable, queue signal, resume scheduler |
| 1753 | */ |
| 1754 | if (VG_(threads)[tid].status == VgTs_WaitSys) { |
| 1755 | /* Since this signal interrupted a syscall, it means the |
| 1756 | client's signal mask was applied, so we can't get here |
| 1757 | unless the client wants this signal right now. This means |
| 1758 | we can simply use the async_signalhandler. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1759 | async_signalhandler(sigNo, info, uc); |
| 1760 | VG_(core_panic)("async_signalhandler returned!?\n"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1761 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1762 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1763 | if (info->VKI_SIGINFO_si_pid == 0) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1764 | /* There's a per-user limit of pending siginfo signals. If |
| 1765 | you exceed this, by having more than that number of |
| 1766 | pending signals with siginfo, then new signals are |
| 1767 | delivered without siginfo. This condition can be caused |
| 1768 | by any unrelated program you're running at the same time |
| 1769 | as Valgrind, if it has a large number of pending siginfo |
| 1770 | signals which it isn't taking delivery of. |
| 1771 | |
| 1772 | Since we depend on siginfo to work out why we were sent a |
| 1773 | signal and what we should do about it, we really can't |
| 1774 | continue unless we get it. */ |
| 1775 | VG_(message)(Vg_UserMsg, "Signal %d (%s) appears to have lost its siginfo; I can't go on.", |
| 1776 | sigNo, signame(sigNo)); |
| 1777 | VG_(message)(Vg_UserMsg, " This may be because one of your programs has consumed your"); |
| 1778 | VG_(message)(Vg_UserMsg, " ration of siginfo structures."); |
njn | 504b72b | 2005-08-16 01:39:34 +0000 | [diff] [blame] | 1779 | VG_(printf)( |
| 1780 | " For more information, see:\n" |
| 1781 | " http://kerneltrap.org/mailarchive/1/message/25599/thread\n" |
| 1782 | " Basically, some program on your system is building up a large queue of\n" |
| 1783 | " pending signals, and this causes the siginfo data for other signals to\n" |
| 1784 | " be dropped because it's exceeding a system limit. However, Valgrind\n" |
| 1785 | " absolutely needs siginfo for SIGSEGV. A workaround is to track down the\n" |
| 1786 | " offending program and avoid running it while using Valgrind, but there\n" |
| 1787 | " is no easy way to do this. Apparently the problem was fixed in kernel\n" |
| 1788 | " 2.6.12.\n"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1789 | |
| 1790 | /* It's a fatal signal, so we force the default handler. */ |
| 1791 | VG_(set_default_handler)(sigNo); |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1792 | deliver_signal(tid, info, uc); |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1793 | resume_scheduler(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1794 | VG_(exit)(99); /* If we can't resume, then just exit */ |
| 1795 | } |
| 1796 | |
| 1797 | if (VG_(clo_trace_signals)) |
| 1798 | VG_(message)(Vg_DebugMsg, "Routing user-sent sync signal %d via queue", |
| 1799 | sigNo); |
| 1800 | |
| 1801 | /* Since every thread has these signals unblocked, we can't rely |
| 1802 | on the kernel to route them properly, so we need to queue |
| 1803 | them manually. */ |
| 1804 | if (info->si_code == VKI_SI_TKILL) |
| 1805 | queue_signal(tid, info); /* directed to us specifically */ |
| 1806 | else |
| 1807 | queue_signal(0, info); /* shared pending */ |
| 1808 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1809 | return; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | if (VG_(clo_trace_signals)) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1813 | VG_(message)(Vg_DebugMsg, "signal %d arrived ... si_code=%d, " |
| 1814 | "EIP=%p, eip=%p", |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1815 | sigNo, info->si_code, VG_(get_IP)(tid), |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1816 | VG_UCONTEXT_INSTR_PTR(uc) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1817 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1818 | vg_assert(sigNo >= 1 && sigNo <= VG_(max_signal)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1819 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1820 | /* Check to see if someone is interested in faults. The fault |
| 1821 | catcher should never be set whilst we're in generated code, so |
| 1822 | check for that. AFAIK the only use of the catcher right now is |
| 1823 | memcheck's leak detector. |
| 1824 | */ |
| 1825 | if (fault_catcher) { |
| 1826 | vg_assert(VG_(in_generated_code) == False); |
| 1827 | |
| 1828 | (*fault_catcher)(sigNo, (Addr)info->VKI_SIGINFO_si_addr); |
| 1829 | /* If the catcher returns, then it didn't handle the fault, |
| 1830 | so carry on panicing. */ |
| 1831 | } |
| 1832 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1833 | /* Special fault-handling case. We can now get signals which can |
| 1834 | act upon and immediately restart the faulting instruction. |
| 1835 | */ |
| 1836 | if (info->si_signo == VKI_SIGSEGV) { |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1837 | Addr fault = (Addr)info->VKI_SIGINFO_si_addr; |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1838 | Addr esp = VG_(get_SP)(tid); |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1839 | NSegment const* seg |
| 1840 | = VG_(am_find_nsegment)(fault); |
| 1841 | NSegment const* seg_next |
| 1842 | = seg ? VG_(am_next_nsegment)( (NSegment*)seg, True/*fwds*/ ) |
| 1843 | : NULL; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1844 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1845 | if (VG_(clo_trace_signals)) { |
| 1846 | if (seg == NULL) |
| 1847 | VG_(message)(Vg_DebugMsg, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1848 | "SIGSEGV: si_code=%d faultaddr=%p tid=%d ESP=%p " |
| 1849 | "seg=NULL", |
| 1850 | info->si_code, fault, tid, esp); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1851 | else |
| 1852 | VG_(message)(Vg_DebugMsg, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1853 | "SIGSEGV: si_code=%d faultaddr=%p tid=%d ESP=%p " |
| 1854 | "seg=%p-%p", |
| 1855 | info->si_code, fault, tid, esp, seg->start, seg->end); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1856 | } |
tom | 148250b | 2005-11-12 00:13:20 +0000 | [diff] [blame] | 1857 | if (info->si_code == VKI_SEGV_MAPERR |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1858 | && seg |
| 1859 | && seg->kind == SkResvn |
| 1860 | && seg->smode == SmUpper |
| 1861 | && seg_next |
| 1862 | && seg_next->kind == SkAnonC |
| 1863 | && seg->end+1 == seg_next->start |
| 1864 | && fault >= (esp - VG_STACK_REDZONE_SZB)) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1865 | /* If the fault address is above esp but below the current known |
| 1866 | stack segment base, and it was a fault because there was |
| 1867 | nothing mapped there (as opposed to a permissions fault), |
| 1868 | then extend the stack segment. |
| 1869 | */ |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1870 | Addr base = VG_PGROUNDDN(esp - VG_STACK_REDZONE_SZB); |
njn | 50ba34e | 2005-04-04 02:41:42 +0000 | [diff] [blame] | 1871 | if (VG_(extend_stack)(base, VG_(threads)[tid].client_stack_szB)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1872 | if (VG_(clo_trace_signals)) |
| 1873 | VG_(message)(Vg_DebugMsg, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1874 | " -> extended stack base to %p", |
| 1875 | VG_PGROUNDDN(fault)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1876 | return; // extension succeeded, restart host (hence guest) |
| 1877 | // instruction |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1878 | } else |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1879 | VG_(message)(Vg_UserMsg, |
| 1880 | "Stack overflow in thread %d: can't grow stack to %p", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1881 | tid, fault); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1882 | } |
njn | b69f7c1 | 2005-06-24 22:17:38 +0000 | [diff] [blame] | 1883 | /* Fall into normal signal handling for all other cases */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1886 | /* OK, this is a signal we really have to deal with. If it came |
| 1887 | from the client's code, then we can jump back into the scheduler |
| 1888 | and have it delivered. Otherwise it's a Valgrind bug. */ |
| 1889 | { |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 1890 | ThreadState *tst |
| 1891 | = VG_(get_ThreadState)(VG_(lwpid_to_vgtid)(VG_(gettid)())); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1892 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1893 | if (VG_(sigismember)(&tst->sig_mask, sigNo)) { |
| 1894 | /* signal is blocked, but they're not allowed to block faults */ |
| 1895 | VG_(set_default_handler)(sigNo); |
| 1896 | } |
| 1897 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1898 | if (VG_(in_generated_code)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1899 | /* Can't continue; must longjmp back to the scheduler and thus |
| 1900 | enter the sighandler immediately. */ |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 1901 | deliver_signal(tid, info, uc); |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1902 | resume_scheduler(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1905 | /* If resume_scheduler returns or its our fault, it means we |
| 1906 | don't have longjmp set up, implying that we weren't running |
| 1907 | client code, and therefore it was actually generated by |
| 1908 | Valgrind internally. |
| 1909 | */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1910 | VG_(message)(Vg_DebugMsg, |
njn | 038ee9e | 2005-12-12 15:54:50 +0000 | [diff] [blame] | 1911 | "VALGRIND INTERNAL ERROR: Valgrind received a signal %d (%s) - exiting", |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1912 | sigNo, signame(sigNo)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1913 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1914 | VG_(message)(Vg_DebugMsg, |
njn | d0073f4 | 2005-05-14 17:00:25 +0000 | [diff] [blame] | 1915 | "si_code=%x; Faulting address: %p; sp: %p", |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1916 | info->si_code, info->VKI_SIGINFO_si_addr, |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1917 | VG_UCONTEXT_STACK_PTR(uc)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1918 | |
| 1919 | if (0) |
| 1920 | VG_(kill_self)(sigNo); /* generate a core dump */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1921 | |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 1922 | //if (tid == 0) /* could happen after everyone has exited */ |
| 1923 | // tid = VG_(master_tid); |
| 1924 | vg_assert(tid != 0); |
| 1925 | |
njn | db13c4f | 2005-06-01 00:00:46 +0000 | [diff] [blame] | 1926 | VG_(core_panic_at)("Killed by fatal signal", |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1927 | VG_UCONTEXT_INSTR_PTR(uc), |
| 1928 | VG_UCONTEXT_STACK_PTR(uc), |
sewardj | acaec5f | 2005-08-19 16:02:59 +0000 | [diff] [blame] | 1929 | VG_UCONTEXT_FRAME_PTR(uc), |
| 1930 | VG_UCONTEXT_LINK_REG(uc)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1936 | Kill this thread. Makes it leave any syscall it might be currently |
| 1937 | blocked in, and return to the scheduler. This doesn't mark the thread |
| 1938 | as exiting; that's the caller's job. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1939 | */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1940 | static void sigvgkill_handler(int signo, vki_siginfo_t *si, struct vki_ucontext *uc) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1941 | { |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 1942 | ThreadId tid = VG_(lwpid_to_vgtid)(VG_(gettid)()); |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1943 | ThreadStatus at_signal = VG_(threads)[tid].status; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1944 | |
| 1945 | if (VG_(clo_trace_signals)) |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1946 | VG_(message)(Vg_DebugMsg, |
| 1947 | "sigvgkill for lwp %d tid %d", VG_(gettid)(), tid); |
| 1948 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1949 | VG_(acquire_BigLock)(tid, "sigvgkill_handler"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1950 | |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 1951 | vg_assert(signo == VG_SIGVGKILL); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1952 | vg_assert(si->si_signo == signo); |
| 1953 | |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1954 | /* jrs 2006 August 3: the following assertion seems incorrect to |
| 1955 | me, and fails on AIX. sigvgkill could be sent to a thread which |
| 1956 | is runnable - see VG_(nuke_all_threads_except) in the scheduler. |
| 1957 | Hence comment these out .. |
| 1958 | |
| 1959 | vg_assert(VG_(threads)[tid].status == VgTs_WaitSys); |
| 1960 | VG_(post_syscall)(tid); |
| 1961 | |
| 1962 | and instead do: |
| 1963 | */ |
| 1964 | if (at_signal == VgTs_WaitSys) |
| 1965 | VG_(post_syscall)(tid); |
| 1966 | /* jrs 2006 August 3 ends */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1967 | |
njn | 06244e7 | 2005-06-21 22:27:19 +0000 | [diff] [blame] | 1968 | resume_scheduler(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1969 | |
| 1970 | VG_(core_panic)("sigvgkill_handler couldn't return to the scheduler\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1973 | static __attribute((unused)) |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1974 | void pp_ksigaction ( struct vki_sigaction* sa ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1975 | { |
| 1976 | Int i; |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1977 | VG_(printf)("pp_ksigaction: handler %p, flags 0x%x, restorer %p\n", |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 1978 | sa->ksa_handler, |
| 1979 | (UInt)sa->sa_flags, |
| 1980 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
| 1981 | sa->sa_restorer |
| 1982 | # else |
| 1983 | 0 |
| 1984 | # endif |
| 1985 | ); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 1986 | VG_(printf)("pp_ksigaction: { "); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1987 | for (i = 1; i <= VG_(max_signal); i++) |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1988 | if (VG_(sigismember(&(sa->sa_mask),i))) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1989 | VG_(printf)("%d ", i); |
| 1990 | VG_(printf)("}\n"); |
| 1991 | } |
| 1992 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1993 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1994 | Force signal handler to default |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1995 | */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1996 | void VG_(set_default_handler)(Int signo) |
| 1997 | { |
| 1998 | struct vki_sigaction sa; |
| 1999 | |
| 2000 | sa.ksa_handler = VKI_SIG_DFL; |
| 2001 | sa.sa_flags = 0; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2002 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2003 | sa.sa_restorer = 0; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2004 | # endif |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2005 | VG_(sigemptyset)(&sa.sa_mask); |
| 2006 | |
| 2007 | VG_(do_sys_sigaction)(signo, &sa, NULL); |
| 2008 | } |
| 2009 | |
| 2010 | /* |
| 2011 | Poll for pending signals, and set the next one up for delivery. |
| 2012 | */ |
| 2013 | void VG_(poll_signals)(ThreadId tid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2014 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2015 | vki_siginfo_t si, *sip; |
| 2016 | vki_sigset_t pollset; |
| 2017 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 2018 | Int i; |
| 2019 | vki_sigset_t saved_mask; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2020 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2021 | /* look for all the signals this thread isn't blocking */ |
| 2022 | for(i = 0; i < _VKI_NSIG_WORDS; i++) |
| 2023 | pollset.sig[i] = ~tst->sig_mask.sig[i]; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2024 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2025 | //VG_(printf)("tid %d pollset=%08x%08x\n", tid, pollset.sig[1], pollset.sig[0]); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2026 | |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 2027 | block_all_host_signals(&saved_mask); // protect signal queue |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2028 | |
| 2029 | /* First look for any queued pending signals */ |
| 2030 | sip = next_queued(tid, &pollset); /* this thread */ |
| 2031 | |
| 2032 | if (sip == NULL) |
| 2033 | sip = next_queued(0, &pollset); /* process-wide */ |
| 2034 | |
| 2035 | /* If there was nothing queued, ask the kernel for a pending signal */ |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2036 | if (sip == NULL && VG_(sigtimedwait_zero)(&pollset, &si) > 0) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2037 | if (VG_(clo_trace_signals)) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2038 | VG_(message)(Vg_DebugMsg, "poll_signals: got signal %d " |
| 2039 | "for thread %d", si.si_signo, tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2040 | sip = &si; |
thughes | 8abf392 | 2004-10-16 10:59:49 +0000 | [diff] [blame] | 2041 | } |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 2042 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2043 | if (sip != NULL) { |
| 2044 | /* OK, something to do; deliver it */ |
| 2045 | if (VG_(clo_trace_signals)) |
| 2046 | VG_(message)(Vg_DebugMsg, "Polling found signal %d for tid %d", |
| 2047 | sip->si_signo, tid); |
njn | 07ef6c0 | 2005-05-18 19:43:09 +0000 | [diff] [blame] | 2048 | if (!is_sig_ign(sip->si_signo)) |
tom | adacaf9 | 2007-12-21 10:24:24 +0000 | [diff] [blame] | 2049 | deliver_signal(tid, sip, NULL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2050 | else if (VG_(clo_trace_signals)) |
| 2051 | VG_(message)(Vg_DebugMsg, " signal %d ignored", sip->si_signo); |
| 2052 | |
| 2053 | sip->si_signo = 0; /* remove from signal queue, if that's |
| 2054 | where it came from */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 2057 | restore_all_host_signals(&saved_mask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2060 | /* At startup, copy the process' real signal state to the SCSS. |
| 2061 | Whilst doing this, block all real signals. Then calculate SKSS and |
| 2062 | set the kernel to that. Also initialise DCSS. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2063 | */ |
| 2064 | void VG_(sigstartup_actions) ( void ) |
| 2065 | { |
| 2066 | Int i, ret; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2067 | vki_sigset_t saved_procmask; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2068 | struct vki_sigaction sa; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2069 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 2070 | /* VG_(printf)("SIGSTARTUP\n"); */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2071 | /* Block all signals. saved_procmask remembers the previous mask, |
| 2072 | which the first thread inherits. |
| 2073 | */ |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 2074 | block_all_host_signals( &saved_procmask ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2075 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2076 | /* Copy per-signal settings to SCSS. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2077 | for (i = 1; i <= _VKI_NSIG; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2078 | /* Get the old host action */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 2079 | ret = VG_(sigaction)(i, NULL, &sa); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2080 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2081 | if (ret != 0) |
| 2082 | break; |
| 2083 | |
| 2084 | /* Try setting it back to see if this signal is really |
| 2085 | available */ |
| 2086 | if (i >= VKI_SIGRTMIN) { |
| 2087 | struct vki_sigaction tsa; |
| 2088 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 2089 | tsa.ksa_handler = (void *)sync_signalhandler; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2090 | tsa.sa_flags = VKI_SA_SIGINFO; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2091 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2092 | tsa.sa_restorer = 0; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2093 | # endif |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2094 | VG_(sigfillset)(&tsa.sa_mask); |
| 2095 | |
| 2096 | /* try setting it to some arbitrary handler */ |
| 2097 | if (VG_(sigaction)(i, &tsa, NULL) != 0) { |
| 2098 | /* failed - not really usable */ |
| 2099 | break; |
| 2100 | } |
| 2101 | |
| 2102 | ret = VG_(sigaction)(i, &sa, NULL); |
| 2103 | vg_assert(ret == 0); |
| 2104 | } |
| 2105 | |
| 2106 | VG_(max_signal) = i; |
| 2107 | |
| 2108 | if (VG_(clo_trace_signals) && VG_(clo_verbosity) > 2) |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 2109 | VG_(printf)("snaffling handler 0x%lx for signal %d\n", |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2110 | (Addr)(sa.ksa_handler), i ); |
| 2111 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 2112 | scss.scss_per_sig[i].scss_handler = sa.ksa_handler; |
| 2113 | scss.scss_per_sig[i].scss_flags = sa.sa_flags; |
| 2114 | scss.scss_per_sig[i].scss_mask = sa.sa_mask; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2115 | scss.scss_per_sig[i].scss_restorer = 0; |
| 2116 | # if !defined(VGP_ppc32_aix5) && !defined(VGP_ppc64_aix5) |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 2117 | scss.scss_per_sig[i].scss_restorer = sa.sa_restorer; |
sewardj | 489bfec | 2006-10-17 02:00:29 +0000 | [diff] [blame] | 2118 | # endif |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2121 | if (VG_(clo_trace_signals)) |
| 2122 | VG_(message)(Vg_DebugMsg, "Max kernel-supported signal is %d", VG_(max_signal)); |
| 2123 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2124 | /* Our private internal signals are treated as ignored */ |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 2125 | scss.scss_per_sig[VG_SIGVGKILL].scss_handler = VKI_SIG_IGN; |
| 2126 | scss.scss_per_sig[VG_SIGVGKILL].scss_flags = VKI_SA_SIGINFO; |
| 2127 | VG_(sigfillset)(&scss.scss_per_sig[VG_SIGVGKILL].scss_mask); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2128 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2129 | /* Copy the process' signal mask into the root thread. */ |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2130 | vg_assert(VG_(threads)[1].status == VgTs_Init); |
| 2131 | for (i = 2; i < VG_N_THREADS; i++) |
| 2132 | vg_assert(VG_(threads)[i].status == VgTs_Empty); |
| 2133 | |
| 2134 | VG_(threads)[1].sig_mask = saved_procmask; |
| 2135 | VG_(threads)[1].tmp_sig_mask = saved_procmask; |
sewardj | 7a61d91 | 2002-04-25 01:27:35 +0000 | [diff] [blame] | 2136 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2137 | /* Calculate SKSS and apply it. This also sets the initial kernel |
| 2138 | mask we need to run with. */ |
nethercote | 9dd1151 | 2004-08-04 15:31:30 +0000 | [diff] [blame] | 2139 | handle_SCSS_change( True /* forced update */ ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2140 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2141 | /* Leave with all signals still blocked; the thread scheduler loop |
| 2142 | will set the appropriate mask at the appropriate time. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2145 | /*--------------------------------------------------------------------*/ |
njn | ed6b824 | 2005-06-01 00:03:17 +0000 | [diff] [blame] | 2146 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2147 | /*--------------------------------------------------------------------*/ |