Serenity Operating System
1/*
2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/ScopeGuard.h>
8#include <AK/Singleton.h>
9#include <AK/StringBuilder.h>
10#include <AK/TemporaryChange.h>
11#include <AK/Time.h>
12#include <Kernel/API/POSIX/signal_numbers.h>
13#include <Kernel/Arch/PageDirectory.h>
14#include <Kernel/Arch/SmapDisabler.h>
15#include <Kernel/Arch/TrapFrame.h>
16#include <Kernel/Debug.h>
17#include <Kernel/Devices/KCOVDevice.h>
18#include <Kernel/FileSystem/OpenFileDescription.h>
19#include <Kernel/InterruptDisabler.h>
20#include <Kernel/KSyms.h>
21#include <Kernel/Memory/MemoryManager.h>
22#include <Kernel/Memory/ScopedAddressSpaceSwitcher.h>
23#include <Kernel/Panic.h>
24#include <Kernel/PerformanceEventBuffer.h>
25#include <Kernel/Process.h>
26#include <Kernel/Scheduler.h>
27#include <Kernel/Sections.h>
28#include <Kernel/Thread.h>
29#include <Kernel/ThreadTracer.h>
30#include <Kernel/TimerQueue.h>
31#include <Kernel/kstdio.h>
32
33namespace Kernel {
34
35static Singleton<SpinlockProtected<Thread::GlobalList, LockRank::None>> s_list;
36
37SpinlockProtected<Thread::GlobalList, LockRank::None>& Thread::all_instances()
38{
39 return *s_list;
40}
41
42ErrorOr<NonnullLockRefPtr<Thread>> Thread::try_create(NonnullLockRefPtr<Process> process)
43{
44 auto kernel_stack_region = TRY(MM.allocate_kernel_region(default_kernel_stack_size, {}, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow));
45 kernel_stack_region->set_stack(true);
46
47 auto block_timer = TRY(try_make_lock_ref_counted<Timer>());
48
49 auto name = TRY(process->name().with([](auto& name) { return name->try_clone(); }));
50 return adopt_nonnull_lock_ref_or_enomem(new (nothrow) Thread(move(process), move(kernel_stack_region), move(block_timer), move(name)));
51}
52
53Thread::Thread(NonnullLockRefPtr<Process> process, NonnullOwnPtr<Memory::Region> kernel_stack_region, NonnullLockRefPtr<Timer> block_timer, NonnullOwnPtr<KString> name)
54 : m_process(move(process))
55 , m_kernel_stack_region(move(kernel_stack_region))
56 , m_name(move(name))
57 , m_block_timer(move(block_timer))
58{
59 bool is_first_thread = m_process->add_thread(*this);
60 if (is_first_thread) {
61 // First thread gets TID == PID
62 m_tid = m_process->pid().value();
63 } else {
64 m_tid = Process::allocate_pid().value();
65 }
66
67 // FIXME: Handle KString allocation failure.
68 m_kernel_stack_region->set_name(MUST(KString::formatted("Kernel stack (thread {})", m_tid.value())));
69
70 Thread::all_instances().with([&](auto& list) {
71 list.append(*this);
72 });
73
74 if constexpr (THREAD_DEBUG) {
75 m_process->name().with([&](auto& process_name) {
76 dbgln("Created new thread {}({}:{})", process_name->view(), m_process->pid().value(), m_tid.value());
77 });
78 }
79
80 reset_fpu_state();
81
82 m_kernel_stack_base = m_kernel_stack_region->vaddr().get();
83 m_kernel_stack_top = m_kernel_stack_region->vaddr().offset(default_kernel_stack_size).get() & ~(FlatPtr)0x7u;
84
85 m_process->address_space().with([&](auto& space) {
86 m_regs.set_initial_state(m_process->is_kernel_process(), *space, m_kernel_stack_top);
87 });
88
89 // We need to add another reference if we could successfully create
90 // all the resources needed for this thread. The reason for this is that
91 // we don't want to delete this thread after dropping the reference,
92 // it may still be running or scheduled to be run.
93 // The finalizer is responsible for dropping this reference once this
94 // thread is ready to be cleaned up.
95 ref();
96}
97
98Thread::~Thread()
99{
100 VERIFY(!m_process_thread_list_node.is_in_list());
101
102 // We shouldn't be queued
103 VERIFY(m_runnable_priority < 0);
104}
105
106Thread::BlockResult Thread::block_impl(BlockTimeout const& timeout, Blocker& blocker)
107{
108 VERIFY(!Processor::current_in_irq());
109 VERIFY(this == Thread::current());
110 ScopedCritical critical;
111
112 SpinlockLocker scheduler_lock(g_scheduler_lock);
113
114 SpinlockLocker block_lock(m_block_lock);
115 // We need to hold m_block_lock so that nobody can unblock a blocker as soon
116 // as it is constructed and registered elsewhere
117
118 ScopeGuard finalize_guard([&] {
119 blocker.finalize();
120 });
121
122 if (!blocker.setup_blocker()) {
123 blocker.will_unblock_immediately_without_blocking(Blocker::UnblockImmediatelyReason::UnblockConditionAlreadyMet);
124 return BlockResult::NotBlocked;
125 }
126
127 // Relaxed semantics are fine for timeout_unblocked because we
128 // synchronize on the spin locks already.
129 Atomic<bool, AK::MemoryOrder::memory_order_relaxed> timeout_unblocked(false);
130 bool timer_was_added = false;
131
132 switch (state()) {
133 case Thread::State::Stopped:
134 // It's possible that we were requested to be stopped!
135 break;
136 case Thread::State::Running:
137 VERIFY(m_blocker == nullptr);
138 break;
139 default:
140 VERIFY_NOT_REACHED();
141 }
142
143 m_blocker = &blocker;
144
145 if (auto& block_timeout = blocker.override_timeout(timeout); !block_timeout.is_infinite()) {
146 // Process::kill_all_threads may be called at any time, which will mark all
147 // threads to die. In that case
148 timer_was_added = TimerQueue::the().add_timer_without_id(*m_block_timer, block_timeout.clock_id(), block_timeout.absolute_time(), [&]() {
149 VERIFY(!Processor::current_in_irq());
150 VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
151 VERIFY(!m_block_lock.is_locked_by_current_processor());
152 // NOTE: this may execute on the same or any other processor!
153 SpinlockLocker scheduler_lock(g_scheduler_lock);
154 SpinlockLocker block_lock(m_block_lock);
155 if (m_blocker && !timeout_unblocked.exchange(true))
156 unblock();
157 });
158 if (!timer_was_added) {
159 // Timeout is already in the past
160 blocker.will_unblock_immediately_without_blocking(Blocker::UnblockImmediatelyReason::TimeoutInThePast);
161 m_blocker = nullptr;
162 return BlockResult::InterruptedByTimeout;
163 }
164 }
165
166 blocker.begin_blocking({});
167
168 set_state(Thread::State::Blocked);
169
170 block_lock.unlock();
171 scheduler_lock.unlock();
172
173 dbgln_if(THREAD_DEBUG, "Thread {} blocking on {} ({}) -->", *this, &blocker, blocker.state_string());
174 bool did_timeout = false;
175 u32 lock_count_to_restore = 0;
176 auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
177 for (;;) {
178 // Yield to the scheduler, and wait for us to resume unblocked.
179 VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
180 VERIFY(Processor::in_critical());
181 yield_without_releasing_big_lock();
182 VERIFY(Processor::in_critical());
183
184 SpinlockLocker block_lock2(m_block_lock);
185 if (m_blocker && !m_blocker->can_be_interrupted() && !m_should_die) {
186 block_lock2.unlock();
187 dbgln("Thread should not be unblocking, current state: {}", state_string());
188 set_state(Thread::State::Blocked);
189 continue;
190 }
191 // Prevent the timeout from unblocking this thread if it happens to
192 // be in the process of firing already
193 did_timeout |= timeout_unblocked.exchange(true);
194 if (m_blocker) {
195 // Remove ourselves...
196 VERIFY(m_blocker == &blocker);
197 m_blocker = nullptr;
198 }
199 dbgln_if(THREAD_DEBUG, "<-- Thread {} unblocked from {} ({})", *this, &blocker, blocker.state_string());
200 break;
201 }
202
203 // Notify the blocker that we are no longer blocking. It may need
204 // to clean up now while we're still holding m_lock
205 auto result = blocker.end_blocking({}, did_timeout); // calls was_unblocked internally
206
207 if (timer_was_added && !did_timeout) {
208 // Cancel the timer while not holding any locks. This allows
209 // the timer function to complete before we remove it
210 // (e.g. if it's on another processor)
211 TimerQueue::the().cancel_timer(*m_block_timer);
212 }
213 if (previous_locked != LockMode::Unlocked) {
214 // NOTE: This may trigger another call to Thread::block().
215 relock_process(previous_locked, lock_count_to_restore);
216 }
217 return result;
218}
219
220void Thread::block(Kernel::Mutex& lock, SpinlockLocker<Spinlock<LockRank::None>>& lock_lock, u32 lock_count)
221{
222 VERIFY(!Processor::current_in_irq());
223 VERIFY(this == Thread::current());
224 ScopedCritical critical;
225
226 SpinlockLocker scheduler_lock(g_scheduler_lock);
227 SpinlockLocker block_lock(m_block_lock);
228
229 switch (state()) {
230 case Thread::State::Stopped:
231 // It's possible that we were requested to be stopped!
232 break;
233 case Thread::State::Running:
234 VERIFY(m_blocker == nullptr);
235 break;
236 default:
237 dbgln("Error: Attempting to block with invalid thread state - {}", state_string());
238 VERIFY_NOT_REACHED();
239 }
240
241 // If we're blocking on the big-lock we may actually be in the process
242 // of unblocking from another lock. If that's the case m_blocking_mutex
243 // is already set
244 auto& big_lock = process().big_lock();
245 VERIFY((&lock == &big_lock && m_blocking_mutex != &big_lock) || !m_blocking_mutex);
246
247 auto* previous_blocking_mutex = m_blocking_mutex;
248 m_blocking_mutex = &lock;
249 m_lock_requested_count = lock_count;
250
251 set_state(Thread::State::Blocked);
252
253 block_lock.unlock();
254 scheduler_lock.unlock();
255
256 lock_lock.unlock();
257
258 dbgln_if(THREAD_DEBUG, "Thread {} blocking on Mutex {}", *this, &lock);
259
260 for (;;) {
261 // Yield to the scheduler, and wait for us to resume unblocked.
262 VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
263 VERIFY(Processor::in_critical());
264 if (&lock != &big_lock && big_lock.is_exclusively_locked_by_current_thread()) {
265 // We're locking another lock and already hold the big lock...
266 // We need to release the big lock
267 yield_and_release_relock_big_lock();
268 } else {
269 // By the time we've reached this another thread might have
270 // marked us as holding the big lock, so this call must not
271 // verify that we're not holding it.
272 yield_without_releasing_big_lock(VerifyLockNotHeld::No);
273 }
274 VERIFY(Processor::in_critical());
275
276 SpinlockLocker block_lock2(m_block_lock);
277 VERIFY(!m_blocking_mutex);
278 m_blocking_mutex = previous_blocking_mutex;
279 break;
280 }
281
282 lock_lock.lock();
283}
284
285u32 Thread::unblock_from_mutex(Kernel::Mutex& mutex)
286{
287 SpinlockLocker scheduler_lock(g_scheduler_lock);
288 SpinlockLocker block_lock(m_block_lock);
289
290 VERIFY(!Processor::current_in_irq());
291 VERIFY(m_blocking_mutex == &mutex);
292
293 dbgln_if(THREAD_DEBUG, "Thread {} unblocked from Mutex {}", *this, &mutex);
294
295 auto requested_count = m_lock_requested_count;
296
297 m_blocking_mutex = nullptr;
298 if (Thread::current() == this) {
299 set_state(Thread::State::Running);
300 return requested_count;
301 }
302 VERIFY(m_state != Thread::State::Runnable && m_state != Thread::State::Running);
303 set_state(Thread::State::Runnable);
304 return requested_count;
305}
306
307void Thread::unblock_from_blocker(Blocker& blocker)
308{
309 auto do_unblock = [&]() {
310 SpinlockLocker scheduler_lock(g_scheduler_lock);
311 SpinlockLocker block_lock(m_block_lock);
312 if (m_blocker != &blocker)
313 return;
314 if (!should_be_stopped() && !is_stopped())
315 unblock();
316 };
317 if (Processor::current_in_irq() != 0) {
318 Processor::deferred_call_queue([do_unblock = move(do_unblock), self = try_make_weak_ptr().release_value_but_fixme_should_propagate_errors()]() {
319 if (auto this_thread = self.strong_ref())
320 do_unblock();
321 });
322 } else {
323 do_unblock();
324 }
325}
326
327void Thread::unblock(u8 signal)
328{
329 VERIFY(!Processor::current_in_irq());
330 VERIFY(g_scheduler_lock.is_locked_by_current_processor());
331 VERIFY(m_block_lock.is_locked_by_current_processor());
332 if (m_state != Thread::State::Blocked)
333 return;
334 if (m_blocking_mutex)
335 return;
336 VERIFY(m_blocker);
337 if (signal != 0) {
338 if (is_handling_page_fault()) {
339 // Don't let signals unblock threads that are blocked inside a page fault handler.
340 // This prevents threads from EINTR'ing the inode read in an inode page fault.
341 // FIXME: There's probably a better way to solve this.
342 return;
343 }
344 if (!m_blocker->can_be_interrupted() && !m_should_die)
345 return;
346 m_blocker->set_interrupted_by_signal(signal);
347 }
348 m_blocker = nullptr;
349 if (Thread::current() == this) {
350 set_state(Thread::State::Running);
351 return;
352 }
353 VERIFY(m_state != Thread::State::Runnable && m_state != Thread::State::Running);
354 set_state(Thread::State::Runnable);
355}
356
357void Thread::set_should_die()
358{
359 if (m_should_die) {
360 dbgln("{} Should already die", *this);
361 return;
362 }
363 ScopedCritical critical;
364
365 // Remember that we should die instead of returning to
366 // the userspace.
367 SpinlockLocker lock(g_scheduler_lock);
368 m_should_die = true;
369
370 // NOTE: Even the current thread can technically be in "Stopped"
371 // state! This is the case when another thread sent a SIGSTOP to
372 // it while it was running and it calls e.g. exit() before
373 // the scheduler gets involved again.
374 if (is_stopped()) {
375 // If we were stopped, we need to briefly resume so that
376 // the kernel stacks can clean up. We won't ever return back
377 // to user mode, though
378 VERIFY(!process().is_stopped());
379 resume_from_stopped();
380 }
381 if (is_blocked()) {
382 SpinlockLocker block_lock(m_block_lock);
383 if (m_blocker) {
384 // We're blocked in the kernel.
385 m_blocker->set_interrupted_by_death();
386 unblock();
387 }
388 }
389}
390
391void Thread::die_if_needed()
392{
393 VERIFY(Thread::current() == this);
394
395 if (!m_should_die)
396 return;
397
398 u32 unlock_count;
399 [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
400
401 dbgln_if(THREAD_DEBUG, "Thread {} is dying", *this);
402
403 {
404 SpinlockLocker lock(g_scheduler_lock);
405 // It's possible that we don't reach the code after this block if the
406 // scheduler is invoked and FinalizerTask cleans up this thread, however
407 // that doesn't matter because we're trying to invoke the scheduler anyway
408 set_state(Thread::State::Dying);
409 }
410
411 ScopedCritical critical;
412
413 // Flag a context switch. Because we're in a critical section,
414 // Scheduler::yield will actually only mark a pending context switch
415 // Simply leaving the critical section would not necessarily trigger
416 // a switch.
417 Scheduler::yield();
418
419 // Now leave the critical section so that we can also trigger the
420 // actual context switch
421 Processor::clear_critical();
422 dbgln("die_if_needed returned from clear_critical!!! in irq: {}", Processor::current_in_irq());
423 // We should never get here, but the scoped scheduler lock
424 // will be released by Scheduler::context_switch again
425 VERIFY_NOT_REACHED();
426}
427
428void Thread::exit(void* exit_value)
429{
430 VERIFY(Thread::current() == this);
431 m_join_blocker_set.thread_did_exit(exit_value);
432 set_should_die();
433 u32 unlock_count;
434 [[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
435 if (m_thread_specific_range.has_value()) {
436 process().address_space().with([&](auto& space) {
437 auto* region = space->find_region_from_range(m_thread_specific_range.value());
438 space->deallocate_region(*region);
439 });
440 }
441#ifdef ENABLE_KERNEL_COVERAGE_COLLECTION
442 KCOVDevice::free_thread();
443#endif
444 die_if_needed();
445}
446
447void Thread::yield_without_releasing_big_lock(VerifyLockNotHeld verify_lock_not_held)
448{
449 VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
450 VERIFY(verify_lock_not_held == VerifyLockNotHeld::No || !process().big_lock().is_exclusively_locked_by_current_thread());
451 // Disable interrupts here. This ensures we don't accidentally switch contexts twice
452 InterruptDisabler disable;
453 Scheduler::yield(); // flag a switch
454 u32 prev_critical = Processor::clear_critical();
455 // NOTE: We may be on a different CPU now!
456 Processor::restore_critical(prev_critical);
457}
458
459void Thread::yield_and_release_relock_big_lock()
460{
461 VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
462 // Disable interrupts here. This ensures we don't accidentally switch contexts twice
463 InterruptDisabler disable;
464 Scheduler::yield(); // flag a switch
465 u32 lock_count_to_restore = 0;
466 auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
467 // NOTE: Even though we call Scheduler::yield here, unless we happen
468 // to be outside of a critical section, the yield will be postponed
469 // until leaving it in relock_process.
470 relock_process(previous_locked, lock_count_to_restore);
471}
472
473LockMode Thread::unlock_process_if_locked(u32& lock_count_to_restore)
474{
475 return process().big_lock().force_unlock_exclusive_if_locked(lock_count_to_restore);
476}
477
478void Thread::relock_process(LockMode previous_locked, u32 lock_count_to_restore)
479{
480 // Clearing the critical section may trigger the context switch
481 // flagged by calling Scheduler::yield above.
482 // We have to do it this way because we intentionally
483 // leave the critical section here to be able to switch contexts.
484 u32 prev_critical = Processor::clear_critical();
485
486 // CONTEXT SWITCH HAPPENS HERE!
487
488 // NOTE: We may be on a different CPU now!
489 Processor::restore_critical(prev_critical);
490
491 if (previous_locked != LockMode::Unlocked) {
492 // We've unblocked, relock the process if needed and carry on.
493 process().big_lock().restore_exclusive_lock(lock_count_to_restore);
494 }
495}
496
497// NOLINTNEXTLINE(readability-make-member-function-const) False positive; We call block<SleepBlocker> which is not const
498auto Thread::sleep(clockid_t clock_id, Time const& duration, Time* remaining_time) -> BlockResult
499{
500 VERIFY(state() == Thread::State::Running);
501 return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(false, &duration, nullptr, clock_id), remaining_time);
502}
503
504// NOLINTNEXTLINE(readability-make-member-function-const) False positive; We call block<SleepBlocker> which is not const
505auto Thread::sleep_until(clockid_t clock_id, Time const& deadline) -> BlockResult
506{
507 VERIFY(state() == Thread::State::Running);
508 return Thread::current()->block<Thread::SleepBlocker>({}, Thread::BlockTimeout(true, &deadline, nullptr, clock_id));
509}
510
511StringView Thread::state_string() const
512{
513 switch (state()) {
514 case Thread::State::Invalid:
515 return "Invalid"sv;
516 case Thread::State::Runnable:
517 return "Runnable"sv;
518 case Thread::State::Running:
519 return "Running"sv;
520 case Thread::State::Dying:
521 return "Dying"sv;
522 case Thread::State::Dead:
523 return "Dead"sv;
524 case Thread::State::Stopped:
525 return "Stopped"sv;
526 case Thread::State::Blocked: {
527 SpinlockLocker block_lock(m_block_lock);
528 if (m_blocking_mutex)
529 return "Mutex"sv;
530 if (m_blocker)
531 return m_blocker->state_string();
532 VERIFY_NOT_REACHED();
533 }
534 }
535 PANIC("Thread::state_string(): Invalid state: {}", (int)state());
536}
537
538void Thread::finalize()
539{
540 VERIFY(Thread::current() == g_finalizer);
541 VERIFY(Thread::current() != this);
542
543#if LOCK_DEBUG
544 VERIFY(!m_lock.is_locked_by_current_processor());
545 if (lock_count() > 0) {
546 dbgln("Thread {} leaking {} Locks!", *this, lock_count());
547 SpinlockLocker list_lock(m_holding_locks_lock);
548 for (auto& info : m_holding_locks_list) {
549 auto const& location = info.lock_location;
550 dbgln(" - Mutex: \"{}\" @ {} locked in function \"{}\" at \"{}:{}\" with a count of: {}", info.lock->name(), info.lock, location.function_name(), location.filename(), location.line_number(), info.count);
551 }
552 VERIFY_NOT_REACHED();
553 }
554#endif
555
556 {
557 SpinlockLocker lock(g_scheduler_lock);
558 dbgln_if(THREAD_DEBUG, "Finalizing thread {}", *this);
559 set_state(Thread::State::Dead);
560 m_join_blocker_set.thread_finalizing();
561 }
562
563 if (m_dump_backtrace_on_finalization) {
564 auto trace_or_error = backtrace();
565 if (!trace_or_error.is_error()) {
566 auto trace = trace_or_error.release_value();
567 dbgln("Backtrace:");
568 kernelputstr(trace->characters(), trace->length());
569 }
570 }
571
572 drop_thread_count();
573}
574
575void Thread::drop_thread_count()
576{
577 bool is_last = process().remove_thread(*this);
578 if (is_last)
579 process().finalize();
580}
581
582void Thread::finalize_dying_threads()
583{
584 VERIFY(Thread::current() == g_finalizer);
585 Vector<Thread*, 32> dying_threads;
586 {
587 SpinlockLocker lock(g_scheduler_lock);
588 for_each_in_state(Thread::State::Dying, [&](Thread& thread) {
589 if (!thread.is_finalizable())
590 return;
591 auto result = dying_threads.try_append(&thread);
592 // We ignore allocation failures above the first 32 guaranteed thread slots, and
593 // just flag our future-selves to finalize these threads at a later point
594 if (result.is_error())
595 g_finalizer_has_work.store(true, AK::MemoryOrder::memory_order_release);
596 });
597 }
598 for (auto* thread : dying_threads) {
599 LockRefPtr<Process> process = thread->process();
600 dbgln_if(PROCESS_DEBUG, "Before finalization, {} has {} refs and its process has {}",
601 *thread, thread->ref_count(), thread->process().ref_count());
602 thread->finalize();
603 dbgln_if(PROCESS_DEBUG, "After finalization, {} has {} refs and its process has {}",
604 *thread, thread->ref_count(), thread->process().ref_count());
605 // This thread will never execute again, drop the running reference
606 // NOTE: This may not necessarily drop the last reference if anything
607 // else is still holding onto this thread!
608 thread->unref();
609 }
610}
611
612void Thread::update_time_scheduled(u64 current_scheduler_time, bool is_kernel, bool no_longer_running)
613{
614 if (m_last_time_scheduled.has_value()) {
615 u64 delta;
616 if (current_scheduler_time >= m_last_time_scheduled.value())
617 delta = current_scheduler_time - m_last_time_scheduled.value();
618 else
619 delta = m_last_time_scheduled.value() - current_scheduler_time; // the unlikely event that the clock wrapped
620 if (delta != 0) {
621 // Add it to the global total *before* updating the thread's value!
622 Scheduler::add_time_scheduled(delta, is_kernel);
623
624 auto& total_time = is_kernel ? m_total_time_scheduled_kernel : m_total_time_scheduled_user;
625 total_time.fetch_add(delta, AK::memory_order_relaxed);
626 }
627 }
628 if (no_longer_running)
629 m_last_time_scheduled = {};
630 else
631 m_last_time_scheduled = current_scheduler_time;
632}
633
634bool Thread::tick()
635{
636 if (previous_mode() == ExecutionMode::Kernel) {
637 ++m_process->m_ticks_in_kernel;
638 ++m_ticks_in_kernel;
639 } else {
640 ++m_process->m_ticks_in_user;
641 ++m_ticks_in_user;
642 }
643 --m_ticks_left;
644 return m_ticks_left != 0;
645}
646
647void Thread::check_dispatch_pending_signal()
648{
649 auto result = DispatchSignalResult::Continue;
650 {
651 SpinlockLocker scheduler_lock(g_scheduler_lock);
652 if (pending_signals_for_state() != 0) {
653 result = dispatch_one_pending_signal();
654 }
655 }
656
657 if (result == DispatchSignalResult::Yield) {
658 yield_without_releasing_big_lock();
659 }
660}
661
662u32 Thread::pending_signals() const
663{
664 SpinlockLocker lock(g_scheduler_lock);
665 return pending_signals_for_state();
666}
667
668u32 Thread::pending_signals_for_state() const
669{
670 VERIFY(g_scheduler_lock.is_locked_by_current_processor());
671 constexpr u32 stopped_signal_mask = (1 << (SIGCONT - 1)) | (1 << (SIGKILL - 1)) | (1 << (SIGTRAP - 1));
672 if (is_handling_page_fault())
673 return 0;
674 return m_state != State::Stopped ? m_pending_signals : m_pending_signals & stopped_signal_mask;
675}
676
677void Thread::send_signal(u8 signal, [[maybe_unused]] Process* sender)
678{
679 VERIFY(signal < NSIG);
680 VERIFY(process().is_user_process());
681 SpinlockLocker scheduler_lock(g_scheduler_lock);
682
683 // FIXME: Figure out what to do for masked signals. Should we also ignore them here?
684 if (should_ignore_signal(signal)) {
685 dbgln_if(SIGNAL_DEBUG, "Signal {} was ignored by {}", signal, process());
686 return;
687 }
688
689 if constexpr (SIGNAL_DEBUG) {
690 if (sender)
691 dbgln("Signal: {} sent {} to {}", *sender, signal, process());
692 else
693 dbgln("Signal: Kernel send {} to {}", signal, process());
694 }
695
696 m_pending_signals |= 1 << (signal - 1);
697 m_signal_senders[signal] = sender ? sender->pid() : pid();
698 m_have_any_unmasked_pending_signals.store((pending_signals_for_state() & ~m_signal_mask) != 0, AK::memory_order_release);
699 m_signal_blocker_set.unblock_all_blockers_whose_conditions_are_met();
700
701 if (!has_unmasked_pending_signals())
702 return;
703
704 if (m_state == Thread::State::Stopped) {
705 if (pending_signals_for_state() != 0) {
706 dbgln_if(SIGNAL_DEBUG, "Signal: Resuming stopped {} to deliver signal {}", *this, signal);
707 resume_from_stopped();
708 }
709 } else {
710 SpinlockLocker block_lock(m_block_lock);
711 dbgln_if(SIGNAL_DEBUG, "Signal: Unblocking {} to deliver signal {}", *this, signal);
712 unblock(signal);
713 }
714}
715
716u32 Thread::update_signal_mask(u32 signal_mask)
717{
718 SpinlockLocker lock(g_scheduler_lock);
719 auto previous_signal_mask = m_signal_mask;
720 m_signal_mask = signal_mask;
721 m_have_any_unmasked_pending_signals.store((pending_signals_for_state() & ~m_signal_mask) != 0, AK::memory_order_release);
722 return previous_signal_mask;
723}
724
725u32 Thread::signal_mask() const
726{
727 SpinlockLocker lock(g_scheduler_lock);
728 return m_signal_mask;
729}
730
731u32 Thread::signal_mask_block(sigset_t signal_set, bool block)
732{
733 SpinlockLocker lock(g_scheduler_lock);
734 auto previous_signal_mask = m_signal_mask;
735 if (block)
736 m_signal_mask |= signal_set;
737 else
738 m_signal_mask &= ~signal_set;
739 m_have_any_unmasked_pending_signals.store((pending_signals_for_state() & ~m_signal_mask) != 0, AK::memory_order_release);
740 return previous_signal_mask;
741}
742
743void Thread::reset_signals_for_exec()
744{
745 SpinlockLocker lock(g_scheduler_lock);
746 // The signal mask is preserved across execve(2).
747 // The pending signal set is preserved across an execve(2).
748 m_have_any_unmasked_pending_signals.store(false, AK::memory_order_release);
749 m_signal_action_masks.fill({});
750 // A successful call to execve(2) removes any existing alternate signal stack
751 m_alternative_signal_stack = 0;
752 m_alternative_signal_stack_size = 0;
753}
754
755// Certain exceptions, such as SIGSEGV and SIGILL, put a
756// thread into a state where the signal handler must be
757// invoked immediately, otherwise it will continue to fault.
758// This function should be used in an exception handler to
759// ensure that when the thread resumes, it's executing in
760// the appropriate signal handler.
761void Thread::send_urgent_signal_to_self(u8 signal)
762{
763 VERIFY(Thread::current() == this);
764 DispatchSignalResult result;
765 {
766 SpinlockLocker lock(g_scheduler_lock);
767 result = dispatch_signal(signal);
768 }
769 if (result == DispatchSignalResult::Terminate) {
770 Thread::current()->die_if_needed();
771 VERIFY_NOT_REACHED(); // dispatch_signal will request termination of the thread, so the above call should never return
772 }
773 if (result == DispatchSignalResult::Yield)
774 yield_and_release_relock_big_lock();
775}
776
777DispatchSignalResult Thread::dispatch_one_pending_signal()
778{
779 VERIFY(g_scheduler_lock.is_locked_by_current_processor());
780 u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
781 if (signal_candidates == 0)
782 return DispatchSignalResult::Continue;
783
784 u8 signal = 1;
785 for (; signal < NSIG; ++signal) {
786 if ((signal_candidates & (1 << (signal - 1))) != 0) {
787 break;
788 }
789 }
790 return dispatch_signal(signal);
791}
792
793DispatchSignalResult Thread::try_dispatch_one_pending_signal(u8 signal)
794{
795 VERIFY(signal != 0);
796 SpinlockLocker scheduler_lock(g_scheduler_lock);
797 u32 signal_candidates = pending_signals_for_state() & ~m_signal_mask;
798 if ((signal_candidates & (1 << (signal - 1))) == 0)
799 return DispatchSignalResult::Continue;
800 return dispatch_signal(signal);
801}
802
803enum class DefaultSignalAction {
804 Terminate,
805 Ignore,
806 DumpCore,
807 Stop,
808 Continue,
809};
810
811static DefaultSignalAction default_signal_action(u8 signal)
812{
813 VERIFY(signal && signal < NSIG);
814
815 switch (signal) {
816 case SIGHUP:
817 case SIGINT:
818 case SIGKILL:
819 case SIGPIPE:
820 case SIGALRM:
821 case SIGUSR1:
822 case SIGUSR2:
823 case SIGVTALRM:
824 case SIGSTKFLT:
825 case SIGIO:
826 case SIGPROF:
827 case SIGTERM:
828 case SIGCANCEL:
829 return DefaultSignalAction::Terminate;
830 case SIGCHLD:
831 case SIGURG:
832 case SIGWINCH:
833 case SIGINFO:
834 return DefaultSignalAction::Ignore;
835 case SIGQUIT:
836 case SIGILL:
837 case SIGTRAP:
838 case SIGABRT:
839 case SIGBUS:
840 case SIGFPE:
841 case SIGSEGV:
842 case SIGXCPU:
843 case SIGXFSZ:
844 case SIGSYS:
845 return DefaultSignalAction::DumpCore;
846 case SIGCONT:
847 return DefaultSignalAction::Continue;
848 case SIGSTOP:
849 case SIGTSTP:
850 case SIGTTIN:
851 case SIGTTOU:
852 return DefaultSignalAction::Stop;
853 default:
854 VERIFY_NOT_REACHED();
855 }
856}
857
858bool Thread::should_ignore_signal(u8 signal) const
859{
860 VERIFY(signal < NSIG);
861 auto const& action = m_process->m_signal_action_data[signal];
862 if (action.handler_or_sigaction.is_null())
863 return default_signal_action(signal) == DefaultSignalAction::Ignore;
864 return ((sighandler_t)action.handler_or_sigaction.get() == SIG_IGN);
865}
866
867bool Thread::has_signal_handler(u8 signal) const
868{
869 VERIFY(signal < NSIG);
870 auto const& action = m_process->m_signal_action_data[signal];
871 return !action.handler_or_sigaction.is_null();
872}
873
874bool Thread::is_signal_masked(u8 signal) const
875{
876 VERIFY(signal < NSIG);
877 return (1 << (signal - 1)) & m_signal_mask;
878}
879
880bool Thread::has_alternative_signal_stack() const
881{
882 return m_alternative_signal_stack_size != 0;
883}
884
885bool Thread::is_in_alternative_signal_stack() const
886{
887 auto sp = get_register_dump_from_stack().userspace_sp();
888 return sp >= m_alternative_signal_stack && sp < m_alternative_signal_stack + m_alternative_signal_stack_size;
889}
890
891static ErrorOr<void> push_value_on_user_stack(FlatPtr& stack, FlatPtr data)
892{
893 stack -= sizeof(FlatPtr);
894 return copy_to_user((FlatPtr*)stack, &data);
895}
896
897template<typename T>
898static ErrorOr<void> copy_value_on_user_stack(FlatPtr& stack, T const& data)
899{
900 stack -= sizeof(data);
901 return copy_to_user((RemoveCVReference<T>*)stack, &data);
902}
903
904void Thread::resume_from_stopped()
905{
906 VERIFY(is_stopped());
907 VERIFY(m_stop_state != State::Invalid);
908 VERIFY(g_scheduler_lock.is_locked_by_current_processor());
909 if (m_stop_state == Thread::State::Blocked) {
910 SpinlockLocker block_lock(m_block_lock);
911 if (m_blocker || m_blocking_mutex) {
912 // Hasn't been unblocked yet
913 set_state(Thread::State::Blocked, 0);
914 } else {
915 // Was unblocked while stopped
916 set_state(Thread::State::Runnable);
917 }
918 } else {
919 set_state(m_stop_state, 0);
920 }
921}
922
923DispatchSignalResult Thread::dispatch_signal(u8 signal)
924{
925 VERIFY_INTERRUPTS_DISABLED();
926 VERIFY(g_scheduler_lock.is_locked_by_current_processor());
927 VERIFY(signal > 0 && signal <= NSIG);
928 VERIFY(process().is_user_process());
929 VERIFY(this == Thread::current());
930
931 dbgln_if(SIGNAL_DEBUG, "Dispatch signal {} to {}, state: {}", signal, *this, state_string());
932
933 if (m_state == Thread::State::Invalid || !is_initialized()) {
934 // Thread has barely been created, we need to wait until it is
935 // at least in Runnable state and is_initialized() returns true,
936 // which indicates that it is fully set up an we actually have
937 // a register state on the stack that we can modify
938 return DispatchSignalResult::Deferred;
939 }
940
941 auto& action = m_process->m_signal_action_data[signal];
942 auto sender_pid = m_signal_senders[signal];
943 auto sender = Process::from_pid_ignoring_jails(sender_pid);
944
945 if (!current_trap() && !action.handler_or_sigaction.is_null()) {
946 // We're trying dispatch a handled signal to a user process that was scheduled
947 // after a yielding/blocking kernel thread, we don't have a register capture of
948 // the thread, so just defer processing the signal to later.
949 return DispatchSignalResult::Deferred;
950 }
951
952 // Mark this signal as handled.
953 m_pending_signals &= ~(1 << (signal - 1));
954 m_have_any_unmasked_pending_signals.store((m_pending_signals & ~m_signal_mask) != 0, AK::memory_order_release);
955
956 auto& process = this->process();
957 auto* tracer = process.tracer();
958 if (signal == SIGSTOP || (tracer && default_signal_action(signal) == DefaultSignalAction::DumpCore)) {
959 dbgln_if(SIGNAL_DEBUG, "Signal {} stopping this thread", signal);
960 if (tracer)
961 tracer->set_regs(get_register_dump_from_stack());
962 set_state(Thread::State::Stopped, signal);
963 return DispatchSignalResult::Yield;
964 }
965
966 if (signal == SIGCONT) {
967 dbgln("signal: SIGCONT resuming {}", *this);
968 } else {
969 if (tracer) {
970 // when a thread is traced, it should be stopped whenever it receives a signal
971 // the tracer is notified of this by using waitpid()
972 // only "pending signals" from the tracer are sent to the tracee
973 if (!tracer->has_pending_signal(signal)) {
974 dbgln("signal: {} stopping {} for tracer", signal, *this);
975 set_state(Thread::State::Stopped, signal);
976 return DispatchSignalResult::Yield;
977 }
978 tracer->unset_signal(signal);
979 }
980 }
981
982 auto handler_vaddr = action.handler_or_sigaction;
983 if (handler_vaddr.is_null()) {
984 switch (default_signal_action(signal)) {
985 case DefaultSignalAction::Stop:
986 set_state(Thread::State::Stopped, signal);
987 return DispatchSignalResult::Yield;
988 case DefaultSignalAction::DumpCore:
989 process.set_should_generate_coredump(true);
990 process.for_each_thread([](auto& thread) {
991 thread.set_dump_backtrace_on_finalization();
992 });
993 [[fallthrough]];
994 case DefaultSignalAction::Terminate:
995 m_process->terminate_due_to_signal(signal);
996 return DispatchSignalResult::Terminate;
997 case DefaultSignalAction::Ignore:
998 VERIFY_NOT_REACHED();
999 case DefaultSignalAction::Continue:
1000 return DispatchSignalResult::Continue;
1001 }
1002 VERIFY_NOT_REACHED();
1003 }
1004
1005 if ((sighandler_t)handler_vaddr.as_ptr() == SIG_IGN) {
1006 dbgln_if(SIGNAL_DEBUG, "Ignored signal {}", signal);
1007 return DispatchSignalResult::Continue;
1008 }
1009
1010 ScopedAddressSpaceSwitcher switcher(m_process);
1011
1012 m_currently_handled_signal = signal;
1013
1014 u32 old_signal_mask = m_signal_mask;
1015 u32 new_signal_mask = m_signal_action_masks[signal].value_or(action.mask);
1016 if ((action.flags & SA_NODEFER) == SA_NODEFER)
1017 new_signal_mask &= ~(1 << (signal - 1));
1018 else
1019 new_signal_mask |= 1 << (signal - 1);
1020
1021 m_signal_mask |= new_signal_mask;
1022 m_have_any_unmasked_pending_signals.store((m_pending_signals & ~m_signal_mask) != 0, AK::memory_order_release);
1023
1024 bool use_alternative_stack = ((action.flags & SA_ONSTACK) != 0) && has_alternative_signal_stack() && !is_in_alternative_signal_stack();
1025
1026 auto setup_stack = [&](RegisterState& state) -> ErrorOr<void> {
1027 FlatPtr stack;
1028 if (use_alternative_stack)
1029 stack = m_alternative_signal_stack + m_alternative_signal_stack_size;
1030 else
1031 stack = state.userspace_sp();
1032
1033 dbgln_if(SIGNAL_DEBUG, "Setting up user stack to return to IP {:p}, SP {:p}", state.ip(), state.userspace_sp());
1034
1035 __ucontext ucontext {
1036 .uc_link = nullptr,
1037 .uc_sigmask = old_signal_mask,
1038 .uc_stack = {
1039 .ss_sp = bit_cast<void*>(stack),
1040 .ss_flags = action.flags & SA_ONSTACK,
1041 .ss_size = use_alternative_stack ? m_alternative_signal_stack_size : 0,
1042 },
1043 .uc_mcontext = {},
1044 };
1045 copy_kernel_registers_into_ptrace_registers(static_cast<PtraceRegisters&>(ucontext.uc_mcontext), state);
1046
1047 auto fill_signal_info_for_signal = [&](siginfo& signal_info) {
1048 if (signal == SIGCHLD) {
1049 if (!sender) {
1050 signal_info.si_code = CLD_EXITED;
1051 return;
1052 }
1053 auto const* thread = sender->thread_list().with([](auto& list) { return list.is_empty() ? nullptr : list.first(); });
1054 if (!thread) {
1055 signal_info.si_code = CLD_EXITED;
1056 return;
1057 }
1058
1059 switch (thread->m_state) {
1060 case State::Dead:
1061 if (sender->should_generate_coredump() && sender->is_dumpable()) {
1062 signal_info.si_code = CLD_DUMPED;
1063 signal_info.si_status = sender->termination_signal();
1064 return;
1065 }
1066 [[fallthrough]];
1067 case State::Dying:
1068 if (sender->termination_signal() == 0) {
1069 signal_info.si_code = CLD_EXITED;
1070 signal_info.si_status = sender->termination_status();
1071 return;
1072 }
1073 signal_info.si_code = CLD_KILLED;
1074 signal_info.si_status = sender->termination_signal();
1075 return;
1076 case State::Runnable:
1077 case State::Running:
1078 case State::Blocked:
1079 signal_info.si_code = CLD_CONTINUED;
1080 return;
1081 case State::Stopped:
1082 signal_info.si_code = CLD_STOPPED;
1083 return;
1084 case State::Invalid:
1085 // Something is wrong, but we're just an observer.
1086 break;
1087 }
1088 }
1089
1090 signal_info.si_code = SI_NOINFO;
1091 };
1092
1093 siginfo signal_info {
1094 .si_signo = signal,
1095 // Filled in below by fill_signal_info_for_signal.
1096 .si_code = 0,
1097 // Set for SI_TIMER, we don't have the data here.
1098 .si_errno = 0,
1099 .si_pid = sender_pid.value(),
1100 .si_uid = sender ? sender->credentials()->uid().value() : 0,
1101 // Set for SIGILL, SIGFPE, SIGSEGV and SIGBUS
1102 // FIXME: We don't generate these signals in a way that can be handled.
1103 .si_addr = 0,
1104 // Set for SIGCHLD.
1105 .si_status = 0,
1106 // Set for SIGPOLL, we don't have SIGPOLL.
1107 .si_band = 0,
1108 // Set for SI_QUEUE, SI_TIMER, SI_ASYNCIO and SI_MESGQ
1109 // We do not generate any of these.
1110 .si_value = {
1111 .sival_int = 0,
1112 },
1113 };
1114
1115 if (action.flags & SA_SIGINFO)
1116 fill_signal_info_for_signal(signal_info);
1117
1118#if ARCH(X86_64)
1119 constexpr static FlatPtr thread_red_zone_size = 128;
1120#elif ARCH(AARCH64)
1121 constexpr static FlatPtr thread_red_zone_size = 0; // FIXME
1122 TODO_AARCH64();
1123#else
1124# error Unknown architecture in dispatch_signal
1125#endif
1126
1127 // Align the stack to 16 bytes.
1128 // Note that we push some elements on to the stack before the return address,
1129 // so we need to account for this here.
1130 constexpr static FlatPtr elements_pushed_on_stack_before_handler_address = 1; // one slot for a saved register
1131 FlatPtr const extra_bytes_pushed_on_stack_before_handler_address = sizeof(ucontext) + sizeof(signal_info);
1132 FlatPtr stack_alignment = (stack - elements_pushed_on_stack_before_handler_address * sizeof(FlatPtr) + extra_bytes_pushed_on_stack_before_handler_address) % 16;
1133 // Also note that we have to skip the thread red-zone (if needed), so do that here.
1134 stack -= thread_red_zone_size + stack_alignment;
1135 auto start_of_stack = stack;
1136
1137 TRY(push_value_on_user_stack(stack, 0)); // syscall return value slot
1138
1139 TRY(copy_value_on_user_stack(stack, ucontext));
1140 auto pointer_to_ucontext = stack;
1141
1142 TRY(copy_value_on_user_stack(stack, signal_info));
1143 auto pointer_to_signal_info = stack;
1144
1145 // Make sure we actually pushed as many elements as we claimed to have pushed.
1146 if (start_of_stack - stack != elements_pushed_on_stack_before_handler_address * sizeof(FlatPtr) + extra_bytes_pushed_on_stack_before_handler_address) {
1147 PANIC("Stack in invalid state after signal trampoline, expected {:x} but got {:x}",
1148 start_of_stack - elements_pushed_on_stack_before_handler_address * sizeof(FlatPtr) - extra_bytes_pushed_on_stack_before_handler_address, stack);
1149 }
1150
1151 VERIFY(stack % 16 == 0);
1152
1153#if ARCH(X86_64)
1154 // Save the FPU/SSE state
1155 TRY(copy_value_on_user_stack(stack, fpu_state()));
1156#endif
1157
1158 TRY(push_value_on_user_stack(stack, pointer_to_ucontext));
1159 TRY(push_value_on_user_stack(stack, pointer_to_signal_info));
1160 TRY(push_value_on_user_stack(stack, signal));
1161
1162 TRY(push_value_on_user_stack(stack, handler_vaddr.get()));
1163
1164 // We write back the adjusted stack value into the register state.
1165 // We have to do this because we can't just pass around a reference to a packed field, as it's UB.
1166 state.set_userspace_sp(stack);
1167
1168 return {};
1169 };
1170
1171 // We now place the thread state on the userspace stack.
1172 // Note that we use a RegisterState.
1173 // Conversely, when the thread isn't blocking the RegisterState may not be
1174 // valid (fork, exec etc) but the tss will, so we use that instead.
1175 auto& regs = get_register_dump_from_stack();
1176
1177 auto result = setup_stack(regs);
1178 if (result.is_error()) {
1179 dbgln("Invalid stack pointer: {}", regs.userspace_sp());
1180 process.set_should_generate_coredump(true);
1181 process.for_each_thread([](auto& thread) {
1182 thread.set_dump_backtrace_on_finalization();
1183 });
1184 m_process->terminate_due_to_signal(signal);
1185 return DispatchSignalResult::Terminate;
1186 }
1187
1188 auto signal_trampoline_addr = process.signal_trampoline().get();
1189 regs.set_ip(signal_trampoline_addr);
1190
1191#if ARCH(X86_64)
1192 // Userspace flags might be invalid for function entry, according to SYSV ABI (section 3.2.1).
1193 // Set them to a known-good value to avoid weird handler misbehavior.
1194 // Only IF (and the reserved bit 1) are set.
1195 regs.set_flags(2 | (regs.rflags & ~safe_eflags_mask));
1196#endif
1197
1198 dbgln_if(SIGNAL_DEBUG, "Thread in state '{}' has been primed with signal handler {:p} to deliver {}", state_string(), m_regs.ip(), signal);
1199
1200 return DispatchSignalResult::Continue;
1201}
1202
1203RegisterState& Thread::get_register_dump_from_stack()
1204{
1205 auto* trap = current_trap();
1206
1207 // We should *always* have a trap. If we don't we're probably a kernel
1208 // thread that hasn't been preempted. If we want to support this, we
1209 // need to capture the registers probably into m_regs and return it
1210 VERIFY(trap);
1211
1212 while (trap) {
1213 if (!trap->next_trap)
1214 break;
1215 trap = trap->next_trap;
1216 }
1217 return *trap->regs;
1218}
1219
1220ErrorOr<NonnullLockRefPtr<Thread>> Thread::try_clone(Process& process)
1221{
1222 auto clone = TRY(Thread::try_create(process));
1223 m_signal_action_masks.span().copy_to(clone->m_signal_action_masks);
1224 clone->m_signal_mask = m_signal_mask;
1225 clone->m_fpu_state = m_fpu_state;
1226 clone->m_thread_specific_data = m_thread_specific_data;
1227 return clone;
1228}
1229
1230void Thread::set_state(State new_state, u8 stop_signal)
1231{
1232 State previous_state;
1233 VERIFY(g_scheduler_lock.is_locked_by_current_processor());
1234 if (new_state == m_state)
1235 return;
1236
1237 {
1238 previous_state = m_state;
1239 if (previous_state == Thread::State::Invalid) {
1240 // If we were *just* created, we may have already pending signals
1241 if (has_unmasked_pending_signals()) {
1242 dbgln_if(THREAD_DEBUG, "Dispatch pending signals to new thread {}", *this);
1243 dispatch_one_pending_signal();
1244 }
1245 }
1246
1247 m_state = new_state;
1248 dbgln_if(THREAD_DEBUG, "Set thread {} state to {}", *this, state_string());
1249 }
1250
1251 if (previous_state == Thread::State::Runnable) {
1252 Scheduler::dequeue_runnable_thread(*this);
1253 } else if (previous_state == Thread::State::Stopped) {
1254 m_stop_state = State::Invalid;
1255 auto& process = this->process();
1256 if (process.set_stopped(false)) {
1257 process.for_each_thread([&](auto& thread) {
1258 if (&thread == this)
1259 return;
1260 if (!thread.is_stopped())
1261 return;
1262 dbgln_if(THREAD_DEBUG, "Resuming peer thread {}", thread);
1263 thread.resume_from_stopped();
1264 });
1265 process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Continued);
1266 // Tell the parent process (if any) about this change.
1267 if (auto parent = Process::from_pid_ignoring_jails(process.ppid())) {
1268 [[maybe_unused]] auto result = parent->send_signal(SIGCHLD, &process);
1269 }
1270 }
1271 }
1272
1273 if (m_state == Thread::State::Runnable) {
1274 Scheduler::enqueue_runnable_thread(*this);
1275 Processor::smp_wake_n_idle_processors(1);
1276 } else if (m_state == Thread::State::Stopped) {
1277 // We don't want to restore to Running state, only Runnable!
1278 m_stop_state = previous_state != Thread::State::Running ? previous_state : Thread::State::Runnable;
1279 auto& process = this->process();
1280 if (!process.set_stopped(true)) {
1281 process.for_each_thread([&](auto& thread) {
1282 if (&thread == this)
1283 return;
1284 if (thread.is_stopped())
1285 return;
1286 dbgln_if(THREAD_DEBUG, "Stopping peer thread {}", thread);
1287 thread.set_state(Thread::State::Stopped, stop_signal);
1288 });
1289 process.unblock_waiters(Thread::WaitBlocker::UnblockFlags::Stopped, stop_signal);
1290 // Tell the parent process (if any) about this change.
1291 if (auto parent = Process::from_pid_ignoring_jails(process.ppid())) {
1292 [[maybe_unused]] auto result = parent->send_signal(SIGCHLD, &process);
1293 }
1294 }
1295 } else if (m_state == Thread::State::Dying) {
1296 VERIFY(previous_state != Thread::State::Blocked);
1297 if (this != Thread::current() && is_finalizable()) {
1298 // Some other thread set this thread to Dying, notify the
1299 // finalizer right away as it can be cleaned up now
1300 Scheduler::notify_finalizer();
1301 }
1302 }
1303}
1304
1305struct RecognizedSymbol {
1306 FlatPtr address;
1307 KernelSymbol const* symbol { nullptr };
1308};
1309
1310static ErrorOr<bool> symbolicate(RecognizedSymbol const& symbol, Process& process, StringBuilder& builder)
1311{
1312 if (symbol.address == 0)
1313 return false;
1314
1315 auto credentials = process.credentials();
1316 bool mask_kernel_addresses = !credentials->is_superuser();
1317 if (!symbol.symbol) {
1318 if (!Memory::is_user_address(VirtualAddress(symbol.address))) {
1319 TRY(builder.try_append("0xdeadc0de\n"sv));
1320 } else {
1321 TRY(process.address_space().with([&](auto& space) -> ErrorOr<void> {
1322 if (auto* region = space->find_region_containing({ VirtualAddress(symbol.address), sizeof(FlatPtr) })) {
1323 size_t offset = symbol.address - region->vaddr().get();
1324 if (auto region_name = region->name(); !region_name.is_null() && !region_name.is_empty())
1325 TRY(builder.try_appendff("{:p} {} + {:#x}\n", (void*)symbol.address, region_name, offset));
1326 else
1327 TRY(builder.try_appendff("{:p} {:p} + {:#x}\n", (void*)symbol.address, region->vaddr().as_ptr(), offset));
1328 } else {
1329 TRY(builder.try_appendff("{:p}\n", symbol.address));
1330 }
1331 return {};
1332 }));
1333 }
1334 return true;
1335 }
1336 unsigned offset = symbol.address - symbol.symbol->address;
1337 if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
1338 TRY(builder.try_appendff("{:p}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address)));
1339 else
1340 TRY(builder.try_appendff("{:p} {} + {:#x}\n", (void*)(mask_kernel_addresses ? 0xdeadc0de : symbol.address), symbol.symbol->name, offset));
1341 return true;
1342}
1343
1344ErrorOr<NonnullOwnPtr<KString>> Thread::backtrace()
1345{
1346 Vector<RecognizedSymbol, 128> recognized_symbols;
1347
1348 auto& process = const_cast<Process&>(this->process());
1349 auto stack_trace = TRY(Processor::capture_stack_trace(*this));
1350 VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
1351 ScopedAddressSpaceSwitcher switcher(process);
1352 for (auto& frame : stack_trace) {
1353 if (Memory::is_user_range(VirtualAddress(frame), sizeof(FlatPtr) * 2)) {
1354 TRY(recognized_symbols.try_append({ frame }));
1355 } else {
1356 TRY(recognized_symbols.try_append({ frame, symbolicate_kernel_address(frame) }));
1357 }
1358 }
1359
1360 StringBuilder builder;
1361 for (auto& symbol : recognized_symbols) {
1362 if (!TRY(symbolicate(symbol, process, builder)))
1363 break;
1364 }
1365 return KString::try_create(builder.string_view());
1366}
1367
1368size_t Thread::thread_specific_region_alignment() const
1369{
1370 return max(process().m_master_tls_alignment, alignof(ThreadSpecificData));
1371}
1372
1373size_t Thread::thread_specific_region_size() const
1374{
1375 return align_up_to(process().m_master_tls_size, thread_specific_region_alignment()) + sizeof(ThreadSpecificData);
1376}
1377
1378ErrorOr<void> Thread::make_thread_specific_region(Badge<Process>)
1379{
1380 // The process may not require a TLS region, or allocate TLS later with sys$allocate_tls (which is what dynamically loaded programs do)
1381 if (!process().m_master_tls_region)
1382 return {};
1383
1384 return process().address_space().with([&](auto& space) -> ErrorOr<void> {
1385 auto* region = TRY(space->allocate_region(Memory::RandomizeVirtualAddress::Yes, {}, thread_specific_region_size(), PAGE_SIZE, "Thread-specific"sv, PROT_READ | PROT_WRITE));
1386
1387 m_thread_specific_range = region->range();
1388
1389 SmapDisabler disabler;
1390 auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment())).as_ptr();
1391 auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);
1392 m_thread_specific_data = VirtualAddress(thread_specific_data);
1393 thread_specific_data->self = thread_specific_data;
1394
1395 if (process().m_master_tls_size != 0)
1396 memcpy(thread_local_storage, process().m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), process().m_master_tls_size);
1397
1398 return {};
1399 });
1400}
1401
1402LockRefPtr<Thread> Thread::from_tid(ThreadID tid)
1403{
1404 return Thread::all_instances().with([&](auto& list) -> LockRefPtr<Thread> {
1405 for (Thread& thread : list) {
1406 if (thread.tid() == tid)
1407 return thread;
1408 }
1409 return nullptr;
1410 });
1411}
1412
1413void Thread::reset_fpu_state()
1414{
1415 memcpy(&m_fpu_state, &Processor::clean_fpu_state(), sizeof(FPUState));
1416}
1417
1418bool Thread::should_be_stopped() const
1419{
1420 return process().is_stopped();
1421}
1422
1423void Thread::track_lock_acquire(LockRank rank)
1424{
1425 // Nothing to do for locks without a rank.
1426 if (rank == LockRank::None)
1427 return;
1428
1429 if (m_lock_rank_mask != LockRank::None) {
1430 // Verify we are only attempting to take a lock of a higher rank.
1431 VERIFY(m_lock_rank_mask > rank);
1432 }
1433
1434 m_lock_rank_mask |= rank;
1435}
1436
1437void Thread::track_lock_release(LockRank rank)
1438{
1439 // Nothing to do for locks without a rank.
1440 if (rank == LockRank::None)
1441 return;
1442
1443 // The rank value from the caller should only contain a single bit, otherwise
1444 // we are disabling the tracking for multiple locks at once which will corrupt
1445 // the lock tracking mask, and we will assert somewhere else.
1446 auto rank_is_a_single_bit = [](auto rank_enum) -> bool {
1447 auto rank = to_underlying(rank_enum);
1448 auto rank_without_least_significant_bit = rank - 1;
1449 return (rank & rank_without_least_significant_bit) == 0;
1450 };
1451
1452 // We can't release locks out of order, as that would violate the ranking.
1453 // This is validated by toggling the least significant bit of the mask, and
1454 // then bit wise or-ing the rank we are trying to release with the resulting
1455 // mask. If the rank we are releasing is truly the highest rank then the mask
1456 // we get back will be equal to the current mask stored on the thread.
1457 auto rank_is_in_order = [](auto mask_enum, auto rank_enum) -> bool {
1458 auto mask = to_underlying(mask_enum);
1459 auto rank = to_underlying(rank_enum);
1460 auto mask_without_least_significant_bit = mask - 1;
1461 return ((mask & mask_without_least_significant_bit) | rank) == mask;
1462 };
1463
1464 VERIFY(has_flag(m_lock_rank_mask, rank));
1465 VERIFY(rank_is_a_single_bit(rank));
1466 VERIFY(rank_is_in_order(m_lock_rank_mask, rank));
1467
1468 m_lock_rank_mask ^= rank;
1469}
1470
1471void Thread::set_name(NonnullOwnPtr<KString> name)
1472{
1473 m_name.with([&](auto& this_name) {
1474 this_name = move(name);
1475 });
1476}
1477
1478}
1479
1480ErrorOr<void> AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, Kernel::Thread const& value)
1481{
1482 return value.process().name().with([&](auto& process_name) {
1483 return AK::Formatter<FormatString>::format(
1484 builder,
1485 "{}({}:{})"sv, process_name->view(), value.pid().value(), value.tid().value());
1486 });
1487}