so i went on and converted a few functions into C++ and tested it and it seemed fine, so i carried on. now i have reached the GDT and have noticed that it doesnt always have the desired result. im not sure if this is just me doing some little thing wrong that i cant see, or if it is the switches i compile it with, but it seems that roughly 1in 7 times i run it in bochs, it runs fine, the other 6 come up with a general protection fault. whereas if i run it in M$ Virtual PC then it runs fine roughly 1 in 3.
i compile every file with this (obviously changing the filename):
Code: Select all
gxx -o kernel.o -c C:/CPPOSDEV/source/kernel.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions
also this is my GDT code:
GDT.h
Code: Select all
#ifndef __GDT_H_
#define __GDT_H_
#include "common.h"
extern "C"{
typedef struct GDT_p{
Word limit;
dWord base;
} __attribute__((packed)) GDT_ptr;
typedef struct GDT_entry{
Word limit_low;
Word base_low;
Byte base_middle;
Byte access;
Byte granularity;
Byte base_high;
} __attribute__((packed)) GDT_t;
extern void GDT_Flush();
}
class GDT{
public:
GDT();
~GDT();
void gdtInit();
void addGdtEntry(int num, unsigned long base, unsigned long limit, Byte access, Byte granularity);
private:
GDT_t gdt[3];
GDT_ptr gdt_ptr;
};
#endif
Code: Select all
#include "includes/GDT.h"
#include "includes/common.h"
extern "C" {GDT_ptr gp;}
GDT::GDT(){}
GDT::~GDT(){}
void GDT::gdtInit(){
gdt_ptr.limit = (Word)(sizeof(GDT_t) *3)-1;
gdt_ptr.base = (dWord) &gdt;
addGdtEntry(0, 0, 0, 0, 0);
addGdtEntry(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
addGdtEntry(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
gp = gdt_ptr;
GDT_Flush();
}
void GDT::addGdtEntry(int num,
unsigned long base,
unsigned long limit,
Byte access,
Byte granularity)
{
gdt[num].base_low = (base & 0xFFFF);
gdt[num].base_middle = (base >> 16) & 0xFF;
gdt[num].base_high = (base >> 24) & 0xFF;
gdt[num].limit_low = (limit & 0xFFFF);
gdt[num].granularity = ((limit >> 16) & 0x0F);
gdt[num].granularity |= (granularity & 0xF0);
gdt[num].access = access;
}
GDT.asm
Code: Select all
[bits 32]
[global _GDT_Flush]
[extern _gp]
_GDT_Flush:
lgdt [_gp]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:flush
flush:
ret
thanks in advance.
James.