Scheduler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <AK/Time.h>
  10. #include <Kernel/Debug.h>
  11. #include <Kernel/Panic.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/RTC.h>
  14. #include <Kernel/Scheduler.h>
  15. #include <Kernel/Time/TimeManagement.h>
  16. #include <Kernel/TimerQueue.h>
  17. // Remove this once SMP is stable and can be enabled by default
  18. #define SCHEDULE_ON_ALL_PROCESSORS 0
  19. namespace Kernel {
  20. class SchedulerPerProcessorData {
  21. AK_MAKE_NONCOPYABLE(SchedulerPerProcessorData);
  22. AK_MAKE_NONMOVABLE(SchedulerPerProcessorData);
  23. public:
  24. SchedulerPerProcessorData() = default;
  25. WeakPtr<Thread> m_pending_beneficiary;
  26. const char* m_pending_donate_reason { nullptr };
  27. bool m_in_scheduler { true };
  28. };
  29. RecursiveSpinLock g_scheduler_lock;
  30. static u32 time_slice_for(const Thread& thread)
  31. {
  32. // One time slice unit == 4ms (assuming 250 ticks/second)
  33. if (thread.is_idle_thread())
  34. return 1;
  35. return 2;
  36. }
  37. READONLY_AFTER_INIT Thread* g_finalizer;
  38. READONLY_AFTER_INIT WaitQueue* g_finalizer_wait_queue;
  39. Atomic<bool> g_finalizer_has_work { false };
  40. READONLY_AFTER_INIT static Process* s_colonel_process;
  41. struct ThreadReadyQueue {
  42. IntrusiveList<Thread, RawPtr<Thread>, &Thread::m_ready_queue_node> thread_list;
  43. };
  44. static SpinLock<u8> g_ready_queues_lock;
  45. static u32 g_ready_queues_mask;
  46. static constexpr u32 g_ready_queue_buckets = sizeof(g_ready_queues_mask) * 8;
  47. READONLY_AFTER_INIT static ThreadReadyQueue* g_ready_queues; // g_ready_queue_buckets entries
  48. static void dump_thread_list();
  49. static inline u32 thread_priority_to_priority_index(u32 thread_priority)
  50. {
  51. // Converts the priority in the range of THREAD_PRIORITY_MIN...THREAD_PRIORITY_MAX
  52. // to a index into g_ready_queues where 0 is the highest priority bucket
  53. VERIFY(thread_priority >= THREAD_PRIORITY_MIN && thread_priority <= THREAD_PRIORITY_MAX);
  54. constexpr u32 thread_priority_count = THREAD_PRIORITY_MAX - THREAD_PRIORITY_MIN + 1;
  55. static_assert(thread_priority_count > 0);
  56. auto priority_bucket = ((thread_priority_count - (thread_priority - THREAD_PRIORITY_MIN)) / thread_priority_count) * (g_ready_queue_buckets - 1);
  57. VERIFY(priority_bucket < g_ready_queue_buckets);
  58. return priority_bucket;
  59. }
  60. Thread& Scheduler::pull_next_runnable_thread()
  61. {
  62. auto affinity_mask = 1u << Processor::current().id();
  63. ScopedSpinLock lock(g_ready_queues_lock);
  64. auto priority_mask = g_ready_queues_mask;
  65. while (priority_mask != 0) {
  66. auto priority = __builtin_ffsl(priority_mask);
  67. VERIFY(priority > 0);
  68. auto& ready_queue = g_ready_queues[--priority];
  69. for (auto& thread : ready_queue.thread_list) {
  70. VERIFY(thread.m_runnable_priority == (int)priority);
  71. if (thread.is_active())
  72. continue;
  73. if (!(thread.affinity() & affinity_mask))
  74. continue;
  75. thread.m_runnable_priority = -1;
  76. ready_queue.thread_list.remove(thread);
  77. if (ready_queue.thread_list.is_empty())
  78. g_ready_queues_mask &= ~(1u << priority);
  79. // Mark it as active because we are using this thread. This is similar
  80. // to comparing it with Processor::current_thread, but when there are
  81. // multiple processors there's no easy way to check whether the thread
  82. // is actually still needed. This prevents accidental finalization when
  83. // a thread is no longer in Running state, but running on another core.
  84. // We need to mark it active here so that this thread won't be
  85. // scheduled on another core if it were to be queued before actually
  86. // switching to it.
  87. // FIXME: Figure out a better way maybe?
  88. thread.set_active(true);
  89. return thread;
  90. }
  91. priority_mask &= ~(1u << priority);
  92. }
  93. return *Processor::idle_thread();
  94. }
  95. bool Scheduler::dequeue_runnable_thread(Thread& thread, bool check_affinity)
  96. {
  97. if (thread.is_idle_thread())
  98. return true;
  99. ScopedSpinLock lock(g_ready_queues_lock);
  100. auto priority = thread.m_runnable_priority;
  101. if (priority < 0) {
  102. VERIFY(!thread.m_ready_queue_node.is_in_list());
  103. return false;
  104. }
  105. if (check_affinity && !(thread.affinity() & (1 << Processor::current().id())))
  106. return false;
  107. VERIFY(g_ready_queues_mask & (1u << priority));
  108. auto& ready_queue = g_ready_queues[priority];
  109. thread.m_runnable_priority = -1;
  110. ready_queue.thread_list.remove(thread);
  111. if (ready_queue.thread_list.is_empty())
  112. g_ready_queues_mask &= ~(1u << priority);
  113. return true;
  114. }
  115. void Scheduler::queue_runnable_thread(Thread& thread)
  116. {
  117. VERIFY(g_scheduler_lock.own_lock());
  118. if (thread.is_idle_thread())
  119. return;
  120. auto priority = thread_priority_to_priority_index(thread.priority());
  121. ScopedSpinLock lock(g_ready_queues_lock);
  122. VERIFY(thread.m_runnable_priority < 0);
  123. thread.m_runnable_priority = (int)priority;
  124. VERIFY(!thread.m_ready_queue_node.is_in_list());
  125. auto& ready_queue = g_ready_queues[priority];
  126. bool was_empty = ready_queue.thread_list.is_empty();
  127. ready_queue.thread_list.append(thread);
  128. if (was_empty)
  129. g_ready_queues_mask |= (1u << priority);
  130. }
  131. UNMAP_AFTER_INIT void Scheduler::start()
  132. {
  133. VERIFY_INTERRUPTS_DISABLED();
  134. // We need to acquire our scheduler lock, which will be released
  135. // by the idle thread once control transferred there
  136. g_scheduler_lock.lock();
  137. auto& processor = Processor::current();
  138. processor.set_scheduler_data(*new SchedulerPerProcessorData());
  139. VERIFY(processor.is_initialized());
  140. auto& idle_thread = *Processor::idle_thread();
  141. VERIFY(processor.current_thread() == &idle_thread);
  142. idle_thread.set_ticks_left(time_slice_for(idle_thread));
  143. idle_thread.did_schedule();
  144. idle_thread.set_initialized(true);
  145. processor.init_context(idle_thread, false);
  146. idle_thread.set_state(Thread::Running);
  147. VERIFY(idle_thread.affinity() == (1u << processor.get_id()));
  148. processor.initialize_context_switching(idle_thread);
  149. VERIFY_NOT_REACHED();
  150. }
  151. bool Scheduler::pick_next()
  152. {
  153. VERIFY_INTERRUPTS_DISABLED();
  154. auto current_thread = Thread::current();
  155. // Set the m_in_scheduler flag before acquiring the spinlock. This
  156. // prevents a recursive call into Scheduler::invoke_async upon
  157. // leaving the scheduler lock.
  158. ScopedCritical critical;
  159. auto& scheduler_data = Processor::current().get_scheduler_data();
  160. scheduler_data.m_in_scheduler = true;
  161. ScopeGuard guard(
  162. []() {
  163. // We may be on a different processor after we got switched
  164. // back to this thread!
  165. auto& scheduler_data = Processor::current().get_scheduler_data();
  166. VERIFY(scheduler_data.m_in_scheduler);
  167. scheduler_data.m_in_scheduler = false;
  168. });
  169. ScopedSpinLock lock(g_scheduler_lock);
  170. if (current_thread->should_die() && current_thread->state() == Thread::Running) {
  171. // Rather than immediately killing threads, yanking the kernel stack
  172. // away from them (which can lead to e.g. reference leaks), we always
  173. // allow Thread::wait_on to return. This allows the kernel stack to
  174. // clean up and eventually we'll get here shortly before transitioning
  175. // back to user mode (from Processor::exit_trap). At this point we
  176. // no longer want to schedule this thread. We can't wait until
  177. // Scheduler::enter_current because we don't want to allow it to
  178. // transition back to user mode.
  179. if constexpr (SCHEDULER_DEBUG)
  180. dbgln("Scheduler[{}]: Thread {} is dying", Processor::id(), *current_thread);
  181. current_thread->set_state(Thread::Dying);
  182. }
  183. if constexpr (SCHEDULER_RUNNABLE_DEBUG) {
  184. dump_thread_list();
  185. }
  186. auto pending_beneficiary = scheduler_data.m_pending_beneficiary.strong_ref();
  187. if (pending_beneficiary && dequeue_runnable_thread(*pending_beneficiary, true)) {
  188. // The thread we're supposed to donate to still exists and we can
  189. const char* reason = scheduler_data.m_pending_donate_reason;
  190. scheduler_data.m_pending_beneficiary = nullptr;
  191. scheduler_data.m_pending_donate_reason = nullptr;
  192. // We need to leave our first critical section before switching context,
  193. // but since we're still holding the scheduler lock we're still in a critical section
  194. critical.leave();
  195. dbgln_if(SCHEDULER_DEBUG, "Processing pending donate to {} reason={}", *pending_beneficiary, reason);
  196. return donate_to_and_switch(pending_beneficiary.ptr(), reason);
  197. }
  198. // Either we're not donating or the beneficiary disappeared.
  199. // Either way clear any pending information
  200. scheduler_data.m_pending_beneficiary = nullptr;
  201. scheduler_data.m_pending_donate_reason = nullptr;
  202. auto& thread_to_schedule = pull_next_runnable_thread();
  203. if constexpr (SCHEDULER_DEBUG) {
  204. dbgln("Scheduler[{}]: Switch to {} @ {:04x}:{:08x}",
  205. Processor::id(),
  206. thread_to_schedule,
  207. thread_to_schedule.tss().cs, thread_to_schedule.tss().eip);
  208. }
  209. // We need to leave our first critical section before switching context,
  210. // but since we're still holding the scheduler lock we're still in a critical section
  211. critical.leave();
  212. thread_to_schedule.set_ticks_left(time_slice_for(thread_to_schedule));
  213. return context_switch(&thread_to_schedule);
  214. }
  215. bool Scheduler::yield()
  216. {
  217. InterruptDisabler disabler;
  218. auto& proc = Processor::current();
  219. auto& scheduler_data = proc.get_scheduler_data();
  220. // Clear any pending beneficiary
  221. scheduler_data.m_pending_beneficiary = nullptr;
  222. scheduler_data.m_pending_donate_reason = nullptr;
  223. auto current_thread = Thread::current();
  224. dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: yielding thread {} in_irq={}", proc.get_id(), *current_thread, proc.in_irq());
  225. VERIFY(current_thread != nullptr);
  226. if (proc.in_irq() || proc.in_critical()) {
  227. // If we're handling an IRQ we can't switch context, or we're in
  228. // a critical section where we don't want to switch contexts, then
  229. // delay until exiting the trap or critical section
  230. proc.invoke_scheduler_async();
  231. return false;
  232. }
  233. if (!Scheduler::pick_next())
  234. return false;
  235. if constexpr (SCHEDULER_DEBUG)
  236. dbgln("Scheduler[{}]: yield returns to thread {} in_irq={}", Processor::id(), *current_thread, Processor::current().in_irq());
  237. return true;
  238. }
  239. bool Scheduler::donate_to_and_switch(Thread* beneficiary, [[maybe_unused]] const char* reason)
  240. {
  241. VERIFY(g_scheduler_lock.own_lock());
  242. auto& proc = Processor::current();
  243. VERIFY(proc.in_critical() == 1);
  244. unsigned ticks_left = Thread::current()->ticks_left();
  245. if (!beneficiary || beneficiary->state() != Thread::Runnable || ticks_left <= 1)
  246. return Scheduler::yield();
  247. unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(*beneficiary));
  248. dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: Donating {} ticks to {}, reason={}", proc.get_id(), ticks_to_donate, *beneficiary, reason);
  249. beneficiary->set_ticks_left(ticks_to_donate);
  250. return Scheduler::context_switch(beneficiary);
  251. }
  252. bool Scheduler::donate_to(RefPtr<Thread>& beneficiary, const char* reason)
  253. {
  254. VERIFY(beneficiary);
  255. if (beneficiary == Thread::current())
  256. return Scheduler::yield();
  257. // Set the m_in_scheduler flag before acquiring the spinlock. This
  258. // prevents a recursive call into Scheduler::invoke_async upon
  259. // leaving the scheduler lock.
  260. ScopedCritical critical;
  261. auto& proc = Processor::current();
  262. auto& scheduler_data = proc.get_scheduler_data();
  263. scheduler_data.m_in_scheduler = true;
  264. ScopeGuard guard(
  265. []() {
  266. // We may be on a different processor after we got switched
  267. // back to this thread!
  268. auto& scheduler_data = Processor::current().get_scheduler_data();
  269. VERIFY(scheduler_data.m_in_scheduler);
  270. scheduler_data.m_in_scheduler = false;
  271. });
  272. VERIFY(!proc.in_irq());
  273. if (proc.in_critical() > 1) {
  274. scheduler_data.m_pending_beneficiary = beneficiary; // Save the beneficiary
  275. scheduler_data.m_pending_donate_reason = reason;
  276. proc.invoke_scheduler_async();
  277. return false;
  278. }
  279. ScopedSpinLock lock(g_scheduler_lock);
  280. // "Leave" the critical section before switching context. Since we
  281. // still hold the scheduler lock, we're not actually leaving it.
  282. // Processor::switch_context expects Processor::in_critical() to be 1
  283. critical.leave();
  284. donate_to_and_switch(beneficiary, reason);
  285. return false;
  286. }
  287. bool Scheduler::context_switch(Thread* thread)
  288. {
  289. thread->did_schedule();
  290. auto from_thread = Thread::current();
  291. if (from_thread == thread)
  292. return false;
  293. if (from_thread) {
  294. // If the last process hasn't blocked (still marked as running),
  295. // mark it as runnable for the next round.
  296. if (from_thread->state() == Thread::Running)
  297. from_thread->set_state(Thread::Runnable);
  298. #ifdef LOG_EVERY_CONTEXT_SWITCH
  299. dbgln("Scheduler[{}]: {} -> {} [prio={}] {:04x}:{:08x}", Processor::id(), from_thread->tid().value(), thread->tid().value(), thread->priority(), thread->tss().cs, thread->tss().eip);
  300. #endif
  301. }
  302. auto& proc = Processor::current();
  303. if (!thread->is_initialized()) {
  304. proc.init_context(*thread, false);
  305. thread->set_initialized(true);
  306. }
  307. thread->set_state(Thread::Running);
  308. proc.switch_context(from_thread, thread);
  309. // NOTE: from_thread at this point reflects the thread we were
  310. // switched from, and thread reflects Thread::current()
  311. enter_current(*from_thread, false);
  312. VERIFY(thread == Thread::current());
  313. #if ARCH(I386)
  314. if (thread->process().is_user_process()) {
  315. auto iopl = get_iopl_from_eflags(Thread::current()->get_register_dump_from_stack().eflags);
  316. if (iopl != 0) {
  317. PANIC("Switched to thread {} with non-zero IOPL={}", Thread::current()->tid().value(), iopl);
  318. }
  319. }
  320. #endif
  321. return true;
  322. }
  323. void Scheduler::enter_current(Thread& prev_thread, bool is_first)
  324. {
  325. VERIFY(g_scheduler_lock.own_lock());
  326. prev_thread.set_active(false);
  327. if (prev_thread.state() == Thread::Dying) {
  328. // If the thread we switched from is marked as dying, then notify
  329. // the finalizer. Note that as soon as we leave the scheduler lock
  330. // the finalizer may free from_thread!
  331. notify_finalizer();
  332. } else if (!is_first) {
  333. // Check if we have any signals we should deliver (even if we don't
  334. // end up switching to another thread).
  335. auto current_thread = Thread::current();
  336. if (!current_thread->is_in_block() && current_thread->previous_mode() != Thread::PreviousMode::KernelMode) {
  337. ScopedSpinLock lock(current_thread->get_lock());
  338. if (current_thread->state() == Thread::Running && current_thread->pending_signals_for_state()) {
  339. current_thread->dispatch_one_pending_signal();
  340. }
  341. }
  342. }
  343. }
  344. void Scheduler::leave_on_first_switch(u32 flags)
  345. {
  346. // This is called when a thread is switched into for the first time.
  347. // At this point, enter_current has already be called, but because
  348. // Scheduler::context_switch is not in the call stack we need to
  349. // clean up and release locks manually here
  350. g_scheduler_lock.unlock(flags);
  351. auto& scheduler_data = Processor::current().get_scheduler_data();
  352. VERIFY(scheduler_data.m_in_scheduler);
  353. scheduler_data.m_in_scheduler = false;
  354. }
  355. void Scheduler::prepare_after_exec()
  356. {
  357. // This is called after exec() when doing a context "switch" into
  358. // the new process. This is called from Processor::assume_context
  359. VERIFY(g_scheduler_lock.own_lock());
  360. auto& scheduler_data = Processor::current().get_scheduler_data();
  361. VERIFY(!scheduler_data.m_in_scheduler);
  362. scheduler_data.m_in_scheduler = true;
  363. }
  364. void Scheduler::prepare_for_idle_loop()
  365. {
  366. // This is called when the CPU finished setting up the idle loop
  367. // and is about to run it. We need to acquire he scheduler lock
  368. VERIFY(!g_scheduler_lock.own_lock());
  369. g_scheduler_lock.lock();
  370. auto& scheduler_data = Processor::current().get_scheduler_data();
  371. VERIFY(!scheduler_data.m_in_scheduler);
  372. scheduler_data.m_in_scheduler = true;
  373. }
  374. Process* Scheduler::colonel()
  375. {
  376. VERIFY(s_colonel_process);
  377. return s_colonel_process;
  378. }
  379. UNMAP_AFTER_INIT void Scheduler::initialize()
  380. {
  381. VERIFY(&Processor::current() != nullptr); // sanity check
  382. RefPtr<Thread> idle_thread;
  383. g_finalizer_wait_queue = new WaitQueue;
  384. g_ready_queues = new ThreadReadyQueue[g_ready_queue_buckets];
  385. g_finalizer_has_work.store(false, AK::MemoryOrder::memory_order_release);
  386. s_colonel_process = Process::create_kernel_process(idle_thread, "colonel", idle_loop, nullptr, 1).leak_ref();
  387. VERIFY(s_colonel_process);
  388. VERIFY(idle_thread);
  389. idle_thread->set_priority(THREAD_PRIORITY_MIN);
  390. idle_thread->set_name(StringView("idle thread #0"));
  391. set_idle_thread(idle_thread);
  392. }
  393. UNMAP_AFTER_INIT void Scheduler::set_idle_thread(Thread* idle_thread)
  394. {
  395. idle_thread->set_idle_thread();
  396. Processor::current().set_idle_thread(*idle_thread);
  397. Processor::current().set_current_thread(*idle_thread);
  398. }
  399. UNMAP_AFTER_INIT Thread* Scheduler::create_ap_idle_thread(u32 cpu)
  400. {
  401. VERIFY(cpu != 0);
  402. // This function is called on the bsp, but creates an idle thread for another AP
  403. VERIFY(Processor::is_bootstrap_processor());
  404. VERIFY(s_colonel_process);
  405. Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, String::formatted("idle thread #{}", cpu), 1 << cpu, false);
  406. VERIFY(idle_thread);
  407. return idle_thread;
  408. }
  409. void Scheduler::timer_tick(const RegisterState& regs)
  410. {
  411. VERIFY_INTERRUPTS_DISABLED();
  412. VERIFY(Processor::current().in_irq());
  413. auto current_thread = Processor::current_thread();
  414. if (!current_thread)
  415. return;
  416. // Sanity checks
  417. VERIFY(current_thread->current_trap());
  418. VERIFY(current_thread->current_trap()->regs == &regs);
  419. #if !SCHEDULE_ON_ALL_PROCESSORS
  420. if (!Processor::is_bootstrap_processor())
  421. return; // TODO: This prevents scheduling on other CPUs!
  422. #endif
  423. if (current_thread->tick())
  424. return;
  425. VERIFY_INTERRUPTS_DISABLED();
  426. VERIFY(Processor::current().in_irq());
  427. Processor::current().invoke_scheduler_async();
  428. }
  429. void Scheduler::invoke_async()
  430. {
  431. VERIFY_INTERRUPTS_DISABLED();
  432. auto& proc = Processor::current();
  433. VERIFY(!proc.in_irq());
  434. // Since this function is called when leaving critical sections (such
  435. // as a SpinLock), we need to check if we're not already doing this
  436. // to prevent recursion
  437. if (!proc.get_scheduler_data().m_in_scheduler)
  438. pick_next();
  439. }
  440. void Scheduler::yield_from_critical()
  441. {
  442. auto& proc = Processor::current();
  443. VERIFY(proc.in_critical());
  444. VERIFY(!proc.in_irq());
  445. yield(); // Flag a context switch
  446. u32 prev_flags;
  447. u32 prev_crit = Processor::current().clear_critical(prev_flags, false);
  448. // Note, we may now be on a different CPU!
  449. Processor::current().restore_critical(prev_crit, prev_flags);
  450. }
  451. void Scheduler::notify_finalizer()
  452. {
  453. if (g_finalizer_has_work.exchange(true, AK::MemoryOrder::memory_order_acq_rel) == false)
  454. g_finalizer_wait_queue->wake_all();
  455. }
  456. void Scheduler::idle_loop(void*)
  457. {
  458. auto& proc = Processor::current();
  459. dbgln("Scheduler[{}]: idle loop running", proc.get_id());
  460. VERIFY(are_interrupts_enabled());
  461. for (;;) {
  462. proc.idle_begin();
  463. asm("hlt");
  464. proc.idle_end();
  465. VERIFY_INTERRUPTS_ENABLED();
  466. #if SCHEDULE_ON_ALL_PROCESSORS
  467. yield();
  468. #else
  469. if (Processor::current().id() == 0)
  470. yield();
  471. #endif
  472. }
  473. }
  474. void Scheduler::dump_scheduler_state()
  475. {
  476. dump_thread_list();
  477. }
  478. void dump_thread_list()
  479. {
  480. dbgln("Scheduler thread list for processor {}:", Processor::id());
  481. auto get_cs = [](Thread& thread) -> u16 {
  482. if (!thread.current_trap())
  483. return thread.tss().cs;
  484. return thread.get_register_dump_from_stack().cs;
  485. };
  486. auto get_eip = [](Thread& thread) -> u32 {
  487. if (!thread.current_trap())
  488. return thread.tss().eip;
  489. return thread.get_register_dump_from_stack().eip;
  490. };
  491. Thread::for_each([&](Thread& thread) -> IterationDecision {
  492. switch (thread.state()) {
  493. case Thread::Dying:
  494. dbgln(" {:14} {:30} @ {:04x}:{:08x} Finalizable: {}, (nsched: {})",
  495. thread.state_string(),
  496. thread,
  497. get_cs(thread),
  498. get_eip(thread),
  499. thread.is_finalizable(),
  500. thread.times_scheduled());
  501. break;
  502. default:
  503. dbgln(" {:14} Pr:{:2} {:30} @ {:04x}:{:08x} (nsched: {})",
  504. thread.state_string(),
  505. thread.priority(),
  506. thread,
  507. get_cs(thread),
  508. get_eip(thread),
  509. thread.times_scheduled());
  510. break;
  511. }
  512. return IterationDecision::Continue;
  513. });
  514. }
  515. }