Thread.cpp 32 KB

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