X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmini%2Fmini-gc.c;h=6c616a5f6f234db359eafe663c1173dce555000f;hb=b511edee10b6dc52d24f1f5fed1135ba340d8f71;hp=45c1b1a51e6ddb4ce5790bc32d0390c1f0a3b5ca;hpb=694ab7d8477ba306f75ee1cb71ec8acd87788f67;p=mono.git diff --git a/mono/mini/mini-gc.c b/mono/mini/mini-gc.c index 45c1b1a51e6..6c616a5f6f2 100644 --- a/mono/mini/mini-gc.c +++ b/mono/mini/mini-gc.c @@ -15,15 +15,10 @@ //#if 0 #if defined(MONO_ARCH_GC_MAPS_SUPPORTED) +#include #include #include -#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) @@ -124,6 +119,7 @@ typedef struct { */ typedef struct { MonoThreadUnwindState unwind_state; + MonoThreadInfo *info; /* For debugging */ mgreg_t tid; gpointer ref_to_track; @@ -261,6 +257,14 @@ 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 @@ -375,6 +379,11 @@ encode_frame_reg (int frame_reg) 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 @@ -400,6 +409,11 @@ decode_frame_reg (int encoded) 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 @@ -417,6 +431,13 @@ static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, 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_ARM64) +// FIXME: +static int callee_saved_regs [] = { }; +#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 }; +#elif defined(TARGET_POWERPC) +static int callee_saved_regs [] = { ppc_r6, ppc_r7, ppc_r8, ppc_r9, ppc_r10, ppc_r11, ppc_r12, ppc_r13, ppc_r14 }; #endif static guint32 @@ -548,6 +569,7 @@ thread_attach_func (void) tls = g_new0 (TlsData, 1); tls->tid = GetCurrentThreadId (); + tls->info = mono_thread_info_current (); stats.tlsdata_size += sizeof (TlsData); return tls; @@ -562,26 +584,51 @@ thread_detach_func (gpointer user_data) } 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 */ return; + } - // FIXME: This isn't true on osx, and we depend on it for mono_get_lmf (). - //g_assert (tls->tid == GetCurrentThreadId ()); + if (tls->tid != GetCurrentThreadId ()) { + /* Happens on osx because threads are not suspended using signals */ +#ifndef TARGET_WIN32 + gboolean res; +#endif - 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; + g_assert (tls->info); +#ifdef TARGET_WIN32 + return; +#else + res = mono_thread_state_init_from_handle (&tls->unwind_state, tls->info); +#endif } else { + tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf (); + if (sigctx) { +#ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX + mono_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx); + tls->unwind_state.valid = TRUE; +#else + tls->unwind_state.valid = FALSE; +#endif + } 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->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 (); } #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL) @@ -638,6 +685,11 @@ get_frame_pointer (MonoContext *ctx, int frame_reg) 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; @@ -652,6 +704,7 @@ static void conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) { MonoJitInfo *ji; + MonoMethod *method; MonoContext ctx, new_ctx; MonoLMF *lmf; guint8 *stack_limit; @@ -706,6 +759,9 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) memset (new_reg_locations, 0, sizeof (new_reg_locations)); while (TRUE) { + if (!tls->unwind_state.valid) + break; + memcpy (&ctx, &new_ctx, sizeof (ctx)); for (i = 0; i < MONO_MAX_IREGS; ++i) { @@ -735,6 +791,8 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) 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: @@ -756,21 +814,43 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) continue; } + if (ji) + method = jinfo_get_method (ji); + else + method = NULL; + /* 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 (char *fname = mono_method_full_name (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 \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; } 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); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname)); + if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) { + DEBUG (char *fname = mono_method_full_name (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; } @@ -801,7 +881,7 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) emap = ji->gc_info; if (!emap) { - 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 (char *fname = mono_method_full_name (jinfo_get_method (ji), 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; } @@ -813,14 +893,14 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) * Debugging aid to control the number of frames scanned precisely */ if (!precise_frame_limit_inited) { - if (getenv ("MONO_PRECISE_COUNT")) - precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT")); + if (g_getenv ("MONO_PRECISE_COUNT")) + precise_frame_limit = atoi (g_getenv ("MONO_PRECISE_COUNT")); precise_frame_limit_inited = TRUE; } if (precise_frame_limit != -1) { if (precise_frame_count [FALSE] == precise_frame_limit) - printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE)); + printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (method, TRUE)); if (precise_frame_count [FALSE] > precise_frame_limit) continue; } @@ -841,7 +921,7 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) 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); 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)); + DEBUG (char *fname = mono_method_full_name (jinfo_get_method (ji), 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 (map->callsite_entry_size == 1) { @@ -863,7 +943,7 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) break; } if (i == map->ncallsites) { - printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE)); + printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (method, TRUE)); g_assert_not_reached (); } cindex = i; @@ -966,7 +1046,7 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) } /* - * Clear locations of precisely stacked registers. + * Clear locations of precisely tracked registers. */ if (precise_regmask) { for (i = 0; i < NREGS; ++i) { @@ -1025,7 +1105,7 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) * pass. */ static void -precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) +precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end, void *gc_data) { int findex, i; FrameInfo *fi; @@ -1034,12 +1114,15 @@ precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) 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)); + DEBUG (char *fname = mono_method_full_name (jinfo_get_method (fi->ji), TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname)); /* * FIXME: Add a function to mark using a bitmap, to avoid doing a @@ -1060,7 +1143,7 @@ precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) MonoObject *obj = *ptr; if (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); + *ptr = mono_gc_scan_object (obj, gc_data); DEBUG (fprintf (logfile, " %p.\n", *ptr)); } else { DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj)); @@ -1101,7 +1184,7 @@ precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) if (obj) { DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj)); - *ptr = mono_gc_scan_object (obj); + *ptr = mono_gc_scan_object (obj, gc_data); DEBUG (fprintf (logfile, " %p.\n", *ptr)); } else { DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj)); @@ -1129,7 +1212,7 @@ precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end) * structure filled up by thread_suspend_func. */ static void -thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise) +thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise, void *gc_data) { TlsData *tls = user_data; @@ -1140,9 +1223,11 @@ thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gb if (!precise) conservative_pass (tls, stack_start, stack_end); else - precise_pass (tls, stack_start, stack_end); + precise_pass (tls, stack_start, stack_end, gc_data); } +#ifndef DISABLE_JIT + static void mini_gc_init_gc_map (MonoCompile *cfg) { @@ -1152,7 +1237,10 @@ mini_gc_init_gc_map (MonoCompile *cfg) if (!mono_gc_is_moving ()) return; - if (!cfg->compile_aot && !mono_gc_precise_stack_mark_enabled ()) + if (cfg->compile_aot) { + if (!enable_gc_maps_for_aot) + return; + } else if (!mono_gc_precise_stack_mark_enabled ()) return; #if 1 @@ -1161,10 +1249,10 @@ mini_gc_init_gc_map (MonoCompile *cfg) static int precise_count; precise_count ++; - if (getenv ("MONO_GCMAP_COUNT")) { - if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT"))) + if (g_getenv ("MONO_GCMAP_COUNT")) { + if (precise_count == atoi (g_getenv ("MONO_GCMAP_COUNT"))) printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE)); - if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT"))) + if (precise_count > atoi (g_getenv ("MONO_GCMAP_COUNT"))) return; } } @@ -1235,30 +1323,50 @@ slot_to_fp_offset (MonoCompile *cfg, int slot) 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 @@ -1279,14 +1387,14 @@ set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type 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); } } @@ -1579,10 +1687,6 @@ process_variables (MonoCompile *cfg) if (ins->inst_offset % SIZEOF_SLOT != 0) continue; - if (is_arg && ins->inst_offset >= gcfg->max_offset) - /* In parent frame */ - continue; - pos = fp_offset_to_slot (cfg, ins->inst_offset); if (is_arg && ins->flags & MONO_INST_IS_DEAD) { @@ -1629,11 +1733,13 @@ process_variables (MonoCompile *cfg) * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for * them during VZERO decomposition. */ - if (!pc_offsets [vmv->vreg]) - pin = TRUE; + 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) { for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) { @@ -1703,7 +1809,7 @@ process_variables (MonoCompile *cfg) 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) @@ -1741,7 +1847,7 @@ process_variables (MonoCompile *cfg) } 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)); } } @@ -1762,35 +1868,17 @@ sp_offset_to_fp_offset (MonoCompile *cfg, int 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); +#ifdef MONO_X86_NO_PUSHES + return (- cfg->arch.sp_fp_offset + sp_offset); +#else return (- cfg->arch.sp_fp_offset - sp_offset); +#endif #else NOT_IMPLEMENTED; return -1; #endif } -static GCSlotType -type_to_gc_slot_type (MonoCompile *cfg, MonoType *t) -{ - if (t->byref) - return SLOT_PIN; - t = mini_type_get_underlying_type (NULL, t); - if (mini_type_is_reference (cfg, 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) { @@ -1840,54 +1928,10 @@ process_param_area_slots (MonoCompile *cfg) set_slot_everywhere (gcfg, i, SLOT_NOREF); } - 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); - GCSlotType type = type_to_gc_slot_type (cfg, t); - - if (MONO_TYPE_ISSTRUCT (t)) { - guint32 align; - guint32 size; - int size_in_slots; - gsize *bitmap; - int j, numbits; - - size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE); - size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT; - - bitmap = get_vtype_bitmap (t, &numbits); - if (type == SLOT_NOREF || !bitmap) { - for (i = 0; i < size_in_slots; ++i) { - set_slot_in_range (gcfg, slot + i, def->backend.pc_offset, callsite->pc_offset + 1, type); - } - if (cfg->verbose_level > 1) - printf ("\t%s param area slots at %s0x%x(fp)=0x%x(sp) (slot = %d-%d) [0x%x-0x%x]\n", slot_type_to_string (type), get_offset_sign (fp_offset), get_offset_val (fp_offset), sp_offset, slot, slot + (size / (int)sizeof (mgreg_t)), def->backend.pc_offset, callsite->pc_offset + 1); - } else { - 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, (slot + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, SLOT_REF); - } - } - if (cfg->verbose_level > 1) - printf ("\tvtype param area slots at %s0x%x(fp)=0x%x(sp) (slot = %d-%d) [0x%x-0x%x]\n", get_offset_sign (fp_offset), get_offset_val (fp_offset), sp_offset, slot, slot + (size / (int)sizeof (mgreg_t)), def->backend.pc_offset, callsite->pc_offset + 1); - } - g_free (bitmap); - } else { - /* 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), get_offset_sign (fp_offset), get_offset_val (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 @@ -1960,7 +2004,7 @@ compute_frame_size (MonoCompile *cfg) /* Compute min/max offsets from the fp */ /* Locals */ -#if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) +#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 @@ -1978,8 +2022,14 @@ compute_frame_size (MonoCompile *cfg) 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 */ @@ -2009,9 +2059,15 @@ compute_frame_size (MonoCompile *cfg) #ifdef TARGET_AMD64 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset); #elif defined(TARGET_X86) +#ifdef MONO_X86_NO_PUSHES + min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset); +#else min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size)); +#endif #elif defined(TARGET_ARM) // FIXME: +#elif defined(TARGET_s390X) + // FIXME: #else NOT_IMPLEMENTED; #endif @@ -2072,15 +2128,15 @@ init_gcfg (MonoCompile *cfg) 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. @@ -2092,6 +2148,19 @@ init_gcfg (MonoCompile *cfg) } } +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) { @@ -2126,12 +2195,10 @@ 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; @@ -2171,18 +2238,10 @@ create_map (MonoCompile *cfg) 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); @@ -2201,6 +2260,7 @@ create_map (MonoCompile *cfg) /* 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; @@ -2237,7 +2297,7 @@ create_map (MonoCompile *cfg) 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); } } @@ -2249,7 +2309,7 @@ create_map (MonoCompile *cfg) 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); } } @@ -2263,7 +2323,7 @@ create_map (MonoCompile *cfg) 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 ++; @@ -2279,7 +2339,7 @@ create_map (MonoCompile *cfg) 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 ++; @@ -2393,13 +2453,15 @@ mini_gc_create_gc_map (MonoCompile *cfg) create_map (cfg); } +#endif /* DISABLE_JIT */ + static void parse_debug_options (void) { char **opts, **ptr; - char *env; + const char *env; - env = getenv ("MONO_GCMAP_DEBUG"); + env = g_getenv ("MONO_GCMAP_DEBUG"); if (!env) return; @@ -2474,11 +2536,18 @@ mini_gc_init (void) #else +void +mini_gc_enable_gc_maps_for_aot (void) +{ +} + void mini_gc_init (void) { } +#ifndef DISABLE_JIT + static void mini_gc_init_gc_map (MonoCompile *cfg) { @@ -2499,8 +2568,12 @@ mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType ty { } +#endif /* DISABLE_JIT */ + #endif +#ifndef DISABLE_JIT + /* * mini_gc_init_cfg: * @@ -2517,6 +2590,8 @@ mini_gc_init_cfg (MonoCompile *cfg) mini_gc_init_gc_map (cfg); } +#endif /* DISABLE_JIT */ + /* * Problems with the current code: * - the stack walk is slow