2 * mini-gc.c: GC interface for the mono JIT
5 * Zoltan Varga (vargaz@gmail.com)
7 * Copyright 2009 Novell, Inc (http://www.novell.com)
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 #include <mono/metadata/gc-internal.h>
16 #if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
18 #include <mono/metadata/gc-internal.h>
19 #include <mono/utils/mono-counters.h>
21 #if SIZEOF_VOID_P == 4
22 typedef guint32 mword;
24 typedef guint64 mword;
27 #define SIZEOF_SLOT ((int)sizeof (mgreg_t))
29 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
31 /* Contains state needed by the GC Map construction code */
34 * This contains information about stack slots initialized in the prolog, encoded using
35 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
36 * means cfa+0, 1 means cfa-4/8, etc.
38 GSList *stack_slots_from_cfa;
39 /* Same for stack slots relative to the frame pointer */
40 GSList *stack_slots_from_fp;
42 /* Number of slots in the map */
44 /* The number of registers in the map */
46 /* Min and Max offsets of the stack frame relative to fp */
47 int min_offset, max_offset;
48 /* Same for the locals area */
49 int locals_min_offset, locals_max_offset;
51 /* The call sites where this frame can be stopped during GC */
52 GCCallSite **callsites;
53 /* The number of call sites */
57 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
58 * runtime, since it includes columns which are 0.
60 int stack_bitmap_width;
62 * A bitmap whose width equals nslots, and whose height equals ncallsites.
63 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
66 guint8 *stack_ref_bitmap;
67 /* Same for SLOT_PIN */
68 guint8 *stack_pin_bitmap;
71 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
74 guint8 *reg_ref_bitmap;
75 guint8 *reg_pin_bitmap;
78 #define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
83 /* We don't support debug levels, its all-or-nothing */
84 #define DEBUG(s) do { s; fflush (logfile); } while (0)
85 #define DEBUG_ENABLED 1
92 #define DEBUG_PRECISE(s) do { s; } while (0)
93 #define DEBUG_PRECISE_ENABLED
95 #define DEBUG_PRECISE(s)
99 * Contains information collected during the conservative stack marking pass,
100 * used during the precise pass. This helps to avoid doing a stack walk twice, which
106 int frame_start_offset;
108 /* Relative to stack_start */
109 int reg_locations [MONO_MAX_IREGS];
110 #ifdef DEBUG_PRECISE_ENABLED
113 int regs [MONO_MAX_IREGS];
117 /* Max number of frames stored in the TLS data */
118 #define MAX_FRAMES 50
121 * Per-thread data kept by this module. This is stored in the GC and passed to us as
122 * parameters, instead of being stored in a TLS variable, since during a collection,
123 * only the collection thread is active.
126 MonoThreadUnwindState unwind_state;
127 MonoThreadInfo *info;
130 gpointer ref_to_track;
131 /* Number of frames collected during the !precise pass */
133 FrameInfo frames [MAX_FRAMES];
136 /* These are constant so don't store them in the GC Maps */
137 /* Number of registers stored in gc maps */
138 #define NREGS MONO_MAX_IREGS
142 * Contains information needed to mark a stack frame.
143 * This is a transient structure, created from a compressed representation on-demand.
147 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
148 * This includes memory which is NOREF thus doesn't need GC maps.
153 * The offset relative to frame_offset where the the memory described by the GC maps
157 /* The number of stack slots in the map */
159 /* The frame pointer register */
161 /* The size of each callsite table entry */
162 guint8 callsite_entry_size;
163 guint has_pin_slots : 1;
164 guint has_ref_slots : 1;
165 guint has_ref_regs : 1;
166 guint has_pin_regs : 1;
168 /* The offsets below are into an external bitmaps array */
171 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
172 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
175 guint32 stack_ref_bitmap_offset;
177 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
178 * different callsites, if the slot starts out as PIN, and later changes to REF.
180 guint32 stack_pin_bitmap_offset;
183 * Corresponding bitmaps for registers
184 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
185 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
187 guint32 reg_pin_bitmap_offset;
188 guint32 reg_ref_bitmap_offset;
190 guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
192 /* The number of bits set in the two masks above */
193 guint8 nref_regs, npin_regs;
196 * A bit array marking slots which contain refs.
197 * This is used only for debugging.
201 /* Callsite offsets */
202 /* These can take up a lot of space, so encode them compactly */
212 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
216 //guint8 encoded_size;
219 * The arrays below are embedded after the struct.
220 * Their address needs to be computed.
223 /* The fixed fields of the GCMap encoded using LEB128 */
224 guint8 encoded [MONO_ZERO_LEN_ARRAY];
226 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
227 guint8 callsites [MONO_ZERO_LEN_ARRAY];
230 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
233 static int precise_frame_count [2], precise_frame_limit = -1;
234 static gboolean precise_frame_limit_inited;
240 int scanned_precisely;
241 int scanned_conservatively;
242 int scanned_registers;
252 int gc_callsites_size;
253 int gc_callsites8_size;
254 int gc_callsites16_size;
255 int gc_callsites32_size;
257 int gc_map_struct_size;
261 static JITGCStats stats;
263 static FILE *logfile;
265 static gboolean enable_gc_maps_for_aot;
268 mini_gc_enable_gc_maps_for_aot (void)
270 enable_gc_maps_for_aot = TRUE;
273 // FIXME: Move these to a shared place
276 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
281 guint8 b = value & 0x7f;
283 if (value != 0) /* more bytes to come */
291 static G_GNUC_UNUSED void
292 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
295 gboolean negative = (value < 0);
303 /* the following is unnecessary if the
304 * implementation of >>= uses an arithmetic rather
305 * than logical shift for a signed left operand
309 value |= - (1 <<(size - 7));
310 /* sign bit of byte is second high order bit (0x40) */
311 if ((value == 0 && !(byte & 0x40)) ||
312 (value == -1 && (byte & 0x40)))
322 static inline guint32
323 decode_uleb128 (guint8 *buf, guint8 **endbuf)
333 res = res | (((int)(b & 0x7f)) << shift);
345 decode_sleb128 (guint8 *buf, guint8 **endbuf)
355 res = res | (((int)(b & 0x7f)) << shift);
358 if (shift < 32 && (b & 0x40))
359 res |= - (1 << shift);
370 encode_frame_reg (int frame_reg)
373 if (frame_reg == AMD64_RSP)
375 else if (frame_reg == AMD64_RBP)
377 #elif defined(TARGET_X86)
378 if (frame_reg == X86_EBP)
380 else if (frame_reg == X86_ESP)
382 #elif defined(TARGET_ARM)
383 if (frame_reg == ARMREG_SP)
385 else if (frame_reg == ARMREG_FP)
387 #elif defined(TARGET_S390X)
388 if (frame_reg == S390_SP)
390 else if (frame_reg == S390_FP)
395 g_assert_not_reached ();
400 decode_frame_reg (int encoded)
405 else if (encoded == 1)
407 #elif defined(TARGET_X86)
410 else if (encoded == 1)
412 #elif defined(TARGET_ARM)
415 else if (encoded == 1)
417 #elif defined(TARGET_S390X)
420 else if (encoded == 1)
425 g_assert_not_reached ();
431 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
433 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
435 #elif defined(TARGET_X86)
436 static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
437 #elif defined(TARGET_ARM)
438 static int callee_saved_regs [] = { ARMREG_V1, ARMREG_V2, ARMREG_V3, ARMREG_V4, ARMREG_V5, ARMREG_V7, ARMREG_FP };
439 #elif defined(TARGET_ARM64)
441 static int callee_saved_regs [] = { };
442 #elif defined(TARGET_S390X)
443 static int callee_saved_regs [] = { s390_r6, s390_r7, s390_r8, s390_r9, s390_r10, s390_r11, s390_r12, s390_r13, s390_r14 };
447 encode_regmask (guint32 regmask)
453 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
454 if (regmask & (1 << callee_saved_regs [i])) {
456 regmask -= (1 << callee_saved_regs [i]);
459 g_assert (regmask == 0);
464 decode_regmask (guint32 regmask)
470 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
471 if (regmask & (1 << i))
472 res |= (1 << callee_saved_regs [i]);
479 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
482 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
486 encode_sleb128 (map->start_offset / SIZEOF_SLOT, buf, &buf);
487 encode_sleb128 (map->end_offset / SIZEOF_SLOT, buf, &buf);
488 encode_sleb128 (map->map_offset / SIZEOF_SLOT, buf, &buf);
489 encode_uleb128 (map->nslots, buf, &buf);
490 g_assert (map->callsite_entry_size <= 4);
491 freg = encode_frame_reg (map->frame_reg);
493 flags = (map->has_ref_slots ? 1 : 0) | (map->has_pin_slots ? 2 : 0) | (map->has_ref_regs ? 4 : 0) | (map->has_pin_regs ? 8 : 0) | ((map->callsite_entry_size - 1) << 4) | (freg << 6);
494 encode_uleb128 (flags, buf, &buf);
495 encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
496 if (map->has_ref_regs)
497 encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
498 if (map->has_pin_regs)
499 encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
500 encode_uleb128 (map->ncallsites, buf, &buf);
508 * Decode the encoded GC map representation in BUF and store the result into MAP.
511 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
514 int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
517 map->start_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
518 map->end_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
519 map->map_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
520 map->nslots = decode_uleb128 (buf, &buf);
521 flags = decode_uleb128 (buf, &buf);
522 map->has_ref_slots = (flags & 1) ? 1 : 0;
523 map->has_pin_slots = (flags & 2) ? 1 : 0;
524 map->has_ref_regs = (flags & 4) ? 1 : 0;
525 map->has_pin_regs = (flags & 8) ? 1 : 0;
526 map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
528 map->frame_reg = decode_frame_reg (freg);
529 map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
530 if (map->has_ref_regs) {
531 map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
533 for (i = 0; i < NREGS; ++i)
534 if (map->reg_ref_mask & (1 << i))
538 if (map->has_pin_regs) {
539 map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
541 for (i = 0; i < NREGS; ++i)
542 if (map->reg_pin_mask & (1 << i))
546 map->ncallsites = decode_uleb128 (buf, &buf);
548 stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
549 reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
550 reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
552 map->stack_ref_bitmap_offset = offset;
553 if (map->has_ref_slots)
554 offset += stack_bitmap_size;
555 map->stack_pin_bitmap_offset = offset;
556 if (map->has_pin_slots)
557 offset += stack_bitmap_size;
558 map->reg_ref_bitmap_offset = offset;
559 if (map->has_ref_regs)
560 offset += reg_ref_bitmap_size;
561 map->reg_pin_bitmap_offset = offset;
562 if (map->has_pin_regs)
563 offset += reg_pin_bitmap_size;
569 thread_attach_func (void)
573 tls = g_new0 (TlsData, 1);
574 tls->tid = GetCurrentThreadId ();
575 tls->info = mono_thread_info_current ();
576 stats.tlsdata_size += sizeof (TlsData);
582 thread_detach_func (gpointer user_data)
584 TlsData *tls = user_data;
590 thread_suspend_func (gpointer user_data, void *sigctx, MonoContext *ctx)
592 TlsData *tls = user_data;
595 /* Happens during startup */
599 if (tls->tid != GetCurrentThreadId ()) {
600 /* Happens on osx because threads are not suspended using signals */
603 g_assert (tls->info);
607 res = mono_thread_state_init_from_handle (&tls->unwind_state, (MonoNativeThreadId)tls->tid, tls->info->native_handle);
610 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
612 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
613 mono_arch_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx);
614 tls->unwind_state.valid = TRUE;
616 tls->unwind_state.valid = FALSE;
619 memcpy (&tls->unwind_state.ctx, ctx, sizeof (MonoContext));
620 tls->unwind_state.valid = TRUE;
622 tls->unwind_state.valid = FALSE;
624 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS] = mono_native_tls_get_value (mono_jit_tls_id);
625 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
628 if (!tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN]) {
629 /* Happens during startup */
630 tls->unwind_state.valid = FALSE;
635 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
638 set_bit (guint8 *bitmap, int width, int y, int x)
640 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
644 clear_bit (guint8 *bitmap, int width, int y, int x)
646 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
650 get_bit (guint8 *bitmap, int width, int y, int x)
652 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
656 slot_type_to_string (GCSlotType type)
666 g_assert_not_reached ();
671 static inline mgreg_t
672 get_frame_pointer (MonoContext *ctx, int frame_reg)
674 #if defined(TARGET_AMD64)
675 if (frame_reg == AMD64_RSP)
677 else if (frame_reg == AMD64_RBP)
679 #elif defined(TARGET_X86)
680 if (frame_reg == X86_ESP)
682 else if (frame_reg == X86_EBP)
684 #elif defined(TARGET_ARM)
685 if (frame_reg == ARMREG_SP)
686 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
687 else if (frame_reg == ARMREG_FP)
688 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
689 #elif defined(TARGET_S390X)
690 if (frame_reg == S390_SP)
691 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
692 else if (frame_reg == S390_FP)
693 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
695 g_assert_not_reached ();
700 * conservatively_pass:
702 * Mark a thread stack conservatively and collect information needed by the precise pass.
705 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
709 MonoContext ctx, new_ctx;
712 gboolean last = TRUE;
716 guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
717 int i, pc_offset, cindex, bitmap_width;
718 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
720 StackFrameInfo frame;
721 mgreg_t *reg_locations [MONO_MAX_IREGS];
722 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
725 guint32 precise_regmask;
729 tls->ref_to_track = NULL;
732 /* tls == NULL can happen during startup */
733 if (mono_thread_internal_current () == NULL || !tls) {
734 mono_gc_conservatively_scan_area (stack_start, stack_end);
735 stats.scanned_stacks += stack_end - stack_start;
739 lmf = tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF];
742 /* Number of bytes scanned based on GC map data */
744 /* Number of bytes scanned precisely based on GC map data */
745 scanned_precisely = 0;
746 /* Number of bytes scanned conservatively based on GC map data */
747 scanned_conservatively = 0;
748 /* Number of bytes scanned conservatively in register save areas */
749 scanned_registers = 0;
751 /* This is one past the last address which we have scanned */
752 stack_limit = stack_start;
754 if (!tls->unwind_state.valid)
755 memset (&new_ctx, 0, sizeof (ctx));
757 memcpy (&new_ctx, &tls->unwind_state.ctx, sizeof (MonoContext));
759 memset (reg_locations, 0, sizeof (reg_locations));
760 memset (new_reg_locations, 0, sizeof (new_reg_locations));
763 if (!tls->unwind_state.valid)
766 memcpy (&ctx, &new_ctx, sizeof (ctx));
768 for (i = 0; i < MONO_MAX_IREGS; ++i) {
769 if (new_reg_locations [i]) {
771 * If the current frame saves the register, it means it might modify its
772 * value, thus the old location might not contain the same value, so
773 * we have to mark it conservatively.
775 if (reg_locations [i]) {
776 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
777 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
778 scanned_registers += SIZEOF_SLOT;
781 reg_locations [i] = new_reg_locations [i];
783 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
787 g_assert ((mgreg_t)stack_limit % SIZEOF_SLOT == 0);
789 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);
795 // FIXME: For skipped frames, scan the param area of the parent frame conservatively ?
797 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
799 * These frames are problematic for several reasons:
800 * - they are unwound through an LMF, and we have no precise register tracking for those.
801 * - the LMF might not contain a precise ip, so we can't compute the call site.
802 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
804 DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
805 for (i = 0; i < MONO_MAX_IREGS; ++i) {
806 if (reg_locations [i]) {
807 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
808 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
809 scanned_registers += SIZEOF_SLOT;
811 reg_locations [i] = NULL;
812 new_reg_locations [i] = NULL;
819 method = jinfo_get_method (ji);
823 /* The last frame can be in any state so mark conservatively */
826 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));
828 DEBUG (fprintf (logfile, "\t <Last frame>\n"));
831 * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
833 for (i = 0; i < MONO_MAX_IREGS; ++i) {
834 if (reg_locations [i]) {
835 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
836 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
837 scanned_registers += SIZEOF_SLOT;
839 if (new_reg_locations [i]) {
840 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), new_reg_locations [i]));
841 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
842 scanned_registers += SIZEOF_SLOT;
844 reg_locations [i] = NULL;
845 new_reg_locations [i] = NULL;
850 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
852 /* These frames are very problematic */
853 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
854 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));
855 DEBUG (fprintf (logfile, "\tSkip.\n"));
859 /* All the other frames are at a call site */
861 if (tls->nframes == MAX_FRAMES) {
863 * Can't save information since the array is full. So scan the rest of the
864 * stack conservatively.
866 DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
870 /* Scan the frame of this method */
873 * A frame contains the following:
878 * - localloc-ed memory
880 g_assert (pc_offset >= 0);
885 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));
886 DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
890 /* The embedded callsite table requires this */
891 g_assert (((mgreg_t)emap % 4) == 0);
894 * Debugging aid to control the number of frames scanned precisely
896 if (!precise_frame_limit_inited) {
897 if (g_getenv ("MONO_PRECISE_COUNT"))
898 precise_frame_limit = atoi (g_getenv ("MONO_PRECISE_COUNT"));
899 precise_frame_limit_inited = TRUE;
902 if (precise_frame_limit != -1) {
903 if (precise_frame_count [FALSE] == precise_frame_limit)
904 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (method, TRUE));
905 if (precise_frame_count [FALSE] > precise_frame_limit)
908 precise_frame_count [FALSE] ++;
910 /* Decode the encoded GC map */
912 memset (map, 0, sizeof (GCMap));
913 decode_gc_map (&emap->encoded [0], map, &p);
914 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
915 map->callsites.offsets8 = p;
916 p += map->callsite_entry_size * map->ncallsites;
919 fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
921 real_frame_start = fp + map->start_offset;
922 frame_start = fp + map->start_offset + map->map_offset;
923 frame_end = fp + map->end_offset;
925 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));
927 /* Find the callsite index */
928 if (map->callsite_entry_size == 1) {
929 for (i = 0; i < map->ncallsites; ++i)
930 /* ip points inside the call instruction */
931 if (map->callsites.offsets8 [i] == pc_offset + 1)
933 } else if (map->callsite_entry_size == 2) {
934 // FIXME: Use a binary search
935 for (i = 0; i < map->ncallsites; ++i)
936 /* ip points inside the call instruction */
937 if (map->callsites.offsets16 [i] == pc_offset + 1)
940 // FIXME: Use a binary search
941 for (i = 0; i < map->ncallsites; ++i)
942 /* ip points inside the call instruction */
943 if (map->callsites.offsets32 [i] == pc_offset + 1)
946 if (i == map->ncallsites) {
947 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (method, TRUE));
948 g_assert_not_reached ();
953 * This is not neccessary true on x86 because frames have a different size at each
956 //g_assert (real_frame_start >= stack_limit);
958 if (real_frame_start > stack_limit) {
959 /* This scans the previously skipped frames as well */
960 DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
961 mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
962 stats.scanned_other += real_frame_start - stack_limit;
965 /* Mark stack slots */
966 if (map->has_pin_slots) {
967 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
968 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
973 for (i = 0; i < map->nslots; ++i) {
974 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
976 DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
977 mono_gc_conservatively_scan_area (p, p + SIZEOF_SLOT);
978 scanned_conservatively += SIZEOF_SLOT;
980 scanned_precisely += SIZEOF_SLOT;
985 scanned_precisely += (map->nslots * SIZEOF_SLOT);
988 /* The area outside of start-end is NOREF */
989 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * SIZEOF_SLOT);
992 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
993 if (map->has_pin_regs) {
994 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
995 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
997 for (i = 0; i < NREGS; ++i) {
998 if (!(map->used_int_regs & (1 << i)))
1001 if (!(map->reg_pin_mask & (1 << i)))
1004 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
1005 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
1006 precise_regmask &= ~(1 << i);
1012 scanned += map->end_offset - map->start_offset;
1014 g_assert (scanned == scanned_precisely + scanned_conservatively);
1016 stack_limit = frame_end;
1018 /* Save information for the precise pass */
1019 fi = &tls->frames [tls->nframes];
1020 fi->nslots = map->nslots;
1021 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
1022 if (map->has_ref_slots)
1023 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
1026 fi->frame_start_offset = frame_start - stack_start;
1027 fi->nreg_locations = 0;
1028 DEBUG_PRECISE (fi->ji = ji);
1029 DEBUG_PRECISE (fi->fp = fp);
1031 if (map->has_ref_regs) {
1032 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
1033 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
1035 for (i = 0; i < NREGS; ++i) {
1036 if (!(map->reg_ref_mask & (1 << i)))
1039 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
1040 DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
1041 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
1042 fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
1043 fi->nreg_locations ++;
1050 * Clear locations of precisely tracked registers.
1052 if (precise_regmask) {
1053 for (i = 0; i < NREGS; ++i) {
1054 if (precise_regmask & (1 << i)) {
1056 * The method uses this register, and we have precise info for it.
1057 * This means the location will be scanned precisely.
1058 * Tell the code at the beginning of the loop that this location is
1061 if (reg_locations [i])
1062 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
1063 reg_locations [i] = NULL;
1071 /* Scan the remaining register save locations */
1072 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1073 if (reg_locations [i]) {
1074 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
1075 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
1076 scanned_registers += SIZEOF_SLOT;
1078 if (new_reg_locations [i]) {
1079 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
1080 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
1081 scanned_registers += SIZEOF_SLOT;
1085 if (stack_limit < stack_end) {
1086 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
1087 mono_gc_conservatively_scan_area (stack_limit, stack_end);
1088 stats.scanned_native += stack_end - stack_limit;
1091 DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
1093 stats.scanned_stacks += stack_end - stack_start;
1094 stats.scanned += scanned;
1095 stats.scanned_precisely += scanned_precisely;
1096 stats.scanned_conservatively += scanned_conservatively;
1097 stats.scanned_registers += scanned_registers;
1099 //mono_gc_conservatively_scan_area (stack_start, stack_end);
1105 * Mark a thread stack precisely based on information saved during the conservative
1109 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
1113 guint8 *frame_start;
1118 if (!tls->unwind_state.valid)
1121 for (findex = 0; findex < tls->nframes; findex ++) {
1122 /* Load information saved by the !precise pass */
1123 fi = &tls->frames [findex];
1124 frame_start = stack_start + fi->frame_start_offset;
1126 DEBUG (char *fname = mono_method_full_name (jinfo_get_method (fi->ji), TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
1129 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1130 * call for each object.
1133 /* Mark stack slots */
1135 guint8 *ref_bitmap = fi->bitmap;
1138 for (i = 0; i < fi->nslots; ++i) {
1139 MonoObject **ptr = (MonoObject**)(frame_start + (i * SIZEOF_SLOT));
1141 live = ref_bitmap [i / 8] & (1 << (i % 8));
1144 MonoObject *obj = *ptr;
1146 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1147 *ptr = mono_gc_scan_object (obj);
1148 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1150 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1155 * This is disabled because the pointer takes up a lot of space.
1156 * Stack slots might be shared between ref and non-ref variables ?
1158 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1159 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));
1161 * Fail fast if the live range is incorrect, and
1162 * the JITted code tries to access this object
1171 /* Mark registers */
1174 * Registers are different from stack slots, they have no address where they
1175 * are stored. Instead, some frame below this frame in the stack saves them
1176 * in its prolog to the stack. We can mark this location precisely.
1178 for (i = 0; i < fi->nreg_locations; ++i) {
1180 * reg_locations [i] contains the address of the stack slot where
1181 * a reg was last saved, so mark that slot.
1183 MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1184 MonoObject *obj = *ptr;
1187 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1188 *ptr = mono_gc_scan_object (obj);
1189 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1191 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1197 * Debugging aid to check for missed refs.
1199 if (tls->ref_to_track) {
1202 for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
1203 if (*p == (mgreg_t)tls->ref_to_track)
1204 printf ("REF AT %p.\n", p);
1211 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1212 * call, and TRUE at the second. USER_DATA points to a TlsData
1213 * structure filled up by thread_suspend_func.
1216 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
1218 TlsData *tls = user_data;
1220 DEBUG (fprintf (logfile, "****************************************\n"));
1221 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));
1222 DEBUG (fprintf (logfile, "****************************************\n"));
1225 conservative_pass (tls, stack_start, stack_end);
1227 precise_pass (tls, stack_start, stack_end);
1233 mini_gc_init_gc_map (MonoCompile *cfg)
1235 if (COMPILE_LLVM (cfg))
1238 if (!mono_gc_is_moving ())
1241 if (cfg->compile_aot) {
1242 if (!enable_gc_maps_for_aot)
1244 } else if (!mono_gc_precise_stack_mark_enabled ())
1248 /* Debugging support */
1250 static int precise_count;
1253 if (g_getenv ("MONO_GCMAP_COUNT")) {
1254 if (precise_count == atoi (g_getenv ("MONO_GCMAP_COUNT")))
1255 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1256 if (precise_count > atoi (g_getenv ("MONO_GCMAP_COUNT")))
1262 cfg->compute_gc_maps = TRUE;
1264 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1268 * mini_gc_set_slot_type_from_fp:
1270 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1271 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1272 * need to call this function for those slots.
1275 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1277 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1279 if (!cfg->compute_gc_maps)
1282 g_assert (slot_offset % SIZEOF_SLOT == 0);
1284 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1288 * mini_gc_set_slot_type_from_cfa:
1290 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1291 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1292 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1293 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1294 * call this function for those slots.
1297 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1299 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1300 int slot = - (slot_offset / SIZEOF_SLOT);
1302 if (!cfg->compute_gc_maps)
1305 g_assert (slot_offset <= 0);
1306 g_assert (slot_offset % SIZEOF_SLOT == 0);
1308 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1312 fp_offset_to_slot (MonoCompile *cfg, int offset)
1314 MonoCompileGC *gcfg = cfg->gc_info;
1316 return (offset - gcfg->min_offset) / SIZEOF_SLOT;
1320 slot_to_fp_offset (MonoCompile *cfg, int slot)
1322 MonoCompileGC *gcfg = cfg->gc_info;
1324 return (slot * SIZEOF_SLOT) + gcfg->min_offset;
1327 static inline MONO_ALWAYS_INLINE void
1328 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1330 g_assert (slot >= 0 && slot < gcfg->nslots);
1332 if (type == SLOT_PIN) {
1333 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1334 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1335 } else if (type == SLOT_REF) {
1336 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1337 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1338 } else if (type == SLOT_NOREF) {
1339 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1340 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1345 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1348 guint8 *ref_bitmap, *pin_bitmap;
1353 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1354 set_slot (gcfg, slot, cindex, type);
1356 ref_bitmap = gcfg->stack_ref_bitmap;
1357 pin_bitmap = gcfg->stack_pin_bitmap;
1358 width = gcfg->stack_bitmap_width;
1361 if (type == SLOT_PIN) {
1362 memset (ref_bitmap + pos, 0, width);
1363 memset (pin_bitmap + pos, 0xff, width);
1364 } else if (type == SLOT_REF) {
1365 memset (ref_bitmap + pos, 0xff, width);
1366 memset (pin_bitmap + pos, 0, width);
1367 } else if (type == SLOT_NOREF) {
1368 memset (ref_bitmap + pos, 0, width);
1369 memset (pin_bitmap + pos, 0, width);
1374 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1378 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1379 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1380 if (callsite_offset >= from && callsite_offset < to)
1381 set_slot (gcfg, slot, cindex, type);
1386 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1388 g_assert (slot >= 0 && slot < gcfg->nregs);
1390 if (type == SLOT_PIN) {
1391 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1392 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1393 } else if (type == SLOT_REF) {
1394 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1395 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1396 } else if (type == SLOT_NOREF) {
1397 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1398 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1403 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1407 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1408 set_reg_slot (gcfg, slot, cindex, type);
1412 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1416 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1417 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1418 if (callsite_offset >= from && callsite_offset < to)
1419 set_reg_slot (gcfg, slot, cindex, type);
1424 process_spill_slots (MonoCompile *cfg)
1426 MonoCompileGC *gcfg = cfg->gc_info;
1431 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1432 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1433 for (l = bb->spill_slot_defs; l; l = l->next) {
1434 MonoInst *def = l->data;
1435 int spill_slot = def->inst_c0;
1436 int bank = def->inst_c1;
1437 int offset = cfg->spill_info [bank][spill_slot].offset;
1438 int slot = fp_offset_to_slot (cfg, offset);
1440 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1441 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1445 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1446 for (l = bb->spill_slot_defs; l; l = l->next) {
1447 MonoInst *def = l->data;
1448 int spill_slot = def->inst_c0;
1449 int bank = def->inst_c1;
1450 int offset = cfg->spill_info [bank][spill_slot].offset;
1451 int slot = fp_offset_to_slot (cfg, offset);
1454 if (bank == MONO_REG_INT_MP)
1460 * Extend the live interval for the GC tracked spill slots
1461 * defined in this bblock.
1462 * FIXME: This is not needed.
1464 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1466 if (cfg->verbose_level > 1)
1467 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1471 /* Set fp spill slots to NOREF */
1472 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1473 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1479 slot = fp_offset_to_slot (cfg, offset);
1481 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1483 if (cfg->verbose_level > 1)
1484 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1487 /* Set int spill slots to NOREF */
1488 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1489 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1495 slot = fp_offset_to_slot (cfg, offset);
1497 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1498 if (cfg->verbose_level > 1)
1499 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1504 * process_other_slots:
1506 * Process stack slots registered using mini_gc_set_slot_type_... ().
1509 process_other_slots (MonoCompile *cfg)
1511 MonoCompileGC *gcfg = cfg->gc_info;
1514 /* Relative to the CFA */
1515 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1516 guint data = GPOINTER_TO_UINT (l->data);
1517 int cfa_slot = data >> 16;
1518 GCSlotType type = data & 0xff;
1522 * Map the cfa relative slot to an fp relative slot.
1523 * slot_addr == cfa - <cfa_slot>*4/8
1524 * fp + cfa_offset == cfa
1525 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1527 slot = (cfg->cfa_offset / SIZEOF_SLOT) - cfa_slot - (gcfg->min_offset / SIZEOF_SLOT);
1529 set_slot_everywhere (gcfg, slot, type);
1531 if (cfg->verbose_level > 1) {
1532 int fp_offset = slot_to_fp_offset (cfg, slot);
1533 if (type == SLOT_NOREF)
1534 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));
1538 /* Relative to the FP */
1539 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1540 gint data = GPOINTER_TO_INT (l->data);
1541 int offset = data >> 16;
1542 GCSlotType type = data & 0xff;
1545 slot = fp_offset_to_slot (cfg, offset);
1547 set_slot_everywhere (gcfg, slot, type);
1549 /* Liveness for these slots is handled by process_spill_slots () */
1551 if (cfg->verbose_level > 1) {
1552 if (type == SLOT_REF)
1553 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1554 else if (type == SLOT_NOREF)
1555 printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset, slot);
1561 get_vtype_bitmap (MonoType *t, int *numbits)
1563 MonoClass *klass = mono_class_from_mono_type (t);
1565 if (klass->generic_container || mono_class_is_open_constructed_type (t)) {
1566 /* FIXME: Generic sharing */
1569 mono_class_compute_gc_descriptor (klass);
1571 return mono_gc_get_bitmap_for_descr (klass->gc_descr, numbits);
1575 static inline const char*
1576 get_offset_sign (int offset)
1578 return offset < 0 ? "-" : "+";
1582 get_offset_val (int offset)
1584 return offset < 0 ? (- offset) : offset;
1588 process_variables (MonoCompile *cfg)
1590 MonoCompileGC *gcfg = cfg->gc_info;
1591 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1592 int i, locals_min_slot, locals_max_slot, cindex;
1596 int locals_min_offset = gcfg->locals_min_offset;
1597 int locals_max_offset = gcfg->locals_max_offset;
1599 /* Slots for locals are NOREF by default */
1600 locals_min_slot = (locals_min_offset - gcfg->min_offset) / SIZEOF_SLOT;
1601 locals_max_slot = (locals_max_offset - gcfg->min_offset) / SIZEOF_SLOT;
1602 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1603 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1607 * Compute the offset where variables are initialized in the first bblock, if any.
1609 pc_offsets = g_new0 (int, cfg->next_vreg);
1611 bb = cfg->bb_entry->next_bb;
1612 MONO_BB_FOR_EACH_INS (bb, tmp) {
1613 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1614 int vreg = tmp->inst_c1;
1615 if (pc_offsets [vreg] == 0) {
1616 g_assert (tmp->backend.pc_offset > 0);
1617 pc_offsets [vreg] = tmp->backend.pc_offset;
1623 * Stack slots holding arguments are initialized in the prolog.
1624 * This means we can treat them alive for the whole method.
1626 for (i = 0; i < cfg->num_varinfo; i++) {
1627 MonoInst *ins = cfg->varinfo [i];
1628 MonoType *t = ins->inst_vtype;
1631 gboolean byref, is_this = FALSE;
1632 gboolean is_arg = i < cfg->locals_start;
1634 if (ins == cfg->ret) {
1635 if (!(ins->opcode == OP_REGOFFSET && MONO_TYPE_ISSTRUCT (t)))
1639 vmv = MONO_VARINFO (cfg, i);
1641 /* For some reason, 'this' is byref */
1642 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1643 t = &cfg->method->klass->byval_arg;
1649 if (ins->opcode == OP_REGVAR) {
1651 GCSlotType slot_type;
1653 t = mini_type_get_underlying_type (NULL, t);
1656 g_assert (hreg < MONO_MAX_IREGS);
1659 slot_type = SLOT_PIN;
1661 slot_type = mini_type_is_reference (cfg, t) ? SLOT_REF : SLOT_NOREF;
1663 if (slot_type == SLOT_PIN) {
1664 /* These have no live interval, be conservative */
1665 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1668 * Unlike variables allocated to the stack, we generate liveness info
1669 * for noref vars in registers in mono_spill_global_vars (), because
1670 * knowing that a register doesn't contain a ref allows us to mark its save
1671 * locations precisely.
1673 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1674 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1675 set_reg_slot (gcfg, hreg, cindex, slot_type);
1678 if (cfg->verbose_level > 1) {
1679 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1685 if (ins->opcode != OP_REGOFFSET)
1688 if (ins->inst_offset % SIZEOF_SLOT != 0)
1691 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1693 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1694 /* These do not get stored in the prolog */
1695 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1697 if (cfg->verbose_level > 1) {
1698 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));
1703 if (MONO_TYPE_ISSTRUCT (t)) {
1705 gsize *bitmap = NULL;
1706 gboolean pin = FALSE;
1710 if (ins->backend.is_pinvoke)
1711 size = mono_class_native_size (ins->klass, NULL);
1713 size = mono_class_value_size (ins->klass, NULL);
1714 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
1716 if (cfg->verbose_level > 1)
1717 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));
1719 if (!ins->klass->has_references) {
1721 for (j = 0; j < size_in_slots; ++j)
1722 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1727 bitmap = get_vtype_bitmap (t, &numbits);
1732 * Most vtypes are marked volatile because of the LDADDR instructions,
1733 * and they have no liveness information since they are decomposed
1734 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1735 * them during VZERO decomposition.
1738 if (!pc_offsets [vmv->vreg])
1741 if (ins->backend.is_pinvoke)
1746 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1747 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1748 for (j = 0; j < numbits; ++j) {
1749 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1750 /* The descriptor is for the boxed object */
1751 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, pin ? SLOT_PIN : SLOT_REF);
1757 if (cfg->verbose_level > 1) {
1758 for (j = 0; j < numbits; ++j) {
1759 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1760 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)));
1764 if (cfg->verbose_level > 1)
1765 printf ("\t\tpinned\n");
1766 for (j = 0; j < size_in_slots; ++j) {
1767 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1776 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1777 /* Vret addr etc. */
1782 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1784 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1785 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1786 set_slot (gcfg, pos, cindex, SLOT_PIN);
1788 if (cfg->verbose_level > 1)
1789 printf ("\tbyref 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));
1794 * This is currently disabled, but could be enabled to debug crashes.
1797 if (t->type == MONO_TYPE_I) {
1799 * Variables created in mono_handle_global_vregs have type I, but they
1800 * could hold GC refs since the vregs they were created from might not been
1801 * marked as holding a GC ref. So be conservative.
1803 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1808 t = mini_type_get_underlying_type (NULL, t);
1810 if (!mini_type_is_reference (cfg, t)) {
1811 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1812 if (cfg->verbose_level > 1)
1813 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));
1814 if (!t->byref && sizeof (mgreg_t) == 4 && (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_R8)) {
1815 set_slot_everywhere (gcfg, pos + 1, SLOT_NOREF);
1816 if (cfg->verbose_level > 1)
1817 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));
1822 /* 'this' is marked INDIRECT for gshared methods */
1823 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1825 * For volatile variables, treat them alive from the point they are
1826 * initialized in the first bblock until the end of the method.
1829 set_slot_everywhere (gcfg, pos, SLOT_REF);
1830 } else if (pc_offsets [vmv->vreg]) {
1831 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1832 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1834 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1836 if (cfg->verbose_level > 1)
1837 printf ("\tvolatile ref 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));
1842 /* Live for the whole method */
1843 set_slot_everywhere (gcfg, pos, SLOT_REF);
1845 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1846 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1847 set_slot (gcfg, pos, cindex, SLOT_REF);
1850 if (cfg->verbose_level > 1) {
1851 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));
1855 g_free (pc_offsets);
1859 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1862 * Convert a sp relative offset to a slot index. This is
1863 * platform specific.
1866 /* fp = sp + offset */
1867 g_assert (cfg->frame_reg == AMD64_RBP);
1868 return (- cfg->arch.sp_fp_offset + sp_offset);
1869 #elif defined(TARGET_X86)
1870 /* The offset is computed from the sp at the start of the call sequence */
1871 g_assert (cfg->frame_reg == X86_EBP);
1872 return (- cfg->arch.sp_fp_offset - sp_offset);
1880 process_param_area_slots (MonoCompile *cfg)
1882 MonoCompileGC *gcfg = cfg->gc_info;
1887 * These slots are used for passing parameters during calls. They are sp relative, not
1888 * fp relative, so they are harder to handle.
1890 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1891 /* The distance between fp and sp is not constant */
1894 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1896 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1897 GCCallSite *callsite = gcfg->callsites [cindex];
1900 for (l = callsite->param_slots; l; l = l->next) {
1901 MonoInst *def = l->data;
1902 MonoType *t = def->inst_vtype;
1903 int sp_offset = def->inst_offset;
1904 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1905 int slot = fp_offset_to_slot (cfg, fp_offset);
1909 if (MONO_TYPE_ISSTRUCT (t)) {
1910 size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE);
1912 size = sizeof (mgreg_t);
1915 for (i = 0; i < size / sizeof (mgreg_t); ++i) {
1916 g_assert (slot + i >= 0 && slot + i < gcfg->nslots);
1917 is_param [slot + i] = TRUE;
1922 /* All param area slots are noref by default */
1923 for (i = 0; i < gcfg->nslots; ++i) {
1925 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1929 * We treat param area slots as being part of the callee's frame, to be able to handle tail calls which overwrite
1930 * the argument area of the caller.
1935 process_finally_clauses (MonoCompile *cfg)
1937 MonoCompileGC *gcfg = cfg->gc_info;
1938 GCCallSite **callsites;
1940 gboolean has_finally;
1941 int i, j, nslots, nregs;
1943 ncallsites = gcfg->ncallsites;
1944 nslots = gcfg->nslots;
1945 nregs = gcfg->nregs;
1946 callsites = gcfg->callsites;
1949 * The calls to the finally clauses don't show up in the cfg. See
1950 * test_0_liveness_8 ().
1951 * Variables accessed inside the finally clause are already marked VOLATILE by
1952 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
1953 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
1955 has_finally = FALSE;
1956 for (i = 0; i < cfg->header->num_clauses; ++i) {
1957 MonoExceptionClause *clause = &cfg->header->clauses [i];
1959 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1964 if (cfg->verbose_level > 1)
1965 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
1966 for (j = 0; j < ncallsites; ++j) {
1967 MonoBasicBlock *bb = callsites [j]->bb;
1968 MonoExceptionClause *clause;
1969 gboolean is_in_finally = FALSE;
1971 for (i = 0; i < cfg->header->num_clauses; ++i) {
1972 clause = &cfg->header->clauses [i];
1974 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
1975 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1976 is_in_finally = TRUE;
1982 if (is_in_finally) {
1983 for (i = 0; i < nslots; ++i)
1984 set_slot (gcfg, i, j, SLOT_PIN);
1985 for (i = 0; i < nregs; ++i)
1986 set_reg_slot (gcfg, i, j, SLOT_PIN);
1993 compute_frame_size (MonoCompile *cfg)
1995 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
1996 int min_offset, max_offset;
1997 MonoCompileGC *gcfg = cfg->gc_info;
1998 MonoMethodSignature *sig = mono_method_signature (cfg->method);
2001 /* Compute min/max offsets from the fp */
2004 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
2005 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, SIZEOF_SLOT);
2006 locals_max_offset = cfg->locals_max_stack_offset;
2008 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
2012 locals_min_offset = ALIGN_TO (locals_min_offset, SIZEOF_SLOT);
2013 locals_max_offset = ALIGN_TO (locals_max_offset, SIZEOF_SLOT);
2015 min_offset = locals_min_offset;
2016 max_offset = locals_max_offset;
2019 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2020 MonoInst *ins = cfg->args [i];
2022 if (ins->opcode == OP_REGOFFSET) {
2023 int size, size_in_slots;
2024 size = mini_type_stack_size_full (cfg->generic_sharing_context, ins->inst_vtype, NULL, ins->backend.is_pinvoke);
2025 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
2027 min_offset = MIN (min_offset, ins->inst_offset);
2028 max_offset = MAX ((int)max_offset, (int)(ins->inst_offset + (size_in_slots * SIZEOF_SLOT)));
2033 g_assert (cfg->frame_reg == cfg->cfa_reg);
2034 g_assert (cfg->cfa_offset > 0);
2036 cfa_max_offset = cfg->cfa_offset;
2038 min_offset = MIN (min_offset, cfa_min_offset);
2039 max_offset = MAX (max_offset, cfa_max_offset);
2041 /* Fp relative slots */
2042 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
2043 gint data = GPOINTER_TO_INT (l->data);
2044 int offset = data >> 16;
2046 min_offset = MIN (min_offset, offset);
2050 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
2051 int stack_offset = ALIGN_TO (cfg->stack_offset, SIZEOF_SLOT);
2052 min_offset = MIN (min_offset, (-stack_offset));
2055 /* Param area slots */
2057 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2058 #elif defined(TARGET_X86)
2059 min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size));
2060 #elif defined(TARGET_ARM)
2062 #elif defined(TARGET_s390X)
2068 gcfg->min_offset = min_offset;
2069 gcfg->max_offset = max_offset;
2070 gcfg->locals_min_offset = locals_min_offset;
2071 gcfg->locals_max_offset = locals_max_offset;
2075 init_gcfg (MonoCompile *cfg)
2077 int i, nregs, nslots;
2078 MonoCompileGC *gcfg = cfg->gc_info;
2079 GCCallSite **callsites;
2088 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2089 ncallsites += g_slist_length (bb->gc_callsites);
2091 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
2093 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2094 for (l = bb->gc_callsites; l; l = l->next)
2095 callsites [i++] = l->data;
2098 /* The callsites should already be ordered by pc offset */
2099 for (i = 1; i < ncallsites; ++i)
2100 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
2103 * The stack frame looks like this:
2105 * <fp + max_offset> == cfa -> <end of previous frame>
2106 * <other stack slots>
2108 * <other stack slots>
2109 * fp + min_offset ->
2114 if (cfg->verbose_level > 1)
2115 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
2117 nslots = (gcfg->max_offset - gcfg->min_offset) / SIZEOF_SLOT;
2120 gcfg->nslots = nslots;
2121 gcfg->nregs = nregs;
2122 gcfg->callsites = callsites;
2123 gcfg->ncallsites = ncallsites;
2124 gcfg->stack_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2125 gcfg->reg_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2126 gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2127 gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2128 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2129 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2131 /* All slots start out as PIN */
2132 memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * nregs);
2133 for (i = 0; i < nregs; ++i) {
2135 * By default, registers are NOREF.
2136 * It is possible for a callee to save them before being defined in this method,
2137 * but the saved value is dead too, so it doesn't need to be marked.
2139 if ((cfg->used_int_regs & (1 << i)))
2140 set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
2144 static inline gboolean
2145 has_bit_set (guint8 *bitmap, int width, int slot)
2148 int pos = width * slot;
2150 for (i = 0; i < width; ++i) {
2151 if (bitmap [pos + i])
2158 create_map (MonoCompile *cfg)
2161 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
2163 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
2164 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
2166 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
2167 MonoCompileGC *gcfg = cfg->gc_info;
2168 GCCallSite **callsites;
2170 guint8 *bitmap, *bitmaps;
2171 guint32 reg_ref_mask, reg_pin_mask;
2173 ncallsites = gcfg->ncallsites;
2174 nslots = gcfg->nslots;
2175 nregs = gcfg->nregs;
2176 callsites = gcfg->callsites;
2179 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
2180 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
2182 has_ref_slots = FALSE;
2183 has_pin_slots = FALSE;
2186 memset (ntypes, 0, sizeof (ntypes));
2187 for (i = 0; i < nslots; ++i) {
2188 gboolean has_ref = FALSE;
2189 gboolean has_pin = FALSE;
2191 if (has_bit_set (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i))
2193 if (has_bit_set (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i))
2197 has_ref_slots = TRUE;
2199 has_pin_slots = TRUE;
2202 ntypes [SLOT_REF] ++;
2204 ntypes [SLOT_PIN] ++;
2206 ntypes [SLOT_NOREF] ++;
2208 if (has_ref || has_pin) {
2215 start = end = nslots;
2217 g_assert (start != -1);
2218 g_assert (start < end);
2221 has_ref_regs = FALSE;
2222 has_pin_regs = FALSE;
2227 for (i = 0; i < nregs; ++i) {
2228 gboolean has_ref = FALSE;
2229 gboolean has_pin = FALSE;
2231 if (!(cfg->used_int_regs & (1 << i)))
2234 if (has_bit_set (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i))
2236 if (has_bit_set (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i))
2240 reg_ref_mask |= (1 << i);
2241 has_ref_regs = TRUE;
2245 reg_pin_mask |= (1 << i);
2246 has_pin_regs = TRUE;
2251 if (cfg->verbose_level > 1)
2252 printf ("Slots: %d Start: %d End: %d Refs: %d NoRefs: %d Pin: %d Callsites: %d\n", nslots, start, end, ntypes [SLOT_REF], ntypes [SLOT_NOREF], ntypes [SLOT_PIN], ncallsites);
2254 /* Create the GC Map */
2256 /* The work bitmaps have one row for each slot, since this is how we access them during construction */
2257 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2258 stack_bitmap_size = stack_bitmap_width * ncallsites;
2259 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2260 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2261 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2262 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2263 bitmaps_size = (has_ref_slots ? stack_bitmap_size : 0) + (has_pin_slots ? stack_bitmap_size : 0) + (has_ref_regs ? reg_ref_bitmap_size : 0) + (has_pin_regs ? reg_pin_bitmap_size : 0);
2265 map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2267 map->frame_reg = cfg->frame_reg;
2268 map->start_offset = gcfg->min_offset;
2269 map->end_offset = gcfg->min_offset + (nslots * SIZEOF_SLOT);
2270 map->map_offset = start * SIZEOF_SLOT;
2271 map->nslots = end - start;
2272 map->has_ref_slots = has_ref_slots;
2273 map->has_pin_slots = has_pin_slots;
2274 map->has_ref_regs = has_ref_regs;
2275 map->has_pin_regs = has_pin_regs;
2276 g_assert (nregs < 32);
2277 map->used_int_regs = cfg->used_int_regs;
2278 map->reg_ref_mask = reg_ref_mask;
2279 map->reg_pin_mask = reg_pin_mask;
2280 map->nref_regs = nref_regs;
2281 map->npin_regs = npin_regs;
2283 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2286 if (has_ref_slots) {
2287 map->stack_ref_bitmap_offset = bitmaps_offset;
2288 bitmaps_offset += stack_bitmap_size;
2290 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2291 for (i = 0; i < nslots; ++i) {
2292 for (j = 0; j < ncallsites; ++j) {
2293 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i, j))
2294 set_bit (bitmap, stack_bitmap_width, j, i - start);
2298 if (has_pin_slots) {
2299 map->stack_pin_bitmap_offset = bitmaps_offset;
2300 bitmaps_offset += stack_bitmap_size;
2302 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2303 for (i = 0; i < nslots; ++i) {
2304 for (j = 0; j < ncallsites; ++j) {
2305 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i, j))
2306 set_bit (bitmap, stack_bitmap_width, j, i - start);
2311 map->reg_ref_bitmap_offset = bitmaps_offset;
2312 bitmaps_offset += reg_ref_bitmap_size;
2314 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2316 for (i = 0; i < nregs; ++i) {
2317 if (reg_ref_mask & (1 << i)) {
2318 for (j = 0; j < ncallsites; ++j) {
2319 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i, j))
2320 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2327 map->reg_pin_bitmap_offset = bitmaps_offset;
2328 bitmaps_offset += reg_pin_bitmap_size;
2330 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2332 for (i = 0; i < nregs; ++i) {
2333 if (reg_pin_mask & (1 << i)) {
2334 for (j = 0; j < ncallsites; ++j) {
2335 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i, j))
2336 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2344 map->ncallsites = ncallsites;
2345 if (cfg->code_len < 256)
2346 map->callsite_entry_size = 1;
2347 else if (cfg->code_len < 65536)
2348 map->callsite_entry_size = 2;
2350 map->callsite_entry_size = 4;
2352 /* Encode the GC Map */
2360 encode_gc_map (map, buf, &endbuf);
2361 g_assert (endbuf - buf < 256);
2363 encoded_size = endbuf - buf;
2364 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2366 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2367 //emap->ref_slots = map->ref_slots;
2369 /* Encoded fixed fields */
2370 p = &emap->encoded [0];
2371 //emap->encoded_size = encoded_size;
2372 memcpy (p, buf, encoded_size);
2375 /* Callsite table */
2376 p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
2377 if (map->callsite_entry_size == 1) {
2378 guint8 *offsets = p;
2379 for (i = 0; i < ncallsites; ++i)
2380 offsets [i] = callsites [i]->pc_offset;
2381 stats.gc_callsites8_size += ncallsites * sizeof (guint8);
2382 } else if (map->callsite_entry_size == 2) {
2383 guint16 *offsets = (guint16*)p;
2384 for (i = 0; i < ncallsites; ++i)
2385 offsets [i] = callsites [i]->pc_offset;
2386 stats.gc_callsites16_size += ncallsites * sizeof (guint16);
2388 guint32 *offsets = (guint32*)p;
2389 for (i = 0; i < ncallsites; ++i)
2390 offsets [i] = callsites [i]->pc_offset;
2391 stats.gc_callsites32_size += ncallsites * sizeof (guint32);
2393 p += ncallsites * map->callsite_entry_size;
2396 memcpy (p, bitmaps, bitmaps_size);
2399 g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
2401 stats.gc_maps_size += alloc_size;
2402 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
2403 stats.gc_bitmaps_size += bitmaps_size;
2404 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
2406 cfg->jit_info->gc_info = emap;
2408 cfg->gc_map = (guint8*)emap;
2409 cfg->gc_map_size = alloc_size;
2412 stats.all_slots += nslots;
2413 stats.ref_slots += ntypes [SLOT_REF];
2414 stats.noref_slots += ntypes [SLOT_NOREF];
2415 stats.pin_slots += ntypes [SLOT_PIN];
2419 mini_gc_create_gc_map (MonoCompile *cfg)
2421 if (!cfg->compute_gc_maps)
2425 * During marking, all frames except the top frame are at a call site, and we mark the
2426 * top frame conservatively. This means that we only need to compute and record
2427 * GC maps for call sites.
2430 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2431 /* Without liveness info, the live ranges are not precise enough */
2434 mono_analyze_liveness_gc (cfg);
2436 compute_frame_size (cfg);
2440 process_spill_slots (cfg);
2441 process_other_slots (cfg);
2442 process_param_area_slots (cfg);
2443 process_variables (cfg);
2444 process_finally_clauses (cfg);
2449 #endif /* DISABLE_JIT */
2452 parse_debug_options (void)
2457 env = g_getenv ("MONO_GCMAP_DEBUG");
2461 opts = g_strsplit (env, ",", -1);
2462 for (ptr = opts; ptr && *ptr; ptr ++) {
2463 /* No options yet */
2464 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2475 memset (&cb, 0, sizeof (cb));
2476 cb.thread_attach_func = thread_attach_func;
2477 cb.thread_detach_func = thread_detach_func;
2478 cb.thread_suspend_func = thread_suspend_func;
2479 /* Comment this out to disable precise stack marking */
2480 cb.thread_mark_func = thread_mark_func;
2481 mono_gc_set_gc_callbacks (&cb);
2483 logfile = mono_gc_get_logfile ();
2485 parse_debug_options ();
2487 mono_counters_register ("GC Maps size",
2488 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2489 mono_counters_register ("GC Call Sites size",
2490 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2491 mono_counters_register ("GC Bitmaps size",
2492 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2493 mono_counters_register ("GC Map struct size",
2494 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2495 mono_counters_register ("GC Call Sites encoded using 8 bits",
2496 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2497 mono_counters_register ("GC Call Sites encoded using 16 bits",
2498 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2499 mono_counters_register ("GC Call Sites encoded using 32 bits",
2500 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2502 mono_counters_register ("GC Map slots (all)",
2503 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2504 mono_counters_register ("GC Map slots (ref)",
2505 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2506 mono_counters_register ("GC Map slots (noref)",
2507 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2508 mono_counters_register ("GC Map slots (pin)",
2509 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2511 mono_counters_register ("GC TLS Data size",
2512 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2514 mono_counters_register ("Stack space scanned (all)",
2515 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2516 mono_counters_register ("Stack space scanned (native)",
2517 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2518 mono_counters_register ("Stack space scanned (other)",
2519 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2520 mono_counters_register ("Stack space scanned (using GC Maps)",
2521 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2522 mono_counters_register ("Stack space scanned (precise)",
2523 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2524 mono_counters_register ("Stack space scanned (pin)",
2525 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2526 mono_counters_register ("Stack space scanned (pin registers)",
2527 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2533 mini_gc_enable_gc_maps_for_aot (void)
2545 mini_gc_init_gc_map (MonoCompile *cfg)
2550 mini_gc_create_gc_map (MonoCompile *cfg)
2555 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2560 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2564 #endif /* DISABLE_JIT */
2573 * Set GC specific options in CFG.
2576 mini_gc_init_cfg (MonoCompile *cfg)
2578 if (mono_gc_is_moving ()) {
2579 cfg->disable_ref_noref_stack_slot_share = TRUE;
2580 cfg->gen_write_barriers = TRUE;
2583 mini_gc_init_gc_map (cfg);
2586 #endif /* DISABLE_JIT */
2589 * Problems with the current code:
2590 * - the stack walk is slow
2591 * - vtypes/refs used in EH regions are treated conservatively
2592 * - if the code is finished, less pinning will be done, causing problems because
2593 * we promote all surviving objects to old-gen.
2594 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2595 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2596 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2597 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2602 * Ideas for creating smaller GC maps:
2603 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2605 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2607 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2608 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2609 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2610 * from an index entry.