blob: db88e7f0fc459b67f7570a691e3f7cbb1fb1dc6f [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001From f189457b79989543f65b8a4e8729eff2cdf9a758 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sat, 13 Aug 2022 19:24:55 -0700
4Subject: [PATCH] core: Define section attributes for clang
5
6Clang's attribute section is not same as gcc, here we need to add flags
7to sections so they can be eventually collected by linker into final
8output segments. Only way to do so with clang is to use
9
10pragma clang section ...
11
12The behavious is described here [1], this allows us to define names bss
13sections. This was not an issue until clang-15 where LLD linker starts
14to detect the section flags before merging them and throws the following
15errors
16
17| ld.lld: error: section type mismatch for .nozi.kdata_page
18| >>> /mnt/b/yoe/master/build/tmp/work/qemuarm64-yoe-linux/optee-os-tadevkit/3.17.0-r0/build/core/arch/arm/kernel/thread.o:(.nozi.kdata_page): SHT_PROGBITS
19| >>> output section .nozi: SHT_NOBITS
20|
21| ld.lld: error: section type mismatch for .nozi.mmu.l2
22| >>> /mnt/b/yoe/master/build/tmp/work/qemuarm64-yoe-linux/optee-os-tadevkit/3.17.0-r0/build/core/arch/arm/mm/core_mmu_lpae.o:(.nozi.mmu.l2): SHT_PROGBITS
23| >>> output section .nozi: SHT_NOBITS
24
25These sections should be carrying SHT_NOBITS but so far it was not
26possible to do so, this patch tries to use clangs pragma to get this
27going and match the functionality with gcc.
28
29[1] https://intel.github.io/llvm-docs/clang/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section
30
31Upstream-Status: Pending
32Signed-off-by: Khem Raj <raj.khem@gmail.com>
33---
34 core/arch/arm/kernel/thread.c | 19 +++++++++++++++--
35 core/arch/arm/mm/core_mmu_lpae.c | 35 ++++++++++++++++++++++++++++----
36 core/arch/arm/mm/pgt_cache.c | 12 ++++++++++-
37 core/kernel/thread.c | 13 +++++++++++-
38 4 files changed, 71 insertions(+), 8 deletions(-)
39
40diff --git a/core/arch/arm/kernel/thread.c b/core/arch/arm/kernel/thread.c
41index f083b159e..432983c86 100644
42--- a/core/arch/arm/kernel/thread.c
43+++ b/core/arch/arm/kernel/thread.c
44@@ -44,15 +44,30 @@ static size_t thread_user_kcode_size __nex_bss;
45 #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \
46 defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64)
47 long thread_user_kdata_sp_offset __nex_bss;
48+#ifdef __clang__
49+#ifndef CFG_VIRTUALIZATION
50+#pragma clang section bss=".nozi.kdata_page"
51+#else
52+#pragma clang section bss=".nex_nozi.kdata_page"
53+#endif
54+#endif
55 static uint8_t thread_user_kdata_page[
56 ROUNDUP(sizeof(struct thread_core_local) * CFG_TEE_CORE_NB_CORE,
57 SMALL_PAGE_SIZE)]
58 __aligned(SMALL_PAGE_SIZE)
59+#ifndef __clang__
60 #ifndef CFG_VIRTUALIZATION
61- __section(".nozi.kdata_page");
62+ __section(".nozi.kdata_page")
63 #else
64- __section(".nex_nozi.kdata_page");
65+ __section(".nex_nozi.kdata_page")
66 #endif
67+#endif
68+ ;
69+#endif
70+
71+/* reset BSS section to default ( .bss ) */
72+#ifdef __clang__
73+#pragma clang section bss=""
74 #endif
75
76 #ifdef ARM32
77diff --git a/core/arch/arm/mm/core_mmu_lpae.c b/core/arch/arm/mm/core_mmu_lpae.c
78index 19cd7b61b..78f5910c5 100644
79--- a/core/arch/arm/mm/core_mmu_lpae.c
80+++ b/core/arch/arm/mm/core_mmu_lpae.c
81@@ -230,19 +230,46 @@ typedef uint16_t l1_idx_t;
82 typedef uint64_t base_xlat_tbls_t[CFG_TEE_CORE_NB_CORE][NUM_BASE_LEVEL_ENTRIES];
83 typedef uint64_t xlat_tbl_t[XLAT_TABLE_ENTRIES];
84
85+#ifdef __clang__
86+#pragma clang section bss=".nozi.mmu.base_table"
87+#endif
88 static base_xlat_tbls_t base_xlation_table[NUM_BASE_TABLES]
89 __aligned(NUM_BASE_LEVEL_ENTRIES * XLAT_ENTRY_SIZE)
90- __section(".nozi.mmu.base_table");
91+#ifndef __clang__
92+ __section(".nozi.mmu.base_table")
93+#endif
94+;
95+#ifdef __clang__
96+#pragma clang section bss=""
97+#endif
98
99+#ifdef __clang__
100+#pragma clang section bss=".nozi.mmu.l2"
101+#endif
102 static xlat_tbl_t xlat_tables[MAX_XLAT_TABLES]
103- __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2");
104+ __aligned(XLAT_TABLE_SIZE)
105+#ifndef __clang__
106+ __section(".nozi.mmu.l2")
107+#endif
108+;
109+#ifdef __clang__
110+#pragma clang section bss=""
111+#endif
112
113 #define XLAT_TABLES_SIZE (sizeof(xlat_tbl_t) * MAX_XLAT_TABLES)
114
115+#ifdef __clang__
116+#pragma clang section bss=".nozi.mmu.l2"
117+#endif
118 /* MMU L2 table for TAs, one for each thread */
119 static xlat_tbl_t xlat_tables_ul1[CFG_NUM_THREADS]
120- __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2");
121-
122+#ifndef __clang__
123+ __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2")
124+#endif
125+;
126+#ifdef __clang__
127+#pragma clang section bss=""
128+#endif
129 /*
130 * TAs page table entry inside a level 1 page table.
131 *
132diff --git a/core/arch/arm/mm/pgt_cache.c b/core/arch/arm/mm/pgt_cache.c
133index d658b3e68..6c36706c0 100644
134--- a/core/arch/arm/mm/pgt_cache.c
135+++ b/core/arch/arm/mm/pgt_cache.c
136@@ -104,8 +104,18 @@ void pgt_init(void)
137 * has a large alignment, while .bss has a small alignment. The current
138 * link script is optimized for small alignment in .bss
139 */
140+#ifdef __clang__
141+#pragma clang section bss=".nozi.mmu.l2"
142+#endif
143 static uint8_t pgt_tables[PGT_CACHE_SIZE][PGT_SIZE]
144- __aligned(PGT_SIZE) __section(".nozi.pgt_cache");
145+ __aligned(PGT_SIZE)
146+#ifndef __clang__
147+ __section(".nozi.pgt_cache")
148+#endif
149+ ;
150+#ifdef __clang__
151+#pragma clang section bss=""
152+#endif
153 size_t n;
154
155 for (n = 0; n < ARRAY_SIZE(pgt_tables); n++) {
156diff --git a/core/kernel/thread.c b/core/kernel/thread.c
157index 18d34e6ad..086129e28 100644
158--- a/core/kernel/thread.c
159+++ b/core/kernel/thread.c
160@@ -37,13 +37,24 @@ struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE] __nex_bss;
161 name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1]
162 #endif
163
164+#define DO_PRAGMA(x) _Pragma (#x)
165+
166+#ifdef __clang__
167+#define DECLARE_STACK(name, num_stacks, stack_size, linkage) \
168+DO_PRAGMA (clang section bss=".nozi_stack." #name) \
169+linkage uint32_t name[num_stacks] \
170+ [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \
171+ STACK_ALIGNMENT) / sizeof(uint32_t)] \
172+ __attribute__((aligned(STACK_ALIGNMENT))); \
173+DO_PRAGMA(clang section bss="")
174+#else
175 #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \
176 linkage uint32_t name[num_stacks] \
177 [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \
178 STACK_ALIGNMENT) / sizeof(uint32_t)] \
179 __attribute__((section(".nozi_stack." # name), \
180 aligned(STACK_ALIGNMENT)))
181-
182+#endif
183 #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack))
184
185 DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE,
186--
1872.37.2
188