From 99019721a9e147c49becc466c5427609b937aca8 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Wed, 21 Apr 2021 18:54:50 +0200 Subject: [PATCH] Enable interrupts --- src/Makefile | 3 ++- src/boot/boot.S | 12 +++++------ src/boucane.cc | 13 ++---------- src/core/idt.cc | 51 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/idt.hpp | 33 ++++++++++++++++++++++++++++++ src/core/int.S | 33 ++++++++++++++++++++++++++++++ src/libs/stdio.cc | 24 ++++------------------ src/libs/stdio.hpp | 11 +--------- 8 files changed, 132 insertions(+), 48 deletions(-) create mode 100644 src/core/idt.cc create mode 100644 src/core/idt.hpp create mode 100644 src/core/int.S diff --git a/src/Makefile b/src/Makefile index 1c2b408..2fe67a6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,10 +8,11 @@ LD_SCRIPT := linker.ld BOOT_OBJ := $(addsuffix .cc,$(basename $(shell find ./boot -name '*.cc' -name '*.S' ! -name "boot.S"))) DRIVERS_OBJ := $(addsuffix .o,$(basename $(shell find ./drivers -name '*.cc' -o -name '*.S'))) LIBS_OBJ := $(addsuffix .cc,$(basename $(shell find ./libs -name '*.cc' -o -name '*.S'))) +CORE_OBJ := $(addsuffix .o,$(basename $(shell find ./core -name '*.cc' -o -name '*.S'))) all: $(EXEC) -$(EXEC): boot/boot.o $(BOOT_OBJ) $(DRIVERS_OBJ) $(LIBS_OBJ) boucane.o +$(EXEC): boot/boot.o $(BOOT_OBJ) $(DRIVERS_OBJ) $(LIBS_OBJ) $(CORE_OBJ) boucane.o $(CC) -n -T $(LD_SCRIPT) -nostdlib -o $@ $^ %.o: %.S diff --git a/src/boot/boot.S b/src/boot/boot.S index 8391e70..d90c4d8 100644 --- a/src/boot/boot.S +++ b/src/boot/boot.S @@ -112,20 +112,20 @@ lgdt (gdtr) jmp $0x08, $new_cs new_cs: +# Pay attention here! +# You are in long mode here +# Thus if you want to add any instructions +# You should use .code64 before +.code64 mov $0x10, %ax mov %ax, %ds mov %ax, %es mov %ax, %fs mov %ax, %gs mov %ax, %ss - -# Pay attention here! -# You are in long mode here -# Thus if you want to add any instructions -# You should use .code64 before +mov $STACK_LOCATION, %esp # Run boucane -.code64 jmp boucane # GDT diff --git a/src/boucane.cc b/src/boucane.cc index 94f266f..8baa1ba 100644 --- a/src/boucane.cc +++ b/src/boucane.cc @@ -1,19 +1,10 @@ #include "boucane.hpp" -#include "libs/string.hpp" +#include "core/idt.hpp" extern "C" void boucane(){ clear(); - char a[]="Loic Guegan"; printk("Booting Boucane v%d.%d.%d",VERSION_MAJOR,VERSION_MINOR, VERSION_PATH); - - char b[10]; - substr(8, 9, a, b); - print("\n"); - printk(b); - - int gg=0; - gg+=((1>2) ? 1 : 0)+9; - printi(gg); + idt_enable_interrupt(); while(1); } \ No newline at end of file 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!" diff --git a/src/libs/stdio.cc b/src/libs/stdio.cc index e73e4af..c7007c2 100644 --- a/src/libs/stdio.cc +++ b/src/libs/stdio.cc @@ -91,13 +91,6 @@ void print(char *s){ } } -void printc(char *str, VIDEO_COLORS c) { - VIDEO_COLORS backup = (VIDEO_COLORS)VS.fg; - VS.fg = c; - print(str); - VS.fg = backup; -} - void printi(int i) { char str[12]; itoa(i, str); @@ -107,17 +100,8 @@ void printi(int i) { void printh(int h) { char str[17]; itoh(h, str); - print(str); -} -void printh(int h, u32 size) { - char str[17]; - char str2[17]; - itoh(h, str); - u32 a = 0; - for (u32 i = min(max(16 - size, 0), 15); i < 16; i++) { - str2[a] = str[i]; - a++; - } - str2[a] = '\0'; - print(str2); + u8 i=0; + while(str[i]=='0') + i++; + print(&str[i]); } diff --git a/src/libs/stdio.hpp b/src/libs/stdio.hpp index fd266a5..804796b 100644 --- a/src/libs/stdio.hpp +++ b/src/libs/stdio.hpp @@ -8,18 +8,13 @@ extern void (*__putchar)(char); /** * Print a char* in the framebuffer */ -void printk(char *,...); +extern "C" void printk(char *,...); /** * Print a char* */ void print(char *s); -/** - * Print a char in the framebuffer - */ -void printc(char *, VIDEO_COLORS c); - /** * Print an integer using itoa() */ @@ -30,7 +25,3 @@ void printi(int i); */ void printh(int h); -/** - * Print an integer as hex using itoh() truncated to size - */ -void printh(int h, u32 size);