Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <arch.h> |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <attestation_token.h> |
| 10 | #include <buffer.h> |
Javier Almansa Sobrino | 68a593a | 2022-07-25 09:35:32 +0100 | [diff] [blame] | 11 | #include <buffer_private.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 12 | #include <cpuid.h> |
| 13 | #include <debug.h> |
| 14 | #include <errno.h> |
| 15 | #include <gic.h> |
| 16 | #include <granule.h> |
| 17 | #include <memory_alloc.h> |
| 18 | #include <sizes.h> |
| 19 | #include <slot_buf_arch.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <stdint.h> |
| 22 | #include <table.h> |
| 23 | #include <xlat_contexts.h> |
| 24 | #include <xlat_tables.h> |
| 25 | |
| 26 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 27 | * All the slot buffers for a given CPU must be mapped by a single translation |
| 28 | * table, which means the max VA size should be <= 4KB * 512 |
| 29 | */ |
| 30 | COMPILER_ASSERT((RMM_SLOT_BUF_VA_SIZE) <= (GRANULE_SIZE * XLAT_TABLE_ENTRIES)); |
| 31 | |
| 32 | /* |
| 33 | * For all translation stages if FEAT_TTST is implemented, while |
| 34 | * the PE is executing in AArch64 state and is using 4KB |
| 35 | * translation granules, the min address space size is 64KB |
| 36 | */ |
| 37 | COMPILER_ASSERT((RMM_SLOT_BUF_VA_SIZE) >= (1 << 16U)); |
| 38 | |
| 39 | #define RMM_SLOT_BUF_MMAP MAP_REGION_TRANSIENT( \ |
| 40 | SLOT_VIRT, \ |
| 41 | RMM_SLOT_BUF_VA_SIZE, \ |
| 42 | PAGE_SIZE) |
| 43 | |
| 44 | #define SLOT_BUF_MMAP_REGIONS UL(1) |
| 45 | |
| 46 | /* |
| 47 | * Attributes for a buffer slot page descriptor. |
| 48 | * Note that the AF bit on the descriptor is handled by the translation |
| 49 | * library (it assumes that access faults are not handled) so it does not |
| 50 | * need to be specified here. |
| 51 | */ |
| 52 | #define SLOT_DESC_ATTR \ |
| 53 | (MT_RW_DATA | MT_SHAREABILITY_ISH | MT_NG) |
| 54 | |
| 55 | /* |
| 56 | * The base tables for all the contexts are manually allocated as a continous |
| 57 | * block of memory. |
| 58 | */ |
| 59 | static uint64_t transient_base_table[XLAT_TABLE_ENTRIES * MAX_CPUS] |
| 60 | __aligned(BASE_XLAT_TABLES_ALIGNMENT) |
| 61 | __section("slot_buffer_xlat_tbls"); |
| 62 | |
| 63 | /* Allocate per-cpu xlat_ctx_tbls */ |
| 64 | static struct xlat_ctx_tbls slot_buf_tbls[MAX_CPUS]; |
| 65 | |
| 66 | /* |
| 67 | * Allocate mmap regions and define common xlat_ctx_cfg shared will |
| 68 | * all slot_buf_xlat_ctx |
| 69 | */ |
| 70 | XLAT_REGISTER_VA_SPACE(slot_buf, VA_HIGH_REGION, |
| 71 | SLOT_BUF_MMAP_REGIONS, |
| 72 | RMM_SLOT_BUF_VA_SIZE); |
| 73 | |
| 74 | /* context definition */ |
| 75 | static struct xlat_ctx slot_buf_xlat_ctx[MAX_CPUS]; |
| 76 | |
| 77 | /* |
| 78 | * Allocate a cache to store the last level table entry where the slot buffers |
| 79 | * are mapped to avoid needing to perform a table walk every time a buffer |
| 80 | * slot operation is needed. |
| 81 | */ |
| 82 | static struct xlat_table_entry te_cache[MAX_CPUS]; |
| 83 | |
Javier Almansa Sobrino | 68a593a | 2022-07-25 09:35:32 +0100 | [diff] [blame] | 84 | uintptr_t slot_to_va(enum buffer_slot slot) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 85 | { |
| 86 | assert(slot < NR_CPU_SLOTS); |
| 87 | |
| 88 | return (uintptr_t)(SLOT_VIRT + (GRANULE_SIZE * slot)); |
| 89 | } |
| 90 | |
| 91 | static inline struct xlat_ctx *get_slot_buf_xlat_ctx(void) |
| 92 | { |
| 93 | return &slot_buf_xlat_ctx[my_cpuid()]; |
| 94 | } |
| 95 | |
Javier Almansa Sobrino | 68a593a | 2022-07-25 09:35:32 +0100 | [diff] [blame] | 96 | struct xlat_table_entry *get_cache_entry(void) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 97 | { |
| 98 | return &te_cache[my_cpuid()]; |
| 99 | } |
| 100 | |
| 101 | __unused static uint64_t slot_to_descriptor(enum buffer_slot slot) |
| 102 | { |
| 103 | uint64_t *entry = xlat_get_pte_from_table(get_cache_entry(), |
| 104 | slot_to_va(slot)); |
| 105 | |
| 106 | return xlat_read_descriptor(entry); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Setup xlat table for slot buffer mechanism for each PE. |
| 111 | * Must be called for every PE in the system |
| 112 | */ |
| 113 | void slot_buf_setup_xlat(void) |
| 114 | { |
| 115 | unsigned int cpuid = my_cpuid(); |
| 116 | int ret = xlat_ctx_create_dynamic(get_slot_buf_xlat_ctx(), |
| 117 | &slot_buf_xlat_ctx_cfg, |
| 118 | &slot_buf_tbls[cpuid], |
| 119 | &transient_base_table[ |
| 120 | XLAT_TABLE_ENTRIES * cpuid], |
| 121 | GET_NUM_BASE_LEVEL_ENTRIES( |
| 122 | RMM_SLOT_BUF_VA_SIZE), |
| 123 | NULL, |
| 124 | 0U); |
| 125 | |
| 126 | if (ret == -EINVAL) { |
| 127 | /* |
| 128 | * If the context was already created, carry on with the |
| 129 | * initialization. If it cannot be created, panic. |
| 130 | */ |
| 131 | ERROR("%s (%u): Failed to create the empty context for the slot buffers\n", |
| 132 | __func__, __LINE__); |
| 133 | panic(); |
| 134 | } |
| 135 | |
| 136 | if (xlat_ctx_cfg_initialized(get_slot_buf_xlat_ctx()) == false) { |
| 137 | /* Add necessary mmap regions during cold boot */ |
| 138 | struct xlat_mmap_region slot_buf_regions[] = { |
| 139 | RMM_SLOT_BUF_MMAP, |
| 140 | {0} |
| 141 | }; |
| 142 | |
| 143 | if (xlat_mmap_add_ctx(get_slot_buf_xlat_ctx(), |
| 144 | slot_buf_regions, true) != 0) { |
| 145 | ERROR("%s (%u): Failed to map slot buffer memory on high region\n", |
| 146 | __func__, __LINE__); |
| 147 | panic(); |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | if (xlat_ctx_tbls_initialized(get_slot_buf_xlat_ctx()) == false) { |
| 153 | /* |
| 154 | * Initialize the translation tables for the current context. |
| 155 | * This is done on the first boot of each CPU. |
| 156 | */ |
| 157 | int err; |
| 158 | |
| 159 | err = xlat_init_tables_ctx(get_slot_buf_xlat_ctx()); |
| 160 | if (err != 0) { |
| 161 | ERROR("%s (%u): xlat initialization failed with code %i\n", |
| 162 | __func__, __LINE__, err); |
| 163 | panic(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Confugure MMU registers. This function assumes that all the |
| 169 | * contexts of a particular VA region (HIGH or LOW VA) use the same |
| 170 | * limits for VA and PA spaces. |
| 171 | */ |
| 172 | if (xlat_arch_setup_mmu_cfg(get_slot_buf_xlat_ctx())) { |
| 173 | ERROR("%s (%u): MMU registers failed to initialize\n", |
| 174 | __func__, __LINE__); |
| 175 | panic(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Finishes initializing the slot buffer mechanism. |
| 181 | * This function must be called after the MMU is enabled. |
| 182 | */ |
| 183 | void slot_buf_init(void) |
| 184 | { |
| 185 | if (is_mmu_enabled() == false) { |
| 186 | ERROR("%s: MMU must be enabled\n", __func__); |
| 187 | panic(); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Initialize (if not done yet) the internal cache with the last level |
| 192 | * translation table that holds the MMU descriptors for the slot |
| 193 | * buffers, so we can access them faster when we need to map/unmap. |
| 194 | */ |
| 195 | if ((get_cache_entry())->table == NULL) { |
| 196 | if (xlat_get_table_from_va(get_cache_entry(), |
| 197 | get_slot_buf_xlat_ctx(), |
| 198 | slot_to_va(SLOT_NS)) != 0) { |
| 199 | ERROR("%s (%u): Failed to initialize table entry cache for CPU %u\n", |
| 200 | __func__, __LINE__, my_cpuid()); |
| 201 | panic(); |
| 202 | |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Buffer slots are intended to be transient, and should not be live at |
| 209 | * entry/exit of the RMM. |
| 210 | */ |
| 211 | void assert_cpu_slots_empty(void) |
| 212 | { |
| 213 | unsigned int i; |
| 214 | |
| 215 | for (i = 0; i < NR_CPU_SLOTS; i++) { |
| 216 | assert(slot_to_descriptor(i) == INVALID_DESC); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static inline bool is_ns_slot(enum buffer_slot slot) |
| 221 | { |
| 222 | return slot == SLOT_NS; |
| 223 | } |
| 224 | |
| 225 | static inline bool is_realm_slot(enum buffer_slot slot) |
| 226 | { |
| 227 | return (slot != SLOT_NS) && (slot < NR_CPU_SLOTS); |
| 228 | } |
| 229 | |
| 230 | static void *ns_granule_map(enum buffer_slot slot, struct granule *granule) |
| 231 | { |
| 232 | unsigned long addr = granule_addr(granule); |
| 233 | |
| 234 | assert(is_ns_slot(slot)); |
Javier Almansa Sobrino | d528efd | 2023-01-05 16:23:54 +0000 | [diff] [blame^] | 235 | return buffer_arch_map(slot, addr); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static void ns_buffer_unmap(enum buffer_slot slot) |
| 239 | { |
| 240 | assert(is_ns_slot(slot)); |
| 241 | |
| 242 | buffer_arch_unmap((void *)slot_to_va(slot)); |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Maps a granule @g into the provided @slot, returning |
| 247 | * the virtual address. |
| 248 | * |
| 249 | * The caller must either hold @g::lock or hold a reference. |
| 250 | */ |
| 251 | void *granule_map(struct granule *g, enum buffer_slot slot) |
| 252 | { |
| 253 | unsigned long addr = granule_addr(g); |
| 254 | |
| 255 | assert(is_realm_slot(slot)); |
| 256 | |
Javier Almansa Sobrino | d528efd | 2023-01-05 16:23:54 +0000 | [diff] [blame^] | 257 | return buffer_arch_map(slot, addr); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void buffer_unmap(void *buf) |
| 261 | { |
| 262 | buffer_arch_unmap(buf); |
| 263 | } |
| 264 | |
| 265 | bool memcpy_ns_read(void *dest, const void *ns_src, unsigned long size); |
| 266 | bool memcpy_ns_write(void *ns_dest, const void *src, unsigned long size); |
| 267 | |
| 268 | /* |
| 269 | * Map a Non secure granule @g into the slot @slot and read data from |
| 270 | * this granule to @dest. Unmap the granule once the read is done. |
| 271 | * |
| 272 | * It returns 'true' on success or `false` if not all data are copied. |
| 273 | * Only the least significant bits of @offset are considered, which allows the |
| 274 | * full PA of a non-granule aligned buffer to be used for the @offset parameter. |
| 275 | */ |
| 276 | bool ns_buffer_read(enum buffer_slot slot, |
| 277 | struct granule *ns_gr, |
| 278 | unsigned int offset, |
| 279 | unsigned int size, |
| 280 | void *dest) |
| 281 | { |
| 282 | uintptr_t src; |
| 283 | bool retval; |
| 284 | |
| 285 | assert(is_ns_slot(slot)); |
| 286 | assert(ns_gr != NULL); |
| 287 | |
| 288 | /* |
| 289 | * To simplify the trapping mechanism around NS access, |
| 290 | * memcpy_ns_read uses a single 8-byte LDR instruction and |
| 291 | * all parameters must be aligned accordingly. |
| 292 | */ |
| 293 | assert(ALIGNED(size, 8)); |
| 294 | assert(ALIGNED(offset, 8)); |
| 295 | assert(ALIGNED(dest, 8)); |
| 296 | |
| 297 | offset &= ~GRANULE_MASK; |
| 298 | assert(offset + size <= GRANULE_SIZE); |
| 299 | |
| 300 | src = (uintptr_t)ns_granule_map(slot, ns_gr) + offset; |
| 301 | retval = memcpy_ns_read(dest, (void *)src, size); |
| 302 | ns_buffer_unmap(slot); |
| 303 | |
| 304 | return retval; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Map a Non secure granule @g into the slot @slot and write data from |
| 309 | * this granule to @dest. Unmap the granule once the write is done. |
| 310 | * |
| 311 | * It returns 'true' on success or `false` if not all data are copied. |
| 312 | * Only the least significant bits of @offset are considered, which allows the |
| 313 | * full PA of a non-granule aligned buffer to be used for the @offset parameter. |
| 314 | */ |
| 315 | bool ns_buffer_write(enum buffer_slot slot, |
| 316 | struct granule *ns_gr, |
| 317 | unsigned int offset, |
| 318 | unsigned int size, |
| 319 | void *src) |
| 320 | { |
| 321 | uintptr_t dest; |
| 322 | bool retval; |
| 323 | |
| 324 | assert(is_ns_slot(slot)); |
| 325 | assert(ns_gr != NULL); |
| 326 | |
| 327 | /* |
| 328 | * To simplify the trapping mechanism around NS access, |
| 329 | * memcpy_ns_write uses a single 8-byte STR instruction and |
| 330 | * all parameters must be aligned accordingly. |
| 331 | */ |
| 332 | assert(ALIGNED(size, 8)); |
| 333 | assert(ALIGNED(offset, 8)); |
| 334 | assert(ALIGNED(src, 8)); |
| 335 | |
| 336 | offset &= ~GRANULE_MASK; |
| 337 | assert(offset + size <= GRANULE_SIZE); |
| 338 | |
| 339 | dest = (uintptr_t)ns_granule_map(slot, ns_gr) + offset; |
| 340 | retval = memcpy_ns_write((void *)dest, src, size); |
| 341 | ns_buffer_unmap(slot); |
| 342 | |
| 343 | return retval; |
| 344 | } |
| 345 | |
| 346 | /****************************************************************************** |
| 347 | * Internal helpers |
| 348 | ******************************************************************************/ |
| 349 | |
Javier Almansa Sobrino | d528efd | 2023-01-05 16:23:54 +0000 | [diff] [blame^] | 350 | void *buffer_map_internal(enum buffer_slot slot, unsigned long addr) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 351 | { |
| 352 | uint64_t attr = SLOT_DESC_ATTR; |
| 353 | uintptr_t va = slot_to_va(slot); |
| 354 | struct xlat_table_entry *entry = get_cache_entry(); |
| 355 | |
| 356 | assert(GRANULE_ALIGNED(addr)); |
| 357 | |
Javier Almansa Sobrino | d528efd | 2023-01-05 16:23:54 +0000 | [diff] [blame^] | 358 | attr |= (slot == SLOT_NS ? MT_NS : MT_REALM); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 359 | |
| 360 | if (xlat_map_memory_page_with_attrs(entry, va, |
| 361 | (uintptr_t)addr, attr) != 0) { |
| 362 | /* Error mapping the buffer */ |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | return (void *)va; |
| 367 | } |
| 368 | |
| 369 | void buffer_unmap_internal(void *buf) |
| 370 | { |
| 371 | /* |
| 372 | * Prevent the compiler from moving prior loads/stores to buf after the |
| 373 | * update to the translation table. Otherwise, those could fault. |
| 374 | */ |
| 375 | COMPILER_BARRIER(); |
| 376 | |
| 377 | xlat_unmap_memory_page(get_cache_entry(), (uintptr_t)buf); |
| 378 | } |