1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6 Modified for s390x by Michael Munday <mike.munday@ibm.com>
7
8 This file is part of libunwind.
9
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice shall be
19 included in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28
29 #include "unwind_i.h"
30 #include <signal.h>
31
32 static int
s390x_handle_signal_frame(unw_cursor_t * cursor)33 s390x_handle_signal_frame (unw_cursor_t *cursor)
34 {
35 struct cursor *c = (struct cursor *) cursor;
36 int ret, i;
37 unw_word_t sc_addr, sp, *gprs, *fprs, *psw;
38
39 ret = dwarf_get (&c->dwarf, c->dwarf.loc[UNW_S390X_R15], &sp);
40 if (ret < 0)
41 return ret;
42
43 /* Save the SP and PC to be able to return execution at this point
44 later in time (unw_resume). */
45 c->sigcontext_sp = sp;
46 c->sigcontext_pc = c->dwarf.ip;
47 switch (c->sigcontext_format)
48 {
49 case S390X_SCF_LINUX_SIGFRAME: /* sigreturn */
50 sc_addr = sp + 160;
51 gprs = ((struct sigcontext*)sc_addr)->sregs->regs.gprs;
52 fprs = (unw_word_t*)((struct sigcontext*)sc_addr)->sregs->fpregs.fprs;
53 psw = &((struct sigcontext*)sc_addr)->sregs->regs.psw.addr;
54 break;
55 case S390X_SCF_LINUX_RT_SIGFRAME: /* rt_sigreturn */
56 sc_addr = sp + sizeof(siginfo_t) + 8 + 160;
57 gprs = ((ucontext_t*)sc_addr)->uc_mcontext.gregs;
58 fprs = (unw_word_t*)((ucontext_t*)sc_addr)->uc_mcontext.fpregs.fprs;
59 psw = &((ucontext_t*)sc_addr)->uc_mcontext.psw.addr;
60 break;
61 default:
62 return -UNW_EUNSPEC;
63 }
64
65 c->sigcontext_addr = sc_addr;
66
67 /* Update the dwarf cursor.
68 Set the location of the registers to the corresponding addresses of the
69 uc_mcontext / sigcontext structure contents. */
70 for (i = UNW_S390X_R0; i <= UNW_S390X_R15; ++i)
71 c->dwarf.loc[i] = DWARF_MEM_LOC (c, (unw_word_t) &gprs[i-UNW_S390X_R0]);
72 for (i = UNW_S390X_F0; i <= UNW_S390X_F15; ++i)
73 c->dwarf.loc[i] = DWARF_MEM_LOC (c, (unw_word_t) &fprs[i-UNW_S390X_F0]);
74
75 c->dwarf.loc[UNW_S390X_IP] = DWARF_MEM_LOC (c, (unw_word_t) psw);
76
77 /* Set SP/CFA and PC/IP.
78 Normally the default CFA on s390x is r15+160. We do not add that offset
79 here because dwarf_step will add the offset. */
80 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_S390X_R15], &c->dwarf.cfa);
81 dwarf_get (&c->dwarf, c->dwarf.loc[UNW_S390X_IP], &c->dwarf.ip);
82
83 c->dwarf.pi_valid = 0;
84 c->dwarf.use_prev_instr = 0;
85
86 return 1;
87 }
88
89 int
unw_step(unw_cursor_t * cursor)90 unw_step (unw_cursor_t *cursor)
91 {
92 struct cursor *c = (struct cursor *) cursor;
93 int ret = 0, val = c->validate, sig;
94
95 #if CONSERVATIVE_CHECKS
96 c->validate = 1;
97 #endif
98
99 Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx)\n",
100 c, c->dwarf.ip, c->dwarf.cfa);
101
102 /* Try DWARF-based unwinding... */
103 c->sigcontext_format = S390X_SCF_NONE;
104 ret = dwarf_step (&c->dwarf);
105
106 #if CONSERVATIVE_CHECKS
107 c->validate = val;
108 #endif
109
110 if (unlikely (ret == -UNW_ENOINFO))
111 {
112 /* GCC doesn't currently emit debug information for signal
113 trampolines on s390x so we check for them explicitly.
114
115 If there isn't debug information available we could also
116 try using the backchain (if available).
117
118 Other platforms also detect PLT entries here. That's
119 tricky to do reliably on s390x so I've left it out for
120 now. */
121
122 /* Memory accesses here are quite likely to be unsafe. */
123 c->validate = 1;
124
125 /* Check if this is a signal frame. */
126 sig = unw_is_signal_frame (cursor);
127 if (sig > 0)
128 {
129 c->sigcontext_format = sig;
130 ret = s390x_handle_signal_frame (cursor);
131 }
132 else
133 {
134 c->dwarf.ip = 0;
135 ret = 0;
136 }
137
138 c->validate = val;
139 return ret;
140 }
141
142 if (unlikely (ret > 0 && c->dwarf.ip == 0))
143 return 0;
144
145 return ret;
146 }
147