Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | From f189457b79989543f65b8a4e8729eff2cdf9a758 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Sat, 13 Aug 2022 19:24:55 -0700 |
| 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 | 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 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 40 | --- a/core/arch/arm/kernel/thread.c |
| 41 | +++ b/core/arch/arm/kernel/thread.c |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 42 | @@ -44,16 +44,31 @@ static size_t thread_user_kcode_size __n |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 43 | #if defined(CFG_CORE_UNMAP_CORE_AT_EL0) && \ |
| 44 | defined(CFG_CORE_WORKAROUND_SPECTRE_BP_SEC) && defined(ARM64) |
| 45 | long thread_user_kdata_sp_offset __nex_bss; |
| 46 | +#ifdef __clang__ |
| 47 | +#ifndef CFG_VIRTUALIZATION |
| 48 | +#pragma clang section bss=".nozi.kdata_page" |
| 49 | +#else |
| 50 | +#pragma clang section bss=".nex_nozi.kdata_page" |
| 51 | +#endif |
| 52 | +#endif |
| 53 | static uint8_t thread_user_kdata_page[ |
| 54 | ROUNDUP(sizeof(struct thread_core_local) * CFG_TEE_CORE_NB_CORE, |
| 55 | SMALL_PAGE_SIZE)] |
| 56 | __aligned(SMALL_PAGE_SIZE) |
| 57 | +#ifndef __clang__ |
| 58 | #ifndef CFG_VIRTUALIZATION |
| 59 | - __section(".nozi.kdata_page"); |
| 60 | + __section(".nozi.kdata_page") |
| 61 | #else |
| 62 | - __section(".nex_nozi.kdata_page"); |
| 63 | + __section(".nex_nozi.kdata_page") |
| 64 | #endif |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 65 | #endif |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 66 | + ; |
| 67 | +#endif |
| 68 | + |
| 69 | +/* reset BSS section to default ( .bss ) */ |
| 70 | +#ifdef __clang__ |
| 71 | +#pragma clang section bss="" |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 72 | +#endif |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 73 | |
| 74 | #ifdef ARM32 |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 75 | uint32_t __nostackcheck thread_get_exceptions(void) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 76 | --- a/core/arch/arm/mm/core_mmu_lpae.c |
| 77 | +++ b/core/arch/arm/mm/core_mmu_lpae.c |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 78 | @@ -233,19 +233,46 @@ typedef uint16_t l1_idx_t; |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 79 | typedef uint64_t base_xlat_tbls_t[CFG_TEE_CORE_NB_CORE][NUM_BASE_LEVEL_ENTRIES]; |
| 80 | typedef uint64_t xlat_tbl_t[XLAT_TABLE_ENTRIES]; |
| 81 | |
| 82 | +#ifdef __clang__ |
| 83 | +#pragma clang section bss=".nozi.mmu.base_table" |
| 84 | +#endif |
| 85 | static base_xlat_tbls_t base_xlation_table[NUM_BASE_TABLES] |
| 86 | __aligned(NUM_BASE_LEVEL_ENTRIES * XLAT_ENTRY_SIZE) |
| 87 | - __section(".nozi.mmu.base_table"); |
| 88 | +#ifndef __clang__ |
| 89 | + __section(".nozi.mmu.base_table") |
| 90 | +#endif |
| 91 | +; |
| 92 | +#ifdef __clang__ |
| 93 | +#pragma clang section bss="" |
| 94 | +#endif |
| 95 | |
| 96 | +#ifdef __clang__ |
| 97 | +#pragma clang section bss=".nozi.mmu.l2" |
| 98 | +#endif |
| 99 | static xlat_tbl_t xlat_tables[MAX_XLAT_TABLES] |
| 100 | - __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2"); |
| 101 | + __aligned(XLAT_TABLE_SIZE) |
| 102 | +#ifndef __clang__ |
| 103 | + __section(".nozi.mmu.l2") |
| 104 | +#endif |
| 105 | +; |
| 106 | +#ifdef __clang__ |
| 107 | +#pragma clang section bss="" |
| 108 | +#endif |
| 109 | |
| 110 | #define XLAT_TABLES_SIZE (sizeof(xlat_tbl_t) * MAX_XLAT_TABLES) |
| 111 | |
| 112 | +#ifdef __clang__ |
| 113 | +#pragma clang section bss=".nozi.mmu.l2" |
| 114 | +#endif |
| 115 | /* MMU L2 table for TAs, one for each thread */ |
| 116 | static xlat_tbl_t xlat_tables_ul1[CFG_NUM_THREADS] |
| 117 | - __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2"); |
| 118 | - |
| 119 | +#ifndef __clang__ |
| 120 | + __aligned(XLAT_TABLE_SIZE) __section(".nozi.mmu.l2") |
| 121 | +#endif |
| 122 | +; |
| 123 | +#ifdef __clang__ |
| 124 | +#pragma clang section bss="" |
| 125 | +#endif |
| 126 | /* |
| 127 | * TAs page table entry inside a level 1 page table. |
| 128 | * |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 129 | --- a/core/arch/arm/mm/pgt_cache.c |
| 130 | +++ b/core/arch/arm/mm/pgt_cache.c |
| 131 | @@ -104,8 +104,18 @@ void pgt_init(void) |
| 132 | * has a large alignment, while .bss has a small alignment. The current |
| 133 | * link script is optimized for small alignment in .bss |
| 134 | */ |
| 135 | +#ifdef __clang__ |
| 136 | +#pragma clang section bss=".nozi.mmu.l2" |
| 137 | +#endif |
| 138 | static uint8_t pgt_tables[PGT_CACHE_SIZE][PGT_SIZE] |
| 139 | - __aligned(PGT_SIZE) __section(".nozi.pgt_cache"); |
| 140 | + __aligned(PGT_SIZE) |
| 141 | +#ifndef __clang__ |
| 142 | + __section(".nozi.pgt_cache") |
| 143 | +#endif |
| 144 | + ; |
| 145 | +#ifdef __clang__ |
| 146 | +#pragma clang section bss="" |
| 147 | +#endif |
| 148 | size_t n; |
| 149 | |
| 150 | for (n = 0; n < ARRAY_SIZE(pgt_tables); n++) { |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 151 | --- a/core/kernel/thread.c |
| 152 | +++ b/core/kernel/thread.c |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 153 | @@ -37,13 +37,24 @@ struct thread_core_local thread_core_loc |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 154 | name[stack_num][sizeof(name[stack_num]) / sizeof(uint32_t) - 1] |
| 155 | #endif |
| 156 | |
| 157 | +#define DO_PRAGMA(x) _Pragma (#x) |
| 158 | + |
| 159 | +#ifdef __clang__ |
| 160 | +#define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ |
| 161 | +DO_PRAGMA (clang section bss=".nozi_stack." #name) \ |
| 162 | +linkage uint32_t name[num_stacks] \ |
| 163 | + [ROUNDUP(stack_size + STACK_CANARY_SIZE + STACK_CHECK_EXTRA, \ |
| 164 | + STACK_ALIGNMENT) / sizeof(uint32_t)] \ |
| 165 | + __attribute__((aligned(STACK_ALIGNMENT))); \ |
| 166 | +DO_PRAGMA(clang section bss="") |
| 167 | +#else |
| 168 | #define DECLARE_STACK(name, num_stacks, stack_size, linkage) \ |
| 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__((section(".nozi_stack." # name), \ |
| 173 | aligned(STACK_ALIGNMENT))) |
| 174 | - |
| 175 | +#endif |
| 176 | #define GET_STACK(stack) ((vaddr_t)(stack) + STACK_SIZE(stack)) |
| 177 | |
| 178 | DECLARE_STACK(stack_tmp, CFG_TEE_CORE_NB_CORE, |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 179 | --- a/core/arch/arm/mm/core_mmu_v7.c |
| 180 | +++ b/core/arch/arm/mm/core_mmu_v7.c |
| 181 | @@ -204,16 +204,46 @@ typedef uint32_t l1_xlat_tbl_t[NUM_L1_EN |
| 182 | typedef uint32_t l2_xlat_tbl_t[NUM_L2_ENTRIES]; |
| 183 | typedef uint32_t ul1_xlat_tbl_t[NUM_UL1_ENTRIES]; |
| 184 | |
| 185 | +#ifdef __clang__ |
| 186 | +#pragma clang section bss=".nozi.mmu.l1" |
| 187 | +#endif |
| 188 | static l1_xlat_tbl_t main_mmu_l1_ttb |
| 189 | - __aligned(L1_ALIGNMENT) __section(".nozi.mmu.l1"); |
| 190 | + __aligned(L1_ALIGNMENT) |
| 191 | +#ifndef __clang__ |
| 192 | + __section(".nozi.mmu.l1") |
| 193 | +#endif |
| 194 | +; |
| 195 | +#ifdef __clang__ |
| 196 | +#pragma clang section bss="" |
| 197 | +#endif |
| 198 | |
| 199 | /* L2 MMU tables */ |
| 200 | +#ifdef __clang__ |
| 201 | +#pragma clang section bss=".nozi.mmu.l2" |
| 202 | +#endif |
| 203 | static l2_xlat_tbl_t main_mmu_l2_ttb[MAX_XLAT_TABLES] |
| 204 | - __aligned(L2_ALIGNMENT) __section(".nozi.mmu.l2"); |
| 205 | + __aligned(L2_ALIGNMENT) |
| 206 | +#ifndef __clang__ |
| 207 | + __section(".nozi.mmu.l2") |
| 208 | +#endif |
| 209 | +; |
| 210 | +#ifdef __clang__ |
| 211 | +#pragma clang section bss="" |
| 212 | +#endif |
| 213 | |
| 214 | /* MMU L1 table for TAs, one for each thread */ |
| 215 | +#ifdef __clang__ |
| 216 | +#pragma clang section bss=".nozi.mmu.ul1" |
| 217 | +#endif |
| 218 | static ul1_xlat_tbl_t main_mmu_ul1_ttb[CFG_NUM_THREADS] |
| 219 | - __aligned(UL1_ALIGNMENT) __section(".nozi.mmu.ul1"); |
| 220 | + __aligned(UL1_ALIGNMENT) |
| 221 | +#ifndef __clang__ |
| 222 | + __section(".nozi.mmu.ul1") |
| 223 | +#endif |
| 224 | +; |
| 225 | +#ifdef __clang__ |
| 226 | +#pragma clang section bss="" |
| 227 | +#endif |
| 228 | |
| 229 | struct mmu_partition { |
| 230 | l1_xlat_tbl_t *l1_table; |