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