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