gdb/s390-linux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for GDB, the GNU debugger.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  4.    for IBM Deutschland Entwicklung GmbH, IBM Corporation.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "arch-utils.h"
  18. #include "frame.h"
  19. #include "inferior.h"
  20. #include "infrun.h"
  21. #include "symtab.h"
  22. #include "target.h"
  23. #include "gdbcore.h"
  24. #include "gdbcmd.h"
  25. #include "objfiles.h"
  26. #include "floatformat.h"
  27. #include "regcache.h"
  28. #include "trad-frame.h"
  29. #include "frame-base.h"
  30. #include "frame-unwind.h"
  31. #include "dwarf2-frame.h"
  32. #include "reggroups.h"
  33. #include "regset.h"
  34. #include "value.h"
  35. #include "dis-asm.h"
  36. #include "solib-svr4.h"
  37. #include "prologue-value.h"
  38. #include "linux-tdep.h"
  39. #include "s390-linux-tdep.h"
  40. #include "auxv.h"
  41. #include "xml-syscall.h"

  42. #include "stap-probe.h"
  43. #include "ax.h"
  44. #include "ax-gdb.h"
  45. #include "user-regs.h"
  46. #include "cli/cli-utils.h"
  47. #include <ctype.h>
  48. #include "elf/common.h"

  49. #include "features/s390-linux32.c"
  50. #include "features/s390-linux32v1.c"
  51. #include "features/s390-linux32v2.c"
  52. #include "features/s390-linux64.c"
  53. #include "features/s390-linux64v1.c"
  54. #include "features/s390-linux64v2.c"
  55. #include "features/s390-te-linux64.c"
  56. #include "features/s390x-linux64.c"
  57. #include "features/s390x-linux64v1.c"
  58. #include "features/s390x-linux64v2.c"
  59. #include "features/s390x-te-linux64.c"

  60. #define XML_SYSCALL_FILENAME_S390 "syscalls/s390-linux.xml"
  61. #define XML_SYSCALL_FILENAME_S390X "syscalls/s390x-linux.xml"

  62. /* The tdep structure.  */

  63. struct gdbarch_tdep
  64. {
  65.   /* ABI version.  */
  66.   enum { ABI_LINUX_S390, ABI_LINUX_ZSERIES } abi;

  67.   /* Pseudo register numbers.  */
  68.   int gpr_full_regnum;
  69.   int pc_regnum;
  70.   int cc_regnum;

  71.   int have_linux_v1;
  72.   int have_linux_v2;
  73.   int have_tdb;
  74. };


  75. /* ABI call-saved register information.  */

  76. static int
  77. s390_register_call_saved (struct gdbarch *gdbarch, int regnum)
  78. {
  79.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  80.   switch (tdep->abi)
  81.     {
  82.     case ABI_LINUX_S390:
  83.       if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
  84.           || regnum == S390_F4_REGNUM || regnum == S390_F6_REGNUM
  85.           || regnum == S390_A0_REGNUM)
  86.         return 1;

  87.       break;

  88.     case ABI_LINUX_ZSERIES:
  89.       if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
  90.           || (regnum >= S390_F8_REGNUM && regnum <= S390_F15_REGNUM)
  91.           || (regnum >= S390_A0_REGNUM && regnum <= S390_A1_REGNUM))
  92.         return 1;

  93.       break;
  94.     }

  95.   return 0;
  96. }

  97. static int
  98. s390_cannot_store_register (struct gdbarch *gdbarch, int regnum)
  99. {
  100.   /* The last-break address is read-only.  */
  101.   return regnum == S390_LAST_BREAK_REGNUM;
  102. }

  103. static void
  104. s390_write_pc (struct regcache *regcache, CORE_ADDR pc)
  105. {
  106.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  107.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  108.   regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);

  109.   /* Set special SYSTEM_CALL register to 0 to prevent the kernel from
  110.      messing with the PC we just installed, if we happen to be within
  111.      an interrupted system call that the kernel wants to restart.

  112.      Note that after we return from the dummy call, the SYSTEM_CALL and
  113.      ORIG_R2 registers will be automatically restored, and the kernel
  114.      continues to restart the system call at this point.  */
  115.   if (register_size (gdbarch, S390_SYSTEM_CALL_REGNUM) > 0)
  116.     regcache_cooked_write_unsigned (regcache, S390_SYSTEM_CALL_REGNUM, 0);
  117. }


  118. /* DWARF Register Mapping.  */

  119. static const short s390_dwarf_regmap[] =
  120. {
  121.   /* General Purpose Registers.  */
  122.   S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
  123.   S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
  124.   S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
  125.   S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,

  126.   /* Floating Point Registers.  */
  127.   S390_F0_REGNUM, S390_F2_REGNUM, S390_F4_REGNUM, S390_F6_REGNUM,
  128.   S390_F1_REGNUM, S390_F3_REGNUM, S390_F5_REGNUM, S390_F7_REGNUM,
  129.   S390_F8_REGNUM, S390_F10_REGNUM, S390_F12_REGNUM, S390_F14_REGNUM,
  130.   S390_F9_REGNUM, S390_F11_REGNUM, S390_F13_REGNUM, S390_F15_REGNUM,

  131.   /* Control Registers (not mapped).  */
  132.   -1, -1, -1, -1, -1, -1, -1, -1,
  133.   -1, -1, -1, -1, -1, -1, -1, -1,

  134.   /* Access Registers.  */
  135.   S390_A0_REGNUM, S390_A1_REGNUM, S390_A2_REGNUM, S390_A3_REGNUM,
  136.   S390_A4_REGNUM, S390_A5_REGNUM, S390_A6_REGNUM, S390_A7_REGNUM,
  137.   S390_A8_REGNUM, S390_A9_REGNUM, S390_A10_REGNUM, S390_A11_REGNUM,
  138.   S390_A12_REGNUM, S390_A13_REGNUM, S390_A14_REGNUM, S390_A15_REGNUM,

  139.   /* Program Status Word.  */
  140.   S390_PSWM_REGNUM,
  141.   S390_PSWA_REGNUM,

  142.   /* GPR Lower Half Access.  */
  143.   S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
  144.   S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
  145.   S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
  146.   S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,

  147.   /* GNU/Linux-specific registers (not mapped).  */
  148.   -1, -1, -1,
  149. };

  150. /* Convert DWARF register number REG to the appropriate register
  151.    number used by GDB.  */
  152. static int
  153. s390_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  154. {
  155.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  156.   /* In a 32-on-64 debug scenario, debug info refers to the full 64-bit
  157.      GPRs.  Note that call frame information still refers to the 32-bit
  158.      lower halves, because s390_adjust_frame_regnum uses register numbers
  159.      66 .. 81 to access GPRs.  */
  160.   if (tdep->gpr_full_regnum != -1 && reg >= 0 && reg < 16)
  161.     return tdep->gpr_full_regnum + reg;

  162.   if (reg >= 0 && reg < ARRAY_SIZE (s390_dwarf_regmap))
  163.     return s390_dwarf_regmap[reg];

  164.   warning (_("Unmapped DWARF Register #%d encountered."), reg);
  165.   return -1;
  166. }

  167. /* Translate a .eh_frame register to DWARF register, or adjust a
  168.    .debug_frame register.  */
  169. static int
  170. s390_adjust_frame_regnum (struct gdbarch *gdbarch, int num, int eh_frame_p)
  171. {
  172.   /* See s390_dwarf_reg_to_regnum for comments.  */
  173.   return (num >= 0 && num < 16)? num + 66 : num;
  174. }


  175. /* Pseudo registers.  */

  176. static int
  177. regnum_is_gpr_full (struct gdbarch_tdep *tdep, int regnum)
  178. {
  179.   return (tdep->gpr_full_regnum != -1
  180.           && regnum >= tdep->gpr_full_regnum
  181.           && regnum <= tdep->gpr_full_regnum + 15);
  182. }

  183. static const char *
  184. s390_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
  185. {
  186.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  187.   if (regnum == tdep->pc_regnum)
  188.     return "pc";

  189.   if (regnum == tdep->cc_regnum)
  190.     return "cc";

  191.   if (regnum_is_gpr_full (tdep, regnum))
  192.     {
  193.       static const char *full_name[] = {
  194.         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  195.         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  196.       };
  197.       return full_name[regnum - tdep->gpr_full_regnum];
  198.     }

  199.   internal_error (__FILE__, __LINE__, _("invalid regnum"));
  200. }

  201. static struct type *
  202. s390_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  203. {
  204.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  205.   if (regnum == tdep->pc_regnum)
  206.     return builtin_type (gdbarch)->builtin_func_ptr;

  207.   if (regnum == tdep->cc_regnum)
  208.     return builtin_type (gdbarch)->builtin_int;

  209.   if (regnum_is_gpr_full (tdep, regnum))
  210.     return builtin_type (gdbarch)->builtin_uint64;

  211.   internal_error (__FILE__, __LINE__, _("invalid regnum"));
  212. }

  213. static enum register_status
  214. s390_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  215.                            int regnum, gdb_byte *buf)
  216. {
  217.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  218.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  219.   int regsize = register_size (gdbarch, regnum);
  220.   ULONGEST val;

  221.   if (regnum == tdep->pc_regnum)
  222.     {
  223.       enum register_status status;

  224.       status = regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &val);
  225.       if (status == REG_VALID)
  226.         {
  227.           if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
  228.             val &= 0x7fffffff;
  229.           store_unsigned_integer (buf, regsize, byte_order, val);
  230.         }
  231.       return status;
  232.     }

  233.   if (regnum == tdep->cc_regnum)
  234.     {
  235.       enum register_status status;

  236.       status = regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
  237.       if (status == REG_VALID)
  238.         {
  239.           if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
  240.             val = (val >> 12) & 3;
  241.           else
  242.             val = (val >> 44) & 3;
  243.           store_unsigned_integer (buf, regsize, byte_order, val);
  244.         }
  245.       return status;
  246.     }

  247.   if (regnum_is_gpr_full (tdep, regnum))
  248.     {
  249.       enum register_status status;
  250.       ULONGEST val_upper;

  251.       regnum -= tdep->gpr_full_regnum;

  252.       status = regcache_raw_read_unsigned (regcache, S390_R0_REGNUM + regnum, &val);
  253.       if (status == REG_VALID)
  254.         status = regcache_raw_read_unsigned (regcache, S390_R0_UPPER_REGNUM + regnum,
  255.                                              &val_upper);
  256.       if (status == REG_VALID)
  257.         {
  258.           val |= val_upper << 32;
  259.           store_unsigned_integer (buf, regsize, byte_order, val);
  260.         }
  261.       return status;
  262.     }

  263.   internal_error (__FILE__, __LINE__, _("invalid regnum"));
  264. }

  265. static void
  266. s390_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  267.                             int regnum, const gdb_byte *buf)
  268. {
  269.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  270.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  271.   int regsize = register_size (gdbarch, regnum);
  272.   ULONGEST val, psw;

  273.   if (regnum == tdep->pc_regnum)
  274.     {
  275.       val = extract_unsigned_integer (buf, regsize, byte_order);
  276.       if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
  277.         {
  278.           regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &psw);
  279.           val = (psw & 0x80000000) | (val & 0x7fffffff);
  280.         }
  281.       regcache_raw_write_unsigned (regcache, S390_PSWA_REGNUM, val);
  282.       return;
  283.     }

  284.   if (regnum == tdep->cc_regnum)
  285.     {
  286.       val = extract_unsigned_integer (buf, regsize, byte_order);
  287.       regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
  288.       if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
  289.         val = (psw & ~((ULONGEST)3 << 12)) | ((val & 3) << 12);
  290.       else
  291.         val = (psw & ~((ULONGEST)3 << 44)) | ((val & 3) << 44);
  292.       regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, val);
  293.       return;
  294.     }

  295.   if (regnum_is_gpr_full (tdep, regnum))
  296.     {
  297.       regnum -= tdep->gpr_full_regnum;
  298.       val = extract_unsigned_integer (buf, regsize, byte_order);
  299.       regcache_raw_write_unsigned (regcache, S390_R0_REGNUM + regnum,
  300.                                    val & 0xffffffff);
  301.       regcache_raw_write_unsigned (regcache, S390_R0_UPPER_REGNUM + regnum,
  302.                                    val >> 32);
  303.       return;
  304.     }

  305.   internal_error (__FILE__, __LINE__, _("invalid regnum"));
  306. }

  307. /* 'float' values are stored in the upper half of floating-point
  308.    registers, even though we are otherwise a big-endian platform.  */

  309. static struct value *
  310. s390_value_from_register (struct gdbarch *gdbarch, struct type *type,
  311.                           int regnum, struct frame_id frame_id)
  312. {
  313.   struct value *value = default_value_from_register (gdbarch, type,
  314.                                                      regnum, frame_id);
  315.   check_typedef (type);

  316.   if (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM
  317.       && TYPE_LENGTH (type) < 8)
  318.     set_value_offset (value, 0);

  319.   return value;
  320. }

  321. /* Register groups.  */

  322. static int
  323. s390_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  324.                                  struct reggroup *group)
  325. {
  326.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  327.   /* We usually save/restore the whole PSW, which includes PC and CC.
  328.      However, some older gdbservers may not support saving/restoring
  329.      the whole PSW yet, and will return an XML register description
  330.      excluding those from the save/restore register groups.  In those
  331.      cases, we still need to explicitly save/restore PC and CC in order
  332.      to push or pop frames.  Since this doesn't hurt anything if we
  333.      already save/restore the whole PSW (it's just redundant), we add
  334.      PC and CC at this point unconditionally.  */
  335.   if (group == save_reggroup || group == restore_reggroup)
  336.     return regnum == tdep->pc_regnum || regnum == tdep->cc_regnum;

  337.   return default_register_reggroup_p (gdbarch, regnum, group);
  338. }


  339. /* Maps for register sets.  */

  340. static const struct regcache_map_entry s390_gregmap[] =
  341.   {
  342.     { 1, S390_PSWM_REGNUM },
  343.     { 1, S390_PSWA_REGNUM },
  344.     { 16, S390_R0_REGNUM },
  345.     { 16, S390_A0_REGNUM },
  346.     { 1, S390_ORIG_R2_REGNUM },
  347.     { 0 }
  348.   };

  349. static const struct regcache_map_entry s390_fpregmap[] =
  350.   {
  351.     { 1, S390_FPC_REGNUM, 8 },
  352.     { 16, S390_F0_REGNUM, 8 },
  353.     { 0 }
  354.   };

  355. static const struct regcache_map_entry s390_regmap_upper[] =
  356.   {
  357.     { 16, S390_R0_UPPER_REGNUM, 4 },
  358.     { 0 }
  359.   };

  360. static const struct regcache_map_entry s390_regmap_last_break[] =
  361.   {
  362.     { 1, REGCACHE_MAP_SKIP, 4 },
  363.     { 1, S390_LAST_BREAK_REGNUM, 4 },
  364.     { 0 }
  365.   };

  366. static const struct regcache_map_entry s390x_regmap_last_break[] =
  367.   {
  368.     { 1, S390_LAST_BREAK_REGNUM, 8 },
  369.     { 0 }
  370.   };

  371. static const struct regcache_map_entry s390_regmap_system_call[] =
  372.   {
  373.     { 1, S390_SYSTEM_CALL_REGNUM, 4 },
  374.     { 0 }
  375.   };

  376. static const struct regcache_map_entry s390_regmap_tdb[] =
  377.   {
  378.     { 1, S390_TDB_DWORD0_REGNUM, 8 },
  379.     { 1, S390_TDB_ABORT_CODE_REGNUM, 8 },
  380.     { 1, S390_TDB_CONFLICT_TOKEN_REGNUM, 8 },
  381.     { 1, S390_TDB_ATIA_REGNUM, 8 },
  382.     { 12, REGCACHE_MAP_SKIP, 8 },
  383.     { 16, S390_TDB_R0_REGNUM, 8 },
  384.     { 0 }
  385.   };


  386. /* Supply the TDB regset.  Like regcache_supply_regset, but invalidate
  387.    the TDB registers unless the TDB format field is valid.  */

  388. static void
  389. s390_supply_tdb_regset (const struct regset *regset, struct regcache *regcache,
  390.                     int regnum, const void *regs, size_t len)
  391. {
  392.   ULONGEST tdw;
  393.   enum register_status ret;
  394.   int i;

  395.   regcache_supply_regset (regset, regcache, regnum, regs, len);
  396.   ret = regcache_cooked_read_unsigned (regcache, S390_TDB_DWORD0_REGNUM, &tdw);
  397.   if (ret != REG_VALID || (tdw >> 56) != 1)
  398.     regcache_supply_regset (regset, regcache, regnum, NULL, len);
  399. }

  400. const struct regset s390_gregset = {
  401.   s390_gregmap,
  402.   regcache_supply_regset,
  403.   regcache_collect_regset
  404. };

  405. const struct regset s390_fpregset = {
  406.   s390_fpregmap,
  407.   regcache_supply_regset,
  408.   regcache_collect_regset
  409. };

  410. static const struct regset s390_upper_regset = {
  411.   s390_regmap_upper,
  412.   regcache_supply_regset,
  413.   regcache_collect_regset
  414. };

  415. const struct regset s390_last_break_regset = {
  416.   s390_regmap_last_break,
  417.   regcache_supply_regset,
  418.   regcache_collect_regset
  419. };

  420. const struct regset s390x_last_break_regset = {
  421.   s390x_regmap_last_break,
  422.   regcache_supply_regset,
  423.   regcache_collect_regset
  424. };

  425. const struct regset s390_system_call_regset = {
  426.   s390_regmap_system_call,
  427.   regcache_supply_regset,
  428.   regcache_collect_regset
  429. };

  430. const struct regset s390_tdb_regset = {
  431.   s390_regmap_tdb,
  432.   s390_supply_tdb_regset,
  433.   regcache_collect_regset
  434. };

  435. /* Iterate over supported core file register note sections. */

  436. static void
  437. s390_iterate_over_regset_sections (struct gdbarch *gdbarch,
  438.                                    iterate_over_regset_sections_cb *cb,
  439.                                    void *cb_data,
  440.                                    const struct regcache *regcache)
  441. {
  442.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  443.   const int gregset_size = (tdep->abi == ABI_LINUX_S390 ?
  444.                             s390_sizeof_gregset : s390x_sizeof_gregset);

  445.   cb (".reg", gregset_size, &s390_gregset, NULL, cb_data);
  446.   cb (".reg2", s390_sizeof_fpregset, &s390_fpregset, NULL, cb_data);

  447.   if (tdep->abi == ABI_LINUX_S390 && tdep->gpr_full_regnum != -1)
  448.     cb (".reg-s390-high-gprs", 16 * 4, &s390_upper_regset,
  449.         "s390 GPR upper halves", cb_data);

  450.   if (tdep->have_linux_v1)
  451.     cb (".reg-s390-last-break", 8,
  452.         (gdbarch_ptr_bit (gdbarch) == 32
  453.          ? &s390_last_break_regset : &s390x_last_break_regset),
  454.         "s930 last-break address", cb_data);

  455.   if (tdep->have_linux_v2)
  456.     cb (".reg-s390-system-call", 4, &s390_system_call_regset,
  457.         "s390 system-call", cb_data);

  458.   /* If regcache is set, we are in "write" (gcore) mode.  In this
  459.      case, don't iterate over the TDB unless its registers are
  460.      available.  */
  461.   if (tdep->have_tdb
  462.       && (regcache == NULL
  463.           || REG_VALID == regcache_register_status (regcache,
  464.                                                     S390_TDB_DWORD0_REGNUM)))
  465.     cb (".reg-s390-tdb", s390_sizeof_tdbregset, &s390_tdb_regset,
  466.         "s390 TDB", cb_data);
  467. }

  468. static const struct target_desc *
  469. s390_core_read_description (struct gdbarch *gdbarch,
  470.                             struct target_ops *target, bfd *abfd)
  471. {
  472.   asection *high_gprs = bfd_get_section_by_name (abfd, ".reg-s390-high-gprs");
  473.   asection *v1 = bfd_get_section_by_name (abfd, ".reg-s390-last-break");
  474.   asection *v2 = bfd_get_section_by_name (abfd, ".reg-s390-system-call");
  475.   asection *section = bfd_get_section_by_name (abfd, ".reg");
  476.   CORE_ADDR hwcap = 0;

  477.   target_auxv_search (target, AT_HWCAP, &hwcap);
  478.   if (!section)
  479.     return NULL;

  480.   switch (bfd_section_size (abfd, section))
  481.     {
  482.     case s390_sizeof_gregset:
  483.       if (high_gprs)
  484.         return ((hwcap & HWCAP_S390_TE) ? tdesc_s390_te_linux64 :
  485.                 v2? tdesc_s390_linux64v2 :
  486.                 v1? tdesc_s390_linux64v1 : tdesc_s390_linux64);
  487.       else
  488.         return (v2? tdesc_s390_linux32v2 :
  489.                 v1? tdesc_s390_linux32v1 : tdesc_s390_linux32);

  490.     case s390x_sizeof_gregset:
  491.       return ((hwcap & HWCAP_S390_TE) ? tdesc_s390x_te_linux64 :
  492.               v2? tdesc_s390x_linux64v2 :
  493.               v1? tdesc_s390x_linux64v1 : tdesc_s390x_linux64);

  494.     default:
  495.       return NULL;
  496.     }
  497. }


  498. /* Decoding S/390 instructions.  */

  499. /* Named opcode values for the S/390 instructions we recognize.  Some
  500.    instructions have their opcode split across two fields; those are the
  501.    op1_* and op2_* enums.  */
  502. enum
  503.   {
  504.     op1_lhi  = 0xa7,   op2_lhi  = 0x08,
  505.     op1_lghi = 0xa7,   op2_lghi = 0x09,
  506.     op1_lgfi = 0xc0,   op2_lgfi = 0x01,
  507.     op_lr    = 0x18,
  508.     op_lgr   = 0xb904,
  509.     op_l     = 0x58,
  510.     op1_ly   = 0xe3,   op2_ly   = 0x58,
  511.     op1_lg   = 0xe3,   op2_lg   = 0x04,
  512.     op_lm    = 0x98,
  513.     op1_lmy  = 0xeb,   op2_lmy  = 0x98,
  514.     op1_lmg  = 0xeb,   op2_lmg  = 0x04,
  515.     op_st    = 0x50,
  516.     op1_sty  = 0xe3,   op2_sty  = 0x50,
  517.     op1_stg  = 0xe3,   op2_stg  = 0x24,
  518.     op_std   = 0x60,
  519.     op_stm   = 0x90,
  520.     op1_stmy = 0xeb,   op2_stmy = 0x90,
  521.     op1_stmg = 0xeb,   op2_stmg = 0x24,
  522.     op1_aghi = 0xa7,   op2_aghi = 0x0b,
  523.     op1_ahi  = 0xa7,   op2_ahi  = 0x0a,
  524.     op1_agfi = 0xc2,   op2_agfi = 0x08,
  525.     op1_afi  = 0xc2,   op2_afi  = 0x09,
  526.     op1_algfi= 0xc2,   op2_algfi= 0x0a,
  527.     op1_alfi = 0xc2,   op2_alfi = 0x0b,
  528.     op_ar    = 0x1a,
  529.     op_agr   = 0xb908,
  530.     op_a     = 0x5a,
  531.     op1_ay   = 0xe3,   op2_ay   = 0x5a,
  532.     op1_ag   = 0xe3,   op2_ag   = 0x08,
  533.     op1_slgfi= 0xc2,   op2_slgfi= 0x04,
  534.     op1_slfi = 0xc2,   op2_slfi = 0x05,
  535.     op_sr    = 0x1b,
  536.     op_sgr   = 0xb909,
  537.     op_s     = 0x5b,
  538.     op1_sy   = 0xe3,   op2_sy   = 0x5b,
  539.     op1_sg   = 0xe3,   op2_sg   = 0x09,
  540.     op_nr    = 0x14,
  541.     op_ngr   = 0xb980,
  542.     op_la    = 0x41,
  543.     op1_lay  = 0xe3,   op2_lay  = 0x71,
  544.     op1_larl = 0xc0,   op2_larl = 0x00,
  545.     op_basr  = 0x0d,
  546.     op_bas   = 0x4d,
  547.     op_bcr   = 0x07,
  548.     op_bc    = 0x0d,
  549.     op_bctr  = 0x06,
  550.     op_bctgr = 0xb946,
  551.     op_bct   = 0x46,
  552.     op1_bctg = 0xe3,   op2_bctg = 0x46,
  553.     op_bxh   = 0x86,
  554.     op1_bxhg = 0xeb,   op2_bxhg = 0x44,
  555.     op_bxle  = 0x87,
  556.     op1_bxleg= 0xeb,   op2_bxleg= 0x45,
  557.     op1_bras = 0xa7,   op2_bras = 0x05,
  558.     op1_brasl= 0xc0,   op2_brasl= 0x05,
  559.     op1_brc  = 0xa7,   op2_brc  = 0x04,
  560.     op1_brcl = 0xc0,   op2_brcl = 0x04,
  561.     op1_brct = 0xa7,   op2_brct = 0x06,
  562.     op1_brctg= 0xa7,   op2_brctg= 0x07,
  563.     op_brxh  = 0x84,
  564.     op1_brxhg= 0xec,   op2_brxhg= 0x44,
  565.     op_brxle = 0x85,
  566.     op1_brxlg= 0xec,   op2_brxlg= 0x45,
  567.     op_svc   = 0x0a,
  568.   };


  569. /* Read a single instruction from address AT.  */

  570. #define S390_MAX_INSTR_SIZE 6
  571. static int
  572. s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
  573. {
  574.   static int s390_instrlen[] = { 2, 4, 4, 6 };
  575.   int instrlen;

  576.   if (target_read_memory (at, &instr[0], 2))
  577.     return -1;
  578.   instrlen = s390_instrlen[instr[0] >> 6];
  579.   if (instrlen > 2)
  580.     {
  581.       if (target_read_memory (at + 2, &instr[2], instrlen - 2))
  582.         return -1;
  583.     }
  584.   return instrlen;
  585. }


  586. /* The functions below are for recognizing and decoding S/390
  587.    instructions of various formats.  Each of them checks whether INSN
  588.    is an instruction of the given format, with the specified opcodes.
  589.    If it is, it sets the remaining arguments to the values of the
  590.    instruction's fields, and returns a non-zero value; otherwise, it
  591.    returns zero.

  592.    These functions' arguments appear in the order they appear in the
  593.    instruction, not in the machine-language form.  So, opcodes always
  594.    come first, even though they're sometimes scattered around the
  595.    instructions.  And displacements appear before base and extension
  596.    registers, as they do in the assembly syntax, not at the end, as
  597.    they do in the machine language.  */
  598. static int
  599. is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
  600. {
  601.   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
  602.     {
  603.       *r1 = (insn[1] >> 4) & 0xf;
  604.       /* i2 is a 16-bit signed quantity.  */
  605.       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
  606.       return 1;
  607.     }
  608.   else
  609.     return 0;
  610. }


  611. static int
  612. is_ril (bfd_byte *insn, int op1, int op2,
  613.         unsigned int *r1, int *i2)
  614. {
  615.   if (insn[0] == op1 && (insn[1] & 0xf) == op2)
  616.     {
  617.       *r1 = (insn[1] >> 4) & 0xf;
  618.       /* i2 is a signed quantity.  If the host 'int' is 32 bits long,
  619.          no sign extension is necessary, but we don't want to assume
  620.          that.  */
  621.       *i2 = (((insn[2] << 24)
  622.               | (insn[3] << 16)
  623.               | (insn[4] << 8)
  624.               | (insn[5])) ^ 0x80000000) - 0x80000000;
  625.       return 1;
  626.     }
  627.   else
  628.     return 0;
  629. }


  630. static int
  631. is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
  632. {
  633.   if (insn[0] == op)
  634.     {
  635.       *r1 = (insn[1] >> 4) & 0xf;
  636.       *r2 = insn[1] & 0xf;
  637.       return 1;
  638.     }
  639.   else
  640.     return 0;
  641. }


  642. static int
  643. is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
  644. {
  645.   if (((insn[0] << 8) | insn[1]) == op)
  646.     {
  647.       /* Yes, insn[3].  insn[2] is unused in RRE format.  */
  648.       *r1 = (insn[3] >> 4) & 0xf;
  649.       *r2 = insn[3] & 0xf;
  650.       return 1;
  651.     }
  652.   else
  653.     return 0;
  654. }


  655. static int
  656. is_rs (bfd_byte *insn, int op,
  657.        unsigned int *r1, unsigned int *r3, int *d2, unsigned int *b2)
  658. {
  659.   if (insn[0] == op)
  660.     {
  661.       *r1 = (insn[1] >> 4) & 0xf;
  662.       *r3 = insn[1] & 0xf;
  663.       *b2 = (insn[2] >> 4) & 0xf;
  664.       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
  665.       return 1;
  666.     }
  667.   else
  668.     return 0;
  669. }


  670. static int
  671. is_rsy (bfd_byte *insn, int op1, int op2,
  672.         unsigned int *r1, unsigned int *r3, int *d2, unsigned int *b2)
  673. {
  674.   if (insn[0] == op1
  675.       && insn[5] == op2)
  676.     {
  677.       *r1 = (insn[1] >> 4) & 0xf;
  678.       *r3 = insn[1] & 0xf;
  679.       *b2 = (insn[2] >> 4) & 0xf;
  680.       /* The 'long displacement' is a 20-bit signed integer.  */
  681.       *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
  682.                 ^ 0x80000) - 0x80000;
  683.       return 1;
  684.     }
  685.   else
  686.     return 0;
  687. }


  688. static int
  689. is_rsi (bfd_byte *insn, int op,
  690.         unsigned int *r1, unsigned int *r3, int *i2)
  691. {
  692.   if (insn[0] == op)
  693.     {
  694.       *r1 = (insn[1] >> 4) & 0xf;
  695.       *r3 = insn[1] & 0xf;
  696.       /* i2 is a 16-bit signed quantity.  */
  697.       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
  698.       return 1;
  699.     }
  700.   else
  701.     return 0;
  702. }


  703. static int
  704. is_rie (bfd_byte *insn, int op1, int op2,
  705.         unsigned int *r1, unsigned int *r3, int *i2)
  706. {
  707.   if (insn[0] == op1
  708.       && insn[5] == op2)
  709.     {
  710.       *r1 = (insn[1] >> 4) & 0xf;
  711.       *r3 = insn[1] & 0xf;
  712.       /* i2 is a 16-bit signed quantity.  */
  713.       *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
  714.       return 1;
  715.     }
  716.   else
  717.     return 0;
  718. }


  719. static int
  720. is_rx (bfd_byte *insn, int op,
  721.        unsigned int *r1, int *d2, unsigned int *x2, unsigned int *b2)
  722. {
  723.   if (insn[0] == op)
  724.     {
  725.       *r1 = (insn[1] >> 4) & 0xf;
  726.       *x2 = insn[1] & 0xf;
  727.       *b2 = (insn[2] >> 4) & 0xf;
  728.       *d2 = ((insn[2] & 0xf) << 8) | insn[3];
  729.       return 1;
  730.     }
  731.   else
  732.     return 0;
  733. }


  734. static int
  735. is_rxy (bfd_byte *insn, int op1, int op2,
  736.         unsigned int *r1, int *d2, unsigned int *x2, unsigned int *b2)
  737. {
  738.   if (insn[0] == op1
  739.       && insn[5] == op2)
  740.     {
  741.       *r1 = (insn[1] >> 4) & 0xf;
  742.       *x2 = insn[1] & 0xf;
  743.       *b2 = (insn[2] >> 4) & 0xf;
  744.       /* The 'long displacement' is a 20-bit signed integer.  */
  745.       *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
  746.                 ^ 0x80000) - 0x80000;
  747.       return 1;
  748.     }
  749.   else
  750.     return 0;
  751. }


  752. /* Prologue analysis.  */

  753. #define S390_NUM_GPRS 16
  754. #define S390_NUM_FPRS 16

  755. struct s390_prologue_data {

  756.   /* The stack.  */
  757.   struct pv_area *stack;

  758.   /* The size and byte-order of a GPR or FPR.  */
  759.   int gpr_size;
  760.   int fpr_size;
  761.   enum bfd_endian byte_order;

  762.   /* The general-purpose registers.  */
  763.   pv_t gpr[S390_NUM_GPRS];

  764.   /* The floating-point registers.  */
  765.   pv_t fpr[S390_NUM_FPRS];

  766.   /* The offset relative to the CFA where the incoming GPR N was saved
  767.      by the function prologue.  0 if not saved or unknown.  */
  768.   int gpr_slot[S390_NUM_GPRS];

  769.   /* Likewise for FPRs.  */
  770.   int fpr_slot[S390_NUM_FPRS];

  771.   /* Nonzero if the backchain was saved.  This is assumed to be the
  772.      case when the incoming SP is saved at the current SP location.  */
  773.   int back_chain_saved_p;
  774. };

  775. /* Return the effective address for an X-style instruction, like:

  776.         L R1, D2(X2, B2)

  777.    Here, X2 and B2 are registers, and D2 is a signed 20-bit
  778.    constant; the effective address is the sum of all three.  If either
  779.    X2 or B2 are zero, then it doesn't contribute to the sum --- this
  780.    means that r0 can't be used as either X2 or B2.  */
  781. static pv_t
  782. s390_addr (struct s390_prologue_data *data,
  783.            int d2, unsigned int x2, unsigned int b2)
  784. {
  785.   pv_t result;

  786.   result = pv_constant (d2);
  787.   if (x2)
  788.     result = pv_add (result, data->gpr[x2]);
  789.   if (b2)
  790.     result = pv_add (result, data->gpr[b2]);

  791.   return result;
  792. }

  793. /* Do a SIZE-byte store of VALUE to D2(X2,B2).  */
  794. static void
  795. s390_store (struct s390_prologue_data *data,
  796.             int d2, unsigned int x2, unsigned int b2, CORE_ADDR size,
  797.             pv_t value)
  798. {
  799.   pv_t addr = s390_addr (data, d2, x2, b2);
  800.   pv_t offset;

  801.   /* Check whether we are storing the backchain.  */
  802.   offset = pv_subtract (data->gpr[S390_SP_REGNUM - S390_R0_REGNUM], addr);

  803.   if (pv_is_constant (offset) && offset.k == 0)
  804.     if (size == data->gpr_size
  805.         && pv_is_register_k (value, S390_SP_REGNUM, 0))
  806.       {
  807.         data->back_chain_saved_p = 1;
  808.         return;
  809.       }


  810.   /* Check whether we are storing a register into the stack.  */
  811.   if (!pv_area_store_would_trash (data->stack, addr))
  812.     pv_area_store (data->stack, addr, size, value);


  813.   /* Note: If this is some store we cannot identify, you might think we
  814.      should forget our cached values, as any of those might have been hit.

  815.      However, we make the assumption that the register save areas are only
  816.      ever stored to once in any given function, and we do recognize these
  817.      stores.  Thus every store we cannot recognize does not hit our data.  */
  818. }

  819. /* Do a SIZE-byte load from D2(X2,B2).  */
  820. static pv_t
  821. s390_load (struct s390_prologue_data *data,
  822.            int d2, unsigned int x2, unsigned int b2, CORE_ADDR size)

  823. {
  824.   pv_t addr = s390_addr (data, d2, x2, b2);

  825.   /* If it's a load from an in-line constant pool, then we can
  826.      simulate that, under the assumption that the code isn't
  827.      going to change between the time the processor actually
  828.      executed it creating the current frame, and the time when
  829.      we're analyzing the code to unwind past that frame.  */
  830.   if (pv_is_constant (addr))
  831.     {
  832.       struct target_section *secp;
  833.       secp = target_section_by_addr (&current_target, addr.k);
  834.       if (secp != NULL
  835.           && (bfd_get_section_flags (secp->the_bfd_section->owner,
  836.                                      secp->the_bfd_section)
  837.               & SEC_READONLY))
  838.         return pv_constant (read_memory_integer (addr.k, size,
  839.                                                  data->byte_order));
  840.     }

  841.   /* Check whether we are accessing one of our save slots.  */
  842.   return pv_area_fetch (data->stack, addr, size);
  843. }

  844. /* Function for finding saved registers in a 'struct pv_area'; we pass
  845.    this to pv_area_scan.

  846.    If VALUE is a saved register, ADDR says it was saved at a constant
  847.    offset from the frame base, and SIZE indicates that the whole
  848.    register was saved, record its offset in the reg_offset table in
  849.    PROLOGUE_UNTYPED.  */
  850. static void
  851. s390_check_for_saved (void *data_untyped, pv_t addr,
  852.                       CORE_ADDR size, pv_t value)
  853. {
  854.   struct s390_prologue_data *data = data_untyped;
  855.   int i, offset;

  856.   if (!pv_is_register (addr, S390_SP_REGNUM))
  857.     return;

  858.   offset = 16 * data->gpr_size + 32 - addr.k;

  859.   /* If we are storing the original value of a register, we want to
  860.      record the CFA offset.  If the same register is stored multiple
  861.      times, the stack slot with the highest address counts.  */

  862.   for (i = 0; i < S390_NUM_GPRS; i++)
  863.     if (size == data->gpr_size
  864.         && pv_is_register_k (value, S390_R0_REGNUM + i, 0))
  865.       if (data->gpr_slot[i] == 0
  866.           || data->gpr_slot[i] > offset)
  867.         {
  868.           data->gpr_slot[i] = offset;
  869.           return;
  870.         }

  871.   for (i = 0; i < S390_NUM_FPRS; i++)
  872.     if (size == data->fpr_size
  873.         && pv_is_register_k (value, S390_F0_REGNUM + i, 0))
  874.       if (data->fpr_slot[i] == 0
  875.           || data->fpr_slot[i] > offset)
  876.         {
  877.           data->fpr_slot[i] = offset;
  878.           return;
  879.         }
  880. }

  881. /* Analyze the prologue of the function starting at START_PC,
  882.    continuing at most until CURRENT_PC.  Initialize DATA to
  883.    hold all information we find out about the state of the registers
  884.    and stack slots.  Return the address of the instruction after
  885.    the last one that changed the SP, FP, or back chain; or zero
  886.    on error.  */
  887. static CORE_ADDR
  888. s390_analyze_prologue (struct gdbarch *gdbarch,
  889.                        CORE_ADDR start_pc,
  890.                        CORE_ADDR current_pc,
  891.                        struct s390_prologue_data *data)
  892. {
  893.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;

  894.   /* Our return value:
  895.      The address of the instruction after the last one that changed
  896.      the SP, FP, or back chain;  zero if we got an error trying to
  897.      read memory.  */
  898.   CORE_ADDR result = start_pc;

  899.   /* The current PC for our abstract interpretation.  */
  900.   CORE_ADDR pc;

  901.   /* The address of the next instruction after that.  */
  902.   CORE_ADDR next_pc;

  903.   /* Set up everything's initial value.  */
  904.   {
  905.     int i;

  906.     data->stack = make_pv_area (S390_SP_REGNUM, gdbarch_addr_bit (gdbarch));

  907.     /* For the purpose of prologue tracking, we consider the GPR size to
  908.        be equal to the ABI word size, even if it is actually larger
  909.        (i.e. when running a 32-bit binary under a 64-bit kernel).  */
  910.     data->gpr_size = word_size;
  911.     data->fpr_size = 8;
  912.     data->byte_order = gdbarch_byte_order (gdbarch);

  913.     for (i = 0; i < S390_NUM_GPRS; i++)
  914.       data->gpr[i] = pv_register (S390_R0_REGNUM + i, 0);

  915.     for (i = 0; i < S390_NUM_FPRS; i++)
  916.       data->fpr[i] = pv_register (S390_F0_REGNUM + i, 0);

  917.     for (i = 0; i < S390_NUM_GPRS; i++)
  918.       data->gpr_slot[i]  = 0;

  919.     for (i = 0; i < S390_NUM_FPRS; i++)
  920.       data->fpr_slot[i]  = 0;

  921.     data->back_chain_saved_p = 0;
  922.   }

  923.   /* Start interpreting instructions, until we hit the frame's
  924.      current PC or the first branch instruction.  */
  925.   for (pc = start_pc; pc > 0 && pc < current_pc; pc = next_pc)
  926.     {
  927.       bfd_byte insn[S390_MAX_INSTR_SIZE];
  928.       int insn_len = s390_readinstruction (insn, pc);

  929.       bfd_byte dummy[S390_MAX_INSTR_SIZE] = { 0 };
  930.       bfd_byte *insn32 = word_size == 4 ? insn : dummy;
  931.       bfd_byte *insn64 = word_size == 8 ? insn : dummy;

  932.       /* Fields for various kinds of instructions.  */
  933.       unsigned int b2, r1, r2, x2, r3;
  934.       int i2, d2;

  935.       /* The values of SP and FP before this instruction,
  936.          for detecting instructions that change them.  */
  937.       pv_t pre_insn_sp, pre_insn_fp;
  938.       /* Likewise for the flag whether the back chain was saved.  */
  939.       int pre_insn_back_chain_saved_p;

  940.       /* If we got an error trying to read the instruction, report it.  */
  941.       if (insn_len < 0)
  942.         {
  943.           result = 0;
  944.           break;
  945.         }

  946.       next_pc = pc + insn_len;

  947.       pre_insn_sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
  948.       pre_insn_fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
  949.       pre_insn_back_chain_saved_p = data->back_chain_saved_p;


  950.       /* LHI r1, i2 --- load halfword immediate.  */
  951.       /* LGHI r1, i2 --- load halfword immediate (64-bit version).  */
  952.       /* LGFI r1, i2 --- load fullword immediate.  */
  953.       if (is_ri (insn32, op1_lhi, op2_lhi, &r1, &i2)
  954.           || is_ri (insn64, op1_lghi, op2_lghi, &r1, &i2)
  955.           || is_ril (insn, op1_lgfi, op2_lgfi, &r1, &i2))
  956.         data->gpr[r1] = pv_constant (i2);

  957.       /* LR r1, r2 --- load from register.  */
  958.       /* LGR r1, r2 --- load from register (64-bit version).  */
  959.       else if (is_rr (insn32, op_lr, &r1, &r2)
  960.                || is_rre (insn64, op_lgr, &r1, &r2))
  961.         data->gpr[r1] = data->gpr[r2];

  962.       /* L r1, d2(x2, b2) --- load.  */
  963.       /* LY r1, d2(x2, b2) --- load (long-displacement version).  */
  964.       /* LG r1, d2(x2, b2) --- load (64-bit version).  */
  965.       else if (is_rx (insn32, op_l, &r1, &d2, &x2, &b2)
  966.                || is_rxy (insn32, op1_ly, op2_ly, &r1, &d2, &x2, &b2)
  967.                || is_rxy (insn64, op1_lg, op2_lg, &r1, &d2, &x2, &b2))
  968.         data->gpr[r1] = s390_load (data, d2, x2, b2, data->gpr_size);

  969.       /* ST r1, d2(x2, b2) --- store.  */
  970.       /* STY r1, d2(x2, b2) --- store (long-displacement version).  */
  971.       /* STG r1, d2(x2, b2) --- store (64-bit version).  */
  972.       else if (is_rx (insn32, op_st, &r1, &d2, &x2, &b2)
  973.                || is_rxy (insn32, op1_sty, op2_sty, &r1, &d2, &x2, &b2)
  974.                || is_rxy (insn64, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
  975.         s390_store (data, d2, x2, b2, data->gpr_size, data->gpr[r1]);

  976.       /* STD r1, d2(x2,b2) --- store floating-point register.  */
  977.       else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
  978.         s390_store (data, d2, x2, b2, data->fpr_size, data->fpr[r1]);

  979.       /* STM r1, r3, d2(b2) --- store multiple.  */
  980.       /* STMY r1, r3, d2(b2) --- store multiple (long-displacement
  981.          version).  */
  982.       /* STMG r1, r3, d2(b2) --- store multiple (64-bit version).  */
  983.       else if (is_rs (insn32, op_stm, &r1, &r3, &d2, &b2)
  984.                || is_rsy (insn32, op1_stmy, op2_stmy, &r1, &r3, &d2, &b2)
  985.                || is_rsy (insn64, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
  986.         {
  987.           for (; r1 <= r3; r1++, d2 += data->gpr_size)
  988.             s390_store (data, d2, 0, b2, data->gpr_size, data->gpr[r1]);
  989.         }

  990.       /* AHI r1, i2 --- add halfword immediate.  */
  991.       /* AGHI r1, i2 --- add halfword immediate (64-bit version).  */
  992.       /* AFI r1, i2 --- add fullword immediate.  */
  993.       /* AGFI r1, i2 --- add fullword immediate (64-bit version).  */
  994.       else if (is_ri (insn32, op1_ahi, op2_ahi, &r1, &i2)
  995.                || is_ri (insn64, op1_aghi, op2_aghi, &r1, &i2)
  996.                || is_ril (insn32, op1_afi, op2_afi, &r1, &i2)
  997.                || is_ril (insn64, op1_agfi, op2_agfi, &r1, &i2))
  998.         data->gpr[r1] = pv_add_constant (data->gpr[r1], i2);

  999.       /* ALFI r1, i2 --- add logical immediate.  */
  1000.       /* ALGFI r1, i2 --- add logical immediate (64-bit version).  */
  1001.       else if (is_ril (insn32, op1_alfi, op2_alfi, &r1, &i2)
  1002.                || is_ril (insn64, op1_algfi, op2_algfi, &r1, &i2))
  1003.         data->gpr[r1] = pv_add_constant (data->gpr[r1],
  1004.                                          (CORE_ADDR)i2 & 0xffffffff);

  1005.       /* AR r1, r2 -- add register.  */
  1006.       /* AGR r1, r2 -- add register (64-bit version).  */
  1007.       else if (is_rr (insn32, op_ar, &r1, &r2)
  1008.                || is_rre (insn64, op_agr, &r1, &r2))
  1009.         data->gpr[r1] = pv_add (data->gpr[r1], data->gpr[r2]);

  1010.       /* A r1, d2(x2, b2) -- add.  */
  1011.       /* AY r1, d2(x2, b2) -- add (long-displacement version).  */
  1012.       /* AG r1, d2(x2, b2) -- add (64-bit version).  */
  1013.       else if (is_rx (insn32, op_a, &r1, &d2, &x2, &b2)
  1014.                || is_rxy (insn32, op1_ay, op2_ay, &r1, &d2, &x2, &b2)
  1015.                || is_rxy (insn64, op1_ag, op2_ag, &r1, &d2, &x2, &b2))
  1016.         data->gpr[r1] = pv_add (data->gpr[r1],
  1017.                                 s390_load (data, d2, x2, b2, data->gpr_size));

  1018.       /* SLFI r1, i2 --- subtract logical immediate.  */
  1019.       /* SLGFI r1, i2 --- subtract logical immediate (64-bit version).  */
  1020.       else if (is_ril (insn32, op1_slfi, op2_slfi, &r1, &i2)
  1021.                || is_ril (insn64, op1_slgfi, op2_slgfi, &r1, &i2))
  1022.         data->gpr[r1] = pv_add_constant (data->gpr[r1],
  1023.                                          -((CORE_ADDR)i2 & 0xffffffff));

  1024.       /* SR r1, r2 -- subtract register.  */
  1025.       /* SGR r1, r2 -- subtract register (64-bit version).  */
  1026.       else if (is_rr (insn32, op_sr, &r1, &r2)
  1027.                || is_rre (insn64, op_sgr, &r1, &r2))
  1028.         data->gpr[r1] = pv_subtract (data->gpr[r1], data->gpr[r2]);

  1029.       /* S r1, d2(x2, b2) -- subtract.  */
  1030.       /* SY r1, d2(x2, b2) -- subtract (long-displacement version).  */
  1031.       /* SG r1, d2(x2, b2) -- subtract (64-bit version).  */
  1032.       else if (is_rx (insn32, op_s, &r1, &d2, &x2, &b2)
  1033.                || is_rxy (insn32, op1_sy, op2_sy, &r1, &d2, &x2, &b2)
  1034.                || is_rxy (insn64, op1_sg, op2_sg, &r1, &d2, &x2, &b2))
  1035.         data->gpr[r1] = pv_subtract (data->gpr[r1],
  1036.                                 s390_load (data, d2, x2, b2, data->gpr_size));

  1037.       /* LA r1, d2(x2, b2) --- load address.  */
  1038.       /* LAY r1, d2(x2, b2) --- load address (long-displacement version).  */
  1039.       else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2)
  1040.                || is_rxy (insn, op1_lay, op2_lay, &r1, &d2, &x2, &b2))
  1041.         data->gpr[r1] = s390_addr (data, d2, x2, b2);

  1042.       /* LARL r1, i2 --- load address relative long.  */
  1043.       else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
  1044.         data->gpr[r1] = pv_constant (pc + i2 * 2);

  1045.       /* BASR r1, 0 --- branch and save.
  1046.          Since r2 is zero, this saves the PC in r1, but doesn't branch.  */
  1047.       else if (is_rr (insn, op_basr, &r1, &r2)
  1048.                && r2 == 0)
  1049.         data->gpr[r1] = pv_constant (next_pc);

  1050.       /* BRAS r1, i2 --- branch relative and save.  */
  1051.       else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
  1052.         {
  1053.           data->gpr[r1] = pv_constant (next_pc);
  1054.           next_pc = pc + i2 * 2;

  1055.           /* We'd better not interpret any backward branches.  We'll
  1056.              never terminate.  */
  1057.           if (next_pc <= pc)
  1058.             break;
  1059.         }

  1060.       /* Terminate search when hitting any other branch instruction.  */
  1061.       else if (is_rr (insn, op_basr, &r1, &r2)
  1062.                || is_rx (insn, op_bas, &r1, &d2, &x2, &b2)
  1063.                || is_rr (insn, op_bcr, &r1, &r2)
  1064.                || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
  1065.                || is_ri (insn, op1_brc, op2_brc, &r1, &i2)
  1066.                || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
  1067.                || is_ril (insn, op1_brasl, op2_brasl, &r2, &i2))
  1068.         break;

  1069.       else
  1070.         {
  1071.           /* An instruction we don't know how to simulate.  The only
  1072.              safe thing to do would be to set every value we're tracking
  1073.              to 'unknown'.  Instead, we'll be optimistic: we assume that
  1074.              we *can* interpret every instruction that the compiler uses
  1075.              to manipulate any of the data we're interested in here --
  1076.              then we can just ignore anything else.  */
  1077.         }

  1078.       /* Record the address after the last instruction that changed
  1079.          the FP, SP, or backlink.  Ignore instructions that changed
  1080.          them back to their original values --- those are probably
  1081.          restore instructions.  (The back chain is never restored,
  1082.          just popped.)  */
  1083.       {
  1084.         pv_t sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
  1085.         pv_t fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];

  1086.         if ((! pv_is_identical (pre_insn_sp, sp)
  1087.              && ! pv_is_register_k (sp, S390_SP_REGNUM, 0)
  1088.              && sp.kind != pvk_unknown)
  1089.             || (! pv_is_identical (pre_insn_fp, fp)
  1090.                 && ! pv_is_register_k (fp, S390_FRAME_REGNUM, 0)
  1091.                 && fp.kind != pvk_unknown)
  1092.             || pre_insn_back_chain_saved_p != data->back_chain_saved_p)
  1093.           result = next_pc;
  1094.       }
  1095.     }

  1096.   /* Record where all the registers were saved.  */
  1097.   pv_area_scan (data->stack, s390_check_for_saved, data);

  1098.   free_pv_area (data->stack);
  1099.   data->stack = NULL;

  1100.   return result;
  1101. }

  1102. /* Advance PC across any function entry prologue instructions to reach
  1103.    some "real" code.  */
  1104. static CORE_ADDR
  1105. s390_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1106. {
  1107.   struct s390_prologue_data data;
  1108.   CORE_ADDR skip_pc;
  1109.   skip_pc = s390_analyze_prologue (gdbarch, pc, (CORE_ADDR)-1, &data);
  1110.   return skip_pc ? skip_pc : pc;
  1111. }

  1112. /* Return true if we are in the functin's epilogue, i.e. after the
  1113.    instruction that destroyed the function's stack frame.  */
  1114. static int
  1115. s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  1116. {
  1117.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;

  1118.   /* In frameless functions, there's not frame to destroy and thus
  1119.      we don't care about the epilogue.

  1120.      In functions with frame, the epilogue sequence is a pair of
  1121.      a LM-type instruction that restores (amongst others) the
  1122.      return register %r14 and the stack pointer %r15, followed
  1123.      by a branch 'br %r14' --or equivalent-- that effects the
  1124.      actual return.

  1125.      In that situation, this function needs to return 'true' in
  1126.      exactly one case: when pc points to that branch instruction.

  1127.      Thus we try to disassemble the one instructions immediately
  1128.      preceding pc and check whether it is an LM-type instruction
  1129.      modifying the stack pointer.

  1130.      Note that disassembling backwards is not reliable, so there
  1131.      is a slight chance of false positives here ...  */

  1132.   bfd_byte insn[6];
  1133.   unsigned int r1, r3, b2;
  1134.   int d2;

  1135.   if (word_size == 4
  1136.       && !target_read_memory (pc - 4, insn, 4)
  1137.       && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
  1138.       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
  1139.     return 1;

  1140.   if (word_size == 4
  1141.       && !target_read_memory (pc - 6, insn, 6)
  1142.       && is_rsy (insn, op1_lmy, op2_lmy, &r1, &r3, &d2, &b2)
  1143.       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
  1144.     return 1;

  1145.   if (word_size == 8
  1146.       && !target_read_memory (pc - 6, insn, 6)
  1147.       && is_rsy (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
  1148.       && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
  1149.     return 1;

  1150.   return 0;
  1151. }

  1152. /* Displaced stepping.  */

  1153. /* Fix up the state of registers and memory after having single-stepped
  1154.    a displaced instruction.  */
  1155. static void
  1156. s390_displaced_step_fixup (struct gdbarch *gdbarch,
  1157.                            struct displaced_step_closure *closure,
  1158.                            CORE_ADDR from, CORE_ADDR to,
  1159.                            struct regcache *regs)
  1160. {
  1161.   /* Since we use simple_displaced_step_copy_insn, our closure is a
  1162.      copy of the instruction.  */
  1163.   gdb_byte *insn = (gdb_byte *) closure;
  1164.   static int s390_instrlen[] = { 2, 4, 4, 6 };
  1165.   int insnlen = s390_instrlen[insn[0] >> 6];

  1166.   /* Fields for various kinds of instructions.  */
  1167.   unsigned int b2, r1, r2, x2, r3;
  1168.   int i2, d2;

  1169.   /* Get current PC and addressing mode bit.  */
  1170.   CORE_ADDR pc = regcache_read_pc (regs);
  1171.   ULONGEST amode = 0;

  1172.   if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
  1173.     {
  1174.       regcache_cooked_read_unsigned (regs, S390_PSWA_REGNUM, &amode);
  1175.       amode &= 0x80000000;
  1176.     }

  1177.   if (debug_displaced)
  1178.     fprintf_unfiltered (gdb_stdlog,
  1179.                         "displaced: (s390) fixup (%s, %s) pc %s len %d amode 0x%x\n",
  1180.                         paddress (gdbarch, from), paddress (gdbarch, to),
  1181.                         paddress (gdbarch, pc), insnlen, (int) amode);

  1182.   /* Handle absolute branch and save instructions.  */
  1183.   if (is_rr (insn, op_basr, &r1, &r2)
  1184.       || is_rx (insn, op_bas, &r1, &d2, &x2, &b2))
  1185.     {
  1186.       /* Recompute saved return address in R1.  */
  1187.       regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
  1188.                                       amode | (from + insnlen));
  1189.     }

  1190.   /* Handle absolute branch instructions.  */
  1191.   else if (is_rr (insn, op_bcr, &r1, &r2)
  1192.            || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
  1193.            || is_rr (insn, op_bctr, &r1, &r2)
  1194.            || is_rre (insn, op_bctgr, &r1, &r2)
  1195.            || is_rx (insn, op_bct, &r1, &d2, &x2, &b2)
  1196.            || is_rxy (insn, op1_bctg, op2_brctg, &r1, &d2, &x2, &b2)
  1197.            || is_rs (insn, op_bxh, &r1, &r3, &d2, &b2)
  1198.            || is_rsy (insn, op1_bxhg, op2_bxhg, &r1, &r3, &d2, &b2)
  1199.            || is_rs (insn, op_bxle, &r1, &r3, &d2, &b2)
  1200.            || is_rsy (insn, op1_bxleg, op2_bxleg, &r1, &r3, &d2, &b2))
  1201.     {
  1202.       /* Update PC iff branch was *not* taken.  */
  1203.       if (pc == to + insnlen)
  1204.         regcache_write_pc (regs, from + insnlen);
  1205.     }

  1206.   /* Handle PC-relative branch and save instructions.  */
  1207.   else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2)
  1208.            || is_ril (insn, op1_brasl, op2_brasl, &r1, &i2))
  1209.     {
  1210.       /* Update PC.  */
  1211.       regcache_write_pc (regs, pc - to + from);
  1212.       /* Recompute saved return address in R1.  */
  1213.       regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
  1214.                                       amode | (from + insnlen));
  1215.     }

  1216.   /* Handle PC-relative branch instructions.  */
  1217.   else if (is_ri (insn, op1_brc, op2_brc, &r1, &i2)
  1218.            || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
  1219.            || is_ri (insn, op1_brct, op2_brct, &r1, &i2)
  1220.            || is_ri (insn, op1_brctg, op2_brctg, &r1, &i2)
  1221.            || is_rsi (insn, op_brxh, &r1, &r3, &i2)
  1222.            || is_rie (insn, op1_brxhg, op2_brxhg, &r1, &r3, &i2)
  1223.            || is_rsi (insn, op_brxle, &r1, &r3, &i2)
  1224.            || is_rie (insn, op1_brxlg, op2_brxlg, &r1, &r3, &i2))
  1225.     {
  1226.       /* Update PC.  */
  1227.       regcache_write_pc (regs, pc - to + from);
  1228.     }

  1229.   /* Handle LOAD ADDRESS RELATIVE LONG.  */
  1230.   else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
  1231.     {
  1232.       /* Update PC.  */
  1233.       regcache_write_pc (regs, from + insnlen);
  1234.       /* Recompute output address in R1.  */
  1235.       regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
  1236.                                       amode | (from + i2 * 2));
  1237.     }

  1238.   /* If we executed a breakpoint instruction, point PC right back at it.  */
  1239.   else if (insn[0] == 0x0 && insn[1] == 0x1)
  1240.     regcache_write_pc (regs, from);

  1241.   /* For any other insn, PC points right after the original instruction.  */
  1242.   else
  1243.     regcache_write_pc (regs, from + insnlen);

  1244.   if (debug_displaced)
  1245.     fprintf_unfiltered (gdb_stdlog,
  1246.                         "displaced: (s390) pc is now %s\n",
  1247.                         paddress (gdbarch, regcache_read_pc (regs)));
  1248. }


  1249. /* Helper routine to unwind pseudo registers.  */

  1250. static struct value *
  1251. s390_unwind_pseudo_register (struct frame_info *this_frame, int regnum)
  1252. {
  1253.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1254.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1255.   struct type *type = register_type (gdbarch, regnum);

  1256.   /* Unwind PC via PSW address.  */
  1257.   if (regnum == tdep->pc_regnum)
  1258.     {
  1259.       struct value *val;

  1260.       val = frame_unwind_register_value (this_frame, S390_PSWA_REGNUM);
  1261.       if (!value_optimized_out (val))
  1262.         {
  1263.           LONGEST pswa = value_as_long (val);

  1264.           if (TYPE_LENGTH (type) == 4)
  1265.             return value_from_pointer (type, pswa & 0x7fffffff);
  1266.           else
  1267.             return value_from_pointer (type, pswa);
  1268.         }
  1269.     }

  1270.   /* Unwind CC via PSW mask.  */
  1271.   if (regnum == tdep->cc_regnum)
  1272.     {
  1273.       struct value *val;

  1274.       val = frame_unwind_register_value (this_frame, S390_PSWM_REGNUM);
  1275.       if (!value_optimized_out (val))
  1276.         {
  1277.           LONGEST pswm = value_as_long (val);

  1278.           if (TYPE_LENGTH (type) == 4)
  1279.             return value_from_longest (type, (pswm >> 12) & 3);
  1280.           else
  1281.             return value_from_longest (type, (pswm >> 44) & 3);
  1282.         }
  1283.     }

  1284.   /* Unwind full GPRs to show at least the lower halves (as the
  1285.      upper halves are undefined).  */
  1286.   if (regnum_is_gpr_full (tdep, regnum))
  1287.     {
  1288.       int reg = regnum - tdep->gpr_full_regnum;
  1289.       struct value *val;

  1290.       val = frame_unwind_register_value (this_frame, S390_R0_REGNUM + reg);
  1291.       if (!value_optimized_out (val))
  1292.         return value_cast (type, val);
  1293.     }

  1294.   return allocate_optimized_out_value (type);
  1295. }

  1296. static struct value *
  1297. s390_trad_frame_prev_register (struct frame_info *this_frame,
  1298.                                struct trad_frame_saved_reg saved_regs[],
  1299.                                int regnum)
  1300. {
  1301.   if (regnum < S390_NUM_REGS)
  1302.     return trad_frame_get_prev_register (this_frame, saved_regs, regnum);
  1303.   else
  1304.     return s390_unwind_pseudo_register (this_frame, regnum);
  1305. }


  1306. /* Normal stack frames.  */

  1307. struct s390_unwind_cache {

  1308.   CORE_ADDR func;
  1309.   CORE_ADDR frame_base;
  1310.   CORE_ADDR local_base;

  1311.   struct trad_frame_saved_reg *saved_regs;
  1312. };

  1313. static int
  1314. s390_prologue_frame_unwind_cache (struct frame_info *this_frame,
  1315.                                   struct s390_unwind_cache *info)
  1316. {
  1317.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1318.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  1319.   struct s390_prologue_data data;
  1320.   pv_t *fp = &data.gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
  1321.   pv_t *sp = &data.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
  1322.   int i;
  1323.   CORE_ADDR cfa;
  1324.   CORE_ADDR func;
  1325.   CORE_ADDR result;
  1326.   ULONGEST reg;
  1327.   CORE_ADDR prev_sp;
  1328.   int frame_pointer;
  1329.   int size;
  1330.   struct frame_info *next_frame;

  1331.   /* Try to find the function start address.  If we can't find it, we don't
  1332.      bother searching for it -- with modern compilers this would be mostly
  1333.      pointless anyway.  Trust that we'll either have valid DWARF-2 CFI data
  1334.      or else a valid backchain ...  */
  1335.   func = get_frame_func (this_frame);
  1336.   if (!func)
  1337.     return 0;

  1338.   /* Try to analyze the prologue.  */
  1339.   result = s390_analyze_prologue (gdbarch, func,
  1340.                                   get_frame_pc (this_frame), &data);
  1341.   if (!result)
  1342.     return 0;

  1343.   /* If this was successful, we should have found the instruction that
  1344.      sets the stack pointer register to the previous value of the stack
  1345.      pointer minus the frame size.  */
  1346.   if (!pv_is_register (*sp, S390_SP_REGNUM))
  1347.     return 0;

  1348.   /* A frame size of zero at this point can mean either a real
  1349.      frameless function, or else a failure to find the prologue.
  1350.      Perform some sanity checks to verify we really have a
  1351.      frameless function.  */
  1352.   if (sp->k == 0)
  1353.     {
  1354.       /* If the next frame is a NORMAL_FRAME, this frame *cannot* have frame
  1355.          size zero.  This is only possible if the next frame is a sentinel
  1356.          frame, a dummy frame, or a signal trampoline frame.  */
  1357.       /* FIXME: cagney/2004-05-01: This sanity check shouldn't be
  1358.          needed, instead the code should simpliy rely on its
  1359.          analysis.  */
  1360.       next_frame = get_next_frame (this_frame);
  1361.       while (next_frame && get_frame_type (next_frame) == INLINE_FRAME)
  1362.         next_frame = get_next_frame (next_frame);
  1363.       if (next_frame
  1364.           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
  1365.         return 0;

  1366.       /* If we really have a frameless function, %r14 must be valid
  1367.          -- in particular, it must point to a different function.  */
  1368.       reg = get_frame_register_unsigned (this_frame, S390_RETADDR_REGNUM);
  1369.       reg = gdbarch_addr_bits_remove (gdbarch, reg) - 1;
  1370.       if (get_pc_function_start (reg) == func)
  1371.         {
  1372.           /* However, there is one case where it *is* valid for %r14
  1373.              to point to the same function -- if this is a recursive
  1374.              call, and we have stopped in the prologue *before* the
  1375.              stack frame was allocated.

  1376.              Recognize this case by looking ahead a bit ...  */

  1377.           struct s390_prologue_data data2;
  1378.           pv_t *sp = &data2.gpr[S390_SP_REGNUM - S390_R0_REGNUM];

  1379.           if (!(s390_analyze_prologue (gdbarch, func, (CORE_ADDR)-1, &data2)
  1380.                 && pv_is_register (*sp, S390_SP_REGNUM)
  1381.                 && sp->k != 0))
  1382.             return 0;
  1383.         }
  1384.     }


  1385.   /* OK, we've found valid prologue data.  */
  1386.   size = -sp->k;

  1387.   /* If the frame pointer originally also holds the same value
  1388.      as the stack pointer, we're probably using it.  If it holds
  1389.      some other value -- even a constant offset -- it is most
  1390.      likely used as temp register.  */
  1391.   if (pv_is_identical (*sp, *fp))
  1392.     frame_pointer = S390_FRAME_REGNUM;
  1393.   else
  1394.     frame_pointer = S390_SP_REGNUM;

  1395.   /* If we've detected a function with stack frame, we'll still have to
  1396.      treat it as frameless if we're currently within the function epilog
  1397.      code at a point where the frame pointer has already been restored.
  1398.      This can only happen in an innermost frame.  */
  1399.   /* FIXME: cagney/2004-05-01: This sanity check shouldn't be needed,
  1400.      instead the code should simpliy rely on its analysis.  */
  1401.   next_frame = get_next_frame (this_frame);
  1402.   while (next_frame && get_frame_type (next_frame) == INLINE_FRAME)
  1403.     next_frame = get_next_frame (next_frame);
  1404.   if (size > 0
  1405.       && (next_frame == NULL
  1406.           || get_frame_type (get_next_frame (this_frame)) != NORMAL_FRAME))
  1407.     {
  1408.       /* See the comment in s390_in_function_epilogue_p on why this is
  1409.          not completely reliable ...  */
  1410.       if (s390_in_function_epilogue_p (gdbarch, get_frame_pc (this_frame)))
  1411.         {
  1412.           memset (&data, 0, sizeof (data));
  1413.           size = 0;
  1414.           frame_pointer = S390_SP_REGNUM;
  1415.         }
  1416.     }

  1417.   /* Once we know the frame register and the frame size, we can unwind
  1418.      the current value of the frame register from the next frame, and
  1419.      add back the frame size to arrive that the previous frame's
  1420.      stack pointer value.  */
  1421.   prev_sp = get_frame_register_unsigned (this_frame, frame_pointer) + size;
  1422.   cfa = prev_sp + 16*word_size + 32;

  1423.   /* Set up ABI call-saved/call-clobbered registers.  */
  1424.   for (i = 0; i < S390_NUM_REGS; i++)
  1425.     if (!s390_register_call_saved (gdbarch, i))
  1426.       trad_frame_set_unknown (info->saved_regs, i);

  1427.   /* CC is always call-clobbered.  */
  1428.   trad_frame_set_unknown (info->saved_regs, S390_PSWM_REGNUM);

  1429.   /* Record the addresses of all register spill slots the prologue parser
  1430.      has recognized.  Consider only registers defined as call-saved by the
  1431.      ABI; for call-clobbered registers the parser may have recognized
  1432.      spurious stores.  */

  1433.   for (i = 0; i < 16; i++)
  1434.     if (s390_register_call_saved (gdbarch, S390_R0_REGNUM + i)
  1435.         && data.gpr_slot[i] != 0)
  1436.       info->saved_regs[S390_R0_REGNUM + i].addr = cfa - data.gpr_slot[i];

  1437.   for (i = 0; i < 16; i++)
  1438.     if (s390_register_call_saved (gdbarch, S390_F0_REGNUM + i)
  1439.         && data.fpr_slot[i] != 0)
  1440.       info->saved_regs[S390_F0_REGNUM + i].addr = cfa - data.fpr_slot[i];

  1441.   /* Function return will set PC to %r14.  */
  1442.   info->saved_regs[S390_PSWA_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];

  1443.   /* In frameless functions, we unwind simply by moving the return
  1444.      address to the PC.  However, if we actually stored to the
  1445.      save area, use that -- we might only think the function frameless
  1446.      because we're in the middle of the prologue ...  */
  1447.   if (size == 0
  1448.       && !trad_frame_addr_p (info->saved_regs, S390_PSWA_REGNUM))
  1449.     {
  1450.       info->saved_regs[S390_PSWA_REGNUM].realreg = S390_RETADDR_REGNUM;
  1451.     }

  1452.   /* Another sanity check: unless this is a frameless function,
  1453.      we should have found spill slots for SP and PC.
  1454.      If not, we cannot unwind further -- this happens e.g. in
  1455.      libc's thread_start routine.  */
  1456.   if (size > 0)
  1457.     {
  1458.       if (!trad_frame_addr_p (info->saved_regs, S390_SP_REGNUM)
  1459.           || !trad_frame_addr_p (info->saved_regs, S390_PSWA_REGNUM))
  1460.         prev_sp = -1;
  1461.     }

  1462.   /* We use the current value of the frame register as local_base,
  1463.      and the top of the register save area as frame_base.  */
  1464.   if (prev_sp != -1)
  1465.     {
  1466.       info->frame_base = prev_sp + 16*word_size + 32;
  1467.       info->local_base = prev_sp - size;
  1468.     }

  1469.   info->func = func;
  1470.   return 1;
  1471. }

  1472. static void
  1473. s390_backchain_frame_unwind_cache (struct frame_info *this_frame,
  1474.                                    struct s390_unwind_cache *info)
  1475. {
  1476.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1477.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  1478.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1479.   CORE_ADDR backchain;
  1480.   ULONGEST reg;
  1481.   LONGEST sp;
  1482.   int i;

  1483.   /* Set up ABI call-saved/call-clobbered registers.  */
  1484.   for (i = 0; i < S390_NUM_REGS; i++)
  1485.     if (!s390_register_call_saved (gdbarch, i))
  1486.       trad_frame_set_unknown (info->saved_regs, i);

  1487.   /* CC is always call-clobbered.  */
  1488.   trad_frame_set_unknown (info->saved_regs, S390_PSWM_REGNUM);

  1489.   /* Get the backchain.  */
  1490.   reg = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
  1491.   backchain = read_memory_unsigned_integer (reg, word_size, byte_order);

  1492.   /* A zero backchain terminates the frame chain.  As additional
  1493.      sanity check, let's verify that the spill slot for SP in the
  1494.      save area pointed to by the backchain in fact links back to
  1495.      the save area.  */
  1496.   if (backchain != 0
  1497.       && safe_read_memory_integer (backchain + 15*word_size,
  1498.                                    word_size, byte_order, &sp)
  1499.       && (CORE_ADDR)sp == backchain)
  1500.     {
  1501.       /* We don't know which registers were saved, but it will have
  1502.          to be at least %r14 and %r15.  This will allow us to continue
  1503.          unwinding, but other prev-frame registers may be incorrect ...  */
  1504.       info->saved_regs[S390_SP_REGNUM].addr = backchain + 15*word_size;
  1505.       info->saved_regs[S390_RETADDR_REGNUM].addr = backchain + 14*word_size;

  1506.       /* Function return will set PC to %r14.  */
  1507.       info->saved_regs[S390_PSWA_REGNUM]
  1508.         = info->saved_regs[S390_RETADDR_REGNUM];

  1509.       /* We use the current value of the frame register as local_base,
  1510.          and the top of the register save area as frame_base.  */
  1511.       info->frame_base = backchain + 16*word_size + 32;
  1512.       info->local_base = reg;
  1513.     }

  1514.   info->func = get_frame_pc (this_frame);
  1515. }

  1516. static struct s390_unwind_cache *
  1517. s390_frame_unwind_cache (struct frame_info *this_frame,
  1518.                          void **this_prologue_cache)
  1519. {
  1520.   volatile struct gdb_exception ex;
  1521.   struct s390_unwind_cache *info;

  1522.   if (*this_prologue_cache)
  1523.     return *this_prologue_cache;

  1524.   info = FRAME_OBSTACK_ZALLOC (struct s390_unwind_cache);
  1525.   *this_prologue_cache = info;
  1526.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  1527.   info->func = -1;
  1528.   info->frame_base = -1;
  1529.   info->local_base = -1;

  1530.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  1531.     {
  1532.       /* Try to use prologue analysis to fill the unwind cache.
  1533.          If this fails, fall back to reading the stack backchain.  */
  1534.       if (!s390_prologue_frame_unwind_cache (this_frame, info))
  1535.         s390_backchain_frame_unwind_cache (this_frame, info);
  1536.     }
  1537.   if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
  1538.     throw_exception (ex);

  1539.   return info;
  1540. }

  1541. static void
  1542. s390_frame_this_id (struct frame_info *this_frame,
  1543.                     void **this_prologue_cache,
  1544.                     struct frame_id *this_id)
  1545. {
  1546.   struct s390_unwind_cache *info
  1547.     = s390_frame_unwind_cache (this_frame, this_prologue_cache);

  1548.   if (info->frame_base == -1)
  1549.     return;

  1550.   *this_id = frame_id_build (info->frame_base, info->func);
  1551. }

  1552. static struct value *
  1553. s390_frame_prev_register (struct frame_info *this_frame,
  1554.                           void **this_prologue_cache, int regnum)
  1555. {
  1556.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1557.   struct s390_unwind_cache *info
  1558.     = s390_frame_unwind_cache (this_frame, this_prologue_cache);

  1559.   return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
  1560. }

  1561. static const struct frame_unwind s390_frame_unwind = {
  1562.   NORMAL_FRAME,
  1563.   default_frame_unwind_stop_reason,
  1564.   s390_frame_this_id,
  1565.   s390_frame_prev_register,
  1566.   NULL,
  1567.   default_frame_sniffer
  1568. };


  1569. /* Code stubs and their stack frames.  For things like PLTs and NULL
  1570.    function calls (where there is no true frame and the return address
  1571.    is in the RETADDR register).  */

  1572. struct s390_stub_unwind_cache
  1573. {
  1574.   CORE_ADDR frame_base;
  1575.   struct trad_frame_saved_reg *saved_regs;
  1576. };

  1577. static struct s390_stub_unwind_cache *
  1578. s390_stub_frame_unwind_cache (struct frame_info *this_frame,
  1579.                               void **this_prologue_cache)
  1580. {
  1581.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1582.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  1583.   struct s390_stub_unwind_cache *info;
  1584.   ULONGEST reg;

  1585.   if (*this_prologue_cache)
  1586.     return *this_prologue_cache;

  1587.   info = FRAME_OBSTACK_ZALLOC (struct s390_stub_unwind_cache);
  1588.   *this_prologue_cache = info;
  1589.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1590.   /* The return address is in register %r14.  */
  1591.   info->saved_regs[S390_PSWA_REGNUM].realreg = S390_RETADDR_REGNUM;

  1592.   /* Retrieve stack pointer and determine our frame base.  */
  1593.   reg = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
  1594.   info->frame_base = reg + 16*word_size + 32;

  1595.   return info;
  1596. }

  1597. static void
  1598. s390_stub_frame_this_id (struct frame_info *this_frame,
  1599.                          void **this_prologue_cache,
  1600.                          struct frame_id *this_id)
  1601. {
  1602.   struct s390_stub_unwind_cache *info
  1603.     = s390_stub_frame_unwind_cache (this_frame, this_prologue_cache);
  1604.   *this_id = frame_id_build (info->frame_base, get_frame_pc (this_frame));
  1605. }

  1606. static struct value *
  1607. s390_stub_frame_prev_register (struct frame_info *this_frame,
  1608.                                void **this_prologue_cache, int regnum)
  1609. {
  1610.   struct s390_stub_unwind_cache *info
  1611.     = s390_stub_frame_unwind_cache (this_frame, this_prologue_cache);
  1612.   return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
  1613. }

  1614. static int
  1615. s390_stub_frame_sniffer (const struct frame_unwind *self,
  1616.                          struct frame_info *this_frame,
  1617.                          void **this_prologue_cache)
  1618. {
  1619.   CORE_ADDR addr_in_block;
  1620.   bfd_byte insn[S390_MAX_INSTR_SIZE];

  1621.   /* If the current PC points to non-readable memory, we assume we
  1622.      have trapped due to an invalid function pointer call.  We handle
  1623.      the non-existing current function like a PLT stub.  */
  1624.   addr_in_block = get_frame_address_in_block (this_frame);
  1625.   if (in_plt_section (addr_in_block)
  1626.       || s390_readinstruction (insn, get_frame_pc (this_frame)) < 0)
  1627.     return 1;
  1628.   return 0;
  1629. }

  1630. static const struct frame_unwind s390_stub_frame_unwind = {
  1631.   NORMAL_FRAME,
  1632.   default_frame_unwind_stop_reason,
  1633.   s390_stub_frame_this_id,
  1634.   s390_stub_frame_prev_register,
  1635.   NULL,
  1636.   s390_stub_frame_sniffer
  1637. };


  1638. /* Signal trampoline stack frames.  */

  1639. struct s390_sigtramp_unwind_cache {
  1640.   CORE_ADDR frame_base;
  1641.   struct trad_frame_saved_reg *saved_regs;
  1642. };

  1643. static struct s390_sigtramp_unwind_cache *
  1644. s390_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
  1645.                                   void **this_prologue_cache)
  1646. {
  1647.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1648.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1649.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  1650.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1651.   struct s390_sigtramp_unwind_cache *info;
  1652.   ULONGEST this_sp, prev_sp;
  1653.   CORE_ADDR next_ra, next_cfa, sigreg_ptr, sigreg_high_off;
  1654.   int i;

  1655.   if (*this_prologue_cache)
  1656.     return *this_prologue_cache;

  1657.   info = FRAME_OBSTACK_ZALLOC (struct s390_sigtramp_unwind_cache);
  1658.   *this_prologue_cache = info;
  1659.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1660.   this_sp = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
  1661.   next_ra = get_frame_pc (this_frame);
  1662.   next_cfa = this_sp + 16*word_size + 32;

  1663.   /* New-style RT frame:
  1664.         retcode + alignment (8 bytes)
  1665.         siginfo (128 bytes)
  1666.         ucontext (contains sigregs at offset 5 words).  */
  1667.   if (next_ra == next_cfa)
  1668.     {
  1669.       sigreg_ptr = next_cfa + 8 + 128 + align_up (5*word_size, 8);
  1670.       /* sigregs are followed by uc_sigmask (8 bytes), then by the
  1671.          upper GPR halves if present.  */
  1672.       sigreg_high_off = 8;
  1673.     }

  1674.   /* Old-style RT frame and all non-RT frames:
  1675.         old signal mask (8 bytes)
  1676.         pointer to sigregs.  */
  1677.   else
  1678.     {
  1679.       sigreg_ptr = read_memory_unsigned_integer (next_cfa + 8,
  1680.                                                  word_size, byte_order);
  1681.       /* sigregs are followed by signo (4 bytes), then by the
  1682.          upper GPR halves if present.  */
  1683.       sigreg_high_off = 4;
  1684.     }

  1685.   /* The sigregs structure looks like this:
  1686.             long   psw_mask;
  1687.             long   psw_addr;
  1688.             long   gprs[16];
  1689.             int    acrs[16];
  1690.             int    fpc;
  1691.             int    __pad;
  1692.             double fprs[16];  */

  1693.   /* PSW mask and address.  */
  1694.   info->saved_regs[S390_PSWM_REGNUM].addr = sigreg_ptr;
  1695.   sigreg_ptr += word_size;
  1696.   info->saved_regs[S390_PSWA_REGNUM].addr = sigreg_ptr;
  1697.   sigreg_ptr += word_size;

  1698.   /* Then the GPRs.  */
  1699.   for (i = 0; i < 16; i++)
  1700.     {
  1701.       info->saved_regs[S390_R0_REGNUM + i].addr = sigreg_ptr;
  1702.       sigreg_ptr += word_size;
  1703.     }

  1704.   /* Then the ACRs.  */
  1705.   for (i = 0; i < 16; i++)
  1706.     {
  1707.       info->saved_regs[S390_A0_REGNUM + i].addr = sigreg_ptr;
  1708.       sigreg_ptr += 4;
  1709.     }

  1710.   /* The floating-point control word.  */
  1711.   info->saved_regs[S390_FPC_REGNUM].addr = sigreg_ptr;
  1712.   sigreg_ptr += 8;

  1713.   /* And finally the FPRs.  */
  1714.   for (i = 0; i < 16; i++)
  1715.     {
  1716.       info->saved_regs[S390_F0_REGNUM + i].addr = sigreg_ptr;
  1717.       sigreg_ptr += 8;
  1718.     }

  1719.   /* If we have them, the GPR upper halves are appended at the end.  */
  1720.   sigreg_ptr += sigreg_high_off;
  1721.   if (tdep->gpr_full_regnum != -1)
  1722.     for (i = 0; i < 16; i++)
  1723.       {
  1724.         info->saved_regs[S390_R0_UPPER_REGNUM + i].addr = sigreg_ptr;
  1725.         sigreg_ptr += 4;
  1726.       }

  1727.   /* Restore the previous frame's SP.  */
  1728.   prev_sp = read_memory_unsigned_integer (
  1729.                         info->saved_regs[S390_SP_REGNUM].addr,
  1730.                         word_size, byte_order);

  1731.   /* Determine our frame base.  */
  1732.   info->frame_base = prev_sp + 16*word_size + 32;

  1733.   return info;
  1734. }

  1735. static void
  1736. s390_sigtramp_frame_this_id (struct frame_info *this_frame,
  1737.                              void **this_prologue_cache,
  1738.                              struct frame_id *this_id)
  1739. {
  1740.   struct s390_sigtramp_unwind_cache *info
  1741.     = s390_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
  1742.   *this_id = frame_id_build (info->frame_base, get_frame_pc (this_frame));
  1743. }

  1744. static struct value *
  1745. s390_sigtramp_frame_prev_register (struct frame_info *this_frame,
  1746.                                    void **this_prologue_cache, int regnum)
  1747. {
  1748.   struct s390_sigtramp_unwind_cache *info
  1749.     = s390_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
  1750.   return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
  1751. }

  1752. static int
  1753. s390_sigtramp_frame_sniffer (const struct frame_unwind *self,
  1754.                              struct frame_info *this_frame,
  1755.                              void **this_prologue_cache)
  1756. {
  1757.   CORE_ADDR pc = get_frame_pc (this_frame);
  1758.   bfd_byte sigreturn[2];

  1759.   if (target_read_memory (pc, sigreturn, 2))
  1760.     return 0;

  1761.   if (sigreturn[0] != op_svc)
  1762.     return 0;

  1763.   if (sigreturn[1] != 119 /* sigreturn */
  1764.       && sigreturn[1] != 173 /* rt_sigreturn */)
  1765.     return 0;

  1766.   return 1;
  1767. }

  1768. static const struct frame_unwind s390_sigtramp_frame_unwind = {
  1769.   SIGTRAMP_FRAME,
  1770.   default_frame_unwind_stop_reason,
  1771.   s390_sigtramp_frame_this_id,
  1772.   s390_sigtramp_frame_prev_register,
  1773.   NULL,
  1774.   s390_sigtramp_frame_sniffer
  1775. };

  1776. /* Retrieve the syscall number at a ptrace syscall-stop.  Return -1
  1777.    upon error. */

  1778. static LONGEST
  1779. s390_linux_get_syscall_number (struct gdbarch *gdbarch,
  1780.                                ptid_t ptid)
  1781. {
  1782.   struct regcache *regs = get_thread_regcache (ptid);
  1783.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1784.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1785.   ULONGEST pc;
  1786.   ULONGEST svc_number = -1;
  1787.   unsigned opcode;

  1788.   /* Assume that the PC points after the 2-byte SVC instruction.  We
  1789.      don't currently support SVC via EXECUTE. */
  1790.   regcache_cooked_read_unsigned (regs, tdep->pc_regnum, &pc);
  1791.   pc -= 2;
  1792.   opcode = read_memory_unsigned_integer ((CORE_ADDR) pc, 1, byte_order);
  1793.   if (opcode != op_svc)
  1794.     return -1;

  1795.   svc_number = read_memory_unsigned_integer ((CORE_ADDR) pc + 1, 1,
  1796.                                              byte_order);
  1797.   if (svc_number == 0)
  1798.     regcache_cooked_read_unsigned (regs, S390_R1_REGNUM, &svc_number);

  1799.   return svc_number;
  1800. }


  1801. /* Frame base handling.  */

  1802. static CORE_ADDR
  1803. s390_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1804. {
  1805.   struct s390_unwind_cache *info
  1806.     = s390_frame_unwind_cache (this_frame, this_cache);
  1807.   return info->frame_base;
  1808. }

  1809. static CORE_ADDR
  1810. s390_local_base_address (struct frame_info *this_frame, void **this_cache)
  1811. {
  1812.   struct s390_unwind_cache *info
  1813.     = s390_frame_unwind_cache (this_frame, this_cache);
  1814.   return info->local_base;
  1815. }

  1816. static const struct frame_base s390_frame_base = {
  1817.   &s390_frame_unwind,
  1818.   s390_frame_base_address,
  1819.   s390_local_base_address,
  1820.   s390_local_base_address
  1821. };

  1822. static CORE_ADDR
  1823. s390_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1824. {
  1825.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1826.   ULONGEST pc;
  1827.   pc = frame_unwind_register_unsigned (next_frame, tdep->pc_regnum);
  1828.   return gdbarch_addr_bits_remove (gdbarch, pc);
  1829. }

  1830. static CORE_ADDR
  1831. s390_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1832. {
  1833.   ULONGEST sp;
  1834.   sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
  1835.   return gdbarch_addr_bits_remove (gdbarch, sp);
  1836. }


  1837. /* DWARF-2 frame support.  */

  1838. static struct value *
  1839. s390_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
  1840.                            int regnum)
  1841. {
  1842.   return s390_unwind_pseudo_register (this_frame, regnum);
  1843. }

  1844. static void
  1845. s390_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1846.                             struct dwarf2_frame_state_reg *reg,
  1847.                             struct frame_info *this_frame)
  1848. {
  1849.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1850.   /* The condition code (and thus PSW mask) is call-clobbered.  */
  1851.   if (regnum == S390_PSWM_REGNUM)
  1852.     reg->how = DWARF2_FRAME_REG_UNDEFINED;

  1853.   /* The PSW address unwinds to the return address.  */
  1854.   else if (regnum == S390_PSWA_REGNUM)
  1855.     reg->how = DWARF2_FRAME_REG_RA;

  1856.   /* Fixed registers are call-saved or call-clobbered
  1857.      depending on the ABI in use.  */
  1858.   else if (regnum < S390_NUM_REGS)
  1859.     {
  1860.       if (s390_register_call_saved (gdbarch, regnum))
  1861.         reg->how = DWARF2_FRAME_REG_SAME_VALUE;
  1862.       else
  1863.         reg->how = DWARF2_FRAME_REG_UNDEFINED;
  1864.     }

  1865.   /* We install a special function to unwind pseudos.  */
  1866.   else
  1867.     {
  1868.       reg->how = DWARF2_FRAME_REG_FN;
  1869.       reg->loc.fn = s390_dwarf2_prev_register;
  1870.     }
  1871. }


  1872. /* Dummy function calls.  */

  1873. /* Return non-zero if TYPE is an integer-like type, zero otherwise.
  1874.    "Integer-like" types are those that should be passed the way
  1875.    integers are: integers, enums, ranges, characters, and booleans.  */
  1876. static int
  1877. is_integer_like (struct type *type)
  1878. {
  1879.   enum type_code code = TYPE_CODE (type);

  1880.   return (code == TYPE_CODE_INT
  1881.           || code == TYPE_CODE_ENUM
  1882.           || code == TYPE_CODE_RANGE
  1883.           || code == TYPE_CODE_CHAR
  1884.           || code == TYPE_CODE_BOOL);
  1885. }

  1886. /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
  1887.    "Pointer-like" types are those that should be passed the way
  1888.    pointers are: pointers and references.  */
  1889. static int
  1890. is_pointer_like (struct type *type)
  1891. {
  1892.   enum type_code code = TYPE_CODE (type);

  1893.   return (code == TYPE_CODE_PTR
  1894.           || code == TYPE_CODE_REF);
  1895. }


  1896. /* Return non-zero if TYPE is a `float singleton' or `double
  1897.    singleton', zero otherwise.

  1898.    A `T singleton' is a struct type with one member, whose type is
  1899.    either T or a `T singleton'.  So, the following are all float
  1900.    singletons:

  1901.    struct { float x };
  1902.    struct { struct { float x; } x; };
  1903.    struct { struct { struct { float x; } x; } x; };

  1904.    ... and so on.

  1905.    All such structures are passed as if they were floats or doubles,
  1906.    as the (revised) ABI says.  */
  1907. static int
  1908. is_float_singleton (struct type *type)
  1909. {
  1910.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
  1911.     {
  1912.       struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
  1913.       CHECK_TYPEDEF (singleton_type);

  1914.       return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
  1915.               || TYPE_CODE (singleton_type) == TYPE_CODE_DECFLOAT
  1916.               || is_float_singleton (singleton_type));
  1917.     }

  1918.   return 0;
  1919. }


  1920. /* Return non-zero if TYPE is a struct-like type, zero otherwise.
  1921.    "Struct-like" types are those that should be passed as structs are:
  1922.    structs and unions.

  1923.    As an odd quirk, not mentioned in the ABI, GCC passes float and
  1924.    double singletons as if they were a plain float, double, etc.  (The
  1925.    corresponding union types are handled normally.)  So we exclude
  1926.    those types here.  *shrug* */
  1927. static int
  1928. is_struct_like (struct type *type)
  1929. {
  1930.   enum type_code code = TYPE_CODE (type);

  1931.   return (code == TYPE_CODE_UNION
  1932.           || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
  1933. }


  1934. /* Return non-zero if TYPE is a float-like type, zero otherwise.
  1935.    "Float-like" types are those that should be passed as
  1936.    floating-point values are.

  1937.    You'd think this would just be floats, doubles, long doubles, etc.
  1938.    But as an odd quirk, not mentioned in the ABI, GCC passes float and
  1939.    double singletons as if they were a plain float, double, etc.  (The
  1940.    corresponding union types are handled normally.)  So we include
  1941.    those types here.  *shrug* */
  1942. static int
  1943. is_float_like (struct type *type)
  1944. {
  1945.   return (TYPE_CODE (type) == TYPE_CODE_FLT
  1946.           || TYPE_CODE (type) == TYPE_CODE_DECFLOAT
  1947.           || is_float_singleton (type));
  1948. }


  1949. static int
  1950. is_power_of_two (unsigned int n)
  1951. {
  1952.   return ((n & (n - 1)) == 0);
  1953. }

  1954. /* Return non-zero if TYPE should be passed as a pointer to a copy,
  1955.    zero otherwise.  */
  1956. static int
  1957. s390_function_arg_pass_by_reference (struct type *type)
  1958. {
  1959.   if (TYPE_LENGTH (type) > 8)
  1960.     return 1;

  1961.   return (is_struct_like (type) && !is_power_of_two (TYPE_LENGTH (type)))
  1962.           || TYPE_CODE (type) == TYPE_CODE_COMPLEX
  1963.           || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type));
  1964. }

  1965. /* Return non-zero if TYPE should be passed in a float register
  1966.    if possible.  */
  1967. static int
  1968. s390_function_arg_float (struct type *type)
  1969. {
  1970.   if (TYPE_LENGTH (type) > 8)
  1971.     return 0;

  1972.   return is_float_like (type);
  1973. }

  1974. /* Return non-zero if TYPE should be passed in an integer register
  1975.    (or a pair of integer registers) if possible.  */
  1976. static int
  1977. s390_function_arg_integer (struct type *type)
  1978. {
  1979.   if (TYPE_LENGTH (type) > 8)
  1980.     return 0;

  1981.    return is_integer_like (type)
  1982.           || is_pointer_like (type)
  1983.           || (is_struct_like (type) && is_power_of_two (TYPE_LENGTH (type)));
  1984. }

  1985. /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
  1986.    word as required for the ABI.  */
  1987. static LONGEST
  1988. extend_simple_arg (struct gdbarch *gdbarch, struct value *arg)
  1989. {
  1990.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1991.   struct type *type = check_typedef (value_type (arg));

  1992.   /* Even structs get passed in the least significant bits of the
  1993.      register / memory word.  It's not really right to extract them as
  1994.      an integer, but it does take care of the extension.  */
  1995.   if (TYPE_UNSIGNED (type))
  1996.     return extract_unsigned_integer (value_contents (arg),
  1997.                                      TYPE_LENGTH (type), byte_order);
  1998.   else
  1999.     return extract_signed_integer (value_contents (arg),
  2000.                                    TYPE_LENGTH (type), byte_order);
  2001. }


  2002. /* Return the alignment required by TYPE.  */
  2003. static int
  2004. alignment_of (struct type *type)
  2005. {
  2006.   int alignment;

  2007.   if (is_integer_like (type)
  2008.       || is_pointer_like (type)
  2009.       || TYPE_CODE (type) == TYPE_CODE_FLT
  2010.       || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  2011.     alignment = TYPE_LENGTH (type);
  2012.   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  2013.            || TYPE_CODE (type) == TYPE_CODE_UNION)
  2014.     {
  2015.       int i;

  2016.       alignment = 1;
  2017.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  2018.         {
  2019.           int field_alignment
  2020.             = alignment_of (check_typedef (TYPE_FIELD_TYPE (type, i)));

  2021.           if (field_alignment > alignment)
  2022.             alignment = field_alignment;
  2023.         }
  2024.     }
  2025.   else
  2026.     alignment = 1;

  2027.   /* Check that everything we ever return is a power of two.  Lots of
  2028.      code doesn't want to deal with aligning things to arbitrary
  2029.      boundaries.  */
  2030.   gdb_assert ((alignment & (alignment - 1)) == 0);

  2031.   return alignment;
  2032. }


  2033. /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
  2034.    place to be passed to a function, as specified by the "GNU/Linux
  2035.    for S/390 ELF Application Binary Interface Supplement".

  2036.    SP is the current stack pointer.  We must put arguments, links,
  2037.    padding, etc. whereever they belong, and return the new stack
  2038.    pointer value.

  2039.    If STRUCT_RETURN is non-zero, then the function we're calling is
  2040.    going to return a structure by value; STRUCT_ADDR is the address of
  2041.    a block we've allocated for it on the stack.

  2042.    Our caller has taken care of any type promotions needed to satisfy
  2043.    prototypes or the old K&R argument-passing rules.  */
  2044. static CORE_ADDR
  2045. s390_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  2046.                       struct regcache *regcache, CORE_ADDR bp_addr,
  2047.                       int nargs, struct value **args, CORE_ADDR sp,
  2048.                       int struct_return, CORE_ADDR struct_addr)
  2049. {
  2050.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  2051.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  2052.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2053.   int i;

  2054.   /* If the i'th argument is passed as a reference to a copy, then
  2055.      copy_addr[i] is the address of the copy we made.  */
  2056.   CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));

  2057.   /* Reserve space for the reference-to-copy area.  */
  2058.   for (i = 0; i < nargs; i++)
  2059.     {
  2060.       struct value *arg = args[i];
  2061.       struct type *type = check_typedef (value_type (arg));

  2062.       if (s390_function_arg_pass_by_reference (type))
  2063.         {
  2064.           sp -= TYPE_LENGTH (type);
  2065.           sp = align_down (sp, alignment_of (type));
  2066.           copy_addr[i] = sp;
  2067.         }
  2068.     }

  2069.   /* Reserve space for the parameter area.  As a conservative
  2070.      simplification, we assume that everything will be passed on the
  2071.      stack.  Since every argument larger than 8 bytes will be
  2072.      passed by reference, we use this simple upper bound.  */
  2073.   sp -= nargs * 8;

  2074.   /* After all that, make sure it's still aligned on an eight-byte
  2075.      boundary.  */
  2076.   sp = align_down (sp, 8);

  2077.   /* Allocate the standard frame areas: the register save area, the
  2078.      word reserved for the compiler (which seems kind of meaningless),
  2079.      and the back chain pointer.  */
  2080.   sp -= 16*word_size + 32;

  2081.   /* Now we have the final SP value.  Make sure we didn't underflow;
  2082.      on 31-bit, this would result in addresses with the high bit set,
  2083.      which causes confusion elsewhere.  Note that if we error out
  2084.      here, stack and registers remain untouched.  */
  2085.   if (gdbarch_addr_bits_remove (gdbarch, sp) != sp)
  2086.     error (_("Stack overflow"));


  2087.   /* Finally, place the actual parameters, working from SP towards
  2088.      higher addresses.  The code above is supposed to reserve enough
  2089.      space for this.  */
  2090.   {
  2091.     int fr = 0;
  2092.     int gr = 2;
  2093.     CORE_ADDR starg = sp + 16*word_size + 32;

  2094.     /* A struct is returned using general register 2.  */
  2095.     if (struct_return)
  2096.       {
  2097.         regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
  2098.                                         struct_addr);
  2099.         gr++;
  2100.       }

  2101.     for (i = 0; i < nargs; i++)
  2102.       {
  2103.         struct value *arg = args[i];
  2104.         struct type *type = check_typedef (value_type (arg));
  2105.         unsigned length = TYPE_LENGTH (type);

  2106.         if (s390_function_arg_pass_by_reference (type))
  2107.           {
  2108.             /* Actually copy the argument contents to the stack slot
  2109.                that was reserved above.  */
  2110.             write_memory (copy_addr[i], value_contents (arg), length);

  2111.             if (gr <= 6)
  2112.               {
  2113.                 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
  2114.                                                 copy_addr[i]);
  2115.                 gr++;
  2116.               }
  2117.             else
  2118.               {
  2119.                 write_memory_unsigned_integer (starg, word_size, byte_order,
  2120.                                                copy_addr[i]);
  2121.                 starg += word_size;
  2122.               }
  2123.           }
  2124.         else if (s390_function_arg_float (type))
  2125.           {
  2126.             /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
  2127.                the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6.  */
  2128.             if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
  2129.               {
  2130.                 /* When we store a single-precision value in an FP register,
  2131.                    it occupies the leftmost bits.  */
  2132.                 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
  2133.                                             0, length, value_contents (arg));
  2134.                 fr += 2;
  2135.               }
  2136.             else
  2137.               {
  2138.                 /* When we store a single-precision value in a stack slot,
  2139.                    it occupies the rightmost bits.  */
  2140.                 starg = align_up (starg + length, word_size);
  2141.                 write_memory (starg - length, value_contents (arg), length);
  2142.               }
  2143.           }
  2144.         else if (s390_function_arg_integer (type) && length <= word_size)
  2145.           {
  2146.             if (gr <= 6)
  2147.               {
  2148.                 /* Integer arguments are always extended to word size.  */
  2149.                 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
  2150.                                               extend_simple_arg (gdbarch,
  2151.                                                                  arg));
  2152.                 gr++;
  2153.               }
  2154.             else
  2155.               {
  2156.                 /* Integer arguments are always extended to word size.  */
  2157.                 write_memory_signed_integer (starg, word_size, byte_order,
  2158.                                              extend_simple_arg (gdbarch, arg));
  2159.                 starg += word_size;
  2160.               }
  2161.           }
  2162.         else if (s390_function_arg_integer (type) && length == 2*word_size)
  2163.           {
  2164.             if (gr <= 5)
  2165.               {
  2166.                 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
  2167.                                        value_contents (arg));
  2168.                 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
  2169.                                        value_contents (arg) + word_size);
  2170.                 gr += 2;
  2171.               }
  2172.             else
  2173.               {
  2174.                 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
  2175.                    in it, then don't go back and use it again later.  */
  2176.                 gr = 7;

  2177.                 write_memory (starg, value_contents (arg), length);
  2178.                 starg += length;
  2179.               }
  2180.           }
  2181.         else
  2182.           internal_error (__FILE__, __LINE__, _("unknown argument type"));
  2183.       }
  2184.   }

  2185.   /* Store return PSWA.  In 31-bit mode, keep addressing mode bit.  */
  2186.   if (word_size == 4)
  2187.     {
  2188.       ULONGEST pswa;
  2189.       regcache_cooked_read_unsigned (regcache, S390_PSWA_REGNUM, &pswa);
  2190.       bp_addr = (bp_addr & 0x7fffffff) | (pswa & 0x80000000);
  2191.     }
  2192.   regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);

  2193.   /* Store updated stack pointer.  */
  2194.   regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);

  2195.   /* We need to return the 'stack part' of the frame ID,
  2196.      which is actually the top of the register save area.  */
  2197.   return sp + 16*word_size + 32;
  2198. }

  2199. /* Assuming THIS_FRAME is a dummy, return the frame ID of that
  2200.    dummy frame.  The frame ID's base needs to match the TOS value
  2201.    returned by push_dummy_call, and the PC match the dummy frame's
  2202.    breakpoint.  */
  2203. static struct frame_id
  2204. s390_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2205. {
  2206.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  2207.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
  2208.   sp = gdbarch_addr_bits_remove (gdbarch, sp);

  2209.   return frame_id_build (sp + 16*word_size + 32,
  2210.                          get_frame_pc (this_frame));
  2211. }

  2212. static CORE_ADDR
  2213. s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  2214. {
  2215.   /* Both the 32- and 64-bit ABI's say that the stack pointer should
  2216.      always be aligned on an eight-byte boundary.  */
  2217.   return (addr & -8);
  2218. }


  2219. /* Function return value access.  */

  2220. static enum return_value_convention
  2221. s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
  2222. {
  2223.   if (TYPE_LENGTH (type) > 8)
  2224.     return RETURN_VALUE_STRUCT_CONVENTION;

  2225.   switch (TYPE_CODE (type))
  2226.     {
  2227.     case TYPE_CODE_STRUCT:
  2228.     case TYPE_CODE_UNION:
  2229.     case TYPE_CODE_ARRAY:
  2230.     case TYPE_CODE_COMPLEX:
  2231.       return RETURN_VALUE_STRUCT_CONVENTION;

  2232.     default:
  2233.       return RETURN_VALUE_REGISTER_CONVENTION;
  2234.     }
  2235. }

  2236. static enum return_value_convention
  2237. s390_return_value (struct gdbarch *gdbarch, struct value *function,
  2238.                    struct type *type, struct regcache *regcache,
  2239.                    gdb_byte *out, const gdb_byte *in)
  2240. {
  2241.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2242.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  2243.   enum return_value_convention rvc;
  2244.   int length;

  2245.   type = check_typedef (type);
  2246.   rvc = s390_return_value_convention (gdbarch, type);
  2247.   length = TYPE_LENGTH (type);

  2248.   if (in)
  2249.     {
  2250.       switch (rvc)
  2251.         {
  2252.         case RETURN_VALUE_REGISTER_CONVENTION:
  2253.           if (TYPE_CODE (type) == TYPE_CODE_FLT
  2254.               || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  2255.             {
  2256.               /* When we store a single-precision value in an FP register,
  2257.                  it occupies the leftmost bits.  */
  2258.               regcache_cooked_write_part (regcache, S390_F0_REGNUM,
  2259.                                           0, length, in);
  2260.             }
  2261.           else if (length <= word_size)
  2262.             {
  2263.               /* Integer arguments are always extended to word size.  */
  2264.               if (TYPE_UNSIGNED (type))
  2265.                 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
  2266.                         extract_unsigned_integer (in, length, byte_order));
  2267.               else
  2268.                 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
  2269.                         extract_signed_integer (in, length, byte_order));
  2270.             }
  2271.           else if (length == 2*word_size)
  2272.             {
  2273.               regcache_cooked_write (regcache, S390_R2_REGNUM, in);
  2274.               regcache_cooked_write (regcache, S390_R3_REGNUM, in + word_size);
  2275.             }
  2276.           else
  2277.             internal_error (__FILE__, __LINE__, _("invalid return type"));
  2278.           break;

  2279.         case RETURN_VALUE_STRUCT_CONVENTION:
  2280.           error (_("Cannot set function return value."));
  2281.           break;
  2282.         }
  2283.     }
  2284.   else if (out)
  2285.     {
  2286.       switch (rvc)
  2287.         {
  2288.         case RETURN_VALUE_REGISTER_CONVENTION:
  2289.           if (TYPE_CODE (type) == TYPE_CODE_FLT
  2290.               || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  2291.             {
  2292.               /* When we store a single-precision value in an FP register,
  2293.                  it occupies the leftmost bits.  */
  2294.               regcache_cooked_read_part (regcache, S390_F0_REGNUM,
  2295.                                          0, length, out);
  2296.             }
  2297.           else if (length <= word_size)
  2298.             {
  2299.               /* Integer arguments occupy the rightmost bits.  */
  2300.               regcache_cooked_read_part (regcache, S390_R2_REGNUM,
  2301.                                          word_size - length, length, out);
  2302.             }
  2303.           else if (length == 2*word_size)
  2304.             {
  2305.               regcache_cooked_read (regcache, S390_R2_REGNUM, out);
  2306.               regcache_cooked_read (regcache, S390_R3_REGNUM, out + word_size);
  2307.             }
  2308.           else
  2309.             internal_error (__FILE__, __LINE__, _("invalid return type"));
  2310.           break;

  2311.         case RETURN_VALUE_STRUCT_CONVENTION:
  2312.           error (_("Function return value unknown."));
  2313.           break;
  2314.         }
  2315.     }

  2316.   return rvc;
  2317. }


  2318. /* Breakpoints.  */

  2319. static const gdb_byte *
  2320. s390_breakpoint_from_pc (struct gdbarch *gdbarch,
  2321.                          CORE_ADDR *pcptr, int *lenptr)
  2322. {
  2323.   static const gdb_byte breakpoint[] = { 0x0, 0x1 };

  2324.   *lenptr = sizeof (breakpoint);
  2325.   return breakpoint;
  2326. }


  2327. /* Address handling.  */

  2328. static CORE_ADDR
  2329. s390_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  2330. {
  2331.   return addr & 0x7fffffff;
  2332. }

  2333. static int
  2334. s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
  2335. {
  2336.   if (byte_size == 4)
  2337.     return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
  2338.   else
  2339.     return 0;
  2340. }

  2341. static const char *
  2342. s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
  2343. {
  2344.   if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
  2345.     return "mode32";
  2346.   else
  2347.     return NULL;
  2348. }

  2349. static int
  2350. s390_address_class_name_to_type_flags (struct gdbarch *gdbarch,
  2351.                                        const char *name,
  2352.                                        int *type_flags_ptr)
  2353. {
  2354.   if (strcmp (name, "mode32") == 0)
  2355.     {
  2356.       *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
  2357.       return 1;
  2358.     }
  2359.   else
  2360.     return 0;
  2361. }

  2362. /* Implement gdbarch_gcc_target_options.  GCC does not know "-m32".  */

  2363. static char *
  2364. s390_gcc_target_options (struct gdbarch *gdbarch)
  2365. {
  2366.   return xstrdup ("-m31");
  2367. }

  2368. /* Implementation of `gdbarch_stap_is_single_operand', as defined in
  2369.    gdbarch.h.  */

  2370. static int
  2371. s390_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
  2372. {
  2373.   return ((isdigit (*s) && s[1] == '(' && s[2] == '%') /* Displacement
  2374.                                                           or indirection.  */
  2375.           || *s == '%' /* Register access.  */
  2376.           || isdigit (*s)); /* Literal number.  */
  2377. }

  2378. /* Set up gdbarch struct.  */

  2379. static struct gdbarch *
  2380. s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2381. {
  2382.   const struct target_desc *tdesc = info.target_desc;
  2383.   struct tdesc_arch_data *tdesc_data = NULL;
  2384.   struct gdbarch *gdbarch;
  2385.   struct gdbarch_tdep *tdep;
  2386.   int tdep_abi;
  2387.   int have_upper = 0;
  2388.   int have_linux_v1 = 0;
  2389.   int have_linux_v2 = 0;
  2390.   int have_tdb = 0;
  2391.   int first_pseudo_reg, last_pseudo_reg;
  2392.   static const char *const stap_register_prefixes[] = { "%", NULL };
  2393.   static const char *const stap_register_indirection_prefixes[] = { "(",
  2394.                                                                     NULL };
  2395.   static const char *const stap_register_indirection_suffixes[] = { ")",
  2396.                                                                     NULL };

  2397.   /* Default ABI and register size.  */
  2398.   switch (info.bfd_arch_info->mach)
  2399.     {
  2400.     case bfd_mach_s390_31:
  2401.       tdep_abi = ABI_LINUX_S390;
  2402.       break;

  2403.     case bfd_mach_s390_64:
  2404.       tdep_abi = ABI_LINUX_ZSERIES;
  2405.       break;

  2406.     default:
  2407.       return NULL;
  2408.     }

  2409.   /* Use default target description if none provided by the target.  */
  2410.   if (!tdesc_has_registers (tdesc))
  2411.     {
  2412.       if (tdep_abi == ABI_LINUX_S390)
  2413.         tdesc = tdesc_s390_linux32;
  2414.       else
  2415.         tdesc = tdesc_s390x_linux64;
  2416.     }

  2417.   /* Check any target description for validity.  */
  2418.   if (tdesc_has_registers (tdesc))
  2419.     {
  2420.       static const char *const gprs[] = {
  2421.         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  2422.         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  2423.       };
  2424.       static const char *const fprs[] = {
  2425.         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  2426.         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"
  2427.       };
  2428.       static const char *const acrs[] = {
  2429.         "acr0", "acr1", "acr2", "acr3", "acr4", "acr5", "acr6", "acr7",
  2430.         "acr8", "acr9", "acr10", "acr11", "acr12", "acr13", "acr14", "acr15"
  2431.       };
  2432.       static const char *const gprs_lower[] = {
  2433.         "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l",
  2434.         "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
  2435.       };
  2436.       static const char *const gprs_upper[] = {
  2437.         "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
  2438.         "r8h", "r9h", "r10h", "r11h", "r12h", "r13h", "r14h", "r15h"
  2439.       };
  2440.       static const char *const tdb_regs[] = {
  2441.         "tdb0", "tac", "tct", "atia",
  2442.         "tr0", "tr1", "tr2", "tr3", "tr4", "tr5", "tr6", "tr7",
  2443.         "tr8", "tr9", "tr10", "tr11", "tr12", "tr13", "tr14", "tr15"
  2444.       };
  2445.       const struct tdesc_feature *feature;
  2446.       int i, valid_p = 1;

  2447.       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.core");
  2448.       if (feature == NULL)
  2449.         return NULL;

  2450.       tdesc_data = tdesc_data_alloc ();

  2451.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2452.                                           S390_PSWM_REGNUM, "pswm");
  2453.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2454.                                           S390_PSWA_REGNUM, "pswa");

  2455.       if (tdesc_unnumbered_register (feature, "r0"))
  2456.         {
  2457.           for (i = 0; i < 16; i++)
  2458.             valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2459.                                                 S390_R0_REGNUM + i, gprs[i]);
  2460.         }
  2461.       else
  2462.         {
  2463.           have_upper = 1;

  2464.           for (i = 0; i < 16; i++)
  2465.             valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2466.                                                 S390_R0_REGNUM + i,
  2467.                                                 gprs_lower[i]);
  2468.           for (i = 0; i < 16; i++)
  2469.             valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2470.                                                 S390_R0_UPPER_REGNUM + i,
  2471.                                                 gprs_upper[i]);
  2472.         }

  2473.       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.fpr");
  2474.       if (feature == NULL)
  2475.         {
  2476.           tdesc_data_cleanup (tdesc_data);
  2477.           return NULL;
  2478.         }

  2479.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2480.                                           S390_FPC_REGNUM, "fpc");
  2481.       for (i = 0; i < 16; i++)
  2482.         valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2483.                                             S390_F0_REGNUM + i, fprs[i]);

  2484.       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.acr");
  2485.       if (feature == NULL)
  2486.         {
  2487.           tdesc_data_cleanup (tdesc_data);
  2488.           return NULL;
  2489.         }

  2490.       for (i = 0; i < 16; i++)
  2491.         valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2492.                                             S390_A0_REGNUM + i, acrs[i]);

  2493.       /* Optional GNU/Linux-specific "registers".  */
  2494.       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.linux");
  2495.       if (feature)
  2496.         {
  2497.           tdesc_numbered_register (feature, tdesc_data,
  2498.                                    S390_ORIG_R2_REGNUM, "orig_r2");

  2499.           if (tdesc_numbered_register (feature, tdesc_data,
  2500.                                        S390_LAST_BREAK_REGNUM, "last_break"))
  2501.             have_linux_v1 = 1;

  2502.           if (tdesc_numbered_register (feature, tdesc_data,
  2503.                                        S390_SYSTEM_CALL_REGNUM, "system_call"))
  2504.             have_linux_v2 = 1;

  2505.           if (have_linux_v2 > have_linux_v1)
  2506.             valid_p = 0;
  2507.         }

  2508.       /* Transaction diagnostic block.  */
  2509.       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.s390.tdb");
  2510.       if (feature)
  2511.         {
  2512.           for (i = 0; i < ARRAY_SIZE (tdb_regs); i++)
  2513.             valid_p &= tdesc_numbered_register (feature, tdesc_data,
  2514.                                                 S390_TDB_DWORD0_REGNUM + i,
  2515.                                                 tdb_regs[i]);
  2516.           have_tdb = 1;
  2517.         }

  2518.       if (!valid_p)
  2519.         {
  2520.           tdesc_data_cleanup (tdesc_data);
  2521.           return NULL;
  2522.         }
  2523.     }

  2524.   /* Find a candidate among extant architectures.  */
  2525.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  2526.        arches != NULL;
  2527.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  2528.     {
  2529.       tdep = gdbarch_tdep (arches->gdbarch);
  2530.       if (!tdep)
  2531.         continue;
  2532.       if (tdep->abi != tdep_abi)
  2533.         continue;
  2534.       if ((tdep->gpr_full_regnum != -1) != have_upper)
  2535.         continue;
  2536.       if (tdesc_data != NULL)
  2537.         tdesc_data_cleanup (tdesc_data);
  2538.       return arches->gdbarch;
  2539.     }

  2540.   /* Otherwise create a new gdbarch for the specified machine type.  */
  2541.   tdep = XCNEW (struct gdbarch_tdep);
  2542.   tdep->abi = tdep_abi;
  2543.   tdep->have_linux_v1 = have_linux_v1;
  2544.   tdep->have_linux_v2 = have_linux_v2;
  2545.   tdep->have_tdb = have_tdb;
  2546.   gdbarch = gdbarch_alloc (&info, tdep);

  2547.   set_gdbarch_believe_pcc_promotion (gdbarch, 0);
  2548.   set_gdbarch_char_signed (gdbarch, 0);

  2549.   /* S/390 GNU/Linux uses either 64-bit or 128-bit long doubles.
  2550.      We can safely let them default to 128-bit, since the debug info
  2551.      will give the size of type actually used in each case.  */
  2552.   set_gdbarch_long_double_bit (gdbarch, 128);
  2553.   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);

  2554.   /* Amount PC must be decremented by after a breakpoint.  This is
  2555.      often the number of bytes returned by gdbarch_breakpoint_from_pc but not
  2556.      always.  */
  2557.   set_gdbarch_decr_pc_after_break (gdbarch, 2);
  2558.   /* Stack grows downward.  */
  2559.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2560.   set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
  2561.   set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
  2562.   set_gdbarch_in_function_epilogue_p (gdbarch, s390_in_function_epilogue_p);

  2563.   set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
  2564.   set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
  2565.   set_gdbarch_fp0_regnum (gdbarch, S390_F0_REGNUM);
  2566.   set_gdbarch_stab_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
  2567.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
  2568.   set_gdbarch_value_from_register (gdbarch, s390_value_from_register);
  2569.   set_gdbarch_core_read_description (gdbarch, s390_core_read_description);
  2570.   set_gdbarch_iterate_over_regset_sections (gdbarch,
  2571.                                             s390_iterate_over_regset_sections);
  2572.   set_gdbarch_cannot_store_register (gdbarch, s390_cannot_store_register);
  2573.   set_gdbarch_write_pc (gdbarch, s390_write_pc);
  2574.   set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
  2575.   set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
  2576.   set_tdesc_pseudo_register_name (gdbarch, s390_pseudo_register_name);
  2577.   set_tdesc_pseudo_register_type (gdbarch, s390_pseudo_register_type);
  2578.   set_tdesc_pseudo_register_reggroup_p (gdbarch,
  2579.                                         s390_pseudo_register_reggroup_p);
  2580.   tdesc_use_registers (gdbarch, tdesc, tdesc_data);

  2581.   /* Assign pseudo register numbers.  */
  2582.   first_pseudo_reg = gdbarch_num_regs (gdbarch);
  2583.   last_pseudo_reg = first_pseudo_reg;
  2584.   tdep->gpr_full_regnum = -1;
  2585.   if (have_upper)
  2586.     {
  2587.       tdep->gpr_full_regnum = last_pseudo_reg;
  2588.       last_pseudo_reg += 16;
  2589.     }
  2590.   tdep->pc_regnum = last_pseudo_reg++;
  2591.   tdep->cc_regnum = last_pseudo_reg++;
  2592.   set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
  2593.   set_gdbarch_num_pseudo_regs (gdbarch, last_pseudo_reg - first_pseudo_reg);

  2594.   /* Inferior function calls.  */
  2595.   set_gdbarch_push_dummy_call (gdbarch, s390_push_dummy_call);
  2596.   set_gdbarch_dummy_id (gdbarch, s390_dummy_id);
  2597.   set_gdbarch_frame_align (gdbarch, s390_frame_align);
  2598.   set_gdbarch_return_value (gdbarch, s390_return_value);

  2599.   /* Syscall handling.  */
  2600.   set_gdbarch_get_syscall_number (gdbarch, s390_linux_get_syscall_number);

  2601.   /* Frame handling.  */
  2602.   dwarf2_frame_set_init_reg (gdbarch, s390_dwarf2_frame_init_reg);
  2603.   dwarf2_frame_set_adjust_regnum (gdbarch, s390_adjust_frame_regnum);
  2604.   dwarf2_append_unwinders (gdbarch);
  2605.   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
  2606.   frame_unwind_append_unwinder (gdbarch, &s390_stub_frame_unwind);
  2607.   frame_unwind_append_unwinder (gdbarch, &s390_sigtramp_frame_unwind);
  2608.   frame_unwind_append_unwinder (gdbarch, &s390_frame_unwind);
  2609.   frame_base_set_default (gdbarch, &s390_frame_base);
  2610.   set_gdbarch_unwind_pc (gdbarch, s390_unwind_pc);
  2611.   set_gdbarch_unwind_sp (gdbarch, s390_unwind_sp);

  2612.   /* Displaced stepping.  */
  2613.   set_gdbarch_displaced_step_copy_insn (gdbarch,
  2614.                                         simple_displaced_step_copy_insn);
  2615.   set_gdbarch_displaced_step_fixup (gdbarch, s390_displaced_step_fixup);
  2616.   set_gdbarch_displaced_step_free_closure (gdbarch,
  2617.                                            simple_displaced_step_free_closure);
  2618.   set_gdbarch_displaced_step_location (gdbarch,
  2619.                                        displaced_step_at_entry_point);
  2620.   set_gdbarch_max_insn_length (gdbarch, S390_MAX_INSTR_SIZE);

  2621.   /* Note that GNU/Linux is the only OS supported on this
  2622.      platform.  */
  2623.   linux_init_abi (info, gdbarch);

  2624.   switch (tdep->abi)
  2625.     {
  2626.     case ABI_LINUX_S390:
  2627.       set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
  2628.       set_gdbarch_gcc_target_options (gdbarch, s390_gcc_target_options);
  2629.       set_solib_svr4_fetch_link_map_offsets
  2630.         (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  2631.       set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_S390);
  2632.       break;

  2633.     case ABI_LINUX_ZSERIES:
  2634.       set_gdbarch_long_bit (gdbarch, 64);
  2635.       set_gdbarch_long_long_bit (gdbarch, 64);
  2636.       set_gdbarch_ptr_bit (gdbarch, 64);
  2637.       set_solib_svr4_fetch_link_map_offsets
  2638.         (gdbarch, svr4_lp64_fetch_link_map_offsets);
  2639.       set_gdbarch_address_class_type_flags (gdbarch,
  2640.                                             s390_address_class_type_flags);
  2641.       set_gdbarch_address_class_type_flags_to_name (gdbarch,
  2642.                                                     s390_address_class_type_flags_to_name);
  2643.       set_gdbarch_address_class_name_to_type_flags (gdbarch,
  2644.                                                     s390_address_class_name_to_type_flags);
  2645.       set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_S390);
  2646.       break;
  2647.     }

  2648.   set_gdbarch_print_insn (gdbarch, print_insn_s390);

  2649.   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);

  2650.   /* Enable TLS support.  */
  2651.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  2652.                                              svr4_fetch_objfile_link_map);

  2653.   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);

  2654.   /* SystemTap functions.  */
  2655.   set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
  2656.   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
  2657.                                           stap_register_indirection_prefixes);
  2658.   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
  2659.                                           stap_register_indirection_suffixes);
  2660.   set_gdbarch_stap_is_single_operand (gdbarch, s390_stap_is_single_operand);

  2661.   return gdbarch;
  2662. }


  2663. extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */

  2664. void
  2665. _initialize_s390_tdep (void)
  2666. {
  2667.   /* Hook us into the gdbarch mechanism.  */
  2668.   register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);

  2669.   /* Initialize the GNU/Linux target descriptions.  */
  2670.   initialize_tdesc_s390_linux32 ();
  2671.   initialize_tdesc_s390_linux32v1 ();
  2672.   initialize_tdesc_s390_linux32v2 ();
  2673.   initialize_tdesc_s390_linux64 ();
  2674.   initialize_tdesc_s390_linux64v1 ();
  2675.   initialize_tdesc_s390_linux64v2 ();
  2676.   initialize_tdesc_s390_te_linux64 ();
  2677.   initialize_tdesc_s390x_linux64 ();
  2678.   initialize_tdesc_s390x_linux64v1 ();
  2679.   initialize_tdesc_s390x_linux64v2 ();
  2680.   initialize_tdesc_s390x_te_linux64 ();
  2681. }