Thread.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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/VM/MemoryManager.h>
  36. #include <Kernel/VM/PageDirectory.h>
  37. #include <LibC/signal_numbers.h>
  38. #include <LibELF/ELFLoader.h>
  39. //#define SIGNAL_DEBUG
  40. //#define THREAD_DEBUG
  41. namespace Kernel {
  42. Thread* Thread::current;
  43. static FPUState s_clean_fpu_state;
  44. u16 thread_specific_selector()
  45. {
  46. static u16 selector;
  47. if (!selector) {
  48. selector = gdt_alloc_entry();
  49. auto& descriptor = get_gdt_entry(selector);
  50. descriptor.dpl = 3;
  51. descriptor.segment_present = 1;
  52. descriptor.granularity = 0;
  53. descriptor.zero = 0;
  54. descriptor.operation_size = 1;
  55. descriptor.descriptor_type = 1;
  56. descriptor.type = 2;
  57. }
  58. return selector;
  59. }
  60. Descriptor& thread_specific_descriptor()
  61. {
  62. return get_gdt_entry(thread_specific_selector());
  63. }
  64. HashTable<Thread*>& thread_table()
  65. {
  66. ASSERT_INTERRUPTS_DISABLED();
  67. static HashTable<Thread*>* table;
  68. if (!table)
  69. table = new HashTable<Thread*>;
  70. return *table;
  71. }
  72. Thread::Thread(Process& process)
  73. : m_process(process)
  74. , m_name(process.name())
  75. {
  76. if (m_process.m_thread_count == 0) {
  77. // First thread gets TID == PID
  78. m_tid = process.pid();
  79. } else {
  80. m_tid = Process::allocate_pid();
  81. }
  82. process.m_thread_count++;
  83. #ifdef THREAD_DEBUG
  84. dbg() << "Created new thread " << process.name() << "(" << process.pid() << ":" << m_tid << ")";
  85. #endif
  86. set_default_signal_dispositions();
  87. m_fpu_state = (FPUState*)kmalloc_aligned(sizeof(FPUState), 16);
  88. reset_fpu_state();
  89. memset(&m_tss, 0, sizeof(m_tss));
  90. m_tss.iomapbase = sizeof(TSS32);
  91. // Only IF is set when a process boots.
  92. m_tss.eflags = 0x0202;
  93. u16 cs, ds, ss, gs;
  94. if (m_process.is_ring0()) {
  95. cs = 0x08;
  96. ds = 0x10;
  97. ss = 0x10;
  98. gs = 0;
  99. } else {
  100. cs = 0x1b;
  101. ds = 0x23;
  102. ss = 0x23;
  103. gs = thread_specific_selector() | 3;
  104. }
  105. m_tss.ds = ds;
  106. m_tss.es = ds;
  107. m_tss.fs = ds;
  108. m_tss.gs = gs;
  109. m_tss.ss = ss;
  110. m_tss.cs = cs;
  111. m_tss.cr3 = m_process.page_directory().cr3();
  112. 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);
  113. m_kernel_stack_region->set_stack(true);
  114. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  115. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
  116. if (m_process.is_ring0()) {
  117. m_tss.esp = m_kernel_stack_top;
  118. } else {
  119. // Ring 3 processes get a separate stack for ring 0.
  120. // The ring 3 stack will be assigned by exec().
  121. m_tss.ss0 = 0x10;
  122. m_tss.esp0 = m_kernel_stack_top;
  123. }
  124. if (m_process.pid() != 0) {
  125. InterruptDisabler disabler;
  126. thread_table().set(this);
  127. Scheduler::init_thread(*this);
  128. }
  129. }
  130. Thread::~Thread()
  131. {
  132. kfree_aligned(m_fpu_state);
  133. {
  134. InterruptDisabler disabler;
  135. thread_table().remove(this);
  136. }
  137. if (selector())
  138. gdt_free_entry(selector());
  139. ASSERT(m_process.m_thread_count);
  140. m_process.m_thread_count--;
  141. }
  142. void Thread::unblock()
  143. {
  144. if (current == this) {
  145. if (m_should_die)
  146. set_state(Thread::Dying);
  147. else
  148. set_state(Thread::Running);
  149. return;
  150. }
  151. ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
  152. if (m_should_die)
  153. set_state(Thread::Dying);
  154. else
  155. set_state(Thread::Runnable);
  156. }
  157. void Thread::set_should_die()
  158. {
  159. if (m_should_die) {
  160. #ifdef THREAD_DEBUG
  161. dbg() << *this << " Should already die";
  162. #endif
  163. return;
  164. }
  165. InterruptDisabler disabler;
  166. // Remember that we should die instead of returning to
  167. // the userspace.
  168. m_should_die = true;
  169. if (is_blocked()) {
  170. ASSERT(in_kernel());
  171. ASSERT(m_blocker != nullptr);
  172. // We're blocked in the kernel.
  173. m_blocker->set_interrupted_by_death();
  174. unblock();
  175. } else if (!in_kernel()) {
  176. // We're executing in userspace (and we're clearly
  177. // not the current thread). No need to unwind, so
  178. // set the state to dying right away. This also
  179. // makes sure we won't be scheduled anymore.
  180. set_state(Thread::State::Dying);
  181. }
  182. }
  183. void Thread::die_if_needed()
  184. {
  185. ASSERT(current == this);
  186. if (!m_should_die)
  187. return;
  188. unlock_process_if_locked();
  189. InterruptDisabler disabler;
  190. set_state(Thread::State::Dying);
  191. if (!Scheduler::is_active())
  192. Scheduler::pick_next_and_switch_now();
  193. }
  194. void Thread::yield_without_holding_big_lock()
  195. {
  196. bool did_unlock = unlock_process_if_locked();
  197. Scheduler::yield();
  198. if (did_unlock)
  199. relock_process();
  200. }
  201. bool Thread::unlock_process_if_locked()
  202. {
  203. return process().big_lock().force_unlock_if_locked();
  204. }
  205. void Thread::relock_process()
  206. {
  207. process().big_lock().lock();
  208. }
  209. u64 Thread::sleep(u32 ticks)
  210. {
  211. ASSERT(state() == Thread::Running);
  212. u64 wakeup_time = g_uptime + ticks;
  213. auto ret = Thread::current->block<Thread::SleepBlocker>(wakeup_time);
  214. if (wakeup_time > g_uptime) {
  215. ASSERT(ret != Thread::BlockResult::WokeNormally);
  216. }
  217. return wakeup_time;
  218. }
  219. u64 Thread::sleep_until(u64 wakeup_time)
  220. {
  221. ASSERT(state() == Thread::Running);
  222. auto ret = Thread::current->block<Thread::SleepBlocker>(wakeup_time);
  223. if (wakeup_time > g_uptime)
  224. ASSERT(ret != Thread::BlockResult::WokeNormally);
  225. return wakeup_time;
  226. }
  227. const char* Thread::state_string() const
  228. {
  229. switch (state()) {
  230. case Thread::Invalid:
  231. return "Invalid";
  232. case Thread::Runnable:
  233. return "Runnable";
  234. case Thread::Running:
  235. return "Running";
  236. case Thread::Dying:
  237. return "Dying";
  238. case Thread::Dead:
  239. return "Dead";
  240. case Thread::Stopped:
  241. return "Stopped";
  242. case Thread::Skip1SchedulerPass:
  243. return "Skip1";
  244. case Thread::Skip0SchedulerPasses:
  245. return "Skip0";
  246. case Thread::Queued:
  247. return "Queued";
  248. case Thread::Blocked:
  249. ASSERT(m_blocker != nullptr);
  250. return m_blocker->state_string();
  251. }
  252. kprintf("Thread::state_string(): Invalid state: %u\n", state());
  253. ASSERT_NOT_REACHED();
  254. return nullptr;
  255. }
  256. void Thread::finalize()
  257. {
  258. ASSERT(current == g_finalizer);
  259. #ifdef THREAD_DEBUG
  260. dbg() << "Finalizing thread " << *this;
  261. #endif
  262. set_state(Thread::State::Dead);
  263. if (m_joiner) {
  264. ASSERT(m_joiner->m_joinee == this);
  265. static_cast<JoinBlocker*>(m_joiner->m_blocker)->set_joinee_exit_value(m_exit_value);
  266. static_cast<JoinBlocker*>(m_joiner->m_blocker)->set_interrupted_by_death();
  267. m_joiner->m_joinee = nullptr;
  268. // NOTE: We clear the joiner pointer here as well, to be tidy.
  269. m_joiner = nullptr;
  270. }
  271. if (m_dump_backtrace_on_finalization)
  272. dbg() << backtrace_impl();
  273. }
  274. void Thread::finalize_dying_threads()
  275. {
  276. ASSERT(current == g_finalizer);
  277. Vector<Thread*, 32> dying_threads;
  278. {
  279. InterruptDisabler disabler;
  280. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  281. dying_threads.append(&thread);
  282. return IterationDecision::Continue;
  283. });
  284. }
  285. for (auto* thread : dying_threads) {
  286. auto& process = thread->process();
  287. thread->finalize();
  288. delete thread;
  289. if (process.m_thread_count == 0)
  290. process.finalize();
  291. }
  292. }
  293. bool Thread::tick()
  294. {
  295. ++m_ticks;
  296. if (tss().cs & 3)
  297. ++m_process.m_ticks_in_user;
  298. else
  299. ++m_process.m_ticks_in_kernel;
  300. return --m_ticks_left;
  301. }
  302. void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
  303. {
  304. ASSERT(signal < 32);
  305. InterruptDisabler disabler;
  306. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  307. if (should_ignore_signal(signal)) {
  308. #ifdef SIGNAL_DEBUG
  309. dbg() << "Signal " << signal << " was ignored by " << process();
  310. #endif
  311. return;
  312. }
  313. #ifdef SIGNAL_DEBUG
  314. if (sender)
  315. dbg() << "Signal: " << *sender << " sent " << signal << " to " << process();
  316. else
  317. dbg() << "Signal: Kernel sent " << signal << " to " << process();
  318. #endif
  319. m_pending_signals |= 1 << (signal - 1);
  320. }
  321. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  322. // thread into a state where the signal handler must be
  323. // invoked immediately, otherwise it will continue to fault.
  324. // This function should be used in an exception handler to
  325. // ensure that when the thread resumes, it's executing in
  326. // the appropriate signal handler.
  327. void Thread::send_urgent_signal_to_self(u8 signal)
  328. {
  329. // FIXME: because of a bug in dispatch_signal we can't
  330. // setup a signal while we are the current thread. Because of
  331. // this we use a work-around where we send the signal and then
  332. // block, allowing the scheduler to properly dispatch the signal
  333. // before the thread is next run.
  334. send_signal(signal, &process());
  335. (void)block<SemiPermanentBlocker>(SemiPermanentBlocker::Reason::Signal);
  336. }
  337. bool Thread::has_unmasked_pending_signals() const
  338. {
  339. return m_pending_signals & ~m_signal_mask;
  340. }
  341. ShouldUnblockThread Thread::dispatch_one_pending_signal()
  342. {
  343. ASSERT_INTERRUPTS_DISABLED();
  344. u32 signal_candidates = m_pending_signals & ~m_signal_mask;
  345. ASSERT(signal_candidates);
  346. u8 signal = 1;
  347. for (; signal < 32; ++signal) {
  348. if (signal_candidates & (1 << (signal - 1))) {
  349. break;
  350. }
  351. }
  352. return dispatch_signal(signal);
  353. }
  354. enum class DefaultSignalAction {
  355. Terminate,
  356. Ignore,
  357. DumpCore,
  358. Stop,
  359. Continue,
  360. };
  361. DefaultSignalAction default_signal_action(u8 signal)
  362. {
  363. ASSERT(signal && signal < NSIG);
  364. switch (signal) {
  365. case SIGHUP:
  366. case SIGINT:
  367. case SIGKILL:
  368. case SIGPIPE:
  369. case SIGALRM:
  370. case SIGUSR1:
  371. case SIGUSR2:
  372. case SIGVTALRM:
  373. case SIGSTKFLT:
  374. case SIGIO:
  375. case SIGPROF:
  376. case SIGTERM:
  377. case SIGPWR:
  378. return DefaultSignalAction::Terminate;
  379. case SIGCHLD:
  380. case SIGURG:
  381. case SIGWINCH:
  382. return DefaultSignalAction::Ignore;
  383. case SIGQUIT:
  384. case SIGILL:
  385. case SIGTRAP:
  386. case SIGABRT:
  387. case SIGBUS:
  388. case SIGFPE:
  389. case SIGSEGV:
  390. case SIGXCPU:
  391. case SIGXFSZ:
  392. case SIGSYS:
  393. return DefaultSignalAction::DumpCore;
  394. case SIGCONT:
  395. return DefaultSignalAction::Continue;
  396. case SIGSTOP:
  397. case SIGTSTP:
  398. case SIGTTIN:
  399. case SIGTTOU:
  400. return DefaultSignalAction::Stop;
  401. }
  402. ASSERT_NOT_REACHED();
  403. }
  404. bool Thread::should_ignore_signal(u8 signal) const
  405. {
  406. ASSERT(signal < 32);
  407. auto& action = m_signal_action_data[signal];
  408. if (action.handler_or_sigaction.is_null())
  409. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  410. if (action.handler_or_sigaction.as_ptr() == SIG_IGN)
  411. return true;
  412. return false;
  413. }
  414. bool Thread::has_signal_handler(u8 signal) const
  415. {
  416. ASSERT(signal < 32);
  417. auto& action = m_signal_action_data[signal];
  418. return !action.handler_or_sigaction.is_null();
  419. }
  420. static void push_value_on_user_stack(u32* stack, u32 data)
  421. {
  422. *stack -= 4;
  423. copy_to_user((u32*)*stack, &data);
  424. }
  425. ShouldUnblockThread Thread::dispatch_signal(u8 signal)
  426. {
  427. ASSERT_INTERRUPTS_DISABLED();
  428. ASSERT(signal > 0 && signal <= 32);
  429. ASSERT(!process().is_ring0());
  430. #ifdef SIGNAL_DEBUG
  431. kprintf("dispatch_signal %s(%u) <- %u\n", process().name().characters(), pid(), signal);
  432. #endif
  433. auto& action = m_signal_action_data[signal];
  434. // FIXME: Implement SA_SIGINFO signal handlers.
  435. ASSERT(!(action.flags & SA_SIGINFO));
  436. // Mark this signal as handled.
  437. m_pending_signals &= ~(1 << (signal - 1));
  438. if (signal == SIGSTOP) {
  439. m_stop_signal = SIGSTOP;
  440. set_state(Stopped);
  441. return ShouldUnblockThread::No;
  442. }
  443. if (signal == SIGCONT && state() == Stopped)
  444. set_state(Runnable);
  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. kprintf("%s(%u) ignored signal %u\n", process().name().characters(), pid(), 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. {
  484. u32 old_esp = *stack;
  485. u32 ret_eip = state.eip;
  486. u32 ret_eflags = state.eflags;
  487. // Align the stack to 16 bytes.
  488. // Note that we push 56 bytes (4 * 14) on to the stack,
  489. // so we need to account for this here.
  490. u32 stack_alignment = (*stack - 56) % 16;
  491. *stack -= stack_alignment;
  492. push_value_on_user_stack(stack, ret_eflags);
  493. push_value_on_user_stack(stack, ret_eip);
  494. push_value_on_user_stack(stack, state.eax);
  495. push_value_on_user_stack(stack, state.ecx);
  496. push_value_on_user_stack(stack, state.edx);
  497. push_value_on_user_stack(stack, state.ebx);
  498. push_value_on_user_stack(stack, old_esp);
  499. push_value_on_user_stack(stack, state.ebp);
  500. push_value_on_user_stack(stack, state.esi);
  501. push_value_on_user_stack(stack, state.edi);
  502. // PUSH old_signal_mask
  503. push_value_on_user_stack(stack, old_signal_mask);
  504. push_value_on_user_stack(stack, signal);
  505. push_value_on_user_stack(stack, handler_vaddr.get());
  506. push_value_on_user_stack(stack, 0); //push fake return address
  507. ASSERT((*stack % 16) == 0);
  508. };
  509. // We now place the thread state on the userspace stack.
  510. // Note that when we are in the kernel (ie. blocking) we cannot use the
  511. // tss, as that will contain kernel state; instead, we use a RegisterState.
  512. // Conversely, when the thread isn't blocking the RegisterState may not be
  513. // valid (fork, exec etc) but the tss will, so we use that instead.
  514. if (!in_kernel()) {
  515. u32* stack = &m_tss.esp;
  516. setup_stack(m_tss, stack);
  517. Scheduler::prepare_to_modify_tss(*this);
  518. m_tss.cs = 0x1b;
  519. m_tss.ds = 0x23;
  520. m_tss.es = 0x23;
  521. m_tss.fs = 0x23;
  522. m_tss.gs = thread_specific_selector() | 3;
  523. m_tss.eip = g_return_to_ring3_from_signal_trampoline.get();
  524. // FIXME: This state is such a hack. It avoids trouble if 'current' is the process receiving a signal.
  525. set_state(Skip1SchedulerPass);
  526. } else {
  527. auto& regs = get_register_dump_from_stack();
  528. u32* stack = &regs.userspace_esp;
  529. setup_stack(regs, stack);
  530. regs.eip = g_return_to_ring3_from_signal_trampoline.get();
  531. }
  532. #ifdef SIGNAL_DEBUG
  533. kprintf("signal: Okay, %s(%u) {%s} has been primed with signal handler %w:%x\n", process().name().characters(), pid(), state_string(), m_tss.cs, m_tss.eip);
  534. #endif
  535. return ShouldUnblockThread::Yes;
  536. }
  537. void Thread::set_default_signal_dispositions()
  538. {
  539. // FIXME: Set up all the right default actions. See signal(7).
  540. memset(&m_signal_action_data, 0, sizeof(m_signal_action_data));
  541. m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress(SIG_IGN);
  542. m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress(SIG_IGN);
  543. }
  544. void Thread::push_value_on_stack(uintptr_t value)
  545. {
  546. m_tss.esp -= 4;
  547. uintptr_t* stack_ptr = (uintptr_t*)m_tss.esp;
  548. copy_to_user(stack_ptr, &value);
  549. }
  550. RegisterState& Thread::get_register_dump_from_stack()
  551. {
  552. // The userspace registers should be stored at the top of the stack
  553. // We have to subtract 2 because the processor decrements the kernel
  554. // stack before pushing the args.
  555. return *(RegisterState*)(kernel_stack_top() - sizeof(RegisterState));
  556. }
  557. u32 Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment)
  558. {
  559. auto* region = m_process.allocate_region(VirtualAddress(), default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, false);
  560. ASSERT(region);
  561. region->set_stack(true);
  562. u32 new_esp = region->vaddr().offset(default_userspace_stack_size).get();
  563. // FIXME: This is weird, we put the argument contents at the base of the stack,
  564. // and the argument pointers at the top? Why?
  565. char* stack_base = (char*)region->vaddr().get();
  566. int argc = arguments.size();
  567. char** argv = (char**)stack_base;
  568. char** env = argv + arguments.size() + 1;
  569. char* bufptr = stack_base + (sizeof(char*) * (arguments.size() + 1)) + (sizeof(char*) * (environment.size() + 1));
  570. SmapDisabler disabler;
  571. for (size_t i = 0; i < arguments.size(); ++i) {
  572. argv[i] = bufptr;
  573. memcpy(bufptr, arguments[i].characters(), arguments[i].length());
  574. bufptr += arguments[i].length();
  575. *(bufptr++) = '\0';
  576. }
  577. argv[arguments.size()] = nullptr;
  578. for (size_t i = 0; i < environment.size(); ++i) {
  579. env[i] = bufptr;
  580. memcpy(bufptr, environment[i].characters(), environment[i].length());
  581. bufptr += environment[i].length();
  582. *(bufptr++) = '\0';
  583. }
  584. env[environment.size()] = nullptr;
  585. auto push_on_new_stack = [&new_esp](u32 value) {
  586. new_esp -= 4;
  587. u32* stack_ptr = (u32*)new_esp;
  588. *stack_ptr = value;
  589. };
  590. // NOTE: The stack needs to be 16-byte aligned.
  591. push_on_new_stack((uintptr_t)env);
  592. push_on_new_stack((uintptr_t)argv);
  593. push_on_new_stack((uintptr_t)argc);
  594. push_on_new_stack(0);
  595. return new_esp;
  596. }
  597. Thread* Thread::clone(Process& process)
  598. {
  599. auto* clone = new Thread(process);
  600. memcpy(clone->m_signal_action_data, m_signal_action_data, sizeof(m_signal_action_data));
  601. clone->m_signal_mask = m_signal_mask;
  602. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  603. clone->m_thread_specific_data = m_thread_specific_data;
  604. return clone;
  605. }
  606. void Thread::initialize()
  607. {
  608. Scheduler::initialize();
  609. asm volatile("fninit");
  610. asm volatile("fxsave %0"
  611. : "=m"(s_clean_fpu_state));
  612. }
  613. Vector<Thread*> Thread::all_threads()
  614. {
  615. Vector<Thread*> threads;
  616. InterruptDisabler disabler;
  617. threads.ensure_capacity(thread_table().size());
  618. for (auto* thread : thread_table())
  619. threads.unchecked_append(thread);
  620. return threads;
  621. }
  622. bool Thread::is_thread(void* ptr)
  623. {
  624. ASSERT_INTERRUPTS_DISABLED();
  625. return thread_table().contains((Thread*)ptr);
  626. }
  627. void Thread::set_state(State new_state)
  628. {
  629. InterruptDisabler disabler;
  630. if (new_state == m_state)
  631. return;
  632. if (new_state == Blocked) {
  633. // we should always have a Blocker while blocked
  634. ASSERT(m_blocker != nullptr);
  635. }
  636. m_state = new_state;
  637. if (m_process.pid() != 0) {
  638. Scheduler::update_state_for_thread(*this);
  639. }
  640. if (new_state == Dying) {
  641. g_finalizer_has_work = true;
  642. g_finalizer_wait_queue->wake_all();
  643. }
  644. }
  645. String Thread::backtrace(ProcessInspectionHandle&) const
  646. {
  647. return backtrace_impl();
  648. }
  649. struct RecognizedSymbol {
  650. u32 address;
  651. const KSym* ksym;
  652. };
  653. static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder)
  654. {
  655. if (!symbol.address)
  656. return false;
  657. bool mask_kernel_addresses = !process.is_superuser();
  658. if (!symbol.ksym) {
  659. if (!is_user_address(VirtualAddress(symbol.address))) {
  660. builder.append("0xdeadc0de\n");
  661. } else {
  662. if (!Scheduler::is_active() && process.elf_loader() && process.elf_loader()->has_symbols())
  663. builder.appendf("%p %s\n", symbol.address, process.elf_loader()->symbolicate(symbol.address).characters());
  664. else
  665. builder.appendf("%p\n", symbol.address);
  666. }
  667. return true;
  668. }
  669. unsigned offset = symbol.address - symbol.ksym->address;
  670. if (symbol.ksym->address == ksym_highest_address && offset > 4096) {
  671. builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
  672. } else {
  673. builder.appendf("%p %s +%u\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address, demangle(symbol.ksym->name).characters(), offset);
  674. }
  675. return true;
  676. }
  677. String Thread::backtrace_impl() const
  678. {
  679. Vector<RecognizedSymbol, 128> recognized_symbols;
  680. u32 start_frame;
  681. if (current == this) {
  682. asm volatile("movl %%ebp, %%eax"
  683. : "=a"(start_frame));
  684. } else {
  685. start_frame = frame_ptr();
  686. recognized_symbols.append({ tss().eip, ksymbolicate(tss().eip) });
  687. }
  688. auto& process = const_cast<Process&>(this->process());
  689. ProcessPagingScope paging_scope(process);
  690. uintptr_t stack_ptr = start_frame;
  691. for (;;) {
  692. if (!process.validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2))
  693. break;
  694. uintptr_t retaddr;
  695. if (is_user_range(VirtualAddress(stack_ptr), sizeof(uintptr_t) * 2)) {
  696. copy_from_user(&retaddr, &((uintptr_t*)stack_ptr)[1]);
  697. recognized_symbols.append({ retaddr, ksymbolicate(retaddr) });
  698. copy_from_user(&stack_ptr, (uintptr_t*)stack_ptr);
  699. } else {
  700. memcpy(&retaddr, &((uintptr_t*)stack_ptr)[1], sizeof(uintptr_t));
  701. recognized_symbols.append({ retaddr, ksymbolicate(retaddr) });
  702. memcpy(&stack_ptr, (uintptr_t*)stack_ptr, sizeof(uintptr_t));
  703. }
  704. }
  705. StringBuilder builder;
  706. for (auto& symbol : recognized_symbols) {
  707. if (!symbolicate(symbol, process, builder))
  708. break;
  709. }
  710. return builder.to_string();
  711. }
  712. Vector<uintptr_t> Thread::raw_backtrace(uintptr_t ebp) const
  713. {
  714. InterruptDisabler disabler;
  715. auto& process = const_cast<Process&>(this->process());
  716. ProcessPagingScope paging_scope(process);
  717. Vector<uintptr_t, Profiling::max_stack_frame_count> backtrace;
  718. backtrace.append(ebp);
  719. for (uintptr_t* stack_ptr = (uintptr_t*)ebp; process.validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(uintptr_t) * 2) && MM.can_read_without_faulting(process, VirtualAddress(stack_ptr), sizeof(uintptr_t) * 2); stack_ptr = (uintptr_t*)*stack_ptr) {
  720. uintptr_t retaddr = stack_ptr[1];
  721. backtrace.append(retaddr);
  722. if (backtrace.size() == Profiling::max_stack_frame_count)
  723. break;
  724. }
  725. return backtrace;
  726. }
  727. void Thread::make_thread_specific_region(Badge<Process>)
  728. {
  729. size_t thread_specific_region_alignment = max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  730. size_t thread_specific_region_size = align_up_to(process().m_master_tls_size, thread_specific_region_alignment) + sizeof(ThreadSpecificData);
  731. auto* region = process().allocate_region({}, thread_specific_region_size, "Thread-specific", PROT_READ | PROT_WRITE, true);
  732. SmapDisabler disabler;
  733. auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment)).as_ptr();
  734. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  735. m_thread_specific_data = VirtualAddress(thread_specific_data);
  736. thread_specific_data->self = thread_specific_data;
  737. if (process().m_master_tls_size)
  738. memcpy(thread_local_storage, process().m_master_tls_region->vaddr().as_ptr(), process().m_master_tls_size);
  739. }
  740. const LogStream& operator<<(const LogStream& stream, const Thread& value)
  741. {
  742. return stream << value.process().name() << "(" << value.pid() << ":" << value.tid() << ")";
  743. }
  744. void Thread::wait_on(WaitQueue& queue, Atomic<bool>* lock, Thread* beneficiary, const char* reason)
  745. {
  746. cli();
  747. bool did_unlock = unlock_process_if_locked();
  748. if (lock)
  749. *lock = false;
  750. set_state(State::Queued);
  751. queue.enqueue(*current);
  752. // Yield and wait for the queue to wake us up again.
  753. if (beneficiary)
  754. Scheduler::donate_to(beneficiary, reason);
  755. else
  756. Scheduler::yield();
  757. // We've unblocked, relock the process if needed and carry on.
  758. if (did_unlock)
  759. relock_process();
  760. }
  761. void Thread::wake_from_queue()
  762. {
  763. ASSERT(state() == State::Queued);
  764. set_state(State::Runnable);
  765. }
  766. Thread* Thread::from_tid(int tid)
  767. {
  768. InterruptDisabler disabler;
  769. Thread* found_thread = nullptr;
  770. Thread::for_each([&](auto& thread) {
  771. if (thread.tid() == tid) {
  772. found_thread = &thread;
  773. return IterationDecision::Break;
  774. }
  775. return IterationDecision::Continue;
  776. });
  777. return found_thread;
  778. }
  779. void Thread::reset_fpu_state()
  780. {
  781. memcpy(m_fpu_state, &s_clean_fpu_state, sizeof(FPUState));
  782. }
  783. }