gdb/ia64-linux-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Functions specific to running gdb native on IA-64 running
  2.    GNU/Linux.

  3.    Copyright (C) 1999-2015 Free Software Foundation, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "inferior.h"
  17. #include "target.h"
  18. #include "gdbcore.h"
  19. #include "regcache.h"
  20. #include "ia64-tdep.h"
  21. #include "linux-nat.h"

  22. #include <signal.h>
  23. #include <sys/ptrace.h>
  24. #include "gdb_wait.h"
  25. #ifdef HAVE_SYS_REG_H
  26. #include <sys/reg.h>
  27. #endif
  28. #include <sys/syscall.h>
  29. #include <sys/user.h>

  30. #include <asm/ptrace_offsets.h>
  31. #include <sys/procfs.h>

  32. /* Prototypes for supply_gregset etc.  */
  33. #include "gregset.h"

  34. /* These must match the order of the register names.

  35.    Some sort of lookup table is needed because the offsets associated
  36.    with the registers are all over the board.  */

  37. static int u_offsets[] =
  38.   {
  39.     /* general registers */
  40.     -1,                /* gr0 not available; i.e, it's always zero.  */
  41.     PT_R1,
  42.     PT_R2,
  43.     PT_R3,
  44.     PT_R4,
  45.     PT_R5,
  46.     PT_R6,
  47.     PT_R7,
  48.     PT_R8,
  49.     PT_R9,
  50.     PT_R10,
  51.     PT_R11,
  52.     PT_R12,
  53.     PT_R13,
  54.     PT_R14,
  55.     PT_R15,
  56.     PT_R16,
  57.     PT_R17,
  58.     PT_R18,
  59.     PT_R19,
  60.     PT_R20,
  61.     PT_R21,
  62.     PT_R22,
  63.     PT_R23,
  64.     PT_R24,
  65.     PT_R25,
  66.     PT_R26,
  67.     PT_R27,
  68.     PT_R28,
  69.     PT_R29,
  70.     PT_R30,
  71.     PT_R31,
  72.     /* gr32 through gr127 not directly available via the ptrace interface.  */
  73.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  74.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  75.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  76.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  77.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  79.     /* Floating point registers */
  80.     -1, -1,        /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0).  */
  81.     PT_F2,
  82.     PT_F3,
  83.     PT_F4,
  84.     PT_F5,
  85.     PT_F6,
  86.     PT_F7,
  87.     PT_F8,
  88.     PT_F9,
  89.     PT_F10,
  90.     PT_F11,
  91.     PT_F12,
  92.     PT_F13,
  93.     PT_F14,
  94.     PT_F15,
  95.     PT_F16,
  96.     PT_F17,
  97.     PT_F18,
  98.     PT_F19,
  99.     PT_F20,
  100.     PT_F21,
  101.     PT_F22,
  102.     PT_F23,
  103.     PT_F24,
  104.     PT_F25,
  105.     PT_F26,
  106.     PT_F27,
  107.     PT_F28,
  108.     PT_F29,
  109.     PT_F30,
  110.     PT_F31,
  111.     PT_F32,
  112.     PT_F33,
  113.     PT_F34,
  114.     PT_F35,
  115.     PT_F36,
  116.     PT_F37,
  117.     PT_F38,
  118.     PT_F39,
  119.     PT_F40,
  120.     PT_F41,
  121.     PT_F42,
  122.     PT_F43,
  123.     PT_F44,
  124.     PT_F45,
  125.     PT_F46,
  126.     PT_F47,
  127.     PT_F48,
  128.     PT_F49,
  129.     PT_F50,
  130.     PT_F51,
  131.     PT_F52,
  132.     PT_F53,
  133.     PT_F54,
  134.     PT_F55,
  135.     PT_F56,
  136.     PT_F57,
  137.     PT_F58,
  138.     PT_F59,
  139.     PT_F60,
  140.     PT_F61,
  141.     PT_F62,
  142.     PT_F63,
  143.     PT_F64,
  144.     PT_F65,
  145.     PT_F66,
  146.     PT_F67,
  147.     PT_F68,
  148.     PT_F69,
  149.     PT_F70,
  150.     PT_F71,
  151.     PT_F72,
  152.     PT_F73,
  153.     PT_F74,
  154.     PT_F75,
  155.     PT_F76,
  156.     PT_F77,
  157.     PT_F78,
  158.     PT_F79,
  159.     PT_F80,
  160.     PT_F81,
  161.     PT_F82,
  162.     PT_F83,
  163.     PT_F84,
  164.     PT_F85,
  165.     PT_F86,
  166.     PT_F87,
  167.     PT_F88,
  168.     PT_F89,
  169.     PT_F90,
  170.     PT_F91,
  171.     PT_F92,
  172.     PT_F93,
  173.     PT_F94,
  174.     PT_F95,
  175.     PT_F96,
  176.     PT_F97,
  177.     PT_F98,
  178.     PT_F99,
  179.     PT_F100,
  180.     PT_F101,
  181.     PT_F102,
  182.     PT_F103,
  183.     PT_F104,
  184.     PT_F105,
  185.     PT_F106,
  186.     PT_F107,
  187.     PT_F108,
  188.     PT_F109,
  189.     PT_F110,
  190.     PT_F111,
  191.     PT_F112,
  192.     PT_F113,
  193.     PT_F114,
  194.     PT_F115,
  195.     PT_F116,
  196.     PT_F117,
  197.     PT_F118,
  198.     PT_F119,
  199.     PT_F120,
  200.     PT_F121,
  201.     PT_F122,
  202.     PT_F123,
  203.     PT_F124,
  204.     PT_F125,
  205.     PT_F126,
  206.     PT_F127,
  207.     /* Predicate registers - we don't fetch these individually.  */
  208.     -1, -1, -1, -1, -1, -1, -1, -1,
  209.     -1, -1, -1, -1, -1, -1, -1, -1,
  210.     -1, -1, -1, -1, -1, -1, -1, -1,
  211.     -1, -1, -1, -1, -1, -1, -1, -1,
  212.     -1, -1, -1, -1, -1, -1, -1, -1,
  213.     -1, -1, -1, -1, -1, -1, -1, -1,
  214.     -1, -1, -1, -1, -1, -1, -1, -1,
  215.     -1, -1, -1, -1, -1, -1, -1, -1,
  216.     /* branch registers */
  217.     PT_B0,
  218.     PT_B1,
  219.     PT_B2,
  220.     PT_B3,
  221.     PT_B4,
  222.     PT_B5,
  223.     PT_B6,
  224.     PT_B7,
  225.     /* Virtual frame pointer and virtual return address pointer.  */
  226.     -1, -1,
  227.     /* other registers */
  228.     PT_PR,
  229.     PT_CR_IIP,        /* ip */
  230.     PT_CR_IPSR, /* psr */
  231.     PT_CFM,        /* cfm */
  232.     /* kernel registers not visible via ptrace interface (?)  */
  233.     -1, -1, -1, -1, -1, -1, -1, -1,
  234.     /* hole */
  235.     -1, -1, -1, -1, -1, -1, -1, -1,
  236.     PT_AR_RSC,
  237.     PT_AR_BSP,
  238.     PT_AR_BSPSTORE,
  239.     PT_AR_RNAT,
  240.     -1,
  241.     -1,                /* Not available: FCR, IA32 floating control register.  */
  242.     -1, -1,
  243.     -1,                /* Not available: EFLAG */
  244.     -1,                /* Not available: CSD */
  245.     -1,                /* Not available: SSD */
  246.     -1,                /* Not available: CFLG */
  247.     -1,                /* Not available: FSR */
  248.     -1,                /* Not available: FIR */
  249.     -1,                /* Not available: FDR */
  250.     -1,
  251.     PT_AR_CCV,
  252.     -1, -1, -1,
  253.     PT_AR_UNAT,
  254.     -1, -1, -1,
  255.     PT_AR_FPSR,
  256.     -1, -1, -1,
  257.     -1,                /* Not available: ITC */
  258.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  259.     -1, -1, -1, -1, -1, -1, -1, -1, -1,
  260.     PT_AR_PFS,
  261.     PT_AR_LC,
  262.     PT_AR_EC,
  263.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  264.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  265.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  266.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  267.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  268.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  269.     -1,
  270.     /* nat bits - not fetched directly; instead we obtain these bits from
  271.        either rnat or unat or from memory.  */
  272.     -1, -1, -1, -1, -1, -1, -1, -1,
  273.     -1, -1, -1, -1, -1, -1, -1, -1,
  274.     -1, -1, -1, -1, -1, -1, -1, -1,
  275.     -1, -1, -1, -1, -1, -1, -1, -1,
  276.     -1, -1, -1, -1, -1, -1, -1, -1,
  277.     -1, -1, -1, -1, -1, -1, -1, -1,
  278.     -1, -1, -1, -1, -1, -1, -1, -1,
  279.     -1, -1, -1, -1, -1, -1, -1, -1,
  280.     -1, -1, -1, -1, -1, -1, -1, -1,
  281.     -1, -1, -1, -1, -1, -1, -1, -1,
  282.     -1, -1, -1, -1, -1, -1, -1, -1,
  283.     -1, -1, -1, -1, -1, -1, -1, -1,
  284.     -1, -1, -1, -1, -1, -1, -1, -1,
  285.     -1, -1, -1, -1, -1, -1, -1, -1,
  286.     -1, -1, -1, -1, -1, -1, -1, -1,
  287.     -1, -1, -1, -1, -1, -1, -1, -1,
  288.   };

  289. static CORE_ADDR
  290. ia64_register_addr (struct gdbarch *gdbarch, int regno)
  291. {
  292.   CORE_ADDR addr;

  293.   if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  294.     error (_("Invalid register number %d."), regno);

  295.   if (u_offsets[regno] == -1)
  296.     addr = 0;
  297.   else
  298.     addr = (CORE_ADDR) u_offsets[regno];

  299.   return addr;
  300. }

  301. static int
  302. ia64_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
  303. {
  304.   return regno < 0
  305.          || regno >= gdbarch_num_regs (gdbarch)
  306.          || u_offsets[regno] == -1;
  307. }

  308. static int
  309. ia64_cannot_store_register (struct gdbarch *gdbarch, int regno)
  310. {
  311.   /* Rationale behind not permitting stores to bspstore...

  312.      The IA-64 architecture provides bspstore and bsp which refer
  313.      memory locations in the RSE's backing store.  bspstore is the
  314.      next location which will be written when the RSE needs to write
  315.      to memory.  bsp is the address at which r32 in the current frame
  316.      would be found if it were written to the backing store.

  317.      The IA-64 architecture provides read-only access to bsp and
  318.      read/write access to bspstore (but only when the RSE is in
  319.      the enforced lazy mode).  It should be noted that stores
  320.      to bspstore also affect the value of bsp.  Changing bspstore
  321.      does not affect the number of dirty entries between bspstore
  322.      and bsp, so changing bspstore by N words will also cause bsp
  323.      to be changed by (roughly) N as well.  (It could be N-1 or N+1
  324.      depending upon where the NaT collection bits fall.)

  325.      OTOH, the Linux kernel provides read/write access to bsp (and
  326.      currently read/write access to bspstore as well).  But it
  327.      is definitely the case that if you change one, the other
  328.      will change at the same time.  It is more useful to gdb to
  329.      be able to change bsp.  So in order to prevent strange and
  330.      undesirable things from happening when a dummy stack frame
  331.      is popped (after calling an inferior function), we allow
  332.      bspstore to be read, but not written.  (Note that popping
  333.      a (generic) dummy stack frame causes all registers that
  334.      were previously read from the inferior process to be written
  335.      back.)  */

  336.   return regno < 0
  337.          || regno >= gdbarch_num_regs (gdbarch)
  338.          || u_offsets[regno] == -1
  339.          || regno == IA64_BSPSTORE_REGNUM;
  340. }

  341. void
  342. supply_gregset (struct regcache *regcache, const gregset_t *gregsetp)
  343. {
  344.   int regi;
  345.   const greg_t *regp = (const greg_t *) gregsetp;

  346.   for (regi = IA64_GR0_REGNUM; regi <= IA64_GR31_REGNUM; regi++)
  347.     {
  348.       regcache_raw_supply (regcache, regi, regp + (regi - IA64_GR0_REGNUM));
  349.     }

  350.   /* FIXME: NAT collection bits are at index 32; gotta deal with these
  351.      somehow...  */

  352.   regcache_raw_supply (regcache, IA64_PR_REGNUM, regp + 33);

  353.   for (regi = IA64_BR0_REGNUM; regi <= IA64_BR7_REGNUM; regi++)
  354.     {
  355.       regcache_raw_supply (regcache, regi,
  356.                            regp + 34 + (regi - IA64_BR0_REGNUM));
  357.     }

  358.   regcache_raw_supply (regcache, IA64_IP_REGNUM, regp + 42);
  359.   regcache_raw_supply (regcache, IA64_CFM_REGNUM, regp + 43);
  360.   regcache_raw_supply (regcache, IA64_PSR_REGNUM, regp + 44);
  361.   regcache_raw_supply (regcache, IA64_RSC_REGNUM, regp + 45);
  362.   regcache_raw_supply (regcache, IA64_BSP_REGNUM, regp + 46);
  363.   regcache_raw_supply (regcache, IA64_BSPSTORE_REGNUM, regp + 47);
  364.   regcache_raw_supply (regcache, IA64_RNAT_REGNUM, regp + 48);
  365.   regcache_raw_supply (regcache, IA64_CCV_REGNUM, regp + 49);
  366.   regcache_raw_supply (regcache, IA64_UNAT_REGNUM, regp + 50);
  367.   regcache_raw_supply (regcache, IA64_FPSR_REGNUM, regp + 51);
  368.   regcache_raw_supply (regcache, IA64_PFS_REGNUM, regp + 52);
  369.   regcache_raw_supply (regcache, IA64_LC_REGNUM, regp + 53);
  370.   regcache_raw_supply (regcache, IA64_EC_REGNUM, regp + 54);
  371. }

  372. void
  373. fill_gregset (const struct regcache *regcache, gregset_t *gregsetp, int regno)
  374. {
  375.   int regi;
  376.   greg_t *regp = (greg_t *) gregsetp;

  377. #define COPY_REG(_idx_,_regi_) \
  378.   if ((regno == -1) || regno == _regi_) \
  379.     regcache_raw_collect (regcache, _regi_, regp + _idx_)

  380.   for (regi = IA64_GR0_REGNUM; regi <= IA64_GR31_REGNUM; regi++)
  381.     {
  382.       COPY_REG (regi - IA64_GR0_REGNUM, regi);
  383.     }

  384.   /* FIXME: NAT collection bits at index 32?  */

  385.   COPY_REG (33, IA64_PR_REGNUM);

  386.   for (regi = IA64_BR0_REGNUM; regi <= IA64_BR7_REGNUM; regi++)
  387.     {
  388.       COPY_REG (34 + (regi - IA64_BR0_REGNUM), regi);
  389.     }

  390.   COPY_REG (42, IA64_IP_REGNUM);
  391.   COPY_REG (43, IA64_CFM_REGNUM);
  392.   COPY_REG (44, IA64_PSR_REGNUM);
  393.   COPY_REG (45, IA64_RSC_REGNUM);
  394.   COPY_REG (46, IA64_BSP_REGNUM);
  395.   COPY_REG (47, IA64_BSPSTORE_REGNUM);
  396.   COPY_REG (48, IA64_RNAT_REGNUM);
  397.   COPY_REG (49, IA64_CCV_REGNUM);
  398.   COPY_REG (50, IA64_UNAT_REGNUM);
  399.   COPY_REG (51, IA64_FPSR_REGNUM);
  400.   COPY_REG (52, IA64_PFS_REGNUM);
  401.   COPY_REG (53, IA64_LC_REGNUM);
  402.   COPY_REG (54, IA64_EC_REGNUM);
  403. }

  404. /*  Given a pointer to a floating point register set in /proc format
  405.    (fpregset_t *), unpack the register contents and supply them as gdb's
  406.    idea of the current floating point register values.  */

  407. void
  408. supply_fpregset (struct regcache *regcache, const fpregset_t *fpregsetp)
  409. {
  410.   int regi;
  411.   const char *from;
  412.   const gdb_byte f_zero[16] = { 0 };
  413.   const gdb_byte f_one[16] =
  414.     { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0 };

  415.   /* Kernel generated cores have fr1==0 instead of 1.0.  Older GDBs
  416.      did the same.  So ignore whatever might be recorded in fpregset_t
  417.      for fr0/fr1 and always supply their expected values.  */

  418.   /* fr0 is always read as zero.  */
  419.   regcache_raw_supply (regcache, IA64_FR0_REGNUM, f_zero);
  420.   /* fr1 is always read as one (1.0).  */
  421.   regcache_raw_supply (regcache, IA64_FR1_REGNUM, f_one);

  422.   for (regi = IA64_FR2_REGNUM; regi <= IA64_FR127_REGNUM; regi++)
  423.     {
  424.       from = (const char *) &((*fpregsetp)[regi - IA64_FR0_REGNUM]);
  425.       regcache_raw_supply (regcache, regi, from);
  426.     }
  427. }

  428. /*  Given a pointer to a floating point register set in /proc format
  429.    (fpregset_t *), update the register specified by REGNO from gdb's idea
  430.    of the current floating point register set.  If REGNO is -1, update
  431.    them all.  */

  432. void
  433. fill_fpregset (const struct regcache *regcache,
  434.                fpregset_t *fpregsetp, int regno)
  435. {
  436.   int regi;

  437.   for (regi = IA64_FR0_REGNUM; regi <= IA64_FR127_REGNUM; regi++)
  438.     {
  439.       if ((regno == -1) || (regno == regi))
  440.         regcache_raw_collect (regcache, regi,
  441.                               &((*fpregsetp)[regi - IA64_FR0_REGNUM]));
  442.     }
  443. }

  444. #define IA64_PSR_DB (1UL << 24)
  445. #define IA64_PSR_DD (1UL << 39)

  446. static void
  447. enable_watchpoints_in_psr (ptid_t ptid)
  448. {
  449.   struct regcache *regcache = get_thread_regcache (ptid);
  450.   ULONGEST psr;

  451.   regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr);
  452.   if (!(psr & IA64_PSR_DB))
  453.     {
  454.       psr |= IA64_PSR_DB;        /* Set the db bit - this enables hardware
  455.                                    watchpoints and breakpoints.  */
  456.       regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr);
  457.     }
  458. }

  459. static long debug_registers[8];

  460. static void
  461. store_debug_register (ptid_t ptid, int idx, long val)
  462. {
  463.   int tid;

  464.   tid = ptid_get_lwp (ptid);
  465.   if (tid == 0)
  466.     tid = ptid_get_pid (ptid);

  467.   (void) ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) (PT_DBR + 8 * idx), val);
  468. }

  469. static void
  470. store_debug_register_pair (ptid_t ptid, int idx, long *dbr_addr,
  471.                            long *dbr_mask)
  472. {
  473.   if (dbr_addr)
  474.     store_debug_register (ptid, 2 * idx, *dbr_addr);
  475.   if (dbr_mask)
  476.     store_debug_register (ptid, 2 * idx + 1, *dbr_mask);
  477. }

  478. static int
  479. is_power_of_2 (int val)
  480. {
  481.   int i, onecount;

  482.   onecount = 0;
  483.   for (i = 0; i < 8 * sizeof (val); i++)
  484.     if (val & (1 << i))
  485.       onecount++;

  486.   return onecount <= 1;
  487. }

  488. static int
  489. ia64_linux_insert_watchpoint (struct target_ops *self,
  490.                               CORE_ADDR addr, int len, int rw,
  491.                               struct expression *cond)
  492. {
  493.   struct lwp_info *lp;
  494.   int idx;
  495.   long dbr_addr, dbr_mask;
  496.   int max_watchpoints = 4;

  497.   if (len <= 0 || !is_power_of_2 (len))
  498.     return -1;

  499.   for (idx = 0; idx < max_watchpoints; idx++)
  500.     {
  501.       dbr_mask = debug_registers[idx * 2 + 1];
  502.       if ((dbr_mask & (0x3UL << 62)) == 0)
  503.         {
  504.           /* Exit loop if both r and w bits clear.  */
  505.           break;
  506.         }
  507.     }

  508.   if (idx == max_watchpoints)
  509.     return -1;

  510.   dbr_addr = (long) addr;
  511.   dbr_mask = (~(len - 1) & 0x00ffffffffffffffL);  /* construct mask to match */
  512.   dbr_mask |= 0x0800000000000000L;           /* Only match privilege level 3 */
  513.   switch (rw)
  514.     {
  515.     case hw_write:
  516.       dbr_mask |= (1L << 62);                        /* Set w bit */
  517.       break;
  518.     case hw_read:
  519.       dbr_mask |= (1L << 63);                        /* Set r bit */
  520.       break;
  521.     case hw_access:
  522.       dbr_mask |= (3L << 62);                        /* Set both r and w bits */
  523.       break;
  524.     default:
  525.       return -1;
  526.     }

  527.   debug_registers[2 * idx] = dbr_addr;
  528.   debug_registers[2 * idx + 1] = dbr_mask;
  529.   ALL_LWPS (lp)
  530.     {
  531.       store_debug_register_pair (lp->ptid, idx, &dbr_addr, &dbr_mask);
  532.       enable_watchpoints_in_psr (lp->ptid);
  533.     }

  534.   return 0;
  535. }

  536. static int
  537. ia64_linux_remove_watchpoint (struct target_ops *self,
  538.                               CORE_ADDR addr, int len, int type,
  539.                               struct expression *cond)
  540. {
  541.   int idx;
  542.   long dbr_addr, dbr_mask;
  543.   int max_watchpoints = 4;

  544.   if (len <= 0 || !is_power_of_2 (len))
  545.     return -1;

  546.   for (idx = 0; idx < max_watchpoints; idx++)
  547.     {
  548.       dbr_addr = debug_registers[2 * idx];
  549.       dbr_mask = debug_registers[2 * idx + 1];
  550.       if ((dbr_mask & (0x3UL << 62)) && addr == (CORE_ADDR) dbr_addr)
  551.         {
  552.           struct lwp_info *lp;

  553.           debug_registers[2 * idx] = 0;
  554.           debug_registers[2 * idx + 1] = 0;
  555.           dbr_addr = 0;
  556.           dbr_mask = 0;

  557.           ALL_LWPS (lp)
  558.             store_debug_register_pair (lp->ptid, idx, &dbr_addr, &dbr_mask);

  559.           return 0;
  560.         }
  561.     }
  562.   return -1;
  563. }

  564. static void
  565. ia64_linux_new_thread (struct lwp_info *lp)
  566. {
  567.   int i, any;

  568.   any = 0;
  569.   for (i = 0; i < 8; i++)
  570.     {
  571.       if (debug_registers[i] != 0)
  572.         any = 1;
  573.       store_debug_register (lp->ptid, i, debug_registers[i]);
  574.     }

  575.   if (any)
  576.     enable_watchpoints_in_psr (lp->ptid);
  577. }

  578. static int
  579. ia64_linux_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
  580. {
  581.   CORE_ADDR psr;
  582.   siginfo_t siginfo;
  583.   struct regcache *regcache = get_current_regcache ();

  584.   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  585.     return 0;

  586.   if (siginfo.si_signo != SIGTRAP
  587.       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  588.     return 0;

  589.   regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr);
  590.   psr |= IA64_PSR_DD;        /* Set the dd bit - this will disable the watchpoint
  591.                            for the next instruction.  */
  592.   regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr);

  593.   *addr_p = (CORE_ADDR) siginfo.si_addr;
  594.   return 1;
  595. }

  596. static int
  597. ia64_linux_stopped_by_watchpoint (struct target_ops *ops)
  598. {
  599.   CORE_ADDR addr;
  600.   return ia64_linux_stopped_data_address (ops, &addr);
  601. }

  602. static int
  603. ia64_linux_can_use_hw_breakpoint (struct target_ops *self,
  604.                                   int type, int cnt, int othertype)
  605. {
  606.   return 1;
  607. }


  608. /* Fetch register REGNUM from the inferior.  */

  609. static void
  610. ia64_linux_fetch_register (struct regcache *regcache, int regnum)
  611. {
  612.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  613.   CORE_ADDR addr;
  614.   size_t size;
  615.   PTRACE_TYPE_RET *buf;
  616.   int pid, i;

  617.   /* r0 cannot be fetched but is always zero.  */
  618.   if (regnum == IA64_GR0_REGNUM)
  619.     {
  620.       const gdb_byte zero[8] = { 0 };

  621.       gdb_assert (sizeof (zero) == register_size (gdbarch, regnum));
  622.       regcache_raw_supply (regcache, regnum, zero);
  623.       return;
  624.     }

  625.   /* fr0 cannot be fetched but is always zero.  */
  626.   if (regnum == IA64_FR0_REGNUM)
  627.     {
  628.       const gdb_byte f_zero[16] = { 0 };

  629.       gdb_assert (sizeof (f_zero) == register_size (gdbarch, regnum));
  630.       regcache_raw_supply (regcache, regnum, f_zero);
  631.       return;
  632.     }

  633.   /* fr1 cannot be fetched but is always one (1.0).  */
  634.   if (regnum == IA64_FR1_REGNUM)
  635.     {
  636.       const gdb_byte f_one[16] =
  637.         { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0 };

  638.       gdb_assert (sizeof (f_one) == register_size (gdbarch, regnum));
  639.       regcache_raw_supply (regcache, regnum, f_one);
  640.       return;
  641.     }

  642.   if (ia64_cannot_fetch_register (gdbarch, regnum))
  643.     {
  644.       regcache_raw_supply (regcache, regnum, NULL);
  645.       return;
  646.     }

  647.   /* Cater for systems like GNU/Linux, that implement threads as
  648.      separate processes.  */
  649.   pid = ptid_get_lwp (inferior_ptid);
  650.   if (pid == 0)
  651.     pid = ptid_get_pid (inferior_ptid);

  652.   /* This isn't really an address, but ptrace thinks of it as one.  */
  653.   addr = ia64_register_addr (gdbarch, regnum);
  654.   size = register_size (gdbarch, regnum);

  655.   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
  656.   buf = alloca (size);

  657.   /* Read the register contents from the inferior a chunk at a time.  */
  658.   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
  659.     {
  660.       errno = 0;
  661.       buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)addr, 0);
  662.       if (errno != 0)
  663.         error (_("Couldn't read register %s (#%d): %s."),
  664.                gdbarch_register_name (gdbarch, regnum),
  665.                regnum, safe_strerror (errno));

  666.       addr += sizeof (PTRACE_TYPE_RET);
  667.     }
  668.   regcache_raw_supply (regcache, regnum, buf);
  669. }

  670. /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
  671.    for all registers.  */

  672. static void
  673. ia64_linux_fetch_registers (struct target_ops *ops,
  674.                             struct regcache *regcache, int regnum)
  675. {
  676.   if (regnum == -1)
  677.     for (regnum = 0;
  678.          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
  679.          regnum++)
  680.       ia64_linux_fetch_register (regcache, regnum);
  681.   else
  682.     ia64_linux_fetch_register (regcache, regnum);
  683. }

  684. /* Store register REGNUM into the inferior.  */

  685. static void
  686. ia64_linux_store_register (const struct regcache *regcache, int regnum)
  687. {
  688.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  689.   CORE_ADDR addr;
  690.   size_t size;
  691.   PTRACE_TYPE_RET *buf;
  692.   int pid, i;

  693.   if (ia64_cannot_store_register (gdbarch, regnum))
  694.     return;

  695.   /* Cater for systems like GNU/Linux, that implement threads as
  696.      separate processes.  */
  697.   pid = ptid_get_lwp (inferior_ptid);
  698.   if (pid == 0)
  699.     pid = ptid_get_pid (inferior_ptid);

  700.   /* This isn't really an address, but ptrace thinks of it as one.  */
  701.   addr = ia64_register_addr (gdbarch, regnum);
  702.   size = register_size (gdbarch, regnum);

  703.   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
  704.   buf = alloca (size);

  705.   /* Write the register contents into the inferior a chunk at a time.  */
  706.   regcache_raw_collect (regcache, regnum, buf);
  707.   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
  708.     {
  709.       errno = 0;
  710.       ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)addr, buf[i]);
  711.       if (errno != 0)
  712.         error (_("Couldn't write register %s (#%d): %s."),
  713.                gdbarch_register_name (gdbarch, regnum),
  714.                regnum, safe_strerror (errno));

  715.       addr += sizeof (PTRACE_TYPE_RET);
  716.     }
  717. }

  718. /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
  719.    this for all registers.  */

  720. static void
  721. ia64_linux_store_registers (struct target_ops *ops,
  722.                             struct regcache *regcache, int regnum)
  723. {
  724.   if (regnum == -1)
  725.     for (regnum = 0;
  726.          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
  727.          regnum++)
  728.       ia64_linux_store_register (regcache, regnum);
  729.   else
  730.     ia64_linux_store_register (regcache, regnum);
  731. }


  732. static target_xfer_partial_ftype *super_xfer_partial;

  733. /* Implement the to_xfer_partial target_ops method.  */

  734. static enum target_xfer_status
  735. ia64_linux_xfer_partial (struct target_ops *ops,
  736.                          enum target_object object,
  737.                          const char *annex,
  738.                          gdb_byte *readbuf, const gdb_byte *writebuf,
  739.                          ULONGEST offset, ULONGEST len,
  740.                          ULONGEST *xfered_len)
  741. {
  742.   if (object == TARGET_OBJECT_UNWIND_TABLE && readbuf != NULL)
  743.     {
  744.       static long gate_table_size;
  745.       gdb_byte *tmp_buf;
  746.       long res;

  747.       /* Probe for the table size once.  */
  748.       if (gate_table_size == 0)
  749.         gate_table_size = syscall (__NR_getunwind, NULL, 0);
  750.       if (gate_table_size < 0)
  751.         return TARGET_XFER_E_IO;

  752.       if (offset >= gate_table_size)
  753.         return TARGET_XFER_EOF;

  754.       tmp_buf = alloca (gate_table_size);
  755.       res = syscall (__NR_getunwind, tmp_buf, gate_table_size);
  756.       if (res < 0)
  757.         return TARGET_XFER_E_IO;
  758.       gdb_assert (res == gate_table_size);

  759.       if (offset + len > gate_table_size)
  760.         len = gate_table_size - offset;

  761.       memcpy (readbuf, tmp_buf + offset, len);
  762.       *xfered_len = len;
  763.       return TARGET_XFER_OK;
  764.     }

  765.   return super_xfer_partial (ops, object, annex, readbuf, writebuf,
  766.                              offset, len, xfered_len);
  767. }

  768. /* For break.b instruction ia64 CPU forgets the immediate value and generates
  769.    SIGILL with ILL_ILLOPC instead of more common SIGTRAP with TRAP_BRKPT.
  770.    ia64 does not use gdbarch_decr_pc_after_break so we do not have to make any
  771.    difference for the signals here.  */

  772. static int
  773. ia64_linux_status_is_event (int status)
  774. {
  775.   return WIFSTOPPED (status) && (WSTOPSIG (status) == SIGTRAP
  776.                                  || WSTOPSIG (status) == SIGILL);
  777. }

  778. void _initialize_ia64_linux_nat (void);

  779. void
  780. _initialize_ia64_linux_nat (void)
  781. {
  782.   struct target_ops *t;

  783.   /* Fill in the generic GNU/Linux methods.  */
  784.   t = linux_target ();

  785.   /* Override the default fetch/store register routines.  */
  786.   t->to_fetch_registers = ia64_linux_fetch_registers;
  787.   t->to_store_registers = ia64_linux_store_registers;

  788.   /* Override the default to_xfer_partial.  */
  789.   super_xfer_partial = t->to_xfer_partial;
  790.   t->to_xfer_partial = ia64_linux_xfer_partial;

  791.   /* Override watchpoint routines.  */

  792.   /* The IA-64 architecture can step over a watch point (without triggering
  793.      it again) if the "dd" (data debug fault disable) bit in the processor
  794.      status word is set.

  795.      This PSR bit is set in ia64_linux_stopped_by_watchpoint when the
  796.      code there has determined that a hardware watchpoint has indeed
  797.      been hit.  The CPU will then be able to execute one instruction
  798.      without triggering a watchpoint.  */

  799.   t->to_have_steppable_watchpoint = 1;
  800.   t->to_can_use_hw_breakpoint = ia64_linux_can_use_hw_breakpoint;
  801.   t->to_stopped_by_watchpoint = ia64_linux_stopped_by_watchpoint;
  802.   t->to_stopped_data_address = ia64_linux_stopped_data_address;
  803.   t->to_insert_watchpoint = ia64_linux_insert_watchpoint;
  804.   t->to_remove_watchpoint = ia64_linux_remove_watchpoint;

  805.   /* Register the target.  */
  806.   linux_nat_add_target (t);
  807.   linux_nat_set_new_thread (t, ia64_linux_new_thread);
  808.   linux_nat_set_status_is_event (t, ia64_linux_status_is_event);
  809. }