summaryrefslogtreecommitdiffstats
path: root/arch/x86/idt/Idt.cpp
blob: e24cbfe9d9877707ca26e8178eca9634279926df (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "Idt.h"


uint64 MngIdt::idt[MngIdt::size];
bool MngIdt::isInitialize = false;
bool MngIdt::idtLoaded = false;
MngIdt mngIdt;

DescInterrupt32::DescInterrupt32(uint32 offset, uint16 selector, uint8 attr)
{
    uint8 gateType = attr & 0x0f;

    if((gateType == TSK_GATE and offset != 0)
        or gateType < TSK_GATE
        or (gateType > TRAP_GATE_16 and gateType < INT_GATE_32)
        or gateType > TRAP_GATE_32
        or !(attr & PRESENT_BIT))
    {
        offsetLow = 0;
        offsetHight = 0;
        segmentSelector = 0;
        attributes = 0;
    }
    else
    {
        offsetLow = offset & 0xffff;
        offsetHight = (offset & 0xffff0000) >> 16;
        segmentSelector = selector;
        attributes = attr;
    }
};

uint64 DescInterrupt32::getDescriptor() const
{
    uint64 desc = 0;

    desc |= offsetLow;
    desc |= (uint32)segmentSelector << 16;
    desc |= (uint64)attributes << 40;
    desc |= (uint64)offsetHight << 48;

    return desc;
};

uint32 DescInterrupt32::getOffset() const
{
    return offsetLow | offsetHight << 16;
};

DescInterrupt32 DescInterrupt32::InterruptKernelLong(uint32 offset, uint16 selector)
{
    return DescInterrupt32(
                          offset,
                          selector,
                          PRESENT_BIT | RING_KERN | INT_GATE_32
                          );
};

DescInterrupt32 DescInterrupt32::TrapKernelLong(uint32 offset, uint16 selector)
{
    return DescInterrupt32(
                          offset,
                          selector,
                          PRESENT_BIT | RING_KERN | TRAP_GATE_32
                          );
};

DescInterrupt32 DescInterrupt32::TaskKernel(uint16 selector)
{
    return DescInterrupt32(
                          NULL,
                          selector,
                          PRESENT_BIT | RING_KERN | TSK_GATE
                          );
};

bool MngIdt::isIntReserved(uint8 interrupt)
{
    for(uint8 indx = 0; indx < reservedIntSize; indx++)
    {
        if(reservedInts[indx] == interrupt) return true;
    }
    return false;
};

MngIdt::MngIdt()
{
    if(!isInitialize)
    {
        MemoryOperations::set(idt, 0, sizeof(uint64) * size);
        currIdx = NULL;
        idtLoaded = false;
        isInitialize = true;
        irqInterruptStart = firstSystemIntNumber;
    }
};

bool MngIdt::canDescriptorWrite(uint8 idx)
{
    if(idt[idx] or idtLoaded) return false;
    else return true;
};

bool MngIdt::addDescriptor(DescInterrupt32 desc)
{
    uint64 descqw = desc.getDescriptor();

    if(!canDescriptorWrite(currIdx) or isIntReserved(currIdx)
        or desc.getOffset() == NULL) return false;

    idt[currIdx] = descqw;

    uint8 attributes = descqw >> 40;

    if((attributes & RING_DRIVERSH or
       attributes & RING_DRIVERSL) and
       (irqInterruptStart == firstSystemIntNumber))
    {
        irqInterruptStart = (uint8)(descqw >> 16);
    }

    while(isIntReserved(++currIdx));

    return true;
};

bool MngIdt::setDescriptor(DescInterrupt32 desc, uint8 idx)
{
    if(!canDescriptorWrite(idx)) return false;

    idt[idx] = desc.getDescriptor();
    return true;
};

bool MngIdt::loadIdt()
{
    if(idtLoaded) return false;

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

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

    // Load descriptor in LDTR
    asm ("lidt %0" :: "m"(descriptor));

    idtLoaded = true;
    return true;
};

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

void DescInterrupt32::setOffset(uint32 offset)
{
    uint8 gateType = attributes & 0x0f;

    if(!(gateType == TSK_GATE and offset != 0))
    {
        offsetLow = offset & 0xffff;
        offsetHight = (offset & 0xffff0000) >> 16;
    }
};
void DescInterrupt32::setGateType(uint8 gateType)
{
    if(gateType < TSK_GATE
        or (gateType > TRAP_GATE_16 and gateType < INT_GATE_32)
        or gateType > TRAP_GATE_32
    ) return;
    else if(gateType == TSK_GATE)
    {
        offsetLow = offsetHight = 0;
    }

    attributes &= 0xfff0;
    attributes |= (gateType & 0x0f);
};

void DescInterrupt32::setDpl(uint8 dpl)
{
    attributes = (dpl & 0x03) << 5;
};

bool MngIdt::isInterruptSet(uint8 numberInterrupt)
{
    return idt[numberInterrupt];
};

uint8 MngIdt::getIrqInterruptStart()
{
    return irqInterruptStart;
};