Scheduler.cpp 9.0 KB

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