1 //===------------------------------- unwind.h -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //
8 // C++ ABI Level 1 ABI documented at:
9 //   https://github.com/ARM-software/abi-aa/blob/main/ehabi32/ehabi32.rst
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef __ARM_EHABI_UNWIND_H__
14 #define __ARM_EHABI_UNWIND_H__
15 
16 typedef uint32_t _Unwind_State;
17 
18 static const _Unwind_State _US_VIRTUAL_UNWIND_FRAME   = 0;
19 static const _Unwind_State _US_UNWIND_FRAME_STARTING  = 1;
20 static const _Unwind_State _US_UNWIND_FRAME_RESUME    = 2;
21 static const _Unwind_State _US_ACTION_MASK            = 3;
22 /* Undocumented flag for force unwinding. */
23 static const _Unwind_State _US_FORCE_UNWIND           = 8;
24 
25 typedef uint32_t _Unwind_EHT_Header;
26 
27 struct _Unwind_Control_Block;
28 typedef struct _Unwind_Control_Block _Unwind_Control_Block;
29 #define _Unwind_Exception _Unwind_Control_Block /* Alias */
30 typedef uint8_t _Unwind_Exception_Class[8];
31 
32 struct _Unwind_Control_Block {
33   _Unwind_Exception_Class exception_class;
34   void (*exception_cleanup)(_Unwind_Reason_Code, _Unwind_Control_Block*);
35 
36   /* Unwinder cache, private fields for the unwinder's use */
37   struct {
38     uint32_t reserved1; /* init reserved1 to 0, then don't touch */
39     uint32_t reserved2;
40     uint32_t reserved3;
41     uint32_t reserved4;
42     uint32_t reserved5;
43   } unwinder_cache;
44 
45   /* Propagation barrier cache (valid after phase 1): */
46   struct {
47     uint32_t sp;
48     uint32_t bitpattern[5];
49   } barrier_cache;
50 
51   /* Cleanup cache (preserved over cleanup): */
52   struct {
53     uint32_t bitpattern[4];
54   } cleanup_cache;
55 
56   /* Pr cache (for pr's benefit): */
57   struct {
58     uint32_t fnstart; /* function start address */
59     _Unwind_EHT_Header* ehtp; /* pointer to EHT entry header word */
60     uint32_t additional;
61     uint32_t reserved1;
62   } pr_cache;
63 
64   long long int :0; /* Enforce the 8-byte alignment */
65 } __attribute__((__aligned__(8)));
66 
67 typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)(
68     _Unwind_State state, _Unwind_Exception *exceptionObject,
69     struct _Unwind_Context *context);
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
75 //
76 // The following are the base functions documented by the C++ ABI
77 //
78 #ifdef __USING_SJLJ_EXCEPTIONS__
79 extern _Unwind_Reason_Code
80     _Unwind_SjLj_RaiseException(_Unwind_Exception *exception_object);
81 extern void _Unwind_SjLj_Resume(_Unwind_Exception *exception_object);
82 #else
83 extern _Unwind_Reason_Code
84     _Unwind_RaiseException(_Unwind_Exception *exception_object);
85 extern void _Unwind_Resume(_Unwind_Exception *exception_object);
86 #endif
87 extern void _Unwind_DeleteException(_Unwind_Exception *exception_object);
88 
89 typedef enum {
90   _UVRSC_CORE = 0, /* integer register */
91   _UVRSC_VFP = 1, /* vfp */
92   _UVRSC_WMMXD = 3, /* Intel WMMX data register */
93   _UVRSC_WMMXC = 4 /* Intel WMMX control register */
94 } _Unwind_VRS_RegClass;
95 
96 typedef enum {
97   _UVRSD_UINT32 = 0,
98   _UVRSD_VFPX = 1,
99   _UVRSD_UINT64 = 3,
100   _UVRSD_FLOAT = 4,
101   _UVRSD_DOUBLE = 5
102 } _Unwind_VRS_DataRepresentation;
103 
104 typedef enum {
105   _UVRSR_OK = 0,
106   _UVRSR_NOT_IMPLEMENTED = 1,
107   _UVRSR_FAILED = 2
108 } _Unwind_VRS_Result;
109 
110 extern void _Unwind_Complete(_Unwind_Exception* exception_object);
111 
112 extern _Unwind_VRS_Result
113 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
114                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
115                 void *valuep);
116 
117 extern _Unwind_VRS_Result
118 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
119                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
120                 void *valuep);
121 
122 extern _Unwind_VRS_Result
123 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
124                 uint32_t discriminator,
125                 _Unwind_VRS_DataRepresentation representation);
126 
127 #if defined(_LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE)
128 #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 extern
129 #else
130 #define _LIBUNWIND_EXPORT_UNWIND_LEVEL1 static __inline__
131 #endif
132 
133 // These are de facto helper functions for ARM, which delegate the function
134 // calls to _Unwind_VRS_Get/Set().  These are not a part of ARM EHABI
135 // specification, thus these function MUST be inlined.  Please don't replace
136 // these with the "extern" function declaration; otherwise, the program
137 // including this <unwind.h> header won't be ABI compatible and will result in
138 // link error when we are linking the program with libgcc.
139 
140 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetGR(struct _Unwind_Context * context,int index)141 uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index) {
142   uintptr_t value = 0;
143   _Unwind_VRS_Get(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
144   return value;
145 }
146 
147 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetGR(struct _Unwind_Context * context,int index,uintptr_t value)148 void _Unwind_SetGR(struct _Unwind_Context *context, int index,
149                    uintptr_t value) {
150   _Unwind_VRS_Set(context, _UVRSC_CORE, (uint32_t)index, _UVRSD_UINT32, &value);
151 }
152 
153 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_GetIP(struct _Unwind_Context * context)154 uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
155   // remove the thumb-bit before returning
156   return _Unwind_GetGR(context, 15) & (~(uintptr_t)0x1);
157 }
158 
159 _LIBUNWIND_EXPORT_UNWIND_LEVEL1
_Unwind_SetIP(struct _Unwind_Context * context,uintptr_t value)160 void _Unwind_SetIP(struct _Unwind_Context *context, uintptr_t value) {
161   uintptr_t thumb_bit = _Unwind_GetGR(context, 15) & ((uintptr_t)0x1);
162   _Unwind_SetGR(context, 15, value | thumb_bit);
163 }
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif // __ARM_EHABI_UNWIND_H__
170