1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include <stdint.h>
#include <stddef.h>
#include <stdnoreturn.h>
#include "task.h"
#include <lib/kstdio.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");
}
|