-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
90 lines (68 loc) · 1.38 KB
/
task.c
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
80
81
82
83
84
85
86
87
88
89
90
#include <stdint.h>
#include <string.h>
#include "intr.h"
#include "console.h"
#include "task.h"
#include "memorymanager.h"
static struct task* first_task = NULL;
static struct task* current_task = NULL;
void idle_task() {
while(1) {
}
}
void task_a() {
while(1) {
kprintf("Hallo\n");
}
}
void task_b() {
while(1) {
kprintf("Welt\n");
}
}
struct task* init_task(void* entry) {
uint8_t* stack = (void*)alloc();
uint8_t* user_stack = (void*)alloc();
//initalize an empty struct
struct cpu_state new_state = {
.eax = 0,
.ebx = 0,
.ecx = 0,
.edx = 0,
.esi = 0,
.edi = 0,
.ebp = 0,
.eip = (uint32_t) entry,
.esp = (uint32_t) user_stack + 4096,
.cs = 0x18 | 0x03,
.ss = 0x20 | 0x03,
.eflags = 0x200,
};
struct cpu_state* state = (void*) (stack + 4096 - sizeof(new_state));
*state = new_state;
struct task* task = (void*) alloc();
task->cpu_state = state;
task->next = first_task;
first_task = task;
return task;
}
void init_multitasking() {
//init_task(task_a);
//init_task(task_b);
init_task(idle_task);
}
struct cpu_state* schedule(struct cpu_state* cpu) {
if(current_task != NULL) {
current_task->cpu_state = cpu;
}
if(current_task == NULL) {
current_task = first_task;
} else {
current_task = current_task->next;
if(current_task == NULL) {
current_task = first_task;
}
}
cpu = current_task->cpu_state;
return cpu;
}