Thread.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Time.h>
  29. #include <Kernel/Arch/i386/CPU.h>
  30. #include <Kernel/FileSystem/FileDescription.h>
  31. #include <Kernel/KSyms.h>
  32. #include <Kernel/Process.h>
  33. #include <Kernel/Profiling.h>
  34. #include <Kernel/Scheduler.h>
  35. #include <Kernel/Thread.h>
  36. #include <Kernel/ThreadTracer.h>
  37. #include <Kernel/TimerQueue.h>
  38. #include <Kernel/VM/MemoryManager.h>
  39. #include <Kernel/VM/PageDirectory.h>
  40. #include <Kernel/VM/ProcessPagingScope.h>
  41. #include <LibC/signal_numbers.h>
  42. #include <LibELF/Loader.h>
  43. //#define SIGNAL_DEBUG
  44. //#define THREAD_DEBUG
  45. namespace Kernel {
  46. Thread::Thread(NonnullRefPtr<Process> process)
  47. : m_process(move(process))
  48. , m_name(m_process->name())
  49. {
  50. if (m_process->m_thread_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed) == 0) {
  51. // First thread gets TID == PID
  52. m_tid = m_process->pid().value();
  53. } else {
  54. m_tid = Process::allocate_pid().value();
  55. }
  56. #ifdef THREAD_DEBUG
  57. dbg() << "Created new thread " << m_process->name() << "(" << m_process->pid().value() << ":" << m_tid.value() << ")";
  58. #endif
  59. set_default_signal_dispositions();
  60. m_fpu_state = (FPUState*)kmalloc_aligned<16>(sizeof(FPUState));
  61. reset_fpu_state();
  62. memset(&m_tss, 0, sizeof(m_tss));
  63. m_tss.iomapbase = sizeof(TSS32);
  64. // Only IF is set when a process boots.
  65. m_tss.eflags = 0x0202;
  66. if (m_process->is_kernel_process()) {
  67. m_tss.cs = GDT_SELECTOR_CODE0;
  68. m_tss.ds = GDT_SELECTOR_DATA0;
  69. m_tss.es = GDT_SELECTOR_DATA0;
  70. m_tss.fs = GDT_SELECTOR_PROC;
  71. m_tss.ss = GDT_SELECTOR_DATA0;
  72. m_tss.gs = 0;
  73. } else {
  74. m_tss.cs = GDT_SELECTOR_CODE3 | 3;
  75. m_tss.ds = GDT_SELECTOR_DATA3 | 3;
  76. m_tss.es = GDT_SELECTOR_DATA3 | 3;
  77. m_tss.fs = GDT_SELECTOR_DATA3 | 3;
  78. m_tss.ss = GDT_SELECTOR_DATA3 | 3;
  79. m_tss.gs = GDT_SELECTOR_TLS | 3;
  80. }
  81. m_tss.cr3 = m_process->page_directory().cr3();
  82. m_kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, String::format("Kernel Stack (Thread %d)", m_tid.value()), Region::Access::Read | Region::Access::Write, false, true);
  83. m_kernel_stack_region->set_stack(true);
  84. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  85. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
  86. if (m_process->is_kernel_process()) {
  87. m_tss.esp = m_tss.esp0 = m_kernel_stack_top;
  88. } else {
  89. // Ring 3 processes get a separate stack for ring 0.
  90. // The ring 3 stack will be assigned by exec().
  91. m_tss.ss0 = GDT_SELECTOR_DATA0;
  92. m_tss.esp0 = m_kernel_stack_top;
  93. }
  94. // We need to add another reference if we could successfully create
  95. // all the resources needed for this thread. The reason for this is that
  96. // we don't want to delete this thread after dropping the reference,
  97. // it may still be running or scheduled to be run.
  98. // The finalizer is responsible for dropping this reference once this
  99. // thread is ready to be cleaned up.
  100. ref();
  101. if (m_process->pid() != 0)
  102. Scheduler::init_thread(*this);
  103. }
  104. Thread::~Thread()
  105. {
  106. {
  107. // We need to explicitly remove ourselves from the thread list
  108. // here. We may get pre-empted in the middle of destructing this
  109. // thread, which causes problems if the thread list is iterated.
  110. // Specifically, if this is the last thread of a process, checking
  111. // block conditions would access m_process, which would be in
  112. // the middle of being destroyed.
  113. ScopedSpinLock lock(g_scheduler_lock);
  114. g_scheduler_data->thread_list_for_state(m_state).remove(*this);
  115. }
  116. }
  117. void Thread::unblock_from_blocker(Blocker& blocker)
  118. {
  119. auto do_unblock = [&]() {
  120. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  121. ScopedSpinLock block_lock(m_block_lock);
  122. if (m_blocker != &blocker)
  123. return;
  124. if (!should_be_stopped() && !is_stopped())
  125. unblock();
  126. };
  127. if (Processor::current().in_irq()) {
  128. Processor::current().deferred_call_queue([do_unblock = move(do_unblock), self = make_weak_ptr()]() {
  129. if (auto this_thread = self.strong_ref())
  130. do_unblock();
  131. });
  132. } else {
  133. do_unblock();
  134. }
  135. }
  136. void Thread::unblock(u8 signal)
  137. {
  138. ASSERT(!Processor::current().in_irq());
  139. ASSERT(g_scheduler_lock.own_lock());
  140. ASSERT(m_block_lock.own_lock());
  141. if (m_state != Thread::Blocked)
  142. return;
  143. ASSERT(m_blocker);
  144. if (signal != 0) {
  145. if (!m_blocker->can_be_interrupted() && !m_should_die)
  146. return;
  147. m_blocker->set_interrupted_by_signal(signal);
  148. }
  149. m_blocker = nullptr;
  150. if (Thread::current() == this) {
  151. set_state(Thread::Running);
  152. return;
  153. }
  154. ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
  155. set_state(Thread::Runnable);
  156. }
  157. void Thread::set_should_die()
  158. {
  159. if (m_should_die) {
  160. #ifdef THREAD_DEBUG
  161. dbg() << *this << " Should already die";
  162. #endif
  163. return;
  164. }
  165. ScopedCritical critical;
  166. // Remember that we should die instead of returning to
  167. // the userspace.
  168. ScopedSpinLock lock(g_scheduler_lock);
  169. m_should_die = true;
  170. // NOTE: Even the current thread can technically be in "Stopped"
  171. // state! This is the case when another thread sent a SIGSTOP to
  172. // it while it was running and it calls e.g. exit() before
  173. // the scheduler gets involved again.
  174. if (is_stopped()) {
  175. // If we were stopped, we need to briefly resume so that
  176. // the kernel stacks can clean up. We won't ever return back
  177. // to user mode, though
  178. ASSERT(!process().is_stopped());
  179. resume_from_stopped();
  180. }
  181. if (is_blocked()) {
  182. ScopedSpinLock block_lock(m_block_lock);
  183. if (m_blocker) {
  184. // We're blocked in the kernel.
  185. m_blocker->set_interrupted_by_death();
  186. unblock();
  187. }
  188. }
  189. }
  190. void Thread::die_if_needed()
  191. {
  192. ASSERT(Thread::current() == this);
  193. if (!m_should_die)
  194. return;
  195. u32 unlock_count;
  196. (void)unlock_process_if_locked(unlock_count);
  197. ScopedCritical critical;
  198. set_should_die();
  199. // Flag a context switch. Because we're in a critical section,
  200. // Scheduler::yield will actually only mark a pending scontext switch
  201. // Simply leaving the critical section would not necessarily trigger
  202. // a switch.
  203. Scheduler::yield();
  204. // Now leave the critical section so that we can also trigger the
  205. // actual context switch
  206. u32 prev_flags;
  207. Processor::current().clear_critical(prev_flags, false);
  208. dbg() << "die_if_needed returned from clear_critical!!! in irq: " << Processor::current().in_irq();
  209. // We should never get here, but the scoped scheduler lock
  210. // will be released by Scheduler::context_switch again
  211. ASSERT_NOT_REACHED();
  212. }
  213. void Thread::exit(void* exit_value)
  214. {
  215. ASSERT(Thread::current() == this);
  216. m_join_condition.thread_did_exit(exit_value);
  217. set_should_die();
  218. u32 unlock_count;
  219. (void)unlock_process_if_locked(unlock_count);
  220. die_if_needed();
  221. }
  222. void Thread::yield_while_not_holding_big_lock()
  223. {
  224. ASSERT(!g_scheduler_lock.own_lock());
  225. u32 prev_flags;
  226. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  227. Scheduler::yield();
  228. // NOTE: We may be on a different CPU now!
  229. Processor::current().restore_critical(prev_crit, prev_flags);
  230. }
  231. void Thread::yield_without_holding_big_lock()
  232. {
  233. ASSERT(!g_scheduler_lock.own_lock());
  234. u32 lock_count_to_restore = 0;
  235. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  236. // NOTE: Even though we call Scheduler::yield here, unless we happen
  237. // to be outside of a critical section, the yield will be postponed
  238. // until leaving it in relock_process.
  239. Scheduler::yield();
  240. relock_process(previous_locked, lock_count_to_restore);
  241. }
  242. void Thread::donate_without_holding_big_lock(RefPtr<Thread>& thread, const char* reason)
  243. {
  244. ASSERT(!g_scheduler_lock.own_lock());
  245. u32 lock_count_to_restore = 0;
  246. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  247. // NOTE: Even though we call Scheduler::yield here, unless we happen
  248. // to be outside of a critical section, the yield will be postponed
  249. // until leaving it in relock_process.
  250. Scheduler::donate_to(thread, reason);
  251. relock_process(previous_locked, lock_count_to_restore);
  252. }
  253. LockMode Thread::unlock_process_if_locked(u32& lock_count_to_restore)
  254. {
  255. return process().big_lock().force_unlock_if_locked(lock_count_to_restore);
  256. }
  257. void Thread::relock_process(LockMode previous_locked, u32 lock_count_to_restore)
  258. {
  259. // Clearing the critical section may trigger the context switch
  260. // flagged by calling Scheduler::donate_to or Scheduler::yield
  261. // above. We have to do it this way because we intentionally
  262. // leave the critical section here to be able to switch contexts.
  263. u32 prev_flags;
  264. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  265. // CONTEXT SWITCH HAPPENS HERE!
  266. // NOTE: We may be on a different CPU now!
  267. Processor::current().restore_critical(prev_crit, prev_flags);
  268. if (previous_locked != LockMode::Unlocked) {
  269. // We've unblocked, relock the process if needed and carry on.
  270. RESTORE_LOCK(process().big_lock(), previous_locked, lock_count_to_restore);
  271. }
  272. }
  273. auto Thread::sleep(clockid_t clock_id, const timespec& duration, timespec* remaining_time) -> BlockResult
  274. {
  275. ASSERT(state() == Thread::Running);
  276. return Thread::current()->block<Thread::SleepBlocker>(nullptr, Thread::BlockTimeout(false, &duration, nullptr, clock_id), remaining_time);
  277. }
  278. auto Thread::sleep_until(clockid_t clock_id, const timespec& deadline) -> BlockResult
  279. {
  280. ASSERT(state() == Thread::Running);
  281. return Thread::current()->block<Thread::SleepBlocker>(nullptr, Thread::BlockTimeout(true, &deadline, nullptr, clock_id));
  282. }
  283. const char* Thread::state_string() const
  284. {
  285. switch (state()) {
  286. case Thread::Invalid:
  287. return "Invalid";
  288. case Thread::Runnable:
  289. return "Runnable";
  290. case Thread::Running:
  291. return "Running";
  292. case Thread::Dying:
  293. return "Dying";
  294. case Thread::Dead:
  295. return "Dead";
  296. case Thread::Stopped:
  297. return "Stopped";
  298. case Thread::Blocked: {
  299. ScopedSpinLock block_lock(m_block_lock);
  300. ASSERT(m_blocker != nullptr);
  301. return m_blocker->state_string();
  302. }
  303. }
  304. klog() << "Thread::state_string(): Invalid state: " << state();
  305. ASSERT_NOT_REACHED();
  306. return nullptr;
  307. }
  308. void Thread::finalize()
  309. {
  310. ASSERT(Thread::current() == g_finalizer);
  311. ASSERT(Thread::current() != this);
  312. #ifdef LOCK_DEBUG
  313. ASSERT(!m_lock.own_lock());
  314. if (lock_count() > 0) {
  315. dbg() << "Thread " << *this << " leaking " << lock_count() << " Locks!";
  316. ScopedSpinLock list_lock(m_holding_locks_lock);
  317. for (auto& info : m_holding_locks_list)
  318. dbg() << " - " << info.lock->name() << " @ " << info.lock << " locked at " << info.file << ":" << info.line << " count: " << info.count;
  319. ASSERT_NOT_REACHED();
  320. }
  321. #endif
  322. {
  323. ScopedSpinLock lock(g_scheduler_lock);
  324. #ifdef THREAD_DEBUG
  325. dbg() << "Finalizing thread " << *this;
  326. #endif
  327. set_state(Thread::State::Dead);
  328. m_join_condition.thread_finalizing();
  329. }
  330. if (m_dump_backtrace_on_finalization)
  331. dbg() << backtrace_impl();
  332. kfree_aligned(m_fpu_state);
  333. auto thread_cnt_before = m_process->m_thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
  334. ASSERT(thread_cnt_before != 0);
  335. if (thread_cnt_before == 1)
  336. process().finalize();
  337. }
  338. void Thread::finalize_dying_threads()
  339. {
  340. ASSERT(Thread::current() == g_finalizer);
  341. Vector<Thread*, 32> dying_threads;
  342. {
  343. ScopedSpinLock lock(g_scheduler_lock);
  344. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  345. if (thread.is_finalizable())
  346. dying_threads.append(&thread);
  347. return IterationDecision::Continue;
  348. });
  349. }
  350. for (auto* thread : dying_threads) {
  351. thread->finalize();
  352. // This thread will never execute again, drop the running reference
  353. // NOTE: This may not necessarily drop the last reference if anything
  354. // else is still holding onto this thread!
  355. thread->unref();
  356. }
  357. }
  358. bool Thread::tick()
  359. {
  360. ++m_ticks;
  361. if (tss().cs & 3)
  362. ++m_process->m_ticks_in_user;
  363. else
  364. ++m_process->m_ticks_in_kernel;
  365. return --m_ticks_left;
  366. }
  367. void Thread::check_dispatch_pending_signal()
  368. {
  369. auto result = DispatchSignalResult::Continue;
  370. {
  371. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  372. if (pending_signals_for_state()) {
  373. ScopedSpinLock lock(m_lock);
  374. result = dispatch_one_pending_signal();
  375. }
  376. }
  377. switch (result) {
  378. case DispatchSignalResult::Yield:
  379. yield_while_not_holding_big_lock();
  380. break;
  381. case DispatchSignalResult::Terminate:
  382. process().die();
  383. break;
  384. default:
  385. break;
  386. }
  387. }
  388. bool Thread::has_pending_signal(u8 signal) const
  389. {
  390. ScopedSpinLock lock(g_scheduler_lock);
  391. return pending_signals_for_state() & (1 << (signal - 1));
  392. }
  393. u32 Thread::pending_signals() const
  394. {
  395. ScopedSpinLock lock(g_scheduler_lock);
  396. return pending_signals_for_state();
  397. }
  398. u32 Thread::pending_signals_for_state() const
  399. {
  400. ASSERT(g_scheduler_lock.own_lock());
  401. constexpr u32 stopped_signal_mask = (1 << (SIGCONT - 1)) | (1 << (SIGKILL - 1)) | (1 << (SIGTRAP - 1));
  402. return m_state != Stopped ? m_pending_signals : m_pending_signals & stopped_signal_mask;
  403. }
  404. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  405. {
  406. ASSERT(signal < 32);
  407. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  408. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  409. if (should_ignore_signal(signal)) {
  410. #ifdef SIGNAL_DEBUG
  411. dbg() << "Signal " << signal << " was ignored by " << process();
  412. #endif
  413. return;
  414. }
  415. #ifdef SIGNAL_DEBUG
  416. if (sender)
  417. dbg() << "Signal: " << *sender << " sent " << signal << " to " << process();
  418. else
  419. dbg() << "Signal: Kernel sent " << signal << " to " << process();
  420. #endif
  421. m_pending_signals |= 1 << (signal - 1);
  422. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  423. if (m_state == Stopped) {
  424. ScopedSpinLock lock(m_lock);
  425. if (pending_signals_for_state()) {
  426. #ifdef SIGNAL_DEBUG
  427. dbg() << "Signal: Resuming stopped " << *this << " to deliver signal " << signal;
  428. #endif
  429. resume_from_stopped();
  430. }
  431. } else {
  432. ScopedSpinLock block_lock(m_block_lock);
  433. #ifdef SIGNAL_DEBUG
  434. dbg() << "Signal: Unblocking " << *this << " to deliver signal " << signal;
  435. #endif
  436. unblock(signal);
  437. }
  438. }
  439. u32 Thread::update_signal_mask(u32 signal_mask)
  440. {
  441. ScopedSpinLock lock(g_scheduler_lock);
  442. auto previous_signal_mask = m_signal_mask;
  443. m_signal_mask = signal_mask;
  444. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  445. return previous_signal_mask;
  446. }
  447. u32 Thread::signal_mask() const
  448. {
  449. ScopedSpinLock lock(g_scheduler_lock);
  450. return m_signal_mask;
  451. }
  452. u32 Thread::signal_mask_block(sigset_t signal_set, bool block)
  453. {
  454. ScopedSpinLock lock(g_scheduler_lock);
  455. auto previous_signal_mask = m_signal_mask;
  456. if (block)
  457. m_signal_mask &= ~signal_set;
  458. else
  459. m_signal_mask |= signal_set;
  460. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  461. return previous_signal_mask;
  462. }
  463. void Thread::clear_signals()
  464. {
  465. ScopedSpinLock lock(g_scheduler_lock);
  466. m_signal_mask = 0;
  467. m_pending_signals = 0;
  468. m_have_any_unmasked_pending_signals.store(false, AK::memory_order_release);
  469. }
  470. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  471. // thread into a state where the signal handler must be
  472. // invoked immediately, otherwise it will continue to fault.
  473. // This function should be used in an exception handler to
  474. // ensure that when the thread resumes, it's executing in
  475. // the appropriate signal handler.
  476. void Thread::send_urgent_signal_to_self(u8 signal)
  477. {
  478. ASSERT(Thread::current() == this);
  479. DispatchSignalResult result;
  480. {
  481. ScopedSpinLock lock(g_scheduler_lock);
  482. result = dispatch_signal(signal);
  483. }
  484. if (result == DispatchSignalResult::Yield)
  485. yield_without_holding_big_lock();
  486. }
  487. DispatchSignalResult Thread::dispatch_one_pending_signal()
  488. {
  489. ASSERT(m_lock.own_lock());
  490. u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
  491. if (signal_candidates == 0)
  492. return DispatchSignalResult::Continue;
  493. u8 signal = 1;
  494. for (; signal < 32; ++signal) {
  495. if (signal_candidates & (1 << (signal - 1))) {
  496. break;
  497. }
  498. }
  499. return dispatch_signal(signal);
  500. }
  501. DispatchSignalResult Thread::try_dispatch_one_pending_signal(u8 signal)
  502. {
  503. ASSERT(signal != 0);
  504. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  505. ScopedSpinLock lock(m_lock);
  506. u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
  507. if (!(signal_candidates & (1 << (signal - 1))))
  508. return DispatchSignalResult::Continue;
  509. return dispatch_signal(signal);
  510. }
  511. enum class DefaultSignalAction {
  512. Terminate,
  513. Ignore,
  514. DumpCore,
  515. Stop,
  516. Continue,
  517. };
  518. static DefaultSignalAction default_signal_action(u8 signal)
  519. {
  520. ASSERT(signal && signal < NSIG);
  521. switch (signal) {
  522. case SIGHUP:
  523. case SIGINT:
  524. case SIGKILL:
  525. case SIGPIPE:
  526. case SIGALRM:
  527. case SIGUSR1:
  528. case SIGUSR2:
  529. case SIGVTALRM:
  530. case SIGSTKFLT:
  531. case SIGIO:
  532. case SIGPROF:
  533. case SIGTERM:
  534. return DefaultSignalAction::Terminate;
  535. case SIGCHLD:
  536. case SIGURG:
  537. case SIGWINCH:
  538. case SIGINFO:
  539. return DefaultSignalAction::Ignore;
  540. case SIGQUIT:
  541. case SIGILL:
  542. case SIGTRAP:
  543. case SIGABRT:
  544. case SIGBUS:
  545. case SIGFPE:
  546. case SIGSEGV:
  547. case SIGXCPU:
  548. case SIGXFSZ:
  549. case SIGSYS:
  550. return DefaultSignalAction::DumpCore;
  551. case SIGCONT:
  552. return DefaultSignalAction::Continue;
  553. case SIGSTOP:
  554. case SIGTSTP:
  555. case SIGTTIN:
  556. case SIGTTOU:
  557. return DefaultSignalAction::Stop;
  558. }
  559. ASSERT_NOT_REACHED();
  560. }
  561. bool Thread::should_ignore_signal(u8 signal) const
  562. {
  563. ASSERT(signal < 32);
  564. auto& action = m_signal_action_data[signal];
  565. if (action.handler_or_sigaction.is_null())
  566. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  567. if (action.handler_or_sigaction.as_ptr() == SIG_IGN)
  568. return true;
  569. return false;
  570. }
  571. bool Thread::has_signal_handler(u8 signal) const
  572. {
  573. ASSERT(signal < 32);
  574. auto& action = m_signal_action_data[signal];
  575. return !action.handler_or_sigaction.is_null();
  576. }
  577. static bool push_value_on_user_stack(u32* stack, u32 data)
  578. {
  579. *stack -= 4;
  580. return copy_to_user((u32*)*stack, &data);
  581. }
  582. void Thread::resume_from_stopped()
  583. {
  584. ASSERT(is_stopped());
  585. ASSERT(m_stop_state != State::Invalid);
  586. ASSERT(g_scheduler_lock.own_lock());
  587. if (m_stop_state == Blocked) {
  588. ScopedSpinLock block_lock(m_block_lock);
  589. if (m_blocker) {
  590. // Hasn't been unblocked yet
  591. set_state(Blocked, 0);
  592. } else {
  593. // Was unblocked while stopped
  594. set_state(Runnable);
  595. }
  596. } else {
  597. set_state(m_stop_state, 0);
  598. }
  599. }
  600. DispatchSignalResult Thread::dispatch_signal(u8 signal)
  601. {
  602. ASSERT_INTERRUPTS_DISABLED();
  603. ASSERT(g_scheduler_lock.own_lock());
  604. ASSERT(signal > 0 && signal <= 32);
  605. ASSERT(process().is_user_process());
  606. ASSERT(this == Thread::current());
  607. #ifdef SIGNAL_DEBUG
  608. klog() << "signal: dispatch signal " << signal << " to " << *this << " state: " << state_string();
  609. #endif
  610. if (m_state == Invalid || !is_initialized()) {
  611. // Thread has barely been created, we need to wait until it is
  612. // at least in Runnable state and is_initialized() returns true,
  613. // which indicates that it is fully set up an we actually have
  614. // a register state on the stack that we can modify
  615. return DispatchSignalResult::Deferred;
  616. }
  617. // if (is_stopped() && signal != SIGCONT && signal != SIGKILL && signal != SIGTRAP) {
  618. //#ifdef SIGNAL_DEBUG
  619. // klog() << "signal: " << *this << " is stopped, will handle signal " << signal << " when resumed";
  620. //#endif
  621. // return DispatchSignalResult::Deferred;
  622. // }
  623. // if (is_blocked()) {
  624. //#ifdef SIGNAL_DEBUG
  625. // klog() << "signal: " << *this << " is blocked, will handle signal " << signal << " when unblocking";
  626. //#endif
  627. // return DispatchSignalResult::Deferred;
  628. // }
  629. auto& action = m_signal_action_data[signal];
  630. // FIXME: Implement SA_SIGINFO signal handlers.
  631. ASSERT(!(action.flags & SA_SIGINFO));
  632. // Mark this signal as handled.
  633. m_pending_signals &= ~(1 << (signal - 1));
  634. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  635. auto& process = this->process();
  636. auto tracer = process.tracer();
  637. if (signal == SIGSTOP || (tracer && default_signal_action(signal) == DefaultSignalAction::DumpCore)) {
  638. #ifdef SIGNAL_DEBUG
  639. dbg() << "signal: signal " << signal << " stopping thread " << *this;
  640. #endif
  641. set_state(State::Stopped, signal);
  642. return DispatchSignalResult::Yield;
  643. }
  644. if (signal == SIGCONT) {
  645. #ifdef SIGNAL_DEBUG
  646. dbg() << "signal: SIGCONT resuming " << *this;
  647. #endif
  648. } else {
  649. if (tracer) {
  650. // when a thread is traced, it should be stopped whenever it receives a signal
  651. // the tracer is notified of this by using waitpid()
  652. // only "pending signals" from the tracer are sent to the tracee
  653. if (!tracer->has_pending_signal(signal)) {
  654. #ifdef SIGNAL_DEBUG
  655. dbg() << "signal: " << signal << " stopping " << *this << " for tracer";
  656. #endif
  657. set_state(Stopped, signal);
  658. return DispatchSignalResult::Yield;
  659. }
  660. tracer->unset_signal(signal);
  661. }
  662. }
  663. auto handler_vaddr = action.handler_or_sigaction;
  664. if (handler_vaddr.is_null()) {
  665. switch (default_signal_action(signal)) {
  666. case DefaultSignalAction::Stop:
  667. set_state(Stopped, signal);
  668. return DispatchSignalResult::Yield;
  669. case DefaultSignalAction::DumpCore:
  670. process.set_dump_core(true);
  671. process.for_each_thread([](auto& thread) {
  672. thread.set_dump_backtrace_on_finalization();
  673. return IterationDecision::Continue;
  674. });
  675. [[fallthrough]];
  676. case DefaultSignalAction::Terminate:
  677. m_process->terminate_due_to_signal(signal);
  678. return DispatchSignalResult::Terminate;
  679. case DefaultSignalAction::Ignore:
  680. ASSERT_NOT_REACHED();
  681. case DefaultSignalAction::Continue:
  682. return DispatchSignalResult::Continue;
  683. }
  684. ASSERT_NOT_REACHED();
  685. }
  686. if (handler_vaddr.as_ptr() == SIG_IGN) {
  687. #ifdef SIGNAL_DEBUG
  688. klog() << "signal: " << *this << " ignored signal " << signal;
  689. #endif
  690. return DispatchSignalResult::Continue;
  691. }
  692. ProcessPagingScope paging_scope(m_process);
  693. u32 old_signal_mask = m_signal_mask;
  694. u32 new_signal_mask = action.mask;
  695. if (action.flags & SA_NODEFER)
  696. new_signal_mask &= ~(1 << (signal - 1));
  697. else
  698. new_signal_mask |= 1 << (signal - 1);
  699. m_signal_mask |= new_signal_mask;
  700. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  701. auto setup_stack = [&](RegisterState& state) {
  702. u32* stack = &state.userspace_esp;
  703. u32 old_esp = *stack;
  704. u32 ret_eip = state.eip;
  705. u32 ret_eflags = state.eflags;
  706. #ifdef SIGNAL_DEBUG
  707. klog() << "signal: setting up user stack to return to eip: " << String::format("%p", ret_eip) << " esp: " << String::format("%p", old_esp);
  708. #endif
  709. // Align the stack to 16 bytes.
  710. // Note that we push 56 bytes (4 * 14) on to the stack,
  711. // so we need to account for this here.
  712. u32 stack_alignment = (*stack - 56) % 16;
  713. *stack -= stack_alignment;
  714. push_value_on_user_stack(stack, ret_eflags);
  715. push_value_on_user_stack(stack, ret_eip);
  716. push_value_on_user_stack(stack, state.eax);
  717. push_value_on_user_stack(stack, state.ecx);
  718. push_value_on_user_stack(stack, state.edx);
  719. push_value_on_user_stack(stack, state.ebx);
  720. push_value_on_user_stack(stack, old_esp);
  721. push_value_on_user_stack(stack, state.ebp);
  722. push_value_on_user_stack(stack, state.esi);
  723. push_value_on_user_stack(stack, state.edi);
  724. // PUSH old_signal_mask
  725. push_value_on_user_stack(stack, old_signal_mask);
  726. push_value_on_user_stack(stack, signal);
  727. push_value_on_user_stack(stack, handler_vaddr.get());
  728. push_value_on_user_stack(stack, 0); //push fake return address
  729. ASSERT((*stack % 16) == 0);
  730. };
  731. // We now place the thread state on the userspace stack.
  732. // Note that we use a RegisterState.
  733. // Conversely, when the thread isn't blocking the RegisterState may not be
  734. // valid (fork, exec etc) but the tss will, so we use that instead.
  735. auto& regs = get_register_dump_from_stack();
  736. setup_stack(regs);
  737. regs.eip = g_return_to_ring3_from_signal_trampoline.get();
  738. #ifdef SIGNAL_DEBUG
  739. klog() << "signal: Okay, " << *this << " {" << state_string() << "} has been primed with signal handler " << String::format("%w", m_tss.cs) << ":" << String::format("%x", m_tss.eip) << " to deliver " << signal;
  740. #endif
  741. return DispatchSignalResult::Continue;
  742. }
  743. void Thread::set_default_signal_dispositions()
  744. {
  745. // FIXME: Set up all the right default actions. See signal(7).
  746. memset(&m_signal_action_data, 0, sizeof(m_signal_action_data));
  747. m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress(SIG_IGN);
  748. m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress(SIG_IGN);
  749. }
  750. bool Thread::push_value_on_stack(FlatPtr value)
  751. {
  752. m_tss.esp -= 4;
  753. FlatPtr* stack_ptr = (FlatPtr*)m_tss.esp;
  754. return copy_to_user(stack_ptr, &value);
  755. }
  756. RegisterState& Thread::get_register_dump_from_stack()
  757. {
  758. return *(RegisterState*)(kernel_stack_top() - sizeof(RegisterState));
  759. }
  760. KResultOr<u32> Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment, Vector<AuxiliaryValue> auxiliary_values)
  761. {
  762. auto* region = m_process->allocate_region(VirtualAddress(), default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, false);
  763. if (!region)
  764. return KResult(-ENOMEM);
  765. region->set_stack(true);
  766. FlatPtr new_esp = region->vaddr().offset(default_userspace_stack_size).get();
  767. auto push_on_new_stack = [&new_esp](u32 value) {
  768. new_esp -= 4;
  769. Userspace<u32*> stack_ptr = new_esp;
  770. return copy_to_user(stack_ptr, &value);
  771. };
  772. auto push_aux_value_on_new_stack = [&new_esp](auxv_t value) {
  773. new_esp -= sizeof(auxv_t);
  774. Userspace<auxv_t*> stack_ptr = new_esp;
  775. return copy_to_user(stack_ptr, &value);
  776. };
  777. auto push_string_on_new_stack = [&new_esp](const String& string) {
  778. new_esp -= round_up_to_power_of_two(string.length() + 1, 4);
  779. Userspace<u32*> stack_ptr = new_esp;
  780. return copy_to_user(stack_ptr, string.characters(), string.length() + 1);
  781. };
  782. Vector<FlatPtr> argv_entries;
  783. for (auto& argument : arguments) {
  784. push_string_on_new_stack(argument);
  785. argv_entries.append(new_esp);
  786. }
  787. Vector<FlatPtr> env_entries;
  788. for (auto& variable : environment) {
  789. push_string_on_new_stack(variable);
  790. env_entries.append(new_esp);
  791. }
  792. for (auto& value : auxiliary_values) {
  793. if (!value.optional_string.is_empty()) {
  794. push_string_on_new_stack(value.optional_string);
  795. value.auxv.a_un.a_ptr = (void*)new_esp;
  796. }
  797. }
  798. for (ssize_t i = auxiliary_values.size() - 1; i >= 0; --i) {
  799. auto& value = auxiliary_values[i];
  800. push_aux_value_on_new_stack(value.auxv);
  801. }
  802. push_on_new_stack(0);
  803. for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
  804. push_on_new_stack(env_entries[i]);
  805. FlatPtr envp = new_esp;
  806. push_on_new_stack(0);
  807. for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
  808. push_on_new_stack(argv_entries[i]);
  809. FlatPtr argv = new_esp;
  810. // NOTE: The stack needs to be 16-byte aligned.
  811. new_esp -= new_esp % 16;
  812. push_on_new_stack((FlatPtr)envp);
  813. push_on_new_stack((FlatPtr)argv);
  814. push_on_new_stack((FlatPtr)argv_entries.size());
  815. push_on_new_stack(0);
  816. return new_esp;
  817. }
  818. RefPtr<Thread> Thread::clone(Process& process)
  819. {
  820. auto clone = adopt(*new Thread(process));
  821. memcpy(clone->m_signal_action_data, m_signal_action_data, sizeof(m_signal_action_data));
  822. clone->m_signal_mask = m_signal_mask;
  823. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  824. clone->m_thread_specific_data = m_thread_specific_data;
  825. clone->m_thread_specific_region_size = m_thread_specific_region_size;
  826. return clone;
  827. }
  828. void Thread::set_state(State new_state, u8 stop_signal)
  829. {
  830. State previous_state;
  831. ASSERT(g_scheduler_lock.own_lock());
  832. if (new_state == m_state)
  833. return;
  834. {
  835. ScopedSpinLock thread_lock(m_lock);
  836. previous_state = m_state;
  837. if (previous_state == Invalid) {
  838. // If we were *just* created, we may have already pending signals
  839. if (has_unmasked_pending_signals()) {
  840. dbg() << "Dispatch pending signals to new thread " << *this;
  841. dispatch_one_pending_signal();
  842. }
  843. }
  844. m_state = new_state;
  845. #ifdef THREAD_DEBUG
  846. dbg() << "Set Thread " << *this << " state to " << state_string();
  847. #endif
  848. }
  849. if (m_process->pid() != 0) {
  850. update_state_for_thread(previous_state);
  851. ASSERT(g_scheduler_data->has_thread(*this));
  852. }
  853. if (previous_state == Stopped) {
  854. m_stop_state = State::Invalid;
  855. auto& process = this->process();
  856. if (process.set_stopped(false) == true) {
  857. process.for_each_thread([&](auto& thread) {
  858. if (&thread == this || !thread.is_stopped())
  859. return IterationDecision::Continue;
  860. #ifdef THREAD_DEBUG
  861. dbg() << "Resuming peer thread " << thread;
  862. #endif
  863. thread.resume_from_stopped();
  864. return IterationDecision::Continue;
  865. });
  866. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Continued);
  867. }
  868. }
  869. if (m_state == Stopped) {
  870. // We don't want to restore to Running state, only Runnable!
  871. m_stop_state = previous_state != Running ? previous_state : Runnable;
  872. auto& process = this->process();
  873. if (process.set_stopped(true) == false) {
  874. process.for_each_thread([&](auto& thread) {
  875. if (&thread == this || thread.is_stopped())
  876. return IterationDecision::Continue;
  877. #ifdef THREAD_DEBUG
  878. dbg() << "Stopping peer thread " << thread;
  879. #endif
  880. thread.set_state(Stopped, stop_signal);
  881. return IterationDecision::Continue;
  882. });
  883. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Stopped, stop_signal);
  884. }
  885. } else if (m_state == Dying) {
  886. ASSERT(previous_state != Blocked);
  887. if (this != Thread::current() && is_finalizable()) {
  888. // Some other thread set this thread to Dying, notify the
  889. // finalizer right away as it can be cleaned up now
  890. Scheduler::notify_finalizer();
  891. }
  892. }
  893. }
  894. void Thread::update_state_for_thread(Thread::State previous_state)
  895. {
  896. ASSERT_INTERRUPTS_DISABLED();
  897. ASSERT(g_scheduler_data);
  898. ASSERT(g_scheduler_lock.own_lock());
  899. auto& previous_list = g_scheduler_data->thread_list_for_state(previous_state);
  900. auto& list = g_scheduler_data->thread_list_for_state(state());
  901. if (&previous_list != &list) {
  902. previous_list.remove(*this);
  903. }
  904. if (list.contains(*this))
  905. return;
  906. list.append(*this);
  907. }
  908. String Thread::backtrace()
  909. {
  910. return backtrace_impl();
  911. }
  912. struct RecognizedSymbol {
  913. u32 address;
  914. const KernelSymbol* symbol { nullptr };
  915. };
  916. static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder, Process::ELFBundle* elf_bundle)
  917. {
  918. if (!symbol.address)
  919. return false;
  920. bool mask_kernel_addresses = !process.is_superuser();
  921. if (!symbol.symbol) {
  922. if (!is_user_address(VirtualAddress(symbol.address))) {
  923. builder.append("0xdeadc0de\n");
  924. } else {
  925. if (elf_bundle && elf_bundle->elf_loader->has_symbols())
  926. builder.appendf("%p %s\n", symbol.address, elf_bundle->elf_loader->symbolicate(symbol.address).characters());
  927. else
  928. builder.appendf("%p\n", symbol.address);
  929. }
  930. return true;
  931. }
  932. unsigned offset = symbol.address - symbol.symbol->address;
  933. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
  934. builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
  935. } else {
  936. builder.appendf("%p %s +%u\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address, demangle(symbol.symbol->name).characters(), offset);
  937. }
  938. return true;
  939. }
  940. String Thread::backtrace_impl()
  941. {
  942. Vector<RecognizedSymbol, 128> recognized_symbols;
  943. auto& process = const_cast<Process&>(this->process());
  944. OwnPtr<Process::ELFBundle> elf_bundle;
  945. if (!Processor::current().in_irq()) {
  946. // If we're handling IRQs we can't really safely symbolicate
  947. elf_bundle = process.elf_bundle();
  948. }
  949. auto stack_trace = Processor::capture_stack_trace(*this);
  950. ASSERT(!g_scheduler_lock.own_lock());
  951. ProcessPagingScope paging_scope(process);
  952. for (auto& frame : stack_trace) {
  953. if (is_user_range(VirtualAddress(frame), sizeof(FlatPtr) * 2)) {
  954. recognized_symbols.append({ frame, symbolicate_kernel_address(frame) });
  955. } else {
  956. recognized_symbols.append({ frame, symbolicate_kernel_address(frame) });
  957. }
  958. }
  959. StringBuilder builder;
  960. for (auto& symbol : recognized_symbols) {
  961. if (!symbolicate(symbol, process, builder, elf_bundle.ptr()))
  962. break;
  963. }
  964. return builder.to_string();
  965. }
  966. Vector<FlatPtr> Thread::raw_backtrace(FlatPtr ebp, FlatPtr eip) const
  967. {
  968. InterruptDisabler disabler;
  969. auto& process = const_cast<Process&>(this->process());
  970. ProcessPagingScope paging_scope(process);
  971. Vector<FlatPtr, Profiling::max_stack_frame_count> backtrace;
  972. backtrace.append(eip);
  973. FlatPtr stack_ptr_copy;
  974. FlatPtr stack_ptr = (FlatPtr)ebp;
  975. while (stack_ptr) {
  976. void* fault_at;
  977. if (!safe_memcpy(&stack_ptr_copy, (void*)stack_ptr, sizeof(FlatPtr), fault_at))
  978. break;
  979. FlatPtr retaddr;
  980. if (!safe_memcpy(&retaddr, (void*)(stack_ptr + sizeof(FlatPtr)), sizeof(FlatPtr), fault_at))
  981. break;
  982. backtrace.append(retaddr);
  983. if (backtrace.size() == Profiling::max_stack_frame_count)
  984. break;
  985. stack_ptr = stack_ptr_copy;
  986. }
  987. return backtrace;
  988. }
  989. KResult Thread::make_thread_specific_region(Badge<Process>)
  990. {
  991. // The process may not require a TLS region
  992. if (!process().m_master_tls_region)
  993. return KSuccess;
  994. size_t thread_specific_region_alignment = max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  995. m_thread_specific_region_size = align_up_to(process().m_master_tls_size, thread_specific_region_alignment) + sizeof(ThreadSpecificData);
  996. auto* region = process().allocate_region({}, m_thread_specific_region_size, "Thread-specific", PROT_READ | PROT_WRITE, true);
  997. if (!region)
  998. return KResult(-ENOMEM);
  999. SmapDisabler disabler;
  1000. auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment)).as_ptr();
  1001. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  1002. m_thread_specific_data = VirtualAddress(thread_specific_data);
  1003. thread_specific_data->self = thread_specific_data;
  1004. if (process().m_master_tls_size)
  1005. memcpy(thread_local_storage, process().m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), process().m_master_tls_size);
  1006. return KSuccess;
  1007. }
  1008. const LogStream& operator<<(const LogStream& stream, const Thread& value)
  1009. {
  1010. return stream << value.process().name() << "(" << value.pid().value() << ":" << value.tid().value() << ")";
  1011. }
  1012. RefPtr<Thread> Thread::from_tid(ThreadID tid)
  1013. {
  1014. RefPtr<Thread> found_thread;
  1015. ScopedSpinLock lock(g_scheduler_lock);
  1016. Thread::for_each([&](auto& thread) {
  1017. if (thread.tid() == tid) {
  1018. found_thread = &thread;
  1019. return IterationDecision::Break;
  1020. }
  1021. return IterationDecision::Continue;
  1022. });
  1023. return found_thread;
  1024. }
  1025. void Thread::reset_fpu_state()
  1026. {
  1027. memcpy(m_fpu_state, &Processor::current().clean_fpu_state(), sizeof(FPUState));
  1028. }
  1029. bool Thread::should_be_stopped() const
  1030. {
  1031. return process().is_stopped();
  1032. }
  1033. }