From 13946b17585feb6cda0656fcf3b60210fafd78eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Fri, 26 Jan 2018 15:02:13 +0100 Subject: [PATCH] Pass around tcbs instead of thread stacks --- src/kernel/include/thread.h | 10 ++++++---- src/kernel/proc/scheduler.c | 6 +++--- src/kernel/proc/thread.c | 15 ++++++++------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/kernel/include/thread.h b/src/kernel/include/thread.h index 6b11d24..d22259b 100644 --- a/src/kernel/include/thread.h +++ b/src/kernel/include/thread.h @@ -2,7 +2,7 @@ #include -struct tcb +struct thread { uintptr_t stack_ptr; uint64_t tid; @@ -10,10 +10,10 @@ struct tcb struct thread *next; }; -#define TCB_OFFSET (PAGE_SIZE - sizeof(struct tcb)) +#define TCB_OFFSET (PAGE_SIZE - sizeof(struct thread)) #define SWTCH_STACK_SIZE (0x8*8) -struct thread +struct thread_stack { uint8_t stack[TCB_OFFSET-SWTCH_STACK_SIZE]; uint64_t RBP; @@ -25,9 +25,11 @@ struct thread uint64_t RBP2; uint64_t ret; - struct tcb tcb; + struct thread tcb; }__attribute__((packed)); +#define thread_stack(th) (struct thread_stack *)((uintptr_t)th - TCB_OFFSET) + struct thread *new_thread(void (*function)(void)); void yield(); int get_tid(); diff --git a/src/kernel/proc/scheduler.c b/src/kernel/proc/scheduler.c index f27bff9..c94f454 100644 --- a/src/kernel/proc/scheduler.c +++ b/src/kernel/proc/scheduler.c @@ -9,10 +9,10 @@ void ready(struct thread *th) { if(!readyQ.last) { - th->tcb.next = 0; + th->next = 0; readyQ.first = readyQ.last = th; } else { - readyQ.last->tcb.next = th; + readyQ.last->next = th; readyQ.last = th; } } @@ -20,7 +20,7 @@ void ready(struct thread *th) struct thread *scheduler_next() { struct thread *th = readyQ.first; - if(!(readyQ.first = th->tcb.next)) + if(!(readyQ.first = th->next)) readyQ.last = 0; return th; } diff --git a/src/kernel/proc/thread.c b/src/kernel/proc/thread.c index 4759b08..7b24d64 100644 --- a/src/kernel/proc/thread.c +++ b/src/kernel/proc/thread.c @@ -7,12 +7,13 @@ struct thread *_current_thread = 0; uint64_t next_tid = 1; struct thread *new_thread(void (*function)(void)) { - struct thread *th = P2V(pmm_alloc()); + struct thread_stack *stk = P2V(pmm_alloc()); + struct thread *th = &stk->tcb; - th->tcb.tid = next_tid++; - th->RBP = (uint64_t)&th->RBP2; - th->ret = (uint64_t)function; - th->tcb.stack_ptr = (uint64_t)&th->RBP; + th->tid = next_tid++; + stk->RBP = (uint64_t)&stk->RBP2; + stk->ret = (uint64_t)function; + th->stack_ptr = (uint64_t)&stk->RBP; return th; } @@ -30,7 +31,7 @@ void set_current_thread(struct thread *th) void switch_thread(struct thread *old, struct thread *new) { set_current_thread(new); - swtch(&old->tcb.stack_ptr, &new->tcb.stack_ptr); + swtch(&old->stack_ptr, &new->stack_ptr); } void yield() @@ -49,5 +50,5 @@ void yield() int get_tid() { - return current_thread()->tcb.tid; + return current_thread()->tid; }