2015-10-18 19:57:31 +00:00
|
|
|
#include <base.hpp>
|
|
|
|
#include <textDISP.hpp>
|
2016-04-15 20:04:01 +00:00
|
|
|
#include <Multitasking.hpp>
|
2015-10-18 19:57:31 +00:00
|
|
|
#include <serial.hpp>
|
2016-04-15 20:04:01 +00:00
|
|
|
#include <blockdev.hpp>
|
2016-04-16 17:59:17 +00:00
|
|
|
#include <pmm.hpp>
|
2015-10-18 19:57:31 +00:00
|
|
|
auto schedule(struct cpu_state* cpu) -> struct cpu_state* {
|
|
|
|
return MTGosHAL::tasks.schedule(cpu);
|
|
|
|
}
|
|
|
|
namespace MTGosHAL {
|
|
|
|
|
2016-04-16 17:59:17 +00:00
|
|
|
Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr)
|
2015-10-18 19:57:31 +00:00
|
|
|
{
|
2016-04-15 20:04:01 +00:00
|
|
|
for(int i=0;i<32;i++) {
|
|
|
|
if(i==2)
|
|
|
|
continue;
|
|
|
|
tss[i]=0;
|
|
|
|
}
|
|
|
|
tss[2]=0x10;
|
2016-04-16 17:59:17 +00:00
|
|
|
//task_states[0] = initTask(stack_a, user_stack_a, task_a);
|
|
|
|
//task_states[1] = initTask(stack_b, user_stack_b, task_b);
|
2015-10-18 19:57:31 +00:00
|
|
|
if(!idt.request(0x20,::schedule)) {
|
|
|
|
err << "Could not start multitasking\nFatal error; Kernel halted!\n";
|
|
|
|
while(true);
|
|
|
|
asm volatile("cli; hlt");
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 17:59:17 +00:00
|
|
|
auto Multitasking::initTask(void(* entry)()) -> struct cpu_state*
|
2015-10-18 19:57:31 +00:00
|
|
|
{
|
2016-04-16 17:59:17 +00:00
|
|
|
void *tmp1, *tmp2;
|
|
|
|
mm >> tmp1 >> tmp2;
|
|
|
|
uint8_t *stack=(uint8_t*)tmp1, *user_stack=(uint8_t*)tmp2;
|
2015-10-18 19:57:31 +00:00
|
|
|
struct cpu_state new_state = {
|
|
|
|
0, //EAX
|
|
|
|
0, //EBX
|
|
|
|
0, //ECX
|
|
|
|
0, //EDX
|
|
|
|
0, //ESI
|
|
|
|
0, //EDI
|
|
|
|
0, //EBP
|
|
|
|
0, //INTR
|
|
|
|
0, //ERROR
|
|
|
|
(uint32_t) entry, //EIP
|
2016-04-15 20:04:01 +00:00
|
|
|
0x18 | 0x03, //CS
|
2015-10-18 19:57:31 +00:00
|
|
|
0x202, // EFLAGS
|
2016-04-16 17:59:17 +00:00
|
|
|
(uint32_t) user_stack+4096, //ESP
|
2016-04-15 20:04:01 +00:00
|
|
|
0x20 | 0x03 //SS
|
2015-10-18 19:57:31 +00:00
|
|
|
};
|
2016-04-16 17:59:17 +00:00
|
|
|
struct cpu_state* state = (struct cpu_state*)(stack+4096-sizeof(new_state));
|
2015-10-18 19:57:31 +00:00
|
|
|
*state = new_state;
|
2016-04-16 17:59:17 +00:00
|
|
|
//Create new task class
|
|
|
|
Task* task = new Task(state);
|
|
|
|
if(first_task)
|
|
|
|
first_task->addTask(task);
|
|
|
|
else {
|
|
|
|
first_task=task;
|
|
|
|
}
|
2015-10-18 19:57:31 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state*
|
|
|
|
{
|
2016-04-22 13:07:52 +00:00
|
|
|
Task* next=nullptr;
|
|
|
|
if(curr_task) {
|
|
|
|
next=curr_task->pause(cpu);
|
|
|
|
}
|
|
|
|
if (!next) {
|
2016-04-16 17:59:17 +00:00
|
|
|
next=first_task;
|
2016-04-22 13:07:52 +00:00
|
|
|
}
|
2016-04-16 17:59:17 +00:00
|
|
|
curr_task=next;
|
|
|
|
return next->unpause();
|
|
|
|
}
|
|
|
|
Task::Task(struct cpu_state* cpu): cpu_state(cpu), next(nullptr) {};
|
|
|
|
//This is run every time this task is chosen by the scheduler
|
|
|
|
auto Task::unpause() -> struct cpu_state* {
|
2016-04-22 13:07:52 +00:00
|
|
|
MTGosHAL::tasks.tss[1] = (uint32_t) (cpu_state + 1);
|
2016-04-16 17:59:17 +00:00
|
|
|
return cpu_state;
|
|
|
|
}
|
|
|
|
//This is run every time the timer ticks and a task is running
|
|
|
|
auto Task::pause(struct cpu_state* cpu) -> Task * {
|
|
|
|
cpu_state=cpu;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
auto Task::addTask(Task* task) -> void {
|
|
|
|
if(next)
|
|
|
|
return next->addTask(task);
|
|
|
|
next=task;
|
2015-10-18 19:57:31 +00:00
|
|
|
}
|
2016-04-22 13:07:52 +00:00
|
|
|
auto Task::hasNext() -> bool {
|
|
|
|
return next!=nullptr;
|
|
|
|
}
|
2015-10-18 19:57:31 +00:00
|
|
|
} // namespace MTGosHAL
|