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