Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 1 | From fc930e468045dda9eab4ebe6927cc322bb294f3b Mon Sep 17 00:00:00 2001 |
| 2 | From: Emekcan Aras <emekcan.aras@arm.com> |
| 3 | Date: Wed, 21 Dec 2022 10:55:58 +0000 |
| 4 | Subject: [PATCH] core: Define section attributes for clang |
| 5 | |
| 6 | Clang's attribute section is not same as gcc, here we need to add flags |
| 7 | to sections so they can be eventually collected by linker into final |
| 8 | output segments. Only way to do so with clang is to use |
| 9 | |
| 10 | pragma clang section ... |
| 11 | |
| 12 | The behavious is described here [1], this allows us to define names bss |
| 13 | sections. This was not an issue until clang-15 where LLD linker starts |
| 14 | to detect the section flags before merging them and throws the following |
| 15 | errors |
| 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 | |
| 25 | These sections should be carrying SHT_NOBITS but so far it was not |
| 26 | possible to do so, this patch tries to use clangs pragma to get this |
| 27 | going 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 | |
| 31 | Upstream-Status: Pending |
| 32 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 33 | |
| 34 | --- |
| 35 | core/arch/arm/kernel/thread.c | 19 +++++++++++++++-- |
| 36 | core/arch/arm/mm/core_mmu_lpae.c | 35 +++++++++++++++++++++++++++---- |
| 37 | core/arch/arm/mm/core_mmu_v7.c | 36 +++++++++++++++++++++++++++++--- |
| 38 | core/kernel/thread.c | 13 +++++++++++- |
| 39 | core/mm/pgt_cache.c | 12 ++++++++++- |
| 40 | 5 files changed, 104 insertions(+), 11 deletions(-) |
| 41 | |
| 42 | diff --git a/core/arch/arm/kernel/thread.c b/core/arch/arm/kernel/thread.c |
| 43 | index 66833b3a0..b3eb9cf9a 100644 |
| 44 | --- a/core/arch/arm/kernel/thread.c |
| 45 | +++ b/core/arch/arm/kernel/thread.c |
| 46 | @@ -45,15 +45,30 @@ static size_t thread_user_kcode_size __nex_bss; |
| 47 | #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \ |
| 48 | defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64) |
| 49 | long thread_user_kdata_sp_offset __nex_bss; |
| 50 | +#ifdef __clang__ |
| 51 | +#ifndef CFG_VIRTUALIZATION |
| 52 | +#pragma clang section bss=".nozi.kdata_page" |
| 53 | +#else |
| 54 | +#pragma clang section bss=".nex_nozi.kdata_page" |
| 55 | +#endif |
| 56 | +#endif |
| 57 | static uint8_t thread_user_kdata_page[ |
| 58 | ROUNDUP(sizeof(struct thread_core_local) * CFG_TEE_CORE_NB_CORE, |
| 59 | SMALL_PAGE_SIZE)] |
| 60 | __aligned(SMALL_PAGE_SIZE) |
| 61 | +#ifndef __clang__ |
| 62 | #ifndef CFG_NS_VIRTUALIZATION |
| 63 | - __section(".nozi.kdata_page"); |
| 64 | + __section(".nozi.kdata_page") |
| 65 | #else |
| 66 | - __section(".nex_nozi.kdata_page"); |
| 67 | + __section(".nex_nozi.kdata_page") |
| 68 | #endif |
| 69 | +#endif |
| 70 | + ; |
| 71 | +#endif |
| 72 | + |
| 73 | +/* reset BSS section to default ( .bss ) */ |
| 74 | +#ifdef __clang__ |
| 75 | +#pragma clang section bss="" |
| 76 | #endif |
| 77 | |
| 78 | #ifdef ARM32 |
| 79 | diff --git a/core/arch/arm/mm/core_mmu_lpae.c b/core/arch/arm/mm/core_mmu_lpae.c |
| 80 | index 7e79f780a..ec4db9dc9 100644 |
| 81 | --- a/core/arch/arm/mm/core_mmu_lpae.c |
| 82 | +++ b/core/arch/arm/mm/core_mmu_lpae.c |
| 83 | @@ -233,19 +233,46 @@ typedef uint16_t l1_idx_t; |
| 84 | typedef uint64_t base_xlat_tbls_t[CFG_TEE_CORE_NB_CORE][NUM_BASE_LEVEL_ENTRIES]; |
| 85 | typedef uint64_t xlat_tbl_t[XLAT_TABLE_ENTRIES]; |
| 86 | |
| 87 | +#ifdef __clang__ |
| 88 | +#pragma clang section bss=".nozi.mmu.base_table" |
| 89 | +#endif |
| 90 | static base_xlat_tbls_t base_xlation_table[NUM_BASE_TABLES] |
| 91 | __aligned(NUM_BASE_LEVEL_ENTRIES * XLAT_ENTRY_SIZE) |
| 92 | - __section(".nozi.mmu.base_table"); |
| 93 | +#ifndef __clang__ |
| 94 | + __section(".nozi.mmu.base_table") |
| 95 | +#endif |
| 96 | +; |
| 97 | +#ifdef __clang__ |
| 98 | +#pragma clang section bss="" |
| 99 | +#endif |
| 100 | |
| 101 | +#ifdef __clang__ |
| 102 | +#pragma clang section bss=".nozi.mmu.l2" |
| 103 | +#endif |
| 104 | static xlat_tbl_t xlat_tables[MAX_XLAT_TABLES] |
| 105 | - __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2"); |
| 106 | + __aligned(XLAT_TABLE_SIZE) |
| 107 | +#ifndef __clang__ |
| 108 | + __section(".nozi.mmu.l2") |
| 109 | +#endif |
| 110 | +; |
| 111 | +#ifdef __clang__ |
| 112 | +#pragma clang section bss="" |
| 113 | +#endif |
| 114 | |
| 115 | #define XLAT_TABLES_SIZE (sizeof(xlat_tbl_t) * MAX_XLAT_TABLES) |
| 116 | |
| 117 | +#ifdef __clang__ |
| 118 | +#pragma clang section bss=".nozi.mmu.l2" |
| 119 | +#endif |
| 120 | /* MMU L2 table for TAs, one for each thread */ |
| 121 | static xlat_tbl_t xlat_tables_ul1[CFG_NUM_THREADS] |
| 122 | - __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2"); |
| 123 | - |
| 124 | +#ifndef __clang__ |
| 125 | + __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2") |
| 126 | +#endif |
| 127 | +; |
| 128 | +#ifdef __clang__ |
| 129 | +#pragma clang section bss="" |
| 130 | +#endif |
| 131 | /* |
| 132 | * TAs page table entry inside a level 1 page table. |
| 133 | * |
| 134 | diff --git a/core/arch/arm/mm/core_mmu_v7.c b/core/arch/arm/mm/core_mmu_v7.c |
| 135 | index 61e703da8..1960c08ca 100644 |
| 136 | --- a/core/arch/arm/mm/core_mmu_v7.c |
| 137 | +++ b/core/arch/arm/mm/core_mmu_v7.c |
| 138 | @@ -204,16 +204,46 @@ typedef uint32_t l1_xlat_tbl_t[NUM_L1_ENTRIES]; |
| 139 | typedef uint32_t l2_xlat_tbl_t[NUM_L2_ENTRIES]; |
| 140 | typedef uint32_t ul1_xlat_tbl_t[NUM_UL1_ENTRIES]; |
| 141 | |
| 142 | +#ifdef __clang__ |
| 143 | +#pragma clang section bss=".nozi.mmu.l1" |
| 144 | +#endif |
| 145 | static l1_xlat_tbl_t main_mmu_l1_ttb |
| 146 | - __aligned(L1_ALIGNMENT) __section(".nozi.mmu.l1"); |
| 147 | + __aligned(L1_ALIGNMENT) |
| 148 | +#ifndef __clang__ |
| 149 | + __section(".nozi.mmu.l1") |
| 150 | +#endif |
| 151 | +; |
| 152 | +#ifdef __clang__ |
| 153 | +#pragma clang section bss="" |
| 154 | +#endif |
| 155 | |
| 156 | /* L2 MMU tables */ |
| 157 | +#ifdef __clang__ |
| 158 | +#pragma clang section bss=".nozi.mmu.l2" |
| 159 | +#endif |
| 160 | static l2_xlat_tbl_t main_mmu_l2_ttb[MAX_XLAT_TABLES] |
| 161 | - __aligned(L2_ALIGNMENT) __section(".nozi.mmu.l2"); |
| 162 | + __aligned(L2_ALIGNMENT) |
| 163 | +#ifndef __clang__ |
| 164 | + __section(".nozi.mmu.l2") |
| 165 | +#endif |
| 166 | +; |
| 167 | +#ifdef __clang__ |
| 168 | +#pragma clang section bss="" |
| 169 | +#endif |
| 170 | |
| 171 | /* MMU L1 table for TAs, one for each thread */ |
| 172 | +#ifdef __clang__ |
| 173 | +#pragma clang section bss=".nozi.mmu.ul1" |
| 174 | +#endif |
| 175 | static ul1_xlat_tbl_t main_mmu_ul1_ttb[CFG_NUM_THREADS] |
| 176 | - __aligned(UL1_ALIGNMENT) __section(".nozi.mmu.ul1"); |
| 177 | + __aligned(UL1_ALIGNMENT) |
| 178 | +#ifndef __clang__ |
| 179 | + __section(".nozi.mmu.ul1") |
| 180 | +#endif |
| 181 | +; |
| 182 | +#ifdef __clang__ |
| 183 | +#pragma clang section bss="" |
| 184 | +#endif |
| 185 | |
| 186 | struct mmu_partition { |
| 187 | l1_xlat_tbl_t *l1_table; |
| 188 | diff --git a/core/kernel/thread.c b/core/kernel/thread.c |
| 189 | index 2a1f22dce..5516b6771 100644 |
| 190 | --- a/core/kernel/thread.c |
| 191 | +++ b/core/kernel/thread.c |
| 192 | @@ -39,13 +39,24 @@ static uint32_t end_canary_value = 0xababab00; |
| 193 | name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1] |
| 194 | #endif |
| 195 | |
| 196 | +#define DO_PRAGMA(x) _Pragma (#x) |
| 197 | + |
| 198 | +#ifdef __clang__ |
| 199 | +#define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ |
| 200 | +DO_PRAGMA (clang section bss=".nozi_stack." #name) \ |
| 201 | +linkage uint32_t name[num_stacks] \ |
| 202 | + [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ |
| 203 | + STACK_ALIGNMENT) / sizeof(uint32_t)] \ |
| 204 | + __attribute__((aligned(STACK_ALIGNMENT))); \ |
| 205 | +DO_PRAGMA(clang section bss="") |
| 206 | +#else |
| 207 | #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ |
| 208 | linkage uint32_t name[num_stacks] \ |
| 209 | [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ |
| 210 | STACK_ALIGNMENT) / sizeof(uint32_t)] \ |
| 211 | __attribute__((section(".nozi_stack." # name), \ |
| 212 | aligned(STACK_ALIGNMENT))) |
| 213 | - |
| 214 | +#endif |
| 215 | #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack)) |
| 216 | |
| 217 | DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, STACK_TMP_SIZE, |
| 218 | diff --git a/core/mm/pgt_cache.c b/core/mm/pgt_cache.c |
| 219 | index 79553c6d2..b9efdf427 100644 |
| 220 | --- a/core/mm/pgt_cache.c |
| 221 | +++ b/core/mm/pgt_cache.c |
| 222 | @@ -410,8 +410,18 @@ void pgt_init(void) |
| 223 | * has a large alignment, while .bss has a small alignment. The current |
| 224 | * link script is optimized for small alignment in .bss |
| 225 | */ |
| 226 | +#ifdef __clang__ |
| 227 | +#pragma clang section bss=".nozi.mmu.l2" |
| 228 | +#endif |
| 229 | static uint8_t pgt_tables[PGT_CACHE_SIZE][PGT_SIZE] |
| 230 | - __aligned(PGT_SIZE) __section(".nozi.pgt_cache"); |
| 231 | + __aligned(PGT_SIZE) |
| 232 | +#ifndef __clang__ |
| 233 | + __section(".nozi.pgt_cache") |
| 234 | +#endif |
| 235 | + ; |
| 236 | +#ifdef __clang__ |
| 237 | +#pragma clang section bss="" |
| 238 | +#endif |
| 239 | size_t n; |
| 240 | |
| 241 | for (n = 0; n < ARRAY_SIZE(pgt_tables); n++) { |