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
|
#include "kernel.h"
#include "log.h"
#include "asm_int.h"
#include "asm_pit.h"
#include "driver/mouse.h"
#include "driver/keyboard.h"
#include "interrupts.h"
#include "scheduler.h"
#include "asm_x86.h"
#include "smp.h"
#include "apic.h"
#include "ringbuffer.h"
ringbuffer mouse_in;
ringbuffer kb_in;
/** 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;
}
/*
* Interrupt dispatcher
*
* Remeber that we are inside an interrupt here!
*
*/
uint32_t interrupt_handler(uint32_t esp, uint32_t irq)
{
uint32_t *stack;
// process irq
switch(irq)
{
case INTERRUPT_PIT_TIMER:
asm_pit_tick();
break;
case INTERRUPT_KEYBOARD:
ringbuffer_put(&kb_in,x86_inb(0x60));
break;
case INTERRUPT_MOUSE:
ringbuffer_put(&mouse_in,x86_inb(0x60));
break;
case INTERRUPT_SYSCALL:
stack=esp;
task_syscall(stack[11],stack[8],stack[10],stack[9]); //eax,ebx,ecx,edx
break;
case INTERRUPT_APIC_TIMER:
case INTERRUPT_IPI:
esp=scheduler_run(esp,0);
break;
case 255: // default or spurious
kpanic("Spurious/Unknown Interrupt!?");
break;
}
// reschedule to kernel worker on these
if(irq==INTERRUPT_SYSCALL||irq==INTERRUPT_KEYBOARD||irq==INTERRUPT_MOUSE)
{
esp=scheduler_wake_worker(esp);
}
// ack all to LAPIC, except software syscalls
if(irq!=INTERRUPT_SYSCALL)
apic_eoi();
return esp;
}
/**
* init interrupt descriptor table
*/
void interrupts_init(uint16_t sel)
{
klog("initializing Mouse and Kb input buffers");
kb_in=ringbuffer_init(1);// 4096 bytes ringbuffer;
mouse_in=ringbuffer_init(1);// 4096 bytes ringbuffer;
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);
// PIT (IOAPIC)
int_install_ir(0x90, 0b10001110, 0x08,&int144);
// Keyboard (IOAPIC)
int_install_ir(0x91, 0b10001110, 0x08,&int145);
// Mouse (IOAPIC)
int_install_ir(0x92, 0b10001110, 0x08,&int146);
// APIC Timer (LAPIC)
int_install_ir(0x8C, 0b10001110, 0x08,&int140);
// System Calls (User Software / Ring 3)
int_install_ir(0x80, 0b11101110, 0x08,&int128);
// Inter Processesor Interrupts (Kernel Software)
int_install_ir(0x81, 0b10001110, 0x08,&int129);
}
/** Installs the interrupt table */
void interrupts_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));
}
|