1 /* Fetch live process registers from TID.
2    Copyright (C) 2015 Oracle, In
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 <stdlib.h>
35 #ifdef __sparc__
36 # include <asm/ptrace.h>
37 # include <sys/ptrace.h>
38 #endif
39 
40 #define BACKEND sparc_
41 #include "libebl_CPU.h"
42 
43 bool
EBLHOOK(set_initial_registers_tid)44 EBLHOOK (set_initial_registers_tid) (pid_t tid __attribute__ ((unused)),
45                                      ebl_tid_registers_t *setfunc __attribute__ ((unused)),
46                                      void *arg __attribute__ ((unused)))
47 {
48 #if !defined(__sparc__) || !defined( __arch64__)
49   return false;
50 #else /* __sparc__ */
51 
52 
53   /* The pt_regs structure filled in by PTRACE_GETREGS provides the
54      PC, the global registers and the output registers.  Note how the
55      %g0 register is not explicitly provided in the structure (it's
56      value is always 0) and the resulting weird packing in the u_regs
57      array: the last element is not used.  */
58 
59   struct pt_regs regs;
60   if (ptrace (PTRACE_GETREGS, tid, &regs, 0) == -1)
61     return false;
62 
63   /* PC: no DWARF number  */
64   if (!setfunc (-1, 1, (Dwarf_Word *) &regs.tpc, arg))
65     return false;
66 
67   /* Global registers: DWARF 0 .. 7  */
68   Dwarf_Word zero = 0;
69   if (!setfunc (0, 1, &zero, arg))
70     return false;
71   if (!setfunc (1, 7, (Dwarf_Word *) &regs.u_regs[0], arg))
72     return false;
73 
74   /* Output registers: DWARF  8 .. 15  */
75   if (!setfunc (8, 8, (Dwarf_Word *) &regs.u_regs[7], arg))
76     return false;
77 
78   /* Local and input registers must be read from the stack.  They are
79      saved in the previous stack frame.  The stack pointer is %o6,
80      read above.  */
81 
82   Dwarf_Word locals_outs[16];
83   Dwarf_Word sp = regs.u_regs[13];
84 
85   if (sp & 1)
86     {
87       /* Registers are 64 bits, and we need to apply the 2047 stack
88          bias in order to get the real stack pointer.  */
89 
90       sp += 2047;
91 
92       for (unsigned i = 0; i < 16; i++)
93         {
94           locals_outs[i] = ptrace (PTRACE_PEEKDATA, tid,
95                                    (void *) (uintptr_t) (sp + (i * 8)),
96                                    NULL);
97           if (errno != 0)
98             return false;
99         }
100     }
101   else
102     {
103       /* Registers are 32 bits.  */
104 
105       for (unsigned i = 0; i < 8; i++)
106         {
107           Dwarf_Word tuple = ptrace (PTRACE_PEEKDATA, tid,
108                                      (void *) (uintptr_t) (sp + (i * 8)),
109                                      NULL);
110           if (errno != 0)
111             return false;
112 
113           locals_outs[2*i] = (tuple >> 32) & 0xffffffff;
114           locals_outs[2*i+1] = tuple & 0xffffffff;
115         }
116     }
117 
118 
119   /* Local registers:  DWARF 16 .. 23 */
120   if (!setfunc (16, 8, &locals_outs[0], arg))
121     return false;
122 
123   /* Input registers: DWARF 24 .. 31 */
124   if (!setfunc (24, 8, &locals_outs[8], arg))
125     return false;
126 
127   return true;
128 #endif
129 }
130