ARM: kprobes: Add test code to kernel tree
v2 cover ARM: kprobes: Add test code to kernel tree
This is pretty much an essential tool for anyone debugging or extending the kprobes instruction simulation code and is a good regression test for kprobes on ARM.
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
Changes since v1:
- Changed KConfig configuration options to only use a single symbol.
Contents:
[PATCH v2 01/10] ARM: kprobes: Add config option for selecting the ARM kprobes tests [PATCH v2 02/10] ARM: kprobes: Add basic API tests
[PATCH v2 03/10] ARM: kprobes: Framework for instruction set test cases [PATCH v2 04/10] ARM: kprobes: Add Thumb instruction simulation test cases [PATCH v2 05/10] ARM: kprobes: Add ARM instruction simulation test cases [PATCH v2 06/10] ARM: kprobes: Add exports for test code
[PATCH v2 07/10] ARM: kprobes: Add decoding table self-consistency tests [PATCH v2 08/10] ARM: kprobes: Add decoding table test coverage analysis [PATCH v2 09/10] ARM: kprobes: Add some benchmarking to test module [PATCH v2 10/10] ARM: kprobes: Add introductory comment to test code
--- arch/arm/Kconfig.debug | 6 + arch/arm/kernel/Makefile | 7 + arch/arm/kernel/kprobes-arm.c | 4 + arch/arm/kernel/kprobes-test-arm.c | 1323 +++++++++++++++++++++++++ arch/arm/kernel/kprobes-test-thumb.c | 1187 +++++++++++++++++++++++ arch/arm/kernel/kprobes-test.c | 1756 ++++++++++++++++++++++++++++++++++ arch/arm/kernel/kprobes-test.h | 392 ++++++++ arch/arm/kernel/kprobes-thumb.c | 7 + arch/arm/kernel/kprobes.h | 8 + 9 files changed, 4690 insertions(+), 0 deletions(-)
PATCH v2 1/10 ARM: kprobes: Add config option for selecting the ARM kprobes tests
From: Jon Medhurst <[email protected]>
--- arch/arm/Kconfig.debug | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
arch/arm/Kconfig.debug +6 -0
@@ -129,4 +129,10 @@ config DEBUG_S3C_UART The uncompressor code port configuration is now handled by CONFIG_S3C_LOWLEVEL_UART_PORT. +config ARM_KPROBES_TEST + tristate "Kprobes test module" + depends on KPROBES && MODULES + help + Perform tests of kprobes API and instruction set simulation. + endmenu
Signature · 1 line
1.7.2.5
PATCH v2 2/10 ARM: kprobes: Add basic API tests
In reply to TixyFrom: Jon Medhurst <[email protected]>
These test that the different kinds of probes can be successfully placed into ARM and Thumb code and that the handlers are called correctly when this code is executed.
--- arch/arm/kernel/Makefile | 2 + arch/arm/kernel/kprobes-test.c | 340 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 342 insertions(+), 0 deletions(-)
arch/arm/kernel/Makefile +2 -0
@@ -43,6 +43,8 @@ obj-$(CONFIG_KPROBES) += kprobes-thumb.o else obj-$(CONFIG_KPROBES) += kprobes-arm.o endif +obj-$(CONFIG_ARM_KPROBES_TEST) += test-kprobes.o +test-kprobes-objs := kprobes-test.o obj-$(CONFIG_ATAGS_PROC) += atags.o obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o obj-$(CONFIG_ARM_THUMBEE) += thumbee.o
arch/arm/kernel/kprobes-test.c new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v2 3/10 ARM: kprobes: Framework for instruction set test cases
In reply to TixyFrom: Jon Medhurst <[email protected]>
On ARM we have to simulate/emulate CPU instructions in order to singlestep them. This patch adds a framework which can be used to construct test cases for different instruction forms. It is described in detail in the in-source comments of kprobes-test.c
--- arch/arm/kernel/kprobes-test.h | 384 ++++++++++++++++++ arch/arm/kernel/kprobes-test.c | 840 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1224 insertions(+), 0 deletions(-)
arch/arm/kernel/kprobes-test.h new file +384 -0
@@ -0,0 +1,384 @@ +/* +* arch/arm/kernel/kprobes-test.h +* +* 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. +*/ + +#define VERBOSE 0/* Set to '1' for more logging of test cases */ + +#ifdef CONFIG_THUMB2_KERNEL +#define NORMAL_ISA "16" +#else +#define NORMAL_ISA "32" +#endif + + +/* Flags used in kprobe_test_flags */ +#define TEST_FLAG_NO_ITBLOCK (1<<0) +#define TEST_FLAG_FULL_ITBLOCK (1<<1) +#define TEST_FLAG_NARROW_INSTR (1<<2) + +extern int kprobe_test_flags; +extern int kprobe_test_cc_position; + + +#define TEST_MEMORY_SIZE 256 + + +/* +* Test case structures. +* +* The arguments given to test cases can be one of three types. +* +* ARG_TYPE_REG +* Load a register with the given value. +* +* ARG_TYPE_PTR +* Load a register with a pointer into the stack buffer (SP + given value). +* +* ARG_TYPE_MEM +* Store the given value into the stack buffer at [SP+index]. +* +*/ + +#define ARG_TYPE_END 0 +#define ARG_TYPE_REG 1 +#define ARG_TYPE_PTR 2 +#define ARG_TYPE_MEM 3 + +#define ARG_FLAG_UNSUPPORTED 0x01 +#define ARG_FLAG_SUPPORTED 0x02 +#define ARG_FLAG_THUMB 0x10/* Must be 16 so TEST_ISA can be used */ +#define ARG_FLAG_ARM 0x20/* Must be 32 so TEST_ISA can be used */ + +struct test_arg { +u8 type;/* ARG_TYPE_x */ +u8 _padding[7 ]; +}; + +struct test_arg_regptr { +u8 type;/* ARG_TYPE_REG or ARG_TYPE_PTR */ +u8 reg; +u8 _padding[2 ]; +u32 val; +}; + +struct test_arg_mem { +u8 type;/* ARG_TYPE_MEM */ +u8 index; +u8 _padding[2 ]; +u32 val; +}; + +struct test_arg_end { +u8 type;/* ARG_TYPE_END */ +u8 flags;/* ARG_FLAG_x */ +u16 code_offset; +u16 branch_offset; +u16 end_offset; +}; + + +/* +* Building blocks for test cases. +* +* Each test case is wrapped between TESTCASE_START and TESTCASE_END. +* +* To specify arguments for a test case the TEST_ARG_{REG,PTR,MEM} macros are +* used followed by a terminating TEST_ARG_END. +* +* After this, the instruction to be tested is defined with TEST_INSTRUCTION. +* Or for branches, TEST_BRANCH_B and TEST_BRANCH_F (branch forwards/backwards). +* +* Some specific test cases may make use of other custom constructs. +*/ + +#if VERBOSE +#define verbose (fmt, ...) pr_info(fmt, ##__VA_ARGS__) +#else +#define verbose (fmt, ...) +#endif + +#define TEST_GROUP (title) \ + verbose("\n"); \ + verbose(title"\n"); \ + verbose("---------------------------------------------------------\n"); + +#define TESTCASE_START (title) \ + __asm__ __volatile__ ( \ + "bl __kprobes_test_case_start \n\t" \ +/* don't use .asciz here as 'title' may be */ \ +/* multiple strings to be concatenated. */ \ + ".ascii "#title" \n\t" \ + ".byte 0 \n\t" \ + ".align 2 \n\t" + +#define TEST_ARG_REG (reg, val) \ + ".byte "__stringify(ARG_TYPE_REG)" \n\t" \ + ".byte "#reg" \n\t" \ + ".short 0 \n\t" \ + ".word "#val" \n\t" + +#define TEST_ARG_PTR (reg, val) \ + ".byte "__stringify(ARG_TYPE_PTR)" \n\t" \ + ".byte "#reg" \n\t" \ + ".short 0 \n\t" \ + ".word "#val" \n\t" + +#define TEST_ARG_MEM (index, val) \ + ".byte "__stringify(ARG_TYPE_MEM)" \n\t" \ + ".byte "#index" \n\t" \ + ".short 0 \n\t" \ + ".word "#val" \n\t" + +#define TEST_ARG_END (flags) \ + ".byte "__stringify(ARG_TYPE_END)" \n\t" \ + ".byte "TEST_ISA flags" \n\t" \ + ".short 50f-0f \n\t" \ + ".short 2f-0f \n\t" \ + ".short 99f-0f \n\t" \ + ".code "TEST_ISA" \n\t" \ + "0: \n\t" + +#define TEST_INSTRUCTION (instruction) \ + "50: nop \n\t" \ + "1: "instruction" \n\t" \ + " nop \n\t" + +#define TEST_BRANCH_F (instruction, xtra_dist) \ + TEST_INSTRUCTION(instruction) \ + ".if "#xtra_dist" \n\t" \ + " b 99f \n\t" \ + ".space "#xtra_dist" \n\t" \ + ".endif \n\t" \ + " b 99f \n\t" \ + "2: nop \n\t" + +#define TEST_BRANCH_B (instruction, xtra_dist) \ + " b 50f \n\t" \ + " b 99f \n\t" \ + "2: nop \n\t" \ + " b 99f \n\t" \ + ".if "#xtra_dist" \n\t" \ + ".space "#xtra_dist" \n\t" \ + ".endif \n\t" \ + TEST_INSTRUCTION(instruction) + +#define TESTCASE_END \ + "2: \n\t" \ + "99: \n\t" \ + " bl __kprobes_test_case_end_"TEST_ISA" \n\t" \ + ".code "NORMAL_ISA" \n\t" \ + : : \ + : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" \ + ); + + +/* +* Macros to define test cases. +* +* Those of the form TEST_{R,P,M}* can be used to define test cases +* which take combinations of the three basic types of arguments. E.g. +* +* TEST_R One register argument +* TEST_RR Two register arguments +* TEST_RPR A register, a pointer, then a register argument +* +* For testing instructions which may branch, there are macros TEST_BF_* +* and TEST_BB_* for branching forwards and backwards. +* +* TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed, +* the just verify that a kprobe is or is not allowed on the given instruction. +*/ + +#define TEST (code) \ + TESTCASE_START(code) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code) \ + TESTCASE_END + +#define TEST_UNSUPPORTED (code) \ + TESTCASE_START(code) \ + TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED)) \ + TEST_INSTRUCTION(code) \ + TESTCASE_END + +#define TEST_SUPPORTED (code) \ + TESTCASE_START(code) \ + TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED)) \ + TEST_INSTRUCTION(code) \ + TESTCASE_END + +#define TEST_R (code1, reg, val, code2) \ + TESTCASE_START(code1 #reg code2) \ + TEST_ARG_REG(reg, val) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg code2) \ + TESTCASE_END + +#define TEST_RR (code1, reg1, val1, code2, reg2, val2, code3) \ + TESTCASE_START(code1 #reg1 code2 #reg2 code3) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \ + TESTCASE_END + +#define TEST_RRR (code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\ + TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_REG(reg3, val3) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TESTCASE_END + +#define TEST_RRRR (code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4) \ + TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_REG(reg3, val3) \ + TEST_ARG_REG(reg4, val4) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \ + TESTCASE_END + +#define TEST_P (code1, reg1, val1, code2) \ + TESTCASE_START(code1 #reg1 code2) \ + TEST_ARG_PTR(reg1, val1) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2) \ + TESTCASE_END + +#define TEST_PR (code1, reg1, val1, code2, reg2, val2, code3) \ + TESTCASE_START(code1 #reg1 code2 #reg2 code3) \ + TEST_ARG_PTR(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \ + TESTCASE_END + +#define TEST_RP (code1, reg1, val1, code2, reg2, val2, code3) \ + TESTCASE_START(code1 #reg1 code2 #reg2 code3) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_PTR(reg2, val2) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \ + TESTCASE_END + +#define TEST_PRR (code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\ + TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TEST_ARG_PTR(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_REG(reg3, val3) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TESTCASE_END + +#define TEST_RPR (code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\ + TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_PTR(reg2, val2) \ + TEST_ARG_REG(reg3, val3) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TESTCASE_END + +#define TEST_RRP (code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\ + TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_PTR(reg3, val3) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \ + TESTCASE_END + +#define TEST_BF_P (code1, reg1, val1, code2) \ + TESTCASE_START(code1 #reg1 code2) \ + TEST_ARG_PTR(reg1, val1) \ + TEST_ARG_END("") \ + TEST_BRANCH_F(code1 #reg1 code2, 0) \ + TESTCASE_END + +#define TEST_BF_X (code, xtra_dist) \ + TESTCASE_START(code) \ + TEST_ARG_END("") \ + TEST_BRANCH_F(code, xtra_dist) \ + TESTCASE_END + +#define TEST_BB_X (code, xtra_dist) \ + TESTCASE_START(code) \ + TEST_ARG_END("") \ + TEST_BRANCH_B(code, xtra_dist) \ + TESTCASE_END + +#define TEST_BF_RX (code1, reg, val, code2, xtra_dist) \ + TESTCASE_START(code1 #reg code2) \ + TEST_ARG_REG(reg, val) \ + TEST_ARG_END("") \ + TEST_BRANCH_F(code1 #reg code2, xtra_dist) \ + TESTCASE_END + +#define TEST_BB_RX (code1, reg, val, code2, xtra_dist) \ + TESTCASE_START(code1 #reg code2) \ + TEST_ARG_REG(reg, val) \ + TEST_ARG_END("") \ + TEST_BRANCH_B(code1 #reg code2, xtra_dist) \ + TESTCASE_END + +#define TEST_BF (code) TEST_BF_X(code, 0) +#define TEST_BB (code) TEST_BB_X(code, 0) + +#define TEST_BF_R (code1, reg, val, code2) TEST_BF_RX(code1, reg, val, code2, 0) +#define TEST_BB_R (code1, reg, val, code2) TEST_BB_RX(code1, reg, val, code2, 0) + +#define TEST_BF_RR (code1, reg1, val1, code2, reg2, val2, code3) \ + TESTCASE_START(code1 #reg1 code2 #reg2 code3) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_END("") \ + TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3, 0) \ + TESTCASE_END + +#define TEST_X (code, codex) \ + TESTCASE_START(code) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code) \ + " b 99f \n\t" \ + " "codex" \n\t" \ + TESTCASE_END + +#define TEST_RX (code1, reg, val, code2, codex) \ + TESTCASE_START(code1 #reg code2) \ + TEST_ARG_REG(reg, val) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 __stringify(reg) code2) \ + " b 99f \n\t" \ + " "codex" \n\t" \ + TESTCASE_END + +#define TEST_RRX (code1, reg1, val1, code2, reg2, val2, code3, codex) \ + TESTCASE_START(code1 #reg1 code2 #reg2 code3) \ + TEST_ARG_REG(reg1, val1) \ + TEST_ARG_REG(reg2, val2) \ + TEST_ARG_END("") \ + TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \ + " b 99f \n\t" \ + " "codex" \n\t" \ + TESTCASE_END + + +/* Various values used in test cases... */ +#define N (val) (val ^ 0xffffffff) +#define VAL1 0x12345678 +#define VAL2 N(VAL1) +#define VAL3 0xa5f801 +#define VAL4 N(VAL3) +#define VALM 0x456789ab +#define VALR 0xdeaddead +#define HH1 0x0123fecb +#define HH2 0xa9874567
arch/arm/kernel/kprobes-test.c +840 -0
857 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v2 3/10 ARM: kprobes: Framework for instruction set test cases
Quoted 9 lines · Tixy
From: Jon Medhurst <[email protected]>
On ARM we have to simulate/emulate CPU instructions in order to singlestep them. This patch adds a framework which can be used to construct test cases for different instruction forms. It is described in detail in the in-source comments of
kprobes-test.cSigned-off-by Jon Medhurst <[email protected]>---
[...]
Quoted 2 lines
+ * Th pair of test case runs is repeated with different combinations of
+ * flag values in CPSR and, for Thumb, different ITState.
s/Th/The/
(noticed this typo by chance I suppose ;-))
Nicolas
PATCH v2 4/10 ARM: kprobes: Add Thumb instruction simulation test cases
In reply to TixyFrom: Jon Medhurst <[email protected]>
--- arch/arm/kernel/Makefile | 3 + arch/arm/kernel/kprobes-test.h | 6 + arch/arm/kernel/kprobes-test-thumb.c | 1187 ++++++++++++++++++++++++++++++++++ arch/arm/kernel/kprobes-test.c | 25 + 4 files changed, 1221 insertions(+), 0 deletions(-)
arch/arm/kernel/Makefile +3 -0
@@ -45,6 +45,9 @@ obj-$(CONFIG_KPROBES) += kprobes-arm.o endif obj-$(CONFIG_ARM_KPROBES_TEST) += test-kprobes.o test-kprobes-objs := kprobes-test.o +ifdef CONFIG_THUMB2_KERNEL +test-kprobes-objs += kprobes-test-thumb.o +endif obj-$(CONFIG_ATAGS_PROC) += atags.o obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o obj-$(CONFIG_ARM_THUMBEE) += thumbee.o
arch/arm/kernel/kprobes-test.h +6 -0
@@ -382,3 +382,9 @@ struct test_arg_end {#define VALR 0xdeaddead#define HH1 0x0123fecb#define HH2 0xa9874567 + + +#ifdef CONFIG_THUMB2_KERNEL +void kprobe_thumb16_test_cases (void ); +void kprobe_thumb32_test_cases (void ); +#endif
arch/arm/kernel/kprobes-test-thumb.c new file +1187 -0
1,187 lines not on this page · raw message
arch/arm/kernel/kprobes-test.c +25 -0
@@ -1117,6 +1117,15 @@ end:* Top level test functions */ +static int run_test_cases (void (*tests)(void )) +{ +pr_info (" Run test cases\n" ); +tests (); + +return 0 ; +} + +static int __init run_all_tests (void ) {int ret =0 ;
@@ -1147,8 +1156,24 @@ static int __init run_all_tests(void)if (ret) goto out; +pr_info ("16-bit Thumb instruction simulation\n" ); + ret =run_test_cases (kprobe_thumb16_test_cases); +if (ret) + goto out; + +pr_info ("32-bit Thumb instruction simulation\n" ); + ret =run_test_cases (kprobe_thumb32_test_cases); +if (ret) + goto out;#endif +pr_info ("Total instruction simulation tests=%d, pass=%d fail=%d\n" , + test_try_count, test_pass_count, test_fail_count); +if (test_fail_count) { + ret = -EINVAL ; + goto out; + } + out:if (ret ==0 )pr_info ("Finished kprobe tests OK\n" );
Signature · 1 line
1.7.2.5
PATCH v2 5/10 ARM: kprobes: Add ARM instruction simulation test cases
In reply to TixyFrom: Jon Medhurst <[email protected]>
--- arch/arm/kernel/Makefile | 2 + arch/arm/kernel/kprobes-test.h | 2 + arch/arm/kernel/kprobes-test-arm.c | 1323 ++++++++++++++++++++++++++++++++++++ arch/arm/kernel/kprobes-test.c | 5 + 4 files changed, 1332 insertions(+), 0 deletions(-)
arch/arm/kernel/Makefile +2 -0
@@ -47,6 +47,8 @@ obj-$(CONFIG_ARM_KPROBES_TEST) += test-kprobes.o test-kprobes-objs := kprobes-test.o ifdef CONFIG_THUMB2_KERNEL test-kprobes-objs += kprobes-test-thumb.o +else +test-kprobes-objs += kprobes-test-arm.o endif obj-$(CONFIG_ATAGS_PROC) += atags.o obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o
arch/arm/kernel/kprobes-test.h +2 -0
@@ -387,4 +387,6 @@ struct test_arg_end {#ifdef CONFIG_THUMB2_KERNEL void kprobe_thumb16_test_cases (void );void kprobe_thumb32_test_cases (void ); +#else +void kprobe_arm_test_cases (void );#endif
arch/arm/kernel/kprobes-test-arm.c new file +1323 -0
1,323 lines not on this page · raw message
arch/arm/kernel/kprobes-test.c +5 -0
@@ -1139,6 +1139,11 @@ static int __init run_all_tests(void)if (ret) goto out; +pr_info ("ARM instruction simulation\n" ); + ret =run_test_cases (kprobe_arm_test_cases); +if (ret) + goto out; +#else /* CONFIG_THUMB2_KERNEL */ pr_info ("Probe 16-bit Thumb code\n" );
Signature · 1 line
1.7.2.5
PATCH v2 6/10 ARM: kprobes: Add exports for test code
In reply to TixyFrom: Jon Medhurst <[email protected]>
The test code will be using kprobes' internal decoding tables so we need to export these for when then the tests are compiled as a module.
--- arch/arm/kernel/kprobes.h | 8 ++++++++ arch/arm/kernel/kprobes-arm.c | 4 ++++ arch/arm/kernel/kprobes-thumb.c | 7 +++++++ 3 files changed, 19 insertions(+), 0 deletions(-)
arch/arm/kernel/kprobes.h +8 -0
@@ -413,6 +413,14 @@ struct decode_reject { DECODE_HEADER(DECODE_TYPE_REJECT, _mask, _value, 0) +#ifdef CONFIG_THUMB2_KERNEL +extern const union decode_item kprobe_decode_thumb16_table[]; +extern const union decode_item kprobe_decode_thumb32_table[]; +#else +extern const union decode_item kprobe_decode_arm_table[]; +#endif + +int kprobe_decode_insn (kprobe_opcode_t insn,struct arch_specific_insn *asi,const union decode_item *table,bool thumb16);
arch/arm/kernel/kprobes-arm.c +4 -0
@@ -60,6 +60,7 @@#include <linux/kernel.h> #include <linux/kprobes.h> +#include <linux/module.h> #include "kprobes.h"
@@ -971,6 +972,9 @@ const union decode_item kprobe_decode_arm_table[] = { DECODE_END }; +#ifdef CONFIG_ARM_KPROBES_TEST_MODULE +EXPORT_SYMBOL_GPL (kprobe_decode_arm_table); +#endif static void __kprobes arm_singlestep (struct kprobe *p,struct pt_regs *regs) {
arch/arm/kernel/kprobes-thumb.c +7 -0
@@ -10,6 +10,7 @@#include <linux/kernel.h> #include <linux/kprobes.h> +#include <linux/module.h> #include "kprobes.h"
@@ -943,6 +944,9 @@ const union decode_item kprobe_decode_thumb32_table[] = {*/ DECODE_END }; +#ifdef CONFIG_ARM_KPROBES_TEST_MODULE +EXPORT_SYMBOL_GPL (kprobe_decode_thumb32_table); +#endif static void __kprobes t16_simulate_bxblx (struct kprobe *p,struct pt_regs *regs)
@@ -1423,6 +1427,9 @@ const union decode_item kprobe_decode_thumb16_table[] = { DECODE_END }; +#ifdef CONFIG_ARM_KPROBES_TEST_MODULE +EXPORT_SYMBOL_GPL (kprobe_decode_thumb16_table); +#endif static unsigned long __kprobes thumb_check_cc (unsigned long cpsr) {
Signature · 1 line
1.7.2.5
PATCH v2 7/10 ARM: kprobes: Add decoding table self-consistency tests
In reply to TixyFrom: Jon Medhurst <[email protected]>
These check that the bitmask and match value used in the decoding tables are self consistent.
--- arch/arm/kernel/kprobes-test.c | 103 ++++++++++++++++++++++++++++++++++++++-- 1 files changed, 99 insertions(+), 4 deletions(-)
arch/arm/kernel/kprobes-test.c +99 -4
@@ -443,6 +443,92 @@ static int run_api_tests(long (*func)(long, long))/* +* 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); +} + + +/* * Framework for instruction set test cases */
@@ -1117,8 +1203,15 @@ end:* Top level test functions */ -static int run_test_cases (void (*tests)(void )) +static int run_test_cases (void (*tests)(void ),const union decode_item *table) { +int ret; + +pr_info (" Check decoding tables\n" ); + ret =table_test (table); +if (ret) +return ret; +pr_info (" Run test cases\n" );tests ();
@@ -1140,7 +1233,7 @@ static int __init run_all_tests(void) goto out;pr_info ("ARM instruction simulation\n" ); - ret =run_test_cases (kprobe_arm_test_cases); + ret =run_test_cases (kprobe_arm_test_cases, kprobe_decode_arm_table);if (ret) goto out;
@@ -1162,12 +1255,14 @@ static int __init run_all_tests(void) goto out;pr_info ("16-bit Thumb instruction simulation\n" ); - ret =run_test_cases (kprobe_thumb16_test_cases); + ret =run_test_cases (kprobe_thumb16_test_cases, + kprobe_decode_thumb16_table);if (ret) goto out;pr_info ("32-bit Thumb instruction simulation\n" ); - ret =run_test_cases (kprobe_thumb32_test_cases); + ret =run_test_cases (kprobe_thumb32_test_cases, + kprobe_decode_thumb32_table);if (ret) goto out;#endif
Signature · 1 line
1.7.2.5
PATCH v2 8/10 ARM: kprobes: Add decoding table test coverage analysis
In reply to TixyFrom: Jon Medhurst <[email protected]>
This is used to verify that all combinations of CPU instructions described by the kprobes decoding tables have a test case.
--- arch/arm/kernel/kprobes-test.c | 261 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 261 insertions(+), 0 deletions(-)
arch/arm/kernel/kprobes-test.c +261 -0
@@ -178,6 +178,7 @@#include <linux/kernel.h> #include <linux/module.h> +#include <linux/slab.h> #include <linux/kprobes.h> #include "kprobes.h"
@@ -529,6 +530,250 @@ static int table_test(const union decode_item *table)/* +* 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. +*/ + +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); +} + + +/* * Framework for instruction set test cases */
@@ -1039,6 +1284,8 @@ static uintptr_t __used kprobes_test_case_start(const char *title, void *stack) } } +coverage_add (current_instruction); +if (end_arg->flags &ARG_FLAG_UNSUPPORTED ) {if (register_test_probe (&test_case_probe) <0 ) goto pass;
@@ -1213,8 +1460,13 @@ static int run_test_cases(void (*tests)(void), const union decode_item *table)return ret;pr_info (" Run test cases\n" ); + ret =coverage_start (table); +if (ret) +return ret; +tests (); +coverage_end ();return 0 ; }
@@ -1274,6 +1526,15 @@ static int __init run_all_tests(void) goto out; } +#if __LINUX_ARM_ARCH__ >=7 +/* We are able to run all test cases so coverage should be complete */ +if (coverage_fail) { +pr_err ("FAIL: Test coverage checks failed\n" ); + ret = -EINVAL ; + goto out; + } +#endif + out:if (ret ==0 )pr_info ("Finished kprobe tests OK\n" );
Signature · 1 line
1.7.2.5
PATCH v2 9/10 ARM: kprobes: Add some benchmarking to test module
In reply to TixyFrom: Jon Medhurst <[email protected]>
These benchmarks show the basic speed of kprobes and verify the success of optimisations done to the emulation of typical function entry instructions (i.e. push/stmdb).
--- arch/arm/kernel/kprobes-test.c | 169 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 169 insertions(+), 0 deletions(-)
arch/arm/kernel/kprobes-test.c +169 -0
@@ -185,6 +185,9 @@#include "kprobes-test.h" +#define BENCHMARKING 1 + +/* * Test basic API */
@@ -444,6 +447,165 @@ static int run_api_tests(long (*func)(long, long))/* +* 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 )) +{ +int t, n; +for (n =1000 ; ; n *=2 ) { +struct timeval before; +struct timeval after; +int i; + +do_gettimeofday (&before); + +for (i = n; i >0 ; --i) +fn (); + +do_gettimeofday (&after); + t = after.tv_usec - before.tv_usec; +if (t <0 ) + t +=1000000 ;/* Adjust time if it wrapped */ +if (t >=250000 ) +break ;/* Stop once we took more than 0.25 seconds */ + } +return t / (n /1000 );/* Return time in nano-seconds */ +}; + +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 */ + + +/* * Decoding table self-consistency tests */
@@ -1526,6 +1688,13 @@ static int __init run_all_tests(void) goto out; } +#if BENCHMARKING +pr_info ("Benchmarks\n" ); + ret =run_benchmarks (); +if (ret) + goto out; +#endif +#if __LINUX_ARM_ARCH__ >=7 /* We are able to run all test cases so coverage should be complete */ if (coverage_fail) {
Signature · 1 line
1.7.2.5
PATCH v2 9/10 ARM: kprobes: Add some benchmarking to test module
Quoted 8 lines · Tixy
From: Jon Medhurst <[email protected]>
These benchmarks show the basic speed of kprobes and verify the success of optimisations done to the emulation of typical function entry instructions (i.e. push/stmdb).
Signed-off-by Jon Medhurst <[email protected]>---
[...]
arch/arm/kernel/kprobes-test.c static int run_api_tests(long (*func)(long, long)) · line 467
Quoted 5 lines
arch/arm/kernel/kprobes-test.c +5
-0+#ifdef CONFIG_THUMB2_KERNEL +#define wide ".w" +#else +#define wide +#endif
I suppose you can't reuse the W() macro from arch/arm/include/asm/unified.h here...
Quoted 12 lines
arch/arm/kernel/kprobes-test.c +12
-0+static int benchmark (void (*fn)(void )) +{ +int t, n; +for (n =1000 ; ; n *=2 ) { +struct timeval before; +struct timeval after; +int i; + +do_gettimeofday (&before); + +for (i = n; i >0 ; --i) +fn ();
arch/arm/kernel/kprobes-test.c static int run_api_tests(long (*func)(long, long)) · line 541
Quoted 10 lines
arch/arm/kernel/kprobes-test.c +10
-0+ +do_gettimeofday (&after); + t = after.tv_usec - before.tv_usec; +if (t <0 ) + t +=1000000 ;/* Adjust time if it wrapped */ +if (t >=250000 ) +break ;/* Stop once we took more than 0.25 seconds */ + } +return t / (n /1000 );/* Return time in nano-seconds */ +};
I think that using sched_clock() here might be a better choice than do_gettimeofday(). It is certainly much lower overhead and easier to use.
Nicolas
PATCH v2 9/10 ARM: kprobes: Add some benchmarking to test module
Quoted 40 lines · Nicolas Pitre
Quoted 8 lines · Tixy
From: Jon Medhurst <[email protected]>
These benchmarks show the basic speed of kprobes and verify the success of optimisations done to the emulation of typical function entry instructions (i.e. push/stmdb).
Signed-off-by Jon Medhurst <[email protected]>---
[...]
arch/arm/kernel/kprobes-test.c static int run_api_tests(long (*func)(long, long)) · line 467
Quoted 5 lines
arch/arm/kernel/kprobes-test.c +5
-0+#ifdef CONFIG_THUMB2_KERNEL +#define wide ".w" +#else +#define wide +#endif I suppose you can't reuse the
W()macro fromarch/arm/include/asm/unified.hhere...Quoted 18 lines
arch/arm/kernel/kprobes-test.c +18
-0+static int benchmark (void (*fn)(void )) +{ +int t, n; +for (n =1000 ; ; n *=2 ) { +struct timeval before; +struct timeval after; +int i; + +do_gettimeofday (&before); + +for (i = n; i >0 ; --i) +fn (); + +do_gettimeofday (&after); + t = after.tv_usec - before.tv_usec; +if (t <0 ) + t +=1000000 ;/* Adjust time if it wrapped */ +if (t >=250000 )
Quoted 8 lines
arch/arm/kernel/kprobes-test.c static int run_api_tests(long (*func)(long, long)) · line 541
Quoted 4 lines
arch/arm/kernel/kprobes-test.c +4
-0+break ;/* Stop once we took more than 0.25 seconds */ + } +return t / (n /1000 );/* Return time in nano-seconds */ +};I think that using
sched_clock()here might be a better choice thando_gettimeofday(). It is certainly much lower overhead and easier to use.
When googling for sched_clock I came across a five year old posting [1] by Russell saying that uses of sched_clock() for timing intervals is broken because the range of values it gives doesn't fill all the bits in the returned integer, leading to a discontinuity when the value wraps.
However, looking at the current implementations of sched_clock() they seems to have full 64-bit range, and git history shows a new common infrastructure for sched_clock being added at the end of last year. So is the full 64-bit range now defined for the sched_clock on API on ARM?
Signature · 3 lines
Tixy [1] http://lkml.indiana.edu/hypermail/linux/kernel/0605.0/0425.html
PATCH v2 9/10 ARM: kprobes: Add some benchmarking to test module
Quoted 4 lines · Tixy
However, looking at the current implementations of
sched_clock()they seems to have full 64-bit range, and git history shows a new common infrastructure forsched_clockbeing added at the end of last year. So is the full 64-bit range now defined for thesched_clockon API on ARM?
Yes - the scheduler requires the full 64-bit range from sched_clock() so all implementations we had needed to be fixed. Anything which doesn't give the full 64-bit range is now considered buggy.
PATCH v3 1/1 ARM: kprobes: Add some benchmarking to test module
In reply to Nicolas PitreFrom: Jon Medhurst <[email protected]>
These benchmarks show the basic speed of kprobes and verify the success of optimisations done to the emulation of typical function entry instructions (i.e. push/stmdb).
---
Changes since v2:
- Modify benchmark() to use sched_clock() rather than do_gettimeofday() as this has lower overheads and gives simpler code - as suggested by Nicolas Pitre.
--- arch/arm/kernel/kprobes-test.c | 161 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 161 insertions(+), 0 deletions(-)
arch/arm/kernel/kprobes-test.c +161 -0
@@ -185,6 +185,9 @@#include "kprobes-test.h" +#define BENCHMARKING 1 + +/* * Test basic API */
@@ -444,6 +447,157 @@ static int run_api_tests(long (*func)(long, long))/* +* 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 */ + + +/* * Decoding table self-consistency tests */
@@ -1526,6 +1680,13 @@ static int __init run_all_tests(void) goto out; } +#if BENCHMARKING +pr_info ("Benchmarks\n" ); + ret =run_benchmarks (); +if (ret) + goto out; +#endif +#if __LINUX_ARM_ARCH__ >=7 /* We are able to run all test cases so coverage should be complete */ if (coverage_fail) {
Signature · 1 line
1.7.2.5
PATCH v2 10/10 ARM: kprobes: Add introductory comment to test code
In reply to TixyFrom: Jon Medhurst <[email protected]>
--- arch/arm/kernel/kprobes-test.c | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-)
arch/arm/kernel/kprobes-test.c +21 -0
@@ -9,6 +9,27 @@*/ /* +* 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 * ------------------- *
Signature · 1 line
1.7.2.5
v2 cover ARM: kprobes: Add test code to kernel tree
In reply to TixyQuoted 3 lines · Tixy
This is pretty much an essential tool for anyone debugging or extending the kprobes instruction simulation code and is a good regression test for kprobes on ARM.
This is amazing work. Well done.
I've posted minor comments on some of the patches already. You may
still add to all of them:
Nicolas