summaryrefslogtreecommitdiff
path: root/kernel/GDT/gdt.c
blob: 4aae655f5dd6125c8ac5e9370e04e3112140cc58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
    );
}