sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 3 | /*--- Thread scheduling. scheduler.c ---*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c0ae705 | 2005-08-25 22:55:19 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 9 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2010 Julian Seward |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | e663cb9 | 2002-04-12 10:26:32 +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 | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 31 | /* |
| 32 | Overview |
| 33 | |
| 34 | Valgrind tries to emulate the kernel's threading as closely as |
| 35 | possible. The client does all threading via the normal syscalls |
| 36 | (on Linux: clone, etc). Valgrind emulates this by creating exactly |
| 37 | the same process structure as would be created without Valgrind. |
| 38 | There are no extra threads. |
| 39 | |
| 40 | The main difference is that Valgrind only allows one client thread |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 41 | to run at once. This is controlled with the CPU Big Lock, |
| 42 | "the_BigLock". Any time a thread wants to run client code or |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 43 | manipulate any shared state (which is anything other than its own |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 44 | ThreadState entry), it must hold the_BigLock. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 45 | |
| 46 | When a thread is about to block in a blocking syscall, it releases |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 47 | the_BigLock, and re-takes it when it becomes runnable again (either |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 48 | because the syscall finished, or we took a signal). |
| 49 | |
| 50 | VG_(scheduler) therefore runs in each thread. It returns only when |
| 51 | the thread is exiting, either because it exited itself, or it was |
| 52 | told to exit by another thread. |
| 53 | |
| 54 | This file is almost entirely OS-independent. The details of how |
| 55 | the OS handles threading and signalling are abstracted away and |
njn | 1277109 | 2005-06-18 02:18:04 +0000 | [diff] [blame] | 56 | implemented elsewhere. [Some of the functions have worked their |
| 57 | way back for the moment, until we do an OS port in earnest...] |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 60 | #include "pub_core_basics.h" |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 61 | #include "pub_core_debuglog.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 62 | #include "pub_core_vki.h" |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 63 | #include "pub_core_vkiscnums.h" // __NR_sched_yield |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 64 | #include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 65 | #include "pub_core_threadstate.h" |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 66 | #include "pub_core_aspacemgr.h" |
njn | 93fe3b2 | 2005-12-21 20:22:52 +0000 | [diff] [blame] | 67 | #include "pub_core_clreq.h" // for VG_USERREQ__* |
njn | 36b66df | 2005-05-12 05:13:04 +0000 | [diff] [blame] | 68 | #include "pub_core_dispatch.h" |
njn | f4c5016 | 2005-06-20 14:18:12 +0000 | [diff] [blame] | 69 | #include "pub_core_errormgr.h" // For VG_(get_n_errs_found)() |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 70 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 71 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 72 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 73 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 74 | #include "pub_core_libcsignal.h" |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 75 | #if defined(VGO_darwin) |
| 76 | #include "pub_core_mach.h" |
| 77 | #endif |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 78 | #include "pub_core_machine.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 79 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 80 | #include "pub_core_options.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 81 | #include "pub_core_replacemalloc.h" |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 82 | #include "pub_core_signals.h" |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 83 | #include "pub_core_stacks.h" |
njn | f4c5016 | 2005-06-20 14:18:12 +0000 | [diff] [blame] | 84 | #include "pub_core_stacktrace.h" // For VG_(get_and_pp_StackTrace)() |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 85 | #include "pub_core_syscall.h" |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 86 | #include "pub_core_syswrap.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 87 | #include "pub_core_tooliface.h" |
njn | f4c5016 | 2005-06-20 14:18:12 +0000 | [diff] [blame] | 88 | #include "pub_core_translate.h" // For VG_(translate)() |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 89 | #include "pub_core_transtab.h" |
sewardj | c8259b8 | 2009-04-22 22:42:10 +0000 | [diff] [blame] | 90 | #include "pub_core_debuginfo.h" // VG_(di_notify_pdb_debuginfo) |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 91 | #include "priv_sema.h" |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 92 | #include "pub_core_scheduler.h" // self |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 93 | #include "pub_core_redir.h" |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 94 | |
sewardj | 63fed7f | 2006-01-17 02:02:47 +0000 | [diff] [blame] | 95 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 96 | /* --------------------------------------------------------------------- |
| 97 | Types and globals for the scheduler. |
| 98 | ------------------------------------------------------------------ */ |
| 99 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 100 | /* ThreadId and ThreadState are defined elsewhere*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 101 | |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 102 | /* Defines the thread-scheduling timeslice, in terms of the number of |
| 103 | basic blocks we attempt to run each thread for. Smaller values |
| 104 | give finer interleaving but much increased scheduling overheads. */ |
sewardj | ea3a99f | 2006-05-07 14:37:03 +0000 | [diff] [blame] | 105 | #define SCHEDULING_QUANTUM 100000 |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 106 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 107 | /* If False, a fault is Valgrind-internal (ie, a bug) */ |
| 108 | Bool VG_(in_generated_code) = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 109 | |
njn | de583aa | 2005-05-11 18:57:02 +0000 | [diff] [blame] | 110 | /* Counts downwards in VG_(run_innerloop). */ |
| 111 | UInt VG_(dispatch_ctr); |
| 112 | |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 113 | /* 64-bit counter for the number of basic blocks done. */ |
| 114 | static ULong bbs_done = 0; |
| 115 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 116 | /* Forwards */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 117 | static void do_client_request ( ThreadId tid ); |
| 118 | static void scheduler_sanity ( ThreadId tid ); |
| 119 | static void mostly_clear_thread_record ( ThreadId tid ); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 120 | |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 121 | /* Stats. */ |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 122 | static ULong n_scheduling_events_MINOR = 0; |
| 123 | static ULong n_scheduling_events_MAJOR = 0; |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 124 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 125 | /* Sanity checking counts. */ |
| 126 | static UInt sanity_fast_count = 0; |
| 127 | static UInt sanity_slow_count = 0; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 128 | |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 129 | void VG_(print_scheduler_stats)(void) |
| 130 | { |
| 131 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 132 | "scheduler: %'llu jumps (bb entries).\n", bbs_done ); |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 133 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 134 | "scheduler: %'llu/%'llu major/minor sched events.\n", |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 135 | n_scheduling_events_MAJOR, n_scheduling_events_MINOR); |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 136 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 137 | " sanity: %d cheap, %d expensive checks.\n", |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 138 | sanity_fast_count, sanity_slow_count ); |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 139 | } |
| 140 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 141 | /* CPU semaphore, so that threads can run exclusively */ |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 142 | static vg_sema_t the_BigLock; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 143 | |
| 144 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 145 | /* --------------------------------------------------------------------- |
| 146 | Helper functions for the scheduler. |
| 147 | ------------------------------------------------------------------ */ |
| 148 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 149 | static |
| 150 | void print_sched_event ( ThreadId tid, Char* what ) |
| 151 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 152 | VG_(message)(Vg_DebugMsg, " SCHED[%d]: %s\n", tid, what ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 153 | } |
| 154 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 155 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 156 | HChar* name_of_sched_event ( UInt event ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 157 | { |
| 158 | switch (event) { |
sewardj | a0fef1b | 2005-11-03 13:46:30 +0000 | [diff] [blame] | 159 | case VEX_TRC_JMP_SYS_SYSCALL: return "SYSCALL"; |
| 160 | case VEX_TRC_JMP_SYS_INT32: return "INT32"; |
| 161 | case VEX_TRC_JMP_SYS_INT128: return "INT128"; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 162 | case VEX_TRC_JMP_SYS_INT129: return "INT129"; |
| 163 | case VEX_TRC_JMP_SYS_INT130: return "INT130"; |
sewardj | a0fef1b | 2005-11-03 13:46:30 +0000 | [diff] [blame] | 164 | case VEX_TRC_JMP_SYS_SYSENTER: return "SYSENTER"; |
sewardj | d79ef68 | 2004-11-26 13:25:17 +0000 | [diff] [blame] | 165 | case VEX_TRC_JMP_CLIENTREQ: return "CLIENTREQ"; |
| 166 | case VEX_TRC_JMP_YIELD: return "YIELD"; |
sewardj | 45f02c4 | 2005-02-05 18:27:14 +0000 | [diff] [blame] | 167 | case VEX_TRC_JMP_NODECODE: return "NODECODE"; |
sewardj | 1f430d3 | 2005-12-16 01:07:11 +0000 | [diff] [blame] | 168 | case VEX_TRC_JMP_MAPFAIL: return "MAPFAIL"; |
tom | 360ed5d | 2006-01-13 09:26:23 +0000 | [diff] [blame] | 169 | case VEX_TRC_JMP_NOREDIR: return "NOREDIR"; |
sewardj | 1f430d3 | 2005-12-16 01:07:11 +0000 | [diff] [blame] | 170 | case VEX_TRC_JMP_EMWARN: return "EMWARN"; |
| 171 | case VEX_TRC_JMP_TINVAL: return "TINVAL"; |
| 172 | case VG_TRC_INVARIANT_FAILED: return "INVFAILED"; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 173 | case VG_TRC_INNER_COUNTERZERO: return "COUNTERZERO"; |
| 174 | case VG_TRC_INNER_FASTMISS: return "FASTMISS"; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 175 | case VG_TRC_FAULT_SIGNAL: return "FAULTSIGNAL"; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 176 | default: return "??UNKNOWN??"; |
| 177 | } |
| 178 | } |
| 179 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 180 | /* Allocate a completely empty ThreadState record. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 181 | ThreadId VG_(alloc_ThreadState) ( void ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 182 | { |
| 183 | Int i; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 184 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 185 | if (VG_(threads)[i].status == VgTs_Empty) { |
| 186 | VG_(threads)[i].status = VgTs_Init; |
| 187 | VG_(threads)[i].exitreason = VgSrc_None; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 188 | return i; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 189 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 190 | } |
| 191 | VG_(printf)("vg_alloc_ThreadState: no free slots available\n"); |
| 192 | VG_(printf)("Increase VG_N_THREADS, rebuild and try again.\n"); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 193 | VG_(core_panic)("VG_N_THREADS is too low"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 194 | /*NOTREACHED*/ |
| 195 | } |
| 196 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 197 | /* |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 198 | Mark a thread as Runnable. This will block until the_BigLock is |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 199 | available, so that we get exclusive access to all the shared |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 200 | structures and the CPU. Up until we get the_BigLock, we must not |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 201 | touch any shared state. |
| 202 | |
| 203 | When this returns, we'll actually be running. |
| 204 | */ |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 205 | void VG_(acquire_BigLock)(ThreadId tid, HChar* who) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 206 | { |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 207 | ThreadState *tst; |
| 208 | |
| 209 | #if 0 |
| 210 | if (VG_(clo_trace_sched)) { |
| 211 | HChar buf[100]; |
| 212 | vg_assert(VG_(strlen)(who) <= 100-50); |
| 213 | VG_(sprintf)(buf, "waiting for lock (%s)", who); |
| 214 | print_sched_event(tid, buf); |
| 215 | } |
| 216 | #endif |
| 217 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 218 | /* First, acquire the_BigLock. We can't do anything else safely |
| 219 | prior to this point. Even doing debug printing prior to this |
| 220 | point is, technically, wrong. */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 221 | ML_(sema_down)(&the_BigLock, False/*not LL*/); |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 222 | |
| 223 | tst = VG_(get_ThreadState)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 224 | |
| 225 | vg_assert(tst->status != VgTs_Runnable); |
| 226 | |
| 227 | tst->status = VgTs_Runnable; |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 228 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 229 | if (VG_(running_tid) != VG_INVALID_THREADID) |
| 230 | VG_(printf)("tid %d found %d running\n", tid, VG_(running_tid)); |
| 231 | vg_assert(VG_(running_tid) == VG_INVALID_THREADID); |
| 232 | VG_(running_tid) = tid; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 233 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 234 | { Addr gsp = VG_(get_SP)(tid); |
| 235 | VG_(unknown_SP_update)(gsp, gsp, 0/*unknown origin*/); |
| 236 | } |
tom | e0008d6 | 2005-11-10 15:02:42 +0000 | [diff] [blame] | 237 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 238 | if (VG_(clo_trace_sched)) { |
| 239 | HChar buf[150]; |
| 240 | vg_assert(VG_(strlen)(who) <= 150-50); |
| 241 | VG_(sprintf)(buf, " acquired lock (%s)", who); |
| 242 | print_sched_event(tid, buf); |
| 243 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 244 | } |
| 245 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 246 | /* |
| 247 | Set a thread into a sleeping state, and give up exclusive access to |
| 248 | the CPU. On return, the thread must be prepared to block until it |
| 249 | is ready to run again (generally this means blocking in a syscall, |
| 250 | but it may mean that we remain in a Runnable state and we're just |
| 251 | yielding the CPU to another thread). |
| 252 | */ |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 253 | void VG_(release_BigLock)(ThreadId tid, ThreadStatus sleepstate, HChar* who) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 254 | { |
| 255 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 256 | |
| 257 | vg_assert(tst->status == VgTs_Runnable); |
| 258 | |
| 259 | vg_assert(sleepstate == VgTs_WaitSys || |
| 260 | sleepstate == VgTs_Yielding); |
| 261 | |
| 262 | tst->status = sleepstate; |
| 263 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 264 | vg_assert(VG_(running_tid) == tid); |
| 265 | VG_(running_tid) = VG_INVALID_THREADID; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 266 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 267 | if (VG_(clo_trace_sched)) { |
| 268 | Char buf[200]; |
| 269 | vg_assert(VG_(strlen)(who) <= 200-100); |
| 270 | VG_(sprintf)(buf, "releasing lock (%s) -> %s", |
| 271 | who, VG_(name_of_ThreadStatus)(sleepstate)); |
| 272 | print_sched_event(tid, buf); |
| 273 | } |
| 274 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 275 | /* Release the_BigLock; this will reschedule any runnable |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 276 | thread. */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 277 | ML_(sema_up)(&the_BigLock, False/*not LL*/); |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 278 | } |
| 279 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 280 | /* See pub_core_scheduler.h for description */ |
| 281 | void VG_(acquire_BigLock_LL) ( HChar* who ) |
| 282 | { |
| 283 | ML_(sema_down)(&the_BigLock, True/*LL*/); |
| 284 | } |
| 285 | |
| 286 | /* See pub_core_scheduler.h for description */ |
| 287 | void VG_(release_BigLock_LL) ( HChar* who ) |
| 288 | { |
| 289 | ML_(sema_up)(&the_BigLock, True/*LL*/); |
| 290 | } |
| 291 | |
| 292 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 293 | /* Clear out the ThreadState and release the semaphore. Leaves the |
| 294 | ThreadState in VgTs_Zombie state, so that it doesn't get |
| 295 | reallocated until the caller is really ready. */ |
| 296 | void VG_(exit_thread)(ThreadId tid) |
| 297 | { |
| 298 | vg_assert(VG_(is_valid_tid)(tid)); |
| 299 | vg_assert(VG_(is_running_thread)(tid)); |
| 300 | vg_assert(VG_(is_exiting)(tid)); |
| 301 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 302 | mostly_clear_thread_record(tid); |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 303 | VG_(running_tid) = VG_INVALID_THREADID; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 304 | |
| 305 | /* There should still be a valid exitreason for this thread */ |
| 306 | vg_assert(VG_(threads)[tid].exitreason != VgSrc_None); |
| 307 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 308 | if (VG_(clo_trace_sched)) |
| 309 | print_sched_event(tid, "release lock in VG_(exit_thread)"); |
| 310 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 311 | ML_(sema_up)(&the_BigLock, False/*not LL*/); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 312 | } |
| 313 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 314 | /* If 'tid' is blocked in a syscall, send it SIGVGKILL so as to get it |
| 315 | out of the syscall and onto doing the next thing, whatever that is. |
| 316 | If it isn't blocked in a syscall, has no effect on the thread. */ |
| 317 | void VG_(get_thread_out_of_syscall)(ThreadId tid) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 318 | { |
| 319 | vg_assert(VG_(is_valid_tid)(tid)); |
| 320 | vg_assert(!VG_(is_running_thread)(tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 321 | |
| 322 | if (VG_(threads)[tid].status == VgTs_WaitSys) { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 323 | if (VG_(clo_trace_signals)) { |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 324 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 325 | "get_thread_out_of_syscall zaps tid %d lwp %d\n", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 326 | tid, VG_(threads)[tid].os_state.lwpid); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 327 | } |
| 328 | # if defined(VGO_darwin) |
| 329 | { |
| 330 | // GrP fixme use mach primitives on darwin? |
| 331 | // GrP fixme thread_abort_safely? |
| 332 | // GrP fixme race for thread with WaitSys set but not in syscall yet? |
| 333 | extern kern_return_t thread_abort(mach_port_t); |
| 334 | thread_abort(VG_(threads)[tid].os_state.lwpid); |
| 335 | } |
| 336 | # else |
| 337 | { |
| 338 | __attribute__((unused)) |
| 339 | Int r = VG_(tkill)(VG_(threads)[tid].os_state.lwpid, VG_SIGVGKILL); |
| 340 | /* JRS 2009-Mar-20: should we assert for r==0 (tkill succeeded)? |
| 341 | I'm really not sure. Here's a race scenario which argues |
| 342 | that we shoudn't; but equally I'm not sure the scenario is |
| 343 | even possible, because of constraints caused by the question |
| 344 | of who holds the BigLock when. |
| 345 | |
| 346 | Target thread tid does sys_read on a socket and blocks. This |
| 347 | function gets called, and we observe correctly that tid's |
| 348 | status is WaitSys but then for whatever reason this function |
| 349 | goes very slowly for a while. Then data arrives from |
| 350 | wherever, tid's sys_read returns, tid exits. Then we do |
| 351 | tkill on tid, but tid no longer exists; tkill returns an |
| 352 | error code and the assert fails. */ |
| 353 | /* vg_assert(r == 0); */ |
| 354 | } |
| 355 | # endif |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | Yield the CPU for a short time to let some other thread run. |
| 361 | */ |
| 362 | void VG_(vg_yield)(void) |
| 363 | { |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 364 | ThreadId tid = VG_(running_tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 365 | |
| 366 | vg_assert(tid != VG_INVALID_THREADID); |
| 367 | vg_assert(VG_(threads)[tid].os_state.lwpid == VG_(gettid)()); |
| 368 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 369 | VG_(release_BigLock)(tid, VgTs_Yielding, "VG_(vg_yield)"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | Tell the kernel we're yielding. |
| 373 | */ |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 374 | VG_(do_syscall0)(__NR_sched_yield); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 375 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 376 | VG_(acquire_BigLock)(tid, "VG_(vg_yield)"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 380 | /* Set the standard set of blocked signals, used whenever we're not |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 381 | running a client syscall. */ |
njn | 1dcee09 | 2009-02-24 03:07:37 +0000 | [diff] [blame] | 382 | static void block_signals(void) |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 383 | { |
| 384 | vki_sigset_t mask; |
| 385 | |
| 386 | VG_(sigfillset)(&mask); |
| 387 | |
| 388 | /* Don't block these because they're synchronous */ |
| 389 | VG_(sigdelset)(&mask, VKI_SIGSEGV); |
| 390 | VG_(sigdelset)(&mask, VKI_SIGBUS); |
| 391 | VG_(sigdelset)(&mask, VKI_SIGFPE); |
| 392 | VG_(sigdelset)(&mask, VKI_SIGILL); |
| 393 | VG_(sigdelset)(&mask, VKI_SIGTRAP); |
| 394 | |
| 395 | /* Can't block these anyway */ |
| 396 | VG_(sigdelset)(&mask, VKI_SIGSTOP); |
| 397 | VG_(sigdelset)(&mask, VKI_SIGKILL); |
| 398 | |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 399 | VG_(sigprocmask)(VKI_SIG_SETMASK, &mask, NULL); |
| 400 | } |
| 401 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 402 | static void os_state_clear(ThreadState *tst) |
| 403 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 404 | tst->os_state.lwpid = 0; |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 405 | tst->os_state.threadgroup = 0; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 406 | # if defined(VGO_linux) |
| 407 | /* no other fields to clear */ |
| 408 | # elif defined(VGO_aix5) |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 409 | tst->os_state.cancel_async = False; |
| 410 | tst->os_state.cancel_disabled = False; |
| 411 | tst->os_state.cancel_progress = Canc_NoRequest; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 412 | # elif defined(VGO_darwin) |
| 413 | tst->os_state.post_mach_trap_fn = NULL; |
| 414 | tst->os_state.pthread = 0; |
| 415 | tst->os_state.func_arg = 0; |
| 416 | VG_(memset)(&tst->os_state.child_go, 0, sizeof(tst->os_state.child_go)); |
| 417 | VG_(memset)(&tst->os_state.child_done, 0, sizeof(tst->os_state.child_done)); |
| 418 | tst->os_state.wq_jmpbuf_valid = False; |
| 419 | tst->os_state.remote_port = 0; |
| 420 | tst->os_state.msgh_id = 0; |
| 421 | VG_(memset)(&tst->os_state.mach_args, 0, sizeof(tst->os_state.mach_args)); |
| 422 | # else |
| 423 | # error "Unknown OS" |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 424 | # endif |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void os_state_init(ThreadState *tst) |
| 428 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 429 | tst->os_state.valgrind_stack_base = 0; |
| 430 | tst->os_state.valgrind_stack_init_SP = 0; |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 431 | os_state_clear(tst); |
| 432 | } |
| 433 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 434 | static |
| 435 | void mostly_clear_thread_record ( ThreadId tid ) |
| 436 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 437 | vki_sigset_t savedmask; |
| 438 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 439 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 440 | VG_(cleanup_thread)(&VG_(threads)[tid].arch); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 441 | VG_(threads)[tid].tid = tid; |
| 442 | |
| 443 | /* Leave the thread in Zombie, so that it doesn't get reallocated |
| 444 | until the caller is finally done with the thread stack. */ |
| 445 | VG_(threads)[tid].status = VgTs_Zombie; |
| 446 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 447 | VG_(sigemptyset)(&VG_(threads)[tid].sig_mask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 448 | VG_(sigemptyset)(&VG_(threads)[tid].tmp_sig_mask); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 449 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 450 | os_state_clear(&VG_(threads)[tid]); |
fitzhardinge | 2842859 | 2004-03-16 22:07:12 +0000 | [diff] [blame] | 451 | |
| 452 | /* start with no altstack */ |
| 453 | VG_(threads)[tid].altstack.ss_sp = (void *)0xdeadbeef; |
| 454 | VG_(threads)[tid].altstack.ss_size = 0; |
| 455 | VG_(threads)[tid].altstack.ss_flags = VKI_SS_DISABLE; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 456 | |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 457 | VG_(clear_out_queued_signals)(tid, &savedmask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 458 | |
| 459 | VG_(threads)[tid].sched_jmpbuf_valid = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 460 | } |
| 461 | |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 462 | /* |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 463 | Called in the child after fork. If the parent has multiple |
| 464 | threads, then we've inherited a VG_(threads) array describing them, |
| 465 | but only the thread which called fork() is actually alive in the |
| 466 | child. This functions needs to clean up all those other thread |
| 467 | structures. |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 468 | |
| 469 | Whichever tid in the parent which called fork() becomes the |
| 470 | master_tid in the child. That's because the only living slot in |
| 471 | VG_(threads) in the child after fork is VG_(threads)[tid], and it |
| 472 | would be too hard to try to re-number the thread and relocate the |
| 473 | thread state down to VG_(threads)[1]. |
| 474 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 475 | This function also needs to reinitialize the_BigLock, since |
| 476 | otherwise we may end up sharing its state with the parent, which |
| 477 | would be deeply confusing. |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 478 | */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 479 | static void sched_fork_cleanup(ThreadId me) |
| 480 | { |
| 481 | ThreadId tid; |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 482 | vg_assert(VG_(running_tid) == me); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 483 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 484 | # if defined(VGO_darwin) |
| 485 | // GrP fixme hack reset Mach ports |
| 486 | VG_(mach_init)(); |
| 487 | # endif |
| 488 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 489 | VG_(threads)[me].os_state.lwpid = VG_(gettid)(); |
| 490 | VG_(threads)[me].os_state.threadgroup = VG_(getpid)(); |
| 491 | |
| 492 | /* clear out all the unused thread slots */ |
| 493 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 494 | if (tid != me) { |
| 495 | mostly_clear_thread_record(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 496 | VG_(threads)[tid].status = VgTs_Empty; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 497 | VG_(clear_syscallInfo)(tid); |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 498 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* re-init and take the sema */ |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 502 | ML_(sema_deinit)(&the_BigLock); |
| 503 | ML_(sema_init)(&the_BigLock); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 504 | ML_(sema_down)(&the_BigLock, False/*not LL*/); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 505 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 506 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 507 | |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 508 | /* First phase of initialisation of the scheduler. Initialise the |
| 509 | bigLock, zeroise the VG_(threads) structure and decide on the |
| 510 | ThreadId of the root thread. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 511 | */ |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 512 | ThreadId VG_(scheduler_init_phase1) ( void ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 513 | { |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 514 | Int i; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 515 | ThreadId tid_main; |
| 516 | |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 517 | VG_(debugLog)(1,"sched","sched_init_phase1\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 518 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 519 | ML_(sema_init)(&the_BigLock); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 520 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 521 | for (i = 0 /* NB; not 1 */; i < VG_N_THREADS; i++) { |
sewardj | c793fd3 | 2005-05-31 17:24:49 +0000 | [diff] [blame] | 522 | /* Paranoia .. completely zero it out. */ |
| 523 | VG_(memset)( & VG_(threads)[i], 0, sizeof( VG_(threads)[i] ) ); |
| 524 | |
| 525 | VG_(threads)[i].sig_queue = NULL; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 526 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 527 | os_state_init(&VG_(threads)[i]); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 528 | mostly_clear_thread_record(i); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 529 | |
njn | 50ba34e | 2005-04-04 02:41:42 +0000 | [diff] [blame] | 530 | VG_(threads)[i].status = VgTs_Empty; |
| 531 | VG_(threads)[i].client_stack_szB = 0; |
| 532 | VG_(threads)[i].client_stack_highest_word = (Addr)NULL; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 533 | } |
| 534 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 535 | tid_main = VG_(alloc_ThreadState)(); |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 536 | |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 537 | /* Bleh. Unfortunately there are various places in the system that |
| 538 | assume that the main thread has a ThreadId of 1. |
| 539 | - Helgrind (possibly) |
| 540 | - stack overflow message in default_action() in m_signals.c |
| 541 | - definitely a lot more places |
| 542 | */ |
| 543 | vg_assert(tid_main == 1); |
| 544 | |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 545 | return tid_main; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | /* Second phase of initialisation of the scheduler. Given the root |
| 550 | ThreadId computed by first phase of initialisation, fill in stack |
| 551 | details and acquire bigLock. Initialise the scheduler. This is |
| 552 | called at startup. The caller subsequently initialises the guest |
| 553 | state components of this main thread. |
| 554 | */ |
| 555 | void VG_(scheduler_init_phase2) ( ThreadId tid_main, |
| 556 | Addr clstack_end, |
| 557 | SizeT clstack_size ) |
| 558 | { |
| 559 | VG_(debugLog)(1,"sched","sched_init_phase2: tid_main=%d, " |
| 560 | "cls_end=0x%lx, cls_sz=%ld\n", |
| 561 | tid_main, clstack_end, clstack_size); |
| 562 | |
| 563 | vg_assert(VG_IS_PAGE_ALIGNED(clstack_end+1)); |
| 564 | vg_assert(VG_IS_PAGE_ALIGNED(clstack_size)); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 565 | |
njn | 50ba34e | 2005-04-04 02:41:42 +0000 | [diff] [blame] | 566 | VG_(threads)[tid_main].client_stack_highest_word |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 567 | = clstack_end + 1 - sizeof(UWord); |
| 568 | VG_(threads)[tid_main].client_stack_szB |
| 569 | = clstack_size; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 570 | |
njn | e9ba34a | 2008-10-13 04:19:15 +0000 | [diff] [blame] | 571 | VG_(atfork)(NULL, NULL, sched_fork_cleanup); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 575 | /* --------------------------------------------------------------------- |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 576 | Helpers for running translations. |
| 577 | ------------------------------------------------------------------ */ |
| 578 | |
| 579 | /* Use gcc's built-in setjmp/longjmp. longjmp must not restore signal |
| 580 | mask state, but does need to pass "val" through. */ |
| 581 | #define SCHEDSETJMP(tid, jumped, stmt) \ |
| 582 | do { \ |
| 583 | ThreadState * volatile _qq_tst = VG_(get_ThreadState)(tid); \ |
| 584 | \ |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 585 | (jumped) = VG_MINIMAL_SETJMP(_qq_tst->sched_jmpbuf); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 586 | if ((jumped) == 0) { \ |
| 587 | vg_assert(!_qq_tst->sched_jmpbuf_valid); \ |
| 588 | _qq_tst->sched_jmpbuf_valid = True; \ |
| 589 | stmt; \ |
| 590 | } else if (VG_(clo_trace_sched)) \ |
| 591 | VG_(printf)("SCHEDSETJMP(line %d) tid %d, jumped=%d\n", \ |
| 592 | __LINE__, tid, jumped); \ |
| 593 | vg_assert(_qq_tst->sched_jmpbuf_valid); \ |
| 594 | _qq_tst->sched_jmpbuf_valid = False; \ |
| 595 | } while(0) |
| 596 | |
| 597 | |
| 598 | /* Do various guest state alignment checks prior to running a thread. |
| 599 | Specifically, check that what we have matches Vex's guest state |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 600 | layout requirements. See libvex.h for details, but in short the |
| 601 | requirements are: There must be no holes in between the primary |
| 602 | guest state, its two copies, and the spill area. In short, all 4 |
| 603 | areas must have a 16-aligned size and be 16-aligned, and placed |
| 604 | back-to-back. */ |
| 605 | static void do_pre_run_checks ( ThreadState* tst ) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 606 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 607 | Addr a_vex = (Addr) & tst->arch.vex; |
| 608 | Addr a_vexsh1 = (Addr) & tst->arch.vex_shadow1; |
| 609 | Addr a_vexsh2 = (Addr) & tst->arch.vex_shadow2; |
| 610 | Addr a_spill = (Addr) & tst->arch.vex_spill; |
| 611 | UInt sz_vex = (UInt) sizeof tst->arch.vex; |
| 612 | UInt sz_vexsh1 = (UInt) sizeof tst->arch.vex_shadow1; |
| 613 | UInt sz_vexsh2 = (UInt) sizeof tst->arch.vex_shadow2; |
| 614 | UInt sz_spill = (UInt) sizeof tst->arch.vex_spill; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 615 | |
| 616 | if (0) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 617 | VG_(printf)("gst %p %d, sh1 %p %d, " |
| 618 | "sh2 %p %d, spill %p %d\n", |
| 619 | (void*)a_vex, sz_vex, |
| 620 | (void*)a_vexsh1, sz_vexsh1, |
| 621 | (void*)a_vexsh2, sz_vexsh2, |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 622 | (void*)a_spill, sz_spill ); |
| 623 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 624 | vg_assert(VG_IS_16_ALIGNED(sz_vex)); |
| 625 | vg_assert(VG_IS_16_ALIGNED(sz_vexsh1)); |
| 626 | vg_assert(VG_IS_16_ALIGNED(sz_vexsh2)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 627 | vg_assert(VG_IS_16_ALIGNED(sz_spill)); |
| 628 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 629 | vg_assert(VG_IS_16_ALIGNED(a_vex)); |
| 630 | vg_assert(VG_IS_16_ALIGNED(a_vexsh1)); |
| 631 | vg_assert(VG_IS_16_ALIGNED(a_vexsh2)); |
| 632 | vg_assert(VG_IS_16_ALIGNED(a_spill)); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 633 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 634 | /* Check that the guest state and its two shadows have the same |
| 635 | size, and that there are no holes in between. The latter is |
| 636 | important because Memcheck assumes that it can reliably access |
| 637 | the shadows by indexing off a pointer to the start of the |
| 638 | primary guest state area. */ |
| 639 | vg_assert(sz_vex == sz_vexsh1); |
| 640 | vg_assert(sz_vex == sz_vexsh2); |
| 641 | vg_assert(a_vex + 1 * sz_vex == a_vexsh1); |
| 642 | vg_assert(a_vex + 2 * sz_vex == a_vexsh2); |
| 643 | /* Also check there's no hole between the second shadow area and |
| 644 | the spill area. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 645 | vg_assert(sz_spill == LibVEX_N_SPILL_BYTES); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 646 | vg_assert(a_vex + 3 * sz_vex == a_spill); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 647 | |
sewardj | 565dc13 | 2010-08-06 08:01:47 +0000 | [diff] [blame] | 648 | # if defined(VGA_amd64) |
| 649 | /* x86/amd64 XMM regs must form an array, ie, have no |
| 650 | holes in between. */ |
| 651 | vg_assert( |
| 652 | (offsetof(VexGuestAMD64State,guest_XMM16) |
| 653 | - offsetof(VexGuestAMD64State,guest_XMM0)) |
| 654 | == (17/*#regs*/-1) * 16/*bytes per reg*/ |
| 655 | ); |
| 656 | # endif |
| 657 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 658 | # if defined(VGA_ppc32) || defined(VGA_ppc64) |
| 659 | /* ppc guest_state vector regs must be 16 byte aligned for |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 660 | loads/stores. This is important! */ |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame^] | 661 | vg_assert(VG_IS_16_ALIGNED(& tst->arch.vex.guest_VSR0)); |
| 662 | vg_assert(VG_IS_16_ALIGNED(& tst->arch.vex_shadow1.guest_VSR0)); |
| 663 | vg_assert(VG_IS_16_ALIGNED(& tst->arch.vex_shadow2.guest_VSR0)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 664 | /* be extra paranoid .. */ |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame^] | 665 | vg_assert(VG_IS_16_ALIGNED(& tst->arch.vex.guest_VSR1)); |
| 666 | vg_assert(VG_IS_16_ALIGNED(& tst->arch.vex_shadow1.guest_VSR1)); |
| 667 | vg_assert(VG_IS_16_ALIGNED(& tst->arch.vex_shadow2.guest_VSR1)); |
sewardj | 565dc13 | 2010-08-06 08:01:47 +0000 | [diff] [blame] | 668 | # endif |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 669 | |
| 670 | # if defined(VGA_arm) |
| 671 | /* arm guest_state VFP regs must be 8 byte aligned for |
| 672 | loads/stores. */ |
| 673 | vg_assert(VG_IS_8_ALIGNED(& tst->arch.vex.guest_D0)); |
| 674 | vg_assert(VG_IS_8_ALIGNED(& tst->arch.vex_shadow1.guest_D0)); |
| 675 | vg_assert(VG_IS_8_ALIGNED(& tst->arch.vex_shadow2.guest_D0)); |
| 676 | /* be extra paranoid .. */ |
| 677 | vg_assert(VG_IS_8_ALIGNED(& tst->arch.vex.guest_D1)); |
| 678 | vg_assert(VG_IS_8_ALIGNED(& tst->arch.vex_shadow1.guest_D1)); |
| 679 | vg_assert(VG_IS_8_ALIGNED(& tst->arch.vex_shadow2.guest_D1)); |
| 680 | # endif |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 681 | |
| 682 | # if defined(VGA_s390x) |
| 683 | /* no special requirements */ |
| 684 | # endif |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
| 688 | /* Run the thread tid for a while, and return a VG_TRC_* value |
| 689 | indicating why VG_(run_innerloop) stopped. */ |
| 690 | static UInt run_thread_for_a_while ( ThreadId tid ) |
| 691 | { |
| 692 | volatile Int jumped; |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 693 | volatile ThreadState* tst = NULL; /* stop gcc complaining */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 694 | volatile UInt trc; |
| 695 | volatile Int dispatch_ctr_SAVED; |
| 696 | volatile Int done_this_time; |
| 697 | |
| 698 | /* Paranoia */ |
| 699 | vg_assert(VG_(is_valid_tid)(tid)); |
| 700 | vg_assert(VG_(is_running_thread)(tid)); |
| 701 | vg_assert(!VG_(is_exiting)(tid)); |
| 702 | |
| 703 | tst = VG_(get_ThreadState)(tid); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 704 | do_pre_run_checks( (ThreadState*)tst ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 705 | /* end Paranoia */ |
| 706 | |
| 707 | trc = 0; |
| 708 | dispatch_ctr_SAVED = VG_(dispatch_ctr); |
| 709 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 710 | # if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 711 | /* On AIX, we need to get a plausible value for SPRG3 for this |
| 712 | thread, since it's used I think as a thread-state pointer. It |
| 713 | is presumably set by the kernel for each dispatched thread and |
| 714 | cannot be changed by user space. It therefore seems safe enough |
| 715 | to copy the host's value of it into the guest state at the point |
| 716 | the thread is dispatched. |
| 717 | (Later): Hmm, looks like SPRG3 is only used in 32-bit mode. |
| 718 | Oh well. */ |
| 719 | { UWord host_sprg3; |
| 720 | __asm__ __volatile__( "mfspr %0,259\n" : "=b"(host_sprg3) ); |
| 721 | VG_(threads)[tid].arch.vex.guest_SPRG3_RO = host_sprg3; |
| 722 | vg_assert(sizeof(VG_(threads)[tid].arch.vex.guest_SPRG3_RO) == sizeof(void*)); |
| 723 | } |
| 724 | # endif |
| 725 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 726 | /* there should be no undealt-with signals */ |
| 727 | //vg_assert(VG_(threads)[tid].siginfo.si_signo == 0); |
| 728 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 729 | if (0) { |
| 730 | vki_sigset_t m; |
| 731 | Int i, err = VG_(sigprocmask)(VKI_SIG_SETMASK, NULL, &m); |
| 732 | vg_assert(err == 0); |
| 733 | VG_(printf)("tid %d: entering code with unblocked signals: ", tid); |
| 734 | for (i = 1; i <= _VKI_NSIG; i++) |
| 735 | if (!VG_(sigismember)(&m, i)) |
| 736 | VG_(printf)("%d ", i); |
| 737 | VG_(printf)("\n"); |
| 738 | } |
| 739 | |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 740 | // Tell the tool this thread is about to run client code |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 741 | VG_TRACK( start_client_code, tid, bbs_done ); |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 742 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 743 | vg_assert(VG_(in_generated_code) == False); |
| 744 | VG_(in_generated_code) = True; |
| 745 | |
| 746 | SCHEDSETJMP( |
| 747 | tid, |
| 748 | jumped, |
| 749 | trc = (UInt)VG_(run_innerloop)( (void*)&tst->arch.vex, |
| 750 | VG_(clo_profile_flags) > 0 ? 1 : 0 ) |
| 751 | ); |
| 752 | |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 753 | vg_assert(VG_(in_generated_code) == True); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 754 | VG_(in_generated_code) = False; |
| 755 | |
| 756 | if (jumped) { |
| 757 | /* We get here if the client took a fault that caused our signal |
| 758 | handler to longjmp. */ |
| 759 | vg_assert(trc == 0); |
| 760 | trc = VG_TRC_FAULT_SIGNAL; |
njn | 1dcee09 | 2009-02-24 03:07:37 +0000 | [diff] [blame] | 761 | block_signals(); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | done_this_time = (Int)dispatch_ctr_SAVED - (Int)VG_(dispatch_ctr) - 0; |
| 765 | |
| 766 | vg_assert(done_this_time >= 0); |
| 767 | bbs_done += (ULong)done_this_time; |
| 768 | |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 769 | // Tell the tool this thread has stopped running client code |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 770 | VG_TRACK( stop_client_code, tid, bbs_done ); |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 771 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 772 | return trc; |
| 773 | } |
| 774 | |
| 775 | |
| 776 | /* Run a no-redir translation just once, and return the resulting |
| 777 | VG_TRC_* value. */ |
| 778 | static UInt run_noredir_translation ( Addr hcode, ThreadId tid ) |
| 779 | { |
| 780 | volatile Int jumped; |
| 781 | volatile ThreadState* tst; |
| 782 | volatile UWord argblock[4]; |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 783 | volatile UInt retval; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 784 | |
| 785 | /* Paranoia */ |
| 786 | vg_assert(VG_(is_valid_tid)(tid)); |
| 787 | vg_assert(VG_(is_running_thread)(tid)); |
| 788 | vg_assert(!VG_(is_exiting)(tid)); |
| 789 | |
| 790 | tst = VG_(get_ThreadState)(tid); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 791 | do_pre_run_checks( (ThreadState*)tst ); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 792 | /* end Paranoia */ |
| 793 | |
| 794 | # if defined(VGA_ppc32) || defined(VGA_ppc64) |
| 795 | /* I don't think we need to clear this thread's guest_RESVN here, |
| 796 | because we can only get here if run_thread_for_a_while() has |
| 797 | been used immediately before, on this same thread. */ |
| 798 | # endif |
| 799 | |
sewardj | 74d6e0e | 2006-01-13 13:04:03 +0000 | [diff] [blame] | 800 | /* There can be 3 outcomes from VG_(run_a_noredir_translation): |
| 801 | |
| 802 | - a signal occurred and the sighandler longjmp'd. Then both [2] |
| 803 | and [3] are unchanged - hence zero. |
| 804 | |
| 805 | - translation ran normally, set [2] (next guest IP) and set [3] |
| 806 | to whatever [1] was beforehand, indicating a normal (boring) |
| 807 | jump to the next block. |
| 808 | |
| 809 | - translation ran normally, set [2] (next guest IP) and set [3] |
| 810 | to something different from [1] beforehand, which indicates a |
| 811 | TRC_ value. |
| 812 | */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 813 | argblock[0] = (UWord)hcode; |
| 814 | argblock[1] = (UWord)&VG_(threads)[tid].arch.vex; |
sewardj | 74d6e0e | 2006-01-13 13:04:03 +0000 | [diff] [blame] | 815 | argblock[2] = 0; /* next guest IP is written here */ |
| 816 | argblock[3] = 0; /* guest state ptr afterwards is written here */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 817 | |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 818 | // Tell the tool this thread is about to run client code |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 819 | VG_TRACK( start_client_code, tid, bbs_done ); |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 820 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 821 | vg_assert(VG_(in_generated_code) == False); |
| 822 | VG_(in_generated_code) = True; |
| 823 | |
| 824 | SCHEDSETJMP( |
| 825 | tid, |
| 826 | jumped, |
| 827 | VG_(run_a_noredir_translation)( &argblock[0] ) |
| 828 | ); |
| 829 | |
| 830 | VG_(in_generated_code) = False; |
| 831 | |
| 832 | if (jumped) { |
| 833 | /* We get here if the client took a fault that caused our signal |
| 834 | handler to longjmp. */ |
sewardj | 74d6e0e | 2006-01-13 13:04:03 +0000 | [diff] [blame] | 835 | vg_assert(argblock[2] == 0); /* next guest IP was not written */ |
| 836 | vg_assert(argblock[3] == 0); /* trc was not written */ |
njn | 1dcee09 | 2009-02-24 03:07:37 +0000 | [diff] [blame] | 837 | block_signals(); |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 838 | retval = VG_TRC_FAULT_SIGNAL; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 839 | } else { |
| 840 | /* store away the guest program counter */ |
| 841 | VG_(set_IP)( tid, argblock[2] ); |
| 842 | if (argblock[3] == argblock[1]) |
| 843 | /* the guest state pointer afterwards was unchanged */ |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 844 | retval = VG_TRC_BORING; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 845 | else |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 846 | retval = (UInt)argblock[3]; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 847 | } |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 848 | |
| 849 | bbs_done++; |
| 850 | |
| 851 | // Tell the tool this thread has stopped running client code |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 852 | VG_TRACK( stop_client_code, tid, bbs_done ); |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 853 | |
| 854 | return retval; |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 855 | } |
| 856 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 857 | |
| 858 | /* --------------------------------------------------------------------- |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 859 | The scheduler proper. |
| 860 | ------------------------------------------------------------------ */ |
| 861 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 862 | static void handle_tt_miss ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 863 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 864 | Bool found; |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 865 | Addr ip = VG_(get_IP)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 866 | |
| 867 | /* Trivial event. Miss in the fast-cache. Do a full |
| 868 | lookup for it. */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 869 | found = VG_(search_transtab)( NULL, ip, True/*upd_fast_cache*/ ); |
sewardj | 5d0d1f3 | 2010-03-14 15:09:27 +0000 | [diff] [blame] | 870 | if (UNLIKELY(!found)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 871 | /* Not found; we need to request a translation. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 872 | if (VG_(translate)( tid, ip, /*debug*/False, 0/*not verbose*/, |
| 873 | bbs_done, True/*allow redirection*/ )) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 874 | found = VG_(search_transtab)( NULL, ip, True ); |
njn | 50ae1a7 | 2005-04-08 23:28:23 +0000 | [diff] [blame] | 875 | vg_assert2(found, "VG_TRC_INNER_FASTMISS: missing tt_fast entry"); |
| 876 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 877 | } else { |
| 878 | // If VG_(translate)() fails, it's because it had to throw a |
| 879 | // signal because the client jumped to a bad address. That |
| 880 | // means that either a signal has been set up for delivery, |
| 881 | // or the thread has been marked for termination. Either |
| 882 | // way, we just need to go back into the scheduler loop. |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 887 | static void handle_syscall(ThreadId tid, UInt trc) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 888 | { |
sewardj | 1ac9d0c | 2007-05-01 14:18:48 +0000 | [diff] [blame] | 889 | ThreadState * volatile tst = VG_(get_ThreadState)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 890 | Bool jumped; |
| 891 | |
| 892 | /* Syscall may or may not block; either way, it will be |
| 893 | complete by the time this call returns, and we'll be |
| 894 | runnable again. We could take a signal while the |
| 895 | syscall runs. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 896 | |
| 897 | if (VG_(clo_sanity_level >= 3)) |
| 898 | VG_(am_do_sync_check)("(BEFORE SYSCALL)",__FILE__,__LINE__); |
| 899 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 900 | SCHEDSETJMP(tid, jumped, VG_(client_syscall)(tid, trc)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 901 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 902 | if (VG_(clo_sanity_level >= 3)) |
| 903 | VG_(am_do_sync_check)("(AFTER SYSCALL)",__FILE__,__LINE__); |
| 904 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 905 | if (!VG_(is_running_thread)(tid)) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 906 | VG_(printf)("tid %d not running; VG_(running_tid)=%d, tid %d status %d\n", |
| 907 | tid, VG_(running_tid), tid, tst->status); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 908 | vg_assert(VG_(is_running_thread)(tid)); |
| 909 | |
| 910 | if (jumped) { |
njn | 1dcee09 | 2009-02-24 03:07:37 +0000 | [diff] [blame] | 911 | block_signals(); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 912 | VG_(poll_signals)(tid); |
| 913 | } |
| 914 | } |
| 915 | |
sewardj | a591a05 | 2006-01-12 14:04:46 +0000 | [diff] [blame] | 916 | /* tid just requested a jump to the noredir version of its current |
| 917 | program counter. So make up that translation if needed, run it, |
| 918 | and return the resulting thread return code. */ |
| 919 | static UInt/*trc*/ handle_noredir_jump ( ThreadId tid ) |
| 920 | { |
| 921 | AddrH hcode = 0; |
| 922 | Addr ip = VG_(get_IP)(tid); |
| 923 | |
| 924 | Bool found = VG_(search_unredir_transtab)( &hcode, ip ); |
| 925 | if (!found) { |
| 926 | /* Not found; we need to request a translation. */ |
| 927 | if (VG_(translate)( tid, ip, /*debug*/False, 0/*not verbose*/, bbs_done, |
| 928 | False/*NO REDIRECTION*/ )) { |
| 929 | |
| 930 | found = VG_(search_unredir_transtab)( &hcode, ip ); |
| 931 | vg_assert2(found, "unredir translation missing after creation?!"); |
| 932 | |
| 933 | } else { |
| 934 | // If VG_(translate)() fails, it's because it had to throw a |
| 935 | // signal because the client jumped to a bad address. That |
| 936 | // means that either a signal has been set up for delivery, |
| 937 | // or the thread has been marked for termination. Either |
| 938 | // way, we just need to go back into the scheduler loop. |
| 939 | return VG_TRC_BORING; |
| 940 | } |
| 941 | |
| 942 | } |
| 943 | |
| 944 | vg_assert(found); |
| 945 | vg_assert(hcode != 0); |
| 946 | |
| 947 | /* Otherwise run it and return the resulting VG_TRC_* value. */ |
| 948 | return run_noredir_translation( hcode, tid ); |
| 949 | } |
| 950 | |
| 951 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 952 | /* |
| 953 | Run a thread until it wants to exit. |
| 954 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 955 | We assume that the caller has already called VG_(acquire_BigLock) for |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 956 | us, so we own the VCPU. Also, all signals are blocked. |
| 957 | */ |
| 958 | VgSchedReturnCode VG_(scheduler) ( ThreadId tid ) |
| 959 | { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 960 | UInt trc; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 961 | ThreadState *tst = VG_(get_ThreadState)(tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 962 | |
sewardj | c24be7a | 2005-03-15 01:40:12 +0000 | [diff] [blame] | 963 | if (VG_(clo_trace_sched)) |
| 964 | print_sched_event(tid, "entering VG_(scheduler)"); |
| 965 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 966 | /* set the proper running signal mask */ |
njn | 1dcee09 | 2009-02-24 03:07:37 +0000 | [diff] [blame] | 967 | block_signals(); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 968 | |
| 969 | vg_assert(VG_(is_running_thread)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 970 | |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 971 | VG_(dispatch_ctr) = SCHEDULING_QUANTUM + 1; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 972 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 973 | while (!VG_(is_exiting)(tid)) { |
| 974 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 975 | if (VG_(dispatch_ctr) == 1) { |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 976 | |
| 977 | # if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 978 | /* Note: count runnable threads before dropping The Lock. */ |
| 979 | Int rt = VG_(count_runnable_threads)(); |
| 980 | # endif |
| 981 | |
| 982 | /* Our slice is done, so yield the CPU to another thread. On |
| 983 | Linux, this doesn't sleep between sleeping and running, |
| 984 | since that would take too much time. On AIX, we have to |
| 985 | prod the scheduler to get it consider other threads; not |
| 986 | doing so appears to cause very long delays before other |
| 987 | runnable threads get rescheduled. */ |
| 988 | |
| 989 | /* 4 July 06: it seems that a zero-length nsleep is needed to |
| 990 | cause async thread cancellation (canceller.c) to terminate |
| 991 | in finite time; else it is in some kind of race/starvation |
| 992 | situation and completion is arbitrarily delayed (although |
| 993 | this is not a deadlock). |
| 994 | |
| 995 | Unfortunately these sleeps cause MPI jobs not to terminate |
| 996 | sometimes (some kind of livelock). So sleeping once |
| 997 | every N opportunities appears to work. */ |
| 998 | |
| 999 | /* 3 Aug 06: doing sys__nsleep works but crashes some apps. |
| 1000 | sys_yield also helps the problem, whilst not crashing apps. */ |
| 1001 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1002 | VG_(release_BigLock)(tid, VgTs_Yielding, |
| 1003 | "VG_(scheduler):timeslice"); |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1004 | /* ------------ now we don't have The Lock ------------ */ |
| 1005 | |
| 1006 | # if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 1007 | { static Int ctr=0; |
| 1008 | vg_assert(__NR_AIX5__nsleep != __NR_AIX5_UNKNOWN); |
| 1009 | vg_assert(__NR_AIX5_yield != __NR_AIX5_UNKNOWN); |
| 1010 | if (1 && rt > 0 && ((++ctr % 3) == 0)) { |
| 1011 | //struct vki_timespec ts; |
| 1012 | //ts.tv_sec = 0; |
| 1013 | //ts.tv_nsec = 0*1000*1000; |
| 1014 | //VG_(do_syscall2)(__NR_AIX5__nsleep, (UWord)&ts, (UWord)NULL); |
| 1015 | VG_(do_syscall0)(__NR_AIX5_yield); |
| 1016 | } |
| 1017 | } |
| 1018 | # endif |
| 1019 | |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1020 | VG_(acquire_BigLock)(tid, "VG_(scheduler):timeslice"); |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1021 | /* ------------ now we do have The Lock ------------ */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1022 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1023 | /* OK, do some relatively expensive housekeeping stuff */ |
| 1024 | scheduler_sanity(tid); |
| 1025 | VG_(sanity_check_general)(False); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1026 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1027 | /* Look for any pending signals for this thread, and set them up |
| 1028 | for delivery */ |
| 1029 | VG_(poll_signals)(tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1030 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1031 | if (VG_(is_exiting)(tid)) |
| 1032 | break; /* poll_signals picked up a fatal signal */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1033 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1034 | /* For stats purposes only. */ |
| 1035 | n_scheduling_events_MAJOR++; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1036 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1037 | /* Figure out how many bbs to ask vg_run_innerloop to do. Note |
| 1038 | that it decrements the counter before testing it for zero, so |
| 1039 | that if tst->dispatch_ctr is set to N you get at most N-1 |
| 1040 | iterations. Also this means that tst->dispatch_ctr must |
| 1041 | exceed zero before entering the innerloop. Also also, the |
| 1042 | decrement is done before the bb is actually run, so you |
| 1043 | always get at least one decrement even if nothing happens. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 1044 | VG_(dispatch_ctr) = SCHEDULING_QUANTUM + 1; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1045 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1046 | /* paranoia ... */ |
| 1047 | vg_assert(tst->tid == tid); |
| 1048 | vg_assert(tst->os_state.lwpid == VG_(gettid)()); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1051 | /* For stats purposes only. */ |
| 1052 | n_scheduling_events_MINOR++; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1053 | |
| 1054 | if (0) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1055 | VG_(message)(Vg_DebugMsg, "thread %d: running for %d bbs\n", |
| 1056 | tid, VG_(dispatch_ctr) - 1 ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1057 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1058 | trc = run_thread_for_a_while ( tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1059 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1060 | if (VG_(clo_trace_sched) && VG_(clo_verbosity) > 2) { |
| 1061 | Char buf[50]; |
| 1062 | VG_(sprintf)(buf, "TRC: %s", name_of_sched_event(trc)); |
| 1063 | print_sched_event(tid, buf); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1066 | if (trc == VEX_TRC_JMP_NOREDIR) { |
| 1067 | /* If we got a request to run a no-redir version of |
| 1068 | something, do so now -- handle_noredir_jump just (creates |
| 1069 | and) runs that one translation. The flip side is that the |
| 1070 | noredir translation can't itself return another noredir |
| 1071 | request -- that would be nonsensical. It can, however, |
| 1072 | return VG_TRC_BORING, which just means keep going as |
| 1073 | normal. */ |
| 1074 | trc = handle_noredir_jump(tid); |
| 1075 | vg_assert(trc != VEX_TRC_JMP_NOREDIR); |
| 1076 | } |
| 1077 | |
| 1078 | switch (trc) { |
| 1079 | case VG_TRC_BORING: |
| 1080 | /* no special event, just keep going. */ |
| 1081 | break; |
| 1082 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1083 | case VG_TRC_INNER_FASTMISS: |
| 1084 | vg_assert(VG_(dispatch_ctr) > 1); |
| 1085 | handle_tt_miss(tid); |
| 1086 | break; |
| 1087 | |
| 1088 | case VEX_TRC_JMP_CLIENTREQ: |
| 1089 | do_client_request(tid); |
| 1090 | break; |
sewardj | a0fef1b | 2005-11-03 13:46:30 +0000 | [diff] [blame] | 1091 | |
| 1092 | case VEX_TRC_JMP_SYS_INT128: /* x86-linux */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1093 | case VEX_TRC_JMP_SYS_INT129: /* x86-darwin */ |
| 1094 | case VEX_TRC_JMP_SYS_INT130: /* x86-darwin */ |
| 1095 | case VEX_TRC_JMP_SYS_SYSCALL: /* amd64-linux, ppc32-linux, amd64-darwin */ |
| 1096 | handle_syscall(tid, trc); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1097 | if (VG_(clo_sanity_level) > 2) |
| 1098 | VG_(sanity_check_general)(True); /* sanity-check every syscall */ |
| 1099 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1100 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1101 | case VEX_TRC_JMP_YIELD: |
| 1102 | /* Explicit yield, because this thread is in a spin-lock |
sewardj | 3fc7575 | 2005-03-12 15:16:31 +0000 | [diff] [blame] | 1103 | or something. Only let the thread run for a short while |
| 1104 | longer. Because swapping to another thread is expensive, |
| 1105 | we're prepared to let this thread eat a little more CPU |
| 1106 | before swapping to another. That means that short term |
| 1107 | spins waiting for hardware to poke memory won't cause a |
| 1108 | thread swap. */ |
sewardj | 3a74fb0 | 2006-03-16 11:31:29 +0000 | [diff] [blame] | 1109 | if (VG_(dispatch_ctr) > 2000) |
| 1110 | VG_(dispatch_ctr) = 2000; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1111 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1112 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1113 | case VG_TRC_INNER_COUNTERZERO: |
| 1114 | /* Timeslice is out. Let a new thread be scheduled. */ |
| 1115 | vg_assert(VG_(dispatch_ctr) == 1); |
| 1116 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1117 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1118 | case VG_TRC_FAULT_SIGNAL: |
| 1119 | /* Everything should be set up (either we're exiting, or |
| 1120 | about to start in a signal handler). */ |
| 1121 | break; |
sewardj | 9d1b5d3 | 2002-04-17 19:40:49 +0000 | [diff] [blame] | 1122 | |
sewardj | 07bdc5e | 2005-03-11 13:19:47 +0000 | [diff] [blame] | 1123 | case VEX_TRC_JMP_MAPFAIL: |
| 1124 | /* Failure of arch-specific address translation (x86/amd64 |
| 1125 | segment override use) */ |
| 1126 | /* jrs 2005 03 11: is this correct? */ |
| 1127 | VG_(synth_fault)(tid); |
| 1128 | break; |
| 1129 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1130 | case VEX_TRC_JMP_EMWARN: { |
| 1131 | static Int counts[EmWarn_NUMBER]; |
| 1132 | static Bool counts_initted = False; |
| 1133 | VexEmWarn ew; |
| 1134 | HChar* what; |
| 1135 | Bool show; |
| 1136 | Int q; |
| 1137 | if (!counts_initted) { |
| 1138 | counts_initted = True; |
| 1139 | for (q = 0; q < EmWarn_NUMBER; q++) |
| 1140 | counts[q] = 0; |
| 1141 | } |
| 1142 | ew = (VexEmWarn)VG_(threads)[tid].arch.vex.guest_EMWARN; |
| 1143 | what = (ew < 0 || ew >= EmWarn_NUMBER) |
| 1144 | ? "unknown (?!)" |
| 1145 | : LibVEX_EmWarn_string(ew); |
| 1146 | show = (ew < 0 || ew >= EmWarn_NUMBER) |
| 1147 | ? True |
| 1148 | : counts[ew]++ < 3; |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1149 | if (show && VG_(clo_show_emwarns) && !VG_(clo_xml)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1150 | VG_(message)( Vg_UserMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1151 | "Emulation warning: unsupported action:\n"); |
| 1152 | VG_(message)( Vg_UserMsg, " %s\n", what); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1153 | VG_(get_and_pp_StackTrace)( tid, VG_(clo_backtrace_size) ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1154 | } |
| 1155 | break; |
| 1156 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1157 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1158 | case VEX_TRC_JMP_EMFAIL: { |
| 1159 | VexEmWarn ew; |
| 1160 | HChar* what; |
| 1161 | ew = (VexEmWarn)VG_(threads)[tid].arch.vex.guest_EMWARN; |
| 1162 | what = (ew < 0 || ew >= EmWarn_NUMBER) |
| 1163 | ? "unknown (?!)" |
| 1164 | : LibVEX_EmWarn_string(ew); |
| 1165 | VG_(message)( Vg_UserMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1166 | "Emulation fatal error -- Valgrind cannot continue:\n"); |
| 1167 | VG_(message)( Vg_UserMsg, " %s\n", what); |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1168 | VG_(get_and_pp_StackTrace)( tid, VG_(clo_backtrace_size) ); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1169 | VG_(message)(Vg_UserMsg, "\n"); |
| 1170 | VG_(message)(Vg_UserMsg, "Valgrind has to exit now. Sorry.\n"); |
| 1171 | VG_(message)(Vg_UserMsg, "\n"); |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 1172 | VG_(exit)(1); |
| 1173 | break; |
| 1174 | } |
| 1175 | |
sewardj | 4f9d674 | 2007-08-29 09:11:35 +0000 | [diff] [blame] | 1176 | case VEX_TRC_JMP_SIGTRAP: |
sewardj | 86df155 | 2006-02-07 20:56:41 +0000 | [diff] [blame] | 1177 | VG_(synth_sigtrap)(tid); |
| 1178 | break; |
| 1179 | |
sewardj | 4f9d674 | 2007-08-29 09:11:35 +0000 | [diff] [blame] | 1180 | case VEX_TRC_JMP_SIGSEGV: |
| 1181 | VG_(synth_fault)(tid); |
| 1182 | break; |
| 1183 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1184 | case VEX_TRC_JMP_SIGBUS: |
| 1185 | VG_(synth_sigbus)(tid); |
| 1186 | break; |
| 1187 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1188 | case VEX_TRC_JMP_NODECODE: |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1189 | VG_(umsg)( |
| 1190 | "valgrind: Unrecognised instruction at address %#lx.\n", |
njn | 81dd3ad | 2009-05-21 02:03:08 +0000 | [diff] [blame] | 1191 | VG_(get_IP)(tid)); |
njn | aaa6dc3 | 2011-01-18 05:16:21 +0000 | [diff] [blame] | 1192 | VG_(get_and_pp_StackTrace)(tid, 50); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1193 | #define M(a) VG_(umsg)(a "\n"); |
njn | 7cf6658 | 2005-10-15 17:18:08 +0000 | [diff] [blame] | 1194 | M("Your program just tried to execute an instruction that Valgrind" ); |
| 1195 | M("did not recognise. There are two possible reasons for this." ); |
| 1196 | M("1. Your program has a bug and erroneously jumped to a non-code" ); |
| 1197 | M(" location. If you are running Memcheck and you just saw a" ); |
| 1198 | M(" warning about a bad jump, it's probably your program's fault."); |
| 1199 | M("2. The instruction is legitimate but Valgrind doesn't handle it,"); |
| 1200 | M(" i.e. it's Valgrind's fault. If you think this is the case or"); |
njn | ec4d513 | 2006-03-21 23:15:43 +0000 | [diff] [blame] | 1201 | M(" you are not sure, please let us know and we'll try to fix it."); |
njn | 7cf6658 | 2005-10-15 17:18:08 +0000 | [diff] [blame] | 1202 | M("Either way, Valgrind will now raise a SIGILL signal which will" ); |
| 1203 | M("probably kill your program." ); |
njn | d502136 | 2005-09-29 00:35:18 +0000 | [diff] [blame] | 1204 | #undef M |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1205 | VG_(synth_sigill)(tid, VG_(get_IP)(tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1206 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1207 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1208 | case VEX_TRC_JMP_TINVAL: |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1209 | VG_(discard_translations)( |
| 1210 | (Addr64)VG_(threads)[tid].arch.vex.guest_TISTART, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1211 | VG_(threads)[tid].arch.vex.guest_TILEN, |
| 1212 | "scheduler(VEX_TRC_JMP_TINVAL)" |
sewardj | 487ac70 | 2005-06-21 12:52:38 +0000 | [diff] [blame] | 1213 | ); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1214 | if (0) |
| 1215 | VG_(printf)("dump translations done.\n"); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | |
sewardj | e3a384b | 2005-07-29 08:51:34 +0000 | [diff] [blame] | 1218 | case VG_TRC_INVARIANT_FAILED: |
| 1219 | /* This typically happens if, after running generated code, |
| 1220 | it is detected that host CPU settings (eg, FPU/Vector |
| 1221 | control words) are not as they should be. Vex's code |
| 1222 | generation specifies the state such control words should |
| 1223 | be in on entry to Vex-generated code, and they should be |
| 1224 | unchanged on exit from it. Failure of this assertion |
| 1225 | usually means a bug in Vex's code generation. */ |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1226 | //{ UInt xx; |
| 1227 | // __asm__ __volatile__ ( |
| 1228 | // "\t.word 0xEEF12A10\n" // fmrx r2,fpscr |
| 1229 | // "\tmov %0, r2" : "=r"(xx) : : "r2" ); |
| 1230 | // VG_(printf)("QQQQ new fpscr = %08x\n", xx); |
| 1231 | //} |
sewardj | e3a384b | 2005-07-29 08:51:34 +0000 | [diff] [blame] | 1232 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 1233 | "run_innerloop detected host " |
| 1234 | "state invariant failure", trc); |
| 1235 | |
sewardj | a0fef1b | 2005-11-03 13:46:30 +0000 | [diff] [blame] | 1236 | case VEX_TRC_JMP_SYS_SYSENTER: |
sewardj | 5438a01 | 2005-08-07 14:49:27 +0000 | [diff] [blame] | 1237 | /* Do whatever simulation is appropriate for an x86 sysenter |
| 1238 | instruction. Note that it is critical to set this thread's |
| 1239 | guest_EIP to point at the code to execute after the |
| 1240 | sysenter, since Vex-generated code will not have set it -- |
| 1241 | vex does not know what it should be. Vex sets the next |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1242 | address to zero, so if you don't set guest_EIP, the thread |
| 1243 | will jump to zero afterwards and probably die as a result. */ |
| 1244 | # if defined(VGP_x86_linux) |
sewardj | 5438a01 | 2005-08-07 14:49:27 +0000 | [diff] [blame] | 1245 | vg_assert2(0, "VG_(scheduler), phase 3: " |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1246 | "sysenter_x86 on x86-linux is not supported"); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1247 | # elif defined(VGP_x86_darwin) |
| 1248 | /* return address in client edx */ |
| 1249 | VG_(threads)[tid].arch.vex.guest_EIP |
| 1250 | = VG_(threads)[tid].arch.vex.guest_EDX; |
| 1251 | handle_syscall(tid, trc); |
sewardj | 5438a01 | 2005-08-07 14:49:27 +0000 | [diff] [blame] | 1252 | # else |
| 1253 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 1254 | "sysenter_x86 on non-x86 platform?!?!"); |
| 1255 | # endif |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1256 | break; |
sewardj | 5438a01 | 2005-08-07 14:49:27 +0000 | [diff] [blame] | 1257 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1258 | default: |
njn | 50ae1a7 | 2005-04-08 23:28:23 +0000 | [diff] [blame] | 1259 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 1260 | "unexpected thread return code (%u)", trc); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1261 | /* NOTREACHED */ |
| 1262 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1263 | |
| 1264 | } /* switch (trc) */ |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 1265 | } |
sewardj | c24be7a | 2005-03-15 01:40:12 +0000 | [diff] [blame] | 1266 | |
| 1267 | if (VG_(clo_trace_sched)) |
| 1268 | print_sched_event(tid, "exiting VG_(scheduler)"); |
| 1269 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1270 | vg_assert(VG_(is_exiting)(tid)); |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 1271 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1272 | return tst->exitreason; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1276 | /* |
| 1277 | This causes all threads to forceably exit. They aren't actually |
| 1278 | dead by the time this returns; you need to call |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1279 | VG_(reap_threads)() to wait for them. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1280 | */ |
| 1281 | void VG_(nuke_all_threads_except) ( ThreadId me, VgSchedReturnCode src ) |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1282 | { |
| 1283 | ThreadId tid; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1284 | |
| 1285 | vg_assert(VG_(is_running_thread)(me)); |
sewardj | 45f02c4 | 2005-02-05 18:27:14 +0000 | [diff] [blame] | 1286 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1287 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 1288 | if (tid == me |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1289 | || VG_(threads)[tid].status == VgTs_Empty) |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1290 | continue; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1291 | if (0) |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1292 | VG_(printf)( |
| 1293 | "VG_(nuke_all_threads_except): nuking tid %d\n", tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1294 | |
| 1295 | VG_(threads)[tid].exitreason = src; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1296 | if (src == VgSrc_FatalSig) |
| 1297 | VG_(threads)[tid].os_state.fatalsig = VKI_SIGKILL; |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1298 | VG_(get_thread_out_of_syscall)(tid); |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1303 | /* --------------------------------------------------------------------- |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1304 | Specifying shadow register values |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1305 | ------------------------------------------------------------------ */ |
| 1306 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1307 | #if defined(VGA_x86) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1308 | # define VG_CLREQ_ARGS guest_EAX |
| 1309 | # define VG_CLREQ_RET guest_EDX |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1310 | #elif defined(VGA_amd64) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1311 | # define VG_CLREQ_ARGS guest_RAX |
| 1312 | # define VG_CLREQ_RET guest_RDX |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1313 | #elif defined(VGA_ppc32) || defined(VGA_ppc64) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1314 | # define VG_CLREQ_ARGS guest_GPR4 |
| 1315 | # define VG_CLREQ_RET guest_GPR3 |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1316 | #elif defined(VGA_arm) |
| 1317 | # define VG_CLREQ_ARGS guest_R4 |
| 1318 | # define VG_CLREQ_RET guest_R3 |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1319 | #elif defined (VGA_s390x) |
| 1320 | # define VG_CLREQ_ARGS guest_r2 |
| 1321 | # define VG_CLREQ_RET guest_r3 |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1322 | #else |
| 1323 | # error Unknown arch |
| 1324 | #endif |
| 1325 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1326 | #define CLREQ_ARGS(regs) ((regs).vex.VG_CLREQ_ARGS) |
| 1327 | #define CLREQ_RET(regs) ((regs).vex.VG_CLREQ_RET) |
| 1328 | #define O_CLREQ_RET (offsetof(VexGuestArchState, VG_CLREQ_RET)) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 1329 | |
njn | 502badb | 2005-05-08 02:04:49 +0000 | [diff] [blame] | 1330 | // These macros write a value to a client's thread register, and tell the |
| 1331 | // tool that it's happened (if necessary). |
| 1332 | |
| 1333 | #define SET_CLREQ_RETVAL(zztid, zzval) \ |
| 1334 | do { CLREQ_RET(VG_(threads)[zztid].arch) = (zzval); \ |
| 1335 | VG_TRACK( post_reg_write, \ |
| 1336 | Vg_CoreClientReq, zztid, O_CLREQ_RET, sizeof(UWord)); \ |
| 1337 | } while (0) |
| 1338 | |
| 1339 | #define SET_CLCALL_RETVAL(zztid, zzval, f) \ |
| 1340 | do { CLREQ_RET(VG_(threads)[zztid].arch) = (zzval); \ |
| 1341 | VG_TRACK( post_reg_write_clientcall_return, \ |
| 1342 | zztid, O_CLREQ_RET, sizeof(UWord), f); \ |
| 1343 | } while (0) |
| 1344 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1345 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1346 | /* --------------------------------------------------------------------- |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1347 | Handle client requests. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1348 | ------------------------------------------------------------------ */ |
| 1349 | |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 1350 | // OS-specific(?) client requests |
| 1351 | static Bool os_client_request(ThreadId tid, UWord *args) |
| 1352 | { |
| 1353 | Bool handled = True; |
| 1354 | |
| 1355 | vg_assert(VG_(is_running_thread)(tid)); |
| 1356 | |
| 1357 | switch(args[0]) { |
| 1358 | case VG_USERREQ__LIBC_FREERES_DONE: |
| 1359 | /* This is equivalent to an exit() syscall, but we don't set the |
| 1360 | exitcode (since it might already be set) */ |
| 1361 | if (0 || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1362 | VG_(message)(Vg_DebugMsg, |
| 1363 | "__libc_freeres() done; really quitting!\n"); |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1364 | VG_(threads)[tid].exitreason = VgSrc_ExitThread; |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 1365 | break; |
| 1366 | |
| 1367 | default: |
| 1368 | handled = False; |
| 1369 | break; |
| 1370 | } |
| 1371 | |
| 1372 | return handled; |
| 1373 | } |
| 1374 | |
| 1375 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1376 | /* Do a client request for the thread tid. After the request, tid may |
| 1377 | or may not still be runnable; if not, the scheduler will have to |
| 1378 | choose a new thread to run. |
| 1379 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1380 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1381 | void do_client_request ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1382 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1383 | UWord* arg = (UWord*)(CLREQ_ARGS(VG_(threads)[tid].arch)); |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1384 | UWord req_no = arg[0]; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1385 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1386 | if (0) |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1387 | VG_(printf)("req no = 0x%llx, arg = %p\n", (ULong)req_no, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1388 | switch (req_no) { |
| 1389 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1390 | case VG_USERREQ__CLIENT_CALL0: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1391 | UWord (*f)(ThreadId) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1392 | if (f == NULL) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1393 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL0: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1394 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1395 | SET_CLCALL_RETVAL(tid, f ( tid ), (Addr)f); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | } |
| 1398 | case VG_USERREQ__CLIENT_CALL1: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1399 | UWord (*f)(ThreadId, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1400 | if (f == NULL) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1401 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL1: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1402 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1403 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1404 | break; |
| 1405 | } |
| 1406 | case VG_USERREQ__CLIENT_CALL2: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1407 | UWord (*f)(ThreadId, UWord, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1408 | if (f == NULL) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1409 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL2: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1410 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1411 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2], arg[3] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | } |
| 1414 | case VG_USERREQ__CLIENT_CALL3: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1415 | UWord (*f)(ThreadId, UWord, UWord, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1416 | if (f == NULL) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1417 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL3: func=%p\n", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1418 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 1419 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2], arg[3], arg[4] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1420 | break; |
| 1421 | } |
| 1422 | |
njn | f09745a | 2005-05-10 03:01:23 +0000 | [diff] [blame] | 1423 | // Nb: this looks like a circular definition, because it kind of is. |
| 1424 | // See comment in valgrind.h to understand what's going on. |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1425 | case VG_USERREQ__RUNNING_ON_VALGRIND: |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1426 | SET_CLREQ_RETVAL(tid, RUNNING_ON_VALGRIND+1); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1427 | break; |
| 1428 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1429 | case VG_USERREQ__PRINTF: { |
sewardj | c560fb3 | 2010-01-28 15:23:54 +0000 | [diff] [blame] | 1430 | /* JRS 2010-Jan-28: this is DEPRECATED; use the |
| 1431 | _VALIST_BY_REF version instead */ |
| 1432 | if (sizeof(va_list) != sizeof(UWord)) |
| 1433 | goto va_list_casting_error_NORETURN; |
sewardj | 05b0715 | 2010-01-04 01:01:02 +0000 | [diff] [blame] | 1434 | union { |
| 1435 | va_list vargs; |
sewardj | c560fb3 | 2010-01-28 15:23:54 +0000 | [diff] [blame] | 1436 | unsigned long uw; |
| 1437 | } u; |
| 1438 | u.uw = (unsigned long)arg[2]; |
| 1439 | Int count = |
| 1440 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], u.vargs ); |
| 1441 | VG_(message_flush)(); |
| 1442 | SET_CLREQ_RETVAL( tid, count ); |
| 1443 | break; |
| 1444 | } |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1445 | |
sewardj | c560fb3 | 2010-01-28 15:23:54 +0000 | [diff] [blame] | 1446 | case VG_USERREQ__PRINTF_BACKTRACE: { |
| 1447 | /* JRS 2010-Jan-28: this is DEPRECATED; use the |
| 1448 | _VALIST_BY_REF version instead */ |
| 1449 | if (sizeof(va_list) != sizeof(UWord)) |
| 1450 | goto va_list_casting_error_NORETURN; |
sewardj | 05b0715 | 2010-01-04 01:01:02 +0000 | [diff] [blame] | 1451 | union { |
| 1452 | va_list vargs; |
sewardj | c560fb3 | 2010-01-28 15:23:54 +0000 | [diff] [blame] | 1453 | unsigned long uw; |
| 1454 | } u; |
| 1455 | u.uw = (unsigned long)arg[2]; |
| 1456 | Int count = |
| 1457 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], u.vargs ); |
| 1458 | VG_(message_flush)(); |
| 1459 | VG_(get_and_pp_StackTrace)( tid, VG_(clo_backtrace_size) ); |
| 1460 | SET_CLREQ_RETVAL( tid, count ); |
| 1461 | break; |
| 1462 | } |
| 1463 | |
| 1464 | case VG_USERREQ__PRINTF_VALIST_BY_REF: { |
| 1465 | va_list* vargsp = (va_list*)arg[2]; |
| 1466 | Int count = |
| 1467 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], *vargsp ); |
| 1468 | VG_(message_flush)(); |
| 1469 | SET_CLREQ_RETVAL( tid, count ); |
| 1470 | break; |
| 1471 | } |
| 1472 | |
| 1473 | case VG_USERREQ__PRINTF_BACKTRACE_VALIST_BY_REF: { |
| 1474 | va_list* vargsp = (va_list*)arg[2]; |
| 1475 | Int count = |
| 1476 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], *vargsp ); |
| 1477 | VG_(message_flush)(); |
| 1478 | VG_(get_and_pp_StackTrace)( tid, VG_(clo_backtrace_size) ); |
| 1479 | SET_CLREQ_RETVAL( tid, count ); |
| 1480 | break; |
| 1481 | } |
| 1482 | |
| 1483 | case VG_USERREQ__INTERNAL_PRINTF_VALIST_BY_REF: { |
| 1484 | va_list* vargsp = (va_list*)arg[2]; |
| 1485 | Int count = |
| 1486 | VG_(vmessage)( Vg_DebugMsg, (char *)arg[1], *vargsp ); |
| 1487 | VG_(message_flush)(); |
| 1488 | SET_CLREQ_RETVAL( tid, count ); |
| 1489 | break; |
| 1490 | } |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1491 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 1492 | case VG_USERREQ__ADD_IFUNC_TARGET: { |
| 1493 | VG_(redir_add_ifunc_target)( arg[1], arg[2] ); |
| 1494 | SET_CLREQ_RETVAL( tid, 0); |
| 1495 | break; } |
| 1496 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1497 | case VG_USERREQ__STACK_REGISTER: { |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1498 | UWord sid = VG_(register_stack)((Addr)arg[1], (Addr)arg[2]); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1499 | SET_CLREQ_RETVAL( tid, sid ); |
| 1500 | break; } |
| 1501 | |
| 1502 | case VG_USERREQ__STACK_DEREGISTER: { |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1503 | VG_(deregister_stack)(arg[1]); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1504 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1505 | break; } |
| 1506 | |
| 1507 | case VG_USERREQ__STACK_CHANGE: { |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1508 | VG_(change_stack)(arg[1], (Addr)arg[2], (Addr)arg[3]); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1509 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1510 | break; } |
| 1511 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1512 | case VG_USERREQ__GET_MALLOCFUNCS: { |
| 1513 | struct vg_mallocfunc_info *info = (struct vg_mallocfunc_info *)arg[1]; |
| 1514 | |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 1515 | info->tl_malloc = VG_(tdict).tool_malloc; |
| 1516 | info->tl_calloc = VG_(tdict).tool_calloc; |
| 1517 | info->tl_realloc = VG_(tdict).tool_realloc; |
| 1518 | info->tl_memalign = VG_(tdict).tool_memalign; |
| 1519 | info->tl___builtin_new = VG_(tdict).tool___builtin_new; |
| 1520 | info->tl___builtin_vec_new = VG_(tdict).tool___builtin_vec_new; |
| 1521 | info->tl_free = VG_(tdict).tool_free; |
| 1522 | info->tl___builtin_delete = VG_(tdict).tool___builtin_delete; |
| 1523 | info->tl___builtin_vec_delete = VG_(tdict).tool___builtin_vec_delete; |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 1524 | info->tl_malloc_usable_size = VG_(tdict).tool_malloc_usable_size; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1525 | |
njn | 088bfb4 | 2005-08-17 05:01:37 +0000 | [diff] [blame] | 1526 | info->mallinfo = VG_(mallinfo); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1527 | info->clo_trace_malloc = VG_(clo_trace_malloc); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1528 | |
| 1529 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1530 | |
| 1531 | break; |
| 1532 | } |
| 1533 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1534 | /* Requests from the client program */ |
| 1535 | |
| 1536 | case VG_USERREQ__DISCARD_TRANSLATIONS: |
| 1537 | if (VG_(clo_verbosity) > 2) |
| 1538 | VG_(printf)( "client request: DISCARD_TRANSLATIONS," |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 1539 | " addr %p, len %lu\n", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1540 | (void*)arg[1], arg[2] ); |
| 1541 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1542 | VG_(discard_translations)( |
| 1543 | arg[1], arg[2], "scheduler(VG_USERREQ__DISCARD_TRANSLATIONS)" |
| 1544 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1545 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1546 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1549 | case VG_USERREQ__COUNT_ERRORS: |
nethercote | f2b1148 | 2004-08-02 12:36:01 +0000 | [diff] [blame] | 1550 | SET_CLREQ_RETVAL( tid, VG_(get_n_errs_found)() ); |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1551 | break; |
| 1552 | |
sewardj | c8259b8 | 2009-04-22 22:42:10 +0000 | [diff] [blame] | 1553 | case VG_USERREQ__LOAD_PDB_DEBUGINFO: |
| 1554 | VG_(di_notify_pdb_debuginfo)( arg[1], arg[2], arg[3], arg[4] ); |
| 1555 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1556 | break; |
| 1557 | |
sewardj | 5c65962 | 2010-08-20 18:22:07 +0000 | [diff] [blame] | 1558 | case VG_USERREQ__MAP_IP_TO_SRCLOC: { |
| 1559 | Addr ip = arg[1]; |
| 1560 | UChar* buf64 = (UChar*)arg[2]; |
| 1561 | |
| 1562 | VG_(memset)(buf64, 0, 64); |
| 1563 | UInt linenum = 0; |
| 1564 | Bool ok = VG_(get_filename_linenum)( |
| 1565 | ip, &buf64[0], 50, NULL, 0, NULL, &linenum |
| 1566 | ); |
| 1567 | if (ok) { |
| 1568 | /* Find the terminating zero in the first 50 bytes. */ |
| 1569 | UInt i; |
| 1570 | for (i = 0; i < 50; i++) { |
| 1571 | if (buf64[i] == 0) |
| 1572 | break; |
| 1573 | } |
| 1574 | /* We must find a zero somewhere in 0 .. 49. Else |
| 1575 | VG_(get_filename_linenum) is not properly zero |
| 1576 | terminating. */ |
| 1577 | vg_assert(i < 50); |
| 1578 | VG_(sprintf)(&buf64[i], ":%u", linenum); |
| 1579 | } else { |
| 1580 | buf64[0] = 0; |
| 1581 | } |
| 1582 | |
| 1583 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1584 | break; |
| 1585 | } |
| 1586 | |
njn | 32f8d8c | 2009-07-15 02:31:45 +0000 | [diff] [blame] | 1587 | case VG_USERREQ__MALLOCLIKE_BLOCK: |
bart | 9134738 | 2011-03-25 20:07:25 +0000 | [diff] [blame] | 1588 | case VG_USERREQ__RESIZEINPLACE_BLOCK: |
njn | 32f8d8c | 2009-07-15 02:31:45 +0000 | [diff] [blame] | 1589 | case VG_USERREQ__FREELIKE_BLOCK: |
| 1590 | // Ignore them if the addr is NULL; otherwise pass onto the tool. |
| 1591 | if (!arg[1]) { |
| 1592 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1593 | break; |
| 1594 | } else { |
| 1595 | goto my_default; |
| 1596 | } |
| 1597 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1598 | default: |
njn | 32f8d8c | 2009-07-15 02:31:45 +0000 | [diff] [blame] | 1599 | my_default: |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 1600 | if (os_client_request(tid, arg)) { |
| 1601 | // do nothing, os_client_request() handled it |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1602 | } else if (VG_(needs).client_requests) { |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1603 | UWord ret; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1604 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1605 | if (VG_(clo_verbosity) > 2) |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 1606 | VG_(printf)("client request: code %lx, addr %p, len %lu\n", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1607 | arg[0], (void*)arg[1], arg[2] ); |
| 1608 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1609 | if ( VG_TDICT_CALL(tool_handle_client_request, tid, arg, &ret) ) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1610 | SET_CLREQ_RETVAL(tid, ret); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1611 | } else { |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1612 | static Bool whined = False; |
| 1613 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1614 | if (!whined && VG_(clo_verbosity) > 2) { |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1615 | // Allow for requests in core, but defined by tools, which |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1616 | // have 0 and 0 in their two high bytes. |
| 1617 | Char c1 = (arg[0] >> 24) & 0xff; |
| 1618 | Char c2 = (arg[0] >> 16) & 0xff; |
| 1619 | if (c1 == 0) c1 = '_'; |
| 1620 | if (c2 == 0) c2 = '_'; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1621 | VG_(message)(Vg_UserMsg, "Warning:\n" |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 1622 | " unhandled client request: 0x%lx (%c%c+0x%lx). Perhaps\n" |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1623 | " VG_(needs).client_requests should be set?\n", |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1624 | arg[0], c1, c2, arg[0] & 0xffff); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1625 | whined = True; |
| 1626 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1627 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
sewardj | c560fb3 | 2010-01-28 15:23:54 +0000 | [diff] [blame] | 1630 | return; |
| 1631 | |
| 1632 | /*NOTREACHED*/ |
| 1633 | va_list_casting_error_NORETURN: |
| 1634 | VG_(umsg)( |
| 1635 | "Valgrind: fatal error - cannot continue: use of the deprecated\n" |
| 1636 | "client requests VG_USERREQ__PRINTF or VG_USERREQ__PRINTF_BACKTRACE\n" |
| 1637 | "on a platform where they cannot be supported. Please use the\n" |
| 1638 | "equivalent _VALIST_BY_REF versions instead.\n" |
| 1639 | "\n" |
| 1640 | "This is a binary-incompatible change in Valgrind's client request\n" |
| 1641 | "mechanism. It is unfortunate, but difficult to avoid. End-users\n" |
| 1642 | "are expected to almost never see this message. The only case in\n" |
| 1643 | "which you might see this message is if your code uses the macros\n" |
| 1644 | "VALGRIND_PRINTF or VALGRIND_PRINTF_BACKTRACE. If so, you will need\n" |
| 1645 | "to recompile such code, using the header files from this version of\n" |
| 1646 | "Valgrind, and not any previous version.\n" |
| 1647 | "\n" |
| 1648 | "If you see this mesage in any other circumstances, it is probably\n" |
| 1649 | "a bug in Valgrind. In this case, please file a bug report at\n" |
| 1650 | "\n" |
| 1651 | " http://www.valgrind.org/support/bug_reports.html\n" |
| 1652 | "\n" |
| 1653 | "Will now abort.\n" |
| 1654 | ); |
| 1655 | vg_assert(0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1659 | /* --------------------------------------------------------------------- |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1660 | Sanity checking (permanently engaged) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1661 | ------------------------------------------------------------------ */ |
| 1662 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1663 | /* Internal consistency checks on the sched structures. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1664 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1665 | void scheduler_sanity ( ThreadId tid ) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1666 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1667 | Bool bad = False; |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1668 | static UInt lasttime = 0; |
| 1669 | UInt now; |
| 1670 | Int lwpid = VG_(gettid)(); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1671 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1672 | if (!VG_(is_running_thread)(tid)) { |
| 1673 | VG_(message)(Vg_DebugMsg, |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1674 | "Thread %d is supposed to be running, " |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1675 | "but doesn't own the_BigLock (owned by %d)\n", |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 1676 | tid, VG_(running_tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1677 | bad = True; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1678 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1679 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1680 | if (lwpid != VG_(threads)[tid].os_state.lwpid) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1681 | VG_(message)(Vg_DebugMsg, |
njn | d06ed47 | 2005-03-13 05:12:31 +0000 | [diff] [blame] | 1682 | "Thread %d supposed to be in LWP %d, but we're actually %d\n", |
| 1683 | tid, VG_(threads)[tid].os_state.lwpid, VG_(gettid)()); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1684 | bad = True; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1685 | } |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1686 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1687 | #if !defined(VGO_darwin) |
| 1688 | // GrP fixme |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1689 | if (lwpid != the_BigLock.owner_lwpid) { |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1690 | VG_(message)(Vg_DebugMsg, |
sewardj | ad0a3a8 | 2006-12-17 18:58:55 +0000 | [diff] [blame] | 1691 | "Thread (LWPID) %d doesn't own the_BigLock\n", |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1692 | tid); |
| 1693 | bad = True; |
| 1694 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1695 | #endif |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1696 | |
| 1697 | /* Periodically show the state of all threads, for debugging |
| 1698 | purposes. */ |
| 1699 | now = VG_(read_millisecond_timer)(); |
| 1700 | if (0 && (!bad) && (lasttime + 4000/*ms*/ <= now)) { |
| 1701 | lasttime = now; |
| 1702 | VG_(printf)("\n------------ Sched State at %d ms ------------\n", |
| 1703 | (Int)now); |
| 1704 | VG_(show_sched_status)(); |
| 1705 | } |
| 1706 | |
| 1707 | /* core_panic also shows the sched status, which is why we don't |
| 1708 | show it above if bad==True. */ |
| 1709 | if (bad) |
| 1710 | VG_(core_panic)("scheduler_sanity: failed"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1713 | void VG_(sanity_check_general) ( Bool force_expensive ) |
| 1714 | { |
| 1715 | ThreadId tid; |
| 1716 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1717 | static UInt next_slow_check_at = 1; |
| 1718 | static UInt slow_check_interval = 25; |
| 1719 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1720 | if (VG_(clo_sanity_level) < 1) return; |
| 1721 | |
| 1722 | /* --- First do all the tests that we can do quickly. ---*/ |
| 1723 | |
| 1724 | sanity_fast_count++; |
| 1725 | |
| 1726 | /* Check stuff pertaining to the memory check system. */ |
| 1727 | |
| 1728 | /* Check that nobody has spuriously claimed that the first or |
| 1729 | last 16 pages of memory have become accessible [...] */ |
| 1730 | if (VG_(needs).sanity_checks) { |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1731 | vg_assert(VG_TDICT_CALL(tool_cheap_sanity_check)); |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* --- Now some more expensive checks. ---*/ |
| 1735 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1736 | /* Once every now and again, check some more expensive stuff. |
| 1737 | Gradually increase the interval between such checks so as not to |
| 1738 | burden long-running programs too much. */ |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1739 | if ( force_expensive |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1740 | || VG_(clo_sanity_level) > 1 |
| 1741 | || (VG_(clo_sanity_level) == 1 |
| 1742 | && sanity_fast_count == next_slow_check_at)) { |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1743 | |
sewardj | f54342a | 2006-10-17 01:51:24 +0000 | [diff] [blame] | 1744 | if (0) VG_(printf)("SLOW at %d\n", sanity_fast_count-1); |
| 1745 | |
| 1746 | next_slow_check_at = sanity_fast_count - 1 + slow_check_interval; |
| 1747 | slow_check_interval++; |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1748 | sanity_slow_count++; |
| 1749 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1750 | if (VG_(needs).sanity_checks) { |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1751 | vg_assert(VG_TDICT_CALL(tool_expensive_sanity_check)); |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1754 | /* Look for stack overruns. Visit all threads. */ |
njn | d666ea7 | 2005-06-26 17:26:22 +0000 | [diff] [blame] | 1755 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1756 | SizeT remains; |
| 1757 | VgStack* stack; |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (VG_(threads)[tid].status == VgTs_Empty || |
| 1760 | VG_(threads)[tid].status == VgTs_Zombie) |
| 1761 | continue; |
| 1762 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1763 | stack |
| 1764 | = (VgStack*) |
| 1765 | VG_(get_ThreadState)(tid)->os_state.valgrind_stack_base; |
sewardj | 46dbd3f | 2010-09-08 08:30:31 +0000 | [diff] [blame] | 1766 | SizeT limit |
| 1767 | = 4096; // Let's say. Checking more causes lots of L2 misses. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1768 | remains |
sewardj | 46dbd3f | 2010-09-08 08:30:31 +0000 | [diff] [blame] | 1769 | = VG_(am_get_VgStack_unused_szB)(stack, limit); |
| 1770 | if (remains < limit) |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1771 | VG_(message)(Vg_DebugMsg, |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 1772 | "WARNING: Thread %d is within %ld bytes " |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1773 | "of running out of stack!\n", |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1774 | tid, remains); |
| 1775 | } |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | if (VG_(clo_sanity_level) > 1) { |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1779 | /* Check sanity of the low-level memory manager. Note that bugs |
| 1780 | in the client's code can cause this to fail, so we don't do |
| 1781 | this check unless specially asked for. And because it's |
| 1782 | potentially very expensive. */ |
| 1783 | VG_(sanity_check_malloc_all)(); |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1784 | } |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1785 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1786 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1787 | /*--------------------------------------------------------------------*/ |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 1788 | /*--- end ---*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1789 | /*--------------------------------------------------------------------*/ |