xref: /freebsd-src/contrib/llvm-project/libunwind/include/unwind_arm_ehabi.h (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //
8349cc55cSDimitry Andric // C++ ABI Level 1 ABI documented at:
9349cc55cSDimitry Andric //   https://github.com/ARM-software/abi-aa/blob/main/ehabi32/ehabi32.rst
10349cc55cSDimitry Andric //
11349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
12349cc55cSDimitry Andric 
13349cc55cSDimitry Andric #ifndef __ARM_EHABI_UNWIND_H__
14349cc55cSDimitry Andric #define __ARM_EHABI_UNWIND_H__
15349cc55cSDimitry Andric 
16349cc55cSDimitry Andric typedef uint32_t _Unwind_State;
17349cc55cSDimitry Andric 
18349cc55cSDimitry Andric static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME   = 0;
19349cc55cSDimitry Andric static const _Unwind_State _US_UNWIND_FRAME_STARTING  = 1;
20349cc55cSDimitry Andric static const _Unwind_State _US_UNWIND_FRAME_RESUME    = 2;
21349cc55cSDimitry Andric static const _Unwind_State _US_ACTION_MASK            = 3;
22349cc55cSDimitry Andric /* Undocumented flag for force unwinding. */
23349cc55cSDimitry Andric static const _Unwind_State _US_FORCE_UNWIND           = 8;
24349cc55cSDimitry Andric 
25349cc55cSDimitry Andric typedef uint32_t _Unwind_EHT_Header;
26349cc55cSDimitry Andric /*
27349cc55cSDimitry Andric  * gcc_personality_v0 references 'struct _Unwind_Exception' all over the place.
28349cc55cSDimitry Andric  * Nothing in libunwind cares about 'struct _Unwind_Control_Block,' so make it
29349cc55cSDimitry Andric  * the alias of struct _Unwind_Exception, instead of the other way around.
30349cc55cSDimitry Andric  */
31349cc55cSDimitry Andric struct _Unwind_Exception;
32349cc55cSDimitry Andric typedef struct _Unwind_Exception _Unwind_Exception;
33349cc55cSDimitry Andric typedef struct _Unwind_Exception _Unwind_Control_Block; /* Alias */
34349cc55cSDimitry Andric typedef uint8_t _Unwind_Exception_Class[8];
35349cc55cSDimitry Andric 
36349cc55cSDimitry Andric struct _Unwind_Exception {
37349cc55cSDimitry Andric   _Unwind_Exception_Class exception_class;
38349cc55cSDimitry Andric   void (*exception_cleanup)(_Unwind_Reason_Code, _Unwind_Control_Block*);
39349cc55cSDimitry Andric 
40349cc55cSDimitry Andric   /* Unwinder cache, private fields for the unwinder's use */
41349cc55cSDimitry Andric   struct {
42349cc55cSDimitry Andric     uint32_t reserved1; /* init reserved1 to 0, then don't touch */
43349cc55cSDimitry Andric     uint32_t reserved2;
44349cc55cSDimitry Andric     uint32_t reserved3;
45349cc55cSDimitry Andric     uint32_t reserved4;
46349cc55cSDimitry Andric     uint32_t reserved5;
47349cc55cSDimitry Andric   } unwinder_cache;
48349cc55cSDimitry Andric 
49349cc55cSDimitry Andric   /* Propagation barrier cache (valid after phase 1): */
50349cc55cSDimitry Andric   struct {
51349cc55cSDimitry Andric     uint32_t sp;
52349cc55cSDimitry Andric     uint32_t bitpattern[5];
53349cc55cSDimitry Andric   } barrier_cache;
54349cc55cSDimitry Andric 
55349cc55cSDimitry Andric   /* Cleanup cache (preserved over cleanup): */
56349cc55cSDimitry Andric   struct {
57349cc55cSDimitry Andric     uint32_t bitpattern[4];
58349cc55cSDimitry Andric   } cleanup_cache;
59349cc55cSDimitry Andric 
60349cc55cSDimitry Andric   /* Pr cache (for pr's benefit): */
61349cc55cSDimitry Andric   struct {
62349cc55cSDimitry Andric     uint32_t fnstart; /* function start address */
63349cc55cSDimitry Andric     _Unwind_EHT_Header* ehtp; /* pointer to EHT entry header word */
64349cc55cSDimitry Andric     uint32_t additional;
65349cc55cSDimitry Andric     uint32_t reserved1;
66349cc55cSDimitry Andric   } pr_cache;
67349cc55cSDimitry Andric 
68349cc55cSDimitry Andric   long long int :0; /* Enforce the 8-byte alignment */
69349cc55cSDimitry Andric } __attribute__((__aligned__(8)));
70349cc55cSDimitry Andric 
71349cc55cSDimitry Andric typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)(
72349cc55cSDimitry Andric     _Unwind_State state, _Unwind_Exception *exceptionObject,
73349cc55cSDimitry Andric     struct _Unwind_Context *context);
74349cc55cSDimitry Andric 
75349cc55cSDimitry Andric #ifdef __cplusplus
76349cc55cSDimitry Andric extern "C" {
77349cc55cSDimitry Andric #endif
78349cc55cSDimitry Andric 
79349cc55cSDimitry Andric //
80349cc55cSDimitry Andric // The following are the base functions documented by the C++ ABI
81349cc55cSDimitry Andric //
82349cc55cSDimitry Andric #ifdef __USING_SJLJ_EXCEPTIONS__
83349cc55cSDimitry Andric extern _Unwind_Reason_Code
84349cc55cSDimitry Andric     _Unwind_SjLj_RaiseException(_Unwind_Exception *exception_object);
85349cc55cSDimitry Andric extern void _Unwind_SjLj_Resume(_Unwind_Exception *exception_object);
86349cc55cSDimitry Andric #else
87349cc55cSDimitry Andric extern _Unwind_Reason_Code
88349cc55cSDimitry Andric     _Unwind_RaiseException(_Unwind_Exception *exception_object);
89349cc55cSDimitry Andric extern void _Unwind_Resume(_Unwind_Exception *exception_object);
90349cc55cSDimitry Andric #endif
91349cc55cSDimitry Andric extern void _Unwind_DeleteException(_Unwind_Exception *exception_object);
92349cc55cSDimitry Andric 
93349cc55cSDimitry Andric typedef enum {
94349cc55cSDimitry Andric   _UVRSC_CORE = 0,  /* integer register */
95349cc55cSDimitry Andric   _UVRSC_VFP = 1,   /* vfp */
96349cc55cSDimitry Andric   _UVRSC_WMMXD = 3, /* Intel WMMX data register */
97*0eae32dcSDimitry Andric   _UVRSC_WMMXC = 4, /* Intel WMMX control register */
98*0eae32dcSDimitry Andric   _UVRSC_PSEUDO = 5 /* Special purpose pseudo register */
99349cc55cSDimitry Andric } _Unwind_VRS_RegClass;
100349cc55cSDimitry Andric 
101349cc55cSDimitry Andric typedef enum {
102349cc55cSDimitry Andric   _UVRSD_UINT32 = 0,
103349cc55cSDimitry Andric   _UVRSD_VFPX = 1,
104349cc55cSDimitry Andric   _UVRSD_UINT64 = 3,
105349cc55cSDimitry Andric   _UVRSD_FLOAT = 4,
106349cc55cSDimitry Andric   _UVRSD_DOUBLE = 5
107349cc55cSDimitry Andric } _Unwind_VRS_DataRepresentation;
108349cc55cSDimitry Andric 
109349cc55cSDimitry Andric typedef enum {
110349cc55cSDimitry Andric   _UVRSR_OK = 0,
111349cc55cSDimitry Andric   _UVRSR_NOT_IMPLEMENTED = 1,
112349cc55cSDimitry Andric   _UVRSR_FAILED = 2
113349cc55cSDimitry Andric } _Unwind_VRS_Result;
114349cc55cSDimitry Andric 
115349cc55cSDimitry Andric extern void _Unwind_Complete(_Unwind_Exception* exception_object);
116349cc55cSDimitry Andric 
117349cc55cSDimitry Andric extern _Unwind_VRS_Result
118349cc55cSDimitry Andric _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
119349cc55cSDimitry Andric                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
120349cc55cSDimitry Andric                 void *valuep);
121349cc55cSDimitry Andric 
122349cc55cSDimitry Andric extern _Unwind_VRS_Result
123349cc55cSDimitry Andric _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
124349cc55cSDimitry Andric                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
125349cc55cSDimitry Andric                 void *valuep);
126349cc55cSDimitry Andric 
127349cc55cSDimitry Andric extern _Unwind_VRS_Result
128349cc55cSDimitry Andric _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
129349cc55cSDimitry Andric                 uint32_t discriminator,
130349cc55cSDimitry Andric                 _Unwind_VRS_DataRepresentation representation);
131349cc55cSDimitry Andric 
132349cc55cSDimitry Andric #if defined(_LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE)
133349cc55cSDimitry Andric #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 extern
134349cc55cSDimitry Andric #else
135349cc55cSDimitry Andric #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 static __inline__
136349cc55cSDimitry Andric #endif
137349cc55cSDimitry Andric 
138349cc55cSDimitry Andric // These are de facto helper functions for ARM, which delegate the function
139349cc55cSDimitry Andric // calls to _Unwind_VRS_Get/Set().  These are not a part of ARM EHABI
140349cc55cSDimitry Andric // specification, thus these function MUST be inlined.  Please don't replace
141349cc55cSDimitry Andric // these with the "extern" function declaration; otherwise, the program
142349cc55cSDimitry Andric // including this <unwind.h> header won't be ABI compatible and will result in
143349cc55cSDimitry Andric // link error when we are linking the program with libgcc.
144349cc55cSDimitry Andric 
145349cc55cSDimitry Andric _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetGR(struct _Unwind_Context * context,int index)146349cc55cSDimitry Andric uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index) {
147349cc55cSDimitry Andric   uintptr_t value = 0;
148349cc55cSDimitry Andric   _Unwind_VRS_Get(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
149349cc55cSDimitry Andric   return value;
150349cc55cSDimitry Andric }
151349cc55cSDimitry Andric 
152349cc55cSDimitry Andric _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetGR(struct _Unwind_Context * context,int index,uintptr_t value)153349cc55cSDimitry Andric void _Unwind_SetGR(struct _Unwind_Context *context, int index,
154349cc55cSDimitry Andric                    uintptr_t value) {
155349cc55cSDimitry Andric   _Unwind_VRS_Set(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
156349cc55cSDimitry Andric }
157349cc55cSDimitry Andric 
158349cc55cSDimitry Andric _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetIP(struct _Unwind_Context * context)159349cc55cSDimitry Andric uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
160349cc55cSDimitry Andric   // remove the thumb-bit before returning
161349cc55cSDimitry Andric   return _Unwind_GetGR(context, 15) & (~(uintptr_t)0x1);
162349cc55cSDimitry Andric }
163349cc55cSDimitry Andric 
164349cc55cSDimitry Andric _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetIP(struct _Unwind_Context * context,uintptr_t value)165349cc55cSDimitry Andric void _Unwind_SetIP(struct _Unwind_Context *context, uintptr_t value) {
166349cc55cSDimitry Andric   uintptr_t thumb_bit = _Unwind_GetGR(context, 15) & ((uintptr_t)0x1);
167349cc55cSDimitry Andric   _Unwind_SetGR(context, 15, value | thumb_bit);
168349cc55cSDimitry Andric }
169349cc55cSDimitry Andric 
170349cc55cSDimitry Andric #ifdef __cplusplus
171349cc55cSDimitry Andric }
172349cc55cSDimitry Andric #endif
173349cc55cSDimitry Andric 
174349cc55cSDimitry Andric #endif // __ARM_EHABI_UNWIND_H__
175