summaryrefslogtreecommitdiff
path: root/kernel/interrupts.c
blob: 30d89947ed868cc24d6444b67530c4585dfb09d5 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "kernel/kernel.h"
#include "asm_int.h"
#include "asm_pit.h"
#include "driver/mouse.h"
#include "interrupts.h"
#include "asm_x86.h"

/** The size of our interrupts table  */
#define INT_MAX 256 // 0-255

/** The interrupt descriptor table */
static struct int_desc
{
    uint16_t addrLo;
    uint16_t sel;
    uint8_t zeros;
    uint8_t flags;
    uint16_t addrHi;
} idt[INT_MAX];

/** The interrupt descriptor table descriptor */
static struct idt_desc
{
    uint16_t size;
    uint16_t baseLo;
    uint16_t baseHi;
} idtd;

/** Sets a handler for a specific interrupt */
static void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr)
{
   uint64_t base=(uint32_t)&(*(uint32_t*)addr);

    idt[irq].addrLo = base & 0xffff;
    idt[irq].addrHi = (base >> 16) & 0xffff;
    idt[irq].zeros=0;
    idt[irq].flags=flags;
    idt[irq].sel=sel;
}

/** Installs the interrupt table */
void int_install()
{
    idtd.size=sizeof(struct int_desc)*INT_MAX;
    uint32_t addr=(uint32_t)&idt[0];
    idtd.baseHi=addr>>16;
    idtd.baseLo=0xffff&addr;
    __asm__("lidt %0"::"m" (idtd));
}

/*
 * Interrupt dispatcher 
 *
 * Remeber that we are inside an interrupt here!
 *
 */

uint32_t interrupt_handler(uint32_t esp, uint32_t irq)
{

    // DO NOT WRITE INSIDE INTERRUPTS!! COZ IT ACQUIRES LOCK AND WE WILL DEADLOCK
    //klog("int: %d on 0x%x",irq,apicID());
    if(irq==0)asm_pit_tick();  

    // mouse and kb
    if(irq==1 || irq==12 ){
	uint32_t in=x86_inb(0x60);
	if(irq==1)keyboard_handle(in); // do this in separate thread!
	// TODO: mouse
	
	// test ipi
	apicIPI(2,170);
//	klog("0x60 in %d",in);
    }

    // 0x80 - a syscall is coming in
    if(irq==128){

	uint32_t *stack=esp;
	uint32_t eax=stack[11];
	uint32_t ebx=stack[8];
	uint32_t ecx=stack[10];
	uint32_t edx=stack[9];

//	klog("syscall: %d (ebx=0x%08X,ecx=0x%08X,edx=0x%08X)",eax,ebx,ecx,edx);

	task_syscall(eax,ebx,ecx,edx);
	esp=my_scheduler(esp,2); // force scheduling of pid=2 (kernel worker)
    }

    if(irq==0 || irq==129)esp=my_scheduler(esp,-1); // autoschedule

    if(irq==200){ // apic timer
	apicEOI();
    }
    if(irq==255)kpanic("Unhandled Interrupt!");
    if(irq==170){
//	if(apicID()!=0)apicIPI(0,170);
	apicEOI();
    }

    return esp;
}

// log helpers //
void errlog(uint32_t error_code)
{
    klog("error_code: 0x%08X",error_code);
}

void defklog(uint32_t esp)
{
    klog("EXCEPTION: eip: 0x%08X",*(uint32_t *)esp);
    esp+=4;
    klog("EXCEPTION: segment: 0x%08X",*(uint16_t *)esp);
    esp+=4;
    klog("EXCPETION: eflags: 0x%08X",*(uint32_t *)esp);
}

void show_selector_error(uint32_t err)
{
   klog("Selector Error Details:");
   klog("External Event: %x",err&0b1);
   klog("Location: 0x%x (0-GDT/1-IDT/2-LDT/3-IDT)",err&0b110);
   klog("Selector: 0x%x",err&0b1111111111111000);
}

void show_page_fault_error(uint32_t error_code)
{
   klog("Page Fault Error Details:");
   klog("error_code_P (Present): %d",error_code&1?1:0);
   klog("error_code_W/R (Write): %d",error_code&2?1:0);
   klog("error_code_U/S (User): %d",error_code&4?1:0);
   klog("error_code_RSVD (Reserved Write) : %d",error_code&8?1:0);
   klog("error_code_I/D (Instruction Fetch): %d",error_code&16?1:0);
   klog("at addr: 0x%08X",x86_get_cr(2));
}

