xref: /optee_os/core/arch/arm/mm/tee_pager.c (revision 036560f774234e21528b2d1af071b9d25111fce3)
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 <sys/queue.h>
30 #include <kernel/abort.h>
31 #include <kernel/panic.h>
32 #include <kernel/tee_misc.h>
33 #include <kernel/tee_ta_manager.h>
34 #include <kernel/thread.h>
35 #include <kernel/tz_proc.h>
36 #include <mm/core_memprot.h>
37 #include <mm/tee_mm.h>
38 #include <mm/tee_mmu_defs.h>
39 #include <mm/tee_pager.h>
40 #include <types_ext.h>
41 #include <stdlib.h>
42 #include <tee_api_defines.h>
43 #include <tee/tee_cryp_provider.h>
44 #include <trace.h>
45 #include <utee_defines.h>
46 #include <util.h>
47 #include <keep.h>
48 #include "pager_private.h"
49 
50 #define PAGER_AE_KEY_BITS	256
51 
52 struct pager_rw_pstate {
53 	uint64_t iv;
54 	uint8_t tag[PAGER_AES_GCM_TAG_LEN];
55 };
56 
57 struct tee_pager_area {
58 	union {
59 		const uint8_t *hashes;
60 		struct pager_rw_pstate *rwp;
61 	} u;
62 	uint8_t *store;
63 	uint32_t flags;
64 	vaddr_t base;
65 	size_t size;
66 	TAILQ_ENTRY(tee_pager_area) link;
67 };
68 
69 static TAILQ_HEAD(tee_pager_area_head, tee_pager_area) tee_pager_area_head =
70 	TAILQ_HEAD_INITIALIZER(tee_pager_area_head);
71 
72 #define INVALID_PGIDX	UINT_MAX
73 
74 /*
75  * struct tee_pager_pmem - Represents a physical page used for paging.
76  *
77  * @pgidx	an index of the entry in tee_pager_tbl_info.
78  * @va_alias	Virtual address where the physical page always is aliased.
79  *		Used during remapping of the page when the content need to
80  *		be updated before it's available at the new location.
81  * @area	a pointer to the pager area
82  */
83 struct tee_pager_pmem {
84 	unsigned pgidx;
85 	void *va_alias;
86 	struct tee_pager_area *area;
87 	TAILQ_ENTRY(tee_pager_pmem) link;
88 };
89 
90 /* The list of physical pages. The first page in the list is the oldest */
91 TAILQ_HEAD(tee_pager_pmem_head, tee_pager_pmem);
92 
93 static struct tee_pager_pmem_head tee_pager_pmem_head =
94 	TAILQ_HEAD_INITIALIZER(tee_pager_pmem_head);
95 
96 static struct tee_pager_pmem_head tee_pager_lock_pmem_head =
97 	TAILQ_HEAD_INITIALIZER(tee_pager_lock_pmem_head);
98 
99 static uint8_t pager_ae_key[PAGER_AE_KEY_BITS / 8];
100 
101 /* number of pages hidden */
102 #define TEE_PAGER_NHIDE (tee_pager_npages / 3)
103 
104 /* Number of registered physical pages, used hiding pages. */
105 static size_t tee_pager_npages;
106 
107 #ifdef CFG_WITH_STATS
108 static struct tee_pager_stats pager_stats;
109 
110 static inline void incr_ro_hits(void)
111 {
112 	pager_stats.ro_hits++;
113 }
114 
115 static inline void incr_rw_hits(void)
116 {
117 	pager_stats.rw_hits++;
118 }
119 
120 static inline void incr_hidden_hits(void)
121 {
122 	pager_stats.hidden_hits++;
123 }
124 
125 static inline void incr_zi_released(void)
126 {
127 	pager_stats.zi_released++;
128 }
129 
130 static inline void incr_npages_all(void)
131 {
132 	pager_stats.npages_all++;
133 }
134 
135 static inline void set_npages(void)
136 {
137 	pager_stats.npages = tee_pager_npages;
138 }
139 
140 void tee_pager_get_stats(struct tee_pager_stats *stats)
141 {
142 	*stats = pager_stats;
143 
144 	pager_stats.hidden_hits = 0;
145 	pager_stats.ro_hits = 0;
146 	pager_stats.rw_hits = 0;
147 	pager_stats.zi_released = 0;
148 }
149 
150 #else /* CFG_WITH_STATS */
151 static inline void incr_ro_hits(void) { }
152 static inline void incr_rw_hits(void) { }
153 static inline void incr_hidden_hits(void) { }
154 static inline void incr_zi_released(void) { }
155 static inline void incr_npages_all(void) { }
156 static inline void set_npages(void) { }
157 
158 void tee_pager_get_stats(struct tee_pager_stats *stats)
159 {
160 	memset(stats, 0, sizeof(struct tee_pager_stats));
161 }
162 #endif /* CFG_WITH_STATS */
163 
164 struct core_mmu_table_info tee_pager_tbl_info;
165 static struct core_mmu_table_info pager_alias_tbl_info;
166 
167 static unsigned pager_lock = SPINLOCK_UNLOCK;
168 
169 /* Defines the range of the alias area */
170 static tee_mm_entry_t *pager_alias_area;
171 /*
172  * Physical pages are added in a stack like fashion to the alias area,
173  * @pager_alias_next_free gives the address of next free entry if
174  * @pager_alias_next_free is != 0
175  */
176 static uintptr_t pager_alias_next_free;
177 
178 static void set_alias_area(tee_mm_entry_t *mm)
179 {
180 	struct core_mmu_table_info *ti = &pager_alias_tbl_info;
181 	size_t tbl_va_size;
182 	unsigned idx;
183 	unsigned last_idx;
184 	vaddr_t smem = tee_mm_get_smem(mm);
185 	size_t nbytes = tee_mm_get_bytes(mm);
186 
187 	DMSG("0x%" PRIxVA " - 0x%" PRIxVA, smem, smem + nbytes);
188 
189 	TEE_ASSERT(!pager_alias_area);
190 	if (!ti->num_entries && !core_mmu_find_table(smem, UINT_MAX, ti)) {
191 		DMSG("Can't find translation table");
192 		panic();
193 	}
194 	if ((1 << ti->shift) != SMALL_PAGE_SIZE) {
195 		DMSG("Unsupported page size in translation table %u",
196 		     1 << ti->shift);
197 		panic();
198 	}
199 
200 	tbl_va_size = (1 << ti->shift) * ti->num_entries;
201 	if (!core_is_buffer_inside(smem, nbytes,
202 				   ti->va_base, tbl_va_size)) {
203 		DMSG("area 0x%" PRIxVA " len 0x%zx doesn't fit it translation table 0x%" PRIxVA " len 0x%zx",
204 			smem, nbytes, ti->va_base, tbl_va_size);
205 		panic();
206 	}
207 
208 	TEE_ASSERT(!(smem & SMALL_PAGE_MASK));
209 	TEE_ASSERT(!(nbytes & SMALL_PAGE_MASK));
210 
211 	pager_alias_area = mm;
212 	pager_alias_next_free = smem;
213 
214 	/* Clear all mapping in the alias area */
215 	idx = core_mmu_va2idx(ti, smem);
216 	last_idx = core_mmu_va2idx(ti, smem + nbytes);
217 	for (; idx < last_idx; idx++)
218 		core_mmu_set_entry(ti, idx, 0, 0);
219 
220 	/* TODO only invalidate entries touched above */
221 	core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
222 }
223 
224 static void generate_ae_key(void)
225 {
226 	TEE_Result res;
227 
228 	res = rng_generate(pager_ae_key, sizeof(pager_ae_key));
229 	TEE_ASSERT(res == TEE_SUCCESS);
230 }
231 
232 void tee_pager_init(tee_mm_entry_t *mm_alias)
233 {
234 	set_alias_area(mm_alias);
235 	generate_ae_key();
236 }
237 
238 static void *pager_add_alias_page(paddr_t pa)
239 {
240 	unsigned idx;
241 	struct core_mmu_table_info *ti = &pager_alias_tbl_info;
242 	uint32_t attr = TEE_MATTR_VALID_BLOCK | TEE_MATTR_GLOBAL |
243 			(TEE_MATTR_CACHE_CACHED << TEE_MATTR_CACHE_SHIFT) |
244 			TEE_MATTR_SECURE | TEE_MATTR_PRW;
245 
246 	DMSG("0x%" PRIxPA, pa);
247 
248 	TEE_ASSERT(pager_alias_next_free && ti->num_entries);
249 	idx = core_mmu_va2idx(ti, pager_alias_next_free);
250 	core_mmu_set_entry(ti, idx, pa, attr);
251 	pager_alias_next_free += SMALL_PAGE_SIZE;
252 	if (pager_alias_next_free >= (tee_mm_get_smem(pager_alias_area) +
253 				      tee_mm_get_bytes(pager_alias_area)))
254 		pager_alias_next_free = 0;
255 	return (void *)core_mmu_idx2va(ti, idx);
256 }
257 
258 static struct tee_pager_area *alloc_area(vaddr_t base, size_t size,
259 			uint32_t flags, const void *store, const void *hashes)
260 {
261 	struct tee_pager_area *area = calloc(1, sizeof(*area));
262 	tee_mm_entry_t *mm_store = NULL;
263 
264 	if (!area)
265 		return NULL;
266 
267 	if (flags & TEE_MATTR_PW) {
268 		if (flags & TEE_MATTR_LOCKED)
269 			goto out;
270 		mm_store = tee_mm_alloc(&tee_mm_sec_ddr, size);
271 		if (!mm_store)
272 			goto bad;
273 		area->store = phys_to_virt(tee_mm_get_smem(mm_store),
274 					   MEM_AREA_TA_RAM);
275 		if (!area->store)
276 			goto bad;
277 		area->u.rwp = calloc(size / SMALL_PAGE_SIZE,
278 				     sizeof(struct pager_rw_pstate));
279 		if (!area->u.rwp)
280 			goto bad;
281 	} else {
282 		area->store = (void *)store;
283 		area->u.hashes = hashes;
284 	}
285 out:
286 	area->base = base;
287 	area->size = size;
288 	area->flags = flags;
289 	return area;
290 bad:
291 	tee_mm_free(mm_store);
292 	free(area->u.rwp);
293 	free(area);
294 	return NULL;
295 }
296 
297 static void area_insert_tail(struct tee_pager_area *area)
298 {
299 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_ALL);
300 
301 	cpu_spin_lock(&pager_lock);
302 
303 	TAILQ_INSERT_TAIL(&tee_pager_area_head, area, link);
304 
305 	cpu_spin_unlock(&pager_lock);
306 	thread_set_exceptions(exceptions);
307 }
308 KEEP_PAGER(area_insert_tail);
309 
310 bool tee_pager_add_core_area(vaddr_t base, size_t size, uint32_t flags,
311 			const void *store, const void *hashes)
312 {
313 	struct tee_pager_area *area;
314 	size_t tbl_va_size;
315 
316 
317 	DMSG("0x%" PRIxPTR " - 0x%" PRIxPTR " : flags 0x%x, store %p, hashes %p",
318 		base, base + size, flags, store, hashes);
319 
320 	TEE_ASSERT(!(base & SMALL_PAGE_MASK) &&
321 			size && !(size & SMALL_PAGE_MASK));
322 
323 	if (!(flags & TEE_MATTR_PW))
324 		TEE_ASSERT(store && hashes);
325 	else if (flags & TEE_MATTR_PW)
326 		TEE_ASSERT(!store && !hashes);
327 	else
328 		panic();
329 
330 	if (!tee_pager_tbl_info.num_entries) {
331 		if (!core_mmu_find_table(base, UINT_MAX, &tee_pager_tbl_info))
332 			return false;
333 		if ((1 << tee_pager_tbl_info.shift) != SMALL_PAGE_SIZE) {
334 			DMSG("Unsupported page size in translation table %u",
335 			     1 << tee_pager_tbl_info.shift);
336 			return false;
337 		}
338 	}
339 
340 	tbl_va_size = (1 << tee_pager_tbl_info.shift) *
341 			tee_pager_tbl_info.num_entries;
342 	if (!core_is_buffer_inside(base, size, tee_pager_tbl_info.va_base,
343 				   tbl_va_size)) {
344 		DMSG("area 0x%" PRIxPTR " len 0x%zx doesn't fit it translation table 0x%" PRIxVA " len 0x%zx",
345 			base, size, tee_pager_tbl_info.va_base, tbl_va_size);
346 		return false;
347 	}
348 
349 	area = alloc_area(base, size, flags, store, hashes);
350 	if (!area)
351 		return false;
352 
353 	area_insert_tail(area);
354 	return true;
355 }
356 
357 static struct tee_pager_area *tee_pager_find_area(vaddr_t va)
358 {
359 	struct tee_pager_area *area;
360 
361 	TAILQ_FOREACH(area, &tee_pager_area_head, link) {
362 		if (core_is_buffer_inside(va, 1, area->base, area->size))
363 			return area;
364 	}
365 	return NULL;
366 }
367 
368 static uint32_t get_area_mattr(struct tee_pager_area *area)
369 {
370 	return TEE_MATTR_VALID_BLOCK | TEE_MATTR_GLOBAL |
371 	       TEE_MATTR_CACHE_CACHED << TEE_MATTR_CACHE_SHIFT |
372 	       TEE_MATTR_SECURE | TEE_MATTR_PR |
373 	       (area->flags & TEE_MATTR_PRWX);
374 }
375 
376 static paddr_t get_pmem_pa(struct tee_pager_pmem *pmem)
377 {
378 	paddr_t pa;
379 	unsigned idx;
380 
381 	idx = core_mmu_va2idx(&pager_alias_tbl_info, (vaddr_t)pmem->va_alias);
382 	core_mmu_get_entry(&pager_alias_tbl_info, idx, &pa, NULL);
383 	return pa;
384 }
385 
386 static bool decrypt_page(struct pager_rw_pstate *rwp, const void *src,
387 			void *dst)
388 {
389 	struct pager_aes_gcm_iv iv = {
390 		{ (vaddr_t)rwp, rwp->iv >> 32, rwp->iv }
391 	};
392 
393 	return pager_aes_gcm_decrypt(pager_ae_key, sizeof(pager_ae_key),
394 				     &iv, rwp->tag, src, dst, SMALL_PAGE_SIZE);
395 }
396 
397 static void encrypt_page(struct pager_rw_pstate *rwp, void *src, void *dst)
398 {
399 	struct pager_aes_gcm_iv iv;
400 
401 	assert((rwp->iv + 1) > rwp->iv);
402 	rwp->iv++;
403 	/*
404 	 * IV is constructed as recommended in section "8.2.1 Deterministic
405 	 * Construction" of "Recommendation for Block Cipher Modes of
406 	 * Operation: Galois/Counter Mode (GCM) and GMAC",
407 	 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
408 	 */
409 	iv.iv[0] = (vaddr_t)rwp;
410 	iv.iv[1] = rwp->iv >> 32;
411 	iv.iv[2] = rwp->iv;
412 
413 	if (!pager_aes_gcm_encrypt(pager_ae_key, sizeof(pager_ae_key),
414 				   &iv, rwp->tag, src, dst, SMALL_PAGE_SIZE))
415 		panic();
416 }
417 
418 static void tee_pager_load_page(struct tee_pager_area *area, vaddr_t page_va,
419 			void *va_alias)
420 {
421 	size_t idx = (page_va - area->base) >> SMALL_PAGE_SHIFT;
422 	const void *stored_page = area->store + idx * SMALL_PAGE_SIZE;
423 
424 	if (!(area->flags & TEE_MATTR_PW)) {
425 		const void *hash = area->u.hashes + idx * TEE_SHA256_HASH_SIZE;
426 
427 		memcpy(va_alias, stored_page, SMALL_PAGE_SIZE);
428 		incr_ro_hits();
429 
430 		if (hash_sha256_check(hash, va_alias, SMALL_PAGE_SIZE) !=
431 				TEE_SUCCESS) {
432 			EMSG("PH 0x%" PRIxVA " failed", page_va);
433 			panic();
434 		}
435 	} else if (area->flags & TEE_MATTR_LOCKED) {
436 		FMSG("Zero init %p %#" PRIxVA, va_alias, page_va);
437 		memset(va_alias, 0, SMALL_PAGE_SIZE);
438 	} else {
439 		FMSG("Restore %p %#" PRIxVA " iv %#" PRIx64,
440 			va_alias, page_va, area->u.rwp[idx].iv);
441 		if (!area->u.rwp[idx].iv)
442 			memset(va_alias, 0, SMALL_PAGE_SIZE);
443 		else if (!decrypt_page(&area->u.rwp[idx], stored_page,
444 				       va_alias)) {
445 			EMSG("PH 0x%" PRIxVA " failed", page_va);
446 			panic();
447 		}
448 		incr_rw_hits();
449 	}
450 }
451 
452 static void tee_pager_save_page(struct tee_pager_pmem *pmem, uint32_t attr)
453 {
454 	const uint32_t dirty_bits = TEE_MATTR_PW | TEE_MATTR_UW |
455 				    TEE_MATTR_HIDDEN_DIRTY_BLOCK;
456 
457 	assert(!(pmem->area->flags & TEE_MATTR_LOCKED));
458 
459 	if (attr & dirty_bits) {
460 		size_t idx = pmem->pgidx - core_mmu_va2idx(&tee_pager_tbl_info,
461 							   pmem->area->base);
462 		void *stored_page = pmem->area->store + idx * SMALL_PAGE_SIZE;
463 
464 		assert(pmem->area->flags & TEE_MATTR_PW);
465 		encrypt_page(&pmem->area->u.rwp[idx], pmem->va_alias,
466 			     stored_page);
467 		FMSG("Saved %#" PRIxVA " iv %#" PRIx64,
468 			core_mmu_idx2va(&tee_pager_tbl_info, pmem->pgidx),
469 			pmem->area->u.rwp[idx].iv);
470 	}
471 }
472 
473 static bool tee_pager_unhide_page(vaddr_t page_va)
474 {
475 	struct tee_pager_pmem *pmem;
476 
477 	TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
478 		paddr_t pa;
479 		uint32_t attr;
480 
481 		if (pmem->pgidx == INVALID_PGIDX)
482 			continue;
483 
484 		core_mmu_get_entry(&tee_pager_tbl_info, pmem->pgidx,
485 				   &pa, &attr);
486 
487 		if (!(attr &
488 		     (TEE_MATTR_HIDDEN_BLOCK | TEE_MATTR_HIDDEN_DIRTY_BLOCK)))
489 			continue;
490 
491 		if (core_mmu_va2idx(&tee_pager_tbl_info, page_va) ==
492 		    pmem->pgidx) {
493 			uint32_t a = get_area_mattr(pmem->area);
494 
495 			/* page is hidden, show and move to back */
496 			assert(pa == get_pmem_pa(pmem));
497 			/*
498 			 * If it's not a dirty block, then it should be
499 			 * read only.
500 			 */
501 			if (!(attr & TEE_MATTR_HIDDEN_DIRTY_BLOCK))
502 				a &= ~(TEE_MATTR_PW | TEE_MATTR_UW);
503 			else
504 				FMSG("Unhide %#" PRIxVA, page_va);
505 			core_mmu_set_entry(&tee_pager_tbl_info, pmem->pgidx, pa,
506 					   a);
507 
508 			TAILQ_REMOVE(&tee_pager_pmem_head, pmem, link);
509 			TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link);
510 
511 			/* TODO only invalidate entry touched above */
512 			core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
513 
514 			incr_hidden_hits();
515 			return true;
516 		}
517 	}
518 
519 	return false;
520 }
521 
522 static void tee_pager_hide_pages(void)
523 {
524 	struct tee_pager_pmem *pmem;
525 	size_t n = 0;
526 
527 	TAILQ_FOREACH(pmem, &tee_pager_pmem_head, link) {
528 		paddr_t pa;
529 		uint32_t attr;
530 		uint32_t a;
531 
532 		if (n >= TEE_PAGER_NHIDE)
533 			break;
534 		n++;
535 
536 		/*
537 		 * we cannot hide pages when pmem->area is not defined as
538 		 * unhide requires pmem->area to be defined
539 		 */
540 		if (!pmem->area)
541 			continue;
542 
543 		core_mmu_get_entry(&tee_pager_tbl_info, pmem->pgidx,
544 				   &pa, &attr);
545 		if (!(attr & TEE_MATTR_VALID_BLOCK))
546 			continue;
547 
548 		assert(pa == get_pmem_pa(pmem));
549 		if (attr & (TEE_MATTR_PW | TEE_MATTR_UW)){
550 			a = TEE_MATTR_HIDDEN_DIRTY_BLOCK;
551 			FMSG("Hide %#" PRIxVA,
552 			     tee_pager_tbl_info.va_base +
553 			     pmem->pgidx * SMALL_PAGE_SIZE);
554 		} else
555 			a = TEE_MATTR_HIDDEN_BLOCK;
556 		core_mmu_set_entry(&tee_pager_tbl_info, pmem->pgidx, pa, a);
557 
558 	}
559 
560 	/* TODO only invalidate entries touched above */
561 	core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
562 }
563 
564 /*
565  * Find mapped pmem, hide and move to pageble pmem.
566  * Return false if page was not mapped, and true if page was mapped.
567  */
568 static bool tee_pager_release_one_phys(vaddr_t page_va)
569 {
570 	struct tee_pager_pmem *pmem;
571 	unsigned pgidx;
572 	paddr_t pa;
573 	uint32_t attr;
574 
575 	pgidx = core_mmu_va2idx(&tee_pager_tbl_info, page_va);
576 	core_mmu_get_entry(&tee_pager_tbl_info, pgidx, &pa, &attr);
577 
578 	FMSG("%" PRIxVA " : %" PRIxPA "|%x", page_va, pa, attr);
579 
580 	TAILQ_FOREACH(pmem, &tee_pager_lock_pmem_head, link) {
581 		if (pmem->pgidx != pgidx)
582 			continue;
583 
584 		assert(pa == get_pmem_pa(pmem));
585 		core_mmu_set_entry(&tee_pager_tbl_info, pgidx, 0, 0);
586 		TAILQ_REMOVE(&tee_pager_lock_pmem_head, pmem, link);
587 		pmem->area = NULL;
588 		pmem->pgidx = INVALID_PGIDX;
589 		tee_pager_npages++;
590 		set_npages();
591 		TAILQ_INSERT_HEAD(&tee_pager_pmem_head, pmem, link);
592 		incr_zi_released();
593 		return true;
594 	}
595 
596 	return false;
597 }
598 
599 /* Finds the oldest page and unmats it from its old virtual address */
600 static struct tee_pager_pmem *tee_pager_get_page(uint32_t next_area_flags)
601 {
602 	struct tee_pager_pmem *pmem;
603 
604 	pmem = TAILQ_FIRST(&tee_pager_pmem_head);
605 	if (!pmem) {
606 		EMSG("No pmem entries");
607 		return NULL;
608 	}
609 	if (pmem->pgidx != INVALID_PGIDX) {
610 		uint32_t a;
611 
612 		core_mmu_get_entry(&tee_pager_tbl_info, pmem->pgidx, NULL, &a);
613 		core_mmu_set_entry(&tee_pager_tbl_info, pmem->pgidx, 0, 0);
614 		/* TODO only invalidate entries touched above */
615 		core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
616 		tee_pager_save_page(pmem, a);
617 	}
618 
619 	TAILQ_REMOVE(&tee_pager_pmem_head, pmem, link);
620 	pmem->pgidx = INVALID_PGIDX;
621 	pmem->area = NULL;
622 	if (next_area_flags & TEE_MATTR_LOCKED) {
623 		/* Move page to lock list */
624 		TEE_ASSERT(tee_pager_npages > 0);
625 		tee_pager_npages--;
626 		set_npages();
627 		TAILQ_INSERT_TAIL(&tee_pager_lock_pmem_head, pmem, link);
628 	} else {
629 		/* move page to back */
630 		TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link);
631 	}
632 
633 	return pmem;
634 }
635 
636 static bool pager_update_permissions(struct tee_pager_area *area,
637 			struct abort_info *ai)
638 {
639 	unsigned pgidx = core_mmu_va2idx(&tee_pager_tbl_info, ai->va);
640 	uint32_t attr;
641 	paddr_t pa;
642 
643 	core_mmu_get_entry(&tee_pager_tbl_info, pgidx, &pa, &attr);
644 
645 	/* Not mapped */
646 	if (!(attr & TEE_MATTR_VALID_BLOCK))
647 		return false;
648 
649 	/* Not readable, should not happen */
650 	if (!(attr & TEE_MATTR_PR)) {
651 		abort_print_error(ai);
652 		panic();
653 	}
654 
655 	switch (core_mmu_get_fault_type(ai->fault_descr)) {
656 	case CORE_MMU_FAULT_TRANSLATION:
657 	case CORE_MMU_FAULT_READ_PERMISSION:
658 		if (ai->abort_type == ABORT_TYPE_PREFETCH &&
659 		    !(attr & TEE_MATTR_PX)) {
660 			/* Attempting to execute from an NOX page */
661 			abort_print_error(ai);
662 			panic();
663 		}
664 		/* Since the page is mapped now it's OK */
665 		return true;
666 	case CORE_MMU_FAULT_WRITE_PERMISSION:
667 		if (!(area->flags & TEE_MATTR_PW)) {
668 			/* Attempting to write to an RO page */
669 			abort_print_error(ai);
670 			panic();
671 		}
672 		if (!(attr & TEE_MATTR_PW)) {
673 			FMSG("Dirty %p", (void *)(ai->va & ~SMALL_PAGE_MASK));
674 			core_mmu_set_entry(&tee_pager_tbl_info, pgidx,
675 					   pa, attr | TEE_MATTR_PW);
676 			/* TODO only invalidate entry above */
677 			core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
678 		}
679 		/* Since permissions has been updated now it's OK */
680 		return true;
681 	default:
682 		/* Some fault we can't deal with */
683 		abort_print_error(ai);
684 		panic();
685 	}
686 
687 }
688 
689 #ifdef CFG_TEE_CORE_DEBUG
690 static void stat_handle_fault(void)
691 {
692 	static size_t num_faults;
693 	static size_t min_npages = SIZE_MAX;
694 	static size_t total_min_npages = SIZE_MAX;
695 
696 	num_faults++;
697 	if ((num_faults % 1024) == 0 || tee_pager_npages < total_min_npages) {
698 		DMSG("nfaults %zu npages %zu (min %zu)",
699 		     num_faults, tee_pager_npages, min_npages);
700 		min_npages = tee_pager_npages; /* reset */
701 	}
702 	if (tee_pager_npages < min_npages)
703 		min_npages = tee_pager_npages;
704 	if (tee_pager_npages < total_min_npages)
705 		total_min_npages = tee_pager_npages;
706 }
707 #else
708 static void stat_handle_fault(void)
709 {
710 }
711 #endif
712 
713 bool tee_pager_handle_fault(struct abort_info *ai)
714 {
715 	struct tee_pager_area *area;
716 	vaddr_t page_va = ai->va & ~SMALL_PAGE_MASK;
717 	uint32_t exceptions;
718 	bool ret;
719 
720 #ifdef TEE_PAGER_DEBUG_PRINT
721 	abort_print(ai);
722 #endif
723 
724 	/*
725 	 * We're updating pages that can affect several active CPUs at a
726 	 * time below. We end up here because a thread tries to access some
727 	 * memory that isn't available. We have to be careful when making
728 	 * that memory available as other threads may succeed in accessing
729 	 * that address the moment after we've made it available.
730 	 *
731 	 * That means that we can't just map the memory and populate the
732 	 * page, instead we use the aliased mapping to populate the page
733 	 * and once everything is ready we map it.
734 	 */
735 	exceptions = thread_mask_exceptions(THREAD_EXCP_IRQ);
736 	cpu_spin_lock(&pager_lock);
737 
738 	stat_handle_fault();
739 
740 	/* check if the access is valid */
741 	area = tee_pager_find_area(ai->va);
742 	if (!area) {
743 		EMSG("Invalid addr 0x%" PRIxVA, ai->va);
744 		ret = false;
745 		goto out;
746 	}
747 
748 	if (!tee_pager_unhide_page(page_va)) {
749 		struct tee_pager_pmem *pmem = NULL;
750 		uint32_t attr;
751 
752 		/*
753 		 * The page wasn't hidden, but some other core may have
754 		 * updated the table entry before we got here or we need
755 		 * to make a read-only page read-write (dirty).
756 		 */
757 		if (pager_update_permissions(area, ai)) {
758 			/*
759 			 * Kind of access is OK with the mapping, we're
760 			 * done here because the fault has already been
761 			 * dealt with by another core.
762 			 */
763 			ret = true;
764 			goto out;
765 		}
766 
767 		pmem = tee_pager_get_page(area->flags);
768 		if (!pmem) {
769 			abort_print(ai);
770 			panic();
771 		}
772 
773 		/* load page code & data */
774 		tee_pager_load_page(area, page_va, pmem->va_alias);
775 
776 		/*
777 		 * We've updated the page using the aliased mapping and
778 		 * some cache maintenence is now needed if it's an
779 		 * executable page.
780 		 *
781 		 * Since the d-cache is a Physically-indexed,
782 		 * physically-tagged (PIPT) cache we can clean the aliased
783 		 * address instead of the real virtual address.
784 		 *
785 		 * The i-cache can also be PIPT, but may be something else
786 		 * to, to keep it simple we invalidate the entire i-cache.
787 		 * As a future optimization we may invalidate only the
788 		 * aliased area if it a PIPT cache else the entire cache.
789 		 */
790 		if (area->flags & TEE_MATTR_PX) {
791 			/*
792 			 * Doing these operations to LoUIS (Level of
793 			 * unification, Inner Shareable) would be enough
794 			 */
795 			cache_maintenance_l1(DCACHE_AREA_CLEAN,
796 				pmem->va_alias, SMALL_PAGE_SIZE);
797 
798 			cache_maintenance_l1(ICACHE_INVALIDATE, NULL, 0);
799 		}
800 
801 		pmem->area = area;
802 		pmem->pgidx = core_mmu_va2idx(&tee_pager_tbl_info, ai->va);
803 		attr = get_area_mattr(area) & ~(TEE_MATTR_PW | TEE_MATTR_UW);
804 		core_mmu_set_entry(&tee_pager_tbl_info, pmem->pgidx,
805 				   get_pmem_pa(pmem), attr);
806 
807 		FMSG("Mapped 0x%" PRIxVA " -> 0x%" PRIxPA,
808 		     core_mmu_idx2va(&tee_pager_tbl_info, pmem->pgidx),
809 				     get_pmem_pa(pmem));
810 
811 	}
812 
813 	tee_pager_hide_pages();
814 	ret = true;
815 out:
816 	cpu_spin_unlock(&pager_lock);
817 	thread_unmask_exceptions(exceptions);
818 	return ret;
819 }
820 
821 void tee_pager_add_pages(vaddr_t vaddr, size_t npages, bool unmap)
822 {
823 	size_t n;
824 
825 	DMSG("0x%" PRIxVA " - 0x%" PRIxVA " : %d",
826 	     vaddr, vaddr + npages * SMALL_PAGE_SIZE, (int)unmap);
827 
828 	/* setup memory */
829 	for (n = 0; n < npages; n++) {
830 		struct tee_pager_pmem *pmem;
831 		tee_vaddr_t va = vaddr + n * SMALL_PAGE_SIZE;
832 		unsigned pgidx = core_mmu_va2idx(&tee_pager_tbl_info, va);
833 		paddr_t pa;
834 		uint32_t attr;
835 
836 		core_mmu_get_entry(&tee_pager_tbl_info, pgidx, &pa, &attr);
837 
838 		/* Ignore unmapped pages/blocks */
839 		if (!(attr & TEE_MATTR_VALID_BLOCK))
840 			continue;
841 
842 		pmem = malloc(sizeof(struct tee_pager_pmem));
843 		if (pmem == NULL) {
844 			EMSG("Can't allocate memory");
845 			panic();
846 		}
847 
848 		pmem->va_alias = pager_add_alias_page(pa);
849 
850 		if (unmap) {
851 			pmem->area = NULL;
852 			pmem->pgidx = INVALID_PGIDX;
853 			core_mmu_set_entry(&tee_pager_tbl_info, pgidx, 0, 0);
854 		} else {
855 			/*
856 			 * The page is still mapped, let's assign the area
857 			 * and update the protection bits accordingly.
858 			 */
859 			pmem->area = tee_pager_find_area(va);
860 			pmem->pgidx = pgidx;
861 			assert(pa == get_pmem_pa(pmem));
862 			core_mmu_set_entry(&tee_pager_tbl_info, pgidx, pa,
863 					   get_area_mattr(pmem->area));
864 		}
865 
866 		tee_pager_npages++;
867 		incr_npages_all();
868 		set_npages();
869 		TAILQ_INSERT_TAIL(&tee_pager_pmem_head, pmem, link);
870 	}
871 
872 	/* Invalidate secure TLB */
873 	core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
874 }
875 
876 void tee_pager_release_phys(void *addr, size_t size)
877 {
878 	bool unmaped = false;
879 	vaddr_t va = (vaddr_t)addr;
880 	vaddr_t begin = ROUNDUP(va, SMALL_PAGE_SIZE);
881 	vaddr_t end = ROUNDDOWN(va + size, SMALL_PAGE_SIZE);
882 	uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_ALL);
883 
884 	cpu_spin_lock(&pager_lock);
885 
886 	for (va = begin; va < end; va += SMALL_PAGE_SIZE)
887 		unmaped |= tee_pager_release_one_phys(va);
888 
889 	/* Invalidate secure TLB */
890 	if (unmaped)
891 		core_tlb_maintenance(TLBINV_UNIFIEDTLB, 0);
892 
893 	cpu_spin_unlock(&pager_lock);
894 	thread_set_exceptions(exceptions);
895 }
896 KEEP_PAGER(tee_pager_release_phys);
897 
898 void *tee_pager_alloc(size_t size, uint32_t flags)
899 {
900 	tee_mm_entry_t *mm;
901 	uint32_t f = TEE_MATTR_PRW | (flags & TEE_MATTR_LOCKED);
902 
903 	if (!size)
904 		return NULL;
905 
906 	mm = tee_mm_alloc(&tee_mm_vcore, ROUNDUP(size, SMALL_PAGE_SIZE));
907 	if (!mm)
908 		return NULL;
909 
910 	tee_pager_add_core_area(tee_mm_get_smem(mm), tee_mm_get_bytes(mm),
911 				f, NULL, NULL);
912 
913 	return (void *)tee_mm_get_smem(mm);
914 }
915