POK(kernelpart)
exceptions.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
22 #if defined (POK_NEEDS_DEBUG) || defined (POK_NEEDS_ERROR_HANDLING)
23 
24 #include <errno.h>
25 #include <arch.h>
26 #include <core/debug.h>
27 #include <core/error.h>
28 #include <core/partition.h>
29 #include <libc.h>
30 #include "event.h"
31 
32 void exception_divide_error();
33 void exception_debug();
34 void exception_nmi();
35 void exception_breakpoint();
36 void exception_overflow();
37 void exception_boundrange();
38 void exception_invalidopcode();
39 void exception_nomath_coproc();
40 void exception_doublefault();
41 void exception_copseg_overrun();
42 void exception_invalid_tss();
43 void exception_segment_not_present();
44 void exception_stackseg_fault();
45 void exception_general_protection();
46 void exception_pagefault();
47 void exception_fpu_fault();
48 void exception_alignement_check();
49 void exception_machine_check();
50 void exception_simd_fault();
51 
52 static const struct
53 {
54  uint16_t vector;
55  void (*handler)(void);
56 }
57 exception_list[] =
58 {
59  { EXCEPTION_DIVIDE_ERROR, exception_divide_error},
60  { EXCEPTION_DEBUG, exception_debug},
61  { EXCEPTION_NMI, exception_nmi},
62  { EXCEPTION_BREAKPOINT, exception_breakpoint},
63  { EXCEPTION_OVERFLOW, exception_overflow},
64  { EXCEPTION_BOUNDRANGE, exception_boundrange},
65  { EXCEPTION_INVALIDOPCODE, exception_invalidopcode},
66  { EXCEPTION_NOMATH_COPROC, exception_nomath_coproc},
67  { EXCEPTION_DOUBLEFAULT, exception_doublefault},
68  { EXCEPTION_COPSEG_OVERRUN, exception_copseg_overrun},
69  { EXCEPTION_INVALID_TSS, exception_invalid_tss},
70  { EXCEPTION_SEGMENT_NOT_PRESENT, exception_segment_not_present},
71  { EXCEPTION_STACKSEG_FAULT, exception_stackseg_fault},
72  { EXCEPTION_GENERAL_PROTECTION, exception_general_protection},
73  { EXCEPTION_PAGEFAULT, exception_pagefault},
74  { EXCEPTION_FPU_FAULT, exception_fpu_fault},
75  { EXCEPTION_ALIGNEMENT_CHECK, exception_alignement_check},
76  { EXCEPTION_MACHINE_CHECK, exception_machine_check},
77  { EXCEPTION_SIMD_FAULT, exception_simd_fault},
78  { 0, NULL}
79 };
80 
82 {
83  int i;
84 
85  for (i = 0; exception_list[i].handler != NULL; ++i)
86  {
87  pok_idt_set_gate (exception_list[i].vector,
89  (uint32_t) exception_list[i].handler,
91  3);
92  }
93 
94  return (POK_ERRNO_OK);
95 }
96 
97 #if defined (POK_NEEDS_DEBUG) && ! defined (POK_NEEDS_ERROR_HANDLING)
98 static void dump_registers (interrupt_frame *frame)
99 {
100  printf ("ES: %x, DS: %x\n", frame->es, frame->ds);
101  printf ("CS: %x, SS: %x\n", frame->cs, frame->ss);
102  printf ("EDI: %x, ESI: %x\n", frame->edi, frame->esi);
103  printf ("EBP: %x, ESP: %x\n", frame->ebp, frame->esp);
104  printf ("EAX: %x, ECX: %x\n", frame->eax, frame->ecx);
105  printf ("EDX: %x, EBX: %x\n", frame->edx, frame->ebx);
106  printf ("EIP: %x, ErrorCode: %x\n", frame->eip, frame->error);
107  printf ("EFLAGS: %x\n\n", frame->eflags);
108 }
109 #endif
110 
111 INTERRUPT_HANDLER (exception_divide_error)
112 {
113  (void) frame;
114 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
115 
116 #ifdef POK_NEEDS_DEBUG
117  printf ("[KERNEL] Raise divide by zero error, current thread=%d\n", POK_SCHED_CURRENT_THREAD);
118 #endif
119 
120  pok_error_declare (POK_ERROR_KIND_NUMERIC_ERROR);
121  pok_sched_activate_error_thread ();
122 #else
123  pok_fatal ("Divide error");
124 #endif
125 }
126 
127 INTERRUPT_HANDLER (exception_debug)
128 {
129  (void)frame;
130 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
131 
132  #ifdef POK_NEEDS_DEBUG
133  printf ("[KERNEL] Raise debug fault\n");
134  #endif
135 
136  pok_error_declare (POK_ERROR_KIND_ILLEGAL_REQUEST);
137  pok_sched_activate_error_thread ();
138 #else
139 
140  #ifdef POK_NEEDS_DEBUG
141  dump_registers(frame);
142  pok_fatal ("Debug fault");
143  #endif
144 #endif
145 }
146 
147 INTERRUPT_HANDLER (exception_nmi)
148 {
149  (void)frame;
150 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
151 
152  #ifdef POK_NEEDS_DEBUG
153  printf ("[KERNEL] Raise exception NMI fault\n");
154  #endif
155 
156  pok_error_declare (POK_ERROR_KIND_ILLEGAL_REQUEST);
157  pok_sched_activate_error_thread ();
158 #else
159 
160  #ifdef POK_NEEDS_DEBUG
161  dump_registers(frame);
162  pok_fatal ("NMI Interrupt");
163  #endif
164 #endif
165 }
166 
167 INTERRUPT_HANDLER (exception_breakpoint)
168 {
169  (void)frame;
170 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
171 
172  #ifdef POK_NEEDS_DEBUG
173  printf ("[KERNEL] Raise exception breakpoint fault\n");
174  #endif
175 
176  pok_error_declare (POK_ERROR_KIND_ILLEGAL_REQUEST);
177  pok_sched_activate_error_thread ();
178 #else
179  #ifdef POK_NEEDS_DEBUG
180  dump_registers(frame);
181  pok_fatal ("Breakpoint");
182  #endif
183 #endif
184 }
185 
186 INTERRUPT_HANDLER (exception_overflow)
187 {
188  (void)frame;
189 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
190 
191  #ifdef POK_NEEDS_DEBUG
192  printf ("[KERNEL] Raise exception overflow fault\n");
193  #endif
194 
195  pok_error_declare (POK_ERROR_KIND_STACK_OVERFLOW);
196  pok_sched_activate_error_thread ();
197 #else
198  #ifdef POK_NEEDS_DEBUG
199  dump_registers(frame);
200  pok_fatal ("Overflow");
201  #endif
202 #endif
203 }
204 
205 INTERRUPT_HANDLER (exception_boundrange)
206 {
207  (void)frame;
208 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
209 
210  #ifdef POK_NEEDS_DEBUG
211  printf ("[KERNEL] Raise exception bound range fault\n");
212  #endif
213 
214  pok_error_declare (POK_ERROR_KIND_STACK_OVERFLOW);
215  pok_sched_activate_error_thread ();
216 #else
217  #ifdef POK_NEEDS_DEBUG
218  dump_registers(frame);
219  pok_fatal ("Bound range exceded");
220  #endif
221 #endif
222 }
223 
224 INTERRUPT_HANDLER (exception_invalidopcode)
225 {
226  (void)frame;
227 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
228 
229  #ifdef POK_NEEDS_DEBUG
230  printf ("[KERNEL] Raise exception invalid opcode fault, current thread: %d\n", POK_SCHED_CURRENT_THREAD);
231  #endif
232 
233  pok_error_declare (POK_ERROR_KIND_ILLEGAL_REQUEST);
234  pok_sched_activate_error_thread ();
235 #else
236  #ifdef POK_NEEDS_DEBUG
237  dump_registers(frame);
238  pok_fatal ("Invalid Opcode");
239  #endif
240 #endif
241 }
242 
243 INTERRUPT_HANDLER (exception_nomath_coproc)
244 {
245  (void)frame;
246 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
247 
248  #ifdef POK_NEEDS_DEBUG
249  printf ("[KERNEL] Raise exception no math coprocessor fault\n");
250  #endif
251 
252  pok_error_declare (POK_ERROR_KIND_ILLEGAL_REQUEST);
253  pok_sched_activate_error_thread ();
254 #else
255 
256  #ifdef POK_NEEDS_DEBUG
257  dump_registers(frame);
258  pok_fatal ("Invalid No Math Coprocessor");
259  #endif
260 
261 #endif
262 }
263 
264 INTERRUPT_HANDLER_errorcode (exception_doublefault)
265 {
266  (void)frame;
267 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
268 
269  #ifdef POK_NEEDS_DEBUG
270  printf ("[KERNEL] Raise exception double fault\n");
271  #endif
272 
273  pok_partition_error (POK_SCHED_CURRENT_PARTITION, POK_ERROR_KIND_PARTITION_HANDLER);
274 #else
275  #ifdef POK_NEEDS_DEBUG
276  dump_registers(frame);
277  pok_fatal ("Double Fault");
278  #endif
279 #endif
280 }
281 
282 INTERRUPT_HANDLER (exception_copseg_overrun)
283 {
284  (void)frame;
285 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
286 
287  #ifdef POK_NEEDS_DEBUG
288  printf ("[KERNEL] Raise exception copseg overrun fault\n");
289  #endif
290 
291  pok_error_declare (POK_ERROR_KIND_MEMORY_VIOLATION);
292  pok_sched_activate_error_thread ();
293 #else
294  #ifdef POK_NEEDS_DEBUG
295  dump_registers(frame);
296  pok_fatal ("Coprocessur Segment Overrun");
297  #endif
298 #endif
299 }
300 
301 INTERRUPT_HANDLER_errorcode (exception_invalid_tss)
302 {
303  (void)frame;
304 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
305 
306  #ifdef POK_NEEDS_DEBUG
307  printf ("[KERNEL] Raise exception invalid tss fault\n");
308  #endif
309 
310  pok_error_declare (POK_ERROR_KIND_MEMORY_VIOLATION);
311  pok_sched_activate_error_thread ();
312 #else
313  #ifdef POK_NEEDS_DEBUG
314  dump_registers(frame);
315  pok_fatal ("Invalid TSS");
316  #endif
317 #endif
318 }
319 
320 INTERRUPT_HANDLER_errorcode (exception_segment_not_present)
321 {
322  (void)frame;
323 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
324 
325  #ifdef POK_NEEDS_DEBUG
326  printf ("[KERNEL] Raise exception segment not present fault %d\n", POK_SCHED_CURRENT_THREAD);
327  #endif
328 
329  pok_error_declare (POK_ERROR_KIND_MEMORY_VIOLATION);
330  pok_sched_activate_error_thread ();
331 #else
332  #ifdef POK_NEEDS_DEBUG
333  dump_registers(frame);
334  pok_fatal ("Segment Not Present");
335  #endif
336 #endif
337 }
338 
339 INTERRUPT_HANDLER_errorcode (exception_stackseg_fault)
340 {
341  (void)frame;
342 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
343 
344  #ifdef POK_NEEDS_DEBUG
345  printf ("[KERNEL] Raise exception stack segment fault\n");
346  #endif
347 
348  pok_error_declare (POK_ERROR_KIND_MEMORY_VIOLATION);
349  pok_sched_activate_error_thread ();
350 #else
351  #ifdef POK_NEEDS_DEBUG
352  dump_registers(frame);
353  pok_fatal ("Stack-Segment Fault");
354  #endif
355 #endif
356 }
357 
358 INTERRUPT_HANDLER_errorcode (exception_general_protection)
359 {
360  (void)frame;
361 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
362 
363  #ifdef POK_NEEDS_DEBUG
364  printf ("[KERNEL] Raise exception general protection fault current thread=%d\n", POK_SCHED_CURRENT_THREAD);
365  #endif
366 
367  pok_error_declare (POK_ERROR_KIND_ILLEGAL_REQUEST);
368  pok_sched_activate_error_thread ();
369 #else
370  #ifdef POK_NEEDS_DEBUG
371  dump_registers(frame);
372  pok_fatal ("General Protection Fault");
373  #endif
374 #endif
375 }
376 
377 INTERRUPT_HANDLER_errorcode (exception_pagefault)
378 {
379  (void)frame;
380 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
381  #ifdef POK_NEEDS_DEBUG
382  printf ("[KERNEL] Raise exception pagefault fault\n");
383  #endif
384 
385  pok_error_declare (POK_ERROR_KIND_MEMORY_VIOLATION);
386  pok_sched_activate_error_thread ();
387 #else
388  #ifdef POK_NEEDS_DEBUG
389  dump_registers(frame);
390  pok_fatal ("Page Fault");
391  #endif
392 #endif
393 }
394 
395 INTERRUPT_HANDLER (exception_fpu_fault)
396 {
397  (void)frame;
398 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
399 
400  #ifdef POK_NEEDS_DEBUG
401  printf ("[KERNEL] Raise exception FPU fault\n");
402  #endif
403 
404  pok_error_declare (POK_ERROR_KIND_HARDWARE_FAULT);
405  pok_sched_activate_error_thread ();
406 #else
407  #ifdef POK_NEEDS_DEBUG
408  dump_registers(frame);
409  pok_fatal ("Floating Point Exception");
410  #endif
411 #endif
412 }
413 
414 INTERRUPT_HANDLER_errorcode (exception_alignement_check)
415 {
416  (void)frame;
417 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
418 
419  #ifdef POK_NEEDS_DEBUG
420  printf ("[KERNEL] Raise exception alignment fault\n");
421  #endif
422 
423  pok_error_declare (POK_ERROR_KIND_HARDWARE_FAULT);
424  pok_sched_activate_error_thread ();
425 #else
426  #ifdef POK_NEEDS_DEBUG
427  dump_registers(frame);
428  pok_fatal ("Bad alignement");
429  #endif
430 #endif
431 }
432 
433 INTERRUPT_HANDLER (exception_machine_check)
434 {
435  (void)frame;
436 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
437 
438  #ifdef POK_NEEDS_DEBUG
439  printf ("[KERNEL] Raise exception machine check fault\n");
440  #endif
441 
442  pok_error_declare (POK_ERROR_KIND_HARDWARE_FAULT);
443  pok_sched_activate_error_thread ();
444 #else
445  #ifdef POK_NEEDS_DEBUG
446  pok_fatal ("Machine check error");
447  dump_registers(frame);
448  #endif
449 #endif
450 }
451 
452 INTERRUPT_HANDLER (exception_simd_fault)
453 {
454  (void)frame;
455 #if defined (POK_NEEDS_PARTITIONS) && defined (POK_NEEDS_ERROR_HANDLING)
456 
457  #ifdef POK_NEEDS_DEBUG
458  printf ("[KERNEL] Raise exception SIMD fault\n");
459  #endif
460 
461  pok_error_declare (POK_ERROR_KIND_HARDWARE_FAULT);
462  pok_sched_activate_error_thread ();
463 #else
464 
465  #ifdef POK_NEEDS_DEBUG
466  dump_registers(frame);
467  pok_fatal ("SIMD Fault");
468  #endif
469 
470 #endif
471 }
472 
473 #endif
474