catch-exc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (C) 1994-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <mach/exc_server.h>
  15. #include <hurd/signal.h>
  16. #include <assert.h>
  17. /* Called by the microkernel when a thread gets an exception. */
  18. kern_return_t
  19. _S_catch_exception_raise (mach_port_t port,
  20. thread_t thread,
  21. task_t task,
  22. #ifdef EXC_MASK_ALL /* New interface flavor. */
  23. exception_type_t exception,
  24. exception_data_t code,
  25. mach_msg_type_number_t codeCnt
  26. #else /* Vanilla Mach 3.0 interface. */
  27. integer_t exception,
  28. integer_t code, long_integer_t subcode
  29. #endif
  30. )
  31. {
  32. error_t err;
  33. struct hurd_sigstate *ss;
  34. int signo;
  35. struct hurd_signal_detail d;
  36. if (task != __mach_task_self ())
  37. /* The sender wasn't the kernel. */
  38. return EPERM;
  39. d.exc = exception;
  40. #ifdef EXC_MASK_ALL
  41. assert (codeCnt >= 2);
  42. d.exc_code = code[0];
  43. d.exc_subcode = code[1];
  44. #else
  45. d.exc_code = code;
  46. d.exc_subcode = subcode;
  47. #endif
  48. /* Call the machine-dependent function to translate the Mach exception
  49. codes into a signal number and subcode. */
  50. _hurd_exception2signal (&d, &signo);
  51. /* Find the sigstate structure for the faulting thread. */
  52. ss = _hurd_thread_sigstate (thread);
  53. if (__spin_lock_locked (&ss->lock))
  54. {
  55. /* Loser. The thread faulted with its sigstate lock held. Its
  56. sigstate data is now suspect. So we reset the parts of it which
  57. could cause trouble for the signal thread. Anything else
  58. clobbered therein will just hose this user thread, but it's
  59. faulting already.
  60. This is almost certainly a library bug: unless random memory
  61. clobberation caused the sigstate lock to gratuitously appear held,
  62. no code should do anything that can fault while holding the
  63. sigstate lock. */
  64. __spin_unlock (&ss->critical_section_lock);
  65. ss->context = NULL;
  66. __spin_unlock (&ss->lock);
  67. }
  68. /* Post the signal. */
  69. _hurd_internal_post_signal (ss, signo, &d,
  70. MACH_PORT_NULL, MACH_MSG_TYPE_PORT_SEND,
  71. 0);
  72. err = __mach_port_deallocate (__mach_task_self (), task);
  73. assert_perror (err);
  74. err = __mach_port_deallocate (__mach_task_self (), thread);
  75. assert_perror (err);
  76. return KERN_SUCCESS;
  77. }
  78. #ifdef EXC_MASK_ALL
  79. /* XXX New interface flavor has additional RPCs that we could be using
  80. instead. These RPCs roll a thread_get_state/thread_set_state into
  81. the message, so the signal thread ought to use these to save some calls.
  82. */
  83. kern_return_t
  84. _S_catch_exception_raise_state (mach_port_t port,
  85. exception_type_t exception,
  86. exception_data_t code,
  87. mach_msg_type_number_t codeCnt,
  88. int *flavor,
  89. thread_state_t old_state,
  90. mach_msg_type_number_t old_stateCnt,
  91. thread_state_t new_state,
  92. mach_msg_type_number_t *new_stateCnt)
  93. {
  94. abort ();
  95. return KERN_FAILURE;
  96. }
  97. kern_return_t
  98. _S_catch_exception_raise_state_identity (mach_port_t exception_port,
  99. thread_t thread,
  100. task_t task,
  101. exception_type_t exception,
  102. exception_data_t code,
  103. mach_msg_type_number_t codeCnt,
  104. int *flavor,
  105. thread_state_t old_state,
  106. mach_msg_type_number_t old_stateCnt,
  107. thread_state_t new_state,
  108. mach_msg_type_number_t *new_stateCnt)
  109. {
  110. abort ();
  111. return KERN_FAILURE;
  112. }
  113. #endif