aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2021-03-06 17:51:05 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2021-03-06 17:51:05 +0200
commitadfb2c00592e08815a546548f3e87809a645696c (patch)
tree45b31744909ea1ebc0d75365bd0fdaac404fc145
parentGDT: flush segment registers on lgdt (diff)
downloadkernel.cpp-adfb2c00592e08815a546548f3e87809a645696c.tar.xz
scheduler: fix task switching
-rw-r--r--src/idt/stubs.S7
-rw-r--r--src/scheduler.cc25
-rw-r--r--src/scheduler.h2
3 files changed, 20 insertions, 14 deletions
diff --git a/src/idt/stubs.S b/src/idt/stubs.S
index 4f1440d..d4fb56f 100644
--- a/src/idt/stubs.S
+++ b/src/idt/stubs.S
@@ -82,13 +82,14 @@ exception_common:
iret
interrupt_common:
- pusha
+ pushal
push %esp
call handle_interrupt
- add $4, %esp
+ #add $4, %esp
+ mov %eax, %esp
- popa
+ popal
/* remove error code and irq from stack */
add $8, %esp
diff --git a/src/scheduler.cc b/src/scheduler.cc
index 1557e28..4de9fff 100644
--- a/src/scheduler.cc
+++ b/src/scheduler.cc
@@ -9,20 +9,24 @@ void task_b(void) {
while (true) printk("B");
}
+constexpr uint16_t max_tasks = 256;
+static cpu_state_t* tasks[max_tasks];
+
static uint8_t stack_a[4096] = {0};
static uint8_t stack_b[4096] = {0};
-constexpr uint16_t max_tasks = 2;
-static cpu_state_t* tasks[max_tasks];
Scheduler::Scheduler(uint16_t cs) : InterruptHandler(0x00) {
- add_task(stack_a, cs, &task_a);
- add_task(stack_b, cs, task_b);
+ 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[0]->eip}, '\n');
+ printk("task1 eip: ", uhex{tasks[1]->eip}, '\n');
}
-bool Scheduler::add_task(uint8_t* stack, uint16_t cs, void (*entry)()) {
- if (last_task_id == uint16_t_max) return false;
+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++;
+ uint16_t id = ++last_task_id;
++num_tasks;
cpu_state_t* cpu_state = reinterpret_cast<cpu_state_t*>(stack + 4096 - sizeof(cpu_state_t));
@@ -33,14 +37,15 @@ bool Scheduler::add_task(uint8_t* stack, uint16_t cs, void (*entry)()) {
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 true;
+ return id;
}
cpu_state_t* Scheduler::trigger(cpu_state_t* cpu) {
tasks[current_task] = cpu;
- if (++current_task >= num_tasks) current_task %= num_tasks;
+ current_task++;
+ if (current_task > num_tasks) current_task = 0;
- // printk("swapped to task ", current_task, '\n');
+ printk("\n\nswapped to task ", current_task, '\n');
return tasks[current_task];
}
diff --git a/src/scheduler.h b/src/scheduler.h
index 9e5e9f2..a3148bb 100644
--- a/src/scheduler.h
+++ b/src/scheduler.h
@@ -7,7 +7,7 @@ class Scheduler : public InterruptHandler {
public:
Scheduler(uint16_t cs);
- bool add_task(uint8_t* stack, uint16_t cs, void (*entry)());
+ [[nodiscard]] uint16_t add_task(uint8_t* stack, uint16_t cs, void (*entry)());
[[nodiscard]] cpu_state_t* trigger(cpu_state_t*) override;
private: