Thread.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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(u64 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.was_interrupted());
  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.was_interrupted());
  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(BlockResult::WokeNormally);
  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. if (m_wait_queue_node.is_in_list())
  828. result = BlockResult::InterruptedByTimeout;
  829. // Make sure we cancel the timer if woke normally.
  830. if (timeout && !result.was_interrupted())
  831. TimerQueue::the().cancel_timer(timer_id);
  832. }
  833. // The API contract guarantees we return with interrupts enabled,
  834. // regardless of how we got called
  835. sti();
  836. return result;
  837. }
  838. void Thread::wake_from_queue()
  839. {
  840. ScopedSpinLock lock(g_scheduler_lock);
  841. ASSERT(state() == State::Queued);
  842. m_wait_reason = nullptr;
  843. if (this != Thread::current())
  844. set_state(State::Runnable);
  845. else
  846. set_state(State::Running);
  847. }
  848. Thread* Thread::from_tid(int tid)
  849. {
  850. InterruptDisabler disabler;
  851. Thread* found_thread = nullptr;
  852. Thread::for_each([&](auto& thread) {
  853. if (thread.tid() == tid) {
  854. found_thread = &thread;
  855. return IterationDecision::Break;
  856. }
  857. return IterationDecision::Continue;
  858. });
  859. return found_thread;
  860. }
  861. void Thread::reset_fpu_state()
  862. {
  863. memcpy(m_fpu_state, &Processor::current().clean_fpu_state(), sizeof(FPUState));
  864. }
  865. void Thread::start_tracing_from(pid_t tracer)
  866. {
  867. m_tracer = ThreadTracer::create(tracer);
  868. }
  869. void Thread::stop_tracing()
  870. {
  871. m_tracer = nullptr;
  872. }
  873. void Thread::tracer_trap(const RegisterState& regs)
  874. {
  875. ASSERT(m_tracer.ptr());
  876. m_tracer->set_regs(regs);
  877. send_urgent_signal_to_self(SIGTRAP);
  878. }
  879. const Thread::Blocker& Thread::blocker() const
  880. {
  881. ASSERT(m_blocker);
  882. return *m_blocker;
  883. }
  884. }