sections.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_SECTIONS_H_
  3. #define _ASM_GENERIC_SECTIONS_H_
  4. /* References to section boundaries */
  5. #include <linux/compiler.h>
  6. #include <linux/types.h>
  7. /*
  8. * Usage guidelines:
  9. * _text, _data: architecture specific, don't use them in arch-independent code
  10. * [_stext, _etext]: contains .text.* sections, may also contain .rodata.*
  11. * and/or .init.* sections
  12. * [_sdata, _edata]: contains .data.* sections, may also contain .rodata.*
  13. * and/or .init.* sections.
  14. * [__start_rodata, __end_rodata]: contains .rodata.* sections
  15. * [__start_ro_after_init, __end_ro_after_init]:
  16. * contains .data..ro_after_init section
  17. * [__init_begin, __init_end]: contains .init.* sections, but .init.text.*
  18. * may be out of this range on some architectures.
  19. * [_sinittext, _einittext]: contains .init.text.* sections
  20. * [__bss_start, __bss_stop]: contains BSS sections
  21. *
  22. * Following global variables are optional and may be unavailable on some
  23. * architectures and/or kernel configurations.
  24. * _text, _data
  25. * __kprobes_text_start, __kprobes_text_end
  26. * __entry_text_start, __entry_text_end
  27. * __ctors_start, __ctors_end
  28. * __irqentry_text_start, __irqentry_text_end
  29. * __softirqentry_text_start, __softirqentry_text_end
  30. * __start_opd, __end_opd
  31. */
  32. extern char _text[], _stext[], _etext[];
  33. extern char _data[], _sdata[], _edata[];
  34. extern char __bss_start[], __bss_stop[];
  35. extern char __init_begin[], __init_end[];
  36. extern char _sinittext[], _einittext[];
  37. extern char __start_ro_after_init[], __end_ro_after_init[];
  38. extern char _end[];
  39. extern char __per_cpu_load[], __per_cpu_start[], __per_cpu_end[];
  40. extern char __kprobes_text_start[], __kprobes_text_end[];
  41. extern char __entry_text_start[], __entry_text_end[];
  42. extern char __start_rodata[], __end_rodata[];
  43. extern char __irqentry_text_start[], __irqentry_text_end[];
  44. extern char __softirqentry_text_start[], __softirqentry_text_end[];
  45. extern char __start_once[], __end_once[];
  46. /* Start and end of .ctors section - used for constructor calls. */
  47. extern char __ctors_start[], __ctors_end[];
  48. /* Start and end of .opd section - used for function descriptors. */
  49. extern char __start_opd[], __end_opd[];
  50. /* Start and end of instrumentation protected text section */
  51. extern char __noinstr_text_start[], __noinstr_text_end[];
  52. extern __visible const void __nosave_begin, __nosave_end;
  53. /* Function descriptor handling (if any). Override in asm/sections.h */
  54. #ifdef CONFIG_HAVE_FUNCTION_DESCRIPTORS
  55. void *dereference_function_descriptor(void *ptr);
  56. void *dereference_kernel_function_descriptor(void *ptr);
  57. #else
  58. #define dereference_function_descriptor(p) ((void *)(p))
  59. #define dereference_kernel_function_descriptor(p) ((void *)(p))
  60. /* An address is simply the address of the function. */
  61. typedef struct {
  62. unsigned long addr;
  63. } func_desc_t;
  64. #endif
  65. static inline bool have_function_descriptors(void)
  66. {
  67. return IS_ENABLED(CONFIG_HAVE_FUNCTION_DESCRIPTORS);
  68. }
  69. /**
  70. * memory_contains - checks if an object is contained within a memory region
  71. * @begin: virtual address of the beginning of the memory region
  72. * @end: virtual address of the end of the memory region
  73. * @virt: virtual address of the memory object
  74. * @size: size of the memory object
  75. *
  76. * Returns: true if the object specified by @virt and @size is entirely
  77. * contained within the memory region defined by @begin and @end, false
  78. * otherwise.
  79. */
  80. static inline bool memory_contains(void *begin, void *end, void *virt,
  81. size_t size)
  82. {
  83. return virt >= begin && virt + size <= end;
  84. }
  85. /**
  86. * memory_intersects - checks if the region occupied by an object intersects
  87. * with another memory region
  88. * @begin: virtual address of the beginning of the memory region
  89. * @end: virtual address of the end of the memory region
  90. * @virt: virtual address of the memory object
  91. * @size: size of the memory object
  92. *
  93. * Returns: true if an object's memory region, specified by @virt and @size,
  94. * intersects with the region specified by @begin and @end, false otherwise.
  95. */
  96. static inline bool memory_intersects(void *begin, void *end, void *virt,
  97. size_t size)
  98. {
  99. void *vend = virt + size;
  100. if (virt < end && vend > begin)
  101. return true;
  102. return false;
  103. }
  104. /**
  105. * init_section_contains - checks if an object is contained within the init
  106. * section
  107. * @virt: virtual address of the memory object
  108. * @size: size of the memory object
  109. *
  110. * Returns: true if the object specified by @virt and @size is entirely
  111. * contained within the init section, false otherwise.
  112. */
  113. static inline bool init_section_contains(void *virt, size_t size)
  114. {
  115. return memory_contains(__init_begin, __init_end, virt, size);
  116. }
  117. /**
  118. * init_section_intersects - checks if the region occupied by an object
  119. * intersects with the init section
  120. * @virt: virtual address of the memory object
  121. * @size: size of the memory object
  122. *
  123. * Returns: true if an object's memory region, specified by @virt and @size,
  124. * intersects with the init section, false otherwise.
  125. */
  126. static inline bool init_section_intersects(void *virt, size_t size)
  127. {
  128. return memory_intersects(__init_begin, __init_end, virt, size);
  129. }
  130. /**
  131. * is_kernel_core_data - checks if the pointer address is located in the
  132. * .data or .bss section
  133. *
  134. * @addr: address to check
  135. *
  136. * Returns: true if the address is located in .data or .bss, false otherwise.
  137. * Note: On some archs it may return true for core RODATA, and false
  138. * for others. But will always be true for core RW data.
  139. */
  140. static inline bool is_kernel_core_data(unsigned long addr)
  141. {
  142. if (addr >= (unsigned long)_sdata && addr < (unsigned long)_edata)
  143. return true;
  144. if (addr >= (unsigned long)__bss_start &&
  145. addr < (unsigned long)__bss_stop)
  146. return true;
  147. return false;
  148. }
  149. /**
  150. * is_kernel_rodata - checks if the pointer address is located in the
  151. * .rodata section
  152. *
  153. * @addr: address to check
  154. *
  155. * Returns: true if the address is located in .rodata, false otherwise.
  156. */
  157. static inline bool is_kernel_rodata(unsigned long addr)
  158. {
  159. return addr >= (unsigned long)__start_rodata &&
  160. addr < (unsigned long)__end_rodata;
  161. }
  162. /**
  163. * is_kernel_inittext - checks if the pointer address is located in the
  164. * .init.text section
  165. *
  166. * @addr: address to check
  167. *
  168. * Returns: true if the address is located in .init.text, false otherwise.
  169. */
  170. static inline bool is_kernel_inittext(unsigned long addr)
  171. {
  172. return addr >= (unsigned long)_sinittext &&
  173. addr < (unsigned long)_einittext;
  174. }
  175. /**
  176. * __is_kernel_text - checks if the pointer address is located in the
  177. * .text section
  178. *
  179. * @addr: address to check
  180. *
  181. * Returns: true if the address is located in .text, false otherwise.
  182. * Note: an internal helper, only check the range of _stext to _etext.
  183. */
  184. static inline bool __is_kernel_text(unsigned long addr)
  185. {
  186. return addr >= (unsigned long)_stext &&
  187. addr < (unsigned long)_etext;
  188. }
  189. /**
  190. * __is_kernel - checks if the pointer address is located in the kernel range
  191. *
  192. * @addr: address to check
  193. *
  194. * Returns: true if the address is located in the kernel range, false otherwise.
  195. * Note: an internal helper, check the range of _stext to _end,
  196. * and range from __init_begin to __init_end, which can be outside
  197. * of the _stext to _end range.
  198. */
  199. static inline bool __is_kernel(unsigned long addr)
  200. {
  201. return ((addr >= (unsigned long)_stext &&
  202. addr < (unsigned long)_end) ||
  203. (addr >= (unsigned long)__init_begin &&
  204. addr < (unsigned long)__init_end));
  205. }
  206. #endif /* _ASM_GENERIC_SECTIONS_H_ */