diff --git a/src/kernel/boot/kmain.c b/src/kernel/boot/kmain.c index 08bd367..42e2a01 100644 --- a/src/kernel/boot/kmain.c +++ b/src/kernel/boot/kmain.c @@ -10,7 +10,7 @@ void thread_function() { - int thread_id = get_tid(); + int thread_id = current_thread()->tid; while(1) { diff --git a/src/kernel/include/thread.h b/src/kernel/include/thread.h index d22259b..d09bed5 100644 --- a/src/kernel/include/thread.h +++ b/src/kernel/include/thread.h @@ -32,6 +32,6 @@ struct thread_stack struct thread *new_thread(void (*function)(void)); void yield(); -int get_tid(); +struct thread *current_thread(); -void swtch(void *, void *); +void switch_stack(void *old_ptr, void *new_ptr); diff --git a/src/kernel/proc/swtch.S b/src/kernel/proc/swtch.S index 1f0aa75..097e8ec 100644 --- a/src/kernel/proc/swtch.S +++ b/src/kernel/proc/swtch.S @@ -1,8 +1,8 @@ .intel_syntax noprefix -.global swtch +.global switch_stack -swtch: +switch_stack: push rbp mov rbp, rsp push r15 diff --git a/src/kernel/proc/thread.c b/src/kernel/proc/thread.c index 7b24d64..b969e05 100644 --- a/src/kernel/proc/thread.c +++ b/src/kernel/proc/thread.c @@ -1,7 +1,7 @@ #include #include -struct thread dummy; +struct thread boot_thread; struct thread *_current_thread = 0; uint64_t next_tid = 1; @@ -11,6 +11,7 @@ struct thread *new_thread(void (*function)(void)) struct thread *th = &stk->tcb; th->tid = next_tid++; + stk->RBP = (uint64_t)&stk->RBP2; stk->ret = (uint64_t)function; th->stack_ptr = (uint64_t)&stk->RBP; @@ -31,7 +32,7 @@ void set_current_thread(struct thread *th) void switch_thread(struct thread *old, struct thread *new) { set_current_thread(new); - swtch(&old->stack_ptr, &new->stack_ptr); + switch_stack(&old->stack_ptr, &new->stack_ptr); } void yield() @@ -42,13 +43,8 @@ void yield() if(old) ready(old); else - old = &dummy; + old = &boot_thread; while(!(new = scheduler_next())); switch_thread(old, new); } - -int get_tid() -{ - return current_thread()->tid; -}