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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
#include "types.h"
#include "Task.h"
#include "kmalloc.h"
#include "VGA.h"
#include "StdLib.h"
#include "i386.h"
#include "system.h"
#include "FileSystem.h"
Task* current;
Task* s_kernelTask;
static pid_t next_pid;
static DoublyLinkedList<Task>* s_tasks;
static bool contextSwitch(Task*);
static void redoKernelTaskTSS()
{
if (!s_kernelTask->selector())
s_kernelTask->setSelector(allocateGDTEntry());
auto& tssDescriptor = getGDTEntry(s_kernelTask->selector());
tssDescriptor.setBase(&s_kernelTask->tss());
tssDescriptor.setLimit(0xffff);
tssDescriptor.dpl = 0;
tssDescriptor.segment_present = 1;
tssDescriptor.granularity = 1;
tssDescriptor.zero = 0;
tssDescriptor.operation_size = 1;
tssDescriptor.descriptor_type = 0;
tssDescriptor.type = 9;
flushGDT();
}
void Task::prepForIRETToNewTask()
{
redoKernelTaskTSS();
s_kernelTask->tss().backlink = current->selector();
loadTaskRegister(s_kernelTask->selector());
}
void Task::initialize()
{
current = nullptr;
next_pid = 0;
s_tasks = new DoublyLinkedList<Task>;
s_kernelTask = new Task(0, "idle", IPC::Handle::Any, Task::Ring0);
redoKernelTaskTSS();
loadTaskRegister(s_kernelTask->selector());
}
#ifdef TASK_SANITY_CHECKS
void Task::checkSanity(const char* msg)
{
char ch = current->name()[0];
kprintf("<%p> %s{%u}%b [%d] :%b: sanity check <%s>\n",
current->name().characters(),
current->name().characters(),
current->name().length(),
current->name()[current->name().length() - 1],
current->pid(), ch, msg ? msg : "");
ASSERT((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
}
#endif
void Task::allocateLDT()
{
ASSERT(!m_tss.ldt);
static const WORD numLDTEntries = 4;
WORD newLDTSelector = allocateGDTEntry();
m_ldtEntries = new Descriptor[numLDTEntries];
#if 1
kprintf("new ldt selector = %x\n", newLDTSelector);
kprintf("new ldt table at = %p\n", m_ldtEntries);
kprintf("new ldt table size = %u\n", (numLDTEntries * 8) - 1);
#endif
Descriptor& ldt = getGDTEntry(newLDTSelector);
ldt.setBase(m_ldtEntries);
ldt.setLimit(numLDTEntries * 8 - 1);
ldt.dpl = 0;
ldt.segment_present = 1;
ldt.granularity = 0;
ldt.zero = 0;
ldt.operation_size = 1;
ldt.descriptor_type = 0;
ldt.type = Descriptor::LDT;
m_tss.ldt = newLDTSelector;
}
Task::Task(void (*e)(), const char* n, IPC::Handle h, RingLevel ring)
: m_name(n)
, m_entry(e)
, m_pid(next_pid++)
, m_handle(h)
, m_state(Runnable)
, m_ring(ring)
{
memset(&m_tss, 0, sizeof(m_tss));
memset(&m_ldtEntries, 0, sizeof(m_ldtEntries));
if (ring == Ring3) {
allocateLDT();
}
// Only IF is set when a task boots.
m_tss.eflags = 0x0202;
WORD dataSegment;
WORD stackSegment;
WORD codeSegment;
if (ring == Ring0) {
codeSegment = 0x08;
dataSegment = 0x10;
stackSegment = dataSegment;
} else {
codeSegment = 0x1b;
dataSegment = 0x23;
stackSegment = dataSegment;
}
m_tss.ds = dataSegment;
m_tss.es = dataSegment;
m_tss.fs = dataSegment;
m_tss.gs = dataSegment;
m_tss.ss = stackSegment;
m_tss.cs = codeSegment;
m_tss.eip = (DWORD)m_entry;
// NOTE: Each task gets 4KB of stack.
// This memory is leaked ATM.
// But uh, there's also no process termination, so I guess it's not technically leaked...
static const DWORD defaultStackSize = 4096;
m_stackTop = ((DWORD)kmalloc(defaultStackSize) + defaultStackSize) & 0xffffff8;
m_tss.esp = m_stackTop;
if (ring == Ring3) {
// Set up a separate stack for Ring0.
// FIXME: Don't leak this stack either.
DWORD ring0StackTop = ((DWORD)kmalloc(defaultStackSize) + defaultStackSize) & 0xffffff8;
m_tss.ss0 = 0x10;
m_tss.esp0 = ring0StackTop;
}
// HACK: Ring2 SS in the TSS is the current PID.
m_tss.ss2 = m_pid;
m_farPtr.offset = 0x12345678;
// Don't add task 0 (kernel dummy task) to task list.
// FIXME: This doesn't belong in the constructor.
if (m_pid == 0)
return;
// Add it to head of task list (meaning it's next to run too, ATM.)
s_tasks->prepend(this);
system.nprocess++;
kprintf("Task %u (%s) spawned @ %p\n", m_pid, m_name.characters(), m_entry);
}
Task::~Task()
{
delete [] m_ldtEntries;
m_ldtEntries = nullptr;
}
void yield()
{
if (!current) {
kprintf( "PANIC: yield() with !current" );
HANG;
}
//kprintf("%s<%u> yield()\n", current->name().characters(), current->pid());
if (!scheduleNewTask())
return;
Descriptor& descriptor = getGDTEntry(current->selector());
descriptor.type = 9;
flushGDT();
//kprintf("yield() jumping to new task: %x (%s)\n", current->farPtr().selector, current->name().characters());
asm(
"ljmp *(%%eax)\n"
::"a"(¤t->farPtr())
);
}
bool scheduleNewTask()
{
if (!current) {
// XXX: The first ever context_switch() goes to the idle task.
// This to setup a reliable place we can return to.
return contextSwitch(Task::kernelTask());
}
// Check and unblock tasks whose wait conditions have been met.
for (auto* task = s_tasks->head(); task; task = task->next()) {
if (task->state() == Task::BlockedReceive && (task->ipc.msg.isValid() || task->ipc.notifies)) {
task->unblock();
continue;
}
if (task->state() == Task::BlockedSend) {
Task* peer = Task::fromIPCHandle(task->ipc.dst);
if (peer && peer->state() == Task::BlockedReceive && peer->acceptsMessageFrom(*task)) {
task->unblock();
continue;
}
}
if (task->state() == Task::BlockedSleep) {
if (task->wakeupTime() <= system.uptime) {
task->unblock();
continue;
}
}
}
auto* prevHead = s_tasks->head();
for (;;) {
// Move head to tail.
s_tasks->append(s_tasks->removeHead());
auto* task = s_tasks->head();
if (task->state() == Task::Runnable || task->state() == Task::Running) {
//kprintf("switch to %s\n", task->name().characters());
return contextSwitch(task);
}
if (task == prevHead) {
// Back at task_head, nothing wants to run.
return contextSwitch(Task::kernelTask());
}
}
}
static void drawSchedulerBanner(Task& task)
{
// FIXME: We need a kernel lock to do stuff like this :(
//return;
auto c = vga_get_cursor();
auto a = vga_get_attr();
vga_set_cursor(0, 50);
vga_set_attr(0x20);
kprintf(" ");
kprintf(" ");
kprintf(" ");
vga_set_cursor(0, 50);
kprintf("pid: %u ", task.pid());
vga_set_cursor(0, 58);
kprintf("%s", task.name().characters());
vga_set_cursor(0, 65);
kprintf("eip: %p", task.tss().eip);
vga_set_attr(a);
vga_set_cursor(c);
}
static bool contextSwitch(Task* t)
{
//kprintf("c_s to %s (same:%u)\n", t->name().characters(), current == t);
t->setTicksLeft(5);
if (current == t)
return false;
// If the last task hasn't blocked (still marked as running),
// mark it as runnable for the next round.
if (current->state() == Task::Running)
current->setState(Task::Runnable);
current = t;
t->setState(Task::Running);
if (!t->selector())
t->setSelector(allocateGDTEntry());
auto& tssDescriptor = getGDTEntry(t->selector());
tssDescriptor.limit_hi = 0;
tssDescriptor.limit_lo = 0xFFFF;
tssDescriptor.base_lo = (DWORD)(&t->tss()) & 0xFFFF;
tssDescriptor.base_hi = ((DWORD)(&t->tss()) >> 16) & 0xFF;
tssDescriptor.base_hi2 = ((DWORD)(&t->tss()) >> 24) & 0xFF;
tssDescriptor.dpl = 0;
tssDescriptor.segment_present = 1;
tssDescriptor.granularity = 1;
tssDescriptor.zero = 0;
tssDescriptor.operation_size = 1;
tssDescriptor.descriptor_type = 0;
tssDescriptor.type = 11; // Busy TSS
flushGDT();
drawSchedulerBanner(*t);
return true;
}
Task* Task::fromPID(pid_t pid)
{
for (auto* task = s_tasks->head(); task; task = task->next()) {
if (task->pid() == pid)
return task;
}
return nullptr;
}
Task* Task::fromIPCHandle(IPC::Handle handle)
{
for (auto* task = s_tasks->head(); task; task = task->next()) {
if (task->handle() == handle)
return task;
}
return nullptr;
}
class FileHandle {
public:
FileHandle() { }
~FileHandle() { }
int seek(int offset);
size_t read(void* buffer, int bufferSize);
int fd() const { return m_fd; }
static FileHandle* fromFileDescriptor(int fd);
//private:
RefPtr<FileSystem::VirtualNode> m_vnode;
int m_fd { -1 };
size_t m_offset { 0 };
};
size_t FileHandle::read(void* buffer, int bufferSize)
{
Task::checkSanity("FileHandle::read");
size_t nread = m_vnode->read((BYTE*)buffer, m_offset, bufferSize);
m_offset += nread;
return nread;
}
int FileHandle::seek(int offset)
{
if (!m_vnode)
return -1;
ASSERT(offset >= 0);
if ((unsigned)offset >= m_vnode->size())
return -1;
m_offset = offset;
return m_offset;
}
FileHandle* FileHandle::fromFileDescriptor(int fd)
{
return current->fileHandleIfExists(fd);
}
FileHandle* Task::fileHandleIfExists(int fd)
{
if (fd < 0)
return nullptr;
if ((unsigned)fd < m_fileHandles.size())
return m_fileHandles[fd];
return nullptr;
}
int Task::sys$seek(int fd, int offset)
{
auto* handle = FileHandle::fromFileDescriptor(fd);
if (!handle)
return -1;
return handle->seek(offset);
}
int Task::sys$read(int fd, void* outbuf, size_t nread)
{
Task::checkSanity("Task::sys$read");
kprintf("Task::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread);
auto* handle = FileHandle::fromFileDescriptor(fd);
kprintf("Task::sys$read: handle=%p\n", handle);
if (!handle) {
kprintf("Task::sys$read: handle not found :(\n");
return -1;
}
kprintf("call read on handle=%p\n", handle);
nread = handle->read(outbuf, nread);
kprintf("called read\n");
kprintf("Task::sys$read: nread=%u\n", nread);
return nread;
}
int Task::sys$close(int fd)
{
auto* handle = FileHandle::fromFileDescriptor(fd);
if (!handle)
return -1;
return 0;
}
int Task::sys$open(const char* path, size_t pathLength)
{
Task::checkSanity("sys$open");
kprintf("Task::sys$open(): PID=%u, path=%s {%u}\n", m_pid, path, pathLength);
auto* handle = current->openFile(String(path, pathLength));
if (handle)
return handle->fd();
return -1;
}
FileHandle* Task::openFile(String&& path)
{
auto vnode = FileSystem::createVirtualNode(move(path));
if (!vnode) {
kprintf("createVirtualNode failed\n");
return nullptr;
}
#if 1
FileHandle* fh = new FileHandle;
kprintf("made new FileHandle\n");
fh->m_fd = m_fileHandles.size();
kprintf(" fd = %d\n", fh->m_fd);
fh->m_vnode = move(vnode);
kprintf(" vnode = %p\n", fh->m_vnode.ptr());
m_fileHandles.append(move(fh)); // FIXME: allow non-move Vector::append
kprintf("Task::openFile(): FileHandle{%p} fd=%d\n", fh, fh->m_fd);
return fh;
#endif
return nullptr;
}
int Task::sys$kill(pid_t pid, int sig)
{
(void) sig;
if (pid == 0) {
// FIXME: Send to same-group processes.
ASSERT(pid != 0);
}
if (pid == -1) {
// FIXME: Send to all processes.
ASSERT(pid != -1);
}
ASSERT_NOT_REACHED();
Task* peer = Task::fromPID(pid);
if (!peer) {
// errno = ESRCH;
return -1;
}
#if 0
send(peer->handle(), IPC::Message(SYS_KILL, DataBuffer::copy((BYTE*)&sig, sizeof(sig))));
IPC::Message response = receive(peer->handle());
return *(int*)response.data();
#endif
return -1;
}
uid_t Task::sys$getuid()
{
return m_uid;
}
bool Task::acceptsMessageFrom(Task& peer)
{
return !ipc.msg.isValid() && (ipc.src == IPC::Handle::Any || ipc.src == peer.handle());
}
void Task::unblock()
{
ASSERT(m_state != Task::Runnable && m_state != Task::Running);
system.nblocked--;
m_state = Task::Runnable;
}
void Task::block(Task::State state)
{
ASSERT(current->state() == Task::Running);
system.nblocked++;
current->setState(state);
}
void block(Task::State state)
{
current->block(state);
yield();
}
void sleep(DWORD ticks)
{
ASSERT(current->state() == Task::Running);
current->setWakeupTime(system.uptime + ticks);
current->block(Task::BlockedSleep);
yield();
}
void Task::sys$sleep(DWORD ticks)
{
ASSERT(this == current);
sleep(ticks);
}
Task* Task::kernelTask()
{
ASSERT(s_kernelTask);
return s_kernelTask;
}
void Task::setError(int error)
{
m_error = error;
}
|