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