PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vm/mach_signal.cpp

http://github.com/slavapestov/factor
C++ | 231 lines | 137 code | 32 blank | 62 comment | 26 complexity | 1cc94ef13445c0902b98256ba6abeab1 MD5 | raw file
  1. /* Fault handler information. MacOSX version.
  2. Copyright (C) 1993-1999, 2002-2003 Bruno Haible <clisp.org at bruno>
  3. Copyright (C) 2003 Paolo Bonzini <gnu.org at bonzini>
  4. Used under BSD license with permission from Paolo Bonzini and Bruno Haible,
  5. 2005-03-10:
  6. http://sourceforge.net/mailarchive/message.php?msg_name=200503102200.32002.bruno%40clisp.org
  7. Modified for Factor by Slava Pestov */
  8. #include "master.hpp"
  9. namespace factor {
  10. /* The exception port on which our thread listens. */
  11. mach_port_t our_exception_port;
  12. /* The following sources were used as a *reference* for this exception handling
  13. code:
  14. 1. Apple's mach/xnu documentation
  15. 2. Timothy J. Wood's "Mach Exception Handlers 101" post to the
  16. omnigroup's macosx-dev list.
  17. http://www.wodeveloper.com/omniLists/macosx-dev/2000/June/msg00137.html */
  18. /* Modify a suspended thread's thread_state so that when the thread resumes
  19. executing, the call frame of the current C primitive (if any) is rewound, and
  20. the appropriate Factor error is thrown from the top-most Factor frame. */
  21. void factor_vm::call_fault_handler(exception_type_t exception,
  22. exception_data_type_t code,
  23. MACH_EXC_STATE_TYPE* exc_state,
  24. MACH_THREAD_STATE_TYPE* thread_state,
  25. MACH_FLOAT_STATE_TYPE* float_state) {
  26. cell handler = 0;
  27. if (exception == EXC_BAD_ACCESS) {
  28. signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state);
  29. signal_fault_pc = (cell)MACH_PROGRAM_COUNTER(thread_state);
  30. verify_memory_protection_error(signal_fault_addr);
  31. handler = (cell)factor::memory_signal_handler_impl;
  32. } else if (exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV) {
  33. signal_fpu_status = fpu_status(mach_fpu_status(float_state));
  34. mach_clear_fpu_status(float_state);
  35. handler = (cell)factor::fp_signal_handler_impl;
  36. } else {
  37. switch (exception) {
  38. case EXC_ARITHMETIC:
  39. signal_number = SIGFPE;
  40. break;
  41. case EXC_BAD_INSTRUCTION:
  42. signal_number = SIGILL;
  43. break;
  44. default:
  45. signal_number = SIGABRT;
  46. break;
  47. }
  48. handler = (cell)factor::synchronous_signal_handler_impl;
  49. }
  50. FACTOR_ASSERT(handler != 0);
  51. dispatch_signal_handler((cell*)&MACH_STACK_POINTER(thread_state),
  52. (cell*)&MACH_PROGRAM_COUNTER(thread_state),
  53. (cell)handler);
  54. }
  55. static void call_fault_handler(mach_port_t thread, exception_type_t exception,
  56. exception_data_type_t code,
  57. MACH_EXC_STATE_TYPE* exc_state,
  58. MACH_THREAD_STATE_TYPE* thread_state,
  59. MACH_FLOAT_STATE_TYPE* float_state) {
  60. /* Look up the VM instance involved */
  61. THREADHANDLE thread_id = pthread_from_mach_thread_np(thread);
  62. FACTOR_ASSERT(thread_id);
  63. std::map<THREADHANDLE, factor_vm*>::const_iterator vm =
  64. thread_vms.find(thread_id);
  65. /* Handle the exception */
  66. if (vm != thread_vms.end())
  67. vm->second->call_fault_handler(exception, code, exc_state, thread_state,
  68. float_state);
  69. }
  70. /* Handle an exception by invoking the user's fault handler and/or forwarding
  71. the duty to the previously installed handlers. */
  72. extern "C" kern_return_t catch_exception_raise(
  73. mach_port_t exception_port, mach_port_t thread, mach_port_t task,
  74. exception_type_t exception, exception_data_t code,
  75. mach_msg_type_number_t code_count) {
  76. /* 10.6 likes to report exceptions from child processes too. Ignore those */
  77. if (task != mach_task_self())
  78. return KERN_FAILURE;
  79. /* Get fault information and the faulting thread's register contents..
  80. See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html. */
  81. MACH_EXC_STATE_TYPE exc_state;
  82. mach_msg_type_number_t exc_state_count = MACH_EXC_STATE_COUNT;
  83. if (thread_get_state(thread, MACH_EXC_STATE_FLAVOR, (natural_t*)&exc_state,
  84. &exc_state_count) !=
  85. KERN_SUCCESS) {
  86. /* The thread is supposed to be suspended while the exception
  87. handler is called. This shouldn't fail. */
  88. return KERN_FAILURE;
  89. }
  90. MACH_THREAD_STATE_TYPE thread_state;
  91. mach_msg_type_number_t thread_state_count = MACH_THREAD_STATE_COUNT;
  92. if (thread_get_state(thread, MACH_THREAD_STATE_FLAVOR,
  93. (natural_t*)&thread_state, &thread_state_count) !=
  94. KERN_SUCCESS) {
  95. /* The thread is supposed to be suspended while the exception
  96. handler is called. This shouldn't fail. */
  97. return KERN_FAILURE;
  98. }
  99. MACH_FLOAT_STATE_TYPE float_state;
  100. mach_msg_type_number_t float_state_count = MACH_FLOAT_STATE_COUNT;
  101. if (thread_get_state(thread, MACH_FLOAT_STATE_FLAVOR,
  102. (natural_t*)&float_state, &float_state_count) !=
  103. KERN_SUCCESS) {
  104. /* The thread is supposed to be suspended while the exception
  105. handler is called. This shouldn't fail. */
  106. return KERN_FAILURE;
  107. }
  108. /* Modify registers so to have the thread resume executing the
  109. fault handler */
  110. call_fault_handler(thread, exception, code[0], &exc_state, &thread_state,
  111. &float_state);
  112. /* Set the faulting thread's register contents..
  113. See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html. */
  114. if (thread_set_state(thread, MACH_FLOAT_STATE_FLAVOR,
  115. (natural_t*)&float_state, float_state_count) !=
  116. KERN_SUCCESS) {
  117. return KERN_FAILURE;
  118. }
  119. if (thread_set_state(thread, MACH_THREAD_STATE_FLAVOR,
  120. (natural_t*)&thread_state, thread_state_count) !=
  121. KERN_SUCCESS) {
  122. return KERN_FAILURE;
  123. }
  124. return KERN_SUCCESS;
  125. }
  126. /* The main function of the thread listening for exceptions. */
  127. static void* mach_exception_thread(void* arg) {
  128. for (;;) {
  129. /* These two structures contain some private kernel data. We don't need
  130. to access any of it so we don't bother defining a proper struct. The
  131. correct definitions are in the xnu source code. */
  132. /* Buffer for a message to be received. */
  133. struct {
  134. mach_msg_header_t head;
  135. mach_msg_body_t msgh_body;
  136. char data[1024];
  137. } msg;
  138. /* Buffer for a reply message. */
  139. struct {
  140. mach_msg_header_t head;
  141. char data[1024];
  142. } reply;
  143. mach_msg_return_t retval;
  144. /* Wait for a message on the exception port. */
  145. retval =
  146. mach_msg(&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0, sizeof(msg),
  147. our_exception_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  148. if (retval != MACH_MSG_SUCCESS) {
  149. abort();
  150. }
  151. /* Handle the message: Call exc_server, which will call
  152. catch_exception_raise and produce a reply message. */
  153. exc_server(&msg.head, &reply.head);
  154. /* Send the reply. */
  155. if (mach_msg(&reply.head, MACH_SEND_MSG, reply.head.msgh_size, 0,
  156. MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL) !=
  157. MACH_MSG_SUCCESS) {
  158. abort();
  159. }
  160. }
  161. return NULL; // quiet warning
  162. }
  163. /* Initialize the Mach exception handler thread. */
  164. void mach_initialize() {
  165. mach_port_t self;
  166. exception_mask_t mask;
  167. self = mach_task_self();
  168. /* Allocate a port on which the thread shall listen for exceptions. */
  169. if (mach_port_allocate(self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port) !=
  170. KERN_SUCCESS)
  171. fatal_error("mach_port_allocate() failed", 0);
  172. /* See
  173. * http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html.
  174. */
  175. if (mach_port_insert_right(self, our_exception_port, our_exception_port,
  176. MACH_MSG_TYPE_MAKE_SEND) !=
  177. KERN_SUCCESS)
  178. fatal_error("mach_port_insert_right() failed", 0);
  179. /* The exceptions we want to catch. */
  180. mask = EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC;
  181. /* Create the thread listening on the exception port. */
  182. start_thread(mach_exception_thread, NULL);
  183. /* Replace the exception port info for these exceptions with our own.
  184. Note that we replace the exception port for the entire task, not only
  185. for a particular thread. This has the effect that when our exception
  186. port gets the message, the thread specific exception port has already
  187. been asked, and we don't need to bother about it. See
  188. http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html. */
  189. if (task_set_exception_ports(self, mask, our_exception_port,
  190. EXCEPTION_DEFAULT, MACHINE_THREAD_STATE) !=
  191. KERN_SUCCESS)
  192. fatal_error("task_set_exception_ports() failed", 0);
  193. }
  194. }