feat(idt): add idt creation through idt entries descriptors

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2021-07-16 02:57:13 +02:00
parent 02811acc2c
commit af24d0f18a
2 changed files with 55 additions and 14 deletions

58
k/idt.c

@ -1,22 +1,57 @@
#include "idt.h" #include "idt.h"
#include "stdio.h"
static struct idt idt = { 0 }; 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 idt_entry create_idt_entry(struct idt_entry_descriptor descriptor)
struct segment_selector selector)
{ {
struct idt_entry res = { 0 }; struct idt_entry res = { 0 };
res.selector = selector; res.selector = descriptor.selector;
res.offset_1 = offset & 0xffff; res.offset_1 = descriptor.offset & 0xffff;
res.offset_2 = (offset >> 16) & 0xffff; res.offset_2 = (descriptor.offset >> 16) & 0xffff;
res.type = INTERRUPT_TYPE; res.type = descriptor.type;
res.size = 1; res.size = 1;
res.desc_priv = 0; res.desc_priv = 0;
res.present = 1; res.present = 1;
@ -26,14 +61,9 @@ struct idt_entry create_idt_entry(uint32_t offset,
static void create_idt(void) 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) for (int i = 0; i < IDT_NB_ENTRIES; ++i)
{ {
struct idt_entry entry = create_idt_entry((uint32_t)&handle_interrupt, struct idt_entry entry = create_idt_entry(idt_entries_descriptors[i]);
selector);
idt.entries[i] = entry; idt.entries[i] = entry;
} }
} }

11
k/idt.h

@ -3,11 +3,13 @@
#include <stdint.h> #include <stdint.h>
#include "gdt.h" #include "gdt.h"
#include "isr.h"
#define IDT_NB_ENTRIES 32 #define IDT_NB_ENTRIES 32
#define IDT_SIZE (IDT_NB_ENTRIES * sizeof(struct idt_entry)) #define IDT_SIZE (IDT_NB_ENTRIES * sizeof(struct idt_entry))
#define INTERRUPT_TYPE 0x6 #define INTERRUPT_TYPE 0x6
#define TRAP_TYPE 0x7
struct idt_entry struct idt_entry
{ {
@ -35,6 +37,15 @@ struct idt_r
uint32_t addr; uint32_t addr;
} __attribute__ ((packed)); } __attribute__ ((packed));
struct idt_entry_descriptor
{
uint32_t offset;
struct segment_selector selector;
char type;
};
void init_idt(void); void init_idt(void);
void interrupt_handler(struct isr_param *isr_param);
#endif /* !IDT_H */ #endif /* !IDT_H */