Thread.cpp 36 KB

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