forked from dtrace4linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstr_linux.c
More file actions
1165 lines (1031 loc) · 31.2 KB
/
instr_linux.c
File metadata and controls
1165 lines (1031 loc) · 31.2 KB
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
/**********************************************************************/
/* This is the Instruction Provider code -- based on FBT and */
/* fbt_linux.c, but it allows us to place probes based on */
/* instruction type, rather than function entry/exit. */
/* */
/* Can be used for interesting things like branches, REP/LOOP and */
/* other instructions, but could be a performance hit since it */
/* could allow for lots of probes (much more than FBT) and could */
/* stress a system. */
/* */
/* Originally conceived to help debug FBT by allowing us to find */
/* more instructions of interest which need to be debugged during */
/* the single step phase. */
/* */
/* CDDL License applies (for now). */
/* */
/* Date: June 2008 */
/* Author: Paul D. Fox */
/* */
/* $Header: Last edited: 02-Jan-2012 1.3 $ */
/**********************************************************************/
#include <dtrace_linux.h>
#include "proc_compat.h"
#include <sys/dtrace_impl.h>
#include <sys/dtrace.h>
#include <dtrace_proto.h>
#include <linux/cpumask.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/kallsyms.h>
#include <linux/seq_file.h>
#include <sys/procfs_isa.h>
# undef NULL
# define NULL 0
MODULE_AUTHOR("Paul D. Fox");
MODULE_LICENSE(DRIVER_LICENSE);
MODULE_DESCRIPTION("DTRACE/Instruction Tracing Driver");
/**********************************************************************/
/* Under Solaris, they use the LOCK prefix opcode for instruction */
/* traps, but this wont work for Linux, since we have lots more */
/* candidate patchable instructions, and LOCK needs the next byte */
/* to decide if we have an invalid instruction or not. Dont know */
/* why they do this, but lets just use INT3 everywhere. (Consider: */
/* LOCK for a RET; in this case the next byte may be garbage as */
/* GCC aligns to a quad boundary). */
/**********************************************************************/
#if defined(linux)
# if defined(__arm__)
# define INSTR_PATCHVAL 0xe1200070
# else
# define INSTR_PATCHVAL 0xcc
# endif
#else
# if defined(__amd64)
# define INSTR_PATCHVAL 0xcc
# else
# define INSTR_PATCHVAL 0xf0
# endif
#endif
#define INSTR_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & instr_probetab_mask)
#define INSTR_PROBETAB_SIZE 0x8000 /* 32k entries -- 128K total */
typedef struct instr_probe {
struct instr_probe *insp_hashnext;
uint8_t *insp_patchpoint;
instr_t insp_patchval;
instr_t insp_savedval;
uint8_t insp_inslen; /* Length of instr we are patching */
char insp_modrm; /* Offset to modrm byte of instruction */
char insp_enabled;
uintptr_t insp_roffset;
dtrace_id_t insp_id;
char *insp_name;
struct modctl *insp_ctl;
int insp_loadcnt;
int insp_symndx;
struct instr_probe *insp_next;
} instr_probe_t;
//static dev_info_t *instr_devi;
static dtrace_provider_id_t instr_id;
static instr_probe_t **instr_probetab;
static int instr_probetab_size;
static int instr_probetab_mask;
static int instr_verbose = 0;
static int num_probes;
extern int dtrace_unhandled;
static void instr_provide_function(struct modctl *mp,
par_module_t *pmp,
char *modname, char *name, uint8_t *st_value,
uint8_t *instr, uint8_t *limit, int);
static const char *(*my_kallsyms_lookup)(unsigned long addr,
unsigned long *symbolsize,
unsigned long *offset,
char **modname, char *namebuf);
/**********************************************************************/
/* For debugging - make sure we dont add a patch to the same addr. */
/**********************************************************************/
static int
instr_is_patched(char *name, uint8_t *addr)
{
instr_probe_t *fbt = instr_probetab[INSTR_ADDR2NDX(addr)];
for (; fbt != NULL; fbt = fbt->insp_hashnext) {
if (fbt->insp_patchpoint == addr) {
dtrace_printf("instr:dup patch: %p %s\n", addr, name);
return 1;
}
}
return 0;
}
/**********************************************************************/
/* Here from INT3 interrupt context to see if the address we hit */
/* is one of ours. */
/**********************************************************************/
static int
instr_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval, trap_instr_t *tinfo)
{
uintptr_t stack0, stack1, stack2, stack3, stack4;
instr_probe_t *fbt = instr_probetab[INSTR_ADDR2NDX(addr)];
//HERE();
for (; fbt != NULL; fbt = fbt->insp_hashnext) {
if (fbt->insp_enabled && (uintptr_t)fbt->insp_patchpoint == addr) {
tinfo->t_opcode = fbt->insp_savedval;
tinfo->t_inslen = fbt->insp_inslen;
tinfo->t_modrm = fbt->insp_modrm;
if (!tinfo->t_doprobe)
return DTRACE_INVOP_ANY;
if (fbt->insp_roffset == 0) {
/*
* When accessing the arguments on the stack,
* we must protect against accessing beyond
* the stack. We can safely set NOFAULT here
* -- we know that interrupts are already
* disabled.
*/
DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
CPU->cpu_dtrace_caller = stack[0];
stack0 = stack[1];
stack1 = stack[2];
stack2 = stack[3];
stack3 = stack[4];
stack4 = stack[5];
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
CPU_DTRACE_BADADDR);
dtrace_probe(fbt->insp_id, stack0, stack1,
stack2, stack3, stack4);
CPU->cpu_dtrace_caller = NULL;
} else {
#ifdef __amd64
/*
* On amd64, we instrument the ret, not the
* leave. We therefore need to set the caller
* to assure that the top frame of a stack()
* action is correct.
*/
DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
CPU->cpu_dtrace_caller = stack[0];
DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
CPU_DTRACE_BADADDR);
#endif
dtrace_probe(fbt->insp_id, fbt->insp_roffset,
rval, 0, 0, 0);
CPU->cpu_dtrace_caller = NULL;
}
return DTRACE_INVOP_ANY;
}
}
//HERE();
return (0);
}
static int
get_refcount(struct module *mp)
{ int sum = 0;
if (mp == NULL)
return 0;
#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
/***********************************************/
/* Linux 2.6.29 does something here */
/* presumably to avoid bad cache line */
/* behavior. We dont really care about this */
/* for now. */
/***********************************************/
# elif defined(CONFIG_SMP)
{int i;
for (i = 0; i < NR_CPUS; i++)
sum += local_read(&mp->ref[i].count);
}
# endif
return sum;
}
/**********************************************************************/
/* This code is called from dtrace.c (dtrace_probe_provide) when */
/* we are arming the fbt functions. Since the kernel doesnt exist */
/* as a module, we need to handle that as a special case. */
/**********************************************************************/
void
instr_provide_kernel(void)
{
static struct module kern;
int n;
static caddr_t ktext;
static caddr_t ketext;
static int first_time = TRUE;
caddr_t a, aend;
char name[KSYM_NAME_LEN];
/***********************************************/
/* In case we disabled the instr provider. */
/***********************************************/
if (instr_probetab == NULL)
return;
if (first_time) {
first_time = FALSE;
ktext = get_proc_addr("_text");
if (ktext == NULL)
ktext = get_proc_addr("_stext");
ketext = get_proc_addr("_etext");
my_kallsyms_lookup = get_proc_addr("kallsyms_lookup");
}
if (ktext == NULL) {
printk("dtracedrv:instr_provide_kernel: Cannot find _text/_stext\n");
return;
}
if (kern.name[0])
return;
strcpy(kern.name, "kernel");
/***********************************************/
/* Walk the code segment, finding the */
/* symbols, and creating a probe for each */
/* one. */
/***********************************************/
for (n = 0, a = ktext; my_kallsyms_lookup && a < ketext; ) {
const char *cp;
unsigned long size;
unsigned long offset;
char *modname = NULL;
//printk("lookup %p kallsyms_lookup=%p\n", a, kallsyms_lookup);
cp = my_kallsyms_lookup((unsigned long) a, &size, &offset, &modname, name);
if (cp && size == 0)
printk("instr_linux: size=0 %p %s\n", a, cp ? cp : "??");
/* printk("a:%p cp:%s size:%lx offset:%lx\n", a, cp ? cp : "--undef--", size, offset);*/
if (cp == NULL)
aend = a + 4;
else
aend = a + (size - offset);
/***********************************************/
/* If this function is toxic, we mustnt */
/* touch it. */
/***********************************************/
if (cp && *cp && !is_toxic_func((unsigned long) a, cp)) {
instr_provide_function(&kern,
(par_module_t *) &kern, //uck on the cast..we dont really need it
"kernel", name,
a, a, aend, n);
}
a = aend;
n++;
}
}
/*ARGSUSED*/
static void
instr_provide_module(void *arg, struct modctl *ctl)
{ int i;
struct module *mp = (struct module *) ctl;
char *modname = mp->name;
char *str = mp->strtab;
char *name;
par_module_t *pmp;
int init;
/***********************************************/
/* Possible memleak here...we allocate a */
/* parallel struct, but need to free if we */
/* are offloaded. */
/***********************************************/
pmp = par_alloc(PARD_INSTR, mp, sizeof *pmp, &init);
if (pmp == NULL || pmp->fbt_nentries) {
/*
* This module has some FBT entries allocated; we're afraid
* to screw with it.
*/
return;
}
if (dtrace_here)
printk("%s(%d):modname=%s num_symtab=%u\n", __FILE__, __LINE__, modname, (unsigned) mp->num_symtab);
if (strcmp(modname, "dtracedrv") == 0)
return;
for (i = 1; i < mp->num_symtab; i++) {
uint8_t *instr, *limit;
Elf_Sym *sym = (Elf_Sym *) &mp->symtab[i];
int dtrace_here = 0;
if (strcmp(modname, "dummy") == 0) dtrace_here = 1;
name = str + sym->st_name;
if (sym->st_name == NULL || *name == '\0')
continue;
/***********************************************/
/* Linux re-encodes the symbol types. */
/***********************************************/
if (sym->st_info != 't' && sym->st_info != 'T')
continue;
// if (strstr(name, "init"))
// continue;
//if (sym->st_info != 'T') {printk("skip -- %02d %c:%s\n", i, sym->st_info, name); continue;}
#if 0
if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
continue;
/*
* Weak symbols are not candidates. This could be made to
* work (where weak functions and their underlying function
* appear as two disjoint probes), but it's not simple.
*/
if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
continue;
#endif
if (strstr(name, "dtrace_") == name &&
strstr(name, "dtrace_safe_") != name) {
/*
* Anything beginning with "dtrace_" may be called
* from probe context unless it explitly indicates
* that it won't be called from probe context by
* using the prefix "dtrace_safe_".
*/
continue;
}
if (strstr(name, "dtracedrv_") == name)
continue;
if (strstr(name, "kdi_") == name ||
strstr(name, "kprobe") == name) {
/*
* Anything beginning with "kdi_" is a part of the
* kernel debugger interface and may be called in
* arbitrary context -- including probe context.
*/
continue;
}
if (strcmp(name, "_init") == 0)
continue;
if (strcmp(name, "_fini") == 0)
continue;
instr = (uint8_t *)sym->st_value;
limit = (uint8_t *)(sym->st_value + sym->st_size);
//printk("trying -- %02d %c:%s\n", i, sym->st_info, name);
//HERE();
/***********************************************/
/* Ignore the init function of modules - we */
/* will never execute them now the module */
/* is loaded, and we dont want to be poking */
/* potential pages which dont exist in */
/* memory or which are being used for data. */
/***********************************************/
if (instr == (uint8_t *) mp->init)
continue;
/***********************************************/
/* We do have syms that appear to point to */
/* unmapped pages. Maybe these are freed */
/* pages after a driver loads. Double check */
/* - if /proc/kallsyms says its not there, */
/* then ignore it. */
/***********************************************/
if (!validate_ptr(instr))
continue;
/***********************************************/
/* Look at the section this symbol is in - */
/* we dont want sections which can */
/* disappear or have disappeared (eg */
/* .init). */
/* */
/* I'm not sure I follow this code for all */
/* kernel releases - if we have the field, */
/* it should have a fixed meaning, but some */
/* modules have bogus section attributes */
/* pointers (maybe pointers to freed */
/* segments?). Lets be careful out there. */
/* */
/* 20090425 Ok - heres the deal. In 2.6.9 */
/* (at least) the section table is */
/* allocated but only for sections which */
/* are SHF_ALLOC. This means the array of */
/* sections cannot be indexed by */
/* sym->st_shndx since the mappings are now */
/* bogus (kernel doesnt adjust the symbol */
/* section indexes). What we need to do is */
/* attempt to find the section by address. */
/***********************************************/
if (!instr_in_text_seg(mp, name, sym))
continue;
/***********************************************/
/* We are good to go... */
/***********************************************/
instr_provide_function(mp, pmp,
modname, name,
(uint8_t *) sym->st_value,
instr, limit, i);
}
}
/**********************************************************************/
/* Common code to handle module and kernel functions. */
/**********************************************************************/
struct masks {
unsigned char offset;
unsigned char and;
unsigned char mask;
};
struct masks lidt_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x01},
{2, 0xf8, 0x18},
{0, 0, 0},
};
struct masks sidt_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x01},
{2, 0xf8, 0x08},
{0, 0, 0},
};
struct masks lgdt_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x01},
{2, 0xf8, 0x10},
{0, 0, 0},
};
struct masks sgdt_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x01},
{2, 0xf8, 0x00},
{0, 0, 0},
};
struct masks wr_cr3_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x22},
{2, 0xf8, 0xd8},
{0, 0, 0},
};
struct masks ltr_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x00},
{2, 0xf8, 0x58},
{0, 0, 0},
};
struct masks ltr2_masks[] = {
{0, 0xff, 0x0f},
{1, 0xff, 0x00},
{2, 0xf8, 0x18},
{0, 0, 0},
};
/* 35ad4: f0 ff 43 20 lock incl 0x20(%rbx) */
/* 2c21e: f0 41 ff 87 5c 01 00 lock incl 0x15c(%r15) */
struct masks inc_masks[] = {
// {0, 0xff, 0xf0}, /* lock */
{0, 0xff, 0xff},
{1, 0x30, 0x00},
{0, 0, 0},
};
static int
instr_cmp(unsigned char *pc, struct masks *mp)
{
for (; mp->and; mp++) {
if ((pc[mp->offset] & mp->and) != mp->mask)
return FALSE;
}
return TRUE;
}
static void
instr_provide_function(struct modctl *mp, par_module_t *pmp,
char *modname, char *name, uint8_t *st_value,
uint8_t *instr, uint8_t *limit, int symndx)
{
int do_print = FALSE;
instr_probe_t *fbt;
int size;
int modrm;
char *orig_name = name;
char name_buf[128];
char pred_buf[128];
/***********************************************/
/* Ignore these - they tend to result in a */
/* lot of dups, and also tend to be in the */
/* same reusable and unmapped page. */
/***********************************************/
if (strncmp(name, "cleanup_module", 14) == 0)
return;
# define UNHANDLED_FBT() if (do_print || dtrace_unhandled) { \
printk("instr:unhandled instr %s:%p %02x %02x %02x %02x\n", \
name, instr, instr[0], instr[1], instr[2], instr[3]); \
}
# define INSTR(val, opcode_name) do {if (instr[0] == val) { \
snprintf(name_buf, sizeof name_buf, "%s-%s", orig_name, opcode_name); \
name = name_buf; \
goto do_instr; \
}} while (0)
# define INSTR2(instr, tbl, opcode_name) do {if (instr_cmp(instr, tbl)) { \
snprintf(name_buf, sizeof name_buf, "%s-%s", orig_name, opcode_name); \
name = name_buf; \
goto do_instr; \
}} while (0)
for (; instr < limit; instr += size) {
/***********************************************/
/* Make sure we dont try and handle data or */
/* bad instructions. */
/***********************************************/
if ((size = dtrace_instr_size_modrm(instr, &modrm)) <= 0)
return;
name_buf[0] = '\0';
INSTR2(instr, lidt_masks, "lidt");
INSTR2(instr, sidt_masks, "sidt");
INSTR2(instr, lgdt_masks, "lgdt");
INSTR2(instr, sgdt_masks, "sgdt");
INSTR2(instr, wr_cr3_masks, "wr_cr3");
INSTR2(instr, ltr_masks, "ltr");
INSTR2(instr, ltr2_masks, "ltr");
/* lock-inc (32b) */
#if 0
INSTR2(instr, inc_masks, "inc");
if (*instr == 0xf0) {
printk("lock %p %02x %02x %02x\n", instr, instr[0], instr[1], instr[2]);
}
#endif
/***********************************************/
/* Add UD2 instructions (BUG_ON). */
/***********************************************/
if (*instr == 0x0f && instr[1] == 0x0b) {
snprintf(name_buf, sizeof name_buf, "%s-ud2", orig_name); \
name = name_buf;
goto do_instr;
}
INSTR(0x70, "jo");
INSTR(0x71, "jno");
INSTR(0x72, "jb");
INSTR(0x73, "jae");
INSTR(0x74, "je");
INSTR(0x75, "jne");
INSTR(0x76, "jbe");
INSTR(0x77, "ja");
INSTR(0x78, "js");
INSTR(0x79, "jns");
INSTR(0x7a, "jp");
INSTR(0x7b, "jnp");
INSTR(0x7c, "jl");
INSTR(0x7d, "jge");
INSTR(0x7e, "jle");
INSTR(0x7f, "jg");
INSTR(0x90, "nop");
INSTR(0xa6, "scas");
INSTR(0xe0, "loopne");
INSTR(0xe1, "loope");
INSTR(0xe2, "loop");
INSTR(0xe8, "callr");
/***********************************************/
/* I was debugging the fwd/back scenario - */
/* dont need this now. */
/***********************************************/
if (0 && *instr == 0xe8) {
if (instr[4] & 0x80)
INSTR(0xe8, "callr-back");
else
INSTR(0xe8, "callr-fwd");
}
INSTR(0xf0, "lock");
INSTR(0xf1, "icebp");
INSTR(0xf2, "repnz");
INSTR(0xf3, "repz");
INSTR(0xfa, "cli");
INSTR(0xfb, "sti");
if (name_buf[0] == 0) {
continue;
}
do_instr:
sprintf(pred_buf, "0x%p", instr);
/***********************************************/
/* Make sure this doesnt overlap another */
/* sym. We are in trouble when this happens */
/* - eg we will mistaken what the emulation */
/* is for, but also, it means something */
/* strange happens, like kernel is reusing */
/* a page (eg for init/exit section of a */
/* module). */
/***********************************************/
if (instr_is_patched(name, instr))
return;
fbt = kmem_zalloc(sizeof (instr_probe_t), KM_SLEEP);
fbt->insp_name = name;
fbt->insp_id = dtrace_probe_create(instr_id, modname,
name, pred_buf, 3, fbt);
num_probes++;
fbt->insp_patchpoint = instr;
fbt->insp_ctl = mp; // ctl;
fbt->insp_loadcnt = get_refcount(mp);
/***********************************************/
/* Save potential overwrite of instruction */
/* and length, because we will need the */
/* entire instruction when we single step */
/* over it. */
/***********************************************/
fbt->insp_savedval = *instr;
fbt->insp_inslen = size;
//if (modrm >= 0 && (instr[modrm] & 0xc7) == 0x05) printk("modrm %s %p rm=%d\n", name, instr, modrm);
fbt->insp_modrm = modrm;
fbt->insp_patchval = INSTR_PATCHVAL;
fbt->insp_hashnext = instr_probetab[INSTR_ADDR2NDX(instr)];
fbt->insp_symndx = symndx;
instr_probetab[INSTR_ADDR2NDX(instr)] = fbt;
if (do_print)
printk("%d:alloc entry-patchpoint: %s %p sz=%d %02x %02x %02x\n",
__LINE__,
name,
fbt->insp_patchpoint,
fbt->insp_inslen,
instr[0], instr[1], instr[2]);
pmp->fbt_nentries++;
}
}
/*ARGSUSED*/
static void
instr_destroy(void *arg, dtrace_id_t id, void *parg)
{
instr_probe_t *fbt = parg, *next, *hash, *last;
struct modctl *ctl = fbt->insp_ctl;
struct module *mp = ctl;
int ndx;
do {
if (mp != NULL && get_refcount(mp) == fbt->insp_loadcnt) {
if ((get_refcount(mp) == fbt->insp_loadcnt &&
mp->state == MODULE_STATE_LIVE)) {
par_module_t *pmp = par_alloc(PARD_INSTR, mp, sizeof *pmp, NULL);
if (pmp && --pmp->fbt_nentries == 0)
par_free(PARD_INSTR, pmp);
}
}
/*
* Now we need to remove this probe from the instr_probetab.
*/
ndx = INSTR_ADDR2NDX(fbt->insp_patchpoint);
last = NULL;
hash = instr_probetab[ndx];
while (hash != fbt) {
ASSERT(hash != NULL);
last = hash;
hash = hash->insp_hashnext;
}
if (last != NULL) {
last->insp_hashnext = fbt->insp_hashnext;
} else {
instr_probetab[ndx] = fbt->insp_hashnext;
}
next = fbt->insp_next;
kmem_free(fbt, sizeof (instr_probe_t));
fbt = next;
} while (fbt != NULL);
}
/*ARGSUSED*/
static int
instr_enable(void *arg, dtrace_id_t id, void *parg)
{
instr_probe_t *fbt = parg;
struct modctl *ctl = fbt->insp_ctl;
struct module *mp = (struct module *) ctl;
# if 0
ctl->mod_nenabled++;
# endif
if (mp->state != MODULE_STATE_LIVE) {
if (instr_verbose) {
cmn_err(CE_NOTE, "fbt is failing for probe %s "
"(module %s unloaded)",
fbt->insp_name, mp->name);
}
return 0;
}
/*
* Now check that our modctl has the expected load count. If it
* doesn't, this module must have been unloaded and reloaded -- and
* we're not going to touch it.
*/
if (get_refcount(mp) != fbt->insp_loadcnt) {
if (instr_verbose) {
cmn_err(CE_NOTE, "fbt is failing for probe %s "
"(module %s reloaded)",
fbt->insp_name, mp->name);
}
return 0;
}
for (; fbt != NULL; fbt = fbt->insp_next) {
fbt->insp_enabled = TRUE;
if (dtrace_here)
printk("instr_enable:patch %p p:%02x\n", fbt->insp_patchpoint, fbt->insp_patchval);
if (memory_set_rw(fbt->insp_patchpoint, 1, TRUE)) {
*fbt->insp_patchpoint = fbt->insp_patchval;
}
}
return 0;
}
/*ARGSUSED*/
static void
instr_disable(void *arg, dtrace_id_t id, void *parg)
{
instr_probe_t *fbt = parg;
struct modctl *ctl = fbt->insp_ctl;
struct module *mp = (struct module *) ctl;
# if 0
ASSERT(ctl->mod_nenabled > 0);
ctl->mod_nenabled--;
if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->insp_loadcnt))
return;
# else
if (mp->state != MODULE_STATE_LIVE ||
get_refcount(mp) != fbt->insp_loadcnt)
return;
# endif
for (; fbt != NULL; fbt = fbt->insp_next) {
fbt->insp_enabled = FALSE;
if (dtrace_here) {
printk("%s:%d: Disable %p:%s:%s\n",
__func__, __LINE__,
fbt->insp_patchpoint,
fbt->insp_ctl->name,
fbt->insp_name);
}
/***********************************************/
/* Memory should be writable, but if we */
/* failed in the instr_enable code, e.g. */
/* because kernel freed an .init section, */
/* then dont try and unpatch something we */
/* didnt patch. */
/***********************************************/
if (*fbt->insp_patchpoint == fbt->insp_patchval) {
if (memory_set_rw(fbt->insp_patchpoint, 1, TRUE))
*fbt->insp_patchpoint = fbt->insp_savedval;
}
}
}
/*ARGSUSED*/
static void
instr_suspend(void *arg, dtrace_id_t id, void *parg)
{
instr_probe_t *fbt = parg;
struct modctl *ctl = fbt->insp_ctl;
struct module *mp = (struct module *) ctl;
# if 0
ASSERT(ctl->mod_nenabled > 0);
if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->insp_loadcnt))
return;
# else
if (mp->state != MODULE_STATE_LIVE ||
get_refcount(mp) != fbt->insp_loadcnt)
return;
# endif
for (; fbt != NULL; fbt = fbt->insp_next)
*fbt->insp_patchpoint = fbt->insp_savedval;
}
/*ARGSUSED*/
static void
instr_resume(void *arg, dtrace_id_t id, void *parg)
{
instr_probe_t *fbt = parg;
struct modctl *ctl = fbt->insp_ctl;
struct module *mp = (struct module *) ctl;
# if 0
ASSERT(ctl->mod_nenabled > 0);
if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->insp_loadcnt))
return;
# else
if (mp->state != MODULE_STATE_LIVE ||
get_refcount(mp) != fbt->insp_loadcnt)
return;
# endif
for (; fbt != NULL; fbt = fbt->insp_next)
*fbt->insp_patchpoint = fbt->insp_patchval;
}
/*ARGSUSED*/
static void
instr_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
{
instr_probe_t *fbt = parg;
struct modctl *ctl = fbt->insp_ctl;
struct module *mp = (struct module *) ctl;
ctf_file_t *fp = NULL;
ctf_funcinfo_t f;
// int error;
ctf_id_t argv[32], type;
int argc = sizeof (argv) / sizeof (ctf_id_t);
// const char *parent;
if (mp->state != MODULE_STATE_LIVE ||
get_refcount(mp) != fbt->insp_loadcnt)
return;
if (fbt->insp_roffset != 0 && desc->dtargd_ndx == 0) {
(void) strcpy(desc->dtargd_native, "int");
return;
}
# if 0
if ((fp = ctf_modopen(mp, &error)) == NULL) {
/*
* We have no CTF information for this module -- and therefore
* no args[] information.
*/
goto err;
}
# endif
//TODO();
if (fp == NULL)
goto err;
# if 0
/*
* If we have a parent container, we must manually import it.
*/
if ((parent = ctf_parent_name(fp)) != NULL) {
ctf_file_t *pfp;
TODO();
struct modctl *mod;
/*
* We must iterate over all modules to find the module that
* is our parent.
*/
for (mod = &modules; mod != NULL; mod = mod->mod_next) {
if (strcmp(mod->mod_filename, parent) == 0)
break;
}
if (mod == NULL)
goto err;
if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL)
goto err;
if (ctf_import(fp, pfp) != 0) {
ctf_close(pfp);
goto err;
}
ctf_close(pfp);
}
# endif
if (ctf_func_info(fp, fbt->insp_symndx, &f) == CTF_ERR)
goto err;
if (fbt->insp_roffset != 0) {
if (desc->dtargd_ndx > 1)
goto err;
ASSERT(desc->dtargd_ndx == 1);
type = f.ctc_return;
} else {
if (desc->dtargd_ndx + 1 > f.ctc_argc)
goto err;
if (ctf_func_args(fp, fbt->insp_symndx, argc, argv) == CTF_ERR)
goto err;
type = argv[desc->dtargd_ndx];
}
if (ctf_type_name(fp, type, desc->dtargd_native,
DTRACE_ARGTYPELEN) != NULL) {
ctf_close(fp);
return;
}
err:
if (fp != NULL)
ctf_close(fp);
desc->dtargd_ndx = DTRACE_ARGNONE;
}
static dtrace_pattr_t instr_attr = {
{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
};
static dtrace_pops_t instr_pops = {
NULL,
instr_provide_module,
instr_enable,
instr_disable,
instr_suspend,
instr_resume,
instr_getargdesc,
NULL,
NULL,
instr_destroy
};
static void
instr_cleanup(dev_info_t *devi)
{
dtrace_invop_remove(instr_invop);
if (instr_id)
dtrace_unregister(instr_id);
// ddi_remove_minor_node(devi, NULL);
if (instr_probetab)
kmem_free(instr_probetab, instr_probetab_size * sizeof (instr_probe_t *));
instr_probetab = NULL;
instr_probetab_mask = 0;
}
/**********************************************************************/
/* Module interface to the kernel. */
/**********************************************************************/
# if 0
static int instr_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)