1 /* ARM EABI compliant unwinding routines.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This file is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
32 #define VRS_PC(vrs) ((vrs)->core.r[R_PC])
33 #define VRS_SP(vrs) ((vrs)->core.r[R_SP])
34 #define VRS_RETURN(vrs) ((vrs)->core.r[R_LR])
41 /* We use normal integer types here to avoid the compiler generating
42 coprocessor instructions. */
51 /* Always populated via VSTM, so no need for the "pad" field from
52 vfp_regs (which is used to store the format word for FSTMX). */
66 /* The ABI specifies that the unwind routines may only use core registers,
67 except when actually manipulating coprocessor state. This allows
68 us to write one implementation that works on all platforms by
69 demand-saving coprocessor registers.
71 During unwinding we hold the coprocessor state in the actual hardware
72 registers and allocate demand-save areas for use during phase1
77 /* The first fields must be the same as a phase2_vrs. */
78 _uw demand_save_flags
;
79 struct core_regs core
;
80 _uw prev_sp
; /* Only valid during forced unwinding. */
82 struct vfpv3_regs vfp_regs_16_to_31
;
83 struct wmmxd_regs wmmxd
;
84 struct wmmxc_regs wmmxc
;
87 #define DEMAND_SAVE_VFP 1 /* VFP state has been saved if not set */
88 #define DEMAND_SAVE_VFP_D 2 /* VFP state is for FLDMD/FSTMD if set */
89 #define DEMAND_SAVE_VFP_V3 4 /* VFPv3 state for regs 16 .. 31 has
90 been saved if not set */
91 #define DEMAND_SAVE_WMMXD 8 /* iWMMXt data registers have been
93 #define DEMAND_SAVE_WMMXC 16 /* iWMMXt control registers have been
96 /* This must match the structure created by the assembly wrappers. */
99 _uw demand_save_flags
;
100 struct core_regs core
;
103 /* Coprocessor register state manipulation functions. */
105 /* Routines for FLDMX/FSTMX format... */
106 void __gnu_Unwind_Save_VFP (struct vfp_regs
* p
);
107 void __gnu_Unwind_Restore_VFP (struct vfp_regs
* p
);
108 void __gnu_Unwind_Save_WMMXD (struct wmmxd_regs
* p
);
109 void __gnu_Unwind_Restore_WMMXD (struct wmmxd_regs
* p
);
110 void __gnu_Unwind_Save_WMMXC (struct wmmxc_regs
* p
);
111 void __gnu_Unwind_Restore_WMMXC (struct wmmxc_regs
* p
);
113 /* ...and those for FLDMD/FSTMD format... */
114 void __gnu_Unwind_Save_VFP_D (struct vfp_regs
* p
);
115 void __gnu_Unwind_Restore_VFP_D (struct vfp_regs
* p
);
117 /* ...and those for VLDM/VSTM format, saving/restoring only registers
119 void __gnu_Unwind_Save_VFP_D_16_to_31 (struct vfpv3_regs
* p
);
120 void __gnu_Unwind_Restore_VFP_D_16_to_31 (struct vfpv3_regs
* p
);
122 /* Restore coprocessor state after phase1 unwinding. */
124 restore_non_core_regs (phase1_vrs
* vrs
)
126 if ((vrs
->demand_save_flags
& DEMAND_SAVE_VFP
) == 0)
128 if (vrs
->demand_save_flags
& DEMAND_SAVE_VFP_D
)
129 __gnu_Unwind_Restore_VFP_D (&vrs
->vfp
);
131 __gnu_Unwind_Restore_VFP (&vrs
->vfp
);
134 if ((vrs
->demand_save_flags
& DEMAND_SAVE_VFP_V3
) == 0)
135 __gnu_Unwind_Restore_VFP_D_16_to_31 (&vrs
->vfp_regs_16_to_31
);
137 if ((vrs
->demand_save_flags
& DEMAND_SAVE_WMMXD
) == 0)
138 __gnu_Unwind_Restore_WMMXD (&vrs
->wmmxd
);
139 if ((vrs
->demand_save_flags
& DEMAND_SAVE_WMMXC
) == 0)
140 __gnu_Unwind_Restore_WMMXC (&vrs
->wmmxc
);
143 #include "unwind-arm-common.inc"
145 /* ABI defined personality routines. */
146 extern _Unwind_Reason_Code
__aeabi_unwind_cpp_pr0 (_Unwind_State
,
147 _Unwind_Control_Block
*, _Unwind_Context
*);// __attribute__((weak));
148 extern _Unwind_Reason_Code
__aeabi_unwind_cpp_pr1 (_Unwind_State
,
149 _Unwind_Control_Block
*, _Unwind_Context
*) __attribute__((weak
));
150 extern _Unwind_Reason_Code
__aeabi_unwind_cpp_pr2 (_Unwind_State
,
151 _Unwind_Control_Block
*, _Unwind_Context
*) __attribute__((weak
));
153 /* ABI defined routine to store a virtual register to memory. */
155 _Unwind_VRS_Result
_Unwind_VRS_Get (_Unwind_Context
*context
,
156 _Unwind_VRS_RegClass regclass
,
158 _Unwind_VRS_DataRepresentation representation
,
161 phase1_vrs
*vrs
= (phase1_vrs
*) context
;
166 if (representation
!= _UVRSD_UINT32
168 return _UVRSR_FAILED
;
169 *(_uw
*) valuep
= vrs
->core
.r
[regno
];
175 return _UVRSR_NOT_IMPLEMENTED
;
178 return _UVRSR_FAILED
;
183 /* ABI defined function to load a virtual register from memory. */
185 _Unwind_VRS_Result
_Unwind_VRS_Set (_Unwind_Context
*context
,
186 _Unwind_VRS_RegClass regclass
,
188 _Unwind_VRS_DataRepresentation representation
,
191 phase1_vrs
*vrs
= (phase1_vrs
*) context
;
196 if (representation
!= _UVRSD_UINT32
198 return _UVRSR_FAILED
;
200 vrs
->core
.r
[regno
] = *(_uw
*) valuep
;
206 return _UVRSR_NOT_IMPLEMENTED
;
209 return _UVRSR_FAILED
;
214 /* ABI defined function to pop registers off the stack. */
216 _Unwind_VRS_Result
_Unwind_VRS_Pop (_Unwind_Context
*context
,
217 _Unwind_VRS_RegClass regclass
,
219 _Unwind_VRS_DataRepresentation representation
)
221 phase1_vrs
*vrs
= (phase1_vrs
*) context
;
231 if (representation
!= _UVRSD_UINT32
)
232 return _UVRSR_FAILED
;
234 mask
= discriminator
& 0xffff;
235 ptr
= (_uw
*) vrs
->core
.r
[R_SP
];
236 /* Pop the requested registers. */
237 for (i
= 0; i
< 16; i
++)
240 vrs
->core
.r
[i
] = *(ptr
++);
242 /* Writeback the stack pointer value if it wasn't restored. */
243 if ((mask
& (1 << R_SP
)) == 0)
244 vrs
->core
.r
[R_SP
] = (_uw
) ptr
;
250 _uw start
= discriminator
>> 16;
251 _uw count
= discriminator
& 0xffff;
253 struct vfpv3_regs tmp_16_to_31
;
257 int num_vfpv3_regs
= 0;
259 /* We use an approximation here by bounding _UVRSD_DOUBLE
260 register numbers at 32 always, since we can't detect if
261 VFPv3 isn't present (in such a case the upper limit is 16). */
262 if ((representation
!= _UVRSD_VFPX
&& representation
!= _UVRSD_DOUBLE
)
263 || start
+ count
> (representation
== _UVRSD_VFPX
? 16 : 32)
264 || (representation
== _UVRSD_VFPX
&& start
>= 16))
265 return _UVRSR_FAILED
;
267 /* Check if we're being asked to pop VFPv3-only registers
268 (numbers 16 through 31). */
270 num_vfpv3_regs
= count
;
271 else if (start
+ count
> 16)
272 num_vfpv3_regs
= start
+ count
- 16;
274 if (num_vfpv3_regs
&& representation
!= _UVRSD_DOUBLE
)
275 return _UVRSR_FAILED
;
277 /* Demand-save coprocessor registers for stage1. */
278 if (start
< 16 && (vrs
->demand_save_flags
& DEMAND_SAVE_VFP
))
280 vrs
->demand_save_flags
&= ~DEMAND_SAVE_VFP
;
282 if (representation
== _UVRSD_DOUBLE
)
284 /* Save in FLDMD/FSTMD format. */
285 vrs
->demand_save_flags
|= DEMAND_SAVE_VFP_D
;
286 __gnu_Unwind_Save_VFP_D (&vrs
->vfp
);
290 /* Save in FLDMX/FSTMX format. */
291 vrs
->demand_save_flags
&= ~DEMAND_SAVE_VFP_D
;
292 __gnu_Unwind_Save_VFP (&vrs
->vfp
);
296 if (num_vfpv3_regs
> 0
297 && (vrs
->demand_save_flags
& DEMAND_SAVE_VFP_V3
))
299 vrs
->demand_save_flags
&= ~DEMAND_SAVE_VFP_V3
;
300 __gnu_Unwind_Save_VFP_D_16_to_31 (&vrs
->vfp_regs_16_to_31
);
303 /* Restore the registers from the stack. Do this by saving the
304 current VFP registers to a memory area, moving the in-memory
305 values into that area, and restoring from the whole area.
306 For _UVRSD_VFPX we assume FSTMX standard format 1. */
307 if (representation
== _UVRSD_VFPX
)
308 __gnu_Unwind_Save_VFP (&tmp
);
311 /* Save registers 0 .. 15 if required. */
313 __gnu_Unwind_Save_VFP_D (&tmp
);
315 /* Save VFPv3 registers 16 .. 31 if required. */
317 __gnu_Unwind_Save_VFP_D_16_to_31 (&tmp_16_to_31
);
320 /* Work out how many registers below register 16 need popping. */
321 tmp_count
= num_vfpv3_regs
> 0 ? 16 - start
: count
;
323 /* Copy registers below 16, if needed.
324 The stack address is only guaranteed to be word aligned, so
325 we can't use doubleword copies. */
326 sp
= (_uw
*) vrs
->core
.r
[R_SP
];
330 dest
= (_uw
*) &tmp
.d
[start
];
335 /* Copy VFPv3 registers numbered >= 16, if needed. */
336 if (num_vfpv3_regs
> 0)
338 /* num_vfpv3_regs is needed below, so copy it. */
339 int tmp_count_2
= num_vfpv3_regs
* 2;
340 int vfpv3_start
= start
< 16 ? 16 : start
;
342 dest
= (_uw
*) &tmp_16_to_31
.d
[vfpv3_start
- 16];
343 while (tmp_count_2
--)
347 /* Skip the format word space if using FLDMX/FSTMX format. */
348 if (representation
== _UVRSD_VFPX
)
351 /* Set the new stack pointer. */
352 vrs
->core
.r
[R_SP
] = (_uw
) sp
;
354 /* Reload the registers. */
355 if (representation
== _UVRSD_VFPX
)
356 __gnu_Unwind_Restore_VFP (&tmp
);
359 /* Restore registers 0 .. 15 if required. */
361 __gnu_Unwind_Restore_VFP_D (&tmp
);
363 /* Restore VFPv3 registers 16 .. 31 if required. */
364 if (num_vfpv3_regs
> 0)
365 __gnu_Unwind_Restore_VFP_D_16_to_31 (&tmp_16_to_31
);
372 _uw start
= discriminator
>> 16;
373 _uw count
= discriminator
& 0xffff;
374 struct wmmxd_regs tmp
;
378 if ((representation
!= _UVRSD_UINT64
) || start
+ count
> 16)
379 return _UVRSR_FAILED
;
381 if (vrs
->demand_save_flags
& DEMAND_SAVE_WMMXD
)
383 /* Demand-save resisters for stage1. */
384 vrs
->demand_save_flags
&= ~DEMAND_SAVE_WMMXD
;
385 __gnu_Unwind_Save_WMMXD (&vrs
->wmmxd
);
388 /* Restore the registers from the stack. Do this by saving the
389 current WMMXD registers to a memory area, moving the in-memory
390 values into that area, and restoring from the whole area. */
391 __gnu_Unwind_Save_WMMXD (&tmp
);
393 /* The stack address is only guaranteed to be word aligned, so
394 we can't use doubleword copies. */
395 sp
= (_uw
*) vrs
->core
.r
[R_SP
];
396 dest
= (_uw
*) &tmp
.wd
[start
];
401 /* Set the new stack pointer. */
402 vrs
->core
.r
[R_SP
] = (_uw
) sp
;
404 /* Reload the registers. */
405 __gnu_Unwind_Restore_WMMXD (&tmp
);
412 struct wmmxc_regs tmp
;
415 if ((representation
!= _UVRSD_UINT32
) || discriminator
> 16)
416 return _UVRSR_FAILED
;
418 if (vrs
->demand_save_flags
& DEMAND_SAVE_WMMXC
)
420 /* Demand-save resisters for stage1. */
421 vrs
->demand_save_flags
&= ~DEMAND_SAVE_WMMXC
;
422 __gnu_Unwind_Save_WMMXC (&vrs
->wmmxc
);
425 /* Restore the registers from the stack. Do this by saving the
426 current WMMXC registers to a memory area, moving the in-memory
427 values into that area, and restoring from the whole area. */
428 __gnu_Unwind_Save_WMMXC (&tmp
);
430 sp
= (_uw
*) vrs
->core
.r
[R_SP
];
431 for (i
= 0; i
< 4; i
++)
432 if (discriminator
& (1 << i
))
435 /* Set the new stack pointer. */
436 vrs
->core
.r
[R_SP
] = (_uw
) sp
;
438 /* Reload the registers. */
439 __gnu_Unwind_Restore_WMMXC (&tmp
);
444 return _UVRSR_FAILED
;
449 /* Core unwinding functions. */
451 /* Calculate the address encoded by a 31-bit self-relative offset at address
454 selfrel_offset31 (const _uw
*p
)
459 /* Sign extend to 32 bits. */
460 if (offset
& (1 << 30))
463 offset
&= ~(1u << 31);
465 return offset
+ (_uw
) p
;
469 __gnu_unwind_get_pr_addr (int idx
)
474 return (_uw
) &__aeabi_unwind_cpp_pr0
;
477 return (_uw
) &__aeabi_unwind_cpp_pr1
;
480 return (_uw
) &__aeabi_unwind_cpp_pr2
;
487 /* ABI defined personality routine entry points. */
490 __aeabi_unwind_cpp_pr0 (_Unwind_State state
,
491 _Unwind_Control_Block
*ucbp
,
492 _Unwind_Context
*context
)
494 return __gnu_unwind_pr_common (state
, ucbp
, context
, 0);
498 __aeabi_unwind_cpp_pr1 (_Unwind_State state
,
499 _Unwind_Control_Block
*ucbp
,
500 _Unwind_Context
*context
)
502 return __gnu_unwind_pr_common (state
, ucbp
, context
, 1);
506 __aeabi_unwind_cpp_pr2 (_Unwind_State state
,
507 _Unwind_Control_Block
*ucbp
,
508 _Unwind_Context
*context
)
510 return __gnu_unwind_pr_common (state
, ucbp
, context
, 2);