-
Notifications
You must be signed in to change notification settings - Fork 0
/
kprobes-test.c
1769 lines (1486 loc) · 42.5 KB
/
kprobes-test.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
/*
* arch/arm/kernel/kprobes-test.c
*
* Copyright (C) 2011 Jon Medhurst <[email protected]>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
/*
* This file contains test code for ARM kprobes.
*
* The top level function run_all_tests() executes tests for all of the
* supported instruction sets: ARM, 16-bit Thumb, and 32-bit Thumb. These tests
* fall into two categories; run_api_tests() checks basic functionality of the
* kprobes API, and run_test_cases() is a comprehensive test for kprobes
* instruction decoding and simulation.
*
* run_test_cases() first checks the kprobes decoding table for self consistency
* (using table_test()) then executes a series of test cases for each of the CPU
* instruction forms. coverage_start() and coverage_end() are used to verify
* that these test cases cover all of the possible combinations of instructions
* described by the kprobes decoding tables.
*
* The individual test cases are in kprobes-test-arm.c and kprobes-test-thumb.c
* which use the macros defined in kprobes-test.h. The rest of this
* documentation will describe the operation of the framework used by these
* test cases.
*/
/*
* TESTING METHODOLOGY
* -------------------
*
* The methodology used to test an ARM instruction 'test_insn' is to use
* inline assembler like:
*
* test_before: nop
* test_case: test_insn
* test_after: nop
*
* When the test case is run a kprobe is placed of each nop. The
* post-handler of the test_before probe is used to modify the saved CPU
* register context to that which we require for the test case. The
* pre-handler of the of the test_after probe saves a copy of the CPU
* register context. In this way we can execute test_insn with a specific
* register context and see the results afterwards.
*
* To actually test the kprobes instruction emulation we perform the above
* step a second time but with an additional kprobe on the test_case
* instruction itself. If the emulation is accurate then the results seen
* by the test_after probe will be identical to the first run which didn't
* have a probe on test_case.
*
* Each test case is run several times with a variety of variations in the
* flags value of stored in CPSR, and for Thumb code, different ITState.
*
* For instructions which can modify PC, a second test_after probe is used
* like this:
*
* test_before: nop
* test_case: test_insn
* test_after: nop
* b test_done
* test_after2: nop
* test_done:
*
* The test case is constructed such that test_insn branches to
* test_after2, or, if testing a conditional instruction, it may just
* continue to test_after. The probes inserted at both locations let us
* determine which happened. A similar approach is used for testing
* backwards branches...
*
* b test_before
* b test_done @ helps to cope with off by 1 branches
* test_after2: nop
* b test_done
* test_before: nop
* test_case: test_insn
* test_after: nop
* test_done:
*
* The macros used to generate the assembler instructions describe above
* are TEST_INSTRUCTION, TEST_BRANCH_F (branch forwards) and TEST_BRANCH_B
* (branch backwards). In these, the local variables numbered 1, 50, 2 and
* 99 represent: test_before, test_case, test_after2 and test_done.
*
* FRAMEWORK
* ---------
*
* Each test case is wrapped between the pair of macros TESTCASE_START and
* TESTCASE_END. As well as performing the inline assembler boilerplate,
* these call out to the kprobes_test_case_start() and
* kprobes_test_case_end() functions which drive the execution of the test
* case. The specific arguments to use for each test case are stored as
* inline data constructed using the various TEST_ARG_* macros. Putting
* this all together, a simple test case may look like:
*
* TESTCASE_START("Testing mov r0, r7")
* TEST_ARG_REG(7, 0x12345678) // Set r7=0x12345678
* TEST_ARG_END("")
* TEST_INSTRUCTION("mov r0, r7")
* TESTCASE_END
*
* Note, in practice the single convenience macro TEST_R would be used for this
* instead.
*
* The above would expand to assembler looking something like:
*
* @ TESTCASE_START
* bl __kprobes_test_case_start
* @ start of inline data...
* .ascii "mov r0, r7" @ text title for test case
* .byte 0
* .align 2
*
* @ TEST_ARG_REG
* .byte ARG_TYPE_REG
* .byte 7
* .short 0
* .word 0x1234567
*
* @ TEST_ARG_END
* .byte ARG_TYPE_END
* .byte TEST_ISA @ flags, including ISA being tested
* .short 50f-0f @ offset of 'test_before'
* .short 2f-0f @ offset of 'test_after2' (if relevent)
* .short 99f-0f @ offset of 'test_done'
* @ start of test case code...
* 0:
* .code TEST_ISA @ switch to ISA being tested
*
* @ TEST_INSTRUCTION
* 50: nop @ location for 'test_before' probe
* 1: mov r0, r7 @ the test case instruction 'test_insn'
* nop @ location for 'test_after' probe
*
* // TESTCASE_END
* 2:
* 99: bl __kprobes_test_case_end_##TEST_ISA
* .code NONMAL_ISA
*
* When the above is execute the following happens...
*
* __kprobes_test_case_start() is an assembler wrapper which sets up space
* for a stack buffer and calls the C function kprobes_test_case_start().
* This C function will do some initial processing of the inline data and
* setup some global state. It then inserts the test_before and test_after
* kprobes and returns a value which causes the assembler wrapper to jump
* to the start of the test case code, (local label '0').
*
* When the test case code executes, the test_before probe will be hit and
* test_before_post_handler will call setup_test_context(). This fills the
* stack buffer and CPU registers with a test pattern and then processes
* the test case arguments. In our example there is one TEST_ARG_REG which
* indicates that R7 should be loaded with the value 0x12345678.
*
* When the test_before probe ends, the test case continues and executes
* the "mov r0, r7" instruction. It then hits the test_after probe and the
* pre-handler for this (test_after_pre_handler) will save a copy of the
* CPU register context. This should now have R0 holding the same value as
* R7.
*
* Finally we get to the call to __kprobes_test_case_end_{32,16}. This is
* an assembler wrapper which switches back to the ISA used by the test
* code and calls the C function kprobes_test_case_end().
*
* For each run through the test case, test_case_run_count is incremented
* by one. For even runs, kprobes_test_case_end() saves a copy of the
* register and stack buffer contents from the test case just run. It then
* inserts a kprobe on the test case instruction 'test_insn' and returns a
* value to cause the test case code to be re-run.
*
* For odd numbered runs, kprobes_test_case_end() compares the register and
* stack buffer contents to those that were saved on the previous even
* numbered run (the one without the kprobe on test_insn). These should be
* the same if the kprobe instruction simulation routine is correct.
*
* The pair of test case runs is repeated with different combinations of
* flag values in CPSR and, for Thumb, different ITState. This is
* controlled by test_context_cpsr().
*
* BUILDING TEST CASES
* -------------------
*
*
* As an aid to building test cases, the stack buffer is initialised with
* some special values:
*
* [SP+13*4] Contains SP+120. This can be used to test instructions
* which load a value into SP.
*
* [SP+15*4] When testing branching instructions using TEST_BRANCH_{F,B},
* this holds the target address of the branch, 'test_after2'.
* This can be used to test instructions which load a PC value
* from memory.
*/
#include <linux/kernel.h>
#ifdef KERNEL
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kprobes.h>
#include <asm/opcodes.h>
#include "kprobes.h"
#endif
#ifndef KERNEL
#include "userspace.h"
#endif
#include "kprobes-test.h"
#define BENCHMARKING 0
#define SANITY 0
/*
* Test basic API
*/
#ifdef KERNEL
static bool test_regs_ok;
static int test_func_instance;
static int pre_handler_called;
static int post_handler_called;
#ifdef KERNEL
static int jprobe_func_called;
static int kretprobe_handler_called;
#endif
#define FUNC_ARG1 0x12345678
#define FUNC_ARG2 0xabcdef
#ifndef CONFIG_THUMB2_KERNEL
long arm_func(long r0, long r1);
static void __used __naked __arm_kprobes_test_func(void)
{
__asm__ __volatile__ (
".arm \n\t"
".type arm_func, %%function \n\t"
"arm_func: \n\t"
"adds r0, r0, r1 \n\t"
"bx lr \n\t"
".code "NORMAL_ISA /* Back to Thumb if necessary */
: : : "r0", "r1", "cc"
);
}
#else /* CONFIG_THUMB2_KERNEL */
long thumb16_func(long r0, long r1);
long thumb32even_func(long r0, long r1);
long thumb32odd_func(long r0, long r1);
static void __used __naked __thumb_kprobes_test_funcs(void)
{
__asm__ __volatile__ (
".type thumb16_func, %%function \n\t"
"thumb16_func: \n\t"
"adds.n r0, r0, r1 \n\t"
"bx lr \n\t"
".align \n\t"
".type thumb32even_func, %%function \n\t"
"thumb32even_func: \n\t"
"adds.w r0, r0, r1 \n\t"
"bx lr \n\t"
".align \n\t"
"nop.n \n\t"
".type thumb32odd_func, %%function \n\t"
"thumb32odd_func: \n\t"
"adds.w r0, r0, r1 \n\t"
"bx lr \n\t"
: : : "r0", "r1", "cc"
);
}
#endif /* CONFIG_THUMB2_KERNEL */
static int call_test_func(long (*func)(long, long), bool check_test_regs)
{
long ret;
++test_func_instance;
test_regs_ok = false;
ret = (*func)(FUNC_ARG1, FUNC_ARG2);
if (ret != FUNC_ARG1 + FUNC_ARG2) {
pr_err("FAIL: call_test_func: func returned %lx\n", ret);
return false;
}
if (check_test_regs && !test_regs_ok) {
pr_err("FAIL: test regs not OK\n");
return false;
}
return true;
}
static int __kprobes pre_handler(struct kprobe *p, struct pt_regs *regs)
{
pre_handler_called = test_func_instance;
if (regs->ARM_r0 == FUNC_ARG1 && regs->ARM_r1 == FUNC_ARG2)
test_regs_ok = true;
return 0;
}
static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
post_handler_called = test_func_instance;
if (regs->ARM_r0 != FUNC_ARG1 + FUNC_ARG2 || regs->ARM_r1 != FUNC_ARG2)
test_regs_ok = false;
}
static struct kprobe the_kprobe = {
.addr = 0,
.pre_handler = pre_handler,
.post_handler = post_handler
};
static int test_kprobe(long (*func)(long, long))
{
int ret;
the_kprobe.addr = (kprobe_opcode_t *)func;
ret = register_kprobe(&the_kprobe);
if (ret < 0) {
pr_err("FAIL: register_kprobe failed with %d\n", ret);
return ret;
}
ret = call_test_func(func, true);
unregister_kprobe(&the_kprobe);
the_kprobe.flags = 0; /* Clear disable flag to allow reuse */
if (!ret)
return -EINVAL;
if (pre_handler_called != test_func_instance) {
pr_err("FAIL: kprobe pre_handler not called\n");
return -EINVAL;
}
if (post_handler_called != test_func_instance) {
pr_err("FAIL: kprobe post_handler not called\n");
return -EINVAL;
}
if (!call_test_func(func, false))
return -EINVAL;
if (pre_handler_called == test_func_instance ||
post_handler_called == test_func_instance) {
pr_err("FAIL: probe called after unregistering\n");
return -EINVAL;
}
return 0;
}
static void __kprobes jprobe_func(long r0, long r1)
{
jprobe_func_called = test_func_instance;
if (r0 == FUNC_ARG1 && r1 == FUNC_ARG2)
test_regs_ok = true;
jprobe_return();
}
static struct jprobe the_jprobe = {
.entry = jprobe_func,
};
static int test_jprobe(long (*func)(long, long))
{
int ret;
the_jprobe.kp.addr = (kprobe_opcode_t *)func;
ret = register_jprobe(&the_jprobe);
if (ret < 0) {
pr_err("FAIL: register_jprobe failed with %d\n", ret);
return ret;
}
ret = call_test_func(func, true);
unregister_jprobe(&the_jprobe);
the_jprobe.kp.flags = 0; /* Clear disable flag to allow reuse */
if (!ret)
return -EINVAL;
if (jprobe_func_called != test_func_instance) {
pr_err("FAIL: jprobe handler function not called\n");
return -EINVAL;
}
if (!call_test_func(func, false))
return -EINVAL;
if (jprobe_func_called == test_func_instance) {
pr_err("FAIL: probe called after unregistering\n");
return -EINVAL;
}
return 0;
}
static int __kprobes
kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{
kretprobe_handler_called = test_func_instance;
if (regs_return_value(regs) == FUNC_ARG1 + FUNC_ARG2)
test_regs_ok = true;
return 0;
}
static struct kretprobe the_kretprobe = {
.handler = kretprobe_handler,
};
static int test_kretprobe(long (*func)(long, long))
{
int ret;
the_kretprobe.kp.addr = (kprobe_opcode_t *)func;
ret = register_kretprobe(&the_kretprobe);
if (ret < 0) {
pr_err("FAIL: register_kretprobe failed with %d\n", ret);
return ret;
}
ret = call_test_func(func, true);
unregister_kretprobe(&the_kretprobe);
the_kretprobe.kp.flags = 0; /* Clear disable flag to allow reuse */
if (!ret)
return -EINVAL;
if (kretprobe_handler_called != test_func_instance) {
pr_err("FAIL: kretprobe handler not called\n");
return -EINVAL;
}
if (!call_test_func(func, false))
return -EINVAL;
if (jprobe_func_called == test_func_instance) {
pr_err("FAIL: kretprobe called after unregistering\n");
return -EINVAL;
}
return 0;
}
#endif
#ifdef KERNEL
static int run_api_tests(long (*func)(long, long))
{
int ret;
pr_info(" kprobe\n");
ret = test_kprobe(func);
if (ret < 0)
return ret;
pr_info(" jprobe\n");
ret = test_jprobe(func);
if (ret < 0)
return ret;
pr_info(" kretprobe\n");
ret = test_kretprobe(func);
if (ret < 0)
return ret;
return 0;
}
#endif
/*
* Benchmarking
*/
#if BENCHMARKING
static void __naked benchmark_nop(void)
{
__asm__ __volatile__ (
"nop \n\t"
"bx lr"
);
}
#ifdef CONFIG_THUMB2_KERNEL
#define wide ".w"
#else
#define wide
#endif
static void __naked benchmark_pushpop1(void)
{
__asm__ __volatile__ (
"stmdb"wide" sp!, {r3-r11,lr} \n\t"
"ldmia"wide" sp!, {r3-r11,pc}"
);
}
static void __naked benchmark_pushpop2(void)
{
__asm__ __volatile__ (
"stmdb"wide" sp!, {r0-r8,lr} \n\t"
"ldmia"wide" sp!, {r0-r8,pc}"
);
}
static void __naked benchmark_pushpop3(void)
{
__asm__ __volatile__ (
"stmdb"wide" sp!, {r4,lr} \n\t"
"ldmia"wide" sp!, {r4,pc}"
);
}
static void __naked benchmark_pushpop4(void)
{
__asm__ __volatile__ (
"stmdb"wide" sp!, {r0,lr} \n\t"
"ldmia"wide" sp!, {r0,pc}"
);
}
#ifdef CONFIG_THUMB2_KERNEL
static void __naked benchmark_pushpop_thumb(void)
{
__asm__ __volatile__ (
"push.n {r0-r7,lr} \n\t"
"pop.n {r0-r7,pc}"
);
}
#endif
static int __kprobes
benchmark_pre_handler(struct kprobe *p, struct pt_regs *regs)
{
return 0;
}
static int benchmark(void(*fn)(void))
{
unsigned n, i, t, t0;
for (n = 1000; ; n *= 2) {
t0 = sched_clock();
for (i = n; i > 0; --i)
fn();
t = sched_clock() - t0;
if (t >= 250000000)
break; /* Stop once we took more than 0.25 seconds */
}
return t / n; /* Time for one iteration in nanoseconds */
};
static int kprobe_benchmark(void(*fn)(void), unsigned offset)
{
struct kprobe k = {
.addr = (kprobe_opcode_t *)((uintptr_t)fn + offset),
.pre_handler = benchmark_pre_handler,
};
int ret = register_kprobe(&k);
if (ret < 0) {
pr_err("FAIL: register_kprobe failed with %d\n", ret);
return ret;
}
ret = benchmark(fn);
unregister_kprobe(&k);
return ret;
};
struct benchmarks {
void (*fn)(void);
unsigned offset;
const char *title;
};
static int run_benchmarks(void)
{
int ret;
struct benchmarks list[] = {
{&benchmark_nop, 0, "nop"},
/*
* benchmark_pushpop{1,3} will have the optimised
* instruction emulation, whilst benchmark_pushpop{2,4} will
* be the equivalent unoptimised instructions.
*/
{&benchmark_pushpop1, 0, "stmdb sp!, {r3-r11,lr}"},
{&benchmark_pushpop1, 4, "ldmia sp!, {r3-r11,pc}"},
{&benchmark_pushpop2, 0, "stmdb sp!, {r0-r8,lr}"},
{&benchmark_pushpop2, 4, "ldmia sp!, {r0-r8,pc}"},
{&benchmark_pushpop3, 0, "stmdb sp!, {r4,lr}"},
{&benchmark_pushpop3, 4, "ldmia sp!, {r4,pc}"},
{&benchmark_pushpop4, 0, "stmdb sp!, {r0,lr}"},
{&benchmark_pushpop4, 4, "ldmia sp!, {r0,pc}"},
#ifdef CONFIG_THUMB2_KERNEL
{&benchmark_pushpop_thumb, 0, "push.n {r0-r7,lr}"},
{&benchmark_pushpop_thumb, 2, "pop.n {r0-r7,pc}"},
#endif
{0}
};
struct benchmarks *b;
for (b = list; b->fn; ++b) {
ret = kprobe_benchmark(b->fn, b->offset);
if (ret < 0)
return ret;
pr_info(" %dns for kprobe %s\n", ret, b->title);
}
pr_info("\n");
return 0;
}
#endif /* BENCHMARKING */
#if SANITY
/*
* Decoding table self-consistency tests
*/
static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
[DECODE_TYPE_TABLE] = sizeof(struct decode_table),
[DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom),
[DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate),
[DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate),
[DECODE_TYPE_OR] = sizeof(struct decode_or),
[DECODE_TYPE_REJECT] = sizeof(struct decode_reject)
};
static int table_iter(const union decode_item *table,
int (*fn)(const struct decode_header *, void *),
void *args)
{
const struct decode_header *h = (struct decode_header *)table;
int result;
for (;;) {
enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
if (type == DECODE_TYPE_END)
return 0;
result = fn(h, args);
if (result)
return result;
h = (struct decode_header *)
((uintptr_t)h + decode_struct_sizes[type]);
}
}
static int table_test_fail(const struct decode_header *h, const char* message)
{
pr_err("FAIL: kprobes test failure \"%s\" (mask %08x, value %08x)\n",
message, h->mask.bits, h->value.bits);
return -EINVAL;
}
struct table_test_args {
const union decode_item *root_table;
u32 parent_mask;
u32 parent_value;
};
static int table_test_fn(const struct decode_header *h, void *args)
{
struct table_test_args *a = (struct table_test_args *)args;
enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
if (h->value.bits & ~h->mask.bits)
return table_test_fail(h, "Match value has bits not in mask");
if ((h->mask.bits & a->parent_mask) != a->parent_mask)
return table_test_fail(h, "Mask has bits not in parent mask");
if ((h->value.bits ^ a->parent_value) & a->parent_mask)
return table_test_fail(h, "Value is inconsistent with parent");
if (type == DECODE_TYPE_TABLE) {
struct decode_table *d = (struct decode_table *)h;
struct table_test_args args2 = *a;
args2.parent_mask = h->mask.bits;
args2.parent_value = h->value.bits;
return table_iter(d->table.table, table_test_fn, &args2);
}
return 0;
}
static int table_test(const union decode_item *table)
{
struct table_test_args args = {
.root_table = table,
.parent_mask = 0,
.parent_value = 0
};
return table_iter(args.root_table, table_test_fn, &args);
}
#endif
/*
* Decoding table test coverage analysis
*
* coverage_start() builds a coverage_table which contains a list of
* coverage_entry's to match each entry in the specified kprobes instruction
* decoding table.
*
* When test cases are run, coverage_add() is called to process each case.
* This looks up the corresponding entry in the coverage_table and sets it as
* being matched, as well as clearing the regs flag appropriate for the test.
*
* After all test cases have been run, coverage_end() is called to check that
* all entries in coverage_table have been matched and that all regs flags are
* cleared. I.e. that all possible combinations of instructions described by
* the kprobes decoding tables have had a test case executed for them.
*/
#ifdef COVERAGE
bool coverage_fail;
#define MAX_COVERAGE_ENTRIES 256
struct coverage_entry {
const struct decode_header *header;
unsigned regs;
unsigned nesting;
char matched;
};
struct coverage_table {
struct coverage_entry *base;
unsigned num_entries;
unsigned nesting;
};
struct coverage_table coverage;
#define COVERAGE_ANY_REG (1<<0)
#define COVERAGE_SP (1<<1)
#define COVERAGE_PC (1<<2)
#define COVERAGE_PCWB (1<<3)
static const char coverage_register_lookup[16] = {
[REG_TYPE_ANY] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC,
[REG_TYPE_SAMEAS16] = COVERAGE_ANY_REG,
[REG_TYPE_SP] = COVERAGE_SP,
[REG_TYPE_PC] = COVERAGE_PC,
[REG_TYPE_NOSP] = COVERAGE_ANY_REG | COVERAGE_SP,
[REG_TYPE_NOSPPC] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC,
[REG_TYPE_NOPC] = COVERAGE_ANY_REG | COVERAGE_PC,
[REG_TYPE_NOPCWB] = COVERAGE_ANY_REG | COVERAGE_PC | COVERAGE_PCWB,
[REG_TYPE_NOPCX] = COVERAGE_ANY_REG,
[REG_TYPE_NOSPPCX] = COVERAGE_ANY_REG | COVERAGE_SP,
};
unsigned coverage_start_registers(const struct decode_header *h)
{
unsigned regs = 0;
int i;
for (i = 0; i < 20; i += 4) {
int r = (h->type_regs.bits >> (DECODE_TYPE_BITS + i)) & 0xf;
regs |= coverage_register_lookup[r] << i;
}
return regs;
}
static int coverage_start_fn(const struct decode_header *h, void *args)
{
struct coverage_table *coverage = (struct coverage_table *)args;
enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
struct coverage_entry *entry = coverage->base + coverage->num_entries;
if (coverage->num_entries == MAX_COVERAGE_ENTRIES - 1) {
pr_err("FAIL: Out of space for test coverage data");
return -ENOMEM;
}
++coverage->num_entries;
entry->header = h;
entry->regs = coverage_start_registers(h);
entry->nesting = coverage->nesting;
entry->matched = false;
if (type == DECODE_TYPE_TABLE) {
struct decode_table *d = (struct decode_table *)h;
int ret;
++coverage->nesting;
ret = table_iter(d->table.table, coverage_start_fn, coverage);
--coverage->nesting;
return ret;
}
return 0;
}
static int coverage_start(const union decode_item *table)
{
coverage.base = kmalloc(MAX_COVERAGE_ENTRIES *
sizeof(struct coverage_entry), GFP_KERNEL);
coverage.num_entries = 0;
coverage.nesting = 0;
return table_iter(table, coverage_start_fn, &coverage);
}
static void
coverage_add_registers(struct coverage_entry *entry, kprobe_opcode_t insn)
{
int regs = entry->header->type_regs.bits >> DECODE_TYPE_BITS;
int i;
for (i = 0; i < 20; i += 4) {
enum decode_reg_type reg_type = (regs >> i) & 0xf;
int reg = (insn >> i) & 0xf;
int flag;
if (!reg_type)
continue;
if (reg == 13)
flag = COVERAGE_SP;
else if (reg == 15)
flag = COVERAGE_PC;
else
flag = COVERAGE_ANY_REG;
entry->regs &= ~(flag << i);
switch (reg_type) {
case REG_TYPE_NONE:
case REG_TYPE_ANY:
case REG_TYPE_SAMEAS16:
break;
case REG_TYPE_SP:
if (reg != 13)
return;
break;
case REG_TYPE_PC:
if (reg != 15)
return;
break;
case REG_TYPE_NOSP:
if (reg == 13)
return;
break;
case REG_TYPE_NOSPPC:
case REG_TYPE_NOSPPCX:
if (reg == 13 || reg == 15)
return;
break;
case REG_TYPE_NOPCWB:
if (!is_writeback(insn))
break;
if (reg == 15) {
entry->regs &= ~(COVERAGE_PCWB << i);
return;
}
break;
case REG_TYPE_NOPC:
case REG_TYPE_NOPCX:
if (reg == 15)
return;
break;
}
}
}
static void coverage_add(kprobe_opcode_t insn)
{
struct coverage_entry *entry = coverage.base;
struct coverage_entry *end = coverage.base + coverage.num_entries;
bool matched = false;
unsigned nesting = 0;
for (; entry < end; ++entry) {
const struct decode_header *h = entry->header;
enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
if (entry->nesting > nesting)
continue; /* Skip sub-table we didn't match */
if (entry->nesting < nesting)
break; /* End of sub-table we were scanning */
if (!matched) {
if ((insn & h->mask.bits) != h->value.bits)
continue;
entry->matched = true;
}
switch (type) {
case DECODE_TYPE_TABLE:
++nesting;
break;
case DECODE_TYPE_CUSTOM:
case DECODE_TYPE_SIMULATE:
case DECODE_TYPE_EMULATE:
coverage_add_registers(entry, insn);
return;
case DECODE_TYPE_OR:
matched = true;
break;
case DECODE_TYPE_REJECT:
default:
return;
}
}
}
static void coverage_end(void)
{
struct coverage_entry *entry = coverage.base;
struct coverage_entry *end = coverage.base + coverage.num_entries;
for (; entry < end; ++entry) {
u32 mask = entry->header->mask.bits;
u32 value = entry->header->value.bits;
if (entry->regs) {
pr_err("FAIL: Register test coverage missing for %08x %08x (%05x)\n",
mask, value, entry->regs);
coverage_fail = true;
}
if (!entry->matched) {
pr_err("FAIL: Test coverage entry missing for %08x %08x\n",
mask, value);
coverage_fail = true;
}
}
kfree(coverage.base);
}
#endif
/*
* Framework for instruction set test cases
*/
void __naked __kprobes_test_case_start(void)
{
__asm__ __volatile__ (
"stmdb sp!, {r4-r11} \n\t"
"sub sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t"
"bic r0, lr, #1 @ r0 = inline title string \n\t"
"mov r1, sp \n\t"
"bl kprobes_test_case_start \n\t"
"bx r0 \n\t"
);
}
#ifndef CONFIG_THUMB2_KERNEL
void __naked __kprobes_test_case_end_32(void)
{
__asm__ __volatile__ (
"mov r4, lr \n\t"
"bl kprobes_test_case_end \n\t"
"cmp r0, #0 \n\t"
"movne pc, r0 \n\t"
"mov r0, r4 \n\t"
"add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t"
"ldmia sp!, {r4-r11} \n\t"
"mov pc, r0 \n\t"
);
}
#else /* CONFIG_THUMB2_KERNEL */