db_exception.c 2.8 KB

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