Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* S390 native-dependent code for GDB, the GNU debugger.
      2  1.11  christos    Copyright (C) 2001-2024 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.12  christos #include "gdbsupport/gdb_vecs.h"
     23  1.11  christos #include "extract-store-integer.h"
     24   1.1  christos #include "regcache.h"
     25   1.1  christos #include "inferior.h"
     26   1.1  christos #include "target.h"
     27   1.1  christos #include "linux-nat.h"
     28   1.1  christos #include "auxv.h"
     29   1.1  christos #include "gregset.h"
     30   1.3  christos #include "regset.h"
     31   1.5  christos #include "nat/linux-ptrace.h"
     32  1.11  christos #include "cli/cli-cmds.h"
     33  1.10  christos #include "gdbarch.h"
     34   1.1  christos 
     35   1.8  christos #include "s390-tdep.h"
     36   1.1  christos #include "s390-linux-tdep.h"
     37   1.1  christos #include "elf/common.h"
     38   1.1  christos 
     39   1.1  christos #include <asm/ptrace.h>
     40   1.6  christos #include "nat/gdb_ptrace.h"
     41   1.1  christos #include <asm/types.h>
     42   1.1  christos #include <sys/procfs.h>
     43   1.1  christos #include <sys/ucontext.h>
     44   1.1  christos #include <elf.h>
     45   1.7  christos #include <algorithm>
     46   1.7  christos #include "inf-ptrace.h"
     47   1.9  christos #include "linux-tdep.h"
     48   1.1  christos 
     49   1.5  christos /* Per-thread arch-specific data.  */
     50   1.1  christos 
     51   1.5  christos struct arch_lwp_info
     52   1.5  christos {
     53   1.5  christos   /* Non-zero if the thread's PER info must be re-written.  */
     54   1.5  christos   int per_info_changed;
     55   1.5  christos };
     56   1.1  christos 
     57   1.1  christos static int have_regset_last_break = 0;
     58   1.1  christos static int have_regset_system_call = 0;
     59   1.1  christos static int have_regset_tdb = 0;
     60   1.5  christos static int have_regset_vxrs = 0;
     61   1.8  christos static int have_regset_gs = 0;
     62   1.1  christos 
     63   1.3  christos /* Register map for 32-bit executables running under a 64-bit
     64   1.3  christos    kernel.  */
     65   1.1  christos 
     66   1.1  christos #ifdef __s390x__
     67   1.3  christos static const struct regcache_map_entry s390_64_regmap_gregset[] =
     68   1.3  christos   {
     69   1.3  christos     /* Skip PSWM and PSWA, since they must be handled specially.  */
     70   1.3  christos     { 2, REGCACHE_MAP_SKIP, 8 },
     71   1.3  christos     { 1, S390_R0_UPPER_REGNUM, 4 }, { 1, S390_R0_REGNUM, 4 },
     72   1.3  christos     { 1, S390_R1_UPPER_REGNUM, 4 }, { 1, S390_R1_REGNUM, 4 },
     73   1.3  christos     { 1, S390_R2_UPPER_REGNUM, 4 }, { 1, S390_R2_REGNUM, 4 },
     74   1.3  christos     { 1, S390_R3_UPPER_REGNUM, 4 }, { 1, S390_R3_REGNUM, 4 },
     75   1.3  christos     { 1, S390_R4_UPPER_REGNUM, 4 }, { 1, S390_R4_REGNUM, 4 },
     76   1.3  christos     { 1, S390_R5_UPPER_REGNUM, 4 }, { 1, S390_R5_REGNUM, 4 },
     77   1.3  christos     { 1, S390_R6_UPPER_REGNUM, 4 }, { 1, S390_R6_REGNUM, 4 },
     78   1.3  christos     { 1, S390_R7_UPPER_REGNUM, 4 }, { 1, S390_R7_REGNUM, 4 },
     79   1.3  christos     { 1, S390_R8_UPPER_REGNUM, 4 }, { 1, S390_R8_REGNUM, 4 },
     80   1.3  christos     { 1, S390_R9_UPPER_REGNUM, 4 }, { 1, S390_R9_REGNUM, 4 },
     81   1.3  christos     { 1, S390_R10_UPPER_REGNUM, 4 }, { 1, S390_R10_REGNUM, 4 },
     82   1.3  christos     { 1, S390_R11_UPPER_REGNUM, 4 }, { 1, S390_R11_REGNUM, 4 },
     83   1.3  christos     { 1, S390_R12_UPPER_REGNUM, 4 }, { 1, S390_R12_REGNUM, 4 },
     84   1.3  christos     { 1, S390_R13_UPPER_REGNUM, 4 }, { 1, S390_R13_REGNUM, 4 },
     85   1.3  christos     { 1, S390_R14_UPPER_REGNUM, 4 }, { 1, S390_R14_REGNUM, 4 },
     86   1.3  christos     { 1, S390_R15_UPPER_REGNUM, 4 }, { 1, S390_R15_REGNUM, 4 },
     87   1.3  christos     { 16, S390_A0_REGNUM, 4 },
     88   1.3  christos     { 1, REGCACHE_MAP_SKIP, 4 }, { 1, S390_ORIG_R2_REGNUM, 4 },
     89   1.3  christos     { 0 }
     90   1.3  christos   };
     91   1.3  christos 
     92   1.3  christos static const struct regset s390_64_gregset =
     93   1.3  christos   {
     94   1.3  christos     s390_64_regmap_gregset,
     95   1.3  christos     regcache_supply_regset,
     96   1.3  christos     regcache_collect_regset
     97   1.3  christos   };
     98   1.3  christos 
     99   1.3  christos #define S390_PSWM_OFFSET 0
    100   1.3  christos #define S390_PSWA_OFFSET 8
    101   1.1  christos #endif
    102   1.1  christos 
    103   1.7  christos /* PER-event mask bits and PER control bits (CR9).  */
    104   1.7  christos 
    105   1.7  christos #define PER_BIT(n)			(1UL << (63 - (n)))
    106   1.7  christos #define PER_EVENT_BRANCH		PER_BIT (32)
    107   1.7  christos #define PER_EVENT_IFETCH		PER_BIT (33)
    108   1.7  christos #define PER_EVENT_STORE			PER_BIT (34)
    109   1.7  christos #define PER_EVENT_NULLIFICATION		PER_BIT (39)
    110   1.7  christos #define PER_CONTROL_BRANCH_ADDRESS	PER_BIT (40)
    111   1.7  christos #define PER_CONTROL_SUSPENSION		PER_BIT (41)
    112   1.7  christos #define PER_CONTROL_ALTERATION		PER_BIT (42)
    113   1.7  christos 
    114   1.8  christos class s390_linux_nat_target final : public linux_nat_target
    115   1.8  christos {
    116   1.8  christos public:
    117   1.8  christos   /* Add our register access methods.  */
    118   1.8  christos   void fetch_registers (struct regcache *, int) override;
    119   1.8  christos   void store_registers (struct regcache *, int) override;
    120   1.8  christos 
    121   1.8  christos   /* Add our watchpoint methods.  */
    122   1.8  christos   int can_use_hw_breakpoint (enum bptype, int, int) override;
    123   1.8  christos   int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
    124   1.8  christos     override;
    125   1.8  christos   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
    126   1.8  christos     override;
    127   1.8  christos   int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
    128   1.8  christos   bool stopped_by_watchpoint () override;
    129   1.8  christos   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
    130   1.8  christos 			 struct expression *) override;
    131   1.8  christos   int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
    132   1.8  christos 			 struct expression *) override;
    133   1.8  christos 
    134   1.8  christos   /* Detect target architecture.  */
    135   1.8  christos   const struct target_desc *read_description () override;
    136  1.10  christos   int auxv_parse (const gdb_byte **readptr,
    137  1.10  christos 		  const gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
    138   1.8  christos     override;
    139   1.8  christos 
    140   1.8  christos   /* Override linux_nat_target low methods.  */
    141   1.8  christos   void low_new_thread (struct lwp_info *lp) override;
    142   1.8  christos   void low_delete_thread (struct arch_lwp_info *lp) override;
    143   1.8  christos   void low_prepare_to_resume (struct lwp_info *lp) override;
    144   1.8  christos   void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
    145   1.8  christos   void low_forget_process (pid_t pid) override;
    146   1.8  christos };
    147   1.8  christos 
    148   1.8  christos static s390_linux_nat_target the_s390_linux_nat_target;
    149   1.7  christos 
    150   1.1  christos /* Fill GDB's register array with the general-purpose register values
    151   1.1  christos    in *REGP.
    152   1.1  christos 
    153   1.1  christos    When debugging a 32-bit executable running under a 64-bit kernel,
    154   1.1  christos    we have to fix up the 64-bit registers we get from the kernel to
    155   1.1  christos    make them look like 32-bit registers.  */
    156   1.1  christos 
    157   1.1  christos void
    158   1.1  christos supply_gregset (struct regcache *regcache, const gregset_t *regp)
    159   1.1  christos {
    160   1.1  christos #ifdef __s390x__
    161   1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
    162   1.1  christos   if (gdbarch_ptr_bit (gdbarch) == 32)
    163   1.1  christos     {
    164   1.1  christos       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    165   1.3  christos       ULONGEST pswm, pswa;
    166   1.1  christos       gdb_byte buf[4];
    167   1.1  christos 
    168   1.3  christos       regcache_supply_regset (&s390_64_gregset, regcache, -1,
    169   1.3  christos 			      regp, sizeof (gregset_t));
    170   1.3  christos       pswm = extract_unsigned_integer ((const gdb_byte *) regp
    171   1.3  christos 				       + S390_PSWM_OFFSET, 8, byte_order);
    172   1.3  christos       pswa = extract_unsigned_integer ((const gdb_byte *) regp
    173   1.3  christos 				       + S390_PSWA_OFFSET, 8, byte_order);
    174   1.1  christos       store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
    175   1.8  christos       regcache->raw_supply (S390_PSWM_REGNUM, buf);
    176   1.1  christos       store_unsigned_integer (buf, 4, byte_order,
    177   1.1  christos 			      (pswa & 0x7fffffff) | (pswm & 0x80000000));
    178   1.8  christos       regcache->raw_supply (S390_PSWA_REGNUM, buf);
    179   1.1  christos       return;
    180   1.1  christos     }
    181   1.1  christos #endif
    182   1.1  christos 
    183   1.3  christos   regcache_supply_regset (&s390_gregset, regcache, -1, regp,
    184   1.3  christos 			  sizeof (gregset_t));
    185   1.1  christos }
    186   1.1  christos 
    187   1.1  christos /* Fill register REGNO (if it is a general-purpose register) in
    188   1.1  christos    *REGP with the value in GDB's register array.  If REGNO is -1,
    189   1.1  christos    do this for all registers.  */
    190   1.1  christos 
    191   1.1  christos void
    192   1.1  christos fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
    193   1.1  christos {
    194   1.1  christos #ifdef __s390x__
    195   1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
    196   1.1  christos   if (gdbarch_ptr_bit (gdbarch) == 32)
    197   1.1  christos     {
    198   1.3  christos       regcache_collect_regset (&s390_64_gregset, regcache, regno,
    199   1.3  christos 			       regp, sizeof (gregset_t));
    200   1.1  christos 
    201   1.1  christos       if (regno == -1
    202   1.1  christos 	  || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
    203   1.1  christos 	{
    204   1.1  christos 	  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    205   1.1  christos 	  ULONGEST pswa, pswm;
    206   1.1  christos 	  gdb_byte buf[4];
    207   1.5  christos 	  gdb_byte *pswm_p = (gdb_byte *) regp + S390_PSWM_OFFSET;
    208   1.5  christos 	  gdb_byte *pswa_p = (gdb_byte *) regp + S390_PSWA_OFFSET;
    209   1.1  christos 
    210   1.5  christos 	  pswm = extract_unsigned_integer (pswm_p, 8, byte_order);
    211   1.1  christos 
    212   1.1  christos 	  if (regno == -1 || regno == S390_PSWM_REGNUM)
    213   1.5  christos 	    {
    214   1.5  christos 	      pswm &= 0x80000000;
    215   1.8  christos 	      regcache->raw_collect (S390_PSWM_REGNUM, buf);
    216   1.5  christos 	      pswm |= (extract_unsigned_integer (buf, 4, byte_order)
    217   1.5  christos 		       & 0xfff7ffff) << 32;
    218   1.5  christos 	    }
    219   1.5  christos 
    220   1.1  christos 	  if (regno == -1 || regno == S390_PSWA_REGNUM)
    221   1.5  christos 	    {
    222   1.8  christos 	      regcache->raw_collect (S390_PSWA_REGNUM, buf);
    223   1.5  christos 	      pswa = extract_unsigned_integer (buf, 4, byte_order);
    224   1.5  christos 	      pswm ^= (pswm ^ pswa) & 0x80000000;
    225   1.5  christos 	      pswa &= 0x7fffffff;
    226   1.5  christos 	      store_unsigned_integer (pswa_p, 8, byte_order, pswa);
    227   1.5  christos 	    }
    228   1.5  christos 
    229   1.5  christos 	  store_unsigned_integer (pswm_p, 8, byte_order, pswm);
    230   1.1  christos 	}
    231   1.1  christos       return;
    232   1.1  christos     }
    233   1.1  christos #endif
    234   1.1  christos 
    235   1.3  christos   regcache_collect_regset (&s390_gregset, regcache, regno, regp,
    236   1.3  christos 			   sizeof (gregset_t));
    237   1.1  christos }
    238   1.1  christos 
    239   1.1  christos /* Fill GDB's register array with the floating-point register values
    240   1.1  christos    in *REGP.  */
    241   1.1  christos void
    242   1.1  christos supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
    243   1.1  christos {
    244   1.3  christos   regcache_supply_regset (&s390_fpregset, regcache, -1, regp,
    245   1.3  christos 			  sizeof (fpregset_t));
    246   1.1  christos }
    247   1.1  christos 
    248   1.1  christos /* Fill register REGNO (if it is a general-purpose register) in
    249   1.1  christos    *REGP with the value in GDB's register array.  If REGNO is -1,
    250   1.1  christos    do this for all registers.  */
    251   1.1  christos void
    252   1.1  christos fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
    253   1.1  christos {
    254   1.3  christos   regcache_collect_regset (&s390_fpregset, regcache, regno, regp,
    255   1.3  christos 			   sizeof (fpregset_t));
    256   1.1  christos }
    257   1.1  christos 
    258   1.1  christos /* Find the TID for the current inferior thread to use with ptrace.  */
    259   1.1  christos static int
    260   1.1  christos s390_inferior_tid (void)
    261   1.1  christos {
    262   1.1  christos   /* GNU/Linux LWP ID's are process ID's.  */
    263   1.8  christos   int tid = inferior_ptid.lwp ();
    264   1.1  christos   if (tid == 0)
    265   1.8  christos     tid = inferior_ptid.pid (); /* Not a threaded program.  */
    266   1.1  christos 
    267   1.1  christos   return tid;
    268   1.1  christos }
    269   1.1  christos 
    270   1.1  christos /* Fetch all general-purpose registers from process/thread TID and
    271   1.1  christos    store their values in GDB's register cache.  */
    272   1.1  christos static void
    273   1.1  christos fetch_regs (struct regcache *regcache, int tid)
    274   1.1  christos {
    275   1.1  christos   gregset_t regs;
    276   1.1  christos   ptrace_area parea;
    277   1.1  christos 
    278   1.1  christos   parea.len = sizeof (regs);
    279   1.1  christos   parea.process_addr = (addr_t) &regs;
    280   1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, psw);
    281   1.6  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
    282   1.1  christos     perror_with_name (_("Couldn't get registers"));
    283   1.1  christos 
    284   1.1  christos   supply_gregset (regcache, (const gregset_t *) &regs);
    285   1.1  christos }
    286   1.1  christos 
    287   1.1  christos /* Store all valid general-purpose registers in GDB's register cache
    288   1.1  christos    into the process/thread specified by TID.  */
    289   1.1  christos static void
    290   1.1  christos store_regs (const struct regcache *regcache, int tid, int regnum)
    291   1.1  christos {
    292   1.1  christos   gregset_t regs;
    293   1.1  christos   ptrace_area parea;
    294   1.1  christos 
    295   1.1  christos   parea.len = sizeof (regs);
    296   1.1  christos   parea.process_addr = (addr_t) &regs;
    297   1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, psw);
    298   1.6  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
    299   1.1  christos     perror_with_name (_("Couldn't get registers"));
    300   1.1  christos 
    301   1.1  christos   fill_gregset (regcache, &regs, regnum);
    302   1.1  christos 
    303   1.6  christos   if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
    304   1.1  christos     perror_with_name (_("Couldn't write registers"));
    305   1.1  christos }
    306   1.1  christos 
    307   1.1  christos /* Fetch all floating-point registers from process/thread TID and store
    308   1.1  christos    their values in GDB's register cache.  */
    309   1.1  christos static void
    310   1.1  christos fetch_fpregs (struct regcache *regcache, int tid)
    311   1.1  christos {
    312   1.1  christos   fpregset_t fpregs;
    313   1.1  christos   ptrace_area parea;
    314   1.1  christos 
    315   1.1  christos   parea.len = sizeof (fpregs);
    316   1.1  christos   parea.process_addr = (addr_t) &fpregs;
    317   1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
    318   1.6  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
    319   1.1  christos     perror_with_name (_("Couldn't get floating point status"));
    320   1.1  christos 
    321   1.1  christos   supply_fpregset (regcache, (const fpregset_t *) &fpregs);
    322   1.1  christos }
    323   1.1  christos 
    324   1.1  christos /* Store all valid floating-point registers in GDB's register cache
    325   1.1  christos    into the process/thread specified by TID.  */
    326   1.1  christos static void
    327   1.1  christos store_fpregs (const struct regcache *regcache, int tid, int regnum)
    328   1.1  christos {
    329   1.1  christos   fpregset_t fpregs;
    330   1.1  christos   ptrace_area parea;
    331   1.1  christos 
    332   1.1  christos   parea.len = sizeof (fpregs);
    333   1.1  christos   parea.process_addr = (addr_t) &fpregs;
    334   1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
    335   1.6  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
    336   1.1  christos     perror_with_name (_("Couldn't get floating point status"));
    337   1.1  christos 
    338   1.1  christos   fill_fpregset (regcache, &fpregs, regnum);
    339   1.1  christos 
    340   1.6  christos   if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
    341   1.1  christos     perror_with_name (_("Couldn't write floating point status"));
    342   1.1  christos }
    343   1.1  christos 
    344   1.3  christos /* Fetch all registers in the kernel's register set whose number is
    345   1.3  christos    REGSET_ID, whose size is REGSIZE, and whose layout is described by
    346   1.3  christos    REGSET, from process/thread TID and store their values in GDB's
    347   1.3  christos    register cache.  */
    348   1.1  christos static void
    349   1.1  christos fetch_regset (struct regcache *regcache, int tid,
    350   1.3  christos 	      int regset_id, int regsize, const struct regset *regset)
    351   1.1  christos {
    352   1.6  christos   void *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.3  christos   if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
    359   1.1  christos     {
    360   1.1  christos       if (errno == ENODATA)
    361   1.3  christos 	regcache_supply_regset (regset, regcache, -1, NULL, regsize);
    362   1.1  christos       else
    363   1.1  christos 	perror_with_name (_("Couldn't get register set"));
    364   1.1  christos     }
    365   1.1  christos   else
    366   1.3  christos     regcache_supply_regset (regset, regcache, -1, buf, regsize);
    367   1.1  christos }
    368   1.1  christos 
    369   1.3  christos /* Store all registers in the kernel's register set whose number is
    370   1.3  christos    REGSET_ID, whose size is REGSIZE, and whose layout is described by
    371   1.3  christos    REGSET, from GDB's register cache back to process/thread TID.  */
    372   1.1  christos static void
    373   1.1  christos store_regset (struct regcache *regcache, int tid,
    374   1.3  christos 	      int regset_id, int regsize, const struct regset *regset)
    375   1.1  christos {
    376   1.6  christos   void *buf = alloca (regsize);
    377   1.1  christos   struct iovec iov;
    378   1.1  christos 
    379   1.1  christos   iov.iov_base = buf;
    380   1.1  christos   iov.iov_len = regsize;
    381   1.1  christos 
    382   1.3  christos   if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
    383   1.1  christos     perror_with_name (_("Couldn't get register set"));
    384   1.1  christos 
    385   1.3  christos   regcache_collect_regset (regset, regcache, -1, buf, regsize);
    386   1.1  christos 
    387   1.3  christos   if (ptrace (PTRACE_SETREGSET, tid, (long) regset_id, (long) &iov) < 0)
    388   1.1  christos     perror_with_name (_("Couldn't set register set"));
    389   1.1  christos }
    390   1.1  christos 
    391   1.1  christos /* Check whether the kernel provides a register set with number REGSET
    392   1.1  christos    of size REGSIZE for process/thread TID.  */
    393   1.1  christos static int
    394   1.1  christos check_regset (int tid, int regset, int regsize)
    395   1.1  christos {
    396   1.6  christos   void *buf = alloca (regsize);
    397   1.1  christos   struct iovec iov;
    398   1.1  christos 
    399   1.1  christos   iov.iov_base = buf;
    400   1.1  christos   iov.iov_len = regsize;
    401   1.1  christos 
    402   1.1  christos   if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
    403   1.1  christos       || errno == ENODATA)
    404   1.1  christos     return 1;
    405   1.1  christos   return 0;
    406   1.1  christos }
    407   1.1  christos 
    408   1.1  christos /* Fetch register REGNUM from the child process.  If REGNUM is -1, do
    409   1.1  christos    this for all registers.  */
    410   1.8  christos void
    411   1.8  christos s390_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
    412   1.1  christos {
    413   1.8  christos   pid_t tid = get_ptrace_pid (regcache->ptid ());
    414   1.1  christos 
    415   1.1  christos   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
    416   1.1  christos     fetch_regs (regcache, tid);
    417   1.1  christos 
    418   1.1  christos   if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
    419   1.1  christos     fetch_fpregs (regcache, tid);
    420   1.1  christos 
    421   1.1  christos   if (have_regset_last_break)
    422   1.1  christos     if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
    423   1.1  christos       fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
    424   1.8  christos 		    (gdbarch_ptr_bit (regcache->arch ()) == 32
    425   1.3  christos 		     ? &s390_last_break_regset : &s390x_last_break_regset));
    426   1.1  christos 
    427   1.1  christos   if (have_regset_system_call)
    428   1.1  christos     if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
    429   1.1  christos       fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
    430   1.3  christos 		    &s390_system_call_regset);
    431   1.1  christos 
    432   1.1  christos   if (have_regset_tdb)
    433   1.1  christos     if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum))
    434   1.1  christos       fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset,
    435   1.3  christos 		    &s390_tdb_regset);
    436   1.5  christos 
    437   1.5  christos   if (have_regset_vxrs)
    438   1.5  christos     {
    439   1.5  christos       if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM
    440   1.5  christos 			   && regnum <= S390_V15_LOWER_REGNUM))
    441   1.5  christos 	fetch_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8,
    442   1.5  christos 		      &s390_vxrs_low_regset);
    443   1.5  christos       if (regnum == -1 || (regnum >= S390_V16_REGNUM
    444   1.5  christos 			   && regnum <= S390_V31_REGNUM))
    445   1.5  christos 	fetch_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16,
    446   1.5  christos 		      &s390_vxrs_high_regset);
    447   1.5  christos     }
    448   1.8  christos 
    449   1.8  christos   if (have_regset_gs)
    450   1.8  christos     {
    451   1.8  christos       if (regnum == -1 || (regnum >= S390_GSD_REGNUM
    452   1.8  christos 			   && regnum <= S390_GSEPLA_REGNUM))
    453   1.8  christos 	fetch_regset (regcache, tid, NT_S390_GS_CB, 4 * 8,
    454   1.8  christos 		      &s390_gs_regset);
    455   1.8  christos       if (regnum == -1 || (regnum >= S390_BC_GSD_REGNUM
    456   1.8  christos 			   && regnum <= S390_BC_GSEPLA_REGNUM))
    457   1.8  christos 	fetch_regset (regcache, tid, NT_S390_GS_BC, 4 * 8,
    458   1.8  christos 		      &s390_gsbc_regset);
    459   1.8  christos     }
    460   1.1  christos }
    461   1.1  christos 
    462   1.1  christos /* Store register REGNUM back into the child process.  If REGNUM is
    463   1.1  christos    -1, do this for all registers.  */
    464   1.8  christos void
    465   1.8  christos s390_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
    466   1.1  christos {
    467   1.8  christos   pid_t tid = get_ptrace_pid (regcache->ptid ());
    468   1.1  christos 
    469   1.1  christos   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
    470   1.1  christos     store_regs (regcache, tid, regnum);
    471   1.1  christos 
    472   1.1  christos   if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
    473   1.1  christos     store_fpregs (regcache, tid, regnum);
    474   1.1  christos 
    475   1.1  christos   /* S390_LAST_BREAK_REGNUM is read-only.  */
    476   1.1  christos 
    477   1.1  christos   if (have_regset_system_call)
    478   1.1  christos     if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
    479   1.1  christos       store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
    480   1.3  christos 		    &s390_system_call_regset);
    481   1.5  christos 
    482   1.5  christos   if (have_regset_vxrs)
    483   1.5  christos     {
    484   1.5  christos       if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM
    485   1.5  christos 			   && regnum <= S390_V15_LOWER_REGNUM))
    486   1.5  christos 	store_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8,
    487   1.5  christos 		      &s390_vxrs_low_regset);
    488   1.5  christos       if (regnum == -1 || (regnum >= S390_V16_REGNUM
    489   1.5  christos 			   && regnum <= S390_V31_REGNUM))
    490   1.5  christos 	store_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16,
    491   1.5  christos 		      &s390_vxrs_high_regset);
    492   1.5  christos     }
    493   1.1  christos }
    494   1.1  christos 
    495   1.1  christos 
    496   1.1  christos /* Hardware-assisted watchpoint handling.  */
    497   1.1  christos 
    498   1.7  christos /* For each process we maintain a list of all currently active
    499   1.7  christos    watchpoints, in order to properly handle watchpoint removal.
    500   1.1  christos 
    501   1.1  christos    The only thing we actually need is the total address space area
    502   1.1  christos    spanned by the watchpoints.  */
    503   1.1  christos 
    504   1.9  christos struct watch_area
    505   1.1  christos {
    506   1.1  christos   CORE_ADDR lo_addr;
    507   1.1  christos   CORE_ADDR hi_addr;
    508   1.9  christos };
    509   1.7  christos 
    510   1.7  christos /* Hardware debug state.  */
    511   1.7  christos 
    512   1.7  christos struct s390_debug_reg_state
    513   1.7  christos {
    514   1.9  christos   std::vector<watch_area> watch_areas;
    515   1.9  christos   std::vector<watch_area> break_areas;
    516   1.1  christos };
    517   1.1  christos 
    518   1.7  christos /* Per-process data.  */
    519   1.7  christos 
    520   1.7  christos struct s390_process_info
    521   1.7  christos {
    522   1.9  christos   struct s390_process_info *next = nullptr;
    523   1.9  christos   pid_t pid = 0;
    524   1.7  christos   struct s390_debug_reg_state state;
    525   1.7  christos };
    526   1.7  christos 
    527   1.7  christos static struct s390_process_info *s390_process_list = NULL;
    528   1.7  christos 
    529   1.7  christos /* Find process data for process PID.  */
    530   1.7  christos 
    531   1.7  christos static struct s390_process_info *
    532   1.7  christos s390_find_process_pid (pid_t pid)
    533   1.7  christos {
    534   1.7  christos   struct s390_process_info *proc;
    535   1.7  christos 
    536   1.7  christos   for (proc = s390_process_list; proc; proc = proc->next)
    537   1.7  christos     if (proc->pid == pid)
    538   1.7  christos       return proc;
    539   1.7  christos 
    540   1.7  christos   return NULL;
    541   1.7  christos }
    542   1.7  christos 
    543   1.7  christos /* Add process data for process PID.  Returns newly allocated info
    544   1.7  christos    object.  */
    545   1.7  christos 
    546   1.7  christos static struct s390_process_info *
    547   1.7  christos s390_add_process (pid_t pid)
    548   1.7  christos {
    549   1.9  christos   struct s390_process_info *proc = new struct s390_process_info;
    550   1.7  christos 
    551   1.7  christos   proc->pid = pid;
    552   1.7  christos   proc->next = s390_process_list;
    553   1.7  christos   s390_process_list = proc;
    554   1.7  christos 
    555   1.7  christos   return proc;
    556   1.7  christos }
    557   1.7  christos 
    558   1.7  christos /* Get data specific info for process PID, creating it if necessary.
    559   1.7  christos    Never returns NULL.  */
    560   1.7  christos 
    561   1.7  christos static struct s390_process_info *
    562   1.7  christos s390_process_info_get (pid_t pid)
    563   1.7  christos {
    564   1.7  christos   struct s390_process_info *proc;
    565   1.7  christos 
    566   1.7  christos   proc = s390_find_process_pid (pid);
    567   1.7  christos   if (proc == NULL)
    568   1.7  christos     proc = s390_add_process (pid);
    569   1.7  christos 
    570   1.7  christos   return proc;
    571   1.7  christos }
    572   1.7  christos 
    573   1.7  christos /* Get hardware debug state for process PID.  */
    574   1.7  christos 
    575   1.7  christos static struct s390_debug_reg_state *
    576   1.7  christos s390_get_debug_reg_state (pid_t pid)
    577   1.7  christos {
    578   1.7  christos   return &s390_process_info_get (pid)->state;
    579   1.7  christos }
    580   1.7  christos 
    581   1.7  christos /* Called whenever GDB is no longer debugging process PID.  It deletes
    582   1.7  christos    data structures that keep track of hardware debug state.  */
    583   1.7  christos 
    584   1.8  christos void
    585   1.8  christos s390_linux_nat_target::low_forget_process (pid_t pid)
    586   1.7  christos {
    587   1.7  christos   struct s390_process_info *proc, **proc_link;
    588   1.7  christos 
    589   1.7  christos   proc = s390_process_list;
    590   1.7  christos   proc_link = &s390_process_list;
    591   1.7  christos 
    592   1.7  christos   while (proc != NULL)
    593   1.7  christos     {
    594   1.7  christos       if (proc->pid == pid)
    595   1.7  christos 	{
    596   1.7  christos 	  *proc_link = proc->next;
    597   1.9  christos 	  delete proc;
    598   1.7  christos 	  return;
    599   1.7  christos 	}
    600   1.7  christos 
    601   1.7  christos       proc_link = &proc->next;
    602   1.7  christos       proc = *proc_link;
    603   1.7  christos     }
    604   1.7  christos }
    605   1.7  christos 
    606   1.7  christos /* linux_nat_new_fork hook.   */
    607   1.7  christos 
    608   1.8  christos void
    609   1.8  christos s390_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid)
    610   1.7  christos {
    611   1.7  christos   pid_t parent_pid;
    612   1.7  christos   struct s390_debug_reg_state *parent_state;
    613   1.7  christos   struct s390_debug_reg_state *child_state;
    614   1.7  christos 
    615   1.7  christos   /* NULL means no watchpoint has ever been set in the parent.  In
    616   1.7  christos      that case, there's nothing to do.  */
    617   1.7  christos   if (lwp_arch_private_info (parent) == NULL)
    618   1.7  christos     return;
    619   1.7  christos 
    620   1.7  christos   /* GDB core assumes the child inherits the watchpoints/hw breakpoints of
    621   1.7  christos      the parent.  So copy the debug state from parent to child.  */
    622   1.7  christos 
    623   1.8  christos   parent_pid = parent->ptid.pid ();
    624   1.7  christos   parent_state = s390_get_debug_reg_state (parent_pid);
    625   1.7  christos   child_state = s390_get_debug_reg_state (child_pid);
    626   1.7  christos 
    627   1.9  christos   child_state->watch_areas = parent_state->watch_areas;
    628   1.9  christos   child_state->break_areas = parent_state->break_areas;
    629   1.7  christos }
    630   1.7  christos 
    631   1.7  christos /* Dump PER state.  */
    632   1.7  christos 
    633   1.7  christos static void
    634   1.7  christos s390_show_debug_regs (int tid, const char *where)
    635   1.7  christos {
    636   1.7  christos   per_struct per_info;
    637   1.7  christos   ptrace_area parea;
    638   1.7  christos 
    639   1.7  christos   parea.len = sizeof (per_info);
    640   1.7  christos   parea.process_addr = (addr_t) &per_info;
    641   1.7  christos   parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
    642   1.7  christos 
    643   1.7  christos   if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea, 0) < 0)
    644   1.7  christos     perror_with_name (_("Couldn't retrieve debug regs"));
    645   1.7  christos 
    646   1.7  christos   debug_printf ("PER (debug) state for %d -- %s\n"
    647   1.7  christos 		"  cr9-11: %lx %lx %lx\n"
    648   1.7  christos 		"  start, end: %lx %lx\n"
    649   1.7  christos 		"  code/ATMID: %x  address: %lx  PAID: %x\n",
    650   1.7  christos 		tid,
    651   1.7  christos 		where,
    652   1.7  christos 		per_info.control_regs.words.cr[0],
    653   1.7  christos 		per_info.control_regs.words.cr[1],
    654   1.7  christos 		per_info.control_regs.words.cr[2],
    655   1.7  christos 		per_info.starting_addr,
    656   1.7  christos 		per_info.ending_addr,
    657   1.7  christos 		per_info.lowcore.words.perc_atmid,
    658   1.7  christos 		per_info.lowcore.words.address,
    659   1.7  christos 		per_info.lowcore.words.access_id);
    660   1.7  christos }
    661   1.1  christos 
    662   1.8  christos bool
    663   1.8  christos s390_linux_nat_target::stopped_by_watchpoint ()
    664   1.1  christos {
    665   1.7  christos   struct s390_debug_reg_state *state
    666   1.8  christos     = s390_get_debug_reg_state (inferior_ptid.pid ());
    667   1.1  christos   per_lowcore_bits per_lowcore;
    668   1.1  christos   ptrace_area parea;
    669   1.1  christos 
    670   1.7  christos   if (show_debug_regs)
    671   1.7  christos     s390_show_debug_regs (s390_inferior_tid (), "stop");
    672   1.7  christos 
    673   1.1  christos   /* Speed up common case.  */
    674   1.9  christos   if (state->watch_areas.empty ())
    675   1.8  christos     return false;
    676   1.1  christos 
    677  1.10  christos   siginfo_t siginfo;
    678  1.10  christos   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
    679  1.10  christos     return false;
    680  1.10  christos   if (siginfo.si_signo != SIGTRAP
    681  1.10  christos       || (siginfo.si_code & 0xffff) != TRAP_HWBKPT)
    682  1.10  christos     return false;
    683  1.10  christos 
    684   1.1  christos   parea.len = sizeof (per_lowcore);
    685   1.1  christos   parea.process_addr = (addr_t) & per_lowcore;
    686   1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
    687   1.6  christos   if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea, 0) < 0)
    688   1.1  christos     perror_with_name (_("Couldn't retrieve watchpoint status"));
    689   1.1  christos 
    690   1.8  christos   bool result = (per_lowcore.perc_storage_alteration == 1
    691   1.8  christos 		 && per_lowcore.perc_store_real_address == 0);
    692   1.1  christos 
    693   1.1  christos   return result;
    694   1.1  christos }
    695   1.1  christos 
    696   1.5  christos /* Each time before resuming a thread, update its PER info.  */
    697   1.5  christos 
    698   1.8  christos void
    699   1.8  christos s390_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
    700   1.1  christos {
    701   1.1  christos   int tid;
    702   1.8  christos   pid_t pid = ptid_of_lwp (lp).pid ();
    703   1.1  christos 
    704   1.1  christos   per_struct per_info;
    705   1.1  christos   ptrace_area parea;
    706   1.1  christos 
    707   1.1  christos   CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
    708   1.7  christos   struct arch_lwp_info *lp_priv = lwp_arch_private_info (lp);
    709   1.7  christos   struct s390_debug_reg_state *state = s390_get_debug_reg_state (pid);
    710   1.7  christos   int step = lwp_is_stepping (lp);
    711   1.1  christos 
    712   1.7  christos   /* Nothing to do if there was never any PER info for this thread.  */
    713   1.7  christos   if (lp_priv == NULL)
    714   1.5  christos     return;
    715   1.5  christos 
    716   1.7  christos   /* If PER info has changed, update it.  When single-stepping, disable
    717   1.7  christos      hardware breakpoints (if any).  Otherwise we're done.  */
    718   1.7  christos   if (!lp_priv->per_info_changed)
    719   1.7  christos     {
    720   1.9  christos       if (!step || state->break_areas.empty ())
    721   1.7  christos 	return;
    722   1.7  christos     }
    723   1.7  christos 
    724   1.7  christos   lp_priv->per_info_changed = 0;
    725   1.5  christos 
    726   1.8  christos   tid = ptid_of_lwp (lp).lwp ();
    727   1.1  christos   if (tid == 0)
    728   1.7  christos     tid = pid;
    729   1.1  christos 
    730   1.1  christos   parea.len = sizeof (per_info);
    731   1.1  christos   parea.process_addr = (addr_t) & per_info;
    732   1.1  christos   parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
    733   1.1  christos 
    734   1.7  christos   /* Clear PER info, but adjust the single_step field (used by older
    735   1.7  christos      kernels only).  */
    736   1.7  christos   memset (&per_info, 0, sizeof (per_info));
    737   1.7  christos   per_info.single_step = (step != 0);
    738   1.7  christos 
    739   1.9  christos   if (!state->watch_areas.empty ())
    740   1.1  christos     {
    741   1.9  christos       for (const auto &area : state->watch_areas)
    742   1.7  christos 	{
    743   1.9  christos 	  watch_lo_addr = std::min (watch_lo_addr, area.lo_addr);
    744   1.9  christos 	  watch_hi_addr = std::max (watch_hi_addr, area.hi_addr);
    745   1.7  christos 	}
    746   1.7  christos 
    747   1.7  christos       /* Enable storage-alteration events.  */
    748   1.7  christos       per_info.control_regs.words.cr[0] |= (PER_EVENT_STORE
    749   1.7  christos 					    | PER_CONTROL_ALTERATION);
    750   1.1  christos     }
    751   1.7  christos 
    752   1.9  christos   if (!state->break_areas.empty ())
    753   1.1  christos     {
    754   1.7  christos       /* Don't install hardware breakpoints while single-stepping, since
    755   1.7  christos 	 our PER settings (e.g. the nullification bit) might then conflict
    756   1.7  christos 	 with the kernel's.  But re-install them afterwards.  */
    757   1.7  christos       if (step)
    758   1.7  christos 	lp_priv->per_info_changed = 1;
    759   1.7  christos       else
    760   1.7  christos 	{
    761   1.9  christos 	  for (const auto &area : state->break_areas)
    762   1.7  christos 	    {
    763   1.9  christos 	      watch_lo_addr = std::min (watch_lo_addr, area.lo_addr);
    764   1.9  christos 	      watch_hi_addr = std::max (watch_hi_addr, area.hi_addr);
    765   1.7  christos 	    }
    766   1.7  christos 
    767   1.7  christos 	  /* If there's just one breakpoint, enable instruction-fetching
    768   1.7  christos 	     nullification events for the breakpoint address (fast).
    769   1.7  christos 	     Otherwise stop after any instruction within the PER area and
    770   1.7  christos 	     after any branch into it (slow).  */
    771   1.7  christos 	  if (watch_hi_addr == watch_lo_addr)
    772   1.7  christos 	    per_info.control_regs.words.cr[0] |= (PER_EVENT_NULLIFICATION
    773   1.7  christos 						  | PER_EVENT_IFETCH);
    774   1.7  christos 	  else
    775   1.7  christos 	    {
    776   1.7  christos 	      /* The PER area must include the instruction before the
    777   1.7  christos 		 first breakpoint address.  */
    778   1.7  christos 	      watch_lo_addr = watch_lo_addr > 6 ? watch_lo_addr - 6 : 0;
    779   1.7  christos 	      per_info.control_regs.words.cr[0]
    780   1.7  christos 		|= (PER_EVENT_BRANCH
    781   1.7  christos 		    | PER_EVENT_IFETCH
    782   1.7  christos 		    | PER_CONTROL_BRANCH_ADDRESS);
    783   1.7  christos 	    }
    784   1.7  christos 	}
    785   1.1  christos     }
    786   1.1  christos   per_info.starting_addr = watch_lo_addr;
    787   1.1  christos   per_info.ending_addr = watch_hi_addr;
    788   1.1  christos 
    789   1.6  christos   if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea, 0) < 0)
    790   1.1  christos     perror_with_name (_("Couldn't modify watchpoint status"));
    791   1.7  christos 
    792   1.7  christos   if (show_debug_regs)
    793   1.7  christos     s390_show_debug_regs (tid, "resume");
    794   1.1  christos }
    795   1.1  christos 
    796   1.7  christos /* Mark the PER info as changed, so the next resume will update it.  */
    797   1.5  christos 
    798   1.5  christos static void
    799   1.7  christos s390_mark_per_info_changed (struct lwp_info *lp)
    800   1.5  christos {
    801   1.7  christos   if (lwp_arch_private_info (lp) == NULL)
    802   1.7  christos     lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
    803   1.5  christos 
    804   1.7  christos   lwp_arch_private_info (lp)->per_info_changed = 1;
    805   1.5  christos }
    806   1.5  christos 
    807   1.5  christos /* When attaching to a new thread, mark its PER info as changed.  */
    808   1.5  christos 
    809   1.8  christos void
    810   1.8  christos s390_linux_nat_target::low_new_thread (struct lwp_info *lp)
    811   1.5  christos {
    812   1.7  christos   s390_mark_per_info_changed (lp);
    813   1.5  christos }
    814   1.5  christos 
    815   1.8  christos /* Function to call when a thread is being deleted.  */
    816   1.8  christos 
    817   1.8  christos void
    818   1.8  christos s390_linux_nat_target::low_delete_thread (struct arch_lwp_info *arch_lwp)
    819   1.8  christos {
    820   1.8  christos   xfree (arch_lwp);
    821   1.8  christos }
    822   1.8  christos 
    823   1.7  christos /* Iterator callback for s390_refresh_per_info.  */
    824   1.7  christos 
    825   1.1  christos static int
    826   1.9  christos s390_refresh_per_info_cb (struct lwp_info *lp)
    827   1.1  christos {
    828   1.7  christos   s390_mark_per_info_changed (lp);
    829   1.1  christos 
    830   1.7  christos   if (!lwp_is_stopped (lp))
    831   1.7  christos     linux_stop_lwp (lp);
    832   1.7  christos   return 0;
    833   1.7  christos }
    834   1.1  christos 
    835   1.7  christos /* Make sure that threads are stopped and mark PER info as changed.  */
    836   1.1  christos 
    837   1.7  christos static int
    838   1.7  christos s390_refresh_per_info (void)
    839   1.7  christos {
    840   1.8  christos   ptid_t pid_ptid = ptid_t (current_lwp_ptid ().pid ());
    841   1.1  christos 
    842   1.9  christos   iterate_over_lwps (pid_ptid, s390_refresh_per_info_cb);
    843   1.1  christos   return 0;
    844   1.1  christos }
    845   1.1  christos 
    846   1.8  christos int
    847   1.8  christos s390_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
    848   1.8  christos 					  enum target_hw_bp_type type,
    849   1.8  christos 					  struct expression *cond)
    850   1.1  christos {
    851   1.9  christos   watch_area area;
    852   1.7  christos   struct s390_debug_reg_state *state
    853   1.8  christos     = s390_get_debug_reg_state (inferior_ptid.pid ());
    854   1.7  christos 
    855   1.7  christos   area.lo_addr = addr;
    856   1.7  christos   area.hi_addr = addr + len - 1;
    857   1.9  christos   state->watch_areas.push_back (area);
    858   1.1  christos 
    859   1.7  christos   return s390_refresh_per_info ();
    860   1.7  christos }
    861   1.1  christos 
    862   1.8  christos int
    863   1.8  christos s390_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
    864   1.8  christos 					  enum target_hw_bp_type type,
    865   1.8  christos 					  struct expression *cond)
    866   1.7  christos {
    867   1.7  christos   unsigned ix;
    868   1.7  christos   struct s390_debug_reg_state *state
    869   1.8  christos     = s390_get_debug_reg_state (inferior_ptid.pid ());
    870   1.7  christos 
    871   1.9  christos   for (ix = 0; ix < state->watch_areas.size (); ix++)
    872   1.1  christos     {
    873   1.9  christos       watch_area &area = state->watch_areas[ix];
    874   1.9  christos       if (area.lo_addr == addr && area.hi_addr == addr + len - 1)
    875   1.7  christos 	{
    876   1.9  christos 	  unordered_remove (state->watch_areas, ix);
    877   1.7  christos 	  return s390_refresh_per_info ();
    878   1.7  christos 	}
    879   1.1  christos     }
    880   1.1  christos 
    881  1.10  christos   gdb_printf (gdb_stderr,
    882  1.10  christos 	      "Attempt to remove nonexistent watchpoint.\n");
    883   1.7  christos   return -1;
    884   1.7  christos }
    885   1.7  christos 
    886   1.7  christos /* Implement the "can_use_hw_breakpoint" target_ops method. */
    887   1.1  christos 
    888   1.8  christos int
    889   1.8  christos s390_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
    890   1.8  christos 					      int cnt, int othertype)
    891   1.7  christos {
    892   1.7  christos   if (type == bp_hardware_watchpoint || type == bp_hardware_breakpoint)
    893   1.7  christos     return 1;
    894   1.1  christos   return 0;
    895   1.1  christos }
    896   1.1  christos 
    897   1.7  christos /* Implement the "insert_hw_breakpoint" target_ops method.  */
    898   1.7  christos 
    899   1.8  christos int
    900   1.8  christos s390_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
    901   1.8  christos 					     struct bp_target_info *bp_tgt)
    902   1.1  christos {
    903   1.9  christos   watch_area area;
    904   1.7  christos   struct s390_debug_reg_state *state;
    905   1.7  christos 
    906   1.7  christos   area.lo_addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
    907   1.7  christos   area.hi_addr = area.lo_addr;
    908   1.8  christos   state = s390_get_debug_reg_state (inferior_ptid.pid ());
    909   1.9  christos   state->break_areas.push_back (area);
    910   1.7  christos 
    911   1.7  christos   return s390_refresh_per_info ();
    912   1.7  christos }
    913   1.7  christos 
    914   1.7  christos /* Implement the "remove_hw_breakpoint" target_ops method.  */
    915   1.7  christos 
    916   1.8  christos int
    917   1.8  christos s390_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
    918   1.8  christos 					     struct bp_target_info *bp_tgt)
    919   1.7  christos {
    920   1.7  christos   unsigned ix;
    921   1.7  christos   struct s390_debug_reg_state *state;
    922   1.7  christos 
    923   1.8  christos   state = s390_get_debug_reg_state (inferior_ptid.pid ());
    924   1.9  christos   for (ix = 0; state->break_areas.size (); ix++)
    925   1.7  christos     {
    926   1.9  christos       watch_area &area = state->break_areas[ix];
    927   1.9  christos       if (area.lo_addr == bp_tgt->placed_address)
    928   1.7  christos 	{
    929   1.9  christos 	  unordered_remove (state->break_areas, ix);
    930   1.7  christos 	  return s390_refresh_per_info ();
    931   1.7  christos 	}
    932   1.7  christos     }
    933   1.7  christos 
    934  1.10  christos   gdb_printf (gdb_stderr,
    935  1.10  christos 	      "Attempt to remove nonexistent breakpoint.\n");
    936   1.7  christos   return -1;
    937   1.1  christos }
    938   1.1  christos 
    939   1.8  christos int
    940   1.8  christos s390_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
    941   1.1  christos {
    942   1.1  christos   return 1;
    943   1.1  christos }
    944   1.1  christos 
    945   1.1  christos static int
    946   1.1  christos s390_target_wordsize (void)
    947   1.1  christos {
    948   1.1  christos   int wordsize = 4;
    949   1.1  christos 
    950   1.1  christos   /* Check for 64-bit inferior process.  This is the case when the host is
    951   1.1  christos      64-bit, and in addition bit 32 of the PSW mask is set.  */
    952   1.1  christos #ifdef __s390x__
    953  1.11  christos   int tid = s390_inferior_tid ();
    954  1.11  christos   gdb_assert (tid != 0);
    955   1.1  christos   long pswm;
    956   1.1  christos 
    957   1.1  christos   errno = 0;
    958  1.11  christos   pswm = (long) ptrace (PTRACE_PEEKUSER, tid, PT_PSWMASK, 0);
    959   1.1  christos   if (errno == 0 && (pswm & 0x100000000ul) != 0)
    960   1.1  christos     wordsize = 8;
    961   1.1  christos #endif
    962   1.1  christos 
    963   1.1  christos   return wordsize;
    964   1.1  christos }
    965   1.1  christos 
    966   1.8  christos int
    967  1.10  christos s390_linux_nat_target::auxv_parse (const gdb_byte **readptr,
    968  1.10  christos 				   const gdb_byte *endptr, CORE_ADDR *typep,
    969   1.8  christos 				   CORE_ADDR *valp)
    970   1.1  christos {
    971  1.11  christos   gdb_assert (inferior_ptid != null_ptid);
    972   1.1  christos   int sizeof_auxv_field = s390_target_wordsize ();
    973  1.11  christos   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
    974  1.10  christos   const gdb_byte *ptr = *readptr;
    975   1.1  christos 
    976   1.1  christos   if (endptr == ptr)
    977   1.1  christos     return 0;
    978   1.1  christos 
    979   1.1  christos   if (endptr - ptr < sizeof_auxv_field * 2)
    980   1.1  christos     return -1;
    981   1.1  christos 
    982   1.1  christos   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
    983   1.1  christos   ptr += sizeof_auxv_field;
    984   1.1  christos   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
    985   1.1  christos   ptr += sizeof_auxv_field;
    986   1.1  christos 
    987   1.1  christos   *readptr = ptr;
    988   1.1  christos   return 1;
    989   1.1  christos }
    990   1.1  christos 
    991   1.8  christos const struct target_desc *
    992   1.8  christos s390_linux_nat_target::read_description ()
    993   1.1  christos {
    994  1.11  christos   if (inferior_ptid == null_ptid)
    995  1.11  christos     return this->beneath ()->read_description ();
    996  1.11  christos 
    997  1.10  christos   int tid = inferior_ptid.pid ();
    998   1.1  christos 
    999   1.1  christos   have_regset_last_break
   1000   1.1  christos     = check_regset (tid, NT_S390_LAST_BREAK, 8);
   1001   1.1  christos   have_regset_system_call
   1002   1.1  christos     = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
   1003   1.1  christos 
   1004   1.1  christos   /* If GDB itself is compiled as 64-bit, we are running on a machine in
   1005   1.1  christos      z/Architecture mode.  If the target is running in 64-bit addressing
   1006   1.1  christos      mode, report s390x architecture.  If the target is running in 31-bit
   1007   1.1  christos      addressing mode, but the kernel supports using 64-bit registers in
   1008   1.1  christos      that mode, report s390 architecture with 64-bit GPRs.  */
   1009   1.5  christos #ifdef __s390x__
   1010   1.5  christos   {
   1011  1.10  christos     CORE_ADDR hwcap = linux_get_hwcap ();
   1012   1.1  christos 
   1013   1.5  christos     have_regset_tdb = (hwcap & HWCAP_S390_TE)
   1014   1.5  christos       && check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset);
   1015   1.5  christos 
   1016   1.5  christos     have_regset_vxrs = (hwcap & HWCAP_S390_VX)
   1017   1.5  christos       && check_regset (tid, NT_S390_VXRS_LOW, 16 * 8)
   1018   1.5  christos       && check_regset (tid, NT_S390_VXRS_HIGH, 16 * 16);
   1019   1.5  christos 
   1020   1.8  christos     have_regset_gs = (hwcap & HWCAP_S390_GS)
   1021   1.8  christos       && check_regset (tid, NT_S390_GS_CB, 4 * 8)
   1022   1.8  christos       && check_regset (tid, NT_S390_GS_BC, 4 * 8);
   1023   1.8  christos 
   1024   1.5  christos     if (s390_target_wordsize () == 8)
   1025   1.8  christos       return (have_regset_gs ? tdesc_s390x_gs_linux64 :
   1026   1.8  christos 	      have_regset_vxrs ?
   1027   1.5  christos 	      (have_regset_tdb ? tdesc_s390x_tevx_linux64 :
   1028   1.5  christos 	       tdesc_s390x_vx_linux64) :
   1029   1.5  christos 	      have_regset_tdb ? tdesc_s390x_te_linux64 :
   1030   1.5  christos 	      have_regset_system_call ? tdesc_s390x_linux64v2 :
   1031   1.5  christos 	      have_regset_last_break ? tdesc_s390x_linux64v1 :
   1032   1.5  christos 	      tdesc_s390x_linux64);
   1033   1.5  christos 
   1034   1.5  christos     if (hwcap & HWCAP_S390_HIGH_GPRS)
   1035   1.8  christos       return (have_regset_gs ? tdesc_s390_gs_linux64 :
   1036   1.8  christos 	      have_regset_vxrs ?
   1037   1.5  christos 	      (have_regset_tdb ? tdesc_s390_tevx_linux64 :
   1038   1.5  christos 	       tdesc_s390_vx_linux64) :
   1039   1.5  christos 	      have_regset_tdb ? tdesc_s390_te_linux64 :
   1040   1.5  christos 	      have_regset_system_call ? tdesc_s390_linux64v2 :
   1041   1.5  christos 	      have_regset_last_break ? tdesc_s390_linux64v1 :
   1042   1.5  christos 	      tdesc_s390_linux64);
   1043   1.5  christos   }
   1044   1.1  christos #endif
   1045   1.1  christos 
   1046   1.1  christos   /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
   1047   1.1  christos      on a 64-bit kernel that does not support using 64-bit registers in 31-bit
   1048   1.1  christos      mode, report s390 architecture with 32-bit GPRs.  */
   1049   1.1  christos   return (have_regset_system_call? tdesc_s390_linux32v2 :
   1050   1.1  christos 	  have_regset_last_break? tdesc_s390_linux32v1 :
   1051   1.1  christos 	  tdesc_s390_linux32);
   1052   1.1  christos }
   1053   1.1  christos 
   1054   1.9  christos void _initialize_s390_nat ();
   1055   1.1  christos void
   1056   1.9  christos _initialize_s390_nat ()
   1057   1.1  christos {
   1058   1.1  christos   /* Register the target.  */
   1059   1.8  christos   linux_target = &the_s390_linux_nat_target;
   1060   1.8  christos   add_inf_child_target (&the_s390_linux_nat_target);
   1061   1.7  christos 
   1062   1.7  christos   /* A maintenance command to enable showing the PER state.  */
   1063   1.7  christos   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
   1064   1.7  christos 			   &show_debug_regs, _("\
   1065   1.7  christos Set whether to show the PER (debug) hardware state."), _("\
   1066   1.7  christos Show whether to show the PER (debug) hardware state."), _("\
   1067   1.7  christos Use \"on\" to enable, \"off\" to disable.\n\
   1068   1.7  christos If enabled, the PER state is shown after it is changed by GDB,\n\
   1069   1.7  christos and when the inferior triggers a breakpoint or watchpoint."),
   1070   1.7  christos 			   NULL,
   1071   1.7  christos 			   NULL,
   1072   1.7  christos 			   &maintenance_set_cmdlist,
   1073   1.7  christos 			   &maintenance_show_cmdlist);
   1074   1.1  christos }
   1075