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