xref: /dflybsd-src/contrib/gdb-7/gdb/amd64-nat.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Native-dependent code for AMD64.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdbarch.h"
225796c8dcSSimon Schubert #include "regcache.h"
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert #include "gdb_assert.h"
255796c8dcSSimon Schubert #include "gdb_string.h"
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert #include "i386-tdep.h"
285796c8dcSSimon Schubert #include "amd64-tdep.h"
295796c8dcSSimon Schubert #include "amd64-nat.h"
305796c8dcSSimon Schubert 
315796c8dcSSimon Schubert /* The following bits of code help with implementing debugging 32-bit
325796c8dcSSimon Schubert    code natively on AMD64.  The idea is to define two mappings between
335796c8dcSSimon Schubert    the register number as used by GDB and the register set used by the
345796c8dcSSimon Schubert    host to represent the general-purpose registers; one for 32-bit
355796c8dcSSimon Schubert    code and one for 64-bit code.  The mappings are specified by the
365796c8dcSSimon Schubert    follwing variables and consist of an array of offsets within the
375796c8dcSSimon Schubert    register set indexed by register number, and the number of
385796c8dcSSimon Schubert    registers supported by the mapping.  We don't need mappings for the
395796c8dcSSimon Schubert    floating-point and SSE registers, since the difference between
405796c8dcSSimon Schubert    64-bit and 32-bit variants are negligable.  The difference in the
415796c8dcSSimon Schubert    number of SSE registers is already handled by the target code.  */
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert /* General-purpose register mapping for native 32-bit code.  */
445796c8dcSSimon Schubert int *amd64_native_gregset32_reg_offset;
455796c8dcSSimon Schubert int amd64_native_gregset32_num_regs = I386_NUM_GREGS;
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert /* General-purpose register mapping for native 64-bit code.  */
485796c8dcSSimon Schubert int *amd64_native_gregset64_reg_offset;
495796c8dcSSimon Schubert int amd64_native_gregset64_num_regs = AMD64_NUM_GREGS;
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert /* Return the offset of REGNUM within the appropriate native
525796c8dcSSimon Schubert    general-purpose register set.  */
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert static int
amd64_native_gregset_reg_offset(struct gdbarch * gdbarch,int regnum)555796c8dcSSimon Schubert amd64_native_gregset_reg_offset (struct gdbarch *gdbarch, int regnum)
565796c8dcSSimon Schubert {
575796c8dcSSimon Schubert   int *reg_offset = amd64_native_gregset64_reg_offset;
585796c8dcSSimon Schubert   int num_regs = amd64_native_gregset64_num_regs;
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
615796c8dcSSimon Schubert 
62*ef5ccd6cSJohn Marino   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
635796c8dcSSimon Schubert     {
645796c8dcSSimon Schubert       reg_offset = amd64_native_gregset32_reg_offset;
655796c8dcSSimon Schubert       num_regs = amd64_native_gregset32_num_regs;
665796c8dcSSimon Schubert     }
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert   if (num_regs > gdbarch_num_regs (gdbarch))
695796c8dcSSimon Schubert     num_regs = gdbarch_num_regs (gdbarch);
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert   if (regnum < num_regs && regnum < gdbarch_num_regs (gdbarch))
725796c8dcSSimon Schubert     return reg_offset[regnum];
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert   return -1;
755796c8dcSSimon Schubert }
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* Return whether the native general-purpose register set supplies
785796c8dcSSimon Schubert    register REGNUM.  */
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert int
amd64_native_gregset_supplies_p(struct gdbarch * gdbarch,int regnum)815796c8dcSSimon Schubert amd64_native_gregset_supplies_p (struct gdbarch *gdbarch, int regnum)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert   return (amd64_native_gregset_reg_offset (gdbarch, regnum) != -1);
845796c8dcSSimon Schubert }
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert /* Supply register REGNUM, whose contents are stored in GREGS, to
885796c8dcSSimon Schubert    REGCACHE.  If REGNUM is -1, supply all appropriate registers.  */
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert void
amd64_supply_native_gregset(struct regcache * regcache,const void * gregs,int regnum)915796c8dcSSimon Schubert amd64_supply_native_gregset (struct regcache *regcache,
925796c8dcSSimon Schubert 			     const void *gregs, int regnum)
935796c8dcSSimon Schubert {
945796c8dcSSimon Schubert   const char *regs = gregs;
955796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
965796c8dcSSimon Schubert   int num_regs = amd64_native_gregset64_num_regs;
975796c8dcSSimon Schubert   int i;
985796c8dcSSimon Schubert 
99*ef5ccd6cSJohn Marino   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
1005796c8dcSSimon Schubert     num_regs = amd64_native_gregset32_num_regs;
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert   if (num_regs > gdbarch_num_regs (gdbarch))
1035796c8dcSSimon Schubert     num_regs = gdbarch_num_regs (gdbarch);
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert   for (i = 0; i < num_regs; i++)
1065796c8dcSSimon Schubert     {
1075796c8dcSSimon Schubert       if (regnum == -1 || regnum == i)
1085796c8dcSSimon Schubert 	{
1095796c8dcSSimon Schubert 	  int offset = amd64_native_gregset_reg_offset (gdbarch, i);
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert 	  if (offset != -1)
1125796c8dcSSimon Schubert 	    regcache_raw_supply (regcache, i, regs + offset);
1135796c8dcSSimon Schubert 	}
1145796c8dcSSimon Schubert     }
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert /* Collect register REGNUM from REGCACHE and store its contents in
1185796c8dcSSimon Schubert    GREGS.  If REGNUM is -1, collect and store all appropriate
1195796c8dcSSimon Schubert    registers.  */
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert void
amd64_collect_native_gregset(const struct regcache * regcache,void * gregs,int regnum)1225796c8dcSSimon Schubert amd64_collect_native_gregset (const struct regcache *regcache,
1235796c8dcSSimon Schubert 			      void *gregs, int regnum)
1245796c8dcSSimon Schubert {
1255796c8dcSSimon Schubert   char *regs = gregs;
1265796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1275796c8dcSSimon Schubert   int num_regs = amd64_native_gregset64_num_regs;
1285796c8dcSSimon Schubert   int i;
1295796c8dcSSimon Schubert 
130*ef5ccd6cSJohn Marino   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
1315796c8dcSSimon Schubert     {
1325796c8dcSSimon Schubert       num_regs = amd64_native_gregset32_num_regs;
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert       /* Make sure %eax, %ebx, %ecx, %edx, %esi, %edi, %ebp, %esp and
1355796c8dcSSimon Schubert          %eip get zero-extended to 64 bits.  */
1365796c8dcSSimon Schubert       for (i = 0; i <= I386_EIP_REGNUM; i++)
1375796c8dcSSimon Schubert 	{
1385796c8dcSSimon Schubert 	  if (regnum == -1 || regnum == i)
1395796c8dcSSimon Schubert 	    memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
1405796c8dcSSimon Schubert 	}
1415796c8dcSSimon Schubert       /* Ditto for %cs, %ss, %ds, %es, %fs, and %gs.  */
1425796c8dcSSimon Schubert       for (i = I386_CS_REGNUM; i <= I386_GS_REGNUM; i++)
1435796c8dcSSimon Schubert 	{
1445796c8dcSSimon Schubert 	  if (regnum == -1 || regnum == i)
1455796c8dcSSimon Schubert 	    memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
1465796c8dcSSimon Schubert 	}
1475796c8dcSSimon Schubert     }
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   if (num_regs > gdbarch_num_regs (gdbarch))
1505796c8dcSSimon Schubert     num_regs = gdbarch_num_regs (gdbarch);
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert   for (i = 0; i < num_regs; i++)
1535796c8dcSSimon Schubert     {
1545796c8dcSSimon Schubert       if (regnum == -1 || regnum == i)
1555796c8dcSSimon Schubert 	{
1565796c8dcSSimon Schubert 	  int offset = amd64_native_gregset_reg_offset (gdbarch, i);
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert 	  if (offset != -1)
1595796c8dcSSimon Schubert 	    regcache_raw_collect (regcache, i, regs + offset);
1605796c8dcSSimon Schubert 	}
1615796c8dcSSimon Schubert     }
1625796c8dcSSimon Schubert }
163