+
+/*
+ * mono_aot_set_make_unreadable:
+ *
+ * Set whenever to make all mmaped memory unreadable. In conjuction with a
+ * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
+ */
+void
+mono_aot_set_make_unreadable (gboolean unreadable)
+{
+ static int inited;
+
+ make_unreadable = unreadable;
+
+ if (make_unreadable && !inited) {
+ mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
+ }
+}
+
+typedef struct {
+ MonoAotModule *module;
+ guint8 *ptr;
+} FindMapUserData;
+
+static void
+find_map (gpointer key, gpointer value, gpointer user_data)
+{
+ MonoAotModule *module = (MonoAotModule*)value;
+ FindMapUserData *data = (FindMapUserData*)user_data;
+
+ if (!data->module)
+ if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
+ data->module = module;
+}
+
+static MonoAotModule*
+find_module_for_addr (void *ptr)
+{
+ FindMapUserData data;
+
+ if (!make_unreadable)
+ return NULL;
+
+ data.module = NULL;
+ data.ptr = (guint8*)ptr;
+
+ mono_aot_lock ();
+ g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
+ mono_aot_unlock ();
+
+ return data.module;
+}
+
+/*
+ * mono_aot_is_pagefault:
+ *
+ * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
+ * within memory allocated by this module.
+ */
+gboolean
+mono_aot_is_pagefault (void *ptr)
+{
+ if (!make_unreadable)
+ return FALSE;
+
+ /*
+ * Not signal safe, but SIGSEGV's are synchronous, and
+ * this is only turned on by a MONO_DEBUG option.
+ */
+ return find_module_for_addr (ptr) != NULL;
+}
+
+/*
+ * mono_aot_handle_pagefault:
+ *
+ * Handle a pagefault caused by an unreadable page by making it readable again.
+ */
+void
+mono_aot_handle_pagefault (void *ptr)
+{
+#ifndef PLATFORM_WIN32
+ guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
+ int res;
+
+ mono_aot_lock ();
+ res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
+ g_assert (res == 0);
+
+ n_pagefaults ++;
+ mono_aot_unlock ();
+#endif
+}