Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / mini-darwin.c
blobae7ec123b9d823ddf58a4e87ea84911b7c13d49a
1 /*
2 * mini-darwin.c: Darwin/MacOS support for Mono.
4 * Authors:
5 * Mono Team (mono-list@lists.ximian.com)
7 * Copyright 2001-2003 Ximian, Inc.
8 * Copyright 2003-2008 Ximian, Inc.
10 * See LICENSE for licensing information.
12 #include <config.h>
13 #include <signal.h>
14 #ifdef HAVE_ALLOCA_H
15 #include <alloca.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #include <math.h>
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
25 #ifdef HAVE_VALGRIND_MEMCHECK_H
26 #include <valgrind/memcheck.h>
27 #endif
29 #include <mono/metadata/assembly.h>
30 #include <mono/metadata/loader.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/object.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/debug-helpers.h>
39 #include <mono/io-layer/io-layer.h>
40 #include "mono/metadata/profiler.h"
41 #include <mono/metadata/profiler-private.h>
42 #include <mono/metadata/mono-config.h>
43 #include <mono/metadata/environment.h>
44 #include <mono/metadata/mono-debug.h>
45 #include <mono/metadata/gc-internal.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/metadata/verify.h>
48 #include <mono/metadata/verify-internals.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/attach.h>
51 #include <mono/utils/mono-math.h>
52 #include <mono/utils/mono-compiler.h>
53 #include <mono/utils/mono-counters.h>
54 #include <mono/utils/mono-logger.h>
55 #include <mono/utils/mono-mmap.h>
56 #include <mono/utils/dtrace.h>
58 #include "mini.h"
59 #include <string.h>
60 #include <ctype.h>
61 #include "trace.h"
62 #include "version.h"
64 #include "jit-icalls.h"
66 /* MacOS includes */
67 #include <mach/mach.h>
68 #include <mach/mach_error.h>
69 #include <mach/exception.h>
70 #include <mach/task.h>
71 #include <pthread.h>
74 * This code disables the CrashReporter of MacOS X by installing
75 * a dummy Mach exception handler.
79 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
81 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
84 * The exception message
86 typedef struct {
87 mach_msg_base_t msg; /* common mach message header */
88 char payload [1024]; /* opaque */
89 } mach_exception_msg_t;
91 /* The exception port */
92 static mach_port_t mach_exception_port = VM_MAP_NULL;
95 * Implicitly called by exc_server. Must be public.
97 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
99 kern_return_t
100 catch_exception_raise (
101 mach_port_t exception_port,
102 mach_port_t thread,
103 mach_port_t task,
104 exception_type_t exception,
105 exception_data_t code,
106 mach_msg_type_number_t code_count)
108 /* consume the exception */
109 return KERN_FAILURE;
113 * Exception thread handler.
115 static
116 void *
117 mach_exception_thread (void *arg)
119 for (;;) {
120 mach_exception_msg_t request;
121 mach_exception_msg_t reply;
122 mach_msg_return_t result;
124 /* receive from "mach_exception_port" */
125 result = mach_msg (&request.msg.header,
126 MACH_RCV_MSG | MACH_RCV_LARGE,
128 sizeof (request),
129 mach_exception_port,
130 MACH_MSG_TIMEOUT_NONE,
131 MACH_PORT_NULL);
133 g_assert (result == MACH_MSG_SUCCESS);
135 /* dispatch to catch_exception_raise () */
136 exc_server (&request.msg.header, &reply.msg.header);
138 /* send back to sender */
139 result = mach_msg (&reply.msg.header,
140 MACH_SEND_MSG,
141 reply.msg.header.msgh_size,
143 MACH_PORT_NULL,
144 MACH_MSG_TIMEOUT_NONE,
145 MACH_PORT_NULL);
147 g_assert (result == MACH_MSG_SUCCESS);
149 return NULL;
152 static void
153 macosx_register_exception_handler ()
155 mach_port_t task;
156 pthread_attr_t attr;
157 pthread_t thread;
159 if (mach_exception_port != VM_MAP_NULL)
160 return;
162 task = mach_task_self ();
164 /* create the "mach_exception_port" with send & receive rights */
165 g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
166 &mach_exception_port) == KERN_SUCCESS);
167 g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
168 MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
170 /* create the exception handler thread */
171 g_assert (!pthread_attr_init (&attr));
172 g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
173 g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
174 pthread_attr_destroy (&attr);
177 * register "mach_exception_port" as a receiver for the
178 * EXC_BAD_ACCESS exception
180 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
182 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
183 mach_exception_port,
184 EXCEPTION_DEFAULT,
185 MACHINE_THREAD_STATE) == KERN_SUCCESS);
188 void
189 mono_runtime_install_handlers (void)
191 #if !defined(__arm__)
192 macosx_register_exception_handler ();
193 #endif
194 mono_runtime_posix_install_handlers ();