feat(idt): add idt creation

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2021-07-14 22:57:38 +02:00
parent 48cc6590a9
commit 96610353c8
6 changed files with 60 additions and 15 deletions

@ -31,7 +31,8 @@ OBJS = \
list.o \ list.o \
memory.o \ memory.o \
serial.o \ serial.o \
gdt.o gdt.o \
idt.o
DEPS = $(OBJS:.o=.d) DEPS = $(OBJS:.o=.d)

16
k/gdt.c

@ -2,7 +2,7 @@
static struct gdt gdt = { 0 }; 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; struct gdt_entry *entry = gdt.entries + KERNEL_DS_INDEX;
@ -30,7 +30,7 @@ static void create_kernel_ds()
entry->base_3 = (base >> 24) & 0xff; 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; struct gdt_entry *entry = gdt.entries + KERNEL_CS_INDEX;
@ -58,13 +58,13 @@ static void create_kernel_cs()
entry->base_3 = (base >> 24) & 0xff; entry->base_3 = (base >> 24) & 0xff;
} }
static void create_gdt() static void create_gdt(void)
{ {
create_kernel_ds(); create_kernel_ds();
create_kernel_cs(); create_kernel_cs();
} }
static void load_gdt() static void load_gdt(void)
{ {
struct gdt_r gdtr; struct gdt_r gdtr;
gdtr.addr = (uint32_t)&gdt; gdtr.addr = (uint32_t)&gdt;
@ -76,7 +76,7 @@ static void load_gdt()
: "memory"); : "memory");
} }
static void load_ds() static void load_ds(void)
{ {
struct segment_selector selector; struct segment_selector selector;
selector.index = KERNEL_DS_INDEX; selector.index = KERNEL_DS_INDEX;
@ -93,7 +93,7 @@ static void load_ds()
: "ax"); : "ax");
} }
static void load_cs() static void load_cs(void)
{ {
struct segment_selector selector; struct segment_selector selector;
selector.index = KERNEL_CS_INDEX; selector.index = KERNEL_CS_INDEX;
@ -108,13 +108,13 @@ static void load_cs()
: "m" (selector)); : "m" (selector));
} }
static void reload_segment_selectors() static void reload_segment_selectors(void)
{ {
load_ds(); load_ds();
load_cs(); load_cs();
} }
void init_gdt() void init_gdt(void)
{ {
create_gdt(); create_gdt();
load_gdt(); load_gdt();

@ -60,6 +60,6 @@ struct segment_selector
uint16_t index : 13; // index of the entry in the table uint16_t index : 13; // index of the entry in the table
} __attribute__ ((packed)); } __attribute__ ((packed));
void init_gdt(); void init_gdt(void);
#endif /* !GDT_H */ #endif /* !GDT_H */

42
k/idt.c

@ -2,7 +2,44 @@
static struct idt idt = { 0 }; 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; struct idt_r idtr;
idtr.addr = (uint32_t)&idt; idtr.addr = (uint32_t)&idt;
@ -14,7 +51,8 @@ static void load_idt()
: "memory"); : "memory");
} }
void init_idt() void init_idt(void)
{ {
create_idt();
load_idt(); load_idt();
} }

@ -7,6 +7,8 @@
#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
struct idt_entry struct idt_entry
{ {
uint16_t offset_1 : 16; uint16_t offset_1 : 16;
@ -33,4 +35,6 @@ struct idt_r
uint32_t addr; uint32_t addr;
} __attribute__ ((packed)); } __attribute__ ((packed));
void init_idt(void);
#endif /* !IDT_H */ #endif /* !IDT_H */

8
k/k.c

@ -23,15 +23,17 @@
*/ */
#include <k/kstd.h> #include <k/kstd.h>
#include "multiboot.h"
#include "stdio.h"
#include "serial.h"
#include "gdt.h" #include "gdt.h"
#include "idt.h"
#include "multiboot.h"
#include "serial.h"
#include "stdio.h"
static void k_init(void) static void k_init(void)
{ {
init_serial(); init_serial();
init_gdt(); init_gdt();
init_idt();
} }
void k_main(unsigned long magic, multiboot_info_t *info) void k_main(unsigned long magic, multiboot_info_t *info)