aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2021-03-16 20:46:15 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2021-03-16 20:46:15 +0200
commit785d20a6b809e140448108e29b791d59c4650c91 (patch)
tree2617763dac6520d83a969157e89c339ee70c6ee3
parentSwitch lto to full (diff)
downloadkernel.cpp-785d20a6b809e140448108e29b791d59c4650c91.tar.xz
Mostly fix scheduler throwing page faults
-rw-r--r--src/cpu/registers.h2
-rw-r--r--src/idt/exception.cc2
-rw-r--r--src/makefile2
-rw-r--r--src/scheduler.cc48
-rw-r--r--src/scheduler.h14
-rw-r--r--src/scheduler/task.cc8
6 files changed, 42 insertions, 34 deletions
diff --git a/src/cpu/registers.h b/src/cpu/registers.h
index d2f2e74..cbeeeb4 100644
--- a/src/cpu/registers.h
+++ b/src/cpu/registers.h
@@ -10,7 +10,7 @@ struct cpu_state {
uint32_t edi; // destination index: string, memory copying and setting, far pointer addressing with ES
uint32_t esi; // source index: string and memory copying
uint32_t ebp; // stack base pointer
- const uint32_t esp; // stack pointer; this register is not used by popa
+ uint32_t esp; // stack pointer; this register is not used by popa
// general registers
uint32_t ebx; // base register: base pointer for memory access
diff --git a/src/idt/exception.cc b/src/idt/exception.cc
index efffd95..b15fe83 100644
--- a/src/idt/exception.cc
+++ b/src/idt/exception.cc
@@ -2,7 +2,7 @@
#include "cpu/registers.h"
extern "C" void handle_exception(const cpu_state_t* r) {
- printk("exception ", uhex{r->irq}, " error ", uhex{r->error}, "eip: ", uhex{r->eip}, '\n');
+ printk("exception ", uhex{r->irq}, " ec ", uhex{r->error}, " eip: ", uhex{r->eip}, '\n');
abort();
};
diff --git a/src/makefile b/src/makefile
index af6c874..d3b3c47 100644
--- a/src/makefile
+++ b/src/makefile
@@ -8,7 +8,7 @@ CXX_OBJ += src/kernel.o \
src/idt.o \
src/idt/exception.o src/idt/interruptgate.o src/idt/interrupthandler.o \
src/vmm.o \
- src/scheduler.o
+ src/scheduler.o src/scheduler/task.o
src/cpu/irq.h: $(OBJ_DIR)/src/idt/stubs.o
@echo " GEN $@"
diff --git a/src/scheduler.cc b/src/scheduler.cc
index cce15a8..9a1b54f 100644
--- a/src/scheduler.cc
+++ b/src/scheduler.cc
@@ -10,42 +10,36 @@ void task_b(void) {
}
constexpr uint16_t max_tasks = 256;
-static cpu_state_t* tasks[max_tasks];
+uint16_t curr_task = 0;
+uint16_t last_task = 0;
+static Scheduler::task tasks[max_tasks];
-static uint8_t stack_a[4096] = {0};
-static uint8_t stack_b[4096] = {0};
+/* TODO dynamically allocate task stacks */
+__attribute__((aligned(16))) uint8_t stack_a[4096] = {0};
+__attribute__((aligned(16))) uint8_t stack_b[4096] = {0};
Scheduler::Scheduler(uint16_t cs) : InterruptHandler(0x00) {
- printk("add_task: ", add_task(stack_b, cs, task_b), '\n');
- printk("add_task: ", add_task(stack_a, cs, task_a), '\n');
-
- printk("task0 eip: ", uhex{tasks[1]->eip}, '\n');
- printk("task1 eip: ", uhex{tasks[2]->eip}, '\n');
+ printk("add_task: ", add_task({stack_a, task_a, cs}), " stack ",
+ uhex{reinterpret_cast<uint32_t>(tasks[1].cpu) + sizeof(cpu_state_t)}, " eip: ", uhex{tasks[1].cpu->eip}, '\n');
+ printk("add_task: ", add_task({stack_b, task_b, cs}), " stack ",
+ uhex{reinterpret_cast<uint32_t>(tasks[2].cpu) + sizeof(cpu_state_t)}, " eip: ", uhex{tasks[2].cpu->eip}, '\n');
}
-uint16_t Scheduler::add_task(uint8_t* stack, uint16_t cs, void (*entry)()) {
- if (last_task_id == (max_tasks - 1)) return 0;
-
- uint16_t id = ++last_task_id;
- ++num_tasks;
+uint16_t Scheduler::add_task(Scheduler::task t) {
+ if (last_task == (max_tasks - 1)) return 0;
- cpu_state_t* cpu_state = reinterpret_cast<cpu_state_t*>(stack + 4096 - sizeof(cpu_state_t));
- cpu_state->eip = reinterpret_cast<uint32_t>(entry);
- cpu_state->cs = cs;
- cpu_state->eflags = 0x202;
+ uint16_t id = ++last_task;
+ tasks[id] = t;
- tasks[id] = cpu_state;
- printk("adding task: cs ", uhex{cpu_state->cs}, " stack ", uhex{reinterpret_cast<uint32_t>(tasks[id])}, " entry ",
- uhex{cpu_state->eip}, '\n');
return id;
}
cpu_state_t* Scheduler::trigger(cpu_state_t* cpu) {
- tasks[current_task] = cpu;
-
- current_task++;
- if (current_task > num_tasks) current_task = 0;
-
- printk("\n\nswapped to task ", current_task, '\n');
- return tasks[current_task];
+ if (curr_task != 0) {
+ tasks[curr_task].cpu = cpu;
+ }
+ ++curr_task;
+ if (curr_task > last_task) curr_task = 1;
+ cpu = tasks[curr_task].cpu;
+ return cpu;
}
diff --git a/src/scheduler.h b/src/scheduler.h
index a3148bb..14b844c 100644
--- a/src/scheduler.h
+++ b/src/scheduler.h
@@ -2,16 +2,22 @@
#include <types.h>
#include "idt.h"
+#include "vmm.h"
class Scheduler : public InterruptHandler {
public:
+ struct task {
+ cpu_state_t* cpu = nullptr;
+ vmm* mm = nullptr;
+
+ task() = default;
+ task(uint8_t* stack, void (*entry)(), uint16_t cs);
+ };
+
Scheduler(uint16_t cs);
- [[nodiscard]] uint16_t add_task(uint8_t* stack, uint16_t cs, void (*entry)());
+ [[nodiscard]] uint16_t add_task(task);
[[nodiscard]] cpu_state_t* trigger(cpu_state_t*) override;
private:
- uint16_t current_task = 0;
- uint16_t last_task_id = 0;
- uint16_t num_tasks = 0;
};
diff --git a/src/scheduler/task.cc b/src/scheduler/task.cc
new file mode 100644
index 0000000..7f62f8d
--- /dev/null
+++ b/src/scheduler/task.cc
@@ -0,0 +1,8 @@
+#include "scheduler.h"
+
+Scheduler::task::task(uint8_t* stack, void (*entry)(), uint16_t cs) {
+ cpu = reinterpret_cast<cpu_state_t*>(stack + 4096 - sizeof(cpu_state_t));
+ cpu->eip = reinterpret_cast<uint32_t>(entry);
+ cpu->cs = cs;
+ cpu->eflags = 0x202;
+}