about summary refs log tree commit diff stats
path: root/kernel.soso/descriptortables.c
blob: b6aab367a243ff5d934d26d287003c17369e45ac (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
#include "screen.h"
#include "common.h"
#include "descriptortables.h"
#include "isr.h"
#include "process.h"

extern void flushGdt(uint32);
extern void flushIdt(uint32);
extern void flushTss();


static void initializeGdt();
static void initializeIdt();
static void setGdtEntry(int32 num, uint32 base, uint32 limit, uint8 access, uint8 gran);
static void setIdtEntry(uint8 num, uint32 base, uint16 sel, uint8 flags);

GdtEntry gGdtEntries[6];
GdtPointer   gGdtPointer;
IdtEntry gIdtEntries[256];
IdtPointer   gIdtPointer;
Tss 		gTss;

extern IsrFunction gInterruptHandlers[];

static void handleDoubleFault(Registers *regs);
static void handleGeneralProtectionFault(Registers *regs);

void initializeDescriptorTables() {
    initializeGdt();

    initializeIdt();

    memset((uint8*)&gInterruptHandlers, 0, sizeof(IsrFunction)*256);

    registerInterruptHandler(8, handleDoubleFault);
    registerInterruptHandler(13, handleGeneralProtectionFault);
}

static void initializeGdt() {
    gGdtPointer.limit = (sizeof(GdtEntry) * 6) - 1;
    gGdtPointer.base  = (uint32)&gGdtEntries;

    setGdtEntry(0, 0, 0, 0, 0);                // 0x00 Null segment
    setGdtEntry(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // 0x08 Code segment
    setGdtEntry(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // 0x10 Data segment
    setGdtEntry(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // 0x18 User mode code segment
    setGdtEntry(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // 0x20 User mode data segment

    //TSS
    memset((uint8*)&gTss, 0, sizeof(gTss));
    gTss.debug_flag = 0x00;
    gTss.io_map = 0x00;
    gTss.esp0 = 0;//0x1FFF0;
    gTss.ss0 = 0x10;//0x18;

    gTss.cs   = 0x0B; //from ring 3 - 0x08 | 3 = 0x0B
    gTss.ss = gTss.ds = gTss.es = gTss.fs = gTss.gs = 0x13; //from ring 3 = 0x10 | 3 = 0x13
    uint32 tss_base = (uint32) &gTss;
    uint32 tss_limit = tss_base + sizeof(gTss);
    setGdtEntry(5, tss_base, tss_limit, 0xE9, 0x00);

    flushGdt((uint32)&gGdtPointer);
    flushTss();
}

// Set the value of one GDT entry.
static void setGdtEntry(int32 num, uint32 base, uint32 limit, uint8 access, uint8 gran) {
    gGdtEntries[num].base_low    = (base & 0xFFFF);
    gGdtEntries[num].base_middle = (base >> 16) & 0xFF;
    gGdtEntries[num].base_high   = (base >> 24) & 0xFF;

    gGdtEntries[num].limit_low   = (limit & 0xFFFF);
    gGdtEntries[num].granularity = (limit >> 16) & 0x0F;
    
    gGdtEntries[num].granularity |= gran & 0xF0;
    gGdtEntries[num].access      = access;
}

void irqTimer();

static void initializeIdt() {
    gIdtPointer.limit = sizeof(IdtEntry) * 256 -1;
    gIdtPointer.base  = (uint32)&gIdtEntries;

    memset((uint8*)&gIdtEntries, 0, sizeof(IdtEntry)*256);

    // Remap the irq table.
    outb(0x20, 0x11);
    outb(0xA0, 0x11);
    outb(0x21, 0x20);
    outb(0xA1, 0x28);
    outb(0x21, 0x04);
    outb(0xA1, 0x02);
    outb(0x21, 0x01);
    outb(0xA1, 0x01);
    outb(0x21, 0x0);
    outb(0xA1, 0x0);

    setIdtEntry( 0, (uint32)isr0 , 0x08, 0x8E);
    setIdtEntry( 1, (uint32)isr1 , 0x08, 0x8E);
    setIdtEntry( 2, (uint32)isr2 , 0x08, 0x8E);
    setIdtEntry( 3, (uint32)isr3 , 0x08, 0x8E);
    setIdtEntry( 4, (uint32)isr4 , 0x08, 0x8E);
    setIdtEntry( 5, (uint32)isr5 , 0x08, 0x8E);
    setIdtEntry( 6, (uint32)isr6 , 0x08, 0x8E);
    setIdtEntry( 7, (uint32)isr7 , 0x08, 0x8E);
    setIdtEntry( 8, (uint32)isr8 , 0x08, 0x8E);
    setIdtEntry( 9, (uint32)isr9 , 0x08, 0x8E);
    setIdtEntry(10, (uint32)isr10, 0x08, 0x8E);
    setIdtEntry(11, (uint32)isr11, 0x08, 0x8E);
    setIdtEntry(12, (uint32)isr12, 0x08, 0x8E);
    setIdtEntry(13, (uint32)isr13, 0x08, 0x8E);
    setIdtEntry(14, (uint32)isr14, 0x08, 0x8E);
    setIdtEntry(15, (uint32)isr15, 0x08, 0x8E);
    setIdtEntry(16, (uint32)isr16, 0x08, 0x8E);
    setIdtEntry(17, (uint32)isr17, 0x08, 0x8E);
    setIdtEntry(18, (uint32)isr18, 0x08, 0x8E);
    setIdtEntry(19, (uint32)isr19, 0x08, 0x8E);
    setIdtEntry(20, (uint32)isr20, 0x08, 0x8E);
    setIdtEntry(21, (uint32)isr21, 0x08, 0x8E);
    setIdtEntry(22, (uint32)isr22, 0x08, 0x8E);
    setIdtEntry(23, (uint32)isr23, 0x08, 0x8E);
    setIdtEntry(24, (uint32)isr24, 0x08, 0x8E);
    setIdtEntry(25, (uint32)isr25, 0x08, 0x8E);
    setIdtEntry(26, (uint32)isr26, 0x08, 0x8E);
    setIdtEntry(27, (uint32)isr27, 0x08, 0x8E);
    setIdtEntry(28, (uint32)isr28, 0x08, 0x8E);
    setIdtEntry(29, (uint32)isr29, 0x08, 0x8E);
    setIdtEntry(30, (uint32)isr30, 0x08, 0x8E);
    setIdtEntry(31, (uint32)isr31, 0x08, 0x8E);

    setIdtEntry(32, (uint32)irqTimer, 0x08, 0x8E);
    setIdtEntry(33, (uint32)irq1, 0x08, 0x8E);
    setIdtEntry(34, (uint32)irq2, 0x08, 0x8E);
    setIdtEntry(35, (uint32)irq3, 0x08, 0x8E);
    setIdtEntry(36, (uint32)irq4, 0x08, 0x8E);
    setIdtEntry(37, (uint32)irq5, 0x08, 0x8E);
    setIdtEntry(38, (uint32)irq6, 0x08, 0x8E);
    setIdtEntry(39, (uint32)irq7, 0x08, 0x8E);
    setIdtEntry(40, (uint32)irq8, 0x08, 0x8E);
    setIdtEntry(41, (uint32)irq9, 0x08, 0x8E);
    setIdtEntry(42, (uint32)irq10, 0x08, 0x8E);
    setIdtEntry(43, (uint32)irq11, 0x08, 0x8E);
    setIdtEntry(44, (uint32)irq12, 0x08, 0x8E);
    setIdtEntry(45, (uint32)irq13, 0x08, 0x8E);
    setIdtEntry(46, (uint32)irq14, 0x08, 0x8E);
    setIdtEntry(47, (uint32)irq15, 0x08, 0x8E);
    setIdtEntry(128, (uint32)isr128, 0x08, 0x8E);

    flushIdt((uint32)&gIdtPointer);
}

static void setIdtEntry(uint8 num, uint32 base, uint16 sel, uint8 flags) {
    gIdtEntries[num].base_lo = base & 0xFFFF;
    gIdtEntries[num].base_hi = (base >> 16) & 0xFFFF;

    gIdtEntries[num].sel     = sel;
    gIdtEntries[num].always0 = 0;
    gIdtEntries[num].flags   = flags  | 0x60;
}

static void handleDoubleFault(Registers *regs) {
    printkf("Double fault!!! Error code:%d\n", regs->errorCode);

    PANIC("Double fault!!!");
}

static void handleGeneralProtectionFault(Registers *regs) {
    printkf("General protection fault!!! Error code:%d - IP:%x\n", regs->errorCode, regs->eip);

    Thread* faultingThread = getCurrentThread();
    if (NULL != faultingThread) {
        Thread* mainThread = getMainKernelThread();

        if (mainThread == faultingThread) {
            PANIC("General protection fault in Kernel main thread!!!");
        }
        else {
            printkf("Faulting thread is %d\n", faultingThread->threadId);

            if (faultingThread->userMode) {
                printkf("Destroying process %d\n", faultingThread->owner->pid);

                destroyProcess(faultingThread->owner);
            }
            else {
                printkf("Destroying kernel thread %d\n", faultingThread->threadId);

                destroyThread(faultingThread);
            }

            waitForSchedule();
        }
    }
    else {
        PANIC("General protection fault!!!");
    }
}