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 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 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 |
| 41 | to run at once. This is controlled with the VCPU semaphore, |
| 42 | "run_sema". Any time a thread wants to run client code or |
| 43 | manipulate any shared state (which is anything other than its own |
| 44 | ThreadState entry), it must hold the run_sema. |
| 45 | |
| 46 | When a thread is about to block in a blocking syscall, it releases |
| 47 | run_sema, and re-takes it when it becomes runnable again (either |
| 48 | because the syscall finished, or we took a signal). |
| 49 | |
| 50 | VG_(scheduler) therefore runs in each thread. It returns only when |
| 51 | the thread is exiting, either because it exited itself, or it was |
| 52 | told to exit by another thread. |
| 53 | |
| 54 | This file is almost entirely OS-independent. The details of how |
| 55 | the OS handles threading and signalling are abstracted away and |
njn | 1277109 | 2005-06-18 02:18:04 +0000 | [diff] [blame] | 56 | implemented elsewhere. [Some of the functions have worked their |
| 57 | way back for the moment, until we do an OS port in earnest...] |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
njn | 1277109 | 2005-06-18 02:18:04 +0000 | [diff] [blame] | 60 | #include "valgrind.h" // for VG_USERREQ__* |
| 61 | #include "coregrind.h" // for VG_USERREQ__* |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 62 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 63 | #include "pub_core_basics.h" |
| 64 | #include "pub_core_threadstate.h" |
njn | 899ce73 | 2005-06-21 00:28:11 +0000 | [diff] [blame] | 65 | #include "pub_core_debuginfo.h" // Needed for pub_core_aspacemgr :( |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 66 | #include "pub_core_aspacemgr.h" |
njn | 36b66df | 2005-05-12 05:13:04 +0000 | [diff] [blame] | 67 | #include "pub_core_dispatch.h" |
njn | f4c5016 | 2005-06-20 14:18:12 +0000 | [diff] [blame] | 68 | #include "pub_core_errormgr.h" // For VG_(get_n_errs_found)() |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 69 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 70 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 71 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 72 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 73 | #include "pub_core_libcsignal.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 74 | #include "pub_core_machine.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 75 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 76 | #include "pub_core_options.h" |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 77 | #include "pub_core_profile.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 78 | #include "pub_core_replacemalloc.h" |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 79 | #include "pub_core_scheduler.h" |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 80 | #include "pub_core_signals.h" |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 81 | #include "pub_core_stacks.h" |
njn | f4c5016 | 2005-06-20 14:18:12 +0000 | [diff] [blame] | 82 | #include "pub_core_stacktrace.h" // For VG_(get_and_pp_StackTrace)() |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 83 | #include "pub_core_syscall.h" |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 84 | #include "pub_core_syswrap.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 85 | #include "pub_core_tooliface.h" |
njn | f4c5016 | 2005-06-20 14:18:12 +0000 | [diff] [blame] | 86 | #include "pub_core_translate.h" // For VG_(translate)() |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 87 | #include "pub_core_transtab.h" |
njn | 3c660b6 | 2005-05-13 22:18:47 +0000 | [diff] [blame] | 88 | #include "vki_unistd.h" |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 89 | #include "priv_sema.h" |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 90 | |
| 91 | /* --------------------------------------------------------------------- |
| 92 | Types and globals for the scheduler. |
| 93 | ------------------------------------------------------------------ */ |
| 94 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 95 | /* ThreadId and ThreadState are defined elsewhere*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 96 | |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 97 | /* Defines the thread-scheduling timeslice, in terms of the number of |
| 98 | basic blocks we attempt to run each thread for. Smaller values |
| 99 | give finer interleaving but much increased scheduling overheads. */ |
| 100 | #define SCHEDULING_QUANTUM 50000 |
| 101 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 102 | /* If true, a fault is Valgrind-internal (ie, a bug) */ |
| 103 | Bool VG_(my_fault) = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 104 | |
njn | de583aa | 2005-05-11 18:57:02 +0000 | [diff] [blame] | 105 | /* Counts downwards in VG_(run_innerloop). */ |
| 106 | UInt VG_(dispatch_ctr); |
| 107 | |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 108 | /* 64-bit counter for the number of basic blocks done. */ |
| 109 | static ULong bbs_done = 0; |
| 110 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 111 | /* Forwards */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 112 | static void do_client_request ( ThreadId tid ); |
| 113 | static void scheduler_sanity ( ThreadId tid ); |
| 114 | static void mostly_clear_thread_record ( ThreadId tid ); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 115 | |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 116 | /* Stats. */ |
| 117 | static UInt n_scheduling_events_MINOR = 0; |
| 118 | static UInt n_scheduling_events_MAJOR = 0; |
| 119 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 120 | /* Sanity checking counts. */ |
| 121 | static UInt sanity_fast_count = 0; |
| 122 | static UInt sanity_slow_count = 0; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 123 | |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 124 | void VG_(print_scheduler_stats)(void) |
| 125 | { |
| 126 | VG_(message)(Vg_DebugMsg, |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 127 | "scheduler: %llu jumps (bb entries).", bbs_done ); |
| 128 | VG_(message)(Vg_DebugMsg, |
| 129 | "scheduler: %d/%d major/minor sched events.", |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 130 | n_scheduling_events_MAJOR, n_scheduling_events_MINOR); |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 131 | VG_(message)(Vg_DebugMsg, |
| 132 | " sanity: %d cheap, %d expensive checks.", |
| 133 | sanity_fast_count, sanity_slow_count ); |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 134 | } |
| 135 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 136 | /* CPU semaphore, so that threads can run exclusively */ |
| 137 | static vg_sema_t run_sema; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 138 | |
| 139 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 140 | /* --------------------------------------------------------------------- |
| 141 | Helper functions for the scheduler. |
| 142 | ------------------------------------------------------------------ */ |
| 143 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 144 | static |
| 145 | void print_sched_event ( ThreadId tid, Char* what ) |
| 146 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 147 | VG_(message)(Vg_DebugMsg, " SCHED[%d]: %s", tid, what ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 148 | } |
| 149 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 150 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 151 | HChar* name_of_sched_event ( UInt event ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 152 | { |
| 153 | switch (event) { |
sewardj | d79ef68 | 2004-11-26 13:25:17 +0000 | [diff] [blame] | 154 | case VEX_TRC_JMP_SYSCALL: return "SYSCALL"; |
| 155 | case VEX_TRC_JMP_CLIENTREQ: return "CLIENTREQ"; |
| 156 | case VEX_TRC_JMP_YIELD: return "YIELD"; |
sewardj | 45f02c4 | 2005-02-05 18:27:14 +0000 | [diff] [blame] | 157 | case VEX_TRC_JMP_NODECODE: return "NODECODE"; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 158 | case VG_TRC_INNER_COUNTERZERO: return "COUNTERZERO"; |
| 159 | case VG_TRC_INNER_FASTMISS: return "FASTMISS"; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 160 | case VG_TRC_FAULT_SIGNAL: return "FAULTSIGNAL"; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 161 | default: return "??UNKNOWN??"; |
| 162 | } |
| 163 | } |
| 164 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 165 | /* Allocate a completely empty ThreadState record. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 166 | ThreadId VG_(alloc_ThreadState) ( void ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 167 | { |
| 168 | Int i; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 169 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 170 | if (VG_(threads)[i].status == VgTs_Empty) { |
| 171 | VG_(threads)[i].status = VgTs_Init; |
| 172 | VG_(threads)[i].exitreason = VgSrc_None; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 173 | return i; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 174 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 175 | } |
| 176 | VG_(printf)("vg_alloc_ThreadState: no free slots available\n"); |
| 177 | VG_(printf)("Increase VG_N_THREADS, rebuild and try again.\n"); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 178 | VG_(core_panic)("VG_N_THREADS is too low"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 179 | /*NOTREACHED*/ |
| 180 | } |
| 181 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 182 | /* |
| 183 | Mark a thread as Runnable. This will block until the run_sema is |
| 184 | available, so that we get exclusive access to all the shared |
| 185 | structures and the CPU. Up until we get the sema, we must not |
| 186 | touch any shared state. |
| 187 | |
| 188 | When this returns, we'll actually be running. |
| 189 | */ |
| 190 | void VG_(set_running)(ThreadId tid) |
| 191 | { |
| 192 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 193 | |
| 194 | vg_assert(tst->status != VgTs_Runnable); |
| 195 | |
| 196 | tst->status = VgTs_Runnable; |
| 197 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 198 | ML_(sema_down)(&run_sema); |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 199 | if (VG_(running_tid) != VG_INVALID_THREADID) |
| 200 | VG_(printf)("tid %d found %d running\n", tid, VG_(running_tid)); |
| 201 | vg_assert(VG_(running_tid) == VG_INVALID_THREADID); |
| 202 | VG_(running_tid) = tid; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 203 | |
| 204 | if (VG_(clo_trace_sched)) |
| 205 | print_sched_event(tid, "now running"); |
tom | deca43f | 2005-07-27 23:04:28 +0000 | [diff] [blame] | 206 | |
| 207 | // While thre modeling is disable, issue thread_run events here |
| 208 | // VG_(tm_thread_switchto)(tid); |
| 209 | VG_TRACK( thread_run, tid ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 210 | } |
| 211 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 212 | /* |
| 213 | Set a thread into a sleeping state, and give up exclusive access to |
| 214 | the CPU. On return, the thread must be prepared to block until it |
| 215 | is ready to run again (generally this means blocking in a syscall, |
| 216 | but it may mean that we remain in a Runnable state and we're just |
| 217 | yielding the CPU to another thread). |
| 218 | */ |
| 219 | void VG_(set_sleeping)(ThreadId tid, ThreadStatus sleepstate) |
| 220 | { |
| 221 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 222 | |
| 223 | vg_assert(tst->status == VgTs_Runnable); |
| 224 | |
| 225 | vg_assert(sleepstate == VgTs_WaitSys || |
| 226 | sleepstate == VgTs_Yielding); |
| 227 | |
| 228 | tst->status = sleepstate; |
| 229 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 230 | vg_assert(VG_(running_tid) == tid); |
| 231 | VG_(running_tid) = VG_INVALID_THREADID; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 232 | |
| 233 | /* Release the run_sema; this will reschedule any runnable |
| 234 | thread. */ |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 235 | ML_(sema_up)(&run_sema); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 236 | |
| 237 | if (VG_(clo_trace_sched)) { |
| 238 | Char buf[50]; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 239 | VG_(sprintf)(buf, "now sleeping in state %s", |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 240 | VG_(name_of_ThreadStatus)(sleepstate)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 241 | print_sched_event(tid, buf); |
nethercote | 75d2624 | 2004-08-01 22:59:18 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 245 | /* Clear out the ThreadState and release the semaphore. Leaves the |
| 246 | ThreadState in VgTs_Zombie state, so that it doesn't get |
| 247 | reallocated until the caller is really ready. */ |
| 248 | void VG_(exit_thread)(ThreadId tid) |
| 249 | { |
| 250 | vg_assert(VG_(is_valid_tid)(tid)); |
| 251 | vg_assert(VG_(is_running_thread)(tid)); |
| 252 | vg_assert(VG_(is_exiting)(tid)); |
| 253 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 254 | mostly_clear_thread_record(tid); |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 255 | VG_(running_tid) = VG_INVALID_THREADID; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 256 | |
| 257 | /* There should still be a valid exitreason for this thread */ |
| 258 | vg_assert(VG_(threads)[tid].exitreason != VgSrc_None); |
| 259 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 260 | ML_(sema_up)(&run_sema); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* Kill a thread. This interrupts whatever a thread is doing, and |
| 264 | makes it exit ASAP. This does not set the exitreason or |
| 265 | exitcode. */ |
| 266 | void VG_(kill_thread)(ThreadId tid) |
| 267 | { |
| 268 | vg_assert(VG_(is_valid_tid)(tid)); |
| 269 | vg_assert(!VG_(is_running_thread)(tid)); |
| 270 | vg_assert(VG_(is_exiting)(tid)); |
| 271 | |
| 272 | if (VG_(threads)[tid].status == VgTs_WaitSys) { |
| 273 | if (VG_(clo_trace_signals)) |
| 274 | VG_(message)(Vg_DebugMsg, "kill_thread zaps tid %d lwp %d", |
| 275 | tid, VG_(threads)[tid].os_state.lwpid); |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 276 | VG_(tkill)(VG_(threads)[tid].os_state.lwpid, VG_SIGVGKILL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | Yield the CPU for a short time to let some other thread run. |
| 282 | */ |
| 283 | void VG_(vg_yield)(void) |
| 284 | { |
| 285 | struct vki_timespec ts = { 0, 1 }; |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 286 | ThreadId tid = VG_(running_tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 287 | |
| 288 | vg_assert(tid != VG_INVALID_THREADID); |
| 289 | vg_assert(VG_(threads)[tid].os_state.lwpid == VG_(gettid)()); |
| 290 | |
| 291 | VG_(set_sleeping)(tid, VgTs_Yielding); |
| 292 | |
| 293 | //VG_(printf)("tid %d yielding EIP=%p\n", tid, VG_(threads)[tid].arch.m_eip); |
| 294 | |
| 295 | /* |
| 296 | Tell the kernel we're yielding. |
| 297 | */ |
| 298 | if (1) |
| 299 | VG_(do_syscall0)(__NR_sched_yield); |
| 300 | else |
| 301 | VG_(nanosleep)(&ts); |
| 302 | |
| 303 | VG_(set_running)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 307 | /* Set the standard set of blocked signals, used wheneever we're not |
| 308 | running a client syscall. */ |
| 309 | static void block_signals(ThreadId tid) |
| 310 | { |
| 311 | vki_sigset_t mask; |
| 312 | |
| 313 | VG_(sigfillset)(&mask); |
| 314 | |
| 315 | /* Don't block these because they're synchronous */ |
| 316 | VG_(sigdelset)(&mask, VKI_SIGSEGV); |
| 317 | VG_(sigdelset)(&mask, VKI_SIGBUS); |
| 318 | VG_(sigdelset)(&mask, VKI_SIGFPE); |
| 319 | VG_(sigdelset)(&mask, VKI_SIGILL); |
| 320 | VG_(sigdelset)(&mask, VKI_SIGTRAP); |
| 321 | |
| 322 | /* Can't block these anyway */ |
| 323 | VG_(sigdelset)(&mask, VKI_SIGSTOP); |
| 324 | VG_(sigdelset)(&mask, VKI_SIGKILL); |
| 325 | |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 326 | VG_(sigprocmask)(VKI_SIG_SETMASK, &mask, NULL); |
| 327 | } |
| 328 | |
njn | 4f6e370 | 2005-05-16 20:50:52 +0000 | [diff] [blame] | 329 | /* Use libc setjmp/longjmp. longjmp must not restore signal mask |
| 330 | state, but does need to pass "val" through. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 331 | #define SCHEDSETJMP(tid, jumped, stmt) \ |
| 332 | do { \ |
| 333 | ThreadState * volatile _qq_tst = VG_(get_ThreadState)(tid); \ |
| 334 | \ |
njn | 4f6e370 | 2005-05-16 20:50:52 +0000 | [diff] [blame] | 335 | (jumped) = setjmp(_qq_tst->sched_jmpbuf); \ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 336 | if ((jumped) == 0) { \ |
| 337 | vg_assert(!_qq_tst->sched_jmpbuf_valid); \ |
| 338 | _qq_tst->sched_jmpbuf_valid = True; \ |
| 339 | stmt; \ |
| 340 | } else if (VG_(clo_trace_sched)) \ |
| 341 | VG_(printf)("SCHEDSETJMP(line %d) tid %d, jumped=%d\n", __LINE__, tid, jumped); \ |
| 342 | vg_assert(_qq_tst->sched_jmpbuf_valid); \ |
| 343 | _qq_tst->sched_jmpbuf_valid = False; \ |
| 344 | } while(0) |
| 345 | |
| 346 | /* Run the thread tid for a while, and return a VG_TRC_* value to the |
| 347 | scheduler indicating what happened. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 348 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 349 | UInt run_thread_for_a_while ( ThreadId tid ) |
| 350 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 351 | volatile Bool jumped; |
| 352 | volatile ThreadState *tst = VG_(get_ThreadState)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 353 | |
sewardj | 7ccc5c2 | 2002-04-24 21:39:11 +0000 | [diff] [blame] | 354 | volatile UInt trc = 0; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 355 | volatile Int dispatch_ctr_SAVED = VG_(dispatch_ctr); |
| 356 | volatile Int done_this_time; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 357 | |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 358 | /* For paranoia purposes only */ |
| 359 | volatile Addr a_vex = (Addr) & VG_(threads)[tid].arch.vex; |
| 360 | volatile Addr a_vexsh = (Addr) & VG_(threads)[tid].arch.vex_shadow; |
| 361 | volatile Addr a_spill = (Addr) & VG_(threads)[tid].arch.vex_spill; |
| 362 | volatile UInt sz_vex = (UInt) sizeof VG_(threads)[tid].arch.vex; |
| 363 | volatile UInt sz_vexsh = (UInt) sizeof VG_(threads)[tid].arch.vex_shadow; |
| 364 | volatile UInt sz_spill = (UInt) sizeof VG_(threads)[tid].arch.vex_spill; |
| 365 | |
| 366 | /* Paranoia */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 367 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 368 | vg_assert(VG_(is_valid_tid)(tid)); |
| 369 | vg_assert(VG_(is_running_thread)(tid)); |
| 370 | vg_assert(!VG_(is_exiting)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 371 | |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 372 | /* Even more paranoia. Check that what we have matches |
| 373 | Vex's guest state layout requirements. */ |
sewardj | 12a74b5 | 2004-11-26 11:57:41 +0000 | [diff] [blame] | 374 | if (0) |
| 375 | VG_(printf)("%p %d %p %d %p %d\n", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 376 | (void*)a_vex, sz_vex, (void*)a_vexsh, sz_vexsh, |
| 377 | (void*)a_spill, sz_spill ); |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 378 | |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 379 | vg_assert(VG_IS_8_ALIGNED(sz_vex)); |
| 380 | vg_assert(VG_IS_8_ALIGNED(sz_vexsh)); |
| 381 | vg_assert(VG_IS_16_ALIGNED(sz_spill)); |
sewardj | 12a74b5 | 2004-11-26 11:57:41 +0000 | [diff] [blame] | 382 | |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 383 | vg_assert(VG_IS_4_ALIGNED(a_vex)); |
| 384 | vg_assert(VG_IS_4_ALIGNED(a_vexsh)); |
| 385 | vg_assert(VG_IS_4_ALIGNED(a_spill)); |
sewardj | 873b313 | 2004-11-25 22:50:17 +0000 | [diff] [blame] | 386 | |
| 387 | vg_assert(sz_vex == sz_vexsh); |
| 388 | vg_assert(a_vex + sz_vex == a_vexsh); |
| 389 | |
| 390 | vg_assert(sz_spill == LibVEX_N_SPILL_BYTES); |
| 391 | vg_assert(a_vex + 2 * sz_vex == a_spill); |
| 392 | |
sewardj | 671ff54 | 2002-05-07 09:25:30 +0000 | [diff] [blame] | 393 | VGP_PUSHCC(VgpRun); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 394 | |
sewardj | 1053733 | 2005-08-05 00:25:11 +0000 | [diff] [blame] | 395 | # if defined(VGA_ppc32) |
| 396 | /* This is necessary due to the hacky way vex models reservations |
| 397 | on ppc. It's really quite incorrect for each thread to have its |
| 398 | own reservation flag/address, since it's really something that |
| 399 | all threads share (that's the whole point). But having shared |
| 400 | guest state is something we can't model with Vex. However, as |
| 401 | per PaulM's 2.4.0ppc, the reservation is modelled using a |
| 402 | reservation flag which is cleared at each context switch. So it |
| 403 | is indeed possible to get away with a per thread-reservation if |
| 404 | the thread's reservation is cleared before running it. |
| 405 | |
| 406 | This should be abstractified and lifted out. |
| 407 | */ |
| 408 | { Int i; |
| 409 | /* Clear any existing reservation. Be paranoid and clear them all. */ |
| 410 | for (i = 0; i < VG_N_THREADS; i++) |
| 411 | VG_(threads)[i].arch.vex.guest_RESVN = 0; |
| 412 | } |
| 413 | # endif |
| 414 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 415 | /* there should be no undealt-with signals */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 416 | //vg_assert(VG_(threads)[tid].siginfo.si_signo == 0); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 417 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 418 | //VG_(printf)("running EIP = %p ESP=%p\n", VG_(threads)[tid].arch.m_eip, VG_(threads)[tid].arch.m_esp); |
| 419 | |
| 420 | vg_assert(VG_(my_fault)); |
| 421 | VG_(my_fault) = False; |
| 422 | |
sewardj | 0312f51 | 2005-03-30 19:04:29 +0000 | [diff] [blame] | 423 | SCHEDSETJMP(tid, jumped, |
| 424 | trc = (UInt)VG_(run_innerloop)( (void*)&tst->arch.vex )); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 425 | |
| 426 | //nextEIP = tst->arch.m_eip; |
| 427 | //if (nextEIP >= VG_(client_end)) |
| 428 | // VG_(printf)("trc=%d jump to %p from %p\n", |
| 429 | // trc, nextEIP, EIP); |
| 430 | |
| 431 | VG_(my_fault) = True; |
| 432 | |
| 433 | if (jumped) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 434 | /* We get here if the client took a fault, which caused our |
| 435 | signal handler to longjmp. */ |
| 436 | vg_assert(trc == 0); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 437 | trc = VG_TRC_FAULT_SIGNAL; |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 438 | block_signals(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 439 | } |
sewardj | 5390e66 | 2005-01-10 16:51:14 +0000 | [diff] [blame] | 440 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 441 | done_this_time = (Int)dispatch_ctr_SAVED - (Int)VG_(dispatch_ctr) - 0; |
| 442 | |
| 443 | vg_assert(done_this_time >= 0); |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 444 | bbs_done += (ULong)done_this_time; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 445 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 446 | VGP_POPCC(VgpRun); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 447 | return trc; |
| 448 | } |
| 449 | |
| 450 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 451 | static void os_state_clear(ThreadState *tst) |
| 452 | { |
| 453 | tst->os_state.lwpid = 0; |
| 454 | tst->os_state.threadgroup = 0; |
| 455 | } |
| 456 | |
| 457 | static void os_state_init(ThreadState *tst) |
| 458 | { |
| 459 | tst->os_state.valgrind_stack_base = 0; |
| 460 | tst->os_state.valgrind_stack_szB = 0; |
| 461 | |
| 462 | os_state_clear(tst); |
| 463 | } |
| 464 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 465 | static |
| 466 | void mostly_clear_thread_record ( ThreadId tid ) |
| 467 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 468 | vki_sigset_t savedmask; |
| 469 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 470 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 471 | VG_(cleanup_thread)(&VG_(threads)[tid].arch); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 472 | VG_(threads)[tid].tid = tid; |
| 473 | |
| 474 | /* Leave the thread in Zombie, so that it doesn't get reallocated |
| 475 | until the caller is finally done with the thread stack. */ |
| 476 | VG_(threads)[tid].status = VgTs_Zombie; |
| 477 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 478 | VG_(sigemptyset)(&VG_(threads)[tid].sig_mask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 479 | VG_(sigemptyset)(&VG_(threads)[tid].tmp_sig_mask); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 480 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 481 | os_state_clear(&VG_(threads)[tid]); |
fitzhardinge | 2842859 | 2004-03-16 22:07:12 +0000 | [diff] [blame] | 482 | |
| 483 | /* start with no altstack */ |
| 484 | VG_(threads)[tid].altstack.ss_sp = (void *)0xdeadbeef; |
| 485 | VG_(threads)[tid].altstack.ss_size = 0; |
| 486 | VG_(threads)[tid].altstack.ss_flags = VKI_SS_DISABLE; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 487 | |
njn | 444eba1 | 2005-05-12 03:47:31 +0000 | [diff] [blame] | 488 | VG_(clear_out_queued_signals)(tid, &savedmask); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 489 | |
| 490 | VG_(threads)[tid].sched_jmpbuf_valid = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 491 | } |
| 492 | |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 493 | /* |
| 494 | Called in the child after fork. If the parent has multiple |
| 495 | threads, then we've inhereted a VG_(threads) array describing them, |
| 496 | but only the thread which called fork() is actually alive in the |
| 497 | child. This functions needs to clean up all those other thread |
| 498 | structures. |
| 499 | |
| 500 | Whichever tid in the parent which called fork() becomes the |
| 501 | master_tid in the child. That's because the only living slot in |
| 502 | VG_(threads) in the child after fork is VG_(threads)[tid], and it |
| 503 | would be too hard to try to re-number the thread and relocate the |
| 504 | thread state down to VG_(threads)[1]. |
| 505 | |
| 506 | This function also needs to reinitialize the run_sema, since |
| 507 | otherwise we may end up sharing its state with the parent, which |
| 508 | would be deeply confusing. |
| 509 | */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 510 | static void sched_fork_cleanup(ThreadId me) |
| 511 | { |
| 512 | ThreadId tid; |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 513 | vg_assert(VG_(running_tid) == me); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 514 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 515 | VG_(threads)[me].os_state.lwpid = VG_(gettid)(); |
| 516 | VG_(threads)[me].os_state.threadgroup = VG_(getpid)(); |
| 517 | |
| 518 | /* clear out all the unused thread slots */ |
| 519 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 520 | if (tid != me) { |
| 521 | mostly_clear_thread_record(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 522 | VG_(threads)[tid].status = VgTs_Empty; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 523 | VG_(clear_syscallInfo)(tid); |
njn | 3f8c437 | 2005-03-13 04:43:10 +0000 | [diff] [blame] | 524 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* re-init and take the sema */ |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 528 | ML_(sema_deinit)(&run_sema); |
| 529 | ML_(sema_init)(&run_sema); |
| 530 | ML_(sema_down)(&run_sema); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 531 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 532 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 533 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 534 | /* Initialise the scheduler. Create a single "main" thread ready to |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 535 | run, with special ThreadId of one. This is called at startup. The |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 536 | caller subsequently initialises the guest state components of this |
| 537 | main thread, thread 1. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 538 | */ |
| 539 | void VG_(scheduler_init) ( void ) |
| 540 | { |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 541 | Int i; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 542 | ThreadId tid_main; |
| 543 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 544 | ML_(sema_init)(&run_sema); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 545 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 546 | for (i = 0 /* NB; not 1 */; i < VG_N_THREADS; i++) { |
sewardj | c793fd3 | 2005-05-31 17:24:49 +0000 | [diff] [blame] | 547 | |
| 548 | /* Paranoia .. completely zero it out. */ |
| 549 | VG_(memset)( & VG_(threads)[i], 0, sizeof( VG_(threads)[i] ) ); |
| 550 | |
| 551 | VG_(threads)[i].sig_queue = NULL; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 552 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 553 | os_state_init(&VG_(threads)[i]); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 554 | mostly_clear_thread_record(i); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 555 | |
njn | 50ba34e | 2005-04-04 02:41:42 +0000 | [diff] [blame] | 556 | VG_(threads)[i].status = VgTs_Empty; |
| 557 | VG_(threads)[i].client_stack_szB = 0; |
| 558 | VG_(threads)[i].client_stack_highest_word = (Addr)NULL; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 559 | } |
| 560 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 561 | tid_main = VG_(alloc_ThreadState)(); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 562 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 563 | /* Initial thread's stack is the original process stack */ |
njn | 50ba34e | 2005-04-04 02:41:42 +0000 | [diff] [blame] | 564 | VG_(threads)[tid_main].client_stack_highest_word |
njn | 90eecea | 2005-04-04 02:48:32 +0000 | [diff] [blame] | 565 | = VG_(clstk_end) - sizeof(UWord); |
njn | 50ba34e | 2005-04-04 02:41:42 +0000 | [diff] [blame] | 566 | VG_(threads)[tid_main].client_stack_szB = VG_(client_rlimit_stack).rlim_cur; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 567 | |
njn | 310ed28 | 2005-06-26 15:11:37 +0000 | [diff] [blame] | 568 | VG_(atfork_child)(sched_fork_cleanup); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 572 | /* --------------------------------------------------------------------- |
| 573 | The scheduler proper. |
| 574 | ------------------------------------------------------------------ */ |
| 575 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 576 | static void handle_tt_miss ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 577 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 578 | Bool found; |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 579 | Addr ip = VG_(get_IP)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 580 | |
| 581 | /* Trivial event. Miss in the fast-cache. Do a full |
| 582 | lookup for it. */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 583 | found = VG_(search_transtab)( NULL, ip, True/*upd_fast_cache*/ ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 584 | if (!found) { |
| 585 | /* Not found; we need to request a translation. */ |
njn | 394213a | 2005-06-19 18:38:24 +0000 | [diff] [blame] | 586 | if (VG_(translate)( tid, ip, /*debug*/False, 0/*not verbose*/, bbs_done )) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 587 | found = VG_(search_transtab)( NULL, ip, True ); |
njn | 50ae1a7 | 2005-04-08 23:28:23 +0000 | [diff] [blame] | 588 | vg_assert2(found, "VG_TRC_INNER_FASTMISS: missing tt_fast entry"); |
| 589 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 590 | } else { |
| 591 | // If VG_(translate)() fails, it's because it had to throw a |
| 592 | // signal because the client jumped to a bad address. That |
| 593 | // means that either a signal has been set up for delivery, |
| 594 | // or the thread has been marked for termination. Either |
| 595 | // way, we just need to go back into the scheduler loop. |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | static void handle_syscall(ThreadId tid) |
| 601 | { |
| 602 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 603 | Bool jumped; |
| 604 | |
| 605 | /* Syscall may or may not block; either way, it will be |
| 606 | complete by the time this call returns, and we'll be |
| 607 | runnable again. We could take a signal while the |
| 608 | syscall runs. */ |
| 609 | SCHEDSETJMP(tid, jumped, VG_(client_syscall)(tid)); |
| 610 | |
| 611 | if (!VG_(is_running_thread)(tid)) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 612 | VG_(printf)("tid %d not running; VG_(running_tid)=%d, tid %d status %d\n", |
| 613 | tid, VG_(running_tid), tid, tst->status); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 614 | vg_assert(VG_(is_running_thread)(tid)); |
| 615 | |
| 616 | if (jumped) { |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 617 | block_signals(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 618 | VG_(poll_signals)(tid); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | Run a thread until it wants to exit. |
| 624 | |
| 625 | We assume that the caller has already called VG_(set_running) for |
| 626 | us, so we own the VCPU. Also, all signals are blocked. |
| 627 | */ |
| 628 | VgSchedReturnCode VG_(scheduler) ( ThreadId tid ) |
| 629 | { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 630 | UInt trc; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 631 | ThreadState *tst = VG_(get_ThreadState)(tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 632 | |
sewardj | c24be7a | 2005-03-15 01:40:12 +0000 | [diff] [blame] | 633 | if (VG_(clo_trace_sched)) |
| 634 | print_sched_event(tid, "entering VG_(scheduler)"); |
| 635 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 636 | VGP_PUSHCC(VgpSched); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 637 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 638 | /* set the proper running signal mask */ |
njn | 9fc3112 | 2005-05-11 18:48:33 +0000 | [diff] [blame] | 639 | block_signals(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 640 | |
| 641 | vg_assert(VG_(is_running_thread)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 642 | |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 643 | VG_(dispatch_ctr) = SCHEDULING_QUANTUM + 1; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 644 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 645 | while(!VG_(is_exiting)(tid)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 646 | if (VG_(dispatch_ctr) == 1) { |
| 647 | /* Our slice is done, so yield the CPU to another thread. This |
| 648 | doesn't sleep between sleeping and running, since that would |
| 649 | take too much time. */ |
| 650 | VG_(set_sleeping)(tid, VgTs_Yielding); |
| 651 | /* nothing */ |
| 652 | VG_(set_running)(tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 653 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 654 | /* OK, do some relatively expensive housekeeping stuff */ |
| 655 | scheduler_sanity(tid); |
| 656 | VG_(sanity_check_general)(False); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 657 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 658 | /* Look for any pending signals for this thread, and set them up |
| 659 | for delivery */ |
| 660 | VG_(poll_signals)(tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 661 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 662 | if (VG_(is_exiting)(tid)) |
| 663 | break; /* poll_signals picked up a fatal signal */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 664 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 665 | /* For stats purposes only. */ |
| 666 | n_scheduling_events_MAJOR++; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 667 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 668 | /* Figure out how many bbs to ask vg_run_innerloop to do. Note |
| 669 | that it decrements the counter before testing it for zero, so |
| 670 | that if tst->dispatch_ctr is set to N you get at most N-1 |
| 671 | iterations. Also this means that tst->dispatch_ctr must |
| 672 | exceed zero before entering the innerloop. Also also, the |
| 673 | decrement is done before the bb is actually run, so you |
| 674 | always get at least one decrement even if nothing happens. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 675 | VG_(dispatch_ctr) = SCHEDULING_QUANTUM + 1; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 676 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 677 | /* paranoia ... */ |
| 678 | vg_assert(tst->tid == tid); |
| 679 | vg_assert(tst->os_state.lwpid == VG_(gettid)()); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 680 | } |
| 681 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 682 | /* For stats purposes only. */ |
| 683 | n_scheduling_events_MINOR++; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 684 | |
| 685 | if (0) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 686 | VG_(message)(Vg_DebugMsg, "thread %d: running for %d bbs", |
| 687 | tid, VG_(dispatch_ctr) - 1 ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 688 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 689 | trc = run_thread_for_a_while ( tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 690 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 691 | if (VG_(clo_trace_sched) && VG_(clo_verbosity) > 2) { |
| 692 | Char buf[50]; |
| 693 | VG_(sprintf)(buf, "TRC: %s", name_of_sched_event(trc)); |
| 694 | print_sched_event(tid, buf); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 695 | } |
| 696 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 697 | switch(trc) { |
| 698 | case VG_TRC_INNER_FASTMISS: |
| 699 | vg_assert(VG_(dispatch_ctr) > 1); |
| 700 | handle_tt_miss(tid); |
| 701 | break; |
| 702 | |
| 703 | case VEX_TRC_JMP_CLIENTREQ: |
| 704 | do_client_request(tid); |
| 705 | break; |
| 706 | |
| 707 | case VEX_TRC_JMP_SYSCALL: |
| 708 | handle_syscall(tid); |
| 709 | if (VG_(clo_sanity_level) > 2) |
| 710 | VG_(sanity_check_general)(True); /* sanity-check every syscall */ |
| 711 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 712 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 713 | case VEX_TRC_JMP_YIELD: |
| 714 | /* Explicit yield, because this thread is in a spin-lock |
sewardj | 3fc7575 | 2005-03-12 15:16:31 +0000 | [diff] [blame] | 715 | or something. Only let the thread run for a short while |
| 716 | longer. Because swapping to another thread is expensive, |
| 717 | we're prepared to let this thread eat a little more CPU |
| 718 | before swapping to another. That means that short term |
| 719 | spins waiting for hardware to poke memory won't cause a |
| 720 | thread swap. */ |
| 721 | if (VG_(dispatch_ctr) > 100) |
| 722 | VG_(dispatch_ctr) = 100; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 723 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 724 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 725 | case VG_TRC_INNER_COUNTERZERO: |
| 726 | /* Timeslice is out. Let a new thread be scheduled. */ |
| 727 | vg_assert(VG_(dispatch_ctr) == 1); |
| 728 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 729 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 730 | case VG_TRC_FAULT_SIGNAL: |
| 731 | /* Everything should be set up (either we're exiting, or |
| 732 | about to start in a signal handler). */ |
| 733 | break; |
sewardj | 9d1b5d3 | 2002-04-17 19:40:49 +0000 | [diff] [blame] | 734 | |
sewardj | 07bdc5e | 2005-03-11 13:19:47 +0000 | [diff] [blame] | 735 | case VEX_TRC_JMP_MAPFAIL: |
| 736 | /* Failure of arch-specific address translation (x86/amd64 |
| 737 | segment override use) */ |
| 738 | /* jrs 2005 03 11: is this correct? */ |
| 739 | VG_(synth_fault)(tid); |
| 740 | break; |
| 741 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 742 | case VEX_TRC_JMP_EMWARN: { |
| 743 | static Int counts[EmWarn_NUMBER]; |
| 744 | static Bool counts_initted = False; |
| 745 | VexEmWarn ew; |
| 746 | HChar* what; |
| 747 | Bool show; |
| 748 | Int q; |
| 749 | if (!counts_initted) { |
| 750 | counts_initted = True; |
| 751 | for (q = 0; q < EmWarn_NUMBER; q++) |
| 752 | counts[q] = 0; |
| 753 | } |
| 754 | ew = (VexEmWarn)VG_(threads)[tid].arch.vex.guest_EMWARN; |
| 755 | what = (ew < 0 || ew >= EmWarn_NUMBER) |
| 756 | ? "unknown (?!)" |
| 757 | : LibVEX_EmWarn_string(ew); |
| 758 | show = (ew < 0 || ew >= EmWarn_NUMBER) |
| 759 | ? True |
| 760 | : counts[ew]++ < 3; |
sewardj | b1131a8 | 2005-03-19 15:12:21 +0000 | [diff] [blame] | 761 | if (show && VG_(clo_show_emwarns)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 762 | VG_(message)( Vg_UserMsg, |
| 763 | "Emulation warning: unsupported action:"); |
| 764 | VG_(message)( Vg_UserMsg, " %s", what); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 765 | VG_(get_and_pp_StackTrace)( tid, VG_(clo_backtrace_size) ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 766 | } |
| 767 | break; |
| 768 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 769 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 770 | case VEX_TRC_JMP_NODECODE: |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 771 | VG_(synth_sigill)(tid, VG_(get_IP)(tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 772 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 773 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 774 | case VEX_TRC_JMP_TINVAL: |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 775 | VG_(discard_translations)( |
| 776 | (Addr64)VG_(threads)[tid].arch.vex.guest_TISTART, |
sewardj | 487ac70 | 2005-06-21 12:52:38 +0000 | [diff] [blame] | 777 | VG_(threads)[tid].arch.vex.guest_TILEN |
| 778 | ); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 779 | if (0) |
| 780 | VG_(printf)("dump translations done.\n"); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 781 | break; |
| 782 | |
sewardj | e3a384b | 2005-07-29 08:51:34 +0000 | [diff] [blame] | 783 | case VG_TRC_INVARIANT_FAILED: |
| 784 | /* This typically happens if, after running generated code, |
| 785 | it is detected that host CPU settings (eg, FPU/Vector |
| 786 | control words) are not as they should be. Vex's code |
| 787 | generation specifies the state such control words should |
| 788 | be in on entry to Vex-generated code, and they should be |
| 789 | unchanged on exit from it. Failure of this assertion |
| 790 | usually means a bug in Vex's code generation. */ |
| 791 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 792 | "run_innerloop detected host " |
| 793 | "state invariant failure", trc); |
| 794 | |
sewardj | 5438a01 | 2005-08-07 14:49:27 +0000 | [diff] [blame] | 795 | case VEX_TRC_JMP_SYSENTER_X86: |
| 796 | /* Do whatever simulation is appropriate for an x86 sysenter |
| 797 | instruction. Note that it is critical to set this thread's |
| 798 | guest_EIP to point at the code to execute after the |
| 799 | sysenter, since Vex-generated code will not have set it -- |
| 800 | vex does not know what it should be. Vex sets the next |
| 801 | address to zero, so if you don't guest_EIP, the thread will |
| 802 | jump to zero afterwards and probably die as a result. */ |
| 803 | # if defined(VGA_x86) |
| 804 | //FIXME: VG_(threads)[tid].arch.vex.guest_EIP = .... |
| 805 | //handle_sysenter_x86(tid); |
| 806 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 807 | "sysenter_x86 on not yet implemented"); |
| 808 | # else |
| 809 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 810 | "sysenter_x86 on non-x86 platform?!?!"); |
| 811 | # endif |
| 812 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 813 | default: |
njn | 50ae1a7 | 2005-04-08 23:28:23 +0000 | [diff] [blame] | 814 | vg_assert2(0, "VG_(scheduler), phase 3: " |
| 815 | "unexpected thread return code (%u)", trc); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 816 | /* NOTREACHED */ |
| 817 | break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 818 | |
| 819 | } /* switch (trc) */ |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 820 | } |
sewardj | c24be7a | 2005-03-15 01:40:12 +0000 | [diff] [blame] | 821 | |
| 822 | if (VG_(clo_trace_sched)) |
| 823 | print_sched_event(tid, "exiting VG_(scheduler)"); |
| 824 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 825 | vg_assert(VG_(is_exiting)(tid)); |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 826 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 827 | VGP_POPCC(VgpSched); |
thughes | 513197c | 2004-06-13 12:07:53 +0000 | [diff] [blame] | 828 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 829 | //if (VG_(clo_model_pthreads)) |
| 830 | // VG_(tm_thread_exit)(tid); |
| 831 | |
| 832 | return tst->exitreason; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 836 | /* |
| 837 | This causes all threads to forceably exit. They aren't actually |
| 838 | dead by the time this returns; you need to call |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 839 | VG_(reap_threads)() to wait for them. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 840 | */ |
| 841 | void VG_(nuke_all_threads_except) ( ThreadId me, VgSchedReturnCode src ) |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 842 | { |
| 843 | ThreadId tid; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 844 | |
| 845 | vg_assert(VG_(is_running_thread)(me)); |
sewardj | 45f02c4 | 2005-02-05 18:27:14 +0000 | [diff] [blame] | 846 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 847 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 848 | if (tid == me |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 849 | || VG_(threads)[tid].status == VgTs_Empty) |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 850 | continue; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 851 | if (0) |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 852 | VG_(printf)( |
| 853 | "VG_(nuke_all_threads_except): nuking tid %d\n", tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 854 | |
| 855 | VG_(threads)[tid].exitreason = src; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 856 | if (src == VgSrc_FatalSig) |
| 857 | VG_(threads)[tid].os_state.fatalsig = VKI_SIGKILL; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 858 | VG_(kill_thread)(tid); |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 863 | /* --------------------------------------------------------------------- |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 864 | Specifying shadow register values |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 865 | ------------------------------------------------------------------ */ |
| 866 | |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 867 | #if defined(VGA_x86) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 868 | # define VG_CLREQ_ARGS guest_EAX |
| 869 | # define VG_CLREQ_RET guest_EDX |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 870 | #elif defined(VGA_amd64) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 871 | # define VG_CLREQ_ARGS guest_RAX |
| 872 | # define VG_CLREQ_RET guest_RDX |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 873 | #elif defined(VGA_ppc32) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 874 | # define VG_CLREQ_ARGS guest_GPR4 |
| 875 | # define VG_CLREQ_RET guest_GPR3 |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 876 | #else |
| 877 | # error Unknown arch |
| 878 | #endif |
| 879 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 880 | #define CLREQ_ARGS(regs) ((regs).vex.VG_CLREQ_ARGS) |
| 881 | #define CLREQ_RET(regs) ((regs).vex.VG_CLREQ_RET) |
| 882 | #define O_CLREQ_RET (offsetof(VexGuestArchState, VG_CLREQ_RET)) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 883 | |
njn | 502badb | 2005-05-08 02:04:49 +0000 | [diff] [blame] | 884 | // These macros write a value to a client's thread register, and tell the |
| 885 | // tool that it's happened (if necessary). |
| 886 | |
| 887 | #define SET_CLREQ_RETVAL(zztid, zzval) \ |
| 888 | do { CLREQ_RET(VG_(threads)[zztid].arch) = (zzval); \ |
| 889 | VG_TRACK( post_reg_write, \ |
| 890 | Vg_CoreClientReq, zztid, O_CLREQ_RET, sizeof(UWord)); \ |
| 891 | } while (0) |
| 892 | |
| 893 | #define SET_CLCALL_RETVAL(zztid, zzval, f) \ |
| 894 | do { CLREQ_RET(VG_(threads)[zztid].arch) = (zzval); \ |
| 895 | VG_TRACK( post_reg_write_clientcall_return, \ |
| 896 | zztid, O_CLREQ_RET, sizeof(UWord), f); \ |
| 897 | } while (0) |
| 898 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 899 | /* --------------------------------------------------------------------- |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 900 | Handle client requests. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 901 | ------------------------------------------------------------------ */ |
| 902 | |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 903 | // OS-specific(?) client requests |
| 904 | static Bool os_client_request(ThreadId tid, UWord *args) |
| 905 | { |
| 906 | Bool handled = True; |
| 907 | |
| 908 | vg_assert(VG_(is_running_thread)(tid)); |
| 909 | |
| 910 | switch(args[0]) { |
| 911 | case VG_USERREQ__LIBC_FREERES_DONE: |
| 912 | /* This is equivalent to an exit() syscall, but we don't set the |
| 913 | exitcode (since it might already be set) */ |
| 914 | if (0 || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) |
| 915 | VG_(message)(Vg_DebugMsg, |
| 916 | "__libc_freeres() done; really quitting!"); |
| 917 | VG_(threads)[tid].exitreason = VgSrc_ExitSyscall; |
| 918 | break; |
| 919 | |
| 920 | default: |
| 921 | handled = False; |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | return handled; |
| 926 | } |
| 927 | |
| 928 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 929 | /* Do a client request for the thread tid. After the request, tid may |
| 930 | or may not still be runnable; if not, the scheduler will have to |
| 931 | choose a new thread to run. |
| 932 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 933 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 934 | void do_client_request ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 935 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 936 | UWord* arg = (UWord*)(CLREQ_ARGS(VG_(threads)[tid].arch)); |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 937 | UWord req_no = arg[0]; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 938 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 939 | if (0) |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 940 | VG_(printf)("req no = 0x%llx, arg = %p\n", (ULong)req_no, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 941 | switch (req_no) { |
| 942 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 943 | case VG_USERREQ__CLIENT_CALL0: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 944 | UWord (*f)(ThreadId) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 945 | if (f == NULL) |
njn | 3d9edb2 | 2005-08-25 01:52:52 +0000 | [diff] [blame^] | 946 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL0: func=%p", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 947 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 948 | SET_CLCALL_RETVAL(tid, f ( tid ), (Addr)f); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 949 | break; |
| 950 | } |
| 951 | case VG_USERREQ__CLIENT_CALL1: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 952 | UWord (*f)(ThreadId, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 953 | if (f == NULL) |
njn | 3d9edb2 | 2005-08-25 01:52:52 +0000 | [diff] [blame^] | 954 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL1: func=%p", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 955 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 956 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 957 | break; |
| 958 | } |
| 959 | case VG_USERREQ__CLIENT_CALL2: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 960 | UWord (*f)(ThreadId, UWord, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 961 | if (f == NULL) |
njn | 3d9edb2 | 2005-08-25 01:52:52 +0000 | [diff] [blame^] | 962 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL2: func=%p", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 963 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 964 | SET_CLCALL_RETVAL(tid, f ( tid, arg[2], arg[3] ), (Addr)f ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | case VG_USERREQ__CLIENT_CALL3: { |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 968 | UWord (*f)(ThreadId, UWord, UWord, UWord) = (void*)arg[1]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 969 | if (f == NULL) |
njn | 3d9edb2 | 2005-08-25 01:52:52 +0000 | [diff] [blame^] | 970 | VG_(message)(Vg_DebugMsg, "VG_USERREQ__CLIENT_CALL3: func=%p", f); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 971 | else |
njn | 2ac9524 | 2005-03-13 23:07:30 +0000 | [diff] [blame] | 972 | 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] | 973 | break; |
| 974 | } |
| 975 | |
njn | f09745a | 2005-05-10 03:01:23 +0000 | [diff] [blame] | 976 | // Nb: this looks like a circular definition, because it kind of is. |
| 977 | // See comment in valgrind.h to understand what's going on. |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 978 | case VG_USERREQ__RUNNING_ON_VALGRIND: |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 979 | SET_CLREQ_RETVAL(tid, RUNNING_ON_VALGRIND+1); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 980 | break; |
| 981 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 982 | case VG_USERREQ__PRINTF: { |
| 983 | int count = |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 984 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], (void*)arg[2] ); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 985 | SET_CLREQ_RETVAL( tid, count ); |
| 986 | break; } |
| 987 | |
| 988 | case VG_USERREQ__INTERNAL_PRINTF: { |
| 989 | int count = |
njn | aa3c26b | 2005-03-12 05:32:28 +0000 | [diff] [blame] | 990 | VG_(vmessage)( Vg_DebugMsg, (char *)arg[1], (void*)arg[2] ); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 991 | SET_CLREQ_RETVAL( tid, count ); |
| 992 | break; } |
| 993 | |
| 994 | case VG_USERREQ__PRINTF_BACKTRACE: { |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 995 | int count = |
nethercote | 3e901a2 | 2004-09-11 13:17:02 +0000 | [diff] [blame] | 996 | VG_(vmessage)( Vg_ClientMsg, (char *)arg[1], (void*)arg[2] ); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 997 | VG_(get_and_pp_StackTrace)( tid, VG_(clo_backtrace_size) ); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 998 | SET_CLREQ_RETVAL( tid, count ); |
| 999 | break; } |
| 1000 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1001 | case VG_USERREQ__STACK_REGISTER: { |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1002 | UWord sid = VG_(register_stack)((Addr)arg[1], (Addr)arg[2]); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1003 | SET_CLREQ_RETVAL( tid, sid ); |
| 1004 | break; } |
| 1005 | |
| 1006 | case VG_USERREQ__STACK_DEREGISTER: { |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1007 | VG_(deregister_stack)(arg[1]); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1008 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1009 | break; } |
| 1010 | |
| 1011 | case VG_USERREQ__STACK_CHANGE: { |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1012 | VG_(change_stack)(arg[1], (Addr)arg[2], (Addr)arg[3]); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1013 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1014 | break; } |
| 1015 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1016 | case VG_USERREQ__GET_MALLOCFUNCS: { |
| 1017 | struct vg_mallocfunc_info *info = (struct vg_mallocfunc_info *)arg[1]; |
| 1018 | |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 1019 | info->tl_malloc = VG_(tdict).tool_malloc; |
| 1020 | info->tl_calloc = VG_(tdict).tool_calloc; |
| 1021 | info->tl_realloc = VG_(tdict).tool_realloc; |
| 1022 | info->tl_memalign = VG_(tdict).tool_memalign; |
| 1023 | info->tl___builtin_new = VG_(tdict).tool___builtin_new; |
| 1024 | info->tl___builtin_vec_new = VG_(tdict).tool___builtin_vec_new; |
| 1025 | info->tl_free = VG_(tdict).tool_free; |
| 1026 | info->tl___builtin_delete = VG_(tdict).tool___builtin_delete; |
| 1027 | info->tl___builtin_vec_delete = VG_(tdict).tool___builtin_vec_delete; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1028 | |
njn | cf81d55 | 2005-03-31 04:52:26 +0000 | [diff] [blame] | 1029 | info->arena_payload_szB = VG_(arena_payload_szB); |
njn | 088bfb4 | 2005-08-17 05:01:37 +0000 | [diff] [blame] | 1030 | info->mallinfo = VG_(mallinfo); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1031 | info->clo_trace_malloc = VG_(clo_trace_malloc); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1032 | |
| 1033 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
| 1034 | |
| 1035 | break; |
| 1036 | } |
| 1037 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1038 | /* Requests from the client program */ |
| 1039 | |
| 1040 | case VG_USERREQ__DISCARD_TRANSLATIONS: |
| 1041 | if (VG_(clo_verbosity) > 2) |
| 1042 | VG_(printf)( "client request: DISCARD_TRANSLATIONS," |
| 1043 | " addr %p, len %d\n", |
| 1044 | (void*)arg[1], arg[2] ); |
| 1045 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1046 | VG_(discard_translations)( arg[1], arg[2] ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1047 | |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1048 | SET_CLREQ_RETVAL( tid, 0 ); /* return value is meaningless */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1051 | case VG_USERREQ__COUNT_ERRORS: |
nethercote | f2b1148 | 2004-08-02 12:36:01 +0000 | [diff] [blame] | 1052 | SET_CLREQ_RETVAL( tid, VG_(get_n_errs_found)() ); |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1055 | default: |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 1056 | if (os_client_request(tid, arg)) { |
| 1057 | // do nothing, os_client_request() handled it |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1058 | } else if (VG_(needs).client_requests) { |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1059 | UWord ret; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1060 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1061 | if (VG_(clo_verbosity) > 2) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1062 | VG_(printf)("client request: code %x, addr %p, len %d\n", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1063 | arg[0], (void*)arg[1], arg[2] ); |
| 1064 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1065 | if ( VG_TDICT_CALL(tool_handle_client_request, tid, arg, &ret) ) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1066 | SET_CLREQ_RETVAL(tid, ret); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1067 | } else { |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1068 | static Bool whined = False; |
| 1069 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1070 | if (!whined && VG_(clo_verbosity) > 2) { |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1071 | // Allow for requests in core, but defined by tools, which |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1072 | // have 0 and 0 in their two high bytes. |
| 1073 | Char c1 = (arg[0] >> 24) & 0xff; |
| 1074 | Char c2 = (arg[0] >> 16) & 0xff; |
| 1075 | if (c1 == 0) c1 = '_'; |
| 1076 | if (c2 == 0) c2 = '_'; |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1077 | VG_(message)(Vg_UserMsg, "Warning:\n" |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1078 | " unhandled client request: 0x%x (%c%c+0x%x). Perhaps\n" |
| 1079 | " VG_(needs).client_requests should be set?\n", |
| 1080 | arg[0], c1, c2, arg[0] & 0xffff); |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 1081 | whined = True; |
| 1082 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1083 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1089 | /* --------------------------------------------------------------------- |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1090 | Sanity checking (permanently engaged) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1091 | ------------------------------------------------------------------ */ |
| 1092 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1093 | /* Internal consistency checks on the sched structures. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1094 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1095 | void scheduler_sanity ( ThreadId tid ) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1096 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1097 | Bool bad = False; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1098 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1099 | if (!VG_(is_running_thread)(tid)) { |
| 1100 | VG_(message)(Vg_DebugMsg, |
| 1101 | "Thread %d is supposed to be running, but doesn't own run_sema (owned by %d)\n", |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 1102 | tid, VG_(running_tid)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1103 | bad = True; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1104 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1105 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1106 | if (VG_(gettid)() != VG_(threads)[tid].os_state.lwpid) { |
| 1107 | VG_(message)(Vg_DebugMsg, |
njn | d06ed47 | 2005-03-13 05:12:31 +0000 | [diff] [blame] | 1108 | "Thread %d supposed to be in LWP %d, but we're actually %d\n", |
| 1109 | tid, VG_(threads)[tid].os_state.lwpid, VG_(gettid)()); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1110 | bad = True; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1111 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1114 | void VG_(sanity_check_general) ( Bool force_expensive ) |
| 1115 | { |
| 1116 | ThreadId tid; |
| 1117 | |
| 1118 | VGP_PUSHCC(VgpCoreCheapSanity); |
| 1119 | |
| 1120 | if (VG_(clo_sanity_level) < 1) return; |
| 1121 | |
| 1122 | /* --- First do all the tests that we can do quickly. ---*/ |
| 1123 | |
| 1124 | sanity_fast_count++; |
| 1125 | |
| 1126 | /* Check stuff pertaining to the memory check system. */ |
| 1127 | |
| 1128 | /* Check that nobody has spuriously claimed that the first or |
| 1129 | last 16 pages of memory have become accessible [...] */ |
| 1130 | if (VG_(needs).sanity_checks) { |
| 1131 | VGP_PUSHCC(VgpToolCheapSanity); |
| 1132 | vg_assert(VG_TDICT_CALL(tool_cheap_sanity_check)); |
| 1133 | VGP_POPCC(VgpToolCheapSanity); |
| 1134 | } |
| 1135 | |
| 1136 | /* --- Now some more expensive checks. ---*/ |
| 1137 | |
| 1138 | /* Once every 25 times, check some more expensive stuff. */ |
| 1139 | if ( force_expensive |
| 1140 | || VG_(clo_sanity_level) > 1 |
| 1141 | || (VG_(clo_sanity_level) == 1 && (sanity_fast_count % 25) == 0)) { |
| 1142 | |
| 1143 | VGP_PUSHCC(VgpCoreExpensiveSanity); |
| 1144 | sanity_slow_count++; |
| 1145 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1146 | if (VG_(needs).sanity_checks) { |
| 1147 | VGP_PUSHCC(VgpToolExpensiveSanity); |
| 1148 | vg_assert(VG_TDICT_CALL(tool_expensive_sanity_check)); |
| 1149 | VGP_POPCC(VgpToolExpensiveSanity); |
| 1150 | } |
| 1151 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1152 | /* Look for stack overruns. Visit all threads. */ |
njn | d666ea7 | 2005-06-26 17:26:22 +0000 | [diff] [blame] | 1153 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1154 | SSizeT remains; |
| 1155 | |
| 1156 | if (VG_(threads)[tid].status == VgTs_Empty || |
| 1157 | VG_(threads)[tid].status == VgTs_Zombie) |
| 1158 | continue; |
| 1159 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1160 | remains = VG_(stack_unused)(tid); |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1161 | if (remains < VKI_PAGE_SIZE) |
| 1162 | VG_(message)(Vg_DebugMsg, |
| 1163 | "WARNING: Thread %d is within %d bytes " |
| 1164 | "of running out of stack!", |
| 1165 | tid, remains); |
| 1166 | } |
| 1167 | |
njn | 6676d5b | 2005-06-19 18:49:19 +0000 | [diff] [blame] | 1168 | VGP_POPCC(VgpCoreExpensiveSanity); |
| 1169 | } |
| 1170 | |
| 1171 | if (VG_(clo_sanity_level) > 1) { |
| 1172 | VGP_PUSHCC(VgpCoreExpensiveSanity); |
| 1173 | /* Check sanity of the low-level memory manager. Note that bugs |
| 1174 | in the client's code can cause this to fail, so we don't do |
| 1175 | this check unless specially asked for. And because it's |
| 1176 | potentially very expensive. */ |
| 1177 | VG_(sanity_check_malloc_all)(); |
| 1178 | VGP_POPCC(VgpCoreExpensiveSanity); |
| 1179 | } |
| 1180 | VGP_POPCC(VgpCoreCheapSanity); |
| 1181 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1182 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1183 | /*--------------------------------------------------------------------*/ |
njn | 278b3d6 | 2005-05-30 23:20:51 +0000 | [diff] [blame] | 1184 | /*--- end ---*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1185 | /*--------------------------------------------------------------------*/ |