Thread.cpp 36 KB

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