blob: c21095a0d7c1f4e49a65323d101e90194d05ddbe [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
5 */
6
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01007#include <assert.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00008#include <buffer.h>
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01009#include <errno.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000010#include <granule.h>
11#include <measurement.h>
12#include <realm.h>
13#include <ripas.h>
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010014#include <s2tt.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000015#include <smc-handler.h>
16#include <smc-rmi.h>
17#include <smc.h>
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +000018#include <status.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000019#include <stddef.h>
20#include <string.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000021
22/*
23 * Validate the map_addr value passed to RMI_RTT_* and RMI_DATA_* commands.
24 */
25static bool validate_map_addr(unsigned long map_addr,
AlexeiFedorov4faab852023-08-30 15:06:49 +010026 long level,
Soby Mathewb4c6df42022-11-09 11:13:29 +000027 struct rd *rd)
28{
AlexeiFedorov14d47ae2023-07-19 15:26:50 +010029 return ((map_addr < realm_ipa_size(rd)) &&
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +000030 s2tte_is_addr_lvl_aligned(&(rd->s2_ctx), map_addr, level));
Soby Mathewb4c6df42022-11-09 11:13:29 +000031}
32
33/*
34 * Structure commands can operate on all RTTs except for the root RTT so
35 * the minimal valid level is the stage 2 starting level + 1.
36 */
37static bool validate_rtt_structure_cmds(unsigned long map_addr,
38 long level,
39 struct rd *rd)
40{
41 int min_level = realm_rtt_starting_level(rd) + 1;
42
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010043 if ((level < min_level) || (level > S2TT_PAGE_LEVEL)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +000044 return false;
45 }
AlexeiFedorovf85f8102023-09-11 16:14:18 +010046 return validate_map_addr(map_addr, level - 1L, rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +000047}
48
49/*
50 * Map/Unmap commands can operate up to a level 2 block entry so min_level is
51 * the smallest block size.
52 */
53static bool validate_rtt_map_cmds(unsigned long map_addr,
54 long level,
55 struct rd *rd)
56{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010057 if ((level < S2TT_MIN_BLOCK_LEVEL) || (level > S2TT_PAGE_LEVEL)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +000058 return false;
59 }
60 return validate_map_addr(map_addr, level, rd);
61}
62
63/*
64 * Entry commands can operate on any entry so the minimal valid level is the
65 * stage 2 starting level.
66 */
67static bool validate_rtt_entry_cmds(unsigned long map_addr,
68 long level,
69 struct rd *rd)
70{
71 if ((level < realm_rtt_starting_level(rd)) ||
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010072 (level > S2TT_PAGE_LEVEL)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +000073 return false;
74 }
75 return validate_map_addr(map_addr, level, rd);
76}
77
AlexeiFedorovac923c82023-04-06 15:12:04 +010078unsigned long smc_rtt_create(unsigned long rd_addr,
79 unsigned long rtt_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +000080 unsigned long map_addr,
81 unsigned long ulevel)
82{
83 struct granule *g_rd;
84 struct granule *g_tbl;
85 struct rd *rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010086 struct s2tt_walk wi;
Soby Mathewb4c6df42022-11-09 11:13:29 +000087 unsigned long *s2tt, *parent_s2tt, parent_s2tte;
88 long level = (long)ulevel;
Soby Mathewb4c6df42022-11-09 11:13:29 +000089 unsigned long ret;
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +000090 struct s2tt_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +000091
92 if (!find_lock_two_granules(rtt_addr,
93 GRANULE_STATE_DELEGATED,
94 &g_tbl,
95 rd_addr,
96 GRANULE_STATE_RD,
97 &g_rd)) {
98 return RMI_ERROR_INPUT;
99 }
100
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000101 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100102 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000103
104 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
105 buffer_unmap(rd);
106 granule_unlock(g_rd);
107 granule_unlock(g_tbl);
108 return RMI_ERROR_INPUT;
109 }
110
Soby Mathewb4c6df42022-11-09 11:13:29 +0000111 s2_ctx = rd->s2_ctx;
112 buffer_unmap(rd);
113
114 /*
115 * Lock the RTT root. Enforcing locking order RD->RTT is enough to
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000116 * ensure deadlock free locking guarantee.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000117 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000118 granule_lock(s2_ctx.g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000119
120 /* Unlock RD after locking RTT Root */
121 granule_unlock(g_rd);
122
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000123 s2tt_walk_lock_unlock(&s2_ctx, map_addr, level - 1L, &wi);
AlexeiFedorov3f5d6272023-10-23 16:27:37 +0100124 if (wi.last_level != (level - 1L)) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100125 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000126 (unsigned char)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000127 goto out_unlock_llt;
128 }
129
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000130 parent_s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100131 assert(parent_s2tt != NULL);
132
Soby Mathewb4c6df42022-11-09 11:13:29 +0000133 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000134 s2tt = buffer_granule_map(g_tbl, SLOT_DELEGATED);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100135 assert(s2tt != NULL);
136
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000137 if (s2tte_is_unassigned_empty(&s2_ctx, parent_s2tte)) {
138 s2tt_init_unassigned_empty(&s2_ctx, s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000139
140 /*
141 * Increase the refcount of the parent, the granule was
142 * locked while table walking and hand-over-hand locking.
143 * Atomicity and acquire/release semantics not required because
144 * the table is accessed always locked.
145 */
146 __granule_get(wi.g_llt);
147
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000148 } else if (s2tte_is_unassigned_ram(&s2_ctx, parent_s2tte)) {
149 s2tt_init_unassigned_ram(&s2_ctx, s2tt);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100150 __granule_get(wi.g_llt);
151
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000152 } else if (s2tte_is_unassigned_ns(&s2_ctx, parent_s2tte)) {
153 s2tt_init_unassigned_ns(&s2_ctx, s2tt);
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100154 __granule_get(wi.g_llt);
155
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000156 } else if (s2tte_is_unassigned_destroyed(&s2_ctx, parent_s2tte)) {
157 s2tt_init_unassigned_destroyed(&s2_ctx, s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000158 __granule_get(wi.g_llt);
159
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000160 } else if (s2tte_is_assigned_destroyed(&s2_ctx, parent_s2tte,
161 level - 1L)) {
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100162 unsigned long block_pa;
163
164 /*
165 * We should observe parent assigned s2tte only when
166 * we create tables above this level.
167 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100168 assert(level > S2TT_MIN_BLOCK_LEVEL);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100169
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000170 block_pa = s2tte_pa(&s2_ctx, parent_s2tte, level - 1L);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100171
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000172 s2tt_init_assigned_destroyed(&s2_ctx, s2tt, block_pa, level);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100173
174 /*
175 * Increase the refcount to mark the granule as in-use. refcount
176 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
177 */
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000178 __granule_refcount_inc(g_tbl, (unsigned short)S2TTES_PER_S2TT);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100179
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000180 } else if (s2tte_is_assigned_empty(&s2_ctx, parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000181 unsigned long block_pa;
182
183 /*
184 * We should observe parent assigned s2tte only when
185 * we create tables above this level.
186 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100187 assert(level > S2TT_MIN_BLOCK_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000188
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000189 block_pa = s2tte_pa(&s2_ctx, parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000190
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000191 s2tt_init_assigned_empty(&s2_ctx, s2tt, block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000192
193 /*
194 * Increase the refcount to mark the granule as in-use. refcount
195 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
196 */
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000197 __granule_refcount_inc(g_tbl, (unsigned short)S2TTES_PER_S2TT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000198
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000199 } else if (s2tte_is_assigned_ram(&s2_ctx, parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000200 unsigned long block_pa;
201
202 /*
203 * We should observe parent valid s2tte only when
204 * we create tables above this level.
205 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100206 assert(level > S2TT_MIN_BLOCK_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000207
208 /*
209 * Break before make. This may cause spurious S2 aborts.
210 */
211 s2tte_write(&parent_s2tt[wi.index], 0UL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100212 s2tt_invalidate_block(&s2_ctx, map_addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000213
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000214 block_pa = s2tte_pa(&s2_ctx, parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000215
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000216 s2tt_init_assigned_ram(&s2_ctx, s2tt, block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000217
218 /*
219 * Increase the refcount to mark the granule as in-use. refcount
220 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
221 */
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000222 __granule_refcount_inc(g_tbl, (unsigned short)S2TTES_PER_S2TT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000223
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000224 } else if (s2tte_is_assigned_ns(&s2_ctx, parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000225 unsigned long block_pa;
226
227 /*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100228 * We should observe parent assigned_ns s2tte only when
Soby Mathewb4c6df42022-11-09 11:13:29 +0000229 * we create tables above this level.
230 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100231 assert(level > S2TT_MIN_BLOCK_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000232
233 /*
234 * Break before make. This may cause spurious S2 aborts.
235 */
236 s2tte_write(&parent_s2tt[wi.index], 0UL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100237 s2tt_invalidate_block(&s2_ctx, map_addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000238
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000239 block_pa = s2tte_pa(&s2_ctx, parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000240
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000241 s2tt_init_assigned_ns(&s2_ctx, s2tt, parent_s2tte,
242 block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000243
244 /*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100245 * Increment the refcount on the parent for the new RTT we are
246 * about to add. The NS block entry doesn't have a refcount
247 * on the parent RTT.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000248 */
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100249 __granule_get(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000250
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000251 } else if (s2tte_is_table(&s2_ctx, parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000252 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000253 (unsigned char)(level - 1L));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000254 goto out_unmap_table;
255
256 } else {
257 assert(false);
258 }
259
260 ret = RMI_SUCCESS;
261
262 granule_set_state(g_tbl, GRANULE_STATE_RTT);
263
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000264 parent_s2tte = s2tte_create_table(&s2_ctx, rtt_addr, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000265 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
266
267out_unmap_table:
268 buffer_unmap(s2tt);
269 buffer_unmap(parent_s2tt);
270out_unlock_llt:
271 granule_unlock(wi.g_llt);
272 granule_unlock(g_tbl);
273 return ret;
274}
275
AlexeiFedorove2002be2023-04-19 17:20:12 +0100276void smc_rtt_fold(unsigned long rd_addr,
277 unsigned long map_addr,
278 unsigned long ulevel,
279 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000280{
281 struct granule *g_rd;
282 struct granule *g_tbl;
283 struct rd *rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100284 struct s2tt_walk wi;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000285 unsigned long *table, *parent_s2tt, parent_s2tte;
286 long level = (long)ulevel;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000287 unsigned long rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000288 unsigned long ret;
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000289 struct s2tt_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000290
291 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
292 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100293 res->x[0] = RMI_ERROR_INPUT;
294 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000295 }
296
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000297 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100298 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000299
300 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
301 buffer_unmap(rd);
302 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100303 res->x[0] = RMI_ERROR_INPUT;
304 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000305 }
306
Soby Mathewb4c6df42022-11-09 11:13:29 +0000307 s2_ctx = rd->s2_ctx;
308 buffer_unmap(rd);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000309 granule_lock(s2_ctx.g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000310 granule_unlock(g_rd);
311
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000312 s2tt_walk_lock_unlock(&s2_ctx, map_addr, level - 1L, &wi);
AlexeiFedorov3f5d6272023-10-23 16:27:37 +0100313 if (wi.last_level != (level - 1L)) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100314 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000315 (unsigned char)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000316 goto out_unlock_parent_table;
317 }
318
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000319 parent_s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100320 assert(parent_s2tt != NULL);
321
Soby Mathewb4c6df42022-11-09 11:13:29 +0000322 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000323 if (!s2tte_is_table(&s2_ctx, parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000324 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000325 (unsigned char)(level - 1L));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000326 goto out_unmap_parent_table;
327 }
328
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000329 rtt_addr = s2tte_pa(&s2_ctx, parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000330 g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT);
331
332 /*
333 * A table descriptor S2TTE always points to a TABLE granule.
334 */
AlexeiFedorov63b71692023-04-19 11:18:42 +0100335 assert(g_tbl != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000336
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000337 table = buffer_granule_map(g_tbl, SLOT_RTT2);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100338 assert(table != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000339
340 /*
341 * The command can succeed only if all 512 S2TTEs are of the same type.
342 * We first check the table's ref. counter to speed up the case when
343 * the host makes a guess whether a memory region can be folded.
344 */
345 if (g_tbl->refcount == 0UL) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000346 if (s2tt_is_unassigned_destroyed_block(&s2_ctx, table)) {
347 parent_s2tte = s2tte_create_unassigned_destroyed(&s2_ctx);
348 } else if (s2tt_is_unassigned_empty_block(&s2_ctx, table)) {
349 parent_s2tte = s2tte_create_unassigned_empty(&s2_ctx);
350 } else if (s2tt_is_unassigned_ram_block(&s2_ctx, table)) {
351 parent_s2tte = s2tte_create_unassigned_ram(&s2_ctx);
352 } else if (s2tt_is_unassigned_ns_block(&s2_ctx, table)) {
353 parent_s2tte = s2tte_create_unassigned_ns(&s2_ctx);
354 } else if (s2tt_maps_assigned_ns_block(&s2_ctx, table, level)) {
Shruti Gupta3105c3f2024-02-26 14:06:11 +0000355
356 /*
357 * The RMM specification does not allow creating block entries less than
358 * S2TT_MIN_BLOCK_LEVEL for ASSIGNED_NS state.
359 */
360 if (level <= S2TT_MIN_BLOCK_LEVEL) {
361 ret = pack_return_code(RMI_ERROR_RTT,
362 (unsigned char)wi.last_level);
363 goto out_unmap_table;
364 }
AlexeiFedorov49752c62023-04-24 14:31:14 +0100365 unsigned long s2tte = s2tte_read(&table[0]);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100366
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100367 /*
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100368 * Since s2tt_maps_assigned_ns_block() has succedded,
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100369 * the PA in first entry of the table is aligned at
370 * parent level. Use the TTE from the first entry
371 * directly as it also has the NS attributes to be used
372 * for the parent block entry.
373 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000374 parent_s2tte = s2tte_create_assigned_ns(&s2_ctx, s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000375 } else {
376 /*
377 * The table holds a mixture of destroyed and
378 * unassigned entries.
379 */
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100380 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000381 (unsigned char)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000382 goto out_unmap_table;
383 }
AlexeiFedorov49752c62023-04-24 14:31:14 +0100384 __granule_put(wi.g_llt);
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000385 } else if (g_tbl->refcount == (unsigned short)S2TTES_PER_S2TT) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000386
387 unsigned long s2tte, block_pa;
388
389 /* The RMM specification does not allow creating block
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100390 * entries less than S2TT_MIN_BLOCK_LEVEL even though
Soby Mathewb4c6df42022-11-09 11:13:29 +0000391 * permitted by the Arm Architecture.
392 * Hence ensure that the table being folded is at a level
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100393 * higher than the S2TT_MIN_BLOCK_LEVEL.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000394 *
395 * A fully populated table cannot be destroyed if that
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100396 * would create a block mapping below S2TT_MIN_BLOCK_LEVEL.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000397 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100398 if (level <= S2TT_MIN_BLOCK_LEVEL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100399 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000400 (unsigned char)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000401 goto out_unmap_table;
402 }
403
404 s2tte = s2tte_read(&table[0]);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000405 block_pa = s2tte_pa(&s2_ctx, s2tte, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000406
407 /*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100408 * The table must also refer to a contiguous block through the
AlexeiFedorov3f840a02023-07-19 10:55:05 +0100409 * same type of s2tte, either Assigned or Valid.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000410 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000411 if (s2tt_maps_assigned_empty_block(&s2_ctx, table, level)) {
412 parent_s2tte = s2tte_create_assigned_empty(&s2_ctx,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100413 block_pa, level - 1L);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000414 } else if (s2tt_maps_assigned_ram_block(&s2_ctx,
415 table, level)) {
416 parent_s2tte = s2tte_create_assigned_ram(&s2_ctx,
417 block_pa,
AlexeiFedorov3a739332023-04-13 13:54:04 +0100418 level - 1L);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000419 } else if (s2tt_maps_assigned_destroyed_block(&s2_ctx,
420 table, level)) {
421 parent_s2tte = s2tte_create_assigned_destroyed(&s2_ctx,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100422 block_pa, level - 1L);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000423 /* The table contains mixed entries that cannot be folded */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000424 } else {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100425 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000426 (unsigned char)level);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000427 goto out_unmap_table;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000428 }
429
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000430 __granule_refcount_dec(g_tbl, (unsigned short)S2TTES_PER_S2TT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000431 } else {
432 /*
433 * The table holds a mixture of different types of s2ttes.
434 */
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100435 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000436 (unsigned char)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000437 goto out_unmap_table;
438 }
439
440 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100441 res->x[1] = rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000442
443 /*
444 * Break before make.
445 */
446 s2tte_write(&parent_s2tt[wi.index], 0UL);
447
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000448 if (s2tte_is_assigned_ram(&s2_ctx, parent_s2tte, level - 1L) ||
449 s2tte_is_assigned_ns(&s2_ctx, parent_s2tte, level - 1L)) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100450 s2tt_invalidate_pages_in_block(&s2_ctx, map_addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000451 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100452 s2tt_invalidate_block(&s2_ctx, map_addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000453 }
454
455 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
456
457 granule_memzero_mapped(table);
458 granule_set_state(g_tbl, GRANULE_STATE_DELEGATED);
459
460out_unmap_table:
461 buffer_unmap(table);
462 granule_unlock(g_tbl);
463out_unmap_parent_table:
464 buffer_unmap(parent_s2tt);
465out_unlock_parent_table:
466 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100467 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000468}
469
AlexeiFedorove2002be2023-04-19 17:20:12 +0100470void smc_rtt_destroy(unsigned long rd_addr,
471 unsigned long map_addr,
472 unsigned long ulevel,
473 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000474{
475 struct granule *g_rd;
476 struct granule *g_tbl;
477 struct rd *rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100478 struct s2tt_walk wi;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000479 unsigned long *table, *parent_s2tt, parent_s2tte;
480 long level = (long)ulevel;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000481 unsigned long rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000482 unsigned long ret;
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000483 struct s2tt_context s2_ctx;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100484 bool in_par, skip_non_live = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000485
486 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
487 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100488 res->x[0] = RMI_ERROR_INPUT;
AlexeiFedorov3ebd4622023-07-18 16:27:39 +0100489 res->x[2] = 0UL;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100490 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000491 }
492
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000493 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100494 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000495
496 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
497 buffer_unmap(rd);
498 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100499 res->x[0] = RMI_ERROR_INPUT;
AlexeiFedorov3ebd4622023-07-18 16:27:39 +0100500 res->x[2] = 0UL;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100501 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000502 }
503
Soby Mathewb4c6df42022-11-09 11:13:29 +0000504 s2_ctx = rd->s2_ctx;
505 in_par = addr_in_par(rd, map_addr);
506 buffer_unmap(rd);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000507 granule_lock(s2_ctx.g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000508 granule_unlock(g_rd);
509
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000510 s2tt_walk_lock_unlock(&s2_ctx, map_addr, level - 1L, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000511
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000512 parent_s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100513 assert(parent_s2tt != NULL);
514
Soby Mathewb4c6df42022-11-09 11:13:29 +0000515 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100516
AlexeiFedorov3f5d6272023-10-23 16:27:37 +0100517 if ((wi.last_level != (level - 1L)) ||
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000518 !s2tte_is_table(&s2_ctx, parent_s2tte, level - 1L)) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100519 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000520 (unsigned char)wi.last_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100521 skip_non_live = true;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000522 goto out_unmap_parent_table;
523 }
524
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000525 rtt_addr = s2tte_pa(&s2_ctx, parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000526
527 /*
528 * Lock the RTT granule. The 'rtt_addr' is verified, thus can be treated
529 * as an internal granule.
530 */
531 g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT);
532
533 /*
534 * A table descriptor S2TTE always points to a TABLE granule.
535 */
536 assert(g_tbl != NULL);
537
538 /*
539 * Read the refcount value. RTT granule is always accessed locked, thus
540 * the refcount can be accessed without atomic operations.
541 */
542 if (g_tbl->refcount != 0UL) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000543 ret = pack_return_code(RMI_ERROR_RTT, (unsigned char)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000544 goto out_unlock_table;
545 }
546
547 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100548 res->x[1] = rtt_addr;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100549 skip_non_live = true;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000550
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000551 table = buffer_granule_map(g_tbl, SLOT_RTT2);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100552 assert(table != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000553
554 if (in_par) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000555 parent_s2tte = s2tte_create_unassigned_destroyed(&s2_ctx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000556 } else {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000557 parent_s2tte = s2tte_create_unassigned_ns(&s2_ctx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000558 }
559
560 __granule_put(wi.g_llt);
561
562 /*
563 * Break before make. Note that this may cause spurious S2 aborts.
564 */
565 s2tte_write(&parent_s2tt[wi.index], 0UL);
AlexeiFedorov22ba1062023-11-15 16:23:37 +0000566
567 if (in_par) {
568 /* For protected IPA, all S2TTEs in the RTT will be invalid */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100569 s2tt_invalidate_block(&s2_ctx, map_addr);
AlexeiFedorov22ba1062023-11-15 16:23:37 +0000570 } else {
571 /*
572 * For unprotected IPA, invalidate the TLB for the entire range
573 * mapped by the RTT as it may have valid NS mappings.
574 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100575 s2tt_invalidate_pages_in_block(&s2_ctx, map_addr);
AlexeiFedorov22ba1062023-11-15 16:23:37 +0000576 }
577
Soby Mathewb4c6df42022-11-09 11:13:29 +0000578 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
579
580 granule_memzero_mapped(table);
581 granule_set_state(g_tbl, GRANULE_STATE_DELEGATED);
582
583 buffer_unmap(table);
584out_unlock_table:
585 granule_unlock(g_tbl);
586out_unmap_parent_table:
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100587 if (skip_non_live) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000588 res->x[2] = s2tt_skip_non_live_entries(&s2_ctx, map_addr,
589 parent_s2tt, &wi);
AlexeiFedorov3ebd4622023-07-18 16:27:39 +0100590 } else {
591 res->x[2] = map_addr;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100592 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000593 buffer_unmap(parent_s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000594 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100595 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000596}
597
598enum map_unmap_ns_op {
599 MAP_NS,
600 UNMAP_NS
601};
602
603/*
604 * We don't hold a reference on the NS granule when it is
605 * mapped into a realm. Instead we rely on the guarantees
606 * provided by the architecture to ensure that a NS access
607 * to a protected granule is prohibited even within the realm.
608 */
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100609static void map_unmap_ns(unsigned long rd_addr,
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000610 unsigned long map_addr,
611 long level,
612 unsigned long host_s2tte,
613 enum map_unmap_ns_op op,
614 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000615{
616 struct granule *g_rd;
617 struct rd *rd;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000618 unsigned long *s2tt, s2tte;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100619 struct s2tt_walk wi;
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000620 struct s2tt_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000621
622 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
623 if (g_rd == NULL) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100624 res->x[0] = RMI_ERROR_INPUT;
625 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000626 }
627
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000628 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100629 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000630
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000631 s2_ctx = rd->s2_ctx;
632
633 if (op == MAP_NS) {
634 if (!host_ns_s2tte_is_valid(&s2_ctx, host_s2tte, level)) {
635 buffer_unmap(rd);
636 granule_unlock(g_rd);
637 res->x[0] = RMI_ERROR_INPUT;
638 return;
639 }
640 }
641
642
Soby Mathewb4c6df42022-11-09 11:13:29 +0000643 if (!validate_rtt_map_cmds(map_addr, level, rd)) {
644 buffer_unmap(rd);
645 granule_unlock(g_rd);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100646 res->x[0] = RMI_ERROR_INPUT;
647 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000648 }
649
AlexeiFedorovc34e3242023-04-12 11:30:33 +0100650 /* Check if map_addr is outside PAR */
651 if (addr_in_par(rd, map_addr)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000652 buffer_unmap(rd);
653 granule_unlock(g_rd);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100654 res->x[0] = RMI_ERROR_INPUT;
655 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000656 }
657
Soby Mathewb4c6df42022-11-09 11:13:29 +0000658 buffer_unmap(rd);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000659 granule_lock(s2_ctx.g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000660 granule_unlock(g_rd);
661
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000662 s2tt_walk_lock_unlock(&s2_ctx, map_addr, level, &wi);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100663
664 /*
665 * For UNMAP_NS, we need to map the table and look
666 * for the end of the non-live region.
667 */
AlexeiFedorov14d47ae2023-07-19 15:26:50 +0100668 if ((op == MAP_NS) && (wi.last_level != level)) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100669 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000670 (unsigned char)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000671 goto out_unlock_llt;
672 }
673
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000674 s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100675 assert(s2tt != NULL);
676
Soby Mathewb4c6df42022-11-09 11:13:29 +0000677 s2tte = s2tte_read(&s2tt[wi.index]);
678
679 if (op == MAP_NS) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000680 if (!s2tte_is_unassigned_ns(&s2_ctx, s2tte)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100681 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000682 (unsigned char)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000683 goto out_unmap_table;
684 }
685
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000686 s2tte = s2tte_create_assigned_ns(&s2_ctx, host_s2tte, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000687 s2tte_write(&s2tt[wi.index], s2tte);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000688
689 } else if (op == UNMAP_NS) {
690 /*
691 * The following check also verifies that map_addr is outside
692 * PAR, as valid_NS s2tte may only cover outside PAR IPA range.
693 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000694 bool assigned_ns = s2tte_is_assigned_ns(&s2_ctx, s2tte,
695 wi.last_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100696
697 if ((wi.last_level != level) || !assigned_ns) {
698 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000699 (unsigned char)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000700 goto out_unmap_table;
701 }
702
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000703 s2tte = s2tte_create_unassigned_ns(&s2_ctx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000704 s2tte_write(&s2tt[wi.index], s2tte);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100705 if (level == S2TT_PAGE_LEVEL) {
706 s2tt_invalidate_page(&s2_ctx, map_addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000707 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100708 s2tt_invalidate_block(&s2_ctx, map_addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000709 }
710 }
711
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100712 res->x[0] = RMI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000713
714out_unmap_table:
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100715 if (op == UNMAP_NS) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000716 res->x[1] = s2tt_skip_non_live_entries(&s2_ctx, map_addr,
717 s2tt, &wi);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100718 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000719 buffer_unmap(s2tt);
720out_unlock_llt:
721 granule_unlock(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000722}
723
724unsigned long smc_rtt_map_unprotected(unsigned long rd_addr,
725 unsigned long map_addr,
726 unsigned long ulevel,
727 unsigned long s2tte)
728{
729 long level = (long)ulevel;
Shruti Gupta9e966b82024-03-21 13:45:24 +0000730 struct smc_result res;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000731
Shruti Gupta9e966b82024-03-21 13:45:24 +0000732 (void)memset(&res, 0, sizeof(struct smc_result));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100733 if ((level < S2TT_MIN_BLOCK_LEVEL) || (level > S2TT_PAGE_LEVEL)) {
AlexeiFedorov7a2f5882023-09-14 11:41:32 +0100734 return RMI_ERROR_INPUT;
735 }
736
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100737 map_unmap_ns(rd_addr, map_addr, level, s2tte, MAP_NS, &res);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000738
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100739 return res.x[0];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000740}
741
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100742void smc_rtt_unmap_unprotected(unsigned long rd_addr,
743 unsigned long map_addr,
744 unsigned long ulevel,
745 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000746{
AlexeiFedorov7a2f5882023-09-14 11:41:32 +0100747 long level = (long)ulevel;
748
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100749 if ((level < S2TT_MIN_BLOCK_LEVEL) || (level > S2TT_PAGE_LEVEL)) {
AlexeiFedorov7a2f5882023-09-14 11:41:32 +0100750 res->x[0] = RMI_ERROR_INPUT;
751 return;
752 }
753
754 map_unmap_ns(rd_addr, map_addr, level, 0UL, UNMAP_NS, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000755}
756
757void smc_rtt_read_entry(unsigned long rd_addr,
758 unsigned long map_addr,
759 unsigned long ulevel,
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100760 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000761{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000762 struct granule *g_rd;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000763 struct rd *rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100764 struct s2tt_walk wi;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000765 unsigned long *s2tt, s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000766 long level = (long)ulevel;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000767 struct s2tt_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000768
769 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
770 if (g_rd == NULL) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100771 res->x[0] = RMI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000772 return;
773 }
774
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000775 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100776 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000777
778 if (!validate_rtt_entry_cmds(map_addr, level, rd)) {
779 buffer_unmap(rd);
780 granule_unlock(g_rd);
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100781 res->x[0] = RMI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000782 return;
783 }
784
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000785 s2_ctx = rd->s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000786 buffer_unmap(rd);
787
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000788 granule_lock(s2_ctx.g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000789 granule_unlock(g_rd);
790
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000791 s2tt_walk_lock_unlock(&s2_ctx, map_addr, level, &wi);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000792 s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100793 assert(s2tt != NULL);
794
Soby Mathewb4c6df42022-11-09 11:13:29 +0000795 s2tte = s2tte_read(&s2tt[wi.index]);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100796 res->x[1] = (unsigned long)wi.last_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000797
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000798 if (s2tte_is_unassigned_empty(&s2_ctx, s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100799 res->x[2] = RMI_UNASSIGNED;
800 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100801 res->x[4] = (unsigned long)RIPAS_EMPTY;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000802 } else if (s2tte_is_unassigned_ram(&s2_ctx, s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100803 res->x[2] = RMI_UNASSIGNED;
804 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100805 res->x[4] = (unsigned long)RIPAS_RAM;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000806 } else if (s2tte_is_unassigned_destroyed(&s2_ctx, s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100807 res->x[2] = RMI_UNASSIGNED;
808 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100809 res->x[4] = (unsigned long)RIPAS_DESTROYED;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000810 } else if (s2tte_is_assigned_empty(&s2_ctx, s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100811 res->x[2] = RMI_ASSIGNED;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000812 res->x[3] = s2tte_pa(&s2_ctx, s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100813 res->x[4] = (unsigned long)RIPAS_EMPTY;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000814 } else if (s2tte_is_assigned_ram(&s2_ctx, s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100815 res->x[2] = RMI_ASSIGNED;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000816 res->x[3] = s2tte_pa(&s2_ctx, s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100817 res->x[4] = (unsigned long)RIPAS_RAM;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000818 } else if (s2tte_is_assigned_destroyed(&s2_ctx, s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100819 res->x[2] = RMI_ASSIGNED;
820 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100821 res->x[4] = (unsigned long)RIPAS_DESTROYED;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000822 } else if (s2tte_is_unassigned_ns(&s2_ctx, s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100823 res->x[2] = RMI_UNASSIGNED;
824 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100825 res->x[4] = (unsigned long)RIPAS_EMPTY;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000826 } else if (s2tte_is_assigned_ns(&s2_ctx, s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100827 res->x[2] = RMI_ASSIGNED;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000828 res->x[3] = host_ns_s2tte(&s2_ctx, s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100829 res->x[4] = (unsigned long)RIPAS_EMPTY;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000830 } else if (s2tte_is_table(&s2_ctx, s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100831 res->x[2] = RMI_TABLE;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000832 res->x[3] = s2tte_pa(&s2_ctx, s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100833 res->x[4] = (unsigned long)RIPAS_EMPTY;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000834 } else {
835 assert(false);
836 }
837
838 buffer_unmap(s2tt);
839 granule_unlock(wi.g_llt);
840
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100841 res->x[0] = RMI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000842}
843
Soby Mathewb4c6df42022-11-09 11:13:29 +0000844static unsigned long validate_data_create_unknown(unsigned long map_addr,
845 struct rd *rd)
846{
847 if (!addr_in_par(rd, map_addr)) {
848 return RMI_ERROR_INPUT;
849 }
850
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100851 if (!validate_map_addr(map_addr, S2TT_PAGE_LEVEL, rd)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000852 return RMI_ERROR_INPUT;
853 }
854
855 return RMI_SUCCESS;
856}
857
858static unsigned long validate_data_create(unsigned long map_addr,
859 struct rd *rd)
860{
Mate Toth-Pal988dfcb2024-01-19 10:52:06 +0100861 if (get_rd_state_locked(rd) != REALM_NEW) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000862 return RMI_ERROR_REALM;
863 }
864
865 return validate_data_create_unknown(map_addr, rd);
866}
867
868/*
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100869 * Implements both RMI_DATA_CREATE and RMI_DATA_CREATE_UNKNOWN
Soby Mathewb4c6df42022-11-09 11:13:29 +0000870 *
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100871 * if @g_src == NULL, implements RMI_DATA_CREATE_UNKNOWN
872 * and RMI_DATA_CREATE otherwise.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000873 */
AlexeiFedorovac923c82023-04-06 15:12:04 +0100874static unsigned long data_create(unsigned long rd_addr,
875 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000876 unsigned long map_addr,
877 struct granule *g_src,
878 unsigned long flags)
879{
880 struct granule *g_data;
881 struct granule *g_rd;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000882 struct rd *rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100883 struct s2tt_walk wi;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000884 struct s2tt_context *s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000885 unsigned long s2tte, *s2tt;
AlexeiFedorovd6d93d82024-02-13 16:52:11 +0000886 unsigned char new_data_state = GRANULE_STATE_DELEGATED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000887 unsigned long ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000888
889 if (!find_lock_two_granules(data_addr,
890 GRANULE_STATE_DELEGATED,
891 &g_data,
892 rd_addr,
893 GRANULE_STATE_RD,
894 &g_rd)) {
895 return RMI_ERROR_INPUT;
896 }
897
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000898 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100899 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000900
901 ret = (g_src != NULL) ?
902 validate_data_create(map_addr, rd) :
903 validate_data_create_unknown(map_addr, rd);
904
905 if (ret != RMI_SUCCESS) {
906 goto out_unmap_rd;
907 }
908
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000909 s2_ctx = &(rd->s2_ctx);
910 granule_lock(s2_ctx->g_rtt, GRANULE_STATE_RTT);
911
912 s2tt_walk_lock_unlock(s2_ctx, map_addr, S2TT_PAGE_LEVEL, &wi);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100913 if (wi.last_level != S2TT_PAGE_LEVEL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100914 ret = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000915 (unsigned char)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000916 goto out_unlock_ll_table;
917 }
918
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000919 s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100920 assert(s2tt != NULL);
921
Soby Mathewb4c6df42022-11-09 11:13:29 +0000922 s2tte = s2tte_read(&s2tt[wi.index]);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000923 if (!s2tte_is_unassigned(s2_ctx, s2tte)) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000924 ret = pack_return_code(RMI_ERROR_RTT,
925 (unsigned char)S2TT_PAGE_LEVEL);
AlexeiFedorov4e2db162023-10-10 13:57:22 +0100926 goto out_unmap_ll_table;
927 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000928
Soby Mathewb4c6df42022-11-09 11:13:29 +0000929 if (g_src != NULL) {
930 bool ns_access_ok;
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000931 void *data = buffer_granule_map(g_data, SLOT_DELEGATED);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000932
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100933 assert(data != NULL);
934
Soby Mathewb4c6df42022-11-09 11:13:29 +0000935 ns_access_ok = ns_buffer_read(SLOT_NS, g_src, 0U,
936 GRANULE_SIZE, data);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000937 if (!ns_access_ok) {
938 /*
939 * Some data may be copied before the failure. Zero
940 * g_data granule as it will remain in delegated state.
941 */
AlexeiFedorov862f96c2024-03-01 16:26:48 +0000942 granule_memzero_mapped(data);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000943 buffer_unmap(data);
944 ret = RMI_ERROR_INPUT;
945 goto out_unmap_ll_table;
946 }
947
Mate Toth-Palc7698312023-08-09 12:49:34 +0200948 measurement_data_granule_measure(
949 rd->measurement[RIM_MEASUREMENT_SLOT],
950 rd->algorithm,
951 data,
952 map_addr,
953 flags);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000954 buffer_unmap(data);
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100955
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000956 s2tte = s2tte_create_assigned_ram(s2_ctx, data_addr,
957 S2TT_PAGE_LEVEL);
AlexeiFedorov4e2db162023-10-10 13:57:22 +0100958 } else {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000959 s2tte = s2tte_create_assigned_unchanged(s2_ctx, s2tte,
960 data_addr,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100961 S2TT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000962 }
963
964 new_data_state = GRANULE_STATE_DATA;
965
Soby Mathewb4c6df42022-11-09 11:13:29 +0000966 s2tte_write(&s2tt[wi.index], s2tte);
967 __granule_get(wi.g_llt);
968
969 ret = RMI_SUCCESS;
970
971out_unmap_ll_table:
972 buffer_unmap(s2tt);
973out_unlock_ll_table:
974 granule_unlock(wi.g_llt);
975out_unmap_rd:
976 buffer_unmap(rd);
977 granule_unlock(g_rd);
978 granule_unlock_transition(g_data, new_data_state);
979 return ret;
980}
981
AlexeiFedorovac923c82023-04-06 15:12:04 +0100982unsigned long smc_data_create(unsigned long rd_addr,
983 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000984 unsigned long map_addr,
985 unsigned long src_addr,
986 unsigned long flags)
987{
988 struct granule *g_src;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000989
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100990 if ((flags != RMI_NO_MEASURE_CONTENT) &&
991 (flags != RMI_MEASURE_CONTENT)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000992 return RMI_ERROR_INPUT;
993 }
994
995 g_src = find_granule(src_addr);
996 if ((g_src == NULL) || (g_src->state != GRANULE_STATE_NS)) {
997 return RMI_ERROR_INPUT;
998 }
999
AlexeiFedorovac923c82023-04-06 15:12:04 +01001000 return data_create(rd_addr, data_addr, map_addr, g_src, flags);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001001}
1002
AlexeiFedorovac923c82023-04-06 15:12:04 +01001003unsigned long smc_data_create_unknown(unsigned long rd_addr,
1004 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +00001005 unsigned long map_addr)
1006{
AlexeiFedorovac923c82023-04-06 15:12:04 +01001007 return data_create(rd_addr, data_addr, map_addr, NULL, 0);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001008}
1009
AlexeiFedorove2002be2023-04-19 17:20:12 +01001010void smc_data_destroy(unsigned long rd_addr,
1011 unsigned long map_addr,
1012 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001013{
1014 struct granule *g_data;
1015 struct granule *g_rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001016 struct s2tt_walk wi;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001017 unsigned long data_addr, s2tte, *s2tt;
1018 struct rd *rd;
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +00001019 struct s2tt_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001020
1021 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
1022 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +01001023 res->x[0] = RMI_ERROR_INPUT;
AlexeiFedorova1b2a1d2023-07-18 15:08:47 +01001024 res->x[2] = 0UL;
AlexeiFedorove2002be2023-04-19 17:20:12 +01001025 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001026 }
1027
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001028 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001029 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001030
AlexeiFedorov868a6512023-09-14 13:21:11 +01001031 if (!addr_in_par(rd, map_addr) ||
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001032 !validate_map_addr(map_addr, S2TT_PAGE_LEVEL, rd)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001033 buffer_unmap(rd);
1034 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +01001035 res->x[0] = RMI_ERROR_INPUT;
AlexeiFedorova1b2a1d2023-07-18 15:08:47 +01001036 res->x[2] = 0UL;
AlexeiFedorove2002be2023-04-19 17:20:12 +01001037 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001038 }
1039
Soby Mathewb4c6df42022-11-09 11:13:29 +00001040 s2_ctx = rd->s2_ctx;
1041 buffer_unmap(rd);
1042
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001043 granule_lock(s2_ctx.g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001044 granule_unlock(g_rd);
1045
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001046 s2tt_walk_lock_unlock(&s2_ctx, map_addr, S2TT_PAGE_LEVEL, &wi);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001047 s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001048 assert(s2tt != NULL);
1049
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001050 if (wi.last_level != S2TT_PAGE_LEVEL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +01001051 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001052 (unsigned char)wi.last_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001053 goto out_unmap_ll_table;
1054 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001055
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001056 s2tte = s2tte_read(&s2tt[wi.index]);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +01001057
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001058 if (s2tte_is_assigned_ram(&s2_ctx, s2tte, S2TT_PAGE_LEVEL)) {
1059 data_addr = s2tte_pa(&s2_ctx, s2tte, S2TT_PAGE_LEVEL);
1060 s2tte = s2tte_create_unassigned_destroyed(&s2_ctx);
AlexeiFedorova43cd312023-04-17 11:42:25 +01001061 s2tte_write(&s2tt[wi.index], s2tte);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001062 s2tt_invalidate_page(&s2_ctx, map_addr);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001063 } else if (s2tte_is_assigned_empty(&s2_ctx, s2tte, S2TT_PAGE_LEVEL)) {
1064 data_addr = s2tte_pa(&s2_ctx, s2tte, S2TT_PAGE_LEVEL);
1065 s2tte = s2tte_create_unassigned_empty(&s2_ctx);
AlexeiFedorova43cd312023-04-17 11:42:25 +01001066 s2tte_write(&s2tt[wi.index], s2tte);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001067 } else if (s2tte_is_assigned_destroyed(&s2_ctx, s2tte,
1068 S2TT_PAGE_LEVEL)) {
1069 data_addr = s2tte_pa(&s2_ctx, s2tte, S2TT_PAGE_LEVEL);
1070 s2tte = s2tte_create_unassigned_destroyed(&s2_ctx);
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +01001071 s2tte_write(&s2tt[wi.index], s2tte);
AlexeiFedorova43cd312023-04-17 11:42:25 +01001072 } else {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001073 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1074 (unsigned char)S2TT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001075 goto out_unmap_ll_table;
1076 }
1077
Soby Mathewb4c6df42022-11-09 11:13:29 +00001078 __granule_put(wi.g_llt);
1079
1080 /*
1081 * Lock the data granule and check expected state. Correct locking order
1082 * is guaranteed because granule address is obtained from a locked
1083 * granule by table walk. This lock needs to be acquired before a state
1084 * transition to or from GRANULE_STATE_DATA for granule address can happen.
1085 */
1086 g_data = find_lock_granule(data_addr, GRANULE_STATE_DATA);
AlexeiFedorov63b71692023-04-19 11:18:42 +01001087 assert(g_data != NULL);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001088 buffer_granule_memzero(g_data, SLOT_DELEGATED);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001089 granule_unlock_transition(g_data, GRANULE_STATE_DELEGATED);
1090
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001091 res->x[0] = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +01001092 res->x[1] = data_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001093out_unmap_ll_table:
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001094 res->x[2] = s2tt_skip_non_live_entries(&s2_ctx, map_addr, s2tt, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001095 buffer_unmap(s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001096 granule_unlock(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001097}
1098
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001099/*
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001100 * Update the ripas value for the entry pointed by @s2ttep.
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001101 *
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001102 * Returns:
1103 * < 0 - On error and the operation was aborted,
1104 * e.g., entry cannot have a ripas.
1105 * 0 - Operation was success and no TLBI is required.
1106 * > 0 - Operation was success and TLBI is required.
1107 * Sets:
1108 * @(*do_tlbi) to 'true' if the TLBs have to be invalidated.
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001109 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001110static int update_ripas(const struct s2tt_context *s2_ctx,
1111 unsigned long *s2ttep, long level,
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001112 enum ripas ripas_val,
1113 enum ripas_change_destroyed change_destroyed)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001114{
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001115 unsigned long pa, s2tte = s2tte_read(s2ttep);
1116 int ret = 0;
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001117
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001118 assert(s2_ctx != NULL);
1119
1120 if (!s2tte_has_ripas(s2_ctx, s2tte, level)) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001121 return -EPERM;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001122 }
1123
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001124 if (ripas_val == RIPAS_RAM) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001125 if (s2tte_is_unassigned_empty(s2_ctx, s2tte)) {
1126 s2tte = s2tte_create_unassigned_ram(s2_ctx);
1127 } else if (s2tte_is_unassigned_destroyed(s2_ctx, s2tte)) {
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001128 if (change_destroyed == CHANGE_DESTROYED) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001129 s2tte = s2tte_create_unassigned_ram(s2_ctx);
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001130 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001131 return -EINVAL;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001132 }
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001133 } else if (s2tte_is_assigned_empty(s2_ctx, s2tte, level)) {
1134 pa = s2tte_pa(s2_ctx, s2tte, level);
1135 s2tte = s2tte_create_assigned_ram(s2_ctx, pa, level);
1136 } else if (s2tte_is_assigned_destroyed(s2_ctx, s2tte, level)) {
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001137 if (change_destroyed == CHANGE_DESTROYED) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001138 pa = s2tte_pa(s2_ctx, s2tte, level);
1139 s2tte = s2tte_create_assigned_ram(s2_ctx, pa,
1140 level);
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001141 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001142 return -EINVAL;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001143 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001144 } else {
1145 /* No action is required */
1146 return 0;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001147 }
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001148 } else if (ripas_val == RIPAS_EMPTY) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001149 if (s2tte_is_unassigned_ram(s2_ctx, s2tte)) {
1150 s2tte = s2tte_create_unassigned_empty(s2_ctx);
1151 } else if (s2tte_is_unassigned_destroyed(s2_ctx, s2tte)) {
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001152 if (change_destroyed == CHANGE_DESTROYED) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001153 s2tte = s2tte_create_unassigned_empty(s2_ctx);
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001154 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001155 return -EINVAL;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001156 }
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001157 } else if (s2tte_is_assigned_ram(s2_ctx, s2tte, level)) {
1158 pa = s2tte_pa(s2_ctx, s2tte, level);
1159 s2tte = s2tte_create_assigned_empty(s2_ctx, pa, level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001160 /* TLBI is required */
1161 ret = 1;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001162 } else if (s2tte_is_assigned_destroyed(s2_ctx, s2tte, level)) {
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001163 if (change_destroyed == CHANGE_DESTROYED) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001164 pa = s2tte_pa(s2_ctx, s2tte, level);
1165 s2tte = s2tte_create_assigned_empty(s2_ctx,
1166 pa, level);
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001167 /* TLBI is required */
1168 ret = 1;
1169 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001170 return -EINVAL;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001171 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001172 } else {
1173 /* No action is required */
1174 return 0;
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001175 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001176 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001177 s2tte_write(s2ttep, s2tte);
1178 return ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001179}
1180
AlexeiFedorov960d1612023-04-25 13:23:39 +01001181void smc_rtt_init_ripas(unsigned long rd_addr,
1182 unsigned long base,
1183 unsigned long top,
1184 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001185{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001186 struct granule *g_rd;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001187 struct rd *rd;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001188 unsigned long addr, map_size;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001189 struct s2tt_walk wi;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001190 struct s2tt_context *s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001191 unsigned long s2tte, *s2tt;
AlexeiFedorov960d1612023-04-25 13:23:39 +01001192 long level;
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001193 unsigned long index;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001194 unsigned int s2ttes_per_s2tt;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001195
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001196 if (top <= base) {
1197 res->x[0] = RMI_ERROR_INPUT;
1198 return;
1199 }
1200
Soby Mathewb4c6df42022-11-09 11:13:29 +00001201 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
1202 if (g_rd == NULL) {
AlexeiFedorov960d1612023-04-25 13:23:39 +01001203 res->x[0] = RMI_ERROR_INPUT;
1204 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001205 }
1206
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001207 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001208 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001209
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001210 if (!validate_map_addr(base, S2TT_PAGE_LEVEL, rd) ||
1211 !validate_map_addr(top, S2TT_PAGE_LEVEL, rd) ||
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001212 !addr_in_par(rd, base) || !addr_in_par(rd, top - GRANULE_SIZE)) {
1213 buffer_unmap(rd);
1214 granule_unlock(g_rd);
1215 res->x[0] = RMI_ERROR_INPUT;
1216 return;
1217 }
1218
Mate Toth-Pal988dfcb2024-01-19 10:52:06 +01001219 if (get_rd_state_locked(rd) != REALM_NEW) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001220 buffer_unmap(rd);
1221 granule_unlock(g_rd);
AlexeiFedorov960d1612023-04-25 13:23:39 +01001222 res->x[0] = RMI_ERROR_REALM;
1223 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001224 }
1225
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001226 s2_ctx = &(rd->s2_ctx);
1227 granule_lock(s2_ctx->g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001228
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001229 s2tt_walk_lock_unlock(s2_ctx, base, S2TT_PAGE_LEVEL, &wi);
AlexeiFedorov960d1612023-04-25 13:23:39 +01001230 level = wi.last_level;
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001231 s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001232 assert(s2tt != NULL);
1233
AlexeiFedorov960d1612023-04-25 13:23:39 +01001234 map_size = s2tte_map_size(level);
1235 addr = base & ~(map_size - 1UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001236
AlexeiFedorov960d1612023-04-25 13:23:39 +01001237 /*
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001238 * If the RTTE covers a range below "base", we need to go deeper.
AlexeiFedorov960d1612023-04-25 13:23:39 +01001239 */
1240 if (addr != base) {
1241 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001242 (unsigned char)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001243 goto out_unmap_llt;
1244 }
1245
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001246 s2ttes_per_s2tt =
1247 (unsigned int)((level == S2TT_MIN_STARTING_LEVEL_LPA2) ?
1248 S2TTES_PER_S2TT_LM1 : S2TTES_PER_S2TT);
1249 for (index = wi.index; index < s2ttes_per_s2tt; index++) {
AlexeiFedorov960d1612023-04-25 13:23:39 +01001250 unsigned long next = addr + map_size;
1251
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001252 /*
1253 * Break on "top_align" failure condition,
1254 * or if this entry crosses the range.
1255 */
AlexeiFedorov960d1612023-04-25 13:23:39 +01001256 if (next > top) {
1257 break;
1258 }
1259
1260 s2tte = s2tte_read(&s2tt[index]);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001261 if (s2tte_is_unassigned_empty(s2_ctx, s2tte)) {
1262 s2tte = s2tte_create_unassigned_ram(s2_ctx);
AlexeiFedorov960d1612023-04-25 13:23:39 +01001263 s2tte_write(&s2tt[index], s2tte);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001264 } else if (!s2tte_is_unassigned_ram(s2_ctx, s2tte)) {
AlexeiFedorov960d1612023-04-25 13:23:39 +01001265 break;
1266 }
Mate Toth-Palc7698312023-08-09 12:49:34 +02001267 measurement_init_ripas_measure(rd->measurement[RIM_MEASUREMENT_SLOT],
1268 rd->algorithm,
1269 addr,
1270 next);
AlexeiFedorovee2fc822023-10-31 14:54:39 +00001271 addr = next;
AlexeiFedorov960d1612023-04-25 13:23:39 +01001272 }
1273
1274 if (addr > base) {
1275 res->x[0] = RMI_SUCCESS;
1276 res->x[1] = addr;
1277 } else {
1278 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001279 (unsigned char)level);
AlexeiFedorov960d1612023-04-25 13:23:39 +01001280 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001281
1282out_unmap_llt:
1283 buffer_unmap(s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001284 buffer_unmap(rd);
1285 granule_unlock(wi.g_llt);
AlexeiFedorov80295e42023-07-10 13:11:14 +01001286 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001287}
1288
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +00001289static void rtt_set_ripas_range(struct s2tt_context *s2_ctx,
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001290 unsigned long *s2tt,
1291 unsigned long base,
1292 unsigned long top,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001293 struct s2tt_walk *wi,
AlexeiFedorov4faab852023-08-30 15:06:49 +01001294 enum ripas ripas_val,
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001295 enum ripas_change_destroyed change_destroyed,
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001296 struct smc_result *res)
1297{
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001298 unsigned long index;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001299 long level = wi->last_level;
AlexeiFedorov4faab852023-08-30 15:06:49 +01001300 unsigned long map_size = s2tte_map_size((int)level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001301
1302 /* Align to the RTT level */
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001303 unsigned long addr = base & ~(map_size - 1UL);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001304
1305 /* Make sure we don't touch a range below the requested range */
1306 if (addr != base) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +01001307 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001308 (unsigned char)level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001309 return;
1310 }
1311
AlexeiFedorovee2fc822023-10-31 14:54:39 +00001312 for (index = wi->index; index < S2TTES_PER_S2TT; index++) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001313 int ret;
1314
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001315 /*
1316 * Break on "top_align" failure condition,
1317 * or if this entry crosses the range.
1318 */
1319 if ((addr + map_size) > top) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001320 break;
1321 }
1322
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001323 ret = update_ripas(s2_ctx, &s2tt[index], level,
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001324 ripas_val, change_destroyed);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001325 if (ret < 0) {
1326 break;
1327 }
1328
1329 /* Handle TLBI */
1330 if (ret != 0) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001331 if (level == S2TT_PAGE_LEVEL) {
1332 s2tt_invalidate_page(s2_ctx, addr);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001333 } else {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001334 s2tt_invalidate_block(s2_ctx, addr);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001335 }
1336 }
AlexeiFedorovee2fc822023-10-31 14:54:39 +00001337
1338 addr += map_size;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001339 }
1340
1341 if (addr > base) {
1342 res->x[0] = RMI_SUCCESS;
1343 res->x[1] = addr;
1344 } else {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +01001345 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001346 (unsigned char)level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001347 }
1348}
1349
1350void smc_rtt_set_ripas(unsigned long rd_addr,
1351 unsigned long rec_addr,
1352 unsigned long base,
1353 unsigned long top,
1354 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001355{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001356 struct granule *g_rd, *g_rec;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001357 struct rec *rec;
1358 struct rd *rd;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001359 struct s2tt_walk wi;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001360 unsigned long *s2tt;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001361 struct s2tt_context *s2_ctx;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001362 enum ripas ripas_val;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001363 enum ripas_change_destroyed change_destroyed;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001364
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001365 if (top <= base) {
1366 res->x[0] = RMI_ERROR_INPUT;
1367 return;
1368 }
1369
Soby Mathewb4c6df42022-11-09 11:13:29 +00001370 if (!find_lock_two_granules(rd_addr,
1371 GRANULE_STATE_RD,
1372 &g_rd,
1373 rec_addr,
1374 GRANULE_STATE_REC,
1375 &g_rec)) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001376 res->x[0] = RMI_ERROR_INPUT;
1377 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001378 }
1379
AlexeiFedorovd6d93d82024-02-13 16:52:11 +00001380 if (granule_refcount_read_acquire(g_rec) != 0U) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001381 res->x[0] = RMI_ERROR_REC;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001382 goto out_unlock_rec_rd;
1383 }
1384
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001385 rec = buffer_granule_map(g_rec, SLOT_REC);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001386 assert(rec != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001387
1388 if (g_rd != rec->realm_info.g_rd) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001389 res->x[0] = RMI_ERROR_REC;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001390 goto out_unmap_rec;
1391 }
1392
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001393 ripas_val = rec->set_ripas.ripas_val;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001394 change_destroyed = rec->set_ripas.change_destroyed;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001395
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001396 /*
1397 * Return error in case of target region:
1398 * - is not the next chunk of requested region
1399 * - extends beyond the end of requested region
1400 */
1401 if ((base != rec->set_ripas.addr) || (top > rec->set_ripas.top)) {
1402 res->x[0] = RMI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001403 goto out_unmap_rec;
1404 }
1405
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001406 rd = buffer_granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001407 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001408
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001409 /*
1410 * At this point, we know base == rec->set_ripas.addr
1411 * and thus must be aligned to GRANULE size.
1412 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001413 assert(validate_map_addr(base, S2TT_PAGE_LEVEL, rd));
Soby Mathewb4c6df42022-11-09 11:13:29 +00001414
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001415 s2_ctx = &(rd->s2_ctx);
1416 granule_lock(s2_ctx->g_rtt, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001417
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001418 /* Walk to the deepest level possible */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001419 s2tt_walk_lock_unlock(s2_ctx, base, S2TT_PAGE_LEVEL, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001420
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001421 /*
1422 * Base has to be aligned to the level at which
1423 * it is mapped in RTT.
1424 */
1425 if (!validate_map_addr(base, wi.last_level, rd)) {
1426 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001427 (unsigned char)wi.last_level);
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001428 goto out_unlock_llt;
1429 }
1430
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00001431 s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001432 assert(s2tt != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001433
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001434 rtt_set_ripas_range(s2_ctx, s2tt, base, top, &wi,
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001435 ripas_val, change_destroyed, res);
1436
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001437 if (res->x[0] == RMI_SUCCESS) {
1438 rec->set_ripas.addr = res->x[1];
Soby Mathewb4c6df42022-11-09 11:13:29 +00001439 }
1440
Soby Mathewb4c6df42022-11-09 11:13:29 +00001441 buffer_unmap(s2tt);
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001442out_unlock_llt:
Soby Mathewb4c6df42022-11-09 11:13:29 +00001443 granule_unlock(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001444 buffer_unmap(rd);
1445out_unmap_rec:
1446 buffer_unmap(rec);
1447out_unlock_rec_rd:
1448 granule_unlock(g_rec);
1449 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001450}