Thread.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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/Arch/x86/SmapDisabler.h>
  32. #include <Kernel/Debug.h>
  33. #include <Kernel/FileSystem/FileDescription.h>
  34. #include <Kernel/KSyms.h>
  35. #include <Kernel/PerformanceEventBuffer.h>
  36. #include <Kernel/Process.h>
  37. #include <Kernel/Scheduler.h>
  38. #include <Kernel/Thread.h>
  39. #include <Kernel/ThreadTracer.h>
  40. #include <Kernel/TimerQueue.h>
  41. #include <Kernel/VM/MemoryManager.h>
  42. #include <Kernel/VM/PageDirectory.h>
  43. #include <Kernel/VM/ProcessPagingScope.h>
  44. #include <LibC/signal_numbers.h>
  45. namespace Kernel {
  46. SpinLock<u8> Thread::g_tid_map_lock;
  47. READONLY_AFTER_INIT HashMap<ThreadID, Thread*>* Thread::g_tid_map;
  48. UNMAP_AFTER_INIT void Thread::initialize()
  49. {
  50. g_tid_map = new HashMap<ThreadID, Thread*>();
  51. }
  52. KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> process)
  53. {
  54. auto kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, {}, Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  55. if (!kernel_stack_region)
  56. return ENOMEM;
  57. kernel_stack_region->set_stack(true);
  58. return adopt(*new Thread(move(process), kernel_stack_region.release_nonnull()));
  59. }
  60. Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stack_region)
  61. : m_process(move(process))
  62. , m_kernel_stack_region(move(kernel_stack_region))
  63. , m_name(m_process->name())
  64. {
  65. bool is_first_thread = m_process->add_thread(*this);
  66. if (is_first_thread) {
  67. // First thread gets TID == PID
  68. m_tid = m_process->pid().value();
  69. } else {
  70. m_tid = Process::allocate_pid().value();
  71. }
  72. m_kernel_stack_region->set_name(String::formatted("Kernel stack (thread {})", m_tid.value()));
  73. {
  74. ScopedSpinLock lock(g_tid_map_lock);
  75. auto result = g_tid_map->set(m_tid, this);
  76. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  77. }
  78. if constexpr (THREAD_DEBUG)
  79. dbgln("Created new thread {}({}:{})", m_process->name(), m_process->pid().value(), m_tid.value());
  80. m_fpu_state = (FPUState*)kmalloc_aligned<16>(sizeof(FPUState));
  81. reset_fpu_state();
  82. m_tss.iomapbase = sizeof(TSS32);
  83. // Only IF is set when a process boots.
  84. m_tss.eflags = 0x0202;
  85. if (m_process->is_kernel_process()) {
  86. m_tss.cs = GDT_SELECTOR_CODE0;
  87. m_tss.ds = GDT_SELECTOR_DATA0;
  88. m_tss.es = GDT_SELECTOR_DATA0;
  89. m_tss.fs = GDT_SELECTOR_PROC;
  90. m_tss.ss = GDT_SELECTOR_DATA0;
  91. m_tss.gs = 0;
  92. } else {
  93. m_tss.cs = GDT_SELECTOR_CODE3 | 3;
  94. m_tss.ds = GDT_SELECTOR_DATA3 | 3;
  95. m_tss.es = GDT_SELECTOR_DATA3 | 3;
  96. m_tss.fs = GDT_SELECTOR_DATA3 | 3;
  97. m_tss.ss = GDT_SELECTOR_DATA3 | 3;
  98. m_tss.gs = GDT_SELECTOR_TLS | 3;
  99. }
  100. m_tss.cr3 = m_process->space().page_directory().cr3();
  101. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  102. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
  103. if (m_process->is_kernel_process()) {
  104. m_tss.esp = m_tss.esp0 = m_kernel_stack_top;
  105. } else {
  106. // Ring 3 processes get a separate stack for ring 0.
  107. // The ring 3 stack will be assigned by exec().
  108. m_tss.ss0 = GDT_SELECTOR_DATA0;
  109. m_tss.esp0 = m_kernel_stack_top;
  110. }
  111. // We need to add another reference if we could successfully create
  112. // all the resources needed for this thread. The reason for this is that
  113. // we don't want to delete this thread after dropping the reference,
  114. // it may still be running or scheduled to be run.
  115. // The finalizer is responsible for dropping this reference once this
  116. // thread is ready to be cleaned up.
  117. ref();
  118. }
  119. Thread::~Thread()
  120. {
  121. {
  122. // We need to explicitly remove ourselves from the thread list
  123. // here. We may get pre-empted in the middle of destructing this
  124. // thread, which causes problems if the thread list is iterated.
  125. // Specifically, if this is the last thread of a process, checking
  126. // block conditions would access m_process, which would be in
  127. // the middle of being destroyed.
  128. ScopedSpinLock lock(g_scheduler_lock);
  129. VERIFY(!m_process_thread_list_node.is_in_list());
  130. // We shouldn't be queued
  131. VERIFY(m_runnable_priority < 0);
  132. }
  133. {
  134. ScopedSpinLock lock(g_tid_map_lock);
  135. auto result = g_tid_map->remove(m_tid);
  136. VERIFY(result);
  137. }
  138. }
  139. void Thread::unblock_from_blocker(Blocker& blocker)
  140. {
  141. auto do_unblock = [&]() {
  142. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  143. ScopedSpinLock block_lock(m_block_lock);
  144. if (m_blocker != &blocker)
  145. return;
  146. if (!should_be_stopped() && !is_stopped())
  147. unblock();
  148. };
  149. if (Processor::current().in_irq()) {
  150. Processor::current().deferred_call_queue([do_unblock = move(do_unblock), self = make_weak_ptr()]() {
  151. if (auto this_thread = self.strong_ref())
  152. do_unblock();
  153. });
  154. } else {
  155. do_unblock();
  156. }
  157. }
  158. void Thread::unblock(u8 signal)
  159. {
  160. VERIFY(!Processor::current().in_irq());
  161. VERIFY(g_scheduler_lock.own_lock());
  162. VERIFY(m_block_lock.own_lock());
  163. if (m_state != Thread::Blocked)
  164. return;
  165. VERIFY(m_blocker);
  166. if (signal != 0) {
  167. if (is_handling_page_fault()) {
  168. // Don't let signals unblock threads that are blocked inside a page fault handler.
  169. // This prevents threads from EINTR'ing the inode read in an inode page fault.
  170. // FIXME: There's probably a better way to solve this.
  171. return;
  172. }
  173. if (!m_blocker->can_be_interrupted() && !m_should_die)
  174. return;
  175. m_blocker->set_interrupted_by_signal(signal);
  176. }
  177. m_blocker = nullptr;
  178. if (Thread::current() == this) {
  179. set_state(Thread::Running);
  180. return;
  181. }
  182. VERIFY(m_state != Thread::Runnable && m_state != Thread::Running);
  183. set_state(Thread::Runnable);
  184. }
  185. void Thread::set_should_die()
  186. {
  187. if (m_should_die) {
  188. dbgln("{} Should already die", *this);
  189. return;
  190. }
  191. ScopedCritical critical;
  192. // Remember that we should die instead of returning to
  193. // the userspace.
  194. ScopedSpinLock lock(g_scheduler_lock);
  195. m_should_die = true;
  196. // NOTE: Even the current thread can technically be in "Stopped"
  197. // state! This is the case when another thread sent a SIGSTOP to
  198. // it while it was running and it calls e.g. exit() before
  199. // the scheduler gets involved again.
  200. if (is_stopped()) {
  201. // If we were stopped, we need to briefly resume so that
  202. // the kernel stacks can clean up. We won't ever return back
  203. // to user mode, though
  204. VERIFY(!process().is_stopped());
  205. resume_from_stopped();
  206. }
  207. if (is_blocked()) {
  208. ScopedSpinLock block_lock(m_block_lock);
  209. if (m_blocker) {
  210. // We're blocked in the kernel.
  211. m_blocker->set_interrupted_by_death();
  212. unblock();
  213. }
  214. }
  215. }
  216. void Thread::die_if_needed()
  217. {
  218. VERIFY(Thread::current() == this);
  219. if (!m_should_die)
  220. return;
  221. u32 unlock_count;
  222. [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
  223. ScopedCritical critical;
  224. // Flag a context switch. Because we're in a critical section,
  225. // Scheduler::yield will actually only mark a pending context switch
  226. // Simply leaving the critical section would not necessarily trigger
  227. // a switch.
  228. Scheduler::yield();
  229. // Now leave the critical section so that we can also trigger the
  230. // actual context switch
  231. u32 prev_flags;
  232. Processor::current().clear_critical(prev_flags, false);
  233. dbgln("die_if_needed returned from clear_critical!!! in irq: {}", Processor::current().in_irq());
  234. // We should never get here, but the scoped scheduler lock
  235. // will be released by Scheduler::context_switch again
  236. VERIFY_NOT_REACHED();
  237. }
  238. void Thread::exit(void* exit_value)
  239. {
  240. VERIFY(Thread::current() == this);
  241. m_join_condition.thread_did_exit(exit_value);
  242. set_should_die();
  243. u32 unlock_count;
  244. [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
  245. die_if_needed();
  246. }
  247. void Thread::yield_while_not_holding_big_lock()
  248. {
  249. VERIFY(!g_scheduler_lock.own_lock());
  250. u32 prev_flags;
  251. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  252. Scheduler::yield();
  253. // NOTE: We may be on a different CPU now!
  254. Processor::current().restore_critical(prev_crit, prev_flags);
  255. }
  256. void Thread::yield_without_holding_big_lock()
  257. {
  258. VERIFY(!g_scheduler_lock.own_lock());
  259. u32 lock_count_to_restore = 0;
  260. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  261. // NOTE: Even though we call Scheduler::yield here, unless we happen
  262. // to be outside of a critical section, the yield will be postponed
  263. // until leaving it in relock_process.
  264. Scheduler::yield();
  265. relock_process(previous_locked, lock_count_to_restore);
  266. }
  267. void Thread::donate_without_holding_big_lock(RefPtr<Thread>& thread, const char* reason)
  268. {
  269. VERIFY(!g_scheduler_lock.own_lock());
  270. u32 lock_count_to_restore = 0;
  271. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  272. // NOTE: Even though we call Scheduler::yield here, unless we happen
  273. // to be outside of a critical section, the yield will be postponed
  274. // until leaving it in relock_process.
  275. Scheduler::donate_to(thread, reason);
  276. relock_process(previous_locked, lock_count_to_restore);
  277. }
  278. LockMode Thread::unlock_process_if_locked(u32& lock_count_to_restore)
  279. {
  280. return process().big_lock().force_unlock_if_locked(lock_count_to_restore);
  281. }
  282. void Thread::relock_process(LockMode previous_locked, u32 lock_count_to_restore)
  283. {
  284. // Clearing the critical section may trigger the context switch
  285. // flagged by calling Scheduler::donate_to or Scheduler::yield
  286. // above. We have to do it this way because we intentionally
  287. // leave the critical section here to be able to switch contexts.
  288. u32 prev_flags;
  289. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  290. // CONTEXT SWITCH HAPPENS HERE!
  291. // NOTE: We may be on a different CPU now!
  292. Processor::current().restore_critical(prev_crit, prev_flags);
  293. if (previous_locked != LockMode::Unlocked) {
  294. // We've unblocked, relock the process if needed and carry on.
  295. RESTORE_LOCK(process().big_lock(), previous_locked, lock_count_to_restore);
  296. }
  297. }
  298. auto Thread::sleep(clockid_t clock_id, const Time& duration, Time* remaining_time) -> BlockResult
  299. {
  300. VERIFY(state() == Thread::Running);
  301. return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(false, &duration, nullptr, clock_id), remaining_time);
  302. }
  303. auto Thread::sleep_until(clockid_t clock_id, const Time& deadline) -> BlockResult
  304. {
  305. VERIFY(state() == Thread::Running);
  306. return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(true, &deadline, nullptr, clock_id));
  307. }
  308. const char* Thread::state_string() const
  309. {
  310. switch (state()) {
  311. case Thread::Invalid:
  312. return "Invalid";
  313. case Thread::Runnable:
  314. return "Runnable";
  315. case Thread::Running:
  316. return "Running";
  317. case Thread::Dying:
  318. return "Dying";
  319. case Thread::Dead:
  320. return "Dead";
  321. case Thread::Stopped:
  322. return "Stopped";
  323. case Thread::Blocked: {
  324. ScopedSpinLock block_lock(m_block_lock);
  325. VERIFY(m_blocker != nullptr);
  326. return m_blocker->state_string();
  327. }
  328. }
  329. klog() << "Thread::state_string(): Invalid state: " << state();
  330. VERIFY_NOT_REACHED();
  331. return nullptr;
  332. }
  333. void Thread::finalize()
  334. {
  335. VERIFY(Thread::current() == g_finalizer);
  336. VERIFY(Thread::current() != this);
  337. #if LOCK_DEBUG
  338. VERIFY(!m_lock.own_lock());
  339. if (lock_count() > 0) {
  340. dbgln("Thread {} leaking {} Locks!", *this, lock_count());
  341. ScopedSpinLock list_lock(m_holding_locks_lock);
  342. for (auto& info : m_holding_locks_list)
  343. dbgln(" - {} @ {} locked at {}:{} count: {}", info.lock->name(), info.lock, info.file, info.line, info.count);
  344. VERIFY_NOT_REACHED();
  345. }
  346. #endif
  347. {
  348. ScopedSpinLock lock(g_scheduler_lock);
  349. dbgln_if(THREAD_DEBUG, "Finalizing thread {}", *this);
  350. set_state(Thread::State::Dead);
  351. m_join_condition.thread_finalizing();
  352. }
  353. if (m_dump_backtrace_on_finalization)
  354. dbgln("{}", backtrace());
  355. kfree_aligned(m_fpu_state);
  356. drop_thread_count(false);
  357. }
  358. void Thread::drop_thread_count(bool initializing_first_thread)
  359. {
  360. bool is_last = process().remove_thread(*this);
  361. if (!initializing_first_thread && is_last)
  362. process().finalize();
  363. }
  364. void Thread::finalize_dying_threads()
  365. {
  366. VERIFY(Thread::current() == g_finalizer);
  367. Vector<Thread*, 32> dying_threads;
  368. {
  369. ScopedSpinLock lock(g_scheduler_lock);
  370. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  371. if (thread.is_finalizable())
  372. dying_threads.append(&thread);
  373. return IterationDecision::Continue;
  374. });
  375. }
  376. for (auto* thread : dying_threads) {
  377. thread->finalize();
  378. // This thread will never execute again, drop the running reference
  379. // NOTE: This may not necessarily drop the last reference if anything
  380. // else is still holding onto this thread!
  381. thread->unref();
  382. }
  383. }
  384. bool Thread::tick()
  385. {
  386. if (previous_mode() == PreviousMode::KernelMode) {
  387. ++m_process->m_ticks_in_kernel;
  388. ++m_ticks_in_kernel;
  389. } else {
  390. ++m_process->m_ticks_in_user;
  391. ++m_ticks_in_user;
  392. }
  393. return --m_ticks_left;
  394. }
  395. void Thread::check_dispatch_pending_signal()
  396. {
  397. auto result = DispatchSignalResult::Continue;
  398. {
  399. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  400. if (pending_signals_for_state()) {
  401. ScopedSpinLock lock(m_lock);
  402. result = dispatch_one_pending_signal();
  403. }
  404. }
  405. switch (result) {
  406. case DispatchSignalResult::Yield:
  407. yield_while_not_holding_big_lock();
  408. break;
  409. case DispatchSignalResult::Terminate:
  410. process().die();
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. u32 Thread::pending_signals() const
  417. {
  418. ScopedSpinLock lock(g_scheduler_lock);
  419. return pending_signals_for_state();
  420. }
  421. u32 Thread::pending_signals_for_state() const
  422. {
  423. VERIFY(g_scheduler_lock.own_lock());
  424. constexpr u32 stopped_signal_mask = (1 << (SIGCONT - 1)) | (1 << (SIGKILL - 1)) | (1 << (SIGTRAP - 1));
  425. if (is_handling_page_fault())
  426. return 0;
  427. return m_state != Stopped ? m_pending_signals : m_pending_signals & stopped_signal_mask;
  428. }
  429. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  430. {
  431. VERIFY(signal < 32);
  432. ScopedSpinLock scheduler_lock(g_scheduler_lock);
  433. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  434. if (should_ignore_signal(signal)) {
  435. dbgln_if(SIGNAL_DEBUG, "Signal {} was ignored by {}", signal, process());
  436. return;
  437. }
  438. if constexpr (SIGNAL_DEBUG) {
  439. if (sender)
  440. dbgln("Signal: {} sent {} to {}", *sender, signal, process());
  441. else
  442. dbgln("Signal: Kernel send {} to {}", signal, process());
  443. }
  444. m_pending_signals |= 1 << (signal - 1);
  445. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  446. if (m_state == Stopped) {
  447. ScopedSpinLock lock(m_lock);
  448. if (pending_signals_for_state()) {
  449. dbgln_if(SIGNAL_DEBUG, "Signal: Resuming stopped {} to deliver signal {}", *this, signal);
  450. resume_from_stopped();
  451. }
  452. } else {
  453. ScopedSpinLock block_lock(m_block_lock);
  454. dbgln_if(SIGNAL_DEBUG, "Signal: Unblocking {} to deliver signal {}", *this, signal);
  455. unblock(signal);
  456. }
  457. }
  458. u32 Thread::update_signal_mask(u32 signal_mask)
  459. {
  460. ScopedSpinLock lock(g_scheduler_lock);
  461. auto previous_signal_mask = m_signal_mask;
  462. m_signal_mask = signal_mask;
  463. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  464. return previous_signal_mask;
  465. }
  466. u32 Thread::signal_mask() const
  467. {
  468. ScopedSpinLock lock(g_scheduler_lock);
  469. return m_signal_mask;
  470. }
  471. u32 Thread::signal_mask_block(sigset_t signal_set, bool block)
  472. {
  473. ScopedSpinLock lock(g_scheduler_lock);
  474. auto previous_signal_mask = m_signal_mask;
  475. if (block)
  476. m_signal_mask &= ~signal_set;
  477. else
  478. m_signal_mask |= signal_set;
  479. m_have_any_unmasked_pending_signals.store(pending_signals_for_state() & ~m_signal_mask, AK::memory_order_release);
  480. return previous_signal_mask;
  481. }
  482. void Thread::clear_signals()
  483. {
  484. ScopedSpinLock lock(g_scheduler_lock);
  485. m_signal_mask = 0;
  486. m_pending_signals = 0;
  487. m_have_any_unmasked_pending_signals.store(false, AK::memory_order_release);
  488. m_signal_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. VERIFY(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. VERIFY(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. VERIFY(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. VERIFY(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. VERIFY_NOT_REACHED();
  580. }
  581. bool Thread::should_ignore_signal(u8 signal) const
  582. {
  583. VERIFY(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. VERIFY(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(FlatPtr* stack, FlatPtr data)
  598. {
  599. *stack -= sizeof(FlatPtr);
  600. return copy_to_user((FlatPtr*)*stack, &data);
  601. }
  602. void Thread::resume_from_stopped()
  603. {
  604. VERIFY(is_stopped());
  605. VERIFY(m_stop_state != State::Invalid);
  606. VERIFY(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. VERIFY_INTERRUPTS_DISABLED();
  623. VERIFY(g_scheduler_lock.own_lock());
  624. VERIFY(signal > 0 && signal <= 32);
  625. VERIFY(process().is_user_process());
  626. VERIFY(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. VERIFY(previous_mode() == PreviousMode::UserMode);
  638. auto& action = m_signal_action_data[signal];
  639. // FIXME: Implement SA_SIGINFO signal handlers.
  640. VERIFY(!(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. VERIFY_NOT_REACHED();
  684. case DefaultSignalAction::Continue:
  685. return DispatchSignalResult::Continue;
  686. }
  687. VERIFY_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. VERIFY(previous_mode() == PreviousMode::UserMode);
  696. VERIFY(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. #if ARCH(I386)
  708. FlatPtr* stack = &state.userspace_esp;
  709. #elif ARCH(X86_64)
  710. FlatPtr* stack = &state.userspace_esp;
  711. #endif
  712. FlatPtr old_esp = *stack;
  713. FlatPtr ret_eip = state.eip;
  714. FlatPtr ret_eflags = state.eflags;
  715. #if SIGNAL_DEBUG
  716. klog() << "signal: setting up user stack to return to eip: " << String::format("%p", (void*)ret_eip) << " esp: " << String::format("%p", (void*)old_esp);
  717. #endif
  718. #if ARCH(I386)
  719. // Align the stack to 16 bytes.
  720. // Note that we push 56 bytes (4 * 14) on to the stack,
  721. // so we need to account for this here.
  722. FlatPtr stack_alignment = (*stack - 56) % 16;
  723. *stack -= stack_alignment;
  724. push_value_on_user_stack(stack, ret_eflags);
  725. push_value_on_user_stack(stack, ret_eip);
  726. push_value_on_user_stack(stack, state.eax);
  727. push_value_on_user_stack(stack, state.ecx);
  728. push_value_on_user_stack(stack, state.edx);
  729. push_value_on_user_stack(stack, state.ebx);
  730. push_value_on_user_stack(stack, old_esp);
  731. push_value_on_user_stack(stack, state.ebp);
  732. push_value_on_user_stack(stack, state.esi);
  733. push_value_on_user_stack(stack, state.edi);
  734. #elif ARCH(X86_64)
  735. // FIXME
  736. #endif
  737. // PUSH old_signal_mask
  738. push_value_on_user_stack(stack, old_signal_mask);
  739. push_value_on_user_stack(stack, signal);
  740. push_value_on_user_stack(stack, handler_vaddr.get());
  741. push_value_on_user_stack(stack, 0); //push fake return address
  742. VERIFY((*stack % 16) == 0);
  743. };
  744. // We now place the thread state on the userspace stack.
  745. // Note that we use a RegisterState.
  746. // Conversely, when the thread isn't blocking the RegisterState may not be
  747. // valid (fork, exec etc) but the tss will, so we use that instead.
  748. auto& regs = get_register_dump_from_stack();
  749. setup_stack(regs);
  750. regs.eip = process.signal_trampoline().get();
  751. #if SIGNAL_DEBUG
  752. dbgln("signal: Thread in state '{}' has been primed with signal handler {:04x}:{:08x} to deliver {}", state_string(), m_tss.cs, m_tss.eip, signal);
  753. #endif
  754. return DispatchSignalResult::Continue;
  755. }
  756. RegisterState& Thread::get_register_dump_from_stack()
  757. {
  758. auto* trap = current_trap();
  759. // We should *always* have a trap. If we don't we're probably a kernel
  760. // thread that hasn't been pre-empted. If we want to support this, we
  761. // need to capture the registers probably into m_tss and return it
  762. VERIFY(trap);
  763. while (trap) {
  764. if (!trap->next_trap)
  765. break;
  766. trap = trap->next_trap;
  767. }
  768. return *trap->regs;
  769. }
  770. RefPtr<Thread> Thread::clone(Process& process)
  771. {
  772. auto thread_or_error = Thread::try_create(process);
  773. if (thread_or_error.is_error())
  774. return {};
  775. auto& clone = thread_or_error.value();
  776. auto signal_action_data_span = m_signal_action_data.span();
  777. signal_action_data_span.copy_to(clone->m_signal_action_data.span());
  778. clone->m_signal_mask = m_signal_mask;
  779. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  780. clone->m_thread_specific_data = m_thread_specific_data;
  781. return clone;
  782. }
  783. void Thread::set_state(State new_state, u8 stop_signal)
  784. {
  785. State previous_state;
  786. VERIFY(g_scheduler_lock.own_lock());
  787. if (new_state == m_state)
  788. return;
  789. {
  790. ScopedSpinLock thread_lock(m_lock);
  791. previous_state = m_state;
  792. if (previous_state == Invalid) {
  793. // If we were *just* created, we may have already pending signals
  794. if (has_unmasked_pending_signals()) {
  795. dbgln_if(THREAD_DEBUG, "Dispatch pending signals to new thread {}", *this);
  796. dispatch_one_pending_signal();
  797. }
  798. }
  799. m_state = new_state;
  800. dbgln_if(THREAD_DEBUG, "Set thread {} state to {}", *this, state_string());
  801. }
  802. if (previous_state == Runnable) {
  803. Scheduler::dequeue_runnable_thread(*this);
  804. } else if (previous_state == Stopped) {
  805. m_stop_state = State::Invalid;
  806. auto& process = this->process();
  807. if (process.set_stopped(false) == true) {
  808. process.for_each_thread([&](auto& thread) {
  809. if (&thread == this || !thread.is_stopped())
  810. return IterationDecision::Continue;
  811. dbgln_if(THREAD_DEBUG, "Resuming peer thread {}", thread);
  812. thread.resume_from_stopped();
  813. return IterationDecision::Continue;
  814. });
  815. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Continued);
  816. }
  817. }
  818. if (m_state == Runnable) {
  819. Scheduler::queue_runnable_thread(*this);
  820. Processor::smp_wake_n_idle_processors(1);
  821. } else if (m_state == Stopped) {
  822. // We don't want to restore to Running state, only Runnable!
  823. m_stop_state = previous_state != Running ? previous_state : Runnable;
  824. auto& process = this->process();
  825. if (process.set_stopped(true) == false) {
  826. process.for_each_thread([&](auto& thread) {
  827. if (&thread == this || thread.is_stopped())
  828. return IterationDecision::Continue;
  829. dbgln_if(THREAD_DEBUG, "Stopping peer thread {}", thread);
  830. thread.set_state(Stopped, stop_signal);
  831. return IterationDecision::Continue;
  832. });
  833. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Stopped, stop_signal);
  834. }
  835. } else if (m_state == Dying) {
  836. VERIFY(previous_state != Blocked);
  837. if (this != Thread::current() && is_finalizable()) {
  838. // Some other thread set this thread to Dying, notify the
  839. // finalizer right away as it can be cleaned up now
  840. Scheduler::notify_finalizer();
  841. }
  842. }
  843. }
  844. struct RecognizedSymbol {
  845. FlatPtr address;
  846. const KernelSymbol* symbol { nullptr };
  847. };
  848. static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder)
  849. {
  850. if (!symbol.address)
  851. return false;
  852. bool mask_kernel_addresses = !process.is_superuser();
  853. if (!symbol.symbol) {
  854. if (!is_user_address(VirtualAddress(symbol.address))) {
  855. builder.append("0xdeadc0de\n");
  856. } else {
  857. builder.appendff("{:p}\n", symbol.address);
  858. }
  859. return true;
  860. }
  861. unsigned offset = symbol.address - symbol.symbol->address;
  862. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
  863. builder.appendff("{:p}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address));
  864. } else {
  865. builder.appendff("{:p} {} +{}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address), demangle(symbol.symbol->name), offset);
  866. }
  867. return true;
  868. }
  869. String Thread::backtrace()
  870. {
  871. Vector<RecognizedSymbol, 128> recognized_symbols;
  872. auto& process = const_cast<Process&>(this->process());
  873. auto stack_trace = Processor::capture_stack_trace(*this);
  874. VERIFY(!g_scheduler_lock.own_lock());
  875. ProcessPagingScope paging_scope(process);
  876. for (auto& frame : stack_trace) {
  877. if (is_user_range(VirtualAddress(frame), sizeof(FlatPtr) * 2)) {
  878. recognized_symbols.append({ frame });
  879. } else {
  880. recognized_symbols.append({ frame, symbolicate_kernel_address(frame) });
  881. }
  882. }
  883. StringBuilder builder;
  884. for (auto& symbol : recognized_symbols) {
  885. if (!symbolicate(symbol, process, builder))
  886. break;
  887. }
  888. return builder.to_string();
  889. }
  890. size_t Thread::thread_specific_region_alignment() const
  891. {
  892. return max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  893. }
  894. size_t Thread::thread_specific_region_size() const
  895. {
  896. return align_up_to(process().m_master_tls_size, thread_specific_region_alignment()) + sizeof(ThreadSpecificData);
  897. }
  898. KResult Thread::make_thread_specific_region(Badge<Process>)
  899. {
  900. // The process may not require a TLS region
  901. if (!process().m_master_tls_region)
  902. return KSuccess;
  903. auto range = process().space().allocate_range({}, thread_specific_region_size());
  904. if (!range.has_value())
  905. return ENOMEM;
  906. auto region_or_error = process().space().allocate_region(range.value(), "Thread-specific", PROT_READ | PROT_WRITE);
  907. if (region_or_error.is_error())
  908. return region_or_error.error();
  909. SmapDisabler disabler;
  910. 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();
  911. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  912. m_thread_specific_data = VirtualAddress(thread_specific_data);
  913. thread_specific_data->self = thread_specific_data;
  914. if (process().m_master_tls_size)
  915. memcpy(thread_local_storage, process().m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), process().m_master_tls_size);
  916. return KSuccess;
  917. }
  918. const LogStream& operator<<(const LogStream& stream, const Thread& value)
  919. {
  920. return stream << value.process().name() << "(" << value.pid().value() << ":" << value.tid().value() << ")";
  921. }
  922. RefPtr<Thread> Thread::from_tid(ThreadID tid)
  923. {
  924. RefPtr<Thread> found_thread;
  925. {
  926. ScopedSpinLock lock(g_tid_map_lock);
  927. auto it = g_tid_map->find(tid);
  928. if (it != g_tid_map->end())
  929. found_thread = it->value;
  930. }
  931. return found_thread;
  932. }
  933. void Thread::reset_fpu_state()
  934. {
  935. memcpy(m_fpu_state, &Processor::current().clean_fpu_state(), sizeof(FPUState));
  936. }
  937. bool Thread::should_be_stopped() const
  938. {
  939. return process().is_stopped();
  940. }
  941. }
  942. void AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, const Kernel::Thread& value)
  943. {
  944. return AK::Formatter<FormatString>::format(
  945. builder,
  946. "{}({}:{})", value.process().name(), value.pid().value(), value.tid().value());
  947. }