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)
50 #elif defined (__i386__)
51 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
53 #define NUM_REGS X86_NREG + 1
54 #define DWARF_DATA_ALIGN (-4)
56 static int map_hw_reg_to_dwarf_reg [0];
58 #define DWARF_DATA_ALIGN 0
61 static gboolean dwarf_reg_to_hw_reg_inited;
63 static int map_dwarf_reg_to_hw_reg [NUM_REGS];
66 * mono_hw_reg_to_dwarf_reg:
68 * Map the hardware register number REG to the register number used by DWARF.
71 mono_hw_reg_to_dwarf_reg (int reg)
74 g_assert_not_reached ();
77 return map_hw_reg_to_dwarf_reg [reg];
86 g_assert (NUM_REGS > 0);
87 g_assert (sizeof (map_hw_reg_to_dwarf_reg) / sizeof (int) == NUM_REGS);
88 for (i = 0; i < NUM_REGS; ++i) {
89 map_dwarf_reg_to_hw_reg [mono_hw_reg_to_dwarf_reg (i)] = i;
92 mono_memory_barrier ();
93 dwarf_reg_to_hw_reg_inited = TRUE;
97 mono_dwarf_reg_to_hw_reg (int reg)
99 if (!dwarf_reg_to_hw_reg_inited)
102 return map_dwarf_reg_to_hw_reg [reg];
105 static G_GNUC_UNUSED void
106 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
111 guint8 b = value & 0x7f;
113 if (value != 0) /* more bytes to come */
121 static inline guint32
122 decode_uleb128 (guint8 *buf, guint8 **endbuf)
132 res = res | (((int)(b & 0x7f)) << shift);
144 * mono_unwind_ops_encode:
146 * Encode the unwind ops in UNWIND_OPS into the compact DWARF encoding.
147 * Return a pointer to malloc'ed memory.
150 mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
155 guint8 *buf, *p, *res;
157 p = buf = g_malloc0 (256);
161 for (; l; l = l->next) {
166 /* Convert the register from the hw encoding to the dwarf encoding */
167 reg = mono_hw_reg_to_dwarf_reg (op->reg);
169 /* Emit an advance_loc if neccesary */
170 if (op->when > loc) {
171 g_assert (op->when - loc < 32);
172 *p ++ = DW_CFA_advance_loc | (op->when - loc);
178 encode_uleb128 (reg, p, &p);
179 encode_uleb128 (op->val, p, &p);
181 case DW_CFA_def_cfa_offset:
183 encode_uleb128 (op->val, p, &p);
185 case DW_CFA_def_cfa_register:
187 encode_uleb128 (reg, p, &p);
190 *p ++ = DW_CFA_offset | reg;
191 encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
194 g_assert_not_reached ();
201 g_assert (p - buf < 256);
203 res = g_malloc (p - buf);
204 memcpy (res, buf, p - buf);
210 #define UNW_DEBUG(stmt) do { stmt; } while (0)
212 #define UNW_DEBUG(stmt) do { } while (0)
215 static G_GNUC_UNUSED void
216 print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *locations)
220 printf ("\t%x: cfa=r%d+%d ", ip, cfa_reg, cfa_offset);
222 for (i = 0; i < nregs; ++i)
223 if (locations [i].loc_type == LOC_OFFSET)
224 printf ("r%d@%d(cfa) ", i, locations [i].offset);
229 * Given the state of the current frame as stored in REGS, execute the unwind
230 * operations in unwind_info until the location counter reaches POS. The result is
231 * stored back into REGS. OUT_CFA will receive the value of the CFA.
232 * This function is signal safe.
235 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len,
236 int data_align_factor,
237 guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs,
238 int nregs, guint8 **out_cfa)
240 Loc locations [NUM_REGS];
241 int i, pos, reg, cfa_reg, cfa_offset;
245 g_assert (nregs <= NUM_REGS);
247 for (i = 0; i < nregs; ++i)
248 locations [i].loc_type = LOC_SAME;
252 while (pos <= ip - start_ip && p < unwind_info + unwind_info_len) {
256 case DW_CFA_advance_loc:
257 UNW_DEBUG (print_dwarf_state (cfa_reg, cfa_offset, pos, nregs, locations));
262 reg = mono_dwarf_reg_to_hw_reg (*p & 0x3f);
264 locations [reg].loc_type = LOC_OFFSET;
265 locations [reg].offset = decode_uleb128 (p, &p) * data_align_factor;
272 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
273 cfa_offset = decode_uleb128 (p, &p);
275 case DW_CFA_def_cfa_offset:
276 cfa_offset = decode_uleb128 (p, &p);
278 case DW_CFA_def_cfa_register:
279 cfa_reg = mono_dwarf_reg_to_hw_reg (decode_uleb128 (p, &p));
282 g_assert_not_reached ();
287 g_assert_not_reached ();
291 cfa_val = (guint8*)regs [cfa_reg] + cfa_offset;
292 for (i = 0; i < nregs; ++i) {
293 if (locations [i].loc_type == LOC_OFFSET)
294 regs [i] = *(gssize*)(cfa_val + locations [i].offset);
301 mono_unwind_init (void)
303 InitializeCriticalSection (&unwind_mutex);
305 mono_counters_register ("Unwind info size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unwind_info_size);
309 mono_unwind_cleanup (void)
313 DeleteCriticalSection (&unwind_mutex);
318 for (i = 0; i < cached_info_next; ++i) {
319 MonoUnwindInfo *cached = cached_info [i];
324 g_free (cached_info);
328 * mono_cache_unwind_info
330 * Save UNWIND_INFO in the unwind info cache and return an id which can be passed
331 * to mono_get_cached_unwind_info to get a cached copy of the info.
332 * A copy is made of the unwind info.
333 * This function is useful for two reasons:
334 * - many methods have the same unwind info
335 * - MonoJitInfo->used_regs is an int so it can't store the pointer to the unwind info
338 mono_cache_unwind_info (guint8 *unwind_info, guint32 unwind_info_len)
341 MonoUnwindInfo *info;
345 if (cached_info == NULL) {
346 cached_info_size = 16;
347 cached_info = g_new0 (MonoUnwindInfo*, cached_info_size);
350 for (i = 0; i < cached_info_next; ++i) {
351 MonoUnwindInfo *cached = cached_info [i];
353 if (cached->len == unwind_info_len && memcmp (cached->info, unwind_info, unwind_info_len) == 0) {
359 info = g_malloc (sizeof (MonoUnwindInfo) + unwind_info_len);
360 info->len = unwind_info_len;
361 memcpy (&info->info, unwind_info, unwind_info_len);
363 i = cached_info_next;
365 if (cached_info_next >= cached_info_size) {
366 MonoUnwindInfo **old_table, **new_table;
369 * Have to resize the table, while synchronizing with
370 * mono_get_cached_unwind_info () using hazard pointers.
373 old_table = cached_info;
374 new_table = g_new0 (MonoUnwindInfo*, cached_info_size * 2);
376 memcpy (new_table, cached_info, cached_info_size * sizeof (MonoUnwindInfo*));
378 mono_memory_barrier ();
380 cached_info = new_table;
382 mono_memory_barrier ();
384 mono_thread_hazardous_free_or_queue (old_table, g_free);
386 cached_info_size *= 2;
389 cached_info [cached_info_next ++] = info;
391 unwind_info_size += sizeof (MonoUnwindInfo) + unwind_info_len;
398 get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
403 /* Get the pointer */
405 /* If we don't have hazard pointers just return the
409 /* Make it hazardous */
410 mono_hazard_pointer_set (hp, hazard_index, p);
411 /* Check that it's still the same. If not, try
414 mono_hazard_pointer_clear (hp, hazard_index);
424 * This function is signal safe.
427 mono_get_cached_unwind_info (guint32 index, guint32 *unwind_info_len)
429 MonoUnwindInfo **table;
430 MonoUnwindInfo *info;
432 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
434 table = get_hazardous_pointer ((gpointer volatile*)&cached_info, hp, 0);
436 info = table [index];
438 *unwind_info_len = info->len;
441 mono_hazard_pointer_clear (hp, 0);