From a10aabfcd52f702057316018cd7847ab2bfe4aa1 Mon Sep 17 00:00:00 2001 From: Ada Christine Date: Tue, 26 May 2026 21:32:27 +0000 Subject: we're bringing kjarna back and not doing the crazy stuff with trying to have task management during efi. that was a bit extra. --- kc/core/cpu/arch_thread.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 kc/core/cpu/arch_thread.c (limited to 'kc/core/cpu/arch_thread.c') diff --git a/kc/core/cpu/arch_thread.c b/kc/core/cpu/arch_thread.c new file mode 100644 index 0000000..c0d707a --- /dev/null +++ b/kc/core/cpu/arch_thread.c @@ -0,0 +1,81 @@ +#include +#include +#include + +#include + +#include "task.h" + +struct arch_register_context +{ + uint64_t + r15, + r14, + r13, + r12, + r11, + r10, + r9, + r8, + rsi, + rdi, + rdx, + rcx, + rbx, + rax, + rbp, + rsp; +}; + +extern uint64_t *get_tss_rsp0(void); + +struct arch_thread_context +{ + struct arch_register_context register_context; + void *return_address; +}; + +extern void arch_swap_thread_stack( + void * volatile *outgoing_stack, + void *incoming_stack); + +extern void arch_begin_thread( + void *(func)(void *), + void *params); + +void arch_create_thread( + struct kc_thread *thread, + void *(*func)(void *), + void *params) +{ + uintptr_t stack_pointer = (uintptr_t)thread->kernel_stack_pointer; + struct arch_thread_context *context = (struct arch_thread_context *) + (stack_pointer -= sizeof(*context)); + thread->kernel_stack_pointer = (void *)stack_pointer; + + context->return_address = (void *)arch_begin_thread; + context->register_context.rdi = (uint64_t)func; + context->register_context.rsi = (uint64_t)params; + context->register_context.rdx = (uint64_t)thread; + context->register_context.rsp = (uint64_t)&context->return_address; +} + +void arch_swap_thread( + struct kc_thread *outgoing, + struct kc_thread *incoming) +{ + arch_swap_thread_stack(&outgoing->kernel_stack_pointer, incoming->kernel_stack_pointer); +} + +void *arch_idle_loop(void *) +{ + for (;;) __asm__ ("hlt"); + return nullptr; +} + +noreturn void arch_terminate_thread(union kc_thread_result result, struct kc_thread *) +{ + kct_terminate(result); + for (;;) __asm__ ("hlt"); +} + -- cgit v1.3.1-1-g115d