gdb/ia64-hpux-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

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

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "ia64-tdep.h"
  16. #include "ia64-hpux-tdep.h"
  17. #include "osabi.h"
  18. #include "gdbtypes.h"
  19. #include "solib.h"
  20. #include "target.h"
  21. #include "frame.h"
  22. #include "regcache.h"
  23. #include "gdbcore.h"
  24. #include "inferior.h"

  25. /* A sequence of instructions pushed on the stack when we want to perform
  26.    an inferior function call.  The main purpose of this code is to save
  27.    the output region of the register frame belonging to the function
  28.    from which we are making the call.  Normally, all registers are saved
  29.    prior to the call, but this does not include stacked registers because
  30.    they are seen by GDB as pseudo registers.

  31.    With Linux kernels, these stacked registers can be saved by simply
  32.    creating a new register frame, or in other words by moving the BSP.
  33.    But the HP/UX kernel does not allow this.  So we rely on this code
  34.    instead, that makes functions calls whose only purpose is to create
  35.    new register frames.

  36.    The array below is the result obtained after assembling the code
  37.    shown below. It's an array of bytes in order to make it independent
  38.    of the host endianess, in case it ends up being used on more than
  39.    one target.

  40.    start:
  41.         // Save b0 before using it (into preserved reg: r4).
  42.         mov r4 = b0
  43.         ;;

  44.         br.call.dptk.few b0 = stub#
  45.         ;;

  46.         // Add a nop bundle where we can insert our dummy breakpoint.
  47.         nop.m 0
  48.         nop.i 0
  49.         nop.i 0
  50.         ;;

  51.    stub:
  52.         // Alloc a new register stack frame.  Here, we set the size
  53.         // of all regions to zero.  Eventually, GDB will manually
  54.         // change the instruction to set the size of the local region
  55.         // to match size of the output region of the function from
  56.         // which we are making the function call.  This is to protect
  57.         // the value of the output registers of the function from
  58.         // which we are making the call.
  59.         alloc r6 = ar.pfs, 0, 0, 0, 0

  60.         // Save b0 before using it again (into preserved reg: r5).
  61.         mov r5 = b0
  62.         ;;

  63.         //  Now that we have protected the entire output region of the
  64.         //  register stack frame, we can call our function that will
  65.         //  setup the arguments, and call our target function.
  66.         br.call.dptk.few b0 = call_dummy#
  67.         ;;

  68.         //  Restore b0, ar.pfs, and return
  69.         mov b0 = r5
  70.         mov.i ar.pfs = r6
  71.         ;;
  72.         br.ret.dptk.few b0
  73.         ;;

  74.    call_dummy:
  75.         //  Alloc a new frame, with 2 local registers, and 8 output registers
  76.         //  (8 output registers for the maximum of 8 slots passed by register).
  77.         alloc r32 = ar.pfs, 2, 0, 8, 0

  78.         //  Save b0 before using it to call our target function.
  79.         mov r33 = b0

  80.         // Load the argument values placed by GDB inside r14-r21 in their
  81.         // proper registers.
  82.         or r34 = r14, r0
  83.         or r35 = r15, r0
  84.         or r36 = r16, r0
  85.         or r37 = r17, r0
  86.         or r38 = r18, r0
  87.         or r39 = r19, r0
  88.         or r40 = r20, r0
  89.         or r41 = r21, r0
  90.         ;;

  91.         // actual call
  92.         br.call.dptk.few b0 = b1
  93.         ;;

  94.         mov.i ar.pfs=r32
  95.         mov b0=r33
  96.         ;;

  97.         br.ret.dptk.few b0
  98.         ;;

  99. */

  100. static const gdb_byte ia64_hpux_dummy_code[] =
  101. {
  102.   0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x40, 0x00,
  103.   0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  104.   0x1d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  105.   0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x52,
  106.   0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  107.   0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  108.   0x02, 0x30, 0x00, 0x00, 0x80, 0x05, 0x50, 0x00,
  109.   0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  110.   0x1d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  111.   0x00, 0x02, 0x00, 0x00, 0x30, 0x00, 0x00, 0x52,
  112.   0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x28,
  113.   0x04, 0x80, 0x03, 0x00, 0x60, 0x00, 0xaa, 0x00,
  114.   0x1d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  115.   0x00, 0x02, 0x00, 0x80, 0x00, 0x00, 0x84, 0x02,
  116.   0x00, 0x00, 0x29, 0x04, 0x80, 0x05, 0x10, 0x02,
  117.   0x00, 0x62, 0x00, 0x40, 0xe4, 0x00, 0x38, 0x80,
  118.   0x00, 0x18, 0x3d, 0x00, 0x0e, 0x20, 0x40, 0x82,
  119.   0x00, 0x1c, 0x40, 0xa0, 0x14, 0x01, 0x38, 0x80,
  120.   0x00, 0x30, 0x49, 0x00, 0x0e, 0x20, 0x70, 0x9a,
  121.   0x00, 0x1c, 0x40, 0x00, 0x45, 0x01, 0x38, 0x80,
  122.   0x0a, 0x48, 0x55, 0x00, 0x0e, 0x20, 0x00, 0x00,
  123.   0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  124.   0x1d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  125.   0x00, 0x02, 0x00, 0x00, 0x10, 0x00, 0x80, 0x12,
  126.   0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  127.   0x01, 0x55, 0x00, 0x00, 0x10, 0x0a, 0x00, 0x07,
  128.   0x1d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  129.   0x00, 0x02, 0x00, 0x80, 0x00, 0x00, 0x84, 0x02
  130. };

  131. /* The offset to be used in order to get the __reason pseudo-register
  132.    when using one of the *UREGS ttrace requests (see system header file
  133.    /usr/include/ia64/sys/uregs.h for more details).

  134.    The documentation for this pseudo-register says that a nonzero value
  135.    indicates that the thread stopped due to a fault, trap, or interrupt.
  136.    A null value indicates a stop inside a syscall.  */
  137. #define IA64_HPUX_UREG_REASON 0x00070000

  138. /* Return nonzero if the value of the register identified by REGNUM
  139.    can be modified.  */

  140. static int
  141. ia64_hpux_can_store_ar_register (int regnum)
  142. {
  143.   switch (regnum)
  144.     {
  145.       case IA64_RSC_REGNUM:
  146.       case IA64_RNAT_REGNUM:
  147.       case IA64_CSD_REGNUM:
  148.       case IA64_SSD_REGNUM:
  149.       case IA64_CCV_REGNUM:
  150.       case IA64_UNAT_REGNUM:
  151.       case IA64_FPSR_REGNUM:
  152.       case IA64_PFS_REGNUM:
  153.       case IA64_LC_REGNUM:
  154.       case IA64_EC_REGNUM:
  155.          return 1;
  156.          break;

  157.       default:
  158.          return 0;
  159.          break;
  160.     }
  161. }

  162. /* The "cannot_store_register" target_ops method.  */

  163. static int
  164. ia64_hpux_cannot_store_register (struct gdbarch *gdbarch, int regnum)
  165. {
  166.   /* General registers.  */

  167.   if (regnum == IA64_GR0_REGNUM)
  168.     return 1;

  169.   /* FP register.  */

  170.   if (regnum == IA64_FR0_REGNUM || regnum == IA64_FR1_REGNUM)
  171.     return 1;

  172.   /* Application registers.  */
  173.   if (regnum >= IA64_AR0_REGNUM && regnum <= IA64_AR0_REGNUM + 127)
  174.     return (!ia64_hpux_can_store_ar_register (regnum));

  175.   /* We can store all other registers.  */
  176.   return 0;
  177. }

  178. /* Return nonzero if the inferior is stopped inside a system call.  */

  179. static int
  180. ia64_hpux_stopped_in_syscall (struct gdbarch *gdbarch)
  181. {
  182.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  183.   struct target_ops *ops = &current_target;
  184.   gdb_byte buf[8];
  185.   int len;

  186.   len = target_read (ops, TARGET_OBJECT_HPUX_UREGS, NULL,
  187.                      buf, IA64_HPUX_UREG_REASON, sizeof (buf));
  188.   if (len == -1)
  189.     /* The target wasn't able to tell us.  Assume we are not stopped
  190.        in a system call, which is the normal situation.  */
  191.     return 0;
  192.   gdb_assert (len == 8);

  193.   return (extract_unsigned_integer (buf, len, byte_order) == 0);
  194. }

  195. /* The "size_of_register_frame" gdbarch_tdep routine for ia64-hpux.  */

  196. static int
  197. ia64_hpux_size_of_register_frame (struct frame_info *this_frame,
  198.                                   ULONGEST cfm)
  199. {
  200.   int sof;

  201.   if (frame_relative_level (this_frame) == 0
  202.       && ia64_hpux_stopped_in_syscall (get_frame_arch (this_frame)))
  203.     /* If the inferior stopped in a system call, the base address
  204.        of the register frame is at BSP - SOL instead of BSP - SOF.
  205.        This is an HP-UX exception.  */
  206.     sof = (cfm & 0x3f80) >> 7;
  207.   else
  208.     sof = (cfm & 0x7f);

  209.   return sof;
  210. }

  211. /* Implement the push_dummy_code gdbarch method.

  212.    This function assumes that the SP is already 16-byte-aligned.  */

  213. static CORE_ADDR
  214. ia64_hpux_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
  215.                            CORE_ADDR funaddr, struct value **args, int nargs,
  216.                            struct type *value_type, CORE_ADDR *real_pc,
  217.                            CORE_ADDR *bp_addr, struct regcache *regcache)
  218. {
  219.   ULONGEST cfm;
  220.   int sof, sol, sor, soo;
  221.   gdb_byte buf[16];

  222.   regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
  223.   sof = cfm & 0x7f;
  224.   sol = (cfm >> 7) & 0x7f;
  225.   sor = (cfm >> 14) & 0xf;
  226.   soo = sof - sol - sor;

  227.   /* Reserve some space on the stack to hold the dummy code.  */
  228.   sp = sp - sizeof (ia64_hpux_dummy_code);

  229.   /* Set the breakpoint address at the first instruction of the bundle
  230.      in the dummy code that has only nops.  This is where the dummy code
  231.      expects us to break.  */
  232.   *bp_addr = sp + 0x20;

  233.   /* Start the inferior function call from the dummy code.  The dummy
  234.      code will then call our function.  */
  235.   *real_pc = sp;

  236.   /* Transfer the dummy code to the inferior.  */
  237.   write_memory (sp, ia64_hpux_dummy_code, sizeof (ia64_hpux_dummy_code));

  238.   /* Update the size of the local portion of the register frame allocated
  239.      by ``stub'' to match the size of the output region of the current
  240.      register frame.  This allows us to save the stacked registers.

  241.      The "alloc" instruction is located at slot 0 of the bundle at +0x30.
  242.      Update the "sof" and "sol" portion of that instruction which are
  243.      respectively at bits 18-24 and 25-31 of the bundle.  */
  244.   memcpy (buf, ia64_hpux_dummy_code + 0x30, sizeof (buf));

  245.   buf[2] |= ((soo & 0x3f) << 2);
  246.   buf[3] |= (soo << 1);
  247.   if (soo > 63)
  248.     buf[3] |= 1;

  249.   write_memory (sp + 0x30, buf, sizeof (buf));

  250.   /* Return the new (already properly aligned) SP.  */
  251.   return sp;
  252. }

  253. /* The "allocate_new_rse_frame" ia64_infcall_ops routine for ia64-hpux.  */

  254. static void
  255. ia64_hpux_allocate_new_rse_frame (struct regcache *regcache, ULONGEST bsp,
  256.                                   int sof)
  257. {
  258.   /* We cannot change the value of the BSP register on HP-UX,
  259.      so we can't allocate a new RSE frame.  */
  260. }

  261. /* The "store_argument_in_slot" ia64_infcall_ops routine for ia64-hpux.  */

  262. static void
  263. ia64_hpux_store_argument_in_slot (struct regcache *regcache, CORE_ADDR bsp,
  264.                                   int slotnum, gdb_byte *buf)
  265. {
  266.   /* The call sequence on this target expects us to place the arguments
  267.      inside r14 - r21.  */
  268.   regcache_cooked_write (regcache, IA64_GR0_REGNUM + 14 + slotnum, buf);
  269. }

  270. /* The "set_function_addr" ia64_infcall_ops routine for ia64-hpux.  */

  271. static void
  272. ia64_hpux_set_function_addr (struct regcache *regcache, CORE_ADDR func_addr)
  273. {
  274.   /* The calling sequence calls the function whose address is placed
  275.      in register b1.  */
  276.   regcache_cooked_write_unsigned (regcache, IA64_BR1_REGNUM, func_addr);
  277. }

  278. /* The ia64_infcall_ops structure for ia64-hpux.  */

  279. static const struct ia64_infcall_ops ia64_hpux_infcall_ops =
  280. {
  281.   ia64_hpux_allocate_new_rse_frame,
  282.   ia64_hpux_store_argument_in_slot,
  283.   ia64_hpux_set_function_addr
  284. };

  285. /* The "dummy_id" gdbarch routine for ia64-hpux.  */

  286. static struct frame_id
  287. ia64_hpux_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  288. {
  289.   CORE_ADDR sp, pc, bp_addr, bsp;

  290.   sp = get_frame_register_unsigned (this_frame, IA64_GR12_REGNUM);

  291.   /* Just double-check that the frame PC is within a certain region
  292.      of the stack that would be plausible for our dummy code (the dummy
  293.      code was pushed at SP + 16).  If not, then return a null frame ID.
  294.      This is necessary in our case, because it is possible to produce
  295.      the same frame ID for a normal frame, if that frame corresponds
  296.      to the function called by our dummy code, and the function has not
  297.      modified the registers that we use to build the dummy frame ID.  */
  298.   pc = get_frame_pc (this_frame);
  299.   if (pc < sp + 16 || pc >= sp + 16 + sizeof (ia64_hpux_dummy_code))
  300.     return null_frame_id;

  301.   /* The call sequence is such that the address of the dummy breakpoint
  302.      we inserted is stored in r5.  */
  303.   bp_addr = get_frame_register_unsigned (this_frame, IA64_GR5_REGNUM);

  304.   bsp = get_frame_register_unsigned (this_frame, IA64_BSP_REGNUM);

  305.   return frame_id_build_special (sp, bp_addr, bsp);
  306. }

  307. /* Should be set to non-NULL if the ia64-hpux solib module is linked in.
  308.    This may not be the case because the shared library support code can
  309.    only be compiled on ia64-hpux.  */

  310. struct target_so_ops *ia64_hpux_so_ops = NULL;

  311. /* The "find_global_pointer_from_solib" gdbarch_tdep routine for
  312.    ia64-hpux.  */

  313. static CORE_ADDR
  314. ia64_hpux_find_global_pointer_from_solib (struct gdbarch *gdbarch,
  315.                                           CORE_ADDR faddr)
  316. {
  317.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  318.   struct target_ops *ops = &current_target;
  319.   gdb_byte buf[8];
  320.   LONGEST len;

  321.   len = target_read (ops, TARGET_OBJECT_HPUX_SOLIB_GOT,
  322.                      paddress (gdbarch, faddr), buf, 0, sizeof (buf));

  323.   return extract_unsigned_integer (buf, len, byte_order);
  324. }

  325. static void
  326. ia64_hpux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  327. {
  328.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  329.   tdep->size_of_register_frame = ia64_hpux_size_of_register_frame;

  330.   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
  331.   set_gdbarch_cannot_store_register (gdbarch, ia64_hpux_cannot_store_register);

  332.   /* Inferior functions must be called from stack. */
  333.   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  334.   set_gdbarch_push_dummy_code (gdbarch, ia64_hpux_push_dummy_code);
  335.   tdep->infcall_ops = ia64_hpux_infcall_ops;
  336.   tdep->find_global_pointer_from_solib
  337.       = ia64_hpux_find_global_pointer_from_solib;
  338.   set_gdbarch_dummy_id (gdbarch, ia64_hpux_dummy_id);

  339.   if (ia64_hpux_so_ops)
  340.     set_solib_ops (gdbarch, ia64_hpux_so_ops);
  341. }

  342. /* Provide a prototype to silence -Wmissing-prototypes.  */
  343. extern initialize_file_ftype _initialize_ia64_hpux_tdep;

  344. void
  345. _initialize_ia64_hpux_tdep (void)
  346. {
  347.   gdbarch_register_osabi (bfd_arch_ia64, 0, GDB_OSABI_HPUX_ELF,
  348.                           ia64_hpux_init_abi);
  349. }