1 /* libunwind - a platform-independent unwind library
2 Copyright (c) 2002-2004 Hewlett-Packard Development Company, L.P.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
6
7 This file is part of libunwind.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27
28 #include <stdlib.h>
29
30 #include "unwind_i.h"
31
32 #ifndef UNW_REMOTE_ONLY
33
34 HIDDEN inline int
s390x_local_resume(unw_addr_space_t as,unw_cursor_t * cursor,void * arg)35 s390x_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
36 {
37 struct cursor *c = (struct cursor *) cursor;
38 ucontext_t uc = *c->uc;
39 ucontext_t *rt = NULL;
40 struct sigcontext *sc = NULL;
41 int i;
42 unw_word_t sp, ip;
43 uc.uc_mcontext.psw.addr = c->dwarf.ip;
44
45 /* Ensure c->pi is up-to-date. On x86-64, it's relatively common to
46 be missing DWARF unwind info. We don't want to fail in that
47 case, because the frame-chain still would let us do a backtrace
48 at least. */
49 dwarf_make_proc_info (&c->dwarf);
50
51 switch (c->sigcontext_format)
52 {
53 case S390X_SCF_NONE:
54 Debug (8, "resuming at ip=%llx via setcontext()\n",
55 (unsigned long long) c->dwarf.ip);
56 setcontext (&uc);
57 abort(); /* unreachable */
58 case S390X_SCF_LINUX_SIGFRAME:
59 Debug (8, "resuming at ip=%llx via signal trampoline\n",
60 (unsigned long long) c->dwarf.ip);
61 sc = (struct sigcontext*)c->sigcontext_addr;
62 for (i = UNW_S390X_R0; i <= UNW_S390X_R15; ++i)
63 sc->sregs->regs.gprs[i-UNW_S390X_R0] = uc.uc_mcontext.gregs[i-UNW_S390X_R0];
64 for (i = UNW_S390X_F0; i <= UNW_S390X_F15; ++i)
65 sc->sregs->fpregs.fprs[i-UNW_S390X_F0] = uc.uc_mcontext.fpregs.fprs[i-UNW_S390X_F0].d;
66 sc->sregs->regs.psw.addr = uc.uc_mcontext.psw.addr;
67
68 sp = c->sigcontext_sp;
69 ip = c->sigcontext_pc;
70 __asm__ __volatile__ (
71 "lgr %%r15, %[sp]\n"
72 "br %[ip]\n"
73 : : [sp] "r" (sp), [ip] "r" (ip)
74 );
75 abort(); /* unreachable */
76 case S390X_SCF_LINUX_RT_SIGFRAME:
77 Debug (8, "resuming at ip=%llx via signal trampoline\n",
78 (unsigned long long) c->dwarf.ip);
79 rt = (ucontext_t*)c->sigcontext_addr;
80 for (i = UNW_S390X_R0; i <= UNW_S390X_R15; ++i)
81 rt->uc_mcontext.gregs[i-UNW_S390X_R0] = uc.uc_mcontext.gregs[i-UNW_S390X_R0];
82 for (i = UNW_S390X_F0; i <= UNW_S390X_F15; ++i)
83 rt->uc_mcontext.fpregs.fprs[i-UNW_S390X_F0] = uc.uc_mcontext.fpregs.fprs[i-UNW_S390X_F0];
84 rt->uc_mcontext.psw.addr = uc.uc_mcontext.psw.addr;
85
86 sp = c->sigcontext_sp;
87 ip = c->sigcontext_pc;
88 __asm__ __volatile__ (
89 "lgr %%r15, %[sp]\n"
90 "br %[ip]\n"
91 : : [sp] "r" (sp), [ip] "r" (ip)
92 );
93 abort(); /* unreachable */
94 }
95 return -UNW_EINVAL;
96 }
97
98 #endif /* !UNW_REMOTE_ONLY */
99
100 /* This routine is responsible for copying the register values in
101 cursor C and establishing them as the current machine state. */
102
103 static inline int
establish_machine_state(struct cursor * c)104 establish_machine_state (struct cursor *c)
105 {
106 int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
107 int write, void *);
108 int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
109 int write, void *);
110 unw_addr_space_t as = c->dwarf.as;
111 void *arg = c->dwarf.as_arg;
112 unw_fpreg_t fpval;
113 unw_word_t val;
114 int reg;
115
116 access_reg = as->acc.access_reg;
117 access_fpreg = as->acc.access_fpreg;
118
119 Debug (8, "copying out cursor state\n");
120
121 for (reg = 0; reg <= UNW_REG_LAST; ++reg)
122 {
123 Debug (16, "copying %s %d\n", unw_regname (reg), reg);
124 if (unw_is_fpreg (reg))
125 {
126 if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
127 (*access_fpreg) (as, reg, &fpval, 1, arg);
128 }
129 else
130 {
131 if (tdep_access_reg (c, reg, &val, 0) >= 0)
132 (*access_reg) (as, reg, &val, 1, arg);
133 }
134 }
135
136 if (c->dwarf.args_size)
137 {
138 if (tdep_access_reg (c, UNW_S390X_R15, &val, 0) >= 0)
139 {
140 val += c->dwarf.args_size;
141 (*access_reg) (as, UNW_S390X_R15, &val, 1, arg);
142 }
143 }
144 return 0;
145 }
146
147 int
unw_resume(unw_cursor_t * cursor)148 unw_resume (unw_cursor_t *cursor)
149 {
150 struct cursor *c = (struct cursor *) cursor;
151 int ret;
152
153 Debug (1, "(cursor=%p)\n", c);
154
155 if ((ret = establish_machine_state (c)) < 0)
156 return ret;
157
158 return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
159 c->dwarf.as_arg);
160 }
161