From 99019721a9e147c49becc466c5427609b937aca8 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Wed, 21 Apr 2021 18:54:50 +0200 Subject: Enable interrupts --- src/core/idt.cc | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/idt.hpp | 33 +++++++++++++++++++++++++++++++++ src/core/int.S | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/core/idt.cc create mode 100644 src/core/idt.hpp create mode 100644 src/core/int.S (limited to 'src/core') diff --git a/src/core/idt.cc b/src/core/idt.cc new file mode 100644 index 0000000..b808625 --- /dev/null +++ b/src/core/idt.cc @@ -0,0 +1,51 @@ +#include "idt.hpp" +#include "libs/string.hpp" + +IDT_REGISTER IDTR = { + IDT_GATE_SIZE*IDT_MAX_ENTRIES, + IDT_ADDR +}; + +extern u64 INT_DEFAULT,INT_0,INT_14; + +void idt_enable_interrupt(void){ + IDT_DESCRIPTOR d; + d.ign=0; + d.ist=0; + d.selector=0x08; + d.options=IDT_OPT_P|IDT_OPT_PRVL_0|IDT_OPT_TYPE_INT; + + // Write idt entries + for(u16 i=0;i>32; + u32 desc96_127=desc.ign; + u32* dst=(u32*)(IDTR.base+index*IDT_GATE_SIZE); + *dst=desc0_31; + *(dst+1)=desc32_63; + *(dst+2)=desc64_95; + *(dst+3)=desc96_127; +} \ No newline at end of file diff --git a/src/core/idt.hpp b/src/core/idt.hpp new file mode 100644 index 0000000..f2aca43 --- /dev/null +++ b/src/core/idt.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "types.hpp" +#include "libs/stdio.hpp" + +#define IDT_GATE_SIZE 16 +#define IDT_MAX_ENTRIES 50 +#define IDT_ADDR 0x200000 + +#define IDT_OPT_P (1 << 15) +#define IDT_OPT_TYPE_INT 0xE << 8 +#define IDT_OPT_PRVL_0 0 +#define IDT_OPT_PRVL_1 (1 << 13) +#define IDT_OPT_PRVL_2 (2 << 13) +#define IDT_OPT_PRVL_3 (3 << 13) + + + +typedef struct IDT_REGISTER { + u16 limit; + u64 base; +} __attribute__((packed)) IDT_REGISTER; + +typedef struct IDT_DESCRIPTOR { + u64 offset; + u16 selector; + u16 options; + u8 ist; + u32 ign; +} __attribute__((packed)) IDT_DESCRIPTOR; + +void idt_enable_interrupt(void); +void idt_write_descriptor(IDT_DESCRIPTOR desc, u16 index); \ No newline at end of file diff --git a/src/core/int.S b/src/core/int.S new file mode 100644 index 0000000..d5ad643 --- /dev/null +++ b/src/core/int.S @@ -0,0 +1,33 @@ +.extern printk + +.macro call_printk msg + mov \msg, %rdi + mov $0, %eax # Required for variadic functions + call printk +.endm + +.globl INT_DEFAULT +INT_DEFAULT: + iretq + +.globl INT_0 +INT_0: + call_printk $MSG_INT_0 + INT_0_INFINITE: + jmp INT_0_INFINITE + iretq + +.globl INT_14 +INT_14: + call_printk $MSG_INT_14 + mov $0, %eax + call printk + INT_14_INFINITE: + jmp INT_14_INFINITE + iretq + + +MSG_INT_0: +.asciz "Zero Division error!" +MSG_INT_14: +.asciz "Page fault!" -- cgit v1.2.3