Threads have isolated memory spaces

This commit is contained in:
Thomas Lovén 2018-02-20 20:38:32 +01:00
parent 2d0f539e23
commit 6523b10984
3 changed files with 17 additions and 9 deletions

View File

@ -8,14 +8,14 @@
#include <thread.h> #include <thread.h>
#include <scheduler.h> #include <scheduler.h>
int thread_id; int *thread_id = (int *)0x20000;
void thread_function() void thread_function()
{ {
thread_id = thread()->tid; *thread_id = thread()->tid;
while(1) while(1)
{ {
debug("Thread %d\n", thread_id); debug("Thread %d\n", *thread_id);
yield(); yield();
} }
} }
@ -35,15 +35,19 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data)
cpu_init(); cpu_init();
uintptr_t P4 = new_P4(); struct thread *th1 = new_thread(thread_function);
write_cr3(P4); 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"); debug_ok("Boot \"Complete\"\n");
ready(new_thread(thread_function));
ready(new_thread(thread_function));
ready(new_thread(thread_function));
start_scheduler(); start_scheduler();
PANIC("Reached end of kernel main function\n"); PANIC("Reached end of kernel main function\n");

View File

@ -8,6 +8,7 @@ struct thread
uint64_t tid; uint64_t tid;
void *stack_ptr; void *stack_ptr;
uint64_t state; uint64_t state;
uint64_t P4;
QUEUE_SPOT(runQ); QUEUE_SPOT(runQ);
uint8_t stack[]; uint8_t stack[];
}; };

View File

@ -1,6 +1,7 @@
#include <thread.h> #include <thread.h>
#include <scheduler.h> #include <scheduler.h>
#include <memory.h> #include <memory.h>
#include <cpu.h>
struct swtch_stack struct swtch_stack
{ {
@ -23,6 +24,7 @@ struct thread *new_thread(void (*function)(void))
struct thread *th = P2V(pmm_calloc()); struct thread *th = P2V(pmm_calloc());
th->tid = next_tid++; th->tid = next_tid++;
th->stack_ptr = incptr(th, PAGE_SIZE - sizeof(struct swtch_stack)); th->stack_ptr = incptr(th, PAGE_SIZE - sizeof(struct swtch_stack));
th->P4 = new_P4();
struct swtch_stack *stk = th->stack_ptr; struct swtch_stack *stk = th->stack_ptr;
stk->RBP = (uint64_t)&stk->RBP2; stk->RBP = (uint64_t)&stk->RBP2;
@ -49,6 +51,7 @@ void scheduler()
while(!(new = scheduler_next())); while(!(new = scheduler_next()));
_thread = new; _thread = new;
write_cr3(new->P4);
switch_stack(&sched_th->stack_ptr, &new->stack_ptr); switch_stack(&sched_th->stack_ptr, &new->stack_ptr);
ready(_thread); ready(_thread);