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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#include "Task.ns.h"
#include <Library/Vector.class.h>
//From Task.wtf.asm
extern "C" u32int read_eip();
extern "C" u32int idle_task();
namespace Task {
Vector <Process*> processes; //TODO : use a linked list instead
Vector <Thread*> threads;
Thread* currentThread = NULL;
Process* currentProcess = NULL;
Thread* idleThread = NULL;
u32int currentThreadId = 0;
u32int nextpid = 1;
void initialize(String cmdline, VirtualTerminal *vt) {
asm volatile ("cli");
threads.clear();
processes.clear();
currentProcess = Process::createKernel(cmdline, vt);
idleThread = new Thread(idle_task, true);
currentThread = threads[0];
currentThreadId = 0;
asm volatile ("sti");
}
Thread* nextThread() {
u32int nid = currentThreadId;
while (1) {
nid++;
if (nid >= threads.size()) nid = 0;
if (threads[nid]->runnable() and threads[nid] != idleThread) {
currentThreadId = nid;
return threads[nid];
}
if (nid == currentThreadId) break;
}
return idleThread;
}
void doSwitch() {
if (currentThread == NULL or currentProcess == NULL) return;
u32int esp, ebp, eip, cr3;
asm volatile("mov %%esp, %0" : "=r"(esp));
asm volatile("mov %%ebp, %0" : "=r"(ebp));
eip = read_eip();
if (eip == 0x12345)
return;
if ((u32int)currentThread != 0xFFFFFFFF) currentThread->setState(esp, ebp, eip);
currentThread = nextThread();
currentProcess = currentThread->getProcess();
esp = currentThread->getEsp();
ebp = currentThread->getEbp();
eip = currentThread->getEip();
cr3 = currentProcess->getPagedir()->physicalAddr;
asm volatile(" \
cli; \
mov %0, %%ebp; \
mov %1, %%esp; \
mov %2, %%ecx; \
mov %3, %%cr3; \
mov $0x12345, %%eax; \
jmp *%%ecx;"
: : "r"(ebp), "r"(esp), "r"(eip), "r"(cr3));
}
void triggerSwitch() {
asm volatile("int $65");
}
u32int nextPid() {
return nextpid++;
}
bool IRQwakeup(u8int irq) {
bool r = false;
for (u32int i = 0; i < threads.size(); i++) {
r = r or threads[i]->irqHappens(irq);
}
return r;
}
void allocKernelPageTable(u32int id, page_table_t *table, u32int tablePhys) {
if (id < 768) return; //this would be a BUG
for (u32int i = 1; i < processes.size(); i++) {
processes[i]->getPagedir()->tables[id] = table;
processes[i]->getPagedir()->tablesPhysical[id] = tablePhys;
}
}
Process* getKernelProcess() {
return processes[0];
}
void registerThread(Thread* t) {
unregisterThread(t); //...//
threads.push(t);
}
void unregisterThread(Thread* t) {
for (u32int i = 0; i < threads.size(); i++) {
if (threads[i] == t) {
threads[i] = threads.back();
threads.pop();
return;
}
}
}
void registerProcess(Process* p) {
unregisterProcess(p); //...//
processes.push(p);
}
void unregisterProcess(Process* p) {
for (u32int i = 0; i < processes.size(); i++) {
if (processes[i] == p) {
processes[i] = processes.back();
processes.pop();
return;
}
}
}
}
|