[interp] Reduce computation under calc_section mutex
[mono-project.git] / mono / mini / mini-gc.c
blobde6626a3367d58a9cb3ae196eaa3022b7d39f394
1 /**
2 * \file
3 * GC interface for the mono JIT
5 * Author:
6 * Zoltan Varga (vargaz@gmail.com)
8 * Copyright 2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include "config.h"
14 #include "mini-gc.h"
15 #include "mini-runtime.h"
16 #include <mono/metadata/gc-internals.h>
18 static gboolean
19 get_provenance (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
21 MonoJitInfo *ji = frame->ji;
22 MonoMethod *method;
23 if (!ji)
24 return FALSE;
25 method = jinfo_get_method (ji);
26 if (method->wrapper_type != MONO_WRAPPER_NONE)
27 return FALSE;
28 *(gpointer *)data = method;
29 return TRUE;
32 static gpointer
33 get_provenance_func (void)
35 gpointer provenance = NULL;
36 mono_walk_stack (get_provenance, MONO_UNWIND_DEFAULT, (gpointer)&provenance);
37 return provenance;
40 #if 0
41 //#if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
43 #include <mono/metadata/sgen-conf.h>
44 #include <mono/metadata/gc-internals.h>
45 #include <mono/utils/mono-counters.h>
46 #include <mono/utils/unlocked.h>
48 //#define SIZEOF_SLOT ((int)sizeof (host_mgreg_t))
49 //#define SIZEOF_SLOT ((int)sizeof (target_mgreg_t))
51 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
53 /* Contains state needed by the GC Map construction code */
54 typedef struct {
56 * This contains information about stack slots initialized in the prolog, encoded using
57 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
58 * means cfa+0, 1 means cfa-4/8, etc.
60 GSList *stack_slots_from_cfa;
61 /* Same for stack slots relative to the frame pointer */
62 GSList *stack_slots_from_fp;
64 /* Number of slots in the map */
65 int nslots;
66 /* The number of registers in the map */
67 int nregs;
68 /* Min and Max offsets of the stack frame relative to fp */
69 int min_offset, max_offset;
70 /* Same for the locals area */
71 int locals_min_offset, locals_max_offset;
73 /* The call sites where this frame can be stopped during GC */
74 GCCallSite **callsites;
75 /* The number of call sites */
76 int ncallsites;
79 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
80 * runtime, since it includes columns which are 0.
82 int stack_bitmap_width;
83 /*
84 * A bitmap whose width equals nslots, and whose height equals ncallsites.
85 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
86 * given callsite.
88 guint8 *stack_ref_bitmap;
89 /* Same for SLOT_PIN */
90 guint8 *stack_pin_bitmap;
93 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
95 int reg_bitmap_width;
96 guint8 *reg_ref_bitmap;
97 guint8 *reg_pin_bitmap;
98 } MonoCompileGC;
100 #undef DEBUG
102 #if 0
103 /* We don't support debug levels, its all-or-nothing */
104 #define DEBUG(s) do { s; fflush (logfile); } while (0)
105 #define DEBUG_ENABLED 1
106 #else
107 #define DEBUG(s)
108 #endif
110 #ifdef DEBUG_ENABLED
111 //#if 1
112 #define DEBUG_PRECISE(s) do { s; } while (0)
113 #define DEBUG_PRECISE_ENABLED
114 #else
115 #define DEBUG_PRECISE(s)
116 #endif
119 * Contains information collected during the conservative stack marking pass,
120 * used during the precise pass. This helps to avoid doing a stack walk twice, which
121 * is expensive.
123 typedef struct {
124 guint8 *bitmap;
125 int nslots;
126 int frame_start_offset;
127 int nreg_locations;
128 /* Relative to stack_start */
129 int reg_locations [MONO_MAX_IREGS];
130 #ifdef DEBUG_PRECISE_ENABLED
131 MonoJitInfo *ji;
132 gpointer fp;
133 int regs [MONO_MAX_IREGS];
134 #endif
135 } FrameInfo;
137 /* Max number of frames stored in the TLS data */
138 #define MAX_FRAMES 50
141 * Per-thread data kept by this module. This is stored in the GC and passed to us as
142 * parameters, instead of being stored in a TLS variable, since during a collection,
143 * only the collection thread is active.
145 typedef struct {
146 MonoThreadUnwindState unwind_state;
147 MonoThreadInfo *info;
148 /* For debugging */
149 host_mgreg_t tid;
150 gpointer ref_to_track;
151 /* Number of frames collected during the !precise pass */
152 int nframes;
153 FrameInfo frames [MAX_FRAMES];
154 } TlsData;
156 /* These are constant so don't store them in the GC Maps */
157 /* Number of registers stored in gc maps */
158 #define NREGS MONO_MAX_IREGS
161 * The GC Map itself.
162 * Contains information needed to mark a stack frame.
163 * This is a transient structure, created from a compressed representation on-demand.
165 typedef struct {
167 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
168 * This includes memory which is NOREF thus doesn't need GC maps.
170 int start_offset;
171 int end_offset;
173 * The offset relative to frame_offset where the the memory described by the GC maps
174 * begins.
176 int map_offset;
177 /* The number of stack slots in the map */
178 int nslots;
179 /* The frame pointer register */
180 guint8 frame_reg;
181 /* The size of each callsite table entry */
182 guint8 callsite_entry_size;
183 guint has_pin_slots : 1;
184 guint has_ref_slots : 1;
185 guint has_ref_regs : 1;
186 guint has_pin_regs : 1;
188 /* The offsets below are into an external bitmaps array */
191 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
192 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
193 * given callsite.
195 guint32 stack_ref_bitmap_offset;
197 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
198 * different callsites, if the slot starts out as PIN, and later changes to REF.
200 guint32 stack_pin_bitmap_offset;
203 * Corresponding bitmaps for registers
204 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
205 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
207 guint32 reg_pin_bitmap_offset;
208 guint32 reg_ref_bitmap_offset;
210 guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
212 /* The number of bits set in the two masks above */
213 guint8 nref_regs, npin_regs;
216 * A bit array marking slots which contain refs.
217 * This is used only for debugging.
219 //guint8 *ref_slots;
221 /* Callsite offsets */
222 /* These can take up a lot of space, so encode them compactly */
223 union {
224 guint8 *offsets8;
225 guint16 *offsets16;
226 guint32 *offsets32;
227 } callsites;
228 int ncallsites;
229 } GCMap;
232 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
234 typedef struct {
235 //guint8 *ref_slots;
236 //guint8 encoded_size;
239 * The arrays below are embedded after the struct.
240 * Their address needs to be computed.
243 /* The fixed fields of the GCMap encoded using LEB128 */
244 guint8 encoded [MONO_ZERO_LEN_ARRAY];
246 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
247 guint8 callsites [MONO_ZERO_LEN_ARRAY];
249 /* The GC bitmaps */
250 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
251 } GCEncodedMap;
253 static int precise_frame_count [2], precise_frame_limit = -1;
254 static gboolean precise_frame_limit_inited;
256 /* Stats */
257 typedef struct {
258 gint32 scanned_stacks;
259 gint32 scanned;
260 gint32 scanned_precisely;
261 gint32 scanned_conservatively;
262 gint32 scanned_registers;
263 gint32 scanned_native;
264 gint32 scanned_other;
266 gint32 all_slots;
267 gint32 noref_slots;
268 gint32 ref_slots;
269 gint32 pin_slots;
271 gint32 gc_maps_size;
272 gint32 gc_callsites_size;
273 gint32 gc_callsites8_size;
274 gint32 gc_callsites16_size;
275 gint32 gc_callsites32_size;
276 gint32 gc_bitmaps_size;
277 gint32 gc_map_struct_size;
278 gint32 tlsdata_size;
279 } JITGCStats;
281 static JITGCStats stats;
283 static FILE *logfile;
285 static gboolean enable_gc_maps_for_aot;
287 void
288 mini_gc_enable_gc_maps_for_aot (void)
290 enable_gc_maps_for_aot = TRUE;
293 // FIXME: Move these to a shared place
295 static inline void
296 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
298 guint8 *p = buf;
300 do {
301 guint8 b = value & 0x7f;
302 value >>= 7;
303 if (value != 0) /* more bytes to come */
304 b |= 0x80;
305 *p ++ = b;
306 } while (value);
308 *endbuf = p;
311 static G_GNUC_UNUSED void
312 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
314 gboolean more = 1;
315 gboolean negative = (value < 0);
316 guint32 size = 32;
317 guint8 byte;
318 guint8 *p = buf;
320 while (more) {
321 byte = value & 0x7f;
322 value >>= 7;
323 /* the following is unnecessary if the
324 * implementation of >>= uses an arithmetic rather
325 * than logical shift for a signed left operand
327 if (negative)
328 /* sign extend */
329 value |= - (1 <<(size - 7));
330 /* sign bit of byte is second high order bit (0x40) */
331 if ((value == 0 && !(byte & 0x40)) ||
332 (value == -1 && (byte & 0x40)))
333 more = 0;
334 else
335 byte |= 0x80;
336 *p ++= byte;
339 *endbuf = p;
342 static inline guint32
343 decode_uleb128 (guint8 *buf, guint8 **endbuf)
345 guint8 *p = buf;
346 guint32 res = 0;
347 int shift = 0;
349 while (TRUE) {
350 guint8 b = *p;
351 p ++;
353 res = res | (((int)(b & 0x7f)) << shift);
354 if (!(b & 0x80))
355 break;
356 shift += 7;
359 *endbuf = p;
361 return res;
364 static inline gint32
365 decode_sleb128 (guint8 *buf, guint8 **endbuf)
367 guint8 *p = buf;
368 gint32 res = 0;
369 int shift = 0;
371 while (TRUE) {
372 guint8 b = *p;
373 p ++;
375 res = res | (((int)(b & 0x7f)) << shift);
376 shift += 7;
377 if (!(b & 0x80)) {
378 if (shift < 32 && (b & 0x40))
379 res |= - (1 << shift);
380 break;
384 *endbuf = p;
386 return res;
389 static int
390 encode_frame_reg (int frame_reg)
392 #ifdef TARGET_AMD64
393 if (frame_reg == AMD64_RSP)
394 return 0;
395 else if (frame_reg == AMD64_RBP)
396 return 1;
397 #elif defined(TARGET_X86)
398 if (frame_reg == X86_EBP)
399 return 0;
400 else if (frame_reg == X86_ESP)
401 return 1;
402 #elif defined(TARGET_ARM)
403 if (frame_reg == ARMREG_SP)
404 return 0;
405 else if (frame_reg == ARMREG_FP)
406 return 1;
407 #elif defined(TARGET_S390X)
408 if (frame_reg == S390_SP)
409 return 0;
410 else if (frame_reg == S390_FP)
411 return 1;
412 #elif defined (TARGET_RISCV)
413 if (frame_reg == RISCV_SP)
414 return 0;
415 else if (frame_reg == RISCV_FP)
416 return 1;
417 #else
418 NOT_IMPLEMENTED;
419 #endif
420 g_assert_not_reached ();
421 return -1;
424 static int
425 decode_frame_reg (int encoded)
427 #ifdef TARGET_AMD64
428 if (encoded == 0)
429 return AMD64_RSP;
430 else if (encoded == 1)
431 return AMD64_RBP;
432 #elif defined(TARGET_X86)
433 if (encoded == 0)
434 return X86_EBP;
435 else if (encoded == 1)
436 return X86_ESP;
437 #elif defined(TARGET_ARM)
438 if (encoded == 0)
439 return ARMREG_SP;
440 else if (encoded == 1)
441 return ARMREG_FP;
442 #elif defined(TARGET_S390X)
443 if (encoded == 0)
444 return S390_SP;
445 else if (encoded == 1)
446 return S390_FP;
447 #elif defined (TARGET_RISCV)
448 if (encoded == 0)
449 return RISCV_SP;
450 else if (encoded == 1)
451 return RISCV_FP;
452 #else
453 NOT_IMPLEMENTED;
454 #endif
455 g_assert_not_reached ();
456 return -1;
459 #ifdef TARGET_AMD64
460 #ifdef HOST_WIN32
461 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
462 #else
463 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
464 #endif
465 #elif defined(TARGET_X86)
466 static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
467 #elif defined(TARGET_ARM)
468 static int callee_saved_regs [] = { ARMREG_V1, ARMREG_V2, ARMREG_V3, ARMREG_V4, ARMREG_V5, ARMREG_V7, ARMREG_FP };
469 #elif defined(TARGET_ARM64)
470 // FIXME:
471 static int callee_saved_regs [] = { };
472 #elif defined(TARGET_S390X)
473 static int callee_saved_regs [] = { s390_r6, s390_r7, s390_r8, s390_r9, s390_r10, s390_r11, s390_r12, s390_r13, s390_r14 };
474 #elif defined(TARGET_POWERPC64) && _CALL_ELF == 2
475 static int callee_saved_regs [] = {
476 ppc_r13, ppc_r14, ppc_r15, ppc_r16,
477 ppc_r17, ppc_r18, ppc_r19, ppc_r20,
478 ppc_r21, ppc_r22, ppc_r23, ppc_r24,
479 ppc_r25, ppc_r26, ppc_r27, ppc_r28,
480 ppc_r29, ppc_r30, ppc_r31 };
481 #elif defined(TARGET_POWERPC)
482 static int callee_saved_regs [] = { ppc_r6, ppc_r7, ppc_r8, ppc_r9, ppc_r10, ppc_r11, ppc_r12, ppc_r13, ppc_r14 };
483 #elif defined (TARGET_RISCV)
484 static int callee_saved_regs [] = {
485 RISCV_S0, RISCV_S1, RISCV_S2, RISCV_S3, RISCV_S4, RISCV_S5,
486 RISCV_S6, RISCV_S7, RISCV_S8, RISCV_S9, RISCV_S10, RISCV_S11,
488 #endif
490 static guint32
491 encode_regmask (guint32 regmask)
493 int i;
494 guint32 res;
496 res = 0;
497 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
498 if (regmask & (1 << callee_saved_regs [i])) {
499 res |= (1 << i);
500 regmask -= (1 << callee_saved_regs [i]);
503 g_assert (regmask == 0);
504 return res;
507 static guint32
508 decode_regmask (guint32 regmask)
510 int i;
511 guint32 res;
513 res = 0;
514 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
515 if (regmask & (1 << i))
516 res |= (1 << callee_saved_regs [i]);
517 return res;
521 * encode_gc_map:
523 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
525 static void
526 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
528 guint32 flags, freg;
530 encode_sleb128 (map->start_offset / SIZEOF_SLOT, buf, &buf);
531 encode_sleb128 (map->end_offset / SIZEOF_SLOT, buf, &buf);
532 encode_sleb128 (map->map_offset / SIZEOF_SLOT, buf, &buf);
533 encode_uleb128 (map->nslots, buf, &buf);
534 g_assert (map->callsite_entry_size <= 4);
535 freg = encode_frame_reg (map->frame_reg);
536 g_assert (freg < 2);
537 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);
538 encode_uleb128 (flags, buf, &buf);
539 encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
540 if (map->has_ref_regs)
541 encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
542 if (map->has_pin_regs)
543 encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
544 encode_uleb128 (map->ncallsites, buf, &buf);
546 *endbuf = buf;
550 * decode_gc_map:
552 * Decode the encoded GC map representation in BUF and store the result into MAP.
554 static void
555 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
557 guint32 flags;
558 int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
559 int i, n;
561 map->start_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
562 map->end_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
563 map->map_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
564 map->nslots = decode_uleb128 (buf, &buf);
565 flags = decode_uleb128 (buf, &buf);
566 map->has_ref_slots = (flags & 1) ? 1 : 0;
567 map->has_pin_slots = (flags & 2) ? 1 : 0;
568 map->has_ref_regs = (flags & 4) ? 1 : 0;
569 map->has_pin_regs = (flags & 8) ? 1 : 0;
570 map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
571 freg = flags >> 6;
572 map->frame_reg = decode_frame_reg (freg);
573 map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
574 if (map->has_ref_regs) {
575 map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
576 n = 0;
577 for (i = 0; i < NREGS; ++i)
578 if (map->reg_ref_mask & (1 << i))
579 n ++;
580 map->nref_regs = n;
582 if (map->has_pin_regs) {
583 map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
584 n = 0;
585 for (i = 0; i < NREGS; ++i)
586 if (map->reg_pin_mask & (1 << i))
587 n ++;
588 map->npin_regs = n;
590 map->ncallsites = decode_uleb128 (buf, &buf);
592 stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
593 reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
594 reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
595 offset = 0;
596 map->stack_ref_bitmap_offset = offset;
597 if (map->has_ref_slots)
598 offset += stack_bitmap_size;
599 map->stack_pin_bitmap_offset = offset;
600 if (map->has_pin_slots)
601 offset += stack_bitmap_size;
602 map->reg_ref_bitmap_offset = offset;
603 if (map->has_ref_regs)
604 offset += reg_ref_bitmap_size;
605 map->reg_pin_bitmap_offset = offset;
606 if (map->has_pin_regs)
607 offset += reg_pin_bitmap_size;
609 *endbuf = buf;
612 static gpointer
613 thread_attach_func (void)
615 TlsData *tls;
617 tls = g_new0 (TlsData, 1);
618 tls->tid = mono_native_thread_id_get ();
619 tls->info = mono_thread_info_current ();
620 UnlockedAdd (&stats.tlsdata_size, sizeof (TlsData));
622 return tls;
625 static void
626 thread_detach_func (gpointer user_data)
628 TlsData *tls = user_data;
630 g_free (tls);
633 static void
634 thread_suspend_func (gpointer user_data, void *sigctx, MonoContext *ctx)
636 TlsData *tls = user_data;
638 if (!tls) {
639 /* Happens during startup */
640 return;
643 if (tls->tid != mono_native_thread_id_get ()) {
644 /* Happens on osx because threads are not suspended using signals */
645 #ifndef TARGET_WIN32
646 gboolean res;
647 #endif
649 g_assert (tls->info);
650 #ifdef TARGET_WIN32
651 return;
652 #else
653 res = mono_thread_state_init_from_handle (&tls->unwind_state, tls->info, NULL);
654 #endif
655 } else {
656 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
657 if (sigctx) {
658 mono_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx);
659 tls->unwind_state.valid = TRUE;
660 } else if (ctx) {
661 memcpy (&tls->unwind_state.ctx, ctx, sizeof (MonoContext));
662 tls->unwind_state.valid = TRUE;
663 } else {
664 tls->unwind_state.valid = FALSE;
666 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS] = mono_tls_get_jit_tls ();
667 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
670 if (!tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN]) {
671 /* Happens during startup */
672 tls->unwind_state.valid = FALSE;
673 return;
677 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
679 static inline void
680 set_bit (guint8 *bitmap, int width, int y, int x)
682 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
685 static inline void
686 clear_bit (guint8 *bitmap, int width, int y, int x)
688 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
691 static inline int
692 get_bit (guint8 *bitmap, int width, int y, int x)
694 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
697 static const char*
698 slot_type_to_string (GCSlotType type)
700 switch (type) {
701 case SLOT_REF:
702 return "ref";
703 case SLOT_NOREF:
704 return "noref";
705 case SLOT_PIN:
706 return "pin";
707 default:
708 g_assert_not_reached ();
709 return NULL;
713 static inline host_mgreg_t
714 get_frame_pointer (MonoContext *ctx, int frame_reg)
716 #if defined(TARGET_AMD64)
717 if (frame_reg == AMD64_RSP)
718 return ctx->rsp;
719 else if (frame_reg == AMD64_RBP)
720 return ctx->rbp;
721 #elif defined(TARGET_X86)
722 if (frame_reg == X86_ESP)
723 return ctx->esp;
724 else if (frame_reg == X86_EBP)
725 return ctx->ebp;
726 #elif defined(TARGET_ARM)
727 if (frame_reg == ARMREG_SP)
728 return (host_mgreg_t)MONO_CONTEXT_GET_SP (ctx);
729 else if (frame_reg == ARMREG_FP)
730 return (host_mgreg_t)MONO_CONTEXT_GET_BP (ctx);
731 #elif defined(TARGET_S390X)
732 if (frame_reg == S390_SP)
733 return (host_mgreg_t)MONO_CONTEXT_GET_SP (ctx);
734 else if (frame_reg == S390_FP)
735 return (host_mgreg_t)MONO_CONTEXT_GET_BP (ctx);
736 #elif defined (TARGET_RISCV)
737 if (frame_reg == RISCV_SP)
738 return MONO_CONTEXT_GET_SP (ctx);
739 else if (frame_reg == RISCV_FP)
740 return MONO_CONTEXT_GET_BP (ctx);
741 #endif
742 g_assert_not_reached ();
743 return 0;
747 * conservatively_pass:
749 * Mark a thread stack conservatively and collect information needed by the precise pass.
751 static void
752 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
754 MonoJitInfo *ji;
755 MonoMethod *method;
756 MonoContext ctx, new_ctx;
757 MonoLMF *lmf;
758 guint8 *stack_limit;
759 gboolean last = TRUE;
760 GCMap *map;
761 GCMap map_tmp;
762 GCEncodedMap *emap;
763 guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
764 int i, pc_offset, cindex, bitmap_width;
765 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
766 gboolean res;
767 StackFrameInfo frame;
768 host_mgreg_t *reg_locations [MONO_MAX_IREGS];
769 host_mgreg_t *new_reg_locations [MONO_MAX_IREGS];
770 guint8 *bitmaps;
771 FrameInfo *fi;
772 guint32 precise_regmask;
774 if (tls) {
775 tls->nframes = 0;
776 tls->ref_to_track = NULL;
779 /* tls == NULL can happen during startup */
780 if (mono_thread_internal_current () == NULL || !tls) {
781 mono_gc_conservatively_scan_area (stack_start, stack_end);
782 UnlockedAdd (&stats.scanned_stacks, stack_end - stack_start);
783 return;
786 lmf = tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF];
787 frame.domain = NULL;
789 /* Number of bytes scanned based on GC map data */
790 scanned = 0;
791 /* Number of bytes scanned precisely based on GC map data */
792 scanned_precisely = 0;
793 /* Number of bytes scanned conservatively based on GC map data */
794 scanned_conservatively = 0;
795 /* Number of bytes scanned conservatively in register save areas */
796 scanned_registers = 0;
798 /* This is one past the last address which we have scanned */
799 stack_limit = stack_start;
801 if (!tls->unwind_state.valid)
802 memset (&new_ctx, 0, sizeof (ctx));
803 else
804 memcpy (&new_ctx, &tls->unwind_state.ctx, sizeof (MonoContext));
806 memset (reg_locations, 0, sizeof (reg_locations));
807 memset (new_reg_locations, 0, sizeof (new_reg_locations));
809 while (TRUE) {
810 if (!tls->unwind_state.valid)
811 break;
813 memcpy (&ctx, &new_ctx, sizeof (ctx));
815 for (i = 0; i < MONO_MAX_IREGS; ++i) {
816 if (new_reg_locations [i]) {
818 * If the current frame saves the register, it means it might modify its
819 * value, thus the old location might not contain the same value, so
820 * we have to mark it conservatively.
822 if (reg_locations [i]) {
823 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
824 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
825 scanned_registers += SIZEOF_SLOT;
828 reg_locations [i] = new_reg_locations [i];
830 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
834 g_assert ((gsize)stack_limit % SIZEOF_SLOT == 0);
836 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);
837 if (!res)
838 break;
840 ji = frame.ji;
842 // FIXME: For skipped frames, scan the param area of the parent frame conservatively ?
843 // FIXME: trampolines
845 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
847 * These frames are problematic for several reasons:
848 * - they are unwound through an LMF, and we have no precise register tracking for those.
849 * - the LMF might not contain a precise ip, so we can't compute the call site.
850 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
852 DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
853 for (i = 0; i < MONO_MAX_IREGS; ++i) {
854 if (reg_locations [i]) {
855 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
856 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
857 scanned_registers += SIZEOF_SLOT;
859 reg_locations [i] = NULL;
860 new_reg_locations [i] = NULL;
862 ctx = new_ctx;
863 continue;
866 if (ji)
867 method = jinfo_get_method (ji);
868 else
869 method = NULL;
871 /* The last frame can be in any state so mark conservatively */
872 if (last) {
873 if (ji) {
874 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));
876 DEBUG (fprintf (logfile, "\t <Last frame>\n"));
877 last = FALSE;
879 * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
881 for (i = 0; i < MONO_MAX_IREGS; ++i) {
882 if (reg_locations [i]) {
883 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
884 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
885 scanned_registers += SIZEOF_SLOT;
887 if (new_reg_locations [i]) {
888 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), new_reg_locations [i]));
889 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
890 scanned_registers += SIZEOF_SLOT;
892 reg_locations [i] = NULL;
893 new_reg_locations [i] = NULL;
895 continue;
898 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
900 /* These frames are very problematic */
901 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
902 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));
903 DEBUG (fprintf (logfile, "\tSkip.\n"));
904 continue;
907 /* All the other frames are at a call site */
909 if (tls->nframes == MAX_FRAMES) {
911 * Can't save information since the array is full. So scan the rest of the
912 * stack conservatively.
914 DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
915 break;
918 /* Scan the frame of this method */
921 * A frame contains the following:
922 * - saved registers
923 * - saved args
924 * - locals
925 * - spill area
926 * - localloc-ed memory
928 g_assert (pc_offset >= 0);
930 emap = ji->gc_info;
932 if (!emap) {
933 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));
934 DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
935 continue;
938 /* The embedded callsite table requires this */
939 g_assert (((gsize)emap % 4) == 0);
942 * Debugging aid to control the number of frames scanned precisely
944 if (!precise_frame_limit_inited) {
945 char *mono_precise_count = g_getenv ("MONO_PRECISE_COUNT");
946 if (mono_precise_count) {
947 precise_frame_limit = atoi (mono_precise_count);
948 g_free (mono_precise_count);
950 precise_frame_limit_inited = TRUE;
953 if (precise_frame_limit != -1) {
954 if (precise_frame_count [FALSE] == precise_frame_limit)
955 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (method, TRUE));
956 if (precise_frame_count [FALSE] > precise_frame_limit)
957 continue;
959 precise_frame_count [FALSE] ++;
961 /* Decode the encoded GC map */
962 map = &map_tmp;
963 memset (map, 0, sizeof (GCMap));
964 decode_gc_map (&emap->encoded [0], map, &p);
965 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
966 map->callsites.offsets8 = p;
967 p += map->callsite_entry_size * map->ncallsites;
968 bitmaps = p;
970 fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
972 real_frame_start = fp + map->start_offset;
973 frame_start = fp + map->start_offset + map->map_offset;
974 frame_end = fp + map->end_offset;
976 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));
978 /* Find the callsite index */
979 if (map->callsite_entry_size == 1) {
980 for (i = 0; i < map->ncallsites; ++i)
981 /* ip points inside the call instruction */
982 if (map->callsites.offsets8 [i] == pc_offset + 1)
983 break;
984 } else if (map->callsite_entry_size == 2) {
985 // FIXME: Use a binary search
986 for (i = 0; i < map->ncallsites; ++i)
987 /* ip points inside the call instruction */
988 if (map->callsites.offsets16 [i] == pc_offset + 1)
989 break;
990 } else {
991 // FIXME: Use a binary search
992 for (i = 0; i < map->ncallsites; ++i)
993 /* ip points inside the call instruction */
994 if (map->callsites.offsets32 [i] == pc_offset + 1)
995 break;
997 if (i == map->ncallsites) {
998 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (method, TRUE));
999 g_assert_not_reached ();
1001 cindex = i;
1004 * This is not neccessary true on x86 because frames have a different size at each
1005 * call site.
1007 //g_assert (real_frame_start >= stack_limit);
1009 if (real_frame_start > stack_limit) {
1010 /* This scans the previously skipped frames as well */
1011 DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
1012 mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
1013 UnlockedAdd (&stats.scanned_other, real_frame_start - stack_limit);
1016 /* Mark stack slots */
1017 if (map->has_pin_slots) {
1018 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
1019 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
1020 guint8 *p;
1021 gboolean pinned;
1023 p = frame_start;
1024 for (i = 0; i < map->nslots; ++i) {
1025 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
1026 if (pinned) {
1027 DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
1028 mono_gc_conservatively_scan_area (p, p + SIZEOF_SLOT);
1029 scanned_conservatively += SIZEOF_SLOT;
1030 } else {
1031 scanned_precisely += SIZEOF_SLOT;
1033 p += SIZEOF_SLOT;
1035 } else {
1036 scanned_precisely += (map->nslots * SIZEOF_SLOT);
1039 /* The area outside of start-end is NOREF */
1040 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * SIZEOF_SLOT);
1042 /* Mark registers */
1043 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
1044 if (map->has_pin_regs) {
1045 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
1046 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
1047 int bindex = 0;
1048 for (i = 0; i < NREGS; ++i) {
1049 if (!(map->used_int_regs & (1 << i)))
1050 continue;
1052 if (!(map->reg_pin_mask & (1 << i)))
1053 continue;
1055 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
1056 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
1057 precise_regmask &= ~(1 << i);
1059 bindex ++;
1063 scanned += map->end_offset - map->start_offset;
1065 g_assert (scanned == scanned_precisely + scanned_conservatively);
1067 stack_limit = frame_end;
1069 /* Save information for the precise pass */
1070 fi = &tls->frames [tls->nframes];
1071 fi->nslots = map->nslots;
1072 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
1073 if (map->has_ref_slots)
1074 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
1075 else
1076 fi->bitmap = NULL;
1077 fi->frame_start_offset = frame_start - stack_start;
1078 fi->nreg_locations = 0;
1079 DEBUG_PRECISE (fi->ji = ji);
1080 DEBUG_PRECISE (fi->fp = fp);
1082 if (map->has_ref_regs) {
1083 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
1084 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
1085 int bindex = 0;
1086 for (i = 0; i < NREGS; ++i) {
1087 if (!(map->reg_ref_mask & (1 << i)))
1088 continue;
1090 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
1091 DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
1092 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
1093 fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
1094 fi->nreg_locations ++;
1096 bindex ++;
1101 * Clear locations of precisely tracked registers.
1103 if (precise_regmask) {
1104 for (i = 0; i < NREGS; ++i) {
1105 if (precise_regmask & (1 << i)) {
1107 * The method uses this register, and we have precise info for it.
1108 * This means the location will be scanned precisely.
1109 * Tell the code at the beginning of the loop that this location is
1110 * processed.
1112 if (reg_locations [i])
1113 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
1114 reg_locations [i] = NULL;
1119 tls->nframes ++;
1122 /* Scan the remaining register save locations */
1123 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1124 if (reg_locations [i]) {
1125 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
1126 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
1127 scanned_registers += SIZEOF_SLOT;
1129 if (new_reg_locations [i]) {
1130 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
1131 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
1132 scanned_registers += SIZEOF_SLOT;
1136 if (stack_limit < stack_end) {
1137 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
1138 mono_gc_conservatively_scan_area (stack_limit, stack_end);
1139 UnlockedAdd (&stats.scanned_native, stack_end - stack_limit);
1142 DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
1144 UnlockedAdd (&stats.scanned_stacks, stack_end - stack_start);
1145 UnlockedAdd (&stats.scanned, scanned);
1146 UnlockedAdd (&stats.scanned_precisely, scanned_precisely);
1147 UnlockedAdd (&stats.scanned_conservatively, scanned_conservatively);
1148 UnlockedAdd (&stats.scanned_registers, scanned_registers);
1150 //mono_gc_conservatively_scan_area (stack_start, stack_end);
1154 * precise_pass:
1156 * Mark a thread stack precisely based on information saved during the conservative
1157 * pass.
1159 static void
1160 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end, void *gc_data)
1162 int findex, i;
1163 FrameInfo *fi;
1164 guint8 *frame_start;
1166 if (!tls)
1167 return;
1169 if (!tls->unwind_state.valid)
1170 return;
1172 for (findex = 0; findex < tls->nframes; findex ++) {
1173 /* Load information saved by the !precise pass */
1174 fi = &tls->frames [findex];
1175 frame_start = stack_start + fi->frame_start_offset;
1177 DEBUG (char *fname = mono_method_full_name (jinfo_get_method (fi->ji), TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
1180 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1181 * call for each object.
1184 /* Mark stack slots */
1185 if (fi->bitmap) {
1186 guint8 *ref_bitmap = fi->bitmap;
1187 gboolean live;
1189 for (i = 0; i < fi->nslots; ++i) {
1190 MonoObject **ptr = (MonoObject**)(frame_start + (i * SIZEOF_SLOT));
1192 live = ref_bitmap [i / 8] & (1 << (i % 8));
1194 if (live) {
1195 MonoObject *obj = *ptr;
1196 if (obj) {
1197 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1198 *ptr = mono_gc_scan_object (obj, gc_data);
1199 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1200 } else {
1201 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1203 } else {
1204 #if 0
1206 * This is disabled because the pointer takes up a lot of space.
1207 * Stack slots might be shared between ref and non-ref variables ?
1209 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1210 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));
1212 * Fail fast if the live range is incorrect, and
1213 * the JITted code tries to access this object
1215 *ptr = DEAD_REF;
1217 #endif
1222 /* Mark registers */
1225 * Registers are different from stack slots, they have no address where they
1226 * are stored. Instead, some frame below this frame in the stack saves them
1227 * in its prolog to the stack. We can mark this location precisely.
1229 for (i = 0; i < fi->nreg_locations; ++i) {
1231 * reg_locations [i] contains the address of the stack slot where
1232 * a reg was last saved, so mark that slot.
1234 MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1235 MonoObject *obj = *ptr;
1237 if (obj) {
1238 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1239 *ptr = mono_gc_scan_object (obj, gc_data);
1240 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1241 } else {
1242 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1248 * Debugging aid to check for missed refs.
1250 if (tls->ref_to_track) {
1251 gpointer *p;
1253 for (p = (gpointer*)stack_start; p < (gpointer*)stack_end; ++p)
1254 if (*p == tls->ref_to_track)
1255 printf ("REF AT %p.\n", p);
1260 * thread_mark_func:
1262 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1263 * call, and TRUE at the second. USER_DATA points to a TlsData
1264 * structure filled up by thread_suspend_func.
1266 static void
1267 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise, void *gc_data)
1269 TlsData *tls = user_data;
1271 DEBUG (fprintf (logfile, "****************************************\n"));
1272 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));
1273 DEBUG (fprintf (logfile, "****************************************\n"));
1275 if (!precise)
1276 conservative_pass (tls, stack_start, stack_end);
1277 else
1278 precise_pass (tls, stack_start, stack_end, gc_data);
1281 #ifndef DISABLE_JIT
1283 static void
1284 mini_gc_init_gc_map (MonoCompile *cfg)
1286 if (COMPILE_LLVM (cfg))
1287 return;
1289 if (!mono_gc_is_moving ())
1290 return;
1292 if (cfg->compile_aot) {
1293 if (!enable_gc_maps_for_aot)
1294 return;
1295 } else if (!mono_gc_precise_stack_mark_enabled ())
1296 return;
1298 #if 1
1299 /* Debugging support */
1301 static int precise_count;
1303 precise_count ++;
1304 char *mono_gcmap_count = g_getenv ("MONO_GCMAP_COUNT");
1305 if (mono_gcmap_count) {
1306 int count = atoi (mono_gcmap_count);
1307 g_free (mono_gcmap_count);
1308 if (precise_count == count)
1309 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1310 if (precise_count > count)
1311 return;
1314 #endif
1316 cfg->compute_gc_maps = TRUE;
1318 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1322 * mini_gc_set_slot_type_from_fp:
1324 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1325 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1326 * need to call this function for those slots.
1328 void
1329 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1331 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1333 if (!cfg->compute_gc_maps)
1334 return;
1336 g_assert (slot_offset % SIZEOF_SLOT == 0);
1338 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1342 * mini_gc_set_slot_type_from_cfa:
1344 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1345 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1346 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1347 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1348 * call this function for those slots.
1350 void
1351 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1353 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1354 int slot = - (slot_offset / SIZEOF_SLOT);
1356 if (!cfg->compute_gc_maps)
1357 return;
1359 g_assert (slot_offset <= 0);
1360 g_assert (slot_offset % SIZEOF_SLOT == 0);
1362 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1365 static inline int
1366 fp_offset_to_slot (MonoCompile *cfg, int offset)
1368 MonoCompileGC *gcfg = cfg->gc_info;
1370 return (offset - gcfg->min_offset) / SIZEOF_SLOT;
1373 static inline int
1374 slot_to_fp_offset (MonoCompile *cfg, int slot)
1376 MonoCompileGC *gcfg = cfg->gc_info;
1378 return (slot * SIZEOF_SLOT) + gcfg->min_offset;
1381 static inline MONO_ALWAYS_INLINE void
1382 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1384 g_assert (slot >= 0 && slot < gcfg->nslots);
1386 if (type == SLOT_PIN) {
1387 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1388 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1389 } else if (type == SLOT_REF) {
1390 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1391 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1392 } else if (type == SLOT_NOREF) {
1393 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1394 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1398 static inline void
1399 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1401 int width, pos;
1402 guint8 *ref_bitmap, *pin_bitmap;
1405 int cindex;
1407 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1408 set_slot (gcfg, slot, cindex, type);
1410 ref_bitmap = gcfg->stack_ref_bitmap;
1411 pin_bitmap = gcfg->stack_pin_bitmap;
1412 width = gcfg->stack_bitmap_width;
1413 pos = width * slot;
1415 if (type == SLOT_PIN) {
1416 memset (ref_bitmap + pos, 0, width);
1417 memset (pin_bitmap + pos, 0xff, width);
1418 } else if (type == SLOT_REF) {
1419 memset (ref_bitmap + pos, 0xff, width);
1420 memset (pin_bitmap + pos, 0, width);
1421 } else if (type == SLOT_NOREF) {
1422 memset (ref_bitmap + pos, 0, width);
1423 memset (pin_bitmap + pos, 0, width);
1427 static inline void
1428 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1430 int cindex;
1432 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1433 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1434 if (callsite_offset >= from && callsite_offset < to)
1435 set_slot (gcfg, slot, cindex, type);
1439 static inline void
1440 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1442 g_assert (slot >= 0 && slot < gcfg->nregs);
1444 if (type == SLOT_PIN) {
1445 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1446 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1447 } else if (type == SLOT_REF) {
1448 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1449 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1450 } else if (type == SLOT_NOREF) {
1451 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1452 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1456 static inline void
1457 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1459 int cindex;
1461 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1462 set_reg_slot (gcfg, slot, cindex, type);
1465 static inline void
1466 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1468 int cindex;
1470 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1471 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1472 if (callsite_offset >= from && callsite_offset < to)
1473 set_reg_slot (gcfg, slot, cindex, type);
1477 static void
1478 process_spill_slots (MonoCompile *cfg)
1480 MonoCompileGC *gcfg = cfg->gc_info;
1481 MonoBasicBlock *bb;
1482 GSList *l;
1483 int i;
1485 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1486 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1487 for (l = bb->spill_slot_defs; l; l = l->next) {
1488 MonoInst *def = l->data;
1489 int spill_slot = def->inst_c0;
1490 int bank = def->inst_c1;
1491 int offset = cfg->spill_info [bank][spill_slot].offset;
1492 int slot = fp_offset_to_slot (cfg, offset);
1494 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1495 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1499 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1500 for (l = bb->spill_slot_defs; l; l = l->next) {
1501 MonoInst *def = l->data;
1502 int spill_slot = def->inst_c0;
1503 int bank = def->inst_c1;
1504 int offset = cfg->spill_info [bank][spill_slot].offset;
1505 int slot = fp_offset_to_slot (cfg, offset);
1506 GCSlotType type;
1508 if (bank == MONO_REG_INT_MP)
1509 type = SLOT_PIN;
1510 else
1511 type = SLOT_REF;
1514 * Extend the live interval for the GC tracked spill slots
1515 * defined in this bblock.
1516 * FIXME: This is not needed.
1518 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1520 if (cfg->verbose_level > 1)
1521 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1525 /* Set fp spill slots to NOREF */
1526 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1527 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1528 int slot;
1530 if (offset == -1)
1531 continue;
1533 slot = fp_offset_to_slot (cfg, offset);
1535 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1536 /* FIXME: 32 bit */
1537 if (cfg->verbose_level > 1)
1538 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1541 /* Set int spill slots to NOREF */
1542 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1543 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1544 int slot;
1546 if (offset == -1)
1547 continue;
1549 slot = fp_offset_to_slot (cfg, offset);
1551 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1552 if (cfg->verbose_level > 1)
1553 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1558 * process_other_slots:
1560 * Process stack slots registered using mini_gc_set_slot_type_... ().
1562 static void
1563 process_other_slots (MonoCompile *cfg)
1565 MonoCompileGC *gcfg = cfg->gc_info;
1566 GSList *l;
1568 /* Relative to the CFA */
1569 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1570 guint data = GPOINTER_TO_UINT (l->data);
1571 int cfa_slot = data >> 16;
1572 GCSlotType type = data & 0xff;
1573 int slot;
1576 * Map the cfa relative slot to an fp relative slot.
1577 * slot_addr == cfa - <cfa_slot>*4/8
1578 * fp + cfa_offset == cfa
1579 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1581 slot = (cfg->cfa_offset / SIZEOF_SLOT) - cfa_slot - (gcfg->min_offset / SIZEOF_SLOT);
1583 set_slot_everywhere (gcfg, slot, type);
1585 if (cfg->verbose_level > 1) {
1586 int fp_offset = slot_to_fp_offset (cfg, slot);
1587 if (type == SLOT_NOREF)
1588 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));
1592 /* Relative to the FP */
1593 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1594 gint data = GPOINTER_TO_INT (l->data);
1595 int offset = data >> 16;
1596 GCSlotType type = data & 0xff;
1597 int slot;
1599 slot = fp_offset_to_slot (cfg, offset);
1601 set_slot_everywhere (gcfg, slot, type);
1603 /* Liveness for these slots is handled by process_spill_slots () */
1605 if (cfg->verbose_level > 1) {
1606 if (type == SLOT_REF)
1607 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1608 else if (type == SLOT_NOREF)
1609 printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset, slot);
1614 static gsize*
1615 get_vtype_bitmap (MonoType *t, int *numbits)
1617 MonoClass *klass = mono_class_from_mono_type_internal (t);
1619 if (klass->generic_container || mono_class_is_open_constructed_type (t)) {
1620 /* FIXME: Generic sharing */
1621 return NULL;
1622 } else {
1623 mono_class_compute_gc_descriptor (klass);
1625 return mono_gc_get_bitmap_for_descr (klass->gc_descr, numbits);
1629 static inline const char*
1630 get_offset_sign (int offset)
1632 return offset < 0 ? "-" : "+";
1635 static inline int
1636 get_offset_val (int offset)
1638 return offset < 0 ? (- offset) : offset;
1641 static void
1642 process_variables (MonoCompile *cfg)
1644 MonoCompileGC *gcfg = cfg->gc_info;
1645 MonoMethodSignature *sig = mono_method_signature_internal (cfg->method);
1646 int i, locals_min_slot, locals_max_slot, cindex;
1647 MonoBasicBlock *bb;
1648 MonoInst *tmp;
1649 int *pc_offsets;
1650 int locals_min_offset = gcfg->locals_min_offset;
1651 int locals_max_offset = gcfg->locals_max_offset;
1653 /* Slots for locals are NOREF by default */
1654 locals_min_slot = (locals_min_offset - gcfg->min_offset) / SIZEOF_SLOT;
1655 locals_max_slot = (locals_max_offset - gcfg->min_offset) / SIZEOF_SLOT;
1656 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1657 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1661 * Compute the offset where variables are initialized in the first bblock, if any.
1663 pc_offsets = g_new0 (int, cfg->next_vreg);
1665 bb = cfg->bb_entry->next_bb;
1666 MONO_BB_FOR_EACH_INS (bb, tmp) {
1667 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1668 int vreg = tmp->inst_c1;
1669 if (pc_offsets [vreg] == 0) {
1670 g_assert (tmp->backend.pc_offset > 0);
1671 pc_offsets [vreg] = tmp->backend.pc_offset;
1677 * Stack slots holding arguments are initialized in the prolog.
1678 * This means we can treat them alive for the whole method.
1680 for (i = 0; i < cfg->num_varinfo; i++) {
1681 MonoInst *ins = cfg->varinfo [i];
1682 MonoType *t = ins->inst_vtype;
1683 MonoMethodVar *vmv;
1684 guint32 pos;
1685 gboolean byref, is_this = FALSE;
1686 gboolean is_arg = i < cfg->locals_start;
1688 if (ins == cfg->ret) {
1689 if (!(ins->opcode == OP_REGOFFSET && MONO_TYPE_ISSTRUCT (t)))
1690 continue;
1693 vmv = MONO_VARINFO (cfg, i);
1695 /* For some reason, 'this' is byref */
1696 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1697 t = m_class_get_byval_arg (cfg->method->klass);
1698 is_this = TRUE;
1701 byref = t->byref;
1703 if (ins->opcode == OP_REGVAR) {
1704 int hreg;
1705 GCSlotType slot_type;
1707 t = mini_get_underlying_type (t);
1709 hreg = ins->dreg;
1710 g_assert (hreg < MONO_MAX_IREGS);
1712 if (byref)
1713 slot_type = SLOT_PIN;
1714 else
1715 slot_type = mini_type_is_reference (t) ? SLOT_REF : SLOT_NOREF;
1717 if (slot_type == SLOT_PIN) {
1718 /* These have no live interval, be conservative */
1719 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1720 } else {
1722 * Unlike variables allocated to the stack, we generate liveness info
1723 * for noref vars in registers in mono_spill_global_vars (), because
1724 * knowing that a register doesn't contain a ref allows us to mark its save
1725 * locations precisely.
1727 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1728 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1729 set_reg_slot (gcfg, hreg, cindex, slot_type);
1732 if (cfg->verbose_level > 1) {
1733 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1736 continue;
1739 if (ins->opcode != OP_REGOFFSET)
1740 continue;
1742 if (ins->inst_offset % SIZEOF_SLOT != 0)
1743 continue;
1745 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1747 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1748 /* These do not get stored in the prolog */
1749 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1751 if (cfg->verbose_level > 1) {
1752 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));
1754 continue;
1757 if (MONO_TYPE_ISSTRUCT (t)) {
1758 int numbits = 0, j;
1759 gsize *bitmap = NULL;
1760 gboolean pin = FALSE;
1761 int size;
1762 int size_in_slots;
1764 if (ins->backend.is_pinvoke)
1765 size = mono_class_native_size (ins->klass, NULL);
1766 else
1767 size = mono_class_value_size (ins->klass, NULL);
1768 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
1770 if (cfg->verbose_level > 1)
1771 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));
1773 if (!ins->klass->has_references) {
1774 if (is_arg) {
1775 for (j = 0; j < size_in_slots; ++j)
1776 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1778 continue;
1781 bitmap = get_vtype_bitmap (t, &numbits);
1782 if (!bitmap)
1783 pin = TRUE;
1786 * Most vtypes are marked volatile because of the LDADDR instructions,
1787 * and they have no liveness information since they are decomposed
1788 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1789 * them during VZERO decomposition.
1791 if (!is_arg) {
1792 if (!pc_offsets [vmv->vreg])
1793 pin = TRUE;
1795 if (ins->backend.is_pinvoke)
1796 pin = TRUE;
1799 if (bitmap) {
1800 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1801 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1802 for (j = 0; j < numbits; ++j) {
1803 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1804 /* The descriptor is for the boxed object */
1805 set_slot (gcfg, (pos + j - (MONO_ABI_SIZEOF (MonoObject) / SIZEOF_SLOT)), cindex, pin ? SLOT_PIN : SLOT_REF);
1811 if (cfg->verbose_level > 1) {
1812 for (j = 0; j < numbits; ++j) {
1813 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1814 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 - (MONO_ABI_SIZEOF (MonoObject) / SIZEOF_SLOT)));
1817 } else {
1818 if (cfg->verbose_level > 1)
1819 printf ("\t\tpinned\n");
1820 for (j = 0; j < size_in_slots; ++j) {
1821 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1825 g_free (bitmap);
1827 continue;
1830 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1831 /* Vret addr etc. */
1832 continue;
1834 if (t->byref) {
1835 if (is_arg) {
1836 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1837 } else {
1838 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1839 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1840 set_slot (gcfg, pos, cindex, SLOT_PIN);
1842 if (cfg->verbose_level > 1)
1843 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));
1844 continue;
1848 * This is currently disabled, but could be enabled to debug crashes.
1850 #if 0
1851 if (t->type == MONO_TYPE_I) {
1853 * Variables created in mono_handle_global_vregs have type I, but they
1854 * could hold GC refs since the vregs they were created from might not been
1855 * marked as holding a GC ref. So be conservative.
1857 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1858 continue;
1860 #endif
1862 t = mini_get_underlying_type (t);
1864 if (!mini_type_is_reference (t)) {
1865 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1866 if (cfg->verbose_level > 1)
1867 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));
1868 if (!t->byref && sizeof (host_mgreg_t) == 4 && (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_R8)) {
1869 set_slot_everywhere (gcfg, pos + 1, SLOT_NOREF);
1870 if (cfg->verbose_level > 1)
1871 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));
1873 continue;
1876 /* 'this' is marked INDIRECT for gshared methods */
1877 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1879 * For volatile variables, treat them alive from the point they are
1880 * initialized in the first bblock until the end of the method.
1882 if (is_arg) {
1883 set_slot_everywhere (gcfg, pos, SLOT_REF);
1884 } else if (pc_offsets [vmv->vreg]) {
1885 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1886 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1887 } else {
1888 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1890 if (cfg->verbose_level > 1)
1891 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));
1892 continue;
1895 if (is_arg) {
1896 /* Live for the whole method */
1897 set_slot_everywhere (gcfg, pos, SLOT_REF);
1898 } else {
1899 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1900 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1901 set_slot (gcfg, pos, cindex, SLOT_REF);
1904 if (cfg->verbose_level > 1) {
1905 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));
1909 g_free (pc_offsets);
1912 static int
1913 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1916 * Convert a sp relative offset to a slot index. This is
1917 * platform specific.
1919 #ifdef TARGET_AMD64
1920 /* fp = sp + offset */
1921 g_assert (cfg->frame_reg == AMD64_RBP);
1922 return (- cfg->arch.sp_fp_offset + sp_offset);
1923 #elif defined(TARGET_X86)
1924 /* The offset is computed from the sp at the start of the call sequence */
1925 g_assert (cfg->frame_reg == X86_EBP);
1926 #ifdef MONO_X86_NO_PUSHES
1927 return (- cfg->arch.sp_fp_offset + sp_offset);
1928 #else
1929 return (- cfg->arch.sp_fp_offset - sp_offset);
1930 #endif
1931 #else
1932 NOT_IMPLEMENTED;
1933 return -1;
1934 #endif
1937 static void
1938 process_param_area_slots (MonoCompile *cfg)
1940 MonoCompileGC *gcfg = cfg->gc_info;
1941 int cindex, i;
1942 gboolean *is_param;
1945 * These slots are used for passing parameters during calls. They are sp relative, not
1946 * fp relative, so they are harder to handle.
1948 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1949 /* The distance between fp and sp is not constant */
1950 return;
1952 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1954 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1955 GCCallSite *callsite = gcfg->callsites [cindex];
1956 GSList *l;
1958 for (l = callsite->param_slots; l; l = l->next) {
1959 MonoInst *def = l->data;
1960 MonoType *t = def->inst_vtype;
1961 int sp_offset = def->inst_offset;
1962 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1963 int slot = fp_offset_to_slot (cfg, fp_offset);
1964 guint32 align;
1965 guint32 size;
1967 if (MONO_TYPE_ISSTRUCT (t)) {
1968 size = mini_type_stack_size_full (t, &align, FALSE);
1969 } else {
1970 size = sizeof (target_mgreg_t);
1973 for (i = 0; i < size / sizeof (target_mgreg_t); ++i) {
1974 g_assert (slot + i >= 0 && slot + i < gcfg->nslots);
1975 is_param [slot + i] = TRUE;
1980 /* All param area slots are noref by default */
1981 for (i = 0; i < gcfg->nslots; ++i) {
1982 if (is_param [i])
1983 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1987 * We treat param area slots as being part of the callee's frame, to be able to handle tailcalls which overwrite
1988 * the argument area of the caller.
1992 static void
1993 process_finally_clauses (MonoCompile *cfg)
1995 MonoCompileGC *gcfg = cfg->gc_info;
1996 GCCallSite **callsites;
1997 int ncallsites;
1998 gboolean has_finally;
1999 int i, j, nslots, nregs;
2001 ncallsites = gcfg->ncallsites;
2002 nslots = gcfg->nslots;
2003 nregs = gcfg->nregs;
2004 callsites = gcfg->callsites;
2007 * The calls to the finally clauses don't show up in the cfg. See
2008 * test_0_liveness_8 ().
2009 * Variables accessed inside the finally clause are already marked VOLATILE by
2010 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
2011 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
2013 has_finally = FALSE;
2014 for (i = 0; i < cfg->header->num_clauses; ++i) {
2015 MonoExceptionClause *clause = &cfg->header->clauses [i];
2017 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2018 has_finally = TRUE;
2021 if (has_finally) {
2022 if (cfg->verbose_level > 1)
2023 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
2024 for (j = 0; j < ncallsites; ++j) {
2025 MonoBasicBlock *bb = callsites [j]->bb;
2026 MonoExceptionClause *clause;
2027 gboolean is_in_finally = FALSE;
2029 for (i = 0; i < cfg->header->num_clauses; ++i) {
2030 clause = &cfg->header->clauses [i];
2032 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
2033 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2034 is_in_finally = TRUE;
2035 break;
2040 if (is_in_finally) {
2041 for (i = 0; i < nslots; ++i)
2042 set_slot (gcfg, i, j, SLOT_PIN);
2043 for (i = 0; i < nregs; ++i)
2044 set_reg_slot (gcfg, i, j, SLOT_PIN);
2050 static void
2051 compute_frame_size (MonoCompile *cfg)
2053 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
2054 int min_offset, max_offset;
2055 MonoCompileGC *gcfg = cfg->gc_info;
2056 MonoMethodSignature *sig = mono_method_signature_internal (cfg->method);
2057 GSList *l;
2059 /* Compute min/max offsets from the fp */
2061 /* Locals */
2062 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
2063 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, SIZEOF_SLOT);
2064 locals_max_offset = cfg->locals_max_stack_offset;
2065 #else
2066 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
2067 NOT_IMPLEMENTED;
2068 #endif
2070 locals_min_offset = ALIGN_TO (locals_min_offset, SIZEOF_SLOT);
2071 locals_max_offset = ALIGN_TO (locals_max_offset, SIZEOF_SLOT);
2073 min_offset = locals_min_offset;
2074 max_offset = locals_max_offset;
2076 /* Arguments */
2077 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2078 MonoInst *ins = cfg->args [i];
2080 if (ins->opcode == OP_REGOFFSET) {
2081 int size, size_in_slots;
2082 size = mini_type_stack_size_full (ins->inst_vtype, NULL, ins->backend.is_pinvoke);
2083 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
2085 min_offset = MIN (min_offset, ins->inst_offset);
2086 max_offset = MAX ((int)max_offset, (int)(ins->inst_offset + (size_in_slots * SIZEOF_SLOT)));
2090 /* Cfa slots */
2091 g_assert (cfg->frame_reg == cfg->cfa_reg);
2092 g_assert (cfg->cfa_offset > 0);
2093 cfa_min_offset = 0;
2094 cfa_max_offset = cfg->cfa_offset;
2096 min_offset = MIN (min_offset, cfa_min_offset);
2097 max_offset = MAX (max_offset, cfa_max_offset);
2099 /* Fp relative slots */
2100 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
2101 gint data = GPOINTER_TO_INT (l->data);
2102 int offset = data >> 16;
2104 min_offset = MIN (min_offset, offset);
2107 /* Spill slots */
2108 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
2109 int stack_offset = ALIGN_TO (cfg->stack_offset, SIZEOF_SLOT);
2110 min_offset = MIN (min_offset, (-stack_offset));
2113 /* Param area slots */
2114 #ifdef TARGET_AMD64
2115 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2116 #elif defined(TARGET_X86)
2117 #ifdef MONO_X86_NO_PUSHES
2118 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2119 #else
2120 min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size));
2121 #endif
2122 #elif defined(TARGET_ARM)
2123 // FIXME:
2124 #elif defined(TARGET_s390X)
2125 // FIXME:
2126 #else
2127 NOT_IMPLEMENTED;
2128 #endif
2130 gcfg->min_offset = min_offset;
2131 gcfg->max_offset = max_offset;
2132 gcfg->locals_min_offset = locals_min_offset;
2133 gcfg->locals_max_offset = locals_max_offset;
2136 static void
2137 init_gcfg (MonoCompile *cfg)
2139 int i, nregs, nslots;
2140 MonoCompileGC *gcfg = cfg->gc_info;
2141 GCCallSite **callsites;
2142 int ncallsites;
2143 MonoBasicBlock *bb;
2144 GSList *l;
2147 * Collect callsites
2149 ncallsites = 0;
2150 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2151 ncallsites += g_slist_length (bb->gc_callsites);
2153 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
2154 i = 0;
2155 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2156 for (l = bb->gc_callsites; l; l = l->next)
2157 callsites [i++] = l->data;
2160 /* The callsites should already be ordered by pc offset */
2161 for (i = 1; i < ncallsites; ++i)
2162 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
2165 * The stack frame looks like this:
2167 * <fp + max_offset> == cfa -> <end of previous frame>
2168 * <other stack slots>
2169 * <locals>
2170 * <other stack slots>
2171 * fp + min_offset ->
2172 * ...
2173 * fp ->
2176 if (cfg->verbose_level > 1)
2177 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
2179 nslots = (gcfg->max_offset - gcfg->min_offset) / SIZEOF_SLOT;
2180 nregs = NREGS;
2182 gcfg->nslots = nslots;
2183 gcfg->nregs = nregs;
2184 gcfg->callsites = callsites;
2185 gcfg->ncallsites = ncallsites;
2186 gcfg->stack_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2187 gcfg->reg_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2188 gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2189 gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2190 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2191 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2193 /* All slots start out as PIN */
2194 memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * nregs);
2195 for (i = 0; i < nregs; ++i) {
2197 * By default, registers are NOREF.
2198 * It is possible for a callee to save them before being defined in this method,
2199 * but the saved value is dead too, so it doesn't need to be marked.
2201 if ((cfg->used_int_regs & (1 << i)))
2202 set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
2206 static inline gboolean
2207 has_bit_set (guint8 *bitmap, int width, int slot)
2209 int i;
2210 int pos = width * slot;
2212 for (i = 0; i < width; ++i) {
2213 if (bitmap [pos + i])
2214 break;
2216 return i < width;
2219 static void
2220 create_map (MonoCompile *cfg)
2222 GCMap *map;
2223 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
2224 int ntypes [16];
2225 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
2226 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
2227 int start, end;
2228 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
2229 MonoCompileGC *gcfg = cfg->gc_info;
2230 GCCallSite **callsites;
2231 int ncallsites;
2232 guint8 *bitmap, *bitmaps;
2233 guint32 reg_ref_mask, reg_pin_mask;
2235 ncallsites = gcfg->ncallsites;
2236 nslots = gcfg->nslots;
2237 nregs = gcfg->nregs;
2238 callsites = gcfg->callsites;
2241 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
2242 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
2244 has_ref_slots = FALSE;
2245 has_pin_slots = FALSE;
2246 start = -1;
2247 end = -1;
2248 memset (ntypes, 0, sizeof (ntypes));
2249 for (i = 0; i < nslots; ++i) {
2250 gboolean has_ref = FALSE;
2251 gboolean has_pin = FALSE;
2253 if (has_bit_set (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i))
2254 has_pin = TRUE;
2255 if (has_bit_set (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i))
2256 has_ref = TRUE;
2258 if (has_ref)
2259 has_ref_slots = TRUE;
2260 if (has_pin)
2261 has_pin_slots = TRUE;
2263 if (has_ref)
2264 ntypes [SLOT_REF] ++;
2265 else if (has_pin)
2266 ntypes [SLOT_PIN] ++;
2267 else
2268 ntypes [SLOT_NOREF] ++;
2270 if (has_ref || has_pin) {
2271 if (start == -1)
2272 start = i;
2273 end = i + 1;
2276 if (start == -1) {
2277 start = end = nslots;
2278 } else {
2279 g_assert (start != -1);
2280 g_assert (start < end);
2283 has_ref_regs = FALSE;
2284 has_pin_regs = FALSE;
2285 reg_ref_mask = 0;
2286 reg_pin_mask = 0;
2287 nref_regs = 0;
2288 npin_regs = 0;
2289 for (i = 0; i < nregs; ++i) {
2290 gboolean has_ref = FALSE;
2291 gboolean has_pin = FALSE;
2293 if (!(cfg->used_int_regs & (1 << i)))
2294 continue;
2296 if (has_bit_set (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i))
2297 has_pin = TRUE;
2298 if (has_bit_set (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i))
2299 has_ref = TRUE;
2301 if (has_ref) {
2302 reg_ref_mask |= (1 << i);
2303 has_ref_regs = TRUE;
2304 nref_regs ++;
2306 if (has_pin) {
2307 reg_pin_mask |= (1 << i);
2308 has_pin_regs = TRUE;
2309 npin_regs ++;
2313 if (cfg->verbose_level > 1)
2314 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);
2316 /* Create the GC Map */
2318 /* The work bitmaps have one row for each slot, since this is how we access them during construction */
2319 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2320 stack_bitmap_size = stack_bitmap_width * ncallsites;
2321 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2322 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2323 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2324 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2325 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);
2327 map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2329 map->frame_reg = cfg->frame_reg;
2330 map->start_offset = gcfg->min_offset;
2331 map->end_offset = gcfg->min_offset + (nslots * SIZEOF_SLOT);
2332 map->map_offset = start * SIZEOF_SLOT;
2333 map->nslots = end - start;
2334 map->has_ref_slots = has_ref_slots;
2335 map->has_pin_slots = has_pin_slots;
2336 map->has_ref_regs = has_ref_regs;
2337 map->has_pin_regs = has_pin_regs;
2338 g_assert (nregs < 32);
2339 map->used_int_regs = cfg->used_int_regs;
2340 map->reg_ref_mask = reg_ref_mask;
2341 map->reg_pin_mask = reg_pin_mask;
2342 map->nref_regs = nref_regs;
2343 map->npin_regs = npin_regs;
2345 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2347 bitmaps_offset = 0;
2348 if (has_ref_slots) {
2349 map->stack_ref_bitmap_offset = bitmaps_offset;
2350 bitmaps_offset += stack_bitmap_size;
2352 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2353 for (i = 0; i < nslots; ++i) {
2354 for (j = 0; j < ncallsites; ++j) {
2355 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i, j))
2356 set_bit (bitmap, stack_bitmap_width, j, i - start);
2360 if (has_pin_slots) {
2361 map->stack_pin_bitmap_offset = bitmaps_offset;
2362 bitmaps_offset += stack_bitmap_size;
2364 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2365 for (i = 0; i < nslots; ++i) {
2366 for (j = 0; j < ncallsites; ++j) {
2367 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i, j))
2368 set_bit (bitmap, stack_bitmap_width, j, i - start);
2372 if (has_ref_regs) {
2373 map->reg_ref_bitmap_offset = bitmaps_offset;
2374 bitmaps_offset += reg_ref_bitmap_size;
2376 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2377 bindex = 0;
2378 for (i = 0; i < nregs; ++i) {
2379 if (reg_ref_mask & (1 << i)) {
2380 for (j = 0; j < ncallsites; ++j) {
2381 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i, j))
2382 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2384 bindex ++;
2388 if (has_pin_regs) {
2389 map->reg_pin_bitmap_offset = bitmaps_offset;
2390 bitmaps_offset += reg_pin_bitmap_size;
2392 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2393 bindex = 0;
2394 for (i = 0; i < nregs; ++i) {
2395 if (reg_pin_mask & (1 << i)) {
2396 for (j = 0; j < ncallsites; ++j) {
2397 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i, j))
2398 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2400 bindex ++;
2405 /* Call sites */
2406 map->ncallsites = ncallsites;
2407 if (cfg->code_len < 256)
2408 map->callsite_entry_size = 1;
2409 else if (cfg->code_len < 65536)
2410 map->callsite_entry_size = 2;
2411 else
2412 map->callsite_entry_size = 4;
2414 /* Encode the GC Map */
2416 guint8 buf [256];
2417 guint8 *endbuf;
2418 GCEncodedMap *emap;
2419 int encoded_size;
2420 guint8 *p;
2422 encode_gc_map (map, buf, &endbuf);
2423 g_assert (endbuf - buf < 256);
2425 encoded_size = endbuf - buf;
2426 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2428 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2429 //emap->ref_slots = map->ref_slots;
2431 /* Encoded fixed fields */
2432 p = &emap->encoded [0];
2433 //emap->encoded_size = encoded_size;
2434 memcpy (p, buf, encoded_size);
2435 p += encoded_size;
2437 /* Callsite table */
2438 p = (guint8*)ALIGN_TO ((gsize)p, map->callsite_entry_size);
2439 if (map->callsite_entry_size == 1) {
2440 guint8 *offsets = p;
2441 for (i = 0; i < ncallsites; ++i)
2442 offsets [i] = callsites [i]->pc_offset;
2443 UnlockedAdd (&stats.gc_callsites8_size, ncallsites * sizeof (guint8));
2444 } else if (map->callsite_entry_size == 2) {
2445 guint16 *offsets = (guint16*)p;
2446 for (i = 0; i < ncallsites; ++i)
2447 offsets [i] = callsites [i]->pc_offset;
2448 UnlockedAdd (&stats.gc_callsites16_size, ncallsites * sizeof (guint16));
2449 } else {
2450 guint32 *offsets = (guint32*)p;
2451 for (i = 0; i < ncallsites; ++i)
2452 offsets [i] = callsites [i]->pc_offset;
2453 UnlockedAdd (&stats.gc_callsites32_size, ncallsites * sizeof (guint32));
2455 p += ncallsites * map->callsite_entry_size;
2457 /* Bitmaps */
2458 memcpy (p, bitmaps, bitmaps_size);
2459 p += bitmaps_size;
2461 g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
2463 UnlockedAdd (&stats.gc_maps_size, alloc_size);
2464 UnlockedAdd (&stats.gc_callsites_size, ncallsites * map->callsite_entry_size);
2465 UnlockedAdd (&stats.gc_bitmaps_size, bitmaps_size);
2466 UnlockedAdd (&stats.gc_map_struct_size, sizeof (GCEncodedMap) + encoded_size);
2468 cfg->jit_info->gc_info = emap;
2470 cfg->gc_map = (guint8*)emap;
2471 cfg->gc_map_size = alloc_size;
2474 UnlockedAdd (&stats.all_slots, nslots);
2475 UnlockedAdd (&stats.ref_slots, ntypes [SLOT_REF]);
2476 UnlockedAdd (&stats.noref_slots, ntypes [SLOT_NOREF]);
2477 UnlockedAdd (&stats.pin_slots, ntypes [SLOT_PIN]);
2480 void
2481 mini_gc_create_gc_map (MonoCompile *cfg)
2483 if (!cfg->compute_gc_maps)
2484 return;
2487 * During marking, all frames except the top frame are at a call site, and we mark the
2488 * top frame conservatively. This means that we only need to compute and record
2489 * GC maps for call sites.
2492 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2493 /* Without liveness info, the live ranges are not precise enough */
2494 return;
2496 mono_analyze_liveness_gc (cfg);
2498 compute_frame_size (cfg);
2500 init_gcfg (cfg);
2502 process_spill_slots (cfg);
2503 process_other_slots (cfg);
2504 process_param_area_slots (cfg);
2505 process_variables (cfg);
2506 process_finally_clauses (cfg);
2508 create_map (cfg);
2511 #endif /* DISABLE_JIT */
2513 static void
2514 parse_debug_options (void)
2516 char **opts, **ptr;
2517 const char *env;
2519 env = g_getenv ("MONO_GCMAP_DEBUG");
2520 if (!env)
2521 return;
2523 opts = g_strsplit (env, ",", -1);
2524 for (ptr = opts; ptr && *ptr; ptr ++) {
2525 /* No options yet */
2526 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2527 exit (1);
2529 g_strfreev (opts);
2530 g_free (env);
2533 void
2534 mini_gc_init (void)
2536 MonoGCCallbacks cb;
2538 memset (&cb, 0, sizeof (cb));
2539 cb.thread_attach_func = thread_attach_func;
2540 cb.thread_detach_func = thread_detach_func;
2541 cb.thread_suspend_func = thread_suspend_func;
2542 /* Comment this out to disable precise stack marking */
2543 cb.thread_mark_func = thread_mark_func;
2544 cb.get_provenance_func = get_provenance_func;
2545 mono_gc_set_gc_callbacks (&cb);
2547 logfile = mono_gc_get_logfile ();
2549 parse_debug_options ();
2551 mono_counters_register ("GC Maps size",
2552 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2553 mono_counters_register ("GC Call Sites size",
2554 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2555 mono_counters_register ("GC Bitmaps size",
2556 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2557 mono_counters_register ("GC Map struct size",
2558 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2559 mono_counters_register ("GC Call Sites encoded using 8 bits",
2560 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2561 mono_counters_register ("GC Call Sites encoded using 16 bits",
2562 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2563 mono_counters_register ("GC Call Sites encoded using 32 bits",
2564 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2566 mono_counters_register ("GC Map slots (all)",
2567 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2568 mono_counters_register ("GC Map slots (ref)",
2569 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2570 mono_counters_register ("GC Map slots (noref)",
2571 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2572 mono_counters_register ("GC Map slots (pin)",
2573 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2575 mono_counters_register ("GC TLS Data size",
2576 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2578 mono_counters_register ("Stack space scanned (all)",
2579 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2580 mono_counters_register ("Stack space scanned (native)",
2581 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2582 mono_counters_register ("Stack space scanned (other)",
2583 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2584 mono_counters_register ("Stack space scanned (using GC Maps)",
2585 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2586 mono_counters_register ("Stack space scanned (precise)",
2587 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2588 mono_counters_register ("Stack space scanned (pin)",
2589 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2590 mono_counters_register ("Stack space scanned (pin registers)",
2591 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2594 #else
2596 void
2597 mini_gc_enable_gc_maps_for_aot (void)
2601 void
2602 mini_gc_init (void)
2604 MonoGCCallbacks cb;
2605 memset (&cb, 0, sizeof (cb));
2606 cb.get_provenance_func = get_provenance_func;
2607 mono_gc_set_gc_callbacks (&cb);
2610 #ifndef DISABLE_JIT
2612 static void
2613 mini_gc_init_gc_map (MonoCompile *cfg)
2617 void
2618 mini_gc_create_gc_map (MonoCompile *cfg)
2622 void
2623 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2627 void
2628 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2632 #endif /* DISABLE_JIT */
2634 #endif
2636 #ifndef DISABLE_JIT
2639 * mini_gc_init_cfg:
2641 * Set GC specific options in CFG.
2643 void
2644 mini_gc_init_cfg (MonoCompile *cfg)
2646 if (mono_gc_is_moving ()) {
2647 cfg->disable_ref_noref_stack_slot_share = TRUE;
2648 cfg->gen_write_barriers = TRUE;
2651 mini_gc_init_gc_map (cfg);
2654 #endif /* DISABLE_JIT */
2657 * Problems with the current code:
2658 * - the stack walk is slow
2659 * - vtypes/refs used in EH regions are treated conservatively
2660 * - if the code is finished, less pinning will be done, causing problems because
2661 * we promote all surviving objects to old-gen.
2662 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2663 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2664 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2665 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2666 * precise there.
2670 * Ideas for creating smaller GC maps:
2671 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2672 * each bitmap.
2673 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2674 * not wasted.
2675 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2676 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2677 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2678 * from an index entry.