[gdbserver] Split a new tracepoint.h file out of server.h.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-x86-low.c
1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
2 for GDB.
3 Copyright (C) 2002-2013 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 <stddef.h>
21 #include <signal.h>
22 #include <limits.h>
23 #include <inttypes.h>
24 #include "server.h"
25 #include "linux-low.h"
26 #include "i387-fp.h"
27 #include "i386-low.h"
28 #include "i386-xstate.h"
29 #include "elf/common.h"
30
31 #include "gdb_proc_service.h"
32 #include "agent.h"
33 #include "tdesc.h"
34 #include "tracepoint.h"
35
36 #ifdef __x86_64__
37 /* Defined in auto-generated file amd64-linux.c. */
38 void init_registers_amd64_linux (void);
39 extern const struct target_desc *tdesc_amd64_linux;
40
41 /* Defined in auto-generated file amd64-avx-linux.c. */
42 void init_registers_amd64_avx_linux (void);
43 extern const struct target_desc *tdesc_amd64_avx_linux;
44
45 /* Defined in auto-generated file x32-linux.c. */
46 void init_registers_x32_linux (void);
47 extern const struct target_desc *tdesc_x32_linux;
48
49 /* Defined in auto-generated file x32-avx-linux.c. */
50 void init_registers_x32_avx_linux (void);
51 extern const struct target_desc *tdesc_x32_avx_linux;
52 #endif
53
54 /* Defined in auto-generated file i386-linux.c. */
55 void init_registers_i386_linux (void);
56 extern const struct target_desc *tdesc_i386_linux;
57
58 /* Defined in auto-generated file i386-mmx-linux.c. */
59 void init_registers_i386_mmx_linux (void);
60 extern const struct target_desc *tdesc_i386_mmx_linux;
61
62 /* Defined in auto-generated file i386-avx-linux.c. */
63 void init_registers_i386_avx_linux (void);
64 extern const struct target_desc *tdesc_i386_avx_linux;
65
66 #ifdef __x86_64__
67 static struct target_desc *tdesc_amd64_linux_no_xml;
68 #endif
69 static struct target_desc *tdesc_i386_linux_no_xml;
70
71
72 static unsigned char jump_insn[] = { 0xe9, 0, 0, 0, 0 };
73 static unsigned char small_jump_insn[] = { 0x66, 0xe9, 0, 0 };
74
75 /* Backward compatibility for gdb without XML support. */
76
77 static const char *xmltarget_i386_linux_no_xml = "@<target>\
78 <architecture>i386</architecture>\
79 <osabi>GNU/Linux</osabi>\
80 </target>";
81
82 #ifdef __x86_64__
83 static const char *xmltarget_amd64_linux_no_xml = "@<target>\
84 <architecture>i386:x86-64</architecture>\
85 <osabi>GNU/Linux</osabi>\
86 </target>";
87 #endif
88
89 #include <sys/reg.h>
90 #include <sys/procfs.h>
91 #include <sys/ptrace.h>
92 #include <sys/uio.h>
93
94 #ifndef PTRACE_GETREGSET
95 #define PTRACE_GETREGSET 0x4204
96 #endif
97
98 #ifndef PTRACE_SETREGSET
99 #define PTRACE_SETREGSET 0x4205
100 #endif
101
102
103 #ifndef PTRACE_GET_THREAD_AREA
104 #define PTRACE_GET_THREAD_AREA 25
105 #endif
106
107 /* This definition comes from prctl.h, but some kernels may not have it. */
108 #ifndef PTRACE_ARCH_PRCTL
109 #define PTRACE_ARCH_PRCTL 30
110 #endif
111
112 /* The following definitions come from prctl.h, but may be absent
113 for certain configurations. */
114 #ifndef ARCH_GET_FS
115 #define ARCH_SET_GS 0x1001
116 #define ARCH_SET_FS 0x1002
117 #define ARCH_GET_FS 0x1003
118 #define ARCH_GET_GS 0x1004
119 #endif
120
121 /* Per-process arch-specific data we want to keep. */
122
123 struct arch_process_info
124 {
125 struct i386_debug_reg_state debug_reg_state;
126 };
127
128 /* Per-thread arch-specific data we want to keep. */
129
130 struct arch_lwp_info
131 {
132 /* Non-zero if our copy differs from what's recorded in the thread. */
133 int debug_registers_changed;
134 };
135
136 #ifdef __x86_64__
137
138 /* Mapping between the general-purpose registers in `struct user'
139 format and GDB's register array layout.
140 Note that the transfer layout uses 64-bit regs. */
141 static /*const*/ int i386_regmap[] =
142 {
143 RAX * 8, RCX * 8, RDX * 8, RBX * 8,
144 RSP * 8, RBP * 8, RSI * 8, RDI * 8,
145 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
146 DS * 8, ES * 8, FS * 8, GS * 8
147 };
148
149 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
150
151 /* So code below doesn't have to care, i386 or amd64. */
152 #define ORIG_EAX ORIG_RAX
153
154 static const int x86_64_regmap[] =
155 {
156 RAX * 8, RBX * 8, RCX * 8, RDX * 8,
157 RSI * 8, RDI * 8, RBP * 8, RSP * 8,
158 R8 * 8, R9 * 8, R10 * 8, R11 * 8,
159 R12 * 8, R13 * 8, R14 * 8, R15 * 8,
160 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
161 DS * 8, ES * 8, FS * 8, GS * 8,
162 -1, -1, -1, -1, -1, -1, -1, -1,
163 -1, -1, -1, -1, -1, -1, -1, -1,
164 -1, -1, -1, -1, -1, -1, -1, -1,
165 -1, -1, -1, -1, -1, -1, -1, -1, -1,
166 ORIG_RAX * 8
167 };
168
169 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
170
171 #else /* ! __x86_64__ */
172
173 /* Mapping between the general-purpose registers in `struct user'
174 format and GDB's register array layout. */
175 static /*const*/ int i386_regmap[] =
176 {
177 EAX * 4, ECX * 4, EDX * 4, EBX * 4,
178 UESP * 4, EBP * 4, ESI * 4, EDI * 4,
179 EIP * 4, EFL * 4, CS * 4, SS * 4,
180 DS * 4, ES * 4, FS * 4, GS * 4
181 };
182
183 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
184
185 #endif
186
187 #ifdef __x86_64__
188
189 /* Returns true if the current inferior belongs to a x86-64 process,
190 per the tdesc. */
191
192 static int
193 is_64bit_tdesc (void)
194 {
195 struct regcache *regcache = get_thread_regcache (current_inferior, 0);
196
197 return register_size (regcache->tdesc, 0) == 8;
198 }
199
200 #endif
201
202 \f
203 /* Called by libthread_db. */
204
205 ps_err_e
206 ps_get_thread_area (const struct ps_prochandle *ph,
207 lwpid_t lwpid, int idx, void **base)
208 {
209 #ifdef __x86_64__
210 int use_64bit = is_64bit_tdesc ();
211
212 if (use_64bit)
213 {
214 switch (idx)
215 {
216 case FS:
217 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
218 return PS_OK;
219 break;
220 case GS:
221 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
222 return PS_OK;
223 break;
224 default:
225 return PS_BADADDR;
226 }
227 return PS_ERR;
228 }
229 #endif
230
231 {
232 unsigned int desc[4];
233
234 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
235 (void *) (intptr_t) idx, (unsigned long) &desc) < 0)
236 return PS_ERR;
237
238 /* Ensure we properly extend the value to 64-bits for x86_64. */
239 *base = (void *) (uintptr_t) desc[1];
240 return PS_OK;
241 }
242 }
243
244 /* Get the thread area address. This is used to recognize which
245 thread is which when tracing with the in-process agent library. We
246 don't read anything from the address, and treat it as opaque; it's
247 the address itself that we assume is unique per-thread. */
248
249 static int
250 x86_get_thread_area (int lwpid, CORE_ADDR *addr)
251 {
252 #ifdef __x86_64__
253 int use_64bit = is_64bit_tdesc ();
254
255 if (use_64bit)
256 {
257 void *base;
258 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_FS) == 0)
259 {
260 *addr = (CORE_ADDR) (uintptr_t) base;
261 return 0;
262 }
263
264 return -1;
265 }
266 #endif
267
268 {
269 struct lwp_info *lwp = find_lwp_pid (pid_to_ptid (lwpid));
270 struct regcache *regcache = get_thread_regcache (get_lwp_thread (lwp), 1);
271 unsigned int desc[4];
272 ULONGEST gs = 0;
273 const int reg_thread_area = 3; /* bits to scale down register value. */
274 int idx;
275
276 collect_register_by_name (regcache, "gs", &gs);
277
278 idx = gs >> reg_thread_area;
279
280 if (ptrace (PTRACE_GET_THREAD_AREA,
281 lwpid_of (lwp),
282 (void *) (long) idx, (unsigned long) &desc) < 0)
283 return -1;
284
285 *addr = desc[1];
286 return 0;
287 }
288 }
289
290
291 \f
292 static int
293 x86_cannot_store_register (int regno)
294 {
295 #ifdef __x86_64__
296 if (is_64bit_tdesc ())
297 return 0;
298 #endif
299
300 return regno >= I386_NUM_REGS;
301 }
302
303 static int
304 x86_cannot_fetch_register (int regno)
305 {
306 #ifdef __x86_64__
307 if (is_64bit_tdesc ())
308 return 0;
309 #endif
310
311 return regno >= I386_NUM_REGS;
312 }
313
314 static void
315 x86_fill_gregset (struct regcache *regcache, void *buf)
316 {
317 int i;
318
319 #ifdef __x86_64__
320 if (register_size (regcache->tdesc, 0) == 8)
321 {
322 for (i = 0; i < X86_64_NUM_REGS; i++)
323 if (x86_64_regmap[i] != -1)
324 collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
325 return;
326 }
327 #endif
328
329 for (i = 0; i < I386_NUM_REGS; i++)
330 collect_register (regcache, i, ((char *) buf) + i386_regmap[i]);
331
332 collect_register_by_name (regcache, "orig_eax",
333 ((char *) buf) + ORIG_EAX * 4);
334 }
335
336 static void
337 x86_store_gregset (struct regcache *regcache, const void *buf)
338 {
339 int i;
340
341 #ifdef __x86_64__
342 if (register_size (regcache->tdesc, 0) == 8)
343 {
344 for (i = 0; i < X86_64_NUM_REGS; i++)
345 if (x86_64_regmap[i] != -1)
346 supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
347 return;
348 }
349 #endif
350
351 for (i = 0; i < I386_NUM_REGS; i++)
352 supply_register (regcache, i, ((char *) buf) + i386_regmap[i]);
353
354 supply_register_by_name (regcache, "orig_eax",
355 ((char *) buf) + ORIG_EAX * 4);
356 }
357
358 static void
359 x86_fill_fpregset (struct regcache *regcache, void *buf)
360 {
361 #ifdef __x86_64__
362 i387_cache_to_fxsave (regcache, buf);
363 #else
364 i387_cache_to_fsave (regcache, buf);
365 #endif
366 }
367
368 static void
369 x86_store_fpregset (struct regcache *regcache, const void *buf)
370 {
371 #ifdef __x86_64__
372 i387_fxsave_to_cache (regcache, buf);
373 #else
374 i387_fsave_to_cache (regcache, buf);
375 #endif
376 }
377
378 #ifndef __x86_64__
379
380 static void
381 x86_fill_fpxregset (struct regcache *regcache, void *buf)
382 {
383 i387_cache_to_fxsave (regcache, buf);
384 }
385
386 static void
387 x86_store_fpxregset (struct regcache *regcache, const void *buf)
388 {
389 i387_fxsave_to_cache (regcache, buf);
390 }
391
392 #endif
393
394 static void
395 x86_fill_xstateregset (struct regcache *regcache, void *buf)
396 {
397 i387_cache_to_xsave (regcache, buf);
398 }
399
400 static void
401 x86_store_xstateregset (struct regcache *regcache, const void *buf)
402 {
403 i387_xsave_to_cache (regcache, buf);
404 }
405
406 /* ??? The non-biarch i386 case stores all the i387 regs twice.
407 Once in i387_.*fsave.* and once in i387_.*fxsave.*.
408 This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
409 doesn't work. IWBN to avoid the duplication in the case where it
410 does work. Maybe the arch_setup routine could check whether it works
411 and update the supported regsets accordingly. */
412
413 static struct regset_info x86_regsets[] =
414 {
415 #ifdef HAVE_PTRACE_GETREGS
416 { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
417 GENERAL_REGS,
418 x86_fill_gregset, x86_store_gregset },
419 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_X86_XSTATE, 0,
420 EXTENDED_REGS, x86_fill_xstateregset, x86_store_xstateregset },
421 # ifndef __x86_64__
422 # ifdef HAVE_PTRACE_GETFPXREGS
423 { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, 0, sizeof (elf_fpxregset_t),
424 EXTENDED_REGS,
425 x86_fill_fpxregset, x86_store_fpxregset },
426 # endif
427 # endif
428 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (elf_fpregset_t),
429 FP_REGS,
430 x86_fill_fpregset, x86_store_fpregset },
431 #endif /* HAVE_PTRACE_GETREGS */
432 { 0, 0, 0, -1, -1, NULL, NULL }
433 };
434
435 static CORE_ADDR
436 x86_get_pc (struct regcache *regcache)
437 {
438 int use_64bit = register_size (regcache->tdesc, 0) == 8;
439
440 if (use_64bit)
441 {
442 unsigned long pc;
443 collect_register_by_name (regcache, "rip", &pc);
444 return (CORE_ADDR) pc;
445 }
446 else
447 {
448 unsigned int pc;
449 collect_register_by_name (regcache, "eip", &pc);
450 return (CORE_ADDR) pc;
451 }
452 }
453
454 static void
455 x86_set_pc (struct regcache *regcache, CORE_ADDR pc)
456 {
457 int use_64bit = register_size (regcache->tdesc, 0) == 8;
458
459 if (use_64bit)
460 {
461 unsigned long newpc = pc;
462 supply_register_by_name (regcache, "rip", &newpc);
463 }
464 else
465 {
466 unsigned int newpc = pc;
467 supply_register_by_name (regcache, "eip", &newpc);
468 }
469 }
470 \f
471 static const unsigned char x86_breakpoint[] = { 0xCC };
472 #define x86_breakpoint_len 1
473
474 static int
475 x86_breakpoint_at (CORE_ADDR pc)
476 {
477 unsigned char c;
478
479 (*the_target->read_memory) (pc, &c, 1);
480 if (c == 0xCC)
481 return 1;
482
483 return 0;
484 }
485 \f
486 /* Support for debug registers. */
487
488 static unsigned long
489 x86_linux_dr_get (ptid_t ptid, int regnum)
490 {
491 int tid;
492 unsigned long value;
493
494 tid = ptid_get_lwp (ptid);
495
496 errno = 0;
497 value = ptrace (PTRACE_PEEKUSER, tid,
498 offsetof (struct user, u_debugreg[regnum]), 0);
499 if (errno != 0)
500 error ("Couldn't read debug register");
501
502 return value;
503 }
504
505 static void
506 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
507 {
508 int tid;
509
510 tid = ptid_get_lwp (ptid);
511
512 errno = 0;
513 ptrace (PTRACE_POKEUSER, tid,
514 offsetof (struct user, u_debugreg[regnum]), value);
515 if (errno != 0)
516 error ("Couldn't write debug register");
517 }
518
519 static int
520 update_debug_registers_callback (struct inferior_list_entry *entry,
521 void *pid_p)
522 {
523 struct lwp_info *lwp = (struct lwp_info *) entry;
524 int pid = *(int *) pid_p;
525
526 /* Only update the threads of this process. */
527 if (pid_of (lwp) == pid)
528 {
529 /* The actual update is done later just before resuming the lwp,
530 we just mark that the registers need updating. */
531 lwp->arch_private->debug_registers_changed = 1;
532
533 /* If the lwp isn't stopped, force it to momentarily pause, so
534 we can update its debug registers. */
535 if (!lwp->stopped)
536 linux_stop_lwp (lwp);
537 }
538
539 return 0;
540 }
541
542 /* Update the inferior's debug register REGNUM from STATE. */
543
544 void
545 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
546 {
547 /* Only update the threads of this process. */
548 int pid = pid_of (get_thread_lwp (current_inferior));
549
550 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
551 fatal ("Invalid debug register %d", regnum);
552
553 find_inferior (&all_lwps, update_debug_registers_callback, &pid);
554 }
555
556 /* Return the inferior's debug register REGNUM. */
557
558 CORE_ADDR
559 i386_dr_low_get_addr (int regnum)
560 {
561 struct lwp_info *lwp = get_thread_lwp (current_inferior);
562 ptid_t ptid = ptid_of (lwp);
563
564 /* DR6 and DR7 are retrieved with some other way. */
565 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
566
567 return x86_linux_dr_get (ptid, regnum);
568 }
569
570 /* Update the inferior's DR7 debug control register from STATE. */
571
572 void
573 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
574 {
575 /* Only update the threads of this process. */
576 int pid = pid_of (get_thread_lwp (current_inferior));
577
578 find_inferior (&all_lwps, update_debug_registers_callback, &pid);
579 }
580
581 /* Return the inferior's DR7 debug control register. */
582
583 unsigned
584 i386_dr_low_get_control (void)
585 {
586 struct lwp_info *lwp = get_thread_lwp (current_inferior);
587 ptid_t ptid = ptid_of (lwp);
588
589 return x86_linux_dr_get (ptid, DR_CONTROL);
590 }
591
592 /* Get the value of the DR6 debug status register from the inferior
593 and record it in STATE. */
594
595 unsigned
596 i386_dr_low_get_status (void)
597 {
598 struct lwp_info *lwp = get_thread_lwp (current_inferior);
599 ptid_t ptid = ptid_of (lwp);
600
601 return x86_linux_dr_get (ptid, DR_STATUS);
602 }
603 \f
604 /* Breakpoint/Watchpoint support. */
605
606 static int
607 x86_insert_point (char type, CORE_ADDR addr, int len)
608 {
609 struct process_info *proc = current_process ();
610 switch (type)
611 {
612 case '0': /* software-breakpoint */
613 {
614 int ret;
615
616 ret = prepare_to_access_memory ();
617 if (ret)
618 return -1;
619 ret = set_gdb_breakpoint_at (addr);
620 done_accessing_memory ();
621 return ret;
622 }
623 case '1': /* hardware-breakpoint */
624 case '2': /* write watchpoint */
625 case '3': /* read watchpoint */
626 case '4': /* access watchpoint */
627 return i386_low_insert_watchpoint (&proc->private->arch_private->debug_reg_state,
628 type, addr, len);
629
630 default:
631 /* Unsupported. */
632 return 1;
633 }
634 }
635
636 static int
637 x86_remove_point (char type, CORE_ADDR addr, int len)
638 {
639 struct process_info *proc = current_process ();
640 switch (type)
641 {
642 case '0': /* software-breakpoint */
643 {
644 int ret;
645
646 ret = prepare_to_access_memory ();
647 if (ret)
648 return -1;
649 ret = delete_gdb_breakpoint_at (addr);
650 done_accessing_memory ();
651 return ret;
652 }
653 case '1': /* hardware-breakpoint */
654 case '2': /* write watchpoint */
655 case '3': /* read watchpoint */
656 case '4': /* access watchpoint */
657 return i386_low_remove_watchpoint (&proc->private->arch_private->debug_reg_state,
658 type, addr, len);
659 default:
660 /* Unsupported. */
661 return 1;
662 }
663 }
664
665 static int
666 x86_stopped_by_watchpoint (void)
667 {
668 struct process_info *proc = current_process ();
669 return i386_low_stopped_by_watchpoint (&proc->private->arch_private->debug_reg_state);
670 }
671
672 static CORE_ADDR
673 x86_stopped_data_address (void)
674 {
675 struct process_info *proc = current_process ();
676 CORE_ADDR addr;
677 if (i386_low_stopped_data_address (&proc->private->arch_private->debug_reg_state,
678 &addr))
679 return addr;
680 return 0;
681 }
682 \f
683 /* Called when a new process is created. */
684
685 static struct arch_process_info *
686 x86_linux_new_process (void)
687 {
688 struct arch_process_info *info = xcalloc (1, sizeof (*info));
689
690 i386_low_init_dregs (&info->debug_reg_state);
691
692 return info;
693 }
694
695 /* Called when a new thread is detected. */
696
697 static struct arch_lwp_info *
698 x86_linux_new_thread (void)
699 {
700 struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
701
702 info->debug_registers_changed = 1;
703
704 return info;
705 }
706
707 /* Called when resuming a thread.
708 If the debug regs have changed, update the thread's copies. */
709
710 static void
711 x86_linux_prepare_to_resume (struct lwp_info *lwp)
712 {
713 ptid_t ptid = ptid_of (lwp);
714 int clear_status = 0;
715
716 if (lwp->arch_private->debug_registers_changed)
717 {
718 int i;
719 int pid = ptid_get_pid (ptid);
720 struct process_info *proc = find_process_pid (pid);
721 struct i386_debug_reg_state *state
722 = &proc->private->arch_private->debug_reg_state;
723
724 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
725 if (state->dr_ref_count[i] > 0)
726 {
727 x86_linux_dr_set (ptid, i, state->dr_mirror[i]);
728
729 /* If we're setting a watchpoint, any change the inferior
730 had done itself to the debug registers needs to be
731 discarded, otherwise, i386_low_stopped_data_address can
732 get confused. */
733 clear_status = 1;
734 }
735
736 x86_linux_dr_set (ptid, DR_CONTROL, state->dr_control_mirror);
737
738 lwp->arch_private->debug_registers_changed = 0;
739 }
740
741 if (clear_status || lwp->stopped_by_watchpoint)
742 x86_linux_dr_set (ptid, DR_STATUS, 0);
743 }
744 \f
745 /* When GDBSERVER is built as a 64-bit application on linux, the
746 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
747 debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
748 as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
749 conversion in-place ourselves. */
750
751 /* These types below (compat_*) define a siginfo type that is layout
752 compatible with the siginfo type exported by the 32-bit userspace
753 support. */
754
755 #ifdef __x86_64__
756
757 typedef int compat_int_t;
758 typedef unsigned int compat_uptr_t;
759
760 typedef int compat_time_t;
761 typedef int compat_timer_t;
762 typedef int compat_clock_t;
763
764 struct compat_timeval
765 {
766 compat_time_t tv_sec;
767 int tv_usec;
768 };
769
770 typedef union compat_sigval
771 {
772 compat_int_t sival_int;
773 compat_uptr_t sival_ptr;
774 } compat_sigval_t;
775
776 typedef struct compat_siginfo
777 {
778 int si_signo;
779 int si_errno;
780 int si_code;
781
782 union
783 {
784 int _pad[((128 / sizeof (int)) - 3)];
785
786 /* kill() */
787 struct
788 {
789 unsigned int _pid;
790 unsigned int _uid;
791 } _kill;
792
793 /* POSIX.1b timers */
794 struct
795 {
796 compat_timer_t _tid;
797 int _overrun;
798 compat_sigval_t _sigval;
799 } _timer;
800
801 /* POSIX.1b signals */
802 struct
803 {
804 unsigned int _pid;
805 unsigned int _uid;
806 compat_sigval_t _sigval;
807 } _rt;
808
809 /* SIGCHLD */
810 struct
811 {
812 unsigned int _pid;
813 unsigned int _uid;
814 int _status;
815 compat_clock_t _utime;
816 compat_clock_t _stime;
817 } _sigchld;
818
819 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
820 struct
821 {
822 unsigned int _addr;
823 } _sigfault;
824
825 /* SIGPOLL */
826 struct
827 {
828 int _band;
829 int _fd;
830 } _sigpoll;
831 } _sifields;
832 } compat_siginfo_t;
833
834 /* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes. */
835 typedef long __attribute__ ((__aligned__ (4))) compat_x32_clock_t;
836
837 typedef struct compat_x32_siginfo
838 {
839 int si_signo;
840 int si_errno;
841 int si_code;
842
843 union
844 {
845 int _pad[((128 / sizeof (int)) - 3)];
846
847 /* kill() */
848 struct
849 {
850 unsigned int _pid;
851 unsigned int _uid;
852 } _kill;
853
854 /* POSIX.1b timers */
855 struct
856 {
857 compat_timer_t _tid;
858 int _overrun;
859 compat_sigval_t _sigval;
860 } _timer;
861
862 /* POSIX.1b signals */
863 struct
864 {
865 unsigned int _pid;
866 unsigned int _uid;
867 compat_sigval_t _sigval;
868 } _rt;
869
870 /* SIGCHLD */
871 struct
872 {
873 unsigned int _pid;
874 unsigned int _uid;
875 int _status;
876 compat_x32_clock_t _utime;
877 compat_x32_clock_t _stime;
878 } _sigchld;
879
880 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
881 struct
882 {
883 unsigned int _addr;
884 } _sigfault;
885
886 /* SIGPOLL */
887 struct
888 {
889 int _band;
890 int _fd;
891 } _sigpoll;
892 } _sifields;
893 } compat_x32_siginfo_t __attribute__ ((__aligned__ (8)));
894
895 #define cpt_si_pid _sifields._kill._pid
896 #define cpt_si_uid _sifields._kill._uid
897 #define cpt_si_timerid _sifields._timer._tid
898 #define cpt_si_overrun _sifields._timer._overrun
899 #define cpt_si_status _sifields._sigchld._status
900 #define cpt_si_utime _sifields._sigchld._utime
901 #define cpt_si_stime _sifields._sigchld._stime
902 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
903 #define cpt_si_addr _sifields._sigfault._addr
904 #define cpt_si_band _sifields._sigpoll._band
905 #define cpt_si_fd _sifields._sigpoll._fd
906
907 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
908 In their place is si_timer1,si_timer2. */
909 #ifndef si_timerid
910 #define si_timerid si_timer1
911 #endif
912 #ifndef si_overrun
913 #define si_overrun si_timer2
914 #endif
915
916 static void
917 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
918 {
919 memset (to, 0, sizeof (*to));
920
921 to->si_signo = from->si_signo;
922 to->si_errno = from->si_errno;
923 to->si_code = from->si_code;
924
925 if (to->si_code == SI_TIMER)
926 {
927 to->cpt_si_timerid = from->si_timerid;
928 to->cpt_si_overrun = from->si_overrun;
929 to->cpt_si_ptr = (intptr_t) from->si_ptr;
930 }
931 else if (to->si_code == SI_USER)
932 {
933 to->cpt_si_pid = from->si_pid;
934 to->cpt_si_uid = from->si_uid;
935 }
936 else if (to->si_code < 0)
937 {
938 to->cpt_si_pid = from->si_pid;
939 to->cpt_si_uid = from->si_uid;
940 to->cpt_si_ptr = (intptr_t) from->si_ptr;
941 }
942 else
943 {
944 switch (to->si_signo)
945 {
946 case SIGCHLD:
947 to->cpt_si_pid = from->si_pid;
948 to->cpt_si_uid = from->si_uid;
949 to->cpt_si_status = from->si_status;
950 to->cpt_si_utime = from->si_utime;
951 to->cpt_si_stime = from->si_stime;
952 break;
953 case SIGILL:
954 case SIGFPE:
955 case SIGSEGV:
956 case SIGBUS:
957 to->cpt_si_addr = (intptr_t) from->si_addr;
958 break;
959 case SIGPOLL:
960 to->cpt_si_band = from->si_band;
961 to->cpt_si_fd = from->si_fd;
962 break;
963 default:
964 to->cpt_si_pid = from->si_pid;
965 to->cpt_si_uid = from->si_uid;
966 to->cpt_si_ptr = (intptr_t) from->si_ptr;
967 break;
968 }
969 }
970 }
971
972 static void
973 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
974 {
975 memset (to, 0, sizeof (*to));
976
977 to->si_signo = from->si_signo;
978 to->si_errno = from->si_errno;
979 to->si_code = from->si_code;
980
981 if (to->si_code == SI_TIMER)
982 {
983 to->si_timerid = from->cpt_si_timerid;
984 to->si_overrun = from->cpt_si_overrun;
985 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
986 }
987 else if (to->si_code == SI_USER)
988 {
989 to->si_pid = from->cpt_si_pid;
990 to->si_uid = from->cpt_si_uid;
991 }
992 else if (to->si_code < 0)
993 {
994 to->si_pid = from->cpt_si_pid;
995 to->si_uid = from->cpt_si_uid;
996 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
997 }
998 else
999 {
1000 switch (to->si_signo)
1001 {
1002 case SIGCHLD:
1003 to->si_pid = from->cpt_si_pid;
1004 to->si_uid = from->cpt_si_uid;
1005 to->si_status = from->cpt_si_status;
1006 to->si_utime = from->cpt_si_utime;
1007 to->si_stime = from->cpt_si_stime;
1008 break;
1009 case SIGILL:
1010 case SIGFPE:
1011 case SIGSEGV:
1012 case SIGBUS:
1013 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
1014 break;
1015 case SIGPOLL:
1016 to->si_band = from->cpt_si_band;
1017 to->si_fd = from->cpt_si_fd;
1018 break;
1019 default:
1020 to->si_pid = from->cpt_si_pid;
1021 to->si_uid = from->cpt_si_uid;
1022 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
1023 break;
1024 }
1025 }
1026 }
1027
1028 static void
1029 compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to,
1030 siginfo_t *from)
1031 {
1032 memset (to, 0, sizeof (*to));
1033
1034 to->si_signo = from->si_signo;
1035 to->si_errno = from->si_errno;
1036 to->si_code = from->si_code;
1037
1038 if (to->si_code == SI_TIMER)
1039 {
1040 to->cpt_si_timerid = from->si_timerid;
1041 to->cpt_si_overrun = from->si_overrun;
1042 to->cpt_si_ptr = (intptr_t) from->si_ptr;
1043 }
1044 else if (to->si_code == SI_USER)
1045 {
1046 to->cpt_si_pid = from->si_pid;
1047 to->cpt_si_uid = from->si_uid;
1048 }
1049 else if (to->si_code < 0)
1050 {
1051 to->cpt_si_pid = from->si_pid;
1052 to->cpt_si_uid = from->si_uid;
1053 to->cpt_si_ptr = (intptr_t) from->si_ptr;
1054 }
1055 else
1056 {
1057 switch (to->si_signo)
1058 {
1059 case SIGCHLD:
1060 to->cpt_si_pid = from->si_pid;
1061 to->cpt_si_uid = from->si_uid;
1062 to->cpt_si_status = from->si_status;
1063 to->cpt_si_utime = from->si_utime;
1064 to->cpt_si_stime = from->si_stime;
1065 break;
1066 case SIGILL:
1067 case SIGFPE:
1068 case SIGSEGV:
1069 case SIGBUS:
1070 to->cpt_si_addr = (intptr_t) from->si_addr;
1071 break;
1072 case SIGPOLL:
1073 to->cpt_si_band = from->si_band;
1074 to->cpt_si_fd = from->si_fd;
1075 break;
1076 default:
1077 to->cpt_si_pid = from->si_pid;
1078 to->cpt_si_uid = from->si_uid;
1079 to->cpt_si_ptr = (intptr_t) from->si_ptr;
1080 break;
1081 }
1082 }
1083 }
1084
1085 static void
1086 siginfo_from_compat_x32_siginfo (siginfo_t *to,
1087 compat_x32_siginfo_t *from)
1088 {
1089 memset (to, 0, sizeof (*to));
1090
1091 to->si_signo = from->si_signo;
1092 to->si_errno = from->si_errno;
1093 to->si_code = from->si_code;
1094
1095 if (to->si_code == SI_TIMER)
1096 {
1097 to->si_timerid = from->cpt_si_timerid;
1098 to->si_overrun = from->cpt_si_overrun;
1099 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
1100 }
1101 else if (to->si_code == SI_USER)
1102 {
1103 to->si_pid = from->cpt_si_pid;
1104 to->si_uid = from->cpt_si_uid;
1105 }
1106 else if (to->si_code < 0)
1107 {
1108 to->si_pid = from->cpt_si_pid;
1109 to->si_uid = from->cpt_si_uid;
1110 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
1111 }
1112 else
1113 {
1114 switch (to->si_signo)
1115 {
1116 case SIGCHLD:
1117 to->si_pid = from->cpt_si_pid;
1118 to->si_uid = from->cpt_si_uid;
1119 to->si_status = from->cpt_si_status;
1120 to->si_utime = from->cpt_si_utime;
1121 to->si_stime = from->cpt_si_stime;
1122 break;
1123 case SIGILL:
1124 case SIGFPE:
1125 case SIGSEGV:
1126 case SIGBUS:
1127 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
1128 break;
1129 case SIGPOLL:
1130 to->si_band = from->cpt_si_band;
1131 to->si_fd = from->cpt_si_fd;
1132 break;
1133 default:
1134 to->si_pid = from->cpt_si_pid;
1135 to->si_uid = from->cpt_si_uid;
1136 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
1137 break;
1138 }
1139 }
1140 }
1141
1142 #endif /* __x86_64__ */
1143
1144 /* Convert a native/host siginfo object, into/from the siginfo in the
1145 layout of the inferiors' architecture. Returns true if any
1146 conversion was done; false otherwise. If DIRECTION is 1, then copy
1147 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
1148 INF. */
1149
1150 static int
1151 x86_siginfo_fixup (siginfo_t *native, void *inf, int direction)
1152 {
1153 #ifdef __x86_64__
1154 unsigned int machine;
1155 int tid = lwpid_of (get_thread_lwp (current_inferior));
1156 int is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine);
1157
1158 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
1159 if (!is_64bit_tdesc ())
1160 {
1161 if (sizeof (siginfo_t) != sizeof (compat_siginfo_t))
1162 fatal ("unexpected difference in siginfo");
1163
1164 if (direction == 0)
1165 compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
1166 else
1167 siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
1168
1169 return 1;
1170 }
1171 /* No fixup for native x32 GDB. */
1172 else if (!is_elf64 && sizeof (void *) == 8)
1173 {
1174 if (sizeof (siginfo_t) != sizeof (compat_x32_siginfo_t))
1175 fatal ("unexpected difference in siginfo");
1176
1177 if (direction == 0)
1178 compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf,
1179 native);
1180 else
1181 siginfo_from_compat_x32_siginfo (native,
1182 (struct compat_x32_siginfo *) inf);
1183
1184 return 1;
1185 }
1186 #endif
1187
1188 return 0;
1189 }
1190 \f
1191 static int use_xml;
1192
1193 /* Format of XSAVE extended state is:
1194 struct
1195 {
1196 fxsave_bytes[0..463]
1197 sw_usable_bytes[464..511]
1198 xstate_hdr_bytes[512..575]
1199 avx_bytes[576..831]
1200 future_state etc
1201 };
1202
1203 Same memory layout will be used for the coredump NT_X86_XSTATE
1204 representing the XSAVE extended state registers.
1205
1206 The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled
1207 extended state mask, which is the same as the extended control register
1208 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask
1209 together with the mask saved in the xstate_hdr_bytes to determine what
1210 states the processor/OS supports and what state, used or initialized,
1211 the process/thread is in. */
1212 #define I386_LINUX_XSAVE_XCR0_OFFSET 464
1213
1214 /* Does the current host support the GETFPXREGS request? The header
1215 file may or may not define it, and even if it is defined, the
1216 kernel will return EIO if it's running on a pre-SSE processor. */
1217 int have_ptrace_getfpxregs =
1218 #ifdef HAVE_PTRACE_GETFPXREGS
1219 -1
1220 #else
1221 0
1222 #endif
1223 ;
1224
1225 /* Does the current host support PTRACE_GETREGSET? */
1226 static int have_ptrace_getregset = -1;
1227
1228 /* Get Linux/x86 target description from running target. */
1229
1230 static const struct target_desc *
1231 x86_linux_read_description (void)
1232 {
1233 unsigned int machine;
1234 int is_elf64;
1235 int avx;
1236 int tid;
1237 static uint64_t xcr0;
1238 struct regset_info *regset;
1239
1240 tid = lwpid_of (get_thread_lwp (current_inferior));
1241
1242 is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine);
1243
1244 if (sizeof (void *) == 4)
1245 {
1246 if (is_elf64 > 0)
1247 error (_("Can't debug 64-bit process with 32-bit GDBserver"));
1248 #ifndef __x86_64__
1249 else if (machine == EM_X86_64)
1250 error (_("Can't debug x86-64 process with 32-bit GDBserver"));
1251 #endif
1252 }
1253
1254 #if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS
1255 if (machine == EM_386 && have_ptrace_getfpxregs == -1)
1256 {
1257 elf_fpxregset_t fpxregs;
1258
1259 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0)
1260 {
1261 have_ptrace_getfpxregs = 0;
1262 have_ptrace_getregset = 0;
1263 return tdesc_i386_mmx_linux;
1264 }
1265 else
1266 have_ptrace_getfpxregs = 1;
1267 }
1268 #endif
1269
1270 if (!use_xml)
1271 {
1272 x86_xcr0 = I386_XSTATE_SSE_MASK;
1273
1274 /* Don't use XML. */
1275 #ifdef __x86_64__
1276 if (machine == EM_X86_64)
1277 return tdesc_amd64_linux_no_xml;
1278 else
1279 #endif
1280 return tdesc_i386_linux_no_xml;
1281 }
1282
1283 if (have_ptrace_getregset == -1)
1284 {
1285 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
1286 struct iovec iov;
1287
1288 iov.iov_base = xstateregs;
1289 iov.iov_len = sizeof (xstateregs);
1290
1291 /* Check if PTRACE_GETREGSET works. */
1292 if (ptrace (PTRACE_GETREGSET, tid,
1293 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
1294 have_ptrace_getregset = 0;
1295 else
1296 {
1297 have_ptrace_getregset = 1;
1298
1299 /* Get XCR0 from XSAVE extended state. */
1300 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
1301 / sizeof (uint64_t))];
1302
1303 /* Use PTRACE_GETREGSET if it is available. */
1304 for (regset = x86_regsets;
1305 regset->fill_function != NULL; regset++)
1306 if (regset->get_request == PTRACE_GETREGSET)
1307 regset->size = I386_XSTATE_SIZE (xcr0);
1308 else if (regset->type != GENERAL_REGS)
1309 regset->size = 0;
1310 }
1311 }
1312
1313 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
1314 avx = (have_ptrace_getregset
1315 && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK);
1316
1317 /* AVX is the highest feature we support. */
1318 if (avx)
1319 x86_xcr0 = xcr0;
1320
1321 if (machine == EM_X86_64)
1322 {
1323 #ifdef __x86_64__
1324 if (avx)
1325 {
1326 if (!is_elf64)
1327 return tdesc_x32_avx_linux;
1328 else
1329 return tdesc_amd64_avx_linux;
1330 }
1331 else
1332 {
1333 if (!is_elf64)
1334 return tdesc_x32_linux;
1335 else
1336 return tdesc_amd64_linux;
1337 }
1338 #endif
1339 }
1340 else
1341 {
1342 if (avx)
1343 return tdesc_i386_avx_linux;
1344 else
1345 return tdesc_i386_linux;
1346 }
1347
1348 gdb_assert_not_reached ("failed to return tdesc");
1349 }
1350
1351 /* Callback for find_inferior. Stops iteration when a thread with a
1352 given PID is found. */
1353
1354 static int
1355 same_process_callback (struct inferior_list_entry *entry, void *data)
1356 {
1357 int pid = *(int *) data;
1358
1359 return (ptid_get_pid (entry->id) == pid);
1360 }
1361
1362 /* Callback for for_each_inferior. Calls the arch_setup routine for
1363 each process. */
1364
1365 static void
1366 x86_arch_setup_process_callback (struct inferior_list_entry *entry)
1367 {
1368 int pid = ptid_get_pid (entry->id);
1369
1370 /* Look up any thread of this processes. */
1371 current_inferior
1372 = (struct thread_info *) find_inferior (&all_threads,
1373 same_process_callback, &pid);
1374
1375 the_low_target.arch_setup ();
1376 }
1377
1378 /* Update all the target description of all processes; a new GDB
1379 connected, and it may or not support xml target descriptions. */
1380
1381 static void
1382 x86_linux_update_xmltarget (void)
1383 {
1384 struct thread_info *save_inferior = current_inferior;
1385
1386 /* Before changing the register cache's internal layout, flush the
1387 contents of the current valid caches back to the threads, and
1388 release the current regcache objects. */
1389 regcache_release ();
1390
1391 for_each_inferior (&all_processes, x86_arch_setup_process_callback);
1392
1393 current_inferior = save_inferior;
1394 }
1395
1396 /* Process qSupported query, "xmlRegisters=". Update the buffer size for
1397 PTRACE_GETREGSET. */
1398
1399 static void
1400 x86_linux_process_qsupported (const char *query)
1401 {
1402 /* Return if gdb doesn't support XML. If gdb sends "xmlRegisters="
1403 with "i386" in qSupported query, it supports x86 XML target
1404 descriptions. */
1405 use_xml = 0;
1406 if (query != NULL && strncmp (query, "xmlRegisters=", 13) == 0)
1407 {
1408 char *copy = xstrdup (query + 13);
1409 char *p;
1410
1411 for (p = strtok (copy, ","); p != NULL; p = strtok (NULL, ","))
1412 {
1413 if (strcmp (p, "i386") == 0)
1414 {
1415 use_xml = 1;
1416 break;
1417 }
1418 }
1419
1420 free (copy);
1421 }
1422
1423 x86_linux_update_xmltarget ();
1424 }
1425
1426 /* Common for x86/x86-64. */
1427
1428 static struct regsets_info x86_regsets_info =
1429 {
1430 x86_regsets, /* regsets */
1431 0, /* num_regsets */
1432 NULL, /* disabled_regsets */
1433 };
1434
1435 #ifdef __x86_64__
1436 static struct regs_info amd64_linux_regs_info =
1437 {
1438 NULL, /* regset_bitmap */
1439 NULL, /* usrregs_info */
1440 &x86_regsets_info
1441 };
1442 #endif
1443 static struct usrregs_info i386_linux_usrregs_info =
1444 {
1445 I386_NUM_REGS,
1446 i386_regmap,
1447 };
1448
1449 static struct regs_info i386_linux_regs_info =
1450 {
1451 NULL, /* regset_bitmap */
1452 &i386_linux_usrregs_info,
1453 &x86_regsets_info
1454 };
1455
1456 const struct regs_info *
1457 x86_linux_regs_info (void)
1458 {
1459 #ifdef __x86_64__
1460 if (is_64bit_tdesc ())
1461 return &amd64_linux_regs_info;
1462 else
1463 #endif
1464 return &i386_linux_regs_info;
1465 }
1466
1467 /* Initialize the target description for the architecture of the
1468 inferior. */
1469
1470 static void
1471 x86_arch_setup (void)
1472 {
1473 current_process ()->tdesc = x86_linux_read_description ();
1474 }
1475
1476 static int
1477 x86_supports_tracepoints (void)
1478 {
1479 return 1;
1480 }
1481
1482 static void
1483 append_insns (CORE_ADDR *to, size_t len, const unsigned char *buf)
1484 {
1485 write_inferior_memory (*to, buf, len);
1486 *to += len;
1487 }
1488
1489 static int
1490 push_opcode (unsigned char *buf, char *op)
1491 {
1492 unsigned char *buf_org = buf;
1493
1494 while (1)
1495 {
1496 char *endptr;
1497 unsigned long ul = strtoul (op, &endptr, 16);
1498
1499 if (endptr == op)
1500 break;
1501
1502 *buf++ = ul;
1503 op = endptr;
1504 }
1505
1506 return buf - buf_org;
1507 }
1508
1509 #ifdef __x86_64__
1510
1511 /* Build a jump pad that saves registers and calls a collection
1512 function. Writes a jump instruction to the jump pad to
1513 JJUMPAD_INSN. The caller is responsible to write it in at the
1514 tracepoint address. */
1515
1516 static int
1517 amd64_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
1518 CORE_ADDR collector,
1519 CORE_ADDR lockaddr,
1520 ULONGEST orig_size,
1521 CORE_ADDR *jump_entry,
1522 CORE_ADDR *trampoline,
1523 ULONGEST *trampoline_size,
1524 unsigned char *jjump_pad_insn,
1525 ULONGEST *jjump_pad_insn_size,
1526 CORE_ADDR *adjusted_insn_addr,
1527 CORE_ADDR *adjusted_insn_addr_end,
1528 char *err)
1529 {
1530 unsigned char buf[40];
1531 int i, offset;
1532 int64_t loffset;
1533
1534 CORE_ADDR buildaddr = *jump_entry;
1535
1536 /* Build the jump pad. */
1537
1538 /* First, do tracepoint data collection. Save registers. */
1539 i = 0;
1540 /* Need to ensure stack pointer saved first. */
1541 buf[i++] = 0x54; /* push %rsp */
1542 buf[i++] = 0x55; /* push %rbp */
1543 buf[i++] = 0x57; /* push %rdi */
1544 buf[i++] = 0x56; /* push %rsi */
1545 buf[i++] = 0x52; /* push %rdx */
1546 buf[i++] = 0x51; /* push %rcx */
1547 buf[i++] = 0x53; /* push %rbx */
1548 buf[i++] = 0x50; /* push %rax */
1549 buf[i++] = 0x41; buf[i++] = 0x57; /* push %r15 */
1550 buf[i++] = 0x41; buf[i++] = 0x56; /* push %r14 */
1551 buf[i++] = 0x41; buf[i++] = 0x55; /* push %r13 */
1552 buf[i++] = 0x41; buf[i++] = 0x54; /* push %r12 */
1553 buf[i++] = 0x41; buf[i++] = 0x53; /* push %r11 */
1554 buf[i++] = 0x41; buf[i++] = 0x52; /* push %r10 */
1555 buf[i++] = 0x41; buf[i++] = 0x51; /* push %r9 */
1556 buf[i++] = 0x41; buf[i++] = 0x50; /* push %r8 */
1557 buf[i++] = 0x9c; /* pushfq */
1558 buf[i++] = 0x48; /* movl <addr>,%rdi */
1559 buf[i++] = 0xbf;
1560 *((unsigned long *)(buf + i)) = (unsigned long) tpaddr;
1561 i += sizeof (unsigned long);
1562 buf[i++] = 0x57; /* push %rdi */
1563 append_insns (&buildaddr, i, buf);
1564
1565 /* Stack space for the collecting_t object. */
1566 i = 0;
1567 i += push_opcode (&buf[i], "48 83 ec 18"); /* sub $0x18,%rsp */
1568 i += push_opcode (&buf[i], "48 b8"); /* mov <tpoint>,%rax */
1569 memcpy (buf + i, &tpoint, 8);
1570 i += 8;
1571 i += push_opcode (&buf[i], "48 89 04 24"); /* mov %rax,(%rsp) */
1572 i += push_opcode (&buf[i],
1573 "64 48 8b 04 25 00 00 00 00"); /* mov %fs:0x0,%rax */
1574 i += push_opcode (&buf[i], "48 89 44 24 08"); /* mov %rax,0x8(%rsp) */
1575 append_insns (&buildaddr, i, buf);
1576
1577 /* spin-lock. */
1578 i = 0;
1579 i += push_opcode (&buf[i], "48 be"); /* movl <lockaddr>,%rsi */
1580 memcpy (&buf[i], (void *) &lockaddr, 8);
1581 i += 8;
1582 i += push_opcode (&buf[i], "48 89 e1"); /* mov %rsp,%rcx */
1583 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1584 i += push_opcode (&buf[i], "f0 48 0f b1 0e"); /* lock cmpxchg %rcx,(%rsi) */
1585 i += push_opcode (&buf[i], "48 85 c0"); /* test %rax,%rax */
1586 i += push_opcode (&buf[i], "75 f4"); /* jne <again> */
1587 append_insns (&buildaddr, i, buf);
1588
1589 /* Set up the gdb_collect call. */
1590 /* At this point, (stack pointer + 0x18) is the base of our saved
1591 register block. */
1592
1593 i = 0;
1594 i += push_opcode (&buf[i], "48 89 e6"); /* mov %rsp,%rsi */
1595 i += push_opcode (&buf[i], "48 83 c6 18"); /* add $0x18,%rsi */
1596
1597 /* tpoint address may be 64-bit wide. */
1598 i += push_opcode (&buf[i], "48 bf"); /* movl <addr>,%rdi */
1599 memcpy (buf + i, &tpoint, 8);
1600 i += 8;
1601 append_insns (&buildaddr, i, buf);
1602
1603 /* The collector function being in the shared library, may be
1604 >31-bits away off the jump pad. */
1605 i = 0;
1606 i += push_opcode (&buf[i], "48 b8"); /* mov $collector,%rax */
1607 memcpy (buf + i, &collector, 8);
1608 i += 8;
1609 i += push_opcode (&buf[i], "ff d0"); /* callq *%rax */
1610 append_insns (&buildaddr, i, buf);
1611
1612 /* Clear the spin-lock. */
1613 i = 0;
1614 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1615 i += push_opcode (&buf[i], "48 a3"); /* mov %rax, lockaddr */
1616 memcpy (buf + i, &lockaddr, 8);
1617 i += 8;
1618 append_insns (&buildaddr, i, buf);
1619
1620 /* Remove stack that had been used for the collect_t object. */
1621 i = 0;
1622 i += push_opcode (&buf[i], "48 83 c4 18"); /* add $0x18,%rsp */
1623 append_insns (&buildaddr, i, buf);
1624
1625 /* Restore register state. */
1626 i = 0;
1627 buf[i++] = 0x48; /* add $0x8,%rsp */
1628 buf[i++] = 0x83;
1629 buf[i++] = 0xc4;
1630 buf[i++] = 0x08;
1631 buf[i++] = 0x9d; /* popfq */
1632 buf[i++] = 0x41; buf[i++] = 0x58; /* pop %r8 */
1633 buf[i++] = 0x41; buf[i++] = 0x59; /* pop %r9 */
1634 buf[i++] = 0x41; buf[i++] = 0x5a; /* pop %r10 */
1635 buf[i++] = 0x41; buf[i++] = 0x5b; /* pop %r11 */
1636 buf[i++] = 0x41; buf[i++] = 0x5c; /* pop %r12 */
1637 buf[i++] = 0x41; buf[i++] = 0x5d; /* pop %r13 */
1638 buf[i++] = 0x41; buf[i++] = 0x5e; /* pop %r14 */
1639 buf[i++] = 0x41; buf[i++] = 0x5f; /* pop %r15 */
1640 buf[i++] = 0x58; /* pop %rax */
1641 buf[i++] = 0x5b; /* pop %rbx */
1642 buf[i++] = 0x59; /* pop %rcx */
1643 buf[i++] = 0x5a; /* pop %rdx */
1644 buf[i++] = 0x5e; /* pop %rsi */
1645 buf[i++] = 0x5f; /* pop %rdi */
1646 buf[i++] = 0x5d; /* pop %rbp */
1647 buf[i++] = 0x5c; /* pop %rsp */
1648 append_insns (&buildaddr, i, buf);
1649
1650 /* Now, adjust the original instruction to execute in the jump
1651 pad. */
1652 *adjusted_insn_addr = buildaddr;
1653 relocate_instruction (&buildaddr, tpaddr);
1654 *adjusted_insn_addr_end = buildaddr;
1655
1656 /* Finally, write a jump back to the program. */
1657
1658 loffset = (tpaddr + orig_size) - (buildaddr + sizeof (jump_insn));
1659 if (loffset > INT_MAX || loffset < INT_MIN)
1660 {
1661 sprintf (err,
1662 "E.Jump back from jump pad too far from tracepoint "
1663 "(offset 0x%" PRIx64 " > int32).", loffset);
1664 return 1;
1665 }
1666
1667 offset = (int) loffset;
1668 memcpy (buf, jump_insn, sizeof (jump_insn));
1669 memcpy (buf + 1, &offset, 4);
1670 append_insns (&buildaddr, sizeof (jump_insn), buf);
1671
1672 /* The jump pad is now built. Wire in a jump to our jump pad. This
1673 is always done last (by our caller actually), so that we can
1674 install fast tracepoints with threads running. This relies on
1675 the agent's atomic write support. */
1676 loffset = *jump_entry - (tpaddr + sizeof (jump_insn));
1677 if (loffset > INT_MAX || loffset < INT_MIN)
1678 {
1679 sprintf (err,
1680 "E.Jump pad too far from tracepoint "
1681 "(offset 0x%" PRIx64 " > int32).", loffset);
1682 return 1;
1683 }
1684
1685 offset = (int) loffset;
1686
1687 memcpy (buf, jump_insn, sizeof (jump_insn));
1688 memcpy (buf + 1, &offset, 4);
1689 memcpy (jjump_pad_insn, buf, sizeof (jump_insn));
1690 *jjump_pad_insn_size = sizeof (jump_insn);
1691
1692 /* Return the end address of our pad. */
1693 *jump_entry = buildaddr;
1694
1695 return 0;
1696 }
1697
1698 #endif /* __x86_64__ */
1699
1700 /* Build a jump pad that saves registers and calls a collection
1701 function. Writes a jump instruction to the jump pad to
1702 JJUMPAD_INSN. The caller is responsible to write it in at the
1703 tracepoint address. */
1704
1705 static int
1706 i386_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
1707 CORE_ADDR collector,
1708 CORE_ADDR lockaddr,
1709 ULONGEST orig_size,
1710 CORE_ADDR *jump_entry,
1711 CORE_ADDR *trampoline,
1712 ULONGEST *trampoline_size,
1713 unsigned char *jjump_pad_insn,
1714 ULONGEST *jjump_pad_insn_size,
1715 CORE_ADDR *adjusted_insn_addr,
1716 CORE_ADDR *adjusted_insn_addr_end,
1717 char *err)
1718 {
1719 unsigned char buf[0x100];
1720 int i, offset;
1721 CORE_ADDR buildaddr = *jump_entry;
1722
1723 /* Build the jump pad. */
1724
1725 /* First, do tracepoint data collection. Save registers. */
1726 i = 0;
1727 buf[i++] = 0x60; /* pushad */
1728 buf[i++] = 0x68; /* push tpaddr aka $pc */
1729 *((int *)(buf + i)) = (int) tpaddr;
1730 i += 4;
1731 buf[i++] = 0x9c; /* pushf */
1732 buf[i++] = 0x1e; /* push %ds */
1733 buf[i++] = 0x06; /* push %es */
1734 buf[i++] = 0x0f; /* push %fs */
1735 buf[i++] = 0xa0;
1736 buf[i++] = 0x0f; /* push %gs */
1737 buf[i++] = 0xa8;
1738 buf[i++] = 0x16; /* push %ss */
1739 buf[i++] = 0x0e; /* push %cs */
1740 append_insns (&buildaddr, i, buf);
1741
1742 /* Stack space for the collecting_t object. */
1743 i = 0;
1744 i += push_opcode (&buf[i], "83 ec 08"); /* sub $0x8,%esp */
1745
1746 /* Build the object. */
1747 i += push_opcode (&buf[i], "b8"); /* mov <tpoint>,%eax */
1748 memcpy (buf + i, &tpoint, 4);
1749 i += 4;
1750 i += push_opcode (&buf[i], "89 04 24"); /* mov %eax,(%esp) */
1751
1752 i += push_opcode (&buf[i], "65 a1 00 00 00 00"); /* mov %gs:0x0,%eax */
1753 i += push_opcode (&buf[i], "89 44 24 04"); /* mov %eax,0x4(%esp) */
1754 append_insns (&buildaddr, i, buf);
1755
1756 /* spin-lock. Note this is using cmpxchg, which leaves i386 behind.
1757 If we cared for it, this could be using xchg alternatively. */
1758
1759 i = 0;
1760 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1761 i += push_opcode (&buf[i], "f0 0f b1 25"); /* lock cmpxchg
1762 %esp,<lockaddr> */
1763 memcpy (&buf[i], (void *) &lockaddr, 4);
1764 i += 4;
1765 i += push_opcode (&buf[i], "85 c0"); /* test %eax,%eax */
1766 i += push_opcode (&buf[i], "75 f2"); /* jne <again> */
1767 append_insns (&buildaddr, i, buf);
1768
1769
1770 /* Set up arguments to the gdb_collect call. */
1771 i = 0;
1772 i += push_opcode (&buf[i], "89 e0"); /* mov %esp,%eax */
1773 i += push_opcode (&buf[i], "83 c0 08"); /* add $0x08,%eax */
1774 i += push_opcode (&buf[i], "89 44 24 fc"); /* mov %eax,-0x4(%esp) */
1775 append_insns (&buildaddr, i, buf);
1776
1777 i = 0;
1778 i += push_opcode (&buf[i], "83 ec 08"); /* sub $0x8,%esp */
1779 append_insns (&buildaddr, i, buf);
1780
1781 i = 0;
1782 i += push_opcode (&buf[i], "c7 04 24"); /* movl <addr>,(%esp) */
1783 memcpy (&buf[i], (void *) &tpoint, 4);
1784 i += 4;
1785 append_insns (&buildaddr, i, buf);
1786
1787 buf[0] = 0xe8; /* call <reladdr> */
1788 offset = collector - (buildaddr + sizeof (jump_insn));
1789 memcpy (buf + 1, &offset, 4);
1790 append_insns (&buildaddr, 5, buf);
1791 /* Clean up after the call. */
1792 buf[0] = 0x83; /* add $0x8,%esp */
1793 buf[1] = 0xc4;
1794 buf[2] = 0x08;
1795 append_insns (&buildaddr, 3, buf);
1796
1797
1798 /* Clear the spin-lock. This would need the LOCK prefix on older
1799 broken archs. */
1800 i = 0;
1801 i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */
1802 i += push_opcode (&buf[i], "a3"); /* mov %eax, lockaddr */
1803 memcpy (buf + i, &lockaddr, 4);
1804 i += 4;
1805 append_insns (&buildaddr, i, buf);
1806
1807
1808 /* Remove stack that had been used for the collect_t object. */
1809 i = 0;
1810 i += push_opcode (&buf[i], "83 c4 08"); /* add $0x08,%esp */
1811 append_insns (&buildaddr, i, buf);
1812
1813 i = 0;
1814 buf[i++] = 0x83; /* add $0x4,%esp (no pop of %cs, assume unchanged) */
1815 buf[i++] = 0xc4;
1816 buf[i++] = 0x04;
1817 buf[i++] = 0x17; /* pop %ss */
1818 buf[i++] = 0x0f; /* pop %gs */
1819 buf[i++] = 0xa9;
1820 buf[i++] = 0x0f; /* pop %fs */
1821 buf[i++] = 0xa1;
1822 buf[i++] = 0x07; /* pop %es */
1823 buf[i++] = 0x1f; /* pop %ds */
1824 buf[i++] = 0x9d; /* popf */
1825 buf[i++] = 0x83; /* add $0x4,%esp (pop of tpaddr aka $pc) */
1826 buf[i++] = 0xc4;
1827 buf[i++] = 0x04;
1828 buf[i++] = 0x61; /* popad */
1829 append_insns (&buildaddr, i, buf);
1830
1831 /* Now, adjust the original instruction to execute in the jump
1832 pad. */
1833 *adjusted_insn_addr = buildaddr;
1834 relocate_instruction (&buildaddr, tpaddr);
1835 *adjusted_insn_addr_end = buildaddr;
1836
1837 /* Write the jump back to the program. */
1838 offset = (tpaddr + orig_size) - (buildaddr + sizeof (jump_insn));
1839 memcpy (buf, jump_insn, sizeof (jump_insn));
1840 memcpy (buf + 1, &offset, 4);
1841 append_insns (&buildaddr, sizeof (jump_insn), buf);
1842
1843 /* The jump pad is now built. Wire in a jump to our jump pad. This
1844 is always done last (by our caller actually), so that we can
1845 install fast tracepoints with threads running. This relies on
1846 the agent's atomic write support. */
1847 if (orig_size == 4)
1848 {
1849 /* Create a trampoline. */
1850 *trampoline_size = sizeof (jump_insn);
1851 if (!claim_trampoline_space (*trampoline_size, trampoline))
1852 {
1853 /* No trampoline space available. */
1854 strcpy (err,
1855 "E.Cannot allocate trampoline space needed for fast "
1856 "tracepoints on 4-byte instructions.");
1857 return 1;
1858 }
1859
1860 offset = *jump_entry - (*trampoline + sizeof (jump_insn));
1861 memcpy (buf, jump_insn, sizeof (jump_insn));
1862 memcpy (buf + 1, &offset, 4);
1863 write_inferior_memory (*trampoline, buf, sizeof (jump_insn));
1864
1865 /* Use a 16-bit relative jump instruction to jump to the trampoline. */
1866 offset = (*trampoline - (tpaddr + sizeof (small_jump_insn))) & 0xffff;
1867 memcpy (buf, small_jump_insn, sizeof (small_jump_insn));
1868 memcpy (buf + 2, &offset, 2);
1869 memcpy (jjump_pad_insn, buf, sizeof (small_jump_insn));
1870 *jjump_pad_insn_size = sizeof (small_jump_insn);
1871 }
1872 else
1873 {
1874 /* Else use a 32-bit relative jump instruction. */
1875 offset = *jump_entry - (tpaddr + sizeof (jump_insn));
1876 memcpy (buf, jump_insn, sizeof (jump_insn));
1877 memcpy (buf + 1, &offset, 4);
1878 memcpy (jjump_pad_insn, buf, sizeof (jump_insn));
1879 *jjump_pad_insn_size = sizeof (jump_insn);
1880 }
1881
1882 /* Return the end address of our pad. */
1883 *jump_entry = buildaddr;
1884
1885 return 0;
1886 }
1887
1888 static int
1889 x86_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
1890 CORE_ADDR collector,
1891 CORE_ADDR lockaddr,
1892 ULONGEST orig_size,
1893 CORE_ADDR *jump_entry,
1894 CORE_ADDR *trampoline,
1895 ULONGEST *trampoline_size,
1896 unsigned char *jjump_pad_insn,
1897 ULONGEST *jjump_pad_insn_size,
1898 CORE_ADDR *adjusted_insn_addr,
1899 CORE_ADDR *adjusted_insn_addr_end,
1900 char *err)
1901 {
1902 #ifdef __x86_64__
1903 if (is_64bit_tdesc ())
1904 return amd64_install_fast_tracepoint_jump_pad (tpoint, tpaddr,
1905 collector, lockaddr,
1906 orig_size, jump_entry,
1907 trampoline, trampoline_size,
1908 jjump_pad_insn,
1909 jjump_pad_insn_size,
1910 adjusted_insn_addr,
1911 adjusted_insn_addr_end,
1912 err);
1913 #endif
1914
1915 return i386_install_fast_tracepoint_jump_pad (tpoint, tpaddr,
1916 collector, lockaddr,
1917 orig_size, jump_entry,
1918 trampoline, trampoline_size,
1919 jjump_pad_insn,
1920 jjump_pad_insn_size,
1921 adjusted_insn_addr,
1922 adjusted_insn_addr_end,
1923 err);
1924 }
1925
1926 /* Return the minimum instruction length for fast tracepoints on x86/x86-64
1927 architectures. */
1928
1929 static int
1930 x86_get_min_fast_tracepoint_insn_len (void)
1931 {
1932 static int warned_about_fast_tracepoints = 0;
1933
1934 #ifdef __x86_64__
1935 /* On x86-64, 5-byte jump instructions with a 4-byte offset are always
1936 used for fast tracepoints. */
1937 if (is_64bit_tdesc ())
1938 return 5;
1939 #endif
1940
1941 if (agent_loaded_p ())
1942 {
1943 char errbuf[IPA_BUFSIZ];
1944
1945 errbuf[0] = '\0';
1946
1947 /* On x86, if trampolines are available, then 4-byte jump instructions
1948 with a 2-byte offset may be used, otherwise 5-byte jump instructions
1949 with a 4-byte offset are used instead. */
1950 if (have_fast_tracepoint_trampoline_buffer (errbuf))
1951 return 4;
1952 else
1953 {
1954 /* GDB has no channel to explain to user why a shorter fast
1955 tracepoint is not possible, but at least make GDBserver
1956 mention that something has gone awry. */
1957 if (!warned_about_fast_tracepoints)
1958 {
1959 warning ("4-byte fast tracepoints not available; %s\n", errbuf);
1960 warned_about_fast_tracepoints = 1;
1961 }
1962 return 5;
1963 }
1964 }
1965 else
1966 {
1967 /* Indicate that the minimum length is currently unknown since the IPA
1968 has not loaded yet. */
1969 return 0;
1970 }
1971 }
1972
1973 static void
1974 add_insns (unsigned char *start, int len)
1975 {
1976 CORE_ADDR buildaddr = current_insn_ptr;
1977
1978 if (debug_threads)
1979 fprintf (stderr, "Adding %d bytes of insn at %s\n",
1980 len, paddress (buildaddr));
1981
1982 append_insns (&buildaddr, len, start);
1983 current_insn_ptr = buildaddr;
1984 }
1985
1986 /* Our general strategy for emitting code is to avoid specifying raw
1987 bytes whenever possible, and instead copy a block of inline asm
1988 that is embedded in the function. This is a little messy, because
1989 we need to keep the compiler from discarding what looks like dead
1990 code, plus suppress various warnings. */
1991
1992 #define EMIT_ASM(NAME, INSNS) \
1993 do \
1994 { \
1995 extern unsigned char start_ ## NAME, end_ ## NAME; \
1996 add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
1997 __asm__ ("jmp end_" #NAME "\n" \
1998 "\t" "start_" #NAME ":" \
1999 "\t" INSNS "\n" \
2000 "\t" "end_" #NAME ":"); \
2001 } while (0)
2002
2003 #ifdef __x86_64__
2004
2005 #define EMIT_ASM32(NAME,INSNS) \
2006 do \
2007 { \
2008 extern unsigned char start_ ## NAME, end_ ## NAME; \
2009 add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
2010 __asm__ (".code32\n" \
2011 "\t" "jmp end_" #NAME "\n" \
2012 "\t" "start_" #NAME ":\n" \
2013 "\t" INSNS "\n" \
2014 "\t" "end_" #NAME ":\n" \
2015 ".code64\n"); \
2016 } while (0)
2017
2018 #else
2019
2020 #define EMIT_ASM32(NAME,INSNS) EMIT_ASM(NAME,INSNS)
2021
2022 #endif
2023
2024 #ifdef __x86_64__
2025
2026 static void
2027 amd64_emit_prologue (void)
2028 {
2029 EMIT_ASM (amd64_prologue,
2030 "pushq %rbp\n\t"
2031 "movq %rsp,%rbp\n\t"
2032 "sub $0x20,%rsp\n\t"
2033 "movq %rdi,-8(%rbp)\n\t"
2034 "movq %rsi,-16(%rbp)");
2035 }
2036
2037
2038 static void
2039 amd64_emit_epilogue (void)
2040 {
2041 EMIT_ASM (amd64_epilogue,
2042 "movq -16(%rbp),%rdi\n\t"
2043 "movq %rax,(%rdi)\n\t"
2044 "xor %rax,%rax\n\t"
2045 "leave\n\t"
2046 "ret");
2047 }
2048
2049 static void
2050 amd64_emit_add (void)
2051 {
2052 EMIT_ASM (amd64_add,
2053 "add (%rsp),%rax\n\t"
2054 "lea 0x8(%rsp),%rsp");
2055 }
2056
2057 static void
2058 amd64_emit_sub (void)
2059 {
2060 EMIT_ASM (amd64_sub,
2061 "sub %rax,(%rsp)\n\t"
2062 "pop %rax");
2063 }
2064
2065 static void
2066 amd64_emit_mul (void)
2067 {
2068 emit_error = 1;
2069 }
2070
2071 static void
2072 amd64_emit_lsh (void)
2073 {
2074 emit_error = 1;
2075 }
2076
2077 static void
2078 amd64_emit_rsh_signed (void)
2079 {
2080 emit_error = 1;
2081 }
2082
2083 static void
2084 amd64_emit_rsh_unsigned (void)
2085 {
2086 emit_error = 1;
2087 }
2088
2089 static void
2090 amd64_emit_ext (int arg)
2091 {
2092 switch (arg)
2093 {
2094 case 8:
2095 EMIT_ASM (amd64_ext_8,
2096 "cbtw\n\t"
2097 "cwtl\n\t"
2098 "cltq");
2099 break;
2100 case 16:
2101 EMIT_ASM (amd64_ext_16,
2102 "cwtl\n\t"
2103 "cltq");
2104 break;
2105 case 32:
2106 EMIT_ASM (amd64_ext_32,
2107 "cltq");
2108 break;
2109 default:
2110 emit_error = 1;
2111 }
2112 }
2113
2114 static void
2115 amd64_emit_log_not (void)
2116 {
2117 EMIT_ASM (amd64_log_not,
2118 "test %rax,%rax\n\t"
2119 "sete %cl\n\t"
2120 "movzbq %cl,%rax");
2121 }
2122
2123 static void
2124 amd64_emit_bit_and (void)
2125 {
2126 EMIT_ASM (amd64_and,
2127 "and (%rsp),%rax\n\t"
2128 "lea 0x8(%rsp),%rsp");
2129 }
2130
2131 static void
2132 amd64_emit_bit_or (void)
2133 {
2134 EMIT_ASM (amd64_or,
2135 "or (%rsp),%rax\n\t"
2136 "lea 0x8(%rsp),%rsp");
2137 }
2138
2139 static void
2140 amd64_emit_bit_xor (void)
2141 {
2142 EMIT_ASM (amd64_xor,
2143 "xor (%rsp),%rax\n\t"
2144 "lea 0x8(%rsp),%rsp");
2145 }
2146
2147 static void
2148 amd64_emit_bit_not (void)
2149 {
2150 EMIT_ASM (amd64_bit_not,
2151 "xorq $0xffffffffffffffff,%rax");
2152 }
2153
2154 static void
2155 amd64_emit_equal (void)
2156 {
2157 EMIT_ASM (amd64_equal,
2158 "cmp %rax,(%rsp)\n\t"
2159 "je .Lamd64_equal_true\n\t"
2160 "xor %rax,%rax\n\t"
2161 "jmp .Lamd64_equal_end\n\t"
2162 ".Lamd64_equal_true:\n\t"
2163 "mov $0x1,%rax\n\t"
2164 ".Lamd64_equal_end:\n\t"
2165 "lea 0x8(%rsp),%rsp");
2166 }
2167
2168 static void
2169 amd64_emit_less_signed (void)
2170 {
2171 EMIT_ASM (amd64_less_signed,
2172 "cmp %rax,(%rsp)\n\t"
2173 "jl .Lamd64_less_signed_true\n\t"
2174 "xor %rax,%rax\n\t"
2175 "jmp .Lamd64_less_signed_end\n\t"
2176 ".Lamd64_less_signed_true:\n\t"
2177 "mov $1,%rax\n\t"
2178 ".Lamd64_less_signed_end:\n\t"
2179 "lea 0x8(%rsp),%rsp");
2180 }
2181
2182 static void
2183 amd64_emit_less_unsigned (void)
2184 {
2185 EMIT_ASM (amd64_less_unsigned,
2186 "cmp %rax,(%rsp)\n\t"
2187 "jb .Lamd64_less_unsigned_true\n\t"
2188 "xor %rax,%rax\n\t"
2189 "jmp .Lamd64_less_unsigned_end\n\t"
2190 ".Lamd64_less_unsigned_true:\n\t"
2191 "mov $1,%rax\n\t"
2192 ".Lamd64_less_unsigned_end:\n\t"
2193 "lea 0x8(%rsp),%rsp");
2194 }
2195
2196 static void
2197 amd64_emit_ref (int size)
2198 {
2199 switch (size)
2200 {
2201 case 1:
2202 EMIT_ASM (amd64_ref1,
2203 "movb (%rax),%al");
2204 break;
2205 case 2:
2206 EMIT_ASM (amd64_ref2,
2207 "movw (%rax),%ax");
2208 break;
2209 case 4:
2210 EMIT_ASM (amd64_ref4,
2211 "movl (%rax),%eax");
2212 break;
2213 case 8:
2214 EMIT_ASM (amd64_ref8,
2215 "movq (%rax),%rax");
2216 break;
2217 }
2218 }
2219
2220 static void
2221 amd64_emit_if_goto (int *offset_p, int *size_p)
2222 {
2223 EMIT_ASM (amd64_if_goto,
2224 "mov %rax,%rcx\n\t"
2225 "pop %rax\n\t"
2226 "cmp $0,%rcx\n\t"
2227 ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
2228 if (offset_p)
2229 *offset_p = 10;
2230 if (size_p)
2231 *size_p = 4;
2232 }
2233
2234 static void
2235 amd64_emit_goto (int *offset_p, int *size_p)
2236 {
2237 EMIT_ASM (amd64_goto,
2238 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
2239 if (offset_p)
2240 *offset_p = 1;
2241 if (size_p)
2242 *size_p = 4;
2243 }
2244
2245 static void
2246 amd64_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
2247 {
2248 int diff = (to - (from + size));
2249 unsigned char buf[sizeof (int)];
2250
2251 if (size != 4)
2252 {
2253 emit_error = 1;
2254 return;
2255 }
2256
2257 memcpy (buf, &diff, sizeof (int));
2258 write_inferior_memory (from, buf, sizeof (int));
2259 }
2260
2261 static void
2262 amd64_emit_const (LONGEST num)
2263 {
2264 unsigned char buf[16];
2265 int i;
2266 CORE_ADDR buildaddr = current_insn_ptr;
2267
2268 i = 0;
2269 buf[i++] = 0x48; buf[i++] = 0xb8; /* mov $<n>,%rax */
2270 memcpy (&buf[i], &num, sizeof (num));
2271 i += 8;
2272 append_insns (&buildaddr, i, buf);
2273 current_insn_ptr = buildaddr;
2274 }
2275
2276 static void
2277 amd64_emit_call (CORE_ADDR fn)
2278 {
2279 unsigned char buf[16];
2280 int i;
2281 CORE_ADDR buildaddr;
2282 LONGEST offset64;
2283
2284 /* The destination function being in the shared library, may be
2285 >31-bits away off the compiled code pad. */
2286
2287 buildaddr = current_insn_ptr;
2288
2289 offset64 = fn - (buildaddr + 1 /* call op */ + 4 /* 32-bit offset */);
2290
2291 i = 0;
2292
2293 if (offset64 > INT_MAX || offset64 < INT_MIN)
2294 {
2295 /* Offset is too large for a call. Use callq, but that requires
2296 a register, so avoid it if possible. Use r10, since it is
2297 call-clobbered, we don't have to push/pop it. */
2298 buf[i++] = 0x48; /* mov $fn,%r10 */
2299 buf[i++] = 0xba;
2300 memcpy (buf + i, &fn, 8);
2301 i += 8;
2302 buf[i++] = 0xff; /* callq *%r10 */
2303 buf[i++] = 0xd2;
2304 }
2305 else
2306 {
2307 int offset32 = offset64; /* we know we can't overflow here. */
2308 memcpy (buf + i, &offset32, 4);
2309 i += 4;
2310 }
2311
2312 append_insns (&buildaddr, i, buf);
2313 current_insn_ptr = buildaddr;
2314 }
2315
2316 static void
2317 amd64_emit_reg (int reg)
2318 {
2319 unsigned char buf[16];
2320 int i;
2321 CORE_ADDR buildaddr;
2322
2323 /* Assume raw_regs is still in %rdi. */
2324 buildaddr = current_insn_ptr;
2325 i = 0;
2326 buf[i++] = 0xbe; /* mov $<n>,%esi */
2327 memcpy (&buf[i], &reg, sizeof (reg));
2328 i += 4;
2329 append_insns (&buildaddr, i, buf);
2330 current_insn_ptr = buildaddr;
2331 amd64_emit_call (get_raw_reg_func_addr ());
2332 }
2333
2334 static void
2335 amd64_emit_pop (void)
2336 {
2337 EMIT_ASM (amd64_pop,
2338 "pop %rax");
2339 }
2340
2341 static void
2342 amd64_emit_stack_flush (void)
2343 {
2344 EMIT_ASM (amd64_stack_flush,
2345 "push %rax");
2346 }
2347
2348 static void
2349 amd64_emit_zero_ext (int arg)
2350 {
2351 switch (arg)
2352 {
2353 case 8:
2354 EMIT_ASM (amd64_zero_ext_8,
2355 "and $0xff,%rax");
2356 break;
2357 case 16:
2358 EMIT_ASM (amd64_zero_ext_16,
2359 "and $0xffff,%rax");
2360 break;
2361 case 32:
2362 EMIT_ASM (amd64_zero_ext_32,
2363 "mov $0xffffffff,%rcx\n\t"
2364 "and %rcx,%rax");
2365 break;
2366 default:
2367 emit_error = 1;
2368 }
2369 }
2370
2371 static void
2372 amd64_emit_swap (void)
2373 {
2374 EMIT_ASM (amd64_swap,
2375 "mov %rax,%rcx\n\t"
2376 "pop %rax\n\t"
2377 "push %rcx");
2378 }
2379
2380 static void
2381 amd64_emit_stack_adjust (int n)
2382 {
2383 unsigned char buf[16];
2384 int i;
2385 CORE_ADDR buildaddr = current_insn_ptr;
2386
2387 i = 0;
2388 buf[i++] = 0x48; /* lea $<n>(%rsp),%rsp */
2389 buf[i++] = 0x8d;
2390 buf[i++] = 0x64;
2391 buf[i++] = 0x24;
2392 /* This only handles adjustments up to 16, but we don't expect any more. */
2393 buf[i++] = n * 8;
2394 append_insns (&buildaddr, i, buf);
2395 current_insn_ptr = buildaddr;
2396 }
2397
2398 /* FN's prototype is `LONGEST(*fn)(int)'. */
2399
2400 static void
2401 amd64_emit_int_call_1 (CORE_ADDR fn, int arg1)
2402 {
2403 unsigned char buf[16];
2404 int i;
2405 CORE_ADDR buildaddr;
2406
2407 buildaddr = current_insn_ptr;
2408 i = 0;
2409 buf[i++] = 0xbf; /* movl $<n>,%edi */
2410 memcpy (&buf[i], &arg1, sizeof (arg1));
2411 i += 4;
2412 append_insns (&buildaddr, i, buf);
2413 current_insn_ptr = buildaddr;
2414 amd64_emit_call (fn);
2415 }
2416
2417 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
2418
2419 static void
2420 amd64_emit_void_call_2 (CORE_ADDR fn, int arg1)
2421 {
2422 unsigned char buf[16];
2423 int i;
2424 CORE_ADDR buildaddr;
2425
2426 buildaddr = current_insn_ptr;
2427 i = 0;
2428 buf[i++] = 0xbf; /* movl $<n>,%edi */
2429 memcpy (&buf[i], &arg1, sizeof (arg1));
2430 i += 4;
2431 append_insns (&buildaddr, i, buf);
2432 current_insn_ptr = buildaddr;
2433 EMIT_ASM (amd64_void_call_2_a,
2434 /* Save away a copy of the stack top. */
2435 "push %rax\n\t"
2436 /* Also pass top as the second argument. */
2437 "mov %rax,%rsi");
2438 amd64_emit_call (fn);
2439 EMIT_ASM (amd64_void_call_2_b,
2440 /* Restore the stack top, %rax may have been trashed. */
2441 "pop %rax");
2442 }
2443
2444 void
2445 amd64_emit_eq_goto (int *offset_p, int *size_p)
2446 {
2447 EMIT_ASM (amd64_eq,
2448 "cmp %rax,(%rsp)\n\t"
2449 "jne .Lamd64_eq_fallthru\n\t"
2450 "lea 0x8(%rsp),%rsp\n\t"
2451 "pop %rax\n\t"
2452 /* jmp, but don't trust the assembler to choose the right jump */
2453 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2454 ".Lamd64_eq_fallthru:\n\t"
2455 "lea 0x8(%rsp),%rsp\n\t"
2456 "pop %rax");
2457
2458 if (offset_p)
2459 *offset_p = 13;
2460 if (size_p)
2461 *size_p = 4;
2462 }
2463
2464 void
2465 amd64_emit_ne_goto (int *offset_p, int *size_p)
2466 {
2467 EMIT_ASM (amd64_ne,
2468 "cmp %rax,(%rsp)\n\t"
2469 "je .Lamd64_ne_fallthru\n\t"
2470 "lea 0x8(%rsp),%rsp\n\t"
2471 "pop %rax\n\t"
2472 /* jmp, but don't trust the assembler to choose the right jump */
2473 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2474 ".Lamd64_ne_fallthru:\n\t"
2475 "lea 0x8(%rsp),%rsp\n\t"
2476 "pop %rax");
2477
2478 if (offset_p)
2479 *offset_p = 13;
2480 if (size_p)
2481 *size_p = 4;
2482 }
2483
2484 void
2485 amd64_emit_lt_goto (int *offset_p, int *size_p)
2486 {
2487 EMIT_ASM (amd64_lt,
2488 "cmp %rax,(%rsp)\n\t"
2489 "jnl .Lamd64_lt_fallthru\n\t"
2490 "lea 0x8(%rsp),%rsp\n\t"
2491 "pop %rax\n\t"
2492 /* jmp, but don't trust the assembler to choose the right jump */
2493 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2494 ".Lamd64_lt_fallthru:\n\t"
2495 "lea 0x8(%rsp),%rsp\n\t"
2496 "pop %rax");
2497
2498 if (offset_p)
2499 *offset_p = 13;
2500 if (size_p)
2501 *size_p = 4;
2502 }
2503
2504 void
2505 amd64_emit_le_goto (int *offset_p, int *size_p)
2506 {
2507 EMIT_ASM (amd64_le,
2508 "cmp %rax,(%rsp)\n\t"
2509 "jnle .Lamd64_le_fallthru\n\t"
2510 "lea 0x8(%rsp),%rsp\n\t"
2511 "pop %rax\n\t"
2512 /* jmp, but don't trust the assembler to choose the right jump */
2513 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2514 ".Lamd64_le_fallthru:\n\t"
2515 "lea 0x8(%rsp),%rsp\n\t"
2516 "pop %rax");
2517
2518 if (offset_p)
2519 *offset_p = 13;
2520 if (size_p)
2521 *size_p = 4;
2522 }
2523
2524 void
2525 amd64_emit_gt_goto (int *offset_p, int *size_p)
2526 {
2527 EMIT_ASM (amd64_gt,
2528 "cmp %rax,(%rsp)\n\t"
2529 "jng .Lamd64_gt_fallthru\n\t"
2530 "lea 0x8(%rsp),%rsp\n\t"
2531 "pop %rax\n\t"
2532 /* jmp, but don't trust the assembler to choose the right jump */
2533 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2534 ".Lamd64_gt_fallthru:\n\t"
2535 "lea 0x8(%rsp),%rsp\n\t"
2536 "pop %rax");
2537
2538 if (offset_p)
2539 *offset_p = 13;
2540 if (size_p)
2541 *size_p = 4;
2542 }
2543
2544 void
2545 amd64_emit_ge_goto (int *offset_p, int *size_p)
2546 {
2547 EMIT_ASM (amd64_ge,
2548 "cmp %rax,(%rsp)\n\t"
2549 "jnge .Lamd64_ge_fallthru\n\t"
2550 ".Lamd64_ge_jump:\n\t"
2551 "lea 0x8(%rsp),%rsp\n\t"
2552 "pop %rax\n\t"
2553 /* jmp, but don't trust the assembler to choose the right jump */
2554 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
2555 ".Lamd64_ge_fallthru:\n\t"
2556 "lea 0x8(%rsp),%rsp\n\t"
2557 "pop %rax");
2558
2559 if (offset_p)
2560 *offset_p = 13;
2561 if (size_p)
2562 *size_p = 4;
2563 }
2564
2565 struct emit_ops amd64_emit_ops =
2566 {
2567 amd64_emit_prologue,
2568 amd64_emit_epilogue,
2569 amd64_emit_add,
2570 amd64_emit_sub,
2571 amd64_emit_mul,
2572 amd64_emit_lsh,
2573 amd64_emit_rsh_signed,
2574 amd64_emit_rsh_unsigned,
2575 amd64_emit_ext,
2576 amd64_emit_log_not,
2577 amd64_emit_bit_and,
2578 amd64_emit_bit_or,
2579 amd64_emit_bit_xor,
2580 amd64_emit_bit_not,
2581 amd64_emit_equal,
2582 amd64_emit_less_signed,
2583 amd64_emit_less_unsigned,
2584 amd64_emit_ref,
2585 amd64_emit_if_goto,
2586 amd64_emit_goto,
2587 amd64_write_goto_address,
2588 amd64_emit_const,
2589 amd64_emit_call,
2590 amd64_emit_reg,
2591 amd64_emit_pop,
2592 amd64_emit_stack_flush,
2593 amd64_emit_zero_ext,
2594 amd64_emit_swap,
2595 amd64_emit_stack_adjust,
2596 amd64_emit_int_call_1,
2597 amd64_emit_void_call_2,
2598 amd64_emit_eq_goto,
2599 amd64_emit_ne_goto,
2600 amd64_emit_lt_goto,
2601 amd64_emit_le_goto,
2602 amd64_emit_gt_goto,
2603 amd64_emit_ge_goto
2604 };
2605
2606 #endif /* __x86_64__ */
2607
2608 static void
2609 i386_emit_prologue (void)
2610 {
2611 EMIT_ASM32 (i386_prologue,
2612 "push %ebp\n\t"
2613 "mov %esp,%ebp\n\t"
2614 "push %ebx");
2615 /* At this point, the raw regs base address is at 8(%ebp), and the
2616 value pointer is at 12(%ebp). */
2617 }
2618
2619 static void
2620 i386_emit_epilogue (void)
2621 {
2622 EMIT_ASM32 (i386_epilogue,
2623 "mov 12(%ebp),%ecx\n\t"
2624 "mov %eax,(%ecx)\n\t"
2625 "mov %ebx,0x4(%ecx)\n\t"
2626 "xor %eax,%eax\n\t"
2627 "pop %ebx\n\t"
2628 "pop %ebp\n\t"
2629 "ret");
2630 }
2631
2632 static void
2633 i386_emit_add (void)
2634 {
2635 EMIT_ASM32 (i386_add,
2636 "add (%esp),%eax\n\t"
2637 "adc 0x4(%esp),%ebx\n\t"
2638 "lea 0x8(%esp),%esp");
2639 }
2640
2641 static void
2642 i386_emit_sub (void)
2643 {
2644 EMIT_ASM32 (i386_sub,
2645 "subl %eax,(%esp)\n\t"
2646 "sbbl %ebx,4(%esp)\n\t"
2647 "pop %eax\n\t"
2648 "pop %ebx\n\t");
2649 }
2650
2651 static void
2652 i386_emit_mul (void)
2653 {
2654 emit_error = 1;
2655 }
2656
2657 static void
2658 i386_emit_lsh (void)
2659 {
2660 emit_error = 1;
2661 }
2662
2663 static void
2664 i386_emit_rsh_signed (void)
2665 {
2666 emit_error = 1;
2667 }
2668
2669 static void
2670 i386_emit_rsh_unsigned (void)
2671 {
2672 emit_error = 1;
2673 }
2674
2675 static void
2676 i386_emit_ext (int arg)
2677 {
2678 switch (arg)
2679 {
2680 case 8:
2681 EMIT_ASM32 (i386_ext_8,
2682 "cbtw\n\t"
2683 "cwtl\n\t"
2684 "movl %eax,%ebx\n\t"
2685 "sarl $31,%ebx");
2686 break;
2687 case 16:
2688 EMIT_ASM32 (i386_ext_16,
2689 "cwtl\n\t"
2690 "movl %eax,%ebx\n\t"
2691 "sarl $31,%ebx");
2692 break;
2693 case 32:
2694 EMIT_ASM32 (i386_ext_32,
2695 "movl %eax,%ebx\n\t"
2696 "sarl $31,%ebx");
2697 break;
2698 default:
2699 emit_error = 1;
2700 }
2701 }
2702
2703 static void
2704 i386_emit_log_not (void)
2705 {
2706 EMIT_ASM32 (i386_log_not,
2707 "or %ebx,%eax\n\t"
2708 "test %eax,%eax\n\t"
2709 "sete %cl\n\t"
2710 "xor %ebx,%ebx\n\t"
2711 "movzbl %cl,%eax");
2712 }
2713
2714 static void
2715 i386_emit_bit_and (void)
2716 {
2717 EMIT_ASM32 (i386_and,
2718 "and (%esp),%eax\n\t"
2719 "and 0x4(%esp),%ebx\n\t"
2720 "lea 0x8(%esp),%esp");
2721 }
2722
2723 static void
2724 i386_emit_bit_or (void)
2725 {
2726 EMIT_ASM32 (i386_or,
2727 "or (%esp),%eax\n\t"
2728 "or 0x4(%esp),%ebx\n\t"
2729 "lea 0x8(%esp),%esp");
2730 }
2731
2732 static void
2733 i386_emit_bit_xor (void)
2734 {
2735 EMIT_ASM32 (i386_xor,
2736 "xor (%esp),%eax\n\t"
2737 "xor 0x4(%esp),%ebx\n\t"
2738 "lea 0x8(%esp),%esp");
2739 }
2740
2741 static void
2742 i386_emit_bit_not (void)
2743 {
2744 EMIT_ASM32 (i386_bit_not,
2745 "xor $0xffffffff,%eax\n\t"
2746 "xor $0xffffffff,%ebx\n\t");
2747 }
2748
2749 static void
2750 i386_emit_equal (void)
2751 {
2752 EMIT_ASM32 (i386_equal,
2753 "cmpl %ebx,4(%esp)\n\t"
2754 "jne .Li386_equal_false\n\t"
2755 "cmpl %eax,(%esp)\n\t"
2756 "je .Li386_equal_true\n\t"
2757 ".Li386_equal_false:\n\t"
2758 "xor %eax,%eax\n\t"
2759 "jmp .Li386_equal_end\n\t"
2760 ".Li386_equal_true:\n\t"
2761 "mov $1,%eax\n\t"
2762 ".Li386_equal_end:\n\t"
2763 "xor %ebx,%ebx\n\t"
2764 "lea 0x8(%esp),%esp");
2765 }
2766
2767 static void
2768 i386_emit_less_signed (void)
2769 {
2770 EMIT_ASM32 (i386_less_signed,
2771 "cmpl %ebx,4(%esp)\n\t"
2772 "jl .Li386_less_signed_true\n\t"
2773 "jne .Li386_less_signed_false\n\t"
2774 "cmpl %eax,(%esp)\n\t"
2775 "jl .Li386_less_signed_true\n\t"
2776 ".Li386_less_signed_false:\n\t"
2777 "xor %eax,%eax\n\t"
2778 "jmp .Li386_less_signed_end\n\t"
2779 ".Li386_less_signed_true:\n\t"
2780 "mov $1,%eax\n\t"
2781 ".Li386_less_signed_end:\n\t"
2782 "xor %ebx,%ebx\n\t"
2783 "lea 0x8(%esp),%esp");
2784 }
2785
2786 static void
2787 i386_emit_less_unsigned (void)
2788 {
2789 EMIT_ASM32 (i386_less_unsigned,
2790 "cmpl %ebx,4(%esp)\n\t"
2791 "jb .Li386_less_unsigned_true\n\t"
2792 "jne .Li386_less_unsigned_false\n\t"
2793 "cmpl %eax,(%esp)\n\t"
2794 "jb .Li386_less_unsigned_true\n\t"
2795 ".Li386_less_unsigned_false:\n\t"
2796 "xor %eax,%eax\n\t"
2797 "jmp .Li386_less_unsigned_end\n\t"
2798 ".Li386_less_unsigned_true:\n\t"
2799 "mov $1,%eax\n\t"
2800 ".Li386_less_unsigned_end:\n\t"
2801 "xor %ebx,%ebx\n\t"
2802 "lea 0x8(%esp),%esp");
2803 }
2804
2805 static void
2806 i386_emit_ref (int size)
2807 {
2808 switch (size)
2809 {
2810 case 1:
2811 EMIT_ASM32 (i386_ref1,
2812 "movb (%eax),%al");
2813 break;
2814 case 2:
2815 EMIT_ASM32 (i386_ref2,
2816 "movw (%eax),%ax");
2817 break;
2818 case 4:
2819 EMIT_ASM32 (i386_ref4,
2820 "movl (%eax),%eax");
2821 break;
2822 case 8:
2823 EMIT_ASM32 (i386_ref8,
2824 "movl 4(%eax),%ebx\n\t"
2825 "movl (%eax),%eax");
2826 break;
2827 }
2828 }
2829
2830 static void
2831 i386_emit_if_goto (int *offset_p, int *size_p)
2832 {
2833 EMIT_ASM32 (i386_if_goto,
2834 "mov %eax,%ecx\n\t"
2835 "or %ebx,%ecx\n\t"
2836 "pop %eax\n\t"
2837 "pop %ebx\n\t"
2838 "cmpl $0,%ecx\n\t"
2839 /* Don't trust the assembler to choose the right jump */
2840 ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
2841
2842 if (offset_p)
2843 *offset_p = 11; /* be sure that this matches the sequence above */
2844 if (size_p)
2845 *size_p = 4;
2846 }
2847
2848 static void
2849 i386_emit_goto (int *offset_p, int *size_p)
2850 {
2851 EMIT_ASM32 (i386_goto,
2852 /* Don't trust the assembler to choose the right jump */
2853 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
2854 if (offset_p)
2855 *offset_p = 1;
2856 if (size_p)
2857 *size_p = 4;
2858 }
2859
2860 static void
2861 i386_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
2862 {
2863 int diff = (to - (from + size));
2864 unsigned char buf[sizeof (int)];
2865
2866 /* We're only doing 4-byte sizes at the moment. */
2867 if (size != 4)
2868 {
2869 emit_error = 1;
2870 return;
2871 }
2872
2873 memcpy (buf, &diff, sizeof (int));
2874 write_inferior_memory (from, buf, sizeof (int));
2875 }
2876
2877 static void
2878 i386_emit_const (LONGEST num)
2879 {
2880 unsigned char buf[16];
2881 int i, hi, lo;
2882 CORE_ADDR buildaddr = current_insn_ptr;
2883
2884 i = 0;
2885 buf[i++] = 0xb8; /* mov $<n>,%eax */
2886 lo = num & 0xffffffff;
2887 memcpy (&buf[i], &lo, sizeof (lo));
2888 i += 4;
2889 hi = ((num >> 32) & 0xffffffff);
2890 if (hi)
2891 {
2892 buf[i++] = 0xbb; /* mov $<n>,%ebx */
2893 memcpy (&buf[i], &hi, sizeof (hi));
2894 i += 4;
2895 }
2896 else
2897 {
2898 buf[i++] = 0x31; buf[i++] = 0xdb; /* xor %ebx,%ebx */
2899 }
2900 append_insns (&buildaddr, i, buf);
2901 current_insn_ptr = buildaddr;
2902 }
2903
2904 static void
2905 i386_emit_call (CORE_ADDR fn)
2906 {
2907 unsigned char buf[16];
2908 int i, offset;
2909 CORE_ADDR buildaddr;
2910
2911 buildaddr = current_insn_ptr;
2912 i = 0;
2913 buf[i++] = 0xe8; /* call <reladdr> */
2914 offset = ((int) fn) - (buildaddr + 5);
2915 memcpy (buf + 1, &offset, 4);
2916 append_insns (&buildaddr, 5, buf);
2917 current_insn_ptr = buildaddr;
2918 }
2919
2920 static void
2921 i386_emit_reg (int reg)
2922 {
2923 unsigned char buf[16];
2924 int i;
2925 CORE_ADDR buildaddr;
2926
2927 EMIT_ASM32 (i386_reg_a,
2928 "sub $0x8,%esp");
2929 buildaddr = current_insn_ptr;
2930 i = 0;
2931 buf[i++] = 0xb8; /* mov $<n>,%eax */
2932 memcpy (&buf[i], &reg, sizeof (reg));
2933 i += 4;
2934 append_insns (&buildaddr, i, buf);
2935 current_insn_ptr = buildaddr;
2936 EMIT_ASM32 (i386_reg_b,
2937 "mov %eax,4(%esp)\n\t"
2938 "mov 8(%ebp),%eax\n\t"
2939 "mov %eax,(%esp)");
2940 i386_emit_call (get_raw_reg_func_addr ());
2941 EMIT_ASM32 (i386_reg_c,
2942 "xor %ebx,%ebx\n\t"
2943 "lea 0x8(%esp),%esp");
2944 }
2945
2946 static void
2947 i386_emit_pop (void)
2948 {
2949 EMIT_ASM32 (i386_pop,
2950 "pop %eax\n\t"
2951 "pop %ebx");
2952 }
2953
2954 static void
2955 i386_emit_stack_flush (void)
2956 {
2957 EMIT_ASM32 (i386_stack_flush,
2958 "push %ebx\n\t"
2959 "push %eax");
2960 }
2961
2962 static void
2963 i386_emit_zero_ext (int arg)
2964 {
2965 switch (arg)
2966 {
2967 case 8:
2968 EMIT_ASM32 (i386_zero_ext_8,
2969 "and $0xff,%eax\n\t"
2970 "xor %ebx,%ebx");
2971 break;
2972 case 16:
2973 EMIT_ASM32 (i386_zero_ext_16,
2974 "and $0xffff,%eax\n\t"
2975 "xor %ebx,%ebx");
2976 break;
2977 case 32:
2978 EMIT_ASM32 (i386_zero_ext_32,
2979 "xor %ebx,%ebx");
2980 break;
2981 default:
2982 emit_error = 1;
2983 }
2984 }
2985
2986 static void
2987 i386_emit_swap (void)
2988 {
2989 EMIT_ASM32 (i386_swap,
2990 "mov %eax,%ecx\n\t"
2991 "mov %ebx,%edx\n\t"
2992 "pop %eax\n\t"
2993 "pop %ebx\n\t"
2994 "push %edx\n\t"
2995 "push %ecx");
2996 }
2997
2998 static void
2999 i386_emit_stack_adjust (int n)
3000 {
3001 unsigned char buf[16];
3002 int i;
3003 CORE_ADDR buildaddr = current_insn_ptr;
3004
3005 i = 0;
3006 buf[i++] = 0x8d; /* lea $<n>(%esp),%esp */
3007 buf[i++] = 0x64;
3008 buf[i++] = 0x24;
3009 buf[i++] = n * 8;
3010 append_insns (&buildaddr, i, buf);
3011 current_insn_ptr = buildaddr;
3012 }
3013
3014 /* FN's prototype is `LONGEST(*fn)(int)'. */
3015
3016 static void
3017 i386_emit_int_call_1 (CORE_ADDR fn, int arg1)
3018 {
3019 unsigned char buf[16];
3020 int i;
3021 CORE_ADDR buildaddr;
3022
3023 EMIT_ASM32 (i386_int_call_1_a,
3024 /* Reserve a bit of stack space. */
3025 "sub $0x8,%esp");
3026 /* Put the one argument on the stack. */
3027 buildaddr = current_insn_ptr;
3028 i = 0;
3029 buf[i++] = 0xc7; /* movl $<arg1>,(%esp) */
3030 buf[i++] = 0x04;
3031 buf[i++] = 0x24;
3032 memcpy (&buf[i], &arg1, sizeof (arg1));
3033 i += 4;
3034 append_insns (&buildaddr, i, buf);
3035 current_insn_ptr = buildaddr;
3036 i386_emit_call (fn);
3037 EMIT_ASM32 (i386_int_call_1_c,
3038 "mov %edx,%ebx\n\t"
3039 "lea 0x8(%esp),%esp");
3040 }
3041
3042 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
3043
3044 static void
3045 i386_emit_void_call_2 (CORE_ADDR fn, int arg1)
3046 {
3047 unsigned char buf[16];
3048 int i;
3049 CORE_ADDR buildaddr;
3050
3051 EMIT_ASM32 (i386_void_call_2_a,
3052 /* Preserve %eax only; we don't have to worry about %ebx. */
3053 "push %eax\n\t"
3054 /* Reserve a bit of stack space for arguments. */
3055 "sub $0x10,%esp\n\t"
3056 /* Copy "top" to the second argument position. (Note that
3057 we can't assume function won't scribble on its
3058 arguments, so don't try to restore from this.) */
3059 "mov %eax,4(%esp)\n\t"
3060 "mov %ebx,8(%esp)");
3061 /* Put the first argument on the stack. */
3062 buildaddr = current_insn_ptr;
3063 i = 0;
3064 buf[i++] = 0xc7; /* movl $<arg1>,(%esp) */
3065 buf[i++] = 0x04;
3066 buf[i++] = 0x24;
3067 memcpy (&buf[i], &arg1, sizeof (arg1));
3068 i += 4;
3069 append_insns (&buildaddr, i, buf);
3070 current_insn_ptr = buildaddr;
3071 i386_emit_call (fn);
3072 EMIT_ASM32 (i386_void_call_2_b,
3073 "lea 0x10(%esp),%esp\n\t"
3074 /* Restore original stack top. */
3075 "pop %eax");
3076 }
3077
3078
3079 void
3080 i386_emit_eq_goto (int *offset_p, int *size_p)
3081 {
3082 EMIT_ASM32 (eq,
3083 /* Check low half first, more likely to be decider */
3084 "cmpl %eax,(%esp)\n\t"
3085 "jne .Leq_fallthru\n\t"
3086 "cmpl %ebx,4(%esp)\n\t"
3087 "jne .Leq_fallthru\n\t"
3088 "lea 0x8(%esp),%esp\n\t"
3089 "pop %eax\n\t"
3090 "pop %ebx\n\t"
3091 /* jmp, but don't trust the assembler to choose the right jump */
3092 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3093 ".Leq_fallthru:\n\t"
3094 "lea 0x8(%esp),%esp\n\t"
3095 "pop %eax\n\t"
3096 "pop %ebx");
3097
3098 if (offset_p)
3099 *offset_p = 18;
3100 if (size_p)
3101 *size_p = 4;
3102 }
3103
3104 void
3105 i386_emit_ne_goto (int *offset_p, int *size_p)
3106 {
3107 EMIT_ASM32 (ne,
3108 /* Check low half first, more likely to be decider */
3109 "cmpl %eax,(%esp)\n\t"
3110 "jne .Lne_jump\n\t"
3111 "cmpl %ebx,4(%esp)\n\t"
3112 "je .Lne_fallthru\n\t"
3113 ".Lne_jump:\n\t"
3114 "lea 0x8(%esp),%esp\n\t"
3115 "pop %eax\n\t"
3116 "pop %ebx\n\t"
3117 /* jmp, but don't trust the assembler to choose the right jump */
3118 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3119 ".Lne_fallthru:\n\t"
3120 "lea 0x8(%esp),%esp\n\t"
3121 "pop %eax\n\t"
3122 "pop %ebx");
3123
3124 if (offset_p)
3125 *offset_p = 18;
3126 if (size_p)
3127 *size_p = 4;
3128 }
3129
3130 void
3131 i386_emit_lt_goto (int *offset_p, int *size_p)
3132 {
3133 EMIT_ASM32 (lt,
3134 "cmpl %ebx,4(%esp)\n\t"
3135 "jl .Llt_jump\n\t"
3136 "jne .Llt_fallthru\n\t"
3137 "cmpl %eax,(%esp)\n\t"
3138 "jnl .Llt_fallthru\n\t"
3139 ".Llt_jump:\n\t"
3140 "lea 0x8(%esp),%esp\n\t"
3141 "pop %eax\n\t"
3142 "pop %ebx\n\t"
3143 /* jmp, but don't trust the assembler to choose the right jump */
3144 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3145 ".Llt_fallthru:\n\t"
3146 "lea 0x8(%esp),%esp\n\t"
3147 "pop %eax\n\t"
3148 "pop %ebx");
3149
3150 if (offset_p)
3151 *offset_p = 20;
3152 if (size_p)
3153 *size_p = 4;
3154 }
3155
3156 void
3157 i386_emit_le_goto (int *offset_p, int *size_p)
3158 {
3159 EMIT_ASM32 (le,
3160 "cmpl %ebx,4(%esp)\n\t"
3161 "jle .Lle_jump\n\t"
3162 "jne .Lle_fallthru\n\t"
3163 "cmpl %eax,(%esp)\n\t"
3164 "jnle .Lle_fallthru\n\t"
3165 ".Lle_jump:\n\t"
3166 "lea 0x8(%esp),%esp\n\t"
3167 "pop %eax\n\t"
3168 "pop %ebx\n\t"
3169 /* jmp, but don't trust the assembler to choose the right jump */
3170 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3171 ".Lle_fallthru:\n\t"
3172 "lea 0x8(%esp),%esp\n\t"
3173 "pop %eax\n\t"
3174 "pop %ebx");
3175
3176 if (offset_p)
3177 *offset_p = 20;
3178 if (size_p)
3179 *size_p = 4;
3180 }
3181
3182 void
3183 i386_emit_gt_goto (int *offset_p, int *size_p)
3184 {
3185 EMIT_ASM32 (gt,
3186 "cmpl %ebx,4(%esp)\n\t"
3187 "jg .Lgt_jump\n\t"
3188 "jne .Lgt_fallthru\n\t"
3189 "cmpl %eax,(%esp)\n\t"
3190 "jng .Lgt_fallthru\n\t"
3191 ".Lgt_jump:\n\t"
3192 "lea 0x8(%esp),%esp\n\t"
3193 "pop %eax\n\t"
3194 "pop %ebx\n\t"
3195 /* jmp, but don't trust the assembler to choose the right jump */
3196 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3197 ".Lgt_fallthru:\n\t"
3198 "lea 0x8(%esp),%esp\n\t"
3199 "pop %eax\n\t"
3200 "pop %ebx");
3201
3202 if (offset_p)
3203 *offset_p = 20;
3204 if (size_p)
3205 *size_p = 4;
3206 }
3207
3208 void
3209 i386_emit_ge_goto (int *offset_p, int *size_p)
3210 {
3211 EMIT_ASM32 (ge,
3212 "cmpl %ebx,4(%esp)\n\t"
3213 "jge .Lge_jump\n\t"
3214 "jne .Lge_fallthru\n\t"
3215 "cmpl %eax,(%esp)\n\t"
3216 "jnge .Lge_fallthru\n\t"
3217 ".Lge_jump:\n\t"
3218 "lea 0x8(%esp),%esp\n\t"
3219 "pop %eax\n\t"
3220 "pop %ebx\n\t"
3221 /* jmp, but don't trust the assembler to choose the right jump */
3222 ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t"
3223 ".Lge_fallthru:\n\t"
3224 "lea 0x8(%esp),%esp\n\t"
3225 "pop %eax\n\t"
3226 "pop %ebx");
3227
3228 if (offset_p)
3229 *offset_p = 20;
3230 if (size_p)
3231 *size_p = 4;
3232 }
3233
3234 struct emit_ops i386_emit_ops =
3235 {
3236 i386_emit_prologue,
3237 i386_emit_epilogue,
3238 i386_emit_add,
3239 i386_emit_sub,
3240 i386_emit_mul,
3241 i386_emit_lsh,
3242 i386_emit_rsh_signed,
3243 i386_emit_rsh_unsigned,
3244 i386_emit_ext,
3245 i386_emit_log_not,
3246 i386_emit_bit_and,
3247 i386_emit_bit_or,
3248 i386_emit_bit_xor,
3249 i386_emit_bit_not,
3250 i386_emit_equal,
3251 i386_emit_less_signed,
3252 i386_emit_less_unsigned,
3253 i386_emit_ref,
3254 i386_emit_if_goto,
3255 i386_emit_goto,
3256 i386_write_goto_address,
3257 i386_emit_const,
3258 i386_emit_call,
3259 i386_emit_reg,
3260 i386_emit_pop,
3261 i386_emit_stack_flush,
3262 i386_emit_zero_ext,
3263 i386_emit_swap,
3264 i386_emit_stack_adjust,
3265 i386_emit_int_call_1,
3266 i386_emit_void_call_2,
3267 i386_emit_eq_goto,
3268 i386_emit_ne_goto,
3269 i386_emit_lt_goto,
3270 i386_emit_le_goto,
3271 i386_emit_gt_goto,
3272 i386_emit_ge_goto
3273 };
3274
3275
3276 static struct emit_ops *
3277 x86_emit_ops (void)
3278 {
3279 #ifdef __x86_64__
3280 if (is_64bit_tdesc ())
3281 return &amd64_emit_ops;
3282 else
3283 #endif
3284 return &i386_emit_ops;
3285 }
3286
3287 static int
3288 x86_supports_range_stepping (void)
3289 {
3290 return 1;
3291 }
3292
3293 /* This is initialized assuming an amd64 target.
3294 x86_arch_setup will correct it for i386 or amd64 targets. */
3295
3296 struct linux_target_ops the_low_target =
3297 {
3298 x86_arch_setup,
3299 x86_linux_regs_info,
3300 x86_cannot_fetch_register,
3301 x86_cannot_store_register,
3302 NULL, /* fetch_register */
3303 x86_get_pc,
3304 x86_set_pc,
3305 x86_breakpoint,
3306 x86_breakpoint_len,
3307 NULL,
3308 1,
3309 x86_breakpoint_at,
3310 x86_insert_point,
3311 x86_remove_point,
3312 x86_stopped_by_watchpoint,
3313 x86_stopped_data_address,
3314 /* collect_ptrace_register/supply_ptrace_register are not needed in the
3315 native i386 case (no registers smaller than an xfer unit), and are not
3316 used in the biarch case (HAVE_LINUX_USRREGS is not defined). */
3317 NULL,
3318 NULL,
3319 /* need to fix up i386 siginfo if host is amd64 */
3320 x86_siginfo_fixup,
3321 x86_linux_new_process,
3322 x86_linux_new_thread,
3323 x86_linux_prepare_to_resume,
3324 x86_linux_process_qsupported,
3325 x86_supports_tracepoints,
3326 x86_get_thread_area,
3327 x86_install_fast_tracepoint_jump_pad,
3328 x86_emit_ops,
3329 x86_get_min_fast_tracepoint_insn_len,
3330 x86_supports_range_stepping,
3331 };
3332
3333 void
3334 initialize_low_arch (void)
3335 {
3336 /* Initialize the Linux target descriptions. */
3337 #ifdef __x86_64__
3338 init_registers_amd64_linux ();
3339 init_registers_amd64_avx_linux ();
3340 init_registers_x32_linux ();
3341 init_registers_x32_avx_linux ();
3342
3343 tdesc_amd64_linux_no_xml = xmalloc (sizeof (struct target_desc));
3344 copy_target_description (tdesc_amd64_linux_no_xml, tdesc_amd64_linux);
3345 tdesc_amd64_linux_no_xml->xmltarget = xmltarget_amd64_linux_no_xml;
3346 #endif
3347 init_registers_i386_linux ();
3348 init_registers_i386_mmx_linux ();
3349 init_registers_i386_avx_linux ();
3350
3351 tdesc_i386_linux_no_xml = xmalloc (sizeof (struct target_desc));
3352 copy_target_description (tdesc_i386_linux_no_xml, tdesc_i386_linux);
3353 tdesc_i386_linux_no_xml->xmltarget = xmltarget_i386_linux_no_xml;
3354
3355 initialize_regsets_info (&x86_regsets_info);
3356 }
This page took 0.116406 seconds and 5 git commands to generate.