hurdfault.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Handle faults in the signal thread.
  2. Copyright (C) 1994-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <hurd.h>
  16. #include <hurd/signal.h>
  17. #include "hurdfault.h"
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <setjmp.h>
  21. #include <stdio.h>
  22. #include <thread_state.h>
  23. #include "faultexc_server.h" /* mig-generated header for our exc server. */
  24. #include <assert.h>
  25. jmp_buf _hurdsig_fault_env;
  26. struct hurd_signal_preemptor _hurdsig_fault_preemptor = {0};
  27. /* XXX temporary to deal with spelling fix */
  28. weak_alias (_hurdsig_fault_preemptor, _hurdsig_fault_preempter)
  29. static mach_port_t forward_sigexc;
  30. kern_return_t
  31. _hurdsig_fault_catch_exception_raise (mach_port_t port,
  32. thread_t thread,
  33. task_t task,
  34. #ifdef EXC_MASK_ALL /* New interface flavor. */
  35. exception_type_t exception,
  36. exception_data_t code,
  37. mach_msg_type_number_t codeCnt
  38. #else /* Vanilla Mach 3.0 interface. */
  39. integer_t exception,
  40. integer_t code, integer_t subcode
  41. #endif
  42. )
  43. {
  44. int signo;
  45. struct hurd_signal_detail d;
  46. if (port != forward_sigexc ||
  47. thread != _hurd_msgport_thread || task != __mach_task_self ())
  48. return EPERM; /* Strange bogosity. */
  49. d.exc = exception;
  50. #ifdef EXC_MASK_ALL
  51. assert (codeCnt >= 2);
  52. d.exc_code = code[0];
  53. d.exc_subcode = code[1];
  54. #else
  55. d.exc_code = code;
  56. d.exc_subcode = subcode;
  57. #endif
  58. /* Call the machine-dependent function to translate the Mach exception
  59. codes into a signal number and subcode. */
  60. _hurd_exception2signal (&d, &signo);
  61. return HURD_PREEMPT_SIGNAL_P (&_hurdsig_fault_preemptor, signo, d.code)
  62. ? 0 : EGREGIOUS;
  63. }
  64. #ifdef EXC_MASK_ALL
  65. /* XXX New interface flavor has additional RPCs that we could be using
  66. instead. These RPCs roll a thread_get_state/thread_set_state into
  67. the message, so the signal thread ought to use these to save some calls.
  68. */
  69. kern_return_t
  70. _hurdsig_fault_catch_exception_raise_state
  71. (mach_port_t port,
  72. exception_type_t exception,
  73. exception_data_t code,
  74. mach_msg_type_number_t codeCnt,
  75. int *flavor,
  76. thread_state_t old_state,
  77. mach_msg_type_number_t old_stateCnt,
  78. thread_state_t new_state,
  79. mach_msg_type_number_t *new_stateCnt)
  80. {
  81. abort ();
  82. return KERN_FAILURE;
  83. }
  84. kern_return_t
  85. _hurdsig_fault_catch_exception_raise_state_identity
  86. (mach_port_t exception_port,
  87. thread_t thread,
  88. task_t task,
  89. exception_type_t exception,
  90. exception_data_t code,
  91. mach_msg_type_number_t codeCnt,
  92. int *flavor,
  93. thread_state_t old_state,
  94. mach_msg_type_number_t old_stateCnt,
  95. thread_state_t new_state,
  96. mach_msg_type_number_t *new_stateCnt)
  97. {
  98. abort ();
  99. return KERN_FAILURE;
  100. }
  101. #endif
  102. #ifdef NDR_CHAR_ASCII /* OSF Mach flavors have different names. */
  103. # define mig_reply_header_t mig_reply_error_t
  104. #endif
  105. static void
  106. faulted (void)
  107. {
  108. struct
  109. {
  110. mach_msg_header_t head;
  111. char buf[64];
  112. } request;
  113. mig_reply_header_t reply;
  114. extern int _hurdsig_fault_exc_server (mach_msg_header_t *,
  115. mach_msg_header_t *);
  116. /* Wait for the exception_raise message forwarded by the proc server. */
  117. if (__mach_msg (&request.head, MACH_RCV_MSG, 0,
  118. sizeof request, forward_sigexc,
  119. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)
  120. != MACH_MSG_SUCCESS)
  121. __libc_fatal ("msg receive failed on signal thread exc\n");
  122. /* Run the exc demuxer which should call the server function above.
  123. That function returns 0 if the exception was expected. */
  124. _hurdsig_fault_exc_server (&request.head, &reply.Head);
  125. if (reply.Head.msgh_remote_port != MACH_PORT_NULL)
  126. __mach_msg (&reply.Head, MACH_SEND_MSG, reply.Head.msgh_size,
  127. 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  128. if (reply.RetCode == MIG_BAD_ID)
  129. __mach_msg_destroy (&request.head);
  130. if (reply.RetCode)
  131. __libc_fatal ("BUG: unexpected fault in signal thread\n");
  132. _hurdsig_fault_preemptor.signals = 0;
  133. longjmp (_hurdsig_fault_env, 1);
  134. }
  135. static char faultstack[1024];
  136. /* Send exceptions for the signal thread to the proc server.
  137. It will forward the message on to our message port,
  138. and then restore the thread's state to code which
  139. does `longjmp (_hurd_sigthread_fault_env, 1)'. */
  140. void
  141. _hurdsig_fault_init (void)
  142. {
  143. error_t err;
  144. struct machine_thread_state state;
  145. mach_port_t sigexc;
  146. /* Allocate a port to receive signal thread exceptions.
  147. We will move this receive right to the proc server. */
  148. err = __mach_port_allocate (__mach_task_self (),
  149. MACH_PORT_RIGHT_RECEIVE, &sigexc);
  150. assert_perror (err);
  151. err = __mach_port_allocate (__mach_task_self (),
  152. MACH_PORT_RIGHT_RECEIVE, &forward_sigexc);
  153. assert_perror (err);
  154. /* Allocate a port to receive the exception msgs forwarded
  155. from the proc server. */
  156. err = __mach_port_insert_right (__mach_task_self (), sigexc,
  157. sigexc, MACH_MSG_TYPE_MAKE_SEND);
  158. assert_perror (err);
  159. /* Set the queue limit for this port to just one. The proc server will
  160. notice if we ever get a second exception while one remains queued and
  161. unreceived, and decide we are hopelessly buggy. */
  162. #ifdef MACH_PORT_RECEIVE_STATUS_COUNT
  163. {
  164. const mach_port_limits_t lim = { mpl_qlimit: 1 };
  165. assert (MACH_PORT_RECEIVE_STATUS_COUNT == sizeof lim / sizeof (natural_t));
  166. err = __mach_port_set_attributes (__mach_task_self (), forward_sigexc,
  167. MACH_PORT_RECEIVE_STATUS,
  168. (mach_port_info_t) &lim,
  169. MACH_PORT_RECEIVE_STATUS_COUNT);
  170. }
  171. #else
  172. err = __mach_port_set_qlimit (__mach_task_self (), forward_sigexc, 1);
  173. #endif
  174. assert_perror (err);
  175. /* This state will be restored when we fault.
  176. It runs the function above. */
  177. memset (&state, 0, sizeof state);
  178. MACHINE_THREAD_STATE_FIX_NEW (&state);
  179. MACHINE_THREAD_STATE_SET_PC (&state, faulted);
  180. MACHINE_THREAD_STATE_SET_SP (&state, faultstack, sizeof faultstack);
  181. err = __USEPORT
  182. (PROC,
  183. __proc_handle_exceptions (port,
  184. sigexc,
  185. forward_sigexc, MACH_MSG_TYPE_MAKE_SEND,
  186. MACHINE_THREAD_STATE_FLAVOR,
  187. (natural_t *) &state,
  188. MACHINE_THREAD_STATE_COUNT));
  189. assert_perror (err);
  190. /* Direct signal thread exceptions to the proc server. */
  191. #ifdef THREAD_EXCEPTION_PORT
  192. err = __thread_set_special_port (_hurd_msgport_thread,
  193. THREAD_EXCEPTION_PORT, sigexc);
  194. #elif defined (EXC_MASK_ALL)
  195. __thread_set_exception_ports (_hurd_msgport_thread,
  196. EXC_MASK_ALL & ~(EXC_MASK_SYSCALL
  197. | EXC_MASK_MACH_SYSCALL
  198. | EXC_MASK_RPC_ALERT),
  199. sigexc,
  200. EXCEPTION_STATE_IDENTITY,
  201. MACHINE_THREAD_STATE);
  202. #else
  203. # error thread_set_exception_ports?
  204. #endif
  205. __mach_port_deallocate (__mach_task_self (), sigexc);
  206. assert_perror (err);
  207. }