* Zoltan Varga (vargaz@gmail.com)
*
* Copyright 2009 Novell, Inc (http://www.novell.com)
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
*/
#include "config.h"
#include <mono/metadata/gc-internal.h>
//#if 0
-#ifdef HAVE_SGEN_GC
+#if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
#include <mono/metadata/gc-internal.h>
#include <mono/utils/mono-counters.h>
+#if SIZEOF_VOID_P == 4
+typedef guint32 mword;
+#else
+typedef guint64 mword;
+#endif
+
+#define SIZEOF_SLOT ((int)sizeof (mgreg_t))
+
+#define GC_BITS_PER_WORD (sizeof (mword) * 8)
+
/* Contains state needed by the GC Map construction code */
typedef struct {
/*
guint8 *reg_pin_bitmap;
} MonoCompileGC;
-#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
+#define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
+
+#undef DEBUG
#if 0
-#define DEBUG(s) do { s; } while (0)
+/* We don't support debug levels, its all-or-nothing */
+#define DEBUG(s) do { s; fflush (logfile); } while (0)
#define DEBUG_ENABLED 1
#else
#define DEBUG(s)
#endif
-#if 1
-#define DEBUG_GC_MAP(s) do { s; fflush (stdout); } while (0)
+#ifdef DEBUG_ENABLED
+//#if 1
+#define DEBUG_PRECISE(s) do { s; } while (0)
+#define DEBUG_PRECISE_ENABLED
#else
-#define DEBUG_GC_MAP(s)
+#define DEBUG_PRECISE(s)
#endif
-#define GC_BITS_PER_WORD (sizeof (gsize) * 8)
-
/*
* Contains information collected during the conservative stack marking pass,
* used during the precise pass. This helps to avoid doing a stack walk twice, which
int nreg_locations;
/* Relative to stack_start */
int reg_locations [MONO_MAX_IREGS];
-#ifdef DEBUG_ENABLED
+#ifdef DEBUG_PRECISE_ENABLED
MonoJitInfo *ji;
gpointer fp;
int regs [MONO_MAX_IREGS];
* only the collection thread is active.
*/
typedef struct {
- MonoLMF *lmf;
- MonoContext ctx;
- gboolean has_context;
- MonoJitTlsData *jit_tls;
+ MonoThreadUnwindState unwind_state;
+ MonoThreadInfo *info;
+ /* For debugging */
+ mgreg_t tid;
+ gpointer ref_to_track;
/* Number of frames collected during the !precise pass */
int nframes;
FrameInfo frames [MAX_FRAMES];
int scanned_precisely;
int scanned_conservatively;
int scanned_registers;
-
+ int scanned_native;
+ int scanned_other;
+
int all_slots;
int noref_slots;
int ref_slots;
static JITGCStats stats;
+static FILE *logfile;
+
+static gboolean enable_gc_maps_for_aot;
+
+void
+mini_gc_enable_gc_maps_for_aot (void)
+{
+ enable_gc_maps_for_aot = TRUE;
+}
+
// FIXME: Move these to a shared place
static inline void
return 0;
else if (frame_reg == AMD64_RBP)
return 1;
+#elif defined(TARGET_X86)
+ if (frame_reg == X86_EBP)
+ return 0;
+ else if (frame_reg == X86_ESP)
+ return 1;
+#elif defined(TARGET_ARM)
+ if (frame_reg == ARMREG_SP)
+ return 0;
+ else if (frame_reg == ARMREG_FP)
+ return 1;
+#elif defined(TARGET_S390X)
+ if (frame_reg == S390_SP)
+ return 0;
+ else if (frame_reg == S390_FP)
+ return 1;
#else
NOT_IMPLEMENTED;
#endif
return AMD64_RSP;
else if (encoded == 1)
return AMD64_RBP;
+#elif defined(TARGET_X86)
+ if (encoded == 0)
+ return X86_EBP;
+ else if (encoded == 1)
+ return X86_ESP;
+#elif defined(TARGET_ARM)
+ if (encoded == 0)
+ return ARMREG_SP;
+ else if (encoded == 1)
+ return ARMREG_FP;
+#elif defined(TARGET_S390X)
+ if (encoded == 0)
+ return S390_SP;
+ else if (encoded == 1)
+ return S390_FP;
#else
NOT_IMPLEMENTED;
#endif
#else
static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
#endif
+#elif defined(TARGET_X86)
+static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
+#elif defined(TARGET_ARM)
+static int callee_saved_regs [] = { ARMREG_V1, ARMREG_V2, ARMREG_V3, ARMREG_V4, ARMREG_V5, ARMREG_V7, ARMREG_FP };
+#elif defined(TARGET_S390X)
+static int callee_saved_regs [] = { s390_r6, s390_r7, s390_r8, s390_r9, s390_r10, s390_r11, s390_r12, s390_r13, s390_r14 };
#endif
static guint32
encode_regmask (guint32 regmask)
{
-#ifdef TARGET_AMD64
int i;
guint32 res;
}
g_assert (regmask == 0);
return res;
-#else
- NOT_IMPLEMENTED;
- return regmask;
-#endif
}
static guint32
decode_regmask (guint32 regmask)
{
-#ifdef TARGET_AMD64
int i;
guint32 res;
if (regmask & (1 << i))
res |= (1 << callee_saved_regs [i]);
return res;
-#else
- NOT_IMPLEMENTED;
- return regmask;
-#endif
}
/*
{
guint32 flags, freg;
- encode_sleb128 (map->start_offset / sizeof (mgreg_t), buf, &buf);
- encode_sleb128 (map->map_offset / sizeof (mgreg_t), buf, &buf);
+ encode_sleb128 (map->start_offset / SIZEOF_SLOT, buf, &buf);
+ encode_sleb128 (map->end_offset / SIZEOF_SLOT, buf, &buf);
+ encode_sleb128 (map->map_offset / SIZEOF_SLOT, buf, &buf);
encode_uleb128 (map->nslots, buf, &buf);
g_assert (map->callsite_entry_size <= 4);
freg = encode_frame_reg (map->frame_reg);
int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
int i, n;
- map->start_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
- map->map_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
+ map->start_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
+ map->end_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
+ map->map_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
map->nslots = decode_uleb128 (buf, &buf);
flags = decode_uleb128 (buf, &buf);
map->has_ref_slots = (flags & 1) ? 1 : 0;
TlsData *tls;
tls = g_new0 (TlsData, 1);
+ tls->tid = GetCurrentThreadId ();
+ tls->info = mono_thread_info_current ();
stats.tlsdata_size += sizeof (TlsData);
return tls;
}
static void
-thread_suspend_func (gpointer user_data, void *sigctx)
+thread_suspend_func (gpointer user_data, void *sigctx, MonoContext *ctx)
{
TlsData *tls = user_data;
- if (!tls)
+ if (!tls) {
/* Happens during startup */
+ tls->unwind_state.valid = FALSE;
return;
+ }
- tls->lmf = mono_get_lmf ();
- if (sigctx) {
- mono_arch_sigctx_to_monoctx (sigctx, &tls->ctx);
- tls->has_context = TRUE;
+ if (tls->tid != GetCurrentThreadId ()) {
+ /* Happens on osx because threads are not suspended using signals */
+ gboolean res;
+
+ g_assert (tls->info);
+ res = mono_thread_state_init_from_handle (&tls->unwind_state, (MonoNativeThreadId)tls->tid, tls->info->native_handle);
} else {
- tls->has_context = FALSE;
+ tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
+ if (sigctx) {
+ mono_arch_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx);
+ tls->unwind_state.valid = TRUE;
+ } else if (ctx) {
+ memcpy (&tls->unwind_state.ctx, ctx, sizeof (MonoContext));
+ tls->unwind_state.valid = TRUE;
+ } else {
+ tls->unwind_state.valid = FALSE;
+ }
+ tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS] = mono_native_tls_get_value (mono_jit_tls_id);
+ tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
+ }
+
+ if (!tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN]) {
+ /* Happens during startup */
+ tls->unwind_state.valid = FALSE;
+ return;
}
- tls->jit_tls = TlsGetValue (mono_jit_tls_id);
}
#define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
}
}
+static inline mgreg_t
+get_frame_pointer (MonoContext *ctx, int frame_reg)
+{
+#if defined(TARGET_AMD64)
+ if (frame_reg == AMD64_RSP)
+ return ctx->rsp;
+ else if (frame_reg == AMD64_RBP)
+ return ctx->rbp;
+#elif defined(TARGET_X86)
+ if (frame_reg == X86_ESP)
+ return ctx->esp;
+ else if (frame_reg == X86_EBP)
+ return ctx->ebp;
+#elif defined(TARGET_ARM)
+ if (frame_reg == ARMREG_SP)
+ return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
+ else if (frame_reg == ARMREG_FP)
+ return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
+#elif defined(TARGET_S390X)
+ if (frame_reg == S390_SP)
+ return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
+ else if (frame_reg == S390_FP)
+ return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
+#endif
+ g_assert_not_reached ();
+ return 0;
+}
+
/*
* conservatively_pass:
*
GCMap *map;
GCMap map_tmp;
GCEncodedMap *emap;
- guint8* fp, *p, *frame_start, *frame_end;
+ guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
int i, pc_offset, cindex, bitmap_width;
int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
gboolean res;
FrameInfo *fi;
guint32 precise_regmask;
+ if (tls) {
+ tls->nframes = 0;
+ tls->ref_to_track = NULL;
+ }
+
/* tls == NULL can happen during startup */
if (mono_thread_internal_current () == NULL || !tls) {
mono_gc_conservatively_scan_area (stack_start, stack_end);
return;
}
- lmf = tls->lmf;
+ lmf = tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF];
frame.domain = NULL;
/* Number of bytes scanned based on GC map data */
/* This is one past the last address which we have scanned */
stack_limit = stack_start;
- if (!tls->has_context)
+ if (!tls->unwind_state.valid)
memset (&new_ctx, 0, sizeof (ctx));
else
- memcpy (&new_ctx, &tls->ctx, sizeof (MonoContext));
+ memcpy (&new_ctx, &tls->unwind_state.ctx, sizeof (MonoContext));
memset (reg_locations, 0, sizeof (reg_locations));
memset (new_reg_locations, 0, sizeof (new_reg_locations));
- tls->nframes = 0;
-
while (TRUE) {
+ if (!tls->unwind_state.valid)
+ break;
+
memcpy (&ctx, &new_ctx, sizeof (ctx));
for (i = 0; i < MONO_MAX_IREGS; ++i) {
* we have to mark it conservatively.
*/
if (reg_locations [i]) {
- DEBUG (printf ("\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
- mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
- scanned_registers += sizeof (mgreg_t);
+ DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
+ scanned_registers += SIZEOF_SLOT;
}
reg_locations [i] = new_reg_locations [i];
- DEBUG (printf ("\treg %s is at location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
}
}
- g_assert ((guint64)stack_limit % sizeof (mgreg_t) == 0);
+ g_assert ((mgreg_t)stack_limit % SIZEOF_SLOT == 0);
-#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
- res = mono_find_jit_info_ext (frame.domain ? frame.domain : mono_domain_get (), tls->jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
+ res = mono_find_jit_info_ext (frame.domain ? frame.domain : tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN], tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS], NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
if (!res)
break;
-#else
- break;
-#endif
+
+ ji = frame.ji;
+
+ // FIXME: For skipped frames, scan the param area of the parent frame conservatively ?
+
+ if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
+ /*
+ * These frames are problematic for several reasons:
+ * - they are unwound through an LMF, and we have no precise register tracking for those.
+ * - the LMF might not contain a precise ip, so we can't compute the call site.
+ * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
+ */
+ DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
+ for (i = 0; i < MONO_MAX_IREGS; ++i) {
+ if (reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
+ scanned_registers += SIZEOF_SLOT;
+ }
+ reg_locations [i] = NULL;
+ new_reg_locations [i] = NULL;
+ }
+ ctx = new_ctx;
+ continue;
+ }
/* The last frame can be in any state so mark conservatively */
if (last) {
+ if (ji) {
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+ }
+ DEBUG (fprintf (logfile, "\t <Last frame>\n"));
last = FALSE;
+ /*
+ * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
+ */
+ for (i = 0; i < MONO_MAX_IREGS; ++i) {
+ if (reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
+ scanned_registers += SIZEOF_SLOT;
+ }
+ if (new_reg_locations [i]) {
+ DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), new_reg_locations [i]));
+ mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
+ scanned_registers += SIZEOF_SLOT;
+ }
+ reg_locations [i] = NULL;
+ new_reg_locations [i] = NULL;
+ }
continue;
}
- ji = frame.ji;
-
- /* This happens with native-to-managed transitions */
- if (!(MONO_CONTEXT_GET_IP (&ctx) >= ji->code_start && (guint8*)MONO_CONTEXT_GET_IP (&ctx) < (guint8*)ji->code_start + ji->code_size))
- continue;
+ pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
/* These frames are very problematic */
if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
- DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
- DEBUG (printf ("\tSkip.\n"));
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+ DEBUG (fprintf (logfile, "\tSkip.\n"));
continue;
}
* Can't save information since the array is full. So scan the rest of the
* stack conservatively.
*/
- DEBUG (printf ("Mark (0): Frame stack full.\n"));
+ DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
break;
}
* - spill area
* - localloc-ed memory
*/
- pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
g_assert (pc_offset >= 0);
emap = ji->gc_info;
if (!emap) {
- DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
- DEBUG (printf ("\tNo GC Map.\n"));
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+ DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
continue;
}
+ /* The embedded callsite table requires this */
+ g_assert (((mgreg_t)emap % 4) == 0);
+
/*
* Debugging aid to control the number of frames scanned precisely
*/
p += map->callsite_entry_size * map->ncallsites;
bitmaps = p;
-#ifdef __x86_64__
- if (map->frame_reg == AMD64_RSP)
- fp = (guint8*)ctx.rsp;
- else if (map->frame_reg == AMD64_RBP)
- fp = (guint8*)ctx.rbp;
- else
- g_assert_not_reached ();
-#else
- fp = NULL;
- g_assert_not_reached ();
-#endif
+ fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
+ real_frame_start = fp + map->start_offset;
frame_start = fp + map->start_offset + map->map_offset;
frame_end = fp + map->end_offset;
- DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(0): %s+0x%x (%p) limit=%p fp=%p frame=%p-%p (%d)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx), stack_limit, fp, frame_start, frame_end, (int)(frame_end - frame_start)); g_free (fname));
+ DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p) limit=%p fp=%p frame=%p-%p (%d)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx), stack_limit, fp, frame_start, frame_end, (int)(frame_end - frame_start)); g_free (fname));
/* Find the callsite index */
- if (ji->code_size < 256) {
+ if (map->callsite_entry_size == 1) {
for (i = 0; i < map->ncallsites; ++i)
/* ip points inside the call instruction */
if (map->callsites.offsets8 [i] == pc_offset + 1)
break;
- } else if (ji->code_size < 65536) {
+ } else if (map->callsite_entry_size == 2) {
// FIXME: Use a binary search
for (i = 0; i < map->ncallsites; ++i)
/* ip points inside the call instruction */
}
cindex = i;
- g_assert (frame_start >= stack_limit);
+ /*
+ * This is not neccessary true on x86 because frames have a different size at each
+ * call site.
+ */
+ //g_assert (real_frame_start >= stack_limit);
- if (frame_start > stack_limit) {
+ if (real_frame_start > stack_limit) {
/* This scans the previously skipped frames as well */
- DEBUG (printf ("\tscan area %p-%p.\n", stack_limit, frame_start));
- mono_gc_conservatively_scan_area (stack_limit, frame_start);
+ DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
+ mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
+ stats.scanned_other += real_frame_start - stack_limit;
}
/* Mark stack slots */
for (i = 0; i < map->nslots; ++i) {
pinned = pin_bitmap [i / 8] & (1 << (i % 8));
if (pinned) {
- DEBUG (printf ("\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
- mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
- scanned_conservatively += sizeof (mgreg_t);
+ DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
+ mono_gc_conservatively_scan_area (p, p + SIZEOF_SLOT);
+ scanned_conservatively += SIZEOF_SLOT;
} else {
- scanned_precisely += sizeof (mgreg_t);
+ scanned_precisely += SIZEOF_SLOT;
}
- p += sizeof (mgreg_t);
+ p += SIZEOF_SLOT;
}
} else {
- scanned_precisely += (map->nslots * sizeof (mgreg_t));
+ scanned_precisely += (map->nslots * SIZEOF_SLOT);
}
/* The area outside of start-end is NOREF */
- scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * sizeof (mgreg_t));
+ scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * SIZEOF_SLOT);
/* Mark registers */
precise_regmask = map->used_int_regs | (1 << map->frame_reg);
continue;
if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
- DEBUG (printf ("\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
+ DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
precise_regmask &= ~(1 << i);
}
bindex ++;
fi->bitmap = NULL;
fi->frame_start_offset = frame_start - stack_start;
fi->nreg_locations = 0;
- DEBUG (fi->ji = ji);
- DEBUG (fi->fp = fp);
+ DEBUG_PRECISE (fi->ji = ji);
+ DEBUG_PRECISE (fi->fp = fp);
if (map->has_ref_regs) {
int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
continue;
if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
- DEBUG (fi->regs [fi->nreg_locations] = i);
- DEBUG (printf ("\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
+ DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
+ DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
fi->nreg_locations ++;
}
}
/*
- * Clear locations of precisely stacked registers.
+ * Clear locations of precisely tracked registers.
*/
if (precise_regmask) {
for (i = 0; i < NREGS; ++i) {
* processed.
*/
if (reg_locations [i])
- DEBUG (printf ("\treg %s at location %p is precise.\n", mono_arch_regname (i), reg_locations [i]));
+ DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
reg_locations [i] = NULL;
}
}
/* Scan the remaining register save locations */
for (i = 0; i < MONO_MAX_IREGS; ++i) {
if (reg_locations [i]) {
- DEBUG (printf ("\tscan saved reg location %p.\n", reg_locations [i]));
- mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
- scanned_registers += sizeof (mgreg_t);
+ DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
+ mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
+ scanned_registers += SIZEOF_SLOT;
}
if (new_reg_locations [i]) {
- DEBUG (printf ("\tscan saved reg location %p.\n", new_reg_locations [i]));
- mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
- scanned_registers += sizeof (mgreg_t);
+ DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
+ mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
+ scanned_registers += SIZEOF_SLOT;
}
}
if (stack_limit < stack_end) {
- DEBUG (printf ("\tscan area %p-%p.\n", stack_limit, stack_end));
+ DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
mono_gc_conservatively_scan_area (stack_limit, stack_end);
+ stats.scanned_native += stack_end - stack_limit;
}
- DEBUG (printf ("Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
+ DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
stats.scanned_stacks += stack_end - stack_start;
stats.scanned += scanned;
if (!tls)
return;
+ if (!tls->unwind_state.valid)
+ return;
+
for (findex = 0; findex < tls->nframes; findex ++) {
/* Load information saved by the !precise pass */
fi = &tls->frames [findex];
frame_start = stack_start + fi->frame_start_offset;
+ DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
+
/*
* FIXME: Add a function to mark using a bitmap, to avoid doing a
* call for each object.
gboolean live;
for (i = 0; i < fi->nslots; ++i) {
- MonoObject **ptr = (MonoObject**)(frame_start + (i * sizeof (mgreg_t)));
+ MonoObject **ptr = (MonoObject**)(frame_start + (i * SIZEOF_SLOT));
live = ref_bitmap [i / 8] & (1 << (i % 8));
if (live) {
MonoObject *obj = *ptr;
if (obj) {
- DEBUG (printf ("\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
+ DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
*ptr = mono_gc_scan_object (obj);
- DEBUG (printf (" %p.\n", *ptr));
+ DEBUG (fprintf (logfile, " %p.\n", *ptr));
} else {
- DEBUG (printf ("\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
+ DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
}
} else {
#if 0
* Stack slots might be shared between ref and non-ref variables ?
*/
if (map->ref_slots [i / 8] & (1 << (i % 8))) {
- DEBUG (printf ("\tref %s0x%x(fp)=%p: dead (%p)\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, *ptr));
+ DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: dead (%p)\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, *ptr));
/*
* Fail fast if the live range is incorrect, and
* the JITted code tries to access this object
MonoObject *obj = *ptr;
if (obj) {
- DEBUG (printf ("\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
+ DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
*ptr = mono_gc_scan_object (obj);
- DEBUG (printf (" %p.\n", *ptr));
+ DEBUG (fprintf (logfile, " %p.\n", *ptr));
} else {
- DEBUG (printf ("\treg %s saved at %p: %p", mono_arch_regname (fi->regs [i]), ptr, obj));
+ DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
}
}
}
+
+ /*
+ * Debugging aid to check for missed refs.
+ */
+ if (tls->ref_to_track) {
+ mgreg_t *p;
+
+ for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
+ if (*p == (mgreg_t)tls->ref_to_track)
+ printf ("REF AT %p.\n", p);
+ }
}
/*
{
TlsData *tls = user_data;
- DEBUG (printf ("*** %s stack marking %p-%p ***\n", precise ? "Precise" : "Conservative", stack_start, stack_end));
+ DEBUG (fprintf (logfile, "****************************************\n"));
+ DEBUG (fprintf (logfile, "*** %s stack marking for thread %p (%p-%p) ***\n", precise ? "Precise" : "Conservative", tls ? GUINT_TO_POINTER (tls->tid) : NULL, stack_start, stack_end));
+ DEBUG (fprintf (logfile, "****************************************\n"));
if (!precise)
conservative_pass (tls, stack_start, stack_end);
if (COMPILE_LLVM (cfg))
return;
+ if (!mono_gc_is_moving ())
+ return;
+
+ if (cfg->compile_aot) {
+ if (!enable_gc_maps_for_aot)
+ return;
+ } else if (!mono_gc_precise_stack_mark_enabled ())
+ return;
+
#if 1
/* Debugging support */
{
if (!cfg->compute_gc_maps)
return;
- g_assert (slot_offset % sizeof (mgreg_t) == 0);
+ g_assert (slot_offset % SIZEOF_SLOT == 0);
gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
}
mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
{
MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
- int slot = - (slot_offset / sizeof (mgreg_t));
+ int slot = - (slot_offset / SIZEOF_SLOT);
if (!cfg->compute_gc_maps)
return;
g_assert (slot_offset <= 0);
- g_assert (slot_offset % sizeof (mgreg_t) == 0);
+ g_assert (slot_offset % SIZEOF_SLOT == 0);
gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
}
{
MonoCompileGC *gcfg = cfg->gc_info;
- return (offset - gcfg->min_offset) / sizeof (mgreg_t);
+ return (offset - gcfg->min_offset) / SIZEOF_SLOT;
}
static inline int
{
MonoCompileGC *gcfg = cfg->gc_info;
- return (slot * sizeof (mgreg_t)) + gcfg->min_offset;
+ return (slot * SIZEOF_SLOT) + gcfg->min_offset;
}
-static inline void
+static inline MONO_ALWAYS_INLINE void
set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
{
g_assert (slot >= 0 && slot < gcfg->nslots);
if (type == SLOT_PIN) {
- clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
- set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
+ set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
} else if (type == SLOT_REF) {
- set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
- clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
+ clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
} else if (type == SLOT_NOREF) {
- clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
- clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
+ clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
}
}
static inline void
set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
{
+ int width, pos;
+ guint8 *ref_bitmap, *pin_bitmap;
+
+ /*
int cindex;
for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
set_slot (gcfg, slot, cindex, type);
+ */
+ ref_bitmap = gcfg->stack_ref_bitmap;
+ pin_bitmap = gcfg->stack_pin_bitmap;
+ width = gcfg->stack_bitmap_width;
+ pos = width * slot;
+
+ if (type == SLOT_PIN) {
+ memset (ref_bitmap + pos, 0, width);
+ memset (pin_bitmap + pos, 0xff, width);
+ } else if (type == SLOT_REF) {
+ memset (ref_bitmap + pos, 0xff, width);
+ memset (pin_bitmap + pos, 0, width);
+ } else if (type == SLOT_NOREF) {
+ memset (ref_bitmap + pos, 0, width);
+ memset (pin_bitmap + pos, 0, width);
+ }
}
static inline void
g_assert (slot >= 0 && slot < gcfg->nregs);
if (type == SLOT_PIN) {
- clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
- set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
+ set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
} else if (type == SLOT_REF) {
- set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
- clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
+ clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
} else if (type == SLOT_NOREF) {
- clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
- clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+ clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
+ clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
}
}
* fp + cfa_offset == cfa
* -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
*/
- slot = (cfg->cfa_offset / sizeof (mgreg_t)) - cfa_slot - (gcfg->min_offset / sizeof (mgreg_t));
+ slot = (cfg->cfa_offset / SIZEOF_SLOT) - cfa_slot - (gcfg->min_offset / SIZEOF_SLOT);
set_slot_everywhere (gcfg, slot, type);
if (cfg->verbose_level > 1) {
int fp_offset = slot_to_fp_offset (cfg, slot);
if (type == SLOT_NOREF)
- printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * sizeof (mgreg_t)));
+ printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * SIZEOF_SLOT));
}
}
if (cfg->verbose_level > 1) {
if (type == SLOT_REF)
printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
+ else if (type == SLOT_NOREF)
+ printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset, slot);
}
}
}
+static gsize*
+get_vtype_bitmap (MonoType *t, int *numbits)
+{
+ MonoClass *klass = mono_class_from_mono_type (t);
+
+ if (klass->generic_container || mono_class_is_open_constructed_type (t)) {
+ /* FIXME: Generic sharing */
+ return NULL;
+ } else {
+ mono_class_compute_gc_descriptor (klass);
+
+ return mono_gc_get_bitmap_for_descr (klass->gc_descr, numbits);
+ }
+}
+
+static inline const char*
+get_offset_sign (int offset)
+{
+ return offset < 0 ? "-" : "+";
+}
+
+static inline int
+get_offset_val (int offset)
+{
+ return offset < 0 ? (- offset) : offset;
+}
+
static void
process_variables (MonoCompile *cfg)
{
int locals_max_offset = gcfg->locals_max_offset;
/* Slots for locals are NOREF by default */
- locals_min_slot = (locals_min_offset - gcfg->min_offset) / sizeof (mgreg_t);
- locals_max_slot = (locals_max_offset - gcfg->min_offset) / sizeof (mgreg_t);
+ locals_min_slot = (locals_min_offset - gcfg->min_offset) / SIZEOF_SLOT;
+ locals_max_slot = (locals_max_offset - gcfg->min_offset) / SIZEOF_SLOT;
for (i = locals_min_slot; i < locals_max_slot; ++i) {
set_slot_everywhere (gcfg, i, SLOT_NOREF);
}
gboolean byref, is_this = FALSE;
gboolean is_arg = i < cfg->locals_start;
- if (ins == cfg->ret)
- continue;
+ if (ins == cfg->ret) {
+ if (!(ins->opcode == OP_REGOFFSET && MONO_TYPE_ISSTRUCT (t)))
+ continue;
+ }
vmv = MONO_VARINFO (cfg, i);
hreg = ins->dreg;
g_assert (hreg < MONO_MAX_IREGS);
- // FIXME: Add back this check
-#if 0
- if (is_arg && gcfg->reg_live_intervals [hreg]) {
- /*
- * FIXME: This argument shares a hreg with a local, we can't add the whole
- * method as a live interval, since it would overlap with the locals
- * live interval.
- */
- continue;
- }
-#endif
-
if (byref)
slot_type = SLOT_PIN;
else
- slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
+ slot_type = mini_type_is_reference (cfg, t) ? SLOT_REF : SLOT_NOREF;
- if (is_arg) {
- /* Live for the whole method */
+ if (slot_type == SLOT_PIN) {
+ /* These have no live interval, be conservative */
set_reg_slot_everywhere (gcfg, hreg, slot_type);
} else {
- if (slot_type == SLOT_PIN) {
- /* These have no live interval, be conservative */
- set_reg_slot_everywhere (gcfg, hreg, slot_type);
- } else {
- /*
- * Unlike variables allocated to the stack, we generate liveness info
- * for noref vars in registers in mono_spill_global_vars (), because
- * knowing that a register doesn't contain a ref allows us to mark its save
- * locations precisely.
- */
- for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
- if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
- set_reg_slot (gcfg, hreg, cindex, slot_type);
- }
+ /*
+ * Unlike variables allocated to the stack, we generate liveness info
+ * for noref vars in registers in mono_spill_global_vars (), because
+ * knowing that a register doesn't contain a ref allows us to mark its save
+ * locations precisely.
+ */
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+ if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
+ set_reg_slot (gcfg, hreg, cindex, slot_type);
}
if (cfg->verbose_level > 1) {
if (ins->opcode != OP_REGOFFSET)
continue;
- if (ins->inst_offset % sizeof (mgreg_t) != 0)
- continue;
-
- if (is_arg && ins->inst_offset >= gcfg->max_offset)
- /* In parent frame */
+ if (ins->inst_offset % SIZEOF_SLOT != 0)
continue;
pos = fp_offset_to_slot (cfg, ins->inst_offset);
set_slot_everywhere (gcfg, pos, SLOT_NOREF);
if (cfg->verbose_level > 1) {
- printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "+", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
+ printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset), pos, mono_type_full_name (ins->inst_vtype));
}
continue;
}
size = mono_class_native_size (ins->klass, NULL);
else
size = mono_class_value_size (ins->klass, NULL);
- size_in_slots = ALIGN_TO (size, sizeof (mgreg_t)) / sizeof (mgreg_t);
+ size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
+
+ if (cfg->verbose_level > 1)
+ printf ("\tvtype R%d at %s0x%x(fp)-%s0x%x(fp) (slot %d-%d): %s\n", vmv->vreg, get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset), get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset + (size_in_slots * SIZEOF_SLOT)), pos, pos + size_in_slots, mono_type_full_name (ins->inst_vtype));
if (!ins->klass->has_references) {
if (is_arg) {
continue;
}
- if (ins->klass->generic_container || mono_class_is_open_constructed_type (t)) {
- /* FIXME: Generic sharing */
+ bitmap = get_vtype_bitmap (t, &numbits);
+ if (!bitmap)
pin = TRUE;
- } else {
- mono_class_compute_gc_descriptor (ins->klass);
-
- bitmap = mono_gc_get_bitmap_for_descr (ins->klass->gc_descr, &numbits);
- g_assert (bitmap);
- /*
- * Most vtypes are marked volatile because of the LDADDR instructions,
- * and they have no liveness information since they are decomposed
- * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
- * them during VZERO decomposition.
- */
+ /*
+ * Most vtypes are marked volatile because of the LDADDR instructions,
+ * and they have no liveness information since they are decomposed
+ * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
+ * them during VZERO decomposition.
+ */
+ if (!is_arg) {
if (!pc_offsets [vmv->vreg])
pin = TRUE;
- }
- if (ins->backend.is_pinvoke)
- pin = TRUE;
+ if (ins->backend.is_pinvoke)
+ pin = TRUE;
+ }
if (bitmap) {
- if (pc_offsets [vmv->vreg]) {
- for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
- if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
- for (j = 0; j < numbits; ++j) {
- if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
- /* The descriptor is for the boxed object */
- set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (gpointer))), cindex, pin ? SLOT_PIN : SLOT_REF);
- }
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+ if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
+ for (j = 0; j < numbits; ++j) {
+ if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
+ /* The descriptor is for the boxed object */
+ set_slot (gcfg, (pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, pin ? SLOT_PIN : SLOT_REF);
}
}
}
}
- } else if (pin) {
+
+ if (cfg->verbose_level > 1) {
+ for (j = 0; j < numbits; ++j) {
+ if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
+ printf ("\t\t%s slot at 0x%x(fp) (slot = %d)\n", pin ? "pin" : "ref", (int)(ins->inst_offset + (j * SIZEOF_SLOT)), (int)(pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)));
+ }
+ }
+ } else {
+ if (cfg->verbose_level > 1)
+ printf ("\t\tpinned\n");
for (j = 0; j < size_in_slots; ++j) {
set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
}
g_free (bitmap);
- if (cfg->verbose_level > 1) {
- printf ("\tvtype R%d at fp+0x%x-0x%x: %s\n", vmv->vreg, (int)ins->inst_offset, (int)(ins->inst_offset + (size / sizeof (mgreg_t))), mono_type_full_name (ins->inst_vtype));
- }
-
continue;
}
t = mini_type_get_underlying_type (NULL, t);
- if (!MONO_TYPE_IS_REFERENCE (t)) {
+ if (!mini_type_is_reference (cfg, t)) {
set_slot_everywhere (gcfg, pos, SLOT_NOREF);
if (cfg->verbose_level > 1)
- printf ("\tnoref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
+ printf ("\tnoref%s at %s0x%x(fp) (R%d, slot = %d): %s\n", (is_arg ? " arg" : ""), ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
+ if (!t->byref && sizeof (mgreg_t) == 4 && (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_R8)) {
+ set_slot_everywhere (gcfg, pos + 1, SLOT_NOREF);
+ if (cfg->verbose_level > 1)
+ printf ("\tnoref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)(ins->inst_offset + 4) : (int)ins->inst_offset + 4, vmv->vreg, pos + 1, mono_type_full_name (ins->inst_vtype));
+ }
continue;
}
}
if (cfg->verbose_level > 1) {
- printf ("\tref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
+ printf ("\tref%s at %s0x%x(fp) (R%d, slot = %d): %s\n", (is_arg ? " arg" : ""), ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
}
}
/* fp = sp + offset */
g_assert (cfg->frame_reg == AMD64_RBP);
return (- cfg->arch.sp_fp_offset + sp_offset);
+#elif defined(TARGET_X86)
+ /* The offset is computed from the sp at the start of the call sequence */
+ g_assert (cfg->frame_reg == X86_EBP);
+ return (- cfg->arch.sp_fp_offset - sp_offset);
#else
NOT_IMPLEMENTED;
return -1;
#endif
}
-static GCSlotType
-type_to_gc_slot_type (MonoType *t)
-{
- if (t->byref)
- return SLOT_PIN;
- t = mini_type_get_underlying_type (NULL, t);
- if (MONO_TYPE_IS_REFERENCE (t))
- return SLOT_REF;
- else {
- if (MONO_TYPE_ISSTRUCT (t)) {
- MonoClass *klass = mono_class_from_mono_type (t);
- if (!klass->has_references) {
- return SLOT_NOREF;
- } else {
- // FIXME:
- return SLOT_PIN;
- }
- }
- return SLOT_NOREF;
- }
-}
-
static void
process_param_area_slots (MonoCompile *cfg)
{
MonoCompileGC *gcfg = cfg->gc_info;
- int i;
+ int cindex, i;
gboolean *is_param;
/*
is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
- for (i = 0; i < gcfg->ncallsites; ++i) {
- GCCallSite *callsite = gcfg->callsites [i];
+ for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+ GCCallSite *callsite = gcfg->callsites [cindex];
GSList *l;
for (l = callsite->param_slots; l; l = l->next) {
MonoInst *def = l->data;
+ MonoType *t = def->inst_vtype;
int sp_offset = def->inst_offset;
int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
int slot = fp_offset_to_slot (cfg, fp_offset);
+ guint32 align;
+ guint32 size;
- g_assert (slot >= 0 && slot < gcfg->nslots);
- is_param [slot] = TRUE;
+ if (MONO_TYPE_ISSTRUCT (t)) {
+ size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE);
+ } else {
+ size = sizeof (mgreg_t);
+ }
+
+ for (i = 0; i < size / sizeof (mgreg_t); ++i) {
+ g_assert (slot + i >= 0 && slot + i < gcfg->nslots);
+ is_param [slot + i] = TRUE;
+ }
}
}
set_slot_everywhere (gcfg, i, SLOT_NOREF);
}
- for (i = 0; i < gcfg->ncallsites; ++i) {
- GCCallSite *callsite = gcfg->callsites [i];
- GSList *l;
-
- for (l = callsite->param_slots; l; l = l->next) {
- MonoInst *def = l->data;
- MonoType *t = def->inst_vtype;
- int sp_offset = def->inst_offset;
- int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
- int slot = fp_offset_to_slot (cfg, fp_offset);
- GCSlotType type = type_to_gc_slot_type (t);
-
- /* The slot is live between the def instruction and the call */
- set_slot_in_range (gcfg, slot, def->backend.pc_offset, callsite->pc_offset + 1, type);
- if (cfg->verbose_level > 1)
- printf ("\t%s param area slot at %s0x%x(fp)=0x%x(sp) (slot = %d) [0x%x-0x%x]\n", slot_type_to_string (type), fp_offset >= 0 ? "+" : "-", ABS (fp_offset), sp_offset, slot, def->backend.pc_offset, callsite->pc_offset + 1);
- }
- }
+ /*
+ * We treat param area slots as being part of the callee's frame, to be able to handle tail calls which overwrite
+ * the argument area of the caller.
+ */
}
static void
/* Compute min/max offsets from the fp */
/* Locals */
-#ifdef TARGET_AMD64
- locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, sizeof (mgreg_t));
+#if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
+ locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, SIZEOF_SLOT);
locals_max_offset = cfg->locals_max_stack_offset;
#else
/* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
NOT_IMPLEMENTED;
#endif
- locals_min_offset = ALIGN_TO (locals_min_offset, sizeof (mgreg_t));
- locals_max_offset = ALIGN_TO (locals_max_offset, sizeof (mgreg_t));
+ locals_min_offset = ALIGN_TO (locals_min_offset, SIZEOF_SLOT);
+ locals_max_offset = ALIGN_TO (locals_max_offset, SIZEOF_SLOT);
min_offset = locals_min_offset;
max_offset = locals_max_offset;
for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
MonoInst *ins = cfg->args [i];
- if (ins->opcode == OP_REGOFFSET)
+ if (ins->opcode == OP_REGOFFSET) {
+ int size, size_in_slots;
+ size = mini_type_stack_size_full (cfg->generic_sharing_context, ins->inst_vtype, NULL, ins->backend.is_pinvoke);
+ size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
+
min_offset = MIN (min_offset, ins->inst_offset);
+ max_offset = MAX ((int)max_offset, (int)(ins->inst_offset + (size_in_slots * SIZEOF_SLOT)));
+ }
}
/* Cfa slots */
/* Spill slots */
if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
- int stack_offset = ALIGN_TO (cfg->stack_offset, sizeof (mgreg_t));
+ int stack_offset = ALIGN_TO (cfg->stack_offset, SIZEOF_SLOT);
min_offset = MIN (min_offset, (-stack_offset));
}
/* Param area slots */
#ifdef TARGET_AMD64
min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
+#elif defined(TARGET_X86)
+ min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size));
+#elif defined(TARGET_ARM)
+ // FIXME:
+#elif defined(TARGET_s390X)
+ // FIXME:
+#else
+ NOT_IMPLEMENTED;
#endif
gcfg->min_offset = min_offset;
if (cfg->verbose_level > 1)
printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
- nslots = (gcfg->max_offset - gcfg->min_offset) / sizeof (mgreg_t);
+ nslots = (gcfg->max_offset - gcfg->min_offset) / SIZEOF_SLOT;
nregs = NREGS;
gcfg->nslots = nslots;
gcfg->nregs = nregs;
gcfg->callsites = callsites;
gcfg->ncallsites = ncallsites;
- gcfg->stack_bitmap_width = ALIGN_TO (nslots, 8) / 8;
- gcfg->reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
- gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
- gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
- gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
- gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
+ gcfg->stack_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
+ gcfg->reg_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
+ gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
+ gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
+ gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
+ gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
/* All slots start out as PIN */
- memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * ncallsites);
+ memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * nregs);
for (i = 0; i < nregs; ++i) {
/*
* By default, registers are NOREF.
}
}
+static inline gboolean
+has_bit_set (guint8 *bitmap, int width, int slot)
+{
+ int i;
+ int pos = width * slot;
+
+ for (i = 0; i < width; ++i) {
+ if (bitmap [pos + i])
+ break;
+ }
+ return i < width;
+}
+
static void
create_map (MonoCompile *cfg)
{
gboolean has_ref = FALSE;
gboolean has_pin = FALSE;
- for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
- has_pin = TRUE;
- if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
- has_ref = TRUE;
- }
+ if (has_bit_set (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i))
+ has_pin = TRUE;
+ if (has_bit_set (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i))
+ has_ref = TRUE;
if (has_ref)
has_ref_slots = TRUE;
if (!(cfg->used_int_regs & (1 << i)))
continue;
- for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i)) {
- has_ref = TRUE;
- break;
- }
- }
- for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i)) {
- has_pin = TRUE;
- break;
- }
- }
+ if (has_bit_set (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i))
+ has_pin = TRUE;
+ if (has_bit_set (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i))
+ has_ref = TRUE;
if (has_ref) {
reg_ref_mask |= (1 << i);
/* Create the GC Map */
+ /* The work bitmaps have one row for each slot, since this is how we access them during construction */
stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
stack_bitmap_size = stack_bitmap_width * ncallsites;
reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
map->frame_reg = cfg->frame_reg;
map->start_offset = gcfg->min_offset;
- map->end_offset = gcfg->min_offset + (nslots * sizeof (mgreg_t));
- map->map_offset = start * sizeof (mgreg_t);
+ map->end_offset = gcfg->min_offset + (nslots * SIZEOF_SLOT);
+ map->map_offset = start * SIZEOF_SLOT;
map->nslots = end - start;
map->has_ref_slots = has_ref_slots;
map->has_pin_slots = has_pin_slots;
bitmap = &bitmaps [map->stack_ref_bitmap_offset];
for (i = 0; i < nslots; ++i) {
for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
+ if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i, j))
set_bit (bitmap, stack_bitmap_width, j, i - start);
}
}
bitmap = &bitmaps [map->stack_pin_bitmap_offset];
for (i = 0; i < nslots; ++i) {
for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
+ if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i, j))
set_bit (bitmap, stack_bitmap_width, j, i - start);
}
}
for (i = 0; i < nregs; ++i) {
if (reg_ref_mask & (1 << i)) {
for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i))
+ if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i, j))
set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
}
bindex ++;
for (i = 0; i < nregs; ++i) {
if (reg_pin_mask & (1 << i)) {
for (j = 0; j < ncallsites; ++j) {
- if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i))
+ if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i, j))
set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
}
bindex ++;
p += encoded_size;
/* Callsite table */
- p = (guint8*)ALIGN_TO ((guint64)p, map->callsite_entry_size);
+ p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
if (map->callsite_entry_size == 1) {
guint8 *offsets = p;
for (i = 0; i < ncallsites; ++i)
/* Bitmaps */
memcpy (p, bitmaps, bitmaps_size);
+ p += bitmaps_size;
+
+ g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
stats.gc_maps_size += alloc_size;
stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
cfg->jit_info->gc_info = emap;
+
+ cfg->gc_map = (guint8*)emap;
+ cfg->gc_map_size = alloc_size;
}
stats.all_slots += nslots;
create_map (cfg);
}
+static void
+parse_debug_options (void)
+{
+ char **opts, **ptr;
+ char *env;
+
+ env = getenv ("MONO_GCMAP_DEBUG");
+ if (!env)
+ return;
+
+ opts = g_strsplit (env, ",", -1);
+ for (ptr = opts; ptr && *ptr; ptr ++) {
+ /* No options yet */
+ fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
+ exit (1);
+ }
+ g_strfreev (opts);
+}
+
void
mini_gc_init (void)
{
cb.thread_mark_func = thread_mark_func;
mono_gc_set_gc_callbacks (&cb);
+ logfile = mono_gc_get_logfile ();
+
+ parse_debug_options ();
+
mono_counters_register ("GC Maps size",
MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
mono_counters_register ("GC Call Sites size",
mono_counters_register ("Stack space scanned (all)",
MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
+ mono_counters_register ("Stack space scanned (native)",
+ MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
+ mono_counters_register ("Stack space scanned (other)",
+ MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
mono_counters_register ("Stack space scanned (using GC Maps)",
MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
mono_counters_register ("Stack space scanned (precise)",
* - vtypes/refs used in EH regions are treated conservatively
* - if the code is finished, less pinning will be done, causing problems because
* we promote all surviving objects to old-gen.
+ * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
+ * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
+ * This manifests in "Unable to find ip offset x in callsite list" assertions.
+ * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
+ * precise there.
*/
/*