blob: fbf4956bb51a3dd3fd7f49500c72816ca8b755d9 [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,
23 unsigned long level,
24 struct rd *rd)
25{
26
27 if (map_addr >= realm_ipa_size(rd)) {
28 return false;
29 }
30 if (!addr_is_level_aligned(map_addr, level)) {
31 return false;
32 }
33 return true;
34}
35
36/*
37 * Structure commands can operate on all RTTs except for the root RTT so
38 * the minimal valid level is the stage 2 starting level + 1.
39 */
40static bool validate_rtt_structure_cmds(unsigned long map_addr,
41 long level,
42 struct rd *rd)
43{
44 int min_level = realm_rtt_starting_level(rd) + 1;
45
46 if ((level < min_level) || (level > RTT_PAGE_LEVEL)) {
47 return false;
48 }
49 return validate_map_addr(map_addr, level, rd);
50}
51
52/*
53 * Map/Unmap commands can operate up to a level 2 block entry so min_level is
54 * the smallest block size.
55 */
56static bool validate_rtt_map_cmds(unsigned long map_addr,
57 long level,
58 struct rd *rd)
59{
60 if ((level < RTT_MIN_BLOCK_LEVEL) || (level > RTT_PAGE_LEVEL)) {
61 return false;
62 }
63 return validate_map_addr(map_addr, level, rd);
64}
65
66/*
67 * Entry commands can operate on any entry so the minimal valid level is the
68 * stage 2 starting level.
69 */
70static bool validate_rtt_entry_cmds(unsigned long map_addr,
71 long level,
72 struct rd *rd)
73{
74 if ((level < realm_rtt_starting_level(rd)) ||
75 (level > RTT_PAGE_LEVEL)) {
76 return false;
77 }
78 return validate_map_addr(map_addr, level, rd);
79}
80
AlexeiFedorovac923c82023-04-06 15:12:04 +010081unsigned long smc_rtt_create(unsigned long rd_addr,
82 unsigned long rtt_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +000083 unsigned long map_addr,
84 unsigned long ulevel)
85{
86 struct granule *g_rd;
87 struct granule *g_tbl;
88 struct rd *rd;
89 struct granule *g_table_root;
90 struct rtt_walk wi;
91 unsigned long *s2tt, *parent_s2tt, parent_s2tte;
92 long level = (long)ulevel;
93 unsigned long ipa_bits;
94 unsigned long ret;
95 struct realm_s2_context s2_ctx;
96 int sl;
97
98 if (!find_lock_two_granules(rtt_addr,
99 GRANULE_STATE_DELEGATED,
100 &g_tbl,
101 rd_addr,
102 GRANULE_STATE_RD,
103 &g_rd)) {
104 return RMI_ERROR_INPUT;
105 }
106
107 rd = granule_map(g_rd, SLOT_RD);
108
109 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
110 buffer_unmap(rd);
111 granule_unlock(g_rd);
112 granule_unlock(g_tbl);
113 return RMI_ERROR_INPUT;
114 }
115
116 g_table_root = rd->s2_ctx.g_rtt;
117 sl = realm_rtt_starting_level(rd);
118 ipa_bits = realm_ipa_bits(rd);
119 s2_ctx = rd->s2_ctx;
120 buffer_unmap(rd);
121
122 /*
123 * Lock the RTT root. Enforcing locking order RD->RTT is enough to
124 * ensure deadlock free locking guarentee.
125 */
126 granule_lock(g_table_root, GRANULE_STATE_RTT);
127
128 /* Unlock RD after locking RTT Root */
129 granule_unlock(g_rd);
130
131 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
132 map_addr, level - 1L, &wi);
133 if (wi.last_level != level - 1L) {
134 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
135 goto out_unlock_llt;
136 }
137
138 parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
139 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
140 s2tt = granule_map(g_tbl, SLOT_DELEGATED);
141
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
Soby Mathewb4c6df42022-11-09 11:13:29 +0000161 } else if (s2tte_is_destroyed(parent_s2tte)) {
162 s2tt_init_destroyed(s2tt);
163 __granule_get(wi.g_llt);
164
AlexeiFedorov3a739332023-04-13 13:54:04 +0100165 } else if (s2tte_is_assigned_empty(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000166 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_empty(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_ram(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000185 unsigned long block_pa;
186
187 /*
188 * We should observe parent valid s2tte only when
189 * we create tables above this level.
190 */
191 assert(level > RTT_MIN_BLOCK_LEVEL);
192
193 /*
194 * Break before make. This may cause spurious S2 aborts.
195 */
196 s2tte_write(&parent_s2tt[wi.index], 0UL);
197 invalidate_block(&s2_ctx, map_addr);
198
199 block_pa = s2tte_pa(parent_s2tte, level - 1L);
200
AlexeiFedorov3a739332023-04-13 13:54:04 +0100201 s2tt_init_assigned_ram(s2tt, block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000202
203 /*
204 * Increase the refcount to mark the granule as in-use. refcount
205 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
206 */
207 __granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
208
AlexeiFedorov3a739332023-04-13 13:54:04 +0100209 } else if (s2tte_is_assigned_ns(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000210 unsigned long block_pa;
211
212 /*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100213 * We should observe parent assigned_ns s2tte only when
Soby Mathewb4c6df42022-11-09 11:13:29 +0000214 * we create tables above this level.
215 */
216 assert(level > RTT_MIN_BLOCK_LEVEL);
217
218 /*
219 * Break before make. This may cause spurious S2 aborts.
220 */
221 s2tte_write(&parent_s2tt[wi.index], 0UL);
222 invalidate_block(&s2_ctx, map_addr);
223
224 block_pa = s2tte_pa(parent_s2tte, level - 1L);
225
AlexeiFedorov3a739332023-04-13 13:54:04 +0100226 s2tt_init_assigned_ns(s2tt, block_pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000227
228 /*
229 * Increase the refcount to mark the granule as in-use. refcount
230 * is incremented by S2TTES_PER_S2TT (ref RTT unfolding).
231 */
232 __granule_refcount_inc(g_tbl, S2TTES_PER_S2TT);
233
234 } else if (s2tte_is_table(parent_s2tte, level - 1L)) {
235 ret = pack_return_code(RMI_ERROR_RTT,
236 (unsigned int)(level - 1L));
237 goto out_unmap_table;
238
239 } else {
240 assert(false);
241 }
242
243 ret = RMI_SUCCESS;
244
245 granule_set_state(g_tbl, GRANULE_STATE_RTT);
246
247 parent_s2tte = s2tte_create_table(rtt_addr, level - 1L);
248 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
249
250out_unmap_table:
251 buffer_unmap(s2tt);
252 buffer_unmap(parent_s2tt);
253out_unlock_llt:
254 granule_unlock(wi.g_llt);
255 granule_unlock(g_tbl);
256 return ret;
257}
258
AlexeiFedorove2002be2023-04-19 17:20:12 +0100259void smc_rtt_fold(unsigned long rd_addr,
260 unsigned long map_addr,
261 unsigned long ulevel,
262 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000263{
264 struct granule *g_rd;
265 struct granule *g_tbl;
266 struct rd *rd;
267 struct granule *g_table_root;
268 struct rtt_walk wi;
269 unsigned long *table, *parent_s2tt, parent_s2tte;
270 long level = (long)ulevel;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100271 unsigned long ipa_bits, rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000272 unsigned long ret;
273 struct realm_s2_context s2_ctx;
274 int sl;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000275
276 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
277 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100278 res->x[0] = RMI_ERROR_INPUT;
279 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000280 }
281
282 rd = granule_map(g_rd, SLOT_RD);
283
284 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
285 buffer_unmap(rd);
286 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100287 res->x[0] = RMI_ERROR_INPUT;
288 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000289 }
290
291 g_table_root = rd->s2_ctx.g_rtt;
292 sl = realm_rtt_starting_level(rd);
293 ipa_bits = realm_ipa_bits(rd);
294 s2_ctx = rd->s2_ctx;
295 buffer_unmap(rd);
296 granule_lock(g_table_root, GRANULE_STATE_RTT);
297 granule_unlock(g_rd);
298
299 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
300 map_addr, level - 1L, &wi);
301 if (wi.last_level != level - 1UL) {
302 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
303 goto out_unlock_parent_table;
304 }
305
306 parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
307 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
308 if (!s2tte_is_table(parent_s2tte, level - 1L)) {
309 ret = pack_return_code(RMI_ERROR_RTT,
310 (unsigned int)(level - 1L));
311 goto out_unmap_parent_table;
312 }
313
AlexeiFedorove2002be2023-04-19 17:20:12 +0100314 rtt_addr = s2tte_pa_table(parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000315 g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT);
316
317 /*
318 * A table descriptor S2TTE always points to a TABLE granule.
319 */
AlexeiFedorov63b71692023-04-19 11:18:42 +0100320 assert(g_tbl != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000321
322 table = granule_map(g_tbl, SLOT_RTT2);
323
324 /*
325 * The command can succeed only if all 512 S2TTEs are of the same type.
326 * We first check the table's ref. counter to speed up the case when
327 * the host makes a guess whether a memory region can be folded.
328 */
329 if (g_tbl->refcount == 0UL) {
330 if (table_is_destroyed_block(table)) {
331 parent_s2tte = s2tte_create_destroyed();
332 __granule_put(wi.g_llt);
333
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100334 } else if (table_is_unassigned_empty_block(table)) {
335 parent_s2tte = s2tte_create_unassigned_empty();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000336 __granule_put(wi.g_llt);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100337
338 } else if (table_is_unassigned_ram_block(table)) {
339 parent_s2tte = s2tte_create_unassigned_ram();
340 __granule_put(wi.g_llt);
341
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100342 } else if (table_is_unassigned_ns_block(table)) {
343 parent_s2tte = s2tte_create_unassigned_ns();
344 __granule_put(wi.g_llt);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100345
Soby Mathewb4c6df42022-11-09 11:13:29 +0000346 } else {
347 /*
348 * The table holds a mixture of destroyed and
349 * unassigned entries.
350 */
AlexeiFedorov892abce2023-04-06 16:32:12 +0100351 ret = pack_return_code(RMI_ERROR_RTT, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000352 goto out_unmap_table;
353 }
354
355 } else if (g_tbl->refcount == S2TTES_PER_S2TT) {
356
357 unsigned long s2tte, block_pa;
358
359 /* The RMM specification does not allow creating block
360 * entries less than RTT_MIN_BLOCK_LEVEL even though
361 * permitted by the Arm Architecture.
362 * Hence ensure that the table being folded is at a level
363 * higher than the RTT_MIN_BLOCK_LEVEL.
364 *
365 * A fully populated table cannot be destroyed if that
366 * would create a block mapping below RTT_MIN_BLOCK_LEVEL.
367 */
368 if (level <= RTT_MIN_BLOCK_LEVEL) {
AlexeiFedorov892abce2023-04-06 16:32:12 +0100369 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000370 goto out_unmap_table;
371 }
372
373 s2tte = s2tte_read(&table[0]);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000374 block_pa = s2tte_pa(s2tte, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000375
376 /*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100377 * The table must also refer to a contiguous block through the
378 * same type of s2tte, either Assigned, Valid or Assigned_NS.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000379 */
AlexeiFedorov3a739332023-04-13 13:54:04 +0100380 if (table_maps_assigned_empty_block(table, level)) {
381 parent_s2tte = s2tte_create_assigned_empty(block_pa,
382 level - 1L);
383 } else if (table_maps_assigned_ram_block(table, level)) {
384 parent_s2tte = s2tte_create_assigned_ram(block_pa,
385 level - 1L);
386 } else if (table_maps_assigned_ns_block(table, level)) {
387 parent_s2tte = s2tte_create_assigned_ns(block_pa,
388 level - 1L);
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000389 /* The table contains mixed entries that cannot be folded */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000390 } else {
AlexeiFedorov80c2f042022-11-25 14:54:46 +0000391 ret = pack_return_code(RMI_ERROR_RTT, level);
392 goto out_unmap_table;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000393 }
394
395 __granule_refcount_dec(g_tbl, S2TTES_PER_S2TT);
396 } else {
397 /*
398 * The table holds a mixture of different types of s2ttes.
399 */
AlexeiFedorov892abce2023-04-06 16:32:12 +0100400 ret = pack_return_code(RMI_ERROR_RTT, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000401 goto out_unmap_table;
402 }
403
404 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100405 res->x[1] = rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000406
407 /*
408 * Break before make.
409 */
410 s2tte_write(&parent_s2tt[wi.index], 0UL);
411
AlexeiFedorov3a739332023-04-13 13:54:04 +0100412 if (s2tte_is_assigned_ram(parent_s2tte, level - 1L) ||
413 s2tte_is_assigned_ns(parent_s2tte, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000414 invalidate_pages_in_block(&s2_ctx, map_addr);
415 } else {
416 invalidate_block(&s2_ctx, map_addr);
417 }
418
419 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
420
421 granule_memzero_mapped(table);
422 granule_set_state(g_tbl, GRANULE_STATE_DELEGATED);
423
424out_unmap_table:
425 buffer_unmap(table);
426 granule_unlock(g_tbl);
427out_unmap_parent_table:
428 buffer_unmap(parent_s2tt);
429out_unlock_parent_table:
430 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100431 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000432}
433
AlexeiFedorove2002be2023-04-19 17:20:12 +0100434void smc_rtt_destroy(unsigned long rd_addr,
435 unsigned long map_addr,
436 unsigned long ulevel,
437 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000438{
439 struct granule *g_rd;
440 struct granule *g_tbl;
441 struct rd *rd;
442 struct granule *g_table_root;
443 struct rtt_walk wi;
444 unsigned long *table, *parent_s2tt, parent_s2tte;
445 long level = (long)ulevel;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100446 unsigned long ipa_bits, rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000447 unsigned long ret;
448 struct realm_s2_context s2_ctx;
449 int sl;
450 bool in_par;
451
452 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
453 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100454 res->x[0] = RMI_ERROR_INPUT;
455 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000456 }
457
458 rd = granule_map(g_rd, SLOT_RD);
459
460 if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
461 buffer_unmap(rd);
462 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100463 res->x[0] = RMI_ERROR_INPUT;
464 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000465 }
466
467 g_table_root = rd->s2_ctx.g_rtt;
468 sl = realm_rtt_starting_level(rd);
469 ipa_bits = realm_ipa_bits(rd);
470 s2_ctx = rd->s2_ctx;
471 in_par = addr_in_par(rd, map_addr);
472 buffer_unmap(rd);
473 granule_lock(g_table_root, GRANULE_STATE_RTT);
474 granule_unlock(g_rd);
475
476 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
477 map_addr, level - 1L, &wi);
478 if (wi.last_level != level - 1UL) {
479 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
480 goto out_unlock_parent_table;
481 }
482
483 parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
484 parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
485 if (!s2tte_is_table(parent_s2tte, level - 1L)) {
486 ret = pack_return_code(RMI_ERROR_RTT,
487 (unsigned int)(level - 1L));
488 goto out_unmap_parent_table;
489 }
490
AlexeiFedorove2002be2023-04-19 17:20:12 +0100491 rtt_addr = s2tte_pa_table(parent_s2tte, level - 1L);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000492
493 /*
494 * Lock the RTT granule. The 'rtt_addr' is verified, thus can be treated
495 * as an internal granule.
496 */
497 g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT);
498
499 /*
500 * A table descriptor S2TTE always points to a TABLE granule.
501 */
502 assert(g_tbl != NULL);
503
504 /*
505 * Read the refcount value. RTT granule is always accessed locked, thus
506 * the refcount can be accessed without atomic operations.
507 */
508 if (g_tbl->refcount != 0UL) {
AlexeiFedorov892abce2023-04-06 16:32:12 +0100509 ret = pack_return_code(RMI_ERROR_RTT, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000510 goto out_unlock_table;
511 }
512
513 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100514 res->x[1] = rtt_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000515
516 table = granule_map(g_tbl, SLOT_RTT2);
517
518 if (in_par) {
519 parent_s2tte = s2tte_create_destroyed();
520 } else {
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100521 parent_s2tte = s2tte_create_unassigned_ns();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000522 }
523
524 __granule_put(wi.g_llt);
525
526 /*
527 * Break before make. Note that this may cause spurious S2 aborts.
528 */
529 s2tte_write(&parent_s2tt[wi.index], 0UL);
530 invalidate_block(&s2_ctx, map_addr);
531 s2tte_write(&parent_s2tt[wi.index], parent_s2tte);
532
533 granule_memzero_mapped(table);
534 granule_set_state(g_tbl, GRANULE_STATE_DELEGATED);
535
536 buffer_unmap(table);
537out_unlock_table:
538 granule_unlock(g_tbl);
539out_unmap_parent_table:
540 buffer_unmap(parent_s2tt);
541out_unlock_parent_table:
542 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100543 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000544}
545
546enum map_unmap_ns_op {
547 MAP_NS,
548 UNMAP_NS
549};
550
551/*
552 * We don't hold a reference on the NS granule when it is
553 * mapped into a realm. Instead we rely on the guarantees
554 * provided by the architecture to ensure that a NS access
555 * to a protected granule is prohibited even within the realm.
556 */
557static unsigned long map_unmap_ns(unsigned long rd_addr,
558 unsigned long map_addr,
559 long level,
560 unsigned long host_s2tte,
561 enum map_unmap_ns_op op)
562{
563 struct granule *g_rd;
564 struct rd *rd;
565 struct granule *g_table_root;
566 unsigned long *s2tt, s2tte;
567 struct rtt_walk wi;
568 unsigned long ipa_bits;
569 unsigned long ret;
570 struct realm_s2_context s2_ctx;
571 int sl;
572
573 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
574 if (g_rd == NULL) {
575 return RMI_ERROR_INPUT;
576 }
577
578 rd = granule_map(g_rd, SLOT_RD);
579
580 if (!validate_rtt_map_cmds(map_addr, level, rd)) {
581 buffer_unmap(rd);
582 granule_unlock(g_rd);
583 return RMI_ERROR_INPUT;
584 }
585
586 g_table_root = rd->s2_ctx.g_rtt;
587 sl = realm_rtt_starting_level(rd);
588 ipa_bits = realm_ipa_bits(rd);
589
AlexeiFedorovc34e3242023-04-12 11:30:33 +0100590 /* Check if map_addr is outside PAR */
591 if (addr_in_par(rd, map_addr)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000592 buffer_unmap(rd);
593 granule_unlock(g_rd);
594 return RMI_ERROR_INPUT;
595 }
596
597 s2_ctx = rd->s2_ctx;
598 buffer_unmap(rd);
599
600 granule_lock(g_table_root, GRANULE_STATE_RTT);
601 granule_unlock(g_rd);
602
603 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
604 map_addr, level, &wi);
605 if (wi.last_level != level) {
606 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
607 goto out_unlock_llt;
608 }
609
610 s2tt = granule_map(wi.g_llt, SLOT_RTT);
611 s2tte = s2tte_read(&s2tt[wi.index]);
612
613 if (op == MAP_NS) {
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100614 if (!s2tte_is_unassigned_ns(s2tte)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000615 ret = pack_return_code(RMI_ERROR_RTT,
616 (unsigned int)level);
617 goto out_unmap_table;
618 }
619
AlexeiFedorov3a739332023-04-13 13:54:04 +0100620 s2tte = s2tte_create_assigned_ns(host_s2tte, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000621 s2tte_write(&s2tt[wi.index], s2tte);
622 __granule_get(wi.g_llt);
623
624 } else if (op == UNMAP_NS) {
625 /*
626 * The following check also verifies that map_addr is outside
627 * PAR, as valid_NS s2tte may only cover outside PAR IPA range.
628 */
AlexeiFedorov3a739332023-04-13 13:54:04 +0100629 if (!s2tte_is_assigned_ns(s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000630 ret = pack_return_code(RMI_ERROR_RTT,
631 (unsigned int)level);
632 goto out_unmap_table;
633 }
634
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100635 s2tte = s2tte_create_unassigned_ns();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000636 s2tte_write(&s2tt[wi.index], s2tte);
637 __granule_put(wi.g_llt);
638 if (level == RTT_PAGE_LEVEL) {
639 invalidate_page(&s2_ctx, map_addr);
640 } else {
641 invalidate_block(&s2_ctx, map_addr);
642 }
643 }
644
645 ret = RMI_SUCCESS;
646
647out_unmap_table:
648 buffer_unmap(s2tt);
649out_unlock_llt:
650 granule_unlock(wi.g_llt);
651 return ret;
652}
653
654unsigned long smc_rtt_map_unprotected(unsigned long rd_addr,
655 unsigned long map_addr,
656 unsigned long ulevel,
657 unsigned long s2tte)
658{
659 long level = (long)ulevel;
660
661 if (!host_ns_s2tte_is_valid(s2tte, level)) {
662 return RMI_ERROR_INPUT;
663 }
664
665 return map_unmap_ns(rd_addr, map_addr, level, s2tte, MAP_NS);
666}
667
668unsigned long smc_rtt_unmap_unprotected(unsigned long rd_addr,
669 unsigned long map_addr,
670 unsigned long ulevel)
671{
672 return map_unmap_ns(rd_addr, map_addr, (long)ulevel, 0UL, UNMAP_NS);
673}
674
675void smc_rtt_read_entry(unsigned long rd_addr,
676 unsigned long map_addr,
677 unsigned long ulevel,
678 struct smc_result *ret)
679{
680 struct granule *g_rd, *g_rtt_root;
681 struct rd *rd;
682 struct rtt_walk wi;
683 unsigned long *s2tt, s2tte;
684 unsigned long ipa_bits;
685 long level = (long)ulevel;
686 int sl;
687
688 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
689 if (g_rd == NULL) {
690 ret->x[0] = RMI_ERROR_INPUT;
691 return;
692 }
693
694 rd = granule_map(g_rd, SLOT_RD);
695
696 if (!validate_rtt_entry_cmds(map_addr, level, rd)) {
697 buffer_unmap(rd);
698 granule_unlock(g_rd);
699 ret->x[0] = RMI_ERROR_INPUT;
700 return;
701 }
702
703 g_rtt_root = rd->s2_ctx.g_rtt;
704 sl = realm_rtt_starting_level(rd);
705 ipa_bits = realm_ipa_bits(rd);
706 buffer_unmap(rd);
707
708 granule_lock(g_rtt_root, GRANULE_STATE_RTT);
709 granule_unlock(g_rd);
710
711 rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits,
712 map_addr, level, &wi);
713 s2tt = granule_map(wi.g_llt, SLOT_RTT);
714 s2tte = s2tte_read(&s2tt[wi.index]);
715 ret->x[1] = wi.last_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000716
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100717 if (s2tte_is_unassigned_empty(s2tte)) {
Yousuf A3daed822022-10-13 16:06:00 +0100718 ret->x[2] = RMI_UNASSIGNED;
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100719 ret->x[3] = 0UL;
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100720 ret->x[4] = RIPAS_EMPTY;
721 } else if (s2tte_is_unassigned_ram(s2tte)) {
722 ret->x[2] = RMI_UNASSIGNED;
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100723 ret->x[3] = 0UL;
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100724 ret->x[4] = RIPAS_RAM;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000725 } else if (s2tte_is_destroyed(s2tte)) {
Yousuf A3daed822022-10-13 16:06:00 +0100726 ret->x[2] = RMI_DESTROYED;
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100727 ret->x[3] = 0UL;
728 ret->x[4] = RIPAS_UNDEFINED;
AlexeiFedorov3a739332023-04-13 13:54:04 +0100729 } else if (s2tte_is_assigned_empty(s2tte, wi.last_level)) {
Yousuf A3daed822022-10-13 16:06:00 +0100730 ret->x[2] = RMI_ASSIGNED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000731 ret->x[3] = s2tte_pa(s2tte, wi.last_level);
Yousuf A62808152022-10-31 10:35:42 +0000732 ret->x[4] = RIPAS_EMPTY;
AlexeiFedorov3a739332023-04-13 13:54:04 +0100733 } else if (s2tte_is_assigned_ram(s2tte, wi.last_level)) {
Yousuf A3daed822022-10-13 16:06:00 +0100734 ret->x[2] = RMI_ASSIGNED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000735 ret->x[3] = s2tte_pa(s2tte, wi.last_level);
Yousuf A62808152022-10-31 10:35:42 +0000736 ret->x[4] = RIPAS_RAM;
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100737 } else if (s2tte_is_unassigned_ns(s2tte)) {
738 ret->x[2] = RMI_UNASSIGNED;
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100739 ret->x[3] = 0UL;
740 ret->x[4] = RIPAS_UNDEFINED;
AlexeiFedorov3a739332023-04-13 13:54:04 +0100741 } else if (s2tte_is_assigned_ns(s2tte, wi.last_level)) {
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100742 ret->x[2] = RMI_ASSIGNED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000743 ret->x[3] = host_ns_s2tte(s2tte, wi.last_level);
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100744 ret->x[4] = RIPAS_UNDEFINED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000745 } else if (s2tte_is_table(s2tte, wi.last_level)) {
Yousuf A3daed822022-10-13 16:06:00 +0100746 ret->x[2] = RMI_TABLE;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000747 ret->x[3] = s2tte_pa_table(s2tte, wi.last_level);
AlexeiFedorov20afb5c2023-04-18 11:44:19 +0100748 ret->x[4] = RIPAS_UNDEFINED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000749 } else {
750 assert(false);
751 }
752
753 buffer_unmap(s2tt);
754 granule_unlock(wi.g_llt);
755
756 ret->x[0] = RMI_SUCCESS;
757}
758
759static void data_granule_measure(struct rd *rd, void *data,
760 unsigned long ipa,
761 unsigned long flags)
762{
763 struct measurement_desc_data measure_desc = {0};
764
765 /* Initialize the measurement descriptior structure */
766 measure_desc.desc_type = MEASURE_DESC_TYPE_DATA;
767 measure_desc.len = sizeof(struct measurement_desc_data);
768 measure_desc.ipa = ipa;
769 measure_desc.flags = flags;
770 memcpy(measure_desc.rim,
771 &rd->measurement[RIM_MEASUREMENT_SLOT],
772 measurement_get_size(rd->algorithm));
773
774 if (flags == RMI_MEASURE_CONTENT) {
775 /*
776 * Hashing the data granules and store the result in the
777 * measurement descriptor structure.
778 */
779 measurement_hash_compute(rd->algorithm,
780 data,
781 GRANULE_SIZE,
782 measure_desc.content);
783 }
784
785 /*
786 * Hashing the measurement descriptor structure; the result is the
787 * updated RIM.
788 */
789 measurement_hash_compute(rd->algorithm,
790 &measure_desc,
791 sizeof(measure_desc),
792 rd->measurement[RIM_MEASUREMENT_SLOT]);
793}
794
795static unsigned long validate_data_create_unknown(unsigned long map_addr,
796 struct rd *rd)
797{
798 if (!addr_in_par(rd, map_addr)) {
799 return RMI_ERROR_INPUT;
800 }
801
802 if (!validate_map_addr(map_addr, RTT_PAGE_LEVEL, rd)) {
803 return RMI_ERROR_INPUT;
804 }
805
806 return RMI_SUCCESS;
807}
808
809static unsigned long validate_data_create(unsigned long map_addr,
810 struct rd *rd)
811{
812 if (get_rd_state_locked(rd) != REALM_STATE_NEW) {
813 return RMI_ERROR_REALM;
814 }
815
816 return validate_data_create_unknown(map_addr, rd);
817}
818
819/*
820 * Implements both Data.Create and Data.CreateUnknown
821 *
822 * if @g_src == NULL, this implemented Data.CreateUnknown
823 * and otherwise this implemented Data.Create.
824 */
AlexeiFedorovac923c82023-04-06 15:12:04 +0100825static unsigned long data_create(unsigned long rd_addr,
826 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000827 unsigned long map_addr,
828 struct granule *g_src,
829 unsigned long flags)
830{
831 struct granule *g_data;
832 struct granule *g_rd;
833 struct granule *g_table_root;
834 struct rd *rd;
835 struct rtt_walk wi;
836 unsigned long s2tte, *s2tt;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000837 enum granule_state new_data_state = GRANULE_STATE_DELEGATED;
838 unsigned long ipa_bits;
839 unsigned long ret;
840 int __unused meas_ret;
841 int sl;
842
843 if (!find_lock_two_granules(data_addr,
844 GRANULE_STATE_DELEGATED,
845 &g_data,
846 rd_addr,
847 GRANULE_STATE_RD,
848 &g_rd)) {
849 return RMI_ERROR_INPUT;
850 }
851
852 rd = granule_map(g_rd, SLOT_RD);
853
854 ret = (g_src != NULL) ?
855 validate_data_create(map_addr, rd) :
856 validate_data_create_unknown(map_addr, rd);
857
858 if (ret != RMI_SUCCESS) {
859 goto out_unmap_rd;
860 }
861
862 g_table_root = rd->s2_ctx.g_rtt;
863 sl = realm_rtt_starting_level(rd);
864 ipa_bits = realm_ipa_bits(rd);
865 granule_lock(g_table_root, GRANULE_STATE_RTT);
866 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
867 map_addr, RTT_PAGE_LEVEL, &wi);
868 if (wi.last_level != RTT_PAGE_LEVEL) {
869 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
870 goto out_unlock_ll_table;
871 }
872
873 s2tt = granule_map(wi.g_llt, SLOT_RTT);
874 s2tte = s2tte_read(&s2tt[wi.index]);
AlexeiFedorovcde1fdc2023-04-18 16:37:25 +0100875 if (!s2tte_is_unassigned_ram(s2tte)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000876 ret = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL);
877 goto out_unmap_ll_table;
878 }
879
Soby Mathewb4c6df42022-11-09 11:13:29 +0000880 if (g_src != NULL) {
881 bool ns_access_ok;
882 void *data = granule_map(g_data, SLOT_DELEGATED);
883
884 ns_access_ok = ns_buffer_read(SLOT_NS, g_src, 0U,
885 GRANULE_SIZE, data);
886
887 if (!ns_access_ok) {
888 /*
889 * Some data may be copied before the failure. Zero
890 * g_data granule as it will remain in delegated state.
891 */
892 (void)memset(data, 0, GRANULE_SIZE);
893 buffer_unmap(data);
894 ret = RMI_ERROR_INPUT;
895 goto out_unmap_ll_table;
896 }
897
898
899 data_granule_measure(rd, data, map_addr, flags);
900
901 buffer_unmap(data);
902 }
903
904 new_data_state = GRANULE_STATE_DATA;
905
AlexeiFedorovcde1fdc2023-04-18 16:37:25 +0100906 s2tte = s2tte_create_assigned_ram(data_addr, RTT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000907
908 s2tte_write(&s2tt[wi.index], s2tte);
909 __granule_get(wi.g_llt);
910
911 ret = RMI_SUCCESS;
912
913out_unmap_ll_table:
914 buffer_unmap(s2tt);
915out_unlock_ll_table:
916 granule_unlock(wi.g_llt);
917out_unmap_rd:
918 buffer_unmap(rd);
919 granule_unlock(g_rd);
920 granule_unlock_transition(g_data, new_data_state);
921 return ret;
922}
923
AlexeiFedorovac923c82023-04-06 15:12:04 +0100924unsigned long smc_data_create(unsigned long rd_addr,
925 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000926 unsigned long map_addr,
927 unsigned long src_addr,
928 unsigned long flags)
929{
930 struct granule *g_src;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000931
932 if (flags != RMI_NO_MEASURE_CONTENT && flags != RMI_MEASURE_CONTENT) {
933 return RMI_ERROR_INPUT;
934 }
935
936 g_src = find_granule(src_addr);
937 if ((g_src == NULL) || (g_src->state != GRANULE_STATE_NS)) {
938 return RMI_ERROR_INPUT;
939 }
940
AlexeiFedorovac923c82023-04-06 15:12:04 +0100941 return data_create(rd_addr, data_addr, map_addr, g_src, flags);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000942}
943
AlexeiFedorovac923c82023-04-06 15:12:04 +0100944unsigned long smc_data_create_unknown(unsigned long rd_addr,
945 unsigned long data_addr,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000946 unsigned long map_addr)
947{
AlexeiFedorovac923c82023-04-06 15:12:04 +0100948 return data_create(rd_addr, data_addr, map_addr, NULL, 0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000949}
950
AlexeiFedorove2002be2023-04-19 17:20:12 +0100951void smc_data_destroy(unsigned long rd_addr,
952 unsigned long map_addr,
953 struct smc_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000954{
955 struct granule *g_data;
956 struct granule *g_rd;
957 struct granule *g_table_root;
958 struct rtt_walk wi;
959 unsigned long data_addr, s2tte, *s2tt;
960 struct rd *rd;
AlexeiFedorove2002be2023-04-19 17:20:12 +0100961 unsigned long ipa_bits, ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000962 struct realm_s2_context s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000963 int sl;
964
965 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
966 if (g_rd == NULL) {
AlexeiFedorove2002be2023-04-19 17:20:12 +0100967 res->x[0] = RMI_ERROR_INPUT;
968 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000969 }
970
971 rd = granule_map(g_rd, SLOT_RD);
972
973 if (!validate_map_addr(map_addr, RTT_PAGE_LEVEL, rd)) {
974 buffer_unmap(rd);
975 granule_unlock(g_rd);
AlexeiFedorove2002be2023-04-19 17:20:12 +0100976 res->x[0] = RMI_ERROR_INPUT;
977 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000978 }
979
980 g_table_root = rd->s2_ctx.g_rtt;
981 sl = realm_rtt_starting_level(rd);
982 ipa_bits = realm_ipa_bits(rd);
983 s2_ctx = rd->s2_ctx;
984 buffer_unmap(rd);
985
986 granule_lock(g_table_root, GRANULE_STATE_RTT);
987 granule_unlock(g_rd);
988
989 rtt_walk_lock_unlock(g_table_root, sl, ipa_bits,
990 map_addr, RTT_PAGE_LEVEL, &wi);
991 if (wi.last_level != RTT_PAGE_LEVEL) {
992 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
993 goto out_unlock_ll_table;
994 }
995
996 s2tt = granule_map(wi.g_llt, SLOT_RTT);
997 s2tte = s2tte_read(&s2tt[wi.index]);
998
AlexeiFedorova43cd312023-04-17 11:42:25 +0100999 if (s2tte_is_assigned_ram(s2tte, RTT_PAGE_LEVEL)) {
1000 data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL);
1001 s2tte = s2tte_create_destroyed();
1002 s2tte_write(&s2tt[wi.index], s2tte);
1003 invalidate_page(&s2_ctx, map_addr);
1004 } else if (s2tte_is_assigned_empty(s2tte, RTT_PAGE_LEVEL)) {
1005 data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL);
1006 s2tte = s2tte_create_unassigned_empty();
1007 s2tte_write(&s2tt[wi.index], s2tte);
1008 } else {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001009 ret = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL);
1010 goto out_unmap_ll_table;
1011 }
1012
Soby Mathewb4c6df42022-11-09 11:13:29 +00001013 __granule_put(wi.g_llt);
1014
1015 /*
1016 * Lock the data granule and check expected state. Correct locking order
1017 * is guaranteed because granule address is obtained from a locked
1018 * granule by table walk. This lock needs to be acquired before a state
1019 * transition to or from GRANULE_STATE_DATA for granule address can happen.
1020 */
1021 g_data = find_lock_granule(data_addr, GRANULE_STATE_DATA);
AlexeiFedorov63b71692023-04-19 11:18:42 +01001022 assert(g_data != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001023 granule_memzero(g_data, SLOT_DELEGATED);
1024 granule_unlock_transition(g_data, GRANULE_STATE_DELEGATED);
1025
1026 ret = RMI_SUCCESS;
AlexeiFedorove2002be2023-04-19 17:20:12 +01001027 res->x[1] = data_addr;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001028
1029out_unmap_ll_table:
1030 buffer_unmap(s2tt);
1031out_unlock_ll_table:
1032 granule_unlock(wi.g_llt);
AlexeiFedorove2002be2023-04-19 17:20:12 +01001033 res->x[0] = ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001034}
1035
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001036/*
1037 * Sets new ripas value in @s2tte.
1038 *
1039 * It returns false if the @s2tte has no ripas value.
1040 * It sets @(*do_tlbi) to 'true' if the TLBs have to be invalidated.
1041 */
1042static bool update_ripas(unsigned long *s2tte, bool *do_tlbi,
1043 unsigned long level, enum ripas ripas_val)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001044{
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001045 unsigned long pa;
1046
1047 *do_tlbi = false;
1048
1049 if (!s2tte_has_ripas(*s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001050 return false;
1051 }
1052
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001053 if (ripas_val == RIPAS_RAM) {
1054 if (s2tte_is_unassigned_empty(*s2tte)) {
1055 *s2tte = s2tte_create_unassigned_ram();
1056 } else if (s2tte_is_assigned_empty(*s2tte, level)) {
1057 pa = s2tte_pa(*s2tte, level);
1058 *s2tte = s2tte_create_assigned_ram(pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001059 }
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001060 } else if (ripas_val == RIPAS_EMPTY) {
1061 if (s2tte_is_unassigned_ram(*s2tte)) {
1062 *s2tte = s2tte_create_unassigned_empty();
1063 } else if (s2tte_is_assigned_ram(*s2tte, level)) {
1064 pa = s2tte_pa(*s2tte, level);
1065 *s2tte = s2tte_create_assigned_empty(pa, level);
1066 *do_tlbi = true;
1067 }
1068 } else {
1069 assert(false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001070 }
1071
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001072 return true;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001073}
1074
1075static void ripas_granule_measure(struct rd *rd,
1076 unsigned long ipa,
1077 unsigned long level)
1078{
1079 struct measurement_desc_ripas measure_desc = {0};
1080
1081 /* Initialize the measurement descriptior structure */
1082 measure_desc.desc_type = MEASURE_DESC_TYPE_RIPAS;
1083 measure_desc.len = sizeof(struct measurement_desc_ripas);
1084 measure_desc.ipa = ipa;
1085 measure_desc.level = level;
1086 memcpy(measure_desc.rim,
1087 &rd->measurement[RIM_MEASUREMENT_SLOT],
1088 measurement_get_size(rd->algorithm));
1089
1090 /*
1091 * Hashing the measurement descriptor structure; the result is the
1092 * updated RIM.
1093 */
1094 measurement_hash_compute(rd->algorithm,
1095 &measure_desc,
1096 sizeof(measure_desc),
1097 rd->measurement[RIM_MEASUREMENT_SLOT]);
1098}
1099
1100unsigned long smc_rtt_init_ripas(unsigned long rd_addr,
1101 unsigned long map_addr,
1102 unsigned long ulevel)
1103{
1104 struct granule *g_rd, *g_rtt_root;
1105 struct rd *rd;
1106 unsigned long ipa_bits;
1107 struct rtt_walk wi;
1108 unsigned long s2tte, *s2tt;
1109 unsigned long ret;
1110 long level = (long)ulevel;
1111 int sl;
1112
1113 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
1114 if (g_rd == NULL) {
1115 return RMI_ERROR_INPUT;
1116 }
1117
1118 rd = granule_map(g_rd, SLOT_RD);
1119
1120 if (get_rd_state_locked(rd) != REALM_STATE_NEW) {
1121 buffer_unmap(rd);
1122 granule_unlock(g_rd);
1123 return RMI_ERROR_REALM;
1124 }
1125
1126 if (!validate_rtt_entry_cmds(map_addr, level, rd)) {
1127 buffer_unmap(rd);
1128 granule_unlock(g_rd);
1129 return RMI_ERROR_INPUT;
1130 }
1131
1132 if (!addr_in_par(rd, map_addr)) {
1133 buffer_unmap(rd);
1134 granule_unlock(g_rd);
1135 return RMI_ERROR_INPUT;
1136 }
1137
1138 g_rtt_root = rd->s2_ctx.g_rtt;
1139 sl = realm_rtt_starting_level(rd);
1140 ipa_bits = realm_ipa_bits(rd);
1141
1142 granule_lock(g_rtt_root, GRANULE_STATE_RTT);
1143 granule_unlock(g_rd);
1144
1145 rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits,
1146 map_addr, level, &wi);
AlexeiFedorovac923c82023-04-06 15:12:04 +01001147
Soby Mathewb4c6df42022-11-09 11:13:29 +00001148 if (wi.last_level != level) {
1149 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
1150 goto out_unlock_llt;
1151 }
1152
1153 s2tt = granule_map(wi.g_llt, SLOT_RTT);
1154 s2tte = s2tte_read(&s2tt[wi.index]);
1155
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001156 if (s2tte_is_unassigned_empty(s2tte)) {
1157 s2tte = s2tte_create_unassigned_ram();
1158 s2tte_write(&s2tt[wi.index], s2tte);
1159 ripas_granule_measure(rd, map_addr, level);
1160 } else if (s2tte_is_unassigned_ram(s2tte)) {
1161 ripas_granule_measure(rd, map_addr, level);
1162 } else {
1163 ret = pack_return_code(RMI_ERROR_RTT, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001164 goto out_unmap_llt;
1165 }
1166
Soby Mathewb4c6df42022-11-09 11:13:29 +00001167 ret = RMI_SUCCESS;
1168
1169out_unmap_llt:
1170 buffer_unmap(s2tt);
1171out_unlock_llt:
1172 buffer_unmap(rd);
1173 granule_unlock(wi.g_llt);
1174 return ret;
1175}
1176
1177unsigned long smc_rtt_set_ripas(unsigned long rd_addr,
1178 unsigned long rec_addr,
1179 unsigned long map_addr,
1180 unsigned long ulevel,
1181 unsigned long uripas)
1182{
1183 struct granule *g_rd, *g_rec, *g_rtt_root;
1184 struct rec *rec;
1185 struct rd *rd;
1186 unsigned long map_size, ipa_bits;
1187 struct rtt_walk wi;
1188 unsigned long s2tte, *s2tt;
1189 struct realm_s2_context s2_ctx;
1190 long level = (long)ulevel;
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001191 enum ripas ripas_val = (enum ripas)uripas;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001192 unsigned long ret;
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001193 bool tlbi_required;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001194 int sl;
1195
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001196 if (ripas_val > RIPAS_RAM) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001197 return RMI_ERROR_INPUT;
1198 }
1199
1200 if (!find_lock_two_granules(rd_addr,
1201 GRANULE_STATE_RD,
1202 &g_rd,
1203 rec_addr,
1204 GRANULE_STATE_REC,
1205 &g_rec)) {
1206 return RMI_ERROR_INPUT;
1207 }
1208
1209 if (granule_refcount_read_acquire(g_rec) != 0UL) {
AlexeiFedorov892abce2023-04-06 16:32:12 +01001210 ret = RMI_ERROR_REC;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001211 goto out_unlock_rec_rd;
1212 }
1213
1214 rec = granule_map(g_rec, SLOT_REC);
1215
1216 if (g_rd != rec->realm_info.g_rd) {
1217 ret = RMI_ERROR_REC;
1218 goto out_unmap_rec;
1219 }
1220
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001221 if (ripas_val != rec->set_ripas.ripas_val) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001222 ret = RMI_ERROR_INPUT;
1223 goto out_unmap_rec;
1224 }
1225
1226 if (map_addr != rec->set_ripas.addr) {
1227 /* Target region is not next chunk of requested region */
1228 ret = RMI_ERROR_INPUT;
1229 goto out_unmap_rec;
1230 }
1231
1232 rd = granule_map(g_rd, SLOT_RD);
1233
1234 if (!validate_rtt_entry_cmds(map_addr, level, rd)) {
1235 ret = RMI_ERROR_INPUT;
1236 goto out_unmap_rd;
1237 }
1238
1239 map_size = s2tte_map_size(level);
1240 if (map_addr + map_size > rec->set_ripas.end) {
1241 /* Target region extends beyond end of requested region */
1242 ret = RMI_ERROR_INPUT;
1243 goto out_unmap_rd;
1244 }
1245
1246 g_rtt_root = rd->s2_ctx.g_rtt;
1247 sl = realm_rtt_starting_level(rd);
1248 ipa_bits = realm_ipa_bits(rd);
1249 s2_ctx = rd->s2_ctx;
1250
1251 granule_lock(g_rtt_root, GRANULE_STATE_RTT);
1252
1253 rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits,
1254 map_addr, level, &wi);
1255 if (wi.last_level != level) {
1256 ret = pack_return_code(RMI_ERROR_RTT, wi.last_level);
1257 goto out_unlock_llt;
1258 }
1259
1260 s2tt = granule_map(wi.g_llt, SLOT_RTT);
1261 s2tte = s2tte_read(&s2tt[wi.index]);
1262
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001263 if (!update_ripas(&s2tte, &tlbi_required, level, ripas_val)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001264 ret = pack_return_code(RMI_ERROR_RTT, (unsigned int)level);
1265 goto out_unmap_llt;
1266 }
1267
1268 s2tte_write(&s2tt[wi.index], s2tte);
1269
AlexeiFedorov0fb44552023-04-14 15:37:58 +01001270 if (tlbi_required) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001271 if (level == RTT_PAGE_LEVEL) {
1272 invalidate_page(&s2_ctx, map_addr);
1273 } else {
1274 invalidate_block(&s2_ctx, map_addr);
1275 }
1276 }
1277
1278 rec->set_ripas.addr += map_size;
1279
1280 ret = RMI_SUCCESS;
1281
1282out_unmap_llt:
1283 buffer_unmap(s2tt);
1284out_unlock_llt:
1285 granule_unlock(wi.g_llt);
1286out_unmap_rd:
1287 buffer_unmap(rd);
1288out_unmap_rec:
1289 buffer_unmap(rec);
1290out_unlock_rec_rd:
1291 granule_unlock(g_rec);
1292 granule_unlock(g_rd);
1293 return ret;
1294}