db_exception.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <errno.h>
  20. #include "api.h"
  21. #include "list.h"
  22. #include "pal.h"
  23. #include "pal_debug.h"
  24. #include "pal_defs.h"
  25. #include "pal_error.h"
  26. #include "pal_internal.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. struct pal_event_handler* eh = &handlers[event];
  43. _DkInternalLock(&eh->lock);
  44. PAL_EVENT_HANDLER upcall = eh->upcall;
  45. _DkInternalUnlock(&eh->lock);
  46. return upcall;
  47. }
  48. PAL_BOL
  49. DkSetExceptionHandler(PAL_EVENT_HANDLER handler, PAL_NUM event) {
  50. ENTER_PAL_CALL(DkSetExceptionHandler);
  51. if (!handler || event == 0 || event > sizeof(handlers) / sizeof(handlers[0])) {
  52. _DkRaiseFailure(PAL_ERROR_INVAL);
  53. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  54. }
  55. struct pal_event_handler* eh = &handlers[event];
  56. _DkInternalLock(&eh->lock);
  57. eh->upcall = handler;
  58. _DkInternalUnlock(&eh->lock);
  59. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  60. }
  61. void DkExceptionReturn(PAL_PTR event) {
  62. _DkExceptionReturn(event);
  63. }
  64. /* This does not return */
  65. noreturn void __abort(void) {
  66. _DkProcessExit(-ENOTRECOVERABLE);
  67. }
  68. void warn(const char* format, ...) {
  69. va_list args;
  70. va_start(args, format);
  71. vprintf(format, args);
  72. va_end(args);
  73. }