Commit | Line | Data |
---|---|---|
d0722149 DE |
1 | /* GNU/Linux/x86-64 specific low level interface, for the remote server |
2 | for GDB. | |
4c38e0a4 | 3 | Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
d0722149 DE |
4 | Free Software Foundation, Inc. |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
aa5ca48f | 21 | #include <stddef.h> |
d0722149 DE |
22 | #include <signal.h> |
23 | #include "server.h" | |
24 | #include "linux-low.h" | |
25 | #include "i387-fp.h" | |
aa5ca48f | 26 | #include "i386-low.h" |
1570b33e L |
27 | #include "i386-xstate.h" |
28 | #include "elf/common.h" | |
d0722149 DE |
29 | |
30 | #include "gdb_proc_service.h" | |
31 | ||
90884b2b | 32 | /* Defined in auto-generated file i386-linux.c. */ |
d0722149 | 33 | void init_registers_i386_linux (void); |
90884b2b L |
34 | /* Defined in auto-generated file amd64-linux.c. */ |
35 | void init_registers_amd64_linux (void); | |
1570b33e L |
36 | /* Defined in auto-generated file i386-avx-linux.c. */ |
37 | void init_registers_i386_avx_linux (void); | |
38 | /* Defined in auto-generated file amd64-avx-linux.c. */ | |
39 | void init_registers_amd64_avx_linux (void); | |
3a13a53b L |
40 | /* Defined in auto-generated file i386-mmx-linux.c. */ |
41 | void init_registers_i386_mmx_linux (void); | |
1570b33e L |
42 | |
43 | /* Backward compatibility for gdb without XML support. */ | |
44 | ||
45 | static const char *xmltarget_i386_linux_no_xml = "@<target>\ | |
46 | <architecture>i386</architecture>\ | |
47 | <osabi>GNU/Linux</osabi>\ | |
48 | </target>"; | |
49 | static const char *xmltarget_amd64_linux_no_xml = "@<target>\ | |
50 | <architecture>i386:x86-64</architecture>\ | |
51 | <osabi>GNU/Linux</osabi>\ | |
52 | </target>"; | |
d0722149 DE |
53 | |
54 | #include <sys/reg.h> | |
55 | #include <sys/procfs.h> | |
56 | #include <sys/ptrace.h> | |
1570b33e L |
57 | #include <sys/uio.h> |
58 | ||
59 | #ifndef PTRACE_GETREGSET | |
60 | #define PTRACE_GETREGSET 0x4204 | |
61 | #endif | |
62 | ||
63 | #ifndef PTRACE_SETREGSET | |
64 | #define PTRACE_SETREGSET 0x4205 | |
65 | #endif | |
66 | ||
d0722149 DE |
67 | |
68 | #ifndef PTRACE_GET_THREAD_AREA | |
69 | #define PTRACE_GET_THREAD_AREA 25 | |
70 | #endif | |
71 | ||
72 | /* This definition comes from prctl.h, but some kernels may not have it. */ | |
73 | #ifndef PTRACE_ARCH_PRCTL | |
74 | #define PTRACE_ARCH_PRCTL 30 | |
75 | #endif | |
76 | ||
77 | /* The following definitions come from prctl.h, but may be absent | |
78 | for certain configurations. */ | |
79 | #ifndef ARCH_GET_FS | |
80 | #define ARCH_SET_GS 0x1001 | |
81 | #define ARCH_SET_FS 0x1002 | |
82 | #define ARCH_GET_FS 0x1003 | |
83 | #define ARCH_GET_GS 0x1004 | |
84 | #endif | |
85 | ||
aa5ca48f DE |
86 | /* Per-process arch-specific data we want to keep. */ |
87 | ||
88 | struct arch_process_info | |
89 | { | |
90 | struct i386_debug_reg_state debug_reg_state; | |
91 | }; | |
92 | ||
93 | /* Per-thread arch-specific data we want to keep. */ | |
94 | ||
95 | struct arch_lwp_info | |
96 | { | |
97 | /* Non-zero if our copy differs from what's recorded in the thread. */ | |
98 | int debug_registers_changed; | |
99 | }; | |
100 | ||
d0722149 DE |
101 | #ifdef __x86_64__ |
102 | ||
103 | /* Mapping between the general-purpose registers in `struct user' | |
104 | format and GDB's register array layout. | |
105 | Note that the transfer layout uses 64-bit regs. */ | |
106 | static /*const*/ int i386_regmap[] = | |
107 | { | |
108 | RAX * 8, RCX * 8, RDX * 8, RBX * 8, | |
109 | RSP * 8, RBP * 8, RSI * 8, RDI * 8, | |
110 | RIP * 8, EFLAGS * 8, CS * 8, SS * 8, | |
111 | DS * 8, ES * 8, FS * 8, GS * 8 | |
112 | }; | |
113 | ||
114 | #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0])) | |
115 | ||
116 | /* So code below doesn't have to care, i386 or amd64. */ | |
117 | #define ORIG_EAX ORIG_RAX | |
118 | ||
119 | static const int x86_64_regmap[] = | |
120 | { | |
121 | RAX * 8, RBX * 8, RCX * 8, RDX * 8, | |
122 | RSI * 8, RDI * 8, RBP * 8, RSP * 8, | |
123 | R8 * 8, R9 * 8, R10 * 8, R11 * 8, | |
124 | R12 * 8, R13 * 8, R14 * 8, R15 * 8, | |
125 | RIP * 8, EFLAGS * 8, CS * 8, SS * 8, | |
126 | DS * 8, ES * 8, FS * 8, GS * 8, | |
127 | -1, -1, -1, -1, -1, -1, -1, -1, | |
128 | -1, -1, -1, -1, -1, -1, -1, -1, | |
129 | -1, -1, -1, -1, -1, -1, -1, -1, | |
130 | -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
131 | ORIG_RAX * 8 | |
132 | }; | |
133 | ||
134 | #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0])) | |
135 | ||
136 | #else /* ! __x86_64__ */ | |
137 | ||
138 | /* Mapping between the general-purpose registers in `struct user' | |
139 | format and GDB's register array layout. */ | |
140 | static /*const*/ int i386_regmap[] = | |
141 | { | |
142 | EAX * 4, ECX * 4, EDX * 4, EBX * 4, | |
143 | UESP * 4, EBP * 4, ESI * 4, EDI * 4, | |
144 | EIP * 4, EFL * 4, CS * 4, SS * 4, | |
145 | DS * 4, ES * 4, FS * 4, GS * 4 | |
146 | }; | |
147 | ||
148 | #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0])) | |
149 | ||
150 | #endif | |
151 | \f | |
152 | /* Called by libthread_db. */ | |
153 | ||
154 | ps_err_e | |
155 | ps_get_thread_area (const struct ps_prochandle *ph, | |
156 | lwpid_t lwpid, int idx, void **base) | |
157 | { | |
158 | #ifdef __x86_64__ | |
159 | int use_64bit = register_size (0) == 8; | |
160 | ||
161 | if (use_64bit) | |
162 | { | |
163 | switch (idx) | |
164 | { | |
165 | case FS: | |
166 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0) | |
167 | return PS_OK; | |
168 | break; | |
169 | case GS: | |
170 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0) | |
171 | return PS_OK; | |
172 | break; | |
173 | default: | |
174 | return PS_BADADDR; | |
175 | } | |
176 | return PS_ERR; | |
177 | } | |
178 | #endif | |
179 | ||
180 | { | |
181 | unsigned int desc[4]; | |
182 | ||
183 | if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, | |
184 | (void *) (intptr_t) idx, (unsigned long) &desc) < 0) | |
185 | return PS_ERR; | |
186 | ||
187 | *(int *)base = desc[1]; | |
188 | return PS_OK; | |
189 | } | |
190 | } | |
191 | \f | |
192 | static int | |
193 | i386_cannot_store_register (int regno) | |
194 | { | |
195 | return regno >= I386_NUM_REGS; | |
196 | } | |
197 | ||
198 | static int | |
199 | i386_cannot_fetch_register (int regno) | |
200 | { | |
201 | return regno >= I386_NUM_REGS; | |
202 | } | |
203 | ||
204 | static void | |
442ea881 | 205 | x86_fill_gregset (struct regcache *regcache, void *buf) |
d0722149 DE |
206 | { |
207 | int i; | |
208 | ||
209 | #ifdef __x86_64__ | |
210 | if (register_size (0) == 8) | |
211 | { | |
212 | for (i = 0; i < X86_64_NUM_REGS; i++) | |
213 | if (x86_64_regmap[i] != -1) | |
442ea881 | 214 | collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]); |
d0722149 DE |
215 | return; |
216 | } | |
217 | #endif | |
218 | ||
219 | for (i = 0; i < I386_NUM_REGS; i++) | |
442ea881 | 220 | collect_register (regcache, i, ((char *) buf) + i386_regmap[i]); |
d0722149 | 221 | |
442ea881 PA |
222 | collect_register_by_name (regcache, "orig_eax", |
223 | ((char *) buf) + ORIG_EAX * 4); | |
d0722149 DE |
224 | } |
225 | ||
226 | static void | |
442ea881 | 227 | x86_store_gregset (struct regcache *regcache, const void *buf) |
d0722149 DE |
228 | { |
229 | int i; | |
230 | ||
231 | #ifdef __x86_64__ | |
232 | if (register_size (0) == 8) | |
233 | { | |
234 | for (i = 0; i < X86_64_NUM_REGS; i++) | |
235 | if (x86_64_regmap[i] != -1) | |
442ea881 | 236 | supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]); |
d0722149 DE |
237 | return; |
238 | } | |
239 | #endif | |
240 | ||
241 | for (i = 0; i < I386_NUM_REGS; i++) | |
442ea881 | 242 | supply_register (regcache, i, ((char *) buf) + i386_regmap[i]); |
d0722149 | 243 | |
442ea881 PA |
244 | supply_register_by_name (regcache, "orig_eax", |
245 | ((char *) buf) + ORIG_EAX * 4); | |
d0722149 DE |
246 | } |
247 | ||
248 | static void | |
442ea881 | 249 | x86_fill_fpregset (struct regcache *regcache, void *buf) |
d0722149 DE |
250 | { |
251 | #ifdef __x86_64__ | |
442ea881 | 252 | i387_cache_to_fxsave (regcache, buf); |
d0722149 | 253 | #else |
442ea881 | 254 | i387_cache_to_fsave (regcache, buf); |
d0722149 DE |
255 | #endif |
256 | } | |
257 | ||
258 | static void | |
442ea881 | 259 | x86_store_fpregset (struct regcache *regcache, const void *buf) |
d0722149 DE |
260 | { |
261 | #ifdef __x86_64__ | |
442ea881 | 262 | i387_fxsave_to_cache (regcache, buf); |
d0722149 | 263 | #else |
442ea881 | 264 | i387_fsave_to_cache (regcache, buf); |
d0722149 DE |
265 | #endif |
266 | } | |
267 | ||
268 | #ifndef __x86_64__ | |
269 | ||
270 | static void | |
442ea881 | 271 | x86_fill_fpxregset (struct regcache *regcache, void *buf) |
d0722149 | 272 | { |
442ea881 | 273 | i387_cache_to_fxsave (regcache, buf); |
d0722149 DE |
274 | } |
275 | ||
276 | static void | |
442ea881 | 277 | x86_store_fpxregset (struct regcache *regcache, const void *buf) |
d0722149 | 278 | { |
442ea881 | 279 | i387_fxsave_to_cache (regcache, buf); |
d0722149 DE |
280 | } |
281 | ||
282 | #endif | |
283 | ||
1570b33e L |
284 | static void |
285 | x86_fill_xstateregset (struct regcache *regcache, void *buf) | |
286 | { | |
287 | i387_cache_to_xsave (regcache, buf); | |
288 | } | |
289 | ||
290 | static void | |
291 | x86_store_xstateregset (struct regcache *regcache, const void *buf) | |
292 | { | |
293 | i387_xsave_to_cache (regcache, buf); | |
294 | } | |
295 | ||
d0722149 DE |
296 | /* ??? The non-biarch i386 case stores all the i387 regs twice. |
297 | Once in i387_.*fsave.* and once in i387_.*fxsave.*. | |
298 | This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS | |
299 | doesn't work. IWBN to avoid the duplication in the case where it | |
300 | does work. Maybe the arch_setup routine could check whether it works | |
301 | and update target_regsets accordingly, maybe by moving target_regsets | |
302 | to linux_target_ops and set the right one there, rather than having to | |
303 | modify the target_regsets global. */ | |
304 | ||
305 | struct regset_info target_regsets[] = | |
306 | { | |
307 | #ifdef HAVE_PTRACE_GETREGS | |
1570b33e | 308 | { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t), |
d0722149 DE |
309 | GENERAL_REGS, |
310 | x86_fill_gregset, x86_store_gregset }, | |
1570b33e L |
311 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_X86_XSTATE, 0, |
312 | EXTENDED_REGS, x86_fill_xstateregset, x86_store_xstateregset }, | |
d0722149 DE |
313 | # ifndef __x86_64__ |
314 | # ifdef HAVE_PTRACE_GETFPXREGS | |
1570b33e | 315 | { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, 0, sizeof (elf_fpxregset_t), |
d0722149 DE |
316 | EXTENDED_REGS, |
317 | x86_fill_fpxregset, x86_store_fpxregset }, | |
318 | # endif | |
319 | # endif | |
1570b33e | 320 | { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (elf_fpregset_t), |
d0722149 DE |
321 | FP_REGS, |
322 | x86_fill_fpregset, x86_store_fpregset }, | |
323 | #endif /* HAVE_PTRACE_GETREGS */ | |
1570b33e | 324 | { 0, 0, 0, -1, -1, NULL, NULL } |
d0722149 DE |
325 | }; |
326 | ||
327 | static CORE_ADDR | |
442ea881 | 328 | x86_get_pc (struct regcache *regcache) |
d0722149 DE |
329 | { |
330 | int use_64bit = register_size (0) == 8; | |
331 | ||
332 | if (use_64bit) | |
333 | { | |
334 | unsigned long pc; | |
442ea881 | 335 | collect_register_by_name (regcache, "rip", &pc); |
d0722149 DE |
336 | return (CORE_ADDR) pc; |
337 | } | |
338 | else | |
339 | { | |
340 | unsigned int pc; | |
442ea881 | 341 | collect_register_by_name (regcache, "eip", &pc); |
d0722149 DE |
342 | return (CORE_ADDR) pc; |
343 | } | |
344 | } | |
345 | ||
346 | static void | |
442ea881 | 347 | x86_set_pc (struct regcache *regcache, CORE_ADDR pc) |
d0722149 DE |
348 | { |
349 | int use_64bit = register_size (0) == 8; | |
350 | ||
351 | if (use_64bit) | |
352 | { | |
353 | unsigned long newpc = pc; | |
442ea881 | 354 | supply_register_by_name (regcache, "rip", &newpc); |
d0722149 DE |
355 | } |
356 | else | |
357 | { | |
358 | unsigned int newpc = pc; | |
442ea881 | 359 | supply_register_by_name (regcache, "eip", &newpc); |
d0722149 DE |
360 | } |
361 | } | |
362 | \f | |
363 | static const unsigned char x86_breakpoint[] = { 0xCC }; | |
364 | #define x86_breakpoint_len 1 | |
365 | ||
366 | static int | |
367 | x86_breakpoint_at (CORE_ADDR pc) | |
368 | { | |
369 | unsigned char c; | |
370 | ||
fc7238bb | 371 | (*the_target->read_memory) (pc, &c, 1); |
d0722149 DE |
372 | if (c == 0xCC) |
373 | return 1; | |
374 | ||
375 | return 0; | |
376 | } | |
377 | \f | |
aa5ca48f DE |
378 | /* Support for debug registers. */ |
379 | ||
380 | static unsigned long | |
381 | x86_linux_dr_get (ptid_t ptid, int regnum) | |
382 | { | |
383 | int tid; | |
384 | unsigned long value; | |
385 | ||
386 | tid = ptid_get_lwp (ptid); | |
387 | ||
388 | errno = 0; | |
389 | value = ptrace (PTRACE_PEEKUSER, tid, | |
390 | offsetof (struct user, u_debugreg[regnum]), 0); | |
391 | if (errno != 0) | |
392 | error ("Couldn't read debug register"); | |
393 | ||
394 | return value; | |
395 | } | |
396 | ||
397 | static void | |
398 | x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value) | |
399 | { | |
400 | int tid; | |
401 | ||
402 | tid = ptid_get_lwp (ptid); | |
403 | ||
404 | errno = 0; | |
405 | ptrace (PTRACE_POKEUSER, tid, | |
406 | offsetof (struct user, u_debugreg[regnum]), value); | |
407 | if (errno != 0) | |
408 | error ("Couldn't write debug register"); | |
409 | } | |
410 | ||
411 | /* Update the inferior's debug register REGNUM from STATE. */ | |
412 | ||
413 | void | |
414 | i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum) | |
415 | { | |
416 | struct inferior_list_entry *lp; | |
417 | CORE_ADDR addr; | |
418 | /* Only need to update the threads of this process. */ | |
419 | int pid = pid_of (get_thread_lwp (current_inferior)); | |
420 | ||
421 | if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR)) | |
422 | fatal ("Invalid debug register %d", regnum); | |
423 | ||
424 | addr = state->dr_mirror[regnum]; | |
425 | ||
426 | for (lp = all_lwps.head; lp; lp = lp->next) | |
427 | { | |
428 | struct lwp_info *lwp = (struct lwp_info *) lp; | |
429 | ||
430 | /* The actual update is done later, we just mark that the register | |
431 | needs updating. */ | |
432 | if (pid_of (lwp) == pid) | |
433 | lwp->arch_private->debug_registers_changed = 1; | |
434 | } | |
435 | } | |
436 | ||
437 | /* Update the inferior's DR7 debug control register from STATE. */ | |
438 | ||
439 | void | |
440 | i386_dr_low_set_control (const struct i386_debug_reg_state *state) | |
441 | { | |
442 | struct inferior_list_entry *lp; | |
443 | /* Only need to update the threads of this process. */ | |
444 | int pid = pid_of (get_thread_lwp (current_inferior)); | |
445 | ||
446 | for (lp = all_lwps.head; lp; lp = lp->next) | |
447 | { | |
448 | struct lwp_info *lwp = (struct lwp_info *) lp; | |
449 | ||
450 | /* The actual update is done later, we just mark that the register | |
451 | needs updating. */ | |
452 | if (pid_of (lwp) == pid) | |
453 | lwp->arch_private->debug_registers_changed = 1; | |
454 | } | |
455 | } | |
456 | ||
457 | /* Get the value of the DR6 debug status register from the inferior | |
458 | and record it in STATE. */ | |
459 | ||
460 | void | |
461 | i386_dr_low_get_status (struct i386_debug_reg_state *state) | |
462 | { | |
463 | struct lwp_info *lwp = get_thread_lwp (current_inferior); | |
464 | ptid_t ptid = ptid_of (lwp); | |
465 | ||
466 | state->dr_status_mirror = x86_linux_dr_get (ptid, DR_STATUS); | |
467 | } | |
468 | \f | |
469 | /* Watchpoint support. */ | |
470 | ||
471 | static int | |
472 | x86_insert_point (char type, CORE_ADDR addr, int len) | |
473 | { | |
474 | struct process_info *proc = current_process (); | |
475 | switch (type) | |
476 | { | |
8b07ae33 PA |
477 | case '0': |
478 | return set_gdb_breakpoint_at (addr); | |
aa5ca48f DE |
479 | case '2': |
480 | case '3': | |
481 | case '4': | |
482 | return i386_low_insert_watchpoint (&proc->private->arch_private->debug_reg_state, | |
483 | type, addr, len); | |
484 | default: | |
485 | /* Unsupported. */ | |
486 | return 1; | |
487 | } | |
488 | } | |
489 | ||
490 | static int | |
491 | x86_remove_point (char type, CORE_ADDR addr, int len) | |
492 | { | |
493 | struct process_info *proc = current_process (); | |
494 | switch (type) | |
495 | { | |
8b07ae33 PA |
496 | case '0': |
497 | return delete_gdb_breakpoint_at (addr); | |
aa5ca48f DE |
498 | case '2': |
499 | case '3': | |
500 | case '4': | |
501 | return i386_low_remove_watchpoint (&proc->private->arch_private->debug_reg_state, | |
502 | type, addr, len); | |
503 | default: | |
504 | /* Unsupported. */ | |
505 | return 1; | |
506 | } | |
507 | } | |
508 | ||
509 | static int | |
510 | x86_stopped_by_watchpoint (void) | |
511 | { | |
512 | struct process_info *proc = current_process (); | |
513 | return i386_low_stopped_by_watchpoint (&proc->private->arch_private->debug_reg_state); | |
514 | } | |
515 | ||
516 | static CORE_ADDR | |
517 | x86_stopped_data_address (void) | |
518 | { | |
519 | struct process_info *proc = current_process (); | |
520 | CORE_ADDR addr; | |
521 | if (i386_low_stopped_data_address (&proc->private->arch_private->debug_reg_state, | |
522 | &addr)) | |
523 | return addr; | |
524 | return 0; | |
525 | } | |
526 | \f | |
527 | /* Called when a new process is created. */ | |
528 | ||
529 | static struct arch_process_info * | |
530 | x86_linux_new_process (void) | |
531 | { | |
532 | struct arch_process_info *info = xcalloc (1, sizeof (*info)); | |
533 | ||
534 | i386_low_init_dregs (&info->debug_reg_state); | |
535 | ||
536 | return info; | |
537 | } | |
538 | ||
539 | /* Called when a new thread is detected. */ | |
540 | ||
541 | static struct arch_lwp_info * | |
542 | x86_linux_new_thread (void) | |
543 | { | |
544 | struct arch_lwp_info *info = xcalloc (1, sizeof (*info)); | |
545 | ||
546 | info->debug_registers_changed = 1; | |
547 | ||
548 | return info; | |
549 | } | |
550 | ||
551 | /* Called when resuming a thread. | |
552 | If the debug regs have changed, update the thread's copies. */ | |
553 | ||
554 | static void | |
555 | x86_linux_prepare_to_resume (struct lwp_info *lwp) | |
556 | { | |
b9a881c2 PA |
557 | ptid_t ptid = ptid_of (lwp); |
558 | ||
aa5ca48f DE |
559 | if (lwp->arch_private->debug_registers_changed) |
560 | { | |
561 | int i; | |
aa5ca48f DE |
562 | int pid = ptid_get_pid (ptid); |
563 | struct process_info *proc = find_process_pid (pid); | |
564 | struct i386_debug_reg_state *state = &proc->private->arch_private->debug_reg_state; | |
565 | ||
566 | for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++) | |
567 | x86_linux_dr_set (ptid, i, state->dr_mirror[i]); | |
568 | ||
569 | x86_linux_dr_set (ptid, DR_CONTROL, state->dr_control_mirror); | |
570 | ||
571 | lwp->arch_private->debug_registers_changed = 0; | |
572 | } | |
b9a881c2 PA |
573 | |
574 | if (lwp->stopped_by_watchpoint) | |
575 | x86_linux_dr_set (ptid, DR_STATUS, 0); | |
aa5ca48f DE |
576 | } |
577 | \f | |
d0722149 DE |
578 | /* When GDBSERVER is built as a 64-bit application on linux, the |
579 | PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since | |
580 | debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same | |
581 | as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit | |
582 | conversion in-place ourselves. */ | |
583 | ||
584 | /* These types below (compat_*) define a siginfo type that is layout | |
585 | compatible with the siginfo type exported by the 32-bit userspace | |
586 | support. */ | |
587 | ||
588 | #ifdef __x86_64__ | |
589 | ||
590 | typedef int compat_int_t; | |
591 | typedef unsigned int compat_uptr_t; | |
592 | ||
593 | typedef int compat_time_t; | |
594 | typedef int compat_timer_t; | |
595 | typedef int compat_clock_t; | |
596 | ||
597 | struct compat_timeval | |
598 | { | |
599 | compat_time_t tv_sec; | |
600 | int tv_usec; | |
601 | }; | |
602 | ||
603 | typedef union compat_sigval | |
604 | { | |
605 | compat_int_t sival_int; | |
606 | compat_uptr_t sival_ptr; | |
607 | } compat_sigval_t; | |
608 | ||
609 | typedef struct compat_siginfo | |
610 | { | |
611 | int si_signo; | |
612 | int si_errno; | |
613 | int si_code; | |
614 | ||
615 | union | |
616 | { | |
617 | int _pad[((128 / sizeof (int)) - 3)]; | |
618 | ||
619 | /* kill() */ | |
620 | struct | |
621 | { | |
622 | unsigned int _pid; | |
623 | unsigned int _uid; | |
624 | } _kill; | |
625 | ||
626 | /* POSIX.1b timers */ | |
627 | struct | |
628 | { | |
629 | compat_timer_t _tid; | |
630 | int _overrun; | |
631 | compat_sigval_t _sigval; | |
632 | } _timer; | |
633 | ||
634 | /* POSIX.1b signals */ | |
635 | struct | |
636 | { | |
637 | unsigned int _pid; | |
638 | unsigned int _uid; | |
639 | compat_sigval_t _sigval; | |
640 | } _rt; | |
641 | ||
642 | /* SIGCHLD */ | |
643 | struct | |
644 | { | |
645 | unsigned int _pid; | |
646 | unsigned int _uid; | |
647 | int _status; | |
648 | compat_clock_t _utime; | |
649 | compat_clock_t _stime; | |
650 | } _sigchld; | |
651 | ||
652 | /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ | |
653 | struct | |
654 | { | |
655 | unsigned int _addr; | |
656 | } _sigfault; | |
657 | ||
658 | /* SIGPOLL */ | |
659 | struct | |
660 | { | |
661 | int _band; | |
662 | int _fd; | |
663 | } _sigpoll; | |
664 | } _sifields; | |
665 | } compat_siginfo_t; | |
666 | ||
667 | #define cpt_si_pid _sifields._kill._pid | |
668 | #define cpt_si_uid _sifields._kill._uid | |
669 | #define cpt_si_timerid _sifields._timer._tid | |
670 | #define cpt_si_overrun _sifields._timer._overrun | |
671 | #define cpt_si_status _sifields._sigchld._status | |
672 | #define cpt_si_utime _sifields._sigchld._utime | |
673 | #define cpt_si_stime _sifields._sigchld._stime | |
674 | #define cpt_si_ptr _sifields._rt._sigval.sival_ptr | |
675 | #define cpt_si_addr _sifields._sigfault._addr | |
676 | #define cpt_si_band _sifields._sigpoll._band | |
677 | #define cpt_si_fd _sifields._sigpoll._fd | |
678 | ||
679 | /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun. | |
680 | In their place is si_timer1,si_timer2. */ | |
681 | #ifndef si_timerid | |
682 | #define si_timerid si_timer1 | |
683 | #endif | |
684 | #ifndef si_overrun | |
685 | #define si_overrun si_timer2 | |
686 | #endif | |
687 | ||
688 | static void | |
689 | compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from) | |
690 | { | |
691 | memset (to, 0, sizeof (*to)); | |
692 | ||
693 | to->si_signo = from->si_signo; | |
694 | to->si_errno = from->si_errno; | |
695 | to->si_code = from->si_code; | |
696 | ||
697 | if (to->si_code < 0) | |
698 | { | |
699 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
700 | } | |
701 | else if (to->si_code == SI_USER) | |
702 | { | |
703 | to->cpt_si_pid = from->si_pid; | |
704 | to->cpt_si_uid = from->si_uid; | |
705 | } | |
706 | else if (to->si_code == SI_TIMER) | |
707 | { | |
708 | to->cpt_si_timerid = from->si_timerid; | |
709 | to->cpt_si_overrun = from->si_overrun; | |
710 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
711 | } | |
712 | else | |
713 | { | |
714 | switch (to->si_signo) | |
715 | { | |
716 | case SIGCHLD: | |
717 | to->cpt_si_pid = from->si_pid; | |
718 | to->cpt_si_uid = from->si_uid; | |
719 | to->cpt_si_status = from->si_status; | |
720 | to->cpt_si_utime = from->si_utime; | |
721 | to->cpt_si_stime = from->si_stime; | |
722 | break; | |
723 | case SIGILL: | |
724 | case SIGFPE: | |
725 | case SIGSEGV: | |
726 | case SIGBUS: | |
727 | to->cpt_si_addr = (intptr_t) from->si_addr; | |
728 | break; | |
729 | case SIGPOLL: | |
730 | to->cpt_si_band = from->si_band; | |
731 | to->cpt_si_fd = from->si_fd; | |
732 | break; | |
733 | default: | |
734 | to->cpt_si_pid = from->si_pid; | |
735 | to->cpt_si_uid = from->si_uid; | |
736 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
737 | break; | |
738 | } | |
739 | } | |
740 | } | |
741 | ||
742 | static void | |
743 | siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from) | |
744 | { | |
745 | memset (to, 0, sizeof (*to)); | |
746 | ||
747 | to->si_signo = from->si_signo; | |
748 | to->si_errno = from->si_errno; | |
749 | to->si_code = from->si_code; | |
750 | ||
751 | if (to->si_code < 0) | |
752 | { | |
753 | to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr; | |
754 | } | |
755 | else if (to->si_code == SI_USER) | |
756 | { | |
757 | to->si_pid = from->cpt_si_pid; | |
758 | to->si_uid = from->cpt_si_uid; | |
759 | } | |
760 | else if (to->si_code == SI_TIMER) | |
761 | { | |
762 | to->si_timerid = from->cpt_si_timerid; | |
763 | to->si_overrun = from->cpt_si_overrun; | |
764 | to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr; | |
765 | } | |
766 | else | |
767 | { | |
768 | switch (to->si_signo) | |
769 | { | |
770 | case SIGCHLD: | |
771 | to->si_pid = from->cpt_si_pid; | |
772 | to->si_uid = from->cpt_si_uid; | |
773 | to->si_status = from->cpt_si_status; | |
774 | to->si_utime = from->cpt_si_utime; | |
775 | to->si_stime = from->cpt_si_stime; | |
776 | break; | |
777 | case SIGILL: | |
778 | case SIGFPE: | |
779 | case SIGSEGV: | |
780 | case SIGBUS: | |
781 | to->si_addr = (void *) (intptr_t) from->cpt_si_addr; | |
782 | break; | |
783 | case SIGPOLL: | |
784 | to->si_band = from->cpt_si_band; | |
785 | to->si_fd = from->cpt_si_fd; | |
786 | break; | |
787 | default: | |
788 | to->si_pid = from->cpt_si_pid; | |
789 | to->si_uid = from->cpt_si_uid; | |
790 | to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr; | |
791 | break; | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | #endif /* __x86_64__ */ | |
797 | ||
798 | /* Convert a native/host siginfo object, into/from the siginfo in the | |
799 | layout of the inferiors' architecture. Returns true if any | |
800 | conversion was done; false otherwise. If DIRECTION is 1, then copy | |
801 | from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to | |
802 | INF. */ | |
803 | ||
804 | static int | |
805 | x86_siginfo_fixup (struct siginfo *native, void *inf, int direction) | |
806 | { | |
807 | #ifdef __x86_64__ | |
808 | /* Is the inferior 32-bit? If so, then fixup the siginfo object. */ | |
809 | if (register_size (0) == 4) | |
810 | { | |
9f1036c1 DE |
811 | if (sizeof (struct siginfo) != sizeof (compat_siginfo_t)) |
812 | fatal ("unexpected difference in siginfo"); | |
d0722149 DE |
813 | |
814 | if (direction == 0) | |
815 | compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native); | |
816 | else | |
817 | siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf); | |
818 | ||
819 | return 1; | |
820 | } | |
821 | #endif | |
822 | ||
823 | return 0; | |
824 | } | |
825 | \f | |
1570b33e L |
826 | static int use_xml; |
827 | ||
828 | /* Update gdbserver_xmltarget. */ | |
829 | ||
830 | static void | |
831 | x86_linux_update_xmltarget (void) | |
832 | { | |
3a13a53b L |
833 | int pid; |
834 | struct regset_info *regset; | |
1570b33e L |
835 | static unsigned long long xcr0; |
836 | static int have_ptrace_getregset = -1; | |
59e04013 | 837 | #if !defined(__x86_64__) && defined(HAVE_PTRACE_GETFPXREGS) |
3a13a53b L |
838 | static int have_ptrace_getfpxregs = -1; |
839 | #endif | |
1570b33e L |
840 | |
841 | if (!current_inferior) | |
842 | return; | |
843 | ||
45ba0d02 PA |
844 | /* Before changing the register cache internal layout or the target |
845 | regsets, flush the contents of the current valid caches back to | |
846 | the threads. */ | |
847 | regcache_invalidate (); | |
848 | ||
3a13a53b | 849 | pid = pid_of (get_thread_lwp (current_inferior)); |
1570b33e L |
850 | #ifdef __x86_64__ |
851 | if (num_xmm_registers == 8) | |
852 | init_registers_i386_linux (); | |
853 | else | |
854 | init_registers_amd64_linux (); | |
855 | #else | |
3a13a53b L |
856 | { |
857 | # ifdef HAVE_PTRACE_GETFPXREGS | |
858 | if (have_ptrace_getfpxregs == -1) | |
859 | { | |
860 | elf_fpxregset_t fpxregs; | |
861 | ||
862 | if (ptrace (PTRACE_GETFPXREGS, pid, 0, (int) &fpxregs) < 0) | |
863 | { | |
864 | have_ptrace_getfpxregs = 0; | |
865 | x86_xcr0 = I386_XSTATE_X87_MASK; | |
866 | ||
867 | /* Disable PTRACE_GETFPXREGS. */ | |
868 | for (regset = target_regsets; | |
869 | regset->fill_function != NULL; regset++) | |
870 | if (regset->get_request == PTRACE_GETFPXREGS) | |
871 | { | |
872 | regset->size = 0; | |
873 | break; | |
874 | } | |
875 | } | |
876 | else | |
877 | have_ptrace_getfpxregs = 1; | |
878 | } | |
879 | ||
880 | if (!have_ptrace_getfpxregs) | |
881 | { | |
882 | init_registers_i386_mmx_linux (); | |
883 | return; | |
884 | } | |
885 | # endif | |
886 | init_registers_i386_linux (); | |
887 | } | |
1570b33e L |
888 | #endif |
889 | ||
890 | if (!use_xml) | |
891 | { | |
892 | /* Don't use XML. */ | |
893 | #ifdef __x86_64__ | |
894 | if (num_xmm_registers == 8) | |
895 | gdbserver_xmltarget = xmltarget_i386_linux_no_xml; | |
896 | else | |
897 | gdbserver_xmltarget = xmltarget_amd64_linux_no_xml; | |
898 | #else | |
899 | gdbserver_xmltarget = xmltarget_i386_linux_no_xml; | |
900 | #endif | |
901 | ||
902 | x86_xcr0 = I386_XSTATE_SSE_MASK; | |
903 | ||
904 | return; | |
905 | } | |
906 | ||
907 | /* Check if XSAVE extended state is supported. */ | |
908 | if (have_ptrace_getregset == -1) | |
909 | { | |
1570b33e L |
910 | unsigned long long xstateregs[I386_XSTATE_SSE_SIZE / sizeof (long long)]; |
911 | struct iovec iov; | |
1570b33e L |
912 | |
913 | iov.iov_base = xstateregs; | |
914 | iov.iov_len = sizeof (xstateregs); | |
915 | ||
916 | /* Check if PTRACE_GETREGSET works. */ | |
917 | if (ptrace (PTRACE_GETREGSET, pid, (unsigned int) NT_X86_XSTATE, | |
918 | &iov) < 0) | |
919 | { | |
920 | have_ptrace_getregset = 0; | |
921 | return; | |
922 | } | |
923 | else | |
924 | have_ptrace_getregset = 1; | |
925 | ||
926 | /* Get XCR0 from XSAVE extended state at byte 464. */ | |
927 | xcr0 = xstateregs[464 / sizeof (long long)]; | |
928 | ||
929 | /* Use PTRACE_GETREGSET if it is available. */ | |
930 | for (regset = target_regsets; | |
931 | regset->fill_function != NULL; regset++) | |
932 | if (regset->get_request == PTRACE_GETREGSET) | |
933 | regset->size = I386_XSTATE_SIZE (xcr0); | |
934 | else if (regset->type != GENERAL_REGS) | |
935 | regset->size = 0; | |
936 | } | |
937 | ||
938 | if (have_ptrace_getregset) | |
939 | { | |
940 | /* AVX is the highest feature we support. */ | |
941 | if ((xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK) | |
942 | { | |
943 | x86_xcr0 = xcr0; | |
944 | ||
945 | #ifdef __x86_64__ | |
946 | /* I386 has 8 xmm regs. */ | |
947 | if (num_xmm_registers == 8) | |
948 | init_registers_i386_avx_linux (); | |
949 | else | |
950 | init_registers_amd64_avx_linux (); | |
951 | #else | |
952 | init_registers_i386_avx_linux (); | |
953 | #endif | |
954 | } | |
955 | } | |
956 | } | |
957 | ||
958 | /* Process qSupported query, "xmlRegisters=". Update the buffer size for | |
959 | PTRACE_GETREGSET. */ | |
960 | ||
961 | static void | |
962 | x86_linux_process_qsupported (const char *query) | |
963 | { | |
964 | /* Return if gdb doesn't support XML. If gdb sends "xmlRegisters=" | |
965 | with "i386" in qSupported query, it supports x86 XML target | |
966 | descriptions. */ | |
967 | use_xml = 0; | |
968 | if (query != NULL && strncmp (query, "xmlRegisters=", 13) == 0) | |
969 | { | |
970 | char *copy = xstrdup (query + 13); | |
971 | char *p; | |
972 | ||
973 | for (p = strtok (copy, ","); p != NULL; p = strtok (NULL, ",")) | |
974 | { | |
975 | if (strcmp (p, "i386") == 0) | |
976 | { | |
977 | use_xml = 1; | |
978 | break; | |
979 | } | |
980 | } | |
981 | ||
982 | free (copy); | |
983 | } | |
984 | ||
985 | x86_linux_update_xmltarget (); | |
986 | } | |
987 | ||
9f1036c1 | 988 | /* Initialize gdbserver for the architecture of the inferior. */ |
d0722149 DE |
989 | |
990 | static void | |
991 | x86_arch_setup (void) | |
992 | { | |
993 | #ifdef __x86_64__ | |
994 | int pid = pid_of (get_thread_lwp (current_inferior)); | |
995 | char *file = linux_child_pid_to_exec_file (pid); | |
996 | int use_64bit = elf_64_file_p (file); | |
997 | ||
998 | free (file); | |
999 | ||
1000 | if (use_64bit < 0) | |
1001 | { | |
1002 | /* This can only happen if /proc/<pid>/exe is unreadable, | |
1003 | but "that can't happen" if we've gotten this far. | |
1004 | Fall through and assume this is a 32-bit program. */ | |
1005 | } | |
1006 | else if (use_64bit) | |
1007 | { | |
d0722149 DE |
1008 | /* Amd64 doesn't have HAVE_LINUX_USRREGS. */ |
1009 | the_low_target.num_regs = -1; | |
1010 | the_low_target.regmap = NULL; | |
1011 | the_low_target.cannot_fetch_register = NULL; | |
1012 | the_low_target.cannot_store_register = NULL; | |
1013 | ||
1014 | /* Amd64 has 16 xmm regs. */ | |
1015 | num_xmm_registers = 16; | |
1016 | ||
1570b33e | 1017 | x86_linux_update_xmltarget (); |
d0722149 DE |
1018 | return; |
1019 | } | |
1020 | #endif | |
1021 | ||
1022 | /* Ok we have a 32-bit inferior. */ | |
1023 | ||
d0722149 DE |
1024 | the_low_target.num_regs = I386_NUM_REGS; |
1025 | the_low_target.regmap = i386_regmap; | |
1026 | the_low_target.cannot_fetch_register = i386_cannot_fetch_register; | |
1027 | the_low_target.cannot_store_register = i386_cannot_store_register; | |
1028 | ||
1029 | /* I386 has 8 xmm regs. */ | |
1030 | num_xmm_registers = 8; | |
1570b33e L |
1031 | |
1032 | x86_linux_update_xmltarget (); | |
d0722149 DE |
1033 | } |
1034 | ||
219f2f23 PA |
1035 | static int |
1036 | x86_supports_tracepoints (void) | |
1037 | { | |
1038 | return 1; | |
1039 | } | |
1040 | ||
d0722149 DE |
1041 | /* This is initialized assuming an amd64 target. |
1042 | x86_arch_setup will correct it for i386 or amd64 targets. */ | |
1043 | ||
1044 | struct linux_target_ops the_low_target = | |
1045 | { | |
1046 | x86_arch_setup, | |
1047 | -1, | |
1048 | NULL, | |
1049 | NULL, | |
1050 | NULL, | |
1051 | x86_get_pc, | |
1052 | x86_set_pc, | |
1053 | x86_breakpoint, | |
1054 | x86_breakpoint_len, | |
1055 | NULL, | |
1056 | 1, | |
1057 | x86_breakpoint_at, | |
aa5ca48f DE |
1058 | x86_insert_point, |
1059 | x86_remove_point, | |
1060 | x86_stopped_by_watchpoint, | |
1061 | x86_stopped_data_address, | |
d0722149 DE |
1062 | /* collect_ptrace_register/supply_ptrace_register are not needed in the |
1063 | native i386 case (no registers smaller than an xfer unit), and are not | |
1064 | used in the biarch case (HAVE_LINUX_USRREGS is not defined). */ | |
1065 | NULL, | |
1066 | NULL, | |
1067 | /* need to fix up i386 siginfo if host is amd64 */ | |
1068 | x86_siginfo_fixup, | |
aa5ca48f DE |
1069 | x86_linux_new_process, |
1070 | x86_linux_new_thread, | |
1570b33e | 1071 | x86_linux_prepare_to_resume, |
219f2f23 PA |
1072 | x86_linux_process_qsupported, |
1073 | x86_supports_tracepoints | |
d0722149 | 1074 | }; |