summaryrefslogtreecommitdiff
path: root/kernel/interrupts.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/interrupts.c')
-rw-r--r--kernel/interrupts.c170
1 files changed, 87 insertions, 83 deletions
diff --git a/kernel/interrupts.c b/kernel/interrupts.c
index 04bac9f..cf75798 100644
--- a/kernel/interrupts.c
+++ b/kernel/interrupts.c
@@ -1,14 +1,14 @@
#include "kernel/kernel.h"
#include "asm/int.h"
-#include "asm/asm.h"
#include "asm/pit.h"
#include "driver/mouse.h"
#include "interrupts.h"
#include "asm/x86.h"
-#define INT_MAX 255 // size of our interrupts table
+/** The size of our interrupts table */
+#define INT_MAX 255
-// the interrupt descriptor table
+/** The interrupt descriptor table */
static struct int_desc
{
uint16_t addrLo;
@@ -18,7 +18,7 @@ static struct int_desc
uint16_t addrHi;
} idt[INT_MAX];
-// interrupt descriptor table descriptor
+/** The interrupt descriptor table descriptor */
static struct idt_desc
{
uint16_t size;
@@ -26,34 +26,63 @@ static struct idt_desc
uint16_t baseHi;
} idtd;
-uint32_t interrupt_handler(uint32_t esp, uint32_t num)
+/** Sets a handler for a specific interrupt */
+static void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr)
{
-// if(num!=0)klog("int: %d %d",num,esp);
+ uint64_t base=(uint32_t)&(*(uint32_t*)addr);
- if(num==0)
- {
- pit_interrupt_handler();
- esp=task_switch_next(esp);
- }
+ idt[irq].addrLo = base & 0xffff;
+ idt[irq].addrHi = (base >> 16) & 0xffff;
+ idt[irq].zeros=0;
+ idt[irq].flags=flags;
+ idt[irq].sel=sel;
+}
+
+/** Installs the interrupt table */
+static void int_install()
+{
+ idtd.size=sizeof(struct int_desc)*INT_MAX;
+ uint32_t addr=(uint32_t)&idt[0];
+ idtd.baseHi=addr>>16;
+ idtd.baseLo=0xffff&addr;
+ __asm__("lidt %0"::"m" (idtd));
+}
- if(num==1)asm_kb_handler();
+/*
+ * Interrupt dispatcher
+ *
+ * Remeber that we are inside an interrupt here!
+ *
+ */
+
+uint32_t interrupt_handler(uint32_t esp, uint32_t irq)
+{
+ if(irq==0)asm_pit_tick();
+ if(irq==1)asm_kb_handler(); // TODO: put in ringbuff
+ if(irq==12)asm_mouse_handler();// TODO: put in ringbuff
- if(num==12)asm_mouse_handler();
+ // 0x80 - a syscall is coming in
+ if(irq==128){
- if(num==128){
uint32_t *stack=esp;
uint32_t eax=stack[11];
uint32_t ebx=stack[8];
uint32_t ecx=stack[10];
uint32_t edx=stack[9];
- klog("syscall: %d (ebx=0x%08X,ecx=0x%08X,edx=0x%08X)",eax,ebx,ecx,edx);
- task_wake_syscall_worker();
- esp=task_syscall(eax,ebx,ecx,edx,esp);
+
+// klog("syscall: %d (ebx=0x%08X,ecx=0x%08X,edx=0x%08X)",eax,ebx,ecx,edx);
+
+ task_syscall(eax,ebx,ecx,edx);
}
+ if(irq==0 || irq==129 || irq==128)esp=my_scheduler(esp);
+
+ if(irq==255)kpanic("Unhandled Interrupt!");
+
return esp;
}
+// log helpers //
void errlog(uint32_t error_code)
{
klog("error_code: 0x%08X",error_code);
@@ -66,28 +95,22 @@ void defklog(uint32_t eip, uint16_t cs, uint32_t flags)
klog("eflags: 0x%08X",flags);
}
-void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr);
-
-
-void exception_handle()
+void show_error(uint32_t err)
{
- kpanic("exception interrupt");
+ klog("interrupt error code: 0x%08x",err);
+ klog("External Event: %x",err&0b001);
+ klog("Location: %x",err&0b110);
+ klog("Selector: %x",err&0b1111111111111000);
}
+//
+
void int_default()
{
klog("default handler");
kpanic("unhandled interrupt (is this a panic or should just iognore?)");
}
-void show_error(uint32_t err)
-{
- klog("interrupt error code: 0x%08x",err);
- klog("External Event: %x",err&0b001);
- klog("Location: %x",err&0b110);
- klog("Selector: %x",err&0b1111111111111000);
-}
-
void exception_handle_0(){ kpanic("Divide by 0"); }
void exception_handle_1(){ kpanic("Single step (debugger)"); }
void exception_handle_2(){ kpanic("Non Maskable Interrupt"); }
@@ -119,8 +142,7 @@ void exception_handle_14(uint32_t error_code,uint32_t eip,uint16_t cs,uint16_t u
klog("error_code_RSVD: %d",error_code&8?1:0);
klog("error_code_I/D: %d",error_code&16?1:0);
klog("at addr: 0x%08X",x86_get_cr(2));
-
- defklog(eip,cs,flags);
+ defklog(eip,cs,flags);
kpanic("Exception: Fault: Page Fault");
}
@@ -129,78 +151,60 @@ void exception_handle_16(){ kpanic("Coprocessor error"); }
void exception_handle_17(){ kpanic("Alignment Check"); }
void exception_handle_18(){ kpanic("Machine Check"); }
-//set a handler for a specific interrupt
-void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr)
-{
- uint64_t base=(uint32_t)&(*(uint32_t*)addr);
-
- idt[irq].addrLo = base & 0xffff;
- idt[irq].addrHi = (base >> 16) & 0xffff;
- idt[irq].zeros=0;
- idt[irq].flags=flags;
- idt[irq].sel=sel;
-}
-
// set default handler for all interrupts for a start
void interrupts_init(uint16_t sel)
{
// Setup PIC
klog("setting up PIC",&idt,&idtd);
- pic_setup();
+ asm_pic_setup();
klog("initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd);
+ // Default interrupt handling
for(int i=0; i<INT_MAX; i++)
{
- int_install_ir(i, 0b10001110, sel,&int_default_handler);
+ int_install_ir(i, 0b10001110, sel,&int255);
}
- // exceptions
- int_install_ir(0, 0b10001110, 0x08,&int_irq0);
- int_install_ir(1, 0b10001110, 0x08,&int_irq1);
- int_install_ir(2, 0b10001110, 0x08,&int_irq2);
- int_install_ir(3, 0b10001110, 0x08,&int_irq3);
- int_install_ir(4, 0b10001110, 0x08,&int_irq4);
- int_install_ir(5, 0b10001110, 0x08,&int_irq5);
- int_install_ir(6, 0b10001110, 0x08,&int_irq6);
- int_install_ir(7, 0b10001110, 0x08,&int_irq7);
- int_install_ir(8, 0b10001110, 0x08,&int_irq8);
- int_install_ir(9, 0b10001110, 0x08,&int_irq9);
- int_install_ir(10, 0b10001110, 0x08,&int_irq10);
- int_install_ir(11, 0b10001110, 0x08,&int_irq11);
- int_install_ir(12, 0b10001110, 0x08,&int_irq12);
- int_install_ir(13, 0b10001110, 0x08,&int_irq13);
- int_install_ir(14, 0b10001110, 0x08,&int_irq14);
- int_install_ir(15, 0b10001110, 0x08,&int_irq15);
- int_install_ir(16, 0b10001110, 0x08,&int_irq16);
- int_install_ir(17, 0b10001110, 0x08,&int_irq17);
- int_install_ir(18, 0b10001110, 0x08,&int_irq18);
-
- // setup some custom interrupts
+ // Exceptions
+ int_install_ir(0, 0b10001110, 0x08,&exc0);
+ int_install_ir(1, 0b10001110, 0x08,&exc1);
+ int_install_ir(2, 0b10001110, 0x08,&exc2);
+ int_install_ir(3, 0b10001110, 0x08,&exc3);
+ int_install_ir(4, 0b10001110, 0x08,&exc4);
+ int_install_ir(5, 0b10001110, 0x08,&exc5);
+ int_install_ir(6, 0b10001110, 0x08,&exc6);
+ int_install_ir(7, 0b10001110, 0x08,&exc7);
+ int_install_ir(8, 0b10001110, 0x08,&exc8);
+ int_install_ir(9, 0b10001110, 0x08,&exc9);
+ int_install_ir(10, 0b10001110, 0x08,&exc10);
+ int_install_ir(11, 0b10001110, 0x08,&exc11);
+ int_install_ir(12, 0b10001110, 0x08,&exc12);
+ int_install_ir(13, 0b10001110, 0x08,&exc13);
+ int_install_ir(14, 0b10001110, 0x08,&exc14);
+ int_install_ir(15, 0b10001110, 0x08,&exc15);
+ int_install_ir(16, 0b10001110, 0x08,&exc16);
+ int_install_ir(17, 0b10001110, 0x08,&exc17);
+ int_install_ir(18, 0b10001110, 0x08,&exc18);
+
+
+ // Custom interrupts (hardcoded)
// remember that we shifted all interrupts with the pic by 32
- // install PIT interrupt handler (irq 0 => 32)
+ // PIT interrupt handler (irq 0 => 32)
int_install_ir(32, 0b10001110, 0x08,&int0);
- // install keyboard interrupt handler (irq 1 => 33)
+ // Keyboard interrupt handler (irq 1 => 33)
int_install_ir(33, 0b10001110, 0x08,&int1);
- //mouse interrupt handler (irq 12 => 34)
+ // Mouse interrupt handler (irq 12 => 34)
int_install_ir(44, 0b10001110, 0x08,&int12);
- //our system calls (can be called from ring3 (0b11))
+ // System Calls / they can be called from ring3 (0b11)
int_install_ir(0x80, 0b11101110, 0x08,&int128);
+ // Wake Scheduler
+ int_install_ir(0x81, 0b11101110, 0x08,&int129);
+ // install IVT
int_install();
}
-
-void int_install()
-{
- idtd.size=sizeof(struct int_desc)*INT_MAX;
-
- uint32_t addr=(uint32_t)&idt[0];
- idtd.baseHi=addr>>16;
- idtd.baseLo=0xffff&addr;
-
- __asm__("lidt %0"::"m" (idtd));
-}