Scheduler.cpp 9.4 KB

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