gdb/ppcobsd-nat.c - gdb

Functions defined

Source code

  1. /* Native-dependent code for OpenBSD/powerpc.

  2.    Copyright (C) 2004-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "inferior.h"
  17. #include "regcache.h"

  18. #include <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include <sys/signal.h>
  21. #include <machine/frame.h>
  22. #include <machine/pcb.h>
  23. #include <machine/reg.h>

  24. #include "ppc-tdep.h"
  25. #include "ppcobsd-tdep.h"
  26. #include "inf-ptrace.h"
  27. #include "obsd-nat.h"
  28. #include "bsd-kvm.h"

  29. /* OpenBSD/powerpc didn't have PT_GETFPREGS/PT_SETFPREGS until release
  30.    4.0.  On older releases the floating-point registers are handled by
  31.    PT_GETREGS/PT_SETREGS, but fpscr wasn't available..  */

  32. #ifdef PT_GETFPREGS

  33. /* Returns true if PT_GETFPREGS fetches this register.  */

  34. static int
  35. getfpregs_supplies (struct gdbarch *gdbarch, int regnum)
  36. {
  37.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  38.   /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
  39.      point registers.  Traditionally, GDB's register set has still
  40.      listed the floating point registers for such machines, so this
  41.      code is harmless.  However, the new E500 port actually omits the
  42.      floating point registers entirely from the register set --- they
  43.      don't even have register numbers assigned to them.

  44.      It's not clear to me how best to update this code, so this assert
  45.      will alert the first person to encounter the NetBSD/E500
  46.      combination to the problem.  */
  47.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  48.   return ((regnum >= tdep->ppc_fp0_regnum
  49.            && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
  50.           || regnum == tdep->ppc_fpscr_regnum);
  51. }

  52. #endif /* PT_GETFPREGS */

  53. /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
  54.    for all registers.  */

  55. static void
  56. ppcobsd_fetch_registers (struct target_ops *ops,
  57.                          struct regcache *regcache, int regnum)
  58. {
  59.   struct reg regs;

  60.   if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
  61.               (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  62.     perror_with_name (_("Couldn't get registers"));

  63.   ppc_supply_gregset (&ppcobsd_gregset, regcache, -1,
  64.                       &regs, sizeof regs);
  65. #ifndef PT_GETFPREGS
  66.   ppc_supply_fpregset (&ppcobsd_gregset, regcache, -1,
  67.                        &regs, sizeof regs);
  68. #endif

  69. #ifdef PT_GETFPREGS
  70.   if (regnum == -1
  71.       || getfpregs_supplies (get_regcache_arch (regcache), regnum))
  72.     {
  73.       struct fpreg fpregs;

  74.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  75.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  76.         perror_with_name (_("Couldn't get floating point status"));

  77.       ppc_supply_fpregset (&ppcobsd_fpregset, regcache, -1,
  78.                            &fpregs, sizeof fpregs);
  79.     }
  80. #endif
  81. }

  82. /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
  83.    this for all registers.  */

  84. static void
  85. ppcobsd_store_registers (struct target_ops *ops,
  86.                          struct regcache *regcache, int regnum)
  87. {
  88.   struct reg regs;

  89.   if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
  90.               (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  91.     perror_with_name (_("Couldn't get registers"));

  92.   ppc_collect_gregset (&ppcobsd_gregset, regcache,
  93.                        regnum, &regs, sizeof regs);
  94. #ifndef PT_GETFPREGS
  95.   ppc_collect_fpregset (&ppcobsd_gregset, regcache,
  96.                         regnum, &regs, sizeof regs);
  97. #endif

  98.   if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
  99.               (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  100.     perror_with_name (_("Couldn't write registers"));

  101. #ifdef PT_GETFPREGS
  102.   if (regnum == -1
  103.       || getfpregs_supplies (get_regcache_arch (regcache), regnum))
  104.     {
  105.       struct fpreg fpregs;

  106.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  107.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  108.         perror_with_name (_("Couldn't get floating point status"));

  109.       ppc_collect_fpregset (&ppcobsd_fpregset, regcache,
  110.                             regnum, &fpregs, sizeof fpregs);

  111.       if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
  112.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  113.         perror_with_name (_("Couldn't write floating point status"));
  114.     }
  115. #endif
  116. }


  117. static int
  118. ppcobsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  119. {
  120.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  121.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  122.   struct switchframe sf;
  123.   struct callframe cf;
  124.   int i, regnum;

  125.   /* The following is true for OpenBSD 3.7:

  126.      The pcb contains %r1 (the stack pointer) at the point of the
  127.      context switch in cpu_switch().  At that point we have a stack
  128.      frame as described by `struct switchframe', and below that a call
  129.      frame as described by `struct callframe'.  From this information
  130.      we reconstruct the register state as it would look when we are in
  131.      cpu_switch().  */

  132.   /* The stack pointer shouldn't be zero.  */
  133.   if (pcb->pcb_sp == 0)
  134.     return 0;

  135.   read_memory (pcb->pcb_sp, (gdb_byte *)&sf, sizeof sf);
  136.   regcache_raw_supply (regcache, gdbarch_sp_regnum (gdbarch), &sf.sp);
  137.   regcache_raw_supply (regcache, tdep->ppc_cr_regnum, &sf.cr);
  138.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 2, &sf.fixreg2);
  139.   for (i = 0, regnum = tdep->ppc_gp0_regnum + 13; i < 19; i++, regnum++)
  140.     regcache_raw_supply (regcache, regnum, &sf.fixreg[i]);

  141.   read_memory (sf.sp, (gdb_byte *)&cf, sizeof cf);
  142.   regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &cf.lr);
  143.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 30, &cf.r30);
  144.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 31, &cf.r31);

  145.   return 1;
  146. }


  147. /* Provide a prototype to silence -Wmissing-prototypes.  */
  148. void _initialize_ppcobsd_nat (void);

  149. void
  150. _initialize_ppcobsd_nat (void)
  151. {
  152.   struct target_ops *t;

  153.   /* Add in local overrides.  */
  154.   t = inf_ptrace_target ();
  155.   t->to_fetch_registers = ppcobsd_fetch_registers;
  156.   t->to_store_registers = ppcobsd_store_registers;
  157.   obsd_add_target (t);

  158.   /* General-purpose registers.  */
  159.   ppcobsd_reg_offsets.r0_offset = offsetof (struct reg, gpr);
  160.   ppcobsd_reg_offsets.gpr_size = 4;
  161.   ppcobsd_reg_offsets.xr_size = 4;
  162.   ppcobsd_reg_offsets.pc_offset = offsetof (struct reg, pc);
  163.   ppcobsd_reg_offsets.ps_offset = offsetof (struct reg, ps);
  164.   ppcobsd_reg_offsets.cr_offset = offsetof (struct reg, cnd);
  165.   ppcobsd_reg_offsets.lr_offset = offsetof (struct reg, lr);
  166.   ppcobsd_reg_offsets.ctr_offset = offsetof (struct reg, cnt);
  167.   ppcobsd_reg_offsets.xer_offset = offsetof (struct reg, xer);
  168.   ppcobsd_reg_offsets.mq_offset = offsetof (struct reg, mq);

  169.   /* Floating-point registers.  */
  170.   ppcobsd_reg_offsets.f0_offset = offsetof (struct reg, fpr);
  171.   ppcobsd_reg_offsets.fpscr_offset = -1;
  172. #ifdef PT_GETFPREGS
  173.   ppcobsd_fpreg_offsets.f0_offset = offsetof (struct fpreg, fpr);
  174.   ppcobsd_fpreg_offsets.fpscr_offset = offsetof (struct fpreg, fpscr);
  175.   ppcobsd_fpreg_offsets.fpscr_size = 4;
  176. #endif

  177.   /* AltiVec registers.  */
  178.   ppcobsd_reg_offsets.vr0_offset = offsetof (struct vreg, vreg);
  179.   ppcobsd_reg_offsets.vscr_offset = offsetof (struct vreg, vscr);
  180.   ppcobsd_reg_offsets.vrsave_offset = offsetof (struct vreg, vrsave);

  181.   /* Support debugging kernel virtual memory images.  */
  182.   bsd_kvm_add_target (ppcobsd_supply_pcb);
  183. }