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