db_exception.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 "linux_list.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_DIVZERO] = 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, PAL_FLG flags)
  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. }