summaryrefslogtreecommitdiffstats
path: root/arch/x86/gdt/Gdt.cpp
blob: 70f30cd37d152171b54165f867aa5aa78eba9e6f (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Gdt.h"


uint64 MngGdt::gdt[MngGdt::size];
bool MngGdt::isInitialize = false;
bool MngGdt::gdtLoaded = false;
MngGdt mngGdt;

MngGdt::MngGdt()
{
    if(!isInitialize)
    {
        MemoryOperations::set(gdt, 0, sizeof(uint64) * size);
        currIdx = 1;
        gdtLoaded = false;
        isInitialize = true;
        codeSegmentKern = NULL;
    }
};

DescSeg32::DescSeg32(uint32 base, uint32 limit, uint8 access, uint8 flags)
{
    this->baseLow = base & 0x0000FFFF;
    this->baseMiddle = (base & 0x00FF0000) >> 16;
    this->baseHight = (base & 0xFF000000) >> 24;

    this->limitLow = limit & 0x0000FFFF;
    this->limitMiddle = (limit & 0x000F0000) >> 16;

    this->access = access;
    this->flags = flags & 0x000E;
};

uint64 DescSeg32::getDescriptor()
{
    uint64 descriptor = 0;

    descriptor |= (uint64)limitLow;
    descriptor |= (uint64)baseLow << 16;
    descriptor |= (uint64)baseMiddle << 32;
    descriptor |= (uint64)access << 40;
    descriptor |= (uint64)limitMiddle << 48;
    descriptor |= (uint64)flags << 52;
    descriptor |= (uint64)baseHight << 56;

    return descriptor;
};

uint64 DescSeg32::flatCodeKernel()
{
    return DescSeg32(0, 0xFFFFF, CODE_ER | PRESENT | NSYSTEMSEG
                                    , PAGEGRAN | HIGHSIZE).getDescriptor();    
};

uint64 DescSeg32::flatDataKernel()
{
    return DescSeg32(0, 0xFFFFF, DATA_RW | PRESENT | NSYSTEMSEG
                                    , PAGEGRAN | HIGHSIZE).getDescriptor();    
}

uint64 DescSeg32::flatCodeUser()
{

    return DescSeg32(0, 0xFFFFF, CODE_ER | PRESENT | NSYSTEMSEG | USER
                                    , PAGEGRAN | HIGHSIZE).getDescriptor();    
};

uint64 DescSeg32::flatDataUser()
{

    return DescSeg32(0, 0xFFFFF, DATA_RW | PRESENT | NSYSTEMSEG | USER
                                    , PAGEGRAN | HIGHSIZE).getDescriptor();    
};

bool MngGdt::addDescriptor(uint64 desc)
{
    if(currIdx >= size or currIdx == 0 or gdtLoaded)
    {
        return false;
    }

    if(desc == DescSeg32::flatCodeKernel()) codeSegmentKern = currIdx * 8;
    this->gdt[currIdx++] = desc;
    return true;
};

bool MngGdt::loadGdt()
{
    if(gdtLoaded)
    {
        return false;
    }

    // Set pointer
    uint64 descriptor = 0;
    uint16 size = sizeof(uint64) * this->size - 1;
    uint32 offset = (uint32)(&gdt);

    descriptor |= offset;
    descriptor <<= 16;
    descriptor |= size;

    SWITCHTOGDT(descriptor);

    gdtLoaded = 1;
    return true;
};

// Get static instance of MngGdt initialized once
MngGdt &MngGdt::getInstance()
{
    if(!isInitialize) mngGdt = MngGdt();
    return mngGdt;
};

uint16 MngGdt::getCodeSegmentKernel()
{
    return codeSegmentKern;
};