200 #include <linux/kernel.h>
201 #include <linux/module.h>
202 #include <linux/slab.h>
211 #define BENCHMARKING 1
218 static bool test_regs_ok;
219 static int test_func_instance;
220 static int pre_handler_called;
221 static int post_handler_called;
222 static int jprobe_func_called;
223 static int kretprobe_handler_called;
225 #define FUNC_ARG1 0x12345678
226 #define FUNC_ARG2 0xabcdef
229 #ifndef CONFIG_THUMB2_KERNEL
237 ".type arm_func, %%function \n\t"
239 "adds r0, r0, r1 \n\t"
242 : : :
"r0",
"r1",
"cc"
248 long thumb16_func(
long r0,
long r1);
249 long thumb32even_func(
long r0,
long r1);
250 long thumb32odd_func(
long r0,
long r1);
255 ".type thumb16_func, %%function \n\t"
257 "adds.n r0, r0, r1 \n\t"
261 ".type thumb32even_func, %%function \n\t"
262 "thumb32even_func: \n\t"
263 "adds.w r0, r0, r1 \n\t"
268 ".type thumb32odd_func, %%function \n\t"
269 "thumb32odd_func: \n\t"
270 "adds.w r0, r0, r1 \n\t"
273 : : :
"r0",
"r1",
"cc"
280 static int call_test_func(
long (*
func)(
long,
long),
bool check_test_regs)
284 ++test_func_instance;
285 test_regs_ok =
false;
289 pr_err(
"FAIL: call_test_func: func returned %lx\n", ret);
293 if (check_test_regs && !test_regs_ok) {
294 pr_err(
"FAIL: test regs not OK\n");
303 pre_handler_called = test_func_instance;
304 if (regs->ARM_r0 == FUNC_ARG1 && regs->ARM_r1 ==
FUNC_ARG2)
312 post_handler_called = test_func_instance;
314 test_regs_ok =
false;
317 static struct kprobe the_kprobe = {
323 static int test_kprobe(
long (*
func)(
long,
long))
330 pr_err(
"FAIL: register_kprobe failed with %d\n", ret);
334 ret = call_test_func(
func,
true);
337 the_kprobe.
flags = 0;
341 if (pre_handler_called != test_func_instance) {
342 pr_err(
"FAIL: kprobe pre_handler not called\n");
345 if (post_handler_called != test_func_instance) {
346 pr_err(
"FAIL: kprobe post_handler not called\n");
349 if (!call_test_func(
func,
false))
351 if (pre_handler_called == test_func_instance ||
352 post_handler_called == test_func_instance) {
353 pr_err(
"FAIL: probe called after unregistering\n");
362 jprobe_func_called = test_func_instance;
368 static struct jprobe the_jprobe = {
369 .entry = jprobe_func,
372 static int test_jprobe(
long (*
func)(
long,
long))
379 pr_err(
"FAIL: register_jprobe failed with %d\n", ret);
383 ret = call_test_func(
func,
true);
386 the_jprobe.
kp.flags = 0;
390 if (jprobe_func_called != test_func_instance) {
391 pr_err(
"FAIL: jprobe handler function not called\n");
394 if (!call_test_func(
func,
false))
396 if (jprobe_func_called == test_func_instance) {
397 pr_err(
"FAIL: probe called after unregistering\n");
407 kretprobe_handler_called = test_func_instance;
408 if (regs_return_value(regs) == FUNC_ARG1 +
FUNC_ARG2)
413 static struct kretprobe the_kretprobe = {
414 .handler = kretprobe_handler,
417 static int test_kretprobe(
long (*
func)(
long,
long))
424 pr_err(
"FAIL: register_kretprobe failed with %d\n", ret);
428 ret = call_test_func(
func,
true);
431 the_kretprobe.
kp.flags = 0;
435 if (kretprobe_handler_called != test_func_instance) {
436 pr_err(
"FAIL: kretprobe handler not called\n");
439 if (!call_test_func(
func,
false))
441 if (jprobe_func_called == test_func_instance) {
442 pr_err(
"FAIL: kretprobe called after unregistering\n");
449 static int run_api_tests(
long (*
func)(
long,
long))
454 ret = test_kprobe(
func);
459 ret = test_jprobe(
func);
464 ret = test_kretprobe(
func);
478 static void __naked benchmark_nop(
void)
486 #ifdef CONFIG_THUMB2_KERNEL
492 static void __naked benchmark_pushpop1(
void)
495 "stmdb"wide" sp!, {r3-r11,lr} \n\t"
496 "ldmia"wide" sp!, {r3-r11,pc}"
500 static void __naked benchmark_pushpop2(
void)
503 "stmdb"wide" sp!, {r0-r8,lr} \n\t"
504 "ldmia"wide" sp!, {r0-r8,pc}"
508 static void __naked benchmark_pushpop3(
void)
511 "stmdb"wide" sp!, {r4,lr} \n\t"
512 "ldmia"wide" sp!, {r4,pc}"
516 static void __naked benchmark_pushpop4(
void)
519 "stmdb"wide" sp!, {r0,lr} \n\t"
520 "ldmia"wide" sp!, {r0,pc}"
525 #ifdef CONFIG_THUMB2_KERNEL
527 static void __naked benchmark_pushpop_thumb(
void)
530 "push.n {r0-r7,lr} \n\t"
538 benchmark_pre_handler(
struct kprobe *p,
struct pt_regs *regs)
543 static int benchmark(
void(*
fn)(
void))
545 unsigned n,
i,
t,
t0;
547 for (n = 1000; ; n *= 2) {
549 for (i = n; i > 0; --
i)
558 static int kprobe_benchmark(
void(*
fn)(
void),
unsigned offset)
567 pr_err(
"FAIL: register_kprobe failed with %d\n", ret);
583 static int run_benchmarks(
void)
587 {&benchmark_nop, 0,
"nop"},
593 {&benchmark_pushpop1, 0,
"stmdb sp!, {r3-r11,lr}"},
594 {&benchmark_pushpop1, 4,
"ldmia sp!, {r3-r11,pc}"},
595 {&benchmark_pushpop2, 0,
"stmdb sp!, {r0-r8,lr}"},
596 {&benchmark_pushpop2, 4,
"ldmia sp!, {r0-r8,pc}"},
597 {&benchmark_pushpop3, 0,
"stmdb sp!, {r4,lr}"},
598 {&benchmark_pushpop3, 4,
"ldmia sp!, {r4,pc}"},
599 {&benchmark_pushpop4, 0,
"stmdb sp!, {r0,lr}"},
600 {&benchmark_pushpop4, 4,
"ldmia sp!, {r0,pc}"},
601 #ifdef CONFIG_THUMB2_KERNEL
602 {&benchmark_pushpop_thumb, 0,
"push.n {r0-r7,lr}"},
603 {&benchmark_pushpop_thumb, 2,
"pop.n {r0-r7,pc}"},
609 for (b = list; b->
fn; ++
b) {
610 ret = kprobe_benchmark(b->
fn, b->
offset);
649 result =
fn(h, args);
662 pr_err(
"FAIL: kprobes test failure \"%s\" (mask %08x, value %08x)\n",
673 static int table_test_fn(
const struct decode_header *h,
void *args)
679 return table_test_fail(h,
"Match value has bits not in mask");
682 return table_test_fail(h,
"Mask has bits not in parent mask");
685 return table_test_fail(h,
"Value is inconsistent with parent");
692 return table_iter(d->
table.
table, table_test_fn, &args2);
698 static int table_test(
const union decode_item *table)
705 return table_iter(args.
root_table, table_test_fn, &args);
728 #define MAX_COVERAGE_ENTRIES 256
745 #define COVERAGE_ANY_REG (1<<0)
746 #define COVERAGE_SP (1<<1)
747 #define COVERAGE_PC (1<<2)
748 #define COVERAGE_PCWB (1<<3)
750 static const char coverage_register_lookup[16] = {
767 for (i = 0; i < 20; i += 4) {
769 regs |= coverage_register_lookup[
r] <<
i;
774 static int coverage_start_fn(
const struct decode_header *h,
void *args)
781 pr_err(
"FAIL: Out of space for test coverage data");
796 ret = table_iter(d->
table.
table, coverage_start_fn, coverage);
804 static int coverage_start(
const union decode_item *table)
810 return table_iter(table, coverage_start_fn, &coverage);
818 for (i = 0; i < 20; i += 4) {
820 int reg = (insn >>
i) & 0xf;
832 entry->
regs &= ~(flag <<
i);
858 if (reg == 13 || reg == 15)
913 coverage_add_registers(entry, insn);
928 static void coverage_end(
void)
938 pr_err(
"FAIL: Register test coverage missing for %08x %08x (%05x)\n",
939 mask, value, entry->
regs);
940 coverage_fail =
true;
943 pr_err(
"FAIL: Test coverage entry missing for %08x %08x\n",
945 coverage_fail =
true;
960 "stmdb sp!, {r4-r11} \n\t"
962 "bic r0, lr, #1 @ r0 = inline title string \n\t"
964 "bl kprobes_test_case_start \n\t"
969 #ifndef CONFIG_THUMB2_KERNEL
975 "bl kprobes_test_case_end \n\t"
980 "ldmia sp!, {r4-r11} \n\t"
987 void __naked __kprobes_test_case_end_16(
void)
991 "bl kprobes_test_case_end \n\t"
996 "ldmia sp!, {r4-r11} \n\t"
1005 "orr lr, lr, #1 @ will return to Thumb code \n\t"
1008 ".word __kprobes_test_case_end_16 \n\t"
1018 static int test_try_count;
1019 static int test_pass_count;
1020 static int test_fail_count;
1022 static struct pt_regs initial_regs;
1023 static struct pt_regs expected_regs;
1024 static struct pt_regs result_regs;
1028 static const char *current_title;
1029 static struct test_arg *current_args;
1030 static u32 *current_stack;
1037 #define TEST_CASE_PASSED -1
1038 #define TEST_CASE_FAILED -2
1040 static int test_case_run_count;
1041 static bool test_case_is_thumb;
1042 static int test_instance;
1051 #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
1053 static unsigned long test_check_cc(
int cc,
unsigned long cpsr)
1060 static int is_last_scenario;
1061 static int probe_should_run;
1062 static int memory_needs_checking;
1064 static unsigned long test_context_cpsr(
int scenario)
1068 probe_should_run = 1;
1071 cpsr = (scenario & 0xf) << 28;
1072 cpsr |= (scenario & 0xf) << 16;
1073 cpsr |= (scenario & 0x1) << 27;
1075 if (!test_case_is_thumb) {
1077 int cc = current_instruction >> 28;
1079 probe_should_run = test_check_cc(cc, cpsr) != 0;
1081 is_last_scenario =
true;
1085 if (kprobe_test_cc_position) {
1087 probe_should_run = test_check_cc(cc, cpsr) != 0;
1091 is_last_scenario =
true;
1095 unsigned x = (scenario >> 4);
1096 unsigned cond_base = x % 7;
1097 unsigned mask = x / 7 + 2;
1103 if ((scenario & 0xf) == 0xf)
1104 is_last_scenario =
true;
1107 cpsr |= cond_base << 13;
1108 cpsr |= (mask & 0x1) << 12;
1109 cpsr |= (mask & 0x2) << 10;
1110 cpsr |= (mask & 0x4) << 8;
1111 cpsr |= (mask & 0x8) << 23;
1112 cpsr |= (mask & 0x10) << 21;
1114 probe_should_run = test_check_cc((cpsr >> 12) & 0xf, cpsr) != 0;
1121 probe_should_run = 0;
1125 probe_should_run = 0;
1132 is_last_scenario =
true;
1140 static void setup_test_context(
struct pt_regs *regs)
1142 int scenario = test_case_run_count>>1;
1147 is_last_scenario =
false;
1148 memory_needs_checking =
false;
1151 val = (scenario & 1) ?
VALM : ~
VALM;
1153 current_stack[i] = val + (i << 8);
1155 if (current_branch_target)
1156 current_stack[15] = current_branch_target;
1158 current_stack[13] = (
u32)current_stack + 120;
1161 val = (scenario & 2) ?
VALR : ~
VALR;
1162 for (i = 0; i < 13; ++
i)
1163 regs->
uregs[i] = val ^ (i << 8);
1164 regs->ARM_lr = val ^ (14 << 8);
1166 regs->ARM_cpsr |= test_context_cpsr(scenario);
1169 args = current_args;
1171 switch (args[0].type) {
1182 (
unsigned long)current_stack + arg->
val;
1183 memory_needs_checking =
true;
1188 current_stack[arg->
index] = arg->
val;
1202 static void unregister_test_probe(
struct test_probe *probe)
1211 static int register_test_probe(
struct test_probe *probe)
1227 test_before_pre_handler(
struct kprobe *p,
struct pt_regs *regs)
1234 test_before_post_handler(
struct kprobe *p,
struct pt_regs *regs,
1235 unsigned long flags)
1237 setup_test_context(regs);
1238 initial_regs = *
regs;
1243 test_case_pre_handler(
struct kprobe *p,
struct pt_regs *regs)
1250 test_after_pre_handler(
struct kprobe *p,
struct pt_regs *regs)
1255 result_regs = *
regs;
1259 regs->ARM_sp = (
unsigned long)current_stack;
1265 static struct test_probe test_before_probe = {
1266 .kprobe.pre_handler = test_before_pre_handler,
1267 .kprobe.post_handler = test_before_post_handler,
1271 .kprobe.pre_handler = test_case_pre_handler,
1274 static struct test_probe test_after_probe = {
1275 .kprobe.pre_handler = test_after_pre_handler,
1278 static struct test_probe test_after2_probe = {
1279 .kprobe.pre_handler = test_after_pre_handler,
1282 static void test_case_cleanup(
void)
1284 unregister_test_probe(&test_before_probe);
1285 unregister_test_probe(&test_case_probe);
1286 unregister_test_probe(&test_after_probe);
1287 unregister_test_probe(&test_after2_probe);
1290 static void print_registers(
struct pt_regs *regs)
1292 pr_err(
"r0 %08lx | r1 %08lx | r2 %08lx | r3 %08lx\n",
1293 regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3);
1294 pr_err(
"r4 %08lx | r5 %08lx | r6 %08lx | r7 %08lx\n",
1295 regs->ARM_r4, regs->ARM_r5, regs->ARM_r6, regs->ARM_r7);
1296 pr_err(
"r8 %08lx | r9 %08lx | r10 %08lx | r11 %08lx\n",
1297 regs->ARM_r8, regs->ARM_r9, regs->ARM_r10, regs->ARM_fp);
1298 pr_err(
"r12 %08lx | sp %08lx | lr %08lx | pc %08lx\n",
1299 regs->ARM_ip, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc);
1300 pr_err(
"cpsr %08lx\n", regs->ARM_cpsr);
1303 static void print_memory(
u32 *
mem,
size_t size)
1306 for (i = 0; i < size /
sizeof(
u32); i += 4)
1307 pr_err(
"%08x %08x %08x %08x\n", mem[i], mem[i+1],
1308 mem[i+2], mem[i+3]);
1311 static size_t expected_memory_size(
u32 *
sp)
1313 size_t size =
sizeof(expected_memory);
1320 static void test_case_failed(
const char *
message)
1322 test_case_cleanup();
1324 pr_err(
"FAIL: %s\n", message);
1325 pr_err(
"FAIL: Test %s\n", current_title);
1326 pr_err(
"FAIL: Scenario %d\n", test_case_run_count >> 1);
1329 static unsigned long next_instruction(
unsigned long pc)
1331 #ifdef CONFIG_THUMB2_KERNEL
1343 unsigned long test_code;
1347 current_title = title;
1348 current_args = args;
1349 current_stack =
stack;
1357 test_code = (
unsigned long)(args + 1);
1360 if (test_case_is_thumb)
1363 current_code_start = test_code;
1365 current_branch_target = 0;
1372 test_code = next_instruction(test_code);
1375 if (test_case_is_thumb) {
1376 u16 *p = (
u16 *)(test_code & ~1);
1377 current_instruction = p[0];
1379 current_instruction <<= 16;
1380 current_instruction |= p[1];
1383 current_instruction = *(
u32 *)test_code;
1386 if (current_title[0] ==
'.')
1387 verbose(
"%s\n", current_title);
1389 verbose(
"%s\t@ %0*x\n", current_title,
1390 test_case_is_thumb ? 4 : 8,
1391 current_instruction);
1393 test_code = next_instruction(test_code);
1397 if (!test_case_is_thumb ||
1399 test_case_failed(
"expected 16-bit instruction");
1403 if (test_case_is_thumb &&
1405 test_case_failed(
"expected 32-bit instruction");
1410 coverage_add(current_instruction);
1413 if (register_test_probe(&test_case_probe) < 0)
1415 test_case_failed(
"registered probe for unsupported instruction");
1420 if (register_test_probe(&test_case_probe) >= 0)
1422 test_case_failed(
"couldn't register probe for supported instruction");
1426 if (register_test_probe(&test_before_probe) < 0) {
1427 test_case_failed(
"register test_before_probe failed");
1430 if (register_test_probe(&test_after_probe) < 0) {
1431 test_case_failed(
"register test_after_probe failed");
1434 if (current_branch_target) {
1435 test_after2_probe.
kprobe.addr =
1437 if (register_test_probe(&test_after2_probe) < 0) {
1438 test_case_failed(
"register test_after2_probe failed");
1444 test_case_run_count = 0;
1446 return current_code_start;
1455 static bool check_test_results(
void)
1460 if (
memcmp(&expected_regs, &result_regs,
sizeof(expected_regs))) {
1461 test_case_failed(
"registers differ");
1465 if (memory_needs_checking) {
1466 mem = (
u32 *)result_regs.ARM_sp;
1467 mem_size = expected_memory_size(mem);
1468 if (
memcmp(expected_memory, mem, mem_size)) {
1469 test_case_failed(
"test memory differs");
1477 pr_err(
"initial_regs:\n");
1478 print_registers(&initial_regs);
1479 pr_err(
"expected_regs:\n");
1480 print_registers(&expected_regs);
1481 pr_err(
"result_regs:\n");
1482 print_registers(&result_regs);
1485 pr_err(
"current_stack=%p\n", current_stack);
1486 pr_err(
"expected_memory:\n");
1487 print_memory(expected_memory, mem_size);
1488 pr_err(
"result_memory:\n");
1489 print_memory(mem, mem_size);
1497 if (test_case_run_count < 0) {
1506 if (test_before_probe.
hit != test_instance) {
1507 test_case_failed(
"test_before_handler not run");
1511 if (test_after_probe.
hit != test_instance &&
1512 test_after2_probe.
hit != test_instance) {
1513 test_case_failed(
"test_after_handler not run");
1522 if ((test_case_run_count & 1) == 0) {
1524 u32 *mem = (
u32 *)result_regs.ARM_sp;
1525 expected_regs = result_regs;
1526 memcpy(expected_memory, mem, expected_memory_size(mem));
1529 if (register_test_probe(&test_case_probe) < 0) {
1530 test_case_failed(
"register test_case_probe failed");
1535 if (probe_should_run == 1) {
1536 if (test_case_probe.
hit != test_instance) {
1537 test_case_failed(
"test_case_handler not run");
1540 }
else if (probe_should_run == 0) {
1541 if (test_case_probe.
hit == test_instance) {
1542 test_case_failed(
"test_case_handler ran");
1548 unregister_test_probe(&test_case_probe);
1550 if (!check_test_results())
1553 if (is_last_scenario)
1558 ++test_case_run_count;
1560 return current_code_start;
1567 test_case_cleanup();
1576 static int run_test_cases(
void (*tests)(
void),
const union decode_item *table)
1580 pr_info(
" Check decoding tables\n");
1581 ret = table_test(table);
1586 ret = coverage_start(table);
1597 static int __init run_all_tests(
void)
1601 pr_info(
"Begining kprobe tests...\n");
1603 #ifndef CONFIG_THUMB2_KERNEL
1610 pr_info(
"ARM instruction simulation\n");
1617 pr_info(
"Probe 16-bit Thumb code\n");
1618 ret = run_api_tests(thumb16_func);
1622 pr_info(
"Probe 32-bit Thumb code, even halfword\n");
1623 ret = run_api_tests(thumb32even_func);
1627 pr_info(
"Probe 32-bit Thumb code, odd halfword\n");
1628 ret = run_api_tests(thumb32odd_func);
1632 pr_info(
"16-bit Thumb instruction simulation\n");
1638 pr_info(
"32-bit Thumb instruction simulation\n");
1645 pr_info(
"Total instruction simulation tests=%d, pass=%d fail=%d\n",
1646 test_try_count, test_pass_count, test_fail_count);
1647 if (test_fail_count) {
1654 ret = run_benchmarks();
1659 #if __LINUX_ARM_ARCH__ >= 7
1661 if (coverage_fail) {
1662 pr_err(
"FAIL: Test coverage checks failed\n");
1670 pr_info(
"Finished kprobe tests OK\n");
1672 pr_err(
"kprobe tests failed\n");
1684 static void __exit kprobe_test_exit(
void)