Thread.cpp 20 KB

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