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 #elif defined(__arm__)
46 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0040a/IHI0040A_aadwarf.pdf
47 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
49 #define DWARF_DATA_ALIGN - 4
51 static int map_hw_reg_to_dwarf_reg [0];
53 #define DWARF_DATA_ALIGN 0
56 static gboolean dwarf_reg_to_hw_reg_inited;
58 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
61 * mono_hw_reg_to_dwarf_reg:
63 * Map the hardware register number REG to the register number used by DWARF.
66 mono_hw_reg_to_dwarf_reg (int reg)
69 g_assert_not_reached ();
72 return map_hw_reg_to_dwarf_reg [reg];
81 g_assert (NUM_REGS > 0);
82 g_assert (sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int) == NUM_REGS);
83 for (i = 0; i < NUM_REGS; ++i) {
84 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
87 mono_memory_barrier ();
88 dwarf_reg_to_hw_reg_inited = TRUE;
92 mono_dwarf_reg_to_hw_reg (int reg)
94 if (!dwarf_reg_to_hw_reg_inited)
97 return map_dwarf_reg_to_hw_reg [reg];
100 static G_GNUC_UNUSED void
101 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
106 guint8 b = value & 0x7f;
108 if (value != 0) /* more bytes to come */
116 static inline guint32
117 decode_uleb128 (guint8 *buf, guint8 **endbuf)
127 res = res | (((int)(b & 0x7f)) << shift);
139 * mono_unwind_ops_encode:
141 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
142 * Return a pointer to malloc'ed memory.
145 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
150 guint8 *buf, *p, *res;
152 p = buf = g_malloc0 (256);
156 for (; l; l = l->next) {
161 /* Convert the register from the hw encoding to the dwarf encoding */
162 reg = mono_hw_reg_to_dwarf_reg (op->reg);
164 /* Emit an advance_loc if neccesary */
165 if (op->when > loc) {
166 g_assert (op->when - loc < 32);
167 *p ++ = DW_CFA_advance_loc | (op->when - loc);
173 encode_uleb128 (reg, p, &p);
174 encode_uleb128 (op->val, p, &p);
176 case DW_CFA_def_cfa_offset:
178 encode_uleb128 (op->val, p, &p);
180 case DW_CFA_def_cfa_register:
182 encode_uleb128 (reg, p, &p);
185 *p ++ = DW_CFA_offset | reg;
186 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
189 g_assert_not_reached ();
196 g_assert (p - buf < 256);
198 res = g_malloc (p - buf);
199 memcpy (res, buf, p - buf);
205 #define UNW_DEBUG(stmt) do { stmt; } while (0)
207 #define UNW_DEBUG(stmt) do { } while (0)
210 static G_GNUC_UNUSED void
211 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
215 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
217 for (i = 0; i < nregs; ++i)
218 if (locations [i].loc_type == LOC_OFFSET)
219 printf ("r%d@%d(cfa) ", i, locations [i].offset);
224 * Given the state of the current frame as stored in REGS, execute the unwind
225 * operations in unwind_info until the location counter reaches POS. The result is
226 * stored back into REGS. OUT_CFA will receive the value of the CFA.
227 * This function is signal safe.
230 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
231 int data_align_factor,
232 guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs,
233 int nregs, guint8 **out_cfa)
235 Loc locations [NUM_REGS];
236 int i, pos, reg, cfa_reg, cfa_offset;
240 g_assert (nregs <= NUM_REGS);
242 for (i = 0; i < nregs; ++i)
243 locations [i].loc_type = LOC_SAME;
247 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
251 case DW_CFA_advance_loc:
252 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
257 reg = mono_dwarf_reg_to_hw_reg (*p & 0x3f);
259 locations [reg].loc_type = LOC_OFFSET;
260 locations [reg].offset = decode_uleb128 (p, &p) * data_align_factor;
267 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
268 cfa_offset = decode_uleb128 (p, &p);
270 case DW_CFA_def_cfa_offset:
271 cfa_offset = decode_uleb128 (p, &p);
273 case DW_CFA_def_cfa_register:
274 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
277 g_assert_not_reached ();
282 g_assert_not_reached ();
286 cfa_val = (guint8*)regs [cfa_reg] + cfa_offset;
287 for (i = 0; i < nregs; ++i) {
288 if (locations [i].loc_type == LOC_OFFSET)
289 regs [i] = *(gssize*)(cfa_val + locations [i].offset);
296 mono_unwind_init (void)
298 InitializeCriticalSection (&unwind_mutex);
300 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
304 mono_unwind_cleanup (void)
308 DeleteCriticalSection (&unwind_mutex);
313 for (i = 0; i < cached_info_next; ++i) {
314 MonoUnwindInfo *cached = cached_info [i];
319 g_free (cached_info);
323 * mono_cache_unwind_info
325 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
326 * to mono_get_cached_unwind_info to get a cached copy of the info.
327 * A copy is made of the unwind info.
328 * This function is useful for two reasons:
329 * - many methods have the same unwind info
330 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
333 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
336 MonoUnwindInfo *info;
340 if (cached_info == NULL) {
341 cached_info_size = 16;
342 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
345 for (i = 0; i < cached_info_next; ++i) {
346 MonoUnwindInfo *cached = cached_info [i];
348 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
354 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
355 info->len = unwind_info_len;
356 memcpy (&info->info, unwind_info, unwind_info_len);
358 i = cached_info_next;
360 if (cached_info_next >= cached_info_size) {
361 MonoUnwindInfo **old_table, **new_table;
364 * Have to resize the table, while synchronizing with
365 * mono_get_cached_unwind_info () using hazard pointers.
368 old_table = cached_info;
369 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
371 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
373 mono_memory_barrier ();
375 cached_info = new_table;
377 mono_memory_barrier ();
379 mono_thread_hazardous_free_or_queue (old_table, g_free);
381 cached_info_size *= 2;
384 cached_info [cached_info_next ++] = info;
386 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
393 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
398 /* Get the pointer */
400 /* If we don't have hazard pointers just return the
404 /* Make it hazardous */
405 mono_hazard_pointer_set (hp, hazard_index, p);
406 /* Check that it's still the same. If not, try
409 mono_hazard_pointer_clear (hp, hazard_index);
419 * This function is signal safe.
422 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
424 MonoUnwindInfo **table;
425 MonoUnwindInfo *info;
427 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
429 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
431 info = table [index];
433 *unwind_info_len = info->len;
436 mono_hazard_pointer_clear (hp, 0);