]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-mips-low.c
Move savestring to common/common-utils.c, make gdbserver use it.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / linux-mips-low.c
CommitLineData
0a30fbc4 1/* GNU/Linux/MIPS specific low level interface, for the remote server for GDB.
28e7fd62 2 Copyright (C) 1995-2013 Free Software Foundation, Inc.
0a30fbc4
DJ
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
0a30fbc4
DJ
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0a30fbc4
DJ
18
19#include "server.h"
58caa3dc 20#include "linux-low.h"
0a30fbc4 21
21b0f40c 22#include <sys/ptrace.h>
186947f7 23#include <endian.h>
21b0f40c
DJ
24
25#include "gdb_proc_service.h"
26
d05b4ac3
UW
27/* Defined in auto-generated file mips-linux.c. */
28void init_registers_mips_linux (void);
1faeff08
MR
29/* Defined in auto-generated file mips-dsp-linux.c. */
30void init_registers_mips_dsp_linux (void);
d05b4ac3
UW
31/* Defined in auto-generated file mips64-linux.c. */
32void init_registers_mips64_linux (void);
1faeff08
MR
33/* Defined in auto-generated file mips64-dsp-linux.c. */
34void init_registers_mips64_dsp_linux (void);
35
36#ifdef __mips64
37#define init_registers_mips_linux init_registers_mips64_linux
38#define init_registers_mips_dsp_linux init_registers_mips64_dsp_linux
39#endif
d05b4ac3 40
21b0f40c
DJ
41#ifndef PTRACE_GET_THREAD_AREA
42#define PTRACE_GET_THREAD_AREA 25
43#endif
44
0a30fbc4
DJ
45#ifdef HAVE_SYS_REG_H
46#include <sys/reg.h>
47#endif
48
117ce543 49#define mips_num_regs 73
1faeff08 50#define mips_dsp_num_regs 80
0a30fbc4
DJ
51
52#include <asm/ptrace.h>
53
1faeff08
MR
54#ifndef DSP_BASE
55#define DSP_BASE 71
56#define DSP_CONTROL 77
57#endif
58
186947f7
DJ
59union mips_register
60{
61 unsigned char buf[8];
62
63 /* Deliberately signed, for proper sign extension. */
64 int reg32;
65 long long reg64;
66};
67
0a30fbc4
DJ
68/* Return the ptrace ``address'' of register REGNO. */
69
1faeff08
MR
70#define mips_base_regs \
71 -1, 1, 2, 3, 4, 5, 6, 7, \
72 8, 9, 10, 11, 12, 13, 14, 15, \
73 16, 17, 18, 19, 20, 21, 22, 23, \
74 24, 25, 26, 27, 28, 29, 30, 31, \
75 \
76 -1, MMLO, MMHI, BADVADDR, CAUSE, PC, \
77 \
78 FPR_BASE, FPR_BASE + 1, FPR_BASE + 2, FPR_BASE + 3, \
79 FPR_BASE + 4, FPR_BASE + 5, FPR_BASE + 6, FPR_BASE + 7, \
80 FPR_BASE + 8, FPR_BASE + 9, FPR_BASE + 10, FPR_BASE + 11, \
81 FPR_BASE + 12, FPR_BASE + 13, FPR_BASE + 14, FPR_BASE + 15, \
82 FPR_BASE + 16, FPR_BASE + 17, FPR_BASE + 18, FPR_BASE + 19, \
83 FPR_BASE + 20, FPR_BASE + 21, FPR_BASE + 22, FPR_BASE + 23, \
84 FPR_BASE + 24, FPR_BASE + 25, FPR_BASE + 26, FPR_BASE + 27, \
85 FPR_BASE + 28, FPR_BASE + 29, FPR_BASE + 30, FPR_BASE + 31, \
86 FPC_CSR, FPC_EIR
87
88#define mips_dsp_regs \
89 DSP_BASE, DSP_BASE + 1, DSP_BASE + 2, DSP_BASE + 3, \
90 DSP_BASE + 4, DSP_BASE + 5, \
91 DSP_CONTROL
92
93static int mips_regmap[mips_num_regs] = {
94 mips_base_regs,
95 0
96};
0a30fbc4 97
1faeff08
MR
98static int mips_dsp_regmap[mips_dsp_num_regs] = {
99 mips_base_regs,
100 mips_dsp_regs,
101 0
102};
0a30fbc4 103
1faeff08
MR
104/* DSP registers are not in any regset and can only be accessed
105 individually. */
0a30fbc4 106
1faeff08
MR
107static unsigned char mips_dsp_regset_bitmap[(mips_dsp_num_regs + 7) / 8] = {
108 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x80
0a30fbc4
DJ
109};
110
1faeff08
MR
111/* Try peeking at an arbitrarily chosen DSP register and pick the available
112 user register set accordingly. */
113
114static void
115mips_arch_setup (void)
116{
117 static void (*init_registers) (void);
118
119 gdb_assert (current_inferior);
120
121 if (init_registers == NULL)
122 {
123 int pid = lwpid_of (get_thread_lwp (current_inferior));
124
125 ptrace (PTRACE_PEEKUSER, pid, DSP_CONTROL, 0);
126 switch (errno)
127 {
128 case 0:
129 the_low_target.num_regs = mips_dsp_num_regs;
130 the_low_target.regmap = mips_dsp_regmap;
131 the_low_target.regset_bitmap = mips_dsp_regset_bitmap;
132 init_registers = init_registers_mips_dsp_linux;
133 break;
134 case EIO:
135 the_low_target.num_regs = mips_num_regs;
136 the_low_target.regmap = mips_regmap;
137 the_low_target.regset_bitmap = NULL;
138 init_registers = init_registers_mips_linux;
139 break;
140 default:
141 perror_with_name ("ptrace");
142 break;
143 }
144 }
145 init_registers ();
146}
147
0a30fbc4
DJ
148/* From mips-linux-nat.c. */
149
150/* Pseudo registers can not be read. ptrace does not provide a way to
151 read (or set) PS_REGNUM, and there's no point in reading or setting
152 ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or FCRIR via
153 ptrace(). */
154
2ec06d2e
DJ
155static int
156mips_cannot_fetch_register (int regno)
0a30fbc4 157{
1faeff08 158 if (the_low_target.regmap[regno] == -1)
0a30fbc4
DJ
159 return 1;
160
117ce543 161 if (find_regno ("r0") == regno)
0a30fbc4
DJ
162 return 1;
163
164 return 0;
165}
166
2ec06d2e
DJ
167static int
168mips_cannot_store_register (int regno)
0a30fbc4 169{
1faeff08 170 if (the_low_target.regmap[regno] == -1)
0a30fbc4
DJ
171 return 1;
172
117ce543 173 if (find_regno ("r0") == regno)
0a30fbc4
DJ
174 return 1;
175
1d33e73a 176 if (find_regno ("cause") == regno)
0a30fbc4
DJ
177 return 1;
178
117ce543 179 if (find_regno ("badvaddr") == regno)
0a30fbc4
DJ
180 return 1;
181
1d33e73a 182 if (find_regno ("fir") == regno)
0a30fbc4
DJ
183 return 1;
184
185 return 0;
186}
2ec06d2e 187
0d62e5e8 188static CORE_ADDR
442ea881 189mips_get_pc (struct regcache *regcache)
0d62e5e8 190{
186947f7 191 union mips_register pc;
442ea881 192 collect_register_by_name (regcache, "pc", pc.buf);
186947f7 193 return register_size (0) == 4 ? pc.reg32 : pc.reg64;
0d62e5e8
DJ
194}
195
196static void
442ea881 197mips_set_pc (struct regcache *regcache, CORE_ADDR pc)
0d62e5e8 198{
186947f7
DJ
199 union mips_register newpc;
200 if (register_size (0) == 4)
201 newpc.reg32 = pc;
202 else
203 newpc.reg64 = pc;
204
442ea881 205 supply_register_by_name (regcache, "pc", newpc.buf);
0d62e5e8
DJ
206}
207
208/* Correct in either endianness. */
186947f7 209static const unsigned int mips_breakpoint = 0x0005000d;
0d62e5e8
DJ
210#define mips_breakpoint_len 4
211
212/* We only place breakpoints in empty marker functions, and thread locking
213 is outside of the function. So rather than importing software single-step,
214 we can just run until exit. */
215static CORE_ADDR
442ea881 216mips_reinsert_addr (void)
0d62e5e8 217{
442ea881 218 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
186947f7 219 union mips_register ra;
442ea881 220 collect_register_by_name (regcache, "r31", ra.buf);
186947f7 221 return register_size (0) == 4 ? ra.reg32 : ra.reg64;
0d62e5e8
DJ
222}
223
224static int
225mips_breakpoint_at (CORE_ADDR where)
226{
186947f7 227 unsigned int insn;
0d62e5e8 228
f450004a 229 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
0d62e5e8
DJ
230 if (insn == mips_breakpoint)
231 return 1;
232
233 /* If necessary, recognize more trap instructions here. GDB only uses the
234 one. */
235 return 0;
236}
237
21b0f40c
DJ
238/* Fetch the thread-local storage pointer for libthread_db. */
239
240ps_err_e
241ps_get_thread_area (const struct ps_prochandle *ph,
1b3f6016 242 lwpid_t lwpid, int idx, void **base)
21b0f40c
DJ
243{
244 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
245 return PS_ERR;
246
247 /* IDX is the bias from the thread pointer to the beginning of the
248 thread descriptor. It has to be subtracted due to implementation
249 quirks in libthread_db. */
250 *base = (void *) ((char *)*base - idx);
251
252 return PS_OK;
253}
254
186947f7
DJ
255#ifdef HAVE_PTRACE_GETREGS
256
257static void
442ea881
PA
258mips_collect_register (struct regcache *regcache,
259 int use_64bit, int regno, union mips_register *reg)
186947f7
DJ
260{
261 union mips_register tmp_reg;
262
263 if (use_64bit)
264 {
442ea881 265 collect_register (regcache, regno, &tmp_reg.reg64);
186947f7
DJ
266 *reg = tmp_reg;
267 }
268 else
269 {
442ea881 270 collect_register (regcache, regno, &tmp_reg.reg32);
186947f7
DJ
271 reg->reg64 = tmp_reg.reg32;
272 }
273}
274
275static void
442ea881
PA
276mips_supply_register (struct regcache *regcache,
277 int use_64bit, int regno, const union mips_register *reg)
186947f7
DJ
278{
279 int offset = 0;
280
281 /* For big-endian 32-bit targets, ignore the high four bytes of each
282 eight-byte slot. */
283 if (__BYTE_ORDER == __BIG_ENDIAN && !use_64bit)
284 offset = 4;
285
442ea881 286 supply_register (regcache, regno, reg->buf + offset);
186947f7
DJ
287}
288
289static void
442ea881
PA
290mips_collect_register_32bit (struct regcache *regcache,
291 int use_64bit, int regno, unsigned char *buf)
186947f7
DJ
292{
293 union mips_register tmp_reg;
294 int reg32;
295
442ea881 296 mips_collect_register (regcache, use_64bit, regno, &tmp_reg);
186947f7
DJ
297 reg32 = tmp_reg.reg64;
298 memcpy (buf, &reg32, 4);
299}
300
301static void
442ea881
PA
302mips_supply_register_32bit (struct regcache *regcache,
303 int use_64bit, int regno, const unsigned char *buf)
186947f7
DJ
304{
305 union mips_register tmp_reg;
306 int reg32;
307
308 memcpy (&reg32, buf, 4);
309 tmp_reg.reg64 = reg32;
442ea881 310 mips_supply_register (regcache, use_64bit, regno, &tmp_reg);
186947f7
DJ
311}
312
313static void
442ea881 314mips_fill_gregset (struct regcache *regcache, void *buf)
186947f7
DJ
315{
316 union mips_register *regset = buf;
317 int i, use_64bit;
318
319 use_64bit = (register_size (0) == 8);
320
117ce543 321 for (i = 1; i < 32; i++)
442ea881
PA
322 mips_collect_register (regcache, use_64bit, i, regset + i);
323
324 mips_collect_register (regcache, use_64bit,
325 find_regno ("lo"), regset + 32);
326 mips_collect_register (regcache, use_64bit,
327 find_regno ("hi"), regset + 33);
328 mips_collect_register (regcache, use_64bit,
329 find_regno ("pc"), regset + 34);
330 mips_collect_register (regcache, use_64bit,
331 find_regno ("badvaddr"), regset + 35);
332 mips_collect_register (regcache, use_64bit,
333 find_regno ("status"), regset + 36);
334 mips_collect_register (regcache, use_64bit,
335 find_regno ("cause"), regset + 37);
336
337 mips_collect_register (regcache, use_64bit,
338 find_regno ("restart"), regset + 0);
186947f7
DJ
339}
340
341static void
442ea881 342mips_store_gregset (struct regcache *regcache, const void *buf)
186947f7
DJ
343{
344 const union mips_register *regset = buf;
345 int i, use_64bit;
346
347 use_64bit = (register_size (0) == 8);
348
349 for (i = 0; i < 32; i++)
442ea881
PA
350 mips_supply_register (regcache, use_64bit, i, regset + i);
351
352 mips_supply_register (regcache, use_64bit, find_regno ("lo"), regset + 32);
353 mips_supply_register (regcache, use_64bit, find_regno ("hi"), regset + 33);
354 mips_supply_register (regcache, use_64bit, find_regno ("pc"), regset + 34);
355 mips_supply_register (regcache, use_64bit,
356 find_regno ("badvaddr"), regset + 35);
357 mips_supply_register (regcache, use_64bit,
358 find_regno ("status"), regset + 36);
359 mips_supply_register (regcache, use_64bit,
360 find_regno ("cause"), regset + 37);
361
362 mips_supply_register (regcache, use_64bit,
363 find_regno ("restart"), regset + 0);
186947f7
DJ
364}
365
366static void
442ea881 367mips_fill_fpregset (struct regcache *regcache, void *buf)
186947f7
DJ
368{
369 union mips_register *regset = buf;
370 int i, use_64bit, first_fp, big_endian;
371
372 use_64bit = (register_size (0) == 8);
373 first_fp = find_regno ("f0");
374 big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
375
376 /* See GDB for a discussion of this peculiar layout. */
377 for (i = 0; i < 32; i++)
378 if (use_64bit)
442ea881 379 collect_register (regcache, first_fp + i, regset[i].buf);
186947f7 380 else
442ea881 381 collect_register (regcache, first_fp + i,
186947f7
DJ
382 regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
383
442ea881
PA
384 mips_collect_register_32bit (regcache, use_64bit,
385 find_regno ("fcsr"), regset[32].buf);
386 mips_collect_register_32bit (regcache, use_64bit, find_regno ("fir"),
186947f7
DJ
387 regset[32].buf + 4);
388}
389
390static void
442ea881 391mips_store_fpregset (struct regcache *regcache, const void *buf)
186947f7
DJ
392{
393 const union mips_register *regset = buf;
394 int i, use_64bit, first_fp, big_endian;
395
396 use_64bit = (register_size (0) == 8);
397 first_fp = find_regno ("f0");
398 big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
399
400 /* See GDB for a discussion of this peculiar layout. */
401 for (i = 0; i < 32; i++)
402 if (use_64bit)
442ea881 403 supply_register (regcache, first_fp + i, regset[i].buf);
186947f7 404 else
442ea881 405 supply_register (regcache, first_fp + i,
186947f7
DJ
406 regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
407
442ea881
PA
408 mips_supply_register_32bit (regcache, use_64bit,
409 find_regno ("fcsr"), regset[32].buf);
410 mips_supply_register_32bit (regcache, use_64bit, find_regno ("fir"),
186947f7
DJ
411 regset[32].buf + 4);
412}
413#endif /* HAVE_PTRACE_GETREGS */
414
415struct regset_info target_regsets[] = {
416#ifdef HAVE_PTRACE_GETREGS
1570b33e 417 { PTRACE_GETREGS, PTRACE_SETREGS, 0, 38 * 8, GENERAL_REGS,
186947f7 418 mips_fill_gregset, mips_store_gregset },
1570b33e 419 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, 33 * 8, FP_REGS,
186947f7
DJ
420 mips_fill_fpregset, mips_store_fpregset },
421#endif /* HAVE_PTRACE_GETREGS */
1570b33e 422 { 0, 0, 0, -1, -1, NULL, NULL }
186947f7
DJ
423};
424
2ec06d2e 425struct linux_target_ops the_low_target = {
1faeff08
MR
426 mips_arch_setup,
427 -1,
428 NULL,
429 NULL,
2ec06d2e
DJ
430 mips_cannot_fetch_register,
431 mips_cannot_store_register,
c14dfd32 432 NULL, /* fetch_register */
0d62e5e8
DJ
433 mips_get_pc,
434 mips_set_pc,
f450004a 435 (const unsigned char *) &mips_breakpoint,
0d62e5e8
DJ
436 mips_breakpoint_len,
437 mips_reinsert_addr,
438 0,
439 mips_breakpoint_at,
2ec06d2e 440};