extable.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/init.h>
  19. #include <asm/sections.h>
  20. #include <asm/uaccess.h>
  21. /*
  22. * mutex protecting text section modification (dynamic code patching).
  23. * some users need to sleep (allocating memory...) while they hold this lock.
  24. *
  25. * NOT exported to modules - patching kernel text is a really delicate matter.
  26. */
  27. DEFINE_MUTEX(text_mutex);
  28. extern struct exception_table_entry __start___ex_table[];
  29. extern struct exception_table_entry __stop___ex_table[];
  30. /* Sort the kernel's built-in exception table */
  31. void __init sort_main_extable(void)
  32. {
  33. sort_extable(__start___ex_table, __stop___ex_table);
  34. }
  35. /* Given an address, look for it in the exception tables. */
  36. const struct exception_table_entry *search_exception_tables(unsigned long addr)
  37. {
  38. const struct exception_table_entry *e;
  39. e = search_extable(__start___ex_table, __stop___ex_table-1, addr);
  40. if (!e)
  41. e = search_module_extables(addr);
  42. return e;
  43. }
  44. int core_kernel_text(unsigned long addr)
  45. {
  46. if (addr >= (unsigned long)_stext &&
  47. addr <= (unsigned long)_etext)
  48. return 1;
  49. if (system_state == SYSTEM_BOOTING &&
  50. addr >= (unsigned long)_sinittext &&
  51. addr <= (unsigned long)_einittext)
  52. return 1;
  53. return 0;
  54. }
  55. int __kernel_text_address(unsigned long addr)
  56. {
  57. if (core_kernel_text(addr))
  58. return 1;
  59. return __module_text_address(addr) != NULL;
  60. }
  61. int kernel_text_address(unsigned long addr)
  62. {
  63. if (core_kernel_text(addr))
  64. return 1;
  65. return module_text_address(addr) != NULL;
  66. }
  67. /*
  68. * On some architectures (PPC64, IA64) function pointers
  69. * are actually only tokens to some data that then holds the
  70. * real function address. As a result, to find if a function
  71. * pointer is part of the kernel text, we need to do some
  72. * special dereferencing first.
  73. */
  74. int func_ptr_is_kernel_text(void *ptr)
  75. {
  76. unsigned long addr;
  77. addr = (unsigned long) dereference_function_descriptor(ptr);
  78. if (core_kernel_text(addr))
  79. return 1;
  80. return module_text_address(addr) != NULL;
  81. }