db_exception.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_exception.c
  15. *
  16. * This file contains APIs to set up handlers of exceptions issued by the
  17. * host, and the methods to pass the exceptions to the upcalls.
  18. */
  19. #include "pal_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_error.h"
  23. #include "api.h"
  24. #include "list.h"
  25. #include "pal_debug.h"
  26. #include <errno.h>
  27. #define INIT_EVENT_HANDLER { .lock = LOCK_INIT }
  28. struct pal_event_handler {
  29. PAL_LOCK lock;
  30. PAL_EVENT_HANDLER upcall;
  31. };
  32. struct pal_event_handler handlers[] = {
  33. [PAL_EVENT_ARITHMETIC_ERROR] = INIT_EVENT_HANDLER,
  34. [PAL_EVENT_MEMFAULT] = INIT_EVENT_HANDLER,
  35. [PAL_EVENT_ILLEGAL] = INIT_EVENT_HANDLER,
  36. [PAL_EVENT_QUIT] = INIT_EVENT_HANDLER,
  37. [PAL_EVENT_SUSPEND] = INIT_EVENT_HANDLER,
  38. [PAL_EVENT_RESUME] = INIT_EVENT_HANDLER,
  39. [PAL_EVENT_FAILURE] = INIT_EVENT_HANDLER,
  40. };
  41. PAL_EVENT_HANDLER _DkGetExceptionHandler (PAL_NUM event)
  42. {
  43. struct pal_event_handler * eh = &handlers[event];
  44. _DkInternalLock(&eh->lock);
  45. PAL_EVENT_HANDLER upcall = eh->upcall;
  46. _DkInternalUnlock(&eh->lock);
  47. return upcall;
  48. }
  49. PAL_BOL
  50. DkSetExceptionHandler (PAL_EVENT_HANDLER handler, PAL_NUM event)
  51. {
  52. ENTER_PAL_CALL(DkSetExceptionHandler);
  53. if (!handler || event == 0 ||
  54. event > sizeof(handlers) / sizeof(handlers[0])) {
  55. _DkRaiseFailure(PAL_ERROR_INVAL);
  56. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  57. }
  58. struct pal_event_handler * eh = &handlers[event];
  59. _DkInternalLock(&eh->lock);
  60. eh->upcall = handler;
  61. _DkInternalUnlock(&eh->lock);
  62. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  63. }
  64. void DkExceptionReturn (PAL_PTR event)
  65. {
  66. _DkExceptionReturn(event);
  67. }
  68. /* This does not return */
  69. noreturn void __abort(void) {
  70. _DkProcessExit(-ENOTRECOVERABLE);
  71. }
  72. void warn (const char *format, ...)
  73. {
  74. va_list args;
  75. va_start (args, format);
  76. vprintf(format, args);
  77. va_end (args);
  78. }