diff options
| author | Ada Christine <adachristine18@gmail.com> | 2026-05-24 11:37:59 +0000 |
|---|---|---|
| committer | Ada Christine <adachristine18@gmail.com> | 2026-05-24 11:37:59 +0000 |
| commit | c55c201952cdeb9285e519869f0c0939c3a5be9c (patch) | |
| tree | f795c1c203bc7068b3ad4aa49e2a7370e210708b /kc/core/cpu | |
| parent | d07bd08dd0446e8c41eccaccbb07e9112200e8ee (diff) | |
arch file to cpu
Diffstat (limited to 'kc/core/cpu')
| -rw-r--r-- | kc/core/cpu/arch_thread.c | 81 | ||||
| -rw-r--r-- | kc/core/cpu/arch_thread.h | 12 |
2 files changed, 93 insertions, 0 deletions
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 <stdint.h> +#include <stddef.h> +#include <stdnoreturn.h> + +#include <libc/stdio.h> + +#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"); +} + diff --git a/kc/core/cpu/arch_thread.h b/kc/core/cpu/arch_thread.h new file mode 100644 index 0000000..2b8d0fb --- /dev/null +++ b/kc/core/cpu/arch_thread.h @@ -0,0 +1,12 @@ +#pragma once + +void * arch_idle_loop(void *); + +void arch_create_thread( + struct kc_thread *thread, + void *(kc_thread_func)(void *), + void *params); + +void arch_swap_thread( + struct kc_thread *outgoing, + struct kc_thread *incoming); |
