test_rodata.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <asm/cacheflush.h>
  13. #include <asm/sections.h>
  14. #include <asm/asm.h>
  15. int rodata_test(void)
  16. {
  17. unsigned long result;
  18. unsigned long start, end;
  19. /* test 1: read the value */
  20. /* If this test fails, some previous testrun has clobbered the state */
  21. if (!rodata_test_data) {
  22. printk(KERN_ERR "rodata_test: test 1 fails (start data)\n");
  23. return -ENODEV;
  24. }
  25. /* test 2: write to the variable; this should fault */
  26. /*
  27. * If this test fails, we managed to overwrite the data
  28. *
  29. * This is written in assembly to be able to catch the
  30. * exception that is supposed to happen in the correct
  31. * case
  32. */
  33. result = 1;
  34. asm volatile(
  35. "0: mov %[zero],(%[rodata_test])\n"
  36. " mov %[zero], %[rslt]\n"
  37. "1:\n"
  38. ".section .fixup,\"ax\"\n"
  39. "2: jmp 1b\n"
  40. ".previous\n"
  41. _ASM_EXTABLE(0b,2b)
  42. : [rslt] "=r" (result)
  43. : [rodata_test] "r" (&rodata_test_data), [zero] "r" (0UL)
  44. );
  45. if (!result) {
  46. printk(KERN_ERR "rodata_test: test data was not read only\n");
  47. return -ENODEV;
  48. }
  49. /* test 3: check the value hasn't changed */
  50. /* If this test fails, we managed to overwrite the data */
  51. if (!rodata_test_data) {
  52. printk(KERN_ERR "rodata_test: Test 3 fails (end data)\n");
  53. return -ENODEV;
  54. }
  55. /* test 4: check if the rodata section is 4Kb aligned */
  56. start = (unsigned long)__start_rodata;
  57. end = (unsigned long)__end_rodata;
  58. if (start & (PAGE_SIZE - 1)) {
  59. printk(KERN_ERR "rodata_test: .rodata is not 4k aligned\n");
  60. return -ENODEV;
  61. }
  62. if (end & (PAGE_SIZE - 1)) {
  63. printk(KERN_ERR "rodata_test: .rodata end is not 4k aligned\n");
  64. return -ENODEV;
  65. }
  66. return 0;
  67. }