Scheduler.cpp 9.9 KB

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