blob: 87fb388cbf6628c3e07982e4e6887fac226c9765 [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
7#include <buffer.h>
8#include <granule.h>
9#include <measurement.h>
10#include <realm.h>
11#include <ripas.h>
12#include <smc-handler.h>
13#include <smc-rmi.h>
14#include <smc.h>
15#include <stddef.h>
16#include <string.h>
17#include <table.h>
18
19/*
20 * Validate the map_addr value passed to RMI_RTT_* and RMI_DATA_* commands.
21 */
22static bool validate_map_addr(unsigned long map_addr,
AlexeiFedorov4faab852023-08-30 15:06:49 +010023 long level,
Soby Mathewb4c6df42022-11-09 11:13:29 +000024 struct rd *rd)
25{
AlexeiFedorov14d47ae2023-07-19 15:26:50 +010026 return ((map_addr < realm_ipa_size(rd)) &&
27 addr_is_level_aligned(map_addr, level));
Soby Mathewb4c6df42022-11-09 11:13:29 +000028}
29
30/*
31 * Structure commands can operate on all RTTs except for the root RTT so
32 * the minimal valid level is the stage 2 starting level + 1.
33 */
34static bool validate_rtt_structure_cmds(unsigned long map_addr,
35 long level,
36 struct rd *rd)
37{
38 int min_level = realm_rtt_starting_level(rd) + 1;
39
40 if ((level < min_level) || (level > RTT_PAGE_LEVEL)) {
41 return false;
42 }
AlexeiFedorovf85f8102023-09-11 16:14:18 +010043 return validate_map_addr(map_addr, level - 1L, rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +000044}
45
46/*
47 * Map/Unmap commands can operate up to a level 2 block entry so min_level is
48 * the smallest block size.
49 */
50static bool validate_rtt_map_cmds(unsigned long map_addr,
51 long level,
52 struct rd *rd)
53{
54 if ((level < RTT_MIN_BLOCK_LEVEL) || (level > RTT_PAGE_LEVEL)) {
55 return false;
56 }
57 return validate_map_addr(map_addr, level, rd);
58}
59
60/*
61 * Entry commands can operate on any entry so the minimal valid level is the
62 * stage 2 starting level.
63 */
64static bool validate_rtt_entry_cmds(unsigned long map_addr,
65 long level,
66 struct rd *rd)
67{
68 if ((level < realm_rtt_starting_level(rd)) ||
69 (level > RTT_PAGE_LEVEL)) {
70 return false;
71 }
72 return validate_map_addr(map_addr, level, rd);
73}
74
AlexeiFedorovac923c82023-04-06 15:12:04 +010075unsigned long smc_rtt_create(unsigned long rd_addr,
76 unsigned long rtt_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +000077 unsigned long map_addr,
78 unsigned long ulevel)
79{
80 struct granule *g_rd;
81 struct granule *g_tbl;
82 struct rd *rd;
83 struct granule *g_table_root;
84 struct rtt_walk wi;
85 unsigned long *s2tt, *parent_s2tt, parent_s2tte;
86 long level = (long)ulevel;
87 unsigned long ipa_bits;
88 unsigned long ret;
89 struct realm_s2_context s2_ctx;
90 int sl;
91
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
101 rd = 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
111 g_table_root = rd->s2_ctx.g_rtt;
112 sl = realm_rtt_starting_level(rd);
113 ipa_bits = realm_ipa_bits(rd);
114 s2_ctx = rd->s2_ctx;
115 buffer_unmap(rd);
116
117 /*
118 * Lock the RTT root. Enforcing locking order RD->RTT is enough to
119 * ensure deadlock free locking guarentee.
120 */
121 granule_lock(g_table_root, GRANULE_STATE_RTT);
122
123 /* Unlock RD after locking RTT Root */
124 granule_unlock(g_rd);
125
126 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
127 map_addr, level - 1L, &wi);
128 if (wi.last_level != level - 1L) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100129 ret = pack_return_code(RMI_ERROR_RTT,
130 (unsigned int)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000131 goto out_unlock_llt;
132 }
133
134 parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100135 assert(parent_s2tt != NULL);
136
Soby Mathewb4c6df42022-11-09 11:13:29 +0000137 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
138 s2tt = granule_map(g_tbl, SLOT_DELEGATED);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100139 assert(s2tt != NULL);
140
Soby Mathewb4c6df42022-11-09 11:13:29 +0000141
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100142 if (s2tte_is_unassigned_empty(parent_s2tte)) {
143 s2tt_init_unassigned_empty(s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000144
145 /*
146 * Increase the refcount of the parent, the granule was
147 * locked while table walking and hand-over-hand locking.
148 * Atomicity and acquire/release semantics not required because
149 * the table is accessed always locked.
150 */
151 __granule_get(wi.g_llt);
152
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100153 } else if (s2tte_is_unassigned_ram(parent_s2tte)) {
154 s2tt_init_unassigned_ram(s2tt);
155 __granule_get(wi.g_llt);
156
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100157 } else if (s2tte_is_unassigned_ns(parent_s2tte)) {
158 s2tt_init_unassigned_ns(s2tt);
159 __granule_get(wi.g_llt);
160
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100161 } else if (s2tte_is_unassigned_destroyed(parent_s2tte)) {
162 s2tt_init_unassigned_destroyed(s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000163 __granule_get(wi.g_llt);
164
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100165 } else if (s2tte_is_assigned_destroyed(parent_s2tte, level - 1L)) {
166 unsigned long block_pa;
167
168 /*
169 * We should observe parent assigned s2tte only when
170 * we create tables above this level.
171 */
172 assert(level > RTT_MIN_BLOCK_LEVEL);
173
174 block_pa = s2tte_pa(parent_s2tte, level - 1L);
175
176 s2tt_init_assigned_destroyed(s2tt, block_pa, level);
177
178 /*
179 * Increase the refcount to mark the granule as in-use. refcount
180 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
181 */
182 __granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
183
AlexeiFedorov3a739332023-04-13 13:54:04 +0100184 } else if (s2tte_is_assigned_empty(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000185 unsigned long block_pa;
186
187 /*
188 * We should observe parent assigned s2tte only when
189 * we create tables above this level.
190 */
191 assert(level > RTT_MIN_BLOCK_LEVEL);
192
193 block_pa = s2tte_pa(parent_s2tte, level - 1L);
194
195 s2tt_init_assigned_empty(s2tt, block_pa, level);
196
197 /*
198 * Increase the refcount to mark the granule as in-use. refcount
199 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
200 */
201 __granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
202
AlexeiFedorov3a739332023-04-13 13:54:04 +0100203 } else if (s2tte_is_assigned_ram(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000204 unsigned long block_pa;
205
206 /*
207 * We should observe parent valid s2tte only when
208 * we create tables above this level.
209 */
210 assert(level > RTT_MIN_BLOCK_LEVEL);
211
212 /*
213 * Break before make. This may cause spurious S2 aborts.
214 */
215 s2tte_write(&parent_s2tt[wi.index], 0UL);
216 invalidate_block(&s2_ctx, map_addr);
217
218 block_pa = s2tte_pa(parent_s2tte, level - 1L);
219
AlexeiFedorov3a739332023-04-13 13:54:04 +0100220 s2tt_init_assigned_ram(s2tt, block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000221
222 /*
223 * Increase the refcount to mark the granule as in-use. refcount
224 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
225 */
226 __granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
227
AlexeiFedorov3a739332023-04-13 13:54:04 +0100228 } else if (s2tte_is_assigned_ns(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000229 unsigned long block_pa;
230
231 /*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100232 * We should observe parent assigned_ns s2tte only when
Soby Mathewb4c6df42022-11-09 11:13:29 +0000233 * we create tables above this level.
234 */
235 assert(level > RTT_MIN_BLOCK_LEVEL);
236
237 /*
238 * Break before make. This may cause spurious S2 aborts.
239 */
240 s2tte_write(&parent_s2tt[wi.index], 0UL);
241 invalidate_block(&s2_ctx, map_addr);
242
243 block_pa = s2tte_pa(parent_s2tte, level - 1L);
244
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100245 s2tt_init_assigned_ns(s2tt, parent_s2tte, block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000246
247 /*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100248 * Increment the refcount on the parent for the new RTT we are
249 * about to add. The NS block entry doesn't have a refcount
250 * on the parent RTT.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000251 */
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100252 __granule_get(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000253
254 } else if (s2tte_is_table(parent_s2tte, level - 1L)) {
255 ret = pack_return_code(RMI_ERROR_RTT,
256 (unsigned int)(level - 1L));
257 goto out_unmap_table;
258
259 } else {
260 assert(false);
261 }
262
263 ret = RMI_SUCCESS;
264
265 granule_set_state(g_tbl, GRANULE_STATE_RTT);
266
267 parent_s2tte = s2tte_create_table(rtt_addr, level - 1L);
268 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
269
270out_unmap_table:
271 buffer_unmap(s2tt);
272 buffer_unmap(parent_s2tt);
273out_unlock_llt:
274 granule_unlock(wi.g_llt);
275 granule_unlock(g_tbl);
276 return ret;
277}
278
AlexeiFedorove2002be2023-04-19 17:20:12 +0100279void smc_rtt_fold(unsigned long rd_addr,
280 unsigned long map_addr,
281 unsigned long ulevel,
282 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000283{
284 struct granule *g_rd;
285 struct granule *g_tbl;
286 struct rd *rd;
287 struct granule *g_table_root;
288 struct rtt_walk wi;
289 unsigned long *table, *parent_s2tt, parent_s2tte;
290 long level = (long)ulevel;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100291 unsigned long ipa_bits, rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000292 unsigned long ret;
293 struct realm_s2_context s2_ctx;
294 int sl;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000295
296 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
297 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100298 res->x[0] = RMI_ERROR_INPUT;
299 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000300 }
301
302 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100303 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000304
305 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
306 buffer_unmap(rd);
307 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100308 res->x[0] = RMI_ERROR_INPUT;
309 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000310 }
311
312 g_table_root = rd->s2_ctx.g_rtt;
313 sl = realm_rtt_starting_level(rd);
314 ipa_bits = realm_ipa_bits(rd);
315 s2_ctx = rd->s2_ctx;
316 buffer_unmap(rd);
317 granule_lock(g_table_root, GRANULE_STATE_RTT);
318 granule_unlock(g_rd);
319
320 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
321 map_addr, level - 1L, &wi);
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100322 if (wi.last_level != level - 1L) {
323 ret = pack_return_code(RMI_ERROR_RTT,
324 (unsigned int)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000325 goto out_unlock_parent_table;
326 }
327
328 parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100329 assert(parent_s2tt != NULL);
330
Soby Mathewb4c6df42022-11-09 11:13:29 +0000331 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
332 if (!s2tte_is_table(parent_s2tte, level - 1L)) {
333 ret = pack_return_code(RMI_ERROR_RTT,
334 (unsigned int)(level - 1L));
335 goto out_unmap_parent_table;
336 }
337
AlexeiFedorove2002be2023-04-19 17:20:12 +0100338 rtt_addr = s2tte_pa_table(parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000339 g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT);
340
341 /*
342 * A table descriptor S2TTE always points to a TABLE granule.
343 */
AlexeiFedorov63b71692023-04-19 11:18:42 +0100344 assert(g_tbl != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000345
346 table = granule_map(g_tbl, SLOT_RTT2);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100347 assert(table != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000348
349 /*
350 * The command can succeed only if all 512 S2TTEs are of the same type.
351 * We first check the table's ref. counter to speed up the case when
352 * the host makes a guess whether a memory region can be folded.
353 */
354 if (g_tbl->refcount == 0UL) {
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100355 if (table_is_unassigned_destroyed_block(table)) {
356 parent_s2tte = s2tte_create_unassigned_destroyed();
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100357 } else if (table_is_unassigned_empty_block(table)) {
358 parent_s2tte = s2tte_create_unassigned_empty();
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100359 } else if (table_is_unassigned_ram_block(table)) {
360 parent_s2tte = s2tte_create_unassigned_ram();
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100361 } else if (table_is_unassigned_ns_block(table)) {
362 parent_s2tte = s2tte_create_unassigned_ns();
AlexeiFedorov49752c62023-04-24 14:31:14 +0100363 } else if (table_maps_assigned_ns_block(table, level)) {
364 unsigned long s2tte = s2tte_read(&table[0]);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100365
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100366 /*
367 * Since table_maps_assigned_ns_block() has succedded,
368 * the PA in first entry of the table is aligned at
369 * parent level. Use the TTE from the first entry
370 * directly as it also has the NS attributes to be used
371 * for the parent block entry.
372 */
373 parent_s2tte = s2tte_create_assigned_ns(s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000374 } else {
375 /*
376 * The table holds a mixture of destroyed and
377 * unassigned entries.
378 */
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100379 ret = pack_return_code(RMI_ERROR_RTT,
380 (unsigned int)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000381 goto out_unmap_table;
382 }
AlexeiFedorov49752c62023-04-24 14:31:14 +0100383 __granule_put(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000384 } else if (g_tbl->refcount == S2TTES_PER_S2TT) {
385
386 unsigned long s2tte, block_pa;
387
388 /* The RMM specification does not allow creating block
389 * entries less than RTT_MIN_BLOCK_LEVEL even though
390 * permitted by the Arm Architecture.
391 * Hence ensure that the table being folded is at a level
392 * higher than the RTT_MIN_BLOCK_LEVEL.
393 *
394 * A fully populated table cannot be destroyed if that
395 * would create a block mapping below RTT_MIN_BLOCK_LEVEL.
396 */
397 if (level <= RTT_MIN_BLOCK_LEVEL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100398 ret = pack_return_code(RMI_ERROR_RTT,
399 (unsigned int)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000400 goto out_unmap_table;
401 }
402
403 s2tte = s2tte_read(&table[0]);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000404 block_pa = s2tte_pa(s2tte, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000405
406 /*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100407 * The table must also refer to a contiguous block through the
AlexeiFedorov3f840a02023-07-19 10:55:05 +0100408 * same type of s2tte, either Assigned or Valid.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000409 */
AlexeiFedorov3a739332023-04-13 13:54:04 +0100410 if (table_maps_assigned_empty_block(table, level)) {
411 parent_s2tte = s2tte_create_assigned_empty(block_pa,
412 level - 1L);
413 } else if (table_maps_assigned_ram_block(table, level)) {
414 parent_s2tte = s2tte_create_assigned_ram(block_pa,
415 level - 1L);
AlexeiFedorov3f840a02023-07-19 10:55:05 +0100416 } else if (table_maps_assigned_destroyed_block(table, level)) {
417 parent_s2tte = s2tte_create_assigned_destroyed(block_pa,
418 level - 1L);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000419 /* The table contains mixed entries that cannot be folded */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000420 } else {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100421 ret = pack_return_code(RMI_ERROR_RTT,
422 (unsigned int)level);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000423 goto out_unmap_table;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000424 }
425
426 __granule_refcount_dec(g_tbl, S2TTES_PER_S2TT);
427 } else {
428 /*
429 * The table holds a mixture of different types of s2ttes.
430 */
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100431 ret = pack_return_code(RMI_ERROR_RTT,
432 (unsigned int)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000433 goto out_unmap_table;
434 }
435
436 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100437 res->x[1] = rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000438
439 /*
440 * Break before make.
441 */
442 s2tte_write(&parent_s2tt[wi.index], 0UL);
443
AlexeiFedorov3a739332023-04-13 13:54:04 +0100444 if (s2tte_is_assigned_ram(parent_s2tte, level - 1L) ||
445 s2tte_is_assigned_ns(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000446 invalidate_pages_in_block(&s2_ctx, map_addr);
447 } else {
448 invalidate_block(&s2_ctx, map_addr);
449 }
450
451 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
452
453 granule_memzero_mapped(table);
454 granule_set_state(g_tbl, GRANULE_STATE_DELEGATED);
455
456out_unmap_table:
457 buffer_unmap(table);
458 granule_unlock(g_tbl);
459out_unmap_parent_table:
460 buffer_unmap(parent_s2tt);
461out_unlock_parent_table:
462 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100463 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000464}
465
AlexeiFedorove2002be2023-04-19 17:20:12 +0100466void smc_rtt_destroy(unsigned long rd_addr,
467 unsigned long map_addr,
468 unsigned long ulevel,
469 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000470{
471 struct granule *g_rd;
472 struct granule *g_tbl;
473 struct rd *rd;
474 struct granule *g_table_root;
475 struct rtt_walk wi;
476 unsigned long *table, *parent_s2tt, parent_s2tte;
477 long level = (long)ulevel;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100478 unsigned long ipa_bits, rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000479 unsigned long ret;
480 struct realm_s2_context s2_ctx;
481 int sl;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100482 bool in_par, skip_non_live = false;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000483
484 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
485 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100486 res->x[0] = RMI_ERROR_INPUT;
AlexeiFedorov3ebd4622023-07-18 16:27:39 +0100487 res->x[2] = 0UL;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100488 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000489 }
490
491 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100492 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000493
494 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
495 buffer_unmap(rd);
496 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100497 res->x[0] = RMI_ERROR_INPUT;
AlexeiFedorov3ebd4622023-07-18 16:27:39 +0100498 res->x[2] = 0UL;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100499 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000500 }
501
502 g_table_root = rd->s2_ctx.g_rtt;
503 sl = realm_rtt_starting_level(rd);
504 ipa_bits = realm_ipa_bits(rd);
505 s2_ctx = rd->s2_ctx;
506 in_par = addr_in_par(rd, map_addr);
507 buffer_unmap(rd);
508 granule_lock(g_table_root, GRANULE_STATE_RTT);
509 granule_unlock(g_rd);
510
511 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
512 map_addr, level - 1L, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000513
514 parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100515 assert(parent_s2tt != NULL);
516
Soby Mathewb4c6df42022-11-09 11:13:29 +0000517 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100518
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100519 if ((wi.last_level != level - 1L) ||
520 !s2tte_is_table(parent_s2tte, level - 1L)) {
521 ret = pack_return_code(RMI_ERROR_RTT,
522 (unsigned int)wi.last_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100523 skip_non_live = true;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000524 goto out_unmap_parent_table;
525 }
526
AlexeiFedorove2002be2023-04-19 17:20:12 +0100527 rtt_addr = s2tte_pa_table(parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000528
529 /*
530 * Lock the RTT granule. The 'rtt_addr' is verified, thus can be treated
531 * as an internal granule.
532 */
533 g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT);
534
535 /*
536 * A table descriptor S2TTE always points to a TABLE granule.
537 */
538 assert(g_tbl != NULL);
539
540 /*
541 * Read the refcount value. RTT granule is always accessed locked, thus
542 * the refcount can be accessed without atomic operations.
543 */
544 if (g_tbl->refcount != 0UL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100545 ret = pack_return_code(RMI_ERROR_RTT, (unsigned int)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000546 goto out_unlock_table;
547 }
548
549 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100550 res->x[1] = rtt_addr;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100551 skip_non_live = true;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000552
553 table = granule_map(g_tbl, SLOT_RTT2);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100554 assert(table != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000555
556 if (in_par) {
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100557 parent_s2tte = s2tte_create_unassigned_destroyed();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000558 } else {
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100559 parent_s2tte = s2tte_create_unassigned_ns();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000560 }
561
562 __granule_put(wi.g_llt);
563
564 /*
565 * Break before make. Note that this may cause spurious S2 aborts.
566 */
567 s2tte_write(&parent_s2tt[wi.index], 0UL);
568 invalidate_block(&s2_ctx, map_addr);
569 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
570
571 granule_memzero_mapped(table);
572 granule_set_state(g_tbl, GRANULE_STATE_DELEGATED);
573
574 buffer_unmap(table);
575out_unlock_table:
576 granule_unlock(g_tbl);
577out_unmap_parent_table:
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100578 if (skip_non_live) {
579 res->x[2] = skip_non_live_entries(map_addr, parent_s2tt, &wi);
AlexeiFedorov3ebd4622023-07-18 16:27:39 +0100580 } else {
581 res->x[2] = map_addr;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100582 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000583 buffer_unmap(parent_s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000584 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100585 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000586}
587
588enum map_unmap_ns_op {
589 MAP_NS,
590 UNMAP_NS
591};
592
593/*
594 * We don't hold a reference on the NS granule when it is
595 * mapped into a realm. Instead we rely on the guarantees
596 * provided by the architecture to ensure that a NS access
597 * to a protected granule is prohibited even within the realm.
598 */
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100599static void map_unmap_ns(unsigned long rd_addr,
600 unsigned long map_addr,
601 long level,
602 unsigned long host_s2tte,
603 enum map_unmap_ns_op op,
604 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000605{
606 struct granule *g_rd;
607 struct rd *rd;
608 struct granule *g_table_root;
609 unsigned long *s2tt, s2tte;
610 struct rtt_walk wi;
611 unsigned long ipa_bits;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000612 struct realm_s2_context s2_ctx;
613 int sl;
614
615 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
616 if (g_rd == NULL) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100617 res->x[0] = RMI_ERROR_INPUT;
618 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000619 }
620
621 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100622 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000623
624 if (!validate_rtt_map_cmds(map_addr, level, rd)) {
625 buffer_unmap(rd);
626 granule_unlock(g_rd);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100627 res->x[0] = RMI_ERROR_INPUT;
628 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000629 }
630
631 g_table_root = rd->s2_ctx.g_rtt;
632 sl = realm_rtt_starting_level(rd);
633 ipa_bits = realm_ipa_bits(rd);
634
AlexeiFedorovc34e3242023-04-12 11:30:33 +0100635 /* Check if map_addr is outside PAR */
636 if (addr_in_par(rd, map_addr)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000637 buffer_unmap(rd);
638 granule_unlock(g_rd);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100639 res->x[0] = RMI_ERROR_INPUT;
640 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000641 }
642
643 s2_ctx = rd->s2_ctx;
644 buffer_unmap(rd);
645
646 granule_lock(g_table_root, GRANULE_STATE_RTT);
647 granule_unlock(g_rd);
648
649 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
650 map_addr, level, &wi);
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100651
652 /*
653 * For UNMAP_NS, we need to map the table and look
654 * for the end of the non-live region.
655 */
AlexeiFedorov14d47ae2023-07-19 15:26:50 +0100656 if ((op == MAP_NS) && (wi.last_level != level)) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100657 res->x[0] = pack_return_code(RMI_ERROR_RTT,
658 (unsigned int)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000659 goto out_unlock_llt;
660 }
661
662 s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100663 assert(s2tt != NULL);
664
Soby Mathewb4c6df42022-11-09 11:13:29 +0000665 s2tte = s2tte_read(&s2tt[wi.index]);
666
667 if (op == MAP_NS) {
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100668 if (!s2tte_is_unassigned_ns(s2tte)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100669 res->x[0] = pack_return_code(RMI_ERROR_RTT,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000670 (unsigned int)level);
671 goto out_unmap_table;
672 }
673
AlexeiFedorov3a739332023-04-13 13:54:04 +0100674 s2tte = s2tte_create_assigned_ns(host_s2tte, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000675 s2tte_write(&s2tt[wi.index], s2tte);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000676
677 } else if (op == UNMAP_NS) {
678 /*
679 * The following check also verifies that map_addr is outside
680 * PAR, as valid_NS s2tte may only cover outside PAR IPA range.
681 */
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100682 bool assigned_ns = s2tte_is_assigned_ns(s2tte, wi.last_level);
683
684 if ((wi.last_level != level) || !assigned_ns) {
685 res->x[0] = pack_return_code(RMI_ERROR_RTT,
686 (unsigned int)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000687 goto out_unmap_table;
688 }
689
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100690 s2tte = s2tte_create_unassigned_ns();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000691 s2tte_write(&s2tt[wi.index], s2tte);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000692 if (level == RTT_PAGE_LEVEL) {
693 invalidate_page(&s2_ctx, map_addr);
694 } else {
695 invalidate_block(&s2_ctx, map_addr);
696 }
697 }
698
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100699 res->x[0] = RMI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000700
701out_unmap_table:
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100702 if (op == UNMAP_NS) {
703 res->x[1] = skip_non_live_entries(map_addr, s2tt, &wi);
704 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000705 buffer_unmap(s2tt);
706out_unlock_llt:
707 granule_unlock(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000708}
709
710unsigned long smc_rtt_map_unprotected(unsigned long rd_addr,
711 unsigned long map_addr,
712 unsigned long ulevel,
713 unsigned long s2tte)
714{
715 long level = (long)ulevel;
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100716 struct smc_result res;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000717
AlexeiFedorov7a2f5882023-09-14 11:41:32 +0100718 if ((level < RTT_MIN_BLOCK_LEVEL) || (level > RTT_PAGE_LEVEL)) {
719 return RMI_ERROR_INPUT;
720 }
721
Soby Mathewb4c6df42022-11-09 11:13:29 +0000722 if (!host_ns_s2tte_is_valid(s2tte, level)) {
723 return RMI_ERROR_INPUT;
724 }
725
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100726 map_unmap_ns(rd_addr, map_addr, level, s2tte, MAP_NS, &res);
727 return res.x[0];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000728}
729
AlexeiFedorov917eabf2023-04-24 12:20:41 +0100730void smc_rtt_unmap_unprotected(unsigned long rd_addr,
731 unsigned long map_addr,
732 unsigned long ulevel,
733 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000734{
AlexeiFedorov7a2f5882023-09-14 11:41:32 +0100735 long level = (long)ulevel;
736
737 if ((level < RTT_MIN_BLOCK_LEVEL) || (level > RTT_PAGE_LEVEL)) {
738 res->x[0] = RMI_ERROR_INPUT;
739 return;
740 }
741
742 map_unmap_ns(rd_addr, map_addr, level, 0UL, UNMAP_NS, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000743}
744
745void smc_rtt_read_entry(unsigned long rd_addr,
746 unsigned long map_addr,
747 unsigned long ulevel,
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100748 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000749{
750 struct granule *g_rd, *g_rtt_root;
751 struct rd *rd;
752 struct rtt_walk wi;
753 unsigned long *s2tt, s2tte;
754 unsigned long ipa_bits;
755 long level = (long)ulevel;
756 int sl;
757
758 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
759 if (g_rd == NULL) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100760 res->x[0] = RMI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000761 return;
762 }
763
764 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100765 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000766
767 if (!validate_rtt_entry_cmds(map_addr, level, rd)) {
768 buffer_unmap(rd);
769 granule_unlock(g_rd);
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100770 res->x[0] = RMI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000771 return;
772 }
773
774 g_rtt_root = rd->s2_ctx.g_rtt;
775 sl = realm_rtt_starting_level(rd);
776 ipa_bits = realm_ipa_bits(rd);
777 buffer_unmap(rd);
778
779 granule_lock(g_rtt_root, GRANULE_STATE_RTT);
780 granule_unlock(g_rd);
781
782 rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits,
783 map_addr, level, &wi);
784 s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100785 assert(s2tt != NULL);
786
Soby Mathewb4c6df42022-11-09 11:13:29 +0000787 s2tte = s2tte_read(&s2tt[wi.index]);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100788 res->x[1] = (unsigned long)wi.last_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000789
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100790 if (s2tte_is_unassigned_empty(s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100791 res->x[2] = RMI_UNASSIGNED;
792 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100793 res->x[4] = (unsigned long)RIPAS_EMPTY;
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100794 } else if (s2tte_is_unassigned_ram(s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100795 res->x[2] = RMI_UNASSIGNED;
796 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100797 res->x[4] = (unsigned long)RIPAS_RAM;
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100798 } else if (s2tte_is_unassigned_destroyed(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_DESTROYED;
AlexeiFedorov3a739332023-04-13 13:54:04 +0100802 } else if (s2tte_is_assigned_empty(s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100803 res->x[2] = RMI_ASSIGNED;
804 res->x[3] = s2tte_pa(s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100805 res->x[4] = (unsigned long)RIPAS_EMPTY;
AlexeiFedorov3a739332023-04-13 13:54:04 +0100806 } else if (s2tte_is_assigned_ram(s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100807 res->x[2] = RMI_ASSIGNED;
808 res->x[3] = s2tte_pa(s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100809 res->x[4] = (unsigned long)RIPAS_RAM;
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100810 } else if (s2tte_is_assigned_destroyed(s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100811 res->x[2] = RMI_ASSIGNED;
812 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100813 res->x[4] = (unsigned long)RIPAS_DESTROYED;
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100814 } else if (s2tte_is_unassigned_ns(s2tte)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100815 res->x[2] = RMI_UNASSIGNED;
816 res->x[3] = 0UL;
AlexeiFedorov4faab852023-08-30 15:06:49 +0100817 res->x[4] = (unsigned long)RIPAS_EMPTY;
AlexeiFedorov3a739332023-04-13 13:54:04 +0100818 } else if (s2tte_is_assigned_ns(s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100819 res->x[2] = RMI_ASSIGNED;
820 res->x[3] = host_ns_s2tte(s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100821 res->x[4] = (unsigned long)RIPAS_EMPTY;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000822 } else if (s2tte_is_table(s2tte, wi.last_level)) {
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100823 res->x[2] = RMI_TABLE;
824 res->x[3] = s2tte_pa_table(s2tte, wi.last_level);
AlexeiFedorov4faab852023-08-30 15:06:49 +0100825 res->x[4] = (unsigned long)RIPAS_EMPTY;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000826 } else {
827 assert(false);
828 }
829
830 buffer_unmap(s2tt);
831 granule_unlock(wi.g_llt);
832
AlexeiFedorov64fd6c32023-07-20 12:33:00 +0100833 res->x[0] = RMI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000834}
835
Soby Mathewb4c6df42022-11-09 11:13:29 +0000836static unsigned long validate_data_create_unknown(unsigned long map_addr,
837 struct rd *rd)
838{
839 if (!addr_in_par(rd, map_addr)) {
840 return RMI_ERROR_INPUT;
841 }
842
843 if (!validate_map_addr(map_addr, RTT_PAGE_LEVEL, rd)) {
844 return RMI_ERROR_INPUT;
845 }
846
847 return RMI_SUCCESS;
848}
849
850static unsigned long validate_data_create(unsigned long map_addr,
851 struct rd *rd)
852{
853 if (get_rd_state_locked(rd) != REALM_STATE_NEW) {
854 return RMI_ERROR_REALM;
855 }
856
857 return validate_data_create_unknown(map_addr, rd);
858}
859
860/*
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100861 * Implements both RMI_DATA_CREATE and RMI_DATA_CREATE_UNKNOWN
Soby Mathewb4c6df42022-11-09 11:13:29 +0000862 *
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100863 * if @g_src == NULL, implements RMI_DATA_CREATE_UNKNOWN
864 * and RMI_DATA_CREATE otherwise.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000865 */
AlexeiFedorovac923c82023-04-06 15:12:04 +0100866static unsigned long data_create(unsigned long rd_addr,
867 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000868 unsigned long map_addr,
869 struct granule *g_src,
870 unsigned long flags)
871{
872 struct granule *g_data;
873 struct granule *g_rd;
874 struct granule *g_table_root;
875 struct rd *rd;
876 struct rtt_walk wi;
877 unsigned long s2tte, *s2tt;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000878 enum granule_state new_data_state = GRANULE_STATE_DELEGATED;
879 unsigned long ipa_bits;
880 unsigned long ret;
881 int __unused meas_ret;
882 int sl;
883
884 if (!find_lock_two_granules(data_addr,
885 GRANULE_STATE_DELEGATED,
886 &g_data,
887 rd_addr,
888 GRANULE_STATE_RD,
889 &g_rd)) {
890 return RMI_ERROR_INPUT;
891 }
892
893 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100894 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000895
896 ret = (g_src != NULL) ?
897 validate_data_create(map_addr, rd) :
898 validate_data_create_unknown(map_addr, rd);
899
900 if (ret != RMI_SUCCESS) {
901 goto out_unmap_rd;
902 }
903
904 g_table_root = rd->s2_ctx.g_rtt;
905 sl = realm_rtt_starting_level(rd);
906 ipa_bits = realm_ipa_bits(rd);
907 granule_lock(g_table_root, GRANULE_STATE_RTT);
908 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
909 map_addr, RTT_PAGE_LEVEL, &wi);
910 if (wi.last_level != RTT_PAGE_LEVEL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +0100911 ret = pack_return_code(RMI_ERROR_RTT,
912 (unsigned int)wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000913 goto out_unlock_ll_table;
914 }
915
916 s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100917 assert(s2tt != NULL);
918
Soby Mathewb4c6df42022-11-09 11:13:29 +0000919 s2tte = s2tte_read(&s2tt[wi.index]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000920
Soby Mathewb4c6df42022-11-09 11:13:29 +0000921 if (g_src != NULL) {
922 bool ns_access_ok;
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100923 void *data;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000924
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100925 if (!s2tte_is_unassigned_ram(s2tte)) {
926 ret = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL);
927 goto out_unmap_ll_table;
928 }
929
930 data = granule_map(g_data, SLOT_DELEGATED);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100931 assert(data != NULL);
932
Soby Mathewb4c6df42022-11-09 11:13:29 +0000933 ns_access_ok = ns_buffer_read(SLOT_NS, g_src, 0U,
934 GRANULE_SIZE, data);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000935 if (!ns_access_ok) {
936 /*
937 * Some data may be copied before the failure. Zero
938 * g_data granule as it will remain in delegated state.
939 */
940 (void)memset(data, 0, GRANULE_SIZE);
941 buffer_unmap(data);
942 ret = RMI_ERROR_INPUT;
943 goto out_unmap_ll_table;
944 }
945
Mate Toth-Palc7698312023-08-09 12:49:34 +0200946 measurement_data_granule_measure(
947 rd->measurement[RIM_MEASUREMENT_SLOT],
948 rd->algorithm,
949 data,
950 map_addr,
951 flags);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000952 buffer_unmap(data);
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100953
954 } else if (!s2tte_is_unassigned(s2tte)) {
955 ret = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL);
956 goto out_unmap_ll_table;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000957 }
958
959 new_data_state = GRANULE_STATE_DATA;
960
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100961 s2tte = s2tte_create_assigned_unchanged(s2tte, data_addr, RTT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000962
963 s2tte_write(&s2tt[wi.index], s2tte);
964 __granule_get(wi.g_llt);
965
966 ret = RMI_SUCCESS;
967
968out_unmap_ll_table:
969 buffer_unmap(s2tt);
970out_unlock_ll_table:
971 granule_unlock(wi.g_llt);
972out_unmap_rd:
973 buffer_unmap(rd);
974 granule_unlock(g_rd);
975 granule_unlock_transition(g_data, new_data_state);
976 return ret;
977}
978
AlexeiFedorovac923c82023-04-06 15:12:04 +0100979unsigned long smc_data_create(unsigned long rd_addr,
980 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000981 unsigned long map_addr,
982 unsigned long src_addr,
983 unsigned long flags)
984{
985 struct granule *g_src;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000986
AlexeiFedorov93f5ec52023-08-31 14:26:53 +0100987 if ((flags != RMI_NO_MEASURE_CONTENT) &&
988 (flags != RMI_MEASURE_CONTENT)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000989 return RMI_ERROR_INPUT;
990 }
991
992 g_src = find_granule(src_addr);
993 if ((g_src == NULL) || (g_src->state != GRANULE_STATE_NS)) {
994 return RMI_ERROR_INPUT;
995 }
996
AlexeiFedorovac923c82023-04-06 15:12:04 +0100997 return data_create(rd_addr, data_addr, map_addr, g_src, flags);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000998}
999
AlexeiFedorovac923c82023-04-06 15:12:04 +01001000unsigned long smc_data_create_unknown(unsigned long rd_addr,
1001 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +00001002 unsigned long map_addr)
1003{
AlexeiFedorovac923c82023-04-06 15:12:04 +01001004 return data_create(rd_addr, data_addr, map_addr, NULL, 0);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001005}
1006
AlexeiFedorove2002be2023-04-19 17:20:12 +01001007void smc_data_destroy(unsigned long rd_addr,
1008 unsigned long map_addr,
1009 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001010{
1011 struct granule *g_data;
1012 struct granule *g_rd;
1013 struct granule *g_table_root;
1014 struct rtt_walk wi;
1015 unsigned long data_addr, s2tte, *s2tt;
1016 struct rd *rd;
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001017 unsigned long ipa_bits;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001018 struct realm_s2_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001019 int sl;
1020
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
1028 rd = 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) ||
1032 !validate_map_addr(map_addr, RTT_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
1040 g_table_root = rd->s2_ctx.g_rtt;
1041 sl = realm_rtt_starting_level(rd);
1042 ipa_bits = realm_ipa_bits(rd);
1043 s2_ctx = rd->s2_ctx;
1044 buffer_unmap(rd);
1045
1046 granule_lock(g_table_root, GRANULE_STATE_RTT);
1047 granule_unlock(g_rd);
1048
1049 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
1050 map_addr, RTT_PAGE_LEVEL, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001051
1052 s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001053 assert(s2tt != NULL);
1054
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001055 if (wi.last_level != RTT_PAGE_LEVEL) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +01001056 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1057 (unsigned int)wi.last_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001058 goto out_unmap_ll_table;
1059 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001060
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001061 s2tte = s2tte_read(&s2tt[wi.index]);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +01001062
AlexeiFedorova43cd312023-04-17 11:42:25 +01001063 if (s2tte_is_assigned_ram(s2tte, RTT_PAGE_LEVEL)) {
1064 data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +01001065 s2tte = s2tte_create_unassigned_destroyed();
AlexeiFedorova43cd312023-04-17 11:42:25 +01001066 s2tte_write(&s2tt[wi.index], s2tte);
1067 invalidate_page(&s2_ctx, map_addr);
1068 } else if (s2tte_is_assigned_empty(s2tte, RTT_PAGE_LEVEL)) {
1069 data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL);
1070 s2tte = s2tte_create_unassigned_empty();
1071 s2tte_write(&s2tt[wi.index], s2tte);
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +01001072 } else if (s2tte_is_assigned_destroyed(s2tte, RTT_PAGE_LEVEL)) {
1073 data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL);
1074 s2tte = s2tte_create_unassigned_destroyed();
1075 s2tte_write(&s2tt[wi.index], s2tte);
AlexeiFedorova43cd312023-04-17 11:42:25 +01001076 } else {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001077 res->x[0] = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001078 goto out_unmap_ll_table;
1079 }
1080
Soby Mathewb4c6df42022-11-09 11:13:29 +00001081 __granule_put(wi.g_llt);
1082
1083 /*
1084 * Lock the data granule and check expected state. Correct locking order
1085 * is guaranteed because granule address is obtained from a locked
1086 * granule by table walk. This lock needs to be acquired before a state
1087 * transition to or from GRANULE_STATE_DATA for granule address can happen.
1088 */
1089 g_data = find_lock_granule(data_addr, GRANULE_STATE_DATA);
AlexeiFedorov63b71692023-04-19 11:18:42 +01001090 assert(g_data != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001091 granule_memzero(g_data, SLOT_DELEGATED);
1092 granule_unlock_transition(g_data, GRANULE_STATE_DELEGATED);
1093
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001094 res->x[0] = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +01001095 res->x[1] = data_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001096out_unmap_ll_table:
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001097 res->x[2] = skip_non_live_entries(map_addr, s2tt, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001098 buffer_unmap(s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001099 granule_unlock(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001100}
1101
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001102/*
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001103 * Update the ripas value for the entry pointed by @s2ttep.
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001104 *
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001105 * Returns:
1106 * < 0 - On error and the operation was aborted,
1107 * e.g., entry cannot have a ripas.
1108 * 0 - Operation was success and no TLBI is required.
1109 * > 0 - Operation was success and TLBI is required.
1110 * Sets:
1111 * @(*do_tlbi) to 'true' if the TLBs have to be invalidated.
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001112 */
AlexeiFedorov4faab852023-08-30 15:06:49 +01001113static int update_ripas(unsigned long *s2ttep, long level,
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001114 enum ripas ripas_val,
1115 enum ripas_change_destroyed change_destroyed)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001116{
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001117 unsigned long pa, s2tte = s2tte_read(s2ttep);
1118 int ret = 0;
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001119
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001120 if (!s2tte_has_ripas(s2tte, level)) {
1121 return -1;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001122 }
1123
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001124 if (ripas_val == RIPAS_RAM) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001125 if (s2tte_is_unassigned_empty(s2tte)) {
1126 s2tte = s2tte_create_unassigned_ram();
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001127 } else if (s2tte_is_unassigned_destroyed(s2tte)) {
1128 if (change_destroyed == CHANGE_DESTROYED) {
1129 s2tte = s2tte_create_unassigned_ram();
1130 } else {
1131 return -1;
1132 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001133 } else if (s2tte_is_assigned_empty(s2tte, level)) {
1134 pa = s2tte_pa(s2tte, level);
1135 s2tte = s2tte_create_assigned_ram(pa, level);
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001136 } else if (s2tte_is_assigned_destroyed(s2tte, level)) {
1137 if (change_destroyed == CHANGE_DESTROYED) {
1138 pa = s2tte_pa(s2tte, level);
1139 s2tte = s2tte_create_assigned_ram(pa, level);
1140 } else {
1141 return -1;
1142 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001143 } else {
1144 /* No action is required */
1145 return 0;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001146 }
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001147 } else if (ripas_val == RIPAS_EMPTY) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001148 if (s2tte_is_unassigned_ram(s2tte)) {
1149 s2tte = s2tte_create_unassigned_empty();
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001150 } else if (s2tte_is_unassigned_destroyed(s2tte)) {
1151 if (change_destroyed == CHANGE_DESTROYED) {
1152 s2tte = s2tte_create_unassigned_empty();
1153 } else {
1154 return -1;
1155 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001156 } else if (s2tte_is_assigned_ram(s2tte, level)) {
1157 pa = s2tte_pa(s2tte, level);
1158 s2tte = s2tte_create_assigned_empty(pa, level);
1159 /* TLBI is required */
1160 ret = 1;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001161 } else if (s2tte_is_assigned_destroyed(s2tte, level)) {
1162 if (change_destroyed == CHANGE_DESTROYED) {
1163 pa = s2tte_pa(s2tte, level);
1164 s2tte = s2tte_create_assigned_empty(pa, level);
1165 /* TLBI is required */
1166 ret = 1;
1167 } else {
1168 return -1;
1169 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001170 } else {
1171 /* No action is required */
1172 return 0;
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001173 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001174 }
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001175 s2tte_write(s2ttep, s2tte);
1176 return ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001177}
1178
AlexeiFedorov960d1612023-04-25 13:23:39 +01001179void smc_rtt_init_ripas(unsigned long rd_addr,
1180 unsigned long base,
1181 unsigned long top,
1182 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001183{
1184 struct granule *g_rd, *g_rtt_root;
1185 struct rd *rd;
AlexeiFedorov960d1612023-04-25 13:23:39 +01001186 unsigned long ipa_bits, addr, map_size;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001187 struct rtt_walk wi;
1188 unsigned long s2tte, *s2tt;
AlexeiFedorov960d1612023-04-25 13:23:39 +01001189 long level;
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001190 unsigned long index;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001191 int sl;
1192
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001193 if (top <= base) {
1194 res->x[0] = RMI_ERROR_INPUT;
1195 return;
1196 }
1197
Soby Mathewb4c6df42022-11-09 11:13:29 +00001198 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
1199 if (g_rd == NULL) {
AlexeiFedorov960d1612023-04-25 13:23:39 +01001200 res->x[0] = RMI_ERROR_INPUT;
1201 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001202 }
1203
1204 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001205 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001206
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001207 if (!validate_map_addr(base, RTT_PAGE_LEVEL, rd) ||
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001208 !validate_map_addr(top, RTT_PAGE_LEVEL, rd) ||
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001209 !addr_in_par(rd, base) || !addr_in_par(rd, top - GRANULE_SIZE)) {
1210 buffer_unmap(rd);
1211 granule_unlock(g_rd);
1212 res->x[0] = RMI_ERROR_INPUT;
1213 return;
1214 }
1215
Soby Mathewb4c6df42022-11-09 11:13:29 +00001216 if (get_rd_state_locked(rd) != REALM_STATE_NEW) {
1217 buffer_unmap(rd);
1218 granule_unlock(g_rd);
AlexeiFedorov960d1612023-04-25 13:23:39 +01001219 res->x[0] = RMI_ERROR_REALM;
1220 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001221 }
1222
Soby Mathewb4c6df42022-11-09 11:13:29 +00001223 g_rtt_root = rd->s2_ctx.g_rtt;
1224 sl = realm_rtt_starting_level(rd);
1225 ipa_bits = realm_ipa_bits(rd);
1226
1227 granule_lock(g_rtt_root, GRANULE_STATE_RTT);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001228
1229 rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits,
AlexeiFedorov960d1612023-04-25 13:23:39 +01001230 base, RTT_PAGE_LEVEL, &wi);
1231 level = wi.last_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001232 s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001233 assert(s2tt != NULL);
1234
AlexeiFedorov960d1612023-04-25 13:23:39 +01001235 map_size = s2tte_map_size(level);
1236 addr = base & ~(map_size - 1UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001237
AlexeiFedorov960d1612023-04-25 13:23:39 +01001238 /*
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001239 * If the RTTE covers a range below "base", we need to go deeper.
AlexeiFedorov960d1612023-04-25 13:23:39 +01001240 */
1241 if (addr != base) {
1242 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1243 (unsigned int)level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001244 goto out_unmap_llt;
1245 }
1246
AlexeiFedorov960d1612023-04-25 13:23:39 +01001247 for (index = wi.index; index < S2TTES_PER_S2TT;
1248 index++, addr += map_size) {
1249 unsigned long next = addr + map_size;
1250
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001251 /*
1252 * Break on "top_align" failure condition,
1253 * or if this entry crosses the range.
1254 */
AlexeiFedorov960d1612023-04-25 13:23:39 +01001255 if (next > top) {
1256 break;
1257 }
1258
1259 s2tte = s2tte_read(&s2tt[index]);
1260 if (s2tte_is_unassigned_empty(s2tte)) {
1261 s2tte = s2tte_create_unassigned_ram();
1262 s2tte_write(&s2tt[index], s2tte);
1263 } else if (!s2tte_is_unassigned_ram(s2tte)) {
1264 break;
1265 }
Mate Toth-Palc7698312023-08-09 12:49:34 +02001266 measurement_init_ripas_measure(rd->measurement[RIM_MEASUREMENT_SLOT],
1267 rd->algorithm,
1268 addr,
1269 next);
AlexeiFedorov960d1612023-04-25 13:23:39 +01001270 }
1271
1272 if (addr > base) {
1273 res->x[0] = RMI_SUCCESS;
1274 res->x[1] = addr;
1275 } else {
1276 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1277 (unsigned int)level);
1278 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001279
1280out_unmap_llt:
1281 buffer_unmap(s2tt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001282 buffer_unmap(rd);
1283 granule_unlock(wi.g_llt);
AlexeiFedorov80295e42023-07-10 13:11:14 +01001284 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001285}
1286
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001287static void rtt_set_ripas_range(struct realm_s2_context *s2_ctx,
1288 unsigned long *s2tt,
1289 unsigned long base,
1290 unsigned long top,
1291 struct rtt_walk *wi,
AlexeiFedorov4faab852023-08-30 15:06:49 +01001292 enum ripas ripas_val,
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001293 enum ripas_change_destroyed change_destroyed,
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001294 struct smc_result *res)
1295{
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001296 unsigned long index;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001297 long level = wi->last_level;
AlexeiFedorov4faab852023-08-30 15:06:49 +01001298 unsigned long map_size = s2tte_map_size((int)level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001299
1300 /* Align to the RTT level */
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001301 unsigned long addr = base & ~(map_size - 1UL);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001302
1303 /* Make sure we don't touch a range below the requested range */
1304 if (addr != base) {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +01001305 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1306 (unsigned int)level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001307 return;
1308 }
1309
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001310 for (index = wi->index; index < S2TTES_PER_S2TT; addr += map_size) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001311 int ret;
1312
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001313 /*
1314 * Break on "top_align" failure condition,
1315 * or if this entry crosses the range.
1316 */
1317 if ((addr + map_size) > top) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001318 break;
1319 }
1320
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001321 ret = update_ripas(&s2tt[index++], level,
1322 ripas_val, change_destroyed);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001323 if (ret < 0) {
1324 break;
1325 }
1326
1327 /* Handle TLBI */
1328 if (ret != 0) {
1329 if (level == RTT_PAGE_LEVEL) {
1330 invalidate_page(s2_ctx, addr);
1331 } else {
1332 invalidate_block(s2_ctx, addr);
1333 }
1334 }
1335 }
1336
1337 if (addr > base) {
1338 res->x[0] = RMI_SUCCESS;
1339 res->x[1] = addr;
1340 } else {
AlexeiFedorovbe37dee2023-07-18 10:44:01 +01001341 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1342 (unsigned int)level);
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001343 }
1344}
1345
1346void smc_rtt_set_ripas(unsigned long rd_addr,
1347 unsigned long rec_addr,
1348 unsigned long base,
1349 unsigned long top,
1350 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001351{
1352 struct granule *g_rd, *g_rec, *g_rtt_root;
1353 struct rec *rec;
1354 struct rd *rd;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001355 unsigned long ipa_bits;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001356 struct rtt_walk wi;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001357 unsigned long *s2tt;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001358 struct realm_s2_context s2_ctx;
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001359 enum ripas ripas_val;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001360 enum ripas_change_destroyed change_destroyed;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001361 int sl;
1362
AlexeiFedorov14d47ae2023-07-19 15:26:50 +01001363 if (top <= base) {
1364 res->x[0] = RMI_ERROR_INPUT;
1365 return;
1366 }
1367
Soby Mathewb4c6df42022-11-09 11:13:29 +00001368 if (!find_lock_two_granules(rd_addr,
1369 GRANULE_STATE_RD,
1370 &g_rd,
1371 rec_addr,
1372 GRANULE_STATE_REC,
1373 &g_rec)) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001374 res->x[0] = RMI_ERROR_INPUT;
1375 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001376 }
1377
1378 if (granule_refcount_read_acquire(g_rec) != 0UL) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001379 res->x[0] = RMI_ERROR_REC;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001380 goto out_unlock_rec_rd;
1381 }
1382
1383 rec = granule_map(g_rec, SLOT_REC);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001384 assert(rec != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001385
1386 if (g_rd != rec->realm_info.g_rd) {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001387 res->x[0] = RMI_ERROR_REC;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001388 goto out_unmap_rec;
1389 }
1390
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001391 ripas_val = rec->set_ripas.ripas_val;
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001392 change_destroyed = rec->set_ripas.change_destroyed;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001393
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001394 /*
1395 * Return error in case of target region:
1396 * - is not the next chunk of requested region
1397 * - extends beyond the end of requested region
1398 */
1399 if ((base != rec->set_ripas.addr) || (top > rec->set_ripas.top)) {
1400 res->x[0] = RMI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001401 goto out_unmap_rec;
1402 }
1403
1404 rd = granule_map(g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001405 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001406
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001407 /*
1408 * At this point, we know base == rec->set_ripas.addr
1409 * and thus must be aligned to GRANULE size.
1410 */
1411 assert(validate_map_addr(base, RTT_PAGE_LEVEL, rd));
Soby Mathewb4c6df42022-11-09 11:13:29 +00001412
Soby Mathewb4c6df42022-11-09 11:13:29 +00001413 g_rtt_root = rd->s2_ctx.g_rtt;
1414 sl = realm_rtt_starting_level(rd);
1415 ipa_bits = realm_ipa_bits(rd);
1416 s2_ctx = rd->s2_ctx;
1417
1418 granule_lock(g_rtt_root, GRANULE_STATE_RTT);
1419
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001420 /* Walk to the deepest level possible */
Soby Mathewb4c6df42022-11-09 11:13:29 +00001421 rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits,
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001422 base, RTT_PAGE_LEVEL, &wi);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001423
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001424 /*
1425 * Base has to be aligned to the level at which
1426 * it is mapped in RTT.
1427 */
1428 if (!validate_map_addr(base, wi.last_level, rd)) {
1429 res->x[0] = pack_return_code(RMI_ERROR_RTT,
1430 (unsigned int)wi.last_level);
1431 goto out_unlock_llt;
1432 }
1433
Soby Mathewb4c6df42022-11-09 11:13:29 +00001434 s2tt = granule_map(wi.g_llt, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +01001435 assert(s2tt != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001436
AlexeiFedorov63614ea2023-07-14 17:07:20 +01001437 rtt_set_ripas_range(&s2_ctx, s2tt, base, top, &wi,
1438 ripas_val, change_destroyed, res);
1439
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +01001440 if (res->x[0] == RMI_SUCCESS) {
1441 rec->set_ripas.addr = res->x[1];
Soby Mathewb4c6df42022-11-09 11:13:29 +00001442 }
1443
Soby Mathewb4c6df42022-11-09 11:13:29 +00001444 buffer_unmap(s2tt);
AlexeiFedorov64fd6c32023-07-20 12:33:00 +01001445out_unlock_llt:
Soby Mathewb4c6df42022-11-09 11:13:29 +00001446 granule_unlock(wi.g_llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001447 buffer_unmap(rd);
1448out_unmap_rec:
1449 buffer_unmap(rec);
1450out_unlock_rec_rd:
1451 granule_unlock(g_rec);
1452 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001453}