diff --git a/k/Makefile b/k/Makefile index 02ba1c9..4ed6e32 100644 --- a/k/Makefile +++ b/k/Makefile @@ -31,7 +31,8 @@ OBJS = \ list.o \ memory.o \ serial.o \ - gdt.o + gdt.o \ + idt.o DEPS = $(OBJS:.o=.d) diff --git a/k/gdt.c b/k/gdt.c index c8e0182..42b53eb 100644 --- a/k/gdt.c +++ b/k/gdt.c @@ -2,7 +2,7 @@ static struct gdt gdt = { 0 }; -static void create_kernel_ds() +static void create_kernel_ds(void) { struct gdt_entry *entry = gdt.entries + KERNEL_DS_INDEX; @@ -30,7 +30,7 @@ static void create_kernel_ds() entry->base_3 = (base >> 24) & 0xff; } -static void create_kernel_cs() +static void create_kernel_cs(void) { struct gdt_entry *entry = gdt.entries + KERNEL_CS_INDEX; @@ -58,13 +58,13 @@ static void create_kernel_cs() entry->base_3 = (base >> 24) & 0xff; } -static void create_gdt() +static void create_gdt(void) { create_kernel_ds(); create_kernel_cs(); } -static void load_gdt() +static void load_gdt(void) { struct gdt_r gdtr; gdtr.addr = (uint32_t)&gdt; @@ -76,7 +76,7 @@ static void load_gdt() : "memory"); } -static void load_ds() +static void load_ds(void) { struct segment_selector selector; selector.index = KERNEL_DS_INDEX; @@ -93,7 +93,7 @@ static void load_ds() : "ax"); } -static void load_cs() +static void load_cs(void) { struct segment_selector selector; selector.index = KERNEL_CS_INDEX; @@ -108,13 +108,13 @@ static void load_cs() : "m" (selector)); } -static void reload_segment_selectors() +static void reload_segment_selectors(void) { load_ds(); load_cs(); } -void init_gdt() +void init_gdt(void) { create_gdt(); load_gdt(); diff --git a/k/gdt.h b/k/gdt.h index c2833cd..7704d0b 100644 --- a/k/gdt.h +++ b/k/gdt.h @@ -60,6 +60,6 @@ struct segment_selector uint16_t index : 13; // index of the entry in the table } __attribute__ ((packed)); -void init_gdt(); +void init_gdt(void); #endif /* !GDT_H */ diff --git a/k/idt.c b/k/idt.c index 510a626..0a2d74c 100644 --- a/k/idt.c +++ b/k/idt.c @@ -2,7 +2,44 @@ static struct idt idt = { 0 }; -static void load_idt() +void handle_interrupt() +{ + return; +} + +struct idt_entry create_idt_entry(uint32_t offset, + struct segment_selector selector) +{ + struct idt_entry res = { 0 }; + + res.selector = selector; + res.offset_1 = offset & 0xffff; + res.offset_2 = (offset >> 16) & 0xffff; + + res.type = INTERRUPT_TYPE; + res.size = 1; + res.desc_priv = 0; + res.present = 1; + + return res; +} + +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); + idt.entries[i] = entry; + } +} + + +static void load_idt(void) { struct idt_r idtr; idtr.addr = (uint32_t)&idt; @@ -14,7 +51,8 @@ static void load_idt() : "memory"); } -void init_idt() +void init_idt(void) { + create_idt(); load_idt(); } diff --git a/k/idt.h b/k/idt.h index c5613f0..39a1857 100644 --- a/k/idt.h +++ b/k/idt.h @@ -7,6 +7,8 @@ #define IDT_NB_ENTRIES 32 #define IDT_SIZE (IDT_NB_ENTRIES * sizeof(struct idt_entry)) +#define INTERRUPT_TYPE 0x6 + struct idt_entry { uint16_t offset_1 : 16; @@ -33,4 +35,6 @@ struct idt_r uint32_t addr; } __attribute__ ((packed)); +void init_idt(void); + #endif /* !IDT_H */ diff --git a/k/k.c b/k/k.c index 40a4ae5..ff57813 100644 --- a/k/k.c +++ b/k/k.c @@ -23,15 +23,17 @@ */ #include -#include "multiboot.h" -#include "stdio.h" -#include "serial.h" #include "gdt.h" +#include "idt.h" +#include "multiboot.h" +#include "serial.h" +#include "stdio.h" static void k_init(void) { init_serial(); init_gdt(); + init_idt(); } void k_main(unsigned long magic, multiboot_info_t *info)