summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAda Christine <adachristine18@gmail.com>2022-01-17 12:23:44 +0000
committerAda Christine <adachristine18@gmail.com>2022-01-17 12:23:44 +0000
commit8c56039ca130dfef1f0bf204cc702eeb10801379 (patch)
tree5f374b6f40f87fd356780c0d88e01651d1efd1a9
parentc5fcdec761211ef74ba84a8b41f34f073177cfab (diff)
task.h required for building
-rw-r--r--kc/core/task.c76
-rw-r--r--kc/core/task.h33
2 files changed, 109 insertions, 0 deletions
diff --git a/kc/core/task.c b/kc/core/task.c
new file mode 100644
index 0000000..8f78516
--- /dev/null
+++ b/kc/core/task.c
@@ -0,0 +1,76 @@
+#include "task.h"
+#include "memory.h"
+#include "panic.h"
+#include "cpu.h"
+
+struct kc_thread *current_task = NULL;
+struct kc_thread *first_ready_task = NULL;
+struct kc_thread *last_ready_task = NULL;
+
+static int schedule_lock_cnt = 0;
+
+static void lock_scheduler(void);
+static void unlock_scheduler(void);
+static void set_thread_status(enum kc_thread_status status);
+static void idle_task_thread(void);
+
+extern uint64_t *get_tss_rsp0(void);
+
+void task_set_thread(struct kc_thread *task)
+{
+ // wrapper around cpu_set_thread
+ uint64_t *rsp0 = get_tss_rsp0();
+ cpu_set_thread(&current_task->state, &task->state, rsp0);
+}
+
+void task_schedule(void)
+{
+ if (first_ready_task)
+ {
+ struct kc_thread *task = first_ready_task;
+ first_ready_task = task->next;
+ task_set_thread(task);
+ }
+}
+
+static void lock_scheduler(void)
+{
+ __asm__ volatile ("cli;");
+ schedule_lock_cnt++;
+}
+
+static void unlock_scheduler(void)
+{
+ if (1 == schedule_lock_cnt)
+ {
+ schedule_lock_cnt--;
+ __asm__ volatile("sti;");
+ }
+}
+
+static void unblock_thread(struct kc_thread *thread)
+{
+ lock_scheduler();
+ if (!first_ready_task)
+ {
+ task_set_thread(thread);
+ }
+ else
+ {
+ first_ready_task->next = thread;
+ first_ready_task = thread;
+ }
+}
+
+static void set_thread_status(enum kc_thread_status status)
+{
+ lock_scheduler();
+ current_task->status = status;
+ task_schedule();
+ unlock_scheduler();
+}
+
+static void idle_task_thread(void)
+{
+}
+
diff --git a/kc/core/task.h b/kc/core/task.h
new file mode 100644
index 0000000..86e262f
--- /dev/null
+++ b/kc/core/task.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <stdint.h>
+
+enum kc_thread_status
+{
+ READY,
+ STARTED,
+ YIELDED,
+ BLOCKED,
+ TERMINATED
+};
+
+struct kc_thread_state
+{
+ uint64_t stack;
+ uint64_t stack_top;
+ uint64_t page_map;
+};
+
+struct kc_thread
+{
+ struct kc_thread *prev;
+ struct kc_thread *next;
+ enum kc_thread_status status;
+ struct kc_thread_state state;
+};
+
+extern void cpu_set_thread(
+ struct kc_thread_state *current,
+ struct kc_thread_state *next,
+ uint64_t *cpu_tss_rsp0);
+