From cff89659403b04597f7bb4a472b632f374692a4d Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Thu, 9 Jul 2020 21:58:52 -0500 Subject: [PATCH] Added interrupt handling --- Makefile | 7 ++++--- src/entry.S | 29 +++++++++++++++++++++++++++-- src/interruptdescriptor.cpp | 8 ++++---- src/interruptdescriptor.hpp | 10 +++++----- src/inthandlers.cpp | 27 ++++++++++++++++++++++----- src/inthandlers.hpp | 8 ++++++-- src/quarkkernel.cpp | 33 +++++++++++++++++++++++++-------- 7 files changed, 93 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 2dd8120..ce2cb91 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ -objs = src/addressspace.o src/tty.o src/buddyallocator.o src/math.o \ +objs = src/addressspace.o src/tty.o src/buddyallocator.o src/math.o \ src/cstring.o src/pagetableentry.o src/multiboot2header.o \ - src/systeminfo.o src/memorymap.o src/entry.o src/quarkkernel.o + src/systeminfo.o src/memorymap.o src/interruptdescriptor.o \ + src/inthandlers.o src/pio.o src/entry.o src/quarkkernel.o link_script = src/linker.ld quark_bin = qkernel quark_img = quark.iso @@ -8,7 +9,7 @@ quark_img = quark.iso CXX = i686-elf-g++ CC = i686-elf-gcc -CPPFLAGS = -ffreestanding -O2 -Wall -fno-exceptions -fno-rtti -ggdb +CPPFLAGS = -ffreestanding -mgeneral-regs-only -O0 -Wall -fno-exceptions -fno-rtti -ggdb .PHONY: all all: $(quark_img) diff --git a/src/entry.S b/src/entry.S index 92768e1..0c9b030 100755 --- a/src/entry.S +++ b/src/entry.S @@ -2,7 +2,23 @@ .include "src/multiboot2header.S" .section .rodata - + +gdt: +.long 0, 0 +.short 0xFFFF +.short 0x0000 +.short 0x9A00 +.short 0x00CF +.short 0xFFFF +.short 0x0000 +.short 0x9200 +.short 0x00CF + +gdt_info: +.short 23 +.long gdt + +.align 16 idt_info: .short idt_end - idt - 1 .long idt @@ -199,9 +215,18 @@ s_end: # Initialize stack mov $stackTop, %esp - + lgdt gdt_info lidt idt_info + jmp $8, $.ldcs +.ldcs: + mov $16, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %gs + mov %ax, %fs + mov %ax, %ss + mov $_bootCmdLine, %eax push %eax diff --git a/src/interruptdescriptor.cpp b/src/interruptdescriptor.cpp index 50d9536..866bd6f 100644 --- a/src/interruptdescriptor.cpp +++ b/src/interruptdescriptor.cpp @@ -16,13 +16,13 @@ kernel::InterruptDescriptor::InterruptDescriptor(void* handler, Type type, unsig { uint32_t offset = (uint32_t) handler; this->m_offset1 = (uint16_t) offset; - this->m_selector = 1; + this->m_selector = 8; this->m_zero = 0; this->m_type = (uint16_t) type; this->m_storage = 0; this->m_dpl = dpl; - this->m_present = 0; - this->m_offset2 = (uint16_t) (offset >> 16); + this->m_present = 1; + this->m_offset2 = offset >> 16; } bool kernel::InterruptDescriptor::present() @@ -58,5 +58,5 @@ void* kernel::InterruptDescriptor::operator=(void* rhs) { uint32_t offset = (uint32_t) rhs; m_offset1 = (uint16_t) offset; - m_offset2 = (uint16_t) (offset >> 16); + m_offset2 = (offset >> 16); } diff --git a/src/interruptdescriptor.hpp b/src/interruptdescriptor.hpp index 65fd261..c4365fe 100644 --- a/src/interruptdescriptor.hpp +++ b/src/interruptdescriptor.hpp @@ -12,11 +12,11 @@ public: enum Type { - TASK32 = 5, - TRAP32 = 15, - INT32 = 14, - TRAP16 = 7, - INT16 = 6 + TASK32 = 5, + TRAP32 = 15, + INT32 = 14, + TRAP16 = 7, + INT16 = 6 }; InterruptDescriptor(); diff --git a/src/inthandlers.cpp b/src/inthandlers.cpp index 64eb7d7..bf780e8 100644 --- a/src/inthandlers.cpp +++ b/src/inthandlers.cpp @@ -1,16 +1,33 @@ #include "inthandlers.hpp" -extern "C" +char* display = (char*) 0xC00B8000; + +__attribute__ ((interrupt)) +void divisionByZero(void* frame) +{ + *display = 'z'; + display += 2; +} + + __attribute__ ((interrupt)) void gpFaultHandler(void* frame, unsigned int error) { - + *display = 'a'; + asm("hlt"); } -extern "C" __attribute__ ((interrupt)) -void pageFaultHandler(unsigned int error) +void pageFaultHandler(void* frame, unsigned int error) { - + *display = '0'; + //asm("hlt"); +} + +__attribute__ ((interrupt)) +void doubleFaultHandler(void* frame, unsigned int error) +{ + *display = '#'; + //asm("hlt"); } diff --git a/src/inthandlers.hpp b/src/inthandlers.hpp index 405ad56..10f085b 100644 --- a/src/inthandlers.hpp +++ b/src/inthandlers.hpp @@ -1,12 +1,16 @@ #ifndef INTHANDLERS_H #define INTHANDLERS_H -extern "C" +__attribute__ ((interrupt)) +void divisionByZero(void* frame); + __attribute__ ((interrupt)) void gpFaultHandler(void* frame, unsigned int error); -extern "C" __attribute__ ((interrupt)) void pageFaultHandler(void* frame, unsigned int error); +__attribute__ ((interrupt)) +void doubleFaultHandler(void* frame, unsigned int error); + #endif diff --git a/src/quarkkernel.cpp b/src/quarkkernel.cpp index ee6b6ce..544c33a 100755 --- a/src/quarkkernel.cpp +++ b/src/quarkkernel.cpp @@ -6,30 +6,47 @@ #include "memorymap.hpp" #include "buddyallocator.hpp" #include "addressspace.hpp" +#include "interruptdescriptor.hpp" +#include "inthandlers.hpp" +#include "pio.hpp" #include "tty.h" using namespace kernel; extern SystemInfo system_info; extern MemoryMap::Region memory_map; +extern InterruptDescriptor idt; + +extern "C" void __cxa_pure_virtual() +{ + +} void main(char* cmdline) { - TTY tty((char*) 0xC00B8000); + TTY tty((char*) 0xC00B8000); tty << "--Quark Kernel--\n"; tty << "Low memory: \t" << (int) system_info.getLowMemory() << " KiB\n"; tty << "High memory:\t" << (int) system_info.getHighMemory() << " KiB\n"; tty << "Type\t\tLocation\t\tSize\n"; MemoryMap memmap(&memory_map, 16); - for(size_t i = 0; i < memmap.size() && memmap[i].getSize() > 0; i++) + for(size_t i = 0; i < memmap.size() && memmap[i].getSize() > 0; i++) { - tty << (int) memmap[i].getType() << "\t\t\t" - << (void*) memmap[i].getLocation() << "\t\t" - << (int) memmap[i].getSize() << "\n"; + tty << (int) memmap[i].getType() << "\t\t\t"; + tty << (void*) memmap[i].getLocation() << "\t\t"; + tty << (int) memmap[i].getSize() << "\n"; } - BuddyAllocator alloc(memmap, (char*) 0xC0000000, - system_info.getHighMemory() / 4 + 256, 6); + BuddyAllocator alloc(memmap, (char*) 0xC0000000, system_info.getHighMemory() / 4 + 256, 6); AddressSpace vmem(alloc); - tty << vmem.mmap((void*) 0x80000000, 0x10000) << '\n'; + vmem.mmap((void*) 0x80000000, 0x10000); + InterruptDescriptor* interruptTable = &idt; + interruptTable[0] = InterruptDescriptor((void*) &divisionByZero, InterruptDescriptor::INT32, 0); + interruptTable[8] = InterruptDescriptor((void*) &doubleFaultHandler, InterruptDescriptor::INT32, 0); + interruptTable[14] = InterruptDescriptor((void*) &pageFaultHandler, InterruptDescriptor::INT32, 0); + //for(int i = 1; i < 32; i++) + // interruptTable[i] = InterruptDescriptor((void*) &pageFaultHandler, InterruptDescriptor::INT32, 0); + outb(0xa1, 0xff); + outb(0x21, 0xff); + asm("sti"); tty << "Nothing left to do. Hanging.\n"; }