void exception_handle(uint32_t esp, uint32_t irq)  
{
    uint32_t error_code=0;

    klog("EXCEPTION: apicID: 0x%08X",apicID());
    klog("EXCEPTION: vector nr.: %d",irq);

    switch(irq){ //this interrupts push also an error_code
	case 8:
	case 10:
	case 11:
	case 12:
	case 13:
	case 14:
	case 17:
	case 30:
	    error_code = *(uint32_t *)esp;
	    esp+=4;
    }
    klog("EXCEPTION: error_code: %d",irq);
    defklog(esp);

    switch(irq){
	case 0:
	    kpanic("Divide by 0");
	case 1:
	    kpanic("Single step (debugger)");
	case 2:
	    kpanic("Non Maskable Interrupt");
	case 3:
	    kpanic("Breakpoint (debugger)");
	case 4:
	    kpanic("Overflow");
	case 5:
	    kpanic("Bounds check");
	case 6:
	    kpanic("Undefined OP Code");
	case 7:
	    kpanic("No coprocessor");
	case 8:
	    kpanic("Double Fault");
	case 9:
	    kpanic("Coprocessor Segment Overrun");
	case 10:
	    show_selector_error(error_code);
	    kpanic("Invalid TSS");
	case 11:
	    show_selector_error(error_code);
	    kpanic("Segment Not Present");
	case 12:
	    show_selector_error(error_code);
	    kpanic("Stack Segment Overrun");
	case 13:
	    show_selector_error(error_code);
	    kpanic("Exception: Fault: General Protection Fault");
	case 14:
	    show_page_fault_error(error_code);
	    kpanic("Exception: Fault: Page Fault");
	case 15:
	    kpanic("RESERVED");
	case 16:
	    kpanic("Coprocessor error");
	case 17:
	    kpanic("Alignment Check");
	case 18:
	    kpanic("Machine Check");
    }
}


// set default handler for all interrupts for a start
void interrupts_init(uint16_t sel)
{
    klog("initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd);

    // Default interrupt handling
    for(int i=0; i<INT_MAX; i++)
    {
	int_install_ir(i, 0b10001110, sel,&int255);
    }

    // Exceptions
    int_install_ir(0,  0b10001110, 0x08,&exc0);
    int_install_ir(1,  0b10001110, 0x08,&exc1);
    int_install_ir(2,  0b10001110, 0x08,&exc2);
    int_install_ir(3,  0b10001110, 0x08,&exc3);
    int_install_ir(4,  0b10001110, 0x08,&exc4);
    int_install_ir(5,  0b10001110, 0x08,&exc5);
    int_install_ir(6,  0b10001110, 0x08,&exc6);
    int_install_ir(7,  0b10001110, 0x08,&exc7);
    int_install_ir(8,  0b10001110, 0x08,&exc8);
    int_install_ir(9,  0b10001110, 0x08,&exc9);
    int_install_ir(10, 0b10001110, 0x08,&exc10);
    int_install_ir(11, 0b10001110, 0x08,&exc11);
    int_install_ir(12, 0b10001110, 0x08,&exc12);
    int_install_ir(13, 0b10001110, 0x08,&exc13);
    int_install_ir(14, 0b10001110, 0x08,&exc14);
    int_install_ir(15, 0b10001110, 0x08,&exc15);
    int_install_ir(16, 0b10001110, 0x08,&exc16);
    int_install_ir(17, 0b10001110, 0x08,&exc17);
    int_install_ir(18, 0b10001110, 0x08,&exc18);
    

    // Custom interrupts (hardcoded)
    // remember that we shifted all interrupts with the pic by 32

    // PIT interrupt handler (irq 0 => 32)
    int_install_ir(32,   0b10001110, 0x08,&int0);

    // Keyboard interrupt handler (irq 1 => 33)
    int_install_ir(33,   0b10001110, 0x08,&int1);

    // Mouse  interrupt handler (irq 12 => 34)
    int_install_ir(44,   0b10001110, 0x08,&int12);

    // System Calls / they can be called from ring3 (0b11)
    int_install_ir(0x80, 0b11101110, 0x08,&int128);

    // Wake Scheduler
    int_install_ir(0x81, 0b11101110, 0x08,&int129);

    // Wake SMP Scheduler
    int_install_ir(0xAA, 0b11101110, 0x08,&int170);

    // APIC Timer
    int_install_ir(200, 0b11101110, 0x08,&int200);

    // install IVT
    int_install();
}