test_rodata.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * test_rodata.c: functional test for mark_rodata_ro function
  3. *
  4. * (C) Copyright 2008 Intel Corporation
  5. * Author: Arjan van de Ven <arjan@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/module.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/sections.h>
  15. #include <asm/asm.h>
  16. int rodata_test(void)
  17. {
  18. unsigned long result;
  19. unsigned long start, end;
  20. /* test 1: read the value */
  21. /* If this test fails, some previous testrun has clobbered the state */
  22. if (!rodata_test_data) {
  23. printk(KERN_ERR "rodata_test: test 1 fails (start data)\n");
  24. return -ENODEV;
  25. }
  26. /* test 2: write to the variable; this should fault */
  27. /*
  28. * If this test fails, we managed to overwrite the data
  29. *
  30. * This is written in assembly to be able to catch the
  31. * exception that is supposed to happen in the correct
  32. * case
  33. */
  34. result = 1;
  35. asm volatile(
  36. "0: mov %[zero],(%[rodata_test])\n"
  37. " mov %[zero], %[rslt]\n"
  38. "1:\n"
  39. ".section .fixup,\"ax\"\n"
  40. "2: jmp 1b\n"
  41. ".previous\n"
  42. _ASM_EXTABLE(0b,2b)
  43. : [rslt] "=r" (result)
  44. : [rodata_test] "r" (&rodata_test_data), [zero] "r" (0UL)
  45. );
  46. if (!result) {
  47. printk(KERN_ERR "rodata_test: test data was not read only\n");
  48. return -ENODEV;
  49. }
  50. /* test 3: check the value hasn't changed */
  51. /* If this test fails, we managed to overwrite the data */
  52. if (!rodata_test_data) {
  53. printk(KERN_ERR "rodata_test: Test 3 fails (end data)\n");
  54. return -ENODEV;
  55. }
  56. /* test 4: check if the rodata section is 4Kb aligned */
  57. start = (unsigned long)__start_rodata;
  58. end = (unsigned long)__end_rodata;
  59. if (start & (PAGE_SIZE - 1)) {
  60. printk(KERN_ERR "rodata_test: .rodata is not 4k aligned\n");
  61. return -ENODEV;
  62. }
  63. if (end & (PAGE_SIZE - 1)) {
  64. printk(KERN_ERR "rodata_test: .rodata end is not 4k aligned\n");
  65. return -ENODEV;
  66. }
  67. return 0;
  68. }
  69. MODULE_LICENSE("GPL");
  70. MODULE_DESCRIPTION("Testcase for the DEBUG_RODATA infrastructure");
  71. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");