1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2021, Linaro Limited
5  */
6 #ifndef TEE_MMU_TYPES_H
7 #define TEE_MMU_TYPES_H
8 
9 #include <stdint.h>
10 #include <sys/queue.h>
11 #include <util.h>
12 
13 #define TEE_MATTR_VALID_BLOCK		BIT(0)
14 #define TEE_MATTR_TABLE			BIT(3)
15 #define TEE_MATTR_PR			BIT(4)
16 #define TEE_MATTR_PW			BIT(5)
17 #define TEE_MATTR_PX			BIT(6)
18 #define TEE_MATTR_PRW			(TEE_MATTR_PR | TEE_MATTR_PW)
19 #define TEE_MATTR_PRX			(TEE_MATTR_PR | TEE_MATTR_PX)
20 #define TEE_MATTR_PRWX			(TEE_MATTR_PRW | TEE_MATTR_PX)
21 #define TEE_MATTR_UR			BIT(7)
22 #define TEE_MATTR_UW			BIT(8)
23 #define TEE_MATTR_UX			BIT(9)
24 #define TEE_MATTR_URW			(TEE_MATTR_UR | TEE_MATTR_UW)
25 #define TEE_MATTR_URX			(TEE_MATTR_UR | TEE_MATTR_UX)
26 #define TEE_MATTR_URWX			(TEE_MATTR_URW | TEE_MATTR_UX)
27 #define TEE_MATTR_PROT_MASK	\
28 		(TEE_MATTR_PRWX | TEE_MATTR_URWX | TEE_MATTR_GUARDED)
29 
30 #define TEE_MATTR_GLOBAL		BIT(10)
31 #define TEE_MATTR_SECURE		BIT(11)
32 
33 #define TEE_MATTR_CACHE_MASK	U(0x7)
34 #define TEE_MATTR_CACHE_SHIFT	U(12)
35 /* These are shifted TEE_MATTR_CACHE_SHIFT */
36 #define TEE_MATTR_CACHE_NONCACHE U(0)
37 #define TEE_MATTR_CACHE_CACHED	U(1)
38 
39 #define TEE_MATTR_GUARDED		BIT(15)
40 
41 /*
42  * Tags TA mappings which are only used during a single call (open session
43  * or invoke command parameters).
44  */
45 #define VM_FLAG_EPHEMERAL		BIT(0)
46 /*
47  * Tags TA mappings that must not be removed (kernel mappings while in user
48  * mode).
49  */
50 #define VM_FLAG_PERMANENT		BIT(1)
51 /* Tags TA mappings that may be shared with other TAs. */
52 #define VM_FLAG_SHAREABLE		BIT(2)
53 /* Tags temporary mappings added to load the ldelf binary */
54 #define VM_FLAG_LDELF			BIT(3)
55 /*
56  * The mapping should only be mapped read-only, not enforced by the vm_*
57  * functions.
58  */
59 #define VM_FLAG_READONLY		BIT(4)
60 
61 /*
62  * Set of flags used by tee_mmu_is_vbuf_inside_ta_private() and
63  * tee_mmu_is_vbuf_intersect_ta_private() to tell if a certain region is
64  * mapping TA internal memory or not.
65  */
66 #define VM_FLAGS_NONPRIV		(VM_FLAG_EPHEMERAL | \
67 					 VM_FLAG_PERMANENT | \
68 					 VM_FLAG_SHAREABLE)
69 
70 struct tee_mmap_region {
71 	unsigned int type; /* enum teecore_memtypes */
72 	unsigned int region_size;
73 	paddr_t pa;
74 	vaddr_t va;
75 	size_t size;
76 	uint32_t attr; /* TEE_MATTR_* above */
77 };
78 
79 struct vm_region {
80 	struct mobj *mobj;
81 	size_t offset;
82 	vaddr_t va;
83 	size_t size;
84 	uint16_t attr; /* TEE_MATTR_* above */
85 	uint16_t flags; /* VM_FLAGS_* above */
86 	TAILQ_ENTRY(vm_region) link;
87 };
88 
89 enum vm_paged_region_type {
90 	PAGED_REGION_TYPE_RO,
91 	PAGED_REGION_TYPE_RW,
92 	PAGED_REGION_TYPE_LOCK,
93 };
94 
95 struct vm_paged_region {
96 	struct fobj *fobj;
97 	size_t fobj_pgoffs;
98 	enum vm_paged_region_type type;
99 	uint32_t flags;
100 	vaddr_t base;
101 	size_t size;
102 	struct pgt **pgt_array;
103 	TAILQ_ENTRY(vm_paged_region) link;
104 	TAILQ_ENTRY(vm_paged_region) fobj_link;
105 };
106 
107 TAILQ_HEAD(vm_paged_region_head, vm_paged_region);
108 TAILQ_HEAD(vm_region_head, vm_region);
109 
110 struct vm_info {
111 	struct vm_region_head regions;
112 	unsigned int asid;
113 };
114 
mattr_perm_to_str(char * str,size_t size,uint32_t attr)115 static inline void mattr_perm_to_str(char *str, size_t size, uint32_t attr)
116 {
117 	if (size < 7)
118 		return;
119 
120 	str[0] = (attr & TEE_MATTR_UR) ? 'r' : '-';
121 	str[1] = (attr & TEE_MATTR_UW) ? 'w' : '-';
122 	str[2] = (attr & TEE_MATTR_UX) ? 'x' : '-';
123 	str[3] = (attr & TEE_MATTR_PR) ? 'R' : '-';
124 	str[4] = (attr & TEE_MATTR_PW) ? 'W' : '-';
125 	str[5] = (attr & TEE_MATTR_PX) ? 'X' : '-';
126 	str[6] = '\0';
127 }
128 
129 #endif
130