From 277e4af4fa9e80816c809542d792ee6bebb7f202 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Fri, 4 May 2012 20:06:37 +0200 Subject: Migration to C++! --- src/kernel/task/idt.c | 219 ----------------------- src/kernel/task/idt.cpp | 223 ++++++++++++++++++++++++ src/kernel/task/idt.h | 2 +- src/kernel/task/sched.c | 50 ------ src/kernel/task/sched.cpp | 50 ++++++ src/kernel/task/sched.h | 4 +- src/kernel/task/syscall.c | 45 ----- src/kernel/task/syscall.cpp | 50 ++++++ src/kernel/task/task.c | 410 ------------------------------------------- src/kernel/task/task.cpp | 411 ++++++++++++++++++++++++++++++++++++++++++++ src/kernel/task/task.h | 29 ++-- src/kernel/task/timer.c | 91 ---------- src/kernel/task/timer.cpp | 91 ++++++++++ 13 files changed, 845 insertions(+), 830 deletions(-) delete mode 100644 src/kernel/task/idt.c create mode 100644 src/kernel/task/idt.cpp delete mode 100644 src/kernel/task/sched.c create mode 100644 src/kernel/task/sched.cpp delete mode 100644 src/kernel/task/syscall.c create mode 100644 src/kernel/task/syscall.cpp delete mode 100644 src/kernel/task/task.c create mode 100644 src/kernel/task/task.cpp delete mode 100644 src/kernel/task/timer.c create mode 100644 src/kernel/task/timer.cpp (limited to 'src/kernel/task') diff --git a/src/kernel/task/idt.c b/src/kernel/task/idt.c deleted file mode 100644 index aed5ea8..0000000 --- a/src/kernel/task/idt.c +++ /dev/null @@ -1,219 +0,0 @@ -#include "idt.h" -#include -#include -#include -#include -#include "task.h" -#include "syscall.h" - -#include - -extern void isr0(); -extern void isr1(); -extern void isr2(); -extern void isr3(); -extern void isr4(); -extern void isr5(); -extern void isr6(); -extern void isr7(); -extern void isr8(); -extern void isr9(); -extern void isr10(); -extern void isr11(); -extern void isr12(); -extern void isr13(); -extern void isr14(); -extern void isr15(); -extern void isr16(); -extern void isr17(); -extern void isr18(); -extern void isr19(); -extern void isr20(); -extern void isr21(); -extern void isr22(); -extern void isr23(); -extern void isr24(); -extern void isr25(); -extern void isr26(); -extern void isr27(); -extern void isr28(); -extern void isr29(); -extern void isr30(); -extern void isr31(); - -extern void irq0(); -extern void irq1(); -extern void irq2(); -extern void irq3(); -extern void irq4(); -extern void irq5(); -extern void irq6(); -extern void irq7(); -extern void irq8(); -extern void irq9(); -extern void irq10(); -extern void irq11(); -extern void irq12(); -extern void irq13(); -extern void irq14(); -extern void irq15(); - -extern void syscall64(); - -extern void idt_flush(int32_t ptr); - -struct idt_entry idt_entries[256]; -struct idt_ptr idt_ptr; - -static int_callback irq_handlers[16] = {0}; -static struct irq_waiter { - struct thread *thread; - struct irq_waiter *next; -} *irq_wakeup[16] = {0}; - -/* Called in idt_.asm when an exception fires (interrupt 0 to 31). - Tries to handle the exception, panics if fails. */ -void idt_isrHandler(struct registers regs) { - if ((regs.int_no == 14 && paging_fault(®s) != 0) || regs.int_no != 14) { - if (tasking_handleException(®s) == 0) { - monitor_write("\nREALLY BAD THIS TIME\t\tUnhandled exception\t#"); - monitor_writeDec(regs.int_no); - monitor_write("\t@"); monitor_writeHex(regs.eip); - PANIC("Unhandled Exception"); - } - } -} - -/* Called in idt_.asm when an IRQ fires (interrupt 32 to 47) - Possibly wakes up a thread that was waiting, possibly calls a handler. */ -void idt_irqHandler(struct registers regs) { - uint32_t doSwitch = (regs.err_code == 0); //IRQ0 = timer - if (regs.err_code > 7) { - outb(0xA0, 0x20); - } - outb(0x20, 0x20); - while (irq_wakeup[regs.err_code] != 0) { - struct irq_waiter *tmp = irq_wakeup[regs.err_code]; - thread_wakeUp(tmp->thread); - irq_wakeup[regs.err_code] = tmp->next; - kfree(tmp); - doSwitch = 1; - } - if (irq_handlers[regs.err_code] != 0) { - irq_handlers[regs.err_code](®s); - } - if (doSwitch) schedule(); -} - -/* Called in idt_.asm on a system call (interrupt 64). - Calls the correct syscall handler (if any). */ -void idt_syscallHandler(struct registers regs) { - if (regs.eax < NUMBER_OF_SYSCALLS && syscalls[regs.eax] != 0) { - syscalls[regs.eax](®s); - } -} - -/* For internal use only. Sets up an entry of the IDT with given parameters. */ -static void idt_setGate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags) { - idt_entries[num].base_lo = base & 0xFFFF; - idt_entries[num].base_hi = (base >> 16) & 0xFFFF; - - idt_entries[num].sel = sel; - idt_entries[num].always0 = 0; - idt_entries[num].flags = flags | 0x60; -} - -/* Remaps the IRQs. Sets up the IDT. */ -void idt_init() { - idt_ptr.limit = (sizeof(struct idt_entry) * 256) - 1; - idt_ptr.base = (uint32_t)&idt_entries; - - memset((uint8_t*)&idt_entries, 0, sizeof(struct idt_entry) * 256); - - //Remap the IRQ table - outb(0x20, 0x11); - outb(0xA0, 0x11); - outb(0x21, 0x20); - outb(0xA1, 0x28); - outb(0x21, 0x04); - outb(0xA1, 0x02); - outb(0x21, 0x01); - outb(0xA1, 0x01); - outb(0x21, 0x0); - outb(0xA1, 0x0); - - idt_setGate(0, (int32_t)isr0, 0x08, 0x8E); - idt_setGate(1, (int32_t)isr1, 0x08, 0x8E); - idt_setGate(2, (int32_t)isr2, 0x08, 0x8E); - idt_setGate(3, (int32_t)isr3, 0x08, 0x8E); - idt_setGate(4, (int32_t)isr4, 0x08, 0x8E); - idt_setGate(5, (int32_t)isr5, 0x08, 0x8E); - idt_setGate(6, (int32_t)isr6, 0x08, 0x8E); - idt_setGate(7, (int32_t)isr7, 0x08, 0x8E); - idt_setGate(8, (int32_t)isr8, 0x08, 0x8E); - idt_setGate(9, (int32_t)isr9, 0x08, 0x8E); - idt_setGate(10, (int32_t)isr10, 0x08, 0x8E); - idt_setGate(11, (int32_t)isr11, 0x08, 0x8E); - idt_setGate(12, (int32_t)isr12, 0x08, 0x8E); - idt_setGate(13, (int32_t)isr13, 0x08, 0x8E); - idt_setGate(14, (int32_t)isr14, 0x08, 0x8E); - idt_setGate(15, (int32_t)isr15, 0x08, 0x8E); - idt_setGate(16, (int32_t)isr16, 0x08, 0x8E); - idt_setGate(17, (int32_t)isr17, 0x08, 0x8E); - idt_setGate(18, (int32_t)isr18, 0x08, 0x8E); - idt_setGate(19, (int32_t)isr19, 0x08, 0x8E); - idt_setGate(20, (int32_t)isr20, 0x08, 0x8E); - idt_setGate(21, (int32_t)isr21, 0x08, 0x8E); - idt_setGate(22, (int32_t)isr22, 0x08, 0x8E); - idt_setGate(23, (int32_t)isr23, 0x08, 0x8E); - idt_setGate(24, (int32_t)isr24, 0x08, 0x8E); - idt_setGate(25, (int32_t)isr25, 0x08, 0x8E); - idt_setGate(26, (int32_t)isr26, 0x08, 0x8E); - idt_setGate(27, (int32_t)isr27, 0x08, 0x8E); - idt_setGate(28, (int32_t)isr28, 0x08, 0x8E); - idt_setGate(29, (int32_t)isr29, 0x08, 0x8E); - idt_setGate(30, (int32_t)isr30, 0x08, 0x8E); - idt_setGate(31, (int32_t)isr31, 0x08, 0x8E); - - idt_setGate(32, (int32_t)irq0, 0x08, 0x8E); - idt_setGate(33, (int32_t)irq1, 0x08, 0x8E); - idt_setGate(34, (int32_t)irq2, 0x08, 0x8E); - idt_setGate(35, (int32_t)irq3, 0x08, 0x8E); - idt_setGate(36, (int32_t)irq4, 0x08, 0x8E); - idt_setGate(37, (int32_t)irq5, 0x08, 0x8E); - idt_setGate(38, (int32_t)irq6, 0x08, 0x8E); - idt_setGate(39, (int32_t)irq7, 0x08, 0x8E); - idt_setGate(40, (int32_t)irq8, 0x08, 0x8E); - idt_setGate(41, (int32_t)irq9, 0x08, 0x8E); - idt_setGate(42, (int32_t)irq10, 0x08, 0x8E); - idt_setGate(43, (int32_t)irq11, 0x08, 0x8E); - idt_setGate(44, (int32_t)irq12, 0x08, 0x8E); - idt_setGate(45, (int32_t)irq13, 0x08, 0x8E); - idt_setGate(46, (int32_t)irq14, 0x08, 0x8E); - idt_setGate(47, (int32_t)irq15, 0x08, 0x8E); - - idt_setGate(64, (int32_t)syscall64, 0x08, 0x8E); - - idt_flush((int32_t)&idt_ptr); - - monitor_write("[IDT] "); -} - -/* Sets up an IRQ handler for given IRQ. */ -void idt_handleIrq(int number, int_callback func) { - if (number < 16 && number >= 0) { - irq_handlers[number] = func; - } -} - -/* Tells the IRQ handler to wake up the current thread when specified IRQ fires. */ -void idt_waitIrq(int number) { - if (number < 16 && number >= 0 && proc_priv() <= PL_KERNEL) { - struct irq_waiter *tmp = kmalloc(sizeof(struct irq_waiter)); - tmp->thread = current_thread; - tmp->next = irq_wakeup[number]; - irq_wakeup[number] = tmp; - - thread_goInactive(); - } -} diff --git a/src/kernel/task/idt.cpp b/src/kernel/task/idt.cpp new file mode 100644 index 0000000..ebc092d --- /dev/null +++ b/src/kernel/task/idt.cpp @@ -0,0 +1,223 @@ +#include "idt.h" +#include +#include +#include +#include +#include "task.h" +#include "syscall.h" + +#include + +extern "C" { + +extern void isr0(); +extern void isr1(); +extern void isr2(); +extern void isr3(); +extern void isr4(); +extern void isr5(); +extern void isr6(); +extern void isr7(); +extern void isr8(); +extern void isr9(); +extern void isr10(); +extern void isr11(); +extern void isr12(); +extern void isr13(); +extern void isr14(); +extern void isr15(); +extern void isr16(); +extern void isr17(); +extern void isr18(); +extern void isr19(); +extern void isr20(); +extern void isr21(); +extern void isr22(); +extern void isr23(); +extern void isr24(); +extern void isr25(); +extern void isr26(); +extern void isr27(); +extern void isr28(); +extern void isr29(); +extern void isr30(); +extern void isr31(); + +extern void irq0(); +extern void irq1(); +extern void irq2(); +extern void irq3(); +extern void irq4(); +extern void irq5(); +extern void irq6(); +extern void irq7(); +extern void irq8(); +extern void irq9(); +extern void irq10(); +extern void irq11(); +extern void irq12(); +extern void irq13(); +extern void irq14(); +extern void irq15(); + +extern void syscall64(); + +} + +extern "C" void idt_flush(int32_t ptr); + +idt_entry idt_entries[256]; +idt_ptr idt_ptr; + +static int_callback irq_handlers[16] = {0}; +static struct irq_waiter { + struct thread *thread; + irq_waiter *next; +} *irq_wakeup[16] = {0}; + +/* Called in idt_.asm when an exception fires (interrupt 0 to 31). + Tries to handle the exception, panics if fails. */ +extern "C" void idt_isrHandler(registers regs) { + if ((regs.int_no == 14 && paging_fault(®s) != 0) || regs.int_no != 14) { + if (tasking_handleException(®s) == 0) { + monitor_write("\nREALLY BAD THIS TIME\t\tUnhandled exception\t#"); + monitor_writeDec(regs.int_no); + monitor_write("\t@"); monitor_writeHex(regs.eip); + PANIC("Unhandled Exception"); + } + } +} + +/* Called in idt_.asm when an IRQ fires (interrupt 32 to 47) + Possibly wakes up a thread that was waiting, possibly calls a handler. */ +extern "C" void idt_irqHandler(registers regs) { + uint32_t doSwitch = (regs.err_code == 0); //IRQ0 = timer + if (regs.err_code > 7) { + outb(0xA0, 0x20); + } + outb(0x20, 0x20); + while (irq_wakeup[regs.err_code] != 0) { + irq_waiter *tmp = irq_wakeup[regs.err_code]; + thread_wakeUp(tmp->thread); + irq_wakeup[regs.err_code] = tmp->next; + kfree(tmp); + doSwitch = 1; + } + if (irq_handlers[regs.err_code] != 0) { + irq_handlers[regs.err_code](®s); + } + if (doSwitch) schedule(); +} + +/* Called in idt_.asm on a system call (interrupt 64). + Calls the correct syscall handler (if any). */ +extern "C" void idt_syscallHandler(registers regs) { + if (regs.eax < NUMBER_OF_SYSCALLS && syscalls[regs.eax] != 0) { + syscalls[regs.eax](®s); + } +} + +/* For internal use only. Sets up an entry of the IDT with given parameters. */ +static void idt_setGate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags) { + idt_entries[num].base_lo = base & 0xFFFF; + idt_entries[num].base_hi = (base >> 16) & 0xFFFF; + + idt_entries[num].sel = sel; + idt_entries[num].always0 = 0; + idt_entries[num].flags = flags | 0x60; +} + +/* Remaps the IRQs. Sets up the IDT. */ +void idt_init() { + idt_ptr.limit = (sizeof(idt_entry) * 256) - 1; + idt_ptr.base = (uint32_t)&idt_entries; + + memset((uint8_t*)&idt_entries, 0, sizeof(idt_entry) * 256); + + //Remap the IRQ table + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, 0x28); + outb(0x21, 0x04); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); + + idt_setGate(0, (int32_t)isr0, 0x08, 0x8E); + idt_setGate(1, (int32_t)isr1, 0x08, 0x8E); + idt_setGate(2, (int32_t)isr2, 0x08, 0x8E); + idt_setGate(3, (int32_t)isr3, 0x08, 0x8E); + idt_setGate(4, (int32_t)isr4, 0x08, 0x8E); + idt_setGate(5, (int32_t)isr5, 0x08, 0x8E); + idt_setGate(6, (int32_t)isr6, 0x08, 0x8E); + idt_setGate(7, (int32_t)isr7, 0x08, 0x8E); + idt_setGate(8, (int32_t)isr8, 0x08, 0x8E); + idt_setGate(9, (int32_t)isr9, 0x08, 0x8E); + idt_setGate(10, (int32_t)isr10, 0x08, 0x8E); + idt_setGate(11, (int32_t)isr11, 0x08, 0x8E); + idt_setGate(12, (int32_t)isr12, 0x08, 0x8E); + idt_setGate(13, (int32_t)isr13, 0x08, 0x8E); + idt_setGate(14, (int32_t)isr14, 0x08, 0x8E); + idt_setGate(15, (int32_t)isr15, 0x08, 0x8E); + idt_setGate(16, (int32_t)isr16, 0x08, 0x8E); + idt_setGate(17, (int32_t)isr17, 0x08, 0x8E); + idt_setGate(18, (int32_t)isr18, 0x08, 0x8E); + idt_setGate(19, (int32_t)isr19, 0x08, 0x8E); + idt_setGate(20, (int32_t)isr20, 0x08, 0x8E); + idt_setGate(21, (int32_t)isr21, 0x08, 0x8E); + idt_setGate(22, (int32_t)isr22, 0x08, 0x8E); + idt_setGate(23, (int32_t)isr23, 0x08, 0x8E); + idt_setGate(24, (int32_t)isr24, 0x08, 0x8E); + idt_setGate(25, (int32_t)isr25, 0x08, 0x8E); + idt_setGate(26, (int32_t)isr26, 0x08, 0x8E); + idt_setGate(27, (int32_t)isr27, 0x08, 0x8E); + idt_setGate(28, (int32_t)isr28, 0x08, 0x8E); + idt_setGate(29, (int32_t)isr29, 0x08, 0x8E); + idt_setGate(30, (int32_t)isr30, 0x08, 0x8E); + idt_setGate(31, (int32_t)isr31, 0x08, 0x8E); + + idt_setGate(32, (int32_t)irq0, 0x08, 0x8E); + idt_setGate(33, (int32_t)irq1, 0x08, 0x8E); + idt_setGate(34, (int32_t)irq2, 0x08, 0x8E); + idt_setGate(35, (int32_t)irq3, 0x08, 0x8E); + idt_setGate(36, (int32_t)irq4, 0x08, 0x8E); + idt_setGate(37, (int32_t)irq5, 0x08, 0x8E); + idt_setGate(38, (int32_t)irq6, 0x08, 0x8E); + idt_setGate(39, (int32_t)irq7, 0x08, 0x8E); + idt_setGate(40, (int32_t)irq8, 0x08, 0x8E); + idt_setGate(41, (int32_t)irq9, 0x08, 0x8E); + idt_setGate(42, (int32_t)irq10, 0x08, 0x8E); + idt_setGate(43, (int32_t)irq11, 0x08, 0x8E); + idt_setGate(44, (int32_t)irq12, 0x08, 0x8E); + idt_setGate(45, (int32_t)irq13, 0x08, 0x8E); + idt_setGate(46, (int32_t)irq14, 0x08, 0x8E); + idt_setGate(47, (int32_t)irq15, 0x08, 0x8E); + + idt_setGate(64, (int32_t)syscall64, 0x08, 0x8E); + + idt_flush((int32_t)&idt_ptr); + + monitor_write("[IDT] "); +} + +/* Sets up an IRQ handler for given IRQ. */ +void idt_handleIrq(int number, int_callback func) { + if (number < 16 && number >= 0) { + irq_handlers[number] = func; + } +} + +/* Tells the IRQ handler to wake up the current thread when specified IRQ fires. */ +void idt_waitIrq(int number) { + if (number < 16 && number >= 0 && proc_priv() <= PL_KERNEL) { + irq_waiter *tmp = new irq_waiter(); + tmp->thread = current_thread; + tmp->next = irq_wakeup[number]; + irq_wakeup[number] = tmp; + + thread_goInactive(); + } +} diff --git a/src/kernel/task/idt.h b/src/kernel/task/idt.h index 1ac03e2..849418e 100644 --- a/src/kernel/task/idt.h +++ b/src/kernel/task/idt.h @@ -29,7 +29,7 @@ struct registers { uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically. }; -typedef void (*int_callback)(struct registers*); +typedef void (*int_callback)(registers*); void idt_init(); void idt_handleIrq(int number, int_callback func); //Set IRQ handler diff --git a/src/kernel/task/sched.c b/src/kernel/task/sched.c deleted file mode 100644 index aa41d5b..0000000 --- a/src/kernel/task/sched.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "sched.h" -#include -#include - -// Lower priority numbers have high priority. Priorities must start at 0. -#define PRIORITIES 3 // we have 4 priority levels -#define PRIORITY(t) (t->process->privilege) //get priority for a thread - -extern struct thread *idle_thread; - -static struct thread *queue[PRIORITIES] = {0}, *last[PRIORITIES] = {0}; - -/* For internal use only. Enqueues specified thread in specified priority queue. */ -static void sched_enqueueIn(struct thread *t, int qid) { - t->queue_next = 0; - if (queue[qid] == 0) { - queue[qid] = last[qid] = t; - } else { - last[qid]->queue_next = t; - last[qid] = t; - } -} - -/* For internal use only. Pops a thread from specified queue, if available. */ -static struct thread *sched_dequeueFrom(int qid) { - if (queue[qid] == 0) return 0; - struct thread *it = queue[qid]; - ASSERT((it->queue_next == 0 && it == last[qid]) || it != last[qid]); - queue[qid] = it->queue_next; - if (queue[qid] == 0) last[qid] = 0; - return it; -} - -/* Used by task.c. Enqueus a thread in the corresponding priority queue. */ -void sched_enqueue(struct thread *t) { - if (t == idle_thread) return; - sched_enqueueIn(t, PRIORITY(t)); -} - -/* Used by task.c. Pops a thread from the lowest priority non-empty queue. */ -struct thread *sched_dequeue() { - struct thread *it = 0; - int i; - for (i = 0; i < PRIORITIES; i++) { - it = sched_dequeueFrom(i); - if (it != 0) break; - } - if (it == 0) return idle_thread; - return it; -} diff --git a/src/kernel/task/sched.cpp b/src/kernel/task/sched.cpp new file mode 100644 index 0000000..e773e97 --- /dev/null +++ b/src/kernel/task/sched.cpp @@ -0,0 +1,50 @@ +#include "sched.h" +#include +#include + +// Lower priority numbers have high priority. Priorities must start at 0. +#define PRIORITIES 3 // we have 4 priority levels +#define PRIORITY(t) (t->process->privilege) //get priority for a thread + +extern thread *idle_thread; + +static thread *queue[PRIORITIES] = {0}, *last[PRIORITIES] = {0}; + +/* For internal use only. Enqueues specified thread in specified priority queue. */ +static void sched_enqueueIn(thread *t, int qid) { + t->queue_next = 0; + if (queue[qid] == 0) { + queue[qid] = last[qid] = t; + } else { + last[qid]->queue_next = t; + last[qid] = t; + } +} + +/* For internal use only. Pops a thread from specified queue, if available. */ +static thread *sched_dequeueFrom(int qid) { + if (queue[qid] == 0) return 0; + thread *it = queue[qid]; + ASSERT((it->queue_next == 0 && it == last[qid]) || it != last[qid]); + queue[qid] = it->queue_next; + if (queue[qid] == 0) last[qid] = 0; + return it; +} + +/* Used by task.c. Enqueus a thread in the corresponding priority queue. */ +void sched_enqueue(thread *t) { + if (t == idle_thread) return; + sched_enqueueIn(t, PRIORITY(t)); +} + +/* Used by task.c. Pops a thread from the lowest priority non-empty queue. */ +thread *sched_dequeue() { + thread *it = 0; + int i; + for (i = 0; i < PRIORITIES; i++) { + it = sched_dequeueFrom(i); + if (it != 0) break; + } + if (it == 0) return idle_thread; + return it; +} diff --git a/src/kernel/task/sched.h b/src/kernel/task/sched.h index 1233a44..7d0dcd3 100644 --- a/src/kernel/task/sched.h +++ b/src/kernel/task/sched.h @@ -3,7 +3,7 @@ #include "task.h" -void sched_enqueue(struct thread *t); -struct thread *sched_dequeue(); +void sched_enqueue(thread *t); +thread *sched_dequeue(); #endif diff --git a/src/kernel/task/syscall.c b/src/kernel/task/syscall.c deleted file mode 100644 index bd27eba..0000000 --- a/src/kernel/task/syscall.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "syscall.h" -#include "task.h" -#include - -#define CALL0(name, scname) static void scname(struct registers* r) { r->eax = name(); } -#define CALL1(name, scname) static void scname(struct registers* r) { \ - r->eax = name(r->ebx); } -#define CALL2(name, scname) static void scname(struct registers* r) { \ - r->eax = name(r->ebx, r->ecx); } -#define CALL3(name, scname) static void scname(struct registers* r) { \ - r->eax = name(r->ebx, r->ecx, r->edx); } -#define CALL0V(name, scname) static void scname(struct registers* r) { name(); } -#define CALL1V(name, scname) static void scname(struct registers* r) { name(r->ebx); } -#define CALL2V(name, scname) static void scname(struct registers* r) { name(r->ebx, r->ecx); } -#define CALL3V(name, scname) static void scname(struct registers* r) { name(r->ebx, r->ecx, r->edx); } -#define CALL4V(name, scname) static void scname(struct registers* r) { name(r->ebx, r->ecx, r->edx, r->esi); } - -CALL0V(thread_exit, thread_exit_sc); -CALL0V(schedule, schedule_sc); -CALL1V(thread_sleep, thread_sleep_sc); -CALL1V(process_exit, process_exit_sc); -CALL1(monitor_write, printk_sc); -CALL1V(idt_waitIrq, irq_wait_sc); -CALL0(proc_priv, proc_priv_sc); -CALL1(process_sbrk, proc_sbrk_sc); -CALL1V(process_brk, proc_brk_sc); - -static void thread_new_sc(struct registers* r) { - cli(); - thread_new(current_thread->process, (thread_entry)r->ebx, (void*)r->ecx, (void*)r->edx); - sti(); -} - -int_callback syscalls[NUMBER_OF_SYSCALLS] = { - thread_exit_sc, //0 - schedule_sc, - thread_sleep_sc, - process_exit_sc, - printk_sc, - thread_new_sc, //5 - irq_wait_sc, - proc_priv_sc, - proc_sbrk_sc, - proc_brk_sc, - 0 }; diff --git a/src/kernel/task/syscall.cpp b/src/kernel/task/syscall.cpp new file mode 100644 index 0000000..6880745 --- /dev/null +++ b/src/kernel/task/syscall.cpp @@ -0,0 +1,50 @@ +#include "syscall.h" +#include "task.h" +#include "timer.h" +#include +#include + +#define CALL0(name, scname) static void scname(registers* r) { r->eax = name(); } +#define CALL1(name, scname) static void scname(registers* r) { \ + r->eax = name(r->ebx); } +#define CALL2(name, scname) static void scname(registers* r) { \ + r->eax = name(r->ebx, r->ecx); } +#define CALL3(name, scname) static void scname(registers* r) { \ + r->eax = name(r->ebx, r->ecx, r->edx); } +#define CALL0V(name, scname) static void scname(registers* r) { name(); } +#define CALL1V(name, scname) static void scname(registers* r) { name(r->ebx); } +#define CALL2V(name, scname) static void scname(registers* r) { name(r->ebx, r->ecx); } +#define CALL3V(name, scname) static void scname(registers* r) { name(r->ebx, r->ecx, r->edx); } +#define CALL4V(name, scname) static void scname(registers* r) { name(r->ebx, r->ecx, r->edx, r->esi); } + +CALL0V(thread_exit, thread_exit_sc); +CALL0V(schedule, schedule_sc); +CALL1V(thread_sleep, thread_sleep_sc); +CALL1V(process_exit, process_exit_sc); +CALL1V(idt_waitIrq, irq_wait_sc); +CALL0(proc_priv, proc_priv_sc); +CALL1(process_sbrk, proc_sbrk_sc); +CALL1V(process_brk, proc_brk_sc); + +static void printk_sc(registers *r) { + monitor_write((char*)r->ebx); +} + +static void thread_new_sc(registers* r) { + cli(); + thread_new(current_thread->process, (thread_entry)r->ebx, (void*)r->ecx, (void*)r->edx); + sti(); +} + +int_callback syscalls[NUMBER_OF_SYSCALLS] = { + thread_exit_sc, //0 + schedule_sc, + thread_sleep_sc, + process_exit_sc, + printk_sc, + thread_new_sc, //5 + irq_wait_sc, + proc_priv_sc, + proc_sbrk_sc, + proc_brk_sc, + 0 }; diff --git a/src/kernel/task/task.c b/src/kernel/task/task.c deleted file mode 100644 index 9d98165..0000000 --- a/src/kernel/task/task.c +++ /dev/null @@ -1,410 +0,0 @@ -#include "task.h" -#include "sched.h" -#include -#include -#include -#include -#include -#include "timer.h" - -#define KSTACKSIZE 0x8000 - -//Static routines for handling threads exiting and all cleanup -static void thread_exit_stackJmp(uint32_t reason); -static void thread_exit2(uint32_t reason); -static void thread_delete(struct thread *th); -static void process_delete(struct process *pr); - -//From task_.asm -extern uint32_t read_eip(); -extern void task_idle(void*); - -static uint32_t nextpid = 1; -struct process *processes = 0, *kernel_process; -struct thread *current_thread = 0, *idle_thread = 0; - -uint32_t tasking_tmpStack[KSTACKSIZE]; - -/* Sets up tasking. Called by kmain on startup. - Creates a kernel process and an IDLE thread in it. */ -void tasking_init() { - cli(); - kernel_process = kmalloc(sizeof(struct process)); //This process must be hidden to users - kernel_process->pid = kernel_process->uid = kernel_process->thread_count = 0; - kernel_process->privilege = PL_KERNEL; - kernel_process->parent = kernel_process; - kernel_process->pagedir = kernel_pagedir; - kernel_process->next = 0; - kernel_process->threads = 0; - current_thread = 0; - idle_thread = thread_new(kernel_process, task_idle, 0, 0); - sti(); - monitor_write("[Tasking] "); -} - -/* Called by the paging functions when a page table is allocated in the kernel space (>K_HIGHHALF_ADDR). - Updates the page directories of all the processes. */ -void tasking_updateKernelPagetable(uint32_t idx, struct page_table *table, uint32_t tablephysical) { - if (idx < FIRST_KERNEL_PAGETABLE) return; - struct process* it = processes; - while (it != 0) { - it->pagedir->tables[idx] = table; - it->pagedir->tablesPhysical[idx] = tablephysical; - it = it->next; - } -} - -/* Called when a timer IRQ fires. Does a context switch. */ -void schedule() { - if (processes == 0) PANIC("No processes are running !"); - asm volatile("cli"); - - uint32_t esp, ebp, eip; - - asm volatile("mov %%esp, %0" : "=r"(esp)); - asm volatile("mov %%ebp, %0" : "=r"(ebp)); - eip = read_eip(); - - if (eip == 0x12345) { - return; - } - - if (current_thread != 0) { - current_thread->esp = esp; - current_thread->ebp = ebp; - current_thread->eip = eip; - if (current_thread->state == TS_RUNNING) sched_enqueue(current_thread); - } - - current_thread = sched_dequeue(); - ASSERT(current_thread != 0); - - pagedir_switch(current_thread->process->pagedir); - - gdt_setKernelStack(((uint32_t)current_thread->kernelStack_addr) + current_thread->kernelStack_size); - - asm volatile(" \ - mov %0, %%ebp; \ - mov %1, %%esp; \ - mov %2, %%ecx; \ - mov $0x12345, %%eax; \ - jmp *%%ecx;" - : : "r"(current_thread->ebp), "r"(current_thread->esp), "r"(current_thread->eip)); -} - -/* Called when an exception happens. Provides a stack trace if it was in kernel land. - Ends the thread for most exceptions, ends the whole process for page faults. */ -uint32_t tasking_handleException(struct registers *regs) { - if (current_thread == 0) return 0; //No tasking yet - NL; WHERE; monitor_write("exception:`"); - char *exception_messages[] = {"Division By Zero","Debug","Non Maskable Interrupt","Breakpoint", - "Into Detected Overflow","Out of Bounds","Invalid Opcode","No Coprocessor", "Double Fault", - "Coprocessor Segment Overrun","Bad TSS","Segment Not Present","Stack Fault","General Protection Fault", - "Page Fault","Unknown Interrupt","Coprocessor Fault","Alignment Check","Machine Check"}; - monitor_write(exception_messages[regs->int_no]); - monitor_write("'\teip:"); monitor_writeHex(regs->eip); - if (regs->eip >= K_HIGHHALF_ADDR) { - monitor_write("\n Exception stack trace :\n"); - stack_trace(regs->ebp); - PANIC("Kernel error'd."); - } - if (regs->int_no == 14) { - monitor_write("\n>>> Process exiting.\n"); - thread_exit_stackJmp(EX_PR_EXCEPTION); - } else { - monitor_write("\n>>> Thread exiting.\n"); - thread_exit_stackJmp(EX_TH_EXCEPTION); - } - PANIC("This should never have happened. Please report this."); - return 0; -} - -/* Puts the current thread in an inactive state. */ -void thread_goInactive() { - current_thread->state = TS_WAKEWAIT; - schedule(); -} - -/* Wakes up the given thread. */ -void thread_wakeUp(struct thread* t) { - if (t->state == TS_WAKEWAIT) { - t->state = TS_RUNNING; - sched_enqueue(t); - } -} - -/* Returns the privilege level of the current process. */ -int proc_priv() { - if (current_thread == 0 || current_thread->process == 0) return PL_UNKNOWN; - return current_thread->process->privilege; -} - -/* For internal use only. Called by thread_exit_stackJmp on a stack that will not be deleted. - Exits current thread or process, depending on the reason. */ -void thread_exit2(uint32_t reason) { //See EX_TH_* defines in task.h - /* - * if reason == EX_TH_NORMAL, it is just one thread exiting because it has to - * if reason == EX_TH_EXCEPTION, it is just one thread exiting because of an exception - * if reason is none of the two cases above, it is the whole process exiting (with error code = reason) - */ - struct thread *th = current_thread; - if (th == 0 || th->process == 0) goto retrn; - struct process *pr = th->process; - if ((reason == EX_TH_NORMAL || reason == EX_TH_EXCEPTION) && pr->thread_count > 1) { - thread_delete(th); - } else { - process_delete(pr); - } - retrn: - sti(); - schedule(); -} - -/* For internal use only. Called by thread_exit and process_exit. - Switches to a stack that will not be deleted when current thread is deleted. */ -void thread_exit_stackJmp(uint32_t reason) { - cli(); - uint32_t *stack; - stack = tasking_tmpStack + (KSTACKSIZE / 4); - stack--; *stack = reason; - stack--; *stack = 0; - asm volatile(" \ - mov %0, %%esp; \ - mov %1, %%ebp; \ - mov %2, %%ecx; \ - mov %3, %%cr3; \ - jmp *%%ecx;" : : - "r"(stack), "r"(stack), "r"(thread_exit2), "r"(kernel_pagedir->physicalAddr)); -} - -/* System call. Exit the current thread. */ -void thread_exit() { - thread_exit_stackJmp(EX_TH_NORMAL); -} - -/* System call. Exit the current process. */ -void process_exit(size_t retval) { - if (retval == EX_TH_NORMAL || retval == EX_TH_EXCEPTION) retval = EX_PR_EXCEPTION; - thread_exit_stackJmp(retval); -} - -/* For internal use only. This is called when a newly created thread first runs - (its address is the value given for EIP). - It switches to user mode if necessary and calls the entry point. */ -static void thread_run(void* u_esp, struct thread *thread, thread_entry entry_point, void *data) { - pagedir_switch(thread->process->pagedir); - if (thread->process->privilege >= PL_USER) { //User mode ! - uint32_t *stack = u_esp; - - stack--; *stack = (uint32_t)data; - stack--; *stack = 0; - size_t esp = (size_t)stack, eip = (size_t)entry_point; - //Setup a false structure for returning from an interrupt : - //value for esp is in ebx, for eip is in ecx - //- update data segments to 0x23 = user data segment with RPL=3 - //- push value for ss : 0x23 (user data seg rpl3) - //- push value for esp - //- push flags - //- update flags, set IF = 1 (interrupts flag) - //- push value for cs : 0x1B = user code segment with RPL=3 - //- push eip - //- return from fake interrupt - asm volatile(" \ - mov $0x23, %%ax; \ - mov %%ax, %%ds; \ - mov %%ax, %%es; \ - mov %%ax, %%fs; \ - mov %%ax, %%gs; \ - \ - pushl $0x23; \ - pushl %%ebx; \ - pushf; \ - pop %%eax; \ - or $0x200, %%eax; \ - push %%eax; \ - pushl $0x1B; \ - push %%ecx; \ - iret; \ - " : : "b"(esp), "c"(eip)); - } else { - asm volatile("sti"); - entry_point(data); - } - thread_exit(0); -} - -/* Creates a new thread for given process. - Allocates a kernel stack and a user stack if necessary. - Sets up the kernel stack for values to be passed to thread_run. */ -struct thread *thread_new(struct process *proc, thread_entry entry_point, void *data, void *u_esp) { - struct thread *t = kmalloc(sizeof(struct thread)); - t->process = proc; - t->next = 0; - proc->thread_count++; - - if (u_esp == 0) u_esp = (void*)proc->stack; - - t->kernelStack_addr = kmalloc(KSTACKSIZE); - t->kernelStack_size = KSTACKSIZE; - - uint32_t *stack = (uint32_t*)((size_t)t->kernelStack_addr + t->kernelStack_size); - - //Pass parameters - stack--; *stack = (uint32_t)data; - stack--; *stack = (uint32_t)entry_point; - stack--; *stack = (uint32_t)t; - stack--; *stack = (uint32_t)u_esp; - stack--; *stack = 0; - t->esp = (uint32_t)stack; - t->ebp = t->esp + 8; - t->eip = (uint32_t)thread_run; - - t->state = TS_RUNNING; - sched_enqueue(t); - - if (proc->threads == 0) { - proc->threads = t; - } else { - struct thread *i = proc->threads; - while (i->next != 0) i = i->next; - i->next = t; - } - return t; -} - -/* Creates a new process. Creates a struct process and fills it up. */ -struct process *process_new(struct process* parent, uint32_t uid, uint32_t privilege) { - struct process* p = kmalloc(sizeof(struct process)); - p->pid = (nextpid++); - p->uid = uid; - p->thread_count = 0; - p->threads = 0; - p->privilege = privilege; - p->parent = parent; - p->pagedir = pagedir_new(); - p->next = processes; - p->data = 0; - p->dataseg = 0; - - p->stack = 0; - if (p->privilege >= PL_USER) { //We are running in user mode - size_t stacksBottom = K_HIGHHALF_ADDR - 0x01000000; - seg_map(simpleseg_make(stacksBottom, USER_STACK_SIZE, 1), p->pagedir, 0); - p->stack = stacksBottom + USER_STACK_SIZE - 4; - } - - processes = p; - return p; -} - -/* Deletes given thread, freeing the stack(s). */ -static void thread_delete(struct thread *th) { - if (th->process->threads == th) { - th->process->threads = th->next; - } else { - struct thread *it = th->process->threads; - while (it) { - if (it->next == th) { - it->next = th->next; - break; - } - it = it->next; - } - } - if (current_thread == th) current_thread = 0; - th->process->thread_count--; - kfree(th->kernelStack_addr); - kfree(th); -} - -/* Deletes a process. First, deletes all its threads. Also deletes the corresponding page directory. */ -static void process_delete(struct process *pr) { - struct thread *it = pr->threads; - while (it != 0) { - thread_delete(it); - it = it->next; - } - if (processes == pr) { - processes = pr->next; - } else { - struct process *it = processes; - while (it) { - if (it->next == pr) { - it->next = pr->next; - break; - } - it = it->next; - } - } - pagedir_delete(pr->pagedir); - kfree(pr); -} - -/* System call. Called by the app to define the place for the heap. */ -/*int process_setheapseg(size_t start, size_t end) { //syscall - struct process *p = current_thread->process; - if (start >= K_HIGHHALF_ADDR || end >= K_HIGHHALF_ADDR) return -1; - if (p->heapseg == 0) { - struct segment *s = simpleseg_make(start, end - start, 1); - if (s == 0) return -5; - p->heapseg = seg_map(s, p->pagedir, 0); - if (p->heapseg == 0) return -1; - return 0; - } else if (p->heapseg->start != start) { - seg_unmap(p->heapseg); - struct segment *s = simpleseg_make(start, end - start, 1); - if (s == 0) return -5; - p->heapseg = seg_map(s, p->pagedir, 0); - if (p->heapseg == 0) return -1; - return 0; - } else { - return simpleseg_resize(p->heapseg, end - start); - } -}*/ - -size_t process_sbrk(size_t size) { - struct process *p = current_thread->process; - if (p->data == 0) return -1; - ASSERT(p->data < K_HIGHHALF_ADDR); - if (p->data + size >= K_HIGHHALF_ADDR) return -1; - - size_t ret; - if (p->dataseg == 0) { - size_t start = p->data; - if (start & 0x0FFF) start = (start & 0xFFFFF000) + 0x1000; - size_t end = start + size; - if (end & 0x0FFF) end = (end & 0xFFFFF000) + 0x1000; - - struct segment *s = simpleseg_make(start, end - start, 1); - if (s == 0) return -5; - p->dataseg = seg_map(s, p->pagedir, 0); - if (p->dataseg == 0) return -1; - - ret = start; - p->data = start + size; - } else { - size_t start = p->dataseg->start; - size_t end = p->data + size; - if (end <= start) return -1; - - if (end & 0x0FFF) end = (end & 0xFFFFF000) + 0x1000; - simpleseg_resize(p->dataseg, end - start); - - ret = p->data; - p->data += size; - } - /* (DBG) monitor_write("(sbrk "); - monitor_writeHex(size); - monitor_write(" "); - monitor_writeHex(ret); - monitor_write(")"); */ - return ret; -} - -void process_brk(size_t ptr) { - struct process *p = current_thread->process; - - ASSERT(ptr < K_HIGHHALF_ADDR); - process_sbrk(ptr - p->data); -} - diff --git a/src/kernel/task/task.cpp b/src/kernel/task/task.cpp new file mode 100644 index 0000000..b7c8f45 --- /dev/null +++ b/src/kernel/task/task.cpp @@ -0,0 +1,411 @@ +#include "task.h" +#include "sched.h" +#include +#include +#include +#include +#include +#include "timer.h" + +#define KSTACKSIZE 0x8000 + +//Static routines for handling threads exiting and all cleanup +static void thread_exit_stackJmp(uint32_t reason); +static void thread_exit2(uint32_t reason); +static void thread_delete(thread *th); +static void process_delete(process *pr); + +//From task_.asm +extern "C" uint32_t read_eip(); +extern "C" void task_idle(void*); + +static uint32_t nextpid = 1; +process *processes = 0, *kernel_process; +thread *current_thread = 0, *idle_thread = 0; + +uint32_t tasking_tmpStack[KSTACKSIZE]; + +/* Sets up tasking. Called by kmain on startup. + Creates a kernel process and an IDLE thread in it. */ +void tasking_init() { + cli(); + kernel_process = new process(); //This process must be hidden to users + kernel_process->pid = kernel_process->uid = kernel_process->thread_count = 0; + kernel_process->privilege = PL_KERNEL; + kernel_process->parent = kernel_process; + kernel_process->pagedir = kernel_pagedir; + kernel_process->next = 0; + kernel_process->threads = 0; + current_thread = 0; + idle_thread = thread_new(kernel_process, task_idle, 0, 0); + sti(); + monitor_write("[Tasking] "); +} + +/* Called by the paging functions when a page table is allocated in the kernel space (>K_HIGHHALF_ADDR). + Updates the page directories of all the processes. */ +void tasking_updateKernelPagetable(uint32_t idx, page_table *table, uint32_t tablephysical) { + if (idx < FIRST_KERNEL_PAGETABLE) return; + process* it = processes; + while (it != 0) { + it->pagedir->tables[idx] = table; + it->pagedir->tablesPhysical[idx] = tablephysical; + it = it->next; + } +} + +/* Called when a timer IRQ fires. Does a context switch. */ +void schedule() { + if (processes == 0) PANIC("No processes are running !"); + asm volatile("cli"); + + uint32_t esp, ebp, eip; + + asm volatile("mov %%esp, %0" : "=r"(esp)); + asm volatile("mov %%ebp, %0" : "=r"(ebp)); + eip = read_eip(); + + if (eip == 0x12345) { + return; + } + + if (current_thread != 0) { + current_thread->esp = esp; + current_thread->ebp = ebp; + current_thread->eip = eip; + if (current_thread->state == TS_RUNNING) sched_enqueue(current_thread); + } + + current_thread = sched_dequeue(); + ASSERT(current_thread != 0); + + pagedir_switch(current_thread->process->pagedir); + + gdt_setKernelStack(((uint32_t)current_thread->kernelStack_addr) + current_thread->kernelStack_size); + + asm volatile(" \ + mov %0, %%ebp; \ + mov %1, %%esp; \ + mov %2, %%ecx; \ + mov $0x12345, %%eax; \ + jmp *%%ecx;" + : : "r"(current_thread->ebp), "r"(current_thread->esp), "r"(current_thread->eip)); +} + +/* Called when an exception happens. Provides a stack trace if it was in kernel land. + Ends the thread for most exceptions, ends the whole process for page faults. */ +uint32_t tasking_handleException(registers *regs) { + if (current_thread == 0) return 0; //No tasking yet + NL; WHERE; monitor_write("exception:`"); + char *exception_messages[] = {"Division By Zero","Debug","Non Maskable Interrupt","Breakpoint", + "Into Detected Overflow","Out of Bounds","Invalid Opcode","No Coprocessor", "Double Fault", + "Coprocessor Segment Overrun","Bad TSS","Segment Not Present","Stack Fault","General Protection Fault", + "Page Fault","Unknown Interrupt","Coprocessor Fault","Alignment Check","Machine Check"}; + monitor_write(exception_messages[regs->int_no]); + monitor_write("'\teip:"); monitor_writeHex(regs->eip); + if (regs->eip >= K_HIGHHALF_ADDR) { + monitor_write("\n Exception stack trace :\n"); + stack_trace(regs->ebp); + PANIC("Kernel error'd."); + } + if (regs->int_no == 14) { + monitor_write("\n>>> Process exiting.\n"); + thread_exit_stackJmp(EX_PR_EXCEPTION); + } else { + monitor_write("\n>>> Thread exiting.\n"); + thread_exit_stackJmp(EX_TH_EXCEPTION); + } + PANIC("This should never have happened. Please report this."); + return 0; +} + +/* Puts the current thread in an inactive state. */ +void thread_goInactive() { + current_thread->state = TS_WAKEWAIT; + schedule(); +} + +/* Wakes up the given thread. */ +void thread_wakeUp(thread* t) { + if (t->state == TS_WAKEWAIT) { + t->state = TS_RUNNING; + sched_enqueue(t); + } +} + +/* Returns the privilege level of the current process. */ +int proc_priv() { + if (current_thread == 0 || current_thread->process == 0) return PL_UNKNOWN; + return current_thread->process->privilege; +} + +/* For internal use only. Called by thread_exit_stackJmp on a stack that will not be deleted. + Exits current thread or process, depending on the reason. */ +void thread_exit2(uint32_t reason) { //See EX_TH_* defines in task.h + /* + * if reason == EX_TH_NORMAL, it is just one thread exiting because it has to + * if reason == EX_TH_EXCEPTION, it is just one thread exiting because of an exception + * if reason is none of the two cases above, it is the whole process exiting (with error code = reason) + */ + thread *th = current_thread; + process* pr; + if (th == 0 || th->process == 0) goto retrn; + pr = th->process; + if ((reason == EX_TH_NORMAL || reason == EX_TH_EXCEPTION) && pr->thread_count > 1) { + thread_delete(th); + } else { + process_delete(pr); + } + retrn: + sti(); + schedule(); +} + +/* For internal use only. Called by thread_exit and process_exit. + Switches to a stack that will not be deleted when current thread is deleted. */ +void thread_exit_stackJmp(uint32_t reason) { + cli(); + uint32_t *stack; + stack = tasking_tmpStack + (KSTACKSIZE / 4); + stack--; *stack = reason; + stack--; *stack = 0; + asm volatile(" \ + mov %0, %%esp; \ + mov %1, %%ebp; \ + mov %2, %%ecx; \ + mov %3, %%cr3; \ + jmp *%%ecx;" : : + "r"(stack), "r"(stack), "r"(thread_exit2), "r"(kernel_pagedir->physicalAddr)); +} + +/* System call. Exit the current thread. */ +void thread_exit() { + thread_exit_stackJmp(EX_TH_NORMAL); +} + +/* System call. Exit the current process. */ +void process_exit(size_t retval) { + if (retval == EX_TH_NORMAL || retval == EX_TH_EXCEPTION) retval = EX_PR_EXCEPTION; + thread_exit_stackJmp(retval); +} + +/* For internal use only. This is called when a newly created thread first runs + (its address is the value given for EIP). + It switches to user mode if necessary and calls the entry point. */ +static void thread_run(void* u_esp, thread *thread, thread_entry entry_point, void *data) { + pagedir_switch(thread->process->pagedir); + if (thread->process->privilege >= PL_USER) { //User mode ! + uint32_t *stack = (uint32_t*)u_esp; + + stack--; *stack = (uint32_t)data; + stack--; *stack = 0; + size_t esp = (size_t)stack, eip = (size_t)entry_point; + //Setup a false structure for returning from an interrupt : + //value for esp is in ebx, for eip is in ecx + //- update data segments to 0x23 = user data segment with RPL=3 + //- push value for ss : 0x23 (user data seg rpl3) + //- push value for esp + //- push flags + //- update flags, set IF = 1 (interrupts flag) + //- push value for cs : 0x1B = user code segment with RPL=3 + //- push eip + //- return from fake interrupt + asm volatile(" \ + mov $0x23, %%ax; \ + mov %%ax, %%ds; \ + mov %%ax, %%es; \ + mov %%ax, %%fs; \ + mov %%ax, %%gs; \ + \ + pushl $0x23; \ + pushl %%ebx; \ + pushf; \ + pop %%eax; \ + or $0x200, %%eax; \ + push %%eax; \ + pushl $0x1B; \ + push %%ecx; \ + iret; \ + " : : "b"(esp), "c"(eip)); + } else { + asm volatile("sti"); + entry_point(data); + } + thread_exit(); +} + +/* Creates a new thread for given process. + Allocates a kernel stack and a user stack if necessary. + Sets up the kernel stack for values to be passed to thread_run. */ +thread *thread_new(process *proc, thread_entry entry_point, void *data, void *u_esp) { + thread *t = new thread(); + t->process = proc; + t->next = 0; + proc->thread_count++; + + if (u_esp == 0) u_esp = (void*)proc->stack; + + t->kernelStack_addr = kmalloc(KSTACKSIZE); + t->kernelStack_size = KSTACKSIZE; + + uint32_t *stack = (uint32_t*)((size_t)t->kernelStack_addr + t->kernelStack_size); + + //Pass parameters + stack--; *stack = (uint32_t)data; + stack--; *stack = (uint32_t)entry_point; + stack--; *stack = (uint32_t)t; + stack--; *stack = (uint32_t)u_esp; + stack--; *stack = 0; + t->esp = (uint32_t)stack; + t->ebp = t->esp + 8; + t->eip = (uint32_t)thread_run; + + t->state = TS_RUNNING; + sched_enqueue(t); + + if (proc->threads == 0) { + proc->threads = t; + } else { + thread *i = proc->threads; + while (i->next != 0) i = i->next; + i->next = t; + } + return t; +} + +/* Creates a new process. Creates a struct process and fills it up. */ +process *process_new(process* parent, uint32_t uid, uint32_t privilege) { + process* p = new process(); + p->pid = (nextpid++); + p->uid = uid; + p->thread_count = 0; + p->threads = 0; + p->privilege = privilege; + p->parent = parent; + p->pagedir = pagedir_new(); + p->next = processes; + p->data = 0; + p->dataseg = 0; + + p->stack = 0; + if (p->privilege >= PL_USER) { //We are running in user mode + size_t stacksBottom = K_HIGHHALF_ADDR - 0x01000000; + seg_map(simpleseg_make(stacksBottom, USER_STACK_SIZE, 1), p->pagedir, 0); + p->stack = stacksBottom + USER_STACK_SIZE - 4; + } + + processes = p; + return p; +} + +/* Deletes given thread, freeing the stack(s). */ +static void thread_delete(thread *th) { + if (th->process->threads == th) { + th->process->threads = th->next; + } else { + thread *it = th->process->threads; + while (it) { + if (it->next == th) { + it->next = th->next; + break; + } + it = it->next; + } + } + if (current_thread == th) current_thread = 0; + th->process->thread_count--; + kfree(th->kernelStack_addr); + kfree(th); +} + +/* Deletes a process. First, deletes all its threads. Also deletes the corresponding page directory. */ +static void process_delete(process *pr) { + thread *it = pr->threads; + while (it != 0) { + thread_delete(it); + it = it->next; + } + if (processes == pr) { + processes = pr->next; + } else { + process *it = processes; + while (it) { + if (it->next == pr) { + it->next = pr->next; + break; + } + it = it->next; + } + } + pagedir_delete(pr->pagedir); + kfree(pr); +} + +/* System call. Called by the app to define the place for the heap. */ +/*int process_setheapseg(size_t start, size_t end) { //syscall + struct process *p = current_thread->process; + if (start >= K_HIGHHALF_ADDR || end >= K_HIGHHALF_ADDR) return -1; + if (p->heapseg == 0) { + struct segment *s = simpleseg_make(start, end - start, 1); + if (s == 0) return -5; + p->heapseg = seg_map(s, p->pagedir, 0); + if (p->heapseg == 0) return -1; + return 0; + } else if (p->heapseg->start != start) { + seg_unmap(p->heapseg); + struct segment *s = simpleseg_make(start, end - start, 1); + if (s == 0) return -5; + p->heapseg = seg_map(s, p->pagedir, 0); + if (p->heapseg == 0) return -1; + return 0; + } else { + return simpleseg_resize(p->heapseg, end - start); + } +}*/ + +size_t process_sbrk(size_t size) { + process *p = current_thread->process; + if (p->data == 0) return -1; + ASSERT(p->data < K_HIGHHALF_ADDR); + if (p->data + size >= K_HIGHHALF_ADDR) return -1; + + size_t ret; + if (p->dataseg == 0) { + size_t start = p->data; + if (start & 0x0FFF) start = (start & 0xFFFFF000) + 0x1000; + size_t end = start + size; + if (end & 0x0FFF) end = (end & 0xFFFFF000) + 0x1000; + + segment *s = simpleseg_make(start, end - start, 1); + if (s == 0) return -5; + p->dataseg = seg_map(s, p->pagedir, 0); + if (p->dataseg == 0) return -1; + + ret = start; + p->data = start + size; + } else { + size_t start = p->dataseg->start; + size_t end = p->data + size; + if (end <= start) return -1; + + if (end & 0x0FFF) end = (end & 0xFFFFF000) + 0x1000; + simpleseg_resize(p->dataseg, end - start); + + ret = p->data; + p->data += size; + } + /* (DBG) monitor_write("(sbrk "); + monitor_writeHex(size); + monitor_write(" "); + monitor_writeHex(ret); + monitor_write(")"); */ + return ret; +} + +void process_brk(size_t ptr) { + process *p = current_thread->process; + + ASSERT(ptr < K_HIGHHALF_ADDR); + process_sbrk(ptr - p->data); +} + diff --git a/src/kernel/task/task.h b/src/kernel/task/task.h index 63cb35a..47d7632 100644 --- a/src/kernel/task/task.h +++ b/src/kernel/task/task.h @@ -20,16 +20,17 @@ typedef void (*thread_entry)(void*); +struct thread; struct process { uint32_t pid, uid, privilege, thread_count; - struct process *parent; - struct page_directory *pagedir; + process *parent; + page_directory *pagedir; size_t stack, data; - struct segment_map *dataseg; + segment_map *dataseg; - struct process *next; //Forms a linked list - struct thread *threads; + process *next; //Forms a linked list + thread *threads; }; struct thread { @@ -40,21 +41,25 @@ struct thread { void* kernelStack_addr; uint32_t kernelStack_size; - struct thread *next, *queue_next; //queue_next is used in sched.c + thread *next, *queue_next; //queue_next is used in sched.c }; -extern struct thread *current_thread; +extern thread *current_thread; void tasking_init(); +#ifdef __cplusplus +extern "C" void schedule(); +#else void schedule(); -void tasking_updateKernelPagetable(uint32_t idx, struct page_table *table, uint32_t tablePhysical); -uint32_t tasking_handleException(struct registers *regs); +#endif +void tasking_updateKernelPagetable(uint32_t idx, page_table *table, uint32_t tablePhysical); +uint32_t tasking_handleException(registers *regs); void thread_goInactive(); //Blocks the current thread. it is then waked up by another thread or a system event. -void thread_wakeUp(struct thread *t); +void thread_wakeUp(thread *t); int proc_priv(); //Returns current privilege level -struct thread * thread_new(struct process *proc, thread_entry entry_point, void *data, void *u_esp); -struct process* process_new(struct process *parent, uint32_t uid, uint32_t privilege); +thread * thread_new(process *proc, thread_entry entry_point, void *data, void *u_esp); +process* process_new(process *parent, uint32_t uid, uint32_t privilege); void thread_exit(); //syscall void process_exit(size_t retval); //syscall diff --git a/src/kernel/task/timer.c b/src/kernel/task/timer.c deleted file mode 100644 index 35a94f9..0000000 --- a/src/kernel/task/timer.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "timer.h" -#include "task.h" -#include "idt.h" -#include -#include -#include - -static uint32_t tick = 0, frequency = 0, uptime = 0; - -static void timer_callback(struct registers *regs); -static void timer_wakeUpSleepingThreads(); - -/* Called by kmain. Sets up the PIT and the IRQ0 handler. */ -void timer_init(uint32_t freq) { - frequency = freq; - - idt_handleIrq(0, timer_callback); - - uint32_t divisor = 1193180 / freq; - - outb(0x43, 0x36); //Command byte - - uint8_t l = (divisor & 0xFF), h = (divisor >> 8); - outb(0x40, l); - outb(0x40, h); - - monitor_write("[PIT] "); -} - -/* Accessor function to get machine uptime. */ -uint32_t timer_uptime() { return uptime; } - -/* Accessor function, gets uptime in miliseconds. */ -uint32_t timer_time() { - return (uptime * 1000) + (tick * 1000 / frequency); -} - -/* Called when IRQ0 fires. Updates the uptime variable. - DOES NOT provoke a task switch. The task switch is called in idt.c (IRQ handler). */ -void timer_callback(struct registers *regs) { - tick++; - if (tick == frequency) { - uptime++; - tick = 0; - } - timer_wakeUpSleepingThreads(); -} - -//************************************************ SLEEP FUNCTIONS ***************** - -static struct sleeping_thread { - uint32_t wakeup_time; - struct thread *thread; - struct sleeping_thread *next; -} *sleeping_threads = 0; - -/* Makes the current thread sleep. */ -void thread_sleep(uint32_t msecs) { - if (current_thread == 0) return; - // Create the sleeping_thread structure - struct sleeping_thread *sf = kmalloc(sizeof(struct sleeping_thread)), *tmp; - sf->wakeup_time = timer_time() + msecs; - sf->thread = current_thread; - //Insert it at the right place - if (sleeping_threads == 0 || sleeping_threads->wakeup_time >= sf->wakeup_time) { - sf->next = sleeping_threads; - sleeping_threads = sf; - } else { - tmp = sleeping_threads; - while (1) { - if (tmp->next == 0 || tmp->next->wakeup_time >= sf->wakeup_time) { - sf->next = tmp->next; - tmp->next = sf; - break; - } - tmp = tmp->next; - } - } - - thread_goInactive(); -} - -void timer_wakeUpSleepingThreads() { - uint32_t time = timer_time(); - while (sleeping_threads != 0 && sleeping_threads->wakeup_time <= time) { - struct sleeping_thread *tmp = sleeping_threads; - thread_wakeUp(tmp->thread); - sleeping_threads = tmp->next; - kfree(tmp); - } -} diff --git a/src/kernel/task/timer.cpp b/src/kernel/task/timer.cpp new file mode 100644 index 0000000..05c4550 --- /dev/null +++ b/src/kernel/task/timer.cpp @@ -0,0 +1,91 @@ +#include "timer.h" +#include "task.h" +#include "idt.h" +#include +#include +#include + +static uint32_t tick = 0, frequency = 0, uptime = 0; + +static void timer_callback(registers *regs); +static void timer_wakeUpSleepingThreads(); + +/* Called by kmain. Sets up the PIT and the IRQ0 handler. */ +void timer_init(uint32_t freq) { + frequency = freq; + + idt_handleIrq(0, timer_callback); + + uint32_t divisor = 1193180 / freq; + + outb(0x43, 0x36); //Command byte + + uint8_t l = (divisor & 0xFF), h = (divisor >> 8); + outb(0x40, l); + outb(0x40, h); + + monitor_write("[PIT] "); +} + +/* Accessor function to get machine uptime. */ +uint32_t timer_uptime() { return uptime; } + +/* Accessor function, gets uptime in miliseconds. */ +uint32_t timer_time() { + return (uptime * 1000) + (tick * 1000 / frequency); +} + +/* Called when IRQ0 fires. Updates the uptime variable. + DOES NOT provoke a task switch. The task switch is called in idt.c (IRQ handler). */ +void timer_callback(registers *regs) { + tick++; + if (tick == frequency) { + uptime++; + tick = 0; + } + timer_wakeUpSleepingThreads(); +} + +//************************************************ SLEEP FUNCTIONS ***************** + +static struct sleeping_thread { + uint32_t wakeup_time; + struct thread *thread; + sleeping_thread *next; +} *sleeping_threads = 0; + +/* Makes the current thread sleep. */ +void thread_sleep(uint32_t msecs) { + if (current_thread == 0) return; + // Create the sleeping_thread structure + sleeping_thread *sf = (sleeping_thread*)kmalloc(sizeof(sleeping_thread)), *tmp; + sf->wakeup_time = timer_time() + msecs; + sf->thread = current_thread; + //Insert it at the right place + if (sleeping_threads == 0 || sleeping_threads->wakeup_time >= sf->wakeup_time) { + sf->next = sleeping_threads; + sleeping_threads = sf; + } else { + tmp = sleeping_threads; + while (1) { + if (tmp->next == 0 || tmp->next->wakeup_time >= sf->wakeup_time) { + sf->next = tmp->next; + tmp->next = sf; + break; + } + tmp = tmp->next; + } + } + + thread_goInactive(); +} + +void timer_wakeUpSleepingThreads() { + uint32_t time = timer_time(); + while (sleeping_threads != 0 && sleeping_threads->wakeup_time <= time) { + sleeping_thread *tmp = sleeping_threads; + thread_wakeUp(tmp->thread); + sleeping_threads = tmp->next; + kfree(tmp); + } +} -- cgit v1.2.3