1/* DWARF2 EH unwinding support for S/390 Linux.
2   Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11In addition to the permissions in the GNU General Public License, the
12Free Software Foundation gives you unlimited permission to link the
13compiled version of this file with other programs, and to distribute
14those programs without any restriction coming from the use of this
15file.  (The General Public License restrictions do apply in other
16respects; for example, they cover modification of the file, and
17distribution when not linked into another program.)
18
19GCC is distributed in the hope that it will be useful, but WITHOUT ANY
20WARRANTY; without even the implied warranty of MERCHANTABILITY or
21FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22for more details.
23
24You should have received a copy of the GNU General Public License
25along with GCC; see the file COPYING.  If not, write to the Free
26Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2702110-1301, USA.  */
28
29/* Do code reading to identify a signal frame, and set the frame
30   state data appropriately.  See unwind-dw2.c for the structs.  */
31
32#define MD_FALLBACK_FRAME_STATE_FOR s390_fallback_frame_state
33
34static _Unwind_Reason_Code
35s390_fallback_frame_state (struct _Unwind_Context *context,
36			   _Unwind_FrameState *fs)
37{
38  unsigned char *pc = context->ra;
39  long new_cfa;
40  int i;
41
42  typedef struct
43  {
44    unsigned long psw_mask;
45    unsigned long psw_addr;
46    unsigned long gprs[16];
47    unsigned int  acrs[16];
48    unsigned int  fpc;
49    unsigned int  __pad;
50    double        fprs[16];
51  } __attribute__ ((__aligned__ (8))) sigregs_;
52
53  sigregs_ *regs;
54  int *signo;
55
56  /* svc $__NR_sigreturn or svc $__NR_rt_sigreturn  */
57  if (pc[0] != 0x0a || (pc[1] != 119 && pc[1] != 173))
58    return _URC_END_OF_STACK;
59
60  /* Legacy frames:
61       old signal mask (8 bytes)
62       pointer to sigregs (8 bytes) - points always to next location
63       sigregs
64       retcode
65     This frame layout was used on kernels < 2.6.9 for non-RT frames,
66     and on kernels < 2.4.13 for RT frames as well.  Note that we need
67     to look at RA to detect this layout -- this means that if you use
68     sa_restorer to install a different signal restorer on a legacy
69     kernel, unwinding from signal frames will not work.  */
70  if (context->ra == context->cfa + 16 + sizeof (sigregs_))
71    {
72      regs = (sigregs_ *)(context->cfa + 16);
73      signo = NULL;
74    }
75
76  /* New-style RT frame:
77     retcode + alignment (8 bytes)
78     siginfo (128 bytes)
79     ucontext (contains sigregs)  */
80  else if (pc[1] == 173 /* __NR_rt_sigreturn */)
81    {
82      struct ucontext_
83      {
84	unsigned long     uc_flags;
85	struct ucontext_ *uc_link;
86	unsigned long     uc_stack[3];
87	sigregs_          uc_mcontext;
88      } *uc = context->cfa + 8 + 128;
89
90      regs = &uc->uc_mcontext;
91      signo = context->cfa + sizeof(long);
92    }
93
94  /* New-style non-RT frame:
95     old signal mask (8 bytes)
96     pointer to sigregs (followed by signal number)  */
97  else
98    {
99      regs = *(sigregs_ **)(context->cfa + 8);
100      signo = (int *)(regs + 1);
101    }
102
103  new_cfa = regs->gprs[15] + 16*sizeof(long) + 32;
104  fs->cfa_how = CFA_REG_OFFSET;
105  fs->cfa_reg = 15;
106  fs->cfa_offset =
107    new_cfa - (long) context->cfa + 16*sizeof(long) + 32;
108
109  for (i = 0; i < 16; i++)
110    {
111      fs->regs.reg[i].how = REG_SAVED_OFFSET;
112      fs->regs.reg[i].loc.offset =
113	(long)&regs->gprs[i] - new_cfa;
114    }
115  for (i = 0; i < 16; i++)
116    {
117      fs->regs.reg[16+i].how = REG_SAVED_OFFSET;
118      fs->regs.reg[16+i].loc.offset =
119	(long)&regs->fprs[i] - new_cfa;
120    }
121
122  /* Load return addr from PSW into dummy register 32.  */
123
124  fs->regs.reg[32].how = REG_SAVED_OFFSET;
125  fs->regs.reg[32].loc.offset = (long)&regs->psw_addr - new_cfa;
126  fs->retaddr_column = 32;
127  /* SIGILL, SIGFPE and SIGTRAP are delivered with psw_addr
128     after the faulting instruction rather than before it.
129     Don't set FS->signal_frame in that case.  */
130  if (!signo || (*signo != 4 && *signo != 5 && *signo != 8))
131    fs->signal_frame = 1;
132
133  return _URC_NO_REASON;
134}
135