root/arch/arm/probes/kprobes/test-core.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /* SPDX-License-Identifier: GPL-2.0-only */
   2 /*
   3  * arch/arm/probes/kprobes/test-core.h
   4  *
   5  * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
   6  */
   7 
   8 #define VERBOSE 0 /* Set to '1' for more logging of test cases */
   9 
  10 #ifdef CONFIG_THUMB2_KERNEL
  11 #define NORMAL_ISA "16"
  12 #else
  13 #define NORMAL_ISA "32"
  14 #endif
  15 
  16 
  17 /* Flags used in kprobe_test_flags */
  18 #define TEST_FLAG_NO_ITBLOCK    (1<<0)
  19 #define TEST_FLAG_FULL_ITBLOCK  (1<<1)
  20 #define TEST_FLAG_NARROW_INSTR  (1<<2)
  21 
  22 extern int kprobe_test_flags;
  23 extern int kprobe_test_cc_position;
  24 
  25 
  26 #define TEST_MEMORY_SIZE 256
  27 
  28 
  29 /*
  30  * Test case structures.
  31  *
  32  * The arguments given to test cases can be one of three types.
  33  *
  34  *   ARG_TYPE_REG
  35  *      Load a register with the given value.
  36  *
  37  *   ARG_TYPE_PTR
  38  *      Load a register with a pointer into the stack buffer (SP + given value).
  39  *
  40  *   ARG_TYPE_MEM
  41  *      Store the given value into the stack buffer at [SP+index].
  42  *
  43  */
  44 
  45 #define ARG_TYPE_END            0
  46 #define ARG_TYPE_REG            1
  47 #define ARG_TYPE_PTR            2
  48 #define ARG_TYPE_MEM            3
  49 #define ARG_TYPE_REG_MASKED     4
  50 
  51 #define ARG_FLAG_UNSUPPORTED    0x01
  52 #define ARG_FLAG_SUPPORTED      0x02
  53 #define ARG_FLAG_THUMB          0x10    /* Must be 16 so TEST_ISA can be used */
  54 #define ARG_FLAG_ARM            0x20    /* Must be 32 so TEST_ISA can be used */
  55 
  56 struct test_arg {
  57         u8      type;           /* ARG_TYPE_x */
  58         u8      _padding[7];
  59 };
  60 
  61 struct test_arg_regptr {
  62         u8      type;           /* ARG_TYPE_REG or ARG_TYPE_PTR or ARG_TYPE_REG_MASKED */
  63         u8      reg;
  64         u8      _padding[2];
  65         u32     val;
  66 };
  67 
  68 struct test_arg_mem {
  69         u8      type;           /* ARG_TYPE_MEM */
  70         u8      index;
  71         u8      _padding[2];
  72         u32     val;
  73 };
  74 
  75 struct test_arg_end {
  76         u8      type;           /* ARG_TYPE_END */
  77         u8      flags;          /* ARG_FLAG_x */
  78         u16     code_offset;
  79         u16     branch_offset;
  80         u16     end_offset;
  81 };
  82 
  83 
  84 /*
  85  * Building blocks for test cases.
  86  *
  87  * Each test case is wrapped between TESTCASE_START and TESTCASE_END.
  88  *
  89  * To specify arguments for a test case the TEST_ARG_{REG,PTR,MEM} macros are
  90  * used followed by a terminating TEST_ARG_END.
  91  *
  92  * After this, the instruction to be tested is defined with TEST_INSTRUCTION.
  93  * Or for branches, TEST_BRANCH_B and TEST_BRANCH_F (branch forwards/backwards).
  94  *
  95  * Some specific test cases may make use of other custom constructs.
  96  */
  97 
  98 #if VERBOSE
  99 #define verbose(fmt, ...) pr_info(fmt, ##__VA_ARGS__)
 100 #else
 101 #define verbose(fmt, ...)
 102 #endif
 103 
 104 #define TEST_GROUP(title)                                       \
 105         verbose("\n");                                          \
 106         verbose(title"\n");                                     \
 107         verbose("---------------------------------------------------------\n");
 108 
 109 #define TESTCASE_START(title)                                   \
 110         __asm__ __volatile__ (                                  \
 111         "bl     __kprobes_test_case_start               \n\t"   \
 112         ".pushsection .rodata                           \n\t"   \
 113         "10:                                            \n\t"   \
 114         /* don't use .asciz here as 'title' may be */           \
 115         /* multiple strings to be concatenated.  */             \
 116         ".ascii "#title"                                \n\t"   \
 117         ".byte  0                                       \n\t"   \
 118         ".popsection                                    \n\t"   \
 119         ".word  10b                                     \n\t"
 120 
 121 #define TEST_ARG_REG(reg, val)                                  \
 122         ".byte  "__stringify(ARG_TYPE_REG)"             \n\t"   \
 123         ".byte  "#reg"                                  \n\t"   \
 124         ".short 0                                       \n\t"   \
 125         ".word  "#val"                                  \n\t"
 126 
 127 #define TEST_ARG_PTR(reg, val)                                  \
 128         ".byte  "__stringify(ARG_TYPE_PTR)"             \n\t"   \
 129         ".byte  "#reg"                                  \n\t"   \
 130         ".short 0                                       \n\t"   \
 131         ".word  "#val"                                  \n\t"
 132 
 133 #define TEST_ARG_MEM(index, val)                                \
 134         ".byte  "__stringify(ARG_TYPE_MEM)"             \n\t"   \
 135         ".byte  "#index"                                \n\t"   \
 136         ".short 0                                       \n\t"   \
 137         ".word  "#val"                                  \n\t"
 138 
 139 #define TEST_ARG_REG_MASKED(reg, val)                           \
 140         ".byte  "__stringify(ARG_TYPE_REG_MASKED)"      \n\t"   \
 141         ".byte  "#reg"                                  \n\t"   \
 142         ".short 0                                       \n\t"   \
 143         ".word  "#val"                                  \n\t"
 144 
 145 #define TEST_ARG_END(flags)                                     \
 146         ".byte  "__stringify(ARG_TYPE_END)"             \n\t"   \
 147         ".byte  "TEST_ISA flags"                        \n\t"   \
 148         ".short 50f-0f                                  \n\t"   \
 149         ".short 2f-0f                                   \n\t"   \
 150         ".short 99f-0f                                  \n\t"   \
 151         ".code "TEST_ISA"                               \n\t"   \
 152         "0:                                             \n\t"
 153 
 154 #define TEST_INSTRUCTION(instruction)                           \
 155         "50:    nop                                     \n\t"   \
 156         "1:     "instruction"                           \n\t"   \
 157         "       nop                                     \n\t"
 158 
 159 #define TEST_BRANCH_F(instruction)                              \
 160         TEST_INSTRUCTION(instruction)                           \
 161         "       b       99f                             \n\t"   \
 162         "2:     nop                                     \n\t"
 163 
 164 #define TEST_BRANCH_B(instruction)                              \
 165         "       b       50f                             \n\t"   \
 166         "       b       99f                             \n\t"   \
 167         "2:     nop                                     \n\t"   \
 168         "       b       99f                             \n\t"   \
 169         TEST_INSTRUCTION(instruction)
 170 
 171 #define TEST_BRANCH_FX(instruction, codex)                      \
 172         TEST_INSTRUCTION(instruction)                           \
 173         "       b       99f                             \n\t"   \
 174         codex"                                          \n\t"   \
 175         "       b       99f                             \n\t"   \
 176         "2:     nop                                     \n\t"
 177 
 178 #define TEST_BRANCH_BX(instruction, codex)                      \
 179         "       b       50f                             \n\t"   \
 180         "       b       99f                             \n\t"   \
 181         "2:     nop                                     \n\t"   \
 182         "       b       99f                             \n\t"   \
 183         codex"                                          \n\t"   \
 184         TEST_INSTRUCTION(instruction)
 185 
 186 #define TESTCASE_END                                            \
 187         "2:                                             \n\t"   \
 188         "99:                                            \n\t"   \
 189         "       bl __kprobes_test_case_end_"TEST_ISA"   \n\t"   \
 190         ".code "NORMAL_ISA"                             \n\t"   \
 191         : :                                                     \
 192         : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"    \
 193         );
 194 
 195 
 196 /*
 197  * Macros to define test cases.
 198  *
 199  * Those of the form TEST_{R,P,M}* can be used to define test cases
 200  * which take combinations of the three basic types of arguments. E.g.
 201  *
 202  *   TEST_R     One register argument
 203  *   TEST_RR    Two register arguments
 204  *   TEST_RPR   A register, a pointer, then a register argument
 205  *
 206  * For testing instructions which may branch, there are macros TEST_BF_*
 207  * and TEST_BB_* for branching forwards and backwards.
 208  *
 209  * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
 210  * the just verify that a kprobe is or is not allowed on the given instruction.
 211  */
 212 
 213 #define TEST(code)                              \
 214         TESTCASE_START(code)                    \
 215         TEST_ARG_END("")                        \
 216         TEST_INSTRUCTION(code)                  \
 217         TESTCASE_END
 218 
 219 #define TEST_UNSUPPORTED(code)                                  \
 220         TESTCASE_START(code)                                    \
 221         TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED))      \
 222         TEST_INSTRUCTION(code)                                  \
 223         TESTCASE_END
 224 
 225 #define TEST_SUPPORTED(code)                                    \
 226         TESTCASE_START(code)                                    \
 227         TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED))        \
 228         TEST_INSTRUCTION(code)                                  \
 229         TESTCASE_END
 230 
 231 #define TEST_R(code1, reg, val, code2)                  \
 232         TESTCASE_START(code1 #reg code2)                \
 233         TEST_ARG_REG(reg, val)                          \
 234         TEST_ARG_END("")                                \
 235         TEST_INSTRUCTION(code1 #reg code2)              \
 236         TESTCASE_END
 237 
 238 #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3)    \
 239         TESTCASE_START(code1 #reg1 code2 #reg2 code3)           \
 240         TEST_ARG_REG(reg1, val1)                                \
 241         TEST_ARG_REG(reg2, val2)                                \
 242         TEST_ARG_END("")                                        \
 243         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)         \
 244         TESTCASE_END
 245 
 246 #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
 247         TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)               \
 248         TEST_ARG_REG(reg1, val1)                                                \
 249         TEST_ARG_REG(reg2, val2)                                                \
 250         TEST_ARG_REG(reg3, val3)                                                \
 251         TEST_ARG_END("")                                                        \
 252         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)             \
 253         TESTCASE_END
 254 
 255 #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4)   \
 256         TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4)         \
 257         TEST_ARG_REG(reg1, val1)                                                \
 258         TEST_ARG_REG(reg2, val2)                                                \
 259         TEST_ARG_REG(reg3, val3)                                                \
 260         TEST_ARG_REG(reg4, val4)                                                \
 261         TEST_ARG_END("")                                                        \
 262         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4)       \
 263         TESTCASE_END
 264 
 265 #define TEST_P(code1, reg1, val1, code2)        \
 266         TESTCASE_START(code1 #reg1 code2)       \
 267         TEST_ARG_PTR(reg1, val1)                \
 268         TEST_ARG_END("")                        \
 269         TEST_INSTRUCTION(code1 #reg1 code2)     \
 270         TESTCASE_END
 271 
 272 #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3)    \
 273         TESTCASE_START(code1 #reg1 code2 #reg2 code3)           \
 274         TEST_ARG_PTR(reg1, val1)                                \
 275         TEST_ARG_REG(reg2, val2)                                \
 276         TEST_ARG_END("")                                        \
 277         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)         \
 278         TESTCASE_END
 279 
 280 #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3)    \
 281         TESTCASE_START(code1 #reg1 code2 #reg2 code3)           \
 282         TEST_ARG_REG(reg1, val1)                                \
 283         TEST_ARG_PTR(reg2, val2)                                \
 284         TEST_ARG_END("")                                        \
 285         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)         \
 286         TESTCASE_END
 287 
 288 #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
 289         TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)               \
 290         TEST_ARG_PTR(reg1, val1)                                                \
 291         TEST_ARG_REG(reg2, val2)                                                \
 292         TEST_ARG_REG(reg3, val3)                                                \
 293         TEST_ARG_END("")                                                        \
 294         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)             \
 295         TESTCASE_END
 296 
 297 #define TEST_RPR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
 298         TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)               \
 299         TEST_ARG_REG(reg1, val1)                                                \
 300         TEST_ARG_PTR(reg2, val2)                                                \
 301         TEST_ARG_REG(reg3, val3)                                                \
 302         TEST_ARG_END("")                                                        \
 303         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)             \
 304         TESTCASE_END
 305 
 306 #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
 307         TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)               \
 308         TEST_ARG_REG(reg1, val1)                                                \
 309         TEST_ARG_REG(reg2, val2)                                                \
 310         TEST_ARG_PTR(reg3, val3)                                                \
 311         TEST_ARG_END("")                                                        \
 312         TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)             \
 313         TESTCASE_END
 314 
 315 #define TEST_BF_P(code1, reg1, val1, code2)     \
 316         TESTCASE_START(code1 #reg1 code2)       \
 317         TEST_ARG_PTR(reg1, val1)                \
 318         TEST_ARG_END("")                        \
 319         TEST_BRANCH_F(code1 #reg1 code2)        \
 320         TESTCASE_END
 321 
 322 #define TEST_BF(code)                           \
 323         TESTCASE_START(code)                    \
 324         TEST_ARG_END("")                        \
 325         TEST_BRANCH_F(code)                     \
 326         TESTCASE_END
 327 
 328 #define TEST_BB(code)                           \
 329         TESTCASE_START(code)                    \
 330         TEST_ARG_END("")                        \
 331         TEST_BRANCH_B(code)                     \
 332         TESTCASE_END
 333 
 334 #define TEST_BF_R(code1, reg, val, code2)       \
 335         TESTCASE_START(code1 #reg code2)        \
 336         TEST_ARG_REG(reg, val)                  \
 337         TEST_ARG_END("")                        \
 338         TEST_BRANCH_F(code1 #reg code2)         \
 339         TESTCASE_END
 340 
 341 #define TEST_BB_R(code1, reg, val, code2)       \
 342         TESTCASE_START(code1 #reg code2)        \
 343         TEST_ARG_REG(reg, val)                  \
 344         TEST_ARG_END("")                        \
 345         TEST_BRANCH_B(code1 #reg code2)         \
 346         TESTCASE_END
 347 
 348 #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3) \
 349         TESTCASE_START(code1 #reg1 code2 #reg2 code3)           \
 350         TEST_ARG_REG(reg1, val1)                                \
 351         TEST_ARG_REG(reg2, val2)                                \
 352         TEST_ARG_END("")                                        \
 353         TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3)            \
 354         TESTCASE_END
 355 
 356 #define TEST_BF_X(code, codex)                  \
 357         TESTCASE_START(code)                    \
 358         TEST_ARG_END("")                        \
 359         TEST_BRANCH_FX(code, codex)             \
 360         TESTCASE_END
 361 
 362 #define TEST_BB_X(code, codex)                  \
 363         TESTCASE_START(code)                    \
 364         TEST_ARG_END("")                        \
 365         TEST_BRANCH_BX(code, codex)             \
 366         TESTCASE_END
 367 
 368 #define TEST_BF_RX(code1, reg, val, code2, codex)       \
 369         TESTCASE_START(code1 #reg code2)                \
 370         TEST_ARG_REG(reg, val)                          \
 371         TEST_ARG_END("")                                \
 372         TEST_BRANCH_FX(code1 #reg code2, codex)         \
 373         TESTCASE_END
 374 
 375 #define TEST_X(code, codex)                     \
 376         TESTCASE_START(code)                    \
 377         TEST_ARG_END("")                        \
 378         TEST_INSTRUCTION(code)                  \
 379         "       b       99f             \n\t"   \
 380         "       "codex"                 \n\t"   \
 381         TESTCASE_END
 382 
 383 #define TEST_RX(code1, reg, val, code2, codex)          \
 384         TESTCASE_START(code1 #reg code2)                \
 385         TEST_ARG_REG(reg, val)                          \
 386         TEST_ARG_END("")                                \
 387         TEST_INSTRUCTION(code1 __stringify(reg) code2)  \
 388         "       b       99f             \n\t"           \
 389         "       "codex"                 \n\t"           \
 390         TESTCASE_END
 391 
 392 #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex)            \
 393         TESTCASE_START(code1 #reg1 code2 #reg2 code3)                           \
 394         TEST_ARG_REG(reg1, val1)                                                \
 395         TEST_ARG_REG(reg2, val2)                                                \
 396         TEST_ARG_END("")                                                        \
 397         TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \
 398         "       b       99f             \n\t"                                   \
 399         "       "codex"                 \n\t"                                   \
 400         TESTCASE_END
 401 
 402 #define TEST_RMASKED(code1, reg, mask, code2)           \
 403         TESTCASE_START(code1 #reg code2)                \
 404         TEST_ARG_REG_MASKED(reg, mask)                  \
 405         TEST_ARG_END("")                                \
 406         TEST_INSTRUCTION(code1 #reg code2)              \
 407         TESTCASE_END
 408 
 409 /*
 410  * We ignore the state of the imprecise abort disable flag (CPSR.A) because this
 411  * can change randomly as the kernel doesn't take care to preserve or initialise
 412  * this across context switches. Also, with Security Extensions, the flag may
 413  * not be under control of the kernel; for this reason we ignore the state of
 414  * the FIQ disable flag CPSR.F as well.
 415  */
 416 #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
 417 
 418 
 419 /*
 420  * Macros for defining space directives spread over multiple lines.
 421  * These are required so the compiler guesses better the length of inline asm
 422  * code and will spill the literal pool early enough to avoid generating PC
 423  * relative loads with out of range offsets.
 424  */
 425 #define TWICE(x)        x x
 426 #define SPACE_0x8       TWICE(".space 4\n\t")
 427 #define SPACE_0x10      TWICE(SPACE_0x8)
 428 #define SPACE_0x20      TWICE(SPACE_0x10)
 429 #define SPACE_0x40      TWICE(SPACE_0x20)
 430 #define SPACE_0x80      TWICE(SPACE_0x40)
 431 #define SPACE_0x100     TWICE(SPACE_0x80)
 432 #define SPACE_0x200     TWICE(SPACE_0x100)
 433 #define SPACE_0x400     TWICE(SPACE_0x200)
 434 #define SPACE_0x800     TWICE(SPACE_0x400)
 435 #define SPACE_0x1000    TWICE(SPACE_0x800)
 436 
 437 
 438 /* Various values used in test cases... */
 439 #define N(val)  (val ^ 0xffffffff)
 440 #define VAL1    0x12345678
 441 #define VAL2    N(VAL1)
 442 #define VAL3    0xa5f801
 443 #define VAL4    N(VAL3)
 444 #define VALM    0x456789ab
 445 #define VALR    0xdeaddead
 446 #define HH1     0x0123fecb
 447 #define HH2     0xa9874567
 448 
 449 
 450 #ifdef CONFIG_THUMB2_KERNEL
 451 void kprobe_thumb16_test_cases(void);
 452 void kprobe_thumb32_test_cases(void);
 453 #else
 454 void kprobe_arm_test_cases(void);
 455 #endif

/* [<][>][^][v][top][bottom][index][help] */