-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.C
executable file
·174 lines (139 loc) · 5.29 KB
/
exceptions.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
/*
File: exceptions.C
Author: R. Bettati
Department of Computer Science
Texas A&M University
Date : 09/03/05
*/
/*--------------------------------------------------------------------------*/
/* DEFINES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* INCLUDES */
/*--------------------------------------------------------------------------*/
#include "assert.H"
#include "utils.H"
#include "console.H"
#include "idt.H"
#include "exceptions.H"
/*--------------------------------------------------------------------------*/
/* EXTERNS */
/*--------------------------------------------------------------------------*/
/* The low-level functions (defined in file 'IDT::low.s') that handle the
32 Intel-defined CPU exceptions.
These functions are actually merely stubs that put the error code and
the exception code on the stack and then call a low-level function, which
in turn calls the exception dispatcher (defined in 'exceptions.H').
Yes, there are more efficient ways to handle exceptions, but they require more
code replication.
*/
extern "C" void isr0();
extern "C" void isr1();
extern "C" void isr2();
extern "C" void isr3();
extern "C" void isr4();
extern "C" void isr5();
extern "C" void isr6();
extern "C" void isr7();
extern "C" void isr8();
extern "C" void isr9();
extern "C" void isr10();
extern "C" void isr11();
extern "C" void isr12();
extern "C" void isr13();
extern "C" void isr14();
extern "C" void isr15();
extern "C" void isr16();
extern "C" void isr17();
extern "C" void isr18();
extern "C" void isr19();
extern "C" void isr20();
extern "C" void isr21();
extern "C" void isr22();
extern "C" void isr23();
extern "C" void isr24();
extern "C" void isr25();
extern "C" void isr26();
extern "C" void isr27();
extern "C" void isr28();
extern "C" void isr29();
extern "C" void isr30();
extern "C" void isr31();
/*--------------------------------------------------------------------------*/
/* LOCAL VARIABLES */
/*--------------------------------------------------------------------------*/
static ExceptionHandler handler_table[EXCEPTION_TABLE_SIZE];
static ExceptionHandler default_handler;
/*--------------------------------------------------------------------------*/
/* EXPORTED EXCEPTION DISPATCHER FUNCTIONS */
/*--------------------------------------------------------------------------*/
void init_exception_dispatcher() {
/* -- INITIALIZE LOW-LEVEL EXCEPTION HANDLERS */
/* Add any new ISRs to the IDT here using IDT::set_gate */
IDT::set_gate( 0, (unsigned) isr0, 0x08, 0x8E);
IDT::set_gate( 1, (unsigned) isr1, 0x08, 0x8E);
IDT::set_gate( 2, (unsigned) isr2, 0x08, 0x8E);
IDT::set_gate( 3, (unsigned) isr3, 0x08, 0x8E);
IDT::set_gate( 4, (unsigned) isr4, 0x08, 0x8E);
IDT::set_gate( 5, (unsigned) isr5, 0x08, 0x8E);
IDT::set_gate( 6, (unsigned) isr6, 0x08, 0x8E);
IDT::set_gate( 7, (unsigned) isr7, 0x08, 0x8E);
IDT::set_gate( 8, (unsigned) isr8, 0x08, 0x8E);
IDT::set_gate( 9, (unsigned) isr9, 0x08, 0x8E);
IDT::set_gate(10, (unsigned)isr10, 0x08, 0x8E);
IDT::set_gate(11, (unsigned)isr11, 0x08, 0x8E);
IDT::set_gate(12, (unsigned)isr12, 0x08, 0x8E);
IDT::set_gate(13, (unsigned)isr13, 0x08, 0x8E);
IDT::set_gate(14, (unsigned)isr14, 0x08, 0x8E);
IDT::set_gate(15, (unsigned)isr15, 0x08, 0x8E);
IDT::set_gate(16, (unsigned)isr16, 0x08, 0x8E);
IDT::set_gate(17, (unsigned)isr17, 0x08, 0x8E);
IDT::set_gate(18, (unsigned)isr18, 0x08, 0x8E);
IDT::set_gate(19, (unsigned)isr19, 0x08, 0x8E);
IDT::set_gate(20, (unsigned)isr20, 0x08, 0x8E);
IDT::set_gate(21, (unsigned)isr21, 0x08, 0x8E);
IDT::set_gate(22, (unsigned)isr22, 0x08, 0x8E);
IDT::set_gate(23, (unsigned)isr23, 0x08, 0x8E);
IDT::set_gate(24, (unsigned)isr24, 0x08, 0x8E);
IDT::set_gate(25, (unsigned)isr25, 0x08, 0x8E);
IDT::set_gate(26, (unsigned)isr26, 0x08, 0x8E);
IDT::set_gate(27, (unsigned)isr27, 0x08, 0x8E);
IDT::set_gate(28, (unsigned)isr28, 0x08, 0x8E);
IDT::set_gate(29, (unsigned)isr29, 0x08, 0x8E);
IDT::set_gate(30, (unsigned)isr30, 0x08, 0x8E);
IDT::set_gate(31, (unsigned)isr31, 0x08, 0x8E);
/* -- INITIALIZE THE HIGH-LEVEL EXCEPTION HANDLER */
int i;
for(i = 0; i < EXCEPTION_TABLE_SIZE; i++) {
handler_table[i] = NULL;
}
default_handler = NULL;
}
void dispatch_exception(REGS * _r) {
/* -- EXCEPTION NUMBER */
unsigned int exc_no = _r->int_no;
Console::puts("EXCEPTION DISPATCHER: exc_no = ");
Console::putui(exc_no);
Console::puts("\n");
assert((exc_no >= 0) && (exc_no < EXCEPTION_TABLE_SIZE));
/* -- HAS A HANDLER BEEN REGISTERED FOR THIS EXCEPTION NO? */
ExceptionHandler handler = handler_table[exc_no];
if (!handler) {
/* --- NO DEFAULT HANDLER HAS BEEN REGISTERED. SIMPLY RETURN AN ERROR. */
Console::puts("NO DEFAULT EXCEPTION HANDLER REGISTERED\n");
abort();
}
else {
/* -- HANDLE THE EXCEPTION OR INTERRUPT */
handler(_r);
}
}
void register_exception_handler(unsigned int _isr_code,
ExceptionHandler _handler) {
assert(_isr_code >= 0 && _isr_code < EXCEPTION_TABLE_SIZE);
handler_table[_isr_code] = _handler;
Console::puts("Installed exception handler at ISR ");
Console::putui(_isr_code);
Console::puts("\n");
}