18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_GENERIC_SECTIONS_H_ 38c2ecf20Sopenharmony_ci#define _ASM_GENERIC_SECTIONS_H_ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* References to section boundaries */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/compiler.h> 88c2ecf20Sopenharmony_ci#include <linux/types.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci/* 118c2ecf20Sopenharmony_ci * Usage guidelines: 128c2ecf20Sopenharmony_ci * _text, _data: architecture specific, don't use them in arch-independent code 138c2ecf20Sopenharmony_ci * [_stext, _etext]: contains .text.* sections, may also contain .rodata.* 148c2ecf20Sopenharmony_ci * and/or .init.* sections 158c2ecf20Sopenharmony_ci * [_sdata, _edata]: contains .data.* sections, may also contain .rodata.* 168c2ecf20Sopenharmony_ci * and/or .init.* sections. 178c2ecf20Sopenharmony_ci * [__start_rodata, __end_rodata]: contains .rodata.* sections 188c2ecf20Sopenharmony_ci * [__start_ro_after_init, __end_ro_after_init]: 198c2ecf20Sopenharmony_ci * contains .data..ro_after_init section 208c2ecf20Sopenharmony_ci * [__init_begin, __init_end]: contains .init.* sections, but .init.text.* 218c2ecf20Sopenharmony_ci * may be out of this range on some architectures. 228c2ecf20Sopenharmony_ci * [_sinittext, _einittext]: contains .init.text.* sections 238c2ecf20Sopenharmony_ci * [__bss_start, __bss_stop]: contains BSS sections 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * Following global variables are optional and may be unavailable on some 268c2ecf20Sopenharmony_ci * architectures and/or kernel configurations. 278c2ecf20Sopenharmony_ci * _text, _data 288c2ecf20Sopenharmony_ci * __kprobes_text_start, __kprobes_text_end 298c2ecf20Sopenharmony_ci * __entry_text_start, __entry_text_end 308c2ecf20Sopenharmony_ci * __ctors_start, __ctors_end 318c2ecf20Sopenharmony_ci * __irqentry_text_start, __irqentry_text_end 328c2ecf20Sopenharmony_ci * __softirqentry_text_start, __softirqentry_text_end 338c2ecf20Sopenharmony_ci * __start_opd, __end_opd 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_ciextern char _text[], _stext[], _etext[]; 368c2ecf20Sopenharmony_ciextern char _data[], _sdata[], _edata[]; 378c2ecf20Sopenharmony_ciextern char __bss_start[], __bss_stop[]; 388c2ecf20Sopenharmony_ciextern char __init_begin[], __init_end[]; 398c2ecf20Sopenharmony_ciextern char _sinittext[], _einittext[]; 408c2ecf20Sopenharmony_ciextern char __start_ro_after_init[], __end_ro_after_init[]; 418c2ecf20Sopenharmony_ciextern char _end[]; 428c2ecf20Sopenharmony_ciextern char __per_cpu_load[], __per_cpu_start[], __per_cpu_end[]; 438c2ecf20Sopenharmony_ciextern char __kprobes_text_start[], __kprobes_text_end[]; 448c2ecf20Sopenharmony_ciextern char __entry_text_start[], __entry_text_end[]; 458c2ecf20Sopenharmony_ciextern char __start_rodata[], __end_rodata[]; 468c2ecf20Sopenharmony_ciextern char __irqentry_text_start[], __irqentry_text_end[]; 478c2ecf20Sopenharmony_ciextern char __softirqentry_text_start[], __softirqentry_text_end[]; 488c2ecf20Sopenharmony_ciextern char __start_once[], __end_once[]; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* Start and end of .ctors section - used for constructor calls. */ 518c2ecf20Sopenharmony_ciextern char __ctors_start[], __ctors_end[]; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* Start and end of .opd section - used for function descriptors. */ 548c2ecf20Sopenharmony_ciextern char __start_opd[], __end_opd[]; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* Start and end of instrumentation protected text section */ 578c2ecf20Sopenharmony_ciextern char __noinstr_text_start[], __noinstr_text_end[]; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciextern __visible const void __nosave_begin, __nosave_end; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* Function descriptor handling (if any). Override in asm/sections.h */ 628c2ecf20Sopenharmony_ci#ifndef dereference_function_descriptor 638c2ecf20Sopenharmony_ci#define dereference_function_descriptor(p) ((void *)(p)) 648c2ecf20Sopenharmony_ci#define dereference_kernel_function_descriptor(p) ((void *)(p)) 658c2ecf20Sopenharmony_ci#endif 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* random extra sections (if any). Override 688c2ecf20Sopenharmony_ci * in asm/sections.h */ 698c2ecf20Sopenharmony_ci#ifndef arch_is_kernel_text 708c2ecf20Sopenharmony_cistatic inline int arch_is_kernel_text(unsigned long addr) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci#endif 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#ifndef arch_is_kernel_data 778c2ecf20Sopenharmony_cistatic inline int arch_is_kernel_data(unsigned long addr) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci return 0; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci#endif 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* 848c2ecf20Sopenharmony_ci * Check if an address is part of freed initmem. This is needed on architectures 858c2ecf20Sopenharmony_ci * with virt == phys kernel mapping, for code that wants to check if an address 868c2ecf20Sopenharmony_ci * is part of a static object within [_stext, _end]. After initmem is freed, 878c2ecf20Sopenharmony_ci * memory can be allocated from it, and such allocations would then have 888c2ecf20Sopenharmony_ci * addresses within the range [_stext, _end]. 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_ci#ifndef arch_is_kernel_initmem_freed 918c2ecf20Sopenharmony_cistatic inline int arch_is_kernel_initmem_freed(unsigned long addr) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci return 0; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci#endif 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/** 988c2ecf20Sopenharmony_ci * memory_contains - checks if an object is contained within a memory region 998c2ecf20Sopenharmony_ci * @begin: virtual address of the beginning of the memory region 1008c2ecf20Sopenharmony_ci * @end: virtual address of the end of the memory region 1018c2ecf20Sopenharmony_ci * @virt: virtual address of the memory object 1028c2ecf20Sopenharmony_ci * @size: size of the memory object 1038c2ecf20Sopenharmony_ci * 1048c2ecf20Sopenharmony_ci * Returns: true if the object specified by @virt and @size is entirely 1058c2ecf20Sopenharmony_ci * contained within the memory region defined by @begin and @end, false 1068c2ecf20Sopenharmony_ci * otherwise. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_cistatic inline bool memory_contains(void *begin, void *end, void *virt, 1098c2ecf20Sopenharmony_ci size_t size) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci return virt >= begin && virt + size <= end; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/** 1158c2ecf20Sopenharmony_ci * memory_intersects - checks if the region occupied by an object intersects 1168c2ecf20Sopenharmony_ci * with another memory region 1178c2ecf20Sopenharmony_ci * @begin: virtual address of the beginning of the memory region 1188c2ecf20Sopenharmony_ci * @end: virtual address of the end of the memory region 1198c2ecf20Sopenharmony_ci * @virt: virtual address of the memory object 1208c2ecf20Sopenharmony_ci * @size: size of the memory object 1218c2ecf20Sopenharmony_ci * 1228c2ecf20Sopenharmony_ci * Returns: true if an object's memory region, specified by @virt and @size, 1238c2ecf20Sopenharmony_ci * intersects with the region specified by @begin and @end, false otherwise. 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_cistatic inline bool memory_intersects(void *begin, void *end, void *virt, 1268c2ecf20Sopenharmony_ci size_t size) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci void *vend = virt + size; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci if (virt < end && vend > begin) 1318c2ecf20Sopenharmony_ci return true; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci return false; 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci/** 1378c2ecf20Sopenharmony_ci * init_section_contains - checks if an object is contained within the init 1388c2ecf20Sopenharmony_ci * section 1398c2ecf20Sopenharmony_ci * @virt: virtual address of the memory object 1408c2ecf20Sopenharmony_ci * @size: size of the memory object 1418c2ecf20Sopenharmony_ci * 1428c2ecf20Sopenharmony_ci * Returns: true if the object specified by @virt and @size is entirely 1438c2ecf20Sopenharmony_ci * contained within the init section, false otherwise. 1448c2ecf20Sopenharmony_ci */ 1458c2ecf20Sopenharmony_cistatic inline bool init_section_contains(void *virt, size_t size) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci return memory_contains(__init_begin, __init_end, virt, size); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci/** 1518c2ecf20Sopenharmony_ci * init_section_intersects - checks if the region occupied by an object 1528c2ecf20Sopenharmony_ci * intersects with the init section 1538c2ecf20Sopenharmony_ci * @virt: virtual address of the memory object 1548c2ecf20Sopenharmony_ci * @size: size of the memory object 1558c2ecf20Sopenharmony_ci * 1568c2ecf20Sopenharmony_ci * Returns: true if an object's memory region, specified by @virt and @size, 1578c2ecf20Sopenharmony_ci * intersects with the init section, false otherwise. 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_cistatic inline bool init_section_intersects(void *virt, size_t size) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci return memory_intersects(__init_begin, __init_end, virt, size); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci/** 1658c2ecf20Sopenharmony_ci * is_kernel_rodata - checks if the pointer address is located in the 1668c2ecf20Sopenharmony_ci * .rodata section 1678c2ecf20Sopenharmony_ci * 1688c2ecf20Sopenharmony_ci * @addr: address to check 1698c2ecf20Sopenharmony_ci * 1708c2ecf20Sopenharmony_ci * Returns: true if the address is located in .rodata, false otherwise. 1718c2ecf20Sopenharmony_ci */ 1728c2ecf20Sopenharmony_cistatic inline bool is_kernel_rodata(unsigned long addr) 1738c2ecf20Sopenharmony_ci{ 1748c2ecf20Sopenharmony_ci return addr >= (unsigned long)__start_rodata && 1758c2ecf20Sopenharmony_ci addr < (unsigned long)__end_rodata; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci#endif /* _ASM_GENERIC_SECTIONS_H_ */ 179