old-MTGos-old/kernel/hal/x86/init/Multitasking.cpp
2016-04-16 19:59:17 +02:00

104 lines
2.5 KiB
C++

#include <base.hpp>
#include <textDISP.hpp>
#include <Multitasking.hpp>
#include <serial.hpp>
#include <blockdev.hpp>
#include <pmm.hpp>
auto schedule(struct cpu_state* cpu) -> struct cpu_state* {
return MTGosHAL::tasks.schedule(cpu);
}
namespace MTGosHAL {
Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr)
{
for(int i=0;i<32;i++) {
if(i==2)
continue;
tss[i]=0;
}
tss[2]=0x10;
//task_states[0] = initTask(stack_a, user_stack_a, task_a);
//task_states[1] = initTask(stack_b, user_stack_b, task_b);
if(!idt.request(0x20,::schedule)) {
err << "Could not start multitasking\nFatal error; Kernel halted!\n";
while(true);
asm volatile("cli; hlt");
}
}
auto Multitasking::initTask(void(* entry)()) -> struct cpu_state*
{
void *tmp1, *tmp2;
mm >> tmp1 >> tmp2;
uint8_t *stack=(uint8_t*)tmp1, *user_stack=(uint8_t*)tmp2;
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
0x18 | 0x03, //CS
0x202, // EFLAGS
(uint32_t) user_stack+4096, //ESP
0x20 | 0x03 //SS
};
struct cpu_state* state = (struct cpu_state*)(stack+4096-sizeof(new_state));
*state = new_state;
//Create new task class
Task* task = new Task(state);
if(first_task)
first_task->addTask(task);
else {
first_task=task;
}
curr_task=first_task;
return state;
}
auto Multitasking::task_a() -> void
{
while(true) {
char arr[513];
arr[512]=0;
disk.readSector(disk.getDriveNumByName("ATA0m"),0,(uint8_t*)arr);
out << arr;
}
}
auto Multitasking::task_b() -> void
{
while(true) {
char arr[513];
arr[512]=0;
disk.readSector(disk.getDriveNumByName("ATA0m"),1,(uint8_t*)arr);
out << arr;
}
}
auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state*
{
Task* next=curr_task->pause(cpu);
if(next==nullptr)
next=first_task;
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* {
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;
}
} // namespace MTGosHAL