Adding a scheduler function and cleanup

This commit is contained in:
Thomas Lovén 2018-03-29 12:28:10 +02:00
parent b5f5a40fbc
commit a39fdcf3ce
3 changed files with 40 additions and 42 deletions

View File

@ -10,7 +10,7 @@
void thread_function() void thread_function()
{ {
int thread_id = current_thread()->tid; int thread_id = thread()->tid;
while(1) while(1)
{ {
@ -40,7 +40,8 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data)
ready(new_thread(thread_function)); ready(new_thread(thread_function));
ready(new_thread(thread_function)); ready(new_thread(thread_function));
ready(new_thread(thread_function)); ready(new_thread(thread_function));
yield();
start_scheduler();
PANIC("Reached end of kernel main function\n"); PANIC("Reached end of kernel main function\n");
for(;;); for(;;);

View File

@ -5,15 +5,17 @@
struct thread struct thread
{ {
uintptr_t stack_ptr;
uint64_t tid; uint64_t tid;
void *stack_ptr;
uint64_t state; uint64_t state;
QUEUE_SPOT(runQ); QUEUE_SPOT(runQ);
uint8_t stack[];
}; };
struct thread *thread();
struct thread *new_thread(void (*function)(void)); struct thread *new_thread(void (*function)(void));
void yield(); void yield();
struct thread *current_thread(); void start_scheduler();
void switch_stack(void *old_ptr, void *new_ptr); void switch_stack(void *old_ptr, void *new_ptr);

View File

@ -2,12 +2,8 @@
#include <scheduler.h> #include <scheduler.h>
#include <memory.h> #include <memory.h>
#define TCB_OFFSET (PAGE_SIZE - sizeof(struct thread)) struct swtch_stack
#define SWTCH_STACK_SIZE (0x8*8)
struct thread_stack
{ {
uint8_t stack[TCB_OFFSET-SWTCH_STACK_SIZE];
uint64_t RBP; uint64_t RBP;
uint64_t RBX; uint64_t RBX;
uint64_t R12; uint64_t R12;
@ -16,56 +12,55 @@ struct thread_stack
uint64_t R15; uint64_t R15;
uint64_t RBP2; uint64_t RBP2;
uint64_t ret; uint64_t ret;
struct thread tcb;
}__attribute__((packed)); }__attribute__((packed));
#define thread_stack(th) (struct thread_stack *)((uintptr_t)th - TCB_OFFSET) struct thread *sched_th = 0;
struct thread *_thread = 0;
struct thread boot_thread;
struct thread *_current_thread = 0;
uint64_t next_tid = 1; uint64_t next_tid = 1;
struct thread *new_thread(void (*function)(void)) struct thread *new_thread(void (*function)(void))
{ {
struct thread_stack *stk = P2V(pmm_alloc()); struct thread *th = P2V(pmm_calloc());
struct thread *th = &stk->tcb;
th->tid = next_tid++; th->tid = next_tid++;
th->stack_ptr = incptr(th, PAGE_SIZE - sizeof(struct swtch_stack));
struct swtch_stack *stk = th->stack_ptr;
stk->RBP = (uint64_t)&stk->RBP2; stk->RBP = (uint64_t)&stk->RBP2;
stk->ret = (uint64_t)function; stk->ret = (uint64_t)function;
th->stack_ptr = (uint64_t)&stk->RBP;
return th; return th;
} }
struct thread *current_thread() struct thread *thread()
{ {
return _current_thread; return _thread;
}
void set_current_thread(struct thread *th)
{
_current_thread = th;
}
void switch_thread(struct thread *old, struct thread *new)
{
set_current_thread(new);
switch_stack(&old->stack_ptr, &new->stack_ptr);
} }
void yield() void yield()
{ {
struct thread *old, *new; switch_stack(&_thread->stack_ptr, &sched_th->stack_ptr);
}
old = current_thread();
if(old) void scheduler()
ready(old); {
else while(1)
old = &boot_thread; {
while(!(new = scheduler_next())); struct thread *new = 0;
while(!(new = scheduler_next()));
switch_thread(old, new);
_thread = new;
switch_stack(&sched_th->stack_ptr, &new->stack_ptr);
ready(_thread);
_thread = 0;
}
}
void start_scheduler()
{
sched_th = new_thread(scheduler);
sched_th->tid = (uint64_t)-1;
uint64_t stack;
switch_stack(&stack, &sched_th->stack_ptr);
} }