Scheduler.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. }
  39. return true;
  40. }
  41. if (process.state() == Process::BlockedRead) {
  42. ASSERT(process.m_fdBlockedOnRead != -1);
  43. // FIXME: Block until the amount of data wanted is available.
  44. if (process.m_file_descriptors[process.m_fdBlockedOnRead]->hasDataAvailableForRead())
  45. process.unblock();
  46. return true;
  47. }
  48. if (process.state() == Process::Skip1SchedulerPass) {
  49. process.set_state(Process::Skip0SchedulerPasses);
  50. return true;
  51. }
  52. if (process.state() == Process::Skip0SchedulerPasses) {
  53. process.set_state(Process::Runnable);
  54. return true;
  55. }
  56. if (process.state() == Process::Dead) {
  57. if (current != &process && !Process::from_pid(process.ppid()))
  58. Process::reap(process.pid());
  59. return true;
  60. }
  61. return true;
  62. });
  63. // Dispatch any pending signals.
  64. // FIXME: Do we really need this to be a separate pass over the process list?
  65. Process::for_each_not_in_state(Process::Dead, [] (auto& process) {
  66. if (!process.has_unmasked_pending_signals())
  67. return true;
  68. // We know how to interrupt blocked processes, but if they are just executing
  69. // at some random point in the kernel, let them continue. They'll be in userspace
  70. // sooner or later and we can deliver the signal then.
  71. // FIXME: Maybe we could check when returning from a syscall if there's a pending
  72. // signal and dispatch it then and there? Would that be doable without the
  73. // syscall effectively being "interrupted" despite having completed?
  74. if (process.in_kernel() && !process.is_blocked())
  75. return true;
  76. process.dispatch_one_pending_signal();
  77. if (process.is_blocked()) {
  78. process.m_was_interrupted_while_blocked = true;
  79. process.unblock();
  80. }
  81. return true;
  82. });
  83. #ifdef SCHEDULER_DEBUG
  84. dbgprintf("Scheduler choices:\n");
  85. for (auto* process = g_processes->head(); process; process = process->next()) {
  86. //if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
  87. // continue;
  88. dbgprintf("% 12s %s(%u) @ %w:%x\n", toString(process->state()), process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  89. }
  90. #endif
  91. auto* prevHead = g_processes->head();
  92. for (;;) {
  93. // Move head to tail.
  94. g_processes->append(g_processes->removeHead());
  95. auto* process = g_processes->head();
  96. if (process->state() == Process::Runnable || process->state() == Process::Running) {
  97. #ifdef SCHEDULER_DEBUG
  98. dbgprintf("switch to %s(%u) @ %w:%x\n", process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  99. #endif
  100. return context_switch(*process);
  101. }
  102. if (process == prevHead) {
  103. // Back at process_head, nothing wants to run. Send in the colonel!
  104. return context_switch(*s_colonel_process);
  105. }
  106. }
  107. }
  108. bool Scheduler::yield()
  109. {
  110. if (!current) {
  111. kprintf("PANIC: sched_yield() with !current");
  112. HANG;
  113. }
  114. //dbgprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  115. InterruptDisabler disabler;
  116. if (!pick_next())
  117. return 1;
  118. //dbgprintf("yield() jumping to new process: %x (%s)\n", current->farPtr().selector, current->name().characters());
  119. switch_now();
  120. return 0;
  121. }
  122. void Scheduler::pick_next_and_switch_now()
  123. {
  124. bool someone_wants_to_run = pick_next();
  125. ASSERT(someone_wants_to_run);
  126. switch_now();
  127. }
  128. void Scheduler::switch_now()
  129. {
  130. Descriptor& descriptor = getGDTEntry(current->selector());
  131. descriptor.type = 9;
  132. flushGDT();
  133. asm("sti\n"
  134. "ljmp *(%%eax)\n"
  135. ::"a"(&current->farPtr())
  136. );
  137. }
  138. bool Scheduler::context_switch(Process& process)
  139. {
  140. process.set_ticks_left(time_slice);
  141. process.did_schedule();
  142. if (current == &process)
  143. return false;
  144. if (current) {
  145. // If the last process hasn't blocked (still marked as running),
  146. // mark it as runnable for the next round.
  147. if (current->state() == Process::Running)
  148. current->set_state(Process::Runnable);
  149. #ifdef LOG_EVERY_CONTEXT_SWITCH
  150. dbgprintf("Scheduler: %s(%u) -> %s(%u)\n", current->name().characters(), current->pid(), process.name().characters(), process.pid());
  151. #endif
  152. }
  153. current = &process;
  154. process.set_state(Process::Running);
  155. #ifdef COOL_GLOBALS
  156. g_cool_globals->current_pid = process.pid();
  157. #endif
  158. if (!process.selector()) {
  159. process.setSelector(gdt_alloc_entry());
  160. auto& descriptor = getGDTEntry(process.selector());
  161. descriptor.setBase(&process.tss());
  162. descriptor.setLimit(0xffff);
  163. descriptor.dpl = 0;
  164. descriptor.segment_present = 1;
  165. descriptor.granularity = 1;
  166. descriptor.zero = 0;
  167. descriptor.operation_size = 1;
  168. descriptor.descriptor_type = 0;
  169. }
  170. auto& descriptor = getGDTEntry(process.selector());
  171. descriptor.type = 11; // Busy TSS
  172. flushGDT();
  173. return true;
  174. }
  175. int sched_yield()
  176. {
  177. return Scheduler::yield();
  178. }
  179. static void initialize_redirection()
  180. {
  181. auto& descriptor = getGDTEntry(s_redirection.selector);
  182. descriptor.setBase(&s_redirection.tss);
  183. descriptor.setLimit(0xffff);
  184. descriptor.dpl = 0;
  185. descriptor.segment_present = 1;
  186. descriptor.granularity = 1;
  187. descriptor.zero = 0;
  188. descriptor.operation_size = 1;
  189. descriptor.descriptor_type = 0;
  190. descriptor.type = 9;
  191. flushGDT();
  192. }
  193. void Scheduler::prepare_for_iret_to_new_process()
  194. {
  195. auto& descriptor = getGDTEntry(s_redirection.selector);
  196. descriptor.type = 9;
  197. s_redirection.tss.backlink = current->selector();
  198. load_task_register(s_redirection.selector);
  199. }
  200. void Scheduler::prepare_to_modify_tss(Process& process)
  201. {
  202. // This ensures that a currently running process modifying its own TSS
  203. // in order to yield() and end up somewhere else doesn't just end up
  204. // right after the yield().
  205. if (current == &process)
  206. load_task_register(s_redirection.selector);
  207. }
  208. void Scheduler::initialize()
  209. {
  210. memset(&s_redirection, 0, sizeof(s_redirection));
  211. s_redirection.selector = gdt_alloc_entry();
  212. initialize_redirection();
  213. s_colonel_process = Process::create_kernel_process(nullptr, "colonel");
  214. current = nullptr;
  215. load_task_register(s_redirection.selector);
  216. }
  217. void Scheduler::timer_tick(RegisterDump& regs)
  218. {
  219. if (!current)
  220. return;
  221. system.uptime++;
  222. if (current->tick())
  223. return;
  224. current->tss().gs = regs.gs;
  225. current->tss().fs = regs.fs;
  226. current->tss().es = regs.es;
  227. current->tss().ds = regs.ds;
  228. current->tss().edi = regs.edi;
  229. current->tss().esi = regs.esi;
  230. current->tss().ebp = regs.ebp;
  231. current->tss().ebx = regs.ebx;
  232. current->tss().edx = regs.edx;
  233. current->tss().ecx = regs.ecx;
  234. current->tss().eax = regs.eax;
  235. current->tss().eip = regs.eip;
  236. current->tss().cs = regs.cs;
  237. current->tss().eflags = regs.eflags;
  238. // Compute process stack pointer.
  239. // Add 12 for CS, EIP, EFLAGS (interrupt mechanic)
  240. current->tss().esp = regs.esp + 12;
  241. current->tss().ss = regs.ss;
  242. if ((current->tss().cs & 3) != 0) {
  243. current->tss().ss = regs.ss_if_crossRing;
  244. current->tss().esp = regs.esp_if_crossRing;
  245. }
  246. if (!pick_next())
  247. return;
  248. prepare_for_iret_to_new_process();
  249. // Set the NT (nested task) flag.
  250. asm(
  251. "pushf\n"
  252. "orl $0x00004000, (%esp)\n"
  253. "popf\n"
  254. );
  255. }