2 /*--------------------------------------------------------------------*/
3 /*--- The thread state. pub_core_threadstate.h ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #ifndef __PUB_CORE_THREADSTATE_H
32 #define __PUB_CORE_THREADSTATE_H
34 //--------------------------------------------------------------------
35 // PURPOSE: This module defines the ThreadState type and the
36 // VG_(threads)[] data structure which holds all the important thread
37 // state. It also defines some simple operations on the data structure
38 // that don't require any external help. (m_scheduler does the complex
40 //--------------------------------------------------------------------
42 #include "pub_tool_threadstate.h"
44 #include <l4/sys/types.h>
45 #include <l4/sys/utcb.h>
46 #include <l4re_helper.h>
47 #include <l4/sys/thread.h>
49 #include "pub_l4re_consts.h"
52 /*------------------------------------------------------------*/
54 /*------------------------------------------------------------*/
59 Empty -> Init -> Runnable <=> WaitSys/Yielding
65 VgTs_Empty, /* this slot is not in use */
66 VgTs_Init, /* just allocated */
67 VgTs_Runnable, /* ready to run */
68 VgTs_WaitSys, /* waiting for a syscall to complete */
69 VgTs_Yielding, /* temporarily yielding the CPU */
70 VgTs_Zombie, /* transient state just before exiting */
74 /* Return codes from the scheduler. */
77 VgSrc_None, /* not exiting yet */
78 VgSrc_ExitThread, /* just this thread is exiting */
79 VgSrc_ExitProcess, /* entire process is exiting */
80 VgSrc_FatalSig /* Killed by the default action of a fatal
87 typedef VexGuestX86State VexGuestArchState;
88 #elif defined(VGA_amd64)
89 typedef VexGuestAMD64State VexGuestArchState;
90 #elif defined(VGA_ppc32)
91 typedef VexGuestPPC32State VexGuestArchState;
92 #elif defined(VGA_ppc64)
93 typedef VexGuestPPC64State VexGuestArchState;
94 #elif defined(VGA_arm)
95 typedef VexGuestARMState VexGuestArchState;
97 # error Unknown architecture
100 /* Forward declarations */
101 struct SyscallStatus;
104 /* Architecture-specific thread state */
107 /* --- BEGIN vex-mandated guest state --- */
109 /* Note that for code generation reasons, we require that the
110 guest state area, its two shadows, and the spill area, are
111 16-aligned and have 16-aligned sizes, and there are no holes
112 in between. This is checked by do_pre_run_checks() in
115 /* Saved machine context. */
116 VexGuestArchState vex __attribute__((aligned(16)));
118 /* Saved shadow context (2 copies). */
119 VexGuestArchState vex_shadow1 __attribute__((aligned(16)));
120 VexGuestArchState vex_shadow2 __attribute__((aligned(16)));
123 UChar vex_spill[LibVEX_N_SPILL_BYTES] __attribute__((aligned(16)));
125 /* --- END vex-mandated guest state --- */
130 /* OS-specific thread state. IMPORTANT: if you add fields to this,
131 you _must_ add code to os_state_clear() to initialise those
136 Int lwpid; // PID of kernel task (Darwin: Mach thread)
137 Int threadgroup; // thread group id
139 ThreadId parent; // parent tid (if any)
141 /* runtime details */
142 Addr valgrind_stack_base; // Valgrind's stack (VgStack*)
143 Addr valgrind_stack_init_SP; // starting value for SP
146 Word exitcode; // in the case of exitgroup, set by someone else
147 Int fatalsig; // fatal signal
149 # if defined(VGO_aix5)
150 /* AIX specific fields to make thread cancellation sort-of work */
151 /* What is this thread's current cancellation state a la
152 POSIX (deferred vs async, enable vs disabled) ? */
153 Bool cancel_async; // current cancel mode (async vs deferred)
154 Bool cancel_disabled; // cancellation disabled?
155 /* What's happened so far? */
156 enum { Canc_NoRequest=0, // no cancellation requested
157 Canc_Requested=1, // requested but not actioned
158 Canc_Actioned=2 } // requested and actioned
160 /* Initial state is False, False, Canc_Normal. */
163 # if defined(VGO_darwin)
164 // Mach trap POST handler as chosen by PRE
165 void (*post_mach_trap_fn)(ThreadId tid,
166 struct SyscallArgs *, struct SyscallStatus *);
168 // This thread's pthread
171 // Argument passed when thread started
174 // Synchronization between child thread and parent thread's POST wrapper
175 semaphore_t child_go;
176 semaphore_t child_done;
178 // Workqueue re-entry
179 // (setjmp in PRE(workq_ops), longjmp in wqthread_hijack)
180 // DDD: JRS fixme: this comment is no longer correct; wq_jmpbuf is
181 // never used, and there is no such setjmp or longjmp pair.
182 // I guess we could leave wq_jmpbuf_valid in place though, since
183 // it does allow for an assertion in ML_(wqthread_continue_NORETURN).
184 Bool wq_jmpbuf_valid;
187 // Values saved from transient Mach RPC messages
188 Addr remote_port; // destination for original message
189 Int msgh_id; // outgoing message id
196 } mach_port_allocate;
201 } mach_port_mod_refs;
206 } mach_port_insert_right;
224 UWord new_protection;
254 } mach_vm_deallocate;
259 unsigned int new_protection;
272 } io_connect_unmap_memory;
275 } task_get_special_port;
281 } WindowServer_29828;
284 } WindowServer_29831;
287 } io_registry_entry_from_path;
291 #if defined(VGO_l4re)
292 char utcb_store[L4RE_UTCB_SIZE]; // XXX rename utcb_copy ?
298 SYS_ENTER_KDEBUG = 0x25,
299 SYS_LINUX_INT80 = 0x26,
301 SYS_ARTIFICIAL = 0x28,
307 /* Overall thread state */
309 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
310 The thread identity is simply the index in vg_threads[].
311 ThreadId == 1 is the root thread and has the special property
312 that we don't try and allocate or deallocate its stack. For
313 convenience of generating error message, we also put the
314 ThreadId in this tid field, but be aware that it should
315 ALWAYS == the index in vg_threads[]. */
318 /* Current scheduling status. */
321 /* This is set if the thread is in the process of exiting for any
322 reason. The precise details of the exit are in the OS-specific
324 VgSchedReturnCode exitreason;
326 /* Architecture-specific thread state. */
327 ThreadArchState arch;
329 /* This thread's blocked-signals mask. Semantics is that for a
330 signal to be delivered to this thread, the signal must not be
331 blocked by this signal mask. If more than one thread accepts a
332 signal, then it will be delivered to one at random. If all
333 threads block the signal, it will remain pending until either a
334 thread unblocks it or someone uses sigwaitsig/sigtimedwait. */
335 vki_sigset_t sig_mask;
337 /* tmp_sig_mask is usually the same as sig_mask, and is kept in
338 sync whenever sig_mask is changed. The only time they have
339 different values is during the execution of a sigsuspend, where
340 tmp_sig_mask is the temporary mask which sigsuspend installs.
341 It is only consulted to compute the signal mask applied to a
343 vki_sigset_t tmp_sig_mask;
345 /* A little signal queue for signals we can't get the kernel to
346 queue for us. This is only allocated as needed, since it should
348 struct SigQueue *sig_queue;
350 /* Client stacks. When a thread slot is freed, we don't deallocate its
351 stack; we just leave it lying around for the next use of the
352 slot. If the next use of the slot requires a larger stack,
353 only then is the old one deallocated and a new one
356 For the main thread (threadid == 1), this mechanism doesn't
357 apply. We don't know the size of the stack since we didn't
358 allocate it, and furthermore we never reallocate it. */
360 /* The allocated size of this thread's stack */
361 SizeT client_stack_szB;
363 /* Address of the highest legitimate word in this stack. This is
364 used for error messages only -- not critical for execution
365 correctness. Is is set for all stacks, specifically including
366 ThreadId == 1 (the main thread). */
367 Addr client_stack_highest_word;
369 /* Alternate signal stack */
370 vki_stack_t altstack;
372 /* OS-specific thread state */
373 ThreadOSstate os_state;
375 /* Per-thread jmp_buf to resume scheduler after a signal */
376 Bool sched_jmpbuf_valid;
377 jmp_buf sched_jmpbuf;
382 #if defined(VGO_l4re)
385 * L4Re redirections that trap into Valgrind even though they are not
388 * _dl_open, _dl_close, _dl_mmap
399 unsigned int redir_type;
424 /*------------------------------------------------------------*/
425 /*--- L4Re OS state accessor functions ---*/
426 /*------------------------------------------------------------*/
427 static inline char *ts_utcb_copy(ThreadState *ts)
428 { return ts->os_state.utcb_store; }
430 static inline l4_utcb_t *ts_utcb(ThreadState *ts)
431 { return ts->os_state.utcb; }
435 /*------------------------------------------------------------*/
436 /*--- The thread table. ---*/
437 /*------------------------------------------------------------*/
439 /* A statically allocated array of threads. NOTE: [0] is
440 never used, to simplify the simulation of initialisers for
442 extern ThreadState VG_(threads)[VG_N_THREADS];
444 // The running thread. m_scheduler should be the only other module
446 extern ThreadId VG_(running_tid);
449 /*------------------------------------------------------------*/
450 /*--- Basic operations on the thread table. ---*/
451 /*------------------------------------------------------------*/
453 // Convert a ThreadStatus to a string.
454 const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status );
456 /* Get the ThreadState for a particular thread */
457 extern ThreadState *VG_(get_ThreadState) ( ThreadId tid );
459 /* Check that tid is in range and denotes a non-Empty thread. */
460 extern Bool VG_(is_valid_tid) ( ThreadId tid );
462 /* Returns true if a thread is currently running (ie, has the CPU lock) */
463 extern Bool VG_(is_running_thread)(ThreadId tid);
465 /* Returns true if the thread is in the process of exiting */
466 extern Bool VG_(is_exiting)(ThreadId tid);
468 /* Return the number of non-dead Threads */
469 extern Int VG_(count_living_threads)(void);
471 /* Return the number of threads in VgTs_Runnable state */
472 extern Int VG_(count_runnable_threads)(void);
474 /* Given an LWP id (ie, real kernel thread id), find the corresponding
476 extern ThreadId VG_(lwpid_to_vgtid)(Int lwpid);
478 #endif // __PUB_CORE_THREADSTATE_H
480 /*--------------------------------------------------------------------*/
482 /*--------------------------------------------------------------------*/