From 0e2daf575e3aa6e6745e0c2a38b9c1ce53153600 Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Wed, 14 Oct 2015 20:02:41 +0200 Subject: [PATCH] Tried to fix it. Seemingly we need a i686-elf gcc now --- kernel.settings | 3 ++ kernel/Makefile | 5 ++-- kernel/hal/x86/Makefile | 8 ++--- kernel/hal/x86/include/base.hpp | 10 +++---- kernel/hal/x86/include/serial.hpp | 2 +- kernel/hal/x86/include/textDISP.hpp | 1 - kernel/hal/x86/init/gdt.cpp | 2 +- kernel/hal/x86/init/idt.cpp | 12 ++++---- kernel/hal/x86/init/init.cpp | 45 +++++++++++++---------------- kernel/hal/x86/io/keyboard.cpp | 8 ++--- kernel/hal/x86/io/serial.cpp | 8 +++-- kernel/hal/x86/io/textDISP.cpp | 2 ++ kernel/kernel.ld | 7 +++++ 13 files changed, 61 insertions(+), 52 deletions(-) diff --git a/kernel.settings b/kernel.settings index f63e43d..631873b 100644 --- a/kernel.settings +++ b/kernel.settings @@ -1,6 +1,9 @@ MODE = debug #MODE = release # enables optimization +#PREFIX = +PREFIX = i686-elf- + ifeq ($(MODE),debug) CFLAGS += -g3 -DDEBUG CPPFLAGS += -g3 -DDEBUG diff --git a/kernel/Makefile b/kernel/Makefile index 1a452b9..ba607e6 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,7 +1,8 @@ +include ../kernel.settings SRCS = $(shell find -name '*.cpp' -o -name '*.[cS]') OBJS = $(addsuffix .o,$(basename $(SRCS))) -LD = g++ -LDFLAGS = -T kernel.ld -nostdlib -nodefaultlibs +LD = $(PREFIX)g++ +LDFLAGS = -T kernel.ld -nostdlib -nodefaultlibs -m32 -nostdlib -fno-builtin all: hal $(LD) $(LDFLAGS) -o mtgos $(OBJS) hal: diff --git a/kernel/hal/x86/Makefile b/kernel/hal/x86/Makefile index be4f360..dce64bd 100644 --- a/kernel/hal/x86/Makefile +++ b/kernel/hal/x86/Makefile @@ -1,14 +1,14 @@ +include ../../../kernel.settings SRCS = $(shell find -name '*.cpp' -o -name '*.[cS]') OBJS = $(addsuffix .o,$(basename $(SRCS))) -CPP = g++ -CC = gcc +CPP = $(PREFIX)g++ +CC = $(PREFIX)gcc ASFLAGS = -m32 CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib +CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused LDFLAGS = -r -melf_i386 -include ../../../kernel.settings all: $(OBJS) diff --git a/kernel/hal/x86/include/base.hpp b/kernel/hal/x86/include/base.hpp index 31f6fd9..b26746c 100644 --- a/kernel/hal/x86/include/base.hpp +++ b/kernel/hal/x86/include/base.hpp @@ -11,10 +11,10 @@ namespace MTGosHAL { class IDT; enum class BG_color: uint16_t; enum class FG_color: uint16_t; - extern Serial* debug; - extern Screen* out; - extern Screen* err; - extern GDT* gdt; - extern IDT* idt; + extern Serial debug; + extern Screen out; + extern Screen err; + extern GDT gdt; + extern IDT idt; } #endif diff --git a/kernel/hal/x86/include/serial.hpp b/kernel/hal/x86/include/serial.hpp index de992ae..fb8c28b 100644 --- a/kernel/hal/x86/include/serial.hpp +++ b/kernel/hal/x86/include/serial.hpp @@ -22,7 +22,7 @@ namespace MTGosHAL { auto serial_received() -> int; auto getChar() -> char; public: - Serial(uint32_t baud); + Serial(); auto debug() -> void; }; } diff --git a/kernel/hal/x86/include/textDISP.hpp b/kernel/hal/x86/include/textDISP.hpp index 2d62396..b91d12d 100644 --- a/kernel/hal/x86/include/textDISP.hpp +++ b/kernel/hal/x86/include/textDISP.hpp @@ -45,7 +45,6 @@ namespace MTGosHAL { private: FG_color fg; BG_color bg; - int x,y; uint16_t* vmem=(uint16_t*)0xB8000; auto putChar(char c) -> void; public: diff --git a/kernel/hal/x86/init/gdt.cpp b/kernel/hal/x86/init/gdt.cpp index d847a8f..7c3288d 100644 --- a/kernel/hal/x86/init/gdt.cpp +++ b/kernel/hal/x86/init/gdt.cpp @@ -14,7 +14,7 @@ namespace MTGosHAL { gdt[i] |= ((base >> 24) & 0xffLL) << 56; } auto GDT::apply() -> void { - *debug << "We are now trying to set our GDT. If the CPU triplefaults, something went wrong in MTGosHAL::GDT::apply() or loadGDT().\n"; + debug << "We are now trying to set our GDT. If the CPU triplefaults, something went wrong in MTGosHAL::GDT::apply() or loadGDT().\n"; fin={(uint16_t)7*8-1, (void*)gdt}; loadGDT((void*)(&fin)); }; diff --git a/kernel/hal/x86/init/idt.cpp b/kernel/hal/x86/init/idt.cpp index b206d09..a76ef03 100644 --- a/kernel/hal/x86/init/idt.cpp +++ b/kernel/hal/x86/init/idt.cpp @@ -25,15 +25,15 @@ namespace MTGosHAL { idt[i]|=((uint64_t)flags)<<40; } auto IDT::apply() -> void { - *debug << "Now trying to load the IDT.\n"; + debug << "Now trying to load the IDT.\n"; idtptr.limit=(uint16_t)(256*8-1); idtptr.pointer=(uint64_t*)&idt; loadIDT((void*)&idtptr); } auto IDT::handle(struct cpu_state* cpu) -> void { - *debug << "Interrupt 0x" << Base::HEXADECIMAL << (int) cpu->intr << " was raised.\n"; + debug << "Interrupt 0x" << Base::HEXADECIMAL << (int) cpu->intr << " was raised.\n"; if(cpu->intr<=0x1F) { - *out << "Exception 0x" << Base::HEXADECIMAL << (int) cpu->intr << "! Kernel halted!\n"; + out << "Exception 0x" << Base::HEXADECIMAL << (int) cpu->intr << "! Kernel halted!\n"; while(1) { asm volatile("cli; hlt"); } @@ -42,9 +42,9 @@ namespace MTGosHAL { outb(0xA0, 0x20); } outb(0x20, 0x20); - *debug << "The IRQ " << Base::DECIMAL << (int) cpu->intr-0x20 << " was handled.\n"; + debug << "The IRQ " << Base::DECIMAL << (int) cpu->intr-0x20 << " was handled.\n"; if(cpu->intr==0x20) { - debug->debug(); + debug.debug(); } } for(int i=0;i<16;i++) { @@ -63,5 +63,5 @@ namespace MTGosHAL { } } extern "C" void handleINT(struct cpu_state* cpu) { - MTGosHAL::idt->handle(cpu); + MTGosHAL::idt.handle(cpu); } diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index 7fa525e..9c54d50 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -7,22 +7,16 @@ #include extern "C" void intr_stub_0(void); namespace MTGosHAL { - Serial* debug; - Screen* out; - Screen* err; - Keyboard* in; - IDT* idt; + Serial debug; + Screen out; + Screen err; + Keyboard in; + IDT idt; + GDT gdt; void main() { - Serial serialOUT(115200); - Screen display; - GDT gdt; - IDT interrupts; - debug=&serialOUT; - err=&display; - out=&display; - idt=&interrupts; - *out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; - *debug << "Hello debugger! This is MTGos version 0.0\nThese logs are probably very long, so please redirect the output to a file.\n"; + out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; + err << BG_color::BLACK << FG_color::RED; + debug << "Hello debugger! This is MTGos version 0.0\nThese logs are probably very long, so please redirect the output to a file.\n"; gdt.setEntry(0, 0, 0, 0); gdt.setEntry(1, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); gdt.setEntry(2, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); @@ -31,25 +25,26 @@ namespace MTGosHAL { gdt.setEntry(5, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_TSS | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); gdt.setEntry(6, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_TSS | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT); gdt.apply(); - *debug << "We are now creating the IDT.\n"; + debug << "We are now creating the IDT.\n"; for(int i=0;i<256;i++) { - idt->setEntry(i, (void *)((uint32_t)&intr_stub_0+i*16), SEG_KERNEL, IDT_INTERRUPT_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); + idt.setEntry(i, (void *)((uint32_t)&intr_stub_0+i*16), SEG_KERNEL, IDT_INTERRUPT_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); } - idt->setEntry(48, (void *)((uint32_t)&intr_stub_0+768), SEG_KERNEL, IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); - idt->setEntry(8, (void *)((uint32_t)&intr_stub_0+128), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); - idt->apply(); - -//Init the Keyboard - Keyboard kb; - in = &kb; + idt.setEntry(48, (void *)((uint32_t)&intr_stub_0+768), SEG_KERNEL, IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); + idt.setEntry(8, (void *)((uint32_t)&intr_stub_0+128), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); + idt.apply(); sti(); for(;;); } } +typedef void (*constructor)(); +extern "C" constructor start_ctors; +extern "C" constructor end_ctors; extern "C" void init() { + for(constructor* i = &start_ctors; i != &end_ctors; ++i) + (*i)(); MTGosHAL::main(); } extern "C" void __cxa_pure_virtual() { - *MTGosHAL::debug << "A pure virtual function just got called.\n"; + MTGosHAL::debug << "A pure virtual function just got called.\n"; } diff --git a/kernel/hal/x86/io/keyboard.cpp b/kernel/hal/x86/io/keyboard.cpp index 5180082..d3623b8 100644 --- a/kernel/hal/x86/io/keyboard.cpp +++ b/kernel/hal/x86/io/keyboard.cpp @@ -18,11 +18,11 @@ namespace MTGosHAL { outb(0x60, command); } auto Keyboard::handleIRQ1(struct cpu_state* cpu) -> void { - *debug << "Keyboard interrupt received.\nGot: 0x" << Base::HEXADECIMAL << (int) inb(0x60) << ".\n"; + debug << "Keyboard interrupt received.\nGot: 0x" << Base::HEXADECIMAL << (int) inb(0x60) << ".\n"; } Keyboard::Keyboard(): shift(false), numlock(true), caps(false), scrollock(false) { - if(!idt->request(0x21, (void(*)(struct cpu_state*))&MTGosHAL::Keyboard::handleIRQ1)) { - *debug << "Could not get an handler for IRQ1 (Keyboard)\n"; + if(!idt.request(0x21, (void(*)(struct cpu_state*))&MTGosHAL::Keyboard::handleIRQ1)) { + debug << "Could not get an handler for IRQ1 (Keyboard)\n"; return; } //Clear keyboard buffer @@ -34,7 +34,7 @@ namespace MTGosHAL { sendCommand(0x20); uint8_t ccb=inb(0x60); if(!(ccb&4)) { - *debug << "Keyboard didn't pass self-test!\nDeactivating IRQ1.\n"; + debug << "Keyboard didn't pass self-test!\nDeactivating IRQ1.\n"; ccb &= ~1; } else { ccb |= 1; diff --git a/kernel/hal/x86/io/serial.cpp b/kernel/hal/x86/io/serial.cpp index d94876c..bf17c23 100644 --- a/kernel/hal/x86/io/serial.cpp +++ b/kernel/hal/x86/io/serial.cpp @@ -31,7 +31,8 @@ namespace MTGosHAL { *this << '\n'; return chr; } - Serial::Serial(uint32_t baud) { + Serial::Serial() { + uint32_t baud=115200; port=*((uint16_t*)0x0400); union { uint8_t b[2]; @@ -54,9 +55,10 @@ namespace MTGosHAL { auto Serial::debug() -> void { if(getChar()) { *this << "The interactive debug shell was started.\n> "; - *out << "The debug shell was started.\n"; + out << "The debug shell was started.\n"; char buf[256]; - *out << buf << "\nDebug shell closed.\n"; + *this >> buf; + out << buf << "\nDebug shell closed.\n"; *this << "Unknown command. TODO\n"; } uint64_t wt = waittimes; diff --git a/kernel/hal/x86/io/textDISP.cpp b/kernel/hal/x86/io/textDISP.cpp index b52100e..4970f83 100644 --- a/kernel/hal/x86/io/textDISP.cpp +++ b/kernel/hal/x86/io/textDISP.cpp @@ -1,5 +1,6 @@ #include #include +int x=0, y=0; namespace MTGosHAL { auto Screen::putChar(char c) -> void { switch(c) { @@ -27,6 +28,7 @@ namespace MTGosHAL { auto Screen::clrscr() -> void { for(int p=0; p void { for(int p=0; p