-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupt.c
336 lines (294 loc) · 5.82 KB
/
interrupt.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "header/type.h"
#include "header/io.h"
#include "header/kprint.h"
#include "header/power.h"
#include <link.h>
#include <kprint.h>
/* We do not use 16-bit mode here and everywhere!*/
#define ISR_TASK 0x5
#define ISR_INTR 0xE
#define ISR_TRAP 0XF
#include <uart.h>
/* PIC helpers */
#define PIC1 0x20
#define PIC2 0xA0
#define PIC1_CMD PIC1
#define PIC1_DATA (PIC1+1)
#define PIC2_CMD PIC2
#define PIC2_DATA (PIC2+1)
#define PIC_READ_IRR 0x0a
#define PIC_READ_ISR 0x0b
#define ICW1_ICW4 0x01
#define ICW1_SINGLE 0x02
#define ICW1_INTERVAL4 0X04
#define ICW1_LEVEL 0x08
#define ICW1_INIT 0x10
/* ICW2 : vector offset
* ICW3 : M/S wiring
* ICW4 : Additional info
*/
#define ICW4_8086 0x01
#define ICW4_AUTO 0x02
#define ICW4_BUF_SLAVE 0x08
#define ICW4_BUF_MASTER 0x0c
#define ICW4_SFNM 0x10
#define PIC1_OFFSET 0x20
#define PIC2_OFFSET 0x28
#define isr __attribute__((interrupt))
/* The IDT table requires the minimum limit of 100h.
* There are 256 intr's available.
*/
volatile struct idt_entry {
u16 limit; //bytes-1
u32 addr;//Linear address
} __attribute__((packed));
volatile struct idt_entry idt_entry;
/* IA-32 table */
struct idt_desc {
u16 offset_l;
u16 cs;
u8 zero;
//Control flags
u8 type:4 ;
u8 ss:1 ;
u8 ring:2 ;
u8 present:1 ;
u16 offset_h;
} __attribute__((packed));
void int_start(void) //Only use this when we are ready!
{
asm volatile("sti");
return;
}
void int_stop(void) //Use this when we are done!
{
asm volatile("cli");
return ;
}
u8 pic_get_irq(int ocw3)
{
outb(PIC1_CMD,ocw3);
outb(PIC2_CMD,ocw3);
return (inb(PIC2_CMD)<<8)|inb(PIC1_CMD);
}
u8 this_int(void) //Only in ISRs.
{
return pic_get_irq(PIC_READ_ISR);
}
void int_ack(void) //PIC version,use this at the end of ISR.
{
if (this_int()>8)
outb(PIC2_CMD,0x20);
outb(PIC1_CMD,0x20);
}
void irq_mask(u8 irq)
{
u16 port;
u8 value;
if (irq<8)
{
port=PIC1_DATA;
}
else
{
port=PIC2_DATA;
irq-=8;
}
value=inb(port)|(1<<irq);
outb(port,value);
}
void irq_unmask(irq)
{
u16 port;
u8 value;
if (irq<8)
{
port=PIC1_DATA;
}
else
{
port=PIC2_DATA;
irq-=8;
}
value=inb(port)&(~(1<<irq));
outb(port,value);
}
struct interrupt_frame {
u32 eip;
u32 cs;
u32 eflags;
};
isr void unknown_isr(struct interrupt_frame *frame)
{
int_stop();
if (this_int()!=1)
{
printk("intr %x\n",this_int());
}
int_ack();
int_start();
return;
}
isr void unknown_fault(struct interrupt_frame *frame,u32 code)
{
kprint("Unknown Fault.\n",PR_VGA,ERROR);
return;
}
isr void div_by_zero(struct interrupt_frame *frame)
{
printk("fault: Divide by zero at ip %x.\n",frame->eip);
printk("System halted.");
halt();
return ;
}
isr void overflow(struct interrupt_frame *frame)
{
kprint("fault: Overflow. at ip %x\n",PR_VGA,ERROR);
return ;
}
isr void bound_check(struct interrupt_frame *frame)
{
kprint("Bound check failed.\n",PR_VGA,ERROR);
return ;
}
isr void illegal_op(struct interrupt_frame *frame)
{
printk("fault: Illegal instruction at ip %x.\nSystem halted.\n",frame->eip);
halt();
return ;
}
isr void nodev(struct interrupt_frame *frame)
{
kprint("No such device.\n",PR_VGA,ERROR);
return ;
}
isr void double_fault(struct interrupt_frame *frame,u32 errno)
{
kprint("Double fault occurred!\n",PR_VGA,ERROR);
return ;
}
isr void invalid_seg(struct interrupt_frame *frame,u32 errno)
{
kprint("Attempt to load an invalid segment.\n",PR_VGA,ERROR);
return ;
}
isr void gpe(struct interrupt_frame *frame,u32 errno)
{
printk("fault:General Protection Fault at %x errno %x\n",frame->eip,errno);
for (;;);
return ;
}
isr void page_fault(struct interrupt_frame *frame,u32 errno)
{
u32 addr;
asm volatile("mov %%cr2,%0":"=a"(addr)::);
if (addr)
{
printk("fault:Page fault at %x access %x.\n",frame->eip,addr);
}
else
printk("fault:Attempt to deref NULL pointer at %x.\n",frame->eip);
for (;;);
return ;
}
isr void nmi_handler(struct interrupt_frame *frame)
{
printk("fault:nmi at %x\n",frame->eip);
halt();
}
void (*faults[])(struct interrupt_frame,u32)=
{
div_by_zero,
unknown_isr, //trap,not now.
nmi_handler, //Unused
unknown_isr, //int 3
overflow,
bound_check,
illegal_op,
nodev,
double_fault,//With errno
unknown_isr, //x87_seg_overflow
unknown_fault, //TSS,errno
invalid_seg,
invalid_seg,
gpe,
page_fault, //Page fault.This will be a very big handler.
unknown_isr, //x87 error.
unknown_isr
};
void idt_write(struct idt_desc *this,u32 func,u8 type,u8 ring)
{
this->offset_l = func&0xFFFF;
this->cs = 0x08;
this->zero = 0;
this->type = type;
this->ss = 0;
this->ring = ring;
this->present = 1;
this->offset_h = (func&0xFFFF0000)>>16;
asm volatile("mfence":::"memory");
return;
}
volatile struct idt_desc IDT[256];
void idt_load(struct idt_entry *entry,struct idt_desc *idt,u32 limit)
{
entry->limit=limit*8-1;
entry->addr=idt;
asm volatile("mfence;lidt (%0)"::"d"(entry):"memory");
return;
}
void intr_init(void)
{
for (u32 i=0;i<=17;i++)
idt_write(IDT+i,faults[i],ISR_TRAP,0);
for (u32 i=18;i<255;i++)
{
idt_write(IDT+i,unknown_isr,ISR_INTR,0);
}
idt_write(IDT+0x20+0x4,serial_handler,ISR_INTR,0);
idt_load(&idt_entry,IDT,255);
pic_init();
for (int i=1;i<=16;i++)
irq_unmask(i);
printk("IDT at %x\n",&idt_entry);
return;
}
void idt_reload(void)
{
idt_load(&idt_entry,IDT,255);
return;
}
void set_intr(int n,void (*func)(struct interrupt_frame,u32))
{
idt_write(IDT+n,func,ISR_INTR,0);
idt_reload();
return ;
}
void pic_remap(int off1,int off2)
{
u8 a1,a2;
a1=inb(PIC1_DATA);
a2=inb(PIC2_DATA);
outb(PIC1_CMD,ICW1_INIT|ICW1_ICW4);
outb(PIC2_CMD,ICW1_INIT|ICW1_ICW4);
outb(PIC1_DATA,off1);
outb(PIC2_DATA,off2);
outb(PIC1_DATA,4);
outb(PIC2_DATA,2);
outb(PIC1_DATA,ICW4_8086);
outb(PIC2_DATA,ICW4_8086);
outb(PIC1_DATA,a1);
outb(PIC2_DATA,a2);
return;
}
void pic_init(void)
{
pic_remap(PIC1_OFFSET,PIC2_OFFSET);
return;
}
void pic_disable(void)
{
outb(PIC1_CMD,0xff);
outb(PIC2_CMD,0xff);
return ;
}