[x86/Linux] Fix GetCallerSp (#9384)
[platform/upstream/coreclr.git] / src / pal / src / exception / seh-unwind.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 /*++
6
7
8
9 Module Name:
10
11     seh-unwind.cpp
12
13 Abstract:
14
15     Implementation of exception API functions based on
16     the Unwind API.
17
18
19
20 --*/
21
22 #ifndef FEATURE_PAL_SXS
23 #error FEATURE_PAL_SXS needs to be defined for this file.
24 #endif // !FEATURE_PAL_SXS
25
26 #include "pal/context.h"
27 #include "pal.h"
28 #include <dlfcn.h>
29  
30 #if HAVE_LIBUNWIND_H
31 #ifndef __linux__
32 #define UNW_LOCAL_ONLY
33 #endif // !__linux__       
34 #include <libunwind.h>
35 #ifdef __linux__
36 #ifdef HAVE_LIBUNWIND_PTRACE
37 #include <libunwind-ptrace.h>
38 #endif // HAVE_LIBUNWIND_PTRACE
39 #endif // __linux__    
40 #endif // HAVE_LIBUNWIND_H
41
42
43 //----------------------------------------------------------------------
44 // Virtual Unwinding
45 //----------------------------------------------------------------------
46
47 #if HAVE_LIBUNWIND_H
48 #if UNWIND_CONTEXT_IS_UCONTEXT_T
49
50 #if defined(_AMD64_)
51 #define ASSIGN_UNWIND_REGS \
52     ASSIGN_REG(Rip)        \
53     ASSIGN_REG(Rsp)        \
54     ASSIGN_REG(Rbp)        \
55     ASSIGN_REG(Rbx)        \
56     ASSIGN_REG(R12)        \
57     ASSIGN_REG(R13)        \
58     ASSIGN_REG(R14)        \
59     ASSIGN_REG(R15)
60 #elif defined(_ARM64_)
61 #define ASSIGN_UNWIND_REGS \
62     ASSIGN_REG(Pc)         \
63     ASSIGN_REG(Sp)         \
64     ASSIGN_REG(Fp)         \
65     ASSIGN_REG(Lr)         \
66     ASSIGN_REG(X19)        \
67     ASSIGN_REG(X20)        \
68     ASSIGN_REG(X21)        \
69     ASSIGN_REG(X22)        \
70     ASSIGN_REG(X23)        \
71     ASSIGN_REG(X24)        \
72     ASSIGN_REG(X25)        \
73     ASSIGN_REG(X26)        \
74     ASSIGN_REG(X27)        \
75     ASSIGN_REG(X28)
76 #elif defined(_X86_)
77 #define ASSIGN_UNWIND_REGS \
78     ASSIGN_REG(Eip)        \
79     ASSIGN_REG(Esp)        \
80     ASSIGN_REG(Ebp)        \
81     ASSIGN_REG(Ebx)        \
82     ASSIGN_REG(Esi)        \
83     ASSIGN_REG(Edi)
84 #else
85 #error unsupported architecture
86 #endif
87
88 static void WinContextToUnwindContext(CONTEXT *winContext, unw_context_t *unwContext)
89 {
90 #define ASSIGN_REG(reg) MCREG_##reg(unwContext->uc_mcontext) = winContext->reg;
91     ASSIGN_UNWIND_REGS
92 #undef ASSIGN_REG
93 }
94 #else
95 static void WinContextToUnwindContext(CONTEXT *winContext, unw_context_t *unwContext)
96 {
97 #if defined(_ARM_)    
98     // Assuming that unw_set_reg() on cursor will point the cursor to the
99     // supposed stack frame is dangerous for libunwind-arm in Linux.
100     // It is because libunwind's unw_cursor_t has other data structure
101     // initialized by unw_init_local(), which are not updated by
102     // unw_set_reg().
103     unwContext->regs[0] = 0;
104     unwContext->regs[1] = 0;
105     unwContext->regs[2] = 0;
106     unwContext->regs[3] = 0;
107     unwContext->regs[4] = winContext->R4;
108     unwContext->regs[5] = winContext->R5;
109     unwContext->regs[6] = winContext->R6;
110     unwContext->regs[7] = winContext->R7;
111     unwContext->regs[8] = winContext->R8;
112     unwContext->regs[9] = winContext->R9;
113     unwContext->regs[10] = winContext->R10;
114     unwContext->regs[11] = winContext->R11;
115     unwContext->regs[12] = 0;
116     unwContext->regs[13] = winContext->Sp;
117     unwContext->regs[14] = winContext->Lr;
118     unwContext->regs[15] = winContext->Pc;
119 #endif    
120
121
122 static void WinContextToUnwindCursor(CONTEXT *winContext, unw_cursor_t *cursor)
123 {
124 #if defined(_AMD64_)
125     unw_set_reg(cursor, UNW_REG_IP, winContext->Rip);
126     unw_set_reg(cursor, UNW_REG_SP, winContext->Rsp);
127     unw_set_reg(cursor, UNW_X86_64_RBP, winContext->Rbp);
128     unw_set_reg(cursor, UNW_X86_64_RBX, winContext->Rbx);
129     unw_set_reg(cursor, UNW_X86_64_R12, winContext->R12);
130     unw_set_reg(cursor, UNW_X86_64_R13, winContext->R13);
131     unw_set_reg(cursor, UNW_X86_64_R14, winContext->R14);
132     unw_set_reg(cursor, UNW_X86_64_R15, winContext->R15);
133 #elif defined(_X86_)
134     unw_set_reg(cursor, UNW_REG_IP, winContext->Eip);
135     unw_set_reg(cursor, UNW_REG_SP, winContext->Esp);
136     unw_set_reg(cursor, UNW_X86_EBP, winContext->Ebp);
137     unw_set_reg(cursor, UNW_X86_EBX, winContext->Ebx);
138     unw_set_reg(cursor, UNW_X86_ESI, winContext->Esi);
139     unw_set_reg(cursor, UNW_X86_EDI, winContext->Edi);
140 #endif
141 }
142 #endif
143
144 static void UnwindContextToWinContext(unw_cursor_t *cursor, CONTEXT *winContext)
145 {
146 #if defined(_AMD64_)
147     unw_get_reg(cursor, UNW_REG_IP, (unw_word_t *) &winContext->Rip);
148     unw_get_reg(cursor, UNW_REG_SP, (unw_word_t *) &winContext->Rsp);
149     unw_get_reg(cursor, UNW_X86_64_RBP, (unw_word_t *) &winContext->Rbp);
150     unw_get_reg(cursor, UNW_X86_64_RBX, (unw_word_t *) &winContext->Rbx);
151     unw_get_reg(cursor, UNW_X86_64_R12, (unw_word_t *) &winContext->R12);
152     unw_get_reg(cursor, UNW_X86_64_R13, (unw_word_t *) &winContext->R13);
153     unw_get_reg(cursor, UNW_X86_64_R14, (unw_word_t *) &winContext->R14);
154     unw_get_reg(cursor, UNW_X86_64_R15, (unw_word_t *) &winContext->R15);
155 #elif defined(_X86_)
156     unw_get_reg(cursor, UNW_REG_IP, (unw_word_t *) &winContext->Eip);
157     unw_get_reg(cursor, UNW_REG_SP, (unw_word_t *) &winContext->Esp);
158     unw_get_reg(cursor, UNW_REG_SP, (unw_word_t *) &winContext->ResumeEsp);
159     unw_get_reg(cursor, UNW_X86_EBP, (unw_word_t *) &winContext->Ebp);
160     unw_get_reg(cursor, UNW_X86_EBX, (unw_word_t *) &winContext->Ebx);
161     unw_get_reg(cursor, UNW_X86_ESI, (unw_word_t *) &winContext->Esi);
162     unw_get_reg(cursor, UNW_X86_EDI, (unw_word_t *) &winContext->Edi);
163 #elif defined(_ARM_)
164     unw_get_reg(cursor, UNW_REG_SP, (unw_word_t *) &winContext->Sp);
165     unw_get_reg(cursor, UNW_REG_IP, (unw_word_t *) &winContext->Pc);
166     unw_get_reg(cursor, UNW_ARM_R14, (unw_word_t *) &winContext->Lr);
167     unw_get_reg(cursor, UNW_ARM_R4, (unw_word_t *) &winContext->R4);
168     unw_get_reg(cursor, UNW_ARM_R5, (unw_word_t *) &winContext->R5);
169     unw_get_reg(cursor, UNW_ARM_R6, (unw_word_t *) &winContext->R6);
170     unw_get_reg(cursor, UNW_ARM_R7, (unw_word_t *) &winContext->R7);
171     unw_get_reg(cursor, UNW_ARM_R8, (unw_word_t *) &winContext->R8);
172     unw_get_reg(cursor, UNW_ARM_R9, (unw_word_t *) &winContext->R9);
173     unw_get_reg(cursor, UNW_ARM_R10, (unw_word_t *) &winContext->R10);
174     unw_get_reg(cursor, UNW_ARM_R11, (unw_word_t *) &winContext->R11);
175 #elif defined(_ARM64_)
176     unw_get_reg(cursor, UNW_REG_IP, (unw_word_t *) &winContext->Pc);
177     unw_get_reg(cursor, UNW_REG_SP, (unw_word_t *) &winContext->Sp);
178     unw_get_reg(cursor, UNW_AARCH64_X29, (unw_word_t *) &winContext->Fp);
179     unw_get_reg(cursor, UNW_AARCH64_X30, (unw_word_t *) &winContext->Lr);
180     unw_get_reg(cursor, UNW_AARCH64_X19, (unw_word_t *) &winContext->X19);
181     unw_get_reg(cursor, UNW_AARCH64_X20, (unw_word_t *) &winContext->X20);
182     unw_get_reg(cursor, UNW_AARCH64_X21, (unw_word_t *) &winContext->X21);
183     unw_get_reg(cursor, UNW_AARCH64_X22, (unw_word_t *) &winContext->X22);
184     unw_get_reg(cursor, UNW_AARCH64_X23, (unw_word_t *) &winContext->X23);
185     unw_get_reg(cursor, UNW_AARCH64_X24, (unw_word_t *) &winContext->X24);
186     unw_get_reg(cursor, UNW_AARCH64_X25, (unw_word_t *) &winContext->X25);
187     unw_get_reg(cursor, UNW_AARCH64_X26, (unw_word_t *) &winContext->X26);
188     unw_get_reg(cursor, UNW_AARCH64_X27, (unw_word_t *) &winContext->X27);
189     unw_get_reg(cursor, UNW_AARCH64_X28, (unw_word_t *) &winContext->X28);
190 #else
191 #error unsupported architecture
192 #endif
193 }
194
195 static void GetContextPointer(unw_cursor_t *cursor, unw_context_t *unwContext, int reg, SIZE_T **contextPointer)
196 {
197 #if defined(HAVE_UNW_GET_SAVE_LOC)
198     unw_save_loc_t saveLoc;
199     unw_get_save_loc(cursor, reg, &saveLoc);
200     if (saveLoc.type == UNW_SLT_MEMORY)
201     {
202         SIZE_T *pLoc = (SIZE_T *)saveLoc.u.addr;
203         // Filter out fake save locations that point to unwContext 
204         if (unwContext == NULL || (pLoc < (SIZE_T *)unwContext) || ((SIZE_T *)(unwContext + 1) <= pLoc))
205             *contextPointer = (SIZE_T *)saveLoc.u.addr;
206     }
207 #else
208     // Returning NULL indicates that we don't have context pointers available
209     *contextPointer = NULL;
210 #endif
211 }
212
213 static void GetContextPointers(unw_cursor_t *cursor, unw_context_t *unwContext, KNONVOLATILE_CONTEXT_POINTERS *contextPointers)
214 {
215 #if defined(_AMD64_)
216     GetContextPointer(cursor, unwContext, UNW_X86_64_RBP, &contextPointers->Rbp);
217     GetContextPointer(cursor, unwContext, UNW_X86_64_RBX, &contextPointers->Rbx);
218     GetContextPointer(cursor, unwContext, UNW_X86_64_R12, &contextPointers->R12);
219     GetContextPointer(cursor, unwContext, UNW_X86_64_R13, &contextPointers->R13);
220     GetContextPointer(cursor, unwContext, UNW_X86_64_R14, &contextPointers->R14);
221     GetContextPointer(cursor, unwContext, UNW_X86_64_R15, &contextPointers->R15);
222 #elif defined(_X86_)
223     GetContextPointer(cursor, unwContext, UNW_X86_EBX, &contextPointers->Ebx);
224     GetContextPointer(cursor, unwContext, UNW_X86_EBP, &contextPointers->Ebp);
225     GetContextPointer(cursor, unwContext, UNW_X86_ESI, &contextPointers->Esi);
226     GetContextPointer(cursor, unwContext, UNW_X86_EDI, &contextPointers->Edi);
227 #elif defined(_ARM_)
228     GetContextPointer(cursor, unwContext, UNW_ARM_R4, &contextPointers->R4);
229     GetContextPointer(cursor, unwContext, UNW_ARM_R5, &contextPointers->R5);
230     GetContextPointer(cursor, unwContext, UNW_ARM_R6, &contextPointers->R6);
231     GetContextPointer(cursor, unwContext, UNW_ARM_R7, &contextPointers->R7);
232     GetContextPointer(cursor, unwContext, UNW_ARM_R8, &contextPointers->R8);
233     GetContextPointer(cursor, unwContext, UNW_ARM_R9, &contextPointers->R9);
234     GetContextPointer(cursor, unwContext, UNW_ARM_R10, &contextPointers->R10);
235     GetContextPointer(cursor, unwContext, UNW_ARM_R11, &contextPointers->R11);
236 #elif defined(_ARM64_)
237     GetContextPointer(cursor, unwContext, UNW_AARCH64_X19, &contextPointers->X19);
238     GetContextPointer(cursor, unwContext, UNW_AARCH64_X20, &contextPointers->X20);
239     GetContextPointer(cursor, unwContext, UNW_AARCH64_X21, &contextPointers->X21);
240     GetContextPointer(cursor, unwContext, UNW_AARCH64_X22, &contextPointers->X22);
241     GetContextPointer(cursor, unwContext, UNW_AARCH64_X23, &contextPointers->X23);
242     GetContextPointer(cursor, unwContext, UNW_AARCH64_X24, &contextPointers->X24);
243     GetContextPointer(cursor, unwContext, UNW_AARCH64_X25, &contextPointers->X25);
244     GetContextPointer(cursor, unwContext, UNW_AARCH64_X26, &contextPointers->X26);
245     GetContextPointer(cursor, unwContext, UNW_AARCH64_X27, &contextPointers->X27);
246     GetContextPointer(cursor, unwContext, UNW_AARCH64_X28, &contextPointers->X28);
247 #else
248 #error unsupported architecture
249 #endif
250 }
251
252 extern int g_common_signal_handler_context_locvar_offset;
253
254 BOOL PAL_VirtualUnwind(CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *contextPointers)
255 {
256     int st;
257     unw_context_t unwContext;
258     unw_cursor_t cursor;
259
260     DWORD64 curPc = CONTEXTGetPC(context);
261
262 #ifndef __APPLE__
263     // Check if the PC is the return address from the SEHProcessException in the common_signal_handler. 
264     // If that's the case, extract its local variable containing the native_context_t of the hardware 
265     // exception and return that. This skips the hardware signal handler trampoline that the libunwind 
266     // cannot cross on some systems.
267     if ((void*)curPc == g_SEHProcessExceptionReturnAddress)
268     {
269         ULONG contextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT | CONTEXT_EXCEPTION_ACTIVE;
270
271     #if defined(_AMD64_)
272         contextFlags |= CONTEXT_XSTATE;
273     #endif
274         size_t nativeContext = *(size_t*)(CONTEXTGetFP(context) + g_common_signal_handler_context_locvar_offset);
275         CONTEXTFromNativeContext((const native_context_t *)nativeContext, context, contextFlags);
276
277         return TRUE;
278     }
279 #endif 
280
281     if ((context->ContextFlags & CONTEXT_EXCEPTION_ACTIVE) != 0)
282     {
283         // The current frame is a source of hardware exception. Due to the fact that
284         // we use the low level unwinder to unwind just one frame a time, the
285         // unwinder doesn't have the signal_frame flag set. So it doesn't
286         // know that it should not decrement the PC before looking up the unwind info.
287         // So we compensate it by incrementing the PC before passing it to the unwinder.
288         // Without it, the unwinder would not find unwind info if the hardware exception
289         // happened in the first instruction of a function.
290         CONTEXTSetPC(context, curPc + 1);
291     }
292
293 #if !UNWIND_CONTEXT_IS_UCONTEXT_T
294     st = unw_getcontext(&unwContext);
295     if (st < 0)
296     {
297         return FALSE;
298     }
299 #endif
300
301     WinContextToUnwindContext(context, &unwContext);
302
303     st = unw_init_local(&cursor, &unwContext);
304     if (st < 0)
305     {
306         return FALSE;
307     }
308
309 #if !UNWIND_CONTEXT_IS_UCONTEXT_T
310     // Set the unwind context to the specified windows context
311     WinContextToUnwindCursor(context, &cursor);
312 #endif
313
314     st = unw_step(&cursor);
315     if (st < 0)
316     {
317         return FALSE;
318     }
319
320     // Check if the frame we have unwound to is a frame that caused
321     // synchronous signal, like a hardware exception and record it
322     // in the context flags.
323     if (unw_is_signal_frame(&cursor) > 0)
324     {
325         context->ContextFlags |= CONTEXT_EXCEPTION_ACTIVE;
326 #if defined(_ARM_) || defined(_ARM64_) || defined(_X86_)
327         context->ContextFlags &= ~CONTEXT_UNWOUND_TO_CALL;
328 #endif // _ARM_ || _ARM64_
329     }
330     else
331     {
332         context->ContextFlags &= ~CONTEXT_EXCEPTION_ACTIVE;
333 #if defined(_ARM_) || defined(_ARM64_) || defined(_X86_)
334         context->ContextFlags |= CONTEXT_UNWOUND_TO_CALL;
335 #endif // _ARM_ || _ARM64_
336     }
337
338     // Update the passed in windows context to reflect the unwind
339     //
340     UnwindContextToWinContext(&cursor, context);
341
342     // FreeBSD, NetBSD, OSX and Alpine appear to do two different things when unwinding
343     // 1: If it reaches where it cannot unwind anymore, say a 
344     // managed frame.  It will return 0, but also update the $pc
345     // 2: If it unwinds all the way to _start it will return
346     // 0 from the step, but $pc will stay the same.
347     // So we detect that here and set the $pc to NULL in that case.
348     // This is the default behavior of the libunwind on Linux.
349     if (st == 0 && CONTEXTGetPC(context) == curPc)
350     {
351         CONTEXTSetPC(context, 0);
352     }
353
354     if (contextPointers != NULL)
355     {
356         GetContextPointers(&cursor, &unwContext, contextPointers);
357     }
358     return TRUE;
359 }
360
361 #else
362 #error don't know how to unwind on this platform
363 #endif
364
365 // These methods are only used on the AMD64 build
366 #ifdef _AMD64_
367 #ifdef HAVE_UNW_GET_ACCESSORS
368
369 static struct LibunwindCallbacksInfoType
370 {
371      CONTEXT *Context;
372      ReadMemoryWordCallback readMemCallback;
373 } LibunwindCallbacksInfo;
374
375 static int get_dyn_info_list_addr(unw_addr_space_t as, unw_word_t *dilap, void *arg)
376 {
377     return -UNW_ENOINFO;
378 }
379
380 static int access_mem(unw_addr_space_t as, unw_word_t addr, unw_word_t *valp, int write, void *arg)
381 {
382     if (write)
383     {
384         ASSERT("Memory write must never be called by libunwind during stackwalk");
385         return -UNW_EINVAL;
386     }
387
388     // access_mem sometimes gets called by _UPT_find_proc_info, in such cases arg has a pointer to libunwind internal data
389     // returned by _UPT_create. It makes it impossible to use arg for passing readMemCallback. That's why we have to use global variable.
390     if (LibunwindCallbacksInfo.readMemCallback((SIZE_T)addr, (SIZE_T *)valp))
391     {
392         return UNW_ESUCCESS;
393     }
394     else 
395     {
396         return -UNW_EUNSPEC;
397     }
398 }
399
400 static int access_reg(unw_addr_space_t as, unw_regnum_t regnum, unw_word_t *valp, int write, void *arg)
401 {
402     if (write)
403     {
404         ASSERT("Register write must never be called by libunwind during stackwalk");
405         return -UNW_EREADONLYREG;
406     }
407
408     CONTEXT *winContext = LibunwindCallbacksInfo.Context;
409
410     switch (regnum) 
411     {
412 #if defined(_AMD64_)
413         case UNW_REG_IP:       *valp = (unw_word_t) winContext->Rip; break;
414         case UNW_REG_SP:       *valp = (unw_word_t) winContext->Rsp; break;
415         case UNW_X86_64_RBP:   *valp = (unw_word_t) winContext->Rbp; break;
416         case UNW_X86_64_RBX:   *valp = (unw_word_t) winContext->Rbx; break;
417         case UNW_X86_64_R12:   *valp = (unw_word_t) winContext->R12; break;
418         case UNW_X86_64_R13:   *valp = (unw_word_t) winContext->R13; break;
419         case UNW_X86_64_R14:   *valp = (unw_word_t) winContext->R14; break;
420         case UNW_X86_64_R15:   *valp = (unw_word_t) winContext->R15; break;
421 #elif defined(_ARM_)
422         case UNW_ARM_R13:      *valp = (unw_word_t) winContext->Sp; break;
423         case UNW_ARM_R14:      *valp = (unw_word_t) winContext->Lr; break;
424         case UNW_ARM_R15:      *valp = (unw_word_t) winContext->Pc; break;
425         case UNW_ARM_R4:       *valp = (unw_word_t) winContext->R4; break;
426         case UNW_ARM_R5:       *valp = (unw_word_t) winContext->R5; break;
427         case UNW_ARM_R6:       *valp = (unw_word_t) winContext->R6; break;
428         case UNW_ARM_R7:       *valp = (unw_word_t) winContext->R7; break;
429         case UNW_ARM_R8:       *valp = (unw_word_t) winContext->R8; break;
430         case UNW_ARM_R9:       *valp = (unw_word_t) winContext->R9; break;
431         case UNW_ARM_R10:      *valp = (unw_word_t) winContext->R10; break;
432         case UNW_ARM_R11:      *valp = (unw_word_t) winContext->R11; break;
433 #elif defined(_ARM64_)
434         case UNW_REG_IP:       *valp = (unw_word_t) winContext->Pc; break;
435         case UNW_REG_SP:       *valp = (unw_word_t) winContext->Sp; break;
436         case UNW_AARCH64_X29:  *valp = (unw_word_t) winContext->Fp; break;
437         case UNW_AARCH64_X30:  *valp = (unw_word_t) winContext->Lr; break;
438         case UNW_AARCH64_X19:  *valp = (unw_word_t) winContext->X19; break;
439         case UNW_AARCH64_X20:  *valp = (unw_word_t) winContext->X20; break;
440         case UNW_AARCH64_X21:  *valp = (unw_word_t) winContext->X21; break;
441         case UNW_AARCH64_X22:  *valp = (unw_word_t) winContext->X22; break;
442         case UNW_AARCH64_X23:  *valp = (unw_word_t) winContext->X23; break;
443         case UNW_AARCH64_X24:  *valp = (unw_word_t) winContext->X24; break;
444         case UNW_AARCH64_X25:  *valp = (unw_word_t) winContext->X25; break;
445         case UNW_AARCH64_X26:  *valp = (unw_word_t) winContext->X26; break;
446         case UNW_AARCH64_X27:  *valp = (unw_word_t) winContext->X27; break;
447         case UNW_AARCH64_X28:  *valp = (unw_word_t) winContext->X28; break;
448 #else
449 #error unsupported architecture
450 #endif
451         default:
452             ASSERT("Attempt to read an unknown register.");
453             return -UNW_EBADREG;
454     }
455     return UNW_ESUCCESS;
456 }
457
458 static int access_fpreg(unw_addr_space_t as, unw_regnum_t regnum, unw_fpreg_t *fpvalp, int write, void *arg)
459 {
460     ASSERT("Not supposed to be ever called");
461     return -UNW_EINVAL;
462 }
463
464 static int resume(unw_addr_space_t as, unw_cursor_t *cp, void *arg)
465 {
466     ASSERT("Not supposed to be ever called");
467     return -UNW_EINVAL;
468 }
469
470 static int get_proc_name(unw_addr_space_t as, unw_word_t addr, char *bufp, size_t buf_len, unw_word_t *offp, void *arg)
471 {
472     ASSERT("Not supposed to be ever called");
473     return -UNW_EINVAL;  
474 }
475
476 int find_proc_info(unw_addr_space_t as, 
477                    unw_word_t ip, unw_proc_info_t *pip,
478                    int need_unwind_info, void *arg)
479 {
480 #ifdef HAVE_LIBUNWIND_PTRACE
481     // UNIXTODO: libunwind RPM package on Fedora/CentOS/RedHat doesn't have libunwind-ptrace.so 
482     // and we can't use it from a shared library like libmscordaccore.so.
483     // That's why all calls to ptrace parts of libunwind ifdeffed out for now.
484     return _UPT_find_proc_info(as, ip, pip, need_unwind_info, arg);
485 #else    
486     return -UNW_EINVAL;
487 #endif    
488 }
489
490 void put_unwind_info(unw_addr_space_t as, unw_proc_info_t *pip, void *arg)
491 {
492 #ifdef HAVE_LIBUNWIND_PTRACE    
493     return _UPT_put_unwind_info(as, pip, arg);
494 #endif    
495 }
496
497 static unw_accessors_t unwind_accessors =
498 {
499     .find_proc_info = find_proc_info,
500     .put_unwind_info = put_unwind_info,
501     .get_dyn_info_list_addr = get_dyn_info_list_addr,
502     .access_mem = access_mem,
503     .access_reg = access_reg,
504     .access_fpreg = access_fpreg,
505     .resume = resume,
506     .get_proc_name = get_proc_name
507 };
508
509 BOOL PAL_VirtualUnwindOutOfProc(CONTEXT *context, 
510                                 KNONVOLATILE_CONTEXT_POINTERS *contextPointers, 
511                                 DWORD pid, 
512                                 ReadMemoryWordCallback readMemCallback)
513 {
514     // This function can be executed only by one thread at a time. 
515     // The reason for this is that we need to pass context and read mem function to libunwind callbacks
516     // but "arg" is already used by the pointer returned from _UPT_create(). 
517     // So we resort to using global variables and a lock.
518     struct Lock 
519     {
520         CRITICAL_SECTION cs;
521         Lock()
522         {        
523             // ctor of a static variable is a thread-safe way to initialize critical section exactly once (clang,gcc)
524             InitializeCriticalSection(&cs);
525         }
526     };
527     struct LockHolder
528     {
529         CRITICAL_SECTION *cs;
530         LockHolder(CRITICAL_SECTION *cs)
531         {
532             this->cs = cs;
533             EnterCriticalSection(cs);
534         }
535
536         ~LockHolder()
537         {
538             LeaveCriticalSection(cs);
539             cs = NULL;
540         }
541     };    
542     static Lock lock;
543     LockHolder lockHolder(&lock.cs);
544
545     int st;
546     unw_cursor_t cursor;
547     unw_addr_space_t addrSpace = 0;
548     void *libunwindUptPtr = NULL;
549     BOOL result = FALSE;
550
551     LibunwindCallbacksInfo.Context = context;
552     LibunwindCallbacksInfo.readMemCallback = readMemCallback;
553
554     addrSpace = unw_create_addr_space(&unwind_accessors, 0);
555 #ifdef HAVE_LIBUNWIND_PTRACE    
556     libunwindUptPtr = _UPT_create(pid);
557 #endif    
558     st = unw_init_remote(&cursor, addrSpace, libunwindUptPtr);
559     if (st < 0)
560     {
561         result = FALSE;
562         goto Exit;
563     }
564
565     st = unw_step(&cursor);
566     if (st < 0)
567     {
568         result = FALSE;
569         goto Exit;
570     }
571
572     UnwindContextToWinContext(&cursor, context);
573
574     if (contextPointers != NULL)
575     {
576         GetContextPointers(&cursor, NULL, contextPointers);
577     }
578     result = TRUE;
579
580 Exit:
581 #ifdef HAVE_LIBUNWIND_PTRACE
582     if (libunwindUptPtr != NULL) 
583     {
584         _UPT_destroy(libunwindUptPtr);
585     }
586 #endif    
587     if (addrSpace != 0) 
588     {
589         unw_destroy_addr_space(addrSpace);
590     }    
591     return result;
592 }
593 #else // HAVE_UNW_GET_ACCESSORS
594
595 BOOL PAL_VirtualUnwindOutOfProc(CONTEXT *context, 
596                                 KNONVOLATILE_CONTEXT_POINTERS *contextPointers, 
597                                 DWORD pid, 
598                                 ReadMemoryWordCallback readMemCallback)
599 {
600     //UNIXTODO: Implement for Mac flavor of libunwind
601     return FALSE;
602 }
603
604 #endif // !HAVE_UNW_GET_ACCESSORS
605 #endif // _AMD64_
606
607 struct ExceptionRecords
608 {
609     CONTEXT ContextRecord;
610     EXCEPTION_RECORD ExceptionRecord;
611 };
612
613 // Max number of fallback contexts that are used when malloc fails to allocate ExceptionRecords structure
614 static const int MaxFallbackContexts = sizeof(size_t) * 8;
615 // Array of fallback contexts
616 static ExceptionRecords s_fallbackContexts[MaxFallbackContexts];
617 // Bitmap used for allocating fallback contexts - bits set to 1 represent already allocated context.
618 static volatile size_t s_allocatedContextsBitmap = 0;
619
620 /*++
621 Function:
622     AllocateExceptionRecords
623
624     Allocate EXCEPTION_RECORD and CONTEXT structures for an exception.
625 Parameters:
626     exceptionRecord - output pointer to the allocated exception record
627     contextRecord - output pointer to the allocated context record
628 --*/
629 VOID
630 AllocateExceptionRecords(EXCEPTION_RECORD** exceptionRecord, CONTEXT** contextRecord)
631 {
632     ExceptionRecords* records;
633     if (posix_memalign((void**)&records, alignof(ExceptionRecords), sizeof(ExceptionRecords)) != 0)
634     {
635         size_t bitmap;
636         size_t newBitmap;
637         int index;
638
639         do
640         {
641             bitmap = s_allocatedContextsBitmap;
642             index = __builtin_ffsl(~bitmap) - 1;
643             if (index < 0)
644             {
645                 PROCAbort();
646             }
647
648             newBitmap = bitmap | ((size_t)1 << index);
649         }
650         while (__sync_val_compare_and_swap(&s_allocatedContextsBitmap, bitmap, newBitmap) != bitmap);
651
652         records = &s_fallbackContexts[index];
653     }
654
655     *contextRecord = &records->ContextRecord;
656     *exceptionRecord = &records->ExceptionRecord;
657 }
658
659 /*++
660 Function:
661     PAL_FreeExceptionRecords
662
663     Free EXCEPTION_RECORD and CONTEXT structures of an exception that were allocated by the
664     AllocateExceptionRecords.
665 Parameters:
666     exceptionRecord - exception record
667     contextRecord - context record
668 --*/
669 VOID
670 PALAPI
671 PAL_FreeExceptionRecords(IN EXCEPTION_RECORD *exceptionRecord, IN CONTEXT *contextRecord)
672 {
673     // Both records are allocated at once and the allocated memory starts at the contextRecord
674     ExceptionRecords* records = (ExceptionRecords*)contextRecord;
675     if ((records >= &s_fallbackContexts[0]) && (records < &s_fallbackContexts[MaxFallbackContexts]))
676     {
677         int index = records - &s_fallbackContexts[0];
678         __sync_fetch_and_and(&s_allocatedContextsBitmap, ~((size_t)1 << index));
679     }
680     else
681     {
682         free(contextRecord);
683     }
684 }
685
686 /*++
687 Function:
688     RtlpRaiseException
689
690 Parameters:
691     ExceptionRecord - the Windows exception record to throw
692
693 Note:
694     The name of this function and the name of the ExceptionRecord 
695     parameter is used in the sos lldb plugin code to read the exception
696     record. See coreclr\src\ToolBox\SOS\lldbplugin\services.cpp.
697
698     This function must not be inlined or optimized so the below PAL_VirtualUnwind
699     calls end up with RaiseException caller's context and so the above debugger 
700     code finds the function and ExceptionRecord parameter.
701 --*/
702 PAL_NORETURN
703 __attribute__((noinline))
704 __attribute__((optnone))
705 static void 
706 RtlpRaiseException(EXCEPTION_RECORD *ExceptionRecord, CONTEXT *ContextRecord)
707 {
708     throw PAL_SEHException(ExceptionRecord, ContextRecord);
709 }
710
711 /*++
712 Function:
713   RaiseException
714
715 See MSDN doc.
716 --*/
717 // no PAL_NORETURN, as callers must assume this can return for continuable exceptions.
718 __attribute__((noinline))
719 VOID
720 PALAPI
721 RaiseException(IN DWORD dwExceptionCode,
722                IN DWORD dwExceptionFlags,
723                IN DWORD nNumberOfArguments,
724                IN CONST ULONG_PTR *lpArguments)
725 {
726     // PERF_ENTRY_ONLY is used here because RaiseException may or may not
727     // return. We can not get latency data without PERF_EXIT. For this reason,
728     // PERF_ENTRY_ONLY is used to profile frequency only.
729     PERF_ENTRY_ONLY(RaiseException);
730     ENTRY("RaiseException(dwCode=%#x, dwFlags=%#x, nArgs=%u, lpArguments=%p)\n",
731           dwExceptionCode, dwExceptionFlags, nNumberOfArguments, lpArguments);
732
733     /* Validate parameters */
734     if (dwExceptionCode & RESERVED_SEH_BIT)
735     {
736         WARN("Exception code %08x has bit 28 set; clearing it.\n", dwExceptionCode);
737         dwExceptionCode ^= RESERVED_SEH_BIT;
738     }
739
740     if (nNumberOfArguments > EXCEPTION_MAXIMUM_PARAMETERS)
741     {
742         WARN("Number of arguments (%d) exceeds the limit "
743             "EXCEPTION_MAXIMUM_PARAMETERS (%d); ignoring extra parameters.\n",
744             nNumberOfArguments, EXCEPTION_MAXIMUM_PARAMETERS);
745         nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
746     }
747
748     CONTEXT *contextRecord;
749     EXCEPTION_RECORD *exceptionRecord;
750     AllocateExceptionRecords(&exceptionRecord, &contextRecord);
751
752     ZeroMemory(exceptionRecord, sizeof(EXCEPTION_RECORD));
753
754     exceptionRecord->ExceptionCode = dwExceptionCode;
755     exceptionRecord->ExceptionFlags = dwExceptionFlags;
756     exceptionRecord->ExceptionRecord = NULL;
757     exceptionRecord->ExceptionAddress = NULL; // will be set by RtlpRaiseException
758     exceptionRecord->NumberParameters = nNumberOfArguments;
759     if (nNumberOfArguments)
760     {
761         CopyMemory(exceptionRecord->ExceptionInformation, lpArguments,
762                    nNumberOfArguments * sizeof(ULONG_PTR));
763     }
764
765     // Capture the context of RaiseException.
766     ZeroMemory(contextRecord, sizeof(CONTEXT));
767     contextRecord->ContextFlags = CONTEXT_FULL;
768     CONTEXT_CaptureContext(contextRecord);
769
770     // We have to unwind one level to get the actual context user code could be resumed at.
771     PAL_VirtualUnwind(contextRecord, NULL);
772
773     exceptionRecord->ExceptionAddress = (void *)CONTEXTGetPC(contextRecord);
774
775     RtlpRaiseException(exceptionRecord, contextRecord);
776
777     LOGEXIT("RaiseException returns\n");
778 }