2011-01-08 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / m68klinux-nat.c
1 /* Motorola m68k native support for GNU/Linux.
2
3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "language.h"
25 #include "gdbcore.h"
26 #include "gdb_string.h"
27 #include "regcache.h"
28 #include "target.h"
29 #include "linux-nat.h"
30
31 #include "m68k-tdep.h"
32
33 #include <sys/param.h>
34 #include <sys/dir.h>
35 #include <signal.h>
36 #include <sys/ptrace.h>
37 #include <sys/user.h>
38 #include <sys/ioctl.h>
39 #include <fcntl.h>
40 #include <sys/procfs.h>
41
42 #ifdef HAVE_SYS_REG_H
43 #include <sys/reg.h>
44 #endif
45
46 #include <sys/file.h>
47 #include "gdb_stat.h"
48
49 #include "floatformat.h"
50
51 #include "target.h"
52
53 /* Prototypes for supply_gregset etc. */
54 #include "gregset.h"
55 \f
56 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
57 static const int regmap[] =
58 {
59 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
60 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
61 PT_SR, PT_PC,
62 /* PT_FP0, ..., PT_FP7 */
63 21, 24, 27, 30, 33, 36, 39, 42,
64 /* PT_FPCR, PT_FPSR, PT_FPIAR */
65 45, 46, 47
66 };
67
68 /* Which ptrace request retrieves which registers?
69 These apply to the corresponding SET requests as well. */
70 #define NUM_GREGS (18)
71 #define MAX_NUM_REGS (NUM_GREGS + 11)
72
73 int
74 getregs_supplies (int regno)
75 {
76 return 0 <= regno && regno < NUM_GREGS;
77 }
78
79 int
80 getfpregs_supplies (int regno)
81 {
82 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
83 }
84
85 /* Does the current host support the GETREGS request? */
86 int have_ptrace_getregs =
87 #ifdef HAVE_PTRACE_GETREGS
88 1
89 #else
90 0
91 #endif
92 ;
93
94 \f
95
96 /* Fetching registers directly from the U area, one at a time. */
97
98 /* Fetch one register. */
99
100 static void
101 fetch_register (struct regcache *regcache, int regno)
102 {
103 struct gdbarch *gdbarch = get_regcache_arch (regcache);
104 long regaddr;
105 int i;
106 char buf[MAX_REGISTER_SIZE];
107 int tid;
108
109 /* Overload thread id onto process id. */
110 tid = TIDGET (inferior_ptid);
111 if (tid == 0)
112 tid = PIDGET (inferior_ptid); /* no thread id, just use
113 process id. */
114
115 regaddr = 4 * regmap[regno];
116 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
117 {
118 errno = 0;
119 *(long *) &buf[i] = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
120 regaddr += sizeof (long);
121 if (errno != 0)
122 error (_("Couldn't read register %s (#%d): %s."),
123 gdbarch_register_name (gdbarch, regno),
124 regno, safe_strerror (errno));
125 }
126 regcache_raw_supply (regcache, regno, buf);
127 }
128
129 /* Fetch register values from the inferior.
130 If REGNO is negative, do this for all registers.
131 Otherwise, REGNO specifies which register (so we can save time). */
132
133 static void
134 old_fetch_inferior_registers (struct regcache *regcache, int regno)
135 {
136 if (regno >= 0)
137 {
138 fetch_register (regcache, regno);
139 }
140 else
141 {
142 for (regno = 0;
143 regno < gdbarch_num_regs (get_regcache_arch (regcache));
144 regno++)
145 {
146 fetch_register (regcache, regno);
147 }
148 }
149 }
150
151 /* Store one register. */
152
153 static void
154 store_register (const struct regcache *regcache, int regno)
155 {
156 struct gdbarch *gdbarch = get_regcache_arch (regcache);
157 long regaddr;
158 int i;
159 int tid;
160 char buf[MAX_REGISTER_SIZE];
161
162 /* Overload thread id onto process id. */
163 tid = TIDGET (inferior_ptid);
164 if (tid == 0)
165 tid = PIDGET (inferior_ptid); /* no thread id, just use
166 process id. */
167
168 regaddr = 4 * regmap[regno];
169
170 /* Put the contents of regno into a local buffer. */
171 regcache_raw_collect (regcache, regno, buf);
172
173 /* Store the local buffer into the inferior a chunk at the time. */
174 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
175 {
176 errno = 0;
177 ptrace (PTRACE_POKEUSER, tid, regaddr, *(long *) &buf[i]);
178 regaddr += sizeof (long);
179 if (errno != 0)
180 error (_("Couldn't write register %s (#%d): %s."),
181 gdbarch_register_name (gdbarch, regno),
182 regno, safe_strerror (errno));
183 }
184 }
185
186 /* Store our register values back into the inferior.
187 If REGNO is negative, do this for all registers.
188 Otherwise, REGNO specifies which register (so we can save time). */
189
190 static void
191 old_store_inferior_registers (const struct regcache *regcache, int regno)
192 {
193 if (regno >= 0)
194 {
195 store_register (regcache, regno);
196 }
197 else
198 {
199 for (regno = 0;
200 regno < gdbarch_num_regs (get_regcache_arch (regcache));
201 regno++)
202 {
203 store_register (regcache, regno);
204 }
205 }
206 }
207 \f
208 /* Given a pointer to a general register set in /proc format
209 (elf_gregset_t *), unpack the register contents and supply
210 them as gdb's idea of the current register values. */
211
212 void
213 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
214 {
215 struct gdbarch *gdbarch = get_regcache_arch (regcache);
216 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
217 int regi;
218
219 for (regi = M68K_D0_REGNUM;
220 regi <= gdbarch_sp_regnum (gdbarch);
221 regi++)
222 regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
223 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
224 &regp[PT_SR]);
225 regcache_raw_supply (regcache,
226 gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
227 }
228
229 /* Fill register REGNO (if it is a general-purpose register) in
230 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
231 do this for all registers. */
232 void
233 fill_gregset (const struct regcache *regcache,
234 elf_gregset_t *gregsetp, int regno)
235 {
236 elf_greg_t *regp = (elf_greg_t *) gregsetp;
237 int i;
238
239 for (i = 0; i < NUM_GREGS; i++)
240 if (regno == -1 || regno == i)
241 regcache_raw_collect (regcache, i, regp + regmap[i]);
242 }
243
244 #ifdef HAVE_PTRACE_GETREGS
245
246 /* Fetch all general-purpose registers from process/thread TID and
247 store their values in GDB's register array. */
248
249 static void
250 fetch_regs (struct regcache *regcache, int tid)
251 {
252 elf_gregset_t regs;
253
254 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
255 {
256 if (errno == EIO)
257 {
258 /* The kernel we're running on doesn't support the GETREGS
259 request. Reset `have_ptrace_getregs'. */
260 have_ptrace_getregs = 0;
261 return;
262 }
263
264 perror_with_name (_("Couldn't get registers"));
265 }
266
267 supply_gregset (regcache, (const elf_gregset_t *) &regs);
268 }
269
270 /* Store all valid general-purpose registers in GDB's register array
271 into the process/thread specified by TID. */
272
273 static void
274 store_regs (const struct regcache *regcache, int tid, int regno)
275 {
276 elf_gregset_t regs;
277
278 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
279 perror_with_name (_("Couldn't get registers"));
280
281 fill_gregset (regcache, &regs, regno);
282
283 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
284 perror_with_name (_("Couldn't write registers"));
285 }
286
287 #else
288
289 static void fetch_regs (struct regcache *regcache, int tid)
290 {
291 }
292
293 static void store_regs (const struct regcache *regcache, int tid, int regno)
294 {
295 }
296
297 #endif
298
299 \f
300 /* Transfering floating-point registers between GDB, inferiors and cores. */
301
302 /* What is the address of fpN within the floating-point register set F? */
303 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
304
305 /* Fill GDB's register array with the floating-point register values in
306 *FPREGSETP. */
307
308 void
309 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
310 {
311 struct gdbarch *gdbarch = get_regcache_arch (regcache);
312 int regi;
313
314 for (regi = gdbarch_fp0_regnum (gdbarch);
315 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
316 regcache_raw_supply (regcache, regi,
317 FPREG_ADDR (fpregsetp,
318 regi - gdbarch_fp0_regnum (gdbarch)));
319 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
320 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
321 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
322 }
323
324 /* Fill register REGNO (if it is a floating-point register) in
325 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
326 do this for all registers. */
327
328 void
329 fill_fpregset (const struct regcache *regcache,
330 elf_fpregset_t *fpregsetp, int regno)
331 {
332 struct gdbarch *gdbarch = get_regcache_arch (regcache);
333 int i;
334
335 /* Fill in the floating-point registers. */
336 for (i = gdbarch_fp0_regnum (gdbarch);
337 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
338 if (regno == -1 || regno == i)
339 regcache_raw_collect (regcache, i,
340 FPREG_ADDR (fpregsetp,
341 i - gdbarch_fp0_regnum (gdbarch)));
342
343 /* Fill in the floating-point control registers. */
344 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
345 if (regno == -1 || regno == i)
346 regcache_raw_collect (regcache, i,
347 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
348 }
349
350 #ifdef HAVE_PTRACE_GETREGS
351
352 /* Fetch all floating-point registers from process/thread TID and store
353 thier values in GDB's register array. */
354
355 static void
356 fetch_fpregs (struct regcache *regcache, int tid)
357 {
358 elf_fpregset_t fpregs;
359
360 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
361 perror_with_name (_("Couldn't get floating point status"));
362
363 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
364 }
365
366 /* Store all valid floating-point registers in GDB's register array
367 into the process/thread specified by TID. */
368
369 static void
370 store_fpregs (const struct regcache *regcache, int tid, int regno)
371 {
372 elf_fpregset_t fpregs;
373
374 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
375 perror_with_name (_("Couldn't get floating point status"));
376
377 fill_fpregset (regcache, &fpregs, regno);
378
379 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
380 perror_with_name (_("Couldn't write floating point status"));
381 }
382
383 #else
384
385 static void fetch_fpregs (struct regcache *regcache, int tid)
386 {
387 }
388
389 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
390 {
391 }
392
393 #endif
394 \f
395 /* Transferring arbitrary registers between GDB and inferior. */
396
397 /* Fetch register REGNO from the child process. If REGNO is -1, do
398 this for all registers (including the floating point and SSE
399 registers). */
400
401 static void
402 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
403 struct regcache *regcache, int regno)
404 {
405 int tid;
406
407 /* Use the old method of peeking around in `struct user' if the
408 GETREGS request isn't available. */
409 if (! have_ptrace_getregs)
410 {
411 old_fetch_inferior_registers (regcache, regno);
412 return;
413 }
414
415 /* GNU/Linux LWP ID's are process ID's. */
416 tid = TIDGET (inferior_ptid);
417 if (tid == 0)
418 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
419
420 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
421 transfers more registers in one system call, and we'll cache the
422 results. But remember that fetch_fpxregs can fail, and return
423 zero. */
424 if (regno == -1)
425 {
426 fetch_regs (regcache, tid);
427
428 /* The call above might reset `have_ptrace_getregs'. */
429 if (! have_ptrace_getregs)
430 {
431 old_fetch_inferior_registers (regcache, -1);
432 return;
433 }
434
435 fetch_fpregs (regcache, tid);
436 return;
437 }
438
439 if (getregs_supplies (regno))
440 {
441 fetch_regs (regcache, tid);
442 return;
443 }
444
445 if (getfpregs_supplies (regno))
446 {
447 fetch_fpregs (regcache, tid);
448 return;
449 }
450
451 internal_error (__FILE__, __LINE__,
452 _("Got request for bad register number %d."), regno);
453 }
454
455 /* Store register REGNO back into the child process. If REGNO is -1,
456 do this for all registers (including the floating point and SSE
457 registers). */
458 static void
459 m68k_linux_store_inferior_registers (struct target_ops *ops,
460 struct regcache *regcache, int regno)
461 {
462 int tid;
463
464 /* Use the old method of poking around in `struct user' if the
465 SETREGS request isn't available. */
466 if (! have_ptrace_getregs)
467 {
468 old_store_inferior_registers (regcache, regno);
469 return;
470 }
471
472 /* GNU/Linux LWP ID's are process ID's. */
473 tid = TIDGET (inferior_ptid);
474 if (tid == 0)
475 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
476
477 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
478 transfers more registers in one system call. But remember that
479 store_fpregs can fail, and return zero. */
480 if (regno == -1)
481 {
482 store_regs (regcache, tid, regno);
483 store_fpregs (regcache, tid, regno);
484 return;
485 }
486
487 if (getregs_supplies (regno))
488 {
489 store_regs (regcache, tid, regno);
490 return;
491 }
492
493 if (getfpregs_supplies (regno))
494 {
495 store_fpregs (regcache, tid, regno);
496 return;
497 }
498
499 internal_error (__FILE__, __LINE__,
500 _("Got request to store bad register number %d."), regno);
501 }
502 \f
503 /* Interpreting register set info found in core files. */
504
505 /* Provide registers to GDB from a core file.
506
507 (We can't use the generic version of this function in
508 core-regset.c, because we need to use elf_gregset_t instead of
509 gregset_t.)
510
511 CORE_REG_SECT points to an array of bytes, which are the contents
512 of a `note' from a core file which BFD thinks might contain
513 register contents. CORE_REG_SIZE is its size.
514
515 WHICH says which register set corelow suspects this is:
516 0 --- the general-purpose register set, in elf_gregset_t format
517 2 --- the floating-point register set, in elf_fpregset_t format
518
519 REG_ADDR isn't used on GNU/Linux. */
520
521 static void
522 fetch_core_registers (struct regcache *regcache,
523 char *core_reg_sect, unsigned core_reg_size,
524 int which, CORE_ADDR reg_addr)
525 {
526 elf_gregset_t gregset;
527 elf_fpregset_t fpregset;
528
529 switch (which)
530 {
531 case 0:
532 if (core_reg_size != sizeof (gregset))
533 warning (_("Wrong size gregset in core file."));
534 else
535 {
536 memcpy (&gregset, core_reg_sect, sizeof (gregset));
537 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
538 }
539 break;
540
541 case 2:
542 if (core_reg_size != sizeof (fpregset))
543 warning (_("Wrong size fpregset in core file."));
544 else
545 {
546 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
547 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
548 }
549 break;
550
551 default:
552 /* We've covered all the kinds of registers we know about here,
553 so this must be something we wouldn't know what to do with
554 anyway. Just ignore it. */
555 break;
556 }
557 }
558 \f
559
560 /* Register that we are able to handle GNU/Linux ELF core file
561 formats. */
562
563 static struct core_fns linux_elf_core_fns =
564 {
565 bfd_target_elf_flavour, /* core_flavour */
566 default_check_format, /* check_format */
567 default_core_sniffer, /* core_sniffer */
568 fetch_core_registers, /* core_read_registers */
569 NULL /* next */
570 };
571
572 void _initialize_m68k_linux_nat (void);
573
574 void
575 _initialize_m68k_linux_nat (void)
576 {
577 struct target_ops *t;
578
579 /* Fill in the generic GNU/Linux methods. */
580 t = linux_target ();
581
582 /* Add our register access methods. */
583 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
584 t->to_store_registers = m68k_linux_store_inferior_registers;
585
586 /* Register the target. */
587 linux_nat_add_target (t);
588
589 deprecated_add_core_fns (&linux_elf_core_fns);
590 }
This page took 0.041346 seconds and 4 git commands to generate.