diff --git a/kernel/Makefile b/kernel/Makefile index a609b72..4c9eff3 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -20,9 +20,11 @@ LIBS:=$(LIBS) -nostdlib -lk -lgcc ARCHDIR=arch/$(HOSTARCH) MMUDIR=mmu +DEVICEDIR=arch/devices include $(ARCHDIR)/make.config include ${MMUDIR}/make.config +include ${DEVICEDIR}/make.config CFLAGS:=$(CFLAGS) $(KERNEL_ARCH_CFLAGS) CPPFLAGS:=$(CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS) @@ -32,7 +34,8 @@ LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS) KERNEL_OBJS=\ $(KERNEL_ARCH_OBJS) \ kernel/kernel.o \ -${MMU_OBJS} +${MMU_OBJS} \ +${KERNEL_DEVICE_OBJS} OBJS=\ $(ARCHDIR)/crti.o \ @@ -41,6 +44,7 @@ $(KERNEL_OBJS) \ $(ARCHDIR)/crtend.o \ $(ARCHDIR)/crtn.o \ ${MMU_OBJS} \ +${KERNEL_DEVICE_OBJS} \ LINK_LIST=\ $(LDFLAGS) \ diff --git a/kernel/arch/i386/make.config b/kernel/arch/i386/make.config index 95c7199..ada6ad5 100644 --- a/kernel/arch/i386/make.config +++ b/kernel/arch/i386/make.config @@ -6,6 +6,7 @@ KERNEL_ARCH_LIBS= KERNEL_ARCH_OBJS=\ $(ARCHDIR)/boot.o \ $(ARCHDIR)/tty.o \ -${ARCHDIR}/x86_int.o \ +${ARCHDIR}/interrupts.o \ ${ARCHDIR}/x86.o \ +${ARCHDIR}/asm/interrupts.o \ diff --git a/kernel/arch/i386/x86.c b/kernel/arch/i386/x86.c index 5dfa181..d0e135d 100644 --- a/kernel/arch/i386/x86.c +++ b/kernel/arch/i386/x86.c @@ -7,21 +7,27 @@ struct idtr kidtr; struct idtdesc kidt[256]; uint8_t stack[STACKSIZE]; +extern void gdt_flush(); +extern void _asm_int_0(); +extern void _asm_int_1(); +extern void isr0(); -void read_gdtr(uint32_t* tgdtr) { - asm("sgdt %0" : "=m" (*tgdtr)); +void load_idt(struct idtr *idtr) { + asm volatile("lidt %0" : : "m" (*idtr)); } -void load_gdt(struct gdtr *gdtr) { - asm volatile("lgdt %0" : : "m" (*gdtr)); +void outb(uint32_t ad, uint8_t v) +{ + asm volatile("outb %%al, %%dx" :: "d" (ad), "a" (v)); } -void load_idt(struct idtr *idtr) { - asm volatile("lidt %0" : : "m" (*idtr)); +uint8_t inb(uint32_t ad) +{ + uint8_t v; + asm volatile("inb %%dx, %%al" : "=a" (v) : "d" (ad)); + return v; } -//extern void gdt_flush(); - void gdt_init(void) { create_gdt_descriptor(0x0, 0x0, 0x0, 0x0, &kgdt[0]); @@ -50,37 +56,15 @@ void create_gdt_descriptor(uint32_t base, uint32_t limit, uint8_t access, uint8_ return; } -void create_idt_descriptor(uint16_t select, uint32_t offset, uint8_t type, struct idtdesc * desc) { - //printf("select: %d, offset: %d, type: %d\n", select, offset, type); - desc->offset0_15 = (offset & 0xFFFF); - desc->select = select; - desc->access_gran = type; - desc->alwaysZero = 0; - desc->offset16_31 = (offset & 0xFFFF0000) >> 16; +void create_idt_descriptor(uint16_t select, uint32_t offset, uint8_t type, uint8_t num) { + kidt[num].offset0_15 = (offset & 0xFFFF); + kidt[num].select = select; + kidt[num].access_gran = type; + kidt[num].alwaysZero = 0; + kidt[num].offset16_31 = (offset & 0xFFFF0000) >> 16; return; } -extern void _asm_int_0(); -extern void _asm_int_1(); -extern void isr0(); - -void outb(uint32_t ad, uint8_t v) -{ - asm volatile("outb %%al, %%dx" :: "d" (ad), "a" (v)); -} - -uint8_t inb(uint32_t ad) -{ - uint8_t v; - asm volatile("inb %%dx, %%al" : "=a" (v) : "d" (ad)); - printf("IN: %d\n", v); - return v; -} -void isr_default_int(int id) -{ - printf("default int %d", id); - asm volatile("hlt"); -} void init_pic(void) { @@ -112,210 +96,4 @@ void init_idt(void) { printf("IDT initialized\n"); } -void *irq_routines[16] ={ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0 -}; - -void fault_handler(struct regs *r) -{ - printf("ERROR everywhere\n"); - if (r->int_no < 32) - { - printf("ERROR here\n"); - asm("hlt"); - } else { - printf("ERROR there\n"); - asm("hlt"); - } -} - -void setup_isr(void) { - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[0]); - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[1]); - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[2]); - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[3]); - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[4]); - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[5]); - create_idt_descriptor(0x08, (uint32_t)isr0, INTGATE, &kidt[6]); -} - -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(); -void irq_install_handler(int irq, void (*handler)(struct regs *r)){ - printf("INSTALL %d\n", irq); - irq_routines[irq] = handler; -} -void irq_uninstall_handler(int irq){ - irq_routines[irq] = 0; -} - -void irq_install(){ - create_idt_descriptor(0x08, (unsigned)irq0, INTGATE, &kidt[32]); - create_idt_descriptor(0x08, (unsigned)irq1, INTGATE, &kidt[33]); - create_idt_descriptor(0x08, (unsigned)irq2, INTGATE, &kidt[34]); - create_idt_descriptor(0x08, (unsigned)irq3, INTGATE, &kidt[35]); - create_idt_descriptor(0x08, (unsigned)irq4, INTGATE, &kidt[36]); - create_idt_descriptor(0x08, (unsigned)irq5, INTGATE, &kidt[37]); - create_idt_descriptor(0x08, (unsigned)irq6, INTGATE, &kidt[38]); - create_idt_descriptor(0x08, (unsigned)irq7, INTGATE, &kidt[39]); - create_idt_descriptor(0x08, (unsigned)irq8, INTGATE, &kidt[40]); - create_idt_descriptor(0x08, (unsigned)irq9, INTGATE, &kidt[41]); - create_idt_descriptor(0x08, (unsigned)irq10, INTGATE, &kidt[42]); - create_idt_descriptor(0x08, (unsigned)irq11, INTGATE, &kidt[43]); - create_idt_descriptor(0x08, (unsigned)irq12, INTGATE, &kidt[44]); - create_idt_descriptor(0x08, (unsigned)irq13, INTGATE, &kidt[45]); - create_idt_descriptor(0x08, (unsigned)irq14, INTGATE, &kidt[46]); - create_idt_descriptor(0x08, (unsigned)irq15, INTGATE, &kidt[47]); -} - -void irq_handler(struct regs *r){ - void (*handler)(struct regs *r); - - handler = irq_routines[r->int_no - 32]; - if (handler) - { - handler(r); - } - - // * 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 (r->int_no >= 40) - { - outb(0xA0, 0x20); - } - - // * In either case, we need to send an EOI to the master - // * interrupt controller too - outb(0x20, 0x20); -} - -int shift = 0; -unsigned char kbdus[256] = { - 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */ - '9', '0', '-', '=', '\b', /* Backspace */ - '\t', /* Tab */ - 'q', 'w', 'e', 'r', /* 19 */ - 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */ - 0, /* 29 - Control */ - 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */ - '\'', '`', 0, /* Left shift */ - '\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */ - 'm', ',', '.', '/', 0, /* Right shift */ - '*', - 0, /* Alt */ - ' ', /* Space bar */ - 0, /* Caps lock */ - 0, /* 59 - F1 key ... > */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, /* < ... F10 */ - 0, /* 69 - Num lock*/ - 0, /* Scroll Lock */ - 0, /* Home key */ - 0, /* Up Arrow */ - 0, /* Page Up */ - '-', - 0, /* Left Arrow */ - 0, - 0, /* Right Arrow */ - '+', - 0, /* 79 - End key*/ - 0, /* Down Arrow */ - 0, /* Page Down */ - 0, /* Insert Key */ - 0, /* Delete Key */ - 0, 0, 0, - 0, /* F11 Key */ - 0, /* F12 Key */ - 0, - 0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', - '\b', '\t', - 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', - 0, - 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', - '\"', '~', 0, '|', - 'Z', 'X', 'C', 'V', 'B', 'N', 'M', - '<', '>', '?', 0, '*', - 0, /* Alt */ - ' ', /* Space bar */ - 0, /* Caps lock */ - 0, /* 59 - F1 key ... > */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, /* < ... F10 */ - 0, /* 69 - Num lock*/ - 0, /* Scroll Lock */ - 0, /* Home key */ - 0, /* Up Arrow */ - 0, /* Page Up */ - '-', - 0, /* Left Arrow */ - 0, - 0, /* Right Arrow */ - '+', - 0, /* 79 - End key*/ - 0, /* Down Arrow */ - 0, /* Page Down */ - 0, /* Insert Key */ - 0, /* Delete Key */ - 0, 0, 0, - 0, /* F11 Key */ - 0, /* F12 Key */ - 0 - -}; - -void pause(){ - int i = 0; - while(i < 100000000){ - i++; - } - while(inb(0x60) & 0x80){ - - }; -} -void keyboard_handler(struct regs *r){ - unsigned char scancode; - printf("KEYBOARDd\n"); - scancode = inb(0x60); - - if (scancode & 0x80) - { - if(scancode == 0xAA) - shift = 0; - } - else - { - if(scancode == 0x2a){shift = 1;return;} - // if(scancode == 0x1c){shell_enterpressed();return;} -/* if(scancode == 0x0e){ - if(shell_can_backspace()){ - printchar(kbdus[scancode]); - } - shell_backspace(); - return; - }*/ - if(shift){ - printf("%c",kbdus[scancode+90]); -// shell_addchar(kbdus[scancode+90]); - }else{ - printf("%c",kbdus[scancode]); -// shell_addchar(kbdus[scancode]); - } - } -} diff --git a/kernel/arch/i386/x86_int.asm b/kernel/arch/i386/x86_int.asm deleted file mode 100644 index 9dea05a..0000000 --- a/kernel/arch/i386/x86_int.asm +++ /dev/null @@ -1,262 +0,0 @@ - - -global gdt_flush -extern kgdtr -gdt_flush: - lgdt [kgdtr] ; Load the GDT with our '_gp' which is a special pointer - mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - jmp 0x08:flush2 ; 0x08 is the offset to our code segment: Far jump! -flush2: - ret ; Returns back to the C code! - - -global idt_load -extern kidtr -idt_load: - lidt [kidtr] - ret - -global isr0 -global isr1 -global isr2 -global isr3 -global isr4 -global isr5 -global isr6 -global isr7 -global isr8 -global isr9 -global isr10 -global isr11 -global isr12 -global isr13 -global isr14 -global isr15 -global isr16 -global isr17 -global isr18 -global isr19 -global isr20 -global isr21 -global isr22 -global isr23 -global isr24 -global isr25 -global isr26 -global isr27 -global isr28 -global isr29 -global isr30 -global isr31 - -%macro ISR_NOCODE 1 - isr%1: - cli - push byte 0 - push byte %1 - jmp isr_common_stub -%endmacro - -%macro ISR_CODE 1 - isr%1: - cli - push byte %1 - jmp isr_common_stub -%endmacro - - -;Divide By Zero Exception -ISR_NOCODE 0 - -;Debug Exception -ISR_NOCODE 1 - -;Non Maskable Interrrupt Exception -ISR_NOCODE 2 - -;Breakpoint Exception -ISR_NOCODE 3 - -;Into Detected Overflow Exception -ISR_NOCODE 4 - -;Out of Bounds Exception -ISR_NOCODE 5 - -;Invalid Opcode Exception -ISR_NOCODE 6 - -;No Coprocessor Exception -ISR_NOCODE 7 - -;Double Fault Exception -ISR_CODE 8 - -;Coprocessor Segment Overrun Exception -ISR_NOCODE 9 - -;Bad TSS Exception -ISR_CODE 10 - -;Segment Not Present Exception -ISR_CODE 11 - -;Stack Fault Exception -ISR_CODE 12 - -;General Protection Fault Exception -ISR_CODE 13 - -;Page Fault Exception -ISR_CODE 14 - -;Unknown Interrupt Exception -ISR_NOCODE 15 - -;Coprocessor Fault Exception -ISR_NOCODE 16 - -;Alignment Check Exception -ISR_NOCODE 17 - -;Machine Check Exception -ISR_NOCODE 18 - -;Reserved -ISR_NOCODE 19 - -;Reserved -ISR_NOCODE 20 - -;Reserved -ISR_NOCODE 21 - -;Reserved -ISR_NOCODE 22 - -;Reserved -ISR_NOCODE 23 - -;Reserved -ISR_NOCODE 24 - -;Reserved -ISR_NOCODE 25 - -;Reserved -ISR_NOCODE 26 - -;Reserved -ISR_NOCODE 27 - -;Reserved -ISR_NOCODE 28 - -;Reserved -ISR_NOCODE 29 - -;Reserved -ISR_NOCODE 30 - -;Reserved -ISR_NOCODE 31 - -extern fault_handler -isr_common_stub: - pusha - push ds - push es - push fs - push gs - mov ax, 0x10 ; Load the Kernel Data Segment descriptor! - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov eax, esp ; Push us the stack - push eax - mov eax, fault_handler - call eax ; A special call, preserves the 'eip' register - pop eax - pop gs - pop fs - pop es - pop ds - popa - add esp, 8 ; Cleans up the pushed error code and pushed ISR number - iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP! - -global irq0 -global irq1 -global irq2 -global irq3 -global irq4 -global irq5 -global irq6 -global irq7 -global irq8 -global irq9 -global irq10 -global irq11 -global irq12 -global irq13 -global irq14 -global irq15 - -%macro make_irq 1 - irq%1: - cli - push byte 0 - push byte %1+32 - jmp irq_common_stub -%endmacro - -make_irq 0 -make_irq 1 -make_irq 2 -make_irq 3 -make_irq 4 -make_irq 5 -make_irq 6 -make_irq 7 -make_irq 8 -make_irq 9 -make_irq 10 -make_irq 11 -make_irq 12 -make_irq 13 -make_irq 14 -make_irq 15 - -extern irq_handler - -; This is a stub that we have created for IRQ based ISRs. This calls -; '_irq_handler' in our C code. We need to create this in an 'irq.c' -irq_common_stub: - pusha - push ds - push es - push fs - push gs - mov ax, 0x10 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov eax, esp - push eax - mov eax, irq_handler - call eax - pop eax - pop gs - pop fs - pop es - pop ds - popa - add esp, 8 - iret diff --git a/kernel/include/arch/i386/x86.h b/kernel/include/arch/i386/x86.h index 1a0d2d6..46d5862 100644 --- a/kernel/include/arch/i386/x86.h +++ b/kernel/include/arch/i386/x86.h @@ -92,21 +92,16 @@ void init_gdt(void); void init_idt(void); -void create_idt_descriptor(uint16_t, uint32_t, uint8_t, struct idtdesc *); +void create_idt_descriptor(uint16_t, uint32_t, uint8_t, uint8_t); void init_pic(void); void setup_isr(void); -void irq_install_handler(int irq, void (*handler)(struct regs *r)); -void irq_uninstall_handler(int irq); -void irq_remap(void); -void irq_install(); -void irq_handler(struct regs *r); +void outb(uint32_t , uint8_t ); +uint8_t inb(uint32_t ); -void keyboard_handler(struct regs *r); -void pause(); #endif diff --git a/kernel/include/kernel/tty.h b/kernel/include/kernel/tty.h index 07fca3c..6fd4f4b 100644 --- a/kernel/include/kernel/tty.h +++ b/kernel/include/kernel/tty.h @@ -7,5 +7,6 @@ void terminal_initialize(void); void terminal_putchar(char c); void terminal_write(const char* data, size_t size); void terminal_writestring(const char* data); +void terminal_new_line(void); -#endif \ No newline at end of file +#endif diff --git a/kernel/kernel/kernel.c b/kernel/kernel/kernel.c index 548feb8..9302250 100644 --- a/kernel/kernel/kernel.c +++ b/kernel/kernel/kernel.c @@ -1,6 +1,8 @@ #include #include -//#include +#include +#include +#include #include #include void divide_by_zero(void); @@ -33,7 +35,7 @@ void kernel_main(void) { char* test2= (char*) kmalloc(20); printf("Memory Address for test2: %d\n", test2);*/ while(inb(0x60) & 0x80){}; - divide_by_zero(); +// divide_by_zero(); asm volatile("sti"); printf("DONE\n"); while(1) {