-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.c
222 lines (189 loc) · 3.99 KB
/
exception.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
#include "exception.h"
#include "lib.h"
#include "sys_handler.h"
/* Exception handlers */
#define PF_PROT_VIOL 0x00000001 //protection violation
#define PF_WR 0x00000002 //1 - write access caused fault
//0 - read access caused fault
#define PF_US 0x00000004 //1 - occured in user mode
//0 - occured in supervisor mode
#define PF_RSVD 0x00000008 //1 - some reserved bits set to 1
#define PF_ID 0x00000010 //1 - caused by an instruction fetch
#define DIE_BY_EXCEPTION 255
void exc_div0()
{
cli();
printf("Division by 0 exception\n");
int i;
for ( i = 0; i < 0x0FFFFFFF; i++ );
printf("Self destruction in 3... ");
for ( i = 0; i < 0x0FFFFFFF; i++ );
printf("2... ");
for ( i = 0; i < 0x0FFFFFFF; i++ );
printf("1... ");
for ( i = 0; i < 0x1FFFFFFF; i++ );
printf("Self destruct exception");
halt( DIE_BY_EXCEPTION );
asm volatile(".1: hlt; jmp .1;");
}
void exc_debug()
{
cli();
printf("Debug Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_nmi()
{
cli();
printf("NMI Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_breakpoint()
{
cli();
printf("Breakpoint Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_overflow()
{
cli();
printf("Overflow Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_bound_range()
{
cli();
printf("Bound Range Exceeded Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_opcode()
{
cli();
printf("Invalid Opcode Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_dev_not_avail()
{
cli();
printf("Device Not Available Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_dbl_fault()
{
cli();
printf("Double Fault Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_seg_overrun()
{
cli();
printf("Coprocessor segment overrun\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_invalid_tss()
{
cli();
printf("Invalid TSS Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_seg_not_present()
{
cli();
printf("Segment Not Present Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_stack_fault()
{
cli();
printf("Stack Fault Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_gen_protection()
{
cli();
uint32_t err_code;
asm volatile (
"POPL %0": "=r" (err_code)
);
printf("General Protection Exception\n");
asm volatile (
"popl %0": "=r" (err_code)
);
//printf("Error code is: 0x%x\n", err_code);
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_page_fault()
{
cli();
printf("Page Fault Exception\n");
//find page fault linear address
uint32_t cr2;
asm volatile (
"movl %%cr2, %0": "=r" (cr2)
);
printf( "Attempt to access linear address 0x%x failed\n", cr2 );
uint32_t err_code;
asm volatile(
"popl %%eax ;"
"movl %%eax, %0 ;"
: "=r" (err_code)
);
printf("Error code was: %d\n", err_code & 0x1F);
printf("Caused by:\n");
if ( err_code & PF_PROT_VIOL )
printf( " - protection violation\n");
else printf(" - non-present page\n");
if ( err_code & PF_WR )
printf( " - write access\n");
else printf(" - read access\n");
if ( err_code & PF_US )
printf( " - user\n");
else printf(" - supervisor\n");
if ( err_code & PF_RSVD )
printf( " - reserved bits set to 1\n" );
if ( err_code & PF_ID )
printf( " - instruction fetch\n" );
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_reserved()
{
cli();
printf("Got a reserved interrupt. Weird.\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_flt_point()
{
cli();
printf("Floating-Point Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_alignemnt()
{
cli();
printf("Alignment Check Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}
void exc_machine_check()
{
cli();
printf("Machine Check Exception\n");
halt( DIE_BY_EXCEPTION );
while(1);
}