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