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