1 /*
2 * Copyright (c) 2018-2025, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <arch.h>
8 #include <arch_helpers.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <lib/xlat_tables/xlat_tables_v2.h>
12 #include <platform_def.h>
13 #include <plat/common/platform.h>
14 #include <services/spm_mm_partition.h>
15 #include <services/spm_mm_svc.h>
16
17 #include "spm_mm_private.h"
18 #include "spm_shim_private.h"
19
20 /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
21 static spinlock_t mem_attr_smc_lock;
22
23 /*
24 * Attributes are encoded using a different format in the SMC interface than in
25 * the Trusted Firmware, where the mmap_attr_t enum type is used. This function
26 * converts an attributes value from the SMC format to the mmap_attr_t format by
27 * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER.
28 * The other fields are left as 0 because they are ignored by the function
29 * xlat_change_mem_attributes_ctx().
30 */
smc_attr_to_mmap_attr(unsigned int attributes)31 static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
32 {
33 unsigned int tf_attr = 0U;
34
35 unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
36 >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
37
38 if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) {
39 tf_attr |= MT_RW | MT_USER;
40 } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) {
41 tf_attr |= MT_RO | MT_USER;
42 } else {
43 /* Other values are reserved. */
44 assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS);
45 /* The only requirement is that there's no access from EL0 */
46 tf_attr |= MT_RO | MT_PRIVILEGED;
47 }
48
49 if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
50 tf_attr |= MT_EXECUTE;
51 } else {
52 tf_attr |= MT_EXECUTE_NEVER;
53 }
54
55 return tf_attr;
56 }
57
58 /*
59 * This function converts attributes from the Trusted Firmware format into the
60 * SMC interface format.
61 */
smc_mmap_to_smc_attr(unsigned int attr)62 static unsigned int smc_mmap_to_smc_attr(unsigned int attr)
63 {
64 unsigned int smc_attr = 0U;
65
66 unsigned int data_access;
67
68 if ((attr & MT_USER) == 0) {
69 /* No access from EL0. */
70 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
71 } else {
72 if ((attr & MT_RW) != 0) {
73 assert(MT_TYPE(attr) != MT_DEVICE);
74 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW;
75 } else {
76 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO;
77 }
78 }
79
80 smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
81 << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
82
83 if ((attr & MT_EXECUTE_NEVER) != 0U) {
84 smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC;
85 }
86
87 return smc_attr;
88 }
89
spm_memory_attributes_get_smc_handler(sp_context_t * sp_ctx,uintptr_t base_va,uint32_t * page_count,uint32_t * attr)90 int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx,
91 uintptr_t base_va,
92 uint32_t *page_count,
93 uint32_t *attr)
94 {
95 uint32_t cur_attr;
96 uint32_t table_level;
97 uint32_t count;
98 int rc;
99
100 assert((page_count != NULL) && (*page_count > 0));
101 assert(attr != NULL);
102
103 base_va &= ~(PAGE_SIZE_MASK);
104
105 spin_lock(&mem_attr_smc_lock);
106
107 rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
108 base_va, attr, &table_level);
109 if (rc != 0) {
110 goto err_out;
111 }
112
113 /*
114 * Caculate how many pages in this block entry from base_va including
115 * its page.
116 */
117 count = ((XLAT_BLOCK_SIZE(table_level) -
118 (base_va & XLAT_BLOCK_MASK(table_level))) >> PAGE_SIZE_SHIFT);
119 base_va += XLAT_BLOCK_SIZE(table_level);
120
121 while ((count < *page_count) && (base_va != 0x00)) {
122 rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
123 base_va, &cur_attr, &table_level);
124 if (rc != 0) {
125 goto err_out;
126 }
127
128 if (*attr != cur_attr) {
129 *page_count = count;
130 break;
131 }
132
133 base_va += XLAT_BLOCK_SIZE(table_level);
134 count += (XLAT_BLOCK_SIZE(table_level) >> PAGE_SIZE_SHIFT);
135 }
136
137 *attr = smc_mmap_to_smc_attr(*attr);
138
139 err_out:
140 spin_unlock(&mem_attr_smc_lock);
141 /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */
142 assert((rc == 0) || (rc == -EINVAL));
143
144 if (rc == 0) {
145 return SPM_MM_SUCCESS;
146 } else {
147 return SPM_MM_INVALID_PARAMETER;
148 }
149 }
150
spm_memory_attributes_set_smc_handler(sp_context_t * sp_ctx,u_register_t page_address,u_register_t pages_count,u_register_t smc_attributes)151 int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx,
152 u_register_t page_address,
153 u_register_t pages_count,
154 u_register_t smc_attributes)
155 {
156 uintptr_t base_va = (uintptr_t) page_address;
157 size_t size = (size_t) (pages_count * PAGE_SIZE);
158 uint32_t attributes = (uint32_t) smc_attributes;
159
160 INFO(" Start address : 0x%lx\n", base_va);
161 INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count, size);
162 INFO(" Attributes : 0x%x\n", attributes);
163
164 spin_lock(&mem_attr_smc_lock);
165
166 int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
167 base_va, size,
168 smc_attr_to_mmap_attr(attributes));
169
170 spin_unlock(&mem_attr_smc_lock);
171
172 /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */
173 assert((ret == 0) || (ret == -EINVAL));
174
175 return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER;
176 }
177