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!!!");
    }
}
n>:&:stream:char -> out:&:cell, in:&:stream:char [ local-scope load-ingredients # skip whitespace in <- skip-whitespace in c:char, eof?:boolean <- peek in reply-if eof?, 0/nil pair?:boolean <- equal c, 40/open-paren { break-if pair? # atom b:&:buffer <- new-buffer 30 { done?:boolean <- end-of-stream? in break-if done? # stop before close paren or space c:char <- peek in done? <- equal c, 41/close-paren break-if done? done? <- space? c break-if done? c <- read in b <- append b, c loop } s:text <- buffer-to-array b out <- new-atom s } { break-unless pair? # pair read in # skip the open-paren out <- new cell:type # start out with nil # read in first element of pair { end?:boolean <- end-of-stream? in not-end?:boolean <- not end? assert not-end?, [unbalanced '(' in expression] c <- peek in close-paren?:boolean <- equal c, 41/close-paren break-if close-paren? first:&:cell, in <- parse in *out <- merge 1/pair, first, 0/nil } # read in any remaining elements curr:&:cell <- copy out { in <- skip-whitespace in end?:boolean <- end-of-stream? in not-end?:boolean <- not end? assert not-end?, [unbalanced '(' in expression] # termination check: ')' c <- peek in { close-paren?:boolean <- equal c, 41/close-paren break-unless close-paren? read in # skip ')' break +end-pair:label } # still here? read next element of pair next:&:cell, in <- parse in is-dot?:boolean <- atom-match? next, [.] { break-if is-dot? next-curr:&:cell <- new-pair next, 0/nil curr <- set-rest curr, next-curr curr <- rest curr } { break-unless is-dot? # deal with dotted pair in <- skip-whitespace in c <- peek in not-close-paren?:boolean <- not-equal c, 41/close-paren assert not-close-paren?, [')' cannot immediately follow '.'] final:&:cell <- parse in curr <- set-rest curr, final # we're not gonna update curr, so better make sure the next iteration # is going to end the pair in <- skip-whitespace in c <- peek in close-paren?:boolean <- equal c, 41/close-paren assert close-paren?, ['.' must be followed by exactly one expression before ')'] } loop } +end-pair } ] def skip-whitespace in:&:stream:char -> in:&:stream:char [ local-scope load-ingredients { done?:boolean <- end-of-stream? in reply-if done?, 0/null c:char <- peek in space?:boolean <- space? c break-unless space? read in # skip loop } ] def to-text x:&:cell -> out:text [ local-scope load-ingredients buf:&:buffer <- new-buffer 30 buf <- to-buffer x, buf out <- buffer-to-array buf ] def to-buffer x:&:cell, buf:&:buffer -> buf:&:buffer [ local-scope load-ingredients # base case: empty cell { break-if x buf <- append buf, [<>] reply } # base case: atom { s:text, atom?:boolean <- maybe-convert *x, atom:variant break-unless atom? buf <- append buf, s reply } # recursive case: pair buf <- append buf, [< ] first:&:cell <- first x buf <- to-buffer first, buf buf <- append buf, [ | ] rest:&:cell <- rest x buf <- to-buffer rest, buf buf <- append buf, [ >] ] scenario parse-single-letter-atom [ local-scope s:text <- new [a] x:&:cell <- parse s s2:text, 10:boolean/raw <- maybe-convert *x, atom:variant 11:@:char/raw <- copy *s2 memory-should-contain [ 10 <- 1 # parse result is an atom 11:@:char <- [a] ] ] scenario parse-atom [ local-scope s:text <- new [abc] x:&:cell <- parse s s2:text, 10:boolean/raw <- maybe-convert *x, atom:variant 11:@:char/raw <- copy *s2 memory-should-contain [ 10 <- 1 # parse result is an atom 11:@:char <- [abc] ] ] scenario parse-list-of-two-atoms [ local-scope s:text <- new [(abc def)] x:&:cell <- parse s trace-should-contain [ app/parse: < abc | < def | <> > > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x x2:&:cell <- rest x s1:text, 11:boolean/raw <- maybe-convert *x1, atom:variant 12:boolean/raw <- is-pair? x2 x3:&:cell <- first x2 s2:text, 13:boolean/raw <- maybe-convert *x3, atom:variant 14:&:cell/raw <- rest x2 20:@:char/raw <- copy *s1 30:@:char/raw <- copy *s2 memory-should-contain [ 10 <- 1 # parse result is a pair 11 <- 1 # result.first is an atom 12 <- 1 # result.rest is a pair 13 <- 1 # result.rest.first is an atom 14 <- 0 # result.rest.rest is nil 20:@:char <- [abc] # result.first 30:@:char <- [def] # result.rest.first ] ] scenario parse-list-with-extra-spaces [ local-scope s:text <- new [ ( abc def ) ] # extra spaces x:&:cell <- parse s trace-should-contain [ app/parse: < abc | < def | <> > > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x x2:&:cell <- rest x s1:text, 11:boolean/raw <- maybe-convert *x1, atom:variant 12:boolean/raw <- is-pair? x2 x3:&:cell <- first x2 s2:text, 13:boolean/raw <- maybe-convert *x3, atom:variant 14:&:cell/raw <- rest x2 20:@:char/raw <- copy *s1 30:@:char/raw <- copy *s2 memory-should-contain [ 10 <- 1 # parse result is a pair 11 <- 1 # result.first is an atom 12 <- 1 # result.rest is a pair 13 <- 1 # result.rest.first is an atom 14 <- 0 # result.rest.rest is nil 20:@:char <- [abc] # result.first 30:@:char <- [def] # result.rest.first ] ] scenario parse-list-of-more-than-two-atoms [ local-scope s:text <- new [(abc def ghi)] x:&:cell <- parse s trace-should-contain [ app/parse: < abc | < def | < ghi | <> > > > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x x2:&:cell <- rest x s1:text, 11:boolean/raw <- maybe-convert *x1, atom:variant 12:boolean/raw <- is-pair? x2 x3:&:cell <- first x2 s2:text, 13:boolean/raw <- maybe-convert *x3, atom:variant x4:&:cell <- rest x2 14:boolean/raw <- is-pair? x4 x5:&:cell <- first x4 s3:text, 15:boolean/raw <- maybe-convert *x5, atom:variant 16:&:cell/raw <- rest x4 20:@:char/raw <- copy *s1 30:@:char/raw <- copy *s2 40:@:char/raw <- copy *s3 memory-should-contain [ 10 <- 1 # parse result is a pair 11 <- 1 # result.first is an atom 12 <- 1 # result.rest is a pair 13 <- 1 # result.rest.first is an atom 14 <- 1 # result.rest.rest is a pair 15 <- 1 # result.rest.rest.first is an atom 16 <- 0 # result.rest.rest.rest is nil 20:@:char <- [abc] # result.first 30:@:char <- [def] # result.rest.first 40:@:char <- [ghi] # result.rest.rest ] ] scenario parse-nested-list [ local-scope s:text <- new [((abc))] x:&:cell <- parse s trace-should-contain [ app/parse: < < abc | <> > | <> > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x 11:boolean/raw <- is-pair? x x2:&:cell <- first x1 s1:text, 12:boolean/raw <- maybe-convert *x2, atom:variant 13:&:cell/raw <- rest x1 14:&:cell/raw <- rest x 20:@:char/raw <- copy *s1 memory-should-contain [ 10 <- 1 # parse result is a pair 11 <- 1 # result.first is a pair 12 <- 1 # result.first.first is an atom 13 <- 0 # result.first.rest is nil 14 <- 0 # result.rest is nil 20:@:char <- [abc] # result.first.first ] ] scenario parse-nested-list-2 [ local-scope s:text <- new [((abc) def)] x:&:cell <- parse s trace-should-contain [ app/parse: < < abc | <> > | < def | <> > > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x 11:boolean/raw <- is-pair? x x2:&:cell <- first x1 s1:text, 12:boolean/raw <- maybe-convert *x2, atom:variant 13:&:cell/raw <- rest x1 x3:&:cell <- rest x x4:&:cell <- first x3 s2:text, 14:boolean/raw <- maybe-convert *x4, atom:variant 15:&:cell/raw <- rest x3 20:@:char/raw <- copy *s1 30:@:char/raw <- copy *s2 memory-should-contain [ 10 <- 1 # parse result is a pair 11 <- 1 # result.first is a pair 12 <- 1 # result.first.first is an atom 13 <- 0 # result.first.rest is nil 14 <- 1 # result.rest.first is an atom 15 <- 0 # result.rest.rest is nil 20:@:char <- [abc] # result.first.first 30:@:char <- [def] # result.rest.first ] ] # todo: uncomment these tests after we figure out how to continue tests after # assertion failures #? scenario parse-error [ #? local-scope #? s:text <- new [(] #? #? hide-errors #? x:&:cell <- parse s #? #? show-errors #? trace-should-contain [ #? error: unbalanced '(' in expression #? ] #? ] #? #? scenario parse-error-after-element [ #? local-scope #? s:text <- new [(abc] #? #? hide-errors #? x:&:cell <- parse s #? #? show-errors #? trace-should-contain [ #? error: unbalanced '(' in expression #? ] #? ] scenario parse-dotted-list-of-two-atoms [ local-scope s:text <- new [(abc . def)] x:&:cell <- parse s trace-should-contain [ app/parse: < abc | def > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x x2:&:cell <- rest x s1:text, 11:boolean/raw <- maybe-convert *x1, atom:variant s2:text, 12:boolean/raw <- maybe-convert *x2, atom:variant 20:@:char/raw <- copy *s1 30:@:char/raw <- copy *s2 memory-should-contain [ # parses to < abc | def > 10 <- 1 # parse result is a pair 11 <- 1 # result.first is an atom 12 <- 1 # result.rest is an atom 20:@:char <- [abc] # result.first 30:@:char <- [def] # result.rest ] ] scenario parse-dotted-list-of-more-than-two-atoms [ local-scope s:text <- new [(abc def . ghi)] x:&:cell <- parse s trace-should-contain [ app/parse: < abc | < def | ghi > > ] 10:boolean/raw <- is-pair? x x1:&:cell <- first x x2:&:cell <- rest x s1:text, 11:boolean/raw <- maybe-convert *x1, atom:variant 12:boolean/raw <- is-pair? x2 x3:&:cell <- first x2 s2:text, 13:boolean/raw <- maybe-convert *x3, atom:variant x4:&:cell <- rest x2 s3:text, 14:boolean/raw <- maybe-convert *x4, atom:variant 20:@:char/raw <- copy *s1 30:@:char/raw <- copy *s2 40:@:char/raw <- copy *s3 memory-should-contain [ 10 <- 1 # parse result is a pair 11 <- 1 # result.first is an atom 12 <- 1 # result.rest is a pair 13 <- 1 # result.rest.first is an atom 14 <- 1 # result.rest.rest is an atom 20:@:char <- [abc] # result.first 30:@:char <- [def] # result.rest.first 40:@:char <- [ghi] # result.rest.rest ] ] ## convert tree of cells to mu text def to-mu in:&:cell -> out:text [ local-scope load-ingredients buf:&:buffer <- new-buffer 30 buf <- to-mu in, buf out <- buffer-to-array buf ] def to-mu in:&:cell, buf:&:buffer -> buf:&:buffer, result-name:text [ local-scope load-ingredients # null cell? no change. # pair with all atoms? gensym a new variable # pair containing other pairs? recurse result-name <- copy 0 ]