#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 interrupt descriptor table static struct int_desc { uint16_t addrLo; uint16_t sel; uint8_t zeros; uint8_t flags; uint16_t addrHi; } idt[INT_MAX]; // interrupt descriptor table descriptor static struct idt_desc { uint16_t size; uint16_t baseLo; uint16_t baseHi; } idtd; uint32_t interrupt_handler(uint32_t esp, uint32_t num) { if(num!=0)klog("int: %d %d",num,esp); if(num==0) { pit_interrupt_handler(); esp=task_switch_next(esp); } if(num==1)asm_kb_handler(); if(num==12)asm_mouse_handler(); 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); } return esp; } void errlog(uint32_t error_code) { klog("error_code: 0x%08X",error_code); } void defklog(uint32_t eip, uint16_t cs, uint32_t flags) { klog("eip: 0x%08X",eip); klog("segment: 0x%08X",cs); klog("eflags: 0x%08X",flags); } void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr); void exception_handle() { kpanic("exception interrupt"); } 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"); } void exception_handle_3(){ kpanic("Breakpoint (debugger)"); } void exception_handle_4(){ kpanic("Overflow"); } void exception_handle_5(){ kpanic("Bounds check"); } void exception_handle_6(){ kpanic("Undefined OP Code"); } void exception_handle_7(){ kpanic("No coprocessor"); } void exception_handle_8(){ kpanic("Double Fault"); } void exception_handle_9(){ kpanic("Coprocessor Segment Overrun"); } void exception_handle_10(){ kpanic("Invalid TSS"); } void exception_handle_11(){ kpanic("Segment Not Present"); } void exception_handle_12(){ kpanic("Stack Segment Overrun"); } void exception_handle_13(uint32_t error_code,uint32_t eip,uint16_t cs,uint16_t unused, uint32_t flags) { errlog(error_code); defklog(eip,cs,flags); kpanic("Exception: Fault: General Protection Fault"); } void exception_handle_14(uint32_t error_code,uint32_t eip,uint16_t cs,uint16_t unused, uint32_t flags) { errlog(error_code); klog("error_code_P: %d",error_code&1?1:0); klog("error_code_W/R: %d",error_code&2?1:0); klog("error_code_U/S: %d",error_code&4?1:0); 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); kpanic("Exception: Fault: Page Fault"); } void exception_handle_15(){ kpanic("Unassigned"); } 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(); klog("initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd); for(int i=0; i 32) int_install_ir(32, 0b10001110, 0x08,&int0); // install keyboard interrupt handler (irq 1 => 33) int_install_ir(33, 0b10001110, 0x08,&int1); //mouse interrupt handler (irq 12 => 34) int_install_ir(44, 0b10001110, 0x08,&int12); //our system calls (can be called from ring3 (0b11)) int_install_ir(0x80, 0b11101110, 0x08,&int128); 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)); }