old-MTGos-old/kernel/hal/x86/init/Multitasking.cpp

83 lines
2 KiB
C++
Raw Normal View History

#include <base.hpp>
#include <textDISP.hpp>
#include <Multitasking.hpp>
#include <serial.hpp>
#include <blockdev.hpp>
2016-06-16 18:10:07 +00:00
#include <idt.hpp>
2016-05-21 18:18:20 +00:00
#include <vmm3.hpp>
2016-06-16 18:10:07 +00:00
namespace MTGosHAL {
auto schedule(struct cpu_state* cpu) -> struct cpu_state* {
return MTGosHAL::tasks.schedule(cpu);
}
2016-04-16 17:59:17 +00:00
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;
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);
2016-06-16 18:10:07 +00:00
if(!idt.request(0x20,MTGosHAL::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*
{
2016-05-21 18:18:20 +00:00
void* tmp1, *tmp2;
2016-06-16 18:43:41 +00:00
mm >> tmp1 >> tmp2;
2016-05-21 18:18:20 +00:00
uint8_t *stack=(uint8_t*)tmp1, *user_stack=(uint8_t*)tmp2;
struct cpu_state new_state = {
2016-06-17 19:39:43 +00:00
0.0, //ST0
0.0, //ST1
0.0, //ST2
0.0, //ST3
0.0, //ST4
0.0, //ST5
0.0, //ST6
0.0, //ST7
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
2016-04-16 17:59:17 +00:00
(uint32_t) user_stack+4096, //ESP
0x20 | 0x03 //SS
};
2016-04-16 17:59:17 +00:00
struct cpu_state* state = (struct cpu_state*)(stack+4096-sizeof(new_state));
*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;
}
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;
2016-06-16 18:43:41 +00:00
struct cpu_state* cpu_state=next->unpause();
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;
}
} // namespace MTGosHAL