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