Share fork_inferior et al with gdbserver
[external/binutils.git] / gdb / gdbserver / lynx-i386-low.c
1 /* Copyright (C) 2010-2017 Free Software Foundation, Inc.
2
3    This file is part of GDB.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include "server.h"
19 #include "lynx-low.h"
20 #include <limits.h>
21 #include <sys/ptrace.h>
22
23 /* The following two typedefs are defined in a .h file which is not
24    in the standard include path (/sys/include/family/x86/ucontext.h),
25    so we just duplicate them here.  */
26
27 /* General register context */
28 typedef struct usr_econtext {
29
30     uint32_t    uec_fault;
31     uint32_t    uec_es;
32     uint32_t    uec_ds;
33     uint32_t    uec_edi;
34     uint32_t    uec_esi;
35     uint32_t    uec_ebp;
36     uint32_t    uec_temp;
37     uint32_t    uec_ebx;
38     uint32_t    uec_edx;
39     uint32_t    uec_ecx;
40     uint32_t    uec_eax;
41     uint32_t    uec_inum;
42     uint32_t    uec_ecode;
43     uint32_t    uec_eip;
44     uint32_t    uec_cs;
45     uint32_t    uec_eflags;
46     uint32_t    uec_esp;
47     uint32_t    uec_ss;
48     uint32_t    uec_fs;
49     uint32_t    uec_gs;
50 } usr_econtext_t;
51
52 /* Floating point and SIMD register context */
53 typedef struct usr_fcontext {
54         uint16_t         ufc_control;
55         uint16_t         ufc_status;
56         uint16_t         ufc_tag;
57         uint16_t         ufc_opcode;
58         uint8_t         *ufc_inst_off;
59         uint32_t         ufc_inst_sel;
60         uint8_t         *ufc_data_off;
61         uint32_t         ufc_data_sel;
62         uint32_t         usse_mxcsr;
63         uint32_t         usse_mxcsr_mask;
64         struct ufp387_real {
65                 uint16_t umant4;
66                 uint16_t umant3;
67                 uint16_t umant2;
68                 uint16_t umant1;
69                 uint16_t us_and_e;
70                 uint16_t ureserved_1;
71                 uint16_t ureserved_2;
72                 uint16_t ureserved_3;
73         } ufc_reg[8];
74         struct uxmm_register {
75                 uint16_t uchunk_1;
76                 uint16_t uchunk_2;
77                 uint16_t uchunk_3;
78                 uint16_t uchunk_4;
79                 uint16_t uchunk_5;
80                 uint16_t uchunk_6;
81                 uint16_t uchunk_7;
82                 uint16_t uchunk_8;
83         } uxmm_reg[8];
84         char ureserved[16][14];
85 } usr_fcontext_t;
86
87 /* The index of various registers inside the regcache.  */
88
89 enum lynx_i386_gdb_regnum
90 {
91   I386_EAX_REGNUM,
92   I386_ECX_REGNUM,
93   I386_EDX_REGNUM,
94   I386_EBX_REGNUM,
95   I386_ESP_REGNUM,
96   I386_EBP_REGNUM,
97   I386_ESI_REGNUM,
98   I386_EDI_REGNUM,
99   I386_EIP_REGNUM,
100   I386_EFLAGS_REGNUM,
101   I386_CS_REGNUM,
102   I386_SS_REGNUM,
103   I386_DS_REGNUM,
104   I386_ES_REGNUM,
105   I386_FS_REGNUM,
106   I386_GS_REGNUM,
107   I386_ST0_REGNUM,
108   I386_FCTRL_REGNUM = I386_ST0_REGNUM + 8,
109   I386_FSTAT_REGNUM,
110   I386_FTAG_REGNUM,
111   I386_FISEG_REGNUM,
112   I386_FIOFF_REGNUM,
113   I386_FOSEG_REGNUM,
114   I386_FOOFF_REGNUM,
115   I386_FOP_REGNUM,
116   I386_XMM0_REGNUM = 32,
117   I386_MXCSR_REGNUM = I386_XMM0_REGNUM + 8,
118   I386_SENTINEL_REGUM
119 };
120
121 /* Defined in auto-generated file i386.c.  */
122 extern void init_registers_i386 (void);
123 extern const struct target_desc *tdesc_i386;
124
125 /* The fill_function for the general-purpose register set.  */
126
127 static void
128 lynx_i386_fill_gregset (struct regcache *regcache, char *buf)
129 {
130 #define lynx_i386_collect_gp(regnum, fld) \
131   collect_register (regcache, regnum, \
132                     buf + offsetof (usr_econtext_t, uec_##fld))
133
134   lynx_i386_collect_gp (I386_EAX_REGNUM, eax);
135   lynx_i386_collect_gp (I386_ECX_REGNUM, ecx);
136   lynx_i386_collect_gp (I386_EDX_REGNUM, edx);
137   lynx_i386_collect_gp (I386_EBX_REGNUM, ebx);
138   lynx_i386_collect_gp (I386_ESP_REGNUM, esp);
139   lynx_i386_collect_gp (I386_EBP_REGNUM, ebp);
140   lynx_i386_collect_gp (I386_ESI_REGNUM, esi);
141   lynx_i386_collect_gp (I386_EDI_REGNUM, edi);
142   lynx_i386_collect_gp (I386_EIP_REGNUM, eip);
143   lynx_i386_collect_gp (I386_EFLAGS_REGNUM, eflags);
144   lynx_i386_collect_gp (I386_CS_REGNUM, cs);
145   lynx_i386_collect_gp (I386_SS_REGNUM, ss);
146   lynx_i386_collect_gp (I386_DS_REGNUM, ds);
147   lynx_i386_collect_gp (I386_ES_REGNUM, es);
148   lynx_i386_collect_gp (I386_FS_REGNUM, fs);
149   lynx_i386_collect_gp (I386_GS_REGNUM, gs);
150 }
151
152 /* The store_function for the general-purpose register set.  */
153
154 static void
155 lynx_i386_store_gregset (struct regcache *regcache, const char *buf)
156 {
157 #define lynx_i386_supply_gp(regnum, fld) \
158   supply_register (regcache, regnum, \
159                    buf + offsetof (usr_econtext_t, uec_##fld))
160
161   lynx_i386_supply_gp (I386_EAX_REGNUM, eax);
162   lynx_i386_supply_gp (I386_ECX_REGNUM, ecx);
163   lynx_i386_supply_gp (I386_EDX_REGNUM, edx);
164   lynx_i386_supply_gp (I386_EBX_REGNUM, ebx);
165   lynx_i386_supply_gp (I386_ESP_REGNUM, esp);
166   lynx_i386_supply_gp (I386_EBP_REGNUM, ebp);
167   lynx_i386_supply_gp (I386_ESI_REGNUM, esi);
168   lynx_i386_supply_gp (I386_EDI_REGNUM, edi);
169   lynx_i386_supply_gp (I386_EIP_REGNUM, eip);
170   lynx_i386_supply_gp (I386_EFLAGS_REGNUM, eflags);
171   lynx_i386_supply_gp (I386_CS_REGNUM, cs);
172   lynx_i386_supply_gp (I386_SS_REGNUM, ss);
173   lynx_i386_supply_gp (I386_DS_REGNUM, ds);
174   lynx_i386_supply_gp (I386_ES_REGNUM, es);
175   lynx_i386_supply_gp (I386_FS_REGNUM, fs);
176   lynx_i386_supply_gp (I386_GS_REGNUM, gs);
177 }
178
179 /* Extract the first 16 bits of register REGNUM in the REGCACHE,
180    and store these 2 bytes at DEST.
181
182    This is useful to collect certain 16bit registers which are known
183    by GDBserver as 32bit registers (such as the Control Register
184    for instance).  */
185
186 static void
187 collect_16bit_register (struct regcache *regcache, int regnum, char *dest)
188 {
189   gdb_byte word[4];
190
191   collect_register (regcache, regnum, word);
192   memcpy (dest, word, 2);
193 }
194
195 /* The fill_function for the floating-point register set.  */
196
197 static void
198 lynx_i386_fill_fpregset (struct regcache *regcache, char *buf)
199 {
200   int i;
201
202   /* Collect %st0 .. %st7.  */
203   for (i = 0; i < 8; i++)
204     collect_register (regcache, I386_ST0_REGNUM + i,
205                       buf + offsetof (usr_fcontext_t, ufc_reg)
206                       + i * sizeof (struct ufp387_real));
207
208   /* Collect the other FPU registers.  */
209   collect_16bit_register (regcache, I386_FCTRL_REGNUM,
210                           buf + offsetof (usr_fcontext_t, ufc_control));
211   collect_16bit_register (regcache, I386_FSTAT_REGNUM,
212                           buf + offsetof (usr_fcontext_t, ufc_status));
213   collect_16bit_register (regcache, I386_FTAG_REGNUM,
214                           buf + offsetof (usr_fcontext_t, ufc_tag));
215   collect_register (regcache, I386_FISEG_REGNUM,
216                     buf + offsetof (usr_fcontext_t, ufc_inst_sel));
217   collect_register (regcache, I386_FIOFF_REGNUM,
218                     buf + offsetof (usr_fcontext_t, ufc_inst_off));
219   collect_register (regcache, I386_FOSEG_REGNUM,
220                     buf + offsetof (usr_fcontext_t, ufc_data_sel));
221   collect_register (regcache, I386_FOOFF_REGNUM,
222                     buf + offsetof (usr_fcontext_t, ufc_data_off));
223   collect_16bit_register (regcache, I386_FOP_REGNUM,
224                           buf + offsetof (usr_fcontext_t, ufc_opcode));
225
226   /* Collect the XMM registers.  */
227   for (i = 0; i < 8; i++)
228     collect_register (regcache, I386_XMM0_REGNUM + i,
229                       buf + offsetof (usr_fcontext_t, uxmm_reg)
230                       + i * sizeof (struct uxmm_register));
231   collect_register (regcache, I386_MXCSR_REGNUM,
232                     buf + offsetof (usr_fcontext_t, usse_mxcsr));
233 }
234
235 /* This is the supply counterpart for collect_16bit_register:
236    It extracts a 2byte value from BUF, and uses that value to
237    set REGNUM's value in the regcache.
238
239    This is useful to supply the value of certain 16bit registers
240    which are known by GDBserver as 32bit registers (such as the Control
241    Register for instance).  */
242
243 static void
244 supply_16bit_register (struct regcache *regcache, int regnum, const char *buf)
245 {
246   gdb_byte word[4];
247
248   memcpy (word, buf, 2);
249   memset (word + 2, 0, 2);
250   supply_register (regcache, regnum, word);
251 }
252
253 /* The store_function for the floating-point register set.  */
254
255 static void
256 lynx_i386_store_fpregset (struct regcache *regcache, const char *buf)
257 {
258   int i;
259
260   /* Store the %st0 .. %st7 registers.  */
261   for (i = 0; i < 8; i++)
262     supply_register (regcache, I386_ST0_REGNUM + i,
263                      buf + offsetof (usr_fcontext_t, ufc_reg)
264                      + i * sizeof (struct ufp387_real));
265
266   /* Store the other FPU registers.  */
267   supply_16bit_register (regcache, I386_FCTRL_REGNUM,
268                          buf + offsetof (usr_fcontext_t, ufc_control));
269   supply_16bit_register (regcache, I386_FSTAT_REGNUM,
270                          buf + offsetof (usr_fcontext_t, ufc_status));
271   supply_16bit_register (regcache, I386_FTAG_REGNUM,
272                          buf + offsetof (usr_fcontext_t, ufc_tag));
273   supply_register (regcache, I386_FISEG_REGNUM,
274                    buf + offsetof (usr_fcontext_t, ufc_inst_sel));
275   supply_register (regcache, I386_FIOFF_REGNUM,
276                    buf + offsetof (usr_fcontext_t, ufc_inst_off));
277   supply_register (regcache, I386_FOSEG_REGNUM,
278                    buf + offsetof (usr_fcontext_t, ufc_data_sel));
279   supply_register (regcache, I386_FOOFF_REGNUM,
280                    buf + offsetof (usr_fcontext_t, ufc_data_off));
281   supply_16bit_register (regcache, I386_FOP_REGNUM,
282                          buf + offsetof (usr_fcontext_t, ufc_opcode));
283
284   /* Store the XMM registers.  */
285   for (i = 0; i < 8; i++)
286     supply_register (regcache, I386_XMM0_REGNUM + i,
287                      buf + offsetof (usr_fcontext_t, uxmm_reg)
288                      + i * sizeof (struct uxmm_register));
289   supply_register (regcache, I386_MXCSR_REGNUM,
290                    buf + offsetof (usr_fcontext_t, usse_mxcsr));
291 }
292
293 /* Implements the lynx_target_ops.arch_setup routine.  */
294
295 static void
296 lynx_i386_arch_setup (void)
297 {
298   init_registers_i386 ();
299   lynx_tdesc = tdesc_i386;
300 }
301
302 /* Description of all the x86-lynx register sets.  */
303
304 struct lynx_regset_info lynx_target_regsets[] = {
305   /* General Purpose Registers.  */
306   {PTRACE_GETREGS, PTRACE_SETREGS, sizeof(usr_econtext_t),
307    lynx_i386_fill_gregset, lynx_i386_store_gregset},
308   /* Floating Point Registers.  */
309   { PTRACE_GETFPREGS, PTRACE_SETFPREGS, sizeof(usr_fcontext_t),
310     lynx_i386_fill_fpregset, lynx_i386_store_fpregset },
311   /* End of list marker.  */
312   {0, 0, -1, NULL, NULL }
313 };
314
315 /* The lynx_target_ops vector for x86-lynx.  */
316
317 struct lynx_target_ops the_low_target = {
318   lynx_i386_arch_setup,
319 };