-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterrupt_handlers.cpp
More file actions
238 lines (198 loc) · 6.79 KB
/
interrupt_handlers.cpp
File metadata and controls
238 lines (198 loc) · 6.79 KB
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
#include "interrupt_handlers.hpp"
u32 interrupts_counter[256] = {0};
u32 pic_interrupts_counter[16] = {0};
void (*int_handler[32])(const int_registers*);
void (*pic_int_handler[16])(const int_registers*);
// interrupts: disabled <= 0 < enabled
i32 interrupts_semaphore = 0; // used by (push|pop)_ints
void dump_int_summary()
{
for (u32 i = 0; i < sizeof(pic_interrupts_counter) / sizeof(u32); ++i)
{
if (pic_interrupts_counter[i])
term.printk("PIC int %x: %d\n", i, pic_interrupts_counter[i]);
}
for (u32 i = 0; i < sizeof(interrupts_counter) / sizeof(u32); ++i)
{
if (interrupts_counter[i])
term.printk("int %x: %d\n", i, interrupts_counter[i]);
}
}
void interrupt_handler(const int_registers int_regs)
{
#ifdef DEBUG_INTERRUPTS
LOG(KERN_DEBUG "int %d: 0x%x \n", int_regs.int_nbr, int_regs.err_code);
#endif
interrupts_counter[int_regs.int_nbr]++;
if (int_handler[int_regs.int_nbr])
{
int_handler[int_regs.int_nbr](&int_regs);
}
else
{
term.printk("unknown interrupt (%d)\n", int_regs.int_nbr);
PANIC("unhandled interrupt\n");
}
}
void pic_interrupt_handler(const int_registers int_regs)
{
#ifdef DEBUG_INTERRUPTS
if (int_regs.pic_int_nbr != 0 && int_regs.pic_int_nbr != 1) // skip: timer, kbd
LOG(KERN_DEBUG "PIC int %d\n", int_regs.pic_int_nbr);
#endif
if (int_regs.pic_int_nbr >= 8)
outb(PIC2_CMD, PIC_EOI);
outb(PIC1_CMD, PIC_EOI);
pic_interrupts_counter[int_regs.pic_int_nbr]++;
if (pic_int_handler[int_regs.pic_int_nbr])
{
pic_int_handler[int_regs.pic_int_nbr](&int_regs);
}
else
{
term.printk("unknown interrupt (%d)\n", int_regs.pic_int_nbr);
PANIC("unhandled PIC interrupt\n");
}
}
void add_interrupt_handler(u32 nbr, void (*handler)(const int_registers*))
{
int_handler[nbr] = handler;
}
void add_pic_interrupt_handler(u32 nbr, void (*handler)(const int_registers*))
{
pic_int_handler[nbr] = handler;
}
void set_interrupts_handlers()
{
add_interrupt_handler(INT_GPF , general_protection_fault_handler);
add_interrupt_handler(INT_ZERO_DIV , divide_by_zero_handler);
add_interrupt_handler(INT_DBG , debug_trap_handler);
add_interrupt_handler(INT_NMI , nmi_handler);
add_interrupt_handler(INT_BREAKPOINT , breakpoint_handler);
add_interrupt_handler(INT_OVERFLOW , overflow_handler);
add_interrupt_handler(INT_BOUND , bound_check_fail_handler);
add_interrupt_handler(INT_OPCODE , invalid_opcode_handler);
add_interrupt_handler(INT_FEATURE , non_available_feature_handler);
add_interrupt_handler(INT_DOUBLE , double_fault_handler);
add_interrupt_handler(INT_TSS , invalid_tss_handler);
add_interrupt_handler(INT_SEGMENT , invalid_segment_handler);
add_interrupt_handler(INT_STACK , invalid_stack_segment_handler);
add_interrupt_handler(INT_X87 , fpu_floating_point_exception_handler);
add_interrupt_handler(INT_ALIGNMENT , alignment_check_handler);
add_interrupt_handler(INT_SIMD , simd_floating_point_exception_handler);
add_interrupt_handler(INT_VIRTUALIZATION , virtualization_exception_handler);
}
void timer_handler(const int_registers *ir)
{
(void)ir;
timer.tick();
sched.get_current()->tick();
if (!(timer.ticks % TIME_SLICE))
{
interrupts_semaphore++; // if yield doesnt return (task switched) we won't have the push_ints from interrupt_handler
sched.yield();
interrupts_semaphore--; // if yield returns we get back to regular count (we will have the push_ints from interrupt_handlers)
}
}
void keyboard_handler(const int_registers *ir)
{
(void)ir;
term.kbd_ready_callback();
}
void divide_by_zero_handler(const int_registers *ir)
{
LOG(KERN_WARNING "division by zero @eip: 0x%x\n", ir->eip);
kill_me();
}
void debug_trap_handler(const int_registers *ir)
{
(void)ir;
LOG(KERN_INFO "debug interruption");
}
void nmi_handler(const int_registers *ir)
{
(void)ir;
PANIC("unknown hardware failure");
}
void breakpoint_handler(const int_registers *ir)
{
LOG(KERN_INFO "breakpoint hit @eip: 0x%x\n", ir->eip);
}
void overflow_handler(const int_registers *ir)
{
(void)ir;
LOG(KERN_WARNING "overflow\n");
kill_me();
}
void bound_check_fail_handler(const int_registers *ir)
{
LOG(KERN_WARNING "bound check failed @eip: 0x%x\n", ir->eip);
kill_me();
}
void invalid_opcode_handler(const int_registers *ir)
{
LOG(KERN_WARNING "invalid opcode @eip: 0x%x\n", ir->eip);
kill_me();
}
void non_available_feature_handler(const int_registers *ir)
{
LOG(KERN_WARNING "use of non supported/activated cpu feature @eip: 0x%x\n", ir->eip);
kill_me();
}
void double_fault_handler(const int_registers *ir)
{
(void)ir;
PANIC("double fault");
}
void invalid_tss_handler(const int_registers *ir)
{
LOG(KERN_WARNING "invalid task switch segment selector @eip: 0x%x\n", ir->eip);
}
void invalid_segment_handler(const int_registers *ir)
{
LOG(KERN_WARNING "invalid segment selector 0x%x @eip: 0x%x\n", ir->err_code, ir->eip);
}
void invalid_stack_segment_handler(const int_registers *ir)
{
LOG(KERN_WARNING "invalid stack segment selector 0x%x @eip: 0x%x\n", ir->err_code, ir->eip);
}
void general_protection_fault_handler(const int_registers *ir)
{
LOG(KERN_EMERG "general protection fault @eip: 0x%x\n", ir->eip);
kill_me();
}
void page_fault_handler(const int_registers *ir)
{
u32 fault;
asm volatile ("mov %0, cr2" : "=r"(fault)); // fault addr is in cr2
LOG(KERN_EMERG "page fault @0x%x %c%c%c%c%c\n", fault,
ir->err_code & 0x1 ? 'v' : 'p', // protection violation or non present page
ir->err_code & 0x2 ? 'w' : 'r', // fault caused by read or write
ir->err_code & 0x4 ? 'u' : 'k', // user or kernel
ir->err_code & 0x8 ? 'c' : '-', // accessed cpu bits of page (PSE or PAE enabled)
ir->err_code & 0x10 ? 'i' : '-' // caused by instruction (NX enabled)
);
kill_me();
}
void fpu_floating_point_exception_handler(const int_registers *ir)
{
LOG(KERN_EMERG "floating point exception @eip: 0x%x\n", ir->eip);
// TODO: check detailed error in x87 regs
kill_me();
}
void alignment_check_handler(const int_registers *ir)
{
LOG(KERN_EMERG "ailgnment check failed @eip: 0x%x\n", ir->eip);
kill_me();
}
void simd_floating_point_exception_handler(const int_registers *ir)
{
LOG(KERN_EMERG "SIMD floating point exception @eip: 0x%x\n", ir->eip);
// TODO: check detailed error in SSE reg MXCSR
kill_me();
}
void virtualization_exception_handler(const int_registers *ir)
{
LOG(KERN_EMERG "virtualization exception @eip: 0x%x\n", ir->eip);
kill_me();
}