1/* ARM EABI compliant unwinding routines.
2   Copyright (C) 2004-2022 Free Software Foundation, Inc.
3   Contributed by Paul Brook
4
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
8   later version.
9
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.
14
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.
18
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/>.  */
23
24#pragma GCC target ("general-regs-only")
25#include "unwind.h"
26
27/* Misc constants.  */
28#define R_IP	12
29#define R_SP	13
30#define R_LR	14
31#define R_PC	15
32
33#define VRS_PC(vrs) ((vrs)->core.r[R_PC])
34#define VRS_SP(vrs) ((vrs)->core.r[R_SP])
35#define VRS_RETURN(vrs) ((vrs)->core.r[R_LR])
36
37struct core_regs
38{
39  _uw r[16];
40};
41
42/* We use normal integer types here to avoid the compiler generating
43   coprocessor instructions.  */
44struct vfp_regs
45{
46  _uw64 d[16];
47  _uw pad;
48};
49
50struct vfpv3_regs
51{
52  /* Always populated via VSTM, so no need for the "pad" field from
53     vfp_regs (which is used to store the format word for FSTMX).  */
54  _uw64 d[16];
55};
56
57struct wmmxd_regs
58{
59  _uw64 wd[16];
60};
61
62struct wmmxc_regs
63{
64  _uw wc[4];
65};
66
67/* The ABI specifies that the unwind routines may only use core registers,
68   except when actually manipulating coprocessor state.  This allows
69   us to write one implementation that works on all platforms by
70   demand-saving coprocessor registers.
71
72   During unwinding we hold the coprocessor state in the actual hardware
73   registers and allocate demand-save areas for use during phase1
74   unwinding.  */
75
76typedef struct
77{
78  /* The first fields must be the same as a phase2_vrs.  */
79  _uw demand_save_flags;
80  struct core_regs core;
81  _uw prev_sp; /* Only valid during forced unwinding.  */
82  struct vfp_regs vfp;
83  struct vfpv3_regs vfp_regs_16_to_31;
84  struct wmmxd_regs wmmxd;
85  struct wmmxc_regs wmmxc;
86} phase1_vrs;
87
88#define DEMAND_SAVE_VFP 1	/* VFP state has been saved if not set */
89#define DEMAND_SAVE_VFP_D 2	/* VFP state is for FLDMD/FSTMD if set */
90#define DEMAND_SAVE_VFP_V3 4    /* VFPv3 state for regs 16 .. 31 has
91                                   been saved if not set */
92#define DEMAND_SAVE_WMMXD 8	/* iWMMXt data registers have been
93				   saved if not set.  */
94#define DEMAND_SAVE_WMMXC 16	/* iWMMXt control registers have been
95				   saved if not set.  */
96
97/* This must match the structure created by the assembly wrappers.  */
98typedef struct
99{
100  _uw demand_save_flags;
101  struct core_regs core;
102} phase2_vrs;
103
104/* Coprocessor register state manipulation functions.  */
105
106/* Routines for FLDMX/FSTMX format...  */
107void __gnu_Unwind_Save_VFP (struct vfp_regs * p);
108void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);
109void __gnu_Unwind_Save_WMMXD (struct wmmxd_regs * p);
110void __gnu_Unwind_Restore_WMMXD (struct wmmxd_regs * p);
111void __gnu_Unwind_Save_WMMXC (struct wmmxc_regs * p);
112void __gnu_Unwind_Restore_WMMXC (struct wmmxc_regs * p);
113
114/* ...and those for FLDMD/FSTMD format...  */
115void __gnu_Unwind_Save_VFP_D (struct vfp_regs * p);
116void __gnu_Unwind_Restore_VFP_D (struct vfp_regs * p);
117
118/* ...and those for VLDM/VSTM format, saving/restoring only registers
119   16 through 31.  */
120void __gnu_Unwind_Save_VFP_D_16_to_31 (struct vfpv3_regs * p);
121void __gnu_Unwind_Restore_VFP_D_16_to_31 (struct vfpv3_regs * p);
122
123/* Restore coprocessor state after phase1 unwinding.  */
124static void
125restore_non_core_regs (phase1_vrs * vrs)
126{
127  if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0)
128    {
129      if (vrs->demand_save_flags & DEMAND_SAVE_VFP_D)
130        __gnu_Unwind_Restore_VFP_D (&vrs->vfp);
131      else
132        __gnu_Unwind_Restore_VFP (&vrs->vfp);
133    }
134
135  if ((vrs->demand_save_flags & DEMAND_SAVE_VFP_V3) == 0)
136    __gnu_Unwind_Restore_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
137
138  if ((vrs->demand_save_flags & DEMAND_SAVE_WMMXD) == 0)
139    __gnu_Unwind_Restore_WMMXD (&vrs->wmmxd);
140  if ((vrs->demand_save_flags & DEMAND_SAVE_WMMXC) == 0)
141    __gnu_Unwind_Restore_WMMXC (&vrs->wmmxc);
142}
143
144#include "unwind-arm-common.inc"
145
146/* ABI defined personality routines.  */
147extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State,
148    _Unwind_Control_Block *, _Unwind_Context *);// __attribute__((weak));
149extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State,
150    _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
151extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State,
152    _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
153
154/* ABI defined routine to store a virtual register to memory.  */
155
156_Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context,
157				    _Unwind_VRS_RegClass regclass,
158				    _uw regno,
159				    _Unwind_VRS_DataRepresentation representation,
160				    void *valuep)
161{
162  phase1_vrs *vrs = (phase1_vrs *) context;
163
164  switch (regclass)
165    {
166    case _UVRSC_CORE:
167      if (representation != _UVRSD_UINT32
168	  || regno > 15)
169	return _UVRSR_FAILED;
170      *(_uw *) valuep = vrs->core.r[regno];
171      return _UVRSR_OK;
172
173    case _UVRSC_VFP:
174    case _UVRSC_WMMXD:
175    case _UVRSC_WMMXC:
176      return _UVRSR_NOT_IMPLEMENTED;
177
178    default:
179      return _UVRSR_FAILED;
180    }
181}
182
183
184/* ABI defined function to load a virtual register from memory.  */
185
186_Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context,
187				    _Unwind_VRS_RegClass regclass,
188				    _uw regno,
189				    _Unwind_VRS_DataRepresentation representation,
190				    void *valuep)
191{
192  phase1_vrs *vrs = (phase1_vrs *) context;
193
194  switch (regclass)
195    {
196    case _UVRSC_CORE:
197      if (representation != _UVRSD_UINT32
198	  || regno > 15)
199	return _UVRSR_FAILED;
200
201      vrs->core.r[regno] = *(_uw *) valuep;
202      return _UVRSR_OK;
203
204    case _UVRSC_VFP:
205    case _UVRSC_WMMXD:
206    case _UVRSC_WMMXC:
207      return _UVRSR_NOT_IMPLEMENTED;
208
209    default:
210      return _UVRSR_FAILED;
211    }
212}
213
214
215/* ABI defined function to pop registers off the stack.  */
216
217_Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context,
218				    _Unwind_VRS_RegClass regclass,
219				    _uw discriminator,
220				    _Unwind_VRS_DataRepresentation representation)
221{
222  phase1_vrs *vrs = (phase1_vrs *) context;
223
224  switch (regclass)
225    {
226    case _UVRSC_CORE:
227      {
228	_uw *ptr;
229	_uw mask;
230	int i;
231
232	if (representation != _UVRSD_UINT32)
233	  return _UVRSR_FAILED;
234
235	mask = discriminator & 0xffff;
236	ptr = (_uw *) vrs->core.r[R_SP];
237	/* Pop the requested registers.  */
238	for (i = 0; i < 16; i++)
239	  {
240	    if (mask & (1 << i))
241	      vrs->core.r[i] = *(ptr++);
242	  }
243	/* Writeback the stack pointer value if it wasn't restored.  */
244	if ((mask & (1 << R_SP)) == 0)
245	  vrs->core.r[R_SP] = (_uw) ptr;
246      }
247      return _UVRSR_OK;
248
249    case _UVRSC_VFP:
250      {
251	_uw start = discriminator >> 16;
252	_uw count = discriminator & 0xffff;
253	struct vfp_regs tmp;
254	struct vfpv3_regs tmp_16_to_31;
255	int tmp_count;
256	_uw *sp;
257	_uw *dest;
258        int num_vfpv3_regs = 0;
259
260        /* We use an approximation here by bounding _UVRSD_DOUBLE
261           register numbers at 32 always, since we can't detect if
262           VFPv3 isn't present (in such a case the upper limit is 16).  */
263	if ((representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
264            || start + count > (representation == _UVRSD_VFPX ? 16 : 32)
265            || (representation == _UVRSD_VFPX && start >= 16))
266	  return _UVRSR_FAILED;
267
268        /* Check if we're being asked to pop VFPv3-only registers
269           (numbers 16 through 31).  */
270	if (start >= 16)
271          num_vfpv3_regs = count;
272        else if (start + count > 16)
273          num_vfpv3_regs = start + count - 16;
274
275        if (num_vfpv3_regs && representation != _UVRSD_DOUBLE)
276          return _UVRSR_FAILED;
277
278	/* Demand-save coprocessor registers for stage1.  */
279	if (start < 16 && (vrs->demand_save_flags & DEMAND_SAVE_VFP))
280	  {
281	    vrs->demand_save_flags &= ~DEMAND_SAVE_VFP;
282
283            if (representation == _UVRSD_DOUBLE)
284              {
285                /* Save in FLDMD/FSTMD format.  */
286	        vrs->demand_save_flags |= DEMAND_SAVE_VFP_D;
287	        __gnu_Unwind_Save_VFP_D (&vrs->vfp);
288              }
289            else
290              {
291                /* Save in FLDMX/FSTMX format.  */
292	        vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_D;
293	        __gnu_Unwind_Save_VFP (&vrs->vfp);
294              }
295	  }
296
297        if (num_vfpv3_regs > 0
298            && (vrs->demand_save_flags & DEMAND_SAVE_VFP_V3))
299	  {
300	    vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_V3;
301            __gnu_Unwind_Save_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
302	  }
303
304	/* Restore the registers from the stack.  Do this by saving the
305	   current VFP registers to a memory area, moving the in-memory
306	   values into that area, and restoring from the whole area.
307	   For _UVRSD_VFPX we assume FSTMX standard format 1.  */
308        if (representation == _UVRSD_VFPX)
309  	  __gnu_Unwind_Save_VFP (&tmp);
310        else
311          {
312	    /* Save registers 0 .. 15 if required.  */
313            if (start < 16)
314              __gnu_Unwind_Save_VFP_D (&tmp);
315
316	    /* Save VFPv3 registers 16 .. 31 if required.  */
317            if (num_vfpv3_regs)
318  	      __gnu_Unwind_Save_VFP_D_16_to_31 (&tmp_16_to_31);
319          }
320
321	/* Work out how many registers below register 16 need popping.  */
322	tmp_count = num_vfpv3_regs > 0 ? 16 - start : count;
323
324	/* Copy registers below 16, if needed.
325	   The stack address is only guaranteed to be word aligned, so
326	   we can't use doubleword copies.  */
327	sp = (_uw *) vrs->core.r[R_SP];
328        if (tmp_count > 0)
329          {
330	    tmp_count *= 2;
331	    dest = (_uw *) &tmp.d[start];
332	    while (tmp_count--)
333	      *(dest++) = *(sp++);
334          }
335
336	/* Copy VFPv3 registers numbered >= 16, if needed.  */
337        if (num_vfpv3_regs > 0)
338          {
339            /* num_vfpv3_regs is needed below, so copy it.  */
340            int tmp_count_2 = num_vfpv3_regs * 2;
341            int vfpv3_start = start < 16 ? 16 : start;
342
343	    dest = (_uw *) &tmp_16_to_31.d[vfpv3_start - 16];
344	    while (tmp_count_2--)
345	      *(dest++) = *(sp++);
346          }
347
348	/* Skip the format word space if using FLDMX/FSTMX format.  */
349	if (representation == _UVRSD_VFPX)
350	  sp++;
351
352	/* Set the new stack pointer.  */
353	vrs->core.r[R_SP] = (_uw) sp;
354
355	/* Reload the registers.  */
356        if (representation == _UVRSD_VFPX)
357  	  __gnu_Unwind_Restore_VFP (&tmp);
358        else
359          {
360	    /* Restore registers 0 .. 15 if required.  */
361            if (start < 16)
362              __gnu_Unwind_Restore_VFP_D (&tmp);
363
364	    /* Restore VFPv3 registers 16 .. 31 if required.  */
365            if (num_vfpv3_regs > 0)
366  	      __gnu_Unwind_Restore_VFP_D_16_to_31 (&tmp_16_to_31);
367          }
368      }
369      return _UVRSR_OK;
370
371    case _UVRSC_WMMXD:
372      {
373	_uw start = discriminator >> 16;
374	_uw count = discriminator & 0xffff;
375	struct wmmxd_regs tmp;
376	_uw *sp;
377	_uw *dest;
378
379	if ((representation != _UVRSD_UINT64) || start + count > 16)
380	  return _UVRSR_FAILED;
381
382	if (vrs->demand_save_flags & DEMAND_SAVE_WMMXD)
383	  {
384	    /* Demand-save resisters for stage1.  */
385	    vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXD;
386	    __gnu_Unwind_Save_WMMXD (&vrs->wmmxd);
387	  }
388
389	/* Restore the registers from the stack.  Do this by saving the
390	   current WMMXD registers to a memory area, moving the in-memory
391	   values into that area, and restoring from the whole area.  */
392	__gnu_Unwind_Save_WMMXD (&tmp);
393
394	/* The stack address is only guaranteed to be word aligned, so
395	   we can't use doubleword copies.  */
396	sp = (_uw *) vrs->core.r[R_SP];
397	dest = (_uw *) &tmp.wd[start];
398	count *= 2;
399	while (count--)
400	  *(dest++) = *(sp++);
401
402	/* Set the new stack pointer.  */
403	vrs->core.r[R_SP] = (_uw) sp;
404
405	/* Reload the registers.  */
406	__gnu_Unwind_Restore_WMMXD (&tmp);
407      }
408      return _UVRSR_OK;
409
410    case _UVRSC_WMMXC:
411      {
412	int i;
413	struct wmmxc_regs tmp;
414	_uw *sp;
415
416	if ((representation != _UVRSD_UINT32) || discriminator > 16)
417	  return _UVRSR_FAILED;
418
419	if (vrs->demand_save_flags & DEMAND_SAVE_WMMXC)
420	  {
421	    /* Demand-save resisters for stage1.  */
422	    vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXC;
423	    __gnu_Unwind_Save_WMMXC (&vrs->wmmxc);
424	  }
425
426	/* Restore the registers from the stack.  Do this by saving the
427	   current WMMXC registers to a memory area, moving the in-memory
428	   values into that area, and restoring from the whole area.  */
429	__gnu_Unwind_Save_WMMXC (&tmp);
430
431	sp = (_uw *) vrs->core.r[R_SP];
432	for (i = 0; i < 4; i++)
433	  if (discriminator & (1 << i))
434	    tmp.wc[i] = *(sp++);
435
436	/* Set the new stack pointer.  */
437	vrs->core.r[R_SP] = (_uw) sp;
438
439	/* Reload the registers.  */
440	__gnu_Unwind_Restore_WMMXC (&tmp);
441      }
442      return _UVRSR_OK;
443
444    default:
445      return _UVRSR_FAILED;
446    }
447}
448
449
450/* Core unwinding functions.  */
451
452/* Calculate the address encoded by a 31-bit self-relative offset at address
453   P.  */
454static inline _uw
455selfrel_offset31 (const _uw *p)
456{
457  _uw offset;
458
459  offset = *p;
460  /* Sign extend to 32 bits.  */
461  if (offset & (1 << 30))
462    offset |= 1u << 31;
463  else
464    offset &= ~(1u << 31);
465
466  return offset + (_uw) p;
467}
468
469static _uw
470__gnu_unwind_get_pr_addr (int idx)
471{
472  switch (idx)
473    {
474    case 0:
475      return (_uw) &__aeabi_unwind_cpp_pr0;
476
477    case 1:
478      return (_uw) &__aeabi_unwind_cpp_pr1;
479
480    case 2:
481      return (_uw) &__aeabi_unwind_cpp_pr2;
482
483    default:
484      return 0;
485    }
486}
487
488/* ABI defined personality routine entry points.  */
489
490_Unwind_Reason_Code
491__aeabi_unwind_cpp_pr0 (_Unwind_State state,
492			_Unwind_Control_Block *ucbp,
493			_Unwind_Context *context)
494{
495  return __gnu_unwind_pr_common (state, ucbp, context, 0);
496}
497
498_Unwind_Reason_Code
499__aeabi_unwind_cpp_pr1 (_Unwind_State state,
500			_Unwind_Control_Block *ucbp,
501			_Unwind_Context *context)
502{
503  return __gnu_unwind_pr_common (state, ucbp, context, 1);
504}
505
506_Unwind_Reason_Code
507__aeabi_unwind_cpp_pr2 (_Unwind_State state,
508			_Unwind_Control_Block *ucbp,
509			_Unwind_Context *context)
510{
511  return __gnu_unwind_pr_common (state, ucbp, context, 2);
512}
513
514#ifdef __FreeBSD__
515/* FreeBSD expects these to be functions */
516inline _Unwind_Ptr
517_Unwind_GetIP (struct _Unwind_Context *context)
518{
519  return _Unwind_GetGR (context, 15) & ~(_Unwind_Word)1;
520}
521
522inline _Unwind_Ptr
523_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
524{
525  *ip_before_insn = 0;
526  return _Unwind_GetIP (context);
527}
528
529inline void
530_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
531{
532  _Unwind_SetGR (context, 15, val | (_Unwind_GetGR (context, 15) & 1));
533}
534#endif
535