gdb/x86: Handle kernels using compact xsave format
[external/binutils.git] / gdb / gdbserver / regcache.c
1 /* Register support routines for the remote server for GDB.
2    Copyright (C) 2001-2018 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "server.h"
20 #include "regdef.h"
21 #include "gdbthread.h"
22 #include "tdesc.h"
23 #include "rsp-low.h"
24 #ifndef IN_PROCESS_AGENT
25
26 struct regcache *
27 get_thread_regcache (struct thread_info *thread, int fetch)
28 {
29   struct regcache *regcache;
30
31   regcache = thread_regcache_data (thread);
32
33   /* Threads' regcaches are created lazily, because biarch targets add
34      the main thread/lwp before seeing it stop for the first time, and
35      it is only after the target sees the thread stop for the first
36      time that the target has a chance of determining the process's
37      architecture.  IOW, when we first add the process's main thread
38      we don't know which architecture/tdesc its regcache should
39      have.  */
40   if (regcache == NULL)
41     {
42       struct process_info *proc = get_thread_process (thread);
43
44       gdb_assert (proc->tdesc != NULL);
45
46       regcache = new_register_cache (proc->tdesc);
47       set_thread_regcache_data (thread, regcache);
48     }
49
50   if (fetch && regcache->registers_valid == 0)
51     {
52       struct thread_info *saved_thread = current_thread;
53
54       current_thread = thread;
55       /* Invalidate all registers, to prevent stale left-overs.  */
56       memset (regcache->register_status, REG_UNAVAILABLE,
57               regcache->tdesc->reg_defs.size ());
58       fetch_inferior_registers (regcache, -1);
59       current_thread = saved_thread;
60       regcache->registers_valid = 1;
61     }
62
63   return regcache;
64 }
65
66 /* See common/common-regcache.h.  */
67
68 struct regcache *
69 get_thread_regcache_for_ptid (ptid_t ptid)
70 {
71   return get_thread_regcache (find_thread_ptid (ptid), 1);
72 }
73
74 void
75 regcache_invalidate_thread (struct thread_info *thread)
76 {
77   struct regcache *regcache;
78
79   regcache = thread_regcache_data (thread);
80
81   if (regcache == NULL)
82     return;
83
84   if (regcache->registers_valid)
85     {
86       struct thread_info *saved_thread = current_thread;
87
88       current_thread = thread;
89       store_inferior_registers (regcache, -1);
90       current_thread = saved_thread;
91     }
92
93   regcache->registers_valid = 0;
94 }
95
96 /* See regcache.h.  */
97
98 void
99 regcache_invalidate_pid (int pid)
100 {
101   /* Only invalidate the regcaches of threads of this process.  */
102   for_each_thread (pid, regcache_invalidate_thread);
103 }
104
105 /* See regcache.h.  */
106
107 void
108 regcache_invalidate (void)
109 {
110   /* Only update the threads of the current process.  */
111   int pid = current_thread->id.pid ();
112
113   regcache_invalidate_pid (pid);
114 }
115
116 #endif
117
118 struct regcache *
119 init_register_cache (struct regcache *regcache,
120                      const struct target_desc *tdesc,
121                      unsigned char *regbuf)
122 {
123   if (regbuf == NULL)
124     {
125 #ifndef IN_PROCESS_AGENT
126       /* Make sure to zero-initialize the register cache when it is
127          created, in case there are registers the target never
128          fetches.  This way they'll read as zero instead of
129          garbage.  */
130       regcache->tdesc = tdesc;
131       regcache->registers
132         = (unsigned char *) xcalloc (1, tdesc->registers_size);
133       regcache->registers_owned = 1;
134       regcache->register_status
135         = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
136       memset ((void *) regcache->register_status, REG_UNAVAILABLE,
137               tdesc->reg_defs.size ());
138 #else
139       gdb_assert_not_reached ("can't allocate memory from the heap");
140 #endif
141     }
142   else
143     {
144       regcache->tdesc = tdesc;
145       regcache->registers = regbuf;
146       regcache->registers_owned = 0;
147 #ifndef IN_PROCESS_AGENT
148       regcache->register_status = NULL;
149 #endif
150     }
151
152   regcache->registers_valid = 0;
153
154   return regcache;
155 }
156
157 #ifndef IN_PROCESS_AGENT
158
159 struct regcache *
160 new_register_cache (const struct target_desc *tdesc)
161 {
162   struct regcache *regcache = XCNEW (struct regcache);
163
164   gdb_assert (tdesc->registers_size != 0);
165
166   return init_register_cache (regcache, tdesc, NULL);
167 }
168
169 void
170 free_register_cache (struct regcache *regcache)
171 {
172   if (regcache)
173     {
174       if (regcache->registers_owned)
175         free (regcache->registers);
176       free (regcache->register_status);
177       free (regcache);
178     }
179 }
180
181 #endif
182
183 void
184 regcache_cpy (struct regcache *dst, struct regcache *src)
185 {
186   gdb_assert (src != NULL && dst != NULL);
187   gdb_assert (src->tdesc == dst->tdesc);
188   gdb_assert (src != dst);
189
190   memcpy (dst->registers, src->registers, src->tdesc->registers_size);
191 #ifndef IN_PROCESS_AGENT
192   if (dst->register_status != NULL && src->register_status != NULL)
193     memcpy (dst->register_status, src->register_status,
194             src->tdesc->reg_defs.size ());
195 #endif
196   dst->registers_valid = src->registers_valid;
197 }
198
199 /* Return a reference to the description of register N.  */
200
201 static const struct reg &
202 find_register_by_number (const struct target_desc *tdesc, int n)
203 {
204   return tdesc->reg_defs[n];
205 }
206
207 #ifndef IN_PROCESS_AGENT
208
209 void
210 registers_to_string (struct regcache *regcache, char *buf)
211 {
212   unsigned char *registers = regcache->registers;
213   const struct target_desc *tdesc = regcache->tdesc;
214
215   for (int i = 0; i < tdesc->reg_defs.size (); ++i)
216     {
217       if (regcache->register_status[i] == REG_VALID)
218         {
219           bin2hex (registers, buf, register_size (tdesc, i));
220           buf += register_size (tdesc, i) * 2;
221         }
222       else
223         {
224           memset (buf, 'x', register_size (tdesc, i) * 2);
225           buf += register_size (tdesc, i) * 2;
226         }
227       registers += register_size (tdesc, i);
228     }
229   *buf = '\0';
230 }
231
232 void
233 registers_from_string (struct regcache *regcache, char *buf)
234 {
235   int len = strlen (buf);
236   unsigned char *registers = regcache->registers;
237   const struct target_desc *tdesc = regcache->tdesc;
238
239   if (len != tdesc->registers_size * 2)
240     {
241       warning ("Wrong sized register packet (expected %d bytes, got %d)",
242                2 * tdesc->registers_size, len);
243       if (len > tdesc->registers_size * 2)
244         len = tdesc->registers_size * 2;
245     }
246   hex2bin (buf, registers, len / 2);
247 }
248
249 int
250 find_regno (const struct target_desc *tdesc, const char *name)
251 {
252   for (int i = 0; i < tdesc->reg_defs.size (); ++i)
253     {
254       if (strcmp (name, find_register_by_number (tdesc, i).name) == 0)
255         return i;
256     }
257   internal_error (__FILE__, __LINE__, "Unknown register %s requested",
258                   name);
259 }
260
261 static void
262 free_register_cache_thread (struct thread_info *thread)
263 {
264   struct regcache *regcache = thread_regcache_data (thread);
265
266   if (regcache != NULL)
267     {
268       regcache_invalidate_thread (thread);
269       free_register_cache (regcache);
270       set_thread_regcache_data (thread, NULL);
271     }
272 }
273
274 void
275 regcache_release (void)
276 {
277   /* Flush and release all pre-existing register caches.  */
278   for_each_thread (free_register_cache_thread);
279 }
280 #endif
281
282 int
283 register_cache_size (const struct target_desc *tdesc)
284 {
285   return tdesc->registers_size;
286 }
287
288 int
289 register_size (const struct target_desc *tdesc, int n)
290 {
291   return find_register_by_number (tdesc, n).size / 8;
292 }
293
294 /* See common/common-regcache.h.  */
295
296 int
297 regcache_register_size (const struct regcache *regcache, int n)
298 {
299   return register_size (regcache->tdesc, n);
300 }
301
302 static unsigned char *
303 register_data (struct regcache *regcache, int n, int fetch)
304 {
305   return (regcache->registers
306           + find_register_by_number (regcache->tdesc, n).offset / 8);
307 }
308
309 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
310    If BUF is NULL, the register's value is recorded as
311    unavailable.  */
312
313 void
314 supply_register (struct regcache *regcache, int n, const void *buf)
315 {
316   if (buf)
317     {
318       memcpy (register_data (regcache, n, 0), buf,
319               register_size (regcache->tdesc, n));
320 #ifndef IN_PROCESS_AGENT
321       if (regcache->register_status != NULL)
322         regcache->register_status[n] = REG_VALID;
323 #endif
324     }
325   else
326     {
327       memset (register_data (regcache, n, 0), 0,
328               register_size (regcache->tdesc, n));
329 #ifndef IN_PROCESS_AGENT
330       if (regcache->register_status != NULL)
331         regcache->register_status[n] = REG_UNAVAILABLE;
332 #endif
333     }
334 }
335
336 /* Supply register N with value zero to REGCACHE.  */
337
338 void
339 supply_register_zeroed (struct regcache *regcache, int n)
340 {
341   memset (register_data (regcache, n, 0), 0,
342           register_size (regcache->tdesc, n));
343 #ifndef IN_PROCESS_AGENT
344   if (regcache->register_status != NULL)
345     regcache->register_status[n] = REG_VALID;
346 #endif
347 }
348
349 #ifndef IN_PROCESS_AGENT
350
351 /* Supply register called NAME with value zero to REGCACHE.  */
352
353 void
354 supply_register_by_name_zeroed (struct regcache *regcache,
355                                 const char *name)
356 {
357   supply_register_zeroed (regcache, find_regno (regcache->tdesc, name));
358 }
359
360 #endif
361
362 /* Supply the whole register set whose contents are stored in BUF, to
363    REGCACHE.  If BUF is NULL, all the registers' values are recorded
364    as unavailable.  */
365
366 void
367 supply_regblock (struct regcache *regcache, const void *buf)
368 {
369   if (buf)
370     {
371       const struct target_desc *tdesc = regcache->tdesc;
372
373       memcpy (regcache->registers, buf, tdesc->registers_size);
374 #ifndef IN_PROCESS_AGENT
375       {
376         int i;
377
378         for (i = 0; i < tdesc->reg_defs.size (); i++)
379           regcache->register_status[i] = REG_VALID;
380       }
381 #endif
382     }
383   else
384     {
385       const struct target_desc *tdesc = regcache->tdesc;
386
387       memset (regcache->registers, 0, tdesc->registers_size);
388 #ifndef IN_PROCESS_AGENT
389       {
390         int i;
391
392         for (i = 0; i < tdesc->reg_defs.size (); i++)
393           regcache->register_status[i] = REG_UNAVAILABLE;
394       }
395 #endif
396     }
397 }
398
399 #ifndef IN_PROCESS_AGENT
400
401 void
402 supply_register_by_name (struct regcache *regcache,
403                          const char *name, const void *buf)
404 {
405   supply_register (regcache, find_regno (regcache->tdesc, name), buf);
406 }
407
408 #endif
409
410 void
411 collect_register (struct regcache *regcache, int n, void *buf)
412 {
413   memcpy (buf, register_data (regcache, n, 1),
414           register_size (regcache->tdesc, n));
415 }
416
417 enum register_status
418 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
419                             ULONGEST *val)
420 {
421   int size;
422
423   gdb_assert (regcache != NULL);
424   gdb_assert (regnum >= 0
425               && regnum < regcache->tdesc->reg_defs.size ());
426
427   size = register_size (regcache->tdesc, regnum);
428
429   if (size > (int) sizeof (ULONGEST))
430     error (_("That operation is not available on integers of more than"
431             "%d bytes."),
432           (int) sizeof (ULONGEST));
433
434   *val = 0;
435   collect_register (regcache, regnum, val);
436
437   return REG_VALID;
438 }
439
440 #ifndef IN_PROCESS_AGENT
441
442 void
443 collect_register_as_string (struct regcache *regcache, int n, char *buf)
444 {
445   bin2hex (register_data (regcache, n, 1), buf,
446            register_size (regcache->tdesc, n));
447 }
448
449 void
450 collect_register_by_name (struct regcache *regcache,
451                           const char *name, void *buf)
452 {
453   collect_register (regcache, find_regno (regcache->tdesc, name), buf);
454 }
455
456 /* Special handling for register PC.  */
457
458 CORE_ADDR
459 regcache_read_pc (struct regcache *regcache)
460 {
461   CORE_ADDR pc_val;
462
463   if (the_target->read_pc)
464     pc_val = the_target->read_pc (regcache);
465   else
466     internal_error (__FILE__, __LINE__,
467                     "regcache_read_pc: Unable to find PC");
468
469   return pc_val;
470 }
471
472 void
473 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
474 {
475   if (the_target->write_pc)
476     the_target->write_pc (regcache, pc);
477   else
478     internal_error (__FILE__, __LINE__,
479                     "regcache_write_pc: Unable to update PC");
480 }
481
482 #endif