xref: /optee_os/core/arch/arm/kernel/kern.ld.S (revision 4af447d4084e293800d4e463d65003c016b91f29)
1/* SPDX-License-Identifier: (BSD-2-Clause AND MIT) */
2/*
3 * Copyright (c) 2014, Linaro Limited
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 2008-2010 Travis Geiselbrecht
31 *
32 * Permission is hereby granted, free of charge, to any person obtaining
33 * a copy of this software and associated documentation files
34 * (the "Software"), to deal in the Software without restriction,
35 * including without limitation the rights to use, copy, modify, merge,
36 * publish, distribute, sublicense, and/or sell copies of the Software,
37 * and to permit persons to whom the Software is furnished to do so,
38 * subject to the following conditions:
39 *
40 * The above copyright notice and this permission notice shall be
41 * included in all copies or substantial portions of the Software.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 */
51
52#include <mm/core_mmu.h>
53#include <platform_config.h>
54#include <util.h>
55
56/*
57 * TEE_RAM_VA_START:            The start virtual address of the TEE RAM
58 * TEE_TEXT_VA_START:           The start virtual address of the OP-TEE text
59 */
60#define TEE_RAM_VA_START        TEE_RAM_START
61#define TEE_TEXT_VA_START       (TEE_RAM_VA_START + \
62					(TEE_LOAD_ADDR - TEE_RAM_START))
63
64/*
65 * Note:
66 * Clang 11 (ld.lld) generates non-relocatable reference when using ROUNDDOWN()
67 * from <util.h>, which does not work with ASLR.
68 */
69#define LD_ROUNDDOWN(x, y) ((x) - ((x) % (y)))
70
71OUTPUT_FORMAT(CFG_KERN_LINKER_FORMAT)
72OUTPUT_ARCH(CFG_KERN_LINKER_ARCH)
73
74ENTRY(_start)
75SECTIONS
76{
77	. = TEE_TEXT_VA_START;
78#ifdef ARM32
79	ASSERT(!(TEE_TEXT_VA_START & 31), "text start should align to 32bytes")
80#endif
81#ifdef ARM64
82	ASSERT(!(TEE_TEXT_VA_START & 127), "text start should align to 128bytes")
83#endif
84	__text_start = .;
85
86	/*
87	 * Memory between TEE_TEXT_VA_START and page aligned rounded down
88	 * value will be mapped with unpaged "text" section attributes:
89	 * likely to be read-only/executable.
90	 */
91	__flatmap_unpg_rx_start = LD_ROUNDDOWN(__text_start, SMALL_PAGE_SIZE);
92
93	.text : {
94		KEEP(*(.text._start))
95		__identity_map_init_start = .;
96		*(.identity_map .identity_map.* \
97			/*
98			 * The one below is needed because it's a weak
99			 * symbol that may be overridden by platform
100			 * specific code.
101			 */
102		  .text.get_core_pos_mpidr)
103		__identity_map_init_end = .;
104		KEEP(*(.text.init .text.plat_cpu_reset_early \
105		       .text.reset .text.reset_primary .text.unhandled_cpu \
106		       .text.__assert_flat_mapped_range))
107
108#ifdef CFG_WITH_PAGER
109		*(.text)
110/* Include list of sections needed for paging */
111#include <text_unpaged.ld.S>
112#else
113		*(.text .text.*)
114#endif
115		*(.sram.text.glue_7* .gnu.linkonce.t.*)
116		. = ALIGN(8);
117	}
118	__text_end = .;
119
120#ifdef CFG_CORE_RODATA_NOEXEC
121	. = ALIGN(SMALL_PAGE_SIZE);
122#endif
123	__flatmap_unpg_rx_size = . - __flatmap_unpg_rx_start;
124	__flatmap_unpg_ro_start = .;
125
126	.rodata : ALIGN(8) {
127		__rodata_start = .;
128		*(.gnu.linkonce.r.*)
129#ifdef CFG_WITH_PAGER
130		*(.rodata .rodata.__unpaged .rodata.__unpaged.*)
131#include <rodata_unpaged.ld.S>
132#else
133#ifdef CFG_DT
134		__rodata_dtdrv_start = .;
135		KEEP(*(.rodata.dtdrv))
136		__rodata_dtdrv_end = .;
137#endif
138		*(.rodata .rodata.*)
139		. = ALIGN(8);
140		KEEP(*(SORT(.scattered_array*)));
141#endif
142		. = ALIGN(8);
143		__rodata_end = .;
144	}
145
146	.got : { *(.got.plt) *(.got) }
147	.plt : { *(.plt) }
148
149	.ctors : ALIGN(8) {
150		__ctor_list = .;
151		KEEP(*(.ctors .ctors.* .init_array .init_array.*))
152		__ctor_end = .;
153	}
154	.dtors : ALIGN(8) {
155		__dtor_list = .;
156		KEEP(*(.dtors .dtors.* .fini_array .fini_array.*))
157		__dtor_end = .;
158	}
159
160	/* .ARM.exidx is sorted, so has to go in its own output section.  */
161	.ARM.exidx : {
162		__exidx_start = .;
163		*(.ARM.exidx* .gnu.linkonce.armexidx.*)
164		__exidx_end = .;
165	}
166
167	.ARM.extab : {
168		__extab_start = .;
169		*(.ARM.extab*)
170		__extab_end = .;
171	}
172
173	/* Start page aligned read-write memory */
174#ifdef CFG_CORE_RWDATA_NOEXEC
175	. = ALIGN(SMALL_PAGE_SIZE);
176#endif
177	__flatmap_unpg_ro_size = . - __flatmap_unpg_ro_start;
178
179#ifdef CFG_VIRTUALIZATION
180	__flatmap_nex_rw_start = . ;
181	.nex_data : ALIGN(8) {
182		*(.nex_data .nex_data.*)
183	}
184
185	.nex_bss : ALIGN(8) {
186		__nex_bss_start = .;
187		*(.nex_bss .nex_bss.*)
188		__nex_bss_end = .;
189	}
190
191	/*
192	 * We want to keep all nexus memory in one place, because
193	 * it should be always mapped and it is easier to map one
194	 * memory region than two.
195	 * Next section are NOLOAD ones, but they are followed
196	 * by sections with data. Thus, this NOLOAD section will
197	 * be included in the resulting binary, filled with zeroes
198	 */
199	.nex_stack (NOLOAD) : {
200		__nozi_stack_start = .;
201		KEEP(*(.nozi_stack.stack_tmp .nozi_stack.stack_abt))
202		. = ALIGN(8);
203		__nozi_stack_end = .;
204	}
205
206	.nex_heap (NOLOAD) : {
207		__nex_heap_start = .;
208		. += CFG_CORE_NEX_HEAP_SIZE;
209		. = ALIGN(16 * 1024);
210		__nex_heap_end = .;
211	}
212	.nex_nozi (NOLOAD) : {
213		ASSERT(!(ABSOLUTE(.) & (16 * 1024 - 1)), "align nozi to 16kB");
214		KEEP(*(.nozi.mmu.base_table .nozi.mmu.l2))
215	}
216
217	. = ALIGN(SMALL_PAGE_SIZE);
218
219	__flatmap_nex_rw_size = . - __flatmap_nex_rw_start;
220	__flatmap_nex_rw_end = .;
221#endif
222
223	__flatmap_unpg_rw_start = .;
224
225	.data : ALIGN(8) {
226		/* writable data  */
227		__data_start_rom = .;
228		/* in one segment binaries, the rom data address is on top
229		   of the ram data address */
230		__data_start = .;
231		*(.data .data.* .gnu.linkonce.d.*)
232		. = ALIGN(8);
233	}
234
235	/* unintialized data */
236	.bss : {
237		__data_end = .;
238		__bss_start = .;
239		*(.bss .bss.*)
240		*(.gnu.linkonce.b.*)
241		*(COMMON)
242		. = ALIGN(8);
243		__bss_end = .;
244	}
245
246	.heap1 (NOLOAD) : {
247		/*
248		 * We're keeping track of the padding added before the
249		 * .nozi section so we can do something useful with
250		 * this otherwise wasted memory.
251		 */
252		__heap1_start = .;
253#ifndef CFG_WITH_PAGER
254		. += CFG_CORE_HEAP_SIZE;
255#endif
256#ifdef CFG_WITH_LPAE
257		. = ALIGN(4 * 1024);
258#else
259		. = ALIGN(16 * 1024);
260#endif
261		__heap1_end = .;
262	}
263	/*
264	 * Uninitialized data that shouldn't be zero initialized at
265	 * runtime.
266	 *
267	 * L1 mmu table requires 16 KiB alignment
268	 */
269	.nozi (NOLOAD) : {
270		__nozi_start = .;
271		KEEP(*(.nozi .nozi.*))
272		. = ALIGN(16);
273		__nozi_end = .;
274		/*
275		 * If virtualization is enabled, abt and tmp stacks will placed
276		 * at above .nex_stack section and thread stacks will go there
277		 */
278		__nozi_stack_start = .;
279		KEEP(*(.nozi_stack .nozi_stack.*))
280		. = ALIGN(8);
281		__nozi_stack_end = .;
282	}
283
284#ifdef CFG_WITH_PAGER
285	.heap2 (NOLOAD) : {
286		__heap2_start = .;
287		/*
288		 * Reserve additional memory for heap, the total should be
289		 * at least CFG_CORE_HEAP_SIZE, but count what has already
290		 * been reserved in .heap1
291		 */
292		. += CFG_CORE_HEAP_SIZE - (__heap1_end - __heap1_start);
293		. = ALIGN(SMALL_PAGE_SIZE);
294		__heap2_end = .;
295	}
296
297	/* Start page aligned read-only memory */
298	__flatmap_unpg_rw_size = . - __flatmap_unpg_rw_start;
299
300	__init_start = .;
301	__flatmap_init_rx_start = .;
302
303	ASSERT(!(__flatmap_init_rx_start & (SMALL_PAGE_SIZE - 1)),
304		"read-write memory is not paged aligned")
305
306	.text_init : {
307/*
308 * Include list of sections needed for boot initialization, this list
309 * overlaps with unpaged.ld.S but since unpaged.ld.S is first all those
310 * sections will go into the unpaged area.
311 */
312#include <text_init.ld.S>
313		KEEP(*(.text.startup.*));
314		/* Make sure constructor functions are available during init */
315		KEEP(*(.text._GLOBAL__sub_*));
316		. = ALIGN(8);
317	}
318
319#ifdef CFG_CORE_RODATA_NOEXEC
320	. = ALIGN(SMALL_PAGE_SIZE);
321#endif
322	__flatmap_init_rx_size = . - __flatmap_init_rx_start;
323	__flatmap_init_ro_start = .;
324
325	.rodata_init : {
326#include <rodata_init.ld.S>
327
328		. = ALIGN(8);
329		KEEP(*(SORT(.scattered_array*)));
330
331		. = ALIGN(8);
332		__rodata_init_end = .;
333	}
334
335	__init_end = ALIGN(__rodata_init_end, SMALL_PAGE_SIZE);
336	__get_tee_init_end = __init_end;
337	__init_size = __init_end - __init_start;
338
339	/* vcore flat map stops here. No need to page align, rodata follows. */
340	__flatmap_init_ro_size = __init_end - __flatmap_init_ro_start;
341
342	.rodata_pageable : ALIGN(8) {
343#ifdef CFG_DT
344		__rodata_dtdrv_start = .;
345		KEEP(*(.rodata.dtdrv))
346		__rodata_dtdrv_end = .;
347#endif
348		*(.rodata*)
349	}
350
351#ifdef CFG_CORE_RODATA_NOEXEC
352	. = ALIGN(SMALL_PAGE_SIZE);
353#endif
354
355	.text_pageable : ALIGN(8) {
356		*(.text*)
357		. = ALIGN(SMALL_PAGE_SIZE);
358	}
359
360	__pageable_part_end = .;
361	__pageable_part_start = __init_end;
362	__pageable_start = __init_start;
363	__pageable_end = __pageable_part_end;
364
365	ASSERT(TEE_LOAD_ADDR >= TEE_RAM_START,
366		"Load address before start of physical memory")
367	ASSERT(TEE_LOAD_ADDR < (TEE_RAM_START + TEE_RAM_PH_SIZE),
368		"Load address after end of physical memory")
369	ASSERT((TEE_RAM_VA_START + TEE_RAM_PH_SIZE - __init_end) >
370		SMALL_PAGE_SIZE, "Too few free pages to initialize paging")
371
372
373#endif /*CFG_WITH_PAGER*/
374
375#ifdef CFG_CORE_SANITIZE_KADDRESS
376	. = TEE_RAM_VA_START + (TEE_RAM_VA_SIZE * 8) / 9 - 8;
377	. = ALIGN(8);
378	.asan_shadow : {
379		__asan_shadow_start = .;
380		. += TEE_RAM_VA_SIZE / 9;
381		__asan_shadow_end = .;
382		__asan_shadow_size = __asan_shadow_end - __asan_shadow_start;
383	}
384#endif /*CFG_CORE_SANITIZE_KADDRESS*/
385
386	__end = .;
387
388#ifndef CFG_WITH_PAGER
389	__init_size = __data_end - TEE_TEXT_VA_START;
390#endif
391	/*
392	 * Guard against moving the location counter backwards in the assignment
393	 * below.
394	 */
395	ASSERT(. <= (TEE_RAM_VA_START + TEE_RAM_VA_SIZE),
396		"TEE_RAM_VA_SIZE is too small")
397	. = TEE_RAM_VA_START + TEE_RAM_VA_SIZE;
398
399	_end_of_ram = .;
400
401#ifndef CFG_WITH_PAGER
402	__flatmap_unpg_rw_size = _end_of_ram - __flatmap_unpg_rw_start;
403	__get_tee_init_end = .;
404#endif
405
406	/*
407	 * These regions will not become a normal part of the dumped
408	 * binary, instead some are interpreted by the dump script and
409	 * converted into suitable format for OP-TEE itself to use.
410	 */
411	.dynamic : { *(.dynamic) }
412	.hash : { *(.hash) }
413	.dynsym : { *(.dynsym) }
414	.dynstr : { *(.dynstr) }
415
416	.rel : {
417		*(.rel.*)
418	}
419	.rela : {
420		*(.rela.*)
421	}
422#ifndef CFG_CORE_ASLR
423	ASSERT(SIZEOF(.rel) == 0, "Relocation entries not expected")
424	ASSERT(SIZEOF(.rela) == 0, "Relocation entries not expected")
425#endif
426
427	/DISCARD/ : {
428		/* Strip unnecessary stuff */
429		*(.comment .note .eh_frame .interp)
430		/* Strip meta variables */
431		*(__keep_meta_vars*)
432	}
433
434}
435
436/* Unpaged read-only memories */
437__vcore_unpg_rx_start = __flatmap_unpg_rx_start;
438__vcore_unpg_ro_start = __flatmap_unpg_ro_start;
439#ifdef CFG_CORE_RODATA_NOEXEC
440__vcore_unpg_rx_size = __flatmap_unpg_rx_size;
441__vcore_unpg_ro_size = __flatmap_unpg_ro_size;
442#else
443__vcore_unpg_rx_size = __flatmap_unpg_rx_size + __flatmap_unpg_ro_size;
444__vcore_unpg_ro_size = 0;
445#endif
446__vcore_unpg_rx_end = __vcore_unpg_rx_start + __vcore_unpg_rx_size;
447__vcore_unpg_ro_end = __vcore_unpg_ro_start + __vcore_unpg_ro_size;
448
449/* Unpaged read-write memory */
450__vcore_unpg_rw_start = __flatmap_unpg_rw_start;
451__vcore_unpg_rw_size = __flatmap_unpg_rw_size;
452__vcore_unpg_rw_end = __vcore_unpg_rw_start + __vcore_unpg_rw_size;
453
454#ifdef CFG_VIRTUALIZATION
455/* Nexus read-write memory */
456__vcore_nex_rw_start = __flatmap_nex_rw_start;
457__vcore_nex_rw_size = __flatmap_nex_rw_size;
458__vcore_nex_rw_end = __vcore_nex_rw_start + __vcore_nex_rw_size;
459#endif
460
461#ifdef CFG_WITH_PAGER
462/*
463 * Core init mapping shall cover up to end of the physical RAM.
464 * This is required since the hash table is appended to the
465 * binary data after the firmware build sequence.
466 */
467#define __FLATMAP_PAGER_TRAILING_SPACE	\
468	(TEE_RAM_START + TEE_RAM_PH_SIZE - \
469		(__flatmap_init_ro_start + __flatmap_init_ro_size))
470
471/* Paged/init read-only memories */
472__vcore_init_rx_start = __flatmap_init_rx_start;
473__vcore_init_ro_start = __flatmap_init_ro_start;
474#ifdef CFG_CORE_RODATA_NOEXEC
475__vcore_init_rx_size = __flatmap_init_rx_size;
476__vcore_init_ro_size = __flatmap_init_ro_size + __FLATMAP_PAGER_TRAILING_SPACE;
477#else
478__vcore_init_rx_size = __flatmap_init_rx_size + __flatmap_init_ro_size +
479		       __FLATMAP_PAGER_TRAILING_SPACE;
480__vcore_init_ro_size = 0;
481#endif /* CFG_CORE_RODATA_NOEXEC */
482__vcore_init_rx_end = __vcore_init_rx_start + __vcore_init_rx_size;
483__vcore_init_ro_end = __vcore_init_ro_start + __vcore_init_ro_size;
484#endif /* CFG_WITH_PAGER */
485
486#ifdef CFG_CORE_SANITIZE_KADDRESS
487__asan_map_start = (__asan_shadow_start / SMALL_PAGE_SIZE) *
488		   SMALL_PAGE_SIZE;
489__asan_map_end = ((__asan_shadow_end - 1) / SMALL_PAGE_SIZE) *
490		 SMALL_PAGE_SIZE + SMALL_PAGE_SIZE;
491__asan_map_size = __asan_map_end - __asan_map_start;
492#endif /*CFG_CORE_SANITIZE_KADDRESS*/
493