]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/linux-nios2-low.c
Update year range in copyright notice of all files owned by the GDB project.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / linux-nios2-low.c
1 /* GNU/Linux/Nios II specific low level interface, for the remote server for
2 GDB.
3 Copyright (C) 2008-2015 Free Software Foundation, Inc.
4
5 Contributed by Mentor Graphics, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "server.h"
23 #include "linux-low.h"
24 #include <sys/ptrace.h>
25 #include <endian.h>
26 #include "gdb_proc_service.h"
27 #include <asm/ptrace.h>
28
29 #ifndef PTRACE_GET_THREAD_AREA
30 #define PTRACE_GET_THREAD_AREA 25
31 #endif
32
33 /* The following definition must agree with the number of registers
34 defined in "struct user_regs" in GLIBC
35 (ports/sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
36 NIOS2_NUM_REGS in GDB proper. */
37
38 #define nios2_num_regs 49
39
40 /* Defined in auto-generated file nios2-linux.c. */
41
42 void init_registers_nios2_linux (void);
43 extern const struct target_desc *tdesc_nios2_linux;
44
45 /* This union is used to convert between int and byte buffer
46 representations of register contents. */
47
48 union nios2_register
49 {
50 unsigned char buf[4];
51 int reg32;
52 };
53
54 /* Return the ptrace ``address'' of register REGNO. */
55
56 static int nios2_regmap[] = {
57 -1, 1, 2, 3, 4, 5, 6, 7,
58 8, 9, 10, 11, 12, 13, 14, 15,
59 16, 17, 18, 19, 20, 21, 22, 23,
60 24, 25, 26, 27, 28, 29, 30, 31,
61 32, 33, 34, 35, 36, 37, 38, 39,
62 40, 41, 42, 43, 44, 45, 46, 47,
63 48,
64 0
65 };
66
67 /* Implement the arch_setup linux_target_ops method. */
68
69 static void
70 nios2_arch_setup (void)
71 {
72 current_process ()->tdesc = tdesc_nios2_linux;
73 }
74
75 /* Implement the cannot_fetch_register linux_target_ops method. */
76
77 static int
78 nios2_cannot_fetch_register (int regno)
79 {
80 if (nios2_regmap[regno] == -1)
81 return 1;
82
83 return 0;
84 }
85
86 /* Implement the cannot_store_register linux_target_ops method. */
87
88 static int
89 nios2_cannot_store_register (int regno)
90 {
91 if (nios2_regmap[regno] == -1)
92 return 1;
93
94 return 0;
95 }
96
97 /* Implement the get_pc linux_target_ops method. */
98
99 static CORE_ADDR
100 nios2_get_pc (struct regcache *regcache)
101 {
102 union nios2_register pc;
103
104 collect_register_by_name (regcache, "pc", pc.buf);
105 return pc.reg32;
106 }
107
108 /* Implement the set_pc linux_target_ops method. */
109
110 static void
111 nios2_set_pc (struct regcache *regcache, CORE_ADDR pc)
112 {
113 union nios2_register newpc;
114
115 newpc.reg32 = pc;
116 supply_register_by_name (regcache, "pc", newpc.buf);
117 }
118
119 /* Breakpoint support. */
120
121 static const unsigned int nios2_breakpoint = 0x003b6ffa;
122 #define nios2_breakpoint_len 4
123
124 /* Implement the breakpoint_reinsert_addr linux_target_ops method. */
125
126 static CORE_ADDR
127 nios2_reinsert_addr (void)
128 {
129 union nios2_register ra;
130 struct regcache *regcache = get_thread_regcache (current_thread, 1);
131
132 collect_register_by_name (regcache, "ra", ra.buf);
133 return ra.reg32;
134 }
135
136 /* Implement the breakpoint_at linux_target_ops method. */
137
138 static int
139 nios2_breakpoint_at (CORE_ADDR where)
140 {
141 unsigned int insn;
142
143 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
144 if (insn == nios2_breakpoint)
145 return 1;
146 return 0;
147 }
148
149 /* Fetch the thread-local storage pointer for libthread_db. */
150
151 ps_err_e
152 ps_get_thread_area (const struct ps_prochandle *ph,
153 lwpid_t lwpid, int idx, void **base)
154 {
155 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
156 return PS_ERR;
157
158 /* IDX is the bias from the thread pointer to the beginning of the
159 thread descriptor. It has to be subtracted due to implementation
160 quirks in libthread_db. */
161 *base = (void *) ((char *) *base - idx);
162
163 return PS_OK;
164 }
165
166 #ifdef HAVE_PTRACE_GETREGS
167
168 /* Helper functions to collect/supply a single register REGNO. */
169
170 static void
171 nios2_collect_register (struct regcache *regcache, int regno,
172 union nios2_register *reg)
173 {
174 union nios2_register tmp_reg;
175
176 collect_register (regcache, regno, &tmp_reg.reg32);
177 reg->reg32 = tmp_reg.reg32;
178 }
179
180 static void
181 nios2_supply_register (struct regcache *regcache, int regno,
182 const union nios2_register *reg)
183 {
184 supply_register (regcache, regno, reg->buf);
185 }
186
187 /* We have only a single register set on Nios II. */
188
189 static void
190 nios2_fill_gregset (struct regcache *regcache, void *buf)
191 {
192 union nios2_register *regset = buf;
193 int i;
194
195 for (i = 1; i < nios2_num_regs; i++)
196 nios2_collect_register (regcache, i, regset + i);
197 }
198
199 static void
200 nios2_store_gregset (struct regcache *regcache, const void *buf)
201 {
202 const union nios2_register *regset = buf;
203 int i;
204
205 for (i = 0; i < nios2_num_regs; i++)
206 nios2_supply_register (regcache, i, regset + i);
207 }
208 #endif /* HAVE_PTRACE_GETREGS */
209
210 static struct regset_info nios2_regsets[] =
211 {
212 #ifdef HAVE_PTRACE_GETREGS
213 { PTRACE_GETREGS, PTRACE_SETREGS, 0, nios2_num_regs * 4, GENERAL_REGS,
214 nios2_fill_gregset, nios2_store_gregset },
215 #endif /* HAVE_PTRACE_GETREGS */
216 { 0, 0, 0, -1, -1, NULL, NULL }
217 };
218
219 static struct regsets_info nios2_regsets_info =
220 {
221 nios2_regsets, /* regsets */
222 0, /* num_regsets */
223 NULL, /* disabled_regsets */
224 };
225
226 static struct usrregs_info nios2_usrregs_info =
227 {
228 nios2_num_regs,
229 nios2_regmap,
230 };
231
232 static struct regs_info regs_info =
233 {
234 NULL, /* regset_bitmap */
235 &nios2_usrregs_info,
236 &nios2_regsets_info
237 };
238
239 static const struct regs_info *
240 nios2_regs_info (void)
241 {
242 return &regs_info;
243 }
244
245 struct linux_target_ops the_low_target =
246 {
247 nios2_arch_setup,
248 nios2_regs_info,
249 nios2_cannot_fetch_register,
250 nios2_cannot_store_register,
251 NULL,
252 nios2_get_pc,
253 nios2_set_pc,
254 (const unsigned char *) &nios2_breakpoint,
255 nios2_breakpoint_len,
256 nios2_reinsert_addr,
257 0,
258 nios2_breakpoint_at,
259 };
260
261 void
262 initialize_low_arch (void)
263 {
264 init_registers_nios2_linux ();
265
266 initialize_regsets_info (&nios2_regsets_info);
267 }