2 * unwind.c: Stack Unwinding Interface
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2008 Novell, Inc.
11 #include "mini-unwind.h"
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 for (i = 0; i < sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int); ++i) {
108 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
111 #ifdef TARGET_POWERPC
112 map_dwarf_reg_to_hw_reg [DWARF_PC_REG] = ppc_lr;
115 mono_memory_barrier ();
116 dwarf_reg_to_hw_reg_inited = TRUE;
120 mono_dwarf_reg_to_hw_reg (int reg)
122 if (!dwarf_reg_to_hw_reg_inited)
125 return map_dwarf_reg_to_hw_reg [reg];
128 static G_GNUC_UNUSED void
129 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
134 guint8 b = value & 0x7f;
136 if (value != 0) /* more bytes to come */
144 static G_GNUC_UNUSED void
145 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
148 gboolean negative = (value < 0);
156 /* the following is unnecessary if the
157 * implementation of >>= uses an arithmetic rather
158 * than logical shift for a signed left operand
162 value |= - (1 <<(size - 7));
163 /* sign bit of byte is second high order bit (0x40) */
164 if ((value == 0 && !(byte & 0x40)) ||
165 (value == -1 && (byte & 0x40)))
175 static inline guint32
176 decode_uleb128 (guint8 *buf, guint8 **endbuf)
186 res = res | (((int)(b & 0x7f)) << shift);
198 decode_sleb128 (guint8 *buf, guint8 **endbuf)
208 res = res | (((int)(b & 0x7f)) << shift);
211 if (shift < 32 && (b & 0x40))
212 res |= - (1 << shift);
223 * mono_unwind_ops_encode:
225 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
226 * Return a pointer to malloc'ed memory.
229 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
234 guint8 *buf, *p, *res;
236 p = buf = g_malloc0 (4096);
240 for (; l; l = l->next) {
245 /* Convert the register from the hw encoding to the dwarf encoding */
246 reg = mono_hw_reg_to_dwarf_reg (op->reg);
248 /* Emit an advance_loc if neccesary */
249 while (op->when > loc) {
250 if (op->when - loc < 32) {
251 *p ++ = DW_CFA_advance_loc | (op->when - loc);
254 *p ++ = DW_CFA_advance_loc | (30);
262 encode_uleb128 (reg, p, &p);
263 encode_uleb128 (op->val, p, &p);
265 case DW_CFA_def_cfa_offset:
267 encode_uleb128 (op->val, p, &p);
269 case DW_CFA_def_cfa_register:
271 encode_uleb128 (reg, p, &p);
275 *p ++ = DW_CFA_offset_extended_sf;
276 encode_uleb128 (reg, p, &p);
277 encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
279 *p ++ = DW_CFA_offset | reg;
280 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
284 g_assert_not_reached ();
289 g_assert (p - buf < 4096);
291 res = g_malloc (p - buf);
292 memcpy (res, buf, p - buf);
298 #define UNW_DEBUG(stmt) do { stmt; } while (0)
300 #define UNW_DEBUG(stmt) do { } while (0)
303 static G_GNUC_UNUSED void
304 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
308 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
310 for (i = 0; i < nregs; ++i)
311 if (locations [i].loc_type == LOC_OFFSET)
312 printf ("r%d@%d(cfa) ", i, locations [i].offset);
317 * Given the state of the current frame as stored in REGS, execute the unwind
318 * operations in unwind_info until the location counter reaches POS. The result is
319 * stored back into REGS. OUT_CFA will receive the value of the CFA.
320 * This function is signal safe.
323 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
324 guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs,
325 int nregs, guint8 **out_cfa)
327 Loc locations [NUM_REGS];
328 int i, pos, reg, cfa_reg, cfa_offset;
332 for (i = 0; i < NUM_REGS; ++i)
333 locations [i].loc_type = LOC_SAME;
339 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
343 case DW_CFA_advance_loc:
344 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
351 locations [reg].loc_type = LOC_OFFSET;
352 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
359 cfa_reg = decode_uleb128 (p, &p);
360 cfa_offset = decode_uleb128 (p, &p);
362 case DW_CFA_def_cfa_offset:
363 cfa_offset = decode_uleb128 (p, &p);
365 case DW_CFA_def_cfa_register:
366 cfa_reg = decode_uleb128 (p, &p);
368 case DW_CFA_offset_extended_sf:
369 reg = decode_uleb128 (p, &p);
370 locations [reg].loc_type = LOC_OFFSET;
371 locations [reg].offset = decode_sleb128 (p, &p) * DWARF_DATA_ALIGN;
373 case DW_CFA_advance_loc4:
378 g_assert_not_reached ();
383 g_assert_not_reached ();
387 cfa_val = (guint8*)regs [mono_dwarf_reg_to_hw_reg (cfa_reg)] + cfa_offset;
388 for (i = 0; i < NUM_REGS; ++i) {
389 if (locations [i].loc_type == LOC_OFFSET) {
390 int hreg = mono_dwarf_reg_to_hw_reg (i);
391 g_assert (hreg < nregs);
392 regs [hreg] = *(gssize*)(cfa_val + locations [i].offset);
400 mono_unwind_init (void)
402 InitializeCriticalSection (&unwind_mutex);
404 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
408 mono_unwind_cleanup (void)
412 DeleteCriticalSection (&unwind_mutex);
417 for (i = 0; i < cached_info_next; ++i) {
418 MonoUnwindInfo *cached = cached_info [i];
423 g_free (cached_info);
427 * mono_cache_unwind_info
429 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
430 * to mono_get_cached_unwind_info to get a cached copy of the info.
431 * A copy is made of the unwind info.
432 * This function is useful for two reasons:
433 * - many methods have the same unwind info
434 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
437 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
440 MonoUnwindInfo *info;
444 if (cached_info == NULL) {
445 cached_info_size = 16;
446 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
449 for (i = 0; i < cached_info_next; ++i) {
450 MonoUnwindInfo *cached = cached_info [i];
452 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
458 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
459 info->len = unwind_info_len;
460 memcpy (&info->info, unwind_info, unwind_info_len);
462 i = cached_info_next;
464 if (cached_info_next >= cached_info_size) {
465 MonoUnwindInfo **old_table, **new_table;
468 * Have to resize the table, while synchronizing with
469 * mono_get_cached_unwind_info () using hazard pointers.
472 old_table = cached_info;
473 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
475 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
477 mono_memory_barrier ();
479 cached_info = new_table;
481 mono_memory_barrier ();
483 mono_thread_hazardous_free_or_queue (old_table, g_free);
485 cached_info_size *= 2;
488 cached_info [cached_info_next ++] = info;
490 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
497 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
502 /* Get the pointer */
504 /* If we don't have hazard pointers just return the
508 /* Make it hazardous */
509 mono_hazard_pointer_set (hp, hazard_index, p);
510 /* Check that it's still the same. If not, try
513 mono_hazard_pointer_clear (hp, hazard_index);
523 * This function is signal safe.
526 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
528 MonoUnwindInfo **table;
529 MonoUnwindInfo *info;
531 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
533 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
535 info = table [index];
537 *unwind_info_len = info->len;
540 mono_hazard_pointer_clear (hp, 0);
546 * mono_unwind_get_dwarf_data_align:
548 * Return the data alignment used by the encoded unwind information.
551 mono_unwind_get_dwarf_data_align (void)
553 return DWARF_DATA_ALIGN;
557 * mono_unwind_get_dwarf_pc_reg:
559 * Return the dwarf register number of the register holding the ip of the
563 mono_unwind_get_dwarf_pc_reg (void)
569 decode_cie_op (guint8 *p, guint8 **endp)
574 case DW_CFA_advance_loc:
579 decode_uleb128 (p, &p);
586 decode_uleb128 (p, &p);
587 decode_uleb128 (p, &p);
589 case DW_CFA_def_cfa_offset:
590 decode_uleb128 (p, &p);
592 case DW_CFA_def_cfa_register:
593 decode_uleb128 (p, &p);
595 case DW_CFA_advance_loc4:
599 g_assert_not_reached ();
604 g_assert_not_reached ();
611 * mono_unwind_get_ops_from_fde:
613 * Return the unwind opcodes encoded in a DWARF FDE entry.
616 mono_unwind_get_ops_from_fde (guint8 *fde, guint32 *out_len, guint32 *code_len)
618 guint8 *p, *cie, *code, *fde_cfi, *cie_cfi;
619 gint32 fde_len, cie_offset, pc_begin, pc_range, aug_len, fde_data_len;
620 gint32 cie_len, cie_id, cie_version, code_align, data_align, return_reg;
621 gint32 i, cie_aug_len, buf_len;
626 * http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
632 // FIXME: Endianess ?
633 fde_len = *(guint32*)p;
634 g_assert (fde_len != 0xffffffff && fde_len != 0);
636 cie_offset = *(guint32*)p;
637 cie = p - cie_offset;
639 pc_begin = *(gint32*)p;
642 pc_range = *(guint32*)p;
644 aug_len = decode_uleb128 (p, &p);
645 g_assert (aug_len == 0);
647 fde_data_len = fde + 4 + fde_len - p;
650 *code_len = pc_range;
654 cie_len = *(guint32*)p;
656 cie_id = *(guint32*)p;
657 g_assert (cie_id == 0);
660 g_assert (cie_version == 1);
662 cie_aug_str = (char*)p;
663 p += strlen (cie_aug_str) + 1;
664 code_align = decode_uleb128 (p, &p);
665 data_align = decode_sleb128 (p, &p);
666 return_reg = decode_uleb128 (p, &p);
667 if (strstr (cie_aug_str, "z")) {
668 cie_aug_len = decode_uleb128 (p, &p);
673 /* Make sure the FDE uses the same constants as we do */
674 g_assert (code_align == 1);
675 g_assert (data_align == DWARF_DATA_ALIGN);
676 g_assert (return_reg == DWARF_PC_REG);
678 buf_len = (cie + cie_len + 4 - cie_cfi) + (fde + fde_len + 4 - fde_cfi);
679 buf = g_malloc0 (buf_len);
683 while (p < cie + cie_len + 4) {
684 if (*p == DW_CFA_nop)
687 decode_cie_op (p, &p);
689 memcpy (buf + i, cie_cfi, p - cie_cfi);
693 while (p < fde + fde_len + 4) {
694 if (*p == DW_CFA_nop)
697 decode_cie_op (p, &p);
699 memcpy (buf + i, fde_cfi, p - fde_cfi);
701 g_assert (i <= buf_len);
705 return g_realloc (buf, i);