Thread.cpp 33 KB

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