From 474ba91122aa4b32fdd9348d90aec7468b3d2b8a Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 1 Feb 2015 21:35:36 -0500 Subject: a lotta changes, crashes on STI --- kernel/gdt-as.S | 2 +- kernel/idt.c | 2 ++ kernel/include/irq.h | 1 - kernel/include/isr.h | 2 ++ kernel/irq.c | 25 +++++------------- kernel/isr-as.S | 74 ++++++++++++++++++++++++++-------------------------- kernel/isr.c | 20 +++++++++++--- kernel/main.c | 13 +++++---- 8 files changed, 72 insertions(+), 67 deletions(-) (limited to 'kernel') diff --git a/kernel/gdt-as.S b/kernel/gdt-as.S index 7c8d630..10f051a 100644 --- a/kernel/gdt-as.S +++ b/kernel/gdt-as.S @@ -9,6 +9,6 @@ gdt_flush: # prototype: void gdt_flush(uint32) mov %ax, %fs mov %ax, %gs mov %ax, %ss - jmp .flush + jmp $0x8, $.flush .flush: ret diff --git a/kernel/idt.c b/kernel/idt.c index 9eda0ce..0108991 100644 --- a/kernel/idt.c +++ b/kernel/idt.c @@ -1,6 +1,8 @@ #include #include #include "idt.h" +#include "isr.h" +#include "irq.h" void idt_set_gate(uint8_t idx, uint32_t base, uint16_t sel, uint8_t flags) { diff --git a/kernel/include/irq.h b/kernel/include/irq.h index 1dcbc17..8fac8e1 100644 --- a/kernel/include/irq.h +++ b/kernel/include/irq.h @@ -17,5 +17,4 @@ extern void _irq15(void); struct regs_t; -void irq_set_handler(int irq, void (*handler)(struct regs_t*)); void irq_init(void); diff --git a/kernel/include/isr.h b/kernel/include/isr.h index d3543af..5d6d282 100644 --- a/kernel/include/isr.h +++ b/kernel/include/isr.h @@ -44,3 +44,5 @@ struct regs_t { uint32_t int_no, err_code; /* our 'push byte #' and ecodes do this */ uint32_t eip, cs, eflags, useresp, ss; /* pushed by the processor automatically */ }; + +void set_interrupt_handler(uint8_t interrupt, void (*func)(struct regs_t)); diff --git a/kernel/irq.c b/kernel/irq.c index 2498055..5d48c46 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -6,19 +6,8 @@ #include "irq.h" #include "isr.h" -static void *irq_callbacks[16] = { NULL }; - -void irq_set_handler(int irq, void (*handler)(struct regs_t*)) -{ - if(irq < 16 && irq >= 0) - irq_callbacks[irq] = handler; -} - -void irq_reset_handler(int irq) -{ - if(irq < 16 && irq >= 0) - irq_callbacks[irq] = NULL; -} +/* in isr.c */ +extern void *int_callbacks[256]; void irq_remap(void) { @@ -37,7 +26,7 @@ void irq_remap(void) void irq_init(void) { irq_remap(); - + printf("IRQ handlers installed.\n"); idt_set_gate(32, (uint32_t)_irq0, 0x08, 0x8E); idt_set_gate(33, (uint32_t)_irq1, 0x08, 0x8E); idt_set_gate(34, (uint32_t)_irq2, 0x08, 0x8E); @@ -56,11 +45,11 @@ void irq_init(void) idt_set_gate(47, (uint32_t)_irq15, 0x08, 0x8E); } -void irq_handler(struct regs_t *regs) +void irq_handler(struct regs_t regs) { - void (*handler)(struct regs_t *r); + void (*handler)(struct regs_t r); - handler = irq_callbacks[regs->int_no - 32]; + handler = int_callbacks[regs.int_no]; if(handler) { @@ -70,7 +59,7 @@ void irq_handler(struct regs_t *regs) /* If the IDT entry that was invoked was greater than 40 * (meaning IRQ8 - 15), then we need to send an EOI to * the slave controller */ - if (regs->int_no >= 40) + if (regs.int_no >= 40) { outb(0xA0, 0x20); } diff --git a/kernel/isr-as.S b/kernel/isr-as.S index 2bc77f3..464ec5b 100644 --- a/kernel/isr-as.S +++ b/kernel/isr-as.S @@ -1,4 +1,5 @@ -isr_handler: + .extern isr_handler +isr_stub: pusha push %ds push %es @@ -11,8 +12,7 @@ isr_handler: mov %ax, %gs mov %esp, %eax # Push us the stack push %eax - mov $isr_handler, %eax - call *%eax + call isr_handler pop %eax pop %gs pop %fs @@ -20,7 +20,6 @@ isr_handler: pop %ds popa add $8, %esp - sti iret # stub ISR's: @@ -56,159 +55,160 @@ isr_handler: .global _isr29 .global _isr30 .global _isr31 -# Interrupts 8, 10, 11, 12, 13, and 14 push error codes onto the stack + + # Interrupts 8, 10, 11, 12, 13, and 14 push error codes onto the stack _isr0: cli push $0 push $0 - jmp isr_handler + jmp isr_stub _isr1: cli push $0 push $1 - jmp isr_handler + jmp isr_stub _isr2: cli push $0 push $2 - jmp isr_handler + jmp isr_stub _isr3: cli push $0 push $3 - jmp isr_handler + jmp isr_stub _isr4: cli push $0 push $4 - jmp isr_handler + jmp isr_stub _isr5: cli push $0 push $5 - jmp isr_handler + jmp isr_stub _isr6: cli push $0 push $6 - jmp isr_handler + jmp isr_stub _isr7: cli push $0 push $7 - jmp isr_handler + jmp isr_stub _isr8: cli push $8 - jmp isr_handler + jmp isr_stub _isr9: cli push $0 push $9 - jmp isr_handler + jmp isr_stub _isr10: cli push $10 - jmp isr_handler + jmp isr_stub _isr11: cli push $11 - jmp isr_handler + jmp isr_stub _isr12: cli push $12 - jmp isr_handler + jmp isr_stub _isr13: cli push $13 - jmp isr_handler + jmp isr_stub _isr14: cli push $14 - jmp isr_handler + jmp isr_stub _isr15: cli push $0 push $15 - jmp isr_handler + jmp isr_stub _isr16: cli push $0 push $16 - jmp isr_handler + jmp isr_stub _isr17: cli push $0 push $17 - jmp isr_handler + jmp isr_stub _isr18: cli push $0 push $18 - jmp isr_handler + jmp isr_stub _isr19: cli push $0 push $19 - jmp isr_handler + jmp isr_stub _isr20: cli push $0 push $20 - jmp isr_handler + jmp isr_stub _isr21: cli push $0 push $21 - jmp isr_handler + jmp isr_stub _isr22: cli push $0 push $22 - jmp isr_handler + jmp isr_stub _isr23: cli push $0 push $23 - jmp isr_handler + jmp isr_stub _isr24: cli push $0 push $24 - jmp isr_handler + jmp isr_stub _isr25: cli push $0 push $25 - jmp isr_handler + jmp isr_stub _isr26: cli push $0 push $26 - jmp isr_handler + jmp isr_stub _isr27: cli push $0 push $27 - jmp isr_handler + jmp isr_stub _isr28: cli push $0 push $28 - jmp isr_handler + jmp isr_stub _isr29: cli push $0 push $29 - jmp isr_handler + jmp isr_stub _isr30: cli push $0 push $30 - jmp isr_handler + jmp isr_stub _isr31: cli push $0 push $31 - jmp isr_handler + jmp isr_stub diff --git a/kernel/isr.c b/kernel/isr.c index 424bd22..a4627d0 100644 --- a/kernel/isr.c +++ b/kernel/isr.c @@ -1,17 +1,31 @@ +#include #include #include "isr.h" #include "idt.h" +#include "panic.h" -void isr_handle(struct regs_t *regs) +void (*int_callbacks[256])(struct regs_t) = { NULL }; + +void set_interrupt_handler(uint8_t n, void (*callback)(struct regs_t)) +{ + int_callbacks[n] = callback; +} + +void isr_handler(struct regs_t regs) { - if(regs->int_no < 32) + if(int_callbacks[regs.int_no]) + { + int_callbacks[regs.int_no](regs); + } + else { - panic("received exception!\n"); + printf("WARNING: unhandled ISR"); } } void isr_init(void) { + printf("ISR handlers installed.\n"); idt_set_gate(0, (uint32_t)_isr0, 0x08, 0x8E); idt_set_gate(1, (uint32_t)_isr1, 0x08, 0x8E); idt_set_gate(2, (uint32_t)_isr2, 0x08, 0x8E); diff --git a/kernel/main.c b/kernel/main.c index fd56fb0..63ba635 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -5,9 +5,8 @@ #include "tty.h" #include -void irq1(struct regs_t *regs) +void interrupt(struct regs_t regs) { - printf("Keypress\n"); } void main(struct multiboot_header *hdr, uint32_t magic) @@ -15,17 +14,17 @@ void main(struct multiboot_header *hdr, uint32_t magic) tty_init(); gdt_init(); - printf("GDT initialized.\n"); - idt_init(); - printf("IDT initialized.\n"); isr_init(); irq_init(); - irq_set_handler(1, irq1); + for(int i=0;i<256;++i) + set_interrupt_handler(i, interrupt); + + asm("sti"); - printf("Hello, world!\n"); + printf("Boot finished.!\n"); while(1) ; } -- cgit v1.1