2018-11-08 00:15:02 +03:00
|
|
|
#include "Scheduler.h"
|
|
|
|
#include "Process.h"
|
|
|
|
#include "system.h"
|
2019-02-01 05:50:06 +03:00
|
|
|
#include "RTC.h"
|
|
|
|
#include "i8253.h"
|
2019-02-06 17:05:47 +03:00
|
|
|
#include <AK/TemporaryChange.h>
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2018-11-08 00:24:20 +03:00
|
|
|
//#define LOG_EVERY_CONTEXT_SWITCH
|
2018-11-08 00:15:02 +03:00
|
|
|
//#define SCHEDULER_DEBUG
|
|
|
|
|
2019-02-07 14:21:17 +03:00
|
|
|
static dword time_slice_for(Process::Priority priority)
|
|
|
|
{
|
|
|
|
// One time slice unit == 1ms
|
|
|
|
switch (priority) {
|
|
|
|
case Process::HighPriority:
|
|
|
|
return 50;
|
2019-02-12 14:11:22 +03:00
|
|
|
case Process::NormalPriority:
|
|
|
|
return 15;
|
|
|
|
case Process::LowPriority:
|
|
|
|
return 5;
|
2019-02-07 14:21:17 +03:00
|
|
|
}
|
2019-02-12 14:11:22 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-07 14:21:17 +03:00
|
|
|
}
|
2018-11-08 00:15:02 +03:00
|
|
|
|
|
|
|
Process* current;
|
2019-01-25 09:52:44 +03:00
|
|
|
Process* g_last_fpu_process;
|
2019-02-06 20:45:21 +03:00
|
|
|
Process* g_finalizer;
|
2018-11-08 00:15:02 +03:00
|
|
|
static Process* s_colonel_process;
|
|
|
|
|
2018-11-08 01:13:38 +03:00
|
|
|
struct TaskRedirectionData {
|
|
|
|
word selector;
|
|
|
|
TSS32 tss;
|
|
|
|
};
|
|
|
|
static TaskRedirectionData s_redirection;
|
2019-02-06 17:05:47 +03:00
|
|
|
static bool s_active;
|
|
|
|
|
|
|
|
bool Scheduler::is_active()
|
|
|
|
{
|
|
|
|
return s_active;
|
|
|
|
}
|
2018-11-08 01:13:38 +03:00
|
|
|
|
2018-11-08 00:15:02 +03:00
|
|
|
bool Scheduler::pick_next()
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-02-06 17:05:47 +03:00
|
|
|
ASSERT(!s_active);
|
|
|
|
|
|
|
|
TemporaryChange<bool> change(s_active, true);
|
|
|
|
|
|
|
|
ASSERT(s_active);
|
2018-11-08 00:15:02 +03:00
|
|
|
|
|
|
|
if (!current) {
|
|
|
|
// XXX: The first ever context_switch() goes to the idle process.
|
|
|
|
// This to setup a reliable place we can return to.
|
|
|
|
return context_switch(*s_colonel_process);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check and unblock processes whose wait conditions have been met.
|
2019-02-01 05:50:06 +03:00
|
|
|
Process::for_each([] (Process& process) {
|
2018-11-08 00:15:02 +03:00
|
|
|
if (process.state() == Process::BlockedSleep) {
|
2019-01-31 19:31:23 +03:00
|
|
|
if (process.wakeup_time() <= system.uptime)
|
2018-11-08 00:15:02 +03:00
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.state() == Process::BlockedWait) {
|
2018-11-11 17:36:40 +03:00
|
|
|
process.for_each_child([&process] (Process& child) {
|
|
|
|
if (child.state() != Process::Dead)
|
|
|
|
return true;
|
2018-11-29 01:30:06 +03:00
|
|
|
if (process.waitee_pid() == -1 || process.waitee_pid() == child.pid()) {
|
|
|
|
process.m_waitee_pid = child.pid();
|
2018-11-11 17:36:40 +03:00
|
|
|
process.unblock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2018-11-08 00:15:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.state() == Process::BlockedRead) {
|
2018-12-03 02:42:48 +03:00
|
|
|
ASSERT(process.m_blocked_fd != -1);
|
2018-11-08 00:15:02 +03:00
|
|
|
// FIXME: Block until the amount of data wanted is available.
|
2019-01-16 02:47:00 +03:00
|
|
|
if (process.m_fds[process.m_blocked_fd].descriptor->can_read(process))
|
2018-11-08 00:15:02 +03:00
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-12 03:28:46 +03:00
|
|
|
if (process.state() == Process::BlockedWrite) {
|
|
|
|
ASSERT(process.m_blocked_fd != -1);
|
2019-01-15 11:17:22 +03:00
|
|
|
if (process.m_fds[process.m_blocked_fd].descriptor->can_write(process))
|
2018-11-12 03:28:46 +03:00
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:18:35 +03:00
|
|
|
if (process.state() == Process::BlockedConnect) {
|
|
|
|
ASSERT(process.m_blocked_connecting_socket);
|
|
|
|
if (process.m_blocked_connecting_socket->is_connected())
|
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-16 01:12:20 +03:00
|
|
|
if (process.state() == Process::BlockedSelect) {
|
2019-01-16 19:20:58 +03:00
|
|
|
if (process.wakeup_requested()) {
|
|
|
|
process.m_wakeup_requested = false;
|
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-01 05:50:06 +03:00
|
|
|
if (process.m_select_has_timeout) {
|
|
|
|
auto now_sec = RTC::now();
|
|
|
|
auto now_usec = PIT::ticks_since_boot() % 1000;
|
|
|
|
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)) {
|
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 01:12:20 +03:00
|
|
|
for (int fd : process.m_select_read_fds) {
|
2019-01-16 02:47:00 +03:00
|
|
|
if (process.m_fds[fd].descriptor->can_read(process)) {
|
2019-01-16 01:12:20 +03:00
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int fd : process.m_select_write_fds) {
|
|
|
|
if (process.m_fds[fd].descriptor->can_write(process)) {
|
|
|
|
process.unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:21:32 +03:00
|
|
|
if (process.state() == Process::Skip1SchedulerPass) {
|
|
|
|
process.set_state(Process::Skip0SchedulerPasses);
|
2018-11-08 01:13:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:21:32 +03:00
|
|
|
if (process.state() == Process::Skip0SchedulerPasses) {
|
2018-11-08 01:13:38 +03:00
|
|
|
process.set_state(Process::Runnable);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:24:20 +03:00
|
|
|
if (process.state() == Process::Dead) {
|
2018-11-29 00:01:24 +03:00
|
|
|
if (current != &process && !Process::from_pid(process.ppid())) {
|
|
|
|
auto name = process.name();
|
|
|
|
auto pid = process.pid();
|
|
|
|
auto exit_status = Process::reap(process);
|
2019-01-01 05:56:39 +03:00
|
|
|
dbgprintf("reaped unparented process %s(%u), exit status: %u\n", name.characters(), pid, exit_status);
|
2018-11-29 00:01:24 +03:00
|
|
|
}
|
2018-11-08 00:24:20 +03:00
|
|
|
return true;
|
2018-11-08 01:59:49 +03:00
|
|
|
}
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2019-02-06 20:45:21 +03:00
|
|
|
if (process.state() == Process::Dying) {
|
|
|
|
ASSERT(g_finalizer);
|
|
|
|
if (g_finalizer->state() == Process::BlockedLurking)
|
|
|
|
g_finalizer->unblock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:15:02 +03:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Dispatch any pending signals.
|
|
|
|
// FIXME: Do we really need this to be a separate pass over the process list?
|
2019-02-06 19:28:14 +03:00
|
|
|
Process::for_each_living([] (auto& process) {
|
2018-11-08 00:15:02 +03:00
|
|
|
if (!process.has_unmasked_pending_signals())
|
|
|
|
return true;
|
|
|
|
// We know how to interrupt blocked processes, but if they are just executing
|
|
|
|
// at some random point in the kernel, let them continue. They'll be in userspace
|
|
|
|
// sooner or later and we can deliver the signal then.
|
|
|
|
// FIXME: Maybe we could check when returning from a syscall if there's a pending
|
|
|
|
// signal and dispatch it then and there? Would that be doable without the
|
|
|
|
// syscall effectively being "interrupted" despite having completed?
|
|
|
|
if (process.in_kernel() && !process.is_blocked())
|
|
|
|
return true;
|
2018-11-29 01:30:06 +03:00
|
|
|
// NOTE: dispatch_one_pending_signal() may unblock the process.
|
|
|
|
bool was_blocked = process.is_blocked();
|
2019-02-04 16:06:38 +03:00
|
|
|
if (process.dispatch_one_pending_signal() == ShouldUnblockProcess::No)
|
2018-11-16 23:14:25 +03:00
|
|
|
return true;
|
2018-11-29 01:30:06 +03:00
|
|
|
if (was_blocked) {
|
|
|
|
dbgprintf("Unblock %s(%u) due to signal\n", process.name().characters(), process.pid());
|
2018-11-08 00:15:02 +03:00
|
|
|
process.m_was_interrupted_while_blocked = true;
|
|
|
|
process.unblock();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
#ifdef SCHEDULER_DEBUG
|
|
|
|
dbgprintf("Scheduler choices:\n");
|
|
|
|
for (auto* process = g_processes->head(); process; process = process->next()) {
|
|
|
|
//if (process->state() == Process::BlockedWait || process->state() == Process::BlockedSleep)
|
|
|
|
// continue;
|
2019-01-31 19:31:23 +03:00
|
|
|
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);
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
auto* previous_head = g_processes->head();
|
2018-11-08 00:15:02 +03:00
|
|
|
for (;;) {
|
|
|
|
// Move head to tail.
|
2018-12-21 04:10:45 +03:00
|
|
|
g_processes->append(g_processes->remove_head());
|
2018-11-08 00:15:02 +03:00
|
|
|
auto* process = g_processes->head();
|
|
|
|
|
2019-02-06 20:45:21 +03:00
|
|
|
if (process->state() == Process::Runnable || process->state() == Process::Running) {
|
2018-11-08 00:15:02 +03:00
|
|
|
#ifdef SCHEDULER_DEBUG
|
2018-11-08 01:13:38 +03:00
|
|
|
dbgprintf("switch to %s(%u) @ %w:%x\n", process->name().characters(), process->pid(), process->tss().cs, process->tss().eip);
|
2018-11-08 00:15:02 +03:00
|
|
|
#endif
|
|
|
|
return context_switch(*process);
|
|
|
|
}
|
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
if (process == previous_head) {
|
2018-11-08 01:13:38 +03:00
|
|
|
// Back at process_head, nothing wants to run. Send in the colonel!
|
2018-11-08 00:15:02 +03:00
|
|
|
return context_switch(*s_colonel_process);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 13:12:23 +03:00
|
|
|
bool Scheduler::donate_to(Process* beneficiary, const char* reason)
|
|
|
|
{
|
|
|
|
(void)reason;
|
|
|
|
unsigned ticks_left = current->ticks_left();
|
|
|
|
if (!beneficiary || beneficiary->state() != Process::Runnable || ticks_left <= 1) {
|
|
|
|
return yield();
|
|
|
|
}
|
|
|
|
|
2019-02-09 00:06:30 +03:00
|
|
|
unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(beneficiary->priority()));
|
2019-02-07 13:12:23 +03:00
|
|
|
#ifdef SCHEDULER_DEBUG
|
|
|
|
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);
|
|
|
|
#endif
|
|
|
|
context_switch(*beneficiary);
|
|
|
|
beneficiary->set_ticks_left(ticks_to_donate);
|
|
|
|
switch_now();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:15:02 +03:00
|
|
|
bool Scheduler::yield()
|
|
|
|
{
|
2019-01-23 07:05:45 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-02-06 17:05:47 +03:00
|
|
|
ASSERT(current);
|
2018-11-08 00:15:02 +03:00
|
|
|
//dbgprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
|
|
|
|
|
2019-02-06 17:05:47 +03:00
|
|
|
if (!pick_next())
|
2018-11-08 00:15:02 +03:00
|
|
|
return 1;
|
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
//dbgprintf("yield() jumping to new process: %x (%s)\n", current->far_ptr().selector, current->name().characters());
|
2018-11-08 00:15:02 +03:00
|
|
|
switch_now();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::pick_next_and_switch_now()
|
|
|
|
{
|
|
|
|
bool someone_wants_to_run = pick_next();
|
|
|
|
ASSERT(someone_wants_to_run);
|
|
|
|
switch_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::switch_now()
|
|
|
|
{
|
2018-12-03 02:39:25 +03:00
|
|
|
Descriptor& descriptor = get_gdt_entry(current->selector());
|
2018-11-08 00:15:02 +03:00
|
|
|
descriptor.type = 9;
|
2018-12-03 02:39:25 +03:00
|
|
|
flush_gdt();
|
2018-11-08 00:15:02 +03:00
|
|
|
asm("sti\n"
|
|
|
|
"ljmp *(%%eax)\n"
|
2019-01-31 19:31:23 +03:00
|
|
|
::"a"(¤t->far_ptr())
|
2018-11-08 00:15:02 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scheduler::context_switch(Process& process)
|
|
|
|
{
|
2019-02-07 14:21:17 +03:00
|
|
|
process.set_ticks_left(time_slice_for(process.priority()));
|
2018-11-08 00:15:02 +03:00
|
|
|
process.did_schedule();
|
|
|
|
|
|
|
|
if (current == &process)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (current) {
|
|
|
|
// If the last process hasn't blocked (still marked as running),
|
|
|
|
// mark it as runnable for the next round.
|
|
|
|
if (current->state() == Process::Running)
|
|
|
|
current->set_state(Process::Runnable);
|
2018-11-08 00:24:20 +03:00
|
|
|
|
|
|
|
#ifdef LOG_EVERY_CONTEXT_SWITCH
|
2019-02-05 10:32:32 +03:00
|
|
|
dbgprintf("Scheduler: %s(%u) -> %s(%u) %w:%x\n",
|
|
|
|
current->name().characters(), current->pid(),
|
|
|
|
process.name().characters(), process.pid(),
|
|
|
|
process.tss().cs, process.tss().eip);
|
2018-11-08 00:24:20 +03:00
|
|
|
#endif
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
current = &process;
|
|
|
|
process.set_state(Process::Running);
|
|
|
|
|
|
|
|
#ifdef COOL_GLOBALS
|
|
|
|
g_cool_globals->current_pid = process.pid();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!process.selector()) {
|
2019-01-31 19:31:23 +03:00
|
|
|
process.set_selector(gdt_alloc_entry());
|
2018-12-03 02:39:25 +03:00
|
|
|
auto& descriptor = get_gdt_entry(process.selector());
|
2019-01-31 19:31:23 +03:00
|
|
|
descriptor.set_base(&process.tss());
|
|
|
|
descriptor.set_limit(0xffff);
|
2018-11-08 00:15:02 +03:00
|
|
|
descriptor.dpl = 0;
|
|
|
|
descriptor.segment_present = 1;
|
|
|
|
descriptor.granularity = 1;
|
|
|
|
descriptor.zero = 0;
|
|
|
|
descriptor.operation_size = 1;
|
|
|
|
descriptor.descriptor_type = 0;
|
|
|
|
}
|
|
|
|
|
2018-12-03 02:39:25 +03:00
|
|
|
auto& descriptor = get_gdt_entry(process.selector());
|
2018-11-08 00:15:02 +03:00
|
|
|
descriptor.type = 11; // Busy TSS
|
2018-12-03 02:39:25 +03:00
|
|
|
flush_gdt();
|
2018-11-08 00:15:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:13:38 +03:00
|
|
|
static void initialize_redirection()
|
2018-11-08 00:15:02 +03:00
|
|
|
{
|
2018-12-03 02:39:25 +03:00
|
|
|
auto& descriptor = get_gdt_entry(s_redirection.selector);
|
2019-01-31 19:31:23 +03:00
|
|
|
descriptor.set_base(&s_redirection.tss);
|
|
|
|
descriptor.set_limit(0xffff);
|
2018-11-08 01:13:38 +03:00
|
|
|
descriptor.dpl = 0;
|
|
|
|
descriptor.segment_present = 1;
|
|
|
|
descriptor.granularity = 1;
|
|
|
|
descriptor.zero = 0;
|
|
|
|
descriptor.operation_size = 1;
|
|
|
|
descriptor.descriptor_type = 0;
|
|
|
|
descriptor.type = 9;
|
2018-12-03 02:39:25 +03:00
|
|
|
flush_gdt();
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::prepare_for_iret_to_new_process()
|
|
|
|
{
|
2018-12-03 02:39:25 +03:00
|
|
|
auto& descriptor = get_gdt_entry(s_redirection.selector);
|
2018-11-08 01:13:38 +03:00
|
|
|
descriptor.type = 9;
|
|
|
|
s_redirection.tss.backlink = current->selector();
|
|
|
|
load_task_register(s_redirection.selector);
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
|
2018-11-08 01:59:49 +03:00
|
|
|
void Scheduler::prepare_to_modify_tss(Process& process)
|
2018-11-08 00:15:02 +03:00
|
|
|
{
|
2018-11-08 01:59:49 +03:00
|
|
|
// This ensures that a currently running process modifying its own TSS
|
|
|
|
// in order to yield() and end up somewhere else doesn't just end up
|
|
|
|
// right after the yield().
|
|
|
|
if (current == &process)
|
|
|
|
load_task_register(s_redirection.selector);
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:28:12 +03:00
|
|
|
Process* Scheduler::colonel()
|
|
|
|
{
|
|
|
|
return s_colonel_process;
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:15:02 +03:00
|
|
|
void Scheduler::initialize()
|
|
|
|
{
|
2018-11-08 01:13:38 +03:00
|
|
|
s_redirection.selector = gdt_alloc_entry();
|
|
|
|
initialize_redirection();
|
2018-12-25 01:10:48 +03:00
|
|
|
s_colonel_process = Process::create_kernel_process("colonel", nullptr);
|
2018-11-08 01:13:38 +03:00
|
|
|
load_task_register(s_redirection.selector);
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
2018-11-08 02:24:59 +03:00
|
|
|
|
|
|
|
void Scheduler::timer_tick(RegisterDump& regs)
|
|
|
|
{
|
|
|
|
if (!current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
system.uptime++;
|
|
|
|
|
|
|
|
if (current->tick())
|
|
|
|
return;
|
|
|
|
|
|
|
|
current->tss().gs = regs.gs;
|
|
|
|
current->tss().fs = regs.fs;
|
|
|
|
current->tss().es = regs.es;
|
|
|
|
current->tss().ds = regs.ds;
|
|
|
|
current->tss().edi = regs.edi;
|
|
|
|
current->tss().esi = regs.esi;
|
|
|
|
current->tss().ebp = regs.ebp;
|
|
|
|
current->tss().ebx = regs.ebx;
|
|
|
|
current->tss().edx = regs.edx;
|
|
|
|
current->tss().ecx = regs.ecx;
|
|
|
|
current->tss().eax = regs.eax;
|
|
|
|
current->tss().eip = regs.eip;
|
|
|
|
current->tss().cs = regs.cs;
|
|
|
|
current->tss().eflags = regs.eflags;
|
|
|
|
|
|
|
|
// Compute process stack pointer.
|
|
|
|
// Add 12 for CS, EIP, EFLAGS (interrupt mechanic)
|
|
|
|
current->tss().esp = regs.esp + 12;
|
|
|
|
current->tss().ss = regs.ss;
|
|
|
|
|
|
|
|
if ((current->tss().cs & 3) != 0) {
|
|
|
|
current->tss().ss = regs.ss_if_crossRing;
|
|
|
|
current->tss().esp = regs.esp_if_crossRing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pick_next())
|
|
|
|
return;
|
|
|
|
prepare_for_iret_to_new_process();
|
|
|
|
|
|
|
|
// Set the NT (nested task) flag.
|
|
|
|
asm(
|
|
|
|
"pushf\n"
|
|
|
|
"orl $0x00004000, (%esp)\n"
|
|
|
|
"popf\n"
|
|
|
|
);
|
|
|
|
}
|