Thread.cpp 54 KB

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