From f4c3fb30b762724ca8fc09602ea05474d967cf42 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 30 Sep 2019 12:10:33 +0300 Subject: [PATCH] [amd64] Move kidle init to a function --- include/sys/thread.h | 9 ++++++- sys/amd64/sys/kidle.c | 37 ++++++++------------------- sys/amd64/sys/thread.c | 57 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/include/sys/thread.h b/include/sys/thread.h index cd25cfa..ddc88e2 100644 --- a/include/sys/thread.h +++ b/include/sys/thread.h @@ -55,7 +55,14 @@ int thread_info_init(thread_info_t *info); * @brief Setup thread data structures * @param t Thread */ -int thread_init(thread_t *t); +int thread_init(thread_t *t, + mm_space_t space, + uintptr_t ip, + uintptr_t kstack_base, + size_t kstack_size, + uintptr_t ustack_base, + size_t ustack_size, + uint32_t flags); /** * @brief Set thread instruction pointer diff --git a/sys/amd64/sys/kidle.c b/sys/amd64/sys/kidle.c index 6fa6501..b78939c 100644 --- a/sys/amd64/sys/kidle.c +++ b/sys/amd64/sys/kidle.c @@ -1,4 +1,5 @@ #include "sys/thread.h" +#include "sys/assert.h" #include "sys/sched.h" #include "sys/kidle.h" #include "sys/debug.h" @@ -8,38 +9,20 @@ static char kidle_kstack[8192]; static thread_t kidle_thread; static void kidle(void) { - // Debug: print time ~each second - uint32_t v_p = 0; while (1) { - uint32_t v = systick / SYSTICK_RES; - if (v != v_p) { - kdebug("%d\n", v); - v_p = v; - } asm volatile ("sti; hlt"); } } void kidle_init(void) { - // TODO: functions for thread creation including kthread creation - thread_info_init(&kidle_thread.info); - - kidle_thread.info.flags = THREAD_KERNEL; - - kidle_thread.kstack_base = (uintptr_t) kidle_kstack; - kidle_thread.kstack_size = 4096; - kidle_thread.kstack_ptr = kidle_thread.kstack_base + kidle_thread.kstack_size - sizeof(amd64_thread_context_t); - - amd64_thread_context_t *ctx = (amd64_thread_context_t *) kidle_thread.kstack_ptr; - - ctx->cr3 = (uintptr_t) mm_kernel - 0xFFFFFF0000000000; - ctx->rip = (uintptr_t) kidle; - ctx->cs = 0x08; - ctx->ss = 0x10; - ctx->rsp = kidle_thread.kstack_ptr; - ctx->rflags = 0x248; - - kidle_thread.next = NULL; - + assert(thread_init(&kidle_thread, + mm_kernel, + (uintptr_t) kidle, + (uintptr_t) kidle_kstack, + sizeof(kidle_kstack), + 0, + 0, + THREAD_KERNEL) == 0, + "Failed to set kidle up\n"); sched_add(&kidle_thread); } diff --git a/sys/amd64/sys/thread.c b/sys/amd64/sys/thread.c index f369a4d..252ac94 100644 --- a/sys/amd64/sys/thread.c +++ b/sys/amd64/sys/thread.c @@ -4,21 +4,62 @@ #include "sys/debug.h" #include "sys/heap.h" +#define AMD64_DEFAULT_KSTACK_SIZE 0x4000 + #define ctx0(t) ((amd64_thread_context_t *) (t->kstack_ptr)) -int thread_init(thread_t *t) { +int thread_init(thread_t *t, + mm_space_t space, + uintptr_t ip, + uintptr_t kstack_base, + size_t kstack_size, + uintptr_t ustack_base, + size_t ustack_size, + uint32_t flags + ) { if (thread_info_init(&t->info) != 0) { return -1; } + t->info.flags = flags; - // t->kstack_base = (uintptr_t) kmalloc(sizeof(amd64_thread_context_t)); - // // TODO: assert - // if (!t->kstack_base) { - // panic("Failed to allocate thread stack\n"); - // } + // Mandatory for both kernel and user threads + if (!kstack_base) { + kstack_base = (uintptr_t) kmalloc(AMD64_DEFAULT_KSTACK_SIZE); + kstack_size = AMD64_DEFAULT_KSTACK_SIZE; + if (!kstack_base) { + panic("Failed to allocate thread kstack\n"); + } + } + t->kstack_base = kstack_base; + t->kstack_size = kstack_size; + t->kstack_ptr = t->kstack_base + t->kstack_size - sizeof(amd64_thread_context_t); - // t->kstack_size = sizeof(amd64_thread_context_t); - // t->kstack_ptr = t->kstack_base + t->kstack_size; + // If we're not kernel, additionally set the ustack + if (!(flags & THREAD_KERNEL)) { + assert(ustack_base, "Tried to create an userspace task, but passed no ustack\n"); + + panic("Not implemented\n"); + } + + // Set CS:RIP + if (flags & THREAD_KERNEL) { + ctx0(t)->cs = 0x08; + ctx0(t)->ss = 0x10; + ctx0(t)->rsp = t->kstack_ptr; + ctx0(t)->rflags = 0x248; + ctx0(t)->rip = ip; + } else { + panic("Not implemented\n"); + } + + assert(((uintptr_t) space) > 0xFFFFFF0000000000, + "Invalid PML4 address provided: %p\n", space); + + // Set task space + t->info.space = space; + ctx0(t)->cr3 = ((uintptr_t) space) - 0xFFFFFF0000000000; + + t->next = NULL; return 0; }