Scheduler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "Scheduler.h"
  2. #include "Process.h"
  3. #include "system.h"
  4. #include "RTC.h"
  5. #include "i8253.h"
  6. #include <AK/TemporaryChange.h>
  7. //#define LOG_EVERY_CONTEXT_SWITCH
  8. //#define SCHEDULER_DEBUG
  9. static dword time_slice_for(Process::Priority priority)
  10. {
  11. // One time slice unit == 1ms
  12. switch (priority) {
  13. case Process::HighPriority:
  14. return 50;
  15. case Process::NormalPriority:
  16. return 15;
  17. case Process::LowPriority:
  18. return 5;
  19. }
  20. ASSERT_NOT_REACHED();
  21. }
  22. Process* current;
  23. Process* g_last_fpu_process;
  24. Process* g_finalizer;
  25. static Process* s_colonel_process;
  26. struct TaskRedirectionData {
  27. word selector;
  28. TSS32 tss;
  29. };
  30. static TaskRedirectionData s_redirection;
  31. static bool s_active;
  32. bool Scheduler::is_active()
  33. {
  34. return s_active;
  35. }
  36. bool Scheduler::pick_next()
  37. {
  38. ASSERT_INTERRUPTS_DISABLED();
  39. ASSERT(!s_active);
  40. TemporaryChange<bool> change(s_active, true);
  41. ASSERT(s_active);
  42. if (!current) {
  43. // XXX: The first ever context_switch() goes to the idle process.
  44. // This to setup a reliable place we can return to.
  45. return context_switch(*s_colonel_process);
  46. }
  47. // Check and unblock processes whose wait conditions have been met.
  48. Process::for_each([] (Process& process) {
  49. if (process.state() == Process::BlockedSleep) {
  50. if (process.wakeup_time() <= system.uptime)
  51. process.unblock();
  52. return true;
  53. }
  54. if (process.state() == Process::BlockedWait) {
  55. process.for_each_child([&process] (Process& child) {
  56. if (child.state() != Process::Dead)
  57. return true;
  58. if (process.waitee_pid() == -1 || process.waitee_pid() == child.pid()) {
  59. process.m_waitee_pid = child.pid();
  60. process.unblock();
  61. return false;
  62. }
  63. return true;
  64. });
  65. return true;
  66. }
  67. if (process.state() == Process::BlockedRead) {
  68. ASSERT(process.m_blocked_fd != -1);
  69. // FIXME: Block until the amount of data wanted is available.
  70. if (process.m_fds[process.m_blocked_fd].descriptor->can_read(process))
  71. process.unblock();
  72. return true;
  73. }
  74. if (process.state() == Process::BlockedWrite) {
  75. ASSERT(process.m_blocked_fd != -1);
  76. if (process.m_fds[process.m_blocked_fd].descriptor->can_write(process))
  77. process.unblock();
  78. return true;
  79. }
  80. if (process.state() == Process::BlockedConnect) {
  81. ASSERT(process.m_blocked_connecting_socket);
  82. if (process.m_blocked_connecting_socket->is_connected())
  83. process.unblock();
  84. return true;
  85. }
  86. if (process.state() == Process::BlockedSelect) {
  87. if (process.m_select_has_timeout) {
  88. auto now_sec = RTC::now();
  89. auto now_usec = PIT::ticks_since_boot() % 1000;
  90. if (now_sec > process.m_select_timeout.tv_sec || (now_sec == process.m_select_timeout.tv_sec && now_usec >= process.m_select_timeout.tv_usec)) {
  91. process.unblock();
  92. return true;
  93. }
  94. }
  95. for (int fd : process.m_select_read_fds) {
  96. if (process.m_fds[fd].descriptor->can_read(process)) {
  97. process.unblock();
  98. return true;
  99. }
  100. }
  101. for (int fd : process.m_select_write_fds) {
  102. if (process.m_fds[fd].descriptor->can_write(process)) {
  103. process.unblock();
  104. return true;
  105. }
  106. }
  107. return true;
  108. }
  109. if (process.state() == Process::Skip1SchedulerPass) {
  110. process.set_state(Process::Skip0SchedulerPasses);
  111. return true;
  112. }
  113. if (process.state() == Process::Skip0SchedulerPasses) {
  114. process.set_state(Process::Runnable);
  115. return true;
  116. }
  117. if (process.state() == Process::Dead) {
  118. if (current != &process && (!process.ppid() || !Process::from_pid(process.ppid()))) {
  119. auto name = process.name();
  120. auto pid = process.pid();
  121. auto exit_status = Process::reap(process);
  122. dbgprintf("reaped unparented process %s(%u), exit status: %u\n", name.characters(), pid, exit_status);
  123. }
  124. return true;
  125. }
  126. if (process.state() == Process::Dying) {
  127. ASSERT(g_finalizer);
  128. if (g_finalizer->state() == Process::BlockedLurking)
  129. g_finalizer->unblock();
  130. return true;
  131. }
  132. return true;
  133. });
  134. // Dispatch any pending signals.
  135. // FIXME: Do we really need this to be a separate pass over the process list?
  136. Process::for_each_living([] (auto& process) {
  137. if (!process.has_unmasked_pending_signals())
  138. return true;
  139. // FIXME: It would be nice if the Scheduler didn't have to worry about who is "current"
  140. // For now, avoid dispatching signals to "current" and do it in a scheduling pass
  141. // while some other process is interrupted. Otherwise a mess will be made.
  142. if (&process == current)
  143. return true;
  144. // We know how to interrupt blocked processes, but if they are just executing
  145. // at some random point in the kernel, let them continue. They'll be in userspace
  146. // sooner or later and we can deliver the signal then.
  147. // FIXME: Maybe we could check when returning from a syscall if there's a pending
  148. // signal and dispatch it then and there? Would that be doable without the
  149. // syscall effectively being "interrupted" despite having completed?
  150. if (process.in_kernel() && !process.is_blocked() && !process.is_stopped())
  151. return true;
  152. // NOTE: dispatch_one_pending_signal() may unblock the process.
  153. bool was_blocked = process.is_blocked();
  154. if (process.dispatch_one_pending_signal() == ShouldUnblockProcess::No)
  155. return true;
  156. if (was_blocked) {
  157. dbgprintf("Unblock %s(%u) due to signal\n", process.name().characters(), process.pid());
  158. process.m_was_interrupted_while_blocked = true;
  159. process.unblock();
  160. }
  161. return true;
  162. });
  163. #ifdef SCHEDULER_DEBUG
  164. dbgprintf("Scheduler choices:\n");
  165. for (auto* process = g_processes->head(); process; process = process->next()) {
  166. //if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
  167. // continue;
  168. dbgprintf("[K%x] % 12s %s(%u) @ %w:%x\n", process, to_string(process->state()), process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  169. }
  170. #endif
  171. auto* previous_head = g_processes->head();
  172. for (;;) {
  173. // Move head to tail.
  174. g_processes->append(g_processes->remove_head());
  175. auto* process = g_processes->head();
  176. if (process->state() == Process::Runnable || process->state() == Process::Running) {
  177. #ifdef SCHEDULER_DEBUG
  178. kprintf("switch to %s(%u) @ %w:%x\n", process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
  179. #endif
  180. return context_switch(*process);
  181. }
  182. if (process == previous_head) {
  183. // Back at process_head, nothing wants to run. Send in the colonel!
  184. return context_switch(*s_colonel_process);
  185. }
  186. }
  187. }
  188. bool Scheduler::donate_to(Process* beneficiary, const char* reason)
  189. {
  190. (void)reason;
  191. unsigned ticks_left = current->ticks_left();
  192. if (!beneficiary || beneficiary->state() != Process::Runnable || ticks_left <= 1) {
  193. return yield();
  194. }
  195. unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(beneficiary->priority()));
  196. #ifdef SCHEDULER_DEBUG
  197. dbgprintf("%s(%u) donating %u ticks to %s(%u), reason=%s\n", current->name().characters(), current->pid(), ticks_to_donate, beneficiary->name().characters(), beneficiary->pid(), reason);
  198. #endif
  199. context_switch(*beneficiary);
  200. beneficiary->set_ticks_left(ticks_to_donate);
  201. switch_now();
  202. return 0;
  203. }
  204. bool Scheduler::yield()
  205. {
  206. InterruptDisabler disabler;
  207. ASSERT(current);
  208. //dbgprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
  209. if (!pick_next())
  210. return 1;
  211. //dbgprintf("yield() jumping to new process: %x (%s)\n", current->far_ptr().selector, current->name().characters());
  212. switch_now();
  213. return 0;
  214. }
  215. void Scheduler::pick_next_and_switch_now()
  216. {
  217. bool someone_wants_to_run = pick_next();
  218. ASSERT(someone_wants_to_run);
  219. switch_now();
  220. }
  221. void Scheduler::switch_now()
  222. {
  223. Descriptor& descriptor = get_gdt_entry(current->selector());
  224. descriptor.type = 9;
  225. flush_gdt();
  226. asm("sti\n"
  227. "ljmp *(%%eax)\n"
  228. ::"a"(&current->far_ptr())
  229. );
  230. }
  231. bool Scheduler::context_switch(Process& process)
  232. {
  233. process.set_ticks_left(time_slice_for(process.priority()));
  234. process.did_schedule();
  235. if (current == &process)
  236. return false;
  237. if (current) {
  238. // If the last process hasn't blocked (still marked as running),
  239. // mark it as runnable for the next round.
  240. if (current->state() == Process::Running)
  241. current->set_state(Process::Runnable);
  242. #ifdef LOG_EVERY_CONTEXT_SWITCH
  243. dbgprintf("Scheduler: %s(%u) -> %s(%u) %w:%x\n",
  244. current->name().characters(), current->pid(),
  245. process.name().characters(), process.pid(),
  246. process.tss().cs, process.tss().eip);
  247. #endif
  248. }
  249. current = &process;
  250. process.set_state(Process::Running);
  251. #ifdef COOL_GLOBALS
  252. g_cool_globals->current_pid = process.pid();
  253. #endif
  254. if (!process.selector()) {
  255. process.set_selector(gdt_alloc_entry());
  256. auto& descriptor = get_gdt_entry(process.selector());
  257. descriptor.set_base(&process.tss());
  258. descriptor.set_limit(0xffff);
  259. descriptor.dpl = 0;
  260. descriptor.segment_present = 1;
  261. descriptor.granularity = 1;
  262. descriptor.zero = 0;
  263. descriptor.operation_size = 1;
  264. descriptor.descriptor_type = 0;
  265. }
  266. auto& descriptor = get_gdt_entry(process.selector());
  267. descriptor.type = 11; // Busy TSS
  268. flush_gdt();
  269. return true;
  270. }
  271. static void initialize_redirection()
  272. {
  273. auto& descriptor = get_gdt_entry(s_redirection.selector);
  274. descriptor.set_base(&s_redirection.tss);
  275. descriptor.set_limit(0xffff);
  276. descriptor.dpl = 0;
  277. descriptor.segment_present = 1;
  278. descriptor.granularity = 1;
  279. descriptor.zero = 0;
  280. descriptor.operation_size = 1;
  281. descriptor.descriptor_type = 0;
  282. descriptor.type = 9;
  283. flush_gdt();
  284. }
  285. void Scheduler::prepare_for_iret_to_new_process()
  286. {
  287. auto& descriptor = get_gdt_entry(s_redirection.selector);
  288. descriptor.type = 9;
  289. s_redirection.tss.backlink = current->selector();
  290. load_task_register(s_redirection.selector);
  291. }
  292. void Scheduler::prepare_to_modify_tss(Process& process)
  293. {
  294. // This ensures that a currently running process modifying its own TSS
  295. // in order to yield() and end up somewhere else doesn't just end up
  296. // right after the yield().
  297. if (current == &process)
  298. load_task_register(s_redirection.selector);
  299. }
  300. Process* Scheduler::colonel()
  301. {
  302. return s_colonel_process;
  303. }
  304. void Scheduler::initialize()
  305. {
  306. s_redirection.selector = gdt_alloc_entry();
  307. initialize_redirection();
  308. s_colonel_process = Process::create_kernel_process("colonel", nullptr);
  309. load_task_register(s_redirection.selector);
  310. }
  311. void Scheduler::timer_tick(RegisterDump& regs)
  312. {
  313. if (!current)
  314. return;
  315. system.uptime++;
  316. if (current->tick())
  317. return;
  318. current->tss().gs = regs.gs;
  319. current->tss().fs = regs.fs;
  320. current->tss().es = regs.es;
  321. current->tss().ds = regs.ds;
  322. current->tss().edi = regs.edi;
  323. current->tss().esi = regs.esi;
  324. current->tss().ebp = regs.ebp;
  325. current->tss().ebx = regs.ebx;
  326. current->tss().edx = regs.edx;
  327. current->tss().ecx = regs.ecx;
  328. current->tss().eax = regs.eax;
  329. current->tss().eip = regs.eip;
  330. current->tss().cs = regs.cs;
  331. current->tss().eflags = regs.eflags;
  332. // Compute process stack pointer.
  333. // Add 12 for CS, EIP, EFLAGS (interrupt mechanic)
  334. current->tss().esp = regs.esp + 12;
  335. current->tss().ss = regs.ss;
  336. if ((current->tss().cs & 3) != 0) {
  337. current->tss().ss = regs.ss_if_crossRing;
  338. current->tss().esp = regs.esp_if_crossRing;
  339. }
  340. if (!pick_next())
  341. return;
  342. prepare_for_iret_to_new_process();
  343. // Set the NT (nested task) flag.
  344. asm(
  345. "pushf\n"
  346. "orl $0x00004000, (%esp)\n"
  347. "popf\n"
  348. );
  349. }