Implement ISRs and fault handling

This commit is contained in:
Jenny Curle 2019-04-07 19:25:27 +01:00
parent d845a64a4e
commit 388834ef8a
6 changed files with 222 additions and 2 deletions

87
arch/i386/isr.s Executable file
View File

@ -0,0 +1,87 @@
%macro ISR 1
global isr%1
isr%1:
cli
push byte 0
push byte %1
jmp isr_common
%endmacro
%macro ISR_ERR 1
global isr%1
isr%1:
cli
push byte %1
jmp isr_common
%endmacro
%macro IRQ 2
global irq%1
irq%1:
cli
push byte 0
push byte %2
jmp isr_common
%endmacro
ISR 0
ISR 1
ISR 2
ISR 3
ISR 4
ISR 5
ISR 6
ISR 7
ISR_ERR 8
ISR 9
ISR_ERR 10
ISR_ERR 11
ISR_ERR 12
ISR_ERR 13
ISR_ERR 14
ISR 15
ISR 16
ISR_ERR 17
ISR 18
ISR 19
ISR 20
ISR 21
ISR 22
ISR 23
ISR 24
ISR 25
ISR 26
ISR 27
ISR 28
ISR 29
ISR_ERR 30
ISR 31
extern fault_handler
isr_common:
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, fault_handler
call eax
pop eax
pop gs
pop fs
pop es
pop ds
popa
add esp, 8
iret

View File

@ -6,4 +6,5 @@ KERNEL_ARCH_LIBS=
KERNEL_ARCH_OBJS= \ KERNEL_ARCH_OBJS= \
$(ARCHDIR)/boot.o \ $(ARCHDIR)/boot.o \
$(ARCHDIR)/gdt.o \ $(ARCHDIR)/gdt.o \
$(ARCHDIR)/isr.o \
$(ARCHDIR)/tty.o $(ARCHDIR)/tty.o

View File

@ -1,7 +1,19 @@
#pragma once #pragma once
#include <stddef.h>
#include <stdint.h>
void gdt_set_gate(int, unsigned long, unsigned long, unsigned char, unsigned char); void gdt_set_gate(int, unsigned long, unsigned long, unsigned char, unsigned char);
void gdt_install(); void gdt_install();
void idt_set_gate(unsigned char, unsigned long, unsigned short, unsigned char); void idt_set_gate(unsigned char, unsigned long, unsigned short, unsigned char);
void idt_install(); void idt_install();
typedef struct registers {
uint32_t ds;
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t int_no, err_code;
uint32_t eip, cs, eflags, useresp, ss;
} registers_t;
void isr_install();

115
kernel/isr.c Executable file
View File

@ -0,0 +1,115 @@
#include <kernel/descriptor_tables.h>
#include <kernel/tty.h>
#include <kernel/utils.h>
#include <stdint.h>
//These are all reserved by Intel, and need to be here.
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();
void isr_install() {
idt_set_gate(0, (unsigned)isr0, 0x00, 0x8E);
idt_set_gate(1, (unsigned)isr1, 0x00, 0x8E);
idt_set_gate(2, (unsigned)isr2, 0x00, 0x8E);
idt_set_gate(3, (unsigned)isr3, 0x00, 0x8E);
idt_set_gate(4, (unsigned)isr4, 0x00, 0x8E);
idt_set_gate(5, (unsigned)isr5, 0x00, 0x8E);
idt_set_gate(6, (unsigned)isr6, 0x00, 0x8E);
idt_set_gate(7, (unsigned)isr7, 0x00, 0x8E);
idt_set_gate(8, (unsigned)isr8, 0x00, 0x8E);
idt_set_gate(9, (unsigned)isr9, 0x00, 0x8E);
idt_set_gate(10, (unsigned)isr10, 0x00, 0x8E);
idt_set_gate(11, (unsigned)isr11, 0x00, 0x8E);
idt_set_gate(12, (unsigned)isr12, 0x00, 0x8E);
idt_set_gate(13, (unsigned)isr13, 0x00, 0x8E);
idt_set_gate(14, (unsigned)isr14, 0x00, 0x8E);
idt_set_gate(15, (unsigned)isr15, 0x00, 0x8E);
idt_set_gate(16, (unsigned)isr16, 0x00, 0x8E);
idt_set_gate(17, (unsigned)isr17, 0x00, 0x8E);
idt_set_gate(18, (unsigned)isr18, 0x00, 0x8E);
idt_set_gate(19, (unsigned)isr19, 0x00, 0x8E);
idt_set_gate(20, (unsigned)isr20, 0x00, 0x8E);
idt_set_gate(21, (unsigned)isr21, 0x00, 0x8E);
idt_set_gate(22, (unsigned)isr22, 0x00, 0x8E);
idt_set_gate(23, (unsigned)isr23, 0x00, 0x8E);
idt_set_gate(24, (unsigned)isr24, 0x00, 0x8E);
idt_set_gate(25, (unsigned)isr25, 0x00, 0x8E);
idt_set_gate(26, (unsigned)isr26, 0x00, 0x8E);
idt_set_gate(27, (unsigned)isr27, 0x00, 0x8E);
idt_set_gate(28, (unsigned)isr28, 0x00, 0x8E);
idt_set_gate(29, (unsigned)isr29, 0x00, 0x8E);
idt_set_gate(30, (unsigned)isr30, 0x00, 0x8E);
idt_set_gate(31, (unsigned)isr31, 0x00, 0x8E);
}
const 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",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};
void fault_handler(registers_t *r) {
if(r->int_no < 32) {
term_write(exception_messages[r->int_no], strlen(exception_messages[r->int_no]));
puts(" Exception. Halting.");
for(;;);
}
}

View File

@ -20,6 +20,7 @@ int kernel_main(void) {
// Prepare interrupts // Prepare interrupts
serial_print(0x3F8, "[INFO] Beginning IDT subroutine.\n"); serial_print(0x3F8, "[INFO] Beginning IDT subroutine.\n");
idt_install(); idt_install();
isr_install();
//Print a copyright message. //Print a copyright message.
term_writes("(c)"); term_writes("(c)");

View File

@ -33,6 +33,7 @@ kernel/utils.o \
kernel/serial.o \ kernel/serial.o \
kernel/gdt.o \ kernel/gdt.o \
kernel/idt.o \ kernel/idt.o \
kernel/isr.o \
kernel/kernel.o kernel/kernel.o
OBJS=\ OBJS=\
@ -65,6 +66,9 @@ $(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
$(ARCHDIR)/gdt.o: $(ARCHDIR)/gdt.o:
nasm -f elf $(ARCHDIR)/gdt.s nasm -f elf $(ARCHDIR)/gdt.s
$(ARCHDIR)/isr.o:
nasm -f elf $(ARCHDIR)/isr.s
.c.o: .c.o:
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS) $(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
.s.o: .s.o: