db_exception.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, .upcall = NULL }
  28. struct pal_event_handler {
  29. PAL_LOCK lock;
  30. PAL_EVENT_HANDLER upcall;
  31. };
  32. struct pal_event_handler handlers[] = {
  33. [0] = INIT_EVENT_HANDLER,
  34. [PAL_EVENT_ARITHMETIC_ERROR] = INIT_EVENT_HANDLER,
  35. [PAL_EVENT_MEMFAULT] = INIT_EVENT_HANDLER,
  36. [PAL_EVENT_ILLEGAL] = INIT_EVENT_HANDLER,
  37. [PAL_EVENT_QUIT] = INIT_EVENT_HANDLER,
  38. [PAL_EVENT_SUSPEND] = INIT_EVENT_HANDLER,
  39. [PAL_EVENT_RESUME] = INIT_EVENT_HANDLER,
  40. [PAL_EVENT_FAILURE] = INIT_EVENT_HANDLER,
  41. };
  42. PAL_EVENT_HANDLER _DkGetExceptionHandler(PAL_NUM event) {
  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. ENTER_PAL_CALL(DkSetExceptionHandler);
  52. if (!handler || event == 0 || event >= ARRAY_SIZE(handlers)) {
  53. _DkRaiseFailure(PAL_ERROR_INVAL);
  54. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  55. }
  56. struct pal_event_handler* eh = &handlers[event];
  57. _DkInternalLock(&eh->lock);
  58. eh->upcall = handler;
  59. _DkInternalUnlock(&eh->lock);
  60. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  61. }
  62. void DkExceptionReturn(PAL_PTR event) {
  63. _DkExceptionReturn(event);
  64. }
  65. /* This does not return */
  66. noreturn void __abort(void) {
  67. _DkProcessExit(-ENOTRECOVERABLE);
  68. }
  69. void warn(const char* format, ...) {
  70. va_list args;
  71. va_start(args, format);
  72. vprintf(format, args);
  73. va_end(args);
  74. }