Thread.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/TemporaryChange.h>
  10. #include <AK/Time.h>
  11. #include <Kernel/API/POSIX/signal_numbers.h>
  12. #include <Kernel/Arch/PageDirectory.h>
  13. #include <Kernel/Arch/SmapDisabler.h>
  14. #include <Kernel/Arch/TrapFrame.h>
  15. #include <Kernel/Debug.h>
  16. #include <Kernel/Devices/KCOVDevice.h>
  17. #include <Kernel/FileSystem/OpenFileDescription.h>
  18. #include <Kernel/InterruptDisabler.h>
  19. #include <Kernel/KSyms.h>
  20. #include <Kernel/Memory/MemoryManager.h>
  21. #include <Kernel/Memory/ScopedAddressSpaceSwitcher.h>
  22. #include <Kernel/Panic.h>
  23. #include <Kernel/PerformanceEventBuffer.h>
  24. #include <Kernel/Process.h>
  25. #include <Kernel/Scheduler.h>
  26. #include <Kernel/Sections.h>
  27. #include <Kernel/Thread.h>
  28. #include <Kernel/ThreadTracer.h>
  29. #include <Kernel/TimerQueue.h>
  30. #include <Kernel/kstdio.h>
  31. namespace Kernel {
  32. static Singleton<SpinlockProtected<Thread::GlobalList, LockRank::None>> s_list;
  33. SpinlockProtected<Thread::GlobalList, LockRank::None>& Thread::all_instances()
  34. {
  35. return *s_list;
  36. }
  37. ErrorOr<NonnullRefPtr<Thread>> Thread::create(NonnullRefPtr<Process> process)
  38. {
  39. auto kernel_stack_region = TRY(MM.allocate_kernel_region(default_kernel_stack_size, {}, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow));
  40. kernel_stack_region->set_stack(true);
  41. auto block_timer = TRY(try_make_lock_ref_counted<Timer>());
  42. auto name = TRY(process->name().with([](auto& name) { return name->try_clone(); }));
  43. return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(process), move(kernel_stack_region), move(block_timer), move(name)));
  44. }
  45. Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Memory::Region> kernel_stack_region, NonnullLockRefPtr<Timer> block_timer, NonnullOwnPtr<KString> name)
  46. : m_process(move(process))
  47. , m_kernel_stack_region(move(kernel_stack_region))
  48. , m_name(move(name))
  49. , m_block_timer(move(block_timer))
  50. {
  51. bool is_first_thread = m_process->add_thread(*this);
  52. if (is_first_thread) {
  53. // First thread gets TID == PID
  54. m_tid = m_process->pid().value();
  55. } else {
  56. m_tid = Process::allocate_pid().value();
  57. }
  58. // FIXME: Handle KString allocation failure.
  59. m_kernel_stack_region->set_name(MUST(KString::formatted("Kernel stack (thread {})", m_tid.value())));
  60. Thread::all_instances().with([&](auto& list) {
  61. list.append(*this);
  62. });
  63. if constexpr (THREAD_DEBUG) {
  64. m_process->name().with([&](auto& process_name) {
  65. dbgln("Created new thread {}({}:{})", process_name->view(), m_process->pid().value(), m_tid.value());
  66. });
  67. }
  68. reset_fpu_state();
  69. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  70. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & ~(FlatPtr)0x7u;
  71. m_process->address_space().with([&](auto& space) {
  72. m_regs.set_initial_state(m_process->is_kernel_process(), *space, m_kernel_stack_top);
  73. });
  74. // We need to add another reference if we could successfully create
  75. // all the resources needed for this thread. The reason for this is that
  76. // we don't want to delete this thread after dropping the reference,
  77. // it may still be running or scheduled to be run.
  78. // The finalizer is responsible for dropping this reference once this
  79. // thread is ready to be cleaned up.
  80. ref();
  81. }
  82. Thread::~Thread()
  83. {
  84. VERIFY(!m_process_thread_list_node.is_in_list());
  85. // We shouldn't be queued
  86. VERIFY(m_runnable_priority < 0);
  87. }
  88. Thread::BlockResult Thread::block_impl(BlockTimeout const& timeout, Blocker& blocker)
  89. {
  90. VERIFY(!Processor::current_in_irq());
  91. VERIFY(this == Thread::current());
  92. ScopedCritical critical;
  93. SpinlockLocker scheduler_lock(g_scheduler_lock);
  94. SpinlockLocker block_lock(m_block_lock);
  95. // We need to hold m_block_lock so that nobody can unblock a blocker as soon
  96. // as it is constructed and registered elsewhere
  97. ScopeGuard finalize_guard([&] {
  98. blocker.finalize();
  99. });
  100. if (!blocker.setup_blocker()) {
  101. blocker.will_unblock_immediately_without_blocking(Blocker::UnblockImmediatelyReason::UnblockConditionAlreadyMet);
  102. return BlockResult::NotBlocked;
  103. }
  104. // Relaxed semantics are fine for timeout_unblocked because we
  105. // synchronize on the spin locks already.
  106. Atomic<bool, AK::MemoryOrder::memory_order_relaxed> timeout_unblocked(false);
  107. bool timer_was_added = false;
  108. switch (state()) {
  109. case Thread::State::Stopped:
  110. // It's possible that we were requested to be stopped!
  111. break;
  112. case Thread::State::Running:
  113. VERIFY(m_blocker == nullptr);
  114. break;
  115. default:
  116. VERIFY_NOT_REACHED();
  117. }
  118. m_blocker = &blocker;
  119. if (auto& block_timeout = blocker.override_timeout(timeout); !block_timeout.is_infinite()) {
  120. // Process::kill_all_threads may be called at any time, which will mark all
  121. // threads to die. In that case
  122. timer_was_added = TimerQueue::the().add_timer_without_id(*m_block_timer, block_timeout.clock_id(), block_timeout.absolute_time(), [&]() {
  123. VERIFY(!Processor::current_in_irq());
  124. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  125. VERIFY(!m_block_lock.is_locked_by_current_processor());
  126. // NOTE: this may execute on the same or any other processor!
  127. SpinlockLocker scheduler_lock(g_scheduler_lock);
  128. SpinlockLocker block_lock(m_block_lock);
  129. if (m_blocker && !timeout_unblocked.exchange(true))
  130. unblock();
  131. });
  132. if (!timer_was_added) {
  133. // Timeout is already in the past
  134. blocker.will_unblock_immediately_without_blocking(Blocker::UnblockImmediatelyReason::TimeoutInThePast);
  135. m_blocker = nullptr;
  136. return BlockResult::InterruptedByTimeout;
  137. }
  138. }
  139. blocker.begin_blocking({});
  140. set_state(Thread::State::Blocked);
  141. block_lock.unlock();
  142. scheduler_lock.unlock();
  143. dbgln_if(THREAD_DEBUG, "Thread {} blocking on {} ({}) -->", *this, &blocker, blocker.state_string());
  144. bool did_timeout = false;
  145. u32 lock_count_to_restore = 0;
  146. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  147. for (;;) {
  148. // Yield to the scheduler, and wait for us to resume unblocked.
  149. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  150. VERIFY(Processor::in_critical());
  151. yield_without_releasing_big_lock();
  152. VERIFY(Processor::in_critical());
  153. SpinlockLocker block_lock2(m_block_lock);
  154. if (m_blocker && !m_blocker->can_be_interrupted() && !m_should_die) {
  155. block_lock2.unlock();
  156. dbgln("Thread should not be unblocking, current state: {}", state_string());
  157. set_state(Thread::State::Blocked);
  158. continue;
  159. }
  160. // Prevent the timeout from unblocking this thread if it happens to
  161. // be in the process of firing already
  162. did_timeout |= timeout_unblocked.exchange(true);
  163. if (m_blocker) {
  164. // Remove ourselves...
  165. VERIFY(m_blocker == &blocker);
  166. m_blocker = nullptr;
  167. }
  168. dbgln_if(THREAD_DEBUG, "<-- Thread {} unblocked from {} ({})", *this, &blocker, blocker.state_string());
  169. break;
  170. }
  171. // Notify the blocker that we are no longer blocking. It may need
  172. // to clean up now while we're still holding m_lock
  173. auto result = blocker.end_blocking({}, did_timeout); // calls was_unblocked internally
  174. if (timer_was_added && !did_timeout) {
  175. // Cancel the timer while not holding any locks. This allows
  176. // the timer function to complete before we remove it
  177. // (e.g. if it's on another processor)
  178. TimerQueue::the().cancel_timer(*m_block_timer);
  179. }
  180. if (previous_locked != LockMode::Unlocked) {
  181. // NOTE: This may trigger another call to Thread::block().
  182. relock_process(previous_locked, lock_count_to_restore);
  183. }
  184. return result;
  185. }
  186. void Thread::block(Kernel::Mutex& lock, SpinlockLocker<Spinlock<LockRank::None>>& lock_lock, u32 lock_count)
  187. {
  188. VERIFY(!Processor::current_in_irq());
  189. VERIFY(this == Thread::current());
  190. ScopedCritical critical;
  191. SpinlockLocker scheduler_lock(g_scheduler_lock);
  192. SpinlockLocker block_lock(m_block_lock);
  193. switch (state()) {
  194. case Thread::State::Stopped:
  195. // It's possible that we were requested to be stopped!
  196. break;
  197. case Thread::State::Running:
  198. VERIFY(m_blocker == nullptr);
  199. break;
  200. default:
  201. dbgln("Error: Attempting to block with invalid thread state - {}", state_string());
  202. VERIFY_NOT_REACHED();
  203. }
  204. // If we're blocking on the big-lock we may actually be in the process
  205. // of unblocking from another lock. If that's the case m_blocking_mutex
  206. // is already set
  207. auto& big_lock = process().big_lock();
  208. VERIFY((&lock == &big_lock && m_blocking_mutex != &big_lock) || !m_blocking_mutex);
  209. auto* previous_blocking_mutex = m_blocking_mutex;
  210. m_blocking_mutex = &lock;
  211. m_lock_requested_count = lock_count;
  212. set_state(Thread::State::Blocked);
  213. block_lock.unlock();
  214. scheduler_lock.unlock();
  215. lock_lock.unlock();
  216. dbgln_if(THREAD_DEBUG, "Thread {} blocking on Mutex {}", *this, &lock);
  217. for (;;) {
  218. // Yield to the scheduler, and wait for us to resume unblocked.
  219. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  220. VERIFY(Processor::in_critical());
  221. if (&lock != &big_lock && big_lock.is_exclusively_locked_by_current_thread()) {
  222. // We're locking another lock and already hold the big lock...
  223. // We need to release the big lock
  224. yield_and_release_relock_big_lock();
  225. } else {
  226. // By the time we've reached this another thread might have
  227. // marked us as holding the big lock, so this call must not
  228. // verify that we're not holding it.
  229. yield_without_releasing_big_lock(VerifyLockNotHeld::No);
  230. }
  231. VERIFY(Processor::in_critical());
  232. SpinlockLocker block_lock2(m_block_lock);
  233. VERIFY(!m_blocking_mutex);
  234. m_blocking_mutex = previous_blocking_mutex;
  235. break;
  236. }
  237. lock_lock.lock();
  238. }
  239. u32 Thread::unblock_from_mutex(Kernel::Mutex& mutex)
  240. {
  241. SpinlockLocker scheduler_lock(g_scheduler_lock);
  242. SpinlockLocker block_lock(m_block_lock);
  243. VERIFY(!Processor::current_in_irq());
  244. VERIFY(m_blocking_mutex == &mutex);
  245. dbgln_if(THREAD_DEBUG, "Thread {} unblocked from Mutex {}", *this, &mutex);
  246. auto requested_count = m_lock_requested_count;
  247. m_blocking_mutex = nullptr;
  248. if (Thread::current() == this) {
  249. set_state(Thread::State::Running);
  250. return requested_count;
  251. }
  252. VERIFY(m_state != Thread::State::Runnable && m_state != Thread::State::Running);
  253. set_state(Thread::State::Runnable);
  254. return requested_count;
  255. }
  256. void Thread::unblock_from_blocker(Blocker& blocker)
  257. {
  258. auto do_unblock = [&]() {
  259. SpinlockLocker scheduler_lock(g_scheduler_lock);
  260. SpinlockLocker block_lock(m_block_lock);
  261. if (m_blocker != &blocker)
  262. return;
  263. if (!should_be_stopped() && !is_stopped())
  264. unblock();
  265. };
  266. if (Processor::current_in_irq() != 0) {
  267. Processor::deferred_call_queue([do_unblock = move(do_unblock), self = try_make_weak_ptr().release_value_but_fixme_should_propagate_errors()]() {
  268. if (auto this_thread = self.strong_ref())
  269. do_unblock();
  270. });
  271. } else {
  272. do_unblock();
  273. }
  274. }
  275. void Thread::unblock(u8 signal)
  276. {
  277. VERIFY(!Processor::current_in_irq());
  278. VERIFY(g_scheduler_lock.is_locked_by_current_processor());
  279. VERIFY(m_block_lock.is_locked_by_current_processor());
  280. if (m_state != Thread::State::Blocked)
  281. return;
  282. if (m_blocking_mutex)
  283. return;
  284. VERIFY(m_blocker);
  285. if (signal != 0) {
  286. if (is_handling_page_fault()) {
  287. // Don't let signals unblock threads that are blocked inside a page fault handler.
  288. // This prevents threads from EINTR'ing the inode read in an inode page fault.
  289. // FIXME: There's probably a better way to solve this.
  290. return;
  291. }
  292. if (!m_blocker->can_be_interrupted() && !m_should_die)
  293. return;
  294. m_blocker->set_interrupted_by_signal(signal);
  295. }
  296. m_blocker = nullptr;
  297. if (Thread::current() == this) {
  298. set_state(Thread::State::Running);
  299. return;
  300. }
  301. VERIFY(m_state != Thread::State::Runnable && m_state != Thread::State::Running);
  302. set_state(Thread::State::Runnable);
  303. }
  304. void Thread::set_should_die()
  305. {
  306. if (m_should_die) {
  307. dbgln("{} Should already die", *this);
  308. return;
  309. }
  310. ScopedCritical critical;
  311. // Remember that we should die instead of returning to
  312. // the userspace.
  313. SpinlockLocker lock(g_scheduler_lock);
  314. m_should_die = true;
  315. // NOTE: Even the current thread can technically be in "Stopped"
  316. // state! This is the case when another thread sent a SIGSTOP to
  317. // it while it was running and it calls e.g. exit() before
  318. // the scheduler gets involved again.
  319. if (is_stopped()) {
  320. // If we were stopped, we need to briefly resume so that
  321. // the kernel stacks can clean up. We won't ever return back
  322. // to user mode, though
  323. VERIFY(!process().is_stopped());
  324. resume_from_stopped();
  325. }
  326. if (is_blocked()) {
  327. SpinlockLocker block_lock(m_block_lock);
  328. if (m_blocker) {
  329. // We're blocked in the kernel.
  330. m_blocker->set_interrupted_by_death();
  331. unblock();
  332. }
  333. }
  334. }
  335. void Thread::die_if_needed()
  336. {
  337. VERIFY(Thread::current() == this);
  338. if (!m_should_die)
  339. return;
  340. u32 unlock_count;
  341. [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
  342. dbgln_if(THREAD_DEBUG, "Thread {} is dying", *this);
  343. {
  344. SpinlockLocker lock(g_scheduler_lock);
  345. // It's possible that we don't reach the code after this block if the
  346. // scheduler is invoked and FinalizerTask cleans up this thread, however
  347. // that doesn't matter because we're trying to invoke the scheduler anyway
  348. set_state(Thread::State::Dying);
  349. }
  350. ScopedCritical critical;
  351. // Flag a context switch. Because we're in a critical section,
  352. // Scheduler::yield will actually only mark a pending context switch
  353. // Simply leaving the critical section would not necessarily trigger
  354. // a switch.
  355. Scheduler::yield();
  356. // Now leave the critical section so that we can also trigger the
  357. // actual context switch
  358. Processor::clear_critical();
  359. dbgln("die_if_needed returned from clear_critical!!! in irq: {}", Processor::current_in_irq());
  360. // We should never get here, but the scoped scheduler lock
  361. // will be released by Scheduler::context_switch again
  362. VERIFY_NOT_REACHED();
  363. }
  364. void Thread::exit(void* exit_value)
  365. {
  366. VERIFY(Thread::current() == this);
  367. m_join_blocker_set.thread_did_exit(exit_value);
  368. set_should_die();
  369. u32 unlock_count;
  370. [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
  371. if (m_thread_specific_range.has_value()) {
  372. process().address_space().with([&](auto& space) {
  373. auto* region = space->find_region_from_range(m_thread_specific_range.value());
  374. space->deallocate_region(*region);
  375. });
  376. }
  377. #ifdef ENABLE_KERNEL_COVERAGE_COLLECTION
  378. KCOVDevice::free_thread();
  379. #endif
  380. die_if_needed();
  381. }
  382. void Thread::yield_without_releasing_big_lock(VerifyLockNotHeld verify_lock_not_held)
  383. {
  384. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  385. VERIFY(verify_lock_not_held == VerifyLockNotHeld::No || !process().big_lock().is_exclusively_locked_by_current_thread());
  386. // Disable interrupts here. This ensures we don't accidentally switch contexts twice
  387. InterruptDisabler disable;
  388. Scheduler::yield(); // flag a switch
  389. u32 prev_critical = Processor::clear_critical();
  390. // NOTE: We may be on a different CPU now!
  391. Processor::restore_critical(prev_critical);
  392. }
  393. void Thread::yield_and_release_relock_big_lock()
  394. {
  395. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  396. // Disable interrupts here. This ensures we don't accidentally switch contexts twice
  397. InterruptDisabler disable;
  398. Scheduler::yield(); // flag a switch
  399. u32 lock_count_to_restore = 0;
  400. auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
  401. // NOTE: Even though we call Scheduler::yield here, unless we happen
  402. // to be outside of a critical section, the yield will be postponed
  403. // until leaving it in relock_process.
  404. relock_process(previous_locked, lock_count_to_restore);
  405. }
  406. LockMode Thread::unlock_process_if_locked(u32& lock_count_to_restore)
  407. {
  408. return process().big_lock().force_unlock_exclusive_if_locked(lock_count_to_restore);
  409. }
  410. void Thread::relock_process(LockMode previous_locked, u32 lock_count_to_restore)
  411. {
  412. // Clearing the critical section may trigger the context switch
  413. // flagged by calling Scheduler::yield above.
  414. // We have to do it this way because we intentionally
  415. // leave the critical section here to be able to switch contexts.
  416. u32 prev_critical = Processor::clear_critical();
  417. // CONTEXT SWITCH HAPPENS HERE!
  418. // NOTE: We may be on a different CPU now!
  419. Processor::restore_critical(prev_critical);
  420. if (previous_locked != LockMode::Unlocked) {
  421. // We've unblocked, relock the process if needed and carry on.
  422. process().big_lock().restore_exclusive_lock(lock_count_to_restore);
  423. }
  424. }
  425. // NOLINTNEXTLINE(readability-make-member-function-const) False positive; We call block<SleepBlocker> which is not const
  426. auto Thread::sleep(clockid_t clock_id, Time const& duration, Time* remaining_time) -> BlockResult
  427. {
  428. VERIFY(state() == Thread::State::Running);
  429. return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(false, &duration, nullptr, clock_id), remaining_time);
  430. }
  431. // NOLINTNEXTLINE(readability-make-member-function-const) False positive; We call block<SleepBlocker> which is not const
  432. auto Thread::sleep_until(clockid_t clock_id, Time const& deadline) -> BlockResult
  433. {
  434. VERIFY(state() == Thread::State::Running);
  435. return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(true, &deadline, nullptr, clock_id));
  436. }
  437. StringView Thread::state_string() const
  438. {
  439. switch (state()) {
  440. case Thread::State::Invalid:
  441. return "Invalid"sv;
  442. case Thread::State::Runnable:
  443. return "Runnable"sv;
  444. case Thread::State::Running:
  445. return "Running"sv;
  446. case Thread::State::Dying:
  447. return "Dying"sv;
  448. case Thread::State::Dead:
  449. return "Dead"sv;
  450. case Thread::State::Stopped:
  451. return "Stopped"sv;
  452. case Thread::State::Blocked: {
  453. SpinlockLocker block_lock(m_block_lock);
  454. if (m_blocking_mutex)
  455. return "Mutex"sv;
  456. if (m_blocker)
  457. return m_blocker->state_string();
  458. VERIFY_NOT_REACHED();
  459. }
  460. }
  461. PANIC("Thread::state_string(): Invalid state: {}", (int)state());
  462. }
  463. void Thread::finalize()
  464. {
  465. VERIFY(Thread::current() == g_finalizer);
  466. VERIFY(Thread::current() != this);
  467. #if LOCK_DEBUG
  468. VERIFY(!m_lock.is_locked_by_current_processor());
  469. if (lock_count() > 0) {
  470. dbgln("Thread {} leaking {} Locks!", *this, lock_count());
  471. SpinlockLocker list_lock(m_holding_locks_lock);
  472. for (auto& info : m_holding_locks_list) {
  473. auto const& location = info.lock_location;
  474. dbgln(" - Mutex: \"{}\" @ {} locked in function \"{}\" at \"{}:{}\" with a count of: {}", info.lock->name(), info.lock, location.function_name(), location.filename(), location.line_number(), info.count);
  475. }
  476. VERIFY_NOT_REACHED();
  477. }
  478. #endif
  479. {
  480. SpinlockLocker lock(g_scheduler_lock);
  481. dbgln_if(THREAD_DEBUG, "Finalizing thread {}", *this);
  482. set_state(Thread::State::Dead);
  483. m_join_blocker_set.thread_finalizing();
  484. }
  485. if (m_dump_backtrace_on_finalization) {
  486. auto trace_or_error = backtrace();
  487. if (!trace_or_error.is_error()) {
  488. auto trace = trace_or_error.release_value();
  489. dbgln("Backtrace:");
  490. kernelputstr(trace->characters(), trace->length());
  491. }
  492. }
  493. drop_thread_count();
  494. }
  495. void Thread::drop_thread_count()
  496. {
  497. bool is_last = process().remove_thread(*this);
  498. if (is_last)
  499. process().finalize();
  500. }
  501. void Thread::finalize_dying_threads()
  502. {
  503. VERIFY(Thread::current() == g_finalizer);
  504. Vector<Thread*, 32> dying_threads;
  505. {
  506. SpinlockLocker lock(g_scheduler_lock);
  507. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  508. if (!thread.is_finalizable())
  509. return;
  510. auto result = dying_threads.try_append(&thread);
  511. // We ignore allocation failures above the first 32 guaranteed thread slots, and
  512. // just flag our future-selves to finalize these threads at a later point
  513. if (result.is_error())
  514. g_finalizer_has_work.store(true, AK::MemoryOrder::memory_order_release);
  515. });
  516. }
  517. for (auto* thread : dying_threads) {
  518. RefPtr<Process> const process = thread->process();
  519. dbgln_if(PROCESS_DEBUG, "Before finalization, {} has {} refs and its process has {}",
  520. *thread, thread->ref_count(), thread->process().ref_count());
  521. thread->finalize();
  522. dbgln_if(PROCESS_DEBUG, "After finalization, {} has {} refs and its process has {}",
  523. *thread, thread->ref_count(), thread->process().ref_count());
  524. // This thread will never execute again, drop the running reference
  525. // NOTE: This may not necessarily drop the last reference if anything
  526. // else is still holding onto this thread!
  527. thread->unref();
  528. }
  529. }
  530. void Thread::update_time_scheduled(u64 current_scheduler_time, bool is_kernel, bool no_longer_running)
  531. {
  532. if (m_last_time_scheduled.has_value()) {
  533. u64 delta;
  534. if (current_scheduler_time >= m_last_time_scheduled.value())
  535. delta = current_scheduler_time - m_last_time_scheduled.value();
  536. else
  537. delta = m_last_time_scheduled.value() - current_scheduler_time; // the unlikely event that the clock wrapped
  538. if (delta != 0) {
  539. // Add it to the global total *before* updating the thread's value!
  540. Scheduler::add_time_scheduled(delta, is_kernel);
  541. auto& total_time = is_kernel ? m_total_time_scheduled_kernel : m_total_time_scheduled_user;
  542. total_time.fetch_add(delta, AK::memory_order_relaxed);
  543. }
  544. }
  545. if (no_longer_running)
  546. m_last_time_scheduled = {};
  547. else
  548. m_last_time_scheduled = current_scheduler_time;
  549. }
  550. bool Thread::tick()
  551. {
  552. if (previous_mode() == ExecutionMode::Kernel) {
  553. ++m_process->m_ticks_in_kernel;
  554. ++m_ticks_in_kernel;
  555. } else {
  556. ++m_process->m_ticks_in_user;
  557. ++m_ticks_in_user;
  558. }
  559. --m_ticks_left;
  560. return m_ticks_left != 0;
  561. }
  562. void Thread::check_dispatch_pending_signal()
  563. {
  564. auto result = DispatchSignalResult::Continue;
  565. {
  566. SpinlockLocker scheduler_lock(g_scheduler_lock);
  567. if (pending_signals_for_state() != 0) {
  568. result = dispatch_one_pending_signal();
  569. }
  570. }
  571. if (result == DispatchSignalResult::Yield) {
  572. yield_without_releasing_big_lock();
  573. }
  574. }
  575. u32 Thread::pending_signals() const
  576. {
  577. SpinlockLocker lock(g_scheduler_lock);
  578. return pending_signals_for_state();
  579. }
  580. u32 Thread::pending_signals_for_state() const
  581. {
  582. VERIFY(g_scheduler_lock.is_locked_by_current_processor());
  583. constexpr u32 stopped_signal_mask = (1 << (SIGCONT - 1)) | (1 << (SIGKILL - 1)) | (1 << (SIGTRAP - 1));
  584. if (is_handling_page_fault())
  585. return 0;
  586. return m_state != State::Stopped ? m_pending_signals : m_pending_signals & stopped_signal_mask;
  587. }
  588. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  589. {
  590. VERIFY(signal < NSIG);
  591. VERIFY(process().is_user_process());
  592. SpinlockLocker scheduler_lock(g_scheduler_lock);
  593. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  594. if (should_ignore_signal(signal)) {
  595. dbgln_if(SIGNAL_DEBUG, "Signal {} was ignored by {}", signal, process());
  596. return;
  597. }
  598. if constexpr (SIGNAL_DEBUG) {
  599. if (sender)
  600. dbgln("Signal: {} sent {} to {}", *sender, signal, process());
  601. else
  602. dbgln("Signal: Kernel send {} to {}", signal, process());
  603. }
  604. m_pending_signals |= 1 << (signal - 1);
  605. m_signal_senders[signal] = sender ? sender->pid() : pid();
  606. m_have_any_unmasked_pending_signals.store((pending_signals_for_state() & ~m_signal_mask) != 0, AK::memory_order_release);
  607. m_signal_blocker_set.unblock_all_blockers_whose_conditions_are_met();
  608. if (!has_unmasked_pending_signals())
  609. return;
  610. if (m_state == Thread::State::Stopped) {
  611. if (pending_signals_for_state() != 0) {
  612. dbgln_if(SIGNAL_DEBUG, "Signal: Resuming stopped {} to deliver signal {}", *this, signal);
  613. resume_from_stopped();
  614. }
  615. } else {
  616. SpinlockLocker block_lock(m_block_lock);
  617. dbgln_if(SIGNAL_DEBUG, "Signal: Unblocking {} to deliver signal {}", *this, signal);
  618. unblock(signal);
  619. }
  620. }
  621. u32 Thread::update_signal_mask(u32 signal_mask)
  622. {
  623. SpinlockLocker lock(g_scheduler_lock);
  624. auto previous_signal_mask = m_signal_mask;
  625. m_signal_mask = signal_mask;
  626. m_have_any_unmasked_pending_signals.store((pending_signals_for_state() & ~m_signal_mask) != 0, AK::memory_order_release);
  627. return previous_signal_mask;
  628. }
  629. u32 Thread::signal_mask() const
  630. {
  631. SpinlockLocker lock(g_scheduler_lock);
  632. return m_signal_mask;
  633. }
  634. u32 Thread::signal_mask_block(sigset_t signal_set, bool block)
  635. {
  636. SpinlockLocker lock(g_scheduler_lock);
  637. auto previous_signal_mask = m_signal_mask;
  638. if (block)
  639. m_signal_mask |= signal_set;
  640. else
  641. m_signal_mask &= ~signal_set;
  642. m_have_any_unmasked_pending_signals.store((pending_signals_for_state() & ~m_signal_mask) != 0, AK::memory_order_release);
  643. return previous_signal_mask;
  644. }
  645. void Thread::reset_signals_for_exec()
  646. {
  647. SpinlockLocker lock(g_scheduler_lock);
  648. // The signal mask is preserved across execve(2).
  649. // The pending signal set is preserved across an execve(2).
  650. m_have_any_unmasked_pending_signals.store(false, AK::memory_order_release);
  651. m_signal_action_masks.fill({});
  652. // A successful call to execve(2) removes any existing alternate signal stack
  653. m_alternative_signal_stack = 0;
  654. m_alternative_signal_stack_size = 0;
  655. }
  656. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  657. // thread into a state where the signal handler must be
  658. // invoked immediately, otherwise it will continue to fault.
  659. // This function should be used in an exception handler to
  660. // ensure that when the thread resumes, it's executing in
  661. // the appropriate signal handler.
  662. void Thread::send_urgent_signal_to_self(u8 signal)
  663. {
  664. VERIFY(Thread::current() == this);
  665. DispatchSignalResult result;
  666. {
  667. SpinlockLocker lock(g_scheduler_lock);
  668. result = dispatch_signal(signal);
  669. }
  670. if (result == DispatchSignalResult::Terminate) {
  671. Thread::current()->die_if_needed();
  672. VERIFY_NOT_REACHED(); // dispatch_signal will request termination of the thread, so the above call should never return
  673. }
  674. if (result == DispatchSignalResult::Yield)
  675. yield_and_release_relock_big_lock();
  676. }
  677. DispatchSignalResult Thread::dispatch_one_pending_signal()
  678. {
  679. VERIFY(g_scheduler_lock.is_locked_by_current_processor());
  680. u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
  681. if (signal_candidates == 0)
  682. return DispatchSignalResult::Continue;
  683. u8 signal = 1;
  684. for (; signal < NSIG; ++signal) {
  685. if ((signal_candidates & (1 << (signal - 1))) != 0) {
  686. break;
  687. }
  688. }
  689. return dispatch_signal(signal);
  690. }
  691. DispatchSignalResult Thread::try_dispatch_one_pending_signal(u8 signal)
  692. {
  693. VERIFY(signal != 0);
  694. SpinlockLocker scheduler_lock(g_scheduler_lock);
  695. u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
  696. if ((signal_candidates & (1 << (signal - 1))) == 0)
  697. return DispatchSignalResult::Continue;
  698. return dispatch_signal(signal);
  699. }
  700. enum class DefaultSignalAction {
  701. Terminate,
  702. Ignore,
  703. DumpCore,
  704. Stop,
  705. Continue,
  706. };
  707. static DefaultSignalAction default_signal_action(u8 signal)
  708. {
  709. VERIFY(signal && signal < NSIG);
  710. switch (signal) {
  711. case SIGHUP:
  712. case SIGINT:
  713. case SIGKILL:
  714. case SIGPIPE:
  715. case SIGALRM:
  716. case SIGUSR1:
  717. case SIGUSR2:
  718. case SIGVTALRM:
  719. case SIGSTKFLT:
  720. case SIGIO:
  721. case SIGPROF:
  722. case SIGTERM:
  723. case SIGCANCEL:
  724. return DefaultSignalAction::Terminate;
  725. case SIGCHLD:
  726. case SIGURG:
  727. case SIGWINCH:
  728. case SIGINFO:
  729. return DefaultSignalAction::Ignore;
  730. case SIGQUIT:
  731. case SIGILL:
  732. case SIGTRAP:
  733. case SIGABRT:
  734. case SIGBUS:
  735. case SIGFPE:
  736. case SIGSEGV:
  737. case SIGXCPU:
  738. case SIGXFSZ:
  739. case SIGSYS:
  740. return DefaultSignalAction::DumpCore;
  741. case SIGCONT:
  742. return DefaultSignalAction::Continue;
  743. case SIGSTOP:
  744. case SIGTSTP:
  745. case SIGTTIN:
  746. case SIGTTOU:
  747. return DefaultSignalAction::Stop;
  748. default:
  749. VERIFY_NOT_REACHED();
  750. }
  751. }
  752. bool Thread::should_ignore_signal(u8 signal) const
  753. {
  754. VERIFY(signal < NSIG);
  755. auto const& action = m_process->m_signal_action_data[signal];
  756. if (action.handler_or_sigaction.is_null())
  757. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  758. return ((sighandler_t)action.handler_or_sigaction.get() == SIG_IGN);
  759. }
  760. bool Thread::has_signal_handler(u8 signal) const
  761. {
  762. VERIFY(signal < NSIG);
  763. auto const& action = m_process->m_signal_action_data[signal];
  764. return !action.handler_or_sigaction.is_null();
  765. }
  766. bool Thread::is_signal_masked(u8 signal) const
  767. {
  768. VERIFY(signal < NSIG);
  769. return (1 << (signal - 1)) & m_signal_mask;
  770. }
  771. bool Thread::has_alternative_signal_stack() const
  772. {
  773. return m_alternative_signal_stack_size != 0;
  774. }
  775. bool Thread::is_in_alternative_signal_stack() const
  776. {
  777. auto sp = get_register_dump_from_stack().userspace_sp();
  778. return sp >= m_alternative_signal_stack && sp < m_alternative_signal_stack + m_alternative_signal_stack_size;
  779. }
  780. static ErrorOr<void> push_value_on_user_stack(FlatPtr& stack, FlatPtr data)
  781. {
  782. stack -= sizeof(FlatPtr);
  783. return copy_to_user((FlatPtr*)stack, &data);
  784. }
  785. template<typename T>
  786. static ErrorOr<void> copy_value_on_user_stack(FlatPtr& stack, T const& data)
  787. {
  788. stack -= sizeof(data);
  789. return copy_to_user((RemoveCVReference<T>*)stack, &data);
  790. }
  791. void Thread::resume_from_stopped()
  792. {
  793. VERIFY(is_stopped());
  794. VERIFY(m_stop_state != State::Invalid);
  795. VERIFY(g_scheduler_lock.is_locked_by_current_processor());
  796. if (m_stop_state == Thread::State::Blocked) {
  797. SpinlockLocker block_lock(m_block_lock);
  798. if (m_blocker || m_blocking_mutex) {
  799. // Hasn't been unblocked yet
  800. set_state(Thread::State::Blocked, 0);
  801. } else {
  802. // Was unblocked while stopped
  803. set_state(Thread::State::Runnable);
  804. }
  805. } else {
  806. set_state(m_stop_state, 0);
  807. }
  808. }
  809. DispatchSignalResult Thread::dispatch_signal(u8 signal)
  810. {
  811. VERIFY_INTERRUPTS_DISABLED();
  812. VERIFY(g_scheduler_lock.is_locked_by_current_processor());
  813. VERIFY(signal > 0 && signal <= NSIG);
  814. VERIFY(process().is_user_process());
  815. VERIFY(this == Thread::current());
  816. dbgln_if(SIGNAL_DEBUG, "Dispatch signal {} to {}, state: {}", signal, *this, state_string());
  817. if (m_state == Thread::State::Invalid || !is_initialized()) {
  818. // Thread has barely been created, we need to wait until it is
  819. // at least in Runnable state and is_initialized() returns true,
  820. // which indicates that it is fully set up an we actually have
  821. // a register state on the stack that we can modify
  822. return DispatchSignalResult::Deferred;
  823. }
  824. auto& action = m_process->m_signal_action_data[signal];
  825. auto sender_pid = m_signal_senders[signal];
  826. auto sender = Process::from_pid_ignoring_jails(sender_pid);
  827. if (!current_trap() && !action.handler_or_sigaction.is_null()) {
  828. // We're trying dispatch a handled signal to a user process that was scheduled
  829. // after a yielding/blocking kernel thread, we don't have a register capture of
  830. // the thread, so just defer processing the signal to later.
  831. return DispatchSignalResult::Deferred;
  832. }
  833. // Mark this signal as handled.
  834. m_pending_signals &= ~(1 << (signal - 1));
  835. m_have_any_unmasked_pending_signals.store((m_pending_signals & ~m_signal_mask) != 0, AK::memory_order_release);
  836. auto& process = this->process();
  837. auto* tracer = process.tracer();
  838. if (signal == SIGSTOP || (tracer && default_signal_action(signal) == DefaultSignalAction::DumpCore)) {
  839. dbgln_if(SIGNAL_DEBUG, "Signal {} stopping this thread", signal);
  840. if (tracer)
  841. tracer->set_regs(get_register_dump_from_stack());
  842. set_state(Thread::State::Stopped, signal);
  843. return DispatchSignalResult::Yield;
  844. }
  845. if (signal == SIGCONT) {
  846. dbgln("signal: SIGCONT resuming {}", *this);
  847. } else {
  848. if (tracer) {
  849. // when a thread is traced, it should be stopped whenever it receives a signal
  850. // the tracer is notified of this by using waitpid()
  851. // only "pending signals" from the tracer are sent to the tracee
  852. if (!tracer->has_pending_signal(signal)) {
  853. dbgln("signal: {} stopping {} for tracer", signal, *this);
  854. set_state(Thread::State::Stopped, signal);
  855. return DispatchSignalResult::Yield;
  856. }
  857. tracer->unset_signal(signal);
  858. }
  859. }
  860. auto handler_vaddr = action.handler_or_sigaction;
  861. if (handler_vaddr.is_null()) {
  862. switch (default_signal_action(signal)) {
  863. case DefaultSignalAction::Stop:
  864. set_state(Thread::State::Stopped, signal);
  865. return DispatchSignalResult::Yield;
  866. case DefaultSignalAction::DumpCore:
  867. process.set_should_generate_coredump(true);
  868. process.for_each_thread([](auto& thread) {
  869. thread.set_dump_backtrace_on_finalization();
  870. });
  871. [[fallthrough]];
  872. case DefaultSignalAction::Terminate:
  873. m_process->terminate_due_to_signal(signal);
  874. return DispatchSignalResult::Terminate;
  875. case DefaultSignalAction::Ignore:
  876. VERIFY_NOT_REACHED();
  877. case DefaultSignalAction::Continue:
  878. return DispatchSignalResult::Continue;
  879. }
  880. VERIFY_NOT_REACHED();
  881. }
  882. if ((sighandler_t)handler_vaddr.as_ptr() == SIG_IGN) {
  883. dbgln_if(SIGNAL_DEBUG, "Ignored signal {}", signal);
  884. return DispatchSignalResult::Continue;
  885. }
  886. ScopedAddressSpaceSwitcher switcher(m_process);
  887. m_currently_handled_signal = signal;
  888. u32 old_signal_mask = m_signal_mask;
  889. u32 new_signal_mask = m_signal_action_masks[signal].value_or(action.mask);
  890. if ((action.flags & SA_NODEFER) == SA_NODEFER)
  891. new_signal_mask &= ~(1 << (signal - 1));
  892. else
  893. new_signal_mask |= 1 << (signal - 1);
  894. m_signal_mask |= new_signal_mask;
  895. m_have_any_unmasked_pending_signals.store((m_pending_signals & ~m_signal_mask) != 0, AK::memory_order_release);
  896. bool use_alternative_stack = ((action.flags & SA_ONSTACK) != 0) && has_alternative_signal_stack() && !is_in_alternative_signal_stack();
  897. auto setup_stack = [&](RegisterState& state) -> ErrorOr<void> {
  898. FlatPtr stack;
  899. if (use_alternative_stack)
  900. stack = m_alternative_signal_stack + m_alternative_signal_stack_size;
  901. else
  902. stack = state.userspace_sp();
  903. dbgln_if(SIGNAL_DEBUG, "Setting up user stack to return to IP {:p}, SP {:p}", state.ip(), state.userspace_sp());
  904. __ucontext ucontext {
  905. .uc_link = nullptr,
  906. .uc_sigmask = old_signal_mask,
  907. .uc_stack = {
  908. .ss_sp = bit_cast<void*>(stack),
  909. .ss_flags = action.flags & SA_ONSTACK,
  910. .ss_size = use_alternative_stack ? m_alternative_signal_stack_size : 0,
  911. },
  912. .uc_mcontext = {},
  913. };
  914. copy_kernel_registers_into_ptrace_registers(static_cast<PtraceRegisters&>(ucontext.uc_mcontext), state);
  915. auto fill_signal_info_for_signal = [&](siginfo& signal_info) {
  916. if (signal == SIGCHLD) {
  917. if (!sender) {
  918. signal_info.si_code = CLD_EXITED;
  919. return;
  920. }
  921. auto const* thread = sender->thread_list().with([](auto& list) { return list.is_empty() ? nullptr : list.first(); });
  922. if (!thread) {
  923. signal_info.si_code = CLD_EXITED;
  924. return;
  925. }
  926. switch (thread->m_state) {
  927. case State::Dead:
  928. if (sender->should_generate_coredump() && sender->is_dumpable()) {
  929. signal_info.si_code = CLD_DUMPED;
  930. signal_info.si_status = sender->termination_signal();
  931. return;
  932. }
  933. [[fallthrough]];
  934. case State::Dying:
  935. if (sender->termination_signal() == 0) {
  936. signal_info.si_code = CLD_EXITED;
  937. signal_info.si_status = sender->termination_status();
  938. return;
  939. }
  940. signal_info.si_code = CLD_KILLED;
  941. signal_info.si_status = sender->termination_signal();
  942. return;
  943. case State::Runnable:
  944. case State::Running:
  945. case State::Blocked:
  946. signal_info.si_code = CLD_CONTINUED;
  947. return;
  948. case State::Stopped:
  949. signal_info.si_code = CLD_STOPPED;
  950. return;
  951. case State::Invalid:
  952. // Something is wrong, but we're just an observer.
  953. break;
  954. }
  955. }
  956. signal_info.si_code = SI_NOINFO;
  957. };
  958. siginfo signal_info {
  959. .si_signo = signal,
  960. // Filled in below by fill_signal_info_for_signal.
  961. .si_code = 0,
  962. // Set for SI_TIMER, we don't have the data here.
  963. .si_errno = 0,
  964. .si_pid = sender_pid.value(),
  965. .si_uid = sender ? sender->credentials()->uid().value() : 0,
  966. // Set for SIGILL, SIGFPE, SIGSEGV and SIGBUS
  967. // FIXME: We don't generate these signals in a way that can be handled.
  968. .si_addr = 0,
  969. // Set for SIGCHLD.
  970. .si_status = 0,
  971. // Set for SIGPOLL, we don't have SIGPOLL.
  972. .si_band = 0,
  973. // Set for SI_QUEUE, SI_TIMER, SI_ASYNCIO and SI_MESGQ
  974. // We do not generate any of these.
  975. .si_value = {
  976. .sival_int = 0,
  977. },
  978. };
  979. if (action.flags & SA_SIGINFO)
  980. fill_signal_info_for_signal(signal_info);
  981. #if ARCH(X86_64)
  982. constexpr static FlatPtr thread_red_zone_size = 128;
  983. #elif ARCH(AARCH64)
  984. constexpr static FlatPtr thread_red_zone_size = 0; // FIXME
  985. TODO_AARCH64();
  986. #else
  987. # error Unknown architecture in dispatch_signal
  988. #endif
  989. // Align the stack to 16 bytes.
  990. // Note that we push some elements on to the stack before the return address,
  991. // so we need to account for this here.
  992. constexpr static FlatPtr elements_pushed_on_stack_before_handler_address = 1; // one slot for a saved register
  993. FlatPtr const extra_bytes_pushed_on_stack_before_handler_address = sizeof(ucontext) + sizeof(signal_info);
  994. FlatPtr stack_alignment = (stack - elements_pushed_on_stack_before_handler_address * sizeof(FlatPtr) + extra_bytes_pushed_on_stack_before_handler_address) % 16;
  995. // Also note that we have to skip the thread red-zone (if needed), so do that here.
  996. stack -= thread_red_zone_size + stack_alignment;
  997. auto start_of_stack = stack;
  998. TRY(push_value_on_user_stack(stack, 0)); // syscall return value slot
  999. TRY(copy_value_on_user_stack(stack, ucontext));
  1000. auto pointer_to_ucontext = stack;
  1001. TRY(copy_value_on_user_stack(stack, signal_info));
  1002. auto pointer_to_signal_info = stack;
  1003. // Make sure we actually pushed as many elements as we claimed to have pushed.
  1004. if (start_of_stack - stack != elements_pushed_on_stack_before_handler_address * sizeof(FlatPtr) + extra_bytes_pushed_on_stack_before_handler_address) {
  1005. PANIC("Stack in invalid state after signal trampoline, expected {:x} but got {:x}",
  1006. start_of_stack - elements_pushed_on_stack_before_handler_address * sizeof(FlatPtr) - extra_bytes_pushed_on_stack_before_handler_address, stack);
  1007. }
  1008. VERIFY(stack % 16 == 0);
  1009. #if ARCH(X86_64)
  1010. // Save the FPU/SSE state
  1011. TRY(copy_value_on_user_stack(stack, fpu_state()));
  1012. #endif
  1013. TRY(push_value_on_user_stack(stack, pointer_to_ucontext));
  1014. TRY(push_value_on_user_stack(stack, pointer_to_signal_info));
  1015. TRY(push_value_on_user_stack(stack, signal));
  1016. TRY(push_value_on_user_stack(stack, handler_vaddr.get()));
  1017. // We write back the adjusted stack value into the register state.
  1018. // We have to do this because we can't just pass around a reference to a packed field, as it's UB.
  1019. state.set_userspace_sp(stack);
  1020. return {};
  1021. };
  1022. // We now place the thread state on the userspace stack.
  1023. // Note that we use a RegisterState.
  1024. // Conversely, when the thread isn't blocking the RegisterState may not be
  1025. // valid (fork, exec etc) but the tss will, so we use that instead.
  1026. auto& regs = get_register_dump_from_stack();
  1027. auto result = setup_stack(regs);
  1028. if (result.is_error()) {
  1029. dbgln("Invalid stack pointer: {}", regs.userspace_sp());
  1030. process.set_should_generate_coredump(true);
  1031. process.for_each_thread([](auto& thread) {
  1032. thread.set_dump_backtrace_on_finalization();
  1033. });
  1034. m_process->terminate_due_to_signal(signal);
  1035. return DispatchSignalResult::Terminate;
  1036. }
  1037. auto signal_trampoline_addr = process.signal_trampoline().get();
  1038. regs.set_ip(signal_trampoline_addr);
  1039. #if ARCH(X86_64)
  1040. // Userspace flags might be invalid for function entry, according to SYSV ABI (section 3.2.1).
  1041. // Set them to a known-good value to avoid weird handler misbehavior.
  1042. // Only IF (and the reserved bit 1) are set.
  1043. regs.set_flags(2 | (regs.rflags & ~safe_eflags_mask));
  1044. #endif
  1045. dbgln_if(SIGNAL_DEBUG, "Thread in state '{}' has been primed with signal handler {:p} to deliver {}", state_string(), m_regs.ip(), signal);
  1046. return DispatchSignalResult::Continue;
  1047. }
  1048. RegisterState& Thread::get_register_dump_from_stack()
  1049. {
  1050. auto* trap = current_trap();
  1051. // We should *always* have a trap. If we don't we're probably a kernel
  1052. // thread that hasn't been preempted. If we want to support this, we
  1053. // need to capture the registers probably into m_regs and return it
  1054. VERIFY(trap);
  1055. while (trap) {
  1056. if (!trap->next_trap)
  1057. break;
  1058. trap = trap->next_trap;
  1059. }
  1060. return *trap->regs;
  1061. }
  1062. ErrorOr<NonnullRefPtr<Thread>> Thread::clone(NonnullRefPtr<Process> process)
  1063. {
  1064. auto clone = TRY(Thread::create(move(process)));
  1065. m_signal_action_masks.span().copy_to(clone->m_signal_action_masks);
  1066. clone->m_signal_mask = m_signal_mask;
  1067. clone->m_fpu_state = m_fpu_state;
  1068. clone->m_thread_specific_data = m_thread_specific_data;
  1069. return clone;
  1070. }
  1071. void Thread::set_state(State new_state, u8 stop_signal)
  1072. {
  1073. State previous_state;
  1074. VERIFY(g_scheduler_lock.is_locked_by_current_processor());
  1075. if (new_state == m_state)
  1076. return;
  1077. {
  1078. previous_state = m_state;
  1079. if (previous_state == Thread::State::Invalid) {
  1080. // If we were *just* created, we may have already pending signals
  1081. if (has_unmasked_pending_signals()) {
  1082. dbgln_if(THREAD_DEBUG, "Dispatch pending signals to new thread {}", *this);
  1083. dispatch_one_pending_signal();
  1084. }
  1085. }
  1086. m_state = new_state;
  1087. dbgln_if(THREAD_DEBUG, "Set thread {} state to {}", *this, state_string());
  1088. }
  1089. if (previous_state == Thread::State::Runnable) {
  1090. Scheduler::dequeue_runnable_thread(*this);
  1091. } else if (previous_state == Thread::State::Stopped) {
  1092. m_stop_state = State::Invalid;
  1093. auto& process = this->process();
  1094. if (process.set_stopped(false)) {
  1095. process.for_each_thread([&](auto& thread) {
  1096. if (&thread == this)
  1097. return;
  1098. if (!thread.is_stopped())
  1099. return;
  1100. dbgln_if(THREAD_DEBUG, "Resuming peer thread {}", thread);
  1101. thread.resume_from_stopped();
  1102. });
  1103. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Continued);
  1104. // Tell the parent process (if any) about this change.
  1105. if (auto parent = Process::from_pid_ignoring_jails(process.ppid())) {
  1106. [[maybe_unused]] auto result = parent->send_signal(SIGCHLD, &process);
  1107. }
  1108. }
  1109. }
  1110. if (m_state == Thread::State::Runnable) {
  1111. Scheduler::enqueue_runnable_thread(*this);
  1112. Processor::smp_wake_n_idle_processors(1);
  1113. } else if (m_state == Thread::State::Stopped) {
  1114. // We don't want to restore to Running state, only Runnable!
  1115. m_stop_state = previous_state != Thread::State::Running ? previous_state : Thread::State::Runnable;
  1116. auto& process = this->process();
  1117. if (!process.set_stopped(true)) {
  1118. process.for_each_thread([&](auto& thread) {
  1119. if (&thread == this)
  1120. return;
  1121. if (thread.is_stopped())
  1122. return;
  1123. dbgln_if(THREAD_DEBUG, "Stopping peer thread {}", thread);
  1124. thread.set_state(Thread::State::Stopped, stop_signal);
  1125. });
  1126. process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Stopped, stop_signal);
  1127. // Tell the parent process (if any) about this change.
  1128. if (auto parent = Process::from_pid_ignoring_jails(process.ppid())) {
  1129. [[maybe_unused]] auto result = parent->send_signal(SIGCHLD, &process);
  1130. }
  1131. }
  1132. } else if (m_state == Thread::State::Dying) {
  1133. VERIFY(previous_state != Thread::State::Blocked);
  1134. if (this != Thread::current() && is_finalizable()) {
  1135. // Some other thread set this thread to Dying, notify the
  1136. // finalizer right away as it can be cleaned up now
  1137. Scheduler::notify_finalizer();
  1138. }
  1139. }
  1140. }
  1141. struct RecognizedSymbol {
  1142. FlatPtr address;
  1143. KernelSymbol const* symbol { nullptr };
  1144. };
  1145. static ErrorOr<bool> symbolicate(RecognizedSymbol const& symbol, Process& process, StringBuilder& builder)
  1146. {
  1147. if (symbol.address == 0)
  1148. return false;
  1149. auto credentials = process.credentials();
  1150. bool mask_kernel_addresses = !credentials->is_superuser();
  1151. if (!symbol.symbol) {
  1152. if (!Memory::is_user_address(VirtualAddress(symbol.address))) {
  1153. TRY(builder.try_append("0xdeadc0de\n"sv));
  1154. } else {
  1155. TRY(process.address_space().with([&](auto& space) -> ErrorOr<void> {
  1156. if (auto* region = space->find_region_containing({ VirtualAddress(symbol.address), sizeof(FlatPtr) })) {
  1157. size_t offset = symbol.address - region->vaddr().get();
  1158. if (auto region_name = region->name(); !region_name.is_null() && !region_name.is_empty())
  1159. TRY(builder.try_appendff("{:p} {} + {:#x}\n", (void*)symbol.address, region_name, offset));
  1160. else
  1161. TRY(builder.try_appendff("{:p} {:p} + {:#x}\n", (void*)symbol.address, region->vaddr().as_ptr(), offset));
  1162. } else {
  1163. TRY(builder.try_appendff("{:p}\n", symbol.address));
  1164. }
  1165. return {};
  1166. }));
  1167. }
  1168. return true;
  1169. }
  1170. unsigned offset = symbol.address - symbol.symbol->address;
  1171. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
  1172. TRY(builder.try_appendff("{:p}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address)));
  1173. else
  1174. TRY(builder.try_appendff("{:p} {} + {:#x}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address), symbol.symbol->name, offset));
  1175. return true;
  1176. }
  1177. ErrorOr<NonnullOwnPtr<KString>> Thread::backtrace()
  1178. {
  1179. Vector<RecognizedSymbol, 128> recognized_symbols;
  1180. auto& process = const_cast<Process&>(this->process());
  1181. auto stack_trace = TRY(Processor::capture_stack_trace(*this));
  1182. VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
  1183. ScopedAddressSpaceSwitcher switcher(process);
  1184. for (auto& frame : stack_trace) {
  1185. if (Memory::is_user_range(VirtualAddress(frame), sizeof(FlatPtr) * 2)) {
  1186. TRY(recognized_symbols.try_append({ frame }));
  1187. } else {
  1188. TRY(recognized_symbols.try_append({ frame, symbolicate_kernel_address(frame) }));
  1189. }
  1190. }
  1191. StringBuilder builder;
  1192. for (auto& symbol : recognized_symbols) {
  1193. if (!TRY(symbolicate(symbol, process, builder)))
  1194. break;
  1195. }
  1196. return KString::try_create(builder.string_view());
  1197. }
  1198. size_t Thread::thread_specific_region_alignment() const
  1199. {
  1200. return max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  1201. }
  1202. size_t Thread::thread_specific_region_size() const
  1203. {
  1204. return align_up_to(process().m_master_tls_size, thread_specific_region_alignment()) + sizeof(ThreadSpecificData);
  1205. }
  1206. ErrorOr<void> Thread::make_thread_specific_region(Badge<Process>)
  1207. {
  1208. // The process may not require a TLS region, or allocate TLS later with sys$allocate_tls (which is what dynamically loaded programs do)
  1209. if (!process().m_master_tls_region)
  1210. return {};
  1211. return process().address_space().with([&](auto& space) -> ErrorOr<void> {
  1212. auto* region = TRY(space->allocate_region(Memory::RandomizeVirtualAddress::Yes, {}, thread_specific_region_size(), PAGE_SIZE, "Thread-specific"sv, PROT_READ | PROT_WRITE));
  1213. m_thread_specific_range = region->range();
  1214. SmapDisabler disabler;
  1215. auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment())).as_ptr();
  1216. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  1217. m_thread_specific_data = VirtualAddress(thread_specific_data);
  1218. thread_specific_data->self = thread_specific_data;
  1219. if (process().m_master_tls_size != 0)
  1220. memcpy(thread_local_storage, process().m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), process().m_master_tls_size);
  1221. return {};
  1222. });
  1223. }
  1224. RefPtr<Thread> Thread::from_tid(ThreadID tid)
  1225. {
  1226. return Thread::all_instances().with([&](auto& list) -> RefPtr<Thread> {
  1227. for (Thread& thread : list) {
  1228. if (thread.tid() == tid)
  1229. return thread;
  1230. }
  1231. return nullptr;
  1232. });
  1233. }
  1234. void Thread::reset_fpu_state()
  1235. {
  1236. memcpy(&m_fpu_state, &Processor::clean_fpu_state(), sizeof(FPUState));
  1237. }
  1238. bool Thread::should_be_stopped() const
  1239. {
  1240. return process().is_stopped();
  1241. }
  1242. void Thread::track_lock_acquire(LockRank rank)
  1243. {
  1244. // Nothing to do for locks without a rank.
  1245. if (rank == LockRank::None)
  1246. return;
  1247. if (m_lock_rank_mask != LockRank::None) {
  1248. // Verify we are only attempting to take a lock of a higher rank.
  1249. VERIFY(m_lock_rank_mask > rank);
  1250. }
  1251. m_lock_rank_mask |= rank;
  1252. }
  1253. void Thread::track_lock_release(LockRank rank)
  1254. {
  1255. // Nothing to do for locks without a rank.
  1256. if (rank == LockRank::None)
  1257. return;
  1258. // The rank value from the caller should only contain a single bit, otherwise
  1259. // we are disabling the tracking for multiple locks at once which will corrupt
  1260. // the lock tracking mask, and we will assert somewhere else.
  1261. auto rank_is_a_single_bit = [](auto rank_enum) -> bool {
  1262. auto rank = to_underlying(rank_enum);
  1263. auto rank_without_least_significant_bit = rank - 1;
  1264. return (rank & rank_without_least_significant_bit) == 0;
  1265. };
  1266. // We can't release locks out of order, as that would violate the ranking.
  1267. // This is validated by toggling the least significant bit of the mask, and
  1268. // then bit wise or-ing the rank we are trying to release with the resulting
  1269. // mask. If the rank we are releasing is truly the highest rank then the mask
  1270. // we get back will be equal to the current mask stored on the thread.
  1271. auto rank_is_in_order = [](auto mask_enum, auto rank_enum) -> bool {
  1272. auto mask = to_underlying(mask_enum);
  1273. auto rank = to_underlying(rank_enum);
  1274. auto mask_without_least_significant_bit = mask - 1;
  1275. return ((mask & mask_without_least_significant_bit) | rank) == mask;
  1276. };
  1277. VERIFY(has_flag(m_lock_rank_mask, rank));
  1278. VERIFY(rank_is_a_single_bit(rank));
  1279. VERIFY(rank_is_in_order(m_lock_rank_mask, rank));
  1280. m_lock_rank_mask ^= rank;
  1281. }
  1282. void Thread::set_name(NonnullOwnPtr<KString> name)
  1283. {
  1284. m_name.with([&](auto& this_name) {
  1285. this_name = move(name);
  1286. });
  1287. }
  1288. }
  1289. ErrorOr<void> AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, Kernel::Thread const& value)
  1290. {
  1291. return value.process().name().with([&](auto& process_name) {
  1292. return AK::Formatter<FormatString>::format(
  1293. builder,
  1294. "{}({}:{})"sv, process_name->view(), value.pid().value(), value.tid().value());
  1295. });
  1296. }