From 6523b10984df379c6de7fecc4fc3fc2abd9a2b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Tue, 20 Feb 2018 20:38:32 +0100 Subject: [PATCH] Threads have isolated memory spaces --- src/kernel/boot/kmain.c | 22 +++++++++++++--------- src/kernel/include/thread.h | 1 + src/kernel/proc/thread.c | 3 +++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/kernel/boot/kmain.c b/src/kernel/boot/kmain.c index c0c27e5..127be07 100644 --- a/src/kernel/boot/kmain.c +++ b/src/kernel/boot/kmain.c @@ -8,14 +8,14 @@ #include #include -int thread_id; +int *thread_id = (int *)0x20000; void thread_function() { - thread_id = thread()->tid; + *thread_id = thread()->tid; while(1) { - debug("Thread %d\n", thread_id); + debug("Thread %d\n", *thread_id); yield(); } } @@ -35,15 +35,19 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data) cpu_init(); - uintptr_t P4 = new_P4(); - write_cr3(P4); + 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); + + ready(th1); + ready(th2); + ready(th3); debug_ok("Boot \"Complete\"\n"); - ready(new_thread(thread_function)); - ready(new_thread(thread_function)); - ready(new_thread(thread_function)); - start_scheduler(); PANIC("Reached end of kernel main function\n"); diff --git a/src/kernel/include/thread.h b/src/kernel/include/thread.h index 397019c..ed212dd 100644 --- a/src/kernel/include/thread.h +++ b/src/kernel/include/thread.h @@ -8,6 +8,7 @@ struct thread uint64_t tid; void *stack_ptr; uint64_t state; + uint64_t P4; QUEUE_SPOT(runQ); uint8_t stack[]; }; diff --git a/src/kernel/proc/thread.c b/src/kernel/proc/thread.c index 1e3bd9b..6b3f993 100644 --- a/src/kernel/proc/thread.c +++ b/src/kernel/proc/thread.c @@ -1,6 +1,7 @@ #include #include #include +#include struct swtch_stack { @@ -23,6 +24,7 @@ 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; @@ -49,6 +51,7 @@ void scheduler() while(!(new = scheduler_next())); _thread = new; + write_cr3(new->P4); switch_stack(&sched_th->stack_ptr, &new->stack_ptr); ready(_thread);