Thread.cpp 36 KB

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