Exception.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* This Hello World simply print out "Hello World" */
  4. #include "pal.h"
  5. #include "pal_debug.h"
  6. void handler1 (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  7. {
  8. pal_printf("Div-by-Zero Exception Handler 1: %p, rip = %p\n",
  9. arg, context->rip);
  10. while (*(unsigned char *) context->rip != 0x90)
  11. context->rip++;
  12. DkExceptionReturn(event);
  13. }
  14. void handler2 (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  15. {
  16. pal_printf("Div-by-Zero Exception Handler 2: %p, rip = %p\n",
  17. arg, context->rip);
  18. while (*(unsigned char *) context->rip != 0x90)
  19. context->rip++;
  20. DkExceptionReturn(event);
  21. }
  22. void handler3 (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  23. {
  24. pal_printf("Memory Fault Exception Handler: %p, rip = %p\n",
  25. arg, context->rip);
  26. while (*(unsigned char *) context->rip != 0x90)
  27. context->rip++;
  28. DkExceptionReturn(event);
  29. }
  30. int main (void)
  31. {
  32. volatile long i;
  33. DkSetExceptionHandler(handler1, PAL_EVENT_DIVZERO, 0);
  34. i = 0;
  35. i = 1 / i;
  36. asm volatile("nop");
  37. DkSetExceptionHandler(handler2, PAL_EVENT_DIVZERO, 0);
  38. i = 0;
  39. i = 1 / i;
  40. asm volatile("nop");
  41. DkSetExceptionHandler(handler3, PAL_EVENT_MEMFAULT, 0);
  42. *(volatile long *) 0x10000 = 0;
  43. asm volatile("nop");
  44. return 0;
  45. }