Thread.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #include <AK/ELF/ELFLoader.h>
  2. #include <AK/StringBuilder.h>
  3. #include <Kernel/FileSystem/FileDescription.h>
  4. #include <Kernel/Process.h>
  5. #include <Kernel/Scheduler.h>
  6. #include <Kernel/Thread.h>
  7. #include <Kernel/VM/MemoryManager.h>
  8. #include <LibC/signal_numbers.h>
  9. //#define SIGNAL_DEBUG
  10. u16 thread_specific_selector()
  11. {
  12. static u16 selector;
  13. if (!selector) {
  14. selector = gdt_alloc_entry();
  15. auto& descriptor = get_gdt_entry(selector);
  16. descriptor.dpl = 3;
  17. descriptor.segment_present = 1;
  18. descriptor.granularity = 0;
  19. descriptor.zero = 0;
  20. descriptor.operation_size = 1;
  21. descriptor.descriptor_type = 1;
  22. descriptor.type = 2;
  23. }
  24. return selector;
  25. }
  26. Descriptor& thread_specific_descriptor()
  27. {
  28. return get_gdt_entry(thread_specific_selector());
  29. }
  30. HashTable<Thread*>& thread_table()
  31. {
  32. ASSERT_INTERRUPTS_DISABLED();
  33. static HashTable<Thread*>* table;
  34. if (!table)
  35. table = new HashTable<Thread*>;
  36. return *table;
  37. }
  38. Thread::Thread(Process& process)
  39. : m_process(process)
  40. , m_tid(process.m_next_tid++)
  41. {
  42. dbgprintf("Thread{%p}: New thread TID=%u in %s(%u)\n", this, m_tid, process.name().characters(), process.pid());
  43. set_default_signal_dispositions();
  44. m_fpu_state = (FPUState*)kmalloc_aligned(sizeof(FPUState), 16);
  45. memset(m_fpu_state, 0, sizeof(FPUState));
  46. memset(&m_tss, 0, sizeof(m_tss));
  47. // Only IF is set when a process boots.
  48. m_tss.eflags = 0x0202;
  49. u16 cs, ds, ss, gs;
  50. if (m_process.is_ring0()) {
  51. cs = 0x08;
  52. ds = 0x10;
  53. ss = 0x10;
  54. gs = 0;
  55. } else {
  56. cs = 0x1b;
  57. ds = 0x23;
  58. ss = 0x23;
  59. gs = thread_specific_selector() | 3;
  60. }
  61. m_tss.ds = ds;
  62. m_tss.es = ds;
  63. m_tss.fs = ds;
  64. m_tss.gs = gs;
  65. m_tss.ss = ss;
  66. m_tss.cs = cs;
  67. m_tss.cr3 = m_process.page_directory().cr3();
  68. if (m_process.is_ring0()) {
  69. // FIXME: This memory is leaked.
  70. // But uh, there's also no kernel process termination, so I guess it's not technically leaked...
  71. m_kernel_stack_base = (u32)kmalloc_eternal(default_kernel_stack_size);
  72. m_kernel_stack_top = (m_kernel_stack_base + default_kernel_stack_size) & 0xfffffff8u;
  73. m_tss.esp = m_kernel_stack_top;
  74. } else {
  75. // Ring3 processes need a separate stack for Ring0.
  76. m_kernel_stack_region = MM.allocate_kernel_region(default_kernel_stack_size, String::format("Kernel Stack (Thread %d)", m_tid));
  77. m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
  78. m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & 0xfffffff8u;
  79. m_tss.ss0 = 0x10;
  80. m_tss.esp0 = m_kernel_stack_top;
  81. }
  82. // HACK: Ring2 SS in the TSS is the current PID.
  83. m_tss.ss2 = m_process.pid();
  84. m_far_ptr.offset = 0x98765432;
  85. if (m_process.pid() != 0) {
  86. InterruptDisabler disabler;
  87. thread_table().set(this);
  88. Scheduler::init_thread(*this);
  89. }
  90. }
  91. Thread::~Thread()
  92. {
  93. dbgprintf("~Thread{%p}\n", this);
  94. kfree_aligned(m_fpu_state);
  95. {
  96. InterruptDisabler disabler;
  97. thread_table().remove(this);
  98. }
  99. if (g_last_fpu_thread == this)
  100. g_last_fpu_thread = nullptr;
  101. if (selector())
  102. gdt_free_entry(selector());
  103. if (m_userspace_stack_region)
  104. m_process.deallocate_region(*m_userspace_stack_region);
  105. }
  106. void Thread::unblock()
  107. {
  108. if (current == this) {
  109. set_state(Thread::Running);
  110. return;
  111. }
  112. ASSERT(m_state != Thread::Runnable && m_state != Thread::Running);
  113. set_state(Thread::Runnable);
  114. }
  115. void Thread::block_helper()
  116. {
  117. // This function mostly exists to avoid circular header dependencies. If
  118. // anything needs adding, think carefully about whether it belongs in
  119. // block() instead. Remember that we're unlocking here, so be very careful
  120. // about altering any state once we're unlocked!
  121. bool did_unlock = process().big_lock().unlock_if_locked();
  122. Scheduler::yield();
  123. if (did_unlock)
  124. process().big_lock().lock();
  125. }
  126. u64 Thread::sleep(u32 ticks)
  127. {
  128. ASSERT(state() == Thread::Running);
  129. u64 wakeup_time = g_uptime + ticks;
  130. auto ret = current->block<Thread::SleepBlocker>(wakeup_time);
  131. if (wakeup_time > g_uptime) {
  132. ASSERT(ret == Thread::BlockResult::InterruptedBySignal);
  133. }
  134. return wakeup_time;
  135. }
  136. u64 Thread::sleep_until(u64 wakeup_time)
  137. {
  138. ASSERT(state() == Thread::Running);
  139. auto ret = current->block<Thread::SleepBlocker>(wakeup_time);
  140. if (wakeup_time > g_uptime)
  141. ASSERT(ret == Thread::BlockResult::InterruptedBySignal);
  142. return wakeup_time;
  143. }
  144. const char* Thread::state_string() const
  145. {
  146. switch (state()) {
  147. case Thread::Invalid:
  148. return "Invalid";
  149. case Thread::Runnable:
  150. return "Runnable";
  151. case Thread::Running:
  152. return "Running";
  153. case Thread::Dying:
  154. return "Dying";
  155. case Thread::Dead:
  156. return "Dead";
  157. case Thread::Stopped:
  158. return "Stopped";
  159. case Thread::Skip1SchedulerPass:
  160. return "Skip1";
  161. case Thread::Skip0SchedulerPasses:
  162. return "Skip0";
  163. case Thread::Blocked:
  164. ASSERT(m_blocker != nullptr);
  165. return m_blocker->state_string();
  166. }
  167. kprintf("Thread::state_string(): Invalid state: %u\n", state());
  168. ASSERT_NOT_REACHED();
  169. return nullptr;
  170. }
  171. void Thread::finalize()
  172. {
  173. ASSERT(current == g_finalizer);
  174. dbgprintf("Finalizing Thread %u in %s(%u)\n", tid(), m_process.name().characters(), pid());
  175. set_state(Thread::State::Dead);
  176. if (m_dump_backtrace_on_finalization)
  177. dbg() << backtrace_impl();
  178. if (this == &m_process.main_thread()) {
  179. m_process.finalize();
  180. return;
  181. }
  182. delete this;
  183. }
  184. void Thread::finalize_dying_threads()
  185. {
  186. ASSERT(current == g_finalizer);
  187. Vector<Thread*, 32> dying_threads;
  188. {
  189. InterruptDisabler disabler;
  190. for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
  191. dying_threads.append(&thread);
  192. return IterationDecision::Continue;
  193. });
  194. }
  195. for (auto* thread : dying_threads)
  196. thread->finalize();
  197. }
  198. bool Thread::tick()
  199. {
  200. ++m_ticks;
  201. if (tss().cs & 3)
  202. ++m_process.m_ticks_in_user;
  203. else
  204. ++m_process.m_ticks_in_kernel;
  205. return --m_ticks_left;
  206. }
  207. void Thread::send_signal(u8 signal, Process* sender)
  208. {
  209. ASSERT(signal < 32);
  210. InterruptDisabler disabler;
  211. // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
  212. if (should_ignore_signal(signal)) {
  213. dbg() << "signal " << signal << " was ignored by " << process();
  214. return;
  215. }
  216. if (sender)
  217. dbgprintf("signal: %s(%u) sent %d to %s(%u)\n", sender->name().characters(), sender->pid(), signal, process().name().characters(), pid());
  218. else
  219. dbgprintf("signal: kernel sent %d to %s(%u)\n", signal, process().name().characters(), pid());
  220. m_pending_signals |= 1 << (signal - 1);
  221. }
  222. // Certain exceptions, such as SIGSEGV and SIGILL, put a
  223. // thread into a state where the signal handler must be
  224. // invoked immediately, otherwise it will continue to fault.
  225. // This function should be used in an exception handler to
  226. // ensure that when the thread resumes, it's executing in
  227. // the appropriate signal handler.
  228. void Thread::send_urgent_signal_to_self(u8 signal)
  229. {
  230. // FIXME: because of a bug in dispatch_signal we can't
  231. // setup a signal while we are the current thread. Because of
  232. // this we use a work-around where we send the signal and then
  233. // block, allowing the scheduler to properly dispatch the signal
  234. // before the thread is next run.
  235. send_signal(signal, &process());
  236. (void)block<SemiPermanentBlocker>(SemiPermanentBlocker::Reason::Signal);
  237. }
  238. bool Thread::has_unmasked_pending_signals() const
  239. {
  240. return m_pending_signals & ~m_signal_mask;
  241. }
  242. ShouldUnblockThread Thread::dispatch_one_pending_signal()
  243. {
  244. ASSERT_INTERRUPTS_DISABLED();
  245. u32 signal_candidates = m_pending_signals & ~m_signal_mask;
  246. ASSERT(signal_candidates);
  247. u8 signal = 1;
  248. for (; signal < 32; ++signal) {
  249. if (signal_candidates & (1 << (signal - 1))) {
  250. break;
  251. }
  252. }
  253. return dispatch_signal(signal);
  254. }
  255. enum class DefaultSignalAction {
  256. Terminate,
  257. Ignore,
  258. DumpCore,
  259. Stop,
  260. Continue,
  261. };
  262. DefaultSignalAction default_signal_action(u8 signal)
  263. {
  264. ASSERT(signal && signal < NSIG);
  265. switch (signal) {
  266. case SIGHUP:
  267. case SIGINT:
  268. case SIGKILL:
  269. case SIGPIPE:
  270. case SIGALRM:
  271. case SIGUSR1:
  272. case SIGUSR2:
  273. case SIGVTALRM:
  274. case SIGSTKFLT:
  275. case SIGIO:
  276. case SIGPROF:
  277. case SIGTERM:
  278. case SIGPWR:
  279. return DefaultSignalAction::Terminate;
  280. case SIGCHLD:
  281. case SIGURG:
  282. case SIGWINCH:
  283. return DefaultSignalAction::Ignore;
  284. case SIGQUIT:
  285. case SIGILL:
  286. case SIGTRAP:
  287. case SIGABRT:
  288. case SIGBUS:
  289. case SIGFPE:
  290. case SIGSEGV:
  291. case SIGXCPU:
  292. case SIGXFSZ:
  293. case SIGSYS:
  294. return DefaultSignalAction::DumpCore;
  295. case SIGCONT:
  296. return DefaultSignalAction::Continue;
  297. case SIGSTOP:
  298. case SIGTSTP:
  299. case SIGTTIN:
  300. case SIGTTOU:
  301. return DefaultSignalAction::Stop;
  302. }
  303. ASSERT_NOT_REACHED();
  304. }
  305. bool Thread::should_ignore_signal(u8 signal) const
  306. {
  307. ASSERT(signal < 32);
  308. auto& action = m_signal_action_data[signal];
  309. if (action.handler_or_sigaction.is_null())
  310. return default_signal_action(signal) == DefaultSignalAction::Ignore;
  311. if (action.handler_or_sigaction.as_ptr() == SIG_IGN)
  312. return true;
  313. return false;
  314. }
  315. bool Thread::has_signal_handler(u8 signal) const
  316. {
  317. ASSERT(signal < 32);
  318. auto& action = m_signal_action_data[signal];
  319. return !action.handler_or_sigaction.is_null();
  320. }
  321. ShouldUnblockThread Thread::dispatch_signal(u8 signal)
  322. {
  323. ASSERT_INTERRUPTS_DISABLED();
  324. ASSERT(signal > 0 && signal <= 32);
  325. ASSERT(!process().is_ring0());
  326. #ifdef SIGNAL_DEBUG
  327. kprintf("dispatch_signal %s(%u) <- %u\n", process().name().characters(), pid(), signal);
  328. #endif
  329. auto& action = m_signal_action_data[signal];
  330. // FIXME: Implement SA_SIGINFO signal handlers.
  331. ASSERT(!(action.flags & SA_SIGINFO));
  332. // Mark this signal as handled.
  333. m_pending_signals &= ~(1 << (signal - 1));
  334. if (signal == SIGSTOP) {
  335. set_state(Stopped);
  336. return ShouldUnblockThread::No;
  337. }
  338. if (signal == SIGCONT && state() == Stopped)
  339. set_state(Runnable);
  340. auto handler_vaddr = action.handler_or_sigaction;
  341. if (handler_vaddr.is_null()) {
  342. switch (default_signal_action(signal)) {
  343. case DefaultSignalAction::Stop:
  344. set_state(Stopped);
  345. return ShouldUnblockThread::No;
  346. case DefaultSignalAction::DumpCore:
  347. process().for_each_thread([](auto& thread) {
  348. thread.set_dump_backtrace_on_finalization();
  349. return IterationDecision::Continue;
  350. });
  351. [[fallthrough]];
  352. case DefaultSignalAction::Terminate:
  353. m_process.terminate_due_to_signal(signal);
  354. return ShouldUnblockThread::No;
  355. case DefaultSignalAction::Ignore:
  356. ASSERT_NOT_REACHED();
  357. case DefaultSignalAction::Continue:
  358. return ShouldUnblockThread::Yes;
  359. }
  360. ASSERT_NOT_REACHED();
  361. }
  362. if (handler_vaddr.as_ptr() == SIG_IGN) {
  363. #ifdef SIGNAL_DEBUG
  364. kprintf("%s(%u) ignored signal %u\n", process().name().characters(), pid(), signal);
  365. #endif
  366. return ShouldUnblockThread::Yes;
  367. }
  368. ProcessPagingScope paging_scope(m_process);
  369. auto& regs = get_RegisterDump_from_stack();
  370. u32 old_signal_mask = m_signal_mask;
  371. u32 new_signal_mask = action.mask;
  372. if (action.flags & SA_NODEFER)
  373. new_signal_mask &= ~(1 << (signal - 1));
  374. else
  375. new_signal_mask |= 1 << (signal - 1);
  376. m_signal_mask |= new_signal_mask;
  377. u32 old_esp = regs.esp_if_crossRing;
  378. u32 ret_eip = regs.eip;
  379. u32 ret_eflags = regs.eflags;
  380. // Align the stack to 16 bytes.
  381. // Note that we push 56 bytes (4 * 14) on to the stack,
  382. // so we need to account for this here.
  383. u32 stack_alignment = (regs.esp_if_crossRing - 56) % 16;
  384. regs.esp_if_crossRing -= stack_alignment;
  385. push_value_on_user_stack(regs, ret_eflags);
  386. push_value_on_user_stack(regs, ret_eip);
  387. push_value_on_user_stack(regs, regs.eax);
  388. push_value_on_user_stack(regs, regs.ecx);
  389. push_value_on_user_stack(regs, regs.edx);
  390. push_value_on_user_stack(regs, regs.ebx);
  391. push_value_on_user_stack(regs, old_esp);
  392. push_value_on_user_stack(regs, regs.ebp);
  393. push_value_on_user_stack(regs, regs.esi);
  394. push_value_on_user_stack(regs, regs.edi);
  395. // PUSH old_signal_mask
  396. push_value_on_user_stack(regs, old_signal_mask);
  397. push_value_on_user_stack(regs, signal);
  398. push_value_on_user_stack(regs, handler_vaddr.get());
  399. push_value_on_user_stack(regs, 0); //push fake return address
  400. regs.eip = g_return_to_ring3_from_signal_trampoline.get();
  401. ASSERT((regs.esp_if_crossRing % 16) == 0);
  402. // If we're not blocking we need to update the tss so
  403. // that the far jump in Scheduler goes to the proper location.
  404. // When we are blocking we don't update the TSS as we want to
  405. // resume at the blocker and descend the stack, cleaning up nicely.
  406. if (!in_kernel()) {
  407. Scheduler::prepare_to_modify_tss(*this);
  408. m_tss.cs = 0x1b;
  409. m_tss.ds = 0x23;
  410. m_tss.es = 0x23;
  411. m_tss.fs = 0x23;
  412. m_tss.gs = thread_specific_selector() | 3;
  413. m_tss.eip = regs.eip;
  414. m_tss.esp = regs.esp_if_crossRing;
  415. // FIXME: This state is such a hack. It avoids trouble if 'current' is the process receiving a signal.
  416. set_state(Skip1SchedulerPass);
  417. }
  418. #ifdef SIGNAL_DEBUG
  419. 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);
  420. #endif
  421. return ShouldUnblockThread::Yes;
  422. }
  423. void Thread::set_default_signal_dispositions()
  424. {
  425. // FIXME: Set up all the right default actions. See signal(7).
  426. memset(&m_signal_action_data, 0, sizeof(m_signal_action_data));
  427. m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress((u32)SIG_IGN);
  428. m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress((u32)SIG_IGN);
  429. }
  430. void Thread::push_value_on_user_stack(RegisterDump& registers, u32 value)
  431. {
  432. registers.esp_if_crossRing -= 4;
  433. u32* stack_ptr = (u32*)registers.esp_if_crossRing;
  434. *stack_ptr = value;
  435. }
  436. void Thread::push_value_on_stack(u32 value)
  437. {
  438. m_tss.esp -= 4;
  439. u32* stack_ptr = (u32*)m_tss.esp;
  440. *stack_ptr = value;
  441. }
  442. RegisterDump& Thread::get_RegisterDump_from_stack()
  443. {
  444. // The userspace registers should be stored at the top of the stack
  445. // We have to subtract 2 because the processor decrements the kernel
  446. // stack before pushing the args.
  447. return *(RegisterDump*)(kernel_stack_top() - sizeof(RegisterDump) - 2);
  448. }
  449. void Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment)
  450. {
  451. auto* region = m_process.allocate_region(VirtualAddress(), default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, false);
  452. ASSERT(region);
  453. m_tss.esp = region->vaddr().offset(default_userspace_stack_size).get();
  454. char* stack_base = (char*)region->vaddr().get();
  455. int argc = arguments.size();
  456. char** argv = (char**)stack_base;
  457. char** env = argv + arguments.size() + 1;
  458. char* bufptr = stack_base + (sizeof(char*) * (arguments.size() + 1)) + (sizeof(char*) * (environment.size() + 1));
  459. for (int i = 0; i < arguments.size(); ++i) {
  460. argv[i] = bufptr;
  461. memcpy(bufptr, arguments[i].characters(), arguments[i].length());
  462. bufptr += arguments[i].length();
  463. *(bufptr++) = '\0';
  464. }
  465. argv[arguments.size()] = nullptr;
  466. for (int i = 0; i < environment.size(); ++i) {
  467. env[i] = bufptr;
  468. memcpy(bufptr, environment[i].characters(), environment[i].length());
  469. bufptr += environment[i].length();
  470. *(bufptr++) = '\0';
  471. }
  472. env[environment.size()] = nullptr;
  473. // NOTE: The stack needs to be 16-byte aligned.
  474. push_value_on_stack((u32)env);
  475. push_value_on_stack((u32)argv);
  476. push_value_on_stack((u32)argc);
  477. push_value_on_stack(0);
  478. }
  479. void Thread::make_userspace_stack_for_secondary_thread(void* argument)
  480. {
  481. m_userspace_stack_region = m_process.allocate_region(VirtualAddress(), default_userspace_stack_size, String::format("Stack (Thread %d)", tid()), PROT_READ | PROT_WRITE, false);
  482. ASSERT(m_userspace_stack_region);
  483. m_tss.esp = m_userspace_stack_region->vaddr().offset(default_userspace_stack_size).get();
  484. // NOTE: The stack needs to be 16-byte aligned.
  485. push_value_on_stack((u32)argument);
  486. push_value_on_stack(0);
  487. }
  488. Thread* Thread::clone(Process& process)
  489. {
  490. auto* clone = new Thread(process);
  491. memcpy(clone->m_signal_action_data, m_signal_action_data, sizeof(m_signal_action_data));
  492. clone->m_signal_mask = m_signal_mask;
  493. memcpy(clone->m_fpu_state, m_fpu_state, sizeof(FPUState));
  494. clone->m_has_used_fpu = m_has_used_fpu;
  495. clone->m_thread_specific_data = m_thread_specific_data;
  496. return clone;
  497. }
  498. void Thread::initialize()
  499. {
  500. Scheduler::initialize();
  501. }
  502. Vector<Thread*> Thread::all_threads()
  503. {
  504. Vector<Thread*> threads;
  505. InterruptDisabler disabler;
  506. threads.ensure_capacity(thread_table().size());
  507. for (auto* thread : thread_table())
  508. threads.unchecked_append(thread);
  509. return threads;
  510. }
  511. bool Thread::is_thread(void* ptr)
  512. {
  513. ASSERT_INTERRUPTS_DISABLED();
  514. return thread_table().contains((Thread*)ptr);
  515. }
  516. void Thread::set_state(State new_state)
  517. {
  518. InterruptDisabler disabler;
  519. if (new_state == Blocked) {
  520. // we should always have a Blocker while blocked
  521. ASSERT(m_blocker != nullptr);
  522. }
  523. m_state = new_state;
  524. if (m_process.pid() != 0) {
  525. Scheduler::update_state_for_thread(*this);
  526. }
  527. }
  528. String Thread::backtrace(ProcessInspectionHandle&) const
  529. {
  530. return backtrace_impl();
  531. }
  532. String Thread::backtrace_impl() const
  533. {
  534. auto& process = const_cast<Process&>(this->process());
  535. ProcessPagingScope paging_scope(process);
  536. struct RecognizedSymbol {
  537. u32 address;
  538. const KSym* ksym;
  539. };
  540. StringBuilder builder;
  541. Vector<RecognizedSymbol, 64> recognized_symbols;
  542. recognized_symbols.append({ tss().eip, ksymbolicate(tss().eip) });
  543. for (u32* stack_ptr = (u32*)frame_ptr(); process.validate_read_from_kernel(VirtualAddress((u32)stack_ptr)); stack_ptr = (u32*)*stack_ptr) {
  544. u32 retaddr = stack_ptr[1];
  545. recognized_symbols.append({ retaddr, ksymbolicate(retaddr) });
  546. }
  547. for (auto& symbol : recognized_symbols) {
  548. if (!symbol.address)
  549. break;
  550. if (!symbol.ksym) {
  551. if (!Scheduler::is_active() && process.elf_loader() && process.elf_loader()->has_symbols())
  552. builder.appendf("%p %s\n", symbol.address, process.elf_loader()->symbolicate(symbol.address).characters());
  553. else
  554. builder.appendf("%p\n", symbol.address);
  555. continue;
  556. }
  557. unsigned offset = symbol.address - symbol.ksym->address;
  558. if (symbol.ksym->address == ksym_highest_address && offset > 4096)
  559. builder.appendf("%p\n", symbol.address);
  560. else
  561. builder.appendf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
  562. }
  563. return builder.to_string();
  564. }
  565. void Thread::make_thread_specific_region(Badge<Process>)
  566. {
  567. size_t thread_specific_region_alignment = max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
  568. size_t thread_specific_region_size = align_up_to(process().m_master_tls_size, thread_specific_region_alignment) + sizeof(ThreadSpecificData);
  569. auto* region = process().allocate_region({}, thread_specific_region_size, "Thread-specific", PROT_READ | PROT_WRITE, true);
  570. auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment)).as_ptr();
  571. auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
  572. m_thread_specific_data = VirtualAddress((u32)thread_specific_data);
  573. thread_specific_data->self = thread_specific_data;
  574. if (process().m_master_tls_size)
  575. memcpy(thread_local_storage, process().m_master_tls_region->vaddr().as_ptr(), process().m_master_tls_size);
  576. }
  577. const LogStream& operator<<(const LogStream& stream, const Thread& value)
  578. {
  579. return stream << value.process().name() << "(" << value.pid() << ":" << value.tid() << ")";
  580. }