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