Scheduler.cpp 8.1 KB

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