2 * unwind.c: Stack Unwinding Interface
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2008 Novell, Inc.
13 #include <mono/utils/mono-counters.h>
14 #include <mono/metadata/threads-types.h>
28 guint8 info [MONO_ZERO_LEN_ARRAY];
31 static CRITICAL_SECTION unwind_mutex;
33 static MonoUnwindInfo **cached_info;
34 static int cached_info_next, cached_info_size;
36 static int unwind_info_size;
38 #define unwind_lock() EnterCriticalSection (&unwind_mutex)
39 #define unwind_unlock() LeaveCriticalSection (&unwind_mutex)
42 static int map_hw_reg_to_dwarf_reg [] = { 0, 2, 1, 3, 7, 6, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
43 #define NUM_REGS AMD64_NREG
44 #define DWARF_DATA_ALIGN (-8)
45 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (AMD64_RIP))
46 #elif defined(TARGET_ARM)
47 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
48 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
50 #define DWARF_DATA_ALIGN (-4)
51 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (ARMREG_LR))
52 #elif defined (TARGET_X86)
53 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
55 #define NUM_REGS X86_NREG + 1
56 #define DWARF_DATA_ALIGN (-4)
57 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
58 #elif defined (TARGET_POWERPC)
59 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
60 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
61 9, 10, 11, 12, 13, 14, 15, 16,
62 17, 18, 19, 20, 21, 22, 23, 24,
63 25, 26, 27, 28, 29, 30, 31 };
65 #define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
66 #define DWARF_PC_REG 108
68 static int map_hw_reg_to_dwarf_reg [16];
70 #define DWARF_DATA_ALIGN 0
71 #define DWARF_PC_REG -1
74 static gboolean dwarf_reg_to_hw_reg_inited;
76 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
79 * mono_hw_reg_to_dwarf_reg:
81 * Map the hardware register number REG to the register number used by DWARF.
84 mono_hw_reg_to_dwarf_reg (int reg)
90 g_assert (reg < NUM_REGS);
94 g_assert_not_reached ();
97 return map_hw_reg_to_dwarf_reg [reg];
106 g_assert (NUM_REGS > 0);
107 g_assert (sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int) == NUM_REGS);
108 for (i = 0; i < NUM_REGS; ++i) {
109 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
112 mono_memory_barrier ();
113 dwarf_reg_to_hw_reg_inited = TRUE;
117 mono_dwarf_reg_to_hw_reg (int reg)
119 if (!dwarf_reg_to_hw_reg_inited)
122 return map_dwarf_reg_to_hw_reg [reg];
125 static G_GNUC_UNUSED void
126 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
131 guint8 b = value & 0x7f;
133 if (value != 0) /* more bytes to come */
141 static G_GNUC_UNUSED void
142 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
145 gboolean negative = (value < 0);
153 /* the following is unnecessary if the
154 * implementation of >>= uses an arithmetic rather
155 * than logical shift for a signed left operand
159 value |= - (1 <<(size - 7));
160 /* sign bit of byte is second high order bit (0x40) */
161 if ((value == 0 && !(byte & 0x40)) ||
162 (value == -1 && (byte & 0x40)))
172 static inline guint32
173 decode_uleb128 (guint8 *buf, guint8 **endbuf)
183 res = res | (((int)(b & 0x7f)) << shift);
195 decode_sleb128 (guint8 *buf, guint8 **endbuf)
205 res = res | (((int)(b & 0x7f)) << shift);
208 if (shift < 32 && (b & 0x40))
209 res |= - (1 << shift);
220 * mono_unwind_ops_encode:
222 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
223 * Return a pointer to malloc'ed memory.
226 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
231 guint8 *buf, *p, *res;
233 p = buf = g_malloc0 (4096);
237 for (; l; l = l->next) {
242 /* Convert the register from the hw encoding to the dwarf encoding */
243 reg = mono_hw_reg_to_dwarf_reg (op->reg);
245 /* Emit an advance_loc if neccesary */
246 while (op->when > loc) {
247 if (op->when - loc < 32) {
248 *p ++ = DW_CFA_advance_loc | (op->when - loc);
251 *p ++ = DW_CFA_advance_loc | (30);
259 encode_uleb128 (reg, p, &p);
260 encode_uleb128 (op->val, p, &p);
262 case DW_CFA_def_cfa_offset:
264 encode_uleb128 (op->val, p, &p);
266 case DW_CFA_def_cfa_register:
268 encode_uleb128 (reg, p, &p);
272 *p ++ = DW_CFA_offset_extended_sf;
273 encode_uleb128 (reg, p, &p);
274 encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
276 *p ++ = DW_CFA_offset | reg;
277 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
281 g_assert_not_reached ();
286 g_assert (p - buf < 4096);
288 res = g_malloc (p - buf);
289 memcpy (res, buf, p - buf);
295 #define UNW_DEBUG(stmt) do { stmt; } while (0)
297 #define UNW_DEBUG(stmt) do { } while (0)
300 static G_GNUC_UNUSED void
301 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
305 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
307 for (i = 0; i < nregs; ++i)
308 if (locations [i].loc_type == LOC_OFFSET)
309 printf ("r%d@%d(cfa) ", i, locations [i].offset);
314 * Given the state of the current frame as stored in REGS, execute the unwind
315 * operations in unwind_info until the location counter reaches POS. The result is
316 * stored back into REGS. OUT_CFA will receive the value of the CFA.
317 * This function is signal safe.
320 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
321 guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs,
322 int nregs, guint8 **out_cfa)
324 Loc locations [NUM_REGS];
325 int i, pos, reg, cfa_reg, cfa_offset, offset;
329 g_assert (nregs <= NUM_REGS);
331 for (i = 0; i < nregs; ++i)
332 locations [i].loc_type = LOC_SAME;
338 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
342 case DW_CFA_advance_loc:
343 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
348 reg = mono_dwarf_reg_to_hw_reg (*p & 0x3f);
350 locations [reg].loc_type = LOC_OFFSET;
351 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
358 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
359 cfa_offset = decode_uleb128 (p, &p);
361 case DW_CFA_def_cfa_offset:
362 cfa_offset = decode_uleb128 (p, &p);
364 case DW_CFA_def_cfa_register:
365 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
367 case DW_CFA_offset_extended_sf:
368 reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
369 offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
371 case DW_CFA_advance_loc4:
376 g_assert_not_reached ();
381 g_assert_not_reached ();
385 cfa_val = (guint8*)regs [cfa_reg] + cfa_offset;
386 for (i = 0; i < nregs; ++i) {
387 if (locations [i].loc_type == LOC_OFFSET)
388 regs [i] = *(gssize*)(cfa_val + locations [i].offset);
395 mono_unwind_init (void)
397 InitializeCriticalSection (&unwind_mutex);
399 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
403 mono_unwind_cleanup (void)
407 DeleteCriticalSection (&unwind_mutex);
412 for (i = 0; i < cached_info_next; ++i) {
413 MonoUnwindInfo *cached = cached_info [i];
418 g_free (cached_info);
422 * mono_cache_unwind_info
424 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
425 * to mono_get_cached_unwind_info to get a cached copy of the info.
426 * A copy is made of the unwind info.
427 * This function is useful for two reasons:
428 * - many methods have the same unwind info
429 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
432 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
435 MonoUnwindInfo *info;
439 if (cached_info == NULL) {
440 cached_info_size = 16;
441 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
444 for (i = 0; i < cached_info_next; ++i) {
445 MonoUnwindInfo *cached = cached_info [i];
447 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
453 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
454 info->len = unwind_info_len;
455 memcpy (&info->info, unwind_info, unwind_info_len);
457 i = cached_info_next;
459 if (cached_info_next >= cached_info_size) {
460 MonoUnwindInfo **old_table, **new_table;
463 * Have to resize the table, while synchronizing with
464 * mono_get_cached_unwind_info () using hazard pointers.
467 old_table = cached_info;
468 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
470 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
472 mono_memory_barrier ();
474 cached_info = new_table;
476 mono_memory_barrier ();
478 mono_thread_hazardous_free_or_queue (old_table, g_free);
480 cached_info_size *= 2;
483 cached_info [cached_info_next ++] = info;
485 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
492 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
497 /* Get the pointer */
499 /* If we don't have hazard pointers just return the
503 /* Make it hazardous */
504 mono_hazard_pointer_set (hp, hazard_index, p);
505 /* Check that it's still the same. If not, try
508 mono_hazard_pointer_clear (hp, hazard_index);
518 * This function is signal safe.
521 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
523 MonoUnwindInfo **table;
524 MonoUnwindInfo *info;
526 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
528 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
530 info = table [index];
532 *unwind_info_len = info->len;
535 mono_hazard_pointer_clear (hp, 0);
541 * mono_unwind_get_dwarf_data_align:
543 * Return the data alignment used by the encoded unwind information.
546 mono_unwind_get_dwarf_data_align (void)
548 return DWARF_DATA_ALIGN;
552 * mono_unwind_get_dwarf_pc_reg:
554 * Return the dwarf register number of the register holding the ip of the
558 mono_unwind_get_dwarf_pc_reg (void)
564 decode_cie_op (guint8 *p, guint8 **endp)
569 case DW_CFA_advance_loc:
574 decode_uleb128 (p, &p);
581 decode_uleb128 (p, &p);
582 decode_uleb128 (p, &p);
584 case DW_CFA_def_cfa_offset:
585 decode_uleb128 (p, &p);
587 case DW_CFA_def_cfa_register:
588 decode_uleb128 (p, &p);
590 case DW_CFA_advance_loc4:
594 g_assert_not_reached ();
599 g_assert_not_reached ();
606 * mono_unwind_get_ops_from_fde:
608 * Return the unwind opcodes encoded in a DWARF FDE entry.
611 mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len)
613 guint8 *p, *cie, *code, *fde_cfi, *cie_cfi;
614 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
615 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
616 gint32 i, cie_aug_len, buf_len;
621 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
627 // FIXME: Endianess ?
628 fde_len = *(guint32*)p;
629 g_assert (fde_len != 0xffffffff && fde_len != 0);
631 cie_offset = *(guint32*)p;
632 cie = p - cie_offset;
634 pc_begin = *(gint32*)p;
637 pc_range = *(guint32*)p;
639 aug_len = decode_uleb128 (p, &p);
640 g_assert (aug_len == 0);
642 fde_data_len = fde + 4 + fde_len - p;
646 cie_len = *(guint32*)p;
648 cie_id = *(guint32*)p;
649 g_assert (cie_id == 0);
652 g_assert (cie_version == 1);
654 cie_aug_str = (char*)p;
655 p += strlen (cie_aug_str) + 1;
656 code_align = decode_uleb128 (p, &p);
657 data_align = decode_sleb128 (p, &p);
658 return_reg = decode_uleb128 (p, &p);
659 if (strstr (cie_aug_str, "z")) {
660 cie_aug_len = decode_uleb128 (p, &p);
665 /* Make sure the FDE uses the same constants as we do */
666 g_assert (code_align == 1);
667 g_assert (data_align == DWARF_DATA_ALIGN);
668 g_assert (return_reg == DWARF_PC_REG);
670 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
671 buf = g_malloc0 (buf_len);
675 while (p < cie + cie_len + 4) {
676 if (*p == DW_CFA_nop)
679 decode_cie_op (p, &p);
681 memcpy (buf + i, cie_cfi, p - cie_cfi);
685 while (p < fde + fde_len + 4) {
686 if (*p == DW_CFA_nop)
689 decode_cie_op (p, &p);
691 memcpy (buf + i, fde_cfi, p - fde_cfi);
693 g_assert (i <= buf_len);
697 return g_realloc (buf, i);