Automatic date update in version.in
[platform/upstream/binutils.git] / gdb / tilegx-linux-nat.c
1 /* Native-dependent code for GNU/Linux TILE-Gx.
2
3    Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "gdbcore.h"
23 #include "regcache.h"
24 #include "linux-nat.h"
25
26 #include <sys/ptrace.h>
27
28 #include <sys/procfs.h>
29
30 /* Defines ps_err_e, struct ps_prochandle.  */
31 #include "gdb_proc_service.h"
32
33 /* Prototypes for supply_gregset etc.  */
34 #include "gregset.h"
35
36 /* The register sets used in GNU/Linux ELF core-dumps are identical to
37    the register sets in `struct user' that is used for a.out
38    core-dumps, and is also used by `ptrace'.  The corresponding types
39    are `elf_gregset_t' for the general-purpose registers (with
40    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
41    for the floating-point registers.
42
43    Those types used to be available under the names `gregset_t' and
44    `fpregset_t' too, and this file used those names in the past.  But
45    those names are now used for the register sets used in the
46    `mcontext_t' type, and have a different size and layout.  */
47
48 /* Mapping between the general-purpose registers in `struct user'
49    format and GDB's register array layout.  Note that we map the
50    first 56 registers (0 thru 55) one-to-one.  GDB maps the pc to
51    slot 64, but ptrace returns it in slot 56.  */
52 static const int regmap[] =
53 {
54    0,  1,  2,  3,  4,  5,  6,  7,
55    8,  9, 10, 11, 12, 13, 14, 15,
56   16, 17, 18, 19, 20, 21, 22, 23,
57   24, 25, 26, 27, 28, 29, 30, 31,
58   32, 33, 34, 35, 36, 37, 38, 39,
59   40, 41, 42, 43, 44, 45, 46, 47,
60   48, 49, 50, 51, 52, 53, 54, 55,
61   -1, -1, -1, -1, -1, -1, -1, -1,
62   56, 58
63 };
64
65 /* Transfering the general-purpose registers between GDB, inferiors
66    and core files.  */
67
68 /* Fill GDB's register array with the general-purpose register values
69    in *GREGSETP.  */
70
71 void
72 supply_gregset (struct regcache* regcache,
73                 const elf_gregset_t *gregsetp)
74 {
75   elf_greg_t *regp = (elf_greg_t *) gregsetp;
76   int i;
77
78   for (i = 0; i < sizeof (regmap) / sizeof (regmap[0]); i++)
79     if (regmap[i] >= 0)
80       regcache_raw_supply (regcache, i, regp + regmap[i]);
81 }
82
83 /* Fill registers in *GREGSETPS with the values in GDB's
84    register array.  */
85
86 void
87 fill_gregset (const struct regcache* regcache,
88               elf_gregset_t *gregsetp, int regno)
89 {
90   elf_greg_t *regp = (elf_greg_t *) gregsetp;
91   int i;
92
93   for (i = 0; i < sizeof (regmap) / sizeof (regmap[0]); i++)
94     if (regmap[i] >= 0)
95       regcache_raw_collect (regcache, i, regp + regmap[i]);
96 }
97
98 /* Transfering floating-point registers between GDB, inferiors and cores.  */
99
100 /* Fill GDB's register array with the floating-point register values in
101    *FPREGSETP.  */
102
103 void
104 supply_fpregset (struct regcache *regcache,
105                  const elf_fpregset_t *fpregsetp)
106 {
107   /* NOTE: There are no floating-point registers for TILE-Gx.  */
108 }
109
110 /* Fill register REGNO (if it is a floating-point register) in
111    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
112    do this for all registers.  */
113
114 void
115 fill_fpregset (const struct regcache *regcache,
116                elf_fpregset_t *fpregsetp, int regno)
117 {
118   /* NOTE: There are no floating-point registers for TILE-Gx.  */
119 }
120
121 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
122    for all registers.  */
123
124 static void
125 fetch_inferior_registers (struct target_ops *ops,
126                           struct regcache *regcache, int regnum)
127 {
128   elf_gregset_t regs;
129   int tid;
130
131   tid = ptid_get_lwp (inferior_ptid);
132   if (tid == 0)
133     tid = ptid_get_pid (inferior_ptid);
134
135   if (ptrace (PTRACE_GETREGS, tid, 0, (PTRACE_TYPE_ARG3) &regs) < 0)
136     perror_with_name (_("Couldn't get registers"));
137
138   supply_gregset (regcache, (const elf_gregset_t *)&regs);
139 }
140
141 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
142    this for all registers.  */
143
144 static void
145 store_inferior_registers (struct target_ops *ops,
146                           struct regcache *regcache, int regnum)
147 {
148   elf_gregset_t regs;
149   int tid;
150
151   tid = ptid_get_lwp (inferior_ptid);
152   if (tid == 0)
153     tid = ptid_get_pid (inferior_ptid);
154
155   if (ptrace (PTRACE_GETREGS, tid, 0, (PTRACE_TYPE_ARG3) &regs) < 0)
156     perror_with_name (_("Couldn't get registers"));
157
158   fill_gregset (regcache, &regs, regnum);
159
160   if (ptrace (PTRACE_SETREGS, tid, 0, (PTRACE_TYPE_ARG3) &regs) < 0)
161     perror_with_name (_("Couldn't write registers"));
162 }
163
164
165 extern initialize_file_ftype _initialize_tile_linux_nat;
166
167 void
168 _initialize_tile_linux_nat (void)
169 {
170   struct target_ops *t;
171
172   /* Fill in the generic GNU/Linux methods.  */
173   t = linux_target ();
174
175   /* Add our register access methods.  */
176   t->to_fetch_registers = fetch_inferior_registers;
177   t->to_store_registers = store_inferior_registers;
178
179   /* Register the target.  */
180   linux_nat_add_target (t);
181 }