Thread.cpp 39 KB

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