Thread.cpp 33 KB

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