unwind-arm.h revision 352377
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_OK = 0,                /* operation completed successfully */
33	_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
34	_URC_END_OF_STACK = 5,
35	_URC_HANDLER_FOUND = 6,
36	_URC_INSTALL_CONTEXT = 7,
37	_URC_CONTINUE_UNWIND = 8,
38	_URC_FAILURE = 9,            /* unspecified failure of some kind */
39	_URC_FATAL_PHASE1_ERROR = _URC_FAILURE
40} _Unwind_Reason_Code;
41
42typedef int _Unwind_Action;
43
44typedef uint32_t _Unwind_State;
45#ifdef __clang__
46static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME  = 0;
47static const _Unwind_State _US_UNWIND_FRAME_STARTING = 1;
48static const _Unwind_State _US_UNWIND_FRAME_RESUME   = 2;
49static const _Unwind_State _US_ACTION_MASK           = 3;
50#else // GCC fails at knowing what a constant expression is
51#	define _US_VIRTUAL_UNWIND_FRAME  0
52#	define _US_UNWIND_FRAME_STARTING 1
53#	define _US_UNWIND_FRAME_RESUME   2
54#	define _US_ACTION_MASK           3
55#endif
56
57typedef struct _Unwind_Context _Unwind_Context;
58
59typedef uint32_t _Unwind_EHT_Header;
60
61struct _Unwind_Exception
62{
63	uint64_t exception_class;
64	void (*exception_cleanup)(_Unwind_Reason_Code, struct _Unwind_Exception *);
65	/* Unwinder cache, private fields for the unwinder's use */
66	struct
67	{
68		uint32_t reserved1;
69		uint32_t reserved2;
70		uint32_t reserved3;
71		uint32_t reserved4;
72		uint32_t reserved5;
73	/* init reserved1 to 0, then don't touch */
74	} unwinder_cache;
75	/* Propagation barrier cache (valid after phase 1): */
76	struct
77	{
78		uint32_t sp;
79		uint32_t bitpattern[5];
80	} barrier_cache;
81	/* Cleanup cache (preserved over cleanup): */
82	struct
83	{
84		uint32_t bitpattern[4];
85	} cleanup_cache;
86	/* Pr cache (for pr's benefit): */
87	struct
88	{
89		/** function start address */
90		uint32_t fnstart;
91		/** pointer to EHT entry header word */
92		_Unwind_EHT_Header *ehtp;
93		/** additional data */
94		uint32_t additional;
95		uint32_t reserved1;
96	} pr_cache;
97	/** Force alignment of next item to 8-byte boundary */
98	long long int :0;
99};
100
101/* Unwinding functions */
102_Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *ucbp);
103void _Unwind_Resume(struct _Unwind_Exception *ucbp);
104void _Unwind_Complete(struct _Unwind_Exception *ucbp);
105void _Unwind_DeleteException(struct _Unwind_Exception *ucbp);
106void *_Unwind_GetLanguageSpecificData(struct _Unwind_Context*);
107
108typedef enum
109{
110	_UVRSR_OK = 0,
111	_UVRSR_NOT_IMPLEMENTED = 1,
112	_UVRSR_FAILED = 2
113} _Unwind_VRS_Result;
114typedef enum
115{
116	_UVRSC_CORE = 0,
117	_UVRSC_VFP = 1,
118	_UVRSC_WMMXD = 3,
119	_UVRSC_WMMXC = 4
120} _Unwind_VRS_RegClass;
121typedef enum
122{
123	_UVRSD_UINT32 = 0,
124	_UVRSD_VFPX = 1,
125	_UVRSD_UINT64 = 3,
126	_UVRSD_FLOAT = 4,
127	_UVRSD_DOUBLE = 5
128} _Unwind_VRS_DataRepresentation;
129
130_Unwind_VRS_Result _Unwind_VRS_Get(_Unwind_Context *context,
131                                   _Unwind_VRS_RegClass regclass,
132                                   uint32_t regno,
133                                   _Unwind_VRS_DataRepresentation representation,
134                                   void *valuep);
135_Unwind_VRS_Result _Unwind_VRS_Set(_Unwind_Context *context,
136                                   _Unwind_VRS_RegClass regclass,
137                                   uint32_t regno,
138                                   _Unwind_VRS_DataRepresentation representation,
139                                   void *valuep);
140
141/* Return the base-address for data references.  */
142extern unsigned long _Unwind_GetDataRelBase(struct _Unwind_Context *);
143
144/* Return the base-address for text references.  */
145extern unsigned long _Unwind_GetTextRelBase(struct _Unwind_Context *);
146extern unsigned long _Unwind_GetRegionStart(struct _Unwind_Context *);
147
148typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn) (struct _Unwind_Context *,
149						 void *);
150extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *);
151extern _Unwind_Reason_Code
152	  _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *);
153
154/**
155 * The next set of functions are compatibility extensions, implementing Itanium
156 * ABI functions on top of ARM ones.
157 */
158
159#define _UA_SEARCH_PHASE	1
160#define _UA_CLEANUP_PHASE	2
161#define _UA_HANDLER_FRAME	4
162#define _UA_FORCE_UNWIND	8
163
164static inline unsigned long _Unwind_GetGR(struct _Unwind_Context *context, int reg)
165{
166	unsigned long val;
167	_Unwind_VRS_Get(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val);
168	return val;
169}
170static inline  void _Unwind_SetGR(struct _Unwind_Context *context, int reg, unsigned long val)
171{
172	_Unwind_VRS_Set(context, _UVRSC_CORE, reg, _UVRSD_UINT32, &val);
173}
174static inline unsigned long _Unwind_GetIP(_Unwind_Context *context)
175{
176	// Low bit store the thumb state - discard it
177	return _Unwind_GetGR(context, 15) & ~1;
178}
179static inline void _Unwind_SetIP(_Unwind_Context *context, unsigned long val)
180{
181	// The lowest bit of the instruction pointer indicates whether we're in
182	// thumb or ARM mode.  This is assumed to be fixed throughout a function,
183	// so must be propagated when setting the program counter.
184	unsigned long thumbState = _Unwind_GetGR(context, 15) & 1;
185   _Unwind_SetGR(context, 15, (val | thumbState));
186}
187
188/** GNU API function that unwinds the frame */
189_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception*, struct _Unwind_Context*);
190
191
192#define DECLARE_PERSONALITY_FUNCTION(name) \
193_Unwind_Reason_Code name(_Unwind_State state,\
194                         struct _Unwind_Exception *exceptionObject,\
195                         struct _Unwind_Context *context);
196
197#define BEGIN_PERSONALITY_FUNCTION(name) \
198_Unwind_Reason_Code name(_Unwind_State state,\
199                         struct _Unwind_Exception *exceptionObject,\
200                         struct _Unwind_Context *context)\
201{\
202	int version = 1;\
203	uint64_t exceptionClass = exceptionObject->exception_class;\
204	int actions;\
205	switch (state)\
206	{\
207		default: return _URC_FAILURE;\
208		case _US_VIRTUAL_UNWIND_FRAME:\
209		{\
210			actions = _UA_SEARCH_PHASE;\
211			break;\
212		}\
213		case _US_UNWIND_FRAME_STARTING:\
214		{\
215			actions = _UA_CLEANUP_PHASE;\
216			if (exceptionObject->barrier_cache.sp == _Unwind_GetGR(context, 13))\
217			{\
218				actions |= _UA_HANDLER_FRAME;\
219			}\
220			break;\
221		}\
222		case _US_UNWIND_FRAME_RESUME:\
223		{\
224			return continueUnwinding(exceptionObject, context);\
225			break;\
226		}\
227	}\
228	_Unwind_SetGR (context, 12, reinterpret_cast<unsigned long>(exceptionObject));\
229
230#define CALL_PERSONALITY_FUNCTION(name) name(state,exceptionObject,context)
231