1 /* 2 * Copyright (c) 2016, Linaro Limited 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <arm.h> 30 #include <assert.h> 31 #include <keep.h> 32 #include <sys/queue.h> 33 #include <kernel/abort.h> 34 #include <kernel/panic.h> 35 #include <kernel/spinlock.h> 36 #include <kernel/tee_misc.h> 37 #include <kernel/tee_ta_manager.h> 38 #include <kernel/thread.h> 39 #include <mm/core_memprot.h> 40 #include <mm/tee_mm.h> 41 #include <mm/tee_pager.h> 42 #include <types_ext.h> 43 #include <stdlib.h> 44 #include <tee_api_defines.h> 45 #include <tee/tee_cryp_provider.h> 46 #include <trace.h> 47 #include <utee_defines.h> 48 #include <util.h> 49 50 #include "pager_private.h" 51 52 #define PAGER_AE_KEY_BITS 256 53 54 struct pager_rw_pstate { 55 uint64_t iv; 56 uint8_t tag[PAGER_AES_GCM_TAG_LEN]; 57 }; 58 59 enum area_type { 60 AREA_TYPE_RO, 61 AREA_TYPE_RW, 62 AREA_TYPE_LOCK, 63 }; 64 65 struct tee_pager_area { 66 union { 67 const uint8_t *hashes; 68 struct pager_rw_pstate *rwp; 69 } u; 70 uint8_t *store; 71 enum area_type type; 72 uint32_t flags; 73 vaddr_t base; 74 size_t size; 75 struct pgt *pgt; 76 TAILQ_ENTRY(tee_pager_area) link; 77 }; 78 79 TAILQ_HEAD(tee_pager_area_head, tee_pager_area); 80 81 static struct tee_pager_area_head tee_pager_area_head = 82 TAILQ_HEAD_INITIALIZER(tee_pager_area_head); 83 84 #define INVALID_PGIDX UINT_MAX 85 86 /* 87 * struct tee_pager_pmem - Represents a physical page used for paging. 88 * 89 * @pgidx an index of the entry in area->ti. 90 * @va_alias Virtual address where the physical page always is aliased. 91 * Used during remapping of the page when the content need to 92 * be updated before it's available at the new location. 93 * @area a pointer to the pager area 94 */ 95 struct tee_pager_pmem { 96 unsigned pgidx; 97 void *va_alias; 98 struct tee_pager_area *area; 99 TAILQ_ENTRY(tee_pager_pmem) link; 100 }; 101 102 /* The list of physical pages. The first page in the list is the oldest */ 103 TAILQ_HEAD(tee_pager_pmem_head, tee_pager_pmem); 104 105 static struct tee_pager_pmem_head tee_pager_pmem_head = 106 TAILQ_HEAD_INITIALIZER(tee_pager_pmem_head); 107 108 static struct tee_pager_pmem_head tee_pager_lock_pmem_head = 109 TAILQ_HEAD_INITIALIZER(tee_pager_lock_pmem_head); 110 111 static uint8_t pager_ae_key[PAGER_AE_KEY_BITS / 8]; 112 113 /* number of pages hidden */ 114 #define TEE_PAGER_NHIDE (tee_pager_npages / 3) 115 116 /* Number of registered physical pages, used hiding pages. */ 117 static size_t tee_pager_npages; 118 119 #ifdef CFG_WITH_STATS 120 static struct tee_pager_stats pager_stats; 121 122 static inline void incr_ro_hits(void) 123 { 124 pager_stats.ro_hits++; 125 } 126 127 static inline void incr_rw_hits(void) 128 { 129 pager_stats.rw_hits++; 130 } 131 132 static inline void incr_hidden_hits(void) 133 { 134 pager_stats.hidden_hits++; 135 } 136 137 static inline void incr_zi_released(void) 138 { 139 pager_stats.zi_released++; 140 } 141 142 static inline void incr_npages_all(void) 143 { 144 pager_stats.npages_all++; 145 } 146 147 static inline void set_npages(void) 148 { 149 pager_stats.npages = tee_pager_npages; 150 } 151 152 void tee_pager_get_stats(struct tee_pager_stats *stats) 153 { 154 *stats = pager_stats; 155 156 pager_stats.hidden_hits = 0; 157 pager_stats.ro_hits = 0; 158 pager_stats.rw_hits = 0; 159 pager_stats.zi_released = 0; 160 } 161 162 #else /* CFG_WITH_STATS */ 163 static inline void incr_ro_hits(void) { } 164 static inline void incr_rw_hits(void) { } 165 static inline void incr_hidden_hits(void) { } 166 static inline void incr_zi_released(void) { } 167 static inline void incr_npages_all(void) { } 168 static inline void set_npages(void) { } 169 170 void tee_pager_get_stats(struct tee_pager_stats *stats) 171 { 172 memset(stats, 0, sizeof(struct tee_pager_stats)); 173 } 174 #endif /* CFG_WITH_STATS */ 175 176 static struct pgt pager_core_pgt; 177 struct core_mmu_table_info tee_pager_tbl_info; 178 static struct core_mmu_table_info pager_alias_tbl_info; 179 180 static unsigned pager_spinlock = SPINLOCK_UNLOCK; 181 182 /* Defines the range of the alias area */ 183 static tee_mm_entry_t *pager_alias_area; 184 /* 185 * Physical pages are added in a stack like fashion to the alias area, 186 * @pager_alias_next_free gives the address of next free entry if 187 * @pager_alias_next_free is != 0 188 */ 189 static uintptr_t pager_alias_next_free; 190 191 static uint32_t pager_lock(void) 192 { 193 uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 194 195 cpu_spin_lock(&pager_spinlock); 196 return exceptions; 197 } 198 199 static void pager_unlock(uint32_t exceptions) 200 { 201 cpu_spin_unlock(&pager_spinlock); 202 thread_set_exceptions(exceptions); 203 } 204 205 static void set_alias_area(tee_mm_entry_t *mm) 206 { 207 struct core_mmu_table_info *ti = &pager_alias_tbl_info; 208 size_t tbl_va_size; 209 unsigned idx; 210 unsigned last_idx; 211 vaddr_t smem = tee_mm_get_smem(mm); 212 size_t nbytes = tee_mm_get_bytes(mm); 213 214 DMSG("0x%" PRIxVA " - 0x%" PRIxVA, smem, smem + nbytes); 215 216 if (pager_alias_area) 217 panic("null pager_alias_area"); 218 219 if (!ti->num_entries && !core_mmu_find_table(smem, UINT_MAX, ti)) 220 panic("Can't find translation table"); 221 222 if ((1 << ti->shift) != SMALL_PAGE_SIZE) 223 panic("Unsupported page size in translation table"); 224 225 tbl_va_size = (1 << ti->shift) * ti->num_entries; 226 if (!core_is_buffer_inside(smem, nbytes, 227 ti->va_base, tbl_va_size)) { 228 EMSG("area 0x%" PRIxVA " len 0x%zx doesn't fit it translation table 0x%" PRIxVA " len 0x%zx", 229 smem, nbytes, ti->va_base, tbl_va_size); 230 panic(); 231 } 232 233 if (smem & SMALL_PAGE_MASK || nbytes & SMALL_PAGE_MASK) 234 panic("invalid area alignment"); 235 236 pager_alias_area = mm; 237 pager_alias_next_free = smem; 238 239 /* Clear all mapping in the alias area */ 240 idx = core_mmu_va2idx(ti, smem); 241 last_idx = core_mmu_va2idx(ti, smem + nbytes); 242 for (; idx < last_idx; idx++) 243 core_mmu_set_entry(ti, idx, 0, 0); 244 245 /* TODO only invalidate entries touched above */ 246 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 247 } 248 249 static void generate_ae_key(void) 250 { 251 if (rng_generate(pager_ae_key, sizeof(pager_ae_key)) != TEE_SUCCESS) 252 panic("failed to generate random"); 253 } 254 255 void tee_pager_init(tee_mm_entry_t *mm_alias) 256 { 257 set_alias_area(mm_alias); 258 generate_ae_key(); 259 } 260 261 static void *pager_add_alias_page(paddr_t pa) 262 { 263 unsigned idx; 264 struct core_mmu_table_info *ti = &pager_alias_tbl_info; 265 uint32_t attr = TEE_MATTR_VALID_BLOCK | TEE_MATTR_GLOBAL | 266 (TEE_MATTR_CACHE_CACHED << TEE_MATTR_CACHE_SHIFT) | 267 TEE_MATTR_SECURE | TEE_MATTR_PRW; 268 269 DMSG("0x%" PRIxPA, pa); 270 271 if (!pager_alias_next_free || !ti->num_entries) 272 panic("invalid alias entry"); 273 274 idx = core_mmu_va2idx(ti, pager_alias_next_free); 275 core_mmu_set_entry(ti, idx, pa, attr); 276 pgt_inc_used_entries(&pager_core_pgt); 277 pager_alias_next_free += SMALL_PAGE_SIZE; 278 if (pager_alias_next_free >= (tee_mm_get_smem(pager_alias_area) + 279 tee_mm_get_bytes(pager_alias_area))) 280 pager_alias_next_free = 0; 281 return (void *)core_mmu_idx2va(ti, idx); 282 } 283 284 static struct tee_pager_area *alloc_area(struct pgt *pgt, 285 vaddr_t base, size_t size, 286 uint32_t flags, const void *store, 287 const void *hashes) 288 { 289 struct tee_pager_area *area = calloc(1, sizeof(*area)); 290 enum area_type at; 291 tee_mm_entry_t *mm_store = NULL; 292 293 if (!area) 294 return NULL; 295 296 if (flags & (TEE_MATTR_PW | TEE_MATTR_UW)) { 297 if (flags & TEE_MATTR_LOCKED) { 298 at = AREA_TYPE_LOCK; 299 goto out; 300 } 301 mm_store = tee_mm_alloc(&tee_mm_sec_ddr, size); 302 if (!mm_store) 303 goto bad; 304 area->store = phys_to_virt(tee_mm_get_smem(mm_store), 305 MEM_AREA_TA_RAM); 306 if (!area->store) 307 goto bad; 308 area->u.rwp = calloc(size / SMALL_PAGE_SIZE, 309 sizeof(struct pager_rw_pstate)); 310 if (!area->u.rwp) 311 goto bad; 312 at = AREA_TYPE_RW; 313 } else { 314 area->store = (void *)store; 315 area->u.hashes = hashes; 316 at = AREA_TYPE_RO; 317 } 318 out: 319 area->pgt = pgt; 320 area->base = base; 321 area->size = size; 322 area->flags = flags; 323 area->type = at; 324 return area; 325 bad: 326 tee_mm_free(mm_store); 327 free(area->u.rwp); 328 free(area); 329 return NULL; 330 } 331 332 static void area_insert_tail(struct tee_pager_area *area) 333 { 334 uint32_t exceptions = pager_lock(); 335 336 TAILQ_INSERT_TAIL(&tee_pager_area_head, area, link); 337 338 pager_unlock(exceptions); 339 } 340 KEEP_PAGER(area_insert_tail); 341 342 static size_t tbl_usage_count(struct pgt *pgt) 343 { 344 size_t n; 345 paddr_t pa; 346 size_t usage = 0; 347 348 for (n = 0; n < tee_pager_tbl_info.num_entries; n++) { 349 core_mmu_get_entry_primitive(pgt->tbl, tee_pager_tbl_info.level, 350 n, &pa, NULL); 351 if (pa) 352 usage++; 353 } 354 return usage; 355 } 356 357 bool tee_pager_add_core_area(vaddr_t base, size_t size, uint32_t flags, 358 const void *store, const void *hashes) 359 { 360 struct tee_pager_area *area; 361 size_t tbl_va_size; 362 struct core_mmu_table_info *ti = &tee_pager_tbl_info; 363 364 DMSG("0x%" PRIxPTR " - 0x%" PRIxPTR " : flags 0x%x, store %p, hashes %p", 365 base, base + size, flags, store, hashes); 366 367 if (base & SMALL_PAGE_MASK || size & SMALL_PAGE_MASK || !size) { 368 EMSG("invalid pager area [%" PRIxVA " +0x%zx]", base, size); 369 panic(); 370 } 371 372 if (!(flags & TEE_MATTR_PW) && (!store || !hashes)) 373 panic("write pages cannot provide store or hashes"); 374 375 if ((flags & TEE_MATTR_PW) && (store || hashes)) 376 panic("non-write pages must provide store and hashes"); 377 378 if (!pager_core_pgt.tbl) { 379 pager_core_pgt.tbl = ti->table; 380 pgt_set_used_entries(&pager_core_pgt, 381 tbl_usage_count(&pager_core_pgt)); 382 } 383 384 tbl_va_size = (1 << ti->shift) * ti->num_entries; 385 if (!core_is_buffer_inside(base, size, ti->va_base, tbl_va_size)) { 386 DMSG("area 0x%" PRIxPTR " len 0x%zx doesn't fit it translation table 0x%" PRIxVA " len 0x%zx", 387 base, size, ti->va_base, tbl_va_size); 388 return false; 389 } 390 391 area = alloc_area(&pager_core_pgt, base, size, flags, store, hashes); 392 if (!area) 393 return false; 394 395 area_insert_tail(area); 396 return true; 397 } 398 399 static struct tee_pager_area *find_area(struct tee_pager_area_head *areas, 400 vaddr_t va) 401 { 402 struct tee_pager_area *area; 403 404 if (!areas) 405 return NULL; 406 407 TAILQ_FOREACH(area, areas, link) { 408 if (core_is_buffer_inside(va, 1, area->base, area->size)) 409 return area; 410 } 411 return NULL; 412 } 413 414 #ifdef CFG_PAGED_USER_TA 415 static struct tee_pager_area *find_uta_area(vaddr_t va) 416 { 417 struct tee_ta_ctx *ctx = thread_get_tsd()->ctx; 418 419 if (!ctx || !is_user_ta_ctx(ctx)) 420 return NULL; 421 return find_area(to_user_ta_ctx(ctx)->areas, va); 422 } 423 #else 424 static struct tee_pager_area *find_uta_area(vaddr_t va __unused) 425 { 426 return NULL; 427 } 428 #endif /*CFG_PAGED_USER_TA*/ 429 430 431 static uint32_t get_area_mattr(uint32_t area_flags) 432 { 433 uint32_t attr = TEE_MATTR_VALID_BLOCK | TEE_MATTR_SECURE | 434 TEE_MATTR_CACHE_CACHED << TEE_MATTR_CACHE_SHIFT | 435 (area_flags & (TEE_MATTR_PRWX | TEE_MATTR_URWX)); 436 437 if (!(area_flags & (TEE_MATTR_UR | TEE_MATTR_UX | TEE_MATTR_UW))) 438 attr |= TEE_MATTR_GLOBAL; 439 440 return attr; 441 } 442 443 static paddr_t get_pmem_pa(struct tee_pager_pmem *pmem) 444 { 445 paddr_t pa; 446 unsigned idx; 447 448 idx = core_mmu_va2idx(&pager_alias_tbl_info, (vaddr_t)pmem->va_alias); 449 core_mmu_get_entry(&pager_alias_tbl_info, idx, &pa, NULL); 450 return pa; 451 } 452 453 static bool decrypt_page(struct pager_rw_pstate *rwp, const void *src, 454 void *dst) 455 { 456 struct pager_aes_gcm_iv iv = { 457 { (vaddr_t)rwp, rwp->iv >> 32, rwp->iv } 458 }; 459 460 return pager_aes_gcm_decrypt(pager_ae_key, sizeof(pager_ae_key), 461 &iv, rwp->tag, src, dst, SMALL_PAGE_SIZE); 462 } 463 464 static void encrypt_page(struct pager_rw_pstate *rwp, void *src, void *dst) 465 { 466 struct pager_aes_gcm_iv iv; 467 468 assert((rwp->iv + 1) > rwp->iv); 469 rwp->iv++; 470 /* 471 * IV is constructed as recommended in section "8.2.1 Deterministic 472 * Construction" of "Recommendation for Block Cipher Modes of 473 * Operation: Galois/Counter Mode (GCM) and GMAC", 474 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf 475 */ 476 iv.iv[0] = (vaddr_t)rwp; 477 iv.iv[1] = rwp->iv >> 32; 478 iv.iv[2] = rwp->iv; 479 480 if (!pager_aes_gcm_encrypt(pager_ae_key, sizeof(pager_ae_key), 481 &iv, rwp->tag, 482 src, dst, SMALL_PAGE_SIZE)) 483 panic("gcm failed"); 484 } 485 486 static void tee_pager_load_page(struct tee_pager_area *area, vaddr_t page_va, 487 void *va_alias) 488 { 489 size_t idx = (page_va - area->base) >> SMALL_PAGE_SHIFT; 490 const void *stored_page = area->store + idx * SMALL_PAGE_SIZE; 491 492 switch (area->type) { 493 case AREA_TYPE_RO: 494 { 495 const void *hash = area->u.hashes + 496 idx * TEE_SHA256_HASH_SIZE; 497 498 memcpy(va_alias, stored_page, SMALL_PAGE_SIZE); 499 incr_ro_hits(); 500 501 if (hash_sha256_check(hash, va_alias, 502 SMALL_PAGE_SIZE) != TEE_SUCCESS) { 503 EMSG("PH 0x%" PRIxVA " failed", page_va); 504 panic(); 505 } 506 } 507 break; 508 case AREA_TYPE_RW: 509 FMSG("Restore %p %#" PRIxVA " iv %#" PRIx64, 510 va_alias, page_va, area->u.rwp[idx].iv); 511 if (!area->u.rwp[idx].iv) 512 memset(va_alias, 0, SMALL_PAGE_SIZE); 513 else if (!decrypt_page(&area->u.rwp[idx], stored_page, 514 va_alias)) { 515 EMSG("PH 0x%" PRIxVA " failed", page_va); 516 panic(); 517 } 518 incr_rw_hits(); 519 break; 520 case AREA_TYPE_LOCK: 521 FMSG("Zero init %p %#" PRIxVA, va_alias, page_va); 522 memset(va_alias, 0, SMALL_PAGE_SIZE); 523 break; 524 default: 525 panic(); 526 } 527 } 528 529 static void tee_pager_save_page(struct tee_pager_pmem *pmem, uint32_t attr) 530 { 531 const uint32_t dirty_bits = TEE_MATTR_PW | TEE_MATTR_UW | 532 TEE_MATTR_HIDDEN_DIRTY_BLOCK; 533 534 if (pmem->area->type == AREA_TYPE_RW && (attr & dirty_bits)) { 535 size_t offs = pmem->area->base & CORE_MMU_PGDIR_MASK; 536 size_t idx = pmem->pgidx - (offs >> SMALL_PAGE_SHIFT); 537 void *stored_page = pmem->area->store + idx * SMALL_PAGE_SIZE; 538 539 assert(pmem->area->flags & (TEE_MATTR_PW | TEE_MATTR_UW)); 540 encrypt_page(&pmem->area->u.rwp[idx], pmem->va_alias, 541 stored_page); 542 FMSG("Saved %#" PRIxVA " iv %#" PRIx64, 543 pmem->area->base + idx * SMALL_PAGE_SIZE, 544 pmem->area->u.rwp[idx].iv); 545 } 546 } 547 548 static void area_get_entry(struct tee_pager_area *area, size_t idx, 549 paddr_t *pa, uint32_t *attr) 550 { 551 assert(area->pgt); 552 assert(idx < tee_pager_tbl_info.num_entries); 553 core_mmu_get_entry_primitive(area->pgt->tbl, tee_pager_tbl_info.level, 554 idx, pa, attr); 555 } 556 557 static void area_set_entry(struct tee_pager_area *area, size_t idx, 558 paddr_t pa, uint32_t attr) 559 { 560 assert(area->pgt); 561 assert(idx < tee_pager_tbl_info.num_entries); 562 core_mmu_set_entry_primitive(area->pgt->tbl, tee_pager_tbl_info.level, 563 idx, pa, attr); 564 } 565 566 static size_t area_va2idx(struct tee_pager_area *area, vaddr_t va) 567 { 568 return (va - (area->base & ~CORE_MMU_PGDIR_MASK)) >> SMALL_PAGE_SHIFT; 569 } 570 571 static vaddr_t __maybe_unused area_idx2va(struct tee_pager_area *area, 572 size_t idx) 573 { 574 return (idx << SMALL_PAGE_SHIFT) + (area->base & ~CORE_MMU_PGDIR_MASK); 575 } 576 577 #ifdef CFG_PAGED_USER_TA 578 static void free_area(struct tee_pager_area *area) 579 { 580 tee_mm_free(tee_mm_find(&tee_mm_sec_ddr, 581 virt_to_phys(area->store))); 582 if (area->type == AREA_TYPE_RW) 583 free(area->u.rwp); 584 free(area); 585 } 586 587 static bool pager_add_uta_area(struct user_ta_ctx *utc, vaddr_t base, 588 size_t size) 589 { 590 struct tee_pager_area *area; 591 uint32_t flags; 592 vaddr_t b = base; 593 size_t s = size; 594 595 if (!utc->areas) { 596 utc->areas = malloc(sizeof(*utc->areas)); 597 if (!utc->areas) 598 return false; 599 TAILQ_INIT(utc->areas); 600 } 601 602 flags = TEE_MATTR_PRW | TEE_MATTR_URWX; 603 604 while (s) { 605 size_t s2; 606 607 if (find_area(utc->areas, b)) 608 return false; 609 610 s2 = MIN(CORE_MMU_PGDIR_SIZE - (b & CORE_MMU_PGDIR_MASK), s); 611 612 /* Table info will be set when the context is activated. */ 613 area = alloc_area(NULL, b, s2, flags, NULL, NULL); 614 if (!area) 615 return false; 616 TAILQ_INSERT_TAIL(utc->areas, area, link); 617 b += s2; 618 s -= s2; 619 } 620 621 return true; 622 } 623 624 bool tee_pager_add_uta_area(struct user_ta_ctx *utc, vaddr_t base, size_t size) 625 { 626 return pager_add_uta_area(utc, base, size); 627 } 628 629 void tee_pager_rem_uta_areas(struct user_ta_ctx *utc) 630 { 631 struct tee_pager_area *area; 632 633 if (!utc->areas) 634 return; 635 636 while (true) { 637 area = TAILQ_FIRST(utc->areas); 638 if (!area) 639 break; 640 TAILQ_REMOVE(utc->areas, area, link); 641 free_area(area); 642 } 643 644 free(utc->areas); 645 } 646 647 bool tee_pager_set_uta_area_attr(struct user_ta_ctx *utc, vaddr_t base, 648 size_t size, uint32_t flags) 649 { 650 bool ret; 651 vaddr_t b = base; 652 size_t s = size; 653 size_t s2; 654 struct tee_pager_area *area = find_area(utc->areas, b); 655 uint32_t exceptions; 656 struct tee_pager_pmem *pmem; 657 paddr_t pa; 658 uint32_t a; 659 uint32_t f; 660 661 f = (flags & TEE_MATTR_URWX) | TEE_MATTR_UR | TEE_MATTR_PR; 662 if (f & TEE_MATTR_UW) 663 f |= TEE_MATTR_PW; 664 f = get_area_mattr(f); 665 666 exceptions = pager_lock(); 667 668 while (s) { 669 s2 = MIN(CORE_MMU_PGDIR_SIZE - (b & CORE_MMU_PGDIR_MASK), s); 670 if (!area || area->base != b || area->size != s2) { 671 ret = false; 672 goto out; 673 } 674 b += s2; 675 s -= s2; 676 677 TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) { 678 if (pmem->area != area) 679 continue; 680 area_get_entry(pmem->area, pmem->pgidx, &pa, &a); 681 if (a & TEE_MATTR_VALID_BLOCK) 682 assert(pa == get_pmem_pa(pmem)); 683 else 684 pa = get_pmem_pa(pmem); 685 if (a == f) 686 continue; 687 area_set_entry(pmem->area, pmem->pgidx, 0, 0); 688 /* TODO only invalidate entries touched above */ 689 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 690 if (!(flags & TEE_MATTR_UW)) 691 tee_pager_save_page(pmem, a); 692 area_set_entry(pmem->area, pmem->pgidx, pa, f); 693 } 694 695 area->flags = f; 696 area = TAILQ_NEXT(area, link); 697 } 698 699 ret = true; 700 out: 701 pager_unlock(exceptions); 702 return ret; 703 } 704 KEEP_PAGER(tee_pager_set_uta_area_attr); 705 #endif /*CFG_PAGED_USER_TA*/ 706 707 static bool tee_pager_unhide_page(vaddr_t page_va) 708 { 709 struct tee_pager_pmem *pmem; 710 711 TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) { 712 paddr_t pa; 713 uint32_t attr; 714 715 if (pmem->pgidx == INVALID_PGIDX) 716 continue; 717 718 area_get_entry(pmem->area, pmem->pgidx, &pa, &attr); 719 720 if (!(attr & 721 (TEE_MATTR_HIDDEN_BLOCK | TEE_MATTR_HIDDEN_DIRTY_BLOCK))) 722 continue; 723 724 if (area_va2idx(pmem->area, page_va) == pmem->pgidx) { 725 uint32_t a = get_area_mattr(pmem->area->flags); 726 727 /* page is hidden, show and move to back */ 728 if (pa != get_pmem_pa(pmem)) 729 panic("unexpected pa"); 730 731 /* 732 * If it's not a dirty block, then it should be 733 * read only. 734 */ 735 if (!(attr & TEE_MATTR_HIDDEN_DIRTY_BLOCK)) 736 a &= ~(TEE_MATTR_PW | TEE_MATTR_UW); 737 else 738 FMSG("Unhide %#" PRIxVA, page_va); 739 740 if (page_va == 0x8000a000) 741 FMSG("unhide %#" PRIxVA " a %#" PRIX32, 742 page_va, a); 743 area_set_entry(pmem->area, pmem->pgidx, pa, a); 744 745 TAILQ_REMOVE(&tee_pager_pmem_head, pmem, link); 746 TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link); 747 748 /* TODO only invalidate entry touched above */ 749 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 750 751 incr_hidden_hits(); 752 return true; 753 } 754 } 755 756 return false; 757 } 758 759 static void tee_pager_hide_pages(void) 760 { 761 struct tee_pager_pmem *pmem; 762 size_t n = 0; 763 764 TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) { 765 paddr_t pa; 766 uint32_t attr; 767 uint32_t a; 768 769 if (n >= TEE_PAGER_NHIDE) 770 break; 771 n++; 772 773 /* we cannot hide pages when pmem->area is not defined. */ 774 if (!pmem->area) 775 continue; 776 777 area_get_entry(pmem->area, pmem->pgidx, &pa, &attr); 778 if (!(attr & TEE_MATTR_VALID_BLOCK)) 779 continue; 780 781 assert(pa == get_pmem_pa(pmem)); 782 if (attr & (TEE_MATTR_PW | TEE_MATTR_UW)){ 783 a = TEE_MATTR_HIDDEN_DIRTY_BLOCK; 784 FMSG("Hide %#" PRIxVA, 785 area_idx2va(pmem->area, pmem->pgidx)); 786 } else 787 a = TEE_MATTR_HIDDEN_BLOCK; 788 area_set_entry(pmem->area, pmem->pgidx, pa, a); 789 } 790 791 /* TODO only invalidate entries touched above */ 792 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 793 } 794 795 /* 796 * Find mapped pmem, hide and move to pageble pmem. 797 * Return false if page was not mapped, and true if page was mapped. 798 */ 799 static bool tee_pager_release_one_phys(struct tee_pager_area *area, 800 vaddr_t page_va) 801 { 802 struct tee_pager_pmem *pmem; 803 unsigned pgidx; 804 paddr_t pa; 805 uint32_t attr; 806 807 pgidx = area_va2idx(area, page_va); 808 area_get_entry(area, pgidx, &pa, &attr); 809 810 FMSG("%" PRIxVA " : %" PRIxPA "|%x", page_va, pa, attr); 811 812 TAILQ_FOREACH(pmem, &tee_pager_lock_pmem_head, link) { 813 if (pmem->area != area || pmem->pgidx != pgidx) 814 continue; 815 816 assert(pa == get_pmem_pa(pmem)); 817 area_set_entry(area, pgidx, 0, 0); 818 pgt_dec_used_entries(area->pgt); 819 TAILQ_REMOVE(&tee_pager_lock_pmem_head, pmem, link); 820 pmem->area = NULL; 821 pmem->pgidx = INVALID_PGIDX; 822 tee_pager_npages++; 823 set_npages(); 824 TAILQ_INSERT_HEAD(&tee_pager_pmem_head, pmem, link); 825 incr_zi_released(); 826 return true; 827 } 828 829 return false; 830 } 831 832 /* Finds the oldest page and unmats it from its old virtual address */ 833 static struct tee_pager_pmem *tee_pager_get_page(struct tee_pager_area *area) 834 { 835 struct tee_pager_pmem *pmem; 836 837 pmem = TAILQ_FIRST(&tee_pager_pmem_head); 838 if (!pmem) { 839 EMSG("No pmem entries"); 840 return NULL; 841 } 842 if (pmem->pgidx != INVALID_PGIDX) { 843 uint32_t a; 844 845 assert(pmem->area && pmem->area->pgt); 846 area_get_entry(pmem->area, pmem->pgidx, NULL, &a); 847 area_set_entry(pmem->area, pmem->pgidx, 0, 0); 848 pgt_dec_used_entries(pmem->area->pgt); 849 /* TODO only invalidate entries touched above */ 850 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 851 tee_pager_save_page(pmem, a); 852 } 853 854 TAILQ_REMOVE(&tee_pager_pmem_head, pmem, link); 855 pmem->pgidx = INVALID_PGIDX; 856 pmem->area = NULL; 857 if (area->type == AREA_TYPE_LOCK) { 858 /* Move page to lock list */ 859 if (tee_pager_npages <= 0) 860 panic("running out of page"); 861 tee_pager_npages--; 862 set_npages(); 863 TAILQ_INSERT_TAIL(&tee_pager_lock_pmem_head, pmem, link); 864 } else { 865 /* move page to back */ 866 TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link); 867 } 868 869 return pmem; 870 } 871 872 static bool pager_update_permissions(struct tee_pager_area *area, 873 struct abort_info *ai, bool *handled) 874 { 875 unsigned int pgidx = area_va2idx(area, ai->va); 876 uint32_t attr; 877 paddr_t pa; 878 879 *handled = false; 880 881 area_get_entry(area, pgidx, &pa, &attr); 882 883 /* Not mapped */ 884 if (!(attr & TEE_MATTR_VALID_BLOCK)) 885 return false; 886 887 /* Not readable, should not happen */ 888 if (abort_is_user_exception(ai)) { 889 if (!(attr & TEE_MATTR_UR)) 890 return true; 891 } else { 892 if (!(attr & TEE_MATTR_PR)) { 893 abort_print_error(ai); 894 panic(); 895 } 896 } 897 898 switch (core_mmu_get_fault_type(ai->fault_descr)) { 899 case CORE_MMU_FAULT_TRANSLATION: 900 case CORE_MMU_FAULT_READ_PERMISSION: 901 if (ai->abort_type == ABORT_TYPE_PREFETCH) { 902 /* Check attempting to execute from an NOX page */ 903 if (abort_is_user_exception(ai)) { 904 if (!(attr & TEE_MATTR_UX)) 905 return true; 906 } else { 907 if (!(attr & TEE_MATTR_PX)) { 908 abort_print_error(ai); 909 panic(); 910 } 911 } 912 } 913 /* Since the page is mapped now it's OK */ 914 break; 915 case CORE_MMU_FAULT_WRITE_PERMISSION: 916 /* Check attempting to write to an RO page */ 917 if (abort_is_user_exception(ai)) { 918 if (!(area->flags & TEE_MATTR_UW)) 919 return true; 920 if (!(attr & TEE_MATTR_UW)) { 921 FMSG("Dirty %p", 922 (void *)(ai->va & ~SMALL_PAGE_MASK)); 923 area_set_entry(area, pgidx, pa, 924 get_area_mattr(area->flags)); 925 /* TODO only invalidate entry above */ 926 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 927 } 928 929 } else { 930 if (!(area->flags & TEE_MATTR_PW)) { 931 abort_print_error(ai); 932 panic(); 933 } 934 if (!(attr & TEE_MATTR_PW)) { 935 FMSG("Dirty %p", 936 (void *)(ai->va & ~SMALL_PAGE_MASK)); 937 area_set_entry(area, pgidx, pa, 938 get_area_mattr(area->flags)); 939 /* TODO only invalidate entry above */ 940 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 941 } 942 } 943 /* Since permissions has been updated now it's OK */ 944 break; 945 default: 946 /* Some fault we can't deal with */ 947 if (abort_is_user_exception(ai)) 948 return true; 949 abort_print_error(ai); 950 panic(); 951 } 952 *handled = true; 953 return true; 954 } 955 956 #ifdef CFG_TEE_CORE_DEBUG 957 static void stat_handle_fault(void) 958 { 959 static size_t num_faults; 960 static size_t min_npages = SIZE_MAX; 961 static size_t total_min_npages = SIZE_MAX; 962 963 num_faults++; 964 if ((num_faults % 1024) == 0 || tee_pager_npages < total_min_npages) { 965 DMSG("nfaults %zu npages %zu (min %zu)", 966 num_faults, tee_pager_npages, min_npages); 967 min_npages = tee_pager_npages; /* reset */ 968 } 969 if (tee_pager_npages < min_npages) 970 min_npages = tee_pager_npages; 971 if (tee_pager_npages < total_min_npages) 972 total_min_npages = tee_pager_npages; 973 } 974 #else 975 static void stat_handle_fault(void) 976 { 977 } 978 #endif 979 980 bool tee_pager_handle_fault(struct abort_info *ai) 981 { 982 struct tee_pager_area *area; 983 vaddr_t page_va = ai->va & ~SMALL_PAGE_MASK; 984 uint32_t exceptions; 985 bool ret; 986 987 #ifdef TEE_PAGER_DEBUG_PRINT 988 abort_print(ai); 989 #endif 990 991 /* 992 * We're updating pages that can affect several active CPUs at a 993 * time below. We end up here because a thread tries to access some 994 * memory that isn't available. We have to be careful when making 995 * that memory available as other threads may succeed in accessing 996 * that address the moment after we've made it available. 997 * 998 * That means that we can't just map the memory and populate the 999 * page, instead we use the aliased mapping to populate the page 1000 * and once everything is ready we map it. 1001 */ 1002 exceptions = pager_lock(); 1003 1004 stat_handle_fault(); 1005 1006 /* check if the access is valid */ 1007 if (abort_is_user_exception(ai)) { 1008 area = find_uta_area(ai->va); 1009 1010 } else { 1011 area = find_area(&tee_pager_area_head, ai->va); 1012 if (!area) 1013 area = find_uta_area(ai->va); 1014 } 1015 if (!area) { 1016 ret = false; 1017 goto out; 1018 } 1019 1020 if (!tee_pager_unhide_page(page_va)) { 1021 struct tee_pager_pmem *pmem = NULL; 1022 uint32_t attr; 1023 1024 /* 1025 * The page wasn't hidden, but some other core may have 1026 * updated the table entry before we got here or we need 1027 * to make a read-only page read-write (dirty). 1028 */ 1029 if (pager_update_permissions(area, ai, &ret)) { 1030 /* 1031 * Nothing more to do with the abort. The problem 1032 * could already have been dealt with from another 1033 * core or if ret is false the TA will be paniced. 1034 */ 1035 goto out; 1036 } 1037 1038 pmem = tee_pager_get_page(area); 1039 if (!pmem) { 1040 abort_print(ai); 1041 panic(); 1042 } 1043 1044 /* load page code & data */ 1045 tee_pager_load_page(area, page_va, pmem->va_alias); 1046 1047 /* 1048 * We've updated the page using the aliased mapping and 1049 * some cache maintenence is now needed if it's an 1050 * executable page. 1051 * 1052 * Since the d-cache is a Physically-indexed, 1053 * physically-tagged (PIPT) cache we can clean the aliased 1054 * address instead of the real virtual address. 1055 * 1056 * The i-cache can also be PIPT, but may be something else 1057 * to, to keep it simple we invalidate the entire i-cache. 1058 * As a future optimization we may invalidate only the 1059 * aliased area if it a PIPT cache else the entire cache. 1060 */ 1061 if (area->flags & (TEE_MATTR_PX | TEE_MATTR_UX)) { 1062 /* 1063 * Doing these operations to LoUIS (Level of 1064 * unification, Inner Shareable) would be enough 1065 */ 1066 cache_maintenance_l1(DCACHE_AREA_CLEAN, 1067 pmem->va_alias, SMALL_PAGE_SIZE); 1068 1069 cache_maintenance_l1(ICACHE_INVALIDATE, NULL, 0); 1070 } 1071 1072 pmem->area = area; 1073 pmem->pgidx = area_va2idx(area, ai->va); 1074 attr = get_area_mattr(area->flags) & 1075 ~(TEE_MATTR_PW | TEE_MATTR_UW); 1076 area_set_entry(area, pmem->pgidx, get_pmem_pa(pmem), attr); 1077 pgt_inc_used_entries(area->pgt); 1078 1079 FMSG("Mapped 0x%" PRIxVA " -> 0x%" PRIxPA, 1080 area_idx2va(area, pmem->pgidx), get_pmem_pa(pmem)); 1081 1082 } 1083 1084 tee_pager_hide_pages(); 1085 ret = true; 1086 out: 1087 pager_unlock(exceptions); 1088 return ret; 1089 } 1090 1091 void tee_pager_add_pages(vaddr_t vaddr, size_t npages, bool unmap) 1092 { 1093 struct core_mmu_table_info *ti = &tee_pager_tbl_info; 1094 size_t n; 1095 1096 DMSG("0x%" PRIxVA " - 0x%" PRIxVA " : %d", 1097 vaddr, vaddr + npages * SMALL_PAGE_SIZE, (int)unmap); 1098 1099 /* setup memory */ 1100 for (n = 0; n < npages; n++) { 1101 struct tee_pager_pmem *pmem; 1102 vaddr_t va = vaddr + n * SMALL_PAGE_SIZE; 1103 unsigned pgidx = core_mmu_va2idx(ti, va); 1104 paddr_t pa; 1105 uint32_t attr; 1106 1107 /* 1108 * Note that we can only support adding pages in the 1109 * valid range of this table info, currently not a problem. 1110 */ 1111 core_mmu_get_entry(ti, pgidx, &pa, &attr); 1112 1113 /* Ignore unmapped pages/blocks */ 1114 if (!(attr & TEE_MATTR_VALID_BLOCK)) 1115 continue; 1116 1117 pmem = malloc(sizeof(struct tee_pager_pmem)); 1118 if (!pmem) 1119 panic("out of mem"); 1120 1121 pmem->va_alias = pager_add_alias_page(pa); 1122 1123 if (unmap) { 1124 pmem->area = NULL; 1125 pmem->pgidx = INVALID_PGIDX; 1126 core_mmu_set_entry(ti, pgidx, 0, 0); 1127 pgt_dec_used_entries(&pager_core_pgt); 1128 } else { 1129 /* 1130 * The page is still mapped, let's assign the area 1131 * and update the protection bits accordingly. 1132 */ 1133 pmem->area = find_area(&tee_pager_area_head, va); 1134 assert(pmem->area->pgt == &pager_core_pgt); 1135 pmem->pgidx = pgidx; 1136 assert(pa == get_pmem_pa(pmem)); 1137 area_set_entry(pmem->area, pgidx, pa, 1138 get_area_mattr(pmem->area->flags)); 1139 } 1140 1141 tee_pager_npages++; 1142 incr_npages_all(); 1143 set_npages(); 1144 TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link); 1145 } 1146 1147 /* Invalidate secure TLB */ 1148 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 1149 } 1150 1151 #ifdef CFG_PAGED_USER_TA 1152 static struct pgt *find_pgt(struct pgt *pgt, vaddr_t va) 1153 { 1154 struct pgt *p = pgt; 1155 1156 while (p && (va & ~CORE_MMU_PGDIR_MASK) != p->vabase) 1157 p = SLIST_NEXT(p, link); 1158 return p; 1159 } 1160 1161 void tee_pager_assign_uta_tables(struct user_ta_ctx *utc) 1162 { 1163 struct tee_pager_area *area; 1164 struct pgt *pgt = SLIST_FIRST(&thread_get_tsd()->pgt_cache); 1165 1166 TAILQ_FOREACH(area, utc->areas, link) { 1167 if (!area->pgt) 1168 area->pgt = find_pgt(pgt, area->base); 1169 else 1170 assert(area->pgt == find_pgt(pgt, area->base)); 1171 if (!area->pgt) 1172 panic(); 1173 } 1174 } 1175 1176 static void pager_save_and_release_entry(struct tee_pager_pmem *pmem) 1177 { 1178 uint32_t attr; 1179 1180 assert(pmem->area && pmem->area->pgt); 1181 1182 area_get_entry(pmem->area, pmem->pgidx, NULL, &attr); 1183 area_set_entry(pmem->area, pmem->pgidx, 0, 0); 1184 tee_pager_save_page(pmem, attr); 1185 assert(pmem->area->pgt->num_used_entries); 1186 pmem->area->pgt->num_used_entries--; 1187 pmem->pgidx = INVALID_PGIDX; 1188 pmem->area = NULL; 1189 } 1190 1191 void tee_pager_pgt_save_and_release_entries(struct pgt *pgt) 1192 { 1193 struct tee_pager_pmem *pmem; 1194 struct tee_pager_area *area; 1195 uint32_t exceptions = pager_lock(); 1196 1197 if (!pgt->num_used_entries) 1198 goto out; 1199 1200 TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) { 1201 if (!pmem->area || pmem->pgidx == INVALID_PGIDX) 1202 continue; 1203 if (pmem->area->pgt == pgt) 1204 pager_save_and_release_entry(pmem); 1205 } 1206 assert(!pgt->num_used_entries); 1207 1208 out: 1209 if (is_user_ta_ctx(pgt->ctx)) { 1210 TAILQ_FOREACH(area, to_user_ta_ctx(pgt->ctx)->areas, link) { 1211 if (area->pgt == pgt) 1212 area->pgt = NULL; 1213 } 1214 } 1215 1216 pager_unlock(exceptions); 1217 } 1218 KEEP_PAGER(tee_pager_pgt_save_and_release_entries); 1219 #endif /*CFG_PAGED_USER_TA*/ 1220 1221 void tee_pager_release_phys(void *addr, size_t size) 1222 { 1223 bool unmaped = false; 1224 vaddr_t va = (vaddr_t)addr; 1225 vaddr_t begin = ROUNDUP(va, SMALL_PAGE_SIZE); 1226 vaddr_t end = ROUNDDOWN(va + size, SMALL_PAGE_SIZE); 1227 struct tee_pager_area *area; 1228 uint32_t exceptions; 1229 1230 if (!size) 1231 return; 1232 1233 area = find_area(&tee_pager_area_head, begin); 1234 if (!area || 1235 area != find_area(&tee_pager_area_head, end - SMALL_PAGE_SIZE)) 1236 panic(); 1237 1238 exceptions = pager_lock(); 1239 1240 for (va = begin; va < end; va += SMALL_PAGE_SIZE) 1241 unmaped |= tee_pager_release_one_phys(area, va); 1242 1243 /* Invalidate secure TLB */ 1244 if (unmaped) 1245 core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0); 1246 1247 pager_unlock(exceptions); 1248 } 1249 KEEP_PAGER(tee_pager_release_phys); 1250 1251 void *tee_pager_alloc(size_t size, uint32_t flags) 1252 { 1253 tee_mm_entry_t *mm; 1254 uint32_t f = TEE_MATTR_PW | TEE_MATTR_PR | (flags & TEE_MATTR_LOCKED); 1255 1256 if (!size) 1257 return NULL; 1258 1259 mm = tee_mm_alloc(&tee_mm_vcore, ROUNDUP(size, SMALL_PAGE_SIZE)); 1260 if (!mm) 1261 return NULL; 1262 1263 tee_pager_add_core_area(tee_mm_get_smem(mm), tee_mm_get_bytes(mm), 1264 f, NULL, NULL); 1265 1266 return (void *)tee_mm_get_smem(mm); 1267 } 1268