gdb/s390-linux-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* S390 native-dependent code for GDB, the GNU debugger.
  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  4.    for IBM Deutschland Entwicklung GmbH, IBM Corporation.

  5.    This file is part of GDB.

  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
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

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

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

  16. #include "defs.h"
  17. #include "regcache.h"
  18. #include "inferior.h"
  19. #include "target.h"
  20. #include "linux-nat.h"
  21. #include "auxv.h"
  22. #include "gregset.h"
  23. #include "regset.h"

  24. #include "s390-linux-tdep.h"
  25. #include "elf/common.h"

  26. #include <asm/ptrace.h>
  27. #include <sys/ptrace.h>
  28. #include <asm/types.h>
  29. #include <sys/procfs.h>
  30. #include <sys/ucontext.h>
  31. #include <elf.h>

  32. #ifndef PTRACE_GETREGSET
  33. #define PTRACE_GETREGSET 0x4204
  34. #endif

  35. #ifndef PTRACE_SETREGSET
  36. #define PTRACE_SETREGSET 0x4205
  37. #endif

  38. static int have_regset_last_break = 0;
  39. static int have_regset_system_call = 0;
  40. static int have_regset_tdb = 0;

  41. /* Register map for 32-bit executables running under a 64-bit
  42.    kernel.  */

  43. #ifdef __s390x__
  44. static const struct regcache_map_entry s390_64_regmap_gregset[] =
  45.   {
  46.     /* Skip PSWM and PSWA, since they must be handled specially.  */
  47.     { 2, REGCACHE_MAP_SKIP, 8 },
  48.     { 1, S390_R0_UPPER_REGNUM, 4 }, { 1, S390_R0_REGNUM, 4 },
  49.     { 1, S390_R1_UPPER_REGNUM, 4 }, { 1, S390_R1_REGNUM, 4 },
  50.     { 1, S390_R2_UPPER_REGNUM, 4 }, { 1, S390_R2_REGNUM, 4 },
  51.     { 1, S390_R3_UPPER_REGNUM, 4 }, { 1, S390_R3_REGNUM, 4 },
  52.     { 1, S390_R4_UPPER_REGNUM, 4 }, { 1, S390_R4_REGNUM, 4 },
  53.     { 1, S390_R5_UPPER_REGNUM, 4 }, { 1, S390_R5_REGNUM, 4 },
  54.     { 1, S390_R6_UPPER_REGNUM, 4 }, { 1, S390_R6_REGNUM, 4 },
  55.     { 1, S390_R7_UPPER_REGNUM, 4 }, { 1, S390_R7_REGNUM, 4 },
  56.     { 1, S390_R8_UPPER_REGNUM, 4 }, { 1, S390_R8_REGNUM, 4 },
  57.     { 1, S390_R9_UPPER_REGNUM, 4 }, { 1, S390_R9_REGNUM, 4 },
  58.     { 1, S390_R10_UPPER_REGNUM, 4 }, { 1, S390_R10_REGNUM, 4 },
  59.     { 1, S390_R11_UPPER_REGNUM, 4 }, { 1, S390_R11_REGNUM, 4 },
  60.     { 1, S390_R12_UPPER_REGNUM, 4 }, { 1, S390_R12_REGNUM, 4 },
  61.     { 1, S390_R13_UPPER_REGNUM, 4 }, { 1, S390_R13_REGNUM, 4 },
  62.     { 1, S390_R14_UPPER_REGNUM, 4 }, { 1, S390_R14_REGNUM, 4 },
  63.     { 1, S390_R15_UPPER_REGNUM, 4 }, { 1, S390_R15_REGNUM, 4 },
  64.     { 16, S390_A0_REGNUM, 4 },
  65.     { 1, REGCACHE_MAP_SKIP, 4 }, { 1, S390_ORIG_R2_REGNUM, 4 },
  66.     { 0 }
  67.   };

  68. static const struct regset s390_64_gregset =
  69.   {
  70.     s390_64_regmap_gregset,
  71.     regcache_supply_regset,
  72.     regcache_collect_regset
  73.   };

  74. #define S390_PSWM_OFFSET 0
  75. #define S390_PSWA_OFFSET 8
  76. #endif

  77. /* Fill GDB's register array with the general-purpose register values
  78.    in *REGP.

  79.    When debugging a 32-bit executable running under a 64-bit kernel,
  80.    we have to fix up the 64-bit registers we get from the kernel to
  81.    make them look like 32-bit registers.  */

  82. void
  83. supply_gregset (struct regcache *regcache, const gregset_t *regp)
  84. {
  85. #ifdef __s390x__
  86.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  87.   if (gdbarch_ptr_bit (gdbarch) == 32)
  88.     {
  89.       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  90.       ULONGEST pswm, pswa;
  91.       gdb_byte buf[4];

  92.       regcache_supply_regset (&s390_64_gregset, regcache, -1,
  93.                               regp, sizeof (gregset_t));
  94.       pswm = extract_unsigned_integer ((const gdb_byte *) regp
  95.                                        + S390_PSWM_OFFSET, 8, byte_order);
  96.       pswa = extract_unsigned_integer ((const gdb_byte *) regp
  97.                                        + S390_PSWA_OFFSET, 8, byte_order);
  98.       store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
  99.       regcache_raw_supply (regcache, S390_PSWM_REGNUM, buf);
  100.       store_unsigned_integer (buf, 4, byte_order,
  101.                               (pswa & 0x7fffffff) | (pswm & 0x80000000));
  102.       regcache_raw_supply (regcache, S390_PSWA_REGNUM, buf);
  103.       return;
  104.     }
  105. #endif

  106.   regcache_supply_regset (&s390_gregset, regcache, -1, regp,
  107.                           sizeof (gregset_t));
  108. }

  109. /* Fill register REGNO (if it is a general-purpose register) in
  110.    *REGP with the value in GDB's register array.  If REGNO is -1,
  111.    do this for all registers.  */

  112. void
  113. fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
  114. {
  115. #ifdef __s390x__
  116.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  117.   if (gdbarch_ptr_bit (gdbarch) == 32)
  118.     {
  119.       regcache_collect_regset (&s390_64_gregset, regcache, regno,
  120.                                regp, sizeof (gregset_t));

  121.       if (regno == -1
  122.           || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
  123.         {
  124.           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  125.           ULONGEST pswa, pswm;
  126.           gdb_byte buf[4];

  127.           regcache_raw_collect (regcache, S390_PSWM_REGNUM, buf);
  128.           pswm = extract_unsigned_integer (buf, 4, byte_order);
  129.           regcache_raw_collect (regcache, S390_PSWA_REGNUM, buf);
  130.           pswa = extract_unsigned_integer (buf, 4, byte_order);

  131.           if (regno == -1 || regno == S390_PSWM_REGNUM)
  132.             store_unsigned_integer ((gdb_byte *) regp + S390_PSWM_OFFSET, 8,
  133.                                     byte_order, ((pswm & 0xfff7ffff) << 32) |
  134.                                     (pswa & 0x80000000));
  135.           if (regno == -1 || regno == S390_PSWA_REGNUM)
  136.             store_unsigned_integer ((gdb_byte *) regp + S390_PSWA_OFFSET, 8,
  137.                                     byte_order, pswa & 0x7fffffff);
  138.         }
  139.       return;
  140.     }
  141. #endif

  142.   regcache_collect_regset (&s390_gregset, regcache, regno, regp,
  143.                            sizeof (gregset_t));
  144. }

  145. /* Fill GDB's register array with the floating-point register values
  146.    in *REGP.  */
  147. void
  148. supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
  149. {
  150.   regcache_supply_regset (&s390_fpregset, regcache, -1, regp,
  151.                           sizeof (fpregset_t));
  152. }

  153. /* Fill register REGNO (if it is a general-purpose register) in
  154.    *REGP with the value in GDB's register array.  If REGNO is -1,
  155.    do this for all registers.  */
  156. void
  157. fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
  158. {
  159.   regcache_collect_regset (&s390_fpregset, regcache, regno, regp,
  160.                            sizeof (fpregset_t));
  161. }

  162. /* Find the TID for the current inferior thread to use with ptrace.  */
  163. static int
  164. s390_inferior_tid (void)
  165. {
  166.   /* GNU/Linux LWP ID's are process ID's.  */
  167.   int tid = ptid_get_lwp (inferior_ptid);
  168.   if (tid == 0)
  169.     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */

  170.   return tid;
  171. }

  172. /* Fetch all general-purpose registers from process/thread TID and
  173.    store their values in GDB's register cache.  */
  174. static void
  175. fetch_regs (struct regcache *regcache, int tid)
  176. {
  177.   gregset_t regs;
  178.   ptrace_area parea;

  179.   parea.len = sizeof (regs);
  180.   parea.process_addr = (addr_t) &regs;
  181.   parea.kernel_addr = offsetof (struct user_regs_struct, psw);
  182.   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
  183.     perror_with_name (_("Couldn't get registers"));

  184.   supply_gregset (regcache, (const gregset_t *) &regs);
  185. }

  186. /* Store all valid general-purpose registers in GDB's register cache
  187.    into the process/thread specified by TID.  */
  188. static void
  189. store_regs (const struct regcache *regcache, int tid, int regnum)
  190. {
  191.   gregset_t regs;
  192.   ptrace_area parea;

  193.   parea.len = sizeof (regs);
  194.   parea.process_addr = (addr_t) &regs;
  195.   parea.kernel_addr = offsetof (struct user_regs_struct, psw);
  196.   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
  197.     perror_with_name (_("Couldn't get registers"));

  198.   fill_gregset (regcache, &regs, regnum);

  199.   if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
  200.     perror_with_name (_("Couldn't write registers"));
  201. }

  202. /* Fetch all floating-point registers from process/thread TID and store
  203.    their values in GDB's register cache.  */
  204. static void
  205. fetch_fpregs (struct regcache *regcache, int tid)
  206. {
  207.   fpregset_t fpregs;
  208.   ptrace_area parea;

  209.   parea.len = sizeof (fpregs);
  210.   parea.process_addr = (addr_t) &fpregs;
  211.   parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
  212.   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
  213.     perror_with_name (_("Couldn't get floating point status"));

  214.   supply_fpregset (regcache, (const fpregset_t *) &fpregs);
  215. }

  216. /* Store all valid floating-point registers in GDB's register cache
  217.    into the process/thread specified by TID.  */
  218. static void
  219. store_fpregs (const struct regcache *regcache, int tid, int regnum)
  220. {
  221.   fpregset_t fpregs;
  222.   ptrace_area parea;

  223.   parea.len = sizeof (fpregs);
  224.   parea.process_addr = (addr_t) &fpregs;
  225.   parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
  226.   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
  227.     perror_with_name (_("Couldn't get floating point status"));

  228.   fill_fpregset (regcache, &fpregs, regnum);

  229.   if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
  230.     perror_with_name (_("Couldn't write floating point status"));
  231. }

  232. /* Fetch all registers in the kernel's register set whose number is
  233.    REGSET_ID, whose size is REGSIZE, and whose layout is described by
  234.    REGSET, from process/thread TID and store their values in GDB's
  235.    register cache.  */
  236. static void
  237. fetch_regset (struct regcache *regcache, int tid,
  238.               int regset_id, int regsize, const struct regset *regset)
  239. {
  240.   gdb_byte *buf = alloca (regsize);
  241.   struct iovec iov;

  242.   iov.iov_base = buf;
  243.   iov.iov_len = regsize;

  244.   if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
  245.     {
  246.       if (errno == ENODATA)
  247.         regcache_supply_regset (regset, regcache, -1, NULL, regsize);
  248.       else
  249.         perror_with_name (_("Couldn't get register set"));
  250.     }
  251.   else
  252.     regcache_supply_regset (regset, regcache, -1, buf, regsize);
  253. }

  254. /* Store all registers in the kernel's register set whose number is
  255.    REGSET_ID, whose size is REGSIZE, and whose layout is described by
  256.    REGSET, from GDB's register cache back to process/thread TID.  */
  257. static void
  258. store_regset (struct regcache *regcache, int tid,
  259.               int regset_id, int regsize, const struct regset *regset)
  260. {
  261.   gdb_byte *buf = alloca (regsize);
  262.   struct iovec iov;

  263.   iov.iov_base = buf;
  264.   iov.iov_len = regsize;

  265.   if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
  266.     perror_with_name (_("Couldn't get register set"));

  267.   regcache_collect_regset (regset, regcache, -1, buf, regsize);

  268.   if (ptrace (PTRACE_SETREGSET, tid, (long) regset_id, (long) &iov) < 0)
  269.     perror_with_name (_("Couldn't set register set"));
  270. }

  271. /* Check whether the kernel provides a register set with number REGSET
  272.    of size REGSIZE for process/thread TID.  */
  273. static int
  274. check_regset (int tid, int regset, int regsize)
  275. {
  276.   gdb_byte *buf = alloca (regsize);
  277.   struct iovec iov;

  278.   iov.iov_base = buf;
  279.   iov.iov_len = regsize;

  280.   if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
  281.       || errno == ENODATA)
  282.     return 1;
  283.   return 0;
  284. }

  285. /* Fetch register REGNUM from the child process.  If REGNUM is -1, do
  286.    this for all registers.  */
  287. static void
  288. s390_linux_fetch_inferior_registers (struct target_ops *ops,
  289.                                      struct regcache *regcache, int regnum)
  290. {
  291.   int tid = s390_inferior_tid ();

  292.   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
  293.     fetch_regs (regcache, tid);

  294.   if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
  295.     fetch_fpregs (regcache, tid);

  296.   if (have_regset_last_break)
  297.     if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
  298.       fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
  299.                     (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32
  300.                      ? &s390_last_break_regset : &s390x_last_break_regset));

  301.   if (have_regset_system_call)
  302.     if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
  303.       fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
  304.                     &s390_system_call_regset);

  305.   if (have_regset_tdb)
  306.     if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum))
  307.       fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset,
  308.                     &s390_tdb_regset);
  309. }

  310. /* Store register REGNUM back into the child process.  If REGNUM is
  311.    -1, do this for all registers.  */
  312. static void
  313. s390_linux_store_inferior_registers (struct target_ops *ops,
  314.                                      struct regcache *regcache, int regnum)
  315. {
  316.   int tid = s390_inferior_tid ();

  317.   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
  318.     store_regs (regcache, tid, regnum);

  319.   if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
  320.     store_fpregs (regcache, tid, regnum);

  321.   /* S390_LAST_BREAK_REGNUM is read-only.  */

  322.   if (have_regset_system_call)
  323.     if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
  324.       store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
  325.                     &s390_system_call_regset);
  326. }


  327. /* Hardware-assisted watchpoint handling.  */

  328. /* We maintain a list of all currently active watchpoints in order
  329.    to properly handle watchpoint removal.

  330.    The only thing we actually need is the total address space area
  331.    spanned by the watchpoints.  */

  332. struct watch_area
  333. {
  334.   struct watch_area *next;
  335.   CORE_ADDR lo_addr;
  336.   CORE_ADDR hi_addr;
  337. };

  338. static struct watch_area *watch_base = NULL;

  339. static int
  340. s390_stopped_by_watchpoint (struct target_ops *ops)
  341. {
  342.   per_lowcore_bits per_lowcore;
  343.   ptrace_area parea;
  344.   int result;

  345.   /* Speed up common case.  */
  346.   if (!watch_base)
  347.     return 0;

  348.   parea.len = sizeof (per_lowcore);
  349.   parea.process_addr = (addr_t) & per_lowcore;
  350.   parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
  351.   if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0)
  352.     perror_with_name (_("Couldn't retrieve watchpoint status"));

  353.   result = (per_lowcore.perc_storage_alteration == 1
  354.             && per_lowcore.perc_store_real_address == 0);

  355.   if (result)
  356.     {
  357.       /* Do not report this watchpoint again.  */
  358.       memset (&per_lowcore, 0, sizeof (per_lowcore));
  359.       if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea) < 0)
  360.         perror_with_name (_("Couldn't clear watchpoint status"));
  361.     }

  362.   return result;
  363. }

  364. static void
  365. s390_fix_watch_points (struct lwp_info *lp)
  366. {
  367.   int tid;

  368.   per_struct per_info;
  369.   ptrace_area parea;

  370.   CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
  371.   struct watch_area *area;

  372.   tid = ptid_get_lwp (lp->ptid);
  373.   if (tid == 0)
  374.     tid = ptid_get_pid (lp->ptid);

  375.   for (area = watch_base; area; area = area->next)
  376.     {
  377.       watch_lo_addr = min (watch_lo_addr, area->lo_addr);
  378.       watch_hi_addr = max (watch_hi_addr, area->hi_addr);
  379.     }

  380.   parea.len = sizeof (per_info);
  381.   parea.process_addr = (addr_t) & per_info;
  382.   parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
  383.   if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0)
  384.     perror_with_name (_("Couldn't retrieve watchpoint status"));

  385.   if (watch_base)
  386.     {
  387.       per_info.control_regs.bits.em_storage_alteration = 1;
  388.       per_info.control_regs.bits.storage_alt_space_ctl = 1;
  389.     }
  390.   else
  391.     {
  392.       per_info.control_regs.bits.em_storage_alteration = 0;
  393.       per_info.control_regs.bits.storage_alt_space_ctl = 0;
  394.     }
  395.   per_info.starting_addr = watch_lo_addr;
  396.   per_info.ending_addr = watch_hi_addr;

  397.   if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0)
  398.     perror_with_name (_("Couldn't modify watchpoint status"));
  399. }

  400. static int
  401. s390_insert_watchpoint (struct target_ops *self,
  402.                         CORE_ADDR addr, int len, int type,
  403.                         struct expression *cond)
  404. {
  405.   struct lwp_info *lp;
  406.   struct watch_area *area = xmalloc (sizeof (struct watch_area));

  407.   if (!area)
  408.     return -1;

  409.   area->lo_addr = addr;
  410.   area->hi_addr = addr + len - 1;

  411.   area->next = watch_base;
  412.   watch_base = area;

  413.   ALL_LWPS (lp)
  414.     s390_fix_watch_points (lp);
  415.   return 0;
  416. }

  417. static int
  418. s390_remove_watchpoint (struct target_ops *self,
  419.                         CORE_ADDR addr, int len, int type,
  420.                         struct expression *cond)
  421. {
  422.   struct lwp_info *lp;
  423.   struct watch_area *area, **parea;

  424.   for (parea = &watch_base; *parea; parea = &(*parea)->next)
  425.     if ((*parea)->lo_addr == addr
  426.         && (*parea)->hi_addr == addr + len - 1)
  427.       break;

  428.   if (!*parea)
  429.     {
  430.       fprintf_unfiltered (gdb_stderr,
  431.                           "Attempt to remove nonexistent watchpoint.\n");
  432.       return -1;
  433.     }

  434.   area = *parea;
  435.   *parea = area->next;
  436.   xfree (area);

  437.   ALL_LWPS (lp)
  438.     s390_fix_watch_points (lp);
  439.   return 0;
  440. }

  441. static int
  442. s390_can_use_hw_breakpoint (struct target_ops *self,
  443.                             int type, int cnt, int othertype)
  444. {
  445.   return type == bp_hardware_watchpoint;
  446. }

  447. static int
  448. s390_region_ok_for_hw_watchpoint (struct target_ops *self,
  449.                                   CORE_ADDR addr, int cnt)
  450. {
  451.   return 1;
  452. }

  453. static int
  454. s390_target_wordsize (void)
  455. {
  456.   int wordsize = 4;

  457.   /* Check for 64-bit inferior process.  This is the case when the host is
  458.      64-bit, and in addition bit 32 of the PSW mask is set.  */
  459. #ifdef __s390x__
  460.   long pswm;

  461.   errno = 0;
  462.   pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
  463.   if (errno == 0 && (pswm & 0x100000000ul) != 0)
  464.     wordsize = 8;
  465. #endif

  466.   return wordsize;
  467. }

  468. static int
  469. s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
  470.                  gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  471. {
  472.   int sizeof_auxv_field = s390_target_wordsize ();
  473.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  474.   gdb_byte *ptr = *readptr;

  475.   if (endptr == ptr)
  476.     return 0;

  477.   if (endptr - ptr < sizeof_auxv_field * 2)
  478.     return -1;

  479.   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  480.   ptr += sizeof_auxv_field;
  481.   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  482.   ptr += sizeof_auxv_field;

  483.   *readptr = ptr;
  484.   return 1;
  485. }

  486. #ifdef __s390x__
  487. static unsigned long
  488. s390_get_hwcap (void)
  489. {
  490.   CORE_ADDR field;

  491.   if (target_auxv_search (&current_target, AT_HWCAP, &field))
  492.     return (unsigned long) field;

  493.   return 0;
  494. }
  495. #endif

  496. static const struct target_desc *
  497. s390_read_description (struct target_ops *ops)
  498. {
  499.   int tid = s390_inferior_tid ();

  500.   have_regset_last_break
  501.     = check_regset (tid, NT_S390_LAST_BREAK, 8);
  502.   have_regset_system_call
  503.     = check_regset (tid, NT_S390_SYSTEM_CALL, 4);

  504. #ifdef __s390x__
  505.   /* If GDB itself is compiled as 64-bit, we are running on a machine in
  506.      z/Architecture mode.  If the target is running in 64-bit addressing
  507.      mode, report s390x architecture.  If the target is running in 31-bit
  508.      addressing mode, but the kernel supports using 64-bit registers in
  509.      that mode, report s390 architecture with 64-bit GPRs.  */

  510.   have_regset_tdb = (s390_get_hwcap () & HWCAP_S390_TE) ?
  511.     check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset) : 0;

  512.   if (s390_target_wordsize () == 8)
  513.     return (have_regset_tdb ? tdesc_s390x_te_linux64 :
  514.             have_regset_system_call? tdesc_s390x_linux64v2 :
  515.             have_regset_last_break? tdesc_s390x_linux64v1 :
  516.             tdesc_s390x_linux64);

  517.   if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS)
  518.     return (have_regset_tdb ? tdesc_s390_te_linux64 :
  519.             have_regset_system_call? tdesc_s390_linux64v2 :
  520.             have_regset_last_break? tdesc_s390_linux64v1 :
  521.             tdesc_s390_linux64);
  522. #endif

  523.   /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
  524.      on a 64-bit kernel that does not support using 64-bit registers in 31-bit
  525.      mode, report s390 architecture with 32-bit GPRs.  */
  526.   return (have_regset_system_call? tdesc_s390_linux32v2 :
  527.           have_regset_last_break? tdesc_s390_linux32v1 :
  528.           tdesc_s390_linux32);
  529. }

  530. void _initialize_s390_nat (void);

  531. void
  532. _initialize_s390_nat (void)
  533. {
  534.   struct target_ops *t;

  535.   /* Fill in the generic GNU/Linux methods.  */
  536.   t = linux_target ();

  537.   /* Add our register access methods.  */
  538.   t->to_fetch_registers = s390_linux_fetch_inferior_registers;
  539.   t->to_store_registers = s390_linux_store_inferior_registers;

  540.   /* Add our watchpoint methods.  */
  541.   t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint;
  542.   t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint;
  543.   t->to_have_continuable_watchpoint = 1;
  544.   t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint;
  545.   t->to_insert_watchpoint = s390_insert_watchpoint;
  546.   t->to_remove_watchpoint = s390_remove_watchpoint;

  547.   /* Detect target architecture.  */
  548.   t->to_read_description = s390_read_description;
  549.   t->to_auxv_parse = s390_auxv_parse;

  550.   /* Register the target.  */
  551.   linux_nat_add_target (t);
  552.   linux_nat_set_new_thread (t, s390_fix_watch_points);
  553. }