Added a working GDT.

This commit is contained in:
Morten Delenk 2015-10-10 19:34:31 +02:00
parent 3645036ee9
commit c7b0fffe13
6 changed files with 80 additions and 2 deletions

View file

@ -5,8 +5,8 @@ CPP = g++
CC = gcc CC = gcc
LD = ld LD = ld
ASFLAGS = -m32 ASFLAGS = -m32
CFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -fbuiltin CFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -fbuiltin -march=native
CPPFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -fbuiltin CPPFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -fbuiltin -march=native
LDFLAGS = -r -melf_i386 LDFLAGS = -r -melf_i386
hal.o: $(OBJS) hal.o: $(OBJS)

View file

@ -0,0 +1,14 @@
.global loadGDT
//void _stdcall loadGDT(struct gdtp* ptr);
loadGDT:
mov 0x4(%esp), %eax // Load argument
lgdt (%eax)
//GDT is loaded now
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
ljmp $0x8, $.1
.1:

View file

@ -5,10 +5,12 @@ namespace MTGosHAL {
class Output; class Output;
class Serial; class Serial;
class Screen; class Screen;
class GDT;
enum class BG_color: uint16_t; enum class BG_color: uint16_t;
enum class FG_color: uint16_t; enum class FG_color: uint16_t;
extern Serial* debug; extern Serial* debug;
extern Screen* out; extern Screen* out;
extern Screen* err; extern Screen* err;
extern GDT* gdt;
} }
#endif #endif

View file

@ -0,0 +1,31 @@
#ifndef _GDT_HPP
#define _GDT_HPP
#include <stdint.h>
#define GDT_FLAG_DATASEG 0x02
#define GDT_FLAG_CODESEG 0x0a
#define GDT_FLAG_TSS 0x09
#define GDT_FLAG_SEGMENT 0x10
#define GDT_FLAG_RING0 0x00
#define GDT_FLAG_RING3 0x60
#define GDT_FLAG_PRESENT 0x80
#define GDT_FLAG_4K_GRAN 0x800
#define GDT_FLAG_32_BIT 0x400
extern "C" void loadGDT(void * gdtpr);
namespace MTGosHAL {
class GDT {
private:
uint64_t gdt[7];
struct gdtp {
uint16_t limit;
void* pointer;
} __attribute__((packed));
struct gdtp fin;
public:
GDT();
auto setEntry(int i, unsigned int base, unsigned int limit, int flags) -> void;
auto apply() -> void;
};
}
#endif

View file

@ -0,0 +1,21 @@
#include <base.hpp>
#include <gdt.hpp>
#include <serial.hpp>
namespace MTGosHAL {
GDT::GDT() {
gdt[0]=gdt[1]=gdt[2]=gdt[3]=gdt[4]=gdt[5]=gdt[6]=0x0ull;
}
auto GDT::setEntry(int i, unsigned int base, unsigned int limit, int flags) -> void {
gdt[i] = limit & 0xffffLL;
gdt[i] |= (base & 0xffffffLL) << 16;
gdt[i] |= (flags & 0xffLL) << 40;
gdt[i] |= ((limit >> 16) & 0xfLL) << 48;
gdt[i] |= ((flags >> 8 )& 0xffLL) << 52;
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";
fin={(uint16_t)7*8-1, (void*)gdt};
loadGDT((void*)(&fin));
};
}

View file

@ -2,6 +2,7 @@
#include <output.hpp> #include <output.hpp>
#include <serial.hpp> #include <serial.hpp>
#include <textDISP.hpp> #include <textDISP.hpp>
#include <gdt.hpp>
namespace MTGosHAL { namespace MTGosHAL {
Serial* debug; Serial* debug;
Screen* out; Screen* out;
@ -9,11 +10,20 @@ namespace MTGosHAL {
void main() { void main() {
Serial serialOUT(115200); Serial serialOUT(115200);
Screen display; Screen display;
GDT gdt;
debug=&serialOUT; debug=&serialOUT;
err=&display; err=&display;
out=&display; out=&display;
*out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; *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"; *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);
gdt.setEntry(3, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT | GDT_FLAG_RING3);
gdt.setEntry(4, 0, 0xfffff, GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT | GDT_FLAG_RING3);
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();
for(;;); for(;;);
} }
} }