Thread.cpp 35 KB

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