Thread.cpp 52 KB

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