sched: add kernel-mode context switching

This commit is contained in:
2023-04-30 14:27:57 +01:00
parent 2cb2d9100a
commit 085c3d2a89
12 changed files with 340 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
#include <socks/sched.h>
#include <socks/object.h>
#include <socks/cpu.h>
#include <socks/machine/thread.h>
static struct object_type thread_type = {
.ob_name = "thread",
@@ -24,6 +25,25 @@ struct thread *thread_alloc(void)
return t;
}
kern_status_t thread_init(struct thread *thr, uintptr_t ip)
{
thr->tr_prio = PRIO_NORMAL;
thr->tr_state = THREAD_READY;
thr->tr_quantum_target = default_quantum();
thr->tr_kstack = vm_page_alloc(THREAD_KSTACK_ORDER, VM_NORMAL);
if (!thr->tr_kstack) {
return KERN_NO_MEMORY;
}
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack) + vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
thr->tr_bp = thr->tr_sp;
prepare_stack(ip, &thr->tr_sp);
return KERN_OK;
}
void thread_free(struct thread *thr)
{
@@ -36,7 +56,7 @@ struct thread *current_thread(void)
return NULL;
}
struct thread *out = cpu->c_current_thread;
struct thread *out = cpu->c_rq.rq_cur;
put_cpu(cpu);
return out;
}
@@ -45,3 +65,8 @@ bool need_resched(void)
{
return (current_thread()->tr_flags & THREAD_F_NEED_RESCHED) != 0;
}
int thread_priority(struct thread *thr)
{
return thr->tr_prio;
}