• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
36OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT)
37
38#ifdef CONFIG_X86_32
39OUTPUT_ARCH(i386)
40ENTRY(phys_startup_32)
41#else
42OUTPUT_ARCH(i386:x86-64)
43ENTRY(phys_startup_64)
44#endif
45
46jiffies = 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
100PHDRS {
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
112SECTIONS
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#ifdef CONFIG_CFI_CLANG
148		. = ALIGN(PAGE_SIZE);
149		__cfi_jt_start = .;
150		*(.text..L.cfi.jumptable .text..L.cfi.jumptable.*)
151		__cfi_jt_end = .;
152#endif
153	} :text = 0x9090
154
155	NOTES :text :note
156
157	EXCEPTION_TABLE(16) :text = 0x9090
158
159	/* End of text section, which should occupy whole number of pages */
160	_etext = .;
161	. = ALIGN(PAGE_SIZE);
162
163	X86_ALIGN_RODATA_BEGIN
164	RO_DATA(PAGE_SIZE)
165	X86_ALIGN_RODATA_END
166
167	/* Data */
168	.data : AT(ADDR(.data) - LOAD_OFFSET) {
169		/* Start of data section */
170		_sdata = .;
171
172		/* init_task */
173		INIT_TASK_DATA(THREAD_SIZE)
174
175#ifdef CONFIG_X86_32
176		/* 32 bit has nosave before _edata */
177		NOSAVE_DATA
178#endif
179
180		PAGE_ALIGNED_DATA(PAGE_SIZE)
181
182		CACHELINE_ALIGNED_DATA(L1_CACHE_BYTES)
183
184		DATA_DATA
185		CONSTRUCTORS
186
187		/* rarely changed data like cpu maps */
188		READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES)
189
190		/* End of data section */
191		_edata = .;
192	} :data
193
194	BUG_TABLE
195
196	ORC_UNWIND_TABLE
197
198	. = ALIGN(PAGE_SIZE);
199	__vvar_page = .;
200
201	.vvar : AT(ADDR(.vvar) - LOAD_OFFSET) {
202		/* work around gold bug 13023 */
203		__vvar_beginning_hack = .;
204
205		/* Place all vvars at the offsets in asm/vvar.h. */
206#define EMIT_VVAR(name, offset) 			\
207		. = __vvar_beginning_hack + offset;	\
208		*(.vvar_ ## name)
209#define __VVAR_KERNEL_LDS
210#include <asm/vvar.h>
211#undef __VVAR_KERNEL_LDS
212#undef EMIT_VVAR
213
214		/*
215		 * Pad the rest of the page with zeros.  Otherwise the loader
216		 * can leave garbage here.
217		 */
218		. = __vvar_beginning_hack + PAGE_SIZE;
219	} :data
220
221	. = ALIGN(__vvar_page + PAGE_SIZE, PAGE_SIZE);
222
223	/* Init code and data - will be freed after init */
224	. = ALIGN(PAGE_SIZE);
225	.init.begin : AT(ADDR(.init.begin) - LOAD_OFFSET) {
226		__init_begin = .; /* paired with __init_end */
227	}
228
229#if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
230	/*
231	 * percpu offsets are zero-based on SMP.  PERCPU_VADDR() changes the
232	 * output PHDR, so the next output section - .init.text - should
233	 * start another segment - init.
234	 */
235	PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu)
236	ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START,
237	       "per-CPU data too large - increase CONFIG_PHYSICAL_START")
238#endif
239
240	INIT_TEXT_SECTION(PAGE_SIZE)
241#ifdef CONFIG_X86_64
242	:init
243#endif
244
245	/*
246	 * Section for code used exclusively before alternatives are run. All
247	 * references to such code must be patched out by alternatives, normally
248	 * by using X86_FEATURE_ALWAYS CPU feature bit.
249	 *
250	 * See static_cpu_has() for an example.
251	 */
252	.altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) {
253		*(.altinstr_aux)
254	}
255
256	INIT_DATA_SECTION(16)
257
258	.x86_cpu_dev.init : AT(ADDR(.x86_cpu_dev.init) - LOAD_OFFSET) {
259		__x86_cpu_dev_start = .;
260		*(.x86_cpu_dev.init)
261		__x86_cpu_dev_end = .;
262	}
263
264#ifdef CONFIG_X86_INTEL_MID
265	.x86_intel_mid_dev.init : AT(ADDR(.x86_intel_mid_dev.init) - \
266								LOAD_OFFSET) {
267		__x86_intel_mid_dev_start = .;
268		*(.x86_intel_mid_dev.init)
269		__x86_intel_mid_dev_end = .;
270	}
271#endif
272
273	/*
274	 * start address and size of operations which during runtime
275	 * can be patched with virtualization friendly instructions or
276	 * baremetal native ones. Think page table operations.
277	 * Details in paravirt_types.h
278	 */
279	. = ALIGN(8);
280	.parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
281		__parainstructions = .;
282		*(.parainstructions)
283		__parainstructions_end = .;
284	}
285
286	/*
287	 * struct alt_inst entries. From the header (alternative.h):
288	 * "Alternative instructions for different CPU types or capabilities"
289	 * Think locking instructions on spinlocks.
290	 */
291	. = ALIGN(8);
292	.altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) {
293		__alt_instructions = .;
294		*(.altinstructions)
295		__alt_instructions_end = .;
296	}
297
298	/*
299	 * And here are the replacement instructions. The linker sticks
300	 * them as binary blobs. The .altinstructions has enough data to
301	 * get the address and the length of them to patch the kernel safely.
302	 */
303	.altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
304		*(.altinstr_replacement)
305	}
306
307	/*
308	 * struct iommu_table_entry entries are injected in this section.
309	 * It is an array of IOMMUs which during run time gets sorted depending
310	 * on its dependency order. After rootfs_initcall is complete
311	 * this section can be safely removed.
312	 */
313	.iommu_table : AT(ADDR(.iommu_table) - LOAD_OFFSET) {
314		__iommu_table = .;
315		*(.iommu_table)
316		__iommu_table_end = .;
317	}
318
319	. = ALIGN(8);
320	.apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) {
321		__apicdrivers = .;
322		*(.apicdrivers);
323		__apicdrivers_end = .;
324	}
325
326	. = ALIGN(8);
327	/*
328	 * .exit.text is discard at runtime, not link time, to deal with
329	 *  references from .altinstructions and .eh_frame
330	 */
331	.exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) {
332		EXIT_TEXT
333	}
334
335	.exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) {
336		EXIT_DATA
337	}
338
339#if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP)
340	PERCPU_SECTION(INTERNODE_CACHE_BYTES)
341#endif
342
343	. = ALIGN(PAGE_SIZE);
344
345	/* freed after init ends here */
346	.init.end : AT(ADDR(.init.end) - LOAD_OFFSET) {
347		__init_end = .;
348	}
349
350	/*
351	 * smp_locks might be freed after init
352	 * start/end must be page aligned
353	 */
354	. = ALIGN(PAGE_SIZE);
355	.smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
356		__smp_locks = .;
357		*(.smp_locks)
358		. = ALIGN(PAGE_SIZE);
359		__smp_locks_end = .;
360	}
361
362#ifdef CONFIG_X86_64
363	.data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) {
364		NOSAVE_DATA
365	}
366#endif
367
368	/* BSS */
369	. = ALIGN(PAGE_SIZE);
370	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {
371		__bss_start = .;
372		*(.bss..page_aligned)
373		. = ALIGN(PAGE_SIZE);
374		*(BSS_MAIN)
375		BSS_DECRYPTED
376		. = ALIGN(PAGE_SIZE);
377		__bss_stop = .;
378	}
379
380	/*
381	 * The memory occupied from _text to here, __end_of_kernel_reserve, is
382	 * automatically reserved in setup_arch(). Anything after here must be
383	 * explicitly reserved using memblock_reserve() or it will be discarded
384	 * and treated as available memory.
385	 */
386	__end_of_kernel_reserve = .;
387
388	. = ALIGN(PAGE_SIZE);
389	.brk : AT(ADDR(.brk) - LOAD_OFFSET) {
390		__brk_base = .;
391		. += 64 * 1024;		/* 64k alignment slop space */
392		*(.bss..brk)		/* areas brk users have reserved */
393		__brk_limit = .;
394	}
395
396	. = ALIGN(PAGE_SIZE);		/* keep VO_INIT_SIZE page aligned */
397	_end = .;
398
399#ifdef CONFIG_AMD_MEM_ENCRYPT
400	/*
401	 * Early scratch/workarea section: Lives outside of the kernel proper
402	 * (_text - _end).
403	 *
404	 * Resides after _end because even though the .brk section is after
405	 * __end_of_kernel_reserve, the .brk section is later reserved as a
406	 * part of the kernel. Since it is located after __end_of_kernel_reserve
407	 * it will be discarded and become part of the available memory. As
408	 * such, it can only be used by very early boot code and must not be
409	 * needed afterwards.
410	 *
411	 * Currently used by SME for performing in-place encryption of the
412	 * kernel during boot. Resides on a 2MB boundary to simplify the
413	 * pagetable setup used for SME in-place encryption.
414	 */
415	. = ALIGN(HPAGE_SIZE);
416	.init.scratch : AT(ADDR(.init.scratch) - LOAD_OFFSET) {
417		__init_scratch_begin = .;
418		*(.init.scratch)
419		. = ALIGN(HPAGE_SIZE);
420		__init_scratch_end = .;
421	}
422#endif
423
424	STABS_DEBUG
425	DWARF_DEBUG
426
427	DISCARDS
428	/DISCARD/ : {
429		*(.eh_frame)
430	}
431}
432
433
434#ifdef CONFIG_X86_32
435/*
436 * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility:
437 */
438. = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
439	   "kernel image bigger than KERNEL_IMAGE_SIZE");
440#else
441/*
442 * Per-cpu symbols which need to be offset from __per_cpu_load
443 * for the boot processor.
444 */
445#define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load
446INIT_PER_CPU(gdt_page);
447INIT_PER_CPU(fixed_percpu_data);
448INIT_PER_CPU(irq_stack_backing_store);
449
450/*
451 * Build-time check on the image size:
452 */
453. = ASSERT((_end - _text <= KERNEL_IMAGE_SIZE),
454	   "kernel image bigger than KERNEL_IMAGE_SIZE");
455
456#ifdef CONFIG_SMP
457. = ASSERT((fixed_percpu_data == 0),
458           "fixed_percpu_data is not at start of per-cpu area");
459#endif
460
461#endif /* CONFIG_X86_32 */
462
463#ifdef CONFIG_KEXEC_CORE
464#include <asm/kexec.h>
465
466. = ASSERT(kexec_control_code_size <= KEXEC_CONTROL_CODE_MAX_SIZE,
467           "kexec control code size is too big");
468#endif
469
470#ifdef CONFIG_CFI_CLANG
471. = ASSERT((__cfi_jt_end - __cfi_jt_start > 0),
472	   "CFI jump table is empty");
473#endif
474