Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 1 | From ff1b556ac2cd6bbb857a1ac03e0557eb490bc845 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] [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 | core/arch/arm/kernel/thread.c | 19 +++++++++++++++-- |
| 35 | core/arch/arm/mm/core_mmu_lpae.c | 35 +++++++++++++++++++++++++++---- |
| 36 | core/arch/arm/mm/core_mmu_v7.c | 36 +++++++++++++++++++++++++++++--- |
| 37 | core/arch/arm/mm/pgt_cache.c | 12 ++++++++++- |
| 38 | core/kernel/thread.c | 13 +++++++++++- |
| 39 | 5 files changed, 104 insertions(+), 11 deletions(-) |
| 40 | |
| 41 | diff --git a/core/arch/arm/kernel/thread.c b/core/arch/arm/kernel/thread.c |
| 42 | index 05dbbe56..8e6ea034 100644 |
| 43 | --- a/core/arch/arm/kernel/thread.c |
| 44 | +++ b/core/arch/arm/kernel/thread.c |
| 45 | @@ -44,15 +44,30 @@ static size_t thread_user_kcode_size __nex_bss; |
| 46 | #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \ |
| 47 | defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64) |
| 48 | long thread_user_kdata_sp_offset __nex_bss; |
| 49 | +#ifdef __clang__ |
| 50 | +#ifndef CFG_VIRTUALIZATION |
| 51 | +#pragma clang section bss=".nozi.kdata_page" |
| 52 | +#else |
| 53 | +#pragma clang section bss=".nex_nozi.kdata_page" |
| 54 | +#endif |
| 55 | +#endif |
| 56 | static uint8_t thread_user_kdata_page[ |
| 57 | ROUNDUP(sizeof(struct thread_core_local) * CFG_TEE_CORE_NB_CORE, |
| 58 | SMALL_PAGE_SIZE)] |
| 59 | __aligned(SMALL_PAGE_SIZE) |
| 60 | +#ifndef __clang__ |
| 61 | #ifndef CFG_VIRTUALIZATION |
| 62 | - __section(".nozi.kdata_page"); |
| 63 | + __section(".nozi.kdata_page") |
| 64 | #else |
| 65 | - __section(".nex_nozi.kdata_page"); |
| 66 | + __section(".nex_nozi.kdata_page") |
| 67 | #endif |
| 68 | +#endif |
| 69 | + ; |
| 70 | +#endif |
| 71 | + |
| 72 | +/* reset BSS section to default ( .bss ) */ |
| 73 | +#ifdef __clang__ |
| 74 | +#pragma clang section bss="" |
| 75 | #endif |
| 76 | |
| 77 | #ifdef ARM32 |
| 78 | diff --git a/core/arch/arm/mm/core_mmu_lpae.c b/core/arch/arm/mm/core_mmu_lpae.c |
| 79 | index 3f08eec6..e6dc9261 100644 |
| 80 | --- a/core/arch/arm/mm/core_mmu_lpae.c |
| 81 | +++ b/core/arch/arm/mm/core_mmu_lpae.c |
| 82 | @@ -233,19 +233,46 @@ typedef uint16_t l1_idx_t; |
| 83 | typedef uint64_t base_xlat_tbls_t[CFG_TEE_CORE_NB_CORE][NUM_BASE_LEVEL_ENTRIES]; |
| 84 | typedef uint64_t xlat_tbl_t[XLAT_TABLE_ENTRIES]; |
| 85 | |
| 86 | +#ifdef __clang__ |
| 87 | +#pragma clang section bss=".nozi.mmu.base_table" |
| 88 | +#endif |
| 89 | static base_xlat_tbls_t base_xlation_table[NUM_BASE_TABLES] |
| 90 | __aligned(NUM_BASE_LEVEL_ENTRIES * XLAT_ENTRY_SIZE) |
| 91 | - __section(".nozi.mmu.base_table"); |
| 92 | +#ifndef __clang__ |
| 93 | + __section(".nozi.mmu.base_table") |
| 94 | +#endif |
| 95 | +; |
| 96 | +#ifdef __clang__ |
| 97 | +#pragma clang section bss="" |
| 98 | +#endif |
| 99 | |
| 100 | +#ifdef __clang__ |
| 101 | +#pragma clang section bss=".nozi.mmu.l2" |
| 102 | +#endif |
| 103 | static xlat_tbl_t xlat_tables[MAX_XLAT_TABLES] |
| 104 | - __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2"); |
| 105 | + __aligned(XLAT_TABLE_SIZE) |
| 106 | +#ifndef __clang__ |
| 107 | + __section(".nozi.mmu.l2") |
| 108 | +#endif |
| 109 | +; |
| 110 | +#ifdef __clang__ |
| 111 | +#pragma clang section bss="" |
| 112 | +#endif |
| 113 | |
| 114 | #define XLAT_TABLES_SIZE (sizeof(xlat_tbl_t) * MAX_XLAT_TABLES) |
| 115 | |
| 116 | +#ifdef __clang__ |
| 117 | +#pragma clang section bss=".nozi.mmu.l2" |
| 118 | +#endif |
| 119 | /* MMU L2 table for TAs, one for each thread */ |
| 120 | static xlat_tbl_t xlat_tables_ul1[CFG_NUM_THREADS] |
| 121 | - __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2"); |
| 122 | - |
| 123 | +#ifndef __clang__ |
| 124 | + __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2") |
| 125 | +#endif |
| 126 | +; |
| 127 | +#ifdef __clang__ |
| 128 | +#pragma clang section bss="" |
| 129 | +#endif |
| 130 | /* |
| 131 | * TAs page table entry inside a level 1 page table. |
| 132 | * |
| 133 | diff --git a/core/arch/arm/mm/core_mmu_v7.c b/core/arch/arm/mm/core_mmu_v7.c |
| 134 | index cd85bd22..3e18f54f 100644 |
| 135 | --- a/core/arch/arm/mm/core_mmu_v7.c |
| 136 | +++ b/core/arch/arm/mm/core_mmu_v7.c |
| 137 | @@ -204,16 +204,46 @@ typedef uint32_t l1_xlat_tbl_t[NUM_L1_ENTRIES]; |
| 138 | typedef uint32_t l2_xlat_tbl_t[NUM_L2_ENTRIES]; |
| 139 | typedef uint32_t ul1_xlat_tbl_t[NUM_UL1_ENTRIES]; |
| 140 | |
| 141 | +#ifdef __clang__ |
| 142 | +#pragma clang section bss=".nozi.mmu.l1" |
| 143 | +#endif |
| 144 | static l1_xlat_tbl_t main_mmu_l1_ttb |
| 145 | - __aligned(L1_ALIGNMENT) __section(".nozi.mmu.l1"); |
| 146 | + __aligned(L1_ALIGNMENT) |
| 147 | +#ifndef __clang__ |
| 148 | + __section(".nozi.mmu.l1") |
| 149 | +#endif |
| 150 | +; |
| 151 | +#ifdef __clang__ |
| 152 | +#pragma clang section bss="" |
| 153 | +#endif |
| 154 | |
| 155 | /* L2 MMU tables */ |
| 156 | +#ifdef __clang__ |
| 157 | +#pragma clang section bss=".nozi.mmu.l2" |
| 158 | +#endif |
| 159 | static l2_xlat_tbl_t main_mmu_l2_ttb[MAX_XLAT_TABLES] |
| 160 | - __aligned(L2_ALIGNMENT) __section(".nozi.mmu.l2"); |
| 161 | + __aligned(L2_ALIGNMENT) |
| 162 | +#ifndef __clang__ |
| 163 | + __section(".nozi.mmu.l2") |
| 164 | +#endif |
| 165 | +; |
| 166 | +#ifdef __clang__ |
| 167 | +#pragma clang section bss="" |
| 168 | +#endif |
| 169 | |
| 170 | /* MMU L1 table for TAs, one for each thread */ |
| 171 | +#ifdef __clang__ |
| 172 | +#pragma clang section bss=".nozi.mmu.ul1" |
| 173 | +#endif |
| 174 | static ul1_xlat_tbl_t main_mmu_ul1_ttb[CFG_NUM_THREADS] |
| 175 | - __aligned(UL1_ALIGNMENT) __section(".nozi.mmu.ul1"); |
| 176 | + __aligned(UL1_ALIGNMENT) |
| 177 | +#ifndef __clang__ |
| 178 | + __section(".nozi.mmu.ul1") |
| 179 | +#endif |
| 180 | +; |
| 181 | +#ifdef __clang__ |
| 182 | +#pragma clang section bss="" |
| 183 | +#endif |
| 184 | |
| 185 | struct mmu_partition { |
| 186 | l1_xlat_tbl_t *l1_table; |
| 187 | diff --git a/core/arch/arm/mm/pgt_cache.c b/core/arch/arm/mm/pgt_cache.c |
| 188 | index a7b1b10e..489859ce 100644 |
| 189 | --- a/core/arch/arm/mm/pgt_cache.c |
| 190 | +++ b/core/arch/arm/mm/pgt_cache.c |
| 191 | @@ -410,8 +410,18 @@ void pgt_init(void) |
| 192 | * has a large alignment, while .bss has a small alignment. The current |
| 193 | * link script is optimized for small alignment in .bss |
| 194 | */ |
| 195 | +#ifdef __clang__ |
| 196 | +#pragma clang section bss=".nozi.mmu.l2" |
| 197 | +#endif |
| 198 | static uint8_t pgt_tables[PGT_CACHE_SIZE][PGT_SIZE] |
| 199 | - __aligned(PGT_SIZE) __section(".nozi.pgt_cache"); |
| 200 | + __aligned(PGT_SIZE) |
| 201 | +#ifndef __clang__ |
| 202 | + __section(".nozi.pgt_cache") |
| 203 | +#endif |
| 204 | + ; |
| 205 | +#ifdef __clang__ |
| 206 | +#pragma clang section bss="" |
| 207 | +#endif |
| 208 | size_t n; |
| 209 | |
| 210 | for (n = 0; n < ARRAY_SIZE(pgt_tables); n++) { |
| 211 | diff --git a/core/kernel/thread.c b/core/kernel/thread.c |
| 212 | index d1f2f382..8de124ae 100644 |
| 213 | --- a/core/kernel/thread.c |
| 214 | +++ b/core/kernel/thread.c |
| 215 | @@ -38,13 +38,24 @@ struct thread_core_local thread_core_local[CFG_TEE_CORE_NB_CORE] __nex_bss; |
| 216 | name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1] |
| 217 | #endif |
| 218 | |
| 219 | +#define DO_PRAGMA(x) _Pragma (#x) |
| 220 | + |
| 221 | +#ifdef __clang__ |
| 222 | +#define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ |
| 223 | +DO_PRAGMA (clang section bss=".nozi_stack." #name) \ |
| 224 | +linkage uint32_t name[num_stacks] \ |
| 225 | + [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ |
| 226 | + STACK_ALIGNMENT) / sizeof(uint32_t)] \ |
| 227 | + __attribute__((aligned(STACK_ALIGNMENT))); \ |
| 228 | +DO_PRAGMA(clang section bss="") |
| 229 | +#else |
| 230 | #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ |
| 231 | linkage uint32_t name[num_stacks] \ |
| 232 | [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ |
| 233 | STACK_ALIGNMENT) / sizeof(uint32_t)] \ |
| 234 | __attribute__((section(".nozi_stack." # name), \ |
| 235 | aligned(STACK_ALIGNMENT))) |
| 236 | - |
| 237 | +#endif |
| 238 | #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack)) |
| 239 | |
| 240 | DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, STACK_TMP_SIZE, |
| 241 | -- |
| 242 | 2.17.1 |
| 243 | |