1/* DWARF2 EH unwinding support for Alpha Linux.
2   Copyright (C) 2004-2022 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3, or (at your option)
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16Under Section 7 of GPL version 3, you are granted additional
17permissions described in the GCC Runtime Library Exception, version
183.1, as published by the Free Software Foundation.
19
20You should have received a copy of the GNU General Public License and
21a copy of the GCC Runtime Library Exception along with this program;
22see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23<http://www.gnu.org/licenses/>.  */
24
25#ifndef inhibit_libc
26/* Do code reading to identify a signal frame, and set the frame
27   state data appropriately.  See unwind-dw2.c for the structs.  */
28
29#include <signal.h>
30#include <sys/ucontext.h>
31
32#define MD_FALLBACK_FRAME_STATE_FOR alpha_fallback_frame_state
33
34static _Unwind_Reason_Code
35alpha_fallback_frame_state (struct _Unwind_Context *context,
36			    _Unwind_FrameState *fs)
37{
38  unsigned int *pc = context->ra;
39  struct sigcontext *sc;
40  long new_cfa;
41  int i;
42
43  if (pc[0] != 0x47fe0410		/* mov $30,$16 */
44      || pc[2] != 0x00000083)		/* callsys */
45    return _URC_END_OF_STACK;
46  if (context->cfa == 0)
47    return _URC_END_OF_STACK;
48  if (pc[1] == 0x201f0067)		/* lda $0,NR_sigreturn */
49    sc = context->cfa;
50  else if (pc[1] == 0x201f015f)		/* lda $0,NR_rt_sigreturn */
51    {
52      struct rt_sigframe {
53	siginfo_t info;
54	ucontext_t uc;
55      } *rt_ = context->cfa;
56      /* The void * cast is necessary to avoid an aliasing warning.
57         The aliasing warning is correct, but should not be a problem
58         because it does not alias anything.  */
59      sc = (struct sigcontext *) (void *) &rt_->uc.uc_mcontext;
60    }
61  else
62    return _URC_END_OF_STACK;
63
64  new_cfa = sc->sc_regs[30];
65  fs->regs.cfa_how = CFA_REG_OFFSET;
66  fs->regs.cfa_reg = 30;
67  fs->regs.cfa_offset = new_cfa - (long) context->cfa;
68  for (i = 0; i < 30; ++i)
69    {
70      fs->regs.reg[i].how = REG_SAVED_OFFSET;
71      fs->regs.reg[i].loc.offset
72	= (long) &sc->sc_regs[i] - new_cfa;
73    }
74  for (i = 0; i < 31; ++i)
75    {
76      fs->regs.reg[i+32].how = REG_SAVED_OFFSET;
77      fs->regs.reg[i+32].loc.offset
78	= (long) &sc->sc_fpregs[i] - new_cfa;
79    }
80  fs->regs.reg[64].how = REG_SAVED_OFFSET;
81  fs->regs.reg[64].loc.offset = (long)&sc->sc_pc - new_cfa;
82  fs->retaddr_column = 64;
83  fs->signal_frame = 1;
84
85  return _URC_NO_REASON;
86}
87
88#define MD_FROB_UPDATE_CONTEXT alpha_frob_update_context
89
90/* Fix up for signal handlers that don't have S flag set.  */
91
92static void
93alpha_frob_update_context (struct _Unwind_Context *context,
94			   _Unwind_FrameState *fs ATTRIBUTE_UNUSED)
95{
96  unsigned int *pc = context->ra;
97
98  if (pc[0] == 0x47fe0410		/* mov $30,$16 */
99      && pc[2] == 0x00000083		/* callsys */
100      && (pc[1] == 0x201f0067		/* lda $0,NR_sigreturn */
101	  || pc[1] == 0x201f015f))	/* lda $0,NR_rt_sigreturn */
102    _Unwind_SetSignalFrame (context, 1);
103}
104#endif
105