summaryrefslogtreecommitdiffstats
path: root/arch/x86/gdt/Gdt.cpp
blob: 80fcd13091e16baa4ce18a9745c5a2e39b5451a9 (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
#include "Gdt.h"


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();    
};

MngGdt::MngGdt()
{
    gdt[0] = 0;
    currIdx = 1;
    isGdtLoaded = 0;
};

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

    this->gdt[currIdx++] = desc;
    return true;
};

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

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

    pointer |= offset;
    pointer <<= 16;
    pointer |= size;

    SWITCHTOGDT(pointer);

    isGdtLoaded = 1;
    return true;
};

bool MngGdt::getIsGdtLoaded()
{
    return isGdtLoaded;
};

void MngGdt::operator=(const MngGdt& src)
{
    if(this->isGdtLoaded == 1 || src.isGdtLoaded == 1)
    {
        return;
    }

    // Fix it. Write method to copy from ... to ...
    for(int i = 0; i < this->size; i++) this->gdt[i] = src.gdt[i];
    this->isGdtLoaded = src.isGdtLoaded;
    this->currIdx = src.currIdx;
};