Scheduler.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_pid() == -1 || process.waitee_pid() == child.pid()) {
  34. process.m_waitee_pid = 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_blocked_fd != -1);
  44. // FIXME: Block until the amount of data wanted is available.
  45. if (process.m_fds[process.m_blocked_fd].descriptor->has_data_available_for_reading())
  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. // NOTE: dispatch_one_pending_signal() may unblock the process.
  88. bool was_blocked = process.is_blocked();
  89. if (!process.dispatch_one_pending_signal())
  90. return true;
  91. if (was_blocked) {
  92. dbgprintf("Unblock %s(%u) due to signal\n", process.name().characters(), process.pid());
  93. process.m_was_interrupted_while_blocked = true;
  94. process.unblock();
  95. }
  96. return true;
  97. });
  98. #ifdef SCHEDULER_DEBUG
  99. dbgprintf("Scheduler choices:\n");
  100. for (auto* process = g_processes->head(); process; process = process->next()) {
  101. //if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
  102. // continue;
  103. dbgprintf("% 12s %s(%u) @ %w:%x\n", toString(process->state()), process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  104. }
  105. #endif
  106. auto* prevHead = g_processes->head();
  107. for (;;) {
  108. // Move head to tail.
  109. g_processes->append(g_processes->remove_head());
  110. auto* process = g_processes->head();
  111. if (process->state() == Process::Runnable || process->state() == Process::Running) {
  112. #ifdef SCHEDULER_DEBUG
  113. dbgprintf("switch to %s(%u) @ %w:%x\n", process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  114. #endif
  115. return context_switch(*process);
  116. }
  117. if (process == prevHead) {
  118. // Back at process_head, nothing wants to run. Send in the colonel!
  119. return context_switch(*s_colonel_process);
  120. }
  121. }
  122. }
  123. bool Scheduler::yield()
  124. {
  125. if (!current) {
  126. kprintf("PANIC: sched_yield() with !current");
  127. HANG;
  128. }
  129. //dbgprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  130. InterruptDisabler disabler;
  131. if (!pick_next())
  132. return 1;
  133. //dbgprintf("yield() jumping to new process: %x (%s)\n", current->farPtr().selector, current->name().characters());
  134. switch_now();
  135. return 0;
  136. }
  137. void Scheduler::pick_next_and_switch_now()
  138. {
  139. bool someone_wants_to_run = pick_next();
  140. ASSERT(someone_wants_to_run);
  141. switch_now();
  142. }
  143. void Scheduler::switch_now()
  144. {
  145. Descriptor& descriptor = get_gdt_entry(current->selector());
  146. descriptor.type = 9;
  147. flush_gdt();
  148. asm("sti\n"
  149. "ljmp *(%%eax)\n"
  150. ::"a"(&current->farPtr())
  151. );
  152. }
  153. bool Scheduler::context_switch(Process& process)
  154. {
  155. process.set_ticks_left(time_slice);
  156. process.did_schedule();
  157. if (process.tss().cs & 3) {
  158. ++process.m_ticks_in_user;
  159. } else {
  160. ++process.m_ticks_in_kernel;
  161. }
  162. if (current == &process)
  163. return false;
  164. if (current) {
  165. // If the last process hasn't blocked (still marked as running),
  166. // mark it as runnable for the next round.
  167. if (current->state() == Process::Running)
  168. current->set_state(Process::Runnable);
  169. #ifdef LOG_EVERY_CONTEXT_SWITCH
  170. dbgprintf("Scheduler: %s(%u) -> %s(%u)\n", current->name().characters(), current->pid(), process.name().characters(), process.pid());
  171. #endif
  172. }
  173. current = &process;
  174. process.set_state(Process::Running);
  175. #ifdef COOL_GLOBALS
  176. g_cool_globals->current_pid = process.pid();
  177. #endif
  178. if (!process.selector()) {
  179. process.setSelector(gdt_alloc_entry());
  180. auto& descriptor = get_gdt_entry(process.selector());
  181. descriptor.setBase(&process.tss());
  182. descriptor.setLimit(0xffff);
  183. descriptor.dpl = 0;
  184. descriptor.segment_present = 1;
  185. descriptor.granularity = 1;
  186. descriptor.zero = 0;
  187. descriptor.operation_size = 1;
  188. descriptor.descriptor_type = 0;
  189. }
  190. auto& descriptor = get_gdt_entry(process.selector());
  191. descriptor.type = 11; // Busy TSS
  192. flush_gdt();
  193. return true;
  194. }
  195. int sched_yield()
  196. {
  197. return Scheduler::yield();
  198. }
  199. static void initialize_redirection()
  200. {
  201. auto& descriptor = get_gdt_entry(s_redirection.selector);
  202. descriptor.setBase(&s_redirection.tss);
  203. descriptor.setLimit(0xffff);
  204. descriptor.dpl = 0;
  205. descriptor.segment_present = 1;
  206. descriptor.granularity = 1;
  207. descriptor.zero = 0;
  208. descriptor.operation_size = 1;
  209. descriptor.descriptor_type = 0;
  210. descriptor.type = 9;
  211. flush_gdt();
  212. }
  213. void Scheduler::prepare_for_iret_to_new_process()
  214. {
  215. auto& descriptor = get_gdt_entry(s_redirection.selector);
  216. descriptor.type = 9;
  217. s_redirection.tss.backlink = current->selector();
  218. load_task_register(s_redirection.selector);
  219. }
  220. void Scheduler::prepare_to_modify_tss(Process& process)
  221. {
  222. // This ensures that a currently running process modifying its own TSS
  223. // in order to yield() and end up somewhere else doesn't just end up
  224. // right after the yield().
  225. if (current == &process)
  226. load_task_register(s_redirection.selector);
  227. }
  228. void Scheduler::initialize()
  229. {
  230. memset(&s_redirection, 0, sizeof(s_redirection));
  231. s_redirection.selector = gdt_alloc_entry();
  232. initialize_redirection();
  233. s_colonel_process = Process::create_kernel_process(nullptr, "colonel");
  234. current = nullptr;
  235. load_task_register(s_redirection.selector);
  236. }
  237. void Scheduler::timer_tick(RegisterDump& regs)
  238. {
  239. if (!current)
  240. return;
  241. system.uptime++;
  242. if (current->tick())
  243. return;
  244. current->tss().gs = regs.gs;
  245. current->tss().fs = regs.fs;
  246. current->tss().es = regs.es;
  247. current->tss().ds = regs.ds;
  248. current->tss().edi = regs.edi;
  249. current->tss().esi = regs.esi;
  250. current->tss().ebp = regs.ebp;
  251. current->tss().ebx = regs.ebx;
  252. current->tss().edx = regs.edx;
  253. current->tss().ecx = regs.ecx;
  254. current->tss().eax = regs.eax;
  255. current->tss().eip = regs.eip;
  256. current->tss().cs = regs.cs;
  257. current->tss().eflags = regs.eflags;
  258. // Compute process stack pointer.
  259. // Add 12 for CS, EIP, EFLAGS (interrupt mechanic)
  260. current->tss().esp = regs.esp + 12;
  261. current->tss().ss = regs.ss;
  262. if ((current->tss().cs & 3) != 0) {
  263. current->tss().ss = regs.ss_if_crossRing;
  264. current->tss().esp = regs.esp_if_crossRing;
  265. }
  266. if (!pick_next())
  267. return;
  268. prepare_for_iret_to_new_process();
  269. // Set the NT (nested task) flag.
  270. asm(
  271. "pushf\n"
  272. "orl $0x00004000, (%esp)\n"
  273. "popf\n"
  274. );
  275. }