sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A user-space pthreads implementation. vg_scheduler.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c953984 | 2002-10-02 13:26:35 +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 | |
| 10 | Copyright (C) 2000-2002 Julian Seward |
| 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 | |
| 31 | #include "vg_include.h" |
sewardj | e12a45f | 2003-03-15 20:03:33 +0000 | [diff] [blame] | 32 | |
| 33 | /* Sidestep the normal check which disallows using valgrind.h |
| 34 | directly. */ |
| 35 | #define __VALGRIND_SOMESKIN_H |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 36 | #include "valgrind.h" /* for VG_USERREQ__RUNNING_ON_VALGRIND and |
| 37 | VG_USERREQ__DISCARD_TRANSLATIONS */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 38 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 39 | /* BORKAGE/ISSUES as of 29 May 02 |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 40 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 41 | - Currently, when a signal is run, just the ThreadStatus.status fields |
| 42 | are saved in the signal frame, along with the CPU state. Question: |
| 43 | should I also save and restore: |
| 44 | ThreadStatus.joiner |
| 45 | ThreadStatus.waited_on_mid |
| 46 | ThreadStatus.awaken_at |
| 47 | ThreadStatus.retval |
| 48 | Currently unsure, and so am not doing so. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 49 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 50 | - Signals interrupting read/write and nanosleep: SA_RESTART settings. |
| 51 | Read/write correctly return with EINTR when SA_RESTART isn't |
| 52 | specified and they are interrupted by a signal. nanosleep just |
| 53 | pretends signals don't exist -- should be fixed. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 54 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 55 | - So, what's the deal with signals and mutexes? If a thread is |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 56 | blocked on a mutex, or for a condition variable for that matter, can |
| 57 | signals still be delivered to it? This has serious consequences -- |
| 58 | deadlocks, etc. |
| 59 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 60 | - Signals still not really right. Each thread should have its |
| 61 | own pending-set, but there is just one process-wide pending set. |
| 62 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 63 | TODO for valgrind-1.0: |
| 64 | |
sewardj | 055fbb8 | 2002-05-30 00:40:55 +0000 | [diff] [blame] | 65 | - Update assertion checking in scheduler_sanity(). |
| 66 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 67 | TODO sometime: |
| 68 | |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 69 | - poll() in the vg_libpthread.c -- should it handle the nanosleep |
| 70 | being interrupted by a signal? Ditto accept? |
| 71 | |
sewardj | b60c1ad | 2002-05-29 20:23:26 +0000 | [diff] [blame] | 72 | - Mutex scrubbing - clearup_after_thread_exit: look for threads |
| 73 | blocked on mutexes held by the exiting thread, and release them |
| 74 | appropriately. (??) |
| 75 | |
| 76 | - pthread_atfork |
| 77 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 78 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | /* --------------------------------------------------------------------- |
| 82 | Types and globals for the scheduler. |
| 83 | ------------------------------------------------------------------ */ |
| 84 | |
| 85 | /* type ThreadId is defined in vg_include.h. */ |
| 86 | |
| 87 | /* struct ThreadState is defined in vg_include.h. */ |
| 88 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 89 | /* Globals. A statically allocated array of threads. NOTE: [0] is |
| 90 | never used, to simplify the simulation of initialisers for |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 91 | LinuxThreads. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 92 | ThreadState VG_(threads)[VG_N_THREADS]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 93 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 94 | /* The process' fork-handler stack. */ |
| 95 | static Int vg_fhstack_used = 0; |
| 96 | static ForkHandlerEntry vg_fhstack[VG_N_FORKHANDLERSTACK]; |
| 97 | |
| 98 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 99 | /* The tid of the thread currently in VG_(baseBlock). */ |
| 100 | static Int vg_tid_currently_in_baseBlock = VG_INVALID_THREADID; |
| 101 | |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 102 | /* The tid either currently in baseBlock, or was in baseBlock before |
| 103 | was saved it out; this is only updated when a new thread is loaded |
| 104 | into the baseBlock */ |
| 105 | static Int vg_tid_last_in_baseBlock = VG_INVALID_THREADID; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 106 | |
| 107 | /* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */ |
| 108 | jmp_buf VG_(scheduler_jmpbuf); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 109 | /* This says whether scheduler_jmpbuf is actually valid. Needed so |
| 110 | that our signal handler doesn't longjmp when the buffer isn't |
| 111 | actually valid. */ |
| 112 | Bool VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 113 | /* ... and if so, here's the signal which caused it to do so. */ |
| 114 | Int VG_(longjmpd_on_signal); |
| 115 | |
| 116 | |
| 117 | /* Machinery to keep track of which threads are waiting on which |
| 118 | fds. */ |
| 119 | typedef |
| 120 | struct { |
| 121 | /* The thread which made the request. */ |
| 122 | ThreadId tid; |
| 123 | |
| 124 | /* The next two fields describe the request. */ |
| 125 | /* File descriptor waited for. -1 means this slot is not in use */ |
| 126 | Int fd; |
| 127 | /* The syscall number the fd is used in. */ |
| 128 | Int syscall_no; |
| 129 | |
| 130 | /* False => still waiting for select to tell us the fd is ready |
| 131 | to go. True => the fd is ready, but the results have not yet |
| 132 | been delivered back to the calling thread. Once the latter |
| 133 | happens, this entire record is marked as no longer in use, by |
| 134 | making the fd field be -1. */ |
| 135 | Bool ready; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 136 | |
| 137 | /* The result from SK_(pre_blocking_syscall)(); is passed to |
| 138 | * SK_(post_blocking_syscall)(). */ |
| 139 | void* pre_result; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 140 | } |
| 141 | VgWaitedOnFd; |
| 142 | |
| 143 | static VgWaitedOnFd vg_waiting_fds[VG_N_WAITING_FDS]; |
| 144 | |
| 145 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 146 | /* Keeping track of keys. */ |
| 147 | typedef |
| 148 | struct { |
| 149 | /* Has this key been allocated ? */ |
| 150 | Bool inuse; |
| 151 | /* If .inuse==True, records the address of the associated |
| 152 | destructor, or NULL if none. */ |
| 153 | void (*destructor)(void*); |
| 154 | } |
| 155 | ThreadKeyState; |
| 156 | |
| 157 | /* And our array of thread keys. */ |
| 158 | static ThreadKeyState vg_thread_keys[VG_N_THREAD_KEYS]; |
| 159 | |
| 160 | typedef UInt ThreadKey; |
| 161 | |
| 162 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 163 | UInt VG_(written_shadow_reg); |
| 164 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 165 | /* Forwards */ |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 166 | static void do_client_request ( ThreadId tid ); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 167 | static void scheduler_sanity ( void ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 168 | static void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid ); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 169 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 170 | /* --------------------------------------------------------------------- |
| 171 | Helper functions for the scheduler. |
| 172 | ------------------------------------------------------------------ */ |
| 173 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 174 | __inline__ |
| 175 | Bool VG_(is_valid_tid) ( ThreadId tid ) |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 176 | { |
| 177 | /* tid is unsigned, hence no < 0 test. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 178 | if (tid == 0) return False; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 179 | if (tid >= VG_N_THREADS) return False; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 180 | if (VG_(threads)[tid].status == VgTs_Empty) return False; |
| 181 | return True; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | __inline__ |
| 186 | Bool VG_(is_valid_or_empty_tid) ( ThreadId tid ) |
| 187 | { |
| 188 | /* tid is unsigned, hence no < 0 test. */ |
| 189 | if (tid == 0) return False; |
| 190 | if (tid >= VG_N_THREADS) return False; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 191 | return True; |
| 192 | } |
| 193 | |
| 194 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 195 | /* For constructing error messages only: try and identify a thread |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 196 | whose stack satisfies the predicate p, or return VG_INVALID_THREADID |
| 197 | if none do. A small complication is dealing with any currently |
| 198 | VG_(baseBlock)-resident thread. |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 199 | */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 200 | ThreadId VG_(first_matching_thread_stack) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 201 | ( Bool (*p) ( Addr stack_min, Addr stack_max )) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 202 | { |
| 203 | ThreadId tid, tid_to_skip; |
| 204 | |
| 205 | tid_to_skip = VG_INVALID_THREADID; |
| 206 | |
| 207 | /* First check to see if there's a currently-loaded thread in |
| 208 | VG_(baseBlock). */ |
| 209 | if (vg_tid_currently_in_baseBlock != VG_INVALID_THREADID) { |
| 210 | tid = vg_tid_currently_in_baseBlock; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 211 | if ( p ( VG_(baseBlock)[VGOFF_(m_esp)], |
| 212 | VG_(threads)[tid].stack_highest_word) ) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 213 | return tid; |
| 214 | else |
| 215 | tid_to_skip = tid; |
| 216 | } |
| 217 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 218 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 219 | if (VG_(threads)[tid].status == VgTs_Empty) continue; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 220 | if (tid == tid_to_skip) continue; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 221 | if ( p ( VG_(threads)[tid].m_esp, |
| 222 | VG_(threads)[tid].stack_highest_word) ) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 223 | return tid; |
| 224 | } |
| 225 | return VG_INVALID_THREADID; |
| 226 | } |
| 227 | |
| 228 | |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 229 | /* Print the scheduler status. */ |
| 230 | void VG_(pp_sched_status) ( void ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 231 | { |
| 232 | Int i; |
| 233 | VG_(printf)("\nsched status:\n"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 234 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 235 | if (VG_(threads)[i].status == VgTs_Empty) continue; |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 236 | VG_(printf)("\nThread %d: status = ", i); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 237 | switch (VG_(threads)[i].status) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 238 | case VgTs_Runnable: VG_(printf)("Runnable"); break; |
| 239 | case VgTs_WaitFD: VG_(printf)("WaitFD"); break; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 240 | case VgTs_WaitJoinee: VG_(printf)("WaitJoinee(%d)", |
| 241 | VG_(threads)[i].joiner_jee_tid); |
| 242 | break; |
| 243 | case VgTs_WaitJoiner: VG_(printf)("WaitJoiner"); break; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 244 | case VgTs_Sleeping: VG_(printf)("Sleeping"); break; |
| 245 | case VgTs_WaitMX: VG_(printf)("WaitMX"); break; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 246 | case VgTs_WaitCV: VG_(printf)("WaitCV"); break; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 247 | case VgTs_WaitSIG: VG_(printf)("WaitSIG"); break; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 248 | default: VG_(printf)("???"); break; |
| 249 | } |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 250 | VG_(printf)(", associated_mx = %p, associated_cv = %p\n", |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 251 | VG_(threads)[i].associated_mx, |
| 252 | VG_(threads)[i].associated_cv ); |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 253 | VG_(pp_ExeContext)( |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 254 | VG_(get_ExeContext2)( VG_(threads)[i].m_eip, VG_(threads)[i].m_ebp, |
| 255 | VG_(threads)[i].m_esp, |
| 256 | VG_(threads)[i].stack_highest_word) |
| 257 | ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 258 | } |
| 259 | VG_(printf)("\n"); |
| 260 | } |
| 261 | |
| 262 | static |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 263 | void add_waiting_fd ( ThreadId tid, Int fd, Int syscall_no, void* pre_res ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 264 | { |
| 265 | Int i; |
| 266 | |
| 267 | vg_assert(fd != -1); /* avoid total chaos */ |
| 268 | |
| 269 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
| 270 | if (vg_waiting_fds[i].fd == -1) |
| 271 | break; |
| 272 | |
| 273 | if (i == VG_N_WAITING_FDS) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 274 | VG_(core_panic)("add_waiting_fd: VG_N_WAITING_FDS is too low"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 275 | /* |
| 276 | VG_(printf)("add_waiting_fd: add (tid %d, fd %d) at slot %d\n", |
| 277 | tid, fd, i); |
| 278 | */ |
| 279 | vg_waiting_fds[i].fd = fd; |
| 280 | vg_waiting_fds[i].tid = tid; |
| 281 | vg_waiting_fds[i].ready = False; |
| 282 | vg_waiting_fds[i].syscall_no = syscall_no; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 283 | vg_waiting_fds[i].pre_result = pre_res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | static |
| 289 | void print_sched_event ( ThreadId tid, Char* what ) |
| 290 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 291 | VG_(message)(Vg_DebugMsg, " SCHED[%d]: %s", tid, what ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
| 295 | static |
| 296 | void print_pthread_event ( ThreadId tid, Char* what ) |
| 297 | { |
| 298 | VG_(message)(Vg_DebugMsg, "PTHREAD[%d]: %s", tid, what ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | static |
| 303 | Char* name_of_sched_event ( UInt event ) |
| 304 | { |
| 305 | switch (event) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 306 | case VG_TRC_EBP_JMP_SYSCALL: return "SYSCALL"; |
| 307 | case VG_TRC_EBP_JMP_CLIENTREQ: return "CLIENTREQ"; |
| 308 | case VG_TRC_INNER_COUNTERZERO: return "COUNTERZERO"; |
| 309 | case VG_TRC_INNER_FASTMISS: return "FASTMISS"; |
| 310 | case VG_TRC_UNRESUMABLE_SIGNAL: return "FATALSIGNAL"; |
| 311 | default: return "??UNKNOWN??"; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* Create a translation of the client basic block beginning at |
| 317 | orig_addr, and add it to the translation cache & translation table. |
| 318 | This probably doesn't really belong here, but, hey ... |
| 319 | */ |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 320 | static |
| 321 | void create_translation_for ( ThreadId tid, Addr orig_addr ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 322 | { |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 323 | Addr trans_addr; |
| 324 | Int orig_size, trans_size; |
| 325 | UShort jumps[VG_MAX_JUMPS]; |
| 326 | Int i; |
| 327 | |
| 328 | for(i = 0; i < VG_MAX_JUMPS; i++) |
| 329 | jumps[i] = (UShort)-1; |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 330 | |
| 331 | /* Make a translation, into temporary storage. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 332 | VG_(translate)( &VG_(threads)[tid], |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 333 | orig_addr, &orig_size, &trans_addr, &trans_size, jumps ); |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 334 | |
| 335 | /* Copy data at trans_addr into the translation cache. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 336 | /* Since the .orig_size and .trans_size fields are |
| 337 | UShort, be paranoid. */ |
| 338 | vg_assert(orig_size > 0 && orig_size < 65536); |
| 339 | vg_assert(trans_size > 0 && trans_size < 65536); |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 340 | |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 341 | VG_(add_to_trans_tab)( orig_addr, orig_size, trans_addr, trans_size, jumps ); |
sewardj | 6c3769f | 2002-11-29 01:02:45 +0000 | [diff] [blame] | 342 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 343 | /* Free the intermediary -- was allocated by VG_(emit_code). */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 344 | VG_(arena_free)( VG_AR_JITTER, (void*)trans_addr ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | /* Allocate a completely empty ThreadState record. */ |
| 349 | static |
| 350 | ThreadId vg_alloc_ThreadState ( void ) |
| 351 | { |
| 352 | Int i; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 353 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 354 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 355 | return i; |
| 356 | } |
| 357 | VG_(printf)("vg_alloc_ThreadState: no free slots available\n"); |
| 358 | VG_(printf)("Increase VG_N_THREADS, rebuild and try again.\n"); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 359 | VG_(core_panic)("VG_N_THREADS is too low"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 360 | /*NOTREACHED*/ |
| 361 | } |
| 362 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 363 | ThreadState* VG_(get_ThreadState)( ThreadId tid ) |
| 364 | { |
| 365 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
| 366 | return & VG_(threads)[tid]; |
| 367 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 368 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 369 | ThreadState* VG_(get_current_thread_state) ( void ) |
| 370 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 371 | vg_assert(VG_(is_valid_tid)(vg_tid_currently_in_baseBlock)); |
| 372 | return & VG_(threads)[vg_tid_currently_in_baseBlock]; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | |
| 376 | ThreadId VG_(get_current_tid) ( void ) |
| 377 | { |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 378 | if (!VG_(is_valid_tid)(vg_tid_currently_in_baseBlock)) |
| 379 | return VG_INVALID_THREADID; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 380 | return vg_tid_currently_in_baseBlock; |
| 381 | } |
| 382 | |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 383 | ThreadId VG_(get_current_or_recent_tid) ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 384 | { |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 385 | vg_assert(vg_tid_currently_in_baseBlock == vg_tid_last_in_baseBlock || |
| 386 | vg_tid_currently_in_baseBlock == VG_INVALID_THREADID); |
| 387 | vg_assert(VG_(is_valid_tid)(vg_tid_last_in_baseBlock)); |
| 388 | |
| 389 | return vg_tid_last_in_baseBlock; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 390 | } |
| 391 | |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 392 | ThreadId VG_(get_tid_from_ThreadState) (ThreadState* tst) |
| 393 | { |
| 394 | vg_assert(tst >= &VG_(threads)[1] && tst < &VG_(threads)[VG_N_THREADS]); |
| 395 | return tst->tid; |
| 396 | } |
| 397 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 398 | /* Copy the saved state of a thread into VG_(baseBlock), ready for it |
| 399 | to be run. */ |
| 400 | __inline__ |
| 401 | void VG_(load_thread_state) ( ThreadId tid ) |
| 402 | { |
| 403 | Int i; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 404 | vg_assert(vg_tid_currently_in_baseBlock == VG_INVALID_THREADID); |
| 405 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 406 | VG_(baseBlock)[VGOFF_(ldt)] = (UInt)VG_(threads)[tid].ldt; |
| 407 | VG_(baseBlock)[VGOFF_(m_cs)] = VG_(threads)[tid].m_cs; |
| 408 | VG_(baseBlock)[VGOFF_(m_ss)] = VG_(threads)[tid].m_ss; |
| 409 | VG_(baseBlock)[VGOFF_(m_ds)] = VG_(threads)[tid].m_ds; |
| 410 | VG_(baseBlock)[VGOFF_(m_es)] = VG_(threads)[tid].m_es; |
| 411 | VG_(baseBlock)[VGOFF_(m_fs)] = VG_(threads)[tid].m_fs; |
| 412 | VG_(baseBlock)[VGOFF_(m_gs)] = VG_(threads)[tid].m_gs; |
| 413 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 414 | VG_(baseBlock)[VGOFF_(m_eax)] = VG_(threads)[tid].m_eax; |
| 415 | VG_(baseBlock)[VGOFF_(m_ebx)] = VG_(threads)[tid].m_ebx; |
| 416 | VG_(baseBlock)[VGOFF_(m_ecx)] = VG_(threads)[tid].m_ecx; |
| 417 | VG_(baseBlock)[VGOFF_(m_edx)] = VG_(threads)[tid].m_edx; |
| 418 | VG_(baseBlock)[VGOFF_(m_esi)] = VG_(threads)[tid].m_esi; |
| 419 | VG_(baseBlock)[VGOFF_(m_edi)] = VG_(threads)[tid].m_edi; |
| 420 | VG_(baseBlock)[VGOFF_(m_ebp)] = VG_(threads)[tid].m_ebp; |
| 421 | VG_(baseBlock)[VGOFF_(m_esp)] = VG_(threads)[tid].m_esp; |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 422 | VG_(baseBlock)[VGOFF_(m_eflags)] = VG_(threads)[tid].m_eflags & ~EFlagD; |
| 423 | VG_(baseBlock)[VGOFF_(m_dflag)] = VG_(extractDflag)(VG_(threads)[tid].m_eflags); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 424 | VG_(baseBlock)[VGOFF_(m_eip)] = VG_(threads)[tid].m_eip; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 425 | |
| 426 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 427 | VG_(baseBlock)[VGOFF_(m_fpustate) + i] = VG_(threads)[tid].m_fpu[i]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 428 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 429 | if (VG_(needs).shadow_regs) { |
| 430 | VG_(baseBlock)[VGOFF_(sh_eax)] = VG_(threads)[tid].sh_eax; |
| 431 | VG_(baseBlock)[VGOFF_(sh_ebx)] = VG_(threads)[tid].sh_ebx; |
| 432 | VG_(baseBlock)[VGOFF_(sh_ecx)] = VG_(threads)[tid].sh_ecx; |
| 433 | VG_(baseBlock)[VGOFF_(sh_edx)] = VG_(threads)[tid].sh_edx; |
| 434 | VG_(baseBlock)[VGOFF_(sh_esi)] = VG_(threads)[tid].sh_esi; |
| 435 | VG_(baseBlock)[VGOFF_(sh_edi)] = VG_(threads)[tid].sh_edi; |
| 436 | VG_(baseBlock)[VGOFF_(sh_ebp)] = VG_(threads)[tid].sh_ebp; |
| 437 | VG_(baseBlock)[VGOFF_(sh_esp)] = VG_(threads)[tid].sh_esp; |
| 438 | VG_(baseBlock)[VGOFF_(sh_eflags)] = VG_(threads)[tid].sh_eflags; |
| 439 | } else { |
| 440 | /* Fields shouldn't be used -- check their values haven't changed. */ |
| 441 | /* Nb: they are written to by some macros like SET_EDX, but they |
| 442 | * should just write VG_UNUSED_SHADOW_REG_VALUE. */ |
| 443 | vg_assert( |
| 444 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_eax && |
| 445 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_ebx && |
| 446 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_ecx && |
| 447 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_edx && |
| 448 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_esi && |
| 449 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_edi && |
| 450 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_ebp && |
| 451 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_esp && |
| 452 | VG_UNUSED_SHADOW_REG_VALUE == VG_(threads)[tid].sh_eflags); |
| 453 | } |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 454 | |
| 455 | vg_tid_currently_in_baseBlock = tid; |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 456 | vg_tid_last_in_baseBlock = tid; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | /* Copy the state of a thread from VG_(baseBlock), presumably after it |
| 461 | has been descheduled. For sanity-check purposes, fill the vacated |
| 462 | VG_(baseBlock) with garbage so as to make the system more likely to |
| 463 | fail quickly if we erroneously continue to poke around inside |
| 464 | VG_(baseBlock) without first doing a load_thread_state(). |
| 465 | */ |
| 466 | __inline__ |
| 467 | void VG_(save_thread_state) ( ThreadId tid ) |
| 468 | { |
| 469 | Int i; |
| 470 | const UInt junk = 0xDEADBEEF; |
| 471 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 472 | vg_assert(vg_tid_currently_in_baseBlock != VG_INVALID_THREADID); |
| 473 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 474 | |
| 475 | /* We don't copy out the LDT entry, because it can never be changed |
| 476 | by the normal actions of the thread, only by the modify_ldt |
| 477 | syscall, in which case we will correctly be updating |
sewardj | fb5e527 | 2002-12-08 23:27:21 +0000 | [diff] [blame] | 478 | VG_(threads)[tid].ldt. This printf happens iff the following |
| 479 | assertion fails. */ |
sewardj | ca340b3 | 2002-12-08 22:14:11 +0000 | [diff] [blame] | 480 | if ((void*)VG_(threads)[tid].ldt != (void*)VG_(baseBlock)[VGOFF_(ldt)]) |
| 481 | VG_(printf)("VG_(threads)[%d].ldt=%p VG_(baseBlock)[VGOFF_(ldt)]=%p\n", |
sewardj | fb5e527 | 2002-12-08 23:27:21 +0000 | [diff] [blame] | 482 | tid, (void*)VG_(threads)[tid].ldt, |
| 483 | (void*)VG_(baseBlock)[VGOFF_(ldt)]); |
sewardj | ca340b3 | 2002-12-08 22:14:11 +0000 | [diff] [blame] | 484 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 485 | vg_assert((void*)VG_(threads)[tid].ldt |
| 486 | == (void*)VG_(baseBlock)[VGOFF_(ldt)]); |
| 487 | |
| 488 | VG_(threads)[tid].m_cs = VG_(baseBlock)[VGOFF_(m_cs)]; |
| 489 | VG_(threads)[tid].m_ss = VG_(baseBlock)[VGOFF_(m_ss)]; |
| 490 | VG_(threads)[tid].m_ds = VG_(baseBlock)[VGOFF_(m_ds)]; |
| 491 | VG_(threads)[tid].m_es = VG_(baseBlock)[VGOFF_(m_es)]; |
| 492 | VG_(threads)[tid].m_fs = VG_(baseBlock)[VGOFF_(m_fs)]; |
| 493 | VG_(threads)[tid].m_gs = VG_(baseBlock)[VGOFF_(m_gs)]; |
| 494 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 495 | VG_(threads)[tid].m_eax = VG_(baseBlock)[VGOFF_(m_eax)]; |
| 496 | VG_(threads)[tid].m_ebx = VG_(baseBlock)[VGOFF_(m_ebx)]; |
| 497 | VG_(threads)[tid].m_ecx = VG_(baseBlock)[VGOFF_(m_ecx)]; |
| 498 | VG_(threads)[tid].m_edx = VG_(baseBlock)[VGOFF_(m_edx)]; |
| 499 | VG_(threads)[tid].m_esi = VG_(baseBlock)[VGOFF_(m_esi)]; |
| 500 | VG_(threads)[tid].m_edi = VG_(baseBlock)[VGOFF_(m_edi)]; |
| 501 | VG_(threads)[tid].m_ebp = VG_(baseBlock)[VGOFF_(m_ebp)]; |
| 502 | VG_(threads)[tid].m_esp = VG_(baseBlock)[VGOFF_(m_esp)]; |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 503 | VG_(threads)[tid].m_eflags = VG_(insertDflag)(VG_(baseBlock)[VGOFF_(m_eflags)], |
| 504 | VG_(baseBlock)[VGOFF_(m_dflag)]); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 505 | VG_(threads)[tid].m_eip = VG_(baseBlock)[VGOFF_(m_eip)]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 506 | |
| 507 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 508 | VG_(threads)[tid].m_fpu[i] = VG_(baseBlock)[VGOFF_(m_fpustate) + i]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 509 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 510 | if (VG_(needs).shadow_regs) { |
| 511 | VG_(threads)[tid].sh_eax = VG_(baseBlock)[VGOFF_(sh_eax)]; |
| 512 | VG_(threads)[tid].sh_ebx = VG_(baseBlock)[VGOFF_(sh_ebx)]; |
| 513 | VG_(threads)[tid].sh_ecx = VG_(baseBlock)[VGOFF_(sh_ecx)]; |
| 514 | VG_(threads)[tid].sh_edx = VG_(baseBlock)[VGOFF_(sh_edx)]; |
| 515 | VG_(threads)[tid].sh_esi = VG_(baseBlock)[VGOFF_(sh_esi)]; |
| 516 | VG_(threads)[tid].sh_edi = VG_(baseBlock)[VGOFF_(sh_edi)]; |
| 517 | VG_(threads)[tid].sh_ebp = VG_(baseBlock)[VGOFF_(sh_ebp)]; |
| 518 | VG_(threads)[tid].sh_esp = VG_(baseBlock)[VGOFF_(sh_esp)]; |
| 519 | VG_(threads)[tid].sh_eflags = VG_(baseBlock)[VGOFF_(sh_eflags)]; |
| 520 | } else { |
| 521 | /* Fill with recognisable junk */ |
| 522 | VG_(threads)[tid].sh_eax = |
| 523 | VG_(threads)[tid].sh_ebx = |
| 524 | VG_(threads)[tid].sh_ecx = |
| 525 | VG_(threads)[tid].sh_edx = |
| 526 | VG_(threads)[tid].sh_esi = |
| 527 | VG_(threads)[tid].sh_edi = |
| 528 | VG_(threads)[tid].sh_ebp = |
| 529 | VG_(threads)[tid].sh_esp = |
| 530 | VG_(threads)[tid].sh_eflags = VG_UNUSED_SHADOW_REG_VALUE; |
| 531 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 532 | |
| 533 | /* Fill it up with junk. */ |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 534 | VG_(baseBlock)[VGOFF_(ldt)] = junk; |
| 535 | VG_(baseBlock)[VGOFF_(m_cs)] = junk; |
| 536 | VG_(baseBlock)[VGOFF_(m_ss)] = junk; |
| 537 | VG_(baseBlock)[VGOFF_(m_ds)] = junk; |
| 538 | VG_(baseBlock)[VGOFF_(m_es)] = junk; |
| 539 | VG_(baseBlock)[VGOFF_(m_fs)] = junk; |
| 540 | VG_(baseBlock)[VGOFF_(m_gs)] = junk; |
| 541 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 542 | VG_(baseBlock)[VGOFF_(m_eax)] = junk; |
| 543 | VG_(baseBlock)[VGOFF_(m_ebx)] = junk; |
| 544 | VG_(baseBlock)[VGOFF_(m_ecx)] = junk; |
| 545 | VG_(baseBlock)[VGOFF_(m_edx)] = junk; |
| 546 | VG_(baseBlock)[VGOFF_(m_esi)] = junk; |
| 547 | VG_(baseBlock)[VGOFF_(m_edi)] = junk; |
| 548 | VG_(baseBlock)[VGOFF_(m_ebp)] = junk; |
| 549 | VG_(baseBlock)[VGOFF_(m_esp)] = junk; |
| 550 | VG_(baseBlock)[VGOFF_(m_eflags)] = junk; |
| 551 | VG_(baseBlock)[VGOFF_(m_eip)] = junk; |
| 552 | |
| 553 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
| 554 | VG_(baseBlock)[VGOFF_(m_fpustate) + i] = junk; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 555 | |
| 556 | vg_tid_currently_in_baseBlock = VG_INVALID_THREADID; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
| 560 | /* Run the thread tid for a while, and return a VG_TRC_* value to the |
| 561 | scheduler indicating what happened. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 562 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 563 | UInt run_thread_for_a_while ( ThreadId tid ) |
| 564 | { |
sewardj | 7ccc5c2 | 2002-04-24 21:39:11 +0000 | [diff] [blame] | 565 | volatile UInt trc = 0; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 566 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 567 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 568 | vg_assert(VG_(bbs_to_go) > 0); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 569 | vg_assert(!VG_(scheduler_jmpbuf_valid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 570 | |
sewardj | 671ff54 | 2002-05-07 09:25:30 +0000 | [diff] [blame] | 571 | VGP_PUSHCC(VgpRun); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 572 | VG_(load_thread_state) ( tid ); |
| 573 | if (__builtin_setjmp(VG_(scheduler_jmpbuf)) == 0) { |
| 574 | /* try this ... */ |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 575 | VG_(scheduler_jmpbuf_valid) = True; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 576 | trc = VG_(run_innerloop)(); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 577 | VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 578 | /* We get here if the client didn't take a fault. */ |
| 579 | } else { |
| 580 | /* We get here if the client took a fault, which caused our |
| 581 | signal handler to longjmp. */ |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 582 | VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 583 | vg_assert(trc == 0); |
| 584 | trc = VG_TRC_UNRESUMABLE_SIGNAL; |
| 585 | } |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 586 | |
| 587 | vg_assert(!VG_(scheduler_jmpbuf_valid)); |
| 588 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 589 | VG_(save_thread_state) ( tid ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 590 | VGP_POPCC(VgpRun); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 591 | return trc; |
| 592 | } |
| 593 | |
| 594 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 595 | static |
| 596 | void mostly_clear_thread_record ( ThreadId tid ) |
| 597 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 598 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 599 | VG_(threads)[tid].ldt = NULL; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 600 | VG_(threads)[tid].tid = tid; |
| 601 | VG_(threads)[tid].status = VgTs_Empty; |
| 602 | VG_(threads)[tid].associated_mx = NULL; |
| 603 | VG_(threads)[tid].associated_cv = NULL; |
| 604 | VG_(threads)[tid].awaken_at = 0; |
| 605 | VG_(threads)[tid].joinee_retval = NULL; |
| 606 | VG_(threads)[tid].joiner_thread_return = NULL; |
| 607 | VG_(threads)[tid].joiner_jee_tid = VG_INVALID_THREADID; |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 608 | VG_(threads)[tid].detached = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 609 | VG_(threads)[tid].cancel_st = True; /* PTHREAD_CANCEL_ENABLE */ |
| 610 | VG_(threads)[tid].cancel_ty = True; /* PTHREAD_CANCEL_DEFERRED */ |
| 611 | VG_(threads)[tid].cancel_pend = NULL; /* not pending */ |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 612 | VG_(threads)[tid].custack_used = 0; |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 613 | VG_(threads)[tid].n_signals_returned = 0; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 614 | VG_(ksigemptyset)(&VG_(threads)[tid].sig_mask); |
| 615 | VG_(ksigemptyset)(&VG_(threads)[tid].sigs_waited_for); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 616 | VG_(threads)[tid].specifics_ptr = NULL; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 620 | /* Initialise the scheduler. Create a single "main" thread ready to |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 621 | run, with special ThreadId of one. This is called at startup; the |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 622 | caller takes care to park the client's state is parked in |
| 623 | VG_(baseBlock). |
| 624 | */ |
| 625 | void VG_(scheduler_init) ( void ) |
| 626 | { |
| 627 | Int i; |
| 628 | Addr startup_esp; |
| 629 | ThreadId tid_main; |
| 630 | |
| 631 | startup_esp = VG_(baseBlock)[VGOFF_(m_esp)]; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 632 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 633 | for (i = 0 /* NB; not 1 */; i < VG_N_THREADS; i++) { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 634 | mostly_clear_thread_record(i); |
| 635 | VG_(threads)[i].stack_size = 0; |
| 636 | VG_(threads)[i].stack_base = (Addr)NULL; |
| 637 | VG_(threads)[i].stack_highest_word = (Addr)NULL; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
| 641 | vg_waiting_fds[i].fd = -1; /* not in use */ |
| 642 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 643 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 644 | vg_thread_keys[i].inuse = False; |
| 645 | vg_thread_keys[i].destructor = NULL; |
| 646 | } |
| 647 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 648 | vg_fhstack_used = 0; |
| 649 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 650 | /* Assert this is thread zero, which has certain magic |
| 651 | properties. */ |
| 652 | tid_main = vg_alloc_ThreadState(); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 653 | vg_assert(tid_main == 1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 654 | VG_(threads)[tid_main].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 655 | |
| 656 | /* Copy VG_(baseBlock) state to tid_main's slot. */ |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 657 | vg_tid_currently_in_baseBlock = tid_main; |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 658 | vg_tid_last_in_baseBlock = tid_main; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 659 | VG_(save_thread_state) ( tid_main ); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 660 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 661 | VG_(threads)[tid_main].stack_highest_word |
| 662 | = VG_(threads)[tid_main].m_esp /* -4 ??? */; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 663 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 664 | /* So now ... */ |
| 665 | vg_assert(vg_tid_currently_in_baseBlock == VG_INVALID_THREADID); |
sewardj | 872051c | 2002-07-13 12:12:56 +0000 | [diff] [blame] | 666 | |
| 667 | /* Not running client code right now. */ |
| 668 | VG_(scheduler_jmpbuf_valid) = False; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
| 672 | /* What if fd isn't a valid fd? */ |
| 673 | static |
| 674 | void set_fd_nonblocking ( Int fd ) |
| 675 | { |
| 676 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 677 | vg_assert(!VG_(is_kerror)(res)); |
| 678 | res |= VKI_O_NONBLOCK; |
| 679 | res = VG_(fcntl)( fd, VKI_F_SETFL, res ); |
| 680 | vg_assert(!VG_(is_kerror)(res)); |
| 681 | } |
| 682 | |
| 683 | static |
| 684 | void set_fd_blocking ( Int fd ) |
| 685 | { |
| 686 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 687 | vg_assert(!VG_(is_kerror)(res)); |
| 688 | res &= ~VKI_O_NONBLOCK; |
| 689 | res = VG_(fcntl)( fd, VKI_F_SETFL, res ); |
| 690 | vg_assert(!VG_(is_kerror)(res)); |
| 691 | } |
| 692 | |
| 693 | static |
| 694 | Bool fd_is_blockful ( Int fd ) |
| 695 | { |
| 696 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 697 | vg_assert(!VG_(is_kerror)(res)); |
| 698 | return (res & VKI_O_NONBLOCK) ? False : True; |
| 699 | } |
| 700 | |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 701 | static |
| 702 | Bool fd_is_valid ( Int fd ) |
| 703 | { |
| 704 | Int res = VG_(fcntl)( fd, VKI_F_GETFL, 0 ); |
| 705 | return VG_(is_kerror)(res) ? False : True; |
| 706 | } |
| 707 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 708 | |
| 709 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 710 | /* vthread tid is returning from a signal handler; modify its |
| 711 | stack/regs accordingly. */ |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 712 | |
| 713 | /* [Helper fn for handle_signal_return] tid, assumed to be in WaitFD |
| 714 | for read or write, has been interrupted by a signal. Find and |
| 715 | clear the relevant vg_waiting_fd[] entry. Most of the code in this |
| 716 | procedure is total paranoia, if you look closely. */ |
sewardj | 420b928 | 2003-04-03 22:28:00 +0000 | [diff] [blame] | 717 | |
| 718 | /* 4 Apr 2003: monty@mysql.com sent a fix, which adds the comparisons |
| 719 | against -1, and the following explaination. |
| 720 | |
| 721 | Valgrind uses fd = -1 internally to tell that a file descriptor is |
| 722 | not in use, as the following code shows (at end of |
| 723 | cleanup_waiting_fd_table()). |
| 724 | |
| 725 | vg_assert(waiters == 1); |
| 726 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
| 727 | if (vg_waiting_fds[i].tid == tid && vg_waiting_fds[i].fd != -1) |
| 728 | break; |
| 729 | vg_assert(i < VG_N_WAITING_FDS); |
| 730 | vg_assert(vg_waiting_fds[i].fd != -1); |
| 731 | vg_waiting_fds[i].fd = -1; -- not in use |
| 732 | ^^^^^^^ |
| 733 | |
| 734 | The bug is that valrind is setting fd = -1 for a not used file |
| 735 | descriptor but vg_waiting_fds[i].tid is not reset. |
| 736 | |
| 737 | What happens is that on a later call to cleanup_waiting_fd_table() |
| 738 | the function will find old files that was waited on before by the |
| 739 | same thread, even if they are marked as 'not in use' by the above |
| 740 | code. |
| 741 | |
| 742 | I first tried to fix the bug by setting vg_waiting_fds[i].tid to 0 |
| 743 | at the end of the above function but this didn't fix the bug. |
| 744 | (Maybe there is other places in the code where tid is not properly |
| 745 | reset). After adding the test for 'fd == -1' to the loops in |
| 746 | cleanup_waiting_fd_table() all problems disappeared. |
| 747 | */ |
| 748 | |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 749 | static |
| 750 | void cleanup_waiting_fd_table ( ThreadId tid ) |
| 751 | { |
| 752 | Int i, waiters; |
| 753 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 754 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 755 | vg_assert(VG_(threads)[tid].status == VgTs_WaitFD); |
| 756 | vg_assert(VG_(threads)[tid].m_eax == __NR_read |
| 757 | || VG_(threads)[tid].m_eax == __NR_write); |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 758 | |
| 759 | /* Excessively paranoidly ... find the fd this op was waiting |
| 760 | for, and mark it as not being waited on. */ |
| 761 | waiters = 0; |
| 762 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
sewardj | 420b928 | 2003-04-03 22:28:00 +0000 | [diff] [blame] | 763 | if (vg_waiting_fds[i].tid == tid && vg_waiting_fds[i].fd != -1) { |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 764 | waiters++; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 765 | vg_assert(vg_waiting_fds[i].syscall_no == VG_(threads)[tid].m_eax); |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | vg_assert(waiters == 1); |
| 769 | for (i = 0; i < VG_N_WAITING_FDS; i++) |
sewardj | 420b928 | 2003-04-03 22:28:00 +0000 | [diff] [blame] | 770 | if (vg_waiting_fds[i].tid == tid && vg_waiting_fds[i].fd != -1) |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 771 | break; |
| 772 | vg_assert(i < VG_N_WAITING_FDS); |
| 773 | vg_assert(vg_waiting_fds[i].fd != -1); |
| 774 | vg_waiting_fds[i].fd = -1; /* not in use */ |
| 775 | } |
| 776 | |
| 777 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 778 | static |
| 779 | void handle_signal_return ( ThreadId tid ) |
| 780 | { |
| 781 | Char msg_buf[100]; |
| 782 | Bool restart_blocked_syscalls; |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 783 | struct vki_timespec * rem; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 784 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 785 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 786 | |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 787 | /* Increment signal-returned counter. Used only to implement |
| 788 | pause(). */ |
| 789 | VG_(threads)[tid].n_signals_returned++; |
| 790 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 791 | restart_blocked_syscalls = VG_(signal_returns)(tid); |
| 792 | |
| 793 | if (restart_blocked_syscalls) |
| 794 | /* Easy; we don't have to do anything. */ |
| 795 | return; |
| 796 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 797 | if (VG_(threads)[tid].status == VgTs_WaitFD |
| 798 | && (VG_(threads)[tid].m_eax == __NR_read |
| 799 | || VG_(threads)[tid].m_eax == __NR_write)) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 800 | /* read() or write() interrupted. Force a return with EINTR. */ |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 801 | cleanup_waiting_fd_table(tid); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 802 | VG_(threads)[tid].m_eax = -VKI_EINTR; |
| 803 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 804 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 805 | if (VG_(clo_trace_sched)) { |
| 806 | VG_(sprintf)(msg_buf, |
| 807 | "read() / write() interrupted by signal; return EINTR" ); |
| 808 | print_sched_event(tid, msg_buf); |
| 809 | } |
| 810 | return; |
| 811 | } |
| 812 | |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 813 | if (VG_(threads)[tid].status == VgTs_Sleeping |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 814 | && VG_(threads)[tid].m_eax == __NR_nanosleep) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 815 | /* We interrupted a nanosleep(). The right thing to do is to |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 816 | write the unused time to nanosleep's second param, but that's |
| 817 | too much effort ... we just say that 1 nanosecond was not |
| 818 | used, and return EINTR. */ |
| 819 | rem = (struct vki_timespec *)VG_(threads)[tid].m_ecx; /* arg2 */ |
| 820 | if (rem != NULL) { |
| 821 | rem->tv_sec = 0; |
| 822 | rem->tv_nsec = 1; |
| 823 | } |
| 824 | SET_EAX(tid, -VKI_EINTR); |
| 825 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 826 | return; |
| 827 | } |
| 828 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 829 | if (VG_(threads)[tid].status == VgTs_WaitFD) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 830 | VG_(core_panic)("handle_signal_return: unknown interrupted syscall"); |
sewardj | 1ffa8da | 2002-04-26 22:47:57 +0000 | [diff] [blame] | 831 | } |
| 832 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 833 | /* All other cases? Just return. */ |
| 834 | } |
| 835 | |
| 836 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 837 | static |
| 838 | void sched_do_syscall ( ThreadId tid ) |
| 839 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 840 | UInt saved_eax; |
| 841 | UInt res, syscall_no; |
| 842 | UInt fd; |
| 843 | void* pre_res; |
| 844 | Bool orig_fd_blockness; |
| 845 | Char msg_buf[100]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 846 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 847 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 848 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 849 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 850 | syscall_no = VG_(threads)[tid].m_eax; /* syscall number */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 851 | |
| 852 | if (syscall_no == __NR_nanosleep) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 853 | UInt t_now, t_awaken; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 854 | struct vki_timespec* req; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 855 | req = (struct vki_timespec*)VG_(threads)[tid].m_ebx; /* arg1 */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 856 | t_now = VG_(read_millisecond_timer)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 857 | t_awaken |
| 858 | = t_now |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 859 | + (UInt)1000ULL * (UInt)(req->tv_sec) |
| 860 | + (UInt)(req->tv_nsec) / 1000000; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 861 | VG_(threads)[tid].status = VgTs_Sleeping; |
| 862 | VG_(threads)[tid].awaken_at = t_awaken; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 863 | if (VG_(clo_trace_sched)) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 864 | VG_(sprintf)(msg_buf, "at %d: nanosleep for %d", |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 865 | t_now, t_awaken-t_now); |
| 866 | print_sched_event(tid, msg_buf); |
| 867 | } |
| 868 | /* Force the scheduler to run something else for a while. */ |
| 869 | return; |
| 870 | } |
| 871 | |
sewardj | aec22c0 | 2002-04-29 01:58:08 +0000 | [diff] [blame] | 872 | if (syscall_no != __NR_read && syscall_no != __NR_write) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 873 | /* We think it's non-blocking. Just do it in the normal way. */ |
| 874 | VG_(perform_assumed_nonblocking_syscall)(tid); |
| 875 | /* The thread is still runnable. */ |
| 876 | return; |
| 877 | } |
| 878 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 879 | /* Set the fd to nonblocking, and do the syscall, which will return |
| 880 | immediately, in order to lodge a request with the Linux kernel. |
| 881 | We later poll for I/O completion using select(). */ |
| 882 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 883 | fd = VG_(threads)[tid].m_ebx /* arg1 */; |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 884 | |
| 885 | /* Deal with error case immediately. */ |
| 886 | if (!fd_is_valid(fd)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 887 | if (VG_(needs).core_errors) |
| 888 | VG_(message)(Vg_UserMsg, |
| 889 | "Warning: invalid file descriptor %d in syscall %s", |
| 890 | fd, syscall_no == __NR_read ? "read()" : "write()" ); |
| 891 | pre_res = VG_(pre_known_blocking_syscall)(tid, syscall_no); |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 892 | KERNEL_DO_SYSCALL(tid, res); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 893 | VG_(post_known_blocking_syscall)(tid, syscall_no, pre_res, res); |
sewardj | 3947e62 | 2002-05-23 16:52:11 +0000 | [diff] [blame] | 894 | /* We're still runnable. */ |
| 895 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 896 | return; |
| 897 | } |
| 898 | |
| 899 | /* From here onwards we know that fd is valid. */ |
| 900 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 901 | orig_fd_blockness = fd_is_blockful(fd); |
| 902 | set_fd_nonblocking(fd); |
| 903 | vg_assert(!fd_is_blockful(fd)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 904 | pre_res = VG_(pre_known_blocking_syscall)(tid, syscall_no); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 905 | |
| 906 | /* This trashes the thread's %eax; we have to preserve it. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 907 | saved_eax = VG_(threads)[tid].m_eax; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 908 | KERNEL_DO_SYSCALL(tid,res); |
| 909 | |
| 910 | /* Restore original blockfulness of the fd. */ |
| 911 | if (orig_fd_blockness) |
| 912 | set_fd_blocking(fd); |
| 913 | else |
| 914 | set_fd_nonblocking(fd); |
| 915 | |
sewardj | aec22c0 | 2002-04-29 01:58:08 +0000 | [diff] [blame] | 916 | if (res != -VKI_EWOULDBLOCK || !orig_fd_blockness) { |
| 917 | /* Finish off in the normal way. Don't restore %EAX, since that |
| 918 | now (correctly) holds the result of the call. We get here if either: |
| 919 | 1. The call didn't block, or |
| 920 | 2. The fd was already in nonblocking mode before we started to |
| 921 | mess with it. In this case, we're not expecting to handle |
| 922 | the I/O completion -- the client is. So don't file a |
| 923 | completion-wait entry. |
| 924 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 925 | VG_(post_known_blocking_syscall)(tid, syscall_no, pre_res, res); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 926 | /* We're still runnable. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 927 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 928 | |
| 929 | } else { |
| 930 | |
sewardj | aec22c0 | 2002-04-29 01:58:08 +0000 | [diff] [blame] | 931 | vg_assert(res == -VKI_EWOULDBLOCK && orig_fd_blockness); |
| 932 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 933 | /* It would have blocked. First, restore %EAX to what it was |
| 934 | before our speculative call. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 935 | VG_(threads)[tid].m_eax = saved_eax; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 936 | /* Put this fd in a table of fds on which we are waiting for |
| 937 | completion. The arguments for select() later are constructed |
| 938 | from this table. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 939 | add_waiting_fd(tid, fd, saved_eax /* which holds the syscall # */, |
| 940 | pre_res); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 941 | /* Deschedule thread until an I/O completion happens. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 942 | VG_(threads)[tid].status = VgTs_WaitFD; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 943 | if (VG_(clo_trace_sched)) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 944 | VG_(sprintf)(msg_buf,"block until I/O ready on fd %d", fd); |
| 945 | print_sched_event(tid, msg_buf); |
| 946 | } |
| 947 | |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | |
| 952 | /* Find out which of the fds in vg_waiting_fds are now ready to go, by |
| 953 | making enquiries with select(), and mark them as ready. We have to |
| 954 | wait for the requesting threads to fall into the the WaitFD state |
| 955 | before we can actually finally deliver the results, so this |
| 956 | procedure doesn't do that; complete_blocked_syscalls() does it. |
| 957 | |
| 958 | It might seem odd that a thread which has done a blocking syscall |
| 959 | is not in WaitFD state; the way this can happen is if it initially |
| 960 | becomes WaitFD, but then a signal is delivered to it, so it becomes |
| 961 | Runnable for a while. In this case we have to wait for the |
| 962 | sighandler to return, whereupon the WaitFD state is resumed, and |
| 963 | only at that point can the I/O result be delivered to it. However, |
| 964 | this point may be long after the fd is actually ready. |
| 965 | |
| 966 | So, poll_for_ready_fds() merely detects fds which are ready. |
| 967 | complete_blocked_syscalls() does the second half of the trick, |
| 968 | possibly much later: it delivers the results from ready fds to |
| 969 | threads in WaitFD state. |
| 970 | */ |
sewardj | 9a199dc | 2002-04-14 13:01:38 +0000 | [diff] [blame] | 971 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 972 | void poll_for_ready_fds ( void ) |
| 973 | { |
| 974 | vki_ksigset_t saved_procmask; |
| 975 | vki_fd_set readfds; |
| 976 | vki_fd_set writefds; |
| 977 | vki_fd_set exceptfds; |
| 978 | struct vki_timeval timeout; |
| 979 | Int fd, fd_max, i, n_ready, syscall_no, n_ok; |
| 980 | ThreadId tid; |
| 981 | Bool rd_ok, wr_ok, ex_ok; |
| 982 | Char msg_buf[100]; |
| 983 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 984 | struct vki_timespec* rem; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 985 | UInt t_now; |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 986 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 987 | /* Awaken any sleeping threads whose sleep has expired. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 988 | for (tid = 1; tid < VG_N_THREADS; tid++) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 989 | if (VG_(threads)[tid].status == VgTs_Sleeping) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 990 | break; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 991 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 992 | /* Avoid pointless calls to VG_(read_millisecond_timer). */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 993 | if (tid < VG_N_THREADS) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 994 | t_now = VG_(read_millisecond_timer)(); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 995 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 996 | if (VG_(threads)[tid].status != VgTs_Sleeping) |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 997 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 998 | if (t_now >= VG_(threads)[tid].awaken_at) { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 999 | /* Resume this thread. Set to zero the remaining-time |
| 1000 | (second) arg of nanosleep, since it's used up all its |
| 1001 | time. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1002 | vg_assert(VG_(threads)[tid].m_eax == __NR_nanosleep); |
| 1003 | rem = (struct vki_timespec *)VG_(threads)[tid].m_ecx; /* arg2 */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1004 | if (rem != NULL) { |
| 1005 | rem->tv_sec = 0; |
| 1006 | rem->tv_nsec = 0; |
| 1007 | } |
| 1008 | /* Make the syscall return 0 (success). */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1009 | VG_(threads)[tid].m_eax = 0; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1010 | /* Reschedule this thread. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1011 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1012 | if (VG_(clo_trace_sched)) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1013 | VG_(sprintf)(msg_buf, "at %d: nanosleep done", |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1014 | t_now); |
| 1015 | print_sched_event(tid, msg_buf); |
| 1016 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1020 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 1021 | /* And look for threads waiting on file descriptors which are now |
| 1022 | ready for I/O.*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1023 | timeout.tv_sec = 0; |
| 1024 | timeout.tv_usec = 0; |
| 1025 | |
| 1026 | VKI_FD_ZERO(&readfds); |
| 1027 | VKI_FD_ZERO(&writefds); |
| 1028 | VKI_FD_ZERO(&exceptfds); |
| 1029 | fd_max = -1; |
| 1030 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1031 | if (vg_waiting_fds[i].fd == -1 /* not in use */) |
| 1032 | continue; |
| 1033 | if (vg_waiting_fds[i].ready /* already ready? */) |
| 1034 | continue; |
| 1035 | fd = vg_waiting_fds[i].fd; |
| 1036 | /* VG_(printf)("adding QUERY for fd %d\n", fd); */ |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 1037 | vg_assert(fd >= 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1038 | if (fd > fd_max) |
| 1039 | fd_max = fd; |
| 1040 | tid = vg_waiting_fds[i].tid; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1041 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1042 | syscall_no = vg_waiting_fds[i].syscall_no; |
| 1043 | switch (syscall_no) { |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 1044 | case __NR_read: |
| 1045 | /* In order to catch timeout events on fds which are |
| 1046 | readable and which have been ioctl(TCSETA)'d with a |
| 1047 | VTIMEout, we appear to need to ask if the fd is |
| 1048 | writable, for some reason. Ask me not why. Since this |
| 1049 | is strange and potentially troublesome we only do it if |
| 1050 | the user asks specially. */ |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 1051 | if (VG_(strstr)(VG_(clo_weird_hacks), "ioctl-VTIME") != NULL) |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 1052 | VKI_FD_SET(fd, &writefds); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1053 | VKI_FD_SET(fd, &readfds); break; |
| 1054 | case __NR_write: |
| 1055 | VKI_FD_SET(fd, &writefds); break; |
| 1056 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1057 | VG_(core_panic)("poll_for_ready_fds: unexpected syscall"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1058 | /*NOTREACHED*/ |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | |
sewardj | e462e20 | 2002-04-13 04:09:07 +0000 | [diff] [blame] | 1063 | /* Short cut: if no fds are waiting, give up now. */ |
| 1064 | if (fd_max == -1) |
| 1065 | return; |
| 1066 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1067 | /* BLOCK ALL SIGNALS. We don't want the complication of select() |
| 1068 | getting interrupted. */ |
| 1069 | VG_(block_all_host_signals)( &saved_procmask ); |
| 1070 | |
| 1071 | n_ready = VG_(select) |
| 1072 | ( fd_max+1, &readfds, &writefds, &exceptfds, &timeout); |
| 1073 | if (VG_(is_kerror)(n_ready)) { |
| 1074 | VG_(printf)("poll_for_ready_fds: select returned %d\n", n_ready); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1075 | VG_(core_panic)("poll_for_ready_fds: select failed?!"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1076 | /*NOTREACHED*/ |
| 1077 | } |
| 1078 | |
| 1079 | /* UNBLOCK ALL SIGNALS */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1080 | VG_(restore_all_host_signals)( &saved_procmask ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1081 | |
| 1082 | /* VG_(printf)("poll_for_io_completions: %d fs ready\n", n_ready); */ |
| 1083 | |
| 1084 | if (n_ready == 0) |
| 1085 | return; |
| 1086 | |
| 1087 | /* Inspect all the fds we know about, and handle any completions that |
| 1088 | have happened. */ |
| 1089 | /* |
| 1090 | VG_(printf)("\n\n"); |
| 1091 | for (fd = 0; fd < 100; fd++) |
| 1092 | if (VKI_FD_ISSET(fd, &writefds) || VKI_FD_ISSET(fd, &readfds)) { |
| 1093 | VG_(printf)("X"); } else { VG_(printf)("."); }; |
| 1094 | VG_(printf)("\n\nfd_max = %d\n", fd_max); |
| 1095 | */ |
| 1096 | |
| 1097 | for (fd = 0; fd <= fd_max; fd++) { |
| 1098 | rd_ok = VKI_FD_ISSET(fd, &readfds); |
| 1099 | wr_ok = VKI_FD_ISSET(fd, &writefds); |
| 1100 | ex_ok = VKI_FD_ISSET(fd, &exceptfds); |
| 1101 | |
| 1102 | n_ok = (rd_ok ? 1 : 0) + (wr_ok ? 1 : 0) + (ex_ok ? 1 : 0); |
| 1103 | if (n_ok == 0) |
| 1104 | continue; |
| 1105 | if (n_ok > 1) { |
| 1106 | VG_(printf)("offending fd = %d\n", fd); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1107 | VG_(core_panic)("poll_for_ready_fds: multiple events on fd"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1108 | } |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 1109 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1110 | /* An I/O event completed for fd. Find the thread which |
| 1111 | requested this. */ |
| 1112 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1113 | if (vg_waiting_fds[i].fd == -1 /* not in use */) |
| 1114 | continue; |
| 1115 | if (vg_waiting_fds[i].fd == fd) |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | /* And a bit more paranoia ... */ |
| 1120 | vg_assert(i >= 0 && i < VG_N_WAITING_FDS); |
| 1121 | |
| 1122 | /* Mark the fd as ready. */ |
| 1123 | vg_assert(! vg_waiting_fds[i].ready); |
| 1124 | vg_waiting_fds[i].ready = True; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | /* See comment attached to poll_for_ready_fds() for explaination. */ |
sewardj | 9a199dc | 2002-04-14 13:01:38 +0000 | [diff] [blame] | 1130 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1131 | void complete_blocked_syscalls ( void ) |
| 1132 | { |
| 1133 | Int fd, i, res, syscall_no; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1134 | void* pre_res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1135 | ThreadId tid; |
| 1136 | Char msg_buf[100]; |
| 1137 | |
| 1138 | /* Inspect all the outstanding fds we know about. */ |
| 1139 | |
| 1140 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1141 | if (vg_waiting_fds[i].fd == -1 /* not in use */) |
| 1142 | continue; |
| 1143 | if (! vg_waiting_fds[i].ready) |
| 1144 | continue; |
| 1145 | |
| 1146 | fd = vg_waiting_fds[i].fd; |
| 1147 | tid = vg_waiting_fds[i].tid; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1148 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* The thread actually has to be waiting for the I/O event it |
| 1151 | requested before we can deliver the result! */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1152 | if (VG_(threads)[tid].status != VgTs_WaitFD) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1153 | continue; |
| 1154 | |
| 1155 | /* Ok, actually do it! We can safely use %EAX as the syscall |
| 1156 | number, because the speculative call made by |
| 1157 | sched_do_syscall() doesn't change %EAX in the case where the |
| 1158 | call would have blocked. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1159 | syscall_no = vg_waiting_fds[i].syscall_no; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1160 | vg_assert(syscall_no == VG_(threads)[tid].m_eax); |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 1161 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1162 | pre_res = vg_waiting_fds[i].pre_result; |
| 1163 | |
sewardj | bc7d878 | 2002-06-30 12:44:54 +0000 | [diff] [blame] | 1164 | /* In a rare case pertaining to writing into a pipe, write() |
| 1165 | will block when asked to write > 4096 bytes even though the |
| 1166 | kernel claims, when asked via select(), that blocking will |
| 1167 | not occur for a write on that fd. This can cause deadlocks. |
| 1168 | An easy answer is to limit the size of the write to 4096 |
| 1169 | anyway and hope that the client program's logic can handle |
| 1170 | the short write. That shoulds dubious to me, so we don't do |
| 1171 | it by default. */ |
| 1172 | if (syscall_no == __NR_write |
| 1173 | && VG_(threads)[tid].m_edx /* arg3, count */ > 4096 |
| 1174 | && VG_(strstr)(VG_(clo_weird_hacks), "truncate-writes") != NULL) { |
| 1175 | /* VG_(printf)("truncate write from %d to 4096\n", |
| 1176 | VG_(threads)[tid].m_edx ); */ |
| 1177 | VG_(threads)[tid].m_edx = 4096; |
| 1178 | } |
| 1179 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1180 | KERNEL_DO_SYSCALL(tid,res); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1181 | VG_(post_known_blocking_syscall)(tid, syscall_no, pre_res, res); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1182 | |
| 1183 | /* Reschedule. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1184 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1185 | /* Mark slot as no longer in use. */ |
| 1186 | vg_waiting_fds[i].fd = -1; |
| 1187 | /* pp_sched_status(); */ |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1188 | if (VG_(clo_trace_sched)) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1189 | VG_(sprintf)(msg_buf,"resume due to I/O completion on fd %d", fd); |
| 1190 | print_sched_event(tid, msg_buf); |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | static |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1197 | void check_for_pthread_cond_timedwait ( void ) |
| 1198 | { |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1199 | Int i, now; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1200 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1201 | if (VG_(threads)[i].status != VgTs_WaitCV) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1202 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1203 | if (VG_(threads)[i].awaken_at == 0xFFFFFFFF /* no timeout */) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1204 | continue; |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1205 | now = VG_(read_millisecond_timer)(); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1206 | if (now >= VG_(threads)[i].awaken_at) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1207 | do_pthread_cond_timedwait_TIMEOUT(i); |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1208 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | static |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1214 | void nanosleep_for_a_while ( void ) |
| 1215 | { |
| 1216 | Int res; |
| 1217 | struct vki_timespec req; |
| 1218 | struct vki_timespec rem; |
| 1219 | req.tv_sec = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1220 | req.tv_nsec = 10 * 1000 * 1000; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1221 | res = VG_(nanosleep)( &req, &rem ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1222 | vg_assert(res == 0 /* ok */ || res == 1 /* interrupted by signal */); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | /* --------------------------------------------------------------------- |
| 1227 | The scheduler proper. |
| 1228 | ------------------------------------------------------------------ */ |
| 1229 | |
| 1230 | /* Run user-space threads until either |
| 1231 | * Deadlock occurs |
| 1232 | * One thread asks to shutdown Valgrind |
| 1233 | * The specified number of basic blocks has gone by. |
| 1234 | */ |
| 1235 | VgSchedReturnCode VG_(scheduler) ( void ) |
| 1236 | { |
| 1237 | ThreadId tid, tid_next; |
| 1238 | UInt trc; |
| 1239 | UInt dispatch_ctr_SAVED; |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1240 | Int done_this_time, n_in_bounded_wait; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1241 | Addr trans_addr; |
sewardj | 14e0342 | 2002-04-24 19:51:31 +0000 | [diff] [blame] | 1242 | Bool sigs_delivered; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1243 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1244 | /* Start with the root thread. tid in general indicates the |
| 1245 | currently runnable/just-finished-running thread. */ |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1246 | VG_(last_run_tid) = tid = 1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1247 | |
| 1248 | /* This is the top level scheduler loop. It falls into three |
| 1249 | phases. */ |
| 1250 | while (True) { |
| 1251 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1252 | /* ======================= Phase 0 of 3 ======================= |
| 1253 | Be paranoid. Always a good idea. */ |
sewardj | d7fd4d2 | 2002-04-24 01:57:27 +0000 | [diff] [blame] | 1254 | stage1: |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1255 | scheduler_sanity(); |
sewardj | 0c3b53f | 2002-05-01 01:58:35 +0000 | [diff] [blame] | 1256 | VG_(do_sanity_checks)( False ); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1257 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1258 | /* ======================= Phase 1 of 3 ======================= |
| 1259 | Handle I/O completions and signals. This may change the |
| 1260 | status of various threads. Then select a new thread to run, |
| 1261 | or declare deadlock, or sleep if there are no runnable |
| 1262 | threads but some are blocked on I/O. */ |
| 1263 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1264 | /* Was a debug-stop requested? */ |
| 1265 | if (VG_(bbs_to_go) == 0) |
| 1266 | goto debug_stop; |
| 1267 | |
| 1268 | /* Do the following loop until a runnable thread is found, or |
| 1269 | deadlock is detected. */ |
| 1270 | while (True) { |
| 1271 | |
| 1272 | /* For stats purposes only. */ |
| 1273 | VG_(num_scheduling_events_MAJOR) ++; |
| 1274 | |
| 1275 | /* See if any I/O operations which we were waiting for have |
| 1276 | completed, and, if so, make runnable the relevant waiting |
| 1277 | threads. */ |
| 1278 | poll_for_ready_fds(); |
| 1279 | complete_blocked_syscalls(); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1280 | check_for_pthread_cond_timedwait(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* See if there are any signals which need to be delivered. If |
| 1283 | so, choose thread(s) to deliver them to, and build signal |
| 1284 | delivery frames on those thread(s) stacks. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1285 | |
| 1286 | /* Be careful about delivering signals to a thread waiting |
| 1287 | for a mutex. In particular, when the handler is running, |
| 1288 | that thread is temporarily apparently-not-waiting for the |
| 1289 | mutex, so if it is unlocked by another thread whilst the |
| 1290 | handler is running, this thread is not informed. When the |
| 1291 | handler returns, the thread resumes waiting on the mutex, |
| 1292 | even if, as a result, it has missed the unlocking of it. |
| 1293 | Potential deadlock. This sounds all very strange, but the |
| 1294 | POSIX standard appears to require this behaviour. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1295 | sigs_delivered = VG_(deliver_signals)(); |
sewardj | 14e0342 | 2002-04-24 19:51:31 +0000 | [diff] [blame] | 1296 | if (sigs_delivered) |
sewardj | 0c3b53f | 2002-05-01 01:58:35 +0000 | [diff] [blame] | 1297 | VG_(do_sanity_checks)( False ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1298 | |
| 1299 | /* Try and find a thread (tid) to run. */ |
| 1300 | tid_next = tid; |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1301 | n_in_bounded_wait = 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1302 | while (True) { |
| 1303 | tid_next++; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1304 | if (tid_next >= VG_N_THREADS) tid_next = 1; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1305 | if (VG_(threads)[tid_next].status == VgTs_WaitFD |
| 1306 | || VG_(threads)[tid_next].status == VgTs_Sleeping |
| 1307 | || VG_(threads)[tid_next].status == VgTs_WaitSIG |
| 1308 | || (VG_(threads)[tid_next].status == VgTs_WaitCV |
| 1309 | && VG_(threads)[tid_next].awaken_at != 0xFFFFFFFF)) |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1310 | n_in_bounded_wait ++; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1311 | if (VG_(threads)[tid_next].status == VgTs_Runnable) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1312 | break; /* We can run this one. */ |
| 1313 | if (tid_next == tid) |
| 1314 | break; /* been all the way round */ |
| 1315 | } |
| 1316 | tid = tid_next; |
| 1317 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1318 | if (VG_(threads)[tid].status == VgTs_Runnable) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1319 | /* Found a suitable candidate. Fall out of this loop, so |
| 1320 | we can advance to stage 2 of the scheduler: actually |
| 1321 | running the thread. */ |
| 1322 | break; |
| 1323 | } |
| 1324 | |
| 1325 | /* We didn't find a runnable thread. Now what? */ |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1326 | if (n_in_bounded_wait == 0) { |
sewardj | 54cacf0 | 2002-04-12 23:24:59 +0000 | [diff] [blame] | 1327 | /* No runnable threads and no prospect of any appearing |
| 1328 | even if we wait for an arbitrary length of time. In |
| 1329 | short, we have a deadlock. */ |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 1330 | VG_(pp_sched_status)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1331 | return VgSrc_Deadlock; |
| 1332 | } |
| 1333 | |
| 1334 | /* At least one thread is in a fd-wait state. Delay for a |
| 1335 | while, and go round again, in the hope that eventually a |
| 1336 | thread becomes runnable. */ |
| 1337 | nanosleep_for_a_while(); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1338 | /* pp_sched_status(); */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1339 | /* VG_(printf)("."); */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | /* ======================= Phase 2 of 3 ======================= |
| 1344 | Wahey! We've finally decided that thread tid is runnable, so |
| 1345 | we now do that. Run it for as much of a quanta as possible. |
| 1346 | Trivial requests are handled and the thread continues. The |
| 1347 | aim is not to do too many of Phase 1 since it is expensive. */ |
| 1348 | |
| 1349 | if (0) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1350 | VG_(printf)("SCHED: tid %d\n", tid); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1351 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1352 | VG_TRACK( thread_run, tid ); |
| 1353 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1354 | /* Figure out how many bbs to ask vg_run_innerloop to do. Note |
| 1355 | that it decrements the counter before testing it for zero, so |
| 1356 | that if VG_(dispatch_ctr) is set to N you get at most N-1 |
| 1357 | iterations. Also this means that VG_(dispatch_ctr) must |
| 1358 | exceed zero before entering the innerloop. Also also, the |
| 1359 | decrement is done before the bb is actually run, so you |
| 1360 | always get at least one decrement even if nothing happens. |
| 1361 | */ |
| 1362 | if (VG_(bbs_to_go) >= VG_SCHEDULING_QUANTUM) |
| 1363 | VG_(dispatch_ctr) = VG_SCHEDULING_QUANTUM + 1; |
| 1364 | else |
| 1365 | VG_(dispatch_ctr) = (UInt)VG_(bbs_to_go) + 1; |
| 1366 | |
| 1367 | /* ... and remember what we asked for. */ |
| 1368 | dispatch_ctr_SAVED = VG_(dispatch_ctr); |
| 1369 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1370 | /* paranoia ... */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1371 | vg_assert(VG_(threads)[tid].tid == tid); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1372 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1373 | /* Actually run thread tid. */ |
| 1374 | while (True) { |
| 1375 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1376 | VG_(last_run_tid) = tid; |
| 1377 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1378 | /* For stats purposes only. */ |
| 1379 | VG_(num_scheduling_events_MINOR) ++; |
| 1380 | |
| 1381 | if (0) |
| 1382 | VG_(message)(Vg_DebugMsg, "thread %d: running for %d bbs", |
| 1383 | tid, VG_(dispatch_ctr) - 1 ); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1384 | # if 0 |
| 1385 | if (VG_(bbs_done) > 31700000 + 0) { |
| 1386 | dispatch_ctr_SAVED = VG_(dispatch_ctr) = 2; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1387 | VG_(translate)(&VG_(threads)[tid], VG_(threads)[tid].m_eip, |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1388 | NULL,NULL,NULL); |
| 1389 | } |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1390 | vg_assert(VG_(threads)[tid].m_eip != 0); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1391 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1392 | |
| 1393 | trc = run_thread_for_a_while ( tid ); |
| 1394 | |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1395 | # if 0 |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1396 | if (0 == VG_(threads)[tid].m_eip) { |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1397 | VG_(printf)("tid = %d, dc = %llu\n", tid, VG_(bbs_done)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1398 | vg_assert(0 != VG_(threads)[tid].m_eip); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1399 | } |
| 1400 | # endif |
| 1401 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1402 | /* Deal quickly with trivial scheduling events, and resume the |
| 1403 | thread. */ |
| 1404 | |
| 1405 | if (trc == VG_TRC_INNER_FASTMISS) { |
| 1406 | vg_assert(VG_(dispatch_ctr) > 0); |
| 1407 | |
| 1408 | /* Trivial event. Miss in the fast-cache. Do a full |
| 1409 | lookup for it. */ |
| 1410 | trans_addr |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1411 | = VG_(search_transtab) ( VG_(threads)[tid].m_eip ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1412 | if (trans_addr == (Addr)0) { |
| 1413 | /* Not found; we need to request a translation. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1414 | create_translation_for( |
| 1415 | tid, VG_(threads)[tid].m_eip ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1416 | trans_addr = VG_(search_transtab) ( VG_(threads)[tid].m_eip ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1417 | if (trans_addr == (Addr)0) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1418 | VG_(core_panic)("VG_TRC_INNER_FASTMISS: missing tt_fast entry"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1419 | } |
| 1420 | continue; /* with this thread */ |
| 1421 | } |
| 1422 | |
| 1423 | if (trc == VG_TRC_EBP_JMP_CLIENTREQ) { |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 1424 | UInt reqno = *(UInt*)(VG_(threads)[tid].m_eax); |
| 1425 | /* VG_(printf)("request 0x%x\n", reqno); */ |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 1426 | |
| 1427 | /* Are we really absolutely totally quitting? */ |
| 1428 | if (reqno == VG_USERREQ__LIBC_FREERES_DONE) { |
| 1429 | if (0 || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 1430 | VG_(message)(Vg_DebugMsg, |
| 1431 | "__libc_freeres() done; really quitting!"); |
| 1432 | } |
| 1433 | return VgSrc_ExitSyscall; |
| 1434 | } |
| 1435 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1436 | do_client_request(tid); |
| 1437 | /* Following the request, we try and continue with the |
| 1438 | same thread if still runnable. If not, go back to |
| 1439 | Stage 1 to select a new thread to run. */ |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 1440 | if (VG_(threads)[tid].status == VgTs_Runnable |
| 1441 | && reqno != VG_USERREQ__PTHREAD_YIELD) |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1442 | continue; /* with this thread */ |
| 1443 | else |
| 1444 | goto stage1; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1447 | if (trc == VG_TRC_EBP_JMP_SYSCALL) { |
| 1448 | /* Do a syscall for the vthread tid. This could cause it |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1449 | to become non-runnable. One special case: spot the |
| 1450 | client doing calls to exit() and take this as the cue |
| 1451 | to exit. */ |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1452 | # if 0 |
| 1453 | { UInt* esp; Int i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1454 | esp=(UInt*)VG_(threads)[tid].m_esp; |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1455 | VG_(printf)("\nBEFORE\n"); |
| 1456 | for (i = 10; i >= -10; i--) |
| 1457 | VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]); |
| 1458 | } |
| 1459 | # endif |
| 1460 | |
sewardj | 1fe7b00 | 2002-07-16 01:43:15 +0000 | [diff] [blame] | 1461 | /* Deal with calling __libc_freeres() at exit. When the |
| 1462 | client does __NR_exit, it's exiting for good. So we |
| 1463 | then run VG_(__libc_freeres_wrapper). That quits by |
| 1464 | doing VG_USERREQ__LIBC_FREERES_DONE, and at that point |
| 1465 | we really exit. To be safe we nuke all other threads |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 1466 | currently running. |
| 1467 | |
| 1468 | If not valgrinding (cachegrinding, etc) don't do this. |
| 1469 | __libc_freeres does some invalid frees which crash |
| 1470 | the unprotected malloc/free system. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1471 | |
sewardj | f3fb92d | 2003-02-23 03:26:08 +0000 | [diff] [blame] | 1472 | if (VG_(threads)[tid].m_eax == __NR_exit |
| 1473 | # if defined(__NR_exit_group) |
| 1474 | || VG_(threads)[tid].m_eax == __NR_exit_group |
| 1475 | # endif |
| 1476 | ) { |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1477 | |
| 1478 | /* If __NR_exit, remember the supplied argument. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1479 | VG_(exitcode) = VG_(threads)[tid].m_ebx; /* syscall arg1 */ |
| 1480 | |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1481 | /* Only run __libc_freeres if the skin says it's ok and |
| 1482 | it hasn't been overridden with --run-libc-freeres=no |
| 1483 | on the command line. */ |
| 1484 | |
| 1485 | if (VG_(needs).libc_freeres && VG_(clo_run_libc_freeres)) { |
| 1486 | |
sewardj | 0063189 | 2002-10-05 15:34:38 +0000 | [diff] [blame] | 1487 | if (VG_(clo_verbosity) > 2 |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1488 | || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 1489 | VG_(message)(Vg_DebugMsg, |
| 1490 | "Caught __NR_exit; running __libc_freeres()"); |
| 1491 | } |
| 1492 | VG_(nuke_all_threads_except) ( tid ); |
| 1493 | VG_(threads)[tid].m_eip = (UInt)(&VG_(__libc_freeres_wrapper)); |
| 1494 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 1495 | goto stage1; /* party on, dudes (but not for much longer :) */ |
| 1496 | |
| 1497 | } else { |
| 1498 | /* We won't run __libc_freeres; just exit now. */ |
sewardj | 0063189 | 2002-10-05 15:34:38 +0000 | [diff] [blame] | 1499 | if (VG_(clo_verbosity) > 2 |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1500 | || VG_(clo_trace_syscalls) || VG_(clo_trace_sched)) { |
| 1501 | VG_(message)(Vg_DebugMsg, |
| 1502 | "Caught __NR_exit; quitting"); |
| 1503 | } |
| 1504 | return VgSrc_ExitSyscall; |
| 1505 | } |
| 1506 | |
sewardj | ade9d0d | 2002-07-26 10:52:48 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1509 | /* We've dealt with __NR_exit at this point. */ |
sewardj | f3fb92d | 2003-02-23 03:26:08 +0000 | [diff] [blame] | 1510 | { Bool b |
| 1511 | = VG_(threads)[tid].m_eax != __NR_exit |
| 1512 | # if defined(__NR_exit_group) |
| 1513 | && VG_(threads)[tid].m_eax != __NR_exit_group |
| 1514 | # endif |
| 1515 | ; |
| 1516 | vg_assert(b); |
| 1517 | } |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1518 | |
sewardj | 83798bf | 2002-05-24 00:11:16 +0000 | [diff] [blame] | 1519 | /* Trap syscalls to __NR_sched_yield and just have this |
| 1520 | thread yield instead. Not essential, just an |
| 1521 | optimisation. */ |
| 1522 | if (VG_(threads)[tid].m_eax == __NR_sched_yield) { |
| 1523 | SET_EAX(tid, 0); /* syscall returns with success */ |
| 1524 | goto stage1; /* find a new thread to run */ |
| 1525 | } |
| 1526 | |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1527 | sched_do_syscall(tid); |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1528 | |
| 1529 | # if 0 |
| 1530 | { UInt* esp; Int i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1531 | esp=(UInt*)VG_(threads)[tid].m_esp; |
sewardj | b3eef6b | 2002-05-01 00:05:27 +0000 | [diff] [blame] | 1532 | VG_(printf)("AFTER\n"); |
| 1533 | for (i = 10; i >= -10; i--) |
| 1534 | VG_(printf)("%2d %p = 0x%x\n", i, &esp[i], esp[i]); |
| 1535 | } |
| 1536 | # endif |
| 1537 | |
sewardj | 77f0fc1 | 2002-07-12 01:23:03 +0000 | [diff] [blame] | 1538 | if (VG_(threads)[tid].status == VgTs_Runnable) { |
| 1539 | /* Better do a signal check, since if in a tight loop |
| 1540 | with a slow syscall it may be a very long time |
| 1541 | before we get back to the main signal check in Stage 1. */ |
| 1542 | sigs_delivered = VG_(deliver_signals)(); |
| 1543 | if (sigs_delivered) |
| 1544 | VG_(do_sanity_checks)( False ); |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1545 | continue; /* with this thread */ |
sewardj | 77f0fc1 | 2002-07-12 01:23:03 +0000 | [diff] [blame] | 1546 | } else { |
| 1547 | goto stage1; |
| 1548 | } |
sewardj | 51c0aaf | 2002-04-25 01:32:10 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
sewardj | d7fd4d2 | 2002-04-24 01:57:27 +0000 | [diff] [blame] | 1551 | /* It's an event we can't quickly deal with. Give up running |
| 1552 | this thread and handle things the expensive way. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | } |
| 1555 | |
| 1556 | /* ======================= Phase 3 of 3 ======================= |
| 1557 | Handle non-trivial thread requests, mostly pthread stuff. */ |
| 1558 | |
| 1559 | /* Ok, we've fallen out of the dispatcher for a |
| 1560 | non-completely-trivial reason. First, update basic-block |
| 1561 | counters. */ |
| 1562 | |
| 1563 | done_this_time = (Int)dispatch_ctr_SAVED - (Int)VG_(dispatch_ctr) - 1; |
| 1564 | vg_assert(done_this_time >= 0); |
| 1565 | VG_(bbs_to_go) -= (ULong)done_this_time; |
| 1566 | VG_(bbs_done) += (ULong)done_this_time; |
| 1567 | |
| 1568 | if (0 && trc != VG_TRC_INNER_FASTMISS) |
| 1569 | VG_(message)(Vg_DebugMsg, "thread %d: completed %d bbs, trc %d", |
| 1570 | tid, done_this_time, (Int)trc ); |
| 1571 | |
| 1572 | if (0 && trc != VG_TRC_INNER_FASTMISS) |
| 1573 | VG_(message)(Vg_DebugMsg, "thread %d: %ld bbs, event %s", |
| 1574 | tid, VG_(bbs_done), |
| 1575 | name_of_sched_event(trc) ); |
sewardj | 9d1b5d3 | 2002-04-17 19:40:49 +0000 | [diff] [blame] | 1576 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1577 | /* Examine the thread's return code to figure out why it |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 1578 | stopped. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1579 | |
| 1580 | switch (trc) { |
| 1581 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1582 | case VG_TRC_INNER_COUNTERZERO: |
| 1583 | /* Timeslice is out. Let a new thread be scheduled, |
| 1584 | simply by doing nothing, causing us to arrive back at |
| 1585 | Phase 1. */ |
| 1586 | if (VG_(bbs_to_go) == 0) { |
| 1587 | goto debug_stop; |
| 1588 | } |
| 1589 | vg_assert(VG_(dispatch_ctr) == 0); |
| 1590 | break; |
| 1591 | |
| 1592 | case VG_TRC_UNRESUMABLE_SIGNAL: |
| 1593 | /* It got a SIGSEGV/SIGBUS, which we need to deliver right |
| 1594 | away. Again, do nothing, so we wind up back at Phase |
| 1595 | 1, whereupon the signal will be "delivered". */ |
| 1596 | break; |
| 1597 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1598 | default: |
| 1599 | VG_(printf)("\ntrc = %d\n", trc); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1600 | VG_(core_panic)("VG_(scheduler), phase 3: " |
| 1601 | "unexpected thread return code"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1602 | /* NOTREACHED */ |
| 1603 | break; |
| 1604 | |
| 1605 | } /* switch (trc) */ |
| 1606 | |
| 1607 | /* That completes Phase 3 of 3. Return now to the top of the |
| 1608 | main scheduler loop, to Phase 1 of 3. */ |
| 1609 | |
| 1610 | } /* top-level scheduler loop */ |
| 1611 | |
| 1612 | |
| 1613 | /* NOTREACHED */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1614 | VG_(core_panic)("scheduler: post-main-loop ?!"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1615 | /* NOTREACHED */ |
| 1616 | |
| 1617 | debug_stop: |
| 1618 | /* If we exited because of a debug stop, print the translation |
| 1619 | of the last block executed -- by translating it again, and |
| 1620 | throwing away the result. */ |
| 1621 | VG_(printf)( |
| 1622 | "======vvvvvvvv====== LAST TRANSLATION ======vvvvvvvv======\n"); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1623 | VG_(translate)( &VG_(threads)[tid], |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1624 | VG_(threads)[tid].m_eip, NULL, NULL, NULL, NULL ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1625 | VG_(printf)("\n"); |
| 1626 | VG_(printf)( |
| 1627 | "======^^^^^^^^====== LAST TRANSLATION ======^^^^^^^^======\n"); |
| 1628 | |
| 1629 | return VgSrc_BbsDone; |
| 1630 | } |
| 1631 | |
| 1632 | |
| 1633 | /* --------------------------------------------------------------------- |
| 1634 | The pthread implementation. |
| 1635 | ------------------------------------------------------------------ */ |
| 1636 | |
| 1637 | #include <pthread.h> |
| 1638 | #include <errno.h> |
| 1639 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 1640 | #define VG_PTHREAD_STACK_MIN \ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 1641 | (VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1642 | |
| 1643 | /* /usr/include/bits/pthreadtypes.h: |
| 1644 | typedef unsigned long int pthread_t; |
| 1645 | */ |
| 1646 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1647 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1648 | /* ----------------------------------------------------------- |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1649 | Thread CREATION, JOINAGE and CANCELLATION: HELPER FNS |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 1650 | -------------------------------------------------------- */ |
| 1651 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1652 | /* We've decided to action a cancellation on tid. Make it jump to |
| 1653 | thread_exit_wrapper() in vg_libpthread.c, passing PTHREAD_CANCELED |
| 1654 | as the arg. */ |
| 1655 | static |
| 1656 | void make_thread_jump_to_cancelhdlr ( ThreadId tid ) |
| 1657 | { |
| 1658 | Char msg_buf[100]; |
| 1659 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | dadc8d0 | 2002-12-08 23:24:18 +0000 | [diff] [blame] | 1660 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1661 | /* Push PTHREAD_CANCELED on the stack and jump to the cancellation |
| 1662 | handler -- which is really thread_exit_wrapper() in |
| 1663 | vg_libpthread.c. */ |
| 1664 | vg_assert(VG_(threads)[tid].cancel_pend != NULL); |
sewardj | 4bdd996 | 2002-12-26 11:51:50 +0000 | [diff] [blame] | 1665 | |
| 1666 | /* Push a suitable arg, and mark it as readable. */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1667 | VG_(threads)[tid].m_esp -= 4; |
| 1668 | * (UInt*)(VG_(threads)[tid].m_esp) = (UInt)PTHREAD_CANCELED; |
sewardj | 4bdd996 | 2002-12-26 11:51:50 +0000 | [diff] [blame] | 1669 | VG_TRACK( post_mem_write, VG_(threads)[tid].m_esp, sizeof(void*) ); |
| 1670 | |
| 1671 | /* Push a bogus return address. It will not return, but we still |
| 1672 | need to have it so that the arg is at the correct stack offset. |
| 1673 | Don't mark as readable; any attempt to read this is and internal |
| 1674 | valgrind bug since thread_exit_wrapper should not return. */ |
| 1675 | VG_(threads)[tid].m_esp -= 4; |
| 1676 | * (UInt*)(VG_(threads)[tid].m_esp) = 0xBEADDEEF; |
| 1677 | |
| 1678 | /* .cancel_pend will hold &thread_exit_wrapper */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1679 | VG_(threads)[tid].m_eip = (UInt)VG_(threads)[tid].cancel_pend; |
sewardj | dadc8d0 | 2002-12-08 23:24:18 +0000 | [diff] [blame] | 1680 | |
| 1681 | /* Clear out the waited-for-signals set, if needed, so as not to |
| 1682 | cause the sanity checker to bomb before |
| 1683 | cleanup_after_thread_exited() really cleans up properly for this |
| 1684 | thread. */ |
| 1685 | if (VG_(threads)[tid].status == VgTs_WaitSIG) { |
| 1686 | VG_(ksigemptyset)( & VG_(threads)[tid].sigs_waited_for ); |
| 1687 | } |
| 1688 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1689 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | dadc8d0 | 2002-12-08 23:24:18 +0000 | [diff] [blame] | 1690 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1691 | /* Make sure we aren't cancelled again whilst handling this |
| 1692 | cancellation. */ |
| 1693 | VG_(threads)[tid].cancel_st = False; |
| 1694 | if (VG_(clo_trace_sched)) { |
| 1695 | VG_(sprintf)(msg_buf, |
| 1696 | "jump to cancellation handler (hdlr = %p)", |
| 1697 | VG_(threads)[tid].cancel_pend); |
| 1698 | print_sched_event(tid, msg_buf); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1704 | /* Release resources and generally clean up once a thread has finally |
| 1705 | disappeared. */ |
| 1706 | static |
| 1707 | void cleanup_after_thread_exited ( ThreadId tid ) |
| 1708 | { |
sewardj | 89f20fd | 2002-06-30 10:57:30 +0000 | [diff] [blame] | 1709 | Int i; |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 1710 | vki_ksigset_t irrelevant_sigmask; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1711 | vg_assert(VG_(is_valid_or_empty_tid)(tid)); |
| 1712 | vg_assert(VG_(threads)[tid].status == VgTs_Empty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1713 | /* Its stack is now off-limits */ |
| 1714 | VG_TRACK( die_mem_stack, VG_(threads)[tid].stack_base, |
| 1715 | VG_(threads)[tid].stack_size ); |
| 1716 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1717 | /* Forget about any pending signals directed specifically at this |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1718 | thread, and get rid of signal handlers specifically arranged for |
| 1719 | this thread. */ |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 1720 | VG_(block_all_host_signals)( &irrelevant_sigmask ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1721 | VG_(handle_SCSS_change)( False /* lazy update */ ); |
sewardj | 89f20fd | 2002-06-30 10:57:30 +0000 | [diff] [blame] | 1722 | |
| 1723 | /* Clean up the waiting_fd table */ |
| 1724 | for (i = 0; i < VG_N_WAITING_FDS; i++) { |
| 1725 | if (vg_waiting_fds[i].tid == tid) { |
| 1726 | vg_waiting_fds[i].fd = -1; /* not in use */ |
| 1727 | } |
| 1728 | } |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1729 | |
| 1730 | /* Deallocate its LDT, if it ever had one. */ |
| 1731 | VG_(deallocate_LDT_for_thread)( VG_(threads)[tid].ldt ); |
| 1732 | VG_(threads)[tid].ldt = NULL; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1736 | /* Look for matching pairs of threads waiting for joiners and threads |
| 1737 | waiting for joinees. For each such pair copy the return value of |
| 1738 | the joinee into the joiner, let the joiner resume and discard the |
| 1739 | joinee. */ |
| 1740 | static |
| 1741 | void maybe_rendezvous_joiners_and_joinees ( void ) |
| 1742 | { |
| 1743 | Char msg_buf[100]; |
| 1744 | void** thread_return; |
| 1745 | ThreadId jnr, jee; |
| 1746 | |
| 1747 | for (jnr = 1; jnr < VG_N_THREADS; jnr++) { |
| 1748 | if (VG_(threads)[jnr].status != VgTs_WaitJoinee) |
| 1749 | continue; |
| 1750 | jee = VG_(threads)[jnr].joiner_jee_tid; |
| 1751 | if (jee == VG_INVALID_THREADID) |
| 1752 | continue; |
| 1753 | vg_assert(VG_(is_valid_tid)(jee)); |
| 1754 | if (VG_(threads)[jee].status != VgTs_WaitJoiner) |
| 1755 | continue; |
| 1756 | /* ok! jnr is waiting to join with jee, and jee is waiting to be |
| 1757 | joined by ... well, any thread. So let's do it! */ |
| 1758 | |
| 1759 | /* Copy return value to where joiner wants it. */ |
| 1760 | thread_return = VG_(threads)[jnr].joiner_thread_return; |
| 1761 | if (thread_return != NULL) { |
| 1762 | /* CHECK thread_return writable */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1763 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[jnr], |
| 1764 | "pthread_join: thread_return", |
| 1765 | (Addr)thread_return, sizeof(void*)); |
sewardj | 5a3798b | 2002-06-04 23:24:22 +0000 | [diff] [blame] | 1766 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1767 | *thread_return = VG_(threads)[jee].joinee_retval; |
| 1768 | /* Not really right, since it makes the thread's return value |
| 1769 | appear to be defined even if it isn't. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1770 | VG_TRACK( post_mem_write, (Addr)thread_return, sizeof(void*) ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | /* Joinee is discarded */ |
| 1774 | VG_(threads)[jee].status = VgTs_Empty; /* bye! */ |
| 1775 | cleanup_after_thread_exited ( jee ); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 1776 | if (VG_(clo_trace_sched)) { |
| 1777 | VG_(sprintf)(msg_buf, |
| 1778 | "rendezvous with joinee %d. %d resumes, %d exits.", |
| 1779 | jee, jnr, jee ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1780 | print_sched_event(jnr, msg_buf); |
| 1781 | } |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 1782 | |
| 1783 | VG_TRACK( post_thread_join, jnr, jee ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1784 | |
| 1785 | /* joiner returns with success */ |
| 1786 | VG_(threads)[jnr].status = VgTs_Runnable; |
| 1787 | SET_EDX(jnr, 0); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1792 | /* Nuke all threads other than tid. POSIX specifies that this should |
| 1793 | happen in __NR_exec, and after a __NR_fork() when I am the child, |
| 1794 | as POSIX requires. */ |
| 1795 | void VG_(nuke_all_threads_except) ( ThreadId me ) |
| 1796 | { |
| 1797 | ThreadId tid; |
| 1798 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 1799 | if (tid == me |
| 1800 | || VG_(threads)[tid].status == VgTs_Empty) |
| 1801 | continue; |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1802 | if (0) |
| 1803 | VG_(printf)( |
| 1804 | "VG_(nuke_all_threads_except): nuking tid %d\n", tid); |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1805 | VG_(threads)[tid].status = VgTs_Empty; |
| 1806 | cleanup_after_thread_exited( tid ); |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1811 | /* ----------------------------------------------------------- |
| 1812 | Thread CREATION, JOINAGE and CANCELLATION: REQUESTS |
| 1813 | -------------------------------------------------------- */ |
| 1814 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1815 | static |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1816 | void do__cleanup_push ( ThreadId tid, CleanupEntry* cu ) |
| 1817 | { |
| 1818 | Int sp; |
| 1819 | Char msg_buf[100]; |
| 1820 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1821 | sp = VG_(threads)[tid].custack_used; |
| 1822 | if (VG_(clo_trace_sched)) { |
| 1823 | VG_(sprintf)(msg_buf, |
| 1824 | "cleanup_push (fn %p, arg %p) -> slot %d", |
| 1825 | cu->fn, cu->arg, sp); |
| 1826 | print_sched_event(tid, msg_buf); |
| 1827 | } |
| 1828 | vg_assert(sp >= 0 && sp <= VG_N_CLEANUPSTACK); |
| 1829 | if (sp == VG_N_CLEANUPSTACK) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1830 | VG_(core_panic)("do__cleanup_push: VG_N_CLEANUPSTACK is too small." |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1831 | " Increase and recompile."); |
| 1832 | VG_(threads)[tid].custack[sp] = *cu; |
| 1833 | sp++; |
| 1834 | VG_(threads)[tid].custack_used = sp; |
| 1835 | SET_EDX(tid, 0); |
| 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | static |
| 1840 | void do__cleanup_pop ( ThreadId tid, CleanupEntry* cu ) |
| 1841 | { |
| 1842 | Int sp; |
| 1843 | Char msg_buf[100]; |
| 1844 | vg_assert(VG_(is_valid_tid)(tid)); |
| 1845 | sp = VG_(threads)[tid].custack_used; |
| 1846 | if (VG_(clo_trace_sched)) { |
njn | 3665092 | 2002-10-04 09:18:09 +0000 | [diff] [blame] | 1847 | VG_(sprintf)(msg_buf, "cleanup_pop from slot %d", sp-1); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1848 | print_sched_event(tid, msg_buf); |
| 1849 | } |
| 1850 | vg_assert(sp >= 0 && sp <= VG_N_CLEANUPSTACK); |
| 1851 | if (sp == 0) { |
| 1852 | SET_EDX(tid, -1); |
| 1853 | return; |
| 1854 | } |
| 1855 | sp--; |
njn | 3665092 | 2002-10-04 09:18:09 +0000 | [diff] [blame] | 1856 | VG_TRACK( pre_mem_write, Vg_CorePThread, & VG_(threads)[tid], |
| 1857 | "cleanup pop", (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1858 | *cu = VG_(threads)[tid].custack[sp]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1859 | VG_TRACK( post_mem_write, (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 1860 | VG_(threads)[tid].custack_used = sp; |
| 1861 | SET_EDX(tid, 0); |
| 1862 | } |
| 1863 | |
| 1864 | |
| 1865 | static |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1866 | void do_pthread_yield ( ThreadId tid ) |
| 1867 | { |
| 1868 | Char msg_buf[100]; |
| 1869 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 1870 | if (VG_(clo_trace_sched)) { |
| 1871 | VG_(sprintf)(msg_buf, "yield"); |
| 1872 | print_sched_event(tid, msg_buf); |
| 1873 | } |
| 1874 | SET_EDX(tid, 0); |
| 1875 | } |
| 1876 | |
| 1877 | |
| 1878 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1879 | void do__testcancel ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1880 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1881 | Char msg_buf[100]; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1882 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1883 | if (VG_(clo_trace_sched)) { |
| 1884 | VG_(sprintf)(msg_buf, "testcancel"); |
| 1885 | print_sched_event(tid, msg_buf); |
| 1886 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1887 | if (/* is there a cancellation pending on this thread? */ |
| 1888 | VG_(threads)[tid].cancel_pend != NULL |
| 1889 | && /* is this thread accepting cancellations? */ |
| 1890 | VG_(threads)[tid].cancel_st) { |
| 1891 | /* Ok, let's do the cancellation. */ |
| 1892 | make_thread_jump_to_cancelhdlr ( tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1893 | } else { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1894 | /* No, we keep going. */ |
| 1895 | SET_EDX(tid, 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1896 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | |
| 1900 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1901 | void do__set_cancelstate ( ThreadId tid, Int state ) |
| 1902 | { |
| 1903 | Bool old_st; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1904 | Char msg_buf[100]; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1905 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1906 | if (VG_(clo_trace_sched)) { |
| 1907 | VG_(sprintf)(msg_buf, "set_cancelstate to %d (%s)", state, |
| 1908 | state==PTHREAD_CANCEL_ENABLE |
| 1909 | ? "ENABLE" |
| 1910 | : (state==PTHREAD_CANCEL_DISABLE ? "DISABLE" : "???")); |
| 1911 | print_sched_event(tid, msg_buf); |
| 1912 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1913 | old_st = VG_(threads)[tid].cancel_st; |
| 1914 | if (state == PTHREAD_CANCEL_ENABLE) { |
| 1915 | VG_(threads)[tid].cancel_st = True; |
| 1916 | } else |
| 1917 | if (state == PTHREAD_CANCEL_DISABLE) { |
| 1918 | VG_(threads)[tid].cancel_st = False; |
| 1919 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1920 | VG_(core_panic)("do__set_cancelstate"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1921 | } |
| 1922 | SET_EDX(tid, old_st ? PTHREAD_CANCEL_ENABLE |
| 1923 | : PTHREAD_CANCEL_DISABLE); |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | static |
| 1928 | void do__set_canceltype ( ThreadId tid, Int type ) |
| 1929 | { |
| 1930 | Bool old_ty; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1931 | Char msg_buf[100]; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1932 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1933 | if (VG_(clo_trace_sched)) { |
| 1934 | VG_(sprintf)(msg_buf, "set_canceltype to %d (%s)", type, |
| 1935 | type==PTHREAD_CANCEL_ASYNCHRONOUS |
| 1936 | ? "ASYNCHRONOUS" |
| 1937 | : (type==PTHREAD_CANCEL_DEFERRED ? "DEFERRED" : "???")); |
| 1938 | print_sched_event(tid, msg_buf); |
| 1939 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1940 | old_ty = VG_(threads)[tid].cancel_ty; |
| 1941 | if (type == PTHREAD_CANCEL_ASYNCHRONOUS) { |
| 1942 | VG_(threads)[tid].cancel_ty = False; |
| 1943 | } else |
| 1944 | if (type == PTHREAD_CANCEL_DEFERRED) { |
sewardj | af00b6d | 2002-05-29 23:30:28 +0000 | [diff] [blame] | 1945 | VG_(threads)[tid].cancel_ty = True; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1946 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1947 | VG_(core_panic)("do__set_canceltype"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1948 | } |
| 1949 | SET_EDX(tid, old_ty ? PTHREAD_CANCEL_DEFERRED |
| 1950 | : PTHREAD_CANCEL_ASYNCHRONOUS); |
| 1951 | } |
| 1952 | |
| 1953 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1954 | /* Set or get the detach state for thread det. */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1955 | static |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1956 | void do__set_or_get_detach ( ThreadId tid, |
| 1957 | Int what, ThreadId det ) |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1958 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1959 | ThreadId i; |
| 1960 | Char msg_buf[100]; |
| 1961 | /* VG_(printf)("do__set_or_get_detach tid %d what %d det %d\n", |
| 1962 | tid, what, det); */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1963 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1964 | if (VG_(clo_trace_sched)) { |
| 1965 | VG_(sprintf)(msg_buf, "set_or_get_detach %d (%s) for tid %d", what, |
| 1966 | what==0 ? "not-detached" : ( |
| 1967 | what==1 ? "detached" : ( |
| 1968 | what==2 ? "fetch old value" : "???")), |
| 1969 | det ); |
| 1970 | print_sched_event(tid, msg_buf); |
| 1971 | } |
| 1972 | |
| 1973 | if (!VG_(is_valid_tid)(det)) { |
| 1974 | SET_EDX(tid, -1); |
| 1975 | return; |
| 1976 | } |
| 1977 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1978 | switch (what) { |
| 1979 | case 2: /* get */ |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1980 | SET_EDX(tid, VG_(threads)[det].detached ? 1 : 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1981 | return; |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1982 | case 1: /* set detached. If someone is in a join-wait for det, |
| 1983 | do not detach. */ |
| 1984 | for (i = 1; i < VG_N_THREADS; i++) { |
| 1985 | if (VG_(threads)[i].status == VgTs_WaitJoinee |
| 1986 | && VG_(threads)[i].joiner_jee_tid == det) { |
| 1987 | SET_EDX(tid, 0); |
| 1988 | if (VG_(clo_trace_sched)) { |
| 1989 | VG_(sprintf)(msg_buf, |
njn | 9b6d34e | 2002-10-15 08:48:08 +0000 | [diff] [blame] | 1990 | "tid %d not detached because %d in join-wait for it", |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 1991 | det, i); |
| 1992 | print_sched_event(tid, msg_buf); |
| 1993 | } |
| 1994 | return; |
| 1995 | } |
| 1996 | } |
| 1997 | VG_(threads)[det].detached = True; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1998 | SET_EDX(tid, 0); |
| 1999 | return; |
| 2000 | case 0: /* set not detached */ |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2001 | VG_(threads)[det].detached = False; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2002 | SET_EDX(tid, 0); |
| 2003 | return; |
| 2004 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 2005 | VG_(core_panic)("do__set_or_get_detach"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | |
| 2010 | static |
| 2011 | void do__set_cancelpend ( ThreadId tid, |
| 2012 | ThreadId cee, |
| 2013 | void (*cancelpend_hdlr)(void*) ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2014 | { |
| 2015 | Char msg_buf[100]; |
| 2016 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2017 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2018 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 2019 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2020 | if (!VG_(is_valid_tid)(cee)) { |
| 2021 | if (VG_(clo_trace_sched)) { |
| 2022 | VG_(sprintf)(msg_buf, |
| 2023 | "set_cancelpend for invalid tid %d", cee); |
| 2024 | print_sched_event(tid, msg_buf); |
| 2025 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2026 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2027 | "pthread_cancel: target thread does not exist, or invalid"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2028 | SET_EDX(tid, -VKI_ESRCH); |
| 2029 | return; |
| 2030 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2031 | |
| 2032 | VG_(threads)[cee].cancel_pend = cancelpend_hdlr; |
| 2033 | |
| 2034 | if (VG_(clo_trace_sched)) { |
| 2035 | VG_(sprintf)(msg_buf, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2036 | "set_cancelpend (hdlr = %p, set by tid %d)", |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2037 | cancelpend_hdlr, tid); |
| 2038 | print_sched_event(cee, msg_buf); |
| 2039 | } |
| 2040 | |
| 2041 | /* Thread doing the cancelling returns with success. */ |
| 2042 | SET_EDX(tid, 0); |
| 2043 | |
| 2044 | /* Perhaps we can nuke the cancellee right now? */ |
| 2045 | do__testcancel(cee); |
| 2046 | } |
| 2047 | |
| 2048 | |
| 2049 | static |
| 2050 | void do_pthread_join ( ThreadId tid, |
| 2051 | ThreadId jee, void** thread_return ) |
| 2052 | { |
| 2053 | Char msg_buf[100]; |
| 2054 | ThreadId i; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2055 | /* jee, the joinee, is the thread specified as an arg in thread |
| 2056 | tid's call to pthread_join. So tid is the join-er. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2057 | vg_assert(VG_(is_valid_tid)(tid)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2058 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2059 | |
| 2060 | if (jee == tid) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2061 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2062 | "pthread_join: attempt to join to self"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2063 | SET_EDX(tid, EDEADLK); /* libc constant, not a kernel one */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2064 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2065 | return; |
| 2066 | } |
| 2067 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2068 | /* Flush any completed pairs, so as to make sure what we're looking |
| 2069 | at is up-to-date. */ |
| 2070 | maybe_rendezvous_joiners_and_joinees(); |
| 2071 | |
| 2072 | /* Is this a sane request? */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2073 | if (jee < 0 |
| 2074 | || jee >= VG_N_THREADS |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2075 | || VG_(threads)[jee].status == VgTs_Empty) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2076 | /* Invalid thread to join to. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2077 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2078 | "pthread_join: target thread does not exist, or invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2079 | SET_EDX(tid, EINVAL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2080 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2081 | return; |
| 2082 | } |
| 2083 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2084 | /* Is anyone else already in a join-wait for jee? */ |
| 2085 | for (i = 1; i < VG_N_THREADS; i++) { |
| 2086 | if (i == tid) continue; |
| 2087 | if (VG_(threads)[i].status == VgTs_WaitJoinee |
| 2088 | && VG_(threads)[i].joiner_jee_tid == jee) { |
| 2089 | /* Someone already did join on this thread */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2090 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2091 | "pthread_join: another thread already " |
| 2092 | "in join-wait for target thread"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2093 | SET_EDX(tid, EINVAL); |
| 2094 | VG_(threads)[tid].status = VgTs_Runnable; |
| 2095 | return; |
| 2096 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2099 | /* Mark this thread as waiting for the joinee. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2100 | VG_(threads)[tid].status = VgTs_WaitJoinee; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2101 | VG_(threads)[tid].joiner_thread_return = thread_return; |
| 2102 | VG_(threads)[tid].joiner_jee_tid = jee; |
| 2103 | |
| 2104 | /* Look for matching joiners and joinees and do the right thing. */ |
| 2105 | maybe_rendezvous_joiners_and_joinees(); |
| 2106 | |
| 2107 | /* Return value is irrelevant since this this thread becomes |
| 2108 | non-runnable. maybe_resume_joiner() will cause it to return the |
| 2109 | right value when it resumes. */ |
| 2110 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2111 | if (VG_(clo_trace_sched)) { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2112 | VG_(sprintf)(msg_buf, |
| 2113 | "wait for joinee %d (may already be ready)", jee); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2114 | print_sched_event(tid, msg_buf); |
| 2115 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2119 | /* ( void* ): calling thread waits for joiner and returns the void* to |
| 2120 | it. This is one of two ways in which a thread can finally exit -- |
| 2121 | the other is do__quit. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2122 | static |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2123 | void do__wait_joiner ( ThreadId tid, void* retval ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2124 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2125 | Char msg_buf[100]; |
| 2126 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2127 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 2128 | if (VG_(clo_trace_sched)) { |
| 2129 | VG_(sprintf)(msg_buf, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2130 | "do__wait_joiner(retval = %p) (non-detached thread exit)", retval); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2131 | print_sched_event(tid, msg_buf); |
| 2132 | } |
| 2133 | VG_(threads)[tid].status = VgTs_WaitJoiner; |
| 2134 | VG_(threads)[tid].joinee_retval = retval; |
| 2135 | maybe_rendezvous_joiners_and_joinees(); |
| 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | /* ( no-args ): calling thread disappears from the system forever. |
| 2140 | Reclaim resources. */ |
| 2141 | static |
| 2142 | void do__quit ( ThreadId tid ) |
| 2143 | { |
| 2144 | Char msg_buf[100]; |
| 2145 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2146 | vg_assert(VG_(threads)[tid].status == VgTs_Runnable); |
| 2147 | VG_(threads)[tid].status = VgTs_Empty; /* bye! */ |
| 2148 | cleanup_after_thread_exited ( tid ); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2149 | if (VG_(clo_trace_sched)) { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 2150 | VG_(sprintf)(msg_buf, "do__quit (detached thread exit)"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2151 | print_sched_event(tid, msg_buf); |
| 2152 | } |
| 2153 | /* Return value is irrelevant; this thread will not get |
| 2154 | rescheduled. */ |
| 2155 | } |
| 2156 | |
| 2157 | |
| 2158 | /* Should never be entered. If it is, will be on the simulated |
| 2159 | CPU. */ |
| 2160 | static |
| 2161 | void do__apply_in_new_thread_bogusRA ( void ) |
| 2162 | { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 2163 | VG_(core_panic)("do__apply_in_new_thread_bogusRA"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | /* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn |
| 2167 | MUST NOT return -- ever. Eventually it will do either __QUIT or |
| 2168 | __WAIT_JOINER. Return the child tid to the parent. */ |
| 2169 | static |
| 2170 | void do__apply_in_new_thread ( ThreadId parent_tid, |
| 2171 | void* (*fn)(void *), |
| 2172 | void* arg ) |
| 2173 | { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2174 | Addr new_stack; |
| 2175 | UInt new_stk_szb; |
| 2176 | ThreadId tid; |
| 2177 | Char msg_buf[100]; |
| 2178 | |
| 2179 | /* Paranoia ... */ |
| 2180 | vg_assert(sizeof(pthread_t) == sizeof(UInt)); |
| 2181 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2182 | vg_assert(VG_(threads)[parent_tid].status != VgTs_Empty); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2183 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2184 | tid = vg_alloc_ThreadState(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2185 | |
| 2186 | /* If we've created the main thread's tid, we're in deep trouble :) */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2187 | vg_assert(tid != 1); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2188 | vg_assert(VG_(is_valid_or_empty_tid)(tid)); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2189 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 2190 | /* do this early, before the child gets any memory writes */ |
| 2191 | VG_TRACK ( post_thread_create, parent_tid, tid ); |
| 2192 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2193 | /* Create new thread with default attrs: |
| 2194 | deferred cancellation, not detached |
| 2195 | */ |
| 2196 | mostly_clear_thread_record(tid); |
| 2197 | VG_(threads)[tid].status = VgTs_Runnable; |
| 2198 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2199 | /* Copy the parent's CPU state into the child's, in a roundabout |
| 2200 | way (via baseBlock). */ |
| 2201 | VG_(load_thread_state)(parent_tid); |
sewardj | ca340b3 | 2002-12-08 22:14:11 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* We inherit our parent's LDT. */ |
| 2204 | if (VG_(threads)[parent_tid].ldt == NULL) { |
| 2205 | /* We hope this is the common case. */ |
| 2206 | VG_(baseBlock)[VGOFF_(ldt)] = 0; |
| 2207 | } else { |
| 2208 | /* No luck .. we have to take a copy of the parent's. */ |
| 2209 | VG_(threads)[tid].ldt |
| 2210 | = VG_(allocate_LDT_for_thread)( VG_(threads)[parent_tid].ldt ); |
| 2211 | VG_(baseBlock)[VGOFF_(ldt)] = (UInt)VG_(threads)[tid].ldt; |
| 2212 | } |
| 2213 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2214 | VG_(save_thread_state)(tid); |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2215 | vg_tid_last_in_baseBlock = tid; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2216 | |
| 2217 | /* Consider allocating the child a stack, if the one it already has |
| 2218 | is inadequate. */ |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 2219 | new_stk_szb = VG_PTHREAD_STACK_MIN; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2220 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2221 | if (new_stk_szb > VG_(threads)[tid].stack_size) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2222 | /* Again, for good measure :) We definitely don't want to be |
| 2223 | allocating a stack for the main thread. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2224 | vg_assert(tid != 1); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2225 | /* for now, we don't handle the case of anything other than |
| 2226 | assigning it for the first time. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2227 | vg_assert(VG_(threads)[tid].stack_size == 0); |
| 2228 | vg_assert(VG_(threads)[tid].stack_base == (Addr)NULL); |
sewardj | e904795 | 2002-06-05 20:28:33 +0000 | [diff] [blame] | 2229 | new_stack = (Addr)VG_(get_memory_from_mmap)( new_stk_szb, |
| 2230 | "new thread stack" ); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2231 | VG_(threads)[tid].stack_base = new_stack; |
| 2232 | VG_(threads)[tid].stack_size = new_stk_szb; |
| 2233 | VG_(threads)[tid].stack_highest_word |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2234 | = new_stack + new_stk_szb |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2235 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB; /* -4 ??? */; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2236 | } |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2237 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2238 | /* Having got memory to hold the thread's stack: |
| 2239 | - set %esp as base + size |
| 2240 | - mark everything below %esp inaccessible |
| 2241 | - mark redzone at stack end inaccessible |
| 2242 | */ |
| 2243 | VG_(threads)[tid].m_esp = VG_(threads)[tid].stack_base |
| 2244 | + VG_(threads)[tid].stack_size |
| 2245 | - VG_AR_CLIENT_STACKBASE_REDZONE_SZB; |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 2246 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2247 | VG_TRACK ( die_mem_stack, VG_(threads)[tid].stack_base, |
| 2248 | + new_stk_szb - VG_AR_CLIENT_STACKBASE_REDZONE_SZB); |
| 2249 | VG_TRACK ( ban_mem_stack, VG_(threads)[tid].m_esp, |
| 2250 | VG_AR_CLIENT_STACKBASE_REDZONE_SZB ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2251 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2252 | /* push two args */ |
| 2253 | VG_(threads)[tid].m_esp -= 8; |
| 2254 | VG_TRACK ( new_mem_stack, (Addr)VG_(threads)[tid].m_esp, 2 * 4 ); |
| 2255 | VG_TRACK ( pre_mem_write, Vg_CorePThread, & VG_(threads)[tid], |
| 2256 | "new thread: stack", |
| 2257 | (Addr)VG_(threads)[tid].m_esp, 2 * 4 ); |
| 2258 | |
| 2259 | /* push arg and (bogus) return address */ |
| 2260 | * (UInt*)(VG_(threads)[tid].m_esp+4) = (UInt)arg; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2261 | * (UInt*)(VG_(threads)[tid].m_esp) |
| 2262 | = (UInt)&do__apply_in_new_thread_bogusRA; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2263 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2264 | VG_TRACK ( post_mem_write, VG_(threads)[tid].m_esp, 2 * 4 ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2265 | |
| 2266 | /* this is where we start */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2267 | VG_(threads)[tid].m_eip = (UInt)fn; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2268 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2269 | if (VG_(clo_trace_sched)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2270 | VG_(sprintf)(msg_buf, "new thread, created by %d", parent_tid ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2271 | print_sched_event(tid, msg_buf); |
| 2272 | } |
| 2273 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2274 | /* We inherit our parent's signal mask. */ |
| 2275 | VG_(threads)[tid].sig_mask = VG_(threads)[parent_tid].sig_mask; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2276 | VG_(ksigemptyset)(&VG_(threads)[tid].sigs_waited_for); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2277 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 2278 | /* return child's tid to parent */ |
| 2279 | SET_EDX(parent_tid, tid); /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2283 | /* ----------------------------------------------------------- |
| 2284 | MUTEXes |
| 2285 | -------------------------------------------------------- */ |
| 2286 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2287 | /* pthread_mutex_t is a struct with at 5 words: |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2288 | typedef struct |
| 2289 | { |
| 2290 | int __m_reserved; -- Reserved for future use |
| 2291 | int __m_count; -- Depth of recursive locking |
| 2292 | _pthread_descr __m_owner; -- Owner thread (if recursive or errcheck) |
| 2293 | int __m_kind; -- Mutex kind: fast, recursive or errcheck |
| 2294 | struct _pthread_fastlock __m_lock; -- Underlying fast lock |
| 2295 | } pthread_mutex_t; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2296 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2297 | #define PTHREAD_MUTEX_INITIALIZER \ |
| 2298 | {0, 0, 0, PTHREAD_MUTEX_TIMED_NP, __LOCK_INITIALIZER} |
| 2299 | # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \ |
| 2300 | {0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __LOCK_INITIALIZER} |
| 2301 | # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \ |
| 2302 | {0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, __LOCK_INITIALIZER} |
| 2303 | # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \ |
| 2304 | {0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, __LOCK_INITIALIZER} |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2305 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2306 | How we use it: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2307 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2308 | __m_kind never changes and indicates whether or not it is recursive. |
| 2309 | |
| 2310 | __m_count indicates the lock count; if 0, the mutex is not owned by |
| 2311 | anybody. |
| 2312 | |
| 2313 | __m_owner has a ThreadId value stuffed into it. We carefully arrange |
| 2314 | that ThreadId == 0 is invalid (VG_INVALID_THREADID), so that |
| 2315 | statically initialised mutexes correctly appear |
| 2316 | to belong to nobody. |
| 2317 | |
| 2318 | In summary, a not-in-use mutex is distinguised by having __m_owner |
| 2319 | == 0 (VG_INVALID_THREADID) and __m_count == 0 too. If one of those |
| 2320 | conditions holds, the other should too. |
| 2321 | |
| 2322 | There is no linked list of threads waiting for this mutex. Instead |
| 2323 | a thread in WaitMX state points at the mutex with its waited_on_mx |
| 2324 | field. This makes _unlock() inefficient, but simple to implement the |
| 2325 | right semantics viz-a-viz signals. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2326 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2327 | We don't have to deal with mutex initialisation; the client side |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2328 | deals with that for us. |
| 2329 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2330 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2331 | /* Helper fns ... */ |
| 2332 | static |
| 2333 | void release_one_thread_waiting_on_mutex ( pthread_mutex_t* mutex, |
| 2334 | Char* caller ) |
| 2335 | { |
| 2336 | Int i; |
| 2337 | Char msg_buf[100]; |
| 2338 | |
| 2339 | /* Find some arbitrary thread waiting on this mutex, and make it |
| 2340 | runnable. If none are waiting, mark the mutex as not held. */ |
| 2341 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2342 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2343 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2344 | if (VG_(threads)[i].status == VgTs_WaitMX |
| 2345 | && VG_(threads)[i].associated_mx == mutex) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2346 | break; |
| 2347 | } |
| 2348 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 2349 | VG_TRACK( post_mutex_unlock, (ThreadId)mutex->__m_owner, mutex ); |
| 2350 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2351 | vg_assert(i <= VG_N_THREADS); |
| 2352 | if (i == VG_N_THREADS) { |
| 2353 | /* Nobody else is waiting on it. */ |
| 2354 | mutex->__m_count = 0; |
| 2355 | mutex->__m_owner = VG_INVALID_THREADID; |
| 2356 | } else { |
| 2357 | /* Notionally transfer the hold to thread i, whose |
| 2358 | pthread_mutex_lock() call now returns with 0 (success). */ |
| 2359 | /* The .count is already == 1. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2360 | vg_assert(VG_(threads)[i].associated_mx == mutex); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2361 | mutex->__m_owner = (_pthread_descr)i; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2362 | VG_(threads)[i].status = VgTs_Runnable; |
| 2363 | VG_(threads)[i].associated_mx = NULL; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2364 | /* m_edx already holds pth_mx_lock() success (0) */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2365 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 2366 | VG_TRACK( post_mutex_lock, (ThreadId)i, mutex); |
| 2367 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2368 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2369 | VG_(sprintf)(msg_buf, "%s mx %p: RESUME", |
| 2370 | caller, mutex ); |
| 2371 | print_pthread_event(i, msg_buf); |
| 2372 | } |
| 2373 | } |
| 2374 | } |
| 2375 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2376 | |
| 2377 | static |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2378 | void do_pthread_mutex_lock( ThreadId tid, |
| 2379 | Bool is_trylock, |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2380 | pthread_mutex_t* mutex ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2381 | { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2382 | Char msg_buf[100]; |
| 2383 | Char* caller |
sewardj | 8ccc2be | 2002-05-10 20:26:37 +0000 | [diff] [blame] | 2384 | = is_trylock ? "pthread_mutex_trylock" |
| 2385 | : "pthread_mutex_lock "; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2386 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2387 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2388 | VG_(sprintf)(msg_buf, "%s mx %p ...", caller, mutex ); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2389 | print_pthread_event(tid, msg_buf); |
| 2390 | } |
| 2391 | |
| 2392 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2393 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2394 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2395 | |
| 2396 | /* POSIX doesn't mandate this, but for sanity ... */ |
| 2397 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2398 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2399 | "pthread_mutex_lock/trylock: mutex is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2400 | SET_EDX(tid, EINVAL); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2401 | return; |
| 2402 | } |
| 2403 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2404 | /* More paranoia ... */ |
| 2405 | switch (mutex->__m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2406 | # ifndef GLIBC_2_1 |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2407 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2408 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2409 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2410 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2411 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2412 | # endif |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2413 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2414 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2415 | if (mutex->__m_count >= 0) break; |
| 2416 | /* else fall thru */ |
| 2417 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2418 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2419 | "pthread_mutex_lock/trylock: mutex is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2420 | SET_EDX(tid, EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2421 | return; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2424 | if (mutex->__m_count > 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2425 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2426 | vg_assert(VG_(is_valid_tid)((ThreadId)mutex->__m_owner)); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2427 | |
| 2428 | /* Someone has it already. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2429 | if ((ThreadId)mutex->__m_owner == tid) { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2430 | /* It's locked -- by me! */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2431 | if (mutex->__m_kind == PTHREAD_MUTEX_RECURSIVE_NP) { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2432 | /* return 0 (success). */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2433 | mutex->__m_count++; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2434 | SET_EDX(tid, 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 2435 | if (0) |
| 2436 | VG_(printf)("!!!!!! tid %d, mx %p -> locked %d\n", |
| 2437 | tid, mutex, mutex->__m_count); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2438 | return; |
| 2439 | } else { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2440 | if (is_trylock) |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2441 | SET_EDX(tid, EBUSY); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2442 | else |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2443 | SET_EDX(tid, EDEADLK); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2444 | return; |
| 2445 | } |
| 2446 | } else { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2447 | /* Someone else has it; we have to wait. Mark ourselves |
| 2448 | thusly. */ |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 2449 | /* GUARD: __m_count > 0 && __m_owner is valid */ |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2450 | if (is_trylock) { |
| 2451 | /* caller is polling; so return immediately. */ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2452 | SET_EDX(tid, EBUSY); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2453 | } else { |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2454 | VG_TRACK ( pre_mutex_lock, tid, mutex ); |
| 2455 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2456 | VG_(threads)[tid].status = VgTs_WaitMX; |
| 2457 | VG_(threads)[tid].associated_mx = mutex; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2458 | SET_EDX(tid, 0); /* pth_mx_lock success value */ |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 2459 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2460 | VG_(sprintf)(msg_buf, "%s mx %p: BLOCK", |
| 2461 | caller, mutex ); |
| 2462 | print_pthread_event(tid, msg_buf); |
| 2463 | } |
| 2464 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2465 | return; |
| 2466 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2467 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2468 | } else { |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2469 | /* Nobody owns it. Sanity check ... */ |
| 2470 | vg_assert(mutex->__m_owner == VG_INVALID_THREADID); |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2471 | |
| 2472 | VG_TRACK ( pre_mutex_lock, tid, mutex ); |
| 2473 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2474 | /* We get it! [for the first time]. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2475 | mutex->__m_count = 1; |
| 2476 | mutex->__m_owner = (_pthread_descr)tid; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2477 | |
| 2478 | VG_TRACK( post_mutex_lock, tid, mutex); |
| 2479 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2480 | /* return 0 (success). */ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2481 | SET_EDX(tid, 0); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2482 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2483 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | |
| 2487 | static |
| 2488 | void do_pthread_mutex_unlock ( ThreadId tid, |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 2489 | pthread_mutex_t* mutex ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2490 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2491 | Char msg_buf[100]; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2492 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 2493 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2494 | VG_(sprintf)(msg_buf, "pthread_mutex_unlock mx %p ...", mutex ); |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 2495 | print_pthread_event(tid, msg_buf); |
| 2496 | } |
| 2497 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2498 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2499 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2500 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2501 | |
| 2502 | if (mutex == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2503 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2504 | "pthread_mutex_unlock: mutex is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2505 | SET_EDX(tid, EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2506 | return; |
| 2507 | } |
| 2508 | |
sewardj | d8acdf2 | 2002-11-13 21:57:52 +0000 | [diff] [blame] | 2509 | /* If this was locked before the dawn of time, pretend it was |
| 2510 | locked now so that it balances with unlocks */ |
| 2511 | if (mutex->__m_kind & VG_PTHREAD_PREHISTORY) { |
| 2512 | mutex->__m_kind &= ~VG_PTHREAD_PREHISTORY; |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2513 | VG_TRACK( pre_mutex_lock, (ThreadId)mutex->__m_owner, mutex ); |
sewardj | d8acdf2 | 2002-11-13 21:57:52 +0000 | [diff] [blame] | 2514 | VG_TRACK( post_mutex_lock, (ThreadId)mutex->__m_owner, mutex ); |
| 2515 | } |
| 2516 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2517 | /* More paranoia ... */ |
| 2518 | switch (mutex->__m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2519 | # ifndef GLIBC_2_1 |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2520 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2521 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2522 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2523 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2524 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2525 | # endif |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2526 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2527 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2528 | if (mutex->__m_count >= 0) break; |
| 2529 | /* else fall thru */ |
| 2530 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2531 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2532 | "pthread_mutex_unlock: mutex is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2533 | SET_EDX(tid, EINVAL); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2534 | return; |
| 2535 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2536 | |
| 2537 | /* Barf if we don't currently hold the mutex. */ |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2538 | if (mutex->__m_count == 0) { |
| 2539 | /* nobody holds it */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2540 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2541 | "pthread_mutex_unlock: mutex is not locked"); |
| 2542 | SET_EDX(tid, EPERM); |
| 2543 | return; |
| 2544 | } |
| 2545 | |
| 2546 | if ((ThreadId)mutex->__m_owner != tid) { |
| 2547 | /* we don't hold it */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2548 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2549 | "pthread_mutex_unlock: mutex is locked by a different thread"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2550 | SET_EDX(tid, EPERM); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2551 | return; |
| 2552 | } |
| 2553 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2554 | /* If it's a multiply-locked recursive mutex, just decrement the |
| 2555 | lock count and return. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2556 | if (mutex->__m_count > 1) { |
| 2557 | vg_assert(mutex->__m_kind == PTHREAD_MUTEX_RECURSIVE_NP); |
| 2558 | mutex->__m_count --; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2559 | SET_EDX(tid, 0); /* success */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2560 | return; |
| 2561 | } |
| 2562 | |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2563 | /* Now we're sure it is locked exactly once, and by the thread who |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2564 | is now doing an unlock on it. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 2565 | vg_assert(mutex->__m_count == 1); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2566 | vg_assert((ThreadId)mutex->__m_owner == tid); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 2567 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2568 | /* Release at max one thread waiting on this mutex. */ |
| 2569 | release_one_thread_waiting_on_mutex ( mutex, "pthread_mutex_lock" ); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2570 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2571 | /* Our (tid's) pth_unlock() returns with 0 (success). */ |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2572 | SET_EDX(tid, 0); /* Success. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2576 | /* ----------------------------------------------------------- |
| 2577 | CONDITION VARIABLES |
| 2578 | -------------------------------------------------------- */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2579 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2580 | /* The relevant native types are as follows: |
| 2581 | (copied from /usr/include/bits/pthreadtypes.h) |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2582 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2583 | -- Conditions (not abstract because of PTHREAD_COND_INITIALIZER |
| 2584 | typedef struct |
| 2585 | { |
| 2586 | struct _pthread_fastlock __c_lock; -- Protect against concurrent access |
| 2587 | _pthread_descr __c_waiting; -- Threads waiting on this condition |
| 2588 | } pthread_cond_t; |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2589 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2590 | -- Attribute for conditionally variables. |
| 2591 | typedef struct |
| 2592 | { |
| 2593 | int __dummy; |
| 2594 | } pthread_condattr_t; |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2595 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2596 | #define PTHREAD_COND_INITIALIZER {__LOCK_INITIALIZER, 0} |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2597 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2598 | We don't use any fields of pthread_cond_t for anything at all. |
| 2599 | Only the identity of the CVs is important. |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2600 | |
| 2601 | Linux pthreads supports no attributes on condition variables, so we |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2602 | don't need to think too hard there. */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 2603 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2604 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2605 | static |
| 2606 | void do_pthread_cond_timedwait_TIMEOUT ( ThreadId tid ) |
| 2607 | { |
| 2608 | Char msg_buf[100]; |
| 2609 | pthread_mutex_t* mx; |
| 2610 | pthread_cond_t* cv; |
| 2611 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2612 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2613 | && VG_(threads)[tid].status == VgTs_WaitCV |
| 2614 | && VG_(threads)[tid].awaken_at != 0xFFFFFFFF); |
| 2615 | mx = VG_(threads)[tid].associated_mx; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2616 | vg_assert(mx != NULL); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2617 | cv = VG_(threads)[tid].associated_cv; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2618 | vg_assert(cv != NULL); |
| 2619 | |
| 2620 | if (mx->__m_owner == VG_INVALID_THREADID) { |
| 2621 | /* Currently unheld; hand it out to thread tid. */ |
| 2622 | vg_assert(mx->__m_count == 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2623 | VG_(threads)[tid].status = VgTs_Runnable; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2624 | SET_EDX(tid, ETIMEDOUT); /* pthread_cond_wait return value */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2625 | VG_(threads)[tid].associated_cv = NULL; |
| 2626 | VG_(threads)[tid].associated_mx = NULL; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2627 | mx->__m_owner = (_pthread_descr)tid; |
| 2628 | mx->__m_count = 1; |
| 2629 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 2630 | VG_TRACK( post_mutex_lock, tid, mx ); |
| 2631 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2632 | if (VG_(clo_trace_pthread_level) >= 1) { |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2633 | VG_(sprintf)(msg_buf, |
| 2634 | "pthread_cond_timedwai cv %p: TIMEOUT with mx %p", |
| 2635 | cv, mx ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2636 | print_pthread_event(tid, msg_buf); |
| 2637 | } |
| 2638 | } else { |
| 2639 | /* Currently held. Make thread tid be blocked on it. */ |
| 2640 | vg_assert(mx->__m_count > 0); |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2641 | VG_TRACK( pre_mutex_lock, tid, mx ); |
| 2642 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2643 | VG_(threads)[tid].status = VgTs_WaitMX; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2644 | SET_EDX(tid, ETIMEDOUT); /* pthread_cond_wait return value */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2645 | VG_(threads)[tid].associated_cv = NULL; |
| 2646 | VG_(threads)[tid].associated_mx = mx; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2647 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2648 | VG_(sprintf)(msg_buf, |
| 2649 | "pthread_cond_timedwai cv %p: TIMEOUT -> BLOCK for mx %p", |
| 2650 | cv, mx ); |
| 2651 | print_pthread_event(tid, msg_buf); |
| 2652 | } |
| 2653 | |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2658 | static |
| 2659 | void release_N_threads_waiting_on_cond ( pthread_cond_t* cond, |
| 2660 | Int n_to_release, |
| 2661 | Char* caller ) |
| 2662 | { |
| 2663 | Int i; |
| 2664 | Char msg_buf[100]; |
| 2665 | pthread_mutex_t* mx; |
| 2666 | |
| 2667 | while (True) { |
| 2668 | if (n_to_release == 0) |
| 2669 | return; |
| 2670 | |
| 2671 | /* Find a thread waiting on this CV. */ |
| 2672 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2673 | if (VG_(threads)[i].status == VgTs_Empty) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2674 | continue; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2675 | if (VG_(threads)[i].status == VgTs_WaitCV |
| 2676 | && VG_(threads)[i].associated_cv == cond) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2677 | break; |
| 2678 | } |
| 2679 | vg_assert(i <= VG_N_THREADS); |
| 2680 | |
| 2681 | if (i == VG_N_THREADS) { |
| 2682 | /* Nobody else is waiting on it. */ |
| 2683 | return; |
| 2684 | } |
| 2685 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2686 | mx = VG_(threads)[i].associated_mx; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2687 | vg_assert(mx != NULL); |
| 2688 | |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2689 | VG_TRACK( pre_mutex_lock, i, mx ); |
| 2690 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2691 | if (mx->__m_owner == VG_INVALID_THREADID) { |
| 2692 | /* Currently unheld; hand it out to thread i. */ |
| 2693 | vg_assert(mx->__m_count == 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2694 | VG_(threads)[i].status = VgTs_Runnable; |
| 2695 | VG_(threads)[i].associated_cv = NULL; |
| 2696 | VG_(threads)[i].associated_mx = NULL; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2697 | mx->__m_owner = (_pthread_descr)i; |
| 2698 | mx->__m_count = 1; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2699 | /* .m_edx already holds pth_cond_wait success value (0) */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2700 | |
sewardj | 0af43bc | 2002-10-22 04:30:35 +0000 | [diff] [blame] | 2701 | VG_TRACK( post_mutex_lock, i, mx ); |
| 2702 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2703 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2704 | VG_(sprintf)(msg_buf, "%s cv %p: RESUME with mx %p", |
| 2705 | caller, cond, mx ); |
| 2706 | print_pthread_event(i, msg_buf); |
| 2707 | } |
| 2708 | |
| 2709 | } else { |
| 2710 | /* Currently held. Make thread i be blocked on it. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2711 | vg_assert(mx->__m_count > 0); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2712 | VG_(threads)[i].status = VgTs_WaitMX; |
| 2713 | VG_(threads)[i].associated_cv = NULL; |
| 2714 | VG_(threads)[i].associated_mx = mx; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2715 | SET_EDX(i, 0); /* pth_cond_wait success value */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2716 | |
| 2717 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2718 | VG_(sprintf)(msg_buf, "%s cv %p: BLOCK for mx %p", |
| 2719 | caller, cond, mx ); |
| 2720 | print_pthread_event(i, msg_buf); |
| 2721 | } |
| 2722 | |
| 2723 | } |
| 2724 | |
| 2725 | n_to_release--; |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | |
| 2730 | static |
| 2731 | void do_pthread_cond_wait ( ThreadId tid, |
| 2732 | pthread_cond_t *cond, |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2733 | pthread_mutex_t *mutex, |
| 2734 | UInt ms_end ) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2735 | { |
| 2736 | Char msg_buf[100]; |
| 2737 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2738 | /* If ms_end == 0xFFFFFFFF, wait forever (no timeout). Otherwise, |
| 2739 | ms_end is the ending millisecond. */ |
| 2740 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2741 | /* pre: mutex should be a valid mutex and owned by tid. */ |
| 2742 | if (VG_(clo_trace_pthread_level) >= 2) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2743 | VG_(sprintf)(msg_buf, "pthread_cond_wait cv %p, mx %p, end %d ...", |
| 2744 | cond, mutex, ms_end ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2745 | print_pthread_event(tid, msg_buf); |
| 2746 | } |
| 2747 | |
| 2748 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2749 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2750 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2751 | |
| 2752 | if (mutex == NULL || cond == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2753 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2754 | "pthread_cond_wait/timedwait: cond or mutex is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2755 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2756 | return; |
| 2757 | } |
| 2758 | |
| 2759 | /* More paranoia ... */ |
| 2760 | switch (mutex->__m_kind) { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2761 | # ifndef GLIBC_2_1 |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2762 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 2763 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 2764 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2765 | # ifdef GLIBC_2_1 |
sewardj | 8e651d7 | 2002-05-10 21:00:19 +0000 | [diff] [blame] | 2766 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 2767 | # endif |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2768 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 2769 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2770 | if (mutex->__m_count >= 0) break; |
| 2771 | /* else fall thru */ |
| 2772 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2773 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2774 | "pthread_cond_wait/timedwait: mutex is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2775 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2776 | return; |
| 2777 | } |
| 2778 | |
| 2779 | /* Barf if we don't currently hold the mutex. */ |
| 2780 | if (mutex->__m_count == 0 /* nobody holds it */ |
| 2781 | || (ThreadId)mutex->__m_owner != tid /* we don't hold it */) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2782 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2783 | "pthread_cond_wait/timedwait: mutex is unlocked " |
| 2784 | "or is locked but not owned by thread"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2785 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2786 | return; |
| 2787 | } |
| 2788 | |
| 2789 | /* Queue ourselves on the condition. */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2790 | VG_(threads)[tid].status = VgTs_WaitCV; |
| 2791 | VG_(threads)[tid].associated_cv = cond; |
| 2792 | VG_(threads)[tid].associated_mx = mutex; |
| 2793 | VG_(threads)[tid].awaken_at = ms_end; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2794 | |
| 2795 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2796 | VG_(sprintf)(msg_buf, |
| 2797 | "pthread_cond_wait cv %p, mx %p: BLOCK", |
| 2798 | cond, mutex ); |
| 2799 | print_pthread_event(tid, msg_buf); |
| 2800 | } |
| 2801 | |
| 2802 | /* Release the mutex. */ |
| 2803 | release_one_thread_waiting_on_mutex ( mutex, "pthread_cond_wait " ); |
| 2804 | } |
| 2805 | |
| 2806 | |
| 2807 | static |
| 2808 | void do_pthread_cond_signal_or_broadcast ( ThreadId tid, |
| 2809 | Bool broadcast, |
| 2810 | pthread_cond_t *cond ) |
| 2811 | { |
| 2812 | Char msg_buf[100]; |
| 2813 | Char* caller |
| 2814 | = broadcast ? "pthread_cond_broadcast" |
| 2815 | : "pthread_cond_signal "; |
| 2816 | |
| 2817 | if (VG_(clo_trace_pthread_level) >= 2) { |
| 2818 | VG_(sprintf)(msg_buf, "%s cv %p ...", |
| 2819 | caller, cond ); |
| 2820 | print_pthread_event(tid, msg_buf); |
| 2821 | } |
| 2822 | |
| 2823 | /* Paranoia ... */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2824 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2825 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2826 | |
| 2827 | if (cond == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2828 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2829 | "pthread_cond_signal/broadcast: cond is NULL"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2830 | SET_EDX(tid, EINVAL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2831 | return; |
| 2832 | } |
| 2833 | |
| 2834 | release_N_threads_waiting_on_cond ( |
| 2835 | cond, |
| 2836 | broadcast ? VG_N_THREADS : 1, |
| 2837 | caller |
| 2838 | ); |
| 2839 | |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2840 | SET_EDX(tid, 0); /* success */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 2843 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2844 | /* ----------------------------------------------------------- |
| 2845 | THREAD SPECIFIC DATA |
| 2846 | -------------------------------------------------------- */ |
| 2847 | |
| 2848 | static __inline__ |
| 2849 | Bool is_valid_key ( ThreadKey k ) |
| 2850 | { |
| 2851 | /* k unsigned; hence no < 0 check */ |
| 2852 | if (k >= VG_N_THREAD_KEYS) return False; |
| 2853 | if (!vg_thread_keys[k].inuse) return False; |
| 2854 | return True; |
| 2855 | } |
| 2856 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2857 | |
| 2858 | /* Return in %EDX a value of 1 if the key is valid, else 0. */ |
| 2859 | static |
| 2860 | void do_pthread_key_validate ( ThreadId tid, |
| 2861 | pthread_key_t key ) |
| 2862 | { |
| 2863 | Char msg_buf[100]; |
| 2864 | |
| 2865 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2866 | VG_(sprintf)(msg_buf, "pthread_key_validate key %p", |
| 2867 | key ); |
| 2868 | print_pthread_event(tid, msg_buf); |
| 2869 | } |
| 2870 | |
| 2871 | vg_assert(sizeof(pthread_key_t) == sizeof(ThreadKey)); |
| 2872 | vg_assert(VG_(is_valid_tid)(tid) |
| 2873 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 2874 | |
| 2875 | if (is_valid_key((ThreadKey)key)) { |
| 2876 | SET_EDX(tid, 1); |
| 2877 | } else { |
| 2878 | SET_EDX(tid, 0); |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2883 | static |
| 2884 | void do_pthread_key_create ( ThreadId tid, |
| 2885 | pthread_key_t* key, |
| 2886 | void (*destructor)(void*) ) |
| 2887 | { |
| 2888 | Int i; |
| 2889 | Char msg_buf[100]; |
| 2890 | |
| 2891 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2892 | VG_(sprintf)(msg_buf, "pthread_key_create *key %p, destr %p", |
| 2893 | key, destructor ); |
| 2894 | print_pthread_event(tid, msg_buf); |
| 2895 | } |
| 2896 | |
| 2897 | vg_assert(sizeof(pthread_key_t) == sizeof(ThreadKey)); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2898 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2899 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2900 | |
| 2901 | for (i = 0; i < VG_N_THREAD_KEYS; i++) |
| 2902 | if (!vg_thread_keys[i].inuse) |
| 2903 | break; |
| 2904 | |
| 2905 | if (i == VG_N_THREAD_KEYS) { |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2906 | /* SET_EDX(tid, EAGAIN); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2907 | return; |
| 2908 | */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 2909 | VG_(core_panic)("pthread_key_create: VG_N_THREAD_KEYS is too low;" |
| 2910 | " increase and recompile"); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 2913 | vg_thread_keys[i].inuse = True; |
| 2914 | vg_thread_keys[i].destructor = destructor; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2915 | |
sewardj | 5a3798b | 2002-06-04 23:24:22 +0000 | [diff] [blame] | 2916 | /* check key for addressibility */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2917 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[tid], |
| 2918 | "pthread_key_create: key", |
| 2919 | (Addr)key, sizeof(pthread_key_t)); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2920 | *key = i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2921 | VG_TRACK( post_mem_write, (Addr)key, sizeof(pthread_key_t) ); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2922 | |
| 2923 | SET_EDX(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | |
| 2927 | static |
| 2928 | void do_pthread_key_delete ( ThreadId tid, pthread_key_t key ) |
| 2929 | { |
| 2930 | Char msg_buf[100]; |
| 2931 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 2932 | VG_(sprintf)(msg_buf, "pthread_key_delete key %d", |
| 2933 | key ); |
| 2934 | print_pthread_event(tid, msg_buf); |
| 2935 | } |
| 2936 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2937 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2938 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2939 | |
| 2940 | if (!is_valid_key(key)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2941 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2942 | "pthread_key_delete: key is invalid"); |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2943 | SET_EDX(tid, EINVAL); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2944 | return; |
| 2945 | } |
| 2946 | |
| 2947 | vg_thread_keys[key].inuse = False; |
sewardj | 648b315 | 2002-12-09 19:07:59 +0000 | [diff] [blame] | 2948 | vg_thread_keys[key].destructor = NULL; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2949 | SET_EDX(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2953 | /* Get the .specific_ptr for a thread. Return 1 if the thread-slot |
| 2954 | isn't in use, so that client-space can scan all thread slots. 1 |
| 2955 | cannot be confused with NULL or a legitimately-aligned specific_ptr |
| 2956 | value. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2957 | static |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2958 | void do_pthread_getspecific_ptr ( ThreadId tid ) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2959 | { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2960 | void** specifics_ptr; |
| 2961 | Char msg_buf[100]; |
| 2962 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2963 | if (VG_(clo_trace_pthread_level) >= 1) { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2964 | VG_(sprintf)(msg_buf, "pthread_getspecific_ptr" ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2965 | print_pthread_event(tid, msg_buf); |
| 2966 | } |
| 2967 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2968 | vg_assert(VG_(is_valid_or_empty_tid)(tid)); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2969 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2970 | if (VG_(threads)[tid].status == VgTs_Empty) { |
| 2971 | SET_EDX(tid, 1); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2972 | return; |
| 2973 | } |
| 2974 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2975 | specifics_ptr = VG_(threads)[tid].specifics_ptr; |
| 2976 | vg_assert(specifics_ptr == NULL |
| 2977 | || IS_ALIGNED4_ADDR(specifics_ptr)); |
| 2978 | |
| 2979 | SET_EDX(tid, (UInt)specifics_ptr); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | |
| 2983 | static |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2984 | void do_pthread_setspecific_ptr ( ThreadId tid, void** ptr ) |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2985 | { |
| 2986 | Char msg_buf[100]; |
| 2987 | if (VG_(clo_trace_pthread_level) >= 1) { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2988 | VG_(sprintf)(msg_buf, "pthread_setspecific_ptr ptr %p", |
| 2989 | ptr ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2990 | print_pthread_event(tid, msg_buf); |
| 2991 | } |
| 2992 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 2993 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 2994 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2995 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 2996 | VG_(threads)[tid].specifics_ptr = ptr; |
sewardj | c3bd5f5 | 2002-05-01 03:24:23 +0000 | [diff] [blame] | 2997 | SET_EDX(tid, 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3001 | /* Helper for calling destructors at thread exit. If key is valid, |
| 3002 | copy the thread's specific value into cu->arg and put the *key*'s |
| 3003 | destructor fn address in cu->fn. Then return 0 to the caller. |
| 3004 | Otherwise return non-zero to the caller. */ |
| 3005 | static |
| 3006 | void do__get_key_destr_and_spec ( ThreadId tid, |
| 3007 | pthread_key_t key, |
| 3008 | CleanupEntry* cu ) |
| 3009 | { |
| 3010 | Char msg_buf[100]; |
| 3011 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 3012 | VG_(sprintf)(msg_buf, |
| 3013 | "get_key_destr_and_arg (key = %d)", key ); |
| 3014 | print_pthread_event(tid, msg_buf); |
| 3015 | } |
| 3016 | vg_assert(VG_(is_valid_tid)(tid)); |
| 3017 | vg_assert(key >= 0 && key < VG_N_THREAD_KEYS); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3018 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3019 | if (!vg_thread_keys[key].inuse) { |
| 3020 | SET_EDX(tid, -1); |
| 3021 | return; |
| 3022 | } |
njn | 3665092 | 2002-10-04 09:18:09 +0000 | [diff] [blame] | 3023 | VG_TRACK( pre_mem_write, Vg_CorePThread, & VG_(threads)[tid], |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3024 | "get_key_destr_and_spec: cu", (Addr)cu, |
njn | 3665092 | 2002-10-04 09:18:09 +0000 | [diff] [blame] | 3025 | sizeof(CleanupEntry) ); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3026 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3027 | cu->fn = vg_thread_keys[key].destructor; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3028 | if (VG_(threads)[tid].specifics_ptr == NULL) { |
| 3029 | cu->arg = NULL; |
| 3030 | } else { |
| 3031 | VG_TRACK( pre_mem_read, Vg_CorePThread, & VG_(threads)[tid], |
| 3032 | "get_key_destr_and_spec: key", |
| 3033 | (Addr)(&VG_(threads)[tid].specifics_ptr[key]), |
| 3034 | sizeof(void*) ); |
| 3035 | cu->arg = VG_(threads)[tid].specifics_ptr[key]; |
| 3036 | } |
| 3037 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3038 | VG_TRACK( post_mem_write, (Addr)cu, sizeof(CleanupEntry) ); |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3039 | SET_EDX(tid, 0); |
| 3040 | } |
| 3041 | |
| 3042 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3043 | /* --------------------------------------------------- |
| 3044 | SIGNALS |
| 3045 | ------------------------------------------------ */ |
| 3046 | |
| 3047 | /* See comment in vg_libthread.c:pthread_sigmask() regarding |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3048 | deliberate confusion of types sigset_t and vki_sigset_t. Return 0 |
| 3049 | for OK and 1 for some kind of addressing error, which the |
| 3050 | vg_libpthread.c routine turns into return values 0 and EFAULT |
| 3051 | respectively. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3052 | static |
| 3053 | void do_pthread_sigmask ( ThreadId tid, |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3054 | Int vki_how, |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3055 | vki_ksigset_t* newmask, |
| 3056 | vki_ksigset_t* oldmask ) |
| 3057 | { |
| 3058 | Char msg_buf[100]; |
| 3059 | if (VG_(clo_trace_pthread_level) >= 1) { |
| 3060 | VG_(sprintf)(msg_buf, |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3061 | "pthread_sigmask vki_how %d, newmask %p, oldmask %p", |
| 3062 | vki_how, newmask, oldmask ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3063 | print_pthread_event(tid, msg_buf); |
| 3064 | } |
| 3065 | |
| 3066 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3067 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3068 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3069 | if (newmask) |
| 3070 | VG_TRACK( pre_mem_read, Vg_CorePThread, &VG_(threads)[tid], |
| 3071 | "pthread_sigmask: newmask", |
| 3072 | (Addr)newmask, sizeof(vki_ksigset_t)); |
| 3073 | if (oldmask) |
| 3074 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[tid], |
| 3075 | "pthread_sigmask: oldmask", |
| 3076 | (Addr)oldmask, sizeof(vki_ksigset_t)); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3077 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3078 | VG_(do_pthread_sigmask_SCSS_upd) ( tid, vki_how, newmask, oldmask ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3079 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3080 | if (oldmask) |
| 3081 | VG_TRACK( post_mem_write, (Addr)oldmask, sizeof(vki_ksigset_t) ); |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 3082 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3083 | /* Success. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3084 | SET_EDX(tid, 0); |
| 3085 | } |
| 3086 | |
| 3087 | |
| 3088 | static |
| 3089 | void do_sigwait ( ThreadId tid, |
| 3090 | vki_ksigset_t* set, |
| 3091 | Int* sig ) |
| 3092 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3093 | vki_ksigset_t irrelevant_sigmask; |
| 3094 | Char msg_buf[100]; |
| 3095 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3096 | if (VG_(clo_trace_signals) || VG_(clo_trace_sched)) { |
| 3097 | VG_(sprintf)(msg_buf, |
| 3098 | "suspend due to sigwait(): set %p, sig %p", |
| 3099 | set, sig ); |
| 3100 | print_pthread_event(tid, msg_buf); |
| 3101 | } |
| 3102 | |
| 3103 | vg_assert(VG_(is_valid_tid)(tid) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3104 | && VG_(threads)[tid].status == VgTs_Runnable); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3105 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3106 | /* Change SCSS */ |
| 3107 | VG_(threads)[tid].sigs_waited_for = *set; |
| 3108 | VG_(threads)[tid].status = VgTs_WaitSIG; |
| 3109 | |
| 3110 | VG_(block_all_host_signals)( &irrelevant_sigmask ); |
| 3111 | VG_(handle_SCSS_change)( False /* lazy update */ ); |
| 3112 | } |
| 3113 | |
| 3114 | |
| 3115 | static |
| 3116 | void do_pthread_kill ( ThreadId tid, /* me */ |
| 3117 | ThreadId thread, /* thread to signal */ |
| 3118 | Int sig ) |
| 3119 | { |
| 3120 | Char msg_buf[100]; |
| 3121 | |
| 3122 | if (VG_(clo_trace_signals) || VG_(clo_trace_pthread_level) >= 1) { |
| 3123 | VG_(sprintf)(msg_buf, |
| 3124 | "pthread_kill thread %d, signo %d", |
| 3125 | thread, sig ); |
| 3126 | print_pthread_event(tid, msg_buf); |
| 3127 | } |
| 3128 | |
| 3129 | vg_assert(VG_(is_valid_tid)(tid) |
| 3130 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 3131 | |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3132 | if (!VG_(is_valid_tid)(thread)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3133 | VG_(record_pthread_error)( tid, |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3134 | "pthread_kill: invalid target thread"); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3135 | SET_EDX(tid, -VKI_ESRCH); |
| 3136 | return; |
| 3137 | } |
| 3138 | |
| 3139 | if (sig < 1 || sig > VKI_KNSIG) { |
| 3140 | SET_EDX(tid, -VKI_EINVAL); |
| 3141 | return; |
| 3142 | } |
| 3143 | |
| 3144 | VG_(send_signal_to_thread)( thread, sig ); |
| 3145 | SET_EDX(tid, 0); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3149 | /* ----------------------------------------------------------- |
| 3150 | FORK HANDLERS. |
| 3151 | -------------------------------------------------------- */ |
| 3152 | |
| 3153 | static |
| 3154 | void do__set_fhstack_used ( ThreadId tid, Int n ) |
| 3155 | { |
| 3156 | Char msg_buf[100]; |
| 3157 | if (VG_(clo_trace_sched)) { |
| 3158 | VG_(sprintf)(msg_buf, "set_fhstack_used to %d", n ); |
| 3159 | print_pthread_event(tid, msg_buf); |
| 3160 | } |
| 3161 | |
| 3162 | vg_assert(VG_(is_valid_tid)(tid) |
| 3163 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 3164 | |
| 3165 | if (n >= 0 && n < VG_N_FORKHANDLERSTACK) { |
| 3166 | vg_fhstack_used = n; |
| 3167 | SET_EDX(tid, 0); |
| 3168 | } else { |
| 3169 | SET_EDX(tid, -1); |
| 3170 | } |
| 3171 | } |
| 3172 | |
| 3173 | |
| 3174 | static |
| 3175 | void do__get_fhstack_used ( ThreadId tid ) |
| 3176 | { |
| 3177 | Int n; |
| 3178 | Char msg_buf[100]; |
| 3179 | if (VG_(clo_trace_sched)) { |
| 3180 | VG_(sprintf)(msg_buf, "get_fhstack_used" ); |
| 3181 | print_pthread_event(tid, msg_buf); |
| 3182 | } |
| 3183 | |
| 3184 | vg_assert(VG_(is_valid_tid)(tid) |
| 3185 | && VG_(threads)[tid].status == VgTs_Runnable); |
| 3186 | |
| 3187 | n = vg_fhstack_used; |
| 3188 | vg_assert(n >= 0 && n < VG_N_FORKHANDLERSTACK); |
| 3189 | SET_EDX(tid, n); |
| 3190 | } |
| 3191 | |
| 3192 | static |
| 3193 | void do__set_fhstack_entry ( ThreadId tid, Int n, ForkHandlerEntry* fh ) |
| 3194 | { |
| 3195 | Char msg_buf[100]; |
| 3196 | if (VG_(clo_trace_sched)) { |
| 3197 | VG_(sprintf)(msg_buf, "set_fhstack_entry %d to %p", n, fh ); |
| 3198 | print_pthread_event(tid, msg_buf); |
| 3199 | } |
| 3200 | |
| 3201 | vg_assert(VG_(is_valid_tid)(tid) |
| 3202 | && VG_(threads)[tid].status == VgTs_Runnable); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3203 | VG_TRACK( pre_mem_read, Vg_CorePThread, &VG_(threads)[tid], |
| 3204 | "pthread_atfork: prepare/parent/child", |
| 3205 | (Addr)fh, sizeof(ForkHandlerEntry)); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3206 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3207 | if (n < 0 || n >= VG_N_FORKHANDLERSTACK) { |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3208 | SET_EDX(tid, -1); |
| 3209 | return; |
| 3210 | } |
| 3211 | |
| 3212 | vg_fhstack[n] = *fh; |
| 3213 | SET_EDX(tid, 0); |
| 3214 | } |
| 3215 | |
| 3216 | |
| 3217 | static |
| 3218 | void do__get_fhstack_entry ( ThreadId tid, Int n, /*OUT*/ |
| 3219 | ForkHandlerEntry* fh ) |
| 3220 | { |
| 3221 | Char msg_buf[100]; |
| 3222 | if (VG_(clo_trace_sched)) { |
| 3223 | VG_(sprintf)(msg_buf, "get_fhstack_entry %d", n ); |
| 3224 | print_pthread_event(tid, msg_buf); |
| 3225 | } |
| 3226 | |
| 3227 | vg_assert(VG_(is_valid_tid)(tid) |
| 3228 | && VG_(threads)[tid].status == VgTs_Runnable); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3229 | VG_TRACK( pre_mem_write, Vg_CorePThread, &VG_(threads)[tid], |
| 3230 | "fork: prepare/parent/child", |
| 3231 | (Addr)fh, sizeof(ForkHandlerEntry)); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3232 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3233 | if (n < 0 || n >= VG_N_FORKHANDLERSTACK) { |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3234 | SET_EDX(tid, -1); |
| 3235 | return; |
| 3236 | } |
| 3237 | |
| 3238 | *fh = vg_fhstack[n]; |
| 3239 | SET_EDX(tid, 0); |
| 3240 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3241 | VG_TRACK( post_mem_write, (Addr)fh, sizeof(ForkHandlerEntry) ); |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3245 | /* --------------------------------------------------------------------- |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3246 | Handle client requests. |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3247 | ------------------------------------------------------------------ */ |
| 3248 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3249 | /* Do a client request for the thread tid. After the request, tid may |
| 3250 | or may not still be runnable; if not, the scheduler will have to |
| 3251 | choose a new thread to run. |
| 3252 | */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3253 | static |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3254 | void do_client_request ( ThreadId tid ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3255 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3256 | # define RETURN_WITH(vvv) \ |
| 3257 | { tst->m_edx = (vvv); \ |
| 3258 | tst->sh_edx = VG_(written_shadow_reg); \ |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | ThreadState* tst = &VG_(threads)[tid]; |
| 3262 | UInt* arg = (UInt*)(VG_(threads)[tid].m_eax); |
| 3263 | UInt req_no = arg[0]; |
| 3264 | |
| 3265 | /* VG_(printf)("req no = 0x%x\n", req_no); */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3266 | switch (req_no) { |
| 3267 | |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3268 | case VG_USERREQ__MALLOC: |
| 3269 | RETURN_WITH( |
| 3270 | (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocMalloc ) |
| 3271 | ); |
| 3272 | break; |
| 3273 | |
| 3274 | case VG_USERREQ__BUILTIN_NEW: |
| 3275 | RETURN_WITH( |
| 3276 | (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocNew ) |
| 3277 | ); |
| 3278 | break; |
| 3279 | |
| 3280 | case VG_USERREQ__BUILTIN_VEC_NEW: |
| 3281 | RETURN_WITH( |
| 3282 | (UInt)VG_(client_malloc) ( tst, arg[1], Vg_AllocNewVec ) |
| 3283 | ); |
| 3284 | break; |
| 3285 | |
| 3286 | case VG_USERREQ__FREE: |
| 3287 | VG_(client_free) ( tst, (void*)arg[1], Vg_AllocMalloc ); |
| 3288 | RETURN_WITH(0); /* irrelevant */ |
| 3289 | break; |
| 3290 | |
| 3291 | case VG_USERREQ__BUILTIN_DELETE: |
| 3292 | VG_(client_free) ( tst, (void*)arg[1], Vg_AllocNew ); |
| 3293 | RETURN_WITH(0); /* irrelevant */ |
| 3294 | break; |
| 3295 | |
| 3296 | case VG_USERREQ__BUILTIN_VEC_DELETE: |
| 3297 | VG_(client_free) ( tst, (void*)arg[1], Vg_AllocNewVec ); |
| 3298 | RETURN_WITH(0); /* irrelevant */ |
| 3299 | break; |
| 3300 | |
| 3301 | case VG_USERREQ__CALLOC: |
| 3302 | RETURN_WITH( |
| 3303 | (UInt)VG_(client_calloc) ( tst, arg[1], arg[2] ) |
| 3304 | ); |
| 3305 | break; |
| 3306 | |
| 3307 | case VG_USERREQ__REALLOC: |
| 3308 | RETURN_WITH( |
| 3309 | (UInt)VG_(client_realloc) ( tst, (void*)arg[1], arg[2] ) |
| 3310 | ); |
| 3311 | break; |
| 3312 | |
| 3313 | case VG_USERREQ__MEMALIGN: |
| 3314 | RETURN_WITH( |
| 3315 | (UInt)VG_(client_memalign) ( tst, arg[1], arg[2] ) |
| 3316 | ); |
| 3317 | break; |
| 3318 | |
| 3319 | case VG_USERREQ__PTHREAD_GET_THREADID: |
| 3320 | RETURN_WITH(tid); |
| 3321 | break; |
| 3322 | |
| 3323 | case VG_USERREQ__RUNNING_ON_VALGRIND: |
| 3324 | RETURN_WITH(1); |
| 3325 | break; |
| 3326 | |
| 3327 | case VG_USERREQ__GET_PTHREAD_TRACE_LEVEL: |
| 3328 | RETURN_WITH(VG_(clo_trace_pthread_level)); |
| 3329 | break; |
| 3330 | |
| 3331 | case VG_USERREQ__READ_MILLISECOND_TIMER: |
| 3332 | RETURN_WITH(VG_(read_millisecond_timer)()); |
| 3333 | break; |
| 3334 | |
| 3335 | /* Some of these may make thread tid non-runnable, but the |
| 3336 | scheduler checks for that on return from this function. */ |
| 3337 | case VG_USERREQ__PTHREAD_MUTEX_LOCK: |
| 3338 | do_pthread_mutex_lock( tid, False, (void *)(arg[1]) ); |
| 3339 | break; |
| 3340 | |
| 3341 | case VG_USERREQ__PTHREAD_MUTEX_TRYLOCK: |
| 3342 | do_pthread_mutex_lock( tid, True, (void *)(arg[1]) ); |
| 3343 | break; |
| 3344 | |
| 3345 | case VG_USERREQ__PTHREAD_MUTEX_UNLOCK: |
| 3346 | do_pthread_mutex_unlock( tid, (void *)(arg[1]) ); |
| 3347 | break; |
| 3348 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3349 | case VG_USERREQ__PTHREAD_GETSPECIFIC_PTR: |
| 3350 | do_pthread_getspecific_ptr ( tid ); |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3351 | break; |
| 3352 | |
| 3353 | case VG_USERREQ__SET_CANCELTYPE: |
| 3354 | do__set_canceltype ( tid, arg[1] ); |
| 3355 | break; |
| 3356 | |
| 3357 | case VG_USERREQ__CLEANUP_PUSH: |
| 3358 | do__cleanup_push ( tid, (CleanupEntry*)(arg[1]) ); |
| 3359 | break; |
| 3360 | |
| 3361 | case VG_USERREQ__CLEANUP_POP: |
| 3362 | do__cleanup_pop ( tid, (CleanupEntry*)(arg[1]) ); |
| 3363 | break; |
| 3364 | |
| 3365 | case VG_USERREQ__TESTCANCEL: |
| 3366 | do__testcancel ( tid ); |
| 3367 | break; |
| 3368 | |
| 3369 | case VG_USERREQ__GET_N_SIGS_RETURNED: |
| 3370 | RETURN_WITH(VG_(threads)[tid].n_signals_returned); |
| 3371 | break; |
| 3372 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3373 | case VG_USERREQ__PTHREAD_JOIN: |
| 3374 | do_pthread_join( tid, arg[1], (void**)(arg[2]) ); |
| 3375 | break; |
| 3376 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3377 | case VG_USERREQ__PTHREAD_COND_WAIT: |
| 3378 | do_pthread_cond_wait( tid, |
| 3379 | (pthread_cond_t *)(arg[1]), |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3380 | (pthread_mutex_t *)(arg[2]), |
| 3381 | 0xFFFFFFFF /* no timeout */ ); |
| 3382 | break; |
| 3383 | |
| 3384 | case VG_USERREQ__PTHREAD_COND_TIMEDWAIT: |
| 3385 | do_pthread_cond_wait( tid, |
| 3386 | (pthread_cond_t *)(arg[1]), |
| 3387 | (pthread_mutex_t *)(arg[2]), |
| 3388 | arg[3] /* timeout millisecond point */ ); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3389 | break; |
| 3390 | |
| 3391 | case VG_USERREQ__PTHREAD_COND_SIGNAL: |
| 3392 | do_pthread_cond_signal_or_broadcast( |
| 3393 | tid, |
| 3394 | False, /* signal, not broadcast */ |
| 3395 | (pthread_cond_t *)(arg[1]) ); |
| 3396 | break; |
| 3397 | |
| 3398 | case VG_USERREQ__PTHREAD_COND_BROADCAST: |
| 3399 | do_pthread_cond_signal_or_broadcast( |
| 3400 | tid, |
| 3401 | True, /* broadcast, not signal */ |
| 3402 | (pthread_cond_t *)(arg[1]) ); |
| 3403 | break; |
| 3404 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3405 | case VG_USERREQ__PTHREAD_KEY_VALIDATE: |
| 3406 | do_pthread_key_validate ( tid, |
| 3407 | (pthread_key_t)(arg[1]) ); |
| 3408 | break; |
| 3409 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3410 | case VG_USERREQ__PTHREAD_KEY_CREATE: |
| 3411 | do_pthread_key_create ( tid, |
| 3412 | (pthread_key_t*)(arg[1]), |
| 3413 | (void(*)(void*))(arg[2]) ); |
| 3414 | break; |
| 3415 | |
| 3416 | case VG_USERREQ__PTHREAD_KEY_DELETE: |
| 3417 | do_pthread_key_delete ( tid, |
| 3418 | (pthread_key_t)(arg[1]) ); |
| 3419 | break; |
| 3420 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 3421 | case VG_USERREQ__PTHREAD_SETSPECIFIC_PTR: |
| 3422 | do_pthread_setspecific_ptr ( tid, |
| 3423 | (void**)(arg[1]) ); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3426 | case VG_USERREQ__PTHREAD_SIGMASK: |
| 3427 | do_pthread_sigmask ( tid, |
| 3428 | arg[1], |
| 3429 | (vki_ksigset_t*)(arg[2]), |
| 3430 | (vki_ksigset_t*)(arg[3]) ); |
| 3431 | break; |
| 3432 | |
| 3433 | case VG_USERREQ__SIGWAIT: |
| 3434 | do_sigwait ( tid, |
| 3435 | (vki_ksigset_t*)(arg[1]), |
| 3436 | (Int*)(arg[2]) ); |
| 3437 | break; |
| 3438 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3439 | case VG_USERREQ__PTHREAD_KILL: |
| 3440 | do_pthread_kill ( tid, arg[1], arg[2] ); |
| 3441 | break; |
| 3442 | |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 3443 | case VG_USERREQ__PTHREAD_YIELD: |
| 3444 | do_pthread_yield ( tid ); |
sewardj | 18a62ff | 2002-07-12 22:30:51 +0000 | [diff] [blame] | 3445 | /* On return from do_client_request(), the scheduler will |
| 3446 | select a new thread to run. */ |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 3447 | break; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3448 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3449 | case VG_USERREQ__SET_CANCELSTATE: |
| 3450 | do__set_cancelstate ( tid, arg[1] ); |
| 3451 | break; |
| 3452 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 3453 | case VG_USERREQ__SET_OR_GET_DETACH: |
| 3454 | do__set_or_get_detach ( tid, arg[1], arg[2] ); |
| 3455 | break; |
| 3456 | |
| 3457 | case VG_USERREQ__SET_CANCELPEND: |
| 3458 | do__set_cancelpend ( tid, arg[1], (void(*)(void*))arg[2] ); |
| 3459 | break; |
| 3460 | |
| 3461 | case VG_USERREQ__WAIT_JOINER: |
| 3462 | do__wait_joiner ( tid, (void*)arg[1] ); |
| 3463 | break; |
| 3464 | |
| 3465 | case VG_USERREQ__QUIT: |
| 3466 | do__quit ( tid ); |
| 3467 | break; |
| 3468 | |
| 3469 | case VG_USERREQ__APPLY_IN_NEW_THREAD: |
| 3470 | do__apply_in_new_thread ( tid, (void*(*)(void*))arg[1], |
| 3471 | (void*)arg[2] ); |
| 3472 | break; |
| 3473 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 3474 | case VG_USERREQ__GET_KEY_D_AND_S: |
| 3475 | do__get_key_destr_and_spec ( tid, |
| 3476 | (pthread_key_t)arg[1], |
| 3477 | (CleanupEntry*)arg[2] ); |
| 3478 | break; |
| 3479 | |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 3480 | case VG_USERREQ__NUKE_OTHER_THREADS: |
| 3481 | VG_(nuke_all_threads_except) ( tid ); |
| 3482 | SET_EDX(tid, 0); |
| 3483 | break; |
| 3484 | |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3485 | case VG_USERREQ__PTHREAD_ERROR: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3486 | VG_(record_pthread_error)( tid, (Char*)(arg[1]) ); |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 3487 | SET_EDX(tid, 0); |
| 3488 | break; |
| 3489 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 3490 | case VG_USERREQ__SET_FHSTACK_USED: |
| 3491 | do__set_fhstack_used( tid, (Int)(arg[1]) ); |
| 3492 | break; |
| 3493 | |
| 3494 | case VG_USERREQ__GET_FHSTACK_USED: |
| 3495 | do__get_fhstack_used( tid ); |
| 3496 | break; |
| 3497 | |
| 3498 | case VG_USERREQ__SET_FHSTACK_ENTRY: |
| 3499 | do__set_fhstack_entry( tid, (Int)(arg[1]), |
| 3500 | (ForkHandlerEntry*)(arg[2]) ); |
| 3501 | break; |
| 3502 | |
| 3503 | case VG_USERREQ__GET_FHSTACK_ENTRY: |
| 3504 | do__get_fhstack_entry( tid, (Int)(arg[1]), |
| 3505 | (ForkHandlerEntry*)(arg[2]) ); |
| 3506 | break; |
| 3507 | |
sewardj | 77e466c | 2002-04-14 02:29:29 +0000 | [diff] [blame] | 3508 | case VG_USERREQ__SIGNAL_RETURNS: |
| 3509 | handle_signal_return(tid); |
| 3510 | break; |
sewardj | 69a72a5 | 2002-11-03 13:41:41 +0000 | [diff] [blame] | 3511 | |
| 3512 | case VG_USERREQ__LOGMESSAGE: |
| 3513 | VG_(message)(Vg_UserMsg, "%s", (Char*)(arg[1])); |
| 3514 | break; |
sewardj | 54cacf0 | 2002-04-12 23:24:59 +0000 | [diff] [blame] | 3515 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3516 | /* Requests from the client program */ |
| 3517 | |
| 3518 | case VG_USERREQ__DISCARD_TRANSLATIONS: |
| 3519 | if (VG_(clo_verbosity) > 2) |
| 3520 | VG_(printf)( "client request: DISCARD_TRANSLATIONS," |
| 3521 | " addr %p, len %d\n", |
| 3522 | (void*)arg[1], arg[2] ); |
| 3523 | |
| 3524 | VG_(invalidate_translations)( arg[1], arg[2] ); |
| 3525 | |
| 3526 | SET_EDX( tid, 0 ); /* return value is meaningless */ |
| 3527 | break; |
| 3528 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3529 | default: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3530 | if (VG_(needs).client_requests) { |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3531 | UInt ret; |
| 3532 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3533 | if (VG_(clo_verbosity) > 2) |
| 3534 | VG_(printf)("client request: code %d, addr %p, len %d\n", |
| 3535 | arg[0], (void*)arg[1], arg[2] ); |
| 3536 | |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3537 | if (SK_(handle_client_request) ( &VG_(threads)[tid], arg, &ret )) |
| 3538 | SET_EDX(tid, ret); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3539 | } else { |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 3540 | static Bool whined = False; |
| 3541 | |
| 3542 | if (!whined) { |
| 3543 | VG_(message)(Vg_UserMsg, "Warning:\n" |
| 3544 | " unhandled client request: 0x%x (%c%c+%d). Perhaps\n" |
| 3545 | " VG_(needs).client_requests should be set?\n", |
| 3546 | arg[0], (arg[0] >> 24) & 0xff, (arg[0] >> 16) & 0xff, |
| 3547 | arg[0] & 0xffff); |
| 3548 | whined = True; |
| 3549 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3550 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3551 | break; |
| 3552 | } |
sewardj | 124ca2a | 2002-06-20 10:19:38 +0000 | [diff] [blame] | 3553 | |
| 3554 | # undef RETURN_WITH |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3558 | /* --------------------------------------------------------------------- |
| 3559 | Sanity checking. |
| 3560 | ------------------------------------------------------------------ */ |
| 3561 | |
| 3562 | /* Internal consistency checks on the sched/pthread structures. */ |
| 3563 | static |
| 3564 | void scheduler_sanity ( void ) |
| 3565 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3566 | pthread_mutex_t* mx; |
| 3567 | pthread_cond_t* cv; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3568 | Int i; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3569 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3570 | /* VG_(printf)("scheduler_sanity\n"); */ |
| 3571 | for (i = 1; i < VG_N_THREADS; i++) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3572 | mx = VG_(threads)[i].associated_mx; |
| 3573 | cv = VG_(threads)[i].associated_cv; |
| 3574 | if (VG_(threads)[i].status == VgTs_WaitMX) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3575 | /* If we're waiting on a MX: (1) the mx is not null, (2, 3) |
| 3576 | it's actually held by someone, since otherwise this thread |
| 3577 | is deadlocked, (4) the mutex's owner is not us, since |
| 3578 | otherwise this thread is also deadlocked. The logic in |
| 3579 | do_pthread_mutex_lock rejects attempts by a thread to lock |
| 3580 | a (non-recursive) mutex which it already owns. |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3581 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3582 | (2) has been seen to fail sometimes. I don't know why. |
| 3583 | Possibly to do with signals. */ |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3584 | vg_assert(cv == NULL); |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3585 | /* 1 */ vg_assert(mx != NULL); |
| 3586 | /* 2 */ vg_assert(mx->__m_count > 0); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3587 | /* 3 */ vg_assert(VG_(is_valid_tid)((ThreadId)mx->__m_owner)); |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3588 | /* 4 */ vg_assert(i != (ThreadId)mx->__m_owner); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3589 | } else |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3590 | if (VG_(threads)[i].status == VgTs_WaitCV) { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 3591 | vg_assert(cv != NULL); |
| 3592 | vg_assert(mx != NULL); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3593 | } else { |
sewardj | 0555387 | 2002-04-20 20:53:17 +0000 | [diff] [blame] | 3594 | /* Unfortunately these don't hold true when a sighandler is |
| 3595 | running. To be fixed. */ |
| 3596 | /* vg_assert(cv == NULL); */ |
| 3597 | /* vg_assert(mx == NULL); */ |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3598 | } |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3599 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3600 | if (VG_(threads)[i].status != VgTs_Empty) { |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3601 | Int |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3602 | stack_used = (Addr)VG_(threads)[i].stack_highest_word |
| 3603 | - (Addr)VG_(threads)[i].m_esp; |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3604 | if (i > 1 /* not the root thread */ |
| 3605 | && stack_used |
| 3606 | >= (VG_PTHREAD_STACK_MIN - 1000 /* paranoia */)) { |
| 3607 | VG_(message)(Vg_UserMsg, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3608 | "Error: STACK OVERFLOW: " |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3609 | "thread %d: stack used %d, available %d", |
| 3610 | i, stack_used, VG_PTHREAD_STACK_MIN ); |
| 3611 | VG_(message)(Vg_UserMsg, |
| 3612 | "Terminating Valgrind. If thread(s) " |
| 3613 | "really need more stack, increase"); |
| 3614 | VG_(message)(Vg_UserMsg, |
| 3615 | "VG_PTHREAD_STACK_SIZE in vg_include.h and recompile."); |
| 3616 | VG_(exit)(1); |
| 3617 | } |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3618 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3619 | if (VG_(threads)[i].status == VgTs_WaitSIG) { |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3620 | vg_assert( ! VG_(kisemptysigset)( |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3621 | & VG_(threads)[i].sigs_waited_for) ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3622 | } else { |
| 3623 | vg_assert( VG_(kisemptysigset)( |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 3624 | & VG_(threads)[i].sigs_waited_for) ); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
sewardj | bf290b9 | 2002-05-01 02:28:01 +0000 | [diff] [blame] | 3627 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3628 | } |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 3629 | |
| 3630 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 3631 | if (!vg_thread_keys[i].inuse) |
| 3632 | vg_assert(vg_thread_keys[i].destructor == NULL); |
| 3633 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 3637 | /*--------------------------------------------------------------------*/ |
| 3638 | /*--- end vg_scheduler.c ---*/ |
| 3639 | /*--------------------------------------------------------------------*/ |