Thread.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/StringBuilder.h>
  28. #include <Kernel/Arch/i386/CPU.h>
  29. #include <Kernel/FileSystem/FileDescription.h>
  30. #include <Kernel/KSyms.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/Profiling.h>
  33. #include <Kernel/Scheduler.h>
  34. #include <Kernel/Thread.h>
  35. #include <Kernel/ThreadTracer.h>
  36. #include <Kernel/TimerQueue.h>
  37. #include <Kernel/VM/MemoryManager.h>
  38. #include <Kernel/VM/PageDirectory.h>
  39. #include <Kernel/VM/ProcessPagingScope.h>
  40. #include <LibC/signal_numbers.h>
  41. #include <LibELF/Loader.h>
  42. //#define SIGNAL_DEBUG
  43. //#define THREAD_DEBUG
  44. namespace Kernel {
  45. Thread::Thread(NonnullRefPtr<Process> process)
  46. : m_process(move(process))
  47. , m_name(m_process->name())
  48. {
  49. if (m_process->m_thread_count.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel) == 0) {
  50. // First thread gets TID == PID
  51. m_tid = m_process->pid().value();
  52. } else {
  53. m_tid = Process::allocate_pid().value();
  54. }
  55. #ifdef THREAD_DEBUG
  56. dbg() << "Created new thread " << m_process->name() << "(" << m_process->pid().value() << ":" << m_tid.value() << ")";
  57. #endif
  58. set_default_signal_dispositions();
  59. m_fpu_state = (FPUState*)kmalloc_aligned<16>(sizeof(FPUState));
  60. reset_fpu_state();
  61. memset(&m_tss, 0, sizeof(m_tss));
  62. m_tss.iomapbase = sizeof(TSS32);
  63. // Only IF is set when a process boots.
  64. m_tss.eflags = 0x0202;
  65. if (m_process->is_kernel_process()) {
  66. m_tss.cs = GDT_SELECTOR_CODE0;
  67. m_tss.ds = GDT_SELECTOR_DATA0;
  68. m_tss.es = GDT_SELECTOR_DATA0;
  69. m_tss.fs = GDT_SELECTOR_PROC;
  70. m_tss.ss = GDT_SELECTOR_DATA0;
  71. m_tss.gs = 0;
  72. } else {
  73. m_tss.cs = GDT_SELECTOR_CODE3 | 3;
  74. m_tss.ds = GDT_SELECTOR_DATA3 | 3;
  75. m_tss.es = GDT_SELECTOR_DATA3 | 3;
  76. m_tss.fs = GDT_SELECTOR_DATA3 | 3;
  77. m_tss.ss = GDT_SELECTOR_DATA3 | 3;
  78. m_tss.gs = GDT_SELECTOR_TLS | 3;
  79. }
  80. m_tss.cr3 = m_process->page_directory().cr3();
  81. m_kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, String::format("Kernel Stack (Thread %d)", m_tid.value()), Region::Access::Read | Region::Access::Write, false, true);
  82. m_kernel_stack_region->set_stack(true);
  83. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  84. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
  85. if (m_process->is_kernel_process()) {
  86. m_tss.esp = m_tss.esp0 = m_kernel_stack_top;
  87. } else {
  88. // Ring 3 processes get a separate stack for ring 0.
  89. // The ring 3 stack will be assigned by exec().
  90. m_tss.ss0 = GDT_SELECTOR_DATA0;
  91. m_tss.esp0 = m_kernel_stack_top;
  92. }
  93. // We need to add another reference if we could successfully create
  94. // all the resources needed for this thread. The reason for this is that
  95. // we don't want to delete this thread after dropping the reference,
  96. // it may still be running or scheduled to be run.
  97. // The finalizer is responsible for dropping this reference once this
  98. // thread is ready to be cleaned up.
  99. ref();
  100. if (m_process->pid() != 0)
  101. Scheduler::init_thread(*this);
  102. }
  103. Thread::~Thread()
  104. {
  105. ASSERT(!m_joiner);
  106. }
  107. void Thread::unblock()
  108. {
  109. ASSERT(m_lock.own_lock());
  110. m_blocker = nullptr;
  111. if (Thread::current() == this) {
  112. set_state(Thread::Running);
  113. return;
  114. }
  115. ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
  116. set_state(Thread::Runnable);
  117. }
  118. void Thread::set_should_die()
  119. {
  120. if (m_should_die) {
  121. #ifdef THREAD_DEBUG
  122. dbg() << *this << " Should already die";
  123. #endif
  124. return;
  125. }
  126. ScopedCritical critical;
  127. // Remember that we should die instead of returning to
  128. // the userspace.
  129. {
  130. ScopedSpinLock lock(g_scheduler_lock);
  131. m_should_die = true;
  132. // NOTE: Even the current thread can technically be in "Stopped"
  133. // state! This is the case when another thread sent a SIGSTOP to
  134. // it while it was running and it calls e.g. exit() before
  135. // the scheduler gets involved again.
  136. if (is_stopped()) {
  137. // If we were stopped, we need to briefly resume so that
  138. // the kernel stacks can clean up. We won't ever return back
  139. // to user mode, though
  140. resume_from_stopped();
  141. } else if (state() == Queued) {
  142. // m_queue can only be accessed safely if g_scheduler_lock is held!
  143. if (m_queue) {
  144. m_queue->dequeue(*this);
  145. m_queue = nullptr;
  146. // Wake the thread
  147. wake_from_queue();
  148. }
  149. }
  150. }
  151. if (is_blocked()) {
  152. ScopedSpinLock lock(m_lock);
  153. ASSERT(m_blocker != nullptr);
  154. // We're blocked in the kernel.
  155. m_blocker->set_interrupted_by_death();
  156. unblock();
  157. }
  158. }
  159. void Thread::die_if_needed()
  160. {
  161. ASSERT(Thread::current() == this);
  162. if (!m_should_die)
  163. return;
  164. unlock_process_if_locked();
  165. ScopedCritical critical;
  166. set_should_die();
  167. // Flag a context switch. Because we're in a critical section,
  168. // Scheduler::yield will actually only mark a pending scontext switch
  169. // Simply leaving the critical section would not necessarily trigger
  170. // a switch.
  171. Scheduler::yield();
  172. // Now leave the critical section so that we can also trigger the
  173. // actual context switch
  174. u32 prev_flags;
  175. Processor::current().clear_critical(prev_flags, false);
  176. dbg() << "die_if_needed returned form clear_critical!!! in irq: " << Processor::current().in_irq();
  177. // We should never get here, but the scoped scheduler lock
  178. // will be released by Scheduler::context_switch again
  179. ASSERT_NOT_REACHED();
  180. }
  181. void Thread::yield_without_holding_big_lock()
  182. {
  183. bool did_unlock = unlock_process_if_locked();
  184. // NOTE: Even though we call Scheduler::yield here, unless we happen
  185. // to be outside of a critical section, the yield will be postponed
  186. // until leaving it in relock_process.
  187. Scheduler::yield();
  188. relock_process(did_unlock);
  189. }
  190. bool Thread::unlock_process_if_locked()
  191. {
  192. return process().big_lock().force_unlock_if_locked();
  193. }
  194. void Thread::relock_process(bool did_unlock)
  195. {
  196. // Clearing the critical section may trigger the context switch
  197. // flagged by calling Scheduler::donate_to or Scheduler::yield
  198. // above. We have to do it this way because we intentionally
  199. // leave the critical section here to be able to switch contexts.
  200. u32 prev_flags;
  201. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  202. if (did_unlock) {
  203. // We've unblocked, relock the process if needed and carry on.
  204. process().big_lock().lock();
  205. }
  206. // NOTE: We may be on a different CPU now!
  207. Processor::current().restore_critical(prev_crit, prev_flags);
  208. }
  209. u64 Thread::sleep(u64 ticks)
  210. {
  211. ASSERT(state() == Thread::Running);
  212. u64 wakeup_time = g_uptime + ticks;
  213. auto ret = Thread::current()->block<Thread::SleepBlocker>(nullptr, wakeup_time);
  214. if (wakeup_time > g_uptime) {
  215. ASSERT(ret.was_interrupted());
  216. }
  217. return wakeup_time;
  218. }
  219. u64 Thread::sleep_until(u64 wakeup_time)
  220. {
  221. ASSERT(state() == Thread::Running);
  222. auto ret = Thread::current()->block<Thread::SleepBlocker>(nullptr, wakeup_time);
  223. if (wakeup_time > g_uptime)
  224. ASSERT(ret.was_interrupted());
  225. return wakeup_time;
  226. }
  227. const char* Thread::state_string() const
  228. {
  229. switch (state()) {
  230. case Thread::Invalid:
  231. return "Invalid";
  232. case Thread::Runnable:
  233. return "Runnable";
  234. case Thread::Running:
  235. return "Running";
  236. case Thread::Dying:
  237. return "Dying";
  238. case Thread::Dead:
  239. return "Dead";
  240. case Thread::Stopped:
  241. return "Stopped";
  242. case Thread::Queued:
  243. return "Queued";
  244. case Thread::Blocked: {
  245. ScopedSpinLock lock(m_lock);
  246. ASSERT(m_blocker != nullptr);
  247. return m_blocker->state_string();
  248. }
  249. }
  250. klog() << "Thread::state_string(): Invalid state: " << state();
  251. ASSERT_NOT_REACHED();
  252. return nullptr;
  253. }
  254. void Thread::finalize()
  255. {
  256. ASSERT(Thread::current() == g_finalizer);
  257. ASSERT(Thread::current() != this);
  258. #ifdef THREAD_DEBUG
  259. dbg() << "Finalizing thread " << *this;
  260. #endif
  261. set_state(Thread::State::Dead);
  262. if (auto* joiner = m_joiner.exchange(nullptr, AK::memory_order_acq_rel)) {
  263. // Notify joiner that we exited
  264. static_cast<JoinBlocker*>(joiner->m_blocker)->joinee_exited(m_exit_value);
  265. }
  266. if (m_dump_backtrace_on_finalization)
  267. dbg() << backtrace_impl();
  268. kfree_aligned(m_fpu_state);
  269. auto thread_cnt_before = m_process->m_thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
  270. ASSERT(thread_cnt_before != 0);
  271. if (thread_cnt_before == 1)
  272. process().finalize();
  273. }
  274. void Thread::finalize_dying_threads()
  275. {
  276. ASSERT(Thread::current() == g_finalizer);
  277. Vector<Thread*, 32> dying_threads;
  278. {
  279. ScopedSpinLock lock(g_scheduler_lock);
  280. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  281. if (thread.is_finalizable())
  282. dying_threads.append(&thread);
  283. return IterationDecision::Continue;
  284. });
  285. }
  286. for (auto* thread : dying_threads) {
  287. thread->finalize();
  288. // This thread will never execute again, drop the running reference
  289. // NOTE: This may not necessarily drop the last reference if anything
  290. // else is still holding onto this thread!
  291. thread->unref();
  292. }
  293. }
  294. bool Thread::tick()
  295. {
  296. ++m_ticks;
  297. if (tss().cs & 3)
  298. ++m_process->m_ticks_in_user;
  299. else
  300. ++m_process->m_ticks_in_kernel;
  301. return --m_ticks_left;
  302. }
  303. bool Thread::has_pending_signal(u8 signal) const
  304. {
  305. ScopedSpinLock lock(g_scheduler_lock);
  306. return m_pending_signals & (1 << (signal - 1));
  307. }
  308. u32 Thread::pending_signals() const
  309. {
  310. ScopedSpinLock lock(g_scheduler_lock);
  311. return m_pending_signals;
  312. }
  313. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  314. {
  315. ASSERT(signal < 32);
  316. ScopedSpinLock lock(g_scheduler_lock);
  317. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  318. if (should_ignore_signal(signal)) {
  319. #ifdef SIGNAL_DEBUG
  320. dbg() << "Signal " << signal << " was ignored by " << process();
  321. #endif
  322. return;
  323. }
  324. #ifdef SIGNAL_DEBUG
  325. if (sender)
  326. dbg() << "Signal: " << *sender << " sent " << signal << " to " << process();
  327. else
  328. dbg() << "Signal: Kernel sent " << signal << " to " << process();
  329. #endif
  330. m_pending_signals |= 1 << (signal - 1);
  331. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  332. }
  333. u32 Thread::update_signal_mask(u32 signal_mask)
  334. {
  335. ScopedSpinLock lock(g_scheduler_lock);
  336. auto previous_signal_mask = m_signal_mask;
  337. m_signal_mask = signal_mask;
  338. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  339. return previous_signal_mask;
  340. }
  341. u32 Thread::signal_mask() const
  342. {
  343. ScopedSpinLock lock(g_scheduler_lock);
  344. return m_signal_mask;
  345. }
  346. u32 Thread::signal_mask_block(sigset_t signal_set, bool block)
  347. {
  348. ScopedSpinLock lock(g_scheduler_lock);
  349. auto previous_signal_mask = m_signal_mask;
  350. if (block)
  351. m_signal_mask &= ~signal_set;
  352. else
  353. m_signal_mask |= signal_set;
  354. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  355. return previous_signal_mask;
  356. }
  357. void Thread::clear_signals()
  358. {
  359. ScopedSpinLock lock(g_scheduler_lock);
  360. m_signal_mask = 0;
  361. m_pending_signals = 0;
  362. m_have_any_unmasked_pending_signals.store(false, AK::memory_order_release);
  363. }
  364. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  365. // thread into a state where the signal handler must be
  366. // invoked immediately, otherwise it will continue to fault.
  367. // This function should be used in an exception handler to
  368. // ensure that when the thread resumes, it's executing in
  369. // the appropriate signal handler.
  370. void Thread::send_urgent_signal_to_self(u8 signal)
  371. {
  372. ASSERT(Thread::current() == this);
  373. ScopedSpinLock lock(g_scheduler_lock);
  374. if (dispatch_signal(signal) == ShouldUnblockThread::No)
  375. Scheduler::yield();
  376. }
  377. ShouldUnblockThread Thread::dispatch_one_pending_signal()
  378. {
  379. ASSERT(m_lock.own_lock());
  380. u32 signal_candidates = m_pending_signals & ~m_signal_mask;
  381. ASSERT(signal_candidates);
  382. u8 signal = 1;
  383. for (; signal < 32; ++signal) {
  384. if (signal_candidates & (1 << (signal - 1))) {
  385. break;
  386. }
  387. }
  388. return dispatch_signal(signal);
  389. }
  390. enum class DefaultSignalAction {
  391. Terminate,
  392. Ignore,
  393. DumpCore,
  394. Stop,
  395. Continue,
  396. };
  397. static DefaultSignalAction default_signal_action(u8 signal)
  398. {
  399. ASSERT(signal && signal < NSIG);
  400. switch (signal) {
  401. case SIGHUP:
  402. case SIGINT:
  403. case SIGKILL:
  404. case SIGPIPE:
  405. case SIGALRM:
  406. case SIGUSR1:
  407. case SIGUSR2:
  408. case SIGVTALRM:
  409. case SIGSTKFLT:
  410. case SIGIO:
  411. case SIGPROF:
  412. case SIGTERM:
  413. return DefaultSignalAction::Terminate;
  414. case SIGCHLD:
  415. case SIGURG:
  416. case SIGWINCH:
  417. case SIGINFO:
  418. return DefaultSignalAction::Ignore;
  419. case SIGQUIT:
  420. case SIGILL:
  421. case SIGTRAP:
  422. case SIGABRT:
  423. case SIGBUS:
  424. case SIGFPE:
  425. case SIGSEGV:
  426. case SIGXCPU:
  427. case SIGXFSZ:
  428. case SIGSYS:
  429. return DefaultSignalAction::DumpCore;
  430. case SIGCONT:
  431. return DefaultSignalAction::Continue;
  432. case SIGSTOP:
  433. case SIGTSTP:
  434. case SIGTTIN:
  435. case SIGTTOU:
  436. return DefaultSignalAction::Stop;
  437. }
  438. ASSERT_NOT_REACHED();
  439. }
  440. bool Thread::should_ignore_signal(u8 signal) const
  441. {
  442. ASSERT(signal < 32);
  443. auto& action = m_signal_action_data[signal];
  444. if (action.handler_or_sigaction.is_null())
  445. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  446. if (action.handler_or_sigaction.as_ptr() == SIG_IGN)
  447. return true;
  448. return false;
  449. }
  450. bool Thread::has_signal_handler(u8 signal) const
  451. {
  452. ASSERT(signal < 32);
  453. auto& action = m_signal_action_data[signal];
  454. return !action.handler_or_sigaction.is_null();
  455. }
  456. static bool push_value_on_user_stack(u32* stack, u32 data)
  457. {
  458. *stack -= 4;
  459. return copy_to_user((u32*)*stack, &data);
  460. }
  461. void Thread::resume_from_stopped()
  462. {
  463. ASSERT(is_stopped());
  464. ASSERT(m_stop_state != State::Invalid);
  465. set_state(m_stop_state);
  466. m_stop_state = State::Invalid;
  467. // make sure SemiPermanentBlocker is unblocked
  468. if (m_state != Thread::Runnable && m_state != Thread::Running) {
  469. ScopedSpinLock lock(m_lock);
  470. if (m_blocker && m_blocker->is_reason_signal())
  471. unblock();
  472. }
  473. }
  474. ShouldUnblockThread Thread::dispatch_signal(u8 signal)
  475. {
  476. ASSERT_INTERRUPTS_DISABLED();
  477. ASSERT(g_scheduler_lock.own_lock());
  478. ASSERT(signal > 0 && signal <= 32);
  479. ASSERT(process().is_user_process());
  480. #ifdef SIGNAL_DEBUG
  481. klog() << "signal: dispatch signal " << signal << " to " << *this;
  482. #endif
  483. if (m_state == Invalid || !is_initialized()) {
  484. // Thread has barely been created, we need to wait until it is
  485. // at least in Runnable state and is_initialized() returns true,
  486. // which indicates that it is fully set up an we actually have
  487. // a register state on the stack that we can modify
  488. return ShouldUnblockThread::No;
  489. }
  490. auto& action = m_signal_action_data[signal];
  491. // FIXME: Implement SA_SIGINFO signal handlers.
  492. ASSERT(!(action.flags & SA_SIGINFO));
  493. // Mark this signal as handled.
  494. m_pending_signals &= ~(1 << (signal - 1));
  495. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  496. if (signal == SIGSTOP) {
  497. if (!is_stopped()) {
  498. m_stop_signal = SIGSTOP;
  499. set_state(State::Stopped);
  500. }
  501. return ShouldUnblockThread::No;
  502. }
  503. if (signal == SIGCONT && is_stopped()) {
  504. resume_from_stopped();
  505. } else {
  506. auto* thread_tracer = tracer();
  507. if (thread_tracer != nullptr) {
  508. // when a thread is traced, it should be stopped whenever it receives a signal
  509. // the tracer is notified of this by using waitpid()
  510. // only "pending signals" from the tracer are sent to the tracee
  511. if (!thread_tracer->has_pending_signal(signal)) {
  512. m_stop_signal = signal;
  513. // make sure SemiPermanentBlocker is unblocked
  514. ScopedSpinLock lock(m_lock);
  515. if (m_blocker && m_blocker->is_reason_signal())
  516. unblock();
  517. set_state(Stopped);
  518. return ShouldUnblockThread::No;
  519. }
  520. thread_tracer->unset_signal(signal);
  521. }
  522. }
  523. auto handler_vaddr = action.handler_or_sigaction;
  524. if (handler_vaddr.is_null()) {
  525. switch (default_signal_action(signal)) {
  526. case DefaultSignalAction::Stop:
  527. m_stop_signal = signal;
  528. set_state(Stopped);
  529. return ShouldUnblockThread::No;
  530. case DefaultSignalAction::DumpCore:
  531. process().for_each_thread([](auto& thread) {
  532. thread.set_dump_backtrace_on_finalization();
  533. return IterationDecision::Continue;
  534. });
  535. [[fallthrough]];
  536. case DefaultSignalAction::Terminate:
  537. m_process->terminate_due_to_signal(signal);
  538. return ShouldUnblockThread::No;
  539. case DefaultSignalAction::Ignore:
  540. ASSERT_NOT_REACHED();
  541. case DefaultSignalAction::Continue:
  542. return ShouldUnblockThread::Yes;
  543. }
  544. ASSERT_NOT_REACHED();
  545. }
  546. if (handler_vaddr.as_ptr() == SIG_IGN) {
  547. #ifdef SIGNAL_DEBUG
  548. klog() << "signal: " << *this << " ignored signal " << signal;
  549. #endif
  550. return ShouldUnblockThread::Yes;
  551. }
  552. ProcessPagingScope paging_scope(m_process);
  553. u32 old_signal_mask = m_signal_mask;
  554. u32 new_signal_mask = action.mask;
  555. if (action.flags & SA_NODEFER)
  556. new_signal_mask &= ~(1 << (signal - 1));
  557. else
  558. new_signal_mask |= 1 << (signal - 1);
  559. m_signal_mask |= new_signal_mask;
  560. m_have_any_unmasked_pending_signals.store(m_pending_signals & ~m_signal_mask, AK::memory_order_release);
  561. auto setup_stack = [&](RegisterState& state) {
  562. u32* stack = &state.userspace_esp;
  563. u32 old_esp = *stack;
  564. u32 ret_eip = state.eip;
  565. u32 ret_eflags = state.eflags;
  566. #ifdef SIGNAL_DEBUG
  567. klog() << "signal: setting up user stack to return to eip: " << String::format("%p", ret_eip) << " esp: " << String::format("%p", old_esp);
  568. #endif
  569. // Align the stack to 16 bytes.
  570. // Note that we push 56 bytes (4 * 14) on to the stack,
  571. // so we need to account for this here.
  572. u32 stack_alignment = (*stack - 56) % 16;
  573. *stack -= stack_alignment;
  574. push_value_on_user_stack(stack, ret_eflags);
  575. push_value_on_user_stack(stack, ret_eip);
  576. push_value_on_user_stack(stack, state.eax);
  577. push_value_on_user_stack(stack, state.ecx);
  578. push_value_on_user_stack(stack, state.edx);
  579. push_value_on_user_stack(stack, state.ebx);
  580. push_value_on_user_stack(stack, old_esp);
  581. push_value_on_user_stack(stack, state.ebp);
  582. push_value_on_user_stack(stack, state.esi);
  583. push_value_on_user_stack(stack, state.edi);
  584. // PUSH old_signal_mask
  585. push_value_on_user_stack(stack, old_signal_mask);
  586. push_value_on_user_stack(stack, signal);
  587. push_value_on_user_stack(stack, handler_vaddr.get());
  588. push_value_on_user_stack(stack, 0); //push fake return address
  589. ASSERT((*stack % 16) == 0);
  590. };
  591. // We now place the thread state on the userspace stack.
  592. // Note that we use a RegisterState.
  593. // Conversely, when the thread isn't blocking the RegisterState may not be
  594. // valid (fork, exec etc) but the tss will, so we use that instead.
  595. auto& regs = get_register_dump_from_stack();
  596. setup_stack(regs);
  597. regs.eip = g_return_to_ring3_from_signal_trampoline.get();
  598. #ifdef SIGNAL_DEBUG
  599. klog() << "signal: Okay, " << *this << " {" << state_string() << "} has been primed with signal handler " << String::format("%w", m_tss.cs) << ":" << String::format("%x", m_tss.eip) << " to deliver " << signal;
  600. #endif
  601. return ShouldUnblockThread::Yes;
  602. }
  603. void Thread::set_default_signal_dispositions()
  604. {
  605. // FIXME: Set up all the right default actions. See signal(7).
  606. memset(&m_signal_action_data, 0, sizeof(m_signal_action_data));
  607. m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress(SIG_IGN);
  608. m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress(SIG_IGN);
  609. }
  610. bool Thread::push_value_on_stack(FlatPtr value)
  611. {
  612. m_tss.esp -= 4;
  613. FlatPtr* stack_ptr = (FlatPtr*)m_tss.esp;
  614. return copy_to_user(stack_ptr, &value);
  615. }
  616. RegisterState& Thread::get_register_dump_from_stack()
  617. {
  618. return *(RegisterState*)(kernel_stack_top() - sizeof(RegisterState));
  619. }
  620. KResultOr<u32> Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment, Vector<AuxiliaryValue> auxiliary_values)
  621. {
  622. auto* region = m_process->allocate_region(VirtualAddress(), default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, false);
  623. if (!region)
  624. return KResult(-ENOMEM);
  625. region->set_stack(true);
  626. FlatPtr new_esp = region->vaddr().offset(default_userspace_stack_size).get();
  627. auto push_on_new_stack = [&new_esp](u32 value) {
  628. new_esp -= 4;
  629. Userspace<u32*> stack_ptr = new_esp;
  630. return copy_to_user(stack_ptr, &value);
  631. };
  632. auto push_aux_value_on_new_stack = [&new_esp](auxv_t value) {
  633. new_esp -= sizeof(auxv_t);
  634. Userspace<auxv_t*> stack_ptr = new_esp;
  635. return copy_to_user(stack_ptr, &value);
  636. };
  637. auto push_string_on_new_stack = [&new_esp](const String& string) {
  638. new_esp -= round_up_to_power_of_two(string.length() + 1, 4);
  639. Userspace<u32*> stack_ptr = new_esp;
  640. return copy_to_user(stack_ptr, string.characters(), string.length() + 1);
  641. };
  642. Vector<FlatPtr> argv_entries;
  643. for (auto& argument : arguments) {
  644. push_string_on_new_stack(argument);
  645. argv_entries.append(new_esp);
  646. }
  647. Vector<FlatPtr> env_entries;
  648. for (auto& variable : environment) {
  649. push_string_on_new_stack(variable);
  650. env_entries.append(new_esp);
  651. }
  652. for (auto& value : auxiliary_values) {
  653. if (!value.optional_string.is_empty()) {
  654. push_string_on_new_stack(value.optional_string);
  655. value.auxv.a_un.a_ptr = (void*)new_esp;
  656. }
  657. }
  658. for (ssize_t i = auxiliary_values.size() - 1; i >= 0; --i) {
  659. auto& value = auxiliary_values[i];
  660. push_aux_value_on_new_stack(value.auxv);
  661. }
  662. push_on_new_stack(0);
  663. for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
  664. push_on_new_stack(env_entries[i]);
  665. FlatPtr envp = new_esp;
  666. push_on_new_stack(0);
  667. for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
  668. push_on_new_stack(argv_entries[i]);
  669. FlatPtr argv = new_esp;
  670. // NOTE: The stack needs to be 16-byte aligned.
  671. new_esp -= new_esp % 16;
  672. push_on_new_stack((FlatPtr)envp);
  673. push_on_new_stack((FlatPtr)argv);
  674. push_on_new_stack((FlatPtr)argv_entries.size());
  675. push_on_new_stack(0);
  676. return new_esp;
  677. }
  678. RefPtr<Thread> Thread::clone(Process& process)
  679. {
  680. auto clone = adopt(*new Thread(process));
  681. memcpy(clone->m_signal_action_data, m_signal_action_data, sizeof(m_signal_action_data));
  682. clone->m_signal_mask = m_signal_mask;
  683. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  684. clone->m_thread_specific_data = m_thread_specific_data;
  685. clone->m_thread_specific_region_size = m_thread_specific_region_size;
  686. return clone;
  687. }
  688. void Thread::set_state(State new_state)
  689. {
  690. ScopedSpinLock lock(g_scheduler_lock);
  691. if (new_state == m_state)
  692. return;
  693. if (new_state == Blocked) {
  694. // we should always have a Blocker while blocked
  695. ASSERT(m_blocker != nullptr);
  696. }
  697. auto previous_state = m_state;
  698. if (previous_state == Invalid) {
  699. // If we were *just* created, we may have already pending signals
  700. ScopedSpinLock thread_lock(m_lock);
  701. if (has_unmasked_pending_signals()) {
  702. dbg() << "Dispatch pending signals to new thread " << *this;
  703. dispatch_one_pending_signal();
  704. }
  705. }
  706. if (new_state == Stopped) {
  707. m_stop_state = m_state;
  708. }
  709. m_state = new_state;
  710. #ifdef THREAD_DEBUG
  711. dbg() << "Set Thread " << *this << " state to " << state_string();
  712. #endif
  713. if (m_process->pid() != 0) {
  714. update_state_for_thread(previous_state);
  715. ASSERT(g_scheduler_data->has_thread(*this));
  716. }
  717. if (m_state == Dying) {
  718. ASSERT(previous_state != Queued);
  719. if (this != Thread::current() && is_finalizable()) {
  720. // Some other thread set this thread to Dying, notify the
  721. // finalizer right away as it can be cleaned up now
  722. Scheduler::notify_finalizer();
  723. }
  724. }
  725. }
  726. void Thread::update_state_for_thread(Thread::State previous_state)
  727. {
  728. ASSERT_INTERRUPTS_DISABLED();
  729. ASSERT(g_scheduler_data);
  730. ASSERT(g_scheduler_lock.own_lock());
  731. auto& previous_list = g_scheduler_data->thread_list_for_state(previous_state);
  732. auto& list = g_scheduler_data->thread_list_for_state(state());
  733. if (&previous_list != &list) {
  734. previous_list.remove(*this);
  735. }
  736. if (list.contains(*this))
  737. return;
  738. list.append(*this);
  739. }
  740. String Thread::backtrace()
  741. {
  742. return backtrace_impl();
  743. }
  744. struct RecognizedSymbol {
  745. u32 address;
  746. const KernelSymbol* symbol { nullptr };
  747. };
  748. static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder, Process::ELFBundle* elf_bundle)
  749. {
  750. if (!symbol.address)
  751. return false;
  752. bool mask_kernel_addresses = !process.is_superuser();
  753. if (!symbol.symbol) {
  754. if (!is_user_address(VirtualAddress(symbol.address))) {
  755. builder.append("0xdeadc0de\n");
  756. } else {
  757. if (elf_bundle && elf_bundle->elf_loader->has_symbols())
  758. builder.appendf("%p %s\n", symbol.address, elf_bundle->elf_loader->symbolicate(symbol.address).characters());
  759. else
  760. builder.appendf("%p\n", symbol.address);
  761. }
  762. return true;
  763. }
  764. unsigned offset = symbol.address - symbol.symbol->address;
  765. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
  766. builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
  767. } else {
  768. builder.appendf("%p %s +%u\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address, demangle(symbol.symbol->name).characters(), offset);
  769. }
  770. return true;
  771. }
  772. String Thread::backtrace_impl()
  773. {
  774. Vector<RecognizedSymbol, 128> recognized_symbols;
  775. auto& process = const_cast<Process&>(this->process());
  776. OwnPtr<Process::ELFBundle> elf_bundle;
  777. if (!Processor::current().in_irq()) {
  778. // If we're handling IRQs we can't really safely symbolicate
  779. elf_bundle = process.elf_bundle();
  780. }
  781. ProcessPagingScope paging_scope(process);
  782. // To prevent a context switch involving this thread, which may happen
  783. // on another processor, we need to acquire the scheduler lock while
  784. // walking the stack
  785. {
  786. ScopedSpinLock lock(g_scheduler_lock);
  787. FlatPtr stack_ptr, eip;
  788. if (Processor::get_context_frame_ptr(*this, stack_ptr, eip)) {
  789. recognized_symbols.append({ eip, symbolicate_kernel_address(eip) });
  790. while (stack_ptr) {
  791. FlatPtr retaddr;
  792. if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
  793. if (!copy_from_user(&retaddr, &((FlatPtr*)stack_ptr)[1]))
  794. break;
  795. recognized_symbols.append({ retaddr, symbolicate_kernel_address(retaddr) });
  796. if (!copy_from_user(&stack_ptr, (FlatPtr*)stack_ptr))
  797. break;
  798. } else {
  799. void* fault_at;
  800. if (!safe_memcpy(&retaddr, &((FlatPtr*)stack_ptr)[1], sizeof(FlatPtr), fault_at))
  801. break;
  802. recognized_symbols.append({ retaddr, symbolicate_kernel_address(retaddr) });
  803. if (!safe_memcpy(&stack_ptr, (FlatPtr*)stack_ptr, sizeof(FlatPtr), fault_at))
  804. break;
  805. }
  806. }
  807. }
  808. }
  809. StringBuilder builder;
  810. for (auto& symbol : recognized_symbols) {
  811. if (!symbolicate(symbol, process, builder, elf_bundle.ptr()))
  812. break;
  813. }
  814. return builder.to_string();
  815. }
  816. Vector<FlatPtr> Thread::raw_backtrace(FlatPtr ebp, FlatPtr eip) const
  817. {
  818. InterruptDisabler disabler;
  819. auto& process = const_cast<Process&>(this->process());
  820. ProcessPagingScope paging_scope(process);
  821. Vector<FlatPtr, Profiling::max_stack_frame_count> backtrace;
  822. backtrace.append(eip);
  823. FlatPtr stack_ptr_copy;
  824. FlatPtr stack_ptr = (FlatPtr)ebp;
  825. while (stack_ptr) {
  826. void* fault_at;
  827. if (!safe_memcpy(&stack_ptr_copy, (void*)stack_ptr, sizeof(FlatPtr), fault_at))
  828. break;
  829. FlatPtr retaddr;
  830. if (!safe_memcpy(&retaddr, (void*)(stack_ptr + sizeof(FlatPtr)), sizeof(FlatPtr), fault_at))
  831. break;
  832. backtrace.append(retaddr);
  833. if (backtrace.size() == Profiling::max_stack_frame_count)
  834. break;
  835. stack_ptr = stack_ptr_copy;
  836. }
  837. return backtrace;
  838. }
  839. KResult Thread::make_thread_specific_region(Badge<Process>)
  840. {
  841. size_t thread_specific_region_alignment = max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  842. m_thread_specific_region_size = align_up_to(process().m_master_tls_size, thread_specific_region_alignment) + sizeof(ThreadSpecificData);
  843. auto* region = process().allocate_region({}, m_thread_specific_region_size, "Thread-specific", PROT_READ | PROT_WRITE, true);
  844. if (!region)
  845. return KResult(-ENOMEM);
  846. SmapDisabler disabler;
  847. auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment)).as_ptr();
  848. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  849. m_thread_specific_data = VirtualAddress(thread_specific_data);
  850. thread_specific_data->self = thread_specific_data;
  851. if (process().m_master_tls_size)
  852. memcpy(thread_local_storage, process().m_master_tls_region->vaddr().as_ptr(), process().m_master_tls_size);
  853. return KSuccess;
  854. }
  855. const LogStream& operator<<(const LogStream& stream, const Thread& value)
  856. {
  857. return stream << value.process().name() << "(" << value.pid().value() << ":" << value.tid().value() << ")";
  858. }
  859. Thread::BlockResult Thread::wait_on(WaitQueue& queue, const char* reason, timeval* timeout, Atomic<bool>* lock, RefPtr<Thread> beneficiary)
  860. {
  861. auto* current_thread = Thread::current();
  862. TimerId timer_id {};
  863. bool did_unlock;
  864. {
  865. ScopedCritical critical;
  866. // We need to be in a critical section *and* then also acquire the
  867. // scheduler lock. The only way acquiring the scheduler lock could
  868. // block us is if another core were to be holding it, in which case
  869. // we need to wait until the scheduler lock is released again
  870. {
  871. ScopedSpinLock sched_lock(g_scheduler_lock);
  872. // m_queue can only be accessed safely if g_scheduler_lock is held!
  873. m_queue = &queue;
  874. if (!queue.enqueue(*current_thread)) {
  875. // The WaitQueue was already requested to wake someone when
  876. // nobody was waiting. So return right away as we shouldn't
  877. // be waiting
  878. // The API contract guarantees we return with interrupts enabled,
  879. // regardless of how we got called
  880. critical.set_interrupt_flag_on_destruction(true);
  881. return BlockResult::NotBlocked;
  882. }
  883. did_unlock = unlock_process_if_locked();
  884. if (lock)
  885. *lock = false;
  886. set_state(State::Queued);
  887. m_wait_reason = reason;
  888. if (timeout) {
  889. timer_id = TimerQueue::the().add_timer(*timeout, [&]() {
  890. wake_from_queue();
  891. });
  892. }
  893. // Yield and wait for the queue to wake us up again.
  894. if (beneficiary)
  895. Scheduler::donate_to(beneficiary, reason);
  896. else
  897. Scheduler::yield();
  898. }
  899. // We've unblocked, relock the process if needed and carry on.
  900. relock_process(did_unlock);
  901. // This looks counter productive, but we may not actually leave
  902. // the critical section we just restored. It depends on whether
  903. // we were in one while being called.
  904. if (current_thread->should_die()) {
  905. // We're being unblocked so that we can clean up. We shouldn't
  906. // be in Dying state until we're about to return back to user mode
  907. ASSERT(current_thread->state() == Thread::Running);
  908. #ifdef THREAD_DEBUG
  909. dbg() << "Dying thread " << *current_thread << " was unblocked";
  910. #endif
  911. }
  912. }
  913. BlockResult result(BlockResult::WokeNormally);
  914. {
  915. // To be able to look at m_wait_queue_node we once again need the
  916. // scheduler lock, which is held when we insert into the queue
  917. ScopedSpinLock sched_lock(g_scheduler_lock);
  918. if (m_queue) {
  919. ASSERT(m_queue == &queue);
  920. // If our thread was still in the queue, we timed out
  921. m_queue = nullptr;
  922. if (queue.dequeue(*current_thread))
  923. result = BlockResult::InterruptedByTimeout;
  924. } else {
  925. // Our thread was already removed from the queue. The only
  926. // way this can happen if someone else is trying to kill us.
  927. // In this case, the queue should not contain us anymore.
  928. result = BlockResult::InterruptedByDeath;
  929. }
  930. // Make sure we cancel the timer if woke normally.
  931. if (timeout && !result.was_interrupted())
  932. TimerQueue::the().cancel_timer(timer_id);
  933. }
  934. // The API contract guarantees we return with interrupts enabled,
  935. // regardless of how we got called
  936. sti();
  937. return result;
  938. }
  939. void Thread::wake_from_queue()
  940. {
  941. ScopedSpinLock lock(g_scheduler_lock);
  942. ASSERT(state() == State::Queued);
  943. m_wait_reason = nullptr;
  944. if (this != Thread::current())
  945. set_state(State::Runnable);
  946. else
  947. set_state(State::Running);
  948. }
  949. RefPtr<Thread> Thread::from_tid(ThreadID tid)
  950. {
  951. RefPtr<Thread> found_thread;
  952. ScopedSpinLock lock(g_scheduler_lock);
  953. Thread::for_each([&](auto& thread) {
  954. if (thread.tid() == tid) {
  955. found_thread = &thread;
  956. return IterationDecision::Break;
  957. }
  958. return IterationDecision::Continue;
  959. });
  960. return found_thread;
  961. }
  962. void Thread::reset_fpu_state()
  963. {
  964. memcpy(m_fpu_state, &Processor::current().clean_fpu_state(), sizeof(FPUState));
  965. }
  966. void Thread::start_tracing_from(ProcessID tracer)
  967. {
  968. m_tracer = ThreadTracer::create(tracer);
  969. }
  970. void Thread::stop_tracing()
  971. {
  972. m_tracer = nullptr;
  973. }
  974. void Thread::tracer_trap(const RegisterState& regs)
  975. {
  976. ASSERT(m_tracer.ptr());
  977. m_tracer->set_regs(regs);
  978. send_urgent_signal_to_self(SIGTRAP);
  979. }
  980. const Thread::Blocker& Thread::blocker() const
  981. {
  982. ASSERT(m_lock.own_lock());
  983. ASSERT(m_blocker);
  984. return *m_blocker;
  985. }
  986. }