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