gdb/spu-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* SPU target-dependent code for GDB, the GNU debugger.
  2.    Copyright (C) 2006-2015 Free Software Foundation, Inc.

  3.    Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
  4.    Based on a port by Sid Manning <sid@us.ibm.com>.

  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 "gdbtypes.h"
  19. #include "gdbcmd.h"
  20. #include "gdbcore.h"
  21. #include "frame.h"
  22. #include "frame-unwind.h"
  23. #include "frame-base.h"
  24. #include "trad-frame.h"
  25. #include "symtab.h"
  26. #include "symfile.h"
  27. #include "value.h"
  28. #include "inferior.h"
  29. #include "dis-asm.h"
  30. #include "objfiles.h"
  31. #include "language.h"
  32. #include "regcache.h"
  33. #include "reggroups.h"
  34. #include "floatformat.h"
  35. #include "block.h"
  36. #include "observer.h"
  37. #include "infcall.h"
  38. #include "dwarf2.h"
  39. #include "dwarf2-frame.h"
  40. #include "ax.h"
  41. #include "spu-tdep.h"


  42. /* The list of available "set spu " and "show spu " commands.  */
  43. static struct cmd_list_element *setspucmdlist = NULL;
  44. static struct cmd_list_element *showspucmdlist = NULL;

  45. /* Whether to stop for new SPE contexts.  */
  46. static int spu_stop_on_load_p = 0;
  47. /* Whether to automatically flush the SW-managed cache.  */
  48. static int spu_auto_flush_cache_p = 1;


  49. /* The tdep structure.  */
  50. struct gdbarch_tdep
  51. {
  52.   /* The spufs ID identifying our address space.  */
  53.   int id;

  54.   /* SPU-specific vector type.  */
  55.   struct type *spu_builtin_type_vec128;
  56. };


  57. /* SPU-specific vector type.  */
  58. static struct type *
  59. spu_builtin_type_vec128 (struct gdbarch *gdbarch)
  60. {
  61.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  62.   if (!tdep->spu_builtin_type_vec128)
  63.     {
  64.       const struct builtin_type *bt = builtin_type (gdbarch);
  65.       struct type *t;

  66.       t = arch_composite_type (gdbarch,
  67.                                "__spu_builtin_type_vec128", TYPE_CODE_UNION);
  68.       append_composite_type_field (t, "uint128", bt->builtin_int128);
  69.       append_composite_type_field (t, "v2_int64",
  70.                                    init_vector_type (bt->builtin_int64, 2));
  71.       append_composite_type_field (t, "v4_int32",
  72.                                    init_vector_type (bt->builtin_int32, 4));
  73.       append_composite_type_field (t, "v8_int16",
  74.                                    init_vector_type (bt->builtin_int16, 8));
  75.       append_composite_type_field (t, "v16_int8",
  76.                                    init_vector_type (bt->builtin_int8, 16));
  77.       append_composite_type_field (t, "v2_double",
  78.                                    init_vector_type (bt->builtin_double, 2));
  79.       append_composite_type_field (t, "v4_float",
  80.                                    init_vector_type (bt->builtin_float, 4));

  81.       TYPE_VECTOR (t) = 1;
  82.       TYPE_NAME (t) = "spu_builtin_type_vec128";

  83.       tdep->spu_builtin_type_vec128 = t;
  84.     }

  85.   return tdep->spu_builtin_type_vec128;
  86. }


  87. /* The list of available "info spu " commands.  */
  88. static struct cmd_list_element *infospucmdlist = NULL;

  89. /* Registers.  */

  90. static const char *
  91. spu_register_name (struct gdbarch *gdbarch, int reg_nr)
  92. {
  93.   static char *register_names[] =
  94.     {
  95.       "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  96.       "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  97.       "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  98.       "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  99.       "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
  100.       "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
  101.       "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
  102.       "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
  103.       "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
  104.       "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
  105.       "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
  106.       "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
  107.       "r96", "r97", "r98", "r99", "r100", "r101", "r102", "r103",
  108.       "r104", "r105", "r106", "r107", "r108", "r109", "r110", "r111",
  109.       "r112", "r113", "r114", "r115", "r116", "r117", "r118", "r119",
  110.       "r120", "r121", "r122", "r123", "r124", "r125", "r126", "r127",
  111.       "id", "pc", "sp", "fpscr", "srr0", "lslr", "decr", "decr_status"
  112.     };

  113.   if (reg_nr < 0)
  114.     return NULL;
  115.   if (reg_nr >= sizeof register_names / sizeof *register_names)
  116.     return NULL;

  117.   return register_names[reg_nr];
  118. }

  119. static struct type *
  120. spu_register_type (struct gdbarch *gdbarch, int reg_nr)
  121. {
  122.   if (reg_nr < SPU_NUM_GPRS)
  123.     return spu_builtin_type_vec128 (gdbarch);

  124.   switch (reg_nr)
  125.     {
  126.     case SPU_ID_REGNUM:
  127.       return builtin_type (gdbarch)->builtin_uint32;

  128.     case SPU_PC_REGNUM:
  129.       return builtin_type (gdbarch)->builtin_func_ptr;

  130.     case SPU_SP_REGNUM:
  131.       return builtin_type (gdbarch)->builtin_data_ptr;

  132.     case SPU_FPSCR_REGNUM:
  133.       return builtin_type (gdbarch)->builtin_uint128;

  134.     case SPU_SRR0_REGNUM:
  135.       return builtin_type (gdbarch)->builtin_uint32;

  136.     case SPU_LSLR_REGNUM:
  137.       return builtin_type (gdbarch)->builtin_uint32;

  138.     case SPU_DECR_REGNUM:
  139.       return builtin_type (gdbarch)->builtin_uint32;

  140.     case SPU_DECR_STATUS_REGNUM:
  141.       return builtin_type (gdbarch)->builtin_uint32;

  142.     default:
  143.       internal_error (__FILE__, __LINE__, _("invalid regnum"));
  144.     }
  145. }

  146. /* Pseudo registers for preferred slots - stack pointer.  */

  147. static enum register_status
  148. spu_pseudo_register_read_spu (struct regcache *regcache, const char *regname,
  149.                               gdb_byte *buf)
  150. {
  151.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  152.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  153.   enum register_status status;
  154.   gdb_byte reg[32];
  155.   char annex[32];
  156.   ULONGEST id;
  157.   ULONGEST ul;

  158.   status = regcache_raw_read_unsigned (regcache, SPU_ID_REGNUM, &id);
  159.   if (status != REG_VALID)
  160.     return status;
  161.   xsnprintf (annex, sizeof annex, "%d/%s", (int) id, regname);
  162.   memset (reg, 0, sizeof reg);
  163.   target_read (&current_target, TARGET_OBJECT_SPU, annex,
  164.                reg, 0, sizeof reg);

  165.   ul = strtoulst ((char *) reg, NULL, 16);
  166.   store_unsigned_integer (buf, 4, byte_order, ul);
  167.   return REG_VALID;
  168. }

  169. static enum register_status
  170. spu_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  171.                           int regnum, gdb_byte *buf)
  172. {
  173.   gdb_byte reg[16];
  174.   char annex[32];
  175.   ULONGEST id;
  176.   enum register_status status;

  177.   switch (regnum)
  178.     {
  179.     case SPU_SP_REGNUM:
  180.       status = regcache_raw_read (regcache, SPU_RAW_SP_REGNUM, reg);
  181.       if (status != REG_VALID)
  182.         return status;
  183.       memcpy (buf, reg, 4);
  184.       return status;

  185.     case SPU_FPSCR_REGNUM:
  186.       status = regcache_raw_read_unsigned (regcache, SPU_ID_REGNUM, &id);
  187.       if (status != REG_VALID)
  188.         return status;
  189.       xsnprintf (annex, sizeof annex, "%d/fpcr", (int) id);
  190.       target_read (&current_target, TARGET_OBJECT_SPU, annex, buf, 0, 16);
  191.       return status;

  192.     case SPU_SRR0_REGNUM:
  193.       return spu_pseudo_register_read_spu (regcache, "srr0", buf);

  194.     case SPU_LSLR_REGNUM:
  195.       return spu_pseudo_register_read_spu (regcache, "lslr", buf);

  196.     case SPU_DECR_REGNUM:
  197.       return spu_pseudo_register_read_spu (regcache, "decr", buf);

  198.     case SPU_DECR_STATUS_REGNUM:
  199.       return spu_pseudo_register_read_spu (regcache, "decr_status", buf);

  200.     default:
  201.       internal_error (__FILE__, __LINE__, _("invalid regnum"));
  202.     }
  203. }

  204. static void
  205. spu_pseudo_register_write_spu (struct regcache *regcache, const char *regname,
  206.                                const gdb_byte *buf)
  207. {
  208.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  209.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  210.   char reg[32];
  211.   char annex[32];
  212.   ULONGEST id;

  213.   regcache_raw_read_unsigned (regcache, SPU_ID_REGNUM, &id);
  214.   xsnprintf (annex, sizeof annex, "%d/%s", (int) id, regname);
  215.   xsnprintf (reg, sizeof reg, "0x%s",
  216.              phex_nz (extract_unsigned_integer (buf, 4, byte_order), 4));
  217.   target_write (&current_target, TARGET_OBJECT_SPU, annex,
  218.                 (gdb_byte *) reg, 0, strlen (reg));
  219. }

  220. static void
  221. spu_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  222.                            int regnum, const gdb_byte *buf)
  223. {
  224.   gdb_byte reg[16];
  225.   char annex[32];
  226.   ULONGEST id;

  227.   switch (regnum)
  228.     {
  229.     case SPU_SP_REGNUM:
  230.       regcache_raw_read (regcache, SPU_RAW_SP_REGNUM, reg);
  231.       memcpy (reg, buf, 4);
  232.       regcache_raw_write (regcache, SPU_RAW_SP_REGNUM, reg);
  233.       break;

  234.     case SPU_FPSCR_REGNUM:
  235.       regcache_raw_read_unsigned (regcache, SPU_ID_REGNUM, &id);
  236.       xsnprintf (annex, sizeof annex, "%d/fpcr", (int) id);
  237.       target_write (&current_target, TARGET_OBJECT_SPU, annex, buf, 0, 16);
  238.       break;

  239.     case SPU_SRR0_REGNUM:
  240.       spu_pseudo_register_write_spu (regcache, "srr0", buf);
  241.       break;

  242.     case SPU_LSLR_REGNUM:
  243.       spu_pseudo_register_write_spu (regcache, "lslr", buf);
  244.       break;

  245.     case SPU_DECR_REGNUM:
  246.       spu_pseudo_register_write_spu (regcache, "decr", buf);
  247.       break;

  248.     case SPU_DECR_STATUS_REGNUM:
  249.       spu_pseudo_register_write_spu (regcache, "decr_status", buf);
  250.       break;

  251.     default:
  252.       internal_error (__FILE__, __LINE__, _("invalid regnum"));
  253.     }
  254. }

  255. static int
  256. spu_ax_pseudo_register_collect (struct gdbarch *gdbarch,
  257.                                 struct agent_expr *ax, int regnum)
  258. {
  259.   switch (regnum)
  260.     {
  261.     case SPU_SP_REGNUM:
  262.       ax_reg_mask (ax, SPU_RAW_SP_REGNUM);
  263.       return 0;

  264.     case SPU_FPSCR_REGNUM:
  265.     case SPU_SRR0_REGNUM:
  266.     case SPU_LSLR_REGNUM:
  267.     case SPU_DECR_REGNUM:
  268.     case SPU_DECR_STATUS_REGNUM:
  269.       return -1;

  270.     default:
  271.       internal_error (__FILE__, __LINE__, _("invalid regnum"));
  272.     }
  273. }

  274. static int
  275. spu_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
  276.                                    struct agent_expr *ax, int regnum)
  277. {
  278.   switch (regnum)
  279.     {
  280.     case SPU_SP_REGNUM:
  281.       ax_reg (ax, SPU_RAW_SP_REGNUM);
  282.       return 0;

  283.     case SPU_FPSCR_REGNUM:
  284.     case SPU_SRR0_REGNUM:
  285.     case SPU_LSLR_REGNUM:
  286.     case SPU_DECR_REGNUM:
  287.     case SPU_DECR_STATUS_REGNUM:
  288.       return -1;

  289.     default:
  290.       internal_error (__FILE__, __LINE__, _("invalid regnum"));
  291.     }
  292. }


  293. /* Value conversion -- access scalar values at the preferred slot.  */

  294. static struct value *
  295. spu_value_from_register (struct gdbarch *gdbarch, struct type *type,
  296.                          int regnum, struct frame_id frame_id)
  297. {
  298.   struct value *value = default_value_from_register (gdbarch, type,
  299.                                                      regnum, frame_id);
  300.   int len = TYPE_LENGTH (type);

  301.   if (regnum < SPU_NUM_GPRS && len < 16)
  302.     {
  303.       int preferred_slot = len < 4 ? 4 - len : 0;
  304.       set_value_offset (value, preferred_slot);
  305.     }

  306.   return value;
  307. }

  308. /* Register groups.  */

  309. static int
  310. spu_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  311.                          struct reggroup *group)
  312. {
  313.   /* Registers displayed via 'info regs'.  */
  314.   if (group == general_reggroup)
  315.     return 1;

  316.   /* Registers displayed via 'info float'.  */
  317.   if (group == float_reggroup)
  318.     return 0;

  319.   /* Registers that need to be saved/restored in order to
  320.      push or pop frames.  */
  321.   if (group == save_reggroup || group == restore_reggroup)
  322.     return 1;

  323.   return default_register_reggroup_p (gdbarch, regnum, group);
  324. }

  325. /* DWARF-2 register numbers.  */

  326. static int
  327. spu_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  328. {
  329.   /* Use cooked instead of raw SP.  */
  330.   return (reg == SPU_RAW_SP_REGNUM)? SPU_SP_REGNUM : reg;
  331. }


  332. /* Address handling.  */

  333. static int
  334. spu_gdbarch_id (struct gdbarch *gdbarch)
  335. {
  336.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  337.   int id = tdep->id;

  338.   /* The objfile architecture of a standalone SPU executable does not
  339.      provide an SPU ID.  Retrieve it from the objfile's relocated
  340.      address range in this special case.  */
  341.   if (id == -1
  342.       && symfile_objfile && symfile_objfile->obfd
  343.       && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu
  344.       && symfile_objfile->sections != symfile_objfile->sections_end)
  345.     id = SPUADDR_SPU (obj_section_addr (symfile_objfile->sections));

  346.   return id;
  347. }

  348. static int
  349. spu_address_class_type_flags (int byte_size, int dwarf2_addr_class)
  350. {
  351.   if (dwarf2_addr_class == 1)
  352.     return TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
  353.   else
  354.     return 0;
  355. }

  356. static const char *
  357. spu_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
  358. {
  359.   if (type_flags & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1)
  360.     return "__ea";
  361.   else
  362.     return NULL;
  363. }

  364. static int
  365. spu_address_class_name_to_type_flags (struct gdbarch *gdbarch,
  366.                                       const char *name, int *type_flags_ptr)
  367. {
  368.   if (strcmp (name, "__ea") == 0)
  369.     {
  370.       *type_flags_ptr = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1;
  371.       return 1;
  372.     }
  373.   else
  374.    return 0;
  375. }

  376. static void
  377. spu_address_to_pointer (struct gdbarch *gdbarch,
  378.                         struct type *type, gdb_byte *buf, CORE_ADDR addr)
  379. {
  380.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  381.   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
  382.                           SPUADDR_ADDR (addr));
  383. }

  384. static CORE_ADDR
  385. spu_pointer_to_address (struct gdbarch *gdbarch,
  386.                         struct type *type, const gdb_byte *buf)
  387. {
  388.   int id = spu_gdbarch_id (gdbarch);
  389.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  390.   ULONGEST addr
  391.     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);

  392.   /* Do not convert __ea pointers.  */
  393.   if (TYPE_ADDRESS_CLASS_1 (type))
  394.     return addr;

  395.   return addr? SPUADDR (id, addr) : 0;
  396. }

  397. static CORE_ADDR
  398. spu_integer_to_address (struct gdbarch *gdbarch,
  399.                         struct type *type, const gdb_byte *buf)
  400. {
  401.   int id = spu_gdbarch_id (gdbarch);
  402.   ULONGEST addr = unpack_long (type, buf);

  403.   return SPUADDR (id, addr);
  404. }


  405. /* Decoding SPU instructions.  */

  406. enum
  407.   {
  408.     op_lqd   = 0x34,
  409.     op_lqx   = 0x3c4,
  410.     op_lqa   = 0x61,
  411.     op_lqr   = 0x67,
  412.     op_stqd  = 0x24,
  413.     op_stqx  = 0x144,
  414.     op_stqa  = 0x41,
  415.     op_stqr  = 0x47,

  416.     op_il    = 0x081,
  417.     op_ila   = 0x21,
  418.     op_a     = 0x0c0,
  419.     op_ai    = 0x1c,

  420.     op_selb  = 0x8,

  421.     op_br    = 0x64,
  422.     op_bra   = 0x60,
  423.     op_brsl  = 0x66,
  424.     op_brasl = 0x62,
  425.     op_brnz  = 0x42,
  426.     op_brz   = 0x40,
  427.     op_brhnz = 0x46,
  428.     op_brhz  = 0x44,
  429.     op_bi    = 0x1a8,
  430.     op_bisl  = 0x1a9,
  431.     op_biz   = 0x128,
  432.     op_binz  = 0x129,
  433.     op_bihz  = 0x12a,
  434.     op_bihnz = 0x12b,
  435.   };

  436. static int
  437. is_rr (unsigned int insn, int op, int *rt, int *ra, int *rb)
  438. {
  439.   if ((insn >> 21) == op)
  440.     {
  441.       *rt = insn & 127;
  442.       *ra = (insn >> 7) & 127;
  443.       *rb = (insn >> 14) & 127;
  444.       return 1;
  445.     }

  446.   return 0;
  447. }

  448. static int
  449. is_rrr (unsigned int insn, int op, int *rt, int *ra, int *rb, int *rc)
  450. {
  451.   if ((insn >> 28) == op)
  452.     {
  453.       *rt = (insn >> 21) & 127;
  454.       *ra = (insn >> 7) & 127;
  455.       *rb = (insn >> 14) & 127;
  456.       *rc = insn & 127;
  457.       return 1;
  458.     }

  459.   return 0;
  460. }

  461. static int
  462. is_ri7 (unsigned int insn, int op, int *rt, int *ra, int *i7)
  463. {
  464.   if ((insn >> 21) == op)
  465.     {
  466.       *rt = insn & 127;
  467.       *ra = (insn >> 7) & 127;
  468.       *i7 = (((insn >> 14) & 127) ^ 0x40) - 0x40;
  469.       return 1;
  470.     }

  471.   return 0;
  472. }

  473. static int
  474. is_ri10 (unsigned int insn, int op, int *rt, int *ra, int *i10)
  475. {
  476.   if ((insn >> 24) == op)
  477.     {
  478.       *rt = insn & 127;
  479.       *ra = (insn >> 7) & 127;
  480.       *i10 = (((insn >> 14) & 0x3ff) ^ 0x200) - 0x200;
  481.       return 1;
  482.     }

  483.   return 0;
  484. }

  485. static int
  486. is_ri16 (unsigned int insn, int op, int *rt, int *i16)
  487. {
  488.   if ((insn >> 23) == op)
  489.     {
  490.       *rt = insn & 127;
  491.       *i16 = (((insn >> 7) & 0xffff) ^ 0x8000) - 0x8000;
  492.       return 1;
  493.     }

  494.   return 0;
  495. }

  496. static int
  497. is_ri18 (unsigned int insn, int op, int *rt, int *i18)
  498. {
  499.   if ((insn >> 25) == op)
  500.     {
  501.       *rt = insn & 127;
  502.       *i18 = (((insn >> 7) & 0x3ffff) ^ 0x20000) - 0x20000;
  503.       return 1;
  504.     }

  505.   return 0;
  506. }

  507. static int
  508. is_branch (unsigned int insn, int *offset, int *reg)
  509. {
  510.   int rt, i7, i16;

  511.   if (is_ri16 (insn, op_br, &rt, &i16)
  512.       || is_ri16 (insn, op_brsl, &rt, &i16)
  513.       || is_ri16 (insn, op_brnz, &rt, &i16)
  514.       || is_ri16 (insn, op_brz, &rt, &i16)
  515.       || is_ri16 (insn, op_brhnz, &rt, &i16)
  516.       || is_ri16 (insn, op_brhz, &rt, &i16))
  517.     {
  518.       *reg = SPU_PC_REGNUM;
  519.       *offset = i16 << 2;
  520.       return 1;
  521.     }

  522.   if (is_ri16 (insn, op_bra, &rt, &i16)
  523.       || is_ri16 (insn, op_brasl, &rt, &i16))
  524.     {
  525.       *reg = -1;
  526.       *offset = i16 << 2;
  527.       return 1;
  528.     }

  529.   if (is_ri7 (insn, op_bi, &rt, reg, &i7)
  530.       || is_ri7 (insn, op_bisl, &rt, reg, &i7)
  531.       || is_ri7 (insn, op_biz, &rt, reg, &i7)
  532.       || is_ri7 (insn, op_binz, &rt, reg, &i7)
  533.       || is_ri7 (insn, op_bihz, &rt, reg, &i7)
  534.       || is_ri7 (insn, op_bihnz, &rt, reg, &i7))
  535.     {
  536.       *offset = 0;
  537.       return 1;
  538.     }

  539.   return 0;
  540. }


  541. /* Prolog parsing.  */

  542. struct spu_prologue_data
  543.   {
  544.     /* Stack frame size.  -1 if analysis was unsuccessful.  */
  545.     int size;

  546.     /* How to find the CFA.  The CFA is equal to SP at function entry.  */
  547.     int cfa_reg;
  548.     int cfa_offset;

  549.     /* Offset relative to CFA where a register is saved.  -1 if invalid.  */
  550.     int reg_offset[SPU_NUM_GPRS];
  551.   };

  552. static CORE_ADDR
  553. spu_analyze_prologue (struct gdbarch *gdbarch,
  554.                       CORE_ADDR start_pc, CORE_ADDR end_pc,
  555.                       struct spu_prologue_data *data)
  556. {
  557.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  558.   int found_sp = 0;
  559.   int found_fp = 0;
  560.   int found_lr = 0;
  561.   int found_bc = 0;
  562.   int reg_immed[SPU_NUM_GPRS];
  563.   gdb_byte buf[16];
  564.   CORE_ADDR prolog_pc = start_pc;
  565.   CORE_ADDR pc;
  566.   int i;


  567.   /* Initialize DATA to default values.  */
  568.   data->size = -1;

  569.   data->cfa_reg = SPU_RAW_SP_REGNUM;
  570.   data->cfa_offset = 0;

  571.   for (i = 0; i < SPU_NUM_GPRS; i++)
  572.     data->reg_offset[i] = -1;

  573.   /* Set up REG_IMMED array.  This is non-zero for a register if we know its
  574.      preferred slot currently holds this immediate value.  */
  575.   for (i = 0; i < SPU_NUM_GPRS; i++)
  576.       reg_immed[i] = 0;

  577.   /* Scan instructions until the first branch.

  578.      The following instructions are important prolog components:

  579.         - The first instruction to set up the stack pointer.
  580.         - The first instruction to set up the frame pointer.
  581.         - The first instruction to save the link register.
  582.         - The first instruction to save the backchain.

  583.      We return the instruction after the latest of these four,
  584.      or the incoming PC if none is found.  The first instruction
  585.      to set up the stack pointer also defines the frame size.

  586.      Note that instructions saving incoming arguments to their stack
  587.      slots are not counted as important, because they are hard to
  588.      identify with certainty.  This should not matter much, because
  589.      arguments are relevant only in code compiled with debug data,
  590.      and in such code the GDB core will advance until the first source
  591.      line anyway, using SAL data.

  592.      For purposes of stack unwinding, we analyze the following types
  593.      of instructions in addition:

  594.       - Any instruction adding to the current frame pointer.
  595.       - Any instruction loading an immediate constant into a register.
  596.       - Any instruction storing a register onto the stack.

  597.      These are used to compute the CFA and REG_OFFSET output.  */

  598.   for (pc = start_pc; pc < end_pc; pc += 4)
  599.     {
  600.       unsigned int insn;
  601.       int rt, ra, rb, rc, immed;

  602.       if (target_read_memory (pc, buf, 4))
  603.         break;
  604.       insn = extract_unsigned_integer (buf, 4, byte_order);

  605.       /* AI is the typical instruction to set up a stack frame.
  606.          It is also used to initialize the frame pointer.  */
  607.       if (is_ri10 (insn, op_ai, &rt, &ra, &immed))
  608.         {
  609.           if (rt == data->cfa_reg && ra == data->cfa_reg)
  610.             data->cfa_offset -= immed;

  611.           if (rt == SPU_RAW_SP_REGNUM && ra == SPU_RAW_SP_REGNUM
  612.               && !found_sp)
  613.             {
  614.               found_sp = 1;
  615.               prolog_pc = pc + 4;

  616.               data->size = -immed;
  617.             }
  618.           else if (rt == SPU_FP_REGNUM && ra == SPU_RAW_SP_REGNUM
  619.                    && !found_fp)
  620.             {
  621.               found_fp = 1;
  622.               prolog_pc = pc + 4;

  623.               data->cfa_reg = SPU_FP_REGNUM;
  624.               data->cfa_offset -= immed;
  625.             }
  626.         }

  627.       /* A is used to set up stack frames of size >= 512 bytes.
  628.          If we have tracked the contents of the addend register,
  629.          we can handle this as well.  */
  630.       else if (is_rr (insn, op_a, &rt, &ra, &rb))
  631.         {
  632.           if (rt == data->cfa_reg && ra == data->cfa_reg)
  633.             {
  634.               if (reg_immed[rb] != 0)
  635.                 data->cfa_offset -= reg_immed[rb];
  636.               else
  637.                 data->cfa_reg = -1/* We don't know the CFA any more.  */
  638.             }

  639.           if (rt == SPU_RAW_SP_REGNUM && ra == SPU_RAW_SP_REGNUM
  640.               && !found_sp)
  641.             {
  642.               found_sp = 1;
  643.               prolog_pc = pc + 4;

  644.               if (reg_immed[rb] != 0)
  645.                 data->size = -reg_immed[rb];
  646.             }
  647.         }

  648.       /* We need to track IL and ILA used to load immediate constants
  649.          in case they are later used as input to an A instruction.  */
  650.       else if (is_ri16 (insn, op_il, &rt, &immed))
  651.         {
  652.           reg_immed[rt] = immed;

  653.           if (rt == SPU_RAW_SP_REGNUM && !found_sp)
  654.             found_sp = 1;
  655.         }

  656.       else if (is_ri18 (insn, op_ila, &rt, &immed))
  657.         {
  658.           reg_immed[rt] = immed & 0x3ffff;

  659.           if (rt == SPU_RAW_SP_REGNUM && !found_sp)
  660.             found_sp = 1;
  661.         }

  662.       /* STQD is used to save registers to the stack.  */
  663.       else if (is_ri10 (insn, op_stqd, &rt, &ra, &immed))
  664.         {
  665.           if (ra == data->cfa_reg)
  666.             data->reg_offset[rt] = data->cfa_offset - (immed << 4);

  667.           if (ra == data->cfa_reg && rt == SPU_LR_REGNUM
  668.               && !found_lr)
  669.             {
  670.               found_lr = 1;
  671.               prolog_pc = pc + 4;
  672.             }

  673.           if (ra == SPU_RAW_SP_REGNUM
  674.               && (found_sp? immed == 0 : rt == SPU_RAW_SP_REGNUM)
  675.               && !found_bc)
  676.             {
  677.               found_bc = 1;
  678.               prolog_pc = pc + 4;
  679.             }
  680.         }

  681.       /* _start uses SELB to set up the stack pointer.  */
  682.       else if (is_rrr (insn, op_selb, &rt, &ra, &rb, &rc))
  683.         {
  684.           if (rt == SPU_RAW_SP_REGNUM && !found_sp)
  685.             found_sp = 1;
  686.         }

  687.       /* We terminate if we find a branch.  */
  688.       else if (is_branch (insn, &immed, &ra))
  689.         break;
  690.     }


  691.   /* If we successfully parsed until here, and didn't find any instruction
  692.      modifying SP, we assume we have a frameless function.  */
  693.   if (!found_sp)
  694.     data->size = 0;

  695.   /* Return cooked instead of raw SP.  */
  696.   if (data->cfa_reg == SPU_RAW_SP_REGNUM)
  697.     data->cfa_reg = SPU_SP_REGNUM;

  698.   return prolog_pc;
  699. }

  700. /* Return the first instruction after the prologue starting at PC.  */
  701. static CORE_ADDR
  702. spu_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  703. {
  704.   struct spu_prologue_data data;
  705.   return spu_analyze_prologue (gdbarch, pc, (CORE_ADDR)-1, &data);
  706. }

  707. /* Return the frame pointer in use at address PC.  */
  708. static void
  709. spu_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
  710.                            int *reg, LONGEST *offset)
  711. {
  712.   struct spu_prologue_data data;
  713.   spu_analyze_prologue (gdbarch, pc, (CORE_ADDR)-1, &data);

  714.   if (data.size != -1 && data.cfa_reg != -1)
  715.     {
  716.       /* The 'frame pointer' address is CFA minus frame size.  */
  717.       *reg = data.cfa_reg;
  718.       *offset = data.cfa_offset - data.size;
  719.     }
  720.   else
  721.     {
  722.       /* ??? We don't really know ...  */
  723.       *reg = SPU_SP_REGNUM;
  724.       *offset = 0;
  725.     }
  726. }

  727. /* Return true if we are in the function's epilogue, i.e. after the
  728.    instruction that destroyed the function's stack frame.

  729.    1) scan forward from the point of execution:
  730.        a) If you find an instruction that modifies the stack pointer
  731.           or transfers control (except a return), execution is not in
  732.           an epilogue, return.
  733.        b) Stop scanning if you find a return instruction or reach the
  734.           end of the function or reach the hard limit for the size of
  735.           an epilogue.
  736.    2) scan backward from the point of execution:
  737.         a) If you find an instruction that modifies the stack pointer,
  738.             execution *is* in an epilogue, return.
  739.         b) Stop scanning if you reach an instruction that transfers
  740.            control or the beginning of the function or reach the hard
  741.            limit for the size of an epilogue.  */

  742. static int
  743. spu_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  744. {
  745.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  746.   CORE_ADDR scan_pc, func_start, func_end, epilogue_start, epilogue_end;
  747.   bfd_byte buf[4];
  748.   unsigned int insn;
  749.   int rt, ra, rb, immed;

  750.   /* Find the search limits based on function boundaries and hard limit.
  751.      We assume the epilogue can be up to 64 instructions long.  */

  752.   const int spu_max_epilogue_size = 64 * 4;

  753.   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
  754.     return 0;

  755.   if (pc - func_start < spu_max_epilogue_size)
  756.     epilogue_start = func_start;
  757.   else
  758.     epilogue_start = pc - spu_max_epilogue_size;

  759.   if (func_end - pc < spu_max_epilogue_size)
  760.     epilogue_end = func_end;
  761.   else
  762.     epilogue_end = pc + spu_max_epilogue_size;

  763.   /* Scan forward until next 'bi $0'.  */

  764.   for (scan_pc = pc; scan_pc < epilogue_end; scan_pc += 4)
  765.     {
  766.       if (target_read_memory (scan_pc, buf, 4))
  767.         return 0;
  768.       insn = extract_unsigned_integer (buf, 4, byte_order);

  769.       if (is_branch (insn, &immed, &ra))
  770.         {
  771.           if (immed == 0 && ra == SPU_LR_REGNUM)
  772.             break;

  773.           return 0;
  774.         }

  775.       if (is_ri10 (insn, op_ai, &rt, &ra, &immed)
  776.           || is_rr (insn, op_a, &rt, &ra, &rb)
  777.           || is_ri10 (insn, op_lqd, &rt, &ra, &immed))
  778.         {
  779.           if (rt == SPU_RAW_SP_REGNUM)
  780.             return 0;
  781.         }
  782.     }

  783.   if (scan_pc >= epilogue_end)
  784.     return 0;

  785.   /* Scan backward until adjustment to stack pointer (R1).  */

  786.   for (scan_pc = pc - 4; scan_pc >= epilogue_start; scan_pc -= 4)
  787.     {
  788.       if (target_read_memory (scan_pc, buf, 4))
  789.         return 0;
  790.       insn = extract_unsigned_integer (buf, 4, byte_order);

  791.       if (is_branch (insn, &immed, &ra))
  792.         return 0;

  793.       if (is_ri10 (insn, op_ai, &rt, &ra, &immed)
  794.           || is_rr (insn, op_a, &rt, &ra, &rb)
  795.           || is_ri10 (insn, op_lqd, &rt, &ra, &immed))
  796.         {
  797.           if (rt == SPU_RAW_SP_REGNUM)
  798.             return 1;
  799.         }
  800.     }

  801.   return 0;
  802. }


  803. /* Normal stack frames.  */

  804. struct spu_unwind_cache
  805. {
  806.   CORE_ADDR func;
  807.   CORE_ADDR frame_base;
  808.   CORE_ADDR local_base;

  809.   struct trad_frame_saved_reg *saved_regs;
  810. };

  811. static struct spu_unwind_cache *
  812. spu_frame_unwind_cache (struct frame_info *this_frame,
  813.                         void **this_prologue_cache)
  814. {
  815.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  816.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  817.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  818.   struct spu_unwind_cache *info;
  819.   struct spu_prologue_data data;
  820.   CORE_ADDR id = tdep->id;
  821.   gdb_byte buf[16];

  822.   if (*this_prologue_cache)
  823.     return *this_prologue_cache;

  824.   info = FRAME_OBSTACK_ZALLOC (struct spu_unwind_cache);
  825.   *this_prologue_cache = info;
  826.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  827.   info->frame_base = 0;
  828.   info->local_base = 0;

  829.   /* Find the start of the current function, and analyze its prologue.  */
  830.   info->func = get_frame_func (this_frame);
  831.   if (info->func == 0)
  832.     {
  833.       /* Fall back to using the current PC as frame ID.  */
  834.       info->func = get_frame_pc (this_frame);
  835.       data.size = -1;
  836.     }
  837.   else
  838.     spu_analyze_prologue (gdbarch, info->func, get_frame_pc (this_frame),
  839.                           &data);

  840.   /* If successful, use prologue analysis data.  */
  841.   if (data.size != -1 && data.cfa_reg != -1)
  842.     {
  843.       CORE_ADDR cfa;
  844.       int i;

  845.       /* Determine CFA via unwound CFA_REG plus CFA_OFFSET.  */
  846.       get_frame_register (this_frame, data.cfa_reg, buf);
  847.       cfa = extract_unsigned_integer (buf, 4, byte_order) + data.cfa_offset;
  848.       cfa = SPUADDR (id, cfa);

  849.       /* Call-saved register slots.  */
  850.       for (i = 0; i < SPU_NUM_GPRS; i++)
  851.         if (i == SPU_LR_REGNUM
  852.             || (i >= SPU_SAVED1_REGNUM && i <= SPU_SAVEDN_REGNUM))
  853.           if (data.reg_offset[i] != -1)
  854.             info->saved_regs[i].addr = cfa - data.reg_offset[i];

  855.       /* Frame bases.  */
  856.       info->frame_base = cfa;
  857.       info->local_base = cfa - data.size;
  858.     }

  859.   /* Otherwise, fall back to reading the backchain link.  */
  860.   else
  861.     {
  862.       CORE_ADDR reg;
  863.       LONGEST backchain;
  864.       ULONGEST lslr;
  865.       int status;

  866.       /* Get local store limit.  */
  867.       lslr = get_frame_register_unsigned (this_frame, SPU_LSLR_REGNUM);
  868.       if (!lslr)
  869.         lslr = (ULONGEST) -1;

  870.       /* Get the backchain.  */
  871.       reg = get_frame_register_unsigned (this_frame, SPU_SP_REGNUM);
  872.       status = safe_read_memory_integer (SPUADDR (id, reg), 4, byte_order,
  873.                                          &backchain);

  874.       /* A zero backchain terminates the frame chain.  Also, sanity
  875.          check against the local store size limit.  */
  876.       if (status && backchain > 0 && backchain <= lslr)
  877.         {
  878.           /* Assume the link register is saved into its slot.  */
  879.           if (backchain + 16 <= lslr)
  880.             info->saved_regs[SPU_LR_REGNUM].addr = SPUADDR (id,
  881.                                                             backchain + 16);

  882.           /* Frame bases.  */
  883.           info->frame_base = SPUADDR (id, backchain);
  884.           info->local_base = SPUADDR (id, reg);
  885.         }
  886.     }

  887.   /* If we didn't find a frame, we cannot determine SP / return address.  */
  888.   if (info->frame_base == 0)
  889.     return info;

  890.   /* The previous SP is equal to the CFA.  */
  891.   trad_frame_set_value (info->saved_regs, SPU_SP_REGNUM,
  892.                         SPUADDR_ADDR (info->frame_base));

  893.   /* Read full contents of the unwound link register in order to
  894.      be able to determine the return address.  */
  895.   if (trad_frame_addr_p (info->saved_regs, SPU_LR_REGNUM))
  896.     target_read_memory (info->saved_regs[SPU_LR_REGNUM].addr, buf, 16);
  897.   else
  898.     get_frame_register (this_frame, SPU_LR_REGNUM, buf);

  899.   /* Normally, the return address is contained in the slot 0 of the
  900.      link register, and slots 1-3 are zero.  For an overlay return,
  901.      slot 0 contains the address of the overlay manager return stub,
  902.      slot 1 contains the partition number of the overlay section to
  903.      be returned to, and slot 2 contains the return address within
  904.      that section.  Return the latter address in that case.  */
  905.   if (extract_unsigned_integer (buf + 8, 4, byte_order) != 0)
  906.     trad_frame_set_value (info->saved_regs, SPU_PC_REGNUM,
  907.                           extract_unsigned_integer (buf + 8, 4, byte_order));
  908.   else
  909.     trad_frame_set_value (info->saved_regs, SPU_PC_REGNUM,
  910.                           extract_unsigned_integer (buf, 4, byte_order));

  911.   return info;
  912. }

  913. static void
  914. spu_frame_this_id (struct frame_info *this_frame,
  915.                    void **this_prologue_cache, struct frame_id *this_id)
  916. {
  917.   struct spu_unwind_cache *info =
  918.     spu_frame_unwind_cache (this_frame, this_prologue_cache);

  919.   if (info->frame_base == 0)
  920.     return;

  921.   *this_id = frame_id_build (info->frame_base, info->func);
  922. }

  923. static struct value *
  924. spu_frame_prev_register (struct frame_info *this_frame,
  925.                          void **this_prologue_cache, int regnum)
  926. {
  927.   struct spu_unwind_cache *info
  928.     = spu_frame_unwind_cache (this_frame, this_prologue_cache);

  929.   /* Special-case the stack pointer.  */
  930.   if (regnum == SPU_RAW_SP_REGNUM)
  931.     regnum = SPU_SP_REGNUM;

  932.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  933. }

  934. static const struct frame_unwind spu_frame_unwind = {
  935.   NORMAL_FRAME,
  936.   default_frame_unwind_stop_reason,
  937.   spu_frame_this_id,
  938.   spu_frame_prev_register,
  939.   NULL,
  940.   default_frame_sniffer
  941. };

  942. static CORE_ADDR
  943. spu_frame_base_address (struct frame_info *this_frame, void **this_cache)
  944. {
  945.   struct spu_unwind_cache *info
  946.     = spu_frame_unwind_cache (this_frame, this_cache);
  947.   return info->local_base;
  948. }

  949. static const struct frame_base spu_frame_base = {
  950.   &spu_frame_unwind,
  951.   spu_frame_base_address,
  952.   spu_frame_base_address,
  953.   spu_frame_base_address
  954. };

  955. static CORE_ADDR
  956. spu_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  957. {
  958.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  959.   CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, SPU_PC_REGNUM);
  960.   /* Mask off interrupt enable bit.  */
  961.   return SPUADDR (tdep->id, pc & -4);
  962. }

  963. static CORE_ADDR
  964. spu_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  965. {
  966.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  967.   CORE_ADDR sp = frame_unwind_register_unsigned (next_frame, SPU_SP_REGNUM);
  968.   return SPUADDR (tdep->id, sp);
  969. }

  970. static CORE_ADDR
  971. spu_read_pc (struct regcache *regcache)
  972. {
  973.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
  974.   ULONGEST pc;
  975.   regcache_cooked_read_unsigned (regcache, SPU_PC_REGNUM, &pc);
  976.   /* Mask off interrupt enable bit.  */
  977.   return SPUADDR (tdep->id, pc & -4);
  978. }

  979. static void
  980. spu_write_pc (struct regcache *regcache, CORE_ADDR pc)
  981. {
  982.   /* Keep interrupt enabled state unchanged.  */
  983.   ULONGEST old_pc;

  984.   regcache_cooked_read_unsigned (regcache, SPU_PC_REGNUM, &old_pc);
  985.   regcache_cooked_write_unsigned (regcache, SPU_PC_REGNUM,
  986.                                   (SPUADDR_ADDR (pc) & -4) | (old_pc & 3));
  987. }


  988. /* Cell/B.E. cross-architecture unwinder support.  */

  989. struct spu2ppu_cache
  990. {
  991.   struct frame_id frame_id;
  992.   struct regcache *regcache;
  993. };

  994. static struct gdbarch *
  995. spu2ppu_prev_arch (struct frame_info *this_frame, void **this_cache)
  996. {
  997.   struct spu2ppu_cache *cache = *this_cache;
  998.   return get_regcache_arch (cache->regcache);
  999. }

  1000. static void
  1001. spu2ppu_this_id (struct frame_info *this_frame,
  1002.                  void **this_cache, struct frame_id *this_id)
  1003. {
  1004.   struct spu2ppu_cache *cache = *this_cache;
  1005.   *this_id = cache->frame_id;
  1006. }

  1007. static struct value *
  1008. spu2ppu_prev_register (struct frame_info *this_frame,
  1009.                        void **this_cache, int regnum)
  1010. {
  1011.   struct spu2ppu_cache *cache = *this_cache;
  1012.   struct gdbarch *gdbarch = get_regcache_arch (cache->regcache);
  1013.   gdb_byte *buf;

  1014.   buf = alloca (register_size (gdbarch, regnum));
  1015.   regcache_cooked_read (cache->regcache, regnum, buf);
  1016.   return frame_unwind_got_bytes (this_frame, regnum, buf);
  1017. }

  1018. static int
  1019. spu2ppu_sniffer (const struct frame_unwind *self,
  1020.                  struct frame_info *this_frame, void **this_prologue_cache)
  1021. {
  1022.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1023.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1024.   CORE_ADDR base, func, backchain;
  1025.   gdb_byte buf[4];

  1026.   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_spu)
  1027.     return 0;

  1028.   base = get_frame_sp (this_frame);
  1029.   func = get_frame_pc (this_frame);
  1030.   if (target_read_memory (base, buf, 4))
  1031.     return 0;
  1032.   backchain = extract_unsigned_integer (buf, 4, byte_order);

  1033.   if (!backchain)
  1034.     {
  1035.       struct frame_info *fi;

  1036.       struct spu2ppu_cache *cache
  1037.         = FRAME_OBSTACK_CALLOC (1, struct spu2ppu_cache);

  1038.       cache->frame_id = frame_id_build (base + 16, func);

  1039.       for (fi = get_next_frame (this_frame); fi; fi = get_next_frame (fi))
  1040.         if (gdbarch_bfd_arch_info (get_frame_arch (fi))->arch != bfd_arch_spu)
  1041.           break;

  1042.       if (fi)
  1043.         {
  1044.           cache->regcache = frame_save_as_regcache (fi);
  1045.           *this_prologue_cache = cache;
  1046.           return 1;
  1047.         }
  1048.       else
  1049.         {
  1050.           struct regcache *regcache;
  1051.           regcache = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
  1052.           cache->regcache = regcache_dup (regcache);
  1053.           *this_prologue_cache = cache;
  1054.           return 1;
  1055.         }
  1056.     }

  1057.   return 0;
  1058. }

  1059. static void
  1060. spu2ppu_dealloc_cache (struct frame_info *self, void *this_cache)
  1061. {
  1062.   struct spu2ppu_cache *cache = this_cache;
  1063.   regcache_xfree (cache->regcache);
  1064. }

  1065. static const struct frame_unwind spu2ppu_unwind = {
  1066.   ARCH_FRAME,
  1067.   default_frame_unwind_stop_reason,
  1068.   spu2ppu_this_id,
  1069.   spu2ppu_prev_register,
  1070.   NULL,
  1071.   spu2ppu_sniffer,
  1072.   spu2ppu_dealloc_cache,
  1073.   spu2ppu_prev_arch,
  1074. };


  1075. /* Function calling convention.  */

  1076. static CORE_ADDR
  1077. spu_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  1078. {
  1079.   return sp & ~15;
  1080. }

  1081. static CORE_ADDR
  1082. spu_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
  1083.                      struct value **args, int nargs, struct type *value_type,
  1084.                      CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  1085.                      struct regcache *regcache)
  1086. {
  1087.   /* Allocate space sufficient for a breakpoint, keeping the stack aligned.  */
  1088.   sp = (sp - 4) & ~15;
  1089.   /* Store the address of that breakpoint */
  1090.   *bp_addr = sp;
  1091.   /* The call starts at the callee's entry point.  */
  1092.   *real_pc = funaddr;

  1093.   return sp;
  1094. }

  1095. static int
  1096. spu_scalar_value_p (struct type *type)
  1097. {
  1098.   switch (TYPE_CODE (type))
  1099.     {
  1100.     case TYPE_CODE_INT:
  1101.     case TYPE_CODE_ENUM:
  1102.     case TYPE_CODE_RANGE:
  1103.     case TYPE_CODE_CHAR:
  1104.     case TYPE_CODE_BOOL:
  1105.     case TYPE_CODE_PTR:
  1106.     case TYPE_CODE_REF:
  1107.       return TYPE_LENGTH (type) <= 16;

  1108.     default:
  1109.       return 0;
  1110.     }
  1111. }

  1112. static void
  1113. spu_value_to_regcache (struct regcache *regcache, int regnum,
  1114.                        struct type *type, const gdb_byte *in)
  1115. {
  1116.   int len = TYPE_LENGTH (type);

  1117.   if (spu_scalar_value_p (type))
  1118.     {
  1119.       int preferred_slot = len < 4 ? 4 - len : 0;
  1120.       regcache_cooked_write_part (regcache, regnum, preferred_slot, len, in);
  1121.     }
  1122.   else
  1123.     {
  1124.       while (len >= 16)
  1125.         {
  1126.           regcache_cooked_write (regcache, regnum++, in);
  1127.           in += 16;
  1128.           len -= 16;
  1129.         }

  1130.       if (len > 0)
  1131.         regcache_cooked_write_part (regcache, regnum, 0, len, in);
  1132.     }
  1133. }

  1134. static void
  1135. spu_regcache_to_value (struct regcache *regcache, int regnum,
  1136.                        struct type *type, gdb_byte *out)
  1137. {
  1138.   int len = TYPE_LENGTH (type);

  1139.   if (spu_scalar_value_p (type))
  1140.     {
  1141.       int preferred_slot = len < 4 ? 4 - len : 0;
  1142.       regcache_cooked_read_part (regcache, regnum, preferred_slot, len, out);
  1143.     }
  1144.   else
  1145.     {
  1146.       while (len >= 16)
  1147.         {
  1148.           regcache_cooked_read (regcache, regnum++, out);
  1149.           out += 16;
  1150.           len -= 16;
  1151.         }

  1152.       if (len > 0)
  1153.         regcache_cooked_read_part (regcache, regnum, 0, len, out);
  1154.     }
  1155. }

  1156. static CORE_ADDR
  1157. spu_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1158.                      struct regcache *regcache, CORE_ADDR bp_addr,
  1159.                      int nargs, struct value **args, CORE_ADDR sp,
  1160.                      int struct_return, CORE_ADDR struct_addr)
  1161. {
  1162.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1163.   CORE_ADDR sp_delta;
  1164.   int i;
  1165.   int regnum = SPU_ARG1_REGNUM;
  1166.   int stack_arg = -1;
  1167.   gdb_byte buf[16];

  1168.   /* Set the return address.  */
  1169.   memset (buf, 0, sizeof buf);
  1170.   store_unsigned_integer (buf, 4, byte_order, SPUADDR_ADDR (bp_addr));
  1171.   regcache_cooked_write (regcache, SPU_LR_REGNUM, buf);

  1172.   /* If STRUCT_RETURN is true, then the struct return address (in
  1173.      STRUCT_ADDR) will consume the first argument-passing register.
  1174.      Both adjust the register count and store that value.  */
  1175.   if (struct_return)
  1176.     {
  1177.       memset (buf, 0, sizeof buf);
  1178.       store_unsigned_integer (buf, 4, byte_order, SPUADDR_ADDR (struct_addr));
  1179.       regcache_cooked_write (regcache, regnum++, buf);
  1180.     }

  1181.   /* Fill in argument registers.  */
  1182.   for (i = 0; i < nargs; i++)
  1183.     {
  1184.       struct value *arg = args[i];
  1185.       struct type *type = check_typedef (value_type (arg));
  1186.       const gdb_byte *contents = value_contents (arg);
  1187.       int n_regs = align_up (TYPE_LENGTH (type), 16) / 16;

  1188.       /* If the argument doesn't wholly fit into registers, it and
  1189.          all subsequent arguments go to the stack.  */
  1190.       if (regnum + n_regs - 1 > SPU_ARGN_REGNUM)
  1191.         {
  1192.           stack_arg = i;
  1193.           break;
  1194.         }

  1195.       spu_value_to_regcache (regcache, regnum, type, contents);
  1196.       regnum += n_regs;
  1197.     }

  1198.   /* Overflow arguments go to the stack.  */
  1199.   if (stack_arg != -1)
  1200.     {
  1201.       CORE_ADDR ap;

  1202.       /* Allocate all required stack size.  */
  1203.       for (i = stack_arg; i < nargs; i++)
  1204.         {
  1205.           struct type *type = check_typedef (value_type (args[i]));
  1206.           sp -= align_up (TYPE_LENGTH (type), 16);
  1207.         }

  1208.       /* Fill in stack arguments.  */
  1209.       ap = sp;
  1210.       for (i = stack_arg; i < nargs; i++)
  1211.         {
  1212.           struct value *arg = args[i];
  1213.           struct type *type = check_typedef (value_type (arg));
  1214.           int len = TYPE_LENGTH (type);
  1215.           int preferred_slot;

  1216.           if (spu_scalar_value_p (type))
  1217.             preferred_slot = len < 4 ? 4 - len : 0;
  1218.           else
  1219.             preferred_slot = 0;

  1220.           target_write_memory (ap + preferred_slot, value_contents (arg), len);
  1221.           ap += align_up (TYPE_LENGTH (type), 16);
  1222.         }
  1223.     }

  1224.   /* Allocate stack frame header.  */
  1225.   sp -= 32;

  1226.   /* Store stack back chain.  */
  1227.   regcache_cooked_read (regcache, SPU_RAW_SP_REGNUM, buf);
  1228.   target_write_memory (sp, buf, 16);

  1229.   /* Finally, update all slots of the SP register.  */
  1230.   sp_delta = sp - extract_unsigned_integer (buf, 4, byte_order);
  1231.   for (i = 0; i < 4; i++)
  1232.     {
  1233.       CORE_ADDR sp_slot = extract_unsigned_integer (buf + 4*i, 4, byte_order);
  1234.       store_unsigned_integer (buf + 4*i, 4, byte_order, sp_slot + sp_delta);
  1235.     }
  1236.   regcache_cooked_write (regcache, SPU_RAW_SP_REGNUM, buf);

  1237.   return sp;
  1238. }

  1239. static struct frame_id
  1240. spu_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1241. {
  1242.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1243.   CORE_ADDR pc = get_frame_register_unsigned (this_frame, SPU_PC_REGNUM);
  1244.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, SPU_SP_REGNUM);
  1245.   return frame_id_build (SPUADDR (tdep->id, sp), SPUADDR (tdep->id, pc & -4));
  1246. }

  1247. /* Function return value access.  */

  1248. static enum return_value_convention
  1249. spu_return_value (struct gdbarch *gdbarch, struct value *function,
  1250.                   struct type *type, struct regcache *regcache,
  1251.                   gdb_byte *out, const gdb_byte *in)
  1252. {
  1253.   struct type *func_type = function ? value_type (function) : NULL;
  1254.   enum return_value_convention rvc;
  1255.   int opencl_vector = 0;

  1256.   if (func_type)
  1257.     {
  1258.       func_type = check_typedef (func_type);

  1259.       if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
  1260.         func_type = check_typedef (TYPE_TARGET_TYPE (func_type));

  1261.       if (TYPE_CODE (func_type) == TYPE_CODE_FUNC
  1262.           && TYPE_CALLING_CONVENTION (func_type) == DW_CC_GDB_IBM_OpenCL
  1263.           && TYPE_CODE (type) == TYPE_CODE_ARRAY
  1264.           && TYPE_VECTOR (type))
  1265.         opencl_vector = 1;
  1266.     }

  1267.   if (TYPE_LENGTH (type) <= (SPU_ARGN_REGNUM - SPU_ARG1_REGNUM + 1) * 16)
  1268.     rvc = RETURN_VALUE_REGISTER_CONVENTION;
  1269.   else
  1270.     rvc = RETURN_VALUE_STRUCT_CONVENTION;

  1271.   if (in)
  1272.     {
  1273.       switch (rvc)
  1274.         {
  1275.         case RETURN_VALUE_REGISTER_CONVENTION:
  1276.           if (opencl_vector && TYPE_LENGTH (type) == 2)
  1277.             regcache_cooked_write_part (regcache, SPU_ARG1_REGNUM, 2, 2, in);
  1278.           else
  1279.             spu_value_to_regcache (regcache, SPU_ARG1_REGNUM, type, in);
  1280.           break;

  1281.         case RETURN_VALUE_STRUCT_CONVENTION:
  1282.           error (_("Cannot set function return value."));
  1283.           break;
  1284.         }
  1285.     }
  1286.   else if (out)
  1287.     {
  1288.       switch (rvc)
  1289.         {
  1290.         case RETURN_VALUE_REGISTER_CONVENTION:
  1291.           if (opencl_vector && TYPE_LENGTH (type) == 2)
  1292.             regcache_cooked_read_part (regcache, SPU_ARG1_REGNUM, 2, 2, out);
  1293.           else
  1294.             spu_regcache_to_value (regcache, SPU_ARG1_REGNUM, type, out);
  1295.           break;

  1296.         case RETURN_VALUE_STRUCT_CONVENTION:
  1297.           error (_("Function return value unknown."));
  1298.           break;
  1299.         }
  1300.     }

  1301.   return rvc;
  1302. }


  1303. /* Breakpoints.  */

  1304. static const gdb_byte *
  1305. spu_breakpoint_from_pc (struct gdbarch *gdbarch,
  1306.                         CORE_ADDR * pcptr, int *lenptr)
  1307. {
  1308.   static const gdb_byte breakpoint[] = { 0x00, 0x00, 0x3f, 0xff };

  1309.   *lenptr = sizeof breakpoint;
  1310.   return breakpoint;
  1311. }

  1312. static int
  1313. spu_memory_remove_breakpoint (struct gdbarch *gdbarch,
  1314.                               struct bp_target_info *bp_tgt)
  1315. {
  1316.   /* We work around a problem in combined Cell/B.E. debugging here.  Consider
  1317.      that in a combined application, we have some breakpoints inserted in SPU
  1318.      code, and now the application forks (on the PPU side).  GDB common code
  1319.      will assume that the fork system call copied all breakpoints into the new
  1320.      process' address space, and that all those copies now need to be removed
  1321.      (see breakpoint.c:detach_breakpoints).

  1322.      While this is certainly true for PPU side breakpoints, it is not true
  1323.      for SPU side breakpoints.  fork will clone the SPU context file
  1324.      descriptors, so that all the existing SPU contexts are in accessible
  1325.      in the new process.  However, the contents of the SPU contexts themselves
  1326.      are *not* cloned.  Therefore the effect of detach_breakpoints is to
  1327.      remove SPU breakpoints from the *original* SPU context's local store
  1328.      -- this is not the correct behaviour.

  1329.      The workaround is to check whether the PID we are asked to remove this
  1330.      breakpoint from (i.e. ptid_get_pid (inferior_ptid)) is different from the
  1331.      PID of the current inferior (i.e. current_inferior ()->pid).  This is only
  1332.      true in the context of detach_breakpoints.  If so, we simply do nothing.
  1333.      [ Note that for the fork child process, it does not matter if breakpoints
  1334.      remain inserted, because those SPU contexts are not runnable anyway --
  1335.      the Linux kernel allows only the original process to invoke spu_run.  */

  1336.   if (ptid_get_pid (inferior_ptid) != current_inferior ()->pid)
  1337.     return 0;

  1338.   return default_memory_remove_breakpoint (gdbarch, bp_tgt);
  1339. }


  1340. /* Software single-stepping support.  */

  1341. static int
  1342. spu_software_single_step (struct frame_info *frame)
  1343. {
  1344.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1345.   struct address_space *aspace = get_frame_address_space (frame);
  1346.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1347.   CORE_ADDR pc, next_pc;
  1348.   unsigned int insn;
  1349.   int offset, reg;
  1350.   gdb_byte buf[4];
  1351.   ULONGEST lslr;

  1352.   pc = get_frame_pc (frame);

  1353.   if (target_read_memory (pc, buf, 4))
  1354.     return 1;
  1355.   insn = extract_unsigned_integer (buf, 4, byte_order);

  1356.   /* Get local store limit.  */
  1357.   lslr = get_frame_register_unsigned (frame, SPU_LSLR_REGNUM);
  1358.   if (!lslr)
  1359.     lslr = (ULONGEST) -1;

  1360.   /* Next sequential instruction is at PC + 4, except if the current
  1361.      instruction is a PPE-assisted call, in which case it is at PC + 8.
  1362.      Wrap around LS limit to be on the safe side.  */
  1363.   if ((insn & 0xffffff00) == 0x00002100)
  1364.     next_pc = (SPUADDR_ADDR (pc) + 8) & lslr;
  1365.   else
  1366.     next_pc = (SPUADDR_ADDR (pc) + 4) & lslr;

  1367.   insert_single_step_breakpoint (gdbarch,
  1368.                                  aspace, SPUADDR (SPUADDR_SPU (pc), next_pc));

  1369.   if (is_branch (insn, &offset, &reg))
  1370.     {
  1371.       CORE_ADDR target = offset;

  1372.       if (reg == SPU_PC_REGNUM)
  1373.         target += SPUADDR_ADDR (pc);
  1374.       else if (reg != -1)
  1375.         {
  1376.           int optim, unavail;

  1377.           if (get_frame_register_bytes (frame, reg, 0, 4, buf,
  1378.                                          &optim, &unavail))
  1379.             target += extract_unsigned_integer (buf, 4, byte_order) & -4;
  1380.           else
  1381.             {
  1382.               if (optim)
  1383.                 throw_error (OPTIMIZED_OUT_ERROR,
  1384.                              _("Could not determine address of "
  1385.                                "single-step breakpoint."));
  1386.               if (unavail)
  1387.                 throw_error (NOT_AVAILABLE_ERROR,
  1388.                              _("Could not determine address of "
  1389.                                "single-step breakpoint."));
  1390.             }
  1391.         }

  1392.       target = target & lslr;
  1393.       if (target != next_pc)
  1394.         insert_single_step_breakpoint (gdbarch, aspace,
  1395.                                        SPUADDR (SPUADDR_SPU (pc), target));
  1396.     }

  1397.   return 1;
  1398. }


  1399. /* Longjmp support.  */

  1400. static int
  1401. spu_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  1402. {
  1403.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1404.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1405.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1406.   gdb_byte buf[4];
  1407.   CORE_ADDR jb_addr;
  1408.   int optim, unavail;

  1409.   /* Jump buffer is pointed to by the argument register $r3.  */
  1410.   if (!get_frame_register_bytes (frame, SPU_ARG1_REGNUM, 0, 4, buf,
  1411.                                  &optim, &unavail))
  1412.     return 0;

  1413.   jb_addr = extract_unsigned_integer (buf, 4, byte_order);
  1414.   if (target_read_memory (SPUADDR (tdep->id, jb_addr), buf, 4))
  1415.     return 0;

  1416.   *pc = extract_unsigned_integer (buf, 4, byte_order);
  1417.   *pc = SPUADDR (tdep->id, *pc);
  1418.   return 1;
  1419. }


  1420. /* Disassembler.  */

  1421. struct spu_dis_asm_data
  1422. {
  1423.   struct gdbarch *gdbarch;
  1424.   int id;
  1425. };

  1426. static void
  1427. spu_dis_asm_print_address (bfd_vma addr, struct disassemble_info *info)
  1428. {
  1429.   struct spu_dis_asm_data *data = info->application_data;
  1430.   print_address (data->gdbarch, SPUADDR (data->id, addr), info->stream);
  1431. }

  1432. static int
  1433. gdb_print_insn_spu (bfd_vma memaddr, struct disassemble_info *info)
  1434. {
  1435.   /* The opcodes disassembler does 18-bit address arithmetic.  Make
  1436.      sure the SPU ID encoded in the high bits is added back when we
  1437.      call print_address.  */
  1438.   struct disassemble_info spu_info = *info;
  1439.   struct spu_dis_asm_data data;
  1440.   data.gdbarch = info->application_data;
  1441.   data.id = SPUADDR_SPU (memaddr);

  1442.   spu_info.application_data = &data;
  1443.   spu_info.print_address_func = spu_dis_asm_print_address;
  1444.   return print_insn_spu (memaddr, &spu_info);
  1445. }


  1446. /* Target overlays for the SPU overlay manager.

  1447.    See the documentation of simple_overlay_update for how the
  1448.    interface is supposed to work.

  1449.    Data structures used by the overlay manager:

  1450.    struct ovly_table
  1451.      {
  1452.         u32 vma;
  1453.         u32 size;
  1454.         u32 pos;
  1455.         u32 buf;
  1456.      } _ovly_table[];   -- one entry per overlay section

  1457.    struct ovly_buf_table
  1458.      {
  1459.         u32 mapped;
  1460.      } _ovly_buf_table[];  -- one entry per overlay buffer

  1461.    _ovly_table should never change.

  1462.    Both tables are aligned to a 16-byte boundary, the symbols
  1463.    _ovly_table and _ovly_buf_table are of type STT_OBJECT and their
  1464.    size set to the size of the respective array. buf in _ovly_table is
  1465.    an index into _ovly_buf_table.

  1466.    mapped is an index into _ovly_table.  Both the mapped and buf indices start
  1467.    from one to reference the first entry in their respective tables.  */

  1468. /* Using the per-objfile private data mechanism, we store for each
  1469.    objfile an array of "struct spu_overlay_table" structures, one
  1470.    for each obj_section of the objfile.  This structure holds two
  1471.    fields, MAPPED_PTR and MAPPED_VAL.  If MAPPED_PTR is zero, this
  1472.    is *not* an overlay section.  If it is non-zero, it represents
  1473.    a target address.  The overlay section is mapped iff the target
  1474.    integer at this location equals MAPPED_VAL.  */

  1475. static const struct objfile_data *spu_overlay_data;

  1476. struct spu_overlay_table
  1477.   {
  1478.     CORE_ADDR mapped_ptr;
  1479.     CORE_ADDR mapped_val;
  1480.   };

  1481. /* Retrieve the overlay table for OBJFILE.  If not already cached, read
  1482.    the _ovly_table data structure from the target and initialize the
  1483.    spu_overlay_table data structure from it.  */
  1484. static struct spu_overlay_table *
  1485. spu_get_overlay_table (struct objfile *objfile)
  1486. {
  1487.   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd)?
  1488.                    BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  1489.   struct bound_minimal_symbol ovly_table_msym, ovly_buf_table_msym;
  1490.   CORE_ADDR ovly_table_base, ovly_buf_table_base;
  1491.   unsigned ovly_table_size, ovly_buf_table_size;
  1492.   struct spu_overlay_table *tbl;
  1493.   struct obj_section *osect;
  1494.   gdb_byte *ovly_table;
  1495.   int i;

  1496.   tbl = objfile_data (objfile, spu_overlay_data);
  1497.   if (tbl)
  1498.     return tbl;

  1499.   ovly_table_msym = lookup_minimal_symbol ("_ovly_table", NULL, objfile);
  1500.   if (!ovly_table_msym.minsym)
  1501.     return NULL;

  1502.   ovly_buf_table_msym = lookup_minimal_symbol ("_ovly_buf_table",
  1503.                                                NULL, objfile);
  1504.   if (!ovly_buf_table_msym.minsym)
  1505.     return NULL;

  1506.   ovly_table_base = BMSYMBOL_VALUE_ADDRESS (ovly_table_msym);
  1507.   ovly_table_size = MSYMBOL_SIZE (ovly_table_msym.minsym);

  1508.   ovly_buf_table_base = BMSYMBOL_VALUE_ADDRESS (ovly_buf_table_msym);
  1509.   ovly_buf_table_size = MSYMBOL_SIZE (ovly_buf_table_msym.minsym);

  1510.   ovly_table = xmalloc (ovly_table_size);
  1511.   read_memory (ovly_table_base, ovly_table, ovly_table_size);

  1512.   tbl = OBSTACK_CALLOC (&objfile->objfile_obstack,
  1513.                         objfile->sections_end - objfile->sections,
  1514.                         struct spu_overlay_table);

  1515.   for (i = 0; i < ovly_table_size / 16; i++)
  1516.     {
  1517.       CORE_ADDR vma  = extract_unsigned_integer (ovly_table + 16*i + 0,
  1518.                                                  4, byte_order);
  1519.       CORE_ADDR size = extract_unsigned_integer (ovly_table + 16*i + 4,
  1520.                                                  4, byte_order);
  1521.       CORE_ADDR pos  = extract_unsigned_integer (ovly_table + 16*i + 8,
  1522.                                                  4, byte_order);
  1523.       CORE_ADDR buf  = extract_unsigned_integer (ovly_table + 16*i + 12,
  1524.                                                  4, byte_order);

  1525.       if (buf == 0 || (buf - 1) * 4 >= ovly_buf_table_size)
  1526.         continue;

  1527.       ALL_OBJFILE_OSECTIONS (objfile, osect)
  1528.         if (vma == bfd_section_vma (objfile->obfd, osect->the_bfd_section)
  1529.             && pos == osect->the_bfd_section->filepos)
  1530.           {
  1531.             int ndx = osect - objfile->sections;
  1532.             tbl[ndx].mapped_ptr = ovly_buf_table_base + (buf - 1) * 4;
  1533.             tbl[ndx].mapped_val = i + 1;
  1534.             break;
  1535.           }
  1536.     }

  1537.   xfree (ovly_table);
  1538.   set_objfile_data (objfile, spu_overlay_data, tbl);
  1539.   return tbl;
  1540. }

  1541. /* Read _ovly_buf_table entry from the target to dermine whether
  1542.    OSECT is currently mapped, and update the mapped state.  */
  1543. static void
  1544. spu_overlay_update_osect (struct obj_section *osect)
  1545. {
  1546.   enum bfd_endian byte_order = bfd_big_endian (osect->objfile->obfd)?
  1547.                    BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  1548.   struct spu_overlay_table *ovly_table;
  1549.   CORE_ADDR id, val;

  1550.   ovly_table = spu_get_overlay_table (osect->objfile);
  1551.   if (!ovly_table)
  1552.     return;

  1553.   ovly_table += osect - osect->objfile->sections;
  1554.   if (ovly_table->mapped_ptr == 0)
  1555.     return;

  1556.   id = SPUADDR_SPU (obj_section_addr (osect));
  1557.   val = read_memory_unsigned_integer (SPUADDR (id, ovly_table->mapped_ptr),
  1558.                                       4, byte_order);
  1559.   osect->ovly_mapped = (val == ovly_table->mapped_val);
  1560. }

  1561. /* If OSECT is NULL, then update all sections' mapped state.
  1562.    If OSECT is non-NULL, then update only OSECT's mapped state.  */
  1563. static void
  1564. spu_overlay_update (struct obj_section *osect)
  1565. {
  1566.   /* Just one section.  */
  1567.   if (osect)
  1568.     spu_overlay_update_osect (osect);

  1569.   /* All sections.  */
  1570.   else
  1571.     {
  1572.       struct objfile *objfile;

  1573.       ALL_OBJSECTIONS (objfile, osect)
  1574.         if (section_is_overlay (osect))
  1575.           spu_overlay_update_osect (osect);
  1576.     }
  1577. }

  1578. /* Whenever a new objfile is loaded, read the target's _ovly_table.
  1579.    If there is one, go through all sections and make sure for non-
  1580.    overlay sections LMA equals VMA, while for overlay sections LMA
  1581.    is larger than SPU_OVERLAY_LMA.  */
  1582. static void
  1583. spu_overlay_new_objfile (struct objfile *objfile)
  1584. {
  1585.   struct spu_overlay_table *ovly_table;
  1586.   struct obj_section *osect;

  1587.   /* If we've already touched this file, do nothing.  */
  1588.   if (!objfile || objfile_data (objfile, spu_overlay_data) != NULL)
  1589.     return;

  1590.   /* Consider only SPU objfiles.  */
  1591.   if (bfd_get_arch (objfile->obfd) != bfd_arch_spu)
  1592.     return;

  1593.   /* Check if this objfile has overlays.  */
  1594.   ovly_table = spu_get_overlay_table (objfile);
  1595.   if (!ovly_table)
  1596.     return;

  1597.   /* Now go and fiddle with all the LMAs.  */
  1598.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  1599.     {
  1600.       bfd *obfd = objfile->obfd;
  1601.       asection *bsect = osect->the_bfd_section;
  1602.       int ndx = osect - objfile->sections;

  1603.       if (ovly_table[ndx].mapped_ptr == 0)
  1604.         bfd_section_lma (obfd, bsect) = bfd_section_vma (obfd, bsect);
  1605.       else
  1606.         bfd_section_lma (obfd, bsect) = SPU_OVERLAY_LMA + bsect->filepos;
  1607.     }
  1608. }


  1609. /* Insert temporary breakpoint on "main" function of newly loaded
  1610.    SPE context OBJFILE.  */
  1611. static void
  1612. spu_catch_start (struct objfile *objfile)
  1613. {
  1614.   struct bound_minimal_symbol minsym;
  1615.   struct compunit_symtab *cust;
  1616.   CORE_ADDR pc;
  1617.   char buf[32];

  1618.   /* Do this only if requested by "set spu stop-on-load on".  */
  1619.   if (!spu_stop_on_load_p)
  1620.     return;

  1621.   /* Consider only SPU objfiles.  */
  1622.   if (!objfile || bfd_get_arch (objfile->obfd) != bfd_arch_spu)
  1623.     return;

  1624.   /* The main objfile is handled differently.  */
  1625.   if (objfile == symfile_objfile)
  1626.     return;

  1627.   /* There can be multiple symbols named "main".  Search for the
  1628.      "main" in *this* objfile.  */
  1629.   minsym = lookup_minimal_symbol ("main", NULL, objfile);
  1630.   if (!minsym.minsym)
  1631.     return;

  1632.   /* If we have debugging information, try to use it -- this
  1633.      will allow us to properly skip the prologue.  */
  1634.   pc = BMSYMBOL_VALUE_ADDRESS (minsym);
  1635.   cust
  1636.     = find_pc_sect_compunit_symtab (pc, MSYMBOL_OBJ_SECTION (minsym.objfile,
  1637.                                                              minsym.minsym));
  1638.   if (cust != NULL)
  1639.     {
  1640.       const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (cust);
  1641.       struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1642.       struct symbol *sym;
  1643.       struct symtab_and_line sal;

  1644.       sym = block_lookup_symbol (block, "main", VAR_DOMAIN);
  1645.       if (sym)
  1646.         {
  1647.           fixup_symbol_section (sym, objfile);
  1648.           sal = find_function_start_sal (sym, 1);
  1649.           pc = sal.pc;
  1650.         }
  1651.     }

  1652.   /* Use a numerical address for the set_breakpoint command to avoid having
  1653.      the breakpoint re-set incorrectly.  */
  1654.   xsnprintf (buf, sizeof buf, "*%s", core_addr_to_string (pc));
  1655.   create_breakpoint (get_objfile_arch (objfile), buf /* arg */,
  1656.                      NULL /* cond_string */, -1 /* thread */,
  1657.                      NULL /* extra_string */,
  1658.                      0 /* parse_condition_and_thread */, 1 /* tempflag */,
  1659.                      bp_breakpoint /* type_wanted */,
  1660.                      0 /* ignore_count */,
  1661.                      AUTO_BOOLEAN_FALSE /* pending_break_support */,
  1662.                      &bkpt_breakpoint_ops /* ops */, 0 /* from_tty */,
  1663.                      1 /* enabled */, 0 /* internal  */, 0);
  1664. }


  1665. /* Look up OBJFILE loaded into FRAME's SPU context.  */
  1666. static struct objfile *
  1667. spu_objfile_from_frame (struct frame_info *frame)
  1668. {
  1669.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1670.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1671.   struct objfile *obj;

  1672.   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
  1673.     return NULL;

  1674.   ALL_OBJFILES (obj)
  1675.     {
  1676.       if (obj->sections != obj->sections_end
  1677.           && SPUADDR_SPU (obj_section_addr (obj->sections)) == tdep->id)
  1678.         return obj;
  1679.     }

  1680.   return NULL;
  1681. }

  1682. /* Flush cache for ea pointer access if available.  */
  1683. static void
  1684. flush_ea_cache (void)
  1685. {
  1686.   struct bound_minimal_symbol msymbol;
  1687.   struct objfile *obj;

  1688.   if (!has_stack_frames ())
  1689.     return;

  1690.   obj = spu_objfile_from_frame (get_current_frame ());
  1691.   if (obj == NULL)
  1692.     return;

  1693.   /* Lookup inferior function __cache_flush.  */
  1694.   msymbol = lookup_minimal_symbol ("__cache_flush", NULL, obj);
  1695.   if (msymbol.minsym != NULL)
  1696.     {
  1697.       struct type *type;
  1698.       CORE_ADDR addr;

  1699.       type = objfile_type (obj)->builtin_void;
  1700.       type = lookup_function_type (type);
  1701.       type = lookup_pointer_type (type);
  1702.       addr = BMSYMBOL_VALUE_ADDRESS (msymbol);

  1703.       call_function_by_hand (value_from_pointer (type, addr), 0, NULL);
  1704.     }
  1705. }

  1706. /* This handler is called when the inferior has stopped.  If it is stopped in
  1707.    SPU architecture then flush the ea cache if used.  */
  1708. static void
  1709. spu_attach_normal_stop (struct bpstats *bs, int print_frame)
  1710. {
  1711.   if (!spu_auto_flush_cache_p)
  1712.     return;

  1713.   /* Temporarily reset spu_auto_flush_cache_p to avoid recursively
  1714.      re-entering this function when __cache_flush stops.  */
  1715.   spu_auto_flush_cache_p = 0;
  1716.   flush_ea_cache ();
  1717.   spu_auto_flush_cache_p = 1;
  1718. }


  1719. /* "info spu" commands.  */

  1720. static void
  1721. info_spu_event_command (char *args, int from_tty)
  1722. {
  1723.   struct frame_info *frame = get_selected_frame (NULL);
  1724.   ULONGEST event_status = 0;
  1725.   ULONGEST event_mask = 0;
  1726.   struct cleanup *chain;
  1727.   gdb_byte buf[100];
  1728.   char annex[32];
  1729.   LONGEST len;
  1730.   int id;

  1731.   if (gdbarch_bfd_arch_info (get_frame_arch (frame))->arch != bfd_arch_spu)
  1732.     error (_("\"info spu\" is only supported on the SPU architecture."));

  1733.   id = get_frame_register_unsigned (frame, SPU_ID_REGNUM);

  1734.   xsnprintf (annex, sizeof annex, "%d/event_status", id);
  1735.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1736.                      buf, 0, (sizeof (buf) - 1));
  1737.   if (len <= 0)
  1738.     error (_("Could not read event_status."));
  1739.   buf[len] = '\0';
  1740.   event_status = strtoulst ((char *) buf, NULL, 16);

  1741.   xsnprintf (annex, sizeof annex, "%d/event_mask", id);
  1742.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1743.                      buf, 0, (sizeof (buf) - 1));
  1744.   if (len <= 0)
  1745.     error (_("Could not read event_mask."));
  1746.   buf[len] = '\0';
  1747.   event_mask = strtoulst ((char *) buf, NULL, 16);

  1748.   chain = make_cleanup_ui_out_tuple_begin_end (current_uiout, "SPUInfoEvent");

  1749.   if (ui_out_is_mi_like_p (current_uiout))
  1750.     {
  1751.       ui_out_field_fmt (current_uiout, "event_status",
  1752.                         "0x%s", phex_nz (event_status, 4));
  1753.       ui_out_field_fmt (current_uiout, "event_mask",
  1754.                         "0x%s", phex_nz (event_mask, 4));
  1755.     }
  1756.   else
  1757.     {
  1758.       printf_filtered (_("Event Status 0x%s\n"), phex (event_status, 4));
  1759.       printf_filtered (_("Event Mask   0x%s\n"), phex (event_mask, 4));
  1760.     }

  1761.   do_cleanups (chain);
  1762. }

  1763. static void
  1764. info_spu_signal_command (char *args, int from_tty)
  1765. {
  1766.   struct frame_info *frame = get_selected_frame (NULL);
  1767.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1768.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1769.   ULONGEST signal1 = 0;
  1770.   ULONGEST signal1_type = 0;
  1771.   int signal1_pending = 0;
  1772.   ULONGEST signal2 = 0;
  1773.   ULONGEST signal2_type = 0;
  1774.   int signal2_pending = 0;
  1775.   struct cleanup *chain;
  1776.   char annex[32];
  1777.   gdb_byte buf[100];
  1778.   LONGEST len;
  1779.   int id;

  1780.   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
  1781.     error (_("\"info spu\" is only supported on the SPU architecture."));

  1782.   id = get_frame_register_unsigned (frame, SPU_ID_REGNUM);

  1783.   xsnprintf (annex, sizeof annex, "%d/signal1", id);
  1784.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex, buf, 0, 4);
  1785.   if (len < 0)
  1786.     error (_("Could not read signal1."));
  1787.   else if (len == 4)
  1788.     {
  1789.       signal1 = extract_unsigned_integer (buf, 4, byte_order);
  1790.       signal1_pending = 1;
  1791.     }

  1792.   xsnprintf (annex, sizeof annex, "%d/signal1_type", id);
  1793.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1794.                      buf, 0, (sizeof (buf) - 1));
  1795.   if (len <= 0)
  1796.     error (_("Could not read signal1_type."));
  1797.   buf[len] = '\0';
  1798.   signal1_type = strtoulst ((char *) buf, NULL, 16);

  1799.   xsnprintf (annex, sizeof annex, "%d/signal2", id);
  1800.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex, buf, 0, 4);
  1801.   if (len < 0)
  1802.     error (_("Could not read signal2."));
  1803.   else if (len == 4)
  1804.     {
  1805.       signal2 = extract_unsigned_integer (buf, 4, byte_order);
  1806.       signal2_pending = 1;
  1807.     }

  1808.   xsnprintf (annex, sizeof annex, "%d/signal2_type", id);
  1809.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1810.                      buf, 0, (sizeof (buf) - 1));
  1811.   if (len <= 0)
  1812.     error (_("Could not read signal2_type."));
  1813.   buf[len] = '\0';
  1814.   signal2_type = strtoulst ((char *) buf, NULL, 16);

  1815.   chain = make_cleanup_ui_out_tuple_begin_end (current_uiout, "SPUInfoSignal");

  1816.   if (ui_out_is_mi_like_p (current_uiout))
  1817.     {
  1818.       ui_out_field_int (current_uiout, "signal1_pending", signal1_pending);
  1819.       ui_out_field_fmt (current_uiout, "signal1", "0x%s", phex_nz (signal1, 4));
  1820.       ui_out_field_int (current_uiout, "signal1_type", signal1_type);
  1821.       ui_out_field_int (current_uiout, "signal2_pending", signal2_pending);
  1822.       ui_out_field_fmt (current_uiout, "signal2", "0x%s", phex_nz (signal2, 4));
  1823.       ui_out_field_int (current_uiout, "signal2_type", signal2_type);
  1824.     }
  1825.   else
  1826.     {
  1827.       if (signal1_pending)
  1828.         printf_filtered (_("Signal 1 control word 0x%s "), phex (signal1, 4));
  1829.       else
  1830.         printf_filtered (_("Signal 1 not pending "));

  1831.       if (signal1_type)
  1832.         printf_filtered (_("(Type Or)\n"));
  1833.       else
  1834.         printf_filtered (_("(Type Overwrite)\n"));

  1835.       if (signal2_pending)
  1836.         printf_filtered (_("Signal 2 control word 0x%s "), phex (signal2, 4));
  1837.       else
  1838.         printf_filtered (_("Signal 2 not pending "));

  1839.       if (signal2_type)
  1840.         printf_filtered (_("(Type Or)\n"));
  1841.       else
  1842.         printf_filtered (_("(Type Overwrite)\n"));
  1843.     }

  1844.   do_cleanups (chain);
  1845. }

  1846. static void
  1847. info_spu_mailbox_list (gdb_byte *buf, int nr, enum bfd_endian byte_order,
  1848.                        const char *field, const char *msg)
  1849. {
  1850.   struct cleanup *chain;
  1851.   int i;

  1852.   if (nr <= 0)
  1853.     return;

  1854.   chain = make_cleanup_ui_out_table_begin_end (current_uiout, 1, nr, "mbox");

  1855.   ui_out_table_header (current_uiout, 32, ui_left, field, msg);
  1856.   ui_out_table_body (current_uiout);

  1857.   for (i = 0; i < nr; i++)
  1858.     {
  1859.       struct cleanup *val_chain;
  1860.       ULONGEST val;
  1861.       val_chain = make_cleanup_ui_out_tuple_begin_end (current_uiout, "mbox");
  1862.       val = extract_unsigned_integer (buf + 4*i, 4, byte_order);
  1863.       ui_out_field_fmt (current_uiout, field, "0x%s", phex (val, 4));
  1864.       do_cleanups (val_chain);

  1865.       if (!ui_out_is_mi_like_p (current_uiout))
  1866.         printf_filtered ("\n");
  1867.     }

  1868.   do_cleanups (chain);
  1869. }

  1870. static void
  1871. info_spu_mailbox_command (char *args, int from_tty)
  1872. {
  1873.   struct frame_info *frame = get_selected_frame (NULL);
  1874.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1875.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1876.   struct cleanup *chain;
  1877.   char annex[32];
  1878.   gdb_byte buf[1024];
  1879.   LONGEST len;
  1880.   int id;

  1881.   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
  1882.     error (_("\"info spu\" is only supported on the SPU architecture."));

  1883.   id = get_frame_register_unsigned (frame, SPU_ID_REGNUM);

  1884.   chain = make_cleanup_ui_out_tuple_begin_end (current_uiout, "SPUInfoMailbox");

  1885.   xsnprintf (annex, sizeof annex, "%d/mbox_info", id);
  1886.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1887.                      buf, 0, sizeof buf);
  1888.   if (len < 0)
  1889.     error (_("Could not read mbox_info."));

  1890.   info_spu_mailbox_list (buf, len / 4, byte_order,
  1891.                          "mbox", "SPU Outbound Mailbox");

  1892.   xsnprintf (annex, sizeof annex, "%d/ibox_info", id);
  1893.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1894.                      buf, 0, sizeof buf);
  1895.   if (len < 0)
  1896.     error (_("Could not read ibox_info."));

  1897.   info_spu_mailbox_list (buf, len / 4, byte_order,
  1898.                          "ibox", "SPU Outbound Interrupt Mailbox");

  1899.   xsnprintf (annex, sizeof annex, "%d/wbox_info", id);
  1900.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1901.                      buf, 0, sizeof buf);
  1902.   if (len < 0)
  1903.     error (_("Could not read wbox_info."));

  1904.   info_spu_mailbox_list (buf, len / 4, byte_order,
  1905.                          "wbox", "SPU Inbound Mailbox");

  1906.   do_cleanups (chain);
  1907. }

  1908. static ULONGEST
  1909. spu_mfc_get_bitfield (ULONGEST word, int first, int last)
  1910. {
  1911.   ULONGEST mask = ~(~(ULONGEST)0 << (last - first + 1));
  1912.   return (word >> (63 - last)) & mask;
  1913. }

  1914. static void
  1915. info_spu_dma_cmdlist (gdb_byte *buf, int nr, enum bfd_endian byte_order)
  1916. {
  1917.   static char *spu_mfc_opcode[256] =
  1918.     {
  1919.     /* 00 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1920.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1921.     /* 10 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1922.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1923.     /* 20 */ "put", "putb", "putf", NULL, "putl", "putlb", "putlf", NULL,
  1924.              "puts", "putbs", "putfs", NULL, NULL, NULL, NULL, NULL,
  1925.     /* 30 */ "putr", "putrb", "putrf", NULL, "putrl", "putrlb", "putrlf", NULL,
  1926.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1927.     /* 40 */ "get", "getb", "getf", NULL, "getl", "getlb", "getlf", NULL,
  1928.              "gets", "getbs", "getfs", NULL, NULL, NULL, NULL, NULL,
  1929.     /* 50 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1930.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1931.     /* 60 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1932.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1933.     /* 70 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1934.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1935.     /* 80 */ "sdcrt", "sdcrtst", NULL, NULL, NULL, NULL, NULL, NULL,
  1936.              NULL, "sdcrz", NULL, NULL, NULL, "sdcrst", NULL, "sdcrf",
  1937.     /* 90 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1938.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1939.     /* a0 */ "sndsig", "sndsigb", "sndsigf", NULL, NULL, NULL, NULL, NULL,
  1940.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1941.     /* b0 */ "putlluc", NULL, NULL, NULL, "putllc", NULL, NULL, NULL,
  1942.              "putqlluc", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1943.     /* c0 */ "barrier", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1944.              "mfceieio", NULL, NULL, NULL, "mfcsync", NULL, NULL, NULL,
  1945.     /* d0 */ "getllar", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1946.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1947.     /* e0 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1948.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1949.     /* f0 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1950.              NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1951.     };

  1952.   int *seq = alloca (nr * sizeof (int));
  1953.   int done = 0;
  1954.   struct cleanup *chain;
  1955.   int i, j;


  1956.   /* Determine sequence in which to display (valid) entries.  */
  1957.   for (i = 0; i < nr; i++)
  1958.     {
  1959.       /* Search for the first valid entry all of whose
  1960.          dependencies are met.  */
  1961.       for (j = 0; j < nr; j++)
  1962.         {
  1963.           ULONGEST mfc_cq_dw3;
  1964.           ULONGEST dependencies;

  1965.           if (done & (1 << (nr - 1 - j)))
  1966.             continue;

  1967.           mfc_cq_dw3
  1968.             = extract_unsigned_integer (buf + 32*j + 24,8, byte_order);
  1969.           if (!spu_mfc_get_bitfield (mfc_cq_dw3, 16, 16))
  1970.             continue;

  1971.           dependencies = spu_mfc_get_bitfield (mfc_cq_dw3, 0, nr - 1);
  1972.           if ((dependencies & done) != dependencies)
  1973.             continue;

  1974.           seq[i] = j;
  1975.           done |= 1 << (nr - 1 - j);
  1976.           break;
  1977.         }

  1978.       if (j == nr)
  1979.         break;
  1980.     }

  1981.   nr = i;


  1982.   chain = make_cleanup_ui_out_table_begin_end (current_uiout, 10, nr,
  1983.                                                "dma_cmd");

  1984.   ui_out_table_header (current_uiout, 7, ui_left, "opcode", "Opcode");
  1985.   ui_out_table_header (current_uiout, 3, ui_left, "tag", "Tag");
  1986.   ui_out_table_header (current_uiout, 3, ui_left, "tid", "TId");
  1987.   ui_out_table_header (current_uiout, 3, ui_left, "rid", "RId");
  1988.   ui_out_table_header (current_uiout, 18, ui_left, "ea", "EA");
  1989.   ui_out_table_header (current_uiout, 7, ui_left, "lsa", "LSA");
  1990.   ui_out_table_header (current_uiout, 7, ui_left, "size", "Size");
  1991.   ui_out_table_header (current_uiout, 7, ui_left, "lstaddr", "LstAddr");
  1992.   ui_out_table_header (current_uiout, 7, ui_left, "lstsize", "LstSize");
  1993.   ui_out_table_header (current_uiout, 1, ui_left, "error_p", "E");

  1994.   ui_out_table_body (current_uiout);

  1995.   for (i = 0; i < nr; i++)
  1996.     {
  1997.       struct cleanup *cmd_chain;
  1998.       ULONGEST mfc_cq_dw0;
  1999.       ULONGEST mfc_cq_dw1;
  2000.       ULONGEST mfc_cq_dw2;
  2001.       int mfc_cmd_opcode, mfc_cmd_tag, rclass_id, tclass_id;
  2002.       int list_lsa, list_size, mfc_lsa, mfc_size;
  2003.       ULONGEST mfc_ea;
  2004.       int list_valid_p, noop_valid_p, qw_valid_p, ea_valid_p, cmd_error_p;

  2005.       /* Decode contents of MFC Command Queue Context Save/Restore Registers.
  2006.          See "Cell Broadband Engine Registers V1.3", section 3.3.2.1.  */

  2007.       mfc_cq_dw0
  2008.         = extract_unsigned_integer (buf + 32*seq[i], 8, byte_order);
  2009.       mfc_cq_dw1
  2010.         = extract_unsigned_integer (buf + 32*seq[i] + 8, 8, byte_order);
  2011.       mfc_cq_dw2
  2012.         = extract_unsigned_integer (buf + 32*seq[i] + 16, 8, byte_order);

  2013.       list_lsa = spu_mfc_get_bitfield (mfc_cq_dw0, 0, 14);
  2014.       list_size = spu_mfc_get_bitfield (mfc_cq_dw0, 15, 26);
  2015.       mfc_cmd_opcode = spu_mfc_get_bitfield (mfc_cq_dw0, 27, 34);
  2016.       mfc_cmd_tag = spu_mfc_get_bitfield (mfc_cq_dw0, 35, 39);
  2017.       list_valid_p = spu_mfc_get_bitfield (mfc_cq_dw0, 40, 40);
  2018.       rclass_id = spu_mfc_get_bitfield (mfc_cq_dw0, 41, 43);
  2019.       tclass_id = spu_mfc_get_bitfield (mfc_cq_dw0, 44, 46);

  2020.       mfc_ea = spu_mfc_get_bitfield (mfc_cq_dw1, 0, 51) << 12
  2021.                 | spu_mfc_get_bitfield (mfc_cq_dw2, 25, 36);

  2022.       mfc_lsa = spu_mfc_get_bitfield (mfc_cq_dw2, 0, 13);
  2023.       mfc_size = spu_mfc_get_bitfield (mfc_cq_dw2, 14, 24);
  2024.       noop_valid_p = spu_mfc_get_bitfield (mfc_cq_dw2, 37, 37);
  2025.       qw_valid_p = spu_mfc_get_bitfield (mfc_cq_dw2, 38, 38);
  2026.       ea_valid_p = spu_mfc_get_bitfield (mfc_cq_dw2, 39, 39);
  2027.       cmd_error_p = spu_mfc_get_bitfield (mfc_cq_dw2, 40, 40);

  2028.       cmd_chain = make_cleanup_ui_out_tuple_begin_end (current_uiout, "cmd");

  2029.       if (spu_mfc_opcode[mfc_cmd_opcode])
  2030.         ui_out_field_string (current_uiout, "opcode", spu_mfc_opcode[mfc_cmd_opcode]);
  2031.       else
  2032.         ui_out_field_int (current_uiout, "opcode", mfc_cmd_opcode);

  2033.       ui_out_field_int (current_uiout, "tag", mfc_cmd_tag);
  2034.       ui_out_field_int (current_uiout, "tid", tclass_id);
  2035.       ui_out_field_int (current_uiout, "rid", rclass_id);

  2036.       if (ea_valid_p)
  2037.         ui_out_field_fmt (current_uiout, "ea", "0x%s", phex (mfc_ea, 8));
  2038.       else
  2039.         ui_out_field_skip (current_uiout, "ea");

  2040.       ui_out_field_fmt (current_uiout, "lsa", "0x%05x", mfc_lsa << 4);
  2041.       if (qw_valid_p)
  2042.         ui_out_field_fmt (current_uiout, "size", "0x%05x", mfc_size << 4);
  2043.       else
  2044.         ui_out_field_fmt (current_uiout, "size", "0x%05x", mfc_size);

  2045.       if (list_valid_p)
  2046.         {
  2047.           ui_out_field_fmt (current_uiout, "lstaddr", "0x%05x", list_lsa << 3);
  2048.           ui_out_field_fmt (current_uiout, "lstsize", "0x%05x", list_size << 3);
  2049.         }
  2050.       else
  2051.         {
  2052.           ui_out_field_skip (current_uiout, "lstaddr");
  2053.           ui_out_field_skip (current_uiout, "lstsize");
  2054.         }

  2055.       if (cmd_error_p)
  2056.         ui_out_field_string (current_uiout, "error_p", "*");
  2057.       else
  2058.         ui_out_field_skip (current_uiout, "error_p");

  2059.       do_cleanups (cmd_chain);

  2060.       if (!ui_out_is_mi_like_p (current_uiout))
  2061.         printf_filtered ("\n");
  2062.     }

  2063.   do_cleanups (chain);
  2064. }

  2065. static void
  2066. info_spu_dma_command (char *args, int from_tty)
  2067. {
  2068.   struct frame_info *frame = get_selected_frame (NULL);
  2069.   struct gdbarch *gdbarch = get_frame_arch (frame);
  2070.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2071.   ULONGEST dma_info_type;
  2072.   ULONGEST dma_info_mask;
  2073.   ULONGEST dma_info_status;
  2074.   ULONGEST dma_info_stall_and_notify;
  2075.   ULONGEST dma_info_atomic_command_status;
  2076.   struct cleanup *chain;
  2077.   char annex[32];
  2078.   gdb_byte buf[1024];
  2079.   LONGEST len;
  2080.   int id;

  2081.   if (gdbarch_bfd_arch_info (get_frame_arch (frame))->arch != bfd_arch_spu)
  2082.     error (_("\"info spu\" is only supported on the SPU architecture."));

  2083.   id = get_frame_register_unsigned (frame, SPU_ID_REGNUM);

  2084.   xsnprintf (annex, sizeof annex, "%d/dma_info", id);
  2085.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  2086.                      buf, 0, 40 + 16 * 32);
  2087.   if (len <= 0)
  2088.     error (_("Could not read dma_info."));

  2089.   dma_info_type
  2090.     = extract_unsigned_integer (buf, 8, byte_order);
  2091.   dma_info_mask
  2092.     = extract_unsigned_integer (buf + 8, 8, byte_order);
  2093.   dma_info_status
  2094.     = extract_unsigned_integer (buf + 16, 8, byte_order);
  2095.   dma_info_stall_and_notify
  2096.     = extract_unsigned_integer (buf + 24, 8, byte_order);
  2097.   dma_info_atomic_command_status
  2098.     = extract_unsigned_integer (buf + 32, 8, byte_order);

  2099.   chain = make_cleanup_ui_out_tuple_begin_end (current_uiout, "SPUInfoDMA");

  2100.   if (ui_out_is_mi_like_p (current_uiout))
  2101.     {
  2102.       ui_out_field_fmt (current_uiout, "dma_info_type", "0x%s",
  2103.                         phex_nz (dma_info_type, 4));
  2104.       ui_out_field_fmt (current_uiout, "dma_info_mask", "0x%s",
  2105.                         phex_nz (dma_info_mask, 4));
  2106.       ui_out_field_fmt (current_uiout, "dma_info_status", "0x%s",
  2107.                         phex_nz (dma_info_status, 4));
  2108.       ui_out_field_fmt (current_uiout, "dma_info_stall_and_notify", "0x%s",
  2109.                         phex_nz (dma_info_stall_and_notify, 4));
  2110.       ui_out_field_fmt (current_uiout, "dma_info_atomic_command_status", "0x%s",
  2111.                         phex_nz (dma_info_atomic_command_status, 4));
  2112.     }
  2113.   else
  2114.     {
  2115.       const char *query_msg = _("no query pending");

  2116.       if (dma_info_type & 4)
  2117.         switch (dma_info_type & 3)
  2118.           {
  2119.             case 1: query_msg = _("'any' query pending"); break;
  2120.             case 2: query_msg = _("'all' query pending"); break;
  2121.             default: query_msg = _("undefined query type"); break;
  2122.           }

  2123.       printf_filtered (_("Tag-Group Status  0x%s\n"),
  2124.                        phex (dma_info_status, 4));
  2125.       printf_filtered (_("Tag-Group Mask    0x%s (%s)\n"),
  2126.                        phex (dma_info_mask, 4), query_msg);
  2127.       printf_filtered (_("Stall-and-Notify  0x%s\n"),
  2128.                        phex (dma_info_stall_and_notify, 4));
  2129.       printf_filtered (_("Atomic Cmd Status 0x%s\n"),
  2130.                        phex (dma_info_atomic_command_status, 4));
  2131.       printf_filtered ("\n");
  2132.     }

  2133.   info_spu_dma_cmdlist (buf + 40, 16, byte_order);
  2134.   do_cleanups (chain);
  2135. }

  2136. static void
  2137. info_spu_proxydma_command (char *args, int from_tty)
  2138. {
  2139.   struct frame_info *frame = get_selected_frame (NULL);
  2140.   struct gdbarch *gdbarch = get_frame_arch (frame);
  2141.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2142.   ULONGEST dma_info_type;
  2143.   ULONGEST dma_info_mask;
  2144.   ULONGEST dma_info_status;
  2145.   struct cleanup *chain;
  2146.   char annex[32];
  2147.   gdb_byte buf[1024];
  2148.   LONGEST len;
  2149.   int id;

  2150.   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
  2151.     error (_("\"info spu\" is only supported on the SPU architecture."));

  2152.   id = get_frame_register_unsigned (frame, SPU_ID_REGNUM);

  2153.   xsnprintf (annex, sizeof annex, "%d/proxydma_info", id);
  2154.   len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
  2155.                      buf, 0, 24 + 8 * 32);
  2156.   if (len <= 0)
  2157.     error (_("Could not read proxydma_info."));

  2158.   dma_info_type = extract_unsigned_integer (buf, 8, byte_order);
  2159.   dma_info_mask = extract_unsigned_integer (buf + 8, 8, byte_order);
  2160.   dma_info_status = extract_unsigned_integer (buf + 16, 8, byte_order);

  2161.   chain = make_cleanup_ui_out_tuple_begin_end (current_uiout,
  2162.                                                "SPUInfoProxyDMA");

  2163.   if (ui_out_is_mi_like_p (current_uiout))
  2164.     {
  2165.       ui_out_field_fmt (current_uiout, "proxydma_info_type", "0x%s",
  2166.                         phex_nz (dma_info_type, 4));
  2167.       ui_out_field_fmt (current_uiout, "proxydma_info_mask", "0x%s",
  2168.                         phex_nz (dma_info_mask, 4));
  2169.       ui_out_field_fmt (current_uiout, "proxydma_info_status", "0x%s",
  2170.                         phex_nz (dma_info_status, 4));
  2171.     }
  2172.   else
  2173.     {
  2174.       const char *query_msg;

  2175.       switch (dma_info_type & 3)
  2176.         {
  2177.         case 0: query_msg = _("no query pending"); break;
  2178.         case 1: query_msg = _("'any' query pending"); break;
  2179.         case 2: query_msg = _("'all' query pending"); break;
  2180.         default: query_msg = _("undefined query type"); break;
  2181.         }

  2182.       printf_filtered (_("Tag-Group Status  0x%s\n"),
  2183.                        phex (dma_info_status, 4));
  2184.       printf_filtered (_("Tag-Group Mask    0x%s (%s)\n"),
  2185.                        phex (dma_info_mask, 4), query_msg);
  2186.       printf_filtered ("\n");
  2187.     }

  2188.   info_spu_dma_cmdlist (buf + 24, 8, byte_order);
  2189.   do_cleanups (chain);
  2190. }

  2191. static void
  2192. info_spu_command (char *args, int from_tty)
  2193. {
  2194.   printf_unfiltered (_("\"info spu\" must be followed by "
  2195.                        "the name of an SPU facility.\n"));
  2196.   help_list (infospucmdlist, "info spu ", all_commands, gdb_stdout);
  2197. }


  2198. /* Root of all "set spu "/"show spu " commands.  */

  2199. static void
  2200. show_spu_command (char *args, int from_tty)
  2201. {
  2202.   help_list (showspucmdlist, "show spu ", all_commands, gdb_stdout);
  2203. }

  2204. static void
  2205. set_spu_command (char *args, int from_tty)
  2206. {
  2207.   help_list (setspucmdlist, "set spu ", all_commands, gdb_stdout);
  2208. }

  2209. static void
  2210. show_spu_stop_on_load (struct ui_file *file, int from_tty,
  2211.                        struct cmd_list_element *c, const char *value)
  2212. {
  2213.   fprintf_filtered (file, _("Stopping for new SPE threads is %s.\n"),
  2214.                     value);
  2215. }

  2216. static void
  2217. show_spu_auto_flush_cache (struct ui_file *file, int from_tty,
  2218.                            struct cmd_list_element *c, const char *value)
  2219. {
  2220.   fprintf_filtered (file, _("Automatic software-cache flush is %s.\n"),
  2221.                     value);
  2222. }


  2223. /* Set up gdbarch struct.  */

  2224. static struct gdbarch *
  2225. spu_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2226. {
  2227.   struct gdbarch *gdbarch;
  2228.   struct gdbarch_tdep *tdep;
  2229.   int id = -1;

  2230.   /* Which spufs ID was requested as address space?  */
  2231.   if (info.tdep_info)
  2232.     id = *(int *)info.tdep_info;
  2233.   /* For objfile architectures of SPU solibs, decode the ID from the name.
  2234.      This assumes the filename convention employed by solib-spu.c.  */
  2235.   else if (info.abfd)
  2236.     {
  2237.       char *name = strrchr (info.abfd->filename, '@');
  2238.       if (name)
  2239.         sscanf (name, "@0x%*x <%d>", &id);
  2240.     }

  2241.   /* Find a candidate among extant architectures.  */
  2242.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  2243.        arches != NULL;
  2244.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  2245.     {
  2246.       tdep = gdbarch_tdep (arches->gdbarch);
  2247.       if (tdep && tdep->id == id)
  2248.         return arches->gdbarch;
  2249.     }

  2250.   /* None found, so create a new architecture.  */
  2251.   tdep = XCNEW (struct gdbarch_tdep);
  2252.   tdep->id = id;
  2253.   gdbarch = gdbarch_alloc (&info, tdep);

  2254.   /* Disassembler.  */
  2255.   set_gdbarch_print_insn (gdbarch, gdb_print_insn_spu);

  2256.   /* Registers.  */
  2257.   set_gdbarch_num_regs (gdbarch, SPU_NUM_REGS);
  2258.   set_gdbarch_num_pseudo_regs (gdbarch, SPU_NUM_PSEUDO_REGS);
  2259.   set_gdbarch_sp_regnum (gdbarch, SPU_SP_REGNUM);
  2260.   set_gdbarch_pc_regnum (gdbarch, SPU_PC_REGNUM);
  2261.   set_gdbarch_read_pc (gdbarch, spu_read_pc);
  2262.   set_gdbarch_write_pc (gdbarch, spu_write_pc);
  2263.   set_gdbarch_register_name (gdbarch, spu_register_name);
  2264.   set_gdbarch_register_type (gdbarch, spu_register_type);
  2265.   set_gdbarch_pseudo_register_read (gdbarch, spu_pseudo_register_read);
  2266.   set_gdbarch_pseudo_register_write (gdbarch, spu_pseudo_register_write);
  2267.   set_gdbarch_value_from_register (gdbarch, spu_value_from_register);
  2268.   set_gdbarch_register_reggroup_p (gdbarch, spu_register_reggroup_p);
  2269.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, spu_dwarf_reg_to_regnum);
  2270.   set_gdbarch_ax_pseudo_register_collect
  2271.     (gdbarch, spu_ax_pseudo_register_collect);
  2272.   set_gdbarch_ax_pseudo_register_push_stack
  2273.     (gdbarch, spu_ax_pseudo_register_push_stack);

  2274.   /* Data types.  */
  2275.   set_gdbarch_char_signed (gdbarch, 0);
  2276.   set_gdbarch_ptr_bit (gdbarch, 32);
  2277.   set_gdbarch_addr_bit (gdbarch, 32);
  2278.   set_gdbarch_short_bit (gdbarch, 16);
  2279.   set_gdbarch_int_bit (gdbarch, 32);
  2280.   set_gdbarch_long_bit (gdbarch, 32);
  2281.   set_gdbarch_long_long_bit (gdbarch, 64);
  2282.   set_gdbarch_float_bit (gdbarch, 32);
  2283.   set_gdbarch_double_bit (gdbarch, 64);
  2284.   set_gdbarch_long_double_bit (gdbarch, 64);
  2285.   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  2286.   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  2287.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);

  2288.   /* Address handling.  */
  2289.   set_gdbarch_address_to_pointer (gdbarch, spu_address_to_pointer);
  2290.   set_gdbarch_pointer_to_address (gdbarch, spu_pointer_to_address);
  2291.   set_gdbarch_integer_to_address (gdbarch, spu_integer_to_address);
  2292.   set_gdbarch_address_class_type_flags (gdbarch, spu_address_class_type_flags);
  2293.   set_gdbarch_address_class_type_flags_to_name
  2294.     (gdbarch, spu_address_class_type_flags_to_name);
  2295.   set_gdbarch_address_class_name_to_type_flags
  2296.     (gdbarch, spu_address_class_name_to_type_flags);


  2297.   /* Inferior function calls.  */
  2298.   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  2299.   set_gdbarch_frame_align (gdbarch, spu_frame_align);
  2300.   set_gdbarch_frame_red_zone_size (gdbarch, 2000);
  2301.   set_gdbarch_push_dummy_code (gdbarch, spu_push_dummy_code);
  2302.   set_gdbarch_push_dummy_call (gdbarch, spu_push_dummy_call);
  2303.   set_gdbarch_dummy_id (gdbarch, spu_dummy_id);
  2304.   set_gdbarch_return_value (gdbarch, spu_return_value);

  2305.   /* Frame handling.  */
  2306.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2307.   dwarf2_append_unwinders (gdbarch);
  2308.   frame_unwind_append_unwinder (gdbarch, &spu_frame_unwind);
  2309.   frame_base_set_default (gdbarch, &spu_frame_base);
  2310.   set_gdbarch_unwind_pc (gdbarch, spu_unwind_pc);
  2311.   set_gdbarch_unwind_sp (gdbarch, spu_unwind_sp);
  2312.   set_gdbarch_virtual_frame_pointer (gdbarch, spu_virtual_frame_pointer);
  2313.   set_gdbarch_frame_args_skip (gdbarch, 0);
  2314.   set_gdbarch_skip_prologue (gdbarch, spu_skip_prologue);
  2315.   set_gdbarch_in_function_epilogue_p (gdbarch, spu_in_function_epilogue_p);

  2316.   /* Cell/B.E. cross-architecture unwinder support.  */
  2317.   frame_unwind_prepend_unwinder (gdbarch, &spu2ppu_unwind);

  2318.   /* Breakpoints.  */
  2319.   set_gdbarch_decr_pc_after_break (gdbarch, 4);
  2320.   set_gdbarch_breakpoint_from_pc (gdbarch, spu_breakpoint_from_pc);
  2321.   set_gdbarch_memory_remove_breakpoint (gdbarch, spu_memory_remove_breakpoint);
  2322.   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
  2323.   set_gdbarch_software_single_step (gdbarch, spu_software_single_step);
  2324.   set_gdbarch_get_longjmp_target (gdbarch, spu_get_longjmp_target);

  2325.   /* Overlays.  */
  2326.   set_gdbarch_overlay_update (gdbarch, spu_overlay_update);

  2327.   return gdbarch;
  2328. }

  2329. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2330. extern initialize_file_ftype _initialize_spu_tdep;

  2331. void
  2332. _initialize_spu_tdep (void)
  2333. {
  2334.   register_gdbarch_init (bfd_arch_spu, spu_gdbarch_init);

  2335.   /* Add ourselves to objfile event chain.  */
  2336.   observer_attach_new_objfile (spu_overlay_new_objfile);
  2337.   spu_overlay_data = register_objfile_data ();

  2338.   /* Install spu stop-on-load handler.  */
  2339.   observer_attach_new_objfile (spu_catch_start);

  2340.   /* Add ourselves to normal_stop event chain.  */
  2341.   observer_attach_normal_stop (spu_attach_normal_stop);

  2342.   /* Add root prefix command for all "set spu"/"show spu" commands.  */
  2343.   add_prefix_cmd ("spu", no_class, set_spu_command,
  2344.                   _("Various SPU specific commands."),
  2345.                   &setspucmdlist, "set spu ", 0, &setlist);
  2346.   add_prefix_cmd ("spu", no_class, show_spu_command,
  2347.                   _("Various SPU specific commands."),
  2348.                   &showspucmdlist, "show spu ", 0, &showlist);

  2349.   /* Toggle whether or not to add a temporary breakpoint at the "main"
  2350.      function of new SPE contexts.  */
  2351.   add_setshow_boolean_cmd ("stop-on-load", class_support,
  2352.                           &spu_stop_on_load_p, _("\
  2353. Set whether to stop for new SPE threads."),
  2354.                            _("\
  2355. Show whether to stop for new SPE threads."),
  2356.                            _("\
  2357. Use \"on\" to give control to the user when a new SPE thread\n\
  2358. enters its \"main\" function.\n\
  2359. Use \"off\" to disable stopping for new SPE threads."),
  2360.                           NULL,
  2361.                           show_spu_stop_on_load,
  2362.                           &setspucmdlist, &showspucmdlist);

  2363.   /* Toggle whether or not to automatically flush the software-managed
  2364.      cache whenever SPE execution stops.  */
  2365.   add_setshow_boolean_cmd ("auto-flush-cache", class_support,
  2366.                           &spu_auto_flush_cache_p, _("\
  2367. Set whether to automatically flush the software-managed cache."),
  2368.                            _("\
  2369. Show whether to automatically flush the software-managed cache."),
  2370.                            _("\
  2371. Use \"on\" to automatically flush the software-managed cache\n\
  2372. whenever SPE execution stops.\n\
  2373. Use \"off\" to never automatically flush the software-managed cache."),
  2374.                           NULL,
  2375.                           show_spu_auto_flush_cache,
  2376.                           &setspucmdlist, &showspucmdlist);

  2377.   /* Add root prefix command for all "info spu" commands.  */
  2378.   add_prefix_cmd ("spu", class_info, info_spu_command,
  2379.                   _("Various SPU specific commands."),
  2380.                   &infospucmdlist, "info spu ", 0, &infolist);

  2381.   /* Add various "info spu" commands.  */
  2382.   add_cmd ("event", class_info, info_spu_event_command,
  2383.            _("Display SPU event facility status.\n"),
  2384.            &infospucmdlist);
  2385.   add_cmd ("signal", class_info, info_spu_signal_command,
  2386.            _("Display SPU signal notification facility status.\n"),
  2387.            &infospucmdlist);
  2388.   add_cmd ("mailbox", class_info, info_spu_mailbox_command,
  2389.            _("Display SPU mailbox facility status.\n"),
  2390.            &infospucmdlist);
  2391.   add_cmd ("dma", class_info, info_spu_dma_command,
  2392.            _("Display MFC DMA status.\n"),
  2393.            &infospucmdlist);
  2394.   add_cmd ("proxydma", class_info, info_spu_proxydma_command,
  2395.            _("Display MFC Proxy-DMA status.\n"),
  2396.            &infospucmdlist);
  2397. }