Go back to C and adapt code

This commit is contained in:
manzerbredes 2015-07-28 14:19:00 +04:00
parent aac010a9e3
commit e0c565f7ff
18 changed files with 242 additions and 308 deletions

View file

@ -4,8 +4,8 @@ EXEC=gdt.o
all:$(EXEC)
#----- GDT -----
$(EXEC): gdt.cpp
$(CXX) -c -o $(EXEC) gdt.cpp
$(EXEC): gdt.c
$(CC) $(CFLAGS) -c -o $(EXEC) $^
#---------------

50
kernel/GDT/gdt.c Normal file
View file

@ -0,0 +1,50 @@
#include "gdt.h"
#include "../Helpers/types.h"
#include "../Helpers/memory.h"
//Declare global vars
GdtDescriptorStruct GDT_DESCRIPTORS[4];
GdtPointerStruct GDT_POINTER;
//Adapt parameter to the gdt descriptor structure
void gdt_initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, GdtDescriptorStruct *Descriptor){
Descriptor->limit1 = limit & 0xFFFF;
Descriptor->base1 = base & 0xFFFF;
Descriptor->base2 = (base & 0xFF0000) >> 16;
Descriptor->access = access;
Descriptor->limit2 = (limit & 0xF0000 ) >> 16;
Descriptor->flags = flags & 0xFF;
Descriptor->base3 = (base & 0xFF000000) >> 24;
}
//Copy the gdt into mémory and load it
void gdt_loadGdt(){
//Init default segment
gdt_initGdtDesc(0x0,0x0,0x0,0x0, &GDT_DESCRIPTORS[0]);
//Init code segment
gdt_initGdtDesc(0x0,0xFFFFF,0x9A,0x0D, &GDT_DESCRIPTORS[1]);
//Init data segment
gdt_initGdtDesc(0x0,0xFFFFF,0x92,0x0D, &GDT_DESCRIPTORS[2]);
//Init stack segment
gdt_initGdtDesc(0x00B00000,0x00000500,0x96,0x0D, &GDT_DESCRIPTORS[3]);
//Init GDT Pointer
GDT_POINTER.size=4*sizeof(GDT_DESCRIPTORS);
GDT_POINTER.segment=0x00007E00;
//Copy Gdt into memory and init registers
memcpy((u32)GDT_DESCRIPTORS, (u32)GDT_POINTER.segment, (u32)GDT_POINTER.size);
//Load the GDT
__asm__("lgdtl (%0);"
:
:"r"(&GDT_POINTER)
);
}

View file

@ -1,58 +0,0 @@
#include "gdt.hpp"
#include "../Helpers/types.hpp"
#include "../Helpers/memory.hpp"
//Constructor
Gdt::Gdt(){
//Init conventional segment
this->initGdtDesc(0x0,0x0,0x0,0x0, &m_Descriptors[0]);
//Init code segment
this->initGdtDesc(0x0,0xFFFFF,0x9A,0x0D, &m_Descriptors[1]);
//Init data segment
this->initGdtDesc(0x0,0xFFFFF,0x92,0x0D, &m_Descriptors[2]);
//Init stack segment
this->initGdtDesc(0x00B00000,0x00000500,0x96,0x0D, &m_Descriptors[3]);
//Init GDT Pointer
this->m_Pointer.size=4*sizeof(this->m_Descriptors);
this->m_Pointer.segment=0x00007E00;
}
//Destructor
Gdt::~Gdt(){
}
//Adapt parameter to the gdt descriptor structure
void Gdt::initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, gdtDescriptorStruct *Descriptor){
Descriptor->limit1 = limit & 0xFFFF;
Descriptor->base1 = base & 0xFFFF;
Descriptor->base2 = (base & 0xFF0000) >> 16;
Descriptor->access = access;
Descriptor->limit2 = (limit & 0xF0000 ) >> 16;
Descriptor->flags = flags & 0xFF;
Descriptor->base3 = (base & 0xFF000000) >> 24;
}
//Copy the gdt into mémory and load it
void Gdt::loadGdt(){
//Copy Gdt into memory and init registers
memcpy((u32)m_Descriptors, (u32)m_Pointer.segment, (u32)m_Pointer.size);
//Put m_Pointer in a var to pass it to assembly code
int *gdtAdress=(int *)&m_Pointer;
__asm__("lgdtl (%0);"
:
:"r"(gdtAdress)
);
}

37
kernel/GDT/gdt.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef __GDT__
#define __GDT__
#include "../Helpers/types.h"
//Define GDT pointer
struct GdtPointerStruct{
u16 size;
u32 segment;
} __attribute__ ((packed));
//Define GDT descriptor
struct GdtDescriptorStruct{
u16 limit1;
u16 base1;
u8 base2;
u8 access;
u8 limit2 : 4;
u8 flags : 4;
u8 base3;
} __attribute__ ((packed));
//Typedef :
typedef struct GdtPointerStruct GdtPointerStruct;
typedef struct GdtDescriptorStruct GdtDescriptorStruct;
//Functions :
void gdt_initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, GdtDescriptorStruct *Descriptor);
void gdt_loadGdt();
#endif

View file

@ -1,55 +0,0 @@
#ifndef __GDT__
#define __GDT__
#include "../Helpers/types.hpp"
//Define GDT pointer
struct gdtPointerStruct{
u16 size;
u32 segment;
} __attribute__ ((packed));
//Define GDT descriptor
struct gdtDescriptorStruct{
u16 limit1;
u16 base1;
u8 base2;
u8 access;
u8 limit2 : 4;
u8 flags : 4;
u8 base3;
} __attribute__ ((packed));
//Typedef :
typedef struct gdtPointerStruct gdtPointerStruct;
typedef struct gdtDescriptorStruct gdtDescriptorStruct;
//Gdt class
class Gdt{
private:
//Data members
gdtDescriptorStruct m_Descriptors[4];
gdtPointerStruct m_Pointer;
//Methods
void initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, gdtDescriptorStruct *Descriptor);
public:
//Constructor
Gdt();
//Destructor
~Gdt();
//Methods
void loadGdt();
};
#endif