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(__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 (__i386__)
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))
59 static int map_hw_reg_to_dwarf_reg [16];
61 #define DWARF_DATA_ALIGN 0
62 #define DWARF_PC_REG -1
65 static gboolean dwarf_reg_to_hw_reg_inited;
67 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
70 * mono_hw_reg_to_dwarf_reg:
72 * Map the hardware register number REG to the register number used by DWARF.
75 mono_hw_reg_to_dwarf_reg (int reg)
78 g_assert_not_reached ();
81 return map_hw_reg_to_dwarf_reg [reg];
90 g_assert (NUM_REGS > 0);
91 g_assert (sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int) == NUM_REGS);
92 for (i = 0; i < NUM_REGS; ++i) {
93 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
96 mono_memory_barrier ();
97 dwarf_reg_to_hw_reg_inited = TRUE;
101 mono_dwarf_reg_to_hw_reg (int reg)
103 if (!dwarf_reg_to_hw_reg_inited)
106 return map_dwarf_reg_to_hw_reg [reg];
109 static G_GNUC_UNUSED void
110 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
115 guint8 b = value & 0x7f;
117 if (value != 0) /* more bytes to come */
125 static inline guint32
126 decode_uleb128 (guint8 *buf, guint8 **endbuf)
136 res = res | (((int)(b & 0x7f)) << shift);
148 * mono_unwind_ops_encode:
150 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
151 * Return a pointer to malloc'ed memory.
154 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
159 guint8 *buf, *p, *res;
161 p = buf = g_malloc0 (256);
165 for (; l; l = l->next) {
170 /* Convert the register from the hw encoding to the dwarf encoding */
171 reg = mono_hw_reg_to_dwarf_reg (op->reg);
173 /* Emit an advance_loc if neccesary */
174 while (op->when > loc) {
175 if (op->when - loc < 32) {
176 *p ++ = DW_CFA_advance_loc | (op->when - loc);
179 *p ++ = DW_CFA_advance_loc | (30);
187 encode_uleb128 (reg, p, &p);
188 encode_uleb128 (op->val, p, &p);
190 case DW_CFA_def_cfa_offset:
192 encode_uleb128 (op->val, p, &p);
194 case DW_CFA_def_cfa_register:
196 encode_uleb128 (reg, p, &p);
199 *p ++ = DW_CFA_offset | reg;
200 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
203 g_assert_not_reached ();
208 g_assert (p - buf < 256);
210 res = g_malloc (p - buf);
211 memcpy (res, buf, p - buf);
217 #define UNW_DEBUG(stmt) do { stmt; } while (0)
219 #define UNW_DEBUG(stmt) do { } while (0)
222 static G_GNUC_UNUSED void
223 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
227 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
229 for (i = 0; i < nregs; ++i)
230 if (locations [i].loc_type == LOC_OFFSET)
231 printf ("r%d@%d(cfa) ", i, locations [i].offset);
236 * Given the state of the current frame as stored in REGS, execute the unwind
237 * operations in unwind_info until the location counter reaches POS. The result is
238 * stored back into REGS. OUT_CFA will receive the value of the CFA.
239 * This function is signal safe.
242 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
243 guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs,
244 int nregs, guint8 **out_cfa)
246 Loc locations [NUM_REGS];
247 int i, pos, reg, cfa_reg, cfa_offset;
251 g_assert (nregs <= NUM_REGS);
253 for (i = 0; i < nregs; ++i)
254 locations [i].loc_type = LOC_SAME;
258 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
262 case DW_CFA_advance_loc:
263 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
268 reg = mono_dwarf_reg_to_hw_reg (*p & 0x3f);
270 locations [reg].loc_type = LOC_OFFSET;
271 locations [reg].offset = decode_uleb128 (p, &p) * DWARF_DATA_ALIGN;
278 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
279 cfa_offset = decode_uleb128 (p, &p);
281 case DW_CFA_def_cfa_offset:
282 cfa_offset = decode_uleb128 (p, &p);
284 case DW_CFA_def_cfa_register:
285 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
288 g_assert_not_reached ();
293 g_assert_not_reached ();
297 cfa_val = (guint8*)regs [cfa_reg] + cfa_offset;
298 for (i = 0; i < nregs; ++i) {
299 if (locations [i].loc_type == LOC_OFFSET)
300 regs [i] = *(gssize*)(cfa_val + locations [i].offset);
307 mono_unwind_init (void)
309 InitializeCriticalSection (&unwind_mutex);
311 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
315 mono_unwind_cleanup (void)
319 DeleteCriticalSection (&unwind_mutex);
324 for (i = 0; i < cached_info_next; ++i) {
325 MonoUnwindInfo *cached = cached_info [i];
330 g_free (cached_info);
334 * mono_cache_unwind_info
336 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
337 * to mono_get_cached_unwind_info to get a cached copy of the info.
338 * A copy is made of the unwind info.
339 * This function is useful for two reasons:
340 * - many methods have the same unwind info
341 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
344 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
347 MonoUnwindInfo *info;
351 if (cached_info == NULL) {
352 cached_info_size = 16;
353 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
356 for (i = 0; i < cached_info_next; ++i) {
357 MonoUnwindInfo *cached = cached_info [i];
359 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
365 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
366 info->len = unwind_info_len;
367 memcpy (&info->info, unwind_info, unwind_info_len);
369 i = cached_info_next;
371 if (cached_info_next >= cached_info_size) {
372 MonoUnwindInfo **old_table, **new_table;
375 * Have to resize the table, while synchronizing with
376 * mono_get_cached_unwind_info () using hazard pointers.
379 old_table = cached_info;
380 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
382 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
384 mono_memory_barrier ();
386 cached_info = new_table;
388 mono_memory_barrier ();
390 mono_thread_hazardous_free_or_queue (old_table, g_free);
392 cached_info_size *= 2;
395 cached_info [cached_info_next ++] = info;
397 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
404 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
409 /* Get the pointer */
411 /* If we don't have hazard pointers just return the
415 /* Make it hazardous */
416 mono_hazard_pointer_set (hp, hazard_index, p);
417 /* Check that it's still the same. If not, try
420 mono_hazard_pointer_clear (hp, hazard_index);
430 * This function is signal safe.
433 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
435 MonoUnwindInfo **table;
436 MonoUnwindInfo *info;
438 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
440 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
442 info = table [index];
444 *unwind_info_len = info->len;
447 mono_hazard_pointer_clear (hp, 0);
453 * mono_unwind_get_dwarf_data_align:
455 * Return the data alignment used by the encoded unwind information.
458 mono_unwind_get_dwarf_data_align (void)
460 return DWARF_DATA_ALIGN;
464 * mono_unwind_get_dwarf_pc_reg:
466 * Return the dwarf register number of the register holding the ip of the
470 mono_unwind_get_dwarf_pc_reg (void)