Scheduler.cpp 12 KB

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