db_exception.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include <errno.h>
  29. #define INIT_EVENT_HANDLER { .lock = LOCK_INIT }
  30. struct pal_event_handler {
  31. PAL_LOCK lock;
  32. PAL_EVENT_HANDLER upcall;
  33. };
  34. struct pal_event_handler handlers[] = {
  35. [PAL_EVENT_ARITHMETIC_ERROR] = INIT_EVENT_HANDLER,
  36. [PAL_EVENT_MEMFAULT] = INIT_EVENT_HANDLER,
  37. [PAL_EVENT_ILLEGAL] = INIT_EVENT_HANDLER,
  38. [PAL_EVENT_QUIT] = INIT_EVENT_HANDLER,
  39. [PAL_EVENT_SUSPEND] = INIT_EVENT_HANDLER,
  40. [PAL_EVENT_RESUME] = INIT_EVENT_HANDLER,
  41. [PAL_EVENT_FAILURE] = INIT_EVENT_HANDLER,
  42. };
  43. PAL_EVENT_HANDLER _DkGetExceptionHandler (PAL_NUM event)
  44. {
  45. struct pal_event_handler * eh = &handlers[event];
  46. _DkInternalLock(&eh->lock);
  47. PAL_EVENT_HANDLER upcall = eh->upcall;
  48. _DkInternalUnlock(&eh->lock);
  49. return upcall;
  50. }
  51. PAL_BOL
  52. DkSetExceptionHandler (PAL_EVENT_HANDLER handler, PAL_NUM event)
  53. {
  54. ENTER_PAL_CALL(DkSetExceptionHandler);
  55. if (!handler || event == 0 ||
  56. event > sizeof(handlers) / sizeof(handlers[0])) {
  57. _DkRaiseFailure(PAL_ERROR_INVAL);
  58. LEAVE_PAL_CALL_RETURN(PAL_FALSE);
  59. }
  60. struct pal_event_handler * eh = &handlers[event];
  61. _DkInternalLock(&eh->lock);
  62. eh->upcall = handler;
  63. _DkInternalUnlock(&eh->lock);
  64. LEAVE_PAL_CALL_RETURN(PAL_TRUE);
  65. }
  66. void DkExceptionReturn (PAL_PTR event)
  67. {
  68. _DkExceptionReturn(event);
  69. }
  70. /* This does not return */
  71. noreturn void __abort(void) {
  72. _DkProcessExit(-ENOTRECOVERABLE);
  73. }
  74. void warn (const char *format, ...)
  75. {
  76. va_list args;
  77. va_start (args, format);
  78. vprintf(format, &args);
  79. va_end (args);
  80. }