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