Thread.cpp 35 KB

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