xref: /optee_os/core/arch/arm/mm/tee_pager.c (revision f3bb2312f41f85790c77e749f096f10be36e9506)
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 = ROUNDUP(size, SMALL_PAGE_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 	struct thread_specific_data *tsd = thread_get_tsd();
627 	struct tee_pager_area *area;
628 	struct core_mmu_table_info dir_info = { NULL };
629 
630 	if (&utc->ctx != tsd->ctx) {
631 		/*
632 		 * Changes are to an utc that isn't active. Just add the
633 		 * areas page tables will be dealt with later.
634 		 */
635 		return pager_add_uta_area(utc, base, size);
636 	}
637 
638 	/*
639 	 * Assign page tables before adding areas to be able to tell which
640 	 * are newly added and should be removed in case of failure.
641 	 */
642 	tee_pager_assign_uta_tables(utc);
643 	if (!pager_add_uta_area(utc, base, size)) {
644 		struct tee_pager_area *next_a;
645 
646 		/* Remove all added areas */
647 		TAILQ_FOREACH_SAFE(area, utc->areas, link, next_a) {
648 			if (!area->pgt) {
649 				TAILQ_REMOVE(utc->areas, area, link);
650 				free_area(area);
651 			}
652 		}
653 		return false;
654 	}
655 
656 	/*
657 	 * Assign page tables to the new areas and make sure that the page
658 	 * tables are registered in the upper table.
659 	 */
660 	tee_pager_assign_uta_tables(utc);
661 	core_mmu_get_user_pgdir(&dir_info);
662 	TAILQ_FOREACH(area, utc->areas, link) {
663 		paddr_t pa;
664 		size_t idx;
665 		uint32_t attr;
666 
667 		idx = core_mmu_va2idx(&dir_info, area->pgt->vabase);
668 		core_mmu_get_entry(&dir_info, idx, &pa, &attr);
669 
670 		/*
671 		 * Check if the page table already is used, if it is, it's
672 		 * already registered.
673 		 */
674 		if (area->pgt->num_used_entries) {
675 			assert(attr & TEE_MATTR_TABLE);
676 			assert(pa == virt_to_phys(area->pgt->tbl));
677 			continue;
678 		}
679 
680 		attr = TEE_MATTR_SECURE | TEE_MATTR_TABLE;
681 		pa = virt_to_phys(area->pgt->tbl);
682 		assert(pa);
683 		/*
684 		 * Note that the update of the table entry is guaranteed to
685 		 * be atomic.
686 		 */
687 		core_mmu_set_entry(&dir_info, idx, pa, attr);
688 	}
689 
690 	return true;
691 }
692 
693 static void init_tbl_info_from_pgt(struct core_mmu_table_info *ti,
694 				   struct pgt *pgt)
695 {
696 	assert(pgt);
697 	ti->table = pgt->tbl;
698 	ti->va_base = pgt->vabase;
699 	ti->level = tee_pager_tbl_info.level;
700 	ti->shift = tee_pager_tbl_info.shift;
701 	ti->num_entries = tee_pager_tbl_info.num_entries;
702 }
703 
704 static void transpose_area(struct tee_pager_area *area, struct pgt *new_pgt,
705 			   vaddr_t new_base)
706 {
707 	uint32_t exceptions = pager_lock();
708 
709 	/*
710 	 * If there's no pgt assigned to the old area there's no pages to
711 	 * deal with either, just update with a new pgt and base.
712 	 */
713 	if (area->pgt) {
714 		struct core_mmu_table_info old_ti;
715 		struct core_mmu_table_info new_ti;
716 		struct tee_pager_pmem *pmem;
717 
718 		init_tbl_info_from_pgt(&old_ti, area->pgt);
719 		init_tbl_info_from_pgt(&new_ti, new_pgt);
720 
721 
722 		TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
723 			vaddr_t va;
724 			paddr_t pa;
725 			uint32_t attr;
726 
727 			if (pmem->area != area)
728 				continue;
729 			core_mmu_get_entry(&old_ti, pmem->pgidx, &pa, &attr);
730 			core_mmu_set_entry(&old_ti, pmem->pgidx, 0, 0);
731 
732 			assert(pa == get_pmem_pa(pmem));
733 			assert(attr);
734 			assert(area->pgt->num_used_entries);
735 			area->pgt->num_used_entries--;
736 
737 			va = core_mmu_idx2va(&old_ti, pmem->pgidx);
738 			va = va - area->base + new_base;
739 			pmem->pgidx = core_mmu_va2idx(&new_ti, va);
740 			core_mmu_set_entry(&new_ti, pmem->pgidx, pa, attr);
741 			new_pgt->num_used_entries++;
742 		}
743 	}
744 
745 	area->pgt = new_pgt;
746 	area->base = new_base;
747 	pager_unlock(exceptions);
748 }
749 KEEP_PAGER(transpose_area);
750 
751 void tee_pager_transfer_uta_region(struct user_ta_ctx *src_utc,
752 				   vaddr_t src_base,
753 				   struct user_ta_ctx *dst_utc,
754 				   vaddr_t dst_base, struct pgt **dst_pgt,
755 				   size_t size)
756 {
757 	struct tee_pager_area *area;
758 	struct tee_pager_area *next_a;
759 
760 	TAILQ_FOREACH_SAFE(area, src_utc->areas, link, next_a) {
761 		vaddr_t new_area_base;
762 		size_t new_idx;
763 
764 		if (!core_is_buffer_inside(area->base, area->size,
765 					  src_base, size))
766 			continue;
767 
768 		TAILQ_REMOVE(src_utc->areas, area, link);
769 
770 		new_area_base = dst_base + (src_base - area->base);
771 		new_idx = (new_area_base - dst_pgt[0]->vabase) /
772 			  CORE_MMU_PGDIR_SIZE;
773 		assert((new_area_base & ~CORE_MMU_PGDIR_MASK) ==
774 		       dst_pgt[new_idx]->vabase);
775 		transpose_area(area, dst_pgt[new_idx], new_area_base);
776 
777 		/*
778 		 * Assert that this will not cause any conflicts in the new
779 		 * utc.  This should already be guaranteed, but a bug here
780 		 * could be tricky to find.
781 		 */
782 		assert(!find_area(dst_utc->areas, area->base));
783 		TAILQ_INSERT_TAIL(dst_utc->areas, area, link);
784 	}
785 }
786 
787 void tee_pager_rem_uta_region(struct user_ta_ctx *utc, vaddr_t base,
788 			      size_t size)
789 {
790 	struct tee_pager_area *area;
791 	struct tee_pager_area *next_a;
792 	size_t s = ROUNDUP(size, SMALL_PAGE_SIZE);
793 
794 	TAILQ_FOREACH_SAFE(area, utc->areas, link, next_a) {
795 		if (core_is_buffer_inside(area->base, area->size, base, s)) {
796 			TAILQ_REMOVE(utc->areas, area, link);
797 			free_area(area);
798 		}
799 	}
800 
801 }
802 
803 void tee_pager_rem_uta_areas(struct user_ta_ctx *utc)
804 {
805 	struct tee_pager_area *area;
806 
807 	if (!utc->areas)
808 		return;
809 
810 	while (true) {
811 		area = TAILQ_FIRST(utc->areas);
812 		if (!area)
813 			break;
814 		TAILQ_REMOVE(utc->areas, area, link);
815 		free_area(area);
816 	}
817 
818 	free(utc->areas);
819 }
820 
821 bool tee_pager_set_uta_area_attr(struct user_ta_ctx *utc, vaddr_t base,
822 				 size_t size, uint32_t flags)
823 {
824 	bool ret;
825 	vaddr_t b = base;
826 	size_t s = size;
827 	size_t s2;
828 	struct tee_pager_area *area = find_area(utc->areas, b);
829 	uint32_t exceptions;
830 	struct tee_pager_pmem *pmem;
831 	paddr_t pa;
832 	uint32_t a;
833 	uint32_t f;
834 
835 	f = (flags & TEE_MATTR_URWX) | TEE_MATTR_UR | TEE_MATTR_PR;
836 	if (f & TEE_MATTR_UW)
837 		f |= TEE_MATTR_PW;
838 	f = get_area_mattr(f);
839 
840 	exceptions = pager_lock();
841 
842 	while (s) {
843 		s2 = MIN(CORE_MMU_PGDIR_SIZE - (b & CORE_MMU_PGDIR_MASK), s);
844 		if (!area || area->base != b || area->size != s2) {
845 			ret = false;
846 			goto out;
847 		}
848 		b += s2;
849 		s -= s2;
850 
851 		TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
852 			if (pmem->area != area)
853 				continue;
854 			area_get_entry(pmem->area, pmem->pgidx, &pa, &a);
855 			if (a & TEE_MATTR_VALID_BLOCK)
856 				assert(pa == get_pmem_pa(pmem));
857 			else
858 				pa = get_pmem_pa(pmem);
859 			if (a == f)
860 				continue;
861 			area_set_entry(pmem->area, pmem->pgidx, 0, 0);
862 			/* TODO only invalidate entries touched above */
863 			core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
864 			if (!(flags & TEE_MATTR_UW))
865 				tee_pager_save_page(pmem, a);
866 
867 			area_set_entry(pmem->area, pmem->pgidx, pa, f);
868 
869 			if (flags & TEE_MATTR_UX) {
870 				void *va = (void *)area_idx2va(pmem->area,
871 							       pmem->pgidx);
872 
873 				cache_maintenance_l1(DCACHE_AREA_CLEAN, va,
874 						     SMALL_PAGE_SIZE);
875 				cache_maintenance_l1(ICACHE_AREA_INVALIDATE, va,
876 						     SMALL_PAGE_SIZE);
877 			}
878 		}
879 
880 		area->flags = f;
881 		area = TAILQ_NEXT(area, link);
882 	}
883 
884 	ret = true;
885 out:
886 	pager_unlock(exceptions);
887 	return ret;
888 }
889 KEEP_PAGER(tee_pager_set_uta_area_attr);
890 #endif /*CFG_PAGED_USER_TA*/
891 
892 static bool tee_pager_unhide_page(vaddr_t page_va)
893 {
894 	struct tee_pager_pmem *pmem;
895 
896 	TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
897 		paddr_t pa;
898 		uint32_t attr;
899 
900 		if (pmem->pgidx == INVALID_PGIDX)
901 			continue;
902 
903 		area_get_entry(pmem->area, pmem->pgidx, &pa, &attr);
904 
905 		if (!(attr &
906 		     (TEE_MATTR_HIDDEN_BLOCK | TEE_MATTR_HIDDEN_DIRTY_BLOCK)))
907 			continue;
908 
909 		if (area_va2idx(pmem->area, page_va) == pmem->pgidx) {
910 			uint32_t a = get_area_mattr(pmem->area->flags);
911 
912 			/* page is hidden, show and move to back */
913 			if (pa != get_pmem_pa(pmem))
914 				panic("unexpected pa");
915 
916 			/*
917 			 * If it's not a dirty block, then it should be
918 			 * read only.
919 			 */
920 			if (!(attr & TEE_MATTR_HIDDEN_DIRTY_BLOCK))
921 				a &= ~(TEE_MATTR_PW | TEE_MATTR_UW);
922 			else
923 				FMSG("Unhide %#" PRIxVA, page_va);
924 
925 			if (page_va == 0x8000a000)
926 				FMSG("unhide %#" PRIxVA " a %#" PRIX32,
927 					page_va, a);
928 			area_set_entry(pmem->area, pmem->pgidx, pa, a);
929 
930 			TAILQ_REMOVE(&tee_pager_pmem_head, pmem, link);
931 			TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link);
932 
933 			/* TODO only invalidate entry touched above */
934 			core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
935 
936 			incr_hidden_hits();
937 			return true;
938 		}
939 	}
940 
941 	return false;
942 }
943 
944 static void tee_pager_hide_pages(void)
945 {
946 	struct tee_pager_pmem *pmem;
947 	size_t n = 0;
948 
949 	TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
950 		paddr_t pa;
951 		uint32_t attr;
952 		uint32_t a;
953 
954 		if (n >= TEE_PAGER_NHIDE)
955 			break;
956 		n++;
957 
958 		/* we cannot hide pages when pmem->area is not defined. */
959 		if (!pmem->area)
960 			continue;
961 
962 		area_get_entry(pmem->area, pmem->pgidx, &pa, &attr);
963 		if (!(attr & TEE_MATTR_VALID_BLOCK))
964 			continue;
965 
966 		assert(pa == get_pmem_pa(pmem));
967 		if (attr & (TEE_MATTR_PW | TEE_MATTR_UW)){
968 			a = TEE_MATTR_HIDDEN_DIRTY_BLOCK;
969 			FMSG("Hide %#" PRIxVA,
970 			     area_idx2va(pmem->area, pmem->pgidx));
971 		} else
972 			a = TEE_MATTR_HIDDEN_BLOCK;
973 		area_set_entry(pmem->area, pmem->pgidx, pa, a);
974 	}
975 
976 	/* TODO only invalidate entries touched above */
977 	core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
978 }
979 
980 /*
981  * Find mapped pmem, hide and move to pageble pmem.
982  * Return false if page was not mapped, and true if page was mapped.
983  */
984 static bool tee_pager_release_one_phys(struct tee_pager_area *area,
985 				       vaddr_t page_va)
986 {
987 	struct tee_pager_pmem *pmem;
988 	unsigned pgidx;
989 	paddr_t pa;
990 	uint32_t attr;
991 
992 	pgidx = area_va2idx(area, page_va);
993 	area_get_entry(area, pgidx, &pa, &attr);
994 
995 	FMSG("%" PRIxVA " : %" PRIxPA "|%x", page_va, pa, attr);
996 
997 	TAILQ_FOREACH(pmem, &tee_pager_lock_pmem_head, link) {
998 		if (pmem->area != area || pmem->pgidx != pgidx)
999 			continue;
1000 
1001 		assert(pa == get_pmem_pa(pmem));
1002 		area_set_entry(area, pgidx, 0, 0);
1003 		pgt_dec_used_entries(area->pgt);
1004 		TAILQ_REMOVE(&tee_pager_lock_pmem_head, pmem, link);
1005 		pmem->area = NULL;
1006 		pmem->pgidx = INVALID_PGIDX;
1007 		tee_pager_npages++;
1008 		set_npages();
1009 		TAILQ_INSERT_HEAD(&tee_pager_pmem_head, pmem, link);
1010 		incr_zi_released();
1011 		return true;
1012 	}
1013 
1014 	return false;
1015 }
1016 
1017 /* Finds the oldest page and unmats it from its old virtual address */
1018 static struct tee_pager_pmem *tee_pager_get_page(struct tee_pager_area *area)
1019 {
1020 	struct tee_pager_pmem *pmem;
1021 
1022 	pmem = TAILQ_FIRST(&tee_pager_pmem_head);
1023 	if (!pmem) {
1024 		EMSG("No pmem entries");
1025 		return NULL;
1026 	}
1027 	if (pmem->pgidx != INVALID_PGIDX) {
1028 		uint32_t a;
1029 
1030 		assert(pmem->area && pmem->area->pgt);
1031 		area_get_entry(pmem->area, pmem->pgidx, NULL, &a);
1032 		area_set_entry(pmem->area, pmem->pgidx, 0, 0);
1033 		pgt_dec_used_entries(pmem->area->pgt);
1034 		/* TODO only invalidate entries touched above */
1035 		core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
1036 		tee_pager_save_page(pmem, a);
1037 	}
1038 
1039 	TAILQ_REMOVE(&tee_pager_pmem_head, pmem, link);
1040 	pmem->pgidx = INVALID_PGIDX;
1041 	pmem->area = NULL;
1042 	if (area->type == AREA_TYPE_LOCK) {
1043 		/* Move page to lock list */
1044 		if (tee_pager_npages <= 0)
1045 			panic("running out of page");
1046 		tee_pager_npages--;
1047 		set_npages();
1048 		TAILQ_INSERT_TAIL(&tee_pager_lock_pmem_head, pmem, link);
1049 	} else {
1050 		/* move page to back */
1051 		TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link);
1052 	}
1053 
1054 	return pmem;
1055 }
1056 
1057 static bool pager_update_permissions(struct tee_pager_area *area,
1058 			struct abort_info *ai, bool *handled)
1059 {
1060 	unsigned int pgidx = area_va2idx(area, ai->va);
1061 	uint32_t attr;
1062 	paddr_t pa;
1063 
1064 	*handled = false;
1065 
1066 	area_get_entry(area, pgidx, &pa, &attr);
1067 
1068 	/* Not mapped */
1069 	if (!(attr & TEE_MATTR_VALID_BLOCK))
1070 		return false;
1071 
1072 	/* Not readable, should not happen */
1073 	if (abort_is_user_exception(ai)) {
1074 		if (!(attr & TEE_MATTR_UR))
1075 			return true;
1076 	} else {
1077 		if (!(attr & TEE_MATTR_PR)) {
1078 			abort_print_error(ai);
1079 			panic();
1080 		}
1081 	}
1082 
1083 	switch (core_mmu_get_fault_type(ai->fault_descr)) {
1084 	case CORE_MMU_FAULT_TRANSLATION:
1085 	case CORE_MMU_FAULT_READ_PERMISSION:
1086 		if (ai->abort_type == ABORT_TYPE_PREFETCH) {
1087 			/* Check attempting to execute from an NOX page */
1088 			if (abort_is_user_exception(ai)) {
1089 				if (!(attr & TEE_MATTR_UX))
1090 					return true;
1091 			} else {
1092 				if (!(attr & TEE_MATTR_PX)) {
1093 					abort_print_error(ai);
1094 					panic();
1095 				}
1096 			}
1097 		}
1098 		/* Since the page is mapped now it's OK */
1099 		break;
1100 	case CORE_MMU_FAULT_WRITE_PERMISSION:
1101 		/* Check attempting to write to an RO page */
1102 		if (abort_is_user_exception(ai)) {
1103 			if (!(area->flags & TEE_MATTR_UW))
1104 				return true;
1105 			if (!(attr & TEE_MATTR_UW)) {
1106 				FMSG("Dirty %p",
1107 				     (void *)(ai->va & ~SMALL_PAGE_MASK));
1108 				area_set_entry(area, pgidx, pa,
1109 					       get_area_mattr(area->flags));
1110 				/* TODO only invalidate entry above */
1111 				core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
1112 			}
1113 
1114 		} else {
1115 			if (!(area->flags & TEE_MATTR_PW)) {
1116 				abort_print_error(ai);
1117 				panic();
1118 			}
1119 			if (!(attr & TEE_MATTR_PW)) {
1120 				FMSG("Dirty %p",
1121 				     (void *)(ai->va & ~SMALL_PAGE_MASK));
1122 				area_set_entry(area, pgidx, pa,
1123 					       get_area_mattr(area->flags));
1124 				/* TODO only invalidate entry above */
1125 				core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
1126 			}
1127 		}
1128 		/* Since permissions has been updated now it's OK */
1129 		break;
1130 	default:
1131 		/* Some fault we can't deal with */
1132 		if (abort_is_user_exception(ai))
1133 			return true;
1134 		abort_print_error(ai);
1135 		panic();
1136 	}
1137 	*handled = true;
1138 	return true;
1139 }
1140 
1141 #ifdef CFG_TEE_CORE_DEBUG
1142 static void stat_handle_fault(void)
1143 {
1144 	static size_t num_faults;
1145 	static size_t min_npages = SIZE_MAX;
1146 	static size_t total_min_npages = SIZE_MAX;
1147 
1148 	num_faults++;
1149 	if ((num_faults % 1024) == 0 || tee_pager_npages < total_min_npages) {
1150 		DMSG("nfaults %zu npages %zu (min %zu)",
1151 		     num_faults, tee_pager_npages, min_npages);
1152 		min_npages = tee_pager_npages; /* reset */
1153 	}
1154 	if (tee_pager_npages < min_npages)
1155 		min_npages = tee_pager_npages;
1156 	if (tee_pager_npages < total_min_npages)
1157 		total_min_npages = tee_pager_npages;
1158 }
1159 #else
1160 static void stat_handle_fault(void)
1161 {
1162 }
1163 #endif
1164 
1165 bool tee_pager_handle_fault(struct abort_info *ai)
1166 {
1167 	struct tee_pager_area *area;
1168 	vaddr_t page_va = ai->va & ~SMALL_PAGE_MASK;
1169 	uint32_t exceptions;
1170 	bool ret;
1171 
1172 #ifdef TEE_PAGER_DEBUG_PRINT
1173 	abort_print(ai);
1174 #endif
1175 
1176 	/*
1177 	 * We're updating pages that can affect several active CPUs at a
1178 	 * time below. We end up here because a thread tries to access some
1179 	 * memory that isn't available. We have to be careful when making
1180 	 * that memory available as other threads may succeed in accessing
1181 	 * that address the moment after we've made it available.
1182 	 *
1183 	 * That means that we can't just map the memory and populate the
1184 	 * page, instead we use the aliased mapping to populate the page
1185 	 * and once everything is ready we map it.
1186 	 */
1187 	exceptions = pager_lock();
1188 
1189 	stat_handle_fault();
1190 
1191 	/* check if the access is valid */
1192 	if (abort_is_user_exception(ai)) {
1193 		area = find_uta_area(ai->va);
1194 
1195 	} else {
1196 		area = find_area(&tee_pager_area_head, ai->va);
1197 		if (!area)
1198 			area = find_uta_area(ai->va);
1199 	}
1200 	if (!area || !area->pgt) {
1201 		ret = false;
1202 		goto out;
1203 	}
1204 
1205 	if (!tee_pager_unhide_page(page_va)) {
1206 		struct tee_pager_pmem *pmem = NULL;
1207 		uint32_t attr;
1208 
1209 		/*
1210 		 * The page wasn't hidden, but some other core may have
1211 		 * updated the table entry before we got here or we need
1212 		 * to make a read-only page read-write (dirty).
1213 		 */
1214 		if (pager_update_permissions(area, ai, &ret)) {
1215 			/*
1216 			 * Nothing more to do with the abort. The problem
1217 			 * could already have been dealt with from another
1218 			 * core or if ret is false the TA will be paniced.
1219 			 */
1220 			goto out;
1221 		}
1222 
1223 		pmem = tee_pager_get_page(area);
1224 		if (!pmem) {
1225 			abort_print(ai);
1226 			panic();
1227 		}
1228 
1229 		/* load page code & data */
1230 		tee_pager_load_page(area, page_va, pmem->va_alias);
1231 
1232 		/*
1233 		 * We've updated the page using the aliased mapping and
1234 		 * some cache maintenence is now needed if it's an
1235 		 * executable page.
1236 		 *
1237 		 * Since the d-cache is a Physically-indexed,
1238 		 * physically-tagged (PIPT) cache we can clean the aliased
1239 		 * address instead of the real virtual address.
1240 		 *
1241 		 * The i-cache can also be PIPT, but may be something else
1242 		 * to, to keep it simple we invalidate the entire i-cache.
1243 		 * As a future optimization we may invalidate only the
1244 		 * aliased area if it a PIPT cache else the entire cache.
1245 		 */
1246 		if (area->flags & (TEE_MATTR_PX | TEE_MATTR_UX)) {
1247 			/*
1248 			 * Doing these operations to LoUIS (Level of
1249 			 * unification, Inner Shareable) would be enough
1250 			 */
1251 			cache_maintenance_l1(DCACHE_AREA_CLEAN,
1252 				pmem->va_alias, SMALL_PAGE_SIZE);
1253 
1254 			cache_maintenance_l1(ICACHE_INVALIDATE, NULL, 0);
1255 		}
1256 
1257 		pmem->area = area;
1258 		pmem->pgidx = area_va2idx(area, ai->va);
1259 		attr = get_area_mattr(area->flags) &
1260 			~(TEE_MATTR_PW | TEE_MATTR_UW);
1261 		area_set_entry(area, pmem->pgidx, get_pmem_pa(pmem), attr);
1262 		pgt_inc_used_entries(area->pgt);
1263 
1264 		FMSG("Mapped 0x%" PRIxVA " -> 0x%" PRIxPA,
1265 		     area_idx2va(area, pmem->pgidx), get_pmem_pa(pmem));
1266 
1267 	}
1268 
1269 	tee_pager_hide_pages();
1270 	ret = true;
1271 out:
1272 	pager_unlock(exceptions);
1273 	return ret;
1274 }
1275 
1276 void tee_pager_add_pages(vaddr_t vaddr, size_t npages, bool unmap)
1277 {
1278 	struct core_mmu_table_info *ti = &tee_pager_tbl_info;
1279 	size_t n;
1280 
1281 	DMSG("0x%" PRIxVA " - 0x%" PRIxVA " : %d",
1282 	     vaddr, vaddr + npages * SMALL_PAGE_SIZE, (int)unmap);
1283 
1284 	/* setup memory */
1285 	for (n = 0; n < npages; n++) {
1286 		struct tee_pager_pmem *pmem;
1287 		vaddr_t va = vaddr + n * SMALL_PAGE_SIZE;
1288 		unsigned pgidx = core_mmu_va2idx(ti, va);
1289 		paddr_t pa;
1290 		uint32_t attr;
1291 
1292 		/*
1293 		 * Note that we can only support adding pages in the
1294 		 * valid range of this table info, currently not a problem.
1295 		 */
1296 		core_mmu_get_entry(ti, pgidx, &pa, &attr);
1297 
1298 		/* Ignore unmapped pages/blocks */
1299 		if (!(attr & TEE_MATTR_VALID_BLOCK))
1300 			continue;
1301 
1302 		pmem = malloc(sizeof(struct tee_pager_pmem));
1303 		if (!pmem)
1304 			panic("out of mem");
1305 
1306 		pmem->va_alias = pager_add_alias_page(pa);
1307 
1308 		if (unmap) {
1309 			pmem->area = NULL;
1310 			pmem->pgidx = INVALID_PGIDX;
1311 			core_mmu_set_entry(ti, pgidx, 0, 0);
1312 			pgt_dec_used_entries(&pager_core_pgt);
1313 		} else {
1314 			/*
1315 			 * The page is still mapped, let's assign the area
1316 			 * and update the protection bits accordingly.
1317 			 */
1318 			pmem->area = find_area(&tee_pager_area_head, va);
1319 			assert(pmem->area->pgt == &pager_core_pgt);
1320 			pmem->pgidx = pgidx;
1321 			assert(pa == get_pmem_pa(pmem));
1322 			area_set_entry(pmem->area, pgidx, pa,
1323 				       get_area_mattr(pmem->area->flags));
1324 		}
1325 
1326 		tee_pager_npages++;
1327 		incr_npages_all();
1328 		set_npages();
1329 		TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link);
1330 	}
1331 
1332 	/* Invalidate secure TLB */
1333 	core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
1334 }
1335 
1336 #ifdef CFG_PAGED_USER_TA
1337 static struct pgt *find_pgt(struct pgt *pgt, vaddr_t va)
1338 {
1339 	struct pgt *p = pgt;
1340 
1341 	while (p && (va & ~CORE_MMU_PGDIR_MASK) != p->vabase)
1342 		p = SLIST_NEXT(p, link);
1343 	return p;
1344 }
1345 
1346 void tee_pager_assign_uta_tables(struct user_ta_ctx *utc)
1347 {
1348 	struct tee_pager_area *area;
1349 	struct pgt *pgt = SLIST_FIRST(&thread_get_tsd()->pgt_cache);
1350 
1351 	TAILQ_FOREACH(area, utc->areas, link) {
1352 		if (!area->pgt)
1353 			area->pgt = find_pgt(pgt, area->base);
1354 		else
1355 			assert(area->pgt == find_pgt(pgt, area->base));
1356 		if (!area->pgt)
1357 			panic();
1358 	}
1359 }
1360 
1361 static void pager_save_and_release_entry(struct tee_pager_pmem *pmem)
1362 {
1363 	uint32_t attr;
1364 
1365 	assert(pmem->area && pmem->area->pgt);
1366 
1367 	area_get_entry(pmem->area, pmem->pgidx, NULL, &attr);
1368 	area_set_entry(pmem->area, pmem->pgidx, 0, 0);
1369 	tee_pager_save_page(pmem, attr);
1370 	assert(pmem->area->pgt->num_used_entries);
1371 	pmem->area->pgt->num_used_entries--;
1372 	pmem->pgidx = INVALID_PGIDX;
1373 	pmem->area = NULL;
1374 }
1375 
1376 void tee_pager_pgt_save_and_release_entries(struct pgt *pgt)
1377 {
1378 	struct tee_pager_pmem *pmem;
1379 	struct tee_pager_area *area;
1380 	uint32_t exceptions = pager_lock();
1381 
1382 	if (!pgt->num_used_entries)
1383 		goto out;
1384 
1385 	TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
1386 		if (!pmem->area || pmem->pgidx == INVALID_PGIDX)
1387 			continue;
1388 		if (pmem->area->pgt == pgt)
1389 			pager_save_and_release_entry(pmem);
1390 	}
1391 	assert(!pgt->num_used_entries);
1392 
1393 out:
1394 	if (is_user_ta_ctx(pgt->ctx)) {
1395 		TAILQ_FOREACH(area, to_user_ta_ctx(pgt->ctx)->areas, link) {
1396 			if (area->pgt == pgt)
1397 				area->pgt = NULL;
1398 		}
1399 	}
1400 
1401 	pager_unlock(exceptions);
1402 }
1403 KEEP_PAGER(tee_pager_pgt_save_and_release_entries);
1404 #endif /*CFG_PAGED_USER_TA*/
1405 
1406 void tee_pager_release_phys(void *addr, size_t size)
1407 {
1408 	bool unmaped = false;
1409 	vaddr_t va = (vaddr_t)addr;
1410 	vaddr_t begin = ROUNDUP(va, SMALL_PAGE_SIZE);
1411 	vaddr_t end = ROUNDDOWN(va + size, SMALL_PAGE_SIZE);
1412 	struct tee_pager_area *area;
1413 	uint32_t exceptions;
1414 
1415 	if (!size)
1416 		return;
1417 
1418 	area = find_area(&tee_pager_area_head, begin);
1419 	if (!area ||
1420 	    area != find_area(&tee_pager_area_head, end - SMALL_PAGE_SIZE))
1421 		panic();
1422 
1423 	exceptions = pager_lock();
1424 
1425 	for (va = begin; va < end; va += SMALL_PAGE_SIZE)
1426 		unmaped |= tee_pager_release_one_phys(area, va);
1427 
1428 	/* Invalidate secure TLB */
1429 	if (unmaped)
1430 		core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
1431 
1432 	pager_unlock(exceptions);
1433 }
1434 KEEP_PAGER(tee_pager_release_phys);
1435 
1436 void *tee_pager_alloc(size_t size, uint32_t flags)
1437 {
1438 	tee_mm_entry_t *mm;
1439 	uint32_t f = TEE_MATTR_PW | TEE_MATTR_PR | (flags & TEE_MATTR_LOCKED);
1440 
1441 	if (!size)
1442 		return NULL;
1443 
1444 	mm = tee_mm_alloc(&tee_mm_vcore, ROUNDUP(size, SMALL_PAGE_SIZE));
1445 	if (!mm)
1446 		return NULL;
1447 
1448 	tee_pager_add_core_area(tee_mm_get_smem(mm), tee_mm_get_bytes(mm),
1449 				f, NULL, NULL);
1450 
1451 	return (void *)tee_mm_get_smem(mm);
1452 }
1453