Thread.cpp 54 KB

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