-
Notifications
You must be signed in to change notification settings - Fork 1
/
x86-64-linux-backend.c
1218 lines (1080 loc) · 29.9 KB
/
x86-64-linux-backend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Agent backend bits specific to x86-64 Linux.
Copyright (C) 2012 Mentor Graphics.
This file is part of dagent, the Debugging Agent.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "backend.h"
#include "agent.h"
#include "agent-expr.h"
#include "tracepoint.h"
#include "log.h"
extern int agent_mem_read_to (unsigned char *to,
gdb_addr_t from, gdb_size_t len);
static void x86_64_emit_call (void *fn);
#define JUMP_SIZE 5
static const unsigned char jump_insn[] = { 0xe9, 0, 0, 0, 0 };
#define MAX_INSN_LENGTH 16
/* This enum lists registers in the order that GDB expects to see
them, and so should be sync'ed with amd64-tdep.c . */
enum gdb_reg_num
{
GDB_REG_invalid = -1,
GDB_REG_RAX,
GDB_REG_RBX,
GDB_REG_RCX,
GDB_REG_RDX,
GDB_REG_RSI,
GDB_REG_RDI,
GDB_REG_RBP,
GDB_REG_RSP,
GDB_REG_R8,
GDB_REG_R9,
GDB_REG_R10,
GDB_REG_R11,
GDB_REG_R12,
GDB_REG_R13,
GDB_REG_R14,
GDB_REG_R15,
GDB_REG_RIP,
GDB_REG_EFLAGS,
GDB_REG_CS,
GDB_REG_SS,
GDB_REG_DS,
GDB_REG_ES,
GDB_REG_FS,
GDB_REG_GS,
GDB_REG_ST0,
dummy
};
/* Table mapping signal context positions to GDB's numbering of registers. The
ordering comes from <sys/ucontext.h> . */
static const enum gdb_reg_num
signal_reg_to_gdb_reg_map[] =
{
GDB_REG_R8,
GDB_REG_R9,
GDB_REG_R10,
GDB_REG_R11,
GDB_REG_R12,
GDB_REG_R13,
GDB_REG_R14,
GDB_REG_R15,
GDB_REG_RDI,
GDB_REG_RSI,
GDB_REG_RBP,
GDB_REG_RBX,
GDB_REG_RDX,
GDB_REG_RAX,
GDB_REG_RCX,
GDB_REG_RSP,
GDB_REG_RIP,
GDB_REG_EFLAGS
};
#define GBUFSIZE 544
static uint64_t
x86_64_get_reg (agent_thread_info_t *tinfo, int regnum)
{
return ((unsigned int *) (tinfo->regblock))[regnum];
}
static void
x86_64_set_reg (agent_thread_info_t *tinfo, int regnum, uint64_t val)
{
((unsigned int *) (tinfo->regblock))[regnum] = val;
}
/* References below are from: "AMD64 Architecture programmer’s Manual
Volume 3: General-Purpose and System Instructions" (rev 3.17). */
#define ADDR_PREFIX 0x67
#define DATA_PREFIX 0x66
#define LOCK_PREFIX 0xf0
#define CS_PREFIX 0x2e
#define DS_PREFIX 0x3e
#define ES_PREFIX 0x26
#define FS_PREFIX 0x64
#define GS_PREFIX 0x65
#define SS_PREFIX 0x36
#define REPE_PREFIX 0xf3
#define REPNE_PREFIX 0xf2
/* Given a pointer to bytes of an instruction INSN, advance it past the
various prefixes that are possible. END is the address at which to stop
scanning, just in case. Return the pointer to base instruction, or NULL
if error. */
static unsigned char *
skip_insn_prefixes (unsigned char *insn, unsigned char *end)
{
for (; insn < end; ++insn)
{
switch (*insn)
{
case ADDR_PREFIX:
case DATA_PREFIX:
case LOCK_PREFIX:
case CS_PREFIX:
case DS_PREFIX:
case ES_PREFIX:
case FS_PREFIX:
case GS_PREFIX:
case SS_PREFIX:
case REPE_PREFIX:
case REPNE_PREFIX:
break;
default:
return insn;
}
}
/* Something is very wrong if we get here. */
return NULL;
}
/* From Appendix A - Opcode and Operand Encodings, Tables A-1, A-2,
A-3 and A-4. */
/* The use this macros instead of hard-coding a 0 is just to help
revisiting the tables in the architecture manual. */
/* Invalid opcode. */
#define X 0
/* Invalid opcode in 64-bit. */
#define x 0
/* A Null Prefix in 64-bit mode. */
#define N 0
/* REX prefix in 64-bit mode. */
#define R 0
/* Reserved. */
#define r 0
static const char one_byte_op_has_ModRM[256] =
{
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* ----------------------- ---------------------- */
/* 0x00 */ 1, 1, 1, 1, 0, 0, x, x, 1, 1, 1, 1, 0, 0, x, x,
/* 0x10 */ 1, 1, 1, 1, 0, 0, x, x, 1, 1, 1, 1, 0, 0, x, x,
/* 0x20 */ 1, 1, 1, 1, 0, 0, N, x, 1, 1, 1, 1, 0, 0, N, x,
/* 0x30 */ 1, 1, 1, 1, 0, 0, N, x, 1, 1, 1, 1, 0, 0, N, x,
/* 0x40 */ R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
/* 0x50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x60 */ x, x, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0,
/* 0x70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x80 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0x90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0xa0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0xb0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0xc0 */ 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0xd0 */ 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0xe0 */ 0, 0, 0, x, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0xf0 */ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1
};
static char two_byte_op_has_ModRM[256] =
{
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* ----------------------- ---------------------- */
/* 0x00 */ 1, 1, 1, 1, X, 0, 0, 0, 0, 0, X, 0, X, 1, 0, 1,
/* 0x10 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0x20 */ 1, 1, 1, 1, X, X, X, X, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0x30 */ 0, 0, 0, 0, 0, 0, X, X, X, X, X, X, X, X, X, X,
/* 0x40 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0x50 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0x60 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, X, X, 1, 1,
/* 0x70 */ 1, 1, 1, 1, 1, 1, 1, 0, X, X, X, X, X, X, 1, 1,
/* 0x80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x90 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0xa0 */ 0, 0, 0, 1, 1, 1, X, X, 0, 0, 0, 1, 1, 1, 1, 1,
/* 0xb0 */ 1, 1, 1, 1, 1, 1, 1, 1, r, 1, 1, 1, 1, 1, 1, 1,
/* 0xc0 */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0xd0 */ X, 1, 1, 1, 1, 1, X, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0xe0 */ 1, 1, 1, 1, 1, 1, X, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 0xf0 */ X, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, X
};
#undef x
#undef X
#undef N
#undef R
#undef r
/* Parse instruction at INSN, and if it uses RIP-relative addressing,
return the offset into INSN where the displacement to be adjusted
is found. */
static int
rip_relative_offset (unsigned char *insn)
{
unsigned char *p;
unsigned char *modrm = NULL;
p = insn;
/* Skip AMD64 register extensions / REX instruction prefix. The
value of a REX prefix is in the range 40h through 4Fh, depending
on the particular combination of AMD64 register extensions
desired. */
if ((*p & 0xf0) == 0x40)
++p;
/* Follows the opcode. It can be 1 or 2 bytes long. All two-byte
opcodes have 0x0f as their first byte. */
if (*p != 0x0f)
{
if (one_byte_op_has_ModRM[*p])
{
++p;
modrm = p;
}
}
else
{
++p;
if (two_byte_op_has_ModRM[*p])
{
++p;
modrm = insn;
}
}
/* When RIP-relative addressing is in use, ModRM.mod == 0, and
ModRM.rm = 0b101. */
if (modrm != NULL && (*modrm & 0xc7) == 0x05)
{
/* The displacement is found right after the ModRM byte. */
++p;
return p - insn;
}
return 0;
}
/* Unlike displaced stepping, for fast tracepoints we want to alter
the replaced instructions, because we won't have a post-single-step
stop in which to fix up registers or memory.
Fortunately we don't have to worry about adjusting every imaginable
instruction. For instance, small-displacement jumps aren't an issue,
because they are too short to be a fast tracepoint site anyway.
TO is the address in scratch space to modify. OLDLOD is the original
location of instruction INSN. NBYTES is the number of bytes.
*/
static void
adjust_jump_pad_insns (gdb_addr_t *to, gdb_addr_t oldloc,
size_t nbytes, unsigned char *insn)
{
int offset = 0, rel32, newrel;
unsigned char *insn0 = insn;
/* Get past the prefixes. */
insn = skip_insn_prefixes (insn, insn + MAX_INSN_LENGTH);
/* Adjust calls with 32-bit relative addresses as push/jump, with
the address pushed being the location where the original call in
the user program would return to. */
if (insn[0] == 0xe8)
{
unsigned char buf[5];
unsigned int ret_addr;
/* Where "ret" in the original code will return to. */
ret_addr = oldloc + nbytes;
buf[0] = 0x68; /* pushq $... */
memcpy (&buf[1], &ret_addr, 4);
/* Push the push. */
append_insns (to, 5, buf);
/* Convert the relative call to a relative jump. */
insn[0] = 0xe9;
/* Adjust the destination offset. */
rel32 = *((int *) (insn + 1));
newrel = (oldloc - *to) + rel32;
*((int *) (insn + 1)) = newrel;
/* Write the adjusted jump into its displaced location. */
append_insns (to, nbytes, insn0);
return;
}
offset = rip_relative_offset (insn);
if (!offset)
{
/* Adjust jumps with 32-bit relative addresses. Calls are
already handled above. */
if (insn[0] == 0xe9)
offset = 1;
/* Adjust conditional jumps. */
else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
offset = 2;
}
if (offset)
{
rel32 = *((int *) (insn + offset));
newrel = (oldloc - *to) + rel32;
*((int *) (insn + offset)) = newrel;
gdb_verbose ("Adjusted insn rel32=0x%x at 0x%" PRIx64
" to rel32=0x%x at 0x%" PRIx64,
rel32, oldloc, newrel, *to);
}
/* Write the adjusted instructions into their displaced
location, being sure to copy any prefixes too! */
append_insns (to, nbytes, insn0);
}
/* Trampolines are not used on x86-64. */
/* Build a jump pad for tracepoint TPOINT installed at TPADDR that saves
registers and calls a collection function COLLECTOR. Writes a jump
instruction to the jump pad to JJUMPAD_INSN and JJUMPPAD_INSN_SIZE
is the size of the jump instruction. The caller is responsible to write
it in at the tracepoint address. ORIG_BYTES is the bytes of the instruction
being replaced, and ORIG_SIZE is the length of instruction. The base address
of the jump pad is JUMP_ENTRY. TRAMPOLINE is the address of trampoline
used in jumping to jump pad, and TRAMPOLINE_SIZE is the size of the
trampoline. Return 0 if successful. */
static int
x86_64_install_fast_tracepoint_jump_pad (gdb_addr_t tpoint, gdb_addr_t tpaddr,
gdb_addr_t collector,
unsigned char *orig_bytes,
size_t orig_size,
gdb_addr_t *jump_entry,
unsigned char *jjumppad_insn,
size_t *jjumppad_insn_size,
gdb_addr_t *trampoline,
size_t *trampoline_size,
char *err)
{
unsigned char buf[100], adjust[100];
int i, offset;
int64_t offset64;
gdb_addr_t buildaddr = *jump_entry;
/* Build the jump pad. */
/* First, do tracepoint data collection. Save registers. */
i = 0;
/* Need to ensure stack pointer saved first. */
buf[i++] = 0x54; /* push %rsp */
buf[i++] = 0x55; /* push %rbp */
buf[i++] = 0x57; /* push %rdi */
buf[i++] = 0x56; /* push %rsi */
buf[i++] = 0x52; /* push %rdx */
buf[i++] = 0x51; /* push %rcx */
buf[i++] = 0x53; /* push %rbx */
buf[i++] = 0x50; /* push %rax */
buf[i++] = 0x41; buf[i++] = 0x57; /* push %r15 */
buf[i++] = 0x41; buf[i++] = 0x56; /* push %r14 */
buf[i++] = 0x41; buf[i++] = 0x55; /* push %r13 */
buf[i++] = 0x41; buf[i++] = 0x54; /* push %r12 */
buf[i++] = 0x41; buf[i++] = 0x53; /* push %r11 */
buf[i++] = 0x41; buf[i++] = 0x52; /* push %r10 */
buf[i++] = 0x41; buf[i++] = 0x51; /* push %r9 */
buf[i++] = 0x41; buf[i++] = 0x50; /* push %r8 */
buf[i++] = 0x9c; /* pushfq */
buf[i++] = 0x48; /* movl <addr>,%rdi */
buf[i++] = 0xbf;
*((unsigned long *)(buf + i)) = (unsigned long) tpaddr;
i += sizeof (unsigned long);
buf[i++] = 0x57; /* push %rdi */
append_insns (&buildaddr, i, buf);
/* Set up the gdb_collect call. */
/* At this point, the stack pointer is the base of our saved
register block. */
i = 0;
buf[i++] = 0x48; /* mov %rsp,%rsi */
buf[i++] = 0x89;
buf[i++] = 0xe6;
/* tpoint address may be 64-bit wide. */
buf[i++] = 0x48; /* movl <addr>,%rdi */
buf[i++] = 0xbf;
memcpy ((void *) (buf + i), (void *) &tpoint, 8);
i += 8;
append_insns (&buildaddr, i, buf);
i = 0; /* mov $collector, %rax */
buf[i++] = 0x48;
buf[i++] = 0xb8;
memcpy (buf + i, &collector, 8);
i += 8;
append_insns (&buildaddr, i, buf);
i = 0; /* callq *%rax */
buf[i++] = 0xff;
buf[i++] = 0xd0;
append_insns (&buildaddr, i, buf);
i = 0;
buf[i++] = 0x48; /* add $0x8,%rsp */
buf[i++] = 0x83;
buf[i++] = 0xc4;
buf[i++] = 0x08;
buf[i++] = 0x9d; /* popfq */
buf[i++] = 0x41; buf[i++] = 0x58; /* pop %r8 */
buf[i++] = 0x41; buf[i++] = 0x59; /* pop %r9 */
buf[i++] = 0x41; buf[i++] = 0x5a; /* pop %r10 */
buf[i++] = 0x41; buf[i++] = 0x5b; /* pop %r11 */
buf[i++] = 0x41; buf[i++] = 0x5c; /* pop %r12 */
buf[i++] = 0x41; buf[i++] = 0x5d; /* pop %r13 */
buf[i++] = 0x41; buf[i++] = 0x5e; /* pop %r14 */
buf[i++] = 0x41; buf[i++] = 0x5f; /* pop %r15 */
buf[i++] = 0x58; /* pop %rax */
buf[i++] = 0x5b; /* pop %rbx */
buf[i++] = 0x59; /* pop %rcx */
buf[i++] = 0x5a; /* pop %rdx */
buf[i++] = 0x5e; /* pop %rsi */
buf[i++] = 0x5f; /* pop %rdi */
buf[i++] = 0x5d; /* pop %rbp */
buf[i++] = 0x5c; /* pop %rsp */
append_insns (&buildaddr, i, buf);
/* Now, adjust the original instruction to execute in the jump
pad. */
memcpy (adjust, orig_bytes, orig_size);
adjust_jump_pad_insns (&buildaddr, tpaddr, orig_size, adjust);
/* Finally, write the jump back to the program. */
offset64 = (tpaddr + orig_size) - (buildaddr + JUMP_SIZE);
if (offset64 < - (((int64_t) 1) << 32)
|| (((int64_t) 1) << 32) <= offset64)
{
gdb_verbose ("Error: cannot handle jump back's wanted offset of 0x%"
PRIx64 ", >32 bits", offset64);
sprintf (err, "E.Jump back from jump pad too far from tracepoint "
"(offset 0x%" PRIx64 " > int32).%c", offset64, 0);
return -1;
}
offset = (int) offset64;
memcpy ((void *) buf, jump_insn, JUMP_SIZE);
memcpy ((void *) (buf + 1), (void *) &offset, 4);
append_insns (&buildaddr, JUMP_SIZE, buf);
/* The jump pad is now built. Wire in a jump to our jump pad. This
is always done last (by our caller actually), so that we can
install fast tracepoints with threads running. This relies on
the agent's atomic write support. */
offset64 = *jump_entry - (tpaddr + JUMP_SIZE);
if (offset64 < - (((int64_t) 1) << 32)
|| (((int64_t) 1) << 32) <= offset64)
{
gdb_verbose ("Error: Cannot handle jump's wanted offset of 0x%"
PRIx64 ", >32 bits", offset64);
sprintf (err,
"E.Jump pad too far from tracepoint "
"(offset 0x%" PRIx64 " > int32).", offset64);
return -1;
}
offset = (int) offset64;
memcpy ((void *) buf, jump_insn, JUMP_SIZE);
memcpy ((void *) (buf + 1), (void *) &offset, 4);
memcpy (jjumppad_insn, buf, JUMP_SIZE);
*jjumppad_insn_size = 5;
/* Return the end address of our pad. */
*jump_entry = buildaddr;
return 0;
}
/* Given a block of registers as saved by the jump pad, return the
* given (GDB-number) register.
*
* \pin raw_regs Pointer to a register block
* \pin regnum GDB register number
* \return Register's value, as a 64-bit number
*/
static uint64_t
x86_64_get_raw_reg (unsigned char *raw_regs, int regnum)
{
int pos = -1;
if (regnum == GDB_REG_RIP)
pos = 0;
else if (regnum == GDB_REG_EFLAGS)
pos = 1;
else if (GDB_REG_R8 <= regnum && regnum <= GDB_REG_R15)
pos = (regnum - GDB_REG_R8) + 2;
else if (GDB_REG_RAX <= regnum && regnum <= GDB_REG_RSP)
pos = (regnum - GDB_REG_RAX) + 10;
else
/* this should maybe be allowed to return an error code */
agent_fatal ("bad register number");
return ((uint64_t *) raw_regs)[pos];
}
/* Move a block of saved registers (typically located on the stack) REGS
into a thread TFINO's register block. */
static void
get_fast_tracepoint_regs (agent_thread_info_t *tinfo, unsigned char *regs)
{
int i;
/* (It might be faster to maintain a bitmap and only work with registers
of actual interest.) */
memset ((void *) (tinfo->regblock), 0, GBUFSIZE);
/* Copy over only the registers actually saved by the jump pad. */
for (i = 0; i < 18; ++i)
((long *) (tinfo->regblock))[i] = (long) x86_64_get_raw_reg (regs, i);
}
#if defined BUILD_UST
static struct ust_register_map x86_64_st_collect_regmap[] =
{
ST_COLLECT_REG(rax),
ST_COLLECT_REG(rbx),
ST_COLLECT_REG(rcx),
ST_COLLECT_REG(rdx),
ST_COLLECT_REG(rsi),
ST_COLLECT_REG(rdi),
ST_COLLECT_REG(rbp),
ST_COLLECT_REG(rsp),
ST_COLLECT_REG(r8),
ST_COLLECT_REG(r9),
ST_COLLECT_REG(r10),
ST_COLLECT_REG(r11),
ST_COLLECT_REG(r12),
ST_COLLECT_REG(r13),
ST_COLLECT_REG(r14),
ST_COLLECT_REG(r15),
{ -1, 0 },
ST_COLLECT_REG(rflags),
ST_COLLECT_REG(cs),
ST_COLLECT_REG(ss),
};
#endif
/* A function used to trick optimizers. */
int
always_true ()
{
return 1;
}
/* Our general strategy for emitting code is to avoid specifying raw
bytes whenever possible, and instead copy a block of inline asm
that is embedded in the function. This is a little messy, because
we need to keep the compiler from discarding what looks like dead
code, plus suppress various warnings. */
#define EMIT_ASM(NAME,INSNS) \
{ extern unsigned char start_ ## NAME, end_ ## NAME; \
add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \
if (always_true ()) \
goto skipover ## NAME; \
__asm__ ("start_" #NAME ":\n\t" INSNS "\n\tend_" #NAME ":\n\t"); \
skipover ## NAME: \
; }
static void
x86_64_emit_prologue (void)
{
EMIT_ASM (prologue,
"pushq %rbp\n\t"
"movq %rsp,%rbp\n\t"
"sub $0x20,%rsp\n\t"
"movq %rdi,-8(%rbp)\n\t"
"movq %rsi,-16(%rbp)");
}
static void
x86_64_emit_epilogue (void)
{
EMIT_ASM (epilogue,
"movq -16(%rbp),%rdi\n\t"
"movq %rax,(%rdi)\n\t"
"xor %rax,%rax\n\t"
"leave\n\t"
"ret");
}
static void
x86_64_emit_add (void)
{
EMIT_ASM (add,
"add (%rsp),%rax\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_sub (void)
{
EMIT_ASM (sub,
"sub %rax,(%rsp)\n\t"
"pop %rax");
}
static void
x86_64_emit_mul (void)
{
emit_error = 1;
}
static void
x86_64_emit_lsh (void)
{
emit_error = 1;
}
static void
x86_64_emit_rsh_signed (void)
{
emit_error = 1;
}
static void
x86_64_emit_rsh_unsigned (void)
{
emit_error = 1;
}
static void
x86_64_emit_ext (int arg)
{
switch (arg)
{
case 8:
EMIT_ASM (ext_8,
"cbtw\n\t"
"cwtl\n\t"
"cltq");
break;
case 16:
EMIT_ASM (ext_16,
"cwtl\n\t"
"cltq");
break;
case 32:
EMIT_ASM (ext_32,
"cltq");
break;
default:
emit_error = 1;
}
}
static void
x86_64_emit_log_not (void)
{
EMIT_ASM (log_not,
"test %rax,%rax\n\t"
"sete %cl\n\t"
"movzbq %cl,%rax");
}
static void
x86_64_emit_bit_and (void)
{
EMIT_ASM (and,
"and (%rsp),%rax\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_bit_or (void)
{
EMIT_ASM (or,
"or (%rsp),%rax\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_bit_xor (void)
{
EMIT_ASM (xor,
"xor (%rsp),%rax\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_bit_not (void)
{
EMIT_ASM (bit_not,
"xorq $0xffffffffffffffff,%rax");
}
static void
x86_64_emit_equal (void)
{
EMIT_ASM (equal,
"cmp %rax,(%rsp)\n\t"
"je .Lequal_true\n\t"
"xor %rax,%rax\n\t"
"jmp .Lequal_end\n\t"
".Lequal_true:\n\t"
"mov $0x1,%rax\n\t"
".Lequal_end:\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_less_signed (void)
{
EMIT_ASM (less_signed,
"cmp %rax,(%rsp)\n\t"
"jl .Lless_signed_true\n\t"
"xor %rax,%rax\n\t"
"jmp .Lless_signed_end\n\t"
".Lless_signed_true:\n\t"
"mov $1,%rax\n\t"
".Lless_signed_end:\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_less_unsigned (void)
{
EMIT_ASM (less_unsigned,
"cmp %rax,(%rsp)\n\t"
"jb .Lless_unsigned_true\n\t"
"xor %rax,%rax\n\t"
"jmp .Lless_unsigned_end\n\t"
".Lless_unsigned_true:\n\t"
"mov $1,%rax\n\t"
".Lless_unsigned_end:\n\t"
"lea 0x8(%rsp),%rsp");
}
static void
x86_64_emit_ref (int size, int (*agent_mem_read_to) (unsigned char *,
gdb_addr_t from, gdb_size_t))
{
#ifndef SPEED_CHECK
unsigned char buf[16];
int i;
gdb_addr_t buildaddr;
/* In the normal case, we need to set up a call to memory read API. */
EMIT_ASM (ref_a,
"sub $0x8,%rsp\n\t"
/* (need to do caller saves?) */
"mov %rsp,%rdi\n\t"
"mov %rax,%rsi");
buildaddr = current_insn_ptr;
i = 0;
buf[i++] = 0x48; /* mov $<n>,%rdx */
buf[i++] = 0xc7;
buf[i++] = 0xc2;
*((int *) (&buf[i])) = size;
i += 4;
append_insns (&buildaddr, i, buf);
current_insn_ptr = buildaddr;
x86_64_emit_call (agent_mem_read_to);
EMIT_ASM (ref_b,
/* Check the return result for error reports. */
"test %rax,%rax\n\t"
"jz .Lref_ok\n\t"
/* An error; clean up and return. */
/* Return a zero in &value. */
"movq -16(%rbp),%rdi\n\t"
"movq $0,(%rdi)\n\t"
/* This should be the value of expr_eval_mem_read_error. */
"movq $10,%rax\n\t"
"leave\n\t"
"ret\n\t"
".Lref_ok:\n\t"
/* Make the address of the result be on the bytecode stack top. */
"mov %rsp,%rax");
#endif /* SPEED_CHECK */
switch (size)
{
case 1:
EMIT_ASM (ref1,
"movb (%rax),%al");
break;
case 2:
EMIT_ASM (ref2,
"movw (%rax),%ax");
break;
case 4:
EMIT_ASM (ref4,
"movl (%rax),%eax");
break;
case 8:
EMIT_ASM (ref8,
"movq (%rax),%rax");
break;
}
#ifndef SPEED_CHECK
EMIT_ASM (ref_c,
"lea 0x8(%rsp),%rsp");
#endif /* SPEED_CHECK */
}
static void
x86_64_emit_if_goto (int *offset_p, int *size_p)
{
EMIT_ASM (if_goto,
"mov %rax,%rcx\n\t"
"pop %rax\n\t"
"cmp $0,%rcx\n\t"
".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0");
if (offset_p)
*offset_p = 10;
if (size_p)
*size_p = 4;
}
static void
x86_64_emit_goto (int *offset_p, int *size_p)
{
EMIT_ASM (goto,
/* jmp, but don't trust the assembler to choose the right jump */
".byte 0xe9, 0x0, 0x0, 0x0, 0x0");
if (offset_p)
*offset_p = 1;
if (size_p)
*size_p = 4;
}
static void
x86_64_write_goto_address (unsigned char *from, unsigned char *to, int size)
{
int diff = (to - (from + size));
if (size != 4)
{
emit_error = 1;
return;
}
*((int *) from) = diff;
}
static void
x86_64_emit_const (int64_t num)
{
unsigned char buf[16];
int i;
gdb_addr_t buildaddr = current_insn_ptr;
if (num)
{
i = 0;
buf[i++] = 0x48; buf[i++] = 0xb8; /* mov $<n>,%rax */
*((int64_t *) (&buf[i])) = num;
i += 8;
append_insns (&buildaddr, i, buf);
current_insn_ptr = buildaddr;
}
else
{
EMIT_ASM (const,
"xor %rax,%rax");
}
}
static void
x86_64_emit_call (void *fn)
{
unsigned char buf[16];
int i, offset;
gdb_addr_t buildaddr;
buildaddr = current_insn_ptr;
i = 0;
buf[i++] = 0xe8; /* call <reladdr> */
offset = ((unsigned long) fn) - (buildaddr + i + 4);
memcpy ((void *) (buf + i), (void *) &offset, 4);
i += 4;
append_insns (&buildaddr, i, buf);
current_insn_ptr = buildaddr;
}
static void
x86_64_emit_reg (int reg)
{
unsigned char buf[16];
int i;
gdb_addr_t buildaddr;
/* Don't assume raw_regs is still in %rdi. */
EMIT_ASM (reg,
"movq -8(%rbp),%rdi");
buildaddr = current_insn_ptr;
i = 0;
buf[i++] = 0xbe; /* mov $<n>,%esi */
*((int *) (&buf[i])) = reg;
i += 4;
append_insns (&buildaddr, i, buf);
current_insn_ptr = buildaddr;
x86_64_emit_call (x86_64_get_raw_reg);
}
static void
x86_64_emit_pop (void)
{
EMIT_ASM (pop,
"pop %rax");
}
static void
x86_64_emit_stack_flush (void)
{
EMIT_ASM (stack_flush,
"push %rax");
}
static void
x86_64_emit_zero_ext (int arg)
{
switch (arg)
{
case 8:
EMIT_ASM (zero_ext_8,
"and $0xff,%rax");
break;
case 16:
EMIT_ASM (zero_ext_16,
"and $0xffff,%rax");
break;
case 32:
EMIT_ASM (zero_ext_32,
"mov $0xffffffff,%rcx\n\t"
"and %rcx,%rax");
break;
default:
emit_error = 1;
}
}
static void
x86_64_emit_swap (void)
{
EMIT_ASM (swap,
"mov %rax,%rcx\n\t"
"pop %rax\n\t"
"push %rcx");
}
static void
x86_64_emit_stack_adjust (int n)
{
unsigned char buf[16];