Scheduler.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "Scheduler.h"
  2. #include "Process.h"
  3. #include "system.h"
  4. //#define SCHEDULER_DEBUG
  5. static const dword time_slice = 5; // *10 = 50ms
  6. Process* current;
  7. static Process* s_colonel_process;
  8. bool Scheduler::pick_next()
  9. {
  10. ASSERT_INTERRUPTS_DISABLED();
  11. if (!current) {
  12. // XXX: The first ever context_switch() goes to the idle process.
  13. // This to setup a reliable place we can return to.
  14. return context_switch(*s_colonel_process);
  15. }
  16. // Check and unblock processes whose wait conditions have been met.
  17. Process::for_each([] (auto& process) {
  18. if (process.state() == Process::BlockedSleep) {
  19. if (process.wakeupTime() <= system.uptime)
  20. process.unblock();
  21. return true;
  22. }
  23. if (process.state() == Process::BlockedWait) {
  24. auto* waitee = Process::from_pid(process.waitee());
  25. if (!waitee) {
  26. kprintf("waitee %u of %s(%u) reaped before I could wait?\n", process.waitee(), process.name().characters(), process.pid());
  27. ASSERT_NOT_REACHED();
  28. }
  29. if (waitee->state() == Process::Dead) {
  30. process.m_waitee_status = (waitee->m_termination_status << 8) | waitee->m_termination_signal;
  31. process.unblock();
  32. waitee->set_state(Process::Forgiven);
  33. }
  34. return true;
  35. }
  36. if (process.state() == Process::BlockedRead) {
  37. ASSERT(process.m_fdBlockedOnRead != -1);
  38. // FIXME: Block until the amount of data wanted is available.
  39. if (process.m_file_descriptors[process.m_fdBlockedOnRead]->hasDataAvailableForRead())
  40. process.unblock();
  41. return true;
  42. }
  43. return true;
  44. });
  45. // Forgive dead orphans.
  46. // FIXME: Does this really make sense?
  47. Process::for_each_in_state(Process::Dead, [] (auto& process) {
  48. if (!Process::from_pid(process.ppid()))
  49. process.set_state(Process::Forgiven);
  50. return true;
  51. });
  52. // Clean up forgiven processes.
  53. // FIXME: Do we really need this to be a separate pass over the process list?
  54. Process::for_each_in_state(Process::Forgiven, [] (auto& process) {
  55. g_processes->remove(&process);
  56. g_dead_processes->append(&process);
  57. return true;
  58. });
  59. // Dispatch any pending signals.
  60. // FIXME: Do we really need this to be a separate pass over the process list?
  61. Process::for_each_not_in_state(Process::Dead, [] (auto& process) {
  62. if (!process.has_unmasked_pending_signals())
  63. return true;
  64. // We know how to interrupt blocked processes, but if they are just executing
  65. // at some random point in the kernel, let them continue. They'll be in userspace
  66. // sooner or later and we can deliver the signal then.
  67. // FIXME: Maybe we could check when returning from a syscall if there's a pending
  68. // signal and dispatch it then and there? Would that be doable without the
  69. // syscall effectively being "interrupted" despite having completed?
  70. if (process.in_kernel() && !process.is_blocked())
  71. return true;
  72. process.dispatch_one_pending_signal();
  73. if (process.is_blocked()) {
  74. process.m_was_interrupted_while_blocked = true;
  75. process.unblock();
  76. }
  77. return true;
  78. });
  79. #ifdef SCHEDULER_DEBUG
  80. dbgprintf("Scheduler choices:\n");
  81. for (auto* process = g_processes->head(); process; process = process->next()) {
  82. //if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
  83. // continue;
  84. dbgprintf("% 12s %s(%u) @ %w:%x\n", toString(process->state()), process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  85. }
  86. #endif
  87. auto* prevHead = g_processes->head();
  88. for (;;) {
  89. // Move head to tail.
  90. g_processes->append(g_processes->removeHead());
  91. auto* process = g_processes->head();
  92. if (process->state() == Process::Runnable || process->state() == Process::Running) {
  93. #ifdef SCHEDULER_DEBUG
  94. dbgprintf("switch to %s(%u)\n", process->name().characters(), process->pid());
  95. #endif
  96. return context_switch(*process);
  97. }
  98. if (process == prevHead) {
  99. // Back at process_head, nothing wants to run.
  100. kprintf("Nothing wants to run!\n");
  101. kprintf("PID OWNER STATE NSCHED NAME\n");
  102. for (auto* process = g_processes->head(); process; process = process->next()) {
  103. kprintf("%w %w:%w %b %w %s\n",
  104. process->pid(),
  105. process->uid(),
  106. process->gid(),
  107. process->state(),
  108. process->timesScheduled(),
  109. process->name().characters());
  110. }
  111. kprintf("Switch to kernel process @ %w:%x\n", s_colonel_process->tss().cs, s_colonel_process->tss().eip);
  112. return context_switch(*s_colonel_process);
  113. }
  114. }
  115. }
  116. bool Scheduler::yield()
  117. {
  118. if (!current) {
  119. kprintf("PANIC: sched_yield() with !current");
  120. HANG;
  121. }
  122. //dbgprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  123. InterruptDisabler disabler;
  124. if (!pick_next())
  125. return 1;
  126. //dbgprintf("yield() jumping to new process: %x (%s)\n", current->farPtr().selector, current->name().characters());
  127. switch_now();
  128. return 0;
  129. }
  130. void Scheduler::pick_next_and_switch_now()
  131. {
  132. bool someone_wants_to_run = pick_next();
  133. ASSERT(someone_wants_to_run);
  134. switch_now();
  135. }
  136. void Scheduler::switch_now()
  137. {
  138. Descriptor& descriptor = getGDTEntry(current->selector());
  139. descriptor.type = 9;
  140. flushGDT();
  141. asm("sti\n"
  142. "ljmp *(%%eax)\n"
  143. ::"a"(&current->farPtr())
  144. );
  145. }
  146. bool Scheduler::context_switch(Process& process)
  147. {
  148. process.set_ticks_left(time_slice);
  149. process.did_schedule();
  150. if (current == &process)
  151. return false;
  152. if (current) {
  153. // If the last process hasn't blocked (still marked as running),
  154. // mark it as runnable for the next round.
  155. if (current->state() == Process::Running)
  156. current->set_state(Process::Runnable);
  157. }
  158. current = &process;
  159. process.set_state(Process::Running);
  160. #ifdef COOL_GLOBALS
  161. g_cool_globals->current_pid = process.pid();
  162. #endif
  163. if (!process.selector()) {
  164. process.setSelector(gdt_alloc_entry());
  165. auto& descriptor = getGDTEntry(process.selector());
  166. descriptor.setBase(&process.tss());
  167. descriptor.setLimit(0xffff);
  168. descriptor.dpl = 0;
  169. descriptor.segment_present = 1;
  170. descriptor.granularity = 1;
  171. descriptor.zero = 0;
  172. descriptor.operation_size = 1;
  173. descriptor.descriptor_type = 0;
  174. }
  175. auto& descriptor = getGDTEntry(process.selector());
  176. descriptor.type = 11; // Busy TSS
  177. flushGDT();
  178. return true;
  179. }
  180. int sched_yield()
  181. {
  182. return Scheduler::yield();
  183. }
  184. static void redo_colonel_process_tss()
  185. {
  186. if (!s_colonel_process->selector())
  187. s_colonel_process->setSelector(gdt_alloc_entry());
  188. auto& tssDescriptor = getGDTEntry(s_colonel_process->selector());
  189. tssDescriptor.setBase(&s_colonel_process->tss());
  190. tssDescriptor.setLimit(0xffff);
  191. tssDescriptor.dpl = 0;
  192. tssDescriptor.segment_present = 1;
  193. tssDescriptor.granularity = 1;
  194. tssDescriptor.zero = 0;
  195. tssDescriptor.operation_size = 1;
  196. tssDescriptor.descriptor_type = 0;
  197. tssDescriptor.type = 9;
  198. flushGDT();
  199. }
  200. void Scheduler::prepare_for_iret_to_new_process()
  201. {
  202. redo_colonel_process_tss();
  203. s_colonel_process->tss().backlink = current->selector();
  204. load_task_register(s_colonel_process->selector());
  205. }
  206. void Scheduler::prepare_to_modify_own_tss()
  207. {
  208. // This ensures that a process modifying its own TSS in order to yield()
  209. // and end up somewhere else doesn't just end up right after the yield().
  210. load_task_register(s_colonel_process->selector());
  211. }
  212. static void hlt_loop()
  213. {
  214. for (;;) {
  215. asm volatile("hlt");
  216. }
  217. }
  218. void Scheduler::initialize()
  219. {
  220. s_colonel_process = Process::create_kernel_process(hlt_loop, "colonel");
  221. current = nullptr;
  222. redo_colonel_process_tss();
  223. load_task_register(s_colonel_process->selector());
  224. }