Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/kernel/kprobes-test.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Jon Medhurst | a43bc69 | 2011-08-28 16:18:43 +0100 | [diff] [blame] | 11 | /* |
| 12 | * TESTING METHODOLOGY |
| 13 | * ------------------- |
| 14 | * |
| 15 | * The methodology used to test an ARM instruction 'test_insn' is to use |
| 16 | * inline assembler like: |
| 17 | * |
| 18 | * test_before: nop |
| 19 | * test_case: test_insn |
| 20 | * test_after: nop |
| 21 | * |
| 22 | * When the test case is run a kprobe is placed of each nop. The |
| 23 | * post-handler of the test_before probe is used to modify the saved CPU |
| 24 | * register context to that which we require for the test case. The |
| 25 | * pre-handler of the of the test_after probe saves a copy of the CPU |
| 26 | * register context. In this way we can execute test_insn with a specific |
| 27 | * register context and see the results afterwards. |
| 28 | * |
| 29 | * To actually test the kprobes instruction emulation we perform the above |
| 30 | * step a second time but with an additional kprobe on the test_case |
| 31 | * instruction itself. If the emulation is accurate then the results seen |
| 32 | * by the test_after probe will be identical to the first run which didn't |
| 33 | * have a probe on test_case. |
| 34 | * |
| 35 | * Each test case is run several times with a variety of variations in the |
| 36 | * flags value of stored in CPSR, and for Thumb code, different ITState. |
| 37 | * |
| 38 | * For instructions which can modify PC, a second test_after probe is used |
| 39 | * like this: |
| 40 | * |
| 41 | * test_before: nop |
| 42 | * test_case: test_insn |
| 43 | * test_after: nop |
| 44 | * b test_done |
| 45 | * test_after2: nop |
| 46 | * test_done: |
| 47 | * |
| 48 | * The test case is constructed such that test_insn branches to |
| 49 | * test_after2, or, if testing a conditional instruction, it may just |
| 50 | * continue to test_after. The probes inserted at both locations let us |
| 51 | * determine which happened. A similar approach is used for testing |
| 52 | * backwards branches... |
| 53 | * |
| 54 | * b test_before |
| 55 | * b test_done @ helps to cope with off by 1 branches |
| 56 | * test_after2: nop |
| 57 | * b test_done |
| 58 | * test_before: nop |
| 59 | * test_case: test_insn |
| 60 | * test_after: nop |
| 61 | * test_done: |
| 62 | * |
| 63 | * The macros used to generate the assembler instructions describe above |
| 64 | * are TEST_INSTRUCTION, TEST_BRANCH_F (branch forwards) and TEST_BRANCH_B |
| 65 | * (branch backwards). In these, the local variables numbered 1, 50, 2 and |
| 66 | * 99 represent: test_before, test_case, test_after2 and test_done. |
| 67 | * |
| 68 | * FRAMEWORK |
| 69 | * --------- |
| 70 | * |
| 71 | * Each test case is wrapped between the pair of macros TESTCASE_START and |
| 72 | * TESTCASE_END. As well as performing the inline assembler boilerplate, |
| 73 | * these call out to the kprobes_test_case_start() and |
| 74 | * kprobes_test_case_end() functions which drive the execution of the test |
| 75 | * case. The specific arguments to use for each test case are stored as |
| 76 | * inline data constructed using the various TEST_ARG_* macros. Putting |
| 77 | * this all together, a simple test case may look like: |
| 78 | * |
| 79 | * TESTCASE_START("Testing mov r0, r7") |
| 80 | * TEST_ARG_REG(7, 0x12345678) // Set r7=0x12345678 |
| 81 | * TEST_ARG_END("") |
| 82 | * TEST_INSTRUCTION("mov r0, r7") |
| 83 | * TESTCASE_END |
| 84 | * |
| 85 | * Note, in practice the single convenience macro TEST_R would be used for this |
| 86 | * instead. |
| 87 | * |
| 88 | * The above would expand to assembler looking something like: |
| 89 | * |
| 90 | * @ TESTCASE_START |
| 91 | * bl __kprobes_test_case_start |
| 92 | * @ start of inline data... |
| 93 | * .ascii "mov r0, r7" @ text title for test case |
| 94 | * .byte 0 |
| 95 | * .align 2 |
| 96 | * |
| 97 | * @ TEST_ARG_REG |
| 98 | * .byte ARG_TYPE_REG |
| 99 | * .byte 7 |
| 100 | * .short 0 |
| 101 | * .word 0x1234567 |
| 102 | * |
| 103 | * @ TEST_ARG_END |
| 104 | * .byte ARG_TYPE_END |
| 105 | * .byte TEST_ISA @ flags, including ISA being tested |
| 106 | * .short 50f-0f @ offset of 'test_before' |
| 107 | * .short 2f-0f @ offset of 'test_after2' (if relevent) |
| 108 | * .short 99f-0f @ offset of 'test_done' |
| 109 | * @ start of test case code... |
| 110 | * 0: |
| 111 | * .code TEST_ISA @ switch to ISA being tested |
| 112 | * |
| 113 | * @ TEST_INSTRUCTION |
| 114 | * 50: nop @ location for 'test_before' probe |
| 115 | * 1: mov r0, r7 @ the test case instruction 'test_insn' |
| 116 | * nop @ location for 'test_after' probe |
| 117 | * |
| 118 | * // TESTCASE_END |
| 119 | * 2: |
| 120 | * 99: bl __kprobes_test_case_end_##TEST_ISA |
| 121 | * .code NONMAL_ISA |
| 122 | * |
| 123 | * When the above is execute the following happens... |
| 124 | * |
| 125 | * __kprobes_test_case_start() is an assembler wrapper which sets up space |
| 126 | * for a stack buffer and calls the C function kprobes_test_case_start(). |
| 127 | * This C function will do some initial processing of the inline data and |
| 128 | * setup some global state. It then inserts the test_before and test_after |
| 129 | * kprobes and returns a value which causes the assembler wrapper to jump |
| 130 | * to the start of the test case code, (local label '0'). |
| 131 | * |
| 132 | * When the test case code executes, the test_before probe will be hit and |
| 133 | * test_before_post_handler will call setup_test_context(). This fills the |
| 134 | * stack buffer and CPU registers with a test pattern and then processes |
| 135 | * the test case arguments. In our example there is one TEST_ARG_REG which |
| 136 | * indicates that R7 should be loaded with the value 0x12345678. |
| 137 | * |
| 138 | * When the test_before probe ends, the test case continues and executes |
| 139 | * the "mov r0, r7" instruction. It then hits the test_after probe and the |
| 140 | * pre-handler for this (test_after_pre_handler) will save a copy of the |
| 141 | * CPU register context. This should now have R0 holding the same value as |
| 142 | * R7. |
| 143 | * |
| 144 | * Finally we get to the call to __kprobes_test_case_end_{32,16}. This is |
| 145 | * an assembler wrapper which switches back to the ISA used by the test |
| 146 | * code and calls the C function kprobes_test_case_end(). |
| 147 | * |
| 148 | * For each run through the test case, test_case_run_count is incremented |
| 149 | * by one. For even runs, kprobes_test_case_end() saves a copy of the |
| 150 | * register and stack buffer contents from the test case just run. It then |
| 151 | * inserts a kprobe on the test case instruction 'test_insn' and returns a |
| 152 | * value to cause the test case code to be re-run. |
| 153 | * |
| 154 | * For odd numbered runs, kprobes_test_case_end() compares the register and |
| 155 | * stack buffer contents to those that were saved on the previous even |
| 156 | * numbered run (the one without the kprobe on test_insn). These should be |
| 157 | * the same if the kprobe instruction simulation routine is correct. |
| 158 | * |
| 159 | * The pair of test case runs is repeated with different combinations of |
| 160 | * flag values in CPSR and, for Thumb, different ITState. This is |
| 161 | * controlled by test_context_cpsr(). |
| 162 | * |
| 163 | * BUILDING TEST CASES |
| 164 | * ------------------- |
| 165 | * |
| 166 | * |
| 167 | * As an aid to building test cases, the stack buffer is initialised with |
| 168 | * some special values: |
| 169 | * |
| 170 | * [SP+13*4] Contains SP+120. This can be used to test instructions |
| 171 | * which load a value into SP. |
| 172 | * |
| 173 | * [SP+15*4] When testing branching instructions using TEST_BRANCH_{F,B}, |
| 174 | * this holds the target address of the branch, 'test_after2'. |
| 175 | * This can be used to test instructions which load a PC value |
| 176 | * from memory. |
| 177 | */ |
| 178 | |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 179 | #include <linux/kernel.h> |
| 180 | #include <linux/module.h> |
Jon Medhurst | 963780d | 2011-08-28 16:38:35 +0100 | [diff] [blame^] | 181 | #include <linux/slab.h> |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 182 | #include <linux/kprobes.h> |
| 183 | |
| 184 | #include "kprobes.h" |
Jon Medhurst | a43bc69 | 2011-08-28 16:18:43 +0100 | [diff] [blame] | 185 | #include "kprobes-test.h" |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 186 | |
| 187 | |
| 188 | /* |
| 189 | * Test basic API |
| 190 | */ |
| 191 | |
| 192 | static bool test_regs_ok; |
| 193 | static int test_func_instance; |
| 194 | static int pre_handler_called; |
| 195 | static int post_handler_called; |
| 196 | static int jprobe_func_called; |
| 197 | static int kretprobe_handler_called; |
| 198 | |
| 199 | #define FUNC_ARG1 0x12345678 |
| 200 | #define FUNC_ARG2 0xabcdef |
| 201 | |
| 202 | |
| 203 | #ifndef CONFIG_THUMB2_KERNEL |
| 204 | |
| 205 | long arm_func(long r0, long r1); |
| 206 | |
| 207 | static void __used __naked __arm_kprobes_test_func(void) |
| 208 | { |
| 209 | __asm__ __volatile__ ( |
| 210 | ".arm \n\t" |
| 211 | ".type arm_func, %%function \n\t" |
| 212 | "arm_func: \n\t" |
| 213 | "adds r0, r0, r1 \n\t" |
| 214 | "bx lr \n\t" |
| 215 | ".code "NORMAL_ISA /* Back to Thumb if necessary */ |
| 216 | : : : "r0", "r1", "cc" |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | #else /* CONFIG_THUMB2_KERNEL */ |
| 221 | |
| 222 | long thumb16_func(long r0, long r1); |
| 223 | long thumb32even_func(long r0, long r1); |
| 224 | long thumb32odd_func(long r0, long r1); |
| 225 | |
| 226 | static void __used __naked __thumb_kprobes_test_funcs(void) |
| 227 | { |
| 228 | __asm__ __volatile__ ( |
| 229 | ".type thumb16_func, %%function \n\t" |
| 230 | "thumb16_func: \n\t" |
| 231 | "adds.n r0, r0, r1 \n\t" |
| 232 | "bx lr \n\t" |
| 233 | |
| 234 | ".align \n\t" |
| 235 | ".type thumb32even_func, %%function \n\t" |
| 236 | "thumb32even_func: \n\t" |
| 237 | "adds.w r0, r0, r1 \n\t" |
| 238 | "bx lr \n\t" |
| 239 | |
| 240 | ".align \n\t" |
| 241 | "nop.n \n\t" |
| 242 | ".type thumb32odd_func, %%function \n\t" |
| 243 | "thumb32odd_func: \n\t" |
| 244 | "adds.w r0, r0, r1 \n\t" |
| 245 | "bx lr \n\t" |
| 246 | |
| 247 | : : : "r0", "r1", "cc" |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | #endif /* CONFIG_THUMB2_KERNEL */ |
| 252 | |
| 253 | |
| 254 | static int call_test_func(long (*func)(long, long), bool check_test_regs) |
| 255 | { |
| 256 | long ret; |
| 257 | |
| 258 | ++test_func_instance; |
| 259 | test_regs_ok = false; |
| 260 | |
| 261 | ret = (*func)(FUNC_ARG1, FUNC_ARG2); |
| 262 | if (ret != FUNC_ARG1 + FUNC_ARG2) { |
| 263 | pr_err("FAIL: call_test_func: func returned %lx\n", ret); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | if (check_test_regs && !test_regs_ok) { |
| 268 | pr_err("FAIL: test regs not OK\n"); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | static int __kprobes pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 276 | { |
| 277 | pre_handler_called = test_func_instance; |
| 278 | if (regs->ARM_r0 == FUNC_ARG1 && regs->ARM_r1 == FUNC_ARG2) |
| 279 | test_regs_ok = true; |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs, |
| 284 | unsigned long flags) |
| 285 | { |
| 286 | post_handler_called = test_func_instance; |
| 287 | if (regs->ARM_r0 != FUNC_ARG1 + FUNC_ARG2 || regs->ARM_r1 != FUNC_ARG2) |
| 288 | test_regs_ok = false; |
| 289 | } |
| 290 | |
| 291 | static struct kprobe the_kprobe = { |
| 292 | .addr = 0, |
| 293 | .pre_handler = pre_handler, |
| 294 | .post_handler = post_handler |
| 295 | }; |
| 296 | |
| 297 | static int test_kprobe(long (*func)(long, long)) |
| 298 | { |
| 299 | int ret; |
| 300 | |
| 301 | the_kprobe.addr = (kprobe_opcode_t *)func; |
| 302 | ret = register_kprobe(&the_kprobe); |
| 303 | if (ret < 0) { |
| 304 | pr_err("FAIL: register_kprobe failed with %d\n", ret); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | ret = call_test_func(func, true); |
| 309 | |
| 310 | unregister_kprobe(&the_kprobe); |
| 311 | the_kprobe.flags = 0; /* Clear disable flag to allow reuse */ |
| 312 | |
| 313 | if (!ret) |
| 314 | return -EINVAL; |
| 315 | if (pre_handler_called != test_func_instance) { |
| 316 | pr_err("FAIL: kprobe pre_handler not called\n"); |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | if (post_handler_called != test_func_instance) { |
| 320 | pr_err("FAIL: kprobe post_handler not called\n"); |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | if (!call_test_func(func, false)) |
| 324 | return -EINVAL; |
| 325 | if (pre_handler_called == test_func_instance || |
| 326 | post_handler_called == test_func_instance) { |
| 327 | pr_err("FAIL: probe called after unregistering\n"); |
| 328 | return -EINVAL; |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static void __kprobes jprobe_func(long r0, long r1) |
| 335 | { |
| 336 | jprobe_func_called = test_func_instance; |
| 337 | if (r0 == FUNC_ARG1 && r1 == FUNC_ARG2) |
| 338 | test_regs_ok = true; |
| 339 | jprobe_return(); |
| 340 | } |
| 341 | |
| 342 | static struct jprobe the_jprobe = { |
| 343 | .entry = jprobe_func, |
| 344 | }; |
| 345 | |
| 346 | static int test_jprobe(long (*func)(long, long)) |
| 347 | { |
| 348 | int ret; |
| 349 | |
| 350 | the_jprobe.kp.addr = (kprobe_opcode_t *)func; |
| 351 | ret = register_jprobe(&the_jprobe); |
| 352 | if (ret < 0) { |
| 353 | pr_err("FAIL: register_jprobe failed with %d\n", ret); |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | ret = call_test_func(func, true); |
| 358 | |
| 359 | unregister_jprobe(&the_jprobe); |
| 360 | the_jprobe.kp.flags = 0; /* Clear disable flag to allow reuse */ |
| 361 | |
| 362 | if (!ret) |
| 363 | return -EINVAL; |
| 364 | if (jprobe_func_called != test_func_instance) { |
| 365 | pr_err("FAIL: jprobe handler function not called\n"); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | if (!call_test_func(func, false)) |
| 369 | return -EINVAL; |
| 370 | if (jprobe_func_called == test_func_instance) { |
| 371 | pr_err("FAIL: probe called after unregistering\n"); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int __kprobes |
| 379 | kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs) |
| 380 | { |
| 381 | kretprobe_handler_called = test_func_instance; |
| 382 | if (regs_return_value(regs) == FUNC_ARG1 + FUNC_ARG2) |
| 383 | test_regs_ok = true; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static struct kretprobe the_kretprobe = { |
| 388 | .handler = kretprobe_handler, |
| 389 | }; |
| 390 | |
| 391 | static int test_kretprobe(long (*func)(long, long)) |
| 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | the_kretprobe.kp.addr = (kprobe_opcode_t *)func; |
| 396 | ret = register_kretprobe(&the_kretprobe); |
| 397 | if (ret < 0) { |
| 398 | pr_err("FAIL: register_kretprobe failed with %d\n", ret); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | ret = call_test_func(func, true); |
| 403 | |
| 404 | unregister_kretprobe(&the_kretprobe); |
| 405 | the_kretprobe.kp.flags = 0; /* Clear disable flag to allow reuse */ |
| 406 | |
| 407 | if (!ret) |
| 408 | return -EINVAL; |
| 409 | if (kretprobe_handler_called != test_func_instance) { |
| 410 | pr_err("FAIL: kretprobe handler not called\n"); |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | if (!call_test_func(func, false)) |
| 414 | return -EINVAL; |
| 415 | if (jprobe_func_called == test_func_instance) { |
| 416 | pr_err("FAIL: kretprobe called after unregistering\n"); |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int run_api_tests(long (*func)(long, long)) |
| 424 | { |
| 425 | int ret; |
| 426 | |
| 427 | pr_info(" kprobe\n"); |
| 428 | ret = test_kprobe(func); |
| 429 | if (ret < 0) |
| 430 | return ret; |
| 431 | |
| 432 | pr_info(" jprobe\n"); |
| 433 | ret = test_jprobe(func); |
| 434 | if (ret < 0) |
| 435 | return ret; |
| 436 | |
| 437 | pr_info(" kretprobe\n"); |
| 438 | ret = test_kretprobe(func); |
| 439 | if (ret < 0) |
| 440 | return ret; |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /* |
Jon Medhurst | 68f360e | 2011-08-28 16:35:11 +0100 | [diff] [blame] | 447 | * Decoding table self-consistency tests |
| 448 | */ |
| 449 | |
| 450 | static const int decode_struct_sizes[NUM_DECODE_TYPES] = { |
| 451 | [DECODE_TYPE_TABLE] = sizeof(struct decode_table), |
| 452 | [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom), |
| 453 | [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate), |
| 454 | [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate), |
| 455 | [DECODE_TYPE_OR] = sizeof(struct decode_or), |
| 456 | [DECODE_TYPE_REJECT] = sizeof(struct decode_reject) |
| 457 | }; |
| 458 | |
| 459 | static int table_iter(const union decode_item *table, |
| 460 | int (*fn)(const struct decode_header *, void *), |
| 461 | void *args) |
| 462 | { |
| 463 | const struct decode_header *h = (struct decode_header *)table; |
| 464 | int result; |
| 465 | |
| 466 | for (;;) { |
| 467 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; |
| 468 | |
| 469 | if (type == DECODE_TYPE_END) |
| 470 | return 0; |
| 471 | |
| 472 | result = fn(h, args); |
| 473 | if (result) |
| 474 | return result; |
| 475 | |
| 476 | h = (struct decode_header *) |
| 477 | ((uintptr_t)h + decode_struct_sizes[type]); |
| 478 | |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static int table_test_fail(const struct decode_header *h, const char* message) |
| 483 | { |
| 484 | |
| 485 | pr_err("FAIL: kprobes test failure \"%s\" (mask %08x, value %08x)\n", |
| 486 | message, h->mask.bits, h->value.bits); |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | |
| 490 | struct table_test_args { |
| 491 | const union decode_item *root_table; |
| 492 | u32 parent_mask; |
| 493 | u32 parent_value; |
| 494 | }; |
| 495 | |
| 496 | static int table_test_fn(const struct decode_header *h, void *args) |
| 497 | { |
| 498 | struct table_test_args *a = (struct table_test_args *)args; |
| 499 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; |
| 500 | |
| 501 | if (h->value.bits & ~h->mask.bits) |
| 502 | return table_test_fail(h, "Match value has bits not in mask"); |
| 503 | |
| 504 | if ((h->mask.bits & a->parent_mask) != a->parent_mask) |
| 505 | return table_test_fail(h, "Mask has bits not in parent mask"); |
| 506 | |
| 507 | if ((h->value.bits ^ a->parent_value) & a->parent_mask) |
| 508 | return table_test_fail(h, "Value is inconsistent with parent"); |
| 509 | |
| 510 | if (type == DECODE_TYPE_TABLE) { |
| 511 | struct decode_table *d = (struct decode_table *)h; |
| 512 | struct table_test_args args2 = *a; |
| 513 | args2.parent_mask = h->mask.bits; |
| 514 | args2.parent_value = h->value.bits; |
| 515 | return table_iter(d->table.table, table_test_fn, &args2); |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int table_test(const union decode_item *table) |
| 522 | { |
| 523 | struct table_test_args args = { |
| 524 | .root_table = table, |
| 525 | .parent_mask = 0, |
| 526 | .parent_value = 0 |
| 527 | }; |
| 528 | return table_iter(args.root_table, table_test_fn, &args); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | /* |
Jon Medhurst | 963780d | 2011-08-28 16:38:35 +0100 | [diff] [blame^] | 533 | * Decoding table test coverage analysis |
| 534 | * |
| 535 | * coverage_start() builds a coverage_table which contains a list of |
| 536 | * coverage_entry's to match each entry in the specified kprobes instruction |
| 537 | * decoding table. |
| 538 | * |
| 539 | * When test cases are run, coverage_add() is called to process each case. |
| 540 | * This looks up the corresponding entry in the coverage_table and sets it as |
| 541 | * being matched, as well as clearing the regs flag appropriate for the test. |
| 542 | * |
| 543 | * After all test cases have been run, coverage_end() is called to check that |
| 544 | * all entries in coverage_table have been matched and that all regs flags are |
| 545 | * cleared. I.e. that all possible combinations of instructions described by |
| 546 | * the kprobes decoding tables have had a test case executed for them. |
| 547 | */ |
| 548 | |
| 549 | bool coverage_fail; |
| 550 | |
| 551 | #define MAX_COVERAGE_ENTRIES 256 |
| 552 | |
| 553 | struct coverage_entry { |
| 554 | const struct decode_header *header; |
| 555 | unsigned regs; |
| 556 | unsigned nesting; |
| 557 | char matched; |
| 558 | }; |
| 559 | |
| 560 | struct coverage_table { |
| 561 | struct coverage_entry *base; |
| 562 | unsigned num_entries; |
| 563 | unsigned nesting; |
| 564 | }; |
| 565 | |
| 566 | struct coverage_table coverage; |
| 567 | |
| 568 | #define COVERAGE_ANY_REG (1<<0) |
| 569 | #define COVERAGE_SP (1<<1) |
| 570 | #define COVERAGE_PC (1<<2) |
| 571 | #define COVERAGE_PCWB (1<<3) |
| 572 | |
| 573 | static const char coverage_register_lookup[16] = { |
| 574 | [REG_TYPE_ANY] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC, |
| 575 | [REG_TYPE_SAMEAS16] = COVERAGE_ANY_REG, |
| 576 | [REG_TYPE_SP] = COVERAGE_SP, |
| 577 | [REG_TYPE_PC] = COVERAGE_PC, |
| 578 | [REG_TYPE_NOSP] = COVERAGE_ANY_REG | COVERAGE_SP, |
| 579 | [REG_TYPE_NOSPPC] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC, |
| 580 | [REG_TYPE_NOPC] = COVERAGE_ANY_REG | COVERAGE_PC, |
| 581 | [REG_TYPE_NOPCWB] = COVERAGE_ANY_REG | COVERAGE_PC | COVERAGE_PCWB, |
| 582 | [REG_TYPE_NOPCX] = COVERAGE_ANY_REG, |
| 583 | [REG_TYPE_NOSPPCX] = COVERAGE_ANY_REG | COVERAGE_SP, |
| 584 | }; |
| 585 | |
| 586 | unsigned coverage_start_registers(const struct decode_header *h) |
| 587 | { |
| 588 | unsigned regs = 0; |
| 589 | int i; |
| 590 | for (i = 0; i < 20; i += 4) { |
| 591 | int r = (h->type_regs.bits >> (DECODE_TYPE_BITS + i)) & 0xf; |
| 592 | regs |= coverage_register_lookup[r] << i; |
| 593 | } |
| 594 | return regs; |
| 595 | } |
| 596 | |
| 597 | static int coverage_start_fn(const struct decode_header *h, void *args) |
| 598 | { |
| 599 | struct coverage_table *coverage = (struct coverage_table *)args; |
| 600 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; |
| 601 | struct coverage_entry *entry = coverage->base + coverage->num_entries; |
| 602 | |
| 603 | if (coverage->num_entries == MAX_COVERAGE_ENTRIES - 1) { |
| 604 | pr_err("FAIL: Out of space for test coverage data"); |
| 605 | return -ENOMEM; |
| 606 | } |
| 607 | |
| 608 | ++coverage->num_entries; |
| 609 | |
| 610 | entry->header = h; |
| 611 | entry->regs = coverage_start_registers(h); |
| 612 | entry->nesting = coverage->nesting; |
| 613 | entry->matched = false; |
| 614 | |
| 615 | if (type == DECODE_TYPE_TABLE) { |
| 616 | struct decode_table *d = (struct decode_table *)h; |
| 617 | int ret; |
| 618 | ++coverage->nesting; |
| 619 | ret = table_iter(d->table.table, coverage_start_fn, coverage); |
| 620 | --coverage->nesting; |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int coverage_start(const union decode_item *table) |
| 628 | { |
| 629 | coverage.base = kmalloc(MAX_COVERAGE_ENTRIES * |
| 630 | sizeof(struct coverage_entry), GFP_KERNEL); |
| 631 | coverage.num_entries = 0; |
| 632 | coverage.nesting = 0; |
| 633 | return table_iter(table, coverage_start_fn, &coverage); |
| 634 | } |
| 635 | |
| 636 | static void |
| 637 | coverage_add_registers(struct coverage_entry *entry, kprobe_opcode_t insn) |
| 638 | { |
| 639 | int regs = entry->header->type_regs.bits >> DECODE_TYPE_BITS; |
| 640 | int i; |
| 641 | for (i = 0; i < 20; i += 4) { |
| 642 | enum decode_reg_type reg_type = (regs >> i) & 0xf; |
| 643 | int reg = (insn >> i) & 0xf; |
| 644 | int flag; |
| 645 | |
| 646 | if (!reg_type) |
| 647 | continue; |
| 648 | |
| 649 | if (reg == 13) |
| 650 | flag = COVERAGE_SP; |
| 651 | else if (reg == 15) |
| 652 | flag = COVERAGE_PC; |
| 653 | else |
| 654 | flag = COVERAGE_ANY_REG; |
| 655 | entry->regs &= ~(flag << i); |
| 656 | |
| 657 | switch (reg_type) { |
| 658 | |
| 659 | case REG_TYPE_NONE: |
| 660 | case REG_TYPE_ANY: |
| 661 | case REG_TYPE_SAMEAS16: |
| 662 | break; |
| 663 | |
| 664 | case REG_TYPE_SP: |
| 665 | if (reg != 13) |
| 666 | return; |
| 667 | break; |
| 668 | |
| 669 | case REG_TYPE_PC: |
| 670 | if (reg != 15) |
| 671 | return; |
| 672 | break; |
| 673 | |
| 674 | case REG_TYPE_NOSP: |
| 675 | if (reg == 13) |
| 676 | return; |
| 677 | break; |
| 678 | |
| 679 | case REG_TYPE_NOSPPC: |
| 680 | case REG_TYPE_NOSPPCX: |
| 681 | if (reg == 13 || reg == 15) |
| 682 | return; |
| 683 | break; |
| 684 | |
| 685 | case REG_TYPE_NOPCWB: |
| 686 | if (!is_writeback(insn)) |
| 687 | break; |
| 688 | if (reg == 15) { |
| 689 | entry->regs &= ~(COVERAGE_PCWB << i); |
| 690 | return; |
| 691 | } |
| 692 | break; |
| 693 | |
| 694 | case REG_TYPE_NOPC: |
| 695 | case REG_TYPE_NOPCX: |
| 696 | if (reg == 15) |
| 697 | return; |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | static void coverage_add(kprobe_opcode_t insn) |
| 705 | { |
| 706 | struct coverage_entry *entry = coverage.base; |
| 707 | struct coverage_entry *end = coverage.base + coverage.num_entries; |
| 708 | bool matched = false; |
| 709 | unsigned nesting = 0; |
| 710 | |
| 711 | for (; entry < end; ++entry) { |
| 712 | const struct decode_header *h = entry->header; |
| 713 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; |
| 714 | |
| 715 | if (entry->nesting > nesting) |
| 716 | continue; /* Skip sub-table we didn't match */ |
| 717 | |
| 718 | if (entry->nesting < nesting) |
| 719 | break; /* End of sub-table we were scanning */ |
| 720 | |
| 721 | if (!matched) { |
| 722 | if ((insn & h->mask.bits) != h->value.bits) |
| 723 | continue; |
| 724 | entry->matched = true; |
| 725 | } |
| 726 | |
| 727 | switch (type) { |
| 728 | |
| 729 | case DECODE_TYPE_TABLE: |
| 730 | ++nesting; |
| 731 | break; |
| 732 | |
| 733 | case DECODE_TYPE_CUSTOM: |
| 734 | case DECODE_TYPE_SIMULATE: |
| 735 | case DECODE_TYPE_EMULATE: |
| 736 | coverage_add_registers(entry, insn); |
| 737 | return; |
| 738 | |
| 739 | case DECODE_TYPE_OR: |
| 740 | matched = true; |
| 741 | break; |
| 742 | |
| 743 | case DECODE_TYPE_REJECT: |
| 744 | default: |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static void coverage_end(void) |
| 752 | { |
| 753 | struct coverage_entry *entry = coverage.base; |
| 754 | struct coverage_entry *end = coverage.base + coverage.num_entries; |
| 755 | |
| 756 | for (; entry < end; ++entry) { |
| 757 | u32 mask = entry->header->mask.bits; |
| 758 | u32 value = entry->header->value.bits; |
| 759 | |
| 760 | if (entry->regs) { |
| 761 | pr_err("FAIL: Register test coverage missing for %08x %08x (%05x)\n", |
| 762 | mask, value, entry->regs); |
| 763 | coverage_fail = true; |
| 764 | } |
| 765 | if (!entry->matched) { |
| 766 | pr_err("FAIL: Test coverage entry missing for %08x %08x\n", |
| 767 | mask, value); |
| 768 | coverage_fail = true; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | kfree(coverage.base); |
| 773 | } |
| 774 | |
| 775 | |
| 776 | /* |
Jon Medhurst | a43bc69 | 2011-08-28 16:18:43 +0100 | [diff] [blame] | 777 | * Framework for instruction set test cases |
| 778 | */ |
| 779 | |
| 780 | void __naked __kprobes_test_case_start(void) |
| 781 | { |
| 782 | __asm__ __volatile__ ( |
| 783 | "stmdb sp!, {r4-r11} \n\t" |
| 784 | "sub sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t" |
| 785 | "bic r0, lr, #1 @ r0 = inline title string \n\t" |
| 786 | "mov r1, sp \n\t" |
| 787 | "bl kprobes_test_case_start \n\t" |
| 788 | "bx r0 \n\t" |
| 789 | ); |
| 790 | } |
| 791 | |
| 792 | #ifndef CONFIG_THUMB2_KERNEL |
| 793 | |
| 794 | void __naked __kprobes_test_case_end_32(void) |
| 795 | { |
| 796 | __asm__ __volatile__ ( |
| 797 | "mov r4, lr \n\t" |
| 798 | "bl kprobes_test_case_end \n\t" |
| 799 | "cmp r0, #0 \n\t" |
| 800 | "movne pc, r0 \n\t" |
| 801 | "mov r0, r4 \n\t" |
| 802 | "add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t" |
| 803 | "ldmia sp!, {r4-r11} \n\t" |
| 804 | "mov pc, r0 \n\t" |
| 805 | ); |
| 806 | } |
| 807 | |
| 808 | #else /* CONFIG_THUMB2_KERNEL */ |
| 809 | |
| 810 | void __naked __kprobes_test_case_end_16(void) |
| 811 | { |
| 812 | __asm__ __volatile__ ( |
| 813 | "mov r4, lr \n\t" |
| 814 | "bl kprobes_test_case_end \n\t" |
| 815 | "cmp r0, #0 \n\t" |
| 816 | "bxne r0 \n\t" |
| 817 | "mov r0, r4 \n\t" |
| 818 | "add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t" |
| 819 | "ldmia sp!, {r4-r11} \n\t" |
| 820 | "bx r0 \n\t" |
| 821 | ); |
| 822 | } |
| 823 | |
| 824 | void __naked __kprobes_test_case_end_32(void) |
| 825 | { |
| 826 | __asm__ __volatile__ ( |
| 827 | ".arm \n\t" |
| 828 | "orr lr, lr, #1 @ will return to Thumb code \n\t" |
| 829 | "ldr pc, 1f \n\t" |
| 830 | "1: \n\t" |
| 831 | ".word __kprobes_test_case_end_16 \n\t" |
| 832 | ); |
| 833 | } |
| 834 | |
| 835 | #endif |
| 836 | |
| 837 | |
| 838 | int kprobe_test_flags; |
| 839 | int kprobe_test_cc_position; |
| 840 | |
| 841 | static int test_try_count; |
| 842 | static int test_pass_count; |
| 843 | static int test_fail_count; |
| 844 | |
| 845 | static struct pt_regs initial_regs; |
| 846 | static struct pt_regs expected_regs; |
| 847 | static struct pt_regs result_regs; |
| 848 | |
| 849 | static u32 expected_memory[TEST_MEMORY_SIZE/sizeof(u32)]; |
| 850 | |
| 851 | static const char *current_title; |
| 852 | static struct test_arg *current_args; |
| 853 | static u32 *current_stack; |
| 854 | static uintptr_t current_branch_target; |
| 855 | |
| 856 | static uintptr_t current_code_start; |
| 857 | static kprobe_opcode_t current_instruction; |
| 858 | |
| 859 | |
| 860 | #define TEST_CASE_PASSED -1 |
| 861 | #define TEST_CASE_FAILED -2 |
| 862 | |
| 863 | static int test_case_run_count; |
| 864 | static bool test_case_is_thumb; |
| 865 | static int test_instance; |
| 866 | |
| 867 | /* |
| 868 | * We ignore the state of the imprecise abort disable flag (CPSR.A) because this |
| 869 | * can change randomly as the kernel doesn't take care to preserve or initialise |
| 870 | * this across context switches. Also, with Security Extentions, the flag may |
| 871 | * not be under control of the kernel; for this reason we ignore the state of |
| 872 | * the FIQ disable flag CPSR.F as well. |
| 873 | */ |
| 874 | #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT) |
| 875 | |
| 876 | static unsigned long test_check_cc(int cc, unsigned long cpsr) |
| 877 | { |
| 878 | unsigned long temp; |
| 879 | |
| 880 | switch (cc) { |
| 881 | case 0x0: /* eq */ |
| 882 | return cpsr & PSR_Z_BIT; |
| 883 | |
| 884 | case 0x1: /* ne */ |
| 885 | return (~cpsr) & PSR_Z_BIT; |
| 886 | |
| 887 | case 0x2: /* cs */ |
| 888 | return cpsr & PSR_C_BIT; |
| 889 | |
| 890 | case 0x3: /* cc */ |
| 891 | return (~cpsr) & PSR_C_BIT; |
| 892 | |
| 893 | case 0x4: /* mi */ |
| 894 | return cpsr & PSR_N_BIT; |
| 895 | |
| 896 | case 0x5: /* pl */ |
| 897 | return (~cpsr) & PSR_N_BIT; |
| 898 | |
| 899 | case 0x6: /* vs */ |
| 900 | return cpsr & PSR_V_BIT; |
| 901 | |
| 902 | case 0x7: /* vc */ |
| 903 | return (~cpsr) & PSR_V_BIT; |
| 904 | |
| 905 | case 0x8: /* hi */ |
| 906 | cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */ |
| 907 | return cpsr & PSR_C_BIT; |
| 908 | |
| 909 | case 0x9: /* ls */ |
| 910 | cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */ |
| 911 | return (~cpsr) & PSR_C_BIT; |
| 912 | |
| 913 | case 0xa: /* ge */ |
| 914 | cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 915 | return (~cpsr) & PSR_N_BIT; |
| 916 | |
| 917 | case 0xb: /* lt */ |
| 918 | cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 919 | return cpsr & PSR_N_BIT; |
| 920 | |
| 921 | case 0xc: /* gt */ |
| 922 | temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 923 | temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */ |
| 924 | return (~temp) & PSR_N_BIT; |
| 925 | |
| 926 | case 0xd: /* le */ |
| 927 | temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 928 | temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */ |
| 929 | return temp & PSR_N_BIT; |
| 930 | |
| 931 | case 0xe: /* al */ |
| 932 | case 0xf: /* unconditional */ |
| 933 | return true; |
| 934 | } |
| 935 | BUG(); |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | static int is_last_scenario; |
| 940 | static int probe_should_run; /* 0 = no, 1 = yes, -1 = unknown */ |
| 941 | static int memory_needs_checking; |
| 942 | |
| 943 | static unsigned long test_context_cpsr(int scenario) |
| 944 | { |
| 945 | unsigned long cpsr; |
| 946 | |
| 947 | probe_should_run = 1; |
| 948 | |
| 949 | /* Default case is that we cycle through 16 combinations of flags */ |
| 950 | cpsr = (scenario & 0xf) << 28; /* N,Z,C,V flags */ |
| 951 | cpsr |= (scenario & 0xf) << 16; /* GE flags */ |
| 952 | cpsr |= (scenario & 0x1) << 27; /* Toggle Q flag */ |
| 953 | |
| 954 | if (!test_case_is_thumb) { |
| 955 | /* Testing ARM code */ |
| 956 | probe_should_run = test_check_cc(current_instruction >> 28, cpsr) != 0; |
| 957 | if (scenario == 15) |
| 958 | is_last_scenario = true; |
| 959 | |
| 960 | } else if (kprobe_test_flags & TEST_FLAG_NO_ITBLOCK) { |
| 961 | /* Testing Thumb code without setting ITSTATE */ |
| 962 | if (kprobe_test_cc_position) { |
| 963 | int cc = (current_instruction >> kprobe_test_cc_position) & 0xf; |
| 964 | probe_should_run = test_check_cc(cc, cpsr) != 0; |
| 965 | } |
| 966 | |
| 967 | if (scenario == 15) |
| 968 | is_last_scenario = true; |
| 969 | |
| 970 | } else if (kprobe_test_flags & TEST_FLAG_FULL_ITBLOCK) { |
| 971 | /* Testing Thumb code with all combinations of ITSTATE */ |
| 972 | unsigned x = (scenario >> 4); |
| 973 | unsigned cond_base = x % 7; /* ITSTATE<7:5> */ |
| 974 | unsigned mask = x / 7 + 2; /* ITSTATE<4:0>, bits reversed */ |
| 975 | |
| 976 | if (mask > 0x1f) { |
| 977 | /* Finish by testing state from instruction 'itt al' */ |
| 978 | cond_base = 7; |
| 979 | mask = 0x4; |
| 980 | if ((scenario & 0xf) == 0xf) |
| 981 | is_last_scenario = true; |
| 982 | } |
| 983 | |
| 984 | cpsr |= cond_base << 13; /* ITSTATE<7:5> */ |
| 985 | cpsr |= (mask & 0x1) << 12; /* ITSTATE<4> */ |
| 986 | cpsr |= (mask & 0x2) << 10; /* ITSTATE<3> */ |
| 987 | cpsr |= (mask & 0x4) << 8; /* ITSTATE<2> */ |
| 988 | cpsr |= (mask & 0x8) << 23; /* ITSTATE<1> */ |
| 989 | cpsr |= (mask & 0x10) << 21; /* ITSTATE<0> */ |
| 990 | |
| 991 | probe_should_run = test_check_cc((cpsr >> 12) & 0xf, cpsr) != 0; |
| 992 | |
| 993 | } else { |
| 994 | /* Testing Thumb code with several combinations of ITSTATE */ |
| 995 | switch (scenario) { |
| 996 | case 16: /* Clear NZCV flags and 'it eq' state (false as Z=0) */ |
| 997 | cpsr = 0x00000800; |
| 998 | probe_should_run = 0; |
| 999 | break; |
| 1000 | case 17: /* Set NZCV flags and 'it vc' state (false as V=1) */ |
| 1001 | cpsr = 0xf0007800; |
| 1002 | probe_should_run = 0; |
| 1003 | break; |
| 1004 | case 18: /* Clear NZCV flags and 'it ls' state (true as C=0) */ |
| 1005 | cpsr = 0x00009800; |
| 1006 | break; |
| 1007 | case 19: /* Set NZCV flags and 'it cs' state (true as C=1) */ |
| 1008 | cpsr = 0xf0002800; |
| 1009 | is_last_scenario = true; |
| 1010 | break; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | return cpsr; |
| 1015 | } |
| 1016 | |
| 1017 | static void setup_test_context(struct pt_regs *regs) |
| 1018 | { |
| 1019 | int scenario = test_case_run_count>>1; |
| 1020 | unsigned long val; |
| 1021 | struct test_arg *args; |
| 1022 | int i; |
| 1023 | |
| 1024 | is_last_scenario = false; |
| 1025 | memory_needs_checking = false; |
| 1026 | |
| 1027 | /* Initialise test memory on stack */ |
| 1028 | val = (scenario & 1) ? VALM : ~VALM; |
| 1029 | for (i = 0; i < TEST_MEMORY_SIZE / sizeof(current_stack[0]); ++i) |
| 1030 | current_stack[i] = val + (i << 8); |
| 1031 | /* Put target of branch on stack for tests which load PC from memory */ |
| 1032 | if (current_branch_target) |
| 1033 | current_stack[15] = current_branch_target; |
| 1034 | /* Put a value for SP on stack for tests which load SP from memory */ |
| 1035 | current_stack[13] = (u32)current_stack + 120; |
| 1036 | |
| 1037 | /* Initialise register values to their default state */ |
| 1038 | val = (scenario & 2) ? VALR : ~VALR; |
| 1039 | for (i = 0; i < 13; ++i) |
| 1040 | regs->uregs[i] = val ^ (i << 8); |
| 1041 | regs->ARM_lr = val ^ (14 << 8); |
| 1042 | regs->ARM_cpsr &= ~(APSR_MASK | PSR_IT_MASK); |
| 1043 | regs->ARM_cpsr |= test_context_cpsr(scenario); |
| 1044 | |
| 1045 | /* Perform testcase specific register setup */ |
| 1046 | args = current_args; |
| 1047 | for (; args[0].type != ARG_TYPE_END; ++args) |
| 1048 | switch (args[0].type) { |
| 1049 | case ARG_TYPE_REG: { |
| 1050 | struct test_arg_regptr *arg = |
| 1051 | (struct test_arg_regptr *)args; |
| 1052 | regs->uregs[arg->reg] = arg->val; |
| 1053 | break; |
| 1054 | } |
| 1055 | case ARG_TYPE_PTR: { |
| 1056 | struct test_arg_regptr *arg = |
| 1057 | (struct test_arg_regptr *)args; |
| 1058 | regs->uregs[arg->reg] = |
| 1059 | (unsigned long)current_stack + arg->val; |
| 1060 | memory_needs_checking = true; |
| 1061 | break; |
| 1062 | } |
| 1063 | case ARG_TYPE_MEM: { |
| 1064 | struct test_arg_mem *arg = (struct test_arg_mem *)args; |
| 1065 | current_stack[arg->index] = arg->val; |
| 1066 | break; |
| 1067 | } |
| 1068 | default: |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | struct test_probe { |
| 1074 | struct kprobe kprobe; |
| 1075 | bool registered; |
| 1076 | int hit; |
| 1077 | }; |
| 1078 | |
| 1079 | static void unregister_test_probe(struct test_probe *probe) |
| 1080 | { |
| 1081 | if (probe->registered) { |
| 1082 | unregister_kprobe(&probe->kprobe); |
| 1083 | probe->kprobe.flags = 0; /* Clear disable flag to allow reuse */ |
| 1084 | } |
| 1085 | probe->registered = false; |
| 1086 | } |
| 1087 | |
| 1088 | static int register_test_probe(struct test_probe *probe) |
| 1089 | { |
| 1090 | int ret; |
| 1091 | |
| 1092 | if (probe->registered) |
| 1093 | BUG(); |
| 1094 | |
| 1095 | ret = register_kprobe(&probe->kprobe); |
| 1096 | if (ret >= 0) { |
| 1097 | probe->registered = true; |
| 1098 | probe->hit = -1; |
| 1099 | } |
| 1100 | return ret; |
| 1101 | } |
| 1102 | |
| 1103 | static int __kprobes |
| 1104 | test_before_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 1105 | { |
| 1106 | container_of(p, struct test_probe, kprobe)->hit = test_instance; |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | static void __kprobes |
| 1111 | test_before_post_handler(struct kprobe *p, struct pt_regs *regs, |
| 1112 | unsigned long flags) |
| 1113 | { |
| 1114 | setup_test_context(regs); |
| 1115 | initial_regs = *regs; |
| 1116 | initial_regs.ARM_cpsr &= ~PSR_IGNORE_BITS; |
| 1117 | } |
| 1118 | |
| 1119 | static int __kprobes |
| 1120 | test_case_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 1121 | { |
| 1122 | container_of(p, struct test_probe, kprobe)->hit = test_instance; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static int __kprobes |
| 1127 | test_after_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 1128 | { |
| 1129 | if (container_of(p, struct test_probe, kprobe)->hit == test_instance) |
| 1130 | return 0; /* Already run for this test instance */ |
| 1131 | |
| 1132 | result_regs = *regs; |
| 1133 | result_regs.ARM_cpsr &= ~PSR_IGNORE_BITS; |
| 1134 | |
| 1135 | /* Undo any changes done to SP by the test case */ |
| 1136 | regs->ARM_sp = (unsigned long)current_stack; |
| 1137 | |
| 1138 | container_of(p, struct test_probe, kprobe)->hit = test_instance; |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | static struct test_probe test_before_probe = { |
| 1143 | .kprobe.pre_handler = test_before_pre_handler, |
| 1144 | .kprobe.post_handler = test_before_post_handler, |
| 1145 | }; |
| 1146 | |
| 1147 | static struct test_probe test_case_probe = { |
| 1148 | .kprobe.pre_handler = test_case_pre_handler, |
| 1149 | }; |
| 1150 | |
| 1151 | static struct test_probe test_after_probe = { |
| 1152 | .kprobe.pre_handler = test_after_pre_handler, |
| 1153 | }; |
| 1154 | |
| 1155 | static struct test_probe test_after2_probe = { |
| 1156 | .kprobe.pre_handler = test_after_pre_handler, |
| 1157 | }; |
| 1158 | |
| 1159 | static void test_case_cleanup(void) |
| 1160 | { |
| 1161 | unregister_test_probe(&test_before_probe); |
| 1162 | unregister_test_probe(&test_case_probe); |
| 1163 | unregister_test_probe(&test_after_probe); |
| 1164 | unregister_test_probe(&test_after2_probe); |
| 1165 | } |
| 1166 | |
| 1167 | static void print_registers(struct pt_regs *regs) |
| 1168 | { |
| 1169 | pr_err("r0 %08lx | r1 %08lx | r2 %08lx | r3 %08lx\n", |
| 1170 | regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3); |
| 1171 | pr_err("r4 %08lx | r5 %08lx | r6 %08lx | r7 %08lx\n", |
| 1172 | regs->ARM_r4, regs->ARM_r5, regs->ARM_r6, regs->ARM_r7); |
| 1173 | pr_err("r8 %08lx | r9 %08lx | r10 %08lx | r11 %08lx\n", |
| 1174 | regs->ARM_r8, regs->ARM_r9, regs->ARM_r10, regs->ARM_fp); |
| 1175 | pr_err("r12 %08lx | sp %08lx | lr %08lx | pc %08lx\n", |
| 1176 | regs->ARM_ip, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc); |
| 1177 | pr_err("cpsr %08lx\n", regs->ARM_cpsr); |
| 1178 | } |
| 1179 | |
| 1180 | static void print_memory(u32 *mem, size_t size) |
| 1181 | { |
| 1182 | int i; |
| 1183 | for (i = 0; i < size / sizeof(u32); i += 4) |
| 1184 | pr_err("%08x %08x %08x %08x\n", mem[i], mem[i+1], |
| 1185 | mem[i+2], mem[i+3]); |
| 1186 | } |
| 1187 | |
| 1188 | static size_t expected_memory_size(u32 *sp) |
| 1189 | { |
| 1190 | size_t size = sizeof(expected_memory); |
| 1191 | int offset = (uintptr_t)sp - (uintptr_t)current_stack; |
| 1192 | if (offset > 0) |
| 1193 | size -= offset; |
| 1194 | return size; |
| 1195 | } |
| 1196 | |
| 1197 | static void test_case_failed(const char *message) |
| 1198 | { |
| 1199 | test_case_cleanup(); |
| 1200 | |
| 1201 | pr_err("FAIL: %s\n", message); |
| 1202 | pr_err("FAIL: Test %s\n", current_title); |
| 1203 | pr_err("FAIL: Scenario %d\n", test_case_run_count >> 1); |
| 1204 | } |
| 1205 | |
| 1206 | static unsigned long next_instruction(unsigned long pc) |
| 1207 | { |
| 1208 | #ifdef CONFIG_THUMB2_KERNEL |
| 1209 | if ((pc & 1) && !is_wide_instruction(*(u16 *)(pc - 1))) |
| 1210 | return pc + 2; |
| 1211 | else |
| 1212 | #endif |
| 1213 | return pc + 4; |
| 1214 | } |
| 1215 | |
| 1216 | static uintptr_t __used kprobes_test_case_start(const char *title, void *stack) |
| 1217 | { |
| 1218 | struct test_arg *args; |
| 1219 | struct test_arg_end *end_arg; |
| 1220 | unsigned long test_code; |
| 1221 | |
| 1222 | args = (struct test_arg *)PTR_ALIGN(title + strlen(title) + 1, 4); |
| 1223 | |
| 1224 | current_title = title; |
| 1225 | current_args = args; |
| 1226 | current_stack = stack; |
| 1227 | |
| 1228 | ++test_try_count; |
| 1229 | |
| 1230 | while (args->type != ARG_TYPE_END) |
| 1231 | ++args; |
| 1232 | end_arg = (struct test_arg_end *)args; |
| 1233 | |
| 1234 | test_code = (unsigned long)(args + 1); /* Code starts after args */ |
| 1235 | |
| 1236 | test_case_is_thumb = end_arg->flags & ARG_FLAG_THUMB; |
| 1237 | if (test_case_is_thumb) |
| 1238 | test_code |= 1; |
| 1239 | |
| 1240 | current_code_start = test_code; |
| 1241 | |
| 1242 | current_branch_target = 0; |
| 1243 | if (end_arg->branch_offset != end_arg->end_offset) |
| 1244 | current_branch_target = test_code + end_arg->branch_offset; |
| 1245 | |
| 1246 | test_code += end_arg->code_offset; |
| 1247 | test_before_probe.kprobe.addr = (kprobe_opcode_t *)test_code; |
| 1248 | |
| 1249 | test_code = next_instruction(test_code); |
| 1250 | test_case_probe.kprobe.addr = (kprobe_opcode_t *)test_code; |
| 1251 | |
| 1252 | if (test_case_is_thumb) { |
| 1253 | u16 *p = (u16 *)(test_code & ~1); |
| 1254 | current_instruction = p[0]; |
| 1255 | if (is_wide_instruction(current_instruction)) { |
| 1256 | current_instruction <<= 16; |
| 1257 | current_instruction |= p[1]; |
| 1258 | } |
| 1259 | } else { |
| 1260 | current_instruction = *(u32 *)test_code; |
| 1261 | } |
| 1262 | |
| 1263 | if (current_title[0] == '.') |
| 1264 | verbose("%s\n", current_title); |
| 1265 | else |
| 1266 | verbose("%s\t@ %0*x\n", current_title, |
| 1267 | test_case_is_thumb ? 4 : 8, |
| 1268 | current_instruction); |
| 1269 | |
| 1270 | test_code = next_instruction(test_code); |
| 1271 | test_after_probe.kprobe.addr = (kprobe_opcode_t *)test_code; |
| 1272 | |
| 1273 | if (kprobe_test_flags & TEST_FLAG_NARROW_INSTR) { |
| 1274 | if (!test_case_is_thumb || |
| 1275 | is_wide_instruction(current_instruction)) { |
| 1276 | test_case_failed("expected 16-bit instruction"); |
| 1277 | goto fail; |
| 1278 | } |
| 1279 | } else { |
| 1280 | if (test_case_is_thumb && |
| 1281 | !is_wide_instruction(current_instruction)) { |
| 1282 | test_case_failed("expected 32-bit instruction"); |
| 1283 | goto fail; |
| 1284 | } |
| 1285 | } |
| 1286 | |
Jon Medhurst | 963780d | 2011-08-28 16:38:35 +0100 | [diff] [blame^] | 1287 | coverage_add(current_instruction); |
| 1288 | |
Jon Medhurst | a43bc69 | 2011-08-28 16:18:43 +0100 | [diff] [blame] | 1289 | if (end_arg->flags & ARG_FLAG_UNSUPPORTED) { |
| 1290 | if (register_test_probe(&test_case_probe) < 0) |
| 1291 | goto pass; |
| 1292 | test_case_failed("registered probe for unsupported instruction"); |
| 1293 | goto fail; |
| 1294 | } |
| 1295 | |
| 1296 | if (end_arg->flags & ARG_FLAG_SUPPORTED) { |
| 1297 | if (register_test_probe(&test_case_probe) >= 0) |
| 1298 | goto pass; |
| 1299 | test_case_failed("couldn't register probe for supported instruction"); |
| 1300 | goto fail; |
| 1301 | } |
| 1302 | |
| 1303 | if (register_test_probe(&test_before_probe) < 0) { |
| 1304 | test_case_failed("register test_before_probe failed"); |
| 1305 | goto fail; |
| 1306 | } |
| 1307 | if (register_test_probe(&test_after_probe) < 0) { |
| 1308 | test_case_failed("register test_after_probe failed"); |
| 1309 | goto fail; |
| 1310 | } |
| 1311 | if (current_branch_target) { |
| 1312 | test_after2_probe.kprobe.addr = |
| 1313 | (kprobe_opcode_t *)current_branch_target; |
| 1314 | if (register_test_probe(&test_after2_probe) < 0) { |
| 1315 | test_case_failed("register test_after2_probe failed"); |
| 1316 | goto fail; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /* Start first run of test case */ |
| 1321 | test_case_run_count = 0; |
| 1322 | ++test_instance; |
| 1323 | return current_code_start; |
| 1324 | pass: |
| 1325 | test_case_run_count = TEST_CASE_PASSED; |
| 1326 | return (uintptr_t)test_after_probe.kprobe.addr; |
| 1327 | fail: |
| 1328 | test_case_run_count = TEST_CASE_FAILED; |
| 1329 | return (uintptr_t)test_after_probe.kprobe.addr; |
| 1330 | } |
| 1331 | |
| 1332 | static bool check_test_results(void) |
| 1333 | { |
| 1334 | size_t mem_size = 0; |
| 1335 | u32 *mem = 0; |
| 1336 | |
| 1337 | if (memcmp(&expected_regs, &result_regs, sizeof(expected_regs))) { |
| 1338 | test_case_failed("registers differ"); |
| 1339 | goto fail; |
| 1340 | } |
| 1341 | |
| 1342 | if (memory_needs_checking) { |
| 1343 | mem = (u32 *)result_regs.ARM_sp; |
| 1344 | mem_size = expected_memory_size(mem); |
| 1345 | if (memcmp(expected_memory, mem, mem_size)) { |
| 1346 | test_case_failed("test memory differs"); |
| 1347 | goto fail; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | return true; |
| 1352 | |
| 1353 | fail: |
| 1354 | pr_err("initial_regs:\n"); |
| 1355 | print_registers(&initial_regs); |
| 1356 | pr_err("expected_regs:\n"); |
| 1357 | print_registers(&expected_regs); |
| 1358 | pr_err("result_regs:\n"); |
| 1359 | print_registers(&result_regs); |
| 1360 | |
| 1361 | if (mem) { |
| 1362 | pr_err("current_stack=%p\n", current_stack); |
| 1363 | pr_err("expected_memory:\n"); |
| 1364 | print_memory(expected_memory, mem_size); |
| 1365 | pr_err("result_memory:\n"); |
| 1366 | print_memory(mem, mem_size); |
| 1367 | } |
| 1368 | |
| 1369 | return false; |
| 1370 | } |
| 1371 | |
| 1372 | static uintptr_t __used kprobes_test_case_end(void) |
| 1373 | { |
| 1374 | if (test_case_run_count < 0) { |
| 1375 | if (test_case_run_count == TEST_CASE_PASSED) |
| 1376 | /* kprobes_test_case_start did all the needed testing */ |
| 1377 | goto pass; |
| 1378 | else |
| 1379 | /* kprobes_test_case_start failed */ |
| 1380 | goto fail; |
| 1381 | } |
| 1382 | |
| 1383 | if (test_before_probe.hit != test_instance) { |
| 1384 | test_case_failed("test_before_handler not run"); |
| 1385 | goto fail; |
| 1386 | } |
| 1387 | |
| 1388 | if (test_after_probe.hit != test_instance && |
| 1389 | test_after2_probe.hit != test_instance) { |
| 1390 | test_case_failed("test_after_handler not run"); |
| 1391 | goto fail; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Even numbered test runs ran without a probe on the test case so |
| 1396 | * we can gather reference results. The subsequent odd numbered run |
| 1397 | * will have the probe inserted. |
| 1398 | */ |
| 1399 | if ((test_case_run_count & 1) == 0) { |
| 1400 | /* Save results from run without probe */ |
| 1401 | u32 *mem = (u32 *)result_regs.ARM_sp; |
| 1402 | expected_regs = result_regs; |
| 1403 | memcpy(expected_memory, mem, expected_memory_size(mem)); |
| 1404 | |
| 1405 | /* Insert probe onto test case instruction */ |
| 1406 | if (register_test_probe(&test_case_probe) < 0) { |
| 1407 | test_case_failed("register test_case_probe failed"); |
| 1408 | goto fail; |
| 1409 | } |
| 1410 | } else { |
| 1411 | /* Check probe ran as expected */ |
| 1412 | if (probe_should_run == 1) { |
| 1413 | if (test_case_probe.hit != test_instance) { |
| 1414 | test_case_failed("test_case_handler not run"); |
| 1415 | goto fail; |
| 1416 | } |
| 1417 | } else if (probe_should_run == 0) { |
| 1418 | if (test_case_probe.hit == test_instance) { |
| 1419 | test_case_failed("test_case_handler ran"); |
| 1420 | goto fail; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /* Remove probe for any subsequent reference run */ |
| 1425 | unregister_test_probe(&test_case_probe); |
| 1426 | |
| 1427 | if (!check_test_results()) |
| 1428 | goto fail; |
| 1429 | |
| 1430 | if (is_last_scenario) |
| 1431 | goto pass; |
| 1432 | } |
| 1433 | |
| 1434 | /* Do next test run */ |
| 1435 | ++test_case_run_count; |
| 1436 | ++test_instance; |
| 1437 | return current_code_start; |
| 1438 | fail: |
| 1439 | ++test_fail_count; |
| 1440 | goto end; |
| 1441 | pass: |
| 1442 | ++test_pass_count; |
| 1443 | end: |
| 1444 | test_case_cleanup(); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | /* |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 1450 | * Top level test functions |
| 1451 | */ |
| 1452 | |
Jon Medhurst | 68f360e | 2011-08-28 16:35:11 +0100 | [diff] [blame] | 1453 | static int run_test_cases(void (*tests)(void), const union decode_item *table) |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1454 | { |
Jon Medhurst | 68f360e | 2011-08-28 16:35:11 +0100 | [diff] [blame] | 1455 | int ret; |
| 1456 | |
| 1457 | pr_info(" Check decoding tables\n"); |
| 1458 | ret = table_test(table); |
| 1459 | if (ret) |
| 1460 | return ret; |
| 1461 | |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1462 | pr_info(" Run test cases\n"); |
Jon Medhurst | 963780d | 2011-08-28 16:38:35 +0100 | [diff] [blame^] | 1463 | ret = coverage_start(table); |
| 1464 | if (ret) |
| 1465 | return ret; |
| 1466 | |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1467 | tests(); |
| 1468 | |
Jon Medhurst | 963780d | 2011-08-28 16:38:35 +0100 | [diff] [blame^] | 1469 | coverage_end(); |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 1474 | static int __init run_all_tests(void) |
| 1475 | { |
| 1476 | int ret = 0; |
| 1477 | |
| 1478 | pr_info("Begining kprobe tests...\n"); |
| 1479 | |
| 1480 | #ifndef CONFIG_THUMB2_KERNEL |
| 1481 | |
| 1482 | pr_info("Probe ARM code\n"); |
| 1483 | ret = run_api_tests(arm_func); |
| 1484 | if (ret) |
| 1485 | goto out; |
| 1486 | |
Jon Medhurst | c0cc6df | 2011-08-27 12:41:05 +0100 | [diff] [blame] | 1487 | pr_info("ARM instruction simulation\n"); |
Jon Medhurst | 68f360e | 2011-08-28 16:35:11 +0100 | [diff] [blame] | 1488 | ret = run_test_cases(kprobe_arm_test_cases, kprobe_decode_arm_table); |
Jon Medhurst | c0cc6df | 2011-08-27 12:41:05 +0100 | [diff] [blame] | 1489 | if (ret) |
| 1490 | goto out; |
| 1491 | |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 1492 | #else /* CONFIG_THUMB2_KERNEL */ |
| 1493 | |
| 1494 | pr_info("Probe 16-bit Thumb code\n"); |
| 1495 | ret = run_api_tests(thumb16_func); |
| 1496 | if (ret) |
| 1497 | goto out; |
| 1498 | |
| 1499 | pr_info("Probe 32-bit Thumb code, even halfword\n"); |
| 1500 | ret = run_api_tests(thumb32even_func); |
| 1501 | if (ret) |
| 1502 | goto out; |
| 1503 | |
| 1504 | pr_info("Probe 32-bit Thumb code, odd halfword\n"); |
| 1505 | ret = run_api_tests(thumb32odd_func); |
| 1506 | if (ret) |
| 1507 | goto out; |
| 1508 | |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1509 | pr_info("16-bit Thumb instruction simulation\n"); |
Jon Medhurst | 68f360e | 2011-08-28 16:35:11 +0100 | [diff] [blame] | 1510 | ret = run_test_cases(kprobe_thumb16_test_cases, |
| 1511 | kprobe_decode_thumb16_table); |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1512 | if (ret) |
| 1513 | goto out; |
| 1514 | |
| 1515 | pr_info("32-bit Thumb instruction simulation\n"); |
Jon Medhurst | 68f360e | 2011-08-28 16:35:11 +0100 | [diff] [blame] | 1516 | ret = run_test_cases(kprobe_thumb32_test_cases, |
| 1517 | kprobe_decode_thumb32_table); |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1518 | if (ret) |
| 1519 | goto out; |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 1520 | #endif |
| 1521 | |
Jon Medhurst | c7054aa | 2011-08-27 12:40:30 +0100 | [diff] [blame] | 1522 | pr_info("Total instruction simulation tests=%d, pass=%d fail=%d\n", |
| 1523 | test_try_count, test_pass_count, test_fail_count); |
| 1524 | if (test_fail_count) { |
| 1525 | ret = -EINVAL; |
| 1526 | goto out; |
| 1527 | } |
| 1528 | |
Jon Medhurst | 963780d | 2011-08-28 16:38:35 +0100 | [diff] [blame^] | 1529 | #if __LINUX_ARM_ARCH__ >= 7 |
| 1530 | /* We are able to run all test cases so coverage should be complete */ |
| 1531 | if (coverage_fail) { |
| 1532 | pr_err("FAIL: Test coverage checks failed\n"); |
| 1533 | ret = -EINVAL; |
| 1534 | goto out; |
| 1535 | } |
| 1536 | #endif |
| 1537 | |
Jon Medhurst | 9eed179 | 2011-08-28 16:02:38 +0100 | [diff] [blame] | 1538 | out: |
| 1539 | if (ret == 0) |
| 1540 | pr_info("Finished kprobe tests OK\n"); |
| 1541 | else |
| 1542 | pr_err("kprobe tests failed\n"); |
| 1543 | |
| 1544 | return ret; |
| 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | /* |
| 1549 | * Module setup |
| 1550 | */ |
| 1551 | |
| 1552 | #ifdef MODULE |
| 1553 | |
| 1554 | static void __exit kprobe_test_exit(void) |
| 1555 | { |
| 1556 | } |
| 1557 | |
| 1558 | module_init(run_all_tests) |
| 1559 | module_exit(kprobe_test_exit) |
| 1560 | MODULE_LICENSE("GPL"); |
| 1561 | |
| 1562 | #else /* !MODULE */ |
| 1563 | |
| 1564 | late_initcall(run_all_tests); |
| 1565 | |
| 1566 | #endif |