1da0c48c4Sopenharmony_ci/* Fetch live process registers from TID.
2da0c48c4Sopenharmony_ci   Copyright (C) 2015 Oracle, In
3da0c48c4Sopenharmony_ci   This file is part of elfutils.
4da0c48c4Sopenharmony_ci
5da0c48c4Sopenharmony_ci   This file is free software; you can redistribute it and/or modify
6da0c48c4Sopenharmony_ci   it under the terms of either
7da0c48c4Sopenharmony_ci
8da0c48c4Sopenharmony_ci     * the GNU Lesser General Public License as published by the Free
9da0c48c4Sopenharmony_ci       Software Foundation; either version 3 of the License, or (at
10da0c48c4Sopenharmony_ci       your option) any later version
11da0c48c4Sopenharmony_ci
12da0c48c4Sopenharmony_ci   or
13da0c48c4Sopenharmony_ci
14da0c48c4Sopenharmony_ci     * the GNU General Public License as published by the Free
15da0c48c4Sopenharmony_ci       Software Foundation; either version 2 of the License, or (at
16da0c48c4Sopenharmony_ci       your option) any later version
17da0c48c4Sopenharmony_ci
18da0c48c4Sopenharmony_ci   or both in parallel, as here.
19da0c48c4Sopenharmony_ci
20da0c48c4Sopenharmony_ci   elfutils is distributed in the hope that it will be useful, but
21da0c48c4Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
22da0c48c4Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23da0c48c4Sopenharmony_ci   General Public License for more details.
24da0c48c4Sopenharmony_ci
25da0c48c4Sopenharmony_ci   You should have received copies of the GNU General Public License and
26da0c48c4Sopenharmony_ci   the GNU Lesser General Public License along with this program.  If
27da0c48c4Sopenharmony_ci   not, see <http://www.gnu.org/licenses/>.  */
28da0c48c4Sopenharmony_ci
29da0c48c4Sopenharmony_ci#ifdef HAVE_CONFIG_H
30da0c48c4Sopenharmony_ci# include <config.h>
31da0c48c4Sopenharmony_ci#endif
32da0c48c4Sopenharmony_ci
33da0c48c4Sopenharmony_ci#include "system.h"
34da0c48c4Sopenharmony_ci#include <stdlib.h>
35da0c48c4Sopenharmony_ci#ifdef __sparc__
36da0c48c4Sopenharmony_ci# include <asm/ptrace.h>
37da0c48c4Sopenharmony_ci# include <sys/ptrace.h>
38da0c48c4Sopenharmony_ci#endif
39da0c48c4Sopenharmony_ci
40da0c48c4Sopenharmony_ci#define BACKEND sparc_
41da0c48c4Sopenharmony_ci#include "libebl_CPU.h"
42da0c48c4Sopenharmony_ci
43da0c48c4Sopenharmony_cibool
44da0c48c4Sopenharmony_ciEBLHOOK (set_initial_registers_tid) (pid_t tid __attribute__ ((unused)),
45da0c48c4Sopenharmony_ci                                     ebl_tid_registers_t *setfunc __attribute__ ((unused)),
46da0c48c4Sopenharmony_ci                                     void *arg __attribute__ ((unused)))
47da0c48c4Sopenharmony_ci{
48da0c48c4Sopenharmony_ci#if !defined(__sparc__) || !defined( __arch64__)
49da0c48c4Sopenharmony_ci  return false;
50da0c48c4Sopenharmony_ci#else /* __sparc__ */
51da0c48c4Sopenharmony_ci
52da0c48c4Sopenharmony_ci
53da0c48c4Sopenharmony_ci  /* The pt_regs structure filled in by PTRACE_GETREGS provides the
54da0c48c4Sopenharmony_ci     PC, the global registers and the output registers.  Note how the
55da0c48c4Sopenharmony_ci     %g0 register is not explicitly provided in the structure (it's
56da0c48c4Sopenharmony_ci     value is always 0) and the resulting weird packing in the u_regs
57da0c48c4Sopenharmony_ci     array: the last element is not used.  */
58da0c48c4Sopenharmony_ci
59da0c48c4Sopenharmony_ci  struct pt_regs regs;
60da0c48c4Sopenharmony_ci  if (ptrace (PTRACE_GETREGS, tid, &regs, 0) == -1)
61da0c48c4Sopenharmony_ci    return false;
62da0c48c4Sopenharmony_ci
63da0c48c4Sopenharmony_ci  /* PC: no DWARF number  */
64da0c48c4Sopenharmony_ci  if (!setfunc (-1, 1, (Dwarf_Word *) &regs.tpc, arg))
65da0c48c4Sopenharmony_ci    return false;
66da0c48c4Sopenharmony_ci
67da0c48c4Sopenharmony_ci  /* Global registers: DWARF 0 .. 7  */
68da0c48c4Sopenharmony_ci  Dwarf_Word zero = 0;
69da0c48c4Sopenharmony_ci  if (!setfunc (0, 1, &zero, arg))
70da0c48c4Sopenharmony_ci    return false;
71da0c48c4Sopenharmony_ci  if (!setfunc (1, 7, (Dwarf_Word *) &regs.u_regs[0], arg))
72da0c48c4Sopenharmony_ci    return false;
73da0c48c4Sopenharmony_ci
74da0c48c4Sopenharmony_ci  /* Output registers: DWARF  8 .. 15  */
75da0c48c4Sopenharmony_ci  if (!setfunc (8, 8, (Dwarf_Word *) &regs.u_regs[7], arg))
76da0c48c4Sopenharmony_ci    return false;
77da0c48c4Sopenharmony_ci
78da0c48c4Sopenharmony_ci  /* Local and input registers must be read from the stack.  They are
79da0c48c4Sopenharmony_ci     saved in the previous stack frame.  The stack pointer is %o6,
80da0c48c4Sopenharmony_ci     read above.  */
81da0c48c4Sopenharmony_ci
82da0c48c4Sopenharmony_ci  Dwarf_Word locals_outs[16];
83da0c48c4Sopenharmony_ci  Dwarf_Word sp = regs.u_regs[13];
84da0c48c4Sopenharmony_ci
85da0c48c4Sopenharmony_ci  if (sp & 1)
86da0c48c4Sopenharmony_ci    {
87da0c48c4Sopenharmony_ci      /* Registers are 64 bits, and we need to apply the 2047 stack
88da0c48c4Sopenharmony_ci         bias in order to get the real stack pointer.  */
89da0c48c4Sopenharmony_ci
90da0c48c4Sopenharmony_ci      sp += 2047;
91da0c48c4Sopenharmony_ci
92da0c48c4Sopenharmony_ci      for (unsigned i = 0; i < 16; i++)
93da0c48c4Sopenharmony_ci        {
94da0c48c4Sopenharmony_ci          locals_outs[i] = ptrace (PTRACE_PEEKDATA, tid,
95da0c48c4Sopenharmony_ci                                   (void *) (uintptr_t) (sp + (i * 8)),
96da0c48c4Sopenharmony_ci                                   NULL);
97da0c48c4Sopenharmony_ci          if (errno != 0)
98da0c48c4Sopenharmony_ci            return false;
99da0c48c4Sopenharmony_ci        }
100da0c48c4Sopenharmony_ci    }
101da0c48c4Sopenharmony_ci  else
102da0c48c4Sopenharmony_ci    {
103da0c48c4Sopenharmony_ci      /* Registers are 32 bits.  */
104da0c48c4Sopenharmony_ci
105da0c48c4Sopenharmony_ci      for (unsigned i = 0; i < 8; i++)
106da0c48c4Sopenharmony_ci        {
107da0c48c4Sopenharmony_ci          Dwarf_Word tuple = ptrace (PTRACE_PEEKDATA, tid,
108da0c48c4Sopenharmony_ci                                     (void *) (uintptr_t) (sp + (i * 8)),
109da0c48c4Sopenharmony_ci                                     NULL);
110da0c48c4Sopenharmony_ci          if (errno != 0)
111da0c48c4Sopenharmony_ci            return false;
112da0c48c4Sopenharmony_ci
113da0c48c4Sopenharmony_ci          locals_outs[2*i] = (tuple >> 32) & 0xffffffff;
114da0c48c4Sopenharmony_ci          locals_outs[2*i+1] = tuple & 0xffffffff;
115da0c48c4Sopenharmony_ci        }
116da0c48c4Sopenharmony_ci    }
117da0c48c4Sopenharmony_ci
118da0c48c4Sopenharmony_ci
119da0c48c4Sopenharmony_ci  /* Local registers:  DWARF 16 .. 23 */
120da0c48c4Sopenharmony_ci  if (!setfunc (16, 8, &locals_outs[0], arg))
121da0c48c4Sopenharmony_ci    return false;
122da0c48c4Sopenharmony_ci
123da0c48c4Sopenharmony_ci  /* Input registers: DWARF 24 .. 31 */
124da0c48c4Sopenharmony_ci  if (!setfunc (24, 8, &locals_outs[8], arg))
125da0c48c4Sopenharmony_ci    return false;
126da0c48c4Sopenharmony_ci
127da0c48c4Sopenharmony_ci  return true;
128da0c48c4Sopenharmony_ci#endif
129da0c48c4Sopenharmony_ci}
130