1/* ARM EABI compliant unwinding routines.
2   Copyright (C) 2004-2015 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#include "unwind.h"
25
26/* Misc constants.  */
27#define R_IP	12
28#define R_SP	13
29#define R_LR	14
30#define R_PC	15
31
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])
35
36struct core_regs
37{
38  _uw r[16];
39};
40
41/* We use normal integer types here to avoid the compiler generating
42   coprocessor instructions.  */
43struct vfp_regs
44{
45  _uw64 d[16];
46  _uw pad;
47};
48
49struct vfpv3_regs
50{
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).  */
53  _uw64 d[16];
54};
55
56struct wmmxd_regs
57{
58  _uw64 wd[16];
59};
60
61struct wmmxc_regs
62{
63  _uw wc[4];
64};
65
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.
70
71   During unwinding we hold the coprocessor state in the actual hardware
72   registers and allocate demand-save areas for use during phase1
73   unwinding.  */
74
75typedef struct
76{
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.  */
81  struct vfp_regs vfp;
82  struct vfpv3_regs vfp_regs_16_to_31;
83  struct wmmxd_regs wmmxd;
84  struct wmmxc_regs wmmxc;
85} phase1_vrs;
86
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
92				   saved if not set.  */
93#define DEMAND_SAVE_WMMXC 16	/* iWMMXt control registers have been
94				   saved if not set.  */
95
96/* This must match the structure created by the assembly wrappers.  */
97typedef struct
98{
99  _uw demand_save_flags;
100  struct core_regs core;
101} phase2_vrs;
102
103/* Coprocessor register state manipulation functions.  */
104
105/* Routines for FLDMX/FSTMX format...  */
106void __gnu_Unwind_Save_VFP (struct vfp_regs * p);
107void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);
108void __gnu_Unwind_Save_WMMXD (struct wmmxd_regs * p);
109void __gnu_Unwind_Restore_WMMXD (struct wmmxd_regs * p);
110void __gnu_Unwind_Save_WMMXC (struct wmmxc_regs * p);
111void __gnu_Unwind_Restore_WMMXC (struct wmmxc_regs * p);
112
113/* ...and those for FLDMD/FSTMD format...  */
114void __gnu_Unwind_Save_VFP_D (struct vfp_regs * p);
115void __gnu_Unwind_Restore_VFP_D (struct vfp_regs * p);
116
117/* ...and those for VLDM/VSTM format, saving/restoring only registers
118   16 through 31.  */
119void __gnu_Unwind_Save_VFP_D_16_to_31 (struct vfpv3_regs * p);
120void __gnu_Unwind_Restore_VFP_D_16_to_31 (struct vfpv3_regs * p);
121
122/* Restore coprocessor state after phase1 unwinding.  */
123static void
124restore_non_core_regs (phase1_vrs * vrs)
125{
126  if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0)
127    {
128      if (vrs->demand_save_flags & DEMAND_SAVE_VFP_D)
129        __gnu_Unwind_Restore_VFP_D (&vrs->vfp);
130      else
131        __gnu_Unwind_Restore_VFP (&vrs->vfp);
132    }
133
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);
136
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);
141}
142
143#include "unwind-arm-common.inc"
144
145/* ABI defined personality routines.  */
146extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State,
147    _Unwind_Control_Block *, _Unwind_Context *);// __attribute__((weak));
148extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State,
149    _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
150extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State,
151    _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
152
153/* ABI defined routine to store a virtual register to memory.  */
154
155_Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context,
156				    _Unwind_VRS_RegClass regclass,
157				    _uw regno,
158				    _Unwind_VRS_DataRepresentation representation,
159				    void *valuep)
160{
161  phase1_vrs *vrs = (phase1_vrs *) context;
162
163  switch (regclass)
164    {
165    case _UVRSC_CORE:
166      if (representation != _UVRSD_UINT32
167	  || regno > 15)
168	return _UVRSR_FAILED;
169      *(_uw *) valuep = vrs->core.r[regno];
170      return _UVRSR_OK;
171
172    case _UVRSC_VFP:
173    case _UVRSC_WMMXD:
174    case _UVRSC_WMMXC:
175      return _UVRSR_NOT_IMPLEMENTED;
176
177    default:
178      return _UVRSR_FAILED;
179    }
180}
181
182
183/* ABI defined function to load a virtual register from memory.  */
184
185_Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context,
186				    _Unwind_VRS_RegClass regclass,
187				    _uw regno,
188				    _Unwind_VRS_DataRepresentation representation,
189				    void *valuep)
190{
191  phase1_vrs *vrs = (phase1_vrs *) context;
192
193  switch (regclass)
194    {
195    case _UVRSC_CORE:
196      if (representation != _UVRSD_UINT32
197	  || regno > 15)
198	return _UVRSR_FAILED;
199
200      vrs->core.r[regno] = *(_uw *) valuep;
201      return _UVRSR_OK;
202
203    case _UVRSC_VFP:
204    case _UVRSC_WMMXD:
205    case _UVRSC_WMMXC:
206      return _UVRSR_NOT_IMPLEMENTED;
207
208    default:
209      return _UVRSR_FAILED;
210    }
211}
212
213
214/* ABI defined function to pop registers off the stack.  */
215
216_Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context,
217				    _Unwind_VRS_RegClass regclass,
218				    _uw discriminator,
219				    _Unwind_VRS_DataRepresentation representation)
220{
221  phase1_vrs *vrs = (phase1_vrs *) context;
222
223  switch (regclass)
224    {
225    case _UVRSC_CORE:
226      {
227	_uw *ptr;
228	_uw mask;
229	int i;
230
231	if (representation != _UVRSD_UINT32)
232	  return _UVRSR_FAILED;
233
234	mask = discriminator & 0xffff;
235	ptr = (_uw *) vrs->core.r[R_SP];
236	/* Pop the requested registers.  */
237	for (i = 0; i < 16; i++)
238	  {
239	    if (mask & (1 << i))
240	      vrs->core.r[i] = *(ptr++);
241	  }
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;
245      }
246      return _UVRSR_OK;
247
248    case _UVRSC_VFP:
249      {
250	_uw start = discriminator >> 16;
251	_uw count = discriminator & 0xffff;
252	struct vfp_regs tmp;
253	struct vfpv3_regs tmp_16_to_31;
254	int tmp_count;
255	_uw *sp;
256	_uw *dest;
257        int num_vfpv3_regs = 0;
258
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;
266
267        /* Check if we're being asked to pop VFPv3-only registers
268           (numbers 16 through 31).  */
269	if (start >= 16)
270          num_vfpv3_regs = count;
271        else if (start + count > 16)
272          num_vfpv3_regs = start + count - 16;
273
274        if (num_vfpv3_regs && representation != _UVRSD_DOUBLE)
275          return _UVRSR_FAILED;
276
277	/* Demand-save coprocessor registers for stage1.  */
278	if (start < 16 && (vrs->demand_save_flags & DEMAND_SAVE_VFP))
279	  {
280	    vrs->demand_save_flags &= ~DEMAND_SAVE_VFP;
281
282            if (representation == _UVRSD_DOUBLE)
283              {
284                /* Save in FLDMD/FSTMD format.  */
285	        vrs->demand_save_flags |= DEMAND_SAVE_VFP_D;
286	        __gnu_Unwind_Save_VFP_D (&vrs->vfp);
287              }
288            else
289              {
290                /* Save in FLDMX/FSTMX format.  */
291	        vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_D;
292	        __gnu_Unwind_Save_VFP (&vrs->vfp);
293              }
294	  }
295
296        if (num_vfpv3_regs > 0
297            && (vrs->demand_save_flags & DEMAND_SAVE_VFP_V3))
298	  {
299	    vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_V3;
300            __gnu_Unwind_Save_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
301	  }
302
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);
309        else
310          {
311	    /* Save registers 0 .. 15 if required.  */
312            if (start < 16)
313              __gnu_Unwind_Save_VFP_D (&tmp);
314
315	    /* Save VFPv3 registers 16 .. 31 if required.  */
316            if (num_vfpv3_regs)
317  	      __gnu_Unwind_Save_VFP_D_16_to_31 (&tmp_16_to_31);
318          }
319
320	/* Work out how many registers below register 16 need popping.  */
321	tmp_count = num_vfpv3_regs > 0 ? 16 - start : count;
322
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];
327        if (tmp_count > 0)
328          {
329	    tmp_count *= 2;
330	    dest = (_uw *) &tmp.d[start];
331	    while (tmp_count--)
332	      *(dest++) = *(sp++);
333          }
334
335	/* Copy VFPv3 registers numbered >= 16, if needed.  */
336        if (num_vfpv3_regs > 0)
337          {
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;
341
342	    dest = (_uw *) &tmp_16_to_31.d[vfpv3_start - 16];
343	    while (tmp_count_2--)
344	      *(dest++) = *(sp++);
345          }
346
347	/* Skip the format word space if using FLDMX/FSTMX format.  */
348	if (representation == _UVRSD_VFPX)
349	  sp++;
350
351	/* Set the new stack pointer.  */
352	vrs->core.r[R_SP] = (_uw) sp;
353
354	/* Reload the registers.  */
355        if (representation == _UVRSD_VFPX)
356  	  __gnu_Unwind_Restore_VFP (&tmp);
357        else
358          {
359	    /* Restore registers 0 .. 15 if required.  */
360            if (start < 16)
361              __gnu_Unwind_Restore_VFP_D (&tmp);
362
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);
366          }
367      }
368      return _UVRSR_OK;
369
370    case _UVRSC_WMMXD:
371      {
372	_uw start = discriminator >> 16;
373	_uw count = discriminator & 0xffff;
374	struct wmmxd_regs tmp;
375	_uw *sp;
376	_uw *dest;
377
378	if ((representation != _UVRSD_UINT64) || start + count > 16)
379	  return _UVRSR_FAILED;
380
381	if (vrs->demand_save_flags & DEMAND_SAVE_WMMXD)
382	  {
383	    /* Demand-save resisters for stage1.  */
384	    vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXD;
385	    __gnu_Unwind_Save_WMMXD (&vrs->wmmxd);
386	  }
387
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);
392
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];
397	count *= 2;
398	while (count--)
399	  *(dest++) = *(sp++);
400
401	/* Set the new stack pointer.  */
402	vrs->core.r[R_SP] = (_uw) sp;
403
404	/* Reload the registers.  */
405	__gnu_Unwind_Restore_WMMXD (&tmp);
406      }
407      return _UVRSR_OK;
408
409    case _UVRSC_WMMXC:
410      {
411	int i;
412	struct wmmxc_regs tmp;
413	_uw *sp;
414
415	if ((representation != _UVRSD_UINT32) || discriminator > 16)
416	  return _UVRSR_FAILED;
417
418	if (vrs->demand_save_flags & DEMAND_SAVE_WMMXC)
419	  {
420	    /* Demand-save resisters for stage1.  */
421	    vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXC;
422	    __gnu_Unwind_Save_WMMXC (&vrs->wmmxc);
423	  }
424
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);
429
430	sp = (_uw *) vrs->core.r[R_SP];
431	for (i = 0; i < 4; i++)
432	  if (discriminator & (1 << i))
433	    tmp.wc[i] = *(sp++);
434
435	/* Set the new stack pointer.  */
436	vrs->core.r[R_SP] = (_uw) sp;
437
438	/* Reload the registers.  */
439	__gnu_Unwind_Restore_WMMXC (&tmp);
440      }
441      return _UVRSR_OK;
442
443    default:
444      return _UVRSR_FAILED;
445    }
446}
447
448
449/* Core unwinding functions.  */
450
451/* Calculate the address encoded by a 31-bit self-relative offset at address
452   P.  */
453static inline _uw
454selfrel_offset31 (const _uw *p)
455{
456  _uw offset;
457
458  offset = *p;
459  /* Sign extend to 32 bits.  */
460  if (offset & (1 << 30))
461    offset |= 1u << 31;
462  else
463    offset &= ~(1u << 31);
464
465  return offset + (_uw) p;
466}
467
468static _uw
469__gnu_unwind_get_pr_addr (int idx)
470{
471  switch (idx)
472    {
473    case 0:
474      return (_uw) &__aeabi_unwind_cpp_pr0;
475
476    case 1:
477      return (_uw) &__aeabi_unwind_cpp_pr1;
478
479    case 2:
480      return (_uw) &__aeabi_unwind_cpp_pr2;
481
482    default:
483      return 0;
484    }
485}
486
487/* ABI defined personality routine entry points.  */
488
489_Unwind_Reason_Code
490__aeabi_unwind_cpp_pr0 (_Unwind_State state,
491			_Unwind_Control_Block *ucbp,
492			_Unwind_Context *context)
493{
494  return __gnu_unwind_pr_common (state, ucbp, context, 0);
495}
496
497_Unwind_Reason_Code
498__aeabi_unwind_cpp_pr1 (_Unwind_State state,
499			_Unwind_Control_Block *ucbp,
500			_Unwind_Context *context)
501{
502  return __gnu_unwind_pr_common (state, ucbp, context, 1);
503}
504
505_Unwind_Reason_Code
506__aeabi_unwind_cpp_pr2 (_Unwind_State state,
507			_Unwind_Control_Block *ucbp,
508			_Unwind_Context *context)
509{
510  return __gnu_unwind_pr_common (state, ucbp, context, 2);
511}
512