diff --git a/src/kernel/boot/kmain.c b/src/kernel/boot/kmain.c index 127be07..fe2b320 100644 --- a/src/kernel/boot/kmain.c +++ b/src/kernel/boot/kmain.c @@ -5,17 +5,17 @@ #include #include #include -#include +#include #include -int *thread_id = (int *)0x20000; +int *pid = (int *)0x20000; void thread_function() { - *thread_id = thread()->tid; + *pid = process()->pid; while(1) { - debug("Thread %d\n", *thread_id); + debug("Process %d\n", *pid); yield(); } } @@ -35,16 +35,16 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data) cpu_init(); - struct thread *th1 = new_thread(thread_function); - vmm_set_page(th1->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); - struct thread *th2 = new_thread(thread_function); - vmm_set_page(th2->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); - struct thread *th3 = new_thread(thread_function); - vmm_set_page(th3->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); + struct process *p1 = new_process(thread_function); + vmm_set_page(p1->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); + struct process *p2 = new_process(thread_function); + vmm_set_page(p2->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); + struct process *p3 = new_process(thread_function); + vmm_set_page(p3->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); - ready(th1); - ready(th2); - ready(th3); + ready(p1); + ready(p2); + ready(p3); debug_ok("Boot \"Complete\"\n"); diff --git a/src/kernel/include/thread.h b/src/kernel/include/process.h similarity index 70% rename from src/kernel/include/thread.h rename to src/kernel/include/process.h index ed212dd..c375907 100644 --- a/src/kernel/include/thread.h +++ b/src/kernel/include/process.h @@ -3,9 +3,9 @@ #include #include -struct thread +struct process { - uint64_t tid; + uint64_t pid; void *stack_ptr; uint64_t state; uint64_t P4; @@ -13,9 +13,9 @@ struct thread uint8_t stack[]; }; -struct thread *thread(); +struct process *process(); -struct thread *new_thread(void (*function)(void)); +struct process *new_process(void (*function)(void)); void yield(); void start_scheduler(); diff --git a/src/kernel/include/scheduler.h b/src/kernel/include/scheduler.h index a98c689..d8123ef 100644 --- a/src/kernel/include/scheduler.h +++ b/src/kernel/include/scheduler.h @@ -1,8 +1,8 @@ #pragma once #include -#define runQ readyQ, readyQ_next, struct thread +#define runQ readyQ, readyQ_next, struct process QUEUE_DECLARE(runQ); -void ready(struct thread *th); -struct thread *scheduler_next(); +void ready(struct process *proc); +struct process *scheduler_next(); diff --git a/src/kernel/proc/process.c b/src/kernel/proc/process.c new file mode 100644 index 0000000..ff2b57d --- /dev/null +++ b/src/kernel/proc/process.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +struct swtch_stack +{ + uint64_t RBP; + uint64_t RBX; + uint64_t R12; + uint64_t R13; + uint64_t R14; + uint64_t R15; + uint64_t RBP2; + uint64_t ret; +}__attribute__((packed)); + +struct process *sched_proc = 0; +struct process *_proc = 0; + +uint64_t next_pid = 1; +struct process *new_process(void (*function)(void)) +{ + struct process *proc = P2V(pmm_calloc()); + proc->pid = next_pid++; + proc->stack_ptr = incptr(proc, PAGE_SIZE - sizeof(struct swtch_stack)); + proc->P4 = new_P4(); + + struct swtch_stack *stk = proc->stack_ptr; + stk->RBP = (uint64_t)&stk->RBP2; + stk->ret = (uint64_t)function; + + return proc; +} + +struct process *process() +{ + return _proc; +} + +void yield() +{ + switch_stack(&_proc->stack_ptr, &sched_proc->stack_ptr); +} + +void scheduler() +{ + while(1) + { + struct process *new = 0; + while(!(new = scheduler_next())); + + _proc = new; + write_cr3(new->P4); + switch_stack(&sched_proc->stack_ptr, &new->stack_ptr); + + ready(_proc); + _proc = 0; + } +} + +void start_scheduler() +{ + sched_proc = new_process(scheduler); + sched_proc->pid = (uint64_t)-1; + + uint64_t stack; + switch_stack(&stack, &sched_proc->stack_ptr); +} diff --git a/src/kernel/proc/scheduler.c b/src/kernel/proc/scheduler.c index b1f7ef2..38ae733 100644 --- a/src/kernel/proc/scheduler.c +++ b/src/kernel/proc/scheduler.c @@ -1,15 +1,15 @@ #include #include -#include +#include QUEUE_DEFINE(runQ); -void ready(struct thread *th) +void ready(struct process *proc) { - queue_add(runQ, th); + queue_add(runQ, proc); } -struct thread *scheduler_next() +struct process *scheduler_next() { return queue_pop(runQ); } diff --git a/src/kernel/proc/thread.c b/src/kernel/proc/thread.c deleted file mode 100644 index 6b3f993..0000000 --- a/src/kernel/proc/thread.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include - -struct swtch_stack -{ - uint64_t RBP; - uint64_t RBX; - uint64_t R12; - uint64_t R13; - uint64_t R14; - uint64_t R15; - uint64_t RBP2; - uint64_t ret; -}__attribute__((packed)); - -struct thread *sched_th = 0; -struct thread *_thread = 0; - -uint64_t next_tid = 1; -struct thread *new_thread(void (*function)(void)) -{ - struct thread *th = P2V(pmm_calloc()); - th->tid = next_tid++; - th->stack_ptr = incptr(th, PAGE_SIZE - sizeof(struct swtch_stack)); - th->P4 = new_P4(); - - struct swtch_stack *stk = th->stack_ptr; - stk->RBP = (uint64_t)&stk->RBP2; - stk->ret = (uint64_t)function; - - return th; -} - -struct thread *thread() -{ - return _thread; -} - -void yield() -{ - switch_stack(&_thread->stack_ptr, &sched_th->stack_ptr); -} - -void scheduler() -{ - while(1) - { - struct thread *new = 0; - while(!(new = scheduler_next())); - - _thread = new; - write_cr3(new->P4); - 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); -}