Imported Upstream version 0.160
[platform/upstream/elfutils.git] / backends / s390_initreg.c
1 /* Fetch live process registers from TID.
2    Copyright (C) 2013 Red Hat, Inc.
3    This file is part of elfutils.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11
12    or
13
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17
18    or both in parallel, as here.
19
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "system.h"
34 #include <assert.h>
35 #ifdef __s390__
36 # include <sys/user.h>
37 # include <asm/ptrace.h>
38 # include <sys/ptrace.h>
39 #endif
40
41 #define BACKEND s390_
42 #include "libebl_CPU.h"
43
44 bool
45 s390_set_initial_registers_tid (pid_t tid __attribute__ ((unused)),
46                           ebl_tid_registers_t *setfunc __attribute__ ((unused)),
47                                 void *arg __attribute__ ((unused)))
48 {
49 #ifndef __s390__
50   return false;
51 #else /* __s390__ */
52   struct user user_regs;
53   ptrace_area parea;
54   parea.process_addr = (uintptr_t) &user_regs;
55   parea.kernel_addr = 0;
56   parea.len = sizeof (user_regs);
57   if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea, NULL) != 0)
58     return false;
59   /* If we run as s390x we get the 64-bit registers of tid.
60      But -m31 executable seems to use only the 32-bit parts of its
61      registers so we ignore the upper half.  */
62   Dwarf_Word dwarf_regs[16];
63   for (unsigned u = 0; u < 16; u++)
64     dwarf_regs[u] = user_regs.regs.gprs[u];
65   if (! setfunc (0, 16, dwarf_regs, arg))
66     return false;
67   /* Avoid conversion double -> integer.  */
68   eu_static_assert (sizeof user_regs.regs.fp_regs.fprs[0]
69                     == sizeof dwarf_regs[0]);
70   for (unsigned u = 0; u < 16; u++)
71     {
72       // Store the double bits as is in the Dwarf_Word without conversion.
73       union
74         {
75           double d;
76           Dwarf_Word w;
77         } fpr = { .d = user_regs.regs.fp_regs.fprs[u] };
78       dwarf_regs[u] = fpr.w;
79     }
80
81   if (! setfunc (16, 16, dwarf_regs, arg))
82     return false;
83   dwarf_regs[0] = user_regs.regs.psw.addr;
84   return setfunc (-1, 1, dwarf_regs, arg);
85 #endif /* __s390__ */
86 }
87
88 void
89 s390_normalize_pc (Ebl *ebl __attribute__ ((unused)), Dwarf_Addr *pc)
90 {
91   assert (ebl->class == ELFCLASS32);
92
93   /* Clear S390 bit 31.  */
94   *pc &= (1U << 31) - 1;
95 }