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)
12 #include <mono/metadata/gc-internal.h>
17 #include <mono/metadata/gc-internal.h>
18 #include <mono/utils/mono-counters.h>
20 /* Contains state needed by the GC Map construction code */
23 * This contains information about stack slots initialized in the prolog, encoded using
24 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
25 * means cfa+0, 1 means cfa-4/8, etc.
27 GSList *stack_slots_from_cfa;
28 /* Same for stack slots relative to the frame pointer */
29 GSList *stack_slots_from_fp;
31 /* Number of slots in the map */
33 /* The number of registers in the map */
35 /* Min and Max offsets of the stack frame relative to fp */
36 int min_offset, max_offset;
37 /* Same for the locals area */
38 int locals_min_offset, locals_max_offset;
40 /* The call sites where this frame can be stopped during GC */
41 GCCallSite **callsites;
42 /* The number of call sites */
46 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
47 * runtime, since it includes columns which are 0.
51 * A bitmap whose width equals nslots, and whose height equals ncallsites.
52 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
56 /* Same for SLOT_PIN */
60 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
63 guint8 *reg_ref_bitmap;
64 guint8 *reg_pin_bitmap;
67 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
70 #define DEBUG(s) do { s; } while (0)
76 #define DEBUG_GC_MAP(s) do { s; fflush (stdout); } while (0)
78 #define DEBUG_GC_MAP(s)
81 #define GC_BITS_PER_WORD (sizeof (gsize) * 8)
84 * Contains information collected during the conservative stack marking pass,
85 * used during the precise pass. This helps to avoid doing a stack walk twice, which
87 * FIXME: Optimize memory usage.
92 /* FIXME: Store an offset from the previous frame */
95 int regs [MONO_MAX_IREGS];
96 gpointer reg_locations [MONO_MAX_IREGS];
99 /* Max number of frames stored in the TLS data */
100 #define MAX_FRAMES 50
103 * Per-thread data kept by this module. This is stored in the GC and passed to us as
104 * parameters, instead of being stored in a TLS variable, since during a collection,
105 * only the collection thread is active.
110 gboolean has_context;
111 MonoJitTlsData *jit_tls;
112 /* Number of frames collected during the !precise pass */
114 FrameInfo frames [MAX_FRAMES];
117 /* These are constant so don't store them in the GC Maps */
118 /* Number of registers stored in gc maps */
119 #define NREGS MONO_MAX_IREGS
123 * Contains information needed to mark a stack frame.
124 * This is a transient structure, created from a compressed representation on-demand.
128 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
129 * This includes memory which is NOREF thus doesn't need GC maps.
134 * The offset relative to frame_offset where the the memory described by the GC maps
138 /* The number of stack slots in the map */
140 /* The frame pointer register */
142 /* The size of each callsite table entry */
143 guint8 callsite_entry_size;
144 guint has_pin_slots : 1;
145 guint has_ref_slots : 1;
146 guint has_ref_regs : 1;
147 guint has_pin_regs : 1;
149 /* The offsets below are into an external bitmaps array */
152 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
153 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
156 guint32 stack_ref_bitmap_offset;
158 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
159 * different callsites, if the slot starts out as PIN, and later changes to REF.
161 guint32 stack_pin_bitmap_offset;
164 * Corresponding bitmaps for registers
165 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
166 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
168 guint32 reg_pin_bitmap_offset;
169 guint32 reg_ref_bitmap_offset;
171 guint32 reg_ref_mask, reg_pin_mask;
173 /* The number of bits set in the two masks above */
174 guint8 nref_regs, npin_regs;
177 * A bit array marking slots which contain refs.
178 * This is used only for debugging.
182 /* Callsite offsets */
183 /* These can take up a lot of space, so encode them compactly */
193 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
197 //guint8 encoded_size;
200 * The arrays below are embedded after the struct.
201 * Their address needs to be computed.
204 /* The fixed fields of the GCMap encoded using LEB128 */
205 guint8 encoded [MONO_ZERO_LEN_ARRAY];
207 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
208 guint8 callsites [MONO_ZERO_LEN_ARRAY];
211 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
214 // FIXME: Move these to a shared place
217 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
222 guint8 b = value & 0x7f;
224 if (value != 0) /* more bytes to come */
232 static G_GNUC_UNUSED void
233 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
236 gboolean negative = (value < 0);
244 /* the following is unnecessary if the
245 * implementation of >>= uses an arithmetic rather
246 * than logical shift for a signed left operand
250 value |= - (1 <<(size - 7));
251 /* sign bit of byte is second high order bit (0x40) */
252 if ((value == 0 && !(byte & 0x40)) ||
253 (value == -1 && (byte & 0x40)))
263 static inline guint32
264 decode_uleb128 (guint8 *buf, guint8 **endbuf)
274 res = res | (((int)(b & 0x7f)) << shift);
286 decode_sleb128 (guint8 *buf, guint8 **endbuf)
296 res = res | (((int)(b & 0x7f)) << shift);
299 if (shift < 32 && (b & 0x40))
300 res |= - (1 << shift);
313 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
316 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
320 encode_sleb128 (map->start_offset / sizeof (mgreg_t), buf, &buf);
321 encode_sleb128 (map->start_offset / sizeof (mgreg_t), buf, &buf);
322 encode_sleb128 (map->map_offset / sizeof (mgreg_t), buf, &buf);
323 encode_uleb128 (map->nslots, buf, &buf);
324 encode_uleb128 (map->callsite_entry_size, buf, &buf);
325 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->frame_reg << 4);
326 encode_uleb128 (flags, buf, &buf);
327 if (map->has_ref_regs)
328 encode_uleb128 (map->reg_ref_mask, buf, &buf);
329 if (map->has_pin_regs)
330 encode_uleb128 (map->reg_pin_mask, buf, &buf);
331 if (map->has_ref_regs)
332 encode_uleb128 (map->nref_regs, buf, &buf);
333 if (map->has_pin_regs)
334 encode_uleb128 (map->npin_regs, buf, &buf);
335 encode_uleb128 (map->ncallsites, buf, &buf);
343 * Decode the encoded GC map representation in BUF and store the result into MAP.
346 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
349 int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset;
351 map->start_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
352 map->start_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
353 map->map_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
354 map->nslots = decode_uleb128 (buf, &buf);
355 map->callsite_entry_size = decode_uleb128 (buf, &buf);
356 flags = decode_uleb128 (buf, &buf);
357 map->has_ref_slots = (flags & 1) ? 1 : 0;
358 map->has_pin_slots = (flags & 2) ? 1 : 0;
359 map->has_ref_regs = (flags & 4) ? 1 : 0;
360 map->has_pin_regs = (flags & 8) ? 1 : 0;
361 map->frame_reg = flags >> 4;
362 if (map->has_ref_regs)
363 map->reg_ref_mask = decode_uleb128 (buf, &buf);
364 if (map->has_pin_regs)
365 map->reg_pin_mask = decode_uleb128 (buf, &buf);
366 if (map->has_ref_regs)
367 map->nref_regs = decode_uleb128 (buf, &buf);
368 if (map->has_pin_regs)
369 map->npin_regs = decode_uleb128 (buf, &buf);
370 map->ncallsites = decode_uleb128 (buf, &buf);
372 stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
373 reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
374 reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
376 map->stack_ref_bitmap_offset = offset;
377 if (map->has_ref_slots)
378 offset += stack_bitmap_size;
379 map->stack_pin_bitmap_offset = offset;
380 if (map->has_pin_slots)
381 offset += stack_bitmap_size;
382 map->reg_ref_bitmap_offset = offset;
383 if (map->has_ref_regs)
384 offset += reg_ref_bitmap_size;
385 map->reg_pin_bitmap_offset = offset;
386 if (map->has_pin_regs)
387 offset += reg_pin_bitmap_size;
393 thread_attach_func (void)
395 return g_new0 (TlsData, 1);
399 thread_suspend_func (gpointer user_data, void *sigctx)
401 TlsData *tls = user_data;
404 /* Happens during startup */
407 tls->lmf = mono_get_lmf ();
409 mono_arch_sigctx_to_monoctx (sigctx, &tls->ctx);
410 tls->has_context = TRUE;
412 tls->has_context = FALSE;
414 tls->jit_tls = TlsGetValue (mono_jit_tls_id);
417 static int precise_frame_count [2], precise_frame_limit = -1;
418 static gboolean precise_frame_limit_inited;
424 int scanned_precisely;
425 int scanned_conservatively;
426 int scanned_registers;
434 int gc_callsites_size;
435 int gc_callsites8_num;
436 int gc_callsites16_num;
437 int gc_callsites32_num;
439 int gc_map_struct_size;
442 static JITGCStats stats;
444 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
447 set_bit (guint8 *bitmap, int width, int y, int x)
449 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
453 clear_bit (guint8 *bitmap, int width, int y, int x)
455 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
459 get_bit (guint8 *bitmap, int width, int y, int x)
461 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
465 slot_type_to_string (GCSlotType type)
475 g_assert_not_reached ();
481 * conservatively_pass:
483 * Mark a thread stack conservatively and collect information needed by the precise pass.
486 conservative_pass (gpointer user_data, guint8 *stack_start, guint8 *stack_end)
488 TlsData *tls = user_data;
490 MonoContext ctx, new_ctx;
493 gboolean last = TRUE;
497 guint8* fp, *p, *frame_start, *frame_end;
498 int i, pc_offset, cindex, bitmap_width;
499 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
501 StackFrameInfo frame;
502 mgreg_t *reg_locations [MONO_MAX_IREGS];
503 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
506 guint32 precise_regmask;
508 /* tls == NULL can happen during startup */
509 if (mono_thread_internal_current () == NULL || !tls) {
510 mono_gc_conservatively_scan_area (stack_start, stack_end);
511 stats.scanned_stacks += stack_end - stack_start;
518 /* Number of bytes scanned based on GC map data */
520 /* Number of bytes scanned precisely based on GC map data */
521 scanned_precisely = 0;
522 /* Number of bytes scanned conservatively based on GC map data */
523 scanned_conservatively = 0;
524 /* Number of bytes scanned conservatively in register save areas */
525 scanned_registers = 0;
527 /* This is one past the last address which we have scanned */
528 stack_limit = stack_start;
530 if (!tls->has_context)
531 memset (&new_ctx, 0, sizeof (ctx));
533 memcpy (&new_ctx, &tls->ctx, sizeof (MonoContext));
535 memset (reg_locations, 0, sizeof (reg_locations));
536 memset (new_reg_locations, 0, sizeof (new_reg_locations));
541 memcpy (&ctx, &new_ctx, sizeof (ctx));
543 for (i = 0; i < MONO_MAX_IREGS; ++i) {
544 if (new_reg_locations [i]) {
546 * If the current frame saves the register, it means it might modify its
547 * value, thus the old location might not contain the same value, so
548 * we have to mark it conservatively.
549 * FIXME: This happens very often, due to:
550 * - outside the live intervals of the variables allocated to a register,
551 * we have to treat the register as PIN, since we don't know whenever it
552 * has the same value as in the caller, or a new dead value.
554 if (reg_locations [i]) {
555 DEBUG (printf ("\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
556 mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
557 scanned_registers += sizeof (mgreg_t);
560 reg_locations [i] = new_reg_locations [i];
562 DEBUG (printf ("\treg %s is at location %p.\n", mono_arch_regname (i), reg_locations [i]));
566 g_assert ((guint64)stack_limit % sizeof (mgreg_t) == 0);
568 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
569 res = mono_find_jit_info_ext (frame.domain ? frame.domain : mono_domain_get (), tls->jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
576 /* The last frame can be in any state so mark conservatively */
582 /* These frames are returned by mono_find_jit_info () two times */
586 /* All the other frames are at a call site */
588 if (tls->nframes == MAX_FRAMES) {
590 * Can't save information since the array is full. So scan the rest of the
591 * stack conservatively.
596 /* Scan the frame of this method */
599 * A frame contains the following:
604 * - localloc-ed memory
611 DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(%d): No GC map for %s\n", precise, fname); g_free (fname));
617 * Debugging aid to control the number of frames scanned precisely
619 if (!precise_frame_limit_inited) {
620 if (getenv ("MONO_PRECISE_COUNT"))
621 precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT"));
622 precise_frame_limit_inited = TRUE;
625 if (precise_frame_limit != -1) {
626 if (precise_frame_count [FALSE] == precise_frame_limit)
627 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
628 if (precise_frame_count [FALSE] > precise_frame_limit)
631 precise_frame_count [FALSE] ++;
633 /* Decode the encoded GC map */
635 memset (map, 0, sizeof (GCMap));
636 decode_gc_map (&emap->encoded [0], map, &p);
637 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
638 map->callsites.offsets8 = p;
639 p += map->callsite_entry_size * map->ncallsites;
643 if (map->frame_reg == AMD64_RSP)
644 fp = (guint8*)ctx.rsp;
645 else if (map->frame_reg == AMD64_RBP)
646 fp = (guint8*)ctx.rbp;
648 g_assert_not_reached ();
651 g_assert_not_reached ();
654 frame_start = fp + map->start_offset + map->map_offset;
655 frame_end = fp + map->end_offset;
657 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
658 g_assert (pc_offset >= 0);
660 DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(%d): %s+0x%x (%p) limit=%p fp=%p frame=%p-%p (%d)\n", precise, fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx), stack_limit, fp, frame_start, frame_end, (int)(frame_end - frame_start)); g_free (fname));
662 /* Find the callsite index */
663 if (ji->code_size < 256) {
664 for (i = 0; i < map->ncallsites; ++i)
665 /* ip points inside the call instruction */
666 if (map->callsites.offsets8 [i] == pc_offset + 1)
668 } else if (ji->code_size < 65536) {
669 // FIXME: Use a binary search
670 for (i = 0; i < map->ncallsites; ++i)
671 /* ip points inside the call instruction */
672 if (map->callsites.offsets16 [i] == pc_offset + 1)
675 // FIXME: Use a binary search
676 for (i = 0; i < map->ncallsites; ++i)
677 /* ip points inside the call instruction */
678 if (map->callsites.offsets32 [i] == pc_offset + 1)
681 if (i == map->ncallsites) {
682 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
683 g_assert_not_reached ();
687 g_assert (frame_start >= stack_limit);
689 if (frame_start > stack_limit) {
690 /* This scans the previously skipped frames as well */
691 DEBUG (printf ("\tscan area %p-%p.\n", stack_limit, frame_start));
692 mono_gc_conservatively_scan_area (stack_limit, frame_start);
695 /* Mark stack slots */
696 if (map->has_pin_slots) {
697 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
698 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
703 for (i = 0; i < map->nslots; ++i) {
704 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
706 DEBUG (printf ("\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
707 mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
708 scanned_conservatively += sizeof (mgreg_t);
710 scanned_precisely += sizeof (mgreg_t);
712 p += sizeof (mgreg_t);
715 scanned_precisely += (map->nslots * sizeof (mgreg_t));
718 /* The area outside of start-end is NOREF */
719 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * sizeof (mgreg_t));
723 if (map->has_pin_regs) {
724 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
725 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
727 for (i = 0; i < NREGS; ++i) {
728 if (!(map->reg_pin_mask & (1 << i)))
731 if (reg_locations [i] && !(pin_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
733 * The method uses this register, and we have precise info for it.
734 * This means the location will be scanned precisely.
736 precise_regmask |= (1 << i);
737 DEBUG (printf ("\treg %s at location %p is precise.\n", mono_arch_regname (i), reg_locations [i]));
739 if (reg_locations [i])
740 DEBUG (printf ("\treg %s at location %p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
746 scanned += map->end_offset - map->start_offset;
748 g_assert (scanned == scanned_precisely + scanned_conservatively);
750 stack_limit = frame_end;
752 /* Save information for the precise pass */
753 fi = &tls->frames [tls->nframes];
754 fi->nslots = map->nslots;
755 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
756 if (map->has_ref_slots)
757 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
760 fi->frame_start = frame_start;
761 fi->nreg_locations = 0;
763 if (map->has_ref_regs) {
764 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
765 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
767 for (i = 0; i < NREGS; ++i) {
768 if (!(map->reg_ref_mask & (1 << i)))
771 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
772 fi->regs [fi->nreg_locations] = i;
773 fi->reg_locations [fi->nreg_locations] = reg_locations [i];
774 fi->nreg_locations ++;
780 if (precise_regmask) {
781 for (i = 0; i < NREGS; ++i) {
782 if (precise_regmask & (1 << i))
784 * Tell the code at the beginning of the loop that this location is
787 reg_locations [i] = NULL;
794 /* Scan the remaining register save locations */
795 for (i = 0; i < MONO_MAX_IREGS; ++i) {
796 if (reg_locations [i]) {
797 DEBUG (printf ("\tscan saved reg location %p.\n", reg_locations [i]));
798 mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
799 scanned_registers += sizeof (mgreg_t);
801 // FIXME: Is this needed ?
802 if (new_reg_locations [i]) {
803 DEBUG (printf ("\tscan saved reg location %p.\n", new_reg_locations [i]));
804 mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
805 scanned_registers += sizeof (mgreg_t);
809 if (stack_limit < stack_end) {
810 DEBUG (printf ("\tscan area %p-%p.\n", stack_limit, stack_end));
811 mono_gc_conservatively_scan_area (stack_limit, stack_end);
814 DEBUG (printf ("Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
816 stats.scanned_stacks += stack_end - stack_start;
817 stats.scanned += scanned;
818 stats.scanned_precisely += scanned_precisely;
819 stats.scanned_conservatively += scanned_conservatively;
820 stats.scanned_registers += scanned_registers;
822 //mono_gc_conservatively_scan_area (stack_start, stack_end);
828 * Mark a thread stack precisely based on information saved during the conservative
832 precise_pass (TlsData *tls)
840 for (findex = 0; findex < tls->nframes; findex ++) {
841 /* Load information saved by the !precise pass */
842 fi = &tls->frames [findex];
845 * FIXME: Add a function to mark using a bitmap, to avoid doing a
846 * call for each object.
849 /* Mark stack slots */
851 guint8 *ref_bitmap = fi->bitmap;
854 for (i = 0; i < fi->nslots; ++i) {
855 MonoObject **ptr = (MonoObject**)(fi->frame_start + (i * sizeof (mgreg_t)));
857 live = ref_bitmap [i / 8] & (1 << (i % 8));
860 MonoObject *obj = *ptr;
862 DEBUG (printf ("\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fp)), ptr, obj));
863 *ptr = mono_gc_scan_object (obj);
864 DEBUG (printf (" %p.\n", *ptr));
866 DEBUG (printf ("\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fp)), ptr, obj));
871 * This is disabled because the pointer takes up a lot of space.
872 * Stack slots might be shared between ref and non-ref variables ?
874 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
875 DEBUG (printf ("\tref %s0x%x(fp)=%p: dead (%p)\n", (guint8*)ptr >= (guint8*)fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fp)), ptr, *ptr));
877 * Fail fast if the live range is incorrect, and
878 * the JITted code tries to access this object
890 * Registers are different from stack slots, they have no address where they
891 * are stored. Instead, some frame below this frame in the stack saves them
892 * in its prolog to the stack. We can mark this location precisely.
894 for (i = 0; i < fi->nreg_locations; ++i) {
896 * reg_locations [i] contains the address of the stack slot where
897 * a reg was last saved, so mark that slot.
899 MonoObject **ptr = (MonoObject**)fi->reg_locations [i];
900 MonoObject *obj = *ptr;
903 DEBUG (printf ("\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), fi->reg_locations [i], obj));
904 *ptr = mono_gc_scan_object (obj);
905 DEBUG (printf (" %p.\n", *ptr));
907 DEBUG (printf ("\treg %s saved at %p: %p", mono_arch_regname (fi->regs [i]), fi->reg_locations [i], obj));
916 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
917 * call, and TRUE at the second. USER_DATA points to a TlsData
918 * structure filled up by thread_suspend_func.
921 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
923 TlsData *tls = user_data;
925 DEBUG (printf ("*** %s stack marking %p-%p ***\n", precise ? "Precise" : "Conservative", stack_start, stack_end));
928 conservative_pass (tls, stack_start, stack_end);
934 mini_gc_init_gc_map (MonoCompile *cfg)
936 if (COMPILE_LLVM (cfg))
940 /* Debugging support */
942 static int precise_count;
945 if (getenv ("MONO_GCMAP_COUNT")) {
946 if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT")))
947 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
948 if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT")))
954 cfg->compute_gc_maps = TRUE;
956 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
960 * mini_gc_set_slot_type_from_fp:
962 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
963 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
964 * need to call this function for those slots.
967 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
969 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
971 if (!cfg->compute_gc_maps)
974 g_assert (slot_offset % sizeof (mgreg_t) == 0);
976 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
980 * mini_gc_set_slot_type_from_cfa:
982 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
983 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
984 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
985 * the end of the method. By default, all stack slots are type PIN, so there is no need to
986 * call this function for those slots.
989 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
991 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
992 int slot = - (slot_offset / sizeof (mgreg_t));
994 if (!cfg->compute_gc_maps)
997 g_assert (slot_offset <= 0);
998 g_assert (slot_offset % sizeof (mgreg_t) == 0);
1000 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1004 fp_offset_to_slot (MonoCompile *cfg, int offset)
1006 MonoCompileGC *gcfg = cfg->gc_info;
1008 return (offset - gcfg->min_offset) / sizeof (mgreg_t);
1012 slot_to_fp_offset (MonoCompile *cfg, int slot)
1014 MonoCompileGC *gcfg = cfg->gc_info;
1016 return (slot * sizeof (mgreg_t)) + gcfg->min_offset;
1020 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1022 g_assert (slot >= 0 && slot < gcfg->nslots);
1024 if (type == SLOT_PIN) {
1025 clear_bit (gcfg->ref_bitmap, gcfg->bitmap_width, callsite_index, slot);
1026 set_bit (gcfg->pin_bitmap, gcfg->bitmap_width, callsite_index, slot);
1027 } else if (type == SLOT_REF) {
1028 set_bit (gcfg->ref_bitmap, gcfg->bitmap_width, callsite_index, slot);
1029 clear_bit (gcfg->pin_bitmap, gcfg->bitmap_width, callsite_index, slot);
1030 } else if (type == SLOT_NOREF) {
1031 clear_bit (gcfg->ref_bitmap, gcfg->bitmap_width, callsite_index, slot);
1032 clear_bit (gcfg->pin_bitmap, gcfg->bitmap_width, callsite_index, slot);
1037 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1041 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1042 set_slot (gcfg, slot, cindex, type);
1046 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1050 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1051 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1052 if (callsite_offset >= from && callsite_offset < to)
1053 set_slot (gcfg, slot, cindex, type);
1058 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1060 g_assert (slot >= 0 && slot < gcfg->nregs);
1062 if (type == SLOT_PIN) {
1063 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1064 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1065 } else if (type == SLOT_REF) {
1066 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1067 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1068 } else if (type == SLOT_NOREF) {
1069 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1070 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1075 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1079 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1080 set_reg_slot (gcfg, slot, cindex, type);
1084 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1088 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1089 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1090 if (callsite_offset >= from && callsite_offset < to)
1091 set_reg_slot (gcfg, slot, cindex, type);
1096 process_spill_slots (MonoCompile *cfg)
1098 MonoCompileGC *gcfg = cfg->gc_info;
1103 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1104 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1105 for (l = bb->spill_slot_defs; l; l = l->next) {
1106 MonoInst *def = l->data;
1107 int spill_slot = def->inst_c0;
1108 int bank = def->inst_c1;
1109 int offset = cfg->spill_info [bank][spill_slot].offset;
1110 int slot = fp_offset_to_slot (cfg, offset);
1112 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1113 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1117 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1118 for (l = bb->spill_slot_defs; l; l = l->next) {
1119 MonoInst *def = l->data;
1120 int spill_slot = def->inst_c0;
1121 int bank = def->inst_c1;
1122 int offset = cfg->spill_info [bank][spill_slot].offset;
1123 int slot = fp_offset_to_slot (cfg, offset);
1126 if (bank == MONO_REG_INT_MP)
1132 * Extend the live interval for the GC tracked spill slots
1133 * defined in this bblock.
1134 * FIXME: This is not needed.
1136 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1138 if (cfg->verbose_level > 1)
1139 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "+" : "-", ABS (offset), slot);
1143 /* Set fp spill slots to NOREF */
1144 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1145 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1151 slot = fp_offset_to_slot (cfg, offset);
1153 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1155 if (cfg->verbose_level > 1)
1156 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1159 /* Set int spill slots to NOREF */
1160 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1161 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1167 slot = fp_offset_to_slot (cfg, offset);
1169 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1170 if (cfg->verbose_level > 1)
1171 printf ("\tint spill slot at fp+0x%x (slot = %d)\n", offset, slot);
1176 * process_other_slots:
1178 * Process stack slots registered using mini_gc_set_slot_type_... ().
1181 process_other_slots (MonoCompile *cfg)
1183 MonoCompileGC *gcfg = cfg->gc_info;
1186 /* Relative to the CFA */
1187 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1188 guint data = GPOINTER_TO_UINT (l->data);
1189 int cfa_slot = data >> 16;
1190 GCSlotType type = data & 0xff;
1194 * Map the cfa relative slot to an fp relative slot.
1195 * slot_addr == cfa - <cfa_slot>*4/8
1196 * fp + cfa_offset == cfa
1197 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1199 slot = (cfg->cfa_offset / sizeof (mgreg_t)) - cfa_slot - (gcfg->min_offset / sizeof (mgreg_t));
1201 set_slot_everywhere (gcfg, slot, type);
1203 if (cfg->verbose_level > 1) {
1204 int fp_offset = slot_to_fp_offset (cfg, slot);
1205 if (type == SLOT_NOREF)
1206 printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * sizeof (mgreg_t)));
1210 /* Relative to the FP */
1211 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1212 gint data = GPOINTER_TO_INT (l->data);
1213 int offset = data >> 16;
1214 GCSlotType type = data & 0xff;
1217 slot = fp_offset_to_slot (cfg, offset);
1219 set_slot_everywhere (gcfg, slot, type);
1221 /* Liveness for these slots is handled by process_spill_slots () */
1223 if (cfg->verbose_level > 1) {
1224 if (type == SLOT_REF)
1225 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1231 process_variables (MonoCompile *cfg)
1233 MonoCompileGC *gcfg = cfg->gc_info;
1234 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1235 int i, locals_min_slot, locals_max_slot, cindex;
1239 int locals_min_offset = gcfg->locals_min_offset;
1240 int locals_max_offset = gcfg->locals_max_offset;
1242 /* Slots for locals are NOREF by default */
1243 locals_min_slot = (locals_min_offset - gcfg->min_offset) / sizeof (mgreg_t);
1244 locals_max_slot = (locals_max_offset - gcfg->min_offset) / sizeof (mgreg_t);
1245 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1246 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1250 * Compute the offset where variables are initialized in the first bblock, if any.
1252 pc_offsets = g_new0 (int, cfg->next_vreg);
1254 bb = cfg->bb_entry->next_bb;
1255 MONO_BB_FOR_EACH_INS (bb, tmp) {
1256 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1257 int vreg = tmp->inst_c1;
1258 if (pc_offsets [vreg] == 0) {
1259 g_assert (tmp->backend.pc_offset > 0);
1260 pc_offsets [vreg] = tmp->backend.pc_offset;
1266 * Stack slots holding arguments are initialized in the prolog.
1267 * This means we can treat them alive for the whole method.
1269 for (i = 0; i < cfg->num_varinfo; i++) {
1270 MonoInst *ins = cfg->varinfo [i];
1271 MonoType *t = ins->inst_vtype;
1274 gboolean byref, is_this = FALSE;
1275 gboolean is_arg = i < cfg->locals_start;
1277 if (ins == cfg->ret)
1280 vmv = MONO_VARINFO (cfg, i);
1282 /* For some reason, 'this' is byref */
1283 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1284 t = &cfg->method->klass->byval_arg;
1290 if (ins->opcode == OP_REGVAR) {
1292 GCSlotType slot_type;
1294 t = mini_type_get_underlying_type (NULL, t);
1297 g_assert (hreg < MONO_MAX_IREGS);
1299 // FIXME: Add back this check
1301 if (is_arg && gcfg->reg_live_intervals [hreg]) {
1303 * FIXME: This argument shares a hreg with a local, we can't add the whole
1304 * method as a live interval, since it would overlap with the locals
1312 slot_type = SLOT_PIN;
1314 slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
1317 /* Live for the whole method */
1318 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1320 if (slot_type == SLOT_PIN) {
1321 /* These have no live interval, be conservative */
1322 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1325 * Unlike variables allocated to the stack, we generate liveness info
1326 * for noref vars in registers in mono_spill_global_vars (), because
1327 * knowing that a register doesn't contain a ref allows us to mark its save
1328 * locations precisely.
1330 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1331 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1332 set_reg_slot (gcfg, hreg, cindex, slot_type);
1336 if (cfg->verbose_level > 1) {
1337 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1343 if (ins->opcode != OP_REGOFFSET)
1346 if (ins->inst_offset % sizeof (mgreg_t) != 0)
1349 if (is_arg && ins->inst_offset >= gcfg->max_offset)
1350 /* In parent frame */
1353 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1355 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1356 /* These do not get stored in the prolog */
1357 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1359 if (cfg->verbose_level > 1) {
1360 printf ("\tdead arg at fp%s0x%x (slot=%d): %s\n", ins->inst_offset < 0 ? "-" : "+", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
1365 if (MONO_TYPE_ISSTRUCT (t)) {
1367 gsize *bitmap = NULL;
1368 gboolean pin = FALSE;
1372 if (ins->backend.is_pinvoke)
1373 size = mono_class_native_size (ins->klass, NULL);
1375 size = mono_class_value_size (ins->klass, NULL);
1376 size_in_slots = ALIGN_TO (size, sizeof (mgreg_t)) / sizeof (mgreg_t);
1378 if (!ins->klass->has_references) {
1380 for (j = 0; j < size_in_slots; ++j)
1381 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1386 if (ins->klass->generic_container || mono_class_is_open_constructed_type (t)) {
1387 /* FIXME: Generic sharing */
1390 mono_class_compute_gc_descriptor (ins->klass);
1392 bitmap = mono_gc_get_bitmap_for_descr (ins->klass->gc_descr, &numbits);
1399 * Most vtypes are marked volatile because of the LDADDR instructions,
1400 * and they have no liveness information since they are decomposed
1401 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1402 * them during VZERO decomposition.
1404 if (!pc_offsets [vmv->vreg])
1408 if (ins->backend.is_pinvoke)
1412 if (pc_offsets [vmv->vreg]) {
1413 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1414 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1415 for (j = 0; j < numbits; ++j) {
1416 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1417 /* The descriptor is for the boxed object */
1418 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (gpointer))), cindex, pin ? SLOT_PIN : SLOT_REF);
1425 for (j = 0; j < size_in_slots; ++j) {
1426 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1432 if (cfg->verbose_level > 1) {
1433 printf ("\tvtype R%d at fp+0x%x-0x%x: %s\n", vmv->vreg, (int)ins->inst_offset, (int)(ins->inst_offset + (size / sizeof (mgreg_t))), mono_type_full_name (ins->inst_vtype));
1439 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1440 /* Vret addr etc. */
1444 // FIXME: Not everywhere
1445 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1450 * This is currently disabled, but could be enabled to debug crashes.
1453 if (t->type == MONO_TYPE_I) {
1455 * Variables created in mono_handle_global_vregs have type I, but they
1456 * could hold GC refs since the vregs they were created from might not been
1457 * marked as holding a GC ref. So be conservative.
1459 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1464 t = mini_type_get_underlying_type (NULL, t);
1466 if (!MONO_TYPE_IS_REFERENCE (t)) {
1467 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1468 if (cfg->verbose_level > 1)
1469 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));
1473 /* 'this' is marked INDIRECT for gshared methods */
1474 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1476 * For volatile variables, treat them alive from the point they are
1477 * initialized in the first bblock until the end of the method.
1480 set_slot_everywhere (gcfg, pos, SLOT_REF);
1481 } else if (pc_offsets [vmv->vreg]) {
1482 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1483 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1485 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1487 if (cfg->verbose_level > 1)
1488 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));
1493 /* Live for the whole method */
1494 set_slot_everywhere (gcfg, pos, SLOT_REF);
1496 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1497 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1498 set_slot (gcfg, pos, cindex, SLOT_REF);
1501 if (cfg->verbose_level > 1) {
1502 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));
1506 g_free (pc_offsets);
1510 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1513 * Convert a sp relative offset to a slot index. This is
1514 * platform specific.
1517 /* fp = sp + offset */
1518 g_assert (cfg->frame_reg == AMD64_RBP);
1519 return (- cfg->arch.sp_fp_offset + sp_offset);
1527 type_to_gc_slot_type (MonoType *t)
1531 t = mini_type_get_underlying_type (NULL, t);
1532 if (MONO_TYPE_IS_REFERENCE (t))
1535 if (MONO_TYPE_ISSTRUCT (t)) {
1536 MonoClass *klass = mono_class_from_mono_type (t);
1537 if (!klass->has_references) {
1549 process_param_area_slots (MonoCompile *cfg)
1551 MonoCompileGC *gcfg = cfg->gc_info;
1556 * These slots are used for passing parameters during calls. They are sp relative, not
1557 * fp relative, so they are harder to handle.
1559 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1560 /* The distance between fp and sp is not constant */
1563 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1565 for (i = 0; i < gcfg->ncallsites; ++i) {
1566 GCCallSite *callsite = gcfg->callsites [i];
1569 for (l = callsite->param_slots; l; l = l->next) {
1570 MonoInst *def = l->data;
1571 int sp_offset = def->inst_offset;
1572 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1573 int slot = fp_offset_to_slot (cfg, fp_offset);
1575 g_assert (slot >= 0 && slot < gcfg->nslots);
1576 is_param [slot] = TRUE;
1580 /* All param area slots are noref by default */
1581 for (i = 0; i < gcfg->nslots; ++i) {
1583 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1586 for (i = 0; i < gcfg->ncallsites; ++i) {
1587 GCCallSite *callsite = gcfg->callsites [i];
1590 for (l = callsite->param_slots; l; l = l->next) {
1591 MonoInst *def = l->data;
1592 MonoType *t = def->inst_vtype;
1593 int sp_offset = def->inst_offset;
1594 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1595 int slot = fp_offset_to_slot (cfg, fp_offset);
1596 GCSlotType type = type_to_gc_slot_type (t);
1598 /* The slot is live between the def instruction and the call */
1599 set_slot_in_range (gcfg, slot, def->backend.pc_offset, callsite->pc_offset + 1, type);
1600 if (cfg->verbose_level > 1)
1601 printf ("\t%s param area slot at %s0x%x(fp)=0x%x(sp) (slot = %d) [0x%x-0x%x]\n", slot_type_to_string (type), fp_offset >= 0 ? "+" : "-", ABS (fp_offset), sp_offset, slot, def->backend.pc_offset, callsite->pc_offset + 1);
1607 compute_frame_size (MonoCompile *cfg)
1609 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
1610 int min_offset, max_offset;
1611 MonoCompileGC *gcfg = cfg->gc_info;
1612 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1615 /* Compute min/max offsets from the fp */
1619 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, sizeof (mgreg_t));
1620 locals_max_offset = cfg->locals_max_stack_offset;
1622 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
1626 locals_min_offset = ALIGN_TO (locals_min_offset, sizeof (mgreg_t));
1627 locals_max_offset = ALIGN_TO (locals_max_offset, sizeof (mgreg_t));
1629 min_offset = locals_min_offset;
1630 max_offset = locals_max_offset;
1633 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1634 MonoInst *ins = cfg->args [i];
1636 if (ins->opcode == OP_REGOFFSET)
1637 min_offset = MIN (min_offset, ins->inst_offset);
1641 g_assert (cfg->frame_reg == cfg->cfa_reg);
1642 g_assert (cfg->cfa_offset > 0);
1644 cfa_max_offset = cfg->cfa_offset;
1646 min_offset = MIN (min_offset, cfa_min_offset);
1647 max_offset = MAX (max_offset, cfa_max_offset);
1649 /* Fp relative slots */
1650 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1651 gint data = GPOINTER_TO_INT (l->data);
1652 int offset = data >> 16;
1654 min_offset = MIN (min_offset, offset);
1658 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
1659 int stack_offset = ALIGN_TO (cfg->stack_offset, sizeof (mgreg_t));
1660 min_offset = MIN (min_offset, (-stack_offset));
1663 /* Param area slots */
1665 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
1668 gcfg->min_offset = min_offset;
1669 gcfg->max_offset = max_offset;
1670 gcfg->locals_min_offset = locals_min_offset;
1671 gcfg->locals_max_offset = locals_max_offset;
1675 init_gcfg (MonoCompile *cfg)
1677 int i, nregs, nslots;
1678 MonoCompileGC *gcfg = cfg->gc_info;
1679 GCCallSite **callsites;
1688 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1689 ncallsites += g_slist_length (bb->gc_callsites);
1691 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
1693 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1694 for (l = bb->gc_callsites; l; l = l->next)
1695 callsites [i++] = l->data;
1698 /* The callsites should already be ordered by pc offset */
1699 for (i = 1; i < ncallsites; ++i)
1700 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
1703 * The stack frame looks like this:
1705 * <fp + max_offset> == cfa -> <end of previous frame>
1706 * <other stack slots>
1708 * <other stack slots>
1709 * fp + min_offset ->
1714 if (cfg->verbose_level > 1)
1715 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
1717 nslots = (gcfg->max_offset - gcfg->min_offset) / sizeof (mgreg_t);
1720 gcfg->nslots = nslots;
1721 gcfg->nregs = nregs;
1722 gcfg->callsites = callsites;
1723 gcfg->ncallsites = ncallsites;
1724 gcfg->bitmap_width = ALIGN_TO (nslots, 8) / 8;
1725 gcfg->reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
1726 gcfg->ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->bitmap_width * ncallsites);
1727 gcfg->pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->bitmap_width * ncallsites);
1728 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
1729 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
1731 /* All slots start out as PIN */
1732 memset (gcfg->pin_bitmap, 0xff, gcfg->bitmap_width * ncallsites);
1733 for (i = 0; i < nregs; ++i) {
1735 * By default, registers are PIN.
1736 * This is because we don't know their type outside their live range, since
1737 * they could have the same value as in the caller, or a value set by the
1738 * current method etc.
1740 if ((cfg->used_int_regs & (1 << i)))
1741 set_reg_slot_everywhere (gcfg, i, SLOT_PIN);
1747 create_map (MonoCompile *cfg)
1750 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
1752 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
1753 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
1755 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
1756 MonoCompileGC *gcfg = cfg->gc_info;
1757 GCCallSite **callsites;
1759 guint8 *bitmap, *bitmaps;
1760 guint32 reg_ref_mask, reg_pin_mask;
1762 ncallsites = gcfg->ncallsites;
1763 nslots = gcfg->nslots;
1764 nregs = gcfg->nregs;
1765 callsites = gcfg->callsites;
1768 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
1769 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
1771 has_ref_slots = FALSE;
1772 has_pin_slots = FALSE;
1775 memset (ntypes, 0, sizeof (ntypes));
1776 for (i = 0; i < nslots; ++i) {
1777 gboolean has_ref = FALSE;
1778 gboolean has_pin = FALSE;
1780 for (j = 0; j < ncallsites; ++j) {
1781 if (get_bit (gcfg->pin_bitmap, gcfg->bitmap_width, j, i))
1783 if (get_bit (gcfg->ref_bitmap, gcfg->bitmap_width, j, i))
1788 has_ref_slots = TRUE;
1790 has_pin_slots = TRUE;
1793 ntypes [SLOT_REF] ++;
1795 ntypes [SLOT_PIN] ++;
1797 ntypes [SLOT_NOREF] ++;
1799 if (has_ref || has_pin) {
1806 start = end = nslots;
1808 g_assert (start != -1);
1809 g_assert (start < end);
1812 has_ref_regs = FALSE;
1813 has_pin_regs = FALSE;
1818 for (i = 0; i < nregs; ++i) {
1819 gboolean has_ref = FALSE;
1820 gboolean has_pin = FALSE;
1822 for (j = 0; j < ncallsites; ++j) {
1823 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i)) {
1828 for (j = 0; j < ncallsites; ++j) {
1829 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i)) {
1836 reg_ref_mask |= (1 << i);
1837 has_ref_regs = TRUE;
1841 reg_pin_mask |= (1 << i);
1842 has_pin_regs = TRUE;
1847 if (cfg->verbose_level > 1)
1848 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);
1850 /* Create the GC Map */
1852 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
1853 stack_bitmap_size = stack_bitmap_width * ncallsites;
1854 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
1855 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
1856 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
1857 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
1858 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);
1860 map = mono_mempool_alloc (cfg->mempool, sizeof (GCMap));
1862 map->frame_reg = cfg->frame_reg;
1863 map->start_offset = gcfg->min_offset;
1864 map->end_offset = gcfg->min_offset + (nslots * sizeof (mgreg_t));
1865 map->map_offset = start * sizeof (mgreg_t);
1866 map->nslots = end - start;
1867 map->has_ref_slots = has_ref_slots;
1868 map->has_pin_slots = has_pin_slots;
1869 map->has_ref_regs = has_ref_regs;
1870 map->has_pin_regs = has_pin_regs;
1871 g_assert (nregs < 32);
1872 map->reg_ref_mask = reg_ref_mask;
1873 map->reg_pin_mask = reg_pin_mask;
1874 map->nref_regs = nref_regs;
1875 map->npin_regs = npin_regs;
1877 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
1880 if (has_ref_slots) {
1881 map->stack_ref_bitmap_offset = bitmaps_offset;
1882 bitmaps_offset += stack_bitmap_size;
1884 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
1885 for (i = 0; i < nslots; ++i) {
1886 for (j = 0; j < ncallsites; ++j) {
1887 if (get_bit (gcfg->ref_bitmap, gcfg->bitmap_width, j, i))
1888 set_bit (bitmap, stack_bitmap_width, j, i - start);
1892 if (has_pin_slots) {
1893 map->stack_pin_bitmap_offset = bitmaps_offset;
1894 bitmaps_offset += stack_bitmap_size;
1896 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
1897 for (i = 0; i < nslots; ++i) {
1898 for (j = 0; j < ncallsites; ++j) {
1899 if (get_bit (gcfg->pin_bitmap, gcfg->bitmap_width, j, i))
1900 set_bit (bitmap, stack_bitmap_width, j, i - start);
1905 map->reg_ref_bitmap_offset = bitmaps_offset;
1906 bitmaps_offset += reg_ref_bitmap_size;
1908 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
1910 for (i = 0; i < nregs; ++i) {
1911 if (reg_ref_mask & (1 << i)) {
1912 for (j = 0; j < ncallsites; ++j) {
1913 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i))
1914 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
1921 map->reg_pin_bitmap_offset = bitmaps_offset;
1922 bitmaps_offset += reg_pin_bitmap_size;
1924 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
1926 for (i = 0; i < nregs; ++i) {
1927 if (reg_pin_mask & (1 << i)) {
1928 for (j = 0; j < ncallsites; ++j) {
1929 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i))
1930 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
1938 map->ncallsites = ncallsites;
1939 if (cfg->code_len < 256)
1940 map->callsite_entry_size = 1;
1941 else if (cfg->code_len < 65536)
1942 map->callsite_entry_size = 2;
1944 map->callsite_entry_size = 4;
1946 /* Encode the GC Map */
1954 encode_gc_map (map, buf, &endbuf);
1955 g_assert (endbuf - buf < 256);
1957 encoded_size = endbuf - buf;
1958 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
1960 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
1961 //emap->ref_slots = map->ref_slots;
1963 /* Encoded fixed fields */
1964 p = &emap->encoded [0];
1965 //emap->encoded_size = encoded_size;
1966 memcpy (p, buf, encoded_size);
1969 /* Callsite table */
1970 p = (guint8*)ALIGN_TO ((guint64)p, map->callsite_entry_size);
1971 if (map->callsite_entry_size == 1) {
1972 guint8 *offsets = p;
1973 for (i = 0; i < ncallsites; ++i)
1974 offsets [i] = callsites [i]->pc_offset;
1975 stats.gc_callsites8_num ++;
1976 } else if (map->callsite_entry_size == 2) {
1977 guint16 *offsets = (guint16*)p;
1978 for (i = 0; i < ncallsites; ++i)
1979 offsets [i] = callsites [i]->pc_offset;
1980 stats.gc_callsites16_num ++;
1982 guint32 *offsets = (guint32*)p;
1983 for (i = 0; i < ncallsites; ++i)
1984 offsets [i] = callsites [i]->pc_offset;
1985 stats.gc_callsites32_num ++;
1987 p += ncallsites * map->callsite_entry_size;
1990 memcpy (p, bitmaps, bitmaps_size);
1992 stats.gc_maps_size += alloc_size;
1993 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
1994 stats.gc_bitmaps_size += bitmaps_size;
1995 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
1997 cfg->jit_info->gc_info = emap;
2000 stats.all_slots += nslots;
2001 stats.ref_slots += ntypes [SLOT_REF];
2002 stats.noref_slots += ntypes [SLOT_NOREF];
2003 stats.pin_slots += ntypes [SLOT_PIN];
2007 mini_gc_create_gc_map (MonoCompile *cfg)
2009 if (!cfg->compute_gc_maps)
2013 * During marking, all frames except the top frame are at a call site, and we mark the
2014 * top frame conservatively. This means that we only need to compute and record
2015 * GC maps for call sites.
2018 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2019 /* Without liveness info, the live ranges are not precise enough */
2022 if (cfg->header->num_clauses)
2024 * The calls to the finally clauses don't show up in the cfg. See
2025 * test_0_liveness_8 ().
2029 mono_analyze_liveness_gc (cfg);
2031 compute_frame_size (cfg);
2035 process_spill_slots (cfg);
2036 process_other_slots (cfg);
2037 process_param_area_slots (cfg);
2038 process_variables (cfg);
2048 memset (&cb, 0, sizeof (cb));
2049 cb.thread_attach_func = thread_attach_func;
2050 cb.thread_suspend_func = thread_suspend_func;
2051 /* Comment this out to disable precise stack marking */
2052 cb.thread_mark_func = thread_mark_func;
2053 mono_gc_set_gc_callbacks (&cb);
2055 mono_counters_register ("GC Maps size",
2056 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2057 mono_counters_register ("GC Call Sites size",
2058 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2059 mono_counters_register ("GC Bitmaps size",
2060 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2061 mono_counters_register ("GC Map struct size",
2062 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2063 mono_counters_register ("GC Call Sites encoded using 8 bits",
2064 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_num);
2065 mono_counters_register ("GC Call Sites encoded using 16 bits",
2066 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_num);
2067 mono_counters_register ("GC Call Sites encoded using 32 bits",
2068 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_num);
2070 mono_counters_register ("GC Map slots (all)",
2071 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2072 mono_counters_register ("GC Map slots (ref)",
2073 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2074 mono_counters_register ("GC Map slots (noref)",
2075 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2076 mono_counters_register ("GC Map slots (pin)",
2077 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2079 mono_counters_register ("Stack space scanned (all)",
2080 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2081 mono_counters_register ("Stack space scanned (using GC Maps)",
2082 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2083 mono_counters_register ("Stack space scanned (precise)",
2084 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2085 mono_counters_register ("Stack space scanned (pin)",
2086 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2087 mono_counters_register ("Stack space scanned (pin registers)",
2088 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2099 mini_gc_init_gc_map (MonoCompile *cfg)
2104 mini_gc_create_gc_map (MonoCompile *cfg)
2109 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2114 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2123 * Set GC specific options in CFG.
2126 mini_gc_init_cfg (MonoCompile *cfg)
2128 if (mono_gc_is_moving ()) {
2129 cfg->disable_ref_noref_stack_slot_share = TRUE;
2130 cfg->gen_write_barriers = TRUE;
2133 mini_gc_init_gc_map (cfg);
2137 * Problems with the current code:
2138 * - the stack walk is slow
2139 * - vtypes/refs used in EH regions are treated conservatively
2140 * - if the code is finished, less pinning will be done, causing problems because
2141 * we promote all surviving objects to old-gen.