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