Thread.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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(sizeof(FPUState), 16);
  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_ring0()) {
  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_ring0()) {
  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. if (m_process->pid() != 0)
  94. Scheduler::init_thread(*this);
  95. }
  96. Thread::~Thread()
  97. {
  98. kfree_aligned(m_fpu_state);
  99. auto thread_cnt_before = m_process->m_thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
  100. ASSERT(thread_cnt_before != 0);
  101. }
  102. void Thread::unblock()
  103. {
  104. ASSERT(m_lock.own_lock());
  105. m_blocker = nullptr;
  106. if (Thread::current() == this) {
  107. set_state(Thread::Running);
  108. return;
  109. }
  110. ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
  111. set_state(Thread::Runnable);
  112. }
  113. void Thread::set_should_die()
  114. {
  115. if (m_should_die) {
  116. #ifdef THREAD_DEBUG
  117. dbg() << *this << " Should already die";
  118. #endif
  119. return;
  120. }
  121. ScopedCritical critical;
  122. // Remember that we should die instead of returning to
  123. // the userspace.
  124. {
  125. ScopedSpinLock lock(g_scheduler_lock);
  126. m_should_die = true;
  127. // NOTE: Even the current thread can technically be in "Stopped"
  128. // state! This is the case when another thread sent a SIGSTOP to
  129. // it while it was running and it calls e.g. exit() before
  130. // the scheduler gets involved again.
  131. if (is_stopped()) {
  132. // If we were stopped, we need to briefly resume so that
  133. // the kernel stacks can clean up. We won't ever return back
  134. // to user mode, though
  135. resume_from_stopped();
  136. }
  137. }
  138. if (is_blocked()) {
  139. ScopedSpinLock lock(m_lock);
  140. ASSERT(m_blocker != nullptr);
  141. // We're blocked in the kernel.
  142. m_blocker->set_interrupted_by_death();
  143. unblock();
  144. }
  145. }
  146. void Thread::die_if_needed()
  147. {
  148. ASSERT(Thread::current() == this);
  149. if (!m_should_die)
  150. return;
  151. unlock_process_if_locked();
  152. ScopedCritical critical;
  153. set_should_die();
  154. // Flag a context switch. Because we're in a critical section,
  155. // Scheduler::yield will actually only mark a pending scontext switch
  156. // Simply leaving the critical section would not necessarily trigger
  157. // a switch.
  158. Scheduler::yield();
  159. // Now leave the critical section so that we can also trigger the
  160. // actual context switch
  161. u32 prev_flags;
  162. Processor::current().clear_critical(prev_flags, false);
  163. dbg() << "die_if_needed returned form clear_critical!!! in irq: " << Processor::current().in_irq();
  164. // We should never get here, but the scoped scheduler lock
  165. // will be released by Scheduler::context_switch again
  166. ASSERT_NOT_REACHED();
  167. }
  168. void Thread::yield_without_holding_big_lock()
  169. {
  170. bool did_unlock = unlock_process_if_locked();
  171. Scheduler::yield();
  172. relock_process(did_unlock);
  173. }
  174. bool Thread::unlock_process_if_locked()
  175. {
  176. return process().big_lock().force_unlock_if_locked();
  177. }
  178. void Thread::relock_process(bool did_unlock)
  179. {
  180. if (did_unlock)
  181. process().big_lock().lock();
  182. }
  183. u64 Thread::sleep(u64 ticks)
  184. {
  185. ASSERT(state() == Thread::Running);
  186. u64 wakeup_time = g_uptime + ticks;
  187. auto ret = Thread::current()->block<Thread::SleepBlocker>(nullptr, wakeup_time);
  188. if (wakeup_time > g_uptime) {
  189. ASSERT(ret.was_interrupted());
  190. }
  191. return wakeup_time;
  192. }
  193. u64 Thread::sleep_until(u64 wakeup_time)
  194. {
  195. ASSERT(state() == Thread::Running);
  196. auto ret = Thread::current()->block<Thread::SleepBlocker>(nullptr, wakeup_time);
  197. if (wakeup_time > g_uptime)
  198. ASSERT(ret.was_interrupted());
  199. return wakeup_time;
  200. }
  201. const char* Thread::state_string() const
  202. {
  203. switch (state()) {
  204. case Thread::Invalid:
  205. return "Invalid";
  206. case Thread::Runnable:
  207. return "Runnable";
  208. case Thread::Running:
  209. return "Running";
  210. case Thread::Dying:
  211. return "Dying";
  212. case Thread::Dead:
  213. return "Dead";
  214. case Thread::Stopped:
  215. return "Stopped";
  216. case Thread::Skip1SchedulerPass:
  217. return "Skip1";
  218. case Thread::Skip0SchedulerPasses:
  219. return "Skip0";
  220. case Thread::Queued:
  221. return "Queued";
  222. case Thread::Blocked:
  223. ASSERT(m_blocker != nullptr);
  224. return m_blocker->state_string();
  225. }
  226. klog() << "Thread::state_string(): Invalid state: " << state();
  227. ASSERT_NOT_REACHED();
  228. return nullptr;
  229. }
  230. void Thread::finalize()
  231. {
  232. ASSERT(Thread::current() == g_finalizer);
  233. ASSERT(Thread::current() != this);
  234. #ifdef THREAD_DEBUG
  235. dbg() << "Finalizing thread " << *this;
  236. #endif
  237. set_state(Thread::State::Dead);
  238. if (m_joiner) {
  239. ScopedSpinLock lock(m_joiner->m_lock);
  240. ASSERT(m_joiner->m_joinee == this);
  241. static_cast<JoinBlocker*>(m_joiner->m_blocker)->set_joinee_exit_value(m_exit_value);
  242. static_cast<JoinBlocker*>(m_joiner->m_blocker)->set_interrupted_by_death();
  243. m_joiner->m_joinee = nullptr;
  244. // NOTE: We clear the joiner pointer here as well, to be tidy.
  245. m_joiner = nullptr;
  246. }
  247. if (m_dump_backtrace_on_finalization)
  248. dbg() << backtrace_impl();
  249. }
  250. void Thread::finalize_dying_threads()
  251. {
  252. ASSERT(Thread::current() == g_finalizer);
  253. Vector<Thread*, 32> dying_threads;
  254. {
  255. ScopedSpinLock lock(g_scheduler_lock);
  256. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  257. if (thread.is_finalizable())
  258. dying_threads.append(&thread);
  259. return IterationDecision::Continue;
  260. });
  261. }
  262. for (auto* thread : dying_threads) {
  263. auto& process = thread->process();
  264. thread->finalize();
  265. delete thread;
  266. if (process.m_thread_count.load(AK::MemoryOrder::memory_order_consume) == 0)
  267. process.finalize();
  268. }
  269. }
  270. bool Thread::tick()
  271. {
  272. ++m_ticks;
  273. if (tss().cs & 3)
  274. ++m_process->m_ticks_in_user;
  275. else
  276. ++m_process->m_ticks_in_kernel;
  277. return --m_ticks_left;
  278. }
  279. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  280. {
  281. ASSERT(signal < 32);
  282. InterruptDisabler disabler;
  283. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  284. if (should_ignore_signal(signal)) {
  285. #ifdef SIGNAL_DEBUG
  286. dbg() << "Signal " << signal << " was ignored by " << process();
  287. #endif
  288. return;
  289. }
  290. #ifdef SIGNAL_DEBUG
  291. if (sender)
  292. dbg() << "Signal: " << *sender << " sent " << signal << " to " << process();
  293. else
  294. dbg() << "Signal: Kernel sent " << signal << " to " << process();
  295. #endif
  296. ScopedSpinLock lock(g_scheduler_lock);
  297. m_pending_signals |= 1 << (signal - 1);
  298. }
  299. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  300. // thread into a state where the signal handler must be
  301. // invoked immediately, otherwise it will continue to fault.
  302. // This function should be used in an exception handler to
  303. // ensure that when the thread resumes, it's executing in
  304. // the appropriate signal handler.
  305. void Thread::send_urgent_signal_to_self(u8 signal)
  306. {
  307. ASSERT(Thread::current() == this);
  308. ScopedSpinLock lock(g_scheduler_lock);
  309. if (dispatch_signal(signal) == ShouldUnblockThread::No)
  310. Scheduler::yield();
  311. }
  312. ShouldUnblockThread Thread::dispatch_one_pending_signal()
  313. {
  314. ASSERT_INTERRUPTS_DISABLED();
  315. u32 signal_candidates = m_pending_signals & ~m_signal_mask;
  316. ASSERT(signal_candidates);
  317. u8 signal = 1;
  318. for (; signal < 32; ++signal) {
  319. if (signal_candidates & (1 << (signal - 1))) {
  320. break;
  321. }
  322. }
  323. return dispatch_signal(signal);
  324. }
  325. enum class DefaultSignalAction {
  326. Terminate,
  327. Ignore,
  328. DumpCore,
  329. Stop,
  330. Continue,
  331. };
  332. static DefaultSignalAction default_signal_action(u8 signal)
  333. {
  334. ASSERT(signal && signal < NSIG);
  335. switch (signal) {
  336. case SIGHUP:
  337. case SIGINT:
  338. case SIGKILL:
  339. case SIGPIPE:
  340. case SIGALRM:
  341. case SIGUSR1:
  342. case SIGUSR2:
  343. case SIGVTALRM:
  344. case SIGSTKFLT:
  345. case SIGIO:
  346. case SIGPROF:
  347. case SIGTERM:
  348. case SIGPWR:
  349. return DefaultSignalAction::Terminate;
  350. case SIGCHLD:
  351. case SIGURG:
  352. case SIGWINCH:
  353. return DefaultSignalAction::Ignore;
  354. case SIGQUIT:
  355. case SIGILL:
  356. case SIGTRAP:
  357. case SIGABRT:
  358. case SIGBUS:
  359. case SIGFPE:
  360. case SIGSEGV:
  361. case SIGXCPU:
  362. case SIGXFSZ:
  363. case SIGSYS:
  364. return DefaultSignalAction::DumpCore;
  365. case SIGCONT:
  366. return DefaultSignalAction::Continue;
  367. case SIGSTOP:
  368. case SIGTSTP:
  369. case SIGTTIN:
  370. case SIGTTOU:
  371. return DefaultSignalAction::Stop;
  372. }
  373. ASSERT_NOT_REACHED();
  374. }
  375. bool Thread::should_ignore_signal(u8 signal) const
  376. {
  377. ASSERT(signal < 32);
  378. auto& action = m_signal_action_data[signal];
  379. if (action.handler_or_sigaction.is_null())
  380. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  381. if (action.handler_or_sigaction.as_ptr() == SIG_IGN)
  382. return true;
  383. return false;
  384. }
  385. bool Thread::has_signal_handler(u8 signal) const
  386. {
  387. ASSERT(signal < 32);
  388. auto& action = m_signal_action_data[signal];
  389. return !action.handler_or_sigaction.is_null();
  390. }
  391. static void push_value_on_user_stack(u32* stack, u32 data)
  392. {
  393. *stack -= 4;
  394. copy_to_user((u32*)*stack, &data);
  395. }
  396. void Thread::resume_from_stopped()
  397. {
  398. ASSERT(is_stopped());
  399. ASSERT(m_stop_state != State::Invalid);
  400. set_state(m_stop_state);
  401. m_stop_state = State::Invalid;
  402. // make sure SemiPermanentBlocker is unblocked
  403. if (m_state != Thread::Runnable && m_state != Thread::Running) {
  404. ScopedSpinLock lock(m_lock);
  405. if (m_blocker && m_blocker->is_reason_signal())
  406. unblock();
  407. }
  408. }
  409. ShouldUnblockThread Thread::dispatch_signal(u8 signal)
  410. {
  411. ASSERT_INTERRUPTS_DISABLED();
  412. ASSERT(g_scheduler_lock.is_locked());
  413. ASSERT(signal > 0 && signal <= 32);
  414. ASSERT(!process().is_ring0());
  415. #ifdef SIGNAL_DEBUG
  416. klog() << "signal: dispatch signal " << signal << " to " << *this;
  417. #endif
  418. auto& action = m_signal_action_data[signal];
  419. // FIXME: Implement SA_SIGINFO signal handlers.
  420. ASSERT(!(action.flags & SA_SIGINFO));
  421. // Mark this signal as handled.
  422. m_pending_signals &= ~(1 << (signal - 1));
  423. if (signal == SIGSTOP) {
  424. if (!is_stopped()) {
  425. m_stop_signal = SIGSTOP;
  426. set_state(State::Stopped);
  427. }
  428. return ShouldUnblockThread::No;
  429. }
  430. if (signal == SIGCONT && is_stopped()) {
  431. resume_from_stopped();
  432. } else {
  433. auto* thread_tracer = tracer();
  434. if (thread_tracer != nullptr) {
  435. // when a thread is traced, it should be stopped whenever it receives a signal
  436. // the tracer is notified of this by using waitpid()
  437. // only "pending signals" from the tracer are sent to the tracee
  438. if (!thread_tracer->has_pending_signal(signal)) {
  439. m_stop_signal = signal;
  440. // make sure SemiPermanentBlocker is unblocked
  441. ScopedSpinLock lock(m_lock);
  442. if (m_blocker && m_blocker->is_reason_signal())
  443. unblock();
  444. set_state(Stopped);
  445. return ShouldUnblockThread::No;
  446. }
  447. thread_tracer->unset_signal(signal);
  448. }
  449. }
  450. auto handler_vaddr = action.handler_or_sigaction;
  451. if (handler_vaddr.is_null()) {
  452. switch (default_signal_action(signal)) {
  453. case DefaultSignalAction::Stop:
  454. m_stop_signal = signal;
  455. set_state(Stopped);
  456. return ShouldUnblockThread::No;
  457. case DefaultSignalAction::DumpCore:
  458. process().for_each_thread([](auto& thread) {
  459. thread.set_dump_backtrace_on_finalization();
  460. return IterationDecision::Continue;
  461. });
  462. [[fallthrough]];
  463. case DefaultSignalAction::Terminate:
  464. m_process->terminate_due_to_signal(signal);
  465. return ShouldUnblockThread::No;
  466. case DefaultSignalAction::Ignore:
  467. ASSERT_NOT_REACHED();
  468. case DefaultSignalAction::Continue:
  469. return ShouldUnblockThread::Yes;
  470. }
  471. ASSERT_NOT_REACHED();
  472. }
  473. if (handler_vaddr.as_ptr() == SIG_IGN) {
  474. #ifdef SIGNAL_DEBUG
  475. klog() << "signal: " << *this << " ignored signal " << signal;
  476. #endif
  477. return ShouldUnblockThread::Yes;
  478. }
  479. ProcessPagingScope paging_scope(m_process);
  480. u32 old_signal_mask = m_signal_mask;
  481. u32 new_signal_mask = action.mask;
  482. if (action.flags & SA_NODEFER)
  483. new_signal_mask &= ~(1 << (signal - 1));
  484. else
  485. new_signal_mask |= 1 << (signal - 1);
  486. m_signal_mask |= new_signal_mask;
  487. auto setup_stack = [&]<typename ThreadState>(ThreadState state, u32* stack) {
  488. u32 old_esp = *stack;
  489. u32 ret_eip = state.eip;
  490. u32 ret_eflags = state.eflags;
  491. #ifdef SIGNAL_DEBUG
  492. klog() << "signal: setting up user stack to return to eip: " << String::format("%p", ret_eip) << " esp: " << String::format("%p", old_esp);
  493. #endif
  494. // Align the stack to 16 bytes.
  495. // Note that we push 56 bytes (4 * 14) on to the stack,
  496. // so we need to account for this here.
  497. u32 stack_alignment = (*stack - 56) % 16;
  498. *stack -= stack_alignment;
  499. push_value_on_user_stack(stack, ret_eflags);
  500. push_value_on_user_stack(stack, ret_eip);
  501. push_value_on_user_stack(stack, state.eax);
  502. push_value_on_user_stack(stack, state.ecx);
  503. push_value_on_user_stack(stack, state.edx);
  504. push_value_on_user_stack(stack, state.ebx);
  505. push_value_on_user_stack(stack, old_esp);
  506. push_value_on_user_stack(stack, state.ebp);
  507. push_value_on_user_stack(stack, state.esi);
  508. push_value_on_user_stack(stack, state.edi);
  509. // PUSH old_signal_mask
  510. push_value_on_user_stack(stack, old_signal_mask);
  511. push_value_on_user_stack(stack, signal);
  512. push_value_on_user_stack(stack, handler_vaddr.get());
  513. push_value_on_user_stack(stack, 0); //push fake return address
  514. ASSERT((*stack % 16) == 0);
  515. };
  516. // We now place the thread state on the userspace stack.
  517. // Note that we use a RegisterState.
  518. // Conversely, when the thread isn't blocking the RegisterState may not be
  519. // valid (fork, exec etc) but the tss will, so we use that instead.
  520. auto& regs = get_register_dump_from_stack();
  521. u32* stack = &regs.userspace_esp;
  522. setup_stack(regs, stack);
  523. regs.eip = g_return_to_ring3_from_signal_trampoline.get();
  524. #ifdef SIGNAL_DEBUG
  525. 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;
  526. #endif
  527. return ShouldUnblockThread::Yes;
  528. }
  529. void Thread::set_default_signal_dispositions()
  530. {
  531. // FIXME: Set up all the right default actions. See signal(7).
  532. memset(&m_signal_action_data, 0, sizeof(m_signal_action_data));
  533. m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress(SIG_IGN);
  534. m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress(SIG_IGN);
  535. }
  536. void Thread::push_value_on_stack(FlatPtr value)
  537. {
  538. m_tss.esp -= 4;
  539. FlatPtr* stack_ptr = (FlatPtr*)m_tss.esp;
  540. copy_to_user(stack_ptr, &value);
  541. }
  542. RegisterState& Thread::get_register_dump_from_stack()
  543. {
  544. return *(RegisterState*)(kernel_stack_top() - sizeof(RegisterState));
  545. }
  546. u32 Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment, Vector<AuxiliaryValue> auxiliary_values)
  547. {
  548. auto* region = m_process->allocate_region(VirtualAddress(), default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, false);
  549. ASSERT(region);
  550. region->set_stack(true);
  551. FlatPtr new_esp = region->vaddr().offset(default_userspace_stack_size).get();
  552. auto push_on_new_stack = [&new_esp](u32 value) {
  553. new_esp -= 4;
  554. Userspace<u32*> stack_ptr = new_esp;
  555. copy_to_user(stack_ptr, &value);
  556. };
  557. auto push_aux_value_on_new_stack = [&new_esp](auxv_t value) {
  558. new_esp -= sizeof(auxv_t);
  559. Userspace<auxv_t*> stack_ptr = new_esp;
  560. copy_to_user(stack_ptr, &value);
  561. };
  562. auto push_string_on_new_stack = [&new_esp](const String& string) {
  563. new_esp -= round_up_to_power_of_two(string.length() + 1, 4);
  564. Userspace<u32*> stack_ptr = new_esp;
  565. copy_to_user(stack_ptr, string.characters(), string.length() + 1);
  566. };
  567. Vector<FlatPtr> argv_entries;
  568. for (auto& argument : arguments) {
  569. push_string_on_new_stack(argument);
  570. argv_entries.append(new_esp);
  571. }
  572. Vector<FlatPtr> env_entries;
  573. for (auto& variable : environment) {
  574. push_string_on_new_stack(variable);
  575. env_entries.append(new_esp);
  576. }
  577. for (auto& value : auxiliary_values) {
  578. if (!value.optional_string.is_empty()) {
  579. push_string_on_new_stack(value.optional_string);
  580. value.auxv.a_un.a_ptr = (void*)new_esp;
  581. }
  582. }
  583. for (ssize_t i = auxiliary_values.size() - 1; i >= 0; --i) {
  584. auto& value = auxiliary_values[i];
  585. push_aux_value_on_new_stack(value.auxv);
  586. }
  587. push_on_new_stack(0);
  588. for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
  589. push_on_new_stack(env_entries[i]);
  590. FlatPtr envp = new_esp;
  591. push_on_new_stack(0);
  592. for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
  593. push_on_new_stack(argv_entries[i]);
  594. FlatPtr argv = new_esp;
  595. // NOTE: The stack needs to be 16-byte aligned.
  596. new_esp -= new_esp % 16;
  597. push_on_new_stack((FlatPtr)envp);
  598. push_on_new_stack((FlatPtr)argv);
  599. push_on_new_stack((FlatPtr)argv_entries.size());
  600. push_on_new_stack(0);
  601. return new_esp;
  602. }
  603. Thread* Thread::clone(Process& process)
  604. {
  605. auto* clone = new Thread(process);
  606. memcpy(clone->m_signal_action_data, m_signal_action_data, sizeof(m_signal_action_data));
  607. clone->m_signal_mask = m_signal_mask;
  608. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  609. clone->m_thread_specific_data = m_thread_specific_data;
  610. clone->m_thread_specific_region_size = m_thread_specific_region_size;
  611. return clone;
  612. }
  613. void Thread::set_state(State new_state)
  614. {
  615. ScopedSpinLock lock(g_scheduler_lock);
  616. if (new_state == m_state)
  617. return;
  618. if (new_state == Blocked) {
  619. // we should always have a Blocker while blocked
  620. ASSERT(m_blocker != nullptr);
  621. }
  622. if (new_state == Stopped) {
  623. m_stop_state = m_state;
  624. }
  625. auto previous_state = m_state;
  626. m_state = new_state;
  627. #ifdef THREAD_DEBUG
  628. dbg() << "Set Thread " << *this << " state to " << state_string();
  629. #endif
  630. if (m_process->pid() != 0) {
  631. update_state_for_thread(previous_state);
  632. ASSERT(g_scheduler_data->has_thread(*this));
  633. }
  634. if (m_state == Dying) {
  635. ASSERT(previous_state != Queued);
  636. if (this != Thread::current() && is_finalizable()) {
  637. // Some other thread set this thread to Dying, notify the
  638. // finalizer right away as it can be cleaned up now
  639. Scheduler::notify_finalizer();
  640. }
  641. }
  642. }
  643. void Thread::update_state_for_thread(Thread::State previous_state)
  644. {
  645. ASSERT_INTERRUPTS_DISABLED();
  646. ASSERT(g_scheduler_data);
  647. ASSERT(g_scheduler_lock.own_lock());
  648. auto& previous_list = g_scheduler_data->thread_list_for_state(previous_state);
  649. auto& list = g_scheduler_data->thread_list_for_state(state());
  650. if (&previous_list != &list) {
  651. previous_list.remove(*this);
  652. }
  653. if (list.contains(*this))
  654. return;
  655. list.append(*this);
  656. }
  657. String Thread::backtrace()
  658. {
  659. return backtrace_impl();
  660. }
  661. struct RecognizedSymbol {
  662. u32 address;
  663. const KernelSymbol* symbol { nullptr };
  664. };
  665. static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder, Process::ELFBundle* elf_bundle)
  666. {
  667. if (!symbol.address)
  668. return false;
  669. bool mask_kernel_addresses = !process.is_superuser();
  670. if (!symbol.symbol) {
  671. if (!is_user_address(VirtualAddress(symbol.address))) {
  672. builder.append("0xdeadc0de\n");
  673. } else {
  674. if (elf_bundle && elf_bundle->elf_loader->has_symbols())
  675. builder.appendf("%p %s\n", symbol.address, elf_bundle->elf_loader->symbolicate(symbol.address).characters());
  676. else
  677. builder.appendf("%p\n", symbol.address);
  678. }
  679. return true;
  680. }
  681. unsigned offset = symbol.address - symbol.symbol->address;
  682. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
  683. builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
  684. } else {
  685. builder.appendf("%p %s +%u\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address, demangle(symbol.symbol->name).characters(), offset);
  686. }
  687. return true;
  688. }
  689. String Thread::backtrace_impl()
  690. {
  691. Vector<RecognizedSymbol, 128> recognized_symbols;
  692. auto& process = const_cast<Process&>(this->process());
  693. auto elf_bundle = process.elf_bundle();
  694. ProcessPagingScope paging_scope(process);
  695. // To prevent a context switch involving this thread, which may happen
  696. // on another processor, we need to acquire the scheduler lock while
  697. // walking the stack
  698. {
  699. ScopedSpinLock lock(g_scheduler_lock);
  700. FlatPtr stack_ptr, eip;
  701. if (Processor::get_context_frame_ptr(*this, stack_ptr, eip)) {
  702. recognized_symbols.append({ eip, symbolicate_kernel_address(eip) });
  703. for (;;) {
  704. if (!process.validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2))
  705. break;
  706. FlatPtr retaddr;
  707. if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
  708. copy_from_user(&retaddr, &((FlatPtr*)stack_ptr)[1]);
  709. recognized_symbols.append({ retaddr, symbolicate_kernel_address(retaddr) });
  710. copy_from_user(&stack_ptr, (FlatPtr*)stack_ptr);
  711. } else {
  712. memcpy(&retaddr, &((FlatPtr*)stack_ptr)[1], sizeof(FlatPtr));
  713. recognized_symbols.append({ retaddr, symbolicate_kernel_address(retaddr) });
  714. memcpy(&stack_ptr, (FlatPtr*)stack_ptr, sizeof(FlatPtr));
  715. }
  716. }
  717. }
  718. }
  719. StringBuilder builder;
  720. for (auto& symbol : recognized_symbols) {
  721. if (!symbolicate(symbol, process, builder, elf_bundle.ptr()))
  722. break;
  723. }
  724. return builder.to_string();
  725. }
  726. Vector<FlatPtr> Thread::raw_backtrace(FlatPtr ebp, FlatPtr eip) const
  727. {
  728. InterruptDisabler disabler;
  729. auto& process = const_cast<Process&>(this->process());
  730. ProcessPagingScope paging_scope(process);
  731. Vector<FlatPtr, Profiling::max_stack_frame_count> backtrace;
  732. backtrace.append(eip);
  733. for (FlatPtr* stack_ptr = (FlatPtr*)ebp; process.validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2) && MM.can_read_without_faulting(process, VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2); stack_ptr = (FlatPtr*)*stack_ptr) {
  734. FlatPtr retaddr = stack_ptr[1];
  735. backtrace.append(retaddr);
  736. if (backtrace.size() == Profiling::max_stack_frame_count)
  737. break;
  738. }
  739. return backtrace;
  740. }
  741. void Thread::make_thread_specific_region(Badge<Process>)
  742. {
  743. size_t thread_specific_region_alignment = max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  744. m_thread_specific_region_size = align_up_to(process().m_master_tls_size, thread_specific_region_alignment) + sizeof(ThreadSpecificData);
  745. auto* region = process().allocate_region({}, m_thread_specific_region_size, "Thread-specific", PROT_READ | PROT_WRITE, true);
  746. SmapDisabler disabler;
  747. auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment)).as_ptr();
  748. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  749. m_thread_specific_data = VirtualAddress(thread_specific_data);
  750. thread_specific_data->self = thread_specific_data;
  751. if (process().m_master_tls_size)
  752. memcpy(thread_local_storage, process().m_master_tls_region->vaddr().as_ptr(), process().m_master_tls_size);
  753. }
  754. const LogStream& operator<<(const LogStream& stream, const Thread& value)
  755. {
  756. return stream << value.process().name() << "(" << value.pid().value() << ":" << value.tid().value() << ")";
  757. }
  758. Thread::BlockResult Thread::wait_on(WaitQueue& queue, const char* reason, timeval* timeout, Atomic<bool>* lock, Thread* beneficiary)
  759. {
  760. auto* current_thread = Thread::current();
  761. TimerId timer_id {};
  762. bool did_unlock;
  763. {
  764. ScopedCritical critical;
  765. // We need to be in a critical section *and* then also acquire the
  766. // scheduler lock. The only way acquiring the scheduler lock could
  767. // block us is if another core were to be holding it, in which case
  768. // we need to wait until the scheduler lock is released again
  769. {
  770. ScopedSpinLock sched_lock(g_scheduler_lock);
  771. if (!queue.enqueue(*current_thread)) {
  772. // The WaitQueue was already requested to wake someone when
  773. // nobody was waiting. So return right away as we shouldn't
  774. // be waiting
  775. // The API contract guarantees we return with interrupts enabled,
  776. // regardless of how we got called
  777. critical.set_interrupt_flag_on_destruction(true);
  778. return BlockResult::NotBlocked;
  779. }
  780. did_unlock = unlock_process_if_locked();
  781. if (lock)
  782. *lock = false;
  783. set_state(State::Queued);
  784. m_wait_reason = reason;
  785. if (timeout) {
  786. timer_id = TimerQueue::the().add_timer(*timeout, [&]() {
  787. wake_from_queue();
  788. });
  789. }
  790. // Yield and wait for the queue to wake us up again.
  791. if (beneficiary)
  792. Scheduler::donate_to(beneficiary, reason);
  793. else
  794. Scheduler::yield();
  795. }
  796. // Clearing the critical section may trigger the context switch
  797. // flagged by calling Scheduler::donate_to or Scheduler::yield
  798. // above. We have to do it this way because we intentionally
  799. // leave the critical section here to be able to switch contexts.
  800. u32 prev_flags;
  801. u32 prev_crit = Processor::current().clear_critical(prev_flags, true);
  802. // We've unblocked, relock the process if needed and carry on.
  803. relock_process(did_unlock);
  804. // NOTE: We may be on a differenct CPU now!
  805. Processor::current().restore_critical(prev_crit, prev_flags);
  806. // This looks counter productive, but we may not actually leave
  807. // the critical section we just restored. It depends on whether
  808. // we were in one while being called.
  809. if (current_thread->should_die()) {
  810. // We're being unblocked so that we can clean up. We shouldn't
  811. // be in Dying state until we're about to return back to user mode
  812. ASSERT(current_thread->state() == Thread::Running);
  813. #ifdef THREAD_DEBUG
  814. dbg() << "Dying thread " << *current_thread << " was unblocked";
  815. #endif
  816. }
  817. }
  818. BlockResult result(BlockResult::WokeNormally);
  819. {
  820. // To be able to look at m_wait_queue_node we once again need the
  821. // scheduler lock, which is held when we insert into the queue
  822. ScopedSpinLock sched_lock(g_scheduler_lock);
  823. // If our thread was still in the queue, we timed out
  824. if (queue.dequeue(*current_thread))
  825. result = BlockResult::InterruptedByTimeout;
  826. // Make sure we cancel the timer if woke normally.
  827. if (timeout && !result.was_interrupted())
  828. TimerQueue::the().cancel_timer(timer_id);
  829. }
  830. // The API contract guarantees we return with interrupts enabled,
  831. // regardless of how we got called
  832. sti();
  833. return result;
  834. }
  835. void Thread::wake_from_queue()
  836. {
  837. ScopedSpinLock lock(g_scheduler_lock);
  838. ASSERT(state() == State::Queued);
  839. m_wait_reason = nullptr;
  840. if (this != Thread::current())
  841. set_state(State::Runnable);
  842. else
  843. set_state(State::Running);
  844. }
  845. Thread* Thread::from_tid(ThreadID tid)
  846. {
  847. InterruptDisabler disabler;
  848. Thread* found_thread = nullptr;
  849. Thread::for_each([&](auto& thread) {
  850. if (thread.tid() == tid) {
  851. found_thread = &thread;
  852. return IterationDecision::Break;
  853. }
  854. return IterationDecision::Continue;
  855. });
  856. return found_thread;
  857. }
  858. void Thread::reset_fpu_state()
  859. {
  860. memcpy(m_fpu_state, &Processor::current().clean_fpu_state(), sizeof(FPUState));
  861. }
  862. void Thread::start_tracing_from(ProcessID tracer)
  863. {
  864. m_tracer = ThreadTracer::create(tracer);
  865. }
  866. void Thread::stop_tracing()
  867. {
  868. m_tracer = nullptr;
  869. }
  870. void Thread::tracer_trap(const RegisterState& regs)
  871. {
  872. ASSERT(m_tracer.ptr());
  873. m_tracer->set_regs(regs);
  874. send_urgent_signal_to_self(SIGTRAP);
  875. }
  876. const Thread::Blocker& Thread::blocker() const
  877. {
  878. ASSERT(m_blocker);
  879. return *m_blocker;
  880. }
  881. }