GDB (API)
/home/stan/gdb/src/gdb/ppc-linux-nat.c
Go to the documentation of this file.
00001 /* PPC GNU/Linux native support.
00002 
00003    Copyright (C) 1988-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_string.h"
00022 #include "observer.h"
00023 #include "frame.h"
00024 #include "inferior.h"
00025 #include "gdbthread.h"
00026 #include "gdbcore.h"
00027 #include "regcache.h"
00028 #include "gdb_assert.h"
00029 #include "target.h"
00030 #include "linux-nat.h"
00031 
00032 #include <stdint.h>
00033 #include <sys/types.h>
00034 #include <signal.h>
00035 #include <sys/user.h>
00036 #include <sys/ioctl.h>
00037 #include "gdb_wait.h"
00038 #include <fcntl.h>
00039 #include <sys/procfs.h>
00040 #include <sys/ptrace.h>
00041 
00042 /* Prototypes for supply_gregset etc.  */
00043 #include "gregset.h"
00044 #include "ppc-tdep.h"
00045 #include "ppc-linux-tdep.h"
00046 
00047 /* Required when using the AUXV.  */
00048 #include "elf/common.h"
00049 #include "auxv.h"
00050 
00051 /* This sometimes isn't defined.  */
00052 #ifndef PT_ORIG_R3
00053 #define PT_ORIG_R3 34
00054 #endif
00055 #ifndef PT_TRAP
00056 #define PT_TRAP 40
00057 #endif
00058 
00059 /* The PPC_FEATURE_* defines should be provided by <asm/cputable.h>.
00060    If they aren't, we can provide them ourselves (their values are fixed
00061    because they are part of the kernel ABI).  They are used in the AT_HWCAP
00062    entry of the AUXV.  */
00063 #ifndef PPC_FEATURE_CELL
00064 #define PPC_FEATURE_CELL 0x00010000
00065 #endif
00066 #ifndef PPC_FEATURE_BOOKE
00067 #define PPC_FEATURE_BOOKE 0x00008000
00068 #endif
00069 #ifndef PPC_FEATURE_HAS_DFP
00070 #define PPC_FEATURE_HAS_DFP     0x00000400  /* Decimal Floating Point.  */
00071 #endif
00072 
00073 /* Glibc's headers don't define PTRACE_GETVRREGS so we cannot use a
00074    configure time check.  Some older glibc's (for instance 2.2.1)
00075    don't have a specific powerpc version of ptrace.h, and fall back on
00076    a generic one.  In such cases, sys/ptrace.h defines
00077    PTRACE_GETFPXREGS and PTRACE_SETFPXREGS to the same numbers that
00078    ppc kernel's asm/ptrace.h defines PTRACE_GETVRREGS and
00079    PTRACE_SETVRREGS to be.  This also makes a configury check pretty
00080    much useless.  */
00081 
00082 /* These definitions should really come from the glibc header files,
00083    but Glibc doesn't know about the vrregs yet.  */
00084 #ifndef PTRACE_GETVRREGS
00085 #define PTRACE_GETVRREGS 18
00086 #define PTRACE_SETVRREGS 19
00087 #endif
00088 
00089 /* PTRACE requests for POWER7 VSX registers.  */
00090 #ifndef PTRACE_GETVSXREGS
00091 #define PTRACE_GETVSXREGS 27
00092 #define PTRACE_SETVSXREGS 28
00093 #endif
00094 
00095 /* Similarly for the ptrace requests for getting / setting the SPE
00096    registers (ev0 -- ev31, acc, and spefscr).  See the description of
00097    gdb_evrregset_t for details.  */
00098 #ifndef PTRACE_GETEVRREGS
00099 #define PTRACE_GETEVRREGS 20
00100 #define PTRACE_SETEVRREGS 21
00101 #endif
00102 
00103 /* Similarly for the hardware watchpoint support.  These requests are used
00104    when the PowerPC HWDEBUG ptrace interface is not available.  */
00105 #ifndef PTRACE_GET_DEBUGREG
00106 #define PTRACE_GET_DEBUGREG    25
00107 #endif
00108 #ifndef PTRACE_SET_DEBUGREG
00109 #define PTRACE_SET_DEBUGREG    26
00110 #endif
00111 #ifndef PTRACE_GETSIGINFO
00112 #define PTRACE_GETSIGINFO    0x4202
00113 #endif
00114 
00115 /* These requests are used when the PowerPC HWDEBUG ptrace interface is
00116    available.  It exposes the debug facilities of PowerPC processors, as well
00117    as additional features of BookE processors, such as ranged breakpoints and
00118    watchpoints and hardware-accelerated condition evaluation.  */
00119 #ifndef PPC_PTRACE_GETHWDBGINFO
00120 
00121 /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the PowerPC HWDEBUG 
00122    ptrace interface is not present in ptrace.h, so we'll have to pretty much
00123    include it all here so that the code at least compiles on older systems.  */
00124 #define PPC_PTRACE_GETHWDBGINFO 0x89
00125 #define PPC_PTRACE_SETHWDEBUG   0x88
00126 #define PPC_PTRACE_DELHWDEBUG   0x87
00127 
00128 struct ppc_debug_info
00129 {
00130         uint32_t version;               /* Only version 1 exists to date.  */
00131         uint32_t num_instruction_bps;
00132         uint32_t num_data_bps;
00133         uint32_t num_condition_regs;
00134         uint32_t data_bp_alignment;
00135         uint32_t sizeof_condition;      /* size of the DVC register.  */
00136         uint64_t features;
00137 };
00138 
00139 /* Features will have bits indicating whether there is support for:  */
00140 #define PPC_DEBUG_FEATURE_INSN_BP_RANGE         0x1
00141 #define PPC_DEBUG_FEATURE_INSN_BP_MASK          0x2
00142 #define PPC_DEBUG_FEATURE_DATA_BP_RANGE         0x4
00143 #define PPC_DEBUG_FEATURE_DATA_BP_MASK          0x8
00144 
00145 struct ppc_hw_breakpoint
00146 {
00147         uint32_t version;               /* currently, version must be 1 */
00148         uint32_t trigger_type;          /* only some combinations allowed */
00149         uint32_t addr_mode;             /* address match mode */
00150         uint32_t condition_mode;        /* break/watchpoint condition flags */
00151         uint64_t addr;                  /* break/watchpoint address */
00152         uint64_t addr2;                 /* range end or mask */
00153         uint64_t condition_value;       /* contents of the DVC register */
00154 };
00155 
00156 /* Trigger type.  */
00157 #define PPC_BREAKPOINT_TRIGGER_EXECUTE  0x1
00158 #define PPC_BREAKPOINT_TRIGGER_READ     0x2
00159 #define PPC_BREAKPOINT_TRIGGER_WRITE    0x4
00160 #define PPC_BREAKPOINT_TRIGGER_RW       0x6
00161 
00162 /* Address mode.  */
00163 #define PPC_BREAKPOINT_MODE_EXACT               0x0
00164 #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE     0x1
00165 #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE     0x2
00166 #define PPC_BREAKPOINT_MODE_MASK                0x3
00167 
00168 /* Condition mode.  */
00169 #define PPC_BREAKPOINT_CONDITION_NONE   0x0
00170 #define PPC_BREAKPOINT_CONDITION_AND    0x1
00171 #define PPC_BREAKPOINT_CONDITION_EXACT  0x1
00172 #define PPC_BREAKPOINT_CONDITION_OR     0x2
00173 #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
00174 #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
00175 #define PPC_BREAKPOINT_CONDITION_BE_SHIFT       16
00176 #define PPC_BREAKPOINT_CONDITION_BE(n)  \
00177         (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
00178 #endif /* PPC_PTRACE_GETHWDBGINFO */
00179 
00180 /* Feature defined on Linux kernel v3.9: DAWR interface, that enables wider
00181    watchpoint (up to 512 bytes).  */
00182 #ifndef PPC_DEBUG_FEATURE_DATA_BP_DAWR
00183 #define PPC_DEBUG_FEATURE_DATA_BP_DAWR  0x10
00184 #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */
00185 
00186 /* Similarly for the general-purpose (gp0 -- gp31)
00187    and floating-point registers (fp0 -- fp31).  */
00188 #ifndef PTRACE_GETREGS
00189 #define PTRACE_GETREGS 12
00190 #endif
00191 #ifndef PTRACE_SETREGS
00192 #define PTRACE_SETREGS 13
00193 #endif
00194 #ifndef PTRACE_GETFPREGS
00195 #define PTRACE_GETFPREGS 14
00196 #endif
00197 #ifndef PTRACE_SETFPREGS
00198 #define PTRACE_SETFPREGS 15
00199 #endif
00200 
00201 /* This oddity is because the Linux kernel defines elf_vrregset_t as
00202    an array of 33 16 bytes long elements.  I.e. it leaves out vrsave.
00203    However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
00204    the vrsave as an extra 4 bytes at the end.  I opted for creating a
00205    flat array of chars, so that it is easier to manipulate for gdb.
00206 
00207    There are 32 vector registers 16 bytes longs, plus a VSCR register
00208    which is only 4 bytes long, but is fetched as a 16 bytes
00209    quantity.  Up to here we have the elf_vrregset_t structure.
00210    Appended to this there is space for the VRSAVE register: 4 bytes.
00211    Even though this vrsave register is not included in the regset
00212    typedef, it is handled by the ptrace requests.
00213 
00214    Note that GNU/Linux doesn't support little endian PPC hardware,
00215    therefore the offset at which the real value of the VSCR register
00216    is located will be always 12 bytes.
00217 
00218    The layout is like this (where x is the actual value of the vscr reg): */
00219 
00220 /* *INDENT-OFF* */
00221 /*
00222    |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
00223    <------->     <-------><-------><->
00224      VR0           VR31     VSCR    VRSAVE
00225 */
00226 /* *INDENT-ON* */
00227 
00228 #define SIZEOF_VRREGS 33*16+4
00229 
00230 typedef char gdb_vrregset_t[SIZEOF_VRREGS];
00231 
00232 /* This is the layout of the POWER7 VSX registers and the way they overlap
00233    with the existing FPR and VMX registers.
00234 
00235                     VSR doubleword 0               VSR doubleword 1
00236            ----------------------------------------------------------------
00237    VSR[0]  |             FPR[0]            |                              |
00238            ----------------------------------------------------------------
00239    VSR[1]  |             FPR[1]            |                              |
00240            ----------------------------------------------------------------
00241            |              ...              |                              |
00242            |              ...              |                              |
00243            ----------------------------------------------------------------
00244    VSR[30] |             FPR[30]           |                              |
00245            ----------------------------------------------------------------
00246    VSR[31] |             FPR[31]           |                              |
00247            ----------------------------------------------------------------
00248    VSR[32] |                             VR[0]                            |
00249            ----------------------------------------------------------------
00250    VSR[33] |                             VR[1]                            |
00251            ----------------------------------------------------------------
00252            |                              ...                             |
00253            |                              ...                             |
00254            ----------------------------------------------------------------
00255    VSR[62] |                             VR[30]                           |
00256            ----------------------------------------------------------------
00257    VSR[63] |                             VR[31]                           |
00258           ----------------------------------------------------------------
00259 
00260    VSX has 64 128bit registers.  The first 32 registers overlap with
00261    the FP registers (doubleword 0) and hence extend them with additional
00262    64 bits (doubleword 1).  The other 32 regs overlap with the VMX
00263    registers.  */
00264 #define SIZEOF_VSXREGS 32*8
00265 
00266 typedef char gdb_vsxregset_t[SIZEOF_VSXREGS];
00267 
00268 /* On PPC processors that support the Signal Processing Extension
00269    (SPE) APU, the general-purpose registers are 64 bits long.
00270    However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
00271    ptrace calls only access the lower half of each register, to allow
00272    them to behave the same way they do on non-SPE systems.  There's a
00273    separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
00274    read and write the top halves of all the general-purpose registers
00275    at once, along with some SPE-specific registers.
00276 
00277    GDB itself continues to claim the general-purpose registers are 32
00278    bits long.  It has unnamed raw registers that hold the upper halves
00279    of the gprs, and the full 64-bit SIMD views of the registers,
00280    'ev0' -- 'ev31', are pseudo-registers that splice the top and
00281    bottom halves together.
00282 
00283    This is the structure filled in by PTRACE_GETEVRREGS and written to
00284    the inferior's registers by PTRACE_SETEVRREGS.  */
00285 struct gdb_evrregset_t
00286 {
00287   unsigned long evr[32];
00288   unsigned long long acc;
00289   unsigned long spefscr;
00290 };
00291 
00292 /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
00293    PTRACE_SETVSXREGS requests, for reading and writing the VSX
00294    POWER7 registers 0 through 31.  Zero if we've tried one of them and
00295    gotten an error.  Note that VSX registers 32 through 63 overlap
00296    with VR registers 0 through 31.  */
00297 int have_ptrace_getsetvsxregs = 1;
00298 
00299 /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
00300    PTRACE_SETVRREGS requests, for reading and writing the Altivec
00301    registers.  Zero if we've tried one of them and gotten an
00302    error.  */
00303 int have_ptrace_getvrregs = 1;
00304 
00305 /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
00306    PTRACE_SETEVRREGS requests, for reading and writing the SPE
00307    registers.  Zero if we've tried one of them and gotten an
00308    error.  */
00309 int have_ptrace_getsetevrregs = 1;
00310 
00311 /* Non-zero if our kernel may support the PTRACE_GETREGS and
00312    PTRACE_SETREGS requests, for reading and writing the
00313    general-purpose registers.  Zero if we've tried one of
00314    them and gotten an error.  */
00315 int have_ptrace_getsetregs = 1;
00316 
00317 /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
00318    PTRACE_SETFPREGS requests, for reading and writing the
00319    floating-pointers registers.  Zero if we've tried one of
00320    them and gotten an error.  */
00321 int have_ptrace_getsetfpregs = 1;
00322 
00323 /* *INDENT-OFF* */
00324 /* registers layout, as presented by the ptrace interface:
00325 PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
00326 PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
00327 PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
00328 PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
00329 PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6,
00330 PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
00331 PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22,
00332 PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
00333 PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38,
00334 PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
00335 PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54,
00336 PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
00337 PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
00338 /* *INDENT_ON * */
00339 
00340 static int
00341 ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
00342 {
00343   int u_addr = -1;
00344   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00345   /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
00346      interface, and not the wordsize of the program's ABI.  */
00347   int wordsize = sizeof (long);
00348 
00349   /* General purpose registers occupy 1 slot each in the buffer.  */
00350   if (regno >= tdep->ppc_gp0_regnum 
00351       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
00352     u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
00353 
00354   /* Floating point regs: eight bytes each in both 32- and 64-bit
00355      ptrace interfaces.  Thus, two slots each in 32-bit interface, one
00356      slot each in 64-bit interface.  */
00357   if (tdep->ppc_fp0_regnum >= 0
00358       && regno >= tdep->ppc_fp0_regnum
00359       && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
00360     u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
00361 
00362   /* UISA special purpose registers: 1 slot each.  */
00363   if (regno == gdbarch_pc_regnum (gdbarch))
00364     u_addr = PT_NIP * wordsize;
00365   if (regno == tdep->ppc_lr_regnum)
00366     u_addr = PT_LNK * wordsize;
00367   if (regno == tdep->ppc_cr_regnum)
00368     u_addr = PT_CCR * wordsize;
00369   if (regno == tdep->ppc_xer_regnum)
00370     u_addr = PT_XER * wordsize;
00371   if (regno == tdep->ppc_ctr_regnum)
00372     u_addr = PT_CTR * wordsize;
00373 #ifdef PT_MQ
00374   if (regno == tdep->ppc_mq_regnum)
00375     u_addr = PT_MQ * wordsize;
00376 #endif
00377   if (regno == tdep->ppc_ps_regnum)
00378     u_addr = PT_MSR * wordsize;
00379   if (regno == PPC_ORIG_R3_REGNUM)
00380     u_addr = PT_ORIG_R3 * wordsize;
00381   if (regno == PPC_TRAP_REGNUM)
00382     u_addr = PT_TRAP * wordsize;
00383   if (tdep->ppc_fpscr_regnum >= 0
00384       && regno == tdep->ppc_fpscr_regnum)
00385     {
00386       /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
00387          kernel headers incorrectly contained the 32-bit definition of
00388          PT_FPSCR.  For the 32-bit definition, floating-point
00389          registers occupy two 32-bit "slots", and the FPSCR lives in
00390          the second half of such a slot-pair (hence +1).  For 64-bit,
00391          the FPSCR instead occupies the full 64-bit 2-word-slot and
00392          hence no adjustment is necessary.  Hack around this.  */
00393       if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
00394         u_addr = (48 + 32) * wordsize;
00395       /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
00396          slot and not just its second word.  The PT_FPSCR supplied when
00397          GDB is compiled as a 32-bit app doesn't reflect this.  */
00398       else if (wordsize == 4 && register_size (gdbarch, regno) == 8
00399                && PT_FPSCR == (48 + 2*32 + 1))
00400         u_addr = (48 + 2*32) * wordsize;
00401       else
00402         u_addr = PT_FPSCR * wordsize;
00403     }
00404   return u_addr;
00405 }
00406 
00407 /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
00408    registers set mechanism, as opposed to the interface for all the
00409    other registers, that stores/fetches each register individually.  */
00410 static void
00411 fetch_vsx_register (struct regcache *regcache, int tid, int regno)
00412 {
00413   int ret;
00414   gdb_vsxregset_t regs;
00415   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00416   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00417   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
00418 
00419   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
00420   if (ret < 0)
00421     {
00422       if (errno == EIO)
00423         {
00424           have_ptrace_getsetvsxregs = 0;
00425           return;
00426         }
00427       perror_with_name (_("Unable to fetch VSX register"));
00428     }
00429 
00430   regcache_raw_supply (regcache, regno,
00431                        regs + (regno - tdep->ppc_vsr0_upper_regnum)
00432                        * vsxregsize);
00433 }
00434 
00435 /* The Linux kernel ptrace interface for AltiVec registers uses the
00436    registers set mechanism, as opposed to the interface for all the
00437    other registers, that stores/fetches each register individually.  */
00438 static void
00439 fetch_altivec_register (struct regcache *regcache, int tid, int regno)
00440 {
00441   int ret;
00442   int offset = 0;
00443   gdb_vrregset_t regs;
00444   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00445   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00446   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
00447 
00448   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
00449   if (ret < 0)
00450     {
00451       if (errno == EIO)
00452         {
00453           have_ptrace_getvrregs = 0;
00454           return;
00455         }
00456       perror_with_name (_("Unable to fetch AltiVec register"));
00457     }
00458  
00459   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
00460      long on the hardware.  We deal only with the lower 4 bytes of the
00461      vector.  VRSAVE is at the end of the array in a 4 bytes slot, so
00462      there is no need to define an offset for it.  */
00463   if (regno == (tdep->ppc_vrsave_regnum - 1))
00464     offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
00465   
00466   regcache_raw_supply (regcache, regno,
00467                        regs + (regno
00468                                - tdep->ppc_vr0_regnum) * vrregsize + offset);
00469 }
00470 
00471 /* Fetch the top 32 bits of TID's general-purpose registers and the
00472    SPE-specific registers, and place the results in EVRREGSET.  If we
00473    don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
00474    zeros.
00475 
00476    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
00477    PTRACE_SETEVRREGS requests are supported is isolated here, and in
00478    set_spe_registers.  */
00479 static void
00480 get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
00481 {
00482   if (have_ptrace_getsetevrregs)
00483     {
00484       if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
00485         return;
00486       else
00487         {
00488           /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
00489              we just return zeros.  */
00490           if (errno == EIO)
00491             have_ptrace_getsetevrregs = 0;
00492           else
00493             /* Anything else needs to be reported.  */
00494             perror_with_name (_("Unable to fetch SPE registers"));
00495         }
00496     }
00497 
00498   memset (evrregset, 0, sizeof (*evrregset));
00499 }
00500 
00501 /* Supply values from TID for SPE-specific raw registers: the upper
00502    halves of the GPRs, the accumulator, and the spefscr.  REGNO must
00503    be the number of an upper half register, acc, spefscr, or -1 to
00504    supply the values of all registers.  */
00505 static void
00506 fetch_spe_register (struct regcache *regcache, int tid, int regno)
00507 {
00508   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00509   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00510   struct gdb_evrregset_t evrregs;
00511 
00512   gdb_assert (sizeof (evrregs.evr[0])
00513               == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
00514   gdb_assert (sizeof (evrregs.acc)
00515               == register_size (gdbarch, tdep->ppc_acc_regnum));
00516   gdb_assert (sizeof (evrregs.spefscr)
00517               == register_size (gdbarch, tdep->ppc_spefscr_regnum));
00518 
00519   get_spe_registers (tid, &evrregs);
00520 
00521   if (regno == -1)
00522     {
00523       int i;
00524 
00525       for (i = 0; i < ppc_num_gprs; i++)
00526         regcache_raw_supply (regcache, tdep->ppc_ev0_upper_regnum + i,
00527                              &evrregs.evr[i]);
00528     }
00529   else if (tdep->ppc_ev0_upper_regnum <= regno
00530            && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
00531     regcache_raw_supply (regcache, regno,
00532                          &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
00533 
00534   if (regno == -1
00535       || regno == tdep->ppc_acc_regnum)
00536     regcache_raw_supply (regcache, tdep->ppc_acc_regnum, &evrregs.acc);
00537 
00538   if (regno == -1
00539       || regno == tdep->ppc_spefscr_regnum)
00540     regcache_raw_supply (regcache, tdep->ppc_spefscr_regnum,
00541                          &evrregs.spefscr);
00542 }
00543 
00544 static void
00545 fetch_register (struct regcache *regcache, int tid, int regno)
00546 {
00547   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00548   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00549   /* This isn't really an address.  But ptrace thinks of it as one.  */
00550   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
00551   int bytes_transferred;
00552   unsigned int offset;         /* Offset of registers within the u area.  */
00553   gdb_byte buf[MAX_REGISTER_SIZE];
00554 
00555   if (altivec_register_p (gdbarch, regno))
00556     {
00557       /* If this is the first time through, or if it is not the first
00558          time through, and we have comfirmed that there is kernel
00559          support for such a ptrace request, then go and fetch the
00560          register.  */
00561       if (have_ptrace_getvrregs)
00562        {
00563          fetch_altivec_register (regcache, tid, regno);
00564          return;
00565        }
00566      /* If we have discovered that there is no ptrace support for
00567         AltiVec registers, fall through and return zeroes, because
00568         regaddr will be -1 in this case.  */
00569     }
00570   if (vsx_register_p (gdbarch, regno))
00571     {
00572       if (have_ptrace_getsetvsxregs)
00573         {
00574           fetch_vsx_register (regcache, tid, regno);
00575           return;
00576         }
00577     }
00578   else if (spe_register_p (gdbarch, regno))
00579     {
00580       fetch_spe_register (regcache, tid, regno);
00581       return;
00582     }
00583 
00584   if (regaddr == -1)
00585     {
00586       memset (buf, '\0', register_size (gdbarch, regno));   /* Supply zeroes */
00587       regcache_raw_supply (regcache, regno, buf);
00588       return;
00589     }
00590 
00591   /* Read the raw register using sizeof(long) sized chunks.  On a
00592      32-bit platform, 64-bit floating-point registers will require two
00593      transfers.  */
00594   for (bytes_transferred = 0;
00595        bytes_transferred < register_size (gdbarch, regno);
00596        bytes_transferred += sizeof (long))
00597     {
00598       long l;
00599 
00600       errno = 0;
00601       l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
00602       regaddr += sizeof (long);
00603       if (errno != 0)
00604         {
00605           char message[128];
00606           xsnprintf (message, sizeof (message), "reading register %s (#%d)",
00607                      gdbarch_register_name (gdbarch, regno), regno);
00608           perror_with_name (message);
00609         }
00610       memcpy (&buf[bytes_transferred], &l, sizeof (l));
00611     }
00612 
00613   /* Now supply the register.  Keep in mind that the regcache's idea
00614      of the register's size may not be a multiple of sizeof
00615      (long).  */
00616   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
00617     {
00618       /* Little-endian values are always found at the left end of the
00619          bytes transferred.  */
00620       regcache_raw_supply (regcache, regno, buf);
00621     }
00622   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
00623     {
00624       /* Big-endian values are found at the right end of the bytes
00625          transferred.  */
00626       size_t padding = (bytes_transferred - register_size (gdbarch, regno));
00627       regcache_raw_supply (regcache, regno, buf + padding);
00628     }
00629   else 
00630     internal_error (__FILE__, __LINE__,
00631                     _("fetch_register: unexpected byte order: %d"),
00632                     gdbarch_byte_order (gdbarch));
00633 }
00634 
00635 static void
00636 supply_vsxregset (struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
00637 {
00638   int i;
00639   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00640   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00641   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
00642 
00643   for (i = 0; i < ppc_num_vshrs; i++)
00644     {
00645         regcache_raw_supply (regcache, tdep->ppc_vsr0_upper_regnum + i,
00646                              *vsxregsetp + i * vsxregsize);
00647     }
00648 }
00649 
00650 static void
00651 supply_vrregset (struct regcache *regcache, gdb_vrregset_t *vrregsetp)
00652 {
00653   int i;
00654   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00655   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00656   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
00657   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
00658   int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
00659 
00660   for (i = 0; i < num_of_vrregs; i++)
00661     {
00662       /* The last 2 registers of this set are only 32 bit long, not
00663          128.  However an offset is necessary only for VSCR because it
00664          occupies a whole vector, while VRSAVE occupies a full 4 bytes
00665          slot.  */
00666       if (i == (num_of_vrregs - 2))
00667         regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
00668                              *vrregsetp + i * vrregsize + offset);
00669       else
00670         regcache_raw_supply (regcache, tdep->ppc_vr0_regnum + i,
00671                              *vrregsetp + i * vrregsize);
00672     }
00673 }
00674 
00675 static void
00676 fetch_vsx_registers (struct regcache *regcache, int tid)
00677 {
00678   int ret;
00679   gdb_vsxregset_t regs;
00680 
00681   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
00682   if (ret < 0)
00683     {
00684       if (errno == EIO)
00685         {
00686           have_ptrace_getsetvsxregs = 0;
00687           return;
00688         }
00689       perror_with_name (_("Unable to fetch VSX registers"));
00690     }
00691   supply_vsxregset (regcache, &regs);
00692 }
00693 
00694 static void
00695 fetch_altivec_registers (struct regcache *regcache, int tid)
00696 {
00697   int ret;
00698   gdb_vrregset_t regs;
00699   
00700   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
00701   if (ret < 0)
00702     {
00703       if (errno == EIO)
00704         {
00705           have_ptrace_getvrregs = 0;
00706           return;
00707         }
00708       perror_with_name (_("Unable to fetch AltiVec registers"));
00709     }
00710   supply_vrregset (regcache, &regs);
00711 }
00712 
00713 /* This function actually issues the request to ptrace, telling
00714    it to get all general-purpose registers and put them into the
00715    specified regset.
00716    
00717    If the ptrace request does not exist, this function returns 0
00718    and properly sets the have_ptrace_* flag.  If the request fails,
00719    this function calls perror_with_name.  Otherwise, if the request
00720    succeeds, then the regcache gets filled and 1 is returned.  */
00721 static int
00722 fetch_all_gp_regs (struct regcache *regcache, int tid)
00723 {
00724   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00725   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00726   gdb_gregset_t gregset;
00727 
00728   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
00729     {
00730       if (errno == EIO)
00731         {
00732           have_ptrace_getsetregs = 0;
00733           return 0;
00734         }
00735       perror_with_name (_("Couldn't get general-purpose registers."));
00736     }
00737 
00738   supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
00739 
00740   return 1;
00741 }
00742 
00743 /* This is a wrapper for the fetch_all_gp_regs function.  It is
00744    responsible for verifying if this target has the ptrace request
00745    that can be used to fetch all general-purpose registers at one
00746    shot.  If it doesn't, then we should fetch them using the
00747    old-fashioned way, which is to iterate over the registers and
00748    request them one by one.  */
00749 static void
00750 fetch_gp_regs (struct regcache *regcache, int tid)
00751 {
00752   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00753   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00754   int i;
00755 
00756   if (have_ptrace_getsetregs)
00757     if (fetch_all_gp_regs (regcache, tid))
00758       return;
00759 
00760   /* If we've hit this point, it doesn't really matter which
00761      architecture we are using.  We just need to read the
00762      registers in the "old-fashioned way".  */
00763   for (i = 0; i < ppc_num_gprs; i++)
00764     fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
00765 }
00766 
00767 /* This function actually issues the request to ptrace, telling
00768    it to get all floating-point registers and put them into the
00769    specified regset.
00770    
00771    If the ptrace request does not exist, this function returns 0
00772    and properly sets the have_ptrace_* flag.  If the request fails,
00773    this function calls perror_with_name.  Otherwise, if the request
00774    succeeds, then the regcache gets filled and 1 is returned.  */
00775 static int
00776 fetch_all_fp_regs (struct regcache *regcache, int tid)
00777 {
00778   gdb_fpregset_t fpregs;
00779 
00780   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
00781     {
00782       if (errno == EIO)
00783         {
00784           have_ptrace_getsetfpregs = 0;
00785           return 0;
00786         }
00787       perror_with_name (_("Couldn't get floating-point registers."));
00788     }
00789 
00790   supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
00791 
00792   return 1;
00793 }
00794 
00795 /* This is a wrapper for the fetch_all_fp_regs function.  It is
00796    responsible for verifying if this target has the ptrace request
00797    that can be used to fetch all floating-point registers at one
00798    shot.  If it doesn't, then we should fetch them using the
00799    old-fashioned way, which is to iterate over the registers and
00800    request them one by one.  */
00801 static void
00802 fetch_fp_regs (struct regcache *regcache, int tid)
00803 {
00804   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00805   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00806   int i;
00807 
00808   if (have_ptrace_getsetfpregs)
00809     if (fetch_all_fp_regs (regcache, tid))
00810       return;
00811  
00812   /* If we've hit this point, it doesn't really matter which
00813      architecture we are using.  We just need to read the
00814      registers in the "old-fashioned way".  */
00815   for (i = 0; i < ppc_num_fprs; i++)
00816     fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
00817 }
00818 
00819 static void 
00820 fetch_ppc_registers (struct regcache *regcache, int tid)
00821 {
00822   int i;
00823   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00824   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00825 
00826   fetch_gp_regs (regcache, tid);
00827   if (tdep->ppc_fp0_regnum >= 0)
00828     fetch_fp_regs (regcache, tid);
00829   fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
00830   if (tdep->ppc_ps_regnum != -1)
00831     fetch_register (regcache, tid, tdep->ppc_ps_regnum);
00832   if (tdep->ppc_cr_regnum != -1)
00833     fetch_register (regcache, tid, tdep->ppc_cr_regnum);
00834   if (tdep->ppc_lr_regnum != -1)
00835     fetch_register (regcache, tid, tdep->ppc_lr_regnum);
00836   if (tdep->ppc_ctr_regnum != -1)
00837     fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
00838   if (tdep->ppc_xer_regnum != -1)
00839     fetch_register (regcache, tid, tdep->ppc_xer_regnum);
00840   if (tdep->ppc_mq_regnum != -1)
00841     fetch_register (regcache, tid, tdep->ppc_mq_regnum);
00842   if (ppc_linux_trap_reg_p (gdbarch))
00843     {
00844       fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
00845       fetch_register (regcache, tid, PPC_TRAP_REGNUM);
00846     }
00847   if (tdep->ppc_fpscr_regnum != -1)
00848     fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
00849   if (have_ptrace_getvrregs)
00850     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
00851       fetch_altivec_registers (regcache, tid);
00852   if (have_ptrace_getsetvsxregs)
00853     if (tdep->ppc_vsr0_upper_regnum != -1)
00854       fetch_vsx_registers (regcache, tid);
00855   if (tdep->ppc_ev0_upper_regnum >= 0)
00856     fetch_spe_register (regcache, tid, -1);
00857 }
00858 
00859 /* Fetch registers from the child process.  Fetch all registers if
00860    regno == -1, otherwise fetch all general registers or all floating
00861    point registers depending upon the value of regno.  */
00862 static void
00863 ppc_linux_fetch_inferior_registers (struct target_ops *ops,
00864                                     struct regcache *regcache, int regno)
00865 {
00866   /* Overload thread id onto process id.  */
00867   int tid = ptid_get_lwp (inferior_ptid);
00868 
00869   /* No thread id, just use process id.  */
00870   if (tid == 0)
00871     tid = ptid_get_pid (inferior_ptid);
00872 
00873   if (regno == -1)
00874     fetch_ppc_registers (regcache, tid);
00875   else 
00876     fetch_register (regcache, tid, regno);
00877 }
00878 
00879 /* Store one VSX register.  */
00880 static void
00881 store_vsx_register (const struct regcache *regcache, int tid, int regno)
00882 {
00883   int ret;
00884   gdb_vsxregset_t regs;
00885   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00886   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00887   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
00888 
00889   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
00890   if (ret < 0)
00891     {
00892       if (errno == EIO)
00893         {
00894           have_ptrace_getsetvsxregs = 0;
00895           return;
00896         }
00897       perror_with_name (_("Unable to fetch VSX register"));
00898     }
00899 
00900   regcache_raw_collect (regcache, regno, regs +
00901                         (regno - tdep->ppc_vsr0_upper_regnum) * vsxregsize);
00902 
00903   ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
00904   if (ret < 0)
00905     perror_with_name (_("Unable to store VSX register"));
00906 }
00907 
00908 /* Store one register.  */
00909 static void
00910 store_altivec_register (const struct regcache *regcache, int tid, int regno)
00911 {
00912   int ret;
00913   int offset = 0;
00914   gdb_vrregset_t regs;
00915   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00916   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00917   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
00918 
00919   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
00920   if (ret < 0)
00921     {
00922       if (errno == EIO)
00923         {
00924           have_ptrace_getvrregs = 0;
00925           return;
00926         }
00927       perror_with_name (_("Unable to fetch AltiVec register"));
00928     }
00929 
00930   /* VSCR is fetched as a 16 bytes quantity, but it is really 4 bytes
00931      long on the hardware.  */
00932   if (regno == (tdep->ppc_vrsave_regnum - 1))
00933     offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
00934 
00935   regcache_raw_collect (regcache, regno,
00936                         regs + (regno
00937                                 - tdep->ppc_vr0_regnum) * vrregsize + offset);
00938 
00939   ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
00940   if (ret < 0)
00941     perror_with_name (_("Unable to store AltiVec register"));
00942 }
00943 
00944 /* Assuming TID referrs to an SPE process, set the top halves of TID's
00945    general-purpose registers and its SPE-specific registers to the
00946    values in EVRREGSET.  If we don't support PTRACE_SETEVRREGS, do
00947    nothing.
00948 
00949    All the logic to deal with whether or not the PTRACE_GETEVRREGS and
00950    PTRACE_SETEVRREGS requests are supported is isolated here, and in
00951    get_spe_registers.  */
00952 static void
00953 set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
00954 {
00955   if (have_ptrace_getsetevrregs)
00956     {
00957       if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
00958         return;
00959       else
00960         {
00961           /* EIO means that the PTRACE_SETEVRREGS request isn't
00962              supported; we fail silently, and don't try the call
00963              again.  */
00964           if (errno == EIO)
00965             have_ptrace_getsetevrregs = 0;
00966           else
00967             /* Anything else needs to be reported.  */
00968             perror_with_name (_("Unable to set SPE registers"));
00969         }
00970     }
00971 }
00972 
00973 /* Write GDB's value for the SPE-specific raw register REGNO to TID.
00974    If REGNO is -1, write the values of all the SPE-specific
00975    registers.  */
00976 static void
00977 store_spe_register (const struct regcache *regcache, int tid, int regno)
00978 {
00979   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00980   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00981   struct gdb_evrregset_t evrregs;
00982 
00983   gdb_assert (sizeof (evrregs.evr[0])
00984               == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
00985   gdb_assert (sizeof (evrregs.acc)
00986               == register_size (gdbarch, tdep->ppc_acc_regnum));
00987   gdb_assert (sizeof (evrregs.spefscr)
00988               == register_size (gdbarch, tdep->ppc_spefscr_regnum));
00989 
00990   if (regno == -1)
00991     /* Since we're going to write out every register, the code below
00992        should store to every field of evrregs; if that doesn't happen,
00993        make it obvious by initializing it with suspicious values.  */
00994     memset (&evrregs, 42, sizeof (evrregs));
00995   else
00996     /* We can only read and write the entire EVR register set at a
00997        time, so to write just a single register, we do a
00998        read-modify-write maneuver.  */
00999     get_spe_registers (tid, &evrregs);
01000 
01001   if (regno == -1)
01002     {
01003       int i;
01004 
01005       for (i = 0; i < ppc_num_gprs; i++)
01006         regcache_raw_collect (regcache,
01007                               tdep->ppc_ev0_upper_regnum + i,
01008                               &evrregs.evr[i]);
01009     }
01010   else if (tdep->ppc_ev0_upper_regnum <= regno
01011            && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
01012     regcache_raw_collect (regcache, regno,
01013                           &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
01014 
01015   if (regno == -1
01016       || regno == tdep->ppc_acc_regnum)
01017     regcache_raw_collect (regcache,
01018                           tdep->ppc_acc_regnum,
01019                           &evrregs.acc);
01020 
01021   if (regno == -1
01022       || regno == tdep->ppc_spefscr_regnum)
01023     regcache_raw_collect (regcache,
01024                           tdep->ppc_spefscr_regnum,
01025                           &evrregs.spefscr);
01026 
01027   /* Write back the modified register set.  */
01028   set_spe_registers (tid, &evrregs);
01029 }
01030 
01031 static void
01032 store_register (const struct regcache *regcache, int tid, int regno)
01033 {
01034   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01035   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01036   /* This isn't really an address.  But ptrace thinks of it as one.  */
01037   CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
01038   int i;
01039   size_t bytes_to_transfer;
01040   gdb_byte buf[MAX_REGISTER_SIZE];
01041 
01042   if (altivec_register_p (gdbarch, regno))
01043     {
01044       store_altivec_register (regcache, tid, regno);
01045       return;
01046     }
01047   if (vsx_register_p (gdbarch, regno))
01048     {
01049       store_vsx_register (regcache, tid, regno);
01050       return;
01051     }
01052   else if (spe_register_p (gdbarch, regno))
01053     {
01054       store_spe_register (regcache, tid, regno);
01055       return;
01056     }
01057 
01058   if (regaddr == -1)
01059     return;
01060 
01061   /* First collect the register.  Keep in mind that the regcache's
01062      idea of the register's size may not be a multiple of sizeof
01063      (long).  */
01064   memset (buf, 0, sizeof buf);
01065   bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
01066   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
01067     {
01068       /* Little-endian values always sit at the left end of the buffer.  */
01069       regcache_raw_collect (regcache, regno, buf);
01070     }
01071   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01072     {
01073       /* Big-endian values sit at the right end of the buffer.  */
01074       size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
01075       regcache_raw_collect (regcache, regno, buf + padding);
01076     }
01077 
01078   for (i = 0; i < bytes_to_transfer; i += sizeof (long))
01079     {
01080       long l;
01081 
01082       memcpy (&l, &buf[i], sizeof (l));
01083       errno = 0;
01084       ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
01085       regaddr += sizeof (long);
01086 
01087       if (errno == EIO 
01088           && (regno == tdep->ppc_fpscr_regnum
01089               || regno == PPC_ORIG_R3_REGNUM
01090               || regno == PPC_TRAP_REGNUM))
01091         {
01092           /* Some older kernel versions don't allow fpscr, orig_r3
01093              or trap to be written.  */
01094           continue;
01095         }
01096 
01097       if (errno != 0)
01098         {
01099           char message[128];
01100           xsnprintf (message, sizeof (message), "writing register %s (#%d)",
01101                      gdbarch_register_name (gdbarch, regno), regno);
01102           perror_with_name (message);
01103         }
01104     }
01105 }
01106 
01107 static void
01108 fill_vsxregset (const struct regcache *regcache, gdb_vsxregset_t *vsxregsetp)
01109 {
01110   int i;
01111   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01112   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01113   int vsxregsize = register_size (gdbarch, tdep->ppc_vsr0_upper_regnum);
01114 
01115   for (i = 0; i < ppc_num_vshrs; i++)
01116     regcache_raw_collect (regcache, tdep->ppc_vsr0_upper_regnum + i,
01117                           *vsxregsetp + i * vsxregsize);
01118 }
01119 
01120 static void
01121 fill_vrregset (const struct regcache *regcache, gdb_vrregset_t *vrregsetp)
01122 {
01123   int i;
01124   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01125   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01126   int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
01127   int vrregsize = register_size (gdbarch, tdep->ppc_vr0_regnum);
01128   int offset = vrregsize - register_size (gdbarch, tdep->ppc_vrsave_regnum);
01129 
01130   for (i = 0; i < num_of_vrregs; i++)
01131     {
01132       /* The last 2 registers of this set are only 32 bit long, not
01133          128, but only VSCR is fetched as a 16 bytes quantity.  */
01134       if (i == (num_of_vrregs - 2))
01135         regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
01136                               *vrregsetp + i * vrregsize + offset);
01137       else
01138         regcache_raw_collect (regcache, tdep->ppc_vr0_regnum + i,
01139                               *vrregsetp + i * vrregsize);
01140     }
01141 }
01142 
01143 static void
01144 store_vsx_registers (const struct regcache *regcache, int tid)
01145 {
01146   int ret;
01147   gdb_vsxregset_t regs;
01148 
01149   ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
01150   if (ret < 0)
01151     {
01152       if (errno == EIO)
01153         {
01154           have_ptrace_getsetvsxregs = 0;
01155           return;
01156         }
01157       perror_with_name (_("Couldn't get VSX registers"));
01158     }
01159 
01160   fill_vsxregset (regcache, &regs);
01161 
01162   if (ptrace (PTRACE_SETVSXREGS, tid, 0, &regs) < 0)
01163     perror_with_name (_("Couldn't write VSX registers"));
01164 }
01165 
01166 static void
01167 store_altivec_registers (const struct regcache *regcache, int tid)
01168 {
01169   int ret;
01170   gdb_vrregset_t regs;
01171 
01172   ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
01173   if (ret < 0)
01174     {
01175       if (errno == EIO)
01176         {
01177           have_ptrace_getvrregs = 0;
01178           return;
01179         }
01180       perror_with_name (_("Couldn't get AltiVec registers"));
01181     }
01182 
01183   fill_vrregset (regcache, &regs);
01184   
01185   if (ptrace (PTRACE_SETVRREGS, tid, 0, &regs) < 0)
01186     perror_with_name (_("Couldn't write AltiVec registers"));
01187 }
01188 
01189 /* This function actually issues the request to ptrace, telling
01190    it to store all general-purpose registers present in the specified
01191    regset.
01192    
01193    If the ptrace request does not exist, this function returns 0
01194    and properly sets the have_ptrace_* flag.  If the request fails,
01195    this function calls perror_with_name.  Otherwise, if the request
01196    succeeds, then the regcache is stored and 1 is returned.  */
01197 static int
01198 store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
01199 {
01200   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01201   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01202   gdb_gregset_t gregset;
01203 
01204   if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
01205     {
01206       if (errno == EIO)
01207         {
01208           have_ptrace_getsetregs = 0;
01209           return 0;
01210         }
01211       perror_with_name (_("Couldn't get general-purpose registers."));
01212     }
01213 
01214   fill_gregset (regcache, &gregset, regno);
01215 
01216   if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
01217     {
01218       if (errno == EIO)
01219         {
01220           have_ptrace_getsetregs = 0;
01221           return 0;
01222         }
01223       perror_with_name (_("Couldn't set general-purpose registers."));
01224     }
01225 
01226   return 1;
01227 }
01228 
01229 /* This is a wrapper for the store_all_gp_regs function.  It is
01230    responsible for verifying if this target has the ptrace request
01231    that can be used to store all general-purpose registers at one
01232    shot.  If it doesn't, then we should store them using the
01233    old-fashioned way, which is to iterate over the registers and
01234    store them one by one.  */
01235 static void
01236 store_gp_regs (const struct regcache *regcache, int tid, int regno)
01237 {
01238   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01239   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01240   int i;
01241 
01242   if (have_ptrace_getsetregs)
01243     if (store_all_gp_regs (regcache, tid, regno))
01244       return;
01245 
01246   /* If we hit this point, it doesn't really matter which
01247      architecture we are using.  We just need to store the
01248      registers in the "old-fashioned way".  */
01249   for (i = 0; i < ppc_num_gprs; i++)
01250     store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
01251 }
01252 
01253 /* This function actually issues the request to ptrace, telling
01254    it to store all floating-point registers present in the specified
01255    regset.
01256    
01257    If the ptrace request does not exist, this function returns 0
01258    and properly sets the have_ptrace_* flag.  If the request fails,
01259    this function calls perror_with_name.  Otherwise, if the request
01260    succeeds, then the regcache is stored and 1 is returned.  */
01261 static int
01262 store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
01263 {
01264   gdb_fpregset_t fpregs;
01265 
01266   if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
01267     {
01268       if (errno == EIO)
01269         {
01270           have_ptrace_getsetfpregs = 0;
01271           return 0;
01272         }
01273       perror_with_name (_("Couldn't get floating-point registers."));
01274     }
01275 
01276   fill_fpregset (regcache, &fpregs, regno);
01277 
01278   if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
01279     {
01280       if (errno == EIO)
01281         {
01282           have_ptrace_getsetfpregs = 0;
01283           return 0;
01284         }
01285       perror_with_name (_("Couldn't set floating-point registers."));
01286     }
01287 
01288   return 1;
01289 }
01290 
01291 /* This is a wrapper for the store_all_fp_regs function.  It is
01292    responsible for verifying if this target has the ptrace request
01293    that can be used to store all floating-point registers at one
01294    shot.  If it doesn't, then we should store them using the
01295    old-fashioned way, which is to iterate over the registers and
01296    store them one by one.  */
01297 static void
01298 store_fp_regs (const struct regcache *regcache, int tid, int regno)
01299 {
01300   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01301   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01302   int i;
01303 
01304   if (have_ptrace_getsetfpregs)
01305     if (store_all_fp_regs (regcache, tid, regno))
01306       return;
01307 
01308   /* If we hit this point, it doesn't really matter which
01309      architecture we are using.  We just need to store the
01310      registers in the "old-fashioned way".  */
01311   for (i = 0; i < ppc_num_fprs; i++)
01312     store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
01313 }
01314 
01315 static void
01316 store_ppc_registers (const struct regcache *regcache, int tid)
01317 {
01318   int i;
01319   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01320   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01321  
01322   store_gp_regs (regcache, tid, -1);
01323   if (tdep->ppc_fp0_regnum >= 0)
01324     store_fp_regs (regcache, tid, -1);
01325   store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
01326   if (tdep->ppc_ps_regnum != -1)
01327     store_register (regcache, tid, tdep->ppc_ps_regnum);
01328   if (tdep->ppc_cr_regnum != -1)
01329     store_register (regcache, tid, tdep->ppc_cr_regnum);
01330   if (tdep->ppc_lr_regnum != -1)
01331     store_register (regcache, tid, tdep->ppc_lr_regnum);
01332   if (tdep->ppc_ctr_regnum != -1)
01333     store_register (regcache, tid, tdep->ppc_ctr_regnum);
01334   if (tdep->ppc_xer_regnum != -1)
01335     store_register (regcache, tid, tdep->ppc_xer_regnum);
01336   if (tdep->ppc_mq_regnum != -1)
01337     store_register (regcache, tid, tdep->ppc_mq_regnum);
01338   if (tdep->ppc_fpscr_regnum != -1)
01339     store_register (regcache, tid, tdep->ppc_fpscr_regnum);
01340   if (ppc_linux_trap_reg_p (gdbarch))
01341     {
01342       store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
01343       store_register (regcache, tid, PPC_TRAP_REGNUM);
01344     }
01345   if (have_ptrace_getvrregs)
01346     if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
01347       store_altivec_registers (regcache, tid);
01348   if (have_ptrace_getsetvsxregs)
01349     if (tdep->ppc_vsr0_upper_regnum != -1)
01350       store_vsx_registers (regcache, tid);
01351   if (tdep->ppc_ev0_upper_regnum >= 0)
01352     store_spe_register (regcache, tid, -1);
01353 }
01354 
01355 /* Fetch the AT_HWCAP entry from the aux vector.  */
01356 static unsigned long
01357 ppc_linux_get_hwcap (void)
01358 {
01359   CORE_ADDR field;
01360 
01361   if (target_auxv_search (&current_target, AT_HWCAP, &field))
01362     return (unsigned long) field;
01363 
01364   return 0;
01365 }
01366 
01367 /* The cached DABR value, to install in new threads.
01368    This variable is used when the PowerPC HWDEBUG ptrace
01369    interface is not available.  */
01370 static long saved_dabr_value;
01371 
01372 /* Global structure that will store information about the available
01373    features provided by the PowerPC HWDEBUG ptrace interface.  */
01374 static struct ppc_debug_info hwdebug_info;
01375 
01376 /* Global variable that holds the maximum number of slots that the
01377    kernel will use.  This is only used when PowerPC HWDEBUG ptrace interface
01378    is available.  */
01379 static size_t max_slots_number = 0;
01380 
01381 struct hw_break_tuple
01382 {
01383   long slot;
01384   struct ppc_hw_breakpoint *hw_break;
01385 };
01386 
01387 /* This is an internal VEC created to store information about *points inserted
01388    for each thread.  This is used when PowerPC HWDEBUG ptrace interface is
01389    available.  */
01390 typedef struct thread_points
01391   {
01392     /* The TID to which this *point relates.  */
01393     int tid;
01394     /* Information about the *point, such as its address, type, etc.
01395 
01396        Each element inside this vector corresponds to a hardware
01397        breakpoint or watchpoint in the thread represented by TID.  The maximum
01398        size of these vector is MAX_SLOTS_NUMBER.  If the hw_break element of
01399        the tuple is NULL, then the position in the vector is free.  */
01400     struct hw_break_tuple *hw_breaks;
01401   } *thread_points_p;
01402 DEF_VEC_P (thread_points_p);
01403 
01404 VEC(thread_points_p) *ppc_threads = NULL;
01405 
01406 /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
01407    available.  */
01408 #define PPC_DEBUG_CURRENT_VERSION 1
01409 
01410 /* Returns non-zero if we support the PowerPC HWDEBUG ptrace interface.  */
01411 static int
01412 have_ptrace_hwdebug_interface (void)
01413 {
01414   static int have_ptrace_hwdebug_interface = -1;
01415 
01416   if (have_ptrace_hwdebug_interface == -1)
01417     {
01418       int tid;
01419 
01420       tid = ptid_get_lwp (inferior_ptid);
01421       if (tid == 0)
01422         tid = ptid_get_pid (inferior_ptid);
01423 
01424       /* Check for kernel support for PowerPC HWDEBUG ptrace interface.  */
01425       if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info) >= 0)
01426         {
01427           /* Check whether PowerPC HWDEBUG ptrace interface is functional and
01428              provides any supported feature.  */
01429           if (hwdebug_info.features != 0)
01430             {
01431               have_ptrace_hwdebug_interface = 1;
01432               max_slots_number = hwdebug_info.num_instruction_bps
01433                 + hwdebug_info.num_data_bps
01434                 + hwdebug_info.num_condition_regs;
01435               return have_ptrace_hwdebug_interface;
01436             }
01437         }
01438       /* Old school interface and no PowerPC HWDEBUG ptrace support.  */
01439       have_ptrace_hwdebug_interface = 0;
01440       memset (&hwdebug_info, 0, sizeof (struct ppc_debug_info));
01441     }
01442 
01443   return have_ptrace_hwdebug_interface;
01444 }
01445 
01446 static int
01447 ppc_linux_can_use_hw_breakpoint (int type, int cnt, int ot)
01448 {
01449   int total_hw_wp, total_hw_bp;
01450 
01451   if (have_ptrace_hwdebug_interface ())
01452     {
01453       /* When PowerPC HWDEBUG ptrace interface is available, the number of
01454          available hardware watchpoints and breakpoints is stored at the
01455          hwdebug_info struct.  */
01456       total_hw_bp = hwdebug_info.num_instruction_bps;
01457       total_hw_wp = hwdebug_info.num_data_bps;
01458     }
01459   else
01460     {
01461       /* When we do not have PowerPC HWDEBUG ptrace interface, we should
01462          consider having 1 hardware watchpoint and no hardware breakpoints.  */
01463       total_hw_bp = 0;
01464       total_hw_wp = 1;
01465     }
01466 
01467   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
01468       || type == bp_access_watchpoint || type == bp_watchpoint)
01469     {
01470       if (cnt + ot > total_hw_wp)
01471         return -1;
01472     }
01473   else if (type == bp_hardware_breakpoint)
01474     {
01475       if (cnt > total_hw_bp)
01476         return -1;
01477     }
01478 
01479   if (!have_ptrace_hwdebug_interface ())
01480     {
01481       int tid;
01482       ptid_t ptid = inferior_ptid;
01483 
01484       /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG
01485          and whether the target has DABR.  If either answer is no, the
01486          ptrace call will return -1.  Fail in that case.  */
01487       tid = ptid_get_lwp (ptid);
01488       if (tid == 0)
01489         tid = ptid_get_pid (ptid);
01490 
01491       if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
01492         return 0;
01493     }
01494 
01495   return 1;
01496 }
01497 
01498 static int
01499 ppc_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
01500 {
01501   /* Handle sub-8-byte quantities.  */
01502   if (len <= 0)
01503     return 0;
01504 
01505   /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
01506      restrictions for watchpoints in the processors.  In that case, we use that
01507      information to determine the hardcoded watchable region for
01508      watchpoints.  */
01509   if (have_ptrace_hwdebug_interface ())
01510     {
01511       int region_size;
01512       /* Embedded DAC-based processors, like the PowerPC 440 have ranged
01513          watchpoints and can watch any access within an arbitrary memory
01514          region. This is useful to watch arrays and structs, for instance.  It
01515          takes two hardware watchpoints though.  */
01516       if (len > 1
01517           && hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
01518           && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
01519         return 2;
01520       /* Check if the processor provides DAWR interface.  */
01521       if (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR)
01522         /* DAWR interface allows to watch up to 512 byte wide ranges which
01523            can't cross a 512 byte boundary.  */
01524         region_size = 512;
01525       else
01526         region_size = hwdebug_info.data_bp_alignment;
01527       /* Server processors provide one hardware watchpoint and addr+len should
01528          fall in the watchable region provided by the ptrace interface.  */
01529       if (region_size
01530           && (addr + len > (addr & ~(region_size - 1)) + region_size))
01531         return 0;
01532     }
01533   /* addr+len must fall in the 8 byte watchable region for DABR-based
01534      processors (i.e., server processors).  Without the new PowerPC HWDEBUG 
01535      ptrace interface, DAC-based processors (i.e., embedded processors) will
01536      use addresses aligned to 4-bytes due to the way the read/write flags are
01537      passed in the old ptrace interface.  */
01538   else if (((ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
01539            && (addr + len) > (addr & ~3) + 4)
01540            || (addr + len) > (addr & ~7) + 8)
01541     return 0;
01542 
01543   return 1;
01544 }
01545 
01546 /* This function compares two ppc_hw_breakpoint structs field-by-field.  */
01547 static int
01548 hwdebug_point_cmp (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
01549 {
01550   return (a->trigger_type == b->trigger_type
01551           && a->addr_mode == b->addr_mode
01552           && a->condition_mode == b->condition_mode
01553           && a->addr == b->addr
01554           && a->addr2 == b->addr2
01555           && a->condition_value == b->condition_value);
01556 }
01557 
01558 /* This function can be used to retrieve a thread_points by the TID of the
01559    related process/thread.  If nothing has been found, and ALLOC_NEW is 0,
01560    it returns NULL.  If ALLOC_NEW is non-zero, a new thread_points for the
01561    provided TID will be created and returned.  */
01562 static struct thread_points *
01563 hwdebug_find_thread_points_by_tid (int tid, int alloc_new)
01564 {
01565   int i;
01566   struct thread_points *t;
01567 
01568   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
01569     if (t->tid == tid)
01570       return t;
01571 
01572   t = NULL;
01573 
01574   /* Do we need to allocate a new point_item
01575      if the wanted one does not exist?  */
01576   if (alloc_new)
01577     {
01578       t = xmalloc (sizeof (struct thread_points));
01579       t->hw_breaks
01580         = xzalloc (max_slots_number * sizeof (struct hw_break_tuple));
01581       t->tid = tid;
01582       VEC_safe_push (thread_points_p, ppc_threads, t);
01583     }
01584 
01585   return t;
01586 }
01587 
01588 /* This function is a generic wrapper that is responsible for inserting a
01589    *point (i.e., calling `ptrace' in order to issue the request to the
01590    kernel) and registering it internally in GDB.  */
01591 static void
01592 hwdebug_insert_point (struct ppc_hw_breakpoint *b, int tid)
01593 {
01594   int i;
01595   long slot;
01596   struct ppc_hw_breakpoint *p = xmalloc (sizeof (struct ppc_hw_breakpoint));
01597   struct hw_break_tuple *hw_breaks;
01598   struct cleanup *c = make_cleanup (xfree, p);
01599   struct thread_points *t;
01600   struct hw_break_tuple *tuple;
01601 
01602   memcpy (p, b, sizeof (struct ppc_hw_breakpoint));
01603 
01604   errno = 0;
01605   slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p);
01606   if (slot < 0)
01607     perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));
01608 
01609   /* Everything went fine, so we have to register this *point.  */
01610   t = hwdebug_find_thread_points_by_tid (tid, 1);
01611   gdb_assert (t != NULL);
01612   hw_breaks = t->hw_breaks;
01613 
01614   /* Find a free element in the hw_breaks vector.  */
01615   for (i = 0; i < max_slots_number; i++)
01616     if (hw_breaks[i].hw_break == NULL)
01617       {
01618         hw_breaks[i].slot = slot;
01619         hw_breaks[i].hw_break = p;
01620         break;
01621       }
01622 
01623   gdb_assert (i != max_slots_number);
01624 
01625   discard_cleanups (c);
01626 }
01627 
01628 /* This function is a generic wrapper that is responsible for removing a
01629    *point (i.e., calling `ptrace' in order to issue the request to the
01630    kernel), and unregistering it internally at GDB.  */
01631 static void
01632 hwdebug_remove_point (struct ppc_hw_breakpoint *b, int tid)
01633 {
01634   int i;
01635   struct hw_break_tuple *hw_breaks;
01636   struct thread_points *t;
01637 
01638   t = hwdebug_find_thread_points_by_tid (tid, 0);
01639   gdb_assert (t != NULL);
01640   hw_breaks = t->hw_breaks;
01641 
01642   for (i = 0; i < max_slots_number; i++)
01643     if (hw_breaks[i].hw_break && hwdebug_point_cmp (hw_breaks[i].hw_break, b))
01644       break;
01645 
01646   gdb_assert (i != max_slots_number);
01647 
01648   /* We have to ignore ENOENT errors because the kernel implements hardware
01649      breakpoints/watchpoints as "one-shot", that is, they are automatically
01650      deleted when hit.  */
01651   errno = 0;
01652   if (ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot) < 0)
01653     if (errno != ENOENT)
01654       perror_with_name (_("Unexpected error deleting "
01655                           "breakpoint or watchpoint"));
01656 
01657   xfree (hw_breaks[i].hw_break);
01658   hw_breaks[i].hw_break = NULL;
01659 }
01660 
01661 /* Return the number of registers needed for a ranged breakpoint.  */
01662 
01663 static int
01664 ppc_linux_ranged_break_num_registers (struct target_ops *target)
01665 {
01666   return ((have_ptrace_hwdebug_interface ()
01667            && hwdebug_info.features & PPC_DEBUG_FEATURE_INSN_BP_RANGE)?
01668           2 : -1);
01669 }
01670 
01671 /* Insert the hardware breakpoint described by BP_TGT.  Returns 0 for
01672    success, 1 if hardware breakpoints are not supported or -1 for failure.  */
01673 
01674 static int
01675 ppc_linux_insert_hw_breakpoint (struct gdbarch *gdbarch,
01676                                   struct bp_target_info *bp_tgt)
01677 {
01678   struct lwp_info *lp;
01679   struct ppc_hw_breakpoint p;
01680 
01681   if (!have_ptrace_hwdebug_interface ())
01682     return -1;
01683 
01684   p.version = PPC_DEBUG_CURRENT_VERSION;
01685   p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
01686   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
01687   p.addr = (uint64_t) bp_tgt->placed_address;
01688   p.condition_value = 0;
01689 
01690   if (bp_tgt->length)
01691     {
01692       p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
01693 
01694       /* The breakpoint will trigger if the address of the instruction is
01695          within the defined range, as follows: p.addr <= address < p.addr2.  */
01696       p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
01697     }
01698   else
01699     {
01700       p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
01701       p.addr2 = 0;
01702     }
01703 
01704   ALL_LWPS (lp)
01705     hwdebug_insert_point (&p, ptid_get_lwp (lp->ptid));
01706 
01707   return 0;
01708 }
01709 
01710 static int
01711 ppc_linux_remove_hw_breakpoint (struct gdbarch *gdbarch,
01712                                   struct bp_target_info *bp_tgt)
01713 {
01714   struct lwp_info *lp;
01715   struct ppc_hw_breakpoint p;
01716 
01717   if (!have_ptrace_hwdebug_interface ())
01718     return -1;
01719 
01720   p.version = PPC_DEBUG_CURRENT_VERSION;
01721   p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
01722   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
01723   p.addr = (uint64_t) bp_tgt->placed_address;
01724   p.condition_value = 0;
01725 
01726   if (bp_tgt->length)
01727     {
01728       p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
01729 
01730       /* The breakpoint will trigger if the address of the instruction is within
01731          the defined range, as follows: p.addr <= address < p.addr2.  */
01732       p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
01733     }
01734   else
01735     {
01736       p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
01737       p.addr2 = 0;
01738     }
01739 
01740   ALL_LWPS (lp)
01741     hwdebug_remove_point (&p, ptid_get_lwp (lp->ptid));
01742 
01743   return 0;
01744 }
01745 
01746 static int
01747 get_trigger_type (int rw)
01748 {
01749   int t;
01750 
01751   if (rw == hw_read)
01752     t = PPC_BREAKPOINT_TRIGGER_READ;
01753   else if (rw == hw_write)
01754     t = PPC_BREAKPOINT_TRIGGER_WRITE;
01755   else
01756     t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
01757 
01758   return t;
01759 }
01760 
01761 /* Insert a new masked watchpoint at ADDR using the mask MASK.
01762    RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
01763    or hw_access for an access watchpoint.  Returns 0 on success and throws
01764    an error on failure.  */
01765 
01766 static int
01767 ppc_linux_insert_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
01768                                   CORE_ADDR mask, int rw)
01769 {
01770   struct lwp_info *lp;
01771   struct ppc_hw_breakpoint p;
01772 
01773   gdb_assert (have_ptrace_hwdebug_interface ());
01774 
01775   p.version = PPC_DEBUG_CURRENT_VERSION;
01776   p.trigger_type = get_trigger_type (rw);
01777   p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
01778   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
01779   p.addr = addr;
01780   p.addr2 = mask;
01781   p.condition_value = 0;
01782 
01783   ALL_LWPS (lp)
01784     hwdebug_insert_point (&p, ptid_get_lwp (lp->ptid));
01785 
01786   return 0;
01787 }
01788 
01789 /* Remove a masked watchpoint at ADDR with the mask MASK.
01790    RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
01791    or hw_access for an access watchpoint.  Returns 0 on success and throws
01792    an error on failure.  */
01793 
01794 static int
01795 ppc_linux_remove_mask_watchpoint (struct target_ops *ops, CORE_ADDR addr,
01796                                   CORE_ADDR mask, int rw)
01797 {
01798   struct lwp_info *lp;
01799   struct ppc_hw_breakpoint p;
01800 
01801   gdb_assert (have_ptrace_hwdebug_interface ());
01802 
01803   p.version = PPC_DEBUG_CURRENT_VERSION;
01804   p.trigger_type = get_trigger_type (rw);
01805   p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
01806   p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
01807   p.addr = addr;
01808   p.addr2 = mask;
01809   p.condition_value = 0;
01810 
01811   ALL_LWPS (lp)
01812     hwdebug_remove_point (&p, ptid_get_lwp (lp->ptid));
01813 
01814   return 0;
01815 }
01816 
01817 /* Check whether we have at least one free DVC register.  */
01818 static int
01819 can_use_watchpoint_cond_accel (void)
01820 {
01821   struct thread_points *p;
01822   int tid = ptid_get_lwp (inferior_ptid);
01823   int cnt = hwdebug_info.num_condition_regs, i;
01824   CORE_ADDR tmp_value;
01825 
01826   if (!have_ptrace_hwdebug_interface () || cnt == 0)
01827     return 0;
01828 
01829   p = hwdebug_find_thread_points_by_tid (tid, 0);
01830 
01831   if (p)
01832     {
01833       for (i = 0; i < max_slots_number; i++)
01834         if (p->hw_breaks[i].hw_break != NULL
01835             && (p->hw_breaks[i].hw_break->condition_mode
01836                 != PPC_BREAKPOINT_CONDITION_NONE))
01837           cnt--;
01838 
01839       /* There are no available slots now.  */
01840       if (cnt <= 0)
01841         return 0;
01842     }
01843 
01844   return 1;
01845 }
01846 
01847 /* Calculate the enable bits and the contents of the Data Value Compare
01848    debug register present in BookE processors.
01849 
01850    ADDR is the address to be watched, LEN is the length of watched data
01851    and DATA_VALUE is the value which will trigger the watchpoint.
01852    On exit, CONDITION_MODE will hold the enable bits for the DVC, and
01853    CONDITION_VALUE will hold the value which should be put in the
01854    DVC register.  */
01855 static void
01856 calculate_dvc (CORE_ADDR addr, int len, CORE_ADDR data_value,
01857                uint32_t *condition_mode, uint64_t *condition_value)
01858 {
01859   int i, num_byte_enable, align_offset, num_bytes_off_dvc,
01860       rightmost_enabled_byte;
01861   CORE_ADDR addr_end_data, addr_end_dvc;
01862 
01863   /* The DVC register compares bytes within fixed-length windows which
01864      are word-aligned, with length equal to that of the DVC register.
01865      We need to calculate where our watch region is relative to that
01866      window and enable comparison of the bytes which fall within it.  */
01867 
01868   align_offset = addr % hwdebug_info.sizeof_condition;
01869   addr_end_data = addr + len;
01870   addr_end_dvc = (addr - align_offset
01871                   + hwdebug_info.sizeof_condition);
01872   num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
01873                          addr_end_data - addr_end_dvc : 0;
01874   num_byte_enable = len - num_bytes_off_dvc;
01875   /* Here, bytes are numbered from right to left.  */
01876   rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
01877                               addr_end_dvc - addr_end_data : 0;
01878 
01879   *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
01880   for (i = 0; i < num_byte_enable; i++)
01881     *condition_mode
01882       |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);
01883 
01884   /* Now we need to match the position within the DVC of the comparison
01885      value with where the watch region is relative to the window
01886      (i.e., the ALIGN_OFFSET).  */
01887 
01888   *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
01889                       << rightmost_enabled_byte * 8);
01890 }
01891 
01892 /* Return the number of memory locations that need to be accessed to
01893    evaluate the expression which generated the given value chain.
01894    Returns -1 if there's any register access involved, or if there are
01895    other kinds of values which are not acceptable in a condition
01896    expression (e.g., lval_computed or lval_internalvar).  */
01897 static int
01898 num_memory_accesses (struct value *v)
01899 {
01900   int found_memory_cnt = 0;
01901   struct value *head = v;
01902 
01903   /* The idea here is that evaluating an expression generates a series
01904      of values, one holding the value of every subexpression.  (The
01905      expression a*b+c has five subexpressions: a, b, a*b, c, and
01906      a*b+c.)  GDB's values hold almost enough information to establish
01907      the criteria given above --- they identify memory lvalues,
01908      register lvalues, computed values, etcetera.  So we can evaluate
01909      the expression, and then scan the chain of values that leaves
01910      behind to determine the memory locations involved in the evaluation
01911      of an expression.
01912 
01913      However, I don't think that the values returned by inferior
01914      function calls are special in any way.  So this function may not
01915      notice that an expression contains an inferior function call.
01916      FIXME.  */
01917 
01918   for (; v; v = value_next (v))
01919     {
01920       /* Constants and values from the history are fine.  */
01921       if (VALUE_LVAL (v) == not_lval || deprecated_value_modifiable (v) == 0)
01922         continue;
01923       else if (VALUE_LVAL (v) == lval_memory)
01924         {
01925           /* A lazy memory lvalue is one that GDB never needed to fetch;
01926              we either just used its address (e.g., `a' in `a.b') or
01927              we never needed it at all (e.g., `a' in `a,b').  */
01928           if (!value_lazy (v))
01929             found_memory_cnt++;
01930         }
01931       /* Other kinds of values are not fine.  */
01932       else
01933         return -1;
01934     }
01935 
01936   return found_memory_cnt;
01937 }
01938 
01939 /* Verifies whether the expression COND can be implemented using the
01940    DVC (Data Value Compare) register in BookE processors.  The expression
01941    must test the watch value for equality with a constant expression.
01942    If the function returns 1, DATA_VALUE will contain the constant against
01943    which the watch value should be compared and LEN will contain the size
01944    of the constant.  */
01945 static int
01946 check_condition (CORE_ADDR watch_addr, struct expression *cond,
01947                  CORE_ADDR *data_value, int *len)
01948 {
01949   int pc = 1, num_accesses_left, num_accesses_right;
01950   struct value *left_val, *right_val, *left_chain, *right_chain;
01951 
01952   if (cond->elts[0].opcode != BINOP_EQUAL)
01953     return 0;
01954 
01955   fetch_subexp_value (cond, &pc, &left_val, NULL, &left_chain, 0);
01956   num_accesses_left = num_memory_accesses (left_chain);
01957 
01958   if (left_val == NULL || num_accesses_left < 0)
01959     {
01960       free_value_chain (left_chain);
01961 
01962       return 0;
01963     }
01964 
01965   fetch_subexp_value (cond, &pc, &right_val, NULL, &right_chain, 0);
01966   num_accesses_right = num_memory_accesses (right_chain);
01967 
01968   if (right_val == NULL || num_accesses_right < 0)
01969     {
01970       free_value_chain (left_chain);
01971       free_value_chain (right_chain);
01972 
01973       return 0;
01974     }
01975 
01976   if (num_accesses_left == 1 && num_accesses_right == 0
01977       && VALUE_LVAL (left_val) == lval_memory
01978       && value_address (left_val) == watch_addr)
01979     {
01980       *data_value = value_as_long (right_val);
01981 
01982       /* DATA_VALUE is the constant in RIGHT_VAL, but actually has
01983          the same type as the memory region referenced by LEFT_VAL.  */
01984       *len = TYPE_LENGTH (check_typedef (value_type (left_val)));
01985     }
01986   else if (num_accesses_left == 0 && num_accesses_right == 1
01987            && VALUE_LVAL (right_val) == lval_memory
01988            && value_address (right_val) == watch_addr)
01989     {
01990       *data_value = value_as_long (left_val);
01991 
01992       /* DATA_VALUE is the constant in LEFT_VAL, but actually has
01993          the same type as the memory region referenced by RIGHT_VAL.  */
01994       *len = TYPE_LENGTH (check_typedef (value_type (right_val)));
01995     }
01996   else
01997     {
01998       free_value_chain (left_chain);
01999       free_value_chain (right_chain);
02000 
02001       return 0;
02002     }
02003 
02004   free_value_chain (left_chain);
02005   free_value_chain (right_chain);
02006 
02007   return 1;
02008 }
02009 
02010 /* Return non-zero if the target is capable of using hardware to evaluate
02011    the condition expression, thus only triggering the watchpoint when it is
02012    true.  */
02013 static int
02014 ppc_linux_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int rw,
02015                                           struct expression *cond)
02016 {
02017   CORE_ADDR data_value;
02018 
02019   return (have_ptrace_hwdebug_interface ()
02020           && hwdebug_info.num_condition_regs > 0
02021           && check_condition (addr, cond, &data_value, &len));
02022 }
02023 
02024 /* Set up P with the parameters necessary to request a watchpoint covering
02025    LEN bytes starting at ADDR and if possible with condition expression COND
02026    evaluated by hardware.  INSERT tells if we are creating a request for
02027    inserting or removing the watchpoint.  */
02028 
02029 static void
02030 create_watchpoint_request (struct ppc_hw_breakpoint *p, CORE_ADDR addr,
02031                            int len, int rw, struct expression *cond,
02032                            int insert)
02033 {
02034   if (len == 1
02035       || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
02036     {
02037       int use_condition;
02038       CORE_ADDR data_value;
02039 
02040       use_condition = (insert? can_use_watchpoint_cond_accel ()
02041                         : hwdebug_info.num_condition_regs > 0);
02042       if (cond && use_condition && check_condition (addr, cond,
02043                                                     &data_value, &len))
02044         calculate_dvc (addr, len, data_value, &p->condition_mode,
02045                        &p->condition_value);
02046       else
02047         {
02048           p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
02049           p->condition_value = 0;
02050         }
02051 
02052       p->addr_mode = PPC_BREAKPOINT_MODE_EXACT;
02053       p->addr2 = 0;
02054     }
02055   else
02056     {
02057       p->addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
02058       p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
02059       p->condition_value = 0;
02060 
02061       /* The watchpoint will trigger if the address of the memory access is
02062          within the defined range, as follows: p->addr <= address < p->addr2.
02063 
02064          Note that the above sentence just documents how ptrace interprets
02065          its arguments; the watchpoint is set to watch the range defined by
02066          the user _inclusively_, as specified by the user interface.  */
02067       p->addr2 = (uint64_t) addr + len;
02068     }
02069 
02070   p->version = PPC_DEBUG_CURRENT_VERSION;
02071   p->trigger_type = get_trigger_type (rw);
02072   p->addr = (uint64_t) addr;
02073 }
02074 
02075 static int
02076 ppc_linux_insert_watchpoint (CORE_ADDR addr, int len, int rw,
02077                              struct expression *cond)
02078 {
02079   struct lwp_info *lp;
02080   int ret = -1;
02081 
02082   if (have_ptrace_hwdebug_interface ())
02083     {
02084       struct ppc_hw_breakpoint p;
02085 
02086       create_watchpoint_request (&p, addr, len, rw, cond, 1);
02087 
02088       ALL_LWPS (lp)
02089         hwdebug_insert_point (&p, ptid_get_lwp (lp->ptid));
02090 
02091       ret = 0;
02092     }
02093   else
02094     {
02095       long dabr_value;
02096       long read_mode, write_mode;
02097 
02098       if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
02099         {
02100           /* PowerPC 440 requires only the read/write flags to be passed
02101              to the kernel.  */
02102           read_mode = 1;
02103           write_mode = 2;
02104         }
02105       else
02106         {
02107           /* PowerPC 970 and other DABR-based processors are required to pass
02108              the Breakpoint Translation bit together with the flags.  */
02109           read_mode = 5;
02110           write_mode = 6;
02111         }
02112 
02113       dabr_value = addr & ~(read_mode | write_mode);
02114       switch (rw)
02115         {
02116           case hw_read:
02117             /* Set read and translate bits.  */
02118             dabr_value |= read_mode;
02119             break;
02120           case hw_write:
02121             /* Set write and translate bits.  */
02122             dabr_value |= write_mode;
02123             break;
02124           case hw_access:
02125             /* Set read, write and translate bits.  */
02126             dabr_value |= read_mode | write_mode;
02127             break;
02128         }
02129 
02130       saved_dabr_value = dabr_value;
02131 
02132       ALL_LWPS (lp)
02133         if (ptrace (PTRACE_SET_DEBUGREG, ptid_get_lwp (lp->ptid), 0,
02134                     saved_dabr_value) < 0)
02135           return -1;
02136 
02137       ret = 0;
02138     }
02139 
02140   return ret;
02141 }
02142 
02143 static int
02144 ppc_linux_remove_watchpoint (CORE_ADDR addr, int len, int rw,
02145                              struct expression *cond)
02146 {
02147   struct lwp_info *lp;
02148   int ret = -1;
02149 
02150   if (have_ptrace_hwdebug_interface ())
02151     {
02152       struct ppc_hw_breakpoint p;
02153 
02154       create_watchpoint_request (&p, addr, len, rw, cond, 0);
02155 
02156       ALL_LWPS (lp)
02157         hwdebug_remove_point (&p, ptid_get_lwp (lp->ptid));
02158 
02159       ret = 0;
02160     }
02161   else
02162     {
02163       saved_dabr_value = 0;
02164       ALL_LWPS (lp)
02165         if (ptrace (PTRACE_SET_DEBUGREG, ptid_get_lwp (lp->ptid), 0,
02166                     saved_dabr_value) < 0)
02167           return -1;
02168 
02169       ret = 0;
02170     }
02171 
02172   return ret;
02173 }
02174 
02175 static void
02176 ppc_linux_new_thread (struct lwp_info *lp)
02177 {
02178   int tid = ptid_get_lwp (lp->ptid);
02179 
02180   if (have_ptrace_hwdebug_interface ())
02181     {
02182       int i;
02183       struct thread_points *p;
02184       struct hw_break_tuple *hw_breaks;
02185 
02186       if (VEC_empty (thread_points_p, ppc_threads))
02187         return;
02188 
02189       /* Get a list of breakpoints from any thread.  */
02190       p = VEC_last (thread_points_p, ppc_threads);
02191       hw_breaks = p->hw_breaks;
02192 
02193       /* Copy that thread's breakpoints and watchpoints to the new thread.  */
02194       for (i = 0; i < max_slots_number; i++)
02195         if (hw_breaks[i].hw_break)
02196           {
02197             /* Older kernels did not make new threads inherit their parent
02198                thread's debug state, so we always clear the slot and replicate
02199                the debug state ourselves, ensuring compatibility with all
02200                kernels.  */
02201 
02202             /* The ppc debug resource accounting is done through "slots".
02203                Ask the kernel the deallocate this specific *point's slot.  */
02204             ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot);
02205 
02206             hwdebug_insert_point (hw_breaks[i].hw_break, tid);
02207           }
02208     }
02209   else
02210     ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
02211 }
02212 
02213 static void
02214 ppc_linux_thread_exit (struct thread_info *tp, int silent)
02215 {
02216   int i;
02217   int tid = ptid_get_lwp (tp->ptid);
02218   struct hw_break_tuple *hw_breaks;
02219   struct thread_points *t = NULL, *p;
02220 
02221   if (!have_ptrace_hwdebug_interface ())
02222     return;
02223 
02224   for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
02225     if (p->tid == tid)
02226       {
02227         t = p;
02228         break;
02229       }
02230 
02231   if (t == NULL)
02232     return;
02233 
02234   VEC_unordered_remove (thread_points_p, ppc_threads, i);
02235 
02236   hw_breaks = t->hw_breaks;
02237 
02238   for (i = 0; i < max_slots_number; i++)
02239     if (hw_breaks[i].hw_break)
02240       xfree (hw_breaks[i].hw_break);
02241 
02242   xfree (t->hw_breaks);
02243   xfree (t);
02244 }
02245 
02246 static int
02247 ppc_linux_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
02248 {
02249   siginfo_t siginfo;
02250 
02251   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
02252     return 0;
02253 
02254   if (siginfo.si_signo != SIGTRAP
02255       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
02256     return 0;
02257 
02258   if (have_ptrace_hwdebug_interface ())
02259     {
02260       int i;
02261       struct thread_points *t;
02262       struct hw_break_tuple *hw_breaks;
02263       /* The index (or slot) of the *point is passed in the si_errno field.  */
02264       int slot = siginfo.si_errno;
02265 
02266       t = hwdebug_find_thread_points_by_tid (ptid_get_lwp (inferior_ptid), 0);
02267 
02268       /* Find out if this *point is a hardware breakpoint.
02269          If so, we should return 0.  */
02270       if (t)
02271         {
02272           hw_breaks = t->hw_breaks;
02273           for (i = 0; i < max_slots_number; i++)
02274            if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
02275                && hw_breaks[i].hw_break->trigger_type
02276                     == PPC_BREAKPOINT_TRIGGER_EXECUTE)
02277              return 0;
02278         }
02279     }
02280 
02281   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
02282   return 1;
02283 }
02284 
02285 static int
02286 ppc_linux_stopped_by_watchpoint (void)
02287 {
02288   CORE_ADDR addr;
02289   return ppc_linux_stopped_data_address (&current_target, &addr);
02290 }
02291 
02292 static int
02293 ppc_linux_watchpoint_addr_within_range (struct target_ops *target,
02294                                         CORE_ADDR addr,
02295                                         CORE_ADDR start, int length)
02296 {
02297   int mask;
02298 
02299   if (have_ptrace_hwdebug_interface ()
02300       && ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
02301     return start <= addr && start + length >= addr;
02302   else if (ppc_linux_get_hwcap () & PPC_FEATURE_BOOKE)
02303     mask = 3;
02304   else
02305     mask = 7;
02306 
02307   addr &= ~mask;
02308 
02309   /* Check whether [start, start+length-1] intersects [addr, addr+mask].  */
02310   return start <= addr + mask && start + length - 1 >= addr;
02311 }
02312 
02313 /* Return the number of registers needed for a masked hardware watchpoint.  */
02314 
02315 static int
02316 ppc_linux_masked_watch_num_registers (struct target_ops *target,
02317                                       CORE_ADDR addr, CORE_ADDR mask)
02318 {
02319   if (!have_ptrace_hwdebug_interface ()
02320            || (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
02321     return -1;
02322   else if ((mask & 0xC0000000) != 0xC0000000)
02323     {
02324       warning (_("The given mask covers kernel address space "
02325                  "and cannot be used.\n"));
02326 
02327       return -2;
02328     }
02329   else
02330     return 2;
02331 }
02332 
02333 static void
02334 ppc_linux_store_inferior_registers (struct target_ops *ops,
02335                                     struct regcache *regcache, int regno)
02336 {
02337   /* Overload thread id onto process id.  */
02338   int tid = ptid_get_lwp (inferior_ptid);
02339 
02340   /* No thread id, just use process id.  */
02341   if (tid == 0)
02342     tid = ptid_get_pid (inferior_ptid);
02343 
02344   if (regno >= 0)
02345     store_register (regcache, tid, regno);
02346   else
02347     store_ppc_registers (regcache, tid);
02348 }
02349 
02350 /* Functions for transferring registers between a gregset_t or fpregset_t
02351    (see sys/ucontext.h) and gdb's regcache.  The word size is that used
02352    by the ptrace interface, not the current program's ABI.  Eg. if a
02353    powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
02354    read or write 64-bit gregsets.  This is to suit the host libthread_db.  */
02355 
02356 void
02357 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
02358 {
02359   const struct regset *regset = ppc_linux_gregset (sizeof (long));
02360 
02361   ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
02362 }
02363 
02364 void
02365 fill_gregset (const struct regcache *regcache,
02366               gdb_gregset_t *gregsetp, int regno)
02367 {
02368   const struct regset *regset = ppc_linux_gregset (sizeof (long));
02369 
02370   if (regno == -1)
02371     memset (gregsetp, 0, sizeof (*gregsetp));
02372   ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
02373 }
02374 
02375 void
02376 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
02377 {
02378   const struct regset *regset = ppc_linux_fpregset ();
02379 
02380   ppc_supply_fpregset (regset, regcache, -1,
02381                        fpregsetp, sizeof (*fpregsetp));
02382 }
02383 
02384 void
02385 fill_fpregset (const struct regcache *regcache,
02386                gdb_fpregset_t *fpregsetp, int regno)
02387 {
02388   const struct regset *regset = ppc_linux_fpregset ();
02389 
02390   ppc_collect_fpregset (regset, regcache, regno,
02391                         fpregsetp, sizeof (*fpregsetp));
02392 }
02393 
02394 static int
02395 ppc_linux_target_wordsize (void)
02396 {
02397   int wordsize = 4;
02398 
02399   /* Check for 64-bit inferior process.  This is the case when the host is
02400      64-bit, and in addition the top bit of the MSR register is set.  */
02401 #ifdef __powerpc64__
02402   long msr;
02403 
02404   int tid = ptid_get_lwp (inferior_ptid);
02405   if (tid == 0)
02406     tid = ptid_get_pid (inferior_ptid);
02407 
02408   errno = 0;
02409   msr = (long) ptrace (PTRACE_PEEKUSER, tid, PT_MSR * 8, 0);
02410   if (errno == 0 && msr < 0)
02411     wordsize = 8;
02412 #endif
02413 
02414   return wordsize;
02415 }
02416 
02417 static int
02418 ppc_linux_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
02419                       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
02420 {
02421   int sizeof_auxv_field = ppc_linux_target_wordsize ();
02422   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
02423   gdb_byte *ptr = *readptr;
02424 
02425   if (endptr == ptr)
02426     return 0;
02427 
02428   if (endptr - ptr < sizeof_auxv_field * 2)
02429     return -1;
02430 
02431   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
02432   ptr += sizeof_auxv_field;
02433   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
02434   ptr += sizeof_auxv_field;
02435 
02436   *readptr = ptr;
02437   return 1;
02438 }
02439 
02440 static const struct target_desc *
02441 ppc_linux_read_description (struct target_ops *ops)
02442 {
02443   int altivec = 0;
02444   int vsx = 0;
02445   int isa205 = 0;
02446   int cell = 0;
02447 
02448   int tid = ptid_get_lwp (inferior_ptid);
02449   if (tid == 0)
02450     tid = ptid_get_pid (inferior_ptid);
02451 
02452   if (have_ptrace_getsetevrregs)
02453     {
02454       struct gdb_evrregset_t evrregset;
02455 
02456       if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
02457         return tdesc_powerpc_e500l;
02458 
02459       /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
02460          Anything else needs to be reported.  */
02461       else if (errno != EIO)
02462         perror_with_name (_("Unable to fetch SPE registers"));
02463     }
02464 
02465   if (have_ptrace_getsetvsxregs)
02466     {
02467       gdb_vsxregset_t vsxregset;
02468 
02469       if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
02470         vsx = 1;
02471 
02472       /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
02473          Anything else needs to be reported.  */
02474       else if (errno != EIO)
02475         perror_with_name (_("Unable to fetch VSX registers"));
02476     }
02477 
02478   if (have_ptrace_getvrregs)
02479     {
02480       gdb_vrregset_t vrregset;
02481 
02482       if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
02483         altivec = 1;
02484 
02485       /* EIO means that the PTRACE_GETVRREGS request isn't supported.
02486          Anything else needs to be reported.  */
02487       else if (errno != EIO)
02488         perror_with_name (_("Unable to fetch AltiVec registers"));
02489     }
02490 
02491   /* Power ISA 2.05 (implemented by Power 6 and newer processors) increases
02492      the FPSCR from 32 bits to 64 bits.  Even though Power 7 supports this
02493      ISA version, it doesn't have PPC_FEATURE_ARCH_2_05 set, only
02494      PPC_FEATURE_ARCH_2_06.  Since for now the only bits used in the higher
02495      half of the register are for Decimal Floating Point, we check if that
02496      feature is available to decide the size of the FPSCR.  */
02497   if (ppc_linux_get_hwcap () & PPC_FEATURE_HAS_DFP)
02498     isa205 = 1;
02499 
02500   if (ppc_linux_get_hwcap () & PPC_FEATURE_CELL)
02501     cell = 1;
02502 
02503   if (ppc_linux_target_wordsize () == 8)
02504     {
02505       if (cell)
02506         return tdesc_powerpc_cell64l;
02507       else if (vsx)
02508         return isa205? tdesc_powerpc_isa205_vsx64l : tdesc_powerpc_vsx64l;
02509       else if (altivec)
02510         return isa205
02511           ? tdesc_powerpc_isa205_altivec64l : tdesc_powerpc_altivec64l;
02512 
02513       return isa205? tdesc_powerpc_isa205_64l : tdesc_powerpc_64l;
02514     }
02515 
02516   if (cell)
02517     return tdesc_powerpc_cell32l;
02518   else if (vsx)
02519     return isa205? tdesc_powerpc_isa205_vsx32l : tdesc_powerpc_vsx32l;
02520   else if (altivec)
02521     return isa205? tdesc_powerpc_isa205_altivec32l : tdesc_powerpc_altivec32l;
02522 
02523   return isa205? tdesc_powerpc_isa205_32l : tdesc_powerpc_32l;
02524 }
02525 
02526 void _initialize_ppc_linux_nat (void);
02527 
02528 void
02529 _initialize_ppc_linux_nat (void)
02530 {
02531   struct target_ops *t;
02532 
02533   /* Fill in the generic GNU/Linux methods.  */
02534   t = linux_target ();
02535 
02536   /* Add our register access methods.  */
02537   t->to_fetch_registers = ppc_linux_fetch_inferior_registers;
02538   t->to_store_registers = ppc_linux_store_inferior_registers;
02539 
02540   /* Add our breakpoint/watchpoint methods.  */
02541   t->to_can_use_hw_breakpoint = ppc_linux_can_use_hw_breakpoint;
02542   t->to_insert_hw_breakpoint = ppc_linux_insert_hw_breakpoint;
02543   t->to_remove_hw_breakpoint = ppc_linux_remove_hw_breakpoint;
02544   t->to_region_ok_for_hw_watchpoint = ppc_linux_region_ok_for_hw_watchpoint;
02545   t->to_insert_watchpoint = ppc_linux_insert_watchpoint;
02546   t->to_remove_watchpoint = ppc_linux_remove_watchpoint;
02547   t->to_insert_mask_watchpoint = ppc_linux_insert_mask_watchpoint;
02548   t->to_remove_mask_watchpoint = ppc_linux_remove_mask_watchpoint;
02549   t->to_stopped_by_watchpoint = ppc_linux_stopped_by_watchpoint;
02550   t->to_stopped_data_address = ppc_linux_stopped_data_address;
02551   t->to_watchpoint_addr_within_range = ppc_linux_watchpoint_addr_within_range;
02552   t->to_can_accel_watchpoint_condition
02553     = ppc_linux_can_accel_watchpoint_condition;
02554   t->to_masked_watch_num_registers = ppc_linux_masked_watch_num_registers;
02555   t->to_ranged_break_num_registers = ppc_linux_ranged_break_num_registers;
02556 
02557   t->to_read_description = ppc_linux_read_description;
02558   t->to_auxv_parse = ppc_linux_auxv_parse;
02559 
02560   observer_attach_thread_exit (ppc_linux_thread_exit);
02561 
02562   /* Register the target.  */
02563   linux_nat_add_target (t);
02564   linux_nat_set_new_thread (t, ppc_linux_new_thread);
02565 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines