Thread.cpp 54 KB

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