Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * arch/arm/probes/kprobes/test-core.h
0004  *
0005  * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
0006  */
0007 
0008 #define VERBOSE 0 /* Set to '1' for more logging of test cases */
0009 
0010 #ifdef CONFIG_THUMB2_KERNEL
0011 #define NORMAL_ISA "16"
0012 #else
0013 #define NORMAL_ISA "32"
0014 #endif
0015 
0016 
0017 /* Flags used in kprobe_test_flags */
0018 #define TEST_FLAG_NO_ITBLOCK    (1<<0)
0019 #define TEST_FLAG_FULL_ITBLOCK  (1<<1)
0020 #define TEST_FLAG_NARROW_INSTR  (1<<2)
0021 
0022 extern int kprobe_test_flags;
0023 extern int kprobe_test_cc_position;
0024 
0025 
0026 #define TEST_MEMORY_SIZE 256
0027 
0028 
0029 /*
0030  * Test case structures.
0031  *
0032  * The arguments given to test cases can be one of three types.
0033  *
0034  *   ARG_TYPE_REG
0035  *  Load a register with the given value.
0036  *
0037  *   ARG_TYPE_PTR
0038  *  Load a register with a pointer into the stack buffer (SP + given value).
0039  *
0040  *   ARG_TYPE_MEM
0041  *  Store the given value into the stack buffer at [SP+index].
0042  *
0043  */
0044 
0045 #define ARG_TYPE_END        0
0046 #define ARG_TYPE_REG        1
0047 #define ARG_TYPE_PTR        2
0048 #define ARG_TYPE_MEM        3
0049 #define ARG_TYPE_REG_MASKED 4
0050 
0051 #define ARG_FLAG_UNSUPPORTED    0x01
0052 #define ARG_FLAG_SUPPORTED  0x02
0053 #define ARG_FLAG_THUMB      0x10    /* Must be 16 so TEST_ISA can be used */
0054 #define ARG_FLAG_ARM        0x20    /* Must be 32 so TEST_ISA can be used */
0055 
0056 struct test_arg {
0057     u8  type;       /* ARG_TYPE_x */
0058     u8  _padding[7];
0059 };
0060 
0061 struct test_arg_regptr {
0062     u8  type;       /* ARG_TYPE_REG or ARG_TYPE_PTR or ARG_TYPE_REG_MASKED */
0063     u8  reg;
0064     u8  _padding[2];
0065     u32 val;
0066 };
0067 
0068 struct test_arg_mem {
0069     u8  type;       /* ARG_TYPE_MEM */
0070     u8  index;
0071     u8  _padding[2];
0072     u32 val;
0073 };
0074 
0075 struct test_arg_end {
0076     u8  type;       /* ARG_TYPE_END */
0077     u8  flags;      /* ARG_FLAG_x */
0078     u16 code_offset;
0079     u16 branch_offset;
0080     u16 end_offset;
0081 };
0082 
0083 
0084 /*
0085  * Building blocks for test cases.
0086  *
0087  * Each test case is wrapped between TESTCASE_START and TESTCASE_END.
0088  *
0089  * To specify arguments for a test case the TEST_ARG_{REG,PTR,MEM} macros are
0090  * used followed by a terminating TEST_ARG_END.
0091  *
0092  * After this, the instruction to be tested is defined with TEST_INSTRUCTION.
0093  * Or for branches, TEST_BRANCH_B and TEST_BRANCH_F (branch forwards/backwards).
0094  *
0095  * Some specific test cases may make use of other custom constructs.
0096  */
0097 
0098 #if VERBOSE
0099 #define verbose(fmt, ...) pr_info(fmt, ##__VA_ARGS__)
0100 #else
0101 #define verbose(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
0102 #endif
0103 
0104 #define TEST_GROUP(title)                   \
0105     verbose("\n");                      \
0106     verbose(title"\n");                 \
0107     verbose("---------------------------------------------------------\n");
0108 
0109 #define TESTCASE_START(title)                   \
0110     __asm__ __volatile__ (                  \
0111     ".syntax unified                \n\t"   \
0112     "bl __kprobes_test_case_start       \n\t"   \
0113     ".pushsection .rodata               \n\t"   \
0114     "10:                        \n\t"   \
0115     /* don't use .asciz here as 'title' may be */       \
0116     /* multiple strings to be concatenated.  */     \
0117     ".ascii "#title"                \n\t"   \
0118     ".byte  0                   \n\t"   \
0119     ".popsection                    \n\t"   \
0120     ".word  10b                 \n\t"
0121 
0122 #define TEST_ARG_REG(reg, val)                  \
0123     ".byte  "__stringify(ARG_TYPE_REG)"     \n\t"   \
0124     ".byte  "#reg"                  \n\t"   \
0125     ".short 0                   \n\t"   \
0126     ".word  "#val"                  \n\t"
0127 
0128 #define TEST_ARG_PTR(reg, val)                  \
0129     ".byte  "__stringify(ARG_TYPE_PTR)"     \n\t"   \
0130     ".byte  "#reg"                  \n\t"   \
0131     ".short 0                   \n\t"   \
0132     ".word  "#val"                  \n\t"
0133 
0134 #define TEST_ARG_MEM(index, val)                \
0135     ".byte  "__stringify(ARG_TYPE_MEM)"     \n\t"   \
0136     ".byte  "#index"                \n\t"   \
0137     ".short 0                   \n\t"   \
0138     ".word  "#val"                  \n\t"
0139 
0140 #define TEST_ARG_REG_MASKED(reg, val)               \
0141     ".byte  "__stringify(ARG_TYPE_REG_MASKED)"  \n\t"   \
0142     ".byte  "#reg"                  \n\t"   \
0143     ".short 0                   \n\t"   \
0144     ".word  "#val"                  \n\t"
0145 
0146 #define TEST_ARG_END(flags)                 \
0147     ".byte  "__stringify(ARG_TYPE_END)"     \n\t"   \
0148     ".byte  "TEST_ISA flags"            \n\t"   \
0149     ".short 50f-0f                  \n\t"   \
0150     ".short 2f-0f                   \n\t"   \
0151     ".short 99f-0f                  \n\t"   \
0152     ".code "TEST_ISA"               \n\t"   \
0153     "0:                     \n\t"
0154 
0155 #define TEST_INSTRUCTION(instruction)               \
0156     "50:    nop                 \n\t"   \
0157     "1: "instruction"               \n\t"   \
0158     "   nop                 \n\t"
0159 
0160 #define TEST_BRANCH_F(instruction)              \
0161     TEST_INSTRUCTION(instruction)               \
0162     "   b   99f             \n\t"   \
0163     "2: nop                 \n\t"
0164 
0165 #define TEST_BRANCH_B(instruction)              \
0166     "   b   50f             \n\t"   \
0167     "   b   99f             \n\t"   \
0168     "2: nop                 \n\t"   \
0169     "   b   99f             \n\t"   \
0170     TEST_INSTRUCTION(instruction)
0171 
0172 #define TEST_BRANCH_FX(instruction, codex)          \
0173     TEST_INSTRUCTION(instruction)               \
0174     "   b   99f             \n\t"   \
0175     codex"                      \n\t"   \
0176     "   b   99f             \n\t"   \
0177     "2: nop                 \n\t"
0178 
0179 #define TEST_BRANCH_BX(instruction, codex)          \
0180     "   b   50f             \n\t"   \
0181     "   b   99f             \n\t"   \
0182     "2: nop                 \n\t"   \
0183     "   b   99f             \n\t"   \
0184     codex"                      \n\t"   \
0185     TEST_INSTRUCTION(instruction)
0186 
0187 #define TESTCASE_END                        \
0188     "2:                     \n\t"   \
0189     "99:                        \n\t"   \
0190     "   bl __kprobes_test_case_end_"TEST_ISA"   \n\t"   \
0191     ".code "NORMAL_ISA"             \n\t"   \
0192     : :                         \
0193     : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"    \
0194     );
0195 
0196 
0197 /*
0198  * Macros to define test cases.
0199  *
0200  * Those of the form TEST_{R,P,M}* can be used to define test cases
0201  * which take combinations of the three basic types of arguments. E.g.
0202  *
0203  *   TEST_R One register argument
0204  *   TEST_RR    Two register arguments
0205  *   TEST_RPR   A register, a pointer, then a register argument
0206  *
0207  * For testing instructions which may branch, there are macros TEST_BF_*
0208  * and TEST_BB_* for branching forwards and backwards.
0209  *
0210  * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
0211  * the just verify that a kprobe is or is not allowed on the given instruction.
0212  */
0213 
0214 #define TEST(code)              \
0215     TESTCASE_START(code)            \
0216     TEST_ARG_END("")            \
0217     TEST_INSTRUCTION(code)          \
0218     TESTCASE_END
0219 
0220 #define TEST_UNSUPPORTED(code)                  \
0221     TESTCASE_START(code)                    \
0222     TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED))  \
0223     TEST_INSTRUCTION(code)                  \
0224     TESTCASE_END
0225 
0226 #define TEST_SUPPORTED(code)                    \
0227     TESTCASE_START(code)                    \
0228     TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED))    \
0229     TEST_INSTRUCTION(code)                  \
0230     TESTCASE_END
0231 
0232 #define TEST_R(code1, reg, val, code2)          \
0233     TESTCASE_START(code1 #reg code2)        \
0234     TEST_ARG_REG(reg, val)              \
0235     TEST_ARG_END("")                \
0236     TEST_INSTRUCTION(code1 #reg code2)      \
0237     TESTCASE_END
0238 
0239 #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3)    \
0240     TESTCASE_START(code1 #reg1 code2 #reg2 code3)       \
0241     TEST_ARG_REG(reg1, val1)                \
0242     TEST_ARG_REG(reg2, val2)                \
0243     TEST_ARG_END("")                    \
0244     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)     \
0245     TESTCASE_END
0246 
0247 #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
0248     TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)       \
0249     TEST_ARG_REG(reg1, val1)                        \
0250     TEST_ARG_REG(reg2, val2)                        \
0251     TEST_ARG_REG(reg3, val3)                        \
0252     TEST_ARG_END("")                            \
0253     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)     \
0254     TESTCASE_END
0255 
0256 #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4)   \
0257     TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4)     \
0258     TEST_ARG_REG(reg1, val1)                        \
0259     TEST_ARG_REG(reg2, val2)                        \
0260     TEST_ARG_REG(reg3, val3)                        \
0261     TEST_ARG_REG(reg4, val4)                        \
0262     TEST_ARG_END("")                            \
0263     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4)   \
0264     TESTCASE_END
0265 
0266 #define TEST_P(code1, reg1, val1, code2)    \
0267     TESTCASE_START(code1 #reg1 code2)   \
0268     TEST_ARG_PTR(reg1, val1)        \
0269     TEST_ARG_END("")            \
0270     TEST_INSTRUCTION(code1 #reg1 code2) \
0271     TESTCASE_END
0272 
0273 #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3)    \
0274     TESTCASE_START(code1 #reg1 code2 #reg2 code3)       \
0275     TEST_ARG_PTR(reg1, val1)                \
0276     TEST_ARG_REG(reg2, val2)                \
0277     TEST_ARG_END("")                    \
0278     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)     \
0279     TESTCASE_END
0280 
0281 #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3)    \
0282     TESTCASE_START(code1 #reg1 code2 #reg2 code3)       \
0283     TEST_ARG_REG(reg1, val1)                \
0284     TEST_ARG_PTR(reg2, val2)                \
0285     TEST_ARG_END("")                    \
0286     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)     \
0287     TESTCASE_END
0288 
0289 #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
0290     TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)       \
0291     TEST_ARG_PTR(reg1, val1)                        \
0292     TEST_ARG_REG(reg2, val2)                        \
0293     TEST_ARG_REG(reg3, val3)                        \
0294     TEST_ARG_END("")                            \
0295     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)     \
0296     TESTCASE_END
0297 
0298 #define TEST_RPR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
0299     TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)       \
0300     TEST_ARG_REG(reg1, val1)                        \
0301     TEST_ARG_PTR(reg2, val2)                        \
0302     TEST_ARG_REG(reg3, val3)                        \
0303     TEST_ARG_END("")                            \
0304     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)     \
0305     TESTCASE_END
0306 
0307 #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
0308     TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)       \
0309     TEST_ARG_REG(reg1, val1)                        \
0310     TEST_ARG_REG(reg2, val2)                        \
0311     TEST_ARG_PTR(reg3, val3)                        \
0312     TEST_ARG_END("")                            \
0313     TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)     \
0314     TESTCASE_END
0315 
0316 #define TEST_BF_P(code1, reg1, val1, code2) \
0317     TESTCASE_START(code1 #reg1 code2)   \
0318     TEST_ARG_PTR(reg1, val1)        \
0319     TEST_ARG_END("")            \
0320     TEST_BRANCH_F(code1 #reg1 code2)    \
0321     TESTCASE_END
0322 
0323 #define TEST_BF(code)               \
0324     TESTCASE_START(code)            \
0325     TEST_ARG_END("")            \
0326     TEST_BRANCH_F(code)         \
0327     TESTCASE_END
0328 
0329 #define TEST_BB(code)               \
0330     TESTCASE_START(code)            \
0331     TEST_ARG_END("")            \
0332     TEST_BRANCH_B(code)         \
0333     TESTCASE_END
0334 
0335 #define TEST_BF_R(code1, reg, val, code2)   \
0336     TESTCASE_START(code1 #reg code2)    \
0337     TEST_ARG_REG(reg, val)          \
0338     TEST_ARG_END("")            \
0339     TEST_BRANCH_F(code1 #reg code2)     \
0340     TESTCASE_END
0341 
0342 #define TEST_BB_R(code1, reg, val, code2)   \
0343     TESTCASE_START(code1 #reg code2)    \
0344     TEST_ARG_REG(reg, val)          \
0345     TEST_ARG_END("")            \
0346     TEST_BRANCH_B(code1 #reg code2)     \
0347     TESTCASE_END
0348 
0349 #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3) \
0350     TESTCASE_START(code1 #reg1 code2 #reg2 code3)       \
0351     TEST_ARG_REG(reg1, val1)                \
0352     TEST_ARG_REG(reg2, val2)                \
0353     TEST_ARG_END("")                    \
0354     TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3)        \
0355     TESTCASE_END
0356 
0357 #define TEST_BF_X(code, codex)          \
0358     TESTCASE_START(code)            \
0359     TEST_ARG_END("")            \
0360     TEST_BRANCH_FX(code, codex)     \
0361     TESTCASE_END
0362 
0363 #define TEST_BB_X(code, codex)          \
0364     TESTCASE_START(code)            \
0365     TEST_ARG_END("")            \
0366     TEST_BRANCH_BX(code, codex)     \
0367     TESTCASE_END
0368 
0369 #define TEST_BF_RX(code1, reg, val, code2, codex)   \
0370     TESTCASE_START(code1 #reg code2)        \
0371     TEST_ARG_REG(reg, val)              \
0372     TEST_ARG_END("")                \
0373     TEST_BRANCH_FX(code1 #reg code2, codex)     \
0374     TESTCASE_END
0375 
0376 #define TEST_X(code, codex)         \
0377     TESTCASE_START(code)            \
0378     TEST_ARG_END("")            \
0379     TEST_INSTRUCTION(code)          \
0380     "   b   99f     \n\t"   \
0381     "   "codex"         \n\t"   \
0382     TESTCASE_END
0383 
0384 #define TEST_RX(code1, reg, val, code2, codex)      \
0385     TESTCASE_START(code1 #reg code2)        \
0386     TEST_ARG_REG(reg, val)              \
0387     TEST_ARG_END("")                \
0388     TEST_INSTRUCTION(code1 __stringify(reg) code2)  \
0389     "   b   99f     \n\t"       \
0390     "   "codex"         \n\t"       \
0391     TESTCASE_END
0392 
0393 #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex)        \
0394     TESTCASE_START(code1 #reg1 code2 #reg2 code3)               \
0395     TEST_ARG_REG(reg1, val1)                        \
0396     TEST_ARG_REG(reg2, val2)                        \
0397     TEST_ARG_END("")                            \
0398     TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \
0399     "   b   99f     \n\t"                   \
0400     "   "codex"         \n\t"                   \
0401     TESTCASE_END
0402 
0403 #define TEST_RMASKED(code1, reg, mask, code2)       \
0404     TESTCASE_START(code1 #reg code2)        \
0405     TEST_ARG_REG_MASKED(reg, mask)          \
0406     TEST_ARG_END("")                \
0407     TEST_INSTRUCTION(code1 #reg code2)      \
0408     TESTCASE_END
0409 
0410 /*
0411  * We ignore the state of the imprecise abort disable flag (CPSR.A) because this
0412  * can change randomly as the kernel doesn't take care to preserve or initialise
0413  * this across context switches. Also, with Security Extensions, the flag may
0414  * not be under control of the kernel; for this reason we ignore the state of
0415  * the FIQ disable flag CPSR.F as well.
0416  */
0417 #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
0418 
0419 
0420 /*
0421  * Macros for defining space directives spread over multiple lines.
0422  * These are required so the compiler guesses better the length of inline asm
0423  * code and will spill the literal pool early enough to avoid generating PC
0424  * relative loads with out of range offsets.
0425  */
0426 #define TWICE(x)    x x
0427 #define SPACE_0x8   TWICE(".space 4\n\t")
0428 #define SPACE_0x10  TWICE(SPACE_0x8)
0429 #define SPACE_0x20  TWICE(SPACE_0x10)
0430 #define SPACE_0x40  TWICE(SPACE_0x20)
0431 #define SPACE_0x80  TWICE(SPACE_0x40)
0432 #define SPACE_0x100 TWICE(SPACE_0x80)
0433 #define SPACE_0x200 TWICE(SPACE_0x100)
0434 #define SPACE_0x400 TWICE(SPACE_0x200)
0435 #define SPACE_0x800 TWICE(SPACE_0x400)
0436 #define SPACE_0x1000    TWICE(SPACE_0x800)
0437 
0438 
0439 /* Various values used in test cases... */
0440 #define N(val)  (val ^ 0xffffffff)
0441 #define VAL1    0x12345678
0442 #define VAL2    N(VAL1)
0443 #define VAL3    0xa5f801
0444 #define VAL4    N(VAL3)
0445 #define VALM    0x456789ab
0446 #define VALR    0xdeaddead
0447 #define HH1 0x0123fecb
0448 #define HH2 0xa9874567
0449 
0450 
0451 #ifdef CONFIG_THUMB2_KERNEL
0452 void kprobe_thumb16_test_cases(void);
0453 void kprobe_thumb32_test_cases(void);
0454 #else
0455 void kprobe_arm_test_cases(void);
0456 #endif