GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / x86 / kernel / vmlinux.lds.S
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ld script for the x86 kernel
4  *
5  * Historic 32-bit version written by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6  *
7  * Modernisation, unification and other changes and fixes:
8  *   Copyright (C) 2007-2009  Sam Ravnborg <sam@ravnborg.org>
9  *
10  *
11  * Don't define absolute symbols until and unless you know that symbol
12  * value is should remain constant even if kernel image is relocated
13  * at run time. Absolute symbols are not relocated. If symbol value should
14  * change if kernel is relocated, make the symbol section relative and
15  * put it inside the section definition.
16  */
17
18 #ifdef CONFIG_X86_32
19 #define LOAD_OFFSET __PAGE_OFFSET
20 #else
21 #define LOAD_OFFSET __START_KERNEL_map
22 #endif
23
24 #define RUNTIME_DISCARD_EXIT
25
26 #include <asm-generic/vmlinux.lds.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/thread_info.h>
29 #include <asm/page_types.h>
30 #include <asm/orc_lookup.h>
31 #include <asm/cache.h>
32 #include <asm/boot.h>
33
34 #undef i386     /* in case the preprocessor is a 32bit one */
35
36 OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT)
37
38 #ifdef CONFIG_X86_32
39 OUTPUT_ARCH(i386)
40 ENTRY(phys_startup_32)
41 #else
42 OUTPUT_ARCH(i386:x86-64)
43 ENTRY(phys_startup_64)
44 #endif
45
46 jiffies = jiffies_64;
47
48 #if defined(CONFIG_X86_64)
49 /*
50  * On 64-bit, align RODATA to 2MB so we retain large page mappings for
51  * boundaries spanning kernel text, rodata and data sections.
52  *
53  * However, kernel identity mappings will have different RWX permissions
54  * to the pages mapping to text and to the pages padding (which are freed) the
55  * text section. Hence kernel identity mappings will be broken to smaller
56  * pages. For 64-bit, kernel text and kernel identity mappings are different,
57  * so we can enable protection checks as well as retain 2MB large page
58  * mappings for kernel text.
59  */
60 #define X86_ALIGN_RODATA_BEGIN  . = ALIGN(HPAGE_SIZE);
61
62 #define X86_ALIGN_RODATA_END                                    \
63                 . = ALIGN(HPAGE_SIZE);                          \
64                 __end_rodata_hpage_align = .;                   \
65                 __end_rodata_aligned = .;
66
67 #define ALIGN_ENTRY_TEXT_BEGIN  . = ALIGN(PMD_SIZE);
68 #define ALIGN_ENTRY_TEXT_END    . = ALIGN(PMD_SIZE);
69
70 /*
71  * This section contains data which will be mapped as decrypted. Memory
72  * encryption operates on a page basis. Make this section PMD-aligned
73  * to avoid splitting the pages while mapping the section early.
74  *
75  * Note: We use a separate section so that only this section gets
76  * decrypted to avoid exposing more than we wish.
77  */
78 #define BSS_DECRYPTED                                           \
79         . = ALIGN(PMD_SIZE);                                    \
80         __start_bss_decrypted = .;                              \
81         *(.bss..decrypted);                                     \
82         . = ALIGN(PAGE_SIZE);                                   \
83         __start_bss_decrypted_unused = .;                       \
84         . = ALIGN(PMD_SIZE);                                    \
85         __end_bss_decrypted = .;                                \
86
87 #else
88
89 #define X86_ALIGN_RODATA_BEGIN
90 #define X86_ALIGN_RODATA_END                                    \
91                 . = ALIGN(PAGE_SIZE);                           \
92                 __end_rodata_aligned = .;
93
94 #define ALIGN_ENTRY_TEXT_BEGIN
95 #define ALIGN_ENTRY_TEXT_END
96 #define BSS_DECRYPTED
97
98 #endif
99
100 PHDRS {
101         text PT_LOAD FLAGS(5);          /* R_E */
102         data PT_LOAD FLAGS(6);          /* RW_ */
103 #ifdef CONFIG_X86_64
104 #ifdef CONFIG_SMP
105         percpu PT_LOAD FLAGS(6);        /* RW_ */
106 #endif
107         init PT_LOAD FLAGS(7);          /* RWE */
108 #endif
109         note PT_NOTE FLAGS(0);          /* ___ */
110 }
111
112 SECTIONS
113 {
114 #ifdef CONFIG_X86_32
115         . = LOAD_OFFSET + LOAD_PHYSICAL_ADDR;
116         phys_startup_32 = ABSOLUTE(startup_32 - LOAD_OFFSET);
117 #else
118         . = __START_KERNEL;
119         phys_startup_64 = ABSOLUTE(startup_64 - LOAD_OFFSET);
120 #endif
121
122         /* Text and read-only data */
123         .text :  AT(ADDR(.text) - LOAD_OFFSET) {
124                 _text = .;
125                 _stext = .;
126                 /* bootstrapping code */
127                 HEAD_TEXT
128                 TEXT_TEXT
129                 SCHED_TEXT
130                 CPUIDLE_TEXT
131                 LOCK_TEXT
132                 KPROBES_TEXT
133                 ALIGN_ENTRY_TEXT_BEGIN
134                 ENTRY_TEXT
135                 IRQENTRY_TEXT
136                 ALIGN_ENTRY_TEXT_END
137                 SOFTIRQENTRY_TEXT
138                 *(.fixup)
139                 *(.gnu.warning)
140
141 #ifdef CONFIG_RETPOLINE
142                 __indirect_thunk_start = .;
143                 *(.text.__x86.indirect_thunk)
144                 __indirect_thunk_end = .;
145 #endif
146
147                 /* End of text section */
148                 _etext = .;
149         } :text = 0x9090
150
151         NOTES :text :note
152
153         EXCEPTION_TABLE(16) :text = 0x9090
154
155         /* .text should occupy whole number of pages */
156         . = ALIGN(PAGE_SIZE);
157         X86_ALIGN_RODATA_BEGIN
158         RO_DATA(PAGE_SIZE)
159         X86_ALIGN_RODATA_END
160
161         /* Data */
162         .data : AT(ADDR(.data) - LOAD_OFFSET) {
163                 /* Start of data section */
164                 _sdata = .;
165
166                 /* init_task */
167                 INIT_TASK_DATA(THREAD_SIZE)
168
169 #ifdef CONFIG_X86_32
170                 /* 32 bit has nosave before _edata */
171                 NOSAVE_DATA
172 #endif
173
174                 PAGE_ALIGNED_DATA(PAGE_SIZE)
175
176                 CACHELINE_ALIGNED_DATA(L1_CACHE_BYTES)
177
178                 DATA_DATA
179                 CONSTRUCTORS
180
181                 /* rarely changed data like cpu maps */
182                 READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES)
183
184                 /* End of data section */
185                 _edata = .;
186         } :data
187
188         BUG_TABLE
189
190         ORC_UNWIND_TABLE
191
192         . = ALIGN(PAGE_SIZE);
193         __vvar_page = .;
194
195         .vvar : AT(ADDR(.vvar) - LOAD_OFFSET) {
196                 /* work around gold bug 13023 */
197                 __vvar_beginning_hack = .;
198
199                 /* Place all vvars at the offsets in asm/vvar.h. */
200 #define EMIT_VVAR(name, offset)                         \
201                 . = __vvar_beginning_hack + offset;     \
202                 *(.vvar_ ## name)
203 #define __VVAR_KERNEL_LDS
204 #include <asm/vvar.h>
205 #undef __VVAR_KERNEL_LDS
206 #undef EMIT_VVAR
207
208                 /*
209                  * Pad the rest of the page with zeros.  Otherwise the loader
210                  * can leave garbage here.
211                  */
212                 . = __vvar_beginning_hack + PAGE_SIZE;
213         } :data
214
215         . = ALIGN(__vvar_page + PAGE_SIZE, PAGE_SIZE);
216
217         /* Init code and data - will be freed after init */
218         . = ALIGN(PAGE_SIZE);
219         .init.begin : AT(ADDR(.init.begin) - LOAD_OFFSET) {
220                 __init_begin = .; /* paired with __init_end */
221         }
222
223 #if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
224         /*
225          * percpu offsets are zero-based on SMP.  PERCPU_VADDR() changes the
226          * output PHDR, so the next output section - .init.text - should
227          * start another segment - init.
228          */
229         PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu)
230         ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START,
231                "per-CPU data too large - increase CONFIG_PHYSICAL_START")
232 #endif
233
234         INIT_TEXT_SECTION(PAGE_SIZE)
235 #ifdef CONFIG_X86_64
236         :init
237 #endif
238
239         /*
240          * Section for code used exclusively before alternatives are run. All
241          * references to such code must be patched out by alternatives, normally
242          * by using X86_FEATURE_ALWAYS CPU feature bit.
243          *
244          * See static_cpu_has() for an example.
245          */
246         .altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) {
247                 *(.altinstr_aux)
248         }
249
250         INIT_DATA_SECTION(16)
251
252         .x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
253                 __x86_cpu_dev_start = .;
254                 *(.x86_cpu_dev.init)
255                 __x86_cpu_dev_end = .;
256         }
257
258 #ifdef CONFIG_X86_INTEL_MID
259         .x86_intel_mid_dev.init : AT(ADDR(.x86_intel_mid_dev.init) - \
260                                                                 LOAD_OFFSET) {
261                 __x86_intel_mid_dev_start = .;
262                 *(.x86_intel_mid_dev.init)
263                 __x86_intel_mid_dev_end = .;
264         }
265 #endif
266
267         /*
268          * start address and size of operations which during runtime
269          * can be patched with virtualization friendly instructions or
270          * baremetal native ones. Think page table operations.
271          * Details in paravirt_types.h
272          */
273         . = ALIGN(8);
274         .parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
275                 __parainstructions = .;
276                 *(.parainstructions)
277                 __parainstructions_end = .;
278         }
279
280         /*
281          * struct alt_inst entries. From the header (alternative.h):
282          * "Alternative instructions for different CPU types or capabilities"
283          * Think locking instructions on spinlocks.
284          */
285         . = ALIGN(8);
286         .altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) {
287                 __alt_instructions = .;
288                 *(.altinstructions)
289                 __alt_instructions_end = .;
290         }
291
292         /*
293          * And here are the replacement instructions. The linker sticks
294          * them as binary blobs. The .altinstructions has enough data to
295          * get the address and the length of them to patch the kernel safely.
296          */
297         .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
298                 *(.altinstr_replacement)
299         }
300
301         /*
302          * struct iommu_table_entry entries are injected in this section.
303          * It is an array of IOMMUs which during run time gets sorted depending
304          * on its dependency order. After rootfs_initcall is complete
305          * this section can be safely removed.
306          */
307         .iommu_table : AT(ADDR(.iommu_table) - LOAD_OFFSET) {
308                 __iommu_table = .;
309                 *(.iommu_table)
310                 __iommu_table_end = .;
311         }
312
313         . = ALIGN(8);
314         .apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) {
315                 __apicdrivers = .;
316                 *(.apicdrivers);
317                 __apicdrivers_end = .;
318         }
319
320         . = ALIGN(8);
321         /*
322          * .exit.text is discard at runtime, not link time, to deal with
323          *  references from .altinstructions and .eh_frame
324          */
325         .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) {
326                 EXIT_TEXT
327         }
328
329         .exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) {
330                 EXIT_DATA
331         }
332
333 #if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP)
334         PERCPU_SECTION(INTERNODE_CACHE_BYTES)
335 #endif
336
337         . = ALIGN(PAGE_SIZE);
338
339         /* freed after init ends here */
340         .init.end : AT(ADDR(.init.end) - LOAD_OFFSET) {
341                 __init_end = .;
342         }
343
344         /*
345          * smp_locks might be freed after init
346          * start/end must be page aligned
347          */
348         . = ALIGN(PAGE_SIZE);
349         .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
350                 __smp_locks = .;
351                 *(.smp_locks)
352                 . = ALIGN(PAGE_SIZE);
353                 __smp_locks_end = .;
354         }
355
356 #ifdef CONFIG_X86_64
357         .data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) {
358                 NOSAVE_DATA
359         }
360 #endif
361
362         /* BSS */
363         . = ALIGN(PAGE_SIZE);
364         .bss : AT(ADDR(.bss) - LOAD_OFFSET) {
365                 __bss_start = .;
366                 *(.bss..page_aligned)
367                 . = ALIGN(PAGE_SIZE);
368                 *(BSS_MAIN)
369                 BSS_DECRYPTED
370                 . = ALIGN(PAGE_SIZE);
371                 __bss_stop = .;
372         }
373
374         /*
375          * The memory occupied from _text to here, __end_of_kernel_reserve, is
376          * automatically reserved in setup_arch(). Anything after here must be
377          * explicitly reserved using memblock_reserve() or it will be discarded
378          * and treated as available memory.
379          */
380         __end_of_kernel_reserve = .;
381
382         . = ALIGN(PAGE_SIZE);
383         .brk : AT(ADDR(.brk) - LOAD_OFFSET) {
384                 __brk_base = .;
385                 . += 64 * 1024;         /* 64k alignment slop space */
386                 *(.bss..brk)            /* areas brk users have reserved */
387                 __brk_limit = .;
388         }
389
390         . = ALIGN(PAGE_SIZE);           /* keep VO_INIT_SIZE page aligned */
391         _end = .;
392
393 #ifdef CONFIG_AMD_MEM_ENCRYPT
394         /*
395          * Early scratch/workarea section: Lives outside of the kernel proper
396          * (_text - _end).
397          *
398          * Resides after _end because even though the .brk section is after
399          * __end_of_kernel_reserve, the .brk section is later reserved as a
400          * part of the kernel. Since it is located after __end_of_kernel_reserve
401          * it will be discarded and become part of the available memory. As
402          * such, it can only be used by very early boot code and must not be
403          * needed afterwards.
404          *
405          * Currently used by SME for performing in-place encryption of the
406          * kernel during boot. Resides on a 2MB boundary to simplify the
407          * pagetable setup used for SME in-place encryption.
408          */
409         . = ALIGN(HPAGE_SIZE);
410         .init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
411                 __init_scratch_begin = .;
412                 *(.init.scratch)
413                 . = ALIGN(HPAGE_SIZE);
414                 __init_scratch_end = .;
415         }
416 #endif
417
418         STABS_DEBUG
419         DWARF_DEBUG
420
421         DISCARDS
422         /DISCARD/ : {
423                 *(.eh_frame)
424         }
425 }
426
427
428 #ifdef CONFIG_X86_32
429 /*
430  * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility:
431  */
432 . = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
433            "kernel image bigger than KERNEL_IMAGE_SIZE");
434 #else
435 /*
436  * Per-cpu symbols which need to be offset from __per_cpu_load
437  * for the boot processor.
438  */
439 #define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load
440 INIT_PER_CPU(gdt_page);
441 INIT_PER_CPU(fixed_percpu_data);
442 INIT_PER_CPU(irq_stack_backing_store);
443
444 /*
445  * Build-time check on the image size:
446  */
447 . = ASSERT((_end - _text <= KERNEL_IMAGE_SIZE),
448            "kernel image bigger than KERNEL_IMAGE_SIZE");
449
450 #ifdef CONFIG_SMP
451 . = ASSERT((fixed_percpu_data == 0),
452            "fixed_percpu_data is not at start of per-cpu area");
453 #endif
454
455 #endif /* CONFIG_X86_32 */
456
457 #ifdef CONFIG_KEXEC_CORE
458 #include <asm/kexec.h>
459
460 . = ASSERT(kexec_control_code_size <= KEXEC_CONTROL_CODE_MAX_SIZE,
461            "kexec control code size is too big");
462 #endif
463