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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#include <thread.h>
#include <malloc.h>
#include <dbglog.h>
#include <idt.h>
#include <gdt.h>
#include <frame.h>
#include <paging.h>
#include <worker.h>
#include <process.h>
void save_context_and_enter_scheduler(saved_context_t *ctx);
void resume_context(saved_context_t *ctx);
thread_t *current_thread = 0;
// ====================== //
// THE PROGRAMMABLE TIMER //
// ====================== //
void set_pit_frequency(uint32_t freq) {
uint32_t divisor = 1193180 / freq;
ASSERT(divisor < 65536); // must fit on 16 bits
uint8_t l = (divisor & 0xFF);
uint8_t h = ((divisor >> 8) & 0xFF);
outb(0x43, 0x36);
outb(0x40, l);
outb(0x40, h);
}
// =========================== //
// CRITICAL SECTION MANAGEMENT //
// =========================== //
int enter_critical(int level) {
asm volatile("cli");
if (current_thread == 0) return CL_EXCL;
int prev_level = current_thread->critical_level;
if (level > prev_level) current_thread->critical_level = level;
if (current_thread->critical_level < CL_NOINT) asm volatile("sti");
return prev_level;
}
void exit_critical(int prev_level) {
asm volatile("cli");
if (current_thread == 0) return;
if (prev_level < current_thread->critical_level) current_thread->critical_level = prev_level;
if (current_thread->critical_level < CL_NOINT) asm volatile("sti");
}
// ================== //
// THE TASK SCHEDULER //
// ================== //
static thread_t *queue_first_thread = 0, *queue_last_thread = 0;
void enqueue_thread(thread_t *t, bool just_ran) {
ASSERT(t->state == T_STATE_RUNNING);
if (queue_first_thread == 0) {
queue_first_thread = queue_last_thread = t;
t->next_in_queue = 0;
} else if (just_ran) {
t->next_in_queue = 0;
queue_last_thread->next_in_queue = t;
queue_last_thread = t;
} else {
t->next_in_queue = queue_first_thread;
queue_first_thread = t;
}
}
thread_t* dequeue_thread() {
thread_t *t = queue_first_thread;
if (t == 0) return 0;
queue_first_thread = t->next_in_queue;
if (queue_first_thread == 0) queue_last_thread = 0;
return t;
}
// ================ //
// THE TASKING CODE //
// ================ //
void run_scheduler() {
// At this point, interrupts are disabled
// This function is expected NEVER TO RETURN
if (current_thread != 0 && current_thread->state == T_STATE_RUNNING) {
current_thread->last_ran = worker_get_time();
if (current_thread->proc) current_thread->proc->last_ran = current_thread->last_ran;
enqueue_thread(current_thread, true);
}
current_thread = dequeue_thread();
if (current_thread != 0) {
set_kernel_stack(current_thread->stack_region->addr + current_thread->stack_region->size);
resume_context(¤t_thread->ctx);
} else {
// Wait for an IRQ
asm volatile("sti; hlt");
// At this point an IRQ has happenned
// and has been processed. Loop around.
run_scheduler();
}
}
static void run_thread(void (*entry)(void*), void* data) {
ASSERT(current_thread->state == T_STATE_RUNNING);
switch_pagedir(get_kernel_pagedir());
asm volatile("sti");
entry(data);
exit();
}
thread_t *new_thread(entry_t entry, void* data) {
thread_t *t = (thread_t*)malloc(sizeof(thread_t));
if (t == 0) return 0;
void* stack = region_alloc(KPROC_STACK_SIZE + PAGE_SIZE, "Stack", pf_handler_stackoverflow);
if (stack == 0) {
free(t);
return 0;
}
void* stack_low = stack + PAGE_SIZE;
void* stack_high = stack_low + KPROC_STACK_SIZE;
for (void* i = stack_low; i < stack_high; i += PAGE_SIZE) {
uint32_t f = frame_alloc(1);
if (f == 0) {
PANIC("TODO (OOM could not create kernel stack for new thread)");
}
bool map_ok = pd_map_page(i, f, true);
if (!map_ok) {
PANIC("TODO (OOM(2) could not create kernel stack for new thread)");
}
}
t->stack_region = find_region(stack);
ASSERT(stack_high == t->stack_region->addr + t->stack_region->size);
t->ctx.esp = (uint32_t*)stack_high;
*(--t->ctx.esp) = (uint32_t)data; // push second argument : data
*(--t->ctx.esp) = (uint32_t)entry; // push first argument : entry point
*(--t->ctx.esp) = 0; // push invalid return address (the run_thread function never returns)
t->ctx.eip = (void(*)())run_thread;
t->state = T_STATE_PAUSED;
t->last_ran = 0;
t->current_pd_d = get_kernel_pagedir();
// used by user processes
t->proc = 0;
t->user_ex_handler = 0;
t->critical_level = CL_USER;
return t;
}
// ========== //
// SETUP CODE //
// ========== //
static void irq0_handler(registers_t *regs) {
worker_notify_time(1000000 / TASK_SWITCH_FREQUENCY);
if (current_thread != 0 && current_thread->critical_level == CL_USER) {
save_context_and_enter_scheduler(¤t_thread->ctx);
}
}
void threading_setup(entry_t cont, void* arg) {
set_pit_frequency(TASK_SWITCH_FREQUENCY);
idt_set_irq_handler(IRQ0, irq0_handler);
thread_t *t = new_thread(cont, arg);
ASSERT(t != 0);
resume_thread(t);
exit_critical(CL_USER);
run_scheduler(); // never returns
ASSERT(false);
}
// ======================= //
// TASK STATE MANIPULATION //
// ======================= //
void yield() {
ASSERT(current_thread != 0 && current_thread->critical_level != CL_EXCL);
save_context_and_enter_scheduler(¤t_thread->ctx);
}
void pause() {
ASSERT(current_thread != 0 && current_thread->critical_level != CL_EXCL);
current_thread->state = T_STATE_PAUSED;
save_context_and_enter_scheduler(¤t_thread->ctx);
}
void usleep(int usecs) {
void sleeper_resume(void* t) {
thread_t *thread = (thread_t*)t;
resume_thread(thread);
}
if (current_thread == 0) return;
bool ok = worker_push_in(usecs, sleeper_resume, current_thread);
if (ok) pause();
}
void exit() {
current_thread->state = T_STATE_FINISHED;
// TODO : add job for deleting the thread, or whatever
yield(); // expected never to return!
ASSERT(false);
}
bool resume_thread(thread_t *thread) {
bool ret = false;
int st = enter_critical(CL_NOINT);
if (thread->state == T_STATE_PAUSED) {
ret = true;
thread->state = T_STATE_RUNNING;
enqueue_thread(thread, false);
}
exit_critical(st);
return ret;
}
/* vim: set ts=4 sw=4 tw=0 noet :*/
|