Thread.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Demangle.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Time.h>
  10. #include <Kernel/Arch/x86/CPU.h>
  11. #include <Kernel/Arch/x86/SmapDisabler.h>
  12. #include <Kernel/Debug.h>
  13. #include <Kernel/FileSystem/FileDescription.h>
  14. #include <Kernel/KSyms.h>
  15. #include <Kernel/Panic.h>
  16. #include <Kernel/PerformanceEventBuffer.h>
  17. #include <Kernel/Process.h>
  18. #include <Kernel/Scheduler.h>
  19. #include <Kernel/Thread.h>
  20. #include <Kernel/ThreadTracer.h>
  21. #include <Kernel/TimerQueue.h>
  22. #include <Kernel/VM/MemoryManager.h>
  23. #include <Kernel/VM/PageDirectory.h>
  24. #include <Kernel/VM/ProcessPagingScope.h>
  25. #include <LibC/signal_numbers.h>
  26. namespace Kernel {
  27. SpinLock<u8> Thread::g_tid_map_lock;
  28. READONLY_AFTER_INIT HashMap<ThreadID, Thread*>* Thread::g_tid_map;
  29. UNMAP_AFTER_INIT void Thread::initialize()
  30. {
  31. g_tid_map = new HashMap<ThreadID, Thread*>();
  32. }
  33. KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> process)
  34. {
  35. auto kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, {}, Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  36. if (!kernel_stack_region)
  37. return ENOMEM;
  38. kernel_stack_region->set_stack(true);
  39. auto block_timer = adopt_ref_if_nonnull(new Timer());
  40. if (!block_timer)
  41. return ENOMEM;
  42. auto thread = adopt_ref_if_nonnull(new Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull()));
  43. if (!thread)
  44. return ENOMEM;
  45. return thread.release_nonnull();
  46. }
  47. Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer)
  48. : m_process(move(process))
  49. , m_kernel_stack_region(move(kernel_stack_region))
  50. , m_name(m_process->name())
  51. , m_block_timer(block_timer)
  52. {
  53. bool is_first_thread = m_process->add_thread(*this);
  54. if (is_first_thread) {
  55. // First thread gets TID == PID
  56. m_tid = m_process->pid().value();
  57. } else {
  58. m_tid = Process::allocate_pid().value();
  59. }
  60. {
  61. // FIXME: Go directly to KString
  62. auto string = String::formatted("Kernel stack (thread {})", m_tid.value());
  63. m_kernel_stack_region->set_name(KString::try_create(string));
  64. }
  65. {
  66. ScopedSpinLock lock(g_tid_map_lock);
  67. auto result = g_tid_map->set(m_tid, this);
  68. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  69. }
  70. if constexpr (THREAD_DEBUG)
  71. dbgln("Created new thread {}({}:{})", m_process->name(), m_process->pid().value(), m_tid.value());
  72. m_fpu_state = (FPUState*)kmalloc_aligned<16>(sizeof(FPUState));
  73. reset_fpu_state();
  74. m_tss.iomapbase = sizeof(TSS32);
  75. // Only IF is set when a process boots.
  76. m_tss.eflags = 0x0202;
  77. if (m_process->is_kernel_process()) {
  78. m_tss.cs = GDT_SELECTOR_CODE0;
  79. m_tss.ds = GDT_SELECTOR_DATA0;
  80. m_tss.es = GDT_SELECTOR_DATA0;
  81. m_tss.fs = GDT_SELECTOR_PROC;
  82. m_tss.ss = GDT_SELECTOR_DATA0;
  83. m_tss.gs = 0;
  84. } else {
  85. m_tss.cs = GDT_SELECTOR_CODE3 | 3;
  86. m_tss.ds = GDT_SELECTOR_DATA3 | 3;
  87. m_tss.es = GDT_SELECTOR_DATA3 | 3;
  88. m_tss.fs = GDT_SELECTOR_DATA3 | 3;
  89. m_tss.ss = GDT_SELECTOR_DATA3 | 3;
  90. m_tss.gs = GDT_SELECTOR_TLS | 3;
  91. }
  92. m_tss.cr3 = m_process->space().page_directory().cr3();
  93. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  94. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
  95. if (m_process->is_kernel_process()) {
  96. m_tss.esp = m_tss.esp0 = m_kernel_stack_top;
  97. } else {
  98. // Ring 3 processes get a separate stack for ring 0.
  99. // The ring 3 stack will be assigned by exec().
  100. m_tss.ss0 = GDT_SELECTOR_DATA0;
  101. m_tss.esp0 = m_kernel_stack_top;
  102. }
  103. // We need to add another reference if we could successfully create
  104. // all the resources needed for this thread. The reason for this is that
  105. // we don't want to delete this thread after dropping the reference,
  106. // it may still be running or scheduled to be run.
  107. // The finalizer is responsible for dropping this reference once this
  108. // thread is ready to be cleaned up.
  109. ref();
  110. }
  111. Thread::~Thread()
  112. {
  113. {
  114. // We need to explicitly remove ourselves from the thread list
  115. // here. We may get pre-empted in the middle of destructing this
  116. // thread, which causes problems if the thread list is iterated.
  117. // Specifically, if this is the last thread of a process, checking
  118. // block conditions would access m_process, which would be in
  119. // the middle of being destroyed.
  120. ScopedSpinLock lock(g_scheduler_lock);
  121. VERIFY(!m_process_thread_list_node.is_in_list());
  122. // We shouldn't be queued
  123. VERIFY(m_runnable_priority < 0);
  124. }
  125. {
  126. ScopedSpinLock lock(g_tid_map_lock);
  127. auto result = g_tid_map->remove(m_tid);
  128. VERIFY(result);
  129. }
  130. }
  131. void Thread::unblock_from_blocker(Blocker& blocker)
  132. {
  133. auto do_unblock = [&]() {
  134. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  135. ScopedSpinLock block_lock(m_block_lock);
  136. if (m_blocker != &blocker)
  137. return;
  138. if (!should_be_stopped() && !is_stopped())
  139. unblock();
  140. };
  141. if (Processor::current().in_irq()) {
  142. Processor::current().deferred_call_queue([do_unblock = move(do_unblock), self = make_weak_ptr()]() {
  143. if (auto this_thread = self.strong_ref())
  144. do_unblock();
  145. });
  146. } else {
  147. do_unblock();
  148. }
  149. }
  150. void Thread::unblock(u8 signal)
  151. {
  152. VERIFY(!Processor::current().in_irq());
  153. VERIFY(g_scheduler_lock.own_lock());
  154. VERIFY(m_block_lock.own_lock());
  155. if (m_state != Thread::Blocked)
  156. return;
  157. VERIFY(m_blocker);
  158. if (signal != 0) {
  159. if (is_handling_page_fault()) {
  160. // Don't let signals unblock threads that are blocked inside a page fault handler.
  161. // This prevents threads from EINTR'ing the inode read in an inode page fault.
  162. // FIXME: There's probably a better way to solve this.
  163. return;
  164. }
  165. if (!m_blocker->can_be_interrupted() && !m_should_die)
  166. return;
  167. m_blocker->set_interrupted_by_signal(signal);
  168. }
  169. m_blocker = nullptr;
  170. if (Thread::current() == this) {
  171. set_state(Thread::Running);
  172. return;
  173. }
  174. VERIFY(m_state != Thread::Runnable && m_state != Thread::Running);
  175. set_state(Thread::Runnable);
  176. }
  177. void Thread::set_should_die()
  178. {
  179. if (m_should_die) {
  180. dbgln("{} Should already die", *this);
  181. return;
  182. }
  183. ScopedCritical critical;
  184. // Remember that we should die instead of returning to
  185. // the userspace.
  186. ScopedSpinLock lock(g_scheduler_lock);
  187. m_should_die = true;
  188. // NOTE: Even the current thread can technically be in "Stopped"
  189. // state! This is the case when another thread sent a SIGSTOP to
  190. // it while it was running and it calls e.g. exit() before
  191. // the scheduler gets involved again.
  192. if (is_stopped()) {
  193. // If we were stopped, we need to briefly resume so that
  194. // the kernel stacks can clean up. We won't ever return back
  195. // to user mode, though
  196. VERIFY(!process().is_stopped());
  197. resume_from_stopped();
  198. }
  199. if (is_blocked()) {
  200. ScopedSpinLock block_lock(m_block_lock);
  201. if (m_blocker) {
  202. // We're blocked in the kernel.
  203. m_blocker->set_interrupted_by_death();
  204. unblock();
  205. }
  206. }
  207. }
  208. void Thread::die_if_needed()
  209. {
  210. VERIFY(Thread::current() == this);
  211. if (!m_should_die)
  212. return;
  213. u32 unlock_count;
  214. [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
  215. dbgln_if(THREAD_DEBUG, "Thread {} is dying", *this);
  216. {
  217. ScopedSpinLock lock(g_scheduler_lock);
  218. // It's possible that we don't reach the code after this block if the
  219. // scheduler is invoked and FinalizerTask cleans up this thread, however
  220. // that doesn't matter because we're trying to invoke the scheduler anyway
  221. set_state(Thread::Dying);
  222. }
  223. ScopedCritical critical;
  224. // Flag a context switch. Because we're in a critical section,
  225. // Scheduler::yield will actually only mark a pending context switch
  226. // Simply leaving the critical section would not necessarily trigger
  227. // a switch.
  228. Scheduler::yield();
  229. // Now leave the critical section so that we can also trigger the
  230. // actual context switch
  231. u32 prev_flags;
  232. Processor::current().clear_critical(prev_flags, false);
  233. dbgln("die_if_needed returned from clear_critical!!! in irq: {}", Processor::current().in_irq());
  234. // We should never get here, but the scoped scheduler lock
  235. // will be released by Scheduler::context_switch again
  236. VERIFY_NOT_REACHED();
  237. }
  238. void Thread::exit(void* exit_value)
  239. {
  240. VERIFY(Thread::current() == this);
  241. m_join_condition.thread_did_exit(exit_value);
  242. set_should_die();
  243. u32 unlock_count;
  244. [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
  245. if (m_thread_specific_range.has_value()) {
  246. auto* region = process().space().find_region_from_range(m_thread_specific_range.value());
  247. VERIFY(region);
  248. if (!process().space().deallocate_region(*region))
  249. dbgln("Failed to unmap TLS range, exiting thread anyway.");
  250. }
  251. die_if_needed();
  252. }
  253. void Thread::yield_while_not_holding_big_lock()
  254. {
  255. VERIFY(!g_scheduler_lock.own_lock());
  256. u32 prev_flags;
  257. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  258. Scheduler::yield();
  259. // NOTE: We may be on a different CPU now!
  260. Processor::current().restore_critical(prev_crit, prev_flags);
  261. }
  262. void Thread::yield_without_holding_big_lock()
  263. {
  264. VERIFY(!g_scheduler_lock.own_lock());
  265. u32 lock_count_to_restore = 0;
  266. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  267. // NOTE: Even though we call Scheduler::yield here, unless we happen
  268. // to be outside of a critical section, the yield will be postponed
  269. // until leaving it in relock_process.
  270. Scheduler::yield();
  271. relock_process(previous_locked, lock_count_to_restore);
  272. }
  273. void Thread::donate_without_holding_big_lock(RefPtr<Thread>& thread, const char* reason)
  274. {
  275. VERIFY(!g_scheduler_lock.own_lock());
  276. u32 lock_count_to_restore = 0;
  277. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  278. // NOTE: Even though we call Scheduler::yield here, unless we happen
  279. // to be outside of a critical section, the yield will be postponed
  280. // until leaving it in relock_process.
  281. Scheduler::donate_to(thread, reason);
  282. relock_process(previous_locked, lock_count_to_restore);
  283. }
  284. LockMode Thread::unlock_process_if_locked(u32& lock_count_to_restore)
  285. {
  286. return process().big_lock().force_unlock_if_locked(lock_count_to_restore);
  287. }
  288. void Thread::relock_process(LockMode previous_locked, u32 lock_count_to_restore)
  289. {
  290. // Clearing the critical section may trigger the context switch
  291. // flagged by calling Scheduler::donate_to or Scheduler::yield
  292. // above. We have to do it this way because we intentionally
  293. // leave the critical section here to be able to switch contexts.
  294. u32 prev_flags;
  295. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  296. // CONTEXT SWITCH HAPPENS HERE!
  297. // NOTE: We may be on a different CPU now!
  298. Processor::current().restore_critical(prev_crit, prev_flags);
  299. if (previous_locked != LockMode::Unlocked) {
  300. // We've unblocked, relock the process if needed and carry on.
  301. process().big_lock().restore_lock(previous_locked, lock_count_to_restore);
  302. }
  303. }
  304. auto Thread::sleep(clockid_t clock_id, const Time& duration, Time* remaining_time) -> BlockResult
  305. {
  306. VERIFY(state() == Thread::Running);
  307. return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(false, &duration, nullptr, clock_id), remaining_time);
  308. }
  309. auto Thread::sleep_until(clockid_t clock_id, const Time& deadline) -> BlockResult
  310. {
  311. VERIFY(state() == Thread::Running);
  312. return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(true, &deadline, nullptr, clock_id));
  313. }
  314. const char* Thread::state_string() const
  315. {
  316. switch (state()) {
  317. case Thread::Invalid:
  318. return "Invalid";
  319. case Thread::Runnable:
  320. return "Runnable";
  321. case Thread::Running:
  322. return "Running";
  323. case Thread::Dying:
  324. return "Dying";
  325. case Thread::Dead:
  326. return "Dead";
  327. case Thread::Stopped:
  328. return "Stopped";
  329. case Thread::Blocked: {
  330. ScopedSpinLock block_lock(m_block_lock);
  331. VERIFY(m_blocker != nullptr);
  332. return m_blocker->state_string();
  333. }
  334. }
  335. PANIC("Thread::state_string(): Invalid state: {}", (int)state());
  336. }
  337. void Thread::finalize()
  338. {
  339. VERIFY(Thread::current() == g_finalizer);
  340. VERIFY(Thread::current() != this);
  341. #if LOCK_DEBUG
  342. VERIFY(!m_lock.own_lock());
  343. if (lock_count() > 0) {
  344. dbgln("Thread {} leaking {} Locks!", *this, lock_count());
  345. ScopedSpinLock list_lock(m_holding_locks_lock);
  346. for (auto& info : m_holding_locks_list) {
  347. const auto& location = info.source_location;
  348. dbgln(" - Lock: \"{}\" @ {} locked in function \"{}\" at \"{}:{}\" with a count of: {}", info.lock->name(), info.lock, location.function_name(), location.filename(), location.line_number(), info.count);
  349. }
  350. VERIFY_NOT_REACHED();
  351. }
  352. #endif
  353. {
  354. ScopedSpinLock lock(g_scheduler_lock);
  355. dbgln_if(THREAD_DEBUG, "Finalizing thread {}", *this);
  356. set_state(Thread::State::Dead);
  357. m_join_condition.thread_finalizing();
  358. }
  359. if (m_dump_backtrace_on_finalization)
  360. dbgln("{}", backtrace());
  361. kfree_aligned(m_fpu_state);
  362. drop_thread_count(false);
  363. }
  364. void Thread::drop_thread_count(bool initializing_first_thread)
  365. {
  366. bool is_last = process().remove_thread(*this);
  367. if (!initializing_first_thread && is_last)
  368. process().finalize();
  369. }
  370. void Thread::finalize_dying_threads()
  371. {
  372. VERIFY(Thread::current() == g_finalizer);
  373. Vector<Thread*, 32> dying_threads;
  374. {
  375. ScopedSpinLock lock(g_scheduler_lock);
  376. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  377. if (thread.is_finalizable())
  378. dying_threads.append(&thread);
  379. });
  380. }
  381. for (auto* thread : dying_threads) {
  382. thread->finalize();
  383. // This thread will never execute again, drop the running reference
  384. // NOTE: This may not necessarily drop the last reference if anything
  385. // else is still holding onto this thread!
  386. thread->unref();
  387. }
  388. }
  389. bool Thread::tick()
  390. {
  391. if (previous_mode() == PreviousMode::KernelMode) {
  392. ++m_process->m_ticks_in_kernel;
  393. ++m_ticks_in_kernel;
  394. } else {
  395. ++m_process->m_ticks_in_user;
  396. ++m_ticks_in_user;
  397. }
  398. return --m_ticks_left;
  399. }
  400. void Thread::check_dispatch_pending_signal()
  401. {
  402. auto result = DispatchSignalResult::Continue;
  403. {
  404. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  405. if (pending_signals_for_state()) {
  406. ScopedSpinLock lock(m_lock);
  407. result = dispatch_one_pending_signal();
  408. }
  409. }
  410. switch (result) {
  411. case DispatchSignalResult::Yield:
  412. yield_while_not_holding_big_lock();
  413. break;
  414. case DispatchSignalResult::Terminate:
  415. process().die();
  416. break;
  417. default:
  418. break;
  419. }
  420. }
  421. u32 Thread::pending_signals() const
  422. {
  423. ScopedSpinLock lock(g_scheduler_lock);
  424. return pending_signals_for_state();
  425. }
  426. u32 Thread::pending_signals_for_state() const
  427. {
  428. VERIFY(g_scheduler_lock.own_lock());
  429. constexpr u32 stopped_signal_mask = (1 << (SIGCONT - 1)) | (1 << (SIGKILL - 1)) | (1 << (SIGTRAP - 1));
  430. if (is_handling_page_fault())
  431. return 0;
  432. return m_state != Stopped ? m_pending_signals : m_pending_signals & stopped_signal_mask;
  433. }
  434. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  435. {
  436. VERIFY(signal < 32);
  437. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  438. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  439. if (should_ignore_signal(signal)) {
  440. dbgln_if(SIGNAL_DEBUG, "Signal {} was ignored by {}", signal, process());
  441. return;
  442. }
  443. if constexpr (SIGNAL_DEBUG) {
  444. if (sender)
  445. dbgln("Signal: {} sent {} to {}", *sender, signal, process());
  446. else
  447. dbgln("Signal: Kernel send {} to {}", signal, process());
  448. }
  449. m_pending_signals |= 1 << (signal - 1);
  450. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  451. if (m_state == Stopped) {
  452. ScopedSpinLock lock(m_lock);
  453. if (pending_signals_for_state()) {
  454. dbgln_if(SIGNAL_DEBUG, "Signal: Resuming stopped {} to deliver signal {}", *this, signal);
  455. resume_from_stopped();
  456. }
  457. } else {
  458. ScopedSpinLock block_lock(m_block_lock);
  459. dbgln_if(SIGNAL_DEBUG, "Signal: Unblocking {} to deliver signal {}", *this, signal);
  460. unblock(signal);
  461. }
  462. }
  463. u32 Thread::update_signal_mask(u32 signal_mask)
  464. {
  465. ScopedSpinLock lock(g_scheduler_lock);
  466. auto previous_signal_mask = m_signal_mask;
  467. m_signal_mask = signal_mask;
  468. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  469. return previous_signal_mask;
  470. }
  471. u32 Thread::signal_mask() const
  472. {
  473. ScopedSpinLock lock(g_scheduler_lock);
  474. return m_signal_mask;
  475. }
  476. u32 Thread::signal_mask_block(sigset_t signal_set, bool block)
  477. {
  478. ScopedSpinLock lock(g_scheduler_lock);
  479. auto previous_signal_mask = m_signal_mask;
  480. if (block)
  481. m_signal_mask &= ~signal_set;
  482. else
  483. m_signal_mask |= signal_set;
  484. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  485. return previous_signal_mask;
  486. }
  487. void Thread::clear_signals()
  488. {
  489. ScopedSpinLock lock(g_scheduler_lock);
  490. m_signal_mask = 0;
  491. m_pending_signals = 0;
  492. m_have_any_unmasked_pending_signals.store(false, AK::memory_order_release);
  493. m_signal_action_data.fill({});
  494. }
  495. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  496. // thread into a state where the signal handler must be
  497. // invoked immediately, otherwise it will continue to fault.
  498. // This function should be used in an exception handler to
  499. // ensure that when the thread resumes, it's executing in
  500. // the appropriate signal handler.
  501. void Thread::send_urgent_signal_to_self(u8 signal)
  502. {
  503. VERIFY(Thread::current() == this);
  504. DispatchSignalResult result;
  505. {
  506. ScopedSpinLock lock(g_scheduler_lock);
  507. result = dispatch_signal(signal);
  508. }
  509. if (result == DispatchSignalResult::Yield)
  510. yield_without_holding_big_lock();
  511. }
  512. DispatchSignalResult Thread::dispatch_one_pending_signal()
  513. {
  514. VERIFY(m_lock.own_lock());
  515. u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
  516. if (signal_candidates == 0)
  517. return DispatchSignalResult::Continue;
  518. u8 signal = 1;
  519. for (; signal < 32; ++signal) {
  520. if (signal_candidates & (1 << (signal - 1))) {
  521. break;
  522. }
  523. }
  524. return dispatch_signal(signal);
  525. }
  526. DispatchSignalResult Thread::try_dispatch_one_pending_signal(u8 signal)
  527. {
  528. VERIFY(signal != 0);
  529. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  530. ScopedSpinLock lock(m_lock);
  531. u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
  532. if (!(signal_candidates & (1 << (signal - 1))))
  533. return DispatchSignalResult::Continue;
  534. return dispatch_signal(signal);
  535. }
  536. enum class DefaultSignalAction {
  537. Terminate,
  538. Ignore,
  539. DumpCore,
  540. Stop,
  541. Continue,
  542. };
  543. static DefaultSignalAction default_signal_action(u8 signal)
  544. {
  545. VERIFY(signal && signal < NSIG);
  546. switch (signal) {
  547. case SIGHUP:
  548. case SIGINT:
  549. case SIGKILL:
  550. case SIGPIPE:
  551. case SIGALRM:
  552. case SIGUSR1:
  553. case SIGUSR2:
  554. case SIGVTALRM:
  555. case SIGSTKFLT:
  556. case SIGIO:
  557. case SIGPROF:
  558. case SIGTERM:
  559. return DefaultSignalAction::Terminate;
  560. case SIGCHLD:
  561. case SIGURG:
  562. case SIGWINCH:
  563. case SIGINFO:
  564. return DefaultSignalAction::Ignore;
  565. case SIGQUIT:
  566. case SIGILL:
  567. case SIGTRAP:
  568. case SIGABRT:
  569. case SIGBUS:
  570. case SIGFPE:
  571. case SIGSEGV:
  572. case SIGXCPU:
  573. case SIGXFSZ:
  574. case SIGSYS:
  575. return DefaultSignalAction::DumpCore;
  576. case SIGCONT:
  577. return DefaultSignalAction::Continue;
  578. case SIGSTOP:
  579. case SIGTSTP:
  580. case SIGTTIN:
  581. case SIGTTOU:
  582. return DefaultSignalAction::Stop;
  583. }
  584. VERIFY_NOT_REACHED();
  585. }
  586. bool Thread::should_ignore_signal(u8 signal) const
  587. {
  588. VERIFY(signal < 32);
  589. auto& action = m_signal_action_data[signal];
  590. if (action.handler_or_sigaction.is_null())
  591. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  592. if (action.handler_or_sigaction.as_ptr() == SIG_IGN)
  593. return true;
  594. return false;
  595. }
  596. bool Thread::has_signal_handler(u8 signal) const
  597. {
  598. VERIFY(signal < 32);
  599. auto& action = m_signal_action_data[signal];
  600. return !action.handler_or_sigaction.is_null();
  601. }
  602. static bool push_value_on_user_stack(FlatPtr* stack, FlatPtr data)
  603. {
  604. *stack -= sizeof(FlatPtr);
  605. return copy_to_user((FlatPtr*)*stack, &data);
  606. }
  607. void Thread::resume_from_stopped()
  608. {
  609. VERIFY(is_stopped());
  610. VERIFY(m_stop_state != State::Invalid);
  611. VERIFY(g_scheduler_lock.own_lock());
  612. if (m_stop_state == Blocked) {
  613. ScopedSpinLock block_lock(m_block_lock);
  614. if (m_blocker) {
  615. // Hasn't been unblocked yet
  616. set_state(Blocked, 0);
  617. } else {
  618. // Was unblocked while stopped
  619. set_state(Runnable);
  620. }
  621. } else {
  622. set_state(m_stop_state, 0);
  623. }
  624. }
  625. DispatchSignalResult Thread::dispatch_signal(u8 signal)
  626. {
  627. VERIFY_INTERRUPTS_DISABLED();
  628. VERIFY(g_scheduler_lock.own_lock());
  629. VERIFY(signal > 0 && signal <= 32);
  630. VERIFY(process().is_user_process());
  631. VERIFY(this == Thread::current());
  632. dbgln_if(SIGNAL_DEBUG, "Dispatch signal {} to {}, state: {}", signal, *this, state_string());
  633. if (m_state == Invalid || !is_initialized()) {
  634. // Thread has barely been created, we need to wait until it is
  635. // at least in Runnable state and is_initialized() returns true,
  636. // which indicates that it is fully set up an we actually have
  637. // a register state on the stack that we can modify
  638. return DispatchSignalResult::Deferred;
  639. }
  640. VERIFY(previous_mode() == PreviousMode::UserMode);
  641. auto& action = m_signal_action_data[signal];
  642. // FIXME: Implement SA_SIGINFO signal handlers.
  643. VERIFY(!(action.flags & SA_SIGINFO));
  644. // Mark this signal as handled.
  645. m_pending_signals &= ~(1 << (signal - 1));
  646. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  647. auto& process = this->process();
  648. auto tracer = process.tracer();
  649. if (signal == SIGSTOP || (tracer && default_signal_action(signal) == DefaultSignalAction::DumpCore)) {
  650. dbgln_if(SIGNAL_DEBUG, "Signal {} stopping this thread", signal);
  651. set_state(State::Stopped, signal);
  652. return DispatchSignalResult::Yield;
  653. }
  654. if (signal == SIGCONT) {
  655. dbgln("signal: SIGCONT resuming {}", *this);
  656. } else {
  657. if (tracer) {
  658. // when a thread is traced, it should be stopped whenever it receives a signal
  659. // the tracer is notified of this by using waitpid()
  660. // only "pending signals" from the tracer are sent to the tracee
  661. if (!tracer->has_pending_signal(signal)) {
  662. dbgln("signal: {} stopping {} for tracer", signal, *this);
  663. set_state(Stopped, signal);
  664. return DispatchSignalResult::Yield;
  665. }
  666. tracer->unset_signal(signal);
  667. }
  668. }
  669. auto handler_vaddr = action.handler_or_sigaction;
  670. if (handler_vaddr.is_null()) {
  671. switch (default_signal_action(signal)) {
  672. case DefaultSignalAction::Stop:
  673. set_state(Stopped, signal);
  674. return DispatchSignalResult::Yield;
  675. case DefaultSignalAction::DumpCore:
  676. process.set_dump_core(true);
  677. process.for_each_thread([](auto& thread) {
  678. thread.set_dump_backtrace_on_finalization();
  679. });
  680. [[fallthrough]];
  681. case DefaultSignalAction::Terminate:
  682. m_process->terminate_due_to_signal(signal);
  683. return DispatchSignalResult::Terminate;
  684. case DefaultSignalAction::Ignore:
  685. VERIFY_NOT_REACHED();
  686. case DefaultSignalAction::Continue:
  687. return DispatchSignalResult::Continue;
  688. }
  689. VERIFY_NOT_REACHED();
  690. }
  691. if (handler_vaddr.as_ptr() == SIG_IGN) {
  692. dbgln_if(SIGNAL_DEBUG, "Ignored signal {}", signal);
  693. return DispatchSignalResult::Continue;
  694. }
  695. VERIFY(previous_mode() == PreviousMode::UserMode);
  696. VERIFY(current_trap());
  697. ProcessPagingScope paging_scope(m_process);
  698. u32 old_signal_mask = m_signal_mask;
  699. u32 new_signal_mask = action.mask;
  700. if (action.flags & SA_NODEFER)
  701. new_signal_mask &= ~(1 << (signal - 1));
  702. else
  703. new_signal_mask |= 1 << (signal - 1);
  704. m_signal_mask |= new_signal_mask;
  705. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  706. auto setup_stack = [&](RegisterState& state) {
  707. #if ARCH(I386)
  708. FlatPtr* stack = &state.userspace_esp;
  709. FlatPtr old_esp = *stack;
  710. FlatPtr ret_eip = state.eip;
  711. FlatPtr ret_eflags = state.eflags;
  712. #elif ARCH(X86_64)
  713. FlatPtr* stack = &state.userspace_esp;
  714. #endif
  715. dbgln_if(SIGNAL_DEBUG, "Setting up user stack to return to EIP {:p}, ESP {:p}", ret_eip, old_esp);
  716. #if ARCH(I386)
  717. // Align the stack to 16 bytes.
  718. // Note that we push 56 bytes (4 * 14) on to the stack,
  719. // so we need to account for this here.
  720. FlatPtr stack_alignment = (*stack - 56) % 16;
  721. *stack -= stack_alignment;
  722. push_value_on_user_stack(stack, ret_eflags);
  723. push_value_on_user_stack(stack, ret_eip);
  724. push_value_on_user_stack(stack, state.eax);
  725. push_value_on_user_stack(stack, state.ecx);
  726. push_value_on_user_stack(stack, state.edx);
  727. push_value_on_user_stack(stack, state.ebx);
  728. push_value_on_user_stack(stack, old_esp);
  729. push_value_on_user_stack(stack, state.ebp);
  730. push_value_on_user_stack(stack, state.esi);
  731. push_value_on_user_stack(stack, state.edi);
  732. #elif ARCH(X86_64)
  733. // FIXME
  734. #endif
  735. // PUSH old_signal_mask
  736. push_value_on_user_stack(stack, old_signal_mask);
  737. push_value_on_user_stack(stack, signal);
  738. push_value_on_user_stack(stack, handler_vaddr.get());
  739. push_value_on_user_stack(stack, 0); //push fake return address
  740. VERIFY((*stack % 16) == 0);
  741. };
  742. // We now place the thread state on the userspace stack.
  743. // Note that we use a RegisterState.
  744. // Conversely, when the thread isn't blocking the RegisterState may not be
  745. // valid (fork, exec etc) but the tss will, so we use that instead.
  746. auto& regs = get_register_dump_from_stack();
  747. setup_stack(regs);
  748. regs.eip = process.signal_trampoline().get();
  749. dbgln_if(SIGNAL_DEBUG, "Thread in state '{}' has been primed with signal handler {:04x}:{:08x} to deliver {}", state_string(), m_tss.cs, m_tss.eip, signal);
  750. return DispatchSignalResult::Continue;
  751. }
  752. RegisterState& Thread::get_register_dump_from_stack()
  753. {
  754. auto* trap = current_trap();
  755. // We should *always* have a trap. If we don't we're probably a kernel
  756. // thread that hasn't been pre-empted. If we want to support this, we
  757. // need to capture the registers probably into m_tss and return it
  758. VERIFY(trap);
  759. while (trap) {
  760. if (!trap->next_trap)
  761. break;
  762. trap = trap->next_trap;
  763. }
  764. return *trap->regs;
  765. }
  766. RefPtr<Thread> Thread::clone(Process& process)
  767. {
  768. auto thread_or_error = Thread::try_create(process);
  769. if (thread_or_error.is_error())
  770. return {};
  771. auto& clone = thread_or_error.value();
  772. auto signal_action_data_span = m_signal_action_data.span();
  773. signal_action_data_span.copy_to(clone->m_signal_action_data.span());
  774. clone->m_signal_mask = m_signal_mask;
  775. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  776. clone->m_thread_specific_data = m_thread_specific_data;
  777. return clone;
  778. }
  779. void Thread::set_state(State new_state, u8 stop_signal)
  780. {
  781. State previous_state;
  782. VERIFY(g_scheduler_lock.own_lock());
  783. if (new_state == m_state)
  784. return;
  785. {
  786. ScopedSpinLock thread_lock(m_lock);
  787. previous_state = m_state;
  788. if (previous_state == Invalid) {
  789. // If we were *just* created, we may have already pending signals
  790. if (has_unmasked_pending_signals()) {
  791. dbgln_if(THREAD_DEBUG, "Dispatch pending signals to new thread {}", *this);
  792. dispatch_one_pending_signal();
  793. }
  794. }
  795. m_state = new_state;
  796. dbgln_if(THREAD_DEBUG, "Set thread {} state to {}", *this, state_string());
  797. }
  798. if (previous_state == Runnable) {
  799. Scheduler::dequeue_runnable_thread(*this);
  800. } else if (previous_state == Stopped) {
  801. m_stop_state = State::Invalid;
  802. auto& process = this->process();
  803. if (process.set_stopped(false) == true) {
  804. process.for_each_thread([&](auto& thread) {
  805. if (&thread == this)
  806. return;
  807. if (!thread.is_stopped())
  808. return;
  809. dbgln_if(THREAD_DEBUG, "Resuming peer thread {}", thread);
  810. thread.resume_from_stopped();
  811. });
  812. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Continued);
  813. // Tell the parent process (if any) about this change.
  814. if (auto parent = Process::from_pid(process.ppid())) {
  815. [[maybe_unused]] auto result = parent->send_signal(SIGCHLD, &process);
  816. }
  817. }
  818. }
  819. if (m_state == Runnable) {
  820. Scheduler::queue_runnable_thread(*this);
  821. Processor::smp_wake_n_idle_processors(1);
  822. } else if (m_state == Stopped) {
  823. // We don't want to restore to Running state, only Runnable!
  824. m_stop_state = previous_state != Running ? previous_state : Runnable;
  825. auto& process = this->process();
  826. if (process.set_stopped(true) == false) {
  827. process.for_each_thread([&](auto& thread) {
  828. if (&thread == this)
  829. return;
  830. if (thread.is_stopped())
  831. return;
  832. dbgln_if(THREAD_DEBUG, "Stopping peer thread {}", thread);
  833. thread.set_state(Stopped, stop_signal);
  834. });
  835. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Stopped, stop_signal);
  836. // Tell the parent process (if any) about this change.
  837. if (auto parent = Process::from_pid(process.ppid())) {
  838. [[maybe_unused]] auto result = parent->send_signal(SIGCHLD, &process);
  839. }
  840. }
  841. } else if (m_state == Dying) {
  842. VERIFY(previous_state != Blocked);
  843. if (this != Thread::current() && is_finalizable()) {
  844. // Some other thread set this thread to Dying, notify the
  845. // finalizer right away as it can be cleaned up now
  846. Scheduler::notify_finalizer();
  847. }
  848. }
  849. }
  850. struct RecognizedSymbol {
  851. FlatPtr address;
  852. const KernelSymbol* symbol { nullptr };
  853. };
  854. static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder)
  855. {
  856. if (!symbol.address)
  857. return false;
  858. bool mask_kernel_addresses = !process.is_superuser();
  859. if (!symbol.symbol) {
  860. if (!is_user_address(VirtualAddress(symbol.address))) {
  861. builder.append("0xdeadc0de\n");
  862. } else {
  863. builder.appendff("{:p}\n", symbol.address);
  864. }
  865. return true;
  866. }
  867. unsigned offset = symbol.address - symbol.symbol->address;
  868. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
  869. builder.appendff("{:p}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address));
  870. } else {
  871. builder.appendff("{:p} {} +{}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address), demangle(symbol.symbol->name), offset);
  872. }
  873. return true;
  874. }
  875. String Thread::backtrace()
  876. {
  877. Vector<RecognizedSymbol, 128> recognized_symbols;
  878. auto& process = const_cast<Process&>(this->process());
  879. auto stack_trace = Processor::capture_stack_trace(*this);
  880. VERIFY(!g_scheduler_lock.own_lock());
  881. ProcessPagingScope paging_scope(process);
  882. for (auto& frame : stack_trace) {
  883. if (is_user_range(VirtualAddress(frame), sizeof(FlatPtr) * 2)) {
  884. recognized_symbols.append({ frame });
  885. } else {
  886. recognized_symbols.append({ frame, symbolicate_kernel_address(frame) });
  887. }
  888. }
  889. StringBuilder builder;
  890. for (auto& symbol : recognized_symbols) {
  891. if (!symbolicate(symbol, process, builder))
  892. break;
  893. }
  894. return builder.to_string();
  895. }
  896. size_t Thread::thread_specific_region_alignment() const
  897. {
  898. return max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  899. }
  900. size_t Thread::thread_specific_region_size() const
  901. {
  902. return align_up_to(process().m_master_tls_size, thread_specific_region_alignment()) + sizeof(ThreadSpecificData);
  903. }
  904. KResult Thread::make_thread_specific_region(Badge<Process>)
  905. {
  906. // The process may not require a TLS region, or allocate TLS later with sys$allocate_tls (which is what dynamically loaded programs do)
  907. if (!process().m_master_tls_region)
  908. return KSuccess;
  909. auto range = process().space().allocate_range({}, thread_specific_region_size());
  910. if (!range.has_value())
  911. return ENOMEM;
  912. auto region_or_error = process().space().allocate_region(range.value(), "Thread-specific", PROT_READ | PROT_WRITE);
  913. if (region_or_error.is_error())
  914. return region_or_error.error();
  915. m_thread_specific_range = range.value();
  916. SmapDisabler disabler;
  917. auto* thread_specific_data = (ThreadSpecificData*)region_or_error.value()->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment())).as_ptr();
  918. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  919. m_thread_specific_data = VirtualAddress(thread_specific_data);
  920. thread_specific_data->self = thread_specific_data;
  921. if (process().m_master_tls_size)
  922. memcpy(thread_local_storage, process().m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), process().m_master_tls_size);
  923. return KSuccess;
  924. }
  925. RefPtr<Thread> Thread::from_tid(ThreadID tid)
  926. {
  927. RefPtr<Thread> found_thread;
  928. {
  929. ScopedSpinLock lock(g_tid_map_lock);
  930. if (auto it = g_tid_map->find(tid); it != g_tid_map->end()) {
  931. // We need to call try_ref() here as there is a window between
  932. // dropping the last reference and calling the Thread's destructor!
  933. // We shouldn't remove the threads from that list until it is truly
  934. // destructed as it may stick around past finalization in order to
  935. // be able to wait() on it!
  936. if (it->value->try_ref()) {
  937. found_thread = adopt_ref(*it->value);
  938. }
  939. }
  940. }
  941. return found_thread;
  942. }
  943. void Thread::reset_fpu_state()
  944. {
  945. memcpy(m_fpu_state, &Processor::current().clean_fpu_state(), sizeof(FPUState));
  946. }
  947. bool Thread::should_be_stopped() const
  948. {
  949. return process().is_stopped();
  950. }
  951. }
  952. void AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, const Kernel::Thread& value)
  953. {
  954. return AK::Formatter<FormatString>::format(
  955. builder,
  956. "{}({}:{})", value.process().name(), value.pid().value(), value.tid().value());
  957. }