Home | History | Annotate | Line # | Download | only in gdb
s390-linux-nat.c revision 1.1
      1  1.1  christos /* S390 native-dependent code for GDB, the GNU debugger.
      2  1.1  christos    Copyright (C) 2001-2014 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    Contributed by D.J. Barrow (djbarrow (at) de.ibm.com,barrow_dj (at) yahoo.com)
      5  1.1  christos    for IBM Deutschland Entwicklung GmbH, IBM Corporation.
      6  1.1  christos 
      7  1.1  christos    This file is part of GDB.
      8  1.1  christos 
      9  1.1  christos    This program is free software; you can redistribute it and/or modify
     10  1.1  christos    it under the terms of the GNU General Public License as published by
     11  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     12  1.1  christos    (at your option) any later version.
     13  1.1  christos 
     14  1.1  christos    This program is distributed in the hope that it will be useful,
     15  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  1.1  christos    GNU General Public License for more details.
     18  1.1  christos 
     19  1.1  christos    You should have received a copy of the GNU General Public License
     20  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21  1.1  christos 
     22  1.1  christos #include "defs.h"
     23  1.1  christos #include "regcache.h"
     24  1.1  christos #include "inferior.h"
     25  1.1  christos #include "target.h"
     26  1.1  christos #include "linux-nat.h"
     27  1.1  christos #include "auxv.h"
     28  1.1  christos #include "gregset.h"
     29  1.1  christos 
     30  1.1  christos #include "s390-linux-tdep.h"
     31  1.1  christos #include "elf/common.h"
     32  1.1  christos 
     33  1.1  christos #include <asm/ptrace.h>
     34  1.1  christos #include <sys/ptrace.h>
     35  1.1  christos #include <asm/types.h>
     36  1.1  christos #include <sys/procfs.h>
     37  1.1  christos #include <sys/ucontext.h>
     38  1.1  christos #include <elf.h>
     39  1.1  christos 
     40  1.1  christos #ifndef PTRACE_GETREGSET
     41  1.1  christos #define PTRACE_GETREGSET 0x4204
     42  1.1  christos #endif
     43  1.1  christos 
     44  1.1  christos #ifndef PTRACE_SETREGSET
     45  1.1  christos #define PTRACE_SETREGSET 0x4205
     46  1.1  christos #endif
     47  1.1  christos 
     48  1.1  christos static int have_regset_last_break = 0;
     49  1.1  christos static int have_regset_system_call = 0;
     50  1.1  christos static int have_regset_tdb = 0;
     51  1.1  christos 
     52  1.1  christos /* Map registers to gregset/ptrace offsets.
     53  1.1  christos    These arrays are defined in s390-tdep.c.  */
     54  1.1  christos 
     55  1.1  christos #ifdef __s390x__
     56  1.1  christos #define regmap_gregset s390x_regmap_gregset
     57  1.1  christos #else
     58  1.1  christos #define regmap_gregset s390_regmap_gregset
     59  1.1  christos #endif
     60  1.1  christos 
     61  1.1  christos #define regmap_fpregset s390_regmap_fpregset
     62  1.1  christos 
     63  1.1  christos /* Fill the regset described by MAP into REGCACHE, using the values
     64  1.1  christos    from REGP.  The MAP array represents each register as a pair
     65  1.1  christos    (offset, regno) of short integers and is terminated with -1. */
     66  1.1  christos 
     67  1.1  christos static void
     68  1.1  christos s390_native_supply (struct regcache *regcache, const short *map,
     69  1.1  christos 		    const gdb_byte *regp)
     70  1.1  christos {
     71  1.1  christos   for (; map[0] >= 0; map += 2)
     72  1.1  christos     regcache_raw_supply (regcache, map[1], regp ? regp + map[0] : NULL);
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos /* Collect the register REGNO out of the regset described by MAP from
     76  1.1  christos    REGCACHE into REGP.  If REGNO == -1, do this for all registers in
     77  1.1  christos    this regset. */
     78  1.1  christos 
     79  1.1  christos static void
     80  1.1  christos s390_native_collect (const struct regcache *regcache, const short *map,
     81  1.1  christos 		     int regno, gdb_byte *regp)
     82  1.1  christos {
     83  1.1  christos   for (; map[0] >= 0; map += 2)
     84  1.1  christos     if (regno == -1 || regno == map[1])
     85  1.1  christos       regcache_raw_collect (regcache, map[1], regp + map[0]);
     86  1.1  christos }
     87  1.1  christos 
     88  1.1  christos /* Fill GDB's register array with the general-purpose register values
     89  1.1  christos    in *REGP.
     90  1.1  christos 
     91  1.1  christos    When debugging a 32-bit executable running under a 64-bit kernel,
     92  1.1  christos    we have to fix up the 64-bit registers we get from the kernel to
     93  1.1  christos    make them look like 32-bit registers.  */
     94  1.1  christos 
     95  1.1  christos void
     96  1.1  christos supply_gregset (struct regcache *regcache, const gregset_t *regp)
     97  1.1  christos {
     98  1.1  christos #ifdef __s390x__
     99  1.1  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    100  1.1  christos   if (gdbarch_ptr_bit (gdbarch) == 32)
    101  1.1  christos     {
    102  1.1  christos       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    103  1.1  christos       ULONGEST pswm = 0, pswa = 0;
    104  1.1  christos       gdb_byte buf[4];
    105  1.1  christos       const short *map;
    106  1.1  christos 
    107  1.1  christos       for (map = regmap_gregset; map[0] >= 0; map += 2)
    108  1.1  christos 	{
    109  1.1  christos 	  const gdb_byte *p = (const gdb_byte *) regp + map[0];
    110  1.1  christos 	  int regno = map[1];
    111  1.1  christos 
    112  1.1  christos 	  if (regno == S390_PSWM_REGNUM)
    113  1.1  christos 	    pswm = extract_unsigned_integer (p, 8, byte_order);
    114  1.1  christos 	  else if (regno == S390_PSWA_REGNUM)
    115  1.1  christos 	    pswa = extract_unsigned_integer (p, 8, byte_order);
    116  1.1  christos 	  else
    117  1.1  christos 	    {
    118  1.1  christos 	      if ((regno >= S390_R0_REGNUM && regno <= S390_R15_REGNUM)
    119  1.1  christos 		  || regno == S390_ORIG_R2_REGNUM)
    120  1.1  christos 		p += 4;
    121  1.1  christos 	      regcache_raw_supply (regcache, regno, p);
    122  1.1  christos 	    }
    123  1.1  christos 	}
    124  1.1  christos 
    125  1.1  christos       store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
    126  1.1  christos       regcache_raw_supply (regcache, S390_PSWM_REGNUM, buf);
    127  1.1  christos       store_unsigned_integer (buf, 4, byte_order,
    128  1.1  christos 			      (pswa & 0x7fffffff) | (pswm & 0x80000000));
    129  1.1  christos       regcache_raw_supply (regcache, S390_PSWA_REGNUM, buf);
    130  1.1  christos       return;
    131  1.1  christos     }
    132  1.1  christos #endif
    133  1.1  christos 
    134  1.1  christos   s390_native_supply (regcache, regmap_gregset, (const gdb_byte *) regp);
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos /* Fill register REGNO (if it is a general-purpose register) in
    138  1.1  christos    *REGP with the value in GDB's register array.  If REGNO is -1,
    139  1.1  christos    do this for all registers.  */
    140  1.1  christos 
    141  1.1  christos void
    142  1.1  christos fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
    143  1.1  christos {
    144  1.1  christos #ifdef __s390x__
    145  1.1  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    146  1.1  christos   if (gdbarch_ptr_bit (gdbarch) == 32)
    147  1.1  christos     {
    148  1.1  christos       gdb_byte *psw_p[2];
    149  1.1  christos       const short *map;
    150  1.1  christos 
    151  1.1  christos       for (map = regmap_gregset; map[0] >= 0; map += 2)
    152  1.1  christos 	{
    153  1.1  christos 	  gdb_byte *p = (gdb_byte *) regp + map[0];
    154  1.1  christos 	  int reg = map[1];
    155  1.1  christos 
    156  1.1  christos 	  if (reg >= S390_PSWM_REGNUM && reg <= S390_PSWA_REGNUM)
    157  1.1  christos 	    psw_p[reg - S390_PSWM_REGNUM] = p;
    158  1.1  christos 
    159  1.1  christos 	  else if (regno == -1 || regno == reg)
    160  1.1  christos 	    {
    161  1.1  christos 	      if ((reg >= S390_R0_REGNUM && reg <= S390_R15_REGNUM)
    162  1.1  christos 		  || reg == S390_ORIG_R2_REGNUM)
    163  1.1  christos 		{
    164  1.1  christos 		  memset (p, 0, 4);
    165  1.1  christos 		  p += 4;
    166  1.1  christos 		}
    167  1.1  christos 	      regcache_raw_collect (regcache, reg, p + 4);
    168  1.1  christos 	    }
    169  1.1  christos 	}
    170  1.1  christos 
    171  1.1  christos       if (regno == -1
    172  1.1  christos 	  || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
    173  1.1  christos 	{
    174  1.1  christos 	  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    175  1.1  christos 	  ULONGEST pswa, pswm;
    176  1.1  christos 	  gdb_byte buf[4];
    177  1.1  christos 
    178  1.1  christos 	  regcache_raw_collect (regcache, S390_PSWM_REGNUM, buf);
    179  1.1  christos 	  pswm = extract_unsigned_integer (buf, 4, byte_order);
    180  1.1  christos 	  regcache_raw_collect (regcache, S390_PSWA_REGNUM, buf);
    181  1.1  christos 	  pswa = extract_unsigned_integer (buf, 4, byte_order);
    182  1.1  christos 
    183  1.1  christos 	  if (regno == -1 || regno == S390_PSWM_REGNUM)
    184  1.1  christos 	    store_unsigned_integer (psw_p[0], 8, byte_order,
    185  1.1  christos 				    ((pswm & 0xfff7ffff) << 32) |
    186  1.1  christos 				    (pswa & 0x80000000));
    187  1.1  christos 	  if (regno == -1 || regno == S390_PSWA_REGNUM)
    188  1.1  christos 	    store_unsigned_integer (psw_p[1], 8, byte_order,
    189  1.1  christos 				    pswa & 0x7fffffff);
    190  1.1  christos 	}
    191  1.1  christos       return;
    192  1.1  christos     }
    193  1.1  christos #endif
    194  1.1  christos 
    195  1.1  christos   s390_native_collect (regcache, regmap_gregset, regno, (gdb_byte *) regp);
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos /* Fill GDB's register array with the floating-point register values
    199  1.1  christos    in *REGP.  */
    200  1.1  christos void
    201  1.1  christos supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
    202  1.1  christos {
    203  1.1  christos   s390_native_supply (regcache, regmap_fpregset, (const gdb_byte *) regp);
    204  1.1  christos }
    205  1.1  christos 
    206  1.1  christos /* Fill register REGNO (if it is a general-purpose register) in
    207  1.1  christos    *REGP with the value in GDB's register array.  If REGNO is -1,
    208  1.1  christos    do this for all registers.  */
    209  1.1  christos void
    210  1.1  christos fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
    211  1.1  christos {
    212  1.1  christos   s390_native_collect (regcache, regmap_fpregset, regno, (gdb_byte *) regp);
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos /* Find the TID for the current inferior thread to use with ptrace.  */
    216  1.1  christos static int
    217  1.1  christos s390_inferior_tid (void)
    218  1.1  christos {
    219  1.1  christos   /* GNU/Linux LWP ID's are process ID's.  */
    220  1.1  christos   int tid = ptid_get_lwp (inferior_ptid);
    221  1.1  christos   if (tid == 0)
    222  1.1  christos     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
    223  1.1  christos 
    224  1.1  christos   return tid;
    225  1.1  christos }
    226  1.1  christos 
    227  1.1  christos /* Fetch all general-purpose registers from process/thread TID and
    228  1.1  christos    store their values in GDB's register cache.  */
    229  1.1  christos static void
    230  1.1  christos fetch_regs (struct regcache *regcache, int tid)
    231  1.1  christos {
    232  1.1  christos   gregset_t regs;
    233  1.1  christos   ptrace_area parea;
    234  1.1  christos 
    235  1.1  christos   parea.len = sizeof (regs);
    236  1.1  christos   parea.process_addr = (addr_t) &regs;
    237  1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, psw);
    238  1.1  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
    239  1.1  christos     perror_with_name (_("Couldn't get registers"));
    240  1.1  christos 
    241  1.1  christos   supply_gregset (regcache, (const gregset_t *) &regs);
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos /* Store all valid general-purpose registers in GDB's register cache
    245  1.1  christos    into the process/thread specified by TID.  */
    246  1.1  christos static void
    247  1.1  christos store_regs (const struct regcache *regcache, int tid, int regnum)
    248  1.1  christos {
    249  1.1  christos   gregset_t regs;
    250  1.1  christos   ptrace_area parea;
    251  1.1  christos 
    252  1.1  christos   parea.len = sizeof (regs);
    253  1.1  christos   parea.process_addr = (addr_t) &regs;
    254  1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, psw);
    255  1.1  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
    256  1.1  christos     perror_with_name (_("Couldn't get registers"));
    257  1.1  christos 
    258  1.1  christos   fill_gregset (regcache, &regs, regnum);
    259  1.1  christos 
    260  1.1  christos   if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
    261  1.1  christos     perror_with_name (_("Couldn't write registers"));
    262  1.1  christos }
    263  1.1  christos 
    264  1.1  christos /* Fetch all floating-point registers from process/thread TID and store
    265  1.1  christos    their values in GDB's register cache.  */
    266  1.1  christos static void
    267  1.1  christos fetch_fpregs (struct regcache *regcache, int tid)
    268  1.1  christos {
    269  1.1  christos   fpregset_t fpregs;
    270  1.1  christos   ptrace_area parea;
    271  1.1  christos 
    272  1.1  christos   parea.len = sizeof (fpregs);
    273  1.1  christos   parea.process_addr = (addr_t) &fpregs;
    274  1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
    275  1.1  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
    276  1.1  christos     perror_with_name (_("Couldn't get floating point status"));
    277  1.1  christos 
    278  1.1  christos   supply_fpregset (regcache, (const fpregset_t *) &fpregs);
    279  1.1  christos }
    280  1.1  christos 
    281  1.1  christos /* Store all valid floating-point registers in GDB's register cache
    282  1.1  christos    into the process/thread specified by TID.  */
    283  1.1  christos static void
    284  1.1  christos store_fpregs (const struct regcache *regcache, int tid, int regnum)
    285  1.1  christos {
    286  1.1  christos   fpregset_t fpregs;
    287  1.1  christos   ptrace_area parea;
    288  1.1  christos 
    289  1.1  christos   parea.len = sizeof (fpregs);
    290  1.1  christos   parea.process_addr = (addr_t) &fpregs;
    291  1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
    292  1.1  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0)
    293  1.1  christos     perror_with_name (_("Couldn't get floating point status"));
    294  1.1  christos 
    295  1.1  christos   fill_fpregset (regcache, &fpregs, regnum);
    296  1.1  christos 
    297  1.1  christos   if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0)
    298  1.1  christos     perror_with_name (_("Couldn't write floating point status"));
    299  1.1  christos }
    300  1.1  christos 
    301  1.1  christos /* Fetch all registers in the kernel's register set whose number is REGSET,
    302  1.1  christos    whose size is REGSIZE, and whose layout is described by REGMAP, from
    303  1.1  christos    process/thread TID and store their values in GDB's register cache.  */
    304  1.1  christos static void
    305  1.1  christos fetch_regset (struct regcache *regcache, int tid,
    306  1.1  christos 	      int regset, int regsize, const short *regmap)
    307  1.1  christos {
    308  1.1  christos   gdb_byte *buf = alloca (regsize);
    309  1.1  christos   struct iovec iov;
    310  1.1  christos 
    311  1.1  christos   iov.iov_base = buf;
    312  1.1  christos   iov.iov_len = regsize;
    313  1.1  christos 
    314  1.1  christos   if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
    315  1.1  christos     {
    316  1.1  christos       if (errno == ENODATA)
    317  1.1  christos 	s390_native_supply (regcache, regmap, NULL);
    318  1.1  christos       else
    319  1.1  christos 	perror_with_name (_("Couldn't get register set"));
    320  1.1  christos     }
    321  1.1  christos   else
    322  1.1  christos     s390_native_supply (regcache, regmap, buf);
    323  1.1  christos }
    324  1.1  christos 
    325  1.1  christos /* Store all registers in the kernel's register set whose number is REGSET,
    326  1.1  christos    whose size is REGSIZE, and whose layout is described by REGMAP, from
    327  1.1  christos    GDB's register cache back to process/thread TID.  */
    328  1.1  christos static void
    329  1.1  christos store_regset (struct regcache *regcache, int tid,
    330  1.1  christos 	      int regset, int regsize, const short *regmap)
    331  1.1  christos {
    332  1.1  christos   gdb_byte *buf = alloca (regsize);
    333  1.1  christos   struct iovec iov;
    334  1.1  christos 
    335  1.1  christos   iov.iov_base = buf;
    336  1.1  christos   iov.iov_len = regsize;
    337  1.1  christos 
    338  1.1  christos   if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) < 0)
    339  1.1  christos     perror_with_name (_("Couldn't get register set"));
    340  1.1  christos 
    341  1.1  christos   s390_native_collect (regcache, regmap, -1, buf);
    342  1.1  christos 
    343  1.1  christos   if (ptrace (PTRACE_SETREGSET, tid, (long) regset, (long) &iov) < 0)
    344  1.1  christos     perror_with_name (_("Couldn't set register set"));
    345  1.1  christos }
    346  1.1  christos 
    347  1.1  christos /* Check whether the kernel provides a register set with number REGSET
    348  1.1  christos    of size REGSIZE for process/thread TID.  */
    349  1.1  christos static int
    350  1.1  christos check_regset (int tid, int regset, int regsize)
    351  1.1  christos {
    352  1.1  christos   gdb_byte *buf = alloca (regsize);
    353  1.1  christos   struct iovec iov;
    354  1.1  christos 
    355  1.1  christos   iov.iov_base = buf;
    356  1.1  christos   iov.iov_len = regsize;
    357  1.1  christos 
    358  1.1  christos   if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
    359  1.1  christos       || errno == ENODATA)
    360  1.1  christos     return 1;
    361  1.1  christos   return 0;
    362  1.1  christos }
    363  1.1  christos 
    364  1.1  christos /* Fetch register REGNUM from the child process.  If REGNUM is -1, do
    365  1.1  christos    this for all registers.  */
    366  1.1  christos static void
    367  1.1  christos s390_linux_fetch_inferior_registers (struct target_ops *ops,
    368  1.1  christos 				     struct regcache *regcache, int regnum)
    369  1.1  christos {
    370  1.1  christos   int tid = s390_inferior_tid ();
    371  1.1  christos 
    372  1.1  christos   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
    373  1.1  christos     fetch_regs (regcache, tid);
    374  1.1  christos 
    375  1.1  christos   if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
    376  1.1  christos     fetch_fpregs (regcache, tid);
    377  1.1  christos 
    378  1.1  christos   if (have_regset_last_break)
    379  1.1  christos     if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
    380  1.1  christos       fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
    381  1.1  christos 		    (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32
    382  1.1  christos 		     ? s390_regmap_last_break : s390x_regmap_last_break));
    383  1.1  christos 
    384  1.1  christos   if (have_regset_system_call)
    385  1.1  christos     if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
    386  1.1  christos       fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
    387  1.1  christos 		    s390_regmap_system_call);
    388  1.1  christos 
    389  1.1  christos   if (have_regset_tdb)
    390  1.1  christos     if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum))
    391  1.1  christos       fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset,
    392  1.1  christos 		    s390_regmap_tdb);
    393  1.1  christos }
    394  1.1  christos 
    395  1.1  christos /* Store register REGNUM back into the child process.  If REGNUM is
    396  1.1  christos    -1, do this for all registers.  */
    397  1.1  christos static void
    398  1.1  christos s390_linux_store_inferior_registers (struct target_ops *ops,
    399  1.1  christos 				     struct regcache *regcache, int regnum)
    400  1.1  christos {
    401  1.1  christos   int tid = s390_inferior_tid ();
    402  1.1  christos 
    403  1.1  christos   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
    404  1.1  christos     store_regs (regcache, tid, regnum);
    405  1.1  christos 
    406  1.1  christos   if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
    407  1.1  christos     store_fpregs (regcache, tid, regnum);
    408  1.1  christos 
    409  1.1  christos   /* S390_LAST_BREAK_REGNUM is read-only.  */
    410  1.1  christos 
    411  1.1  christos   if (have_regset_system_call)
    412  1.1  christos     if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
    413  1.1  christos       store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
    414  1.1  christos 		    s390_regmap_system_call);
    415  1.1  christos }
    416  1.1  christos 
    417  1.1  christos 
    418  1.1  christos /* Hardware-assisted watchpoint handling.  */
    419  1.1  christos 
    420  1.1  christos /* We maintain a list of all currently active watchpoints in order
    421  1.1  christos    to properly handle watchpoint removal.
    422  1.1  christos 
    423  1.1  christos    The only thing we actually need is the total address space area
    424  1.1  christos    spanned by the watchpoints.  */
    425  1.1  christos 
    426  1.1  christos struct watch_area
    427  1.1  christos {
    428  1.1  christos   struct watch_area *next;
    429  1.1  christos   CORE_ADDR lo_addr;
    430  1.1  christos   CORE_ADDR hi_addr;
    431  1.1  christos };
    432  1.1  christos 
    433  1.1  christos static struct watch_area *watch_base = NULL;
    434  1.1  christos 
    435  1.1  christos static int
    436  1.1  christos s390_stopped_by_watchpoint (void)
    437  1.1  christos {
    438  1.1  christos   per_lowcore_bits per_lowcore;
    439  1.1  christos   ptrace_area parea;
    440  1.1  christos   int result;
    441  1.1  christos 
    442  1.1  christos   /* Speed up common case.  */
    443  1.1  christos   if (!watch_base)
    444  1.1  christos     return 0;
    445  1.1  christos 
    446  1.1  christos   parea.len = sizeof (per_lowcore);
    447  1.1  christos   parea.process_addr = (addr_t) & per_lowcore;
    448  1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
    449  1.1  christos   if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0)
    450  1.1  christos     perror_with_name (_("Couldn't retrieve watchpoint status"));
    451  1.1  christos 
    452  1.1  christos   result = (per_lowcore.perc_storage_alteration == 1
    453  1.1  christos 	    && per_lowcore.perc_store_real_address == 0);
    454  1.1  christos 
    455  1.1  christos   if (result)
    456  1.1  christos     {
    457  1.1  christos       /* Do not report this watchpoint again.  */
    458  1.1  christos       memset (&per_lowcore, 0, sizeof (per_lowcore));
    459  1.1  christos       if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea) < 0)
    460  1.1  christos 	perror_with_name (_("Couldn't clear watchpoint status"));
    461  1.1  christos     }
    462  1.1  christos 
    463  1.1  christos   return result;
    464  1.1  christos }
    465  1.1  christos 
    466  1.1  christos static void
    467  1.1  christos s390_fix_watch_points (struct lwp_info *lp)
    468  1.1  christos {
    469  1.1  christos   int tid;
    470  1.1  christos 
    471  1.1  christos   per_struct per_info;
    472  1.1  christos   ptrace_area parea;
    473  1.1  christos 
    474  1.1  christos   CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
    475  1.1  christos   struct watch_area *area;
    476  1.1  christos 
    477  1.1  christos   tid = ptid_get_lwp (lp->ptid);
    478  1.1  christos   if (tid == 0)
    479  1.1  christos     tid = ptid_get_pid (lp->ptid);
    480  1.1  christos 
    481  1.1  christos   for (area = watch_base; area; area = area->next)
    482  1.1  christos     {
    483  1.1  christos       watch_lo_addr = min (watch_lo_addr, area->lo_addr);
    484  1.1  christos       watch_hi_addr = max (watch_hi_addr, area->hi_addr);
    485  1.1  christos     }
    486  1.1  christos 
    487  1.1  christos   parea.len = sizeof (per_info);
    488  1.1  christos   parea.process_addr = (addr_t) & per_info;
    489  1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
    490  1.1  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0)
    491  1.1  christos     perror_with_name (_("Couldn't retrieve watchpoint status"));
    492  1.1  christos 
    493  1.1  christos   if (watch_base)
    494  1.1  christos     {
    495  1.1  christos       per_info.control_regs.bits.em_storage_alteration = 1;
    496  1.1  christos       per_info.control_regs.bits.storage_alt_space_ctl = 1;
    497  1.1  christos     }
    498  1.1  christos   else
    499  1.1  christos     {
    500  1.1  christos       per_info.control_regs.bits.em_storage_alteration = 0;
    501  1.1  christos       per_info.control_regs.bits.storage_alt_space_ctl = 0;
    502  1.1  christos     }
    503  1.1  christos   per_info.starting_addr = watch_lo_addr;
    504  1.1  christos   per_info.ending_addr = watch_hi_addr;
    505  1.1  christos 
    506  1.1  christos   if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0)
    507  1.1  christos     perror_with_name (_("Couldn't modify watchpoint status"));
    508  1.1  christos }
    509  1.1  christos 
    510  1.1  christos static int
    511  1.1  christos s390_insert_watchpoint (CORE_ADDR addr, int len, int type,
    512  1.1  christos 			struct expression *cond)
    513  1.1  christos {
    514  1.1  christos   struct lwp_info *lp;
    515  1.1  christos   struct watch_area *area = xmalloc (sizeof (struct watch_area));
    516  1.1  christos 
    517  1.1  christos   if (!area)
    518  1.1  christos     return -1;
    519  1.1  christos 
    520  1.1  christos   area->lo_addr = addr;
    521  1.1  christos   area->hi_addr = addr + len - 1;
    522  1.1  christos 
    523  1.1  christos   area->next = watch_base;
    524  1.1  christos   watch_base = area;
    525  1.1  christos 
    526  1.1  christos   ALL_LWPS (lp)
    527  1.1  christos     s390_fix_watch_points (lp);
    528  1.1  christos   return 0;
    529  1.1  christos }
    530  1.1  christos 
    531  1.1  christos static int
    532  1.1  christos s390_remove_watchpoint (CORE_ADDR addr, int len, int type,
    533  1.1  christos 			struct expression *cond)
    534  1.1  christos {
    535  1.1  christos   struct lwp_info *lp;
    536  1.1  christos   struct watch_area *area, **parea;
    537  1.1  christos 
    538  1.1  christos   for (parea = &watch_base; *parea; parea = &(*parea)->next)
    539  1.1  christos     if ((*parea)->lo_addr == addr
    540  1.1  christos 	&& (*parea)->hi_addr == addr + len - 1)
    541  1.1  christos       break;
    542  1.1  christos 
    543  1.1  christos   if (!*parea)
    544  1.1  christos     {
    545  1.1  christos       fprintf_unfiltered (gdb_stderr,
    546  1.1  christos 			  "Attempt to remove nonexistent watchpoint.\n");
    547  1.1  christos       return -1;
    548  1.1  christos     }
    549  1.1  christos 
    550  1.1  christos   area = *parea;
    551  1.1  christos   *parea = area->next;
    552  1.1  christos   xfree (area);
    553  1.1  christos 
    554  1.1  christos   ALL_LWPS (lp)
    555  1.1  christos     s390_fix_watch_points (lp);
    556  1.1  christos   return 0;
    557  1.1  christos }
    558  1.1  christos 
    559  1.1  christos static int
    560  1.1  christos s390_can_use_hw_breakpoint (int type, int cnt, int othertype)
    561  1.1  christos {
    562  1.1  christos   return type == bp_hardware_watchpoint;
    563  1.1  christos }
    564  1.1  christos 
    565  1.1  christos static int
    566  1.1  christos s390_region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
    567  1.1  christos {
    568  1.1  christos   return 1;
    569  1.1  christos }
    570  1.1  christos 
    571  1.1  christos static int
    572  1.1  christos s390_target_wordsize (void)
    573  1.1  christos {
    574  1.1  christos   int wordsize = 4;
    575  1.1  christos 
    576  1.1  christos   /* Check for 64-bit inferior process.  This is the case when the host is
    577  1.1  christos      64-bit, and in addition bit 32 of the PSW mask is set.  */
    578  1.1  christos #ifdef __s390x__
    579  1.1  christos   long pswm;
    580  1.1  christos 
    581  1.1  christos   errno = 0;
    582  1.1  christos   pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
    583  1.1  christos   if (errno == 0 && (pswm & 0x100000000ul) != 0)
    584  1.1  christos     wordsize = 8;
    585  1.1  christos #endif
    586  1.1  christos 
    587  1.1  christos   return wordsize;
    588  1.1  christos }
    589  1.1  christos 
    590  1.1  christos static int
    591  1.1  christos s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
    592  1.1  christos 		 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
    593  1.1  christos {
    594  1.1  christos   int sizeof_auxv_field = s390_target_wordsize ();
    595  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
    596  1.1  christos   gdb_byte *ptr = *readptr;
    597  1.1  christos 
    598  1.1  christos   if (endptr == ptr)
    599  1.1  christos     return 0;
    600  1.1  christos 
    601  1.1  christos   if (endptr - ptr < sizeof_auxv_field * 2)
    602  1.1  christos     return -1;
    603  1.1  christos 
    604  1.1  christos   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
    605  1.1  christos   ptr += sizeof_auxv_field;
    606  1.1  christos   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
    607  1.1  christos   ptr += sizeof_auxv_field;
    608  1.1  christos 
    609  1.1  christos   *readptr = ptr;
    610  1.1  christos   return 1;
    611  1.1  christos }
    612  1.1  christos 
    613  1.1  christos #ifdef __s390x__
    614  1.1  christos static unsigned long
    615  1.1  christos s390_get_hwcap (void)
    616  1.1  christos {
    617  1.1  christos   CORE_ADDR field;
    618  1.1  christos 
    619  1.1  christos   if (target_auxv_search (&current_target, AT_HWCAP, &field))
    620  1.1  christos     return (unsigned long) field;
    621  1.1  christos 
    622  1.1  christos   return 0;
    623  1.1  christos }
    624  1.1  christos #endif
    625  1.1  christos 
    626  1.1  christos static const struct target_desc *
    627  1.1  christos s390_read_description (struct target_ops *ops)
    628  1.1  christos {
    629  1.1  christos   int tid = s390_inferior_tid ();
    630  1.1  christos 
    631  1.1  christos   have_regset_last_break
    632  1.1  christos     = check_regset (tid, NT_S390_LAST_BREAK, 8);
    633  1.1  christos   have_regset_system_call
    634  1.1  christos     = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
    635  1.1  christos 
    636  1.1  christos #ifdef __s390x__
    637  1.1  christos   /* If GDB itself is compiled as 64-bit, we are running on a machine in
    638  1.1  christos      z/Architecture mode.  If the target is running in 64-bit addressing
    639  1.1  christos      mode, report s390x architecture.  If the target is running in 31-bit
    640  1.1  christos      addressing mode, but the kernel supports using 64-bit registers in
    641  1.1  christos      that mode, report s390 architecture with 64-bit GPRs.  */
    642  1.1  christos 
    643  1.1  christos   have_regset_tdb = (s390_get_hwcap () & HWCAP_S390_TE) ?
    644  1.1  christos     check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset) : 0;
    645  1.1  christos 
    646  1.1  christos   if (s390_target_wordsize () == 8)
    647  1.1  christos     return (have_regset_tdb ? tdesc_s390x_te_linux64 :
    648  1.1  christos 	    have_regset_system_call? tdesc_s390x_linux64v2 :
    649  1.1  christos 	    have_regset_last_break? tdesc_s390x_linux64v1 :
    650  1.1  christos 	    tdesc_s390x_linux64);
    651  1.1  christos 
    652  1.1  christos   if (s390_get_hwcap () & HWCAP_S390_HIGH_GPRS)
    653  1.1  christos     return (have_regset_tdb ? tdesc_s390_te_linux64 :
    654  1.1  christos 	    have_regset_system_call? tdesc_s390_linux64v2 :
    655  1.1  christos 	    have_regset_last_break? tdesc_s390_linux64v1 :
    656  1.1  christos 	    tdesc_s390_linux64);
    657  1.1  christos #endif
    658  1.1  christos 
    659  1.1  christos   /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
    660  1.1  christos      on a 64-bit kernel that does not support using 64-bit registers in 31-bit
    661  1.1  christos      mode, report s390 architecture with 32-bit GPRs.  */
    662  1.1  christos   return (have_regset_system_call? tdesc_s390_linux32v2 :
    663  1.1  christos 	  have_regset_last_break? tdesc_s390_linux32v1 :
    664  1.1  christos 	  tdesc_s390_linux32);
    665  1.1  christos }
    666  1.1  christos 
    667  1.1  christos void _initialize_s390_nat (void);
    668  1.1  christos 
    669  1.1  christos void
    670  1.1  christos _initialize_s390_nat (void)
    671  1.1  christos {
    672  1.1  christos   struct target_ops *t;
    673  1.1  christos 
    674  1.1  christos   /* Fill in the generic GNU/Linux methods.  */
    675  1.1  christos   t = linux_target ();
    676  1.1  christos 
    677  1.1  christos   /* Add our register access methods.  */
    678  1.1  christos   t->to_fetch_registers = s390_linux_fetch_inferior_registers;
    679  1.1  christos   t->to_store_registers = s390_linux_store_inferior_registers;
    680  1.1  christos 
    681  1.1  christos   /* Add our watchpoint methods.  */
    682  1.1  christos   t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint;
    683  1.1  christos   t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint;
    684  1.1  christos   t->to_have_continuable_watchpoint = 1;
    685  1.1  christos   t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint;
    686  1.1  christos   t->to_insert_watchpoint = s390_insert_watchpoint;
    687  1.1  christos   t->to_remove_watchpoint = s390_remove_watchpoint;
    688  1.1  christos 
    689  1.1  christos   /* Detect target architecture.  */
    690  1.1  christos   t->to_read_description = s390_read_description;
    691  1.1  christos   t->to_auxv_parse = s390_auxv_parse;
    692  1.1  christos 
    693  1.1  christos   /* Register the target.  */
    694  1.1  christos   linux_nat_add_target (t);
    695  1.1  christos   linux_nat_set_new_thread (t, s390_fix_watch_points);
    696  1.1  christos }
    697