[MULTITASKING] Preemptive multitasking

This commit is contained in:
2016-11-18 10:36:21 +01:00
parent d3d065b2e2
commit 664379b35c
7 changed files with 26 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
#include <list.h>
#include <debug.h>
#include <sse.h>
#include <apic.h>
LIST(thread_t, ready_queue);
@@ -39,6 +40,7 @@ void scheduler()
{
while(1)
{
apic_timer_stop();
thread_t *old = 0, *new = 0;
if((old = get_last_thread()))
{
@@ -51,7 +53,8 @@ void scheduler()
}
}
while(!(new = scheduler_next()));
while(!(new = scheduler_next()))asm("sti");
asm("cli");
if(!process_alive(new->process))
{
@@ -62,6 +65,7 @@ void scheduler()
set_last_thread(new);
switch_process(new->process);
sse_restore(new->sse_registers);
apic_timer(1000000);
switch_thread(scheduler_th, new);
}
}
@@ -71,6 +75,8 @@ void schedule()
// This function handles swithing to the next thread in the ready queue
thread_t *old = get_current_thread();
if(old == scheduler_th)
return;
if(old)
{

View File

@@ -7,6 +7,7 @@ swtch:
# Switches stacks preserving callee preserved registers according to System V ABI
push rbp
mov rbp, rsp
push rdi
push r15
push r14
push r13
@@ -24,5 +25,6 @@ swtch:
pop r13
pop r14
pop r15
pop rdi
leaveq
ret

View File

@@ -4,6 +4,7 @@
#include <mem.h>
#include <list.h>
#include <debug.h>
#include <registers.h>
thread_t *current_thread = 0;
@@ -16,9 +17,16 @@ thread_t *new_thread(void (*func)(void))
thread_stack_t *stack = kcalloc(1, sizeof(thread_stack_t));
thread_t *th = &stack->tcb;
stack->function_address = (uint64_t)func;
stack->thread = (uint64_t)th;
stack->function_address = (uint64_t)isr_return;
stack->RBP = (uint64_t)&stack->zero_frame;
th->r.rip = (uint64_t)func;
th->r.rflags = RFLAGS_IF;
th->r.cs = 0x8;
th->r.ss = 0x10;
th->r.rsp = (uint64_t)&th->stack_pointer;
th->tid = tid++;
th->state = THREAD_STATE_READY;
th->stack_pointer = (uint64_t)&stack->RBP;