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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/**
* Copyright (c) 2026 Alexey Gavrilov <alexey.gavrilov@mail.com>
*
* This file is part of ObjectiveOS.
*
* ObjectiveOS is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* ObjectiveOS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* ObjectiveOS. If not, see <https://www.gnu.org/licenses/>.
*/
#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;
};
|