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
|
#include "scheduler.h"
#include <stdlib.h>
void task_a(void) {
while (true) printk("A");
}
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};
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');
}
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;
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;
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];
}
|