extable.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Rewritten by Rusty Russell, on the backs of many others...
  2. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/ftrace.h>
  16. #include <linux/memory.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/init.h>
  20. #include <asm/sections.h>
  21. #include <asm/uaccess.h>
  22. /*
  23. * mutex protecting text section modification (dynamic code patching).
  24. * some users need to sleep (allocating memory...) while they hold this lock.
  25. *
  26. * NOT exported to modules - patching kernel text is a really delicate matter.
  27. */
  28. DEFINE_MUTEX(text_mutex);
  29. extern struct exception_table_entry __start___ex_table[];
  30. extern struct exception_table_entry __stop___ex_table[];
  31. /* Sort the kernel's built-in exception table */
  32. void __init sort_main_extable(void)
  33. {
  34. sort_extable(__start___ex_table, __stop___ex_table);
  35. }
  36. /* Given an address, look for it in the exception tables. */
  37. const struct exception_table_entry *search_exception_tables(unsigned long addr)
  38. {
  39. const struct exception_table_entry *e;
  40. e = search_extable(__start___ex_table, __stop___ex_table-1, addr);
  41. if (!e)
  42. e = search_module_extables(addr);
  43. return e;
  44. }
  45. static inline int init_kernel_text(unsigned long addr)
  46. {
  47. if (addr >= (unsigned long)_sinittext &&
  48. addr <= (unsigned long)_einittext)
  49. return 1;
  50. return 0;
  51. }
  52. int core_kernel_text(unsigned long addr)
  53. {
  54. if (addr >= (unsigned long)_stext &&
  55. addr <= (unsigned long)_etext)
  56. return 1;
  57. if (system_state == SYSTEM_BOOTING &&
  58. init_kernel_text(addr))
  59. return 1;
  60. return 0;
  61. }
  62. /**
  63. * core_kernel_data - tell if addr points to kernel data
  64. * @addr: address to test
  65. *
  66. * Returns true if @addr passed in is from the core kernel data
  67. * section.
  68. *
  69. * Note: On some archs it may return true for core RODATA, and false
  70. * for others. But will always be true for core RW data.
  71. */
  72. int core_kernel_data(unsigned long addr)
  73. {
  74. if (addr >= (unsigned long)_sdata &&
  75. addr < (unsigned long)_edata)
  76. return 1;
  77. return 0;
  78. }
  79. int __kernel_text_address(unsigned long addr)
  80. {
  81. if (core_kernel_text(addr))
  82. return 1;
  83. if (is_module_text_address(addr))
  84. return 1;
  85. /*
  86. * There might be init symbols in saved stacktraces.
  87. * Give those symbols a chance to be printed in
  88. * backtraces (such as lockdep traces).
  89. *
  90. * Since we are after the module-symbols check, there's
  91. * no danger of address overlap:
  92. */
  93. if (init_kernel_text(addr))
  94. return 1;
  95. return 0;
  96. }
  97. int kernel_text_address(unsigned long addr)
  98. {
  99. if (core_kernel_text(addr))
  100. return 1;
  101. return is_module_text_address(addr);
  102. }
  103. /*
  104. * On some architectures (PPC64, IA64) function pointers
  105. * are actually only tokens to some data that then holds the
  106. * real function address. As a result, to find if a function
  107. * pointer is part of the kernel text, we need to do some
  108. * special dereferencing first.
  109. */
  110. int func_ptr_is_kernel_text(void *ptr)
  111. {
  112. unsigned long addr;
  113. addr = (unsigned long) dereference_function_descriptor(ptr);
  114. if (core_kernel_text(addr))
  115. return 1;
  116. return is_module_text_address(addr);
  117. }