xref: /netbsd-src/external/gpl3/gdb/dist/gdb/amd64-nat.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /* Native-dependent code for AMD64.
2 
3    Copyright (C) 2003-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "gdbarch.h"
21 #include "regcache.h"
22 
23 #include "i386-tdep.h"
24 #include "amd64-tdep.h"
25 #include "amd64-nat.h"
26 
27 /* The following bits of code help with implementing debugging 32-bit
28    code natively on AMD64.  The idea is to define two mappings between
29    the register number as used by GDB and the register set used by the
30    host to represent the general-purpose registers; one for 32-bit
31    code and one for 64-bit code.  The mappings are specified by the
32    following variables and consist of an array of offsets within the
33    register set indexed by register number, and the number of
34    registers supported by the mapping.  We don't need mappings for the
35    floating-point and SSE registers, since the difference between
36    64-bit and 32-bit variants are negligible.  The difference in the
37    number of SSE registers is already handled by the target code.  */
38 
39 /* General-purpose register mapping for native 32-bit code.  */
40 int *amd64_native_gregset32_reg_offset;
41 int amd64_native_gregset32_num_regs = I386_NUM_GREGS;
42 
43 /* General-purpose register mapping for native 64-bit code.  */
44 int *amd64_native_gregset64_reg_offset;
45 int amd64_native_gregset64_num_regs = AMD64_NUM_GREGS;
46 
47 /* Return the offset of REGNUM within the appropriate native
48    general-purpose register set.  */
49 
50 static int
51 amd64_native_gregset_reg_offset (struct gdbarch *gdbarch, int regnum)
52 {
53   int *reg_offset = amd64_native_gregset64_reg_offset;
54   int num_regs = amd64_native_gregset64_num_regs;
55 
56   gdb_assert (regnum >= 0);
57 
58   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
59     {
60       reg_offset = amd64_native_gregset32_reg_offset;
61       num_regs = amd64_native_gregset32_num_regs;
62     }
63 
64   if (num_regs > gdbarch_num_regs (gdbarch))
65     num_regs = gdbarch_num_regs (gdbarch);
66 
67   if (regnum >= num_regs)
68     return -1;
69 
70   return reg_offset[regnum];
71 }
72 
73 /* Return whether the native general-purpose register set supplies
74    register REGNUM.  */
75 
76 int
77 amd64_native_gregset_supplies_p (struct gdbarch *gdbarch, int regnum)
78 {
79   return (amd64_native_gregset_reg_offset (gdbarch, regnum) != -1);
80 }
81 
82 
83 /* Supply register REGNUM, whose contents are stored in GREGS, to
84    REGCACHE.  If REGNUM is -1, supply all appropriate registers.  */
85 
86 void
87 amd64_supply_native_gregset (struct regcache *regcache,
88 			     const void *gregs, int regnum)
89 {
90   const char *regs = (const char *) gregs;
91   struct gdbarch *gdbarch = regcache->arch ();
92   int num_regs = amd64_native_gregset64_num_regs;
93   int i;
94 
95   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
96     num_regs = amd64_native_gregset32_num_regs;
97 
98   if (num_regs > gdbarch_num_regs (gdbarch))
99     num_regs = gdbarch_num_regs (gdbarch);
100 
101   for (i = 0; i < num_regs; i++)
102     {
103       if (regnum == -1 || regnum == i)
104 	{
105 	  int offset = amd64_native_gregset_reg_offset (gdbarch, i);
106 
107 	  if (offset != -1)
108 	    regcache->raw_supply (i, regs + offset);
109 	}
110     }
111 }
112 
113 /* Collect register REGNUM from REGCACHE and store its contents in
114    GREGS.  If REGNUM is -1, collect and store all appropriate
115    registers.  */
116 
117 void
118 amd64_collect_native_gregset (const struct regcache *regcache,
119 			      void *gregs, int regnum)
120 {
121   char *regs = (char *) gregs;
122   struct gdbarch *gdbarch = regcache->arch ();
123   int num_regs = amd64_native_gregset64_num_regs;
124   int i;
125 
126   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
127     {
128       num_regs = amd64_native_gregset32_num_regs;
129 
130       /* Make sure %eax, %ebx, %ecx, %edx, %esi, %edi, %ebp, %esp and
131 	 %eip get zero-extended to 64 bits.  */
132       for (i = 0; i <= I386_EIP_REGNUM; i++)
133 	{
134 	  if (regnum == -1 || regnum == i)
135 	    memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
136 	}
137       /* Ditto for %cs, %ss, %ds, %es, %fs, and %gs.  */
138       for (i = I386_CS_REGNUM; i <= I386_GS_REGNUM; i++)
139 	{
140 	  if (regnum == -1 || regnum == i)
141 	    memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
142 	}
143     }
144 
145   if (num_regs > gdbarch_num_regs (gdbarch))
146     num_regs = gdbarch_num_regs (gdbarch);
147 
148   for (i = 0; i < num_regs; i++)
149     {
150       if (regnum == -1 || regnum == i)
151 	{
152 	  int offset = amd64_native_gregset_reg_offset (gdbarch, i);
153 
154 	  if (offset != -1)
155 	    regcache->raw_collect (i, regs + offset);
156 	}
157     }
158 }
159