unwind-arm.h revision 369446
150479Speter/* 21553Srgrimes * Copyright 2012 David Chisnall. All rights reserved. 380029Sobrien * 480029Sobrien * Permission is hereby granted, free of charge, to any person obtaining a copy 580029Sobrien * of this software and associated documentation files (the "Software"), to 61553Srgrimes * deal in the Software without restriction, including without limitation the 774816Sru * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 880029Sobrien * sell copies of the Software, and to permit persons to whom the Software is 91553Srgrimes * furnished to do so, subject to the following conditions: 1080029Sobrien * 1180029Sobrien * The above copyright notice and this permission notice shall be 1280029Sobrien * included in all copies or substantial portions of the Software. 1380029Sobrien * 1480029Sobrien * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1558904Sshin * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1680029Sobrien * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1758904Sshin * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 1817637Speter * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 1917637Speter * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2017637Speter * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2180029Sobrien */ 2217637Speter 2358904Sshin/* For uint32_t and uint64_t */ 2458804Sshin#include <stdint.h> 2558804Sshin 2658904Sshin/** 2718584Sfenner * ARM-specific unwind definitions. These are taken from the ARM EHABI 2842624Ssimokawa * specification. 2942624Ssimokawa */ 3080029Sobrien typedef enum 3142624Ssimokawa{ 3242624Ssimokawa _URC_NO_REASON = 0, 3380029Sobrien _URC_OK = 0, /* operation completed successfully */ 3418584Sfenner _URC_FOREIGN_EXCEPTION_CAUGHT = 1, 3518584Sfenner _URC_END_OF_STACK = 5, 3636799Simp _URC_HANDLER_FOUND = 6, 3780029Sobrien _URC_INSTALL_CONTEXT = 7, 3880029Sobrien _URC_CONTINUE_UNWIND = 8, 3918584Sfenner _URC_FAILURE = 9, /* unspecified failure of some kind */ 401553Srgrimes _URC_FATAL_PHASE1_ERROR = _URC_FAILURE 41} _Unwind_Reason_Code; 42 43typedef int _Unwind_Action; 44 45typedef uint32_t _Unwind_State; 46#ifdef __clang__ 47static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME = 0; 48static const _Unwind_State _US_UNWIND_FRAME_STARTING = 1; 49static const _Unwind_State _US_UNWIND_FRAME_RESUME = 2; 50static const _Unwind_State _US_ACTION_MASK = 3; 51#else // GCC fails at knowing what a constant expression is 52# define _US_VIRTUAL_UNWIND_FRAME 0 53# define _US_UNWIND_FRAME_STARTING 1 54# define _US_UNWIND_FRAME_RESUME 2 55# define _US_ACTION_MASK 3 56#endif 57 58typedef struct _Unwind_Context _Unwind_Context; 59 60typedef uint32_t _Unwind_EHT_Header; 61 62struct _Unwind_Exception 63{ 64 uint64_t exception_class; 65 void (*exception_cleanup)(_Unwind_Reason_Code, struct _Unwind_Exception *); 66 /* Unwinder cache, private fields for the unwinder's use */ 67 struct 68 { 69 uint32_t reserved1; 70 uint32_t reserved2; 71 uint32_t reserved3; 72 uint32_t reserved4; 73 uint32_t reserved5; 74 /* init reserved1 to 0, then don't touch */ 75 } unwinder_cache; 76 /* Propagation barrier cache (valid after phase 1): */ 77 struct 78 { 79 uint32_t sp; 80 uint32_t bitpattern[5]; 81 } barrier_cache; 82 /* Cleanup cache (preserved over cleanup): */ 83 struct 84 { 85 uint32_t bitpattern[4]; 86 } cleanup_cache; 87 /* Pr cache (for pr's benefit): */ 88 struct 89 { 90 /** function start address */ 91 uint32_t fnstart; 92 /** pointer to EHT entry header word */ 93 _Unwind_EHT_Header *ehtp; 94 /** additional data */ 95 uint32_t additional; 96 uint32_t reserved1; 97 } pr_cache; 98 /** Force alignment of next item to 8-byte boundary */ 99 long long int :0; 100}; 101 102/* Unwinding functions */ 103_Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *ucbp); 104void _Unwind_Resume(struct _Unwind_Exception *ucbp); 105void _Unwind_Complete(struct _Unwind_Exception *ucbp); 106void _Unwind_DeleteException(struct _Unwind_Exception *ucbp); 107void *_Unwind_GetLanguageSpecificData(struct _Unwind_Context*); 108 109typedef enum 110{ 111 _UVRSR_OK = 0, 112 _UVRSR_NOT_IMPLEMENTED = 1, 113 _UVRSR_FAILED = 2 114} _Unwind_VRS_Result; 115typedef enum 116{ 117 _UVRSC_CORE = 0, 118 _UVRSC_VFP = 1, 119 _UVRSC_WMMXD = 3, 120 _UVRSC_WMMXC = 4 121} _Unwind_VRS_RegClass; 122typedef enum 123{ 124 _UVRSD_UINT32 = 0, 125 _UVRSD_VFPX = 1, 126 _UVRSD_UINT64 = 3, 127 _UVRSD_FLOAT = 4, 128 _UVRSD_DOUBLE = 5 129} _Unwind_VRS_DataRepresentation; 130 131_Unwind_VRS_Result _Unwind_VRS_Get(_Unwind_Context *context, 132 _Unwind_VRS_RegClass regclass, 133 uint32_t regno, 134 _Unwind_VRS_DataRepresentation representation, 135 void *valuep); 136_Unwind_VRS_Result _Unwind_VRS_Set(_Unwind_Context *context, 137 _Unwind_VRS_RegClass regclass, 138 uint32_t regno, 139 _Unwind_VRS_DataRepresentation representation, 140 void *valuep); 141 142/* Return the base-address for data references. */ 143extern unsigned long _Unwind_GetDataRelBase(struct _Unwind_Context *); 144 145/* Return the base-address for text references. */ 146extern unsigned long _Unwind_GetTextRelBase(struct _Unwind_Context *); 147extern unsigned long _Unwind_GetRegionStart(struct _Unwind_Context *); 148 149typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn) (struct _Unwind_Context *, 150 void *); 151extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *); 152extern _Unwind_Reason_Code 153 _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *); 154 155/** 156 * The next set of functions are compatibility extensions, implementing Itanium 157 * ABI functions on top of ARM ones. 158 */ 159 160#define _UA_SEARCH_PHASE 1 161#define _UA_CLEANUP_PHASE 2 162#define _UA_HANDLER_FRAME 4 163#define _UA_FORCE_UNWIND 8 164 165static inline unsigned long _Unwind_GetGR(struct _Unwind_Context *context, int reg) 166{ 167 unsigned long val; 168 _Unwind_VRS_Get(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val); 169 return val; 170} 171static inline void _Unwind_SetGR(struct _Unwind_Context *context, int reg, unsigned long val) 172{ 173 _Unwind_VRS_Set(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val); 174} 175static inline unsigned long _Unwind_GetIP(_Unwind_Context *context) 176{ 177 // Low bit store the thumb state - discard it 178 return _Unwind_GetGR(context, 15) & ~1; 179} 180static inline void _Unwind_SetIP(_Unwind_Context *context, unsigned long val) 181{ 182 // The lowest bit of the instruction pointer indicates whether we're in 183 // thumb or ARM mode. This is assumed to be fixed throughout a function, 184 // so must be propagated when setting the program counter. 185 unsigned long thumbState = _Unwind_GetGR(context, 15) & 1; 186 _Unwind_SetGR(context, 15, (val | thumbState)); 187} 188 189/** GNU API function that unwinds the frame */ 190_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception*, struct _Unwind_Context*); 191 192 193#define DECLARE_PERSONALITY_FUNCTION(name) \ 194_Unwind_Reason_Code name(_Unwind_State state,\ 195 struct _Unwind_Exception *exceptionObject,\ 196 struct _Unwind_Context *context); 197 198#define BEGIN_PERSONALITY_FUNCTION(name) \ 199_Unwind_Reason_Code name(_Unwind_State state,\ 200 struct _Unwind_Exception *exceptionObject,\ 201 struct _Unwind_Context *context)\ 202{\ 203 int version = 1;\ 204 uint64_t exceptionClass = exceptionObject->exception_class;\ 205 int actions;\ 206 switch (state)\ 207 {\ 208 default: return _URC_FAILURE;\ 209 case _US_VIRTUAL_UNWIND_FRAME:\ 210 {\ 211 actions = _UA_SEARCH_PHASE;\ 212 break;\ 213 }\ 214 case _US_UNWIND_FRAME_STARTING:\ 215 {\ 216 actions = _UA_CLEANUP_PHASE;\ 217 if (exceptionObject->barrier_cache.sp == _Unwind_GetGR(context, 13))\ 218 {\ 219 actions |= _UA_HANDLER_FRAME;\ 220 }\ 221 break;\ 222 }\ 223 case _US_UNWIND_FRAME_RESUME:\ 224 {\ 225 return continueUnwinding(exceptionObject, context);\ 226 break;\ 227 }\ 228 }\ 229 _Unwind_SetGR (context, 12, reinterpret_cast<unsigned long>(exceptionObject));\ 230 231#define CALL_PERSONALITY_FUNCTION(name) name(state,exceptionObject,context) 232