From af24d0f18a8fe5d9b33b490f4679c12314a27f63 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Fri, 16 Jul 2021 02:57:13 +0200 Subject: [PATCH] feat(idt): add idt creation through idt entries descriptors Signed-off-by: Julien CLEMENT --- k/idt.c | 58 +++++++++++++++++++++++++++++++++++++++++++-------------- k/idt.h | 11 +++++++++++ 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/k/idt.c b/k/idt.c index 0a2d74c..5975f40 100644 --- a/k/idt.c +++ b/k/idt.c @@ -1,22 +1,57 @@ #include "idt.h" +#include "stdio.h" static struct idt idt = { 0 }; -void handle_interrupt() +static struct idt_entry_descriptor idt_entries_descriptors[] = { + {(uint32_t)&isr_divide, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 0 + {(uint32_t)&isr_debug, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 1 + {(uint32_t)&isr_nmi, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 2 + {(uint32_t)&isr_breakpoint, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 3 + {(uint32_t)&isr_overflow, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 4 + {(uint32_t)&isr_bound_range_exceeded, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 5 + {(uint32_t)&isr_invalid_opcode, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 6 + {(uint32_t)&isr_device_not_available, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 7 + {(uint32_t)&isr_double_fault, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 8 + {(uint32_t)&isr_coprocessor_segment_overrun, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 9 + {(uint32_t)&isr_invalid_tss, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 10 + {(uint32_t)&isr_segment_not_present, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 11 + {(uint32_t)&isr_stack_segment_fault, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 12 + {(uint32_t)&isr_general_protection, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 13 + {(uint32_t)&isr_page_fault, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 14 + {0, {0, 0, 0}, 0}, // 15 + {(uint32_t)&isr_fpu_floating_point_error, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 16 + {(uint32_t)&isr_alignment_check, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 17 + {(uint32_t)&isr_machine_check, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 18 + {(uint32_t)&isr_simd_floating_point_exception, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 19 + {(uint32_t)&isr_virtualization_exception, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 20 + {(uint32_t)&isr_control_protection_exception, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 21 + {0, {0, 0, 0}, 0}, // 22 + {0, {0, 0, 0}, 0}, // 23 + {0, {0, 0, 0}, 0}, // 24 + {0, {0, 0, 0}, 0}, // 25 + {0, {0, 0, 0}, 0}, // 26 + {0, {0, 0, 0}, 0}, // 27 + {0, {0, 0, 0}, 0}, // 28 + {0, {0, 0, 0}, 0}, // 29 + {0, {0, 0, 0}, 0}, // 30 + {0, {0, 0, 0}, 0} // 31 +}; + +void interrupt_handler(struct isr_param *isr_param) { - return; + printf("Oh no %d!\r\n", isr_param->int_vector); } -struct idt_entry create_idt_entry(uint32_t offset, - struct segment_selector selector) +struct idt_entry create_idt_entry(struct idt_entry_descriptor descriptor) { struct idt_entry res = { 0 }; - res.selector = selector; - res.offset_1 = offset & 0xffff; - res.offset_2 = (offset >> 16) & 0xffff; + res.selector = descriptor.selector; + res.offset_1 = descriptor.offset & 0xffff; + res.offset_2 = (descriptor.offset >> 16) & 0xffff; - res.type = INTERRUPT_TYPE; + res.type = descriptor.type; res.size = 1; res.desc_priv = 0; res.present = 1; @@ -26,14 +61,9 @@ struct idt_entry create_idt_entry(uint32_t offset, static void create_idt(void) { - struct segment_selector selector; - selector.rpl = 0; - selector.table_indicator = 0; - selector.index = KERNEL_CS_INDEX; for (int i = 0; i < IDT_NB_ENTRIES; ++i) { - struct idt_entry entry = create_idt_entry((uint32_t)&handle_interrupt, - selector); + struct idt_entry entry = create_idt_entry(idt_entries_descriptors[i]); idt.entries[i] = entry; } } diff --git a/k/idt.h b/k/idt.h index 39a1857..2d6c276 100644 --- a/k/idt.h +++ b/k/idt.h @@ -3,11 +3,13 @@ #include #include "gdt.h" +#include "isr.h" #define IDT_NB_ENTRIES 32 #define IDT_SIZE (IDT_NB_ENTRIES * sizeof(struct idt_entry)) #define INTERRUPT_TYPE 0x6 +#define TRAP_TYPE 0x7 struct idt_entry { @@ -35,6 +37,15 @@ struct idt_r uint32_t addr; } __attribute__ ((packed)); +struct idt_entry_descriptor +{ + uint32_t offset; + struct segment_selector selector; + char type; +}; + void init_idt(void); +void interrupt_handler(struct isr_param *isr_param); + #endif /* !IDT_H */